4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/param.h>
11 #include <linux/jiffies.h>
12 #include <linux/workqueue.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include <linux/proc_fs.h>
21 #include <asm/uaccess.h>
22 #include <linux/mfd/rk818.h>
23 //#include <linux/power/rk818_battery.h>
24 #include <linux/time.h>
25 #include <linux/gpio.h>
26 #include <linux/of_gpio.h>
28 static int dbg_enable =0;
29 module_param_named(dbg_level, dbg_enable, int, 0644);
30 #define DBG( args...) \
37 #define VB_MOD_REG 0x21
39 #define CHRG_COMP_REG1 0x99
40 #define CHRG_COMP_REG2 0x9A
41 #define SUP_STS_REG 0xA0
42 #define USB_CTRL_REG 0xA1
43 #define CHRG_CTRL_REG1 0xA3
44 #define CHRG_CTRL_REG2 0xA4
45 #define CHRG_CTRL_REG3 0xA5
46 #define BAT_CTRL_REG 0xA6
47 #define BAT_HTS_TS1_REG 0xA8
48 #define BAT_LTS_TS1_REG 0xA9
49 #define BAT_HTS_TS2_REG 0xAA
50 #define BAT_LTS_TS2_REG 0xAB
53 #define TS_CTRL_REG 0xAC
54 #define ADC_CTRL_REG 0xAD
56 #define ON_SOURCE 0xAE
57 #define OFF_SOURCE 0xAF
61 #define FRAME_SMP_INTERV_REG 0xB2
62 #define AUTO_SLP_CUR_THR_REG 0xB3
64 #define GASCNT_CAL_REG3 0xB4
65 #define GASCNT_CAL_REG2 0xB5
66 #define GASCNT_CAL_REG1 0xB6
67 #define GASCNT_CAL_REG0 0xB7
73 #define BAT_CUR_AVG_REGH 0xBC
74 #define BAT_CUR_AVG_REGL 0xBD
77 #define TS1_ADC_REGH 0xBE
78 #define TS1_ADC_REGL 0xBF
79 #define TS2_ADC_REGH 0xC0
80 #define TS2_ADC_REGL 0xC1
82 #define BAT_OCV_REGH 0xC2
83 #define BAT_OCV_REGL 0xC3
84 #define BAT_VOL_REGH 0xC4
85 #define BAT_VOL_REGL 0xC5
87 #define RELAX_ENTRY_THRES_REGH 0xC6
88 #define RELAX_ENTRY_THRES_REGL 0xC7
89 #define RELAX_EXIT_THRES_REGH 0xC8
90 #define RELAX_EXIT_THRES_REGL 0xC9
92 #define RELAX_VOL1_REGH 0xCA
93 #define RELAX_VOL1_REGL 0xCB
94 #define RELAX_VOL2_REGH 0xCC
95 #define RELAX_VOL2_REGL 0xCD
97 #define BAT_CUR_R_CALC_REGH 0xCE
98 #define BAT_CUR_R_CALC_REGL 0xCF
99 #define BAT_VOL_R_CALC_REGH 0xD0
100 #define BAT_VOL_R_CALC_REGL 0xD1
102 #define CAL_OFFSET_REGH 0xD2
103 #define CAL_OFFSET_REGL 0xD3
105 #define NON_ACT_TIMER_CNT_REGL 0xD4
107 #define VCALIB0_REGH 0xD5
108 #define VCALIB0_REGL 0xD6
109 #define VCALIB1_REGH 0xD7
110 #define VCALIB1_REGL 0xD8
112 #define IOFFSET_REGH 0xDD
113 #define IOFFSET_REGL 0xDE
116 /*0xE0 ~0xF2 data register,*/
119 #define REMAIN_CAP_REG3 0xE1
120 #define REMAIN_CAP_REG2 0xE2
121 #define REMAIN_CAP_REG1 0xE3
122 #define REMAIN_CAP_REG0 0xE4
126 #define FCC_REGL 0xE1
127 #define FCC_REGH 0xE2
129 #define GG_EN 1<<7 // gasgauge module enable bit 0: disable 1:enabsle TS_CTRL_REG 0xAC
131 #define ADC_VOL_EN 1<<7 //if GG_EN = 0 , then the ADC of BAT voltage controlled by the bit 0:diabsle 1:enable
132 #define ADC_CUR_EN 1<<6 //if GG_EN = 0, then the ADC of BAT current controlled by the bit 0: disable 1: enable
133 #define ADC_TS1_EN 1<<5 //the ADC of TS1 controlled by the bit 0:disabsle 1:enable
134 #define ADC_TS2_EN 1<<4 //the ADC of TS2 controlled by the bit 0:disabsle 1:enable
135 #define ADC_PHASE 1<<3 //ADC colock phase 0:normal 1:inverted
136 #define ADC_CLK_SEL 7
137 /*******************************************************************
138 #define ADC_CLK_SEL_2M 0x000
139 #define ADC_CLK_SEL_1M 0x001
140 #define ADC_CLK_SEL_500K 0x002
141 #define ADC_CLK_SEL_250K 0x003
142 #define ADC_CLK_SEL_125K 0x004
143 **********************************************************************/
145 #define CUR_SAMPL_CON_TIMES 3<<6 // ADC bat current continue sample times 00:8 01:16 10:32 11:64
146 #define ADC_OFF_CAL_INTERV 3<<4 //ADC offset calibreation interval time 00:8min 01:16min 10:32min 11:48min
147 #define OCV_SAMPL_INTERV 3<<2 //OCV sampling interval time 00:8min 01:16min 10:32min :11:48min
150 #define ADC_CUR_VOL_MODE 1<<1 //ADC working in current voltage collection mode
151 #define ADC_RES_MODE 1 //ADC working in resistor calculation mode 0:disable 1:enable
154 #define RES_CUR_AVG_SEL 3<<5 //average current filter times 00:1/2 01:1/4 10:1/8 11:1/16
155 #define BAT_CON 1<<4 //battery first connection,edge trigger 0:NOT 1:YES
156 #define RELAX_VOL1_UPD 1<<3 //battery voltage1 update in relax status 0: NOT 1:YE
157 #define RELAX_VOL2_UPD 1<<2 //battery voltage2 update in relax status 0: NOT 1:YE
158 #define RELAX_STS 1<<1 //battery coming into relax status 0: NOT 1:YE
159 #define IV_AVG_UPD_STS 1<<0 //battery average voltage and current updated status 0: NOT 1:YES
161 //FRAME_SMP_INTERV_REG
162 #define AUTO_SLP_EN 1<<5 // auto sleep mode 0:disable 1:enable
163 #define FRAME_SMP_INTERV_TIME 0x1F //
165 #define PLUG_IN_STS 1<<6
168 #define BAT_EXS (1<<7)
169 #define CHARGE_OFF (0x00<<4)
170 #define DEAD_CHARGE (0x01<<4)
171 #define TRICKLE_CHARGE (0x02<<4)
172 #define CC_OR_CV (0x03<<4)
173 #define CHARGE_FINISH (0x04<<4)
174 #define USB_OVER_VOL (0x05<<4)
175 #define BAT_TMP_ERR (0x06<<4)
176 #define TIMER_ERR (0x07<<4)
177 #define USB_EXIST (1<<1)// usb is exists
178 #define USB_EFF (1<<0)// usb is effective
181 #define CHRG_CT_EN (1<<7)
183 #define VLIM_4000MV (0x00<<4)
184 #define VLIM_4100MV (0x01<<4)
185 #define VLIM_4200MV (0x02<<4)
186 #define VLIM_4300MV (0x03<<4)
187 #define VLIM_4400MV (0x04<<4)
188 #define VLIM_4500MV (0x05<<4)
189 #define VLIM_4600MV (0x06<<4)
190 #define VLIM_4700MV (0x07<<4)
192 #define ILIM_45MA (0x00)
193 #define ILIM_300MA (0x01)
194 #define ILIM_80MA (0x02)
195 #define ILIM_820MA (0x03)
196 #define ILIM_1000MA (0x04)
197 #define ILIM_1200MA (0x05)
198 #define ILIM_1400MA (0x06)
199 #define ILIM_1600MA (0x07)
200 #define ILIM_1800MA (0x08)
201 #define ILIM_2000MA (0x09)
202 #define ILIM_2200MA (0x0A)
203 #define ILIM_2400MA (0x0B)
204 #define ILIM_2600MA (0x0C)
205 #define ILIM_2800MA (0x0D)
206 #define ILIM_3000MA (0x0E)
209 #define CHRG_EN (0x01<<7)
212 #define CHRG_VOL4050 (0x00<<4)
213 #define CHRG_VOL4100 (0x01<<4)
214 #define CHRG_VOL4150 (0x02<<4)
215 #define CHRG_VOL4200 (0x03<<4)
216 #define CHRG_VOL4300 (0x04<<4)
217 #define CHRG_VOL4350 (0x05<<4)
220 #define CHRG_CUR1000mA (0x00)
221 #define CHRG_CUR1200mA (0x01)
222 #define CHRG_CUR1400mA (0x02)
223 #define CHRG_CUR1600mA (0x03)
224 #define CHRG_CUR1800mA (0x04)
225 #define CHRG_CUR2000mA (0x05)
226 #define CHRG_CUR2200mA (0x06)
227 #define CHRG_CUR2400mA (0x07)
228 #define CHRG_CUR2600mA (0x08)
229 #define CHRG_CUR2800mA (0x09)
230 #define CHRG_CUR3000mA (0x0A)
233 #define DRIVER_VERSION "1.0.0"
234 #define ROLEX_SPEED 100 * 1000
236 #define CHARGING 0x01
237 #define DISCHARGING 0x00
239 #define TIMER_MS_COUNTS 1000
240 #define MAX_CHAR 0x7F
241 #define MAX_UNSIGNED_CHAR 0xFF
242 #define MAX_INT 0x7FFFFFFF
243 #define MAX_UNSIGNED_INT 0xFFFF
244 #define MAX_INT8 0x7F
245 #define MAX_UINT8 0xFF
247 /* Voltage and Current buffers */
250 static int16_t av_v[AV_SIZE];
251 static int16_t av_c[AV_SIZE];
253 static uint16_t av_v_index;
254 static uint16_t av_c_index;
256 #define INTERPOLATE_MAX 1000
257 //#define OCV_TABLE_SIZE
261 struct cell_state cell;
262 struct power_supply bat;
263 struct power_supply ac;
264 struct power_supply usb;
265 struct delayed_work work;
266 // struct i2c_client *client;
269 struct battery_platform_data *platform_data;
270 struct notifier_block battery_nb;
271 struct workqueue_struct *wq;
272 struct delayed_work battery_monitor_work;
273 struct delayed_work charge_check_work;
291 int poweroff_voltage;
292 int warnning_voltage;
300 int warnning_capacity;
324 int current_k;//(ICALIB0,ICALIB1)
327 int voltage_k;//VCALIB0 VCALIB1
330 int relax_entry_thres;
331 int relax_exit_thres;
340 struct timeval soc_timer;
341 struct timeval change_timer;
347 struct battery_info *data;
350 u32 interpolate(int value, u32 *table, int size)
355 for (i = 0; i < size; i++){
356 if (value < table[i])
360 if ((i > 0) && (i < size)) {
361 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
362 d /= table[i] - table[i-1];
363 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
365 d = i * ((INTERPOLATE_MAX+size/2)/size);
373 /* Returns (a * b) / c */
374 int32_t ab_div_c(u32 a, u32 b, u32 c)
380 sign = ((((a^b)^c) & 0x80000000) != 0);
386 tmp = ((int32_t) a*b + (c>>1)) / c;
398 static int32_t abs_int(int32_t x)
400 return (x > 0) ? x : -x;
403 /* Returns diviation between 'size' array members */
404 uint16_t diff_array(int16_t *arr, uint8_t size)
409 for (i = 0; i < size-1; i++)
410 diff += abs_int(arr[i] - arr[i+1]);
412 if (diff > MAX_UNSIGNED_INT)
413 diff = MAX_UNSIGNED_INT;
415 return (uint16_t) diff;
419 static enum power_supply_property rk818_battery_props[] = {
421 POWER_SUPPLY_PROP_STATUS,
422 POWER_SUPPLY_PROP_CURRENT_NOW,
423 POWER_SUPPLY_PROP_VOLTAGE_NOW,
424 POWER_SUPPLY_PROP_PRESENT,
425 POWER_SUPPLY_PROP_PRESENT,
426 POWER_SUPPLY_PROP_CAPACITY,
428 POWER_SUPPLY_PROP_STATUS,
429 POWER_SUPPLY_PROP_PRESENT,
430 POWER_SUPPLY_PROP_VOLTAGE_NOW,
431 POWER_SUPPLY_PROP_CURRENT_NOW,
432 POWER_SUPPLY_PROP_CAPACITY,
433 POWER_SUPPLY_PROP_TEMP,
434 POWER_SUPPLY_PROP_TECHNOLOGY,
435 POWER_SUPPLY_PROP_HEALTH,
436 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
437 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
438 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
443 static enum power_supply_property rk818_battery_ac_props[] = {
444 POWER_SUPPLY_PROP_ONLINE,
446 static enum power_supply_property rk818_battery_usb_props[] = {
447 POWER_SUPPLY_PROP_ONLINE,
451 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
454 ret = rk818_i2c_read(rk818, reg, len,buf);
458 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
461 ret = rk818_i2c_write(rk818, reg,(int)len, *buf);
464 static void dump_gauge_register(struct battery_info *di)
468 DBG("%s dump charger register start: \n",__FUNCTION__);
469 for(i = 0xAC;i < 0xDE; i ++){
470 battery_read(di ->rk818, i, &buf,1);
471 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
477 static void dump_charger_register(struct battery_info *di)
482 DBG("%s dump the register start: \n",__FUNCTION__);
483 for(i = 0x99;i < 0xAB; i ++){
484 battery_read(di ->rk818, i, &buf,1);
485 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
491 //POWER_SUPPLY_PROP_STATUS
492 static int rk818_battery_status(struct battery_info *di)
496 //POWER_SUPPLY_PROP_PRESENT,
497 static int rk818_battery_present(struct rk818_battery_info *di)
503 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
504 * based on the voltage.
506 static int _voltage_to_capacity(struct battery_info * di, int voltage)
512 ocv_table = di->platform_data->battery_ocv;
513 ocv_size = di->platform_data->ocv_size;
514 // ocv_table = di->ocv_table;
515 // ocv_size = di->ocv_size;
516 tmp = interpolate(voltage, ocv_table, ocv_size);
517 di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
518 di->temp_nac= ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
519 DBG("temp = %d real-soc =%d nac= %d, fcc = %d\n", tmp, di->temp_soc, di->temp_nac,di->fcc);
522 //POWER_SUPPLY_PROP_CURRENT_NOW,
523 static int _get_average_current(struct battery_info *di)
530 ret = battery_read(di->rk818,BAT_CUR_AVG_REGL, &buf, 1);
532 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
536 ret = battery_read(di->rk818,BAT_CUR_AVG_REGH, &buf, 1);
538 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
541 current_now |= (buf<<8);
543 if(current_now &0x800)
546 // temp = current_now*1000*90/14/4096*500/521;
547 temp = current_now*1506/1000;//1000*90/14/4096*500/521;
550 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
554 DBG("%s, average current current_now = %d current = %d\n",__FUNCTION__, current_now, temp);
559 #define to_device_info(x) container_of((x), \
560 struct battery_info, bat);
562 static int rk818_battery_get_property(struct power_supply *psy,
563 enum power_supply_property psp,
564 union power_supply_propval *val)
567 struct battery_info *di = to_device_info(psy);
570 case POWER_SUPPLY_PROP_CURRENT_NOW:
571 val->intval = di->current_avg;
574 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
575 case POWER_SUPPLY_PROP_PRESENT:
576 val->intval = di->voltage;// rk818_battery_voltage(di);
577 if (psp == POWER_SUPPLY_PROP_PRESENT)
578 val->intval = val->intval <= 0 ? 0 : 1;
581 case POWER_SUPPLY_PROP_CAPACITY:
584 if(di->real_soc > 100)
586 val->intval =di->real_soc;
587 //DBG("POWER_SUPPLY_PROP_CAPACITY = %d, val->intval =%d\n", di->real_soc, val->intval);
589 case POWER_SUPPLY_PROP_HEALTH:
590 val->intval = POWER_SUPPLY_HEALTH_GOOD;//rk818_battery_health(di);
593 case POWER_SUPPLY_PROP_STATUS:
594 val->intval = di->status;
595 //DBG("gBatStatus=%d\n",val->intval);
605 #define to_ac_device_info(x) container_of((x), \
606 struct battery_info, ac);
608 static int rk818_battery_ac_get_property(struct power_supply *psy,
609 enum power_supply_property psp,
610 union power_supply_propval *val)
612 //DBG("%s:%d psp = %d\n",__FUNCTION__,__LINE__,psp);
614 struct battery_info *di = to_ac_device_info(psy);
617 case POWER_SUPPLY_PROP_ONLINE:
618 val->intval = di->ac_online; /*discharging*/
619 //DBG("%s:%d val->intval = %d di->status = %d\n",__FUNCTION__,__LINE__,val->intval, di->status);
629 #define to_usb_device_info(x) container_of((x), \
630 struct battery_info, usb);
632 static int rk818_battery_usb_get_property(struct power_supply *psy,
633 enum power_supply_property psp,
634 union power_supply_propval *val)
637 struct battery_info *di = to_usb_device_info(psy);
640 case POWER_SUPPLY_PROP_ONLINE:
641 val->intval = di->usb_online; /*discharging*/
642 //DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
653 static void battery_powersupply_init(struct battery_info *di)
655 di->bat.name = "BATTERY";
656 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
657 di->bat.properties = rk818_battery_props;
658 di->bat.num_properties = ARRAY_SIZE(rk818_battery_props);
659 di->bat.get_property = rk818_battery_get_property;
662 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
663 di->ac.properties = rk818_battery_ac_props;
664 di->ac.num_properties = ARRAY_SIZE(rk818_battery_ac_props);
665 di->ac.get_property = rk818_battery_ac_get_property;
667 di->usb.name = "USB";
668 di->usb.type = POWER_SUPPLY_TYPE_USB;
669 di->usb.properties = rk818_battery_usb_props;
670 di->usb.num_properties = ARRAY_SIZE(rk818_battery_usb_props);
671 di->usb.get_property = rk818_battery_usb_get_property;
675 static int _gauge_enable(struct battery_info *di)
679 DBG("%s start \n", __FUNCTION__);
680 ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
681 DBG("_gauge_enable read-%d\n", buf);
684 dev_err(di->dev, "error reading TS_CTRL_REG");
689 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); //enable
690 ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
694 DBG("%s,%d\n",__FUNCTION__, buf);
701 static int _gauge_disable(struct battery_info *di)
706 ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
708 dev_err(di->dev, "error reading TS_CTRL_REG");
712 buf &= (~0x80);//GG_EN
713 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); //enable
719 static int _set_auto_sleep_cur(struct battery_info *di, u8 value)
724 ret = battery_write(di->rk818, AUTO_SLP_CUR_THR_REG, &buf, 1); //enable
727 static int _set_sleep_smp_time(struct battery_info *di, u8 value)
734 ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
736 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
740 temp = (buf&(AUTO_SLP_EN))|value;
741 ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &temp, 1); //enable
746 static int _autosleep_enable(struct battery_info *di)
751 ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
753 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
756 if(!(buf & AUTO_SLP_EN)){
758 ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &buf, 1); //enable
762 _set_auto_sleep_cur(di, di->sleep_cur); // <di->sleep_cur , into sleep-mode
763 _set_sleep_smp_time(di, di->sleep_smp_time); // time of adc work , sleep-mode
771 static int _autosleep_disable(struct battery_info *di)
776 ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
778 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
781 if((buf & AUTO_SLP_EN)){
782 buf &= (~AUTO_SLP_EN);
783 ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &buf, 1); //enable
792 static void _set_relax_thres(struct battery_info *di)
795 int enter_thres,exit_thres;
796 struct cell_state *cell = &di->cell;
798 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
799 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
801 buf = enter_thres&0xff;
802 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf,1);
803 buf = (enter_thres>>8)&0xff;
804 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf,1);
806 buf = exit_thres&0xff;
807 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf,1);
808 buf = (exit_thres>>8)&0xff;
809 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf,1);
812 battery_read(di->rk818,GGCON, &buf, 1);
814 battery_write(di->rk818, GGCON, &buf,1);
817 static int rk818_battery_voltage(struct battery_info *di)
824 ret = battery_read(di->rk818,BAT_VOL_REGL, &buf, 1);
826 ret = battery_read(di->rk818,BAT_VOL_REGH,&buf, 1);
830 //ret = battery_read(di->rk818,BAT_VOL_REGH, buf, 2);
832 dev_err(di->dev, "error reading BAT_VOL_REGH");
836 //voltage_now = temp;//(buf[0]<<8)|buf[1];
837 voltage_now = di ->voltage_k*temp + di->voltage_b;
839 DBG("the rea-time voltage is %d\n",voltage_now);
843 static int _get_OCV_voltage(struct battery_info *di)
850 ret = battery_read(di->rk818,BAT_OCV_REGL, &buf, 1);
852 ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 1);
856 //ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 2);
858 dev_err(di->dev, "error reading BAT_OCV_REGH");
862 //voltage_now = temp;//(buf[0]<<8)|buf[1];
863 voltage_now = di ->voltage_k*temp + di->voltage_b;
864 DBG("the OCV voltage is %d\n", voltage_now);
869 static int _get_ts1_adc(struct battery_info *di)
875 ret = battery_read(di->rk818,TS1_ADC_REGL, &buf, 1);
877 ret = battery_read(di->rk818,TS1_ADC_REGH, &buf, 1);
883 static int _get_ts2_adc(struct battery_info *di)
889 ret = battery_read(di->rk818,TS2_ADC_REGL, &buf, 1);
891 ret = battery_read(di->rk818,TS2_ADC_REGH, &buf, 1);
898 static void _capacity_init(struct battery_info *di, u32 capacity)
904 capacity_ma = capacity*2201;//36*14/900*4096/521*500;
905 DBG("%s WRITE GANCNT_CAL_REG %d\n", __FUNCTION__, capacity_ma);
907 buf = (capacity_ma>>24)&0xff;
908 battery_write(di->rk818, GASCNT_CAL_REG3, &buf,1);
909 buf = (capacity_ma>>16)&0xff;
910 battery_write(di->rk818, GASCNT_CAL_REG2, &buf,1);
911 buf = (capacity_ma>>8)&0xff;
912 battery_write(di->rk818, GASCNT_CAL_REG1, &buf,1);
913 buf = (capacity_ma&0xff)|0x01;
914 battery_write(di->rk818, GASCNT_CAL_REG0, &buf,1);
915 battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
922 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
928 if(capacity >= di ->qmax){
929 capacity = di ->qmax;
931 capacity_ma = capacity;
932 // DBG("%s WRITE GANCNT_CAL_REG %d\n", __FUNCTION__, capacity_ma);
933 buf = (capacity_ma>>24)&0xff;
934 battery_write(di->rk818, REMAIN_CAP_REG3, &buf,1);
935 buf = (capacity_ma>>16)&0xff;
936 battery_write(di->rk818, REMAIN_CAP_REG2, &buf,1);
937 buf = (capacity_ma>>8)&0xff;
938 battery_write(di->rk818, REMAIN_CAP_REG1, &buf,1);
939 buf = (capacity_ma&0xff)|0x01;
940 battery_write(di->rk818, REMAIN_CAP_REG0, &buf,1);
946 static int _get_remain_capacity(struct battery_info *di)
953 ret = battery_read(di->rk818,REMAIN_CAP_REG3, &buf, 1);
955 ret = battery_read(di->rk818,REMAIN_CAP_REG2, &buf, 1);
957 ret = battery_read(di->rk818,REMAIN_CAP_REG1, &buf, 1);
959 ret = battery_read(di->rk818,REMAIN_CAP_REG0, &buf, 1);
962 capacity = temp;///4096*900/14/36*500/521;
963 DBG("%s GASCNT_CAL_REG %d capacity =%d \n",__FUNCTION__, temp, capacity);
969 static int _get_capacity(struct battery_info *di)
976 ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 1);
978 ret = battery_read(di->rk818,GASCNT_CAL_REG2, &buf, 1);
980 ret = battery_read(di->rk818,GASCNT_CAL_REG1, &buf, 1);
982 ret = battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
985 capacity = temp/2201;///4096*900/14/36*500/521;
986 //DBG("%s GASCNT_CAL_REG %d capacity =%d \n",__FUNCTION__, temp, capacity);
991 static int _get_realtime_capacity(struct battery_info *di)
999 ret = battery_read(di->rk818,GASCNT3, &buf, 1);
1001 ret = battery_read(di->rk818,GASCNT2, &buf, 1);
1003 ret = battery_read(di->rk818,GASCNT1, &buf, 1);
1005 ret = battery_read(di->rk818,GASCNT0, &buf, 1);
1007 // ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 4);
1008 // temp = buf[0] << 24 | buf[1] << 24 | buf[2] << 24 |buf[3] ;
1009 capacity = temp/2201;///4096*900/14/36*500/521;
1010 //DBG("%s GASCNT = 0x%4x capacity =%d \n",__FUNCTION__, temp,capacity);
1016 static int _get_relax_vol1(struct battery_info *di)
1019 int temp = 0,voltage_now;
1022 ret = battery_read(di->rk818,RELAX_VOL1_REGL, &buf, 1);
1024 ret = battery_read(di->rk818,RELAX_VOL1_REGH, &buf, 1);
1026 // ret = battery_read(di->rk818,RELAX_VOL1_REGH, &buf, 2);
1027 // temp = (buf[0]<<8)|buf[1];
1028 voltage_now = di ->voltage_k*temp + di->voltage_b;
1033 static int _get_relax_vol2(struct battery_info *di)
1036 int temp = 0,voltage_now;
1039 ret = battery_read(di->rk818,RELAX_VOL2_REGL, &buf, 1);
1041 ret = battery_read(di->rk818,RELAX_VOL2_REGH, &buf, 1);
1043 // ret = battery_read(di->rk818,RELAX_VOL2_REGH, &buf, 2);
1044 // temp = (buf[0]<<8)|buf[1];
1045 voltage_now = di ->voltage_k*temp + di->voltage_b;
1050 static bool _is_relax_mode(struct battery_info *di)
1054 int relax_vol1,relax_vol2;
1056 struct cell_state *cell = &di->cell;
1058 ret = battery_read(di->rk818,GGSTS, &status, 1);
1059 DBG(" GGSTS the value is %2x the realsoc = %d \n", status, di->real_soc);
1061 if(!(status&RELAX_STS))
1063 if((!(status&RELAX_VOL1_UPD))||(!(status&RELAX_VOL2_UPD)))
1066 if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1067 && (di->real_soc <= cell->config->ocv->flat_zone_high))
1069 relax_vol1 = _get_relax_vol1(di);
1070 relax_vol2 = _get_relax_vol2(di);
1071 DBG("relax_vol1 = %d relax_vol2 =%d \n", relax_vol1,relax_vol2);
1072 if((abs_int((relax_vol2 - relax_vol1)))/8/60 > 4 )
1080 static int relax_soc(struct battery_info *di)
1083 _voltage_to_capacity( di, di->voltage);
1084 return di->temp_soc;
1087 static int _get_vcalib0(struct battery_info *di)
1094 ret = battery_read(di->rk818,VCALIB0_REGL, &buf, 1);
1096 ret = battery_read(di->rk818,VCALIB0_REGH, &buf, 1);
1099 //ret = battery_read(di->rk818,VCALIB0_REGH, &buf,2);
1100 //temp = (buf[0]<<8)|buf[1];
1102 DBG("%s voltage0 offset vale is %d\n",__FUNCTION__, temp);
1106 static int _get_vcalib1(struct battery_info *di)
1113 ret = battery_read(di->rk818,VCALIB1_REGL, &buf, 1);
1115 ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 1);
1118 //ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 2);
1119 //temp = (buf[0]<<8)|buf[1];
1120 DBG("%s voltage1 offset vale is %d\n",__FUNCTION__, temp);
1124 static void _get_voltage_offset_value(struct battery_info *di)
1126 int vcalib0,vcalib1;
1128 vcalib0 = _get_vcalib0(di);
1129 vcalib1 = _get_vcalib1(di);
1131 di->voltage_k = (4200 - 3000)/(vcalib1 - vcalib0);
1132 di->voltage_b = 4200 - di->voltage_k*vcalib1;
1137 static int _get_ioffset(struct battery_info *di)
1144 ret = battery_read(di->rk818,IOFFSET_REGL, &buf, 1);
1146 ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 1);
1149 //ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 2);
1150 //temp = (buf[0]<<8)|buf[1];
1152 DBG("%s IOFFSET value is %d\n", __FUNCTION__, temp);
1156 static int _set_cal_offset(struct battery_info *di, u32 value)
1161 DBG("%s\n",__FUNCTION__);
1163 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1); //enable
1164 buf = (value >> 8)&0xff;
1165 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1); //enable
1166 DBG("%s set CAL_OFFSET_REG %d\n",__FUNCTION__, temp);
1171 static bool _is_first_poweron(struct battery_info * di)
1177 ret = battery_read(di->rk818,GGSTS, &buf, 1);
1178 DBG("%s GGSTS value is %2x \n", __FUNCTION__, buf );
1182 battery_write(di->rk818,GGSTS, &buf, 1);
1183 battery_read(di->rk818,GGSTS, &temp, 1);
1184 }while(temp&BAT_CON);
1192 static bool fg_check_relaxed(struct battery_info * di)//(struct cell_state *cell)
1194 struct cell_state *cell = &di->cell;
1199 if (abs_int(di->current_avg) <=
1200 cell->config->ocv->sleep_enter_current) {
1201 if (cell->sleep_samples < MAX_UINT8)
1202 cell->sleep_samples++;
1204 if (cell->sleep_samples >=
1205 cell->config->ocv->sleep_enter_samples) {
1206 /* Entering sleep mode */
1207 do_gettimeofday(&cell->sleep_timer);
1208 do_gettimeofday(&cell->el_sleep_timer);
1210 cell->calibrate = true;
1213 cell->sleep_samples = 0;
1216 /* The battery cell is Sleeping, checking if need to exit
1217 sleep mode count number of seconds that cell spent in
1219 do_gettimeofday(&now);
1220 cell->cumulative_sleep +=
1221 now.tv_sec + cell->el_sleep_timer.tv_sec;
1222 do_gettimeofday(&cell->el_sleep_timer);
1224 /* Check if we need to reset Sleep */
1225 if (abs_int(di->current_avg) >
1226 cell->config->ocv->sleep_exit_current) {
1228 if (abs_int(di->current_avg) >
1229 cell->config->ocv->sleep_exit_current) {
1231 if (cell->sleep_samples < MAX_UINT8)
1232 cell->sleep_samples++;
1235 cell->sleep_samples = 0;
1238 /* Check if we need to reset a Sleep timer */
1239 if (cell->sleep_samples >
1240 cell->config->ocv->sleep_exit_samples) {
1241 /* Exit sleep mode */
1243 cell->sleep_timer.tv_sec = 0;
1244 cell->sleep = false;
1245 cell->relax = false;
1248 cell->sleep_samples = 0;
1252 if (now.tv_sec-cell->sleep_timer.tv_sec >
1253 cell->config->ocv->relax_period) {
1255 cell->calibrate = true;
1264 /* Checks for right conditions for OCV correction */
1265 static bool fg_can_ocv(struct battery_info * di)//(struct cell_state *cell)
1267 struct cell_state *cell = &di->cell;
1269 /* Voltage should be stable */
1270 if (cell->config->ocv->voltage_diff <= diff_array(av_v, AV_SIZE))
1273 /* Current should be stable */
1274 if (cell->config->ocv->current_diff <= diff_array(av_c, AV_SIZE))
1277 /* SOC should be out of Flat Zone */
1278 if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1279 && (di->real_soc <= cell->config->ocv->flat_zone_high))
1282 /* Current should be less then SleepEnterCurrent */
1283 if (abs_int(di->current_avg) >= cell->config->ocv->sleep_enter_current)
1286 /* Don't allow OCV below EDV1, unless OCVbelowEDV1 is set */
1287 //if (cell->edv1 && !cell->config->ocv_below_edv1)
1295 /* Sets the battery Voltage, and recalculates the average voltage */
1296 void fg_set_voltage(int16_t voltage)
1301 /* put voltage reading int the buffer and update average */
1303 av_v_index %= AV_SIZE;
1304 av_v[av_v_index] = voltage;
1305 for (i = 0; i < AV_SIZE; i++)
1310 /* Sets the battery Current, and recalculates the average current */
1311 void fg_set_current( int16_t cur)
1316 /* put current reading int the buffer and update average */
1318 av_c_index %= AV_SIZE;
1319 av_c[av_c_index] = cur;
1320 for (i = 0; i < AV_SIZE; i++)
1325 static int _copy_soc(struct battery_info * di, u8 save_soc)
1331 battery_write(di->rk818, SOC_REG, &soc, 1);
1332 battery_read(di->rk818, SOC_REG, &soc, 1);
1333 DBG(" the save soc-reg = %d \n", soc);
1337 static void _save_rsoc_nac(struct battery_info * di)
1343 battery_write(di->rk818, SOC_REG, &buf, 1);
1347 static int _rsoc_init(struct battery_info * di)
1351 u32 remain_capacity;
1353 vol = di->voltage_ocv; //_get_OCV_voltage(di);
1354 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1355 if(_is_first_poweron(di)){
1357 DBG(" %s this is first poweron\n", __FUNCTION__);
1358 _voltage_to_capacity(di, di->voltage_ocv);
1359 di->real_soc = di->temp_soc;
1360 di->nac = di->temp_nac;
1362 DBG(" %s this is not not not first poweron\n", __FUNCTION__);
1363 battery_read(di->rk818,SOC_REG, &temp, 1);
1364 remain_capacity = _get_remain_capacity(di);
1365 if(remain_capacity >= di->qmax)
1366 remain_capacity = di->qmax;
1367 DBG("saved SOC_REG = 0x%8x\n", temp);
1368 DBG("saved remain_capacity = %d\n", remain_capacity);
1371 di->real_soc = temp;
1372 //di->nac = di->fcc*temp/100;
1373 di->nac = remain_capacity;
1378 static int _get_soc(struct battery_info *di)
1381 return di->remain_capacity * 100 / di->fcc;
1384 static u8 get_charge_status(struct battery_info * di)
1389 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1390 DBG("%s ----- SUP_STS_REG(0xA0) = 0x%02x\n", __FUNCTION__, status);
1391 status &= ~(0x07<<4);
1399 case TRICKLE_CHARGE:// (0x02<<4)
1402 case CC_OR_CV: // (0x03<<4)
1405 case CHARGE_FINISH:// (0x04<<4)
1406 ret = CHARGE_FINISH;
1409 case USB_OVER_VOL:// (0x05<<4)
1413 case BAT_TMP_ERR:// (0x06<<4)
1417 case TIMER_ERR:// (0x07<<4)
1421 case USB_EXIST:// (1<<1)// usb is exists
1425 case USB_EFF:// (1<<0)// usb is effective
1436 static void rk818_battery_charger_init(struct battery_info *di)
1438 u8 chrg_ctrl_reg1,usb_ctrl_reg;// chrg_ctrl_reg2;
1442 DBG("%s start\n",__FUNCTION__);
1444 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1445 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1446 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1447 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1449 DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1450 //usb_ctrl_reg &= (0x01<<7);
1451 usb_ctrl_reg |= (VLIM_4400MV | ILIM_1200MA)|(0x01<<7);
1453 chrg_ctrl_reg1 &= (0x00);
1454 chrg_ctrl_reg1 |=(0x01<<7)| (CHRG_VOL4200| CHRG_CUR1400mA);
1456 sup_sts_reg &= ~(0x01<<3);
1457 sup_sts_reg |= (0x01<<2);
1459 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1461 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1462 //battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1463 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1466 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1467 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1468 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1469 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1470 DBG(" new usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x, SUP_STS_REG=0x%2x\n ",
1471 usb_ctrl_reg, chrg_ctrl_reg1,sup_sts_reg);
1473 DBG("%s end\n",__FUNCTION__);
1477 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
1479 void charge_disable_open_otg(struct battery_info *di, int value )
1481 // u8 chrg_ctrl_reg1,dcdc_en_reg;
1483 DBG("1 ---- charge disable \n");
1484 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0<< 7); //ldo9
1485 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); //ldo9
1488 DBG("1 ---- charge disable \n");
1489 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); //ldo9
1490 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7); //ldo9
1495 static void fg_init(struct battery_info *di)
1497 DBG("%s start\n",__FUNCTION__);
1499 _get_voltage_offset_value(di); //get the volatege offset
1500 // _autosleep_enable(di);
1501 rk818_battery_charger_init(di);
1502 // _set_relax_thres(di);
1503 di->current_offset = _get_ioffset(di); //get the current offset , the value write to the CAL_OFFSET
1504 _set_cal_offset(di,di->current_offset+42);
1506 di->voltage = rk818_battery_voltage(di);
1507 di->voltage_ocv = _get_OCV_voltage(di);
1509 _capacity_init(di, di->nac);
1510 // _get_realtime_capacity( di);
1511 di->remain_capacity = _get_capacity(di);
1512 // _get_realtime_capacity( di);
1513 do_gettimeofday(&di->soc_timer);
1514 di->change_timer = di->soc_timer;
1516 for (i = 0; i < AV_SIZE; i++) {
1517 av_v[i] = di->voltage;
1523 dump_gauge_register(di);
1524 dump_charger_register(di);
1525 DBG("nac =%d , remain_capacity = %d \n"
1526 " OCV_voltage =%d, voltage =%d \n",
1527 di->nac, di->remain_capacity,
1528 di->voltage_ocv, di->voltage);
1532 static int capacity_changed(struct battery_info *di)
1534 s32 acc_value, samples = 0;
1538 // fg_set_voltage(&di->cell, di->voltage_mV);
1539 //fg_set_current(&di->cell, (int16_t)(di->current_uA/1000));
1545 static void rk818_battery_info(struct battery_info *di)
1547 //di->status = rk818_battery_status(di);
1548 //di->voltage = rk818_battery_voltage(di);
1549 di->present = rk818_battery_present(di);
1550 di->bat_current = _get_average_current(di);
1551 di->temp_soc= rk818_battery_soc(di);
1552 di->tempreture =rk818_battery_temperature(di);
1553 di->health = rk818_battery_health(di);
1557 static void rk818_battery_display_smooth(struct battery_info *di)
1561 // int relaxmode_soc;
1562 // int coulomp_soc, soc;
1564 status = di->status;
1565 if(status == POWER_SUPPLY_STATUS_CHARGING){
1566 //DBG("charging smooth ... \n");
1568 //DBG(" BATTERY NOT RELAX MODE \n");
1569 DBG("di->remain_capacity =%d, di->fcc = %d\n", di->remain_capacity,di->fcc);
1570 di->temp_soc = _get_soc(di);
1571 charge_status = get_charge_status( di);
1572 if(di->temp_soc >= 100){
1574 //di->status = POWER_SUPPLY_STATUS_FULL;
1577 do_gettimeofday(&di->soc_timer);
1579 if(di->temp_soc!= di->real_soc){
1580 di->change_timer = di->soc_timer;
1581 if(di->real_soc < di->temp_soc)
1584 di->real_soc =di->temp_soc;
1587 // DBG("charge_status =0x%x\n", charge_status);
1588 if((charge_status ==CHARGE_FINISH) && (di->real_soc < 100)){
1589 DBG("CHARGE_FINISH di->real_soc < 100 \n ");
1590 if((di->soc_counter < 10)){
1593 di->soc_counter = 0;
1594 if(di->real_soc < 100){
1596 // _save_rsoc_nac( di);
1602 if(di->real_soc <= 0)
1604 if(di->real_soc >= 100){
1606 di->status = POWER_SUPPLY_STATUS_FULL;
1610 if(status == POWER_SUPPLY_STATUS_DISCHARGING){
1611 //DBG("discharging smooth ... \n");
1612 di->temp_soc = _get_soc(di);
1613 do_gettimeofday(&di->soc_timer);
1614 if(di->temp_soc!= di->real_soc){
1615 di->change_timer = di->soc_timer;
1616 di->real_soc = di->temp_soc;
1617 // _save_rsoc_nac( di);
1619 if(di->real_soc <= 0)
1621 if(di->real_soc >= 100){
1625 if(!_is_relax_mode( di)){
1626 DBG(" BATTERY NOT RELAX MODE \n");
1627 di->temp_soc = _get_soc(di);
1628 do_gettimeofday(&di->soc_timer);
1629 if(di->temp_soc!= di->real_soc){
1630 di->change_timer = di->soc_timer;
1631 di->real_soc = di->temp_soc;
1632 _save_rsoc_nac( di);
1636 DBG("BATTERY RELAX MODE\n ");
1637 //relaxmode_soc = relax_soc(di);
1638 coulomp_soc = _get_soc(di);
1639 soc =coulomp_soc;// (coulomp_soc*20 + relaxmode_soc*80)/100;
1641 if((soc > di->real_soc)&&(di->soc_counter < 10)){
1645 di->soc_counter = 0;
1646 if(di->real_soc < 100){
1648 _save_rsoc_nac( di);
1651 DBG(" remaxmode_soc = %d , coulomp-soc =%d soc = %d\n",relaxmode_soc, coulomp_soc, soc);
1656 //DBG("%s exit \n", __FUNCTION__);
1659 static void rk818_battery_update_status(struct battery_info *di)
1662 di->voltage = rk818_battery_voltage( di);
1663 di->current_avg = _get_average_current(di);
1664 di->remain_capacity = _get_realtime_capacity( di);
1667 rk818_battery_display_smooth(di);
1670 "voltage = %d, current-avg = %d\n"
1671 "fcc = %d ,remain_capacity =%d\n"
1674 di->voltage, di->current_avg,
1675 di->fcc, di->remain_capacity,
1679 extern int dwc_vbus_status(void);
1680 extern int get_gadget_connect_flag(void);
1682 //state of charge ----running
1683 static int get_charging_status(struct battery_info *di)
1686 ////////////////////////////////////////////
1688 u8 usb_ctrl_reg;// chrg_ctrl_reg2;
1692 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1693 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1695 DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1696 usb_ctrl_reg &= (0x01<<7);
1697 usb_ctrl_reg |= ( ILIM_300MA);
1699 /////////////////////////////////////////
1701 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1702 int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
1703 int vbus_status = dwc_vbus_status();
1704 if (1 == vbus_status) {
1705 if (0 == get_gadget_connect_flag()){
1706 if (++di->check_count >= 5){
1722 if (2 == vbus_status) {
1738 static void get_battery_status(struct battery_info *di)
1743 ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1744 //int vbus_status = dwc_vbus_status();
1746 if(buf&PLUG_IN_STS){
1747 //if(vbus_status != 0){
1748 get_charging_status(di);
1749 di->status = POWER_SUPPLY_STATUS_CHARGING;
1750 // di->ac_online = 1;
1751 if(di->real_soc == 100)
1752 di->status = POWER_SUPPLY_STATUS_FULL;
1755 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1760 //DBG("%s ,di->status = %d\n",__FUNCTION__, di->status);
1763 static void rk818_battery_work(struct work_struct *work)
1766 struct battery_info *di = container_of(work,
1767 struct battery_info, battery_monitor_work.work);
1769 get_battery_status(di);
1770 battery_read(di->rk818,0x00, &buf, 1);
1771 DBG("RTC =0x%2x\n ", buf);
1772 battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1773 //DBG("VB_MOD_REG =%2x, the value is %2x\n ", VB_MOD_REG,buf);
1774 battery_read(di->rk818,SUP_STS_REG, &buf, 1);
1775 // DBG("SUP_STS_REG =%2x, the value is %2x\n ", SUP_STS_REG,buf);
1776 vbus_status = dwc_vbus_status();
1777 // DBG("vbus_status =%2x\n ", vbus_status);
1779 rk818_battery_update_status(di);
1782 di ->resume = false;
1783 di->real_soc = _get_soc(di);
1784 if(di->real_soc <= 0)
1786 if(di->real_soc >= 100)
1789 if ((di->ac_online == 0 )&&( di->usb_online ==0)&&(di->remain_capacity > di->qmax +10)){
1790 _capacity_init(di, di->qmax);
1791 di->remain_capacity = _get_realtime_capacity( di);
1794 //DBG("soc = %d", di->real_soc);
1795 _copy_soc(di, di->real_soc);
1796 _save_remain_capacity(di, di->remain_capacity);
1797 power_supply_changed(&di->bat);
1798 // power_supply_changed(&di->usb);
1799 power_supply_changed(&di->ac);
1800 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1804 static void rk818_battery_charge_check_work(struct work_struct *work)
1806 struct battery_info *di = container_of(work,
1807 struct battery_info, charge_check_work.work);
1808 charge_disable_open_otg(di,di->charge_otg);
1811 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
1813 int register_battery_notifier(struct notifier_block *nb)
1815 return blocking_notifier_chain_register(&battery_chain_head, nb);
1817 EXPORT_SYMBOL_GPL(register_battery_notifier);
1819 int unregister_battery_notifier(struct notifier_block *nb)
1821 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
1823 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
1825 int battery_notifier_call_chain(unsigned long val)
1827 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
1828 == NOTIFY_BAD) ? -EINVAL : 0;
1830 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
1831 static int battery_notifier_call(struct notifier_block *nb,
1832 unsigned long event, void *data)
1834 struct battery_info *di=
1835 container_of(nb, struct battery_info, battery_nb);
1839 DBG(" CHARGE enable \n");
1840 di ->charge_otg = 0;
1841 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1845 di ->charge_otg = 1;
1846 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1848 DBG("charge disable OTG enable \n");
1856 static int rk818_battery_parse_dt(struct rk818 *rk818)
1858 struct device_node *regs,*rk818_pmic_np;
1859 struct battery_platform_data *data;
1860 struct cell_config *cell_cfg;
1861 struct property *prop;
1865 rk818_pmic_np = of_node_get(rk818->dev->of_node);
1866 if (!rk818_pmic_np) {
1867 printk("could not find pmic sub-node\n");
1871 regs = of_find_node_by_name(rk818_pmic_np, "battery");
1873 printk("could not find battery sub-node\n");
1877 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
1878 memset(data, 0, sizeof(*data));
1880 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
1881 /* determine the number of brightness levels */
1882 prop = of_find_property(regs, "ocv_table", &length);
1885 data->ocv_size= length / sizeof(u32);
1886 /* read brightness levels from DT property */
1887 if (data->ocv_size > 0) {
1888 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
1889 data->battery_ocv= devm_kzalloc(rk818->dev, size, GFP_KERNEL);
1890 if (!data->battery_ocv)
1892 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
1893 DBG("the battery OCV TABLE : ");
1894 for(i =0; i< data->ocv_size; i++ )
1895 DBG("%d ", data->battery_ocv[i]);
1900 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
1903 data->max_charger_currentmA= out_value;
1904 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
1907 data->max_charger_voltagemV= out_value;
1908 ret = of_property_read_u32(regs, "design_capacity", &out_value);
1911 cell_cfg->design_capacity = out_value;
1912 ret = of_property_read_u32(regs, "design_qmax", &out_value);
1915 cell_cfg->design_qmax =out_value;
1916 data->cell_cfg =cell_cfg;
1917 rk818->battery_data = data;
1918 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
1919 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
1920 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
1921 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
1926 static struct of_device_id rk818_battery_of_match[] = {
1927 { .compatible = "rk818_battery" },
1931 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
1933 static int rk818_battery_parse_dt(struct device *dev)
1939 static int battery_probe(struct platform_device *pdev)
1941 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
1942 struct rk818_platform_data *rk818_platform_data = chip->dev->platform_data;
1943 // struct battery_platform_data *pdata ;//= rk818_platform_data->battery_data;
1944 // struct battery_platform_data defdata ;//= rk818_platform_data->battery_data;
1945 struct battery_info *di;
1946 struct ocv_config *ocv;
1947 struct edv_config *edv;
1950 DBG("%s is the battery driver version %s\n",__FUNCTION__,DRIVER_VERSION);
1951 rk818_battery_parse_dt(chip);
1953 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1955 dev_err(&pdev->dev, "no memory for state\n");
1959 ocv = devm_kzalloc(&pdev->dev, sizeof(*ocv), GFP_KERNEL);
1961 dev_err(&pdev->dev, "ocv no memory for state\n");
1965 edv = devm_kzalloc(&pdev->dev, sizeof(*edv), GFP_KERNEL);
1967 dev_err(&pdev->dev, "edv no memory for state\n");
1974 di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
1975 if (!di->platform_data) {
1981 platform_set_drvdata(pdev, di);
1982 /*apply battery cell configuration*/
1983 //di->cell.config = di->platform_data->cell_cfg;
1984 di->platform_data = chip->battery_data;
1985 di->platform_data->cell_cfg = chip->battery_data->cell_cfg;
1986 di->platform_data->cell_cfg->ocv = ocv;
1987 di->platform_data->cell_cfg->edv = edv;
1988 di->design_capacity = chip->battery_data->cell_cfg->design_capacity;
1989 di->qmax = chip->battery_data->cell_cfg->design_qmax;
1990 di->fcc = di->design_capacity;
1991 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1993 battery_powersupply_init(di);
1995 ret = power_supply_register(&pdev->dev, &di->bat);
1997 dev_dbg(&pdev->dev, "failed to register main battery\n");
2000 ret = power_supply_register(&pdev->dev, &di->usb);
2002 dev_dbg(&pdev->dev, "failed to register usb power supply\n");
2005 ret = power_supply_register(&pdev->dev, &di->ac);
2007 dev_dbg(&pdev->dev, "failed to register ac power supply\n");
2011 di->wq = create_singlethread_workqueue("battery-work");
2012 INIT_DELAYED_WORK(&di->battery_monitor_work,rk818_battery_work);
2013 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2014 //queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2015 INIT_DELAYED_WORK(&di->charge_check_work,rk818_battery_charge_check_work);
2017 di->battery_nb.notifier_call = battery_notifier_call;
2018 register_battery_notifier(&di->battery_nb);
2019 printk("battery probe ok... \n");
2023 power_supply_unregister(&di->ac);
2025 power_supply_unregister(&di->usb);
2027 power_supply_unregister(&di->bat);
2031 static int battery_remove(struct platform_device *dev)
2036 static int battery_suspend(struct platform_device *dev,pm_message_t state)
2039 struct battery_info *di = platform_get_drvdata(dev);
2040 DBG("%s--------------------\n",__FUNCTION__);
2042 printk("battery NULL di\n");
2043 cancel_delayed_work(&di ->battery_monitor_work);
2044 DBG("%s---------end--------\n",__FUNCTION__);
2049 static int battery_resume(struct platform_device *dev)
2055 struct battery_info *di = platform_get_drvdata(dev);
2057 ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
2059 // struct battery_info *di = platform_get_drvdata(dev);
2060 DBG("%s--------------------\n",__FUNCTION__);
2061 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2063 DBG("charge--status 0x%02x--------------------buf = 0x%02x\n", get_charge_status( di),buf);
2068 static struct platform_driver battery_driver = {
2069 .probe = battery_probe,
2070 .remove = battery_remove,
2071 .suspend = battery_suspend,
2072 .resume = battery_resume,
2075 .name = "rk818-battery",
2077 // .of_match_table = of_match_ptr(rk818_battery_parse_dt),
2081 static int __init battery_init(void)
2083 return platform_driver_register(&battery_driver);
2085 fs_initcall_sync(battery_init);
2086 static void __exit battery_exit(void)
2088 platform_driver_unregister(&battery_driver);
2090 module_exit(battery_exit);
2092 MODULE_LICENSE("GPL");
2093 MODULE_ALIAS("platform:rk818-battery");
2094 MODULE_AUTHOR("ROCKCHIP");