1 /* 2 * rk816_battery.h: fuel gauge driver structures 3 * 4 * Copyright (C) 2016 Rockchip Electronics Co., Ltd 5 * Author: chenjh <chenjh@rock-chips.com> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms and conditions of the GNU General Public License, 9 * version 2, as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 */ 16 17 #ifndef RK816_BATTERY 18 #define RK816_BATTERY 19 20 /*TS_CTRL_REG*/ 21 #define GG_EN BIT(7) 22 #define ADC_CUR_EN BIT(6) 23 #define ADC_TS1_EN BIT(5) 24 #define ADC_TS2_EN BIT(4) 25 26 /*GGCON*/ 27 #define ADC_CUR_MODE (0x01 << 1) 28 #define AVG_CUR_MODE (0x00 << 0) 29 #define ADC_CAL_MIN_MSK 0x30 30 #define ADC_CAL_8MIN (0x00 << 4) 31 #define OCV_SAMP_MIN_MSK 0x0c 32 #define OCV_SAMP_8MIN (0x00 << 2) 33 34 /*GGSTS*/ 35 #define FCC_LOCK (1 << 5) 36 #define BAT_CON (1 << 4) 37 #define RELAX_STS (1 << 1) 38 #define RELAX_VOL1_UPD BIT(3) 39 #define RELAX_VOL2_UPD BIT(2) 40 #define RELAX_VOL12_UPD_MSK (RELAX_VOL1_UPD | RELAX_VOL2_UPD) 41 42 /*SUP_STS_REG*/ 43 #define BAT_EXS (1 << 7) 44 #define CHARGE_OFF (0x00 << 4) 45 #define DEAD_CHARGE (0x01 << 4) 46 #define TRICKLE_CHARGE (0x02 << 4) 47 #define CC_OR_CV (0x03 << 4) 48 #define CHARGE_FINISH (0x04 << 4) 49 #define USB_OVER_VOL (0x05 << 4) 50 #define BAT_TMP_ERR (0x06 << 4) 51 #define TIMER_ERR (0x07 << 4) 52 #define USB_EXIST (1 << 1) 53 #define USB_EFF (1 << 0) 54 #define USB_VLIMIT_EN BIT(3) 55 #define USB_CLIMIT_EN BIT(2) 56 #define CHRG_STATUS_MSK 0x70 57 58 /*USB_CTRL_REG*/ 59 #define CHRG_CT_EN (1 << 7) 60 #define INPUT_CUR_MSK (0x0f) 61 #define FINISH_CUR_MSK 0xc0 62 #define CHRG_CUR_MSK (0x0f) 63 64 /* BAT_CTRL_REG */ 65 #define USB_SYS_EN BIT(6) 66 67 /* THERMAL_REG */ 68 #define FB_TEMP_MSK 0x0c 69 #define HOTDIE_STS BIT(1) 70 71 /*CHGR_CUR_INPUT*/ 72 #define INPUT_CUR450MA (0x00) 73 #define INPUT_CUR80MA (0x01) 74 #define INPUT_CUR850MA (0x02) 75 #define INPUT_CUR1000MA (0x03) 76 #define INPUT_CUR1250MA (0x04) 77 #define INPUT_CUR1500MA (0x05) 78 #define INPUT_CUR1750MA (0x06) 79 #define INPUT_CUR2000MA (0x07) 80 #define INPUT_CUR2250MA (0x08) 81 #define INPUT_CUR2500MA (0x09) 82 #define INPUT_CUR2750MA (0x0A) 83 #define INPUT_CUR3000MA (0x0B) 84 85 /*CHRG_VOL_SEL*/ 86 #define CHRG_VOL4050MV (0x00 << 4) 87 #define CHRG_VOL4100MV (0x01 << 4) 88 #define CHRG_VOL4150MV (0x02 << 4) 89 #define CHRG_VOL4200MV (0x03 << 4) 90 #define CHRG_VOL4300MV (0x04 << 4) 91 #define CHRG_VOL4350MV (0x05 << 4) 92 93 /*CHRG_CUR_SEL*/ 94 #define CHRG_CUR1000MA (0x00) 95 #define CHRG_CUR1200MA (0x01) 96 #define CHRG_CUR1400MA (0x02) 97 #define CHRG_CUR1600MA (0x03) 98 #define CHRG_CUR1800MA (0x04) 99 #define CHRG_CUR2000MA (0x05) 100 #define CHRG_CUR2200MA (0x06) 101 #define CHRG_CUR2400MA (0x07) 102 #define CHRG_CUR2600MA (0x08) 103 #define CHRG_CUR2800MA (0x09) 104 #define CHRG_CUR3000MA (0x0A) 105 106 /*THREAML_REG*/ 107 #define TEMP_85C (0x00 << 2) 108 #define TEMP_95C (0x01 << 2) 109 #define TEMP_105C (0x02 << 2) 110 #define TEMP_115C (0x03 << 2) 111 112 /*CHRG_CTRL_REG2*/ 113 #define CHG_CCCV_4HOUR (0x00) 114 #define CHG_CCCV_5HOUR (0x01) 115 #define CHG_CCCV_6HOUR (0x02) 116 #define CHG_CCCV_8HOUR (0x03) 117 #define CHG_CCCV_10HOUR (0x04) 118 #define CHG_CCCV_12HOUR (0x05) 119 #define CHG_CCCV_14HOUR (0x06) 120 #define CHG_CCCV_16HOUR (0x07) 121 #define FINISH_100MA (0x00 << 6) 122 #define FINISH_150MA (0x01 << 6) 123 #define FINISH_200MA (0x02 << 6) 124 #define FINISH_250MA (0x03 << 6) 125 126 /*CHRG_CTRL_REG3*/ 127 #define CHRG_TERM_ANA_SIGNAL (0 << 5) 128 #define CHRG_TERM_DIG_SIGNAL (1 << 5) 129 #define CHRG_TIMER_CCCV_EN (1 << 2) 130 #define CHRG_EN (1 << 7) 131 132 #define BOOST_OTG_MASK ((0x3 << 5) | (0x3 << 1)) 133 #define BOOST_OTG_ON ((0x3 << 5) | (0x3 << 1)) 134 #define BOOST_OTG_OFF ((0x3 << 5) | (0x0 << 1)) 135 #define BOOST_OFF_OTG_ON ((0x3 << 5) | (0x2 << 1)) 136 137 #define OTG_BOOST_SLP_ON (0x3 << 5) 138 139 /* MISC_MARK_REG */ 140 #define FG_INIT BIT(3) 141 #define FG_RESET_LATE BIT(1) 142 #define FG_RESET_NOW BIT(0) 143 #define ALGO_REST_MODE_MSK (0xc0) 144 #define ALGO_REST_MODE_SHIFT 6 145 146 #define FB_TEMP_SHIFT 2 147 #define CHRG_VOL_SEL_SHIFT 4 148 #define CHRG_CRU_INPUT_SHIFT 0 149 #define CHRG_CRU_SEL_SHIFT 0 150 #define CHRG_CCCV_HOUR_SHIFT 0 151 #define OCV_CALIB_SHIFT (1 << 1) 152 #define PLUG_IN_STS (1 << 6) 153 154 #define DRIVER_VERSION "1.2" 155 #define TIMER_MS_COUNTS 1000 156 #define MAX_PERCENTAGE 100 157 #define MAX_INT 0x7FFF 158 #define MAX_INTERPOLATE 1000 159 160 struct temp_chrg_table { 161 int temp_down; 162 int temp_up; 163 u32 chrg_current; 164 u8 set_chrg_current; 165 }; 166 167 struct battery_platform_data { 168 u32 *ocv_table; 169 u32 *zero_table; 170 struct temp_chrg_table *tc_table; 171 u32 tc_count; 172 173 u32 table_t[4][21]; 174 int temp_t[4]; 175 u32 temp_t_num; 176 177 u32 *ntc_table; 178 u32 ocv_size; 179 u32 ntc_size; 180 int ntc_degree_from; 181 u32 ntc_factor; 182 u32 max_input_current; 183 u32 max_chrg_current; 184 u32 max_chrg_voltage; 185 u32 pwroff_vol; 186 u32 monitor_sec; 187 u32 zero_algorithm_vol; 188 u32 zero_reserve_dsoc; 189 u32 bat_res; 190 u32 design_capacity; 191 u32 design_qmax; 192 u32 sleep_enter_current; 193 u32 sleep_exit_current; 194 u32 sleep_filter_current; 195 u32 power_dc2otg; 196 u32 max_soc_offset; 197 u32 bat_mode; 198 u32 fb_temp; 199 u32 energy_mode; 200 u32 cccv_hour; 201 u32 dc_det_adc; 202 int dc_det_pin; 203 u8 dc_det_level; 204 int otg5v_suspend_enable; 205 u32 sample_res; 206 bool extcon; 207 }; 208 209 enum work_mode { 210 MODE_ZERO = 0, 211 MODE_FINISH, 212 MODE_SMOOTH_CHRG, 213 MODE_SMOOTH_DISCHRG, 214 MODE_SMOOTH, 215 }; 216 217 enum bat_mode { 218 MODE_BATTARY = 0, 219 MODE_VIRTUAL, 220 }; 221 222 enum charger_t { 223 USB_TYPE_UNKNOWN_CHARGER, 224 USB_TYPE_NONE_CHARGER, 225 USB_TYPE_USB_CHARGER, 226 USB_TYPE_AC_CHARGER, 227 USB_TYPE_CDP_CHARGER, 228 DC_TYPE_DC_CHARGER, 229 DC_TYPE_NONE_CHARGER, 230 }; 231 232 enum charger_state { 233 OFFLINE = 0, 234 ONLINE 235 }; 236 237 static const u16 FEED_BACK_TEMP[] = { 238 85, 95, 105, 115 239 }; 240 241 static const u16 CHRG_VOL_SEL[] = { 242 4050, 4100, 4150, 4200, 4250, 4300, 4350 243 }; 244 245 /* 246 * If sample resistor changes, we need caculate a new CHRG_CUR_SEL[] table. 247 * 248 * Calculation method: 249 * 1. find 20mR(default) current charge table, that is: 250 * 20mR: [1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400] 251 * 252 * 2. caculate Rfac(not care much, just using it) by sample resistor(ie. Rsam); 253 * Rsam = 20mR: Rfac = 10; 254 * Rsam > 20mR: Rfac = Rsam * 10 / 20; 255 * Rsam < 20mR: Rfac = 20 * 10 / Rsam; 256 * 257 * 3. from step2, we get Rfac, then we can get new charge current table by 20mR 258 * charge table: 259 * Iorg: member from 20mR charge table; Inew: new member for charge table. 260 * 261 * Rsam > 20mR: Inew = Iorg * 10 / Rfac; 262 * Rsam < 20mR: Inew = Iorg * Rfac / 10; 263 * 264 * Notice: Inew should round up if it is not a integer!!! 265 * 266 * Example: 267 * 10mR: [2000, 2400, 2800, 3200, 3600, 4000, 4500, 4800] 268 * 20mR: [1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400] 269 * 40mR: [500, 600, 700, 800, 900, 1000, 1125, 1200] 270 * 50mR: [400, 480, 560, 640, 720, 800, 900, 960] 271 * 60mR: [334, 400, 467, 534, 600, 667, 750, 800] 272 * 273 * You should add property 'sample_res = <Rsam>' at battery node. 274 */ 275 276 static const u16 CHRG_CUR_SEL[] = { 277 1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400 278 }; 279 280 static const u16 CHRG_CUR_INPUT[] = { 281 450, 80, 850, 1000, 1250, 1500, 1750, 2000 282 }; 283 284 void kernel_power_off(void); 285 286 #endif 287