2 *rk818-battery.h - Battery fuel gauge driver structures
8 #define VB_MOD_REG 0x21
9 #define THERMAL_REG 0x22
10 #define NT_STS_MSK_REG2 0x4f
11 #define DCDC_ILMAX_REG 0x90
12 #define CHRG_COMP_REG1 0x99
13 #define CHRG_COMP_REG2 0x9A
14 #define SUP_STS_REG 0xA0
15 #define USB_CTRL_REG 0xA1
16 #define CHRG_CTRL_REG1 0xA3
17 #define CHRG_CTRL_REG2 0xA4
18 #define CHRG_CTRL_REG3 0xA5
19 #define BAT_CTRL_REG 0xA6
20 #define BAT_HTS_TS1_REG 0xA8
21 #define BAT_LTS_TS1_REG 0xA9
22 #define BAT_HTS_TS2_REG 0xAA
23 #define BAT_LTS_TS2_REG 0xAB
26 #define TS_CTRL_REG 0xAC
27 #define ADC_CTRL_REG 0xAD
29 #define ON_SOURCE 0xAE
30 #define OFF_SOURCE 0xAF
34 #define FRAME_SMP_INTERV_REG 0xB2
35 #define AUTO_SLP_CUR_THR_REG 0xB3
37 #define GASCNT_CAL_REG3 0xB4
38 #define GASCNT_CAL_REG2 0xB5
39 #define GASCNT_CAL_REG1 0xB6
40 #define GASCNT_CAL_REG0 0xB7
46 #define BAT_CUR_AVG_REGH 0xBC
47 #define BAT_CUR_AVG_REGL 0xBD
49 #define TS1_ADC_REGH 0xBE
50 #define TS1_ADC_REGL 0xBF
51 #define TS2_ADC_REGH 0xC0
52 #define TS2_ADC_REGL 0xC1
54 #define BAT_OCV_REGH 0xC2
55 #define BAT_OCV_REGL 0xC3
56 #define BAT_VOL_REGH 0xC4
57 #define BAT_VOL_REGL 0xC5
59 #define RELAX_ENTRY_THRES_REGH 0xC6
60 #define RELAX_ENTRY_THRES_REGL 0xC7
61 #define RELAX_EXIT_THRES_REGH 0xC8
62 #define RELAX_EXIT_THRES_REGL 0xC9
64 #define RELAX_VOL1_REGH 0xCA
65 #define RELAX_VOL1_REGL 0xCB
66 #define RELAX_VOL2_REGH 0xCC
67 #define RELAX_VOL2_REGL 0xCD
69 #define BAT_CUR_R_CALC_REGH 0xCE
70 #define BAT_CUR_R_CALC_REGL 0xCF
71 #define BAT_VOL_R_CALC_REGH 0xD0
72 #define BAT_VOL_R_CALC_REGL 0xD1
74 #define CAL_OFFSET_REGH 0xD2
75 #define CAL_OFFSET_REGL 0xD3
77 #define NON_ACT_TIMER_CNT_REG 0xD4
79 #define VCALIB0_REGH 0xD5
80 #define VCALIB0_REGL 0xD6
81 #define VCALIB1_REGH 0xD7
82 #define VCALIB1_REGL 0xD8
84 #define IOFFSET_REGH 0xDD
85 #define IOFFSET_REGL 0xDE
88 /*0xE0 ~0xF2 data register,*/
91 #define REMAIN_CAP_REG3 0xE1
92 #define REMAIN_CAP_REG2 0xE2
93 #define REMAIN_CAP_REG1 0xE3
94 #define REMAIN_CAP_REG0 0xE4
96 #define UPDAT_LEVE_REG 0xE5
98 #define NEW_FCC_REG3 0xE6
99 #define NEW_FCC_REG2 0xE7
100 #define NEW_FCC_REG1 0xE8
101 #define NEW_FCC_REG0 0xE9
103 #define NON_ACT_TIMER_CNT_REG_SAVE 0xEA
104 #define OCV_VOL_VALID_REG 0xEB
105 #define REBOOT_CNT_REG 0xEC
106 #define PCB_IOFFSET_REG 0xED
107 #define MISC_MARK_REG 0xEE
109 #define PLUG_IN_INT (0)
110 #define PLUG_OUT_INT (1)
112 /* gasgauge module enable bit 0: disable 1:enabsle
118 if GG_EN = 0 , then the ADC of BAT voltage controlled by the
119 bit 0:diabsle 1:enable
121 #define ADC_VOL_EN (1<<7)
123 if GG_EN = 0, then the ADC of BAT current controlled by the
124 bit 0: disable 1: enable
126 #define ADC_CUR_EN (1<<6)
127 /*the ADC of TS1 controlled by the bit 0:disabsle 1:enable */
128 #define ADC_TS1_EN (1<<5)
129 /*the ADC of TS2 controlled by the bit 0:disabsle 1:enable */
130 #define ADC_TS2_EN (1<<4)
131 /*ADC colock phase 0:normal 1:inverted*/
132 #define ADC_PHASE (1<<3)
133 #define ADC_CLK_SEL 7
134 /*****************************************************
135 #define ADC_CLK_SEL_2M 0x000
136 #define ADC_CLK_SEL_1M 0x001
137 #define ADC_CLK_SEL_500K 0x002
138 #define ADC_CLK_SEL_250K 0x003
139 #define ADC_CLK_SEL_125K 0x004
140 ******************************************************/
142 /* ADC bat current continue sample times 00:8 01:16 10:32 11:64*/
143 #define CUR_SAMPL_CON_TIMES (3<<6)
144 /*ADC offset calibreation interval time 00:8min 01:16min 10:32min 11:48min*/
145 #define ADC_OFF_CAL_INTERV (3<<4)
146 /*OCV sampling interval time 00:8min 01:16min 10:32min :11:48min*/
147 #define OCV_SAMPL_INTERV (3<<2)
149 /*ADC working in current voltage collection mode*/
150 #define ADC_CUR_VOL_MODE (1<<1)
151 /*ADC working in resistor calculation mode 0:disable 1:enable*/
152 #define ADC_RES_MODE 1
155 /*average current filter times 00:1/2 01:1/4 10:1/8 11:1/16*/
156 #define RES_CUR_AVG_SEL (3<<5)
157 /*battery first connection,edge trigger 0:NOT 1:YES*/
158 #define BAT_CON (1<<4)
159 /*battery voltage1 update in relax status 0: NOT 1:YE*/
160 #define RELAX_VOL1_UPD (1<<3)
161 /*battery voltage2 update in relax status 0: NOT 1:YE*/
162 #define RELAX_VOL2_UPD (1<<2)
163 /*battery coming into relax status 0: NOT 1:YE*/
164 #define RELAX_STS (1<<1)
165 /*battery average voltage and current updated status 0: NOT 1:YES*/
166 #define IV_AVG_UPD_STS (1<<0)
168 /*FRAME_SMP_INTERV_REG*/
169 #define AUTO_SLP_EN (1<<5)
170 /* auto sleep mode 0:disable 1:enable*/
171 #define FRAME_SMP_INTERV_TIME 0x1F
174 #define PLUG_IN_STS (1<<6)
177 #define BAT_EXS (1<<7)
178 #define CHARGE_OFF (0x00<<4)
179 #define DEAD_CHARGE (0x01<<4)
180 #define TRICKLE_CHARGE (0x02<<4)
181 #define CC_OR_CV (0x03<<4)
182 #define CHARGE_FINISH (0x04<<4)
183 #define USB_OVER_VOL (0x05<<4)
184 #define BAT_TMP_ERR (0x06<<4)
185 #define TIMER_ERR (0x07<<4)
187 #define USB_EXIST (1<<1)
188 /* usb is effective*/
189 #define USB_EFF (1<<0)
192 #define CHRG_CT_EN (1<<7)
195 #define VLIM_4000MV (0x00<<4)
196 #define VLIM_4100MV (0x01<<4)
197 #define VLIM_4200MV (0x02<<4)
198 #define VLIM_4300MV (0x03<<4)
199 #define VLIM_4400MV (0x04<<4)
200 #define VLIM_4500MV (0x05<<4)
201 #define VLIM_4600MV (0x06<<4)
202 #define VLIM_4700MV (0x07<<4)
206 #define ILIM_450MA (0x00)
207 #define ILIM_800MA (0x01)
208 #define ILIM_850MA (0x02)
209 #define ILIM_1000MA (0x03)
210 #define ILIM_1250MA (0x04)
211 #define ILIM_1500MA (0x05)
212 #define ILIM_1750MA (0x06)
213 #define ILIM_2000MA (0x07)
214 #define ILIM_2250MA (0x08)
215 #define ILIM_2500MA (0x09)
216 #define ILIM_2750MA (0x0A)
217 #define ILIM_3000MA (0x0B)
221 #define CHRG_EN (0x01<<7)
224 #define CHRG_VOL4050 (0x00<<4)
225 #define CHRG_VOL4100 (0x01<<4)
226 #define CHRG_VOL4150 (0x02<<4)
227 #define CHRG_VOL4200 (0x03<<4)
228 #define CHRG_VOL4300 (0x04<<4)
229 #define CHRG_VOL4350 (0x05<<4)
232 #define CHRG_CUR1000mA (0x00)
233 #define CHRG_CUR1200mA (0x01)
234 #define CHRG_CUR1400mA (0x02)
235 #define CHRG_CUR1600mA (0x03)
236 #define CHRG_CUR1800mA (0x04)
237 #define CHRG_CUR2000mA (0x05)
238 #define CHRG_CUR2200mA (0x06)
239 #define CHRG_CUR2400mA (0x07)
240 #define CHRG_CUR2600mA (0x08)
241 #define CHRG_CUR2800mA (0x09)
242 #define CHRG_CUR3000mA (0x0A)
245 #define FINISH_100MA (0x00<<6)
246 #define FINISH_150MA (0x01<<6)
247 #define FINISH_200MA (0x02<<6)
248 #define FINISH_250MA (0x03<<6)
250 /*temp feed back degree*/
251 #define TEMP_85C (0x00 << 2)
252 #define TEMP_95C (0x01 << 2)
253 #define TEMP_105C (0x02 << 2)
254 #define TEMP_115C (0x03 << 2)
258 #define CHRG_TERM_ANA_SIGNAL (0 << 5)
259 #define CHRG_TERM_DIG_SIGNAL (1 << 5)
260 #define CHRG_TIMER_CCCV_EN (1 << 2)
263 #define CHG_CCCV_4HOUR (0x00)
264 #define CHG_CCCV_5HOUR (0x01)
265 #define CHG_CCCV_6HOUR (0x02)
266 #define CHG_CCCV_8HOUR (0x03)
267 #define CHG_CCCV_10HOUR (0x04)
268 #define CHG_CCCV_12HOUR (0x05)
269 #define CHG_CCCV_14HOUR (0x06)
270 #define CHG_CCCV_16HOUR (0x07)
273 #define SAMP_TIME_8MIN (0X00<<4)
274 #define SAMP_TIME_16MIN (0X01<<4)
275 #define SAMP_TIME_32MIN (0X02<<4)
276 #define SAMP_TIME_48MIN (0X03<<4)
278 #define ADC_CURRENT_MODE (1 << 1)
279 #define ADC_VOLTAGE_MODE (0 << 1)
281 #define DRIVER_VERSION "4.0.0"
282 #define ROLEX_SPEED (100 * 1000)
284 #define CHARGING 0x01
285 #define DISCHARGING 0x00
287 #define TIMER_MS_COUNTS 1000
288 #define MAX_CHAR 0x7F
289 #define MAX_UNSIGNED_CHAR 0xFF
290 #define MAX_INT 0x7FFF
291 #define MAX_UNSIGNED_INT 0xFFFF
292 #define MAX_INT8 0x7F
293 #define MAX_UINT8 0xFF
295 /* Gas Gauge Constatnts */
297 #define MAX_CAPACITY 0x7fff
299 #define MAX_PERCENTAGE 100
301 /* Num, cycles with no Learning, after this many cycles, the gauge
302 start adjusting FCC, based on Estimated Cell Degradation */
303 #define NO_LEARNING_CYCLES 25
304 /* Size of the OCV Lookup table */
305 #define OCV_TABLE_SIZE 21
310 /*voltage_diff, current_diff: Maximal allowed deviation
311 of the voltage and the current from one reading to the
312 next that allows the fuel gauge to apply an OCV correction.
313 The main purpose of these thresholds is to filter current
314 and voltage spikes. Recommended value: these value are
315 highly depend on the load nature. if the load creates a lot
316 of current spikes .the value may need to be increase*/
317 uint8_t voltage_diff;
318 uint8_t current_diff;
319 /* sleep_enter_current: if the current remains under
320 this threshold for [sleep_enter_samples]
321 consecutive samples. the gauge enters the SLEEP MODE*/
322 uint16_t sleep_enter_current;
323 /*sleep_enter_samples: the number of samples that
324 satis fy asleep enter or exit condition in order
325 to actually enter of exit SLEEP mode*/
326 uint8_t sleep_enter_samples;
327 /*sleep_exit_samples: to exit SLEEP mode , average
328 current should pass this threshold first. then
329 current should remain above this threshold for
330 [sleep_exit_samples] consecutive samples*/
331 uint16_t sleep_exit_current;
332 /*sleep_exit_samples: to exit SLEEP mode, average
333 current should pass this threshold first, then current
334 should remain above this threshold for [sleep_exit_samples]
335 consecutive samples.*/
336 uint8_t sleep_exit_samples;
337 /*relax_period: defines the number of seconds the
338 fuel gauge should spend in the SLEEP mode
339 before entering the OCV mode, this setting makes
340 the gauge wait for a cell voltage recovery after
341 a charge or discharge operation*/
342 uint16_t relax_period;
343 /* flat_zone_low : flat_zone_high :if soc falls into
344 the flat zone low% - flat zone high %.the fuel gauge
345 wait for a cell voltage recovery after a charge or
346 discharge operation.*/
347 uint8_t flat_zone_low;
348 uint8_t flat_zone_high;
349 /*FCC leaning is disqualified if the discharge capacity
350 in the OCV mode is greater than this threshold*/
351 uint16_t max_ocv_discharge;
352 /*the 21-point OCV table*/
353 uint16_t table[OCV_TABLE_SIZE];
363 /* EDV Point tracking data */
367 int16_t min_capacity;
371 /* EDV Configuration */
373 /*avieraging: True = evokes averaging on voltage
374 reading to detect an EDV condition.
375 False = no averaging of voltage readings to detect an
378 /*sequential_edv: the sequential_edv setting defines
379 how many times in a row the battery should
380 pass the EDV threshold to detect an EDV condition.
381 this setting is intended to fiter short voltage spikes
382 cause by current spikes*/
383 uint8_t sequential_edv;
384 /*filter_light: difine the calculated EDV voltage
385 recovery IIR filter strength
386 light-lsetting : for light load (below Qmax/5)
387 heavy setting : for ligh load (above Qmax/5)
388 the filter is applied only if the load is greater than
389 Qmax/3. if average = True. then the Qmax/5 threshold
390 is compared to averge current.otherwise it is compared
392 Recommended value: 15-255. 255---disabsle the filter
394 uint8_t filter_light;
395 uint8_t filter_heavy;
396 /*overload_current: the current level above which an
397 EDV condition will not be detected and
398 capacity not reconciled*/
399 int16_t overload_current;
401 struct edv_point edv[3];
402 /*edv: the end-of-discharge voltage-to-capactiy
403 correlation points.*/
404 /*struct edv_point *edv;*/
407 /* General Battery Cell Gauging Configuration */
409 bool cc_polarity; /*To Be Determined*/
411 /*ocv_below_edv1: if set (True), OCV correction allowed
414 /*cc_voltage: the charge complete voltage threshold(e.g. 4.2v)
415 of the battery. charge cannot be considered complete if the
416 battery voltage is below this threshold*/
418 /*cc_current:the charge complete current threshold(e.g. c/20).
419 charge cannot be considered complete when charge
420 current and average current are greater than this threshold*/
422 /*design_capacity: design capacity of the battery.
423 the battery datasheet should provide this value*/
424 uint16_t design_capacity;
425 /*design_qmax: the calculated discharge capacity of
426 the OCV discharge curve*/
428 /*r_sense: the resistance of the current sence element.
429 the sense resistor needs to be slelected to
430 ensure accurate current measuremen and integration
431 at currents >OFF consumption*/
433 /*qmax_adjust: the value decremented from QMAX
434 every cycle for aging compensation.*/
436 /*fcc_adjust: the value decremented from the FCC
437 when no learning happen for 25 cycles in a row*/
439 /*max_overcharge: the fuel gauge tracks the capacity
440 that goes into the battery after a termination
441 condition is detected. this improve gauging accuracy
442 if the charger's charge termination condition does't
443 match to the fuel gauge charge termination condition.*/
444 uint16_t max_overcharge;
445 /*electronics_load: the current that the system consumes
446 int the OFF mode(MPU low power, screen OFF)*/
447 uint16_t electronics_load;
448 /*max_increment: the maximum increment of FCC if the
449 learned capacity is much greater than the exiting
450 FCC. recommentded value 150mAh*/
451 int16_t max_increment;
452 /*max_decrement: the maximum increment of FCC if the
453 learned capacity is much lower than the exiting FCC*/
454 int16_t max_decrement;
455 /*low_temp: the correlation between voltage and remaining
456 capacity is considered inaccurate below this temperature.
457 any leaning will be disqualified, if the battery temperature
458 is below this threshold
461 /*deep_dsg_voltage:in order to qualify capacity learning on
462 the discharge, the battery voltage should
463 be within EDV-deep-dsg_voltage and EDV.*/
464 uint16_t deep_dsg_voltage;
466 max_dsg_voltage:limits the amount of the estimated
467 discharge when learning is in progress. if the amount of
468 the capacity estimation get greater than this threshold,
469 the learning gets disqualified
471 uint16_t max_dsg_estimate;
473 light_load: FCC learning on discharge disqualifies if
474 the load is below this threshold when the
475 when EDV2 is reached.
479 near_full: this defines a capacity zone from FCC
480 to FCC - near_full. A discharge cycles start
481 from this capacity zone qualifies for FCC larning.
485 cycle_threshold: the amount of capacity that should
486 be dicharged from the battery to increment the cycle
487 count by 1.cycle counting happens on the discharge only.
489 uint16_t cycle_threshold;
490 /*recharge: the voltage of recharge.*/
493 mode_swtich_capacity: this defines how much capacity
494 should pass through the coulomb counter to cause a cycle
495 count start condition (either charge or discharge). the gauge
496 support 2 cycle typeds.charge and discharge. a cycle starts
497 when mode_switch_capacity passes through the coulomb counter
498 the cycle get canceled and switches to the opposite direciton
499 if mode_switch_capacity passes though
500 the coulomb counter in oppositer direciton.
502 uint8_t mode_switch_capacity;
503 /*call_period: approximate time between fuel gauge calls.*/
506 struct ocv_config *ocv;
507 struct edv_config *edv;
512 light-load: ( < C/40)
517 SOC : state-of-charge of the battery in %,it represents
518 the % full of the battery from the system empty voltage.
519 SOC = NAC/FCC, SOC = 1 -DOD
523 nac :nominal avaiable charge of the battery in mAh.
524 it represents the present remain capacity of the battery
525 to the system empty voltage under nominal conditions
529 fcc: full battery capacity .this represents the discharge capacity
530 of the battery from the defined full condition to the system empty
531 voltage(EDV0) under nominal conditions.the value is learned by
532 the algorithm on qualified charge and discharge cycleds
535 /* qmax: the battery capacity(mAh) at the OCV curve discharge rate*/
545 cycle_count: it represents how many charge or discharge
546 cycles a battery has experience. this is used to estimate the
547 change of impedance of the battery due to "aging"
551 sleep : in this mode ,the battery fuel gauge is counting
552 discharge with the coulomb counter and checking for the
553 battery relaxed condition, if a relaxed battery is destected
554 the fuel gauge enters OCV mode
574 struct timeval sleep_timer;
575 struct timeval el_sleep_timer;
576 uint16_t cumulative_sleep;
581 int16_t learn_offset;
582 uint16_t learned_cycle;
587 int16_t overcharge_q;
588 int16_t charge_cycle_q;
589 int16_t discharge_cycle_q;
591 uint8_t sequential_cc;
592 uint8_t sleep_samples;
593 uint8_t sequential_edvs;
595 uint16_t electronics_load;
596 uint16_t cycle_dsg_estimate;
598 struct edv_state edv;
603 struct cell_config *config;
606 struct battery_platform_data {
607 int *battery_tmp_tbl;
608 unsigned int tblsize;
610 unsigned int ocv_size;
612 unsigned int monitoring_interval;
613 unsigned int max_charger_ilimitmA;
614 unsigned int max_charger_currentmA;
615 unsigned int max_charger_voltagemV;
616 unsigned int termination_currentmA;
618 unsigned int max_bat_voltagemV;
619 unsigned int low_bat_voltagemV;
620 unsigned int chrg_diff_vol;
621 unsigned int power_off_thresd;
622 unsigned int sense_resistor_mohm;
625 unsigned long features;
626 unsigned long errata;
628 struct cell_config *cell_cfg;
632 FG_NORMAL_MODE = 0,/*work normally*/
633 TEST_POWER_MODE, /*work without battery*/
636 enum hw_support_adp {
637 HW_ADP_TYPE_USB = 0,/*'HW' means:hardware*/
643 /* don't change the following ID, they depend on usb check
644 * interface: dwc_otg_check_dpdm()
659 void kernel_power_off(void);
660 #if defined(CONFIG_ARCH_ROCKCHIP)
661 int dwc_vbus_status(void);
662 int get_gadget_connect_flag(void);
663 void rk_send_wakeup_key(void);
666 static inline int get_gadget_connect_flag(void)
671 static inline int dwc_otg_check_dpdm(bool wait)
676 static inline void rk_send_wakeup_key(void)