2 * rk818/rk819 battery driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd
5 * Author: zhangqing <zhangqing@rock-chips.com>
6 * chenjh <chenjh@rock-chips.com>
7 * Andy Yan <andy.yan@rock-chips.com>
9 * Copyright (C) 2008-2009 Texas Instruments, Inc.
10 * Author: Texas Instruments, Inc.
12 * Copyright (C) 2008-2009 Texas Instruments, Inc.
13 * Author: Texas Instruments, Inc.
14 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
15 * Author: zhangqing <zhangqing@rock-chips.com>
16 * Copyright (C) 2014-2015 Intel Mobile Communications GmbH
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
40 #include <linux/gpio.h>
41 #include <linux/proc_fs.h>
42 #include <linux/uaccess.h>
43 #include <linux/mfd/rk818.h>
44 #include <linux/time.h>
45 #include <linux/interrupt.h>
46 #include <linux/rtc.h>
47 #include <linux/wakelock.h>
48 #include <linux/of_gpio.h>
49 #include <linux/irq.h>
50 #include <linux/irqdomain.h>
51 #include <linux/usb/phy.h>
54 #if defined(CONFIG_X86_INTEL_SOFIA)
55 #include <linux/usb/phy-intel.h>
57 #include <linux/power/rk_usbbc.h>
59 #include "rk818_battery.h"
61 /* if you want to disable, don't set it as 0,
62 just be: "static int dbg_enable;" is ok*/
64 static int dbg_enable;
65 #define RK818_SYS_DBG 1
67 module_param_named(dbg_level, dbg_enable, int, 0644);
69 #define DBG(args...) \
76 #define DEFAULT_BAT_RES 135
77 #define DEFAULT_CHRG_VOL 4200
78 #define DEFAULT_CHRG_CUR 1000
79 #define DEFAULT_INPUT_CUR 1400
80 #define DEFAULT_SLP_ENTER_CUR 600
81 #define DEFAULT_SLP_EXIT_CUR 600
83 #define DSOC_DISCHRG_EMU_CURR 1200
84 #define DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
85 #define DSOC_DISCHRG_FAST_EER_RANGE 10
86 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
87 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
88 #define DSOC_CHRG_FAST_EER_RANGE 10
89 #define DSOC_CHRG_EMU_CURR 1200
90 #define DSOC_CHRG_TERM_CURR 600
91 #define DSOC_CHRG_TERM_VOL 4100
92 #define CHRG_FINISH_VOL 4100
94 /*realtime RSOC calib param*/
95 #define RSOC_DISCHRG_ERR_LOWER 40
96 #define RSOC_DISCHRG_ERR_UPPER 50
97 #define RSOC_ERR_CHCK_CNT 15
98 #define RSOC_COMPS 20 /*compensation*/
99 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
100 #define RSOC_CALIB_DISCHRGR_TIME 3 /*min*/
102 #define RSOC_RESUME_ERR 10
103 #define REBOOT_INTER_MIN 1
105 #define INTERPOLATE_MAX 1000
106 #define MAX_INT 0x7FFF
107 #define TIME_10MIN_SEC 600
109 #define CHRG_VOL_SHIFT 4
110 #define CHRG_ILIM_SHIFT 0
111 #define CHRG_ICUR_SHIFT 0
112 #define DEF_CHRG_VOL CHRG_VOL4200
113 #define DEF_CHRG_CURR_SEL CHRG_CUR1400mA
114 #define DEF_CHRG_CURR_LMT ILIM_2000MA
116 /*TEST_POWER_MODE params*/
117 #define TEST_CURRENT 1000
118 #define TEST_VOLTAGE 3800
120 #define TEST_STATUS POWER_SUPPLY_STATUS_CHARGING
121 #define TEST_PRESET 1
122 #define TEST_AC_ONLINE 1
123 #define TEST_USB_ONLINE 0
125 #define ZERO_ALGOR_THRESD 3800
126 #define DISCHRG_ZERO_MODE 1
127 #define DISCHRG_NORMAL_MODE 0
128 #define DEF_LAST_ZERO_MODE_SOC -1
130 #define DISCHRG_MODE 0
133 #define TREND_STAT_FLAT 0
134 #define TREND_STAT_DOWN -1
135 #define TREND_STAT_UP 1
136 #define TREND_CAP_DIFF 5
140 #define SLP_CURR_MAX 40
141 #define SLP_CURR_MIN 6
142 #define WAKEUP_SEC_THRESD 40
143 #define CHRG_TIME_STEP (60)
144 #define DISCHRG_TIME_STEP_0 (30 * 60)
145 #define DISCHRG_TIME_STEP_1 (60 * 60)
147 #define DEF_PCB_OFFSET 42
148 #define DEF_CAL_OFFSET 0x832
149 #define DEF_PWRPATH_RES 50
150 #define SEC_TO_EMPTY 300
151 #define DSOC_CHRG_FINISH_CURR 1100
152 #define SLP_CHRG_CURR 1000
153 #define SLP_DSOC_VOL_THRESD 3600
154 /*if voltage is lower than this thresd,
155 we consider it as invalid
157 #define INVALID_VOL_THRESD 2500
158 #define PWR_OFF_THRESD 3400
159 #define MIN_ZERO_ACCURACY 10 /*0.01%*/
161 #define MAX_FCC 10000
164 * the following table value depends on datasheet
166 int CHRG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
168 int CHRG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
169 2250, 2400, 2600, 2800, 3000};
171 int CHRG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
172 2000, 2250, 2500, 2750, 3000};
174 u8 CHRG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
176 #define RK818_DC_IN 0
177 #define RK818_DC_OUT 1
179 #define OCV_VALID_SHIFT (0)
180 #define OCV_CALIB_SHIFT (1)
181 #define FIRST_PWRON_SHIFT (2)
183 #define SEC_TO_MIN(x) ((x) / 60)
185 struct rk81x_battery {
187 struct cell_state cell;
188 struct power_supply bat;
189 struct power_supply ac;
190 struct power_supply usb;
191 struct delayed_work work;
193 struct pinctrl *pinctrl;
194 struct pinctrl_state *pins_default;
196 struct battery_platform_data *pdata;
210 uint16_t voltage_ocv;
211 uint16_t relax_voltage;
217 bool pcb_ioffset_updated;
234 int bat_res_update_cnt;
240 unsigned long dod0_time;
253 int current_k;/* (ICALIB0, ICALIB1) */
256 int voltage_k;/* VCALIB0 VCALIB1 */
259 int zero_timeout_cnt;
260 int zero_old_remain_cap;
265 int charge_smooth_time;
269 unsigned long suspend_time_sum;
273 int suspend_charge_current;
276 bool charge_smooth_status;
277 bool discharge_smooth_status;
283 struct notifier_block battery_nb;
284 struct usb_phy *usb_phy;
285 struct notifier_block usb_nb;
286 struct notifier_block fb_nb;
289 int s2r; /*suspend to resume*/
290 struct workqueue_struct *wq;
291 struct delayed_work battery_monitor_work;
292 struct delayed_work otg_check_work;
293 struct delayed_work usb_phy_delay_work;
294 struct delayed_work chrg_term_mode_switch_work;
295 struct delayed_work ac_usb_check_work;
296 struct delayed_work dc_det_check_work;
297 enum bc_port_type charge_otg;
300 struct wake_lock resume_wake_lock;
301 unsigned long plug_in_base;
302 unsigned long plug_out_base;
303 unsigned long finish_sig_base;
304 unsigned long power_on_base;
309 bool is_first_poweron;
312 int debug_finish_real_soc;
313 int debug_finish_temp_soc;
319 unsigned long wakeup_sec;
320 u32 delta_vol_smooth;
321 unsigned long dischrg_normal_base;
322 unsigned long dischrg_emu_base;
323 unsigned long chrg_normal_base;
324 unsigned long chrg_term_base;
325 unsigned long chrg_emu_base;
326 unsigned long chrg_finish_base;
327 unsigned long fcc_update_sec;
329 u8 dischrg_algorithm_mode;
330 int last_zero_mode_dsoc;
332 unsigned long dischrg_save_sec;
333 unsigned long chrg_save_sec;
334 struct timeval suspend_rtc_base;
337 u32 support_usb_adp, support_dc_adp;
339 #define to_device_info(x) container_of((x), \
340 struct rk81x_battery, bat)
342 #define to_ac_device_info(x) container_of((x), \
343 struct rk81x_battery, ac)
345 #define to_usb_device_info(x) container_of((x), \
346 struct rk81x_battery, usb)
348 static int loader_charged;
350 static int __init rk81x_bat_loader_charged(char *__unused)
354 pr_info("battery charged in loader\n");
358 __setup("loader_charged", rk81x_bat_loader_charged);
360 static u64 g_base_sec;
361 static u64 get_runtime_sec(void)
363 u64 ts_ns = local_clock();
365 do_div(ts_ns, 1000000000);
367 return ts_ns + g_base_sec;
370 static u64 is_local_clock_reset(void)
372 u64 ts_ns = local_clock();
374 do_div(ts_ns, 1000000000);
379 static inline unsigned long BASE_TO_SEC(unsigned long x)
382 return get_runtime_sec() - x;
387 static inline unsigned long BASE_TO_MIN(unsigned long x)
389 return BASE_TO_SEC(x) / 60;
392 static bool rk81x_bat_support_adp_type(enum hw_support_adp type)
397 case HW_ADP_TYPE_USB:
405 case HW_ADP_TYPE_DUAL:
406 if (support_usb_adp && support_dc_adp)
416 static bool rk81x_chrg_online(struct rk81x_battery *di)
418 return di->usb_online || di->ac_online;
421 static u32 interpolate(int value, u32 *table, int size)
426 for (i = 0; i < size; i++) {
427 if (value < table[i])
431 if ((i > 0) && (i < size)) {
432 d = (value - table[i-1]) * (INTERPOLATE_MAX / (size - 1));
433 d /= table[i] - table[i-1];
434 d = d + (i-1) * (INTERPOLATE_MAX / (size - 1));
436 d = i * ((INTERPOLATE_MAX + size / 2) / size);
445 /* Returns (a * b) / c */
446 static int32_t ab_div_c(u32 a, u32 b, u32 c)
452 sign = ((((a^b)^c) & 0x80000000) != 0);
458 tmp = (a * b + (c >> 1)) / c;
470 static int div(int val)
472 return (val == 0) ? 1 : val;
475 static int rk81x_bat_read(struct rk81x_battery *di, u8 reg,
476 u8 buf[], unsigned len)
481 for (i = 0; ret < 0 && i < 3; i++) {
482 ret = rk818_i2c_read(di->rk818, reg, len, buf);
484 dev_err(di->dev, "read reg:0x%02x failed\n", reg);
487 return (ret < 0) ? ret : 0;
490 static int rk81x_bat_write(struct rk81x_battery *di, u8 reg,
491 u8 const buf[], unsigned len)
496 for (i = 0; ret < 0 && i < 3; i++) {
497 ret = rk818_i2c_write(di->rk818, reg, (int)len, *buf);
499 dev_err(di->dev, "write reg:0x%02x failed\n", reg);
502 return (ret < 0) ? ret : 0;
505 static int rk81x_bat_set_bit(struct rk81x_battery *di, u8 reg, u8 shift)
510 for (i = 0; ret < 0 && i < 3; i++) {
511 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
513 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
519 static int rk81x_bat_clr_bit(struct rk81x_battery *di, u8 reg, u8 shift)
524 for (i = 0; ret < 0 && i < 3; i++) {
525 ret = rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
527 dev_err(di->dev, "set reg:0x%02x failed\n", reg);
533 static u8 rk81x_bat_read_bit(struct rk81x_battery *di, u8 reg, u8 shift)
538 rk81x_bat_read(di, reg, &buf, 1);
539 val = (buf & BIT(shift)) >> shift;
543 static void rk81x_dbg_dmp_gauge_regs(struct rk81x_battery *di)
548 DBG("%s dump charger register start:\n", __func__);
549 for (i = 0xAC; i < 0xEE; i++) {
550 rk81x_bat_read(di, i, &buf, 1);
551 DBG("0x%02x : 0x%02x\n", i, buf);
556 static void rk81x_dbg_dmp_charger_regs(struct rk81x_battery *di)
561 DBG("%s dump the register start:\n", __func__);
562 for (i = 0x99; i < 0xAB; i++) {
563 rk81x_bat_read(di, i, &buf, 1);
564 DBG(" the register is 0x%02x, the value is 0x%02x\n", i, buf);
569 static void rk81x_bat_reset_zero_var(struct rk81x_battery *di)
571 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
572 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
575 static void rk81x_bat_capacity_init_post(struct rk81x_battery *di)
577 rk81x_bat_reset_zero_var(di);
578 di->trend_start_cap = di->remain_capacity;
581 static void rk81x_bat_capacity_init(struct rk81x_battery *di, u32 capacity)
587 delta_cap = capacity - di->remain_capacity;
591 di->adjust_cap += delta_cap;
593 capacity_ma = capacity * 2390;/* 2134;//36*14/900*4096/521*500; */
595 buf = (capacity_ma >> 24) & 0xff;
596 rk81x_bat_write(di, GASCNT_CAL_REG3, &buf, 1);
597 buf = (capacity_ma >> 16) & 0xff;
598 rk81x_bat_write(di, GASCNT_CAL_REG2, &buf, 1);
599 buf = (capacity_ma >> 8) & 0xff;
600 rk81x_bat_write(di, GASCNT_CAL_REG1, &buf, 1);
601 buf = (capacity_ma & 0xff) | 0x01;
602 rk81x_bat_write(di, GASCNT_CAL_REG0, &buf, 1);
603 rk81x_bat_read(di, GASCNT_CAL_REG0, &buf, 1);
607 dev_dbg(di->dev, "update capacity :%d--remain_cap:%d\n",
608 capacity, di->remain_capacity);
613 * interface for debug: do rk81x_bat_first_pwron() without unloading battery
615 static ssize_t bat_calib_read(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct power_supply *psy_bat = dev_get_drvdata(dev);
619 struct rk81x_battery *di = to_device_info(psy_bat);
622 val = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
624 return sprintf(buf, "%d\n", val);
627 static ssize_t bat_calib_write(struct device *dev,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
633 struct power_supply *psy_bat = dev_get_drvdata(dev);
634 struct rk81x_battery *di = to_device_info(psy_bat);
636 ret = kstrtou8(buf, 0, &val);
641 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
643 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
648 * interface for debug: force battery to over discharge
650 static ssize_t bat_test_power_read(struct device *dev,
651 struct device_attribute *attr, char *buf)
653 struct power_supply *psy_bat = dev_get_drvdata(dev);
654 struct rk81x_battery *di = to_device_info(psy_bat);
656 return sprintf(buf, "%d\n", di->fg_drv_mode);
659 static ssize_t bat_test_power_write(struct device *dev,
660 struct device_attribute *attr,
661 const char *buf, size_t count)
665 struct power_supply *psy_bat = dev_get_drvdata(dev);
666 struct rk81x_battery *di = to_device_info(psy_bat);
668 ret = kstrtou8(buf, 0, &val);
673 di->fg_drv_mode = TEST_POWER_MODE;
675 di->fg_drv_mode = FG_NORMAL_MODE;
680 static ssize_t bat_fcc_read(struct device *dev,
681 struct device_attribute *attr, char *buf)
683 struct power_supply *psy_bat = dev_get_drvdata(dev);
684 struct rk81x_battery *di = to_device_info(psy_bat);
686 return sprintf(buf, "%d\n", di->fcc);
689 static ssize_t bat_fcc_write(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
695 struct power_supply *psy_bat = dev_get_drvdata(dev);
696 struct rk81x_battery *di = to_device_info(psy_bat);
698 ret = kstrtou16(buf, 0, &val);
707 static ssize_t bat_dsoc_read(struct device *dev,
708 struct device_attribute *attr, char *buf)
710 struct power_supply *psy_bat = dev_get_drvdata(dev);
711 struct rk81x_battery *di = to_device_info(psy_bat);
713 return sprintf(buf, "%d\n", di->dsoc);
716 static ssize_t bat_dsoc_write(struct device *dev,
717 struct device_attribute *attr,
718 const char *buf, size_t count)
722 struct power_supply *psy_bat = dev_get_drvdata(dev);
723 struct rk81x_battery *di = to_device_info(psy_bat);
725 ret = kstrtou8(buf, 0, &val);
734 static ssize_t bat_rsoc_read(struct device *dev,
735 struct device_attribute *attr, char *buf)
737 struct power_supply *psy_bat = dev_get_drvdata(dev);
738 struct rk81x_battery *di = to_device_info(psy_bat);
740 return sprintf(buf, "%d\n", di->rsoc);
743 static ssize_t bat_rsoc_write(struct device *dev,
744 struct device_attribute *attr,
745 const char *buf, size_t count)
750 struct power_supply *psy_bat = dev_get_drvdata(dev);
751 struct rk81x_battery *di = to_device_info(psy_bat);
753 ret = kstrtou8(buf, 0, &val);
757 capacity = di->fcc * val / 100;
758 rk81x_bat_capacity_init(di, capacity);
759 rk81x_bat_capacity_init_post(di);
764 static ssize_t bat_remain_cap_read(struct device *dev,
765 struct device_attribute *attr,
768 struct power_supply *psy_bat = dev_get_drvdata(dev);
769 struct rk81x_battery *di = to_device_info(psy_bat);
771 return sprintf(buf, "%d\n", di->remain_capacity);
774 static struct device_attribute rk818_bat_attr[] = {
775 __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
776 __ATTR(dsoc, 0664, bat_dsoc_read, bat_dsoc_write),
777 __ATTR(rsoc, 0664, bat_rsoc_read, bat_rsoc_write),
778 __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
779 __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
780 __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
784 static int rk81x_bat_gauge_enable(struct rk81x_battery *di)
789 ret = rk81x_bat_read(di, TS_CTRL_REG, &buf, 1);
791 dev_err(di->dev, "error reading TS_CTRL_REG");
796 rk81x_bat_write(di, TS_CTRL_REG, &buf, 1);
801 static void rk81x_bat_save_level(struct rk81x_battery *di, u8 save_soc)
803 rk81x_bat_write(di, UPDAT_LEVE_REG, &save_soc, 1);
806 static u8 rk81x_bat_get_level(struct rk81x_battery *di)
810 rk81x_bat_read(di, UPDAT_LEVE_REG, &soc, 1);
815 static int rk81x_bat_get_vcalib0(struct rk81x_battery *di)
821 ret = rk81x_bat_read(di, VCALIB0_REGL, &buf, 1);
823 ret = rk81x_bat_read(di, VCALIB0_REGH, &buf, 1);
826 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
830 static int rk81x_bat_get_vcalib1(struct rk81x_battery *di)
836 ret = rk81x_bat_read(di, VCALIB1_REGL, &buf, 1);
838 ret = rk81x_bat_read(di, VCALIB1_REGH, &buf, 1);
841 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
845 static int rk81x_bat_get_ioffset(struct rk81x_battery *di)
851 ret = rk81x_bat_read(di, IOFFSET_REGL, &buf, 1);
853 ret = rk81x_bat_read(di, IOFFSET_REGH, &buf, 1);
859 static uint16_t rk81x_bat_get_cal_offset(struct rk81x_battery *di)
865 ret = rk81x_bat_read(di, CAL_OFFSET_REGL, &buf, 1);
867 ret = rk81x_bat_read(di, CAL_OFFSET_REGH, &buf, 1);
873 static int rk81x_bat_set_cal_offset(struct rk81x_battery *di, u32 value)
879 ret = rk81x_bat_write(di, CAL_OFFSET_REGL, &buf, 1);
880 buf = (value >> 8) & 0xff;
881 ret = rk81x_bat_write(di, CAL_OFFSET_REGH, &buf, 1);
886 static void rk81x_bat_get_vol_offset(struct rk81x_battery *di)
888 int vcalib0, vcalib1;
890 vcalib0 = rk81x_bat_get_vcalib0(di);
891 vcalib1 = rk81x_bat_get_vcalib1(di);
893 di->voltage_k = (4200 - 3000) * 1000 / div((vcalib1 - vcalib0));
894 di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
895 DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
898 static uint16_t rk81x_bat_get_ocv_vol(struct rk81x_battery *di)
903 uint16_t voltage_now = 0;
907 for (i = 0; i < 3; i++) {
908 ret = rk81x_bat_read(di, BAT_OCV_REGL, &buf, 1);
910 ret = rk81x_bat_read(di, BAT_OCV_REGH, &buf, 1);
914 dev_err(di->dev, "error read BAT_OCV_REGH");
919 if (val[0] == val[1])
924 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
929 static int rk81x_bat_get_vol(struct rk81x_battery *di)
938 for (i = 0; i < 3; i++) {
939 ret = rk81x_bat_read(di, BAT_VOL_REGL, &buf, 1);
941 ret = rk81x_bat_read(di, BAT_VOL_REGH, &buf, 1);
945 dev_err(di->dev, "error read BAT_VOL_REGH");
950 if (val[0] == val[1])
955 vol = di->voltage_k * temp / 1000 + di->voltage_b;
960 static bool is_rk81x_bat_relax_mode(struct rk81x_battery *di)
965 ret = rk81x_bat_read(di, GGSTS, &status, 1);
967 if ((!(status & RELAX_VOL1_UPD)) || (!(status & RELAX_VOL2_UPD)))
973 static uint16_t rk81x_bat_get_relax_vol1(struct rk81x_battery *di)
977 uint16_t temp = 0, voltage_now;
979 ret = rk81x_bat_read(di, RELAX_VOL1_REGL, &buf, 1);
981 ret = rk81x_bat_read(di, RELAX_VOL1_REGH, &buf, 1);
984 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
989 static uint16_t rk81x_bat_get_relax_vol2(struct rk81x_battery *di)
993 uint16_t temp = 0, voltage_now;
995 ret = rk81x_bat_read(di, RELAX_VOL2_REGL, &buf, 1);
997 ret = rk81x_bat_read(di, RELAX_VOL2_REGH, &buf, 1);
1000 voltage_now = di->voltage_k * temp / 1000 + di->voltage_b;
1005 static uint16_t rk81x_bat_get_relax_vol(struct rk81x_battery *di)
1009 uint16_t relax_vol1, relax_vol2;
1012 ret = rk81x_bat_read(di, GGSTS, &status, 1);
1013 ret = rk81x_bat_read(di, GGCON, &ggcon, 1);
1015 relax_vol1 = rk81x_bat_get_relax_vol1(di);
1016 relax_vol2 = rk81x_bat_get_relax_vol2(di);
1017 DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1018 __func__, status, ggcon, relax_vol1, relax_vol2);
1020 if (is_rk81x_bat_relax_mode(di))
1021 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1027 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
1028 * based on the voltage.
1030 static int rk81x_bat_vol_to_capacity(struct rk81x_battery *di, int voltage)
1037 ocv_table = di->pdata->battery_ocv;
1038 ocv_size = di->pdata->ocv_size;
1039 tmp = interpolate(voltage, ocv_table, ocv_size);
1040 ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
1041 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
1046 static int rk81x_bat_get_raw_adc_current(struct rk81x_battery *di)
1052 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1054 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
1058 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1060 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1066 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
1076 static void rk81x_bat_ioffset_sample_set(struct rk81x_battery *di, int time)
1080 rk81x_bat_read(di, GGCON, &ggcon, 1);
1081 ggcon &= ~(0x30); /*clear <5:4>*/
1083 rk81x_bat_write(di, GGCON, &ggcon, 1);
1087 * when charger finish signal comes, we need calibrate the current, make it
1090 static bool rk81x_bat_zero_current_calib(struct rk81x_battery *di)
1100 if ((di->chrg_status == CHARGE_FINISH) &&
1101 (BASE_TO_MIN(di->power_on_base) >= 3) &&
1102 (abs(di->current_avg) > 4)) {
1103 for (retry = 0; retry < 5; retry++) {
1104 adc_value = rk81x_bat_get_raw_adc_current(di);
1105 if (!rk81x_chrg_online(di) || abs(adc_value) > 30) {
1106 dev_warn(di->dev, "charger plugout\n");
1111 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1112 C0 = rk81x_bat_get_cal_offset(di);
1113 C1 = adc_value + C0;
1114 DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1116 rk81x_bat_set_cal_offset(di, C1);
1117 DBG("<%s>. new cal_offset = %d\n",
1118 __func__, rk81x_bat_get_cal_offset(di));
1120 adc_value = rk81x_bat_get_raw_adc_current(di);
1121 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1122 if (abs(adc_value) < 4) {
1123 if (rk81x_bat_get_cal_offset(di) < 0x7ff) {
1124 ioffset = rk81x_bat_get_ioffset(di);
1125 rk81x_bat_set_cal_offset(di,
1128 ioffset = rk81x_bat_get_ioffset(di);
1129 pcb_offset = C1 - ioffset;
1130 di->pcb_ioffset = pcb_offset;
1131 di->pcb_ioffset_updated = true;
1136 DBG("<%s>. update the cal_offset, C1 = %d\n"
1137 "i_offset = %d, pcb_offset = %d\n",
1138 __func__, C1, ioffset, pcb_offset);
1142 dev_dbg(di->dev, "ioffset cal failed\n");
1143 rk81x_bat_set_cal_offset(di, C0);
1146 di->pcb_ioffset_updated = false;
1153 static void rk81x_bat_set_relax_thres(struct rk81x_battery *di)
1156 int enter_thres, exit_thres;
1157 struct cell_state *cell = &di->cell;
1159 enter_thres = (cell->config->ocv->sleep_enter_current) * 1000 / 1506;
1160 exit_thres = (cell->config->ocv->sleep_exit_current) * 1000 / 1506;
1161 DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1162 __func__, cell->config->ocv->sleep_enter_current,
1163 cell->config->ocv->sleep_exit_current);
1165 buf = enter_thres & 0xff;
1166 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGL, &buf, 1);
1167 buf = (enter_thres >> 8) & 0xff;
1168 rk81x_bat_write(di, RELAX_ENTRY_THRES_REGH, &buf, 1);
1170 buf = exit_thres & 0xff;
1171 rk81x_bat_write(di, RELAX_EXIT_THRES_REGL, &buf, 1);
1172 buf = (exit_thres >> 8) & 0xff;
1173 rk81x_bat_write(di, RELAX_EXIT_THRES_REGH, &buf, 1);
1175 /* set sample time */
1176 rk81x_bat_read(di, GGCON, &buf, 1);
1177 buf &= ~(3 << 2);/*8min*/
1178 buf &= ~0x01; /* clear bat_res calc*/
1179 rk81x_bat_write(di, GGCON, &buf, 1);
1182 static void rk81x_bat_restart_relax(struct rk81x_battery *di)
1187 rk81x_bat_read(di, GGCON, &ggcon, 1);
1189 rk81x_bat_write(di, GGCON, &ggcon, 1);
1191 rk81x_bat_read(di, GGSTS, &ggsts, 1);
1193 rk81x_bat_write(di, GGSTS, &ggsts, 1);
1196 static int rk81x_bat_get_avg_current(struct rk81x_battery *di)
1205 for (i = 0; i < 3; i++) {
1206 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGL, &buf, 1);
1208 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1213 ret = rk81x_bat_read(di, BAT_CUR_AVG_REGH, &buf, 1);
1215 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1221 if (val[0] == val[1])
1222 current_now = val[0];
1224 current_now = val[2];
1226 if (current_now & 0x800)
1227 current_now -= 4096;
1229 temp = current_now * 1506 / 1000;/*1000*90/14/4096*500/521;*/
1234 static void rk81x_bat_set_power_supply_state(struct rk81x_battery *di,
1235 enum charger_type charger_type)
1237 di->usb_online = OFFLINE;
1238 di->ac_online = OFFLINE;
1240 switch (charger_type) {
1242 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1245 di->usb_online = ONLINE;
1246 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1248 case DC_CHARGER:/*treat dc as ac*/
1250 di->ac_online = ONLINE;
1251 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
1254 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
1258 queue_delayed_work(di->wq, &di->chrg_term_mode_switch_work,
1259 msecs_to_jiffies(1000));
1262 /* high load: current < 0 with charger in.
1263 * System will not shutdown while dsoc=0% with charging state(ac_online),
1264 * which will cause over discharge, so oppose status before report states.
1266 static void rk81x_bat_lowpwr_check(struct rk81x_battery *di)
1269 int pwr_off_thresd = di->pdata->power_off_thresd - 50;
1271 if (di->current_avg < 0 && di->voltage < pwr_off_thresd) {
1273 time = get_runtime_sec();
1275 if (BASE_TO_SEC(time) > (MINUTE)) {
1276 rk81x_bat_set_power_supply_state(di, NO_CHARGER);
1277 dev_info(di->dev, "low power....\n");
1284 static int is_rk81x_bat_exist(struct rk81x_battery *di)
1288 rk81x_bat_read(di, SUP_STS_REG, &buf, 1);
1290 return (buf & 0x80) ? 1 : 0;
1293 static bool is_rk81x_bat_first_poweron(struct rk81x_battery *di)
1298 rk81x_bat_read(di, GGSTS, &buf, 1);
1299 DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1300 /*di->pwron_bat_con = buf;*/
1304 rk81x_bat_write(di, GGSTS, &buf, 1);
1305 rk81x_bat_read(di, GGSTS, &temp, 1);
1306 } while (temp & BAT_CON);
1313 static void rk81x_bat_flatzone_vol_init(struct rk81x_battery *di)
1320 ocv_table = di->pdata->battery_ocv;
1321 ocv_size = di->pdata->ocv_size;
1323 for (j = 0; j < 21; j++)
1327 for (i = 1; i < ocv_size-1; i++) {
1328 if (ocv_table[i+1] < ocv_table[i] + 20)
1329 temp_table[j++] = i;
1332 temp_table[j] = temp_table[j-1] + 1;
1334 di->enter_flatzone = ocv_table[i];
1337 for (i = 0; i < 20; i++) {
1338 if (temp_table[i] < temp_table[i+1])
1343 di->exit_flatzone = ocv_table[i];
1345 DBG("enter_flatzone = %d exit_flatzone = %d\n",
1346 di->enter_flatzone, di->exit_flatzone);
1349 static void rk81x_bat_power_on_save(struct rk81x_battery *di, int ocv_voltage)
1351 u8 ocv_valid, first_pwron;
1355 /*buf==1: OCV_VOL is valid*/
1356 ocv_valid = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1357 first_pwron = rk81x_bat_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1358 DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1360 if (first_pwron == 1 || ocv_valid == 1) {
1361 DBG("<%s> enter.\n", __func__);
1362 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
1363 if ((ocv_soc < 20) && (ocv_voltage > 2750)) {
1364 di->dod0_voltage = ocv_voltage;
1365 di->dod0_capacity = di->temp_nac;
1370 di->dod0_level = 100;
1371 else if (ocv_soc < 5)
1372 di->dod0_level = 95;
1373 else if (ocv_soc < 10)
1374 di->dod0_level = 90;
1376 di->dod0_level = 80;
1377 /* save_soc = di->dod0_level; */
1378 soc_level = rk81x_bat_get_level(di);
1379 if (soc_level > di->dod0_level) {
1380 di->dod0_status = 0;
1382 if (soc_level <= 80)
1384 rk81x_bat_save_level(di, soc_level);
1386 di->dod0_status = 1;
1388 di->fcc_update_sec = get_runtime_sec();
1391 dev_info(di->dev, "dod0_vol:%d, dod0_cap:%d\n"
1392 "dod0:%d, soc_level:%d: dod0_status:%d\n"
1394 di->dod0_voltage, di->dod0_capacity,
1395 ocv_soc, soc_level, di->dod0_status,
1401 static int rk81x_bat_get_rsoc(struct rk81x_battery *di)
1403 return (di->remain_capacity + di->fcc / 200) * 100 / div(di->fcc);
1406 static enum power_supply_property rk_battery_props[] = {
1407 POWER_SUPPLY_PROP_STATUS,
1408 POWER_SUPPLY_PROP_CURRENT_NOW,
1409 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1410 POWER_SUPPLY_PROP_PRESENT,
1411 POWER_SUPPLY_PROP_HEALTH,
1412 POWER_SUPPLY_PROP_CAPACITY,
1415 static int rk81x_battery_get_property(struct power_supply *psy,
1416 enum power_supply_property psp,
1417 union power_supply_propval *val)
1419 struct rk81x_battery *di = to_device_info(psy);
1422 case POWER_SUPPLY_PROP_CURRENT_NOW:
1423 val->intval = di->current_avg * 1000;/*uA*/
1424 if (di->fg_drv_mode == TEST_POWER_MODE)
1425 val->intval = TEST_CURRENT * 1000;
1427 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1428 val->intval = di->voltage * 1000;/*uV*/
1429 if (di->fg_drv_mode == TEST_POWER_MODE)
1430 val->intval = TEST_VOLTAGE * 1000;
1433 case POWER_SUPPLY_PROP_PRESENT:
1434 val->intval = is_rk81x_bat_exist(di);
1435 if (di->fg_drv_mode == TEST_POWER_MODE)
1436 val->intval = TEST_PRESET;
1439 case POWER_SUPPLY_PROP_CAPACITY:
1440 val->intval = di->dsoc;
1441 if (di->fg_drv_mode == TEST_POWER_MODE)
1442 val->intval = TEST_SOC;
1444 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1446 case POWER_SUPPLY_PROP_HEALTH:
1447 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1449 case POWER_SUPPLY_PROP_STATUS:
1450 val->intval = di->psy_status;
1451 if (di->fg_drv_mode == TEST_POWER_MODE)
1452 val->intval = TEST_STATUS;
1462 static enum power_supply_property rk_battery_ac_props[] = {
1463 POWER_SUPPLY_PROP_ONLINE,
1466 static enum power_supply_property rk_battery_usb_props[] = {
1467 POWER_SUPPLY_PROP_ONLINE,
1470 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1471 enum power_supply_property psp,
1472 union power_supply_propval *val)
1475 struct rk81x_battery *di = to_ac_device_info(psy);
1478 case POWER_SUPPLY_PROP_ONLINE:
1479 if (rk81x_chrg_online(di))
1480 rk81x_bat_lowpwr_check(di);
1481 val->intval = di->ac_online; /*discharging*/
1482 if (di->fg_drv_mode == TEST_POWER_MODE)
1483 val->intval = TEST_AC_ONLINE;
1493 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1494 enum power_supply_property psp,
1495 union power_supply_propval *val)
1498 struct rk81x_battery *di = to_usb_device_info(psy);
1501 case POWER_SUPPLY_PROP_ONLINE:
1502 if (rk81x_chrg_online(di))
1503 rk81x_bat_lowpwr_check(di);
1504 val->intval = di->usb_online;
1505 if (di->fg_drv_mode == TEST_POWER_MODE)
1506 val->intval = TEST_USB_ONLINE;
1516 static int rk81x_bat_power_supply_init(struct rk81x_battery *di)
1520 di->bat.name = "BATTERY";
1521 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1522 di->bat.properties = rk_battery_props;
1523 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1524 di->bat.get_property = rk81x_battery_get_property;
1527 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1528 di->ac.properties = rk_battery_ac_props;
1529 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1530 di->ac.get_property = rk81x_battery_ac_get_property;
1532 di->usb.name = "USB";
1533 di->usb.type = POWER_SUPPLY_TYPE_USB;
1534 di->usb.properties = rk_battery_usb_props;
1535 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1536 di->usb.get_property = rk81x_battery_usb_get_property;
1538 ret = power_supply_register(di->dev, &di->bat);
1540 dev_err(di->dev, "failed to register main battery\n");
1543 ret = power_supply_register(di->dev, &di->usb);
1545 dev_err(di->dev, "failed to register usb power supply\n");
1548 ret = power_supply_register(di->dev, &di->ac);
1550 dev_err(di->dev, "failed to register ac power supply\n");
1557 power_supply_unregister(&di->ac);
1559 power_supply_unregister(&di->usb);
1561 power_supply_unregister(&di->bat);
1566 static void rk81x_bat_save_remain_capacity(struct rk81x_battery *di,
1570 static u32 capacity_ma;
1572 if (capacity >= di->qmax)
1573 capacity = di->qmax;
1578 if (capacity_ma == capacity)
1581 capacity_ma = capacity;
1583 buf = (capacity_ma >> 24) & 0xff;
1584 rk81x_bat_write(di, REMAIN_CAP_REG3, &buf, 1);
1585 buf = (capacity_ma >> 16) & 0xff;
1586 rk81x_bat_write(di, REMAIN_CAP_REG2, &buf, 1);
1587 buf = (capacity_ma >> 8) & 0xff;
1588 rk81x_bat_write(di, REMAIN_CAP_REG1, &buf, 1);
1589 buf = (capacity_ma & 0xff) | 0x01;
1590 rk81x_bat_write(di, REMAIN_CAP_REG0, &buf, 1);
1593 static int rk81x_bat_get_remain_capacity(struct rk81x_battery *di)
1601 for (i = 0; i < 3; i++) {
1602 ret = rk81x_bat_read(di, REMAIN_CAP_REG3, &buf, 1);
1604 ret = rk81x_bat_read(di, REMAIN_CAP_REG2, &buf, 1);
1605 val[i] |= buf << 16;
1606 ret = rk81x_bat_read(di, REMAIN_CAP_REG1, &buf, 1);
1608 ret = rk81x_bat_read(di, REMAIN_CAP_REG0, &buf, 1);
1612 if (val[0] == val[1])
1620 static void rk81x_bat_save_fcc(struct rk81x_battery *di, u32 capacity)
1625 capacity_ma = capacity;
1626 buf = (capacity_ma >> 24) & 0xff;
1627 rk81x_bat_write(di, NEW_FCC_REG3, &buf, 1);
1628 buf = (capacity_ma >> 16) & 0xff;
1629 rk81x_bat_write(di, NEW_FCC_REG2, &buf, 1);
1630 buf = (capacity_ma >> 8) & 0xff;
1631 rk81x_bat_write(di, NEW_FCC_REG1, &buf, 1);
1632 buf = (capacity_ma & 0xff) | 0x01;
1633 rk81x_bat_write(di, NEW_FCC_REG0, &buf, 1);
1635 dev_info(di->dev, "update fcc : %d\n", capacity);
1638 static int rk81x_bat_get_fcc(struct rk81x_battery *di)
1643 rk81x_bat_read(di, NEW_FCC_REG3, &buf, 1);
1644 capacity = buf << 24;
1645 rk81x_bat_read(di, NEW_FCC_REG2, &buf, 1);
1646 capacity |= buf << 16;
1647 rk81x_bat_read(di, NEW_FCC_REG1, &buf, 1);
1648 capacity |= buf << 8;
1649 rk81x_bat_read(di, NEW_FCC_REG0, &buf, 1);
1652 if (capacity < MIN_FCC) {
1653 dev_warn(di->dev, "invalid fcc(0x%x), use design capacity",
1655 capacity = di->design_capacity;
1656 rk81x_bat_save_fcc(di, capacity);
1657 } else if (capacity > di->qmax) {
1658 dev_warn(di->dev, "invalid fcc(0x%x), use qmax", capacity);
1659 capacity = di->qmax;
1660 rk81x_bat_save_fcc(di, capacity);
1666 static int rk81x_bat_get_realtime_capacity(struct rk81x_battery *di)
1675 for (i = 0; i < 3; i++) {
1676 ret = rk81x_bat_read(di, GASCNT3, &buf, 1);
1678 ret = rk81x_bat_read(di, GASCNT2, &buf, 1);
1679 val[i] |= buf << 16;
1680 ret = rk81x_bat_read(di, GASCNT1, &buf, 1);
1682 ret = rk81x_bat_read(di, GASCNT0, &buf, 1);
1685 if (val[0] == val[1])
1690 capacity = temp / 2390;/* 4096*900/14/36*500/521; */
1695 static int rk81x_bat_save_dsoc(struct rk81x_battery *di, u8 save_soc)
1699 if (last_soc != save_soc) {
1700 rk81x_bat_write(di, SOC_REG, &save_soc, 1);
1701 last_soc = save_soc;
1707 static int rk81x_bat_save_reboot_cnt(struct rk81x_battery *di, u8 save_cnt)
1712 rk81x_bat_write(di, REBOOT_CNT_REG, &cnt, 1);
1716 static void rk81x_bat_set_current(struct rk81x_battery *di, int charge_current)
1720 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1721 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1722 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1723 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
1726 static void rk81x_bat_set_chrg_current(struct rk81x_battery *di,
1727 enum charger_type charger_type)
1729 switch (charger_type) {
1732 rk81x_bat_set_current(di, ILIM_450MA);
1736 rk81x_bat_set_current(di, di->chrg_i_lmt);
1739 rk81x_bat_set_current(di, ILIM_450MA);
1743 #if defined(CONFIG_ARCH_ROCKCHIP)
1745 static void rk81x_bat_set_charger_param(struct rk81x_battery *di,
1746 enum charger_type charger_type)
1748 rk81x_bat_set_chrg_current(di, charger_type);
1749 rk81x_bat_set_power_supply_state(di, charger_type);
1751 switch (charger_type) {
1753 power_supply_changed(&di->bat);
1757 power_supply_changed(&di->usb);
1760 power_supply_changed(&di->ac);
1767 static enum charger_type rk81x_bat_get_dc_state(struct rk81x_battery *di)
1770 enum charger_type charger_type = NO_CHARGER;
1772 if (di->fg_drv_mode == TEST_POWER_MODE) {
1773 charger_type = DC_CHARGER;
1780 if (!gpio_is_valid(di->dc_det_pin))
1783 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1785 pr_err("Failed to request gpio %d with ret:""%d\n",
1786 di->dc_det_pin, ret);
1790 gpio_direction_input(di->dc_det_pin);
1791 ret = gpio_get_value(di->dc_det_pin);
1792 if (ret == di->dc_det_level)
1793 charger_type = DC_CHARGER;
1795 charger_type = NO_CHARGER;
1796 gpio_free(di->dc_det_pin);
1798 return charger_type;
1801 static void rk81x_battery_dc_delay_work(struct work_struct *work)
1803 enum charger_type charger_type;
1804 struct rk81x_battery *di = container_of(work,
1805 struct rk81x_battery, dc_det_check_work.work);
1807 charger_type = rk81x_bat_get_dc_state(di);
1809 if (charger_type == DC_CHARGER)
1810 rk81x_bat_set_charger_param(di, DC_CHARGER);
1811 else/*NO_CHARGER: maybe usb charger still plugin*/
1812 queue_delayed_work(di->wq,
1813 &di->ac_usb_check_work,
1814 msecs_to_jiffies(10));
1817 static void rk81x_battery_acusb_delay_work(struct work_struct *work)
1820 int gadget_flag, usb_id;
1821 struct rk81x_battery *di = container_of(work,
1822 struct rk81x_battery, ac_usb_check_work.work);
1824 rk81x_bat_read(di, VB_MOD_REG, &buf, 1);
1825 usb_id = dwc_otg_check_dpdm(0);
1828 if ((buf & PLUG_IN_STS) != 0)
1829 rk81x_bat_set_charger_param(di, DC_CHARGER);
1831 rk81x_bat_set_charger_param(di, NO_CHARGER);
1835 rk81x_bat_set_charger_param(di, USB_CHARGER);
1838 rk81x_bat_set_charger_param(di, AC_CHARGER);
1843 /*check unstanderd charger*/
1844 if (usb_id == 1 || usb_id == 3) {
1845 gadget_flag = get_gadget_connect_flag();
1846 if (0 == gadget_flag) {
1848 if (di->check_count >= 5) {
1849 di->check_count = 0;
1850 rk81x_bat_set_charger_param(di, AC_CHARGER);
1852 queue_delayed_work(di->wq,
1853 &di->ac_usb_check_work,
1854 msecs_to_jiffies(1000));
1856 } else {/*confirm: USB_CHARGER*/
1857 di->check_count = 0;
1863 #if defined(CONFIG_X86_INTEL_SOFIA)
1864 static int rk81x_get_chrg_type_by_usb_phy(struct rk81x_battery *di, int ma)
1866 enum charger_type charger_type;
1869 charger_type = AC_CHARGER;
1871 charger_type = USB_CHARGER;
1873 charger_type = NO_CHARGER;
1877 dev_info(di->dev, "limit current:%d\n", ma);
1879 return charger_type;
1882 static void rk81x_battery_usb_notifier_delayed_work(struct work_struct *work)
1884 struct rk81x_battery *di;
1885 enum charger_type type;
1887 di = container_of(work, struct rk81x_battery, usb_phy_delay_work.work);
1888 type = rk81x_get_chrg_type_by_usb_phy(di, di->ma);
1890 rk81x_bat_set_chrg_current(di, type);
1891 power_supply_changed(&di->usb);
1894 static int rk81x_battery_usb_notifier(struct notifier_block *nb,
1895 unsigned long event, void *data)
1897 struct rk81x_battery *di;
1898 struct power_supply_cable_props *cable_props;
1899 enum charger_type type;
1901 di = container_of(nb, struct rk81x_battery, usb_nb);
1907 case USB_EVENT_CHARGER:
1908 cable_props = (struct power_supply_cable_props *)data;
1909 type = rk81x_get_chrg_type_by_usb_phy(di, cable_props->ma);
1910 rk81x_bat_set_power_supply_state(di, type);
1911 queue_delayed_work(di->wq, &di->usb_phy_delay_work,
1912 msecs_to_jiffies(50));
1923 static int rk81x_battery_fb_notifier(struct notifier_block *nb,
1924 unsigned long event, void *data)
1926 struct rk81x_battery *di;
1927 struct fb_event *evdata = data;
1930 di = container_of(nb, struct rk81x_battery, fb_nb);
1932 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
1935 blank = *(int *)evdata->data;
1937 if (di->fb_blank != blank)
1938 di->fb_blank = blank;
1942 if (blank == FB_BLANK_UNBLANK)
1943 di->early_resume = 1;
1948 static int rk81x_battery_register_fb_notify(struct rk81x_battery *di)
1950 memset(&di->fb_nb, 0, sizeof(di->fb_nb));
1951 di->fb_nb.notifier_call = rk81x_battery_fb_notifier;
1953 return fb_register_client(&di->fb_nb);
1957 * it is first time for battery to be weld, init by ocv table
1959 static void rk81x_bat_first_pwron(struct rk81x_battery *di)
1961 rk81x_bat_save_fcc(di, di->design_capacity);
1962 di->fcc = rk81x_bat_get_fcc(di);
1964 di->rsoc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
1965 di->dsoc = di->rsoc;
1966 di->nac = di->temp_nac;
1968 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1969 rk81x_bat_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1970 DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1971 __func__, di->dsoc, di->nac, di->fcc);
1974 static int rk81x_bat_get_calib_vol(struct rk81x_battery *di)
1979 int relax_vol = di->relax_voltage;
1980 int ocv_vol = di->voltage_ocv;
1982 init_cur = rk81x_bat_get_avg_current(di);
1983 diff = (di->bat_res + di->pdata->chrg_diff_vol) * init_cur;
1985 est_vol = di->voltage - diff;
1987 if (di->loader_charged) {
1988 calib_vol = est_vol;
1992 if (di->pwroff_min > 8) {
1993 if (abs(relax_vol - ocv_vol) < 100) {
1994 calib_vol = ocv_vol;
1996 if (abs(relax_vol - est_vol) > abs(ocv_vol - est_vol))
1997 calib_vol = ocv_vol;
1999 calib_vol = relax_vol;
2001 } else if (di->pwroff_min > 2) {
2002 calib_vol = ocv_vol;
2007 dev_info(di->dev, "c=%d, v=%d, relax=%d, ocv=%d, est=%d, calib=%d\n",
2008 init_cur, di->voltage, relax_vol, ocv_vol, est_vol, calib_vol);
2014 * it is not first time for battery to be weld, init by last record info
2016 static void rk81x_bat_not_first_pwron(struct rk81x_battery *di)
2020 int remain_capacity;
2022 int calib_vol, calib_soc, calib_capacity;
2024 rk81x_bat_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
2025 rk81x_bat_read(di, SOC_REG, &pwron_soc, 1);
2026 init_soc = pwron_soc;
2027 remain_capacity = rk81x_bat_get_remain_capacity(di);
2029 /* check if support uboot charge,
2030 * if support, uboot charge driver should have done init work,
2031 * so here we should skip init work
2033 #if defined(CONFIG_ARCH_ROCKCHIP)
2034 if (di->loader_charged)
2037 calib_vol = rk81x_bat_get_calib_vol(di);
2038 if (calib_vol > 0) {
2039 calib_soc = rk81x_bat_vol_to_capacity(di, calib_vol);
2040 calib_capacity = di->temp_nac;
2042 if (abs(calib_soc - init_soc) >= 70 || di->loader_charged) {
2043 init_soc = calib_soc;
2044 remain_capacity = calib_capacity;
2046 dev_info(di->dev, "calib_vol %d, init soc %d, remain_cap %d\n",
2047 calib_vol, init_soc, remain_capacity);
2050 ocv_soc = rk81x_bat_vol_to_capacity(di, di->voltage_ocv);
2051 DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
2052 __func__, remain_capacity, di->temp_nac);
2054 if (di->pwroff_min > 0) {
2055 if (di->pwroff_min > 30) {
2056 rk81x_bat_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2058 remain_capacity = di->temp_nac;
2059 DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
2060 __func__, remain_capacity);
2062 } else if ((di->pwroff_min > 5) &&
2063 (abs(ocv_soc - init_soc) >= 10)) {
2064 if (remain_capacity >= di->temp_nac * 120/100)
2065 remain_capacity = di->temp_nac * 110/100;
2066 else if (remain_capacity < di->temp_nac * 8/10)
2067 remain_capacity = di->temp_nac * 9/10;
2068 DBG("<%s> pwroff > 5 minute, remain_cap = %d\n",
2069 __func__, remain_capacity);
2072 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
2075 di->dsoc = init_soc;
2076 di->nac = remain_capacity;
2079 dev_info(di->dev, "reg soc=%d, init soc = %d, init cap=%d\n",
2080 pwron_soc, di->dsoc, di->nac);
2083 static u8 rk81x_bat_get_pwroff_min(struct rk81x_battery *di)
2085 u8 curr_pwroff_min, last_pwroff_min;
2087 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG,
2088 &curr_pwroff_min, 1);
2089 rk81x_bat_read(di, NON_ACT_TIMER_CNT_REG_SAVE,
2090 &last_pwroff_min, 1);
2092 rk81x_bat_write(di, NON_ACT_TIMER_CNT_REG_SAVE,
2093 &curr_pwroff_min, 1);
2095 return (curr_pwroff_min != last_pwroff_min) ? curr_pwroff_min : 0;
2098 static int rk81x_bat_rsoc_init(struct rk81x_battery *di)
2100 u8 calib_en;/*debug*/
2102 di->voltage = rk81x_bat_get_vol(di);
2103 di->voltage_ocv = rk81x_bat_get_ocv_vol(di);
2104 di->pwroff_min = rk81x_bat_get_pwroff_min(di);
2105 di->relax_voltage = rk81x_bat_get_relax_vol(di);
2106 di->current_avg = rk81x_bat_get_avg_current(di);
2108 dev_info(di->dev, "v=%d, ov=%d, rv=%d, c=%d, pwroff_min=%d\n",
2109 di->voltage, di->voltage_ocv, di->relax_voltage,
2110 di->current_avg, di->pwroff_min);
2112 calib_en = rk81x_bat_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2113 DBG("readbit: calib_en=%d\n", calib_en);
2114 if (is_rk81x_bat_first_poweron(di) ||
2115 ((di->pwroff_min >= 30) && (calib_en == 1))) {
2116 rk81x_bat_first_pwron(di);
2117 rk81x_bat_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
2120 rk81x_bat_not_first_pwron(di);
2126 static u8 rk81x_bat_get_chrg_status(struct rk81x_battery *di)
2131 rk81x_bat_read(di, SUP_STS_REG, &status, 1);
2136 DBG(" CHARGE-OFF ...\n");
2140 DBG(" DEAD CHARGE ...\n");
2142 case TRICKLE_CHARGE:
2144 DBG(" TRICKLE CHARGE ...\n ");
2148 DBG(" CC or CV ...\n");
2151 ret = CHARGE_FINISH;
2152 DBG(" CHARGE FINISH ...\n");
2156 DBG(" USB OVER VOL ...\n");
2160 DBG(" BAT TMP ERROR ...\n");
2164 DBG(" TIMER ERROR ...\n");
2168 DBG(" USB EXIST ...\n");
2172 DBG(" USB EFF...\n");
2181 static void rk81x_bat_match_param(struct rk81x_battery *di, int chrg_vol,
2182 int chrg_ilim, int chrg_cur)
2186 di->chrg_v_lmt = DEF_CHRG_VOL;
2187 di->chrg_i_lmt = DEF_CHRG_CURR_LMT;
2188 di->chrg_i_cur = DEF_CHRG_CURR_SEL;
2190 for (i = 0; i < ARRAY_SIZE(CHRG_V_LMT); i++) {
2191 if (chrg_vol < CHRG_V_LMT[i])
2194 di->chrg_v_lmt = (i << CHRG_VOL_SHIFT);
2197 for (i = 0; i < ARRAY_SIZE(CHRG_I_LMT); i++) {
2198 if (chrg_ilim < CHRG_I_LMT[i])
2201 di->chrg_i_lmt = (i << CHRG_ILIM_SHIFT);
2204 for (i = 0; i < ARRAY_SIZE(CHRG_I_CUR); i++) {
2205 if (chrg_cur < CHRG_I_CUR[i])
2208 di->chrg_i_cur = (i << CHRG_ICUR_SHIFT);
2210 DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2211 __func__, di->chrg_v_lmt, di->chrg_i_lmt, di->chrg_i_cur);
2214 static u8 rk81x_bat_select_finish_ma(int fcc)
2216 u8 ma = FINISH_150MA;
2221 else if (fcc >= 4000)
2224 else if (fcc >= 3000)
2234 * there is a timer inside rk81x to calc how long the battery is in charging
2235 * state. rk81x will close PowerPath inside IC when timer reach, which will
2236 * stop the charging work. we have to reset the corresponding bits to restart
2237 * the timer to avoid that case.
2239 static void rk81x_bat_init_chrg_timer(struct rk81x_battery *di)
2243 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
2244 buf &= ~CHRG_TIMER_CCCV_EN;
2245 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
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);
2250 dev_info(di->dev, "reset cccv charge timer\n");
2254 static void rk81x_bat_charger_init(struct rk81x_battery *di)
2256 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2257 u8 sup_sts_reg, thremal_reg, ggcon;
2258 int chrg_vol, chrg_cur, chrg_ilim;
2261 chrg_vol = di->pdata->max_charger_voltagemV;
2262 chrg_cur = di->pdata->max_charger_currentmA;
2263 chrg_ilim = di->pdata->max_charger_ilimitmA;
2265 rk81x_bat_match_param(di, chrg_vol, chrg_ilim, chrg_cur);
2266 finish_ma = rk81x_bat_select_finish_ma(di->fcc);
2268 /*rk81x_bat_init_chrg_timer(di);*/
2270 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2271 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2272 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2273 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2274 rk81x_bat_read(di, SUP_STS_REG, &sup_sts_reg, 1);
2275 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2276 rk81x_bat_read(di, GGCON, &ggcon, 1);
2278 usb_ctrl_reg &= (~0x0f);
2280 if (rk81x_bat_support_adp_type(HW_ADP_TYPE_USB))
2281 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2283 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2285 if (di->fg_drv_mode == TEST_POWER_MODE)
2286 usb_ctrl_reg |= (CHRG_CT_EN | di->chrg_i_lmt);
2288 chrg_ctrl_reg1 &= (0x00);
2289 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chrg_v_lmt | di->chrg_i_cur);
2291 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2292 chrg_ctrl_reg3 &= ~CHRG_TIMER_CCCV_EN;/*disable*/
2294 chrg_ctrl_reg2 &= ~(0xc7);
2295 chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2297 sup_sts_reg &= ~(0x01 << 3);
2298 sup_sts_reg |= (0x01 << 2);
2300 thremal_reg &= (~0x0c);
2301 thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2302 ggcon |= ADC_CURRENT_MODE;
2304 rk81x_bat_write(di, THERMAL_REG, &thremal_reg, 1);
2305 rk81x_bat_write(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2306 /*don't touch charge setting when boot int loader charge mode*/
2307 if (!di->loader_charged)
2308 rk81x_bat_write(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2309 rk81x_bat_write(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2310 rk81x_bat_write(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2311 rk81x_bat_write(di, SUP_STS_REG, &sup_sts_reg, 1);
2312 rk81x_bat_write(di, GGCON, &ggcon, 1);
2315 static void rk81x_bat_fg_init(struct rk81x_battery *di)
2322 rk81x_bat_write(di, ADC_CTRL_REG, &val, 1);
2324 rk81x_bat_gauge_enable(di);
2325 /* get the volatege offset */
2326 rk81x_bat_get_vol_offset(di);
2327 rk81x_bat_charger_init(di);
2328 rk81x_bat_set_relax_thres(di);
2330 /* get the current offset , the value write to the CAL_OFFSET */
2331 di->current_offset = rk81x_bat_get_ioffset(di);
2332 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
2333 DBG("<%s>. pcb_offset = 0x%x, io_offset = 0x%x\n",
2334 __func__, pcb_offset, di->current_offset);
2336 pcb_offset = DEF_PCB_OFFSET;
2337 cal_offset = pcb_offset + di->current_offset;
2338 if (cal_offset < 0x7ff || cal_offset > 0x8ff)
2339 cal_offset = DEF_CAL_OFFSET;
2340 rk81x_bat_set_cal_offset(di, cal_offset);
2341 /* set sample time for cal_offset interval*/
2342 rk81x_bat_ioffset_sample_set(di, SAMP_TIME_8MIN);
2344 rk81x_bat_rsoc_init(di);
2345 rk81x_bat_capacity_init(di, di->nac);
2346 rk81x_bat_capacity_init_post(di);
2348 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
2349 di->current_avg = rk81x_bat_get_avg_current(di);
2351 rk81x_bat_restart_relax(di);
2352 rk81x_bat_power_on_save(di, di->voltage_ocv);
2354 rk81x_bat_write(di, OCV_VOL_VALID_REG, &val, 1);
2356 rk81x_dbg_dmp_gauge_regs(di);
2357 rk81x_dbg_dmp_charger_regs(di);
2360 "nac = %d , remain_capacity = %d\n"
2361 "OCV_voltage = %d, voltage = %d\n"
2362 "SOC = %d, fcc = %d\n, current=%d\n"
2363 "cal_offset = 0x%x\n",
2365 di->nac, di->remain_capacity,
2366 di->voltage_ocv, di->voltage,
2367 di->dsoc, di->fcc, di->current_avg,
2371 static void rk81x_bat_zero_calc_linek(struct rk81x_battery *di)
2373 int dead_voltage, ocv_voltage;
2374 int voltage, voltage_old, voltage_now;
2379 int ocv_soc, dead_soc;
2380 int power_off_thresd = di->pdata->power_off_thresd;
2383 voltage_old = rk81x_bat_get_vol(di);
2385 voltage_now = rk81x_bat_get_vol(di);
2387 } while ((voltage_old == voltage_now) && (count_num < 11));
2388 DBG("<%s>. current calc count=%d\n", __func__, count_num);
2391 for (i = 0; i < 10; i++) {
2392 voltage += rk81x_bat_get_vol(di);
2397 currentnow = rk81x_bat_get_avg_current(di);
2399 /* 50 mo power-path mos */
2400 dead_voltage = power_off_thresd - currentnow *
2401 (di->bat_res + DEF_PWRPATH_RES) / 1000;
2403 ocv_voltage = voltage - (currentnow * di->bat_res) / 1000;
2404 DBG("ZERO0: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2405 dead_voltage, ocv_voltage);
2407 dead_soc = rk81x_bat_vol_to_capacity(di, dead_voltage);
2408 q_dead = di->temp_nac;
2409 DBG("ZERO0: dead_voltage_soc = %d, q_dead = %d\n",
2412 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2413 q_ocv = di->temp_nac;
2414 DBG("ZERO0: ocv_voltage_soc = %d, q_ocv = %d\n",
2417 rsoc = ocv_soc - dead_soc;
2418 if ((di->dsoc == 1) && (rsoc > 0)) {/*discharge*/
2420 } else if (rsoc > 0) {
2421 di->line_k = (di->display_soc + rsoc / 2) / div(rsoc);
2424 di->display_soc = di->dsoc * 1000;
2427 di->zero_old_remain_cap = di->remain_capacity;
2429 DBG("ZERO-new: new-line_k=%d, dsoc=%d, X0soc=%d\n"
2430 "ZERO-new: di->display_soc=%d, old_remain_cap=%d\n\n",
2431 di->line_k, di->dsoc, rsoc,
2432 di->display_soc, di->zero_old_remain_cap);
2435 static void rk81x_bat_zero_algorithm(struct rk81x_battery *di)
2437 int delta_cap, delta_soc;
2440 di->zero_timeout_cnt++;
2441 delta_cap = di->zero_old_remain_cap - di->remain_capacity;
2442 delta_soc = di->line_k * (delta_cap * 100) / div(di->fcc);
2444 DBG("ZERO1: line_k=%d, display_soc(Y0)=%d, dsoc=%d, rsoc=%d\n"
2445 "ZERO1: delta_soc(X0)=%d, delta_cap=%d, old_remain_cap = %d\n"
2446 "ZERO1: timeout_cnt=%d\n\n",
2447 di->line_k, di->display_soc, di->dsoc, di->rsoc,
2448 delta_soc, delta_cap, di->zero_old_remain_cap,
2449 di->zero_timeout_cnt);
2451 if ((abs(delta_soc) > MIN_ZERO_ACCURACY) ||
2452 (di->zero_timeout_cnt > 500)) {
2453 DBG("ZERO1:--------- enter calc -----------\n");
2454 di->zero_timeout_cnt = 0;
2455 di->display_soc -= delta_soc;
2456 tmp_dsoc = (di->display_soc + 500) / 1000;
2457 di->dsoc = tmp_dsoc;
2459 DBG("ZERO1: display_soc(Y0)=%d, dsoc=%d, rsoc=%d, tmp_soc=%d",
2460 di->display_soc, di->dsoc, di->rsoc, tmp_dsoc);
2462 rk81x_bat_zero_calc_linek(di);
2466 static int rk81x_bat_est_ocv_vol(struct rk81x_battery *di)
2468 return (di->voltage -
2469 (di->bat_res * di->current_avg) / 1000);
2472 static int rk81x_bat_est_ocv_soc(struct rk81x_battery *di)
2474 int ocv_soc, ocv_voltage;
2476 ocv_voltage = rk81x_bat_est_ocv_vol(di);
2477 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_voltage);
2482 /* we will estimate a ocv voltage to get a ocv soc.
2483 * if there is a big offset between ocv_soc and rsoc,
2484 * we will decide whether we should reinit capacity or not
2486 static void rk81x_bat_rsoc_dischrg_check(struct rk81x_battery *di)
2488 int ocv_soc = di->est_ocv_soc;
2489 int ocv_volt = di->est_ocv_vol;
2490 int rsoc = rk81x_bat_get_rsoc(di);
2491 int max_volt = di->pdata->max_charger_voltagemV;
2493 if (ocv_volt > max_volt)
2496 if (di->plug_out_min >= RSOC_CALIB_DISCHRGR_TIME) {
2497 if ((ocv_soc-rsoc >= RSOC_DISCHRG_ERR_LOWER) ||
2499 (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)) {
2501 di->err_soc_sum += ocv_soc;
2505 DBG("<%s>. rsoc err_chck_cnt = %d, err_soc_sum = %d\n",
2506 __func__, di->err_chck_cnt, di->err_soc_sum);
2508 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2509 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2510 if (rsoc-ocv_soc >= RSOC_DISCHRG_ERR_UPPER)
2511 ocv_soc += RSOC_COMPS;
2513 di->temp_nac = ocv_soc * di->fcc / 100;
2514 rk81x_bat_capacity_init(di, di->temp_nac);
2515 rk81x_bat_capacity_init_post(di);
2516 di->rsoc = rk81x_bat_get_rsoc(di);
2517 di->remain_capacity =
2518 rk81x_bat_get_realtime_capacity(di);
2519 di->err_soc_sum = 0;
2520 di->err_chck_cnt = 0;
2521 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2525 di->err_chck_cnt = 0;
2526 di->err_soc_sum = 0;
2530 static void rk81x_bat_rsoc_check(struct rk81x_battery *di)
2532 u8 status = di->psy_status;
2534 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2535 (status == POWER_SUPPLY_STATUS_FULL)) {
2536 if ((di->current_avg < 0) &&
2537 (di->chrg_status != CHARGE_FINISH))
2538 rk81x_bat_rsoc_dischrg_check(di);
2541 rsoc_chrg_calib(di);
2544 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2545 rk81x_bat_rsoc_dischrg_check(di);
2549 static void rk81x_bat_emulator_dischrg(struct rk81x_battery *di)
2551 u32 temp, soc_time = 0;
2552 unsigned long sec_unit;
2554 if (!di->dischrg_emu_base)
2555 di->dischrg_emu_base = get_runtime_sec();
2557 sec_unit = BASE_TO_SEC(di->dischrg_emu_base) + di->dischrg_save_sec;
2559 temp = di->fcc * 3600 / 100;
2561 if (abs(di->current_avg) < DSOC_DISCHRG_EMU_CURR)
2562 soc_time = temp / div(abs(DSOC_DISCHRG_EMU_CURR));
2564 soc_time = temp / div(abs(di->current_avg));
2566 if (sec_unit > soc_time) {
2568 di->dischrg_emu_base = get_runtime_sec();
2569 di->dischrg_save_sec = 0;
2572 DBG("<%s> soc_time=%d, sec_unit=%lu\n",
2573 __func__, soc_time, sec_unit);
2577 * when there is a big offset between dsoc and rsoc, dsoc needs to
2578 * speed up to keep pace witch rsoc.
2580 static void rk81x_bat_emulator_chrg(struct rk81x_battery *di)
2582 u32 soc_time = 0, temp;
2584 unsigned long chrg_emu_sec;
2586 if (!di->chrg_emu_base)
2587 di->chrg_emu_base = get_runtime_sec();
2589 chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base) + di->chrg_save_sec;
2590 temp = di->fcc * 3600 / 100;
2592 if (di->ac_online) {
2593 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2594 soc_time = temp / abs(DSOC_CHRG_EMU_CURR);
2596 soc_time = temp / div(abs(di->current_avg));
2598 soc_time = temp / 450;
2601 plus_soc = chrg_emu_sec / soc_time;
2602 if (chrg_emu_sec > soc_time) {
2603 di->dsoc += plus_soc;
2604 di->chrg_emu_base = get_runtime_sec();
2605 di->chrg_save_sec = 0;
2608 DBG("<%s>. soc_time=%d, chrg_emu_sec=%lu, plus_soc=%d\n",
2609 __func__, soc_time, chrg_emu_sec, plus_soc);
2612 /* check voltage and current when dsoc is close to full.
2613 * we will do a fake charge to adjust charing speed which
2614 * aims to make battery full charged and match finish signal.
2616 static void rk81x_bat_terminal_chrg(struct rk81x_battery *di)
2620 unsigned long chrg_term_sec;
2622 if (!di->chrg_term_base)
2623 di->chrg_term_base = get_runtime_sec();
2625 chrg_term_sec = BASE_TO_SEC(di->chrg_term_base) + di->chrg_save_sec;
2626 /*check current and voltage*/
2628 soc_time = di->fcc * 3600 / 100 / (abs(DSOC_CHRG_TERM_CURR));
2630 plus_soc = chrg_term_sec / soc_time;
2631 if (chrg_term_sec > soc_time) {
2632 di->dsoc += plus_soc;
2633 di->chrg_term_base = get_runtime_sec();
2634 di->chrg_save_sec = 0;
2636 DBG("<%s>. soc_time=%d, chrg_term_sec=%lu, plus_soc=%d\n",
2637 __func__, soc_time, chrg_term_sec, plus_soc);
2640 static void rk81x_bat_normal_dischrg(struct rk81x_battery *di)
2643 int now_current = di->current_avg;
2644 unsigned long dischrg_normal_sec;
2646 if (!di->dischrg_normal_base)
2647 di->dischrg_normal_base = get_runtime_sec();
2649 dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base) +
2650 di->dischrg_save_sec;
2652 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
2653 DBG("<%s>. rsoc=%d, dsoc=%d, dischrg_st=%d\n",
2654 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status);
2656 if (di->rsoc == di->dsoc) {
2657 DBG("<%s>. rsoc == dsoc\n", __func__);
2658 di->dsoc = di->rsoc;
2659 di->dischrg_normal_base = get_runtime_sec();
2660 di->dischrg_save_sec = 0;
2661 /*di->discharge_smooth_status = false;*/
2662 } else if (di->rsoc > di->dsoc - 1) {
2663 DBG("<%s>. rsoc > dsoc - 1\n", __func__);
2664 if (dischrg_normal_sec > soc_time * 3 / 2) {
2666 di->dischrg_normal_base = get_runtime_sec();
2667 di->dischrg_save_sec = 0;
2669 di->discharge_smooth_status = true;
2671 } else if (di->rsoc < di->dsoc - 1) {
2672 DBG("<%s>. rsoc < dsoc - 1\n", __func__);
2673 if (dischrg_normal_sec > soc_time * 3 / 4) {
2675 di->dischrg_normal_base = get_runtime_sec();
2676 di->dischrg_save_sec = 0;
2678 di->discharge_smooth_status = true;
2680 } else if (di->rsoc == di->dsoc - 1) {
2681 DBG("<%s>. rsoc == dsoc - 1\n", __func__);
2682 if (di->discharge_smooth_status) {
2683 if (dischrg_normal_sec > soc_time * 3 / 4) {
2685 di->dischrg_normal_base = get_runtime_sec();
2686 di->dischrg_save_sec = 0;
2687 di->discharge_smooth_status = false;
2691 di->dischrg_normal_base = get_runtime_sec();
2692 di->dischrg_save_sec = 0;
2693 di->discharge_smooth_status = false;
2697 DBG("<%s>, rsoc = %d, dsoc = %d, discharge_smooth_status = %d\n"
2698 "dischrg_normal_sec = %lu, soc_time = %d, delta_vol=%d\n",
2699 __func__, di->rsoc, di->dsoc, di->discharge_smooth_status,
2700 dischrg_normal_sec, soc_time, di->delta_vol_smooth);
2703 static void rk81x_bat_dischrg_smooth(struct rk81x_battery *di)
2707 /* first resume from suspend: we don't run this,
2708 * the sleep_dischrg will handle dsoc, and what
2709 * ever this is fake wakeup or not, we should clean
2710 * zero algorithm mode, or it will handle the dsoc.
2713 rk81x_bat_reset_zero_var(di);
2717 di->rsoc = rk81x_bat_get_rsoc(di);
2719 DBG("<%s>. rsoc = %d, dsoc = %d, dischrg_algorithm_mode=%d\n",
2720 __func__, di->rsoc, di->dsoc, di->dischrg_algorithm_mode);
2722 if (di->dischrg_algorithm_mode == DISCHRG_NORMAL_MODE) {
2723 delta_soc = di->dsoc - di->rsoc;
2725 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2726 di->dischrg_normal_base = 0;
2727 rk81x_bat_emulator_dischrg(di);
2729 di->chrg_emu_base = 0;
2730 rk81x_bat_normal_dischrg(di);
2733 if (di->voltage < ZERO_ALGOR_THRESD) {
2734 di->dischrg_normal_base = 0;
2735 di->chrg_emu_base = 0;
2736 di->dischrg_algorithm_mode = DISCHRG_ZERO_MODE;
2737 di->zero_timeout_cnt = 0;
2739 DBG("<%s>. dsoc=%d, last_zero_mode_dsoc=%d\n",
2740 __func__, di->dsoc, di->last_zero_mode_dsoc);
2741 if (di->dsoc != di->last_zero_mode_dsoc) {
2742 di->display_soc = di->dsoc * 1000;
2743 di->last_zero_mode_dsoc = di->dsoc;
2744 rk81x_bat_zero_calc_linek(di);
2745 DBG("<%s>. first calc, init linek\n", __func__);
2749 rk81x_bat_zero_algorithm(di);
2751 if (di->voltage > ZERO_ALGOR_THRESD + 50) {
2752 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
2753 di->zero_timeout_cnt = 0;
2754 DBG("<%s>. exit zero_algorithm\n", __func__);
2759 static void rk81x_bat_dbg_time_table(struct rk81x_battery *di)
2762 static int old_index;
2765 int mod = di->dsoc % 10;
2766 int index = di->dsoc / 10;
2768 if (rk81x_chrg_online(di))
2769 time = di->plug_in_min;
2771 time = di->plug_out_min;
2773 if ((mod == 0) && (index > 0) && (old_index != index)) {
2774 di->chrg_min[index-1] = time - old_min;
2779 for (i = 1; i < 11; i++)
2780 DBG("Time[%d]=%d, ", (i * 10), di->chrg_min[i-1]);
2784 static void rk81x_bat_dbg_dmp_info(struct rk81x_battery *di)
2786 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2787 u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2788 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2790 rk81x_bat_read(di, MISC_MARK_REG, &misc_reg, 1);
2791 rk81x_bat_read(di, GGCON, &ggcon_reg, 1);
2792 rk81x_bat_read(di, GGSTS, &ggsts_reg, 1);
2793 rk81x_bat_read(di, SUP_STS_REG, &sup_tst_reg, 1);
2794 rk81x_bat_read(di, VB_MOD_REG, &vb_mod_reg, 1);
2795 rk81x_bat_read(di, USB_CTRL_REG, &usb_ctrl_reg, 1);
2796 rk81x_bat_read(di, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2797 rk81x_bat_read(di, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2798 rk81x_bat_read(di, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2799 rk81x_bat_read(di, 0x00, &rtc_val, 1);
2800 rk81x_bat_read(di, THERMAL_REG, &thremal_reg, 1);
2802 DBG("\n------------- dump_debug_regs -----------------\n"
2803 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2804 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2805 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2806 "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2807 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2808 ggcon_reg, ggsts_reg, rtc_val,
2809 sup_tst_reg, vb_mod_reg,
2810 usb_ctrl_reg, chrg_ctrl_reg1,
2811 thremal_reg, misc_reg,
2812 chrg_ctrl_reg2, chrg_ctrl_reg3
2815 DBG("#######################################################\n"
2816 "voltage = %d, current-avg = %d\n"
2817 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2818 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2819 "display_soc = %d, cpapacity_soc = %d\n"
2820 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2821 "finish_real_soc = %d, finish_temp_soc = %d\n"
2822 "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2823 "plug_in = %d, plug_out = %d, finish_sig = %d, finish_chrg=%lu\n"
2824 "sec: chrg=%lu, dischrg=%lu, term_chrg=%lu, emu_chrg=%lu\n"
2825 "emu_dischrg = %lu, power_on_sec = %lu, g_base_sec=%lld\n"
2826 "mode:%d, save_chrg_sec = %lu, save_dischrg_sec = %lu\n"
2827 "#########################################################\n",
2828 di->voltage, di->current_avg,
2829 di->fcc, di->remain_capacity, di->voltage_ocv,
2830 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2832 di->ac_online, di->usb_online, di->psy_status,
2833 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2834 rk81x_bat_get_ioffset(di), rk81x_bat_get_cal_offset(di),
2835 di->adjust_cap, di->plug_in_min, di->plug_out_min,
2836 di->finish_sig_min, BASE_TO_SEC(di->chrg_finish_base),
2837 BASE_TO_SEC(di->chrg_normal_base),
2838 BASE_TO_SEC(di->dischrg_normal_base),
2839 BASE_TO_SEC(di->chrg_term_base),
2840 BASE_TO_SEC(di->chrg_emu_base),
2841 BASE_TO_SEC(di->dischrg_emu_base),
2842 BASE_TO_SEC(di->power_on_base), g_base_sec,
2843 di->current_mode, di->chrg_save_sec, di->dischrg_save_sec
2848 static void rk81x_bat_update_fcc(struct rk81x_battery *di)
2852 int dod0_to_soc100_min;
2854 remain_cap = di->remain_capacity - di->dod0_capacity - di->adjust_cap;
2855 dod0_to_soc100_min = BASE_TO_MIN(di->fcc_update_sec);
2857 DBG("%s: remain_cap:%d, ajust_cap:%d, dod0_status=%d\n"
2858 "dod0_capacity:%d, dod0_to_soc100_min:%d\n",
2859 __func__, remain_cap, di->adjust_cap, di->dod0_status,
2860 di->dod0_capacity, dod0_to_soc100_min);
2862 if ((di->chrg_status == CHARGE_FINISH) && (di->dod0_status == 1) &&
2863 (dod0_to_soc100_min < 1200)) {
2864 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2865 __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2867 fcc0 = remain_cap * 100 / div(100 - di->dod0);
2869 dev_info(di->dev, "%s: fcc0:%d, fcc:%d\n",
2870 __func__, fcc0, di->fcc);
2872 if ((fcc0 < di->qmax) && (fcc0 > 1000)) {
2873 di->dod0_status = 0;
2875 rk81x_bat_capacity_init(di, di->fcc);
2876 rk81x_bat_capacity_init_post(di);
2877 rk81x_bat_save_fcc(di, di->fcc);
2878 rk81x_bat_save_level(di, di->dod0_level);
2879 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2882 di->dod0_status = 0;
2886 static void rk81x_bat_dbg_get_finish_soc(struct rk81x_battery *di)
2888 if (di->chrg_status == CHARGE_FINISH) {
2889 di->debug_finish_real_soc = di->dsoc;
2890 di->debug_finish_temp_soc = di->rsoc;
2894 static void rk81x_bat_wait_finish_sig(struct rk81x_battery *di)
2896 int chrg_finish_vol = di->pdata->max_charger_voltagemV;
2899 if ((di->chrg_status == CHARGE_FINISH) &&
2900 (di->voltage > chrg_finish_vol - 150) && di->enter_finish) {
2901 rk81x_bat_update_fcc(di);/* save new fcc*/
2902 ret = rk81x_bat_zero_current_calib(di);
2904 di->enter_finish = false;
2906 rk81x_bat_dbg_get_finish_soc(di);
2910 static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
2912 unsigned long sec_finish;
2913 int soc_time = 0, plus_soc;
2916 if (di->dsoc < 100) {
2917 if (!di->chrg_finish_base)
2918 di->chrg_finish_base = get_runtime_sec();
2920 sec_finish = BASE_TO_SEC(di->chrg_finish_base) +
2922 temp = di->fcc * 3600 / 100;
2924 soc_time = temp / DSOC_CHRG_FINISH_CURR;
2926 soc_time = temp / 450;
2928 plus_soc = sec_finish / soc_time;
2929 if (sec_finish > soc_time) {
2930 di->dsoc += plus_soc;
2931 di->chrg_finish_base = get_runtime_sec();
2932 di->chrg_save_sec = 0;
2934 DBG("<%s>,CHARGE_FINISH:dsoc<100,dsoc=%d\n"
2935 "soc_time=%d, sec_finish=%lu, plus_soc=%d\n",
2936 __func__, di->dsoc, soc_time, sec_finish, plus_soc);
2940 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
2943 u32 soc_time, unit_sec;
2945 unsigned long chrg_normal_sec;
2947 now_current = rk81x_bat_get_avg_current(di);
2948 soc_time = di->fcc * 3600 / 100 / div(abs(now_current)); /*1% time*/
2950 if (!di->chrg_normal_base)
2951 di->chrg_normal_base = get_runtime_sec();
2953 chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base) + di->chrg_save_sec;
2954 di->rsoc = rk81x_bat_get_rsoc(di);
2956 DBG("<%s>. rsoc=%d, dsoc=%d, chrg_st=%d\n",
2957 __func__, di->rsoc, di->dsoc, di->charge_smooth_status);
2959 if (di->dsoc == di->rsoc) {
2960 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2961 di->rsoc = rk81x_bat_get_rsoc(di);
2962 di->chrg_normal_base = get_runtime_sec();
2963 di->chrg_save_sec = 0;
2964 /*di->charge_smooth_status = false;*/
2965 } else if (di->rsoc < di->dsoc + 1) {
2966 DBG("<%s>. rsoc < dsoc + 1\n", __func__);
2967 unit_sec = soc_time * 3 / 2;
2968 plus_soc = chrg_normal_sec / unit_sec;
2969 if (chrg_normal_sec > unit_sec) {
2970 di->dsoc += plus_soc;
2971 di->chrg_normal_base = get_runtime_sec();
2972 di->chrg_save_sec = 0;
2974 di->charge_smooth_status = true;
2975 } else if (di->rsoc > di->dsoc + 1) {
2976 DBG("<%s>. rsoc > dsoc + 1\n", __func__);
2977 unit_sec = soc_time * 3 / 4;
2978 plus_soc = chrg_normal_sec / unit_sec;
2979 if (chrg_normal_sec > unit_sec) {
2980 di->dsoc += plus_soc;
2981 di->chrg_normal_base = get_runtime_sec();
2982 di->chrg_save_sec = 0;
2984 di->charge_smooth_status = true;
2985 } else if (di->rsoc == di->dsoc + 1) {
2986 DBG("<%s>. rsoc == dsoc + 1\n", __func__);
2987 if (di->charge_smooth_status) {
2988 unit_sec = soc_time * 3 / 4;
2989 if (chrg_normal_sec > unit_sec) {
2990 di->dsoc = di->rsoc;
2991 di->chrg_normal_base = get_runtime_sec();
2992 di->charge_smooth_status = false;
2993 di->chrg_save_sec = 0;
2996 di->dsoc = di->rsoc;
2997 di->chrg_normal_base = get_runtime_sec();
2998 di->charge_smooth_status = false;
2999 di->chrg_save_sec = 0;
3003 DBG("<%s>, rsoc = %d, dsoc = %d, charge_smooth_status = %d\n"
3004 "chrg_normal_sec = %lu, soc_time = %d, plus_soc=%d\n",
3005 __func__, di->rsoc, di->dsoc, di->charge_smooth_status,
3006 chrg_normal_sec, soc_time, plus_soc);
3009 static void rk81x_bat_update_time(struct rk81x_battery *di)
3013 runtime_sec = get_runtime_sec();
3015 /*update by charger type*/
3016 if (rk81x_chrg_online(di))
3017 di->plug_out_base = runtime_sec;
3019 di->plug_in_base = runtime_sec;
3021 /*update by current*/
3022 if (di->chrg_status != CHARGE_FINISH) {
3023 di->finish_sig_base = runtime_sec;
3024 di->chrg_finish_base = runtime_sec;
3027 di->plug_in_min = BASE_TO_MIN(di->plug_in_base);
3028 di->plug_out_min = BASE_TO_MIN(di->plug_out_base);
3029 di->finish_sig_min = BASE_TO_MIN(di->finish_sig_base);
3031 rk81x_bat_dbg_time_table(di);
3034 static int rk81x_bat_get_rsoc_trend(struct rk81x_battery *di, int *trend_mult)
3036 int trend_start_cap = di->trend_start_cap;
3037 int remain_cap = di->remain_capacity;
3041 if (di->s2r && !di->slp_psy_status)
3042 di->trend_start_cap = di->remain_capacity;
3044 diff_cap = remain_cap - trend_start_cap;
3045 DBG("<%s>. trend_start_cap = %d, diff_cap = %d\n",
3046 __func__, trend_start_cap, diff_cap);
3047 *trend_mult = abs(diff_cap) / TREND_CAP_DIFF;
3049 if (abs(diff_cap) >= TREND_CAP_DIFF) {
3050 di->trend_start_cap = di->remain_capacity;
3051 state = (diff_cap > 0) ? TREND_STAT_UP : TREND_STAT_DOWN;
3052 DBG("<%s>. new trend_start_cap=%d", __func__, trend_start_cap);
3054 state = TREND_STAT_FLAT;
3060 static void rk81x_bat_arbitrate_rsoc_trend(struct rk81x_battery *di)
3062 int state, soc_time;
3063 static int trend_down_cnt, trend_up_cnt;
3064 int trend_cnt_thresd;
3065 int now_current = di->current_avg;
3068 trend_cnt_thresd = di->fcc / 100 / TREND_CAP_DIFF;
3069 state = rk81x_bat_get_rsoc_trend(di, &trend_mult);
3070 DBG("<%s>. TREND_STAT = %d, trend_mult = %d\n",
3071 __func__, state, trend_mult);
3072 if (di->chrg_status == CHARGE_FINISH)
3075 if (state == TREND_STAT_UP) {
3076 rk81x_bat_reset_zero_var(di);
3078 trend_up_cnt += trend_mult;
3079 if (trend_up_cnt >= trend_cnt_thresd) {
3081 di->dischrg_save_sec = 0;
3083 } else if (state == TREND_STAT_DOWN) {
3085 trend_down_cnt += trend_mult;
3086 if (trend_down_cnt >= trend_cnt_thresd) {
3088 di->chrg_save_sec = 0;
3092 soc_time = di->fcc * 3600 / 100 / div(abs(now_current));
3093 if ((di->chrg_save_sec + 20 > soc_time) &&
3094 (trend_up_cnt <= trend_cnt_thresd / 2) &&
3096 di->chrg_save_sec = 0;
3098 else if ((di->dischrg_save_sec + 20 > soc_time) &&
3099 (trend_down_cnt <= trend_cnt_thresd / 2) &&
3101 di->dischrg_save_sec = 0;
3103 DBG("<%s>. state=%d, cnt_thresd=%d, soc_time=%d\n"
3104 "up_cnt=%d, down_cnt=%d\n",
3105 __func__, state, trend_cnt_thresd, soc_time,
3106 trend_up_cnt, trend_down_cnt);
3109 static void rk81x_bat_chrg_smooth(struct rk81x_battery *di)
3111 u32 *ocv_table = di->pdata->battery_ocv;
3112 int delta_soc = di->rsoc - di->dsoc;
3114 if (di->chrg_status == CHARGE_FINISH ||
3115 di->slp_chrg_status == CHARGE_FINISH) {
3116 /*clear sleep charge status*/
3117 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3118 di->chrg_emu_base = 0;
3119 di->chrg_normal_base = 0;
3120 di->chrg_term_base = 0;
3121 rk81x_bat_finish_chrg(di);
3122 rk81x_bat_capacity_init(di, di->fcc);
3123 rk81x_bat_capacity_init_post(di);
3124 } else if ((di->ac_online == ONLINE && di->dsoc >= 90) &&
3125 ((di->current_avg > DSOC_CHRG_TERM_CURR) ||
3126 (di->voltage < ocv_table[18] + 20))) {
3127 di->chrg_emu_base = 0;
3128 di->chrg_normal_base = 0;
3129 di->chrg_finish_base = 0;
3130 rk81x_bat_terminal_chrg(di);
3131 } else if (di->chrg_status != CHARGE_FINISH &&
3132 delta_soc >= DSOC_CHRG_FAST_EER_RANGE) {
3133 di->chrg_term_base = 0;
3134 di->chrg_normal_base = 0;
3135 di->chrg_finish_base = 0;
3136 rk81x_bat_emulator_chrg(di);
3138 di->chrg_emu_base = 0;
3139 di->chrg_term_base = 0;
3140 di->chrg_finish_base = 0;
3141 rk81x_bat_normal_chrg(di);
3145 static unsigned long rk81x_bat_save_dischrg_sec(struct rk81x_battery *di)
3147 unsigned long dischrg_normal_sec = BASE_TO_SEC(di->dischrg_normal_base);
3148 unsigned long dischrg_emu_sec = BASE_TO_SEC(di->dischrg_emu_base);
3150 DBG("dischrg_normal_sec=%lu, dischrg_emu_sec=%lu\n",
3151 dischrg_normal_sec, dischrg_emu_sec);
3153 return (dischrg_normal_sec > dischrg_emu_sec) ?
3154 dischrg_normal_sec : dischrg_emu_sec;
3157 static unsigned long rk81x_bat_save_chrg_sec(struct rk81x_battery *di)
3159 unsigned long sec1, sec2;
3160 unsigned long chrg_normal_sec = BASE_TO_SEC(di->chrg_normal_base);
3161 unsigned long chrg_term_sec = BASE_TO_SEC(di->chrg_term_base);
3162 unsigned long chrg_emu_sec = BASE_TO_SEC(di->chrg_emu_base);
3163 unsigned long chrg_finish_sec = BASE_TO_SEC(di->chrg_finish_base);
3165 sec1 = (chrg_normal_sec > chrg_term_sec) ?
3166 chrg_normal_sec : chrg_term_sec;
3168 sec2 = (chrg_emu_sec > chrg_finish_sec) ?
3169 chrg_emu_sec : chrg_finish_sec;
3170 DBG("chrg_normal_sec=%lu, chrg_term_sec=%lu\n"
3171 "chrg_emu_sec=%lu, chrg_finish_sec=%lu\n",
3172 chrg_normal_sec, chrg_term_sec,
3173 chrg_emu_sec, chrg_finish_sec);
3175 return (sec1 > sec2) ? sec1 : sec2;
3178 static void rk81x_bat_display_smooth(struct rk81x_battery *di)
3180 if ((di->current_avg >= 0) || (di->chrg_status == CHARGE_FINISH)) {
3181 if (di->current_mode == DISCHRG_MODE) {
3182 di->current_mode = CHRG_MODE;
3183 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
3184 di->dischrg_normal_base = 0;
3185 di->dischrg_emu_base = 0;
3186 if (di->chrg_status == CHARGE_FINISH)
3187 di->dischrg_save_sec = 0;
3188 if ((di->chrg_status == CHARGE_FINISH) &&
3190 di->chrg_save_sec = 0;
3192 DBG("<%s>---dischrg_save_sec = %lu\n",
3193 __func__, di->dischrg_save_sec);
3196 if (!rk81x_chrg_online(di)) {
3197 dev_err(di->dev, "discharge, current error:%d\n",
3200 rk81x_bat_chrg_smooth(di);
3201 di->discharge_smooth_status = true;
3204 if (di->current_mode == CHRG_MODE) {
3205 di->current_mode = DISCHRG_MODE;
3206 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
3207 di->chrg_normal_base = 0;
3208 di->chrg_emu_base = 0;
3209 di->chrg_term_base = 0;
3210 di->chrg_finish_base = 0;
3211 DBG("<%s>---chrg_save_sec = %lu\n",
3212 __func__, di->chrg_save_sec);
3214 rk81x_bat_dischrg_smooth(di);
3215 di->charge_smooth_status = true;
3220 * update rsoc by relax voltage
3222 static void rk81x_bat_relax_vol_calib(struct rk81x_battery *di)
3224 int relax_vol = di->relax_voltage;
3225 int ocv_soc, capacity;
3227 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3228 capacity = (ocv_soc * di->fcc / 100);
3229 rk81x_bat_capacity_init(di, capacity);
3230 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3231 di->rsoc = rk81x_bat_get_rsoc(di);
3232 rk81x_bat_capacity_init_post(di);
3233 DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
3237 * 1: must do it, 0: when necessary
3239 static void rk81x_bat_vol_calib(struct rk81x_battery *di, int condition)
3241 int ocv_vol = di->est_ocv_vol;
3242 int ocv_soc = 0, capacity = 0;
3244 ocv_soc = rk81x_bat_vol_to_capacity(di, ocv_vol);
3245 capacity = (ocv_soc * di->fcc / 100);
3246 if (condition || (abs(ocv_soc-di->rsoc) >= RSOC_RESUME_ERR)) {
3247 rk81x_bat_capacity_init(di, capacity);
3248 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3249 di->rsoc = rk81x_bat_get_rsoc(di);
3250 rk81x_bat_capacity_init_post(di);
3251 DBG("<%s>, rsoc updated!\n", __func__);
3253 DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
3254 __func__, ocv_vol, ocv_soc, capacity);
3257 static int rk81x_bat_sleep_dischrg(struct rk81x_battery *di)
3261 unsigned long sleep_sec = di->suspend_time_sum;
3262 int power_off_thresd = di->pdata->power_off_thresd;
3264 DBG("<%s>, enter: dsoc=%d, rsoc=%d\n"
3265 "relax_vol=%d, vol=%d, sleep_min=%lu\n",
3266 __func__, di->dsoc, di->rsoc,
3267 di->relax_voltage, di->voltage, sleep_sec / 60);
3269 if (di->relax_voltage >= di->voltage) {
3270 rk81x_bat_relax_vol_calib(di);
3271 rk81x_bat_restart_relax(di);
3273 /* current_avg < 0: make sure the system is not
3274 * wakeup by charger plugin.
3276 /* even if relax voltage is not caught rightly, realtime voltage
3277 * is quite close to relax voltage, we should not do nothing after
3281 rk81x_bat_vol_calib(di, 1);
3285 if (di->dsoc <= di->rsoc) {
3286 di->sum_suspend_cap = (SLP_CURR_MIN * sleep_sec / 3600);
3287 delta_soc = di->sum_suspend_cap * 100 / di->fcc;
3288 temp_dsoc = di->dsoc - delta_soc;
3290 pr_info("battery calib0: rl=%d, dl=%d, intl=%d\n",
3291 di->rsoc, di->dsoc, delta_soc);
3293 if (delta_soc > 0) {
3294 if ((temp_dsoc < di->dsoc) && (di->dsoc < 5))
3296 else if ((temp_dsoc < 5) && (di->dsoc >= 5))
3298 else if (temp_dsoc > 5)
3299 di->dsoc = temp_dsoc;
3302 DBG("%s: dsoc<=rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3303 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3305 /*di->dsoc > di->rsoc*/
3306 di->sum_suspend_cap = (SLP_CURR_MAX * sleep_sec / 3600);
3307 delta_soc = di->sum_suspend_cap / (di->fcc / 100);
3308 temp_dsoc = di->dsoc - di->rsoc;
3310 pr_info("battery calib1: rsoc=%d, dsoc=%d, intsoc=%d\n",
3311 di->rsoc, di->dsoc, delta_soc);
3313 if ((di->est_ocv_vol > SLP_DSOC_VOL_THRESD) &&
3314 (temp_dsoc > delta_soc))
3315 di->dsoc -= delta_soc;
3317 di->dsoc = di->rsoc;
3319 DBG("%s: dsoc > rsoc, sum_cap=%d==>delta_soc=%d,temp_dsoc=%d\n",
3320 __func__, di->sum_suspend_cap, delta_soc, temp_dsoc);
3323 if (!di->relax_voltage && di->voltage <= power_off_thresd)
3329 DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
3330 __func__, di->dsoc, di->rsoc, di->sum_suspend_cap);
3335 static int rk81x_bat_sleep_chrg(struct rk81x_battery *di)
3338 unsigned long sleep_sec;
3340 sleep_sec = di->suspend_time_sum;
3341 if (((di->suspend_charge_current < 800) &&
3342 (di->ac_online == ONLINE)) ||
3343 (di->chrg_status == CHARGE_FINISH)) {
3344 DBG("<%s>,sleep: ac online current < 800\n", __func__);
3345 if (sleep_sec > 0) {
3346 /*default charge current: 1000mA*/
3347 sleep_soc = SLP_CHRG_CURR * sleep_sec * 100
3348 / 3600 / div(di->fcc);
3351 DBG("<%s>, usb charge\n", __func__);
3358 * only do report when there is a change.
3360 * if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3361 * when dsoc == 0, we must do report. But it will generate too much android
3362 * info when we enter test_power mode without battery, so we add a fg_drv_mode
3365 static void rk81x_bat_power_supply_changed(struct rk81x_battery *di)
3368 static u32 old_ac_status;
3369 static u32 old_usb_status;
3370 static u32 old_charge_status;
3373 state_changed = false;
3374 if ((di->dsoc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3375 state_changed = true;
3376 else if (di->dsoc != old_soc)
3377 state_changed = true;
3378 else if (di->ac_online != old_ac_status)
3379 state_changed = true;
3380 else if (di->usb_online != old_usb_status)
3381 state_changed = true;
3382 else if (old_charge_status != di->psy_status)
3383 state_changed = true;
3385 if (rk81x_chrg_online(di)) {
3386 if (di->dsoc == 100)
3387 di->psy_status = POWER_SUPPLY_STATUS_FULL;
3389 di->psy_status = POWER_SUPPLY_STATUS_CHARGING;
3392 if (state_changed) {
3393 power_supply_changed(&di->bat);
3394 power_supply_changed(&di->usb);
3395 power_supply_changed(&di->ac);
3397 old_ac_status = di->ac_online;
3398 old_usb_status = di->usb_online;
3399 old_charge_status = di->psy_status;
3400 dev_info(di->dev, "changed: dsoc=%d, rsoc=%d\n",
3401 di->dsoc, di->rsoc);
3406 static u8 rk81x_bat_get_cvcc_chrg_hour(struct rk81x_battery *di)
3410 rk81x_bat_read(di, CHRG_CTRL_REG2, &buf, 1);
3413 return CHRG_CVCC_HOUR[hour];
3416 /* we have to estimate the charging finish time from now, to decide
3417 * whether we should reset the timer or not.
3419 static void rk81x_bat_chrg_over_time_check(struct rk81x_battery *di)
3422 int remain_capacity;
3424 cvcc_hour = rk81x_bat_get_cvcc_chrg_hour(di);
3425 if (di->dsoc < di->rsoc)
3426 remain_capacity = di->dsoc * di->fcc / 100;
3428 remain_capacity = di->remain_capacity;
3430 DBG("CHRG_TIME(min): %ld, cvcc hour: %d",
3431 BASE_TO_MIN(di->plug_in_base), cvcc_hour);
3433 if (BASE_TO_MIN(di->plug_in_base) >= (cvcc_hour - 2) * 60) {
3434 di->chrg_cap2full = di->fcc - remain_capacity;
3435 if (di->current_avg <= 0)
3436 di->current_avg = 1;
3438 di->chrg_time2full = di->chrg_cap2full * 3600 /
3439 div(abs(di->current_avg));
3441 DBG("CHRG_TIME2FULL(min):%d, chrg_cap2full=%d, current=%d\n",
3442 SEC_TO_MIN(di->chrg_time2full), di->chrg_cap2full,
3445 if (SEC_TO_MIN(di->chrg_time2full) > 60) {
3446 /*rk81x_bat_init_chrg_timer(di);*/
3447 di->plug_in_base = get_runtime_sec();
3448 DBG("%s: reset charge timer\n", __func__);
3455 * in case that we will do reboot stress test, we need a special way
3456 * to ajust the dsoc.
3458 static void rk81x_bat_check_reboot(struct rk81x_battery *di)
3466 rk81x_bat_read(di, REBOOT_CNT_REG, &cnt, 1);
3469 unit_time = di->fcc * 3600 / 100 / 1200;/*1200mA default*/
3470 smooth_time = cnt * BASE_TO_SEC(di->power_on_base);
3472 DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3473 __func__, cnt, unit_time, smooth_time,
3474 BASE_TO_SEC(di->power_on_base), dsoc, rsoc);
3476 if (di->current_avg >= 0 || di->chrg_status == CHARGE_FINISH) {
3477 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3478 if ((dsoc < rsoc - 1) && (smooth_time > unit_time * 3 / 5)) {
3483 rk81x_bat_save_dsoc(di, dsoc);
3486 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time * 3 / 5);
3487 if ((dsoc > rsoc) && (smooth_time > unit_time * 3 / 5)) {
3492 rk81x_bat_save_dsoc(di, dsoc);
3496 rk81x_bat_save_reboot_cnt(di, cnt);
3499 static void rk81x_bat_update_calib_param(struct rk81x_battery *di)
3504 uint16_t cal_offset;
3505 u8 pcb_offset = DEF_PCB_OFFSET;
3507 min = BASE_TO_MIN(di->power_on_base);
3508 if ((min % 8) && (old_min != min)) {
3510 rk81x_bat_get_vol_offset(di);
3511 if (di->pcb_ioffset_updated)
3512 rk81x_bat_read(di, PCB_IOFFSET_REG, &pcb_offset, 1);
3514 current_offset = rk81x_bat_get_ioffset(di);
3515 rk81x_bat_set_cal_offset(di, current_offset + pcb_offset);
3516 cal_offset = rk81x_bat_get_cal_offset(di);
3517 if (cal_offset < 0x7ff)
3518 rk81x_bat_set_cal_offset(di, di->current_offset +
3520 DBG("<%s>. k=%d, b=%d, cal_offset=%d, i_offset=%d\n",
3521 __func__, di->voltage_k, di->voltage_b, cal_offset,
3522 rk81x_bat_get_ioffset(di));
3526 static void rk81x_bat_update_info(struct rk81x_battery *di)
3530 else if (di->dsoc < 0)
3534 * we need update fcc in continuous charging state, if discharge state
3535 * keep at least 2 hour, we decide not to update fcc, so clear the
3536 * fcc update flag: dod0_status.
3538 if (BASE_TO_MIN(di->plug_out_base) > 120)
3539 di->dod0_status = 0;
3541 di->voltage = rk81x_bat_get_vol(di);
3542 di->current_avg = rk81x_bat_get_avg_current(di);
3543 di->chrg_status = rk81x_bat_get_chrg_status(di);
3544 di->relax_voltage = rk81x_bat_get_relax_vol(di);
3545 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
3546 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
3547 /*rk81x_bat_chrg_over_time_check(di);*/
3548 rk81x_bat_update_calib_param(di);
3549 if (di->chrg_status == CC_OR_CV)
3550 di->enter_finish = true;
3552 if (!rk81x_chrg_online(di) && di->s2r)
3555 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
3556 if (di->remain_capacity > di->fcc) {
3557 rk81x_bat_capacity_init(di, di->fcc);
3558 rk81x_bat_capacity_init_post(di);
3559 di->remain_capacity = di->fcc;
3562 di->rsoc = rk81x_bat_get_rsoc(di);
3565 static int rk81x_bat_update_resume_state(struct rk81x_battery *di)
3567 if (di->slp_psy_status)
3568 return rk81x_bat_sleep_chrg(di);
3570 return rk81x_bat_sleep_dischrg(di);
3573 static void rk81x_bat_fcc_flag_check(struct rk81x_battery *di)
3575 u8 ocv_soc, soc_level;
3576 int relax_vol = di->relax_voltage;
3581 ocv_soc = rk81x_bat_vol_to_capacity(di, relax_vol);
3582 DBG("<%s>. ocv_soc=%d, min=%lu, vol=%d\n", __func__,
3583 ocv_soc, SEC_TO_MIN(di->suspend_time_sum), relax_vol);
3585 if ((SEC_TO_MIN(di->suspend_time_sum) > 30) &&
3586 (di->dod0_status == 0) &&
3588 di->dod0_voltage = relax_vol;
3589 di->dod0_capacity = di->temp_nac;
3594 di->dod0_level = 100;
3595 else if (ocv_soc < 5)
3596 di->dod0_level = 90;
3598 di->dod0_level = 80;
3600 /* save_soc = di->dod0_level; */
3601 soc_level = rk81x_bat_get_level(di);
3602 if (soc_level > di->dod0_level) {
3603 di->dod0_status = 0;
3605 di->dod0_status = 1;
3607 di->fcc_update_sec = get_runtime_sec();
3610 dev_info(di->dev, "resume: relax_vol:%d, dod0_cap:%d\n"
3611 "dod0:%d, soc_level:%d: dod0_status:%d\n"
3613 di->dod0_voltage, di->dod0_capacity,
3614 ocv_soc, soc_level, di->dod0_status,
3619 static void rk81x_chrg_term_mode_set(struct rk81x_battery *di, int mode)
3624 rk81x_bat_read(di, CHRG_CTRL_REG3, &buf, 1);
3627 rk81x_bat_write(di, CHRG_CTRL_REG3, &buf, 1);
3629 dev_info(di->dev, "set charge to %s termination mode\n",
3630 mode ? "digital" : "analog");
3633 static void rk81x_chrg_term_mode_switch_work(struct work_struct *work)
3635 struct rk81x_battery *di;
3637 di = container_of(work, struct rk81x_battery,
3638 chrg_term_mode_switch_work.work);
3640 if (rk81x_chrg_online(di))
3641 rk81x_chrg_term_mode_set(di, CHRG_TERM_DIG_SIGNAL);
3643 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
3646 static void rk81x_battery_work(struct work_struct *work)
3648 struct rk81x_battery *di;
3649 int ms = TIMER_MS_COUNTS;
3651 di = container_of(work, struct rk81x_battery,
3652 battery_monitor_work.work);
3653 if (rk81x_chrg_online(di)) {
3654 rk81x_bat_wait_finish_sig(di);
3655 /*rk81x_bat_chrg_finish_routine(di);*/
3657 rk81x_bat_fcc_flag_check(di);
3658 rk81x_bat_arbitrate_rsoc_trend(di);
3659 rk81x_bat_display_smooth(di);
3660 rk81x_bat_update_time(di);
3661 rk81x_bat_update_info(di);
3662 rk81x_bat_rsoc_check(di);
3663 rk81x_bat_power_supply_changed(di);
3664 rk81x_bat_save_dsoc(di, di->dsoc);
3665 rk81x_bat_save_remain_capacity(di, di->remain_capacity);
3667 rk81x_bat_dbg_dmp_info(di);
3669 if (!di->early_resume && di->s2r && !di->slp_psy_status)
3670 ms = 30 * TIMER_MS_COUNTS;
3672 di->early_resume = 0;
3676 queue_delayed_work(di->wq, &di->battery_monitor_work,
3677 msecs_to_jiffies(ms));
3680 #if defined(CONFIG_ARCH_ROCKCHIP)
3681 static void rk81x_battery_otg_delay_work(struct work_struct *work)
3683 struct rk81x_battery *di = container_of(work,
3684 struct rk81x_battery, otg_check_work.work);
3686 enum bc_port_type event = di->charge_otg;
3688 /* do not touch CHRG_CTRL_REG1[7]: CHRG_EN, hardware can
3689 * recognize otg plugin and will auto ajust this bit
3692 case USB_OTG_POWER_ON:
3693 dev_info(di->dev, "charge disable, otg enable\n");
3694 di->otg_online = ONLINE;
3695 /*rk81x_bat_set_charger_param(di, NO_CHARGER);*/
3696 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3697 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3698 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_EN);
3701 case USB_OTG_POWER_OFF:
3702 dev_info(di->dev, "charge enable, otg disable\n");
3703 di->otg_online = OFFLINE;
3704 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_IN_INT);
3705 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, PLUG_OUT_INT);
3706 rk818_set_bits(di->rk818, DCDC_EN_REG, OTG_EN_MASK, OTG_DIS);
3707 /*maybe dc still plugin*/
3708 queue_delayed_work(di->wq, &di->dc_det_check_work,
3709 msecs_to_jiffies(10));
3717 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3719 int register_battery_notifier(struct notifier_block *nb)
3721 return blocking_notifier_chain_register(&battery_chain_head, nb);
3723 EXPORT_SYMBOL_GPL(register_battery_notifier);
3725 int unregister_battery_notifier(struct notifier_block *nb)
3727 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3729 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3731 int battery_notifier_call_chain(unsigned long val)
3733 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3734 == NOTIFY_BAD) ? -EINVAL : 0;
3736 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3738 static int rk81x_bat_usb_notifier_call(struct notifier_block *nb,
3739 unsigned long event, void *data)
3741 enum charger_type charger_type;
3742 struct rk81x_battery *di =
3743 container_of(nb, struct rk81x_battery, battery_nb);
3745 if (di->fg_drv_mode == TEST_POWER_MODE)
3748 /*if dc is pluging, ignore usb*/
3749 charger_type = rk81x_bat_get_dc_state(di);
3750 if ((charger_type == DC_CHARGER) &&
3751 (event != USB_OTG_POWER_OFF) &&
3752 (event != USB_OTG_POWER_ON))
3756 case USB_BC_TYPE_DISCNT:/*maybe dc still plugin*/
3757 queue_delayed_work(di->wq, &di->dc_det_check_work,
3758 msecs_to_jiffies(10));
3760 case USB_BC_TYPE_SDP:
3761 case USB_BC_TYPE_CDP:/*nonstandard charger*/
3762 case USB_BC_TYPE_DCP:/*standard charger*/
3763 queue_delayed_work(di->wq, &di->ac_usb_check_work,
3764 msecs_to_jiffies(10));
3766 case USB_OTG_POWER_ON:/*otg on*/
3767 di->charge_otg = USB_OTG_POWER_ON;
3768 queue_delayed_work(di->wq, &di->otg_check_work,
3769 msecs_to_jiffies(10));
3771 case USB_OTG_POWER_OFF:/*otg off*/
3772 di->charge_otg = USB_OTG_POWER_OFF;
3773 queue_delayed_work(di->wq, &di->otg_check_work,
3774 msecs_to_jiffies(10));
3782 static irqreturn_t rk81x_vbat_lo_irq(int irq, void *bat)
3784 pr_info("\n------- %s:lower power warning!\n", __func__);
3786 rk_send_wakeup_key();
3791 static irqreturn_t rk81x_vbat_plug_in(int irq, void *bat)
3793 pr_info("\n------- %s:irq = %d\n", __func__, irq);
3794 rk_send_wakeup_key();
3798 static irqreturn_t rk81x_vbat_plug_out(int irq, void *bat)
3800 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3801 rk_send_wakeup_key();
3805 static irqreturn_t rk81x_vbat_charge_ok(int irq, void *bat)
3807 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3809 pr_info("\n---------- %s:irq = %d\n", __func__, irq);
3810 di->finish_sig_base = get_runtime_sec();
3811 rk_send_wakeup_key();
3815 static irqreturn_t rk81x_vbat_dc_det(int irq, void *bat)
3817 struct rk81x_battery *di = (struct rk81x_battery *)bat;
3819 queue_delayed_work(di->wq,
3820 &di->dc_det_check_work,
3821 msecs_to_jiffies(10));
3822 rk_send_wakeup_key();
3827 static int rk81x_bat_sysfs_init(struct rk81x_battery *di)
3832 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3833 ret = sysfs_create_file(&di->bat.dev->kobj,
3834 &rk818_bat_attr[i].attr);
3836 dev_err(di->dev, "create battery node(%s) error\n",
3837 rk818_bat_attr[i].attr.name);
3843 static void rk81x_bat_irq_init(struct rk81x_battery *di)
3845 int plug_in_irq, plug_out_irq, chrg_ok_irq, vb_lo_irq;
3847 struct rk818 *chip = di->rk818;
3849 #if defined(CONFIG_X86_INTEL_SOFIA)
3850 vb_lo_irq = chip->irq_base + RK818_IRQ_VB_LO;
3851 chrg_ok_irq = chip->irq_base + RK818_IRQ_CHG_OK;
3852 plug_in_irq = chip->irq_base + RK818_IRQ_PLUG_IN;
3853 plug_out_irq = chip->irq_base + RK818_IRQ_PLUG_OUT;
3855 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3856 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3857 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3858 chrg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3861 ret = request_threaded_irq(vb_lo_irq, NULL, rk81x_vbat_lo_irq,
3862 IRQF_TRIGGER_HIGH, "rk818_vbatlow", di);
3864 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3866 di->irq = vb_lo_irq;
3867 enable_irq_wake(di->irq);
3869 ret = request_threaded_irq(plug_in_irq, NULL, rk81x_vbat_plug_in,
3870 IRQF_TRIGGER_RISING, "rk81x_vbat_plug_in",
3873 dev_err(chip->dev, "plug_in_irq request failed!\n");
3875 ret = request_threaded_irq(plug_out_irq, NULL, rk81x_vbat_plug_out,
3876 IRQF_TRIGGER_FALLING, "rk81x_vbat_plug_out",
3879 dev_err(chip->dev, "plug_out_irq request failed!\n");
3881 ret = request_threaded_irq(chrg_ok_irq, NULL, rk81x_vbat_charge_ok,
3882 IRQF_TRIGGER_RISING, "rk81x_vbat_charge_ok",
3885 dev_err(chip->dev, "chrg_ok_irq request failed!\n");
3888 static void rk81x_bat_info_init(struct rk81x_battery *di,
3892 unsigned long time_base = get_runtime_sec();
3894 rk81x_bat_read(di, RK818_VB_MON_REG, &val, 1);
3895 if (val & PLUG_IN_STS)
3896 rk81x_bat_set_power_supply_state(di, USB_CHARGER);
3898 di->cell.config = di->pdata->cell_cfg;
3899 di->design_capacity = di->pdata->cell_cfg->design_capacity;
3900 di->qmax = di->pdata->cell_cfg->design_qmax;
3901 di->early_resume = 1;
3902 di->psy_status = POWER_SUPPLY_STATUS_DISCHARGING;
3903 di->bat_res = di->pdata->sense_resistor_mohm;
3904 di->dischrg_algorithm_mode = DISCHRG_NORMAL_MODE;
3905 di->last_zero_mode_dsoc = DEF_LAST_ZERO_MODE_SOC;
3906 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
3907 di->loader_charged = loader_charged;
3908 di->chrg_finish_base = time_base;
3909 di->power_on_base = time_base;
3910 di->plug_in_base = time_base;
3911 di->plug_out_base = time_base;
3912 di->finish_sig_base = time_base;
3913 di->fcc = rk81x_bat_get_fcc(di);
3916 static void rk81x_bat_dc_det_init(struct rk81x_battery *di,
3917 struct device_node *np)
3919 struct device *dev = di->dev;
3920 enum of_gpio_flags flags;
3923 di->dc_det_pin = of_get_named_gpio_flags(np, "dc_det_gpio", 0, &flags);
3924 if (di->dc_det_pin == -EPROBE_DEFER) {
3925 dev_err(dev, "dc_det_gpio error\n");
3929 if (gpio_is_valid(di->dc_det_pin)) {
3930 di->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW) ?
3931 RK818_DC_IN : RK818_DC_OUT;
3932 di->dc_det_irq = gpio_to_irq(di->dc_det_pin);
3934 ret = request_irq(di->dc_det_irq, rk81x_vbat_dc_det,
3935 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3936 "rk81x_dc_det", di);
3938 dev_err(di->dev, "rk818_dc_det_irq request failed!\n");
3941 enable_irq_wake(di->dc_det_irq);
3946 gpio_free(di->dc_det_pin);
3949 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
3954 struct timespec tv = {
3955 .tv_nsec = NSEC_PER_SEC >> 1,
3957 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
3959 err = rtc_read_time(rtc, &tm);
3961 dev_err(rtc->dev.parent,
3962 "hctosys: unable to read the hardware clock\n");
3964 err = rtc_valid_tm(&tm);
3966 dev_err(rtc->dev.parent,
3967 "hctosys: invalid date/time\n");
3970 rtc_tm_to_time(&tm, &tv.tv_sec);
3971 delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
3977 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
3979 struct device_node *np;
3980 struct battery_platform_data *pdata;
3981 struct cell_config *cell_cfg;
3982 struct ocv_config *ocv_cfg;
3983 struct property *prop;
3984 struct rk818 *rk818 = di->rk818;
3985 struct device *dev = di->dev;
3990 np = of_find_node_by_name(rk818->dev->of_node, "battery");
3992 dev_err(dev, "battery node not found!\n");
3996 pdata = devm_kzalloc(rk818->dev, sizeof(*pdata), GFP_KERNEL);
4000 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
4004 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
4008 prop = of_find_property(np, "ocv_table", &length);
4010 dev_err(dev, "ocv_table not found!\n");
4013 pdata->ocv_size = length / sizeof(u32);
4014 if (pdata->ocv_size <= 0) {
4015 dev_err(dev, "invalid ocv table\n");
4019 size = sizeof(*pdata->battery_ocv) * pdata->ocv_size;
4021 pdata->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
4022 if (!pdata->battery_ocv)
4025 ret = of_property_read_u32_array(np, "ocv_table", pdata->battery_ocv,
4030 /******************** charger param ****************************/
4031 ret = of_property_read_u32(np, "max_chrg_currentmA", &out_value);
4033 dev_err(dev, "max_chrg_currentmA not found!\n");
4034 out_value = DEFAULT_CHRG_CUR;
4036 pdata->max_charger_currentmA = out_value;
4038 ret = of_property_read_u32(np, "max_input_currentmA", &out_value);
4040 dev_err(dev, "max_charger_ilimitmA not found!\n");
4041 out_value = DEFAULT_INPUT_CUR;
4043 pdata->max_charger_ilimitmA = out_value;
4045 ret = of_property_read_u32(np, "bat_res", &out_value);
4047 dev_err(dev, "bat_res not found!\n");
4048 out_value = DEFAULT_BAT_RES;
4050 pdata->sense_resistor_mohm = out_value;
4052 ret = of_property_read_u32(np, "max_charge_voltagemV", &out_value);
4054 dev_err(dev, "max_charge_voltagemV not found!\n");
4055 out_value = DEFAULT_CHRG_VOL;
4057 pdata->max_charger_voltagemV = out_value;
4059 ret = of_property_read_u32(np, "design_capacity", &out_value);
4061 dev_err(dev, "design_capacity not found!\n");
4064 cell_cfg->design_capacity = out_value;
4066 ret = of_property_read_u32(np, "design_qmax", &out_value);
4068 dev_err(dev, "design_qmax not found!\n");
4071 cell_cfg->design_qmax = out_value;
4073 ret = of_property_read_u32(np, "sleep_enter_current", &out_value);
4075 dev_err(dev, "sleep_enter_current not found!\n");
4076 out_value = DEFAULT_SLP_ENTER_CUR;
4078 ocv_cfg->sleep_enter_current = out_value;
4080 ret = of_property_read_u32(np, "sleep_exit_current", &out_value);
4082 dev_err(dev, "sleep_exit_current not found!\n");
4083 out_value = DEFAULT_SLP_EXIT_CUR;
4085 ocv_cfg->sleep_exit_current = out_value;
4087 ret = of_property_read_u32(np, "power_off_thresd", &out_value);
4089 dev_warn(dev, "power_off_thresd not found!\n");
4090 out_value = PWR_OFF_THRESD;
4092 pdata->power_off_thresd = out_value;
4094 of_property_read_u32(np, "chrg_diff_voltagemV", &pdata->chrg_diff_vol);
4095 of_property_read_u32(np, "virtual_power", &di->fg_drv_mode);
4096 di->fg_drv_mode = di->fg_drv_mode ? TEST_POWER_MODE : FG_NORMAL_MODE;
4098 /************* charger support adp types **********************/
4099 ret = of_property_read_u32(np, "support_usb_adp", &support_usb_adp);
4100 ret = of_property_read_u32(np, "support_dc_adp", &support_dc_adp);
4102 if (!support_usb_adp && !support_dc_adp) {
4103 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
4104 support_usb_adp = 1;
4107 /*if (support_dc_adp)*/
4108 rk81x_bat_dc_det_init(di, np);
4110 cell_cfg->ocv = ocv_cfg;
4111 pdata->cell_cfg = cell_cfg;
4114 DBG("\nthe battery dts info dump:\n"
4116 "max_input_currentmA:%d\n"
4117 "max_chrg_currentmA:%d\n"
4118 "max_charge_voltagemV:%d\n"
4119 "design_capacity:%d\n"
4121 "sleep_enter_current:%d\n"
4122 "sleep_exit_current:%d\n"
4123 "support_usb_adp:%d\n"
4124 "support_dc_adp:%d\n"
4125 "power_off_thresd:%d\n",
4126 pdata->sense_resistor_mohm, pdata->max_charger_ilimitmA,
4127 pdata->max_charger_currentmA, pdata->max_charger_voltagemV,
4128 cell_cfg->design_capacity, cell_cfg->design_qmax,
4129 cell_cfg->ocv->sleep_enter_current,
4130 cell_cfg->ocv->sleep_exit_current,
4131 support_usb_adp, support_dc_adp, pdata->power_off_thresd);
4137 static int rk81x_bat_parse_dt(struct rk81x_battery *di)
4143 static int rk81x_battery_probe(struct platform_device *pdev)
4145 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
4146 struct rk81x_battery *di;
4149 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
4153 di->dev = &pdev->dev;
4154 platform_set_drvdata(pdev, di);
4156 ret = rk81x_bat_parse_dt(di);
4158 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
4162 rk81x_bat_info_init(di, chip);
4163 if (!is_rk81x_bat_exist(di)) {
4164 dev_info(di->dev, "not battery, enter test power mode\n");
4165 di->fg_drv_mode = TEST_POWER_MODE;
4168 ret = rk81x_bat_power_supply_init(di);
4170 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
4174 rk81x_bat_irq_init(di);
4175 rk81x_bat_sysfs_init(di);
4177 rk81x_bat_fg_init(di);
4178 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
4180 rk81x_bat_flatzone_vol_init(di);
4182 #if defined(CONFIG_X86_INTEL_SOFIA)
4183 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
4184 if (IS_ERR_OR_NULL(di->usb_phy)) {
4185 dev_err(di->dev, "get usb phy failed\n");
4186 return PTR_ERR(di->usb_phy);
4188 di->usb_nb.notifier_call = rk81x_battery_usb_notifier;
4189 ret = usb_register_notifier(di->usb_phy, &di->usb_nb);
4191 dev_err(di->dev, "registr usb phy notification failed\n");
4192 INIT_DELAYED_WORK(&di->usb_phy_delay_work,
4193 rk81x_battery_usb_notifier_delayed_work);
4196 rk81x_battery_register_fb_notify(di);
4197 di->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM | WQ_FREEZABLE,
4198 "rk81x-battery-work");
4199 INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
4200 INIT_DELAYED_WORK(&di->chrg_term_mode_switch_work,
4201 rk81x_chrg_term_mode_switch_work);
4203 queue_delayed_work(di->wq, &di->battery_monitor_work,
4204 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4206 #if defined(CONFIG_ARCH_ROCKCHIP)
4207 INIT_DELAYED_WORK(&di->otg_check_work,
4208 rk81x_battery_otg_delay_work);
4209 INIT_DELAYED_WORK(&di->ac_usb_check_work,
4210 rk81x_battery_acusb_delay_work);
4211 INIT_DELAYED_WORK(&di->dc_det_check_work,
4212 rk81x_battery_dc_delay_work);
4214 queue_delayed_work(di->wq, &di->dc_det_check_work,
4215 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
4217 di->battery_nb.notifier_call = rk81x_bat_usb_notifier_call;
4218 rk_bc_detect_notifier_register(&di->battery_nb, &di->charge_otg);
4220 dev_info(di->dev, "battery driver version %s\n", DRIVER_VERSION);
4225 static int rk81x_battery_suspend(struct platform_device *dev,
4228 struct rk81x_battery *di = platform_get_drvdata(dev);
4230 /*while otg and dc both plugin*/
4231 rk81x_bat_set_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4233 di->slp_psy_status = rk81x_chrg_online(di);
4234 di->chrg_status = rk81x_bat_get_chrg_status(di);
4235 di->slp_chrg_status = rk81x_bat_get_chrg_status(di);
4236 di->suspend_charge_current = rk81x_bat_get_avg_current(di);
4237 di->dischrg_save_sec += rk81x_bat_save_dischrg_sec(di);
4238 di->dischrg_normal_base = 0;
4239 di->dischrg_emu_base = 0;
4240 do_gettimeofday(&di->suspend_rtc_base);
4242 if (!rk81x_chrg_online(di)) {
4243 di->chrg_save_sec += rk81x_bat_save_chrg_sec(di);
4244 di->chrg_normal_base = 0;
4245 di->chrg_emu_base = 0;
4246 di->chrg_term_base = 0;
4247 di->chrg_finish_base = 0;
4252 * do not modify the g_base_sec
4254 g_base_sec = get_runtime_sec();
4256 pr_info("battery suspend dl=%d rl=%d c=%d v=%d at=%ld st=0x%x chg=%d\n",
4257 di->dsoc, di->rsoc, di->suspend_charge_current, di->voltage,
4258 di->suspend_time_sum, di->chrg_status, di->slp_psy_status);
4263 static int rk81x_battery_resume(struct platform_device *dev)
4265 struct rk81x_battery *di = platform_get_drvdata(dev);
4266 int pwroff_thresd = di->pdata->power_off_thresd;
4272 /*while otg and dc both plugin*/
4273 rk81x_bat_clr_bit(di, NT_STS_MSK_REG2, CHRG_CVTLMT_INT);
4275 di->discharge_smooth_status = true;
4276 di->charge_smooth_status = true;
4278 vol = rk81x_bat_get_vol(di);
4279 if (vol < INVALID_VOL_THRESD) {
4280 dev_err(di->dev, "invalid voltage :%d", vol);
4285 di->current_avg = rk81x_bat_get_avg_current(di);
4286 di->relax_voltage = rk81x_bat_get_relax_vol(di);
4287 di->est_ocv_vol = rk81x_bat_est_ocv_vol(di);
4288 di->est_ocv_soc = rk81x_bat_est_ocv_soc(di);
4289 delta_time = rk81x_bat_get_suspend_sec(di);
4290 di->suspend_time_sum += delta_time;
4291 #if defined(CONFIG_ARCH_ROCKCHIP)
4292 di->remain_capacity = rk81x_bat_get_realtime_capacity(di);
4295 if (di->slp_psy_status) {
4296 time_step = CHRG_TIME_STEP;
4298 if (di->voltage <= pwroff_thresd + 50)
4299 time_step = DISCHRG_TIME_STEP_0;
4301 time_step = DISCHRG_TIME_STEP_1;
4304 pr_info("battery resume c=%d v=%d ev=%d rv=%d dt=%d at=%ld chg=%d\n",
4305 di->current_avg, di->voltage, di->est_ocv_vol,
4306 di->relax_voltage, delta_time, di->suspend_time_sum,
4307 di->slp_psy_status);
4309 if (di->suspend_time_sum > time_step) {
4310 delta_soc = rk81x_bat_update_resume_state(di);
4312 di->suspend_time_sum = 0;
4315 if ((!rk81x_chrg_online(di) && di->voltage <= pwroff_thresd) ||
4316 rk81x_chrg_online(di))
4317 wake_lock_timeout(&di->resume_wake_lock, 5 * HZ);
4320 * do not modify the g_base_sec
4322 if (is_local_clock_reset())
4323 g_base_sec += delta_time;
4330 static int rk81x_battery_remove(struct platform_device *dev)
4332 struct rk81x_battery *di = platform_get_drvdata(dev);
4334 cancel_delayed_work_sync(&di->battery_monitor_work);
4338 static void rk81x_battery_shutdown(struct platform_device *dev)
4340 struct rk81x_battery *di = platform_get_drvdata(dev);
4342 cancel_delayed_work_sync(&di->battery_monitor_work);
4343 rk_bc_detect_notifier_unregister(&di->battery_nb);
4345 if (BASE_TO_MIN(di->power_on_base) <= REBOOT_INTER_MIN)
4346 rk81x_bat_check_reboot(di);
4348 rk81x_bat_save_reboot_cnt(di, 0);
4349 rk81x_chrg_term_mode_set(di, CHRG_TERM_ANA_SIGNAL);
4352 static struct platform_driver rk81x_battery_driver = {
4354 .name = "rk818-battery",
4355 .owner = THIS_MODULE,
4358 .probe = rk81x_battery_probe,
4359 .remove = rk81x_battery_remove,
4360 .suspend = rk81x_battery_suspend,
4361 .resume = rk81x_battery_resume,
4362 .shutdown = rk81x_battery_shutdown,
4365 static int __init battery_init(void)
4367 return platform_driver_register(&rk81x_battery_driver);
4370 fs_initcall_sync(battery_init);
4371 static void __exit battery_exit(void)
4373 platform_driver_unregister(&rk81x_battery_driver);
4375 module_exit(battery_exit);
4377 MODULE_LICENSE("GPL");
4378 MODULE_ALIAS("platform:rk818-battery");
4379 MODULE_AUTHOR("ROCKCHIP");