2 * rk818/rk819 battery driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd
5 * Author: zhangqing <zhangqing@rock-chips.com>
6 * chenjh <chenjh@rock-chips.com>
7 * Andy Yan <andy.yan@rock-chips.com>
9 * Copyright (C) 2008-2009 Texas Instruments, Inc.
10 * Author: Texas Instruments, Inc.
12 * Copyright (C) 2008-2009 Texas Instruments, Inc.
13 * Author: Texas Instruments, Inc.
14 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
15 * Author: zhangqing <zhangqing@rock-chips.com>
16 * Copyright (C) 2014-2015 Intel Mobile Communications GmbH
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
40 #include <linux/gpio.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
43 #include <linux/mfd/rk818.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/rtc.h>
47 #include <linux/wakelock.h>
48 #include <linux/of_gpio.h>
49 #include <linux/irq.h>
50 #include <linux/irqdomain.h>
51 #include <linux/usb/phy.h>
54 #if defined(CONFIG_X86_INTEL_SOFIA)
55 #include <linux/usb/phy-intel.h>
57 #include <linux/power/rk_usbbc.h>
59 #include "rk818_battery.h"
61 /* if you want to disable, don't set it as 0,
62 just be: "static int dbg_enable;" is ok*/
64 static int dbg_enable;
65 #define RK818_SYS_DBG 1
67 module_param_named(dbg_level, dbg_enable, int, 0644);
69 #define DBG(args...) \
76 #define DEFAULT_BAT_RES 135
77 #define DEFAULT_CHRG_VOL 4200
78 #define DEFAULT_CHRG_CUR 1000
79 #define DEFAULT_INPUT_CUR 1400
80 #define DEFAULT_SLP_ENTER_CUR 600
81 #define DEFAULT_SLP_EXIT_CUR 600
83 #define DSOC_DISCHRG_EMU_CURR 1200
84 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
85 #define DSOC_DISCHRG_FAST_EER_RANGE 10
86 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
87 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
88 #define DSOC_CHRG_FAST_EER_RANGE 10
89 #define DSOC_CHRG_EMU_CURR 1200
90 #define DSOC_CHRG_TERM_CURR 600
91 #define DSOC_CHRG_TERM_VOL 4100
92 #define CHRG_FINISH_VOL 4100
94 /*realtime RSOC calib param*/
95 #define RSOC_DISCHRG_ERR_LOWER 40
96 #define RSOC_DISCHRG_ERR_UPPER 50
97 #define RSOC_ERR_CHCK_CNT 15
98 #define RSOC_COMPS 20 /*compensation*/
99 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
100 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
102 #define RSOC_RESUME_ERR 10
103 #define REBOOT_INTER_MIN 1
105 #define INTERPOLATE_MAX 1000
106 #define MAX_INT 0x7FFF
107 #define TIME_10MIN_SEC 600
109 #define CHRG_VOL_SHIFT 4
110 #define CHRG_ILIM_SHIFT 0
111 #define CHRG_ICUR_SHIFT 0
112 #define DEF_CHRG_VOL CHRG_VOL4200
113 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
114 #define DEF_CHRG_CURR_LMT ILIM_2000MA
116 /*TEST_POWER_MODE params*/
117 #define TEST_CURRENT 1000
118 #define TEST_VOLTAGE 3800
120 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
121 #define TEST_PRESET 1
122 #define TEST_AC_ONLINE 1
123 #define TEST_USB_ONLINE 0
125 #define ZERO_ALGOR_THRESD 3800
126 #define DISCHRG_ZERO_MODE 1
127 #define DISCHRG_NORMAL_MODE 0
128 #define DEF_LAST_ZERO_MODE_SOC -1
130 #define DISCHRG_MODE 0
133 #define TREND_STAT_FLAT 0
134 #define TREND_STAT_DOWN -1
135 #define TREND_STAT_UP 1
136 #define TREND_CAP_DIFF 5
140 #define SLP_CURR_MAX 40
141 #define SLP_CURR_MIN 6
142 #define WAKEUP_SEC_THRESD 40
143 #define CHRG_TIME_STEP (60)
144 #define DISCHRG_TIME_STEP_0 (30 * 60)
145 #define DISCHRG_TIME_STEP_1 (60 * 60)
147 #define DEF_PCB_OFFSET 42
148 #define DEF_CAL_OFFSET 0x832
149 #define DEF_PWRPATH_RES 50
150 #define SEC_TO_EMPTY 300
151 #define DSOC_CHRG_FINISH_CURR 1100
152 #define SLP_CHRG_CURR 1000
153 #define SLP_DSOC_VOL_THRESD 3600
154 /*if voltage is lower than this thresd,
155 we consider it as invalid
157 #define INVALID_VOL_THRESD 2500
158 #define PWR_OFF_THRESD 3400
159 #define MIN_ZERO_ACCURACY 5 /*0.01%*/
160 #define MIN_ROUND_ACCURACY 1
162 #define MAX_FCC 10000
165 * the following table value depends on datasheet
167 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
169 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
170 2250, 2400, 2600, 2800, 3000};
172 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
173 2000, 2250, 2500, 2750, 3000};
175 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
177 #define RK818_DC_IN 0
178 #define RK818_DC_OUT 1
180 #define OCV_VALID_SHIFT (0)
181 #define OCV_CALIB_SHIFT (1)
182 #define FIRST_PWRON_SHIFT (2)
184 #define SEC_TO_MIN(x) ((x) / 60)
186 struct rk81x_battery {
188 struct cell_state cell;
189 struct power_supply bat;
190 struct power_supply ac;
191 struct power_supply usb;
192 struct delayed_work work;
194 struct pinctrl *pinctrl;
195 struct pinctrl_state *pins_default;
197 struct battery_platform_data *pdata;
211 uint16_t voltage_ocv;
212 uint16_t relax_voltage;
218 bool pcb_ioffset_updated;
235 int bat_res_update_cnt;
241 unsigned long dod0_time;
254 int current_k;/* (ICALIB0, ICALIB1) */
257 int voltage_k;/* VCALIB0 VCALIB1 */
260 int zero_timeout_cnt;
261 int zero_old_remain_cap;
266 int charge_smooth_time;
270 unsigned long suspend_time_sum;
274 int suspend_charge_current;
277 bool charge_smooth_status;
278 bool discharge_smooth_status;
284 struct notifier_block battery_nb;
285 struct usb_phy *usb_phy;
286 struct notifier_block usb_nb;
287 struct notifier_block fb_nb;
290 int s2r; /*suspend to resume*/
291 struct workqueue_struct *wq;
292 struct delayed_work battery_monitor_work;
293 struct delayed_work otg_check_work;
294 struct delayed_work usb_phy_delay_work;
295 struct delayed_work chrg_term_mode_switch_work;
296 struct delayed_work ac_usb_check_work;
297 struct delayed_work dc_det_check_work;
298 enum bc_port_type charge_otg;
301 struct wake_lock resume_wake_lock;
302 unsigned long plug_in_base;
303 unsigned long plug_out_base;
304 unsigned long finish_sig_base;
305 unsigned long power_on_base;
310 bool is_first_poweron;
313 int debug_finish_real_soc;
314 int debug_finish_temp_soc;
320 unsigned long wakeup_sec;
321 u32 delta_vol_smooth;
322 unsigned long dischrg_normal_base;
323 unsigned long dischrg_emu_base;
324 unsigned long chrg_normal_base;
325 unsigned long chrg_term_base;
326 unsigned long chrg_emu_base;
327 unsigned long chrg_finish_base;
328 unsigned long fcc_update_sec;
330 u8 dischrg_algorithm_mode;
331 int last_zero_mode_dsoc;
333 unsigned long dischrg_save_sec;
334 unsigned long chrg_save_sec;
335 struct timeval suspend_rtc_base;
338 u32 support_usb_adp, support_dc_adp;
340 #define to_device_info(x) container_of((x), \
341 struct rk81x_battery, bat)
343 #define to_ac_device_info(x) container_of((x), \
344 struct rk81x_battery, ac)
346 #define to_usb_device_info(x) container_of((x), \
347 struct rk81x_battery, usb)
349 static int loader_charged;
351 static int __init rk81x_bat_loader_charged(char *__unused)
355 pr_info("battery charged in loader\n");
359 __setup("loader_charged", rk81x_bat_loader_charged);
361 static u64 g_base_sec;
362 static u64 get_runtime_sec(void)
364 u64 ts_ns = local_clock();
366 do_div(ts_ns, 1000000000);
368 return ts_ns + g_base_sec;
371 static u64 is_local_clock_reset(void)
373 u64 ts_ns = local_clock();
375 do_div(ts_ns, 1000000000);
380 static inline unsigned long BASE_TO_SEC(unsigned long x)
383 return get_runtime_sec() - x;
388 static inline unsigned long BASE_TO_MIN(unsigned long x)
390 return BASE_TO_SEC(x) / 60;
393 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
398 case HW_ADP_TYPE_USB:
406 case HW_ADP_TYPE_DUAL:
407 if (support_usb_adp && support_dc_adp)
417 static bool rk81x_chrg_online(struct rk81x_battery *di)
419 return di->usb_online || di->ac_online;
422 static u32 interpolate(int value, u32 *table, int size)
427 for (i = 0; i < size; i++) {
428 if (value < table[i])
432 if ((i > 0) && (i < size)) {
433 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
434 d /= table[i] - table[i-1];
435 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
437 d = i * ((INTERPOLATE_MAX + size / 2) / size);
446 /* Returns (a * b) / c */
447 static int32_t ab_div_c(u32 a, u32 b, u32 c)
453 sign = ((((a^b)^c) & 0x80000000) != 0);
459 tmp = (a * b + (c >> 1)) / c;
471 static int div(int val)
473 return (val == 0) ? 1 : val;
476 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
477 u8 buf[], unsigned len)
482 for (i = 0; ret < 0 && i < 3; i++) {
483 ret = rk818_i2c_read(di->rk818, reg, len, buf);
485 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
488 return (ret < 0) ? ret : 0;
491 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
492 u8 const buf[], unsigned len)
497 for (i = 0; ret < 0 && i < 3; i++) {
498 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
500 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
503 return (ret < 0) ? ret : 0;
506 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
511 for (i = 0; ret < 0 && i < 3; i++) {
512 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
514 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
520 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
525 for (i = 0; ret < 0 && i < 3; i++) {
526 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
528 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
534 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
539 rk81x_bat_read(di, reg, &buf, 1);
540 val = (buf & BIT(shift)) >> shift;
544 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
549 DBG("%s dump charger register start:\n", __func__);
550 for (i = 0xAC; i < 0xEE; i++) {
551 rk81x_bat_read(di, i, &buf, 1);
552 DBG("0x%02x : 0x%02x\n", i, buf);
557 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
562 DBG("%s dump the register start:\n", __func__);
563 for (i = 0x99; i < 0xAB; i++) {
564 rk81x_bat_read(di, i, &buf, 1);
565 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
570 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
572 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
573 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
576 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
578 rk81x_bat_reset_zero_var(di);
579 di->trend_start_cap = di->remain_capacity;
582 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
588 delta_cap = capacity - di->remain_capacity;
592 di->adjust_cap += delta_cap;
594 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
596 buf = (capacity_ma >> 24) & 0xff;
597 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
598 buf = (capacity_ma >> 16) & 0xff;
599 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
600 buf = (capacity_ma >> 8) & 0xff;
601 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
602 buf = (capacity_ma & 0xff) | 0x01;
603 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
604 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
608 if (di->chrg_status != CHARGE_FINISH || di->dod0_status == 1)
609 dev_dbg(di->dev, "update capacity :%d--remain_cap:%d\n",
610 capacity, di->remain_capacity);
615 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
617 static ssize_t bat_calib_read(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 struct power_supply *psy_bat = dev_get_drvdata(dev);
621 struct rk81x_battery *di = to_device_info(psy_bat);
624 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
626 return sprintf(buf, "%d\n", val);
629 static ssize_t bat_calib_write(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t count)
635 struct power_supply *psy_bat = dev_get_drvdata(dev);
636 struct rk81x_battery *di = to_device_info(psy_bat);
638 ret = kstrtou8(buf, 0, &val);
643 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
645 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
650 * interface for debug: force battery to over discharge
652 static ssize_t bat_test_power_read(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct power_supply *psy_bat = dev_get_drvdata(dev);
656 struct rk81x_battery *di = to_device_info(psy_bat);
658 return sprintf(buf, "%d\n", di->fg_drv_mode);
661 static ssize_t bat_test_power_write(struct device *dev,
662 struct device_attribute *attr,
663 const char *buf, size_t count)
667 struct power_supply *psy_bat = dev_get_drvdata(dev);
668 struct rk81x_battery *di = to_device_info(psy_bat);
670 ret = kstrtou8(buf, 0, &val);
675 di->fg_drv_mode = TEST_POWER_MODE;
677 di->fg_drv_mode = FG_NORMAL_MODE;
682 static ssize_t bat_fcc_read(struct device *dev,
683 struct device_attribute *attr, char *buf)
685 struct power_supply *psy_bat = dev_get_drvdata(dev);
686 struct rk81x_battery *di = to_device_info(psy_bat);
688 return sprintf(buf, "%d\n", di->fcc);
691 static ssize_t bat_fcc_write(struct device *dev,
692 struct device_attribute *attr,
693 const char *buf, size_t count)
697 struct power_supply *psy_bat = dev_get_drvdata(dev);
698 struct rk81x_battery *di = to_device_info(psy_bat);
700 ret = kstrtou16(buf, 0, &val);
709 static ssize_t bat_dsoc_read(struct device *dev,
710 struct device_attribute *attr, char *buf)
712 struct power_supply *psy_bat = dev_get_drvdata(dev);
713 struct rk81x_battery *di = to_device_info(psy_bat);
715 return sprintf(buf, "%d\n", di->dsoc);
718 static ssize_t bat_dsoc_write(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf, size_t count)
724 struct power_supply *psy_bat = dev_get_drvdata(dev);
725 struct rk81x_battery *di = to_device_info(psy_bat);
727 ret = kstrtou8(buf, 0, &val);
736 static ssize_t bat_rsoc_read(struct device *dev,
737 struct device_attribute *attr, char *buf)
739 struct power_supply *psy_bat = dev_get_drvdata(dev);
740 struct rk81x_battery *di = to_device_info(psy_bat);
742 return sprintf(buf, "%d\n", di->rsoc);
745 static ssize_t bat_rsoc_write(struct device *dev,
746 struct device_attribute *attr,
747 const char *buf, size_t count)
752 struct power_supply *psy_bat = dev_get_drvdata(dev);
753 struct rk81x_battery *di = to_device_info(psy_bat);
755 ret = kstrtou8(buf, 0, &val);
759 capacity = di->fcc * val / 100;
760 rk81x_bat_capacity_init(di, capacity);
761 rk81x_bat_capacity_init_post(di);
766 static ssize_t bat_remain_cap_read(struct device *dev,
767 struct device_attribute *attr,
770 struct power_supply *psy_bat = dev_get_drvdata(dev);
771 struct rk81x_battery *di = to_device_info(psy_bat);
773 return sprintf(buf, "%d\n", di->remain_capacity);
776 static struct device_attribute rk818_bat_attr[] = {
777 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
778 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
779 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
780 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
781 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
782 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
786 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
791 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
793 dev_err(di->dev, "error reading TS_CTRL_REG");
798 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
803 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
805 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
808 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
812 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
817 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
823 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
825 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
828 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
832 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
838 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
840 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
843 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
847 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
853 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
855 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
861 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
867 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
869 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
875 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
881 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
882 buf = (value >> 8) & 0xff;
883 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
888 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
890 int vcalib0, vcalib1;
892 vcalib0 = rk81x_bat_get_vcalib0(di);
893 vcalib1 = rk81x_bat_get_vcalib1(di);
895 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
896 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
897 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
900 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
905 uint16_t voltage_now = 0;
909 for (i = 0; i < 3; i++) {
910 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
912 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
916 dev_err(di->dev, "error read BAT_OCV_REGH");
921 if (val[0] == val[1])
926 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
931 static int rk81x_bat_get_vol(struct rk81x_battery *di)
940 for (i = 0; i < 3; i++) {
941 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
943 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
947 dev_err(di->dev, "error read BAT_VOL_REGH");
952 if (val[0] == val[1])
957 vol = di->voltage_k * temp / 1000 + di->voltage_b;
962 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
967 ret = rk81x_bat_read(di, GGSTS, &status, 1);
969 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
975 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
979 uint16_t temp = 0, voltage_now;
981 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
983 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
986 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
991 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
995 uint16_t temp = 0, voltage_now;
997 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
999 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
1002 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
1007 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1011 uint16_t relax_vol1, relax_vol2;
1014 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1015 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1017 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1018 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1019 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1020 __func__, status, ggcon, relax_vol1, relax_vol2);
1022 if (is_rk81x_bat_relax_mode(di))
1023 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1029 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1030 * based on the voltage.
1032 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1039 ocv_table = di->pdata->battery_ocv;
1040 ocv_size = di->pdata->ocv_size;
1041 tmp = interpolate(voltage, ocv_table, ocv_size);
1042 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1043 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1048 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1054 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1056 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1060 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1062 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1068 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1078 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1082 rk81x_bat_read(di, GGCON, &ggcon, 1);
1083 ggcon &= ~(0x30); /*clear <5:4>*/
1085 rk81x_bat_write(di, GGCON, &ggcon, 1);
1089 * when charger finish signal comes, we need calibrate the current, make it
1092 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1102 if ((di->chrg_status == CHARGE_FINISH) &&
1103 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1104 (abs(di->current_avg) > 4)) {
1105 for (retry = 0; retry < 5; retry++) {
1106 adc_value = rk81x_bat_get_raw_adc_current(di);
1107 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1108 dev_warn(di->dev, "charger plugout\n");
1113 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1114 C0 = rk81x_bat_get_cal_offset(di);
1115 C1 = adc_value + C0;
1116 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1118 rk81x_bat_set_cal_offset(di, C1);
1119 DBG("<%s>. new cal_offset = %d\n",
1120 __func__, rk81x_bat_get_cal_offset(di));
1122 adc_value = rk81x_bat_get_raw_adc_current(di);
1123 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1124 if (abs(adc_value) < 4) {
1125 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1126 ioffset = rk81x_bat_get_ioffset(di);
1127 rk81x_bat_set_cal_offset(di,
1130 ioffset = rk81x_bat_get_ioffset(di);
1131 pcb_offset = C1 - ioffset;
1132 di->pcb_ioffset = pcb_offset;
1133 di->pcb_ioffset_updated = true;
1138 DBG("<%s>. update the cal_offset, C1 = %d\n"
1139 "i_offset = %d, pcb_offset = %d\n",
1140 __func__, C1, ioffset, pcb_offset);
1144 dev_dbg(di->dev, "ioffset cal failed\n");
1145 rk81x_bat_set_cal_offset(di, C0);
1148 di->pcb_ioffset_updated = false;
1155 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1158 int enter_thres, exit_thres;
1159 struct cell_state *cell = &di->cell;
1161 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1162 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1163 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1164 __func__, cell->config->ocv->sleep_enter_current,
1165 cell->config->ocv->sleep_exit_current);
1167 buf = enter_thres & 0xff;
1168 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1169 buf = (enter_thres >> 8) & 0xff;
1170 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1172 buf = exit_thres & 0xff;
1173 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1174 buf = (exit_thres >> 8) & 0xff;
1175 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1177 /* set sample time */
1178 rk81x_bat_read(di, GGCON, &buf, 1);
1179 buf &= ~(3 << 2);/*8min*/
1180 buf &= ~0x01; /* clear bat_res calc*/
1181 rk81x_bat_write(di, GGCON, &buf, 1);
1184 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1189 rk81x_bat_read(di, GGCON, &ggcon, 1);
1191 rk81x_bat_write(di, GGCON, &ggcon, 1);
1193 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1195 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1198 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1207 for (i = 0; i < 3; i++) {
1208 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1210 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1215 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1217 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1223 if (val[0] == val[1])
1224 current_now = val[0];
1226 current_now = val[2];
1228 if (current_now & 0x800)
1229 current_now -= 4096;
1231 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1236 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1237 enum charger_type charger_type)
1239 di->usb_online = OFFLINE;
1240 di->ac_online = OFFLINE;
1242 switch (charger_type) {
1244 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1247 di->usb_online = ONLINE;
1248 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1250 case DC_CHARGER:/*treat dc as ac*/
1252 di->ac_online = ONLINE;
1253 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1256 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1260 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1261 msecs_to_jiffies(1000));
1264 /* high load: current < 0 with charger in.
1265 * System will not shutdown while dsoc=0% with charging state(ac_online),
1266 * which will cause over discharge, so oppose status before report states.
1268 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1271 int pwr_off_thresd = di->pdata->power_off_thresd - 50;
1273 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1275 time = get_runtime_sec();
1277 if (BASE_TO_SEC(time) > (MINUTE)) {
1278 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1279 dev_info(di->dev, "low power....\n");
1286 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1290 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1292 return (buf & 0x80) ? 1 : 0;
1295 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1300 rk81x_bat_read(di, GGSTS, &buf, 1);
1301 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1302 /*di->pwron_bat_con = buf;*/
1306 rk81x_bat_write(di, GGSTS, &buf, 1);
1307 rk81x_bat_read(di, GGSTS, &temp, 1);
1308 } while (temp & BAT_CON);
1315 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1322 ocv_table = di->pdata->battery_ocv;
1323 ocv_size = di->pdata->ocv_size;
1325 for (j = 0; j < 21; j++)
1329 for (i = 1; i < ocv_size-1; i++) {
1330 if (ocv_table[i+1] < ocv_table[i] + 20)
1331 temp_table[j++] = i;
1334 temp_table[j] = temp_table[j-1] + 1;
1336 di->enter_flatzone = ocv_table[i];
1339 for (i = 0; i < 20; i++) {
1340 if (temp_table[i] < temp_table[i+1])
1345 di->exit_flatzone = ocv_table[i];
1347 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1348 di->enter_flatzone, di->exit_flatzone);
1351 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1353 u8 ocv_valid, first_pwron;
1357 /*buf==1: OCV_VOL is valid*/
1358 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1359 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1360 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1362 if (first_pwron == 1 || ocv_valid == 1) {
1363 DBG("<%s> enter.\n", __func__);
1364 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1365 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1366 di->dod0_voltage = ocv_voltage;
1367 di->dod0_capacity = di->temp_nac;
1372 di->dod0_level = 100;
1373 else if (ocv_soc < 5)
1374 di->dod0_level = 95;
1375 else if (ocv_soc < 10)
1376 di->dod0_level = 90;
1378 di->dod0_level = 80;
1379 /* save_soc = di->dod0_level; */
1380 soc_level = rk81x_bat_get_level(di);
1381 if (soc_level > di->dod0_level) {
1382 di->dod0_status = 0;
1384 if (soc_level <= 80)
1386 rk81x_bat_save_level(di, soc_level);
1388 di->dod0_status = 1;
1390 di->fcc_update_sec = get_runtime_sec();
1393 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1394 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1396 di->dod0_voltage, di->dod0_capacity,
1397 ocv_soc, soc_level, di->dod0_status,
1403 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1405 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1408 static enum power_supply_property rk_battery_props[] = {
1409 POWER_SUPPLY_PROP_STATUS,
1410 POWER_SUPPLY_PROP_CURRENT_NOW,
1411 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1412 POWER_SUPPLY_PROP_PRESENT,
1413 POWER_SUPPLY_PROP_HEALTH,
1414 POWER_SUPPLY_PROP_CAPACITY,
1417 static int rk81x_battery_get_property(struct power_supply *psy,
1418 enum power_supply_property psp,
1419 union power_supply_propval *val)
1421 struct rk81x_battery *di = to_device_info(psy);
1424 case POWER_SUPPLY_PROP_CURRENT_NOW:
1425 val->intval = di->current_avg * 1000;/*uA*/
1426 if (di->fg_drv_mode == TEST_POWER_MODE)
1427 val->intval = TEST_CURRENT * 1000;
1429 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1430 val->intval = di->voltage * 1000;/*uV*/
1431 if (di->fg_drv_mode == TEST_POWER_MODE)
1432 val->intval = TEST_VOLTAGE * 1000;
1435 case POWER_SUPPLY_PROP_PRESENT:
1436 val->intval = is_rk81x_bat_exist(di);
1437 if (di->fg_drv_mode == TEST_POWER_MODE)
1438 val->intval = TEST_PRESET;
1441 case POWER_SUPPLY_PROP_CAPACITY:
1442 val->intval = di->dsoc;
1443 if (di->fg_drv_mode == TEST_POWER_MODE)
1444 val->intval = TEST_SOC;
1446 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1448 case POWER_SUPPLY_PROP_HEALTH:
1449 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1451 case POWER_SUPPLY_PROP_STATUS:
1452 val->intval = di->psy_status;
1453 if (di->fg_drv_mode == TEST_POWER_MODE)
1454 val->intval = TEST_STATUS;
1464 static enum power_supply_property rk_battery_ac_props[] = {
1465 POWER_SUPPLY_PROP_ONLINE,
1468 static enum power_supply_property rk_battery_usb_props[] = {
1469 POWER_SUPPLY_PROP_ONLINE,
1472 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1473 enum power_supply_property psp,
1474 union power_supply_propval *val)
1477 struct rk81x_battery *di = to_ac_device_info(psy);
1480 case POWER_SUPPLY_PROP_ONLINE:
1481 if (rk81x_chrg_online(di))
1482 rk81x_bat_lowpwr_check(di);
1483 val->intval = di->ac_online; /*discharging*/
1484 if (di->fg_drv_mode == TEST_POWER_MODE)
1485 val->intval = TEST_AC_ONLINE;
1495 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1496 enum power_supply_property psp,
1497 union power_supply_propval *val)
1500 struct rk81x_battery *di = to_usb_device_info(psy);
1503 case POWER_SUPPLY_PROP_ONLINE:
1504 if (rk81x_chrg_online(di))
1505 rk81x_bat_lowpwr_check(di);
1506 val->intval = di->usb_online;
1507 if (di->fg_drv_mode == TEST_POWER_MODE)
1508 val->intval = TEST_USB_ONLINE;
1518 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1522 di->bat.name = "BATTERY";
1523 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1524 di->bat.properties = rk_battery_props;
1525 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1526 di->bat.get_property = rk81x_battery_get_property;
1529 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1530 di->ac.properties = rk_battery_ac_props;
1531 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1532 di->ac.get_property = rk81x_battery_ac_get_property;
1534 di->usb.name = "USB";
1535 di->usb.type = POWER_SUPPLY_TYPE_USB;
1536 di->usb.properties = rk_battery_usb_props;
1537 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1538 di->usb.get_property = rk81x_battery_usb_get_property;
1540 ret = power_supply_register(di->dev, &di->bat);
1542 dev_err(di->dev, "failed to register main battery\n");
1545 ret = power_supply_register(di->dev, &di->usb);
1547 dev_err(di->dev, "failed to register usb power supply\n");
1550 ret = power_supply_register(di->dev, &di->ac);
1552 dev_err(di->dev, "failed to register ac power supply\n");
1559 power_supply_unregister(&di->ac);
1561 power_supply_unregister(&di->usb);
1563 power_supply_unregister(&di->bat);
1568 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1572 static u32 capacity_ma;
1574 if (capacity >= di->qmax)
1575 capacity = di->qmax;
1580 if (capacity_ma == capacity)
1583 capacity_ma = capacity;
1585 buf = (capacity_ma >> 24) & 0xff;
1586 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1587 buf = (capacity_ma >> 16) & 0xff;
1588 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1589 buf = (capacity_ma >> 8) & 0xff;
1590 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1591 buf = (capacity_ma & 0xff) | 0x01;
1592 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1595 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1603 for (i = 0; i < 3; i++) {
1604 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1606 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1607 val[i] |= buf << 16;
1608 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1610 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1614 if (val[0] == val[1])
1622 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1627 capacity_ma = capacity;
1628 buf = (capacity_ma >> 24) & 0xff;
1629 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1630 buf = (capacity_ma >> 16) & 0xff;
1631 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1632 buf = (capacity_ma >> 8) & 0xff;
1633 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1634 buf = (capacity_ma & 0xff) | 0x01;
1635 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1637 dev_info(di->dev, "update fcc : %d\n", capacity);
1640 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1645 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1646 capacity = buf << 24;
1647 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1648 capacity |= buf << 16;
1649 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1650 capacity |= buf << 8;
1651 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1654 if (capacity < MIN_FCC) {
1655 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1657 capacity = di->design_capacity;
1658 rk81x_bat_save_fcc(di, capacity);
1659 } else if (capacity > di->qmax) {
1660 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1661 capacity = di->qmax;
1662 rk81x_bat_save_fcc(di, capacity);
1668 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1677 for (i = 0; i < 3; i++) {
1678 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1680 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1681 val[i] |= buf << 16;
1682 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1684 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1687 if (val[0] == val[1])
1692 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1697 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1701 if (last_soc != save_soc) {
1702 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1703 last_soc = save_soc;
1709 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1714 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1718 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1722 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1723 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1724 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1725 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1728 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1729 enum charger_type charger_type)
1731 switch (charger_type) {
1734 rk81x_bat_set_current(di, ILIM_450MA);
1738 rk81x_bat_set_current(di, di->chrg_i_lmt);
1741 rk81x_bat_set_current(di, ILIM_450MA);
1745 #if defined(CONFIG_ARCH_ROCKCHIP)
1747 static void rk81x_bat_set_charger_param(struct rk81x_battery *di,
1748 enum charger_type charger_type)
1750 rk81x_bat_set_chrg_current(di, charger_type);
1751 rk81x_bat_set_power_supply_state(di, charger_type);
1753 switch (charger_type) {
1755 power_supply_changed(&di->bat);
1759 power_supply_changed(&di->usb);
1762 power_supply_changed(&di->ac);
1769 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1772 enum charger_type charger_type = NO_CHARGER;
1774 if (di->fg_drv_mode == TEST_POWER_MODE) {
1775 charger_type = DC_CHARGER;
1782 if (!gpio_is_valid(di->dc_det_pin))
1785 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1787 pr_err("Failed to request gpio %d with ret:""%d\n",
1788 di->dc_det_pin, ret);
1792 gpio_direction_input(di->dc_det_pin);
1793 ret = gpio_get_value(di->dc_det_pin);
1794 if (ret == di->dc_det_level)
1795 charger_type = DC_CHARGER;
1797 charger_type = NO_CHARGER;
1798 gpio_free(di->dc_det_pin);
1800 return charger_type;
1803 static void rk81x_battery_dc_delay_work(struct work_struct *work)
1805 enum charger_type charger_type;
1806 struct rk81x_battery *di = container_of(work,
1807 struct rk81x_battery, dc_det_check_work.work);
1809 charger_type = rk81x_bat_get_dc_state(di);
1811 if (charger_type == DC_CHARGER)
1812 rk81x_bat_set_charger_param(di, DC_CHARGER);
1813 else/*NO_CHARGER: maybe usb charger still plugin*/
1814 queue_delayed_work(di->wq,
1815 &di->ac_usb_check_work,
1816 msecs_to_jiffies(10));
1819 static void rk81x_battery_acusb_delay_work(struct work_struct *work)
1822 int gadget_flag, usb_id;
1823 struct rk81x_battery *di = container_of(work,
1824 struct rk81x_battery, ac_usb_check_work.work);
1826 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1827 usb_id = dwc_otg_check_dpdm(0);
1830 if ((buf & PLUG_IN_STS) != 0)
1831 rk81x_bat_set_charger_param(di, DC_CHARGER);
1833 rk81x_bat_set_charger_param(di, NO_CHARGER);
1837 rk81x_bat_set_charger_param(di, USB_CHARGER);
1840 rk81x_bat_set_charger_param(di, AC_CHARGER);
1845 /*check unstanderd charger*/
1846 if (usb_id == 1 || usb_id == 3) {
1847 gadget_flag = get_gadget_connect_flag();
1848 if (0 == gadget_flag) {
1850 if (di->check_count >= 5) {
1851 di->check_count = 0;
1852 rk81x_bat_set_charger_param(di, AC_CHARGER);
1854 queue_delayed_work(di->wq,
1855 &di->ac_usb_check_work,
1856 msecs_to_jiffies(1000));
1858 } else {/*confirm: USB_CHARGER*/
1859 di->check_count = 0;
1865 #if defined(CONFIG_X86_INTEL_SOFIA)
1866 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1868 enum charger_type charger_type;
1871 charger_type = AC_CHARGER;
1873 charger_type = USB_CHARGER;
1875 charger_type = NO_CHARGER;
1879 dev_info(di->dev, "limit current:%d\n", ma);
1881 return charger_type;
1884 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1886 struct rk81x_battery *di;
1887 enum charger_type type;
1889 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1890 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1892 rk81x_bat_set_chrg_current(di, type);
1893 power_supply_changed(&di->usb);
1896 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1897 unsigned long event, void *data)
1899 struct rk81x_battery *di;
1900 struct power_supply_cable_props *cable_props;
1901 enum charger_type type;
1903 di = container_of(nb, struct rk81x_battery, usb_nb);
1909 case USB_EVENT_CHARGER:
1910 cable_props = (struct power_supply_cable_props *)data;
1911 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1912 rk81x_bat_set_power_supply_state(di, type);
1913 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1914 msecs_to_jiffies(50));
1925 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1926 unsigned long event, void *data)
1928 struct rk81x_battery *di;
1929 struct fb_event *evdata = data;
1932 di = container_of(nb, struct rk81x_battery, fb_nb);
1934 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1937 blank = *(int *)evdata->data;
1939 if (di->fb_blank != blank)
1940 di->fb_blank = blank;
1944 if (blank == FB_BLANK_UNBLANK)
1945 di->early_resume = 1;
1950 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1952 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1953 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1955 return fb_register_client(&di->fb_nb);
1959 * it is first time for battery to be weld, init by ocv table
1961 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1963 rk81x_bat_save_fcc(di, di->design_capacity);
1964 di->fcc = rk81x_bat_get_fcc(di);
1966 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1967 di->dsoc = di->rsoc;
1968 di->nac = di->temp_nac;
1970 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1971 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1972 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1973 __func__, di->dsoc, di->nac, di->fcc);
1976 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
1981 int relax_vol = di->relax_voltage;
1982 int ocv_vol = di->voltage_ocv;
1984 init_cur = rk81x_bat_get_avg_current(di);
1985 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
1987 est_vol = di->voltage - diff;
1989 if (di->loader_charged) {
1990 calib_vol = est_vol;
1994 if (di->pwroff_min > 8) {
1995 if (abs(relax_vol - ocv_vol) < 100) {
1996 calib_vol = ocv_vol;
1998 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
1999 calib_vol = ocv_vol;
2001 calib_vol = relax_vol;
2003 } else if (di->pwroff_min > 2) {
2004 calib_vol = ocv_vol;
2009 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2010 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2016 * it is not first time for battery to be weld, init by last record info
2018 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2022 int remain_capacity;
2024 int calib_vol, calib_soc, calib_capacity;
2026 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2027 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2028 init_soc = pwron_soc;
2029 remain_capacity = rk81x_bat_get_remain_capacity(di);
2031 /* check if support uboot charge,
2032 * if support, uboot charge driver should have done init work,
2033 * so here we should skip init work
2035 #if defined(CONFIG_ARCH_ROCKCHIP)
2036 if (di->loader_charged) {
2037 dev_info(di->dev, "loader charged\n");
2041 calib_vol = rk81x_bat_get_calib_vol(di);
2042 if (calib_vol > 0) {
2043 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2044 calib_capacity = di->temp_nac;
2046 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2047 init_soc = calib_soc;
2048 remain_capacity = calib_capacity;
2050 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2051 calib_vol, init_soc, remain_capacity);
2054 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2055 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2056 __func__, remain_capacity, di->temp_nac);
2058 if (di->pwroff_min > 0) {
2059 if (di->pwroff_min > 30) {
2060 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2062 remain_capacity = di->temp_nac;
2063 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2064 __func__, remain_capacity);
2066 } else if ((di->pwroff_min > 5) &&
2067 (abs(ocv_soc - init_soc) >= 10)) {
2068 if (remain_capacity >= di->temp_nac * 120/100)
2069 remain_capacity = di->temp_nac * 110/100;
2070 else if (remain_capacity < di->temp_nac * 8/10)
2071 remain_capacity = di->temp_nac * 9/10;
2072 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2073 __func__, remain_capacity);
2076 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2079 di->dsoc = init_soc;
2080 di->nac = remain_capacity;
2083 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2084 pwron_soc, di->dsoc, di->nac);
2087 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2089 u8 curr_pwroff_min, last_pwroff_min;
2091 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2092 &curr_pwroff_min, 1);
2093 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2094 &last_pwroff_min, 1);
2096 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2097 &curr_pwroff_min, 1);
2099 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2102 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2104 u8 calib_en;/*debug*/
2106 di->voltage = rk81x_bat_get_vol(di);
2107 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2108 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2109 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2110 di->current_avg = rk81x_bat_get_avg_current(di);
2112 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2113 di->voltage, di->voltage_ocv, di->relax_voltage,
2114 di->current_avg, di->pwroff_min);
2116 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2117 DBG("readbit: calib_en=%d\n", calib_en);
2118 if (is_rk81x_bat_first_poweron(di) ||
2119 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2120 rk81x_bat_first_pwron(di);
2121 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2124 rk81x_bat_not_first_pwron(di);
2130 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2135 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2140 DBG(" CHARGE-OFF ...\n");
2144 DBG(" DEAD CHARGE ...\n");
2146 case TRICKLE_CHARGE:
2148 DBG(" TRICKLE CHARGE ...\n ");
2152 DBG(" CC or CV ...\n");
2155 ret = CHARGE_FINISH;
2156 DBG(" CHARGE FINISH ...\n");
2160 DBG(" USB OVER VOL ...\n");
2164 DBG(" BAT TMP ERROR ...\n");
2168 DBG(" TIMER ERROR ...\n");
2172 DBG(" USB EXIST ...\n");
2176 DBG(" USB EFF...\n");
2185 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2186 int chrg_ilim, int chrg_cur)
2190 di->chrg_v_lmt = DEF_CHRG_VOL;
2191 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2192 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2194 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2195 if (chrg_vol < CHRG_V_LMT[i])
2198 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2201 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2202 if (chrg_ilim < CHRG_I_LMT[i])
2205 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2208 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2209 if (chrg_cur < CHRG_I_CUR[i])
2212 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2214 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2215 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2218 static u8 rk81x_bat_select_finish_ma(int fcc)
2220 u8 ma = FINISH_150MA;
2225 else if (fcc >= 4000)
2228 else if (fcc >= 3000)
2238 * there is a timer inside rk81x to calc how long the battery is in charging
2239 * state. rk81x will close PowerPath inside IC when timer reach, which will
2240 * stop the charging work. we have to reset the corresponding bits to restart
2241 * the timer to avoid that case.
2243 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2247 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2248 buf &= ~CHRG_TIMER_CCCV_EN;
2249 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2251 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2252 buf |= CHRG_TIMER_CCCV_EN;
2253 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
2254 dev_info(di->dev, "reset cccv charge timer\n");
2258 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2260 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2261 u8 sup_sts_reg, thremal_reg, ggcon;
2262 int chrg_vol, chrg_cur, chrg_ilim;
2265 chrg_vol = di->pdata->max_charger_voltagemV;
2266 chrg_cur = di->pdata->max_charger_currentmA;
2267 chrg_ilim = di->pdata->max_charger_ilimitmA;
2269 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2270 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2272 /*rk81x_bat_init_chrg_timer(di);*/
2274 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2275 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2276 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2277 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2278 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2279 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2280 rk81x_bat_read(di, GGCON, &ggcon, 1);
2282 usb_ctrl_reg &= (~0x0f);
2284 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2285 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2287 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2289 if (di->fg_drv_mode == TEST_POWER_MODE)
2290 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2292 chrg_ctrl_reg1 &= (0x00);
2293 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2295 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2296 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2298 chrg_ctrl_reg2 &= ~(0xc7);
2299 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2301 sup_sts_reg &= ~(0x01 << 3);
2302 sup_sts_reg |= (0x01 << 2);
2304 thremal_reg &= (~0x0c);
2305 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2306 ggcon |= ADC_CURRENT_MODE;
2308 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2309 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2310 /*don't touch charge setting when boot int loader charge mode*/
2311 if (!di->loader_charged)
2312 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2313 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2314 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2315 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2316 rk81x_bat_write(di, GGCON, &ggcon, 1);
2319 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2326 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2328 rk81x_bat_gauge_enable(di);
2329 /* get the volatege offset */
2330 rk81x_bat_get_vol_offset(di);
2331 rk81x_bat_charger_init(di);
2332 rk81x_bat_set_relax_thres(di);
2334 /* get the current offset , the value write to the CAL_OFFSET */
2335 di->current_offset = rk81x_bat_get_ioffset(di);
2336 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2337 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2338 __func__, pcb_offset, di->current_offset);
2340 pcb_offset = DEF_PCB_OFFSET;
2341 cal_offset = pcb_offset + di->current_offset;
2342 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2343 cal_offset = DEF_CAL_OFFSET;
2344 rk81x_bat_set_cal_offset(di, cal_offset);
2345 /* set sample time for cal_offset interval*/
2346 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2348 rk81x_bat_rsoc_init(di);
2349 rk81x_bat_capacity_init(di, di->nac);
2350 rk81x_bat_capacity_init_post(di);
2352 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2353 di->current_avg = rk81x_bat_get_avg_current(di);
2355 rk81x_bat_restart_relax(di);
2356 rk81x_bat_power_on_save(di, di->voltage_ocv);
2358 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2360 rk81x_dbg_dmp_gauge_regs(di);
2361 rk81x_dbg_dmp_charger_regs(di);
2364 "nac = %d , remain_capacity = %d\n"
2365 "OCV_voltage = %d, voltage = %d\n"
2366 "SOC = %d, fcc = %d\n, current=%d\n"
2367 "cal_offset = 0x%x\n",
2369 di->nac, di->remain_capacity,
2370 di->voltage_ocv, di->voltage,
2371 di->dsoc, di->fcc, di->current_avg,
2375 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2377 int dead_voltage, ocv_voltage;
2378 int voltage, voltage_old, voltage_now;
2383 int ocv_soc, dead_soc;
2384 int power_off_thresd = di->pdata->power_off_thresd;
2387 voltage_old = rk81x_bat_get_vol(di);
2389 voltage_now = rk81x_bat_get_vol(di);
2391 } while ((voltage_old == voltage_now) && (count_num < 11));
2392 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2395 for (i = 0; i < 10; i++) {
2396 voltage += rk81x_bat_get_vol(di);
2401 currentnow = rk81x_bat_get_avg_current(di);
2403 /* 50 mo power-path mos */
2404 dead_voltage = power_off_thresd - currentnow *
2405 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2407 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2408 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2409 dead_voltage, ocv_voltage);
2411 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2412 q_dead = di->temp_nac;
2413 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2416 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2417 q_ocv = di->temp_nac;
2418 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2421 rsoc = ocv_soc - dead_soc;
2422 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2424 } else if (rsoc > 0) {
2425 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2428 di->display_soc = di->dsoc * 1000;
2431 di->zero_old_remain_cap = di->remain_capacity;
2433 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2434 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2435 di->line_k, di->dsoc, rsoc,
2436 di->display_soc, di->zero_old_remain_cap);
2439 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2441 int delta_cap, delta_soc;
2444 di->zero_timeout_cnt++;
2445 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2446 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2448 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2449 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2450 "ZERO1: timeout_cnt=%d\n\n",
2451 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2452 delta_soc, delta_cap, di->zero_old_remain_cap,
2453 di->zero_timeout_cnt);
2455 if ((delta_soc >= MIN_ZERO_ACCURACY) ||
2456 (di->zero_timeout_cnt > 500)) {
2457 DBG("ZERO1:--------- enter calc -----------\n");
2458 di->zero_timeout_cnt = 0;
2459 di->display_soc -= delta_soc;
2460 tmp_dsoc = (di->display_soc + MIN_ROUND_ACCURACY) / 1000;
2461 di->dsoc = tmp_dsoc;
2462 /* need to be init, otherwise when switch between discharge and
2463 * charge display_soc will be init as: dsoc * 1000
2465 di->last_zero_mode_dsoc = tmp_dsoc;
2466 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2467 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2469 rk81x_bat_zero_calc_linek(di);
2473 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2475 return (di->voltage -
2476 (di->bat_res * di->current_avg) / 1000);
2479 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2481 int ocv_soc, ocv_voltage;
2483 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2484 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2489 /* we will estimate a ocv voltage to get a ocv soc.
2490 * if there is a big offset between ocv_soc and rsoc,
2491 * we will decide whether we should reinit capacity or not
2493 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2495 int ocv_soc = di->est_ocv_soc;
2496 int ocv_volt = di->est_ocv_vol;
2497 int rsoc = rk81x_bat_get_rsoc(di);
2498 int max_volt = di->pdata->max_charger_voltagemV;
2500 if (ocv_volt > max_volt)
2503 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2504 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2506 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2508 di->err_soc_sum += ocv_soc;
2512 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2513 __func__, di->err_chck_cnt, di->err_soc_sum);
2515 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2516 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2517 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2518 ocv_soc += RSOC_COMPS;
2520 di->temp_nac = ocv_soc * di->fcc / 100;
2521 rk81x_bat_capacity_init(di, di->temp_nac);
2522 rk81x_bat_capacity_init_post(di);
2523 di->rsoc = rk81x_bat_get_rsoc(di);
2524 di->remain_capacity =
2525 rk81x_bat_get_realtime_capacity(di);
2526 di->err_soc_sum = 0;
2527 di->err_chck_cnt = 0;
2528 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2532 di->err_chck_cnt = 0;
2533 di->err_soc_sum = 0;
2537 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2539 u8 status = di->psy_status;
2541 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2542 (status == POWER_SUPPLY_STATUS_FULL)) {
2543 if ((di->current_avg < 0) &&
2544 (di->chrg_status != CHARGE_FINISH))
2545 rk81x_bat_rsoc_dischrg_check(di);
2548 rsoc_chrg_calib(di);
2551 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2552 rk81x_bat_rsoc_dischrg_check(di);
2556 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2558 u32 temp, soc_time = 0;
2559 unsigned long sec_unit;
2561 if (!di->dischrg_emu_base)
2562 di->dischrg_emu_base = get_runtime_sec();
2564 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2566 temp = di->fcc * 3600 / 100;
2568 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2569 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2571 soc_time = temp / div(abs(di->current_avg));
2573 if (sec_unit > soc_time) {
2575 di->dischrg_emu_base = get_runtime_sec();
2576 di->dischrg_save_sec = 0;
2579 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2580 __func__, soc_time, sec_unit);
2584 * when there is a big offset between dsoc and rsoc, dsoc needs to
2585 * speed up to keep pace witch rsoc.
2587 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2589 u32 soc_time = 0, temp;
2591 unsigned long chrg_emu_sec;
2593 if (!di->chrg_emu_base)
2594 di->chrg_emu_base = get_runtime_sec();
2596 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2597 temp = di->fcc * 3600 / 100;
2599 if (di->ac_online) {
2600 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2601 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2603 soc_time = temp / div(abs(di->current_avg));
2605 soc_time = temp / 450;
2608 plus_soc = chrg_emu_sec / soc_time;
2609 if (chrg_emu_sec > soc_time) {
2610 di->dsoc += plus_soc;
2611 di->chrg_emu_base = get_runtime_sec();
2612 di->chrg_save_sec = 0;
2615 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2616 __func__, soc_time, chrg_emu_sec, plus_soc);
2619 /* check voltage and current when dsoc is close to full.
2620 * we will do a fake charge to adjust charing speed which
2621 * aims to make battery full charged and match finish signal.
2623 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2627 unsigned long chrg_term_sec;
2629 if (!di->chrg_term_base)
2630 di->chrg_term_base = get_runtime_sec();
2632 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2633 /*check current and voltage*/
2635 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2637 plus_soc = chrg_term_sec / soc_time;
2638 if (chrg_term_sec > soc_time) {
2639 di->dsoc += plus_soc;
2640 di->chrg_term_base = get_runtime_sec();
2641 di->chrg_save_sec = 0;
2643 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2644 __func__, soc_time, chrg_term_sec, plus_soc);
2647 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2650 int now_current = di->current_avg;
2651 unsigned long dischrg_normal_sec;
2653 if (!di->dischrg_normal_base)
2654 di->dischrg_normal_base = get_runtime_sec();
2656 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2657 di->dischrg_save_sec;
2659 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2660 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2661 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2663 if (di->rsoc == di->dsoc) {
2664 DBG("<%s>. rsoc == dsoc\n", __func__);
2665 di->dsoc = di->rsoc;
2666 di->dischrg_normal_base = get_runtime_sec();
2667 di->dischrg_save_sec = 0;
2668 /*di->discharge_smooth_status = false;*/
2669 } else if (di->rsoc > di->dsoc - 1) {
2670 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2671 if (dischrg_normal_sec > soc_time * 3 / 2) {
2673 di->dischrg_normal_base = get_runtime_sec();
2674 di->dischrg_save_sec = 0;
2676 di->discharge_smooth_status = true;
2678 } else if (di->rsoc < di->dsoc - 1) {
2679 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2680 if (dischrg_normal_sec > soc_time * 3 / 4) {
2682 di->dischrg_normal_base = get_runtime_sec();
2683 di->dischrg_save_sec = 0;
2685 di->discharge_smooth_status = true;
2687 } else if (di->rsoc == di->dsoc - 1) {
2688 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2689 if (di->discharge_smooth_status) {
2690 if (dischrg_normal_sec > soc_time * 3 / 4) {
2692 di->dischrg_normal_base = get_runtime_sec();
2693 di->dischrg_save_sec = 0;
2694 di->discharge_smooth_status = false;
2698 di->dischrg_normal_base = get_runtime_sec();
2699 di->dischrg_save_sec = 0;
2700 di->discharge_smooth_status = false;
2704 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2705 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2706 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2707 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2710 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2715 /* first resume from suspend: we don't run this,
2716 * the sleep_dischrg will handle dsoc, and what
2717 * ever this is fake wakeup or not, we should clean
2718 * zero algorithm mode, or it will handle the dsoc.
2721 rk81x_bat_reset_zero_var(di);
2725 di->rsoc = rk81x_bat_get_rsoc(di);
2727 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2728 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2730 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2731 delta_soc = di->dsoc - di->rsoc;
2733 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2734 di->dischrg_normal_base = 0;
2735 rk81x_bat_emulator_dischrg(di);
2737 di->chrg_emu_base = 0;
2738 rk81x_bat_normal_dischrg(di);
2741 if (di->voltage < ZERO_ALGOR_THRESD) {
2742 di->dischrg_normal_base = 0;
2743 di->chrg_emu_base = 0;
2744 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2745 di->zero_timeout_cnt = 0;
2747 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2748 __func__, di->dsoc, di->last_zero_mode_dsoc);
2749 if (di->dsoc != di->last_zero_mode_dsoc) {
2750 tmp_dsoc = (di->display_soc +
2751 MIN_ROUND_ACCURACY) / 1000;
2752 /* if last display_soc invalid, recalc.
2753 * otherwise keep this value(in case: plugin and
2754 * plugout quickly or wakeup from deep sleep,
2755 * we need't init display_soc)
2757 if (tmp_dsoc != di->dsoc)
2758 /* first init value should round up,
2759 * other wise dsoc will quickly turn to
2760 * dsoc-- if MIN_ROUND_ACCURACY value is
2761 * small,eg:1.(in case: power on system)
2763 di->display_soc = (di->dsoc + 1) *
2764 1000 - MIN_ROUND_ACCURACY;
2765 di->last_zero_mode_dsoc = di->dsoc;
2766 rk81x_bat_zero_calc_linek(di);
2767 DBG("<%s>. first calc, init linek\n", __func__);
2771 rk81x_bat_zero_algorithm(di);
2773 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2774 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2775 di->zero_timeout_cnt = 0;
2776 DBG("<%s>. exit zero_algorithm\n", __func__);
2781 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2784 static int old_index;
2787 int mod = di->dsoc % 10;
2788 int index = di->dsoc / 10;
2790 if (rk81x_chrg_online(di))
2791 time = di->plug_in_min;
2793 time = di->plug_out_min;
2795 if ((mod == 0) && (index > 0) && (old_index != index)) {
2796 di->chrg_min[index-1] = time - old_min;
2801 for (i = 1; i < 11; i++)
2802 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2806 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2808 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2809 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2810 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2813 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2814 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2815 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2816 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2817 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2818 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2819 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2820 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2821 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2822 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2823 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2826 DBG("\n------------- dump_debug_regs -----------------\n"
2827 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2828 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2829 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2830 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2831 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2832 ggcon_reg, ggsts_reg, rtc_val,
2833 sup_tst_reg, vb_mod_reg,
2834 usb_ctrl_reg, chrg_ctrl_reg1,
2835 thremal_reg, misc_reg,
2836 chrg_ctrl_reg2, chrg_ctrl_reg3
2839 DBG("#######################################################\n"
2840 "voltage = %d, current-avg = %d\n"
2841 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2842 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2843 "display_soc = %d, cpapacity_soc = %d\n"
2844 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2845 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2846 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2847 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2848 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2849 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2850 "#########################################################\n",
2851 di->voltage, di->current_avg,
2852 di->fcc, di->remain_capacity, di->voltage_ocv,
2853 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2855 di->ac_online, di->usb_online, di->psy_status,
2856 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2857 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2858 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2859 BASE_TO_SEC(di->chrg_normal_base),
2860 BASE_TO_SEC(di->dischrg_normal_base),
2861 BASE_TO_SEC(di->chrg_term_base),
2862 BASE_TO_SEC(di->chrg_emu_base),
2863 BASE_TO_SEC(di->dischrg_emu_base),
2864 BASE_TO_SEC(di->power_on_base), g_base_sec,
2865 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2869 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2873 int dod0_to_soc100_min;
2875 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2876 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2878 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2879 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2880 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2881 di->dod0_capacity, dod0_to_soc100_min);
2883 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2884 (dod0_to_soc100_min < 1200)) {
2885 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2886 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2888 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2890 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2891 __func__, fcc0, di->fcc);
2893 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2894 di->dod0_status = 0;
2896 rk81x_bat_capacity_init(di, di->fcc);
2897 rk81x_bat_capacity_init_post(di);
2898 rk81x_bat_save_fcc(di, di->fcc);
2899 rk81x_bat_save_level(di, di->dod0_level);
2900 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2903 di->dod0_status = 0;
2907 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2909 if (di->chrg_status == CHARGE_FINISH) {
2910 di->debug_finish_real_soc = di->dsoc;
2911 di->debug_finish_temp_soc = di->rsoc;
2915 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2917 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2920 if ((di->chrg_status == CHARGE_FINISH) &&
2921 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2922 rk81x_bat_update_fcc(di);/* save new fcc*/
2923 ret = rk81x_bat_zero_current_calib(di);
2925 di->enter_finish = false;
2927 rk81x_bat_dbg_get_finish_soc(di);
2931 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2933 unsigned long sec_finish;
2934 int soc_time = 0, plus_soc;
2937 if (di->dsoc < 100) {
2938 if (!di->chrg_finish_base)
2939 di->chrg_finish_base = get_runtime_sec();
2941 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2943 temp = di->fcc * 3600 / 100;
2945 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2947 soc_time = temp / 450;
2949 plus_soc = sec_finish / soc_time;
2950 if (sec_finish > soc_time) {
2951 di->dsoc += plus_soc;
2952 di->chrg_finish_base = get_runtime_sec();
2953 di->chrg_save_sec = 0;
2955 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2956 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2957 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2961 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2964 u32 soc_time, unit_sec;
2966 unsigned long chrg_normal_sec;
2968 now_current = rk81x_bat_get_avg_current(di);
2969 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2971 if (!di->chrg_normal_base)
2972 di->chrg_normal_base = get_runtime_sec();
2974 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2975 di->rsoc = rk81x_bat_get_rsoc(di);
2977 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2978 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2980 if (di->dsoc == di->rsoc) {
2981 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2982 di->rsoc = rk81x_bat_get_rsoc(di);
2983 di->chrg_normal_base = get_runtime_sec();
2984 di->chrg_save_sec = 0;
2985 /*di->charge_smooth_status = false;*/
2986 } else if (di->rsoc < di->dsoc + 1) {
2987 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
2988 unit_sec = soc_time * 3 / 2;
2989 plus_soc = chrg_normal_sec / unit_sec;
2990 if (chrg_normal_sec > unit_sec) {
2991 di->dsoc += plus_soc;
2992 di->chrg_normal_base = get_runtime_sec();
2993 di->chrg_save_sec = 0;
2995 di->charge_smooth_status = true;
2996 } else if (di->rsoc > di->dsoc + 1) {
2997 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
2998 unit_sec = soc_time * 3 / 4;
2999 plus_soc = chrg_normal_sec / unit_sec;
3000 if (chrg_normal_sec > unit_sec) {
3001 di->dsoc += plus_soc;
3002 di->chrg_normal_base = get_runtime_sec();
3003 di->chrg_save_sec = 0;
3005 di->charge_smooth_status = true;
3006 } else if (di->rsoc == di->dsoc + 1) {
3007 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
3008 if (di->charge_smooth_status) {
3009 unit_sec = soc_time * 3 / 4;
3010 if (chrg_normal_sec > unit_sec) {
3011 di->dsoc = di->rsoc;
3012 di->chrg_normal_base = get_runtime_sec();
3013 di->charge_smooth_status = false;
3014 di->chrg_save_sec = 0;
3017 di->dsoc = di->rsoc;
3018 di->chrg_normal_base = get_runtime_sec();
3019 di->charge_smooth_status = false;
3020 di->chrg_save_sec = 0;
3024 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3025 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3026 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3027 chrg_normal_sec, soc_time, plus_soc);
3030 static void rk81x_bat_update_time(struct rk81x_battery *di)
3034 runtime_sec = get_runtime_sec();
3036 /*update by charger type*/
3037 if (rk81x_chrg_online(di))
3038 di->plug_out_base = runtime_sec;
3040 di->plug_in_base = runtime_sec;
3042 /*update by current*/
3043 if (di->chrg_status != CHARGE_FINISH) {
3044 di->finish_sig_base = runtime_sec;
3045 di->chrg_finish_base = runtime_sec;
3048 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3049 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3050 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3052 rk81x_bat_dbg_time_table(di);
3055 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3057 int trend_start_cap = di->trend_start_cap;
3058 int remain_cap = di->remain_capacity;
3062 if (di->s2r && !di->slp_psy_status)
3063 di->trend_start_cap = di->remain_capacity;
3065 diff_cap = remain_cap - trend_start_cap;
3066 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3067 __func__, trend_start_cap, diff_cap);
3068 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3070 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3071 di->trend_start_cap = di->remain_capacity;
3072 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3073 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3075 state = TREND_STAT_FLAT;
3081 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3083 int state, soc_time;
3084 static int trend_down_cnt, trend_up_cnt;
3085 int trend_cnt_thresd;
3086 int now_current = di->current_avg;
3089 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3090 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3091 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3092 __func__, state, trend_mult);
3093 if (di->chrg_status == CHARGE_FINISH)
3096 if (state == TREND_STAT_UP) {
3097 rk81x_bat_reset_zero_var(di);
3099 trend_up_cnt += trend_mult;
3100 if (trend_up_cnt >= trend_cnt_thresd) {
3102 di->dischrg_save_sec = 0;
3104 } else if (state == TREND_STAT_DOWN) {
3106 trend_down_cnt += trend_mult;
3107 if (trend_down_cnt >= trend_cnt_thresd) {
3109 di->chrg_save_sec = 0;
3113 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3114 if ((di->chrg_save_sec * 3 / 4 > soc_time) &&
3115 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3117 di->chrg_save_sec = 0;
3119 else if ((di->dischrg_save_sec * 3 / 4 > soc_time) &&
3120 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3122 di->dischrg_save_sec = 0;
3124 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3125 "up_cnt=%d, down_cnt=%d\n",
3126 __func__, state, trend_cnt_thresd, soc_time,
3127 trend_up_cnt, trend_down_cnt);
3130 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3132 u32 *ocv_table = di->pdata->battery_ocv;
3133 int delta_soc = di->rsoc - di->dsoc;
3135 if (di->chrg_status == CHARGE_FINISH ||
3136 di->slp_chrg_status == CHARGE_FINISH) {
3137 /*clear sleep charge status*/
3138 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3139 di->chrg_emu_base = 0;
3140 di->chrg_normal_base = 0;
3141 di->chrg_term_base = 0;
3142 rk81x_bat_finish_chrg(di);
3143 rk81x_bat_capacity_init(di, di->fcc);
3144 rk81x_bat_capacity_init_post(di);
3145 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3146 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3147 (di->voltage < ocv_table[18] + 20))) {
3148 di->chrg_emu_base = 0;
3149 di->chrg_normal_base = 0;
3150 di->chrg_finish_base = 0;
3151 rk81x_bat_terminal_chrg(di);
3152 } else if (di->chrg_status != CHARGE_FINISH &&
3153 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3154 di->chrg_term_base = 0;
3155 di->chrg_normal_base = 0;
3156 di->chrg_finish_base = 0;
3157 rk81x_bat_emulator_chrg(di);
3159 di->chrg_emu_base = 0;
3160 di->chrg_term_base = 0;
3161 di->chrg_finish_base = 0;
3162 rk81x_bat_normal_chrg(di);
3166 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3168 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3169 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3171 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3172 dischrg_normal_sec, dischrg_emu_sec);
3174 return (dischrg_normal_sec > dischrg_emu_sec) ?
3175 dischrg_normal_sec : dischrg_emu_sec;
3178 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3180 unsigned long sec1, sec2;
3181 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3182 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3183 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3184 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3186 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3187 chrg_normal_sec : chrg_term_sec;
3189 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3190 chrg_emu_sec : chrg_finish_sec;
3191 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3192 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3193 chrg_normal_sec, chrg_term_sec,
3194 chrg_emu_sec, chrg_finish_sec);
3196 return (sec1 > sec2) ? sec1 : sec2;
3199 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3201 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3202 if (di->current_mode == DISCHRG_MODE) {
3203 di->current_mode = CHRG_MODE;
3204 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3205 di->dischrg_normal_base = 0;
3206 di->dischrg_emu_base = 0;
3207 if (di->chrg_status == CHARGE_FINISH)
3208 di->dischrg_save_sec = 0;
3209 if ((di->chrg_status == CHARGE_FINISH) &&
3211 di->chrg_save_sec = 0;
3213 DBG("<%s>---dischrg_save_sec = %lu\n",
3214 __func__, di->dischrg_save_sec);
3217 if (!rk81x_chrg_online(di)) {
3218 dev_err(di->dev, "discharge, current error:%d\n",
3221 rk81x_bat_chrg_smooth(di);
3222 di->discharge_smooth_status = true;
3225 if (di->current_mode == CHRG_MODE) {
3226 di->current_mode = DISCHRG_MODE;
3227 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3228 di->chrg_normal_base = 0;
3229 di->chrg_emu_base = 0;
3230 di->chrg_term_base = 0;
3231 di->chrg_finish_base = 0;
3232 DBG("<%s>---chrg_save_sec = %lu\n",
3233 __func__, di->chrg_save_sec);
3235 rk81x_bat_dischrg_smooth(di);
3236 di->charge_smooth_status = true;
3241 * update rsoc by relax voltage
3243 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3245 int relax_vol = di->relax_voltage;
3246 int ocv_soc, capacity;
3248 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3249 capacity = (ocv_soc * di->fcc / 100);
3250 rk81x_bat_capacity_init(di, capacity);
3251 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3252 di->rsoc = rk81x_bat_get_rsoc(di);
3253 rk81x_bat_capacity_init_post(di);
3254 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3258 * 1: must do it, 0: when necessary
3260 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3262 int ocv_vol = di->est_ocv_vol;
3263 int ocv_soc = 0, capacity = 0;
3265 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3266 capacity = (ocv_soc * di->fcc / 100);
3267 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3268 rk81x_bat_capacity_init(di, capacity);
3269 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3270 di->rsoc = rk81x_bat_get_rsoc(di);
3271 rk81x_bat_capacity_init_post(di);
3272 DBG("<%s>, rsoc updated!\n", __func__);
3274 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3275 __func__, ocv_vol, ocv_soc, capacity);
3278 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3282 unsigned long sleep_sec = di->suspend_time_sum;
3283 int power_off_thresd = di->pdata->power_off_thresd;
3285 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3286 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3287 __func__, di->dsoc, di->rsoc,
3288 di->relax_voltage, di->voltage, sleep_sec / 60);
3290 if (di->relax_voltage >= di->voltage) {
3291 rk81x_bat_relax_vol_calib(di);
3292 rk81x_bat_restart_relax(di);
3294 /* current_avg < 0: make sure the system is not
3295 * wakeup by charger plugin.
3297 /* even if relax voltage is not caught rightly, realtime voltage
3298 * is quite close to relax voltage, we should not do nothing after
3302 rk81x_bat_vol_calib(di, 1);
3306 if (di->dsoc <= di->rsoc) {
3307 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3308 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3309 temp_dsoc = di->dsoc - delta_soc;
3311 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3312 di->rsoc, di->dsoc, delta_soc);
3314 if (delta_soc > 0) {
3315 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3317 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3319 else if (temp_dsoc > 5)
3320 di->dsoc = temp_dsoc;
3323 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3324 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3326 /*di->dsoc > di->rsoc*/
3327 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3328 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3329 temp_dsoc = di->dsoc - di->rsoc;
3331 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3332 di->rsoc, di->dsoc, delta_soc);
3334 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3335 (temp_dsoc > delta_soc))
3336 di->dsoc -= delta_soc;
3338 di->dsoc = di->rsoc;
3340 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3341 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3344 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3350 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3351 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3356 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3359 unsigned long sleep_sec;
3361 sleep_sec = di->suspend_time_sum;
3362 if (((di->suspend_charge_current < 800) &&
3363 (di->ac_online == ONLINE)) ||
3364 (di->chrg_status == CHARGE_FINISH)) {
3365 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3366 if (sleep_sec > 0) {
3367 /*default charge current: 1000mA*/
3368 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3369 / 3600 / div(di->fcc);
3372 DBG("<%s>, usb charge\n", __func__);
3379 * only do report when there is a change.
3381 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3382 * when dsoc == 0, we must do report. But it will generate too much android
3383 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3386 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3389 static u32 old_ac_status;
3390 static u32 old_usb_status;
3391 static u32 old_charge_status;
3394 state_changed = false;
3395 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3396 state_changed = true;
3397 else if (di->dsoc != old_soc)
3398 state_changed = true;
3399 else if (di->ac_online != old_ac_status)
3400 state_changed = true;
3401 else if (di->usb_online != old_usb_status)
3402 state_changed = true;
3403 else if (old_charge_status != di->psy_status)
3404 state_changed = true;
3406 if (rk81x_chrg_online(di)) {
3407 if (di->dsoc == 100)
3408 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3410 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3413 if (state_changed) {
3414 power_supply_changed(&di->bat);
3415 power_supply_changed(&di->usb);
3416 power_supply_changed(&di->ac);
3418 old_ac_status = di->ac_online;
3419 old_usb_status = di->usb_online;
3420 old_charge_status = di->psy_status;
3421 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3422 di->dsoc, di->rsoc);
3427 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3431 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3434 return CHRG_CVCC_HOUR[hour];
3437 /* we have to estimate the charging finish time from now, to decide
3438 * whether we should reset the timer or not.
3440 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3443 int remain_capacity;
3445 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3446 if (di->dsoc < di->rsoc)
3447 remain_capacity = di->dsoc * di->fcc / 100;
3449 remain_capacity = di->remain_capacity;
3451 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3452 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3454 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3455 di->chrg_cap2full = di->fcc - remain_capacity;
3456 if (di->current_avg <= 0)
3457 di->current_avg = 1;
3459 di->chrg_time2full = di->chrg_cap2full * 3600 /
3460 div(abs(di->current_avg));
3462 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3463 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3466 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3467 /*rk81x_bat_init_chrg_timer(di);*/
3468 di->plug_in_base = get_runtime_sec();
3469 DBG("%s: reset charge timer\n", __func__);
3476 * in case that we will do reboot stress test, we need a special way
3477 * to ajust the dsoc.
3479 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3487 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3490 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3491 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3493 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3494 __func__, cnt, unit_time, smooth_time,
3495 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3497 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3498 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3499 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3504 rk81x_bat_save_dsoc(di, dsoc);
3507 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3508 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3513 rk81x_bat_save_dsoc(di, dsoc);
3517 rk81x_bat_save_reboot_cnt(di, cnt);
3520 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3525 uint16_t cal_offset;
3526 u8 pcb_offset = DEF_PCB_OFFSET;
3528 min = BASE_TO_MIN(di->power_on_base);
3529 if ((min % 8) && (old_min != min)) {
3531 rk81x_bat_get_vol_offset(di);
3532 if (di->pcb_ioffset_updated)
3533 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3535 current_offset = rk81x_bat_get_ioffset(di);
3536 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3537 cal_offset = rk81x_bat_get_cal_offset(di);
3538 if (cal_offset < 0x7ff)
3539 rk81x_bat_set_cal_offset(di, di->current_offset +
3541 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3542 __func__, di->voltage_k, di->voltage_b, cal_offset,
3543 rk81x_bat_get_ioffset(di));
3547 static void rk81x_bat_update_info(struct rk81x_battery *di)
3551 else if (di->dsoc < 0)
3555 * we need update fcc in continuous charging state, if discharge state
3556 * keep at least 2 hour, we decide not to update fcc, so clear the
3557 * fcc update flag: dod0_status.
3559 if (BASE_TO_MIN(di->plug_out_base) > 120)
3560 di->dod0_status = 0;
3562 di->voltage = rk81x_bat_get_vol(di);
3563 di->current_avg = rk81x_bat_get_avg_current(di);
3564 di->chrg_status = rk81x_bat_get_chrg_status(di);
3565 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3566 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3567 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3568 /*rk81x_bat_chrg_over_time_check(di);*/
3569 rk81x_bat_update_calib_param(di);
3570 if (di->chrg_status == CC_OR_CV)
3571 di->enter_finish = true;
3573 if (!rk81x_chrg_online(di) && di->s2r)
3576 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3577 if (di->remain_capacity > di->fcc) {
3578 rk81x_bat_capacity_init(di, di->fcc);
3579 rk81x_bat_capacity_init_post(di);
3580 di->remain_capacity = di->fcc;
3583 di->rsoc = rk81x_bat_get_rsoc(di);
3586 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3588 if (di->slp_psy_status)
3589 return rk81x_bat_sleep_chrg(di);
3591 return rk81x_bat_sleep_dischrg(di);
3594 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3596 u8 ocv_soc, soc_level;
3597 int relax_vol = di->relax_voltage;
3602 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3603 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3604 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3606 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3607 (di->dod0_status == 0) &&
3609 di->dod0_voltage = relax_vol;
3610 di->dod0_capacity = di->temp_nac;
3615 di->dod0_level = 100;
3616 else if (ocv_soc < 5)
3617 di->dod0_level = 90;
3619 di->dod0_level = 80;
3621 /* save_soc = di->dod0_level; */
3622 soc_level = rk81x_bat_get_level(di);
3623 if (soc_level > di->dod0_level) {
3624 di->dod0_status = 0;
3626 di->dod0_status = 1;
3628 di->fcc_update_sec = get_runtime_sec();
3631 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3632 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3634 di->dod0_voltage, di->dod0_capacity,
3635 ocv_soc, soc_level, di->dod0_status,
3640 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3645 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3648 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3650 dev_info(di->dev, "set charge to %s termination mode\n",
3651 mode ? "digital" : "analog");
3654 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3656 struct rk81x_battery *di;
3658 di = container_of(work, struct rk81x_battery,
3659 chrg_term_mode_switch_work.work);
3661 if (rk81x_chrg_online(di))
3662 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3664 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3667 static void rk81x_battery_work(struct work_struct *work)
3669 struct rk81x_battery *di;
3670 int ms = TIMER_MS_COUNTS;
3672 di = container_of(work, struct rk81x_battery,
3673 battery_monitor_work.work);
3674 if (rk81x_chrg_online(di)) {
3675 rk81x_bat_wait_finish_sig(di);
3676 /*rk81x_bat_chrg_finish_routine(di);*/
3678 rk81x_bat_fcc_flag_check(di);
3679 rk81x_bat_arbitrate_rsoc_trend(di);
3680 rk81x_bat_display_smooth(di);
3681 rk81x_bat_update_time(di);
3682 rk81x_bat_update_info(di);
3683 rk81x_bat_rsoc_check(di);
3684 rk81x_bat_power_supply_changed(di);
3685 rk81x_bat_save_dsoc(di, di->dsoc);
3686 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3688 rk81x_bat_dbg_dmp_info(di);
3690 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3691 ms = 30 * TIMER_MS_COUNTS;
3693 di->early_resume = 0;
3697 queue_delayed_work(di->wq, &di->battery_monitor_work,
3698 msecs_to_jiffies(ms));
3701 #if defined(CONFIG_ARCH_ROCKCHIP)
3702 static void rk81x_battery_otg_delay_work(struct work_struct *work)
3704 struct rk81x_battery *di = container_of(work,
3705 struct rk81x_battery, otg_check_work.work);
3707 enum bc_port_type event = di->charge_otg;
3709 /* do not touch CHRG_CTRL_REG1[7]: CHRG_EN, hardware can
3710 * recognize otg plugin and will auto ajust this bit
3713 case USB_OTG_POWER_ON:
3714 dev_info(di->dev, "charge disable, otg enable\n");
3715 di->otg_online = ONLINE;
3716 /*rk81x_bat_set_charger_param(di, NO_CHARGER);*/
3717 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3718 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3719 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_EN);
3722 case USB_OTG_POWER_OFF:
3723 dev_info(di->dev, "charge enable, otg disable\n");
3724 di->otg_online = OFFLINE;
3725 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3726 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3727 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_DIS);
3728 /*maybe dc still plugin*/
3729 queue_delayed_work(di->wq, &di->dc_det_check_work,
3730 msecs_to_jiffies(10));
3738 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3740 int register_battery_notifier(struct notifier_block *nb)
3742 return blocking_notifier_chain_register(&battery_chain_head, nb);
3744 EXPORT_SYMBOL_GPL(register_battery_notifier);
3746 int unregister_battery_notifier(struct notifier_block *nb)
3748 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3750 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3752 int battery_notifier_call_chain(unsigned long val)
3754 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3755 == NOTIFY_BAD) ? -EINVAL : 0;
3757 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3759 static int rk81x_bat_usb_notifier_call(struct notifier_block *nb,
3760 unsigned long event, void *data)
3762 enum charger_type charger_type;
3763 struct rk81x_battery *di =
3764 container_of(nb, struct rk81x_battery, battery_nb);
3766 if (di->fg_drv_mode == TEST_POWER_MODE)
3769 /*if dc is pluging, ignore usb*/
3770 charger_type = rk81x_bat_get_dc_state(di);
3771 if ((charger_type == DC_CHARGER) &&
3772 (event != USB_OTG_POWER_OFF) &&
3773 (event != USB_OTG_POWER_ON))
3777 case USB_BC_TYPE_DISCNT:/*maybe dc still plugin*/
3778 queue_delayed_work(di->wq, &di->dc_det_check_work,
3779 msecs_to_jiffies(10));
3781 case USB_BC_TYPE_SDP:
3782 case USB_BC_TYPE_CDP:/*nonstandard charger*/
3783 case USB_BC_TYPE_DCP:/*standard charger*/
3784 queue_delayed_work(di->wq, &di->ac_usb_check_work,
3785 msecs_to_jiffies(10));
3787 case USB_OTG_POWER_ON:/*otg on*/
3788 di->charge_otg = USB_OTG_POWER_ON;
3789 queue_delayed_work(di->wq, &di->otg_check_work,
3790 msecs_to_jiffies(10));
3792 case USB_OTG_POWER_OFF:/*otg off*/
3793 di->charge_otg = USB_OTG_POWER_OFF;
3794 queue_delayed_work(di->wq, &di->otg_check_work,
3795 msecs_to_jiffies(10));
3803 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3805 pr_info("\n------- %s:lower power warning!\n", __func__);
3807 rk_send_wakeup_key();
3812 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3814 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3815 rk_send_wakeup_key();
3819 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3821 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3822 rk_send_wakeup_key();
3826 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3828 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3830 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3831 di->finish_sig_base = get_runtime_sec();
3832 rk_send_wakeup_key();
3836 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3838 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3840 queue_delayed_work(di->wq,
3841 &di->dc_det_check_work,
3842 msecs_to_jiffies(10));
3843 rk_send_wakeup_key();
3848 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3853 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3854 ret = sysfs_create_file(&di->bat.dev->kobj,
3855 &rk818_bat_attr[i].attr);
3857 dev_err(di->dev, "create battery node(%s) error\n",
3858 rk818_bat_attr[i].attr.name);
3864 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3866 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3868 struct rk818 *chip = di->rk818;
3870 #if defined(CONFIG_X86_INTEL_SOFIA)
3871 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3872 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3873 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3874 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3876 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3877 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3878 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3879 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3882 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3883 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3885 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3887 di->irq = vb_lo_irq;
3888 enable_irq_wake(di->irq);
3890 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3891 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3894 dev_err(chip->dev, "plug_in_irq request failed!\n");
3896 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3897 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3900 dev_err(chip->dev, "plug_out_irq request failed!\n");
3902 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3903 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3906 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3909 static void rk81x_bat_info_init(struct rk81x_battery *di,
3913 unsigned long time_base = get_runtime_sec();
3915 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3916 if (val & PLUG_IN_STS)
3917 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3919 di->cell.config = di->pdata->cell_cfg;
3920 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3921 di->qmax = di->pdata->cell_cfg->design_qmax;
3922 di->early_resume = 1;
3923 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3924 di->bat_res = di->pdata->sense_resistor_mohm;
3925 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3926 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3927 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3928 di->loader_charged = loader_charged;
3929 di->chrg_finish_base = time_base;
3930 di->power_on_base = time_base;
3931 di->plug_in_base = time_base;
3932 di->plug_out_base = time_base;
3933 di->finish_sig_base = time_base;
3934 di->fcc = rk81x_bat_get_fcc(di);
3937 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3938 struct device_node *np)
3940 struct device *dev = di->dev;
3941 enum of_gpio_flags flags;
3944 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3945 if (di->dc_det_pin == -EPROBE_DEFER) {
3946 dev_err(dev, "dc_det_gpio error\n");
3950 if (gpio_is_valid(di->dc_det_pin)) {
3951 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3952 RK818_DC_IN : RK818_DC_OUT;
3953 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3955 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3956 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3957 "rk81x_dc_det", di);
3959 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3962 enable_irq_wake(di->dc_det_irq);
3967 gpio_free(di->dc_det_pin);
3970 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3975 struct timespec tv = {
3976 .tv_nsec = NSEC_PER_SEC >> 1,
3978 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3980 err = rtc_read_time(rtc, &tm);
3982 dev_err(rtc->dev.parent,
3983 "hctosys: unable to read the hardware clock\n");
3985 err = rtc_valid_tm(&tm);
3987 dev_err(rtc->dev.parent,
3988 "hctosys: invalid date/time\n");
3991 rtc_tm_to_time(&tm, &tv.tv_sec);
3992 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3998 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4000 struct device_node *np;
4001 struct battery_platform_data *pdata;
4002 struct cell_config *cell_cfg;
4003 struct ocv_config *ocv_cfg;
4004 struct property *prop;
4005 struct rk818 *rk818 = di->rk818;
4006 struct device *dev = di->dev;
4011 np = of_find_node_by_name(rk818->dev->of_node, "battery");
4013 dev_err(dev, "battery node not found!\n");
4017 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
4021 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
4025 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4029 prop = of_find_property(np, "ocv_table", &length);
4031 dev_err(dev, "ocv_table not found!\n");
4034 pdata->ocv_size = length / sizeof(u32);
4035 if (pdata->ocv_size <= 0) {
4036 dev_err(dev, "invalid ocv table\n");
4040 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4042 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4043 if (!pdata->battery_ocv)
4046 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4051 /******************** charger param ****************************/
4052 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4054 dev_err(dev, "max_chrg_currentmA not found!\n");
4055 out_value = DEFAULT_CHRG_CUR;
4057 pdata->max_charger_currentmA = out_value;
4059 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4061 dev_err(dev, "max_charger_ilimitmA not found!\n");
4062 out_value = DEFAULT_INPUT_CUR;
4064 pdata->max_charger_ilimitmA = out_value;
4066 ret = of_property_read_u32(np, "bat_res", &out_value);
4068 dev_err(dev, "bat_res not found!\n");
4069 out_value = DEFAULT_BAT_RES;
4071 pdata->sense_resistor_mohm = out_value;
4073 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4075 dev_err(dev, "max_charge_voltagemV not found!\n");
4076 out_value = DEFAULT_CHRG_VOL;
4078 pdata->max_charger_voltagemV = out_value;
4080 ret = of_property_read_u32(np, "design_capacity", &out_value);
4082 dev_err(dev, "design_capacity not found!\n");
4085 cell_cfg->design_capacity = out_value;
4087 ret = of_property_read_u32(np, "design_qmax", &out_value);
4089 dev_err(dev, "design_qmax not found!\n");
4092 cell_cfg->design_qmax = out_value;
4094 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4096 dev_err(dev, "sleep_enter_current not found!\n");
4097 out_value = DEFAULT_SLP_ENTER_CUR;
4099 ocv_cfg->sleep_enter_current = out_value;
4101 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4103 dev_err(dev, "sleep_exit_current not found!\n");
4104 out_value = DEFAULT_SLP_EXIT_CUR;
4106 ocv_cfg->sleep_exit_current = out_value;
4108 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4110 dev_warn(dev, "power_off_thresd not found!\n");
4111 out_value = PWR_OFF_THRESD;
4113 pdata->power_off_thresd = out_value;
4115 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4116 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4117 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4119 /************* charger support adp types **********************/
4120 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4121 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4123 if (!support_usb_adp && !support_dc_adp) {
4124 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4125 support_usb_adp = 1;
4128 /*if (support_dc_adp)*/
4129 rk81x_bat_dc_det_init(di, np);
4131 cell_cfg->ocv = ocv_cfg;
4132 pdata->cell_cfg = cell_cfg;
4135 DBG("\nthe battery dts info dump:\n"
4137 "max_input_currentmA:%d\n"
4138 "max_chrg_currentmA:%d\n"
4139 "max_charge_voltagemV:%d\n"
4140 "design_capacity:%d\n"
4142 "sleep_enter_current:%d\n"
4143 "sleep_exit_current:%d\n"
4144 "support_usb_adp:%d\n"
4145 "support_dc_adp:%d\n"
4146 "power_off_thresd:%d\n",
4147 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4148 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4149 cell_cfg->design_capacity, cell_cfg->design_qmax,
4150 cell_cfg->ocv->sleep_enter_current,
4151 cell_cfg->ocv->sleep_exit_current,
4152 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4158 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4164 static int rk81x_battery_probe(struct platform_device *pdev)
4166 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4167 struct rk81x_battery *di;
4170 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4174 di->dev = &pdev->dev;
4175 platform_set_drvdata(pdev, di);
4177 ret = rk81x_bat_parse_dt(di);
4179 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4183 rk81x_bat_info_init(di, chip);
4184 if (!is_rk81x_bat_exist(di)) {
4185 dev_info(di->dev, "not battery, enter test power mode\n");
4186 di->fg_drv_mode = TEST_POWER_MODE;
4189 ret = rk81x_bat_power_supply_init(di);
4191 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4195 rk81x_bat_irq_init(di);
4196 rk81x_bat_sysfs_init(di);
4198 rk81x_bat_fg_init(di);
4199 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4201 rk81x_bat_flatzone_vol_init(di);
4203 #if defined(CONFIG_X86_INTEL_SOFIA)
4204 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4205 if (IS_ERR_OR_NULL(di->usb_phy)) {
4206 dev_err(di->dev, "get usb phy failed\n");
4207 return PTR_ERR(di->usb_phy);
4209 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4210 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4212 dev_err(di->dev, "registr usb phy notification failed\n");
4213 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4214 rk81x_battery_usb_notifier_delayed_work);
4217 rk81x_battery_register_fb_notify(di);
4218 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4219 "rk81x-battery-work");
4220 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4221 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4222 rk81x_chrg_term_mode_switch_work);
4224 queue_delayed_work(di->wq, &di->battery_monitor_work,
4225 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4227 #if defined(CONFIG_ARCH_ROCKCHIP)
4228 INIT_DELAYED_WORK(&di->otg_check_work,
4229 rk81x_battery_otg_delay_work);
4230 INIT_DELAYED_WORK(&di->ac_usb_check_work,
4231 rk81x_battery_acusb_delay_work);
4232 INIT_DELAYED_WORK(&di->dc_det_check_work,
4233 rk81x_battery_dc_delay_work);
4235 queue_delayed_work(di->wq, &di->dc_det_check_work,
4236 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4238 di->battery_nb.notifier_call = rk81x_bat_usb_notifier_call;
4239 rk_bc_detect_notifier_register(&di->battery_nb, &di->charge_otg);
4241 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4246 static int rk81x_battery_suspend(struct platform_device *dev,
4249 struct rk81x_battery *di = platform_get_drvdata(dev);
4251 /*while otg and dc both plugin*/
4252 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4254 di->slp_psy_status = rk81x_chrg_online(di);
4255 di->chrg_status = rk81x_bat_get_chrg_status(di);
4256 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4257 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4258 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4259 di->dischrg_normal_base = 0;
4260 di->dischrg_emu_base = 0;
4261 do_gettimeofday(&di->suspend_rtc_base);
4263 if (!rk81x_chrg_online(di)) {
4264 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4265 di->chrg_normal_base = 0;
4266 di->chrg_emu_base = 0;
4267 di->chrg_term_base = 0;
4268 di->chrg_finish_base = 0;
4273 * do not modify the g_base_sec
4275 g_base_sec = get_runtime_sec();
4277 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4278 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4279 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4284 static int rk81x_battery_resume(struct platform_device *dev)
4286 struct rk81x_battery *di = platform_get_drvdata(dev);
4287 int pwroff_thresd = di->pdata->power_off_thresd;
4293 /*while otg and dc both plugin*/
4294 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4296 di->discharge_smooth_status = true;
4297 di->charge_smooth_status = true;
4299 vol = rk81x_bat_get_vol(di);
4300 if (vol < INVALID_VOL_THRESD) {
4301 dev_err(di->dev, "invalid voltage :%d", vol);
4306 di->current_avg = rk81x_bat_get_avg_current(di);
4307 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4308 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4309 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4310 delta_time = rk81x_bat_get_suspend_sec(di);
4311 di->suspend_time_sum += delta_time;
4312 #if defined(CONFIG_ARCH_ROCKCHIP)
4313 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4316 if (di->slp_psy_status) {
4317 time_step = CHRG_TIME_STEP;
4319 if (di->voltage <= pwroff_thresd + 50)
4320 time_step = DISCHRG_TIME_STEP_0;
4322 time_step = DISCHRG_TIME_STEP_1;
4325 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4326 di->current_avg, di->voltage, di->est_ocv_vol,
4327 di->relax_voltage, delta_time, di->suspend_time_sum,
4328 di->slp_psy_status);
4330 if (di->suspend_time_sum > time_step) {
4331 delta_soc = rk81x_bat_update_resume_state(di);
4333 di->suspend_time_sum = 0;
4336 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4337 rk81x_chrg_online(di))
4338 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4341 * do not modify the g_base_sec
4343 if (is_local_clock_reset())
4344 g_base_sec += delta_time;
4351 static int rk81x_battery_remove(struct platform_device *dev)
4353 struct rk81x_battery *di = platform_get_drvdata(dev);
4355 cancel_delayed_work_sync(&di->battery_monitor_work);
4359 static void rk81x_battery_shutdown(struct platform_device *dev)
4361 struct rk81x_battery *di = platform_get_drvdata(dev);
4363 cancel_delayed_work_sync(&di->battery_monitor_work);
4364 rk_bc_detect_notifier_unregister(&di->battery_nb);
4366 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4367 rk81x_bat_check_reboot(di);
4369 rk81x_bat_save_reboot_cnt(di, 0);
4370 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4373 static struct platform_driver rk81x_battery_driver = {
4375 .name = "rk818-battery",
4376 .owner = THIS_MODULE,
4379 .probe = rk81x_battery_probe,
4380 .remove = rk81x_battery_remove,
4381 .suspend = rk81x_battery_suspend,
4382 .resume = rk81x_battery_resume,
4383 .shutdown = rk81x_battery_shutdown,
4386 static int __init battery_init(void)
4388 return platform_driver_register(&rk81x_battery_driver);
4391 fs_initcall_sync(battery_init);
4392 static void __exit battery_exit(void)
4394 platform_driver_unregister(&rk81x_battery_driver);
4396 module_exit(battery_exit);
4398 MODULE_LICENSE("GPL");
4399 MODULE_ALIAS("platform:rk818-battery");
4400 MODULE_AUTHOR("ROCKCHIP");