2 * rk818/rk819 battery driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd
5 * Author: zhangqing <zhangqing@rock-chips.com>
6 * chenjh <chenjh@rock-chips.com>
7 * Andy Yan <andy.yan@rock-chips.com>
9 * Copyright (C) 2008-2009 Texas Instruments, Inc.
10 * Author: Texas Instruments, Inc.
12 * Copyright (C) 2008-2009 Texas Instruments, Inc.
13 * Author: Texas Instruments, Inc.
14 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
15 * Author: zhangqing <zhangqing@rock-chips.com>
16 * Copyright (C) 2014-2015 Intel Mobile Communications GmbH
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
40 #include <linux/gpio.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
43 #include <linux/mfd/rk818.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/rtc.h>
47 #include <linux/wakelock.h>
48 #include <linux/of_gpio.h>
49 #include <linux/irq.h>
50 #include <linux/irqdomain.h>
51 #include <linux/usb/phy.h>
54 #if defined(CONFIG_X86_INTEL_SOFIA)
55 #include <linux/usb/phy-intel.h>
57 #include <linux/power/rk_usbbc.h>
59 #include "rk818_battery.h"
61 /* if you want to disable, don't set it as 0,
62 just be: "static int dbg_enable;" is ok*/
64 static int dbg_enable;
65 #define RK818_SYS_DBG 1
67 module_param_named(dbg_level, dbg_enable, int, 0644);
69 #define DBG(args...) \
76 #define DEFAULT_BAT_RES 135
77 #define DEFAULT_CHRG_VOL 4200
78 #define DEFAULT_CHRG_CUR 1000
79 #define DEFAULT_INPUT_CUR 1400
80 #define DEFAULT_SLP_ENTER_CUR 600
81 #define DEFAULT_SLP_EXIT_CUR 600
83 #define DSOC_DISCHRG_EMU_CURR 1200
84 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
85 #define DSOC_DISCHRG_FAST_EER_RANGE 10
86 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
87 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
88 #define DSOC_CHRG_FAST_EER_RANGE 10
89 #define DSOC_CHRG_EMU_CURR 1200
90 #define DSOC_CHRG_TERM_CURR 600
91 #define DSOC_CHRG_TERM_VOL 4100
92 #define CHRG_FINISH_VOL 4100
94 /*realtime RSOC calib param*/
95 #define RSOC_DISCHRG_ERR_LOWER 40
96 #define RSOC_DISCHRG_ERR_UPPER 50
97 #define RSOC_ERR_CHCK_CNT 15
98 #define RSOC_COMPS 20 /*compensation*/
99 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
100 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
102 #define RSOC_RESUME_ERR 10
103 #define REBOOT_INTER_MIN 1
105 #define INTERPOLATE_MAX 1000
106 #define MAX_INT 0x7FFF
107 #define TIME_10MIN_SEC 600
109 #define CHRG_VOL_SHIFT 4
110 #define CHRG_ILIM_SHIFT 0
111 #define CHRG_ICUR_SHIFT 0
112 #define DEF_CHRG_VOL CHRG_VOL4200
113 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
114 #define DEF_CHRG_CURR_LMT ILIM_2000MA
116 /*TEST_POWER_MODE params*/
117 #define TEST_CURRENT 1000
118 #define TEST_VOLTAGE 3800
120 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
121 #define TEST_PRESET 1
122 #define TEST_AC_ONLINE 1
123 #define TEST_USB_ONLINE 0
125 #define ZERO_ALGOR_THRESD 3800
126 #define DISCHRG_ZERO_MODE 1
127 #define DISCHRG_NORMAL_MODE 0
128 #define DEF_LAST_ZERO_MODE_SOC -1
130 #define DISCHRG_MODE 0
133 #define TREND_STAT_FLAT 0
134 #define TREND_STAT_DOWN -1
135 #define TREND_STAT_UP 1
136 #define TREND_CAP_DIFF 5
140 #define SLP_CURR_MAX 40
141 #define SLP_CURR_MIN 6
142 #define WAKEUP_SEC_THRESD 40
143 #define CHRG_TIME_STEP (60)
144 #define DISCHRG_TIME_STEP_0 (30 * 60)
145 #define DISCHRG_TIME_STEP_1 (60 * 60)
147 #define DEF_PCB_OFFSET 42
148 #define DEF_CAL_OFFSET 0x832
149 #define DEF_PWRPATH_RES 50
150 #define SEC_TO_EMPTY 300
151 #define DSOC_CHRG_FINISH_CURR 1100
152 #define SLP_CHRG_CURR 1000
153 #define SLP_DSOC_VOL_THRESD 3600
154 /*if voltage is lower than this thresd,
155 we consider it as invalid
157 #define INVALID_VOL_THRESD 2500
158 #define PWR_OFF_THRESD 3400
159 #define MIN_ZERO_ACCURACY 5 /*0.01%*/
160 #define MIN_ROUND_ACCURACY 1
162 #define MAX_FCC 10000
165 * the following table value depends on datasheet
167 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
169 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
170 2250, 2400, 2600, 2800, 3000};
172 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
173 2000, 2250, 2500, 2750, 3000};
175 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
177 #define RK818_DC_IN 0
178 #define RK818_DC_OUT 1
180 #define OCV_VALID_SHIFT (0)
181 #define OCV_CALIB_SHIFT (1)
182 #define FIRST_PWRON_SHIFT (2)
184 #define SEC_TO_MIN(x) ((x) / 60)
186 struct rk81x_battery {
188 struct cell_state cell;
189 struct power_supply bat;
190 struct power_supply ac;
191 struct power_supply usb;
192 struct delayed_work work;
194 struct pinctrl *pinctrl;
195 struct pinctrl_state *pins_default;
197 struct battery_platform_data *pdata;
211 uint16_t voltage_ocv;
212 uint16_t relax_voltage;
218 bool pcb_ioffset_updated;
235 int bat_res_update_cnt;
241 unsigned long dod0_time;
254 int current_k;/* (ICALIB0, ICALIB1) */
257 int voltage_k;/* VCALIB0 VCALIB1 */
260 int zero_timeout_cnt;
261 int zero_old_remain_cap;
266 int charge_smooth_time;
270 unsigned long suspend_time_sum;
274 int suspend_charge_current;
277 bool charge_smooth_status;
278 bool discharge_smooth_status;
284 struct notifier_block battery_nb;
285 struct usb_phy *usb_phy;
286 struct notifier_block usb_nb;
287 struct notifier_block fb_nb;
290 int s2r; /*suspend to resume*/
291 struct workqueue_struct *wq;
292 struct delayed_work battery_monitor_work;
293 struct delayed_work otg_check_work;
294 struct delayed_work usb_phy_delay_work;
295 struct delayed_work chrg_term_mode_switch_work;
296 struct delayed_work ac_usb_check_work;
297 struct delayed_work dc_det_check_work;
298 enum bc_port_type charge_otg;
301 struct wake_lock resume_wake_lock;
302 unsigned long plug_in_base;
303 unsigned long plug_out_base;
304 unsigned long finish_sig_base;
305 unsigned long power_on_base;
310 bool is_first_poweron;
313 int debug_finish_real_soc;
314 int debug_finish_temp_soc;
320 unsigned long wakeup_sec;
321 u32 delta_vol_smooth;
322 unsigned long dischrg_normal_base;
323 unsigned long dischrg_emu_base;
324 unsigned long chrg_normal_base;
325 unsigned long chrg_term_base;
326 unsigned long chrg_emu_base;
327 unsigned long chrg_finish_base;
328 unsigned long fcc_update_sec;
330 u8 dischrg_algorithm_mode;
331 int last_zero_mode_dsoc;
333 unsigned long dischrg_save_sec;
334 unsigned long chrg_save_sec;
335 struct timeval suspend_rtc_base;
338 u32 support_usb_adp, support_dc_adp;
340 #define to_device_info(x) container_of((x), \
341 struct rk81x_battery, bat)
343 #define to_ac_device_info(x) container_of((x), \
344 struct rk81x_battery, ac)
346 #define to_usb_device_info(x) container_of((x), \
347 struct rk81x_battery, usb)
349 static int loader_charged;
351 static int __init rk81x_bat_loader_charged(char *__unused)
355 pr_info("battery charged in loader\n");
359 __setup("loader_charged", rk81x_bat_loader_charged);
361 static u64 g_base_sec;
362 static u64 get_runtime_sec(void)
364 u64 ts_ns = local_clock();
366 do_div(ts_ns, 1000000000);
368 return ts_ns + g_base_sec;
371 static u64 is_local_clock_reset(void)
373 u64 ts_ns = local_clock();
375 do_div(ts_ns, 1000000000);
380 static inline unsigned long BASE_TO_SEC(unsigned long x)
383 return (get_runtime_sec() > x) ? (get_runtime_sec() - x) : 0;
388 static inline unsigned long BASE_TO_MIN(unsigned long x)
390 return BASE_TO_SEC(x) / 60;
393 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
398 case HW_ADP_TYPE_USB:
406 case HW_ADP_TYPE_DUAL:
407 if (support_usb_adp && support_dc_adp)
417 static bool rk81x_chrg_online(struct rk81x_battery *di)
419 return di->usb_online || di->ac_online;
422 static u32 interpolate(int value, u32 *table, int size)
427 for (i = 0; i < size; i++) {
428 if (value < table[i])
432 if ((i > 0) && (i < size)) {
433 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
434 d /= table[i] - table[i-1];
435 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
437 d = i * ((INTERPOLATE_MAX + size / 2) / size);
446 /* Returns (a * b) / c */
447 static int32_t ab_div_c(u32 a, u32 b, u32 c)
453 sign = ((((a^b)^c) & 0x80000000) != 0);
459 tmp = (a * b + (c >> 1)) / c;
471 static int div(int val)
473 return (val == 0) ? 1 : val;
476 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
477 u8 buf[], unsigned len)
482 for (i = 0; ret < 0 && i < 3; i++) {
483 ret = rk818_i2c_read(di->rk818, reg, len, buf);
485 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
488 return (ret < 0) ? ret : 0;
491 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
492 u8 const buf[], unsigned len)
497 for (i = 0; ret < 0 && i < 3; i++) {
498 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
500 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
503 return (ret < 0) ? ret : 0;
506 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
511 for (i = 0; ret < 0 && i < 3; i++) {
512 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
514 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
520 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
525 for (i = 0; ret < 0 && i < 3; i++) {
526 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
528 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
534 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
539 rk81x_bat_read(di, reg, &buf, 1);
540 val = (buf & BIT(shift)) >> shift;
544 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
549 DBG("%s dump charger register start:\n", __func__);
550 for (i = 0xAC; i < 0xEE; i++) {
551 rk81x_bat_read(di, i, &buf, 1);
552 DBG("0x%02x : 0x%02x\n", i, buf);
557 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
562 DBG("%s dump the register start:\n", __func__);
563 for (i = 0x99; i < 0xAB; i++) {
564 rk81x_bat_read(di, i, &buf, 1);
565 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
570 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
572 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
573 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
576 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
578 rk81x_bat_reset_zero_var(di);
579 di->trend_start_cap = di->remain_capacity;
582 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
588 delta_cap = capacity - di->remain_capacity;
592 di->adjust_cap += delta_cap;
594 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
596 buf = (capacity_ma >> 24) & 0xff;
597 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
598 buf = (capacity_ma >> 16) & 0xff;
599 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
600 buf = (capacity_ma >> 8) & 0xff;
601 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
602 buf = (capacity_ma & 0xff) | 0x01;
603 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
604 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
608 if (di->chrg_status != CHARGE_FINISH || di->dod0_status == 1)
609 dev_dbg(di->dev, "update capacity :%d--remain_cap:%d\n",
610 capacity, di->remain_capacity);
615 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
617 static ssize_t bat_calib_read(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 struct power_supply *psy_bat = dev_get_drvdata(dev);
621 struct rk81x_battery *di = to_device_info(psy_bat);
624 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
626 return sprintf(buf, "%d\n", val);
629 static ssize_t bat_calib_write(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t count)
635 struct power_supply *psy_bat = dev_get_drvdata(dev);
636 struct rk81x_battery *di = to_device_info(psy_bat);
638 ret = kstrtou8(buf, 0, &val);
643 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
645 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
650 * interface for debug: force battery to over discharge
652 static ssize_t bat_test_power_read(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct power_supply *psy_bat = dev_get_drvdata(dev);
656 struct rk81x_battery *di = to_device_info(psy_bat);
658 return sprintf(buf, "%d\n", di->fg_drv_mode);
661 static ssize_t bat_test_power_write(struct device *dev,
662 struct device_attribute *attr,
663 const char *buf, size_t count)
667 struct power_supply *psy_bat = dev_get_drvdata(dev);
668 struct rk81x_battery *di = to_device_info(psy_bat);
670 ret = kstrtou8(buf, 0, &val);
675 di->fg_drv_mode = TEST_POWER_MODE;
677 di->fg_drv_mode = FG_NORMAL_MODE;
682 static ssize_t bat_fcc_read(struct device *dev,
683 struct device_attribute *attr, char *buf)
685 struct power_supply *psy_bat = dev_get_drvdata(dev);
686 struct rk81x_battery *di = to_device_info(psy_bat);
688 return sprintf(buf, "%d\n", di->fcc);
691 static ssize_t bat_fcc_write(struct device *dev,
692 struct device_attribute *attr,
693 const char *buf, size_t count)
697 struct power_supply *psy_bat = dev_get_drvdata(dev);
698 struct rk81x_battery *di = to_device_info(psy_bat);
700 ret = kstrtou16(buf, 0, &val);
709 static ssize_t bat_dsoc_read(struct device *dev,
710 struct device_attribute *attr, char *buf)
712 struct power_supply *psy_bat = dev_get_drvdata(dev);
713 struct rk81x_battery *di = to_device_info(psy_bat);
715 return sprintf(buf, "%d\n", di->dsoc);
718 static ssize_t bat_dsoc_write(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t count)
724 struct power_supply *psy_bat = dev_get_drvdata(dev);
725 struct rk81x_battery *di = to_device_info(psy_bat);
727 ret = kstrtou8(buf, 0, &val);
736 static ssize_t bat_rsoc_read(struct device *dev,
737 struct device_attribute *attr, char *buf)
739 struct power_supply *psy_bat = dev_get_drvdata(dev);
740 struct rk81x_battery *di = to_device_info(psy_bat);
742 return sprintf(buf, "%d\n", di->rsoc);
745 static ssize_t bat_rsoc_write(struct device *dev,
746 struct device_attribute *attr,
747 const char *buf, size_t count)
752 struct power_supply *psy_bat = dev_get_drvdata(dev);
753 struct rk81x_battery *di = to_device_info(psy_bat);
755 ret = kstrtou8(buf, 0, &val);
759 capacity = di->fcc * val / 100;
760 rk81x_bat_capacity_init(di, capacity);
761 rk81x_bat_capacity_init_post(di);
766 static ssize_t bat_remain_cap_read(struct device *dev,
767 struct device_attribute *attr,
770 struct power_supply *psy_bat = dev_get_drvdata(dev);
771 struct rk81x_battery *di = to_device_info(psy_bat);
773 return sprintf(buf, "%d\n", di->remain_capacity);
776 static struct device_attribute rk818_bat_attr[] = {
777 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
778 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
779 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
780 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
781 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
782 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
786 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
791 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
793 dev_err(di->dev, "error reading TS_CTRL_REG");
798 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
803 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
805 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
808 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
812 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
817 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
823 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
825 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
828 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
832 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
838 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
840 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
843 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
847 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
853 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
855 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
861 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
867 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
869 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
875 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
881 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
882 buf = (value >> 8) & 0xff;
883 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
888 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
890 int vcalib0, vcalib1;
892 vcalib0 = rk81x_bat_get_vcalib0(di);
893 vcalib1 = rk81x_bat_get_vcalib1(di);
895 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
896 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
897 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
900 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
905 uint16_t voltage_now = 0;
909 for (i = 0; i < 3; i++) {
910 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
912 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
916 dev_err(di->dev, "error read BAT_OCV_REGH");
921 if (val[0] == val[1])
926 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
931 static int rk81x_bat_get_vol(struct rk81x_battery *di)
940 for (i = 0; i < 3; i++) {
941 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
943 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
947 dev_err(di->dev, "error read BAT_VOL_REGH");
952 if (val[0] == val[1])
957 vol = di->voltage_k * temp / 1000 + di->voltage_b;
962 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
967 ret = rk81x_bat_read(di, GGSTS, &status, 1);
969 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
975 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
979 uint16_t temp = 0, voltage_now;
981 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
983 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
986 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
991 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
995 uint16_t temp = 0, voltage_now;
997 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
999 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
1002 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
1007 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1011 uint16_t relax_vol1, relax_vol2;
1014 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1015 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1017 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1018 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1019 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1020 __func__, status, ggcon, relax_vol1, relax_vol2);
1022 if (is_rk81x_bat_relax_mode(di))
1023 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1029 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1030 * based on the voltage.
1032 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1039 ocv_table = di->pdata->battery_ocv;
1040 ocv_size = di->pdata->ocv_size;
1041 tmp = interpolate(voltage, ocv_table, ocv_size);
1042 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1043 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1048 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1054 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1056 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1060 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1062 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1068 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1078 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1082 rk81x_bat_read(di, GGCON, &ggcon, 1);
1083 ggcon &= ~(0x30); /*clear <5:4>*/
1085 rk81x_bat_write(di, GGCON, &ggcon, 1);
1089 * when charger finish signal comes, we need calibrate the current, make it
1092 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1102 if ((di->chrg_status == CHARGE_FINISH) &&
1103 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1104 (abs(di->current_avg) > 4)) {
1105 for (retry = 0; retry < 5; retry++) {
1106 adc_value = rk81x_bat_get_raw_adc_current(di);
1107 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1108 dev_warn(di->dev, "charger plugout\n");
1113 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1114 C0 = rk81x_bat_get_cal_offset(di);
1115 C1 = adc_value + C0;
1116 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1118 rk81x_bat_set_cal_offset(di, C1);
1119 DBG("<%s>. new cal_offset = %d\n",
1120 __func__, rk81x_bat_get_cal_offset(di));
1122 adc_value = rk81x_bat_get_raw_adc_current(di);
1123 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1124 if (abs(adc_value) < 4) {
1125 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1126 ioffset = rk81x_bat_get_ioffset(di);
1127 rk81x_bat_set_cal_offset(di,
1130 ioffset = rk81x_bat_get_ioffset(di);
1131 pcb_offset = C1 - ioffset;
1132 di->pcb_ioffset = pcb_offset;
1133 di->pcb_ioffset_updated = true;
1138 DBG("<%s>. update the cal_offset, C1 = %d\n"
1139 "i_offset = %d, pcb_offset = %d\n",
1140 __func__, C1, ioffset, pcb_offset);
1144 dev_dbg(di->dev, "ioffset cal failed\n");
1145 rk81x_bat_set_cal_offset(di, C0);
1148 di->pcb_ioffset_updated = false;
1155 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1158 int enter_thres, exit_thres;
1159 struct cell_state *cell = &di->cell;
1161 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1162 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1163 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1164 __func__, cell->config->ocv->sleep_enter_current,
1165 cell->config->ocv->sleep_exit_current);
1167 buf = enter_thres & 0xff;
1168 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1169 buf = (enter_thres >> 8) & 0xff;
1170 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1172 buf = exit_thres & 0xff;
1173 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1174 buf = (exit_thres >> 8) & 0xff;
1175 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1177 /* set sample time */
1178 rk81x_bat_read(di, GGCON, &buf, 1);
1179 buf &= ~(3 << 2);/*8min*/
1180 buf &= ~0x01; /* clear bat_res calc*/
1181 rk81x_bat_write(di, GGCON, &buf, 1);
1184 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1189 rk81x_bat_read(di, GGCON, &ggcon, 1);
1191 rk81x_bat_write(di, GGCON, &ggcon, 1);
1193 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1195 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1198 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1207 for (i = 0; i < 3; i++) {
1208 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1210 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1215 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1217 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1223 if (val[0] == val[1])
1224 current_now = val[0];
1226 current_now = val[2];
1228 if (current_now & 0x800)
1229 current_now -= 4096;
1231 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1236 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1237 enum charger_type charger_type)
1239 di->usb_online = OFFLINE;
1240 di->ac_online = OFFLINE;
1242 switch (charger_type) {
1244 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1247 di->usb_online = ONLINE;
1248 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1250 case DC_CHARGER:/*treat dc as ac*/
1252 di->ac_online = ONLINE;
1253 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1256 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1260 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1261 msecs_to_jiffies(1000));
1264 /* high load: current < 0 with charger in.
1265 * System will not shutdown while dsoc=0% with charging state(ac_online),
1266 * which will cause over discharge, so oppose status before report states.
1268 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1271 int pwr_off_thresd = di->pdata->power_off_thresd;
1273 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1275 time = get_runtime_sec();
1277 if (BASE_TO_SEC(time) > (MINUTE)) {
1278 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1279 dev_info(di->dev, "low power....\n");
1282 if (di->voltage <= pwr_off_thresd - 50) {
1284 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1291 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1295 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1297 return (buf & 0x80) ? 1 : 0;
1300 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1305 rk81x_bat_read(di, GGSTS, &buf, 1);
1306 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1307 /*di->pwron_bat_con = buf;*/
1311 rk81x_bat_write(di, GGSTS, &buf, 1);
1312 rk81x_bat_read(di, GGSTS, &temp, 1);
1313 } while (temp & BAT_CON);
1320 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1327 ocv_table = di->pdata->battery_ocv;
1328 ocv_size = di->pdata->ocv_size;
1330 for (j = 0; j < 21; j++)
1334 for (i = 1; i < ocv_size-1; i++) {
1335 if (ocv_table[i+1] < ocv_table[i] + 20)
1336 temp_table[j++] = i;
1339 temp_table[j] = temp_table[j-1] + 1;
1341 di->enter_flatzone = ocv_table[i];
1344 for (i = 0; i < 20; i++) {
1345 if (temp_table[i] < temp_table[i+1])
1350 di->exit_flatzone = ocv_table[i];
1352 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1353 di->enter_flatzone, di->exit_flatzone);
1356 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1358 u8 ocv_valid, first_pwron;
1362 /*buf==1: OCV_VOL is valid*/
1363 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1364 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1365 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1367 if (first_pwron == 1 || ocv_valid == 1) {
1368 DBG("<%s> enter.\n", __func__);
1369 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1370 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1371 di->dod0_voltage = ocv_voltage;
1372 di->dod0_capacity = di->temp_nac;
1377 di->dod0_level = 100;
1378 else if (ocv_soc < 5)
1379 di->dod0_level = 95;
1380 else if (ocv_soc < 10)
1381 di->dod0_level = 90;
1383 di->dod0_level = 80;
1384 /* save_soc = di->dod0_level; */
1385 soc_level = rk81x_bat_get_level(di);
1386 if (soc_level > di->dod0_level) {
1387 di->dod0_status = 0;
1389 if (soc_level <= 80)
1391 rk81x_bat_save_level(di, soc_level);
1393 di->dod0_status = 1;
1395 di->fcc_update_sec = get_runtime_sec();
1398 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1399 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1401 di->dod0_voltage, di->dod0_capacity,
1402 ocv_soc, soc_level, di->dod0_status,
1408 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1410 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1413 static enum power_supply_property rk_battery_props[] = {
1414 POWER_SUPPLY_PROP_STATUS,
1415 POWER_SUPPLY_PROP_CURRENT_NOW,
1416 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1417 POWER_SUPPLY_PROP_PRESENT,
1418 POWER_SUPPLY_PROP_HEALTH,
1419 POWER_SUPPLY_PROP_CAPACITY,
1422 static int rk81x_battery_get_property(struct power_supply *psy,
1423 enum power_supply_property psp,
1424 union power_supply_propval *val)
1426 struct rk81x_battery *di = to_device_info(psy);
1429 case POWER_SUPPLY_PROP_CURRENT_NOW:
1430 val->intval = di->current_avg * 1000;/*uA*/
1431 if (di->fg_drv_mode == TEST_POWER_MODE)
1432 val->intval = TEST_CURRENT * 1000;
1434 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1435 val->intval = di->voltage * 1000;/*uV*/
1436 if (di->fg_drv_mode == TEST_POWER_MODE)
1437 val->intval = TEST_VOLTAGE * 1000;
1440 case POWER_SUPPLY_PROP_PRESENT:
1441 val->intval = is_rk81x_bat_exist(di);
1442 if (di->fg_drv_mode == TEST_POWER_MODE)
1443 val->intval = TEST_PRESET;
1446 case POWER_SUPPLY_PROP_CAPACITY:
1447 val->intval = di->dsoc;
1448 if (di->fg_drv_mode == TEST_POWER_MODE)
1449 val->intval = TEST_SOC;
1451 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1453 case POWER_SUPPLY_PROP_HEALTH:
1454 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1456 case POWER_SUPPLY_PROP_STATUS:
1457 val->intval = di->psy_status;
1458 if (di->fg_drv_mode == TEST_POWER_MODE)
1459 val->intval = TEST_STATUS;
1469 static enum power_supply_property rk_battery_ac_props[] = {
1470 POWER_SUPPLY_PROP_ONLINE,
1473 static enum power_supply_property rk_battery_usb_props[] = {
1474 POWER_SUPPLY_PROP_ONLINE,
1477 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1478 enum power_supply_property psp,
1479 union power_supply_propval *val)
1482 struct rk81x_battery *di = to_ac_device_info(psy);
1485 case POWER_SUPPLY_PROP_ONLINE:
1486 if (rk81x_chrg_online(di))
1487 rk81x_bat_lowpwr_check(di);
1488 val->intval = di->ac_online; /*discharging*/
1489 if (di->fg_drv_mode == TEST_POWER_MODE)
1490 val->intval = TEST_AC_ONLINE;
1500 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1501 enum power_supply_property psp,
1502 union power_supply_propval *val)
1505 struct rk81x_battery *di = to_usb_device_info(psy);
1508 case POWER_SUPPLY_PROP_ONLINE:
1509 if (rk81x_chrg_online(di))
1510 rk81x_bat_lowpwr_check(di);
1511 val->intval = di->usb_online;
1512 if (di->fg_drv_mode == TEST_POWER_MODE)
1513 val->intval = TEST_USB_ONLINE;
1523 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1527 di->bat.name = "BATTERY";
1528 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1529 di->bat.properties = rk_battery_props;
1530 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1531 di->bat.get_property = rk81x_battery_get_property;
1534 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1535 di->ac.properties = rk_battery_ac_props;
1536 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1537 di->ac.get_property = rk81x_battery_ac_get_property;
1539 di->usb.name = "USB";
1540 di->usb.type = POWER_SUPPLY_TYPE_USB;
1541 di->usb.properties = rk_battery_usb_props;
1542 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1543 di->usb.get_property = rk81x_battery_usb_get_property;
1545 ret = power_supply_register(di->dev, &di->bat);
1547 dev_err(di->dev, "failed to register main battery\n");
1550 ret = power_supply_register(di->dev, &di->usb);
1552 dev_err(di->dev, "failed to register usb power supply\n");
1555 ret = power_supply_register(di->dev, &di->ac);
1557 dev_err(di->dev, "failed to register ac power supply\n");
1564 power_supply_unregister(&di->ac);
1566 power_supply_unregister(&di->usb);
1568 power_supply_unregister(&di->bat);
1573 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1577 static u32 capacity_ma;
1579 if (capacity >= di->qmax)
1580 capacity = di->qmax;
1585 if (capacity_ma == capacity)
1588 capacity_ma = capacity;
1590 buf = (capacity_ma >> 24) & 0xff;
1591 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1592 buf = (capacity_ma >> 16) & 0xff;
1593 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1594 buf = (capacity_ma >> 8) & 0xff;
1595 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1596 buf = (capacity_ma & 0xff) | 0x01;
1597 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1600 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1608 for (i = 0; i < 3; i++) {
1609 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1611 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1612 val[i] |= buf << 16;
1613 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1615 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1619 if (val[0] == val[1])
1627 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1632 capacity_ma = capacity;
1633 buf = (capacity_ma >> 24) & 0xff;
1634 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1635 buf = (capacity_ma >> 16) & 0xff;
1636 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1637 buf = (capacity_ma >> 8) & 0xff;
1638 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1639 buf = (capacity_ma & 0xff) | 0x01;
1640 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1642 dev_info(di->dev, "update fcc : %d\n", capacity);
1645 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1650 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1651 capacity = buf << 24;
1652 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1653 capacity |= buf << 16;
1654 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1655 capacity |= buf << 8;
1656 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1659 if (capacity < MIN_FCC) {
1660 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1662 capacity = di->design_capacity;
1663 rk81x_bat_save_fcc(di, capacity);
1664 } else if (capacity > di->qmax) {
1665 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1666 capacity = di->qmax;
1667 rk81x_bat_save_fcc(di, capacity);
1673 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1682 for (i = 0; i < 3; i++) {
1683 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1685 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1686 val[i] |= buf << 16;
1687 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1689 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1692 if (val[0] == val[1])
1697 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1702 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1706 if (last_soc != save_soc) {
1707 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1708 last_soc = save_soc;
1714 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1719 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1723 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1727 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1728 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1729 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1730 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1733 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1734 enum charger_type charger_type)
1736 switch (charger_type) {
1739 rk81x_bat_set_current(di, ILIM_450MA);
1743 rk81x_bat_set_current(di, di->chrg_i_lmt);
1746 rk81x_bat_set_current(di, ILIM_450MA);
1750 #if defined(CONFIG_ARCH_ROCKCHIP)
1752 static void rk81x_bat_set_charger_param(struct rk81x_battery *di,
1753 enum charger_type charger_type)
1755 rk81x_bat_set_chrg_current(di, charger_type);
1756 rk81x_bat_set_power_supply_state(di, charger_type);
1758 switch (charger_type) {
1760 power_supply_changed(&di->bat);
1764 power_supply_changed(&di->usb);
1767 power_supply_changed(&di->ac);
1774 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1777 enum charger_type charger_type = NO_CHARGER;
1779 if (di->fg_drv_mode == TEST_POWER_MODE) {
1780 charger_type = DC_CHARGER;
1787 if (!gpio_is_valid(di->dc_det_pin))
1790 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1792 pr_err("Failed to request gpio %d with ret:""%d\n",
1793 di->dc_det_pin, ret);
1797 gpio_direction_input(di->dc_det_pin);
1798 ret = gpio_get_value(di->dc_det_pin);
1799 if (ret == di->dc_det_level)
1800 charger_type = DC_CHARGER;
1802 charger_type = NO_CHARGER;
1803 gpio_free(di->dc_det_pin);
1805 return charger_type;
1808 static void rk81x_battery_dc_delay_work(struct work_struct *work)
1810 enum charger_type charger_type;
1811 struct rk81x_battery *di = container_of(work,
1812 struct rk81x_battery, dc_det_check_work.work);
1814 charger_type = rk81x_bat_get_dc_state(di);
1816 if (charger_type == DC_CHARGER)
1817 rk81x_bat_set_charger_param(di, DC_CHARGER);
1818 else/*NO_CHARGER: maybe usb charger still plugin*/
1819 queue_delayed_work(di->wq,
1820 &di->ac_usb_check_work,
1821 msecs_to_jiffies(10));
1824 static void rk81x_battery_acusb_delay_work(struct work_struct *work)
1827 int gadget_flag, usb_id;
1828 struct rk81x_battery *di = container_of(work,
1829 struct rk81x_battery, ac_usb_check_work.work);
1831 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1832 usb_id = dwc_otg_check_dpdm(0);
1835 if ((buf & PLUG_IN_STS) != 0)
1836 rk81x_bat_set_charger_param(di, DC_CHARGER);
1838 rk81x_bat_set_charger_param(di, NO_CHARGER);
1842 rk81x_bat_set_charger_param(di, USB_CHARGER);
1845 rk81x_bat_set_charger_param(di, AC_CHARGER);
1850 /*check unstanderd charger*/
1851 if (usb_id == 1 || usb_id == 3) {
1852 gadget_flag = get_gadget_connect_flag();
1853 if (0 == gadget_flag) {
1855 if (di->check_count >= 5) {
1856 di->check_count = 0;
1857 rk81x_bat_set_charger_param(di, AC_CHARGER);
1859 queue_delayed_work(di->wq,
1860 &di->ac_usb_check_work,
1861 msecs_to_jiffies(1000));
1863 } else {/*confirm: USB_CHARGER*/
1864 di->check_count = 0;
1870 #if defined(CONFIG_X86_INTEL_SOFIA)
1871 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1873 enum charger_type charger_type;
1876 charger_type = AC_CHARGER;
1878 charger_type = USB_CHARGER;
1880 charger_type = NO_CHARGER;
1884 dev_info(di->dev, "limit current:%d\n", ma);
1886 return charger_type;
1889 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1891 struct rk81x_battery *di;
1892 enum charger_type type;
1894 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1895 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1897 rk81x_bat_set_chrg_current(di, type);
1898 power_supply_changed(&di->usb);
1901 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1902 unsigned long event, void *data)
1904 struct rk81x_battery *di;
1905 struct power_supply_cable_props *cable_props;
1906 enum charger_type type;
1908 di = container_of(nb, struct rk81x_battery, usb_nb);
1914 case USB_EVENT_CHARGER:
1915 cable_props = (struct power_supply_cable_props *)data;
1916 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1917 rk81x_bat_set_power_supply_state(di, type);
1918 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1919 msecs_to_jiffies(50));
1930 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1931 unsigned long event, void *data)
1933 struct rk81x_battery *di;
1934 struct fb_event *evdata = data;
1937 di = container_of(nb, struct rk81x_battery, fb_nb);
1939 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1942 blank = *(int *)evdata->data;
1944 if (di->fb_blank != blank)
1945 di->fb_blank = blank;
1949 if (blank == FB_BLANK_UNBLANK)
1950 di->early_resume = 1;
1955 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1957 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1958 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1960 return fb_register_client(&di->fb_nb);
1964 * it is first time for battery to be weld, init by ocv table
1966 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1968 rk81x_bat_save_fcc(di, di->design_capacity);
1969 di->fcc = rk81x_bat_get_fcc(di);
1971 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1972 di->dsoc = di->rsoc;
1973 di->nac = di->temp_nac;
1975 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1976 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1977 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1978 __func__, di->dsoc, di->nac, di->fcc);
1981 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
1986 int relax_vol = di->relax_voltage;
1987 int ocv_vol = di->voltage_ocv;
1989 init_cur = rk81x_bat_get_avg_current(di);
1990 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
1992 est_vol = di->voltage - diff;
1994 if (di->loader_charged) {
1995 calib_vol = est_vol;
1999 if (di->pwroff_min > 8) {
2000 if (abs(relax_vol - ocv_vol) < 100) {
2001 calib_vol = ocv_vol;
2003 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
2004 calib_vol = ocv_vol;
2006 calib_vol = relax_vol;
2008 } else if (di->pwroff_min > 2) {
2009 calib_vol = ocv_vol;
2014 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2015 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2021 * it is not first time for battery to be weld, init by last record info
2023 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2027 int remain_capacity;
2029 int calib_vol, calib_soc, calib_capacity;
2031 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2032 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2033 init_soc = pwron_soc;
2034 remain_capacity = rk81x_bat_get_remain_capacity(di);
2036 /* check if support uboot charge,
2037 * if support, uboot charge driver should have done init work,
2038 * so here we should skip init work
2040 #if defined(CONFIG_ARCH_ROCKCHIP)
2041 if (di->loader_charged) {
2042 dev_info(di->dev, "loader charged\n");
2046 calib_vol = rk81x_bat_get_calib_vol(di);
2047 if (calib_vol > 0) {
2048 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2049 calib_capacity = di->temp_nac;
2051 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2052 init_soc = calib_soc;
2053 remain_capacity = calib_capacity;
2055 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2056 calib_vol, init_soc, remain_capacity);
2059 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2060 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2061 __func__, remain_capacity, di->temp_nac);
2063 if (di->pwroff_min > 0) {
2064 if (di->pwroff_min > 30) {
2065 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2067 remain_capacity = di->temp_nac;
2068 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2069 __func__, remain_capacity);
2071 } else if ((di->pwroff_min > 5) &&
2072 (abs(ocv_soc - init_soc) >= 10)) {
2073 if (remain_capacity >= di->temp_nac * 120/100)
2074 remain_capacity = di->temp_nac * 110/100;
2075 else if (remain_capacity < di->temp_nac * 8/10)
2076 remain_capacity = di->temp_nac * 9/10;
2077 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2078 __func__, remain_capacity);
2081 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2084 di->dsoc = init_soc;
2085 di->nac = remain_capacity;
2088 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2089 pwron_soc, di->dsoc, di->nac);
2092 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2094 u8 curr_pwroff_min, last_pwroff_min;
2096 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2097 &curr_pwroff_min, 1);
2098 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2099 &last_pwroff_min, 1);
2101 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2102 &curr_pwroff_min, 1);
2104 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2107 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2109 u8 calib_en;/*debug*/
2111 di->voltage = rk81x_bat_get_vol(di);
2112 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2113 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2114 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2115 di->current_avg = rk81x_bat_get_avg_current(di);
2117 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2118 di->voltage, di->voltage_ocv, di->relax_voltage,
2119 di->current_avg, di->pwroff_min);
2121 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2122 DBG("readbit: calib_en=%d\n", calib_en);
2123 if (is_rk81x_bat_first_poweron(di) ||
2124 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2125 rk81x_bat_first_pwron(di);
2126 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2129 rk81x_bat_not_first_pwron(di);
2135 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2140 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2145 DBG(" CHARGE-OFF ...\n");
2149 DBG(" DEAD CHARGE ...\n");
2151 case TRICKLE_CHARGE:
2153 DBG(" TRICKLE CHARGE ...\n ");
2157 DBG(" CC or CV ...\n");
2160 ret = CHARGE_FINISH;
2161 DBG(" CHARGE FINISH ...\n");
2165 DBG(" USB OVER VOL ...\n");
2169 DBG(" BAT TMP ERROR ...\n");
2173 DBG(" TIMER ERROR ...\n");
2177 DBG(" USB EXIST ...\n");
2181 DBG(" USB EFF...\n");
2190 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2191 int chrg_ilim, int chrg_cur)
2195 di->chrg_v_lmt = DEF_CHRG_VOL;
2196 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2197 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2199 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2200 if (chrg_vol < CHRG_V_LMT[i])
2203 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2206 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2207 if (chrg_ilim < CHRG_I_LMT[i])
2210 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2213 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2214 if (chrg_cur < CHRG_I_CUR[i])
2217 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2219 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2220 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2223 static u8 rk81x_bat_select_finish_ma(int fcc)
2225 u8 ma = FINISH_150MA;
2230 else if (fcc >= 4000)
2233 else if (fcc >= 3000)
2243 * there is a timer inside rk81x to calc how long the battery is in charging
2244 * state. rk81x will close PowerPath inside IC when timer reach, which will
2245 * stop the charging work. we have to reset the corresponding bits to restart
2246 * the timer to avoid that case.
2248 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2252 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2253 buf &= ~CHRG_TIMER_CCCV_EN;
2254 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2256 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2257 buf |= CHRG_TIMER_CCCV_EN;
2258 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2259 dev_info(di->dev, "reset cccv charge timer\n");
2263 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2265 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2266 u8 sup_sts_reg, thremal_reg, ggcon;
2267 int chrg_vol, chrg_cur, chrg_ilim;
2270 chrg_vol = di->pdata->max_charger_voltagemV;
2271 chrg_cur = di->pdata->max_charger_currentmA;
2272 chrg_ilim = di->pdata->max_charger_ilimitmA;
2274 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2275 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2277 /*rk81x_bat_init_chrg_timer(di);*/
2279 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2280 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2281 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2282 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2283 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2284 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2285 rk81x_bat_read(di, GGCON, &ggcon, 1);
2287 usb_ctrl_reg &= (~0x0f);
2289 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2290 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2292 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2294 if (di->fg_drv_mode == TEST_POWER_MODE)
2295 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2297 chrg_ctrl_reg1 &= (0x00);
2298 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2300 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2301 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2303 chrg_ctrl_reg2 &= ~(0xc7);
2304 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2306 sup_sts_reg &= ~(0x01 << 3);
2307 sup_sts_reg |= (0x01 << 2);
2309 thremal_reg &= (~0x0c);
2310 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2311 ggcon |= ADC_CURRENT_MODE;
2313 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2314 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2315 /*don't touch charge setting when boot int loader charge mode*/
2316 if (!di->loader_charged)
2317 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2318 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2319 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2320 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2321 rk81x_bat_write(di, GGCON, &ggcon, 1);
2324 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2331 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2333 rk81x_bat_gauge_enable(di);
2334 /* get the volatege offset */
2335 rk81x_bat_get_vol_offset(di);
2336 rk81x_bat_charger_init(di);
2337 rk81x_bat_set_relax_thres(di);
2339 /* get the current offset , the value write to the CAL_OFFSET */
2340 di->current_offset = rk81x_bat_get_ioffset(di);
2341 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2342 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2343 __func__, pcb_offset, di->current_offset);
2345 pcb_offset = DEF_PCB_OFFSET;
2346 cal_offset = pcb_offset + di->current_offset;
2347 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2348 cal_offset = DEF_CAL_OFFSET;
2349 rk81x_bat_set_cal_offset(di, cal_offset);
2350 /* set sample time for cal_offset interval*/
2351 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2353 rk81x_bat_rsoc_init(di);
2354 rk81x_bat_capacity_init(di, di->nac);
2355 rk81x_bat_capacity_init_post(di);
2357 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2358 di->current_avg = rk81x_bat_get_avg_current(di);
2360 rk81x_bat_restart_relax(di);
2361 rk81x_bat_power_on_save(di, di->voltage_ocv);
2363 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2365 rk81x_dbg_dmp_gauge_regs(di);
2366 rk81x_dbg_dmp_charger_regs(di);
2369 "nac = %d , remain_capacity = %d\n"
2370 "OCV_voltage = %d, voltage = %d\n"
2371 "SOC = %d, fcc = %d\n, current=%d\n"
2372 "cal_offset = 0x%x\n",
2374 di->nac, di->remain_capacity,
2375 di->voltage_ocv, di->voltage,
2376 di->dsoc, di->fcc, di->current_avg,
2380 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2382 int dead_voltage, ocv_voltage;
2383 int voltage, voltage_old, voltage_now;
2388 int ocv_soc, dead_soc;
2389 int power_off_thresd = di->pdata->power_off_thresd;
2392 voltage_old = rk81x_bat_get_vol(di);
2394 voltage_now = rk81x_bat_get_vol(di);
2396 } while ((voltage_old == voltage_now) && (count_num < 11));
2397 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2400 for (i = 0; i < 10; i++) {
2401 voltage += rk81x_bat_get_vol(di);
2406 currentnow = rk81x_bat_get_avg_current(di);
2408 /* 50 mo power-path mos */
2409 dead_voltage = power_off_thresd - currentnow *
2410 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2412 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2413 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2414 dead_voltage, ocv_voltage);
2416 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2417 q_dead = di->temp_nac;
2418 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2421 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2422 q_ocv = di->temp_nac;
2423 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2426 rsoc = ocv_soc - dead_soc;
2427 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2429 } else if (rsoc > 0) {
2430 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2433 di->display_soc = di->dsoc * 1000;
2436 di->zero_old_remain_cap = di->remain_capacity;
2438 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2439 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2440 di->line_k, di->dsoc, rsoc,
2441 di->display_soc, di->zero_old_remain_cap);
2444 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2446 int delta_cap, delta_soc;
2449 di->zero_timeout_cnt++;
2450 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2451 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2453 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2454 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2455 "ZERO1: timeout_cnt=%d\n\n",
2456 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2457 delta_soc, delta_cap, di->zero_old_remain_cap,
2458 di->zero_timeout_cnt);
2460 if ((delta_soc >= MIN_ZERO_ACCURACY) ||
2461 (di->zero_timeout_cnt > 500)) {
2462 DBG("ZERO1:--------- enter calc -----------\n");
2463 di->zero_timeout_cnt = 0;
2464 di->display_soc -= delta_soc;
2465 tmp_dsoc = (di->display_soc + MIN_ROUND_ACCURACY) / 1000;
2466 di->dsoc = tmp_dsoc;
2467 /* need to be init, otherwise when switch between discharge and
2468 * charge display_soc will be init as: dsoc * 1000
2470 di->last_zero_mode_dsoc = tmp_dsoc;
2471 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2472 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2474 rk81x_bat_zero_calc_linek(di);
2478 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2480 return (di->voltage -
2481 (di->bat_res * di->current_avg) / 1000);
2484 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2486 int ocv_soc, ocv_voltage;
2488 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2489 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2494 /* we will estimate a ocv voltage to get a ocv soc.
2495 * if there is a big offset between ocv_soc and rsoc,
2496 * we will decide whether we should reinit capacity or not
2498 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2500 int ocv_soc = di->est_ocv_soc;
2501 int ocv_volt = di->est_ocv_vol;
2502 int rsoc = rk81x_bat_get_rsoc(di);
2503 int max_volt = di->pdata->max_charger_voltagemV;
2505 if (ocv_volt > max_volt)
2508 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2509 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2511 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2513 di->err_soc_sum += ocv_soc;
2517 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2518 __func__, di->err_chck_cnt, di->err_soc_sum);
2520 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2521 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2522 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2523 ocv_soc += RSOC_COMPS;
2525 di->temp_nac = ocv_soc * di->fcc / 100;
2526 rk81x_bat_capacity_init(di, di->temp_nac);
2527 rk81x_bat_capacity_init_post(di);
2528 di->rsoc = rk81x_bat_get_rsoc(di);
2529 di->remain_capacity =
2530 rk81x_bat_get_realtime_capacity(di);
2531 di->err_soc_sum = 0;
2532 di->err_chck_cnt = 0;
2533 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2537 di->err_chck_cnt = 0;
2538 di->err_soc_sum = 0;
2542 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2544 u8 status = di->psy_status;
2546 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2547 (status == POWER_SUPPLY_STATUS_FULL)) {
2548 if ((di->current_avg < 0) &&
2549 (di->chrg_status != CHARGE_FINISH))
2550 rk81x_bat_rsoc_dischrg_check(di);
2553 rsoc_chrg_calib(di);
2556 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2557 rk81x_bat_rsoc_dischrg_check(di);
2561 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2563 u32 temp, soc_time = 0;
2564 unsigned long sec_unit;
2566 if (!di->dischrg_emu_base)
2567 di->dischrg_emu_base = get_runtime_sec();
2569 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2571 temp = di->fcc * 3600 / 100;
2573 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2574 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2576 soc_time = temp / div(abs(di->current_avg));
2578 if (sec_unit > soc_time) {
2580 di->dischrg_emu_base = get_runtime_sec();
2581 di->dischrg_save_sec = 0;
2584 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2585 __func__, soc_time, sec_unit);
2589 * when there is a big offset between dsoc and rsoc, dsoc needs to
2590 * speed up to keep pace witch rsoc.
2592 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2594 u32 soc_time = 0, temp;
2596 unsigned long chrg_emu_sec;
2598 if (!di->chrg_emu_base)
2599 di->chrg_emu_base = get_runtime_sec();
2601 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2602 temp = di->fcc * 3600 / 100;
2604 if (di->ac_online) {
2605 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2606 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2608 soc_time = temp / div(abs(di->current_avg));
2610 soc_time = temp / 450;
2613 plus_soc = chrg_emu_sec / soc_time;
2614 if (chrg_emu_sec > soc_time) {
2615 di->dsoc += plus_soc;
2616 di->chrg_emu_base = get_runtime_sec();
2617 di->chrg_save_sec = 0;
2620 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2621 __func__, soc_time, chrg_emu_sec, plus_soc);
2624 /* check voltage and current when dsoc is close to full.
2625 * we will do a fake charge to adjust charing speed which
2626 * aims to make battery full charged and match finish signal.
2628 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2632 unsigned long chrg_term_sec;
2634 if (!di->chrg_term_base)
2635 di->chrg_term_base = get_runtime_sec();
2637 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2638 /*check current and voltage*/
2640 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2642 plus_soc = chrg_term_sec / soc_time;
2643 if (chrg_term_sec > soc_time) {
2644 di->dsoc += plus_soc;
2645 di->chrg_term_base = get_runtime_sec();
2646 di->chrg_save_sec = 0;
2648 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2649 __func__, soc_time, chrg_term_sec, plus_soc);
2652 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2655 int now_current = di->current_avg;
2656 unsigned long dischrg_normal_sec;
2658 if (!di->dischrg_normal_base)
2659 di->dischrg_normal_base = get_runtime_sec();
2661 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2662 di->dischrg_save_sec;
2664 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2665 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2666 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2668 if (di->rsoc == di->dsoc) {
2669 DBG("<%s>. rsoc == dsoc\n", __func__);
2670 di->dsoc = di->rsoc;
2671 di->dischrg_normal_base = get_runtime_sec();
2672 di->dischrg_save_sec = 0;
2673 /*di->discharge_smooth_status = false;*/
2674 } else if (di->rsoc > di->dsoc - 1) {
2675 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2676 if (dischrg_normal_sec > soc_time * 3 / 2) {
2678 di->dischrg_normal_base = get_runtime_sec();
2679 di->dischrg_save_sec = 0;
2681 di->discharge_smooth_status = true;
2683 } else if (di->rsoc < di->dsoc - 1) {
2684 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2685 if (dischrg_normal_sec > soc_time * 3 / 4) {
2687 di->dischrg_normal_base = get_runtime_sec();
2688 di->dischrg_save_sec = 0;
2690 di->discharge_smooth_status = true;
2692 } else if (di->rsoc == di->dsoc - 1) {
2693 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2694 if (di->discharge_smooth_status) {
2695 if (dischrg_normal_sec > soc_time * 3 / 4) {
2697 di->dischrg_normal_base = get_runtime_sec();
2698 di->dischrg_save_sec = 0;
2699 di->discharge_smooth_status = false;
2703 di->dischrg_normal_base = get_runtime_sec();
2704 di->dischrg_save_sec = 0;
2705 di->discharge_smooth_status = false;
2709 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2710 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2711 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2712 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2715 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2720 /* first resume from suspend: we don't run this,
2721 * the sleep_dischrg will handle dsoc, and what
2722 * ever this is fake wakeup or not, we should clean
2723 * zero algorithm mode, or it will handle the dsoc.
2726 rk81x_bat_reset_zero_var(di);
2730 di->rsoc = rk81x_bat_get_rsoc(di);
2732 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2733 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2735 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2736 delta_soc = di->dsoc - di->rsoc;
2738 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2739 di->dischrg_normal_base = 0;
2740 rk81x_bat_emulator_dischrg(di);
2742 di->chrg_emu_base = 0;
2743 rk81x_bat_normal_dischrg(di);
2746 if (di->voltage < ZERO_ALGOR_THRESD) {
2747 di->dischrg_normal_base = 0;
2748 di->chrg_emu_base = 0;
2749 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2750 di->zero_timeout_cnt = 0;
2752 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2753 __func__, di->dsoc, di->last_zero_mode_dsoc);
2754 if (di->dsoc != di->last_zero_mode_dsoc) {
2755 tmp_dsoc = (di->display_soc +
2756 MIN_ROUND_ACCURACY) / 1000;
2757 /* if last display_soc invalid, recalc.
2758 * otherwise keep this value(in case: plugin and
2759 * plugout quickly or wakeup from deep sleep,
2760 * we need't init display_soc)
2762 if (tmp_dsoc != di->dsoc)
2763 /* first init value should round up,
2764 * other wise dsoc will quickly turn to
2765 * dsoc-- if MIN_ROUND_ACCURACY value is
2766 * small,eg:1.(in case: power on system)
2768 di->display_soc = (di->dsoc + 1) *
2769 1000 - MIN_ROUND_ACCURACY;
2770 di->last_zero_mode_dsoc = di->dsoc;
2771 rk81x_bat_zero_calc_linek(di);
2772 DBG("<%s>. first calc, init linek\n", __func__);
2776 rk81x_bat_zero_algorithm(di);
2778 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2779 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2780 di->zero_timeout_cnt = 0;
2781 DBG("<%s>. exit zero_algorithm\n", __func__);
2786 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2789 static int old_index;
2792 int mod = di->dsoc % 10;
2793 int index = di->dsoc / 10;
2795 if (rk81x_chrg_online(di))
2796 time = di->plug_in_min;
2798 time = di->plug_out_min;
2800 if ((mod == 0) && (index > 0) && (old_index != index)) {
2801 di->chrg_min[index-1] = time - old_min;
2806 for (i = 1; i < 11; i++)
2807 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2811 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2813 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2814 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2815 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2818 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2819 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2820 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2821 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2822 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2823 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2824 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2825 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2826 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2827 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2828 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2831 DBG("\n------------- dump_debug_regs -----------------\n"
2832 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2833 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2834 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2835 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2836 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2837 ggcon_reg, ggsts_reg, rtc_val,
2838 sup_tst_reg, vb_mod_reg,
2839 usb_ctrl_reg, chrg_ctrl_reg1,
2840 thremal_reg, misc_reg,
2841 chrg_ctrl_reg2, chrg_ctrl_reg3
2844 DBG("#######################################################\n"
2845 "voltage = %d, current-avg = %d\n"
2846 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2847 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2848 "display_soc = %d, cpapacity_soc = %d\n"
2849 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2850 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2851 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2852 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2853 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2854 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2855 "#########################################################\n",
2856 di->voltage, di->current_avg,
2857 di->fcc, di->remain_capacity, di->voltage_ocv,
2858 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2860 di->ac_online, di->usb_online, di->psy_status,
2861 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2862 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2863 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2864 BASE_TO_SEC(di->chrg_normal_base),
2865 BASE_TO_SEC(di->dischrg_normal_base),
2866 BASE_TO_SEC(di->chrg_term_base),
2867 BASE_TO_SEC(di->chrg_emu_base),
2868 BASE_TO_SEC(di->dischrg_emu_base),
2869 BASE_TO_SEC(di->power_on_base), g_base_sec,
2870 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2874 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2878 int dod0_to_soc100_min;
2880 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2881 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2883 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2884 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2885 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2886 di->dod0_capacity, dod0_to_soc100_min);
2888 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2889 (dod0_to_soc100_min < 1200)) {
2890 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2891 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2893 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2895 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2896 __func__, fcc0, di->fcc);
2898 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2899 di->dod0_status = 0;
2901 rk81x_bat_capacity_init(di, di->fcc);
2902 rk81x_bat_capacity_init_post(di);
2903 rk81x_bat_save_fcc(di, di->fcc);
2904 rk81x_bat_save_level(di, di->dod0_level);
2905 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2908 di->dod0_status = 0;
2912 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2914 if (di->chrg_status == CHARGE_FINISH) {
2915 di->debug_finish_real_soc = di->dsoc;
2916 di->debug_finish_temp_soc = di->rsoc;
2920 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2922 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2925 if ((di->chrg_status == CHARGE_FINISH) &&
2926 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2927 rk81x_bat_update_fcc(di);/* save new fcc*/
2928 ret = rk81x_bat_zero_current_calib(di);
2930 di->enter_finish = false;
2932 rk81x_bat_dbg_get_finish_soc(di);
2936 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2938 unsigned long sec_finish;
2939 int soc_time = 0, plus_soc;
2942 if (di->dsoc < 100) {
2943 if (!di->chrg_finish_base)
2944 di->chrg_finish_base = get_runtime_sec();
2946 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2948 temp = di->fcc * 3600 / 100;
2950 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2952 soc_time = temp / 450;
2954 plus_soc = sec_finish / soc_time;
2955 if (sec_finish > soc_time) {
2956 di->dsoc += plus_soc;
2957 di->chrg_finish_base = get_runtime_sec();
2958 di->chrg_save_sec = 0;
2960 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2961 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2962 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2966 static u8 rk81x_bat_get_valid_soc(unsigned long soc)
2968 return (soc <= 100) ? soc : 0;
2971 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2974 u32 soc_time, unit_sec;
2976 unsigned long chrg_normal_sec;
2978 now_current = rk81x_bat_get_avg_current(di);
2979 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2981 if (!di->chrg_normal_base)
2982 di->chrg_normal_base = get_runtime_sec();
2984 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2985 di->rsoc = rk81x_bat_get_rsoc(di);
2987 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2988 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2990 if (di->dsoc == di->rsoc) {
2991 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2992 di->rsoc = rk81x_bat_get_rsoc(di);
2993 di->chrg_normal_base = get_runtime_sec();
2994 di->chrg_save_sec = 0;
2995 /*di->charge_smooth_status = false;*/
2996 } else if (di->rsoc < di->dsoc + 1) {
2997 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
2998 unit_sec = soc_time * 3 / 2;
2999 plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
3000 if (chrg_normal_sec > unit_sec) {
3001 di->dsoc += plus_soc;
3002 di->chrg_normal_base = get_runtime_sec();
3003 di->chrg_save_sec = 0;
3005 di->charge_smooth_status = true;
3006 } else if (di->rsoc > di->dsoc + 1) {
3007 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
3008 unit_sec = soc_time * 3 / 4;
3009 plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
3010 if (chrg_normal_sec > unit_sec) {
3011 di->dsoc += plus_soc;
3012 di->chrg_normal_base = get_runtime_sec();
3013 di->chrg_save_sec = 0;
3015 di->charge_smooth_status = true;
3016 } else if (di->rsoc == di->dsoc + 1) {
3017 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3018 if (di->charge_smooth_status) {
3019 unit_sec = soc_time * 3 / 4;
3020 if (chrg_normal_sec > unit_sec) {
3021 di->dsoc = di->rsoc;
3022 di->chrg_normal_base = get_runtime_sec();
3023 di->charge_smooth_status = false;
3024 di->chrg_save_sec = 0;
3027 di->dsoc = di->rsoc;
3028 di->chrg_normal_base = get_runtime_sec();
3029 di->charge_smooth_status = false;
3030 di->chrg_save_sec = 0;
3034 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3035 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3036 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3037 chrg_normal_sec, soc_time, plus_soc);
3040 static void rk81x_bat_update_time(struct rk81x_battery *di)
3044 runtime_sec = get_runtime_sec();
3046 /*update by charger type*/
3047 if (rk81x_chrg_online(di))
3048 di->plug_out_base = runtime_sec;
3050 di->plug_in_base = runtime_sec;
3052 /*update by current*/
3053 if (di->chrg_status != CHARGE_FINISH) {
3054 di->finish_sig_base = runtime_sec;
3055 di->chrg_finish_base = runtime_sec;
3058 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3059 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3060 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3062 rk81x_bat_dbg_time_table(di);
3065 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3067 int trend_start_cap = di->trend_start_cap;
3068 int remain_cap = di->remain_capacity;
3072 if (di->s2r && !di->slp_psy_status)
3073 di->trend_start_cap = di->remain_capacity;
3075 diff_cap = remain_cap - trend_start_cap;
3076 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3077 __func__, trend_start_cap, diff_cap);
3078 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3080 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3081 di->trend_start_cap = di->remain_capacity;
3082 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3083 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3085 state = TREND_STAT_FLAT;
3091 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3093 int state, soc_time;
3094 static int trend_down_cnt, trend_up_cnt;
3095 int trend_cnt_thresd;
3096 int now_current = di->current_avg;
3099 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3100 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3101 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3102 __func__, state, trend_mult);
3103 if (di->chrg_status == CHARGE_FINISH)
3106 if (state == TREND_STAT_UP) {
3107 rk81x_bat_reset_zero_var(di);
3109 trend_up_cnt += trend_mult;
3110 if (trend_up_cnt >= trend_cnt_thresd) {
3112 di->dischrg_save_sec = 0;
3114 } else if (state == TREND_STAT_DOWN) {
3116 trend_down_cnt += trend_mult;
3117 if (trend_down_cnt >= trend_cnt_thresd) {
3119 di->chrg_save_sec = 0;
3123 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3124 if ((di->chrg_save_sec * 3 / 4 > soc_time) &&
3125 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3127 di->chrg_save_sec = 0;
3129 else if ((di->dischrg_save_sec * 3 / 4 > soc_time) &&
3130 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3132 di->dischrg_save_sec = 0;
3134 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3135 "up_cnt=%d, down_cnt=%d\n",
3136 __func__, state, trend_cnt_thresd, soc_time,
3137 trend_up_cnt, trend_down_cnt);
3140 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3142 u32 *ocv_table = di->pdata->battery_ocv;
3143 int delta_soc = di->rsoc - di->dsoc;
3145 if (di->chrg_status == CHARGE_FINISH ||
3146 di->slp_chrg_status == CHARGE_FINISH) {
3147 /*clear sleep charge status*/
3148 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3149 di->chrg_emu_base = 0;
3150 di->chrg_normal_base = 0;
3151 di->chrg_term_base = 0;
3152 rk81x_bat_finish_chrg(di);
3153 rk81x_bat_capacity_init(di, di->fcc);
3154 rk81x_bat_capacity_init_post(di);
3155 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3156 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3157 (di->voltage < ocv_table[18] + 20))) {
3158 di->chrg_emu_base = 0;
3159 di->chrg_normal_base = 0;
3160 di->chrg_finish_base = 0;
3161 rk81x_bat_terminal_chrg(di);
3162 } else if (di->chrg_status != CHARGE_FINISH &&
3163 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3164 di->chrg_term_base = 0;
3165 di->chrg_normal_base = 0;
3166 di->chrg_finish_base = 0;
3167 rk81x_bat_emulator_chrg(di);
3169 di->chrg_emu_base = 0;
3170 di->chrg_term_base = 0;
3171 di->chrg_finish_base = 0;
3172 rk81x_bat_normal_chrg(di);
3176 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3178 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3179 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3181 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3182 dischrg_normal_sec, dischrg_emu_sec);
3184 return (dischrg_normal_sec > dischrg_emu_sec) ?
3185 dischrg_normal_sec : dischrg_emu_sec;
3188 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3190 unsigned long sec1, sec2;
3191 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3192 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3193 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3194 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3196 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3197 chrg_normal_sec : chrg_term_sec;
3199 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3200 chrg_emu_sec : chrg_finish_sec;
3201 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3202 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3203 chrg_normal_sec, chrg_term_sec,
3204 chrg_emu_sec, chrg_finish_sec);
3206 return (sec1 > sec2) ? sec1 : sec2;
3209 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3211 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3212 if (di->current_mode == DISCHRG_MODE) {
3213 di->current_mode = CHRG_MODE;
3214 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3215 di->dischrg_normal_base = 0;
3216 di->dischrg_emu_base = 0;
3217 if (di->chrg_status == CHARGE_FINISH)
3218 di->dischrg_save_sec = 0;
3219 if ((di->chrg_status == CHARGE_FINISH) &&
3221 di->chrg_save_sec = 0;
3223 DBG("<%s>---dischrg_save_sec = %lu\n",
3224 __func__, di->dischrg_save_sec);
3227 if (!rk81x_chrg_online(di)) {
3228 dev_err(di->dev, "discharge, current error:%d\n",
3231 rk81x_bat_chrg_smooth(di);
3232 di->discharge_smooth_status = true;
3235 if (di->current_mode == CHRG_MODE) {
3236 di->current_mode = DISCHRG_MODE;
3237 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3238 di->chrg_normal_base = 0;
3239 di->chrg_emu_base = 0;
3240 di->chrg_term_base = 0;
3241 di->chrg_finish_base = 0;
3242 DBG("<%s>---chrg_save_sec = %lu\n",
3243 __func__, di->chrg_save_sec);
3245 rk81x_bat_dischrg_smooth(di);
3246 di->charge_smooth_status = true;
3251 * update rsoc by relax voltage
3253 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3255 int relax_vol = di->relax_voltage;
3256 int ocv_soc, capacity;
3258 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3259 capacity = (ocv_soc * di->fcc / 100);
3260 rk81x_bat_capacity_init(di, capacity);
3261 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3262 di->rsoc = rk81x_bat_get_rsoc(di);
3263 rk81x_bat_capacity_init_post(di);
3264 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3268 * 1: must do it, 0: when necessary
3270 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3272 int ocv_vol = di->est_ocv_vol;
3273 int ocv_soc = 0, capacity = 0;
3275 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3276 capacity = (ocv_soc * di->fcc / 100);
3277 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3278 rk81x_bat_capacity_init(di, capacity);
3279 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3280 di->rsoc = rk81x_bat_get_rsoc(di);
3281 rk81x_bat_capacity_init_post(di);
3282 DBG("<%s>, rsoc updated!\n", __func__);
3284 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3285 __func__, ocv_vol, ocv_soc, capacity);
3288 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3292 unsigned long sleep_sec = di->suspend_time_sum;
3293 int power_off_thresd = di->pdata->power_off_thresd;
3295 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3296 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3297 __func__, di->dsoc, di->rsoc,
3298 di->relax_voltage, di->voltage, sleep_sec / 60);
3300 if (di->relax_voltage >= di->voltage) {
3301 rk81x_bat_relax_vol_calib(di);
3302 rk81x_bat_restart_relax(di);
3304 /* current_avg < 0: make sure the system is not
3305 * wakeup by charger plugin.
3307 /* even if relax voltage is not caught rightly, realtime voltage
3308 * is quite close to relax voltage, we should not do nothing after
3312 rk81x_bat_vol_calib(di, 1);
3316 if (di->dsoc <= di->rsoc) {
3317 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3318 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3319 temp_dsoc = di->dsoc - delta_soc;
3321 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3322 di->rsoc, di->dsoc, delta_soc);
3324 if (delta_soc > 0) {
3325 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3327 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3329 else if (temp_dsoc > 5)
3330 di->dsoc = temp_dsoc;
3333 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3334 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3336 /*di->dsoc > di->rsoc*/
3337 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3338 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3339 temp_dsoc = di->dsoc - di->rsoc;
3341 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3342 di->rsoc, di->dsoc, delta_soc);
3344 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3345 (temp_dsoc > delta_soc))
3346 di->dsoc -= delta_soc;
3348 di->dsoc = di->rsoc;
3350 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3351 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3354 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3360 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3361 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3366 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3369 unsigned long sleep_sec;
3371 sleep_sec = di->suspend_time_sum;
3372 if (((di->suspend_charge_current < 800) &&
3373 (di->ac_online == ONLINE)) ||
3374 (di->chrg_status == CHARGE_FINISH)) {
3375 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3376 if (sleep_sec > 0) {
3377 /*default charge current: 1000mA*/
3378 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3379 / 3600 / div(di->fcc);
3382 DBG("<%s>, usb charge\n", __func__);
3389 * only do report when there is a change.
3391 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3392 * when dsoc == 0, we must do report. But it will generate too much android
3393 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3396 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3399 static u32 old_ac_status;
3400 static u32 old_usb_status;
3401 static u32 old_charge_status;
3404 state_changed = false;
3405 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3406 state_changed = true;
3407 else if (di->dsoc != old_soc)
3408 state_changed = true;
3409 else if (di->ac_online != old_ac_status)
3410 state_changed = true;
3411 else if (di->usb_online != old_usb_status)
3412 state_changed = true;
3413 else if (old_charge_status != di->psy_status)
3414 state_changed = true;
3416 if (rk81x_chrg_online(di)) {
3417 if (di->dsoc == 100)
3418 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3420 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3423 if (state_changed) {
3424 power_supply_changed(&di->bat);
3425 power_supply_changed(&di->usb);
3426 power_supply_changed(&di->ac);
3428 old_ac_status = di->ac_online;
3429 old_usb_status = di->usb_online;
3430 old_charge_status = di->psy_status;
3431 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3432 di->dsoc, di->rsoc);
3437 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3441 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3444 return CHRG_CVCC_HOUR[hour];
3447 /* we have to estimate the charging finish time from now, to decide
3448 * whether we should reset the timer or not.
3450 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3453 int remain_capacity;
3455 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3456 if (di->dsoc < di->rsoc)
3457 remain_capacity = di->dsoc * di->fcc / 100;
3459 remain_capacity = di->remain_capacity;
3461 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3462 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3464 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3465 di->chrg_cap2full = di->fcc - remain_capacity;
3466 if (di->current_avg <= 0)
3467 di->current_avg = 1;
3469 di->chrg_time2full = di->chrg_cap2full * 3600 /
3470 div(abs(di->current_avg));
3472 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3473 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3476 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3477 /*rk81x_bat_init_chrg_timer(di);*/
3478 di->plug_in_base = get_runtime_sec();
3479 DBG("%s: reset charge timer\n", __func__);
3486 * in case that we will do reboot stress test, we need a special way
3487 * to ajust the dsoc.
3489 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3497 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3500 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3501 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3503 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3504 __func__, cnt, unit_time, smooth_time,
3505 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3507 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3508 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3509 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3514 rk81x_bat_save_dsoc(di, dsoc);
3517 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3518 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3523 rk81x_bat_save_dsoc(di, dsoc);
3527 rk81x_bat_save_reboot_cnt(di, cnt);
3530 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3535 uint16_t cal_offset;
3536 u8 pcb_offset = DEF_PCB_OFFSET;
3538 min = BASE_TO_MIN(di->power_on_base);
3539 if ((min % 8) && (old_min != min)) {
3541 rk81x_bat_get_vol_offset(di);
3542 if (di->pcb_ioffset_updated)
3543 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3545 current_offset = rk81x_bat_get_ioffset(di);
3546 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3547 cal_offset = rk81x_bat_get_cal_offset(di);
3548 if (cal_offset < 0x7ff)
3549 rk81x_bat_set_cal_offset(di, di->current_offset +
3551 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3552 __func__, di->voltage_k, di->voltage_b, cal_offset,
3553 rk81x_bat_get_ioffset(di));
3557 static void rk81x_bat_update_info(struct rk81x_battery *di)
3561 else if (di->dsoc < 0)
3565 * we need update fcc in continuous charging state, if discharge state
3566 * keep at least 2 hour, we decide not to update fcc, so clear the
3567 * fcc update flag: dod0_status.
3569 if (BASE_TO_MIN(di->plug_out_base) > 120)
3570 di->dod0_status = 0;
3572 di->voltage = rk81x_bat_get_vol(di);
3573 di->current_avg = rk81x_bat_get_avg_current(di);
3574 di->chrg_status = rk81x_bat_get_chrg_status(di);
3575 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3576 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3577 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3578 /*rk81x_bat_chrg_over_time_check(di);*/
3579 rk81x_bat_update_calib_param(di);
3580 if (di->chrg_status == CC_OR_CV)
3581 di->enter_finish = true;
3583 if (!rk81x_chrg_online(di) && di->s2r)
3586 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3587 if (di->remain_capacity > di->fcc) {
3588 rk81x_bat_capacity_init(di, di->fcc);
3589 rk81x_bat_capacity_init_post(di);
3590 di->remain_capacity = di->fcc;
3593 di->rsoc = rk81x_bat_get_rsoc(di);
3596 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3598 if (di->slp_psy_status)
3599 return rk81x_bat_sleep_chrg(di);
3601 return rk81x_bat_sleep_dischrg(di);
3604 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3606 u8 ocv_soc, soc_level;
3607 int relax_vol = di->relax_voltage;
3612 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3613 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3614 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3616 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3617 (di->dod0_status == 0) &&
3619 di->dod0_voltage = relax_vol;
3620 di->dod0_capacity = di->temp_nac;
3625 di->dod0_level = 100;
3626 else if (ocv_soc < 5)
3627 di->dod0_level = 90;
3629 di->dod0_level = 80;
3631 /* save_soc = di->dod0_level; */
3632 soc_level = rk81x_bat_get_level(di);
3633 if (soc_level > di->dod0_level) {
3634 di->dod0_status = 0;
3636 di->dod0_status = 1;
3638 di->fcc_update_sec = get_runtime_sec();
3641 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3642 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3644 di->dod0_voltage, di->dod0_capacity,
3645 ocv_soc, soc_level, di->dod0_status,
3650 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3655 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3658 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3660 dev_info(di->dev, "set charge to %s termination mode\n",
3661 mode ? "digital" : "analog");
3664 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3666 struct rk81x_battery *di;
3668 di = container_of(work, struct rk81x_battery,
3669 chrg_term_mode_switch_work.work);
3671 if (rk81x_chrg_online(di))
3672 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3674 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3677 static void rk81x_battery_work(struct work_struct *work)
3679 struct rk81x_battery *di;
3680 int ms = TIMER_MS_COUNTS;
3682 di = container_of(work, struct rk81x_battery,
3683 battery_monitor_work.work);
3684 if (rk81x_chrg_online(di)) {
3685 rk81x_bat_wait_finish_sig(di);
3686 /*rk81x_bat_chrg_finish_routine(di);*/
3688 rk81x_bat_fcc_flag_check(di);
3689 rk81x_bat_arbitrate_rsoc_trend(di);
3690 rk81x_bat_display_smooth(di);
3691 rk81x_bat_update_time(di);
3692 rk81x_bat_update_info(di);
3693 rk81x_bat_rsoc_check(di);
3694 rk81x_bat_power_supply_changed(di);
3695 rk81x_bat_save_dsoc(di, di->dsoc);
3696 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3698 rk81x_bat_dbg_dmp_info(di);
3700 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3701 ms = 30 * TIMER_MS_COUNTS;
3703 di->early_resume = 0;
3707 queue_delayed_work(di->wq, &di->battery_monitor_work,
3708 msecs_to_jiffies(ms));
3711 #if defined(CONFIG_ARCH_ROCKCHIP)
3712 static void rk81x_battery_otg_delay_work(struct work_struct *work)
3714 struct rk81x_battery *di = container_of(work,
3715 struct rk81x_battery, otg_check_work.work);
3717 enum bc_port_type event = di->charge_otg;
3719 /* do not touch CHRG_CTRL_REG1[7]: CHRG_EN, hardware can
3720 * recognize otg plugin and will auto ajust this bit
3723 case USB_OTG_POWER_ON:
3724 dev_info(di->dev, "charge disable, otg enable\n");
3725 di->otg_online = ONLINE;
3726 /*rk81x_bat_set_charger_param(di, NO_CHARGER);*/
3727 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3728 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3729 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_EN);
3732 case USB_OTG_POWER_OFF:
3733 dev_info(di->dev, "charge enable, otg disable\n");
3734 di->otg_online = OFFLINE;
3735 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3736 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3737 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_DIS);
3738 /*maybe dc still plugin*/
3739 queue_delayed_work(di->wq, &di->dc_det_check_work,
3740 msecs_to_jiffies(10));
3748 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3750 int register_battery_notifier(struct notifier_block *nb)
3752 return blocking_notifier_chain_register(&battery_chain_head, nb);
3754 EXPORT_SYMBOL_GPL(register_battery_notifier);
3756 int unregister_battery_notifier(struct notifier_block *nb)
3758 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3760 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3762 int battery_notifier_call_chain(unsigned long val)
3764 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3765 == NOTIFY_BAD) ? -EINVAL : 0;
3767 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3769 static int rk81x_bat_usb_notifier_call(struct notifier_block *nb,
3770 unsigned long event, void *data)
3772 enum charger_type charger_type;
3773 struct rk81x_battery *di =
3774 container_of(nb, struct rk81x_battery, battery_nb);
3776 if (di->fg_drv_mode == TEST_POWER_MODE)
3779 /*if dc is pluging, ignore usb*/
3780 charger_type = rk81x_bat_get_dc_state(di);
3781 if ((charger_type == DC_CHARGER) &&
3782 (event != USB_OTG_POWER_OFF) &&
3783 (event != USB_OTG_POWER_ON))
3787 case USB_BC_TYPE_DISCNT:/*maybe dc still plugin*/
3788 queue_delayed_work(di->wq, &di->dc_det_check_work,
3789 msecs_to_jiffies(10));
3791 case USB_BC_TYPE_SDP:
3792 case USB_BC_TYPE_CDP:/*nonstandard charger*/
3793 case USB_BC_TYPE_DCP:/*standard charger*/
3794 queue_delayed_work(di->wq, &di->ac_usb_check_work,
3795 msecs_to_jiffies(10));
3797 case USB_OTG_POWER_ON:/*otg on*/
3798 di->charge_otg = USB_OTG_POWER_ON;
3799 queue_delayed_work(di->wq, &di->otg_check_work,
3800 msecs_to_jiffies(10));
3802 case USB_OTG_POWER_OFF:/*otg off*/
3803 di->charge_otg = USB_OTG_POWER_OFF;
3804 queue_delayed_work(di->wq, &di->otg_check_work,
3805 msecs_to_jiffies(10));
3813 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3815 pr_info("\n------- %s:lower power warning!\n", __func__);
3817 rk_send_wakeup_key();
3822 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3824 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3825 rk_send_wakeup_key();
3829 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3831 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3832 rk_send_wakeup_key();
3836 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3838 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3840 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3841 di->finish_sig_base = get_runtime_sec();
3842 rk_send_wakeup_key();
3846 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3848 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3850 queue_delayed_work(di->wq,
3851 &di->dc_det_check_work,
3852 msecs_to_jiffies(10));
3853 rk_send_wakeup_key();
3858 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3863 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3864 ret = sysfs_create_file(&di->bat.dev->kobj,
3865 &rk818_bat_attr[i].attr);
3867 dev_err(di->dev, "create battery node(%s) error\n",
3868 rk818_bat_attr[i].attr.name);
3874 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3876 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3878 struct rk818 *chip = di->rk818;
3880 #if defined(CONFIG_X86_INTEL_SOFIA)
3881 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3882 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3883 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3884 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3886 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3887 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3888 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3889 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3892 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3893 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3895 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3897 di->irq = vb_lo_irq;
3898 enable_irq_wake(di->irq);
3900 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3901 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3904 dev_err(chip->dev, "plug_in_irq request failed!\n");
3906 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3907 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3910 dev_err(chip->dev, "plug_out_irq request failed!\n");
3912 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3913 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3916 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3919 static void rk81x_bat_info_init(struct rk81x_battery *di,
3923 unsigned long time_base = get_runtime_sec();
3925 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3926 if (val & PLUG_IN_STS)
3927 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3929 di->cell.config = di->pdata->cell_cfg;
3930 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3931 di->qmax = di->pdata->cell_cfg->design_qmax;
3932 di->early_resume = 1;
3933 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3934 di->bat_res = di->pdata->sense_resistor_mohm;
3935 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3936 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3937 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3938 di->loader_charged = loader_charged;
3939 di->chrg_finish_base = time_base;
3940 di->power_on_base = time_base;
3941 di->plug_in_base = time_base;
3942 di->plug_out_base = time_base;
3943 di->finish_sig_base = time_base;
3944 di->fcc = rk81x_bat_get_fcc(di);
3947 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3948 struct device_node *np)
3950 struct device *dev = di->dev;
3951 enum of_gpio_flags flags;
3954 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3955 if (di->dc_det_pin == -EPROBE_DEFER) {
3956 dev_err(dev, "dc_det_gpio error\n");
3960 if (gpio_is_valid(di->dc_det_pin)) {
3961 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3962 RK818_DC_IN : RK818_DC_OUT;
3963 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3965 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3966 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3967 "rk81x_dc_det", di);
3969 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3972 enable_irq_wake(di->dc_det_irq);
3977 gpio_free(di->dc_det_pin);
3980 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3985 struct timespec tv = {
3986 .tv_nsec = NSEC_PER_SEC >> 1,
3988 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3990 err = rtc_read_time(rtc, &tm);
3992 dev_err(rtc->dev.parent,
3993 "hctosys: unable to read the hardware clock\n");
3996 err = rtc_valid_tm(&tm);
3998 dev_err(rtc->dev.parent,
3999 "hctosys: invalid date/time\n");
4003 rtc_tm_to_time(&tm, &tv.tv_sec);
4004 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
4006 return (delta_sec > 0) ? delta_sec : 0;
4010 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4012 struct device_node *np;
4013 struct battery_platform_data *pdata;
4014 struct cell_config *cell_cfg;
4015 struct ocv_config *ocv_cfg;
4016 struct property *prop;
4017 struct rk818 *rk818 = di->rk818;
4018 struct device *dev = di->dev;
4023 np = of_find_node_by_name(rk818->dev->of_node, "battery");
4025 dev_err(dev, "battery node not found!\n");
4029 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
4033 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
4037 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4041 prop = of_find_property(np, "ocv_table", &length);
4043 dev_err(dev, "ocv_table not found!\n");
4046 pdata->ocv_size = length / sizeof(u32);
4047 if (pdata->ocv_size <= 0) {
4048 dev_err(dev, "invalid ocv table\n");
4052 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4054 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4055 if (!pdata->battery_ocv)
4058 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4063 /******************** charger param ****************************/
4064 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4066 dev_err(dev, "max_chrg_currentmA not found!\n");
4067 out_value = DEFAULT_CHRG_CUR;
4069 pdata->max_charger_currentmA = out_value;
4071 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4073 dev_err(dev, "max_charger_ilimitmA not found!\n");
4074 out_value = DEFAULT_INPUT_CUR;
4076 pdata->max_charger_ilimitmA = out_value;
4078 ret = of_property_read_u32(np, "bat_res", &out_value);
4080 dev_err(dev, "bat_res not found!\n");
4081 out_value = DEFAULT_BAT_RES;
4083 pdata->sense_resistor_mohm = out_value;
4085 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4087 dev_err(dev, "max_charge_voltagemV not found!\n");
4088 out_value = DEFAULT_CHRG_VOL;
4090 pdata->max_charger_voltagemV = out_value;
4092 ret = of_property_read_u32(np, "design_capacity", &out_value);
4094 dev_err(dev, "design_capacity not found!\n");
4097 cell_cfg->design_capacity = out_value;
4099 ret = of_property_read_u32(np, "design_qmax", &out_value);
4101 dev_err(dev, "design_qmax not found!\n");
4104 cell_cfg->design_qmax = out_value;
4106 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4108 dev_err(dev, "sleep_enter_current not found!\n");
4109 out_value = DEFAULT_SLP_ENTER_CUR;
4111 ocv_cfg->sleep_enter_current = out_value;
4113 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4115 dev_err(dev, "sleep_exit_current not found!\n");
4116 out_value = DEFAULT_SLP_EXIT_CUR;
4118 ocv_cfg->sleep_exit_current = out_value;
4120 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4122 dev_warn(dev, "power_off_thresd not found!\n");
4123 out_value = PWR_OFF_THRESD;
4125 pdata->power_off_thresd = out_value;
4127 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4128 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4129 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4131 /************* charger support adp types **********************/
4132 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4133 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4135 if (!support_usb_adp && !support_dc_adp) {
4136 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4137 support_usb_adp = 1;
4140 /*if (support_dc_adp)*/
4141 rk81x_bat_dc_det_init(di, np);
4143 cell_cfg->ocv = ocv_cfg;
4144 pdata->cell_cfg = cell_cfg;
4147 DBG("\nthe battery dts info dump:\n"
4149 "max_input_currentmA:%d\n"
4150 "max_chrg_currentmA:%d\n"
4151 "max_charge_voltagemV:%d\n"
4152 "design_capacity:%d\n"
4154 "sleep_enter_current:%d\n"
4155 "sleep_exit_current:%d\n"
4156 "support_usb_adp:%d\n"
4157 "support_dc_adp:%d\n"
4158 "power_off_thresd:%d\n",
4159 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4160 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4161 cell_cfg->design_capacity, cell_cfg->design_qmax,
4162 cell_cfg->ocv->sleep_enter_current,
4163 cell_cfg->ocv->sleep_exit_current,
4164 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4170 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4176 static int rk81x_battery_probe(struct platform_device *pdev)
4178 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4179 struct rk81x_battery *di;
4182 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4186 di->dev = &pdev->dev;
4187 platform_set_drvdata(pdev, di);
4189 ret = rk81x_bat_parse_dt(di);
4191 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4195 rk81x_bat_info_init(di, chip);
4196 if (!is_rk81x_bat_exist(di)) {
4197 dev_info(di->dev, "not battery, enter test power mode\n");
4198 di->fg_drv_mode = TEST_POWER_MODE;
4201 ret = rk81x_bat_power_supply_init(di);
4203 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4207 rk81x_bat_irq_init(di);
4208 rk81x_bat_sysfs_init(di);
4210 rk81x_bat_fg_init(di);
4211 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4213 rk81x_bat_flatzone_vol_init(di);
4215 #if defined(CONFIG_X86_INTEL_SOFIA)
4216 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4217 if (IS_ERR_OR_NULL(di->usb_phy)) {
4218 dev_err(di->dev, "get usb phy failed\n");
4219 return PTR_ERR(di->usb_phy);
4221 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4222 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4224 dev_err(di->dev, "registr usb phy notification failed\n");
4225 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4226 rk81x_battery_usb_notifier_delayed_work);
4229 rk81x_battery_register_fb_notify(di);
4230 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4231 "rk81x-battery-work");
4232 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4233 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4234 rk81x_chrg_term_mode_switch_work);
4236 queue_delayed_work(di->wq, &di->battery_monitor_work,
4237 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4239 #if defined(CONFIG_ARCH_ROCKCHIP)
4240 INIT_DELAYED_WORK(&di->otg_check_work,
4241 rk81x_battery_otg_delay_work);
4242 INIT_DELAYED_WORK(&di->ac_usb_check_work,
4243 rk81x_battery_acusb_delay_work);
4244 INIT_DELAYED_WORK(&di->dc_det_check_work,
4245 rk81x_battery_dc_delay_work);
4247 queue_delayed_work(di->wq, &di->dc_det_check_work,
4248 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4250 di->battery_nb.notifier_call = rk81x_bat_usb_notifier_call;
4251 rk_bc_detect_notifier_register(&di->battery_nb, &di->charge_otg);
4253 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4258 static int rk81x_battery_suspend(struct platform_device *dev,
4261 struct rk81x_battery *di = platform_get_drvdata(dev);
4263 /*while otg and dc both plugin*/
4264 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4266 di->slp_psy_status = rk81x_chrg_online(di);
4267 di->chrg_status = rk81x_bat_get_chrg_status(di);
4268 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4269 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4270 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4271 di->dischrg_normal_base = 0;
4272 di->dischrg_emu_base = 0;
4273 do_gettimeofday(&di->suspend_rtc_base);
4275 if (!rk81x_chrg_online(di)) {
4276 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4277 di->chrg_normal_base = 0;
4278 di->chrg_emu_base = 0;
4279 di->chrg_term_base = 0;
4280 di->chrg_finish_base = 0;
4285 * do not modify the g_base_sec
4287 g_base_sec = get_runtime_sec();
4289 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4290 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4291 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4296 static int rk81x_battery_resume(struct platform_device *dev)
4298 struct rk81x_battery *di = platform_get_drvdata(dev);
4299 int pwroff_thresd = di->pdata->power_off_thresd;
4305 /*while otg and dc both plugin*/
4306 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4308 di->discharge_smooth_status = true;
4309 di->charge_smooth_status = true;
4311 vol = rk81x_bat_get_vol(di);
4312 if (vol < INVALID_VOL_THRESD) {
4313 dev_err(di->dev, "invalid voltage :%d", vol);
4318 di->current_avg = rk81x_bat_get_avg_current(di);
4319 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4320 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4321 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4322 delta_time = rk81x_bat_get_suspend_sec(di);
4323 di->suspend_time_sum += delta_time;
4324 #if defined(CONFIG_ARCH_ROCKCHIP)
4325 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4328 if (di->slp_psy_status) {
4329 time_step = CHRG_TIME_STEP;
4331 if (di->voltage <= pwroff_thresd + 50)
4332 time_step = DISCHRG_TIME_STEP_0;
4334 time_step = DISCHRG_TIME_STEP_1;
4337 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4338 di->current_avg, di->voltage, di->est_ocv_vol,
4339 di->relax_voltage, delta_time, di->suspend_time_sum,
4340 di->slp_psy_status);
4342 if (di->suspend_time_sum > time_step) {
4343 delta_soc = rk81x_bat_update_resume_state(di);
4345 di->suspend_time_sum = 0;
4348 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4349 rk81x_chrg_online(di))
4350 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4353 * do not modify the g_base_sec
4355 if (is_local_clock_reset())
4356 g_base_sec += delta_time;
4363 static int rk81x_battery_remove(struct platform_device *dev)
4365 struct rk81x_battery *di = platform_get_drvdata(dev);
4367 cancel_delayed_work_sync(&di->battery_monitor_work);
4371 static void rk81x_battery_shutdown(struct platform_device *dev)
4373 struct rk81x_battery *di = platform_get_drvdata(dev);
4375 cancel_delayed_work_sync(&di->battery_monitor_work);
4376 rk_bc_detect_notifier_unregister(&di->battery_nb);
4378 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4379 rk81x_bat_check_reboot(di);
4381 rk81x_bat_save_reboot_cnt(di, 0);
4382 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4385 static struct platform_driver rk81x_battery_driver = {
4387 .name = "rk818-battery",
4388 .owner = THIS_MODULE,
4391 .probe = rk81x_battery_probe,
4392 .remove = rk81x_battery_remove,
4393 .suspend = rk81x_battery_suspend,
4394 .resume = rk81x_battery_resume,
4395 .shutdown = rk81x_battery_shutdown,
4398 static int __init battery_init(void)
4400 return platform_driver_register(&rk81x_battery_driver);
4403 fs_initcall_sync(battery_init);
4404 static void __exit battery_exit(void)
4406 platform_driver_unregister(&rk81x_battery_driver);
4408 module_exit(battery_exit);
4410 MODULE_LICENSE("GPL");
4411 MODULE_ALIAS("platform:rk818-battery");
4412 MODULE_AUTHOR("ROCKCHIP");