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 <linux/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>
29 #include <linux/of_gpio.h>
30 #include <linux/irq.h>
31 #include <linux/irqdomain.h>
33 /* if you want to disable, don't set it as 0,
34 just be: "static int dbg_enable;" is ok*/
35 static int dbg_enable;
36 #define RK818_SYS_DBG 1
38 module_param_named(dbg_level, dbg_enable, int, 0644);
39 #define DBG(args...) \
47 #define DEFAULT_BAT_RES 135
48 #define DEFAULT_VLMT 4200
49 #define DEFAULT_ILMT 2000
50 #define DEFAULT_ICUR 1600
52 #define DEF_TEST_ILMT_MA 2000
53 #define DEF_TEST_CURRENT_MA 1800
55 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
56 #define DSOC_DISCHRG_FAST_EER_RANGE 25
57 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
58 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
59 #define DSOC_CHRG_FAST_EER_RANGE 15
60 #define DSOC_CHRG_EMU_CURR 1200
61 #define DSOC_CHG_TERM_CURR 600
62 #define DSOC_CHG_TERM_VOL 4100
63 #define CHG_FINISH_VOL 4100
65 /*realtime RSOC calib param*/
66 #define RSOC_DISCHG_ERR_LOWER 40
67 #define RSOC_DISCHG_ERR_UPPER 50
68 #define RSOC_ERR_CHCK_CNT 15
69 #define RSOC_COMPS 20 /*compensation*/
70 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
71 #define RSOC_CALIB_DISCHGR_TIME 3 /*min*/
73 #define RSOC_RESUME_ERR 10
74 #define REBOOT_INTER_MIN 1
76 #define INTERPOLATE_MAX 1000
77 #define MAX_INT 0x7FFF
78 #define TIME_10MIN_SEC 600
80 #define CHG_VOL_SHIFT 4
81 #define CHG_ILIM_SHIFT 0
82 #define CHG_ICUR_SHIFT 0
83 #define DEF_CHRG_VOL CHRG_VOL4200
84 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
85 #define DEF_CHRG_CURR_LMT ILIM_2000MA
87 /*TEST_POWER_MODE params*/
88 #define TEST_CURRENT 1000
89 #define TEST_VOLTAGE 3800
91 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
93 #define TEST_AC_ONLINE 1
94 #define TEST_USB_ONLINE 0
97 * the following table value depends on datasheet
99 int CHG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
101 int CHG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
102 2250, 2400, 2600, 2800, 3000};
104 int CHG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
105 2000, 2250, 2500, 2750, 3000};
107 u8 CHG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
109 #define RK818_DC_IN 0
110 #define RK818_DC_OUT 1
111 #define SEC_TO_MIN(x) ((x)/60)
112 #define BASE_TO_MIN(x) ((get_seconds()-(x))/60)
113 #define BASE_TO_SEC(x) (get_seconds()-(x))
115 #define OCV_VALID_SHIFT (0)
116 #define OCV_CALIB_SHIFT (1)
117 #define FIRST_PWRON_SHIFT (2)
120 struct battery_info {
122 struct cell_state cell;
123 struct power_supply bat;
124 struct power_supply ac;
125 struct power_supply usb;
126 struct delayed_work work;
127 /* struct i2c_client *client; */
129 struct pinctrl *pinctrl;
130 struct pinctrl_state *pins_default;
133 struct battery_platform_data *platform_data;
137 int dc_det_pullup_inside;
148 uint16_t voltage_ocv;
149 uint16_t relax_voltage;
153 bool pcb_ioffset_updated;
154 unsigned long queue_work_cnt;
158 uint16_t warnning_voltage;
175 int bat_res_update_cnt;
181 unsigned long dod0_time;
194 int current_k;/* (ICALIB0, ICALIB1) */
197 int voltage_k;/* VCALIB0 VCALIB1 */
215 struct timeval soc_timer;
216 struct timeval change_timer;
219 int charge_smooth_time;
223 struct timespec suspend_time;
224 struct timespec resume_time;
225 unsigned long suspend_time_start;
226 unsigned long count_sleep_time;
230 int suspend_charge_current;
233 bool bat_res_updated;
234 bool charge_smooth_status;
236 unsigned long last_plugin_time;
239 unsigned long charging_time;
240 unsigned long discharging_time;
241 unsigned long finish_time;
246 struct notifier_block battery_nb;
247 struct workqueue_struct *wq;
248 struct delayed_work battery_monitor_work;
249 struct delayed_work charge_check_work;
252 struct wake_lock resume_wake_lock;
253 unsigned long sys_on_base;
254 unsigned long chrg_time_base;
258 bool is_first_poweron;
263 int test_chrg_current;
265 int debug_finish_real_soc;
266 int debug_finish_temp_soc;
274 struct battery_info *g_battery;
275 u32 support_uboot_chrg, support_usb_adp, support_dc_adp;
276 static void rk81x_update_battery_info(struct battery_info *di);
278 static bool rk81x_support_adp_type(enum hw_support_adp_t type)
283 case HW_ADP_TYPE_USB:
291 case HW_ADP_TYPE_DUAL:
292 if (support_usb_adp && support_dc_adp)
302 static u32 interpolate(int value, u32 *table, int size)
307 for (i = 0; i < size; i++) {
308 if (value < table[i])
312 if ((i > 0) && (i < size)) {
313 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
314 d /= table[i] - table[i-1];
315 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
317 d = i * ((INTERPOLATE_MAX+size/2)/size);
325 /* Returns (a * b) / c */
326 static int32_t ab_div_c(u32 a, u32 b, u32 c)
332 sign = ((((a^b)^c) & 0x80000000) != 0);
338 tmp = ((int32_t) a*b + (c>>1)) / c;
350 static int32_t abs_int(int32_t x)
352 return (x > 0) ? x : -x;
355 static int abs32_int(int x)
357 return (x > 0) ? x : -x;
360 static int div(int val)
362 return (val == 0) ? 1 : val;
365 static int battery_read(struct rk818 *rk818, u8 reg,
366 u8 buf[], unsigned len)
370 ret = rk818_i2c_read(rk818, reg, len, buf);
374 static int battery_write(struct rk818 *rk818, u8 reg,
375 u8 const buf[], unsigned len)
379 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
383 static void rk81x_set_bit(struct battery_info *di, u8 reg, u8 shift)
385 rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
388 static void rk81x_clr_bit(struct battery_info *di, u8 reg, u8 shift)
390 rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
393 static u8 rk81x_read_bit(struct battery_info *di, u8 reg, u8 shift)
398 battery_read(di->rk818, reg, &buf, 1);
399 val = (buf & BIT(shift)) >> shift;
403 static void dump_gauge_register(struct battery_info *di)
408 DBG("%s dump charger register start:\n", __func__);
409 for (i = 0xAC; i < 0xDF; i++) {
410 battery_read(di->rk818, i, &buf, 1);
411 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
416 static void dump_charger_register(struct battery_info *di)
421 DBG("%s dump the register start:\n", __func__);
422 for (i = 0x99; i < 0xAB; i++) {
423 battery_read(di->rk818, i, &buf, 1);
424 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
431 static void _capacity_init(struct battery_info *di, u32 capacity);
434 * interface for debug: do rsoc_first_poweron_init() without unloading battery
436 static ssize_t bat_calib_read(struct device *dev,
437 struct device_attribute *attr, char *buf)
439 struct battery_info *di = g_battery;
442 val = rk81x_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
443 return sprintf(buf, "%d\n", val);
446 static ssize_t bat_calib_write(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
452 struct battery_info *di = g_battery;
454 ret = kstrtou8(buf, 0, &val);
459 rk81x_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
461 rk81x_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
466 * interface for debug: force battery to over discharge
468 static ssize_t bat_test_power_read(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct battery_info *di = g_battery;
473 return sprintf(buf, "%d\n", di->fg_drv_mode);
476 static ssize_t bat_test_power_write(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
482 struct battery_info *di = g_battery;
484 ret = kstrtou8(buf, 0, &val);
489 di->fg_drv_mode = TEST_POWER_MODE;
491 di->fg_drv_mode = FG_NORMAL_MODE;
497 static ssize_t bat_state_read(struct device *dev,
498 struct device_attribute *attr, char *buf)
500 struct battery_info *di = g_battery;
502 return sprintf(buf, "dsoc = %d, rsoc = %d\n",
503 di->real_soc, di->temp_soc);
506 static ssize_t bat_fcc_read(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 struct battery_info *di = g_battery;
511 return sprintf(buf, "%d", di->fcc);
514 static ssize_t bat_fcc_write(struct device *dev,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
520 struct battery_info *di = g_battery;
522 ret = kstrtou16(buf, 0, &val);
532 static ssize_t bat_soc_read(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct battery_info *di = g_battery;
537 return sprintf(buf, "%d", di->real_soc);
540 static ssize_t bat_soc_write(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
546 struct battery_info *di = g_battery;
548 ret = kstrtou8(buf, 0, &val);
556 static ssize_t bat_temp_soc_read(struct device *dev,
557 struct device_attribute *attr, char *buf)
559 struct battery_info *di = g_battery;
561 return sprintf(buf, "%d", di->temp_soc);
564 static ssize_t bat_temp_soc_write(struct device *dev,
565 struct device_attribute *attr,
566 const char *buf, size_t count)
571 struct battery_info *di = g_battery;
573 ret = kstrtou8(buf, 0, &val);
577 capacity = di->fcc*val/100;
578 _capacity_init(di, capacity);
583 static ssize_t bat_voltage_read(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct battery_info *di = g_battery;
588 return sprintf(buf, "%d", di->voltage);
591 static ssize_t bat_avr_current_read(struct device *dev,
592 struct device_attribute *attr, char *buf)
594 struct battery_info *di = g_battery;
596 return sprintf(buf, "%d", di->current_avg);
599 static ssize_t bat_remain_cap_read(struct device *dev,
600 struct device_attribute *attr,
603 struct battery_info *di = g_battery;
605 return sprintf(buf, "%d", di->remain_capacity);
609 * interface for debug: debug info switch
611 static ssize_t bat_debug_write(struct device *dev,
612 struct device_attribute *attr,
613 const char *buf, size_t count)
618 ret = kstrtou8(buf, 0, &val);
627 static ssize_t bat_regs_read(struct device *dev,
628 struct device_attribute *attr, char *buf)
631 u32 start_offset = 0x0;
632 u32 end_offset = 0xf2;
633 struct battery_info *di = g_battery;
637 str += sprintf(str, "start from add=0x%x, offset=0x%x\n",
638 start_offset, end_offset);
640 for (i = start_offset; i <= end_offset; ) {
641 battery_read(di->rk818, i, &val, 1);
642 str += sprintf(str, "0x%x=0x%x", i, val);
645 str += sprintf(str, "\n");
648 str += sprintf(str, " ");
650 str += sprintf(str, "\n");
658 static struct device_attribute rk818_bat_attr[] = {
659 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
660 __ATTR(soc, 0664, bat_soc_read, bat_soc_write),
661 __ATTR(temp_soc, 0664, bat_temp_soc_read, bat_temp_soc_write),
662 __ATTR(voltage, 0664, bat_voltage_read, NULL),
663 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
664 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
665 __ATTR(debug, 0664, NULL, bat_debug_write),
666 __ATTR(regs, 0664, bat_regs_read, NULL),
667 __ATTR(state, 0664, bat_state_read, NULL),
668 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
669 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
674 static uint16_t get_relax_voltage(struct battery_info *di);
676 static ssize_t show_state_attrs(struct device *dev,
677 struct device_attribute *attr, char *buf)
679 struct battery_info *data = g_battery;
681 if (0 == get_relax_voltage(data)) {
683 "voltage = %d, remain_capacity = %d, status = %d\n",
684 data->voltage, data->remain_capacity,
689 "voltage = %d, remain_capacity = %d, status = %d\n",
690 get_relax_voltage(data), data->remain_capacity,
694 static ssize_t restore_state_attrs(struct device *dev,
695 struct device_attribute *attr,
696 const char *buf, size_t size)
700 static struct device_attribute rkbatt_attrs[] = {
701 __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
704 static int create_sysfs_interfaces(struct device *dev)
708 for (i = 0; i < ARRAY_SIZE(rkbatt_attrs); i++) {
709 if (device_create_file(dev, rkbatt_attrs + i))
717 device_remove_file(dev, rkbatt_attrs + i);
719 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
723 static int _gauge_enable(struct battery_info *di)
729 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
731 dev_err(di->dev, "error reading TS_CTRL_REG");
734 if (!(buf & GG_EN)) {
736 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1);/*enable*/
737 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
741 DBG("%s, %d\n", __func__, buf);
745 static void save_level(struct battery_info *di, u8 save_soc)
750 battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
752 static u8 get_level(struct battery_info *di)
756 battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
760 static int _get_vcalib0(struct battery_info *di)
766 ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
768 ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
771 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
775 static int _get_vcalib1(struct battery_info *di)
781 ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
783 ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
786 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
790 static int _get_ioffset(struct battery_info *di)
796 ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
798 ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
804 static uint16_t _get_cal_offset(struct battery_info *di)
810 ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
812 ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
817 static int _set_cal_offset(struct battery_info *di, u32 value)
823 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
824 buf = (value >> 8)&0xff;
825 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
829 static void _get_voltage_offset_value(struct battery_info *di)
831 int vcalib0, vcalib1;
833 vcalib0 = _get_vcalib0(di);
834 vcalib1 = _get_vcalib1(di);
836 di->voltage_k = (4200 - 3000)*1000/div((vcalib1 - vcalib0));
837 di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
838 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
840 static uint16_t _get_OCV_voltage(struct battery_info *di)
845 uint16_t voltage_now = 0;
849 for (i = 0; i < 3; i++) {
850 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
852 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
856 dev_err(di->dev, "error read BAT_OCV_REGH");
861 if (val[0] == val[1])
866 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
871 static int _get_battery_voltage(struct battery_info *di)
880 for (i = 0; i < 3; i++) {
881 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
883 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
887 dev_err(di->dev, "error read BAT_VOL_REGH");
892 if (val[0] == val[1])
897 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
903 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
904 * based on the voltage.
906 static int _voltage_to_capacity(struct battery_info *di, int voltage)
913 ocv_table = di->platform_data->battery_ocv;
914 ocv_size = di->platform_data->ocv_size;
915 di->warnning_voltage = ocv_table[3];
916 tmp = interpolate(voltage, ocv_table, ocv_size);
917 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
918 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
923 static uint16_t _get_relax_vol1(struct battery_info *di)
927 uint16_t temp = 0, voltage_now;
929 ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
931 ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
934 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
939 static uint16_t _get_relax_vol2(struct battery_info *di)
942 uint16_t temp = 0, voltage_now;
945 ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
947 ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
950 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
955 static int _get_raw_adc_current(struct battery_info *di)
961 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
963 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
967 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
969 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
972 current_now |= (buf<<8);
975 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
982 static void reset_zero_var(struct battery_info *di)
990 static void ioffset_sample_time(struct battery_info *di, int time)
994 battery_read(di->rk818, GGCON, &ggcon, 1);
995 ggcon &= ~(0x30); /*clear <5:4>*/
997 battery_write(di->rk818, GGCON, &ggcon, 1);
1000 static void update_cal_offset(struct battery_info *di)
1002 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
1005 battery_read(di->rk818, PCB_IOFFSET_REG, &pcb_offset, 1);
1006 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n",
1007 __func__, di->queue_work_cnt, mod);
1008 if ((!mod) && (di->pcb_ioffset_updated)) {
1009 _set_cal_offset(di, _get_ioffset(di)+pcb_offset);
1010 DBG("<%s>. 10min update cal_offset = %d",
1011 __func__, di->pcb_ioffset+_get_ioffset(di));
1016 * when charger finish signal comes, we need calibrate the current, make it
1019 static void zero_current_calib(struct battery_info *di)
1028 if ((di->charge_status == CHARGE_FINISH) &&
1029 (abs32_int(di->current_avg) > 4)) {
1030 for (retry = 0; retry < 5; retry++) {
1031 adc_value = _get_raw_adc_current(di);
1032 if (adc_value > 2047)
1035 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1036 C0 = _get_cal_offset(di);
1037 C1 = adc_value + C0;
1038 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1040 _set_cal_offset(di, C1);
1041 DBG("<%s>. new cal_offset = %d\n",
1042 __func__, _get_cal_offset(di));
1045 adc_value = _get_raw_adc_current(di);
1046 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1047 if (adc_value < 4) {
1048 if (_get_cal_offset(di) < 0x7ff)
1049 _set_cal_offset(di, di->current_offset+
1052 ioffset = _get_ioffset(di);
1053 pcb_offset = C1 - ioffset;
1054 di->pcb_ioffset = pcb_offset;
1055 di->pcb_ioffset_updated = true;
1056 battery_write(di->rk818,
1060 DBG("<%s>. update the cal_offset, C1 = %d\n"
1061 "i_offset = %d, pcb_offset = %d\n",
1062 __func__, C1, ioffset, pcb_offset);
1065 di->pcb_ioffset_updated = false;
1072 static bool _is_relax_mode(struct battery_info *di)
1077 ret = battery_read(di->rk818, GGSTS, &status, 1);
1079 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
1085 static uint16_t get_relax_voltage(struct battery_info *di)
1089 uint16_t relax_vol1, relax_vol2;
1092 ret = battery_read(di->rk818, GGSTS, &status, 1);
1093 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
1095 relax_vol1 = _get_relax_vol1(di);
1096 relax_vol2 = _get_relax_vol2(di);
1097 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1098 __func__, status, ggcon, relax_vol1, relax_vol2);
1100 if (_is_relax_mode(di))
1101 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1106 static void _set_relax_thres(struct battery_info *di)
1109 int enter_thres, exit_thres;
1110 struct cell_state *cell = &di->cell;
1112 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
1113 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
1114 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1115 __func__, cell->config->ocv->sleep_enter_current,
1116 cell->config->ocv->sleep_exit_current);
1118 buf = enter_thres&0xff;
1119 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
1120 buf = (enter_thres>>8)&0xff;
1121 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
1123 buf = exit_thres&0xff;
1124 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
1125 buf = (exit_thres>>8)&0xff;
1126 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
1128 /* set sample time */
1129 battery_read(di->rk818, GGCON, &buf, 1);
1130 buf &= ~(3<<2);/*8min*/
1131 buf &= ~0x01; /* clear bat_res calc*/
1132 battery_write(di->rk818, GGCON, &buf, 1);
1135 static void restart_relax(struct battery_info *di)
1137 u8 ggcon;/* chrg_ctrl_reg2;*/
1140 battery_read(di->rk818, GGCON, &ggcon, 1);
1142 battery_write(di->rk818, GGCON, &ggcon, 1);
1144 battery_read(di->rk818, GGSTS, &ggsts, 1);
1146 battery_write(di->rk818, GGSTS, &ggsts, 1);
1149 static int _get_average_current(struct battery_info *di)
1158 for (i = 0; i < 3; i++) {
1159 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
1161 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1166 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
1168 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1174 if (val[0] == val[1])
1175 current_now = val[0];
1177 current_now = val[2];
1179 if (current_now & 0x800)
1180 current_now -= 4096;
1182 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
1187 static int is_rk81x_bat_exist(struct battery_info *di)
1191 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1192 return (buf & 0x80) ? 1 : 0;
1195 static bool _is_first_poweron(struct battery_info *di)
1200 battery_read(di->rk818, GGSTS, &buf, 1);
1201 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1202 /*di->pwron_bat_con = buf;*/
1206 battery_write(di->rk818, GGSTS, &buf, 1);
1207 battery_read(di->rk818, GGSTS, &temp, 1);
1208 } while (temp&BAT_CON);
1214 static void flatzone_voltage_init(struct battery_info *di)
1221 ocv_table = di->platform_data->battery_ocv;
1222 ocv_size = di->platform_data->ocv_size;
1224 for (j = 0; j < 21; j++)
1228 for (i = 1; i < ocv_size-1; i++) {
1229 if (ocv_table[i+1] < ocv_table[i] + 20)
1230 temp_table[j++] = i;
1233 temp_table[j] = temp_table[j-1]+1;
1235 di->enter_flatzone = ocv_table[i];
1239 for (i = 0; i < 20; i++) {
1240 if (temp_table[i] < temp_table[i+1])
1245 di->exit_flatzone = ocv_table[i];
1247 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1248 di->enter_flatzone, di->exit_flatzone);
1251 static void power_on_save(struct battery_info *di, int ocv_voltage)
1253 u8 ocv_valid, first_pwron;
1257 /*buf==1: OCV_VOL is valid*/
1258 ocv_valid = rk81x_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1259 first_pwron = rk81x_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1260 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1262 if (first_pwron == 1 || ocv_valid == 1) {
1263 DBG("<%s> enter.\n", __func__);
1264 ocv_soc = _voltage_to_capacity(di, ocv_voltage);
1266 di->dod0_voltage = ocv_voltage;
1267 di->dod0_capacity = di->nac;
1268 di->dod0_status = 1;
1270 di->dod0_level = 80;
1273 di->dod0_level = 100;
1274 else if (ocv_soc < 5)
1275 di->dod0_level = 95;
1276 else if (ocv_soc < 10)
1277 di->dod0_level = 90;
1278 /* save_soc = di->dod0_level; */
1279 save_soc = get_level(di);
1280 if (save_soc < di->dod0_level)
1281 save_soc = di->dod0_level;
1282 save_level(di, save_soc);
1283 DBG("<%s>: dod0_vol:%d, dod0_cap:%d, dod0:%d, level:%d",
1284 __func__, di->dod0_voltage, di->dod0_capacity,
1291 static int _get_soc(struct battery_info *di)
1293 return di->remain_capacity * 100 / div(di->fcc);
1296 static enum power_supply_property rk_battery_props[] = {
1297 POWER_SUPPLY_PROP_STATUS,
1298 POWER_SUPPLY_PROP_CURRENT_NOW,
1299 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1300 POWER_SUPPLY_PROP_PRESENT,
1301 POWER_SUPPLY_PROP_HEALTH,
1302 POWER_SUPPLY_PROP_CAPACITY,
1305 #define to_device_info(x) container_of((x), \
1306 struct battery_info, bat)
1308 static int rk81x_battery_get_property(struct power_supply *psy,
1309 enum power_supply_property psp,
1310 union power_supply_propval *val)
1312 struct battery_info *di = to_device_info(psy);
1315 case POWER_SUPPLY_PROP_CURRENT_NOW:
1316 val->intval = di->current_avg*1000;/*uA*/
1317 if (di->fg_drv_mode == TEST_POWER_MODE)
1318 val->intval = TEST_CURRENT*1000;
1321 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1322 val->intval = di->voltage*1000;/*uV*/
1323 if (di->fg_drv_mode == TEST_POWER_MODE)
1324 val->intval = TEST_VOLTAGE*1000;
1328 case POWER_SUPPLY_PROP_PRESENT:
1329 val->intval = is_rk81x_bat_exist(di);
1330 if (di->fg_drv_mode == TEST_POWER_MODE)
1331 val->intval = TEST_PRESET;
1335 case POWER_SUPPLY_PROP_CAPACITY:
1336 val->intval = di->real_soc;
1337 if (di->fg_drv_mode == TEST_POWER_MODE)
1338 val->intval = TEST_SOC;
1340 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1343 case POWER_SUPPLY_PROP_HEALTH:
1344 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1347 case POWER_SUPPLY_PROP_STATUS:
1348 val->intval = di->status;
1349 if (di->fg_drv_mode == TEST_POWER_MODE)
1350 val->intval = TEST_STATUS;
1362 static enum power_supply_property rk_battery_ac_props[] = {
1363 POWER_SUPPLY_PROP_ONLINE,
1365 static enum power_supply_property rk_battery_usb_props[] = {
1366 POWER_SUPPLY_PROP_ONLINE,
1370 #define to_ac_device_info(x) container_of((x), \
1371 struct battery_info, ac)
1373 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1374 enum power_supply_property psp,
1375 union power_supply_propval *val)
1378 struct battery_info *di = to_ac_device_info(psy);
1381 case POWER_SUPPLY_PROP_ONLINE:
1382 val->intval = di->ac_online; /*discharging*/
1383 if (di->fg_drv_mode == TEST_POWER_MODE)
1384 val->intval = TEST_AC_ONLINE;
1395 #define to_usb_device_info(x) container_of((x), \
1396 struct battery_info, usb)
1398 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1399 enum power_supply_property psp,
1400 union power_supply_propval *val)
1403 struct battery_info *di = to_usb_device_info(psy);
1406 case POWER_SUPPLY_PROP_ONLINE:
1407 if ((strstr(saved_command_line, "charger") == NULL) &&
1408 (di->real_soc == 0) && (di->work_on == 1))
1411 val->intval = di->usb_online;
1413 if (di->fg_drv_mode == TEST_POWER_MODE)
1414 val->intval = TEST_USB_ONLINE;
1426 static void battery_power_supply_init(struct battery_info *di)
1428 di->bat.name = "BATTERY";
1429 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1430 di->bat.properties = rk_battery_props;
1431 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1432 di->bat.get_property = rk81x_battery_get_property;
1435 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1436 di->ac.properties = rk_battery_ac_props;
1437 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1438 di->ac.get_property = rk81x_battery_ac_get_property;
1440 di->usb.name = "USB";
1441 di->usb.type = POWER_SUPPLY_TYPE_USB;
1442 di->usb.properties = rk_battery_usb_props;
1443 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1444 di->usb.get_property = rk81x_battery_usb_get_property;
1447 static int battery_power_supply_register(struct battery_info *di)
1450 struct device *dev = di->dev;
1452 ret = power_supply_register(dev, &di->bat);
1454 dev_err(dev, "failed to register main battery\n");
1457 ret = power_supply_register(dev, &di->usb);
1459 dev_err(dev, "failed to register usb power supply\n");
1462 ret = power_supply_register(dev, &di->ac);
1464 dev_err(dev, "failed to register ac power supply\n");
1471 power_supply_unregister(&di->ac);
1473 power_supply_unregister(&di->usb);
1475 power_supply_unregister(&di->bat);
1480 static void _capacity_init(struct battery_info *di, u32 capacity)
1486 delta_cap = capacity - di->remain_capacity;
1487 di->adjust_cap += delta_cap;
1491 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1493 buf = (capacity_ma>>24)&0xff;
1494 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1495 buf = (capacity_ma>>16)&0xff;
1496 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1497 buf = (capacity_ma>>8)&0xff;
1498 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1499 buf = (capacity_ma&0xff) | 0x01;
1500 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1501 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1507 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1512 if (capacity >= di->qmax)
1513 capacity = di->qmax;
1518 capacity_ma = capacity;
1520 buf = (capacity_ma>>24)&0xff;
1521 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1522 buf = (capacity_ma>>16)&0xff;
1523 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1524 buf = (capacity_ma>>8)&0xff;
1525 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1526 buf = (capacity_ma&0xff) | 0x01;
1527 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1530 static int _get_remain_capacity(struct battery_info *di)
1538 for (i = 0; i < 3; i++) {
1539 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1541 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1542 val[i] |= buf << 16;
1543 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1545 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1549 if (val[0] == val[1])
1558 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1563 capacity_ma = capacity;
1564 buf = (capacity_ma>>24)&0xff;
1565 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1566 buf = (capacity_ma>>16)&0xff;
1567 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1568 buf = (capacity_ma>>8)&0xff;
1569 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1570 buf = (capacity_ma&0xff) | 0x01;
1571 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1574 static int _get_FCC_capacity(struct battery_info *di)
1581 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1583 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1585 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1587 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1591 capacity = temp-1;/* 4096*900/14/36*500/521 */
1594 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1599 static int _get_realtime_capacity(struct battery_info *di)
1608 for (i = 0; i < 3; i++) {
1609 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1611 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1612 val[i] |= buf << 16;
1613 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1615 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1618 if (val[0] == val[1])
1623 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1628 static int _copy_soc(struct battery_info *di, u8 save_soc)
1633 battery_write(di->rk818, SOC_REG, &soc, 1);
1637 static int copy_reboot_cnt(struct battery_info *di, u8 save_cnt)
1642 battery_write(di->rk818, REBOOT_CNT_REG, &cnt, 1);
1646 static bool support_uboot_charge(void)
1648 return support_uboot_chrg ? true : false;
1653 * There are three ways to detect dc_adp:
1654 * 1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1655 * do not care about whether define dc_det_pin or not;
1656 * 2. define de_det_pin: check gpio level;
1657 * 3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1658 * case that: gpio invalid or not define.
1660 static enum charger_type_t rk81x_get_dc_state(struct battery_info *di)
1662 enum charger_type_t charger_type;
1666 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1668 /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1669 if (!rk81x_support_adp_type(HW_ADP_TYPE_USB)) {
1670 if ((buf & PLUG_IN_STS) != 0)
1671 charger_type = DC_CHARGER;
1673 charger_type = NO_CHARGER;
1675 return charger_type;
1679 /*det by gpio level*/
1680 if (gpio_is_valid(di->dc_det_pin)) {
1681 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1683 pr_err("Failed to request gpio %d with ret:""%d\n",
1684 di->dc_det_pin, ret);
1688 gpio_direction_input(di->dc_det_pin);
1689 ret = gpio_get_value(di->dc_det_pin);
1690 if (ret == di->dc_det_level)
1691 charger_type = DC_CHARGER;
1693 charger_type = NO_CHARGER;
1695 gpio_free(di->dc_det_pin);
1696 DBG("**********rk818 dc_det_pin=%d\n", ret);
1698 return charger_type;
1701 /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1702 else if (rk81x_support_adp_type(HW_ADP_TYPE_DUAL)) {
1703 if ((buf & PLUG_IN_STS) != 0) {
1704 charger_type = dwc_otg_check_dpdm();
1705 if (charger_type == 0)
1706 charger_type = DC_CHARGER;
1708 charger_type = NO_CHARGER;
1712 return charger_type;
1715 static enum charger_type_t rk81x_get_usbac_state(struct battery_info *di)
1717 enum charger_type_t charger_type;
1718 int usb_id, gadget_flag;
1720 usb_id = dwc_otg_check_dpdm();
1723 charger_type = NO_CHARGER;
1727 charger_type = USB_CHARGER;
1730 charger_type = AC_CHARGER;
1733 charger_type = NO_CHARGER;
1736 DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1737 if (charger_type == USB_CHARGER) {
1738 gadget_flag = get_gadget_connect_flag();
1739 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1740 __func__, gadget_flag, di->check_count);
1742 if (0 == gadget_flag) {
1743 if (++di->check_count >= 5) {
1744 charger_type = AC_CHARGER;
1745 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1746 __func__, di->check_count);
1748 charger_type = USB_CHARGER;
1751 charger_type = USB_CHARGER;
1754 di->check_count = 0;
1757 return charger_type;
1761 * it is first time for battery to be weld, init by ocv table
1763 static void rsoc_first_poweron_init(struct battery_info *di)
1765 _save_FCC_capacity(di, di->design_capacity);
1766 di->fcc = _get_FCC_capacity(di);
1768 di->temp_soc = _voltage_to_capacity(di, di->voltage_ocv);
1769 di->real_soc = di->temp_soc;
1770 di->nac = di->temp_nac;
1771 di->first_on_cap = di->nac;
1773 rk81x_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1774 rk81x_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1775 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1776 __func__, di->real_soc, di->nac, di->fcc);
1780 * it is not first time for battery to be weld, init by last record info
1782 static void rsoc_not_first_poweron_init(struct battery_info *di)
1788 int remain_capacity;
1790 enum charger_type_t charger_type;
1792 rk81x_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1793 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1794 init_soc = pwron_soc;
1795 DBG("<%s> Not first pwron, SOC_REG = %d\n", __func__, pwron_soc);
1797 if (rk81x_support_adp_type(HW_ADP_TYPE_USB)) {
1798 charger_type = rk81x_get_usbac_state(di);
1799 if ((pwron_soc == 0) && (charger_type == USB_CHARGER)) {
1801 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1805 remain_capacity = _get_remain_capacity(di);
1806 /* check if support uboot charge,
1807 * if support, uboot charge driver should have done init work,
1808 * so here we should skip init work
1810 if (support_uboot_charge())
1813 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG,
1814 &curr_shtd_time, 1);
1815 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1816 &last_shtd_time, 1);
1817 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1818 &curr_shtd_time, 1);
1819 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n",
1820 __func__, curr_shtd_time, last_shtd_time, charger_type);
1822 ocv_soc = _voltage_to_capacity(di, di->voltage_ocv);
1823 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
1824 __func__, remain_capacity, di->temp_nac);
1826 /* if plugin, make sure current shtd_time diff from last_shtd_time.*/
1827 if (last_shtd_time != curr_shtd_time) {
1828 if (curr_shtd_time > 30) {
1829 rk81x_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1831 remain_capacity = di->temp_nac;
1832 di->first_on_cap = remain_capacity;
1833 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
1834 __func__, remain_capacity);
1836 } else if ((curr_shtd_time > 5) &&
1837 (abs32_int(ocv_soc - init_soc) >= 10)) {
1838 if (remain_capacity >= di->temp_nac*120/100)
1839 remain_capacity = di->temp_nac*110/100;
1840 else if (remain_capacity < di->temp_nac*8/10)
1841 remain_capacity = di->temp_nac*9/10;
1842 DBG("<%s> pwroff > 3 minute, remain_cap = %d\n",
1843 __func__, remain_capacity);
1846 rk81x_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1849 di->real_soc = init_soc;
1850 di->nac = remain_capacity;
1853 DBG("<%s> init_soc = %d, init_capacity=%d\n",
1854 __func__, di->real_soc, di->nac);
1857 static u8 get_sys_pwroff_min(struct battery_info *di)
1859 u8 curr_shtd_time, last_shtd_time;
1861 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG,
1862 &curr_shtd_time, 1);
1863 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1864 &last_shtd_time, 1);
1866 return (curr_shtd_time != last_shtd_time) ? curr_shtd_time : 0;
1869 static int _rsoc_init(struct battery_info *di)
1872 u8 calib_en;/*debug*/
1874 di->voltage = _get_battery_voltage(di);
1875 di->voltage_ocv = _get_OCV_voltage(di);
1876 pwroff_min = get_sys_pwroff_min(di);
1878 DBG("OCV voltage=%d, voltage=%d, pwroff_min=%d\n",
1879 di->voltage_ocv, di->voltage, pwroff_min);
1881 calib_en = rk81x_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
1882 DBG("readbit: calib_en=%d\n", calib_en);
1883 if (_is_first_poweron(di) ||
1884 ((pwroff_min >= 30) && (calib_en == 1))) {
1885 rsoc_first_poweron_init(di);
1886 rk81x_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
1889 rsoc_not_first_poweron_init(di);
1896 static u8 rk81x_get_charge_status(struct battery_info *di)
1901 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1906 DBG(" CHARGE-OFF ...\n");
1911 DBG(" DEAD CHARGE ...\n");
1914 case TRICKLE_CHARGE:
1916 DBG(" TRICKLE CHARGE ...\n ");
1921 DBG(" CC or CV ...\n");
1925 ret = CHARGE_FINISH;
1926 DBG(" CHARGE FINISH ...\n");
1931 DBG(" USB OVER VOL ...\n");
1936 DBG(" BAT TMP ERROR ...\n");
1941 DBG(" TIMER ERROR ...\n");
1946 DBG(" USB EXIST ...\n");
1951 DBG(" USB EFF...\n");
1961 static void set_charge_current(struct battery_info *di, int charge_current)
1965 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1966 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1967 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1968 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1971 static void rk81x_fg_match_param(struct battery_info *di, int chg_vol,
1972 int chg_ilim, int chg_cur)
1976 di->chg_v_lmt = DEF_CHRG_VOL;
1977 di->chg_i_lmt = DEF_CHRG_CURR_LMT;
1978 di->chg_i_cur = DEF_CHRG_CURR_SEL;
1980 for (i = 0; i < ARRAY_SIZE(CHG_V_LMT); i++) {
1981 if (chg_vol < CHG_V_LMT[i])
1984 di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1987 for (i = 0; i < ARRAY_SIZE(CHG_I_LMT); i++) {
1988 if (chg_ilim < CHG_I_LMT[i])
1991 di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1994 for (i = 0; i < ARRAY_SIZE(CHG_I_CUR); i++) {
1995 if (chg_cur < CHG_I_CUR[i])
1998 di->chg_i_cur = (i << CHG_ICUR_SHIFT);
2000 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2001 __func__, di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
2004 static u8 rk81x_chose_finish_ma(int fcc)
2006 u8 ma = FINISH_150MA;
2011 else if (fcc >= 3000 && fcc <= 4000)
2014 else if (fcc > 4000 && fcc <= 5000)
2023 static void rk81x_battery_charger_init(struct battery_info *di)
2025 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2026 u8 sup_sts_reg, thremal_reg;
2027 int chg_vol, chg_cur, chg_ilim;
2030 chg_vol = di->rk818->battery_data->max_charger_voltagemV;
2032 if (di->fg_drv_mode == TEST_POWER_MODE) {
2033 chg_cur = di->test_chrg_current;
2034 chg_ilim = di->test_chrg_ilmt;
2036 chg_cur = di->rk818->battery_data->max_charger_currentmA;
2037 chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
2040 rk81x_fg_match_param(di, chg_vol, chg_ilim, chg_cur);
2041 finish_ma = rk81x_chose_finish_ma(di->fcc);
2043 battery_read(di->rk818, THERMAL_REG, &thremal_reg, 1);
2044 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2045 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2046 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2047 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
2048 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2051 usb_ctrl_reg &= (~0x0f);
2053 if (rk81x_support_adp_type(HW_ADP_TYPE_USB))
2054 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2056 usb_ctrl_reg |= (CHRG_CT_EN | di->chg_i_lmt);
2058 thremal_reg &= (~0x0c);
2059 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2061 chrg_ctrl_reg1 &= (0x00);
2062 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
2064 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2065 chrg_ctrl_reg2 &= ~(0xc7);
2066 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2068 sup_sts_reg &= ~(0x01 << 3);
2069 sup_sts_reg |= (0x01 << 2);
2071 thremal_reg &= (~0x0c);
2072 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2074 battery_write(di->rk818, THERMAL_REG, &thremal_reg, 1);
2075 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2076 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2077 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2078 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2079 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
2082 void charge_disable_open_otg(int value)
2084 struct battery_info *di = g_battery;
2087 DBG("charge disable, enable OTG.\n");
2088 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2089 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2092 DBG("charge enable, disable OTG.\n");
2093 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2094 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2098 static void rk81x_low_waring_init(struct battery_info *di)
2103 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
2105 /* 3.4v: interrupt*/
2106 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06);
2107 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
2110 static void rk81x_fg_init(struct battery_info *di)
2117 adc_ctrl_val = 0x30;
2118 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
2121 /* get the volatege offset */
2122 _get_voltage_offset_value(di);
2123 rk81x_battery_charger_init(di);
2124 _set_relax_thres(di);
2126 /* get the current offset , the value write to the CAL_OFFSET */
2127 di->current_offset = _get_ioffset(di);
2128 battery_read(di->rk818, PCB_IOFFSET_REG, &pcb_offset, 1);
2129 DBG("<%s>. pcb_offset = 0x%x\n", __func__, pcb_offset);
2130 DBG("<%s>. io_offset = 0x%x\n", __func__, di->current_offset);
2132 _set_cal_offset(di, di->current_offset+pcb_offset);
2133 cal_offset = _get_cal_offset(di);
2134 if ((cal_offset < 0x7ff) || (pcb_offset == 0))
2135 _set_cal_offset(di, di->current_offset+42);
2138 _capacity_init(di, di->nac);
2140 di->remain_capacity = _get_realtime_capacity(di);
2141 di->current_avg = _get_average_current(di);
2143 rk81x_low_waring_init(di);
2145 power_on_save(di, di->voltage_ocv);
2146 battery_write(di->rk818, OCV_VOL_VALID_REG, &buf, 1);
2148 /* set sample time for cal_offset interval*/
2149 ioffset_sample_time(di, SAMP_TIME_8MIN);
2150 dump_gauge_register(di);
2151 dump_charger_register(di);
2154 "nac = %d , remain_capacity = %d\n"
2155 "OCV_voltage = %d, voltage = %d\n"
2156 "SOC = %d, fcc = %d\n, current=%d\n"
2157 "cal_offset = 0x%x\n",
2159 di->nac, di->remain_capacity,
2160 di->voltage_ocv, di->voltage,
2161 di->real_soc, di->fcc, di->current_avg,
2165 * this is a very important algorithm to avoid over discharge.
2167 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
2168 static void zero_get_soc(struct battery_info *di)
2170 int dead_voltage, ocv_voltage;
2171 int temp_soc = -1, real_soc;
2172 int currentold, currentnow, voltage;
2179 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
2181 currentold = _get_average_current(di);
2182 _get_cal_offset(di);
2185 currentnow = _get_average_current(di);
2187 } while ((currentold == currentnow) && (count_num < 11));
2190 for (i = 0; i < 10 ; i++)
2191 voltage += _get_battery_voltage(di);
2194 if (di->voltage_old == 0)
2195 di->voltage_old = voltage;
2196 voltage_k = voltage;
2197 voltage = (di->voltage_old*2 + 8*voltage)/10;
2198 di->voltage_old = voltage;
2199 currentnow = _get_average_current(di);
2201 dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
2202 /* 65 mo power-path mos */
2203 ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
2204 DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2205 dead_voltage, ocv_voltage);
2207 ocv_soc = _voltage_to_capacity(di, dead_voltage);
2208 di->q_dead = di->temp_nac;
2209 DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
2210 ocv_soc, di->q_dead);
2212 ocv_soc = _voltage_to_capacity(di, ocv_voltage);
2213 q_ocv = di->temp_nac;
2214 DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
2217 /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
2218 di->q_err = di->remain_capacity - q_ocv;
2219 DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
2220 di->q_err, di->remain_capacity, q_ocv);
2222 if (di->display_soc == 0)
2223 di->display_soc = di->real_soc*1000;
2224 real_soc = di->display_soc;
2226 DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
2227 di->remain_capacity, di->q_dead, di->q_err);
2228 /*[temp_nac]:dead_voltage*/
2229 if (q_ocv > di->q_dead) {
2230 DBG("first: q_ocv > di->q_dead\n");
2233 if ((di->update_k == 0) || (di->zero_cycle >= 500)) {
2237 /* ZQ = Q_ded + Qerr */
2238 /*[temp_nac]:dead_voltage*/
2239 di->q_shtd = di->q_dead + di->q_err;
2240 temp_soc = (di->remain_capacity - di->q_shtd)*
2245 di->line_k = (real_soc + temp_soc/2)
2248 } else if (di->zero_updated && di->update_k >= 10) {
2249 DBG("[K >= 10].\n");
2251 _voltage_to_capacity(di, dead_voltage);
2252 di->q_dead = di->temp_nac;
2253 di->q_shtd = di->q_dead + di->q_err;
2254 temp_soc = ((di->remain_capacity - di->q_shtd)*
2255 1000 + di->fcc/2)/div(di->fcc); /* z1 */
2259 di->line_k = (real_soc + temp_soc/2)
2262 DBG("[K >= 10]. new:line_k = %d\n", di->line_k);
2263 DBG("[K >= 10]. new:Y0(dis_soc)=%d\n", di->display_soc);
2264 DBG("[K >= 10]. new:X0(temp) = %d\n", temp_soc);
2266 } else { /*update_k[1~9]*/
2270 DBG("[K1~9]. (old)Y0=%d, Y0=%d\n",
2271 di->old_display_soc, di->display_soc);
2272 if (di->update_k == 2)
2273 di->old_display_soc = di->display_soc;
2275 temp_soc = ((di->remain_capacity - di->q_shtd)*
2276 1000 + di->fcc/2)/div(di->fcc);
2277 real_soc = di->line_k*temp_soc;
2278 di->display_soc = real_soc;
2280 /* make sure display_soc change at least once*/
2281 if (di->display_soc >= di->old_display_soc)
2282 di->zero_updated = false;
2284 di->zero_updated = true;
2286 DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
2287 DBG("[K1~9]. line_k = %d\n", di->line_k);
2288 DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
2289 di->display_soc, di->real_soc);
2291 if ((di->display_soc+500)/1000 < di->real_soc) {
2293 if ((di->real_soc == 1) &&
2294 (di->display_soc < 100))
2298 /*di->odd_capacity = 0;*/
2302 DBG("second: q_ocv < di->q_dead\n");
2305 if (di->voltage < 3400) {
2306 DBG("second: voltage < 3400\n");
2309 if (di->update_k > 10) {
2312 di->odd_capacity = 0;
2317 if (di->line_k <= 0) {
2319 DBG("ZERO: line_k <= 0, Update line_k!\n");
2322 DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
2323 DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
2324 DBG("ZERO: remain_cap - q_shtd=%d\n",
2325 (di->remain_capacity - di->q_shtd));
2326 DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
2327 di->line_k, di->display_soc, temp_soc);
2328 DBG("ZERO: zero_cycle=%d,(old)Y0=%d, zero_updated=%d, update_k=%d\n",
2329 di->zero_cycle, di->old_display_soc,
2330 di->zero_updated, di->update_k);
2332 DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
2333 di->remain_capacity, di->q_shtd, di->q_err);
2334 DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
2335 di->warnning_voltage, _get_soc(di), di->real_soc);
2339 static int estimate_bat_ocv_vol(struct battery_info *di)
2341 return (di->voltage -
2342 (di->bat_res * di->current_avg) / 1000);
2345 static int estimate_bat_ocv_soc(struct battery_info *di)
2347 int ocv_soc, ocv_voltage;
2349 ocv_voltage = estimate_bat_ocv_vol(di);
2350 ocv_soc = _voltage_to_capacity(di, ocv_voltage);
2355 /* we will estimate a ocv voltage to get a ocv soc.
2356 * if there is a big offset between ocv_soc and rsoc,
2357 * we will decide whether we should reinit capacity or not
2359 static void rsoc_dischrg_calib(struct battery_info *di)
2361 int ocv_soc = di->est_ocv_soc;
2362 int ocv_volt = di->est_ocv_vol;
2363 int temp_soc = _get_soc(di);
2364 int max_volt = di->rk818->battery_data->max_charger_voltagemV;
2366 if (ocv_volt > max_volt)
2369 if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
2370 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
2371 (di->temp_soc == 0) ||
2372 (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
2374 di->err_soc_sum += ocv_soc;
2378 DBG("<%s>. rsoc err_chck_cnt = %d\n",
2379 __func__, di->err_chck_cnt);
2380 DBG("<%s>. rsoc err_soc_sum = %d\n",
2381 __func__, di->err_soc_sum);
2383 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2384 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2385 if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
2386 ocv_soc += RSOC_COMPS;
2388 di->temp_nac = ocv_soc * di->fcc / 100;
2389 _capacity_init(di, di->temp_nac);
2390 di->temp_soc = _get_soc(di);
2391 di->remain_capacity = _get_realtime_capacity(di);
2392 di->err_soc_sum = 0;
2393 di->err_chck_cnt = 0;
2394 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2398 di->err_chck_cnt = 0;
2399 di->err_soc_sum = 0;
2403 static void rsoc_realtime_calib(struct battery_info *di)
2405 u8 status = di->status;
2407 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2408 (status == POWER_SUPPLY_STATUS_FULL)) {
2409 if ((di->current_avg < -10) &&
2410 (di->charge_status != CHARGE_FINISH))
2411 rsoc_dischrg_calib(di);
2414 rsoc_chrg_calib(di);
2417 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2418 rsoc_dischrg_calib(di);
2423 * when there is a big offset between dsoc and rsoc, dsoc needs to
2424 * speed up to keep pace witch rsoc.
2426 static bool do_ac_charger_emulator(struct battery_info *di)
2428 int delta_soc = di->temp_soc - di->real_soc;
2431 if ((di->charge_status != CHARGE_FINISH) &&
2432 (di->ac_online == ONLINE) &&
2433 (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)) {
2434 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2435 soc_time = di->fcc*3600/100/
2436 (abs_int(DSOC_CHRG_EMU_CURR));
2438 soc_time = di->fcc*3600/100/
2439 div(abs_int(di->current_avg));
2441 if (di->emu_chg_cnt > soc_time) {
2443 di->emu_chg_cnt = 0;
2445 DBG("<%s>. soc_time=%d, emu_cnt=%d\n",
2446 __func__, soc_time, di->emu_chg_cnt);
2454 /* check voltage and current when dsoc is close to full.
2455 * we will do a fake charge to adjust charing speed which
2456 * aims to make battery full charged and match finish signal.
2458 static bool do_term_chrg_calib(struct battery_info *di)
2461 u32 *ocv_table = di->platform_data->battery_ocv;
2463 /*check current and voltage*/
2464 if ((di->ac_online == ONLINE && di->real_soc >= 90) &&
2465 ((di->current_avg > DSOC_CHG_TERM_CURR) ||
2466 (di->voltage < ocv_table[18]+20))) {
2467 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2469 if (di->term_chg_cnt > soc_time) {
2471 di->term_chg_cnt = 0;
2473 DBG("<%s>. soc_time=%d, term_cnt=%d\n",
2474 __func__, soc_time, di->term_chg_cnt);
2482 static void normal_discharge(struct battery_info *di)
2485 int now_current = di->current_avg;
2486 int delta_soc = di->real_soc - di->temp_soc;
2488 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2489 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2490 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2491 __func__, delta_soc);
2493 soc_time = di->fcc*3600/100/div(abs_int(now_current));
2496 if (di->temp_soc == di->real_soc) {
2497 DBG("<%s>. temp_soc == real_soc\n", __func__);
2499 } else if (di->temp_soc > di->real_soc) {
2500 DBG("<%s>. temp_soc > real_soc\n", __func__);
2501 di->vol_smooth_time++;
2502 if (di->vol_smooth_time > soc_time*3/2) {
2504 di->vol_smooth_time = 0;
2508 DBG("<%s>. temp_soc < real_soc\n", __func__);
2509 if (di->real_soc == (di->temp_soc + 1)) {
2510 di->change_timer = di->soc_timer;
2511 di->real_soc = di->temp_soc;
2513 di->vol_smooth_time++;
2514 if (di->vol_smooth_time > soc_time*3/4) {
2516 di->vol_smooth_time = 0;
2521 DBG("<%s>, temp_soc = %d, real_soc = %d\n",
2522 __func__, di->temp_soc, di->real_soc);
2523 DBG("<%s>, vol_smooth_time = %d, soc_time = %d\n",
2524 __func__, di->vol_smooth_time, soc_time);
2527 static void rk81x_battery_discharge_smooth(struct battery_info *di)
2531 ocv_soc = _voltage_to_capacity(di, 3800);
2532 di->temp_soc = _get_soc(di);
2534 DBG("<%s>. temp_soc = %d, real_soc = %d\n",
2535 __func__, di->temp_soc, di->real_soc);
2537 if (di->voltage < 3800)
2541 normal_discharge(di);
2544 static int get_charging_time(struct battery_info *di)
2546 return (di->charging_time/60);
2549 static int get_discharging_time(struct battery_info *di)
2551 return (di->discharging_time/60);
2554 static int get_finish_time(struct battery_info *di)
2556 return (di->finish_time/60);
2559 static void upd_time_table(struct battery_info *di);
2560 static void collect_debug_info(struct battery_info *di)
2562 if ((di->ac_online == ONLINE) || (di->usb_online == ONLINE)) {
2563 di->charging_time++;
2564 di->discharging_time = 0;
2566 di->charging_time = 0;
2567 if (di->voltage < 3800)
2568 di->discharging_time += 2;
2570 di->discharging_time++;
2572 if (di->charge_status == CHARGE_FINISH)
2575 di->finish_time = 0;
2577 di->charge_min = get_charging_time(di);
2578 di->discharge_min = get_discharging_time(di);
2579 di->finish_min = get_finish_time(di);
2584 static void dump_debug_info(struct battery_info *di)
2586 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2587 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2588 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2590 collect_debug_info(di);
2592 battery_read(di->rk818, MISC_MARK_REG, &misc_reg, 1);
2593 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2594 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2595 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2596 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2597 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2598 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2599 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2600 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2601 battery_read(di->rk818, 0x00, &rtc_val, 1);
2602 battery_read(di->rk818, THERMAL_REG, &thremal_reg, 1);
2604 DBG("\n------------- dump_debug_regs -----------------\n"
2605 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2606 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2607 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2608 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2609 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2610 ggcon_reg, ggsts_reg, rtc_val,
2611 sup_tst_reg, vb_mod_reg,
2612 usb_ctrl_reg, chrg_ctrl_reg1,
2613 thremal_reg, misc_reg,
2614 chrg_ctrl_reg2, chrg_ctrl_reg3
2618 "########################## [read] 3.0############################\n"
2619 "--------------------------------------------------------------\n"
2620 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2621 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2622 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2623 "diplay_soc = %d, cpapacity_soc = %d, test_mode = %d\n"
2624 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2625 "finish_real_soc = %d, finish_temp_soc = %d\n"
2626 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2627 "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2628 get_relax_voltage(di),
2629 di->voltage, di->current_avg,
2630 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2631 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2632 di->real_soc, _get_soc(di), di->fg_drv_mode,
2633 di->ac_online, di->usb_online, di->status,
2634 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2635 _get_ioffset(di), _get_cal_offset(di), di->adjust_cap,
2636 get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2638 rk81x_get_charge_status(di);
2639 DBG("###########################################################\n");
2642 static void update_fcc_capacity(struct battery_info *di)
2647 remain_cap = di->remain_capacity + di->adjust_cap - di->first_on_cap;
2648 DBG("%s: remain_cap:%d, ajust_cap:%d, first_on_cap=%d\n",
2649 __func__, remain_cap, di->adjust_cap, di->first_on_cap);
2651 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2652 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2653 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2655 if (get_level(di) >= di->dod0_level) {
2656 fcc0 = (remain_cap - di->dod0_capacity)*100
2658 if (fcc0 > di->qmax)
2661 DBG("%s: fcc0:%d, fcc:%d\n", __func__, fcc0, di->fcc);
2662 if ((fcc0 < di->fcc) && (fcc0 > 1000)) {
2664 _capacity_init(di, di->fcc);
2665 _save_FCC_capacity(di, di->fcc);
2666 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2669 di->dod0_status = 0;
2673 static void debug_get_finish_soc(struct battery_info *di)
2675 if (di->charge_status == CHARGE_FINISH) {
2676 di->debug_finish_real_soc = di->real_soc;
2677 di->debug_finish_temp_soc = di->temp_soc;
2681 static void wait_charge_finish_signal(struct battery_info *di)
2683 if ((di->charge_status == CHARGE_FINISH) &&
2684 (di->voltage > CHG_FINISH_VOL))
2685 update_fcc_capacity(di);/* save new fcc*/
2688 debug_get_finish_soc(di);
2691 static void charge_finish_routine(struct battery_info *di)
2693 if ((di->charge_status == CHARGE_FINISH) &&
2694 (di->voltage > CHG_FINISH_VOL)) {
2695 _capacity_init(di, di->fcc);
2696 zero_current_calib(di);
2698 if (di->real_soc < 100) {
2699 DBG("<%s>,CHARGE_FINISH:real_soc<100,real_soc=%d\n",
2700 __func__, di->real_soc);
2702 if ((di->soc_counter < 80)) {
2705 di->soc_counter = 0;
2712 static void normal_charge(struct battery_info *di)
2714 int now_current, soc_time;
2716 now_current = _get_average_current(di);
2717 soc_time = di->fcc*3600/100/div(abs_int(now_current)); /* 1% time; */
2718 di->temp_soc = _get_soc(di);
2720 DBG("<%s>. temp_soc = %d, real_soc = %d\n",
2721 __func__, di->temp_soc, di->real_soc);
2723 if (di->real_soc == di->temp_soc) {
2724 DBG("<%s>. temp_soc == real_soc\n", __func__);
2725 di->temp_soc = _get_soc(di);
2727 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2728 if (di->temp_soc < di->real_soc + 1) {
2729 DBG("<%s>. temp_soc < real_soc\n", __func__);
2730 di->charge_smooth_time++;
2731 if (di->charge_smooth_time > soc_time*3/2) {
2733 di->charge_smooth_time = 0;
2735 di->charge_smooth_status = true;
2738 else if (di->temp_soc > di->real_soc + 1) {
2739 DBG("<%s>. temp_soc > real_soc\n", __func__);
2740 di->charge_smooth_time++;
2741 if (di->charge_smooth_time > soc_time*3/4) {
2743 di->charge_smooth_time = 0;
2745 di->charge_smooth_status = true;
2747 } else if (di->temp_soc == di->real_soc + 1) {
2748 DBG("<%s>. temp_soc == real_soc + 1\n", __func__);
2749 if (di->charge_smooth_status) {
2750 di->charge_smooth_time++;
2751 if (di->charge_smooth_time > soc_time*3/4) {
2752 di->real_soc = di->temp_soc;
2753 di->charge_smooth_time = 0;
2754 di->charge_smooth_status = false;
2758 di->real_soc = di->temp_soc;
2759 di->charge_smooth_status = false;
2764 DBG("<%s>, temp_soc = %d, real_soc = %d\n",
2765 __func__, di->temp_soc, di->real_soc);
2766 DBG("<%s>, vol_smooth_time = %d, soc_time = %d\n",
2767 __func__, di->charge_smooth_time, soc_time);
2772 static void rk81x_battery_charge_smooth(struct battery_info *di)
2775 /*calibrate: aim to match finish signal*/
2776 if (do_term_chrg_calib(di))
2779 /*calibrate: aim to calib error*/
2780 di->term_chg_cnt = 0;
2781 if (do_ac_charger_emulator(di))
2787 static void rk81x_battery_display_smooth(struct battery_info *di)
2792 status = di->status;
2793 charge_status = di->charge_status;
2794 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2795 (status == POWER_SUPPLY_STATUS_FULL)) {
2796 if ((di->current_avg < -10) &&
2797 (charge_status != CHARGE_FINISH))
2798 rk81x_battery_discharge_smooth(di);
2800 rk81x_battery_charge_smooth(di);
2802 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2803 rk81x_battery_discharge_smooth(di);
2804 if (di->real_soc == 1) {
2806 if (di->time2empty >= 300)
2815 * update rsoc by relax voltage
2817 static void resume_relax_calib(struct battery_info *di)
2819 int relax_vol = di->relax_voltage;
2820 int ocv_soc, capacity;
2822 ocv_soc = _voltage_to_capacity(di, relax_vol);
2823 capacity = (ocv_soc * di->fcc / 100);
2824 _capacity_init(di, capacity);
2825 di->remain_capacity = _get_realtime_capacity(di);
2826 di->temp_soc = _get_soc(di);
2827 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
2832 * 0: when neccessary
2834 static void resume_vol_calib(struct battery_info *di, int condition)
2836 int ocv_vol = di->est_ocv_vol;
2837 int ocv_soc = 0, capacity = 0;
2839 ocv_soc = _voltage_to_capacity(di, ocv_vol);
2840 capacity = (ocv_soc * di->fcc / 100);
2841 if (condition || (abs(ocv_soc-di->temp_soc) >= RSOC_RESUME_ERR)) {
2842 _capacity_init(di, capacity);
2843 di->remain_capacity = _get_realtime_capacity(di);
2844 di->temp_soc = _get_soc(di);
2845 DBG("<%s>, rsoc updated!\n", __func__);
2847 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
2848 __func__, ocv_vol, ocv_soc, capacity);
2852 * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
2853 * and dc_adp are plugined in together, the dc_apt has high priority.
2854 * so we check dc_apt first and return rigth away if it's found.
2856 static enum charger_type_t rk81x_get_adp_type(struct battery_info *di)
2859 enum charger_type_t charger_type = NO_CHARGER;
2861 /*check by ic hardware: this check make check work safer*/
2862 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2863 if ((buf & PLUG_IN_STS) == 0)
2867 if (rk81x_support_adp_type(HW_ADP_TYPE_DC)) {
2868 charger_type = rk81x_get_dc_state(di);
2869 if (charger_type == DC_CHARGER)
2870 return charger_type;
2874 charger_type = rk81x_get_usbac_state(di);
2876 return charger_type;
2879 static void rk81x_sleep_discharge(struct battery_info *di)
2884 unsigned long sleep_sec;
2887 enter_rsoc = di->real_soc;
2888 sleep_sec = BASE_TO_SEC(di->suspend_time_start);
2889 sleep_min = BASE_TO_MIN(di->suspend_time_start);
2890 delta_cap = di->suspend_cap - di->remain_capacity;
2891 delta_soc = di->suspend_rsoc - _get_soc(di);
2892 di->sum_suspend_cap += delta_cap;
2894 DBG("<%s>, slp_sec(s)=%lu, slp_min=%d\n"
2895 "delta_cap(s)=%d, delta_soc=%d, sum_cap=%d\n"
2896 "remain_cap=%d, rsoc=%d, dsoc=%d\n"
2897 "relax_vol=%d, vol=%d, curr=%d\n",
2898 __func__, sleep_sec, sleep_min,
2899 delta_cap, delta_soc, di->sum_suspend_cap,
2900 di->remain_capacity, _get_soc(di), di->real_soc,
2901 di->relax_voltage, di->voltage, _get_average_current(di));
2904 if ((sleep_min >= 30) &&
2905 (di->relax_voltage >= di->voltage)) {
2906 resume_relax_calib(di);
2909 /* current_avg < 0: make sure the system is not
2910 * wakeup by charger plugin.
2913 /* even if relax voltage is not caught rightly, realtime voltage
2914 * is quite close to relax voltage, we should not do nothing after
2917 } else if ((sleep_min >= 30) && (di->current_avg < 0)) {
2918 resume_vol_calib(di, 1);
2919 } else if ((sleep_min >= 3) && (di->current_avg < 0)) {
2920 resume_vol_calib(di, 0);
2924 delta_soc = di->sum_suspend_cap/(di->fcc/100);
2926 DBG("<%s>. sum_cap ==> delta_soc = %d\n", __func__, delta_soc);
2927 if (delta_soc > 0) {
2928 if (di->real_soc-(delta_soc*1/3) <= di->temp_soc)
2929 di->real_soc -= (delta_soc*1/3);
2930 else if (di->real_soc-(delta_soc*1/2) < di->temp_soc)
2931 di->real_soc -= (delta_soc*1/2);
2933 di->real_soc -= delta_soc;
2935 /*di->sum_suspend_cap %= (di->fcc/100);*/
2936 if (di->real_soc != enter_rsoc)
2937 di->sum_suspend_cap = 0;
2939 } else if (delta_soc < 0) {
2942 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
2943 __func__, di->real_soc, di->temp_soc, di->sum_suspend_cap);
2946 static void rk81x_sleep_charge(struct battery_info *di)
2948 unsigned long sleep_soc;
2949 unsigned long sleep_sec;
2953 u8 charge_status = di->charge_status;
2955 if ((di->suspend_charge_current >= 0) ||
2956 (rk81x_get_charge_status(di) == CHARGE_FINISH)) {
2957 sleep_sec = BASE_TO_SEC(di->suspend_time_start);
2958 sleep_min = BASE_TO_MIN(di->suspend_time_start);
2959 delta_cap = di->suspend_cap - di->remain_capacity;
2960 delta_soc = di->suspend_rsoc - _get_soc(di);
2962 DBG("<%s>, ac=%d, usb=%d, slp_curr=%d\n",
2963 __func__, di->ac_online, di->usb_online,
2964 di->suspend_charge_current);
2965 if (((di->suspend_charge_current < 800) &&
2966 (di->ac_online == ONLINE)) ||
2967 (charge_status == CHARGE_FINISH)) {
2968 DBG("<%s>,sleep: ac online current < 800\n", __func__);
2969 if (sleep_sec > 0) {
2970 /*default charge current: 1000mA*/
2971 di->count_sleep_time += sleep_sec;
2972 sleep_soc = 1000*di->count_sleep_time*100
2974 DBG("<%s> sleep_soc=%lu, real_soc=%d\n",
2975 __func__, sleep_soc, di->real_soc);
2977 di->count_sleep_time = 0;
2978 di->real_soc += sleep_soc;
2979 if (di->real_soc > 100)
2983 DBG("<%s>, usb charge\n", __func__);
2984 if ((di->temp_soc - di->suspend_rsoc) > 0)
2986 (di->temp_soc - di->suspend_rsoc);
2989 DBG("<%s>, out: dsoc=%d, rsoc=%d\n",
2990 __func__, di->real_soc, di->temp_soc);
2995 * we need flag "sys_wakeup" to make sure that the system is reall power up.
2996 * because there is fake system power up which causes suspend param be cleaned.
2998 static void update_resume_state(struct battery_info *di)
3002 di->sys_wakeup = true;
3003 /*update the info first*/
3004 rk81x_update_battery_info(di);
3007 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING)
3008 rk81x_sleep_discharge(di);
3010 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING)
3011 rk81x_sleep_charge(di);
3015 static void rk81x_set_charger_current(struct battery_info *di,
3016 enum charger_type_t charger_type)
3018 switch (charger_type) {
3021 set_charge_current(di, ILIM_450MA);
3026 set_charge_current(di, di->chg_i_lmt);
3029 set_charge_current(di, ILIM_450MA);
3034 static void rk81x_set_power_supply_state(struct battery_info *di,
3035 enum charger_type_t charger_type)
3037 di->usb_online = OFFLINE;
3038 di->ac_online = OFFLINE;
3039 di->dc_online = OFFLINE;
3041 switch (charger_type) {
3043 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3047 di->usb_online = ONLINE;
3048 di->status = POWER_SUPPLY_STATUS_CHARGING;
3051 case DC_CHARGER:/*treat dc as ac*/
3052 di->dc_online = ONLINE;
3054 di->ac_online = ONLINE;
3055 di->status = POWER_SUPPLY_STATUS_CHARGING;
3058 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3061 if (di->real_soc >= 100)
3062 di->status = POWER_SUPPLY_STATUS_FULL;
3065 static void rk81x_check_battery_status(struct battery_info *di)
3067 enum charger_type_t charger_type;
3069 charger_type = rk81x_get_adp_type(di);
3070 rk81x_set_charger_current(di, charger_type);
3071 rk81x_set_power_supply_state(di, charger_type);
3075 /* high load: current < 0 with charger in.
3076 * System will not shutdown while dsoc=0% with charging state(ac_online),
3077 * which will cause over discharge, so oppose status before report states.
3079 static void last_check_report(struct battery_info *di)
3083 if ((di->real_soc == 0) &&
3084 (di->status == POWER_SUPPLY_STATUS_CHARGING) &&
3085 di->current_avg < 0) {
3086 if (BASE_TO_SEC(time) > 60)
3087 rk81x_set_power_supply_state(di, NO_CHARGER);
3089 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
3090 DBG("status=%d, ac_online=%d, usb_online=%d\n",
3091 di->status, di->ac_online, di->usb_online);
3094 time = get_seconds();
3098 * only do report when there is a change.
3100 * if ((di->real_soc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3101 * when real_soc == 0, we must do report. But it will generate too much android
3102 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3105 static void report_power_supply_changed(struct battery_info *di)
3108 static u32 old_ac_status;
3109 static u32 old_usb_status;
3110 static u32 old_charge_status;
3113 state_changed = false;
3114 if ((di->real_soc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3115 state_changed = true;
3116 else if (di->real_soc != old_soc)
3117 state_changed = true;
3118 else if (di->ac_online != old_ac_status)
3119 state_changed = true;
3120 else if (di->usb_online != old_usb_status)
3121 state_changed = true;
3122 else if (old_charge_status != di->status)
3123 state_changed = true;
3125 if (state_changed) {
3126 power_supply_changed(&di->bat);
3127 power_supply_changed(&di->usb);
3128 power_supply_changed(&di->ac);
3129 old_soc = di->real_soc;
3130 old_ac_status = di->ac_online;
3131 old_usb_status = di->usb_online;
3132 old_charge_status = di->status;
3133 DBG("<%s>. report: dsoc=%d, rsoc=%d\n",
3134 __func__, di->real_soc, di->temp_soc);
3138 static void upd_time_table(struct battery_info *di)
3141 static int old_index;
3144 int mod = di->real_soc % 10;
3145 int index = di->real_soc / 10;
3147 if (di->ac_online == ONLINE || di->usb_online == ONLINE)
3148 time = di->charge_min;
3150 time = di->discharge_min;
3152 if ((mod == 0) && (index > 0) && (old_index != index)) {
3153 di->chrg_min[index-1] = time - old_min;
3158 for (i = 1; i < 11; i++)
3159 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
3164 * there is a timer inside rk81x to calc how long the battery is in charging
3165 * state. rk81x will close PowerPath inside IC when timer reach, which will
3166 * stop the charging work. we have to reset the corresponding bits to restart
3167 * the timer to avoid that case.
3169 static void rk81x_init_chrg_timer(struct battery_info *di)
3173 battery_read(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3175 battery_write(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3176 battery_read(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3177 DBG("%s: clr: CHRG_CTRL_REG3<2> = 0x%x", __func__, buf);
3179 battery_write(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3182 static u8 get_cvcc_charge_hour(struct battery_info *di)
3186 battery_read(di->rk818, CHRG_CTRL_REG2, &buf, 1);
3189 return CHG_CVCC_HOUR[hour];
3192 /* we have to estimate the charging finish time from now, to decide
3193 * whether we should reset the timer or not.
3195 static void rk81x_check_chrg_over_time(struct battery_info *di)
3199 cvcc_hour = get_cvcc_charge_hour(di);
3200 DBG("CHG_TIME(min): %ld, cvcc hour: %d",
3201 BASE_TO_MIN(di->chrg_time_base), cvcc_hour);
3203 if (BASE_TO_MIN(di->chrg_time_base) >= (cvcc_hour-2)*60) {
3204 di->chrg_cap2_full = di->fcc - di->remain_capacity;
3205 if (di->current_avg <= 0)
3206 di->current_avg = 1;
3208 di->chrg_time2_full = di->chrg_cap2_full*3600/
3209 div(abs_int(di->current_avg));
3211 DBG("CHG_TIME2FULL(min):%d, chrg_cap2_full=%d, current=%d\n",
3212 SEC_TO_MIN(di->chrg_time2_full), di->chrg_cap2_full,
3215 if (SEC_TO_MIN(di->chrg_time2_full) > 60) {
3216 rk81x_init_chrg_timer(di);
3217 di->chrg_time_base = get_seconds();
3218 DBG("%s: reset charge timer\n", __func__);
3224 * in case that we will do reboot stress test, we need a special way
3225 * to ajust the dsoc.
3227 static void rk81x_check_reboot(struct battery_info *di)
3229 u8 rsoc = di->temp_soc;
3230 u8 dsoc = di->real_soc;
3231 u8 status = di->status;
3236 battery_read(di->rk818, REBOOT_CNT_REG, &cnt, 1);
3239 unit_time = di->fcc*3600/100/1200;/*1200mA default*/
3240 smooth_time = cnt*BASE_TO_SEC(di->sys_on_base);
3242 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3243 __func__, cnt, unit_time, smooth_time,
3244 BASE_TO_SEC(di->sys_on_base), dsoc, rsoc);
3246 if (((status == POWER_SUPPLY_STATUS_CHARGING) ||
3247 (status == POWER_SUPPLY_STATUS_FULL)) && (di->current_avg > 0)) {
3248 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time*3/5);
3249 if ((dsoc < rsoc-1) && (smooth_time > unit_time*3/5)) {
3254 _copy_soc(di, dsoc);
3256 } else {/*status == POWER_SUPPLY_STATUS_DISCHARGING*/
3258 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time*3/5);
3259 if ((dsoc > rsoc) && (smooth_time > unit_time*3/5)) {
3264 _copy_soc(di, dsoc);
3268 copy_reboot_cnt(di, cnt);
3272 static void rk81x_update_battery_info(struct battery_info *di)
3276 di->remain_capacity = _get_realtime_capacity(di);
3277 if (di->remain_capacity > di->fcc)
3278 _capacity_init(di, di->fcc);
3280 if (di->real_soc > 100)
3282 else if (di->real_soc < 0)
3285 if (di->chrg_time_base == 0)
3286 di->chrg_time_base = get_seconds();
3288 if (di->sys_on_base == 0)
3289 di->sys_on_base = get_seconds();
3291 if (di->status == POWER_SUPPLY_STATUS_DISCHARGING) {
3292 di->chrg_time_base = get_seconds();
3294 /*round off dsoc = 100*/
3295 round_off_dsoc = (di->remain_capacity+di->fcc/100/2)*
3297 if (round_off_dsoc >= 100 && di->real_soc >= 99)
3299 DBG("<%s>. round_off_dsoc = %d", __func__, round_off_dsoc);
3303 di->voltage = _get_battery_voltage(di);
3304 di->current_avg = _get_average_current(di);
3305 di->remain_capacity = _get_realtime_capacity(di);
3306 di->voltage_ocv = _get_OCV_voltage(di);
3307 di->charge_status = rk81x_get_charge_status(di);
3308 di->relax_voltage = get_relax_voltage(di);
3309 di->temp_soc = _get_soc(di);
3310 di->est_ocv_vol = estimate_bat_ocv_vol(di);
3311 di->est_ocv_soc = estimate_bat_ocv_soc(di);
3312 rk81x_check_battery_status(di);/* ac_online, usb_online, status*/
3313 rk81x_check_chrg_over_time(di);
3314 update_cal_offset(di);
3317 static void rk81x_battery_work(struct work_struct *work)
3319 struct battery_info *di = container_of(work,
3320 struct battery_info, battery_monitor_work.work);
3322 update_resume_state(di);
3323 wait_charge_finish_signal(di);
3324 charge_finish_routine(di);
3326 rk81x_battery_display_smooth(di);
3327 rk81x_update_battery_info(di);
3328 rsoc_realtime_calib(di);
3329 last_check_report(di);
3330 report_power_supply_changed(di);
3331 _copy_soc(di, di->real_soc);
3332 _save_remain_capacity(di, di->remain_capacity);
3334 dump_debug_info(di);
3335 di->queue_work_cnt++;
3336 queue_delayed_work(di->wq, &di->battery_monitor_work,
3337 msecs_to_jiffies(TIMER_MS_COUNTS));
3340 static void rk81x_battery_charge_check_work(struct work_struct *work)
3342 struct battery_info *di = container_of(work,
3343 struct battery_info, charge_check_work.work);
3345 DBG("rk81x_battery_charge_check_work\n");
3346 charge_disable_open_otg(di->charge_otg);
3349 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3351 int register_battery_notifier(struct notifier_block *nb)
3353 return blocking_notifier_chain_register(&battery_chain_head, nb);
3355 EXPORT_SYMBOL_GPL(register_battery_notifier);
3357 int unregister_battery_notifier(struct notifier_block *nb)
3359 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3361 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3363 int battery_notifier_call_chain(unsigned long val)
3365 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3366 == NOTIFY_BAD) ? -EINVAL : 0;
3368 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3370 static void poweron_lowerpoer_handle(struct battery_info *di)
3372 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
3373 if ((di->real_soc <= 2) &&
3374 (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
3376 /* kernel_power_off(); */
3381 static int battery_notifier_call(struct notifier_block *nb,
3382 unsigned long event, void *data)
3384 struct battery_info *di =
3385 container_of(nb, struct battery_info, battery_nb);
3389 DBG(" CHARGE enable\n");
3391 queue_delayed_work(di->wq, &di->charge_check_work,
3392 msecs_to_jiffies(50));
3397 queue_delayed_work(di->wq, &di->charge_check_work,
3398 msecs_to_jiffies(50));
3399 DBG("charge disable OTG enable\n");
3403 poweron_lowerpoer_handle(di);
3412 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
3414 pr_info("<%s>lower power warning!\n", __func__);
3416 rk_send_wakeup_key();
3421 static void disable_vbat_low_irq(struct battery_info *di)
3424 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
3425 /*clr vbat low interrupt */
3426 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
3428 static void enable_vbat_low_irq(struct battery_info *di)
3430 /* clr vbat low interrupt */
3431 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
3433 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
3436 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
3438 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3439 g_battery->chrg_time_base = get_seconds();
3440 rk_send_wakeup_key();
3443 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
3445 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3446 charge_disable_open_otg(0);
3447 rk_send_wakeup_key();
3451 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
3453 pr_info("---------- %s:irq = %d\n", __func__, irq);
3454 rk_send_wakeup_key();
3458 static int rk81x_battery_sysfs_init(struct battery_info *di, struct device *dev)
3462 struct kobject *rk818_fg_kobj;
3464 ret = create_sysfs_interfaces(dev);
3467 dev_err(dev, "device RK818 battery sysfs register failed\n");
3471 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3474 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3475 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3477 dev_err(dev, "create rk818_battery node error\n");
3485 power_supply_unregister(&di->ac);
3486 power_supply_unregister(&di->usb);
3487 power_supply_unregister(&di->bat);
3492 static void rk81x_battery_irq_init(struct battery_info *di)
3494 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3496 struct rk818 *chip = di->rk818;
3498 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3499 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3500 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3501 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3503 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3504 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3506 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3508 di->irq = vb_lo_irq;
3509 enable_irq_wake(di->irq);
3510 disable_vbat_low_irq(di);
3512 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3513 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in",
3516 dev_err(chip->dev, "plug_in_irq request failed!\n");
3519 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3520 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out",
3523 dev_err(chip->dev, "plug_out_irq request failed!\n");
3526 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3527 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok",
3530 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3534 static void rk81x_battery_info_init(struct battery_info *di, struct rk818 *chip)
3540 di->platform_data = chip->battery_data;
3541 di->cell.config = di->platform_data->cell_cfg;
3542 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3543 di->qmax = di->platform_data->cell_cfg->design_qmax;
3544 di->fcc = di->design_capacity;
3545 di->vol_smooth_time = 0;
3546 di->charge_smooth_time = 0;
3547 di->charge_smooth_status = false;
3548 di->sleep_status = 0;
3550 di->sys_wakeup = true;
3551 di->pcb_ioffset = 0;
3552 di->pcb_ioffset_updated = false;
3553 di->queue_work_cnt = 0;
3555 di->voltage_old = 0;
3556 di->display_soc = 0;
3559 di->sys_wakeup = true;
3560 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3563 di->discharge_min = 0;
3564 di->charging_time = 0;
3565 di->discharging_time = 0;
3566 di->finish_time = 0;
3570 di->odd_capacity = 0;
3571 di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3572 di->term_chg_cnt = 0;
3573 di->emu_chg_cnt = 0;
3575 di->chrg_time_base = 0;
3576 di->sys_on_base = 0;
3577 di->sum_suspend_cap = 0;
3579 di->first_on_cap = 0;
3580 di->fg_drv_mode = FG_NORMAL_MODE;
3582 for (i = 0; i < 10; i++)
3583 di->chrg_min[i] = 0;
3585 di->debug_finish_real_soc = 0;
3586 di->debug_finish_temp_soc = 0;
3588 fcc_capacity = _get_FCC_capacity(di);
3589 if (fcc_capacity > 1000)
3590 di->fcc = fcc_capacity;
3592 di->fcc = di->design_capacity;
3595 static struct of_device_id rk818_battery_of_match[] = {
3596 { .compatible = "rk818_battery" },
3600 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3605 * dc_det_pullup_inside:
3607 * 0: thers is resistance in the pcb to pull the pin up;
3608 * 1: there is no resistance in the pcb to pull the pin up.
3609 * we have to use inside pullup resistance function,
3610 * so we have to define pinctrl info in DTS and analyze it
3612 static void rk81x_dc_det_init(struct battery_info *di,
3613 struct device_node *np)
3615 struct device *dev = di->dev;
3616 struct rk818 *rk818 = di->rk818;
3617 enum of_gpio_flags flags;
3620 /*thers is resistance in the pcb to pull the pin up*/
3621 if (!di->dc_det_pullup_inside)
3624 /*there is no resistance in the pcb to pull the pin up.*/
3625 di->pinctrl = devm_pinctrl_get(rk818->dev);
3626 if (IS_ERR(di->pinctrl)) {
3627 dev_err(dev, "No pinctrl used!\n");
3631 /* lookup default state */
3632 di->pins_default = pinctrl_lookup_state(di->pinctrl, "default");
3633 if (IS_ERR(di->pins_default)) {
3634 dev_err(dev, "No default pinctrl found!\n");
3636 ret = pinctrl_select_state(di->pinctrl, di->pins_default);
3638 dev_err(dev, "Default pinctrl setting failed!\n");
3641 di->dc_det_pin = of_get_named_gpio_flags(np,
3642 "dc_det_gpio", 0, &flags);
3643 if (di->dc_det_pin == -EPROBE_DEFER)
3644 dev_err(dev, "dc_det_gpio error\n");
3645 if (gpio_is_valid(di->dc_det_pin))
3647 (flags & OF_GPIO_ACTIVE_LOW) ?
3648 RK818_DC_IN : RK818_DC_OUT;
3654 static int rk81x_battery_parse_dt(struct battery_info *di)
3656 struct device_node *regs, *rk818_pmic_np, *test_np;
3657 struct battery_platform_data *data;
3658 struct cell_config *cell_cfg;
3659 struct ocv_config *ocv_cfg;
3660 struct property *prop;
3661 struct rk818 *rk818 = di->rk818;
3662 struct device *dev = di->dev;
3666 rk818_pmic_np = of_node_get(rk818->dev->of_node);
3667 if (!rk818_pmic_np) {
3668 dev_err(dev, "could not find pmic sub-node\n");
3672 regs = of_find_node_by_name(rk818_pmic_np, "battery");
3674 dev_err(dev, "battery node not found!\n");
3678 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3680 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3684 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3686 dev_err(dev, "kzalloc for cell_config failed!\n");
3689 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3691 dev_err(dev, "kzalloc for ocv_config failed!\n");
3695 prop = of_find_property(regs, "ocv_table", &length);
3697 dev_err(dev, "ocv_table not found!\n");
3700 data->ocv_size = length / sizeof(u32);
3702 if (data->ocv_size > 0) {
3703 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3705 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3706 if (!data->battery_ocv) {
3707 dev_err(dev, "kzalloc for ocv_table failed!\n");
3710 ret = of_property_read_u32_array(regs, "ocv_table",
3717 /******************** charger param ****************************/
3718 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3720 dev_err(dev, "max_charge_currentmA not found!\n");
3721 out_value = DEFAULT_ICUR;
3723 data->max_charger_currentmA = out_value;
3725 ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3727 dev_err(dev, "max_charger_ilimitmA not found!\n");
3728 out_value = DEFAULT_ILMT;
3730 data->max_charger_ilimitmA = out_value;
3732 ret = of_property_read_u32(regs, "bat_res", &out_value);
3734 dev_err(dev, "bat_res not found!\n");
3735 out_value = DEFAULT_BAT_RES;
3737 data->sense_resistor_mohm = out_value;
3739 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3741 dev_err(dev, "max_charge_voltagemV not found!\n");
3742 out_value = DEFAULT_VLMT;
3744 data->max_charger_voltagemV = out_value;
3746 ret = of_property_read_u32(regs, "design_capacity", &out_value);
3748 dev_err(dev, "design_capacity not found!\n");
3751 cell_cfg->design_capacity = out_value;
3753 ret = of_property_read_u32(regs, "design_qmax", &out_value);
3755 dev_err(dev, "design_qmax not found!\n");
3758 cell_cfg->design_qmax = out_value;
3760 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3762 dev_err(dev, "sleep_enter_current not found!\n");
3765 ocv_cfg->sleep_enter_current = out_value;
3767 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3769 dev_err(dev, "sleep_exit_current not found!\n");
3772 ocv_cfg->sleep_exit_current = out_value;
3774 /******************** test power param ****************************/
3775 test_np = of_find_node_by_name(regs, "test_power");
3777 dev_err(dev, "test-power node not found!\n");
3778 di->test_chrg_current = DEF_TEST_CURRENT_MA;
3779 di->test_chrg_ilmt = DEF_TEST_ILMT_MA;
3781 ret = of_property_read_u32(test_np, "test_charge_currentmA",
3784 dev_err(dev, "test_charge_currentmA not found!\n");
3785 out_value = DEF_TEST_CURRENT_MA;
3787 di->test_chrg_current = out_value;
3789 ret = of_property_read_u32(test_np, "test_charge_ilimitmA",
3792 dev_err(dev, "test_charge_ilimitmA not found!\n");
3793 out_value = DEF_TEST_ILMT_MA;
3795 di->test_chrg_ilmt = out_value;
3798 /************* charger support adp types **********************/
3799 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3800 ret = of_property_read_u32(regs, "support_usb_adp", &support_usb_adp);
3801 ret = of_property_read_u32(regs, "support_dc_adp", &support_dc_adp);
3802 ret = of_property_read_u32(regs, "dc_det_pullup_inside", &out_value);
3805 di->dc_det_pullup_inside = out_value;
3807 if (!support_usb_adp && !support_dc_adp) {
3808 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
3809 support_usb_adp = 1;
3813 rk81x_dc_det_init(di, regs);
3815 cell_cfg->ocv = ocv_cfg;
3816 data->cell_cfg = cell_cfg;
3817 rk818->battery_data = data;
3819 DBG("\n--------- the battery OCV TABLE dump:\n");
3820 DBG("bat_res :%d\n", data->sense_resistor_mohm);
3821 DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3822 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3823 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3824 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3825 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3826 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3827 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3828 DBG("support_uboot_chrg = %d\n", support_uboot_chrg);
3829 DBG("support_usb_adp = %d\n", support_usb_adp);
3830 DBG("support_dc_adp= %d\n", support_dc_adp);
3831 DBG("test_charge_currentmA = %d\n", di->test_chrg_current);
3832 DBG("test_charge_ilimitmA = %d\n", di->test_chrg_ilmt);
3833 DBG("dc_det_pullup_inside = %d\n", di->dc_det_pullup_inside);
3834 DBG("--------- rk818_battery dt_parse ok.\n");
3839 static int rk81x_battery_parse_dt(struct battery_info *di)
3846 static int rk81x_battery_probe(struct platform_device *pdev)
3848 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3849 struct battery_info *di;
3852 DBG("battery driver version %s\n", DRIVER_VERSION);
3853 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3855 dev_err(&pdev->dev, "kzalloc di failed!\n");
3859 di->dev = &pdev->dev;
3860 platform_set_drvdata(pdev, di);
3862 ret = rk81x_battery_parse_dt(di);
3864 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
3867 rk81x_battery_info_init(di, chip);
3868 if (!is_rk81x_bat_exist(di)) {
3869 pr_info("not find Li-ion battery, test power mode\n");
3870 rk81x_battery_charger_init(di);
3871 di->fg_drv_mode = TEST_POWER_MODE;
3874 battery_power_supply_init(di);
3875 ret = battery_power_supply_register(di);
3877 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
3881 rk81x_battery_irq_init(di);
3882 rk81x_battery_sysfs_init(di, &pdev->dev);
3885 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
3887 flatzone_voltage_init(di);
3888 rk81x_check_battery_status(di);
3890 di->wq = create_singlethread_workqueue("rk81x-battery-work");
3891 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
3892 queue_delayed_work(di->wq, &di->battery_monitor_work,
3893 msecs_to_jiffies(TIMER_MS_COUNTS*5));
3894 INIT_DELAYED_WORK(&di->charge_check_work,
3895 rk81x_battery_charge_check_work);
3896 di->battery_nb.notifier_call = battery_notifier_call;
3897 register_battery_notifier(&di->battery_nb);
3899 DBG("rk81x battery probe ok!\n");
3907 static int rk81x_battery_suspend(struct platform_device *dev,
3910 struct battery_info *di = platform_get_drvdata(dev);
3912 enable_vbat_low_irq(di);
3913 di->sleep_status = di->status;
3915 /* avoid abrupt wakeup which will clean the variable*/
3916 if (di->sys_wakeup) {
3917 di->suspend_cap = di->remain_capacity;
3918 di->suspend_rsoc = _get_soc(di);
3919 di->suspend_time_start = get_seconds();
3920 di->sys_wakeup = false;
3923 pr_info("rk81x-battery suspend: v=%d ld=%d lr=%d c=%d chg=%d\n",
3924 _get_battery_voltage(di), di->real_soc, _get_soc(di),
3925 _get_average_current(di), di->status);
3927 cancel_delayed_work(&di->battery_monitor_work);
3932 static int rk81x_battery_resume(struct platform_device *dev)
3934 struct battery_info *di = platform_get_drvdata(dev);
3937 disable_vbat_low_irq(di);
3938 queue_delayed_work(di->wq, &di->battery_monitor_work,
3939 msecs_to_jiffies(TIMER_MS_COUNTS/2));
3941 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3943 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3945 pr_info("rk81x-battery resume: v=%d rv=%d ld=%d lr=%d c=%d chg=%d\n",
3946 _get_battery_voltage(di), get_relax_voltage(di),
3947 di->real_soc, _get_soc(di), _get_average_current(di),
3951 static int rk81x_battery_remove(struct platform_device *dev)
3953 struct battery_info *di = platform_get_drvdata(dev);
3955 cancel_delayed_work_sync(&di->battery_monitor_work);
3958 static void rk81x_battery_shutdown(struct platform_device *dev)
3960 struct battery_info *di = platform_get_drvdata(dev);
3962 cancel_delayed_work_sync(&di->battery_monitor_work);
3963 if (BASE_TO_MIN(di->sys_on_base) <= REBOOT_INTER_MIN)
3964 rk81x_check_reboot(di);
3966 copy_reboot_cnt(di, 0);
3967 DBG("rk818 shutdown!");
3971 static struct platform_driver rk81x_battery_driver = {
3973 .name = "rk818-battery",
3974 .owner = THIS_MODULE,
3977 .probe = rk81x_battery_probe,
3978 .remove = rk81x_battery_remove,
3979 .suspend = rk81x_battery_suspend,
3980 .resume = rk81x_battery_resume,
3981 .shutdown = rk81x_battery_shutdown,
3984 static int __init battery_init(void)
3986 return platform_driver_register(&rk81x_battery_driver);
3989 fs_initcall_sync(battery_init);
3990 static void __exit battery_exit(void)
3992 platform_driver_unregister(&rk81x_battery_driver);
3994 module_exit(battery_exit);
3996 MODULE_LICENSE("GPL");
3997 MODULE_ALIAS("platform:rk818-battery");
3998 MODULE_AUTHOR("ROCKCHIP");