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 <linux/power/rk_usbbc.h>
59 #include "rk818_battery.h"
61 /* if you want to disable, don't set it as 0,
62 just be: "static int dbg_enable;" is ok*/
64 static int dbg_enable;
65 #define RK818_SYS_DBG 1
67 module_param_named(dbg_level, dbg_enable, int, 0644);
69 #define DBG(args...) \
76 #define DEFAULT_BAT_RES 135
77 #define DEFAULT_CHRG_VOL 4200
78 #define DEFAULT_CHRG_CUR 1000
79 #define DEFAULT_INPUT_CUR 1400
80 #define DEFAULT_SLP_ENTER_CUR 600
81 #define DEFAULT_SLP_EXIT_CUR 600
83 #define DSOC_DISCHRG_EMU_CURR 1200
84 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
85 #define DSOC_DISCHRG_FAST_EER_RANGE 10
86 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
87 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
88 #define DSOC_CHRG_FAST_EER_RANGE 10
89 #define DSOC_CHRG_EMU_CURR 1200
90 #define DSOC_CHRG_TERM_CURR 600
91 #define DSOC_CHRG_TERM_VOL 4100
92 #define CHRG_FINISH_VOL 4100
94 /*realtime RSOC calib param*/
95 #define RSOC_DISCHRG_ERR_LOWER 40
96 #define RSOC_DISCHRG_ERR_UPPER 50
97 #define RSOC_ERR_CHCK_CNT 15
98 #define RSOC_COMPS 20 /*compensation*/
99 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
100 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
102 #define RSOC_RESUME_ERR 10
103 #define REBOOT_INTER_MIN 1
105 #define INTERPOLATE_MAX 1000
106 #define MAX_INT 0x7FFF
107 #define TIME_10MIN_SEC 600
109 #define CHRG_VOL_SHIFT 4
110 #define CHRG_ILIM_SHIFT 0
111 #define CHRG_ICUR_SHIFT 0
112 #define DEF_CHRG_VOL CHRG_VOL4200
113 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
114 #define DEF_CHRG_CURR_LMT ILIM_2000MA
116 /*TEST_POWER_MODE params*/
117 #define TEST_CURRENT 1000
118 #define TEST_VOLTAGE 3800
120 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
121 #define TEST_PRESET 1
122 #define TEST_AC_ONLINE 1
123 #define TEST_USB_ONLINE 0
125 #define ZERO_ALGOR_THRESD 3800
126 #define DISCHRG_ZERO_MODE 1
127 #define DISCHRG_NORMAL_MODE 0
128 #define DEF_LAST_ZERO_MODE_SOC -1
130 #define DISCHRG_MODE 0
133 #define TREND_STAT_FLAT 0
134 #define TREND_STAT_DOWN -1
135 #define TREND_STAT_UP 1
136 #define TREND_CAP_DIFF 5
140 #define SLP_CURR_MAX 40
141 #define SLP_CURR_MIN 6
142 #define WAKEUP_SEC_THRESD 40
143 #define CHRG_TIME_STEP (60)
144 #define DISCHRG_TIME_STEP_0 (30 * 60)
145 #define DISCHRG_TIME_STEP_1 (60 * 60)
147 #define DEF_PCB_OFFSET 42
148 #define DEF_CAL_OFFSET 0x832
149 #define DEF_PWRPATH_RES 50
150 #define SEC_TO_EMPTY 300
151 #define DSOC_CHRG_FINISH_CURR 1100
152 #define SLP_CHRG_CURR 1000
153 #define SLP_DSOC_VOL_THRESD 3600
154 /*if voltage is lower than this thresd,
155 we consider it as invalid
157 #define INVALID_VOL_THRESD 2500
158 #define PWR_OFF_THRESD 3400
159 #define MIN_ZERO_ACCURACY 5 /*0.01%*/
160 #define MIN_ROUND_ACCURACY 1
162 #define MAX_FCC 10000
165 * the following table value depends on datasheet
167 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
169 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
170 2250, 2400, 2600, 2800, 3000};
172 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
173 2000, 2250, 2500, 2750, 3000};
175 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
177 #define RK818_DC_IN 0
178 #define RK818_DC_OUT 1
180 #define OCV_VALID_SHIFT (0)
181 #define OCV_CALIB_SHIFT (1)
182 #define FIRST_PWRON_SHIFT (2)
184 #define SEC_TO_MIN(x) ((x) / 60)
186 struct rk81x_battery {
188 struct cell_state cell;
189 struct power_supply bat;
190 struct power_supply ac;
191 struct power_supply usb;
192 struct delayed_work work;
194 struct pinctrl *pinctrl;
195 struct pinctrl_state *pins_default;
197 struct battery_platform_data *pdata;
211 uint16_t voltage_ocv;
212 uint16_t relax_voltage;
218 bool pcb_ioffset_updated;
235 int bat_res_update_cnt;
241 unsigned long dod0_time;
254 int current_k;/* (ICALIB0, ICALIB1) */
257 int voltage_k;/* VCALIB0 VCALIB1 */
260 int zero_timeout_cnt;
261 int zero_old_remain_cap;
266 int charge_smooth_time;
270 unsigned long suspend_time_sum;
274 int suspend_charge_current;
277 bool charge_smooth_status;
278 bool discharge_smooth_status;
284 struct notifier_block battery_nb;
285 struct usb_phy *usb_phy;
286 struct notifier_block usb_nb;
287 struct notifier_block fb_nb;
290 int s2r; /*suspend to resume*/
291 struct workqueue_struct *wq;
292 struct delayed_work battery_monitor_work;
293 struct delayed_work otg_check_work;
294 struct delayed_work usb_phy_delay_work;
295 struct delayed_work chrg_term_mode_switch_work;
296 struct delayed_work ac_usb_check_work;
297 struct delayed_work dc_det_check_work;
298 enum bc_port_type charge_otg;
301 struct wake_lock resume_wake_lock;
302 unsigned long plug_in_base;
303 unsigned long plug_out_base;
304 unsigned long finish_sig_base;
305 unsigned long power_on_base;
310 bool is_first_poweron;
313 int debug_finish_real_soc;
314 int debug_finish_temp_soc;
320 unsigned long wakeup_sec;
321 u32 delta_vol_smooth;
322 unsigned long dischrg_normal_base;
323 unsigned long dischrg_emu_base;
324 unsigned long chrg_normal_base;
325 unsigned long chrg_term_base;
326 unsigned long chrg_emu_base;
327 unsigned long chrg_finish_base;
328 unsigned long fcc_update_sec;
330 u8 dischrg_algorithm_mode;
331 int last_zero_mode_dsoc;
333 unsigned long dischrg_save_sec;
334 unsigned long chrg_save_sec;
335 struct timeval suspend_rtc_base;
338 u32 support_usb_adp, support_dc_adp;
340 #define to_device_info(x) container_of((x), \
341 struct rk81x_battery, bat)
343 #define to_ac_device_info(x) container_of((x), \
344 struct rk81x_battery, ac)
346 #define to_usb_device_info(x) container_of((x), \
347 struct rk81x_battery, usb)
349 static int loader_charged;
351 static int __init rk81x_bat_loader_charged(char *__unused)
355 pr_info("battery charged in loader\n");
359 __setup("loader_charged", rk81x_bat_loader_charged);
361 static u64 get_runtime_sec(void)
365 get_monotonic_boottime(&ts);
369 static inline unsigned long BASE_TO_SEC(unsigned long x)
372 return (get_runtime_sec() > x) ? (get_runtime_sec() - x) : 0;
377 static inline unsigned long BASE_TO_MIN(unsigned long x)
379 return BASE_TO_SEC(x) / 60;
382 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
387 case HW_ADP_TYPE_USB:
395 case HW_ADP_TYPE_DUAL:
396 if (support_usb_adp && support_dc_adp)
406 static bool rk81x_chrg_online(struct rk81x_battery *di)
408 return di->usb_online || di->ac_online;
411 static u32 interpolate(int value, u32 *table, int size)
416 for (i = 0; i < size; i++) {
417 if (value < table[i])
421 if ((i > 0) && (i < size)) {
422 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
423 d /= table[i] - table[i-1];
424 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
426 d = i * ((INTERPOLATE_MAX + size / 2) / size);
435 /* Returns (a * b) / c */
436 static int32_t ab_div_c(u32 a, u32 b, u32 c)
442 sign = ((((a^b)^c) & 0x80000000) != 0);
448 tmp = (a * b + (c >> 1)) / c;
460 static int div(int val)
462 return (val == 0) ? 1 : val;
465 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
466 u8 buf[], unsigned len)
471 for (i = 0; ret < 0 && i < 3; i++) {
472 ret = rk818_i2c_read(di->rk818, reg, len, buf);
474 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
477 return (ret < 0) ? ret : 0;
480 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
481 u8 const buf[], unsigned len)
486 for (i = 0; ret < 0 && i < 3; i++) {
487 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
489 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
492 return (ret < 0) ? ret : 0;
495 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
500 for (i = 0; ret < 0 && i < 3; i++) {
501 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
503 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
509 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
514 for (i = 0; ret < 0 && i < 3; i++) {
515 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
517 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
523 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
528 rk81x_bat_read(di, reg, &buf, 1);
529 val = (buf & BIT(shift)) >> shift;
533 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
538 DBG("%s dump charger register start:\n", __func__);
539 for (i = 0xAC; i < 0xEE; i++) {
540 rk81x_bat_read(di, i, &buf, 1);
541 DBG("0x%02x : 0x%02x\n", i, buf);
546 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
551 DBG("%s dump the register start:\n", __func__);
552 for (i = 0x99; i < 0xAB; i++) {
553 rk81x_bat_read(di, i, &buf, 1);
554 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
559 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
561 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
562 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
565 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
567 rk81x_bat_reset_zero_var(di);
568 di->trend_start_cap = di->remain_capacity;
571 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
577 delta_cap = capacity - di->remain_capacity;
581 di->adjust_cap += delta_cap;
583 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
585 buf = (capacity_ma >> 24) & 0xff;
586 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
587 buf = (capacity_ma >> 16) & 0xff;
588 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
589 buf = (capacity_ma >> 8) & 0xff;
590 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
591 buf = (capacity_ma & 0xff) | 0x01;
592 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
593 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
597 if (di->chrg_status != CHARGE_FINISH || di->dod0_status == 1)
598 dev_dbg(di->dev, "update capacity :%d--remain_cap:%d\n",
599 capacity, di->remain_capacity);
604 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
606 static ssize_t bat_calib_read(struct device *dev,
607 struct device_attribute *attr, char *buf)
609 struct power_supply *psy_bat = dev_get_drvdata(dev);
610 struct rk81x_battery *di = to_device_info(psy_bat);
613 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
615 return sprintf(buf, "%d\n", val);
618 static ssize_t bat_calib_write(struct device *dev,
619 struct device_attribute *attr,
620 const char *buf, size_t count)
624 struct power_supply *psy_bat = dev_get_drvdata(dev);
625 struct rk81x_battery *di = to_device_info(psy_bat);
627 ret = kstrtou8(buf, 0, &val);
632 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
634 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
639 * interface for debug: force battery to over discharge
641 static ssize_t bat_test_power_read(struct device *dev,
642 struct device_attribute *attr, char *buf)
644 struct power_supply *psy_bat = dev_get_drvdata(dev);
645 struct rk81x_battery *di = to_device_info(psy_bat);
647 return sprintf(buf, "%d\n", di->fg_drv_mode);
650 static ssize_t bat_test_power_write(struct device *dev,
651 struct device_attribute *attr,
652 const char *buf, size_t count)
656 struct power_supply *psy_bat = dev_get_drvdata(dev);
657 struct rk81x_battery *di = to_device_info(psy_bat);
659 ret = kstrtou8(buf, 0, &val);
664 di->fg_drv_mode = TEST_POWER_MODE;
666 di->fg_drv_mode = FG_NORMAL_MODE;
671 static ssize_t bat_fcc_read(struct device *dev,
672 struct device_attribute *attr, char *buf)
674 struct power_supply *psy_bat = dev_get_drvdata(dev);
675 struct rk81x_battery *di = to_device_info(psy_bat);
677 return sprintf(buf, "%d\n", di->fcc);
680 static ssize_t bat_fcc_write(struct device *dev,
681 struct device_attribute *attr,
682 const char *buf, size_t count)
686 struct power_supply *psy_bat = dev_get_drvdata(dev);
687 struct rk81x_battery *di = to_device_info(psy_bat);
689 ret = kstrtou16(buf, 0, &val);
698 static ssize_t bat_dsoc_read(struct device *dev,
699 struct device_attribute *attr, char *buf)
701 struct power_supply *psy_bat = dev_get_drvdata(dev);
702 struct rk81x_battery *di = to_device_info(psy_bat);
704 return sprintf(buf, "%d\n", di->dsoc);
707 static ssize_t bat_dsoc_write(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf, size_t count)
713 struct power_supply *psy_bat = dev_get_drvdata(dev);
714 struct rk81x_battery *di = to_device_info(psy_bat);
716 ret = kstrtou8(buf, 0, &val);
725 static ssize_t bat_rsoc_read(struct device *dev,
726 struct device_attribute *attr, char *buf)
728 struct power_supply *psy_bat = dev_get_drvdata(dev);
729 struct rk81x_battery *di = to_device_info(psy_bat);
731 return sprintf(buf, "%d\n", di->rsoc);
734 static ssize_t bat_rsoc_write(struct device *dev,
735 struct device_attribute *attr,
736 const char *buf, size_t count)
741 struct power_supply *psy_bat = dev_get_drvdata(dev);
742 struct rk81x_battery *di = to_device_info(psy_bat);
744 ret = kstrtou8(buf, 0, &val);
748 capacity = di->fcc * val / 100;
749 rk81x_bat_capacity_init(di, capacity);
750 rk81x_bat_capacity_init_post(di);
755 static ssize_t bat_remain_cap_read(struct device *dev,
756 struct device_attribute *attr,
759 struct power_supply *psy_bat = dev_get_drvdata(dev);
760 struct rk81x_battery *di = to_device_info(psy_bat);
762 return sprintf(buf, "%d\n", di->remain_capacity);
765 static struct device_attribute rk818_bat_attr[] = {
766 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
767 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
768 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
769 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
770 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
771 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
775 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
780 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
782 dev_err(di->dev, "error reading TS_CTRL_REG");
787 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
792 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
794 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
797 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
801 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
806 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
812 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
814 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
817 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
821 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
827 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
829 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
832 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
836 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
842 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
844 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
850 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
856 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
858 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
864 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
870 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
871 buf = (value >> 8) & 0xff;
872 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
877 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
879 int vcalib0, vcalib1;
881 vcalib0 = rk81x_bat_get_vcalib0(di);
882 vcalib1 = rk81x_bat_get_vcalib1(di);
884 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
885 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
886 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
889 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
894 uint16_t voltage_now = 0;
898 for (i = 0; i < 3; i++) {
899 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
901 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
905 dev_err(di->dev, "error read BAT_OCV_REGH");
910 if (val[0] == val[1])
915 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
920 static int rk81x_bat_get_vol(struct rk81x_battery *di)
929 for (i = 0; i < 3; i++) {
930 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
932 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
936 dev_err(di->dev, "error read BAT_VOL_REGH");
941 if (val[0] == val[1])
946 vol = di->voltage_k * temp / 1000 + di->voltage_b;
951 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
956 ret = rk81x_bat_read(di, GGSTS, &status, 1);
958 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
964 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
968 uint16_t temp = 0, voltage_now;
970 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
972 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
975 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
980 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
984 uint16_t temp = 0, voltage_now;
986 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
988 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
991 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
996 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1000 uint16_t relax_vol1, relax_vol2;
1003 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1004 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1006 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1007 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1008 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1009 __func__, status, ggcon, relax_vol1, relax_vol2);
1011 if (is_rk81x_bat_relax_mode(di))
1012 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1018 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1019 * based on the voltage.
1021 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1028 ocv_table = di->pdata->battery_ocv;
1029 ocv_size = di->pdata->ocv_size;
1030 tmp = interpolate(voltage, ocv_table, ocv_size);
1031 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1032 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1037 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1043 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1045 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1049 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1051 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1057 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1067 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1071 rk81x_bat_read(di, GGCON, &ggcon, 1);
1072 ggcon &= ~(0x30); /*clear <5:4>*/
1074 rk81x_bat_write(di, GGCON, &ggcon, 1);
1078 * when charger finish signal comes, we need calibrate the current, make it
1081 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1091 if ((di->chrg_status == CHARGE_FINISH) &&
1092 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1093 (abs(di->current_avg) > 4)) {
1094 for (retry = 0; retry < 5; retry++) {
1095 adc_value = rk81x_bat_get_raw_adc_current(di);
1096 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1097 dev_warn(di->dev, "charger plugout\n");
1102 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1103 C0 = rk81x_bat_get_cal_offset(di);
1104 C1 = adc_value + C0;
1105 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1107 rk81x_bat_set_cal_offset(di, C1);
1108 DBG("<%s>. new cal_offset = %d\n",
1109 __func__, rk81x_bat_get_cal_offset(di));
1111 adc_value = rk81x_bat_get_raw_adc_current(di);
1112 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1113 if (abs(adc_value) < 4) {
1114 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1115 ioffset = rk81x_bat_get_ioffset(di);
1116 rk81x_bat_set_cal_offset(di,
1119 ioffset = rk81x_bat_get_ioffset(di);
1120 pcb_offset = C1 - ioffset;
1121 di->pcb_ioffset = pcb_offset;
1122 di->pcb_ioffset_updated = true;
1127 DBG("<%s>. update the cal_offset, C1 = %d\n"
1128 "i_offset = %d, pcb_offset = %d\n",
1129 __func__, C1, ioffset, pcb_offset);
1133 dev_dbg(di->dev, "ioffset cal failed\n");
1134 rk81x_bat_set_cal_offset(di, C0);
1137 di->pcb_ioffset_updated = false;
1144 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1147 int enter_thres, exit_thres;
1148 struct cell_state *cell = &di->cell;
1150 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1151 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1152 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1153 __func__, cell->config->ocv->sleep_enter_current,
1154 cell->config->ocv->sleep_exit_current);
1156 buf = enter_thres & 0xff;
1157 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1158 buf = (enter_thres >> 8) & 0xff;
1159 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1161 buf = exit_thres & 0xff;
1162 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1163 buf = (exit_thres >> 8) & 0xff;
1164 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1166 /* set sample time */
1167 rk81x_bat_read(di, GGCON, &buf, 1);
1168 buf &= ~(3 << 2);/*8min*/
1169 buf &= ~0x01; /* clear bat_res calc*/
1170 rk81x_bat_write(di, GGCON, &buf, 1);
1173 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1178 rk81x_bat_read(di, GGCON, &ggcon, 1);
1180 rk81x_bat_write(di, GGCON, &ggcon, 1);
1182 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1184 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1187 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1196 for (i = 0; i < 3; i++) {
1197 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1199 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1204 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1206 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1212 if (val[0] == val[1])
1213 current_now = val[0];
1215 current_now = val[2];
1217 if (current_now & 0x800)
1218 current_now -= 4096;
1220 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1225 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1226 enum charger_type charger_type)
1228 di->usb_online = OFFLINE;
1229 di->ac_online = OFFLINE;
1231 switch (charger_type) {
1233 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1236 di->usb_online = ONLINE;
1237 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1239 case DC_CHARGER:/*treat dc as ac*/
1241 di->ac_online = ONLINE;
1242 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1245 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1249 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1250 msecs_to_jiffies(1000));
1253 /* high load: current < 0 with charger in.
1254 * System will not shutdown while dsoc=0% with charging state(ac_online),
1255 * which will cause over discharge, so oppose status before report states.
1257 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1260 int pwr_off_thresd = di->pdata->power_off_thresd;
1262 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1264 time = get_runtime_sec();
1266 if (BASE_TO_SEC(time) > (MINUTE)) {
1267 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1268 dev_info(di->dev, "low power....\n");
1271 if (di->voltage <= pwr_off_thresd - 50) {
1273 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1280 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1284 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1286 return (buf & 0x80) ? 1 : 0;
1289 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1294 rk81x_bat_read(di, GGSTS, &buf, 1);
1295 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1296 /*di->pwron_bat_con = buf;*/
1300 rk81x_bat_write(di, GGSTS, &buf, 1);
1301 rk81x_bat_read(di, GGSTS, &temp, 1);
1302 } while (temp & BAT_CON);
1309 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1316 ocv_table = di->pdata->battery_ocv;
1317 ocv_size = di->pdata->ocv_size;
1319 for (j = 0; j < 21; j++)
1323 for (i = 1; i < ocv_size-1; i++) {
1324 if (ocv_table[i+1] < ocv_table[i] + 20)
1325 temp_table[j++] = i;
1328 temp_table[j] = temp_table[j-1] + 1;
1330 di->enter_flatzone = ocv_table[i];
1333 for (i = 0; i < 20; i++) {
1334 if (temp_table[i] < temp_table[i+1])
1339 di->exit_flatzone = ocv_table[i];
1341 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1342 di->enter_flatzone, di->exit_flatzone);
1345 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1347 u8 ocv_valid, first_pwron;
1351 /*buf==1: OCV_VOL is valid*/
1352 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1353 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1354 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1356 if (first_pwron == 1 || ocv_valid == 1) {
1357 DBG("<%s> enter.\n", __func__);
1358 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1359 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1360 di->dod0_voltage = ocv_voltage;
1361 di->dod0_capacity = di->temp_nac;
1366 di->dod0_level = 100;
1367 else if (ocv_soc < 5)
1368 di->dod0_level = 95;
1369 else if (ocv_soc < 10)
1370 di->dod0_level = 90;
1372 di->dod0_level = 80;
1373 /* save_soc = di->dod0_level; */
1374 soc_level = rk81x_bat_get_level(di);
1375 if (soc_level > di->dod0_level) {
1376 di->dod0_status = 0;
1378 if (soc_level <= 80)
1380 rk81x_bat_save_level(di, soc_level);
1382 di->dod0_status = 1;
1384 di->fcc_update_sec = get_runtime_sec();
1387 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1388 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1390 di->dod0_voltage, di->dod0_capacity,
1391 ocv_soc, soc_level, di->dod0_status,
1397 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1399 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1402 static enum power_supply_property rk_battery_props[] = {
1403 POWER_SUPPLY_PROP_STATUS,
1404 POWER_SUPPLY_PROP_CURRENT_NOW,
1405 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1406 POWER_SUPPLY_PROP_PRESENT,
1407 POWER_SUPPLY_PROP_HEALTH,
1408 POWER_SUPPLY_PROP_CAPACITY,
1411 static int rk81x_battery_get_property(struct power_supply *psy,
1412 enum power_supply_property psp,
1413 union power_supply_propval *val)
1415 struct rk81x_battery *di = to_device_info(psy);
1418 case POWER_SUPPLY_PROP_CURRENT_NOW:
1419 val->intval = di->current_avg * 1000;/*uA*/
1420 if (di->fg_drv_mode == TEST_POWER_MODE)
1421 val->intval = TEST_CURRENT * 1000;
1423 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1424 val->intval = di->voltage * 1000;/*uV*/
1425 if (di->fg_drv_mode == TEST_POWER_MODE)
1426 val->intval = TEST_VOLTAGE * 1000;
1429 case POWER_SUPPLY_PROP_PRESENT:
1430 val->intval = is_rk81x_bat_exist(di);
1431 if (di->fg_drv_mode == TEST_POWER_MODE)
1432 val->intval = TEST_PRESET;
1435 case POWER_SUPPLY_PROP_CAPACITY:
1436 val->intval = di->dsoc;
1437 if (di->fg_drv_mode == TEST_POWER_MODE)
1438 val->intval = TEST_SOC;
1440 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1442 case POWER_SUPPLY_PROP_HEALTH:
1443 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1445 case POWER_SUPPLY_PROP_STATUS:
1446 val->intval = di->psy_status;
1447 if (di->fg_drv_mode == TEST_POWER_MODE)
1448 val->intval = TEST_STATUS;
1458 static enum power_supply_property rk_battery_ac_props[] = {
1459 POWER_SUPPLY_PROP_ONLINE,
1462 static enum power_supply_property rk_battery_usb_props[] = {
1463 POWER_SUPPLY_PROP_ONLINE,
1466 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1467 enum power_supply_property psp,
1468 union power_supply_propval *val)
1471 struct rk81x_battery *di = to_ac_device_info(psy);
1474 case POWER_SUPPLY_PROP_ONLINE:
1475 if (rk81x_chrg_online(di))
1476 rk81x_bat_lowpwr_check(di);
1477 val->intval = di->ac_online; /*discharging*/
1478 if (di->fg_drv_mode == TEST_POWER_MODE)
1479 val->intval = TEST_AC_ONLINE;
1489 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1490 enum power_supply_property psp,
1491 union power_supply_propval *val)
1494 struct rk81x_battery *di = to_usb_device_info(psy);
1497 case POWER_SUPPLY_PROP_ONLINE:
1498 if (rk81x_chrg_online(di))
1499 rk81x_bat_lowpwr_check(di);
1500 val->intval = di->usb_online;
1501 if (di->fg_drv_mode == TEST_POWER_MODE)
1502 val->intval = TEST_USB_ONLINE;
1512 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1516 di->bat.name = "BATTERY";
1517 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1518 di->bat.properties = rk_battery_props;
1519 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1520 di->bat.get_property = rk81x_battery_get_property;
1523 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1524 di->ac.properties = rk_battery_ac_props;
1525 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1526 di->ac.get_property = rk81x_battery_ac_get_property;
1528 di->usb.name = "USB";
1529 di->usb.type = POWER_SUPPLY_TYPE_USB;
1530 di->usb.properties = rk_battery_usb_props;
1531 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1532 di->usb.get_property = rk81x_battery_usb_get_property;
1534 ret = power_supply_register(di->dev, &di->bat);
1536 dev_err(di->dev, "failed to register main battery\n");
1539 ret = power_supply_register(di->dev, &di->usb);
1541 dev_err(di->dev, "failed to register usb power supply\n");
1544 ret = power_supply_register(di->dev, &di->ac);
1546 dev_err(di->dev, "failed to register ac power supply\n");
1553 power_supply_unregister(&di->ac);
1555 power_supply_unregister(&di->usb);
1557 power_supply_unregister(&di->bat);
1562 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1566 static u32 capacity_ma;
1568 if (capacity >= di->qmax)
1569 capacity = di->qmax;
1574 if (capacity_ma == capacity)
1577 capacity_ma = capacity;
1579 buf = (capacity_ma >> 24) & 0xff;
1580 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1581 buf = (capacity_ma >> 16) & 0xff;
1582 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1583 buf = (capacity_ma >> 8) & 0xff;
1584 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1585 buf = (capacity_ma & 0xff) | 0x01;
1586 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1589 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1597 for (i = 0; i < 3; i++) {
1598 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1600 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1601 val[i] |= buf << 16;
1602 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1604 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1608 if (val[0] == val[1])
1616 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1621 capacity_ma = capacity;
1622 buf = (capacity_ma >> 24) & 0xff;
1623 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1624 buf = (capacity_ma >> 16) & 0xff;
1625 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1626 buf = (capacity_ma >> 8) & 0xff;
1627 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1628 buf = (capacity_ma & 0xff) | 0x01;
1629 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1631 dev_info(di->dev, "update fcc : %d\n", capacity);
1634 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1639 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1640 capacity = buf << 24;
1641 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1642 capacity |= buf << 16;
1643 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1644 capacity |= buf << 8;
1645 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1648 if (capacity < MIN_FCC) {
1649 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1651 capacity = di->design_capacity;
1652 rk81x_bat_save_fcc(di, capacity);
1653 } else if (capacity > di->qmax) {
1654 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1655 capacity = di->qmax;
1656 rk81x_bat_save_fcc(di, capacity);
1662 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1671 for (i = 0; i < 3; i++) {
1672 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1674 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1675 val[i] |= buf << 16;
1676 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1678 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1681 if (val[0] == val[1])
1686 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1691 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1695 if (last_soc != save_soc) {
1696 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1697 last_soc = save_soc;
1703 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1708 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1712 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1716 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1717 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1718 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1719 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1722 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1723 enum charger_type charger_type)
1725 switch (charger_type) {
1728 rk81x_bat_set_current(di, ILIM_450MA);
1732 rk81x_bat_set_current(di, di->chrg_i_lmt);
1735 rk81x_bat_set_current(di, ILIM_450MA);
1739 #if defined(CONFIG_ARCH_ROCKCHIP)
1741 static void rk81x_bat_set_charger_param(struct rk81x_battery *di,
1742 enum charger_type charger_type)
1744 rk81x_bat_set_chrg_current(di, charger_type);
1745 rk81x_bat_set_power_supply_state(di, charger_type);
1747 switch (charger_type) {
1749 power_supply_changed(&di->bat);
1753 power_supply_changed(&di->usb);
1756 power_supply_changed(&di->ac);
1763 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1766 enum charger_type charger_type = NO_CHARGER;
1768 if (di->fg_drv_mode == TEST_POWER_MODE) {
1769 charger_type = DC_CHARGER;
1776 if (!gpio_is_valid(di->dc_det_pin))
1779 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1781 pr_err("Failed to request gpio %d with ret:""%d\n",
1782 di->dc_det_pin, ret);
1786 gpio_direction_input(di->dc_det_pin);
1787 ret = gpio_get_value(di->dc_det_pin);
1788 if (ret == di->dc_det_level)
1789 charger_type = DC_CHARGER;
1791 charger_type = NO_CHARGER;
1792 gpio_free(di->dc_det_pin);
1794 return charger_type;
1797 static void rk81x_battery_dc_delay_work(struct work_struct *work)
1799 enum charger_type charger_type;
1800 struct rk81x_battery *di = container_of(work,
1801 struct rk81x_battery, dc_det_check_work.work);
1803 charger_type = rk81x_bat_get_dc_state(di);
1805 if (charger_type == DC_CHARGER)
1806 rk81x_bat_set_charger_param(di, DC_CHARGER);
1807 else/*NO_CHARGER: maybe usb charger still plugin*/
1808 queue_delayed_work(di->wq,
1809 &di->ac_usb_check_work,
1810 msecs_to_jiffies(10));
1813 static void rk81x_battery_acusb_delay_work(struct work_struct *work)
1816 int gadget_flag, usb_id;
1817 struct rk81x_battery *di = container_of(work,
1818 struct rk81x_battery, ac_usb_check_work.work);
1820 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1821 usb_id = dwc_otg_check_dpdm(0);
1824 if ((buf & PLUG_IN_STS) != 0)
1825 rk81x_bat_set_charger_param(di, DC_CHARGER);
1827 rk81x_bat_set_charger_param(di, NO_CHARGER);
1831 rk81x_bat_set_charger_param(di, USB_CHARGER);
1834 rk81x_bat_set_charger_param(di, AC_CHARGER);
1839 /*check unstanderd charger*/
1840 if (usb_id == 1 || usb_id == 3) {
1841 gadget_flag = get_gadget_connect_flag();
1842 if (0 == gadget_flag) {
1844 if (di->check_count >= 5) {
1845 di->check_count = 0;
1846 rk81x_bat_set_charger_param(di, AC_CHARGER);
1848 queue_delayed_work(di->wq,
1849 &di->ac_usb_check_work,
1850 msecs_to_jiffies(1000));
1852 } else {/*confirm: USB_CHARGER*/
1853 di->check_count = 0;
1859 #if defined(CONFIG_X86_INTEL_SOFIA)
1860 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1862 enum charger_type charger_type;
1865 charger_type = AC_CHARGER;
1867 charger_type = USB_CHARGER;
1869 charger_type = NO_CHARGER;
1873 dev_info(di->dev, "limit current:%d\n", ma);
1875 return charger_type;
1878 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1880 struct rk81x_battery *di;
1881 enum charger_type type;
1883 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1884 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1886 rk81x_bat_set_chrg_current(di, type);
1887 power_supply_changed(&di->usb);
1890 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1891 unsigned long event, void *data)
1893 struct rk81x_battery *di;
1894 struct power_supply_cable_props *cable_props;
1895 enum charger_type type;
1897 di = container_of(nb, struct rk81x_battery, usb_nb);
1903 case USB_EVENT_CHARGER:
1904 cable_props = (struct power_supply_cable_props *)data;
1905 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1906 rk81x_bat_set_power_supply_state(di, type);
1907 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1908 msecs_to_jiffies(50));
1919 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1920 unsigned long event, void *data)
1922 struct rk81x_battery *di;
1923 struct fb_event *evdata = data;
1926 di = container_of(nb, struct rk81x_battery, fb_nb);
1928 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1931 blank = *(int *)evdata->data;
1933 if (di->fb_blank != blank)
1934 di->fb_blank = blank;
1938 if (blank == FB_BLANK_UNBLANK)
1939 di->early_resume = 1;
1944 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1946 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1947 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1949 return fb_register_client(&di->fb_nb);
1953 * it is first time for battery to be weld, init by ocv table
1955 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1957 rk81x_bat_save_fcc(di, di->design_capacity);
1958 di->fcc = rk81x_bat_get_fcc(di);
1960 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1961 di->dsoc = di->rsoc;
1962 di->nac = di->temp_nac;
1964 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1965 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1966 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1967 __func__, di->dsoc, di->nac, di->fcc);
1970 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
1975 int relax_vol = di->relax_voltage;
1976 int ocv_vol = di->voltage_ocv;
1978 init_cur = rk81x_bat_get_avg_current(di);
1979 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
1981 est_vol = di->voltage - diff;
1983 if (di->loader_charged) {
1984 calib_vol = est_vol;
1988 if (di->pwroff_min > 8) {
1989 if (abs(relax_vol - ocv_vol) < 100) {
1990 calib_vol = ocv_vol;
1992 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
1993 calib_vol = ocv_vol;
1995 calib_vol = relax_vol;
1997 } else if (di->pwroff_min > 2) {
1998 calib_vol = ocv_vol;
2003 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2004 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2010 * it is not first time for battery to be weld, init by last record info
2012 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2016 int remain_capacity;
2018 int calib_vol, calib_soc, calib_capacity;
2020 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2021 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2022 init_soc = pwron_soc;
2023 remain_capacity = rk81x_bat_get_remain_capacity(di);
2025 /* check if support uboot charge,
2026 * if support, uboot charge driver should have done init work,
2027 * so here we should skip init work
2029 #if defined(CONFIG_ARCH_ROCKCHIP)
2030 if (di->loader_charged) {
2031 dev_info(di->dev, "loader charged\n");
2035 calib_vol = rk81x_bat_get_calib_vol(di);
2036 if (calib_vol > 0) {
2037 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2038 calib_capacity = di->temp_nac;
2040 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2041 init_soc = calib_soc;
2042 remain_capacity = calib_capacity;
2044 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2045 calib_vol, init_soc, remain_capacity);
2048 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2049 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2050 __func__, remain_capacity, di->temp_nac);
2052 if (di->pwroff_min > 0) {
2053 if (di->pwroff_min > 30) {
2054 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2056 remain_capacity = di->temp_nac;
2057 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2058 __func__, remain_capacity);
2060 } else if ((di->pwroff_min > 5) &&
2061 (abs(ocv_soc - init_soc) >= 10)) {
2062 if (remain_capacity >= di->temp_nac * 120/100)
2063 remain_capacity = di->temp_nac * 110/100;
2064 else if (remain_capacity < di->temp_nac * 8/10)
2065 remain_capacity = di->temp_nac * 9/10;
2066 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2067 __func__, remain_capacity);
2070 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2073 di->dsoc = init_soc;
2074 di->nac = remain_capacity;
2077 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2078 pwron_soc, di->dsoc, di->nac);
2081 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2083 u8 curr_pwroff_min, last_pwroff_min;
2085 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2086 &curr_pwroff_min, 1);
2087 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2088 &last_pwroff_min, 1);
2090 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2091 &curr_pwroff_min, 1);
2093 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2096 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2098 u8 calib_en;/*debug*/
2100 di->voltage = rk81x_bat_get_vol(di);
2101 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2102 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2103 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2104 di->current_avg = rk81x_bat_get_avg_current(di);
2106 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2107 di->voltage, di->voltage_ocv, di->relax_voltage,
2108 di->current_avg, di->pwroff_min);
2110 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2111 DBG("readbit: calib_en=%d\n", calib_en);
2112 if (is_rk81x_bat_first_poweron(di) ||
2113 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2114 rk81x_bat_first_pwron(di);
2115 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2118 rk81x_bat_not_first_pwron(di);
2124 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2129 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2134 DBG(" CHARGE-OFF ...\n");
2138 DBG(" DEAD CHARGE ...\n");
2140 case TRICKLE_CHARGE:
2142 DBG(" TRICKLE CHARGE ...\n ");
2146 DBG(" CC or CV ...\n");
2149 ret = CHARGE_FINISH;
2150 DBG(" CHARGE FINISH ...\n");
2154 DBG(" USB OVER VOL ...\n");
2158 DBG(" BAT TMP ERROR ...\n");
2162 DBG(" TIMER ERROR ...\n");
2166 DBG(" USB EXIST ...\n");
2170 DBG(" USB EFF...\n");
2179 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2180 int chrg_ilim, int chrg_cur)
2184 di->chrg_v_lmt = DEF_CHRG_VOL;
2185 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2186 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2188 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2189 if (chrg_vol < CHRG_V_LMT[i])
2192 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2195 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2196 if (chrg_ilim < CHRG_I_LMT[i])
2199 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2202 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2203 if (chrg_cur < CHRG_I_CUR[i])
2206 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2208 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2209 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2212 static u8 rk81x_bat_select_finish_ma(int fcc)
2214 u8 ma = FINISH_150MA;
2219 else if (fcc >= 4000)
2222 else if (fcc >= 3000)
2232 * there is a timer inside rk81x to calc how long the battery is in charging
2233 * state. rk81x will close PowerPath inside IC when timer reach, which will
2234 * stop the charging work. we have to reset the corresponding bits to restart
2235 * the timer to avoid that case.
2237 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2241 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2242 buf &= ~CHRG_TIMER_CCCV_EN;
2243 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2245 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2246 buf |= CHRG_TIMER_CCCV_EN;
2247 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2248 dev_info(di->dev, "reset cccv charge timer\n");
2252 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2254 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2255 u8 sup_sts_reg, thremal_reg, ggcon;
2256 int chrg_vol, chrg_cur, chrg_ilim;
2259 chrg_vol = di->pdata->max_charger_voltagemV;
2260 chrg_cur = di->pdata->max_charger_currentmA;
2261 chrg_ilim = di->pdata->max_charger_ilimitmA;
2263 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2264 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2266 /*rk81x_bat_init_chrg_timer(di);*/
2268 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2269 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2270 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2271 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2272 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2273 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2274 rk81x_bat_read(di, GGCON, &ggcon, 1);
2276 usb_ctrl_reg &= (~0x0f);
2278 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2279 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2281 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2283 if (di->fg_drv_mode == TEST_POWER_MODE)
2284 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2286 chrg_ctrl_reg1 &= (0x00);
2287 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2289 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2290 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2292 chrg_ctrl_reg2 &= ~(0xc7);
2293 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2295 sup_sts_reg &= ~(0x01 << 3);
2296 sup_sts_reg |= (0x01 << 2);
2298 thremal_reg &= (~0x0c);
2299 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2300 ggcon |= ADC_CURRENT_MODE;
2302 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2303 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2304 /*don't touch charge setting when boot int loader charge mode*/
2305 if (!di->loader_charged)
2306 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2307 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2308 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2309 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2310 rk81x_bat_write(di, GGCON, &ggcon, 1);
2313 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2320 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2322 rk81x_bat_gauge_enable(di);
2323 /* get the volatege offset */
2324 rk81x_bat_get_vol_offset(di);
2325 rk81x_bat_charger_init(di);
2326 rk81x_bat_set_relax_thres(di);
2328 /* get the current offset , the value write to the CAL_OFFSET */
2329 di->current_offset = rk81x_bat_get_ioffset(di);
2330 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2331 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2332 __func__, pcb_offset, di->current_offset);
2334 pcb_offset = DEF_PCB_OFFSET;
2335 cal_offset = pcb_offset + di->current_offset;
2336 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2337 cal_offset = DEF_CAL_OFFSET;
2338 rk81x_bat_set_cal_offset(di, cal_offset);
2339 /* set sample time for cal_offset interval*/
2340 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2342 rk81x_bat_rsoc_init(di);
2343 rk81x_bat_capacity_init(di, di->nac);
2344 rk81x_bat_capacity_init_post(di);
2346 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2347 di->current_avg = rk81x_bat_get_avg_current(di);
2349 rk81x_bat_restart_relax(di);
2350 rk81x_bat_power_on_save(di, di->voltage_ocv);
2352 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2354 rk81x_dbg_dmp_gauge_regs(di);
2355 rk81x_dbg_dmp_charger_regs(di);
2358 "nac = %d , remain_capacity = %d\n"
2359 "OCV_voltage = %d, voltage = %d\n"
2360 "SOC = %d, fcc = %d\n, current=%d\n"
2361 "cal_offset = 0x%x\n",
2363 di->nac, di->remain_capacity,
2364 di->voltage_ocv, di->voltage,
2365 di->dsoc, di->fcc, di->current_avg,
2369 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2371 int dead_voltage, ocv_voltage;
2372 int voltage, voltage_old, voltage_now;
2377 int ocv_soc, dead_soc;
2378 int power_off_thresd = di->pdata->power_off_thresd;
2381 voltage_old = rk81x_bat_get_vol(di);
2383 voltage_now = rk81x_bat_get_vol(di);
2385 } while ((voltage_old == voltage_now) && (count_num < 11));
2386 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2389 for (i = 0; i < 10; i++) {
2390 voltage += rk81x_bat_get_vol(di);
2395 currentnow = rk81x_bat_get_avg_current(di);
2397 /* 50 mo power-path mos */
2398 dead_voltage = power_off_thresd - currentnow *
2399 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2401 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2402 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2403 dead_voltage, ocv_voltage);
2405 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2406 q_dead = di->temp_nac;
2407 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2410 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2411 q_ocv = di->temp_nac;
2412 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2415 rsoc = ocv_soc - dead_soc;
2416 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2418 } else if (rsoc > 0) {
2419 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2422 di->display_soc = di->dsoc * 1000;
2425 di->zero_old_remain_cap = di->remain_capacity;
2427 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2428 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2429 di->line_k, di->dsoc, rsoc,
2430 di->display_soc, di->zero_old_remain_cap);
2433 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2435 int delta_cap, delta_soc;
2438 di->zero_timeout_cnt++;
2439 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2440 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2442 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2443 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2444 "ZERO1: timeout_cnt=%d\n\n",
2445 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2446 delta_soc, delta_cap, di->zero_old_remain_cap,
2447 di->zero_timeout_cnt);
2449 if ((delta_soc >= MIN_ZERO_ACCURACY) ||
2450 (di->zero_timeout_cnt > 500)) {
2451 DBG("ZERO1:--------- enter calc -----------\n");
2452 di->zero_timeout_cnt = 0;
2453 di->display_soc -= delta_soc;
2454 tmp_dsoc = (di->display_soc + MIN_ROUND_ACCURACY) / 1000;
2455 di->dsoc = tmp_dsoc;
2456 /* need to be init, otherwise when switch between discharge and
2457 * charge display_soc will be init as: dsoc * 1000
2459 di->last_zero_mode_dsoc = tmp_dsoc;
2460 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2461 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2463 rk81x_bat_zero_calc_linek(di);
2467 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2469 return (di->voltage -
2470 (di->bat_res * di->current_avg) / 1000);
2473 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2475 int ocv_soc, ocv_voltage;
2477 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2478 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2483 /* we will estimate a ocv voltage to get a ocv soc.
2484 * if there is a big offset between ocv_soc and rsoc,
2485 * we will decide whether we should reinit capacity or not
2487 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2489 int ocv_soc = di->est_ocv_soc;
2490 int ocv_volt = di->est_ocv_vol;
2491 int rsoc = rk81x_bat_get_rsoc(di);
2492 int max_volt = di->pdata->max_charger_voltagemV;
2494 if (ocv_volt > max_volt)
2497 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2498 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2500 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2502 di->err_soc_sum += ocv_soc;
2506 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2507 __func__, di->err_chck_cnt, di->err_soc_sum);
2509 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2510 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2511 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2512 ocv_soc += RSOC_COMPS;
2514 di->temp_nac = ocv_soc * di->fcc / 100;
2515 rk81x_bat_capacity_init(di, di->temp_nac);
2516 rk81x_bat_capacity_init_post(di);
2517 di->rsoc = rk81x_bat_get_rsoc(di);
2518 di->remain_capacity =
2519 rk81x_bat_get_realtime_capacity(di);
2520 di->err_soc_sum = 0;
2521 di->err_chck_cnt = 0;
2522 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2526 di->err_chck_cnt = 0;
2527 di->err_soc_sum = 0;
2531 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2533 u8 status = di->psy_status;
2535 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2536 (status == POWER_SUPPLY_STATUS_FULL)) {
2537 if ((di->current_avg < 0) &&
2538 (di->chrg_status != CHARGE_FINISH))
2539 rk81x_bat_rsoc_dischrg_check(di);
2542 rsoc_chrg_calib(di);
2545 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2546 rk81x_bat_rsoc_dischrg_check(di);
2550 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2552 u32 temp, soc_time = 0;
2553 unsigned long sec_unit;
2555 if (!di->dischrg_emu_base)
2556 di->dischrg_emu_base = get_runtime_sec();
2558 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2560 temp = di->fcc * 3600 / 100;
2562 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2563 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2565 soc_time = temp / div(abs(di->current_avg));
2567 if (sec_unit > soc_time) {
2569 di->dischrg_emu_base = get_runtime_sec();
2570 di->dischrg_save_sec = 0;
2573 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2574 __func__, soc_time, sec_unit);
2578 * when there is a big offset between dsoc and rsoc, dsoc needs to
2579 * speed up to keep pace witch rsoc.
2581 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2583 u32 soc_time = 0, temp;
2585 unsigned long chrg_emu_sec;
2587 if (!di->chrg_emu_base)
2588 di->chrg_emu_base = get_runtime_sec();
2590 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2591 temp = di->fcc * 3600 / 100;
2593 if (di->ac_online) {
2594 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2595 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2597 soc_time = temp / div(abs(di->current_avg));
2599 soc_time = temp / 450;
2602 plus_soc = chrg_emu_sec / soc_time;
2603 if (chrg_emu_sec > soc_time) {
2604 di->dsoc += plus_soc;
2605 di->chrg_emu_base = get_runtime_sec();
2606 di->chrg_save_sec = 0;
2609 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2610 __func__, soc_time, chrg_emu_sec, plus_soc);
2613 /* check voltage and current when dsoc is close to full.
2614 * we will do a fake charge to adjust charing speed which
2615 * aims to make battery full charged and match finish signal.
2617 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2621 unsigned long chrg_term_sec;
2623 if (!di->chrg_term_base)
2624 di->chrg_term_base = get_runtime_sec();
2626 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2627 /*check current and voltage*/
2629 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2631 plus_soc = chrg_term_sec / soc_time;
2632 if (chrg_term_sec > soc_time) {
2633 di->dsoc += plus_soc;
2634 di->chrg_term_base = get_runtime_sec();
2635 di->chrg_save_sec = 0;
2637 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2638 __func__, soc_time, chrg_term_sec, plus_soc);
2641 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2644 int now_current = di->current_avg;
2645 unsigned long dischrg_normal_sec;
2647 if (!di->dischrg_normal_base)
2648 di->dischrg_normal_base = get_runtime_sec();
2650 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2651 di->dischrg_save_sec;
2653 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2654 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2655 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2657 if (di->rsoc == di->dsoc) {
2658 DBG("<%s>. rsoc == dsoc\n", __func__);
2659 di->dsoc = di->rsoc;
2660 di->dischrg_normal_base = get_runtime_sec();
2661 di->dischrg_save_sec = 0;
2662 /*di->discharge_smooth_status = false;*/
2663 } else if (di->rsoc > di->dsoc - 1) {
2664 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2665 if (dischrg_normal_sec > soc_time * 3 / 2) {
2667 di->dischrg_normal_base = get_runtime_sec();
2668 di->dischrg_save_sec = 0;
2670 di->discharge_smooth_status = true;
2672 } else if (di->rsoc < di->dsoc - 1) {
2673 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2674 if (dischrg_normal_sec > soc_time * 3 / 4) {
2676 di->dischrg_normal_base = get_runtime_sec();
2677 di->dischrg_save_sec = 0;
2679 di->discharge_smooth_status = true;
2681 } else if (di->rsoc == di->dsoc - 1) {
2682 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2683 if (di->discharge_smooth_status) {
2684 if (dischrg_normal_sec > soc_time * 3 / 4) {
2686 di->dischrg_normal_base = get_runtime_sec();
2687 di->dischrg_save_sec = 0;
2688 di->discharge_smooth_status = false;
2692 di->dischrg_normal_base = get_runtime_sec();
2693 di->dischrg_save_sec = 0;
2694 di->discharge_smooth_status = false;
2698 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2699 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2700 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2701 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2704 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2709 /* first resume from suspend: we don't run this,
2710 * the sleep_dischrg will handle dsoc, and what
2711 * ever this is fake wakeup or not, we should clean
2712 * zero algorithm mode, or it will handle the dsoc.
2715 rk81x_bat_reset_zero_var(di);
2719 di->rsoc = rk81x_bat_get_rsoc(di);
2721 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2722 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2724 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2725 delta_soc = di->dsoc - di->rsoc;
2727 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2728 di->dischrg_normal_base = 0;
2729 rk81x_bat_emulator_dischrg(di);
2731 di->chrg_emu_base = 0;
2732 rk81x_bat_normal_dischrg(di);
2735 if (di->voltage < ZERO_ALGOR_THRESD) {
2736 di->dischrg_normal_base = 0;
2737 di->chrg_emu_base = 0;
2738 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2739 di->zero_timeout_cnt = 0;
2741 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2742 __func__, di->dsoc, di->last_zero_mode_dsoc);
2743 if (di->dsoc != di->last_zero_mode_dsoc) {
2744 tmp_dsoc = (di->display_soc +
2745 MIN_ROUND_ACCURACY) / 1000;
2746 /* if last display_soc invalid, recalc.
2747 * otherwise keep this value(in case: plugin and
2748 * plugout quickly or wakeup from deep sleep,
2749 * we need't init display_soc)
2751 if (tmp_dsoc != di->dsoc)
2752 /* first init value should round up,
2753 * other wise dsoc will quickly turn to
2754 * dsoc-- if MIN_ROUND_ACCURACY value is
2755 * small,eg:1.(in case: power on system)
2757 di->display_soc = (di->dsoc + 1) *
2758 1000 - MIN_ROUND_ACCURACY;
2759 di->last_zero_mode_dsoc = di->dsoc;
2760 rk81x_bat_zero_calc_linek(di);
2761 DBG("<%s>. first calc, init linek\n", __func__);
2765 rk81x_bat_zero_algorithm(di);
2767 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2768 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2769 di->zero_timeout_cnt = 0;
2770 DBG("<%s>. exit zero_algorithm\n", __func__);
2775 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2778 static int old_index;
2781 int mod = di->dsoc % 10;
2782 int index = di->dsoc / 10;
2784 if (rk81x_chrg_online(di))
2785 time = di->plug_in_min;
2787 time = di->plug_out_min;
2789 if ((mod == 0) && (index > 0) && (old_index != index)) {
2790 di->chrg_min[index-1] = time - old_min;
2795 for (i = 1; i < 11; i++)
2796 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2800 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2802 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2803 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2804 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2807 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2808 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2809 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2810 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2811 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2812 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2813 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2814 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2815 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2816 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2817 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2820 DBG("\n------------- dump_debug_regs -----------------\n"
2821 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2822 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2823 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2824 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2825 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2826 ggcon_reg, ggsts_reg, rtc_val,
2827 sup_tst_reg, vb_mod_reg,
2828 usb_ctrl_reg, chrg_ctrl_reg1,
2829 thremal_reg, misc_reg,
2830 chrg_ctrl_reg2, chrg_ctrl_reg3
2833 DBG("#######################################################\n"
2834 "voltage = %d, current-avg = %d\n"
2835 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2836 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2837 "display_soc = %d, cpapacity_soc = %d\n"
2838 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2839 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2840 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2841 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2842 "emu_dischrg = %lu, power_on_sec = %lu\n"
2843 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2844 "#########################################################\n",
2845 di->voltage, di->current_avg,
2846 di->fcc, di->remain_capacity, di->voltage_ocv,
2847 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2849 di->ac_online, di->usb_online, di->psy_status,
2850 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2851 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2852 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2853 BASE_TO_SEC(di->chrg_normal_base),
2854 BASE_TO_SEC(di->dischrg_normal_base),
2855 BASE_TO_SEC(di->chrg_term_base),
2856 BASE_TO_SEC(di->chrg_emu_base),
2857 BASE_TO_SEC(di->dischrg_emu_base),
2858 BASE_TO_SEC(di->power_on_base),
2859 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2863 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2867 int dod0_to_soc100_min;
2869 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2870 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2872 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2873 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2874 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2875 di->dod0_capacity, dod0_to_soc100_min);
2877 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2878 (dod0_to_soc100_min < 1200)) {
2879 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2880 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2882 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2884 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2885 __func__, fcc0, di->fcc);
2887 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2888 di->dod0_status = 0;
2890 rk81x_bat_capacity_init(di, di->fcc);
2891 rk81x_bat_capacity_init_post(di);
2892 rk81x_bat_save_fcc(di, di->fcc);
2893 rk81x_bat_save_level(di, di->dod0_level);
2894 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2897 di->dod0_status = 0;
2901 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2903 if (di->chrg_status == CHARGE_FINISH) {
2904 di->debug_finish_real_soc = di->dsoc;
2905 di->debug_finish_temp_soc = di->rsoc;
2909 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2911 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2914 if ((di->chrg_status == CHARGE_FINISH) &&
2915 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2916 rk81x_bat_update_fcc(di);/* save new fcc*/
2917 ret = rk81x_bat_zero_current_calib(di);
2919 di->enter_finish = false;
2921 rk81x_bat_dbg_get_finish_soc(di);
2925 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2927 unsigned long sec_finish;
2928 int soc_time = 0, plus_soc;
2931 if (di->dsoc < 100) {
2932 if (!di->chrg_finish_base)
2933 di->chrg_finish_base = get_runtime_sec();
2935 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2937 temp = di->fcc * 3600 / 100;
2939 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2941 soc_time = temp / 450;
2943 plus_soc = sec_finish / soc_time;
2944 if (sec_finish > soc_time) {
2945 di->dsoc += plus_soc;
2946 di->chrg_finish_base = get_runtime_sec();
2947 di->chrg_save_sec = 0;
2949 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2950 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2951 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2955 static u8 rk81x_bat_get_valid_soc(unsigned long soc)
2957 return (soc <= 100) ? soc : 0;
2960 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2963 u32 soc_time, unit_sec;
2965 unsigned long chrg_normal_sec;
2967 now_current = rk81x_bat_get_avg_current(di);
2968 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2970 if (!di->chrg_normal_base)
2971 di->chrg_normal_base = get_runtime_sec();
2973 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2974 di->rsoc = rk81x_bat_get_rsoc(di);
2976 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2977 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2979 if (di->dsoc == di->rsoc) {
2980 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2981 di->rsoc = rk81x_bat_get_rsoc(di);
2982 di->chrg_normal_base = get_runtime_sec();
2983 di->chrg_save_sec = 0;
2984 /*di->charge_smooth_status = false;*/
2985 } else if (di->rsoc < di->dsoc + 1) {
2986 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
2987 unit_sec = soc_time * 3 / 2;
2988 plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
2989 if (chrg_normal_sec > unit_sec) {
2990 di->dsoc += plus_soc;
2991 di->chrg_normal_base = get_runtime_sec();
2992 di->chrg_save_sec = 0;
2994 di->charge_smooth_status = true;
2995 } else if (di->rsoc > di->dsoc + 1) {
2996 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
2997 unit_sec = soc_time * 3 / 4;
2998 plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
2999 if (chrg_normal_sec > unit_sec) {
3000 di->dsoc += plus_soc;
3001 di->chrg_normal_base = get_runtime_sec();
3002 di->chrg_save_sec = 0;
3004 di->charge_smooth_status = true;
3005 } else if (di->rsoc == di->dsoc + 1) {
3006 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3007 if (di->charge_smooth_status) {
3008 unit_sec = soc_time * 3 / 4;
3009 if (chrg_normal_sec > unit_sec) {
3010 di->dsoc = di->rsoc;
3011 di->chrg_normal_base = get_runtime_sec();
3012 di->charge_smooth_status = false;
3013 di->chrg_save_sec = 0;
3016 di->dsoc = di->rsoc;
3017 di->chrg_normal_base = get_runtime_sec();
3018 di->charge_smooth_status = false;
3019 di->chrg_save_sec = 0;
3023 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3024 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3025 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3026 chrg_normal_sec, soc_time, plus_soc);
3029 static void rk81x_bat_update_time(struct rk81x_battery *di)
3033 runtime_sec = get_runtime_sec();
3035 /*update by charger type*/
3036 if (rk81x_chrg_online(di))
3037 di->plug_out_base = runtime_sec;
3039 di->plug_in_base = runtime_sec;
3041 /*update by current*/
3042 if (di->chrg_status != CHARGE_FINISH) {
3043 di->finish_sig_base = runtime_sec;
3044 di->chrg_finish_base = runtime_sec;
3047 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3048 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3049 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3051 rk81x_bat_dbg_time_table(di);
3054 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3056 int trend_start_cap = di->trend_start_cap;
3057 int remain_cap = di->remain_capacity;
3061 if (di->s2r && !di->slp_psy_status)
3062 di->trend_start_cap = di->remain_capacity;
3064 diff_cap = remain_cap - trend_start_cap;
3065 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3066 __func__, trend_start_cap, diff_cap);
3067 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3069 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3070 di->trend_start_cap = di->remain_capacity;
3071 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3072 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3074 state = TREND_STAT_FLAT;
3080 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3082 int state, soc_time;
3083 static int trend_down_cnt, trend_up_cnt;
3084 int trend_cnt_thresd;
3085 int now_current = di->current_avg;
3088 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3089 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3090 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3091 __func__, state, trend_mult);
3092 if (di->chrg_status == CHARGE_FINISH)
3095 if (state == TREND_STAT_UP) {
3096 rk81x_bat_reset_zero_var(di);
3098 trend_up_cnt += trend_mult;
3099 if (trend_up_cnt >= trend_cnt_thresd) {
3101 di->dischrg_save_sec = 0;
3103 } else if (state == TREND_STAT_DOWN) {
3105 trend_down_cnt += trend_mult;
3106 if (trend_down_cnt >= trend_cnt_thresd) {
3108 di->chrg_save_sec = 0;
3112 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3113 if ((di->chrg_save_sec * 3 / 4 > soc_time) &&
3114 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3116 di->chrg_save_sec = 0;
3118 else if ((di->dischrg_save_sec * 3 / 4 > soc_time) &&
3119 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3121 di->dischrg_save_sec = 0;
3123 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3124 "up_cnt=%d, down_cnt=%d\n",
3125 __func__, state, trend_cnt_thresd, soc_time,
3126 trend_up_cnt, trend_down_cnt);
3129 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3131 u32 *ocv_table = di->pdata->battery_ocv;
3132 int delta_soc = di->rsoc - di->dsoc;
3134 if (di->chrg_status == CHARGE_FINISH ||
3135 di->slp_chrg_status == CHARGE_FINISH) {
3136 /*clear sleep charge status*/
3137 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3138 di->chrg_emu_base = 0;
3139 di->chrg_normal_base = 0;
3140 di->chrg_term_base = 0;
3141 rk81x_bat_finish_chrg(di);
3142 rk81x_bat_capacity_init(di, di->fcc);
3143 rk81x_bat_capacity_init_post(di);
3144 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3145 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3146 (di->voltage < ocv_table[18] + 20))) {
3147 di->chrg_emu_base = 0;
3148 di->chrg_normal_base = 0;
3149 di->chrg_finish_base = 0;
3150 rk81x_bat_terminal_chrg(di);
3151 } else if (di->chrg_status != CHARGE_FINISH &&
3152 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3153 di->chrg_term_base = 0;
3154 di->chrg_normal_base = 0;
3155 di->chrg_finish_base = 0;
3156 rk81x_bat_emulator_chrg(di);
3158 di->chrg_emu_base = 0;
3159 di->chrg_term_base = 0;
3160 di->chrg_finish_base = 0;
3161 rk81x_bat_normal_chrg(di);
3165 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3167 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3168 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3170 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3171 dischrg_normal_sec, dischrg_emu_sec);
3173 return (dischrg_normal_sec > dischrg_emu_sec) ?
3174 dischrg_normal_sec : dischrg_emu_sec;
3177 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3179 unsigned long sec1, sec2;
3180 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3181 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3182 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3183 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3185 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3186 chrg_normal_sec : chrg_term_sec;
3188 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3189 chrg_emu_sec : chrg_finish_sec;
3190 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3191 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3192 chrg_normal_sec, chrg_term_sec,
3193 chrg_emu_sec, chrg_finish_sec);
3195 return (sec1 > sec2) ? sec1 : sec2;
3198 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3200 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3201 if (di->current_mode == DISCHRG_MODE) {
3202 di->current_mode = CHRG_MODE;
3203 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3204 di->dischrg_normal_base = 0;
3205 di->dischrg_emu_base = 0;
3206 if (di->chrg_status == CHARGE_FINISH)
3207 di->dischrg_save_sec = 0;
3208 if ((di->chrg_status == CHARGE_FINISH) &&
3210 di->chrg_save_sec = 0;
3212 DBG("<%s>---dischrg_save_sec = %lu\n",
3213 __func__, di->dischrg_save_sec);
3216 if (!rk81x_chrg_online(di)) {
3217 dev_err(di->dev, "discharge, current error:%d\n",
3220 rk81x_bat_chrg_smooth(di);
3221 di->discharge_smooth_status = true;
3224 if (di->current_mode == CHRG_MODE) {
3225 di->current_mode = DISCHRG_MODE;
3226 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3227 di->chrg_normal_base = 0;
3228 di->chrg_emu_base = 0;
3229 di->chrg_term_base = 0;
3230 di->chrg_finish_base = 0;
3231 DBG("<%s>---chrg_save_sec = %lu\n",
3232 __func__, di->chrg_save_sec);
3234 rk81x_bat_dischrg_smooth(di);
3235 di->charge_smooth_status = true;
3240 * update rsoc by relax voltage
3242 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3244 int relax_vol = di->relax_voltage;
3245 int ocv_soc, capacity;
3247 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3248 capacity = (ocv_soc * di->fcc / 100);
3249 rk81x_bat_capacity_init(di, capacity);
3250 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3251 di->rsoc = rk81x_bat_get_rsoc(di);
3252 rk81x_bat_capacity_init_post(di);
3253 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3257 * 1: must do it, 0: when necessary
3259 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3261 int ocv_vol = di->est_ocv_vol;
3262 int ocv_soc = 0, capacity = 0;
3264 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3265 capacity = (ocv_soc * di->fcc / 100);
3266 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3267 rk81x_bat_capacity_init(di, capacity);
3268 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3269 di->rsoc = rk81x_bat_get_rsoc(di);
3270 rk81x_bat_capacity_init_post(di);
3271 DBG("<%s>, rsoc updated!\n", __func__);
3273 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3274 __func__, ocv_vol, ocv_soc, capacity);
3277 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3281 unsigned long sleep_sec = di->suspend_time_sum;
3282 int power_off_thresd = di->pdata->power_off_thresd;
3284 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3285 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3286 __func__, di->dsoc, di->rsoc,
3287 di->relax_voltage, di->voltage, sleep_sec / 60);
3289 if (di->relax_voltage >= di->voltage) {
3290 rk81x_bat_relax_vol_calib(di);
3291 rk81x_bat_restart_relax(di);
3293 /* current_avg < 0: make sure the system is not
3294 * wakeup by charger plugin.
3296 /* even if relax voltage is not caught rightly, realtime voltage
3297 * is quite close to relax voltage, we should not do nothing after
3301 rk81x_bat_vol_calib(di, 1);
3305 if (di->dsoc <= di->rsoc) {
3306 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3307 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3308 temp_dsoc = di->dsoc - delta_soc;
3310 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3311 di->rsoc, di->dsoc, delta_soc);
3313 if (delta_soc > 0) {
3314 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3316 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3318 else if (temp_dsoc > 5)
3319 di->dsoc = temp_dsoc;
3322 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3323 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3325 /*di->dsoc > di->rsoc*/
3326 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3327 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3328 temp_dsoc = di->dsoc - di->rsoc;
3330 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3331 di->rsoc, di->dsoc, delta_soc);
3333 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3334 (temp_dsoc > delta_soc))
3335 di->dsoc -= delta_soc;
3337 di->dsoc = di->rsoc;
3339 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3340 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3343 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3349 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3350 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3355 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3358 unsigned long sleep_sec;
3360 sleep_sec = di->suspend_time_sum;
3361 if (((di->suspend_charge_current < 800) &&
3362 (di->ac_online == ONLINE)) ||
3363 (di->chrg_status == CHARGE_FINISH)) {
3364 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3365 if (sleep_sec > 0) {
3366 /*default charge current: 1000mA*/
3367 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3368 / 3600 / div(di->fcc);
3371 DBG("<%s>, usb charge\n", __func__);
3378 * only do report when there is a change.
3380 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3381 * when dsoc == 0, we must do report. But it will generate too much android
3382 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3385 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3388 static u32 old_ac_status;
3389 static u32 old_usb_status;
3390 static u32 old_charge_status;
3393 state_changed = false;
3394 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3395 state_changed = true;
3396 else if (di->dsoc != old_soc)
3397 state_changed = true;
3398 else if (di->ac_online != old_ac_status)
3399 state_changed = true;
3400 else if (di->usb_online != old_usb_status)
3401 state_changed = true;
3402 else if (old_charge_status != di->psy_status)
3403 state_changed = true;
3405 if (rk81x_chrg_online(di)) {
3406 if (di->dsoc == 100)
3407 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3409 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3412 if (state_changed) {
3413 power_supply_changed(&di->bat);
3414 power_supply_changed(&di->usb);
3415 power_supply_changed(&di->ac);
3417 old_ac_status = di->ac_online;
3418 old_usb_status = di->usb_online;
3419 old_charge_status = di->psy_status;
3420 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3421 di->dsoc, di->rsoc);
3426 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3430 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3433 return CHRG_CVCC_HOUR[hour];
3436 /* we have to estimate the charging finish time from now, to decide
3437 * whether we should reset the timer or not.
3439 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3442 int remain_capacity;
3444 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3445 if (di->dsoc < di->rsoc)
3446 remain_capacity = di->dsoc * di->fcc / 100;
3448 remain_capacity = di->remain_capacity;
3450 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3451 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3453 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3454 di->chrg_cap2full = di->fcc - remain_capacity;
3455 if (di->current_avg <= 0)
3456 di->current_avg = 1;
3458 di->chrg_time2full = di->chrg_cap2full * 3600 /
3459 div(abs(di->current_avg));
3461 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3462 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3465 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3466 /*rk81x_bat_init_chrg_timer(di);*/
3467 di->plug_in_base = get_runtime_sec();
3468 DBG("%s: reset charge timer\n", __func__);
3475 * in case that we will do reboot stress test, we need a special way
3476 * to ajust the dsoc.
3478 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3486 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3489 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3490 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3492 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3493 __func__, cnt, unit_time, smooth_time,
3494 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3496 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3497 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3498 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3503 rk81x_bat_save_dsoc(di, dsoc);
3506 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3507 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3512 rk81x_bat_save_dsoc(di, dsoc);
3516 rk81x_bat_save_reboot_cnt(di, cnt);
3519 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3524 uint16_t cal_offset;
3525 u8 pcb_offset = DEF_PCB_OFFSET;
3527 min = BASE_TO_MIN(di->power_on_base);
3528 if ((min % 8) && (old_min != min)) {
3530 rk81x_bat_get_vol_offset(di);
3531 if (di->pcb_ioffset_updated)
3532 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3534 current_offset = rk81x_bat_get_ioffset(di);
3535 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3536 cal_offset = rk81x_bat_get_cal_offset(di);
3537 if (cal_offset < 0x7ff)
3538 rk81x_bat_set_cal_offset(di, di->current_offset +
3540 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3541 __func__, di->voltage_k, di->voltage_b, cal_offset,
3542 rk81x_bat_get_ioffset(di));
3546 static void rk81x_bat_update_info(struct rk81x_battery *di)
3550 else if (di->dsoc < 0)
3554 * we need update fcc in continuous charging state, if discharge state
3555 * keep at least 2 hour, we decide not to update fcc, so clear the
3556 * fcc update flag: dod0_status.
3558 if (BASE_TO_MIN(di->plug_out_base) > 120)
3559 di->dod0_status = 0;
3561 di->voltage = rk81x_bat_get_vol(di);
3562 di->current_avg = rk81x_bat_get_avg_current(di);
3563 di->chrg_status = rk81x_bat_get_chrg_status(di);
3564 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3565 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3566 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3567 /*rk81x_bat_chrg_over_time_check(di);*/
3568 rk81x_bat_update_calib_param(di);
3569 if (di->chrg_status == CC_OR_CV)
3570 di->enter_finish = true;
3572 if (!rk81x_chrg_online(di) && di->s2r)
3575 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3576 if (di->remain_capacity > di->fcc) {
3577 rk81x_bat_capacity_init(di, di->fcc);
3578 rk81x_bat_capacity_init_post(di);
3579 di->remain_capacity = di->fcc;
3582 di->rsoc = rk81x_bat_get_rsoc(di);
3585 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3587 if (di->slp_psy_status)
3588 return rk81x_bat_sleep_chrg(di);
3590 return rk81x_bat_sleep_dischrg(di);
3593 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3595 u8 ocv_soc, soc_level;
3596 int relax_vol = di->relax_voltage;
3601 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3602 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3603 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3605 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3606 (di->dod0_status == 0) &&
3608 di->dod0_voltage = relax_vol;
3609 di->dod0_capacity = di->temp_nac;
3614 di->dod0_level = 100;
3615 else if (ocv_soc < 5)
3616 di->dod0_level = 90;
3618 di->dod0_level = 80;
3620 /* save_soc = di->dod0_level; */
3621 soc_level = rk81x_bat_get_level(di);
3622 if (soc_level > di->dod0_level) {
3623 di->dod0_status = 0;
3625 di->dod0_status = 1;
3627 di->fcc_update_sec = get_runtime_sec();
3630 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3631 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3633 di->dod0_voltage, di->dod0_capacity,
3634 ocv_soc, soc_level, di->dod0_status,
3639 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3644 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3647 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3649 dev_info(di->dev, "set charge to %s termination mode\n",
3650 mode ? "digital" : "analog");
3653 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3655 struct rk81x_battery *di;
3657 di = container_of(work, struct rk81x_battery,
3658 chrg_term_mode_switch_work.work);
3660 if (rk81x_chrg_online(di))
3661 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3663 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3666 static void rk81x_battery_work(struct work_struct *work)
3668 struct rk81x_battery *di;
3669 int ms = TIMER_MS_COUNTS;
3671 di = container_of(work, struct rk81x_battery,
3672 battery_monitor_work.work);
3673 if (rk81x_chrg_online(di)) {
3674 rk81x_bat_wait_finish_sig(di);
3675 /*rk81x_bat_chrg_finish_routine(di);*/
3677 rk81x_bat_fcc_flag_check(di);
3678 rk81x_bat_arbitrate_rsoc_trend(di);
3679 rk81x_bat_display_smooth(di);
3680 rk81x_bat_update_time(di);
3681 rk81x_bat_update_info(di);
3682 rk81x_bat_rsoc_check(di);
3683 rk81x_bat_power_supply_changed(di);
3684 rk81x_bat_save_dsoc(di, di->dsoc);
3685 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3687 rk81x_bat_dbg_dmp_info(di);
3689 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3690 ms = 30 * TIMER_MS_COUNTS;
3692 di->early_resume = 0;
3696 queue_delayed_work(di->wq, &di->battery_monitor_work,
3697 msecs_to_jiffies(ms));
3700 #if defined(CONFIG_ARCH_ROCKCHIP)
3701 static void rk81x_battery_otg_delay_work(struct work_struct *work)
3703 struct rk81x_battery *di = container_of(work,
3704 struct rk81x_battery, otg_check_work.work);
3706 enum bc_port_type event = di->charge_otg;
3708 /* do not touch CHRG_CTRL_REG1[7]: CHRG_EN, hardware can
3709 * recognize otg plugin and will auto ajust this bit
3712 case USB_OTG_POWER_ON:
3713 dev_info(di->dev, "charge disable, otg enable\n");
3714 di->otg_online = ONLINE;
3715 /*rk81x_bat_set_charger_param(di, NO_CHARGER);*/
3716 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3717 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3718 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_EN);
3721 case USB_OTG_POWER_OFF:
3722 dev_info(di->dev, "charge enable, otg disable\n");
3723 di->otg_online = OFFLINE;
3724 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3725 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3726 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_DIS);
3727 /*maybe dc still plugin*/
3728 queue_delayed_work(di->wq, &di->dc_det_check_work,
3729 msecs_to_jiffies(10));
3737 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3739 int register_battery_notifier(struct notifier_block *nb)
3741 return blocking_notifier_chain_register(&battery_chain_head, nb);
3743 EXPORT_SYMBOL_GPL(register_battery_notifier);
3745 int unregister_battery_notifier(struct notifier_block *nb)
3747 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3749 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3751 int battery_notifier_call_chain(unsigned long val)
3753 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3754 == NOTIFY_BAD) ? -EINVAL : 0;
3756 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3758 static int rk81x_bat_usb_notifier_call(struct notifier_block *nb,
3759 unsigned long event, void *data)
3761 enum charger_type charger_type;
3762 struct rk81x_battery *di =
3763 container_of(nb, struct rk81x_battery, battery_nb);
3765 if (di->fg_drv_mode == TEST_POWER_MODE)
3768 /*if dc is pluging, ignore usb*/
3769 charger_type = rk81x_bat_get_dc_state(di);
3770 if ((charger_type == DC_CHARGER) &&
3771 (event != USB_OTG_POWER_OFF) &&
3772 (event != USB_OTG_POWER_ON))
3776 case USB_BC_TYPE_DISCNT:/*maybe dc still plugin*/
3777 queue_delayed_work(di->wq, &di->dc_det_check_work,
3778 msecs_to_jiffies(10));
3780 case USB_BC_TYPE_SDP:
3781 case USB_BC_TYPE_CDP:/*nonstandard charger*/
3782 case USB_BC_TYPE_DCP:/*standard charger*/
3783 queue_delayed_work(di->wq, &di->ac_usb_check_work,
3784 msecs_to_jiffies(10));
3786 case USB_OTG_POWER_ON:/*otg on*/
3787 di->charge_otg = USB_OTG_POWER_ON;
3788 queue_delayed_work(di->wq, &di->otg_check_work,
3789 msecs_to_jiffies(10));
3791 case USB_OTG_POWER_OFF:/*otg off*/
3792 di->charge_otg = USB_OTG_POWER_OFF;
3793 queue_delayed_work(di->wq, &di->otg_check_work,
3794 msecs_to_jiffies(10));
3802 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3804 pr_info("\n------- %s:lower power warning!\n", __func__);
3806 rk_send_wakeup_key();
3811 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3813 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3814 rk_send_wakeup_key();
3818 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3820 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3821 rk_send_wakeup_key();
3825 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3827 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3829 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3830 di->finish_sig_base = get_runtime_sec();
3831 rk_send_wakeup_key();
3835 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3837 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3839 queue_delayed_work(di->wq,
3840 &di->dc_det_check_work,
3841 msecs_to_jiffies(10));
3842 rk_send_wakeup_key();
3847 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3852 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3853 ret = sysfs_create_file(&di->bat.dev->kobj,
3854 &rk818_bat_attr[i].attr);
3856 dev_err(di->dev, "create battery node(%s) error\n",
3857 rk818_bat_attr[i].attr.name);
3863 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3865 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3867 struct rk818 *chip = di->rk818;
3869 #if defined(CONFIG_X86_INTEL_SOFIA)
3870 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3871 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3872 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3873 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3875 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3876 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3877 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3878 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3881 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3882 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3884 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3886 di->irq = vb_lo_irq;
3887 enable_irq_wake(di->irq);
3889 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3890 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3893 dev_err(chip->dev, "plug_in_irq request failed!\n");
3895 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3896 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3899 dev_err(chip->dev, "plug_out_irq request failed!\n");
3901 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3902 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3905 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3908 static void rk81x_bat_info_init(struct rk81x_battery *di,
3912 unsigned long time_base = get_runtime_sec();
3914 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3915 if (val & PLUG_IN_STS)
3916 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3918 di->cell.config = di->pdata->cell_cfg;
3919 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3920 di->qmax = di->pdata->cell_cfg->design_qmax;
3921 di->early_resume = 1;
3922 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3923 di->bat_res = di->pdata->sense_resistor_mohm;
3924 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3925 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3926 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3927 di->loader_charged = loader_charged;
3928 di->chrg_finish_base = time_base;
3929 di->power_on_base = time_base;
3930 di->plug_in_base = time_base;
3931 di->plug_out_base = time_base;
3932 di->finish_sig_base = time_base;
3933 di->fcc = rk81x_bat_get_fcc(di);
3936 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3937 struct device_node *np)
3939 struct device *dev = di->dev;
3940 enum of_gpio_flags flags;
3943 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3944 if (di->dc_det_pin == -EPROBE_DEFER) {
3945 dev_err(dev, "dc_det_gpio error\n");
3949 if (gpio_is_valid(di->dc_det_pin)) {
3950 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3951 RK818_DC_IN : RK818_DC_OUT;
3952 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3954 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3955 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3956 "rk81x_dc_det", di);
3958 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3961 enable_irq_wake(di->dc_det_irq);
3966 gpio_free(di->dc_det_pin);
3969 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3974 struct timespec tv = {
3975 .tv_nsec = NSEC_PER_SEC >> 1,
3977 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3979 err = rtc_read_time(rtc, &tm);
3981 dev_err(rtc->dev.parent,
3982 "hctosys: unable to read the hardware clock\n");
3985 err = rtc_valid_tm(&tm);
3987 dev_err(rtc->dev.parent,
3988 "hctosys: invalid date/time\n");
3992 rtc_tm_to_time(&tm, &tv.tv_sec);
3993 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3995 return (delta_sec > 0) ? delta_sec : 0;
3999 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4001 struct device_node *np;
4002 struct battery_platform_data *pdata;
4003 struct cell_config *cell_cfg;
4004 struct ocv_config *ocv_cfg;
4005 struct property *prop;
4006 struct rk818 *rk818 = di->rk818;
4007 struct device *dev = di->dev;
4012 np = of_find_node_by_name(rk818->dev->of_node, "battery");
4014 dev_err(dev, "battery node not found!\n");
4018 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
4022 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
4026 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4030 prop = of_find_property(np, "ocv_table", &length);
4032 dev_err(dev, "ocv_table not found!\n");
4035 pdata->ocv_size = length / sizeof(u32);
4036 if (pdata->ocv_size <= 0) {
4037 dev_err(dev, "invalid ocv table\n");
4041 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4043 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4044 if (!pdata->battery_ocv)
4047 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4052 /******************** charger param ****************************/
4053 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4055 dev_err(dev, "max_chrg_currentmA not found!\n");
4056 out_value = DEFAULT_CHRG_CUR;
4058 pdata->max_charger_currentmA = out_value;
4060 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4062 dev_err(dev, "max_charger_ilimitmA not found!\n");
4063 out_value = DEFAULT_INPUT_CUR;
4065 pdata->max_charger_ilimitmA = out_value;
4067 ret = of_property_read_u32(np, "bat_res", &out_value);
4069 dev_err(dev, "bat_res not found!\n");
4070 out_value = DEFAULT_BAT_RES;
4072 pdata->sense_resistor_mohm = out_value;
4074 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4076 dev_err(dev, "max_charge_voltagemV not found!\n");
4077 out_value = DEFAULT_CHRG_VOL;
4079 pdata->max_charger_voltagemV = out_value;
4081 ret = of_property_read_u32(np, "design_capacity", &out_value);
4083 dev_err(dev, "design_capacity not found!\n");
4086 cell_cfg->design_capacity = out_value;
4088 ret = of_property_read_u32(np, "design_qmax", &out_value);
4090 dev_err(dev, "design_qmax not found!\n");
4093 cell_cfg->design_qmax = out_value;
4095 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4097 dev_err(dev, "sleep_enter_current not found!\n");
4098 out_value = DEFAULT_SLP_ENTER_CUR;
4100 ocv_cfg->sleep_enter_current = out_value;
4102 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4104 dev_err(dev, "sleep_exit_current not found!\n");
4105 out_value = DEFAULT_SLP_EXIT_CUR;
4107 ocv_cfg->sleep_exit_current = out_value;
4109 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4111 dev_warn(dev, "power_off_thresd not found!\n");
4112 out_value = PWR_OFF_THRESD;
4114 pdata->power_off_thresd = out_value;
4116 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4117 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4118 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4120 /************* charger support adp types **********************/
4121 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4122 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4124 if (!support_usb_adp && !support_dc_adp) {
4125 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4126 support_usb_adp = 1;
4129 /*if (support_dc_adp)*/
4130 rk81x_bat_dc_det_init(di, np);
4132 cell_cfg->ocv = ocv_cfg;
4133 pdata->cell_cfg = cell_cfg;
4136 DBG("\nthe battery dts info dump:\n"
4138 "max_input_currentmA:%d\n"
4139 "max_chrg_currentmA:%d\n"
4140 "max_charge_voltagemV:%d\n"
4141 "design_capacity:%d\n"
4143 "sleep_enter_current:%d\n"
4144 "sleep_exit_current:%d\n"
4145 "support_usb_adp:%d\n"
4146 "support_dc_adp:%d\n"
4147 "power_off_thresd:%d\n",
4148 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4149 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4150 cell_cfg->design_capacity, cell_cfg->design_qmax,
4151 cell_cfg->ocv->sleep_enter_current,
4152 cell_cfg->ocv->sleep_exit_current,
4153 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4159 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4165 static int rk81x_battery_probe(struct platform_device *pdev)
4167 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4168 struct rk81x_battery *di;
4171 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4175 di->dev = &pdev->dev;
4176 platform_set_drvdata(pdev, di);
4178 ret = rk81x_bat_parse_dt(di);
4180 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4184 rk81x_bat_info_init(di, chip);
4185 if (!is_rk81x_bat_exist(di)) {
4186 dev_info(di->dev, "not battery, enter test power mode\n");
4187 di->fg_drv_mode = TEST_POWER_MODE;
4190 ret = rk81x_bat_power_supply_init(di);
4192 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4196 rk81x_bat_irq_init(di);
4197 rk81x_bat_sysfs_init(di);
4199 rk81x_bat_fg_init(di);
4200 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4202 rk81x_bat_flatzone_vol_init(di);
4204 #if defined(CONFIG_X86_INTEL_SOFIA)
4205 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4206 if (IS_ERR_OR_NULL(di->usb_phy)) {
4207 dev_err(di->dev, "get usb phy failed\n");
4208 return PTR_ERR(di->usb_phy);
4210 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4211 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4213 dev_err(di->dev, "registr usb phy notification failed\n");
4214 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4215 rk81x_battery_usb_notifier_delayed_work);
4218 rk81x_battery_register_fb_notify(di);
4219 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4220 "rk81x-battery-work");
4221 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4222 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4223 rk81x_chrg_term_mode_switch_work);
4225 queue_delayed_work(di->wq, &di->battery_monitor_work,
4226 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4228 #if defined(CONFIG_ARCH_ROCKCHIP)
4229 INIT_DELAYED_WORK(&di->otg_check_work,
4230 rk81x_battery_otg_delay_work);
4231 INIT_DELAYED_WORK(&di->ac_usb_check_work,
4232 rk81x_battery_acusb_delay_work);
4233 INIT_DELAYED_WORK(&di->dc_det_check_work,
4234 rk81x_battery_dc_delay_work);
4236 queue_delayed_work(di->wq, &di->dc_det_check_work,
4237 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4239 di->battery_nb.notifier_call = rk81x_bat_usb_notifier_call;
4240 rk_bc_detect_notifier_register(&di->battery_nb, &di->charge_otg);
4242 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4247 static int rk81x_battery_suspend(struct platform_device *dev,
4250 struct rk81x_battery *di = platform_get_drvdata(dev);
4252 /*while otg and dc both plugin*/
4253 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4255 di->slp_psy_status = rk81x_chrg_online(di);
4256 di->chrg_status = rk81x_bat_get_chrg_status(di);
4257 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4258 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4259 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4260 di->dischrg_normal_base = 0;
4261 di->dischrg_emu_base = 0;
4262 do_gettimeofday(&di->suspend_rtc_base);
4264 if (!rk81x_chrg_online(di)) {
4265 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4266 di->chrg_normal_base = 0;
4267 di->chrg_emu_base = 0;
4268 di->chrg_term_base = 0;
4269 di->chrg_finish_base = 0;
4274 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4275 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4276 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4281 static int rk81x_battery_resume(struct platform_device *dev)
4283 struct rk81x_battery *di = platform_get_drvdata(dev);
4284 int pwroff_thresd = di->pdata->power_off_thresd;
4290 /*while otg and dc both plugin*/
4291 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4293 di->discharge_smooth_status = true;
4294 di->charge_smooth_status = true;
4296 vol = rk81x_bat_get_vol(di);
4297 if (vol < INVALID_VOL_THRESD) {
4298 dev_err(di->dev, "invalid voltage :%d", vol);
4303 di->current_avg = rk81x_bat_get_avg_current(di);
4304 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4305 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4306 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4307 delta_time = rk81x_bat_get_suspend_sec(di);
4308 di->suspend_time_sum += delta_time;
4309 #if defined(CONFIG_ARCH_ROCKCHIP)
4310 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4313 if (di->slp_psy_status) {
4314 time_step = CHRG_TIME_STEP;
4316 if (di->voltage <= pwroff_thresd + 50)
4317 time_step = DISCHRG_TIME_STEP_0;
4319 time_step = DISCHRG_TIME_STEP_1;
4322 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4323 di->current_avg, di->voltage, di->est_ocv_vol,
4324 di->relax_voltage, delta_time, di->suspend_time_sum,
4325 di->slp_psy_status);
4327 if (di->suspend_time_sum > time_step) {
4328 delta_soc = rk81x_bat_update_resume_state(di);
4330 di->suspend_time_sum = 0;
4333 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4334 rk81x_chrg_online(di))
4335 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4339 static int rk81x_battery_remove(struct platform_device *dev)
4341 struct rk81x_battery *di = platform_get_drvdata(dev);
4343 cancel_delayed_work_sync(&di->battery_monitor_work);
4347 static void rk81x_battery_shutdown(struct platform_device *dev)
4349 struct rk81x_battery *di = platform_get_drvdata(dev);
4351 cancel_delayed_work_sync(&di->battery_monitor_work);
4352 rk_bc_detect_notifier_unregister(&di->battery_nb);
4354 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4355 rk81x_bat_check_reboot(di);
4357 rk81x_bat_save_reboot_cnt(di, 0);
4358 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4361 static struct platform_driver rk81x_battery_driver = {
4363 .name = "rk818-battery",
4364 .owner = THIS_MODULE,
4367 .probe = rk81x_battery_probe,
4368 .remove = rk81x_battery_remove,
4369 .suspend = rk81x_battery_suspend,
4370 .resume = rk81x_battery_resume,
4371 .shutdown = rk81x_battery_shutdown,
4374 static int __init battery_init(void)
4376 return platform_driver_register(&rk81x_battery_driver);
4379 fs_initcall_sync(battery_init);
4380 static void __exit battery_exit(void)
4382 platform_driver_unregister(&rk81x_battery_driver);
4384 module_exit(battery_exit);
4386 MODULE_LICENSE("GPL");
4387 MODULE_ALIAS("platform:rk818-battery");
4388 MODULE_AUTHOR("ROCKCHIP");