2 * rk818/rk819 battery driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd
5 * Author: zhangqing <zhangqing@rock-chips.com>
6 * chenjh <chenjh@rock-chips.com>
7 * Andy Yan <andy.yan@rock-chips.com>
9 * Copyright (C) 2008-2009 Texas Instruments, Inc.
10 * Author: Texas Instruments, Inc.
12 * Copyright (C) 2008-2009 Texas Instruments, Inc.
13 * Author: Texas Instruments, Inc.
14 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
15 * Author: zhangqing <zhangqing@rock-chips.com>
16 * Copyright (C) 2014-2015 Intel Mobile Communications GmbH
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
40 #include <linux/gpio.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
43 #include <linux/mfd/rk818.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/rtc.h>
47 #include <linux/wakelock.h>
48 #include <linux/of_gpio.h>
49 #include <linux/irq.h>
50 #include <linux/irqdomain.h>
51 #include <linux/usb/phy.h>
54 #if defined(CONFIG_X86_INTEL_SOFIA)
55 #include <linux/usb/phy-intel.h>
57 #include "rk818_battery.h"
59 /* if you want to disable, don't set it as 0,
60 just be: "static int dbg_enable;" is ok*/
62 static int dbg_enable;
63 #define RK818_SYS_DBG 1
65 module_param_named(dbg_level, dbg_enable, int, 0644);
67 #define DBG(args...) \
74 #define DEFAULT_BAT_RES 135
75 #define DEFAULT_CHRG_VOL 4200
76 #define DEFAULT_CHRG_CUR 1000
77 #define DEFAULT_INPUT_CUR 1400
78 #define DEFAULT_SLP_ENTER_CUR 600
79 #define DEFAULT_SLP_EXIT_CUR 600
81 #define DSOC_DISCHRG_EMU_CURR 1200
82 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
83 #define DSOC_DISCHRG_FAST_EER_RANGE 10
84 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
85 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
86 #define DSOC_CHRG_FAST_EER_RANGE 10
87 #define DSOC_CHRG_EMU_CURR 1200
88 #define DSOC_CHRG_TERM_CURR 600
89 #define DSOC_CHRG_TERM_VOL 4100
90 #define CHRG_FINISH_VOL 4100
92 /*realtime RSOC calib param*/
93 #define RSOC_DISCHRG_ERR_LOWER 40
94 #define RSOC_DISCHRG_ERR_UPPER 50
95 #define RSOC_ERR_CHCK_CNT 15
96 #define RSOC_COMPS 20 /*compensation*/
97 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
98 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
100 #define RSOC_RESUME_ERR 10
101 #define REBOOT_INTER_MIN 1
103 #define INTERPOLATE_MAX 1000
104 #define MAX_INT 0x7FFF
105 #define TIME_10MIN_SEC 600
107 #define CHRG_VOL_SHIFT 4
108 #define CHRG_ILIM_SHIFT 0
109 #define CHRG_ICUR_SHIFT 0
110 #define DEF_CHRG_VOL CHRG_VOL4200
111 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
112 #define DEF_CHRG_CURR_LMT ILIM_2000MA
114 /*TEST_POWER_MODE params*/
115 #define TEST_CURRENT 1000
116 #define TEST_VOLTAGE 3800
118 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
119 #define TEST_PRESET 1
120 #define TEST_AC_ONLINE 1
121 #define TEST_USB_ONLINE 0
123 #define ZERO_ALGOR_THRESD 3800
124 #define DISCHRG_ZERO_MODE 1
125 #define DISCHRG_NORMAL_MODE 0
126 #define DEF_LAST_ZERO_MODE_SOC -1
128 #define DISCHRG_MODE 0
131 #define TREND_STAT_FLAT 0
132 #define TREND_STAT_DOWN -1
133 #define TREND_STAT_UP 1
134 #define TREND_CAP_DIFF 5
138 #define SLP_CURR_MAX 40
139 #define SLP_CURR_MIN 6
140 #define WAKEUP_SEC_THRESD 40
141 #define CHRG_TIME_STEP (60)
142 #define DISCHRG_TIME_STEP_0 (30 * 60)
143 #define DISCHRG_TIME_STEP_1 (60 * 60)
145 #define DEF_PCB_OFFSET 42
146 #define DEF_CAL_OFFSET 0x832
147 #define DEF_PWRPATH_RES 50
148 #define SEC_TO_EMPTY 300
149 #define DSOC_CHRG_FINISH_CURR 1100
150 #define SLP_CHRG_CURR 1000
151 #define SLP_DSOC_VOL_THRESD 3600
152 #define PWR_OFF_THRESD 3400
153 #define MIN_ZERO_ACCURACY 10 /*0.01%*/
155 #define MAX_FCC 10000
158 * the following table value depends on datasheet
160 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
162 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
163 2250, 2400, 2600, 2800, 3000};
165 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
166 2000, 2250, 2500, 2750, 3000};
168 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
170 #define RK818_DC_IN 0
171 #define RK818_DC_OUT 1
173 #define OCV_VALID_SHIFT (0)
174 #define OCV_CALIB_SHIFT (1)
175 #define FIRST_PWRON_SHIFT (2)
177 #define SEC_TO_MIN(x) ((x) / 60)
179 struct rk81x_battery {
181 struct cell_state cell;
182 struct power_supply bat;
183 struct power_supply ac;
184 struct power_supply usb;
185 struct delayed_work work;
187 struct pinctrl *pinctrl;
188 struct pinctrl_state *pins_default;
190 struct battery_platform_data *pdata;
203 uint16_t voltage_ocv;
204 uint16_t relax_voltage;
210 bool pcb_ioffset_updated;
227 int bat_res_update_cnt;
233 unsigned long dod0_time;
246 int current_k;/* (ICALIB0, ICALIB1) */
249 int voltage_k;/* VCALIB0 VCALIB1 */
252 int zero_timeout_cnt;
253 int zero_old_remain_cap;
258 int charge_smooth_time;
262 unsigned long suspend_time_sum;
266 int suspend_charge_current;
269 bool charge_smooth_status;
270 bool discharge_smooth_status;
276 struct notifier_block battery_nb;
277 struct usb_phy *usb_phy;
278 struct notifier_block usb_nb;
279 struct notifier_block fb_nb;
282 int s2r; /*suspend to resume*/
283 struct workqueue_struct *wq;
284 struct delayed_work battery_monitor_work;
285 struct delayed_work charge_check_work;
286 struct delayed_work usb_phy_delay_work;
287 struct delayed_work chrg_term_mode_switch_work;
291 struct wake_lock resume_wake_lock;
292 unsigned long plug_in_base;
293 unsigned long plug_out_base;
294 unsigned long finish_sig_base;
295 unsigned long power_on_base;
300 bool is_first_poweron;
303 int debug_finish_real_soc;
304 int debug_finish_temp_soc;
310 unsigned long wakeup_sec;
311 u32 delta_vol_smooth;
312 unsigned long dischrg_normal_base;
313 unsigned long dischrg_emu_base;
314 unsigned long chrg_normal_base;
315 unsigned long chrg_term_base;
316 unsigned long chrg_emu_base;
317 unsigned long chrg_finish_base;
318 unsigned long fcc_update_sec;
320 u8 dischrg_algorithm_mode;
321 int last_zero_mode_dsoc;
323 unsigned long dischrg_save_sec;
324 unsigned long chrg_save_sec;
325 struct timeval suspend_rtc_base;
328 u32 support_usb_adp, support_dc_adp;
330 #define to_device_info(x) container_of((x), \
331 struct rk81x_battery, bat)
333 #define to_ac_device_info(x) container_of((x), \
334 struct rk81x_battery, ac)
336 #define to_usb_device_info(x) container_of((x), \
337 struct rk81x_battery, usb)
339 static int loader_charged;
341 static int __init rk81x_bat_loader_charged(char *__unused)
345 pr_info("battery charged in loader\n");
349 __setup("loader_charged", rk81x_bat_loader_charged);
351 static u64 g_base_sec;
352 static u64 get_runtime_sec(void)
354 u64 ts_ns = local_clock();
356 do_div(ts_ns, 1000000000);
358 return ts_ns + g_base_sec;
361 static u64 is_local_clock_reset(void)
363 u64 ts_ns = local_clock();
365 do_div(ts_ns, 1000000000);
370 static inline unsigned long BASE_TO_SEC(unsigned long x)
373 return get_runtime_sec() - x;
378 static inline unsigned long BASE_TO_MIN(unsigned long x)
380 return BASE_TO_SEC(x) / 60;
383 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
388 case HW_ADP_TYPE_USB:
396 case HW_ADP_TYPE_DUAL:
397 if (support_usb_adp && support_dc_adp)
407 static bool rk81x_chrg_online(struct rk81x_battery *di)
409 return di->usb_online || di->ac_online;
412 static u32 interpolate(int value, u32 *table, int size)
417 for (i = 0; i < size; i++) {
418 if (value < table[i])
422 if ((i > 0) && (i < size)) {
423 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
424 d /= table[i] - table[i-1];
425 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
427 d = i * ((INTERPOLATE_MAX + size / 2) / size);
436 /* Returns (a * b) / c */
437 static int32_t ab_div_c(u32 a, u32 b, u32 c)
443 sign = ((((a^b)^c) & 0x80000000) != 0);
449 tmp = (a * b + (c >> 1)) / c;
461 static int div(int val)
463 return (val == 0) ? 1 : val;
466 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
467 u8 buf[], unsigned len)
472 for (i = 0; ret < 0 && i < 3; i++) {
473 ret = rk818_i2c_read(di->rk818, reg, len, buf);
475 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
478 return (ret < 0) ? ret : 0;
481 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
482 u8 const buf[], unsigned len)
487 for (i = 0; ret < 0 && i < 3; i++) {
488 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
490 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
493 return (ret < 0) ? ret : 0;
496 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
501 for (i = 0; ret < 0 && i < 3; i++) {
502 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
504 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
510 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
515 for (i = 0; ret < 0 && i < 3; i++) {
516 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
518 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
524 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
529 rk81x_bat_read(di, reg, &buf, 1);
530 val = (buf & BIT(shift)) >> shift;
534 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
539 DBG("%s dump charger register start:\n", __func__);
540 for (i = 0xAC; i < 0xEE; i++) {
541 rk81x_bat_read(di, i, &buf, 1);
542 DBG("0x%02x : 0x%02x\n", i, buf);
547 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
552 DBG("%s dump the register start:\n", __func__);
553 for (i = 0x99; i < 0xAB; i++) {
554 rk81x_bat_read(di, i, &buf, 1);
555 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
560 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
562 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
563 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
566 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
568 rk81x_bat_reset_zero_var(di);
569 di->trend_start_cap = di->remain_capacity;
572 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
578 delta_cap = capacity - di->remain_capacity;
582 di->adjust_cap += delta_cap;
584 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
586 buf = (capacity_ma >> 24) & 0xff;
587 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
588 buf = (capacity_ma >> 16) & 0xff;
589 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
590 buf = (capacity_ma >> 8) & 0xff;
591 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
592 buf = (capacity_ma & 0xff) | 0x01;
593 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
594 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
598 dev_info(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 voltage_now = 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_dbg(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_warn(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 - 50;
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");
1275 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1279 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1281 return (buf & 0x80) ? 1 : 0;
1284 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1289 rk81x_bat_read(di, GGSTS, &buf, 1);
1290 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1291 /*di->pwron_bat_con = buf;*/
1295 rk81x_bat_write(di, GGSTS, &buf, 1);
1296 rk81x_bat_read(di, GGSTS, &temp, 1);
1297 } while (temp & BAT_CON);
1304 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1311 ocv_table = di->pdata->battery_ocv;
1312 ocv_size = di->pdata->ocv_size;
1314 for (j = 0; j < 21; j++)
1318 for (i = 1; i < ocv_size-1; i++) {
1319 if (ocv_table[i+1] < ocv_table[i] + 20)
1320 temp_table[j++] = i;
1323 temp_table[j] = temp_table[j-1] + 1;
1325 di->enter_flatzone = ocv_table[i];
1328 for (i = 0; i < 20; i++) {
1329 if (temp_table[i] < temp_table[i+1])
1334 di->exit_flatzone = ocv_table[i];
1336 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1337 di->enter_flatzone, di->exit_flatzone);
1340 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1342 u8 ocv_valid, first_pwron;
1346 /*buf==1: OCV_VOL is valid*/
1347 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1348 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1349 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1351 if (first_pwron == 1 || ocv_valid == 1) {
1352 DBG("<%s> enter.\n", __func__);
1353 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1354 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1355 di->dod0_voltage = ocv_voltage;
1356 di->dod0_capacity = di->temp_nac;
1361 di->dod0_level = 100;
1362 else if (ocv_soc < 5)
1363 di->dod0_level = 95;
1364 else if (ocv_soc < 10)
1365 di->dod0_level = 90;
1367 di->dod0_level = 80;
1368 /* save_soc = di->dod0_level; */
1369 soc_level = rk81x_bat_get_level(di);
1370 if (soc_level > di->dod0_level) {
1371 di->dod0_status = 0;
1373 if (soc_level <= 80)
1375 rk81x_bat_save_level(di, soc_level);
1377 di->dod0_status = 1;
1379 di->fcc_update_sec = get_runtime_sec();
1382 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1383 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1385 di->dod0_voltage, di->dod0_capacity,
1386 ocv_soc, soc_level, di->dod0_status,
1392 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1394 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1397 static enum power_supply_property rk_battery_props[] = {
1398 POWER_SUPPLY_PROP_STATUS,
1399 POWER_SUPPLY_PROP_CURRENT_NOW,
1400 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1401 POWER_SUPPLY_PROP_PRESENT,
1402 POWER_SUPPLY_PROP_HEALTH,
1403 POWER_SUPPLY_PROP_CAPACITY,
1406 static int rk81x_battery_get_property(struct power_supply *psy,
1407 enum power_supply_property psp,
1408 union power_supply_propval *val)
1410 struct rk81x_battery *di = to_device_info(psy);
1413 case POWER_SUPPLY_PROP_CURRENT_NOW:
1414 val->intval = di->current_avg * 1000;/*uA*/
1415 if (di->fg_drv_mode == TEST_POWER_MODE)
1416 val->intval = TEST_CURRENT * 1000;
1418 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1419 val->intval = di->voltage * 1000;/*uV*/
1420 if (di->fg_drv_mode == TEST_POWER_MODE)
1421 val->intval = TEST_VOLTAGE * 1000;
1424 case POWER_SUPPLY_PROP_PRESENT:
1425 val->intval = is_rk81x_bat_exist(di);
1426 if (di->fg_drv_mode == TEST_POWER_MODE)
1427 val->intval = TEST_PRESET;
1430 case POWER_SUPPLY_PROP_CAPACITY:
1431 val->intval = di->dsoc;
1432 if (di->fg_drv_mode == TEST_POWER_MODE)
1433 val->intval = TEST_SOC;
1435 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1437 case POWER_SUPPLY_PROP_HEALTH:
1438 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1440 case POWER_SUPPLY_PROP_STATUS:
1441 val->intval = di->psy_status;
1442 if (di->fg_drv_mode == TEST_POWER_MODE)
1443 val->intval = TEST_STATUS;
1453 static enum power_supply_property rk_battery_ac_props[] = {
1454 POWER_SUPPLY_PROP_ONLINE,
1457 static enum power_supply_property rk_battery_usb_props[] = {
1458 POWER_SUPPLY_PROP_ONLINE,
1461 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1462 enum power_supply_property psp,
1463 union power_supply_propval *val)
1466 struct rk81x_battery *di = to_ac_device_info(psy);
1469 case POWER_SUPPLY_PROP_ONLINE:
1470 if (rk81x_chrg_online(di))
1471 rk81x_bat_lowpwr_check(di);
1472 val->intval = di->ac_online; /*discharging*/
1473 if (di->fg_drv_mode == TEST_POWER_MODE)
1474 val->intval = TEST_AC_ONLINE;
1484 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1485 enum power_supply_property psp,
1486 union power_supply_propval *val)
1489 struct rk81x_battery *di = to_usb_device_info(psy);
1492 case POWER_SUPPLY_PROP_ONLINE:
1493 if (rk81x_chrg_online(di))
1494 rk81x_bat_lowpwr_check(di);
1495 val->intval = di->usb_online;
1496 if (di->fg_drv_mode == TEST_POWER_MODE)
1497 val->intval = TEST_USB_ONLINE;
1507 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1511 di->bat.name = "BATTERY";
1512 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1513 di->bat.properties = rk_battery_props;
1514 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1515 di->bat.get_property = rk81x_battery_get_property;
1518 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1519 di->ac.properties = rk_battery_ac_props;
1520 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1521 di->ac.get_property = rk81x_battery_ac_get_property;
1523 di->usb.name = "USB";
1524 di->usb.type = POWER_SUPPLY_TYPE_USB;
1525 di->usb.properties = rk_battery_usb_props;
1526 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1527 di->usb.get_property = rk81x_battery_usb_get_property;
1529 ret = power_supply_register(di->dev, &di->bat);
1531 dev_err(di->dev, "failed to register main battery\n");
1534 ret = power_supply_register(di->dev, &di->usb);
1536 dev_err(di->dev, "failed to register usb power supply\n");
1539 ret = power_supply_register(di->dev, &di->ac);
1541 dev_err(di->dev, "failed to register ac power supply\n");
1548 power_supply_unregister(&di->ac);
1550 power_supply_unregister(&di->usb);
1552 power_supply_unregister(&di->bat);
1557 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1561 static u32 capacity_ma;
1563 if (capacity >= di->qmax)
1564 capacity = di->qmax;
1569 if (capacity_ma == capacity)
1572 capacity_ma = capacity;
1574 buf = (capacity_ma >> 24) & 0xff;
1575 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1576 buf = (capacity_ma >> 16) & 0xff;
1577 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1578 buf = (capacity_ma >> 8) & 0xff;
1579 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1580 buf = (capacity_ma & 0xff) | 0x01;
1581 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1584 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1592 for (i = 0; i < 3; i++) {
1593 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1595 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1596 val[i] |= buf << 16;
1597 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1599 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1603 if (val[0] == val[1])
1611 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1616 capacity_ma = capacity;
1617 buf = (capacity_ma >> 24) & 0xff;
1618 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1619 buf = (capacity_ma >> 16) & 0xff;
1620 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1621 buf = (capacity_ma >> 8) & 0xff;
1622 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1623 buf = (capacity_ma & 0xff) | 0x01;
1624 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1626 dev_info(di->dev, "update fcc : %d\n", capacity);
1629 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1634 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1635 capacity = buf << 24;
1636 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1637 capacity |= buf << 16;
1638 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1639 capacity |= buf << 8;
1640 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1643 if (capacity < MIN_FCC) {
1644 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1646 capacity = di->design_capacity;
1647 rk81x_bat_save_fcc(di, capacity);
1648 } else if (capacity > di->qmax) {
1649 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1650 capacity = di->qmax;
1651 rk81x_bat_save_fcc(di, capacity);
1657 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1666 for (i = 0; i < 3; i++) {
1667 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1669 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1670 val[i] |= buf << 16;
1671 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1673 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1676 if (val[0] == val[1])
1681 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1686 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1690 if (last_soc != save_soc) {
1691 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1692 last_soc = save_soc;
1698 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1703 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1707 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1711 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1712 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1713 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1714 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1717 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1718 enum charger_type charger_type)
1720 switch (charger_type) {
1723 rk81x_bat_set_current(di, ILIM_450MA);
1727 rk81x_bat_set_current(di, di->chrg_i_lmt);
1730 rk81x_bat_set_current(di, ILIM_450MA);
1734 #if defined(CONFIG_ARCH_ROCKCHIP)
1736 * There are three ways to detect dc_adp:
1737 * 1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1738 * do not care about whether define dc_det_pin or not;
1739 * 2. define de_det_pin: check gpio level;
1740 * 3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1741 * case that: gpio invalid or not define.
1743 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1745 enum charger_type charger_type;
1749 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1751 /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1752 if (!rk81x_bat_support_adp_type(HW_ADP_TYPE_USB)) {
1753 if ((buf & PLUG_IN_STS) != 0)
1754 charger_type = DC_CHARGER;
1756 charger_type = NO_CHARGER;
1758 return charger_type;
1761 /*det by gpio level*/
1762 if (gpio_is_valid(di->dc_det_pin)) {
1763 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1765 pr_err("Failed to request gpio %d with ret:""%d\n",
1766 di->dc_det_pin, ret);
1770 gpio_direction_input(di->dc_det_pin);
1771 ret = gpio_get_value(di->dc_det_pin);
1772 if (ret == di->dc_det_level)
1773 charger_type = DC_CHARGER;
1775 charger_type = NO_CHARGER;
1777 gpio_free(di->dc_det_pin);
1778 DBG("**********rk818 dc_det_pin=%d\n", ret);
1780 return charger_type;
1782 /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1783 } else if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DUAL)) {
1784 if ((buf & PLUG_IN_STS) != 0) {
1785 charger_type = dwc_otg_check_dpdm();
1786 if (charger_type == 0)
1787 charger_type = DC_CHARGER;
1789 charger_type = NO_CHARGER;
1793 return charger_type;
1796 static enum charger_type rk81x_bat_get_usbac_state(struct rk81x_battery *di)
1798 enum charger_type charger_type;
1799 int usb_id, gadget_flag;
1801 usb_id = dwc_otg_check_dpdm();
1804 charger_type = NO_CHARGER;
1808 charger_type = USB_CHARGER;
1811 charger_type = AC_CHARGER;
1814 charger_type = NO_CHARGER;
1817 DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1818 if (charger_type == USB_CHARGER) {
1819 gadget_flag = get_gadget_connect_flag();
1820 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1821 __func__, gadget_flag, di->check_count);
1823 if (0 == gadget_flag) {
1824 if (++di->check_count >= 5) {
1825 charger_type = AC_CHARGER;
1826 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1827 __func__, di->check_count);
1829 charger_type = USB_CHARGER;
1832 charger_type = USB_CHARGER;
1833 di->check_count = 0;
1836 di->check_count = 0;
1839 return charger_type;
1843 * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
1844 * and dc_adp are plugined in together, the dc_apt has high priority.
1845 * so we check dc_apt first and return rigth away if it's found.
1847 static enum charger_type rk81x_bat_get_adp_type(struct rk81x_battery *di)
1850 enum charger_type charger_type = NO_CHARGER;
1852 /*check by ic hardware: this check make check work safer*/
1853 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1854 if ((buf & PLUG_IN_STS) == 0)
1858 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DC)) {
1859 charger_type = rk81x_bat_get_dc_state(di);
1860 if (charger_type == DC_CHARGER)
1861 return charger_type;
1865 charger_type = rk81x_bat_get_usbac_state(di);
1867 return charger_type;
1870 static void rk81x_bat_status_check(struct rk81x_battery *di)
1872 static enum charger_type old_charger_type = DUAL_CHARGER;
1873 enum charger_type charger_type;
1875 charger_type = rk81x_bat_get_adp_type(di);
1876 if (charger_type == old_charger_type)
1878 rk81x_bat_set_chrg_current(di, charger_type);
1879 rk81x_bat_set_power_supply_state(di, charger_type);
1880 old_charger_type = charger_type;
1884 #if defined(CONFIG_X86_INTEL_SOFIA)
1885 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1887 enum charger_type charger_type;
1890 charger_type = AC_CHARGER;
1892 charger_type = USB_CHARGER;
1894 charger_type = NO_CHARGER;
1898 dev_info(di->dev, "limit current:%d\n", ma);
1900 return charger_type;
1903 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1905 struct rk81x_battery *di;
1906 enum charger_type type;
1908 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1909 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1911 rk81x_bat_set_chrg_current(di, type);
1912 power_supply_changed(&di->usb);
1915 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1916 unsigned long event, void *data)
1918 struct rk81x_battery *di;
1919 struct power_supply_cable_props *cable_props;
1920 enum charger_type type;
1922 di = container_of(nb, struct rk81x_battery, usb_nb);
1928 case USB_EVENT_CHARGER:
1929 cable_props = (struct power_supply_cable_props *)data;
1930 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1931 rk81x_bat_set_power_supply_state(di, type);
1932 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1933 msecs_to_jiffies(50));
1944 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1945 unsigned long event, void *data)
1947 struct rk81x_battery *di;
1948 struct fb_event *evdata = data;
1951 di = container_of(nb, struct rk81x_battery, fb_nb);
1953 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1956 blank = *(int *)evdata->data;
1958 if (di->fb_blank != blank)
1959 di->fb_blank = blank;
1963 if (blank == FB_BLANK_UNBLANK)
1964 di->early_resume = 1;
1969 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1971 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1972 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1974 return fb_register_client(&di->fb_nb);
1978 * it is first time for battery to be weld, init by ocv table
1980 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1982 rk81x_bat_save_fcc(di, di->design_capacity);
1983 di->fcc = rk81x_bat_get_fcc(di);
1985 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1986 di->dsoc = di->rsoc;
1987 di->nac = di->temp_nac;
1989 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1990 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1991 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1992 __func__, di->dsoc, di->nac, di->fcc);
1995 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
2000 int relax_vol = di->relax_voltage;
2001 int ocv_vol = di->voltage_ocv;
2003 init_cur = rk81x_bat_get_avg_current(di);
2004 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
2006 est_vol = di->voltage - diff;
2008 if (di->loader_charged) {
2009 calib_vol = est_vol;
2013 if (di->pwroff_min > 8) {
2014 if (abs(relax_vol - ocv_vol) < 100) {
2015 calib_vol = ocv_vol;
2017 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
2018 calib_vol = ocv_vol;
2020 calib_vol = relax_vol;
2022 } else if (di->pwroff_min > 2) {
2023 calib_vol = ocv_vol;
2028 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2029 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2035 * it is not first time for battery to be weld, init by last record info
2037 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2041 int remain_capacity;
2043 int calib_vol, calib_soc, calib_capacity;
2045 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2046 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2047 init_soc = pwron_soc;
2048 remain_capacity = rk81x_bat_get_remain_capacity(di);
2050 /* check if support uboot charge,
2051 * if support, uboot charge driver should have done init work,
2052 * so here we should skip init work
2054 #if defined(CONFIG_ARCH_ROCKCHIP)
2055 if (di->loader_charged)
2058 calib_vol = rk81x_bat_get_calib_vol(di);
2059 if (calib_vol > 0) {
2060 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2061 calib_capacity = di->temp_nac;
2063 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2064 init_soc = calib_soc;
2065 remain_capacity = calib_capacity;
2067 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2068 calib_vol, init_soc, remain_capacity);
2071 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2072 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2073 __func__, remain_capacity, di->temp_nac);
2075 if (di->pwroff_min > 0) {
2076 if (di->pwroff_min > 30) {
2077 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2079 remain_capacity = di->temp_nac;
2080 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2081 __func__, remain_capacity);
2083 } else if ((di->pwroff_min > 5) &&
2084 (abs(ocv_soc - init_soc) >= 10)) {
2085 if (remain_capacity >= di->temp_nac * 120/100)
2086 remain_capacity = di->temp_nac * 110/100;
2087 else if (remain_capacity < di->temp_nac * 8/10)
2088 remain_capacity = di->temp_nac * 9/10;
2089 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2090 __func__, remain_capacity);
2093 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2096 di->dsoc = init_soc;
2097 di->nac = remain_capacity;
2100 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2101 pwron_soc, di->dsoc, di->nac);
2104 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2106 u8 curr_pwroff_min, last_pwroff_min;
2108 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2109 &curr_pwroff_min, 1);
2110 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2111 &last_pwroff_min, 1);
2113 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2114 &curr_pwroff_min, 1);
2116 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2119 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2121 u8 calib_en;/*debug*/
2123 di->voltage = rk81x_bat_get_vol(di);
2124 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2125 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2126 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2127 di->current_avg = rk81x_bat_get_avg_current(di);
2129 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2130 di->voltage, di->voltage_ocv, di->relax_voltage,
2131 di->current_avg, di->pwroff_min);
2133 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2134 DBG("readbit: calib_en=%d\n", calib_en);
2135 if (is_rk81x_bat_first_poweron(di) ||
2136 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2137 rk81x_bat_first_pwron(di);
2138 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2141 rk81x_bat_not_first_pwron(di);
2147 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2152 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2157 DBG(" CHARGE-OFF ...\n");
2161 DBG(" DEAD CHARGE ...\n");
2163 case TRICKLE_CHARGE:
2165 DBG(" TRICKLE CHARGE ...\n ");
2169 DBG(" CC or CV ...\n");
2172 ret = CHARGE_FINISH;
2173 DBG(" CHARGE FINISH ...\n");
2177 DBG(" USB OVER VOL ...\n");
2181 DBG(" BAT TMP ERROR ...\n");
2185 DBG(" TIMER ERROR ...\n");
2189 DBG(" USB EXIST ...\n");
2193 DBG(" USB EFF...\n");
2202 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2203 int chrg_ilim, int chrg_cur)
2207 di->chrg_v_lmt = DEF_CHRG_VOL;
2208 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2209 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2211 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2212 if (chrg_vol < CHRG_V_LMT[i])
2215 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2218 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2219 if (chrg_ilim < CHRG_I_LMT[i])
2222 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2225 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2226 if (chrg_cur < CHRG_I_CUR[i])
2229 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2231 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2232 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2235 static u8 rk81x_bat_select_finish_ma(int fcc)
2237 u8 ma = FINISH_150MA;
2242 else if (fcc >= 4000)
2245 else if (fcc >= 3000)
2255 * there is a timer inside rk81x to calc how long the battery is in charging
2256 * state. rk81x will close PowerPath inside IC when timer reach, which will
2257 * stop the charging work. we have to reset the corresponding bits to restart
2258 * the timer to avoid that case.
2260 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2264 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2265 buf &= ~CHRG_TIMER_CCCV_EN;
2266 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2268 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2269 buf |= CHRG_TIMER_CCCV_EN;
2270 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2271 dev_info(di->dev, "reset cccv charge timer\n");
2275 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2277 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2278 u8 sup_sts_reg, thremal_reg, ggcon;
2279 int chrg_vol, chrg_cur, chrg_ilim;
2282 chrg_vol = di->pdata->max_charger_voltagemV;
2283 chrg_cur = di->pdata->max_charger_currentmA;
2284 chrg_ilim = di->pdata->max_charger_ilimitmA;
2286 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2287 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2289 /*rk81x_bat_init_chrg_timer(di);*/
2291 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2292 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2293 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2294 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2295 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2296 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2297 rk81x_bat_read(di, GGCON, &ggcon, 1);
2299 usb_ctrl_reg &= (~0x0f);
2301 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2302 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2304 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2306 if (di->fg_drv_mode == TEST_POWER_MODE)
2307 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2309 chrg_ctrl_reg1 &= (0x00);
2310 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2312 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2313 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2315 chrg_ctrl_reg2 &= ~(0xc7);
2316 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2318 sup_sts_reg &= ~(0x01 << 3);
2319 sup_sts_reg |= (0x01 << 2);
2321 thremal_reg &= (~0x0c);
2322 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2323 ggcon |= ADC_CURRENT_MODE;
2325 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2326 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2327 /*don't touch charge setting when boot int loader charge mode*/
2328 if (!di->loader_charged)
2329 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2330 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2331 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2332 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2333 rk81x_bat_write(di, GGCON, &ggcon, 1);
2336 void rk81x_charge_disable_open_otg(struct rk81x_battery *di)
2338 int value = di->charge_otg;
2341 DBG("charge disable, enable OTG.\n");
2342 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2343 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2345 DBG("charge enable, disable OTG.\n");
2346 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2347 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2351 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2358 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2360 rk81x_bat_gauge_enable(di);
2361 /* get the volatege offset */
2362 rk81x_bat_get_vol_offset(di);
2363 rk81x_bat_charger_init(di);
2364 rk81x_bat_set_relax_thres(di);
2366 /* get the current offset , the value write to the CAL_OFFSET */
2367 di->current_offset = rk81x_bat_get_ioffset(di);
2368 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2369 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2370 __func__, pcb_offset, di->current_offset);
2372 pcb_offset = DEF_PCB_OFFSET;
2373 cal_offset = pcb_offset + di->current_offset;
2374 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2375 cal_offset = DEF_CAL_OFFSET;
2376 rk81x_bat_set_cal_offset(di, cal_offset);
2377 /* set sample time for cal_offset interval*/
2378 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2380 rk81x_bat_rsoc_init(di);
2381 rk81x_bat_capacity_init(di, di->nac);
2382 rk81x_bat_capacity_init_post(di);
2384 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2385 di->current_avg = rk81x_bat_get_avg_current(di);
2387 rk81x_bat_restart_relax(di);
2388 rk81x_bat_power_on_save(di, di->voltage_ocv);
2390 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2392 rk81x_dbg_dmp_gauge_regs(di);
2393 rk81x_dbg_dmp_charger_regs(di);
2396 "nac = %d , remain_capacity = %d\n"
2397 "OCV_voltage = %d, voltage = %d\n"
2398 "SOC = %d, fcc = %d\n, current=%d\n"
2399 "cal_offset = 0x%x\n",
2401 di->nac, di->remain_capacity,
2402 di->voltage_ocv, di->voltage,
2403 di->dsoc, di->fcc, di->current_avg,
2407 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2409 int dead_voltage, ocv_voltage;
2410 int voltage, voltage_old, voltage_now;
2415 int ocv_soc, dead_soc;
2416 int power_off_thresd = di->pdata->power_off_thresd;
2419 voltage_old = rk81x_bat_get_vol(di);
2421 voltage_now = rk81x_bat_get_vol(di);
2423 } while ((voltage_old == voltage_now) && (count_num < 11));
2424 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2427 for (i = 0; i < 10; i++) {
2428 voltage += rk81x_bat_get_vol(di);
2433 currentnow = rk81x_bat_get_avg_current(di);
2435 /* 50 mo power-path mos */
2436 dead_voltage = power_off_thresd - currentnow *
2437 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2439 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2440 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2441 dead_voltage, ocv_voltage);
2443 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2444 q_dead = di->temp_nac;
2445 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2448 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2449 q_ocv = di->temp_nac;
2450 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2453 rsoc = ocv_soc - dead_soc;
2454 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2456 } else if (rsoc > 0) {
2457 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2460 di->display_soc = di->dsoc * 1000;
2463 di->zero_old_remain_cap = di->remain_capacity;
2465 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2466 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2467 di->line_k, di->dsoc, rsoc,
2468 di->display_soc, di->zero_old_remain_cap);
2471 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2473 int delta_cap, delta_soc;
2476 di->zero_timeout_cnt++;
2477 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2478 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2480 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2481 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2482 "ZERO1: timeout_cnt=%d\n\n",
2483 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2484 delta_soc, delta_cap, di->zero_old_remain_cap,
2485 di->zero_timeout_cnt);
2487 if ((abs(delta_soc) > MIN_ZERO_ACCURACY) ||
2488 (di->zero_timeout_cnt > 500)) {
2489 DBG("ZERO1:--------- enter calc -----------\n");
2490 di->zero_timeout_cnt = 0;
2491 di->display_soc -= delta_soc;
2492 tmp_dsoc = (di->display_soc + 500) / 1000;
2493 di->dsoc = tmp_dsoc;
2495 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2496 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2498 rk81x_bat_zero_calc_linek(di);
2502 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2504 return (di->voltage -
2505 (di->bat_res * di->current_avg) / 1000);
2508 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2510 int ocv_soc, ocv_voltage;
2512 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2513 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2518 /* we will estimate a ocv voltage to get a ocv soc.
2519 * if there is a big offset between ocv_soc and rsoc,
2520 * we will decide whether we should reinit capacity or not
2522 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2524 int ocv_soc = di->est_ocv_soc;
2525 int ocv_volt = di->est_ocv_vol;
2526 int rsoc = rk81x_bat_get_rsoc(di);
2527 int max_volt = di->pdata->max_charger_voltagemV;
2529 if (ocv_volt > max_volt)
2532 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2533 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2535 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2537 di->err_soc_sum += ocv_soc;
2541 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2542 __func__, di->err_chck_cnt, di->err_soc_sum);
2544 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2545 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2546 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2547 ocv_soc += RSOC_COMPS;
2549 di->temp_nac = ocv_soc * di->fcc / 100;
2550 rk81x_bat_capacity_init(di, di->temp_nac);
2551 rk81x_bat_capacity_init_post(di);
2552 di->rsoc = rk81x_bat_get_rsoc(di);
2553 di->remain_capacity =
2554 rk81x_bat_get_realtime_capacity(di);
2555 di->err_soc_sum = 0;
2556 di->err_chck_cnt = 0;
2557 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2561 di->err_chck_cnt = 0;
2562 di->err_soc_sum = 0;
2566 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2568 u8 status = di->psy_status;
2570 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2571 (status == POWER_SUPPLY_STATUS_FULL)) {
2572 if ((di->current_avg < 0) &&
2573 (di->chrg_status != CHARGE_FINISH))
2574 rk81x_bat_rsoc_dischrg_check(di);
2577 rsoc_chrg_calib(di);
2580 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2581 rk81x_bat_rsoc_dischrg_check(di);
2585 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2587 u32 temp, soc_time = 0;
2588 unsigned long sec_unit;
2590 if (!di->dischrg_emu_base)
2591 di->dischrg_emu_base = get_runtime_sec();
2593 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2595 temp = di->fcc * 3600 / 100;
2597 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2598 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2600 soc_time = temp / div(abs(di->current_avg));
2602 if (sec_unit > soc_time) {
2604 di->dischrg_emu_base = get_runtime_sec();
2605 di->dischrg_save_sec = 0;
2608 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2609 __func__, soc_time, sec_unit);
2613 * when there is a big offset between dsoc and rsoc, dsoc needs to
2614 * speed up to keep pace witch rsoc.
2616 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2618 u32 soc_time = 0, temp;
2620 unsigned long chrg_emu_sec;
2622 if (!di->chrg_emu_base)
2623 di->chrg_emu_base = get_runtime_sec();
2625 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2626 temp = di->fcc * 3600 / 100;
2628 if (di->ac_online) {
2629 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2630 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2632 soc_time = temp / div(abs(di->current_avg));
2634 soc_time = temp / 450;
2637 plus_soc = chrg_emu_sec / soc_time;
2638 if (chrg_emu_sec > soc_time) {
2639 di->dsoc += plus_soc;
2640 di->chrg_emu_base = get_runtime_sec();
2641 di->chrg_save_sec = 0;
2644 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2645 __func__, soc_time, chrg_emu_sec, plus_soc);
2648 /* check voltage and current when dsoc is close to full.
2649 * we will do a fake charge to adjust charing speed which
2650 * aims to make battery full charged and match finish signal.
2652 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2656 unsigned long chrg_term_sec;
2658 if (!di->chrg_term_base)
2659 di->chrg_term_base = get_runtime_sec();
2661 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2662 /*check current and voltage*/
2664 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2666 plus_soc = chrg_term_sec / soc_time;
2667 if (chrg_term_sec > soc_time) {
2668 di->dsoc += plus_soc;
2669 di->chrg_term_base = get_runtime_sec();
2670 di->chrg_save_sec = 0;
2672 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2673 __func__, soc_time, chrg_term_sec, plus_soc);
2676 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2679 int now_current = di->current_avg;
2680 unsigned long dischrg_normal_sec;
2682 if (!di->dischrg_normal_base)
2683 di->dischrg_normal_base = get_runtime_sec();
2685 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2686 di->dischrg_save_sec;
2688 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2689 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2690 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2692 if (di->rsoc == di->dsoc) {
2693 DBG("<%s>. rsoc == dsoc\n", __func__);
2694 di->dsoc = di->rsoc;
2695 di->dischrg_normal_base = get_runtime_sec();
2696 di->dischrg_save_sec = 0;
2697 /*di->discharge_smooth_status = false;*/
2698 } else if (di->rsoc > di->dsoc - 1) {
2699 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2700 if (dischrg_normal_sec > soc_time * 3 / 2) {
2702 di->dischrg_normal_base = get_runtime_sec();
2703 di->dischrg_save_sec = 0;
2705 di->discharge_smooth_status = true;
2707 } else if (di->rsoc < di->dsoc - 1) {
2708 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2709 if (dischrg_normal_sec > soc_time * 3 / 4) {
2711 di->dischrg_normal_base = get_runtime_sec();
2712 di->dischrg_save_sec = 0;
2714 di->discharge_smooth_status = true;
2716 } else if (di->rsoc == di->dsoc - 1) {
2717 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2718 if (di->discharge_smooth_status) {
2719 if (dischrg_normal_sec > soc_time * 3 / 4) {
2721 di->dischrg_normal_base = get_runtime_sec();
2722 di->dischrg_save_sec = 0;
2723 di->discharge_smooth_status = false;
2727 di->dischrg_normal_base = get_runtime_sec();
2728 di->dischrg_save_sec = 0;
2729 di->discharge_smooth_status = false;
2733 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2734 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2735 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2736 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2739 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2743 /* first resume from suspend: we don't run this,
2744 * the sleep_dischrg will handle dsoc, and what
2745 * ever this is fake wakeup or not, we should clean
2746 * zero algorithm mode, or it will handle the dsoc.
2749 rk81x_bat_reset_zero_var(di);
2753 di->rsoc = rk81x_bat_get_rsoc(di);
2755 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2756 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2758 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2759 delta_soc = di->dsoc - di->rsoc;
2761 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2762 di->dischrg_normal_base = 0;
2763 rk81x_bat_emulator_dischrg(di);
2765 di->chrg_emu_base = 0;
2766 rk81x_bat_normal_dischrg(di);
2769 if (di->voltage < ZERO_ALGOR_THRESD) {
2770 di->dischrg_normal_base = 0;
2771 di->chrg_emu_base = 0;
2772 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2773 di->zero_timeout_cnt = 0;
2775 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2776 __func__, di->dsoc, di->last_zero_mode_dsoc);
2777 if (di->dsoc != di->last_zero_mode_dsoc) {
2778 di->display_soc = di->dsoc * 1000;
2779 di->last_zero_mode_dsoc = di->dsoc;
2780 rk81x_bat_zero_calc_linek(di);
2781 DBG("<%s>. first calc, init linek\n", __func__);
2785 rk81x_bat_zero_algorithm(di);
2787 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2788 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2789 di->zero_timeout_cnt = 0;
2790 DBG("<%s>. exit zero_algorithm\n", __func__);
2795 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2798 static int old_index;
2801 int mod = di->dsoc % 10;
2802 int index = di->dsoc / 10;
2804 if (rk81x_chrg_online(di))
2805 time = di->plug_in_min;
2807 time = di->plug_out_min;
2809 if ((mod == 0) && (index > 0) && (old_index != index)) {
2810 di->chrg_min[index-1] = time - old_min;
2815 for (i = 1; i < 11; i++)
2816 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2820 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2822 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2823 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2824 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2826 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2827 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2828 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2829 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2830 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2831 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2832 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2833 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2834 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2835 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2836 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2838 DBG("\n------------- dump_debug_regs -----------------\n"
2839 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2840 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2841 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2842 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2843 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2844 ggcon_reg, ggsts_reg, rtc_val,
2845 sup_tst_reg, vb_mod_reg,
2846 usb_ctrl_reg, chrg_ctrl_reg1,
2847 thremal_reg, misc_reg,
2848 chrg_ctrl_reg2, chrg_ctrl_reg3
2851 DBG("#######################################################\n"
2852 "voltage = %d, current-avg = %d\n"
2853 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2854 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2855 "display_soc = %d, cpapacity_soc = %d\n"
2856 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2857 "finish_real_soc = %d, finish_temp_soc = %d\n"
2858 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2859 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2860 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2861 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2862 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2863 "#########################################################\n",
2864 di->voltage, di->current_avg,
2865 di->fcc, di->remain_capacity, di->voltage_ocv,
2866 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2868 di->ac_online, di->usb_online, di->psy_status,
2869 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2870 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2871 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2872 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2873 BASE_TO_SEC(di->chrg_normal_base),
2874 BASE_TO_SEC(di->dischrg_normal_base),
2875 BASE_TO_SEC(di->chrg_term_base),
2876 BASE_TO_SEC(di->chrg_emu_base),
2877 BASE_TO_SEC(di->dischrg_emu_base),
2878 BASE_TO_SEC(di->power_on_base), g_base_sec,
2879 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2884 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2888 int dod0_to_soc100_min;
2890 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2891 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2893 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2894 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2895 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2896 di->dod0_capacity, dod0_to_soc100_min);
2898 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2899 (dod0_to_soc100_min < 1200)) {
2900 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2901 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2903 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2905 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2906 __func__, fcc0, di->fcc);
2908 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2909 di->dod0_status = 0;
2911 rk81x_bat_capacity_init(di, di->fcc);
2912 rk81x_bat_capacity_init_post(di);
2913 rk81x_bat_save_fcc(di, di->fcc);
2914 rk81x_bat_save_level(di, di->dod0_level);
2915 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2918 di->dod0_status = 0;
2922 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2924 if (di->chrg_status == CHARGE_FINISH) {
2925 di->debug_finish_real_soc = di->dsoc;
2926 di->debug_finish_temp_soc = di->rsoc;
2930 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2932 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2935 if ((di->chrg_status == CHARGE_FINISH) &&
2936 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2937 rk81x_bat_update_fcc(di);/* save new fcc*/
2938 ret = rk81x_bat_zero_current_calib(di);
2940 di->enter_finish = false;
2942 rk81x_bat_dbg_get_finish_soc(di);
2946 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2948 unsigned long sec_finish;
2949 int soc_time = 0, plus_soc;
2952 if (di->dsoc < 100) {
2953 if (!di->chrg_finish_base)
2954 di->chrg_finish_base = get_runtime_sec();
2956 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2958 temp = di->fcc * 3600 / 100;
2960 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2961 else if (di->usb_online)
2962 soc_time = temp / 450;
2964 plus_soc = sec_finish / soc_time;
2965 if (sec_finish > soc_time) {
2966 di->dsoc += plus_soc;
2967 di->chrg_finish_base = get_runtime_sec();
2968 di->chrg_save_sec = 0;
2970 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2971 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2972 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2976 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2979 u32 soc_time, unit_sec;
2981 unsigned long chrg_normal_sec;
2983 now_current = rk81x_bat_get_avg_current(di);
2984 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2986 if (!di->chrg_normal_base)
2987 di->chrg_normal_base = get_runtime_sec();
2989 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2990 di->rsoc = rk81x_bat_get_rsoc(di);
2992 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2993 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2995 if (di->dsoc == di->rsoc) {
2996 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2997 di->rsoc = rk81x_bat_get_rsoc(di);
2998 di->chrg_normal_base = get_runtime_sec();
2999 di->chrg_save_sec = 0;
3000 /*di->charge_smooth_status = false;*/
3001 } else if (di->rsoc < di->dsoc + 1) {
3002 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
3003 unit_sec = soc_time * 3 / 2;
3004 plus_soc = chrg_normal_sec / unit_sec;
3005 if (chrg_normal_sec > unit_sec) {
3006 di->dsoc += plus_soc;
3007 di->chrg_normal_base = get_runtime_sec();
3008 di->chrg_save_sec = 0;
3010 di->charge_smooth_status = true;
3011 } else if (di->rsoc > di->dsoc + 1) {
3012 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
3013 unit_sec = soc_time * 3 / 4;
3014 plus_soc = chrg_normal_sec / unit_sec;
3015 if (chrg_normal_sec > unit_sec) {
3016 di->dsoc += plus_soc;
3017 di->chrg_normal_base = get_runtime_sec();
3018 di->chrg_save_sec = 0;
3020 di->charge_smooth_status = true;
3021 } else if (di->rsoc == di->dsoc + 1) {
3022 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3023 if (di->charge_smooth_status) {
3024 unit_sec = soc_time * 3 / 4;
3025 if (chrg_normal_sec > unit_sec) {
3026 di->dsoc = di->rsoc;
3027 di->chrg_normal_base = get_runtime_sec();
3028 di->charge_smooth_status = false;
3029 di->chrg_save_sec = 0;
3032 di->dsoc = di->rsoc;
3033 di->chrg_normal_base = get_runtime_sec();
3034 di->charge_smooth_status = false;
3035 di->chrg_save_sec = 0;
3039 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3040 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3041 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3042 chrg_normal_sec, soc_time, plus_soc);
3045 static void rk81x_bat_update_time(struct rk81x_battery *di)
3049 runtime_sec = get_runtime_sec();
3051 /*update by charger type*/
3052 if (rk81x_chrg_online(di))
3053 di->plug_out_base = runtime_sec;
3055 di->plug_in_base = runtime_sec;
3057 /*update by current*/
3058 if (di->chrg_status != CHARGE_FINISH) {
3059 di->finish_sig_base = runtime_sec;
3060 di->chrg_finish_base = runtime_sec;
3063 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3064 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3065 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3067 rk81x_bat_dbg_time_table(di);
3070 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3072 int trend_start_cap = di->trend_start_cap;
3073 int remain_cap = di->remain_capacity;
3077 if (di->s2r && !di->slp_psy_status)
3078 di->trend_start_cap = di->remain_capacity;
3080 diff_cap = remain_cap - trend_start_cap;
3081 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3082 __func__, trend_start_cap, diff_cap);
3083 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3085 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3086 di->trend_start_cap = di->remain_capacity;
3087 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3088 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3090 state = TREND_STAT_FLAT;
3096 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3098 int state, soc_time;
3099 static int trend_down_cnt, trend_up_cnt;
3100 int trend_cnt_thresd;
3101 int now_current = di->current_avg;
3104 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3105 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3106 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3107 __func__, state, trend_mult);
3108 if (di->chrg_status == CHARGE_FINISH)
3111 if (state == TREND_STAT_UP) {
3112 rk81x_bat_reset_zero_var(di);
3114 trend_up_cnt += trend_mult;
3115 if (trend_up_cnt >= trend_cnt_thresd) {
3117 di->dischrg_save_sec = 0;
3119 } else if (state == TREND_STAT_DOWN) {
3121 trend_down_cnt += trend_mult;
3122 if (trend_down_cnt >= trend_cnt_thresd) {
3124 di->chrg_save_sec = 0;
3128 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3129 if ((di->chrg_save_sec + 20 > soc_time) &&
3130 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3132 di->chrg_save_sec = 0;
3134 else if ((di->dischrg_save_sec + 20 > soc_time) &&
3135 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3137 di->dischrg_save_sec = 0;
3139 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3140 "up_cnt=%d, down_cnt=%d\n",
3141 __func__, state, trend_cnt_thresd, soc_time,
3142 trend_up_cnt, trend_down_cnt);
3145 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3147 u32 *ocv_table = di->pdata->battery_ocv;
3148 int delta_soc = di->rsoc - di->dsoc;
3150 if (di->chrg_status == CHARGE_FINISH ||
3151 di->slp_chrg_status == CHARGE_FINISH) {
3152 /*clear sleep charge status*/
3153 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3154 di->chrg_emu_base = 0;
3155 di->chrg_normal_base = 0;
3156 di->chrg_term_base = 0;
3157 rk81x_bat_finish_chrg(di);
3158 rk81x_bat_capacity_init(di, di->fcc);
3159 rk81x_bat_capacity_init_post(di);
3160 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3161 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3162 (di->voltage < ocv_table[18] + 20))) {
3163 di->chrg_emu_base = 0;
3164 di->chrg_normal_base = 0;
3165 di->chrg_finish_base = 0;
3166 rk81x_bat_terminal_chrg(di);
3167 } else if (di->chrg_status != CHARGE_FINISH &&
3168 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3169 di->chrg_term_base = 0;
3170 di->chrg_normal_base = 0;
3171 di->chrg_finish_base = 0;
3172 rk81x_bat_emulator_chrg(di);
3174 di->chrg_emu_base = 0;
3175 di->chrg_term_base = 0;
3176 di->chrg_finish_base = 0;
3177 rk81x_bat_normal_chrg(di);
3181 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3183 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3184 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3186 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3187 dischrg_normal_sec, dischrg_emu_sec);
3189 return (dischrg_normal_sec > dischrg_emu_sec) ?
3190 dischrg_normal_sec : dischrg_emu_sec;
3193 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3195 unsigned long sec1, sec2;
3196 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3197 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3198 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3199 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3201 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3202 chrg_normal_sec : chrg_term_sec;
3204 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3205 chrg_emu_sec : chrg_finish_sec;
3206 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3207 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3208 chrg_normal_sec, chrg_term_sec,
3209 chrg_emu_sec, chrg_finish_sec);
3211 return (sec1 > sec2) ? sec1 : sec2;
3214 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3216 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3217 if (di->current_mode == DISCHRG_MODE) {
3218 di->current_mode = CHRG_MODE;
3219 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3220 di->dischrg_normal_base = 0;
3221 di->dischrg_emu_base = 0;
3222 if (di->chrg_status == CHARGE_FINISH)
3223 di->dischrg_save_sec = 0;
3224 if ((di->chrg_status == CHARGE_FINISH) &&
3226 di->chrg_save_sec = 0;
3228 DBG("<%s>---dischrg_save_sec = %lu\n",
3229 __func__, di->dischrg_save_sec);
3232 if (!rk81x_chrg_online(di)) {
3233 dev_err(di->dev, "discharge, current error:%d\n",
3236 rk81x_bat_chrg_smooth(di);
3237 di->discharge_smooth_status = true;
3240 if (di->current_mode == CHRG_MODE) {
3241 di->current_mode = DISCHRG_MODE;
3242 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3243 di->chrg_normal_base = 0;
3244 di->chrg_emu_base = 0;
3245 di->chrg_term_base = 0;
3246 di->chrg_finish_base = 0;
3247 DBG("<%s>---chrg_save_sec = %lu\n",
3248 __func__, di->chrg_save_sec);
3250 rk81x_bat_dischrg_smooth(di);
3251 di->charge_smooth_status = true;
3256 * update rsoc by relax voltage
3258 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3260 int relax_vol = di->relax_voltage;
3261 int ocv_soc, capacity;
3263 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3264 capacity = (ocv_soc * di->fcc / 100);
3265 rk81x_bat_capacity_init(di, capacity);
3266 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3267 di->rsoc = rk81x_bat_get_rsoc(di);
3268 rk81x_bat_capacity_init_post(di);
3269 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3273 * 1: must do it, 0: when necessary
3275 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3277 int ocv_vol = di->est_ocv_vol;
3278 int ocv_soc = 0, capacity = 0;
3280 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3281 capacity = (ocv_soc * di->fcc / 100);
3282 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3283 rk81x_bat_capacity_init(di, capacity);
3284 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3285 di->rsoc = rk81x_bat_get_rsoc(di);
3286 rk81x_bat_capacity_init_post(di);
3287 DBG("<%s>, rsoc updated!\n", __func__);
3289 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3290 __func__, ocv_vol, ocv_soc, capacity);
3293 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3297 unsigned long sleep_sec = di->suspend_time_sum;
3298 int power_off_thresd = di->pdata->power_off_thresd;
3300 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3301 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3302 __func__, di->dsoc, di->rsoc,
3303 di->relax_voltage, di->voltage, sleep_sec / 60);
3305 if (di->relax_voltage >= di->voltage) {
3306 rk81x_bat_relax_vol_calib(di);
3307 rk81x_bat_restart_relax(di);
3309 /* current_avg < 0: make sure the system is not
3310 * wakeup by charger plugin.
3312 /* even if relax voltage is not caught rightly, realtime voltage
3313 * is quite close to relax voltage, we should not do nothing after
3317 rk81x_bat_vol_calib(di, 1);
3321 if (di->dsoc <= di->rsoc) {
3322 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3323 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3324 temp_dsoc = di->dsoc - delta_soc;
3326 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3327 di->rsoc, di->dsoc, delta_soc);
3329 if (delta_soc > 0) {
3330 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3332 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3334 else if (temp_dsoc > 5)
3335 di->dsoc = temp_dsoc;
3338 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3339 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3341 /*di->dsoc > di->rsoc*/
3342 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3343 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3344 temp_dsoc = di->dsoc - di->rsoc;
3346 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3347 di->rsoc, di->dsoc, delta_soc);
3349 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3350 (temp_dsoc > delta_soc))
3351 di->dsoc -= delta_soc;
3353 di->dsoc = di->rsoc;
3355 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3356 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3359 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3365 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3366 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3371 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3374 unsigned long sleep_sec;
3376 sleep_sec = di->suspend_time_sum;
3377 if (((di->suspend_charge_current < 800) &&
3378 (di->ac_online == ONLINE)) ||
3379 (di->chrg_status == CHARGE_FINISH)) {
3380 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3381 if (sleep_sec > 0) {
3382 /*default charge current: 1000mA*/
3383 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3384 / 3600 / div(di->fcc);
3387 DBG("<%s>, usb charge\n", __func__);
3394 * only do report when there is a change.
3396 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3397 * when dsoc == 0, we must do report. But it will generate too much android
3398 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3401 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3404 static u32 old_ac_status;
3405 static u32 old_usb_status;
3406 static u32 old_charge_status;
3409 state_changed = false;
3410 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3411 state_changed = true;
3412 else if (di->dsoc != old_soc)
3413 state_changed = true;
3414 else if (di->ac_online != old_ac_status)
3415 state_changed = true;
3416 else if (di->usb_online != old_usb_status)
3417 state_changed = true;
3418 else if (old_charge_status != di->psy_status)
3419 state_changed = true;
3421 if (rk81x_chrg_online(di)) {
3422 if (di->dsoc == 100)
3423 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3425 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3428 if (state_changed) {
3429 power_supply_changed(&di->bat);
3430 power_supply_changed(&di->usb);
3431 power_supply_changed(&di->ac);
3433 old_ac_status = di->ac_online;
3434 old_usb_status = di->usb_online;
3435 old_charge_status = di->psy_status;
3436 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3437 di->dsoc, di->rsoc);
3442 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3446 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3449 return CHRG_CVCC_HOUR[hour];
3452 /* we have to estimate the charging finish time from now, to decide
3453 * whether we should reset the timer or not.
3455 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3458 int remain_capacity;
3460 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3461 if (di->dsoc < di->rsoc)
3462 remain_capacity = di->dsoc * di->fcc / 100;
3464 remain_capacity = di->remain_capacity;
3466 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3467 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3469 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3470 di->chrg_cap2full = di->fcc - remain_capacity;
3471 if (di->current_avg <= 0)
3472 di->current_avg = 1;
3474 di->chrg_time2full = di->chrg_cap2full * 3600 /
3475 div(abs(di->current_avg));
3477 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3478 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3481 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3482 /*rk81x_bat_init_chrg_timer(di);*/
3483 di->plug_in_base = get_runtime_sec();
3484 DBG("%s: reset charge timer\n", __func__);
3491 * in case that we will do reboot stress test, we need a special way
3492 * to ajust the dsoc.
3494 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3498 u8 status = di->psy_status;
3503 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3506 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3507 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3509 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3510 __func__, cnt, unit_time, smooth_time,
3511 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3513 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
3514 (status == POWER_SUPPLY_STATUS_FULL && abs(di->current_avg) < 5)) {
3515 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3516 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3521 rk81x_bat_save_dsoc(di, dsoc);
3523 } else {/*status == POWER_SUPPLY_STATUS_DISCHARGING*/
3525 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3/5);
3526 if ((dsoc > rsoc) && (smooth_time > unit_time * 3/5)) {
3531 rk81x_bat_save_dsoc(di, dsoc);
3535 rk81x_bat_save_reboot_cnt(di, cnt);
3538 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3543 uint16_t cal_offset;
3544 u8 pcb_offset = DEF_PCB_OFFSET;
3546 min = BASE_TO_MIN(di->power_on_base);
3547 if ((min % 8) && (old_min != min)) {
3549 rk81x_bat_get_vol_offset(di);
3550 if (di->pcb_ioffset_updated)
3551 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3553 current_offset = rk81x_bat_get_ioffset(di);
3554 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3555 cal_offset = rk81x_bat_get_cal_offset(di);
3556 if (cal_offset < 0x7ff)
3557 rk81x_bat_set_cal_offset(di, di->current_offset +
3559 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3560 __func__, di->voltage_k, di->voltage_b, cal_offset,
3561 rk81x_bat_get_ioffset(di));
3565 static void rk81x_bat_update_info(struct rk81x_battery *di)
3569 else if (di->dsoc < 0)
3573 * we need update fcc in continuous charging state, if discharge state
3574 * keep at least 2 hour, we decide not to update fcc, so clear the
3575 * fcc update flag: dod0_status.
3577 if (BASE_TO_MIN(di->plug_out_base) > 120)
3578 di->dod0_status = 0;
3580 di->voltage = rk81x_bat_get_vol(di);
3581 di->current_avg = rk81x_bat_get_avg_current(di);
3582 di->chrg_status = rk81x_bat_get_chrg_status(di);
3583 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3584 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3585 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3586 /*rk81x_bat_chrg_over_time_check(di);*/
3587 rk81x_bat_update_calib_param(di);
3588 if (di->chrg_status == CC_OR_CV)
3589 di->enter_finish = true;
3590 #if defined(CONFIG_ARCH_ROCKCHIP)
3591 rk81x_bat_status_check(di);/* ac_online, usb_online, status*/
3594 if (!rk81x_chrg_online(di) && di->s2r)
3597 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3598 if (di->remain_capacity > di->fcc) {
3599 rk81x_bat_capacity_init(di, di->fcc);
3600 rk81x_bat_capacity_init_post(di);
3601 di->remain_capacity = di->fcc;
3604 di->rsoc = rk81x_bat_get_rsoc(di);
3607 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3609 if (di->slp_psy_status)
3610 return rk81x_bat_sleep_chrg(di);
3612 return rk81x_bat_sleep_dischrg(di);
3615 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3617 u8 ocv_soc, soc_level;
3618 int relax_vol = di->relax_voltage;
3623 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3624 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3625 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3627 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3628 (di->dod0_status == 0) &&
3630 di->dod0_voltage = relax_vol;
3631 di->dod0_capacity = di->temp_nac;
3636 di->dod0_level = 100;
3637 else if (ocv_soc < 5)
3638 di->dod0_level = 90;
3640 di->dod0_level = 80;
3642 /* save_soc = di->dod0_level; */
3643 soc_level = rk81x_bat_get_level(di);
3644 if (soc_level > di->dod0_level) {
3645 di->dod0_status = 0;
3647 di->dod0_status = 1;
3649 di->fcc_update_sec = get_runtime_sec();
3652 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3653 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3655 di->dod0_voltage, di->dod0_capacity,
3656 ocv_soc, soc_level, di->dod0_status,
3661 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3666 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3669 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3671 dev_info(di->dev, "set charge to %s termination mode\n",
3672 mode ? "digital" : "analog");
3675 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3677 struct rk81x_battery *di;
3679 di = container_of(work, struct rk81x_battery,
3680 chrg_term_mode_switch_work.work);
3682 if (rk81x_chrg_online(di))
3683 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3685 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3688 static void rk81x_battery_work(struct work_struct *work)
3690 struct rk81x_battery *di;
3691 int ms = TIMER_MS_COUNTS;
3693 di = container_of(work, struct rk81x_battery,
3694 battery_monitor_work.work);
3695 if (rk81x_chrg_online(di)) {
3696 rk81x_bat_wait_finish_sig(di);
3697 /*rk81x_bat_chrg_finish_routine(di);*/
3699 rk81x_bat_fcc_flag_check(di);
3700 rk81x_bat_arbitrate_rsoc_trend(di);
3701 rk81x_bat_display_smooth(di);
3702 rk81x_bat_update_time(di);
3703 rk81x_bat_update_info(di);
3704 rk81x_bat_rsoc_check(di);
3705 rk81x_bat_power_supply_changed(di);
3706 rk81x_bat_save_dsoc(di, di->dsoc);
3707 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3709 rk81x_bat_dbg_dmp_info(di);
3711 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3712 ms = 30 * TIMER_MS_COUNTS;
3714 di->early_resume = 0;
3718 queue_delayed_work(di->wq, &di->battery_monitor_work,
3719 msecs_to_jiffies(ms));
3722 static void rk81x_battery_charge_check_work(struct work_struct *work)
3724 struct rk81x_battery *di = container_of(work,
3725 struct rk81x_battery, charge_check_work.work);
3727 DBG("rk81x_battery_charge_check_work\n");
3728 rk81x_charge_disable_open_otg(di);
3731 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3733 int register_battery_notifier(struct notifier_block *nb)
3735 return blocking_notifier_chain_register(&battery_chain_head, nb);
3737 EXPORT_SYMBOL_GPL(register_battery_notifier);
3739 int unregister_battery_notifier(struct notifier_block *nb)
3741 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3743 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3745 int battery_notifier_call_chain(unsigned long val)
3747 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3748 == NOTIFY_BAD) ? -EINVAL : 0;
3750 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3752 static void poweron_lowerpoer_handle(struct rk81x_battery *di)
3754 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
3755 if ((di->dsoc <= 2) &&
3756 (di->psy_status == POWER_SUPPLY_STATUS_DISCHARGING)) {
3758 /* kernel_power_off(); */
3763 static int rk81x_bat_notifier_call(struct notifier_block *nb,
3764 unsigned long event, void *data)
3766 struct rk81x_battery *di =
3767 container_of(nb, struct rk81x_battery, battery_nb);
3771 DBG(" CHARGE enable\n");
3773 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3774 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3775 queue_delayed_work(di->wq, &di->charge_check_work,
3776 msecs_to_jiffies(50));
3780 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3781 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3782 queue_delayed_work(di->wq, &di->charge_check_work,
3783 msecs_to_jiffies(50));
3784 DBG("charge disable OTG enable\n");
3787 poweron_lowerpoer_handle(di);
3795 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3797 pr_info("\n------- %s:lower power warning!\n", __func__);
3799 rk_send_wakeup_key();
3804 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3806 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3807 rk_send_wakeup_key();
3811 static irqreturn_t rk81x_vbat_plug_out(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_charge_ok(int irq, void *bat)
3820 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3822 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3823 di->finish_sig_base = get_runtime_sec();
3824 rk_send_wakeup_key();
3828 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3830 rk_send_wakeup_key();
3835 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3840 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3841 ret = sysfs_create_file(&di->bat.dev->kobj,
3842 &rk818_bat_attr[i].attr);
3844 dev_err(di->dev, "create battery node(%s) error\n",
3845 rk818_bat_attr[i].attr.name);
3851 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3853 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3855 struct rk818 *chip = di->rk818;
3857 #if defined(CONFIG_X86_INTEL_SOFIA)
3858 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3859 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3860 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3861 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3863 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3864 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3865 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3866 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3869 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3870 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3872 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3874 di->irq = vb_lo_irq;
3875 enable_irq_wake(di->irq);
3877 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3878 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3881 dev_err(chip->dev, "plug_in_irq request failed!\n");
3883 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3884 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3887 dev_err(chip->dev, "plug_out_irq request failed!\n");
3889 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3890 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3893 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3896 static void rk81x_bat_info_init(struct rk81x_battery *di,
3899 unsigned long time_base = get_runtime_sec();
3901 di->cell.config = di->pdata->cell_cfg;
3902 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3903 di->qmax = di->pdata->cell_cfg->design_qmax;
3904 di->early_resume = 1;
3905 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3906 di->bat_res = di->pdata->sense_resistor_mohm;
3907 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3908 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3909 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3910 di->loader_charged = loader_charged;
3911 di->chrg_finish_base = time_base;
3912 di->power_on_base = time_base;
3913 di->plug_in_base = time_base;
3914 di->plug_out_base = time_base;
3915 di->finish_sig_base = time_base;
3916 di->fcc = rk81x_bat_get_fcc(di);
3919 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3920 struct device_node *np)
3922 struct device *dev = di->dev;
3923 enum of_gpio_flags flags;
3926 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3927 if (di->dc_det_pin == -EPROBE_DEFER)
3928 dev_err(dev, "dc_det_gpio error\n");
3929 if (gpio_is_valid(di->dc_det_pin))
3930 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3931 RK818_DC_IN : RK818_DC_OUT;
3932 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3933 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3934 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3935 "rk81x_dc_det", NULL);
3938 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3939 enable_irq_wake(di->dc_det_irq);
3942 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3947 struct timespec tv = {
3948 .tv_nsec = NSEC_PER_SEC >> 1,
3950 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3952 err = rtc_read_time(rtc, &tm);
3954 dev_err(rtc->dev.parent,
3955 "hctosys: unable to read the hardware clock\n");
3957 err = rtc_valid_tm(&tm);
3959 dev_err(rtc->dev.parent,
3960 "hctosys: invalid date/time\n");
3963 rtc_tm_to_time(&tm, &tv.tv_sec);
3964 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3970 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
3972 struct device_node *np;
3973 struct battery_platform_data *pdata;
3974 struct cell_config *cell_cfg;
3975 struct ocv_config *ocv_cfg;
3976 struct property *prop;
3977 struct rk818 *rk818 = di->rk818;
3978 struct device *dev = di->dev;
3983 np = of_find_node_by_name(rk818->dev->of_node, "battery");
3985 dev_err(dev, "battery node not found!\n");
3989 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
3993 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3997 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4001 prop = of_find_property(np, "ocv_table", &length);
4003 dev_err(dev, "ocv_table not found!\n");
4006 pdata->ocv_size = length / sizeof(u32);
4007 if (pdata->ocv_size <= 0) {
4008 dev_err(dev, "invalid ocv table\n");
4012 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4014 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4015 if (!pdata->battery_ocv)
4018 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4023 /******************** charger param ****************************/
4024 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4026 dev_err(dev, "max_chrg_currentmA not found!\n");
4027 out_value = DEFAULT_CHRG_CUR;
4029 pdata->max_charger_currentmA = out_value;
4031 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4033 dev_err(dev, "max_charger_ilimitmA not found!\n");
4034 out_value = DEFAULT_INPUT_CUR;
4036 pdata->max_charger_ilimitmA = out_value;
4038 ret = of_property_read_u32(np, "bat_res", &out_value);
4040 dev_err(dev, "bat_res not found!\n");
4041 out_value = DEFAULT_BAT_RES;
4043 pdata->sense_resistor_mohm = out_value;
4045 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4047 dev_err(dev, "max_charge_voltagemV not found!\n");
4048 out_value = DEFAULT_CHRG_VOL;
4050 pdata->max_charger_voltagemV = out_value;
4052 ret = of_property_read_u32(np, "design_capacity", &out_value);
4054 dev_err(dev, "design_capacity not found!\n");
4057 cell_cfg->design_capacity = out_value;
4059 ret = of_property_read_u32(np, "design_qmax", &out_value);
4061 dev_err(dev, "design_qmax not found!\n");
4064 cell_cfg->design_qmax = out_value;
4066 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4068 dev_err(dev, "sleep_enter_current not found!\n");
4069 out_value = DEFAULT_SLP_ENTER_CUR;
4071 ocv_cfg->sleep_enter_current = out_value;
4073 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4075 dev_err(dev, "sleep_exit_current not found!\n");
4076 out_value = DEFAULT_SLP_EXIT_CUR;
4078 ocv_cfg->sleep_exit_current = out_value;
4080 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4082 dev_warn(dev, "power_off_thresd not found!\n");
4083 out_value = PWR_OFF_THRESD;
4085 pdata->power_off_thresd = out_value;
4087 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4088 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4089 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4091 /************* charger support adp types **********************/
4092 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4093 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4095 if (!support_usb_adp && !support_dc_adp) {
4096 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4097 support_usb_adp = 1;
4101 rk81x_bat_dc_det_init(di, np);
4103 cell_cfg->ocv = ocv_cfg;
4104 pdata->cell_cfg = cell_cfg;
4107 DBG("\nthe battery dts info dump:\n"
4109 "max_input_currentmA:%d\n"
4110 "max_chrg_currentmA:%d\n"
4111 "max_charge_voltagemV:%d\n"
4112 "design_capacity:%d\n"
4114 "sleep_enter_current:%d\n"
4115 "sleep_exit_current:%d\n"
4116 "support_usb_adp:%d\n"
4117 "support_dc_adp:%d\n"
4118 "power_off_thresd:%d\n",
4119 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4120 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4121 cell_cfg->design_capacity, cell_cfg->design_qmax,
4122 cell_cfg->ocv->sleep_enter_current,
4123 cell_cfg->ocv->sleep_exit_current,
4124 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4130 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4136 static int rk81x_battery_probe(struct platform_device *pdev)
4138 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4139 struct rk81x_battery *di;
4142 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4146 di->dev = &pdev->dev;
4147 platform_set_drvdata(pdev, di);
4149 ret = rk81x_bat_parse_dt(di);
4151 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4155 rk81x_bat_info_init(di, chip);
4156 if (!is_rk81x_bat_exist(di)) {
4157 dev_info(di->dev, "not battery, enter test power mode\n");
4158 di->fg_drv_mode = TEST_POWER_MODE;
4161 ret = rk81x_bat_power_supply_init(di);
4163 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4167 rk81x_bat_irq_init(di);
4168 rk81x_bat_sysfs_init(di);
4170 rk81x_bat_fg_init(di);
4171 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4173 rk81x_bat_flatzone_vol_init(di);
4175 #if defined(CONFIG_X86_INTEL_SOFIA)
4176 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4177 if (IS_ERR_OR_NULL(di->usb_phy)) {
4178 dev_err(di->dev, "get usb phy failed\n");
4179 return PTR_ERR(di->usb_phy);
4181 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4182 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4184 dev_err(di->dev, "registr usb phy notification failed\n");
4185 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4186 rk81x_battery_usb_notifier_delayed_work);
4189 rk81x_battery_register_fb_notify(di);
4190 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4191 "rk81x-battery-work");
4192 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4193 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4194 rk81x_chrg_term_mode_switch_work);
4196 queue_delayed_work(di->wq, &di->battery_monitor_work,
4197 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4199 INIT_DELAYED_WORK(&di->charge_check_work,
4200 rk81x_battery_charge_check_work);
4201 di->battery_nb.notifier_call = rk81x_bat_notifier_call;
4202 register_battery_notifier(&di->battery_nb);
4204 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4209 static int rk81x_battery_suspend(struct platform_device *dev,
4212 struct rk81x_battery *di = platform_get_drvdata(dev);
4214 di->slp_psy_status = rk81x_chrg_online(di);
4216 di->chrg_status = rk81x_bat_get_chrg_status(di);
4217 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4218 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4219 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4220 di->dischrg_normal_base = 0;
4221 di->dischrg_emu_base = 0;
4222 do_gettimeofday(&di->suspend_rtc_base);
4224 if (!rk81x_chrg_online(di)) {
4225 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4226 di->chrg_normal_base = 0;
4227 di->chrg_emu_base = 0;
4228 di->chrg_term_base = 0;
4229 di->chrg_finish_base = 0;
4234 * do not modify the g_base_sec
4236 g_base_sec = get_runtime_sec();
4238 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4239 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4240 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4245 static int rk81x_battery_resume(struct platform_device *dev)
4247 struct rk81x_battery *di = platform_get_drvdata(dev);
4248 int pwroff_thresd = di->pdata->power_off_thresd;
4253 di->discharge_smooth_status = true;
4254 di->charge_smooth_status = true;
4256 di->voltage = rk81x_bat_get_vol(di);
4257 di->current_avg = rk81x_bat_get_avg_current(di);
4258 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4259 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4260 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4261 delta_time = rk81x_bat_get_suspend_sec(di);
4262 di->suspend_time_sum += delta_time;
4263 #if defined(CONFIG_ARCH_ROCKCHIP)
4264 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4267 if (di->slp_psy_status) {
4268 time_step = CHRG_TIME_STEP;
4270 if (di->voltage <= pwroff_thresd + 50)
4271 time_step = DISCHRG_TIME_STEP_0;
4273 time_step = DISCHRG_TIME_STEP_1;
4276 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4277 di->current_avg, di->voltage, di->est_ocv_vol,
4278 di->relax_voltage, delta_time, di->suspend_time_sum,
4279 di->slp_psy_status);
4281 if (di->suspend_time_sum > time_step) {
4282 delta_soc = rk81x_bat_update_resume_state(di);
4284 di->suspend_time_sum = 0;
4287 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4288 rk81x_chrg_online(di))
4289 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4291 * do not modify the g_base_sec
4293 if (is_local_clock_reset())
4294 g_base_sec += delta_time;
4301 static int rk81x_battery_remove(struct platform_device *dev)
4303 struct rk81x_battery *di = platform_get_drvdata(dev);
4305 cancel_delayed_work_sync(&di->battery_monitor_work);
4309 static void rk81x_battery_shutdown(struct platform_device *dev)
4311 struct rk81x_battery *di = platform_get_drvdata(dev);
4313 cancel_delayed_work_sync(&di->battery_monitor_work);
4314 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4315 rk81x_bat_check_reboot(di);
4317 rk81x_bat_save_reboot_cnt(di, 0);
4318 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4321 static struct platform_driver rk81x_battery_driver = {
4323 .name = "rk818-battery",
4324 .owner = THIS_MODULE,
4327 .probe = rk81x_battery_probe,
4328 .remove = rk81x_battery_remove,
4329 .suspend = rk81x_battery_suspend,
4330 .resume = rk81x_battery_resume,
4331 .shutdown = rk81x_battery_shutdown,
4334 static int __init battery_init(void)
4336 return platform_driver_register(&rk81x_battery_driver);
4339 fs_initcall_sync(battery_init);
4340 static void __exit battery_exit(void)
4342 platform_driver_unregister(&rk81x_battery_driver);
4344 module_exit(battery_exit);
4346 MODULE_LICENSE("GPL");
4347 MODULE_ALIAS("platform:rk818-battery");
4348 MODULE_AUTHOR("ROCKCHIP");