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 10 /*0.01%*/
161 #define MAX_FCC 10000
164 * the following table value depends on datasheet
166 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
168 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
169 2250, 2400, 2600, 2800, 3000};
171 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
172 2000, 2250, 2500, 2750, 3000};
174 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
176 #define RK818_DC_IN 0
177 #define RK818_DC_OUT 1
179 #define OCV_VALID_SHIFT (0)
180 #define OCV_CALIB_SHIFT (1)
181 #define FIRST_PWRON_SHIFT (2)
183 #define SEC_TO_MIN(x) ((x) / 60)
185 struct rk81x_battery {
187 struct cell_state cell;
188 struct power_supply bat;
189 struct power_supply ac;
190 struct power_supply usb;
191 struct delayed_work work;
193 struct pinctrl *pinctrl;
194 struct pinctrl_state *pins_default;
196 struct battery_platform_data *pdata;
209 uint16_t voltage_ocv;
210 uint16_t relax_voltage;
216 bool pcb_ioffset_updated;
233 int bat_res_update_cnt;
239 unsigned long dod0_time;
252 int current_k;/* (ICALIB0, ICALIB1) */
255 int voltage_k;/* VCALIB0 VCALIB1 */
258 int zero_timeout_cnt;
259 int zero_old_remain_cap;
264 int charge_smooth_time;
268 unsigned long suspend_time_sum;
272 int suspend_charge_current;
275 bool charge_smooth_status;
276 bool discharge_smooth_status;
282 struct notifier_block battery_nb;
283 struct usb_phy *usb_phy;
284 struct notifier_block usb_nb;
285 struct notifier_block fb_nb;
288 int s2r; /*suspend to resume*/
289 struct workqueue_struct *wq;
290 struct delayed_work battery_monitor_work;
291 struct delayed_work charge_check_work;
292 struct delayed_work usb_phy_delay_work;
293 struct delayed_work chrg_term_mode_switch_work;
294 enum bc_port_type charge_otg;
297 struct wake_lock resume_wake_lock;
298 unsigned long plug_in_base;
299 unsigned long plug_out_base;
300 unsigned long finish_sig_base;
301 unsigned long power_on_base;
306 bool is_first_poweron;
309 int debug_finish_real_soc;
310 int debug_finish_temp_soc;
316 unsigned long wakeup_sec;
317 u32 delta_vol_smooth;
318 unsigned long dischrg_normal_base;
319 unsigned long dischrg_emu_base;
320 unsigned long chrg_normal_base;
321 unsigned long chrg_term_base;
322 unsigned long chrg_emu_base;
323 unsigned long chrg_finish_base;
324 unsigned long fcc_update_sec;
326 u8 dischrg_algorithm_mode;
327 int last_zero_mode_dsoc;
329 unsigned long dischrg_save_sec;
330 unsigned long chrg_save_sec;
331 struct timeval suspend_rtc_base;
334 u32 support_usb_adp, support_dc_adp;
336 #define to_device_info(x) container_of((x), \
337 struct rk81x_battery, bat)
339 #define to_ac_device_info(x) container_of((x), \
340 struct rk81x_battery, ac)
342 #define to_usb_device_info(x) container_of((x), \
343 struct rk81x_battery, usb)
345 static int loader_charged;
347 static int __init rk81x_bat_loader_charged(char *__unused)
351 pr_info("battery charged in loader\n");
355 __setup("loader_charged", rk81x_bat_loader_charged);
357 static u64 g_base_sec;
358 static u64 get_runtime_sec(void)
360 u64 ts_ns = local_clock();
362 do_div(ts_ns, 1000000000);
364 return ts_ns + g_base_sec;
367 static u64 is_local_clock_reset(void)
369 u64 ts_ns = local_clock();
371 do_div(ts_ns, 1000000000);
376 static inline unsigned long BASE_TO_SEC(unsigned long x)
379 return get_runtime_sec() - x;
384 static inline unsigned long BASE_TO_MIN(unsigned long x)
386 return BASE_TO_SEC(x) / 60;
389 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
394 case HW_ADP_TYPE_USB:
402 case HW_ADP_TYPE_DUAL:
403 if (support_usb_adp && support_dc_adp)
413 static bool rk81x_chrg_online(struct rk81x_battery *di)
415 return di->usb_online || di->ac_online;
418 static u32 interpolate(int value, u32 *table, int size)
423 for (i = 0; i < size; i++) {
424 if (value < table[i])
428 if ((i > 0) && (i < size)) {
429 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
430 d /= table[i] - table[i-1];
431 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
433 d = i * ((INTERPOLATE_MAX + size / 2) / size);
442 /* Returns (a * b) / c */
443 static int32_t ab_div_c(u32 a, u32 b, u32 c)
449 sign = ((((a^b)^c) & 0x80000000) != 0);
455 tmp = (a * b + (c >> 1)) / c;
467 static int div(int val)
469 return (val == 0) ? 1 : val;
472 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
473 u8 buf[], unsigned len)
478 for (i = 0; ret < 0 && i < 3; i++) {
479 ret = rk818_i2c_read(di->rk818, reg, len, buf);
481 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
484 return (ret < 0) ? ret : 0;
487 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
488 u8 const buf[], unsigned len)
493 for (i = 0; ret < 0 && i < 3; i++) {
494 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
496 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
499 return (ret < 0) ? ret : 0;
502 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
507 for (i = 0; ret < 0 && i < 3; i++) {
508 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
510 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
516 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
521 for (i = 0; ret < 0 && i < 3; i++) {
522 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
524 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
530 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
535 rk81x_bat_read(di, reg, &buf, 1);
536 val = (buf & BIT(shift)) >> shift;
540 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
545 DBG("%s dump charger register start:\n", __func__);
546 for (i = 0xAC; i < 0xEE; i++) {
547 rk81x_bat_read(di, i, &buf, 1);
548 DBG("0x%02x : 0x%02x\n", i, buf);
553 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
558 DBG("%s dump the register start:\n", __func__);
559 for (i = 0x99; i < 0xAB; i++) {
560 rk81x_bat_read(di, i, &buf, 1);
561 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
566 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
568 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
569 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
572 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
574 rk81x_bat_reset_zero_var(di);
575 di->trend_start_cap = di->remain_capacity;
578 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
584 delta_cap = capacity - di->remain_capacity;
588 di->adjust_cap += delta_cap;
590 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
592 buf = (capacity_ma >> 24) & 0xff;
593 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
594 buf = (capacity_ma >> 16) & 0xff;
595 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
596 buf = (capacity_ma >> 8) & 0xff;
597 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
598 buf = (capacity_ma & 0xff) | 0x01;
599 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
600 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
604 dev_dbg(di->dev, "update capacity :%d--remain_cap:%d\n",
605 capacity, di->remain_capacity);
610 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
612 static ssize_t bat_calib_read(struct device *dev,
613 struct device_attribute *attr, char *buf)
615 struct power_supply *psy_bat = dev_get_drvdata(dev);
616 struct rk81x_battery *di = to_device_info(psy_bat);
619 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
621 return sprintf(buf, "%d\n", val);
624 static ssize_t bat_calib_write(struct device *dev,
625 struct device_attribute *attr,
626 const char *buf, size_t count)
630 struct power_supply *psy_bat = dev_get_drvdata(dev);
631 struct rk81x_battery *di = to_device_info(psy_bat);
633 ret = kstrtou8(buf, 0, &val);
638 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
640 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
645 * interface for debug: force battery to over discharge
647 static ssize_t bat_test_power_read(struct device *dev,
648 struct device_attribute *attr, char *buf)
650 struct power_supply *psy_bat = dev_get_drvdata(dev);
651 struct rk81x_battery *di = to_device_info(psy_bat);
653 return sprintf(buf, "%d\n", di->fg_drv_mode);
656 static ssize_t bat_test_power_write(struct device *dev,
657 struct device_attribute *attr,
658 const char *buf, size_t count)
662 struct power_supply *psy_bat = dev_get_drvdata(dev);
663 struct rk81x_battery *di = to_device_info(psy_bat);
665 ret = kstrtou8(buf, 0, &val);
670 di->fg_drv_mode = TEST_POWER_MODE;
672 di->fg_drv_mode = FG_NORMAL_MODE;
677 static ssize_t bat_fcc_read(struct device *dev,
678 struct device_attribute *attr, char *buf)
680 struct power_supply *psy_bat = dev_get_drvdata(dev);
681 struct rk81x_battery *di = to_device_info(psy_bat);
683 return sprintf(buf, "%d\n", di->fcc);
686 static ssize_t bat_fcc_write(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
692 struct power_supply *psy_bat = dev_get_drvdata(dev);
693 struct rk81x_battery *di = to_device_info(psy_bat);
695 ret = kstrtou16(buf, 0, &val);
704 static ssize_t bat_dsoc_read(struct device *dev,
705 struct device_attribute *attr, char *buf)
707 struct power_supply *psy_bat = dev_get_drvdata(dev);
708 struct rk81x_battery *di = to_device_info(psy_bat);
710 return sprintf(buf, "%d\n", di->dsoc);
713 static ssize_t bat_dsoc_write(struct device *dev,
714 struct device_attribute *attr,
715 const char *buf, size_t count)
719 struct power_supply *psy_bat = dev_get_drvdata(dev);
720 struct rk81x_battery *di = to_device_info(psy_bat);
722 ret = kstrtou8(buf, 0, &val);
731 static ssize_t bat_rsoc_read(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct power_supply *psy_bat = dev_get_drvdata(dev);
735 struct rk81x_battery *di = to_device_info(psy_bat);
737 return sprintf(buf, "%d\n", di->rsoc);
740 static ssize_t bat_rsoc_write(struct device *dev,
741 struct device_attribute *attr,
742 const char *buf, size_t count)
747 struct power_supply *psy_bat = dev_get_drvdata(dev);
748 struct rk81x_battery *di = to_device_info(psy_bat);
750 ret = kstrtou8(buf, 0, &val);
754 capacity = di->fcc * val / 100;
755 rk81x_bat_capacity_init(di, capacity);
756 rk81x_bat_capacity_init_post(di);
761 static ssize_t bat_remain_cap_read(struct device *dev,
762 struct device_attribute *attr,
765 struct power_supply *psy_bat = dev_get_drvdata(dev);
766 struct rk81x_battery *di = to_device_info(psy_bat);
768 return sprintf(buf, "%d\n", di->remain_capacity);
771 static struct device_attribute rk818_bat_attr[] = {
772 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
773 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
774 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
775 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
776 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
777 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
781 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
786 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
788 dev_err(di->dev, "error reading TS_CTRL_REG");
793 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
798 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
800 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
803 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
807 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
812 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
818 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
820 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
823 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
827 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
833 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
835 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
838 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
842 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
848 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
850 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
856 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
862 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
864 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
870 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
876 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
877 buf = (value >> 8) & 0xff;
878 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
883 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
885 int vcalib0, vcalib1;
887 vcalib0 = rk81x_bat_get_vcalib0(di);
888 vcalib1 = rk81x_bat_get_vcalib1(di);
890 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
891 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
892 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
895 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
900 uint16_t voltage_now = 0;
904 for (i = 0; i < 3; i++) {
905 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
907 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
911 dev_err(di->dev, "error read BAT_OCV_REGH");
916 if (val[0] == val[1])
921 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
926 static int rk81x_bat_get_vol(struct rk81x_battery *di)
935 for (i = 0; i < 3; i++) {
936 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
938 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
942 dev_err(di->dev, "error read BAT_VOL_REGH");
947 if (val[0] == val[1])
952 vol = di->voltage_k * temp / 1000 + di->voltage_b;
957 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
962 ret = rk81x_bat_read(di, GGSTS, &status, 1);
964 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
970 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
974 uint16_t temp = 0, voltage_now;
976 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
978 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
981 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
986 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
990 uint16_t temp = 0, voltage_now;
992 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
994 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
997 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
1002 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1006 uint16_t relax_vol1, relax_vol2;
1009 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1010 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1012 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1013 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1014 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1015 __func__, status, ggcon, relax_vol1, relax_vol2);
1017 if (is_rk81x_bat_relax_mode(di))
1018 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1024 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1025 * based on the voltage.
1027 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1034 ocv_table = di->pdata->battery_ocv;
1035 ocv_size = di->pdata->ocv_size;
1036 tmp = interpolate(voltage, ocv_table, ocv_size);
1037 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1038 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1043 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1049 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1051 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1055 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1057 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1063 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1073 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1077 rk81x_bat_read(di, GGCON, &ggcon, 1);
1078 ggcon &= ~(0x30); /*clear <5:4>*/
1080 rk81x_bat_write(di, GGCON, &ggcon, 1);
1084 * when charger finish signal comes, we need calibrate the current, make it
1087 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1097 if ((di->chrg_status == CHARGE_FINISH) &&
1098 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1099 (abs(di->current_avg) > 4)) {
1100 for (retry = 0; retry < 5; retry++) {
1101 adc_value = rk81x_bat_get_raw_adc_current(di);
1102 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1103 dev_warn(di->dev, "charger plugout\n");
1108 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1109 C0 = rk81x_bat_get_cal_offset(di);
1110 C1 = adc_value + C0;
1111 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1113 rk81x_bat_set_cal_offset(di, C1);
1114 DBG("<%s>. new cal_offset = %d\n",
1115 __func__, rk81x_bat_get_cal_offset(di));
1117 adc_value = rk81x_bat_get_raw_adc_current(di);
1118 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1119 if (abs(adc_value) < 4) {
1120 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1121 ioffset = rk81x_bat_get_ioffset(di);
1122 rk81x_bat_set_cal_offset(di,
1125 ioffset = rk81x_bat_get_ioffset(di);
1126 pcb_offset = C1 - ioffset;
1127 di->pcb_ioffset = pcb_offset;
1128 di->pcb_ioffset_updated = true;
1133 DBG("<%s>. update the cal_offset, C1 = %d\n"
1134 "i_offset = %d, pcb_offset = %d\n",
1135 __func__, C1, ioffset, pcb_offset);
1139 dev_dbg(di->dev, "ioffset cal failed\n");
1140 rk81x_bat_set_cal_offset(di, C0);
1143 di->pcb_ioffset_updated = false;
1150 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1153 int enter_thres, exit_thres;
1154 struct cell_state *cell = &di->cell;
1156 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1157 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1158 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1159 __func__, cell->config->ocv->sleep_enter_current,
1160 cell->config->ocv->sleep_exit_current);
1162 buf = enter_thres & 0xff;
1163 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1164 buf = (enter_thres >> 8) & 0xff;
1165 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1167 buf = exit_thres & 0xff;
1168 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1169 buf = (exit_thres >> 8) & 0xff;
1170 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1172 /* set sample time */
1173 rk81x_bat_read(di, GGCON, &buf, 1);
1174 buf &= ~(3 << 2);/*8min*/
1175 buf &= ~0x01; /* clear bat_res calc*/
1176 rk81x_bat_write(di, GGCON, &buf, 1);
1179 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1184 rk81x_bat_read(di, GGCON, &ggcon, 1);
1186 rk81x_bat_write(di, GGCON, &ggcon, 1);
1188 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1190 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1193 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1202 for (i = 0; i < 3; i++) {
1203 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1205 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1210 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1212 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1218 if (val[0] == val[1])
1219 current_now = val[0];
1221 current_now = val[2];
1223 if (current_now & 0x800)
1224 current_now -= 4096;
1226 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1231 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1232 enum charger_type charger_type)
1234 di->usb_online = OFFLINE;
1235 di->ac_online = OFFLINE;
1237 switch (charger_type) {
1239 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1242 di->usb_online = ONLINE;
1243 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1245 case DC_CHARGER:/*treat dc as ac*/
1247 di->ac_online = ONLINE;
1248 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1251 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1255 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1256 msecs_to_jiffies(1000));
1259 /* high load: current < 0 with charger in.
1260 * System will not shutdown while dsoc=0% with charging state(ac_online),
1261 * which will cause over discharge, so oppose status before report states.
1263 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1266 int pwr_off_thresd = di->pdata->power_off_thresd - 50;
1268 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1270 time = get_runtime_sec();
1272 if (BASE_TO_SEC(time) > (MINUTE)) {
1273 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1274 dev_info(di->dev, "low power....\n");
1281 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1285 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1287 return (buf & 0x80) ? 1 : 0;
1290 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1295 rk81x_bat_read(di, GGSTS, &buf, 1);
1296 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1297 /*di->pwron_bat_con = buf;*/
1301 rk81x_bat_write(di, GGSTS, &buf, 1);
1302 rk81x_bat_read(di, GGSTS, &temp, 1);
1303 } while (temp & BAT_CON);
1310 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1317 ocv_table = di->pdata->battery_ocv;
1318 ocv_size = di->pdata->ocv_size;
1320 for (j = 0; j < 21; j++)
1324 for (i = 1; i < ocv_size-1; i++) {
1325 if (ocv_table[i+1] < ocv_table[i] + 20)
1326 temp_table[j++] = i;
1329 temp_table[j] = temp_table[j-1] + 1;
1331 di->enter_flatzone = ocv_table[i];
1334 for (i = 0; i < 20; i++) {
1335 if (temp_table[i] < temp_table[i+1])
1340 di->exit_flatzone = ocv_table[i];
1342 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1343 di->enter_flatzone, di->exit_flatzone);
1346 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1348 u8 ocv_valid, first_pwron;
1352 /*buf==1: OCV_VOL is valid*/
1353 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1354 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1355 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1357 if (first_pwron == 1 || ocv_valid == 1) {
1358 DBG("<%s> enter.\n", __func__);
1359 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1360 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1361 di->dod0_voltage = ocv_voltage;
1362 di->dod0_capacity = di->temp_nac;
1367 di->dod0_level = 100;
1368 else if (ocv_soc < 5)
1369 di->dod0_level = 95;
1370 else if (ocv_soc < 10)
1371 di->dod0_level = 90;
1373 di->dod0_level = 80;
1374 /* save_soc = di->dod0_level; */
1375 soc_level = rk81x_bat_get_level(di);
1376 if (soc_level > di->dod0_level) {
1377 di->dod0_status = 0;
1379 if (soc_level <= 80)
1381 rk81x_bat_save_level(di, soc_level);
1383 di->dod0_status = 1;
1385 di->fcc_update_sec = get_runtime_sec();
1388 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1389 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1391 di->dod0_voltage, di->dod0_capacity,
1392 ocv_soc, soc_level, di->dod0_status,
1398 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1400 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1403 static enum power_supply_property rk_battery_props[] = {
1404 POWER_SUPPLY_PROP_STATUS,
1405 POWER_SUPPLY_PROP_CURRENT_NOW,
1406 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1407 POWER_SUPPLY_PROP_PRESENT,
1408 POWER_SUPPLY_PROP_HEALTH,
1409 POWER_SUPPLY_PROP_CAPACITY,
1412 static int rk81x_battery_get_property(struct power_supply *psy,
1413 enum power_supply_property psp,
1414 union power_supply_propval *val)
1416 struct rk81x_battery *di = to_device_info(psy);
1419 case POWER_SUPPLY_PROP_CURRENT_NOW:
1420 val->intval = di->current_avg * 1000;/*uA*/
1421 if (di->fg_drv_mode == TEST_POWER_MODE)
1422 val->intval = TEST_CURRENT * 1000;
1424 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1425 val->intval = di->voltage * 1000;/*uV*/
1426 if (di->fg_drv_mode == TEST_POWER_MODE)
1427 val->intval = TEST_VOLTAGE * 1000;
1430 case POWER_SUPPLY_PROP_PRESENT:
1431 val->intval = is_rk81x_bat_exist(di);
1432 if (di->fg_drv_mode == TEST_POWER_MODE)
1433 val->intval = TEST_PRESET;
1436 case POWER_SUPPLY_PROP_CAPACITY:
1437 val->intval = di->dsoc;
1438 if (di->fg_drv_mode == TEST_POWER_MODE)
1439 val->intval = TEST_SOC;
1441 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1443 case POWER_SUPPLY_PROP_HEALTH:
1444 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1446 case POWER_SUPPLY_PROP_STATUS:
1447 val->intval = di->psy_status;
1448 if (di->fg_drv_mode == TEST_POWER_MODE)
1449 val->intval = TEST_STATUS;
1459 static enum power_supply_property rk_battery_ac_props[] = {
1460 POWER_SUPPLY_PROP_ONLINE,
1463 static enum power_supply_property rk_battery_usb_props[] = {
1464 POWER_SUPPLY_PROP_ONLINE,
1467 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1468 enum power_supply_property psp,
1469 union power_supply_propval *val)
1472 struct rk81x_battery *di = to_ac_device_info(psy);
1475 case POWER_SUPPLY_PROP_ONLINE:
1476 if (rk81x_chrg_online(di))
1477 rk81x_bat_lowpwr_check(di);
1478 val->intval = di->ac_online; /*discharging*/
1479 if (di->fg_drv_mode == TEST_POWER_MODE)
1480 val->intval = TEST_AC_ONLINE;
1490 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1491 enum power_supply_property psp,
1492 union power_supply_propval *val)
1495 struct rk81x_battery *di = to_usb_device_info(psy);
1498 case POWER_SUPPLY_PROP_ONLINE:
1499 if (rk81x_chrg_online(di))
1500 rk81x_bat_lowpwr_check(di);
1501 val->intval = di->usb_online;
1502 if (di->fg_drv_mode == TEST_POWER_MODE)
1503 val->intval = TEST_USB_ONLINE;
1513 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1517 di->bat.name = "BATTERY";
1518 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1519 di->bat.properties = rk_battery_props;
1520 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1521 di->bat.get_property = rk81x_battery_get_property;
1524 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1525 di->ac.properties = rk_battery_ac_props;
1526 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1527 di->ac.get_property = rk81x_battery_ac_get_property;
1529 di->usb.name = "USB";
1530 di->usb.type = POWER_SUPPLY_TYPE_USB;
1531 di->usb.properties = rk_battery_usb_props;
1532 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1533 di->usb.get_property = rk81x_battery_usb_get_property;
1535 ret = power_supply_register(di->dev, &di->bat);
1537 dev_err(di->dev, "failed to register main battery\n");
1540 ret = power_supply_register(di->dev, &di->usb);
1542 dev_err(di->dev, "failed to register usb power supply\n");
1545 ret = power_supply_register(di->dev, &di->ac);
1547 dev_err(di->dev, "failed to register ac power supply\n");
1554 power_supply_unregister(&di->ac);
1556 power_supply_unregister(&di->usb);
1558 power_supply_unregister(&di->bat);
1563 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1567 static u32 capacity_ma;
1569 if (capacity >= di->qmax)
1570 capacity = di->qmax;
1575 if (capacity_ma == capacity)
1578 capacity_ma = capacity;
1580 buf = (capacity_ma >> 24) & 0xff;
1581 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1582 buf = (capacity_ma >> 16) & 0xff;
1583 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1584 buf = (capacity_ma >> 8) & 0xff;
1585 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1586 buf = (capacity_ma & 0xff) | 0x01;
1587 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1590 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1598 for (i = 0; i < 3; i++) {
1599 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1601 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1602 val[i] |= buf << 16;
1603 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1605 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1609 if (val[0] == val[1])
1617 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1622 capacity_ma = capacity;
1623 buf = (capacity_ma >> 24) & 0xff;
1624 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1625 buf = (capacity_ma >> 16) & 0xff;
1626 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1627 buf = (capacity_ma >> 8) & 0xff;
1628 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1629 buf = (capacity_ma & 0xff) | 0x01;
1630 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1632 dev_info(di->dev, "update fcc : %d\n", capacity);
1635 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1640 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1641 capacity = buf << 24;
1642 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1643 capacity |= buf << 16;
1644 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1645 capacity |= buf << 8;
1646 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1649 if (capacity < MIN_FCC) {
1650 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1652 capacity = di->design_capacity;
1653 rk81x_bat_save_fcc(di, capacity);
1654 } else if (capacity > di->qmax) {
1655 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1656 capacity = di->qmax;
1657 rk81x_bat_save_fcc(di, capacity);
1663 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1672 for (i = 0; i < 3; i++) {
1673 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1675 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1676 val[i] |= buf << 16;
1677 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1679 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1682 if (val[0] == val[1])
1687 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1692 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1696 if (last_soc != save_soc) {
1697 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1698 last_soc = save_soc;
1704 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1709 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1713 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1717 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1718 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1719 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1720 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1723 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1724 enum charger_type charger_type)
1726 switch (charger_type) {
1729 rk81x_bat_set_current(di, ILIM_450MA);
1733 rk81x_bat_set_current(di, di->chrg_i_lmt);
1736 rk81x_bat_set_current(di, ILIM_450MA);
1740 #if defined(CONFIG_ARCH_ROCKCHIP)
1742 * There are three ways to detect dc_adp:
1743 * 1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1744 * do not care about whether define dc_det_pin or not;
1745 * 2. define de_det_pin: check gpio level;
1746 * 3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1747 * case that: gpio invalid or not define.
1749 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1751 enum charger_type charger_type;
1755 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1757 /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1758 if (!rk81x_bat_support_adp_type(HW_ADP_TYPE_USB)) {
1759 if ((buf & PLUG_IN_STS) != 0)
1760 charger_type = DC_CHARGER;
1762 charger_type = NO_CHARGER;
1764 return charger_type;
1767 /*det by gpio level*/
1768 if (gpio_is_valid(di->dc_det_pin)) {
1769 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1771 pr_err("Failed to request gpio %d with ret:""%d\n",
1772 di->dc_det_pin, ret);
1776 gpio_direction_input(di->dc_det_pin);
1777 ret = gpio_get_value(di->dc_det_pin);
1778 if (ret == di->dc_det_level)
1779 charger_type = DC_CHARGER;
1781 charger_type = NO_CHARGER;
1783 gpio_free(di->dc_det_pin);
1784 DBG("**********rk818 dc_det_pin=%d\n", ret);
1786 return charger_type;
1788 /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1789 } else if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DUAL)) {
1790 if ((buf & PLUG_IN_STS) != 0) {
1791 charger_type = dwc_otg_check_dpdm(0);
1792 if (charger_type == 0)
1793 charger_type = DC_CHARGER;
1795 charger_type = NO_CHARGER;
1799 return charger_type;
1802 static enum charger_type rk81x_bat_get_usbac_state(struct rk81x_battery *di)
1804 enum charger_type charger_type;
1805 int usb_id, gadget_flag;
1807 usb_id = dwc_otg_check_dpdm(0);
1810 charger_type = NO_CHARGER;
1814 charger_type = USB_CHARGER;
1817 charger_type = AC_CHARGER;
1820 charger_type = NO_CHARGER;
1823 DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1824 if (charger_type == USB_CHARGER) {
1825 gadget_flag = get_gadget_connect_flag();
1826 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1827 __func__, gadget_flag, di->check_count);
1829 if (0 == gadget_flag) {
1830 if (++di->check_count >= 5) {
1831 charger_type = AC_CHARGER;
1832 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1833 __func__, di->check_count);
1835 charger_type = USB_CHARGER;
1838 charger_type = USB_CHARGER;
1839 di->check_count = 0;
1842 di->check_count = 0;
1845 return charger_type;
1849 * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
1850 * and dc_adp are plugined in together, the dc_apt has high priority.
1851 * so we check dc_apt first and return rigth away if it's found.
1853 static enum charger_type rk81x_bat_get_adp_type(struct rk81x_battery *di)
1856 enum charger_type charger_type = NO_CHARGER;
1858 /*check by ic hardware: this check make check work safer*/
1859 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1860 if ((buf & PLUG_IN_STS) == 0)
1864 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_DC)) {
1865 charger_type = rk81x_bat_get_dc_state(di);
1866 if (charger_type == DC_CHARGER)
1867 return charger_type;
1871 charger_type = rk81x_bat_get_usbac_state(di);
1873 return charger_type;
1876 static void rk81x_bat_status_check(struct rk81x_battery *di)
1878 static enum charger_type old_charger_type = DUAL_CHARGER;
1879 enum charger_type charger_type;
1881 charger_type = rk81x_bat_get_adp_type(di);
1882 if (charger_type == old_charger_type)
1884 rk81x_bat_set_chrg_current(di, charger_type);
1885 rk81x_bat_set_power_supply_state(di, charger_type);
1886 old_charger_type = charger_type;
1890 #if defined(CONFIG_X86_INTEL_SOFIA)
1891 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1893 enum charger_type charger_type;
1896 charger_type = AC_CHARGER;
1898 charger_type = USB_CHARGER;
1900 charger_type = NO_CHARGER;
1904 dev_info(di->dev, "limit current:%d\n", ma);
1906 return charger_type;
1909 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1911 struct rk81x_battery *di;
1912 enum charger_type type;
1914 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1915 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1917 rk81x_bat_set_chrg_current(di, type);
1918 power_supply_changed(&di->usb);
1921 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1922 unsigned long event, void *data)
1924 struct rk81x_battery *di;
1925 struct power_supply_cable_props *cable_props;
1926 enum charger_type type;
1928 di = container_of(nb, struct rk81x_battery, usb_nb);
1934 case USB_EVENT_CHARGER:
1935 cable_props = (struct power_supply_cable_props *)data;
1936 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1937 rk81x_bat_set_power_supply_state(di, type);
1938 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1939 msecs_to_jiffies(50));
1950 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1951 unsigned long event, void *data)
1953 struct rk81x_battery *di;
1954 struct fb_event *evdata = data;
1957 di = container_of(nb, struct rk81x_battery, fb_nb);
1959 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1962 blank = *(int *)evdata->data;
1964 if (di->fb_blank != blank)
1965 di->fb_blank = blank;
1969 if (blank == FB_BLANK_UNBLANK)
1970 di->early_resume = 1;
1975 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1977 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1978 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1980 return fb_register_client(&di->fb_nb);
1984 * it is first time for battery to be weld, init by ocv table
1986 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1988 rk81x_bat_save_fcc(di, di->design_capacity);
1989 di->fcc = rk81x_bat_get_fcc(di);
1991 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1992 di->dsoc = di->rsoc;
1993 di->nac = di->temp_nac;
1995 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1996 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1997 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1998 __func__, di->dsoc, di->nac, di->fcc);
2001 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
2006 int relax_vol = di->relax_voltage;
2007 int ocv_vol = di->voltage_ocv;
2009 init_cur = rk81x_bat_get_avg_current(di);
2010 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
2012 est_vol = di->voltage - diff;
2014 if (di->loader_charged) {
2015 calib_vol = est_vol;
2019 if (di->pwroff_min > 8) {
2020 if (abs(relax_vol - ocv_vol) < 100) {
2021 calib_vol = ocv_vol;
2023 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
2024 calib_vol = ocv_vol;
2026 calib_vol = relax_vol;
2028 } else if (di->pwroff_min > 2) {
2029 calib_vol = ocv_vol;
2034 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2035 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2041 * it is not first time for battery to be weld, init by last record info
2043 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2047 int remain_capacity;
2049 int calib_vol, calib_soc, calib_capacity;
2051 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2052 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2053 init_soc = pwron_soc;
2054 remain_capacity = rk81x_bat_get_remain_capacity(di);
2056 /* check if support uboot charge,
2057 * if support, uboot charge driver should have done init work,
2058 * so here we should skip init work
2060 #if defined(CONFIG_ARCH_ROCKCHIP)
2061 if (di->loader_charged)
2064 calib_vol = rk81x_bat_get_calib_vol(di);
2065 if (calib_vol > 0) {
2066 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2067 calib_capacity = di->temp_nac;
2069 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2070 init_soc = calib_soc;
2071 remain_capacity = calib_capacity;
2073 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2074 calib_vol, init_soc, remain_capacity);
2077 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2078 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2079 __func__, remain_capacity, di->temp_nac);
2081 if (di->pwroff_min > 0) {
2082 if (di->pwroff_min > 30) {
2083 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2085 remain_capacity = di->temp_nac;
2086 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2087 __func__, remain_capacity);
2089 } else if ((di->pwroff_min > 5) &&
2090 (abs(ocv_soc - init_soc) >= 10)) {
2091 if (remain_capacity >= di->temp_nac * 120/100)
2092 remain_capacity = di->temp_nac * 110/100;
2093 else if (remain_capacity < di->temp_nac * 8/10)
2094 remain_capacity = di->temp_nac * 9/10;
2095 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2096 __func__, remain_capacity);
2099 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2102 di->dsoc = init_soc;
2103 di->nac = remain_capacity;
2106 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2107 pwron_soc, di->dsoc, di->nac);
2110 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2112 u8 curr_pwroff_min, last_pwroff_min;
2114 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2115 &curr_pwroff_min, 1);
2116 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2117 &last_pwroff_min, 1);
2119 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2120 &curr_pwroff_min, 1);
2122 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2125 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2127 u8 calib_en;/*debug*/
2129 di->voltage = rk81x_bat_get_vol(di);
2130 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2131 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2132 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2133 di->current_avg = rk81x_bat_get_avg_current(di);
2135 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2136 di->voltage, di->voltage_ocv, di->relax_voltage,
2137 di->current_avg, di->pwroff_min);
2139 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2140 DBG("readbit: calib_en=%d\n", calib_en);
2141 if (is_rk81x_bat_first_poweron(di) ||
2142 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2143 rk81x_bat_first_pwron(di);
2144 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2147 rk81x_bat_not_first_pwron(di);
2153 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2158 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2163 DBG(" CHARGE-OFF ...\n");
2167 DBG(" DEAD CHARGE ...\n");
2169 case TRICKLE_CHARGE:
2171 DBG(" TRICKLE CHARGE ...\n ");
2175 DBG(" CC or CV ...\n");
2178 ret = CHARGE_FINISH;
2179 DBG(" CHARGE FINISH ...\n");
2183 DBG(" USB OVER VOL ...\n");
2187 DBG(" BAT TMP ERROR ...\n");
2191 DBG(" TIMER ERROR ...\n");
2195 DBG(" USB EXIST ...\n");
2199 DBG(" USB EFF...\n");
2208 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2209 int chrg_ilim, int chrg_cur)
2213 di->chrg_v_lmt = DEF_CHRG_VOL;
2214 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2215 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2217 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2218 if (chrg_vol < CHRG_V_LMT[i])
2221 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2224 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2225 if (chrg_ilim < CHRG_I_LMT[i])
2228 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2231 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2232 if (chrg_cur < CHRG_I_CUR[i])
2235 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2237 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2238 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2241 static u8 rk81x_bat_select_finish_ma(int fcc)
2243 u8 ma = FINISH_150MA;
2248 else if (fcc >= 4000)
2251 else if (fcc >= 3000)
2261 * there is a timer inside rk81x to calc how long the battery is in charging
2262 * state. rk81x will close PowerPath inside IC when timer reach, which will
2263 * stop the charging work. we have to reset the corresponding bits to restart
2264 * the timer to avoid that case.
2266 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2270 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2271 buf &= ~CHRG_TIMER_CCCV_EN;
2272 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2274 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2275 buf |= CHRG_TIMER_CCCV_EN;
2276 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2277 dev_info(di->dev, "reset cccv charge timer\n");
2281 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2283 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2284 u8 sup_sts_reg, thremal_reg, ggcon;
2285 int chrg_vol, chrg_cur, chrg_ilim;
2288 chrg_vol = di->pdata->max_charger_voltagemV;
2289 chrg_cur = di->pdata->max_charger_currentmA;
2290 chrg_ilim = di->pdata->max_charger_ilimitmA;
2292 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2293 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2295 /*rk81x_bat_init_chrg_timer(di);*/
2297 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2298 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2299 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2300 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2301 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2302 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2303 rk81x_bat_read(di, GGCON, &ggcon, 1);
2305 usb_ctrl_reg &= (~0x0f);
2307 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2308 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2310 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2312 if (di->fg_drv_mode == TEST_POWER_MODE)
2313 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2315 chrg_ctrl_reg1 &= (0x00);
2316 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2318 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2319 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2321 chrg_ctrl_reg2 &= ~(0xc7);
2322 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2324 sup_sts_reg &= ~(0x01 << 3);
2325 sup_sts_reg |= (0x01 << 2);
2327 thremal_reg &= (~0x0c);
2328 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2329 ggcon |= ADC_CURRENT_MODE;
2331 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2332 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2333 /*don't touch charge setting when boot int loader charge mode*/
2334 if (!di->loader_charged)
2335 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2336 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2337 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2338 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2339 rk81x_bat_write(di, GGCON, &ggcon, 1);
2342 void rk81x_charge_disable_open_otg(struct rk81x_battery *di)
2344 enum bc_port_type event = di->charge_otg;
2347 case USB_OTG_POWER_ON:
2348 DBG("charge disable, enable OTG.\n");
2349 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2350 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2353 case USB_OTG_POWER_OFF:
2354 DBG("charge enable, disable OTG.\n");
2355 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2356 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2364 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2371 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2373 rk81x_bat_gauge_enable(di);
2374 /* get the volatege offset */
2375 rk81x_bat_get_vol_offset(di);
2376 rk81x_bat_charger_init(di);
2377 rk81x_bat_set_relax_thres(di);
2379 /* get the current offset , the value write to the CAL_OFFSET */
2380 di->current_offset = rk81x_bat_get_ioffset(di);
2381 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2382 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2383 __func__, pcb_offset, di->current_offset);
2385 pcb_offset = DEF_PCB_OFFSET;
2386 cal_offset = pcb_offset + di->current_offset;
2387 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2388 cal_offset = DEF_CAL_OFFSET;
2389 rk81x_bat_set_cal_offset(di, cal_offset);
2390 /* set sample time for cal_offset interval*/
2391 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2393 rk81x_bat_rsoc_init(di);
2394 rk81x_bat_capacity_init(di, di->nac);
2395 rk81x_bat_capacity_init_post(di);
2397 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2398 di->current_avg = rk81x_bat_get_avg_current(di);
2400 rk81x_bat_restart_relax(di);
2401 rk81x_bat_power_on_save(di, di->voltage_ocv);
2403 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2405 rk81x_dbg_dmp_gauge_regs(di);
2406 rk81x_dbg_dmp_charger_regs(di);
2409 "nac = %d , remain_capacity = %d\n"
2410 "OCV_voltage = %d, voltage = %d\n"
2411 "SOC = %d, fcc = %d\n, current=%d\n"
2412 "cal_offset = 0x%x\n",
2414 di->nac, di->remain_capacity,
2415 di->voltage_ocv, di->voltage,
2416 di->dsoc, di->fcc, di->current_avg,
2420 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2422 int dead_voltage, ocv_voltage;
2423 int voltage, voltage_old, voltage_now;
2428 int ocv_soc, dead_soc;
2429 int power_off_thresd = di->pdata->power_off_thresd;
2432 voltage_old = rk81x_bat_get_vol(di);
2434 voltage_now = rk81x_bat_get_vol(di);
2436 } while ((voltage_old == voltage_now) && (count_num < 11));
2437 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2440 for (i = 0; i < 10; i++) {
2441 voltage += rk81x_bat_get_vol(di);
2446 currentnow = rk81x_bat_get_avg_current(di);
2448 /* 50 mo power-path mos */
2449 dead_voltage = power_off_thresd - currentnow *
2450 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2452 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2453 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2454 dead_voltage, ocv_voltage);
2456 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2457 q_dead = di->temp_nac;
2458 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2461 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2462 q_ocv = di->temp_nac;
2463 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2466 rsoc = ocv_soc - dead_soc;
2467 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2469 } else if (rsoc > 0) {
2470 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2473 di->display_soc = di->dsoc * 1000;
2476 di->zero_old_remain_cap = di->remain_capacity;
2478 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2479 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2480 di->line_k, di->dsoc, rsoc,
2481 di->display_soc, di->zero_old_remain_cap);
2484 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2486 int delta_cap, delta_soc;
2489 di->zero_timeout_cnt++;
2490 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2491 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2493 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2494 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2495 "ZERO1: timeout_cnt=%d\n\n",
2496 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2497 delta_soc, delta_cap, di->zero_old_remain_cap,
2498 di->zero_timeout_cnt);
2500 if ((abs(delta_soc) > MIN_ZERO_ACCURACY) ||
2501 (di->zero_timeout_cnt > 500)) {
2502 DBG("ZERO1:--------- enter calc -----------\n");
2503 di->zero_timeout_cnt = 0;
2504 di->display_soc -= delta_soc;
2505 tmp_dsoc = (di->display_soc + 500) / 1000;
2506 di->dsoc = tmp_dsoc;
2508 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2509 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2511 rk81x_bat_zero_calc_linek(di);
2515 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2517 return (di->voltage -
2518 (di->bat_res * di->current_avg) / 1000);
2521 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2523 int ocv_soc, ocv_voltage;
2525 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2526 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2531 /* we will estimate a ocv voltage to get a ocv soc.
2532 * if there is a big offset between ocv_soc and rsoc,
2533 * we will decide whether we should reinit capacity or not
2535 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2537 int ocv_soc = di->est_ocv_soc;
2538 int ocv_volt = di->est_ocv_vol;
2539 int rsoc = rk81x_bat_get_rsoc(di);
2540 int max_volt = di->pdata->max_charger_voltagemV;
2542 if (ocv_volt > max_volt)
2545 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2546 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2548 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2550 di->err_soc_sum += ocv_soc;
2554 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2555 __func__, di->err_chck_cnt, di->err_soc_sum);
2557 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2558 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2559 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2560 ocv_soc += RSOC_COMPS;
2562 di->temp_nac = ocv_soc * di->fcc / 100;
2563 rk81x_bat_capacity_init(di, di->temp_nac);
2564 rk81x_bat_capacity_init_post(di);
2565 di->rsoc = rk81x_bat_get_rsoc(di);
2566 di->remain_capacity =
2567 rk81x_bat_get_realtime_capacity(di);
2568 di->err_soc_sum = 0;
2569 di->err_chck_cnt = 0;
2570 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2574 di->err_chck_cnt = 0;
2575 di->err_soc_sum = 0;
2579 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2581 u8 status = di->psy_status;
2583 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2584 (status == POWER_SUPPLY_STATUS_FULL)) {
2585 if ((di->current_avg < 0) &&
2586 (di->chrg_status != CHARGE_FINISH))
2587 rk81x_bat_rsoc_dischrg_check(di);
2590 rsoc_chrg_calib(di);
2593 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2594 rk81x_bat_rsoc_dischrg_check(di);
2598 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2600 u32 temp, soc_time = 0;
2601 unsigned long sec_unit;
2603 if (!di->dischrg_emu_base)
2604 di->dischrg_emu_base = get_runtime_sec();
2606 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2608 temp = di->fcc * 3600 / 100;
2610 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2611 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2613 soc_time = temp / div(abs(di->current_avg));
2615 if (sec_unit > soc_time) {
2617 di->dischrg_emu_base = get_runtime_sec();
2618 di->dischrg_save_sec = 0;
2621 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2622 __func__, soc_time, sec_unit);
2626 * when there is a big offset between dsoc and rsoc, dsoc needs to
2627 * speed up to keep pace witch rsoc.
2629 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2631 u32 soc_time = 0, temp;
2633 unsigned long chrg_emu_sec;
2635 if (!di->chrg_emu_base)
2636 di->chrg_emu_base = get_runtime_sec();
2638 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2639 temp = di->fcc * 3600 / 100;
2641 if (di->ac_online) {
2642 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2643 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2645 soc_time = temp / div(abs(di->current_avg));
2647 soc_time = temp / 450;
2650 plus_soc = chrg_emu_sec / soc_time;
2651 if (chrg_emu_sec > soc_time) {
2652 di->dsoc += plus_soc;
2653 di->chrg_emu_base = get_runtime_sec();
2654 di->chrg_save_sec = 0;
2657 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2658 __func__, soc_time, chrg_emu_sec, plus_soc);
2661 /* check voltage and current when dsoc is close to full.
2662 * we will do a fake charge to adjust charing speed which
2663 * aims to make battery full charged and match finish signal.
2665 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2669 unsigned long chrg_term_sec;
2671 if (!di->chrg_term_base)
2672 di->chrg_term_base = get_runtime_sec();
2674 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2675 /*check current and voltage*/
2677 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2679 plus_soc = chrg_term_sec / soc_time;
2680 if (chrg_term_sec > soc_time) {
2681 di->dsoc += plus_soc;
2682 di->chrg_term_base = get_runtime_sec();
2683 di->chrg_save_sec = 0;
2685 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2686 __func__, soc_time, chrg_term_sec, plus_soc);
2689 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2692 int now_current = di->current_avg;
2693 unsigned long dischrg_normal_sec;
2695 if (!di->dischrg_normal_base)
2696 di->dischrg_normal_base = get_runtime_sec();
2698 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2699 di->dischrg_save_sec;
2701 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2702 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2703 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2705 if (di->rsoc == di->dsoc) {
2706 DBG("<%s>. rsoc == dsoc\n", __func__);
2707 di->dsoc = di->rsoc;
2708 di->dischrg_normal_base = get_runtime_sec();
2709 di->dischrg_save_sec = 0;
2710 /*di->discharge_smooth_status = false;*/
2711 } else if (di->rsoc > di->dsoc - 1) {
2712 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2713 if (dischrg_normal_sec > soc_time * 3 / 2) {
2715 di->dischrg_normal_base = get_runtime_sec();
2716 di->dischrg_save_sec = 0;
2718 di->discharge_smooth_status = true;
2720 } else if (di->rsoc < di->dsoc - 1) {
2721 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2722 if (dischrg_normal_sec > soc_time * 3 / 4) {
2724 di->dischrg_normal_base = get_runtime_sec();
2725 di->dischrg_save_sec = 0;
2727 di->discharge_smooth_status = true;
2729 } else if (di->rsoc == di->dsoc - 1) {
2730 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2731 if (di->discharge_smooth_status) {
2732 if (dischrg_normal_sec > soc_time * 3 / 4) {
2734 di->dischrg_normal_base = get_runtime_sec();
2735 di->dischrg_save_sec = 0;
2736 di->discharge_smooth_status = false;
2740 di->dischrg_normal_base = get_runtime_sec();
2741 di->dischrg_save_sec = 0;
2742 di->discharge_smooth_status = false;
2746 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2747 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2748 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2749 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2752 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2756 /* first resume from suspend: we don't run this,
2757 * the sleep_dischrg will handle dsoc, and what
2758 * ever this is fake wakeup or not, we should clean
2759 * zero algorithm mode, or it will handle the dsoc.
2762 rk81x_bat_reset_zero_var(di);
2766 di->rsoc = rk81x_bat_get_rsoc(di);
2768 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2769 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2771 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2772 delta_soc = di->dsoc - di->rsoc;
2774 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2775 di->dischrg_normal_base = 0;
2776 rk81x_bat_emulator_dischrg(di);
2778 di->chrg_emu_base = 0;
2779 rk81x_bat_normal_dischrg(di);
2782 if (di->voltage < ZERO_ALGOR_THRESD) {
2783 di->dischrg_normal_base = 0;
2784 di->chrg_emu_base = 0;
2785 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2786 di->zero_timeout_cnt = 0;
2788 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2789 __func__, di->dsoc, di->last_zero_mode_dsoc);
2790 if (di->dsoc != di->last_zero_mode_dsoc) {
2791 di->display_soc = di->dsoc * 1000;
2792 di->last_zero_mode_dsoc = di->dsoc;
2793 rk81x_bat_zero_calc_linek(di);
2794 DBG("<%s>. first calc, init linek\n", __func__);
2798 rk81x_bat_zero_algorithm(di);
2800 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2801 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2802 di->zero_timeout_cnt = 0;
2803 DBG("<%s>. exit zero_algorithm\n", __func__);
2808 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2811 static int old_index;
2814 int mod = di->dsoc % 10;
2815 int index = di->dsoc / 10;
2817 if (rk81x_chrg_online(di))
2818 time = di->plug_in_min;
2820 time = di->plug_out_min;
2822 if ((mod == 0) && (index > 0) && (old_index != index)) {
2823 di->chrg_min[index-1] = time - old_min;
2828 for (i = 1; i < 11; i++)
2829 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2833 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2835 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2836 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2837 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2839 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2840 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2841 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2842 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2843 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2844 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2845 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2846 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2847 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2848 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2849 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2851 DBG("\n------------- dump_debug_regs -----------------\n"
2852 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2853 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2854 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2855 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2856 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2857 ggcon_reg, ggsts_reg, rtc_val,
2858 sup_tst_reg, vb_mod_reg,
2859 usb_ctrl_reg, chrg_ctrl_reg1,
2860 thremal_reg, misc_reg,
2861 chrg_ctrl_reg2, chrg_ctrl_reg3
2864 DBG("#######################################################\n"
2865 "voltage = %d, current-avg = %d\n"
2866 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2867 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2868 "display_soc = %d, cpapacity_soc = %d\n"
2869 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2870 "finish_real_soc = %d, finish_temp_soc = %d\n"
2871 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2872 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2873 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2874 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2875 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2876 "#########################################################\n",
2877 di->voltage, di->current_avg,
2878 di->fcc, di->remain_capacity, di->voltage_ocv,
2879 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2881 di->ac_online, di->usb_online, di->psy_status,
2882 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2883 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2884 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2885 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2886 BASE_TO_SEC(di->chrg_normal_base),
2887 BASE_TO_SEC(di->dischrg_normal_base),
2888 BASE_TO_SEC(di->chrg_term_base),
2889 BASE_TO_SEC(di->chrg_emu_base),
2890 BASE_TO_SEC(di->dischrg_emu_base),
2891 BASE_TO_SEC(di->power_on_base), g_base_sec,
2892 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2897 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2901 int dod0_to_soc100_min;
2903 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2904 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2906 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2907 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2908 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2909 di->dod0_capacity, dod0_to_soc100_min);
2911 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2912 (dod0_to_soc100_min < 1200)) {
2913 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2914 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2916 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2918 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2919 __func__, fcc0, di->fcc);
2921 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2922 di->dod0_status = 0;
2924 rk81x_bat_capacity_init(di, di->fcc);
2925 rk81x_bat_capacity_init_post(di);
2926 rk81x_bat_save_fcc(di, di->fcc);
2927 rk81x_bat_save_level(di, di->dod0_level);
2928 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2931 di->dod0_status = 0;
2935 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2937 if (di->chrg_status == CHARGE_FINISH) {
2938 di->debug_finish_real_soc = di->dsoc;
2939 di->debug_finish_temp_soc = di->rsoc;
2943 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2945 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2948 if ((di->chrg_status == CHARGE_FINISH) &&
2949 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2950 rk81x_bat_update_fcc(di);/* save new fcc*/
2951 ret = rk81x_bat_zero_current_calib(di);
2953 di->enter_finish = false;
2955 rk81x_bat_dbg_get_finish_soc(di);
2959 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2961 unsigned long sec_finish;
2962 int soc_time = 0, plus_soc;
2965 if (di->dsoc < 100) {
2966 if (!di->chrg_finish_base)
2967 di->chrg_finish_base = get_runtime_sec();
2969 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2971 temp = di->fcc * 3600 / 100;
2973 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2975 soc_time = temp / 450;
2977 plus_soc = sec_finish / soc_time;
2978 if (sec_finish > soc_time) {
2979 di->dsoc += plus_soc;
2980 di->chrg_finish_base = get_runtime_sec();
2981 di->chrg_save_sec = 0;
2983 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2984 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2985 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2989 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2992 u32 soc_time, unit_sec;
2994 unsigned long chrg_normal_sec;
2996 now_current = rk81x_bat_get_avg_current(di);
2997 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2999 if (!di->chrg_normal_base)
3000 di->chrg_normal_base = get_runtime_sec();
3002 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
3003 di->rsoc = rk81x_bat_get_rsoc(di);
3005 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
3006 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
3008 if (di->dsoc == di->rsoc) {
3009 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3010 di->rsoc = rk81x_bat_get_rsoc(di);
3011 di->chrg_normal_base = get_runtime_sec();
3012 di->chrg_save_sec = 0;
3013 /*di->charge_smooth_status = false;*/
3014 } else if (di->rsoc < di->dsoc + 1) {
3015 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
3016 unit_sec = soc_time * 3 / 2;
3017 plus_soc = chrg_normal_sec / unit_sec;
3018 if (chrg_normal_sec > unit_sec) {
3019 di->dsoc += plus_soc;
3020 di->chrg_normal_base = get_runtime_sec();
3021 di->chrg_save_sec = 0;
3023 di->charge_smooth_status = true;
3024 } else if (di->rsoc > di->dsoc + 1) {
3025 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
3026 unit_sec = soc_time * 3 / 4;
3027 plus_soc = chrg_normal_sec / unit_sec;
3028 if (chrg_normal_sec > unit_sec) {
3029 di->dsoc += plus_soc;
3030 di->chrg_normal_base = get_runtime_sec();
3031 di->chrg_save_sec = 0;
3033 di->charge_smooth_status = true;
3034 } else if (di->rsoc == di->dsoc + 1) {
3035 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3036 if (di->charge_smooth_status) {
3037 unit_sec = soc_time * 3 / 4;
3038 if (chrg_normal_sec > unit_sec) {
3039 di->dsoc = di->rsoc;
3040 di->chrg_normal_base = get_runtime_sec();
3041 di->charge_smooth_status = false;
3042 di->chrg_save_sec = 0;
3045 di->dsoc = di->rsoc;
3046 di->chrg_normal_base = get_runtime_sec();
3047 di->charge_smooth_status = false;
3048 di->chrg_save_sec = 0;
3052 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3053 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3054 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3055 chrg_normal_sec, soc_time, plus_soc);
3058 static void rk81x_bat_update_time(struct rk81x_battery *di)
3062 runtime_sec = get_runtime_sec();
3064 /*update by charger type*/
3065 if (rk81x_chrg_online(di))
3066 di->plug_out_base = runtime_sec;
3068 di->plug_in_base = runtime_sec;
3070 /*update by current*/
3071 if (di->chrg_status != CHARGE_FINISH) {
3072 di->finish_sig_base = runtime_sec;
3073 di->chrg_finish_base = runtime_sec;
3076 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3077 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3078 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3080 rk81x_bat_dbg_time_table(di);
3083 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3085 int trend_start_cap = di->trend_start_cap;
3086 int remain_cap = di->remain_capacity;
3090 if (di->s2r && !di->slp_psy_status)
3091 di->trend_start_cap = di->remain_capacity;
3093 diff_cap = remain_cap - trend_start_cap;
3094 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3095 __func__, trend_start_cap, diff_cap);
3096 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3098 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3099 di->trend_start_cap = di->remain_capacity;
3100 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3101 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3103 state = TREND_STAT_FLAT;
3109 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3111 int state, soc_time;
3112 static int trend_down_cnt, trend_up_cnt;
3113 int trend_cnt_thresd;
3114 int now_current = di->current_avg;
3117 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3118 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3119 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3120 __func__, state, trend_mult);
3121 if (di->chrg_status == CHARGE_FINISH)
3124 if (state == TREND_STAT_UP) {
3125 rk81x_bat_reset_zero_var(di);
3127 trend_up_cnt += trend_mult;
3128 if (trend_up_cnt >= trend_cnt_thresd) {
3130 di->dischrg_save_sec = 0;
3132 } else if (state == TREND_STAT_DOWN) {
3134 trend_down_cnt += trend_mult;
3135 if (trend_down_cnt >= trend_cnt_thresd) {
3137 di->chrg_save_sec = 0;
3141 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3142 if ((di->chrg_save_sec + 20 > soc_time) &&
3143 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3145 di->chrg_save_sec = 0;
3147 else if ((di->dischrg_save_sec + 20 > soc_time) &&
3148 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3150 di->dischrg_save_sec = 0;
3152 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3153 "up_cnt=%d, down_cnt=%d\n",
3154 __func__, state, trend_cnt_thresd, soc_time,
3155 trend_up_cnt, trend_down_cnt);
3158 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3160 u32 *ocv_table = di->pdata->battery_ocv;
3161 int delta_soc = di->rsoc - di->dsoc;
3163 if (di->chrg_status == CHARGE_FINISH ||
3164 di->slp_chrg_status == CHARGE_FINISH) {
3165 /*clear sleep charge status*/
3166 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3167 di->chrg_emu_base = 0;
3168 di->chrg_normal_base = 0;
3169 di->chrg_term_base = 0;
3170 rk81x_bat_finish_chrg(di);
3171 rk81x_bat_capacity_init(di, di->fcc);
3172 rk81x_bat_capacity_init_post(di);
3173 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3174 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3175 (di->voltage < ocv_table[18] + 20))) {
3176 di->chrg_emu_base = 0;
3177 di->chrg_normal_base = 0;
3178 di->chrg_finish_base = 0;
3179 rk81x_bat_terminal_chrg(di);
3180 } else if (di->chrg_status != CHARGE_FINISH &&
3181 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3182 di->chrg_term_base = 0;
3183 di->chrg_normal_base = 0;
3184 di->chrg_finish_base = 0;
3185 rk81x_bat_emulator_chrg(di);
3187 di->chrg_emu_base = 0;
3188 di->chrg_term_base = 0;
3189 di->chrg_finish_base = 0;
3190 rk81x_bat_normal_chrg(di);
3194 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3196 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3197 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3199 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3200 dischrg_normal_sec, dischrg_emu_sec);
3202 return (dischrg_normal_sec > dischrg_emu_sec) ?
3203 dischrg_normal_sec : dischrg_emu_sec;
3206 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3208 unsigned long sec1, sec2;
3209 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3210 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3211 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3212 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3214 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3215 chrg_normal_sec : chrg_term_sec;
3217 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3218 chrg_emu_sec : chrg_finish_sec;
3219 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3220 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3221 chrg_normal_sec, chrg_term_sec,
3222 chrg_emu_sec, chrg_finish_sec);
3224 return (sec1 > sec2) ? sec1 : sec2;
3227 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3229 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3230 if (di->current_mode == DISCHRG_MODE) {
3231 di->current_mode = CHRG_MODE;
3232 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3233 di->dischrg_normal_base = 0;
3234 di->dischrg_emu_base = 0;
3235 if (di->chrg_status == CHARGE_FINISH)
3236 di->dischrg_save_sec = 0;
3237 if ((di->chrg_status == CHARGE_FINISH) &&
3239 di->chrg_save_sec = 0;
3241 DBG("<%s>---dischrg_save_sec = %lu\n",
3242 __func__, di->dischrg_save_sec);
3245 if (!rk81x_chrg_online(di)) {
3246 dev_err(di->dev, "discharge, current error:%d\n",
3249 rk81x_bat_chrg_smooth(di);
3250 di->discharge_smooth_status = true;
3253 if (di->current_mode == CHRG_MODE) {
3254 di->current_mode = DISCHRG_MODE;
3255 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3256 di->chrg_normal_base = 0;
3257 di->chrg_emu_base = 0;
3258 di->chrg_term_base = 0;
3259 di->chrg_finish_base = 0;
3260 DBG("<%s>---chrg_save_sec = %lu\n",
3261 __func__, di->chrg_save_sec);
3263 rk81x_bat_dischrg_smooth(di);
3264 di->charge_smooth_status = true;
3269 * update rsoc by relax voltage
3271 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3273 int relax_vol = di->relax_voltage;
3274 int ocv_soc, capacity;
3276 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3277 capacity = (ocv_soc * di->fcc / 100);
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=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3286 * 1: must do it, 0: when necessary
3288 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3290 int ocv_vol = di->est_ocv_vol;
3291 int ocv_soc = 0, capacity = 0;
3293 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3294 capacity = (ocv_soc * di->fcc / 100);
3295 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3296 rk81x_bat_capacity_init(di, capacity);
3297 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3298 di->rsoc = rk81x_bat_get_rsoc(di);
3299 rk81x_bat_capacity_init_post(di);
3300 DBG("<%s>, rsoc updated!\n", __func__);
3302 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3303 __func__, ocv_vol, ocv_soc, capacity);
3306 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3310 unsigned long sleep_sec = di->suspend_time_sum;
3311 int power_off_thresd = di->pdata->power_off_thresd;
3313 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3314 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3315 __func__, di->dsoc, di->rsoc,
3316 di->relax_voltage, di->voltage, sleep_sec / 60);
3318 if (di->relax_voltage >= di->voltage) {
3319 rk81x_bat_relax_vol_calib(di);
3320 rk81x_bat_restart_relax(di);
3322 /* current_avg < 0: make sure the system is not
3323 * wakeup by charger plugin.
3325 /* even if relax voltage is not caught rightly, realtime voltage
3326 * is quite close to relax voltage, we should not do nothing after
3330 rk81x_bat_vol_calib(di, 1);
3334 if (di->dsoc <= di->rsoc) {
3335 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3336 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3337 temp_dsoc = di->dsoc - delta_soc;
3339 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3340 di->rsoc, di->dsoc, delta_soc);
3342 if (delta_soc > 0) {
3343 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3345 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3347 else if (temp_dsoc > 5)
3348 di->dsoc = temp_dsoc;
3351 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3352 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3354 /*di->dsoc > di->rsoc*/
3355 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3356 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3357 temp_dsoc = di->dsoc - di->rsoc;
3359 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3360 di->rsoc, di->dsoc, delta_soc);
3362 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3363 (temp_dsoc > delta_soc))
3364 di->dsoc -= delta_soc;
3366 di->dsoc = di->rsoc;
3368 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3369 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3372 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3378 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3379 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3384 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3387 unsigned long sleep_sec;
3389 sleep_sec = di->suspend_time_sum;
3390 if (((di->suspend_charge_current < 800) &&
3391 (di->ac_online == ONLINE)) ||
3392 (di->chrg_status == CHARGE_FINISH)) {
3393 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3394 if (sleep_sec > 0) {
3395 /*default charge current: 1000mA*/
3396 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3397 / 3600 / div(di->fcc);
3400 DBG("<%s>, usb charge\n", __func__);
3407 * only do report when there is a change.
3409 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3410 * when dsoc == 0, we must do report. But it will generate too much android
3411 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3414 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3417 static u32 old_ac_status;
3418 static u32 old_usb_status;
3419 static u32 old_charge_status;
3422 state_changed = false;
3423 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3424 state_changed = true;
3425 else if (di->dsoc != old_soc)
3426 state_changed = true;
3427 else if (di->ac_online != old_ac_status)
3428 state_changed = true;
3429 else if (di->usb_online != old_usb_status)
3430 state_changed = true;
3431 else if (old_charge_status != di->psy_status)
3432 state_changed = true;
3434 if (rk81x_chrg_online(di)) {
3435 if (di->dsoc == 100)
3436 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3438 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3441 if (state_changed) {
3442 power_supply_changed(&di->bat);
3443 power_supply_changed(&di->usb);
3444 power_supply_changed(&di->ac);
3446 old_ac_status = di->ac_online;
3447 old_usb_status = di->usb_online;
3448 old_charge_status = di->psy_status;
3449 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3450 di->dsoc, di->rsoc);
3455 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3459 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3462 return CHRG_CVCC_HOUR[hour];
3465 /* we have to estimate the charging finish time from now, to decide
3466 * whether we should reset the timer or not.
3468 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3471 int remain_capacity;
3473 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3474 if (di->dsoc < di->rsoc)
3475 remain_capacity = di->dsoc * di->fcc / 100;
3477 remain_capacity = di->remain_capacity;
3479 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3480 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3482 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3483 di->chrg_cap2full = di->fcc - remain_capacity;
3484 if (di->current_avg <= 0)
3485 di->current_avg = 1;
3487 di->chrg_time2full = di->chrg_cap2full * 3600 /
3488 div(abs(di->current_avg));
3490 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3491 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3494 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3495 /*rk81x_bat_init_chrg_timer(di);*/
3496 di->plug_in_base = get_runtime_sec();
3497 DBG("%s: reset charge timer\n", __func__);
3504 * in case that we will do reboot stress test, we need a special way
3505 * to ajust the dsoc.
3507 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3515 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3518 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3519 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3521 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3522 __func__, cnt, unit_time, smooth_time,
3523 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3525 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3526 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3527 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3532 rk81x_bat_save_dsoc(di, dsoc);
3535 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3536 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3541 rk81x_bat_save_dsoc(di, dsoc);
3545 rk81x_bat_save_reboot_cnt(di, cnt);
3548 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3553 uint16_t cal_offset;
3554 u8 pcb_offset = DEF_PCB_OFFSET;
3556 min = BASE_TO_MIN(di->power_on_base);
3557 if ((min % 8) && (old_min != min)) {
3559 rk81x_bat_get_vol_offset(di);
3560 if (di->pcb_ioffset_updated)
3561 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3563 current_offset = rk81x_bat_get_ioffset(di);
3564 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3565 cal_offset = rk81x_bat_get_cal_offset(di);
3566 if (cal_offset < 0x7ff)
3567 rk81x_bat_set_cal_offset(di, di->current_offset +
3569 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3570 __func__, di->voltage_k, di->voltage_b, cal_offset,
3571 rk81x_bat_get_ioffset(di));
3575 static void rk81x_bat_update_info(struct rk81x_battery *di)
3579 else if (di->dsoc < 0)
3583 * we need update fcc in continuous charging state, if discharge state
3584 * keep at least 2 hour, we decide not to update fcc, so clear the
3585 * fcc update flag: dod0_status.
3587 if (BASE_TO_MIN(di->plug_out_base) > 120)
3588 di->dod0_status = 0;
3590 di->voltage = rk81x_bat_get_vol(di);
3591 di->current_avg = rk81x_bat_get_avg_current(di);
3592 di->chrg_status = rk81x_bat_get_chrg_status(di);
3593 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3594 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3595 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3596 /*rk81x_bat_chrg_over_time_check(di);*/
3597 rk81x_bat_update_calib_param(di);
3598 if (di->chrg_status == CC_OR_CV)
3599 di->enter_finish = true;
3600 #if defined(CONFIG_ARCH_ROCKCHIP)
3601 rk81x_bat_status_check(di);/* ac_online, usb_online, status*/
3604 if (!rk81x_chrg_online(di) && di->s2r)
3607 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3608 if (di->remain_capacity > di->fcc) {
3609 rk81x_bat_capacity_init(di, di->fcc);
3610 rk81x_bat_capacity_init_post(di);
3611 di->remain_capacity = di->fcc;
3614 di->rsoc = rk81x_bat_get_rsoc(di);
3617 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3619 if (di->slp_psy_status)
3620 return rk81x_bat_sleep_chrg(di);
3622 return rk81x_bat_sleep_dischrg(di);
3625 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3627 u8 ocv_soc, soc_level;
3628 int relax_vol = di->relax_voltage;
3633 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3634 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3635 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3637 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3638 (di->dod0_status == 0) &&
3640 di->dod0_voltage = relax_vol;
3641 di->dod0_capacity = di->temp_nac;
3646 di->dod0_level = 100;
3647 else if (ocv_soc < 5)
3648 di->dod0_level = 90;
3650 di->dod0_level = 80;
3652 /* save_soc = di->dod0_level; */
3653 soc_level = rk81x_bat_get_level(di);
3654 if (soc_level > di->dod0_level) {
3655 di->dod0_status = 0;
3657 di->dod0_status = 1;
3659 di->fcc_update_sec = get_runtime_sec();
3662 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3663 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3665 di->dod0_voltage, di->dod0_capacity,
3666 ocv_soc, soc_level, di->dod0_status,
3671 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3676 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3679 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3681 dev_info(di->dev, "set charge to %s termination mode\n",
3682 mode ? "digital" : "analog");
3685 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3687 struct rk81x_battery *di;
3689 di = container_of(work, struct rk81x_battery,
3690 chrg_term_mode_switch_work.work);
3692 if (rk81x_chrg_online(di))
3693 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3695 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3698 static void rk81x_battery_work(struct work_struct *work)
3700 struct rk81x_battery *di;
3701 int ms = TIMER_MS_COUNTS;
3703 di = container_of(work, struct rk81x_battery,
3704 battery_monitor_work.work);
3705 if (rk81x_chrg_online(di)) {
3706 rk81x_bat_wait_finish_sig(di);
3707 /*rk81x_bat_chrg_finish_routine(di);*/
3709 rk81x_bat_fcc_flag_check(di);
3710 rk81x_bat_arbitrate_rsoc_trend(di);
3711 rk81x_bat_display_smooth(di);
3712 rk81x_bat_update_time(di);
3713 rk81x_bat_update_info(di);
3714 rk81x_bat_rsoc_check(di);
3715 rk81x_bat_power_supply_changed(di);
3716 rk81x_bat_save_dsoc(di, di->dsoc);
3717 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3719 rk81x_bat_dbg_dmp_info(di);
3721 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3722 ms = 30 * TIMER_MS_COUNTS;
3724 di->early_resume = 0;
3728 queue_delayed_work(di->wq, &di->battery_monitor_work,
3729 msecs_to_jiffies(ms));
3732 static void rk81x_battery_charge_check_work(struct work_struct *work)
3734 struct rk81x_battery *di = container_of(work,
3735 struct rk81x_battery, charge_check_work.work);
3737 DBG("rk81x_battery_charge_check_work\n");
3738 rk81x_charge_disable_open_otg(di);
3741 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3743 int register_battery_notifier(struct notifier_block *nb)
3745 return blocking_notifier_chain_register(&battery_chain_head, nb);
3747 EXPORT_SYMBOL_GPL(register_battery_notifier);
3749 int unregister_battery_notifier(struct notifier_block *nb)
3751 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3753 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3755 int battery_notifier_call_chain(unsigned long val)
3757 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3758 == NOTIFY_BAD) ? -EINVAL : 0;
3760 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3762 static int rk81x_bat_notifier_call(struct notifier_block *nb,
3763 unsigned long event, void *data)
3765 struct rk81x_battery *di =
3766 container_of(nb, struct rk81x_battery, battery_nb);
3769 case USB_OTG_POWER_ON:
3770 dev_info(di->dev, "charge disable, otg enable\n");
3771 di->charge_otg = USB_OTG_POWER_ON;
3772 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3773 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3774 queue_delayed_work(di->wq, &di->charge_check_work,
3775 msecs_to_jiffies(50));
3778 case USB_OTG_POWER_OFF:
3779 dev_info(di->dev, "charge enable, otg disable\n");
3780 di->charge_otg = USB_OTG_POWER_OFF;
3781 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3782 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3783 queue_delayed_work(di->wq, &di->charge_check_work,
3784 msecs_to_jiffies(50));
3792 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3794 pr_info("\n------- %s:lower power warning!\n", __func__);
3796 rk_send_wakeup_key();
3801 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3803 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3804 rk_send_wakeup_key();
3808 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3810 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3811 rk_send_wakeup_key();
3815 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3817 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3819 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3820 di->finish_sig_base = get_runtime_sec();
3821 rk_send_wakeup_key();
3825 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3827 rk_send_wakeup_key();
3832 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3837 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3838 ret = sysfs_create_file(&di->bat.dev->kobj,
3839 &rk818_bat_attr[i].attr);
3841 dev_err(di->dev, "create battery node(%s) error\n",
3842 rk818_bat_attr[i].attr.name);
3848 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3850 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3852 struct rk818 *chip = di->rk818;
3854 #if defined(CONFIG_X86_INTEL_SOFIA)
3855 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3856 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3857 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3858 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3860 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3861 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3862 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3863 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3866 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3867 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3869 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3871 di->irq = vb_lo_irq;
3872 enable_irq_wake(di->irq);
3874 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3875 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3878 dev_err(chip->dev, "plug_in_irq request failed!\n");
3880 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3881 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3884 dev_err(chip->dev, "plug_out_irq request failed!\n");
3886 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3887 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3890 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3893 static void rk81x_bat_info_init(struct rk81x_battery *di,
3897 unsigned long time_base = get_runtime_sec();
3899 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3900 if (val & PLUG_IN_STS)
3901 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3903 di->cell.config = di->pdata->cell_cfg;
3904 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3905 di->qmax = di->pdata->cell_cfg->design_qmax;
3906 di->early_resume = 1;
3907 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3908 di->bat_res = di->pdata->sense_resistor_mohm;
3909 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3910 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3911 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3912 di->loader_charged = loader_charged;
3913 di->chrg_finish_base = time_base;
3914 di->power_on_base = time_base;
3915 di->plug_in_base = time_base;
3916 di->plug_out_base = time_base;
3917 di->finish_sig_base = time_base;
3918 di->fcc = rk81x_bat_get_fcc(di);
3921 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3922 struct device_node *np)
3924 struct device *dev = di->dev;
3925 enum of_gpio_flags flags;
3928 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3929 if (di->dc_det_pin == -EPROBE_DEFER)
3930 dev_err(dev, "dc_det_gpio error\n");
3931 if (gpio_is_valid(di->dc_det_pin))
3932 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3933 RK818_DC_IN : RK818_DC_OUT;
3934 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3935 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3936 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3937 "rk81x_dc_det", NULL);
3940 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3941 enable_irq_wake(di->dc_det_irq);
3944 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3949 struct timespec tv = {
3950 .tv_nsec = NSEC_PER_SEC >> 1,
3952 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3954 err = rtc_read_time(rtc, &tm);
3956 dev_err(rtc->dev.parent,
3957 "hctosys: unable to read the hardware clock\n");
3959 err = rtc_valid_tm(&tm);
3961 dev_err(rtc->dev.parent,
3962 "hctosys: invalid date/time\n");
3965 rtc_tm_to_time(&tm, &tv.tv_sec);
3966 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3972 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
3974 struct device_node *np;
3975 struct battery_platform_data *pdata;
3976 struct cell_config *cell_cfg;
3977 struct ocv_config *ocv_cfg;
3978 struct property *prop;
3979 struct rk818 *rk818 = di->rk818;
3980 struct device *dev = di->dev;
3985 np = of_find_node_by_name(rk818->dev->of_node, "battery");
3987 dev_err(dev, "battery node not found!\n");
3991 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
3995 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3999 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4003 prop = of_find_property(np, "ocv_table", &length);
4005 dev_err(dev, "ocv_table not found!\n");
4008 pdata->ocv_size = length / sizeof(u32);
4009 if (pdata->ocv_size <= 0) {
4010 dev_err(dev, "invalid ocv table\n");
4014 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4016 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4017 if (!pdata->battery_ocv)
4020 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4025 /******************** charger param ****************************/
4026 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4028 dev_err(dev, "max_chrg_currentmA not found!\n");
4029 out_value = DEFAULT_CHRG_CUR;
4031 pdata->max_charger_currentmA = out_value;
4033 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4035 dev_err(dev, "max_charger_ilimitmA not found!\n");
4036 out_value = DEFAULT_INPUT_CUR;
4038 pdata->max_charger_ilimitmA = out_value;
4040 ret = of_property_read_u32(np, "bat_res", &out_value);
4042 dev_err(dev, "bat_res not found!\n");
4043 out_value = DEFAULT_BAT_RES;
4045 pdata->sense_resistor_mohm = out_value;
4047 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4049 dev_err(dev, "max_charge_voltagemV not found!\n");
4050 out_value = DEFAULT_CHRG_VOL;
4052 pdata->max_charger_voltagemV = out_value;
4054 ret = of_property_read_u32(np, "design_capacity", &out_value);
4056 dev_err(dev, "design_capacity not found!\n");
4059 cell_cfg->design_capacity = out_value;
4061 ret = of_property_read_u32(np, "design_qmax", &out_value);
4063 dev_err(dev, "design_qmax not found!\n");
4066 cell_cfg->design_qmax = out_value;
4068 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4070 dev_err(dev, "sleep_enter_current not found!\n");
4071 out_value = DEFAULT_SLP_ENTER_CUR;
4073 ocv_cfg->sleep_enter_current = out_value;
4075 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4077 dev_err(dev, "sleep_exit_current not found!\n");
4078 out_value = DEFAULT_SLP_EXIT_CUR;
4080 ocv_cfg->sleep_exit_current = out_value;
4082 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4084 dev_warn(dev, "power_off_thresd not found!\n");
4085 out_value = PWR_OFF_THRESD;
4087 pdata->power_off_thresd = out_value;
4089 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4090 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4091 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4093 /************* charger support adp types **********************/
4094 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4095 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4097 if (!support_usb_adp && !support_dc_adp) {
4098 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4099 support_usb_adp = 1;
4103 rk81x_bat_dc_det_init(di, np);
4105 cell_cfg->ocv = ocv_cfg;
4106 pdata->cell_cfg = cell_cfg;
4109 DBG("\nthe battery dts info dump:\n"
4111 "max_input_currentmA:%d\n"
4112 "max_chrg_currentmA:%d\n"
4113 "max_charge_voltagemV:%d\n"
4114 "design_capacity:%d\n"
4116 "sleep_enter_current:%d\n"
4117 "sleep_exit_current:%d\n"
4118 "support_usb_adp:%d\n"
4119 "support_dc_adp:%d\n"
4120 "power_off_thresd:%d\n",
4121 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4122 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4123 cell_cfg->design_capacity, cell_cfg->design_qmax,
4124 cell_cfg->ocv->sleep_enter_current,
4125 cell_cfg->ocv->sleep_exit_current,
4126 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4132 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4138 static int rk81x_battery_probe(struct platform_device *pdev)
4140 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4141 struct rk81x_battery *di;
4144 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4148 di->dev = &pdev->dev;
4149 platform_set_drvdata(pdev, di);
4151 ret = rk81x_bat_parse_dt(di);
4153 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4157 rk81x_bat_info_init(di, chip);
4158 if (!is_rk81x_bat_exist(di)) {
4159 dev_info(di->dev, "not battery, enter test power mode\n");
4160 di->fg_drv_mode = TEST_POWER_MODE;
4163 ret = rk81x_bat_power_supply_init(di);
4165 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4169 rk81x_bat_irq_init(di);
4170 rk81x_bat_sysfs_init(di);
4172 rk81x_bat_fg_init(di);
4173 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4175 rk81x_bat_flatzone_vol_init(di);
4177 #if defined(CONFIG_X86_INTEL_SOFIA)
4178 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4179 if (IS_ERR_OR_NULL(di->usb_phy)) {
4180 dev_err(di->dev, "get usb phy failed\n");
4181 return PTR_ERR(di->usb_phy);
4183 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4184 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4186 dev_err(di->dev, "registr usb phy notification failed\n");
4187 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4188 rk81x_battery_usb_notifier_delayed_work);
4191 rk81x_battery_register_fb_notify(di);
4192 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4193 "rk81x-battery-work");
4194 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4195 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4196 rk81x_chrg_term_mode_switch_work);
4198 queue_delayed_work(di->wq, &di->battery_monitor_work,
4199 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4201 #if defined(CONFIG_ARCH_ROCKCHIP)
4202 INIT_DELAYED_WORK(&di->charge_check_work,
4203 rk81x_battery_charge_check_work);
4204 di->battery_nb.notifier_call = rk81x_bat_notifier_call;
4205 rk_bc_detect_notifier_register(&di->battery_nb, &di->charge_otg);
4207 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4212 static int rk81x_battery_suspend(struct platform_device *dev,
4215 struct rk81x_battery *di = platform_get_drvdata(dev);
4217 di->slp_psy_status = rk81x_chrg_online(di);
4219 di->chrg_status = rk81x_bat_get_chrg_status(di);
4220 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4221 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4222 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4223 di->dischrg_normal_base = 0;
4224 di->dischrg_emu_base = 0;
4225 do_gettimeofday(&di->suspend_rtc_base);
4227 if (!rk81x_chrg_online(di)) {
4228 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4229 di->chrg_normal_base = 0;
4230 di->chrg_emu_base = 0;
4231 di->chrg_term_base = 0;
4232 di->chrg_finish_base = 0;
4237 * do not modify the g_base_sec
4239 g_base_sec = get_runtime_sec();
4241 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4242 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4243 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4248 static int rk81x_battery_resume(struct platform_device *dev)
4250 struct rk81x_battery *di = platform_get_drvdata(dev);
4251 int pwroff_thresd = di->pdata->power_off_thresd;
4257 di->discharge_smooth_status = true;
4258 di->charge_smooth_status = true;
4260 vol = rk81x_bat_get_vol(di);
4261 if (vol < INVALID_VOL_THRESD) {
4262 dev_err(di->dev, "invalid voltage :%d", vol);
4267 di->current_avg = rk81x_bat_get_avg_current(di);
4268 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4269 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4270 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4271 delta_time = rk81x_bat_get_suspend_sec(di);
4272 di->suspend_time_sum += delta_time;
4273 #if defined(CONFIG_ARCH_ROCKCHIP)
4274 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4277 if (di->slp_psy_status) {
4278 time_step = CHRG_TIME_STEP;
4280 if (di->voltage <= pwroff_thresd + 50)
4281 time_step = DISCHRG_TIME_STEP_0;
4283 time_step = DISCHRG_TIME_STEP_1;
4286 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4287 di->current_avg, di->voltage, di->est_ocv_vol,
4288 di->relax_voltage, delta_time, di->suspend_time_sum,
4289 di->slp_psy_status);
4291 if (di->suspend_time_sum > time_step) {
4292 delta_soc = rk81x_bat_update_resume_state(di);
4294 di->suspend_time_sum = 0;
4297 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4298 rk81x_chrg_online(di))
4299 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4302 * do not modify the g_base_sec
4304 if (is_local_clock_reset())
4305 g_base_sec += delta_time;
4312 static int rk81x_battery_remove(struct platform_device *dev)
4314 struct rk81x_battery *di = platform_get_drvdata(dev);
4316 cancel_delayed_work_sync(&di->battery_monitor_work);
4320 static void rk81x_battery_shutdown(struct platform_device *dev)
4322 struct rk81x_battery *di = platform_get_drvdata(dev);
4324 cancel_delayed_work_sync(&di->battery_monitor_work);
4325 rk_bc_detect_notifier_unregister(&di->battery_nb);
4327 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4328 rk81x_bat_check_reboot(di);
4330 rk81x_bat_save_reboot_cnt(di, 0);
4331 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4334 static struct platform_driver rk81x_battery_driver = {
4336 .name = "rk818-battery",
4337 .owner = THIS_MODULE,
4340 .probe = rk81x_battery_probe,
4341 .remove = rk81x_battery_remove,
4342 .suspend = rk81x_battery_suspend,
4343 .resume = rk81x_battery_resume,
4344 .shutdown = rk81x_battery_shutdown,
4347 static int __init battery_init(void)
4349 return platform_driver_register(&rk81x_battery_driver);
4352 fs_initcall_sync(battery_init);
4353 static void __exit battery_exit(void)
4355 platform_driver_unregister(&rk81x_battery_driver);
4357 module_exit(battery_exit);
4359 MODULE_LICENSE("GPL");
4360 MODULE_ALIAS("platform:rk818-battery");
4361 MODULE_AUTHOR("ROCKCHIP");