4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/param.h>
11 #include <linux/jiffies.h>
12 #include <linux/workqueue.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include <linux/gpio.h>
21 #include <linux/proc_fs.h>
22 #include <asm/uaccess.h>
23 #include <linux/power/rk818_battery.h>
24 #include <linux/mfd/rk818.h>
25 #include <linux/time.h>
26 #include <linux/interrupt.h>
27 #include <linux/rtc.h>
28 #include <linux/wakelock.h>
30 /* if you want to disable, don't set it as 0, just be: "static int dbg_enable;" is ok*/
31 static int dbg_enable;
32 #define RK818_SYS_DBG 1
34 module_param_named(dbg_level, dbg_enable, int, 0644);
35 #define DBG(args...) \
43 #define INTERPOLATE_MAX 1000
44 #define MAX_INT 0x7FFF
45 #define TIME_10MIN_SEC 600
49 struct cell_state cell;
50 struct power_supply bat;
51 struct power_supply ac;
52 struct power_supply usb;
53 struct delayed_work work;
54 /* struct i2c_client *client; */
57 struct battery_platform_data *platform_data;
69 uint16_t relax_voltage;
73 bool pcb_ioffset_updated;
74 unsigned long queue_work_cnt;
75 uint16_t warnning_voltage;
88 int bat_res_update_cnt;
95 unsigned long dod0_time;
106 int current_k;/* (ICALIB0, ICALIB1) */
109 int voltage_k;/* VCALIB0 VCALIB1 */
120 struct timeval soc_timer;
121 struct timeval change_timer;
124 int charge_smooth_time;
126 int suspend_capacity;
128 struct timespec suspend_time;
129 struct timespec resume_time;
130 unsigned long suspend_time_start;
131 unsigned long count_sleep_time;
133 unsigned long dischrg_sum_sleep_sec;
134 unsigned long dischrg_sum_sleep_capacity;
135 int suspend_temp_soc;
137 int suspend_charge_current;
140 bool bat_res_updated;
141 bool charge_smooth_status;
143 unsigned long last_plugin_time;
146 unsigned long charging_time;
147 unsigned long discharging_time;
149 struct notifier_block battery_nb;
150 struct workqueue_struct *wq;
151 struct delayed_work battery_monitor_work;
152 struct delayed_work charge_check_work;
155 struct wake_lock resume_wake_lock;
157 int debug_finish_real_soc;
158 int debug_finish_temp_soc;
161 struct battery_info *data;
162 struct battery_info *g_battery;
163 u32 support_uboot_chrg;
165 extern int dwc_vbus_status(void);
166 extern int get_gadget_connect_flag(void);
167 extern int dwc_otg_check_dpdm(void);
168 extern void kernel_power_off(void);
169 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
170 extern unsigned int irq_create_mapping(struct irq_domain *domain,
171 irq_hw_number_t hwirq);
173 static void update_battery_info(struct battery_info *di);
175 #define SUPPORT_USB_CHARGE
178 static u32 interpolate(int value, u32 *table, int size)
183 for (i = 0; i < size; i++) {
184 if (value < table[i])
188 if ((i > 0) && (i < size)) {
189 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
190 d /= table[i] - table[i-1];
191 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
193 d = i * ((INTERPOLATE_MAX+size/2)/size);
201 /* Returns (a * b) / c */
202 static int32_t ab_div_c(u32 a, u32 b, u32 c)
208 sign = ((((a^b)^c) & 0x80000000) != 0);
214 tmp = ((int32_t) a*b + (c>>1)) / c;
226 static int32_t abs_int(int32_t x)
228 return (x > 0) ? x : -x;
231 static int abs32_int(int x)
233 return (x > 0) ? x : -x;
237 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
241 ret = rk818_i2c_read(rk818, reg, len, buf);
245 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
248 ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
251 static void dump_gauge_register(struct battery_info *di)
255 DBG("%s dump charger register start: \n", __func__);
256 for (i = 0xAC; i < 0xDF; i++) {
257 battery_read(di->rk818, i, &buf, 1);
258 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
263 static void dump_charger_register(struct battery_info *di)
268 DBG("%s dump the register start: \n", __func__);
269 for (i = 0x99; i < 0xAB; i++) {
270 battery_read(di->rk818, i, &buf, 1);
271 DBG(" the register is 0x%02x, the value is 0x%02x\n ", i, buf);
279 static uint16_t _get_OCV_voltage(struct battery_info *di);
280 static int _voltage_to_capacity(struct battery_info *di, int voltage);
281 static int _get_realtime_capacity(struct battery_info *di);
282 static void power_on_save(struct battery_info *di, int voltage);
283 static void _capacity_init(struct battery_info *di, u32 capacity);
284 static void battery_poweron_status_init(struct battery_info *di);
285 static void power_on_save(struct battery_info *di, int voltage);
286 static void flatzone_voltage_init(struct battery_info *di);
287 static int _get_FCC_capacity(struct battery_info *di);
288 static void _save_FCC_capacity(struct battery_info *di, u32 capacity);
289 static int _get_soc(struct battery_info *di);
290 static int _get_average_current(struct battery_info *di);
291 static int rk_battery_voltage(struct battery_info *di);
292 static uint16_t _get_relax_vol1(struct battery_info *di);
293 static uint16_t _get_relax_vol2(struct battery_info *di);
294 static void update_battery_info(struct battery_info *di);
296 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
298 struct battery_info *di = g_battery;
304 battery_read(di->rk818, SUP_STS_REG, &status, 1);
305 battery_read(di->rk818, SOC_REG, &soc_reg, 1);
306 battery_read(di->rk818, 0x00, &rtc_val, 1);
307 di->voltage_ocv = _get_OCV_voltage(di);
308 _voltage_to_capacity(di, di->voltage_ocv);
309 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
311 return sprintf(buf, "-----------------------------------------------------------------------------\n"
312 "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
313 "real_soc = %d, temp_soc = %d\n"
314 "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
315 "usb_online = %d, ac_online = %d\n"
316 "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
317 "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
318 "relax_volt1 = %d, relax_volt2 = %d\n"
319 "---------------------------------------------------------------------------\n",
320 rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
321 di->real_soc, _get_soc(di),
322 di->fcc, _get_FCC_capacity(di), shtd_time,
323 di->usb_online, di->ac_online,
325 di->voltage_k, di->voltage_b, soc_reg,
326 _get_relax_vol1(di), _get_relax_vol2(di));
329 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
331 struct battery_info *di = g_battery;
332 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
333 u8 usb_ctrl_reg, chrg_ctrl_reg1;
334 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
336 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
337 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
338 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
339 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
340 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
341 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
342 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
343 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
344 battery_read(di->rk818, 0x00, &rtc_val, 1);
346 return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
347 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
348 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
349 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
350 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
351 "---------------------------------------------------------------------------\n",
352 ggcon_reg, ggsts_reg, rtc_val,
353 sup_tst_reg, vb_mod_reg,
354 usb_ctrl_reg, chrg_ctrl_reg1,
355 chrg_ctrl_reg2, chrg_ctrl_reg3
358 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
360 struct battery_info *di = g_battery;
362 return sprintf(buf, "%d", di->fcc);
364 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
366 struct battery_info *di = g_battery;
368 return sprintf(buf, "%d", di->real_soc);
371 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
373 struct battery_info *di = g_battery;
375 return sprintf(buf, "%d", di->temp_soc);
378 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
380 struct battery_info *di = g_battery;
382 return sprintf(buf, "%d", di->voltage);
385 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
387 struct battery_info *di = g_battery;
389 return sprintf(buf, "%d", di->current_avg);
392 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
394 struct battery_info *di = g_battery;
396 return sprintf(buf, "%d", di->remain_capacity);
399 static struct device_attribute rk818_bat_attr[] = {
400 __ATTR(state, 0664, bat_state_read, NULL),
401 __ATTR(regs, 0664, bat_reg_read, NULL),
402 __ATTR(fcc, 0664, bat_fcc_read, NULL),
403 __ATTR(soc, 0664, bat_soc_read, NULL),
404 __ATTR(temp_soc, 0664, bat_temp_soc_read, NULL),
405 __ATTR(voltage, 0664, bat_voltage_read, NULL),
406 __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
407 __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
414 static int batt_table[22];
416 static ssize_t bat_param_read(struct device *dev, struct device_attribute *attr, char *buf)
420 for (i = 0; i < BATT_NUM; i++)
421 printk(KERN_INFO"i = %d batt_table = %d\n", i, batt_table[i]);
423 for (i = 0; i < BATT_NUM; i++)
424 printk(KERN_INFO"i = %d batt_table = %d\n", i + BATT_NUM , batt_table[i+BATT_NUM]);
428 static ssize_t bat_param_write(struct device *dev,
429 struct device_attribute *attr, const char *buf, size_t size)
435 DEVICE_ATTR(rk818batparam, 0664, bat_param_read, bat_param_write);
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 printk(KERN_INFO"get_relax_voltage relax voltage = %d\n", get_relax_voltage(data));
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");
736 static void ioffset_sample_time(struct battery_info *di, int time)
740 battery_read(di->rk818, GGCON, &ggcon, 1);
741 ggcon &= ~(0x30); /*clear <5:4>*/
743 battery_write(di->rk818, GGCON, &ggcon, 1);
744 debug_reg(di, GGCON, "GGCON");
747 static void update_cal_offset(struct battery_info *di)
749 int mod = di->queue_work_cnt % TIME_10MIN_SEC;
751 DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
752 if ((!mod) && (di->pcb_ioffset_updated)) {
753 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
754 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
759 static void zero_current_calibration(struct battery_info *di)
768 if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
770 for (retry = 0; retry < 5; retry++) {
771 adc_value = _get_raw_adc_current(di);
772 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
774 C0 = _get_cal_offset(di);
776 _set_cal_offset(di, C1);
777 DBG("<%s>. C1 = %d\n", __func__, C1);
780 adc_value = _get_raw_adc_current(di);
781 DBG("<%s>. adc_value = %d\n", __func__, adc_value);
784 ioffset = _get_ioffset(di);
785 pcb_offset = C1 - ioffset;
786 di->pcb_ioffset = pcb_offset;
787 di->pcb_ioffset_updated = true;
788 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
791 di->pcb_ioffset_updated = false;
797 static bool _is_relax_mode(struct battery_info *di)
802 ret = battery_read(di->rk818, GGSTS, &status, 1);
804 if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
810 static uint16_t get_relax_voltage(struct battery_info *di)
814 uint16_t relax_vol1, relax_vol2;
817 ret = battery_read(di->rk818, GGSTS, &status, 1);
818 ret = battery_read(di->rk818, GGCON, &ggcon, 1);
820 relax_vol1 = _get_relax_vol1(di);
821 relax_vol2 = _get_relax_vol2(di);
822 DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
823 if (_is_relax_mode(di))
824 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
829 static void _set_relax_thres(struct battery_info *di)
832 int enter_thres, exit_thres;
833 struct cell_state *cell = &di->cell;
835 enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
836 exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
838 buf = enter_thres&0xff;
839 battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
840 buf = (enter_thres>>8)&0xff;
841 battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
843 buf = exit_thres&0xff;
844 battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
845 buf = (exit_thres>>8)&0xff;
846 battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
848 /* set sample time */
849 battery_read(di->rk818, GGCON, &buf, 1);
850 buf &= ~(3<<2);/*8min*/
851 buf &= ~0x01; /* clear bat_res calc*/
852 battery_write(di->rk818, GGCON, &buf, 1);
855 static void restart_relax(struct battery_info *di)
857 u8 ggcon;/* chrg_ctrl_reg2;*/
860 battery_read(di->rk818, GGCON, &ggcon, 1);
862 battery_write(di->rk818, GGCON, &ggcon, 1);
864 battery_read(di->rk818, GGSTS, &ggsts, 1);
866 battery_write(di->rk818, GGSTS, &ggsts, 1);
869 static int _get_average_current(struct battery_info *di)
876 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
878 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
882 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
884 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
887 current_now |= (buf<<8);
889 if (current_now & 0x800)
892 temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
898 static bool _is_first_poweron(struct battery_info *di)
903 battery_read(di->rk818, GGSTS, &buf, 1);
904 DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
905 /*di->pwron_bat_con = buf;*/
909 battery_write(di->rk818, GGSTS, &buf, 1);
910 battery_read(di->rk818, GGSTS, &temp, 1);
911 } while (temp&BAT_CON);
916 static void flatzone_voltage_init(struct battery_info *di)
923 ocv_table = di->platform_data->battery_ocv;
924 ocv_size = di->platform_data->ocv_size;
926 for (j = 0; j < 21; j++)
930 for (i = 1; i < ocv_size-1; i++) {
931 if (ocv_table[i+1] < ocv_table[i] + 20)
935 temp_table[j] = temp_table[j-1]+1;
937 di->enter_flatzone = ocv_table[i];
941 for (i = 0; i <= 20; i++) {
942 if (temp_table[i] < temp_table[i+1])
947 di->exit_flatzone = ocv_table[i];
949 DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
954 static int is_not_flatzone(struct battery_info *di, int voltage)
956 if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
957 DBG("<%s>. is in flat zone\n", __func__);
960 DBG("<%s>. is not in flat zone\n", __func__);
965 static void power_on_save(struct battery_info *di, int voltage)
970 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
972 if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
973 _voltage_to_capacity(di, voltage);
974 if (di->temp_soc < 20) {
975 di->dod0_voltage = voltage;
976 di->dod0_capacity = di->nac;
978 di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
981 if (di->temp_soc <= 0)
982 di->dod0_level = 100;
983 else if (di->temp_soc < 5)
985 else if (di->temp_soc < 10)
987 /* save_soc = di->dod0_level; */
988 save_soc = get_level(di);
989 if (save_soc < di->dod0_level)
990 save_soc = di->dod0_level;
991 save_level(di, save_soc);
992 DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
999 static int _get_soc(struct battery_info *di)
1001 return di->remain_capacity * 100 / di->fcc;
1004 static enum power_supply_property rk_battery_props[] = {
1006 POWER_SUPPLY_PROP_STATUS,
1007 POWER_SUPPLY_PROP_CURRENT_NOW,
1008 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1009 POWER_SUPPLY_PROP_PRESENT,
1010 POWER_SUPPLY_PROP_HEALTH,
1011 POWER_SUPPLY_PROP_CAPACITY,
1014 #define to_device_info(x) container_of((x), \
1015 struct battery_info, bat)
1017 static int rk_battery_get_property(struct power_supply *psy,
1018 enum power_supply_property psp,
1019 union power_supply_propval *val)
1021 struct battery_info *di = to_device_info(psy);
1024 case POWER_SUPPLY_PROP_CURRENT_NOW:
1025 val->intval = di->current_avg*1000;/*uA*/
1028 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1029 val->intval = di->voltage*1000;/*uV*/
1032 case POWER_SUPPLY_PROP_PRESENT:
1033 val->intval = val->intval <= 0 ? 0 : 1;
1036 case POWER_SUPPLY_PROP_CAPACITY:
1037 val->intval = di->real_soc;
1040 case POWER_SUPPLY_PROP_HEALTH:
1041 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1044 case POWER_SUPPLY_PROP_STATUS:
1045 val->intval = di->status;
1056 static enum power_supply_property rk_battery_ac_props[] = {
1057 POWER_SUPPLY_PROP_ONLINE,
1059 static enum power_supply_property rk_battery_usb_props[] = {
1060 POWER_SUPPLY_PROP_ONLINE,
1064 #define to_ac_device_info(x) container_of((x), \
1065 struct battery_info, ac)
1067 static int rk_battery_ac_get_property(struct power_supply *psy,
1068 enum power_supply_property psp,
1069 union power_supply_propval *val)
1072 struct battery_info *di = to_ac_device_info(psy);
1075 case POWER_SUPPLY_PROP_ONLINE:
1076 val->intval = di->ac_online; /*discharging*/
1086 #define to_usb_device_info(x) container_of((x), \
1087 struct battery_info, usb)
1089 static int rk_battery_usb_get_property(struct power_supply *psy,
1090 enum power_supply_property psp,
1091 union power_supply_propval *val)
1094 struct battery_info *di = to_usb_device_info(psy);
1097 case POWER_SUPPLY_PROP_ONLINE:
1098 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1101 val->intval = di->usb_online;
1113 static void battery_power_supply_init(struct battery_info *di)
1115 di->bat.name = "BATTERY";
1116 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1117 di->bat.properties = rk_battery_props;
1118 di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1119 di->bat.get_property = rk_battery_get_property;
1122 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1123 di->ac.properties = rk_battery_ac_props;
1124 di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1125 di->ac.get_property = rk_battery_ac_get_property;
1127 di->usb.name = "USB";
1128 di->usb.type = POWER_SUPPLY_TYPE_USB;
1129 di->usb.properties = rk_battery_usb_props;
1130 di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1131 di->usb.get_property = rk_battery_usb_get_property;
1134 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1138 ret = power_supply_register(dev, &di->bat);
1140 dev_err(dev, "failed to register main battery\n");
1143 ret = power_supply_register(dev, &di->usb);
1145 dev_err(dev, "failed to register usb power supply\n");
1148 ret = power_supply_register(dev, &di->ac);
1150 dev_err(dev, "failed to register ac power supply\n");
1157 power_supply_unregister(&di->ac);
1159 power_supply_unregister(&di->usb);
1161 power_supply_unregister(&di->bat);
1167 /*calc battery resister*/
1168 static void res_mode_init(struct battery_info *di)
1170 u8 ggcon;/* chrg_ctrl_reg2; */
1173 battery_read(di->rk818, GGCON, &ggcon, 1);
1174 battery_read(di->rk818, GGSTS, &ggsts, 1);
1179 battery_write(di->rk818, GGCON, &ggcon, 1);
1180 battery_write(di->rk818, GGSTS, &ggsts, 1);
1184 static void _capacity_init(struct battery_info *di, u32 capacity)
1191 di->voltage_old = 0;
1192 di->display_soc = 0;
1194 capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1196 buf = (capacity_ma>>24)&0xff;
1197 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1198 buf = (capacity_ma>>16)&0xff;
1199 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1200 buf = (capacity_ma>>8)&0xff;
1201 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1202 buf = (capacity_ma&0xff) | 0x01;
1203 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1204 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1210 static void _save_remain_capacity(struct battery_info *di, u32 capacity)
1215 if (capacity >= di->qmax)
1216 capacity = di->qmax;
1218 capacity_ma = capacity;
1220 buf = (capacity_ma>>24)&0xff;
1221 battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1222 buf = (capacity_ma>>16)&0xff;
1223 battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1224 buf = (capacity_ma>>8)&0xff;
1225 battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1226 buf = (capacity_ma&0xff) | 0x01;
1227 battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1230 static int _get_remain_capacity(struct battery_info *di)
1237 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1239 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1241 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1243 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1246 capacity = temp;/* /4096*900/14/36*500/521; */
1252 static void _save_FCC_capacity(struct battery_info *di, u32 capacity)
1257 capacity_ma = capacity;
1258 buf = (capacity_ma>>24)&0xff;
1259 battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1260 buf = (capacity_ma>>16)&0xff;
1261 battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1262 buf = (capacity_ma>>8)&0xff;
1263 battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1264 buf = (capacity_ma&0xff) | 0x01;
1265 battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1268 static int _get_FCC_capacity(struct battery_info *di)
1275 ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1277 ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1279 ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1281 ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1285 capacity = temp-1;/* 4096*900/14/36*500/521 */
1288 DBG("%s NEW_FCC_REG %d capacity = %d\n", __func__, temp, capacity);
1293 static int _get_realtime_capacity(struct battery_info *di)
1300 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1302 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1304 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1306 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1309 capacity = temp/2390;/* 4096*900/14/36*500/521; */
1314 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1316 int remain_capacity;
1324 now_temp_soc = _get_soc(di);
1325 _voltage_to_capacity(di, relax_voltage);
1326 relax_soc = di->temp_soc;
1327 relax_capacity = di->temp_nac;
1328 abs_soc = abs32_int(relax_soc - now_temp_soc);
1330 DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1331 DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1334 if (abs32_int(di->real_soc - relax_soc) <= 5) {
1335 remain_capacity = relax_capacity;
1336 DBG("<%s>. real-soc is close to relax-soc, set: temp_soc = relax_soc\n", __func__);
1339 remain_capacity = _get_realtime_capacity(di);
1340 else if (abs_soc <= 10)
1341 remain_capacity = relax_capacity;
1342 else if (abs_soc <= 20)
1343 remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1345 remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1347 _capacity_init(di, remain_capacity);
1348 di->temp_soc = _get_soc(di);
1349 di->remain_capacity = _get_realtime_capacity(di);
1352 DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1353 if (relax_soc < now_temp_soc) {
1354 if (di->suspend_temp_soc - relax_soc <= 5)
1355 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1356 else if (di->suspend_temp_soc - relax_soc <= 10)
1357 di->real_soc = di->real_soc - 5;
1359 di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1361 now_current = _get_average_current(di);
1362 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1363 min = soc_time / 60;
1364 if (sleep_min > min)
1368 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);
1372 static int _copy_soc(struct battery_info *di, u8 save_soc)
1377 battery_write(di->rk818, SOC_REG, &soc, 1);
1381 static bool support_uboot_charge(void)
1383 return support_uboot_chrg?true:false;
1386 static int _rsoc_init(struct battery_info *di)
1390 u32 remain_capacity;
1393 #ifdef SUPPORT_USB_CHARGE
1398 di->voltage = rk_battery_voltage(di);
1399 di->voltage_ocv = _get_OCV_voltage(di);
1400 DBG("OCV voltage = %d\n" , di->voltage_ocv);
1402 if (_is_first_poweron(di)) {
1403 _save_FCC_capacity(di, di->design_capacity);
1404 di->fcc = _get_FCC_capacity(di);
1406 _voltage_to_capacity(di, di->voltage_ocv);
1407 di->real_soc = di->temp_soc;
1408 di->nac = di->temp_nac;
1409 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1412 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1413 init_soc = pwron_soc;
1414 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1416 #ifdef SUPPORT_USB_CHARGE
1417 otg_status = dwc_otg_check_dpdm();
1418 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1420 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1423 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1424 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1426 battery_write(di->rk818, SOC_REG, &init_soc, 1);
1429 remain_capacity = _get_remain_capacity(di);
1431 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1432 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1433 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1434 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1436 if (!support_uboot_charge()) {
1437 _voltage_to_capacity(di, di->voltage_ocv);
1438 DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1440 /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1441 if (((otg_status != 0) && (curr_shtd_time > 0) && (last_shtd_time != curr_shtd_time)) || ((curr_shtd_time > 0) && (otg_status == 0))) {
1443 if (curr_shtd_time > 30) {
1444 remain_capacity = di->temp_nac;
1445 DBG("<%s>shutdown_time > 30 minute, remain_cap = %d\n", __func__, remain_capacity);
1447 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - di->real_soc) >= 10)) {
1448 if (remain_capacity >= di->temp_nac*120/100)
1449 remain_capacity = di->temp_nac*110/100;
1450 else if (remain_capacity < di->temp_nac*8/10)
1451 remain_capacity = di->temp_nac*9/10;
1453 DBG("<%s> shutdown_time > 3 minute, remain_cap = %d\n", __func__, remain_capacity);
1458 di->real_soc = init_soc;
1459 di->nac = remain_capacity;
1462 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1468 static u8 get_charge_status(struct battery_info *di)
1473 battery_read(di->rk818, SUP_STS_REG, &status, 1);
1478 DBG(" CHARGE-OFF ...\n");
1483 DBG(" DEAD CHARGE ...\n");
1486 case TRICKLE_CHARGE: /* (0x02 << 4) */
1488 DBG(" TRICKLE CHARGE ...\n ");
1491 case CC_OR_CV: /* (0x03 << 4) */
1493 DBG(" CC or CV ...\n");
1496 case CHARGE_FINISH: /* (0x04 << 4) */
1497 ret = CHARGE_FINISH;
1498 DBG(" CHARGE FINISH ...\n");
1501 case USB_OVER_VOL: /* (0x05 << 4) */
1503 DBG(" USB OVER VOL ...\n");
1506 case BAT_TMP_ERR: /* (0x06 << 4) */
1508 DBG(" BAT TMP ERROR ...\n");
1511 case TIMER_ERR: /* (0x07 << 4) */
1513 DBG(" TIMER ERROR ...\n");
1516 case USB_EXIST: /* (1 << 1)// usb is exists */
1518 DBG(" USB EXIST ...\n");
1521 case USB_EFF: /* (1 << 0)// usb is effective */
1523 DBG(" USB EFF...\n");
1533 static void set_charge_current(struct battery_info *di, int charge_current)
1537 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1538 usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1539 usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1540 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1543 static void rk_battery_charger_init(struct battery_info *di)
1545 u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1548 DBG("%s start\n", __func__);
1549 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1550 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1551 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1552 battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1553 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1555 DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1556 usb_ctrl_reg &= (~0x0f);
1557 #ifdef SUPPORT_USB_CHARGE
1558 usb_ctrl_reg |= (VLIM_4400MV | ILIM_45MA | CHRG_CT_EN);
1560 usb_ctrl_reg |= (VLIM_4400MV | ILIM_3000MA) | CHRG_CT_EN);
1562 chrg_ctrl_reg1 &= (0x00);
1563 chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
1565 chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1566 chrg_ctrl_reg2 &= ~(0xc0);
1567 chrg_ctrl_reg2 |= FINISH_100MA;
1569 sup_sts_reg &= ~(0x01 << 3);
1570 sup_sts_reg |= (0x01 << 2);
1572 battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1573 battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1574 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1575 battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1576 battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1578 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1579 debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1580 debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1581 debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1583 DBG("%s end\n", __func__);
1586 void charge_disable_open_otg(int value)
1588 struct battery_info *di = g_battery;
1591 DBG("charge disable, enable OTG.\n");
1592 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1593 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /* enable OTG */
1596 DBG("charge enable, disable OTG.\n");
1597 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1598 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1602 static void low_waring_init(struct battery_info *di)
1607 battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1609 /* 2.8v~3.5v, interrupt */
1610 vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06); /* 3400mV*/
1611 battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1614 static void fg_init(struct battery_info *di)
1618 adc_ctrl_val = 0x30;
1619 battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1622 /* get the volatege offset */
1623 _get_voltage_offset_value(di);
1624 rk_battery_charger_init(di);
1625 _set_relax_thres(di);
1626 /* get the current offset , the value write to the CAL_OFFSET */
1627 di->current_offset = _get_ioffset(di);
1628 _set_cal_offset(di, di->current_offset+42);
1630 _capacity_init(di, di->nac);
1632 di->remain_capacity = _get_realtime_capacity(di);
1633 di->current_avg = _get_average_current(di);
1635 low_waring_init(di);
1637 power_on_save(di, di->voltage_ocv);
1638 /* set sample time for cal_offset interval*/
1639 ioffset_sample_time(di, SAMP_TIME_8MIN);
1640 dump_gauge_register(di);
1641 dump_charger_register(di);
1644 "nac = %d , remain_capacity = %d\n"
1645 "OCV_voltage = %d, voltage = %d\n"
1646 "SOC = %d, fcc = %d\n",
1648 di->nac, di->remain_capacity,
1649 di->voltage_ocv, di->voltage,
1650 di->real_soc, di->fcc);
1654 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1655 static void zero_get_soc(struct battery_info *di)
1657 int ocv_voltage, check_voltage;
1658 int temp_soc = -1, real_soc;
1659 int currentold, currentnow, voltage;
1664 DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1665 /* if (di->voltage < 3600)//di->warnning_voltage) */
1667 /* DBG("+++++++zero mode++++++++displaysoc+++++++++\n"); */
1669 currentold = _get_average_current(di);
1670 _get_cal_offset(di);
1673 currentnow = _get_average_current(di);
1675 } while ((currentold == currentnow) && (count_num < 11));
1678 for (i = 0; i < 10 ; i++)
1679 voltage += rk_battery_voltage(di);
1682 if (di->voltage_old == 0)
1683 di->voltage_old = voltage;
1684 voltage_k = voltage;
1685 voltage = (di->voltage_old*2 + 8*voltage)/10;
1686 di->voltage_old = voltage;
1687 /* DBG("Zero: voltage = %d\n", voltage); */
1689 currentnow = _get_average_current(di);
1690 /* DBG(" zero: current = %d, voltage = %d\n", currentnow, voltage); */
1692 ocv_voltage = 3400 + abs32_int(currentnow)*200/1000;
1693 check_voltage = voltage + abs32_int(currentnow)*(200 - 65)/1000; /* 65 mo power-path mos */
1694 _voltage_to_capacity(di, check_voltage);
1695 /* if ((di->remain_capacity > di->nac) && (update_q == 0)) */
1696 /* DBG(" xxx Zerro: tui suan OCV cap :%d\n", di->temp_nac); */
1697 di->update_q = di->remain_capacity - di->temp_nac;
1698 /* update_q = di->temp_nac; */
1700 /* DBG("Zero: update_q = %d , remain_capacity = %d, temp_nac = %d\n ", di->update_q, di->remain_capacity, di->temp_nac); */
1701 /* relax_volt_update_remain_capacity(di, 3600 + abs32_int(di->current_avg)*200/1000); */
1703 _voltage_to_capacity(di, ocv_voltage);
1705 temp_soc = _get_soc(di); */
1706 if (di->display_soc == 0)
1707 di->display_soc = di->real_soc*1000;
1709 real_soc = di->display_soc;
1710 /* DBG(" Zerro: Q (err) cap :%d\n", di->temp_nac);
1711 DBG(" ZERO : real-soc = %d\n ", di->real_soc); */
1712 DBG("ZERO : ocv_voltage = %d, check_voltage = %d\n ", ocv_voltage, check_voltage);
1713 if (di->remain_capacity > di->temp_nac + di->update_q) {
1715 if (di->update_k == 0 || di->update_k >= 10) {
1716 /* DBG("one..\n"); */
1717 if (di->update_k == 0) {
1718 di->line_q = di->temp_nac + di->update_q; /* ZQ = Q_ded + Qerr */
1719 /* line_q = update_q - di->temp_nac; */
1720 temp_soc = (di->remain_capacity - di->line_q)*1000/di->fcc;/* (RM - ZQ) / FCC = r0 = R0 ; */
1721 /* temp_soc = (line_q)*1000/di->fcc;//(RM - ZQ) / FCC = r0 = R0 ;*
1722 /di->line_k = (real_soc*1000 + temp_soc/2)/temp_soc;//k0 = y0/x0 */
1723 di->line_k = (real_soc + temp_soc/2)/temp_soc;/* k0 = y0/x0 */
1724 /* DBG("Zero: one link = %d realsoc = %d , temp_soc = %d\n", di->line_k, di->real_soc, temp_soc); */
1730 line_q = di->temp_nac + update_q;
1732 /* DBG("two...\n"); */
1733 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* x1 10 */
1735 temp_soc = (line_q)*1000/di->fcc;// x1
1736 real_soc = (di->line_k*temp_soc+500)/1000; //y1 = k0*x1
1738 real_soc = (di->line_k*temp_soc); /* y1 = k0*x1 */
1739 /* DBG("Zero: two link = %d realsoc = %d , temp_soc = %d\n", di->line_k, real_soc, temp_soc); */
1740 di->display_soc = real_soc;
1741 /* if (real_soc != di->real_soc) */
1742 if ((real_soc+500)/1000 < di->real_soc)
1745 DBG("Zero two di->real_soc = %d\n", di->real_soc);
1746 DBG("Zero : temp_soc : %d\n", real_soc);
1748 _voltage_to_capacity(di, ocv_voltage);
1749 di->line_q = di->temp_nac + di->update_q; /* Q1 */
1750 /* line_q = update_q - di->temp_nac; */
1751 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* z1 */
1753 temp_soc = (line_q)*1000/di->fcc;
1754 di->line_k = (di->real_soc*1000 + temp_soc/2)/temp_soc; //k1 = y1/z1
1756 di->line_k = (di->display_soc + temp_soc/2)/temp_soc; /* k1 = y1/z1 */
1757 /* DBG("Zero: two link = %d display_soc = %d , temp_soc = %d\n", di->line_k, di->display_soc, temp_soc); */
1758 /* line_q = di->temp_nac + update_q;// Q1 */
1766 /* DBG("di->remain_capacity = %d, line_q = %d\n ", di->remain_capacity, di->line_q); */
1769 if (di->update_k == 1 || di->update_k != 10) {
1770 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc;/* x */
1771 di->display_soc = di->line_k*temp_soc;
1772 /* if (((di->line_k*temp_soc+500)/1000) != di->real_soc), */
1773 DBG("ZERO : display-soc = %d, real-soc = %d\n", di->display_soc, di->real_soc);
1774 if ((di->display_soc+500)/1000 < di->real_soc)
1776 /* di->real_soc = (line_k*temp_soc+500)/1000 ;//y = k0*x */
1779 /* DBG("three..\n"); */
1781 if (di->update_k > 10) {
1787 DBG("ZERO : update_k = %d\n", di->update_k);
1788 DBG("ZERO : remain_capacity = %d , nac = %d, update_q = %d\n", di->remain_capacity, di->line_q, di->update_q);
1789 DBG("ZERO : Warnning_voltage = %d, line_k = %d, temp_soc = %d real_soc = %d\n\n", di->warnning_voltage, di->line_k, temp_soc, di->real_soc);
1794 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1797 int now_current, soc_time = -1;
1800 voltage = di->voltage;
1801 now_current = di->current_avg;
1802 soc_time = di->fcc*3600/100/(abs_int(now_current));
1803 _voltage_to_capacity(di, 3800);
1804 volt_to_soc = di->temp_soc;
1805 di->temp_soc = _get_soc(di);
1807 DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
1808 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1809 if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) { /* di->warnning_voltage) */
1813 } else if (di->temp_soc == di->real_soc) {
1814 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1815 } else if (di->temp_soc > di->real_soc) {
1816 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1817 di->vol_smooth_time++;
1818 if (di->vol_smooth_time > soc_time*3) {
1820 di->vol_smooth_time = 0;
1824 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1825 if (di->real_soc == (di->temp_soc + 1)) {
1826 di->change_timer = di->soc_timer;
1827 di->real_soc = di->temp_soc;
1829 di->vol_smooth_time++;
1830 if (di->vol_smooth_time > soc_time/3) {
1832 di->vol_smooth_time = 0;
1837 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1838 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
1841 static int get_charging_time(struct battery_info *di)
1843 return (di->charging_time/60);
1846 static int get_discharging_time(struct battery_info *di)
1848 return (di->discharging_time/60);
1850 static void dump_debug_info(struct battery_info *di)
1852 u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
1853 u8 usb_ctrl_reg, chrg_ctrl_reg1;
1854 u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
1856 battery_read(di->rk818, GGCON, &ggcon_reg, 1);
1857 battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
1858 battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
1859 battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
1860 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1861 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1862 battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1863 battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1864 battery_read(di->rk818, 0x00, &rtc_val, 1);
1866 DBG("\n------------- dump_debug_regs -----------------\n"
1867 "GGCON = 0x%2x, GGSTS = 0x%2x, RTC = 0x%2x\n"
1868 "SUP_STS_REG = 0x%2x, VB_MOD_REG = 0x%2x\n"
1869 "USB_CTRL_REG = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
1870 "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
1871 ggcon_reg, ggsts_reg, rtc_val,
1872 sup_tst_reg, vb_mod_reg,
1873 usb_ctrl_reg, chrg_ctrl_reg1,
1874 chrg_ctrl_reg2, chrg_ctrl_reg3
1878 "########################## [read] ################################\n"
1879 "info: 3.4v low warning, digital 100mA finish, 4.2v, 1.6A\n"
1880 "-----------------------------------------------------------------\n"
1881 "realx-voltage = %d, voltage = %d, current-avg = %d\n"
1882 "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
1883 "diplay_soc = %d, cpapacity_soc = %d\n"
1884 "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
1885 "finish_real_soc = %d, finish_temp_soc = %d\n"
1886 "chrg_time = %d, dischrg_time = %d\n",
1887 get_relax_voltage(di),
1888 di->voltage, di->current_avg,
1889 di->fcc, di->remain_capacity, _get_OCV_voltage(di),
1890 di->real_soc, _get_soc(di),
1891 di->ac_online, di->usb_online, di->status,
1892 di->debug_finish_real_soc, di->debug_finish_temp_soc,
1893 get_charging_time(di), get_discharging_time(di)
1895 get_charge_status(di);
1896 DBG("################################################################\n");
1900 static void update_fcc_capacity(struct battery_info *di)
1902 if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
1903 if (get_level(di) >= di->dod0_level) {
1904 di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
1905 if (di->fcc > di->qmax)
1908 _capacity_init(di, di->fcc);
1909 _save_FCC_capacity(di, di->fcc);
1911 di->dod0_status = 0;
1915 static void debug_get_finish_soc(struct battery_info *di)
1917 if (di->charge_status == CHARGE_FINISH) {
1918 di->debug_finish_real_soc = di->real_soc;
1919 di->debug_finish_temp_soc = di->temp_soc;
1923 static void wait_charge_finish_signal(struct battery_info *di)
1925 if (di->charge_status == CHARGE_FINISH)
1926 update_fcc_capacity(di);/* save new fcc*/
1929 debug_get_finish_soc(di);
1932 static void charge_finish_routine(struct battery_info *di)
1934 if (di->charge_status == CHARGE_FINISH) {
1935 _capacity_init(di, di->fcc);
1936 zero_current_calibration(di);
1938 if (di->real_soc < 100) {
1939 DBG("<%s>,CHARGE_FINISH di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
1940 if ((di->soc_counter < 80)) {
1943 di->soc_counter = 0;
1950 static void voltage_to_soc_charge_smooth(struct battery_info *di)
1952 int now_current, soc_time;
1954 now_current = _get_average_current(di);
1955 soc_time = di->fcc*3600/100/(abs_int(now_current)); /* 1% time; */
1956 di->temp_soc = _get_soc(di);
1958 DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1960 if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
1961 di->charge_smooth_time++;
1963 if (di->charge_smooth_time > soc_time/3) {
1965 di->charge_smooth_time = 0;
1967 di->charge_smooth_status = true;
1970 if (di->real_soc == di->temp_soc) {
1971 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1972 di->temp_soc = _get_soc(di);
1974 if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
1976 if (di->temp_soc < di->real_soc + 1) {
1977 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1978 di->charge_smooth_time++;
1979 if (di->charge_smooth_time > soc_time*2) {
1981 di->charge_smooth_time = 0;
1983 di->charge_smooth_status = true;
1986 else if (di->temp_soc > di->real_soc + 1) {
1987 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1988 di->charge_smooth_time++;
1989 if (di->charge_smooth_time > soc_time/3) {
1991 di->charge_smooth_time = 0;
1993 di->charge_smooth_status = true;
1995 } else if (di->temp_soc == di->real_soc + 1) {
1996 DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
1997 if (di->charge_smooth_status) {
1998 di->charge_smooth_time++;
1999 if (di->charge_smooth_time > soc_time/3) {
2000 di->real_soc = di->temp_soc;
2001 di->charge_smooth_time = 0;
2002 di->charge_smooth_status = false;
2006 di->real_soc = di->temp_soc;
2007 di->charge_smooth_status = false;
2013 DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2014 DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2017 static void rk_battery_display_smooth(struct battery_info *di)
2022 status = di->status;
2023 charge_status = di->charge_status;
2024 if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2026 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2027 voltage_to_soc_discharge_smooth(di);
2029 voltage_to_soc_charge_smooth(di);
2031 } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2032 voltage_to_soc_discharge_smooth(di);
2033 if (di->real_soc == 1) {
2035 if (di->time2empty >= 300)
2045 static void software_recharge(struct battery_info *di, int max_cnt)
2047 static int recharge_cnt;
2050 if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2051 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2052 chrg_ctrl_reg1 &= ~(1 << 7);
2053 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2054 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2055 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2057 chrg_ctrl_reg1 |= (1 << 7);
2058 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2059 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2060 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2068 static int estimate_battery_resister(struct battery_info *di)
2071 int avr_voltage1 = 0, avr_current1;
2072 int avr_voltage2 = 0, avr_current2;
2074 int bat_res, ocv_votage;
2075 static unsigned long last_time;
2076 unsigned long delta_time;
2077 int charge_ocv_voltage1, charge_ocv_voltage2;
2078 int charge_ocv_soc1, charge_ocv_soc2;
2080 delta_time = get_seconds() - last_time;
2081 DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2082 if (delta_time >= 20) {/*20s*/
2085 set_charge_current(di, ILIM_45MA);/*450mA*/
2087 for (i = 0; i < 10 ; i++) {
2089 avr_voltage1 += rk_battery_voltage(di);
2092 avr_current1 = _get_average_current(di);
2093 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2094 DBG("------------------------------------------------------------------------------------------\n");
2095 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2098 set_charge_current(di, ILIM_3000MA);
2100 for (i = 0; i < 10 ; i++) {
2102 avr_voltage2 += rk_battery_voltage(di);
2105 avr_current2 = _get_average_current(di);
2106 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2107 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2109 /*calc resister and ocv_votage ocv*/
2110 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2111 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2112 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2114 /*calc sample voltage ocv*/
2115 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2116 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2117 _voltage_to_capacity(di, charge_ocv_voltage1);
2118 charge_ocv_soc1 = di->temp_soc;
2119 _voltage_to_capacity(di, charge_ocv_voltage2);
2120 charge_ocv_soc2 = di->temp_soc;
2122 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2123 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2124 DBG("------------------------------------------------------------------------------------------\n");
2125 last_time = get_seconds();
2135 static int update_battery_resister(struct battery_info *di)
2139 if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2141 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2142 tmp_res = estimate_battery_resister(di);
2144 di->bat_res_update_cnt++;
2145 di->bat_res += tmp_res;
2146 DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2147 if (di->bat_res_update_cnt == 10) {
2148 di->bat_res_updated = true;
2151 DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2160 static void charge_soc_check_routine(struct battery_info *di)
2168 if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2169 min = get_charging_time(di);
2170 update_battery_resister(di);
2172 if ((min >= 30) && (di->bat_res_updated)) {
2174 old_temp_soc = di->temp_soc;
2175 ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2176 _voltage_to_capacity(di, ocv_voltage);
2177 ocv_temp_soc = di->temp_soc;
2179 DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2180 DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2181 DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2183 if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2184 di->temp_soc = ocv_temp_soc;
2186 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2188 remain_capcity = di->temp_soc * di->fcc / 100;
2189 _capacity_init(di, remain_capcity);
2190 di->remain_capacity = _get_realtime_capacity(di);
2191 DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2199 static void update_resume_status_relax_voltage(struct battery_info *di)
2201 unsigned long sleep_soc;
2202 unsigned long sum_sleep_soc;
2203 unsigned long sleep_sec;
2208 int sum_sleep_avr_current;
2212 update_battery_info(di);
2214 di->sys_wakeup = true;
2216 DBG("<%s>, resume----------checkstart\n", __func__);
2217 sleep_sec = get_seconds() - di->suspend_time_start;
2218 sleep_min = sleep_sec / 60;
2220 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2221 __func__, sleep_sec, sleep_min);
2223 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2224 DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2226 delta_capacity = di->suspend_capacity - di->remain_capacity;
2227 delta_soc = di->suspend_temp_soc - _get_soc(di);
2228 di->dischrg_sum_sleep_capacity += delta_capacity;
2229 di->dischrg_sum_sleep_sec += sleep_sec;
2231 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2232 sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2234 DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2235 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2236 DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2237 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2238 DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2239 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2240 DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2242 /*large suspend current*/
2243 if (sum_sleep_avr_current > 20) {
2244 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2245 di->real_soc -= sum_sleep_soc;
2246 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2247 __func__, sum_sleep_soc, di->real_soc);
2249 /* small suspend current*/
2250 } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2252 relax_voltage = get_relax_voltage(di);
2253 di->voltage = rk_battery_voltage(di);
2255 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2256 DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2257 relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2260 DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2261 if (sum_sleep_soc > 0)
2262 di->real_soc -= sum_sleep_soc;
2266 if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2267 di->dischrg_sum_sleep_capacity = 0;
2268 di->dischrg_sum_sleep_sec = 0;
2270 DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2271 DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2274 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2275 DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2276 if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2277 di->temp_soc = _get_soc(di);
2278 charge_status = get_charge_status(di);
2280 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2281 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2282 DBG("resume, sleep : ac online charge current < 1000\n");
2283 if (sleep_sec > 0) {
2284 di->count_sleep_time += sleep_sec;
2285 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2286 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2288 di->count_sleep_time = 0;
2289 di->real_soc += sleep_soc;
2290 if (di->real_soc > 100)
2295 DBG("<%s>, usb charging\n", __func__);
2296 if (di->suspend_temp_soc + 15 < di->temp_soc)
2297 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2299 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2302 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);
2309 #ifdef SUPPORT_USB_CHARGE
2310 static int get_charging_status_type(struct battery_info *di)
2312 int otg_status = dwc_otg_check_dpdm();
2314 if (0 == otg_status) {
2317 di->check_count = 0;
2319 } else if (1 == otg_status) {
2320 if (0 == get_gadget_connect_flag()) {
2321 if (++di->check_count >= 5) {
2333 } else if (2 == otg_status) {
2336 di->check_count = 0;
2339 if (di->ac_online == 1)
2340 set_charge_current(di, ILIM_3000MA);
2342 set_charge_current(di, ILIM_45MA);
2348 static void battery_poweron_status_init(struct battery_info *di)
2352 #ifndef SUPPORT_USB_CHARGE
2356 #ifdef SUPPORT_USB_CHARGE
2358 otg_status = dwc_otg_check_dpdm();
2359 if (otg_status == 1) {
2362 set_charge_current(di, ILIM_45MA);
2363 di->status = POWER_SUPPLY_STATUS_CHARGING;
2364 DBG("++++++++ILIM_45MA++++++\n");
2366 } else if (otg_status == 2) {
2369 di->status = POWER_SUPPLY_STATUS_CHARGING;
2370 set_charge_current(di, ILIM_3000MA);
2371 DBG("++++++++ILIM_1000MA++++++\n");
2373 DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2377 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2378 if (buf&PLUG_IN_STS) {
2381 di->status = POWER_SUPPLY_STATUS_CHARGING;
2382 if (di->real_soc == 100)
2383 di->status = POWER_SUPPLY_STATUS_FULL;
2385 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2389 DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2394 static void check_battery_status(struct battery_info *di)
2399 ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2400 #ifdef SUPPORT_USB_CHARGE
2402 if (strstr(saved_command_line, "charger")) {
2403 if ((buf&PLUG_IN_STS) == 0) {
2404 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2410 if (buf&PLUG_IN_STS) {
2411 get_charging_status_type(di);
2413 di->status = POWER_SUPPLY_STATUS_CHARGING;
2414 if (di->real_soc == 100)
2415 di->status = POWER_SUPPLY_STATUS_FULL;
2417 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2424 if (buf & PLUG_IN_STS) {
2427 di->status = POWER_SUPPLY_STATUS_CHARGING;
2428 if (di->real_soc == 100)
2429 di->status = POWER_SUPPLY_STATUS_FULL;
2431 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2438 static void report_power_supply_changed(struct battery_info *di)
2441 static u32 old_ac_status;
2442 static u32 old_usb_status;
2443 static u32 old_charge_status;
2446 state_changed = false;
2447 if (di->real_soc == 0)
2448 state_changed = true;
2449 else if (di->real_soc == 100)
2450 state_changed = true;
2451 else if (di->real_soc != old_soc)
2452 state_changed = true;
2453 else if (di->ac_online != old_ac_status)
2454 state_changed = true;
2455 else if (di->usb_online != old_usb_status)
2456 state_changed = true;
2457 else if (old_charge_status != di->status)
2458 state_changed = true;
2460 if (state_changed) {
2461 power_supply_changed(&di->bat);
2462 power_supply_changed(&di->usb);
2463 power_supply_changed(&di->ac);
2464 old_soc = di->real_soc;
2465 old_ac_status = di->ac_online;
2466 old_usb_status = di->usb_online;
2467 old_charge_status = di->status;
2471 static void update_battery_info(struct battery_info *di)
2473 di->remain_capacity = _get_realtime_capacity(di);
2474 if (di->remain_capacity > di->fcc)
2475 _capacity_init(di, di->fcc);
2476 else if (di->remain_capacity < 0)
2477 _capacity_init(di, 0);
2479 if (di->real_soc > 100)
2481 else if (di->real_soc < 0)
2484 if ((di->ac_online) || (di->usb_online)) {/*charging*/
2485 di->charging_time++;
2486 di->discharging_time = 0;
2488 di->discharging_time++;
2489 di->charging_time = 0;
2493 di->voltage = rk_battery_voltage(di);
2494 di->current_avg = _get_average_current(di);
2495 di->remain_capacity = _get_realtime_capacity(di);
2496 di->voltage_ocv = _get_OCV_voltage(di);
2497 di->charge_status = get_charge_status(di);
2498 di->otg_status = dwc_otg_check_dpdm();
2499 di->relax_voltage = get_relax_voltage(di);
2500 di->temp_soc = _get_soc(di);
2501 di->remain_capacity = _get_realtime_capacity(di);
2502 check_battery_status(di);/* ac_online, usb_online, status*/
2503 update_cal_offset(di);
2506 static void rk_battery_work(struct work_struct *work)
2508 struct battery_info *di = container_of(work,
2509 struct battery_info, battery_monitor_work.work);
2511 update_resume_status_relax_voltage(di);
2512 wait_charge_finish_signal(di);
2513 charge_finish_routine(di);
2515 rk_battery_display_smooth(di);
2516 update_battery_info(di);
2518 report_power_supply_changed(di);
2519 _copy_soc(di, di->real_soc);
2520 _save_remain_capacity(di, di->remain_capacity);
2522 dump_debug_info(di);
2523 di->queue_work_cnt++;
2524 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2527 static void rk_battery_charge_check_work(struct work_struct *work)
2529 struct battery_info *di = container_of(work,
2530 struct battery_info, charge_check_work.work);
2532 DBG("rk_battery_charge_check_work\n");
2533 charge_disable_open_otg(di->charge_otg);
2536 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2538 int register_battery_notifier(struct notifier_block *nb)
2540 return blocking_notifier_chain_register(&battery_chain_head, nb);
2542 EXPORT_SYMBOL_GPL(register_battery_notifier);
2544 int unregister_battery_notifier(struct notifier_block *nb)
2546 return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2548 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2550 int battery_notifier_call_chain(unsigned long val)
2552 return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2553 == NOTIFY_BAD) ? -EINVAL : 0;
2555 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2557 static void poweron_lowerpoer_handle(struct battery_info *di)
2559 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2560 if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2562 /* kernel_power_off(); */
2567 static int battery_notifier_call(struct notifier_block *nb,
2568 unsigned long event, void *data)
2570 struct battery_info *di =
2571 container_of(nb, struct battery_info, battery_nb);
2575 DBG(" CHARGE enable\n");
2577 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2582 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2583 DBG("charge disable OTG enable\n");
2587 poweron_lowerpoer_handle(di);
2596 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2598 pr_info("<%s>lower power warning!\n", __func__);
2600 _copy_soc(g_battery, 0);
2601 _capacity_init(g_battery, 0);
2602 rk_send_wakeup_key();
2607 static void disable_vbat_low_irq(struct battery_info *di)
2610 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2611 /*clr vbat low interrupt */
2612 /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2614 static void enable_vbat_low_irq(struct battery_info *di)
2616 /* clr vbat low interrupt */
2617 rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2619 rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2622 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2624 pr_info("\n------- %s:irq = %d\n", __func__, irq);
2625 rk_send_wakeup_key();
2628 static irqreturn_t rk818_vbat_plug_out(int irq, void *di)
2630 pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2631 charge_disable_open_otg(0);
2632 rk_send_wakeup_key();
2636 static irqreturn_t rk818_vbat_charge_ok(int irq, void *di)
2638 pr_info("---------- %s:irq = %d\n", __func__, irq);
2639 rk_send_wakeup_key();
2645 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2649 struct kobject *rk818_fg_kobj;
2651 ret = device_create_file(dev, &dev_attr_rk818batparam);
2654 dev_err(dev, "failed to create bat param file\n");
2658 ret = create_sysfs_interfaces(dev);
2661 dev_err(dev, "device RK818 battery sysfs register failed\n");
2665 rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2668 for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2669 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2671 dev_err(dev, "create rk818_battery node error\n");
2679 power_supply_unregister(&di->ac);
2680 power_supply_unregister(&di->usb);
2681 power_supply_unregister(&di->bat);
2686 static void rk818_battery_irq_init(struct battery_info *di)
2688 int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2690 struct rk818 *chip = di->rk818;
2692 vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
2693 plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
2694 plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
2695 chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
2697 ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2698 IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2700 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2702 di->irq = vb_lo_irq;
2703 enable_irq_wake(di->irq);
2704 disable_vbat_low_irq(di);
2706 ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2707 IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2709 dev_err(chip->dev, "plug_in_irq request failed!\n");
2712 ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2713 IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2715 dev_err(chip->dev, "plug_out_irq request failed!\n");
2718 ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2719 IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2721 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2724 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2730 di->platform_data = chip->battery_data;
2731 di->cell.config = di->platform_data->cell_cfg;
2732 di->design_capacity = di->platform_data->cell_cfg->design_capacity;
2733 di->qmax = di->platform_data->cell_cfg->design_qmax;
2734 di->fcc = di->design_capacity;
2735 di->vol_smooth_time = 0;
2736 di->charge_smooth_time = 0;
2737 di->charge_smooth_status = false;
2738 di->sleep_status = 0;
2740 di->sys_wakeup = true;
2741 di->pcb_ioffset = 0;
2742 di->pcb_ioffset_updated = false;
2743 di->queue_work_cnt = 0;
2746 di->voltage_old = 0;
2747 di->display_soc = 0;
2749 di->bat_res_updated = false;
2751 di->sys_wakeup = true;
2752 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2754 di->debug_finish_real_soc = 0;
2755 di->debug_finish_temp_soc = 0;
2757 fcc_capacity = _get_FCC_capacity(di);
2758 if (fcc_capacity > 1000)
2759 di->fcc = fcc_capacity;
2761 di->fcc = di->design_capacity;
2764 static struct of_device_id rk818_battery_of_match[] = {
2765 { .compatible = "rk818_battery" },
2769 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2772 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2774 struct device_node *regs, *rk818_pmic_np;
2775 struct battery_platform_data *data;
2776 struct cell_config *cell_cfg;
2777 struct ocv_config *ocv_cfg;
2778 struct property *prop;
2782 rk818_pmic_np = of_node_get(rk818->dev->of_node);
2783 if (!rk818_pmic_np) {
2784 dev_err(dev, "could not find pmic sub-node\n");
2788 regs = of_find_node_by_name(rk818_pmic_np, "battery");
2790 dev_err(dev, "battery node not found!\n");
2794 data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2796 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2799 memset(data, 0, sizeof(*data));
2801 cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2803 dev_err(dev, "kzalloc for cell_config failed!\n");
2806 ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2808 dev_err(dev, "kzalloc for ocv_config failed!\n");
2812 prop = of_find_property(regs, "ocv_table", &length);
2814 dev_err(dev, "ocv_table not found!\n");
2817 data->ocv_size = length / sizeof(u32);
2819 if (data->ocv_size > 0) {
2820 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2822 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
2823 if (!data->battery_ocv) {
2824 dev_err(dev, "kzalloc for ocv_table failed!\n");
2827 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2832 ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2834 dev_err(dev, "max_charge_currentmA not found!\n");
2837 data->max_charger_currentmA = out_value;
2839 ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
2841 dev_err(dev, "max_charge_voltagemV not found!\n");
2844 data->max_charger_voltagemV = out_value;
2846 ret = of_property_read_u32(regs, "design_capacity", &out_value);
2848 dev_err(dev, "design_capacity not found!\n");
2851 cell_cfg->design_capacity = out_value;
2853 ret = of_property_read_u32(regs, "design_qmax", &out_value);
2855 dev_err(dev, "design_qmax not found!\n");
2858 cell_cfg->design_qmax = out_value;
2860 ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
2862 dev_err(dev, "sleep_enter_current not found!\n");
2865 ocv_cfg->sleep_enter_current = out_value;
2867 ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
2869 dev_err(dev, "sleep_exit_current not found!\n");
2872 ocv_cfg->sleep_exit_current = out_value;
2874 ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
2876 cell_cfg->ocv = ocv_cfg;
2877 data->cell_cfg = cell_cfg;
2878 rk818->battery_data = data;
2880 DBG("\n--------- the battery OCV TABLE dump:\n");
2881 DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
2882 DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
2883 DBG("design_capacity :%d\n", cell_cfg->design_capacity);
2884 DBG("design_qmax :%d\n", cell_cfg->design_qmax);
2885 DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
2886 DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
2887 DBG("uboot chrg = %d\n", support_uboot_chrg);
2888 DBG("\n--------- rk818_battery dt_parse ok.\n");
2893 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2900 static int battery_probe(struct platform_device *pdev)
2902 struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
2903 struct battery_info *di;
2906 DBG("battery driver version %s\n", DRIVER_VERSION);
2907 di = kzalloc(sizeof(*di), GFP_KERNEL);
2909 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
2912 ret = rk_battery_parse_dt(chip, &pdev->dev);
2914 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
2918 platform_set_drvdata(pdev, di);
2919 battery_info_init(di, chip);
2922 wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
2924 flatzone_voltage_init(di);
2925 battery_poweron_status_init(di);
2926 battery_power_supply_init(di);
2927 ret = battery_power_supply_register(di, &pdev->dev);
2929 dev_err(&pdev->dev, "rk power supply register failed!\n");
2932 di->wq = create_singlethread_workqueue("battery-work");
2933 INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
2934 queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2935 INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
2937 di->battery_nb.notifier_call = battery_notifier_call;
2938 register_battery_notifier(&di->battery_nb);
2940 rk818_battery_irq_init(di);
2941 rk818_battery_sysfs_init(di, &pdev->dev);
2942 DBG("------ RK81x battery_probe ok!-------\n");
2949 static int battery_suspend(struct platform_device *dev, pm_message_t state)
2951 struct battery_info *di = platform_get_drvdata(dev);
2953 enable_vbat_low_irq(di);
2954 di->sleep_status = di->status;
2955 di->suspend_charge_current = _get_average_current(di);
2957 /* avoid abrupt wakeup which will clean the variable*/
2958 if (di->sys_wakeup) {
2959 di->suspend_capacity = di->remain_capacity;
2960 di->suspend_temp_soc = _get_soc(di);
2961 di->suspend_time_start = get_seconds();
2962 di->sys_wakeup = false;
2965 cancel_delayed_work(&di->battery_monitor_work);
2966 DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
2967 __func__, di->suspend_temp_soc, di->suspend_charge_current,
2968 di->suspend_capacity, di->sleep_status);
2973 static int battery_resume(struct platform_device *dev)
2975 struct battery_info *di = platform_get_drvdata(dev);
2978 DBG("<%s>\n", __func__);
2979 disable_vbat_low_irq(di);
2980 queue_delayed_work(di->wq, &di->battery_monitor_work,
2981 msecs_to_jiffies(TIMER_MS_COUNTS/2));
2983 if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING || di->real_soc <= 5)
2984 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
2988 static int battery_remove(struct platform_device *dev)
2990 struct battery_info *di = platform_get_drvdata(dev);
2992 cancel_delayed_work_sync(&di->battery_monitor_work);
2995 static void battery_shutdown(struct platform_device *dev)
2997 struct battery_info *di = platform_get_drvdata(dev);
2999 cancel_delayed_work_sync(&di->battery_monitor_work);
3000 DBG("rk818 shutdown!");
3004 static struct platform_driver battery_driver = {
3006 .name = "rk818-battery",
3007 .owner = THIS_MODULE,
3010 .probe = battery_probe,
3011 .remove = battery_remove,
3012 .suspend = battery_suspend,
3013 .resume = battery_resume,
3014 .shutdown = battery_shutdown,
3017 static int __init battery_init(void)
3019 return platform_driver_register(&battery_driver);
3022 fs_initcall_sync(battery_init);
3023 static void __exit battery_exit(void)
3025 platform_driver_unregister(&battery_driver);
3027 module_exit(battery_exit);
3029 MODULE_LICENSE("GPL");
3030 MODULE_ALIAS("platform:rk818-battery");
3031 MODULE_AUTHOR("ROCKCHIP");