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/gpio.h>
21 #include <linux/proc_fs.h>
22 #include <asm/uaccess.h>
23 #include <linux/power/rk818_battery.h>
24 #include <linux/mfd/rk818.h>
25 #include <linux/time.h>
26 #include <linux/interrupt.h>
27 #include <linux/rtc.h>
28 #include <linux/wakelock.h>
32 /* if you want to disable, don't set it as 0, just be: "static int dbg_enable;" is ok*/
33 static int dbg_enable;
34 #define RK818_SYS_DBG 1
36 module_param_named(dbg_level, dbg_enable, int, 0644);
37 #define DBG(args...) \
45 #define DEFAULT_BAT_RES 135
46 #define DEFAULT_VLMT 4200
47 #define DEFAULT_ILMT 2000
48 #define DEFAULT_ICUR 1600
50 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
51 #define DSOC_DISCHRG_FAST_EER_RANGE 25
52 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
53 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
54 #define DSOC_CHRG_FAST_EER_RANGE 25
55 #define DSOC_CHRG_EMU_CURR 1000
56 #define DSOC_CHG_TERM_CURR 500
58 /*realtime RSOC calib param*/
59 #define RSOC_DISCHG_ERR_LOWER 40
60 #define RSOC_DISCHG_ERR_UPPER 50
61 #define RSOC_ERR_CHCK_CNT 15
62 #define RSOC_COMPS 20 /*compensation*/
63 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
64 #define RSOC_CALIB_DISCHGR_TIME 3 /*min*/
66 #define INTERPOLATE_MAX 1000
67 #define MAX_INT 0x7FFF
68 #define TIME_10MIN_SEC 600
70 #define CHG_VOL_SHIFT 4
71 #define CHG_ILIM_SHIFT 0
72 #define CHG_ICUR_SHIFT 0
74 int CHG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
75 int CHG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000};
76 int CHG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000};
79 struct cell_state cell;
80 struct power_supply bat;
81 struct power_supply ac;
82 struct power_supply usb;
83 struct delayed_work work;
84 /* struct i2c_client *client; */
87 struct battery_platform_data *platform_data;
99 uint16_t relax_voltage;
103 bool pcb_ioffset_updated;
104 unsigned long queue_work_cnt;
108 uint16_t warnning_voltage;
126 int bat_res_update_cnt;
133 unsigned long dod0_time;
144 int current_k;/* (ICALIB0, ICALIB1) */
147 int voltage_k;/* VCALIB0 VCALIB1 */
160 struct timeval soc_timer;
161 struct timeval change_timer;
164 int charge_smooth_time;
166 int suspend_capacity;
168 struct timespec suspend_time;
169 struct timespec resume_time;
170 unsigned long suspend_time_start;
171 unsigned long count_sleep_time;
173 unsigned long dischrg_sum_sleep_sec;
174 unsigned long dischrg_sum_sleep_capacity;
175 int suspend_temp_soc;
177 int suspend_charge_current;
180 bool bat_res_updated;
181 bool charge_smooth_status;
183 unsigned long last_plugin_time;
186 unsigned long charging_time;
187 unsigned long discharging_time;
188 unsigned long finish_time;
193 struct notifier_block battery_nb;
194 struct workqueue_struct *wq;
195 struct delayed_work battery_monitor_work;
196 struct delayed_work charge_check_work;
199 struct wake_lock resume_wake_lock;
201 int debug_finish_real_soc;
202 int debug_finish_temp_soc;
210 struct battery_info *g_battery;
211 u32 support_uboot_chrg;
213 extern int dwc_vbus_status(void);
214 extern int get_gadget_connect_flag(void);
215 extern int dwc_otg_check_dpdm(void);
216 extern void kernel_power_off(void);
217 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
218 extern unsigned int irq_create_mapping(struct irq_domain *domain,
219 irq_hw_number_t hwirq);
220 extern void rk_send_wakeup_key(void);
221 static void update_battery_info(struct battery_info *di);
223 #define SUPPORT_USB_CHARGE
226 static u32 interpolate(int value, u32 *table, int size)
231 for (i = 0; i < size; i++) {
232 if (value < table[i])
236 if ((i > 0) && (i < size)) {
237 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
238 d /= table[i] - table[i-1];
239 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
241 d = i * ((INTERPOLATE_MAX+size/2)/size);
249 /* Returns (a * b) / c */
250 static int32_t ab_div_c(u32 a, u32 b, u32 c)
256 sign = ((((a^b)^c) & 0x80000000) != 0);
262 tmp = ((int32_t) a*b + (c>>1)) / c;
274 static int32_t abs_int(int32_t x)
276 return (x > 0) ? x : -x;
279 static int abs32_int(int x)
281 return (x > 0) ? x : -x;
285 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
289 ret = rk818_i2c_read(rk818, reg, len, buf);
293 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
296 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
299 static void dump_gauge_register(struct battery_info *di)
303 DBG("%s dump charger register start: \n", __func__);
304 for (i = 0xAC; i < 0xDF; i++) {
305 battery_read(di->rk818, i, &buf, 1);
306 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
311 static void dump_charger_register(struct battery_info *di)
316 DBG("%s dump the register start: \n", __func__);
317 for (i = 0x99; i < 0xAB; i++) {
318 battery_read(di->rk818, i, &buf, 1);
319 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
327 static uint16_t _get_OCV_voltage(struct battery_info *di);
328 static int _voltage_to_capacity(struct battery_info *di, int voltage);
329 static int _get_realtime_capacity(struct battery_info *di);
330 static void power_on_save(struct battery_info *di, int voltage);
331 static void _capacity_init(struct battery_info *di, u32 capacity);
332 static void battery_poweron_status_init(struct battery_info *di);
333 static void flatzone_voltage_init(struct battery_info *di);
334 static int _get_FCC_capacity(struct battery_info *di);
335 static void _save_FCC_capacity(struct battery_info *di, u32 capacity);
336 static int _get_soc(struct battery_info *di);
337 static int _get_average_current(struct battery_info *di);
338 static int rk_battery_voltage(struct battery_info *di);
339 static uint16_t _get_relax_vol1(struct battery_info *di);
340 static uint16_t _get_relax_vol2(struct battery_info *di);
341 static void update_battery_info(struct battery_info *di);
343 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
345 struct battery_info *di = g_battery;
351 battery_read(di->rk818, SUP_STS_REG, &status, 1);
352 battery_read(di->rk818, SOC_REG, &soc_reg, 1);
353 battery_read(di->rk818, 0x00, &rtc_val, 1);
354 di->voltage_ocv = _get_OCV_voltage(di);
355 _voltage_to_capacity(di, di->voltage_ocv);
356 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
358 return sprintf(buf, "-----------------------------------------------------------------------------\n"
359 "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
360 "real_soc = %d, temp_soc = %d\n"
361 "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
362 "usb_online = %d, ac_online = %d\n"
363 "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
364 "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
365 "relax_volt1 = %d, relax_volt2 = %d\n"
366 "---------------------------------------------------------------------------\n",
367 rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
368 di->real_soc, _get_soc(di),
369 di->fcc, _get_FCC_capacity(di), shtd_time,
370 di->usb_online, di->ac_online,
372 di->voltage_k, di->voltage_b, soc_reg,
373 _get_relax_vol1(di), _get_relax_vol2(di));
376 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
378 struct battery_info *di = g_battery;
379 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
380 u8 usb_ctrl_reg, chrg_ctrl_reg1;
381 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
383 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
384 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
385 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
386 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
387 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
388 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
389 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
390 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
391 battery_read(di->rk818, 0x00, &rtc_val, 1);
393 return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
394 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
395 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
396 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
397 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
398 "---------------------------------------------------------------------------\n",
399 ggcon_reg, ggsts_reg, rtc_val,
400 sup_tst_reg, vb_mod_reg,
401 usb_ctrl_reg, chrg_ctrl_reg1,
402 chrg_ctrl_reg2, chrg_ctrl_reg3
405 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
407 struct battery_info *di = g_battery;
409 return sprintf(buf, "%d", di->fcc);
411 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
413 struct battery_info *di = g_battery;
415 return sprintf(buf, "%d", di->real_soc);
418 static ssize_t bat_soc_write(struct device *dev,
419 struct device_attribute *attr,
420 const char *buf, size_t count)
424 struct battery_info *di = g_battery;
426 ret = sscanf(buf, "%d", &val);
431 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
433 struct battery_info *di = g_battery;
435 return sprintf(buf, "%d", di->temp_soc);
438 static ssize_t bat_temp_soc_write(struct device *dev,
439 struct device_attribute *attr,
440 const char *buf, size_t count)
445 struct battery_info *di = g_battery;
447 ret = sscanf(buf, "%d", &val);
448 capacity = di->fcc*val/100;
449 _capacity_init(di, capacity);
450 di->temp_soc = _get_soc(di);
451 di->remain_capacity = _get_realtime_capacity(di);
456 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
458 struct battery_info *di = g_battery;
460 return sprintf(buf, "%d", di->voltage);
463 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
465 struct battery_info *di = g_battery;
467 return sprintf(buf, "%d", di->current_avg);
470 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
472 struct battery_info *di = g_battery;
474 return sprintf(buf, "%d", di->remain_capacity);
477 static struct device_attribute rk818_bat_attr[] = {
478 __ATTR(state, 0664, bat_state_read, NULL),
479 __ATTR(regs, 0664, bat_reg_read, NULL),
480 __ATTR(fcc, 0664, bat_fcc_read, NULL),
481 __ATTR(soc, 0664, bat_soc_read, bat_soc_write),
482 __ATTR(temp_soc, 0664, bat_temp_soc_read, bat_temp_soc_write),
483 __ATTR(voltage, 0664, bat_voltage_read, NULL),
484 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
485 __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
490 static uint16_t get_relax_voltage(struct battery_info *di);
492 static ssize_t show_state_attrs(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct battery_info *data = g_battery;
497 if (0 == get_relax_voltage(data)) {
499 "voltage = %d, remain_capacity = %d, status = %d\n",
500 data->voltage, data->remain_capacity,
505 "voltage = %d, remain_capacity = %d, status = %d\n",
506 get_relax_voltage(data), data->remain_capacity,
510 static ssize_t restore_state_attrs(struct device *dev,
511 struct device_attribute *attr, const char *buf, size_t size)
515 static struct device_attribute rkbatt_attrs[] = {
516 __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
519 static int create_sysfs_interfaces(struct device *dev)
523 for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++) {
524 if (device_create_file(dev, rkbatt_attrs + liTmep))
531 for (; liTmep >= 0; liTmep--)
532 device_remove_file(dev, rkbatt_attrs + liTmep);
534 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
538 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
542 battery_read(di->rk818, reg, &val, 1);
543 DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
548 static int _gauge_enable(struct battery_info *di)
553 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
555 dev_err(di->dev, "error reading TS_CTRL_REG");
558 if (!(buf & GG_EN)) {
560 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); /* enable */
561 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
565 DBG("%s, %d\n", __func__, buf);
569 static void save_level(struct battery_info *di, u8 save_soc)
574 battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
576 static u8 get_level(struct battery_info *di)
580 battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
584 static int _get_vcalib0(struct battery_info *di)
590 ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
592 ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
595 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
599 static int _get_vcalib1(struct battery_info *di)
605 ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
607 ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
610 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
614 static int _get_ioffset(struct battery_info *di)
621 ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
623 ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
629 static uint16_t _get_cal_offset(struct battery_info *di)
635 ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
637 ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
642 static int _set_cal_offset(struct battery_info *di, u32 value)
648 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
649 buf = (value >> 8)&0xff;
650 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
655 static void _get_voltage_offset_value(struct battery_info *di)
657 int vcalib0, vcalib1;
659 vcalib0 = _get_vcalib0(di);
660 vcalib1 = _get_vcalib1(di);
662 di->voltage_k = (4200 - 3000)*1000/(vcalib1 - vcalib0);
663 di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
664 DBG("voltage_k = %d(x1000) voltage_b = %d\n", di->voltage_k, di->voltage_b);
666 static uint16_t _get_OCV_voltage(struct battery_info *di)
671 uint16_t voltage_now = 0;
673 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
675 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
679 dev_err(di->dev, "error read BAT_OCV_REGH");
683 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
688 static int rk_battery_voltage(struct battery_info *di)
695 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
697 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
701 dev_err(di->dev, "error read BAT_VOL_REGH");
705 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
711 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
712 * based on the voltage.
714 static int _voltage_to_capacity(struct battery_info *di, int voltage)
721 ocv_table = di->platform_data->battery_ocv;
722 ocv_size = di->platform_data->ocv_size;
723 di->warnning_voltage = ocv_table[3];
724 tmp = interpolate(voltage, ocv_table, ocv_size);
725 di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
726 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
731 static uint16_t _get_relax_vol1(struct battery_info *di)
735 uint16_t temp = 0, voltage_now;
737 ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
739 ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
742 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
747 static uint16_t _get_relax_vol2(struct battery_info *di)
750 uint16_t temp = 0, voltage_now;
753 ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
755 ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
758 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
763 static int _get_raw_adc_current(struct battery_info *di)
769 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
771 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
775 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
777 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
780 current_now |= (buf<<8);
783 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
791 static void reset_zero_var(struct battery_info *di)
799 static void ioffset_sample_time(struct battery_info *di, int time)
803 battery_read(di->rk818, GGCON, &ggcon, 1);
804 ggcon &= ~(0x30); /*clear <5:4>*/
806 battery_write(di->rk818, GGCON, &ggcon, 1);
807 debug_reg(di, GGCON, "GGCON");
810 static void update_cal_offset(struct battery_info *di)
812 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
814 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
815 if ((!mod) && (di->pcb_ioffset_updated)) {
816 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
817 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
822 static void zero_current_calibration(struct battery_info *di)
831 if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
833 for (retry = 0; retry < 5; retry++) {
834 adc_value = _get_raw_adc_current(di);
835 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
837 C0 = _get_cal_offset(di);
839 _set_cal_offset(di, C1);
840 DBG("<%s>. C1 = %d\n", __func__, C1);
843 adc_value = _get_raw_adc_current(di);
844 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
847 ioffset = _get_ioffset(di);
848 pcb_offset = C1 - ioffset;
849 di->pcb_ioffset = pcb_offset;
850 di->pcb_ioffset_updated = true;
851 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
854 di->pcb_ioffset_updated = false;
860 static bool _is_relax_mode(struct battery_info *di)
865 ret = battery_read(di->rk818, GGSTS, &status, 1);
867 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
873 static uint16_t get_relax_voltage(struct battery_info *di)
877 uint16_t relax_vol1, relax_vol2;
880 ret = battery_read(di->rk818, GGSTS, &status, 1);
881 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
883 relax_vol1 = _get_relax_vol1(di);
884 relax_vol2 = _get_relax_vol2(di);
885 DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
886 if (_is_relax_mode(di))
887 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
892 static void _set_relax_thres(struct battery_info *di)
895 int enter_thres, exit_thres;
896 struct cell_state *cell = &di->cell;
898 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
899 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
901 buf = enter_thres&0xff;
902 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
903 buf = (enter_thres>>8)&0xff;
904 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
906 buf = exit_thres&0xff;
907 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
908 buf = (exit_thres>>8)&0xff;
909 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
911 /* set sample time */
912 battery_read(di->rk818, GGCON, &buf, 1);
913 buf &= ~(3<<2);/*8min*/
914 buf &= ~0x01; /* clear bat_res calc*/
915 battery_write(di->rk818, GGCON, &buf, 1);
918 static void restart_relax(struct battery_info *di)
920 u8 ggcon;/* chrg_ctrl_reg2;*/
923 battery_read(di->rk818, GGCON, &ggcon, 1);
925 battery_write(di->rk818, GGCON, &ggcon, 1);
927 battery_read(di->rk818, GGSTS, &ggsts, 1);
929 battery_write(di->rk818, GGSTS, &ggsts, 1);
932 static int _get_average_current(struct battery_info *di)
939 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
941 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
945 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
947 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
950 current_now |= (buf<<8);
952 if (current_now & 0x800)
955 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
961 static bool is_bat_exist(struct battery_info *di)
965 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
966 return (buf & 0x80) ? true : false;
969 static bool _is_first_poweron(struct battery_info *di)
974 battery_read(di->rk818, GGSTS, &buf, 1);
975 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
976 /*di->pwron_bat_con = buf;*/
980 battery_write(di->rk818, GGSTS, &buf, 1);
981 battery_read(di->rk818, GGSTS, &temp, 1);
982 } while (temp&BAT_CON);
987 static void flatzone_voltage_init(struct battery_info *di)
994 ocv_table = di->platform_data->battery_ocv;
995 ocv_size = di->platform_data->ocv_size;
997 for (j = 0; j < 21; j++)
1001 for (i = 1; i < ocv_size-1; i++) {
1002 if (ocv_table[i+1] < ocv_table[i] + 20)
1003 temp_table[j++] = i;
1006 temp_table[j] = temp_table[j-1]+1;
1008 di->enter_flatzone = ocv_table[i];
1012 for (i = 0; i <= 20; i++) {
1013 if (temp_table[i] < temp_table[i+1])
1018 di->exit_flatzone = ocv_table[i];
1020 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
1025 static int is_not_flatzone(struct battery_info *di, int voltage)
1027 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
1028 DBG("<%s>. is in flat zone\n", __func__);
1031 DBG("<%s>. is not in flat zone\n", __func__);
1036 static void power_on_save(struct battery_info *di, int voltage)
1041 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
1043 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
1044 _voltage_to_capacity(di, voltage);
1045 if (di->temp_soc < 20) {
1046 di->dod0_voltage = voltage;
1047 di->dod0_capacity = di->nac;
1048 di->dod0_status = 1;
1049 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
1050 di->dod0_level = 80;
1052 if (di->temp_soc <= 0)
1053 di->dod0_level = 100;
1054 else if (di->temp_soc < 5)
1055 di->dod0_level = 95;
1056 else if (di->temp_soc < 10)
1057 di->dod0_level = 90;
1058 /* save_soc = di->dod0_level; */
1059 save_soc = get_level(di);
1060 if (save_soc < di->dod0_level)
1061 save_soc = di->dod0_level;
1062 save_level(di, save_soc);
1063 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
1069 static int _get_full_soc(struct battery_info *di)
1071 if(abs_int(di->fcc - di->remain_capacity) < di->fcc/100)
1074 return di->remain_capacity * 100 / di->fcc;
1076 static int _get_soc(struct battery_info *di)
1078 return di->remain_capacity * 100 / di->fcc;
1081 static enum power_supply_property rk_battery_props[] = {
1083 POWER_SUPPLY_PROP_STATUS,
1084 POWER_SUPPLY_PROP_CURRENT_NOW,
1085 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1086 POWER_SUPPLY_PROP_PRESENT,
1087 POWER_SUPPLY_PROP_HEALTH,
1088 POWER_SUPPLY_PROP_CAPACITY,
1091 #define to_device_info(x) container_of((x), \
1092 struct battery_info, bat)
1094 static int rk_battery_get_property(struct power_supply *psy,
1095 enum power_supply_property psp,
1096 union power_supply_propval *val)
1099 struct battery_info *di = to_device_info(psy);
1102 case POWER_SUPPLY_PROP_CURRENT_NOW:
1103 val->intval = di->current_avg*1000;/*uA*/
1106 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1107 val->intval = di->voltage*1000;/*uV*/
1110 case POWER_SUPPLY_PROP_PRESENT:
1111 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1112 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1113 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1117 case POWER_SUPPLY_PROP_CAPACITY:
1118 val->intval = di->real_soc;
1121 case POWER_SUPPLY_PROP_HEALTH:
1122 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1125 case POWER_SUPPLY_PROP_STATUS:
1126 val->intval = di->status;
1137 static enum power_supply_property rk_battery_ac_props[] = {
1138 POWER_SUPPLY_PROP_ONLINE,
1140 static enum power_supply_property rk_battery_usb_props[] = {
1141 POWER_SUPPLY_PROP_ONLINE,
1145 #define to_ac_device_info(x) container_of((x), \
1146 struct battery_info, ac)
1148 static int rk_battery_ac_get_property(struct power_supply *psy,
1149 enum power_supply_property psp,
1150 union power_supply_propval *val)
1153 struct battery_info *di = to_ac_device_info(psy);
1156 case POWER_SUPPLY_PROP_ONLINE:
1157 val->intval = di->ac_online; /*discharging*/
1167 #define to_usb_device_info(x) container_of((x), \
1168 struct battery_info, usb)
1170 static int rk_battery_usb_get_property(struct power_supply *psy,
1171 enum power_supply_property psp,
1172 union power_supply_propval *val)
1175 struct battery_info *di = to_usb_device_info(psy);
1178 case POWER_SUPPLY_PROP_ONLINE:
1179 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1182 val->intval = di->usb_online;
1194 static void battery_power_supply_init(struct battery_info *di)
1196 di->bat.name = "BATTERY";
1197 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1198 di->bat.properties = rk_battery_props;
1199 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1200 di->bat.get_property = rk_battery_get_property;
1203 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1204 di->ac.properties = rk_battery_ac_props;
1205 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1206 di->ac.get_property = rk_battery_ac_get_property;
1208 di->usb.name = "USB";
1209 di->usb.type = POWER_SUPPLY_TYPE_USB;
1210 di->usb.properties = rk_battery_usb_props;
1211 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1212 di->usb.get_property = rk_battery_usb_get_property;
1215 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1219 ret = power_supply_register(dev, &di->bat);
1221 dev_err(dev, "failed to register main battery\n");
1224 ret = power_supply_register(dev, &di->usb);
1226 dev_err(dev, "failed to register usb power supply\n");
1229 ret = power_supply_register(dev, &di->ac);
1231 dev_err(dev, "failed to register ac power supply\n");
1238 power_supply_unregister(&di->ac);
1240 power_supply_unregister(&di->usb);
1242 power_supply_unregister(&di->bat);
1247 static void _capacity_init(struct battery_info *di, u32 capacity)
1254 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1256 buf = (capacity_ma>>24)&0xff;
1257 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1258 buf = (capacity_ma>>16)&0xff;
1259 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1260 buf = (capacity_ma>>8)&0xff;
1261 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1262 buf = (capacity_ma&0xff) | 0x01;
1263 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1264 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1270 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1275 if (capacity >= di->qmax)
1276 capacity = di->qmax;
1278 capacity_ma = capacity;
1280 buf = (capacity_ma>>24)&0xff;
1281 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1282 buf = (capacity_ma>>16)&0xff;
1283 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1284 buf = (capacity_ma>>8)&0xff;
1285 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1286 buf = (capacity_ma&0xff) | 0x01;
1287 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1290 static int _get_remain_capacity(struct battery_info *di)
1297 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1299 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1301 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1303 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1306 capacity = temp;/* /4096*900/14/36*500/521; */
1312 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1317 capacity_ma = capacity;
1318 buf = (capacity_ma>>24)&0xff;
1319 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1320 buf = (capacity_ma>>16)&0xff;
1321 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1322 buf = (capacity_ma>>8)&0xff;
1323 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1324 buf = (capacity_ma&0xff) | 0x01;
1325 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1328 static int _get_FCC_capacity(struct battery_info *di)
1335 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1337 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1339 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1341 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1345 capacity = temp-1;/* 4096*900/14/36*500/521 */
1348 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1353 static int _get_realtime_capacity(struct battery_info *di)
1360 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1362 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1364 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1366 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1369 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1374 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1376 int remain_capacity;
1384 now_temp_soc = _get_soc(di);
1385 _voltage_to_capacity(di, relax_voltage);
1386 relax_soc = di->temp_soc;
1387 relax_capacity = di->temp_nac;
1388 abs_soc = abs32_int(relax_soc - now_temp_soc);
1390 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1391 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1394 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1395 remain_capacity = relax_capacity;
1396 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1399 remain_capacity = _get_realtime_capacity(di);
1400 else if (abs_soc <= 10)
1401 remain_capacity = relax_capacity;
1402 else if (abs_soc <= 20)
1403 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1405 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1407 _capacity_init(di, remain_capacity);
1408 di->temp_soc = _get_soc(di);
1409 di->remain_capacity = _get_realtime_capacity(di);
1412 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1413 if (relax_soc < now_temp_soc) {
1414 if (di->suspend_temp_soc - relax_soc <= 5)
1415 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1416 else if (di->suspend_temp_soc - relax_soc <= 10)
1417 di->real_soc = di->real_soc - 5;
1419 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1421 now_current = _get_average_current(di);
1422 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1423 min = soc_time / 60;
1424 if (sleep_min > min)
1428 DBG("<%s>. new_temp_soc=%d, new_real_soc=%d, new_remain_cap=%d\n", __func__, _get_soc(di), di->real_soc, di->remain_capacity);
1432 static int _copy_soc(struct battery_info *di, u8 save_soc)
1437 battery_write(di->rk818, SOC_REG, &soc, 1);
1441 static bool support_uboot_charge(void)
1443 return support_uboot_chrg?true:false;
1446 static int _rsoc_init(struct battery_info *di)
1450 u32 remain_capacity;
1453 #ifdef SUPPORT_USB_CHARGE
1458 di->voltage = rk_battery_voltage(di);
1459 di->voltage_ocv = _get_OCV_voltage(di);
1460 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1462 if (_is_first_poweron(di)) {
1463 _save_FCC_capacity(di, di->design_capacity);
1464 di->fcc = _get_FCC_capacity(di);
1466 _voltage_to_capacity(di, di->voltage_ocv);
1467 di->real_soc = di->temp_soc;
1468 di->nac = di->temp_nac;
1469 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1472 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1473 init_soc = pwron_soc;
1474 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1476 #ifdef SUPPORT_USB_CHARGE
1477 otg_status = dwc_otg_check_dpdm();
1478 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1480 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1483 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1484 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1486 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1489 remain_capacity = _get_remain_capacity(di);
1491 if (support_uboot_charge())
1494 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1495 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1496 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1497 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1499 //if (!support_uboot_charge()) {
1501 _voltage_to_capacity(di, di->voltage_ocv);
1502 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1504 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1505 if (last_shtd_time != curr_shtd_time) {
1507 if (curr_shtd_time > 30) {
1508 remain_capacity = di->temp_nac;
1509 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1511 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - init_soc) >= 10)) {
1512 if (remain_capacity >= di->temp_nac*120/100)
1513 remain_capacity = di->temp_nac*110/100;
1514 else if (remain_capacity < di->temp_nac*8/10)
1515 remain_capacity = di->temp_nac*9/10;
1517 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1522 di->real_soc = init_soc;
1523 di->nac = remain_capacity;
1526 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1532 static u8 get_charge_status(struct battery_info *di)
1537 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1542 DBG(" CHARGE-OFF ...\n");
1547 DBG(" DEAD CHARGE ...\n");
1550 case TRICKLE_CHARGE: /* (0x02 << 4) */
1552 DBG(" TRICKLE CHARGE ...\n ");
1555 case CC_OR_CV: /* (0x03 << 4) */
1557 DBG(" CC or CV ...\n");
1560 case CHARGE_FINISH: /* (0x04 << 4) */
1561 ret = CHARGE_FINISH;
1562 DBG(" CHARGE FINISH ...\n");
1565 case USB_OVER_VOL: /* (0x05 << 4) */
1567 DBG(" USB OVER VOL ...\n");
1570 case BAT_TMP_ERR: /* (0x06 << 4) */
1572 DBG(" BAT TMP ERROR ...\n");
1575 case TIMER_ERR: /* (0x07 << 4) */
1577 DBG(" TIMER ERROR ...\n");
1580 case USB_EXIST: /* (1 << 1)// usb is exists */
1582 DBG(" USB EXIST ...\n");
1585 case USB_EFF: /* (1 << 0)// usb is effective */
1587 DBG(" USB EFF...\n");
1597 static void set_charge_current(struct battery_info *di, int charge_current)
1601 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1602 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1603 usb_ctrl_reg |= (charge_current);
1604 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1607 static void fg_match_param(struct battery_info *di, int chg_vol, int chg_ilim, int chg_cur)
1611 di->chg_v_lmt = CHRG_VOL4200;
1612 di->chg_i_lmt = ILIM_1750MA;
1613 di->chg_i_cur = CHRG_CUR1400mA;
1615 for (i=0; i<ARRAY_SIZE(CHG_V_LMT); i++){
1616 if (chg_vol < CHG_V_LMT[i])
1619 di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1622 for (i=0; i<ARRAY_SIZE(CHG_I_LMT); i++){
1623 if (chg_ilim < CHG_I_LMT[i])
1626 di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1629 for (i=0; i<ARRAY_SIZE(CHG_I_CUR); i++){
1630 if (chg_cur < CHG_I_CUR[i])
1633 di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1635 DBG("vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
1636 di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
1639 static void rk_battery_charger_init(struct battery_info *di)
1641 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1644 int chg_vol = di->rk818->battery_data->max_charger_voltagemV;
1645 int chg_cur = di->rk818->battery_data->max_charger_currentmA;
1646 int chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
1647 fg_match_param(di, chg_vol, chg_ilim, chg_cur);
1648 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1649 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1650 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1651 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1652 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1654 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1655 usb_ctrl_reg &= (~0x0f);
1656 #ifdef SUPPORT_USB_CHARGE
1657 usb_ctrl_reg |= (ILIM_450MA);
1659 usb_ctrl_reg |= (di->chg_i_lmt);
1661 chrg_ctrl_reg1 &= (0x00);
1662 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
1664 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1665 chrg_ctrl_reg2 &= ~(0xc0);
1666 chrg_ctrl_reg2 |= FINISH_100MA;
1668 sup_sts_reg &= ~(0x01 << 3);
1669 sup_sts_reg |= (0x01 << 2);
1671 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1672 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1673 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1674 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1675 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1677 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1678 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1679 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1680 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1682 DBG("%s end\n", __func__);
1685 void charge_disable_open_otg(int value)
1687 struct battery_info *di = g_battery;
1690 DBG("charge disable, enable OTG.\n");
1691 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1692 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1695 DBG("charge enable, disable OTG.\n");
1696 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1697 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1701 static void low_waring_init(struct battery_info *di)
1706 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1708 /* 2.8v~3.5v, interrupt */
1709 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1710 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1713 static int set_low_power_interrupt(struct battery_info *di)
1718 ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1719 buf =(buf&0xE8)|(1<<3)|0x110;
1720 ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1726 //set power off voltage 3.0v
1727 static int set_low_power_shutdown(struct battery_info *di)
1732 ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1733 buf =(buf&0xE8)|0x10 ;
1734 ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1739 static void fg_init(struct battery_info *di)
1743 adc_ctrl_val = 0x30;
1744 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1747 /* get the volatege offset */
1748 _get_voltage_offset_value(di);
1749 rk_battery_charger_init(di);
1750 _set_relax_thres(di);
1751 /* get the current offset , the value write to the CAL_OFFSET */
1752 di->current_offset = _get_ioffset(di);
1753 _set_cal_offset(di, di->current_offset+42);
1755 _capacity_init(di, di->nac);
1757 di->remain_capacity = _get_realtime_capacity(di);
1758 di->current_avg = _get_average_current(di);
1760 low_waring_init(di);
1762 power_on_save(di, di->voltage_ocv);
1763 /* set sample time for cal_offset interval*/
1764 ioffset_sample_time(di, SAMP_TIME_8MIN);
1765 set_low_power_shutdown(di);
1766 dump_gauge_register(di);
1767 dump_charger_register(di);
1770 "nac = %d , remain_capacity = %d\n"
1771 "OCV_voltage = %d, voltage = %d\n"
1772 "SOC = %d, fcc = %d\n, current=%d",
1774 di->nac, di->remain_capacity,
1775 di->voltage_ocv, di->voltage,
1776 di->real_soc, di->fcc, di->current_avg);
1780 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1781 static void zero_get_soc(struct battery_info *di)
1783 int dead_voltage, ocv_voltage;
1784 int temp_soc = -1, real_soc;
1785 int currentold, currentnow, voltage;
1792 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1794 currentold = _get_average_current(di);
1795 _get_cal_offset(di);
1798 currentnow = _get_average_current(di);
1800 } while ((currentold == currentnow) && (count_num < 11));
1803 for (i = 0; i < 10 ; i++)
1804 voltage += rk_battery_voltage(di);
1807 if (di->voltage_old == 0)
1808 di->voltage_old = voltage;
1809 voltage_k = voltage;
1810 voltage = (di->voltage_old*2 + 8*voltage)/10;
1811 di->voltage_old = voltage;
1812 currentnow = _get_average_current(di);
1814 dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
1815 /* 65 mo power-path mos */
1816 ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
1817 DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
1818 dead_voltage, ocv_voltage);
1820 _voltage_to_capacity(di, dead_voltage);
1821 di->q_dead = di->temp_nac;
1822 DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
1823 di->temp_soc, di->q_dead);
1825 _voltage_to_capacity(di, ocv_voltage);
1826 q_ocv = di->temp_nac;
1827 DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
1828 di->temp_soc, q_ocv);
1830 /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
1831 di->q_err = di->remain_capacity - q_ocv;
1832 DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
1833 di->q_err, di->remain_capacity, q_ocv);
1835 if (di->display_soc == 0)
1836 di->display_soc = di->real_soc*1000;
1837 real_soc = di->display_soc;
1839 DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
1840 di->remain_capacity, di->q_dead, di->q_err);
1841 /*[temp_nac]:dead_voltage*/
1842 if (q_ocv > di->q_dead) {
1843 DBG("first: q_ocv > di->q_dead\n");
1845 if (di->update_k == 0 || di->update_k >= 10) {
1846 if (di->update_k == 0) {
1848 /* ZQ = Q_ded + Qerr */
1849 /*[temp_nac]:dead_voltage*/
1850 di->q_shtd = di->q_dead + di->q_err;
1851 temp_soc = (di->remain_capacity - di->q_shtd)*
1856 di->line_k = (real_soc + temp_soc/2)
1859 DBG("[K >= 10].\n");
1860 temp_soc = ((di->remain_capacity - di->q_shtd)*
1861 1000 + di->fcc/2)/di->fcc; /* x1 10 */
1863 real_soc = (di->line_k*temp_soc); /*y1=k0*x1*/
1864 di->display_soc = real_soc;
1865 DBG("[K >= 10]. (temp_soc)X0 = %d\n", temp_soc);
1866 DBG("[K >= 10]. in:line_k = %d\n", di->line_k);
1867 DBG("[K >= 10]. (dis-soc)Y0=%d,real-soc=%d\n",
1868 di->display_soc, di->real_soc);
1870 if ((real_soc+500)/1000 < di->real_soc){
1872 di->odd_capacity = 0;
1874 else if (((real_soc+500))/1000 ==
1877 real_soc -= di->odd_capacity;
1878 if ((real_soc+500)/1000 <
1881 di->odd_capacity = 0;
1885 DBG("[k >= 10]. odd_capacity=%d\n",
1888 di->odd_capacity = 0;
1889 _voltage_to_capacity(di, dead_voltage);
1890 di->q_dead = di->temp_nac;
1891 di->q_shtd = di->q_dead + di->q_err;
1892 temp_soc = ((di->remain_capacity - di->q_shtd)*
1893 1000 + di->fcc/2)/di->fcc; /* z1 */
1897 di->line_k = (di->display_soc +
1898 temp_soc/2)/temp_soc;
1899 DBG("[K >= 10]. out:line_k = %d\n", di->line_k);
1905 else { /*update_k[1~9]*/
1910 temp_soc = ((di->remain_capacity - di->q_shtd)*
1911 1000 + di->fcc/2)/di->fcc;
1912 di->display_soc = di->line_k*temp_soc;
1913 DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
1914 DBG("[K1~9]. line_k = %d\n", di->line_k);
1915 DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
1916 di->display_soc, di->real_soc);
1917 if ((di->display_soc+500)/1000 < di->real_soc){
1919 di->odd_capacity = 0;
1921 else if ((real_soc+500)/1000 == di->real_soc) {
1923 real_soc -= di->odd_capacity;
1924 if ((real_soc+500)/1000 < di->real_soc) {
1926 di->odd_capacity = 0;
1928 di->odd_capacity += real_soc/3000+2;
1929 DBG("[K1~9]. odd_capacity=%d\n",
1932 di->odd_capacity = 0;
1935 DBG("second: q_ocv < di->q_dead\n");
1937 if ((di->voltage < 3400) && (di->real_soc > 10)) {
1938 /*di->real_soc = 10;*/
1940 } else if (di->voltage < 3400) {
1941 /*10 -(3.4-Vbat)*100*I*/
1942 if (di->current_avg < 1000)
1943 soc_time = 10-((3400-di->voltage)/10*
1944 abs32_int(di->current_avg))/1000;
1946 DBG("<%s>. ZERO: decrease sec = %d\n",
1947 __func__, soc_time/2);
1948 if (di->update_k > soc_time/2) {
1953 if (di->update_k > 10) {
1960 if (di->line_k <= 0) {
1962 DBG("ZERO: line_k <= 0, Update line_k!\n");
1965 DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
1966 DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
1967 DBG("ZERO: remain_cap - q_shtd=%d\n",
1968 (di->remain_capacity - di->q_shtd));
1969 DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
1970 di->line_k, di->display_soc, temp_soc);
1971 DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
1972 di->remain_capacity, di->q_shtd, di->q_err);
1973 DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
1974 di->warnning_voltage, _get_soc(di), di->real_soc);
1978 static int estimate_bat_ocv_vol(struct battery_info *di)
1980 return (di->voltage -
1981 (di->bat_res * di->current_avg) / 1000);
1984 static int estimate_bat_ocv_soc(struct battery_info *di)
1986 int ocv_soc, ocv_voltage;
1988 ocv_voltage = estimate_bat_ocv_vol(di);
1989 _voltage_to_capacity(di, ocv_voltage);
1990 ocv_soc = di->temp_soc;
1995 static void rsoc_dischrg_calib(struct battery_info *di)
1997 int ocv_soc = di->est_ocv_soc;
1998 int ocv_volt = di->est_ocv_vol;
1999 int temp_soc = _get_soc(di);
2000 int max_volt = di->rk818->battery_data->max_charger_voltagemV;
2002 if (ocv_volt > max_volt)
2005 if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
2006 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
2007 (di->temp_soc == 0) ||
2008 (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
2011 di->err_soc_sum += ocv_soc;
2015 DBG("<%s>. rsoc err_chck_cnt = %d\n",
2016 __func__, di->err_chck_cnt);
2017 DBG("<%s>. rsoc err_soc_sum = %d\n",
2018 __func__, di->err_soc_sum);
2020 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2022 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2023 if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
2024 ocv_soc += RSOC_COMPS;
2026 di->temp_nac = ocv_soc * di->fcc / 100;
2027 _capacity_init(di, di->temp_nac);
2028 di->temp_soc = _get_soc(di);
2029 di->remain_capacity = _get_realtime_capacity(di);
2030 di->err_soc_sum = 0;
2031 di->err_chck_cnt = 0;
2032 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2036 di->err_chck_cnt = 0;
2037 di->err_soc_sum = 0;
2042 static void rsoc_realtime_calib(struct battery_info *di)
2044 u8 status = di->status;
2046 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2047 (status == POWER_SUPPLY_STATUS_FULL)) {
2049 if ((di->current_avg < -10) &&
2050 (di->charge_status != CHARGE_FINISH))
2051 rsoc_dischrg_calib(di);
2054 rsoc_chrg_calib(di);
2057 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2058 rsoc_dischrg_calib(di);
2062 static bool do_ac_charger_emulator(struct battery_info *di)
2064 int delta_soc = di->temp_soc - di->real_soc;
2067 if ((di->charge_status != CHARGE_FINISH)
2069 && (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)){
2071 soc_time = di->fcc*3600/100/(abs_int(DSOC_CHRG_EMU_CURR));
2073 if (di->emu_chg_cnt > soc_time) {
2075 di->emu_chg_cnt = 0;
2077 DBG("<%s>. soc_time=%d, emu_cnt=%d\n",
2078 __func__, soc_time, di->emu_chg_cnt);
2086 static bool do_term_chrg_cali(struct battery_info *di)
2090 if (di->ac_online &&
2091 (di->real_soc >= 90)&&
2092 (di->current_avg > 600)){
2094 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2096 if (di->term_chg_cnt > soc_time) {
2098 di->term_chg_cnt = 0;
2100 DBG("<%s>. soc_time=%d, term_cnt=%d\n",
2101 __func__, soc_time, di->term_chg_cnt);
2109 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
2112 int now_current, soc_time = -1;
2114 int delta_soc = di->real_soc - di->temp_soc;
2116 voltage = di->voltage;
2117 now_current = di->current_avg;
2118 if (now_current == 0)
2121 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE){
2122 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2123 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2124 __func__, delta_soc);
2126 soc_time = di->fcc*3600/100/(abs_int(now_current));
2127 _voltage_to_capacity(di, 3800);
2128 volt_to_soc = di->temp_soc;
2129 di->temp_soc = _get_full_soc(di);
2131 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
2132 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2133 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
2137 } else if (di->temp_soc == di->real_soc) {
2138 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2139 } else if (di->temp_soc > di->real_soc) {
2140 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2141 di->vol_smooth_time++;
2142 if (di->vol_smooth_time > soc_time*3/2) {
2144 di->vol_smooth_time = 0;
2148 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2149 if (di->real_soc == (di->temp_soc + 1)) {
2150 di->change_timer = di->soc_timer;
2151 di->real_soc = di->temp_soc;
2153 di->vol_smooth_time++;
2156 if (di->vol_smooth_time > soc_time*1/4) {
2158 di->vol_smooth_time = 0;
2161 if (di->vol_smooth_time > soc_time*3/4) {
2163 di->vol_smooth_time = 0;
2169 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2170 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
2173 static int get_charging_time(struct battery_info *di)
2175 return (di->charging_time/60);
2178 static int get_discharging_time(struct battery_info *di)
2180 return (di->discharging_time/60);
2183 static int get_finish_time(struct battery_info *di)
2185 return (di->finish_time/60);
2188 static void dump_debug_info(struct battery_info *di)
2190 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2191 u8 usb_ctrl_reg, chrg_ctrl_reg1;
2192 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
2194 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2195 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2196 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2197 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2198 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2199 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2200 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2201 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2202 battery_read(di->rk818, 0x00, &rtc_val, 1);
2204 DBG("\n------------- dump_debug_regs -----------------\n"
2205 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2206 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2207 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2208 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2209 ggcon_reg, ggsts_reg, rtc_val,
2210 sup_tst_reg, vb_mod_reg,
2211 usb_ctrl_reg, chrg_ctrl_reg1,
2212 chrg_ctrl_reg2, chrg_ctrl_reg3
2216 "########################## [read] ################################\n"
2217 "-----------------------------------------------------------------\n"
2218 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2219 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2220 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2221 "diplay_soc = %d, cpapacity_soc = %d\n"
2222 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2223 "finish_real_soc = %d, finish_temp_soc = %d\n"
2224 "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2225 get_relax_voltage(di),
2226 di->voltage, di->current_avg,
2227 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2228 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2229 di->real_soc, _get_soc(di),
2230 di->ac_online, di->usb_online, di->status,
2231 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2232 get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2234 get_charge_status(di);
2235 DBG("################################################################\n");
2238 static void update_fcc_capacity(struct battery_info *di)
2240 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2241 if (get_level(di) >= di->dod0_level) {
2242 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
2243 if (di->fcc > di->qmax)
2246 _capacity_init(di, di->fcc);
2247 _save_FCC_capacity(di, di->fcc);
2249 di->dod0_status = 0;
2253 static void debug_get_finish_soc(struct battery_info *di)
2255 if (di->charge_status == CHARGE_FINISH) {
2256 di->debug_finish_real_soc = di->real_soc;
2257 di->debug_finish_temp_soc = di->temp_soc;
2261 static void wait_charge_finish_signal(struct battery_info *di)
2263 if (di->charge_status == CHARGE_FINISH)
2264 update_fcc_capacity(di);/* save new fcc*/
2267 debug_get_finish_soc(di);
2270 static void charge_finish_routine(struct battery_info *di)
2272 if ((di->charge_status == CHARGE_FINISH)&&
2273 (di->finish_min >= 1)) {
2274 _capacity_init(di, di->fcc);
2275 zero_current_calibration(di);
2277 if (di->real_soc < 100) {
2278 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
2279 if ((di->soc_counter < 80)) {
2282 di->soc_counter = 0;
2289 static void voltage_to_soc_charge_smooth(struct battery_info *di)
2291 int now_current, soc_time;
2294 /*calibrate: aim to match finish signal*/
2295 if (do_term_chrg_cali(di))
2298 /*calibrate: aim to calib error*/
2299 di->term_chg_cnt = 0;
2300 if (do_ac_charger_emulator(di))
2303 di->emu_chg_cnt = 0;
2304 now_current = _get_average_current(di);
2305 if (now_current == 0)
2308 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
2309 di->temp_soc = _get_soc(di);
2311 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2313 if (di->real_soc == di->temp_soc) {
2314 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2315 di->temp_soc = _get_soc(di);
2317 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2319 if (di->temp_soc < di->real_soc + 1) {
2320 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2321 di->charge_smooth_time++;
2322 if (di->charge_smooth_time > soc_time*3/2) {
2324 di->charge_smooth_time = 0;
2326 di->charge_smooth_status = true;
2329 else if (di->temp_soc > di->real_soc + 1) {
2330 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2331 di->charge_smooth_time++;
2332 if (di->charge_smooth_time > soc_time*3/4) {
2334 di->charge_smooth_time = 0;
2336 di->charge_smooth_status = true;
2338 } else if (di->temp_soc == di->real_soc + 1) {
2339 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
2340 if (di->charge_smooth_status) {
2341 di->charge_smooth_time++;
2342 if (di->charge_smooth_time > soc_time*3/4) {
2343 di->real_soc = di->temp_soc;
2344 di->charge_smooth_time = 0;
2345 di->charge_smooth_status = false;
2349 di->real_soc = di->temp_soc;
2350 di->charge_smooth_status = false;
2356 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2357 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2360 static void rk_battery_display_smooth(struct battery_info *di)
2365 status = di->status;
2366 charge_status = di->charge_status;
2367 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2369 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2370 voltage_to_soc_discharge_smooth(di);
2372 voltage_to_soc_charge_smooth(di);
2374 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2375 voltage_to_soc_discharge_smooth(di);
2376 if (di->real_soc == 1) {
2378 if (di->time2empty >= 200)
2388 static void software_recharge(struct battery_info *di, int max_cnt)
2390 static int recharge_cnt;
2393 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2394 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2395 chrg_ctrl_reg1 &= ~(1 << 7);
2396 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2397 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2398 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2400 chrg_ctrl_reg1 |= (1 << 7);
2401 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2402 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2403 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2411 static int estimate_battery_resister(struct battery_info *di)
2414 int avr_voltage1 = 0, avr_current1;
2415 int avr_voltage2 = 0, avr_current2;
2417 int bat_res, ocv_votage;
2418 static unsigned long last_time;
2419 unsigned long delta_time;
2420 int charge_ocv_voltage1, charge_ocv_voltage2;
2421 int charge_ocv_soc1, charge_ocv_soc2;
2423 delta_time = get_seconds() - last_time;
2424 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2425 if (delta_time >= 20) {/*20s*/
2428 set_charge_current(di, ILIM_450MA);/*450mA*/
2430 for (i = 0; i < 10 ; i++) {
2432 avr_voltage1 += rk_battery_voltage(di);
2435 avr_current1 = _get_average_current(di);
2436 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2437 DBG("------------------------------------------------------------------------------------------\n");
2438 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2441 set_charge_current(di, ILIM_3000MA);
2443 for (i = 0; i < 10 ; i++) {
2445 avr_voltage2 += rk_battery_voltage(di);
2448 avr_current2 = _get_average_current(di);
2449 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2450 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2452 /*calc resister and ocv_votage ocv*/
2453 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2454 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2455 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2457 /*calc sample voltage ocv*/
2458 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2459 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2460 _voltage_to_capacity(di, charge_ocv_voltage1);
2461 charge_ocv_soc1 = di->temp_soc;
2462 _voltage_to_capacity(di, charge_ocv_voltage2);
2463 charge_ocv_soc2 = di->temp_soc;
2465 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2466 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2467 DBG("------------------------------------------------------------------------------------------\n");
2468 last_time = get_seconds();
2478 static int update_battery_resister(struct battery_info *di)
2482 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2484 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2485 tmp_res = estimate_battery_resister(di);
2487 di->bat_res_update_cnt++;
2488 di->bat_res += tmp_res;
2489 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2490 if (di->bat_res_update_cnt == 10) {
2491 di->bat_res_updated = true;
2494 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2503 static void charge_soc_check_routine(struct battery_info *di)
2511 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2512 min = get_charging_time(di);
2513 update_battery_resister(di);
2515 if ((min >= 30) && (di->bat_res_updated)) {
2517 old_temp_soc = di->temp_soc;
2518 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2519 _voltage_to_capacity(di, ocv_voltage);
2520 ocv_temp_soc = di->temp_soc;
2522 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2523 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2524 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2526 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2527 di->temp_soc = ocv_temp_soc;
2529 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2531 remain_capcity = di->temp_soc * di->fcc / 100;
2532 _capacity_init(di, remain_capcity);
2533 di->remain_capacity = _get_realtime_capacity(di);
2534 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2542 static void update_resume_status_relax_voltage(struct battery_info *di)
2544 unsigned long sleep_soc;
2545 unsigned long sum_sleep_soc;
2546 unsigned long sleep_sec;
2551 int sum_sleep_avr_current;
2555 update_battery_info(di);
2557 di->sys_wakeup = true;
2559 DBG("<%s>, resume----------checkstart\n", __func__);
2560 sleep_sec = get_seconds() - di->suspend_time_start;
2561 sleep_min = sleep_sec / 60;
2563 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2564 __func__, sleep_sec, sleep_min);
2566 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2567 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2569 delta_capacity = di->suspend_capacity - di->remain_capacity;
2570 delta_soc = di->suspend_temp_soc - _get_soc(di);
2571 di->dischrg_sum_sleep_capacity += delta_capacity;
2572 di->dischrg_sum_sleep_sec += sleep_sec;
2574 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2575 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2577 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2578 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2579 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2580 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2581 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2582 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2583 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2585 /*large suspend current*/
2586 if (sum_sleep_avr_current > 20) {
2587 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2588 di->real_soc -= sum_sleep_soc;
2589 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2590 __func__, sum_sleep_soc, di->real_soc);
2592 /* small suspend current*/
2593 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2595 relax_voltage = get_relax_voltage(di);
2596 di->voltage = rk_battery_voltage(di);
2598 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2599 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2600 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2603 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2604 if (sum_sleep_soc > 0)
2605 di->real_soc -= sum_sleep_soc;
2609 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2610 di->dischrg_sum_sleep_capacity = 0;
2611 di->dischrg_sum_sleep_sec = 0;
2613 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2614 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2617 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2618 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2619 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2620 di->temp_soc = _get_soc(di);
2621 charge_status = get_charge_status(di);
2623 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2624 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2625 DBG("resume, sleep : ac online charge current < 1000\n");
2626 if (sleep_sec > 0) {
2627 di->count_sleep_time += sleep_sec;
2628 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2629 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2631 di->count_sleep_time = 0;
2632 di->real_soc += sleep_soc;
2633 if (di->real_soc > 100)
2638 DBG("<%s>, usb charging\n", __func__);
2639 if (di->suspend_temp_soc + 15 < di->temp_soc)
2640 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2642 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2645 DBG("POWER_SUPPLY_STATUS_CHARGING: di->temp_soc = %d, di->real_soc = %d, sleep_time = %ld\n ", di->temp_soc , di->real_soc, sleep_sec);
2652 #ifdef SUPPORT_USB_CHARGE
2653 static int get_charging_status_type(struct battery_info *di)
2655 int otg_status = dwc_otg_check_dpdm();
2657 if (0 == otg_status) {
2660 di->check_count = 0;
2662 } else if (1 == otg_status) {
2663 if (0 == get_gadget_connect_flag()) {
2664 if (++di->check_count >= 5) {
2676 } else if (2 == otg_status) {
2679 di->check_count = 0;
2682 if (di->ac_online == 1)
2683 set_charge_current(di, di->chg_i_lmt);
2685 set_charge_current(di, ILIM_450MA);
2691 static void battery_poweron_status_init(struct battery_info *di)
2695 #ifndef SUPPORT_USB_CHARGE
2699 #ifdef SUPPORT_USB_CHARGE
2701 otg_status = dwc_otg_check_dpdm();
2702 if (otg_status == 1) {
2705 set_charge_current(di, ILIM_450MA);
2706 di->status = POWER_SUPPLY_STATUS_CHARGING;
2707 DBG("++++++++ILIM_450MA++++++\n");
2709 } else if (otg_status == 2) {
2712 di->status = POWER_SUPPLY_STATUS_CHARGING;
2713 set_charge_current(di, di->chg_i_lmt);
2714 DBG("++++++++ILIM_1000MA++++++\n");
2716 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2720 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2721 if (buf&PLUG_IN_STS) {
2724 di->status = POWER_SUPPLY_STATUS_CHARGING;
2725 if (di->real_soc == 100)
2726 di->status = POWER_SUPPLY_STATUS_FULL;
2728 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2732 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2735 static void check_battery_status(struct battery_info *di)
2740 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2741 #ifdef SUPPORT_USB_CHARGE
2743 if (strstr(saved_command_line, "charger")) {
2744 if ((buf&PLUG_IN_STS) == 0) {
2745 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2751 if (buf&PLUG_IN_STS) {
2752 get_charging_status_type(di);
2754 di->status = POWER_SUPPLY_STATUS_CHARGING;
2755 if (di->real_soc == 100)
2756 di->status = POWER_SUPPLY_STATUS_FULL;
2758 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2765 if (buf & PLUG_IN_STS) {
2768 di->status = POWER_SUPPLY_STATUS_CHARGING;
2769 if (di->real_soc == 100)
2770 di->status = POWER_SUPPLY_STATUS_FULL;
2772 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2779 static void last_check_report(struct battery_info *di)
2781 /* high load: current < 0 with charger in.
2782 * System will not shutdown when dsoc=0% with charging state(ac_online),
2783 * which will cause over discharge, so oppose status.
2787 if ((di->real_soc == 0) && (di->status == POWER_SUPPLY_STATUS_CHARGING)
2788 && di->current_avg < 0){
2789 if (get_seconds() - time > 60){
2790 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2794 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
2795 DBG("status=%d, ac_online=%d, usb_online=%d\n",
2796 di->status, di->ac_online, di->usb_online);
2799 time = get_seconds();
2802 static void report_power_supply_changed(struct battery_info *di)
2805 static u32 old_ac_status;
2806 static u32 old_usb_status;
2807 static u32 old_charge_status;
2810 state_changed = false;
2811 if (di->real_soc == 0)
2812 state_changed = true;
2813 else if (di->real_soc == 100)
2814 state_changed = true;
2815 else if (di->real_soc != old_soc)
2816 state_changed = true;
2817 else if (di->ac_online != old_ac_status)
2818 state_changed = true;
2819 else if (di->usb_online != old_usb_status)
2820 state_changed = true;
2821 else if (old_charge_status != di->status)
2822 state_changed = true;
2824 if (state_changed) {
2825 power_supply_changed(&di->bat);
2826 power_supply_changed(&di->usb);
2827 power_supply_changed(&di->ac);
2828 old_soc = di->real_soc;
2829 old_ac_status = di->ac_online;
2830 old_usb_status = di->usb_online;
2831 old_charge_status = di->status;
2835 static void upd_time_table(struct battery_info *di)
2838 static int old_index = 0;
2839 static int old_min = 0;
2841 int mod = di->real_soc % 10;
2842 int index = di->real_soc / 10;
2844 if (di->ac_online || di->usb_online)
2845 time = di->charge_min;
2847 time = di->discharge_min;
2849 if ((mod == 0) && (index > 0) && (old_index != index)) {
2850 di->chrg_min[index-1] = time - old_min;
2855 for (i=1; i<11; i++)
2856 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
2861 static void update_battery_info(struct battery_info *di)
2863 di->remain_capacity = _get_realtime_capacity(di);
2864 if (di->remain_capacity > di->fcc)
2865 _capacity_init(di, di->fcc);
2867 if (di->real_soc > 100)
2869 else if (di->real_soc < 0)
2872 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2873 di->charging_time++;
2874 di->discharging_time = 0;
2876 di->charging_time = 0;
2877 if (di->voltage < 3800)
2878 di->discharging_time += 2;
2880 di->discharging_time++;
2882 if (di->charge_status == CHARGE_FINISH)
2885 di->finish_time = 0;
2887 di->charge_min = get_charging_time(di);
2888 di->discharge_min = get_discharging_time(di);
2889 di->finish_min = get_finish_time(di);
2892 di->est_ocv_vol = estimate_bat_ocv_vol(di);
2893 di->est_ocv_soc = estimate_bat_ocv_soc(di);
2894 di->voltage = rk_battery_voltage(di);
2895 di->current_avg = _get_average_current(di);
2896 di->remain_capacity = _get_realtime_capacity(di);
2897 di->voltage_ocv = _get_OCV_voltage(di);
2898 di->charge_status = get_charge_status(di);
2899 di->otg_status = dwc_otg_check_dpdm();
2900 di->relax_voltage = get_relax_voltage(di);
2901 di->temp_soc = _get_soc(di);
2902 check_battery_status(di);/* ac_online, usb_online, status*/
2903 update_cal_offset(di);
2907 static void rk_battery_work(struct work_struct *work)
2909 struct battery_info *di = container_of(work,
2910 struct battery_info, battery_monitor_work.work);
2912 update_resume_status_relax_voltage(di);
2913 wait_charge_finish_signal(di);
2914 charge_finish_routine(di);
2916 rk_battery_display_smooth(di);
2917 update_battery_info(di);
2918 rsoc_realtime_calib(di);
2919 last_check_report(di);
2920 report_power_supply_changed(di);
2921 _copy_soc(di, di->real_soc);
2922 _save_remain_capacity(di, di->remain_capacity);
2924 dump_debug_info(di);
2925 di->queue_work_cnt++;
2926 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2929 static void rk_battery_charge_check_work(struct work_struct *work)
2931 struct battery_info *di = container_of(work,
2932 struct battery_info, charge_check_work.work);
2934 DBG("rk_battery_charge_check_work\n");
2935 charge_disable_open_otg(di->charge_otg);
2938 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2940 int register_battery_notifier(struct notifier_block *nb)
2942 return blocking_notifier_chain_register(&battery_chain_head, nb);
2944 EXPORT_SYMBOL_GPL(register_battery_notifier);
2946 int unregister_battery_notifier(struct notifier_block *nb)
2948 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2950 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2952 int battery_notifier_call_chain(unsigned long val)
2954 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2955 == NOTIFY_BAD) ? -EINVAL : 0;
2957 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2959 static void poweron_lowerpoer_handle(struct battery_info *di)
2961 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2962 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2964 /* kernel_power_off(); */
2969 static int battery_notifier_call(struct notifier_block *nb,
2970 unsigned long event, void *data)
2972 struct battery_info *di =
2973 container_of(nb, struct battery_info, battery_nb);
2977 DBG(" CHARGE enable\n");
2979 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2984 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2985 DBG("charge disable OTG enable\n");
2989 poweron_lowerpoer_handle(di);
2998 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
3000 pr_info("<%s>lower power warning!\n", __func__);
3002 _copy_soc(g_battery, 0);
3003 _capacity_init(g_battery, 0);
3004 rk_send_wakeup_key();
3009 static void disable_vbat_low_irq(struct battery_info *di)
3012 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
3013 /*clr vbat low interrupt */
3014 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
3016 static void enable_vbat_low_irq(struct battery_info *di)
3018 /* clr vbat low interrupt */
3019 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
3021 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
3024 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
3026 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3027 rk_send_wakeup_key();
3030 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
3032 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3033 charge_disable_open_otg(0);
3034 rk_send_wakeup_key();
3038 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
3040 pr_info("---------- %s:irq = %d\n", __func__, irq);
3041 rk_send_wakeup_key();
3047 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
3051 struct kobject *rk818_fg_kobj;
3053 ret = create_sysfs_interfaces(dev);
3056 dev_err(dev, "device RK818 battery sysfs register failed\n");
3060 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3063 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3064 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3066 dev_err(dev, "create rk818_battery node error\n");
3074 power_supply_unregister(&di->ac);
3075 power_supply_unregister(&di->usb);
3076 power_supply_unregister(&di->bat);
3081 static void rk818_battery_irq_init(struct battery_info *di)
3083 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3085 struct rk818 *chip = di->rk818;
3087 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3088 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3089 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3090 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3092 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3093 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3095 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3097 di->irq = vb_lo_irq;
3098 enable_irq_wake(di->irq);
3099 disable_vbat_low_irq(di);
3101 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3102 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
3104 dev_err(chip->dev, "plug_in_irq request failed!\n");
3107 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3108 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
3110 dev_err(chip->dev, "plug_out_irq request failed!\n");
3113 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3114 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
3116 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3119 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
3125 di->platform_data = chip->battery_data;
3126 di->cell.config = di->platform_data->cell_cfg;
3127 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3128 di->qmax = di->platform_data->cell_cfg->design_qmax;
3129 di->fcc = di->design_capacity;
3130 di->vol_smooth_time = 0;
3131 di->charge_smooth_time = 0;
3132 di->charge_smooth_status = false;
3133 di->sleep_status = 0;
3135 di->sys_wakeup = true;
3136 di->pcb_ioffset = 0;
3137 di->pcb_ioffset_updated = false;
3138 di->queue_work_cnt = 0;
3140 di->voltage_old = 0;
3141 di->display_soc = 0;
3143 di->bat_res_updated = false;
3145 di->sys_wakeup = true;
3146 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3149 di->discharge_min = 0;
3150 di->charging_time = 0;
3151 di->discharging_time = 0;
3152 di->finish_time = 0;
3156 di->odd_capacity = 0;
3157 di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3158 di->term_chg_cnt = 0;
3159 di->emu_chg_cnt = 0;
3161 for (i=0; i<10; i++)
3162 di->chrg_min[i] = -1;
3164 di->debug_finish_real_soc = 0;
3165 di->debug_finish_temp_soc = 0;
3167 fcc_capacity = _get_FCC_capacity(di);
3168 if (fcc_capacity > 1000)
3169 di->fcc = fcc_capacity;
3171 di->fcc = di->design_capacity;
3174 static struct of_device_id rk818_battery_of_match[] = {
3175 { .compatible = "rk818_battery" },
3179 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3182 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3184 struct device_node *regs, *rk818_pmic_np;
3185 struct battery_platform_data *data;
3186 struct cell_config *cell_cfg;
3187 struct ocv_config *ocv_cfg;
3188 struct property *prop;
3192 rk818_pmic_np = of_node_get(rk818->dev->of_node);
3193 if (!rk818_pmic_np) {
3194 dev_err(dev, "could not find pmic sub-node\n");
3198 regs = of_find_node_by_name(rk818_pmic_np, "battery");
3200 dev_err(dev, "battery node not found!\n");
3204 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3206 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3210 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3212 dev_err(dev, "kzalloc for cell_config failed!\n");
3215 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3217 dev_err(dev, "kzalloc for ocv_config failed!\n");
3221 prop = of_find_property(regs, "ocv_table", &length);
3223 dev_err(dev, "ocv_table not found!\n");
3226 data->ocv_size = length / sizeof(u32);
3228 if (data->ocv_size > 0) {
3229 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3231 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3232 if (!data->battery_ocv) {
3233 dev_err(dev, "kzalloc for ocv_table failed!\n");
3236 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
3241 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3243 dev_err(dev, "max_charge_currentmA not found!\n");
3244 out_value = DEFAULT_ICUR;
3246 data->max_charger_currentmA = out_value;
3248 ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3250 dev_err(dev, "max_charger_ilimitmA not found!\n");
3251 out_value = DEFAULT_ILMT;
3253 data->max_charger_ilimitmA = out_value;
3255 ret = of_property_read_u32(regs, "bat_res", &out_value);
3257 dev_err(dev, "bat_res not found!\n");
3258 out_value = DEFAULT_BAT_RES;
3260 data->sense_resistor_mohm = out_value;
3262 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3264 dev_err(dev, "max_charge_voltagemV not found!\n");
3265 out_value = DEFAULT_VLMT;
3267 data->max_charger_voltagemV = out_value;
3269 ret = of_property_read_u32(regs, "design_capacity", &out_value);
3271 dev_err(dev, "design_capacity not found!\n");
3274 cell_cfg->design_capacity = out_value;
3276 ret = of_property_read_u32(regs, "design_qmax", &out_value);
3278 dev_err(dev, "design_qmax not found!\n");
3281 cell_cfg->design_qmax = out_value;
3283 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3285 dev_err(dev, "sleep_enter_current not found!\n");
3288 ocv_cfg->sleep_enter_current = out_value;
3290 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3292 dev_err(dev, "sleep_exit_current not found!\n");
3295 ocv_cfg->sleep_exit_current = out_value;
3297 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3299 cell_cfg->ocv = ocv_cfg;
3300 data->cell_cfg = cell_cfg;
3301 rk818->battery_data = data;
3303 DBG("\n--------- the battery OCV TABLE dump:\n");
3304 DBG("bat_res :%d\n", data->sense_resistor_mohm);
3305 DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3306 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3307 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3308 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3309 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3310 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3311 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3312 DBG("uboot chrg = %d\n", support_uboot_chrg);
3313 DBG("\n--------- rk818_battery dt_parse ok.\n");
3318 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3325 static int battery_probe(struct platform_device *pdev)
3327 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3328 struct battery_info *di;
3331 DBG("battery driver version %s\n", DRIVER_VERSION);
3332 di = kzalloc(sizeof(*di), GFP_KERNEL);
3334 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
3337 ret = rk_battery_parse_dt(chip, &pdev->dev);
3339 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
3343 platform_set_drvdata(pdev, di);
3344 battery_info_init(di, chip);
3345 if (!is_bat_exist(di)) {
3346 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
3351 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
3353 flatzone_voltage_init(di);
3354 battery_poweron_status_init(di);
3355 battery_power_supply_init(di);
3356 ret = battery_power_supply_register(di, &pdev->dev);
3358 dev_err(&pdev->dev, "rk power supply register failed!\n");
3361 di->wq = create_singlethread_workqueue("battery-work");
3362 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
3363 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
3364 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
3366 di->battery_nb.notifier_call = battery_notifier_call;
3367 register_battery_notifier(&di->battery_nb);
3369 rk818_battery_irq_init(di);
3370 rk818_battery_sysfs_init(di, &pdev->dev);
3371 DBG("------ RK81x battery_probe ok!-------\n");
3378 static int battery_suspend(struct platform_device *dev, pm_message_t state)
3380 struct battery_info *di = platform_get_drvdata(dev);
3382 enable_vbat_low_irq(di);
3383 di->sleep_status = di->status;
3384 di->suspend_charge_current = _get_average_current(di);
3386 /* avoid abrupt wakeup which will clean the variable*/
3387 if (di->sys_wakeup) {
3388 di->suspend_capacity = di->remain_capacity;
3389 di->suspend_temp_soc = _get_soc(di);
3390 di->suspend_time_start = get_seconds();
3391 di->sys_wakeup = false;
3394 cancel_delayed_work(&di->battery_monitor_work);
3395 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
3396 __func__, di->suspend_temp_soc, di->suspend_charge_current,
3397 di->suspend_capacity, di->sleep_status);
3399 set_low_power_interrupt(di);
3403 static int battery_resume(struct platform_device *dev)
3405 struct battery_info *di = platform_get_drvdata(dev);
3407 set_low_power_interrupt(di);
3409 DBG("<%s>\n", __func__);
3410 disable_vbat_low_irq(di);
3411 queue_delayed_work(di->wq, &di->battery_monitor_work,
3412 msecs_to_jiffies(TIMER_MS_COUNTS/2));
3414 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3416 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3421 static int battery_remove(struct platform_device *dev)
3423 struct battery_info *di = platform_get_drvdata(dev);
3425 cancel_delayed_work_sync(&di->battery_monitor_work);
3428 static void battery_shutdown(struct platform_device *dev)
3430 struct battery_info *di = platform_get_drvdata(dev);
3432 cancel_delayed_work_sync(&di->battery_monitor_work);
3433 DBG("rk818 shutdown!");
3437 static struct platform_driver battery_driver = {
3439 .name = "rk818-battery",
3440 .owner = THIS_MODULE,
3443 .probe = battery_probe,
3444 .remove = battery_remove,
3445 .suspend = battery_suspend,
3446 .resume = battery_resume,
3447 .shutdown = battery_shutdown,
3450 static int __init battery_init(void)
3452 return platform_driver_register(&battery_driver);
3455 fs_initcall_sync(battery_init);
3456 static void __exit battery_exit(void)
3458 platform_driver_unregister(&battery_driver);
3460 module_exit(battery_exit);
3462 MODULE_LICENSE("GPL");
3463 MODULE_ALIAS("platform:rk818-battery");
3464 MODULE_AUTHOR("ROCKCHIP");