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