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 printk("_get_full_soc:%d,%d\n",di->fcc,di->remain_capacity);
1072 if(abs_int(di->fcc - di->remain_capacity) < di->fcc/100)
1075 return di->remain_capacity * 100 / di->fcc;
1077 static int _get_soc(struct battery_info *di)
1079 return di->remain_capacity * 100 / di->fcc;
1082 static enum power_supply_property rk_battery_props[] = {
1084 POWER_SUPPLY_PROP_STATUS,
1085 POWER_SUPPLY_PROP_CURRENT_NOW,
1086 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1087 POWER_SUPPLY_PROP_PRESENT,
1088 POWER_SUPPLY_PROP_HEALTH,
1089 POWER_SUPPLY_PROP_CAPACITY,
1092 #define to_device_info(x) container_of((x), \
1093 struct battery_info, bat)
1095 static int rk_battery_get_property(struct power_supply *psy,
1096 enum power_supply_property psp,
1097 union power_supply_propval *val)
1100 struct battery_info *di = to_device_info(psy);
1103 case POWER_SUPPLY_PROP_CURRENT_NOW:
1104 val->intval = di->current_avg*1000;/*uA*/
1107 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1108 val->intval = di->voltage*1000;/*uV*/
1111 case POWER_SUPPLY_PROP_PRESENT:
1112 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1113 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1114 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1118 case POWER_SUPPLY_PROP_CAPACITY:
1119 val->intval = di->real_soc;
1122 case POWER_SUPPLY_PROP_HEALTH:
1123 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1126 case POWER_SUPPLY_PROP_STATUS:
1127 val->intval = di->status;
1138 static enum power_supply_property rk_battery_ac_props[] = {
1139 POWER_SUPPLY_PROP_ONLINE,
1141 static enum power_supply_property rk_battery_usb_props[] = {
1142 POWER_SUPPLY_PROP_ONLINE,
1146 #define to_ac_device_info(x) container_of((x), \
1147 struct battery_info, ac)
1149 static int rk_battery_ac_get_property(struct power_supply *psy,
1150 enum power_supply_property psp,
1151 union power_supply_propval *val)
1154 struct battery_info *di = to_ac_device_info(psy);
1157 case POWER_SUPPLY_PROP_ONLINE:
1158 val->intval = di->ac_online; /*discharging*/
1168 #define to_usb_device_info(x) container_of((x), \
1169 struct battery_info, usb)
1171 static int rk_battery_usb_get_property(struct power_supply *psy,
1172 enum power_supply_property psp,
1173 union power_supply_propval *val)
1176 struct battery_info *di = to_usb_device_info(psy);
1179 case POWER_SUPPLY_PROP_ONLINE:
1180 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1183 val->intval = di->usb_online;
1195 static void battery_power_supply_init(struct battery_info *di)
1197 di->bat.name = "BATTERY";
1198 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1199 di->bat.properties = rk_battery_props;
1200 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1201 di->bat.get_property = rk_battery_get_property;
1204 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1205 di->ac.properties = rk_battery_ac_props;
1206 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1207 di->ac.get_property = rk_battery_ac_get_property;
1209 di->usb.name = "USB";
1210 di->usb.type = POWER_SUPPLY_TYPE_USB;
1211 di->usb.properties = rk_battery_usb_props;
1212 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1213 di->usb.get_property = rk_battery_usb_get_property;
1216 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1220 ret = power_supply_register(dev, &di->bat);
1222 dev_err(dev, "failed to register main battery\n");
1225 ret = power_supply_register(dev, &di->usb);
1227 dev_err(dev, "failed to register usb power supply\n");
1230 ret = power_supply_register(dev, &di->ac);
1232 dev_err(dev, "failed to register ac power supply\n");
1239 power_supply_unregister(&di->ac);
1241 power_supply_unregister(&di->usb);
1243 power_supply_unregister(&di->bat);
1248 static void _capacity_init(struct battery_info *di, u32 capacity)
1255 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1257 buf = (capacity_ma>>24)&0xff;
1258 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1259 buf = (capacity_ma>>16)&0xff;
1260 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1261 buf = (capacity_ma>>8)&0xff;
1262 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1263 buf = (capacity_ma&0xff) | 0x01;
1264 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1265 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1271 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1276 if (capacity >= di->qmax)
1277 capacity = di->qmax;
1279 capacity_ma = capacity;
1281 buf = (capacity_ma>>24)&0xff;
1282 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1283 buf = (capacity_ma>>16)&0xff;
1284 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1285 buf = (capacity_ma>>8)&0xff;
1286 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1287 buf = (capacity_ma&0xff) | 0x01;
1288 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1291 static int _get_remain_capacity(struct battery_info *di)
1298 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1300 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1302 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1304 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1307 capacity = temp;/* /4096*900/14/36*500/521; */
1313 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1318 capacity_ma = capacity;
1319 buf = (capacity_ma>>24)&0xff;
1320 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1321 buf = (capacity_ma>>16)&0xff;
1322 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1323 buf = (capacity_ma>>8)&0xff;
1324 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1325 buf = (capacity_ma&0xff) | 0x01;
1326 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1329 static int _get_FCC_capacity(struct battery_info *di)
1336 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1338 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1340 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1342 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1346 capacity = temp-1;/* 4096*900/14/36*500/521 */
1349 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1354 static int _get_realtime_capacity(struct battery_info *di)
1361 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1363 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1365 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1367 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1370 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1375 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1377 int remain_capacity;
1385 now_temp_soc = _get_soc(di);
1386 _voltage_to_capacity(di, relax_voltage);
1387 relax_soc = di->temp_soc;
1388 relax_capacity = di->temp_nac;
1389 abs_soc = abs32_int(relax_soc - now_temp_soc);
1391 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1392 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1395 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1396 remain_capacity = relax_capacity;
1397 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1400 remain_capacity = _get_realtime_capacity(di);
1401 else if (abs_soc <= 10)
1402 remain_capacity = relax_capacity;
1403 else if (abs_soc <= 20)
1404 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1406 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1408 _capacity_init(di, remain_capacity);
1409 di->temp_soc = _get_soc(di);
1410 di->remain_capacity = _get_realtime_capacity(di);
1413 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1414 if (relax_soc < now_temp_soc) {
1415 if (di->suspend_temp_soc - relax_soc <= 5)
1416 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1417 else if (di->suspend_temp_soc - relax_soc <= 10)
1418 di->real_soc = di->real_soc - 5;
1420 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1422 now_current = _get_average_current(di);
1423 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1424 min = soc_time / 60;
1425 if (sleep_min > min)
1429 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);
1433 static int _copy_soc(struct battery_info *di, u8 save_soc)
1438 battery_write(di->rk818, SOC_REG, &soc, 1);
1442 static bool support_uboot_charge(void)
1444 return support_uboot_chrg?true:false;
1447 static int _rsoc_init(struct battery_info *di)
1451 u32 remain_capacity;
1454 #ifdef SUPPORT_USB_CHARGE
1459 di->voltage = rk_battery_voltage(di);
1460 di->voltage_ocv = _get_OCV_voltage(di);
1461 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1463 if (_is_first_poweron(di)) {
1464 _save_FCC_capacity(di, di->design_capacity);
1465 di->fcc = _get_FCC_capacity(di);
1467 _voltage_to_capacity(di, di->voltage_ocv);
1468 di->real_soc = di->temp_soc;
1469 di->nac = di->temp_nac;
1470 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1473 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1474 init_soc = pwron_soc;
1475 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1477 #ifdef SUPPORT_USB_CHARGE
1478 otg_status = dwc_otg_check_dpdm();
1479 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1481 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1484 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1485 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1487 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1490 remain_capacity = _get_remain_capacity(di);
1492 if (support_uboot_charge())
1495 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1496 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1497 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1498 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1500 //if (!support_uboot_charge()) {
1502 _voltage_to_capacity(di, di->voltage_ocv);
1503 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1505 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1506 if (last_shtd_time != curr_shtd_time) {
1508 if (curr_shtd_time > 30) {
1509 remain_capacity = di->temp_nac;
1510 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1512 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - init_soc) >= 10)) {
1513 if (remain_capacity >= di->temp_nac*120/100)
1514 remain_capacity = di->temp_nac*110/100;
1515 else if (remain_capacity < di->temp_nac*8/10)
1516 remain_capacity = di->temp_nac*9/10;
1518 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1523 di->real_soc = init_soc;
1524 di->nac = remain_capacity;
1527 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1533 static u8 get_charge_status(struct battery_info *di)
1538 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1543 DBG(" CHARGE-OFF ...\n");
1548 DBG(" DEAD CHARGE ...\n");
1551 case TRICKLE_CHARGE: /* (0x02 << 4) */
1553 DBG(" TRICKLE CHARGE ...\n ");
1556 case CC_OR_CV: /* (0x03 << 4) */
1558 DBG(" CC or CV ...\n");
1561 case CHARGE_FINISH: /* (0x04 << 4) */
1562 ret = CHARGE_FINISH;
1563 DBG(" CHARGE FINISH ...\n");
1566 case USB_OVER_VOL: /* (0x05 << 4) */
1568 DBG(" USB OVER VOL ...\n");
1571 case BAT_TMP_ERR: /* (0x06 << 4) */
1573 DBG(" BAT TMP ERROR ...\n");
1576 case TIMER_ERR: /* (0x07 << 4) */
1578 DBG(" TIMER ERROR ...\n");
1581 case USB_EXIST: /* (1 << 1)// usb is exists */
1583 DBG(" USB EXIST ...\n");
1586 case USB_EFF: /* (1 << 0)// usb is effective */
1588 DBG(" USB EFF...\n");
1598 static void set_charge_current(struct battery_info *di, int charge_current)
1602 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1603 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1604 usb_ctrl_reg |= (charge_current);
1605 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1608 static void fg_match_param(struct battery_info *di, int chg_vol, int chg_ilim, int chg_cur)
1612 di->chg_v_lmt = CHRG_VOL4200;
1613 di->chg_i_lmt = ILIM_1750MA;
1614 di->chg_i_cur = CHRG_CUR1400mA;
1616 for (i=0; i<ARRAY_SIZE(CHG_V_LMT); i++){
1617 if (chg_vol < CHG_V_LMT[i])
1620 di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1623 for (i=0; i<ARRAY_SIZE(CHG_I_LMT); i++){
1624 if (chg_ilim < CHG_I_LMT[i])
1627 di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1630 for (i=0; i<ARRAY_SIZE(CHG_I_CUR); i++){
1631 if (chg_cur < CHG_I_CUR[i])
1634 di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1636 DBG("vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
1637 di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
1640 static void rk_battery_charger_init(struct battery_info *di)
1642 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1645 int chg_vol = di->rk818->battery_data->max_charger_voltagemV;
1646 int chg_cur = di->rk818->battery_data->max_charger_currentmA;
1647 int chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
1648 fg_match_param(di, chg_vol, chg_ilim, chg_cur);
1649 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1650 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1651 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1652 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1653 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1655 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1656 usb_ctrl_reg &= (~0x0f);
1657 #ifdef SUPPORT_USB_CHARGE
1658 usb_ctrl_reg |= (ILIM_450MA);
1660 usb_ctrl_reg |= (di->chg_i_lmt);
1662 chrg_ctrl_reg1 &= (0x00);
1663 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
1665 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1666 chrg_ctrl_reg2 &= ~(0xc0);
1667 chrg_ctrl_reg2 |= FINISH_100MA;
1669 sup_sts_reg &= ~(0x01 << 3);
1670 sup_sts_reg |= (0x01 << 2);
1672 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1673 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1674 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1675 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1676 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1678 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1679 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1680 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1681 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1683 DBG("%s end\n", __func__);
1686 void charge_disable_open_otg(int value)
1688 struct battery_info *di = g_battery;
1691 DBG("charge disable, enable OTG.\n");
1692 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1693 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1696 DBG("charge enable, disable OTG.\n");
1697 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1698 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1702 static void low_waring_init(struct battery_info *di)
1707 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1709 /* 2.8v~3.5v, interrupt */
1710 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1711 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1714 static int set_low_power_interrupt(struct battery_info *di)
1719 ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1720 buf =(buf&0xE8)|(1<<3)|0x110;
1721 ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1727 //set power off voltage 3.0v
1728 static int set_low_power_shutdown(struct battery_info *di)
1733 ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1734 buf =(buf&0xE8)|0x10 ;
1735 ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1740 static void fg_init(struct battery_info *di)
1744 adc_ctrl_val = 0x30;
1745 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1748 /* get the volatege offset */
1749 _get_voltage_offset_value(di);
1750 rk_battery_charger_init(di);
1751 _set_relax_thres(di);
1752 /* get the current offset , the value write to the CAL_OFFSET */
1753 di->current_offset = _get_ioffset(di);
1754 _set_cal_offset(di, di->current_offset+42);
1756 _capacity_init(di, di->nac);
1758 di->remain_capacity = _get_realtime_capacity(di);
1759 di->current_avg = _get_average_current(di);
1761 low_waring_init(di);
1763 power_on_save(di, di->voltage_ocv);
1764 /* set sample time for cal_offset interval*/
1765 ioffset_sample_time(di, SAMP_TIME_8MIN);
1766 set_low_power_shutdown(di);
1767 dump_gauge_register(di);
1768 dump_charger_register(di);
1771 "nac = %d , remain_capacity = %d\n"
1772 "OCV_voltage = %d, voltage = %d\n"
1773 "SOC = %d, fcc = %d\n, current=%d",
1775 di->nac, di->remain_capacity,
1776 di->voltage_ocv, di->voltage,
1777 di->real_soc, di->fcc, di->current_avg);
1781 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1782 static void zero_get_soc(struct battery_info *di)
1784 int dead_voltage, ocv_voltage;
1785 int temp_soc = -1, real_soc;
1786 int currentold, currentnow, voltage;
1793 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1795 currentold = _get_average_current(di);
1796 _get_cal_offset(di);
1799 currentnow = _get_average_current(di);
1801 } while ((currentold == currentnow) && (count_num < 11));
1804 for (i = 0; i < 10 ; i++)
1805 voltage += rk_battery_voltage(di);
1808 if (di->voltage_old == 0)
1809 di->voltage_old = voltage;
1810 voltage_k = voltage;
1811 voltage = (di->voltage_old*2 + 8*voltage)/10;
1812 di->voltage_old = voltage;
1813 currentnow = _get_average_current(di);
1815 dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
1816 /* 65 mo power-path mos */
1817 ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
1818 DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
1819 dead_voltage, ocv_voltage);
1821 _voltage_to_capacity(di, dead_voltage);
1822 di->q_dead = di->temp_nac;
1823 DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
1824 di->temp_soc, di->q_dead);
1826 _voltage_to_capacity(di, ocv_voltage);
1827 q_ocv = di->temp_nac;
1828 DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
1829 di->temp_soc, q_ocv);
1831 /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
1832 di->q_err = di->remain_capacity - q_ocv;
1833 DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
1834 di->q_err, di->remain_capacity, q_ocv);
1836 if (di->display_soc == 0)
1837 di->display_soc = di->real_soc*1000;
1838 real_soc = di->display_soc;
1840 DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
1841 di->remain_capacity, di->q_dead, di->q_err);
1842 /*[temp_nac]:dead_voltage*/
1843 if (q_ocv > di->q_dead) {
1844 DBG("first: q_ocv > di->q_dead\n");
1846 if (di->update_k == 0 || di->update_k >= 10) {
1847 if (di->update_k == 0) {
1849 /* ZQ = Q_ded + Qerr */
1850 /*[temp_nac]:dead_voltage*/
1851 di->q_shtd = di->q_dead + di->q_err;
1852 temp_soc = (di->remain_capacity - di->q_shtd)*
1857 di->line_k = (real_soc + temp_soc/2)
1860 DBG("[K >= 10].\n");
1861 temp_soc = ((di->remain_capacity - di->q_shtd)*
1862 1000 + di->fcc/2)/di->fcc; /* x1 10 */
1864 real_soc = (di->line_k*temp_soc); /*y1=k0*x1*/
1865 di->display_soc = real_soc;
1866 DBG("[K >= 10]. (temp_soc)X0 = %d\n", temp_soc);
1867 DBG("[K >= 10]. in:line_k = %d\n", di->line_k);
1868 DBG("[K >= 10]. (dis-soc)Y0=%d,real-soc=%d\n",
1869 di->display_soc, di->real_soc);
1871 if ((real_soc+500)/1000 < di->real_soc){
1873 di->odd_capacity = 0;
1875 else if (((real_soc+500))/1000 ==
1878 real_soc -= di->odd_capacity;
1879 if ((real_soc+500)/1000 <
1882 di->odd_capacity = 0;
1886 DBG("[k >= 10]. odd_capacity=%d\n",
1889 di->odd_capacity = 0;
1890 _voltage_to_capacity(di, dead_voltage);
1891 di->q_dead = di->temp_nac;
1892 di->q_shtd = di->q_dead + di->q_err;
1893 temp_soc = ((di->remain_capacity - di->q_shtd)*
1894 1000 + di->fcc/2)/di->fcc; /* z1 */
1898 di->line_k = (di->display_soc +
1899 temp_soc/2)/temp_soc;
1900 DBG("[K >= 10]. out:line_k = %d\n", di->line_k);
1906 else { /*update_k[1~9]*/
1911 temp_soc = ((di->remain_capacity - di->q_shtd)*
1912 1000 + di->fcc/2)/di->fcc;
1913 di->display_soc = di->line_k*temp_soc;
1914 DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
1915 DBG("[K1~9]. line_k = %d\n", di->line_k);
1916 DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
1917 di->display_soc, di->real_soc);
1918 if ((di->display_soc+500)/1000 < di->real_soc){
1920 di->odd_capacity = 0;
1922 else if ((real_soc+500)/1000 == di->real_soc) {
1924 real_soc -= di->odd_capacity;
1925 if ((real_soc+500)/1000 < di->real_soc) {
1927 di->odd_capacity = 0;
1929 di->odd_capacity += real_soc/3000+2;
1930 DBG("[K1~9]. odd_capacity=%d\n",
1933 di->odd_capacity = 0;
1936 DBG("second: q_ocv < di->q_dead\n");
1938 if ((di->voltage < 3400) && (di->real_soc > 10)) {
1939 /*di->real_soc = 10;*/
1941 } else if (di->voltage < 3400) {
1942 /*10 -(3.4-Vbat)*100*I*/
1943 if (di->current_avg < 1000)
1944 soc_time = 10-((3400-di->voltage)/10*
1945 abs32_int(di->current_avg))/1000;
1947 DBG("<%s>. ZERO: decrease sec = %d\n",
1948 __func__, soc_time/2);
1949 if (di->update_k > soc_time/2) {
1954 if (di->update_k > 10) {
1961 if (di->line_k <= 0) {
1963 DBG("ZERO: line_k <= 0, Update line_k!\n");
1966 DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
1967 DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
1968 DBG("ZERO: remain_cap - q_shtd=%d\n",
1969 (di->remain_capacity - di->q_shtd));
1970 DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
1971 di->line_k, di->display_soc, temp_soc);
1972 DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
1973 di->remain_capacity, di->q_shtd, di->q_err);
1974 DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
1975 di->warnning_voltage, _get_soc(di), di->real_soc);
1979 static int estimate_bat_ocv_vol(struct battery_info *di)
1981 return (di->voltage -
1982 (di->bat_res * di->current_avg) / 1000);
1985 static int estimate_bat_ocv_soc(struct battery_info *di)
1987 int ocv_soc, ocv_voltage;
1989 ocv_voltage = estimate_bat_ocv_vol(di);
1990 _voltage_to_capacity(di, ocv_voltage);
1991 ocv_soc = di->temp_soc;
1996 static void rsoc_dischrg_calib(struct battery_info *di)
1998 int ocv_soc = di->est_ocv_soc;
1999 int ocv_volt = di->est_ocv_vol;
2000 int temp_soc = _get_soc(di);
2001 int max_volt = di->rk818->battery_data->max_charger_voltagemV;
2003 if (ocv_volt > max_volt)
2006 if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
2007 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
2008 (di->temp_soc == 0) ||
2009 (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
2012 di->err_soc_sum += ocv_soc;
2016 DBG("<%s>. rsoc err_chck_cnt = %d\n",
2017 __func__, di->err_chck_cnt);
2018 DBG("<%s>. rsoc err_soc_sum = %d\n",
2019 __func__, di->err_soc_sum);
2021 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2023 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2024 if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
2025 ocv_soc += RSOC_COMPS;
2027 di->temp_nac = ocv_soc * di->fcc / 100;
2028 _capacity_init(di, di->temp_nac);
2029 di->temp_soc = _get_soc(di);
2030 di->remain_capacity = _get_realtime_capacity(di);
2031 di->err_soc_sum = 0;
2032 di->err_chck_cnt = 0;
2033 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2037 di->err_chck_cnt = 0;
2038 di->err_soc_sum = 0;
2043 static void rsoc_realtime_calib(struct battery_info *di)
2045 u8 status = di->status;
2047 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2048 (status == POWER_SUPPLY_STATUS_FULL)) {
2050 if ((di->current_avg < -10) &&
2051 (di->charge_status != CHARGE_FINISH))
2052 rsoc_dischrg_calib(di);
2055 rsoc_chrg_calib(di);
2058 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2059 rsoc_dischrg_calib(di);
2063 static bool do_ac_charger_emulator(struct battery_info *di)
2065 int delta_soc = di->temp_soc - di->real_soc;
2068 if ((di->charge_status != CHARGE_FINISH)
2070 && (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)){
2072 soc_time = di->fcc*3600/100/(abs_int(DSOC_CHRG_EMU_CURR));
2074 if (di->emu_chg_cnt > soc_time) {
2076 di->emu_chg_cnt = 0;
2078 DBG("<%s>. soc_time=%d, emu_cnt=%d\n",
2079 __func__, soc_time, di->emu_chg_cnt);
2087 static bool do_term_chrg_cali(struct battery_info *di)
2091 if (di->ac_online &&
2092 (di->real_soc >= 90)&&
2093 (di->current_avg > 600)){
2095 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2097 if (di->term_chg_cnt > soc_time) {
2099 di->term_chg_cnt = 0;
2101 DBG("<%s>. soc_time=%d, term_cnt=%d\n",
2102 __func__, soc_time, di->term_chg_cnt);
2110 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
2113 int now_current, soc_time = -1;
2115 int delta_soc = di->real_soc - di->temp_soc;
2117 voltage = di->voltage;
2118 now_current = di->current_avg;
2119 if (now_current == 0)
2122 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE){
2123 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2124 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2125 __func__, delta_soc);
2127 soc_time = di->fcc*3600/100/(abs_int(now_current));
2128 _voltage_to_capacity(di, 3800);
2129 volt_to_soc = di->temp_soc;
2130 di->temp_soc = _get_full_soc(di);
2132 printk("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
2133 printk("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2134 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
2138 } else if (di->temp_soc == di->real_soc) {
2139 printk("<%s>. di->temp_soc == di->real_soc\n", __func__);
2140 } else if (di->temp_soc > di->real_soc) {
2141 printk("<%s>. di->temp_soc > di->real_soc\n", __func__);
2142 di->vol_smooth_time++;
2143 if (di->vol_smooth_time > soc_time*3/2) {
2145 di->vol_smooth_time = 0;
2149 printk("<%s>. di->temp_soc < di->real_soc\n", __func__);
2150 if (di->real_soc == (di->temp_soc + 1)) {
2151 di->change_timer = di->soc_timer;
2152 di->real_soc = di->temp_soc;
2154 di->vol_smooth_time++;
2157 if (di->vol_smooth_time > soc_time*1/4) {
2159 di->vol_smooth_time = 0;
2162 if (di->vol_smooth_time > soc_time*3/4) {
2164 di->vol_smooth_time = 0;
2170 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2171 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
2174 static int get_charging_time(struct battery_info *di)
2176 return (di->charging_time/60);
2179 static int get_discharging_time(struct battery_info *di)
2181 return (di->discharging_time/60);
2184 static int get_finish_time(struct battery_info *di)
2186 return (di->finish_time/60);
2189 static void dump_debug_info(struct battery_info *di)
2191 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2192 u8 usb_ctrl_reg, chrg_ctrl_reg1;
2193 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
2195 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2196 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2197 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2198 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2199 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2200 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2201 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2202 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2203 battery_read(di->rk818, 0x00, &rtc_val, 1);
2205 DBG("\n------------- dump_debug_regs -----------------\n"
2206 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2207 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2208 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2209 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2210 ggcon_reg, ggsts_reg, rtc_val,
2211 sup_tst_reg, vb_mod_reg,
2212 usb_ctrl_reg, chrg_ctrl_reg1,
2213 chrg_ctrl_reg2, chrg_ctrl_reg3
2217 "########################## [read] ################################\n"
2218 "-----------------------------------------------------------------\n"
2219 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2220 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2221 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2222 "diplay_soc = %d, cpapacity_soc = %d\n"
2223 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2224 "finish_real_soc = %d, finish_temp_soc = %d\n"
2225 "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2226 get_relax_voltage(di),
2227 di->voltage, di->current_avg,
2228 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2229 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2230 di->real_soc, _get_soc(di),
2231 di->ac_online, di->usb_online, di->status,
2232 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2233 get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2235 get_charge_status(di);
2236 DBG("################################################################\n");
2239 static void update_fcc_capacity(struct battery_info *di)
2241 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2242 if (get_level(di) >= di->dod0_level) {
2243 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
2244 if (di->fcc > di->qmax)
2247 _capacity_init(di, di->fcc);
2248 _save_FCC_capacity(di, di->fcc);
2250 di->dod0_status = 0;
2254 static void debug_get_finish_soc(struct battery_info *di)
2256 if (di->charge_status == CHARGE_FINISH) {
2257 di->debug_finish_real_soc = di->real_soc;
2258 di->debug_finish_temp_soc = di->temp_soc;
2262 static void wait_charge_finish_signal(struct battery_info *di)
2264 if (di->charge_status == CHARGE_FINISH)
2265 update_fcc_capacity(di);/* save new fcc*/
2268 debug_get_finish_soc(di);
2271 static void charge_finish_routine(struct battery_info *di)
2273 if ((di->charge_status == CHARGE_FINISH)&&
2274 (di->finish_min >= 1)) {
2275 _capacity_init(di, di->fcc);
2276 zero_current_calibration(di);
2278 if (di->real_soc < 100) {
2279 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
2280 if ((di->soc_counter < 80)) {
2283 di->soc_counter = 0;
2290 static void voltage_to_soc_charge_smooth(struct battery_info *di)
2292 int now_current, soc_time;
2295 /*calibrate: aim to match finish signal*/
2296 if (do_term_chrg_cali(di))
2299 /*calibrate: aim to calib error*/
2300 di->term_chg_cnt = 0;
2301 if (do_ac_charger_emulator(di))
2304 di->emu_chg_cnt = 0;
2305 now_current = _get_average_current(di);
2306 if (now_current == 0)
2309 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
2310 di->temp_soc = _get_soc(di);
2312 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2314 if (di->real_soc == di->temp_soc) {
2315 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2316 di->temp_soc = _get_soc(di);
2318 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2320 if (di->temp_soc < di->real_soc + 1) {
2321 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2322 di->charge_smooth_time++;
2323 if (di->charge_smooth_time > soc_time*3/2) {
2325 di->charge_smooth_time = 0;
2327 di->charge_smooth_status = true;
2330 else if (di->temp_soc > di->real_soc + 1) {
2331 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2332 di->charge_smooth_time++;
2333 if (di->charge_smooth_time > soc_time*3/4) {
2335 di->charge_smooth_time = 0;
2337 di->charge_smooth_status = true;
2339 } else if (di->temp_soc == di->real_soc + 1) {
2340 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
2341 if (di->charge_smooth_status) {
2342 di->charge_smooth_time++;
2343 if (di->charge_smooth_time > soc_time*3/4) {
2344 di->real_soc = di->temp_soc;
2345 di->charge_smooth_time = 0;
2346 di->charge_smooth_status = false;
2350 di->real_soc = di->temp_soc;
2351 di->charge_smooth_status = false;
2357 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2358 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2361 static void rk_battery_display_smooth(struct battery_info *di)
2366 status = di->status;
2367 charge_status = di->charge_status;
2368 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2370 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2371 voltage_to_soc_discharge_smooth(di);
2373 voltage_to_soc_charge_smooth(di);
2375 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2376 voltage_to_soc_discharge_smooth(di);
2377 if (di->real_soc == 1) {
2379 if (di->time2empty >= 200)
2389 static void software_recharge(struct battery_info *di, int max_cnt)
2391 static int recharge_cnt;
2394 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2395 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2396 chrg_ctrl_reg1 &= ~(1 << 7);
2397 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2398 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2399 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2401 chrg_ctrl_reg1 |= (1 << 7);
2402 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2403 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2404 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2412 static int estimate_battery_resister(struct battery_info *di)
2415 int avr_voltage1 = 0, avr_current1;
2416 int avr_voltage2 = 0, avr_current2;
2418 int bat_res, ocv_votage;
2419 static unsigned long last_time;
2420 unsigned long delta_time;
2421 int charge_ocv_voltage1, charge_ocv_voltage2;
2422 int charge_ocv_soc1, charge_ocv_soc2;
2424 delta_time = get_seconds() - last_time;
2425 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2426 if (delta_time >= 20) {/*20s*/
2429 set_charge_current(di, ILIM_450MA);/*450mA*/
2431 for (i = 0; i < 10 ; i++) {
2433 avr_voltage1 += rk_battery_voltage(di);
2436 avr_current1 = _get_average_current(di);
2437 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2438 DBG("------------------------------------------------------------------------------------------\n");
2439 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2442 set_charge_current(di, ILIM_3000MA);
2444 for (i = 0; i < 10 ; i++) {
2446 avr_voltage2 += rk_battery_voltage(di);
2449 avr_current2 = _get_average_current(di);
2450 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2451 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2453 /*calc resister and ocv_votage ocv*/
2454 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2455 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2456 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2458 /*calc sample voltage ocv*/
2459 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2460 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2461 _voltage_to_capacity(di, charge_ocv_voltage1);
2462 charge_ocv_soc1 = di->temp_soc;
2463 _voltage_to_capacity(di, charge_ocv_voltage2);
2464 charge_ocv_soc2 = di->temp_soc;
2466 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2467 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2468 DBG("------------------------------------------------------------------------------------------\n");
2469 last_time = get_seconds();
2479 static int update_battery_resister(struct battery_info *di)
2483 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2485 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2486 tmp_res = estimate_battery_resister(di);
2488 di->bat_res_update_cnt++;
2489 di->bat_res += tmp_res;
2490 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2491 if (di->bat_res_update_cnt == 10) {
2492 di->bat_res_updated = true;
2495 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2504 static void charge_soc_check_routine(struct battery_info *di)
2512 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2513 min = get_charging_time(di);
2514 update_battery_resister(di);
2516 if ((min >= 30) && (di->bat_res_updated)) {
2518 old_temp_soc = di->temp_soc;
2519 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2520 _voltage_to_capacity(di, ocv_voltage);
2521 ocv_temp_soc = di->temp_soc;
2523 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2524 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2525 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2527 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2528 di->temp_soc = ocv_temp_soc;
2530 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2532 remain_capcity = di->temp_soc * di->fcc / 100;
2533 _capacity_init(di, remain_capcity);
2534 di->remain_capacity = _get_realtime_capacity(di);
2535 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2543 static void update_resume_status_relax_voltage(struct battery_info *di)
2545 unsigned long sleep_soc;
2546 unsigned long sum_sleep_soc;
2547 unsigned long sleep_sec;
2552 int sum_sleep_avr_current;
2556 update_battery_info(di);
2558 di->sys_wakeup = true;
2560 DBG("<%s>, resume----------checkstart\n", __func__);
2561 sleep_sec = get_seconds() - di->suspend_time_start;
2562 sleep_min = sleep_sec / 60;
2564 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2565 __func__, sleep_sec, sleep_min);
2567 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2568 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2570 delta_capacity = di->suspend_capacity - di->remain_capacity;
2571 delta_soc = di->suspend_temp_soc - _get_soc(di);
2572 di->dischrg_sum_sleep_capacity += delta_capacity;
2573 di->dischrg_sum_sleep_sec += sleep_sec;
2575 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2576 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2578 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2579 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2580 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2581 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2582 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2583 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2584 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2586 /*large suspend current*/
2587 if (sum_sleep_avr_current > 20) {
2588 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2589 di->real_soc -= sum_sleep_soc;
2590 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2591 __func__, sum_sleep_soc, di->real_soc);
2593 /* small suspend current*/
2594 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2596 relax_voltage = get_relax_voltage(di);
2597 di->voltage = rk_battery_voltage(di);
2599 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2600 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2601 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2604 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2605 if (sum_sleep_soc > 0)
2606 di->real_soc -= sum_sleep_soc;
2610 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2611 di->dischrg_sum_sleep_capacity = 0;
2612 di->dischrg_sum_sleep_sec = 0;
2614 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2615 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2618 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2619 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2620 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2621 di->temp_soc = _get_soc(di);
2622 charge_status = get_charge_status(di);
2624 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2625 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2626 DBG("resume, sleep : ac online charge current < 1000\n");
2627 if (sleep_sec > 0) {
2628 di->count_sleep_time += sleep_sec;
2629 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2630 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2632 di->count_sleep_time = 0;
2633 di->real_soc += sleep_soc;
2634 if (di->real_soc > 100)
2639 DBG("<%s>, usb charging\n", __func__);
2640 if (di->suspend_temp_soc + 15 < di->temp_soc)
2641 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2643 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2646 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);
2653 #ifdef SUPPORT_USB_CHARGE
2654 static int get_charging_status_type(struct battery_info *di)
2656 int otg_status = dwc_otg_check_dpdm();
2658 if (0 == otg_status) {
2661 di->check_count = 0;
2663 } else if (1 == otg_status) {
2664 if (0 == get_gadget_connect_flag()) {
2665 if (++di->check_count >= 5) {
2677 } else if (2 == otg_status) {
2680 di->check_count = 0;
2683 if (di->ac_online == 1)
2684 set_charge_current(di, di->chg_i_lmt);
2686 set_charge_current(di, ILIM_450MA);
2692 static void battery_poweron_status_init(struct battery_info *di)
2696 #ifndef SUPPORT_USB_CHARGE
2700 #ifdef SUPPORT_USB_CHARGE
2702 otg_status = dwc_otg_check_dpdm();
2703 if (otg_status == 1) {
2706 set_charge_current(di, ILIM_450MA);
2707 di->status = POWER_SUPPLY_STATUS_CHARGING;
2708 DBG("++++++++ILIM_450MA++++++\n");
2710 } else if (otg_status == 2) {
2713 di->status = POWER_SUPPLY_STATUS_CHARGING;
2714 set_charge_current(di, di->chg_i_lmt);
2715 DBG("++++++++ILIM_1000MA++++++\n");
2717 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2721 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2722 if (buf&PLUG_IN_STS) {
2725 di->status = POWER_SUPPLY_STATUS_CHARGING;
2726 if (di->real_soc == 100)
2727 di->status = POWER_SUPPLY_STATUS_FULL;
2729 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2733 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2736 static void check_battery_status(struct battery_info *di)
2741 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2742 #ifdef SUPPORT_USB_CHARGE
2744 if (strstr(saved_command_line, "charger")) {
2745 if ((buf&PLUG_IN_STS) == 0) {
2746 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2752 if (buf&PLUG_IN_STS) {
2753 get_charging_status_type(di);
2755 di->status = POWER_SUPPLY_STATUS_CHARGING;
2756 if (di->real_soc == 100)
2757 di->status = POWER_SUPPLY_STATUS_FULL;
2759 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2766 if (buf & PLUG_IN_STS) {
2769 di->status = POWER_SUPPLY_STATUS_CHARGING;
2770 if (di->real_soc == 100)
2771 di->status = POWER_SUPPLY_STATUS_FULL;
2773 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2780 static void last_check_report(struct battery_info *di)
2782 /* high load: current < 0 with charger in.
2783 * System will not shutdown when dsoc=0% with charging state(ac_online),
2784 * which will cause over discharge, so oppose status.
2788 if ((di->real_soc == 0) && (di->status == POWER_SUPPLY_STATUS_CHARGING)
2789 && di->current_avg < 0){
2790 if (get_seconds() - time > 60){
2791 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2795 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
2796 DBG("status=%d, ac_online=%d, usb_online=%d\n",
2797 di->status, di->ac_online, di->usb_online);
2800 time = get_seconds();
2803 static void report_power_supply_changed(struct battery_info *di)
2806 static u32 old_ac_status;
2807 static u32 old_usb_status;
2808 static u32 old_charge_status;
2811 state_changed = false;
2812 if (di->real_soc == 0)
2813 state_changed = true;
2814 else if (di->real_soc == 100)
2815 state_changed = true;
2816 else if (di->real_soc != old_soc)
2817 state_changed = true;
2818 else if (di->ac_online != old_ac_status)
2819 state_changed = true;
2820 else if (di->usb_online != old_usb_status)
2821 state_changed = true;
2822 else if (old_charge_status != di->status)
2823 state_changed = true;
2825 if (state_changed) {
2826 power_supply_changed(&di->bat);
2827 power_supply_changed(&di->usb);
2828 power_supply_changed(&di->ac);
2829 old_soc = di->real_soc;
2830 old_ac_status = di->ac_online;
2831 old_usb_status = di->usb_online;
2832 old_charge_status = di->status;
2836 static void upd_time_table(struct battery_info *di)
2839 static int old_index = 0;
2840 static int old_min = 0;
2842 int mod = di->real_soc % 10;
2843 int index = di->real_soc / 10;
2845 if (di->ac_online || di->usb_online)
2846 time = di->charge_min;
2848 time = di->discharge_min;
2850 if ((mod == 0) && (index > 0) && (old_index != index)) {
2851 di->chrg_min[index-1] = time - old_min;
2856 for (i=1; i<11; i++)
2857 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
2862 static void update_battery_info(struct battery_info *di)
2864 di->remain_capacity = _get_realtime_capacity(di);
2865 if (di->remain_capacity > di->fcc)
2866 _capacity_init(di, di->fcc);
2868 if (di->real_soc > 100)
2870 else if (di->real_soc < 0)
2873 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2874 di->charging_time++;
2875 di->discharging_time = 0;
2877 di->charging_time = 0;
2878 if (di->voltage < 3800)
2879 di->discharging_time += 2;
2881 di->discharging_time++;
2883 if (di->charge_status == CHARGE_FINISH)
2886 di->finish_time = 0;
2888 di->charge_min = get_charging_time(di);
2889 di->discharge_min = get_discharging_time(di);
2890 di->finish_min = get_finish_time(di);
2893 di->est_ocv_vol = estimate_bat_ocv_vol(di);
2894 di->est_ocv_soc = estimate_bat_ocv_soc(di);
2895 di->voltage = rk_battery_voltage(di);
2896 di->current_avg = _get_average_current(di);
2897 di->remain_capacity = _get_realtime_capacity(di);
2898 di->voltage_ocv = _get_OCV_voltage(di);
2899 di->charge_status = get_charge_status(di);
2900 di->otg_status = dwc_otg_check_dpdm();
2901 di->relax_voltage = get_relax_voltage(di);
2902 di->temp_soc = _get_soc(di);
2903 check_battery_status(di);/* ac_online, usb_online, status*/
2904 update_cal_offset(di);
2908 static void rk_battery_work(struct work_struct *work)
2910 struct battery_info *di = container_of(work,
2911 struct battery_info, battery_monitor_work.work);
2913 update_resume_status_relax_voltage(di);
2914 wait_charge_finish_signal(di);
2915 charge_finish_routine(di);
2917 rk_battery_display_smooth(di);
2918 update_battery_info(di);
2919 rsoc_realtime_calib(di);
2920 last_check_report(di);
2921 report_power_supply_changed(di);
2922 _copy_soc(di, di->real_soc);
2923 _save_remain_capacity(di, di->remain_capacity);
2925 dump_debug_info(di);
2926 di->queue_work_cnt++;
2927 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2930 static void rk_battery_charge_check_work(struct work_struct *work)
2932 struct battery_info *di = container_of(work,
2933 struct battery_info, charge_check_work.work);
2935 DBG("rk_battery_charge_check_work\n");
2936 charge_disable_open_otg(di->charge_otg);
2939 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2941 int register_battery_notifier(struct notifier_block *nb)
2943 return blocking_notifier_chain_register(&battery_chain_head, nb);
2945 EXPORT_SYMBOL_GPL(register_battery_notifier);
2947 int unregister_battery_notifier(struct notifier_block *nb)
2949 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2951 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2953 int battery_notifier_call_chain(unsigned long val)
2955 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2956 == NOTIFY_BAD) ? -EINVAL : 0;
2958 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2960 static void poweron_lowerpoer_handle(struct battery_info *di)
2962 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2963 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2965 /* kernel_power_off(); */
2970 static int battery_notifier_call(struct notifier_block *nb,
2971 unsigned long event, void *data)
2973 struct battery_info *di =
2974 container_of(nb, struct battery_info, battery_nb);
2978 DBG(" CHARGE enable\n");
2980 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2985 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2986 DBG("charge disable OTG enable\n");
2990 poweron_lowerpoer_handle(di);
2999 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
3001 pr_info("<%s>lower power warning!\n", __func__);
3003 _copy_soc(g_battery, 0);
3004 _capacity_init(g_battery, 0);
3005 rk_send_wakeup_key();
3010 static void disable_vbat_low_irq(struct battery_info *di)
3013 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
3014 /*clr vbat low interrupt */
3015 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
3017 static void enable_vbat_low_irq(struct battery_info *di)
3019 /* clr vbat low interrupt */
3020 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
3022 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
3025 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
3027 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3028 rk_send_wakeup_key();
3031 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
3033 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3034 charge_disable_open_otg(0);
3035 rk_send_wakeup_key();
3039 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
3041 pr_info("---------- %s:irq = %d\n", __func__, irq);
3042 rk_send_wakeup_key();
3048 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
3052 struct kobject *rk818_fg_kobj;
3054 ret = create_sysfs_interfaces(dev);
3057 dev_err(dev, "device RK818 battery sysfs register failed\n");
3061 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3064 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3065 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3067 dev_err(dev, "create rk818_battery node error\n");
3075 power_supply_unregister(&di->ac);
3076 power_supply_unregister(&di->usb);
3077 power_supply_unregister(&di->bat);
3082 static void rk818_battery_irq_init(struct battery_info *di)
3084 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3086 struct rk818 *chip = di->rk818;
3088 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3089 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3090 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3091 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3093 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3094 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3096 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3098 di->irq = vb_lo_irq;
3099 enable_irq_wake(di->irq);
3100 disable_vbat_low_irq(di);
3102 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3103 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
3105 dev_err(chip->dev, "plug_in_irq request failed!\n");
3108 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3109 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
3111 dev_err(chip->dev, "plug_out_irq request failed!\n");
3114 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3115 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
3117 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3120 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
3126 di->platform_data = chip->battery_data;
3127 di->cell.config = di->platform_data->cell_cfg;
3128 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3129 di->qmax = di->platform_data->cell_cfg->design_qmax;
3130 di->fcc = di->design_capacity;
3131 di->vol_smooth_time = 0;
3132 di->charge_smooth_time = 0;
3133 di->charge_smooth_status = false;
3134 di->sleep_status = 0;
3136 di->sys_wakeup = true;
3137 di->pcb_ioffset = 0;
3138 di->pcb_ioffset_updated = false;
3139 di->queue_work_cnt = 0;
3141 di->voltage_old = 0;
3142 di->display_soc = 0;
3144 di->bat_res_updated = false;
3146 di->sys_wakeup = true;
3147 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3150 di->discharge_min = 0;
3151 di->charging_time = 0;
3152 di->discharging_time = 0;
3153 di->finish_time = 0;
3157 di->odd_capacity = 0;
3158 di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3159 di->term_chg_cnt = 0;
3160 di->emu_chg_cnt = 0;
3162 for (i=0; i<10; i++)
3163 di->chrg_min[i] = -1;
3165 di->debug_finish_real_soc = 0;
3166 di->debug_finish_temp_soc = 0;
3168 fcc_capacity = _get_FCC_capacity(di);
3169 if (fcc_capacity > 1000)
3170 di->fcc = fcc_capacity;
3172 di->fcc = di->design_capacity;
3175 static struct of_device_id rk818_battery_of_match[] = {
3176 { .compatible = "rk818_battery" },
3180 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3183 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3185 struct device_node *regs, *rk818_pmic_np;
3186 struct battery_platform_data *data;
3187 struct cell_config *cell_cfg;
3188 struct ocv_config *ocv_cfg;
3189 struct property *prop;
3193 rk818_pmic_np = of_node_get(rk818->dev->of_node);
3194 if (!rk818_pmic_np) {
3195 dev_err(dev, "could not find pmic sub-node\n");
3199 regs = of_find_node_by_name(rk818_pmic_np, "battery");
3201 dev_err(dev, "battery node not found!\n");
3205 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3207 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3211 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3213 dev_err(dev, "kzalloc for cell_config failed!\n");
3216 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3218 dev_err(dev, "kzalloc for ocv_config failed!\n");
3222 prop = of_find_property(regs, "ocv_table", &length);
3224 dev_err(dev, "ocv_table not found!\n");
3227 data->ocv_size = length / sizeof(u32);
3229 if (data->ocv_size > 0) {
3230 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3232 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3233 if (!data->battery_ocv) {
3234 dev_err(dev, "kzalloc for ocv_table failed!\n");
3237 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
3242 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3244 dev_err(dev, "max_charge_currentmA not found!\n");
3245 out_value = DEFAULT_ICUR;
3247 data->max_charger_currentmA = out_value;
3249 ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3251 dev_err(dev, "max_charger_ilimitmA not found!\n");
3252 out_value = DEFAULT_ILMT;
3254 data->max_charger_ilimitmA = out_value;
3256 ret = of_property_read_u32(regs, "bat_res", &out_value);
3258 dev_err(dev, "bat_res not found!\n");
3259 out_value = DEFAULT_BAT_RES;
3261 data->sense_resistor_mohm = out_value;
3263 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3265 dev_err(dev, "max_charge_voltagemV not found!\n");
3266 out_value = DEFAULT_VLMT;
3268 data->max_charger_voltagemV = out_value;
3270 ret = of_property_read_u32(regs, "design_capacity", &out_value);
3272 dev_err(dev, "design_capacity not found!\n");
3275 cell_cfg->design_capacity = out_value;
3277 ret = of_property_read_u32(regs, "design_qmax", &out_value);
3279 dev_err(dev, "design_qmax not found!\n");
3282 cell_cfg->design_qmax = out_value;
3284 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3286 dev_err(dev, "sleep_enter_current not found!\n");
3289 ocv_cfg->sleep_enter_current = out_value;
3291 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3293 dev_err(dev, "sleep_exit_current not found!\n");
3296 ocv_cfg->sleep_exit_current = out_value;
3298 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3300 cell_cfg->ocv = ocv_cfg;
3301 data->cell_cfg = cell_cfg;
3302 rk818->battery_data = data;
3304 DBG("\n--------- the battery OCV TABLE dump:\n");
3305 DBG("bat_res :%d\n", data->sense_resistor_mohm);
3306 DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3307 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3308 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3309 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3310 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3311 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3312 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3313 DBG("uboot chrg = %d\n", support_uboot_chrg);
3314 DBG("\n--------- rk818_battery dt_parse ok.\n");
3319 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3326 static int battery_probe(struct platform_device *pdev)
3328 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3329 struct battery_info *di;
3332 DBG("battery driver version %s\n", DRIVER_VERSION);
3333 di = kzalloc(sizeof(*di), GFP_KERNEL);
3335 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
3338 ret = rk_battery_parse_dt(chip, &pdev->dev);
3340 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
3344 platform_set_drvdata(pdev, di);
3345 battery_info_init(di, chip);
3346 if (!is_bat_exist(di)) {
3347 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
3352 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
3354 flatzone_voltage_init(di);
3355 battery_poweron_status_init(di);
3356 battery_power_supply_init(di);
3357 ret = battery_power_supply_register(di, &pdev->dev);
3359 dev_err(&pdev->dev, "rk power supply register failed!\n");
3362 di->wq = create_singlethread_workqueue("battery-work");
3363 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
3364 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
3365 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
3367 di->battery_nb.notifier_call = battery_notifier_call;
3368 register_battery_notifier(&di->battery_nb);
3370 rk818_battery_irq_init(di);
3371 rk818_battery_sysfs_init(di, &pdev->dev);
3372 DBG("------ RK81x battery_probe ok!-------\n");
3379 static int battery_suspend(struct platform_device *dev, pm_message_t state)
3381 struct battery_info *di = platform_get_drvdata(dev);
3383 enable_vbat_low_irq(di);
3384 di->sleep_status = di->status;
3385 di->suspend_charge_current = _get_average_current(di);
3387 /* avoid abrupt wakeup which will clean the variable*/
3388 if (di->sys_wakeup) {
3389 di->suspend_capacity = di->remain_capacity;
3390 di->suspend_temp_soc = _get_soc(di);
3391 di->suspend_time_start = get_seconds();
3392 di->sys_wakeup = false;
3395 cancel_delayed_work(&di->battery_monitor_work);
3396 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
3397 __func__, di->suspend_temp_soc, di->suspend_charge_current,
3398 di->suspend_capacity, di->sleep_status);
3400 set_low_power_interrupt(di);
3404 static int battery_resume(struct platform_device *dev)
3406 struct battery_info *di = platform_get_drvdata(dev);
3408 set_low_power_interrupt(di);
3410 DBG("<%s>\n", __func__);
3411 disable_vbat_low_irq(di);
3412 queue_delayed_work(di->wq, &di->battery_monitor_work,
3413 msecs_to_jiffies(TIMER_MS_COUNTS/2));
3415 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3417 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3422 static int battery_remove(struct platform_device *dev)
3424 struct battery_info *di = platform_get_drvdata(dev);
3426 cancel_delayed_work_sync(&di->battery_monitor_work);
3429 static void battery_shutdown(struct platform_device *dev)
3431 struct battery_info *di = platform_get_drvdata(dev);
3433 cancel_delayed_work_sync(&di->battery_monitor_work);
3434 DBG("rk818 shutdown!");
3438 static struct platform_driver battery_driver = {
3440 .name = "rk818-battery",
3441 .owner = THIS_MODULE,
3444 .probe = battery_probe,
3445 .remove = battery_remove,
3446 .suspend = battery_suspend,
3447 .resume = battery_resume,
3448 .shutdown = battery_shutdown,
3451 static int __init battery_init(void)
3453 return platform_driver_register(&battery_driver);
3456 fs_initcall_sync(battery_init);
3457 static void __exit battery_exit(void)
3459 platform_driver_unregister(&battery_driver);
3461 module_exit(battery_exit);
3463 MODULE_LICENSE("GPL");
3464 MODULE_ALIAS("platform:rk818-battery");
3465 MODULE_AUTHOR("ROCKCHIP");