Merge branch develop-3.10 into develop-3.10-next
[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 *g_battery;
162 u32 support_uboot_chrg;
163
164 extern int dwc_vbus_status(void);
165 extern int get_gadget_connect_flag(void);
166 extern int dwc_otg_check_dpdm(void);
167 extern void kernel_power_off(void);
168 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
169 extern unsigned int irq_create_mapping(struct irq_domain *domain,
170                                                                                         irq_hw_number_t hwirq);
171 extern void rk_send_wakeup_key(void);
172 static void update_battery_info(struct battery_info *di);
173
174 #define SUPPORT_USB_CHARGE
175
176
177 static u32 interpolate(int value, u32 *table, int size)
178 {
179         uint8_t i;
180         uint16_t d;
181
182         for (i = 0; i < size; i++) {
183                 if (value < table[i])
184                         break;
185         }
186
187         if ((i > 0) && (i < size)) {
188                 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
189                 d /= table[i] - table[i-1];
190                 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
191         } else {
192                 d = i * ((INTERPOLATE_MAX+size/2)/size);
193         }
194
195         if (d > 1000)
196                 d = 1000;
197
198         return d;
199 }
200 /* Returns (a * b) / c */
201 static int32_t ab_div_c(u32 a, u32 b, u32 c)
202 {
203         bool sign;
204         u32 ans = MAX_INT;
205         int32_t tmp;
206
207         sign = ((((a^b)^c) & 0x80000000) != 0);
208
209         if (c != 0) {
210                 if (sign)
211                         c = -c;
212
213                 tmp = ((int32_t) a*b + (c>>1)) / c;
214
215                 if (tmp < MAX_INT)
216                         ans = tmp;
217         }
218
219         if (sign)
220                 ans = -ans;
221
222         return ans;
223 }
224
225 static  int32_t abs_int(int32_t x)
226 {
227         return (x > 0) ? x : -x;
228 }
229
230 static  int abs32_int(int x)
231 {
232         return (x > 0) ? x : -x;
233 }
234
235
236 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
237 {
238         int ret;
239
240         ret = rk818_i2c_read(rk818, reg, len, buf);
241         return ret;
242 }
243
244 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
245 {
246         int ret;
247         ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
248         return ret;
249 }
250 static void dump_gauge_register(struct battery_info *di)
251 {
252         int i = 0;
253         char buf;
254         DBG("%s dump charger register start: \n", __func__);
255         for (i = 0xAC; i < 0xDF; i++) {
256                 battery_read(di->rk818, i, &buf, 1);
257                 DBG(" the register is  0x%02x, the value is 0x%02x\n ", i, buf);
258         }
259         DBG("demp end!\n");
260 }
261
262 static void dump_charger_register(struct battery_info *di)
263 {
264
265         int i = 0;
266         char buf;
267         DBG("%s dump the register start: \n", __func__);
268         for (i = 0x99; i < 0xAB; i++) {
269                 battery_read(di->rk818, i, &buf, 1);
270                 DBG(" the register is  0x%02x, the value is 0x%02x\n ", i, buf);
271         }
272         DBG("demp end!\n");
273
274 }
275
276 #if RK818_SYS_DBG
277
278 static uint16_t _get_OCV_voltage(struct battery_info *di);
279 static int _voltage_to_capacity(struct battery_info *di, int voltage);
280 static int _get_realtime_capacity(struct battery_info *di);
281 static void power_on_save(struct   battery_info *di, int voltage);
282 static void  _capacity_init(struct battery_info *di, u32 capacity);
283 static void battery_poweron_status_init(struct battery_info *di);
284 static void power_on_save(struct   battery_info *di, int voltage);
285 static void flatzone_voltage_init(struct battery_info *di);
286 static int _get_FCC_capacity(struct battery_info *di);
287 static void  _save_FCC_capacity(struct battery_info *di, u32 capacity);
288 static int _get_soc(struct   battery_info *di);
289 static int  _get_average_current(struct battery_info *di);
290 static int rk_battery_voltage(struct battery_info *di);
291 static uint16_t _get_relax_vol1(struct battery_info *di);
292 static uint16_t _get_relax_vol2(struct battery_info *di);
293 static void update_battery_info(struct battery_info *di);
294
295 static ssize_t bat_state_read(struct device *dev, struct device_attribute *attr, char *buf)
296 {
297         struct battery_info *di = g_battery;
298         u8 status;
299         u8 rtc_val;
300         u8 soc_reg;
301         u8 shtd_time;
302
303         battery_read(di->rk818, SUP_STS_REG, &status, 1);
304         battery_read(di->rk818, SOC_REG, &soc_reg, 1);
305         battery_read(di->rk818, 0x00, &rtc_val, 1);
306         di->voltage_ocv = _get_OCV_voltage(di);
307         _voltage_to_capacity(di, di->voltage_ocv);
308         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &shtd_time, 1);
309
310         return sprintf(buf, "-----------------------------------------------------------------------------\n"
311                         "volt = %d, ocv_volt = %d, avg_current = %d, remain_cap = %d, ocv_cap = %d\n"
312                         "real_soc = %d, temp_soc = %d\n"
313                         "fcc = %d, FCC_REG = %d, shutdown_time = %d\n"
314                         "usb_online = %d, ac_online = %d\n"
315                         "SUP_STS_REG(0xc7) = 0x%02x, RTC_REG = 0x%02x\n"
316                         "voltage_k = %d, voltage_b = %d, SOC_REG = 0x%02x\n"
317                         "relax_volt1 = %d, relax_volt2 = %d\n"
318                         "---------------------------------------------------------------------------\n",
319                         rk_battery_voltage(di), di->voltage_ocv, _get_average_current(di), _get_realtime_capacity(di), di->temp_nac,
320                         di->real_soc, _get_soc(di),
321                         di->fcc, _get_FCC_capacity(di), shtd_time,
322                         di->usb_online, di->ac_online,
323                         status, rtc_val,
324                         di->voltage_k, di->voltage_b, soc_reg,
325                         _get_relax_vol1(di), _get_relax_vol2(di));
326 }
327
328 static ssize_t bat_reg_read(struct device *dev, struct device_attribute *attr, char *buf)
329 {
330         struct battery_info *di = g_battery;
331         u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
332         u8 usb_ctrl_reg, chrg_ctrl_reg1;
333         u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
334
335         battery_read(di->rk818, GGCON, &ggcon_reg, 1);
336         battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
337         battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
338         battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
339         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
340         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
341         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
342         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
343         battery_read(di->rk818, 0x00, &rtc_val, 1);
344
345         return sprintf(buf, "\n------------- dump_debug_regs -----------------\n"
346             "GGCON = 0x%2x, GGSTS = 0x%2x, RTC  = 0x%2x\n"
347             "SUP_STS_REG  = 0x%2x, VB_MOD_REG   = 0x%2x\n"
348             "USB_CTRL_REG  = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
349             "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n"
350             "---------------------------------------------------------------------------\n",
351             ggcon_reg, ggsts_reg, rtc_val,
352             sup_tst_reg, vb_mod_reg,
353             usb_ctrl_reg, chrg_ctrl_reg1,
354             chrg_ctrl_reg2, chrg_ctrl_reg3
355            );
356 }
357 static ssize_t bat_fcc_read(struct device *dev, struct device_attribute *attr, char *buf)
358 {
359         struct battery_info *di = g_battery;
360
361         return sprintf(buf, "%d", di->fcc);
362 }
363 static ssize_t bat_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
364 {
365         struct battery_info *di = g_battery;
366
367         return sprintf(buf, "%d", di->real_soc);
368 }
369
370 static ssize_t bat_temp_soc_read(struct device *dev, struct device_attribute *attr, char *buf)
371 {
372         struct battery_info *di = g_battery;
373
374         return sprintf(buf, "%d", di->temp_soc);
375 }
376
377 static ssize_t bat_voltage_read(struct device *dev, struct device_attribute *attr, char *buf)
378 {
379         struct battery_info *di = g_battery;
380
381         return sprintf(buf, "%d", di->voltage);
382 }
383
384 static ssize_t bat_avr_current_read(struct device *dev, struct device_attribute *attr, char *buf)
385 {
386         struct battery_info *di = g_battery;
387
388         return sprintf(buf, "%d", di->current_avg);
389 }
390
391 static ssize_t bat_remain_capacity_read(struct device *dev, struct device_attribute *attr, char *buf)
392 {
393         struct battery_info *di = g_battery;
394
395         return sprintf(buf, "%d", di->remain_capacity);
396 }
397
398 static struct device_attribute rk818_bat_attr[] = {
399         __ATTR(state, 0664, bat_state_read, NULL),
400         __ATTR(regs, 0664, bat_reg_read, NULL),
401         __ATTR(fcc, 0664, bat_fcc_read, NULL),
402         __ATTR(soc, 0664, bat_soc_read, NULL),
403         __ATTR(temp_soc, 0664, bat_temp_soc_read, NULL),
404         __ATTR(voltage, 0664, bat_voltage_read, NULL),
405         __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
406         __ATTR(remain_capacity, 0664, bat_remain_capacity_read, NULL),
407 };
408
409 #endif
410
411 static uint16_t get_relax_voltage(struct battery_info *di);
412
413 static ssize_t show_state_attrs(struct device *dev,
414                                 struct device_attribute *attr, char *buf)
415 {
416         struct battery_info *data = g_battery;
417
418         if (0 == get_relax_voltage(data)) {
419                 return sprintf(buf,
420                                 "voltage = %d, remain_capacity = %d, status = %d\n",
421                                 data->voltage, data->remain_capacity,
422                                 data->status);
423
424         } else
425                 return sprintf(buf,
426                                 "voltage = %d, remain_capacity = %d, status = %d\n",
427                                 get_relax_voltage(data), data->remain_capacity,
428                                 data->status);
429 }
430
431 static ssize_t restore_state_attrs(struct device *dev,
432                                 struct device_attribute *attr, const char *buf, size_t size)
433 {
434         return size;
435 }
436 static struct device_attribute rkbatt_attrs[] = {
437         __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
438 };
439
440 static int create_sysfs_interfaces(struct device *dev)
441 {
442         int liTmep;
443
444         for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++)   {
445                 if (device_create_file(dev, rkbatt_attrs + liTmep))
446                         goto error;
447         }
448
449         return 0;
450
451 error:
452         for (; liTmep >= 0; liTmep--)
453                 device_remove_file(dev, rkbatt_attrs + liTmep);
454
455         dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
456         return -1;
457 }
458
459 static int debug_reg(struct battery_info *di, u8 reg, char *reg_name)
460 {
461         u8 val;
462
463         battery_read(di->rk818, reg, &val, 1);
464         DBG("<%s>: %s = 0x%2x\n", __func__, reg_name, val);
465         return val;
466 }
467
468
469 static int  _gauge_enable(struct battery_info *di)
470 {
471         int ret;
472         u8 buf;
473
474         ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
475         if (ret < 0) {
476                 dev_err(di->dev, "error reading TS_CTRL_REG");
477                 return ret;
478         }
479         if (!(buf & GG_EN)) {
480                 buf |= GG_EN;
481                 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1);  /* enable */
482                 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
483                 return 0;
484         }
485
486         DBG("%s, %d\n", __func__, buf);
487         return 0;
488
489 }
490 static void save_level(struct  battery_info *di, u8 save_soc)
491 {
492         u8 soc;
493
494         soc = save_soc;
495         battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
496 }
497 static u8 get_level(struct  battery_info *di)
498 {
499         u8 soc;
500
501         battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
502         return soc;
503 }
504
505 static int _get_vcalib0(struct battery_info *di)
506 {
507         int ret;
508         int temp = 0;
509         u8 buf;
510
511         ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
512         temp = buf;
513         ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
514         temp |= buf<<8;
515
516         DBG("%s voltage0 offset vale is %d\n", __func__, temp);
517         return temp;
518 }
519
520 static int _get_vcalib1(struct  battery_info *di)
521 {
522         int ret;
523         int temp = 0;
524         u8 buf;
525
526         ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
527         temp = buf;
528         ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
529         temp |= buf<<8;
530
531         DBG("%s voltage1 offset vale is %d\n", __func__, temp);
532         return temp;
533 }
534
535 static int _get_ioffset(struct battery_info *di)
536 {
537
538         int ret;
539         int temp = 0;
540         u8 buf;
541
542         ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
543         temp = buf;
544         ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
545         temp |= buf<<8;
546
547         return temp;
548 }
549
550 static uint16_t  _get_cal_offset(struct battery_info *di)
551 {
552         int ret;
553         uint16_t temp = 0;
554         u8 buf;
555
556         ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
557         temp = buf;
558         ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
559         temp |= buf<<8;
560
561         return temp;
562 }
563 static int _set_cal_offset(struct battery_info *di, u32 value)
564 {
565         int ret;
566         u8 buf;
567
568         buf = value&0xff;
569         ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
570         buf = (value >> 8)&0xff;
571         ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
572
573         return 0;
574 }
575 static void _get_voltage_offset_value(struct battery_info *di)
576 {
577         int vcalib0, vcalib1;
578
579         vcalib0 = _get_vcalib0(di);
580         vcalib1 = _get_vcalib1(di);
581
582         di->voltage_k = (4200 - 3000)*1000/(vcalib1 - vcalib0);
583         di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
584         DBG("voltage_k = %d(x1000) voltage_b = %d\n", di->voltage_k, di->voltage_b);
585 }
586 static uint16_t _get_OCV_voltage(struct battery_info *di)
587 {
588         int ret;
589         u8 buf;
590         uint16_t temp;
591         uint16_t voltage_now = 0;
592
593         ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
594         temp = buf;
595         ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
596         temp |= buf<<8;
597
598         if (ret < 0) {
599                 dev_err(di->dev, "error read BAT_OCV_REGH");
600                 return ret;
601         }
602
603         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
604
605         return voltage_now;
606 }
607
608 static int rk_battery_voltage(struct battery_info *di)
609 {
610         int ret;
611         int voltage_now = 0;
612         u8 buf;
613         int temp;
614
615         ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
616         temp = buf;
617         ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
618         temp |= buf<<8;
619
620         if (ret < 0) {
621                 dev_err(di->dev, "error read BAT_VOL_REGH");
622                 return ret;
623         }
624
625         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
626
627         return voltage_now;
628 }
629
630 /* OCV Lookup table
631  * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
632  * based on the voltage.
633  */
634 static int _voltage_to_capacity(struct battery_info *di, int voltage)
635 {
636         u32 *ocv_table;
637         int ocv_size;
638         u32 tmp;
639
640         ocv_table = di->platform_data->battery_ocv;
641         ocv_size = di->platform_data->ocv_size;
642         di->warnning_voltage = ocv_table[3];
643         tmp = interpolate(voltage, ocv_table, ocv_size);
644         di->temp_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
645         di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
646
647         return 0;
648 }
649
650 static uint16_t _get_relax_vol1(struct battery_info *di)
651 {
652         int ret;
653         u8 buf;
654         uint16_t temp = 0, voltage_now;
655
656         ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
657         temp = buf;
658         ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
659         temp |= (buf<<8);
660
661         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
662
663         return voltage_now;
664 }
665
666 static uint16_t _get_relax_vol2(struct battery_info *di)
667 {
668         int ret;
669         uint16_t temp = 0, voltage_now;
670         u8 buf;
671
672         ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
673         temp = buf;
674         ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
675         temp |= (buf<<8);
676
677         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
678
679         return voltage_now;
680 }
681
682 static int  _get_raw_adc_current(struct battery_info *di)
683 {
684         u8 buf;
685         int ret;
686         int current_now;
687
688         ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
689         if (ret < 0) {
690                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
691                 return ret;
692         }
693         current_now = buf;
694         ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
695         if (ret < 0) {
696                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
697                 return ret;
698         }
699         current_now |= (buf<<8);
700
701         if (ret < 0) {
702                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
703                 return ret;
704         }
705
706         return current_now;
707
708 }
709
710
711 static void ioffset_sample_time(struct battery_info *di, int time)
712 {
713         u8 ggcon;
714
715         battery_read(di->rk818, GGCON, &ggcon, 1);
716         ggcon &= ~(0x30); /*clear <5:4>*/
717         ggcon |= time;
718         battery_write(di->rk818, GGCON, &ggcon, 1);
719         debug_reg(di, GGCON, "GGCON");
720 }
721
722 static void update_cal_offset(struct battery_info *di)
723 {
724         int mod = di->queue_work_cnt % TIME_10MIN_SEC;
725
726         DBG("<%s>, queue_work_cnt = %lu, mod = %d\n", __func__, di->queue_work_cnt, mod);
727         if ((!mod) && (di->pcb_ioffset_updated)) {
728                 _set_cal_offset(di, di->pcb_ioffset+_get_ioffset(di));
729                 DBG("<%s>. 10min update cal_offset = %d", __func__, di->pcb_ioffset+_get_ioffset(di));
730         }
731 }
732
733
734 static void zero_current_calibration(struct battery_info *di)
735 {
736         int adc_value;
737         uint16_t C0;
738         uint16_t C1;
739         int ioffset;
740         int pcb_offset;
741         u8 retry = 0;
742
743         if ((di->charge_status == CHARGE_FINISH) && (abs32_int(di->current_avg) > 4)) {
744
745                 for (retry = 0; retry < 5; retry++) {
746                         adc_value = _get_raw_adc_current(di);
747                         DBG("<%s>. adc_value = %d\n", __func__, adc_value);
748
749                         C0 = _get_cal_offset(di);
750                         C1 = adc_value + C0;
751                         _set_cal_offset(di, C1);
752                         DBG("<%s>. C1 = %d\n", __func__, C1);
753                         msleep(2000);
754
755                         adc_value = _get_raw_adc_current(di);
756                         DBG("<%s>. adc_value = %d\n", __func__, adc_value);
757                         if (adc_value < 4) {
758
759                                 ioffset = _get_ioffset(di);
760                                 pcb_offset = C1 - ioffset;
761                                 di->pcb_ioffset = pcb_offset;
762                                 di->pcb_ioffset_updated  = true;
763                                 DBG("<%s>. update the cal_offset, pcb_offset = %d\n", __func__, pcb_offset);
764                                 break;
765                         } else
766                                 di->pcb_ioffset_updated  = false;
767                 }
768         }
769 }
770
771
772 static bool  _is_relax_mode(struct battery_info *di)
773 {
774         int ret;
775         u8 status;
776
777         ret = battery_read(di->rk818, GGSTS, &status, 1);
778
779         if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
780                 return false;
781         else
782                 return true;
783 }
784
785 static uint16_t get_relax_voltage(struct battery_info *di)
786 {
787         int ret;
788         u8 status;
789         uint16_t relax_vol1, relax_vol2;
790         u8 ggcon;
791
792         ret = battery_read(di->rk818, GGSTS, &status, 1);
793         ret = battery_read(di->rk818, GGCON, &ggcon, 1);
794
795         relax_vol1 = _get_relax_vol1(di);
796         relax_vol2 = _get_relax_vol2(di);
797         DBG("<%s>. GGSTS = 0x%x, GGCON = 0x%x, relax_vol1 = %d, relax_vol2 = %d\n", __func__, status, ggcon, relax_vol1, relax_vol2);
798         if (_is_relax_mode(di))
799                 return relax_vol1 > relax_vol2?relax_vol1:relax_vol2;
800         else
801                 return 0;
802 }
803
804 static void  _set_relax_thres(struct battery_info *di)
805 {
806         u8 buf;
807         int enter_thres, exit_thres;
808         struct cell_state *cell = &di->cell;
809
810         enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
811         exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
812
813         buf  = enter_thres&0xff;
814         battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
815         buf = (enter_thres>>8)&0xff;
816         battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
817
818         buf  = exit_thres&0xff;
819         battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
820         buf = (exit_thres>>8)&0xff;
821         battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
822
823         /* set sample time */
824         battery_read(di->rk818, GGCON, &buf, 1);
825         buf &= ~(3<<2);/*8min*/
826         buf &= ~0x01; /* clear bat_res calc*/
827         battery_write(di->rk818, GGCON, &buf, 1);
828 }
829
830 static void restart_relax(struct battery_info *di)
831 {
832         u8 ggcon;/* chrg_ctrl_reg2;*/
833         u8 ggsts;
834
835         battery_read(di->rk818, GGCON, &ggcon, 1);
836         ggcon &= ~0x0c;
837         battery_write(di->rk818, GGCON, &ggcon, 1);
838
839         battery_read(di->rk818, GGSTS, &ggsts, 1);
840         ggsts &= ~0x0c;
841         battery_write(di->rk818, GGSTS, &ggsts, 1);
842 }
843
844 static int  _get_average_current(struct battery_info *di)
845 {
846         u8  buf;
847         int ret;
848         int current_now;
849         int temp;
850
851         ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
852         if (ret < 0) {
853                 dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
854                 return ret;
855         }
856         current_now = buf;
857         ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
858         if (ret < 0) {
859                 dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
860                 return ret;
861         }
862         current_now |= (buf<<8);
863
864         if (current_now & 0x800)
865                 current_now -= 4096;
866
867         temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
868
869         return temp;
870
871 }
872
873 static bool is_bat_exist(struct  battery_info *di)
874 {
875         u8 buf;
876
877         battery_read(di->rk818, SUP_STS_REG, &buf, 1);
878         return (buf & 0x80) ? true : false;
879 }
880
881 static bool _is_first_poweron(struct  battery_info *di)
882 {
883         u8 buf;
884         u8 temp;
885
886         battery_read(di->rk818, GGSTS, &buf, 1);
887         DBG("%s GGSTS value is 0x%2x \n", __func__, buf);
888         /*di->pwron_bat_con = buf;*/
889         if (buf&BAT_CON) {
890                 buf &= ~(BAT_CON);
891                 do {
892                         battery_write(di->rk818, GGSTS, &buf, 1);
893                         battery_read(di->rk818, GGSTS, &temp, 1);
894                 } while (temp&BAT_CON);
895                 return true;
896         }
897         return false;
898 }
899 static void flatzone_voltage_init(struct battery_info *di)
900 {
901         u32 *ocv_table;
902         int ocv_size;
903         int temp_table[21];
904         int i, j;
905
906         ocv_table = di->platform_data->battery_ocv;
907         ocv_size = di->platform_data->ocv_size;
908
909         for (j = 0; j < 21; j++)
910                 temp_table[j] = 0;
911
912         j = 0;
913         for (i = 1; i < ocv_size-1; i++) {
914                 if (ocv_table[i+1] < ocv_table[i] + 20)
915                         temp_table[j++] = i;
916         }
917
918         temp_table[j] = temp_table[j-1]+1;
919         i = temp_table[0];
920         di->enter_flatzone = ocv_table[i];
921         j = 0;
922
923
924         for (i = 0; i <= 20; i++) {
925                 if (temp_table[i] < temp_table[i+1])
926                         j = i+1;
927         }
928
929         i = temp_table[j];
930         di->exit_flatzone = ocv_table[i];
931
932         DBG("enter_flatzone = %d exit_flatzone = %d\n", di->enter_flatzone, di->exit_flatzone);
933
934 }
935
936 #if 0
937 static int is_not_flatzone(struct   battery_info *di, int voltage)
938 {
939         if ((voltage >= di->enter_flatzone) && (voltage <= di->exit_flatzone)) {
940                 DBG("<%s>. is in flat zone\n", __func__);
941                 return 0;
942         } else {
943                 DBG("<%s>. is not in flat zone\n", __func__);
944                 return 1;
945         }
946 }
947 #endif
948 static void power_on_save(struct   battery_info *di, int voltage)
949 {
950         u8 buf;
951         u8 save_soc;
952
953         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &buf, 1);
954
955         if (_is_first_poweron(di) || buf > 30) { /* first power-on or power off time > 30min */
956                 _voltage_to_capacity(di, voltage);
957                 if (di->temp_soc < 20) {
958                         di->dod0_voltage = voltage;
959                         di->dod0_capacity = di->nac;
960                         di->dod0_status = 1;
961                         di->dod0 = di->temp_soc;/* _voltage_to_capacity(di, voltage); */
962                         di->dod0_level = 80;
963
964                         if (di->temp_soc <= 0)
965                                 di->dod0_level = 100;
966                         else if (di->temp_soc < 5)
967                                 di->dod0_level = 95;
968                         else if (di->temp_soc < 10)
969                                 di->dod0_level = 90;
970                         /* save_soc = di->dod0_level; */
971                         save_soc = get_level(di);
972                         if (save_soc <  di->dod0_level)
973                                 save_soc = di->dod0_level;
974                         save_level(di, save_soc);
975                         DBG("<%s>UPDATE-FCC POWER ON : dod0_voltage = %d, dod0_capacity = %d ", __func__, di->dod0_voltage, di->dod0_capacity);
976                 }
977         }
978
979 }
980
981
982 static int _get_soc(struct   battery_info *di)
983 {
984         return di->remain_capacity * 100 / di->fcc;
985 }
986
987 static enum power_supply_property rk_battery_props[] = {
988
989         POWER_SUPPLY_PROP_STATUS,
990         POWER_SUPPLY_PROP_CURRENT_NOW,
991         POWER_SUPPLY_PROP_VOLTAGE_NOW,
992         POWER_SUPPLY_PROP_PRESENT,
993         POWER_SUPPLY_PROP_HEALTH,
994         POWER_SUPPLY_PROP_CAPACITY,
995 };
996
997 #define to_device_info(x) container_of((x), \
998                                 struct battery_info, bat)
999
1000 static int rk_battery_get_property(struct power_supply *psy,
1001         enum power_supply_property psp,
1002         union power_supply_propval *val)
1003 {
1004         u8 buf;
1005         struct battery_info *di = to_device_info(psy);
1006
1007         switch (psp) {
1008         case POWER_SUPPLY_PROP_CURRENT_NOW:
1009                 val->intval = di->current_avg*1000;/*uA*/
1010                 break;
1011
1012         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1013                 val->intval = di->voltage*1000;/*uV*/
1014                 break;
1015
1016         case POWER_SUPPLY_PROP_PRESENT:
1017                 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1018                 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1019                 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1020                 break;
1021
1022
1023         case POWER_SUPPLY_PROP_CAPACITY:
1024                 val->intval = di->real_soc;
1025                 break;
1026
1027         case POWER_SUPPLY_PROP_HEALTH:
1028                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1029                 break;
1030
1031         case POWER_SUPPLY_PROP_STATUS:
1032                 val->intval = di->status;
1033                 break;
1034
1035         default:
1036                 return -EINVAL;
1037         }
1038
1039         return 0;
1040 }
1041
1042
1043 static enum power_supply_property rk_battery_ac_props[] = {
1044         POWER_SUPPLY_PROP_ONLINE,
1045 };
1046 static enum power_supply_property rk_battery_usb_props[] = {
1047         POWER_SUPPLY_PROP_ONLINE,
1048 };
1049
1050
1051 #define to_ac_device_info(x) container_of((x), \
1052                                 struct battery_info, ac)
1053
1054 static int rk_battery_ac_get_property(struct power_supply *psy,
1055         enum power_supply_property psp,
1056         union power_supply_propval *val)
1057 {
1058         int ret = 0;
1059         struct battery_info *di = to_ac_device_info(psy);
1060
1061         switch (psp) {
1062         case POWER_SUPPLY_PROP_ONLINE:
1063                 val->intval = di->ac_online;    /*discharging*/
1064                 break;
1065
1066         default:
1067                 ret = -EINVAL;
1068                 break;
1069         }
1070         return ret;
1071 }
1072
1073 #define to_usb_device_info(x) container_of((x), \
1074                                 struct battery_info, usb)
1075
1076 static int rk_battery_usb_get_property(struct power_supply *psy,
1077         enum power_supply_property psp,
1078         union power_supply_propval *val)
1079 {
1080         int ret = 0;
1081         struct battery_info *di = to_usb_device_info(psy);
1082
1083         switch (psp) {
1084         case POWER_SUPPLY_PROP_ONLINE:
1085                 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1086                         val->intval = 0;
1087                 else
1088                         val->intval = di->usb_online;
1089                 break;
1090
1091         default:
1092                 ret = -EINVAL;
1093                 break;
1094         }
1095
1096         return ret;
1097 }
1098
1099
1100 static void battery_power_supply_init(struct battery_info *di)
1101 {
1102         di->bat.name = "BATTERY";
1103         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1104         di->bat.properties = rk_battery_props;
1105         di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1106         di->bat.get_property = rk_battery_get_property;
1107
1108         di->ac.name = "AC";
1109         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1110         di->ac.properties = rk_battery_ac_props;
1111         di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1112         di->ac.get_property = rk_battery_ac_get_property;
1113
1114         di->usb.name = "USB";
1115         di->usb.type = POWER_SUPPLY_TYPE_USB;
1116         di->usb.properties = rk_battery_usb_props;
1117         di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1118         di->usb.get_property = rk_battery_usb_get_property;
1119 }
1120
1121 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1122 {
1123         int ret;
1124
1125         ret = power_supply_register(dev, &di->bat);
1126         if (ret) {
1127                 dev_err(dev, "failed to register main battery\n");
1128                 goto batt_failed;
1129         }
1130         ret = power_supply_register(dev, &di->usb);
1131         if (ret) {
1132                 dev_err(dev, "failed to register usb power supply\n");
1133                 goto usb_failed;
1134         }
1135         ret = power_supply_register(dev, &di->ac);
1136         if (ret) {
1137                 dev_err(dev, "failed to register ac power supply\n");
1138                 goto ac_failed;
1139         }
1140
1141         return 0;
1142
1143 ac_failed:
1144         power_supply_unregister(&di->ac);
1145 usb_failed:
1146         power_supply_unregister(&di->usb);
1147 batt_failed:
1148         power_supply_unregister(&di->bat);
1149
1150         return ret;
1151 }
1152
1153 static void  _capacity_init(struct battery_info *di, u32 capacity)
1154 {
1155         u8 buf;
1156         u32 capacity_ma;
1157
1158         di->update_k = 0;
1159         di->update_q = 0;
1160         di->voltage_old = 0;
1161         di->display_soc = 0;
1162
1163         capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1164         do {
1165                 buf = (capacity_ma>>24)&0xff;
1166                 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1167                 buf = (capacity_ma>>16)&0xff;
1168                 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1169                 buf = (capacity_ma>>8)&0xff;
1170                 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1171                 buf = (capacity_ma&0xff) | 0x01;
1172                 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1173                 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1174
1175         } while (buf == 0);
1176 }
1177
1178
1179 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
1180 {
1181         u8 buf;
1182         u32 capacity_ma;
1183
1184         if (capacity >= di->qmax)
1185                 capacity = di->qmax;
1186
1187         capacity_ma = capacity;
1188
1189         buf = (capacity_ma>>24)&0xff;
1190         battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1191         buf = (capacity_ma>>16)&0xff;
1192         battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1193         buf = (capacity_ma>>8)&0xff;
1194         battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1195         buf = (capacity_ma&0xff) | 0x01;
1196         battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1197 }
1198
1199 static int _get_remain_capacity(struct battery_info *di)
1200 {
1201         int ret;
1202         int temp = 0;
1203         u8 buf;
1204         u32 capacity;
1205
1206         ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1207         temp = buf << 24;
1208         ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1209         temp |= buf << 16;
1210         ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1211         temp |= buf << 8;
1212         ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1213         temp |= buf;
1214
1215         capacity = temp;/* /4096*900/14/36*500/521; */
1216
1217         return capacity;
1218 }
1219
1220
1221 static void  _save_FCC_capacity(struct battery_info *di, u32 capacity)
1222 {
1223         u8 buf;
1224         u32 capacity_ma;
1225
1226         capacity_ma = capacity;
1227         buf = (capacity_ma>>24)&0xff;
1228         battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1229         buf = (capacity_ma>>16)&0xff;
1230         battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1231         buf = (capacity_ma>>8)&0xff;
1232         battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1233         buf = (capacity_ma&0xff) | 0x01;
1234         battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1235 }
1236
1237 static int _get_FCC_capacity(struct battery_info *di)
1238 {
1239         int ret;
1240         int temp = 0;
1241         u8 buf;
1242         u32 capacity;
1243
1244         ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1245         temp = buf << 24;
1246         ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1247         temp |= buf << 16;
1248         ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1249         temp |= buf << 8;
1250         ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1251         temp |= buf;
1252
1253         if (temp > 1)
1254                 capacity = temp-1;/* 4096*900/14/36*500/521 */
1255         else
1256                 capacity = temp;
1257         DBG("%s NEW_FCC_REG %d  capacity = %d\n", __func__, temp, capacity);
1258
1259         return capacity;
1260 }
1261
1262 static int _get_realtime_capacity(struct battery_info *di)
1263 {
1264         int ret;
1265         int temp = 0;
1266         u8 buf;
1267         u32 capacity;
1268
1269         ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1270         temp = buf << 24;
1271         ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1272         temp |= buf << 16;
1273         ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1274         temp |= buf << 8;
1275         ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1276         temp |= buf;
1277
1278         capacity = temp/2390;/* 4096*900/14/36*500/521; */
1279
1280         return capacity;
1281 }
1282
1283 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1284 {
1285         int remain_capacity;
1286         int relax_capacity;
1287         int now_temp_soc;
1288         int relax_soc;
1289         int abs_soc;
1290         int min, soc_time;
1291         int now_current;
1292
1293         now_temp_soc = _get_soc(di);
1294         _voltage_to_capacity(di, relax_voltage);
1295         relax_soc = di->temp_soc;
1296         relax_capacity = di->temp_nac;
1297         abs_soc = abs32_int(relax_soc - now_temp_soc);
1298
1299         DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1300         DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1301
1302         /*handle temp_soc*/
1303         if (abs32_int(di->real_soc - relax_soc) <= 5) {
1304                 remain_capacity = relax_capacity;
1305                 DBG("<%s>. real-soc is close to relax-soc, set:  temp_soc = relax_soc\n", __func__);
1306         } else {
1307                 if (abs_soc == 0)
1308                         remain_capacity = _get_realtime_capacity(di);
1309                 else if (abs_soc <= 10)
1310                         remain_capacity = relax_capacity;
1311                 else if (abs_soc <= 20)
1312                         remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1313                 else
1314                         remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1315         }
1316         _capacity_init(di, remain_capacity);
1317         di->temp_soc = _get_soc(di);
1318         di->remain_capacity  = _get_realtime_capacity(di);
1319
1320         /*handle real_soc*/
1321         DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1322         if (relax_soc < now_temp_soc) {
1323                 if (di->suspend_temp_soc - relax_soc <= 5)
1324                         di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1325                 else if (di->suspend_temp_soc - relax_soc <= 10)
1326                         di->real_soc = di->real_soc - 5;
1327                 else
1328                         di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1329         } else {
1330                 now_current = _get_average_current(di);
1331                 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1332                 min = soc_time / 60;
1333                 if (sleep_min > min)
1334                         di->real_soc--;
1335         }
1336
1337         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);
1338 }
1339
1340
1341 static int _copy_soc(struct  battery_info *di, u8 save_soc)
1342 {
1343         u8 soc;
1344
1345         soc = save_soc;
1346         battery_write(di->rk818, SOC_REG, &soc, 1);
1347         return 0;
1348 }
1349
1350 static bool support_uboot_charge(void)
1351 {
1352         return support_uboot_chrg?true:false;
1353 }
1354
1355 static int _rsoc_init(struct  battery_info *di)
1356 {
1357         u8 pwron_soc;
1358         u8 init_soc;
1359         u32 remain_capacity;
1360         u8 last_shtd_time;
1361         u8 curr_shtd_time;
1362 #ifdef SUPPORT_USB_CHARGE
1363         int otg_status;
1364 #else
1365         u8 buf;
1366 #endif
1367         di->voltage  = rk_battery_voltage(di);
1368         di->voltage_ocv = _get_OCV_voltage(di);
1369         DBG("OCV voltage = %d\n" , di->voltage_ocv);
1370
1371         if (_is_first_poweron(di)) {
1372                 _save_FCC_capacity(di, di->design_capacity);
1373                 di->fcc = _get_FCC_capacity(di);
1374
1375                 _voltage_to_capacity(di, di->voltage_ocv);
1376                 di->real_soc = di->temp_soc;
1377                 di->nac      = di->temp_nac;
1378                 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1379
1380         } else {
1381                 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1382                 init_soc = pwron_soc;
1383                 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1384
1385 #ifdef SUPPORT_USB_CHARGE
1386                 otg_status = dwc_otg_check_dpdm();
1387                 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1388                         init_soc = 1;
1389                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1390                 }
1391 #else
1392                 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1393                 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1394                         init_soc = 1;
1395                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1396                 }
1397 #endif
1398                 remain_capacity = _get_remain_capacity(di);
1399
1400                 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1401                 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1402                 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1403                 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1404
1405                 if (!support_uboot_charge()) {
1406                         _voltage_to_capacity(di, di->voltage_ocv);
1407                         DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1408
1409                         /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1410                         if (((otg_status != 0) && (curr_shtd_time > 0) && (last_shtd_time != curr_shtd_time)) || ((curr_shtd_time > 0) && (otg_status == 0))) {
1411
1412                                 if (curr_shtd_time > 30) {
1413                                         remain_capacity = di->temp_nac;
1414                                         DBG("<%s>shutdown_time > 30 minute,  remain_cap = %d\n", __func__, remain_capacity);
1415
1416                                 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - di->real_soc) >= 10)) {
1417                                         if (remain_capacity >= di->temp_nac*120/100)
1418                                                 remain_capacity = di->temp_nac*110/100;
1419                                         else if (remain_capacity < di->temp_nac*8/10)
1420                                                 remain_capacity = di->temp_nac*9/10;
1421
1422                                         DBG("<%s> shutdown_time > 3 minute,  remain_cap = %d\n", __func__, remain_capacity);
1423                                 }
1424                         }
1425                 }
1426
1427                 di->real_soc = init_soc;
1428                 di->nac = remain_capacity;
1429                 if (di->nac <= 0)
1430                         di->nac = 0;
1431                 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1432         }
1433         return 0;
1434 }
1435
1436
1437 static u8 get_charge_status(struct battery_info *di)
1438 {
1439         u8 status;
1440         u8 ret = 0;
1441
1442         battery_read(di->rk818, SUP_STS_REG, &status, 1);
1443         status &= (0x70);
1444         switch (status) {
1445         case CHARGE_OFF:
1446                 ret = CHARGE_OFF;
1447                 DBG("  CHARGE-OFF ...\n");
1448                 break;
1449
1450         case DEAD_CHARGE:
1451                 ret = DEAD_CHARGE;
1452                 DBG("  DEAD CHARGE ...\n");
1453                 break;
1454
1455         case  TRICKLE_CHARGE:                           /* (0x02 << 4) */
1456                 ret = DEAD_CHARGE;
1457                 DBG("  TRICKLE CHARGE ...\n ");
1458                 break;
1459
1460         case  CC_OR_CV:                                 /* (0x03 << 4) */
1461                 ret = CC_OR_CV;
1462                 DBG("  CC or CV ...\n");
1463                 break;
1464
1465         case  CHARGE_FINISH:                            /* (0x04 << 4) */
1466                 ret = CHARGE_FINISH;
1467                 DBG("  CHARGE FINISH ...\n");
1468                 break;
1469
1470         case  USB_OVER_VOL:                                     /* (0x05 << 4) */
1471                 ret = USB_OVER_VOL;
1472                 DBG("  USB OVER VOL ...\n");
1473                 break;
1474
1475         case  BAT_TMP_ERR:                                      /* (0x06 << 4) */
1476                 ret = BAT_TMP_ERR;
1477                 DBG("  BAT TMP ERROR ...\n");
1478                 break;
1479
1480         case  TIMER_ERR:                                        /* (0x07 << 4) */
1481                 ret = TIMER_ERR;
1482                 DBG("  TIMER ERROR ...\n");
1483                 break;
1484
1485         case  USB_EXIST:                                        /* (1 << 1)// usb is exists */
1486                 ret = USB_EXIST;
1487                 DBG("  USB EXIST ...\n");
1488                 break;
1489
1490         case  USB_EFF:                                          /* (1 << 0)// usb is effective */
1491                 ret = USB_EFF;
1492                 DBG("  USB EFF...\n");
1493                 break;
1494
1495         default:
1496                 return -EINVAL;
1497         }
1498
1499         return ret;
1500
1501 }
1502 static void set_charge_current(struct battery_info *di, int charge_current)
1503 {
1504         u8 usb_ctrl_reg;
1505
1506         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1507         usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1508         usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1509         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1510 }
1511
1512 static void rk_battery_charger_init(struct  battery_info *di)
1513 {
1514         u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1515         u8 sup_sts_reg;
1516
1517         DBG("%s  start\n", __func__);
1518         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1519         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1520         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1521         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1522         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1523
1524         DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1525         usb_ctrl_reg &= (~0x0f);
1526 #ifdef SUPPORT_USB_CHARGE
1527         usb_ctrl_reg |= (ILIM_450MA | CHRG_CT_EN);
1528 #else
1529         usb_ctrl_reg |= (ILIM_3000MA | CHRG_CT_EN);
1530 #endif
1531         chrg_ctrl_reg1 &= (0x00);
1532         chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
1533
1534         chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1535         chrg_ctrl_reg2 &= ~(0xc0);
1536         chrg_ctrl_reg2 |= FINISH_100MA;
1537
1538         sup_sts_reg &= ~(0x01 << 3);
1539         sup_sts_reg |= (0x01 << 2);
1540
1541         battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1542         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1543         battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1544         battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1545         battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1546
1547         debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1548         debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1549         debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1550         debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1551
1552         DBG("%s  end\n", __func__);
1553 }
1554
1555 void charge_disable_open_otg(int value)
1556 {
1557         struct  battery_info *di = g_battery;
1558
1559         if (value == 1) {
1560                 DBG("charge disable, enable OTG.\n");
1561                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1562                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /*  enable OTG */
1563         }
1564         if (value == 0) {
1565                 DBG("charge enable, disable OTG.\n");
1566                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1567                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1568         }
1569 }
1570
1571 static void low_waring_init(struct battery_info *di)
1572 {
1573         u8 vb_mon_reg;
1574         u8 vb_mon_reg_init;
1575
1576         battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1577
1578         /* 2.8v~3.5v, interrupt */
1579         vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06);  /* 3400mV*/
1580         battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1581 }
1582
1583 static void  fg_init(struct battery_info *di)
1584 {
1585         u8 adc_ctrl_val;
1586
1587         adc_ctrl_val = 0x30;
1588         battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1589
1590         _gauge_enable(di);
1591         /* get the volatege offset */
1592         _get_voltage_offset_value(di);
1593         rk_battery_charger_init(di);
1594         _set_relax_thres(di);
1595         /* get the current offset , the value write to the CAL_OFFSET */
1596         di->current_offset = _get_ioffset(di);
1597         _set_cal_offset(di, di->current_offset+42);
1598         _rsoc_init(di);
1599         _capacity_init(di, di->nac);
1600
1601         di->remain_capacity = _get_realtime_capacity(di);
1602         di->current_avg = _get_average_current(di);
1603
1604         low_waring_init(di);
1605         restart_relax(di);
1606         power_on_save(di, di->voltage_ocv);
1607         /* set sample time for cal_offset interval*/
1608         ioffset_sample_time(di, SAMP_TIME_8MIN);
1609         dump_gauge_register(di);
1610         dump_charger_register(di);
1611
1612         DBG("<%s> :\n"
1613             "nac = %d , remain_capacity = %d\n"
1614             "OCV_voltage = %d, voltage = %d\n"
1615             "SOC = %d, fcc = %d\n",
1616             __func__,
1617             di->nac, di->remain_capacity,
1618             di->voltage_ocv, di->voltage,
1619             di->real_soc, di->fcc);
1620 }
1621
1622
1623 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1624 static void  zero_get_soc(struct   battery_info *di)
1625 {
1626         int ocv_voltage, check_voltage;
1627         int temp_soc = -1, real_soc;
1628         int currentold, currentnow, voltage;
1629         int i;
1630         int voltage_k;
1631         int count_num = 0;
1632
1633         DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1634         /* if (di->voltage <  3600)//di->warnning_voltage) */
1635         {
1636                 /* DBG("+++++++zero mode++++++++displaysoc+++++++++\n"); */
1637                 do {
1638                         currentold = _get_average_current(di);
1639                         _get_cal_offset(di);
1640                         _get_ioffset(di);
1641                         msleep(100);
1642                         currentnow = _get_average_current(di);
1643                         count_num++;
1644                 } while ((currentold == currentnow) && (count_num < 11));
1645
1646                 voltage  = 0;
1647                 for (i = 0; i < 10 ; i++)
1648                         voltage += rk_battery_voltage(di);
1649                 voltage /= 10;
1650
1651                 if (di->voltage_old == 0)
1652                         di->voltage_old = voltage;
1653                 voltage_k = voltage;
1654                 voltage = (di->voltage_old*2 + 8*voltage)/10;
1655                 di->voltage_old = voltage;
1656                 /* DBG("Zero: voltage = %d\n", voltage); */
1657
1658                 currentnow = _get_average_current(di);
1659                 /* DBG(" zero: current = %d, voltage = %d\n", currentnow, voltage); */
1660
1661                 ocv_voltage = 3400 + abs32_int(currentnow)*200/1000;
1662                 check_voltage = voltage + abs32_int(currentnow)*(200 - 65)/1000;   /*  65 mo  power-path mos */
1663                 _voltage_to_capacity(di, check_voltage);
1664                 /* if ((di->remain_capacity > di->nac) && (update_q == 0)) */
1665                 /* DBG(" xxx  Zerro: tui suan OCV cap :%d\n", di->temp_nac); */
1666                 di->update_q = di->remain_capacity - di->temp_nac;
1667                 /* update_q = di->temp_nac; */
1668
1669                 /* DBG("Zero: update_q = %d , remain_capacity = %d, temp_nac = %d\n ", di->update_q, di->remain_capacity, di->temp_nac); */
1670                 /* relax_volt_update_remain_capacity(di, 3600 + abs32_int(di->current_avg)*200/1000); */
1671
1672                 _voltage_to_capacity(di, ocv_voltage);
1673                 /*di->temp_nac;
1674                 temp_soc = _get_soc(di); */
1675                 if (di->display_soc == 0)
1676                         di->display_soc = di->real_soc*1000;
1677
1678                 real_soc = di->display_soc;
1679                 /* DBG(" Zerro: Q (err)   cap :%d\n", di->temp_nac);
1680                 DBG(" ZERO : real-soc = %d\n ", di->real_soc); */
1681                 DBG("ZERO : ocv_voltage = %d, check_voltage = %d\n ", ocv_voltage, check_voltage);
1682                 if (di->remain_capacity > di->temp_nac + di->update_q) {
1683
1684                         if (di->update_k == 0 || di->update_k >= 10) {
1685                                 /* DBG("one..\n"); */
1686                                 if (di->update_k == 0) {
1687                                         di->line_q = di->temp_nac + di->update_q;  /* ZQ = Q_ded +  Qerr */
1688                                         /* line_q = update_q - di->temp_nac; */
1689                                         temp_soc = (di->remain_capacity - di->line_q)*1000/di->fcc;/* (RM - ZQ) / FCC  = r0 = R0 ; */
1690                                         /* temp_soc = (line_q)*1000/di->fcc;//(RM - ZQ) / FCC  = r0 = R0 ;*
1691                                         /di->line_k = (real_soc*1000 + temp_soc/2)/temp_soc;//k0 = y0/x0 */
1692                                         di->line_k = (real_soc + temp_soc/2)/temp_soc;/* k0 = y0/x0 */
1693                                         /* DBG("Zero: one  link = %d realsoc = %d , temp_soc = %d\n", di->line_k, di->real_soc, temp_soc); */
1694
1695
1696                                 } else {
1697                                         /*
1698                                         if (line_q == 0)
1699                                         line_q = di->temp_nac + update_q;
1700                                         */
1701                                         /* DBG("two...\n"); */
1702                                         temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc; /* x1  10 */
1703                                         /*
1704                                         temp_soc = (line_q)*1000/di->fcc;// x1
1705                                         real_soc = (di->line_k*temp_soc+500)/1000;  //y1 = k0*x1
1706                                         */
1707                                         real_soc = (di->line_k*temp_soc); /*  y1 = k0*x1 */
1708                                         /* DBG("Zero: two  link = %d realsoc = %d , temp_soc = %d\n", di->line_k, real_soc, temp_soc); */
1709                                         di->display_soc = real_soc;
1710                                         /* if (real_soc != di->real_soc) */
1711                                         if ((real_soc+500)/1000 < di->real_soc)
1712                                                 di->real_soc--;
1713                                         /*
1714                                         DBG("Zero two di->real_soc = %d\n", di->real_soc);
1715                                         DBG("Zero : temp_soc : %d\n", real_soc);
1716                                         */
1717                                         _voltage_to_capacity(di, ocv_voltage);
1718                                         di->line_q = di->temp_nac + di->update_q; /* Q1 */
1719                                         /* line_q = update_q - di->temp_nac; */
1720                                         temp_soc = ((di->remain_capacity - di->line_q)*1000 +  di->fcc/2)/di->fcc; /* z1 */
1721                                         /*
1722                                         temp_soc = (line_q)*1000/di->fcc;
1723                                         di->line_k = (di->real_soc*1000 +  temp_soc/2)/temp_soc; //k1 = y1/z1
1724                                         */
1725                                         di->line_k = (di->display_soc +  temp_soc/2)/temp_soc; /* k1 = y1/z1 */
1726                                         /* DBG("Zero: two  link = %d display_soc = %d , temp_soc = %d\n", di->line_k, di->display_soc, temp_soc); */
1727                                         /* line_q = di->temp_nac + update_q;// Q1 */
1728
1729
1730                                 }
1731                                 di->update_k = 0;
1732
1733                         }
1734
1735                         /* DBG("di->remain_capacity = %d, line_q = %d\n ", di->remain_capacity, di->line_q); */
1736
1737                         di->update_k++;
1738                         if (di->update_k == 1 || di->update_k != 10) {
1739                                 temp_soc = ((di->remain_capacity - di->line_q)*1000 + di->fcc/2)/di->fcc;/* x */
1740                                 di->display_soc = di->line_k*temp_soc;
1741                                 /* if (((di->line_k*temp_soc+500)/1000) != di->real_soc), */
1742                                 DBG("ZERO : display-soc = %d, real-soc = %d\n", di->display_soc, di->real_soc);
1743                                 if ((di->display_soc+500)/1000 < di->real_soc)
1744                                         di->real_soc--;
1745                                 /* di->real_soc = (line_k*temp_soc+500)/1000 ;//y = k0*x */
1746                         }
1747                 } else {
1748                         /* DBG("three..\n"); */
1749                         di->update_k++;
1750                         if (di->update_k > 10) {
1751                                 di->update_k = 0;
1752                                 di->real_soc--;
1753                         }
1754                 }
1755
1756                 DBG("ZERO : update_k = %d\n", di->update_k);
1757                 DBG("ZERO : remain_capacity = %d , nac = %d, update_q = %d\n", di->remain_capacity, di->line_q, di->update_q);
1758                 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);
1759         }
1760 }
1761
1762
1763 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
1764 {
1765         int voltage;
1766         int now_current, soc_time = -1;
1767         int volt_to_soc;
1768
1769         voltage = di->voltage;
1770         now_current = di->current_avg;
1771         if (now_current == 0)
1772                 now_current = 1;
1773         soc_time = di->fcc*3600/100/(abs_int(now_current));
1774         _voltage_to_capacity(di, 3800);
1775         volt_to_soc = di->temp_soc;
1776         di->temp_soc = _get_soc(di);
1777
1778         DBG("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
1779         DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1780         if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) {  /* di->warnning_voltage) */
1781                 zero_get_soc(di);
1782                 return;
1783
1784         } else if (di->temp_soc == di->real_soc) {
1785                 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1786         } else if (di->temp_soc > di->real_soc) {
1787                 DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1788                 di->vol_smooth_time++;
1789                 if (di->vol_smooth_time > soc_time*3) {
1790                         di->real_soc--;
1791                         di->vol_smooth_time = 0;
1792                 }
1793
1794         } else {
1795                 DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1796                 if (di->real_soc == (di->temp_soc + 1)) {
1797                         di->change_timer = di->soc_timer;
1798                         di->real_soc = di->temp_soc;
1799                 } else {
1800                         di->vol_smooth_time++;
1801                         if (di->vol_smooth_time > soc_time/3) {
1802                                 di->real_soc--;
1803                                 di->vol_smooth_time  = 0;
1804                         }
1805                 }
1806         }
1807
1808         DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1809         DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
1810 }
1811
1812 static int get_charging_time(struct battery_info *di)
1813 {
1814         return (di->charging_time/60);
1815 }
1816
1817 static int get_discharging_time(struct battery_info *di)
1818 {
1819         return (di->discharging_time/60);
1820 }
1821 static void dump_debug_info(struct battery_info *di)
1822 {
1823         u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
1824         u8 usb_ctrl_reg, chrg_ctrl_reg1;
1825         u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
1826
1827         battery_read(di->rk818, GGCON, &ggcon_reg, 1);
1828         battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
1829         battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
1830         battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
1831         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1832         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1833         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1834         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1835         battery_read(di->rk818, 0x00, &rtc_val, 1);
1836
1837         DBG("\n------------- dump_debug_regs -----------------\n"
1838             "GGCON = 0x%2x, GGSTS = 0x%2x, RTC  = 0x%2x\n"
1839             "SUP_STS_REG  = 0x%2x, VB_MOD_REG   = 0x%2x\n"
1840             "USB_CTRL_REG  = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
1841             "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
1842             ggcon_reg, ggsts_reg, rtc_val,
1843             sup_tst_reg, vb_mod_reg,
1844             usb_ctrl_reg, chrg_ctrl_reg1,
1845             chrg_ctrl_reg2, chrg_ctrl_reg3
1846            );
1847
1848         DBG(
1849             "########################## [read] ################################\n"
1850             "info: 3.4v low warning, digital 100mA finish,  4.2v, 1.6A\n"
1851             "-----------------------------------------------------------------\n"
1852             "realx-voltage = %d, voltage = %d, current-avg = %d\n"
1853             "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
1854             "diplay_soc = %d, cpapacity_soc = %d\n"
1855             "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
1856             "finish_real_soc = %d, finish_temp_soc = %d\n"
1857             "chrg_time = %d, dischrg_time = %d\n",
1858             get_relax_voltage(di),
1859             di->voltage, di->current_avg,
1860             di->fcc, di->remain_capacity, _get_OCV_voltage(di),
1861             di->real_soc, _get_soc(di),
1862             di->ac_online, di->usb_online, di->status,
1863             di->debug_finish_real_soc, di->debug_finish_temp_soc,
1864             get_charging_time(di), get_discharging_time(di)
1865            );
1866         get_charge_status(di);
1867         DBG("################################################################\n");
1868
1869 }
1870
1871 static void update_fcc_capacity(struct battery_info *di)
1872 {
1873         if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
1874                 if (get_level(di) >= di->dod0_level) {
1875                         di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
1876                         if (di->fcc > di->qmax)
1877                                 di->fcc = di->qmax;
1878
1879                         _capacity_init(di, di->fcc);
1880                         _save_FCC_capacity(di, di->fcc);
1881                 }
1882                 di->dod0_status = 0;
1883         }
1884 }
1885
1886 static void debug_get_finish_soc(struct battery_info *di)
1887 {
1888         if (di->charge_status == CHARGE_FINISH) {
1889                 di->debug_finish_real_soc = di->real_soc;
1890                 di->debug_finish_temp_soc = di->temp_soc;
1891         }
1892 }
1893
1894 static void wait_charge_finish_signal(struct battery_info *di)
1895 {
1896         if (di->charge_status == CHARGE_FINISH)
1897                 update_fcc_capacity(di);/* save new fcc*/
1898
1899         /* debug msg*/
1900         debug_get_finish_soc(di);
1901 }
1902
1903 static void charge_finish_routine(struct battery_info *di)
1904 {
1905         if (di->charge_status == CHARGE_FINISH) {
1906                 _capacity_init(di, di->fcc);
1907                 zero_current_calibration(di);
1908
1909                 if (di->real_soc < 100) {
1910                         DBG("<%s>,CHARGE_FINISH  di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
1911                         if ((di->soc_counter < 80)) {
1912                                 di->soc_counter++;
1913                         } else {
1914                                 di->soc_counter = 0;
1915                                 di->real_soc++;
1916                         }
1917                 }
1918         }
1919 }
1920
1921 static void voltage_to_soc_charge_smooth(struct battery_info *di)
1922 {
1923         int now_current, soc_time;
1924
1925         now_current = _get_average_current(di);
1926         if (now_current == 0)
1927                 now_current = 1;
1928         soc_time = di->fcc*3600/100/(abs_int(now_current));   /* 1%  time; */
1929         di->temp_soc = _get_soc(di);
1930
1931         DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1932         /*
1933         if ((di->temp_soc >= 85)&&(di->real_soc >= 85)){
1934                 di->charge_smooth_time++;
1935
1936                 if  (di->charge_smooth_time > soc_time/3) {
1937                         di->real_soc++;
1938                         di->charge_smooth_time  = 0;
1939                 }
1940                 di->charge_smooth_status = true;
1941         }*/
1942
1943         if (di->real_soc == di->temp_soc) {
1944                 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
1945                 di->temp_soc = _get_soc(di);
1946         }
1947         if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
1948
1949                 if (di->temp_soc < di->real_soc + 1) {
1950                         DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
1951                         di->charge_smooth_time++;
1952                         if  (di->charge_smooth_time > soc_time*2) {
1953                                 di->real_soc++;
1954                                 di->charge_smooth_time  = 0;
1955                         }
1956                         di->charge_smooth_status = true;
1957                 }
1958
1959                 else if (di->temp_soc > di->real_soc + 1) {
1960                         DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
1961                         di->charge_smooth_time++;
1962                         if  (di->charge_smooth_time > soc_time/3) {
1963                                 di->real_soc++;
1964                                 di->charge_smooth_time  = 0;
1965                         }
1966                         di->charge_smooth_status = true;
1967
1968                 } else if (di->temp_soc == di->real_soc + 1) {
1969                         DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
1970                         if (di->charge_smooth_status) {
1971                                 di->charge_smooth_time++;
1972                                 if (di->charge_smooth_time > soc_time/3) {
1973                                         di->real_soc = di->temp_soc;
1974                                         di->charge_smooth_time  = 0;
1975                                         di->charge_smooth_status = false;
1976                                 }
1977
1978                         } else {
1979                                 di->real_soc = di->temp_soc;
1980                                 di->charge_smooth_status = false;
1981
1982                         }
1983                 }
1984         }
1985
1986         DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
1987         DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
1988 }
1989
1990 static void rk_battery_display_smooth(struct battery_info *di)
1991 {
1992         int status;
1993         u8  charge_status;
1994
1995         status = di->status;
1996         charge_status = di->charge_status;
1997         if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
1998
1999                 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2000                         voltage_to_soc_discharge_smooth(di);
2001                 else
2002                         voltage_to_soc_charge_smooth(di);
2003
2004         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2005                 voltage_to_soc_discharge_smooth(di);
2006                 if (di->real_soc == 1) {
2007                         di->time2empty++;
2008                         if (di->time2empty >= 300)
2009                                 di->real_soc = 0;
2010                 } else {
2011                         di->time2empty = 0;
2012                 }
2013         }
2014
2015 }
2016
2017 #if 0
2018 static void software_recharge(struct battery_info *di, int max_cnt)
2019 {
2020         static int recharge_cnt;
2021         u8 chrg_ctrl_reg1;
2022
2023         if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2024                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2025                 chrg_ctrl_reg1 &= ~(1 << 7);
2026                 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2027                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2028                 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2029                 msleep(400);
2030                 chrg_ctrl_reg1 |= (1 << 7);
2031                 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2032                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2033                 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2034
2035                 recharge_cnt++;
2036         }
2037 }
2038 #endif
2039
2040 #if 0
2041 static int estimate_battery_resister(struct battery_info *di)
2042 {
2043         int i;
2044         int avr_voltage1 = 0, avr_current1;
2045         int avr_voltage2 = 0, avr_current2;
2046         u8 usb_ctrl_reg;
2047         int bat_res, ocv_votage;
2048         static unsigned long last_time;
2049         unsigned long delta_time;
2050         int charge_ocv_voltage1, charge_ocv_voltage2;
2051         int charge_ocv_soc1, charge_ocv_soc2;
2052
2053         delta_time = get_seconds() - last_time;
2054         DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2055         if (delta_time >= 20) {/*20s*/
2056
2057                 /*first sample*/
2058                 set_charge_current(di, ILIM_450MA);/*450mA*/
2059                 msleep(1000);
2060                 for (i = 0; i < 10 ; i++) {
2061                         msleep(100);
2062                         avr_voltage1 += rk_battery_voltage(di);
2063                 }
2064                 avr_voltage1 /= 10;
2065                 avr_current1 = _get_average_current(di);
2066                 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2067                 DBG("------------------------------------------------------------------------------------------\n");
2068                 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2069
2070                 /*second sample*/
2071                 set_charge_current(di, ILIM_3000MA);
2072                 msleep(1000);
2073                 for (i = 0; i < 10 ; i++) {
2074                         msleep(100);
2075                         avr_voltage2 += rk_battery_voltage(di);
2076                 }
2077                 avr_voltage2 /= 10;
2078                 avr_current2 = _get_average_current(di);
2079                 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2080                 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2081
2082                 /*calc resister and ocv_votage ocv*/
2083                 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2084                 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2085                 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2086
2087                 /*calc sample voltage ocv*/
2088                 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2089                 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2090                 _voltage_to_capacity(di, charge_ocv_voltage1);
2091                 charge_ocv_soc1 = di->temp_soc;
2092                 _voltage_to_capacity(di, charge_ocv_voltage2);
2093                 charge_ocv_soc2 = di->temp_soc;
2094
2095                 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2096                 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2097                 DBG("------------------------------------------------------------------------------------------\n");
2098                 last_time = get_seconds();
2099
2100                 return bat_res;
2101         }
2102
2103         return 0;
2104 }
2105 #endif
2106
2107 #if 0
2108 static int update_battery_resister(struct battery_info *di)
2109 {
2110         int tmp_res;
2111
2112         if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2113
2114                 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2115                         tmp_res = estimate_battery_resister(di);
2116                         if (tmp_res != 0)
2117                                 di->bat_res_update_cnt++;
2118                         di->bat_res += tmp_res;
2119                         DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2120                         if (di->bat_res_update_cnt == 10) {
2121                                 di->bat_res_updated = true;
2122                                 di->bat_res /= 10;
2123                         }
2124                         DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2125                 }
2126         }
2127
2128         return tmp_res;
2129 }
2130 #endif
2131
2132 #if 0
2133 static void charge_soc_check_routine(struct battery_info *di)
2134 {
2135         int min;
2136         int ocv_voltage;
2137         int old_temp_soc;
2138         int ocv_temp_soc;
2139         int remain_capcity;
2140
2141         if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2142                 min = get_charging_time(di);
2143                 update_battery_resister(di);
2144         if (0)
2145                 if ((min >= 30) && (di->bat_res_updated)) {
2146
2147                         old_temp_soc = di->temp_soc;
2148                         ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2149                         _voltage_to_capacity(di, ocv_voltage);
2150                         ocv_temp_soc = di->temp_soc;
2151
2152                         DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2153                         DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2154                         DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2155
2156                         if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2157                                 di->temp_soc = ocv_temp_soc;
2158                         else
2159                                 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2160
2161                         remain_capcity = di->temp_soc * di->fcc / 100;
2162                         _capacity_init(di, remain_capcity);
2163                         di->remain_capacity = _get_realtime_capacity(di);
2164                         DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2165                 }
2166         }
2167
2168 }
2169 #endif
2170
2171 #if 1
2172 static void update_resume_status_relax_voltage(struct battery_info *di)
2173 {
2174         unsigned long sleep_soc;
2175         unsigned long sum_sleep_soc;
2176         unsigned long sleep_sec;
2177         int relax_voltage;
2178         u8 charge_status;
2179         int delta_capacity;
2180         int delta_soc;
2181         int sum_sleep_avr_current;
2182         int sleep_min;
2183
2184         if (di->resume) {
2185                 update_battery_info(di);
2186                 di->resume = false;
2187                 di->sys_wakeup = true;
2188
2189                 DBG("<%s>, resume----------checkstart\n", __func__);
2190                 sleep_sec = get_seconds() - di->suspend_time_start;
2191                 sleep_min = sleep_sec  / 60;
2192
2193                 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2194                         __func__, sleep_sec, sleep_min);
2195
2196                 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2197                         DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2198
2199                         delta_capacity =  di->suspend_capacity - di->remain_capacity;
2200                         delta_soc = di->suspend_temp_soc - _get_soc(di);
2201                         di->dischrg_sum_sleep_capacity += delta_capacity;
2202                         di->dischrg_sum_sleep_sec += sleep_sec;
2203
2204                         sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2205                         sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2206
2207                         DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2208                                 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2209                         DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2210                                 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2211                         DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2212                                 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2213                         DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2214
2215                         /*large suspend current*/
2216                         if (sum_sleep_avr_current > 20) {
2217                                 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2218                                 di->real_soc -= sum_sleep_soc;
2219                                 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2220                                         __func__, sum_sleep_soc, di->real_soc);
2221
2222                         /* small suspend current*/
2223                         } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2224
2225                                 relax_voltage = get_relax_voltage(di);
2226                                 di->voltage  = rk_battery_voltage(di);
2227
2228                                 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2229                                         DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2230                                         relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2231
2232                                 } else {
2233                                         DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2234                                         if (sum_sleep_soc > 0)
2235                                                 di->real_soc -= sum_sleep_soc;
2236                                 }
2237                         }
2238
2239                         if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2240                                 di->dischrg_sum_sleep_capacity = 0;
2241                                 di->dischrg_sum_sleep_sec = 0;
2242                         }
2243                         DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2244                         DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2245                 }
2246
2247                 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2248                         DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2249                         if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2250                                 di->temp_soc = _get_soc(di);
2251                                 charge_status = get_charge_status(di);
2252
2253                                 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2254                                 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2255                                         DBG("resume, sleep : ac online charge current < 1000\n");
2256                                         if (sleep_sec > 0) {
2257                                                 di->count_sleep_time += sleep_sec;
2258                                                 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2259                                                 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2260                                                 if (sleep_soc > 0)
2261                                                         di->count_sleep_time = 0;
2262                                                 di->real_soc += sleep_soc;
2263                                                 if (di->real_soc > 100)
2264                                                         di->real_soc = 100;
2265                                         }
2266                                 } else {
2267
2268                                         DBG("<%s>, usb charging\n", __func__);
2269                                         if (di->suspend_temp_soc + 15 < di->temp_soc)
2270                                                 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2271                                         else
2272                                                 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2273                                 }
2274
2275                                 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);
2276                         }
2277                 }
2278         }
2279 }
2280 #endif
2281
2282 #ifdef SUPPORT_USB_CHARGE
2283 static int  get_charging_status_type(struct battery_info *di)
2284 {
2285         int otg_status = dwc_otg_check_dpdm();
2286
2287         if (0 == otg_status) {
2288                 di->usb_online = 0;
2289                 di->ac_online = 0;
2290                 di->check_count = 0;
2291
2292         } else if (1 == otg_status) {
2293                 if (0 == get_gadget_connect_flag()) {
2294                         if (++di->check_count >= 5) {
2295                                 di->ac_online = 1;
2296                                 di->usb_online = 0;
2297                         } else {
2298                                 di->ac_online = 0;
2299                                 di->usb_online = 1;
2300                         }
2301                 } else {
2302                         di->ac_online = 0;
2303                         di->usb_online = 1;
2304                 }
2305
2306         } else if (2 == otg_status) {
2307                 di->ac_online = 1;
2308                 di->usb_online = 0;
2309                 di->check_count = 0;
2310         }
2311
2312         if (di->ac_online == 1)
2313                 set_charge_current(di, ILIM_3000MA);
2314         else
2315                 set_charge_current(di, ILIM_450MA);
2316         return otg_status;
2317 }
2318
2319 #endif
2320
2321 static void battery_poweron_status_init(struct battery_info *di)
2322 {
2323         int otg_status;
2324
2325 #ifndef SUPPORT_USB_CHARGE
2326         u8 buf;
2327 #endif
2328
2329 #ifdef SUPPORT_USB_CHARGE
2330
2331         otg_status = dwc_otg_check_dpdm();
2332         if (otg_status == 1) {
2333                 di->usb_online = 1;
2334                 di->ac_online = 0;
2335                 set_charge_current(di, ILIM_450MA);
2336                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2337                 DBG("++++++++ILIM_450MA++++++\n");
2338
2339         } else if (otg_status == 2) {
2340                 di->usb_online = 0;
2341                 di->ac_online = 1;
2342                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2343                 set_charge_current(di, ILIM_3000MA);
2344                 DBG("++++++++ILIM_1000MA++++++\n");
2345         }
2346         DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2347
2348 #else
2349
2350         battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2351         if (buf&PLUG_IN_STS) {
2352                 di->ac_online = 1;
2353                 di->usb_online = 0;
2354                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2355                 if (di->real_soc == 100)
2356                         di->status = POWER_SUPPLY_STATUS_FULL;
2357         } else {
2358                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2359                 di->ac_online = 0;
2360                 di->usb_online = 0;
2361         }
2362         DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2363 #endif
2364 }
2365
2366
2367 static void check_battery_status(struct battery_info *di)
2368 {
2369         u8 buf;
2370         int ret;
2371
2372         ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2373 #ifdef SUPPORT_USB_CHARGE
2374
2375         if (strstr(saved_command_line, "charger")) {
2376                 if ((buf&PLUG_IN_STS) == 0) {
2377                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2378                         di->ac_online = 0;
2379                         di->usb_online = 0;
2380                 }
2381
2382         } else {
2383                 if (buf&PLUG_IN_STS) {
2384                         get_charging_status_type(di);
2385
2386                         di->status = POWER_SUPPLY_STATUS_CHARGING;
2387                         if (di->real_soc == 100)
2388                                 di->status = POWER_SUPPLY_STATUS_FULL;
2389                 } else {
2390                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2391                         di->ac_online = 0;
2392                         di->usb_online = 0;
2393                 }
2394         }
2395 #else
2396
2397         if (buf & PLUG_IN_STS) {
2398                 di->ac_online = 1;
2399                 di->usb_online = 0;
2400                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2401                 if (di->real_soc == 100)
2402                         di->status = POWER_SUPPLY_STATUS_FULL;
2403         } else {
2404                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2405                 di->ac_online = 0;
2406                 di->usb_online = 0;
2407         }
2408 #endif
2409 }
2410
2411 static void report_power_supply_changed(struct battery_info *di)
2412 {
2413         static u32 old_soc;
2414         static u32 old_ac_status;
2415         static u32 old_usb_status;
2416         static u32 old_charge_status;
2417         bool state_changed;
2418
2419         state_changed = false;
2420         if (di->real_soc == 0)
2421                 state_changed = true;
2422         else if (di->real_soc == 100)
2423                 state_changed = true;
2424         else if (di->real_soc != old_soc)
2425                 state_changed = true;
2426         else if (di->ac_online != old_ac_status)
2427                 state_changed = true;
2428         else if (di->usb_online != old_usb_status)
2429                 state_changed = true;
2430         else if (old_charge_status != di->status)
2431                 state_changed = true;
2432
2433         if (state_changed) {
2434                 power_supply_changed(&di->bat);
2435                 power_supply_changed(&di->usb);
2436                 power_supply_changed(&di->ac);
2437                 old_soc = di->real_soc;
2438                 old_ac_status = di->ac_online;
2439                 old_usb_status = di->usb_online;
2440                 old_charge_status = di->status;
2441         }
2442 }
2443
2444 static void update_battery_info(struct battery_info *di)
2445 {
2446         di->remain_capacity = _get_realtime_capacity(di);
2447         if (di->remain_capacity > di->fcc)
2448                 _capacity_init(di, di->fcc);
2449         else if (di->remain_capacity < 0)
2450                 _capacity_init(di, 0);
2451
2452         if (di->real_soc > 100)
2453                 di->real_soc = 100;
2454         else if (di->real_soc < 0)
2455                 di->real_soc = 0;
2456
2457         if ((di->ac_online) || (di->usb_online)) {/*charging*/
2458                 di->charging_time++;
2459                 di->discharging_time = 0;
2460         } else {
2461                 di->discharging_time++;
2462                 di->charging_time = 0;
2463         }
2464
2465         di->work_on = 1;
2466         di->voltage  = rk_battery_voltage(di);
2467         di->current_avg = _get_average_current(di);
2468         di->remain_capacity = _get_realtime_capacity(di);
2469         di->voltage_ocv = _get_OCV_voltage(di);
2470         di->charge_status = get_charge_status(di);
2471         di->otg_status = dwc_otg_check_dpdm();
2472         di->relax_voltage = get_relax_voltage(di);
2473         di->temp_soc = _get_soc(di);
2474         di->remain_capacity = _get_realtime_capacity(di);
2475         check_battery_status(di);/* ac_online, usb_online, status*/
2476         update_cal_offset(di);
2477 }
2478
2479 static void rk_battery_work(struct work_struct *work)
2480 {
2481         struct battery_info *di = container_of(work,
2482                         struct battery_info, battery_monitor_work.work);
2483
2484         update_resume_status_relax_voltage(di);
2485         wait_charge_finish_signal(di);
2486         charge_finish_routine(di);
2487
2488         rk_battery_display_smooth(di);
2489         update_battery_info(di);
2490
2491         report_power_supply_changed(di);
2492         _copy_soc(di, di->real_soc);
2493         _save_remain_capacity(di, di->remain_capacity);
2494
2495         dump_debug_info(di);
2496         di->queue_work_cnt++;
2497         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2498 }
2499
2500 static void rk_battery_charge_check_work(struct work_struct *work)
2501 {
2502         struct battery_info *di = container_of(work,
2503                         struct battery_info, charge_check_work.work);
2504
2505         DBG("rk_battery_charge_check_work\n");
2506         charge_disable_open_otg(di->charge_otg);
2507 }
2508
2509 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2510
2511 int register_battery_notifier(struct notifier_block *nb)
2512 {
2513         return blocking_notifier_chain_register(&battery_chain_head, nb);
2514 }
2515 EXPORT_SYMBOL_GPL(register_battery_notifier);
2516
2517 int unregister_battery_notifier(struct notifier_block *nb)
2518 {
2519         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2520 }
2521 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2522
2523 int battery_notifier_call_chain(unsigned long val)
2524 {
2525         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2526                 == NOTIFY_BAD) ? -EINVAL : 0;
2527 }
2528 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2529
2530 static void poweron_lowerpoer_handle(struct battery_info *di)
2531 {
2532 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2533         if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2534                 mdelay(1500);
2535                 /* kernel_power_off(); */
2536         }
2537 #endif
2538 }
2539
2540 static int battery_notifier_call(struct notifier_block *nb,
2541                                                                 unsigned long event, void *data)
2542 {
2543         struct battery_info *di =
2544             container_of(nb, struct battery_info, battery_nb);
2545
2546         switch (event) {
2547         case 0:
2548                 DBG(" CHARGE enable\n");
2549                 di->charge_otg = 0;
2550                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2551                 break;
2552
2553         case 1:
2554                 di->charge_otg  = 1;
2555                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2556                 DBG("charge disable OTG enable\n");
2557                 break;
2558
2559         case 2:
2560                 poweron_lowerpoer_handle(di);
2561                 break;
2562
2563         default:
2564                 return NOTIFY_OK;
2565         }
2566         return NOTIFY_OK;
2567 }
2568
2569 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
2570 {
2571         pr_info("<%s>lower power warning!\n", __func__);
2572
2573         _copy_soc(g_battery, 0);
2574         _capacity_init(g_battery, 0);
2575         rk_send_wakeup_key();
2576         kernel_power_off();
2577         return IRQ_HANDLED;
2578 }
2579
2580 static void disable_vbat_low_irq(struct battery_info *di)
2581 {
2582         /* mask vbat low */
2583         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
2584         /*clr vbat low interrupt */
2585         /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
2586 }
2587 static void enable_vbat_low_irq(struct battery_info *di)
2588 {
2589         /* clr vbat low interrupt */
2590         rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
2591         /* mask vbat low */
2592         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
2593 }
2594
2595 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
2596 {
2597         pr_info("\n------- %s:irq = %d\n", __func__, irq);
2598         rk_send_wakeup_key();
2599         return IRQ_HANDLED;
2600 }
2601 static irqreturn_t rk818_vbat_plug_out(int irq, void  *di)
2602 {
2603         pr_info("\n-------- %s:irq = %d\n", __func__, irq);
2604         charge_disable_open_otg(0);
2605         rk_send_wakeup_key();
2606         return IRQ_HANDLED;
2607 }
2608
2609 static irqreturn_t rk818_vbat_charge_ok(int irq, void  *di)
2610 {
2611         pr_info("---------- %s:irq = %d\n", __func__, irq);
2612         rk_send_wakeup_key();
2613         return IRQ_HANDLED;
2614 }
2615
2616
2617
2618 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
2619 {
2620         int ret;
2621         int i;
2622         struct kobject *rk818_fg_kobj;
2623
2624         ret = create_sysfs_interfaces(dev);
2625         if (ret < 0) {
2626                 ret = -EINVAL;
2627                 dev_err(dev, "device RK818 battery sysfs register failed\n");
2628                 goto err_sysfs;
2629         }
2630
2631         rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
2632         if (!rk818_fg_kobj)
2633                 return -ENOMEM;
2634         for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
2635                 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
2636                 if (ret != 0) {
2637                         dev_err(dev, "create rk818_battery node error\n");
2638                         goto err_sysfs;
2639                 }
2640         }
2641
2642         return ret;
2643
2644 err_sysfs:
2645         power_supply_unregister(&di->ac);
2646         power_supply_unregister(&di->usb);
2647         power_supply_unregister(&di->bat);
2648
2649         return ret;
2650 }
2651
2652 static void rk818_battery_irq_init(struct battery_info *di)
2653 {
2654         int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
2655         int ret;
2656         struct rk818 *chip = di->rk818;
2657
2658         vb_lo_irq               = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
2659         plug_in_irq     = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
2660         plug_out_irq    = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
2661         chg_ok_irq      = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
2662
2663         ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
2664                                         IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
2665         if (ret != 0)
2666                 dev_err(chip->dev, "vb_lo_irq request failed!\n");
2667
2668         di->irq = vb_lo_irq;
2669         enable_irq_wake(di->irq);
2670         disable_vbat_low_irq(di);
2671
2672         ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
2673                                         IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
2674         if (ret != 0)
2675                 dev_err(chip->dev, "plug_in_irq request failed!\n");
2676
2677
2678         ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
2679                                         IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
2680         if (ret != 0)
2681                 dev_err(chip->dev, "plug_out_irq request failed!\n");
2682
2683
2684         ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
2685                                         IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
2686         if (ret != 0)
2687                 dev_err(chip->dev, "chg_ok_irq request failed!\n");
2688 }
2689
2690 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
2691 {
2692         u32 fcc_capacity;
2693
2694         di->rk818 = chip;
2695         g_battery = di;
2696         di->platform_data = chip->battery_data;
2697         di->cell.config = di->platform_data->cell_cfg;
2698         di->design_capacity = di->platform_data->cell_cfg->design_capacity;
2699         di->qmax = di->platform_data->cell_cfg->design_qmax;
2700         di->fcc = di->design_capacity;
2701         di->vol_smooth_time = 0;
2702         di->charge_smooth_time = 0;
2703         di->charge_smooth_status = false;
2704         di->sleep_status = 0;
2705         di->work_on = 0;
2706         di->sys_wakeup = true;
2707         di->pcb_ioffset = 0;
2708         di->pcb_ioffset_updated = false;
2709         di->queue_work_cnt = 0;
2710         di->update_k = 0;
2711         di->update_q = 0;
2712         di->voltage_old = 0;
2713         di->display_soc = 0;
2714         di->bat_res = 0;
2715         di->bat_res_updated = false;
2716         di->resume = false;
2717         di->sys_wakeup = true;
2718         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2719
2720         di->debug_finish_real_soc = 0;
2721         di->debug_finish_temp_soc = 0;
2722
2723         fcc_capacity = _get_FCC_capacity(di);
2724         if (fcc_capacity > 1000)
2725                 di->fcc = fcc_capacity;
2726         else
2727                 di->fcc = di->design_capacity;
2728 }
2729 /*
2730 static struct of_device_id rk818_battery_of_match[] = {
2731 { .compatible = "rk818_battery" },
2732 { }
2733 };
2734
2735 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
2736 */
2737 #ifdef CONFIG_OF
2738 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2739 {
2740         struct device_node *regs, *rk818_pmic_np;
2741         struct battery_platform_data *data;
2742         struct cell_config *cell_cfg;
2743         struct ocv_config *ocv_cfg;
2744         struct property *prop;
2745         u32 out_value;
2746         int length, ret;
2747
2748         rk818_pmic_np = of_node_get(rk818->dev->of_node);
2749         if (!rk818_pmic_np) {
2750                 dev_err(dev, "could not find pmic sub-node\n");
2751                 return -EINVAL;
2752         }
2753
2754         regs = of_find_node_by_name(rk818_pmic_np, "battery");
2755         if (!regs) {
2756                 dev_err(dev, "battery node not found!\n");
2757                 return -EINVAL;
2758         }
2759
2760         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
2761         if (!data) {
2762                 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
2763                 return -ENOMEM;
2764         }
2765
2766         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
2767         if (!cell_cfg) {
2768                 dev_err(dev, "kzalloc for cell_config failed!\n");
2769                 return -ENOMEM;
2770         }
2771         ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
2772         if (!ocv_cfg) {
2773                 dev_err(dev, "kzalloc for ocv_config failed!\n");
2774                 return -ENOMEM;
2775         }
2776
2777         prop = of_find_property(regs, "ocv_table", &length);
2778         if (!prop) {
2779                 dev_err(dev, "ocv_table not found!\n");
2780                 return -EINVAL;
2781         }
2782         data->ocv_size = length / sizeof(u32);
2783
2784         if (data->ocv_size > 0) {
2785                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
2786
2787                 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
2788                 if (!data->battery_ocv) {
2789                         dev_err(dev, "kzalloc for ocv_table failed!\n");
2790                         return -ENOMEM;
2791                 }
2792                 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
2793                 if (ret < 0)
2794                         return ret;
2795         }
2796
2797         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
2798         if (ret < 0) {
2799                 dev_err(dev, "max_charge_currentmA not found!\n");
2800                 return ret;
2801         }
2802         data->max_charger_currentmA = out_value;
2803
2804         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
2805         if (ret < 0) {
2806                 dev_err(dev, "max_charge_voltagemV not found!\n");
2807                 return ret;
2808         }
2809         data->max_charger_voltagemV = out_value;
2810
2811         ret = of_property_read_u32(regs, "design_capacity", &out_value);
2812         if (ret < 0) {
2813                 dev_err(dev, "design_capacity not found!\n");
2814                 return ret;
2815         }
2816         cell_cfg->design_capacity  = out_value;
2817
2818         ret = of_property_read_u32(regs, "design_qmax", &out_value);
2819         if (ret < 0) {
2820                 dev_err(dev, "design_qmax not found!\n");
2821                 return ret;
2822         }
2823         cell_cfg->design_qmax = out_value;
2824
2825         ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
2826         if (ret < 0) {
2827                 dev_err(dev, "sleep_enter_current not found!\n");
2828                 return ret;
2829         }
2830         ocv_cfg->sleep_enter_current = out_value;
2831
2832         ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
2833         if (ret < 0) {
2834                 dev_err(dev, "sleep_exit_current not found!\n");
2835                 return ret;
2836         }
2837         ocv_cfg->sleep_exit_current = out_value;
2838
2839         ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
2840
2841         cell_cfg->ocv = ocv_cfg;
2842         data->cell_cfg = cell_cfg;
2843         rk818->battery_data = data;
2844
2845         DBG("\n--------- the battery OCV TABLE dump:\n");
2846         DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
2847         DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
2848         DBG("design_capacity :%d\n", cell_cfg->design_capacity);
2849         DBG("design_qmax :%d\n", cell_cfg->design_qmax);
2850         DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
2851         DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
2852         DBG("uboot chrg = %d\n", support_uboot_chrg);
2853         DBG("\n--------- rk818_battery dt_parse ok.\n");
2854         return 0;
2855 }
2856
2857 #else
2858 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
2859 {
2860         return -ENODEV;
2861 }
2862 #endif
2863
2864
2865 static int battery_probe(struct platform_device *pdev)
2866 {
2867         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
2868         struct battery_info *di;
2869         int ret;
2870
2871         DBG("battery driver version %s\n", DRIVER_VERSION);
2872         di = kzalloc(sizeof(*di), GFP_KERNEL);
2873         if (!di) {
2874                 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
2875                 return -ENOMEM;
2876         }
2877         ret = rk_battery_parse_dt(chip, &pdev->dev);
2878         if (ret < 0) {
2879                 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
2880                 return -EINVAL;
2881         }
2882
2883         platform_set_drvdata(pdev, di);
2884         battery_info_init(di, chip);
2885         if (!is_bat_exist(di)) {
2886                 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
2887                 return -ENODEV;
2888         }
2889         fg_init(di);
2890
2891         wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
2892
2893         flatzone_voltage_init(di);
2894         battery_poweron_status_init(di);
2895         battery_power_supply_init(di);
2896         ret = battery_power_supply_register(di, &pdev->dev);
2897         if (ret) {
2898                 dev_err(&pdev->dev, "rk power supply register failed!\n");
2899                 return ret;
2900         }
2901         di->wq = create_singlethread_workqueue("battery-work");
2902         INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
2903         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2904         INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
2905
2906         di->battery_nb.notifier_call = battery_notifier_call;
2907         register_battery_notifier(&di->battery_nb);
2908
2909         rk818_battery_irq_init(di);
2910         rk818_battery_sysfs_init(di, &pdev->dev);
2911         DBG("------ RK81x battery_probe ok!-------\n");
2912         return ret;
2913 }
2914
2915
2916 #ifdef CONFIG_PM
2917
2918 static int battery_suspend(struct platform_device *dev, pm_message_t state)
2919 {
2920         struct battery_info *di = platform_get_drvdata(dev);
2921
2922         enable_vbat_low_irq(di);
2923         di->sleep_status = di->status;
2924         di->suspend_charge_current = _get_average_current(di);
2925
2926         /* avoid abrupt wakeup which will clean the variable*/
2927         if (di->sys_wakeup) {
2928                 di->suspend_capacity = di->remain_capacity;
2929                 di->suspend_temp_soc = _get_soc(di);
2930                 di->suspend_time_start = get_seconds();
2931                 di->sys_wakeup = false;
2932         }
2933
2934         cancel_delayed_work(&di->battery_monitor_work);
2935         DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
2936             __func__, di->suspend_temp_soc, di->suspend_charge_current,
2937             di->suspend_capacity, di->sleep_status);
2938
2939         return 0;
2940 }
2941
2942 static int battery_resume(struct platform_device *dev)
2943 {
2944         struct battery_info *di = platform_get_drvdata(dev);
2945
2946         di->resume = true;
2947         DBG("<%s>\n", __func__);
2948         disable_vbat_low_irq(di);
2949         queue_delayed_work(di->wq, &di->battery_monitor_work,
2950                                         msecs_to_jiffies(TIMER_MS_COUNTS/2));
2951
2952         if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
2953                         di->real_soc <= 5)
2954                 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
2955
2956         return 0;
2957 }
2958 static int battery_remove(struct platform_device *dev)
2959 {
2960         struct battery_info *di = platform_get_drvdata(dev);
2961
2962         cancel_delayed_work_sync(&di->battery_monitor_work);
2963         return 0;
2964 }
2965 static void battery_shutdown(struct platform_device *dev)
2966 {
2967         struct battery_info *di = platform_get_drvdata(dev);
2968
2969         cancel_delayed_work_sync(&di->battery_monitor_work);
2970         DBG("rk818 shutdown!");
2971 }
2972 #endif
2973
2974 static struct platform_driver battery_driver = {
2975         .driver     = {
2976                 .name   = "rk818-battery",
2977                 .owner  = THIS_MODULE,
2978         },
2979
2980         .probe      = battery_probe,
2981         .remove     = battery_remove,
2982         .suspend    = battery_suspend,
2983         .resume     = battery_resume,
2984         .shutdown  = battery_shutdown,
2985 };
2986
2987 static int __init battery_init(void)
2988 {
2989         return platform_driver_register(&battery_driver);
2990 }
2991
2992 fs_initcall_sync(battery_init);
2993 static void __exit battery_exit(void)
2994 {
2995         platform_driver_unregister(&battery_driver);
2996 }
2997 module_exit(battery_exit);
2998
2999 MODULE_LICENSE("GPL");
3000 MODULE_ALIAS("platform:rk818-battery");
3001 MODULE_AUTHOR("ROCKCHIP");
3002