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 DEFAULT_BAT_RES 135
44 #define DEFAULT_VLMT 4200
45 #define DEFAULT_ILMT 2000
46 #define DEFAULT_ICUR 1600
48 #define INTERPOLATE_MAX 1000
49 #define MAX_INT 0x7FFF
50 #define TIME_10MIN_SEC 600
52 #define CHG_VOL_SHIFT 4
53 #define CHG_ILIM_SHIFT 0
54 #define CHG_ICUR_SHIFT 0
56 int CHG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
57 int CHG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000};
58 int CHG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000};
61 struct cell_state cell;
62 struct power_supply bat;
63 struct power_supply ac;
64 struct power_supply usb;
65 struct delayed_work work;
66 /* struct i2c_client *client; */
69 struct battery_platform_data *platform_data;
81 uint16_t relax_voltage;
85 bool pcb_ioffset_updated;
86 unsigned long queue_work_cnt;
87 uint16_t warnning_voltage;
103 int bat_res_update_cnt;
110 unsigned long dod0_time;
121 int current_k;/* (ICALIB0, ICALIB1) */
124 int voltage_k;/* VCALIB0 VCALIB1 */
137 struct timeval soc_timer;
138 struct timeval change_timer;
141 int charge_smooth_time;
143 int suspend_capacity;
145 struct timespec suspend_time;
146 struct timespec resume_time;
147 unsigned long suspend_time_start;
148 unsigned long count_sleep_time;
150 unsigned long dischrg_sum_sleep_sec;
151 unsigned long dischrg_sum_sleep_capacity;
152 int suspend_temp_soc;
154 int suspend_charge_current;
157 bool bat_res_updated;
158 bool charge_smooth_status;
160 unsigned long last_plugin_time;
163 unsigned long charging_time;
164 unsigned long discharging_time;
165 unsigned long finish_time;
170 struct notifier_block battery_nb;
171 struct workqueue_struct *wq;
172 struct delayed_work battery_monitor_work;
173 struct delayed_work charge_check_work;
176 struct wake_lock resume_wake_lock;
178 int debug_finish_real_soc;
179 int debug_finish_temp_soc;
186 struct battery_info *g_battery;
187 u32 support_uboot_chrg;
189 extern int dwc_vbus_status(void);
190 extern int get_gadget_connect_flag(void);
191 extern int dwc_otg_check_dpdm(void);
192 extern void kernel_power_off(void);
193 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
194 extern unsigned int irq_create_mapping(struct irq_domain *domain,
195 irq_hw_number_t hwirq);
196 extern void rk_send_wakeup_key(void);
197 static void update_battery_info(struct battery_info *di);
199 #define SUPPORT_USB_CHARGE
202 static u32 interpolate(int value, u32 *table, int size)
207 for (i = 0; i < size; i++) {
208 if (value < table[i])
212 if ((i > 0) && (i < size)) {
213 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
214 d /= table[i] - table[i-1];
215 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
217 d = i * ((INTERPOLATE_MAX+size/2)/size);
225 /* Returns (a * b) / c */
226 static int32_t ab_div_c(u32 a, u32 b, u32 c)
232 sign = ((((a^b)^c) & 0x80000000) != 0);
238 tmp = ((int32_t) a*b + (c>>1)) / c;
250 static int32_t abs_int(int32_t x)
252 return (x > 0) ? x : -x;
255 static int abs32_int(int x)
257 return (x > 0) ? x : -x;
261 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
265 ret = rk818_i2c_read(rk818, reg, len, buf);
269 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
272 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
275 static void dump_gauge_register(struct battery_info *di)
279 DBG("%s dump charger register start: \n", __func__);
280 for (i = 0xAC; i < 0xDF; i++) {
281 battery_read(di->rk818, i, &buf, 1);
282 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
287 static void dump_charger_register(struct battery_info *di)
292 DBG("%s dump the register start: \n", __func__);
293 for (i = 0x99; i < 0xAB; i++) {
294 battery_read(di->rk818, i, &buf, 1);
295 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
303 static uint16_t _get_OCV_voltage(struct battery_info *di);
304 static int _voltage_to_capacity(struct battery_info *di, int voltage);
305 static int _get_realtime_capacity(struct battery_info *di);
306 static void power_on_save(struct battery_info *di, int voltage);
307 static void _capacity_init(struct battery_info *di, u32 capacity);
308 static void battery_poweron_status_init(struct battery_info *di);
309 static void power_on_save(struct battery_info *di, int voltage);
310 static void flatzone_voltage_init(struct battery_info *di);
311 static int _get_FCC_capacity(struct battery_info *di);
312 static void _save_FCC_capacity(struct battery_info *di, u32 capacity);
313 static int _get_soc(struct battery_info *di);
314 static int _get_average_current(struct battery_info *di);
315 static int rk_battery_voltage(struct battery_info *di);
316 static uint16_t _get_relax_vol1(struct battery_info *di);
317 static uint16_t _get_relax_vol2(struct battery_info *di);
318 static void update_battery_info(struct battery_info *di);
320 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
322 struct battery_info *di = g_battery;
328 battery_read(di->rk818, SUP_STS_REG, &status, 1);
329 battery_read(di->rk818, SOC_REG, &soc_reg, 1);
330 battery_read(di->rk818, 0x00, &rtc_val, 1);
331 di->voltage_ocv = _get_OCV_voltage(di);
332 _voltage_to_capacity(di, di->voltage_ocv);
333 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
335 return sprintf(buf, "-----------------------------------------------------------------------------\n"
336 "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
337 "real_soc = %d, temp_soc = %d\n"
338 "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
339 "usb_online = %d, ac_online = %d\n"
340 "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
341 "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
342 "relax_volt1 = %d, relax_volt2 = %d\n"
343 "---------------------------------------------------------------------------\n",
344 rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
345 di->real_soc, _get_soc(di),
346 di->fcc, _get_FCC_capacity(di), shtd_time,
347 di->usb_online, di->ac_online,
349 di->voltage_k, di->voltage_b, soc_reg,
350 _get_relax_vol1(di), _get_relax_vol2(di));
353 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
355 struct battery_info *di = g_battery;
356 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
357 u8 usb_ctrl_reg, chrg_ctrl_reg1;
358 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
360 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
361 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
362 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
363 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
364 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
365 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
366 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
367 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
368 battery_read(di->rk818, 0x00, &rtc_val, 1);
370 return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
371 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
372 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
373 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
374 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
375 "---------------------------------------------------------------------------\n",
376 ggcon_reg, ggsts_reg, rtc_val,
377 sup_tst_reg, vb_mod_reg,
378 usb_ctrl_reg, chrg_ctrl_reg1,
379 chrg_ctrl_reg2, chrg_ctrl_reg3
382 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
384 struct battery_info *di = g_battery;
386 return sprintf(buf, "%d", di->fcc);
388 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
390 struct battery_info *di = g_battery;
392 return sprintf(buf, "%d", di->real_soc);
395 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
397 struct battery_info *di = g_battery;
399 return sprintf(buf, "%d", di->temp_soc);
402 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
404 struct battery_info *di = g_battery;
406 return sprintf(buf, "%d", di->voltage);
409 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
411 struct battery_info *di = g_battery;
413 return sprintf(buf, "%d", di->current_avg);
416 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
418 struct battery_info *di = g_battery;
420 return sprintf(buf, "%d", di->remain_capacity);
423 static struct device_attribute rk818_bat_attr[] = {
424 __ATTR(state, 0664, bat_state_read, NULL),
425 __ATTR(regs, 0664, bat_reg_read, NULL),
426 __ATTR(fcc, 0664, bat_fcc_read, NULL),
427 __ATTR(soc, 0664, bat_soc_read, NULL),
428 __ATTR(temp_soc, 0664, bat_temp_soc_read, NULL),
429 __ATTR(voltage, 0664, bat_voltage_read, NULL),
430 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
431 __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
436 static uint16_t get_relax_voltage(struct battery_info *di);
438 static ssize_t show_state_attrs(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct battery_info *data = g_battery;
443 if (0 == get_relax_voltage(data)) {
445 "voltage = %d, remain_capacity = %d, status = %d\n",
446 data->voltage, data->remain_capacity,
451 "voltage = %d, remain_capacity = %d, status = %d\n",
452 get_relax_voltage(data), data->remain_capacity,
456 static ssize_t restore_state_attrs(struct device *dev,
457 struct device_attribute *attr, const char *buf, size_t size)
461 static struct device_attribute rkbatt_attrs[] = {
462 __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
465 static int create_sysfs_interfaces(struct device *dev)
469 for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++) {
470 if (device_create_file(dev, rkbatt_attrs + liTmep))
477 for (; liTmep >= 0; liTmep--)
478 device_remove_file(dev, rkbatt_attrs + liTmep);
480 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
484 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
488 battery_read(di->rk818, reg, &val, 1);
489 DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
494 static int _gauge_enable(struct battery_info *di)
499 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
501 dev_err(di->dev, "error reading TS_CTRL_REG");
504 if (!(buf & GG_EN)) {
506 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); /* enable */
507 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
511 DBG("%s, %d\n", __func__, buf);
515 static void save_level(struct battery_info *di, u8 save_soc)
520 battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
522 static u8 get_level(struct battery_info *di)
526 battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
530 static int _get_vcalib0(struct battery_info *di)
536 ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
538 ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
541 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
545 static int _get_vcalib1(struct battery_info *di)
551 ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
553 ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
556 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
560 static int _get_ioffset(struct battery_info *di)
567 ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
569 ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
575 static uint16_t _get_cal_offset(struct battery_info *di)
581 ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
583 ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
588 static int _set_cal_offset(struct battery_info *di, u32 value)
594 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
595 buf = (value >> 8)&0xff;
596 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
600 static void _get_voltage_offset_value(struct battery_info *di)
602 int vcalib0, vcalib1;
604 vcalib0 = _get_vcalib0(di);
605 vcalib1 = _get_vcalib1(di);
607 di->voltage_k = (4200 - 3000)*1000/(vcalib1 - vcalib0);
608 di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
609 DBG("voltage_k = %d(x1000) voltage_b = %d\n", di->voltage_k, di->voltage_b);
611 static uint16_t _get_OCV_voltage(struct battery_info *di)
616 uint16_t voltage_now = 0;
618 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
620 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
624 dev_err(di->dev, "error read BAT_OCV_REGH");
628 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
633 static int rk_battery_voltage(struct battery_info *di)
640 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
642 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
646 dev_err(di->dev, "error read BAT_VOL_REGH");
650 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
656 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
657 * based on the voltage.
659 static int _voltage_to_capacity(struct battery_info *di, int voltage)
665 ocv_table = di->platform_data->battery_ocv;
666 ocv_size = di->platform_data->ocv_size;
667 di->warnning_voltage = ocv_table[3];
668 tmp = interpolate(voltage, ocv_table, ocv_size);
669 di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
670 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
675 static uint16_t _get_relax_vol1(struct battery_info *di)
679 uint16_t temp = 0, voltage_now;
681 ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
683 ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
686 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
691 static uint16_t _get_relax_vol2(struct battery_info *di)
694 uint16_t temp = 0, voltage_now;
697 ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
699 ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
702 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
707 static int _get_raw_adc_current(struct battery_info *di)
713 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
715 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
719 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
721 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
724 current_now |= (buf<<8);
727 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
735 static void reset_zero_var(struct battery_info *di)
743 static void ioffset_sample_time(struct battery_info *di, int time)
747 battery_read(di->rk818, GGCON, &ggcon, 1);
748 ggcon &= ~(0x30); /*clear <5:4>*/
750 battery_write(di->rk818, GGCON, &ggcon, 1);
751 debug_reg(di, GGCON, "GGCON");
754 static void update_cal_offset(struct battery_info *di)
756 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
758 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
759 if ((!mod) && (di->pcb_ioffset_updated)) {
760 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
761 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
766 static void zero_current_calibration(struct battery_info *di)
775 if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
777 for (retry = 0; retry < 5; retry++) {
778 adc_value = _get_raw_adc_current(di);
779 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
781 C0 = _get_cal_offset(di);
783 _set_cal_offset(di, C1);
784 DBG("<%s>. C1 = %d\n", __func__, C1);
787 adc_value = _get_raw_adc_current(di);
788 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
791 ioffset = _get_ioffset(di);
792 pcb_offset = C1 - ioffset;
793 di->pcb_ioffset = pcb_offset;
794 di->pcb_ioffset_updated = true;
795 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
798 di->pcb_ioffset_updated = false;
804 static bool _is_relax_mode(struct battery_info *di)
809 ret = battery_read(di->rk818, GGSTS, &status, 1);
811 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
817 static uint16_t get_relax_voltage(struct battery_info *di)
821 uint16_t relax_vol1, relax_vol2;
824 ret = battery_read(di->rk818, GGSTS, &status, 1);
825 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
827 relax_vol1 = _get_relax_vol1(di);
828 relax_vol2 = _get_relax_vol2(di);
829 DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
830 if (_is_relax_mode(di))
831 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
836 static void _set_relax_thres(struct battery_info *di)
839 int enter_thres, exit_thres;
840 struct cell_state *cell = &di->cell;
842 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
843 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
845 buf = enter_thres&0xff;
846 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
847 buf = (enter_thres>>8)&0xff;
848 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
850 buf = exit_thres&0xff;
851 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
852 buf = (exit_thres>>8)&0xff;
853 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
855 /* set sample time */
856 battery_read(di->rk818, GGCON, &buf, 1);
857 buf &= ~(3<<2);/*8min*/
858 buf &= ~0x01; /* clear bat_res calc*/
859 battery_write(di->rk818, GGCON, &buf, 1);
862 static void restart_relax(struct battery_info *di)
864 u8 ggcon;/* chrg_ctrl_reg2;*/
867 battery_read(di->rk818, GGCON, &ggcon, 1);
869 battery_write(di->rk818, GGCON, &ggcon, 1);
871 battery_read(di->rk818, GGSTS, &ggsts, 1);
873 battery_write(di->rk818, GGSTS, &ggsts, 1);
876 static int _get_average_current(struct battery_info *di)
883 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
885 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
889 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
891 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
894 current_now |= (buf<<8);
896 if (current_now & 0x800)
899 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
905 static bool is_bat_exist(struct battery_info *di)
909 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
910 return (buf & 0x80) ? true : false;
913 static bool _is_first_poweron(struct battery_info *di)
918 battery_read(di->rk818, GGSTS, &buf, 1);
919 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
920 /*di->pwron_bat_con = buf;*/
924 battery_write(di->rk818, GGSTS, &buf, 1);
925 battery_read(di->rk818, GGSTS, &temp, 1);
926 } while (temp&BAT_CON);
931 static void flatzone_voltage_init(struct battery_info *di)
938 ocv_table = di->platform_data->battery_ocv;
939 ocv_size = di->platform_data->ocv_size;
941 for (j = 0; j < 21; j++)
945 for (i = 1; i < ocv_size-1; i++) {
946 if (ocv_table[i+1] < ocv_table[i] + 20)
950 temp_table[j] = temp_table[j-1]+1;
952 di->enter_flatzone = ocv_table[i];
956 for (i = 0; i <= 20; i++) {
957 if (temp_table[i] < temp_table[i+1])
962 di->exit_flatzone = ocv_table[i];
964 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
969 static int is_not_flatzone(struct battery_info *di, int voltage)
971 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
972 DBG("<%s>. is in flat zone\n", __func__);
975 DBG("<%s>. is not in flat zone\n", __func__);
980 static void power_on_save(struct battery_info *di, int voltage)
985 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
987 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
988 _voltage_to_capacity(di, voltage);
989 if (di->temp_soc < 20) {
990 di->dod0_voltage = voltage;
991 di->dod0_capacity = di->nac;
993 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
996 if (di->temp_soc <= 0)
997 di->dod0_level = 100;
998 else if (di->temp_soc < 5)
1000 else if (di->temp_soc < 10)
1001 di->dod0_level = 90;
1002 /* save_soc = di->dod0_level; */
1003 save_soc = get_level(di);
1004 if (save_soc < di->dod0_level)
1005 save_soc = di->dod0_level;
1006 save_level(di, save_soc);
1007 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
1014 static int _get_soc(struct battery_info *di)
1016 return di->remain_capacity * 100 / di->fcc;
1019 static enum power_supply_property rk_battery_props[] = {
1021 POWER_SUPPLY_PROP_STATUS,
1022 POWER_SUPPLY_PROP_CURRENT_NOW,
1023 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1024 POWER_SUPPLY_PROP_PRESENT,
1025 POWER_SUPPLY_PROP_HEALTH,
1026 POWER_SUPPLY_PROP_CAPACITY,
1029 #define to_device_info(x) container_of((x), \
1030 struct battery_info, bat)
1032 static int rk_battery_get_property(struct power_supply *psy,
1033 enum power_supply_property psp,
1034 union power_supply_propval *val)
1037 struct battery_info *di = to_device_info(psy);
1040 case POWER_SUPPLY_PROP_CURRENT_NOW:
1041 val->intval = di->current_avg*1000;/*uA*/
1044 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1045 val->intval = di->voltage*1000;/*uV*/
1048 case POWER_SUPPLY_PROP_PRESENT:
1049 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1050 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1051 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1055 case POWER_SUPPLY_PROP_CAPACITY:
1056 val->intval = di->real_soc;
1059 case POWER_SUPPLY_PROP_HEALTH:
1060 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1063 case POWER_SUPPLY_PROP_STATUS:
1064 val->intval = di->status;
1075 static enum power_supply_property rk_battery_ac_props[] = {
1076 POWER_SUPPLY_PROP_ONLINE,
1078 static enum power_supply_property rk_battery_usb_props[] = {
1079 POWER_SUPPLY_PROP_ONLINE,
1083 #define to_ac_device_info(x) container_of((x), \
1084 struct battery_info, ac)
1086 static int rk_battery_ac_get_property(struct power_supply *psy,
1087 enum power_supply_property psp,
1088 union power_supply_propval *val)
1091 struct battery_info *di = to_ac_device_info(psy);
1094 case POWER_SUPPLY_PROP_ONLINE:
1095 val->intval = di->ac_online; /*discharging*/
1105 #define to_usb_device_info(x) container_of((x), \
1106 struct battery_info, usb)
1108 static int rk_battery_usb_get_property(struct power_supply *psy,
1109 enum power_supply_property psp,
1110 union power_supply_propval *val)
1113 struct battery_info *di = to_usb_device_info(psy);
1116 case POWER_SUPPLY_PROP_ONLINE:
1117 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1120 val->intval = di->usb_online;
1132 static void battery_power_supply_init(struct battery_info *di)
1134 di->bat.name = "BATTERY";
1135 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1136 di->bat.properties = rk_battery_props;
1137 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1138 di->bat.get_property = rk_battery_get_property;
1141 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1142 di->ac.properties = rk_battery_ac_props;
1143 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1144 di->ac.get_property = rk_battery_ac_get_property;
1146 di->usb.name = "USB";
1147 di->usb.type = POWER_SUPPLY_TYPE_USB;
1148 di->usb.properties = rk_battery_usb_props;
1149 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1150 di->usb.get_property = rk_battery_usb_get_property;
1153 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1157 ret = power_supply_register(dev, &di->bat);
1159 dev_err(dev, "failed to register main battery\n");
1162 ret = power_supply_register(dev, &di->usb);
1164 dev_err(dev, "failed to register usb power supply\n");
1167 ret = power_supply_register(dev, &di->ac);
1169 dev_err(dev, "failed to register ac power supply\n");
1176 power_supply_unregister(&di->ac);
1178 power_supply_unregister(&di->usb);
1180 power_supply_unregister(&di->bat);
1185 static void _capacity_init(struct battery_info *di, u32 capacity)
1192 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1194 buf = (capacity_ma>>24)&0xff;
1195 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1196 buf = (capacity_ma>>16)&0xff;
1197 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1198 buf = (capacity_ma>>8)&0xff;
1199 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1200 buf = (capacity_ma&0xff) | 0x01;
1201 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1202 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1208 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1213 if (capacity >= di->qmax)
1214 capacity = di->qmax;
1216 capacity_ma = capacity;
1218 buf = (capacity_ma>>24)&0xff;
1219 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1220 buf = (capacity_ma>>16)&0xff;
1221 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1222 buf = (capacity_ma>>8)&0xff;
1223 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1224 buf = (capacity_ma&0xff) | 0x01;
1225 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1228 static int _get_remain_capacity(struct battery_info *di)
1235 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1237 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1239 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1241 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1244 capacity = temp;/* /4096*900/14/36*500/521; */
1250 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1255 capacity_ma = capacity;
1256 buf = (capacity_ma>>24)&0xff;
1257 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1258 buf = (capacity_ma>>16)&0xff;
1259 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1260 buf = (capacity_ma>>8)&0xff;
1261 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1262 buf = (capacity_ma&0xff) | 0x01;
1263 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1266 static int _get_FCC_capacity(struct battery_info *di)
1273 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1275 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1277 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1279 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1283 capacity = temp-1;/* 4096*900/14/36*500/521 */
1286 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1291 static int _get_realtime_capacity(struct battery_info *di)
1298 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1300 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1302 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1304 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1307 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1312 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1314 int remain_capacity;
1322 now_temp_soc = _get_soc(di);
1323 _voltage_to_capacity(di, relax_voltage);
1324 relax_soc = di->temp_soc;
1325 relax_capacity = di->temp_nac;
1326 abs_soc = abs32_int(relax_soc - now_temp_soc);
1328 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1329 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1332 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1333 remain_capacity = relax_capacity;
1334 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1337 remain_capacity = _get_realtime_capacity(di);
1338 else if (abs_soc <= 10)
1339 remain_capacity = relax_capacity;
1340 else if (abs_soc <= 20)
1341 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1343 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1345 _capacity_init(di, remain_capacity);
1346 di->temp_soc = _get_soc(di);
1347 di->remain_capacity = _get_realtime_capacity(di);
1350 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1351 if (relax_soc < now_temp_soc) {
1352 if (di->suspend_temp_soc - relax_soc <= 5)
1353 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1354 else if (di->suspend_temp_soc - relax_soc <= 10)
1355 di->real_soc = di->real_soc - 5;
1357 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1359 now_current = _get_average_current(di);
1360 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1361 min = soc_time / 60;
1362 if (sleep_min > min)
1366 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);
1370 static int _copy_soc(struct battery_info *di, u8 save_soc)
1375 battery_write(di->rk818, SOC_REG, &soc, 1);
1379 static bool support_uboot_charge(void)
1381 return support_uboot_chrg?true:false;
1384 static int _rsoc_init(struct battery_info *di)
1388 u32 remain_capacity;
1391 #ifdef SUPPORT_USB_CHARGE
1396 di->voltage = rk_battery_voltage(di);
1397 di->voltage_ocv = _get_OCV_voltage(di);
1398 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1400 if (_is_first_poweron(di)) {
1401 _save_FCC_capacity(di, di->design_capacity);
1402 di->fcc = _get_FCC_capacity(di);
1404 _voltage_to_capacity(di, di->voltage_ocv);
1405 di->real_soc = di->temp_soc;
1406 di->nac = di->temp_nac;
1407 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1410 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1411 init_soc = pwron_soc;
1412 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1414 #ifdef SUPPORT_USB_CHARGE
1415 otg_status = dwc_otg_check_dpdm();
1416 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1418 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1421 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1422 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1424 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1427 remain_capacity = _get_remain_capacity(di);
1429 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1430 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1431 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1432 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1434 if (!support_uboot_charge()) {
1435 _voltage_to_capacity(di, di->voltage_ocv);
1436 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1438 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1439 if (((otg_status != 0) && (curr_shtd_time > 0) && (last_shtd_time != curr_shtd_time)) || ((curr_shtd_time > 0) && (otg_status == 0))) {
1441 if (curr_shtd_time > 30) {
1442 remain_capacity = di->temp_nac;
1443 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1445 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - init_soc) >= 10)) {
1446 if (remain_capacity >= di->temp_nac*120/100)
1447 remain_capacity = di->temp_nac*110/100;
1448 else if (remain_capacity < di->temp_nac*8/10)
1449 remain_capacity = di->temp_nac*9/10;
1451 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1456 di->real_soc = init_soc;
1457 di->nac = remain_capacity;
1460 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1466 static u8 get_charge_status(struct battery_info *di)
1471 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1476 DBG(" CHARGE-OFF ...\n");
1481 DBG(" DEAD CHARGE ...\n");
1484 case TRICKLE_CHARGE: /* (0x02 << 4) */
1486 DBG(" TRICKLE CHARGE ...\n ");
1489 case CC_OR_CV: /* (0x03 << 4) */
1491 DBG(" CC or CV ...\n");
1494 case CHARGE_FINISH: /* (0x04 << 4) */
1495 ret = CHARGE_FINISH;
1496 DBG(" CHARGE FINISH ...\n");
1499 case USB_OVER_VOL: /* (0x05 << 4) */
1501 DBG(" USB OVER VOL ...\n");
1504 case BAT_TMP_ERR: /* (0x06 << 4) */
1506 DBG(" BAT TMP ERROR ...\n");
1509 case TIMER_ERR: /* (0x07 << 4) */
1511 DBG(" TIMER ERROR ...\n");
1514 case USB_EXIST: /* (1 << 1)// usb is exists */
1516 DBG(" USB EXIST ...\n");
1519 case USB_EFF: /* (1 << 0)// usb is effective */
1521 DBG(" USB EFF...\n");
1531 static void set_charge_current(struct battery_info *di, int charge_current)
1535 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1536 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1537 usb_ctrl_reg |= (charge_current);
1538 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1541 static void fg_match_param(struct battery_info *di, int chg_vol, int chg_ilim, int chg_cur)
1545 di->chg_v_lmt = CHRG_VOL4200;
1546 di->chg_i_lmt = ILIM_1750MA;
1547 di->chg_i_cur = CHRG_CUR1400mA;
1549 for (i=0; i<ARRAY_SIZE(CHG_V_LMT); i++){
1550 if (chg_vol < CHG_V_LMT[i])
1553 di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1556 for (i=0; i<ARRAY_SIZE(CHG_I_LMT); i++){
1557 if (chg_ilim < CHG_I_LMT[i])
1560 di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1563 for (i=0; i<ARRAY_SIZE(CHG_I_CUR); i++){
1564 if (chg_cur < CHG_I_CUR[i])
1567 di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1569 DBG("vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
1570 di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
1573 static void rk_battery_charger_init(struct battery_info *di)
1575 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1578 int chg_vol = di->rk818->battery_data->max_charger_voltagemV;
1579 int chg_cur = di->rk818->battery_data->max_charger_currentmA;
1580 int chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
1581 fg_match_param(di, chg_vol, chg_ilim, chg_cur);
1582 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1583 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1584 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1585 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1586 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1588 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1589 usb_ctrl_reg &= (~0x0f);
1590 #ifdef SUPPORT_USB_CHARGE
1591 usb_ctrl_reg |= (ILIM_450MA);
1593 usb_ctrl_reg |= (di->chg_i_lmt);
1595 chrg_ctrl_reg1 &= (0x00);
1596 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
1598 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1599 chrg_ctrl_reg2 &= ~(0xc0);
1600 chrg_ctrl_reg2 |= FINISH_100MA;
1602 sup_sts_reg &= ~(0x01 << 3);
1603 sup_sts_reg |= (0x01 << 2);
1605 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1606 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1607 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1608 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1609 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1611 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1612 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1613 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1614 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1616 DBG("%s end\n", __func__);
1619 void charge_disable_open_otg(int value)
1621 struct battery_info *di = g_battery;
1624 DBG("charge disable, enable OTG.\n");
1625 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1626 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1629 DBG("charge enable, disable OTG.\n");
1630 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1631 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1635 static void low_waring_init(struct battery_info *di)
1640 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1642 /* 2.8v~3.5v, interrupt */
1643 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1644 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1647 static void fg_init(struct battery_info *di)
1651 adc_ctrl_val = 0x30;
1652 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1655 /* get the volatege offset */
1656 _get_voltage_offset_value(di);
1657 rk_battery_charger_init(di);
1658 _set_relax_thres(di);
1659 /* get the current offset , the value write to the CAL_OFFSET */
1660 di->current_offset = _get_ioffset(di);
1661 _set_cal_offset(di, di->current_offset+42);
1663 _capacity_init(di, di->nac);
1665 di->remain_capacity = _get_realtime_capacity(di);
1666 di->current_avg = _get_average_current(di);
1668 low_waring_init(di);
1670 power_on_save(di, di->voltage_ocv);
1671 /* set sample time for cal_offset interval*/
1672 ioffset_sample_time(di, SAMP_TIME_8MIN);
1673 dump_gauge_register(di);
1674 dump_charger_register(di);
1677 "nac = %d , remain_capacity = %d\n"
1678 "OCV_voltage = %d, voltage = %d\n"
1679 "SOC = %d, fcc = %d\n",
1681 di->nac, di->remain_capacity,
1682 di->voltage_ocv, di->voltage,
1683 di->real_soc, di->fcc);
1687 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1688 static void zero_get_soc(struct battery_info *di)
1690 int dead_voltage, ocv_voltage;
1691 int temp_soc = -1, real_soc;
1692 int currentold, currentnow, voltage;
1699 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1701 currentold = _get_average_current(di);
1702 _get_cal_offset(di);
1705 currentnow = _get_average_current(di);
1707 } while ((currentold == currentnow) && (count_num < 11));
1710 for (i = 0; i < 10 ; i++)
1711 voltage += rk_battery_voltage(di);
1714 if (di->voltage_old == 0)
1715 di->voltage_old = voltage;
1716 voltage_k = voltage;
1717 voltage = (di->voltage_old*2 + 8*voltage)/10;
1718 di->voltage_old = voltage;
1719 currentnow = _get_average_current(di);
1721 dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
1722 /* 65 mo power-path mos */
1723 ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
1724 DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
1725 dead_voltage, ocv_voltage);
1727 _voltage_to_capacity(di, dead_voltage);
1728 di->q_dead = di->temp_nac;
1729 DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
1730 di->temp_soc, di->q_dead);
1732 _voltage_to_capacity(di, ocv_voltage);
1733 q_ocv = di->temp_nac;
1734 DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
1735 di->temp_soc, q_ocv);
1737 /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
1738 di->q_err = di->remain_capacity - q_ocv;
1739 DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
1740 di->q_err, di->remain_capacity, q_ocv);
1742 if (di->display_soc == 0)
1743 di->display_soc = di->real_soc*1000;
1744 real_soc = di->display_soc;
1746 DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
1747 di->remain_capacity, di->q_dead, di->q_err);
1748 /*[temp_nac]:dead_voltage*/
1749 if (q_ocv > di->q_dead) {
1750 DBG("first: q_ocv > di->q_dead\n");
1752 if (di->update_k == 0 || di->update_k >= 10) {
1753 if (di->update_k == 0) {
1755 /* ZQ = Q_ded + Qerr */
1756 /*[temp_nac]:dead_voltage*/
1757 di->q_shtd = di->q_dead + di->q_err;
1758 temp_soc = (di->remain_capacity - di->q_shtd)*
1763 di->line_k = (real_soc + temp_soc/2)
1766 DBG("[K >= 10].\n");
1767 temp_soc = ((di->remain_capacity - di->q_shtd)*
1768 1000 + di->fcc/2)/di->fcc; /* x1 10 */
1770 real_soc = (di->line_k*temp_soc); /*y1=k0*x1*/
1771 di->display_soc = real_soc;
1772 DBG("[K >= 10]. (temp_soc)X0 = %d\n", temp_soc);
1773 DBG("[K >= 10]. in:line_k = %d\n", di->line_k);
1774 DBG("[K >= 10]. (dis-soc)Y0=%d,real-soc=%d\n",
1775 di->display_soc, di->real_soc);
1777 if ((real_soc+500)/1000 < di->real_soc){
1779 di->odd_capacity = 0;
1781 else if (((real_soc+500))/1000 ==
1784 real_soc -= di->odd_capacity;
1785 if ((real_soc+500)/1000 <
1788 di->odd_capacity = 0;
1792 DBG("[k >= 10]. odd_capacity=%d\n",
1795 di->odd_capacity = 0;
1796 _voltage_to_capacity(di, dead_voltage);
1797 di->q_dead = di->temp_nac;
1798 di->q_shtd = di->q_dead + di->q_err;
1799 temp_soc = ((di->remain_capacity - di->q_shtd)*
1800 1000 + di->fcc/2)/di->fcc; /* z1 */
1804 di->line_k = (di->display_soc +
1805 temp_soc/2)/temp_soc;
1806 DBG("[K >= 10]. out:line_k = %d\n", di->line_k);
1812 else { /*update_k[1~9]*/
1817 temp_soc = ((di->remain_capacity - di->q_shtd)*
1818 1000 + di->fcc/2)/di->fcc;
1819 di->display_soc = di->line_k*temp_soc;
1820 DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
1821 DBG("[K1~9]. line_k = %d\n", di->line_k);
1822 DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
1823 di->display_soc, di->real_soc);
1824 if ((di->display_soc+500)/1000 < di->real_soc){
1826 di->odd_capacity = 0;
1828 else if ((real_soc+500)/1000 == di->real_soc) {
1830 real_soc -= di->odd_capacity;
1831 if ((real_soc+500)/1000 < di->real_soc) {
1833 di->odd_capacity = 0;
1835 di->odd_capacity += real_soc/3000+2;
1836 DBG("[K1~9]. odd_capacity=%d\n",
1839 di->odd_capacity = 0;
1842 DBG("second: q_ocv < di->q_dead\n");
1844 if ((di->voltage < 3400) && (di->real_soc > 10)) {
1845 /*di->real_soc = 10;*/
1847 } else if (di->voltage < 3400) {
1848 /*10 -(3.4-Vbat)*100*I*/
1849 if (di->current_avg < 1000)
1850 soc_time = 10-((3400-di->voltage)/10*
1851 abs32_int(di->current_avg))/1000;
1853 DBG("<%s>. ZERO: decrease sec = %d\n",
1854 __func__, soc_time/2);
1855 if (di->update_k > soc_time/2) {
1860 if (di->update_k > 10) {
1867 if (di->line_k <= 0) {
1869 DBG("ZERO: line_k <= 0, Update line_k!\n");
1872 DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
1873 DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
1874 DBG("ZERO: remain_cap - q_shtd=%d\n",
1875 (di->remain_capacity - di->q_shtd));
1876 DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
1877 di->line_k, di->display_soc, temp_soc);
1878 DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
1879 di->remain_capacity, di->q_shtd, di->q_err);
1880 DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
1881 di->warnning_voltage, _get_soc(di), di->real_soc);
1885 static int estimate_bat_ocv_vol(struct battery_info *di)
1887 return (di->voltage -
1888 (di->bat_res * di->current_avg) / 1000);
1891 static int estimate_bat_ocv_soc(struct battery_info *di)
1893 int ocv_soc, ocv_voltage;
1895 ocv_voltage = estimate_bat_ocv_vol(di);
1896 _voltage_to_capacity(di, ocv_voltage);
1897 ocv_soc = di->temp_soc;
1901 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1904 int now_current, soc_time = -1;
1907 voltage = di->voltage;
1908 now_current = di->current_avg;
1909 if (now_current == 0)
1911 soc_time = di->fcc*3600/100/(abs_int(now_current));
1912 _voltage_to_capacity(di, 3800);
1913 volt_to_soc = di->temp_soc;
1914 di->temp_soc = _get_soc(di);
1916 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
1917 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1918 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
1922 } else if (di->temp_soc == di->real_soc) {
1923 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1924 } else if (di->temp_soc > di->real_soc) {
1925 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1926 di->vol_smooth_time++;
1927 if (di->vol_smooth_time > soc_time*3) {
1929 di->vol_smooth_time = 0;
1933 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1934 if (di->real_soc == (di->temp_soc + 1)) {
1935 di->change_timer = di->soc_timer;
1936 di->real_soc = di->temp_soc;
1938 di->vol_smooth_time++;
1939 if (di->vol_smooth_time > soc_time/3) {
1941 di->vol_smooth_time = 0;
1946 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1947 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
1950 static int get_charging_time(struct battery_info *di)
1952 return (di->charging_time/60);
1955 static int get_discharging_time(struct battery_info *di)
1957 return (di->discharging_time/60);
1960 static int get_finish_time(struct battery_info *di)
1962 return (di->finish_time/60);
1965 static void dump_debug_info(struct battery_info *di)
1967 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
1968 u8 usb_ctrl_reg, chrg_ctrl_reg1;
1969 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
1971 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
1972 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
1973 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
1974 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
1975 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1976 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1977 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1978 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1979 battery_read(di->rk818, 0x00, &rtc_val, 1);
1981 DBG("\n------------- dump_debug_regs -----------------\n"
1982 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
1983 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
1984 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
1985 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
1986 ggcon_reg, ggsts_reg, rtc_val,
1987 sup_tst_reg, vb_mod_reg,
1988 usb_ctrl_reg, chrg_ctrl_reg1,
1989 chrg_ctrl_reg2, chrg_ctrl_reg3
1993 "########################## [read] ################################\n"
1994 "-----------------------------------------------------------------\n"
1995 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
1996 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
1997 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
1998 "diplay_soc = %d, cpapacity_soc = %d\n"
1999 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2000 "finish_real_soc = %d, finish_temp_soc = %d\n"
2001 "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2002 get_relax_voltage(di),
2003 di->voltage, di->current_avg,
2004 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2005 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2006 di->real_soc, _get_soc(di),
2007 di->ac_online, di->usb_online, di->status,
2008 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2009 get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2011 get_charge_status(di);
2012 DBG("################################################################\n");
2015 static void update_fcc_capacity(struct battery_info *di)
2017 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2018 if (get_level(di) >= di->dod0_level) {
2019 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
2020 if (di->fcc > di->qmax)
2023 _capacity_init(di, di->fcc);
2024 _save_FCC_capacity(di, di->fcc);
2026 di->dod0_status = 0;
2030 static void debug_get_finish_soc(struct battery_info *di)
2032 if (di->charge_status == CHARGE_FINISH) {
2033 di->debug_finish_real_soc = di->real_soc;
2034 di->debug_finish_temp_soc = di->temp_soc;
2038 static void wait_charge_finish_signal(struct battery_info *di)
2040 if (di->charge_status == CHARGE_FINISH)
2041 update_fcc_capacity(di);/* save new fcc*/
2044 debug_get_finish_soc(di);
2047 static void charge_finish_routine(struct battery_info *di)
2049 if (di->charge_status == CHARGE_FINISH) {
2050 _capacity_init(di, di->fcc);
2051 zero_current_calibration(di);
2053 if (di->real_soc < 100) {
2054 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
2055 if ((di->soc_counter < 80)) {
2058 di->soc_counter = 0;
2065 static void voltage_to_soc_charge_smooth(struct battery_info *di)
2067 int now_current, soc_time;
2070 now_current = _get_average_current(di);
2071 if (now_current == 0)
2073 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
2074 di->temp_soc = _get_soc(di);
2076 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2078 if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
2079 di->charge_smooth_time++;
2081 if (di->charge_smooth_time > soc_time/3) {
2083 di->charge_smooth_time = 0;
2085 di->charge_smooth_status = true;
2088 if (di->real_soc == di->temp_soc) {
2089 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2090 di->temp_soc = _get_soc(di);
2092 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2094 if (di->temp_soc < di->real_soc + 1) {
2095 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2096 di->charge_smooth_time++;
2097 if (di->charge_smooth_time > soc_time*2) {
2099 di->charge_smooth_time = 0;
2101 di->charge_smooth_status = true;
2104 else if (di->temp_soc > di->real_soc + 1) {
2105 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2106 di->charge_smooth_time++;
2107 if (di->charge_smooth_time > soc_time/3) {
2109 di->charge_smooth_time = 0;
2111 di->charge_smooth_status = true;
2113 } else if (di->temp_soc == di->real_soc + 1) {
2114 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
2115 if (di->charge_smooth_status) {
2116 di->charge_smooth_time++;
2117 if (di->charge_smooth_time > soc_time/3) {
2118 di->real_soc = di->temp_soc;
2119 di->charge_smooth_time = 0;
2120 di->charge_smooth_status = false;
2124 di->real_soc = di->temp_soc;
2125 di->charge_smooth_status = false;
2131 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2132 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2135 static void rk_battery_display_smooth(struct battery_info *di)
2140 status = di->status;
2141 charge_status = di->charge_status;
2142 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2144 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2145 voltage_to_soc_discharge_smooth(di);
2147 voltage_to_soc_charge_smooth(di);
2149 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2150 voltage_to_soc_discharge_smooth(di);
2151 if (di->real_soc == 1) {
2153 if (di->time2empty >= 300)
2163 static void software_recharge(struct battery_info *di, int max_cnt)
2165 static int recharge_cnt;
2168 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2169 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2170 chrg_ctrl_reg1 &= ~(1 << 7);
2171 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2172 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2173 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2175 chrg_ctrl_reg1 |= (1 << 7);
2176 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2177 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2178 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2186 static int estimate_battery_resister(struct battery_info *di)
2189 int avr_voltage1 = 0, avr_current1;
2190 int avr_voltage2 = 0, avr_current2;
2192 int bat_res, ocv_votage;
2193 static unsigned long last_time;
2194 unsigned long delta_time;
2195 int charge_ocv_voltage1, charge_ocv_voltage2;
2196 int charge_ocv_soc1, charge_ocv_soc2;
2198 delta_time = get_seconds() - last_time;
2199 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2200 if (delta_time >= 20) {/*20s*/
2203 set_charge_current(di, ILIM_450MA);/*450mA*/
2205 for (i = 0; i < 10 ; i++) {
2207 avr_voltage1 += rk_battery_voltage(di);
2210 avr_current1 = _get_average_current(di);
2211 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2212 DBG("------------------------------------------------------------------------------------------\n");
2213 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2216 set_charge_current(di, ILIM_3000MA);
2218 for (i = 0; i < 10 ; i++) {
2220 avr_voltage2 += rk_battery_voltage(di);
2223 avr_current2 = _get_average_current(di);
2224 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2225 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2227 /*calc resister and ocv_votage ocv*/
2228 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2229 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2230 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2232 /*calc sample voltage ocv*/
2233 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2234 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2235 _voltage_to_capacity(di, charge_ocv_voltage1);
2236 charge_ocv_soc1 = di->temp_soc;
2237 _voltage_to_capacity(di, charge_ocv_voltage2);
2238 charge_ocv_soc2 = di->temp_soc;
2240 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2241 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2242 DBG("------------------------------------------------------------------------------------------\n");
2243 last_time = get_seconds();
2253 static int update_battery_resister(struct battery_info *di)
2257 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2259 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2260 tmp_res = estimate_battery_resister(di);
2262 di->bat_res_update_cnt++;
2263 di->bat_res += tmp_res;
2264 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2265 if (di->bat_res_update_cnt == 10) {
2266 di->bat_res_updated = true;
2269 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2278 static void charge_soc_check_routine(struct battery_info *di)
2286 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2287 min = get_charging_time(di);
2288 update_battery_resister(di);
2290 if ((min >= 30) && (di->bat_res_updated)) {
2292 old_temp_soc = di->temp_soc;
2293 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2294 _voltage_to_capacity(di, ocv_voltage);
2295 ocv_temp_soc = di->temp_soc;
2297 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2298 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2299 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2301 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2302 di->temp_soc = ocv_temp_soc;
2304 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2306 remain_capcity = di->temp_soc * di->fcc / 100;
2307 _capacity_init(di, remain_capcity);
2308 di->remain_capacity = _get_realtime_capacity(di);
2309 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2317 static void update_resume_status_relax_voltage(struct battery_info *di)
2319 unsigned long sleep_soc;
2320 unsigned long sum_sleep_soc;
2321 unsigned long sleep_sec;
2326 int sum_sleep_avr_current;
2330 update_battery_info(di);
2332 di->sys_wakeup = true;
2334 DBG("<%s>, resume----------checkstart\n", __func__);
2335 sleep_sec = get_seconds() - di->suspend_time_start;
2336 sleep_min = sleep_sec / 60;
2338 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2339 __func__, sleep_sec, sleep_min);
2341 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2342 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2344 delta_capacity = di->suspend_capacity - di->remain_capacity;
2345 delta_soc = di->suspend_temp_soc - _get_soc(di);
2346 di->dischrg_sum_sleep_capacity += delta_capacity;
2347 di->dischrg_sum_sleep_sec += sleep_sec;
2349 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2350 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2352 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2353 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2354 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2355 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2356 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2357 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2358 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2360 /*large suspend current*/
2361 if (sum_sleep_avr_current > 20) {
2362 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2363 di->real_soc -= sum_sleep_soc;
2364 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2365 __func__, sum_sleep_soc, di->real_soc);
2367 /* small suspend current*/
2368 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2370 relax_voltage = get_relax_voltage(di);
2371 di->voltage = rk_battery_voltage(di);
2373 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2374 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2375 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2378 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2379 if (sum_sleep_soc > 0)
2380 di->real_soc -= sum_sleep_soc;
2384 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2385 di->dischrg_sum_sleep_capacity = 0;
2386 di->dischrg_sum_sleep_sec = 0;
2388 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2389 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2392 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2393 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2394 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2395 di->temp_soc = _get_soc(di);
2396 charge_status = get_charge_status(di);
2398 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2399 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2400 DBG("resume, sleep : ac online charge current < 1000\n");
2401 if (sleep_sec > 0) {
2402 di->count_sleep_time += sleep_sec;
2403 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2404 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2406 di->count_sleep_time = 0;
2407 di->real_soc += sleep_soc;
2408 if (di->real_soc > 100)
2413 DBG("<%s>, usb charging\n", __func__);
2414 if (di->suspend_temp_soc + 15 < di->temp_soc)
2415 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2417 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2420 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);
2427 #ifdef SUPPORT_USB_CHARGE
2428 static int get_charging_status_type(struct battery_info *di)
2430 int otg_status = dwc_otg_check_dpdm();
2432 if (0 == otg_status) {
2435 di->check_count = 0;
2437 } else if (1 == otg_status) {
2438 if (0 == get_gadget_connect_flag()) {
2439 if (++di->check_count >= 5) {
2451 } else if (2 == otg_status) {
2454 di->check_count = 0;
2457 if (di->ac_online == 1)
2458 set_charge_current(di, di->chg_i_lmt);
2460 set_charge_current(di, ILIM_450MA);
2466 static void battery_poweron_status_init(struct battery_info *di)
2470 #ifndef SUPPORT_USB_CHARGE
2474 #ifdef SUPPORT_USB_CHARGE
2476 otg_status = dwc_otg_check_dpdm();
2477 if (otg_status == 1) {
2480 set_charge_current(di, ILIM_450MA);
2481 di->status = POWER_SUPPLY_STATUS_CHARGING;
2482 DBG("++++++++ILIM_450MA++++++\n");
2484 } else if (otg_status == 2) {
2487 di->status = POWER_SUPPLY_STATUS_CHARGING;
2488 set_charge_current(di, di->chg_i_lmt);
2489 DBG("++++++++ILIM_1000MA++++++\n");
2491 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2495 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2496 if (buf&PLUG_IN_STS) {
2499 di->status = POWER_SUPPLY_STATUS_CHARGING;
2500 if (di->real_soc == 100)
2501 di->status = POWER_SUPPLY_STATUS_FULL;
2503 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2507 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2512 static void check_battery_status(struct battery_info *di)
2517 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2518 #ifdef SUPPORT_USB_CHARGE
2520 if (strstr(saved_command_line, "charger")) {
2521 if ((buf&PLUG_IN_STS) == 0) {
2522 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2528 if (buf&PLUG_IN_STS) {
2529 get_charging_status_type(di);
2531 di->status = POWER_SUPPLY_STATUS_CHARGING;
2532 if (di->real_soc == 100)
2533 di->status = POWER_SUPPLY_STATUS_FULL;
2535 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2542 if (buf & PLUG_IN_STS) {
2545 di->status = POWER_SUPPLY_STATUS_CHARGING;
2546 if (di->real_soc == 100)
2547 di->status = POWER_SUPPLY_STATUS_FULL;
2549 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2556 static void report_power_supply_changed(struct battery_info *di)
2559 static u32 old_ac_status;
2560 static u32 old_usb_status;
2561 static u32 old_charge_status;
2564 state_changed = false;
2565 if (di->real_soc == 0)
2566 state_changed = true;
2567 else if (di->real_soc == 100)
2568 state_changed = true;
2569 else if (di->real_soc != old_soc)
2570 state_changed = true;
2571 else if (di->ac_online != old_ac_status)
2572 state_changed = true;
2573 else if (di->usb_online != old_usb_status)
2574 state_changed = true;
2575 else if (old_charge_status != di->status)
2576 state_changed = true;
2578 if (state_changed) {
2579 power_supply_changed(&di->bat);
2580 power_supply_changed(&di->usb);
2581 power_supply_changed(&di->ac);
2582 old_soc = di->real_soc;
2583 old_ac_status = di->ac_online;
2584 old_usb_status = di->usb_online;
2585 old_charge_status = di->status;
2589 static void upd_time_table(struct battery_info *di)
2592 static int old_index = 0;
2593 static int old_min = 0;
2595 int mod = di->real_soc % 10;
2596 int index = di->real_soc / 10;
2598 if (di->ac_online || di->usb_online)
2599 time = di->charge_min;
2601 time = di->discharge_min;
2603 if ((mod == 0) && (index > 0) && (old_index != index)) {
2604 di->chrg_min[index-1] = time - old_min;
2609 for (i=1; i<11; i++)
2610 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
2615 static void update_battery_info(struct battery_info *di)
2617 di->remain_capacity = _get_realtime_capacity(di);
2618 if (di->remain_capacity > di->fcc)
2619 _capacity_init(di, di->fcc);
2621 if (di->real_soc > 100)
2623 else if (di->real_soc < 0)
2626 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2627 di->charging_time++;
2628 di->discharging_time = 0;
2630 di->charging_time = 0;
2631 if (di->voltage < 3800)
2632 di->discharging_time += 2;
2634 di->discharging_time++;
2636 if (di->charge_status == CHARGE_FINISH)
2639 di->finish_time = 0;
2641 di->charge_min = get_charging_time(di);
2642 di->discharge_min = get_discharging_time(di);
2643 di->finish_min = get_finish_time(di);
2646 di->est_ocv_vol = estimate_bat_ocv_vol(di);
2647 di->est_ocv_soc = estimate_bat_ocv_soc(di);
2648 di->voltage = rk_battery_voltage(di);
2649 di->current_avg = _get_average_current(di);
2650 di->remain_capacity = _get_realtime_capacity(di);
2651 di->voltage_ocv = _get_OCV_voltage(di);
2652 di->charge_status = get_charge_status(di);
2653 di->otg_status = dwc_otg_check_dpdm();
2654 di->relax_voltage = get_relax_voltage(di);
2655 di->temp_soc = _get_soc(di);
2656 check_battery_status(di);/* ac_online, usb_online, status*/
2657 update_cal_offset(di);
2661 static void rk_battery_work(struct work_struct *work)
2663 struct battery_info *di = container_of(work,
2664 struct battery_info, battery_monitor_work.work);
2666 update_resume_status_relax_voltage(di);
2667 wait_charge_finish_signal(di);
2668 charge_finish_routine(di);
2670 rk_battery_display_smooth(di);
2671 update_battery_info(di);
2673 report_power_supply_changed(di);
2674 _copy_soc(di, di->real_soc);
2675 _save_remain_capacity(di, di->remain_capacity);
2677 dump_debug_info(di);
2678 di->queue_work_cnt++;
2679 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2682 static void rk_battery_charge_check_work(struct work_struct *work)
2684 struct battery_info *di = container_of(work,
2685 struct battery_info, charge_check_work.work);
2687 DBG("rk_battery_charge_check_work\n");
2688 charge_disable_open_otg(di->charge_otg);
2691 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2693 int register_battery_notifier(struct notifier_block *nb)
2695 return blocking_notifier_chain_register(&battery_chain_head, nb);
2697 EXPORT_SYMBOL_GPL(register_battery_notifier);
2699 int unregister_battery_notifier(struct notifier_block *nb)
2701 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2703 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2705 int battery_notifier_call_chain(unsigned long val)
2707 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2708 == NOTIFY_BAD) ? -EINVAL : 0;
2710 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2712 static void poweron_lowerpoer_handle(struct battery_info *di)
2714 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2715 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2717 /* kernel_power_off(); */
2722 static int battery_notifier_call(struct notifier_block *nb,
2723 unsigned long event, void *data)
2725 struct battery_info *di =
2726 container_of(nb, struct battery_info, battery_nb);
2730 DBG(" CHARGE enable\n");
2732 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2737 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2738 DBG("charge disable OTG enable\n");
2742 poweron_lowerpoer_handle(di);
2751 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2753 pr_info("<%s>lower power warning!\n", __func__);
2755 _copy_soc(g_battery, 0);
2756 _capacity_init(g_battery, 0);
2757 rk_send_wakeup_key();
2762 static void disable_vbat_low_irq(struct battery_info *di)
2765 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2766 /*clr vbat low interrupt */
2767 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2769 static void enable_vbat_low_irq(struct battery_info *di)
2771 /* clr vbat low interrupt */
2772 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2774 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2777 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2779 pr_info("\n------- %s:irq = %d\n", __func__, irq);
2780 rk_send_wakeup_key();
2783 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
2785 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2786 charge_disable_open_otg(0);
2787 rk_send_wakeup_key();
2791 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
2793 pr_info("---------- %s:irq = %d\n", __func__, irq);
2794 rk_send_wakeup_key();
2800 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2804 struct kobject *rk818_fg_kobj;
2806 ret = create_sysfs_interfaces(dev);
2809 dev_err(dev, "device RK818 battery sysfs register failed\n");
2813 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2816 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2817 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2819 dev_err(dev, "create rk818_battery node error\n");
2827 power_supply_unregister(&di->ac);
2828 power_supply_unregister(&di->usb);
2829 power_supply_unregister(&di->bat);
2834 static void rk818_battery_irq_init(struct battery_info *di)
2836 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2838 struct rk818 *chip = di->rk818;
2840 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
2841 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
2842 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
2843 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
2845 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2846 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2848 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2850 di->irq = vb_lo_irq;
2851 enable_irq_wake(di->irq);
2852 disable_vbat_low_irq(di);
2854 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2855 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2857 dev_err(chip->dev, "plug_in_irq request failed!\n");
2860 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2861 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2863 dev_err(chip->dev, "plug_out_irq request failed!\n");
2866 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2867 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2869 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2872 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2878 di->platform_data = chip->battery_data;
2879 di->cell.config = di->platform_data->cell_cfg;
2880 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
2881 di->qmax = di->platform_data->cell_cfg->design_qmax;
2882 di->fcc = di->design_capacity;
2883 di->vol_smooth_time = 0;
2884 di->charge_smooth_time = 0;
2885 di->charge_smooth_status = false;
2886 di->sleep_status = 0;
2888 di->sys_wakeup = true;
2889 di->pcb_ioffset = 0;
2890 di->pcb_ioffset_updated = false;
2891 di->queue_work_cnt = 0;
2893 di->voltage_old = 0;
2894 di->display_soc = 0;
2896 di->bat_res_updated = false;
2898 di->sys_wakeup = true;
2899 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2902 di->discharge_min = 0;
2903 di->charging_time = 0;
2904 di->discharging_time = 0;
2905 di->finish_time = 0;
2909 di->odd_capacity = 0;
2910 di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
2911 for (i=0; i<10; i++)
2912 di->chrg_min[i] = -1;
2914 di->debug_finish_real_soc = 0;
2915 di->debug_finish_temp_soc = 0;
2917 fcc_capacity = _get_FCC_capacity(di);
2918 if (fcc_capacity > 1000)
2919 di->fcc = fcc_capacity;
2921 di->fcc = di->design_capacity;
2924 static struct of_device_id rk818_battery_of_match[] = {
2925 { .compatible = "rk818_battery" },
2929 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2932 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2934 struct device_node *regs, *rk818_pmic_np;
2935 struct battery_platform_data *data;
2936 struct cell_config *cell_cfg;
2937 struct ocv_config *ocv_cfg;
2938 struct property *prop;
2942 rk818_pmic_np = of_node_get(rk818->dev->of_node);
2943 if (!rk818_pmic_np) {
2944 dev_err(dev, "could not find pmic sub-node\n");
2948 regs = of_find_node_by_name(rk818_pmic_np, "battery");
2950 dev_err(dev, "battery node not found!\n");
2954 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2956 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2960 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2962 dev_err(dev, "kzalloc for cell_config failed!\n");
2965 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2967 dev_err(dev, "kzalloc for ocv_config failed!\n");
2971 prop = of_find_property(regs, "ocv_table", &length);
2973 dev_err(dev, "ocv_table not found!\n");
2976 data->ocv_size = length / sizeof(u32);
2978 if (data->ocv_size > 0) {
2979 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2981 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
2982 if (!data->battery_ocv) {
2983 dev_err(dev, "kzalloc for ocv_table failed!\n");
2986 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2991 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2993 dev_err(dev, "max_charge_currentmA not found!\n");
2994 out_value = DEFAULT_ICUR;
2996 data->max_charger_currentmA = out_value;
2998 ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3000 dev_err(dev, "max_charger_ilimitmA not found!\n");
3001 out_value = DEFAULT_ILMT;
3003 data->max_charger_ilimitmA = out_value;
3005 ret = of_property_read_u32(regs, "bat_res", &out_value);
3007 dev_err(dev, "bat_res not found!\n");
3008 out_value = DEFAULT_BAT_RES;
3010 data->sense_resistor_mohm = out_value;
3012 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3014 dev_err(dev, "max_charge_voltagemV not found!\n");
3015 out_value = DEFAULT_VLMT;
3017 data->max_charger_voltagemV = out_value;
3019 ret = of_property_read_u32(regs, "design_capacity", &out_value);
3021 dev_err(dev, "design_capacity not found!\n");
3024 cell_cfg->design_capacity = out_value;
3026 ret = of_property_read_u32(regs, "design_qmax", &out_value);
3028 dev_err(dev, "design_qmax not found!\n");
3031 cell_cfg->design_qmax = out_value;
3033 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3035 dev_err(dev, "sleep_enter_current not found!\n");
3038 ocv_cfg->sleep_enter_current = out_value;
3040 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3042 dev_err(dev, "sleep_exit_current not found!\n");
3045 ocv_cfg->sleep_exit_current = out_value;
3047 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3049 cell_cfg->ocv = ocv_cfg;
3050 data->cell_cfg = cell_cfg;
3051 rk818->battery_data = data;
3053 DBG("\n--------- the battery OCV TABLE dump:\n");
3054 DBG("bat_res :%d\n", data->sense_resistor_mohm);
3055 DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3056 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3057 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3058 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3059 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3060 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3061 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3062 DBG("uboot chrg = %d\n", support_uboot_chrg);
3063 DBG("\n--------- rk818_battery dt_parse ok.\n");
3068 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3075 static int battery_probe(struct platform_device *pdev)
3077 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3078 struct battery_info *di;
3081 DBG("battery driver version %s\n", DRIVER_VERSION);
3082 di = kzalloc(sizeof(*di), GFP_KERNEL);
3084 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
3087 ret = rk_battery_parse_dt(chip, &pdev->dev);
3089 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
3093 platform_set_drvdata(pdev, di);
3094 battery_info_init(di, chip);
3095 if (!is_bat_exist(di)) {
3096 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
3101 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
3103 flatzone_voltage_init(di);
3104 battery_poweron_status_init(di);
3105 battery_power_supply_init(di);
3106 ret = battery_power_supply_register(di, &pdev->dev);
3108 dev_err(&pdev->dev, "rk power supply register failed!\n");
3111 di->wq = create_singlethread_workqueue("battery-work");
3112 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
3113 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
3114 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
3116 di->battery_nb.notifier_call = battery_notifier_call;
3117 register_battery_notifier(&di->battery_nb);
3119 rk818_battery_irq_init(di);
3120 rk818_battery_sysfs_init(di, &pdev->dev);
3121 DBG("------ RK81x battery_probe ok!-------\n");
3128 static int battery_suspend(struct platform_device *dev, pm_message_t state)
3130 struct battery_info *di = platform_get_drvdata(dev);
3132 enable_vbat_low_irq(di);
3133 di->sleep_status = di->status;
3134 di->suspend_charge_current = _get_average_current(di);
3136 /* avoid abrupt wakeup which will clean the variable*/
3137 if (di->sys_wakeup) {
3138 di->suspend_capacity = di->remain_capacity;
3139 di->suspend_temp_soc = _get_soc(di);
3140 di->suspend_time_start = get_seconds();
3141 di->sys_wakeup = false;
3144 cancel_delayed_work(&di->battery_monitor_work);
3145 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
3146 __func__, di->suspend_temp_soc, di->suspend_charge_current,
3147 di->suspend_capacity, di->sleep_status);
3152 static int battery_resume(struct platform_device *dev)
3154 struct battery_info *di = platform_get_drvdata(dev);
3157 DBG("<%s>\n", __func__);
3158 disable_vbat_low_irq(di);
3159 queue_delayed_work(di->wq, &di->battery_monitor_work,
3160 msecs_to_jiffies(TIMER_MS_COUNTS/2));
3162 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3164 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3168 static int battery_remove(struct platform_device *dev)
3170 struct battery_info *di = platform_get_drvdata(dev);
3172 cancel_delayed_work_sync(&di->battery_monitor_work);
3175 static void battery_shutdown(struct platform_device *dev)
3177 struct battery_info *di = platform_get_drvdata(dev);
3179 cancel_delayed_work_sync(&di->battery_monitor_work);
3180 DBG("rk818 shutdown!");
3184 static struct platform_driver battery_driver = {
3186 .name = "rk818-battery",
3187 .owner = THIS_MODULE,
3190 .probe = battery_probe,
3191 .remove = battery_remove,
3192 .suspend = battery_suspend,
3193 .resume = battery_resume,
3194 .shutdown = battery_shutdown,
3197 static int __init battery_init(void)
3199 return platform_driver_register(&battery_driver);
3202 fs_initcall_sync(battery_init);
3203 static void __exit battery_exit(void)
3205 platform_driver_unregister(&battery_driver);
3207 module_exit(battery_exit);
3209 MODULE_LICENSE("GPL");
3210 MODULE_ALIAS("platform:rk818-battery");
3211 MODULE_AUTHOR("ROCKCHIP");