4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/param.h>
11 #include <linux/jiffies.h>
12 #include <linux/workqueue.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include <linux/gpio.h>
21 #include <linux/proc_fs.h>
22 #include <asm/uaccess.h>
23 #include <linux/power/rk818_battery.h>
24 #include <linux/mfd/rk818.h>
25 #include <linux/time.h>
26 #include <linux/interrupt.h>
27 #include <linux/rtc.h>
28 #include <linux/wakelock.h>
30 /* if you want to disable, don't set it as 0, just be: "static int dbg_enable;" is ok*/
31 static int dbg_enable;
32 #define RK818_SYS_DBG 1
34 module_param_named(dbg_level, dbg_enable, int, 0644);
35 #define DBG(args...) \
43 #define INTERPOLATE_MAX 1000
44 #define MAX_INT 0x7FFF
45 #define TIME_10MIN_SEC 600
49 struct cell_state cell;
50 struct power_supply bat;
51 struct power_supply ac;
52 struct power_supply usb;
53 struct delayed_work work;
54 /* struct i2c_client *client; */
57 struct battery_platform_data *platform_data;
69 uint16_t relax_voltage;
73 bool pcb_ioffset_updated;
74 unsigned long queue_work_cnt;
75 uint16_t warnning_voltage;
88 int bat_res_update_cnt;
95 unsigned long dod0_time;
106 int current_k;/* (ICALIB0, ICALIB1) */
109 int voltage_k;/* VCALIB0 VCALIB1 */
120 struct timeval soc_timer;
121 struct timeval change_timer;
124 int charge_smooth_time;
126 int suspend_capacity;
128 struct timespec suspend_time;
129 struct timespec resume_time;
130 unsigned long suspend_time_start;
131 unsigned long count_sleep_time;
133 unsigned long dischrg_sum_sleep_sec;
134 unsigned long dischrg_sum_sleep_capacity;
135 int suspend_temp_soc;
137 int suspend_charge_current;
140 bool bat_res_updated;
141 bool charge_smooth_status;
143 unsigned long last_plugin_time;
146 unsigned long charging_time;
147 unsigned long discharging_time;
149 struct notifier_block battery_nb;
150 struct workqueue_struct *wq;
151 struct delayed_work battery_monitor_work;
152 struct delayed_work charge_check_work;
155 struct wake_lock resume_wake_lock;
157 int debug_finish_real_soc;
158 int debug_finish_temp_soc;
161 struct battery_info *g_battery;
162 u32 support_uboot_chrg;
164 extern int dwc_vbus_status(void);
165 extern int get_gadget_connect_flag(void);
166 extern int dwc_otg_check_dpdm(void);
167 extern void kernel_power_off(void);
168 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
169 extern unsigned int irq_create_mapping(struct irq_domain *domain,
170 irq_hw_number_t hwirq);
171 extern void rk_send_wakeup_key(void);
172 static void update_battery_info(struct battery_info *di);
174 #define SUPPORT_USB_CHARGE
177 static u32 interpolate(int value, u32 *table, int size)
182 for (i = 0; i < size; i++) {
183 if (value < table[i])
187 if ((i > 0) && (i < size)) {
188 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
189 d /= table[i] - table[i-1];
190 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
192 d = i * ((INTERPOLATE_MAX+size/2)/size);
200 /* Returns (a * b) / c */
201 static int32_t ab_div_c(u32 a, u32 b, u32 c)
207 sign = ((((a^b)^c) & 0x80000000) != 0);
213 tmp = ((int32_t) a*b + (c>>1)) / c;
225 static int32_t abs_int(int32_t x)
227 return (x > 0) ? x : -x;
230 static int abs32_int(int x)
232 return (x > 0) ? x : -x;
236 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
240 ret = rk818_i2c_read(rk818, reg, len, buf);
244 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
247 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
250 static void dump_gauge_register(struct battery_info *di)
254 DBG("%s dump charger register start: \n", __func__);
255 for (i = 0xAC; i < 0xDF; i++) {
256 battery_read(di->rk818, i, &buf, 1);
257 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
262 static void dump_charger_register(struct battery_info *di)
267 DBG("%s dump the register start: \n", __func__);
268 for (i = 0x99; i < 0xAB; i++) {
269 battery_read(di->rk818, i, &buf, 1);
270 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
278 static uint16_t _get_OCV_voltage(struct battery_info *di);
279 static int _voltage_to_capacity(struct battery_info *di, int voltage);
280 static int _get_realtime_capacity(struct battery_info *di);
281 static void power_on_save(struct battery_info *di, int voltage);
282 static void _capacity_init(struct battery_info *di, u32 capacity);
283 static void battery_poweron_status_init(struct battery_info *di);
284 static void power_on_save(struct battery_info *di, int voltage);
285 static void flatzone_voltage_init(struct battery_info *di);
286 static int _get_FCC_capacity(struct battery_info *di);
287 static void _save_FCC_capacity(struct battery_info *di, u32 capacity);
288 static int _get_soc(struct battery_info *di);
289 static int _get_average_current(struct battery_info *di);
290 static int rk_battery_voltage(struct battery_info *di);
291 static uint16_t _get_relax_vol1(struct battery_info *di);
292 static uint16_t _get_relax_vol2(struct battery_info *di);
293 static void update_battery_info(struct battery_info *di);
295 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
297 struct battery_info *di = g_battery;
303 battery_read(di->rk818, SUP_STS_REG, &status, 1);
304 battery_read(di->rk818, SOC_REG, &soc_reg, 1);
305 battery_read(di->rk818, 0x00, &rtc_val, 1);
306 di->voltage_ocv = _get_OCV_voltage(di);
307 _voltage_to_capacity(di, di->voltage_ocv);
308 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
310 return sprintf(buf, "-----------------------------------------------------------------------------\n"
311 "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
312 "real_soc = %d, temp_soc = %d\n"
313 "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
314 "usb_online = %d, ac_online = %d\n"
315 "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
316 "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
317 "relax_volt1 = %d, relax_volt2 = %d\n"
318 "---------------------------------------------------------------------------\n",
319 rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
320 di->real_soc, _get_soc(di),
321 di->fcc, _get_FCC_capacity(di), shtd_time,
322 di->usb_online, di->ac_online,
324 di->voltage_k, di->voltage_b, soc_reg,
325 _get_relax_vol1(di), _get_relax_vol2(di));
328 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
330 struct battery_info *di = g_battery;
331 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
332 u8 usb_ctrl_reg, chrg_ctrl_reg1;
333 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
335 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
336 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
337 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
338 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
339 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
340 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
341 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
342 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
343 battery_read(di->rk818, 0x00, &rtc_val, 1);
345 return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
346 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
347 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
348 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
349 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
350 "---------------------------------------------------------------------------\n",
351 ggcon_reg, ggsts_reg, rtc_val,
352 sup_tst_reg, vb_mod_reg,
353 usb_ctrl_reg, chrg_ctrl_reg1,
354 chrg_ctrl_reg2, chrg_ctrl_reg3
357 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
359 struct battery_info *di = g_battery;
361 return sprintf(buf, "%d", di->fcc);
363 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
365 struct battery_info *di = g_battery;
367 return sprintf(buf, "%d", di->real_soc);
370 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
372 struct battery_info *di = g_battery;
374 return sprintf(buf, "%d", di->temp_soc);
377 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
379 struct battery_info *di = g_battery;
381 return sprintf(buf, "%d", di->voltage);
384 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
386 struct battery_info *di = g_battery;
388 return sprintf(buf, "%d", di->current_avg);
391 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
393 struct battery_info *di = g_battery;
395 return sprintf(buf, "%d", di->remain_capacity);
398 static struct device_attribute rk818_bat_attr[] = {
399 __ATTR(state, 0664, bat_state_read, NULL),
400 __ATTR(regs, 0664, bat_reg_read, NULL),
401 __ATTR(fcc, 0664, bat_fcc_read, NULL),
402 __ATTR(soc, 0664, bat_soc_read, NULL),
403 __ATTR(temp_soc, 0664, bat_temp_soc_read, NULL),
404 __ATTR(voltage, 0664, bat_voltage_read, NULL),
405 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
406 __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
411 static uint16_t get_relax_voltage(struct battery_info *di);
413 static ssize_t show_state_attrs(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct battery_info *data = g_battery;
418 if (0 == get_relax_voltage(data)) {
420 "voltage = %d, remain_capacity = %d, status = %d\n",
421 data->voltage, data->remain_capacity,
426 "voltage = %d, remain_capacity = %d, status = %d\n",
427 get_relax_voltage(data), data->remain_capacity,
431 static ssize_t restore_state_attrs(struct device *dev,
432 struct device_attribute *attr, const char *buf, size_t size)
436 static struct device_attribute rkbatt_attrs[] = {
437 __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
440 static int create_sysfs_interfaces(struct device *dev)
444 for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++) {
445 if (device_create_file(dev, rkbatt_attrs + liTmep))
452 for (; liTmep >= 0; liTmep--)
453 device_remove_file(dev, rkbatt_attrs + liTmep);
455 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
459 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
463 battery_read(di->rk818, reg, &val, 1);
464 DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
469 static int _gauge_enable(struct battery_info *di)
474 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
476 dev_err(di->dev, "error reading TS_CTRL_REG");
479 if (!(buf & GG_EN)) {
481 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); /* enable */
482 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
486 DBG("%s, %d\n", __func__, buf);
490 static void save_level(struct battery_info *di, u8 save_soc)
495 battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
497 static u8 get_level(struct battery_info *di)
501 battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
505 static int _get_vcalib0(struct battery_info *di)
511 ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
513 ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
516 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
520 static int _get_vcalib1(struct battery_info *di)
526 ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
528 ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
531 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
535 static int _get_ioffset(struct battery_info *di)
542 ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
544 ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
550 static uint16_t _get_cal_offset(struct battery_info *di)
556 ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
558 ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
563 static int _set_cal_offset(struct battery_info *di, u32 value)
569 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
570 buf = (value >> 8)&0xff;
571 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
575 static void _get_voltage_offset_value(struct battery_info *di)
577 int vcalib0, vcalib1;
579 vcalib0 = _get_vcalib0(di);
580 vcalib1 = _get_vcalib1(di);
582 di->voltage_k = (4200 - 3000)*1000/(vcalib1 - vcalib0);
583 di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
584 DBG("voltage_k = %d(x1000) voltage_b = %d\n", di->voltage_k, di->voltage_b);
586 static uint16_t _get_OCV_voltage(struct battery_info *di)
591 uint16_t voltage_now = 0;
593 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
595 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
599 dev_err(di->dev, "error read BAT_OCV_REGH");
603 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
608 static int rk_battery_voltage(struct battery_info *di)
615 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
617 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
621 dev_err(di->dev, "error read BAT_VOL_REGH");
625 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
631 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
632 * based on the voltage.
634 static int _voltage_to_capacity(struct battery_info *di, int voltage)
640 ocv_table = di->platform_data->battery_ocv;
641 ocv_size = di->platform_data->ocv_size;
642 di->warnning_voltage = ocv_table[3];
643 tmp = interpolate(voltage, ocv_table, ocv_size);
644 di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
645 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
650 static uint16_t _get_relax_vol1(struct battery_info *di)
654 uint16_t temp = 0, voltage_now;
656 ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
658 ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
661 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
666 static uint16_t _get_relax_vol2(struct battery_info *di)
669 uint16_t temp = 0, voltage_now;
672 ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
674 ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
677 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
682 static int _get_raw_adc_current(struct battery_info *di)
688 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
690 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
694 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
696 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
699 current_now |= (buf<<8);
702 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
711 static void ioffset_sample_time(struct battery_info *di, int time)
715 battery_read(di->rk818, GGCON, &ggcon, 1);
716 ggcon &= ~(0x30); /*clear <5:4>*/
718 battery_write(di->rk818, GGCON, &ggcon, 1);
719 debug_reg(di, GGCON, "GGCON");
722 static void update_cal_offset(struct battery_info *di)
724 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
726 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
727 if ((!mod) && (di->pcb_ioffset_updated)) {
728 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
729 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
734 static void zero_current_calibration(struct battery_info *di)
743 if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
745 for (retry = 0; retry < 5; retry++) {
746 adc_value = _get_raw_adc_current(di);
747 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
749 C0 = _get_cal_offset(di);
751 _set_cal_offset(di, C1);
752 DBG("<%s>. C1 = %d\n", __func__, C1);
755 adc_value = _get_raw_adc_current(di);
756 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
759 ioffset = _get_ioffset(di);
760 pcb_offset = C1 - ioffset;
761 di->pcb_ioffset = pcb_offset;
762 di->pcb_ioffset_updated = true;
763 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
766 di->pcb_ioffset_updated = false;
772 static bool _is_relax_mode(struct battery_info *di)
777 ret = battery_read(di->rk818, GGSTS, &status, 1);
779 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
785 static uint16_t get_relax_voltage(struct battery_info *di)
789 uint16_t relax_vol1, relax_vol2;
792 ret = battery_read(di->rk818, GGSTS, &status, 1);
793 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
795 relax_vol1 = _get_relax_vol1(di);
796 relax_vol2 = _get_relax_vol2(di);
797 DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
798 if (_is_relax_mode(di))
799 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
804 static void _set_relax_thres(struct battery_info *di)
807 int enter_thres, exit_thres;
808 struct cell_state *cell = &di->cell;
810 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
811 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
813 buf = enter_thres&0xff;
814 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
815 buf = (enter_thres>>8)&0xff;
816 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
818 buf = exit_thres&0xff;
819 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
820 buf = (exit_thres>>8)&0xff;
821 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
823 /* set sample time */
824 battery_read(di->rk818, GGCON, &buf, 1);
825 buf &= ~(3<<2);/*8min*/
826 buf &= ~0x01; /* clear bat_res calc*/
827 battery_write(di->rk818, GGCON, &buf, 1);
830 static void restart_relax(struct battery_info *di)
832 u8 ggcon;/* chrg_ctrl_reg2;*/
835 battery_read(di->rk818, GGCON, &ggcon, 1);
837 battery_write(di->rk818, GGCON, &ggcon, 1);
839 battery_read(di->rk818, GGSTS, &ggsts, 1);
841 battery_write(di->rk818, GGSTS, &ggsts, 1);
844 static int _get_average_current(struct battery_info *di)
851 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
853 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
857 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
859 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
862 current_now |= (buf<<8);
864 if (current_now & 0x800)
867 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
873 static bool is_bat_exist(struct battery_info *di)
877 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
878 return (buf & 0x80) ? true : false;
881 static bool _is_first_poweron(struct battery_info *di)
886 battery_read(di->rk818, GGSTS, &buf, 1);
887 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
888 /*di->pwron_bat_con = buf;*/
892 battery_write(di->rk818, GGSTS, &buf, 1);
893 battery_read(di->rk818, GGSTS, &temp, 1);
894 } while (temp&BAT_CON);
899 static void flatzone_voltage_init(struct battery_info *di)
906 ocv_table = di->platform_data->battery_ocv;
907 ocv_size = di->platform_data->ocv_size;
909 for (j = 0; j < 21; j++)
913 for (i = 1; i < ocv_size-1; i++) {
914 if (ocv_table[i+1] < ocv_table[i] + 20)
918 temp_table[j] = temp_table[j-1]+1;
920 di->enter_flatzone = ocv_table[i];
924 for (i = 0; i <= 20; i++) {
925 if (temp_table[i] < temp_table[i+1])
930 di->exit_flatzone = ocv_table[i];
932 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
937 static int is_not_flatzone(struct battery_info *di, int voltage)
939 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
940 DBG("<%s>. is in flat zone\n", __func__);
943 DBG("<%s>. is not in flat zone\n", __func__);
948 static void power_on_save(struct battery_info *di, int voltage)
953 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
955 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
956 _voltage_to_capacity(di, voltage);
957 if (di->temp_soc < 20) {
958 di->dod0_voltage = voltage;
959 di->dod0_capacity = di->nac;
961 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
964 if (di->temp_soc <= 0)
965 di->dod0_level = 100;
966 else if (di->temp_soc < 5)
968 else if (di->temp_soc < 10)
970 /* save_soc = di->dod0_level; */
971 save_soc = get_level(di);
972 if (save_soc < di->dod0_level)
973 save_soc = di->dod0_level;
974 save_level(di, save_soc);
975 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
982 static int _get_soc(struct battery_info *di)
984 return di->remain_capacity * 100 / di->fcc;
987 static enum power_supply_property rk_battery_props[] = {
989 POWER_SUPPLY_PROP_STATUS,
990 POWER_SUPPLY_PROP_CURRENT_NOW,
991 POWER_SUPPLY_PROP_VOLTAGE_NOW,
992 POWER_SUPPLY_PROP_PRESENT,
993 POWER_SUPPLY_PROP_HEALTH,
994 POWER_SUPPLY_PROP_CAPACITY,
997 #define to_device_info(x) container_of((x), \
998 struct battery_info, bat)
1000 static int rk_battery_get_property(struct power_supply *psy,
1001 enum power_supply_property psp,
1002 union power_supply_propval *val)
1005 struct battery_info *di = to_device_info(psy);
1008 case POWER_SUPPLY_PROP_CURRENT_NOW:
1009 val->intval = di->current_avg*1000;/*uA*/
1012 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1013 val->intval = di->voltage*1000;/*uV*/
1016 case POWER_SUPPLY_PROP_PRESENT:
1017 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1018 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1019 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1023 case POWER_SUPPLY_PROP_CAPACITY:
1024 val->intval = di->real_soc;
1027 case POWER_SUPPLY_PROP_HEALTH:
1028 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1031 case POWER_SUPPLY_PROP_STATUS:
1032 val->intval = di->status;
1043 static enum power_supply_property rk_battery_ac_props[] = {
1044 POWER_SUPPLY_PROP_ONLINE,
1046 static enum power_supply_property rk_battery_usb_props[] = {
1047 POWER_SUPPLY_PROP_ONLINE,
1051 #define to_ac_device_info(x) container_of((x), \
1052 struct battery_info, ac)
1054 static int rk_battery_ac_get_property(struct power_supply *psy,
1055 enum power_supply_property psp,
1056 union power_supply_propval *val)
1059 struct battery_info *di = to_ac_device_info(psy);
1062 case POWER_SUPPLY_PROP_ONLINE:
1063 val->intval = di->ac_online; /*discharging*/
1073 #define to_usb_device_info(x) container_of((x), \
1074 struct battery_info, usb)
1076 static int rk_battery_usb_get_property(struct power_supply *psy,
1077 enum power_supply_property psp,
1078 union power_supply_propval *val)
1081 struct battery_info *di = to_usb_device_info(psy);
1084 case POWER_SUPPLY_PROP_ONLINE:
1085 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1088 val->intval = di->usb_online;
1100 static void battery_power_supply_init(struct battery_info *di)
1102 di->bat.name = "BATTERY";
1103 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1104 di->bat.properties = rk_battery_props;
1105 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1106 di->bat.get_property = rk_battery_get_property;
1109 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1110 di->ac.properties = rk_battery_ac_props;
1111 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1112 di->ac.get_property = rk_battery_ac_get_property;
1114 di->usb.name = "USB";
1115 di->usb.type = POWER_SUPPLY_TYPE_USB;
1116 di->usb.properties = rk_battery_usb_props;
1117 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1118 di->usb.get_property = rk_battery_usb_get_property;
1121 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1125 ret = power_supply_register(dev, &di->bat);
1127 dev_err(dev, "failed to register main battery\n");
1130 ret = power_supply_register(dev, &di->usb);
1132 dev_err(dev, "failed to register usb power supply\n");
1135 ret = power_supply_register(dev, &di->ac);
1137 dev_err(dev, "failed to register ac power supply\n");
1144 power_supply_unregister(&di->ac);
1146 power_supply_unregister(&di->usb);
1148 power_supply_unregister(&di->bat);
1153 static void _capacity_init(struct battery_info *di, u32 capacity)
1160 di->voltage_old = 0;
1161 di->display_soc = 0;
1163 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1165 buf = (capacity_ma>>24)&0xff;
1166 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1167 buf = (capacity_ma>>16)&0xff;
1168 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1169 buf = (capacity_ma>>8)&0xff;
1170 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1171 buf = (capacity_ma&0xff) | 0x01;
1172 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1173 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1179 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1184 if (capacity >= di->qmax)
1185 capacity = di->qmax;
1187 capacity_ma = capacity;
1189 buf = (capacity_ma>>24)&0xff;
1190 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1191 buf = (capacity_ma>>16)&0xff;
1192 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1193 buf = (capacity_ma>>8)&0xff;
1194 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1195 buf = (capacity_ma&0xff) | 0x01;
1196 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1199 static int _get_remain_capacity(struct battery_info *di)
1206 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1208 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1210 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1212 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1215 capacity = temp;/* /4096*900/14/36*500/521; */
1221 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1226 capacity_ma = capacity;
1227 buf = (capacity_ma>>24)&0xff;
1228 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1229 buf = (capacity_ma>>16)&0xff;
1230 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1231 buf = (capacity_ma>>8)&0xff;
1232 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1233 buf = (capacity_ma&0xff) | 0x01;
1234 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1237 static int _get_FCC_capacity(struct battery_info *di)
1244 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1246 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1248 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1250 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1254 capacity = temp-1;/* 4096*900/14/36*500/521 */
1257 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1262 static int _get_realtime_capacity(struct battery_info *di)
1269 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1271 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1273 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1275 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1278 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1283 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1285 int remain_capacity;
1293 now_temp_soc = _get_soc(di);
1294 _voltage_to_capacity(di, relax_voltage);
1295 relax_soc = di->temp_soc;
1296 relax_capacity = di->temp_nac;
1297 abs_soc = abs32_int(relax_soc - now_temp_soc);
1299 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1300 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1303 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1304 remain_capacity = relax_capacity;
1305 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1308 remain_capacity = _get_realtime_capacity(di);
1309 else if (abs_soc <= 10)
1310 remain_capacity = relax_capacity;
1311 else if (abs_soc <= 20)
1312 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1314 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1316 _capacity_init(di, remain_capacity);
1317 di->temp_soc = _get_soc(di);
1318 di->remain_capacity = _get_realtime_capacity(di);
1321 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1322 if (relax_soc < now_temp_soc) {
1323 if (di->suspend_temp_soc - relax_soc <= 5)
1324 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1325 else if (di->suspend_temp_soc - relax_soc <= 10)
1326 di->real_soc = di->real_soc - 5;
1328 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1330 now_current = _get_average_current(di);
1331 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1332 min = soc_time / 60;
1333 if (sleep_min > min)
1337 DBG("<%s>. new_temp_soc=%d, new_real_soc=%d, new_remain_cap=%d\n", __func__, _get_soc(di), di->real_soc, di->remain_capacity);
1341 static int _copy_soc(struct battery_info *di, u8 save_soc)
1346 battery_write(di->rk818, SOC_REG, &soc, 1);
1350 static bool support_uboot_charge(void)
1352 return support_uboot_chrg?true:false;
1355 static int _rsoc_init(struct battery_info *di)
1359 u32 remain_capacity;
1362 #ifdef SUPPORT_USB_CHARGE
1367 di->voltage = rk_battery_voltage(di);
1368 di->voltage_ocv = _get_OCV_voltage(di);
1369 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1371 if (_is_first_poweron(di)) {
1372 _save_FCC_capacity(di, di->design_capacity);
1373 di->fcc = _get_FCC_capacity(di);
1375 _voltage_to_capacity(di, di->voltage_ocv);
1376 di->real_soc = di->temp_soc;
1377 di->nac = di->temp_nac;
1378 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1381 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1382 init_soc = pwron_soc;
1383 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1385 #ifdef SUPPORT_USB_CHARGE
1386 otg_status = dwc_otg_check_dpdm();
1387 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1389 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1392 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1393 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1395 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1398 remain_capacity = _get_remain_capacity(di);
1400 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1401 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1402 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1403 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1405 if (!support_uboot_charge()) {
1406 _voltage_to_capacity(di, di->voltage_ocv);
1407 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1409 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1410 if (((otg_status != 0) && (curr_shtd_time > 0) && (last_shtd_time != curr_shtd_time)) || ((curr_shtd_time > 0) && (otg_status == 0))) {
1412 if (curr_shtd_time > 30) {
1413 remain_capacity = di->temp_nac;
1414 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1416 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - di->real_soc) >= 10)) {
1417 if (remain_capacity >= di->temp_nac*120/100)
1418 remain_capacity = di->temp_nac*110/100;
1419 else if (remain_capacity < di->temp_nac*8/10)
1420 remain_capacity = di->temp_nac*9/10;
1422 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1427 di->real_soc = init_soc;
1428 di->nac = remain_capacity;
1431 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1437 static u8 get_charge_status(struct battery_info *di)
1442 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1447 DBG(" CHARGE-OFF ...\n");
1452 DBG(" DEAD CHARGE ...\n");
1455 case TRICKLE_CHARGE: /* (0x02 << 4) */
1457 DBG(" TRICKLE CHARGE ...\n ");
1460 case CC_OR_CV: /* (0x03 << 4) */
1462 DBG(" CC or CV ...\n");
1465 case CHARGE_FINISH: /* (0x04 << 4) */
1466 ret = CHARGE_FINISH;
1467 DBG(" CHARGE FINISH ...\n");
1470 case USB_OVER_VOL: /* (0x05 << 4) */
1472 DBG(" USB OVER VOL ...\n");
1475 case BAT_TMP_ERR: /* (0x06 << 4) */
1477 DBG(" BAT TMP ERROR ...\n");
1480 case TIMER_ERR: /* (0x07 << 4) */
1482 DBG(" TIMER ERROR ...\n");
1485 case USB_EXIST: /* (1 << 1)// usb is exists */
1487 DBG(" USB EXIST ...\n");
1490 case USB_EFF: /* (1 << 0)// usb is effective */
1492 DBG(" USB EFF...\n");
1502 static void set_charge_current(struct battery_info *di, int charge_current)
1506 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1507 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1508 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1509 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1512 static void rk_battery_charger_init(struct battery_info *di)
1514 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1517 DBG("%s start\n", __func__);
1518 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1519 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1520 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1521 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1522 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1524 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1525 usb_ctrl_reg &= (~0x0f);
1526 #ifdef SUPPORT_USB_CHARGE
1527 usb_ctrl_reg |= (ILIM_450MA | CHRG_CT_EN);
1529 usb_ctrl_reg |= (ILIM_3000MA | CHRG_CT_EN);
1531 chrg_ctrl_reg1 &= (0x00);
1532 chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
1534 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1535 chrg_ctrl_reg2 &= ~(0xc0);
1536 chrg_ctrl_reg2 |= FINISH_100MA;
1538 sup_sts_reg &= ~(0x01 << 3);
1539 sup_sts_reg |= (0x01 << 2);
1541 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1542 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1543 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1544 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1545 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1547 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1548 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1549 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1550 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1552 DBG("%s end\n", __func__);
1555 void charge_disable_open_otg(int value)
1557 struct battery_info *di = g_battery;
1560 DBG("charge disable, enable OTG.\n");
1561 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1562 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1565 DBG("charge enable, disable OTG.\n");
1566 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1567 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1571 static void low_waring_init(struct battery_info *di)
1576 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1578 /* 2.8v~3.5v, interrupt */
1579 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1580 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1583 static void fg_init(struct battery_info *di)
1587 adc_ctrl_val = 0x30;
1588 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1591 /* get the volatege offset */
1592 _get_voltage_offset_value(di);
1593 rk_battery_charger_init(di);
1594 _set_relax_thres(di);
1595 /* get the current offset , the value write to the CAL_OFFSET */
1596 di->current_offset = _get_ioffset(di);
1597 _set_cal_offset(di, di->current_offset+42);
1599 _capacity_init(di, di->nac);
1601 di->remain_capacity = _get_realtime_capacity(di);
1602 di->current_avg = _get_average_current(di);
1604 low_waring_init(di);
1606 power_on_save(di, di->voltage_ocv);
1607 /* set sample time for cal_offset interval*/
1608 ioffset_sample_time(di, SAMP_TIME_8MIN);
1609 dump_gauge_register(di);
1610 dump_charger_register(di);
1613 "nac = %d , remain_capacity = %d\n"
1614 "OCV_voltage = %d, voltage = %d\n"
1615 "SOC = %d, fcc = %d\n",
1617 di->nac, di->remain_capacity,
1618 di->voltage_ocv, di->voltage,
1619 di->real_soc, di->fcc);
1623 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1624 static void zero_get_soc(struct battery_info *di)
1626 int ocv_voltage, check_voltage;
1627 int temp_soc = -1, real_soc;
1628 int currentold, currentnow, voltage;
1633 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1634 /* if (di->voltage < 3600)//di->warnning_voltage) */
1636 /* DBG("+++++++zero mode++++++++displaysoc+++++++++\n"); */
1638 currentold = _get_average_current(di);
1639 _get_cal_offset(di);
1642 currentnow = _get_average_current(di);
1644 } while ((currentold == currentnow) && (count_num < 11));
1647 for (i = 0; i < 10 ; i++)
1648 voltage += rk_battery_voltage(di);
1651 if (di->voltage_old == 0)
1652 di->voltage_old = voltage;
1653 voltage_k = voltage;
1654 voltage = (di->voltage_old*2 + 8*voltage)/10;
1655 di->voltage_old = voltage;
1656 /* DBG("Zero: voltage = %d\n", voltage); */
1658 currentnow = _get_average_current(di);
1659 /* DBG(" zero: current = %d, voltage = %d\n", currentnow, voltage); */
1661 ocv_voltage = 3400 + abs32_int(currentnow)*200/1000;
1662 check_voltage = voltage + abs32_int(currentnow)*(200 - 65)/1000; /* 65 mo power-path mos */
1663 _voltage_to_capacity(di, check_voltage);
1664 /* if ((di->remain_capacity > di->nac) && (update_q == 0)) */
1665 /* DBG(" xxx Zerro: tui suan OCV cap :%d\n", di->temp_nac); */
1666 di->update_q = di->remain_capacity - di->temp_nac;
1667 /* update_q = di->temp_nac; */
1669 /* DBG("Zero: update_q = %d , remain_capacity = %d, temp_nac = %d\n ", di->update_q, di->remain_capacity, di->temp_nac); */
1670 /* relax_volt_update_remain_capacity(di, 3600 + abs32_int(di->current_avg)*200/1000); */
1672 _voltage_to_capacity(di, ocv_voltage);
1674 temp_soc = _get_soc(di); */
1675 if (di->display_soc == 0)
1676 di->display_soc = di->real_soc*1000;
1678 real_soc = di->display_soc;
1679 /* DBG(" Zerro: Q (err) cap :%d\n", di->temp_nac);
1680 DBG(" ZERO : real-soc = %d\n ", di->real_soc); */
1681 DBG("ZERO : ocv_voltage = %d, check_voltage = %d\n ", ocv_voltage, check_voltage);
1682 if (di->remain_capacity > di->temp_nac + di->update_q) {
1684 if (di->update_k == 0 || di->update_k >= 10) {
1685 /* DBG("one..\n"); */
1686 if (di->update_k == 0) {
1687 di->line_q = di->temp_nac + di->update_q; /* ZQ = Q_ded + Qerr */
1688 /* line_q = update_q - di->temp_nac; */
1689 temp_soc = (di->remain_capacity - di->line_q)*1000/di->fcc;/* (RM - ZQ) / FCC = r0 = R0 ; */
1690 /* temp_soc = (line_q)*1000/di->fcc;//(RM - ZQ) / FCC = r0 = R0 ;*
1691 /di->line_k = (real_soc*1000 + temp_soc/2)/temp_soc;//k0 = y0/x0 */
1692 di->line_k = (real_soc + temp_soc/2)/temp_soc;/* k0 = y0/x0 */
1693 /* DBG("Zero: one link = %d realsoc = %d , temp_soc = %d\n", di->line_k, di->real_soc, temp_soc); */
1699 line_q = di->temp_nac + update_q;
1701 /* DBG("two...\n"); */
1702 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* x1 10 */
1704 temp_soc = (line_q)*1000/di->fcc;// x1
1705 real_soc = (di->line_k*temp_soc+500)/1000; //y1 = k0*x1
1707 real_soc = (di->line_k*temp_soc); /* y1 = k0*x1 */
1708 /* DBG("Zero: two link = %d realsoc = %d , temp_soc = %d\n", di->line_k, real_soc, temp_soc); */
1709 di->display_soc = real_soc;
1710 /* if (real_soc != di->real_soc) */
1711 if ((real_soc+500)/1000 < di->real_soc)
1714 DBG("Zero two di->real_soc = %d\n", di->real_soc);
1715 DBG("Zero : temp_soc : %d\n", real_soc);
1717 _voltage_to_capacity(di, ocv_voltage);
1718 di->line_q = di->temp_nac + di->update_q; /* Q1 */
1719 /* line_q = update_q - di->temp_nac; */
1720 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* z1 */
1722 temp_soc = (line_q)*1000/di->fcc;
1723 di->line_k = (di->real_soc*1000 + temp_soc/2)/temp_soc; //k1 = y1/z1
1725 di->line_k = (di->display_soc + temp_soc/2)/temp_soc; /* k1 = y1/z1 */
1726 /* DBG("Zero: two link = %d display_soc = %d , temp_soc = %d\n", di->line_k, di->display_soc, temp_soc); */
1727 /* line_q = di->temp_nac + update_q;// Q1 */
1735 /* DBG("di->remain_capacity = %d, line_q = %d\n ", di->remain_capacity, di->line_q); */
1738 if (di->update_k == 1 || di->update_k != 10) {
1739 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc;/* x */
1740 di->display_soc = di->line_k*temp_soc;
1741 /* if (((di->line_k*temp_soc+500)/1000) != di->real_soc), */
1742 DBG("ZERO : display-soc = %d, real-soc = %d\n", di->display_soc, di->real_soc);
1743 if ((di->display_soc+500)/1000 < di->real_soc)
1745 /* di->real_soc = (line_k*temp_soc+500)/1000 ;//y = k0*x */
1748 /* DBG("three..\n"); */
1750 if (di->update_k > 10) {
1756 DBG("ZERO : update_k = %d\n", di->update_k);
1757 DBG("ZERO : remain_capacity = %d , nac = %d, update_q = %d\n", di->remain_capacity, di->line_q, di->update_q);
1758 DBG("ZERO : Warnning_voltage = %d, line_k = %d, temp_soc = %d real_soc = %d\n\n", di->warnning_voltage, di->line_k, temp_soc, di->real_soc);
1763 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1766 int now_current, soc_time = -1;
1769 voltage = di->voltage;
1770 now_current = di->current_avg;
1771 if (now_current == 0)
1773 soc_time = di->fcc*3600/100/(abs_int(now_current));
1774 _voltage_to_capacity(di, 3800);
1775 volt_to_soc = di->temp_soc;
1776 di->temp_soc = _get_soc(di);
1778 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
1779 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1780 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
1784 } else if (di->temp_soc == di->real_soc) {
1785 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1786 } else if (di->temp_soc > di->real_soc) {
1787 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1788 di->vol_smooth_time++;
1789 if (di->vol_smooth_time > soc_time*3) {
1791 di->vol_smooth_time = 0;
1795 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1796 if (di->real_soc == (di->temp_soc + 1)) {
1797 di->change_timer = di->soc_timer;
1798 di->real_soc = di->temp_soc;
1800 di->vol_smooth_time++;
1801 if (di->vol_smooth_time > soc_time/3) {
1803 di->vol_smooth_time = 0;
1808 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1809 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
1812 static int get_charging_time(struct battery_info *di)
1814 return (di->charging_time/60);
1817 static int get_discharging_time(struct battery_info *di)
1819 return (di->discharging_time/60);
1821 static void dump_debug_info(struct battery_info *di)
1823 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
1824 u8 usb_ctrl_reg, chrg_ctrl_reg1;
1825 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
1827 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
1828 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
1829 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
1830 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
1831 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1832 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1833 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1834 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1835 battery_read(di->rk818, 0x00, &rtc_val, 1);
1837 DBG("\n------------- dump_debug_regs -----------------\n"
1838 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
1839 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
1840 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
1841 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
1842 ggcon_reg, ggsts_reg, rtc_val,
1843 sup_tst_reg, vb_mod_reg,
1844 usb_ctrl_reg, chrg_ctrl_reg1,
1845 chrg_ctrl_reg2, chrg_ctrl_reg3
1849 "########################## [read] ################################\n"
1850 "info: 3.4v low warning, digital 100mA finish, 4.2v, 1.6A\n"
1851 "-----------------------------------------------------------------\n"
1852 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
1853 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
1854 "diplay_soc = %d, cpapacity_soc = %d\n"
1855 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
1856 "finish_real_soc = %d, finish_temp_soc = %d\n"
1857 "chrg_time = %d, dischrg_time = %d\n",
1858 get_relax_voltage(di),
1859 di->voltage, di->current_avg,
1860 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
1861 di->real_soc, _get_soc(di),
1862 di->ac_online, di->usb_online, di->status,
1863 di->debug_finish_real_soc, di->debug_finish_temp_soc,
1864 get_charging_time(di), get_discharging_time(di)
1866 get_charge_status(di);
1867 DBG("################################################################\n");
1871 static void update_fcc_capacity(struct battery_info *di)
1873 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
1874 if (get_level(di) >= di->dod0_level) {
1875 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
1876 if (di->fcc > di->qmax)
1879 _capacity_init(di, di->fcc);
1880 _save_FCC_capacity(di, di->fcc);
1882 di->dod0_status = 0;
1886 static void debug_get_finish_soc(struct battery_info *di)
1888 if (di->charge_status == CHARGE_FINISH) {
1889 di->debug_finish_real_soc = di->real_soc;
1890 di->debug_finish_temp_soc = di->temp_soc;
1894 static void wait_charge_finish_signal(struct battery_info *di)
1896 if (di->charge_status == CHARGE_FINISH)
1897 update_fcc_capacity(di);/* save new fcc*/
1900 debug_get_finish_soc(di);
1903 static void charge_finish_routine(struct battery_info *di)
1905 if (di->charge_status == CHARGE_FINISH) {
1906 _capacity_init(di, di->fcc);
1907 zero_current_calibration(di);
1909 if (di->real_soc < 100) {
1910 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
1911 if ((di->soc_counter < 80)) {
1914 di->soc_counter = 0;
1921 static void voltage_to_soc_charge_smooth(struct battery_info *di)
1923 int now_current, soc_time;
1925 now_current = _get_average_current(di);
1926 if (now_current == 0)
1928 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
1929 di->temp_soc = _get_soc(di);
1931 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1933 if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
1934 di->charge_smooth_time++;
1936 if (di->charge_smooth_time > soc_time/3) {
1938 di->charge_smooth_time = 0;
1940 di->charge_smooth_status = true;
1943 if (di->real_soc == di->temp_soc) {
1944 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1945 di->temp_soc = _get_soc(di);
1947 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
1949 if (di->temp_soc < di->real_soc + 1) {
1950 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1951 di->charge_smooth_time++;
1952 if (di->charge_smooth_time > soc_time*2) {
1954 di->charge_smooth_time = 0;
1956 di->charge_smooth_status = true;
1959 else if (di->temp_soc > di->real_soc + 1) {
1960 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1961 di->charge_smooth_time++;
1962 if (di->charge_smooth_time > soc_time/3) {
1964 di->charge_smooth_time = 0;
1966 di->charge_smooth_status = true;
1968 } else if (di->temp_soc == di->real_soc + 1) {
1969 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
1970 if (di->charge_smooth_status) {
1971 di->charge_smooth_time++;
1972 if (di->charge_smooth_time > soc_time/3) {
1973 di->real_soc = di->temp_soc;
1974 di->charge_smooth_time = 0;
1975 di->charge_smooth_status = false;
1979 di->real_soc = di->temp_soc;
1980 di->charge_smooth_status = false;
1986 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1987 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
1990 static void rk_battery_display_smooth(struct battery_info *di)
1995 status = di->status;
1996 charge_status = di->charge_status;
1997 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
1999 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2000 voltage_to_soc_discharge_smooth(di);
2002 voltage_to_soc_charge_smooth(di);
2004 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2005 voltage_to_soc_discharge_smooth(di);
2006 if (di->real_soc == 1) {
2008 if (di->time2empty >= 300)
2018 static void software_recharge(struct battery_info *di, int max_cnt)
2020 static int recharge_cnt;
2023 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2024 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2025 chrg_ctrl_reg1 &= ~(1 << 7);
2026 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2027 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2028 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2030 chrg_ctrl_reg1 |= (1 << 7);
2031 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2032 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2033 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2041 static int estimate_battery_resister(struct battery_info *di)
2044 int avr_voltage1 = 0, avr_current1;
2045 int avr_voltage2 = 0, avr_current2;
2047 int bat_res, ocv_votage;
2048 static unsigned long last_time;
2049 unsigned long delta_time;
2050 int charge_ocv_voltage1, charge_ocv_voltage2;
2051 int charge_ocv_soc1, charge_ocv_soc2;
2053 delta_time = get_seconds() - last_time;
2054 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2055 if (delta_time >= 20) {/*20s*/
2058 set_charge_current(di, ILIM_450MA);/*450mA*/
2060 for (i = 0; i < 10 ; i++) {
2062 avr_voltage1 += rk_battery_voltage(di);
2065 avr_current1 = _get_average_current(di);
2066 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2067 DBG("------------------------------------------------------------------------------------------\n");
2068 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2071 set_charge_current(di, ILIM_3000MA);
2073 for (i = 0; i < 10 ; i++) {
2075 avr_voltage2 += rk_battery_voltage(di);
2078 avr_current2 = _get_average_current(di);
2079 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2080 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2082 /*calc resister and ocv_votage ocv*/
2083 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2084 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2085 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2087 /*calc sample voltage ocv*/
2088 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2089 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2090 _voltage_to_capacity(di, charge_ocv_voltage1);
2091 charge_ocv_soc1 = di->temp_soc;
2092 _voltage_to_capacity(di, charge_ocv_voltage2);
2093 charge_ocv_soc2 = di->temp_soc;
2095 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2096 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2097 DBG("------------------------------------------------------------------------------------------\n");
2098 last_time = get_seconds();
2108 static int update_battery_resister(struct battery_info *di)
2112 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2114 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2115 tmp_res = estimate_battery_resister(di);
2117 di->bat_res_update_cnt++;
2118 di->bat_res += tmp_res;
2119 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2120 if (di->bat_res_update_cnt == 10) {
2121 di->bat_res_updated = true;
2124 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2133 static void charge_soc_check_routine(struct battery_info *di)
2141 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2142 min = get_charging_time(di);
2143 update_battery_resister(di);
2145 if ((min >= 30) && (di->bat_res_updated)) {
2147 old_temp_soc = di->temp_soc;
2148 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2149 _voltage_to_capacity(di, ocv_voltage);
2150 ocv_temp_soc = di->temp_soc;
2152 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2153 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2154 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2156 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2157 di->temp_soc = ocv_temp_soc;
2159 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2161 remain_capcity = di->temp_soc * di->fcc / 100;
2162 _capacity_init(di, remain_capcity);
2163 di->remain_capacity = _get_realtime_capacity(di);
2164 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2172 static void update_resume_status_relax_voltage(struct battery_info *di)
2174 unsigned long sleep_soc;
2175 unsigned long sum_sleep_soc;
2176 unsigned long sleep_sec;
2181 int sum_sleep_avr_current;
2185 update_battery_info(di);
2187 di->sys_wakeup = true;
2189 DBG("<%s>, resume----------checkstart\n", __func__);
2190 sleep_sec = get_seconds() - di->suspend_time_start;
2191 sleep_min = sleep_sec / 60;
2193 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2194 __func__, sleep_sec, sleep_min);
2196 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2197 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2199 delta_capacity = di->suspend_capacity - di->remain_capacity;
2200 delta_soc = di->suspend_temp_soc - _get_soc(di);
2201 di->dischrg_sum_sleep_capacity += delta_capacity;
2202 di->dischrg_sum_sleep_sec += sleep_sec;
2204 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2205 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2207 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2208 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2209 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2210 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2211 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2212 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2213 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2215 /*large suspend current*/
2216 if (sum_sleep_avr_current > 20) {
2217 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2218 di->real_soc -= sum_sleep_soc;
2219 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2220 __func__, sum_sleep_soc, di->real_soc);
2222 /* small suspend current*/
2223 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2225 relax_voltage = get_relax_voltage(di);
2226 di->voltage = rk_battery_voltage(di);
2228 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2229 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2230 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2233 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2234 if (sum_sleep_soc > 0)
2235 di->real_soc -= sum_sleep_soc;
2239 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2240 di->dischrg_sum_sleep_capacity = 0;
2241 di->dischrg_sum_sleep_sec = 0;
2243 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2244 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2247 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2248 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2249 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2250 di->temp_soc = _get_soc(di);
2251 charge_status = get_charge_status(di);
2253 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2254 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2255 DBG("resume, sleep : ac online charge current < 1000\n");
2256 if (sleep_sec > 0) {
2257 di->count_sleep_time += sleep_sec;
2258 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2259 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2261 di->count_sleep_time = 0;
2262 di->real_soc += sleep_soc;
2263 if (di->real_soc > 100)
2268 DBG("<%s>, usb charging\n", __func__);
2269 if (di->suspend_temp_soc + 15 < di->temp_soc)
2270 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2272 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2275 DBG("POWER_SUPPLY_STATUS_CHARGING: di->temp_soc = %d, di->real_soc = %d, sleep_time = %ld\n ", di->temp_soc , di->real_soc, sleep_sec);
2282 #ifdef SUPPORT_USB_CHARGE
2283 static int get_charging_status_type(struct battery_info *di)
2285 int otg_status = dwc_otg_check_dpdm();
2287 if (0 == otg_status) {
2290 di->check_count = 0;
2292 } else if (1 == otg_status) {
2293 if (0 == get_gadget_connect_flag()) {
2294 if (++di->check_count >= 5) {
2306 } else if (2 == otg_status) {
2309 di->check_count = 0;
2312 if (di->ac_online == 1)
2313 set_charge_current(di, ILIM_3000MA);
2315 set_charge_current(di, ILIM_450MA);
2321 static void battery_poweron_status_init(struct battery_info *di)
2325 #ifndef SUPPORT_USB_CHARGE
2329 #ifdef SUPPORT_USB_CHARGE
2331 otg_status = dwc_otg_check_dpdm();
2332 if (otg_status == 1) {
2335 set_charge_current(di, ILIM_450MA);
2336 di->status = POWER_SUPPLY_STATUS_CHARGING;
2337 DBG("++++++++ILIM_450MA++++++\n");
2339 } else if (otg_status == 2) {
2342 di->status = POWER_SUPPLY_STATUS_CHARGING;
2343 set_charge_current(di, ILIM_3000MA);
2344 DBG("++++++++ILIM_1000MA++++++\n");
2346 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2350 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2351 if (buf&PLUG_IN_STS) {
2354 di->status = POWER_SUPPLY_STATUS_CHARGING;
2355 if (di->real_soc == 100)
2356 di->status = POWER_SUPPLY_STATUS_FULL;
2358 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2362 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2367 static void check_battery_status(struct battery_info *di)
2372 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2373 #ifdef SUPPORT_USB_CHARGE
2375 if (strstr(saved_command_line, "charger")) {
2376 if ((buf&PLUG_IN_STS) == 0) {
2377 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2383 if (buf&PLUG_IN_STS) {
2384 get_charging_status_type(di);
2386 di->status = POWER_SUPPLY_STATUS_CHARGING;
2387 if (di->real_soc == 100)
2388 di->status = POWER_SUPPLY_STATUS_FULL;
2390 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2397 if (buf & PLUG_IN_STS) {
2400 di->status = POWER_SUPPLY_STATUS_CHARGING;
2401 if (di->real_soc == 100)
2402 di->status = POWER_SUPPLY_STATUS_FULL;
2404 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2411 static void report_power_supply_changed(struct battery_info *di)
2414 static u32 old_ac_status;
2415 static u32 old_usb_status;
2416 static u32 old_charge_status;
2419 state_changed = false;
2420 if (di->real_soc == 0)
2421 state_changed = true;
2422 else if (di->real_soc == 100)
2423 state_changed = true;
2424 else if (di->real_soc != old_soc)
2425 state_changed = true;
2426 else if (di->ac_online != old_ac_status)
2427 state_changed = true;
2428 else if (di->usb_online != old_usb_status)
2429 state_changed = true;
2430 else if (old_charge_status != di->status)
2431 state_changed = true;
2433 if (state_changed) {
2434 power_supply_changed(&di->bat);
2435 power_supply_changed(&di->usb);
2436 power_supply_changed(&di->ac);
2437 old_soc = di->real_soc;
2438 old_ac_status = di->ac_online;
2439 old_usb_status = di->usb_online;
2440 old_charge_status = di->status;
2444 static void update_battery_info(struct battery_info *di)
2446 di->remain_capacity = _get_realtime_capacity(di);
2447 if (di->remain_capacity > di->fcc)
2448 _capacity_init(di, di->fcc);
2450 if (di->real_soc > 100)
2452 else if (di->real_soc < 0)
2455 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2456 di->charging_time++;
2457 di->discharging_time = 0;
2459 di->discharging_time++;
2460 di->charging_time = 0;
2464 di->voltage = rk_battery_voltage(di);
2465 di->current_avg = _get_average_current(di);
2466 di->remain_capacity = _get_realtime_capacity(di);
2467 di->voltage_ocv = _get_OCV_voltage(di);
2468 di->charge_status = get_charge_status(di);
2469 di->otg_status = dwc_otg_check_dpdm();
2470 di->relax_voltage = get_relax_voltage(di);
2471 di->temp_soc = _get_soc(di);
2472 di->remain_capacity = _get_realtime_capacity(di);
2473 check_battery_status(di);/* ac_online, usb_online, status*/
2474 update_cal_offset(di);
2477 static void rk_battery_work(struct work_struct *work)
2479 struct battery_info *di = container_of(work,
2480 struct battery_info, battery_monitor_work.work);
2482 update_resume_status_relax_voltage(di);
2483 wait_charge_finish_signal(di);
2484 charge_finish_routine(di);
2486 rk_battery_display_smooth(di);
2487 update_battery_info(di);
2489 report_power_supply_changed(di);
2490 _copy_soc(di, di->real_soc);
2491 _save_remain_capacity(di, di->remain_capacity);
2493 dump_debug_info(di);
2494 di->queue_work_cnt++;
2495 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2498 static void rk_battery_charge_check_work(struct work_struct *work)
2500 struct battery_info *di = container_of(work,
2501 struct battery_info, charge_check_work.work);
2503 DBG("rk_battery_charge_check_work\n");
2504 charge_disable_open_otg(di->charge_otg);
2507 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2509 int register_battery_notifier(struct notifier_block *nb)
2511 return blocking_notifier_chain_register(&battery_chain_head, nb);
2513 EXPORT_SYMBOL_GPL(register_battery_notifier);
2515 int unregister_battery_notifier(struct notifier_block *nb)
2517 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2519 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2521 int battery_notifier_call_chain(unsigned long val)
2523 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2524 == NOTIFY_BAD) ? -EINVAL : 0;
2526 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2528 static void poweron_lowerpoer_handle(struct battery_info *di)
2530 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2531 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2533 /* kernel_power_off(); */
2538 static int battery_notifier_call(struct notifier_block *nb,
2539 unsigned long event, void *data)
2541 struct battery_info *di =
2542 container_of(nb, struct battery_info, battery_nb);
2546 DBG(" CHARGE enable\n");
2548 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2553 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2554 DBG("charge disable OTG enable\n");
2558 poweron_lowerpoer_handle(di);
2567 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2569 pr_info("<%s>lower power warning!\n", __func__);
2571 _copy_soc(g_battery, 0);
2572 _capacity_init(g_battery, 0);
2573 rk_send_wakeup_key();
2578 static void disable_vbat_low_irq(struct battery_info *di)
2581 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2582 /*clr vbat low interrupt */
2583 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2585 static void enable_vbat_low_irq(struct battery_info *di)
2587 /* clr vbat low interrupt */
2588 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2590 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2593 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2595 pr_info("\n------- %s:irq = %d\n", __func__, irq);
2596 rk_send_wakeup_key();
2599 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
2601 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2602 charge_disable_open_otg(0);
2603 rk_send_wakeup_key();
2607 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
2609 pr_info("---------- %s:irq = %d\n", __func__, irq);
2610 rk_send_wakeup_key();
2616 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2620 struct kobject *rk818_fg_kobj;
2622 ret = create_sysfs_interfaces(dev);
2625 dev_err(dev, "device RK818 battery sysfs register failed\n");
2629 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2632 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2633 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2635 dev_err(dev, "create rk818_battery node error\n");
2643 power_supply_unregister(&di->ac);
2644 power_supply_unregister(&di->usb);
2645 power_supply_unregister(&di->bat);
2650 static void rk818_battery_irq_init(struct battery_info *di)
2652 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2654 struct rk818 *chip = di->rk818;
2656 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
2657 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
2658 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
2659 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
2661 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2662 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2664 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2666 di->irq = vb_lo_irq;
2667 enable_irq_wake(di->irq);
2668 disable_vbat_low_irq(di);
2670 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2671 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2673 dev_err(chip->dev, "plug_in_irq request failed!\n");
2676 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2677 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2679 dev_err(chip->dev, "plug_out_irq request failed!\n");
2682 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2683 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2685 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2688 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2694 di->platform_data = chip->battery_data;
2695 di->cell.config = di->platform_data->cell_cfg;
2696 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
2697 di->qmax = di->platform_data->cell_cfg->design_qmax;
2698 di->fcc = di->design_capacity;
2699 di->vol_smooth_time = 0;
2700 di->charge_smooth_time = 0;
2701 di->charge_smooth_status = false;
2702 di->sleep_status = 0;
2704 di->sys_wakeup = true;
2705 di->pcb_ioffset = 0;
2706 di->pcb_ioffset_updated = false;
2707 di->queue_work_cnt = 0;
2710 di->voltage_old = 0;
2711 di->display_soc = 0;
2713 di->bat_res_updated = false;
2715 di->sys_wakeup = true;
2716 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2718 di->debug_finish_real_soc = 0;
2719 di->debug_finish_temp_soc = 0;
2721 fcc_capacity = _get_FCC_capacity(di);
2722 if (fcc_capacity > 1000)
2723 di->fcc = fcc_capacity;
2725 di->fcc = di->design_capacity;
2728 static struct of_device_id rk818_battery_of_match[] = {
2729 { .compatible = "rk818_battery" },
2733 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2736 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2738 struct device_node *regs, *rk818_pmic_np;
2739 struct battery_platform_data *data;
2740 struct cell_config *cell_cfg;
2741 struct ocv_config *ocv_cfg;
2742 struct property *prop;
2746 rk818_pmic_np = of_node_get(rk818->dev->of_node);
2747 if (!rk818_pmic_np) {
2748 dev_err(dev, "could not find pmic sub-node\n");
2752 regs = of_find_node_by_name(rk818_pmic_np, "battery");
2754 dev_err(dev, "battery node not found!\n");
2758 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2760 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2764 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2766 dev_err(dev, "kzalloc for cell_config failed!\n");
2769 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2771 dev_err(dev, "kzalloc for ocv_config failed!\n");
2775 prop = of_find_property(regs, "ocv_table", &length);
2777 dev_err(dev, "ocv_table not found!\n");
2780 data->ocv_size = length / sizeof(u32);
2782 if (data->ocv_size > 0) {
2783 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2785 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
2786 if (!data->battery_ocv) {
2787 dev_err(dev, "kzalloc for ocv_table failed!\n");
2790 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2795 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2797 dev_err(dev, "max_charge_currentmA not found!\n");
2800 data->max_charger_currentmA = out_value;
2802 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
2804 dev_err(dev, "max_charge_voltagemV not found!\n");
2807 data->max_charger_voltagemV = out_value;
2809 ret = of_property_read_u32(regs, "design_capacity", &out_value);
2811 dev_err(dev, "design_capacity not found!\n");
2814 cell_cfg->design_capacity = out_value;
2816 ret = of_property_read_u32(regs, "design_qmax", &out_value);
2818 dev_err(dev, "design_qmax not found!\n");
2821 cell_cfg->design_qmax = out_value;
2823 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
2825 dev_err(dev, "sleep_enter_current not found!\n");
2828 ocv_cfg->sleep_enter_current = out_value;
2830 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
2832 dev_err(dev, "sleep_exit_current not found!\n");
2835 ocv_cfg->sleep_exit_current = out_value;
2837 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
2839 cell_cfg->ocv = ocv_cfg;
2840 data->cell_cfg = cell_cfg;
2841 rk818->battery_data = data;
2843 DBG("\n--------- the battery OCV TABLE dump:\n");
2844 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
2845 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
2846 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
2847 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
2848 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
2849 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
2850 DBG("uboot chrg = %d\n", support_uboot_chrg);
2851 DBG("\n--------- rk818_battery dt_parse ok.\n");
2856 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2863 static int battery_probe(struct platform_device *pdev)
2865 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
2866 struct battery_info *di;
2869 DBG("battery driver version %s\n", DRIVER_VERSION);
2870 di = kzalloc(sizeof(*di), GFP_KERNEL);
2872 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
2875 ret = rk_battery_parse_dt(chip, &pdev->dev);
2877 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
2881 platform_set_drvdata(pdev, di);
2882 battery_info_init(di, chip);
2883 if (!is_bat_exist(di)) {
2884 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
2889 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
2891 flatzone_voltage_init(di);
2892 battery_poweron_status_init(di);
2893 battery_power_supply_init(di);
2894 ret = battery_power_supply_register(di, &pdev->dev);
2896 dev_err(&pdev->dev, "rk power supply register failed!\n");
2899 di->wq = create_singlethread_workqueue("battery-work");
2900 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
2901 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2902 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
2904 di->battery_nb.notifier_call = battery_notifier_call;
2905 register_battery_notifier(&di->battery_nb);
2907 rk818_battery_irq_init(di);
2908 rk818_battery_sysfs_init(di, &pdev->dev);
2909 DBG("------ RK81x battery_probe ok!-------\n");
2916 static int battery_suspend(struct platform_device *dev, pm_message_t state)
2918 struct battery_info *di = platform_get_drvdata(dev);
2920 enable_vbat_low_irq(di);
2921 di->sleep_status = di->status;
2922 di->suspend_charge_current = _get_average_current(di);
2924 /* avoid abrupt wakeup which will clean the variable*/
2925 if (di->sys_wakeup) {
2926 di->suspend_capacity = di->remain_capacity;
2927 di->suspend_temp_soc = _get_soc(di);
2928 di->suspend_time_start = get_seconds();
2929 di->sys_wakeup = false;
2932 cancel_delayed_work(&di->battery_monitor_work);
2933 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
2934 __func__, di->suspend_temp_soc, di->suspend_charge_current,
2935 di->suspend_capacity, di->sleep_status);
2940 static int battery_resume(struct platform_device *dev)
2942 struct battery_info *di = platform_get_drvdata(dev);
2945 DBG("<%s>\n", __func__);
2946 disable_vbat_low_irq(di);
2947 queue_delayed_work(di->wq, &di->battery_monitor_work,
2948 msecs_to_jiffies(TIMER_MS_COUNTS/2));
2950 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
2952 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
2956 static int battery_remove(struct platform_device *dev)
2958 struct battery_info *di = platform_get_drvdata(dev);
2960 cancel_delayed_work_sync(&di->battery_monitor_work);
2963 static void battery_shutdown(struct platform_device *dev)
2965 struct battery_info *di = platform_get_drvdata(dev);
2967 cancel_delayed_work_sync(&di->battery_monitor_work);
2968 DBG("rk818 shutdown!");
2972 static struct platform_driver battery_driver = {
2974 .name = "rk818-battery",
2975 .owner = THIS_MODULE,
2978 .probe = battery_probe,
2979 .remove = battery_remove,
2980 .suspend = battery_suspend,
2981 .resume = battery_resume,
2982 .shutdown = battery_shutdown,
2985 static int __init battery_init(void)
2987 return platform_driver_register(&battery_driver);
2990 fs_initcall_sync(battery_init);
2991 static void __exit battery_exit(void)
2993 platform_driver_unregister(&battery_driver);
2995 module_exit(battery_exit);
2997 MODULE_LICENSE("GPL");
2998 MODULE_ALIAS("platform:rk818-battery");
2999 MODULE_AUTHOR("ROCKCHIP");