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 DSOC_DISCHRG_FAST_DEC_SEC 120 /*seconds*/
49 #define DSOC_DISCHRG_FAST_EER_RANGE 25
50 #define DSOC_CHRG_FAST_CALIB_CURR_MAX 400 /*mA*/
51 #define DSOC_CHRG_FAST_INC_SEC 120 /*seconds*/
52 #define DSOC_CHRG_FAST_EER_RANGE 25
53 #define DSOC_CHRG_EMU_CURR 1000
54 #define DSOC_CHG_TERM_CURR 500
56 /*realtime RSOC calib param*/
57 #define RSOC_DISCHG_ERR_LOWER 40
58 #define RSOC_DISCHG_ERR_UPPER 50
59 #define RSOC_ERR_CHCK_CNT 15
60 #define RSOC_COMPS 20 /*compensation*/
61 #define RSOC_CALIB_CURR_MAX 900 /*mA*/
62 #define RSOC_CALIB_DISCHGR_TIME 3 /*min*/
64 #define INTERPOLATE_MAX 1000
65 #define MAX_INT 0x7FFF
66 #define TIME_10MIN_SEC 600
68 #define CHG_VOL_SHIFT 4
69 #define CHG_ILIM_SHIFT 0
70 #define CHG_ICUR_SHIFT 0
72 int CHG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
73 int CHG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000};
74 int CHG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000};
77 struct cell_state cell;
78 struct power_supply bat;
79 struct power_supply ac;
80 struct power_supply usb;
81 struct delayed_work work;
82 /* struct i2c_client *client; */
85 struct battery_platform_data *platform_data;
97 uint16_t relax_voltage;
101 bool pcb_ioffset_updated;
102 unsigned long queue_work_cnt;
106 uint16_t warnning_voltage;
124 int bat_res_update_cnt;
131 unsigned long dod0_time;
142 int current_k;/* (ICALIB0, ICALIB1) */
145 int voltage_k;/* VCALIB0 VCALIB1 */
158 struct timeval soc_timer;
159 struct timeval change_timer;
162 int charge_smooth_time;
164 int suspend_capacity;
166 struct timespec suspend_time;
167 struct timespec resume_time;
168 unsigned long suspend_time_start;
169 unsigned long count_sleep_time;
171 unsigned long dischrg_sum_sleep_sec;
172 unsigned long dischrg_sum_sleep_capacity;
173 int suspend_temp_soc;
175 int suspend_charge_current;
178 bool bat_res_updated;
179 bool charge_smooth_status;
181 unsigned long last_plugin_time;
184 unsigned long charging_time;
185 unsigned long discharging_time;
186 unsigned long finish_time;
191 struct notifier_block battery_nb;
192 struct workqueue_struct *wq;
193 struct delayed_work battery_monitor_work;
194 struct delayed_work charge_check_work;
197 struct wake_lock resume_wake_lock;
199 int debug_finish_real_soc;
200 int debug_finish_temp_soc;
207 struct battery_info *g_battery;
208 u32 support_uboot_chrg;
210 extern int dwc_vbus_status(void);
211 extern int get_gadget_connect_flag(void);
212 extern int dwc_otg_check_dpdm(void);
213 extern void kernel_power_off(void);
214 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
215 extern unsigned int irq_create_mapping(struct irq_domain *domain,
216 irq_hw_number_t hwirq);
217 extern void rk_send_wakeup_key(void);
218 static void update_battery_info(struct battery_info *di);
220 #define SUPPORT_USB_CHARGE
223 static u32 interpolate(int value, u32 *table, int size)
228 for (i = 0; i < size; i++) {
229 if (value < table[i])
233 if ((i > 0) && (i < size)) {
234 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
235 d /= table[i] - table[i-1];
236 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
238 d = i * ((INTERPOLATE_MAX+size/2)/size);
246 /* Returns (a * b) / c */
247 static int32_t ab_div_c(u32 a, u32 b, u32 c)
253 sign = ((((a^b)^c) & 0x80000000) != 0);
259 tmp = ((int32_t) a*b + (c>>1)) / c;
271 static int32_t abs_int(int32_t x)
273 return (x > 0) ? x : -x;
276 static int abs32_int(int x)
278 return (x > 0) ? x : -x;
282 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
286 ret = rk818_i2c_read(rk818, reg, len, buf);
290 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
293 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
296 static void dump_gauge_register(struct battery_info *di)
300 DBG("%s dump charger register start: \n", __func__);
301 for (i = 0xAC; i < 0xDF; i++) {
302 battery_read(di->rk818, i, &buf, 1);
303 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
308 static void dump_charger_register(struct battery_info *di)
313 DBG("%s dump the register start: \n", __func__);
314 for (i = 0x99; i < 0xAB; i++) {
315 battery_read(di->rk818, i, &buf, 1);
316 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
324 static uint16_t _get_OCV_voltage(struct battery_info *di);
325 static int _voltage_to_capacity(struct battery_info *di, int voltage);
326 static int _get_realtime_capacity(struct battery_info *di);
327 static void power_on_save(struct battery_info *di, int voltage);
328 static void _capacity_init(struct battery_info *di, u32 capacity);
329 static void battery_poweron_status_init(struct battery_info *di);
330 static void flatzone_voltage_init(struct battery_info *di);
331 static int _get_FCC_capacity(struct battery_info *di);
332 static void _save_FCC_capacity(struct battery_info *di, u32 capacity);
333 static int _get_soc(struct battery_info *di);
334 static int _get_average_current(struct battery_info *di);
335 static int rk_battery_voltage(struct battery_info *di);
336 static uint16_t _get_relax_vol1(struct battery_info *di);
337 static uint16_t _get_relax_vol2(struct battery_info *di);
338 static void update_battery_info(struct battery_info *di);
340 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
342 struct battery_info *di = g_battery;
348 battery_read(di->rk818, SUP_STS_REG, &status, 1);
349 battery_read(di->rk818, SOC_REG, &soc_reg, 1);
350 battery_read(di->rk818, 0x00, &rtc_val, 1);
351 di->voltage_ocv = _get_OCV_voltage(di);
352 _voltage_to_capacity(di, di->voltage_ocv);
353 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
355 return sprintf(buf, "-----------------------------------------------------------------------------\n"
356 "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
357 "real_soc = %d, temp_soc = %d\n"
358 "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
359 "usb_online = %d, ac_online = %d\n"
360 "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
361 "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
362 "relax_volt1 = %d, relax_volt2 = %d\n"
363 "---------------------------------------------------------------------------\n",
364 rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
365 di->real_soc, _get_soc(di),
366 di->fcc, _get_FCC_capacity(di), shtd_time,
367 di->usb_online, di->ac_online,
369 di->voltage_k, di->voltage_b, soc_reg,
370 _get_relax_vol1(di), _get_relax_vol2(di));
373 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
375 struct battery_info *di = g_battery;
376 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
377 u8 usb_ctrl_reg, chrg_ctrl_reg1;
378 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
380 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
381 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
382 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
383 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
384 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
385 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
386 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
387 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
388 battery_read(di->rk818, 0x00, &rtc_val, 1);
390 return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
391 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
392 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
393 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
394 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
395 "---------------------------------------------------------------------------\n",
396 ggcon_reg, ggsts_reg, rtc_val,
397 sup_tst_reg, vb_mod_reg,
398 usb_ctrl_reg, chrg_ctrl_reg1,
399 chrg_ctrl_reg2, chrg_ctrl_reg3
402 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
404 struct battery_info *di = g_battery;
406 return sprintf(buf, "%d", di->fcc);
408 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
410 struct battery_info *di = g_battery;
412 return sprintf(buf, "%d", di->real_soc);
415 static ssize_t bat_soc_write(struct device *dev,
416 struct device_attribute *attr,
417 const char *buf, size_t count)
421 struct battery_info *di = g_battery;
423 ret = sscanf(buf, "%d", &val);
428 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
430 struct battery_info *di = g_battery;
432 return sprintf(buf, "%d", di->temp_soc);
435 static ssize_t bat_temp_soc_write(struct device *dev,
436 struct device_attribute *attr,
437 const char *buf, size_t count)
442 struct battery_info *di = g_battery;
444 ret = sscanf(buf, "%d", &val);
445 capacity = di->fcc*val/100;
446 _capacity_init(di, capacity);
447 di->temp_soc = _get_soc(di);
448 di->remain_capacity = _get_realtime_capacity(di);
453 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
455 struct battery_info *di = g_battery;
457 return sprintf(buf, "%d", di->voltage);
460 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
462 struct battery_info *di = g_battery;
464 return sprintf(buf, "%d", di->current_avg);
467 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
469 struct battery_info *di = g_battery;
471 return sprintf(buf, "%d", di->remain_capacity);
474 static struct device_attribute rk818_bat_attr[] = {
475 __ATTR(state, 0664, bat_state_read, NULL),
476 __ATTR(regs, 0664, bat_reg_read, NULL),
477 __ATTR(fcc, 0664, bat_fcc_read, NULL),
478 __ATTR(soc, 0664, bat_soc_read, bat_soc_write),
479 __ATTR(temp_soc, 0664, bat_temp_soc_read, bat_temp_soc_write),
480 __ATTR(voltage, 0664, bat_voltage_read, NULL),
481 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
482 __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
487 static uint16_t get_relax_voltage(struct battery_info *di);
489 static ssize_t show_state_attrs(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct battery_info *data = g_battery;
494 if (0 == get_relax_voltage(data)) {
496 "voltage = %d, remain_capacity = %d, status = %d\n",
497 data->voltage, data->remain_capacity,
502 "voltage = %d, remain_capacity = %d, status = %d\n",
503 get_relax_voltage(data), data->remain_capacity,
507 static ssize_t restore_state_attrs(struct device *dev,
508 struct device_attribute *attr, const char *buf, size_t size)
512 static struct device_attribute rkbatt_attrs[] = {
513 __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
516 static int create_sysfs_interfaces(struct device *dev)
520 for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++) {
521 if (device_create_file(dev, rkbatt_attrs + liTmep))
528 for (; liTmep >= 0; liTmep--)
529 device_remove_file(dev, rkbatt_attrs + liTmep);
531 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
535 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
539 battery_read(di->rk818, reg, &val, 1);
540 DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
545 static int _gauge_enable(struct battery_info *di)
550 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
552 dev_err(di->dev, "error reading TS_CTRL_REG");
555 if (!(buf & GG_EN)) {
557 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1); /* enable */
558 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
562 DBG("%s, %d\n", __func__, buf);
566 static void save_level(struct battery_info *di, u8 save_soc)
571 battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
573 static u8 get_level(struct battery_info *di)
577 battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
581 static int _get_vcalib0(struct battery_info *di)
587 ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
589 ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
592 DBG("%s voltage0 offset vale is %d\n", __func__, temp);
596 static int _get_vcalib1(struct battery_info *di)
602 ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
604 ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
607 DBG("%s voltage1 offset vale is %d\n", __func__, temp);
611 static int _get_ioffset(struct battery_info *di)
618 ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
620 ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
626 static uint16_t _get_cal_offset(struct battery_info *di)
632 ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
634 ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
639 static int _set_cal_offset(struct battery_info *di, u32 value)
645 ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
646 buf = (value >> 8)&0xff;
647 ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
651 static void _get_voltage_offset_value(struct battery_info *di)
653 int vcalib0, vcalib1;
655 vcalib0 = _get_vcalib0(di);
656 vcalib1 = _get_vcalib1(di);
658 di->voltage_k = (4200 - 3000)*1000/(vcalib1 - vcalib0);
659 di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
660 DBG("voltage_k = %d(x1000) voltage_b = %d\n", di->voltage_k, di->voltage_b);
662 static uint16_t _get_OCV_voltage(struct battery_info *di)
667 uint16_t voltage_now = 0;
669 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
671 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
675 dev_err(di->dev, "error read BAT_OCV_REGH");
679 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
684 static int rk_battery_voltage(struct battery_info *di)
691 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
693 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
697 dev_err(di->dev, "error read BAT_VOL_REGH");
701 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
707 * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
708 * based on the voltage.
710 static int _voltage_to_capacity(struct battery_info *di, int voltage)
716 ocv_table = di->platform_data->battery_ocv;
717 ocv_size = di->platform_data->ocv_size;
718 di->warnning_voltage = ocv_table[3];
719 tmp = interpolate(voltage, ocv_table, ocv_size);
720 di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
721 di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
726 static uint16_t _get_relax_vol1(struct battery_info *di)
730 uint16_t temp = 0, voltage_now;
732 ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
734 ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
737 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
742 static uint16_t _get_relax_vol2(struct battery_info *di)
745 uint16_t temp = 0, voltage_now;
748 ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
750 ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
753 voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
758 static int _get_raw_adc_current(struct battery_info *di)
764 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
766 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
770 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
772 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
775 current_now |= (buf<<8);
778 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
786 static void reset_zero_var(struct battery_info *di)
794 static void ioffset_sample_time(struct battery_info *di, int time)
798 battery_read(di->rk818, GGCON, &ggcon, 1);
799 ggcon &= ~(0x30); /*clear <5:4>*/
801 battery_write(di->rk818, GGCON, &ggcon, 1);
802 debug_reg(di, GGCON, "GGCON");
805 static void update_cal_offset(struct battery_info *di)
807 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
809 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
810 if ((!mod) && (di->pcb_ioffset_updated)) {
811 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
812 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
817 static void zero_current_calibration(struct battery_info *di)
826 if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
828 for (retry = 0; retry < 5; retry++) {
829 adc_value = _get_raw_adc_current(di);
830 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
832 C0 = _get_cal_offset(di);
834 _set_cal_offset(di, C1);
835 DBG("<%s>. C1 = %d\n", __func__, C1);
838 adc_value = _get_raw_adc_current(di);
839 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
842 ioffset = _get_ioffset(di);
843 pcb_offset = C1 - ioffset;
844 di->pcb_ioffset = pcb_offset;
845 di->pcb_ioffset_updated = true;
846 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
849 di->pcb_ioffset_updated = false;
855 static bool _is_relax_mode(struct battery_info *di)
860 ret = battery_read(di->rk818, GGSTS, &status, 1);
862 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
868 static uint16_t get_relax_voltage(struct battery_info *di)
872 uint16_t relax_vol1, relax_vol2;
875 ret = battery_read(di->rk818, GGSTS, &status, 1);
876 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
878 relax_vol1 = _get_relax_vol1(di);
879 relax_vol2 = _get_relax_vol2(di);
880 DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
881 if (_is_relax_mode(di))
882 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
887 static void _set_relax_thres(struct battery_info *di)
890 int enter_thres, exit_thres;
891 struct cell_state *cell = &di->cell;
893 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
894 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
896 buf = enter_thres&0xff;
897 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
898 buf = (enter_thres>>8)&0xff;
899 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
901 buf = exit_thres&0xff;
902 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
903 buf = (exit_thres>>8)&0xff;
904 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
906 /* set sample time */
907 battery_read(di->rk818, GGCON, &buf, 1);
908 buf &= ~(3<<2);/*8min*/
909 buf &= ~0x01; /* clear bat_res calc*/
910 battery_write(di->rk818, GGCON, &buf, 1);
913 static void restart_relax(struct battery_info *di)
915 u8 ggcon;/* chrg_ctrl_reg2;*/
918 battery_read(di->rk818, GGCON, &ggcon, 1);
920 battery_write(di->rk818, GGCON, &ggcon, 1);
922 battery_read(di->rk818, GGSTS, &ggsts, 1);
924 battery_write(di->rk818, GGSTS, &ggsts, 1);
927 static int _get_average_current(struct battery_info *di)
934 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
936 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
940 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
942 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
945 current_now |= (buf<<8);
947 if (current_now & 0x800)
950 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
956 static bool is_bat_exist(struct battery_info *di)
960 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
961 return (buf & 0x80) ? true : false;
964 static bool _is_first_poweron(struct battery_info *di)
969 battery_read(di->rk818, GGSTS, &buf, 1);
970 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
971 /*di->pwron_bat_con = buf;*/
975 battery_write(di->rk818, GGSTS, &buf, 1);
976 battery_read(di->rk818, GGSTS, &temp, 1);
977 } while (temp&BAT_CON);
982 static void flatzone_voltage_init(struct battery_info *di)
989 ocv_table = di->platform_data->battery_ocv;
990 ocv_size = di->platform_data->ocv_size;
992 for (j = 0; j < 21; j++)
996 for (i = 1; i < ocv_size-1; i++) {
997 if (ocv_table[i+1] < ocv_table[i] + 20)
1001 temp_table[j] = temp_table[j-1]+1;
1003 di->enter_flatzone = ocv_table[i];
1007 for (i = 0; i <= 20; i++) {
1008 if (temp_table[i] < temp_table[i+1])
1013 di->exit_flatzone = ocv_table[i];
1015 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
1020 static int is_not_flatzone(struct battery_info *di, int voltage)
1022 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
1023 DBG("<%s>. is in flat zone\n", __func__);
1026 DBG("<%s>. is not in flat zone\n", __func__);
1031 static void power_on_save(struct battery_info *di, int voltage)
1036 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
1038 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
1039 _voltage_to_capacity(di, voltage);
1040 if (di->temp_soc < 20) {
1041 di->dod0_voltage = voltage;
1042 di->dod0_capacity = di->nac;
1043 di->dod0_status = 1;
1044 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
1045 di->dod0_level = 80;
1047 if (di->temp_soc <= 0)
1048 di->dod0_level = 100;
1049 else if (di->temp_soc < 5)
1050 di->dod0_level = 95;
1051 else if (di->temp_soc < 10)
1052 di->dod0_level = 90;
1053 /* save_soc = di->dod0_level; */
1054 save_soc = get_level(di);
1055 if (save_soc < di->dod0_level)
1056 save_soc = di->dod0_level;
1057 save_level(di, save_soc);
1058 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
1065 static int _get_soc(struct battery_info *di)
1067 return di->remain_capacity * 100 / di->fcc;
1070 static enum power_supply_property rk_battery_props[] = {
1072 POWER_SUPPLY_PROP_STATUS,
1073 POWER_SUPPLY_PROP_CURRENT_NOW,
1074 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1075 POWER_SUPPLY_PROP_PRESENT,
1076 POWER_SUPPLY_PROP_HEALTH,
1077 POWER_SUPPLY_PROP_CAPACITY,
1080 #define to_device_info(x) container_of((x), \
1081 struct battery_info, bat)
1083 static int rk_battery_get_property(struct power_supply *psy,
1084 enum power_supply_property psp,
1085 union power_supply_propval *val)
1088 struct battery_info *di = to_device_info(psy);
1091 case POWER_SUPPLY_PROP_CURRENT_NOW:
1092 val->intval = di->current_avg*1000;/*uA*/
1095 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1096 val->intval = di->voltage*1000;/*uV*/
1099 case POWER_SUPPLY_PROP_PRESENT:
1100 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1101 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1102 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1106 case POWER_SUPPLY_PROP_CAPACITY:
1107 val->intval = di->real_soc;
1110 case POWER_SUPPLY_PROP_HEALTH:
1111 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1114 case POWER_SUPPLY_PROP_STATUS:
1115 val->intval = di->status;
1126 static enum power_supply_property rk_battery_ac_props[] = {
1127 POWER_SUPPLY_PROP_ONLINE,
1129 static enum power_supply_property rk_battery_usb_props[] = {
1130 POWER_SUPPLY_PROP_ONLINE,
1134 #define to_ac_device_info(x) container_of((x), \
1135 struct battery_info, ac)
1137 static int rk_battery_ac_get_property(struct power_supply *psy,
1138 enum power_supply_property psp,
1139 union power_supply_propval *val)
1142 struct battery_info *di = to_ac_device_info(psy);
1145 case POWER_SUPPLY_PROP_ONLINE:
1146 val->intval = di->ac_online; /*discharging*/
1156 #define to_usb_device_info(x) container_of((x), \
1157 struct battery_info, usb)
1159 static int rk_battery_usb_get_property(struct power_supply *psy,
1160 enum power_supply_property psp,
1161 union power_supply_propval *val)
1164 struct battery_info *di = to_usb_device_info(psy);
1167 case POWER_SUPPLY_PROP_ONLINE:
1168 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1171 val->intval = di->usb_online;
1183 static void battery_power_supply_init(struct battery_info *di)
1185 di->bat.name = "BATTERY";
1186 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1187 di->bat.properties = rk_battery_props;
1188 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1189 di->bat.get_property = rk_battery_get_property;
1192 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1193 di->ac.properties = rk_battery_ac_props;
1194 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1195 di->ac.get_property = rk_battery_ac_get_property;
1197 di->usb.name = "USB";
1198 di->usb.type = POWER_SUPPLY_TYPE_USB;
1199 di->usb.properties = rk_battery_usb_props;
1200 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1201 di->usb.get_property = rk_battery_usb_get_property;
1204 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1208 ret = power_supply_register(dev, &di->bat);
1210 dev_err(dev, "failed to register main battery\n");
1213 ret = power_supply_register(dev, &di->usb);
1215 dev_err(dev, "failed to register usb power supply\n");
1218 ret = power_supply_register(dev, &di->ac);
1220 dev_err(dev, "failed to register ac power supply\n");
1227 power_supply_unregister(&di->ac);
1229 power_supply_unregister(&di->usb);
1231 power_supply_unregister(&di->bat);
1236 static void _capacity_init(struct battery_info *di, u32 capacity)
1243 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1245 buf = (capacity_ma>>24)&0xff;
1246 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1247 buf = (capacity_ma>>16)&0xff;
1248 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1249 buf = (capacity_ma>>8)&0xff;
1250 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1251 buf = (capacity_ma&0xff) | 0x01;
1252 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1253 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1259 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1264 if (capacity >= di->qmax)
1265 capacity = di->qmax;
1267 capacity_ma = capacity;
1269 buf = (capacity_ma>>24)&0xff;
1270 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1271 buf = (capacity_ma>>16)&0xff;
1272 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1273 buf = (capacity_ma>>8)&0xff;
1274 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1275 buf = (capacity_ma&0xff) | 0x01;
1276 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1279 static int _get_remain_capacity(struct battery_info *di)
1286 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1288 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1290 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1292 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1295 capacity = temp;/* /4096*900/14/36*500/521; */
1301 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1306 capacity_ma = capacity;
1307 buf = (capacity_ma>>24)&0xff;
1308 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1309 buf = (capacity_ma>>16)&0xff;
1310 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1311 buf = (capacity_ma>>8)&0xff;
1312 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1313 buf = (capacity_ma&0xff) | 0x01;
1314 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1317 static int _get_FCC_capacity(struct battery_info *di)
1324 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1326 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1328 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1330 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1334 capacity = temp-1;/* 4096*900/14/36*500/521 */
1337 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1342 static int _get_realtime_capacity(struct battery_info *di)
1349 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1351 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1353 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1355 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1358 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1363 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1365 int remain_capacity;
1373 now_temp_soc = _get_soc(di);
1374 _voltage_to_capacity(di, relax_voltage);
1375 relax_soc = di->temp_soc;
1376 relax_capacity = di->temp_nac;
1377 abs_soc = abs32_int(relax_soc - now_temp_soc);
1379 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1380 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1383 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1384 remain_capacity = relax_capacity;
1385 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1388 remain_capacity = _get_realtime_capacity(di);
1389 else if (abs_soc <= 10)
1390 remain_capacity = relax_capacity;
1391 else if (abs_soc <= 20)
1392 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1394 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1396 _capacity_init(di, remain_capacity);
1397 di->temp_soc = _get_soc(di);
1398 di->remain_capacity = _get_realtime_capacity(di);
1401 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1402 if (relax_soc < now_temp_soc) {
1403 if (di->suspend_temp_soc - relax_soc <= 5)
1404 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1405 else if (di->suspend_temp_soc - relax_soc <= 10)
1406 di->real_soc = di->real_soc - 5;
1408 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1410 now_current = _get_average_current(di);
1411 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1412 min = soc_time / 60;
1413 if (sleep_min > min)
1417 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);
1421 static int _copy_soc(struct battery_info *di, u8 save_soc)
1426 battery_write(di->rk818, SOC_REG, &soc, 1);
1430 static bool support_uboot_charge(void)
1432 return support_uboot_chrg?true:false;
1435 static int _rsoc_init(struct battery_info *di)
1439 u32 remain_capacity;
1442 #ifdef SUPPORT_USB_CHARGE
1447 di->voltage = rk_battery_voltage(di);
1448 di->voltage_ocv = _get_OCV_voltage(di);
1449 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1451 if (_is_first_poweron(di)) {
1452 _save_FCC_capacity(di, di->design_capacity);
1453 di->fcc = _get_FCC_capacity(di);
1455 _voltage_to_capacity(di, di->voltage_ocv);
1456 di->real_soc = di->temp_soc;
1457 di->nac = di->temp_nac;
1458 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1461 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1462 init_soc = pwron_soc;
1463 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1465 #ifdef SUPPORT_USB_CHARGE
1466 otg_status = dwc_otg_check_dpdm();
1467 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1469 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1472 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1473 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1475 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1478 remain_capacity = _get_remain_capacity(di);
1480 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1481 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1482 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1483 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1485 if (!support_uboot_charge()) {
1486 _voltage_to_capacity(di, di->voltage_ocv);
1487 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1489 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1490 if (last_shtd_time != curr_shtd_time) {
1492 if (curr_shtd_time > 30) {
1493 remain_capacity = di->temp_nac;
1494 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1496 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - init_soc) >= 10)) {
1497 if (remain_capacity >= di->temp_nac*120/100)
1498 remain_capacity = di->temp_nac*110/100;
1499 else if (remain_capacity < di->temp_nac*8/10)
1500 remain_capacity = di->temp_nac*9/10;
1502 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1507 di->real_soc = init_soc;
1508 di->nac = remain_capacity;
1511 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1517 static u8 get_charge_status(struct battery_info *di)
1522 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1527 DBG(" CHARGE-OFF ...\n");
1532 DBG(" DEAD CHARGE ...\n");
1535 case TRICKLE_CHARGE: /* (0x02 << 4) */
1537 DBG(" TRICKLE CHARGE ...\n ");
1540 case CC_OR_CV: /* (0x03 << 4) */
1542 DBG(" CC or CV ...\n");
1545 case CHARGE_FINISH: /* (0x04 << 4) */
1546 ret = CHARGE_FINISH;
1547 DBG(" CHARGE FINISH ...\n");
1550 case USB_OVER_VOL: /* (0x05 << 4) */
1552 DBG(" USB OVER VOL ...\n");
1555 case BAT_TMP_ERR: /* (0x06 << 4) */
1557 DBG(" BAT TMP ERROR ...\n");
1560 case TIMER_ERR: /* (0x07 << 4) */
1562 DBG(" TIMER ERROR ...\n");
1565 case USB_EXIST: /* (1 << 1)// usb is exists */
1567 DBG(" USB EXIST ...\n");
1570 case USB_EFF: /* (1 << 0)// usb is effective */
1572 DBG(" USB EFF...\n");
1582 static void set_charge_current(struct battery_info *di, int charge_current)
1586 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1587 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1588 usb_ctrl_reg |= (charge_current);
1589 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1592 static void fg_match_param(struct battery_info *di, int chg_vol, int chg_ilim, int chg_cur)
1596 di->chg_v_lmt = CHRG_VOL4200;
1597 di->chg_i_lmt = ILIM_1750MA;
1598 di->chg_i_cur = CHRG_CUR1400mA;
1600 for (i=0; i<ARRAY_SIZE(CHG_V_LMT); i++){
1601 if (chg_vol < CHG_V_LMT[i])
1604 di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1607 for (i=0; i<ARRAY_SIZE(CHG_I_LMT); i++){
1608 if (chg_ilim < CHG_I_LMT[i])
1611 di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1614 for (i=0; i<ARRAY_SIZE(CHG_I_CUR); i++){
1615 if (chg_cur < CHG_I_CUR[i])
1618 di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1620 DBG("vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
1621 di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
1624 static void rk_battery_charger_init(struct battery_info *di)
1626 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1629 int chg_vol = di->rk818->battery_data->max_charger_voltagemV;
1630 int chg_cur = di->rk818->battery_data->max_charger_currentmA;
1631 int chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
1632 fg_match_param(di, chg_vol, chg_ilim, chg_cur);
1633 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1634 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1635 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1636 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1637 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1639 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1640 usb_ctrl_reg &= (~0x0f);
1641 #ifdef SUPPORT_USB_CHARGE
1642 usb_ctrl_reg |= (ILIM_450MA);
1644 usb_ctrl_reg |= (di->chg_i_lmt);
1646 chrg_ctrl_reg1 &= (0x00);
1647 chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
1649 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1650 chrg_ctrl_reg2 &= ~(0xc0);
1651 chrg_ctrl_reg2 |= FINISH_100MA;
1653 sup_sts_reg &= ~(0x01 << 3);
1654 sup_sts_reg |= (0x01 << 2);
1656 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1657 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1658 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1659 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1660 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1662 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1663 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1664 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1665 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1667 DBG("%s end\n", __func__);
1670 void charge_disable_open_otg(int value)
1672 struct battery_info *di = g_battery;
1675 DBG("charge disable, enable OTG.\n");
1676 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1677 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1680 DBG("charge enable, disable OTG.\n");
1681 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1682 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1686 static void low_waring_init(struct battery_info *di)
1691 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1693 /* 2.8v~3.5v, interrupt */
1694 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1695 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1698 static void fg_init(struct battery_info *di)
1702 adc_ctrl_val = 0x30;
1703 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1706 /* get the volatege offset */
1707 _get_voltage_offset_value(di);
1708 rk_battery_charger_init(di);
1709 _set_relax_thres(di);
1710 /* get the current offset , the value write to the CAL_OFFSET */
1711 di->current_offset = _get_ioffset(di);
1712 _set_cal_offset(di, di->current_offset+42);
1714 _capacity_init(di, di->nac);
1716 di->remain_capacity = _get_realtime_capacity(di);
1717 di->current_avg = _get_average_current(di);
1719 low_waring_init(di);
1721 power_on_save(di, di->voltage_ocv);
1722 /* set sample time for cal_offset interval*/
1723 ioffset_sample_time(di, SAMP_TIME_8MIN);
1724 dump_gauge_register(di);
1725 dump_charger_register(di);
1728 "nac = %d , remain_capacity = %d\n"
1729 "OCV_voltage = %d, voltage = %d\n"
1730 "SOC = %d, fcc = %d\n, current=%d",
1732 di->nac, di->remain_capacity,
1733 di->voltage_ocv, di->voltage,
1734 di->real_soc, di->fcc, di->current_avg);
1738 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1739 static void zero_get_soc(struct battery_info *di)
1741 int dead_voltage, ocv_voltage;
1742 int temp_soc = -1, real_soc;
1743 int currentold, currentnow, voltage;
1750 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1752 currentold = _get_average_current(di);
1753 _get_cal_offset(di);
1756 currentnow = _get_average_current(di);
1758 } while ((currentold == currentnow) && (count_num < 11));
1761 for (i = 0; i < 10 ; i++)
1762 voltage += rk_battery_voltage(di);
1765 if (di->voltage_old == 0)
1766 di->voltage_old = voltage;
1767 voltage_k = voltage;
1768 voltage = (di->voltage_old*2 + 8*voltage)/10;
1769 di->voltage_old = voltage;
1770 currentnow = _get_average_current(di);
1772 dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
1773 /* 65 mo power-path mos */
1774 ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
1775 DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
1776 dead_voltage, ocv_voltage);
1778 _voltage_to_capacity(di, dead_voltage);
1779 di->q_dead = di->temp_nac;
1780 DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
1781 di->temp_soc, di->q_dead);
1783 _voltage_to_capacity(di, ocv_voltage);
1784 q_ocv = di->temp_nac;
1785 DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
1786 di->temp_soc, q_ocv);
1788 /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
1789 di->q_err = di->remain_capacity - q_ocv;
1790 DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
1791 di->q_err, di->remain_capacity, q_ocv);
1793 if (di->display_soc == 0)
1794 di->display_soc = di->real_soc*1000;
1795 real_soc = di->display_soc;
1797 DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
1798 di->remain_capacity, di->q_dead, di->q_err);
1799 /*[temp_nac]:dead_voltage*/
1800 if (q_ocv > di->q_dead) {
1801 DBG("first: q_ocv > di->q_dead\n");
1803 if (di->update_k == 0 || di->update_k >= 10) {
1804 if (di->update_k == 0) {
1806 /* ZQ = Q_ded + Qerr */
1807 /*[temp_nac]:dead_voltage*/
1808 di->q_shtd = di->q_dead + di->q_err;
1809 temp_soc = (di->remain_capacity - di->q_shtd)*
1814 di->line_k = (real_soc + temp_soc/2)
1817 DBG("[K >= 10].\n");
1818 temp_soc = ((di->remain_capacity - di->q_shtd)*
1819 1000 + di->fcc/2)/di->fcc; /* x1 10 */
1821 real_soc = (di->line_k*temp_soc); /*y1=k0*x1*/
1822 di->display_soc = real_soc;
1823 DBG("[K >= 10]. (temp_soc)X0 = %d\n", temp_soc);
1824 DBG("[K >= 10]. in:line_k = %d\n", di->line_k);
1825 DBG("[K >= 10]. (dis-soc)Y0=%d,real-soc=%d\n",
1826 di->display_soc, di->real_soc);
1828 if ((real_soc+500)/1000 < di->real_soc){
1830 di->odd_capacity = 0;
1832 else if (((real_soc+500))/1000 ==
1835 real_soc -= di->odd_capacity;
1836 if ((real_soc+500)/1000 <
1839 di->odd_capacity = 0;
1843 DBG("[k >= 10]. odd_capacity=%d\n",
1846 di->odd_capacity = 0;
1847 _voltage_to_capacity(di, dead_voltage);
1848 di->q_dead = di->temp_nac;
1849 di->q_shtd = di->q_dead + di->q_err;
1850 temp_soc = ((di->remain_capacity - di->q_shtd)*
1851 1000 + di->fcc/2)/di->fcc; /* z1 */
1855 di->line_k = (di->display_soc +
1856 temp_soc/2)/temp_soc;
1857 DBG("[K >= 10]. out:line_k = %d\n", di->line_k);
1863 else { /*update_k[1~9]*/
1868 temp_soc = ((di->remain_capacity - di->q_shtd)*
1869 1000 + di->fcc/2)/di->fcc;
1870 di->display_soc = di->line_k*temp_soc;
1871 DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
1872 DBG("[K1~9]. line_k = %d\n", di->line_k);
1873 DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
1874 di->display_soc, di->real_soc);
1875 if ((di->display_soc+500)/1000 < di->real_soc){
1877 di->odd_capacity = 0;
1879 else if ((real_soc+500)/1000 == di->real_soc) {
1881 real_soc -= di->odd_capacity;
1882 if ((real_soc+500)/1000 < di->real_soc) {
1884 di->odd_capacity = 0;
1886 di->odd_capacity += real_soc/3000+2;
1887 DBG("[K1~9]. odd_capacity=%d\n",
1890 di->odd_capacity = 0;
1893 DBG("second: q_ocv < di->q_dead\n");
1895 if ((di->voltage < 3400) && (di->real_soc > 10)) {
1896 /*di->real_soc = 10;*/
1898 } else if (di->voltage < 3400) {
1899 /*10 -(3.4-Vbat)*100*I*/
1900 if (di->current_avg < 1000)
1901 soc_time = 10-((3400-di->voltage)/10*
1902 abs32_int(di->current_avg))/1000;
1904 DBG("<%s>. ZERO: decrease sec = %d\n",
1905 __func__, soc_time/2);
1906 if (di->update_k > soc_time/2) {
1911 if (di->update_k > 10) {
1918 if (di->line_k <= 0) {
1920 DBG("ZERO: line_k <= 0, Update line_k!\n");
1923 DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
1924 DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
1925 DBG("ZERO: remain_cap - q_shtd=%d\n",
1926 (di->remain_capacity - di->q_shtd));
1927 DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
1928 di->line_k, di->display_soc, temp_soc);
1929 DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
1930 di->remain_capacity, di->q_shtd, di->q_err);
1931 DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
1932 di->warnning_voltage, _get_soc(di), di->real_soc);
1936 static int estimate_bat_ocv_vol(struct battery_info *di)
1938 return (di->voltage -
1939 (di->bat_res * di->current_avg) / 1000);
1942 static int estimate_bat_ocv_soc(struct battery_info *di)
1944 int ocv_soc, ocv_voltage;
1946 ocv_voltage = estimate_bat_ocv_vol(di);
1947 _voltage_to_capacity(di, ocv_voltage);
1948 ocv_soc = di->temp_soc;
1953 static void rsoc_dischrg_calib(struct battery_info *di)
1955 int ocv_soc = di->est_ocv_soc;
1956 int ocv_volt = di->est_ocv_vol;
1957 int temp_soc = _get_soc(di);
1958 int max_volt = di->rk818->battery_data->max_charger_voltagemV;
1960 if (ocv_volt > max_volt)
1963 if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
1964 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
1965 (di->temp_soc == 0) ||
1966 (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
1969 di->err_soc_sum += ocv_soc;
1973 DBG("<%s>. rsoc err_chck_cnt = %d\n",
1974 __func__, di->err_chck_cnt);
1975 DBG("<%s>. rsoc err_soc_sum = %d\n",
1976 __func__, di->err_soc_sum);
1978 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
1980 ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
1981 if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
1982 ocv_soc += RSOC_COMPS;
1984 di->temp_nac = ocv_soc * di->fcc / 100;
1985 _capacity_init(di, di->temp_nac);
1986 di->temp_soc = _get_soc(di);
1987 di->remain_capacity = _get_realtime_capacity(di);
1988 di->err_soc_sum = 0;
1989 di->err_chck_cnt = 0;
1990 DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
1994 di->err_chck_cnt = 0;
1995 di->err_soc_sum = 0;
2000 static void rsoc_realtime_calib(struct battery_info *di)
2002 u8 status = di->status;
2004 if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2005 (status == POWER_SUPPLY_STATUS_FULL)) {
2007 if ((di->current_avg < -10) &&
2008 (di->charge_status != CHARGE_FINISH))
2009 rsoc_dischrg_calib(di);
2012 rsoc_chrg_calib(di);
2015 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2016 rsoc_dischrg_calib(di);
2020 static bool do_ac_charger_emulator(struct battery_info *di)
2022 int delta_soc = di->temp_soc - di->real_soc;
2025 if ((di->charge_status != CHARGE_FINISH)
2027 && (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)){
2029 soc_time = di->fcc*3600/100/(abs_int(DSOC_CHRG_EMU_CURR));
2031 if (di->emu_chg_cnt > soc_time) {
2033 di->emu_chg_cnt = 0;
2035 DBG("<%s>. soc_time=%d, emu_cnt=%d\n",
2036 __func__, soc_time, di->emu_chg_cnt);
2044 static bool do_term_chrg_cali(struct battery_info *di)
2048 if (di->ac_online &&
2049 (di->real_soc >= 90)&&
2050 (di->current_avg > 600)){
2052 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2054 if (di->term_chg_cnt > soc_time) {
2056 di->term_chg_cnt = 0;
2058 DBG("<%s>. soc_time=%d, term_cnt=%d\n",
2059 __func__, soc_time, di->term_chg_cnt);
2067 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
2070 int now_current, soc_time = -1;
2072 int delta_soc = di->real_soc - di->temp_soc;
2074 voltage = di->voltage;
2075 now_current = di->current_avg;
2076 if (now_current == 0)
2079 if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE){
2080 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2081 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2082 __func__, delta_soc);
2084 soc_time = di->fcc*3600/100/(abs_int(now_current));
2085 _voltage_to_capacity(di, 3800);
2086 volt_to_soc = di->temp_soc;
2087 di->temp_soc = _get_soc(di);
2089 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
2090 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2091 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
2095 } else if (di->temp_soc == di->real_soc) {
2096 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2097 } else if (di->temp_soc > di->real_soc) {
2098 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2099 di->vol_smooth_time++;
2100 if (di->vol_smooth_time > soc_time*3/2) {
2102 di->vol_smooth_time = 0;
2106 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2107 if (di->real_soc == (di->temp_soc + 1)) {
2108 di->change_timer = di->soc_timer;
2109 di->real_soc = di->temp_soc;
2111 di->vol_smooth_time++;
2112 if (di->vol_smooth_time > soc_time*3/4) {
2114 di->vol_smooth_time = 0;
2119 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2120 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
2123 static int get_charging_time(struct battery_info *di)
2125 return (di->charging_time/60);
2128 static int get_discharging_time(struct battery_info *di)
2130 return (di->discharging_time/60);
2133 static int get_finish_time(struct battery_info *di)
2135 return (di->finish_time/60);
2138 static void dump_debug_info(struct battery_info *di)
2140 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2141 u8 usb_ctrl_reg, chrg_ctrl_reg1;
2142 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
2144 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2145 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2146 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2147 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2148 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2149 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2150 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2151 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2152 battery_read(di->rk818, 0x00, &rtc_val, 1);
2154 DBG("\n------------- dump_debug_regs -----------------\n"
2155 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
2156 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
2157 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2158 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2159 ggcon_reg, ggsts_reg, rtc_val,
2160 sup_tst_reg, vb_mod_reg,
2161 usb_ctrl_reg, chrg_ctrl_reg1,
2162 chrg_ctrl_reg2, chrg_ctrl_reg3
2166 "########################## [read] ################################\n"
2167 "-----------------------------------------------------------------\n"
2168 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2169 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2170 "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2171 "diplay_soc = %d, cpapacity_soc = %d\n"
2172 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2173 "finish_real_soc = %d, finish_temp_soc = %d\n"
2174 "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2175 get_relax_voltage(di),
2176 di->voltage, di->current_avg,
2177 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2178 di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2179 di->real_soc, _get_soc(di),
2180 di->ac_online, di->usb_online, di->status,
2181 di->debug_finish_real_soc, di->debug_finish_temp_soc,
2182 get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2184 get_charge_status(di);
2185 DBG("################################################################\n");
2188 static void update_fcc_capacity(struct battery_info *di)
2190 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2191 if (get_level(di) >= di->dod0_level) {
2192 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
2193 if (di->fcc > di->qmax)
2196 _capacity_init(di, di->fcc);
2197 _save_FCC_capacity(di, di->fcc);
2199 di->dod0_status = 0;
2203 static void debug_get_finish_soc(struct battery_info *di)
2205 if (di->charge_status == CHARGE_FINISH) {
2206 di->debug_finish_real_soc = di->real_soc;
2207 di->debug_finish_temp_soc = di->temp_soc;
2211 static void wait_charge_finish_signal(struct battery_info *di)
2213 if (di->charge_status == CHARGE_FINISH)
2214 update_fcc_capacity(di);/* save new fcc*/
2217 debug_get_finish_soc(di);
2220 static void charge_finish_routine(struct battery_info *di)
2222 if ((di->charge_status == CHARGE_FINISH)&&
2223 (di->finish_min >= 1)) {
2224 _capacity_init(di, di->fcc);
2225 zero_current_calibration(di);
2227 if (di->real_soc < 100) {
2228 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
2229 if ((di->soc_counter < 80)) {
2232 di->soc_counter = 0;
2239 static void voltage_to_soc_charge_smooth(struct battery_info *di)
2241 int now_current, soc_time;
2244 /*calibrate: aim to match finish signal*/
2245 if (do_term_chrg_cali(di))
2248 /*calibrate: aim to calib error*/
2249 di->term_chg_cnt = 0;
2250 if (do_ac_charger_emulator(di))
2253 di->emu_chg_cnt = 0;
2254 now_current = _get_average_current(di);
2255 if (now_current == 0)
2258 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
2259 di->temp_soc = _get_soc(di);
2261 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2263 if (di->real_soc == di->temp_soc) {
2264 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2265 di->temp_soc = _get_soc(di);
2267 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2269 if (di->temp_soc < di->real_soc + 1) {
2270 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2271 di->charge_smooth_time++;
2272 if (di->charge_smooth_time > soc_time*3/2) {
2274 di->charge_smooth_time = 0;
2276 di->charge_smooth_status = true;
2279 else if (di->temp_soc > di->real_soc + 1) {
2280 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2281 di->charge_smooth_time++;
2282 if (di->charge_smooth_time > soc_time*3/4) {
2284 di->charge_smooth_time = 0;
2286 di->charge_smooth_status = true;
2288 } else if (di->temp_soc == di->real_soc + 1) {
2289 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
2290 if (di->charge_smooth_status) {
2291 di->charge_smooth_time++;
2292 if (di->charge_smooth_time > soc_time*3/4) {
2293 di->real_soc = di->temp_soc;
2294 di->charge_smooth_time = 0;
2295 di->charge_smooth_status = false;
2299 di->real_soc = di->temp_soc;
2300 di->charge_smooth_status = false;
2306 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2307 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2310 static void rk_battery_display_smooth(struct battery_info *di)
2315 status = di->status;
2316 charge_status = di->charge_status;
2317 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2319 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2320 voltage_to_soc_discharge_smooth(di);
2322 voltage_to_soc_charge_smooth(di);
2324 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2325 voltage_to_soc_discharge_smooth(di);
2326 if (di->real_soc == 1) {
2328 if (di->time2empty >= 300)
2338 static void software_recharge(struct battery_info *di, int max_cnt)
2340 static int recharge_cnt;
2343 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2344 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2345 chrg_ctrl_reg1 &= ~(1 << 7);
2346 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2347 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2348 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2350 chrg_ctrl_reg1 |= (1 << 7);
2351 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2352 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2353 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2361 static int estimate_battery_resister(struct battery_info *di)
2364 int avr_voltage1 = 0, avr_current1;
2365 int avr_voltage2 = 0, avr_current2;
2367 int bat_res, ocv_votage;
2368 static unsigned long last_time;
2369 unsigned long delta_time;
2370 int charge_ocv_voltage1, charge_ocv_voltage2;
2371 int charge_ocv_soc1, charge_ocv_soc2;
2373 delta_time = get_seconds() - last_time;
2374 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2375 if (delta_time >= 20) {/*20s*/
2378 set_charge_current(di, ILIM_450MA);/*450mA*/
2380 for (i = 0; i < 10 ; i++) {
2382 avr_voltage1 += rk_battery_voltage(di);
2385 avr_current1 = _get_average_current(di);
2386 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2387 DBG("------------------------------------------------------------------------------------------\n");
2388 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2391 set_charge_current(di, ILIM_3000MA);
2393 for (i = 0; i < 10 ; i++) {
2395 avr_voltage2 += rk_battery_voltage(di);
2398 avr_current2 = _get_average_current(di);
2399 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2400 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2402 /*calc resister and ocv_votage ocv*/
2403 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2404 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2405 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2407 /*calc sample voltage ocv*/
2408 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2409 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2410 _voltage_to_capacity(di, charge_ocv_voltage1);
2411 charge_ocv_soc1 = di->temp_soc;
2412 _voltage_to_capacity(di, charge_ocv_voltage2);
2413 charge_ocv_soc2 = di->temp_soc;
2415 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2416 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2417 DBG("------------------------------------------------------------------------------------------\n");
2418 last_time = get_seconds();
2428 static int update_battery_resister(struct battery_info *di)
2432 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2434 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2435 tmp_res = estimate_battery_resister(di);
2437 di->bat_res_update_cnt++;
2438 di->bat_res += tmp_res;
2439 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2440 if (di->bat_res_update_cnt == 10) {
2441 di->bat_res_updated = true;
2444 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2453 static void charge_soc_check_routine(struct battery_info *di)
2461 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2462 min = get_charging_time(di);
2463 update_battery_resister(di);
2465 if ((min >= 30) && (di->bat_res_updated)) {
2467 old_temp_soc = di->temp_soc;
2468 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2469 _voltage_to_capacity(di, ocv_voltage);
2470 ocv_temp_soc = di->temp_soc;
2472 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2473 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2474 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2476 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2477 di->temp_soc = ocv_temp_soc;
2479 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2481 remain_capcity = di->temp_soc * di->fcc / 100;
2482 _capacity_init(di, remain_capcity);
2483 di->remain_capacity = _get_realtime_capacity(di);
2484 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2492 static void update_resume_status_relax_voltage(struct battery_info *di)
2494 unsigned long sleep_soc;
2495 unsigned long sum_sleep_soc;
2496 unsigned long sleep_sec;
2501 int sum_sleep_avr_current;
2505 update_battery_info(di);
2507 di->sys_wakeup = true;
2509 DBG("<%s>, resume----------checkstart\n", __func__);
2510 sleep_sec = get_seconds() - di->suspend_time_start;
2511 sleep_min = sleep_sec / 60;
2513 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2514 __func__, sleep_sec, sleep_min);
2516 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2517 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2519 delta_capacity = di->suspend_capacity - di->remain_capacity;
2520 delta_soc = di->suspend_temp_soc - _get_soc(di);
2521 di->dischrg_sum_sleep_capacity += delta_capacity;
2522 di->dischrg_sum_sleep_sec += sleep_sec;
2524 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2525 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2527 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2528 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2529 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2530 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2531 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2532 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2533 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2535 /*large suspend current*/
2536 if (sum_sleep_avr_current > 20) {
2537 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2538 di->real_soc -= sum_sleep_soc;
2539 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2540 __func__, sum_sleep_soc, di->real_soc);
2542 /* small suspend current*/
2543 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2545 relax_voltage = get_relax_voltage(di);
2546 di->voltage = rk_battery_voltage(di);
2548 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2549 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2550 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2553 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2554 if (sum_sleep_soc > 0)
2555 di->real_soc -= sum_sleep_soc;
2559 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2560 di->dischrg_sum_sleep_capacity = 0;
2561 di->dischrg_sum_sleep_sec = 0;
2563 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2564 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2567 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2568 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2569 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2570 di->temp_soc = _get_soc(di);
2571 charge_status = get_charge_status(di);
2573 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2574 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2575 DBG("resume, sleep : ac online charge current < 1000\n");
2576 if (sleep_sec > 0) {
2577 di->count_sleep_time += sleep_sec;
2578 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2579 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2581 di->count_sleep_time = 0;
2582 di->real_soc += sleep_soc;
2583 if (di->real_soc > 100)
2588 DBG("<%s>, usb charging\n", __func__);
2589 if (di->suspend_temp_soc + 15 < di->temp_soc)
2590 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2592 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2595 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);
2602 #ifdef SUPPORT_USB_CHARGE
2603 static int get_charging_status_type(struct battery_info *di)
2605 int otg_status = dwc_otg_check_dpdm();
2607 if (0 == otg_status) {
2610 di->check_count = 0;
2612 } else if (1 == otg_status) {
2613 if (0 == get_gadget_connect_flag()) {
2614 if (++di->check_count >= 5) {
2626 } else if (2 == otg_status) {
2629 di->check_count = 0;
2632 if (di->ac_online == 1)
2633 set_charge_current(di, di->chg_i_lmt);
2635 set_charge_current(di, ILIM_450MA);
2641 static void battery_poweron_status_init(struct battery_info *di)
2645 #ifndef SUPPORT_USB_CHARGE
2649 #ifdef SUPPORT_USB_CHARGE
2651 otg_status = dwc_otg_check_dpdm();
2652 if (otg_status == 1) {
2655 set_charge_current(di, ILIM_450MA);
2656 di->status = POWER_SUPPLY_STATUS_CHARGING;
2657 DBG("++++++++ILIM_450MA++++++\n");
2659 } else if (otg_status == 2) {
2662 di->status = POWER_SUPPLY_STATUS_CHARGING;
2663 set_charge_current(di, di->chg_i_lmt);
2664 DBG("++++++++ILIM_1000MA++++++\n");
2666 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2670 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2671 if (buf&PLUG_IN_STS) {
2674 di->status = POWER_SUPPLY_STATUS_CHARGING;
2675 if (di->real_soc == 100)
2676 di->status = POWER_SUPPLY_STATUS_FULL;
2678 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2682 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2687 static void check_battery_status(struct battery_info *di)
2692 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2693 #ifdef SUPPORT_USB_CHARGE
2695 if (strstr(saved_command_line, "charger")) {
2696 if ((buf&PLUG_IN_STS) == 0) {
2697 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2703 if (buf&PLUG_IN_STS) {
2704 get_charging_status_type(di);
2706 di->status = POWER_SUPPLY_STATUS_CHARGING;
2707 if (di->real_soc == 100)
2708 di->status = POWER_SUPPLY_STATUS_FULL;
2710 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2717 if (buf & PLUG_IN_STS) {
2720 di->status = POWER_SUPPLY_STATUS_CHARGING;
2721 if (di->real_soc == 100)
2722 di->status = POWER_SUPPLY_STATUS_FULL;
2724 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2731 static void last_check_report(struct battery_info *di)
2733 /* high load: current < 0 with charger in.
2734 * System will not shutdown when dsoc=0% with charging state(ac_online),
2735 * which will cause over discharge, so oppose status.
2739 if ((di->real_soc == 0) && (di->status == POWER_SUPPLY_STATUS_CHARGING)
2740 && di->current_avg < 0){
2741 if (get_seconds() - time > 60){
2742 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2746 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
2747 DBG("status=%d, ac_online=%d, usb_online=%d\n",
2748 di->status, di->ac_online, di->usb_online);
2751 time = get_seconds();
2754 static void report_power_supply_changed(struct battery_info *di)
2757 static u32 old_ac_status;
2758 static u32 old_usb_status;
2759 static u32 old_charge_status;
2762 state_changed = false;
2763 if (di->real_soc == 0)
2764 state_changed = true;
2765 else if (di->real_soc == 100)
2766 state_changed = true;
2767 else if (di->real_soc != old_soc)
2768 state_changed = true;
2769 else if (di->ac_online != old_ac_status)
2770 state_changed = true;
2771 else if (di->usb_online != old_usb_status)
2772 state_changed = true;
2773 else if (old_charge_status != di->status)
2774 state_changed = true;
2776 if (state_changed) {
2777 power_supply_changed(&di->bat);
2778 power_supply_changed(&di->usb);
2779 power_supply_changed(&di->ac);
2780 old_soc = di->real_soc;
2781 old_ac_status = di->ac_online;
2782 old_usb_status = di->usb_online;
2783 old_charge_status = di->status;
2787 static void upd_time_table(struct battery_info *di)
2790 static int old_index = 0;
2791 static int old_min = 0;
2793 int mod = di->real_soc % 10;
2794 int index = di->real_soc / 10;
2796 if (di->ac_online || di->usb_online)
2797 time = di->charge_min;
2799 time = di->discharge_min;
2801 if ((mod == 0) && (index > 0) && (old_index != index)) {
2802 di->chrg_min[index-1] = time - old_min;
2807 for (i=1; i<11; i++)
2808 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
2813 static void update_battery_info(struct battery_info *di)
2815 di->remain_capacity = _get_realtime_capacity(di);
2816 if (di->remain_capacity > di->fcc)
2817 _capacity_init(di, di->fcc);
2819 if (di->real_soc > 100)
2821 else if (di->real_soc < 0)
2824 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2825 di->charging_time++;
2826 di->discharging_time = 0;
2828 di->charging_time = 0;
2829 if (di->voltage < 3800)
2830 di->discharging_time += 2;
2832 di->discharging_time++;
2834 if (di->charge_status == CHARGE_FINISH)
2837 di->finish_time = 0;
2839 di->charge_min = get_charging_time(di);
2840 di->discharge_min = get_discharging_time(di);
2841 di->finish_min = get_finish_time(di);
2844 di->est_ocv_vol = estimate_bat_ocv_vol(di);
2845 di->est_ocv_soc = estimate_bat_ocv_soc(di);
2846 di->voltage = rk_battery_voltage(di);
2847 di->current_avg = _get_average_current(di);
2848 di->remain_capacity = _get_realtime_capacity(di);
2849 di->voltage_ocv = _get_OCV_voltage(di);
2850 di->charge_status = get_charge_status(di);
2851 di->otg_status = dwc_otg_check_dpdm();
2852 di->relax_voltage = get_relax_voltage(di);
2853 di->temp_soc = _get_soc(di);
2854 check_battery_status(di);/* ac_online, usb_online, status*/
2855 update_cal_offset(di);
2859 static void rk_battery_work(struct work_struct *work)
2861 struct battery_info *di = container_of(work,
2862 struct battery_info, battery_monitor_work.work);
2864 update_resume_status_relax_voltage(di);
2865 wait_charge_finish_signal(di);
2866 charge_finish_routine(di);
2868 rk_battery_display_smooth(di);
2869 update_battery_info(di);
2870 rsoc_realtime_calib(di);
2871 last_check_report(di);
2872 report_power_supply_changed(di);
2873 _copy_soc(di, di->real_soc);
2874 _save_remain_capacity(di, di->remain_capacity);
2876 dump_debug_info(di);
2877 di->queue_work_cnt++;
2878 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2881 static void rk_battery_charge_check_work(struct work_struct *work)
2883 struct battery_info *di = container_of(work,
2884 struct battery_info, charge_check_work.work);
2886 DBG("rk_battery_charge_check_work\n");
2887 charge_disable_open_otg(di->charge_otg);
2890 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2892 int register_battery_notifier(struct notifier_block *nb)
2894 return blocking_notifier_chain_register(&battery_chain_head, nb);
2896 EXPORT_SYMBOL_GPL(register_battery_notifier);
2898 int unregister_battery_notifier(struct notifier_block *nb)
2900 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2902 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2904 int battery_notifier_call_chain(unsigned long val)
2906 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2907 == NOTIFY_BAD) ? -EINVAL : 0;
2909 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2911 static void poweron_lowerpoer_handle(struct battery_info *di)
2913 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2914 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2916 /* kernel_power_off(); */
2921 static int battery_notifier_call(struct notifier_block *nb,
2922 unsigned long event, void *data)
2924 struct battery_info *di =
2925 container_of(nb, struct battery_info, battery_nb);
2929 DBG(" CHARGE enable\n");
2931 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2936 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2937 DBG("charge disable OTG enable\n");
2941 poweron_lowerpoer_handle(di);
2950 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2952 pr_info("<%s>lower power warning!\n", __func__);
2954 _copy_soc(g_battery, 0);
2955 _capacity_init(g_battery, 0);
2956 rk_send_wakeup_key();
2961 static void disable_vbat_low_irq(struct battery_info *di)
2964 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2965 /*clr vbat low interrupt */
2966 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2968 static void enable_vbat_low_irq(struct battery_info *di)
2970 /* clr vbat low interrupt */
2971 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2973 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2976 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2978 pr_info("\n------- %s:irq = %d\n", __func__, irq);
2979 rk_send_wakeup_key();
2982 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
2984 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2985 charge_disable_open_otg(0);
2986 rk_send_wakeup_key();
2990 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
2992 pr_info("---------- %s:irq = %d\n", __func__, irq);
2993 rk_send_wakeup_key();
2999 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
3003 struct kobject *rk818_fg_kobj;
3005 ret = create_sysfs_interfaces(dev);
3008 dev_err(dev, "device RK818 battery sysfs register failed\n");
3012 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3015 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3016 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3018 dev_err(dev, "create rk818_battery node error\n");
3026 power_supply_unregister(&di->ac);
3027 power_supply_unregister(&di->usb);
3028 power_supply_unregister(&di->bat);
3033 static void rk818_battery_irq_init(struct battery_info *di)
3035 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3037 struct rk818 *chip = di->rk818;
3039 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3040 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3041 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3042 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3044 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3045 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3047 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3049 di->irq = vb_lo_irq;
3050 enable_irq_wake(di->irq);
3051 disable_vbat_low_irq(di);
3053 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3054 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
3056 dev_err(chip->dev, "plug_in_irq request failed!\n");
3059 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3060 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
3062 dev_err(chip->dev, "plug_out_irq request failed!\n");
3065 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3066 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
3068 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3071 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
3077 di->platform_data = chip->battery_data;
3078 di->cell.config = di->platform_data->cell_cfg;
3079 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3080 di->qmax = di->platform_data->cell_cfg->design_qmax;
3081 di->fcc = di->design_capacity;
3082 di->vol_smooth_time = 0;
3083 di->charge_smooth_time = 0;
3084 di->charge_smooth_status = false;
3085 di->sleep_status = 0;
3087 di->sys_wakeup = true;
3088 di->pcb_ioffset = 0;
3089 di->pcb_ioffset_updated = false;
3090 di->queue_work_cnt = 0;
3092 di->voltage_old = 0;
3093 di->display_soc = 0;
3095 di->bat_res_updated = false;
3097 di->sys_wakeup = true;
3098 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3101 di->discharge_min = 0;
3102 di->charging_time = 0;
3103 di->discharging_time = 0;
3104 di->finish_time = 0;
3108 di->odd_capacity = 0;
3109 di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3110 di->term_chg_cnt = 0;
3111 di->emu_chg_cnt = 0;
3113 for (i=0; i<10; i++)
3114 di->chrg_min[i] = -1;
3116 di->debug_finish_real_soc = 0;
3117 di->debug_finish_temp_soc = 0;
3119 fcc_capacity = _get_FCC_capacity(di);
3120 if (fcc_capacity > 1000)
3121 di->fcc = fcc_capacity;
3123 di->fcc = di->design_capacity;
3126 static struct of_device_id rk818_battery_of_match[] = {
3127 { .compatible = "rk818_battery" },
3131 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3134 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3136 struct device_node *regs, *rk818_pmic_np;
3137 struct battery_platform_data *data;
3138 struct cell_config *cell_cfg;
3139 struct ocv_config *ocv_cfg;
3140 struct property *prop;
3144 rk818_pmic_np = of_node_get(rk818->dev->of_node);
3145 if (!rk818_pmic_np) {
3146 dev_err(dev, "could not find pmic sub-node\n");
3150 regs = of_find_node_by_name(rk818_pmic_np, "battery");
3152 dev_err(dev, "battery node not found!\n");
3156 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3158 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3162 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3164 dev_err(dev, "kzalloc for cell_config failed!\n");
3167 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3169 dev_err(dev, "kzalloc for ocv_config failed!\n");
3173 prop = of_find_property(regs, "ocv_table", &length);
3175 dev_err(dev, "ocv_table not found!\n");
3178 data->ocv_size = length / sizeof(u32);
3180 if (data->ocv_size > 0) {
3181 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3183 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3184 if (!data->battery_ocv) {
3185 dev_err(dev, "kzalloc for ocv_table failed!\n");
3188 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
3193 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3195 dev_err(dev, "max_charge_currentmA not found!\n");
3196 out_value = DEFAULT_ICUR;
3198 data->max_charger_currentmA = out_value;
3200 ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3202 dev_err(dev, "max_charger_ilimitmA not found!\n");
3203 out_value = DEFAULT_ILMT;
3205 data->max_charger_ilimitmA = out_value;
3207 ret = of_property_read_u32(regs, "bat_res", &out_value);
3209 dev_err(dev, "bat_res not found!\n");
3210 out_value = DEFAULT_BAT_RES;
3212 data->sense_resistor_mohm = out_value;
3214 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3216 dev_err(dev, "max_charge_voltagemV not found!\n");
3217 out_value = DEFAULT_VLMT;
3219 data->max_charger_voltagemV = out_value;
3221 ret = of_property_read_u32(regs, "design_capacity", &out_value);
3223 dev_err(dev, "design_capacity not found!\n");
3226 cell_cfg->design_capacity = out_value;
3228 ret = of_property_read_u32(regs, "design_qmax", &out_value);
3230 dev_err(dev, "design_qmax not found!\n");
3233 cell_cfg->design_qmax = out_value;
3235 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3237 dev_err(dev, "sleep_enter_current not found!\n");
3240 ocv_cfg->sleep_enter_current = out_value;
3242 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3244 dev_err(dev, "sleep_exit_current not found!\n");
3247 ocv_cfg->sleep_exit_current = out_value;
3249 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3251 cell_cfg->ocv = ocv_cfg;
3252 data->cell_cfg = cell_cfg;
3253 rk818->battery_data = data;
3255 DBG("\n--------- the battery OCV TABLE dump:\n");
3256 DBG("bat_res :%d\n", data->sense_resistor_mohm);
3257 DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3258 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3259 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3260 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3261 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3262 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3263 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3264 DBG("uboot chrg = %d\n", support_uboot_chrg);
3265 DBG("\n--------- rk818_battery dt_parse ok.\n");
3270 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3277 static int battery_probe(struct platform_device *pdev)
3279 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3280 struct battery_info *di;
3283 DBG("battery driver version %s\n", DRIVER_VERSION);
3284 di = kzalloc(sizeof(*di), GFP_KERNEL);
3286 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
3289 ret = rk_battery_parse_dt(chip, &pdev->dev);
3291 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
3295 platform_set_drvdata(pdev, di);
3296 battery_info_init(di, chip);
3297 if (!is_bat_exist(di)) {
3298 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
3303 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
3305 flatzone_voltage_init(di);
3306 battery_poweron_status_init(di);
3307 battery_power_supply_init(di);
3308 ret = battery_power_supply_register(di, &pdev->dev);
3310 dev_err(&pdev->dev, "rk power supply register failed!\n");
3313 di->wq = create_singlethread_workqueue("battery-work");
3314 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
3315 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
3316 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
3318 di->battery_nb.notifier_call = battery_notifier_call;
3319 register_battery_notifier(&di->battery_nb);
3321 rk818_battery_irq_init(di);
3322 rk818_battery_sysfs_init(di, &pdev->dev);
3323 DBG("------ RK81x battery_probe ok!-------\n");
3330 static int battery_suspend(struct platform_device *dev, pm_message_t state)
3332 struct battery_info *di = platform_get_drvdata(dev);
3334 enable_vbat_low_irq(di);
3335 di->sleep_status = di->status;
3336 di->suspend_charge_current = _get_average_current(di);
3338 /* avoid abrupt wakeup which will clean the variable*/
3339 if (di->sys_wakeup) {
3340 di->suspend_capacity = di->remain_capacity;
3341 di->suspend_temp_soc = _get_soc(di);
3342 di->suspend_time_start = get_seconds();
3343 di->sys_wakeup = false;
3346 cancel_delayed_work(&di->battery_monitor_work);
3347 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
3348 __func__, di->suspend_temp_soc, di->suspend_charge_current,
3349 di->suspend_capacity, di->sleep_status);
3354 static int battery_resume(struct platform_device *dev)
3356 struct battery_info *di = platform_get_drvdata(dev);
3359 DBG("<%s>\n", __func__);
3360 disable_vbat_low_irq(di);
3361 queue_delayed_work(di->wq, &di->battery_monitor_work,
3362 msecs_to_jiffies(TIMER_MS_COUNTS/2));
3364 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3366 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3370 static int battery_remove(struct platform_device *dev)
3372 struct battery_info *di = platform_get_drvdata(dev);
3374 cancel_delayed_work_sync(&di->battery_monitor_work);
3377 static void battery_shutdown(struct platform_device *dev)
3379 struct battery_info *di = platform_get_drvdata(dev);
3381 cancel_delayed_work_sync(&di->battery_monitor_work);
3382 DBG("rk818 shutdown!");
3386 static struct platform_driver battery_driver = {
3388 .name = "rk818-battery",
3389 .owner = THIS_MODULE,
3392 .probe = battery_probe,
3393 .remove = battery_remove,
3394 .suspend = battery_suspend,
3395 .resume = battery_resume,
3396 .shutdown = battery_shutdown,
3399 static int __init battery_init(void)
3401 return platform_driver_register(&battery_driver);
3404 fs_initcall_sync(battery_init);
3405 static void __exit battery_exit(void)
3407 platform_driver_unregister(&battery_driver);
3409 module_exit(battery_exit);
3411 MODULE_LICENSE("GPL");
3412 MODULE_ALIAS("platform:rk818-battery");
3413 MODULE_AUTHOR("ROCKCHIP");