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 /*if voltage is lower than this thresd,
153 we consider it as invalid
155 #define INVALID_VOL_THRESD 2500
156 #define PWR_OFF_THRESD 3400
157 #define MIN_ZERO_ACCURACY 10 /*0.01%*/
159 #define MAX_FCC 10000
162 * the following table value depends on datasheet
164 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
166 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
167 2250, 2400, 2600, 2800, 3000};
169 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
170 2000, 2250, 2500, 2750, 3000};
172 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
174 #define RK818_DC_IN 0
175 #define RK818_DC_OUT 1
177 #define OCV_VALID_SHIFT (0)
178 #define OCV_CALIB_SHIFT (1)
179 #define FIRST_PWRON_SHIFT (2)
181 #define SEC_TO_MIN(x) ((x) / 60)
183 struct rk81x_battery {
185 struct cell_state cell;
186 struct power_supply bat;
187 struct power_supply ac;
188 struct power_supply usb;
189 struct delayed_work work;
191 struct pinctrl *pinctrl;
192 struct pinctrl_state *pins_default;
194 struct battery_platform_data *pdata;
207 uint16_t voltage_ocv;
208 uint16_t relax_voltage;
214 bool pcb_ioffset_updated;
231 int bat_res_update_cnt;
237 unsigned long dod0_time;
250 int current_k;/* (ICALIB0, ICALIB1) */
253 int voltage_k;/* VCALIB0 VCALIB1 */
256 int zero_timeout_cnt;
257 int zero_old_remain_cap;
262 int charge_smooth_time;
266 unsigned long suspend_time_sum;
270 int suspend_charge_current;
273 bool charge_smooth_status;
274 bool discharge_smooth_status;
280 struct notifier_block battery_nb;
281 struct usb_phy *usb_phy;
282 struct notifier_block usb_nb;
283 struct notifier_block fb_nb;
286 int s2r; /*suspend to resume*/
287 struct workqueue_struct *wq;
288 struct delayed_work battery_monitor_work;
289 struct delayed_work charge_check_work;
290 struct delayed_work usb_phy_delay_work;
291 struct delayed_work chrg_term_mode_switch_work;
295 struct wake_lock resume_wake_lock;
296 unsigned long plug_in_base;
297 unsigned long plug_out_base;
298 unsigned long finish_sig_base;
299 unsigned long power_on_base;
304 bool is_first_poweron;
307 int debug_finish_real_soc;
308 int debug_finish_temp_soc;
314 unsigned long wakeup_sec;
315 u32 delta_vol_smooth;
316 unsigned long dischrg_normal_base;
317 unsigned long dischrg_emu_base;
318 unsigned long chrg_normal_base;
319 unsigned long chrg_term_base;
320 unsigned long chrg_emu_base;
321 unsigned long chrg_finish_base;
322 unsigned long fcc_update_sec;
324 u8 dischrg_algorithm_mode;
325 int last_zero_mode_dsoc;
327 unsigned long dischrg_save_sec;
328 unsigned long chrg_save_sec;
329 struct timeval suspend_rtc_base;
332 u32 support_usb_adp, support_dc_adp;
334 #define to_device_info(x) container_of((x), \
335 struct rk81x_battery, bat)
337 #define to_ac_device_info(x) container_of((x), \
338 struct rk81x_battery, ac)
340 #define to_usb_device_info(x) container_of((x), \
341 struct rk81x_battery, usb)
343 static int loader_charged;
345 static int __init rk81x_bat_loader_charged(char *__unused)
349 pr_info("battery charged in loader\n");
353 __setup("loader_charged", rk81x_bat_loader_charged);
355 static u64 g_base_sec;
356 static u64 get_runtime_sec(void)
358 u64 ts_ns = local_clock();
360 do_div(ts_ns, 1000000000);
362 return ts_ns + g_base_sec;
365 static u64 is_local_clock_reset(void)
367 u64 ts_ns = local_clock();
369 do_div(ts_ns, 1000000000);
374 static inline unsigned long BASE_TO_SEC(unsigned long x)
377 return get_runtime_sec() - x;
382 static inline unsigned long BASE_TO_MIN(unsigned long x)
384 return BASE_TO_SEC(x) / 60;
387 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
392 case HW_ADP_TYPE_USB:
400 case HW_ADP_TYPE_DUAL:
401 if (support_usb_adp && support_dc_adp)
411 static bool rk81x_chrg_online(struct rk81x_battery *di)
413 return di->usb_online || di->ac_online;
416 static u32 interpolate(int value, u32 *table, int size)
421 for (i = 0; i < size; i++) {
422 if (value < table[i])
426 if ((i > 0) && (i < size)) {
427 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
428 d /= table[i] - table[i-1];
429 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
431 d = i * ((INTERPOLATE_MAX + size / 2) / size);
440 /* Returns (a * b) / c */
441 static int32_t ab_div_c(u32 a, u32 b, u32 c)
447 sign = ((((a^b)^c) & 0x80000000) != 0);
453 tmp = (a * b + (c >> 1)) / c;
465 static int div(int val)
467 return (val == 0) ? 1 : val;
470 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
471 u8 buf[], unsigned len)
476 for (i = 0; ret < 0 && i < 3; i++) {
477 ret = rk818_i2c_read(di->rk818, reg, len, buf);
479 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
482 return (ret < 0) ? ret : 0;
485 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
486 u8 const buf[], unsigned len)
491 for (i = 0; ret < 0 && i < 3; i++) {
492 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
494 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
497 return (ret < 0) ? ret : 0;
500 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
505 for (i = 0; ret < 0 && i < 3; i++) {
506 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
508 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
514 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
519 for (i = 0; ret < 0 && i < 3; i++) {
520 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
522 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
528 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
533 rk81x_bat_read(di, reg, &buf, 1);
534 val = (buf & BIT(shift)) >> shift;
538 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
543 DBG("%s dump charger register start:\n", __func__);
544 for (i = 0xAC; i < 0xEE; i++) {
545 rk81x_bat_read(di, i, &buf, 1);
546 DBG("0x%02x : 0x%02x\n", i, buf);
551 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
556 DBG("%s dump the register start:\n", __func__);
557 for (i = 0x99; i < 0xAB; i++) {
558 rk81x_bat_read(di, i, &buf, 1);
559 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
564 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
566 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
567 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
570 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
572 rk81x_bat_reset_zero_var(di);
573 di->trend_start_cap = di->remain_capacity;
576 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
582 delta_cap = capacity - di->remain_capacity;
586 di->adjust_cap += delta_cap;
588 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
590 buf = (capacity_ma >> 24) & 0xff;
591 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
592 buf = (capacity_ma >> 16) & 0xff;
593 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
594 buf = (capacity_ma >> 8) & 0xff;
595 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
596 buf = (capacity_ma & 0xff) | 0x01;
597 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
598 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
602 dev_info(di->dev, "update capacity :%d--remain_cap:%d\n",
603 capacity, di->remain_capacity);
608 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
610 static ssize_t bat_calib_read(struct device *dev,
611 struct device_attribute *attr, char *buf)
613 struct power_supply *psy_bat = dev_get_drvdata(dev);
614 struct rk81x_battery *di = to_device_info(psy_bat);
617 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
619 return sprintf(buf, "%d\n", val);
622 static ssize_t bat_calib_write(struct device *dev,
623 struct device_attribute *attr,
624 const char *buf, size_t count)
628 struct power_supply *psy_bat = dev_get_drvdata(dev);
629 struct rk81x_battery *di = to_device_info(psy_bat);
631 ret = kstrtou8(buf, 0, &val);
636 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
638 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
643 * interface for debug: force battery to over discharge
645 static ssize_t bat_test_power_read(struct device *dev,
646 struct device_attribute *attr, char *buf)
648 struct power_supply *psy_bat = dev_get_drvdata(dev);
649 struct rk81x_battery *di = to_device_info(psy_bat);
651 return sprintf(buf, "%d\n", di->fg_drv_mode);
654 static ssize_t bat_test_power_write(struct device *dev,
655 struct device_attribute *attr,
656 const char *buf, size_t count)
660 struct power_supply *psy_bat = dev_get_drvdata(dev);
661 struct rk81x_battery *di = to_device_info(psy_bat);
663 ret = kstrtou8(buf, 0, &val);
668 di->fg_drv_mode = TEST_POWER_MODE;
670 di->fg_drv_mode = FG_NORMAL_MODE;
675 static ssize_t bat_fcc_read(struct device *dev,
676 struct device_attribute *attr, char *buf)
678 struct power_supply *psy_bat = dev_get_drvdata(dev);
679 struct rk81x_battery *di = to_device_info(psy_bat);
681 return sprintf(buf, "%d\n", di->fcc);
684 static ssize_t bat_fcc_write(struct device *dev,
685 struct device_attribute *attr,
686 const char *buf, size_t count)
690 struct power_supply *psy_bat = dev_get_drvdata(dev);
691 struct rk81x_battery *di = to_device_info(psy_bat);
693 ret = kstrtou16(buf, 0, &val);
702 static ssize_t bat_dsoc_read(struct device *dev,
703 struct device_attribute *attr, char *buf)
705 struct power_supply *psy_bat = dev_get_drvdata(dev);
706 struct rk81x_battery *di = to_device_info(psy_bat);
708 return sprintf(buf, "%d\n", di->dsoc);
711 static ssize_t bat_dsoc_write(struct device *dev,
712 struct device_attribute *attr,
713 const char *buf, size_t count)
717 struct power_supply *psy_bat = dev_get_drvdata(dev);
718 struct rk81x_battery *di = to_device_info(psy_bat);
720 ret = kstrtou8(buf, 0, &val);
729 static ssize_t bat_rsoc_read(struct device *dev,
730 struct device_attribute *attr, char *buf)
732 struct power_supply *psy_bat = dev_get_drvdata(dev);
733 struct rk81x_battery *di = to_device_info(psy_bat);
735 return sprintf(buf, "%d\n", di->rsoc);
738 static ssize_t bat_rsoc_write(struct device *dev,
739 struct device_attribute *attr,
740 const char *buf, size_t count)
745 struct power_supply *psy_bat = dev_get_drvdata(dev);
746 struct rk81x_battery *di = to_device_info(psy_bat);
748 ret = kstrtou8(buf, 0, &val);
752 capacity = di->fcc * val / 100;
753 rk81x_bat_capacity_init(di, capacity);
754 rk81x_bat_capacity_init_post(di);
759 static ssize_t bat_remain_cap_read(struct device *dev,
760 struct device_attribute *attr,
763 struct power_supply *psy_bat = dev_get_drvdata(dev);
764 struct rk81x_battery *di = to_device_info(psy_bat);
766 return sprintf(buf, "%d\n", di->remain_capacity);
769 static struct device_attribute rk818_bat_attr[] = {
770 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
771 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
772 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
773 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
774 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
775 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
779 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
784 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
786 dev_err(di->dev, "error reading TS_CTRL_REG");
791 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
796 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
798 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
801 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
805 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
810 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
816 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
818 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
821 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
825 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
831 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
833 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
836 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
840 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
846 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
848 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
854 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
860 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
862 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
868 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
874 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
875 buf = (value >> 8) & 0xff;
876 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
881 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
883 int vcalib0, vcalib1;
885 vcalib0 = rk81x_bat_get_vcalib0(di);
886 vcalib1 = rk81x_bat_get_vcalib1(di);
888 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
889 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
890 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
893 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
898 uint16_t voltage_now = 0;
902 for (i = 0; i < 3; i++) {
903 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
905 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
909 dev_err(di->dev, "error read BAT_OCV_REGH");
914 if (val[0] == val[1])
919 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
924 static int rk81x_bat_get_vol(struct rk81x_battery *di)
933 for (i = 0; i < 3; i++) {
934 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
936 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
940 dev_err(di->dev, "error read BAT_VOL_REGH");
945 if (val[0] == val[1])
950 vol = di->voltage_k * temp / 1000 + di->voltage_b;
955 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
960 ret = rk81x_bat_read(di, GGSTS, &status, 1);
962 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
968 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
972 uint16_t temp = 0, voltage_now;
974 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
976 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
979 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
984 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
988 uint16_t temp = 0, voltage_now;
990 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
992 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
995 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
1000 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1004 uint16_t relax_vol1, relax_vol2;
1007 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1008 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1010 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1011 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1012 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1013 __func__, status, ggcon, relax_vol1, relax_vol2);
1015 if (is_rk81x_bat_relax_mode(di))
1016 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1022 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1023 * based on the voltage.
1025 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1032 ocv_table = di->pdata->battery_ocv;
1033 ocv_size = di->pdata->ocv_size;
1034 tmp = interpolate(voltage, ocv_table, ocv_size);
1035 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1036 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1041 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1047 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1049 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1053 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1055 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1061 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1071 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1075 rk81x_bat_read(di, GGCON, &ggcon, 1);
1076 ggcon &= ~(0x30); /*clear <5:4>*/
1078 rk81x_bat_write(di, GGCON, &ggcon, 1);
1082 * when charger finish signal comes, we need calibrate the current, make it
1085 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1095 if ((di->chrg_status == CHARGE_FINISH) &&
1096 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1097 (abs(di->current_avg) > 4)) {
1098 for (retry = 0; retry < 5; retry++) {
1099 adc_value = rk81x_bat_get_raw_adc_current(di);
1100 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1101 dev_dbg(di->dev, "charger plugout\n");
1106 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1107 C0 = rk81x_bat_get_cal_offset(di);
1108 C1 = adc_value + C0;
1109 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1111 rk81x_bat_set_cal_offset(di, C1);
1112 DBG("<%s>. new cal_offset = %d\n",
1113 __func__, rk81x_bat_get_cal_offset(di));
1115 adc_value = rk81x_bat_get_raw_adc_current(di);
1116 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1117 if (abs(adc_value) < 4) {
1118 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1119 ioffset = rk81x_bat_get_ioffset(di);
1120 rk81x_bat_set_cal_offset(di,
1123 ioffset = rk81x_bat_get_ioffset(di);
1124 pcb_offset = C1 - ioffset;
1125 di->pcb_ioffset = pcb_offset;
1126 di->pcb_ioffset_updated = true;
1131 DBG("<%s>. update the cal_offset, C1 = %d\n"
1132 "i_offset = %d, pcb_offset = %d\n",
1133 __func__, C1, ioffset, pcb_offset);
1137 dev_warn(di->dev, "ioffset cal failed\n");
1138 rk81x_bat_set_cal_offset(di, C0);
1141 di->pcb_ioffset_updated = false;
1148 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1151 int enter_thres, exit_thres;
1152 struct cell_state *cell = &di->cell;
1154 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1155 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1156 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1157 __func__, cell->config->ocv->sleep_enter_current,
1158 cell->config->ocv->sleep_exit_current);
1160 buf = enter_thres & 0xff;
1161 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1162 buf = (enter_thres >> 8) & 0xff;
1163 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1165 buf = exit_thres & 0xff;
1166 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1167 buf = (exit_thres >> 8) & 0xff;
1168 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1170 /* set sample time */
1171 rk81x_bat_read(di, GGCON, &buf, 1);
1172 buf &= ~(3 << 2);/*8min*/
1173 buf &= ~0x01; /* clear bat_res calc*/
1174 rk81x_bat_write(di, GGCON, &buf, 1);
1177 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1182 rk81x_bat_read(di, GGCON, &ggcon, 1);
1184 rk81x_bat_write(di, GGCON, &ggcon, 1);
1186 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1188 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1191 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1200 for (i = 0; i < 3; i++) {
1201 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1203 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1208 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1210 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1216 if (val[0] == val[1])
1217 current_now = val[0];
1219 current_now = val[2];
1221 if (current_now & 0x800)
1222 current_now -= 4096;
1224 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1229 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1230 enum charger_type charger_type)
1232 di->usb_online = OFFLINE;
1233 di->ac_online = OFFLINE;
1235 switch (charger_type) {
1237 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1240 di->usb_online = ONLINE;
1241 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1243 case DC_CHARGER:/*treat dc as ac*/
1245 di->ac_online = ONLINE;
1246 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1249 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1253 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1254 msecs_to_jiffies(1000));
1257 /* high load: current < 0 with charger in.
1258 * System will not shutdown while dsoc=0% with charging state(ac_online),
1259 * which will cause over discharge, so oppose status before report states.
1261 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1264 int pwr_off_thresd = di->pdata->power_off_thresd - 50;
1266 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1268 time = get_runtime_sec();
1270 if (BASE_TO_SEC(time) > (MINUTE)) {
1271 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1272 dev_info(di->dev, "low power....\n");
1279 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1283 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1285 return (buf & 0x80) ? 1 : 0;
1288 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1293 rk81x_bat_read(di, GGSTS, &buf, 1);
1294 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1295 /*di->pwron_bat_con = buf;*/
1299 rk81x_bat_write(di, GGSTS, &buf, 1);
1300 rk81x_bat_read(di, GGSTS, &temp, 1);
1301 } while (temp & BAT_CON);
1308 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1315 ocv_table = di->pdata->battery_ocv;
1316 ocv_size = di->pdata->ocv_size;
1318 for (j = 0; j < 21; j++)
1322 for (i = 1; i < ocv_size-1; i++) {
1323 if (ocv_table[i+1] < ocv_table[i] + 20)
1324 temp_table[j++] = i;
1327 temp_table[j] = temp_table[j-1] + 1;
1329 di->enter_flatzone = ocv_table[i];
1332 for (i = 0; i < 20; i++) {
1333 if (temp_table[i] < temp_table[i+1])
1338 di->exit_flatzone = ocv_table[i];
1340 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1341 di->enter_flatzone, di->exit_flatzone);
1344 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1346 u8 ocv_valid, first_pwron;
1350 /*buf==1: OCV_VOL is valid*/
1351 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1352 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1353 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1355 if (first_pwron == 1 || ocv_valid == 1) {
1356 DBG("<%s> enter.\n", __func__);
1357 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1358 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1359 di->dod0_voltage = ocv_voltage;
1360 di->dod0_capacity = di->temp_nac;
1365 di->dod0_level = 100;
1366 else if (ocv_soc < 5)
1367 di->dod0_level = 95;
1368 else if (ocv_soc < 10)
1369 di->dod0_level = 90;
1371 di->dod0_level = 80;
1372 /* save_soc = di->dod0_level; */
1373 soc_level = rk81x_bat_get_level(di);
1374 if (soc_level > di->dod0_level) {
1375 di->dod0_status = 0;
1377 if (soc_level <= 80)
1379 rk81x_bat_save_level(di, soc_level);
1381 di->dod0_status = 1;
1383 di->fcc_update_sec = get_runtime_sec();
1386 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1387 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1389 di->dod0_voltage, di->dod0_capacity,
1390 ocv_soc, soc_level, di->dod0_status,
1396 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1398 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1401 static enum power_supply_property rk_battery_props[] = {
1402 POWER_SUPPLY_PROP_STATUS,
1403 POWER_SUPPLY_PROP_CURRENT_NOW,
1404 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1405 POWER_SUPPLY_PROP_PRESENT,
1406 POWER_SUPPLY_PROP_HEALTH,
1407 POWER_SUPPLY_PROP_CAPACITY,
1410 static int rk81x_battery_get_property(struct power_supply *psy,
1411 enum power_supply_property psp,
1412 union power_supply_propval *val)
1414 struct rk81x_battery *di = to_device_info(psy);
1417 case POWER_SUPPLY_PROP_CURRENT_NOW:
1418 val->intval = di->current_avg * 1000;/*uA*/
1419 if (di->fg_drv_mode == TEST_POWER_MODE)
1420 val->intval = TEST_CURRENT * 1000;
1422 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1423 val->intval = di->voltage * 1000;/*uV*/
1424 if (di->fg_drv_mode == TEST_POWER_MODE)
1425 val->intval = TEST_VOLTAGE * 1000;
1428 case POWER_SUPPLY_PROP_PRESENT:
1429 val->intval = is_rk81x_bat_exist(di);
1430 if (di->fg_drv_mode == TEST_POWER_MODE)
1431 val->intval = TEST_PRESET;
1434 case POWER_SUPPLY_PROP_CAPACITY:
1435 val->intval = di->dsoc;
1436 if (di->fg_drv_mode == TEST_POWER_MODE)
1437 val->intval = TEST_SOC;
1439 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1441 case POWER_SUPPLY_PROP_HEALTH:
1442 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1444 case POWER_SUPPLY_PROP_STATUS:
1445 val->intval = di->psy_status;
1446 if (di->fg_drv_mode == TEST_POWER_MODE)
1447 val->intval = TEST_STATUS;
1457 static enum power_supply_property rk_battery_ac_props[] = {
1458 POWER_SUPPLY_PROP_ONLINE,
1461 static enum power_supply_property rk_battery_usb_props[] = {
1462 POWER_SUPPLY_PROP_ONLINE,
1465 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1466 enum power_supply_property psp,
1467 union power_supply_propval *val)
1470 struct rk81x_battery *di = to_ac_device_info(psy);
1473 case POWER_SUPPLY_PROP_ONLINE:
1474 if (rk81x_chrg_online(di))
1475 rk81x_bat_lowpwr_check(di);
1476 val->intval = di->ac_online; /*discharging*/
1477 if (di->fg_drv_mode == TEST_POWER_MODE)
1478 val->intval = TEST_AC_ONLINE;
1488 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1489 enum power_supply_property psp,
1490 union power_supply_propval *val)
1493 struct rk81x_battery *di = to_usb_device_info(psy);
1496 case POWER_SUPPLY_PROP_ONLINE:
1497 if (rk81x_chrg_online(di))
1498 rk81x_bat_lowpwr_check(di);
1499 val->intval = di->usb_online;
1500 if (di->fg_drv_mode == TEST_POWER_MODE)
1501 val->intval = TEST_USB_ONLINE;
1511 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1515 di->bat.name = "BATTERY";
1516 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1517 di->bat.properties = rk_battery_props;
1518 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1519 di->bat.get_property = rk81x_battery_get_property;
1522 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1523 di->ac.properties = rk_battery_ac_props;
1524 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1525 di->ac.get_property = rk81x_battery_ac_get_property;
1527 di->usb.name = "USB";
1528 di->usb.type = POWER_SUPPLY_TYPE_USB;
1529 di->usb.properties = rk_battery_usb_props;
1530 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1531 di->usb.get_property = rk81x_battery_usb_get_property;
1533 ret = power_supply_register(di->dev, &di->bat);
1535 dev_err(di->dev, "failed to register main battery\n");
1538 ret = power_supply_register(di->dev, &di->usb);
1540 dev_err(di->dev, "failed to register usb power supply\n");
1543 ret = power_supply_register(di->dev, &di->ac);
1545 dev_err(di->dev, "failed to register ac power supply\n");
1552 power_supply_unregister(&di->ac);
1554 power_supply_unregister(&di->usb);
1556 power_supply_unregister(&di->bat);
1561 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1565 static u32 capacity_ma;
1567 if (capacity >= di->qmax)
1568 capacity = di->qmax;
1573 if (capacity_ma == capacity)
1576 capacity_ma = capacity;
1578 buf = (capacity_ma >> 24) & 0xff;
1579 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1580 buf = (capacity_ma >> 16) & 0xff;
1581 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1582 buf = (capacity_ma >> 8) & 0xff;
1583 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1584 buf = (capacity_ma & 0xff) | 0x01;
1585 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1588 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1596 for (i = 0; i < 3; i++) {
1597 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1599 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1600 val[i] |= buf << 16;
1601 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1603 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1607 if (val[0] == val[1])
1615 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1620 capacity_ma = capacity;
1621 buf = (capacity_ma >> 24) & 0xff;
1622 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1623 buf = (capacity_ma >> 16) & 0xff;
1624 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1625 buf = (capacity_ma >> 8) & 0xff;
1626 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1627 buf = (capacity_ma & 0xff) | 0x01;
1628 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1630 dev_info(di->dev, "update fcc : %d\n", capacity);
1633 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1638 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1639 capacity = buf << 24;
1640 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1641 capacity |= buf << 16;
1642 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1643 capacity |= buf << 8;
1644 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1647 if (capacity < MIN_FCC) {
1648 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1650 capacity = di->design_capacity;
1651 rk81x_bat_save_fcc(di, capacity);
1652 } else if (capacity > di->qmax) {
1653 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1654 capacity = di->qmax;
1655 rk81x_bat_save_fcc(di, capacity);
1661 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1670 for (i = 0; i < 3; i++) {
1671 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1673 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1674 val[i] |= buf << 16;
1675 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1677 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1680 if (val[0] == val[1])
1685 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1690 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1694 if (last_soc != save_soc) {
1695 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1696 last_soc = save_soc;
1702 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1707 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1711 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1715 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1716 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1717 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1718 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1721 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1722 enum charger_type charger_type)
1724 switch (charger_type) {
1727 rk81x_bat_set_current(di, ILIM_450MA);
1731 rk81x_bat_set_current(di, di->chrg_i_lmt);
1734 rk81x_bat_set_current(di, ILIM_450MA);
1738 #if defined(CONFIG_ARCH_ROCKCHIP)
1740 * There are three ways to detect dc_adp:
1741 * 1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1742 * do not care about whether define dc_det_pin or not;
1743 * 2. define de_det_pin: check gpio level;
1744 * 3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1745 * case that: gpio invalid or not define.
1747 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1749 enum charger_type charger_type;
1753 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1755 /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1756 if (!rk81x_bat_support_adp_type(HW_ADP_TYPE_USB)) {
1757 if ((buf & PLUG_IN_STS) != 0)
1758 charger_type = DC_CHARGER;
1760 charger_type = NO_CHARGER;
1762 return charger_type;
1765 /*det by gpio level*/
1766 if (gpio_is_valid(di->dc_det_pin)) {
1767 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1769 pr_err("Failed to request gpio %d with ret:""%d\n",
1770 di->dc_det_pin, ret);
1774 gpio_direction_input(di->dc_det_pin);
1775 ret = gpio_get_value(di->dc_det_pin);
1776 if (ret == di->dc_det_level)
1777 charger_type = DC_CHARGER;
1779 charger_type = NO_CHARGER;
1781 gpio_free(di->dc_det_pin);
1782 DBG("**********rk818 dc_det_pin=%d\n", ret);
1784 return charger_type;
1786 /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1787 } else if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DUAL)) {
1788 if ((buf & PLUG_IN_STS) != 0) {
1789 charger_type = dwc_otg_check_dpdm(0);
1790 if (charger_type == 0)
1791 charger_type = DC_CHARGER;
1793 charger_type = NO_CHARGER;
1797 return charger_type;
1800 static enum charger_type rk81x_bat_get_usbac_state(struct rk81x_battery *di)
1802 enum charger_type charger_type;
1803 int usb_id, gadget_flag;
1805 usb_id = dwc_otg_check_dpdm(0);
1808 charger_type = NO_CHARGER;
1812 charger_type = USB_CHARGER;
1815 charger_type = AC_CHARGER;
1818 charger_type = NO_CHARGER;
1821 DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1822 if (charger_type == USB_CHARGER) {
1823 gadget_flag = get_gadget_connect_flag();
1824 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1825 __func__, gadget_flag, di->check_count);
1827 if (0 == gadget_flag) {
1828 if (++di->check_count >= 5) {
1829 charger_type = AC_CHARGER;
1830 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1831 __func__, di->check_count);
1833 charger_type = USB_CHARGER;
1836 charger_type = USB_CHARGER;
1837 di->check_count = 0;
1840 di->check_count = 0;
1843 return charger_type;
1847 * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
1848 * and dc_adp are plugined in together, the dc_apt has high priority.
1849 * so we check dc_apt first and return rigth away if it's found.
1851 static enum charger_type rk81x_bat_get_adp_type(struct rk81x_battery *di)
1854 enum charger_type charger_type = NO_CHARGER;
1856 /*check by ic hardware: this check make check work safer*/
1857 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1858 if ((buf & PLUG_IN_STS) == 0)
1862 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DC)) {
1863 charger_type = rk81x_bat_get_dc_state(di);
1864 if (charger_type == DC_CHARGER)
1865 return charger_type;
1869 charger_type = rk81x_bat_get_usbac_state(di);
1871 return charger_type;
1874 static void rk81x_bat_status_check(struct rk81x_battery *di)
1876 static enum charger_type old_charger_type = DUAL_CHARGER;
1877 enum charger_type charger_type;
1879 charger_type = rk81x_bat_get_adp_type(di);
1880 if (charger_type == old_charger_type)
1882 rk81x_bat_set_chrg_current(di, charger_type);
1883 rk81x_bat_set_power_supply_state(di, charger_type);
1884 old_charger_type = charger_type;
1888 #if defined(CONFIG_X86_INTEL_SOFIA)
1889 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1891 enum charger_type charger_type;
1894 charger_type = AC_CHARGER;
1896 charger_type = USB_CHARGER;
1898 charger_type = NO_CHARGER;
1902 dev_info(di->dev, "limit current:%d\n", ma);
1904 return charger_type;
1907 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1909 struct rk81x_battery *di;
1910 enum charger_type type;
1912 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1913 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1915 rk81x_bat_set_chrg_current(di, type);
1916 power_supply_changed(&di->usb);
1919 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1920 unsigned long event, void *data)
1922 struct rk81x_battery *di;
1923 struct power_supply_cable_props *cable_props;
1924 enum charger_type type;
1926 di = container_of(nb, struct rk81x_battery, usb_nb);
1932 case USB_EVENT_CHARGER:
1933 cable_props = (struct power_supply_cable_props *)data;
1934 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1935 rk81x_bat_set_power_supply_state(di, type);
1936 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1937 msecs_to_jiffies(50));
1948 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1949 unsigned long event, void *data)
1951 struct rk81x_battery *di;
1952 struct fb_event *evdata = data;
1955 di = container_of(nb, struct rk81x_battery, fb_nb);
1957 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1960 blank = *(int *)evdata->data;
1962 if (di->fb_blank != blank)
1963 di->fb_blank = blank;
1967 if (blank == FB_BLANK_UNBLANK)
1968 di->early_resume = 1;
1973 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1975 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1976 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1978 return fb_register_client(&di->fb_nb);
1982 * it is first time for battery to be weld, init by ocv table
1984 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1986 rk81x_bat_save_fcc(di, di->design_capacity);
1987 di->fcc = rk81x_bat_get_fcc(di);
1989 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1990 di->dsoc = di->rsoc;
1991 di->nac = di->temp_nac;
1993 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1994 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1995 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1996 __func__, di->dsoc, di->nac, di->fcc);
1999 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
2004 int relax_vol = di->relax_voltage;
2005 int ocv_vol = di->voltage_ocv;
2007 init_cur = rk81x_bat_get_avg_current(di);
2008 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
2010 est_vol = di->voltage - diff;
2012 if (di->loader_charged) {
2013 calib_vol = est_vol;
2017 if (di->pwroff_min > 8) {
2018 if (abs(relax_vol - ocv_vol) < 100) {
2019 calib_vol = ocv_vol;
2021 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
2022 calib_vol = ocv_vol;
2024 calib_vol = relax_vol;
2026 } else if (di->pwroff_min > 2) {
2027 calib_vol = ocv_vol;
2032 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2033 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2039 * it is not first time for battery to be weld, init by last record info
2041 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2045 int remain_capacity;
2047 int calib_vol, calib_soc, calib_capacity;
2049 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2050 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2051 init_soc = pwron_soc;
2052 remain_capacity = rk81x_bat_get_remain_capacity(di);
2054 /* check if support uboot charge,
2055 * if support, uboot charge driver should have done init work,
2056 * so here we should skip init work
2058 #if defined(CONFIG_ARCH_ROCKCHIP)
2059 if (di->loader_charged)
2062 calib_vol = rk81x_bat_get_calib_vol(di);
2063 if (calib_vol > 0) {
2064 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2065 calib_capacity = di->temp_nac;
2067 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2068 init_soc = calib_soc;
2069 remain_capacity = calib_capacity;
2071 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2072 calib_vol, init_soc, remain_capacity);
2075 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2076 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2077 __func__, remain_capacity, di->temp_nac);
2079 if (di->pwroff_min > 0) {
2080 if (di->pwroff_min > 30) {
2081 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2083 remain_capacity = di->temp_nac;
2084 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2085 __func__, remain_capacity);
2087 } else if ((di->pwroff_min > 5) &&
2088 (abs(ocv_soc - init_soc) >= 10)) {
2089 if (remain_capacity >= di->temp_nac * 120/100)
2090 remain_capacity = di->temp_nac * 110/100;
2091 else if (remain_capacity < di->temp_nac * 8/10)
2092 remain_capacity = di->temp_nac * 9/10;
2093 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2094 __func__, remain_capacity);
2097 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2100 di->dsoc = init_soc;
2101 di->nac = remain_capacity;
2104 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2105 pwron_soc, di->dsoc, di->nac);
2108 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2110 u8 curr_pwroff_min, last_pwroff_min;
2112 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2113 &curr_pwroff_min, 1);
2114 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2115 &last_pwroff_min, 1);
2117 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2118 &curr_pwroff_min, 1);
2120 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2123 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2125 u8 calib_en;/*debug*/
2127 di->voltage = rk81x_bat_get_vol(di);
2128 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2129 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2130 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2131 di->current_avg = rk81x_bat_get_avg_current(di);
2133 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2134 di->voltage, di->voltage_ocv, di->relax_voltage,
2135 di->current_avg, di->pwroff_min);
2137 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2138 DBG("readbit: calib_en=%d\n", calib_en);
2139 if (is_rk81x_bat_first_poweron(di) ||
2140 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2141 rk81x_bat_first_pwron(di);
2142 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2145 rk81x_bat_not_first_pwron(di);
2151 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2156 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2161 DBG(" CHARGE-OFF ...\n");
2165 DBG(" DEAD CHARGE ...\n");
2167 case TRICKLE_CHARGE:
2169 DBG(" TRICKLE CHARGE ...\n ");
2173 DBG(" CC or CV ...\n");
2176 ret = CHARGE_FINISH;
2177 DBG(" CHARGE FINISH ...\n");
2181 DBG(" USB OVER VOL ...\n");
2185 DBG(" BAT TMP ERROR ...\n");
2189 DBG(" TIMER ERROR ...\n");
2193 DBG(" USB EXIST ...\n");
2197 DBG(" USB EFF...\n");
2206 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2207 int chrg_ilim, int chrg_cur)
2211 di->chrg_v_lmt = DEF_CHRG_VOL;
2212 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2213 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2215 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2216 if (chrg_vol < CHRG_V_LMT[i])
2219 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2222 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2223 if (chrg_ilim < CHRG_I_LMT[i])
2226 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2229 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2230 if (chrg_cur < CHRG_I_CUR[i])
2233 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2235 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2236 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2239 static u8 rk81x_bat_select_finish_ma(int fcc)
2241 u8 ma = FINISH_150MA;
2246 else if (fcc >= 4000)
2249 else if (fcc >= 3000)
2259 * there is a timer inside rk81x to calc how long the battery is in charging
2260 * state. rk81x will close PowerPath inside IC when timer reach, which will
2261 * stop the charging work. we have to reset the corresponding bits to restart
2262 * the timer to avoid that case.
2264 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
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);
2272 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2273 buf |= CHRG_TIMER_CCCV_EN;
2274 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2275 dev_info(di->dev, "reset cccv charge timer\n");
2279 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2281 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2282 u8 sup_sts_reg, thremal_reg, ggcon;
2283 int chrg_vol, chrg_cur, chrg_ilim;
2286 chrg_vol = di->pdata->max_charger_voltagemV;
2287 chrg_cur = di->pdata->max_charger_currentmA;
2288 chrg_ilim = di->pdata->max_charger_ilimitmA;
2290 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2291 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2293 /*rk81x_bat_init_chrg_timer(di);*/
2295 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2296 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2297 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2298 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2299 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2300 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2301 rk81x_bat_read(di, GGCON, &ggcon, 1);
2303 usb_ctrl_reg &= (~0x0f);
2305 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2306 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2308 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2310 if (di->fg_drv_mode == TEST_POWER_MODE)
2311 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2313 chrg_ctrl_reg1 &= (0x00);
2314 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2316 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2317 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2319 chrg_ctrl_reg2 &= ~(0xc7);
2320 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2322 sup_sts_reg &= ~(0x01 << 3);
2323 sup_sts_reg |= (0x01 << 2);
2325 thremal_reg &= (~0x0c);
2326 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2327 ggcon |= ADC_CURRENT_MODE;
2329 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2330 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2331 /*don't touch charge setting when boot int loader charge mode*/
2332 if (!di->loader_charged)
2333 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2334 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2335 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2336 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2337 rk81x_bat_write(di, GGCON, &ggcon, 1);
2340 void rk81x_charge_disable_open_otg(struct rk81x_battery *di)
2342 int value = di->charge_otg;
2345 DBG("charge disable, enable OTG.\n");
2346 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2347 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2349 DBG("charge enable, disable OTG.\n");
2350 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2351 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2355 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2362 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2364 rk81x_bat_gauge_enable(di);
2365 /* get the volatege offset */
2366 rk81x_bat_get_vol_offset(di);
2367 rk81x_bat_charger_init(di);
2368 rk81x_bat_set_relax_thres(di);
2370 /* get the current offset , the value write to the CAL_OFFSET */
2371 di->current_offset = rk81x_bat_get_ioffset(di);
2372 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2373 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2374 __func__, pcb_offset, di->current_offset);
2376 pcb_offset = DEF_PCB_OFFSET;
2377 cal_offset = pcb_offset + di->current_offset;
2378 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2379 cal_offset = DEF_CAL_OFFSET;
2380 rk81x_bat_set_cal_offset(di, cal_offset);
2381 /* set sample time for cal_offset interval*/
2382 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2384 rk81x_bat_rsoc_init(di);
2385 rk81x_bat_capacity_init(di, di->nac);
2386 rk81x_bat_capacity_init_post(di);
2388 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2389 di->current_avg = rk81x_bat_get_avg_current(di);
2391 rk81x_bat_restart_relax(di);
2392 rk81x_bat_power_on_save(di, di->voltage_ocv);
2394 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2396 rk81x_dbg_dmp_gauge_regs(di);
2397 rk81x_dbg_dmp_charger_regs(di);
2400 "nac = %d , remain_capacity = %d\n"
2401 "OCV_voltage = %d, voltage = %d\n"
2402 "SOC = %d, fcc = %d\n, current=%d\n"
2403 "cal_offset = 0x%x\n",
2405 di->nac, di->remain_capacity,
2406 di->voltage_ocv, di->voltage,
2407 di->dsoc, di->fcc, di->current_avg,
2411 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2413 int dead_voltage, ocv_voltage;
2414 int voltage, voltage_old, voltage_now;
2419 int ocv_soc, dead_soc;
2420 int power_off_thresd = di->pdata->power_off_thresd;
2423 voltage_old = rk81x_bat_get_vol(di);
2425 voltage_now = rk81x_bat_get_vol(di);
2427 } while ((voltage_old == voltage_now) && (count_num < 11));
2428 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2431 for (i = 0; i < 10; i++) {
2432 voltage += rk81x_bat_get_vol(di);
2437 currentnow = rk81x_bat_get_avg_current(di);
2439 /* 50 mo power-path mos */
2440 dead_voltage = power_off_thresd - currentnow *
2441 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2443 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2444 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2445 dead_voltage, ocv_voltage);
2447 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2448 q_dead = di->temp_nac;
2449 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2452 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2453 q_ocv = di->temp_nac;
2454 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2457 rsoc = ocv_soc - dead_soc;
2458 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2460 } else if (rsoc > 0) {
2461 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2464 di->display_soc = di->dsoc * 1000;
2467 di->zero_old_remain_cap = di->remain_capacity;
2469 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2470 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2471 di->line_k, di->dsoc, rsoc,
2472 di->display_soc, di->zero_old_remain_cap);
2475 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2477 int delta_cap, delta_soc;
2480 di->zero_timeout_cnt++;
2481 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2482 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2484 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2485 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2486 "ZERO1: timeout_cnt=%d\n\n",
2487 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2488 delta_soc, delta_cap, di->zero_old_remain_cap,
2489 di->zero_timeout_cnt);
2491 if ((abs(delta_soc) > MIN_ZERO_ACCURACY) ||
2492 (di->zero_timeout_cnt > 500)) {
2493 DBG("ZERO1:--------- enter calc -----------\n");
2494 di->zero_timeout_cnt = 0;
2495 di->display_soc -= delta_soc;
2496 tmp_dsoc = (di->display_soc + 500) / 1000;
2497 di->dsoc = tmp_dsoc;
2499 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2500 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2502 rk81x_bat_zero_calc_linek(di);
2506 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2508 return (di->voltage -
2509 (di->bat_res * di->current_avg) / 1000);
2512 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2514 int ocv_soc, ocv_voltage;
2516 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2517 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2522 /* we will estimate a ocv voltage to get a ocv soc.
2523 * if there is a big offset between ocv_soc and rsoc,
2524 * we will decide whether we should reinit capacity or not
2526 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2528 int ocv_soc = di->est_ocv_soc;
2529 int ocv_volt = di->est_ocv_vol;
2530 int rsoc = rk81x_bat_get_rsoc(di);
2531 int max_volt = di->pdata->max_charger_voltagemV;
2533 if (ocv_volt > max_volt)
2536 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2537 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2539 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2541 di->err_soc_sum += ocv_soc;
2545 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2546 __func__, di->err_chck_cnt, di->err_soc_sum);
2548 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2549 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2550 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2551 ocv_soc += RSOC_COMPS;
2553 di->temp_nac = ocv_soc * di->fcc / 100;
2554 rk81x_bat_capacity_init(di, di->temp_nac);
2555 rk81x_bat_capacity_init_post(di);
2556 di->rsoc = rk81x_bat_get_rsoc(di);
2557 di->remain_capacity =
2558 rk81x_bat_get_realtime_capacity(di);
2559 di->err_soc_sum = 0;
2560 di->err_chck_cnt = 0;
2561 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2565 di->err_chck_cnt = 0;
2566 di->err_soc_sum = 0;
2570 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2572 u8 status = di->psy_status;
2574 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2575 (status == POWER_SUPPLY_STATUS_FULL)) {
2576 if ((di->current_avg < 0) &&
2577 (di->chrg_status != CHARGE_FINISH))
2578 rk81x_bat_rsoc_dischrg_check(di);
2581 rsoc_chrg_calib(di);
2584 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2585 rk81x_bat_rsoc_dischrg_check(di);
2589 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2591 u32 temp, soc_time = 0;
2592 unsigned long sec_unit;
2594 if (!di->dischrg_emu_base)
2595 di->dischrg_emu_base = get_runtime_sec();
2597 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2599 temp = di->fcc * 3600 / 100;
2601 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2602 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2604 soc_time = temp / div(abs(di->current_avg));
2606 if (sec_unit > soc_time) {
2608 di->dischrg_emu_base = get_runtime_sec();
2609 di->dischrg_save_sec = 0;
2612 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2613 __func__, soc_time, sec_unit);
2617 * when there is a big offset between dsoc and rsoc, dsoc needs to
2618 * speed up to keep pace witch rsoc.
2620 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2622 u32 soc_time = 0, temp;
2624 unsigned long chrg_emu_sec;
2626 if (!di->chrg_emu_base)
2627 di->chrg_emu_base = get_runtime_sec();
2629 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2630 temp = di->fcc * 3600 / 100;
2632 if (di->ac_online) {
2633 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2634 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2636 soc_time = temp / div(abs(di->current_avg));
2638 soc_time = temp / 450;
2641 plus_soc = chrg_emu_sec / soc_time;
2642 if (chrg_emu_sec > soc_time) {
2643 di->dsoc += plus_soc;
2644 di->chrg_emu_base = get_runtime_sec();
2645 di->chrg_save_sec = 0;
2648 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2649 __func__, soc_time, chrg_emu_sec, plus_soc);
2652 /* check voltage and current when dsoc is close to full.
2653 * we will do a fake charge to adjust charing speed which
2654 * aims to make battery full charged and match finish signal.
2656 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2660 unsigned long chrg_term_sec;
2662 if (!di->chrg_term_base)
2663 di->chrg_term_base = get_runtime_sec();
2665 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2666 /*check current and voltage*/
2668 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2670 plus_soc = chrg_term_sec / soc_time;
2671 if (chrg_term_sec > soc_time) {
2672 di->dsoc += plus_soc;
2673 di->chrg_term_base = get_runtime_sec();
2674 di->chrg_save_sec = 0;
2676 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2677 __func__, soc_time, chrg_term_sec, plus_soc);
2680 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2683 int now_current = di->current_avg;
2684 unsigned long dischrg_normal_sec;
2686 if (!di->dischrg_normal_base)
2687 di->dischrg_normal_base = get_runtime_sec();
2689 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2690 di->dischrg_save_sec;
2692 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2693 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2694 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2696 if (di->rsoc == di->dsoc) {
2697 DBG("<%s>. rsoc == dsoc\n", __func__);
2698 di->dsoc = di->rsoc;
2699 di->dischrg_normal_base = get_runtime_sec();
2700 di->dischrg_save_sec = 0;
2701 /*di->discharge_smooth_status = false;*/
2702 } else if (di->rsoc > di->dsoc - 1) {
2703 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2704 if (dischrg_normal_sec > soc_time * 3 / 2) {
2706 di->dischrg_normal_base = get_runtime_sec();
2707 di->dischrg_save_sec = 0;
2709 di->discharge_smooth_status = true;
2711 } else if (di->rsoc < di->dsoc - 1) {
2712 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2713 if (dischrg_normal_sec > soc_time * 3 / 4) {
2715 di->dischrg_normal_base = get_runtime_sec();
2716 di->dischrg_save_sec = 0;
2718 di->discharge_smooth_status = true;
2720 } else if (di->rsoc == di->dsoc - 1) {
2721 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2722 if (di->discharge_smooth_status) {
2723 if (dischrg_normal_sec > soc_time * 3 / 4) {
2725 di->dischrg_normal_base = get_runtime_sec();
2726 di->dischrg_save_sec = 0;
2727 di->discharge_smooth_status = false;
2731 di->dischrg_normal_base = get_runtime_sec();
2732 di->dischrg_save_sec = 0;
2733 di->discharge_smooth_status = false;
2737 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2738 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2739 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2740 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2743 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2747 /* first resume from suspend: we don't run this,
2748 * the sleep_dischrg will handle dsoc, and what
2749 * ever this is fake wakeup or not, we should clean
2750 * zero algorithm mode, or it will handle the dsoc.
2753 rk81x_bat_reset_zero_var(di);
2757 di->rsoc = rk81x_bat_get_rsoc(di);
2759 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2760 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2762 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2763 delta_soc = di->dsoc - di->rsoc;
2765 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2766 di->dischrg_normal_base = 0;
2767 rk81x_bat_emulator_dischrg(di);
2769 di->chrg_emu_base = 0;
2770 rk81x_bat_normal_dischrg(di);
2773 if (di->voltage < ZERO_ALGOR_THRESD) {
2774 di->dischrg_normal_base = 0;
2775 di->chrg_emu_base = 0;
2776 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2777 di->zero_timeout_cnt = 0;
2779 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2780 __func__, di->dsoc, di->last_zero_mode_dsoc);
2781 if (di->dsoc != di->last_zero_mode_dsoc) {
2782 di->display_soc = di->dsoc * 1000;
2783 di->last_zero_mode_dsoc = di->dsoc;
2784 rk81x_bat_zero_calc_linek(di);
2785 DBG("<%s>. first calc, init linek\n", __func__);
2789 rk81x_bat_zero_algorithm(di);
2791 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2792 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2793 di->zero_timeout_cnt = 0;
2794 DBG("<%s>. exit zero_algorithm\n", __func__);
2799 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2802 static int old_index;
2805 int mod = di->dsoc % 10;
2806 int index = di->dsoc / 10;
2808 if (rk81x_chrg_online(di))
2809 time = di->plug_in_min;
2811 time = di->plug_out_min;
2813 if ((mod == 0) && (index > 0) && (old_index != index)) {
2814 di->chrg_min[index-1] = time - old_min;
2819 for (i = 1; i < 11; i++)
2820 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2824 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2826 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2827 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2828 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2830 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2831 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2832 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2833 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2834 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2835 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2836 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2837 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2838 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2839 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2840 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2842 DBG("\n------------- dump_debug_regs -----------------\n"
2843 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2844 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2845 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2846 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2847 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2848 ggcon_reg, ggsts_reg, rtc_val,
2849 sup_tst_reg, vb_mod_reg,
2850 usb_ctrl_reg, chrg_ctrl_reg1,
2851 thremal_reg, misc_reg,
2852 chrg_ctrl_reg2, chrg_ctrl_reg3
2855 DBG("#######################################################\n"
2856 "voltage = %d, current-avg = %d\n"
2857 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2858 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2859 "display_soc = %d, cpapacity_soc = %d\n"
2860 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2861 "finish_real_soc = %d, finish_temp_soc = %d\n"
2862 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2863 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2864 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2865 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2866 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2867 "#########################################################\n",
2868 di->voltage, di->current_avg,
2869 di->fcc, di->remain_capacity, di->voltage_ocv,
2870 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2872 di->ac_online, di->usb_online, di->psy_status,
2873 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2874 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2875 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2876 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2877 BASE_TO_SEC(di->chrg_normal_base),
2878 BASE_TO_SEC(di->dischrg_normal_base),
2879 BASE_TO_SEC(di->chrg_term_base),
2880 BASE_TO_SEC(di->chrg_emu_base),
2881 BASE_TO_SEC(di->dischrg_emu_base),
2882 BASE_TO_SEC(di->power_on_base), g_base_sec,
2883 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2888 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2892 int dod0_to_soc100_min;
2894 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2895 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2897 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2898 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2899 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2900 di->dod0_capacity, dod0_to_soc100_min);
2902 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2903 (dod0_to_soc100_min < 1200)) {
2904 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2905 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2907 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2909 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2910 __func__, fcc0, di->fcc);
2912 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2913 di->dod0_status = 0;
2915 rk81x_bat_capacity_init(di, di->fcc);
2916 rk81x_bat_capacity_init_post(di);
2917 rk81x_bat_save_fcc(di, di->fcc);
2918 rk81x_bat_save_level(di, di->dod0_level);
2919 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2922 di->dod0_status = 0;
2926 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2928 if (di->chrg_status == CHARGE_FINISH) {
2929 di->debug_finish_real_soc = di->dsoc;
2930 di->debug_finish_temp_soc = di->rsoc;
2934 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2936 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2939 if ((di->chrg_status == CHARGE_FINISH) &&
2940 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2941 rk81x_bat_update_fcc(di);/* save new fcc*/
2942 ret = rk81x_bat_zero_current_calib(di);
2944 di->enter_finish = false;
2946 rk81x_bat_dbg_get_finish_soc(di);
2950 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2952 unsigned long sec_finish;
2953 int soc_time = 0, plus_soc;
2956 if (di->dsoc < 100) {
2957 if (!di->chrg_finish_base)
2958 di->chrg_finish_base = get_runtime_sec();
2960 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2962 temp = di->fcc * 3600 / 100;
2964 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2965 else if (di->usb_online)
2966 soc_time = temp / 450;
2968 plus_soc = sec_finish / soc_time;
2969 if (sec_finish > soc_time) {
2970 di->dsoc += plus_soc;
2971 di->chrg_finish_base = get_runtime_sec();
2972 di->chrg_save_sec = 0;
2974 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2975 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2976 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2980 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2983 u32 soc_time, unit_sec;
2985 unsigned long chrg_normal_sec;
2987 now_current = rk81x_bat_get_avg_current(di);
2988 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2990 if (!di->chrg_normal_base)
2991 di->chrg_normal_base = get_runtime_sec();
2993 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2994 di->rsoc = rk81x_bat_get_rsoc(di);
2996 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2997 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2999 if (di->dsoc == di->rsoc) {
3000 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3001 di->rsoc = rk81x_bat_get_rsoc(di);
3002 di->chrg_normal_base = get_runtime_sec();
3003 di->chrg_save_sec = 0;
3004 /*di->charge_smooth_status = false;*/
3005 } else if (di->rsoc < di->dsoc + 1) {
3006 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
3007 unit_sec = soc_time * 3 / 2;
3008 plus_soc = chrg_normal_sec / unit_sec;
3009 if (chrg_normal_sec > unit_sec) {
3010 di->dsoc += plus_soc;
3011 di->chrg_normal_base = get_runtime_sec();
3012 di->chrg_save_sec = 0;
3014 di->charge_smooth_status = true;
3015 } else if (di->rsoc > di->dsoc + 1) {
3016 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
3017 unit_sec = soc_time * 3 / 4;
3018 plus_soc = chrg_normal_sec / unit_sec;
3019 if (chrg_normal_sec > unit_sec) {
3020 di->dsoc += plus_soc;
3021 di->chrg_normal_base = get_runtime_sec();
3022 di->chrg_save_sec = 0;
3024 di->charge_smooth_status = true;
3025 } else if (di->rsoc == di->dsoc + 1) {
3026 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3027 if (di->charge_smooth_status) {
3028 unit_sec = soc_time * 3 / 4;
3029 if (chrg_normal_sec > unit_sec) {
3030 di->dsoc = di->rsoc;
3031 di->chrg_normal_base = get_runtime_sec();
3032 di->charge_smooth_status = false;
3033 di->chrg_save_sec = 0;
3036 di->dsoc = di->rsoc;
3037 di->chrg_normal_base = get_runtime_sec();
3038 di->charge_smooth_status = false;
3039 di->chrg_save_sec = 0;
3043 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3044 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3045 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3046 chrg_normal_sec, soc_time, plus_soc);
3049 static void rk81x_bat_update_time(struct rk81x_battery *di)
3053 runtime_sec = get_runtime_sec();
3055 /*update by charger type*/
3056 if (rk81x_chrg_online(di))
3057 di->plug_out_base = runtime_sec;
3059 di->plug_in_base = runtime_sec;
3061 /*update by current*/
3062 if (di->chrg_status != CHARGE_FINISH) {
3063 di->finish_sig_base = runtime_sec;
3064 di->chrg_finish_base = runtime_sec;
3067 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3068 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3069 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3071 rk81x_bat_dbg_time_table(di);
3074 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3076 int trend_start_cap = di->trend_start_cap;
3077 int remain_cap = di->remain_capacity;
3081 if (di->s2r && !di->slp_psy_status)
3082 di->trend_start_cap = di->remain_capacity;
3084 diff_cap = remain_cap - trend_start_cap;
3085 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3086 __func__, trend_start_cap, diff_cap);
3087 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3089 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3090 di->trend_start_cap = di->remain_capacity;
3091 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3092 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3094 state = TREND_STAT_FLAT;
3100 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3102 int state, soc_time;
3103 static int trend_down_cnt, trend_up_cnt;
3104 int trend_cnt_thresd;
3105 int now_current = di->current_avg;
3108 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3109 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3110 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3111 __func__, state, trend_mult);
3112 if (di->chrg_status == CHARGE_FINISH)
3115 if (state == TREND_STAT_UP) {
3116 rk81x_bat_reset_zero_var(di);
3118 trend_up_cnt += trend_mult;
3119 if (trend_up_cnt >= trend_cnt_thresd) {
3121 di->dischrg_save_sec = 0;
3123 } else if (state == TREND_STAT_DOWN) {
3125 trend_down_cnt += trend_mult;
3126 if (trend_down_cnt >= trend_cnt_thresd) {
3128 di->chrg_save_sec = 0;
3132 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3133 if ((di->chrg_save_sec + 20 > soc_time) &&
3134 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3136 di->chrg_save_sec = 0;
3138 else if ((di->dischrg_save_sec + 20 > soc_time) &&
3139 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3141 di->dischrg_save_sec = 0;
3143 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3144 "up_cnt=%d, down_cnt=%d\n",
3145 __func__, state, trend_cnt_thresd, soc_time,
3146 trend_up_cnt, trend_down_cnt);
3149 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3151 u32 *ocv_table = di->pdata->battery_ocv;
3152 int delta_soc = di->rsoc - di->dsoc;
3154 if (di->chrg_status == CHARGE_FINISH ||
3155 di->slp_chrg_status == CHARGE_FINISH) {
3156 /*clear sleep charge status*/
3157 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3158 di->chrg_emu_base = 0;
3159 di->chrg_normal_base = 0;
3160 di->chrg_term_base = 0;
3161 rk81x_bat_finish_chrg(di);
3162 rk81x_bat_capacity_init(di, di->fcc);
3163 rk81x_bat_capacity_init_post(di);
3164 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3165 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3166 (di->voltage < ocv_table[18] + 20))) {
3167 di->chrg_emu_base = 0;
3168 di->chrg_normal_base = 0;
3169 di->chrg_finish_base = 0;
3170 rk81x_bat_terminal_chrg(di);
3171 } else if (di->chrg_status != CHARGE_FINISH &&
3172 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3173 di->chrg_term_base = 0;
3174 di->chrg_normal_base = 0;
3175 di->chrg_finish_base = 0;
3176 rk81x_bat_emulator_chrg(di);
3178 di->chrg_emu_base = 0;
3179 di->chrg_term_base = 0;
3180 di->chrg_finish_base = 0;
3181 rk81x_bat_normal_chrg(di);
3185 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3187 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3188 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3190 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3191 dischrg_normal_sec, dischrg_emu_sec);
3193 return (dischrg_normal_sec > dischrg_emu_sec) ?
3194 dischrg_normal_sec : dischrg_emu_sec;
3197 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3199 unsigned long sec1, sec2;
3200 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3201 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3202 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3203 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3205 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3206 chrg_normal_sec : chrg_term_sec;
3208 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3209 chrg_emu_sec : chrg_finish_sec;
3210 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3211 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3212 chrg_normal_sec, chrg_term_sec,
3213 chrg_emu_sec, chrg_finish_sec);
3215 return (sec1 > sec2) ? sec1 : sec2;
3218 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3220 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3221 if (di->current_mode == DISCHRG_MODE) {
3222 di->current_mode = CHRG_MODE;
3223 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3224 di->dischrg_normal_base = 0;
3225 di->dischrg_emu_base = 0;
3226 if (di->chrg_status == CHARGE_FINISH)
3227 di->dischrg_save_sec = 0;
3228 if ((di->chrg_status == CHARGE_FINISH) &&
3230 di->chrg_save_sec = 0;
3232 DBG("<%s>---dischrg_save_sec = %lu\n",
3233 __func__, di->dischrg_save_sec);
3236 if (!rk81x_chrg_online(di)) {
3237 dev_err(di->dev, "discharge, current error:%d\n",
3240 rk81x_bat_chrg_smooth(di);
3241 di->discharge_smooth_status = true;
3244 if (di->current_mode == CHRG_MODE) {
3245 di->current_mode = DISCHRG_MODE;
3246 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3247 di->chrg_normal_base = 0;
3248 di->chrg_emu_base = 0;
3249 di->chrg_term_base = 0;
3250 di->chrg_finish_base = 0;
3251 DBG("<%s>---chrg_save_sec = %lu\n",
3252 __func__, di->chrg_save_sec);
3254 rk81x_bat_dischrg_smooth(di);
3255 di->charge_smooth_status = true;
3260 * update rsoc by relax voltage
3262 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3264 int relax_vol = di->relax_voltage;
3265 int ocv_soc, capacity;
3267 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3268 capacity = (ocv_soc * di->fcc / 100);
3269 rk81x_bat_capacity_init(di, capacity);
3270 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3271 di->rsoc = rk81x_bat_get_rsoc(di);
3272 rk81x_bat_capacity_init_post(di);
3273 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3277 * 1: must do it, 0: when necessary
3279 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3281 int ocv_vol = di->est_ocv_vol;
3282 int ocv_soc = 0, capacity = 0;
3284 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3285 capacity = (ocv_soc * di->fcc / 100);
3286 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3287 rk81x_bat_capacity_init(di, capacity);
3288 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3289 di->rsoc = rk81x_bat_get_rsoc(di);
3290 rk81x_bat_capacity_init_post(di);
3291 DBG("<%s>, rsoc updated!\n", __func__);
3293 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3294 __func__, ocv_vol, ocv_soc, capacity);
3297 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3301 unsigned long sleep_sec = di->suspend_time_sum;
3302 int power_off_thresd = di->pdata->power_off_thresd;
3304 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3305 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3306 __func__, di->dsoc, di->rsoc,
3307 di->relax_voltage, di->voltage, sleep_sec / 60);
3309 if (di->relax_voltage >= di->voltage) {
3310 rk81x_bat_relax_vol_calib(di);
3311 rk81x_bat_restart_relax(di);
3313 /* current_avg < 0: make sure the system is not
3314 * wakeup by charger plugin.
3316 /* even if relax voltage is not caught rightly, realtime voltage
3317 * is quite close to relax voltage, we should not do nothing after
3321 rk81x_bat_vol_calib(di, 1);
3325 if (di->dsoc <= di->rsoc) {
3326 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3327 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3328 temp_dsoc = di->dsoc - delta_soc;
3330 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3331 di->rsoc, di->dsoc, delta_soc);
3333 if (delta_soc > 0) {
3334 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3336 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3338 else if (temp_dsoc > 5)
3339 di->dsoc = temp_dsoc;
3342 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3343 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3345 /*di->dsoc > di->rsoc*/
3346 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3347 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3348 temp_dsoc = di->dsoc - di->rsoc;
3350 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3351 di->rsoc, di->dsoc, delta_soc);
3353 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3354 (temp_dsoc > delta_soc))
3355 di->dsoc -= delta_soc;
3357 di->dsoc = di->rsoc;
3359 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3360 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3363 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3369 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3370 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3375 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3378 unsigned long sleep_sec;
3380 sleep_sec = di->suspend_time_sum;
3381 if (((di->suspend_charge_current < 800) &&
3382 (di->ac_online == ONLINE)) ||
3383 (di->chrg_status == CHARGE_FINISH)) {
3384 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3385 if (sleep_sec > 0) {
3386 /*default charge current: 1000mA*/
3387 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3388 / 3600 / div(di->fcc);
3391 DBG("<%s>, usb charge\n", __func__);
3398 * only do report when there is a change.
3400 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3401 * when dsoc == 0, we must do report. But it will generate too much android
3402 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3405 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3408 static u32 old_ac_status;
3409 static u32 old_usb_status;
3410 static u32 old_charge_status;
3413 state_changed = false;
3414 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3415 state_changed = true;
3416 else if (di->dsoc != old_soc)
3417 state_changed = true;
3418 else if (di->ac_online != old_ac_status)
3419 state_changed = true;
3420 else if (di->usb_online != old_usb_status)
3421 state_changed = true;
3422 else if (old_charge_status != di->psy_status)
3423 state_changed = true;
3425 if (rk81x_chrg_online(di)) {
3426 if (di->dsoc == 100)
3427 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3429 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3432 if (state_changed) {
3433 power_supply_changed(&di->bat);
3434 power_supply_changed(&di->usb);
3435 power_supply_changed(&di->ac);
3437 old_ac_status = di->ac_online;
3438 old_usb_status = di->usb_online;
3439 old_charge_status = di->psy_status;
3440 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3441 di->dsoc, di->rsoc);
3446 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3450 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3453 return CHRG_CVCC_HOUR[hour];
3456 /* we have to estimate the charging finish time from now, to decide
3457 * whether we should reset the timer or not.
3459 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3462 int remain_capacity;
3464 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3465 if (di->dsoc < di->rsoc)
3466 remain_capacity = di->dsoc * di->fcc / 100;
3468 remain_capacity = di->remain_capacity;
3470 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3471 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3473 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3474 di->chrg_cap2full = di->fcc - remain_capacity;
3475 if (di->current_avg <= 0)
3476 di->current_avg = 1;
3478 di->chrg_time2full = di->chrg_cap2full * 3600 /
3479 div(abs(di->current_avg));
3481 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3482 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3485 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3486 /*rk81x_bat_init_chrg_timer(di);*/
3487 di->plug_in_base = get_runtime_sec();
3488 DBG("%s: reset charge timer\n", __func__);
3495 * in case that we will do reboot stress test, we need a special way
3496 * to ajust the dsoc.
3498 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3506 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3509 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3510 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3512 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3513 __func__, cnt, unit_time, smooth_time,
3514 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3516 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3517 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3518 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3523 rk81x_bat_save_dsoc(di, dsoc);
3526 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3527 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3532 rk81x_bat_save_dsoc(di, dsoc);
3536 rk81x_bat_save_reboot_cnt(di, cnt);
3539 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3544 uint16_t cal_offset;
3545 u8 pcb_offset = DEF_PCB_OFFSET;
3547 min = BASE_TO_MIN(di->power_on_base);
3548 if ((min % 8) && (old_min != min)) {
3550 rk81x_bat_get_vol_offset(di);
3551 if (di->pcb_ioffset_updated)
3552 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3554 current_offset = rk81x_bat_get_ioffset(di);
3555 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3556 cal_offset = rk81x_bat_get_cal_offset(di);
3557 if (cal_offset < 0x7ff)
3558 rk81x_bat_set_cal_offset(di, di->current_offset +
3560 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3561 __func__, di->voltage_k, di->voltage_b, cal_offset,
3562 rk81x_bat_get_ioffset(di));
3566 static void rk81x_bat_update_info(struct rk81x_battery *di)
3570 else if (di->dsoc < 0)
3574 * we need update fcc in continuous charging state, if discharge state
3575 * keep at least 2 hour, we decide not to update fcc, so clear the
3576 * fcc update flag: dod0_status.
3578 if (BASE_TO_MIN(di->plug_out_base) > 120)
3579 di->dod0_status = 0;
3581 di->voltage = rk81x_bat_get_vol(di);
3582 di->current_avg = rk81x_bat_get_avg_current(di);
3583 di->chrg_status = rk81x_bat_get_chrg_status(di);
3584 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3585 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3586 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3587 /*rk81x_bat_chrg_over_time_check(di);*/
3588 rk81x_bat_update_calib_param(di);
3589 if (di->chrg_status == CC_OR_CV)
3590 di->enter_finish = true;
3591 #if defined(CONFIG_ARCH_ROCKCHIP)
3592 rk81x_bat_status_check(di);/* ac_online, usb_online, status*/
3595 if (!rk81x_chrg_online(di) && di->s2r)
3598 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3599 if (di->remain_capacity > di->fcc) {
3600 rk81x_bat_capacity_init(di, di->fcc);
3601 rk81x_bat_capacity_init_post(di);
3602 di->remain_capacity = di->fcc;
3605 di->rsoc = rk81x_bat_get_rsoc(di);
3608 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3610 if (di->slp_psy_status)
3611 return rk81x_bat_sleep_chrg(di);
3613 return rk81x_bat_sleep_dischrg(di);
3616 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3618 u8 ocv_soc, soc_level;
3619 int relax_vol = di->relax_voltage;
3624 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3625 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3626 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3628 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3629 (di->dod0_status == 0) &&
3631 di->dod0_voltage = relax_vol;
3632 di->dod0_capacity = di->temp_nac;
3637 di->dod0_level = 100;
3638 else if (ocv_soc < 5)
3639 di->dod0_level = 90;
3641 di->dod0_level = 80;
3643 /* save_soc = di->dod0_level; */
3644 soc_level = rk81x_bat_get_level(di);
3645 if (soc_level > di->dod0_level) {
3646 di->dod0_status = 0;
3648 di->dod0_status = 1;
3650 di->fcc_update_sec = get_runtime_sec();
3653 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3654 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3656 di->dod0_voltage, di->dod0_capacity,
3657 ocv_soc, soc_level, di->dod0_status,
3662 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3667 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3670 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3672 dev_info(di->dev, "set charge to %s termination mode\n",
3673 mode ? "digital" : "analog");
3676 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3678 struct rk81x_battery *di;
3680 di = container_of(work, struct rk81x_battery,
3681 chrg_term_mode_switch_work.work);
3683 if (rk81x_chrg_online(di))
3684 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3686 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3689 static void rk81x_battery_work(struct work_struct *work)
3691 struct rk81x_battery *di;
3692 int ms = TIMER_MS_COUNTS;
3694 di = container_of(work, struct rk81x_battery,
3695 battery_monitor_work.work);
3696 if (rk81x_chrg_online(di)) {
3697 rk81x_bat_wait_finish_sig(di);
3698 /*rk81x_bat_chrg_finish_routine(di);*/
3700 rk81x_bat_fcc_flag_check(di);
3701 rk81x_bat_arbitrate_rsoc_trend(di);
3702 rk81x_bat_display_smooth(di);
3703 rk81x_bat_update_time(di);
3704 rk81x_bat_update_info(di);
3705 rk81x_bat_rsoc_check(di);
3706 rk81x_bat_power_supply_changed(di);
3707 rk81x_bat_save_dsoc(di, di->dsoc);
3708 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3710 rk81x_bat_dbg_dmp_info(di);
3712 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3713 ms = 30 * TIMER_MS_COUNTS;
3715 di->early_resume = 0;
3719 queue_delayed_work(di->wq, &di->battery_monitor_work,
3720 msecs_to_jiffies(ms));
3723 static void rk81x_battery_charge_check_work(struct work_struct *work)
3725 struct rk81x_battery *di = container_of(work,
3726 struct rk81x_battery, charge_check_work.work);
3728 DBG("rk81x_battery_charge_check_work\n");
3729 rk81x_charge_disable_open_otg(di);
3732 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3734 int register_battery_notifier(struct notifier_block *nb)
3736 return blocking_notifier_chain_register(&battery_chain_head, nb);
3738 EXPORT_SYMBOL_GPL(register_battery_notifier);
3740 int unregister_battery_notifier(struct notifier_block *nb)
3742 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3744 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3746 int battery_notifier_call_chain(unsigned long val)
3748 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3749 == NOTIFY_BAD) ? -EINVAL : 0;
3751 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3753 static void poweron_lowerpoer_handle(struct rk81x_battery *di)
3755 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
3756 if ((di->dsoc <= 2) &&
3757 (di->psy_status == POWER_SUPPLY_STATUS_DISCHARGING)) {
3759 /* kernel_power_off(); */
3764 static int rk81x_bat_notifier_call(struct notifier_block *nb,
3765 unsigned long event, void *data)
3767 struct rk81x_battery *di =
3768 container_of(nb, struct rk81x_battery, battery_nb);
3772 DBG(" CHARGE enable\n");
3774 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3775 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3776 queue_delayed_work(di->wq, &di->charge_check_work,
3777 msecs_to_jiffies(50));
3781 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3782 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3783 queue_delayed_work(di->wq, &di->charge_check_work,
3784 msecs_to_jiffies(50));
3785 DBG("charge disable OTG enable\n");
3788 poweron_lowerpoer_handle(di);
3796 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3798 pr_info("\n------- %s:lower power warning!\n", __func__);
3800 rk_send_wakeup_key();
3805 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3807 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3808 rk_send_wakeup_key();
3812 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3814 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3815 rk_send_wakeup_key();
3819 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3821 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3823 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3824 di->finish_sig_base = get_runtime_sec();
3825 rk_send_wakeup_key();
3829 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3831 rk_send_wakeup_key();
3836 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3841 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3842 ret = sysfs_create_file(&di->bat.dev->kobj,
3843 &rk818_bat_attr[i].attr);
3845 dev_err(di->dev, "create battery node(%s) error\n",
3846 rk818_bat_attr[i].attr.name);
3852 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3854 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3856 struct rk818 *chip = di->rk818;
3858 #if defined(CONFIG_X86_INTEL_SOFIA)
3859 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3860 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3861 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3862 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3864 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3865 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3866 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3867 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3870 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3871 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3873 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3875 di->irq = vb_lo_irq;
3876 enable_irq_wake(di->irq);
3878 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3879 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3882 dev_err(chip->dev, "plug_in_irq request failed!\n");
3884 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3885 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3888 dev_err(chip->dev, "plug_out_irq request failed!\n");
3890 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3891 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3894 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3897 static void rk81x_bat_info_init(struct rk81x_battery *di,
3901 unsigned long time_base = get_runtime_sec();
3903 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3904 if (val & PLUG_IN_STS)
3905 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3907 di->cell.config = di->pdata->cell_cfg;
3908 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3909 di->qmax = di->pdata->cell_cfg->design_qmax;
3910 di->early_resume = 1;
3911 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3912 di->bat_res = di->pdata->sense_resistor_mohm;
3913 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3914 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3915 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3916 di->loader_charged = loader_charged;
3917 di->chrg_finish_base = time_base;
3918 di->power_on_base = time_base;
3919 di->plug_in_base = time_base;
3920 di->plug_out_base = time_base;
3921 di->finish_sig_base = time_base;
3922 di->fcc = rk81x_bat_get_fcc(di);
3925 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3926 struct device_node *np)
3928 struct device *dev = di->dev;
3929 enum of_gpio_flags flags;
3932 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3933 if (di->dc_det_pin == -EPROBE_DEFER)
3934 dev_err(dev, "dc_det_gpio error\n");
3935 if (gpio_is_valid(di->dc_det_pin))
3936 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3937 RK818_DC_IN : RK818_DC_OUT;
3938 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3939 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3940 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3941 "rk81x_dc_det", NULL);
3944 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3945 enable_irq_wake(di->dc_det_irq);
3948 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3953 struct timespec tv = {
3954 .tv_nsec = NSEC_PER_SEC >> 1,
3956 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3958 err = rtc_read_time(rtc, &tm);
3960 dev_err(rtc->dev.parent,
3961 "hctosys: unable to read the hardware clock\n");
3963 err = rtc_valid_tm(&tm);
3965 dev_err(rtc->dev.parent,
3966 "hctosys: invalid date/time\n");
3969 rtc_tm_to_time(&tm, &tv.tv_sec);
3970 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3976 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
3978 struct device_node *np;
3979 struct battery_platform_data *pdata;
3980 struct cell_config *cell_cfg;
3981 struct ocv_config *ocv_cfg;
3982 struct property *prop;
3983 struct rk818 *rk818 = di->rk818;
3984 struct device *dev = di->dev;
3989 np = of_find_node_by_name(rk818->dev->of_node, "battery");
3991 dev_err(dev, "battery node not found!\n");
3995 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
3999 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
4003 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4007 prop = of_find_property(np, "ocv_table", &length);
4009 dev_err(dev, "ocv_table not found!\n");
4012 pdata->ocv_size = length / sizeof(u32);
4013 if (pdata->ocv_size <= 0) {
4014 dev_err(dev, "invalid ocv table\n");
4018 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4020 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4021 if (!pdata->battery_ocv)
4024 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4029 /******************** charger param ****************************/
4030 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4032 dev_err(dev, "max_chrg_currentmA not found!\n");
4033 out_value = DEFAULT_CHRG_CUR;
4035 pdata->max_charger_currentmA = out_value;
4037 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4039 dev_err(dev, "max_charger_ilimitmA not found!\n");
4040 out_value = DEFAULT_INPUT_CUR;
4042 pdata->max_charger_ilimitmA = out_value;
4044 ret = of_property_read_u32(np, "bat_res", &out_value);
4046 dev_err(dev, "bat_res not found!\n");
4047 out_value = DEFAULT_BAT_RES;
4049 pdata->sense_resistor_mohm = out_value;
4051 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4053 dev_err(dev, "max_charge_voltagemV not found!\n");
4054 out_value = DEFAULT_CHRG_VOL;
4056 pdata->max_charger_voltagemV = out_value;
4058 ret = of_property_read_u32(np, "design_capacity", &out_value);
4060 dev_err(dev, "design_capacity not found!\n");
4063 cell_cfg->design_capacity = out_value;
4065 ret = of_property_read_u32(np, "design_qmax", &out_value);
4067 dev_err(dev, "design_qmax not found!\n");
4070 cell_cfg->design_qmax = out_value;
4072 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4074 dev_err(dev, "sleep_enter_current not found!\n");
4075 out_value = DEFAULT_SLP_ENTER_CUR;
4077 ocv_cfg->sleep_enter_current = out_value;
4079 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4081 dev_err(dev, "sleep_exit_current not found!\n");
4082 out_value = DEFAULT_SLP_EXIT_CUR;
4084 ocv_cfg->sleep_exit_current = out_value;
4086 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4088 dev_warn(dev, "power_off_thresd not found!\n");
4089 out_value = PWR_OFF_THRESD;
4091 pdata->power_off_thresd = out_value;
4093 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4094 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4095 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4097 /************* charger support adp types **********************/
4098 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4099 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4101 if (!support_usb_adp && !support_dc_adp) {
4102 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4103 support_usb_adp = 1;
4107 rk81x_bat_dc_det_init(di, np);
4109 cell_cfg->ocv = ocv_cfg;
4110 pdata->cell_cfg = cell_cfg;
4113 DBG("\nthe battery dts info dump:\n"
4115 "max_input_currentmA:%d\n"
4116 "max_chrg_currentmA:%d\n"
4117 "max_charge_voltagemV:%d\n"
4118 "design_capacity:%d\n"
4120 "sleep_enter_current:%d\n"
4121 "sleep_exit_current:%d\n"
4122 "support_usb_adp:%d\n"
4123 "support_dc_adp:%d\n"
4124 "power_off_thresd:%d\n",
4125 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4126 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4127 cell_cfg->design_capacity, cell_cfg->design_qmax,
4128 cell_cfg->ocv->sleep_enter_current,
4129 cell_cfg->ocv->sleep_exit_current,
4130 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4136 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4142 static int rk81x_battery_probe(struct platform_device *pdev)
4144 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4145 struct rk81x_battery *di;
4148 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4152 di->dev = &pdev->dev;
4153 platform_set_drvdata(pdev, di);
4155 ret = rk81x_bat_parse_dt(di);
4157 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4161 rk81x_bat_info_init(di, chip);
4162 if (!is_rk81x_bat_exist(di)) {
4163 dev_info(di->dev, "not battery, enter test power mode\n");
4164 di->fg_drv_mode = TEST_POWER_MODE;
4167 ret = rk81x_bat_power_supply_init(di);
4169 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4173 rk81x_bat_irq_init(di);
4174 rk81x_bat_sysfs_init(di);
4176 rk81x_bat_fg_init(di);
4177 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4179 rk81x_bat_flatzone_vol_init(di);
4181 #if defined(CONFIG_X86_INTEL_SOFIA)
4182 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4183 if (IS_ERR_OR_NULL(di->usb_phy)) {
4184 dev_err(di->dev, "get usb phy failed\n");
4185 return PTR_ERR(di->usb_phy);
4187 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4188 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4190 dev_err(di->dev, "registr usb phy notification failed\n");
4191 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4192 rk81x_battery_usb_notifier_delayed_work);
4195 rk81x_battery_register_fb_notify(di);
4196 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4197 "rk81x-battery-work");
4198 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4199 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4200 rk81x_chrg_term_mode_switch_work);
4202 queue_delayed_work(di->wq, &di->battery_monitor_work,
4203 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4205 INIT_DELAYED_WORK(&di->charge_check_work,
4206 rk81x_battery_charge_check_work);
4207 di->battery_nb.notifier_call = rk81x_bat_notifier_call;
4208 register_battery_notifier(&di->battery_nb);
4210 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4215 static int rk81x_battery_suspend(struct platform_device *dev,
4218 struct rk81x_battery *di = platform_get_drvdata(dev);
4220 di->slp_psy_status = rk81x_chrg_online(di);
4222 di->chrg_status = rk81x_bat_get_chrg_status(di);
4223 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4224 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4225 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4226 di->dischrg_normal_base = 0;
4227 di->dischrg_emu_base = 0;
4228 do_gettimeofday(&di->suspend_rtc_base);
4230 if (!rk81x_chrg_online(di)) {
4231 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4232 di->chrg_normal_base = 0;
4233 di->chrg_emu_base = 0;
4234 di->chrg_term_base = 0;
4235 di->chrg_finish_base = 0;
4240 * do not modify the g_base_sec
4242 g_base_sec = get_runtime_sec();
4244 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4245 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4246 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4251 static int rk81x_battery_resume(struct platform_device *dev)
4253 struct rk81x_battery *di = platform_get_drvdata(dev);
4254 int pwroff_thresd = di->pdata->power_off_thresd;
4260 di->discharge_smooth_status = true;
4261 di->charge_smooth_status = true;
4263 vol = rk81x_bat_get_vol(di);
4264 if (vol < INVALID_VOL_THRESD) {
4265 dev_err(di->dev, "invalid voltage :%d", vol);
4270 di->current_avg = rk81x_bat_get_avg_current(di);
4271 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4272 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4273 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4274 delta_time = rk81x_bat_get_suspend_sec(di);
4275 di->suspend_time_sum += delta_time;
4276 #if defined(CONFIG_ARCH_ROCKCHIP)
4277 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4280 if (di->slp_psy_status) {
4281 time_step = CHRG_TIME_STEP;
4283 if (di->voltage <= pwroff_thresd + 50)
4284 time_step = DISCHRG_TIME_STEP_0;
4286 time_step = DISCHRG_TIME_STEP_1;
4289 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4290 di->current_avg, di->voltage, di->est_ocv_vol,
4291 di->relax_voltage, delta_time, di->suspend_time_sum,
4292 di->slp_psy_status);
4294 if (di->suspend_time_sum > time_step) {
4295 delta_soc = rk81x_bat_update_resume_state(di);
4297 di->suspend_time_sum = 0;
4300 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4301 rk81x_chrg_online(di))
4302 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4305 * do not modify the g_base_sec
4307 if (is_local_clock_reset())
4308 g_base_sec += delta_time;
4315 static int rk81x_battery_remove(struct platform_device *dev)
4317 struct rk81x_battery *di = platform_get_drvdata(dev);
4319 cancel_delayed_work_sync(&di->battery_monitor_work);
4323 static void rk81x_battery_shutdown(struct platform_device *dev)
4325 struct rk81x_battery *di = platform_get_drvdata(dev);
4327 cancel_delayed_work_sync(&di->battery_monitor_work);
4328 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4329 rk81x_bat_check_reboot(di);
4331 rk81x_bat_save_reboot_cnt(di, 0);
4332 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4335 static struct platform_driver rk81x_battery_driver = {
4337 .name = "rk818-battery",
4338 .owner = THIS_MODULE,
4341 .probe = rk81x_battery_probe,
4342 .remove = rk81x_battery_remove,
4343 .suspend = rk81x_battery_suspend,
4344 .resume = rk81x_battery_resume,
4345 .shutdown = rk81x_battery_shutdown,
4348 static int __init battery_init(void)
4350 return platform_driver_register(&rk81x_battery_driver);
4353 fs_initcall_sync(battery_init);
4354 static void __exit battery_exit(void)
4356 platform_driver_unregister(&rk81x_battery_driver);
4358 module_exit(battery_exit);
4360 MODULE_LICENSE("GPL");
4361 MODULE_ALIAS("platform:rk818-battery");
4362 MODULE_AUTHOR("ROCKCHIP");