2 * rk818/rk819 battery driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd
5 * Author: zhangqing <zhangqing@rock-chips.com>
6 * chenjh <chenjh@rock-chips.com>
7 * Andy Yan <andy.yan@rock-chips.com>
9 * Copyright (C) 2008-2009 Texas Instruments, Inc.
10 * Author: Texas Instruments, Inc.
12 * Copyright (C) 2008-2009 Texas Instruments, Inc.
13 * Author: Texas Instruments, Inc.
14 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
15 * Author: zhangqing <zhangqing@rock-chips.com>
16 * Copyright (C) 2014-2015 Intel Mobile Communications GmbH
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
40 #include <linux/gpio.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
43 #include <linux/mfd/rk818.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/rtc.h>
47 #include <linux/wakelock.h>
48 #include <linux/of_gpio.h>
49 #include <linux/irq.h>
50 #include <linux/irqdomain.h>
51 #include <linux/usb/phy.h>
54 #if defined(CONFIG_X86_INTEL_SOFIA)
55 #include <linux/usb/phy-intel.h>
57 #include "rk818_battery.h"
59 /* if you want to disable, don't set it as 0,
60 just be: "static int dbg_enable;" is ok*/
62 static int dbg_enable;
63 #define RK818_SYS_DBG 1
65 module_param_named(dbg_level, dbg_enable, int, 0644);
67 #define DBG(args...) \
74 #define DEFAULT_BAT_RES 135
75 #define DEFAULT_CHRG_VOL 4200
76 #define DEFAULT_CHRG_CUR 1000
77 #define DEFAULT_INPUT_CUR 1400
78 #define DEFAULT_SLP_ENTER_CUR 600
79 #define DEFAULT_SLP_EXIT_CUR 600
81 #define DSOC_DISCHRG_EMU_CURR 1200
82 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
83 #define DSOC_DISCHRG_FAST_EER_RANGE 10
84 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
85 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
86 #define DSOC_CHRG_FAST_EER_RANGE 10
87 #define DSOC_CHRG_EMU_CURR 1200
88 #define DSOC_CHRG_TERM_CURR 600
89 #define DSOC_CHRG_TERM_VOL 4100
90 #define CHRG_FINISH_VOL 4100
92 /*realtime RSOC calib param*/
93 #define RSOC_DISCHRG_ERR_LOWER 40
94 #define RSOC_DISCHRG_ERR_UPPER 50
95 #define RSOC_ERR_CHCK_CNT 15
96 #define RSOC_COMPS 20 /*compensation*/
97 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
98 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
100 #define RSOC_RESUME_ERR 10
101 #define REBOOT_INTER_MIN 1
103 #define INTERPOLATE_MAX 1000
104 #define MAX_INT 0x7FFF
105 #define TIME_10MIN_SEC 600
107 #define CHRG_VOL_SHIFT 4
108 #define CHRG_ILIM_SHIFT 0
109 #define CHRG_ICUR_SHIFT 0
110 #define DEF_CHRG_VOL CHRG_VOL4200
111 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
112 #define DEF_CHRG_CURR_LMT ILIM_2000MA
114 /*TEST_POWER_MODE params*/
115 #define TEST_CURRENT 1000
116 #define TEST_VOLTAGE 3800
118 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
119 #define TEST_PRESET 1
120 #define TEST_AC_ONLINE 1
121 #define TEST_USB_ONLINE 0
123 #define ZERO_ALGOR_THRESD 3800
124 #define DISCHRG_ZERO_MODE 1
125 #define DISCHRG_NORMAL_MODE 0
126 #define DEF_LAST_ZERO_MODE_SOC -1
128 #define DISCHRG_MODE 0
131 #define TREND_STAT_FLAT 0
132 #define TREND_STAT_DOWN -1
133 #define TREND_STAT_UP 1
134 #define TREND_CAP_DIFF 5
138 #define SLP_CURR_MAX 40
139 #define SLP_CURR_MIN 6
140 #define WAKEUP_SEC_THRESD 40
141 #define CHRG_TIME_STEP (60)
142 #define DISCHRG_TIME_STEP_0 (30 * 60)
143 #define DISCHRG_TIME_STEP_1 (60 * 60)
145 #define DEF_PCB_OFFSET 42
146 #define DEF_CAL_OFFSET 0x832
147 #define DEF_PWRPATH_RES 50
148 #define SEC_TO_EMPTY 300
149 #define DSOC_CHRG_FINISH_CURR 1100
150 #define SLP_CHRG_CURR 1000
151 #define SLP_DSOC_VOL_THRESD 3600
152 #define PWR_OFF_THRESD 3400
153 #define MIN_ZERO_ACCURACY 10 /*0.01%*/
155 #define MAX_FCC 10000
158 * the following table value depends on datasheet
160 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
162 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
163 2250, 2400, 2600, 2800, 3000};
165 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
166 2000, 2250, 2500, 2750, 3000};
168 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
170 #define RK818_DC_IN 0
171 #define RK818_DC_OUT 1
173 #define OCV_VALID_SHIFT (0)
174 #define OCV_CALIB_SHIFT (1)
175 #define FIRST_PWRON_SHIFT (2)
176 #define LOADER_CHRG_SHIFT (3)
178 #define SEC_TO_MIN(x) ((x) / 60)
180 struct rk81x_battery {
182 struct cell_state cell;
183 struct power_supply bat;
184 struct power_supply ac;
185 struct power_supply usb;
186 struct delayed_work work;
188 struct pinctrl *pinctrl;
189 struct pinctrl_state *pins_default;
191 struct battery_platform_data *pdata;
204 uint16_t voltage_ocv;
205 uint16_t relax_voltage;
211 bool pcb_ioffset_updated;
228 int bat_res_update_cnt;
234 unsigned long dod0_time;
247 int current_k;/* (ICALIB0, ICALIB1) */
250 int voltage_k;/* VCALIB0 VCALIB1 */
253 int zero_timeout_cnt;
254 int zero_old_remain_cap;
259 int charge_smooth_time;
263 unsigned long suspend_time_sum;
267 int suspend_charge_current;
270 bool charge_smooth_status;
271 bool discharge_smooth_status;
277 struct notifier_block battery_nb;
278 struct usb_phy *usb_phy;
279 struct notifier_block usb_nb;
280 struct notifier_block fb_nb;
283 int s2r; /*suspend to resume*/
284 struct workqueue_struct *wq;
285 struct delayed_work battery_monitor_work;
286 struct delayed_work charge_check_work;
287 struct delayed_work usb_phy_delay_work;
288 struct delayed_work chrg_term_mode_switch_work;
292 struct wake_lock resume_wake_lock;
293 unsigned long plug_in_base;
294 unsigned long plug_out_base;
295 unsigned long finish_sig_base;
296 unsigned long power_on_base;
301 bool is_first_poweron;
304 int debug_finish_real_soc;
305 int debug_finish_temp_soc;
311 unsigned long wakeup_sec;
312 u32 delta_vol_smooth;
313 unsigned long dischrg_normal_base;
314 unsigned long dischrg_emu_base;
315 unsigned long chrg_normal_base;
316 unsigned long chrg_term_base;
317 unsigned long chrg_emu_base;
318 unsigned long chrg_finish_base;
319 unsigned long fcc_update_sec;
321 u8 dischrg_algorithm_mode;
322 int last_zero_mode_dsoc;
324 unsigned long dischrg_save_sec;
325 unsigned long chrg_save_sec;
326 struct timeval suspend_rtc_base;
329 u32 support_usb_adp, support_dc_adp;
331 #define to_device_info(x) container_of((x), \
332 struct rk81x_battery, bat)
334 #define to_ac_device_info(x) container_of((x), \
335 struct rk81x_battery, ac)
337 #define to_usb_device_info(x) container_of((x), \
338 struct rk81x_battery, usb)
340 static int loader_charged;
342 static int __init rk81x_bat_loader_charged(char *__unused)
346 pr_info("battery charged in loader\n");
350 __setup("loader_charged", rk81x_bat_loader_charged);
352 static u64 g_base_sec;
353 static u64 get_runtime_sec(void)
355 u64 ts_ns = local_clock();
357 do_div(ts_ns, 1000000000);
359 return ts_ns + g_base_sec;
362 static u64 is_local_clock_reset(void)
364 u64 ts_ns = local_clock();
366 do_div(ts_ns, 1000000000);
371 static inline unsigned long BASE_TO_SEC(unsigned long x)
374 return get_runtime_sec() - x;
379 static inline unsigned long BASE_TO_MIN(unsigned long x)
381 return BASE_TO_SEC(x) / 60;
384 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
389 case HW_ADP_TYPE_USB:
397 case HW_ADP_TYPE_DUAL:
398 if (support_usb_adp && support_dc_adp)
408 static bool rk81x_chrg_online(struct rk81x_battery *di)
410 return di->usb_online || di->ac_online;
413 static u32 interpolate(int value, u32 *table, int size)
418 for (i = 0; i < size; i++) {
419 if (value < table[i])
423 if ((i > 0) && (i < size)) {
424 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
425 d /= table[i] - table[i-1];
426 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
428 d = i * ((INTERPOLATE_MAX + size / 2) / size);
437 /* Returns (a * b) / c */
438 static int32_t ab_div_c(u32 a, u32 b, u32 c)
444 sign = ((((a^b)^c) & 0x80000000) != 0);
450 tmp = (a * b + (c >> 1)) / c;
462 static int div(int val)
464 return (val == 0) ? 1 : val;
467 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
468 u8 buf[], unsigned len)
473 for (i = 0; ret < 0 && i < 3; i++) {
474 ret = rk818_i2c_read(di->rk818, reg, len, buf);
476 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
479 return (ret < 0) ? ret : 0;
482 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
483 u8 const buf[], unsigned len)
488 for (i = 0; ret < 0 && i < 3; i++) {
489 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
491 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
494 return (ret < 0) ? ret : 0;
497 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
502 for (i = 0; ret < 0 && i < 3; i++) {
503 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
505 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
511 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
516 for (i = 0; ret < 0 && i < 3; i++) {
517 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
519 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
525 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
530 rk81x_bat_read(di, reg, &buf, 1);
531 val = (buf & BIT(shift)) >> shift;
535 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
540 DBG("%s dump charger register start:\n", __func__);
541 for (i = 0xAC; i < 0xEE; i++) {
542 rk81x_bat_read(di, i, &buf, 1);
543 DBG("0x%02x : 0x%02x\n", i, buf);
548 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
553 DBG("%s dump the register start:\n", __func__);
554 for (i = 0x99; i < 0xAB; i++) {
555 rk81x_bat_read(di, i, &buf, 1);
556 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
561 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
563 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
564 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
567 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
569 rk81x_bat_reset_zero_var(di);
570 di->trend_start_cap = di->remain_capacity;
573 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
579 delta_cap = capacity - di->remain_capacity;
583 di->adjust_cap += delta_cap;
585 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
587 buf = (capacity_ma >> 24) & 0xff;
588 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
589 buf = (capacity_ma >> 16) & 0xff;
590 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
591 buf = (capacity_ma >> 8) & 0xff;
592 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
593 buf = (capacity_ma & 0xff) | 0x01;
594 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
595 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
599 dev_info(di->dev, "update capacity :%d--remain_cap:%d\n",
600 capacity, di->remain_capacity);
605 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
607 static ssize_t bat_calib_read(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct power_supply *psy_bat = dev_get_drvdata(dev);
611 struct rk81x_battery *di = to_device_info(psy_bat);
614 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
616 return sprintf(buf, "%d\n", val);
619 static ssize_t bat_calib_write(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf, size_t count)
625 struct power_supply *psy_bat = dev_get_drvdata(dev);
626 struct rk81x_battery *di = to_device_info(psy_bat);
628 ret = kstrtou8(buf, 0, &val);
633 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
635 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
640 * interface for debug: force battery to over discharge
642 static ssize_t bat_test_power_read(struct device *dev,
643 struct device_attribute *attr, char *buf)
645 struct power_supply *psy_bat = dev_get_drvdata(dev);
646 struct rk81x_battery *di = to_device_info(psy_bat);
648 return sprintf(buf, "%d\n", di->fg_drv_mode);
651 static ssize_t bat_test_power_write(struct device *dev,
652 struct device_attribute *attr,
653 const char *buf, size_t count)
657 struct power_supply *psy_bat = dev_get_drvdata(dev);
658 struct rk81x_battery *di = to_device_info(psy_bat);
660 ret = kstrtou8(buf, 0, &val);
665 di->fg_drv_mode = TEST_POWER_MODE;
667 di->fg_drv_mode = FG_NORMAL_MODE;
672 static ssize_t bat_fcc_read(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct power_supply *psy_bat = dev_get_drvdata(dev);
676 struct rk81x_battery *di = to_device_info(psy_bat);
678 return sprintf(buf, "%d\n", di->fcc);
681 static ssize_t bat_fcc_write(struct device *dev,
682 struct device_attribute *attr,
683 const char *buf, size_t count)
687 struct power_supply *psy_bat = dev_get_drvdata(dev);
688 struct rk81x_battery *di = to_device_info(psy_bat);
690 ret = kstrtou16(buf, 0, &val);
699 static ssize_t bat_dsoc_read(struct device *dev,
700 struct device_attribute *attr, char *buf)
702 struct power_supply *psy_bat = dev_get_drvdata(dev);
703 struct rk81x_battery *di = to_device_info(psy_bat);
705 return sprintf(buf, "%d\n", di->dsoc);
708 static ssize_t bat_dsoc_write(struct device *dev,
709 struct device_attribute *attr,
710 const char *buf, size_t count)
714 struct power_supply *psy_bat = dev_get_drvdata(dev);
715 struct rk81x_battery *di = to_device_info(psy_bat);
717 ret = kstrtou8(buf, 0, &val);
726 static ssize_t bat_rsoc_read(struct device *dev,
727 struct device_attribute *attr, char *buf)
729 struct power_supply *psy_bat = dev_get_drvdata(dev);
730 struct rk81x_battery *di = to_device_info(psy_bat);
732 return sprintf(buf, "%d\n", di->rsoc);
735 static ssize_t bat_rsoc_write(struct device *dev,
736 struct device_attribute *attr,
737 const char *buf, size_t count)
742 struct power_supply *psy_bat = dev_get_drvdata(dev);
743 struct rk81x_battery *di = to_device_info(psy_bat);
745 ret = kstrtou8(buf, 0, &val);
749 capacity = di->fcc * val / 100;
750 rk81x_bat_capacity_init(di, capacity);
751 rk81x_bat_capacity_init_post(di);
756 static ssize_t bat_remain_cap_read(struct device *dev,
757 struct device_attribute *attr,
760 struct power_supply *psy_bat = dev_get_drvdata(dev);
761 struct rk81x_battery *di = to_device_info(psy_bat);
763 return sprintf(buf, "%d\n", di->remain_capacity);
766 static struct device_attribute rk818_bat_attr[] = {
767 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
768 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
769 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
770 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
771 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
772 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
776 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
781 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
783 dev_err(di->dev, "error reading TS_CTRL_REG");
788 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
793 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
795 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
798 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
802 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
807 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
813 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
815 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
818 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
822 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
828 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
830 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
833 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
837 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
843 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
845 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
851 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
857 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
859 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
865 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
871 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
872 buf = (value >> 8) & 0xff;
873 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
878 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
880 int vcalib0, vcalib1;
882 vcalib0 = rk81x_bat_get_vcalib0(di);
883 vcalib1 = rk81x_bat_get_vcalib1(di);
885 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
886 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
887 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
890 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
895 uint16_t voltage_now = 0;
899 for (i = 0; i < 3; i++) {
900 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
902 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
906 dev_err(di->dev, "error read BAT_OCV_REGH");
911 if (val[0] == val[1])
916 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
921 static int rk81x_bat_get_vol(struct rk81x_battery *di)
930 for (i = 0; i < 3; i++) {
931 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
933 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
937 dev_err(di->dev, "error read BAT_VOL_REGH");
942 if (val[0] == val[1])
947 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
952 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
957 ret = rk81x_bat_read(di, GGSTS, &status, 1);
959 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
965 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
969 uint16_t temp = 0, voltage_now;
971 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
973 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
976 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
981 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
985 uint16_t temp = 0, voltage_now;
987 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
989 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
992 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
997 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1001 uint16_t relax_vol1, relax_vol2;
1004 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1005 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1007 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1008 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1009 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1010 __func__, status, ggcon, relax_vol1, relax_vol2);
1012 if (is_rk81x_bat_relax_mode(di))
1013 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1019 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1020 * based on the voltage.
1022 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1029 ocv_table = di->pdata->battery_ocv;
1030 ocv_size = di->pdata->ocv_size;
1031 tmp = interpolate(voltage, ocv_table, ocv_size);
1032 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1033 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1038 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1044 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1046 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1050 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1052 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1058 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1068 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1072 rk81x_bat_read(di, GGCON, &ggcon, 1);
1073 ggcon &= ~(0x30); /*clear <5:4>*/
1075 rk81x_bat_write(di, GGCON, &ggcon, 1);
1079 * when charger finish signal comes, we need calibrate the current, make it
1082 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1092 if ((di->chrg_status == CHARGE_FINISH) &&
1093 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1094 (abs(di->current_avg) > 4)) {
1095 for (retry = 0; retry < 5; retry++) {
1096 adc_value = rk81x_bat_get_raw_adc_current(di);
1097 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1098 dev_dbg(di->dev, "charger plugout\n");
1103 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1104 C0 = rk81x_bat_get_cal_offset(di);
1105 C1 = adc_value + C0;
1106 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1108 rk81x_bat_set_cal_offset(di, C1);
1109 DBG("<%s>. new cal_offset = %d\n",
1110 __func__, rk81x_bat_get_cal_offset(di));
1112 adc_value = rk81x_bat_get_raw_adc_current(di);
1113 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1114 if (abs(adc_value) < 4) {
1115 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1116 ioffset = rk81x_bat_get_ioffset(di);
1117 rk81x_bat_set_cal_offset(di,
1120 ioffset = rk81x_bat_get_ioffset(di);
1121 pcb_offset = C1 - ioffset;
1122 di->pcb_ioffset = pcb_offset;
1123 di->pcb_ioffset_updated = true;
1128 DBG("<%s>. update the cal_offset, C1 = %d\n"
1129 "i_offset = %d, pcb_offset = %d\n",
1130 __func__, C1, ioffset, pcb_offset);
1134 dev_warn(di->dev, "ioffset cal failed\n");
1135 rk81x_bat_set_cal_offset(di, C0);
1138 di->pcb_ioffset_updated = false;
1145 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1148 int enter_thres, exit_thres;
1149 struct cell_state *cell = &di->cell;
1151 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1152 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1153 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1154 __func__, cell->config->ocv->sleep_enter_current,
1155 cell->config->ocv->sleep_exit_current);
1157 buf = enter_thres & 0xff;
1158 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1159 buf = (enter_thres >> 8) & 0xff;
1160 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1162 buf = exit_thres & 0xff;
1163 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1164 buf = (exit_thres >> 8) & 0xff;
1165 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1167 /* set sample time */
1168 rk81x_bat_read(di, GGCON, &buf, 1);
1169 buf &= ~(3 << 2);/*8min*/
1170 buf &= ~0x01; /* clear bat_res calc*/
1171 rk81x_bat_write(di, GGCON, &buf, 1);
1174 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1179 rk81x_bat_read(di, GGCON, &ggcon, 1);
1181 rk81x_bat_write(di, GGCON, &ggcon, 1);
1183 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1185 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1188 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1197 for (i = 0; i < 3; i++) {
1198 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1200 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1205 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1207 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1213 if (val[0] == val[1])
1214 current_now = val[0];
1216 current_now = val[2];
1218 if (current_now & 0x800)
1219 current_now -= 4096;
1221 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1226 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1227 enum charger_type charger_type)
1229 di->usb_online = OFFLINE;
1230 di->ac_online = OFFLINE;
1232 switch (charger_type) {
1234 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1237 di->usb_online = ONLINE;
1238 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1240 case DC_CHARGER:/*treat dc as ac*/
1242 di->ac_online = ONLINE;
1243 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1246 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1250 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1251 msecs_to_jiffies(1000));
1254 /* high load: current < 0 with charger in.
1255 * System will not shutdown while dsoc=0% with charging state(ac_online),
1256 * which will cause over discharge, so oppose status before report states.
1258 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1261 int pwr_off_thresd = di->pdata->power_off_thresd - 50;
1263 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1265 time = get_runtime_sec();
1267 if (BASE_TO_SEC(time) > (MINUTE)) {
1268 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1269 dev_info(di->dev, "low power....\n");
1276 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1280 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1282 return (buf & 0x80) ? 1 : 0;
1285 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1290 rk81x_bat_read(di, GGSTS, &buf, 1);
1291 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1292 /*di->pwron_bat_con = buf;*/
1296 rk81x_bat_write(di, GGSTS, &buf, 1);
1297 rk81x_bat_read(di, GGSTS, &temp, 1);
1298 } while (temp & BAT_CON);
1305 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1312 ocv_table = di->pdata->battery_ocv;
1313 ocv_size = di->pdata->ocv_size;
1315 for (j = 0; j < 21; j++)
1319 for (i = 1; i < ocv_size-1; i++) {
1320 if (ocv_table[i+1] < ocv_table[i] + 20)
1321 temp_table[j++] = i;
1324 temp_table[j] = temp_table[j-1] + 1;
1326 di->enter_flatzone = ocv_table[i];
1329 for (i = 0; i < 20; i++) {
1330 if (temp_table[i] < temp_table[i+1])
1335 di->exit_flatzone = ocv_table[i];
1337 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1338 di->enter_flatzone, di->exit_flatzone);
1341 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1343 u8 ocv_valid, first_pwron;
1347 /*buf==1: OCV_VOL is valid*/
1348 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1349 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1350 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1352 if (first_pwron == 1 || ocv_valid == 1) {
1353 DBG("<%s> enter.\n", __func__);
1354 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1355 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1356 di->dod0_voltage = ocv_voltage;
1357 di->dod0_capacity = di->temp_nac;
1362 di->dod0_level = 100;
1363 else if (ocv_soc < 5)
1364 di->dod0_level = 95;
1365 else if (ocv_soc < 10)
1366 di->dod0_level = 90;
1368 di->dod0_level = 80;
1369 /* save_soc = di->dod0_level; */
1370 soc_level = rk81x_bat_get_level(di);
1371 if (soc_level > di->dod0_level) {
1372 di->dod0_status = 0;
1374 if (soc_level <= 80)
1376 rk81x_bat_save_level(di, soc_level);
1378 di->dod0_status = 1;
1380 di->fcc_update_sec = get_runtime_sec();
1383 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1384 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1386 di->dod0_voltage, di->dod0_capacity,
1387 ocv_soc, soc_level, di->dod0_status,
1393 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1395 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1398 static enum power_supply_property rk_battery_props[] = {
1399 POWER_SUPPLY_PROP_STATUS,
1400 POWER_SUPPLY_PROP_CURRENT_NOW,
1401 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1402 POWER_SUPPLY_PROP_PRESENT,
1403 POWER_SUPPLY_PROP_HEALTH,
1404 POWER_SUPPLY_PROP_CAPACITY,
1407 static int rk81x_battery_get_property(struct power_supply *psy,
1408 enum power_supply_property psp,
1409 union power_supply_propval *val)
1411 struct rk81x_battery *di = to_device_info(psy);
1414 case POWER_SUPPLY_PROP_CURRENT_NOW:
1415 val->intval = di->current_avg * 1000;/*uA*/
1416 if (di->fg_drv_mode == TEST_POWER_MODE)
1417 val->intval = TEST_CURRENT * 1000;
1419 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1420 val->intval = di->voltage * 1000;/*uV*/
1421 if (di->fg_drv_mode == TEST_POWER_MODE)
1422 val->intval = TEST_VOLTAGE * 1000;
1425 case POWER_SUPPLY_PROP_PRESENT:
1426 val->intval = is_rk81x_bat_exist(di);
1427 if (di->fg_drv_mode == TEST_POWER_MODE)
1428 val->intval = TEST_PRESET;
1431 case POWER_SUPPLY_PROP_CAPACITY:
1432 val->intval = di->dsoc;
1433 if (di->fg_drv_mode == TEST_POWER_MODE)
1434 val->intval = TEST_SOC;
1436 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1438 case POWER_SUPPLY_PROP_HEALTH:
1439 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1441 case POWER_SUPPLY_PROP_STATUS:
1442 val->intval = di->psy_status;
1443 if (di->fg_drv_mode == TEST_POWER_MODE)
1444 val->intval = TEST_STATUS;
1454 static enum power_supply_property rk_battery_ac_props[] = {
1455 POWER_SUPPLY_PROP_ONLINE,
1458 static enum power_supply_property rk_battery_usb_props[] = {
1459 POWER_SUPPLY_PROP_ONLINE,
1462 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1463 enum power_supply_property psp,
1464 union power_supply_propval *val)
1467 struct rk81x_battery *di = to_ac_device_info(psy);
1470 case POWER_SUPPLY_PROP_ONLINE:
1471 if (rk81x_chrg_online(di))
1472 rk81x_bat_lowpwr_check(di);
1473 val->intval = di->ac_online; /*discharging*/
1474 if (di->fg_drv_mode == TEST_POWER_MODE)
1475 val->intval = TEST_AC_ONLINE;
1485 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1486 enum power_supply_property psp,
1487 union power_supply_propval *val)
1490 struct rk81x_battery *di = to_usb_device_info(psy);
1493 case POWER_SUPPLY_PROP_ONLINE:
1494 if (rk81x_chrg_online(di))
1495 rk81x_bat_lowpwr_check(di);
1496 val->intval = di->usb_online;
1497 if (di->fg_drv_mode == TEST_POWER_MODE)
1498 val->intval = TEST_USB_ONLINE;
1508 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1512 di->bat.name = "BATTERY";
1513 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1514 di->bat.properties = rk_battery_props;
1515 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1516 di->bat.get_property = rk81x_battery_get_property;
1519 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1520 di->ac.properties = rk_battery_ac_props;
1521 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1522 di->ac.get_property = rk81x_battery_ac_get_property;
1524 di->usb.name = "USB";
1525 di->usb.type = POWER_SUPPLY_TYPE_USB;
1526 di->usb.properties = rk_battery_usb_props;
1527 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1528 di->usb.get_property = rk81x_battery_usb_get_property;
1530 ret = power_supply_register(di->dev, &di->bat);
1532 dev_err(di->dev, "failed to register main battery\n");
1535 ret = power_supply_register(di->dev, &di->usb);
1537 dev_err(di->dev, "failed to register usb power supply\n");
1540 ret = power_supply_register(di->dev, &di->ac);
1542 dev_err(di->dev, "failed to register ac power supply\n");
1549 power_supply_unregister(&di->ac);
1551 power_supply_unregister(&di->usb);
1553 power_supply_unregister(&di->bat);
1558 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1562 static u32 capacity_ma;
1564 if (capacity >= di->qmax)
1565 capacity = di->qmax;
1570 if (capacity_ma == capacity)
1573 capacity_ma = capacity;
1575 buf = (capacity_ma >> 24) & 0xff;
1576 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1577 buf = (capacity_ma >> 16) & 0xff;
1578 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1579 buf = (capacity_ma >> 8) & 0xff;
1580 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1581 buf = (capacity_ma & 0xff) | 0x01;
1582 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1585 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1593 for (i = 0; i < 3; i++) {
1594 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1596 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1597 val[i] |= buf << 16;
1598 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1600 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1604 if (val[0] == val[1])
1612 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1617 capacity_ma = capacity;
1618 buf = (capacity_ma >> 24) & 0xff;
1619 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1620 buf = (capacity_ma >> 16) & 0xff;
1621 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1622 buf = (capacity_ma >> 8) & 0xff;
1623 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1624 buf = (capacity_ma & 0xff) | 0x01;
1625 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1627 dev_info(di->dev, "update fcc : %d\n", capacity);
1630 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1635 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1636 capacity = buf << 24;
1637 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1638 capacity |= buf << 16;
1639 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1640 capacity |= buf << 8;
1641 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1644 if (capacity < MIN_FCC) {
1645 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1647 capacity = di->design_capacity;
1648 rk81x_bat_save_fcc(di, capacity);
1649 } else if (capacity > di->qmax) {
1650 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1651 capacity = di->qmax;
1652 rk81x_bat_save_fcc(di, capacity);
1658 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1667 for (i = 0; i < 3; i++) {
1668 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1670 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1671 val[i] |= buf << 16;
1672 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1674 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1677 if (val[0] == val[1])
1682 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1687 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1691 if (last_soc != save_soc) {
1692 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1693 last_soc = save_soc;
1699 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1704 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1708 static u8 rk81x_bat_support_loader_chrg(struct rk81x_battery *di)
1712 ret = rk81x_bat_read_bit(di, MISC_MARK_REG, LOADER_CHRG_SHIFT);
1713 rk81x_bat_clr_bit(di, MISC_MARK_REG, LOADER_CHRG_SHIFT);
1717 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1721 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1722 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1723 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1724 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1727 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1728 enum charger_type charger_type)
1730 switch (charger_type) {
1733 rk81x_bat_set_current(di, ILIM_450MA);
1737 rk81x_bat_set_current(di, di->chrg_i_lmt);
1740 rk81x_bat_set_current(di, ILIM_450MA);
1744 #if defined(CONFIG_ARCH_ROCKCHIP)
1746 * There are three ways to detect dc_adp:
1747 * 1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1748 * do not care about whether define dc_det_pin or not;
1749 * 2. define de_det_pin: check gpio level;
1750 * 3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1751 * case that: gpio invalid or not define.
1753 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1755 enum charger_type charger_type;
1759 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1761 /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1762 if (!rk81x_bat_support_adp_type(HW_ADP_TYPE_USB)) {
1763 if ((buf & PLUG_IN_STS) != 0)
1764 charger_type = DC_CHARGER;
1766 charger_type = NO_CHARGER;
1768 return charger_type;
1771 /*det by gpio level*/
1772 if (gpio_is_valid(di->dc_det_pin)) {
1773 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1775 pr_err("Failed to request gpio %d with ret:""%d\n",
1776 di->dc_det_pin, ret);
1780 gpio_direction_input(di->dc_det_pin);
1781 ret = gpio_get_value(di->dc_det_pin);
1782 if (ret == di->dc_det_level)
1783 charger_type = DC_CHARGER;
1785 charger_type = NO_CHARGER;
1787 gpio_free(di->dc_det_pin);
1788 DBG("**********rk818 dc_det_pin=%d\n", ret);
1790 return charger_type;
1792 /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1793 } else if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DUAL)) {
1794 if ((buf & PLUG_IN_STS) != 0) {
1795 charger_type = dwc_otg_check_dpdm();
1796 if (charger_type == 0)
1797 charger_type = DC_CHARGER;
1799 charger_type = NO_CHARGER;
1803 return charger_type;
1806 static enum charger_type rk81x_bat_get_usbac_state(struct rk81x_battery *di)
1808 enum charger_type charger_type;
1809 int usb_id, gadget_flag;
1811 usb_id = dwc_otg_check_dpdm();
1814 charger_type = NO_CHARGER;
1818 charger_type = USB_CHARGER;
1821 charger_type = AC_CHARGER;
1824 charger_type = NO_CHARGER;
1827 DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1828 if (charger_type == USB_CHARGER) {
1829 gadget_flag = get_gadget_connect_flag();
1830 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1831 __func__, gadget_flag, di->check_count);
1833 if (0 == gadget_flag) {
1834 if (++di->check_count >= 5) {
1835 charger_type = AC_CHARGER;
1836 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1837 __func__, di->check_count);
1839 charger_type = USB_CHARGER;
1842 charger_type = USB_CHARGER;
1843 di->check_count = 0;
1846 di->check_count = 0;
1849 return charger_type;
1853 * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
1854 * and dc_adp are plugined in together, the dc_apt has high priority.
1855 * so we check dc_apt first and return rigth away if it's found.
1857 static enum charger_type rk81x_bat_get_adp_type(struct rk81x_battery *di)
1860 enum charger_type charger_type = NO_CHARGER;
1862 /*check by ic hardware: this check make check work safer*/
1863 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1864 if ((buf & PLUG_IN_STS) == 0)
1868 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DC)) {
1869 charger_type = rk81x_bat_get_dc_state(di);
1870 if (charger_type == DC_CHARGER)
1871 return charger_type;
1875 charger_type = rk81x_bat_get_usbac_state(di);
1877 return charger_type;
1880 static void rk81x_bat_status_check(struct rk81x_battery *di)
1882 static enum charger_type old_charger_type = DUAL_CHARGER;
1883 enum charger_type charger_type;
1885 charger_type = rk81x_bat_get_adp_type(di);
1886 if (charger_type == old_charger_type)
1888 rk81x_bat_set_chrg_current(di, charger_type);
1889 rk81x_bat_set_power_supply_state(di, charger_type);
1890 old_charger_type = charger_type;
1894 #if defined(CONFIG_X86_INTEL_SOFIA)
1895 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1897 enum charger_type charger_type;
1900 charger_type = AC_CHARGER;
1902 charger_type = USB_CHARGER;
1904 charger_type = NO_CHARGER;
1908 dev_info(di->dev, "limit current:%d\n", ma);
1910 return charger_type;
1913 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1915 struct rk81x_battery *di;
1916 enum charger_type type;
1918 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1919 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1921 rk81x_bat_set_chrg_current(di, type);
1922 power_supply_changed(&di->usb);
1925 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1926 unsigned long event, void *data)
1928 struct rk81x_battery *di;
1929 struct power_supply_cable_props *cable_props;
1930 enum charger_type type;
1932 di = container_of(nb, struct rk81x_battery, usb_nb);
1938 case USB_EVENT_CHARGER:
1939 cable_props = (struct power_supply_cable_props *)data;
1940 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1941 rk81x_bat_set_power_supply_state(di, type);
1942 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1943 msecs_to_jiffies(50));
1954 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1955 unsigned long event, void *data)
1957 struct rk81x_battery *di;
1958 struct fb_event *evdata = data;
1961 di = container_of(nb, struct rk81x_battery, fb_nb);
1963 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1966 blank = *(int *)evdata->data;
1968 if (di->fb_blank != blank)
1969 di->fb_blank = blank;
1973 if (blank == FB_BLANK_UNBLANK)
1974 di->early_resume = 1;
1979 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1981 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1982 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1984 return fb_register_client(&di->fb_nb);
1988 * it is first time for battery to be weld, init by ocv table
1990 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1992 rk81x_bat_save_fcc(di, di->design_capacity);
1993 di->fcc = rk81x_bat_get_fcc(di);
1995 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1996 di->dsoc = di->rsoc;
1997 di->nac = di->temp_nac;
1999 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2000 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
2001 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
2002 __func__, di->dsoc, di->nac, di->fcc);
2005 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
2010 int relax_vol = di->relax_voltage;
2011 int ocv_vol = di->voltage_ocv;
2013 init_cur = rk81x_bat_get_avg_current(di);
2014 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
2016 est_vol = di->voltage - diff;
2018 if (di->loader_charged) {
2019 calib_vol = est_vol;
2023 if (di->pwroff_min > 8) {
2024 if (abs(relax_vol - ocv_vol) < 100) {
2025 calib_vol = ocv_vol;
2027 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
2028 calib_vol = ocv_vol;
2030 calib_vol = relax_vol;
2032 } else if (di->pwroff_min > 2) {
2033 calib_vol = ocv_vol;
2038 dev_info(di->dev, "c=%d, v=%d, relax:%d, ocv:=%d, est=%d, calib=%d\n",
2039 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2045 * it is not first time for battery to be weld, init by last record info
2047 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2051 int remain_capacity;
2053 int calib_vol, calib_soc, calib_capacity;
2055 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2056 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2057 init_soc = pwron_soc;
2058 remain_capacity = rk81x_bat_get_remain_capacity(di);
2060 /* check if support uboot charge,
2061 * if support, uboot charge driver should have done init work,
2062 * so here we should skip init work
2064 if (rk81x_bat_support_loader_chrg(di))
2067 calib_vol = rk81x_bat_get_calib_vol(di);
2068 if (calib_vol > 0) {
2069 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2070 calib_capacity = di->temp_nac;
2072 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2073 init_soc = calib_soc;
2074 remain_capacity = calib_capacity;
2076 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2077 calib_vol, init_soc, remain_capacity);
2080 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2081 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2082 __func__, remain_capacity, di->temp_nac);
2084 if (di->pwroff_min > 0) {
2085 if (di->pwroff_min > 30) {
2086 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2088 remain_capacity = di->temp_nac;
2089 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2090 __func__, remain_capacity);
2092 } else if ((di->pwroff_min > 5) &&
2093 (abs(ocv_soc - init_soc) >= 10)) {
2094 if (remain_capacity >= di->temp_nac * 120/100)
2095 remain_capacity = di->temp_nac * 110/100;
2096 else if (remain_capacity < di->temp_nac * 8/10)
2097 remain_capacity = di->temp_nac * 9/10;
2098 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2099 __func__, remain_capacity);
2102 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2105 di->dsoc = init_soc;
2106 di->nac = remain_capacity;
2109 DBG("<%s> init_soc = %d, init_capacity=%d\n",
2110 __func__, di->dsoc, di->nac);
2113 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2115 u8 curr_pwroff_min, last_pwroff_min;
2117 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2118 &curr_pwroff_min, 1);
2119 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2120 &last_pwroff_min, 1);
2122 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2123 &curr_pwroff_min, 1);
2125 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2128 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2130 u8 calib_en;/*debug*/
2132 di->voltage = rk81x_bat_get_vol(di);
2133 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2134 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2135 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2136 di->current_avg = rk81x_bat_get_avg_current(di);
2138 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2139 di->voltage, di->voltage_ocv, di->relax_voltage,
2140 di->current_avg, di->pwroff_min);
2142 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2143 DBG("readbit: calib_en=%d\n", calib_en);
2144 if (is_rk81x_bat_first_poweron(di) ||
2145 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2146 rk81x_bat_first_pwron(di);
2147 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2150 rk81x_bat_not_first_pwron(di);
2156 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2161 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2166 DBG(" CHARGE-OFF ...\n");
2170 DBG(" DEAD CHARGE ...\n");
2172 case TRICKLE_CHARGE:
2174 DBG(" TRICKLE CHARGE ...\n ");
2178 DBG(" CC or CV ...\n");
2181 ret = CHARGE_FINISH;
2182 DBG(" CHARGE FINISH ...\n");
2186 DBG(" USB OVER VOL ...\n");
2190 DBG(" BAT TMP ERROR ...\n");
2194 DBG(" TIMER ERROR ...\n");
2198 DBG(" USB EXIST ...\n");
2202 DBG(" USB EFF...\n");
2211 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2212 int chrg_ilim, int chrg_cur)
2216 di->chrg_v_lmt = DEF_CHRG_VOL;
2217 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2218 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2220 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2221 if (chrg_vol < CHRG_V_LMT[i])
2224 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2227 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2228 if (chrg_ilim < CHRG_I_LMT[i])
2231 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2234 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2235 if (chrg_cur < CHRG_I_CUR[i])
2238 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2240 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2241 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2244 static u8 rk81x_bat_select_finish_ma(int fcc)
2246 u8 ma = FINISH_150MA;
2251 else if (fcc >= 4000)
2254 else if (fcc >= 3000)
2264 * there is a timer inside rk81x to calc how long the battery is in charging
2265 * state. rk81x will close PowerPath inside IC when timer reach, which will
2266 * stop the charging work. we have to reset the corresponding bits to restart
2267 * the timer to avoid that case.
2269 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2273 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2274 buf &= ~CHRG_TIMER_CCCV_EN;
2275 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2277 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2278 buf |= CHRG_TIMER_CCCV_EN;
2279 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2280 dev_info(di->dev, "reset cccv charge timer\n");
2283 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2285 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2286 u8 sup_sts_reg, thremal_reg, ggcon;
2287 int chrg_vol, chrg_cur, chrg_ilim;
2290 chrg_vol = di->pdata->max_charger_voltagemV;
2291 chrg_cur = di->pdata->max_charger_currentmA;
2292 chrg_ilim = di->pdata->max_charger_ilimitmA;
2294 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2295 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2297 rk81x_bat_init_chrg_timer(di);
2299 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2300 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2301 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2302 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2303 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2304 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2305 rk81x_bat_read(di, GGCON, &ggcon, 1);
2307 usb_ctrl_reg &= (~0x0f);
2309 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2310 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2312 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2314 if (di->fg_drv_mode == TEST_POWER_MODE)
2315 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2317 chrg_ctrl_reg1 &= (0x00);
2318 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2320 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2321 chrg_ctrl_reg2 &= ~(0xc7);
2322 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2324 sup_sts_reg &= ~(0x01 << 3);
2325 sup_sts_reg |= (0x01 << 2);
2327 thremal_reg &= (~0x0c);
2328 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2329 ggcon |= ADC_CURRENT_MODE;
2331 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2332 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2333 /*don't touch charge setting when boot int loader charge mode*/
2334 if (!di->loader_charged)
2335 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2336 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2337 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2338 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2339 rk81x_bat_write(di, GGCON, &ggcon, 1);
2342 void rk81x_charge_disable_open_otg(struct rk81x_battery *di)
2344 int value = di->charge_otg;
2347 DBG("charge disable, enable OTG.\n");
2348 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2349 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2351 DBG("charge enable, disable OTG.\n");
2352 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2353 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2357 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2364 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2365 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
2366 if (val & PLUG_IN_STS)
2367 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
2369 rk81x_bat_gauge_enable(di);
2370 /* get the volatege offset */
2371 rk81x_bat_get_vol_offset(di);
2372 rk81x_bat_charger_init(di);
2373 rk81x_bat_set_relax_thres(di);
2375 /* get the current offset , the value write to the CAL_OFFSET */
2376 di->current_offset = rk81x_bat_get_ioffset(di);
2377 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2378 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2379 __func__, pcb_offset, di->current_offset);
2381 pcb_offset = DEF_PCB_OFFSET;
2382 cal_offset = pcb_offset + di->current_offset;
2383 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2384 cal_offset = DEF_CAL_OFFSET;
2385 rk81x_bat_set_cal_offset(di, cal_offset);
2386 /* set sample time for cal_offset interval*/
2387 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2389 rk81x_bat_rsoc_init(di);
2390 rk81x_bat_capacity_init(di, di->nac);
2391 rk81x_bat_capacity_init_post(di);
2393 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2394 di->current_avg = rk81x_bat_get_avg_current(di);
2396 rk81x_bat_restart_relax(di);
2397 rk81x_bat_power_on_save(di, di->voltage_ocv);
2399 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2401 rk81x_dbg_dmp_gauge_regs(di);
2402 rk81x_dbg_dmp_charger_regs(di);
2405 "nac = %d , remain_capacity = %d\n"
2406 "OCV_voltage = %d, voltage = %d\n"
2407 "SOC = %d, fcc = %d\n, current=%d\n"
2408 "cal_offset = 0x%x\n",
2410 di->nac, di->remain_capacity,
2411 di->voltage_ocv, di->voltage,
2412 di->dsoc, di->fcc, di->current_avg,
2416 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2418 int dead_voltage, ocv_voltage;
2419 int voltage, voltage_old, voltage_now;
2422 int count_num, currentnow;
2423 int ocv_soc, dead_soc;
2424 int power_off_thresd = di->pdata->power_off_thresd;
2427 voltage_old = rk81x_bat_get_vol(di);
2429 voltage_now = rk81x_bat_get_vol(di);
2431 } while ((voltage_old == voltage_now) && (count_num < 11));
2432 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2435 for (i = 0; i < 10; i++) {
2436 voltage += rk81x_bat_get_vol(di);
2441 currentnow = rk81x_bat_get_avg_current(di);
2443 /* 50 mo power-path mos */
2444 dead_voltage = power_off_thresd - currentnow *
2445 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2447 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2448 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2449 dead_voltage, ocv_voltage);
2451 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2452 q_dead = di->temp_nac;
2453 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2456 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2457 q_ocv = di->temp_nac;
2458 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2461 rsoc = ocv_soc - dead_soc;
2462 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2464 } else if (rsoc > 1) {
2465 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2468 di->display_soc = di->dsoc * 1000;
2471 di->zero_old_remain_cap = di->remain_capacity;
2473 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2474 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2475 di->line_k, di->dsoc, rsoc,
2476 di->display_soc, di->zero_old_remain_cap);
2479 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2481 int delta_cap, delta_soc;
2484 di->zero_timeout_cnt++;
2485 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2486 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2488 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2489 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2490 "ZERO1: timeout_cnt=%d\n\n",
2491 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2492 delta_soc, delta_cap, di->zero_old_remain_cap,
2493 di->zero_timeout_cnt);
2495 if ((abs(delta_soc) > MIN_ZERO_ACCURACY) ||
2496 (di->zero_timeout_cnt > 500)) {
2497 DBG("ZERO1:--------- enter calc -----------\n");
2498 di->zero_timeout_cnt = 0;
2499 di->display_soc -= delta_soc;
2500 tmp_dsoc = (di->display_soc + 500) / 1000;
2501 di->dsoc = tmp_dsoc;
2503 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2504 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2506 rk81x_bat_zero_calc_linek(di);
2510 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2512 return (di->voltage -
2513 (di->bat_res * di->current_avg) / 1000);
2516 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2518 int ocv_soc, ocv_voltage;
2520 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2521 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2526 /* we will estimate a ocv voltage to get a ocv soc.
2527 * if there is a big offset between ocv_soc and rsoc,
2528 * we will decide whether we should reinit capacity or not
2530 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2532 int ocv_soc = di->est_ocv_soc;
2533 int ocv_volt = di->est_ocv_vol;
2534 int rsoc = rk81x_bat_get_rsoc(di);
2535 int max_volt = di->pdata->max_charger_voltagemV;
2537 if (ocv_volt > max_volt)
2540 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2541 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2543 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2545 di->err_soc_sum += ocv_soc;
2549 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2550 __func__, di->err_chck_cnt, di->err_soc_sum);
2552 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2553 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2554 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2555 ocv_soc += RSOC_COMPS;
2557 di->temp_nac = ocv_soc * di->fcc / 100;
2558 rk81x_bat_capacity_init(di, di->temp_nac);
2559 rk81x_bat_capacity_init_post(di);
2560 di->rsoc = rk81x_bat_get_rsoc(di);
2561 di->remain_capacity =
2562 rk81x_bat_get_realtime_capacity(di);
2563 di->err_soc_sum = 0;
2564 di->err_chck_cnt = 0;
2565 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2569 di->err_chck_cnt = 0;
2570 di->err_soc_sum = 0;
2574 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2576 u8 status = di->psy_status;
2578 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2579 (status == POWER_SUPPLY_STATUS_FULL)) {
2580 if ((di->current_avg < 0) &&
2581 (di->chrg_status != CHARGE_FINISH))
2582 rk81x_bat_rsoc_dischrg_check(di);
2585 rsoc_chrg_calib(di);
2588 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2589 rk81x_bat_rsoc_dischrg_check(di);
2593 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2596 unsigned long sec_unit;
2598 if (!di->dischrg_emu_base)
2599 di->dischrg_emu_base = get_runtime_sec();
2601 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2603 temp = di->fcc * 3600 / 100;
2605 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2606 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2608 soc_time = temp / div(abs(di->current_avg));
2610 if (sec_unit > soc_time) {
2612 di->dischrg_emu_base = get_runtime_sec();
2613 di->dischrg_save_sec = 0;
2616 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2617 __func__, soc_time, sec_unit);
2621 * when there is a big offset between dsoc and rsoc, dsoc needs to
2622 * speed up to keep pace witch rsoc.
2624 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2628 unsigned long chrg_emu_sec;
2630 if (!di->chrg_emu_base)
2631 di->chrg_emu_base = get_runtime_sec();
2633 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2634 temp = di->fcc * 3600 / 100;
2636 if (di->ac_online) {
2637 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2638 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2640 soc_time = temp / div(abs(di->current_avg));
2642 soc_time = temp / 450;
2645 plus_soc = chrg_emu_sec / soc_time;
2646 if (chrg_emu_sec > soc_time) {
2647 di->dsoc += plus_soc;
2648 di->chrg_emu_base = get_runtime_sec();
2649 di->chrg_save_sec = 0;
2652 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2653 __func__, soc_time, chrg_emu_sec, plus_soc);
2656 /* check voltage and current when dsoc is close to full.
2657 * we will do a fake charge to adjust charing speed which
2658 * aims to make battery full charged and match finish signal.
2660 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2664 unsigned long chrg_term_sec;
2666 if (!di->chrg_term_base)
2667 di->chrg_term_base = get_runtime_sec();
2669 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2670 /*check current and voltage*/
2672 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2674 plus_soc = chrg_term_sec / soc_time;
2675 if (chrg_term_sec > soc_time) {
2676 di->dsoc += plus_soc;
2677 di->chrg_term_base = get_runtime_sec();
2678 di->chrg_save_sec = 0;
2680 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2681 __func__, soc_time, chrg_term_sec, plus_soc);
2684 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2687 int now_current = di->current_avg;
2688 unsigned long dischrg_normal_sec;
2690 if (!di->dischrg_normal_base)
2691 di->dischrg_normal_base = get_runtime_sec();
2693 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2694 di->dischrg_save_sec;
2696 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2697 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2698 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2700 if (di->rsoc == di->dsoc) {
2701 DBG("<%s>. rsoc == dsoc\n", __func__);
2702 di->dsoc = di->rsoc;
2703 di->dischrg_normal_base = get_runtime_sec();
2704 di->dischrg_save_sec = 0;
2705 /*di->discharge_smooth_status = false;*/
2706 } else if (di->rsoc > di->dsoc - 1) {
2707 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2708 if (dischrg_normal_sec > soc_time * 3 / 2) {
2710 di->dischrg_normal_base = get_runtime_sec();
2711 di->dischrg_save_sec = 0;
2713 di->discharge_smooth_status = true;
2715 } else if (di->rsoc < di->dsoc - 1) {
2716 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2717 if (dischrg_normal_sec > soc_time * 3 / 4) {
2719 di->dischrg_normal_base = get_runtime_sec();
2720 di->dischrg_save_sec = 0;
2722 di->discharge_smooth_status = true;
2724 } else if (di->rsoc == di->dsoc - 1) {
2725 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2726 if (di->discharge_smooth_status) {
2727 if (dischrg_normal_sec > soc_time * 3 / 4) {
2729 di->dischrg_normal_base = get_runtime_sec();
2730 di->dischrg_save_sec = 0;
2731 di->discharge_smooth_status = false;
2735 di->dischrg_normal_base = get_runtime_sec();
2736 di->dischrg_save_sec = 0;
2737 di->discharge_smooth_status = false;
2741 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2742 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2743 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2744 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2747 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2751 /* first resume from suspend: we don't run this,
2752 * the sleep_dischrg will handle dsoc, and what
2753 * ever this is fake wakeup or not, we should clean
2754 * zero algorithm mode, or it will handle the dsoc.
2757 rk81x_bat_reset_zero_var(di);
2761 di->rsoc = rk81x_bat_get_rsoc(di);
2763 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2764 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2766 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2767 delta_soc = di->dsoc - di->rsoc;
2769 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2770 di->dischrg_normal_base = 0;
2771 rk81x_bat_emulator_dischrg(di);
2773 di->chrg_emu_base = 0;
2774 rk81x_bat_normal_dischrg(di);
2777 if (di->voltage < ZERO_ALGOR_THRESD) {
2778 di->dischrg_normal_base = 0;
2779 di->chrg_emu_base = 0;
2780 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2781 di->zero_timeout_cnt = 0;
2783 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2784 __func__, di->dsoc, di->last_zero_mode_dsoc);
2785 if (di->dsoc != di->last_zero_mode_dsoc) {
2786 di->display_soc = di->dsoc * 1000;
2787 di->last_zero_mode_dsoc = di->dsoc;
2788 rk81x_bat_zero_calc_linek(di);
2789 DBG("<%s>. first calc, init linek\n", __func__);
2793 rk81x_bat_zero_algorithm(di);
2795 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2796 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2797 di->zero_timeout_cnt = 0;
2798 DBG("<%s>. exit zero_algorithm\n", __func__);
2803 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2806 static int old_index;
2809 int mod = di->dsoc % 10;
2810 int index = di->dsoc / 10;
2812 if (rk81x_chrg_online(di))
2813 time = di->plug_in_min;
2815 time = di->plug_out_min;
2817 if ((mod == 0) && (index > 0) && (old_index != index)) {
2818 di->chrg_min[index-1] = time - old_min;
2823 for (i = 1; i < 11; i++)
2824 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2828 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2830 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2831 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2832 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2834 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2835 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2836 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2837 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2838 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2839 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2840 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2841 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2842 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2843 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2844 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2846 DBG("\n------------- dump_debug_regs -----------------\n"
2847 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2848 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2849 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2850 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2851 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2852 ggcon_reg, ggsts_reg, rtc_val,
2853 sup_tst_reg, vb_mod_reg,
2854 usb_ctrl_reg, chrg_ctrl_reg1,
2855 thremal_reg, misc_reg,
2856 chrg_ctrl_reg2, chrg_ctrl_reg3
2859 DBG("#######################################################\n"
2860 "voltage = %d, current-avg = %d\n"
2861 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2862 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2863 "display_soc = %d, cpapacity_soc = %d\n"
2864 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2865 "finish_real_soc = %d, finish_temp_soc = %d\n"
2866 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2867 "plug_in = %d, plug_out = %d, finish_sig = %d\n"
2868 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2869 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2870 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2871 "#########################################################\n",
2872 di->voltage, di->current_avg,
2873 di->fcc, di->remain_capacity, di->voltage_ocv,
2874 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2876 di->ac_online, di->usb_online, di->psy_status,
2877 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2878 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2879 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2881 BASE_TO_SEC(di->chrg_normal_base),
2882 BASE_TO_SEC(di->dischrg_normal_base),
2883 BASE_TO_SEC(di->chrg_term_base),
2884 BASE_TO_SEC(di->chrg_emu_base),
2885 BASE_TO_SEC(di->dischrg_emu_base),
2886 BASE_TO_SEC(di->power_on_base), g_base_sec,
2887 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2892 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2896 int dod0_to_soc100_min;
2898 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2899 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2901 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2902 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2903 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2904 di->dod0_capacity, dod0_to_soc100_min);
2906 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2907 (dod0_to_soc100_min < 1200)) {
2908 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2909 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2911 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2913 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2914 __func__, fcc0, di->fcc);
2916 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2917 di->dod0_status = 0;
2919 rk81x_bat_capacity_init(di, di->fcc);
2920 rk81x_bat_capacity_init_post(di);
2921 rk81x_bat_save_fcc(di, di->fcc);
2922 rk81x_bat_save_level(di, di->dod0_level);
2923 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2926 di->dod0_status = 0;
2930 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2932 if (di->chrg_status == CHARGE_FINISH) {
2933 di->debug_finish_real_soc = di->dsoc;
2934 di->debug_finish_temp_soc = di->rsoc;
2938 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2940 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2943 if ((di->chrg_status == CHARGE_FINISH) &&
2944 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2945 rk81x_bat_update_fcc(di);/* save new fcc*/
2946 ret = rk81x_bat_zero_current_calib(di);
2948 di->enter_finish = false;
2950 rk81x_bat_dbg_get_finish_soc(di);
2954 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2956 unsigned long sec_finish;
2957 int soc_time, plus_soc;
2960 if (di->dsoc < 100) {
2961 if (!di->chrg_finish_base)
2962 di->chrg_finish_base = get_runtime_sec();
2964 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2966 temp = di->fcc * 3600 / 100;
2968 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2969 else if (di->usb_online)
2970 soc_time = temp / 450;
2972 plus_soc = sec_finish / soc_time;
2973 if (sec_finish > soc_time) {
2974 di->dsoc += plus_soc;
2975 di->chrg_finish_base = get_runtime_sec();
2976 di->chrg_save_sec = 0;
2978 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2979 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2980 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2984 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2987 u32 soc_time, unit_sec;
2989 unsigned long chrg_normal_sec;
2991 now_current = rk81x_bat_get_avg_current(di);
2992 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2994 if (!di->chrg_normal_base)
2995 di->chrg_normal_base = get_runtime_sec();
2997 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2998 di->rsoc = rk81x_bat_get_rsoc(di);
3000 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
3001 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
3003 if (di->dsoc == di->rsoc) {
3004 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3005 di->rsoc = rk81x_bat_get_rsoc(di);
3006 di->chrg_normal_base = get_runtime_sec();
3007 di->chrg_save_sec = 0;
3008 /*di->charge_smooth_status = false;*/
3009 } else if (di->rsoc < di->dsoc + 1) {
3010 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
3011 unit_sec = soc_time * 3 / 2;
3012 plus_soc = chrg_normal_sec / unit_sec;
3013 if (chrg_normal_sec > unit_sec) {
3014 di->dsoc += plus_soc;
3015 di->chrg_normal_base = get_runtime_sec();
3016 di->chrg_save_sec = 0;
3018 di->charge_smooth_status = true;
3019 } else if (di->rsoc > di->dsoc + 1) {
3020 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
3021 unit_sec = soc_time * 3 / 4;
3022 plus_soc = chrg_normal_sec / unit_sec;
3023 if (chrg_normal_sec > unit_sec) {
3024 di->dsoc += plus_soc;
3025 di->chrg_normal_base = get_runtime_sec();
3026 di->chrg_save_sec = 0;
3028 di->charge_smooth_status = true;
3029 } else if (di->rsoc == di->dsoc + 1) {
3030 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3031 if (di->charge_smooth_status) {
3032 unit_sec = soc_time * 3 / 4;
3033 if (chrg_normal_sec > unit_sec) {
3034 di->dsoc = di->rsoc;
3035 di->chrg_normal_base = get_runtime_sec();
3036 di->charge_smooth_status = false;
3037 di->chrg_save_sec = 0;
3040 di->dsoc = di->rsoc;
3041 di->chrg_normal_base = get_runtime_sec();
3042 di->charge_smooth_status = false;
3043 di->chrg_save_sec = 0;
3047 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3048 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3049 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3050 chrg_normal_sec, soc_time, plus_soc);
3053 static void rk81x_bat_update_time(struct rk81x_battery *di)
3057 runtime_sec = get_runtime_sec();
3059 /*update by charger type*/
3060 if (rk81x_chrg_online(di))
3061 di->plug_out_base = runtime_sec;
3063 di->plug_in_base = runtime_sec;
3065 /*update by current*/
3066 if (di->chrg_status != CHARGE_FINISH) {
3067 di->finish_sig_base = runtime_sec;
3068 di->chrg_finish_base = runtime_sec;
3071 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3072 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3073 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3075 rk81x_bat_dbg_time_table(di);
3078 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di)
3080 int trend_start_cap = di->trend_start_cap;
3081 int remain_cap = di->remain_capacity;
3085 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3086 __func__, trend_start_cap, diff_cap);
3087 diff_cap = remain_cap - trend_start_cap;
3088 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3089 di->trend_start_cap = di->remain_capacity;
3090 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3091 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3093 state = TREND_STAT_FLAT;
3097 di->trend_start_cap = di->remain_capacity;
3102 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3104 int state, soc_time;
3105 static int trend_down_cnt, trend_up_cnt;
3106 int trend_cnt_thresd;
3107 int now_current = di->current_avg;
3109 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3110 state = rk81x_bat_get_rsoc_trend(di);
3111 DBG("<%s>. TREND_STAT = %d, trend_down_cnt = %d\n",
3112 __func__, state, trend_down_cnt);
3113 if (di->chrg_status == CHARGE_FINISH)
3116 if (state == TREND_STAT_UP) {
3117 rk81x_bat_reset_zero_var(di);
3120 if (trend_up_cnt >= trend_cnt_thresd) {
3122 di->dischrg_save_sec = 0;
3124 } else if (state == TREND_STAT_DOWN) {
3127 if (trend_down_cnt >= trend_cnt_thresd) {
3129 di->chrg_save_sec = 0;
3131 } else {/*state == TREND_STAT_FLAT*/
3132 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3133 if ((di->chrg_save_sec + 20 > soc_time) &&
3134 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3136 di->chrg_save_sec = 0;
3138 else if ((di->dischrg_save_sec + 20 > soc_time) &&
3139 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3141 di->dischrg_save_sec = 0;
3144 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3145 "up_cnt=%d, down_cnt=%d\n",
3146 __func__, state, trend_cnt_thresd, soc_time,
3147 trend_up_cnt, trend_down_cnt);
3150 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3152 u32 *ocv_table = di->pdata->battery_ocv;
3153 int delta_soc = di->rsoc - di->dsoc;
3154 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
3156 if ((di->chrg_status == CHARGE_FINISH ||
3157 di->slp_chrg_status == CHARGE_FINISH) &&
3158 (di->voltage > chrg_finish_vol - 150)) {
3159 /*clear sleep charge status*/
3160 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3161 di->chrg_emu_base = 0;
3162 di->chrg_normal_base = 0;
3163 di->chrg_term_base = 0;
3164 rk81x_bat_finish_chrg(di);
3165 rk81x_bat_capacity_init(di, di->fcc);
3166 rk81x_bat_capacity_init_post(di);
3167 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3168 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3169 (di->voltage < ocv_table[18] + 20))) {
3170 di->chrg_emu_base = 0;
3171 di->chrg_normal_base = 0;
3172 di->chrg_finish_base = 0;
3173 rk81x_bat_terminal_chrg(di);
3174 } else if (di->chrg_status != CHARGE_FINISH &&
3175 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3176 di->chrg_term_base = 0;
3177 di->chrg_normal_base = 0;
3178 di->chrg_finish_base = 0;
3179 rk81x_bat_emulator_chrg(di);
3181 di->chrg_emu_base = 0;
3182 di->chrg_term_base = 0;
3183 di->chrg_finish_base = 0;
3184 rk81x_bat_normal_chrg(di);
3188 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3190 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3191 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3193 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3194 dischrg_normal_sec, dischrg_emu_sec);
3196 return (dischrg_normal_sec > dischrg_emu_sec) ?
3197 dischrg_normal_sec : dischrg_emu_sec;
3200 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3202 unsigned long sec1, sec2;
3203 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3204 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3205 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3206 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3208 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3209 chrg_normal_sec : chrg_term_sec;
3211 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3212 chrg_emu_sec : chrg_finish_sec;
3213 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3214 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3215 chrg_normal_sec, chrg_term_sec,
3216 chrg_emu_sec, chrg_finish_sec);
3218 return (sec1 > sec2) ? sec1 : sec2;
3221 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3223 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3224 if (di->current_mode == DISCHRG_MODE) {
3225 di->current_mode = CHRG_MODE;
3226 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3227 di->dischrg_normal_base = 0;
3228 di->dischrg_emu_base = 0;
3229 if (di->chrg_status == CHARGE_FINISH)
3230 di->dischrg_save_sec = 0;
3231 if ((di->chrg_status == CHARGE_FINISH) &&
3233 di->chrg_save_sec = 0;
3235 DBG("<%s>---dischrg_save_sec = %lu\n",
3236 __func__, di->dischrg_save_sec);
3239 if (!rk81x_chrg_online(di)) {
3240 dev_err(di->dev, "discharge, current error:%d\n",
3243 rk81x_bat_chrg_smooth(di);
3244 di->discharge_smooth_status = true;
3247 if (di->current_mode == CHRG_MODE) {
3248 di->current_mode = DISCHRG_MODE;
3249 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3250 di->chrg_normal_base = 0;
3251 di->chrg_emu_base = 0;
3252 di->chrg_term_base = 0;
3253 di->chrg_finish_base = 0;
3254 DBG("<%s>---chrg_save_sec = %lu\n",
3255 __func__, di->chrg_save_sec);
3257 rk81x_bat_dischrg_smooth(di);
3258 di->charge_smooth_status = true;
3263 * update rsoc by relax voltage
3265 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3267 int relax_vol = di->relax_voltage;
3268 int ocv_soc, capacity;
3270 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3271 capacity = (ocv_soc * di->fcc / 100);
3272 rk81x_bat_capacity_init(di, capacity);
3273 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3274 di->rsoc = rk81x_bat_get_rsoc(di);
3275 rk81x_bat_capacity_init_post(di);
3276 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3280 * 1: must do it, 0: when necessary
3282 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3284 int ocv_vol = di->est_ocv_vol;
3285 int ocv_soc = 0, capacity = 0;
3287 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3288 capacity = (ocv_soc * di->fcc / 100);
3289 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3290 rk81x_bat_capacity_init(di, capacity);
3291 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3292 di->rsoc = rk81x_bat_get_rsoc(di);
3293 rk81x_bat_capacity_init_post(di);
3294 DBG("<%s>, rsoc updated!\n", __func__);
3296 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3297 __func__, ocv_vol, ocv_soc, capacity);
3300 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3302 int delta_soc, temp_dsoc;
3303 unsigned long sleep_sec = di->suspend_time_sum;
3304 int power_off_thresd = di->pdata->power_off_thresd;
3306 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3307 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3308 __func__, di->dsoc, di->rsoc,
3309 di->relax_voltage, di->voltage, sleep_sec / 60);
3311 if (di->relax_voltage >= di->voltage) {
3312 rk81x_bat_relax_vol_calib(di);
3313 rk81x_bat_restart_relax(di);
3315 /* current_avg < 0: make sure the system is not
3316 * wakeup by charger plugin.
3318 /* even if relax voltage is not caught rightly, realtime voltage
3319 * is quite close to relax voltage, we should not do nothing after
3323 rk81x_bat_vol_calib(di, 1);
3327 if (di->dsoc <= di->rsoc) {
3328 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3329 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3330 temp_dsoc = di->dsoc - delta_soc;
3332 if (delta_soc > 0) {
3333 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3335 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3337 else if (temp_dsoc > 5)
3338 di->dsoc = temp_dsoc;
3341 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3342 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3344 /*di->dsoc > di->rsoc*/
3345 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3346 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3347 temp_dsoc = di->dsoc - di->rsoc;
3348 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3349 (temp_dsoc > delta_soc))
3350 di->dsoc -= delta_soc;
3352 di->dsoc = di->rsoc;
3354 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3355 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3358 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3364 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3365 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3370 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3373 unsigned long sleep_sec;
3375 sleep_sec = di->suspend_time_sum;
3376 if (((di->suspend_charge_current < 800) &&
3377 (di->ac_online == ONLINE)) ||
3378 (di->chrg_status == CHARGE_FINISH)) {
3379 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3380 if (sleep_sec > 0) {
3381 /*default charge current: 1000mA*/
3382 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3383 / 3600 / div(di->fcc);
3386 DBG("<%s>, usb charge\n", __func__);
3393 * only do report when there is a change.
3395 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3396 * when dsoc == 0, we must do report. But it will generate too much android
3397 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3400 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3403 static u32 old_ac_status;
3404 static u32 old_usb_status;
3405 static u32 old_charge_status;
3408 state_changed = false;
3409 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3410 state_changed = true;
3411 else if (di->dsoc != old_soc)
3412 state_changed = true;
3413 else if (di->ac_online != old_ac_status)
3414 state_changed = true;
3415 else if (di->usb_online != old_usb_status)
3416 state_changed = true;
3417 else if (old_charge_status != di->psy_status)
3418 state_changed = true;
3420 if (di->dsoc >= 100 && rk81x_chrg_online(di))
3421 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3423 if (state_changed) {
3424 power_supply_changed(&di->bat);
3425 power_supply_changed(&di->usb);
3426 power_supply_changed(&di->ac);
3428 old_ac_status = di->ac_online;
3429 old_usb_status = di->usb_online;
3430 old_charge_status = di->psy_status;
3431 DBG("<%s>. report: dsoc=%d, rsoc=%d\n",
3432 __func__, di->dsoc, di->rsoc);
3436 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3440 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3443 return CHRG_CVCC_HOUR[hour];
3446 /* we have to estimate the charging finish time from now, to decide
3447 * whether we should reset the timer or not.
3449 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3452 int remain_capacity;
3454 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3455 if (di->dsoc < di->rsoc)
3456 remain_capacity = di->dsoc * di->fcc / 100;
3458 remain_capacity = di->remain_capacity;
3460 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3461 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3463 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3464 di->chrg_cap2full = di->fcc - remain_capacity;
3465 if (di->current_avg <= 0)
3466 di->current_avg = 1;
3468 di->chrg_time2full = di->chrg_cap2full * 3600 /
3469 div(abs(di->current_avg));
3471 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3472 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3475 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3476 rk81x_bat_init_chrg_timer(di);
3477 di->plug_in_base = get_runtime_sec();
3478 DBG("%s: reset charge timer\n", __func__);
3484 * in case that we will do reboot stress test, we need a special way
3485 * to ajust the dsoc.
3487 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3491 u8 status = di->psy_status;
3496 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3499 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3500 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3502 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3503 __func__, cnt, unit_time, smooth_time,
3504 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3506 if (((status == POWER_SUPPLY_STATUS_CHARGING) ||
3507 (status == POWER_SUPPLY_STATUS_FULL)) &&
3508 (abs(di->current_avg) < 5)) {
3509 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3510 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3515 rk81x_bat_save_dsoc(di, dsoc);
3517 } else {/*status == POWER_SUPPLY_STATUS_DISCHARGING*/
3519 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3/5);
3520 if ((dsoc > rsoc) && (smooth_time > unit_time * 3/5)) {
3525 rk81x_bat_save_dsoc(di, dsoc);
3529 rk81x_bat_save_reboot_cnt(di, cnt);
3532 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3537 uint16_t cal_offset;
3538 u8 pcb_offset = DEF_PCB_OFFSET;
3540 min = BASE_TO_MIN(di->power_on_base);
3541 if ((min % 8) && (old_min != min)) {
3543 rk81x_bat_get_vol_offset(di);
3544 if (di->pcb_ioffset_updated)
3545 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3547 current_offset = rk81x_bat_get_ioffset(di);
3548 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3549 cal_offset = rk81x_bat_get_cal_offset(di);
3550 if (cal_offset < 0x7ff)
3551 rk81x_bat_set_cal_offset(di, di->current_offset +
3553 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3554 __func__, di->voltage_k, di->voltage_b, cal_offset,
3555 rk81x_bat_get_ioffset(di));
3559 static void rk81x_bat_update_info(struct rk81x_battery *di)
3563 else if (di->dsoc < 0)
3567 * we need update fcc in continuous charging state, if discharge state
3568 * keep at least 2 hour, we decide not to update fcc, so clear the
3569 * fcc update flag: dod0_status.
3571 if (BASE_TO_MIN(di->plug_out_base) > 120)
3572 di->dod0_status = 0;
3574 di->voltage = rk81x_bat_get_vol(di);
3575 di->current_avg = rk81x_bat_get_avg_current(di);
3576 di->chrg_status = rk81x_bat_get_chrg_status(di);
3577 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3578 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3579 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3580 rk81x_bat_chrg_over_time_check(di);
3581 rk81x_bat_update_calib_param(di);
3582 if (di->chrg_status == CC_OR_CV)
3583 di->enter_finish = true;
3584 #if defined(CONFIG_ARCH_ROCKCHIP)
3585 rk81x_bat_status_check(di);/* ac_online, usb_online, status*/
3588 if (!rk81x_chrg_online(di) && di->s2r)
3591 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3592 if (di->remain_capacity > di->fcc) {
3593 rk81x_bat_capacity_init(di, di->fcc);
3594 rk81x_bat_capacity_init_post(di);
3595 di->remain_capacity = di->fcc;
3598 di->rsoc = rk81x_bat_get_rsoc(di);
3601 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3603 if (di->slp_psy_status)
3604 return rk81x_bat_sleep_chrg(di);
3606 return rk81x_bat_sleep_dischrg(di);
3609 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3611 u8 ocv_soc, soc_level;
3612 int relax_vol = di->relax_voltage;
3617 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3618 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3619 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3621 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3622 (di->dod0_status == 0) &&
3624 di->dod0_voltage = relax_vol;
3625 di->dod0_capacity = di->temp_nac;
3630 di->dod0_level = 100;
3631 else if (ocv_soc < 5)
3632 di->dod0_level = 90;
3634 di->dod0_level = 80;
3636 /* save_soc = di->dod0_level; */
3637 soc_level = rk81x_bat_get_level(di);
3638 if (soc_level > di->dod0_level) {
3639 di->dod0_status = 0;
3641 di->dod0_status = 1;
3643 di->fcc_update_sec = get_runtime_sec();
3646 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3647 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3649 di->dod0_voltage, di->dod0_capacity,
3650 ocv_soc, soc_level, di->dod0_status,
3655 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3660 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3663 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3665 dev_info(di->dev, "set charge to %s termination mode\n",
3666 mode ? "digital" : "analog");
3669 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3671 struct rk81x_battery *di;
3673 di = container_of(work, struct rk81x_battery,
3674 chrg_term_mode_switch_work.work);
3676 if (rk81x_chrg_online(di))
3677 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3679 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3682 static void rk81x_battery_work(struct work_struct *work)
3684 struct rk81x_battery *di;
3685 int ms = TIMER_MS_COUNTS;
3687 di = container_of(work, struct rk81x_battery,
3688 battery_monitor_work.work);
3689 if (rk81x_chrg_online(di)) {
3690 rk81x_bat_wait_finish_sig(di);
3691 /*rk81x_bat_chrg_finish_routine(di);*/
3693 rk81x_bat_fcc_flag_check(di);
3694 rk81x_bat_arbitrate_rsoc_trend(di);
3695 rk81x_bat_display_smooth(di);
3696 rk81x_bat_update_time(di);
3697 rk81x_bat_update_info(di);
3698 rk81x_bat_rsoc_check(di);
3699 rk81x_bat_power_supply_changed(di);
3700 rk81x_bat_save_dsoc(di, di->dsoc);
3701 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3703 rk81x_bat_dbg_dmp_info(di);
3705 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3706 ms = 30 * TIMER_MS_COUNTS;
3708 di->early_resume = 0;
3712 queue_delayed_work(di->wq, &di->battery_monitor_work,
3713 msecs_to_jiffies(ms));
3716 static void rk81x_battery_charge_check_work(struct work_struct *work)
3718 struct rk81x_battery *di = container_of(work,
3719 struct rk81x_battery, charge_check_work.work);
3721 DBG("rk81x_battery_charge_check_work\n");
3722 rk81x_charge_disable_open_otg(di);
3725 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3727 int register_battery_notifier(struct notifier_block *nb)
3729 return blocking_notifier_chain_register(&battery_chain_head, nb);
3731 EXPORT_SYMBOL_GPL(register_battery_notifier);
3733 int unregister_battery_notifier(struct notifier_block *nb)
3735 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3737 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3739 int battery_notifier_call_chain(unsigned long val)
3741 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3742 == NOTIFY_BAD) ? -EINVAL : 0;
3744 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3746 static void poweron_lowerpoer_handle(struct rk81x_battery *di)
3748 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
3749 if ((di->dsoc <= 2) &&
3750 (di->psy_status == POWER_SUPPLY_STATUS_DISCHARGING)) {
3752 /* kernel_power_off(); */
3757 static int rk81x_bat_notifier_call(struct notifier_block *nb,
3758 unsigned long event, void *data)
3760 struct rk81x_battery *di =
3761 container_of(nb, struct rk81x_battery, battery_nb);
3765 DBG(" CHARGE enable\n");
3767 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3768 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3769 queue_delayed_work(di->wq, &di->charge_check_work,
3770 msecs_to_jiffies(50));
3774 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3775 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3776 queue_delayed_work(di->wq, &di->charge_check_work,
3777 msecs_to_jiffies(50));
3778 DBG("charge disable OTG enable\n");
3781 poweron_lowerpoer_handle(di);
3789 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3791 pr_info("\n------- %s:lower power warning!\n", __func__);
3793 rk_send_wakeup_key();
3798 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3800 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3801 rk_send_wakeup_key();
3805 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3807 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3808 rk_send_wakeup_key();
3812 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3814 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3816 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3817 di->finish_sig_base = get_runtime_sec();
3818 rk_send_wakeup_key();
3822 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3824 rk_send_wakeup_key();
3829 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3834 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3835 ret = sysfs_create_file(&di->bat.dev->kobj,
3836 &rk818_bat_attr[i].attr);
3838 dev_err(di->dev, "create battery node(%s) error\n",
3839 rk818_bat_attr[i].attr.name);
3845 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3847 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3849 struct rk818 *chip = di->rk818;
3851 #if defined(CONFIG_X86_INTEL_SOFIA)
3852 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3853 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3854 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3855 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3857 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3858 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3859 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3860 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3863 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3864 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3866 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3868 di->irq = vb_lo_irq;
3869 enable_irq_wake(di->irq);
3871 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3872 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3875 dev_err(chip->dev, "plug_in_irq request failed!\n");
3877 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3878 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3881 dev_err(chip->dev, "plug_out_irq request failed!\n");
3883 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3884 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3887 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3890 static void rk81x_bat_info_init(struct rk81x_battery *di,
3893 unsigned long time_base = get_runtime_sec();
3895 di->cell.config = di->pdata->cell_cfg;
3896 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3897 di->qmax = di->pdata->cell_cfg->design_qmax;
3898 di->early_resume = 1;
3899 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3900 di->bat_res = di->pdata->sense_resistor_mohm;
3901 di->fg_drv_mode = FG_NORMAL_MODE;
3902 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3903 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3904 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3905 di->loader_charged = loader_charged;
3906 di->chrg_finish_base = time_base;
3907 di->power_on_base = time_base;
3908 di->plug_in_base = time_base;
3909 di->plug_out_base = time_base;
3910 di->finish_sig_base = time_base;
3911 di->fcc = rk81x_bat_get_fcc(di);
3914 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3915 struct device_node *np)
3917 struct device *dev = di->dev;
3918 enum of_gpio_flags flags;
3921 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3922 if (di->dc_det_pin == -EPROBE_DEFER)
3923 dev_err(dev, "dc_det_gpio error\n");
3924 if (gpio_is_valid(di->dc_det_pin))
3925 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3926 RK818_DC_IN : RK818_DC_OUT;
3928 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3929 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3930 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3931 "rk81x_dc_det", NULL);
3934 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3935 enable_irq_wake(di->dc_det_irq);
3938 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3943 struct timespec tv = {
3944 .tv_nsec = NSEC_PER_SEC >> 1,
3946 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3948 err = rtc_read_time(rtc, &tm);
3950 dev_err(rtc->dev.parent,
3951 "hctosys: unable to read the hardware clock\n");
3953 err = rtc_valid_tm(&tm);
3955 dev_err(rtc->dev.parent,
3956 "hctosys: invalid date/time\n");
3959 rtc_tm_to_time(&tm, &tv.tv_sec);
3960 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3966 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
3968 struct device_node *np;
3969 struct battery_platform_data *pdata;
3970 struct cell_config *cell_cfg;
3971 struct ocv_config *ocv_cfg;
3972 struct property *prop;
3973 struct rk818 *rk818 = di->rk818;
3974 struct device *dev = di->dev;
3979 np = of_find_node_by_name(rk818->dev->of_node, "battery");
3981 dev_err(dev, "battery node not found!\n");
3985 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
3989 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3993 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3997 prop = of_find_property(np, "ocv_table", &length);
3999 dev_err(dev, "ocv_table not found!\n");
4002 pdata->ocv_size = length / sizeof(u32);
4003 if (pdata->ocv_size <= 0) {
4004 dev_err(dev, "invalid ocv table\n");
4008 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4010 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4011 if (!pdata->battery_ocv)
4014 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4019 /******************** charger param ****************************/
4020 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4022 dev_err(dev, "max_chrg_currentmA not found!\n");
4023 out_value = DEFAULT_CHRG_CUR;
4025 pdata->max_charger_currentmA = out_value;
4027 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4029 dev_err(dev, "max_charger_ilimitmA not found!\n");
4030 out_value = DEFAULT_INPUT_CUR;
4032 pdata->max_charger_ilimitmA = out_value;
4034 ret = of_property_read_u32(np, "bat_res", &out_value);
4036 dev_err(dev, "bat_res not found!\n");
4037 out_value = DEFAULT_BAT_RES;
4039 pdata->sense_resistor_mohm = out_value;
4041 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4043 dev_err(dev, "max_charge_voltagemV not found!\n");
4044 out_value = DEFAULT_CHRG_VOL;
4046 pdata->max_charger_voltagemV = out_value;
4048 ret = of_property_read_u32(np, "design_capacity", &out_value);
4050 dev_err(dev, "design_capacity not found!\n");
4053 cell_cfg->design_capacity = out_value;
4055 ret = of_property_read_u32(np, "design_qmax", &out_value);
4057 dev_err(dev, "design_qmax not found!\n");
4060 cell_cfg->design_qmax = out_value;
4062 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4064 dev_err(dev, "sleep_enter_current not found!\n");
4065 out_value = DEFAULT_SLP_ENTER_CUR;
4067 ocv_cfg->sleep_enter_current = out_value;
4069 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4071 dev_err(dev, "sleep_exit_current not found!\n");
4072 out_value = DEFAULT_SLP_EXIT_CUR;
4074 ocv_cfg->sleep_exit_current = out_value;
4076 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4078 dev_warn(dev, "power_off_thresd not found!\n");
4079 out_value = PWR_OFF_THRESD;
4081 pdata->power_off_thresd = out_value;
4083 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4085 /************* charger support adp types **********************/
4086 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4087 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4089 if (!support_usb_adp && !support_dc_adp) {
4090 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4091 support_usb_adp = 1;
4095 rk81x_bat_dc_det_init(di, np);
4097 cell_cfg->ocv = ocv_cfg;
4098 pdata->cell_cfg = cell_cfg;
4101 DBG("\nthe battery dts info dump:\n"
4103 "max_input_currentmA:%d\n"
4104 "max_chrg_currentmA:%d\n"
4105 "max_charge_voltagemV:%d\n"
4106 "design_capacity:%d\n"
4108 "sleep_enter_current:%d\n"
4109 "sleep_exit_current:%d\n"
4110 "support_usb_adp:%d\n"
4111 "support_dc_adp:%d\n"
4112 "power_off_thresd:%d\n",
4113 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4114 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4115 cell_cfg->design_capacity, cell_cfg->design_qmax,
4116 cell_cfg->ocv->sleep_enter_current,
4117 cell_cfg->ocv->sleep_exit_current,
4118 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4124 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4130 static int rk81x_battery_probe(struct platform_device *pdev)
4132 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4133 struct rk81x_battery *di;
4136 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4140 di->dev = &pdev->dev;
4141 platform_set_drvdata(pdev, di);
4143 ret = rk81x_bat_parse_dt(di);
4145 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4149 rk81x_bat_info_init(di, chip);
4150 if (!is_rk81x_bat_exist(di)) {
4151 dev_info(di->dev, "not battery, enter test power mode\n");
4152 di->fg_drv_mode = TEST_POWER_MODE;
4155 ret = rk81x_bat_power_supply_init(di);
4157 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4161 rk81x_bat_irq_init(di);
4162 rk81x_bat_sysfs_init(di);
4164 rk81x_bat_fg_init(di);
4165 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4167 rk81x_bat_flatzone_vol_init(di);
4169 #if defined(CONFIG_X86_INTEL_SOFIA)
4170 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4171 if (IS_ERR_OR_NULL(di->usb_phy)) {
4172 dev_err(di->dev, "get usb phy failed\n");
4173 return PTR_ERR(di->usb_phy);
4175 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4176 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4178 dev_err(di->dev, "registr usb phy notification failed\n");
4179 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4180 rk81x_battery_usb_notifier_delayed_work);
4183 rk81x_battery_register_fb_notify(di);
4184 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4185 "rk81x-battery-work");
4186 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4187 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4188 rk81x_chrg_term_mode_switch_work);
4190 queue_delayed_work(di->wq, &di->battery_monitor_work,
4191 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4193 INIT_DELAYED_WORK(&di->charge_check_work,
4194 rk81x_battery_charge_check_work);
4195 di->battery_nb.notifier_call = rk81x_bat_notifier_call;
4196 register_battery_notifier(&di->battery_nb);
4198 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4203 static int rk81x_battery_suspend(struct platform_device *dev,
4206 struct rk81x_battery *di = platform_get_drvdata(dev);
4208 di->slp_psy_status = rk81x_chrg_online(di);
4210 di->chrg_status = rk81x_bat_get_chrg_status(di);
4211 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4212 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4213 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4214 di->dischrg_normal_base = 0;
4215 di->dischrg_emu_base = 0;
4216 do_gettimeofday(&di->suspend_rtc_base);
4218 * do not modify the g_base_sec
4220 g_base_sec = BASE_TO_SEC(di->power_on_base)+di->power_on_base;
4222 if (!rk81x_chrg_online(di)) {
4223 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4224 di->chrg_normal_base = 0;
4225 di->chrg_emu_base = 0;
4226 di->chrg_term_base = 0;
4227 di->chrg_finish_base = 0;
4235 static int rk81x_battery_resume(struct platform_device *dev)
4237 struct rk81x_battery *di = platform_get_drvdata(dev);
4238 int pwroff_thresd = di->pdata->power_off_thresd;
4243 di->discharge_smooth_status = true;
4244 di->charge_smooth_status = true;
4246 di->voltage = rk81x_bat_get_vol(di);
4247 di->current_avg = rk81x_bat_get_avg_current(di);
4248 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4249 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4250 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4251 delta_time = rk81x_bat_get_suspend_sec(di);
4252 di->suspend_time_sum += delta_time;
4255 * do not modify the g_base_sec
4257 if (is_local_clock_reset())
4258 g_base_sec += delta_time;
4262 if (di->slp_psy_status) {
4263 time_step = CHRG_TIME_STEP;
4265 if (di->voltage <= pwroff_thresd + 50)
4266 time_step = DISCHRG_TIME_STEP_0;
4268 time_step = DISCHRG_TIME_STEP_1;
4271 if (di->suspend_time_sum > time_step) {
4272 delta_soc = rk81x_bat_update_resume_state(di);
4274 di->suspend_time_sum = 0;
4277 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4278 rk81x_chrg_online(di))
4279 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4281 DBG("<%s>. current:%d, est_ocv_vol:%d, delta_time:%d, vol:%d\n"
4282 "relax-vol:%d, suspend_time:%ld, online:%d, resume_sec:%lld\n",
4283 __func__, di->current_avg, di->est_ocv_vol, delta_time,
4284 di->voltage, di->relax_voltage, di->suspend_time_sum,
4285 di->slp_psy_status, get_runtime_sec());
4290 static int rk81x_battery_remove(struct platform_device *dev)
4292 struct rk81x_battery *di = platform_get_drvdata(dev);
4294 cancel_delayed_work_sync(&di->battery_monitor_work);
4298 static void rk81x_battery_shutdown(struct platform_device *dev)
4300 struct rk81x_battery *di = platform_get_drvdata(dev);
4302 cancel_delayed_work_sync(&di->battery_monitor_work);
4303 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4304 rk81x_bat_check_reboot(di);
4306 rk81x_bat_save_reboot_cnt(di, 0);
4307 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4310 static struct platform_driver rk81x_battery_driver = {
4312 .name = "rk818-battery",
4313 .owner = THIS_MODULE,
4316 .probe = rk81x_battery_probe,
4317 .remove = rk81x_battery_remove,
4318 .suspend = rk81x_battery_suspend,
4319 .resume = rk81x_battery_resume,
4320 .shutdown = rk81x_battery_shutdown,
4323 static int __init battery_init(void)
4325 return platform_driver_register(&rk81x_battery_driver);
4328 fs_initcall_sync(battery_init);
4329 static void __exit battery_exit(void)
4331 platform_driver_unregister(&rk81x_battery_driver);
4333 module_exit(battery_exit);
4335 MODULE_LICENSE("GPL");
4336 MODULE_ALIAS("platform:rk818-battery");
4337 MODULE_AUTHOR("ROCKCHIP");