2 *rk818-battery.h - Battery fuel gauge driver structures
7 #include <linux/time.h>
9 #define VB_MOD_REG 0x21
10 #define THERMAL_REG 0x22
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
110 /* gasgauge module enable bit 0: disable 1:enabsle
116 if GG_EN = 0 , then the ADC of BAT voltage controlled by the
117 bit 0:diabsle 1:enable
119 #define ADC_VOL_EN (1<<7)
121 if GG_EN = 0, then the ADC of BAT current controlled by the
122 bit 0: disable 1: enable
124 #define ADC_CUR_EN (1<<6)
125 /*the ADC of TS1 controlled by the bit 0:disabsle 1:enable */
126 #define ADC_TS1_EN (1<<5)
127 /*the ADC of TS2 controlled by the bit 0:disabsle 1:enable */
128 #define ADC_TS2_EN (1<<4)
129 /*ADC colock phase 0:normal 1:inverted*/
130 #define ADC_PHASE (1<<3)
131 #define ADC_CLK_SEL 7
132 /*****************************************************
133 #define ADC_CLK_SEL_2M 0x000
134 #define ADC_CLK_SEL_1M 0x001
135 #define ADC_CLK_SEL_500K 0x002
136 #define ADC_CLK_SEL_250K 0x003
137 #define ADC_CLK_SEL_125K 0x004
138 ******************************************************/
140 /* ADC bat current continue sample times 00:8 01:16 10:32 11:64*/
141 #define CUR_SAMPL_CON_TIMES (3<<6)
142 /*ADC offset calibreation interval time 00:8min 01:16min 10:32min 11:48min*/
143 #define ADC_OFF_CAL_INTERV (3<<4)
144 /*OCV sampling interval time 00:8min 01:16min 10:32min :11:48min*/
145 #define OCV_SAMPL_INTERV (3<<2)
147 /*ADC working in current voltage collection mode*/
148 #define ADC_CUR_VOL_MODE (1<<1)
149 /*ADC working in resistor calculation mode 0:disable 1:enable*/
150 #define ADC_RES_MODE 1
153 /*average current filter times 00:1/2 01:1/4 10:1/8 11:1/16*/
154 #define RES_CUR_AVG_SEL (3<<5)
155 /*battery first connection,edge trigger 0:NOT 1:YES*/
156 #define BAT_CON (1<<4)
157 /*battery voltage1 update in relax status 0: NOT 1:YE*/
158 #define RELAX_VOL1_UPD (1<<3)
159 /*battery voltage2 update in relax status 0: NOT 1:YE*/
160 #define RELAX_VOL2_UPD (1<<2)
161 /*battery coming into relax status 0: NOT 1:YE*/
162 #define RELAX_STS (1<<1)
163 /*battery average voltage and current updated status 0: NOT 1:YES*/
164 #define IV_AVG_UPD_STS (1<<0)
166 /*FRAME_SMP_INTERV_REG*/
167 #define AUTO_SLP_EN (1<<5)
168 /* auto sleep mode 0:disable 1:enable*/
169 #define FRAME_SMP_INTERV_TIME 0x1F
172 #define PLUG_IN_STS (1<<6)
175 #define BAT_EXS (1<<7)
176 #define CHARGE_OFF (0x00<<4)
177 #define DEAD_CHARGE (0x01<<4)
178 #define TRICKLE_CHARGE (0x02<<4)
179 #define CC_OR_CV (0x03<<4)
180 #define CHARGE_FINISH (0x04<<4)
181 #define USB_OVER_VOL (0x05<<4)
182 #define BAT_TMP_ERR (0x06<<4)
183 #define TIMER_ERR (0x07<<4)
185 #define USB_EXIST (1<<1)
186 /* usb is effective*/
187 #define USB_EFF (1<<0)
190 #define CHRG_CT_EN (1<<7)
193 #define VLIM_4000MV (0x00<<4)
194 #define VLIM_4100MV (0x01<<4)
195 #define VLIM_4200MV (0x02<<4)
196 #define VLIM_4300MV (0x03<<4)
197 #define VLIM_4400MV (0x04<<4)
198 #define VLIM_4500MV (0x05<<4)
199 #define VLIM_4600MV (0x06<<4)
200 #define VLIM_4700MV (0x07<<4)
204 #define ILIM_450MA (0x00)
205 #define ILIM_800MA (0x01)
206 #define ILIM_850MA (0x02)
207 #define ILIM_1000MA (0x03)
208 #define ILIM_1250MA (0x04)
209 #define ILIM_1500MA (0x05)
210 #define ILIM_1750MA (0x06)
211 #define ILIM_2000MA (0x07)
212 #define ILIM_2250MA (0x08)
213 #define ILIM_2500MA (0x09)
214 #define ILIM_2750MA (0x0A)
215 #define ILIM_3000MA (0x0B)
219 #define CHRG_EN (0x01<<7)
222 #define CHRG_VOL4050 (0x00<<4)
223 #define CHRG_VOL4100 (0x01<<4)
224 #define CHRG_VOL4150 (0x02<<4)
225 #define CHRG_VOL4200 (0x03<<4)
226 #define CHRG_VOL4300 (0x04<<4)
227 #define CHRG_VOL4350 (0x05<<4)
230 #define CHRG_CUR1000mA (0x00)
231 #define CHRG_CUR1200mA (0x01)
232 #define CHRG_CUR1400mA (0x02)
233 #define CHRG_CUR1600mA (0x03)
234 #define CHRG_CUR1800mA (0x04)
235 #define CHRG_CUR2000mA (0x05)
236 #define CHRG_CUR2200mA (0x06)
237 #define CHRG_CUR2400mA (0x07)
238 #define CHRG_CUR2600mA (0x08)
239 #define CHRG_CUR2800mA (0x09)
240 #define CHRG_CUR3000mA (0x0A)
243 #define FINISH_100MA (0x00<<6)
244 #define FINISH_150MA (0x01<<6)
245 #define FINISH_200MA (0x02<<6)
246 #define FINISH_250MA (0x03<<6)
248 /*temp feed back degree*/
249 #define TEMP_85C (0x00 << 2)
250 #define TEMP_95C (0x01 << 2)
251 #define TEMP_105C (0x02 << 2)
252 #define TEMP_115C (0x03 << 2)
256 #define CHRG_TERM_ANA_SIGNAL (0 << 5)
257 #define CHRG_TERM_DIG_SIGNAL (1 << 5)
260 #define CHG_CCCV_4HOUR (0x00)
261 #define CHG_CCCV_5HOUR (0x01)
262 #define CHG_CCCV_6HOUR (0x02)
263 #define CHG_CCCV_8HOUR (0x03)
264 #define CHG_CCCV_10HOUR (0x04)
265 #define CHG_CCCV_12HOUR (0x05)
266 #define CHG_CCCV_14HOUR (0x06)
267 #define CHG_CCCV_16HOUR (0x07)
270 #define SAMP_TIME_8MIN (0X00<<4)
271 #define SAMP_TIME_16MIN (0X01<<4)
272 #define SAMP_TIME_32MIN (0X02<<4)
273 #define SAMP_TIME_48MIN (0X03<<4)
275 #define DRIVER_VERSION "3.0.0"
276 #define ROLEX_SPEED (100 * 1000)
278 #define CHARGING 0x01
279 #define DISCHARGING 0x00
281 #define TIMER_MS_COUNTS 1000
282 #define MAX_CHAR 0x7F
283 #define MAX_UNSIGNED_CHAR 0xFF
284 #define MAX_INT 0x7FFF
285 #define MAX_UNSIGNED_INT 0xFFFF
286 #define MAX_INT8 0x7F
287 #define MAX_UINT8 0xFF
289 /* Gas Gauge Constatnts */
291 #define MAX_CAPACITY 0x7fff
293 #define MAX_PERCENTAGE 100
295 /* Num, cycles with no Learning, after this many cycles, the gauge
296 start adjusting FCC, based on Estimated Cell Degradation */
297 #define NO_LEARNING_CYCLES 25
298 /* Size of the OCV Lookup table */
299 #define OCV_TABLE_SIZE 21
304 /*voltage_diff, current_diff: Maximal allowed deviation
305 of the voltage and the current from one reading to the
306 next that allows the fuel gauge to apply an OCV correction.
307 The main purpose of these thresholds is to filter current
308 and voltage spikes. Recommended value: these value are
309 highly depend on the load nature. if the load creates a lot
310 of current spikes .the value may need to be increase*/
311 uint8_t voltage_diff;
312 uint8_t current_diff;
313 /* sleep_enter_current: if the current remains under
314 this threshold for [sleep_enter_samples]
315 consecutive samples. the gauge enters the SLEEP MODE*/
316 uint16_t sleep_enter_current;
317 /*sleep_enter_samples: the number of samples that
318 satis fy asleep enter or exit condition in order
319 to actually enter of exit SLEEP mode*/
320 uint8_t sleep_enter_samples;
321 /*sleep_exit_samples: to exit SLEEP mode , average
322 current should pass this threshold first. then
323 current should remain above this threshold for
324 [sleep_exit_samples] consecutive samples*/
325 uint16_t sleep_exit_current;
326 /*sleep_exit_samples: to exit SLEEP mode, average
327 current should pass this threshold first, then current
328 should remain above this threshold for [sleep_exit_samples]
329 consecutive samples.*/
330 uint8_t sleep_exit_samples;
331 /*relax_period: defines the number of seconds the
332 fuel gauge should spend in the SLEEP mode
333 before entering the OCV mode, this setting makes
334 the gauge wait for a cell voltage recovery after
335 a charge or discharge operation*/
336 uint16_t relax_period;
337 /* flat_zone_low : flat_zone_high :if soc falls into
338 the flat zone low% - flat zone high %.the fuel gauge
339 wait for a cell voltage recovery after a charge or
340 discharge operation.*/
341 uint8_t flat_zone_low;
342 uint8_t flat_zone_high;
343 /*FCC leaning is disqualified if the discharge capacity
344 in the OCV mode is greater than this threshold*/
345 uint16_t max_ocv_discharge;
346 /*the 21-point OCV table*/
347 uint16_t table[OCV_TABLE_SIZE];
357 /* EDV Point tracking data */
361 int16_t min_capacity;
365 /* EDV Configuration */
367 /*avieraging: True = evokes averaging on voltage
368 reading to detect an EDV condition.
369 False = no averaging of voltage readings to detect an
372 /*sequential_edv: the sequential_edv setting defines
373 how many times in a row the battery should
374 pass the EDV threshold to detect an EDV condition.
375 this setting is intended to fiter short voltage spikes
376 cause by current spikes*/
377 uint8_t sequential_edv;
378 /*filter_light: difine the calculated EDV voltage
379 recovery IIR filter strength
380 light-lsetting : for light load (below Qmax/5)
381 heavy setting : for ligh load (above Qmax/5)
382 the filter is applied only if the load is greater than
383 Qmax/3. if average = True. then the Qmax/5 threshold
384 is compared to averge current.otherwise it is compared
386 Recommended value: 15-255. 255---disabsle the filter
388 uint8_t filter_light;
389 uint8_t filter_heavy;
390 /*overload_current: the current level above which an
391 EDV condition will not be detected and
392 capacity not reconciled*/
393 int16_t overload_current;
395 struct edv_point edv[3];
396 /*edv: the end-of-discharge voltage-to-capactiy
397 correlation points.*/
398 /*struct edv_point *edv;*/
401 /* General Battery Cell Gauging Configuration */
403 bool cc_polarity; /*To Be Determined*/
405 /*ocv_below_edv1: if set (True), OCV correction allowed
408 /*cc_voltage: the charge complete voltage threshold(e.g. 4.2v)
409 of the battery. charge cannot be considered complete if the
410 battery voltage is below this threshold*/
412 /*cc_current:the charge complete current threshold(e.g. c/20).
413 charge cannot be considered complete when charge
414 current and average current are greater than this threshold*/
416 /*design_capacity: design capacity of the battery.
417 the battery datasheet should provide this value*/
418 uint16_t design_capacity;
419 /*design_qmax: the calculated discharge capacity of
420 the OCV discharge curve*/
422 /*r_sense: the resistance of the current sence element.
423 the sense resistor needs to be slelected to
424 ensure accurate current measuremen and integration
425 at currents >OFF consumption*/
427 /*qmax_adjust: the value decremented from QMAX
428 every cycle for aging compensation.*/
430 /*fcc_adjust: the value decremented from the FCC
431 when no learning happen for 25 cycles in a row*/
433 /*max_overcharge: the fuel gauge tracks the capacity
434 that goes into the battery after a termination
435 condition is detected. this improve gauging accuracy
436 if the charger's charge termination condition does't
437 match to the fuel gauge charge termination condition.*/
438 uint16_t max_overcharge;
439 /*electronics_load: the current that the system consumes
440 int the OFF mode(MPU low power, screen OFF)*/
441 uint16_t electronics_load;
442 /*max_increment: the maximum increment of FCC if the
443 learned capacity is much greater than the exiting
444 FCC. recommentded value 150mAh*/
445 int16_t max_increment;
446 /*max_decrement: the maximum increment of FCC if the
447 learned capacity is much lower than the exiting FCC*/
448 int16_t max_decrement;
449 /*low_temp: the correlation between voltage and remaining
450 capacity is considered inaccurate below this temperature.
451 any leaning will be disqualified, if the battery temperature
452 is below this threshold
455 /*deep_dsg_voltage:in order to qualify capacity learning on
456 the discharge, the battery voltage should
457 be within EDV-deep-dsg_voltage and EDV.*/
458 uint16_t deep_dsg_voltage;
460 max_dsg_voltage:limits the amount of the estimated
461 discharge when learning is in progress. if the amount of
462 the capacity estimation get greater than this threshold,
463 the learning gets disqualified
465 uint16_t max_dsg_estimate;
467 light_load: FCC learning on discharge disqualifies if
468 the load is below this threshold when the
469 when EDV2 is reached.
473 near_full: this defines a capacity zone from FCC
474 to FCC - near_full. A discharge cycles start
475 from this capacity zone qualifies for FCC larning.
479 cycle_threshold: the amount of capacity that should
480 be dicharged from the battery to increment the cycle
481 count by 1.cycle counting happens on the discharge only.
483 uint16_t cycle_threshold;
484 /*recharge: the voltage of recharge.*/
487 mode_swtich_capacity: this defines how much capacity
488 should pass through the coulomb counter to cause a cycle
489 count start condition (either charge or discharge). the gauge
490 support 2 cycle typeds.charge and discharge. a cycle starts
491 when mode_switch_capacity passes through the coulomb counter
492 the cycle get canceled and switches to the opposite direciton
493 if mode_switch_capacity passes though
494 the coulomb counter in oppositer direciton.
496 uint8_t mode_switch_capacity;
497 /*call_period: approximate time between fuel gauge calls.*/
500 struct ocv_config *ocv;
501 struct edv_config *edv;
506 light-load: ( < C/40)
511 SOC : state-of-charge of the battery in %,it represents
512 the % full of the battery from the system empty voltage.
513 SOC = NAC/FCC, SOC = 1 -DOD
517 nac :nominal avaiable charge of the battery in mAh.
518 it represents the present remain capacity of the battery
519 to the system empty voltage under nominal conditions
523 fcc: full battery capacity .this represents the discharge capacity
524 of the battery from the defined full condition to the system empty
525 voltage(EDV0) under nominal conditions.the value is learned by
526 the algorithm on qualified charge and discharge cycleds
529 /* qmax: the battery capacity(mAh) at the OCV curve discharge rate*/
539 cycle_count: it represents how many charge or discharge
540 cycles a battery has experience. this is used to estimate the
541 change of impedance of the battery due to "aging"
545 sleep : in this mode ,the battery fuel gauge is counting
546 discharge with the coulomb counter and checking for the
547 battery relaxed condition, if a relaxed battery is destected
548 the fuel gauge enters OCV mode
568 struct timeval sleep_timer;
569 struct timeval el_sleep_timer;
570 uint16_t cumulative_sleep;
575 int16_t learn_offset;
576 uint16_t learned_cycle;
581 int16_t overcharge_q;
582 int16_t charge_cycle_q;
583 int16_t discharge_cycle_q;
585 uint8_t sequential_cc;
586 uint8_t sleep_samples;
587 uint8_t sequential_edvs;
589 uint16_t electronics_load;
590 uint16_t cycle_dsg_estimate;
592 struct edv_state edv;
597 struct cell_config *config;
600 struct battery_platform_data {
601 int *battery_tmp_tbl;
602 unsigned int tblsize;
604 unsigned int ocv_size;
606 unsigned int monitoring_interval;
607 unsigned int max_charger_ilimitmA;
608 unsigned int max_charger_currentmA;
609 unsigned int max_charger_voltagemV;
610 unsigned int termination_currentmA;
612 unsigned int max_bat_voltagemV;
613 unsigned int low_bat_voltagemV;
615 unsigned int sense_resistor_mohm;
618 unsigned long features;
619 unsigned long errata;
621 struct cell_config *cell_cfg;