Merge tag 'lsk-v3.10-android-15.01'
[firefly-linux-kernel-4.4.55.git] / drivers / power / rk818_battery.c
1 /*
2  * rk818  battery driver
3  *
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.
7  * */
8
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>
29
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
33
34 module_param_named(dbg_level, dbg_enable, int, 0644);
35 #define DBG(args...) \
36         do { \
37                 if (dbg_enable) { \
38                         pr_info(args); \
39                 } \
40         } while (0)
41
42
43 #define INTERPOLATE_MAX                         1000
44 #define MAX_INT                                                 0x7FFF
45 #define TIME_10MIN_SEC                          600
46
47 struct battery_info {
48         struct device           *dev;
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; */
55         struct rk818            *rk818;
56
57         struct battery_platform_data *platform_data;
58
59         int                             work_on;
60         int                             irq;
61         int                             ac_online;
62         int                             usb_online;
63         int                             status;
64         int                             current_avg;
65         int                             current_offset;
66
67         uint16_t                        voltage;
68         uint16_t                        voltage_ocv;
69         uint16_t                        relax_voltage;
70         u8                              charge_status;
71         u8                              otg_status;
72         int                             pcb_ioffset;
73         bool                            pcb_ioffset_updated;
74         unsigned long           queue_work_cnt;
75         uint16_t                        warnning_voltage;
76
77         int                             design_capacity;
78         int                             fcc;
79         int                             qmax;
80         int                             remain_capacity;
81         int                             nac;
82         int                             temp_nac;
83
84         int                             real_soc;
85         int                             display_soc;
86         int                             temp_soc;
87
88         int                             bat_res_update_cnt;
89         int                             soc_counter;
90
91         int                             dod0;
92         int                             dod0_status;
93         int                             dod0_voltage;
94         int                             dod0_capacity;
95         unsigned long           dod0_time;
96         u8                              dod0_level;
97         int                             enter_flatzone;
98         int                             exit_flatzone;
99
100         int                             time2empty;
101         int                             time2full;
102
103         int                             *ocv_table;
104         int                             *res_table;
105
106         int                             current_k;/* (ICALIB0, ICALIB1) */
107         int                             current_b;
108
109         int                             voltage_k;/* VCALIB0 VCALIB1 */
110         int                             voltage_b;
111
112         int                             update_k;
113         int                             line_k;
114         int                             line_q;
115         int                             update_q;
116         int                             voltage_old;
117
118         u8                              check_count;
119         /* u32                  status; */
120         struct timeval          soc_timer;
121         struct timeval          change_timer;
122
123         int                             vol_smooth_time;
124         int                             charge_smooth_time;
125
126         int                             suspend_capacity;
127         int                             resume_capacity;
128         struct timespec suspend_time;
129         struct timespec         resume_time;
130         unsigned long           suspend_time_start;
131         unsigned long           count_sleep_time;
132
133         unsigned long           dischrg_sum_sleep_sec;
134         unsigned long           dischrg_sum_sleep_capacity;
135         int                             suspend_temp_soc;
136         int                             sleep_status;
137         int                             suspend_charge_current;
138         int                             resume_soc;
139         int                             bat_res;
140         bool                            bat_res_updated;
141         bool                            charge_smooth_status;
142         bool                            resume;
143         unsigned long           last_plugin_time;
144         bool                            sys_wakeup;
145
146         unsigned long           charging_time;
147         unsigned long           discharging_time;
148
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;
153         int                                     charge_otg;
154
155         struct wake_lock  resume_wake_lock;
156
157         int     debug_finish_real_soc;
158         int     debug_finish_temp_soc;
159 };
160
161 struct battery_info *data;
162 struct battery_info *g_battery;
163 u32 support_uboot_chrg;
164
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);
172
173 static void update_battery_info(struct battery_info *di);
174
175 #define SUPPORT_USB_CHARGE
176
177
178 static u32 interpolate(int value, u32 *table, int size)
179 {
180         uint8_t i;
181         uint16_t d;
182
183         for (i = 0; i < size; i++) {
184                 if (value < table[i])
185                         break;
186         }
187
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));
192         } else {
193                 d = i * ((INTERPOLATE_MAX+size/2)/size);
194         }
195
196         if (d > 1000)
197                 d = 1000;
198
199         return d;
200 }
201 /* Returns (a * b) / c */
202 static int32_t ab_div_c(u32 a, u32 b, u32 c)
203 {
204         bool sign;
205         u32 ans = MAX_INT;
206         int32_t tmp;
207
208         sign = ((((a^b)^c) & 0x80000000) != 0);
209
210         if (c != 0) {
211                 if (sign)
212                         c = -c;
213
214                 tmp = ((int32_t) a*b + (c>>1)) / c;
215
216                 if (tmp < MAX_INT)
217                         ans = tmp;
218         }
219
220         if (sign)
221                 ans = -ans;
222
223         return ans;
224 }
225
226 static  int32_t abs_int(int32_t x)
227 {
228         return (x > 0) ? x : -x;
229 }
230
231 static  int abs32_int(int x)
232 {
233         return (x > 0) ? x : -x;
234 }
235
236
237 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
238 {
239         int ret;
240
241         ret = rk818_i2c_read(rk818, reg, len, buf);
242         return ret;
243 }
244
245 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
246 {
247         int ret;
248         ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
249         return ret;
250 }
251 static void dump_gauge_register(struct battery_info *di)
252 {
253         int i = 0;
254         char buf;
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);
259         }
260         DBG("demp end!\n");
261 }
262
263 static void dump_charger_register(struct battery_info *di)
264 {
265
266         int i = 0;
267         char buf;
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);
272         }
273         DBG("demp end!\n");
274
275 }
276
277 #if RK818_SYS_DBG
278
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);
295
296 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
297 {
298         struct battery_info *di = g_battery;
299         u8 status;
300         u8 rtc_val;
301         u8 soc_reg;
302         u8 shtd_time;
303
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);
310
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,
324                         status, rtc_val,
325                         di->voltage_k, di->voltage_b, soc_reg,
326                         _get_relax_vol1(di), _get_relax_vol2(di));
327 }
328
329 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
330 {
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;
335
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);
345
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
356            );
357 }
358 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
359 {
360         struct battery_info *di = g_battery;
361
362         return sprintf(buf, "%d", di->fcc);
363 }
364 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
365 {
366         struct battery_info *di = g_battery;
367
368         return sprintf(buf, "%d", di->real_soc);
369 }
370
371 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
372 {
373         struct battery_info *di = g_battery;
374
375         return sprintf(buf, "%d", di->temp_soc);
376 }
377
378 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
379 {
380         struct battery_info *di = g_battery;
381
382         return sprintf(buf, "%d", di->voltage);
383 }
384
385 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
386 {
387         struct battery_info *di = g_battery;
388
389         return sprintf(buf, "%d", di->current_avg);
390 }
391
392 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
393 {
394         struct battery_info *di = g_battery;
395
396         return sprintf(buf, "%d", di->remain_capacity);
397 }
398
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),
408 };
409
410 #endif
411
412 #define BATT_NUM  11
413
414 static int batt_table[22];
415
416 static ssize_t bat_param_read(struct device *dev, struct device_attribute *attr, char *buf)
417 {
418         int i;
419
420         for (i = 0; i < BATT_NUM; i++)
421                 printk(KERN_INFO"i = %d batt_table = %d\n", i, batt_table[i]);
422
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]);
425         return 0;
426 }
427
428 static ssize_t bat_param_write(struct device *dev,
429                                 struct device_attribute *attr, const char *buf, size_t size)
430 {
431         return size;
432 }
433
434
435 DEVICE_ATTR(rk818batparam, 0664, bat_param_read, bat_param_write);
436 static uint16_t get_relax_voltage(struct battery_info *di);
437
438 static ssize_t show_state_attrs(struct device *dev,
439                                 struct device_attribute *attr, char *buf)
440 {
441         printk(KERN_INFO"get_relax_voltage relax voltage = %d\n", get_relax_voltage(data));
442
443         if (0 == get_relax_voltage(data)) {
444                 return sprintf(buf,
445                                 "voltage = %d, remain_capacity = %d, status = %d\n",
446                                 data->voltage, data->remain_capacity,
447                                 data->status);
448
449         } else
450                 return sprintf(buf,
451                                 "voltage = %d, remain_capacity = %d, status = %d\n",
452                                 get_relax_voltage(data), data->remain_capacity,
453                                 data->status);
454 }
455
456 static ssize_t restore_state_attrs(struct device *dev,
457                                 struct device_attribute *attr, const char *buf, size_t size)
458 {
459         return size;
460 }
461 static struct device_attribute rkbatt_attrs[] = {
462         __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
463 };
464
465 static int create_sysfs_interfaces(struct device *dev)
466 {
467         int liTmep;
468
469         for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++)   {
470                 if (device_create_file(dev, rkbatt_attrs + liTmep))
471                         goto error;
472         }
473
474         return 0;
475
476 error:
477         for (; liTmep >= 0; liTmep--)
478                 device_remove_file(dev, rkbatt_attrs + liTmep);
479
480         dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
481         return -1;
482 }
483
484 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
485 {
486         u8 val;
487
488         battery_read(di->rk818, reg, &val, 1);
489         DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
490         return val;
491 }
492
493
494 static int  _gauge_enable(struct battery_info *di)
495 {
496         int ret;
497         u8 buf;
498
499         ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
500         if (ret < 0) {
501                 dev_err(di->dev, "error reading TS_CTRL_REG");
502                 return ret;
503         }
504         if (!(buf & GG_EN)) {
505                 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);
508                 return 0;
509         }
510
511         DBG("%s, %d\n", __func__, buf);
512         return 0;
513
514 }
515 static void save_level(struct  battery_info *di, u8 save_soc)
516 {
517         u8 soc;
518
519         soc = save_soc;
520         battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
521 }
522 static u8 get_level(struct  battery_info *di)
523 {
524         u8 soc;
525
526         battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
527         return soc;
528 }
529
530 static int _get_vcalib0(struct battery_info *di)
531 {
532         int ret;
533         int temp = 0;
534         u8 buf;
535
536         ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
537         temp = buf;
538         ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
539         temp |= buf<<8;
540
541         DBG("%s voltage0 offset vale is %d\n", __func__, temp);
542         return temp;
543 }
544
545 static int _get_vcalib1(struct  battery_info *di)
546 {
547         int ret;
548         int temp = 0;
549         u8 buf;
550
551         ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
552         temp = buf;
553         ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
554         temp |= buf<<8;
555
556         DBG("%s voltage1 offset vale is %d\n", __func__, temp);
557         return temp;
558 }
559
560 static int _get_ioffset(struct battery_info *di)
561 {
562
563         int ret;
564         int temp = 0;
565         u8 buf;
566
567         ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
568         temp = buf;
569         ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
570         temp |= buf<<8;
571
572         return temp;
573 }
574
575 static uint16_t  _get_cal_offset(struct battery_info *di)
576 {
577         int ret;
578         uint16_t temp = 0;
579         u8 buf;
580
581         ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
582         temp = buf;
583         ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
584         temp |= buf<<8;
585
586         return temp;
587 }
588 static int _set_cal_offset(struct battery_info *di, u32 value)
589 {
590         int ret;
591         u8 buf;
592
593         buf = value&0xff;
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);
597
598         return 0;
599 }
600 static void _get_voltage_offset_value(struct battery_info *di)
601 {
602         int vcalib0, vcalib1;
603
604         vcalib0 = _get_vcalib0(di);
605         vcalib1 = _get_vcalib1(di);
606
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);
610 }
611 static uint16_t _get_OCV_voltage(struct battery_info *di)
612 {
613         int ret;
614         u8 buf;
615         uint16_t temp;
616         uint16_t voltage_now = 0;
617
618         ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
619         temp = buf;
620         ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
621         temp |= buf<<8;
622
623         if (ret < 0) {
624                 dev_err(di->dev, "error read BAT_OCV_REGH");
625                 return ret;
626         }
627
628         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
629
630         return voltage_now;
631 }
632
633 static int rk_battery_voltage(struct battery_info *di)
634 {
635         int ret;
636         int voltage_now = 0;
637         u8 buf;
638         int temp;
639
640         ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
641         temp = buf;
642         ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
643         temp |= buf<<8;
644
645         if (ret < 0) {
646                 dev_err(di->dev, "error read BAT_VOL_REGH");
647                 return ret;
648         }
649
650         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
651
652         return voltage_now;
653 }
654
655 /* OCV Lookup table
656  * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
657  * based on the voltage.
658  */
659 static int _voltage_to_capacity(struct battery_info *di, int voltage)
660 {
661         u32 *ocv_table;
662         int ocv_size;
663         u32 tmp;
664
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);
671
672         return 0;
673 }
674
675 static uint16_t _get_relax_vol1(struct battery_info *di)
676 {
677         int ret;
678         u8 buf;
679         uint16_t temp = 0, voltage_now;
680
681         ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
682         temp = buf;
683         ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
684         temp |= (buf<<8);
685
686         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
687
688         return voltage_now;
689 }
690
691 static uint16_t _get_relax_vol2(struct battery_info *di)
692 {
693         int ret;
694         uint16_t temp = 0, voltage_now;
695         u8 buf;
696
697         ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
698         temp = buf;
699         ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
700         temp |= (buf<<8);
701
702         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
703
704         return voltage_now;
705 }
706
707 static int  _get_raw_adc_current(struct battery_info *di)
708 {
709         u8 buf;
710         int ret;
711         int current_now;
712
713         ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
714         if (ret < 0) {
715                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
716                 return ret;
717         }
718         current_now = buf;
719         ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
720         if (ret < 0) {
721                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
722                 return ret;
723         }
724         current_now |= (buf<<8);
725
726         if (ret < 0) {
727                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
728                 return ret;
729         }
730
731         return current_now;
732
733 }
734
735
736 static void ioffset_sample_time(struct battery_info *di, int time)
737 {
738         u8 ggcon;
739
740         battery_read(di->rk818, GGCON, &ggcon, 1);
741         ggcon &= ~(0x30); /*clear <5:4>*/
742         ggcon |= time;
743         battery_write(di->rk818, GGCON, &ggcon, 1);
744         debug_reg(di, GGCON, "GGCON");
745 }
746
747 static void update_cal_offset(struct battery_info *di)
748 {
749         int mod = di->queue_work_cnt % TIME_10MIN_SEC;
750
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));
755         }
756 }
757
758
759 static void zero_current_calibration(struct battery_info *di)
760 {
761         int adc_value;
762         uint16_t C0;
763         uint16_t C1;
764         int ioffset;
765         int pcb_offset;
766         u8 retry = 0;
767
768         if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
769
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);
773
774                         C0 = _get_cal_offset(di);
775                         C1 = adc_value + C0;
776                         _set_cal_offset(di, C1);
777                         DBG("<%s>. C1 = %d\n", __func__, C1);
778                         msleep(2000);
779
780                         adc_value = _get_raw_adc_current(di);
781                         DBG("<%s>. adc_value = %d\n", __func__, adc_value);
782                         if (adc_value < 4) {
783
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);
789                                 break;
790                         } else
791                                 di->pcb_ioffset_updated  = false;
792                 }
793         }
794 }
795
796
797 static bool  _is_relax_mode(struct battery_info *di)
798 {
799         int ret;
800         u8 status;
801
802         ret = battery_read(di->rk818, GGSTS, &status, 1);
803
804         if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
805                 return false;
806         else
807                 return true;
808 }
809
810 static uint16_t get_relax_voltage(struct battery_info *di)
811 {
812         int ret;
813         u8 status;
814         uint16_t relax_vol1, relax_vol2;
815         u8 ggcon;
816
817         ret = battery_read(di->rk818, GGSTS, &status, 1);
818         ret = battery_read(di->rk818, GGCON, &ggcon, 1);
819
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;
825         else
826                 return 0;
827 }
828
829 static void  _set_relax_thres(struct battery_info *di)
830 {
831         u8 buf;
832         int enter_thres, exit_thres;
833         struct cell_state *cell = &di->cell;
834
835         enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
836         exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
837
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);
842
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);
847
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);
853 }
854
855 static void restart_relax(struct battery_info *di)
856 {
857         u8 ggcon;/* chrg_ctrl_reg2;*/
858         u8 ggsts;
859
860         battery_read(di->rk818, GGCON, &ggcon, 1);
861         ggcon &= ~0x0c;
862         battery_write(di->rk818, GGCON, &ggcon, 1);
863
864         battery_read(di->rk818, GGSTS, &ggsts, 1);
865         ggsts &= ~0x0c;
866         battery_write(di->rk818, GGSTS, &ggsts, 1);
867 }
868
869 static int  _get_average_current(struct battery_info *di)
870 {
871         u8  buf;
872         int ret;
873         int current_now;
874         int temp;
875
876         ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
877         if (ret < 0) {
878                 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
879                 return ret;
880         }
881         current_now = buf;
882         ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
883         if (ret < 0) {
884                 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
885                 return ret;
886         }
887         current_now |= (buf<<8);
888
889         if (current_now & 0x800)
890                 current_now -= 4096;
891
892         temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
893
894         return temp;
895
896 }
897
898 static bool _is_first_poweron(struct  battery_info *di)
899 {
900         u8 buf;
901         u8 temp;
902
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;*/
906         if (buf&BAT_CON) {
907                 buf &= ~(BAT_CON);
908                 do {
909                         battery_write(di->rk818, GGSTS, &buf, 1);
910                         battery_read(di->rk818, GGSTS, &temp, 1);
911                 } while (temp&BAT_CON);
912                 return true;
913         }
914         return false;
915 }
916 static void flatzone_voltage_init(struct battery_info *di)
917 {
918         u32 *ocv_table;
919         int ocv_size;
920         int temp_table[21];
921         int i, j;
922
923         ocv_table = di->platform_data->battery_ocv;
924         ocv_size = di->platform_data->ocv_size;
925
926         for (j = 0; j < 21; j++)
927                 temp_table[j] = 0;
928
929         j = 0;
930         for (i = 1; i < ocv_size-1; i++) {
931                 if (ocv_table[i+1] < ocv_table[i] + 20)
932                         temp_table[j++] = i;
933         }
934
935         temp_table[j] = temp_table[j-1]+1;
936         i = temp_table[0];
937         di->enter_flatzone = ocv_table[i];
938         j = 0;
939
940
941         for (i = 0; i <= 20; i++) {
942                 if (temp_table[i] < temp_table[i+1])
943                         j = i+1;
944         }
945
946         i = temp_table[j];
947         di->exit_flatzone = ocv_table[i];
948
949         DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
950
951 }
952
953 #if 0
954 static int is_not_flatzone(struct   battery_info *di, int voltage)
955 {
956         if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
957                 DBG("<%s>. is in flat zone\n", __func__);
958                 return 0;
959         } else {
960                 DBG("<%s>. is not in flat zone\n", __func__);
961                 return 1;
962         }
963 }
964 #endif
965 static void power_on_save(struct   battery_info *di, int voltage)
966 {
967         u8 buf;
968         u8 save_soc;
969
970         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
971
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;
977                         di->dod0_status = 1;
978                         di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
979                         di->dod0_level = 80;
980
981                         if (di->temp_soc <= 0)
982                                 di->dod0_level = 100;
983                         else if (di->temp_soc < 5)
984                                 di->dod0_level = 95;
985                         else if (di->temp_soc < 10)
986                                 di->dod0_level = 90;
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);
993                 }
994         }
995
996 }
997
998
999 static int _get_soc(struct   battery_info *di)
1000 {
1001         return di->remain_capacity * 100 / di->fcc;
1002 }
1003
1004 static enum power_supply_property rk_battery_props[] = {
1005
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,
1012 };
1013
1014 #define to_device_info(x) container_of((x), \
1015                                 struct battery_info, bat)
1016
1017 static int rk_battery_get_property(struct power_supply *psy,
1018         enum power_supply_property psp,
1019         union power_supply_propval *val)
1020 {
1021         struct battery_info *di = to_device_info(psy);
1022
1023         switch (psp) {
1024         case POWER_SUPPLY_PROP_CURRENT_NOW:
1025                 val->intval = di->current_avg*1000;/*uA*/
1026                 break;
1027
1028         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1029                 val->intval = di->voltage*1000;/*uV*/
1030                 break;
1031
1032         case POWER_SUPPLY_PROP_PRESENT:
1033                 val->intval = val->intval <= 0 ? 0 : 1;
1034                 break;
1035
1036         case POWER_SUPPLY_PROP_CAPACITY:
1037                 val->intval = di->real_soc;
1038                 break;
1039
1040         case POWER_SUPPLY_PROP_HEALTH:
1041                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1042                 break;
1043
1044         case POWER_SUPPLY_PROP_STATUS:
1045                 val->intval = di->status;
1046                 break;
1047
1048         default:
1049                 return -EINVAL;
1050         }
1051
1052         return 0;
1053 }
1054
1055
1056 static enum power_supply_property rk_battery_ac_props[] = {
1057         POWER_SUPPLY_PROP_ONLINE,
1058 };
1059 static enum power_supply_property rk_battery_usb_props[] = {
1060         POWER_SUPPLY_PROP_ONLINE,
1061 };
1062
1063
1064 #define to_ac_device_info(x) container_of((x), \
1065                                 struct battery_info, ac)
1066
1067 static int rk_battery_ac_get_property(struct power_supply *psy,
1068         enum power_supply_property psp,
1069         union power_supply_propval *val)
1070 {
1071         int ret = 0;
1072         struct battery_info *di = to_ac_device_info(psy);
1073
1074         switch (psp) {
1075         case POWER_SUPPLY_PROP_ONLINE:
1076                 val->intval = di->ac_online;    /*discharging*/
1077                 break;
1078
1079         default:
1080                 ret = -EINVAL;
1081                 break;
1082         }
1083         return ret;
1084 }
1085
1086 #define to_usb_device_info(x) container_of((x), \
1087                                 struct battery_info, usb)
1088
1089 static int rk_battery_usb_get_property(struct power_supply *psy,
1090         enum power_supply_property psp,
1091         union power_supply_propval *val)
1092 {
1093         int ret = 0;
1094         struct battery_info *di = to_usb_device_info(psy);
1095
1096         switch (psp) {
1097         case POWER_SUPPLY_PROP_ONLINE:
1098                 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1099                         val->intval = 0;
1100                 else
1101                         val->intval = di->usb_online;
1102                 break;
1103
1104         default:
1105                 ret = -EINVAL;
1106                 break;
1107         }
1108
1109         return ret;
1110 }
1111
1112
1113 static void battery_power_supply_init(struct battery_info *di)
1114 {
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;
1120
1121         di->ac.name = "AC";
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;
1126
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;
1132 }
1133
1134 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1135 {
1136         int ret;
1137
1138         ret = power_supply_register(dev, &di->bat);
1139         if (ret) {
1140                 dev_err(dev, "failed to register main battery\n");
1141                 goto batt_failed;
1142         }
1143         ret = power_supply_register(dev, &di->usb);
1144         if (ret) {
1145                 dev_err(dev, "failed to register usb power supply\n");
1146                 goto usb_failed;
1147         }
1148         ret = power_supply_register(dev, &di->ac);
1149         if (ret) {
1150                 dev_err(dev, "failed to register ac power supply\n");
1151                 goto ac_failed;
1152         }
1153
1154         return 0;
1155
1156 ac_failed:
1157         power_supply_unregister(&di->ac);
1158 usb_failed:
1159         power_supply_unregister(&di->usb);
1160 batt_failed:
1161         power_supply_unregister(&di->bat);
1162
1163         return ret;
1164 }
1165
1166 #if 1
1167 /*calc battery resister*/
1168 static void res_mode_init(struct battery_info *di)
1169 {
1170         u8 ggcon;/*  chrg_ctrl_reg2; */
1171         u8 ggsts;
1172
1173         battery_read(di->rk818, GGCON, &ggcon, 1);
1174         battery_read(di->rk818, GGSTS, &ggsts, 1);
1175
1176         ggcon |= 0x01;
1177         ggsts  &= ~0x01;
1178         ggsts |= 0x60;
1179         battery_write(di->rk818, GGCON, &ggcon, 1);
1180         battery_write(di->rk818, GGSTS, &ggsts, 1);
1181 }
1182 #endif
1183
1184 static void  _capacity_init(struct battery_info *di, u32 capacity)
1185 {
1186         u8 buf;
1187         u32 capacity_ma;
1188
1189         di->update_k = 0;
1190         di->update_q = 0;
1191         di->voltage_old = 0;
1192         di->display_soc = 0;
1193
1194         capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1195         do {
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);
1205
1206         } while (buf == 0);
1207 }
1208
1209
1210 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
1211 {
1212         u8 buf;
1213         u32 capacity_ma;
1214
1215         if (capacity >= di->qmax)
1216                 capacity = di->qmax;
1217
1218         capacity_ma = capacity;
1219
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);
1228 }
1229
1230 static int _get_remain_capacity(struct battery_info *di)
1231 {
1232         int ret;
1233         int temp = 0;
1234         u8 buf;
1235         u32 capacity;
1236
1237         ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1238         temp = buf << 24;
1239         ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1240         temp |= buf << 16;
1241         ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1242         temp |= buf << 8;
1243         ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1244         temp |= buf;
1245
1246         capacity = temp;/* /4096*900/14/36*500/521; */
1247
1248         return capacity;
1249 }
1250
1251
1252 static void  _save_FCC_capacity(struct battery_info *di, u32 capacity)
1253 {
1254         u8 buf;
1255         u32 capacity_ma;
1256
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);
1266 }
1267
1268 static int _get_FCC_capacity(struct battery_info *di)
1269 {
1270         int ret;
1271         int temp = 0;
1272         u8 buf;
1273         u32 capacity;
1274
1275         ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1276         temp = buf << 24;
1277         ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1278         temp |= buf << 16;
1279         ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1280         temp |= buf << 8;
1281         ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1282         temp |= buf;
1283
1284         if (temp > 1)
1285                 capacity = temp-1;/* 4096*900/14/36*500/521 */
1286         else
1287                 capacity = temp;
1288         DBG("%s NEW_FCC_REG %d  capacity = %d\n", __func__, temp, capacity);
1289
1290         return capacity;
1291 }
1292
1293 static int _get_realtime_capacity(struct battery_info *di)
1294 {
1295         int ret;
1296         int temp = 0;
1297         u8 buf;
1298         u32 capacity;
1299
1300         ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1301         temp = buf << 24;
1302         ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1303         temp |= buf << 16;
1304         ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1305         temp |= buf << 8;
1306         ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1307         temp |= buf;
1308
1309         capacity = temp/2390;/* 4096*900/14/36*500/521; */
1310
1311         return capacity;
1312 }
1313
1314 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1315 {
1316         int remain_capacity;
1317         int relax_capacity;
1318         int now_temp_soc;
1319         int relax_soc;
1320         int abs_soc;
1321         int min, soc_time;
1322         int now_current;
1323
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);
1329
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);
1332
1333         /*handle temp_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__);
1337         } else {
1338                 if (abs_soc == 0)
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;
1344                 else
1345                         remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1346         }
1347         _capacity_init(di, remain_capacity);
1348         di->temp_soc = _get_soc(di);
1349         di->remain_capacity  = _get_realtime_capacity(di);
1350
1351         /*handle real_soc*/
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;
1358                 else
1359                         di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1360         } else {
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)
1365                         di->real_soc--;
1366         }
1367
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);
1369 }
1370
1371
1372 static int _copy_soc(struct  battery_info *di, u8 save_soc)
1373 {
1374         u8 soc;
1375
1376         soc = save_soc;
1377         battery_write(di->rk818, SOC_REG, &soc, 1);
1378         return 0;
1379 }
1380
1381 static bool support_uboot_charge(void)
1382 {
1383         return support_uboot_chrg?true:false;
1384 }
1385
1386 static int _rsoc_init(struct  battery_info *di)
1387 {
1388         u8 pwron_soc;
1389         u8 init_soc;
1390         u32 remain_capacity;
1391         u8 last_shtd_time;
1392         u8 curr_shtd_time;
1393 #ifdef SUPPORT_USB_CHARGE
1394         int otg_status;
1395 #else
1396         u8 buf;
1397 #endif
1398         di->voltage  = rk_battery_voltage(di);
1399         di->voltage_ocv = _get_OCV_voltage(di);
1400         DBG("OCV voltage = %d\n" , di->voltage_ocv);
1401
1402         if (_is_first_poweron(di)) {
1403                 _save_FCC_capacity(di, di->design_capacity);
1404                 di->fcc = _get_FCC_capacity(di);
1405
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);
1410
1411         } else {
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);
1415
1416 #ifdef SUPPORT_USB_CHARGE
1417                 otg_status = dwc_otg_check_dpdm();
1418                 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1419                         init_soc = 1;
1420                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1421                 }
1422 #else
1423                 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1424                 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1425                         init_soc = 1;
1426                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1427                 }
1428 #endif
1429                 remain_capacity = _get_remain_capacity(di);
1430
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);
1435
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);
1439
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))) {
1442
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);
1446
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;
1452
1453                                         DBG("<%s> shutdown_time > 3 minute,  remain_cap = %d\n", __func__, remain_capacity);
1454                                 }
1455                         }
1456                 }
1457
1458                 di->real_soc = init_soc;
1459                 di->nac = remain_capacity;
1460                 if (di->nac <= 0)
1461                         di->nac = 0;
1462                 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1463         }
1464         return 0;
1465 }
1466
1467
1468 static u8 get_charge_status(struct battery_info *di)
1469 {
1470         u8 status;
1471         u8 ret = 0;
1472
1473         battery_read(di->rk818, SUP_STS_REG, &status, 1);
1474         status &= (0x70);
1475         switch (status) {
1476         case CHARGE_OFF:
1477                 ret = CHARGE_OFF;
1478                 DBG("  CHARGE-OFF ...\n");
1479                 break;
1480
1481         case DEAD_CHARGE:
1482                 ret = DEAD_CHARGE;
1483                 DBG("  DEAD CHARGE ...\n");
1484                 break;
1485
1486         case  TRICKLE_CHARGE:                           /* (0x02 << 4) */
1487                 ret = DEAD_CHARGE;
1488                 DBG("  TRICKLE CHARGE ...\n ");
1489                 break;
1490
1491         case  CC_OR_CV:                                 /* (0x03 << 4) */
1492                 ret = CC_OR_CV;
1493                 DBG("  CC or CV ...\n");
1494                 break;
1495
1496         case  CHARGE_FINISH:                            /* (0x04 << 4) */
1497                 ret = CHARGE_FINISH;
1498                 DBG("  CHARGE FINISH ...\n");
1499                 break;
1500
1501         case  USB_OVER_VOL:                                     /* (0x05 << 4) */
1502                 ret = USB_OVER_VOL;
1503                 DBG("  USB OVER VOL ...\n");
1504                 break;
1505
1506         case  BAT_TMP_ERR:                                      /* (0x06 << 4) */
1507                 ret = BAT_TMP_ERR;
1508                 DBG("  BAT TMP ERROR ...\n");
1509                 break;
1510
1511         case  TIMER_ERR:                                        /* (0x07 << 4) */
1512                 ret = TIMER_ERR;
1513                 DBG("  TIMER ERROR ...\n");
1514                 break;
1515
1516         case  USB_EXIST:                                        /* (1 << 1)// usb is exists */
1517                 ret = USB_EXIST;
1518                 DBG("  USB EXIST ...\n");
1519                 break;
1520
1521         case  USB_EFF:                                          /* (1 << 0)// usb is effective */
1522                 ret = USB_EFF;
1523                 DBG("  USB EFF...\n");
1524                 break;
1525
1526         default:
1527                 return -EINVAL;
1528         }
1529
1530         return ret;
1531
1532 }
1533 static void set_charge_current(struct battery_info *di, int charge_current)
1534 {
1535         u8 usb_ctrl_reg;
1536
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);
1541 }
1542
1543 static void rk_battery_charger_init(struct  battery_info *di)
1544 {
1545         u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1546         u8 sup_sts_reg;
1547
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);
1554
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);
1559 #else
1560         usb_ctrl_reg |= (VLIM_4400MV | ILIM_3000MA) | CHRG_CT_EN);
1561 #endif
1562         chrg_ctrl_reg1 &= (0x00);
1563         chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
1564
1565         chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1566         chrg_ctrl_reg2 &= ~(0xc0);
1567         chrg_ctrl_reg2 |= FINISH_100MA;
1568
1569         sup_sts_reg &= ~(0x01 << 3);
1570         sup_sts_reg |= (0x01 << 2);
1571
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);
1577
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");
1582
1583         DBG("%s  end\n", __func__);
1584 }
1585
1586 void charge_disable_open_otg(int value)
1587 {
1588         struct  battery_info *di = g_battery;
1589
1590         if (value == 1) {
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 */
1594         }
1595         if (value == 0) {
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);
1599         }
1600 }
1601
1602 static void low_waring_init(struct battery_info *di)
1603 {
1604         u8 vb_mon_reg;
1605         u8 vb_mon_reg_init;
1606
1607         battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1608
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);
1612 }
1613
1614 static void  fg_init(struct battery_info *di)
1615 {
1616         u8 adc_ctrl_val;
1617
1618         adc_ctrl_val = 0x30;
1619         battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1620
1621         _gauge_enable(di);
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);
1629         _rsoc_init(di);
1630         _capacity_init(di, di->nac);
1631         res_mode_init(di);
1632         di->remain_capacity = _get_realtime_capacity(di);
1633         di->current_avg = _get_average_current(di);
1634
1635         low_waring_init(di);
1636         restart_relax(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);
1642
1643         DBG("<%s> :\n"
1644             "nac = %d , remain_capacity = %d\n"
1645             "OCV_voltage = %d, voltage = %d\n"
1646             "SOC = %d, fcc = %d\n",
1647             __func__,
1648             di->nac, di->remain_capacity,
1649             di->voltage_ocv, di->voltage,
1650             di->real_soc, di->fcc);
1651 }
1652
1653
1654 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1655 static void  zero_get_soc(struct   battery_info *di)
1656 {
1657         int ocv_voltage, check_voltage;
1658         int temp_soc = -1, real_soc;
1659         int currentold, currentnow, voltage;
1660         int i;
1661         int voltage_k;
1662         int count_num = 0;
1663
1664         DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1665         /* if (di->voltage <  3600)//di->warnning_voltage) */
1666         {
1667                 /* DBG("+++++++zero mode++++++++displaysoc+++++++++\n"); */
1668                 do {
1669                         currentold = _get_average_current(di);
1670                         _get_cal_offset(di);
1671                         _get_ioffset(di);
1672                         msleep(100);
1673                         currentnow = _get_average_current(di);
1674                         count_num++;
1675                 } while ((currentold == currentnow) && (count_num < 11));
1676
1677                 voltage  = 0;
1678                 for (i = 0; i < 10 ; i++)
1679                         voltage += rk_battery_voltage(di);
1680                 voltage /= 10;
1681
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); */
1688
1689                 currentnow = _get_average_current(di);
1690                 /* DBG(" zero: current = %d, voltage = %d\n", currentnow, voltage); */
1691
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; */
1699
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); */
1702
1703                 _voltage_to_capacity(di, ocv_voltage);
1704                 /*di->temp_nac;
1705                 temp_soc = _get_soc(di); */
1706                 if (di->display_soc == 0)
1707                         di->display_soc = di->real_soc*1000;
1708
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) {
1714
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); */
1725
1726
1727                                 } else {
1728                                         /*
1729                                         if (line_q == 0)
1730                                         line_q = di->temp_nac + update_q;
1731                                         */
1732                                         /* DBG("two...\n"); */
1733                                         temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* x1  10 */
1734                                         /*
1735                                         temp_soc = (line_q)*1000/di->fcc;// x1
1736                                         real_soc = (di->line_k*temp_soc+500)/1000;  //y1 = k0*x1
1737                                         */
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)
1743                                                 di->real_soc--;
1744                                         /*
1745                                         DBG("Zero two di->real_soc = %d\n", di->real_soc);
1746                                         DBG("Zero : temp_soc : %d\n", real_soc);
1747                                         */
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 */
1752                                         /*
1753                                         temp_soc = (line_q)*1000/di->fcc;
1754                                         di->line_k = (di->real_soc*1000 +  temp_soc/2)/temp_soc; //k1 = y1/z1
1755                                         */
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 */
1759
1760
1761                                 }
1762                                 di->update_k = 0;
1763
1764                         }
1765
1766                         /* DBG("di->remain_capacity = %d, line_q = %d\n ", di->remain_capacity, di->line_q); */
1767
1768                         di->update_k++;
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)
1775                                         di->real_soc--;
1776                                 /* di->real_soc = (line_k*temp_soc+500)/1000 ;//y = k0*x */
1777                         }
1778                 } else {
1779                         /* DBG("three..\n"); */
1780                         di->update_k++;
1781                         if (di->update_k > 10) {
1782                                 di->update_k = 0;
1783                                 di->real_soc--;
1784                         }
1785                 }
1786
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);
1790         }
1791 }
1792
1793
1794 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1795 {
1796         int voltage;
1797         int now_current, soc_time = -1;
1798         int volt_to_soc;
1799
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);
1806
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) */
1810                 zero_get_soc(di);
1811                 return;
1812
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) {
1819                         di->real_soc--;
1820                         di->vol_smooth_time = 0;
1821                 }
1822
1823         } else {
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;
1828                 } else {
1829                         di->vol_smooth_time++;
1830                         if (di->vol_smooth_time > soc_time/3) {
1831                                 di->real_soc--;
1832                                 di->vol_smooth_time  = 0;
1833                         }
1834                 }
1835         }
1836
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);
1839 }
1840
1841 static int get_charging_time(struct battery_info *di)
1842 {
1843         return (di->charging_time/60);
1844 }
1845
1846 static int get_discharging_time(struct battery_info *di)
1847 {
1848         return (di->discharging_time/60);
1849 }
1850 static void dump_debug_info(struct battery_info *di)
1851 {
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;
1855
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);
1865
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
1875            );
1876
1877         DBG(
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)
1894            );
1895         get_charge_status(di);
1896         DBG("################################################################\n");
1897
1898 }
1899
1900 static void update_fcc_capacity(struct battery_info *di)
1901 {
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)
1906                                 di->fcc = di->qmax;
1907
1908                         _capacity_init(di, di->fcc);
1909                         _save_FCC_capacity(di, di->fcc);
1910                 }
1911                 di->dod0_status = 0;
1912         }
1913 }
1914
1915 static void debug_get_finish_soc(struct battery_info *di)
1916 {
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;
1920         }
1921 }
1922
1923 static void wait_charge_finish_signal(struct battery_info *di)
1924 {
1925         if (di->charge_status == CHARGE_FINISH)
1926                 update_fcc_capacity(di);/* save new fcc*/
1927
1928         /* debug msg*/
1929         debug_get_finish_soc(di);
1930 }
1931
1932 static void charge_finish_routine(struct battery_info *di)
1933 {
1934         if (di->charge_status == CHARGE_FINISH) {
1935                 _capacity_init(di, di->fcc);
1936                 zero_current_calibration(di);
1937
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)) {
1941                                 di->soc_counter++;
1942                         } else {
1943                                 di->soc_counter = 0;
1944                                 di->real_soc++;
1945                         }
1946                 }
1947         }
1948 }
1949
1950 static void voltage_to_soc_charge_smooth(struct battery_info *di)
1951 {
1952         int now_current, soc_time;
1953
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);
1957
1958         DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1959         /*
1960         if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
1961                 di->charge_smooth_time++;
1962
1963                 if  (di->charge_smooth_time > soc_time/3) {
1964                         di->real_soc++;
1965                         di->charge_smooth_time  = 0;
1966                 }
1967                 di->charge_smooth_status = true;
1968         }*/
1969
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);
1973         }
1974         if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
1975
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) {
1980                                 di->real_soc++;
1981                                 di->charge_smooth_time  = 0;
1982                         }
1983                         di->charge_smooth_status = true;
1984                 }
1985
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) {
1990                                 di->real_soc++;
1991                                 di->charge_smooth_time  = 0;
1992                         }
1993                         di->charge_smooth_status = true;
1994
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;
2003                                 }
2004
2005                         } else {
2006                                 di->real_soc = di->temp_soc;
2007                                 di->charge_smooth_status = false;
2008
2009                         }
2010                 }
2011         }
2012
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);
2015 }
2016
2017 static void rk_battery_display_smooth(struct battery_info *di)
2018 {
2019         int status;
2020         u8  charge_status;
2021
2022         status = di->status;
2023         charge_status = di->charge_status;
2024         if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2025
2026                 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2027                         voltage_to_soc_discharge_smooth(di);
2028                 else
2029                         voltage_to_soc_charge_smooth(di);
2030
2031         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2032                 voltage_to_soc_discharge_smooth(di);
2033                 if (di->real_soc == 1) {
2034                         di->time2empty++;
2035                         if (di->time2empty >= 300)
2036                                 di->real_soc = 0;
2037                 } else {
2038                         di->time2empty = 0;
2039                 }
2040         }
2041
2042 }
2043
2044 #if 0
2045 static void software_recharge(struct battery_info *di, int max_cnt)
2046 {
2047         static int recharge_cnt;
2048         u8 chrg_ctrl_reg1;
2049
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);
2056                 msleep(400);
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);
2061
2062                 recharge_cnt++;
2063         }
2064 }
2065 #endif
2066
2067 #if 0
2068 static int estimate_battery_resister(struct battery_info *di)
2069 {
2070         int i;
2071         int avr_voltage1 = 0, avr_current1;
2072         int avr_voltage2 = 0, avr_current2;
2073         u8 usb_ctrl_reg;
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;
2079
2080         delta_time = get_seconds() - last_time;
2081         DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2082         if (delta_time >= 20) {/*20s*/
2083
2084                 /*first sample*/
2085                 set_charge_current(di, ILIM_45MA);/*450mA*/
2086                 msleep(1000);
2087                 for (i = 0; i < 10 ; i++) {
2088                         msleep(100);
2089                         avr_voltage1 += rk_battery_voltage(di);
2090                 }
2091                 avr_voltage1 /= 10;
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);
2096
2097                 /*second sample*/
2098                 set_charge_current(di, ILIM_3000MA);
2099                 msleep(1000);
2100                 for (i = 0; i < 10 ; i++) {
2101                         msleep(100);
2102                         avr_voltage2 += rk_battery_voltage(di);
2103                 }
2104                 avr_voltage2 /= 10;
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);
2108
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);
2113
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;
2121
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();
2126
2127                 return bat_res;
2128         }
2129
2130         return 0;
2131 }
2132 #endif
2133
2134 #if 0
2135 static int update_battery_resister(struct battery_info *di)
2136 {
2137         int tmp_res;
2138
2139         if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2140
2141                 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2142                         tmp_res = estimate_battery_resister(di);
2143                         if (tmp_res != 0)
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;
2149                                 di->bat_res /= 10;
2150                         }
2151                         DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2152                 }
2153         }
2154
2155         return tmp_res;
2156 }
2157 #endif
2158
2159 #if 0
2160 static void charge_soc_check_routine(struct battery_info *di)
2161 {
2162         int min;
2163         int ocv_voltage;
2164         int old_temp_soc;
2165         int ocv_temp_soc;
2166         int remain_capcity;
2167
2168         if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2169                 min = get_charging_time(di);
2170                 update_battery_resister(di);
2171         if (0)
2172                 if ((min >= 30) && (di->bat_res_updated)) {
2173
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;
2178
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);
2182
2183                         if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2184                                 di->temp_soc = ocv_temp_soc;
2185                         else
2186                                 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2187
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);
2192                 }
2193         }
2194
2195 }
2196 #endif
2197
2198 #if 1
2199 static void update_resume_status_relax_voltage(struct battery_info *di)
2200 {
2201         unsigned long sleep_soc;
2202         unsigned long sum_sleep_soc;
2203         unsigned long sleep_sec;
2204         int relax_voltage;
2205         u8 charge_status;
2206         int delta_capacity;
2207         int delta_soc;
2208         int sum_sleep_avr_current;
2209         int sleep_min;
2210
2211         if (di->resume) {
2212                 update_battery_info(di);
2213                 di->resume = false;
2214                 di->sys_wakeup = true;
2215
2216                 DBG("<%s>, resume----------checkstart\n", __func__);
2217                 sleep_sec = get_seconds() - di->suspend_time_start;
2218                 sleep_min = sleep_sec  / 60;
2219
2220                 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2221                         __func__, sleep_sec, sleep_min);
2222
2223                 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2224                         DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2225
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;
2230
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;
2233
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);
2241
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);
2248
2249                         /* small suspend current*/
2250                         } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2251
2252                                 relax_voltage = get_relax_voltage(di);
2253                                 di->voltage  = rk_battery_voltage(di);
2254
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);
2258
2259                                 } else {
2260                                         DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2261                                         if (sum_sleep_soc > 0)
2262                                                 di->real_soc -= sum_sleep_soc;
2263                                 }
2264                         }
2265
2266                         if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2267                                 di->dischrg_sum_sleep_capacity = 0;
2268                                 di->dischrg_sum_sleep_sec = 0;
2269                         }
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);
2272                 }
2273
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);
2279
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);
2287                                                 if (sleep_soc > 0)
2288                                                         di->count_sleep_time = 0;
2289                                                 di->real_soc += sleep_soc;
2290                                                 if (di->real_soc > 100)
2291                                                         di->real_soc = 100;
2292                                         }
2293                                 } else {
2294
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;
2298                                         else
2299                                                 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2300                                 }
2301
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);
2303                         }
2304                 }
2305         }
2306 }
2307 #endif
2308
2309 #ifdef SUPPORT_USB_CHARGE
2310 static int  get_charging_status_type(struct battery_info *di)
2311 {
2312         int otg_status = dwc_otg_check_dpdm();
2313
2314         if (0 == otg_status) {
2315                 di->usb_online = 0;
2316                 di->ac_online = 0;
2317                 di->check_count = 0;
2318
2319         } else if (1 == otg_status) {
2320                 if (0 == get_gadget_connect_flag()) {
2321                         if (++di->check_count >= 5) {
2322                                 di->ac_online = 1;
2323                                 di->usb_online = 0;
2324                         } else {
2325                                 di->ac_online = 0;
2326                                 di->usb_online = 1;
2327                         }
2328                 } else {
2329                         di->ac_online = 0;
2330                         di->usb_online = 1;
2331                 }
2332
2333         } else if (2 == otg_status) {
2334                 di->ac_online = 1;
2335                 di->usb_online = 0;
2336                 di->check_count = 0;
2337         }
2338
2339         if (di->ac_online == 1)
2340                 set_charge_current(di, ILIM_3000MA);
2341         else
2342                 set_charge_current(di, ILIM_45MA);
2343         return otg_status;
2344 }
2345
2346 #endif
2347
2348 static void battery_poweron_status_init(struct battery_info *di)
2349 {
2350         int otg_status;
2351
2352 #ifndef SUPPORT_USB_CHARGE
2353         u8 buf;
2354 #endif
2355
2356 #ifdef SUPPORT_USB_CHARGE
2357
2358         otg_status = dwc_otg_check_dpdm();
2359         if (otg_status == 1) {
2360                 di->usb_online = 1;
2361                 di->ac_online = 0;
2362                 set_charge_current(di, ILIM_45MA);
2363                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2364                 DBG("++++++++ILIM_45MA++++++\n");
2365
2366         } else if (otg_status == 2) {
2367                 di->usb_online = 0;
2368                 di->ac_online = 1;
2369                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2370                 set_charge_current(di, ILIM_3000MA);
2371                 DBG("++++++++ILIM_1000MA++++++\n");
2372         }
2373         DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2374
2375 #else
2376
2377         battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2378         if (buf&PLUG_IN_STS) {
2379                 di->ac_online = 1;
2380                 di->usb_online = 0;
2381                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2382                 if (di->real_soc == 100)
2383                         di->status = POWER_SUPPLY_STATUS_FULL;
2384         } else {
2385                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2386                 di->ac_online = 0;
2387                 di->usb_online = 0;
2388         }
2389         DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2390 #endif
2391 }
2392
2393
2394 static void check_battery_status(struct battery_info *di)
2395 {
2396         u8 buf;
2397         int ret;
2398
2399         ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2400 #ifdef SUPPORT_USB_CHARGE
2401
2402         if (strstr(saved_command_line, "charger")) {
2403                 if ((buf&PLUG_IN_STS) == 0) {
2404                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2405                         di->ac_online = 0;
2406                         di->usb_online = 0;
2407                 }
2408
2409         } else {
2410                 if (buf&PLUG_IN_STS) {
2411                         get_charging_status_type(di);
2412
2413                         di->status = POWER_SUPPLY_STATUS_CHARGING;
2414                         if (di->real_soc == 100)
2415                                 di->status = POWER_SUPPLY_STATUS_FULL;
2416                 } else {
2417                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2418                         di->ac_online = 0;
2419                         di->usb_online = 0;
2420                 }
2421         }
2422 #else
2423
2424         if (buf & PLUG_IN_STS) {
2425                 di->ac_online = 1;
2426                 di->usb_online = 0;
2427                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2428                 if (di->real_soc == 100)
2429                         di->status = POWER_SUPPLY_STATUS_FULL;
2430         } else {
2431                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2432                 di->ac_online = 0;
2433                 di->usb_online = 0;
2434         }
2435 #endif
2436 }
2437
2438 static void report_power_supply_changed(struct battery_info *di)
2439 {
2440         static u32 old_soc;
2441         static u32 old_ac_status;
2442         static u32 old_usb_status;
2443         static u32 old_charge_status;
2444         bool state_changed;
2445
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;
2459
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;
2468         }
2469 }
2470
2471 static void update_battery_info(struct battery_info *di)
2472 {
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);
2478
2479         if (di->real_soc > 100)
2480                 di->real_soc = 100;
2481         else if (di->real_soc < 0)
2482                 di->real_soc = 0;
2483
2484         if ((di->ac_online) || (di->usb_online)) {/*charging*/
2485                 di->charging_time++;
2486                 di->discharging_time = 0;
2487         } else {
2488                 di->discharging_time++;
2489                 di->charging_time = 0;
2490         }
2491
2492         di->work_on = 1;
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);
2504 }
2505
2506 static void rk_battery_work(struct work_struct *work)
2507 {
2508         struct battery_info *di = container_of(work,
2509                         struct battery_info, battery_monitor_work.work);
2510
2511         update_resume_status_relax_voltage(di);
2512         wait_charge_finish_signal(di);
2513         charge_finish_routine(di);
2514
2515         rk_battery_display_smooth(di);
2516         update_battery_info(di);
2517
2518         report_power_supply_changed(di);
2519         _copy_soc(di, di->real_soc);
2520         _save_remain_capacity(di, di->remain_capacity);
2521
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));
2525 }
2526
2527 static void rk_battery_charge_check_work(struct work_struct *work)
2528 {
2529         struct battery_info *di = container_of(work,
2530                         struct battery_info, charge_check_work.work);
2531
2532         DBG("rk_battery_charge_check_work\n");
2533         charge_disable_open_otg(di->charge_otg);
2534 }
2535
2536 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2537
2538 int register_battery_notifier(struct notifier_block *nb)
2539 {
2540         return blocking_notifier_chain_register(&battery_chain_head, nb);
2541 }
2542 EXPORT_SYMBOL_GPL(register_battery_notifier);
2543
2544 int unregister_battery_notifier(struct notifier_block *nb)
2545 {
2546         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2547 }
2548 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2549
2550 int battery_notifier_call_chain(unsigned long val)
2551 {
2552         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2553                 == NOTIFY_BAD) ? -EINVAL : 0;
2554 }
2555 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2556
2557 static void poweron_lowerpoer_handle(struct battery_info *di)
2558 {
2559 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2560         if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2561                 mdelay(1500);
2562                 /* kernel_power_off(); */
2563         }
2564 #endif
2565 }
2566
2567 static int battery_notifier_call(struct notifier_block *nb,
2568                                                                 unsigned long event, void *data)
2569 {
2570         struct battery_info *di =
2571             container_of(nb, struct battery_info, battery_nb);
2572
2573         switch (event) {
2574         case 0:
2575                 DBG(" CHARGE enable\n");
2576                 di->charge_otg = 0;
2577                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2578                 break;
2579
2580         case 1:
2581                 di->charge_otg  = 1;
2582                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2583                 DBG("charge disable OTG enable\n");
2584                 break;
2585
2586         case 2:
2587                 poweron_lowerpoer_handle(di);
2588                 break;
2589
2590         default:
2591                 return NOTIFY_OK;
2592         }
2593         return NOTIFY_OK;
2594 }
2595
2596 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2597 {
2598         pr_info("<%s>lower power warning!\n", __func__);
2599
2600         _copy_soc(g_battery, 0);
2601         _capacity_init(g_battery, 0);
2602         rk_send_wakeup_key();
2603         kernel_power_off();
2604         return IRQ_HANDLED;
2605 }
2606
2607 static void disable_vbat_low_irq(struct battery_info *di)
2608 {
2609         /* mask vbat low */
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));*/
2613 }
2614 static void enable_vbat_low_irq(struct battery_info *di)
2615 {
2616         /* clr vbat low interrupt */
2617         rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2618         /* mask vbat low */
2619         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2620 }
2621
2622 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2623 {
2624         pr_info("\n------- %s:irq = %d\n", __func__, irq);
2625         rk_send_wakeup_key();
2626         return IRQ_HANDLED;
2627 }
2628 static irqreturn_t rk818_vbat_plug_out(int irq, void  *di)
2629 {
2630         pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2631         charge_disable_open_otg(0);
2632         rk_send_wakeup_key();
2633         return IRQ_HANDLED;
2634 }
2635
2636 static irqreturn_t rk818_vbat_charge_ok(int irq, void  *di)
2637 {
2638         pr_info("---------- %s:irq = %d\n", __func__, irq);
2639         rk_send_wakeup_key();
2640         return IRQ_HANDLED;
2641 }
2642
2643
2644
2645 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2646 {
2647         int ret;
2648         int i;
2649         struct kobject *rk818_fg_kobj;
2650
2651         ret = device_create_file(dev, &dev_attr_rk818batparam);
2652         if (ret) {
2653                 ret = -EINVAL;
2654                 dev_err(dev, "failed to create bat param file\n");
2655                 goto err_sysfs;
2656         }
2657
2658         ret = create_sysfs_interfaces(dev);
2659         if (ret < 0) {
2660                 ret = -EINVAL;
2661                 dev_err(dev, "device RK818 battery sysfs register failed\n");
2662                 goto err_sysfs;
2663         }
2664
2665         rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2666         if (!rk818_fg_kobj)
2667                 return -ENOMEM;
2668         for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2669                 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2670                 if (ret != 0) {
2671                         dev_err(dev, "create rk818_battery node error\n");
2672                         goto err_sysfs;
2673                 }
2674         }
2675
2676         return ret;
2677
2678 err_sysfs:
2679         power_supply_unregister(&di->ac);
2680         power_supply_unregister(&di->usb);
2681         power_supply_unregister(&di->bat);
2682
2683         return ret;
2684 }
2685
2686 static void rk818_battery_irq_init(struct battery_info *di)
2687 {
2688         int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2689         int ret;
2690         struct rk818 *chip = di->rk818;
2691
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);
2696
2697         ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2698                                         IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2699         if (ret != 0)
2700                 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2701
2702         di->irq = vb_lo_irq;
2703         enable_irq_wake(di->irq);
2704         disable_vbat_low_irq(di);
2705
2706         ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2707                                         IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2708         if (ret != 0)
2709                 dev_err(chip->dev, "plug_in_irq request failed!\n");
2710
2711
2712         ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2713                                         IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2714         if (ret != 0)
2715                 dev_err(chip->dev, "plug_out_irq request failed!\n");
2716
2717
2718         ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2719                                         IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2720         if (ret != 0)
2721                 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2722 }
2723
2724 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2725 {
2726         u32 fcc_capacity;
2727
2728         di->rk818 = chip;
2729         g_battery = di;
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;
2739         di->work_on = 0;
2740         di->sys_wakeup = true;
2741         di->pcb_ioffset = 0;
2742         di->pcb_ioffset_updated = false;
2743         di->queue_work_cnt = 0;
2744         di->update_k = 0;
2745         di->update_q = 0;
2746         di->voltage_old = 0;
2747         di->display_soc = 0;
2748         di->bat_res = 0;
2749         di->bat_res_updated = false;
2750         di->resume = false;
2751         di->sys_wakeup = true;
2752         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2753
2754         di->debug_finish_real_soc = 0;
2755         di->debug_finish_temp_soc = 0;
2756
2757         fcc_capacity = _get_FCC_capacity(di);
2758         if (fcc_capacity > 1000)
2759                 di->fcc = fcc_capacity;
2760         else
2761                 di->fcc = di->design_capacity;
2762 }
2763 /*
2764 static struct of_device_id rk818_battery_of_match[] = {
2765 { .compatible = "rk818_battery" },
2766 { }
2767 };
2768
2769 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2770 */
2771 #ifdef CONFIG_OF
2772 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2773 {
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;
2779         u32 out_value;
2780         int length, ret;
2781
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");
2785                 return -EINVAL;
2786         }
2787
2788         regs = of_find_node_by_name(rk818_pmic_np, "battery");
2789         if (!regs) {
2790                 dev_err(dev, "battery node not found!\n");
2791                 return -EINVAL;
2792         }
2793
2794         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2795         if (!data) {
2796                 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2797                 return -ENOMEM;
2798         }
2799         memset(data, 0, sizeof(*data));
2800
2801         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2802         if (!cell_cfg) {
2803                 dev_err(dev, "kzalloc for cell_config failed!\n");
2804                 return -ENOMEM;
2805         }
2806         ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2807         if (!ocv_cfg) {
2808                 dev_err(dev, "kzalloc for ocv_config failed!\n");
2809                 return -ENOMEM;
2810         }
2811
2812         prop = of_find_property(regs, "ocv_table", &length);
2813         if (!prop) {
2814                 dev_err(dev, "ocv_table not found!\n");
2815                 return -EINVAL;
2816         }
2817         data->ocv_size = length / sizeof(u32);
2818
2819         if (data->ocv_size > 0) {
2820                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2821
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");
2825                         return -ENOMEM;
2826                 }
2827                 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2828                 if (ret < 0)
2829                         return ret;
2830         }
2831
2832         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2833         if (ret < 0) {
2834                 dev_err(dev, "max_charge_currentmA not found!\n");
2835                 return ret;
2836         }
2837         data->max_charger_currentmA = out_value;
2838
2839         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
2840         if (ret < 0) {
2841                 dev_err(dev, "max_charge_voltagemV not found!\n");
2842                 return ret;
2843         }
2844         data->max_charger_voltagemV = out_value;
2845
2846         ret = of_property_read_u32(regs, "design_capacity", &out_value);
2847         if (ret < 0) {
2848                 dev_err(dev, "design_capacity not found!\n");
2849                 return ret;
2850         }
2851         cell_cfg->design_capacity  = out_value;
2852
2853         ret = of_property_read_u32(regs, "design_qmax", &out_value);
2854         if (ret < 0) {
2855                 dev_err(dev, "design_qmax not found!\n");
2856                 return ret;
2857         }
2858         cell_cfg->design_qmax = out_value;
2859
2860         ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
2861         if (ret < 0) {
2862                 dev_err(dev, "sleep_enter_current not found!\n");
2863                 return ret;
2864         }
2865         ocv_cfg->sleep_enter_current = out_value;
2866
2867         ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
2868         if (ret < 0) {
2869                 dev_err(dev, "sleep_exit_current not found!\n");
2870                 return ret;
2871         }
2872         ocv_cfg->sleep_exit_current = out_value;
2873
2874         ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
2875
2876         cell_cfg->ocv = ocv_cfg;
2877         data->cell_cfg = cell_cfg;
2878         rk818->battery_data = data;
2879
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");
2889         return 0;
2890 }
2891
2892 #else
2893 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2894 {
2895         return -ENODEV;
2896 }
2897 #endif
2898
2899
2900 static int battery_probe(struct platform_device *pdev)
2901 {
2902         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
2903         struct battery_info *di;
2904         int ret;
2905
2906         DBG("battery driver version %s\n", DRIVER_VERSION);
2907         di = kzalloc(sizeof(*di), GFP_KERNEL);
2908         if (!di) {
2909                 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
2910                 return -ENOMEM;
2911         }
2912         ret = rk_battery_parse_dt(chip, &pdev->dev);
2913         if (ret < 0) {
2914                 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
2915                 return -EINVAL;
2916         }
2917
2918         platform_set_drvdata(pdev, di);
2919         battery_info_init(di, chip);
2920         fg_init(di);
2921
2922         wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
2923
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);
2928         if (ret) {
2929                 dev_err(&pdev->dev, "rk power supply register failed!\n");
2930                 return ret;
2931         }
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);
2936
2937         di->battery_nb.notifier_call = battery_notifier_call;
2938         register_battery_notifier(&di->battery_nb);
2939
2940         rk818_battery_irq_init(di);
2941         rk818_battery_sysfs_init(di, &pdev->dev);
2942         DBG("------ RK81x battery_probe ok!-------\n");
2943         return ret;
2944 }
2945
2946
2947 #ifdef CONFIG_PM
2948
2949 static int battery_suspend(struct platform_device *dev, pm_message_t state)
2950 {
2951         struct battery_info *di = platform_get_drvdata(dev);
2952
2953         enable_vbat_low_irq(di);
2954         di->sleep_status = di->status;
2955         di->suspend_charge_current = _get_average_current(di);
2956
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;
2963         }
2964
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);
2969
2970         return 0;
2971 }
2972
2973 static int battery_resume(struct platform_device *dev)
2974 {
2975         struct battery_info *di = platform_get_drvdata(dev);
2976
2977         di->resume = true;
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));
2982
2983         if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING || di->real_soc <= 5)
2984                 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
2985
2986         return 0;
2987 }
2988 static int battery_remove(struct platform_device *dev)
2989 {
2990         struct battery_info *di = platform_get_drvdata(dev);
2991
2992         cancel_delayed_work_sync(&di->battery_monitor_work);
2993         return 0;
2994 }
2995 static void battery_shutdown(struct platform_device *dev)
2996 {
2997         struct battery_info *di = platform_get_drvdata(dev);
2998
2999         cancel_delayed_work_sync(&di->battery_monitor_work);
3000         DBG("rk818 shutdown!");
3001 }
3002 #endif
3003
3004 static struct platform_driver battery_driver = {
3005         .driver     = {
3006                 .name   = "rk818-battery",
3007                 .owner  = THIS_MODULE,
3008         },
3009
3010         .probe      = battery_probe,
3011         .remove     = battery_remove,
3012         .suspend    = battery_suspend,
3013         .resume     = battery_resume,
3014         .shutdown  = battery_shutdown,
3015 };
3016
3017 static int __init battery_init(void)
3018 {
3019         return platform_driver_register(&battery_driver);
3020 }
3021
3022 fs_initcall_sync(battery_init);
3023 static void __exit battery_exit(void)
3024 {
3025         platform_driver_unregister(&battery_driver);
3026 }
3027 module_exit(battery_exit);
3028
3029 MODULE_LICENSE("GPL");
3030 MODULE_ALIAS("platform:rk818-battery");
3031 MODULE_AUTHOR("ROCKCHIP");
3032