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 int rk818_battery_voltage(struct battery_info *di)
799 ret = battery_read(di->rk818,BAT_VOL_REGL, &buf, 1);
801 ret = battery_read(di->rk818,BAT_VOL_REGH,&buf, 1);
805 //ret = battery_read(di->rk818,BAT_VOL_REGH, buf, 2);
807 dev_err(di->dev, "error reading BAT_VOL_REGH");
811 //voltage_now = temp;//(buf[0]<<8)|buf[1];
812 voltage_now = di ->voltage_k*temp + di->voltage_b;
814 DBG("the rea-time voltage is %d\n",voltage_now);
818 static int _get_OCV_voltage(struct battery_info *di)
825 ret = battery_read(di->rk818,BAT_OCV_REGL, &buf, 1);
827 ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 1);
831 //ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 2);
833 dev_err(di->dev, "error reading BAT_OCV_REGH");
837 //voltage_now = temp;//(buf[0]<<8)|buf[1];
838 voltage_now = di ->voltage_k*temp + di->voltage_b;
839 DBG("the OCV voltage is %d\n", voltage_now);
844 static int _get_ts1_adc(struct battery_info *di)
850 ret = battery_read(di->rk818,TS1_ADC_REGL, &buf, 1);
852 ret = battery_read(di->rk818,TS1_ADC_REGH, &buf, 1);
858 static int _get_ts2_adc(struct battery_info *di)
864 ret = battery_read(di->rk818,TS2_ADC_REGL, &buf, 1);
866 ret = battery_read(di->rk818,TS2_ADC_REGH, &buf, 1);
873 static void _capacity_init(struct battery_info *di, u32 capacity)
879 capacity_ma = capacity*2201;//36*14/900*4096/521*500;
880 DBG("%s WRITE GANCNT_CAL_REG %d\n", __FUNCTION__, capacity_ma);
882 buf = (capacity_ma>>24)&0xff;
883 battery_write(di->rk818, GASCNT_CAL_REG3, &buf,1);
884 buf = (capacity_ma>>16)&0xff;
885 battery_write(di->rk818, GASCNT_CAL_REG2, &buf,1);
886 buf = (capacity_ma>>8)&0xff;
887 battery_write(di->rk818, GASCNT_CAL_REG1, &buf,1);
888 buf = (capacity_ma&0xff)|0x01;
889 battery_write(di->rk818, GASCNT_CAL_REG0, &buf,1);
890 battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
897 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
903 if(capacity >= di ->qmax){
904 capacity = di ->qmax;
906 capacity_ma = capacity;
907 // DBG("%s WRITE GANCNT_CAL_REG %d\n", __FUNCTION__, capacity_ma);
908 buf = (capacity_ma>>24)&0xff;
909 battery_write(di->rk818, REMAIN_CAP_REG3, &buf,1);
910 buf = (capacity_ma>>16)&0xff;
911 battery_write(di->rk818, REMAIN_CAP_REG2, &buf,1);
912 buf = (capacity_ma>>8)&0xff;
913 battery_write(di->rk818, REMAIN_CAP_REG1, &buf,1);
914 buf = (capacity_ma&0xff)|0x01;
915 battery_write(di->rk818, REMAIN_CAP_REG0, &buf,1);
921 static int _get_remain_capacity(struct battery_info *di)
928 ret = battery_read(di->rk818,REMAIN_CAP_REG3, &buf, 1);
930 ret = battery_read(di->rk818,REMAIN_CAP_REG2, &buf, 1);
932 ret = battery_read(di->rk818,REMAIN_CAP_REG1, &buf, 1);
934 ret = battery_read(di->rk818,REMAIN_CAP_REG0, &buf, 1);
937 capacity = temp;///4096*900/14/36*500/521;
938 DBG("%s GASCNT_CAL_REG %d capacity =%d \n",__FUNCTION__, temp, capacity);
944 static int _get_capacity(struct battery_info *di)
951 ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 1);
953 ret = battery_read(di->rk818,GASCNT_CAL_REG2, &buf, 1);
955 ret = battery_read(di->rk818,GASCNT_CAL_REG1, &buf, 1);
957 ret = battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
960 capacity = temp/2201;///4096*900/14/36*500/521;
961 //DBG("%s GASCNT_CAL_REG %d capacity =%d \n",__FUNCTION__, temp, capacity);
966 static int _get_realtime_capacity(struct battery_info *di)
974 ret = battery_read(di->rk818,GASCNT3, &buf, 1);
976 ret = battery_read(di->rk818,GASCNT2, &buf, 1);
978 ret = battery_read(di->rk818,GASCNT1, &buf, 1);
980 ret = battery_read(di->rk818,GASCNT0, &buf, 1);
982 // ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 4);
983 // temp = buf[0] << 24 | buf[1] << 24 | buf[2] << 24 |buf[3] ;
984 capacity = temp/2201;///4096*900/14/36*500/521;
985 //DBG("%s GASCNT = 0x%4x capacity =%d \n",__FUNCTION__, temp,capacity);
991 static int _get_vcalib0(struct battery_info *di)
998 ret = battery_read(di->rk818,VCALIB0_REGL, &buf, 1);
1000 ret = battery_read(di->rk818,VCALIB0_REGH, &buf, 1);
1003 //ret = battery_read(di->rk818,VCALIB0_REGH, &buf,2);
1004 //temp = (buf[0]<<8)|buf[1];
1006 DBG("%s voltage0 offset vale is %d\n",__FUNCTION__, temp);
1010 static int _get_vcalib1(struct battery_info *di)
1017 ret = battery_read(di->rk818,VCALIB1_REGL, &buf, 1);
1019 ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 1);
1022 //ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 2);
1023 //temp = (buf[0]<<8)|buf[1];
1024 DBG("%s voltage1 offset vale is %d\n",__FUNCTION__, temp);
1028 static void _get_voltage_offset_value(struct battery_info *di)
1030 int vcalib0,vcalib1;
1032 vcalib0 = _get_vcalib0(di);
1033 vcalib1 = _get_vcalib1(di);
1035 di->voltage_k = (4200 - 3000)/(vcalib1 - vcalib0);
1036 di->voltage_b = 4200 - di->voltage_k*vcalib1;
1041 static int _get_ioffset(struct battery_info *di)
1048 ret = battery_read(di->rk818,IOFFSET_REGL, &buf, 1);
1050 ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 1);
1053 //ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 2);
1054 //temp = (buf[0]<<8)|buf[1];
1056 DBG("%s IOFFSET value is %d\n", __FUNCTION__, temp);
1060 static int _set_cal_offset(struct battery_info *di, u32 value)
1065 DBG("%s\n",__FUNCTION__);
1067 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1); //enable
1068 buf = (value >> 8)&0xff;
1069 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1); //enable
1070 DBG("%s set CAL_OFFSET_REG %d\n",__FUNCTION__, temp);
1075 static bool _is_first_poweron(struct battery_info * di)
1081 ret = battery_read(di->rk818,GGSTS, &buf, 1);
1082 DBG("%s GGSTS value is %2x \n", __FUNCTION__, buf );
1086 battery_write(di->rk818,GGSTS, &buf, 1);
1087 battery_read(di->rk818,GGSTS, &temp, 1);
1088 }while(temp&BAT_CON);
1096 static bool fg_check_relaxed(struct battery_info * di)//(struct cell_state *cell)
1098 struct cell_state *cell = &di->cell;
1103 if (abs_int(di->current_avg) <=
1104 cell->config->ocv->sleep_enter_current) {
1105 if (cell->sleep_samples < MAX_UINT8)
1106 cell->sleep_samples++;
1108 if (cell->sleep_samples >=
1109 cell->config->ocv->sleep_enter_samples) {
1110 /* Entering sleep mode */
1111 do_gettimeofday(&cell->sleep_timer);
1112 do_gettimeofday(&cell->el_sleep_timer);
1114 cell->calibrate = true;
1117 cell->sleep_samples = 0;
1120 /* The battery cell is Sleeping, checking if need to exit
1121 sleep mode count number of seconds that cell spent in
1123 do_gettimeofday(&now);
1124 cell->cumulative_sleep +=
1125 now.tv_sec + cell->el_sleep_timer.tv_sec;
1126 do_gettimeofday(&cell->el_sleep_timer);
1128 /* Check if we need to reset Sleep */
1129 if (abs_int(di->current_avg) >
1130 cell->config->ocv->sleep_exit_current) {
1132 if (abs_int(di->current_avg) >
1133 cell->config->ocv->sleep_exit_current) {
1135 if (cell->sleep_samples < MAX_UINT8)
1136 cell->sleep_samples++;
1139 cell->sleep_samples = 0;
1142 /* Check if we need to reset a Sleep timer */
1143 if (cell->sleep_samples >
1144 cell->config->ocv->sleep_exit_samples) {
1145 /* Exit sleep mode */
1147 cell->sleep_timer.tv_sec = 0;
1148 cell->sleep = false;
1149 cell->relax = false;
1152 cell->sleep_samples = 0;
1156 if (now.tv_sec-cell->sleep_timer.tv_sec >
1157 cell->config->ocv->relax_period) {
1159 cell->calibrate = true;
1168 /* Checks for right conditions for OCV correction */
1169 static bool fg_can_ocv(struct battery_info * di)//(struct cell_state *cell)
1171 struct cell_state *cell = &di->cell;
1173 /* Voltage should be stable */
1174 if (cell->config->ocv->voltage_diff <= diff_array(av_v, AV_SIZE))
1177 /* Current should be stable */
1178 if (cell->config->ocv->current_diff <= diff_array(av_c, AV_SIZE))
1181 /* SOC should be out of Flat Zone */
1182 if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1183 && (di->real_soc <= cell->config->ocv->flat_zone_high))
1186 /* Current should be less then SleepEnterCurrent */
1187 if (abs_int(di->current_avg) >= cell->config->ocv->sleep_enter_current)
1190 /* Don't allow OCV below EDV1, unless OCVbelowEDV1 is set */
1191 //if (cell->edv1 && !cell->config->ocv_below_edv1)
1199 /* Sets the battery Voltage, and recalculates the average voltage */
1200 void fg_set_voltage(int16_t voltage)
1205 /* put voltage reading int the buffer and update average */
1207 av_v_index %= AV_SIZE;
1208 av_v[av_v_index] = voltage;
1209 for (i = 0; i < AV_SIZE; i++)
1214 /* Sets the battery Current, and recalculates the average current */
1215 void fg_set_current( int16_t cur)
1220 /* put current reading int the buffer and update average */
1222 av_c_index %= AV_SIZE;
1223 av_c[av_c_index] = cur;
1224 for (i = 0; i < AV_SIZE; i++)
1229 static int _copy_soc(struct battery_info * di, u8 save_soc)
1235 battery_write(di->rk818, SOC_REG, &soc, 1);
1236 battery_read(di->rk818, SOC_REG, &soc, 1);
1237 DBG(" the save soc-reg = %d \n", soc);
1242 static int _rsoc_init(struct battery_info * di)
1246 u32 remain_capacity;
1248 vol = di->voltage_ocv; //_get_OCV_voltage(di);
1249 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1250 if(_is_first_poweron(di)){
1252 DBG(" %s this is first poweron\n", __FUNCTION__);
1253 _voltage_to_capacity(di, di->voltage_ocv);
1254 di->real_soc = di->temp_soc;
1255 di->nac = di->temp_nac;
1257 DBG(" %s this is not not not first poweron\n", __FUNCTION__);
1258 battery_read(di->rk818,SOC_REG, &temp, 1);
1259 remain_capacity = _get_remain_capacity(di);
1260 if(remain_capacity >= di->qmax)
1261 remain_capacity = di->qmax;
1262 DBG("saved SOC_REG = 0x%8x\n", temp);
1263 DBG("saved remain_capacity = %d\n", remain_capacity);
1266 di->real_soc = temp;
1267 //di->nac = di->fcc*temp/100;
1268 di->nac = remain_capacity;
1273 static int _get_soc(struct battery_info *di)
1276 return di->remain_capacity * 100 / di->fcc;
1279 static u8 get_charge_status(struct battery_info * di)
1284 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1285 DBG("%s ----- SUP_STS_REG(0xA0) = 0x%02x\n", __FUNCTION__, status);
1286 status &= ~(0x07<<4);
1294 case TRICKLE_CHARGE:// (0x02<<4)
1297 case CC_OR_CV: // (0x03<<4)
1300 case CHARGE_FINISH:// (0x04<<4)
1301 ret = CHARGE_FINISH;
1304 case USB_OVER_VOL:// (0x05<<4)
1308 case BAT_TMP_ERR:// (0x06<<4)
1312 case TIMER_ERR:// (0x07<<4)
1316 case USB_EXIST:// (1<<1)// usb is exists
1320 case USB_EFF:// (1<<0)// usb is effective
1331 static void rk818_battery_charger_init(struct battery_info *di)
1333 u8 chrg_ctrl_reg1,usb_ctrl_reg;// chrg_ctrl_reg2;
1337 DBG("%s start\n",__FUNCTION__);
1339 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1340 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1341 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1342 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1344 DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1345 //usb_ctrl_reg &= (0x01<<7);
1346 usb_ctrl_reg |= (VLIM_4400MV | ILIM_1200MA)|(0x01<<7);
1348 chrg_ctrl_reg1 &= (0x00);
1349 chrg_ctrl_reg1 |=(0x01<<7)| (CHRG_VOL4200| CHRG_CUR1400mA);
1351 sup_sts_reg &= ~(0x01<<3);
1352 sup_sts_reg |= (0x01<<2);
1354 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1356 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1357 //battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1358 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1361 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1362 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1363 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1364 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1365 DBG(" new usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x, SUP_STS_REG=0x%2x\n ",
1366 usb_ctrl_reg, chrg_ctrl_reg1,sup_sts_reg);
1368 DBG("%s end\n",__FUNCTION__);
1372 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
1374 void charge_disable_open_otg(struct battery_info *di, int value )
1376 // u8 chrg_ctrl_reg1,dcdc_en_reg;
1378 DBG("1 ---- charge disable \n");
1379 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0<< 7); //ldo9
1380 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); //ldo9
1383 DBG("1 ---- charge disable \n");
1384 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); //ldo9
1385 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7); //ldo9
1390 static void fg_init(struct battery_info *di)
1392 DBG("%s start\n",__FUNCTION__);
1394 _get_voltage_offset_value(di); //get the volatege offset
1395 // _autosleep_enable(di);
1396 rk818_battery_charger_init(di);
1397 // _set_relax_thres(di);
1398 di->current_offset = _get_ioffset(di); //get the current offset , the value write to the CAL_OFFSET
1399 _set_cal_offset(di,di->current_offset+42);
1401 di->voltage = rk818_battery_voltage(di);
1402 di->voltage_ocv = _get_OCV_voltage(di);
1404 _capacity_init(di, di->nac);
1405 // _get_realtime_capacity( di);
1406 di->remain_capacity = _get_capacity(di);
1407 // _get_realtime_capacity( di);
1408 do_gettimeofday(&di->soc_timer);
1409 di->change_timer = di->soc_timer;
1411 for (i = 0; i < AV_SIZE; i++) {
1412 av_v[i] = di->voltage;
1418 dump_gauge_register(di);
1419 dump_charger_register(di);
1420 DBG("nac =%d , remain_capacity = %d \n"
1421 " OCV_voltage =%d, voltage =%d \n",
1422 di->nac, di->remain_capacity,
1423 di->voltage_ocv, di->voltage);
1427 static int capacity_changed(struct battery_info *di)
1429 s32 acc_value, samples = 0;
1433 // fg_set_voltage(&di->cell, di->voltage_mV);
1434 //fg_set_current(&di->cell, (int16_t)(di->current_uA/1000));
1440 static void rk818_battery_info(struct battery_info *di)
1442 //di->status = rk818_battery_status(di);
1443 //di->voltage = rk818_battery_voltage(di);
1444 di->present = rk818_battery_present(di);
1445 di->bat_current = _get_average_current(di);
1446 di->temp_soc= rk818_battery_soc(di);
1447 di->tempreture =rk818_battery_temperature(di);
1448 di->health = rk818_battery_health(di);
1452 static void rk818_battery_display_smooth(struct battery_info *di)
1456 // int relaxmode_soc;
1457 // int coulomp_soc, soc;
1459 status = di->status;
1460 if(status == POWER_SUPPLY_STATUS_CHARGING){
1461 //DBG("charging smooth ... \n");
1463 //DBG(" BATTERY NOT RELAX MODE \n");
1464 DBG("di->remain_capacity =%d, di->fcc = %d\n", di->remain_capacity,di->fcc);
1465 di->temp_soc = _get_soc(di);
1466 charge_status = get_charge_status( di);
1467 if(di->temp_soc >= 100){
1469 //di->status = POWER_SUPPLY_STATUS_FULL;
1472 do_gettimeofday(&di->soc_timer);
1474 if(di->temp_soc!= di->real_soc){
1475 di->change_timer = di->soc_timer;
1476 if(di->real_soc < di->temp_soc)
1479 di->real_soc =di->temp_soc;
1482 // DBG("charge_status =0x%x\n", charge_status);
1483 if((charge_status ==CHARGE_FINISH) && (di->real_soc < 100)){
1484 DBG("CHARGE_FINISH di->real_soc < 100 \n ");
1485 if((di->soc_counter < 10)){
1488 di->soc_counter = 0;
1489 if(di->real_soc < 100){
1491 // _save_rsoc_nac( di);
1497 if(di->real_soc <= 0)
1499 if(di->real_soc >= 100){
1501 di->status = POWER_SUPPLY_STATUS_FULL;
1505 if(status == POWER_SUPPLY_STATUS_DISCHARGING){
1506 //DBG("discharging smooth ... \n");
1507 di->temp_soc = _get_soc(di);
1508 do_gettimeofday(&di->soc_timer);
1509 if(di->temp_soc!= di->real_soc){
1510 di->change_timer = di->soc_timer;
1511 di->real_soc = di->temp_soc;
1512 // _save_rsoc_nac( di);
1514 if(di->real_soc <= 0)
1516 if(di->real_soc >= 100){
1520 if(!_is_relax_mode( di)){
1521 DBG(" BATTERY NOT RELAX MODE \n");
1522 di->temp_soc = _get_soc(di);
1523 do_gettimeofday(&di->soc_timer);
1524 if(di->temp_soc!= di->real_soc){
1525 di->change_timer = di->soc_timer;
1526 di->real_soc = di->temp_soc;
1527 _save_rsoc_nac( di);
1531 DBG("BATTERY RELAX MODE\n ");
1532 //relaxmode_soc = relax_soc(di);
1533 coulomp_soc = _get_soc(di);
1534 soc =coulomp_soc;// (coulomp_soc*20 + relaxmode_soc*80)/100;
1536 if((soc > di->real_soc)&&(di->soc_counter < 10)){
1540 di->soc_counter = 0;
1541 if(di->real_soc < 100){
1543 _save_rsoc_nac( di);
1546 DBG(" remaxmode_soc = %d , coulomp-soc =%d soc = %d\n",relaxmode_soc, coulomp_soc, soc);
1551 //DBG("%s exit \n", __FUNCTION__);
1554 static void rk818_battery_update_status(struct battery_info *di)
1557 di->voltage = rk818_battery_voltage( di);
1558 di->current_avg = _get_average_current(di);
1559 di->remain_capacity = _get_realtime_capacity( di);
1562 rk818_battery_display_smooth(di);
1565 "voltage = %d, current-avg = %d\n"
1566 "fcc = %d ,remain_capacity =%d\n"
1569 di->voltage, di->current_avg,
1570 di->fcc, di->remain_capacity,
1574 extern int dwc_vbus_status(void);
1575 extern int get_gadget_connect_flag(void);
1577 //state of charge ----running
1578 static int get_charging_status(struct battery_info *di)
1581 ////////////////////////////////////////////
1583 u8 usb_ctrl_reg;// chrg_ctrl_reg2;
1587 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1588 // battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1590 DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1591 usb_ctrl_reg &= (0x01<<7);
1592 usb_ctrl_reg |= ( ILIM_300MA);
1594 /////////////////////////////////////////
1596 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1597 int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
1598 int vbus_status = dwc_vbus_status();
1599 if (1 == vbus_status) {
1600 if (0 == get_gadget_connect_flag()){
1601 if (++di->check_count >= 5){
1617 if (2 == vbus_status) {
1633 static void get_battery_status(struct battery_info *di)
1638 ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1639 //int vbus_status = dwc_vbus_status();
1641 if(buf&PLUG_IN_STS){
1642 //if(vbus_status != 0){
1643 get_charging_status(di);
1644 di->status = POWER_SUPPLY_STATUS_CHARGING;
1645 // di->ac_online = 1;
1646 if(di->real_soc == 100)
1647 di->status = POWER_SUPPLY_STATUS_FULL;
1650 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1655 //DBG("%s ,di->status = %d\n",__FUNCTION__, di->status);
1658 static void rk818_battery_work(struct work_struct *work)
1661 struct battery_info *di = container_of(work,
1662 struct battery_info, battery_monitor_work.work);
1664 get_battery_status(di);
1665 battery_read(di->rk818,0x00, &buf, 1);
1666 DBG("RTC =0x%2x\n ", buf);
1667 battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1668 //DBG("VB_MOD_REG =%2x, the value is %2x\n ", VB_MOD_REG,buf);
1669 battery_read(di->rk818,SUP_STS_REG, &buf, 1);
1670 // DBG("SUP_STS_REG =%2x, the value is %2x\n ", SUP_STS_REG,buf);
1671 vbus_status = dwc_vbus_status();
1672 // DBG("vbus_status =%2x\n ", vbus_status);
1674 rk818_battery_update_status(di);
1677 di ->resume = false;
1678 di->real_soc = _get_soc(di);
1679 if(di->real_soc <= 0)
1681 if(di->real_soc >= 100)
1684 if ((di->ac_online == 0 )&&( di->usb_online ==0)&&(di->remain_capacity > di->qmax +10)){
1685 _capacity_init(di, di->qmax);
1686 di->remain_capacity = _get_realtime_capacity( di);
1689 //DBG("soc = %d", di->real_soc);
1690 _copy_soc(di, di->real_soc);
1691 _save_remain_capacity(di, di->remain_capacity);
1692 power_supply_changed(&di->bat);
1693 // power_supply_changed(&di->usb);
1694 power_supply_changed(&di->ac);
1695 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1699 static void rk818_battery_charge_check_work(struct work_struct *work)
1701 struct battery_info *di = container_of(work,
1702 struct battery_info, charge_check_work.work);
1703 charge_disable_open_otg(di,di->charge_otg);
1706 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
1708 int register_battery_notifier(struct notifier_block *nb)
1710 return blocking_notifier_chain_register(&battery_chain_head, nb);
1712 EXPORT_SYMBOL_GPL(register_battery_notifier);
1714 int unregister_battery_notifier(struct notifier_block *nb)
1716 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
1718 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
1720 int battery_notifier_call_chain(unsigned long val)
1722 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
1723 == NOTIFY_BAD) ? -EINVAL : 0;
1725 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
1726 static int battery_notifier_call(struct notifier_block *nb,
1727 unsigned long event, void *data)
1729 struct battery_info *di=
1730 container_of(nb, struct battery_info, battery_nb);
1734 DBG(" CHARGE enable \n");
1735 di ->charge_otg = 0;
1736 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1740 di ->charge_otg = 1;
1741 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1743 DBG("charge disable OTG enable \n");
1751 static int rk818_battery_parse_dt(struct rk818 *rk818)
1753 struct device_node *regs,*rk818_pmic_np;
1754 struct battery_platform_data *data;
1755 struct cell_config *cell_cfg;
1756 struct property *prop;
1760 rk818_pmic_np = of_node_get(rk818->dev->of_node);
1761 if (!rk818_pmic_np) {
1762 printk("could not find pmic sub-node\n");
1766 regs = of_find_node_by_name(rk818_pmic_np, "battery");
1768 printk("could not find battery sub-node\n");
1772 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
1773 memset(data, 0, sizeof(*data));
1775 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
1776 /* determine the number of brightness levels */
1777 prop = of_find_property(regs, "ocv_table", &length);
1780 data->ocv_size= length / sizeof(u32);
1781 /* read brightness levels from DT property */
1782 if (data->ocv_size > 0) {
1783 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
1784 data->battery_ocv= devm_kzalloc(rk818->dev, size, GFP_KERNEL);
1785 if (!data->battery_ocv)
1787 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
1788 DBG("the battery OCV TABLE : ");
1789 for(i =0; i< data->ocv_size; i++ )
1790 DBG("%d ", data->battery_ocv[i]);
1795 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
1798 data->max_charger_currentmA= out_value;
1799 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
1802 data->max_charger_voltagemV= out_value;
1803 ret = of_property_read_u32(regs, "design_capacity", &out_value);
1806 cell_cfg->design_capacity = out_value;
1807 ret = of_property_read_u32(regs, "design_qmax", &out_value);
1810 cell_cfg->design_qmax =out_value;
1811 data->cell_cfg =cell_cfg;
1812 rk818->battery_data = data;
1813 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
1814 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
1815 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
1816 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
1820 static struct of_device_id rk818_battery_of_match[] = {
1821 { .compatible = "rk818_battery" },
1825 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
1827 static int rk818_battery_parse_dt(struct device *dev)
1833 static int battery_probe(struct platform_device *pdev)
1835 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
1836 // struct battery_platform_data *pdata ;//= rk818_platform_data->battery_data;
1837 // struct battery_platform_data defdata ;//= rk818_platform_data->battery_data;
1838 struct battery_info *di;
1839 struct ocv_config *ocv;
1840 struct edv_config *edv;
1843 DBG("%s is the battery driver version %s\n",__FUNCTION__,DRIVER_VERSION);
1844 rk818_battery_parse_dt(chip);
1846 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1848 dev_err(&pdev->dev, "no memory for state\n");
1852 ocv = devm_kzalloc(&pdev->dev, sizeof(*ocv), GFP_KERNEL);
1854 dev_err(&pdev->dev, "ocv no memory for state\n");
1858 edv = devm_kzalloc(&pdev->dev, sizeof(*edv), GFP_KERNEL);
1860 dev_err(&pdev->dev, "edv no memory for state\n");
1867 di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
1868 if (!di->platform_data) {
1874 platform_set_drvdata(pdev, di);
1875 /*apply battery cell configuration*/
1876 //di->cell.config = di->platform_data->cell_cfg;
1877 di->platform_data = chip->battery_data;
1878 di->platform_data->cell_cfg = chip->battery_data->cell_cfg;
1879 di->platform_data->cell_cfg->ocv = ocv;
1880 di->platform_data->cell_cfg->edv = edv;
1881 di->design_capacity = chip->battery_data->cell_cfg->design_capacity;
1882 di->qmax = chip->battery_data->cell_cfg->design_qmax;
1883 di->fcc = di->design_capacity;
1884 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1886 battery_powersupply_init(di);
1888 ret = power_supply_register(&pdev->dev, &di->bat);
1890 dev_dbg(&pdev->dev, "failed to register main battery\n");
1893 ret = power_supply_register(&pdev->dev, &di->usb);
1895 dev_dbg(&pdev->dev, "failed to register usb power supply\n");
1898 ret = power_supply_register(&pdev->dev, &di->ac);
1900 dev_dbg(&pdev->dev, "failed to register ac power supply\n");
1904 di->wq = create_singlethread_workqueue("battery-work");
1905 INIT_DELAYED_WORK(&di->battery_monitor_work,rk818_battery_work);
1906 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1907 //queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1908 INIT_DELAYED_WORK(&di->charge_check_work,rk818_battery_charge_check_work);
1910 di->battery_nb.notifier_call = battery_notifier_call;
1911 register_battery_notifier(&di->battery_nb);
1912 printk("battery probe ok... \n");
1916 power_supply_unregister(&di->ac);
1918 power_supply_unregister(&di->usb);
1920 power_supply_unregister(&di->bat);
1924 static int battery_remove(struct platform_device *dev)
1929 static int battery_suspend(struct platform_device *dev,pm_message_t state)
1931 struct battery_info *di = platform_get_drvdata(dev);
1932 DBG("%s--------------------\n",__FUNCTION__);
1934 printk("battery NULL di\n");
1935 cancel_delayed_work(&di ->battery_monitor_work);
1936 DBG("%s---------end--------\n",__FUNCTION__);
1941 static int battery_resume(struct platform_device *dev)
1946 struct battery_info *di = platform_get_drvdata(dev);
1948 ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1950 // struct battery_info *di = platform_get_drvdata(dev);
1951 DBG("%s--------------------\n",__FUNCTION__);
1952 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1954 DBG("charge--status 0x%02x--------------------buf = 0x%02x\n", get_charge_status( di),buf);
1959 static struct platform_driver battery_driver = {
1960 .probe = battery_probe,
1961 .remove = battery_remove,
1962 .suspend = battery_suspend,
1963 .resume = battery_resume,
1966 .name = "rk818-battery",
1968 .of_match_table = of_match_ptr(rk818_battery_of_match),
1972 static int __init battery_init(void)
1974 return platform_driver_register(&battery_driver);
1976 fs_initcall_sync(battery_init);
1977 static void __exit battery_exit(void)
1979 platform_driver_unregister(&battery_driver);
1981 module_exit(battery_exit);
1983 MODULE_LICENSE("GPL");
1984 MODULE_ALIAS("platform:rk818-battery");
1985 MODULE_AUTHOR("ROCKCHIP");