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_first_poweron(struct battery_info *di)
878 battery_read(di->rk818, GGSTS, &buf, 1);
879 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
880 /*di->pwron_bat_con = buf;*/
884 battery_write(di->rk818, GGSTS, &buf, 1);
885 battery_read(di->rk818, GGSTS, &temp, 1);
886 } while (temp&BAT_CON);
891 static void flatzone_voltage_init(struct battery_info *di)
898 ocv_table = di->platform_data->battery_ocv;
899 ocv_size = di->platform_data->ocv_size;
901 for (j = 0; j < 21; j++)
905 for (i = 1; i < ocv_size-1; i++) {
906 if (ocv_table[i+1] < ocv_table[i] + 20)
910 temp_table[j] = temp_table[j-1]+1;
912 di->enter_flatzone = ocv_table[i];
916 for (i = 0; i <= 20; i++) {
917 if (temp_table[i] < temp_table[i+1])
922 di->exit_flatzone = ocv_table[i];
924 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
929 static int is_not_flatzone(struct battery_info *di, int voltage)
931 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
932 DBG("<%s>. is in flat zone\n", __func__);
935 DBG("<%s>. is not in flat zone\n", __func__);
940 static void power_on_save(struct battery_info *di, int voltage)
945 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
947 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
948 _voltage_to_capacity(di, voltage);
949 if (di->temp_soc < 20) {
950 di->dod0_voltage = voltage;
951 di->dod0_capacity = di->nac;
953 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
956 if (di->temp_soc <= 0)
957 di->dod0_level = 100;
958 else if (di->temp_soc < 5)
960 else if (di->temp_soc < 10)
962 /* save_soc = di->dod0_level; */
963 save_soc = get_level(di);
964 if (save_soc < di->dod0_level)
965 save_soc = di->dod0_level;
966 save_level(di, save_soc);
967 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
974 static int _get_soc(struct battery_info *di)
976 return di->remain_capacity * 100 / di->fcc;
979 static enum power_supply_property rk_battery_props[] = {
981 POWER_SUPPLY_PROP_STATUS,
982 POWER_SUPPLY_PROP_CURRENT_NOW,
983 POWER_SUPPLY_PROP_VOLTAGE_NOW,
984 POWER_SUPPLY_PROP_PRESENT,
985 POWER_SUPPLY_PROP_HEALTH,
986 POWER_SUPPLY_PROP_CAPACITY,
989 #define to_device_info(x) container_of((x), \
990 struct battery_info, bat)
992 static int rk_battery_get_property(struct power_supply *psy,
993 enum power_supply_property psp,
994 union power_supply_propval *val)
996 struct battery_info *di = to_device_info(psy);
999 case POWER_SUPPLY_PROP_CURRENT_NOW:
1000 val->intval = di->current_avg*1000;/*uA*/
1003 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1004 val->intval = di->voltage*1000;/*uV*/
1007 case POWER_SUPPLY_PROP_PRESENT:
1008 val->intval = val->intval <= 0 ? 0 : 1;
1011 case POWER_SUPPLY_PROP_CAPACITY:
1012 val->intval = di->real_soc;
1015 case POWER_SUPPLY_PROP_HEALTH:
1016 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1019 case POWER_SUPPLY_PROP_STATUS:
1020 val->intval = di->status;
1031 static enum power_supply_property rk_battery_ac_props[] = {
1032 POWER_SUPPLY_PROP_ONLINE,
1034 static enum power_supply_property rk_battery_usb_props[] = {
1035 POWER_SUPPLY_PROP_ONLINE,
1039 #define to_ac_device_info(x) container_of((x), \
1040 struct battery_info, ac)
1042 static int rk_battery_ac_get_property(struct power_supply *psy,
1043 enum power_supply_property psp,
1044 union power_supply_propval *val)
1047 struct battery_info *di = to_ac_device_info(psy);
1050 case POWER_SUPPLY_PROP_ONLINE:
1051 val->intval = di->ac_online; /*discharging*/
1061 #define to_usb_device_info(x) container_of((x), \
1062 struct battery_info, usb)
1064 static int rk_battery_usb_get_property(struct power_supply *psy,
1065 enum power_supply_property psp,
1066 union power_supply_propval *val)
1069 struct battery_info *di = to_usb_device_info(psy);
1072 case POWER_SUPPLY_PROP_ONLINE:
1073 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1076 val->intval = di->usb_online;
1088 static void battery_power_supply_init(struct battery_info *di)
1090 di->bat.name = "BATTERY";
1091 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1092 di->bat.properties = rk_battery_props;
1093 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1094 di->bat.get_property = rk_battery_get_property;
1097 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1098 di->ac.properties = rk_battery_ac_props;
1099 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1100 di->ac.get_property = rk_battery_ac_get_property;
1102 di->usb.name = "USB";
1103 di->usb.type = POWER_SUPPLY_TYPE_USB;
1104 di->usb.properties = rk_battery_usb_props;
1105 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1106 di->usb.get_property = rk_battery_usb_get_property;
1109 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1113 ret = power_supply_register(dev, &di->bat);
1115 dev_err(dev, "failed to register main battery\n");
1118 ret = power_supply_register(dev, &di->usb);
1120 dev_err(dev, "failed to register usb power supply\n");
1123 ret = power_supply_register(dev, &di->ac);
1125 dev_err(dev, "failed to register ac power supply\n");
1132 power_supply_unregister(&di->ac);
1134 power_supply_unregister(&di->usb);
1136 power_supply_unregister(&di->bat);
1141 static void _capacity_init(struct battery_info *di, u32 capacity)
1148 di->voltage_old = 0;
1149 di->display_soc = 0;
1151 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1153 buf = (capacity_ma>>24)&0xff;
1154 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1155 buf = (capacity_ma>>16)&0xff;
1156 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1157 buf = (capacity_ma>>8)&0xff;
1158 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1159 buf = (capacity_ma&0xff) | 0x01;
1160 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1161 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1167 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1172 if (capacity >= di->qmax)
1173 capacity = di->qmax;
1175 capacity_ma = capacity;
1177 buf = (capacity_ma>>24)&0xff;
1178 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1179 buf = (capacity_ma>>16)&0xff;
1180 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1181 buf = (capacity_ma>>8)&0xff;
1182 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1183 buf = (capacity_ma&0xff) | 0x01;
1184 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1187 static int _get_remain_capacity(struct battery_info *di)
1194 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1196 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1198 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1200 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1203 capacity = temp;/* /4096*900/14/36*500/521; */
1209 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1214 capacity_ma = capacity;
1215 buf = (capacity_ma>>24)&0xff;
1216 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1217 buf = (capacity_ma>>16)&0xff;
1218 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1219 buf = (capacity_ma>>8)&0xff;
1220 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1221 buf = (capacity_ma&0xff) | 0x01;
1222 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1225 static int _get_FCC_capacity(struct battery_info *di)
1232 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1234 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1236 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1238 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1242 capacity = temp-1;/* 4096*900/14/36*500/521 */
1245 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1250 static int _get_realtime_capacity(struct battery_info *di)
1257 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1259 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1261 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1263 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1266 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1271 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1273 int remain_capacity;
1281 now_temp_soc = _get_soc(di);
1282 _voltage_to_capacity(di, relax_voltage);
1283 relax_soc = di->temp_soc;
1284 relax_capacity = di->temp_nac;
1285 abs_soc = abs32_int(relax_soc - now_temp_soc);
1287 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1288 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1291 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1292 remain_capacity = relax_capacity;
1293 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1296 remain_capacity = _get_realtime_capacity(di);
1297 else if (abs_soc <= 10)
1298 remain_capacity = relax_capacity;
1299 else if (abs_soc <= 20)
1300 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1302 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1304 _capacity_init(di, remain_capacity);
1305 di->temp_soc = _get_soc(di);
1306 di->remain_capacity = _get_realtime_capacity(di);
1309 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1310 if (relax_soc < now_temp_soc) {
1311 if (di->suspend_temp_soc - relax_soc <= 5)
1312 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1313 else if (di->suspend_temp_soc - relax_soc <= 10)
1314 di->real_soc = di->real_soc - 5;
1316 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1318 now_current = _get_average_current(di);
1319 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1320 min = soc_time / 60;
1321 if (sleep_min > min)
1325 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);
1329 static int _copy_soc(struct battery_info *di, u8 save_soc)
1334 battery_write(di->rk818, SOC_REG, &soc, 1);
1338 static bool support_uboot_charge(void)
1340 return support_uboot_chrg?true:false;
1343 static int _rsoc_init(struct battery_info *di)
1347 u32 remain_capacity;
1350 #ifdef SUPPORT_USB_CHARGE
1355 di->voltage = rk_battery_voltage(di);
1356 di->voltage_ocv = _get_OCV_voltage(di);
1357 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1359 if (_is_first_poweron(di)) {
1360 _save_FCC_capacity(di, di->design_capacity);
1361 di->fcc = _get_FCC_capacity(di);
1363 _voltage_to_capacity(di, di->voltage_ocv);
1364 di->real_soc = di->temp_soc;
1365 di->nac = di->temp_nac;
1366 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1369 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1370 init_soc = pwron_soc;
1371 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1373 #ifdef SUPPORT_USB_CHARGE
1374 otg_status = dwc_otg_check_dpdm();
1375 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1377 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1380 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1381 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1383 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1386 remain_capacity = _get_remain_capacity(di);
1388 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1389 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1390 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1391 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1393 if (!support_uboot_charge()) {
1394 _voltage_to_capacity(di, di->voltage_ocv);
1395 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1397 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1398 if (((otg_status != 0) && (curr_shtd_time > 0) && (last_shtd_time != curr_shtd_time)) || ((curr_shtd_time > 0) && (otg_status == 0))) {
1400 if (curr_shtd_time > 30) {
1401 remain_capacity = di->temp_nac;
1402 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1404 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - di->real_soc) >= 10)) {
1405 if (remain_capacity >= di->temp_nac*120/100)
1406 remain_capacity = di->temp_nac*110/100;
1407 else if (remain_capacity < di->temp_nac*8/10)
1408 remain_capacity = di->temp_nac*9/10;
1410 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1415 di->real_soc = init_soc;
1416 di->nac = remain_capacity;
1419 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1425 static u8 get_charge_status(struct battery_info *di)
1430 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1435 DBG(" CHARGE-OFF ...\n");
1440 DBG(" DEAD CHARGE ...\n");
1443 case TRICKLE_CHARGE: /* (0x02 << 4) */
1445 DBG(" TRICKLE CHARGE ...\n ");
1448 case CC_OR_CV: /* (0x03 << 4) */
1450 DBG(" CC or CV ...\n");
1453 case CHARGE_FINISH: /* (0x04 << 4) */
1454 ret = CHARGE_FINISH;
1455 DBG(" CHARGE FINISH ...\n");
1458 case USB_OVER_VOL: /* (0x05 << 4) */
1460 DBG(" USB OVER VOL ...\n");
1463 case BAT_TMP_ERR: /* (0x06 << 4) */
1465 DBG(" BAT TMP ERROR ...\n");
1468 case TIMER_ERR: /* (0x07 << 4) */
1470 DBG(" TIMER ERROR ...\n");
1473 case USB_EXIST: /* (1 << 1)// usb is exists */
1475 DBG(" USB EXIST ...\n");
1478 case USB_EFF: /* (1 << 0)// usb is effective */
1480 DBG(" USB EFF...\n");
1490 static void set_charge_current(struct battery_info *di, int charge_current)
1494 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1495 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1496 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1497 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1500 static void rk_battery_charger_init(struct battery_info *di)
1502 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1505 DBG("%s start\n", __func__);
1506 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1507 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1508 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1509 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1510 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1512 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1513 usb_ctrl_reg &= (~0x0f);
1514 #ifdef SUPPORT_USB_CHARGE
1515 usb_ctrl_reg |= (ILIM_450MA | CHRG_CT_EN);
1517 usb_ctrl_reg |= (ILIM_3000MA | CHRG_CT_EN);
1519 chrg_ctrl_reg1 &= (0x00);
1520 chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
1522 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1523 chrg_ctrl_reg2 &= ~(0xc0);
1524 chrg_ctrl_reg2 |= FINISH_100MA;
1526 sup_sts_reg &= ~(0x01 << 3);
1527 sup_sts_reg |= (0x01 << 2);
1529 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1530 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1531 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1532 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1533 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1535 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1536 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1537 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1538 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1540 DBG("%s end\n", __func__);
1543 void charge_disable_open_otg(int value)
1545 struct battery_info *di = g_battery;
1548 DBG("charge disable, enable OTG.\n");
1549 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1550 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1553 DBG("charge enable, disable OTG.\n");
1554 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1555 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1559 static void low_waring_init(struct battery_info *di)
1564 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1566 /* 2.8v~3.5v, interrupt */
1567 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1568 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1571 static void fg_init(struct battery_info *di)
1575 adc_ctrl_val = 0x30;
1576 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1579 /* get the volatege offset */
1580 _get_voltage_offset_value(di);
1581 rk_battery_charger_init(di);
1582 _set_relax_thres(di);
1583 /* get the current offset , the value write to the CAL_OFFSET */
1584 di->current_offset = _get_ioffset(di);
1585 _set_cal_offset(di, di->current_offset+42);
1587 _capacity_init(di, di->nac);
1589 di->remain_capacity = _get_realtime_capacity(di);
1590 di->current_avg = _get_average_current(di);
1592 low_waring_init(di);
1594 power_on_save(di, di->voltage_ocv);
1595 /* set sample time for cal_offset interval*/
1596 ioffset_sample_time(di, SAMP_TIME_8MIN);
1597 dump_gauge_register(di);
1598 dump_charger_register(di);
1601 "nac = %d , remain_capacity = %d\n"
1602 "OCV_voltage = %d, voltage = %d\n"
1603 "SOC = %d, fcc = %d\n",
1605 di->nac, di->remain_capacity,
1606 di->voltage_ocv, di->voltage,
1607 di->real_soc, di->fcc);
1611 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1612 static void zero_get_soc(struct battery_info *di)
1614 int ocv_voltage, check_voltage;
1615 int temp_soc = -1, real_soc;
1616 int currentold, currentnow, voltage;
1621 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1622 /* if (di->voltage < 3600)//di->warnning_voltage) */
1624 /* DBG("+++++++zero mode++++++++displaysoc+++++++++\n"); */
1626 currentold = _get_average_current(di);
1627 _get_cal_offset(di);
1630 currentnow = _get_average_current(di);
1632 } while ((currentold == currentnow) && (count_num < 11));
1635 for (i = 0; i < 10 ; i++)
1636 voltage += rk_battery_voltage(di);
1639 if (di->voltage_old == 0)
1640 di->voltage_old = voltage;
1641 voltage_k = voltage;
1642 voltage = (di->voltage_old*2 + 8*voltage)/10;
1643 di->voltage_old = voltage;
1644 /* DBG("Zero: voltage = %d\n", voltage); */
1646 currentnow = _get_average_current(di);
1647 /* DBG(" zero: current = %d, voltage = %d\n", currentnow, voltage); */
1649 ocv_voltage = 3400 + abs32_int(currentnow)*200/1000;
1650 check_voltage = voltage + abs32_int(currentnow)*(200 - 65)/1000; /* 65 mo power-path mos */
1651 _voltage_to_capacity(di, check_voltage);
1652 /* if ((di->remain_capacity > di->nac) && (update_q == 0)) */
1653 /* DBG(" xxx Zerro: tui suan OCV cap :%d\n", di->temp_nac); */
1654 di->update_q = di->remain_capacity - di->temp_nac;
1655 /* update_q = di->temp_nac; */
1657 /* DBG("Zero: update_q = %d , remain_capacity = %d, temp_nac = %d\n ", di->update_q, di->remain_capacity, di->temp_nac); */
1658 /* relax_volt_update_remain_capacity(di, 3600 + abs32_int(di->current_avg)*200/1000); */
1660 _voltage_to_capacity(di, ocv_voltage);
1662 temp_soc = _get_soc(di); */
1663 if (di->display_soc == 0)
1664 di->display_soc = di->real_soc*1000;
1666 real_soc = di->display_soc;
1667 /* DBG(" Zerro: Q (err) cap :%d\n", di->temp_nac);
1668 DBG(" ZERO : real-soc = %d\n ", di->real_soc); */
1669 DBG("ZERO : ocv_voltage = %d, check_voltage = %d\n ", ocv_voltage, check_voltage);
1670 if (di->remain_capacity > di->temp_nac + di->update_q) {
1672 if (di->update_k == 0 || di->update_k >= 10) {
1673 /* DBG("one..\n"); */
1674 if (di->update_k == 0) {
1675 di->line_q = di->temp_nac + di->update_q; /* ZQ = Q_ded + Qerr */
1676 /* line_q = update_q - di->temp_nac; */
1677 temp_soc = (di->remain_capacity - di->line_q)*1000/di->fcc;/* (RM - ZQ) / FCC = r0 = R0 ; */
1678 /* temp_soc = (line_q)*1000/di->fcc;//(RM - ZQ) / FCC = r0 = R0 ;*
1679 /di->line_k = (real_soc*1000 + temp_soc/2)/temp_soc;//k0 = y0/x0 */
1680 di->line_k = (real_soc + temp_soc/2)/temp_soc;/* k0 = y0/x0 */
1681 /* DBG("Zero: one link = %d realsoc = %d , temp_soc = %d\n", di->line_k, di->real_soc, temp_soc); */
1687 line_q = di->temp_nac + update_q;
1689 /* DBG("two...\n"); */
1690 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* x1 10 */
1692 temp_soc = (line_q)*1000/di->fcc;// x1
1693 real_soc = (di->line_k*temp_soc+500)/1000; //y1 = k0*x1
1695 real_soc = (di->line_k*temp_soc); /* y1 = k0*x1 */
1696 /* DBG("Zero: two link = %d realsoc = %d , temp_soc = %d\n", di->line_k, real_soc, temp_soc); */
1697 di->display_soc = real_soc;
1698 /* if (real_soc != di->real_soc) */
1699 if ((real_soc+500)/1000 < di->real_soc)
1702 DBG("Zero two di->real_soc = %d\n", di->real_soc);
1703 DBG("Zero : temp_soc : %d\n", real_soc);
1705 _voltage_to_capacity(di, ocv_voltage);
1706 di->line_q = di->temp_nac + di->update_q; /* Q1 */
1707 /* line_q = update_q - di->temp_nac; */
1708 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* z1 */
1710 temp_soc = (line_q)*1000/di->fcc;
1711 di->line_k = (di->real_soc*1000 + temp_soc/2)/temp_soc; //k1 = y1/z1
1713 di->line_k = (di->display_soc + temp_soc/2)/temp_soc; /* k1 = y1/z1 */
1714 /* DBG("Zero: two link = %d display_soc = %d , temp_soc = %d\n", di->line_k, di->display_soc, temp_soc); */
1715 /* line_q = di->temp_nac + update_q;// Q1 */
1723 /* DBG("di->remain_capacity = %d, line_q = %d\n ", di->remain_capacity, di->line_q); */
1726 if (di->update_k == 1 || di->update_k != 10) {
1727 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc;/* x */
1728 di->display_soc = di->line_k*temp_soc;
1729 /* if (((di->line_k*temp_soc+500)/1000) != di->real_soc), */
1730 DBG("ZERO : display-soc = %d, real-soc = %d\n", di->display_soc, di->real_soc);
1731 if ((di->display_soc+500)/1000 < di->real_soc)
1733 /* di->real_soc = (line_k*temp_soc+500)/1000 ;//y = k0*x */
1736 /* DBG("three..\n"); */
1738 if (di->update_k > 10) {
1744 DBG("ZERO : update_k = %d\n", di->update_k);
1745 DBG("ZERO : remain_capacity = %d , nac = %d, update_q = %d\n", di->remain_capacity, di->line_q, di->update_q);
1746 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);
1751 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1754 int now_current, soc_time = -1;
1757 voltage = di->voltage;
1758 now_current = di->current_avg;
1759 if (now_current == 0)
1761 soc_time = di->fcc*3600/100/(abs_int(now_current));
1762 _voltage_to_capacity(di, 3800);
1763 volt_to_soc = di->temp_soc;
1764 di->temp_soc = _get_soc(di);
1766 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
1767 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1768 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
1772 } else if (di->temp_soc == di->real_soc) {
1773 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1774 } else if (di->temp_soc > di->real_soc) {
1775 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1776 di->vol_smooth_time++;
1777 if (di->vol_smooth_time > soc_time*3) {
1779 di->vol_smooth_time = 0;
1783 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1784 if (di->real_soc == (di->temp_soc + 1)) {
1785 di->change_timer = di->soc_timer;
1786 di->real_soc = di->temp_soc;
1788 di->vol_smooth_time++;
1789 if (di->vol_smooth_time > soc_time/3) {
1791 di->vol_smooth_time = 0;
1796 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1797 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
1800 static int get_charging_time(struct battery_info *di)
1802 return (di->charging_time/60);
1805 static int get_discharging_time(struct battery_info *di)
1807 return (di->discharging_time/60);
1809 static void dump_debug_info(struct battery_info *di)
1811 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
1812 u8 usb_ctrl_reg, chrg_ctrl_reg1;
1813 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
1815 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
1816 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
1817 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
1818 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
1819 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1820 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1821 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1822 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1823 battery_read(di->rk818, 0x00, &rtc_val, 1);
1825 DBG("\n------------- dump_debug_regs -----------------\n"
1826 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
1827 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
1828 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
1829 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
1830 ggcon_reg, ggsts_reg, rtc_val,
1831 sup_tst_reg, vb_mod_reg,
1832 usb_ctrl_reg, chrg_ctrl_reg1,
1833 chrg_ctrl_reg2, chrg_ctrl_reg3
1837 "########################## [read] ################################\n"
1838 "info: 3.4v low warning, digital 100mA finish, 4.2v, 1.6A\n"
1839 "-----------------------------------------------------------------\n"
1840 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
1841 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
1842 "diplay_soc = %d, cpapacity_soc = %d\n"
1843 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
1844 "finish_real_soc = %d, finish_temp_soc = %d\n"
1845 "chrg_time = %d, dischrg_time = %d\n",
1846 get_relax_voltage(di),
1847 di->voltage, di->current_avg,
1848 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
1849 di->real_soc, _get_soc(di),
1850 di->ac_online, di->usb_online, di->status,
1851 di->debug_finish_real_soc, di->debug_finish_temp_soc,
1852 get_charging_time(di), get_discharging_time(di)
1854 get_charge_status(di);
1855 DBG("################################################################\n");
1859 static void update_fcc_capacity(struct battery_info *di)
1861 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
1862 if (get_level(di) >= di->dod0_level) {
1863 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
1864 if (di->fcc > di->qmax)
1867 _capacity_init(di, di->fcc);
1868 _save_FCC_capacity(di, di->fcc);
1870 di->dod0_status = 0;
1874 static void debug_get_finish_soc(struct battery_info *di)
1876 if (di->charge_status == CHARGE_FINISH) {
1877 di->debug_finish_real_soc = di->real_soc;
1878 di->debug_finish_temp_soc = di->temp_soc;
1882 static void wait_charge_finish_signal(struct battery_info *di)
1884 if (di->charge_status == CHARGE_FINISH)
1885 update_fcc_capacity(di);/* save new fcc*/
1888 debug_get_finish_soc(di);
1891 static void charge_finish_routine(struct battery_info *di)
1893 if (di->charge_status == CHARGE_FINISH) {
1894 _capacity_init(di, di->fcc);
1895 zero_current_calibration(di);
1897 if (di->real_soc < 100) {
1898 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
1899 if ((di->soc_counter < 80)) {
1902 di->soc_counter = 0;
1909 static void voltage_to_soc_charge_smooth(struct battery_info *di)
1911 int now_current, soc_time;
1913 now_current = _get_average_current(di);
1914 if (now_current == 0)
1916 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
1917 di->temp_soc = _get_soc(di);
1919 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1921 if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
1922 di->charge_smooth_time++;
1924 if (di->charge_smooth_time > soc_time/3) {
1926 di->charge_smooth_time = 0;
1928 di->charge_smooth_status = true;
1931 if (di->real_soc == di->temp_soc) {
1932 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1933 di->temp_soc = _get_soc(di);
1935 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
1937 if (di->temp_soc < di->real_soc + 1) {
1938 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1939 di->charge_smooth_time++;
1940 if (di->charge_smooth_time > soc_time*2) {
1942 di->charge_smooth_time = 0;
1944 di->charge_smooth_status = true;
1947 else if (di->temp_soc > di->real_soc + 1) {
1948 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1949 di->charge_smooth_time++;
1950 if (di->charge_smooth_time > soc_time/3) {
1952 di->charge_smooth_time = 0;
1954 di->charge_smooth_status = true;
1956 } else if (di->temp_soc == di->real_soc + 1) {
1957 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
1958 if (di->charge_smooth_status) {
1959 di->charge_smooth_time++;
1960 if (di->charge_smooth_time > soc_time/3) {
1961 di->real_soc = di->temp_soc;
1962 di->charge_smooth_time = 0;
1963 di->charge_smooth_status = false;
1967 di->real_soc = di->temp_soc;
1968 di->charge_smooth_status = false;
1974 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1975 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
1978 static void rk_battery_display_smooth(struct battery_info *di)
1983 status = di->status;
1984 charge_status = di->charge_status;
1985 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
1987 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
1988 voltage_to_soc_discharge_smooth(di);
1990 voltage_to_soc_charge_smooth(di);
1992 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
1993 voltage_to_soc_discharge_smooth(di);
1994 if (di->real_soc == 1) {
1996 if (di->time2empty >= 300)
2006 static void software_recharge(struct battery_info *di, int max_cnt)
2008 static int recharge_cnt;
2011 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2012 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2013 chrg_ctrl_reg1 &= ~(1 << 7);
2014 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2015 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2016 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2018 chrg_ctrl_reg1 |= (1 << 7);
2019 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2020 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2021 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2029 static int estimate_battery_resister(struct battery_info *di)
2032 int avr_voltage1 = 0, avr_current1;
2033 int avr_voltage2 = 0, avr_current2;
2035 int bat_res, ocv_votage;
2036 static unsigned long last_time;
2037 unsigned long delta_time;
2038 int charge_ocv_voltage1, charge_ocv_voltage2;
2039 int charge_ocv_soc1, charge_ocv_soc2;
2041 delta_time = get_seconds() - last_time;
2042 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2043 if (delta_time >= 20) {/*20s*/
2046 set_charge_current(di, ILIM_450MA);/*450mA*/
2048 for (i = 0; i < 10 ; i++) {
2050 avr_voltage1 += rk_battery_voltage(di);
2053 avr_current1 = _get_average_current(di);
2054 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2055 DBG("------------------------------------------------------------------------------------------\n");
2056 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2059 set_charge_current(di, ILIM_3000MA);
2061 for (i = 0; i < 10 ; i++) {
2063 avr_voltage2 += rk_battery_voltage(di);
2066 avr_current2 = _get_average_current(di);
2067 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2068 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2070 /*calc resister and ocv_votage ocv*/
2071 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2072 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2073 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2075 /*calc sample voltage ocv*/
2076 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2077 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2078 _voltage_to_capacity(di, charge_ocv_voltage1);
2079 charge_ocv_soc1 = di->temp_soc;
2080 _voltage_to_capacity(di, charge_ocv_voltage2);
2081 charge_ocv_soc2 = di->temp_soc;
2083 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2084 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2085 DBG("------------------------------------------------------------------------------------------\n");
2086 last_time = get_seconds();
2096 static int update_battery_resister(struct battery_info *di)
2100 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2102 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2103 tmp_res = estimate_battery_resister(di);
2105 di->bat_res_update_cnt++;
2106 di->bat_res += tmp_res;
2107 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2108 if (di->bat_res_update_cnt == 10) {
2109 di->bat_res_updated = true;
2112 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2121 static void charge_soc_check_routine(struct battery_info *di)
2129 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2130 min = get_charging_time(di);
2131 update_battery_resister(di);
2133 if ((min >= 30) && (di->bat_res_updated)) {
2135 old_temp_soc = di->temp_soc;
2136 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2137 _voltage_to_capacity(di, ocv_voltage);
2138 ocv_temp_soc = di->temp_soc;
2140 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2141 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2142 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2144 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2145 di->temp_soc = ocv_temp_soc;
2147 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2149 remain_capcity = di->temp_soc * di->fcc / 100;
2150 _capacity_init(di, remain_capcity);
2151 di->remain_capacity = _get_realtime_capacity(di);
2152 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2160 static void update_resume_status_relax_voltage(struct battery_info *di)
2162 unsigned long sleep_soc;
2163 unsigned long sum_sleep_soc;
2164 unsigned long sleep_sec;
2169 int sum_sleep_avr_current;
2173 update_battery_info(di);
2175 di->sys_wakeup = true;
2177 DBG("<%s>, resume----------checkstart\n", __func__);
2178 sleep_sec = get_seconds() - di->suspend_time_start;
2179 sleep_min = sleep_sec / 60;
2181 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2182 __func__, sleep_sec, sleep_min);
2184 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2185 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2187 delta_capacity = di->suspend_capacity - di->remain_capacity;
2188 delta_soc = di->suspend_temp_soc - _get_soc(di);
2189 di->dischrg_sum_sleep_capacity += delta_capacity;
2190 di->dischrg_sum_sleep_sec += sleep_sec;
2192 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2193 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2195 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2196 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2197 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2198 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2199 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2200 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2201 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2203 /*large suspend current*/
2204 if (sum_sleep_avr_current > 20) {
2205 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2206 di->real_soc -= sum_sleep_soc;
2207 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2208 __func__, sum_sleep_soc, di->real_soc);
2210 /* small suspend current*/
2211 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2213 relax_voltage = get_relax_voltage(di);
2214 di->voltage = rk_battery_voltage(di);
2216 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2217 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2218 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2221 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2222 if (sum_sleep_soc > 0)
2223 di->real_soc -= sum_sleep_soc;
2227 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2228 di->dischrg_sum_sleep_capacity = 0;
2229 di->dischrg_sum_sleep_sec = 0;
2231 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2232 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2235 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2236 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2237 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2238 di->temp_soc = _get_soc(di);
2239 charge_status = get_charge_status(di);
2241 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2242 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2243 DBG("resume, sleep : ac online charge current < 1000\n");
2244 if (sleep_sec > 0) {
2245 di->count_sleep_time += sleep_sec;
2246 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2247 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2249 di->count_sleep_time = 0;
2250 di->real_soc += sleep_soc;
2251 if (di->real_soc > 100)
2256 DBG("<%s>, usb charging\n", __func__);
2257 if (di->suspend_temp_soc + 15 < di->temp_soc)
2258 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2260 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2263 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);
2270 #ifdef SUPPORT_USB_CHARGE
2271 static int get_charging_status_type(struct battery_info *di)
2273 int otg_status = dwc_otg_check_dpdm();
2275 if (0 == otg_status) {
2278 di->check_count = 0;
2280 } else if (1 == otg_status) {
2281 if (0 == get_gadget_connect_flag()) {
2282 if (++di->check_count >= 5) {
2294 } else if (2 == otg_status) {
2297 di->check_count = 0;
2300 if (di->ac_online == 1)
2301 set_charge_current(di, ILIM_3000MA);
2303 set_charge_current(di, ILIM_450MA);
2309 static void battery_poweron_status_init(struct battery_info *di)
2313 #ifndef SUPPORT_USB_CHARGE
2317 #ifdef SUPPORT_USB_CHARGE
2319 otg_status = dwc_otg_check_dpdm();
2320 if (otg_status == 1) {
2323 set_charge_current(di, ILIM_450MA);
2324 di->status = POWER_SUPPLY_STATUS_CHARGING;
2325 DBG("++++++++ILIM_450MA++++++\n");
2327 } else if (otg_status == 2) {
2330 di->status = POWER_SUPPLY_STATUS_CHARGING;
2331 set_charge_current(di, ILIM_3000MA);
2332 DBG("++++++++ILIM_1000MA++++++\n");
2334 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2338 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2339 if (buf&PLUG_IN_STS) {
2342 di->status = POWER_SUPPLY_STATUS_CHARGING;
2343 if (di->real_soc == 100)
2344 di->status = POWER_SUPPLY_STATUS_FULL;
2346 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2350 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2355 static void check_battery_status(struct battery_info *di)
2360 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2361 #ifdef SUPPORT_USB_CHARGE
2363 if (strstr(saved_command_line, "charger")) {
2364 if ((buf&PLUG_IN_STS) == 0) {
2365 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2371 if (buf&PLUG_IN_STS) {
2372 get_charging_status_type(di);
2374 di->status = POWER_SUPPLY_STATUS_CHARGING;
2375 if (di->real_soc == 100)
2376 di->status = POWER_SUPPLY_STATUS_FULL;
2378 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2385 if (buf & PLUG_IN_STS) {
2388 di->status = POWER_SUPPLY_STATUS_CHARGING;
2389 if (di->real_soc == 100)
2390 di->status = POWER_SUPPLY_STATUS_FULL;
2392 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2399 static void report_power_supply_changed(struct battery_info *di)
2402 static u32 old_ac_status;
2403 static u32 old_usb_status;
2404 static u32 old_charge_status;
2407 state_changed = false;
2408 if (di->real_soc == 0)
2409 state_changed = true;
2410 else if (di->real_soc == 100)
2411 state_changed = true;
2412 else if (di->real_soc != old_soc)
2413 state_changed = true;
2414 else if (di->ac_online != old_ac_status)
2415 state_changed = true;
2416 else if (di->usb_online != old_usb_status)
2417 state_changed = true;
2418 else if (old_charge_status != di->status)
2419 state_changed = true;
2421 if (state_changed) {
2422 power_supply_changed(&di->bat);
2423 power_supply_changed(&di->usb);
2424 power_supply_changed(&di->ac);
2425 old_soc = di->real_soc;
2426 old_ac_status = di->ac_online;
2427 old_usb_status = di->usb_online;
2428 old_charge_status = di->status;
2432 static void update_battery_info(struct battery_info *di)
2434 di->remain_capacity = _get_realtime_capacity(di);
2435 if (di->remain_capacity > di->fcc)
2436 _capacity_init(di, di->fcc);
2437 else if (di->remain_capacity < 0)
2438 _capacity_init(di, 0);
2440 if (di->real_soc > 100)
2442 else if (di->real_soc < 0)
2445 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2446 di->charging_time++;
2447 di->discharging_time = 0;
2449 di->discharging_time++;
2450 di->charging_time = 0;
2454 di->voltage = rk_battery_voltage(di);
2455 di->current_avg = _get_average_current(di);
2456 di->remain_capacity = _get_realtime_capacity(di);
2457 di->voltage_ocv = _get_OCV_voltage(di);
2458 di->charge_status = get_charge_status(di);
2459 di->otg_status = dwc_otg_check_dpdm();
2460 di->relax_voltage = get_relax_voltage(di);
2461 di->temp_soc = _get_soc(di);
2462 di->remain_capacity = _get_realtime_capacity(di);
2463 check_battery_status(di);/* ac_online, usb_online, status*/
2464 update_cal_offset(di);
2467 static void rk_battery_work(struct work_struct *work)
2469 struct battery_info *di = container_of(work,
2470 struct battery_info, battery_monitor_work.work);
2472 update_resume_status_relax_voltage(di);
2473 wait_charge_finish_signal(di);
2474 charge_finish_routine(di);
2476 rk_battery_display_smooth(di);
2477 update_battery_info(di);
2479 report_power_supply_changed(di);
2480 _copy_soc(di, di->real_soc);
2481 _save_remain_capacity(di, di->remain_capacity);
2483 dump_debug_info(di);
2484 di->queue_work_cnt++;
2485 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2488 static void rk_battery_charge_check_work(struct work_struct *work)
2490 struct battery_info *di = container_of(work,
2491 struct battery_info, charge_check_work.work);
2493 DBG("rk_battery_charge_check_work\n");
2494 charge_disable_open_otg(di->charge_otg);
2497 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2499 int register_battery_notifier(struct notifier_block *nb)
2501 return blocking_notifier_chain_register(&battery_chain_head, nb);
2503 EXPORT_SYMBOL_GPL(register_battery_notifier);
2505 int unregister_battery_notifier(struct notifier_block *nb)
2507 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2509 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2511 int battery_notifier_call_chain(unsigned long val)
2513 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2514 == NOTIFY_BAD) ? -EINVAL : 0;
2516 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2518 static void poweron_lowerpoer_handle(struct battery_info *di)
2520 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2521 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2523 /* kernel_power_off(); */
2528 static int battery_notifier_call(struct notifier_block *nb,
2529 unsigned long event, void *data)
2531 struct battery_info *di =
2532 container_of(nb, struct battery_info, battery_nb);
2536 DBG(" CHARGE enable\n");
2538 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2543 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2544 DBG("charge disable OTG enable\n");
2548 poweron_lowerpoer_handle(di);
2557 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2559 pr_info("<%s>lower power warning!\n", __func__);
2561 _copy_soc(g_battery, 0);
2562 _capacity_init(g_battery, 0);
2563 rk_send_wakeup_key();
2568 static void disable_vbat_low_irq(struct battery_info *di)
2571 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2572 /*clr vbat low interrupt */
2573 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2575 static void enable_vbat_low_irq(struct battery_info *di)
2577 /* clr vbat low interrupt */
2578 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2580 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2583 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2585 pr_info("\n------- %s:irq = %d\n", __func__, irq);
2586 rk_send_wakeup_key();
2589 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
2591 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2592 charge_disable_open_otg(0);
2593 rk_send_wakeup_key();
2597 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
2599 pr_info("---------- %s:irq = %d\n", __func__, irq);
2600 rk_send_wakeup_key();
2606 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2610 struct kobject *rk818_fg_kobj;
2612 ret = create_sysfs_interfaces(dev);
2615 dev_err(dev, "device RK818 battery sysfs register failed\n");
2619 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2622 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2623 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2625 dev_err(dev, "create rk818_battery node error\n");
2633 power_supply_unregister(&di->ac);
2634 power_supply_unregister(&di->usb);
2635 power_supply_unregister(&di->bat);
2640 static void rk818_battery_irq_init(struct battery_info *di)
2642 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2644 struct rk818 *chip = di->rk818;
2646 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
2647 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
2648 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
2649 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
2651 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2652 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2654 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2656 di->irq = vb_lo_irq;
2657 enable_irq_wake(di->irq);
2658 disable_vbat_low_irq(di);
2660 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2661 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2663 dev_err(chip->dev, "plug_in_irq request failed!\n");
2666 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2667 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2669 dev_err(chip->dev, "plug_out_irq request failed!\n");
2672 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2673 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2675 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2678 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2684 di->platform_data = chip->battery_data;
2685 di->cell.config = di->platform_data->cell_cfg;
2686 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
2687 di->qmax = di->platform_data->cell_cfg->design_qmax;
2688 di->fcc = di->design_capacity;
2689 di->vol_smooth_time = 0;
2690 di->charge_smooth_time = 0;
2691 di->charge_smooth_status = false;
2692 di->sleep_status = 0;
2694 di->sys_wakeup = true;
2695 di->pcb_ioffset = 0;
2696 di->pcb_ioffset_updated = false;
2697 di->queue_work_cnt = 0;
2700 di->voltage_old = 0;
2701 di->display_soc = 0;
2703 di->bat_res_updated = false;
2705 di->sys_wakeup = true;
2706 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2708 di->debug_finish_real_soc = 0;
2709 di->debug_finish_temp_soc = 0;
2711 fcc_capacity = _get_FCC_capacity(di);
2712 if (fcc_capacity > 1000)
2713 di->fcc = fcc_capacity;
2715 di->fcc = di->design_capacity;
2718 static struct of_device_id rk818_battery_of_match[] = {
2719 { .compatible = "rk818_battery" },
2723 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2726 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2728 struct device_node *regs, *rk818_pmic_np;
2729 struct battery_platform_data *data;
2730 struct cell_config *cell_cfg;
2731 struct ocv_config *ocv_cfg;
2732 struct property *prop;
2736 rk818_pmic_np = of_node_get(rk818->dev->of_node);
2737 if (!rk818_pmic_np) {
2738 dev_err(dev, "could not find pmic sub-node\n");
2742 regs = of_find_node_by_name(rk818_pmic_np, "battery");
2744 dev_err(dev, "battery node not found!\n");
2748 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2750 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2754 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2756 dev_err(dev, "kzalloc for cell_config failed!\n");
2759 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2761 dev_err(dev, "kzalloc for ocv_config failed!\n");
2765 prop = of_find_property(regs, "ocv_table", &length);
2767 dev_err(dev, "ocv_table not found!\n");
2770 data->ocv_size = length / sizeof(u32);
2772 if (data->ocv_size > 0) {
2773 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2775 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
2776 if (!data->battery_ocv) {
2777 dev_err(dev, "kzalloc for ocv_table failed!\n");
2780 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2785 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2787 dev_err(dev, "max_charge_currentmA not found!\n");
2790 data->max_charger_currentmA = out_value;
2792 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
2794 dev_err(dev, "max_charge_voltagemV not found!\n");
2797 data->max_charger_voltagemV = out_value;
2799 ret = of_property_read_u32(regs, "design_capacity", &out_value);
2801 dev_err(dev, "design_capacity not found!\n");
2804 cell_cfg->design_capacity = out_value;
2806 ret = of_property_read_u32(regs, "design_qmax", &out_value);
2808 dev_err(dev, "design_qmax not found!\n");
2811 cell_cfg->design_qmax = out_value;
2813 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
2815 dev_err(dev, "sleep_enter_current not found!\n");
2818 ocv_cfg->sleep_enter_current = out_value;
2820 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
2822 dev_err(dev, "sleep_exit_current not found!\n");
2825 ocv_cfg->sleep_exit_current = out_value;
2827 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
2829 cell_cfg->ocv = ocv_cfg;
2830 data->cell_cfg = cell_cfg;
2831 rk818->battery_data = data;
2833 DBG("\n--------- the battery OCV TABLE dump:\n");
2834 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
2835 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
2836 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
2837 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
2838 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
2839 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
2840 DBG("uboot chrg = %d\n", support_uboot_chrg);
2841 DBG("\n--------- rk818_battery dt_parse ok.\n");
2846 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2853 static int battery_probe(struct platform_device *pdev)
2855 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
2856 struct battery_info *di;
2859 DBG("battery driver version %s\n", DRIVER_VERSION);
2860 di = kzalloc(sizeof(*di), GFP_KERNEL);
2862 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
2865 ret = rk_battery_parse_dt(chip, &pdev->dev);
2867 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
2871 platform_set_drvdata(pdev, di);
2872 battery_info_init(di, chip);
2875 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
2877 flatzone_voltage_init(di);
2878 battery_poweron_status_init(di);
2879 battery_power_supply_init(di);
2880 ret = battery_power_supply_register(di, &pdev->dev);
2882 dev_err(&pdev->dev, "rk power supply register failed!\n");
2885 di->wq = create_singlethread_workqueue("battery-work");
2886 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
2887 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2888 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
2890 di->battery_nb.notifier_call = battery_notifier_call;
2891 register_battery_notifier(&di->battery_nb);
2893 rk818_battery_irq_init(di);
2894 rk818_battery_sysfs_init(di, &pdev->dev);
2895 DBG("------ RK81x battery_probe ok!-------\n");
2902 static int battery_suspend(struct platform_device *dev, pm_message_t state)
2904 struct battery_info *di = platform_get_drvdata(dev);
2906 enable_vbat_low_irq(di);
2907 di->sleep_status = di->status;
2908 di->suspend_charge_current = _get_average_current(di);
2910 /* avoid abrupt wakeup which will clean the variable*/
2911 if (di->sys_wakeup) {
2912 di->suspend_capacity = di->remain_capacity;
2913 di->suspend_temp_soc = _get_soc(di);
2914 di->suspend_time_start = get_seconds();
2915 di->sys_wakeup = false;
2918 cancel_delayed_work(&di->battery_monitor_work);
2919 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
2920 __func__, di->suspend_temp_soc, di->suspend_charge_current,
2921 di->suspend_capacity, di->sleep_status);
2926 static int battery_resume(struct platform_device *dev)
2928 struct battery_info *di = platform_get_drvdata(dev);
2931 DBG("<%s>\n", __func__);
2932 disable_vbat_low_irq(di);
2933 queue_delayed_work(di->wq, &di->battery_monitor_work,
2934 msecs_to_jiffies(TIMER_MS_COUNTS/2));
2936 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
2938 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
2942 static int battery_remove(struct platform_device *dev)
2944 struct battery_info *di = platform_get_drvdata(dev);
2946 cancel_delayed_work_sync(&di->battery_monitor_work);
2949 static void battery_shutdown(struct platform_device *dev)
2951 struct battery_info *di = platform_get_drvdata(dev);
2953 cancel_delayed_work_sync(&di->battery_monitor_work);
2954 DBG("rk818 shutdown!");
2958 static struct platform_driver battery_driver = {
2960 .name = "rk818-battery",
2961 .owner = THIS_MODULE,
2964 .probe = battery_probe,
2965 .remove = battery_remove,
2966 .suspend = battery_suspend,
2967 .resume = battery_resume,
2968 .shutdown = battery_shutdown,
2971 static int __init battery_init(void)
2973 return platform_driver_register(&battery_driver);
2976 fs_initcall_sync(battery_init);
2977 static void __exit battery_exit(void)
2979 platform_driver_unregister(&battery_driver);
2981 module_exit(battery_exit);
2983 MODULE_LICENSE("GPL");
2984 MODULE_ALIAS("platform:rk818-battery");
2985 MODULE_AUTHOR("ROCKCHIP");