c544efd7bda0b568c8a908a872785e2c9747c7e8
[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 <linux/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 #include <linux/of_gpio.h>
30 #include <linux/irq.h>
31 #include <linux/irqdomain.h>
32
33 /* if you  want to disable, don't set it as 0,
34                         just be: "static int dbg_enable;" is ok*/
35 static int dbg_enable;
36 #define RK818_SYS_DBG 1
37
38 module_param_named(dbg_level, dbg_enable, int, 0644);
39 #define DBG(args...) \
40         do { \
41                 if (dbg_enable) { \
42                         pr_info(args); \
43                 } \
44         } while (0)
45
46
47 #define DEFAULT_BAT_RES                 135
48 #define DEFAULT_VLMT                    4200
49 #define DEFAULT_ILMT                    2000
50 #define DEFAULT_ICUR                    1600
51
52 #define DEF_TEST_ILMT_MA                2000
53 #define DEF_TEST_CURRENT_MA             1800
54
55 #define DSOC_DISCHRG_FAST_DEC_SEC       120     /*seconds*/
56 #define DSOC_DISCHRG_FAST_EER_RANGE     25
57 #define DSOC_CHRG_FAST_CALIB_CURR_MAX   400     /*mA*/
58 #define DSOC_CHRG_FAST_INC_SEC          120     /*seconds*/
59 #define DSOC_CHRG_FAST_EER_RANGE        15
60 #define DSOC_CHRG_EMU_CURR              1200
61 #define DSOC_CHG_TERM_CURR              600
62 #define DSOC_CHG_TERM_VOL               4100
63 #define CHG_FINISH_VOL                  4100
64
65 /*realtime RSOC calib param*/
66 #define RSOC_DISCHG_ERR_LOWER   40
67 #define RSOC_DISCHG_ERR_UPPER   50
68 #define RSOC_ERR_CHCK_CNT       15
69 #define RSOC_COMPS              20      /*compensation*/
70 #define RSOC_CALIB_CURR_MAX     900     /*mA*/
71 #define RSOC_CALIB_DISCHGR_TIME 3       /*min*/
72
73 #define RSOC_RESUME_ERR         10
74 #define REBOOT_INTER_MIN        1
75
76 #define INTERPOLATE_MAX         1000
77 #define MAX_INT                 0x7FFF
78 #define TIME_10MIN_SEC          600
79
80 #define CHG_VOL_SHIFT           4
81 #define CHG_ILIM_SHIFT          0
82 #define CHG_ICUR_SHIFT          0
83 #define DEF_CHRG_VOL            CHRG_VOL4200
84 #define DEF_CHRG_CURR_SEL       CHRG_CUR1400mA
85 #define DEF_CHRG_CURR_LMT       ILIM_2000MA
86
87 /*TEST_POWER_MODE params*/
88 #define TEST_CURRENT            1000
89 #define TEST_VOLTAGE            3800
90 #define TEST_SOC                66
91 #define TEST_STATUS             POWER_SUPPLY_STATUS_CHARGING
92 #define TEST_PRESET             1
93 #define TEST_AC_ONLINE          1
94 #define TEST_USB_ONLINE         0
95
96 /*
97  * the following table value depends on datasheet
98  */
99 int CHG_V_LMT[] = {4050, 4100, 4150, 4200, 4300, 4350};
100
101 int CHG_I_CUR[] = {1000, 1200, 1400, 1600, 1800, 2000,
102                    2250, 2400, 2600, 2800, 3000};
103
104 int CHG_I_LMT[] = {450, 800, 850, 1000, 1250, 1500, 1750,
105                    2000, 2250, 2500, 2750, 3000};
106
107 u8 CHG_CVCC_HOUR[] = {4, 5, 6, 8, 10, 12, 14, 16};
108
109 #define RK818_DC_IN             0
110 #define RK818_DC_OUT            1
111 #define SEC_TO_MIN(x)           ((x)/60)
112 #define BASE_TO_MIN(x)          ((get_seconds()-(x))/60)
113 #define BASE_TO_SEC(x)          (get_seconds()-(x))
114
115 #define OCV_VALID_SHIFT         (0)
116 #define OCV_CALIB_SHIFT         (1)
117 #define FIRST_PWRON_SHIFT       (2)
118
119
120 struct battery_info {
121         struct device                   *dev;
122         struct cell_state               cell;
123         struct power_supply             bat;
124         struct power_supply             ac;
125         struct power_supply             usb;
126         struct delayed_work             work;
127         /* struct i2c_client            *client; */
128         struct rk818                    *rk818;
129         struct pinctrl                  *pinctrl;
130         struct pinctrl_state            *pins_default;
131
132
133         struct battery_platform_data    *platform_data;
134
135         int                             dc_det_pin;
136         int                             dc_det_level;
137         int                             dc_det_pullup_inside;
138         int                             work_on;
139         int                             irq;
140         int                             ac_online;
141         int                             usb_online;
142         int                             dc_online;
143         int                             status;
144         int                             current_avg;
145         int                             current_offset;
146
147         uint16_t                        voltage;
148         uint16_t                        voltage_ocv;
149         uint16_t                        relax_voltage;
150         u8                              charge_status;
151         u8                              otg_status;
152         int                             pcb_ioffset;
153         bool                            pcb_ioffset_updated;
154         unsigned long                   queue_work_cnt;
155         u32                             term_chg_cnt;
156         u32                             emu_chg_cnt;
157
158         uint16_t                        warnning_voltage;
159
160         int                             design_capacity;
161         int                             fcc;
162         int                             qmax;
163         int                             remain_capacity;
164         int                             nac;
165         int                             temp_nac;
166         int                             real_soc;
167         int                             display_soc;
168         int                             odd_capacity;
169         int                             temp_soc;
170
171         int                             est_ocv_vol;
172         int                             est_ocv_soc;
173         u8                              err_chck_cnt;
174         int                             err_soc_sum;
175         int                             bat_res_update_cnt;
176         int                             soc_counter;
177         int                             dod0;
178         int                             dod0_status;
179         int                             dod0_voltage;
180         int                             dod0_capacity;
181         unsigned long                   dod0_time;
182         u8                              dod0_level;
183         int                             adjust_cap;
184
185         int                             enter_flatzone;
186         int                             exit_flatzone;
187
188         int                             time2empty;
189         int                             time2full;
190
191         int                             *ocv_table;
192         int                             *res_table;
193
194         int                             current_k;/* (ICALIB0, ICALIB1) */
195         int                             current_b;
196
197         int                             voltage_k;/* VCALIB0 VCALIB1 */
198         int                             voltage_b;
199
200         int                             zero_updated;
201         int                             old_display_soc;
202         int                             zero_cycle;
203
204
205         int                             update_k;
206         int                             line_k;
207         int                             voltage_old;
208
209         int                             q_dead;
210         int                             q_err;
211         int                             q_shtd;
212
213         u8                              check_count;
214         /* u32                          status; */
215         struct timeval                  soc_timer;
216         struct timeval                  change_timer;
217
218         int                             vol_smooth_time;
219         int                             charge_smooth_time;
220         int                             sum_suspend_cap;
221         int                             suspend_cap;
222         int                             resume_capacity;
223         struct timespec                 suspend_time;
224         struct timespec                 resume_time;
225         unsigned long                   suspend_time_start;
226         unsigned long                   count_sleep_time;
227
228         int                             suspend_rsoc;
229         int                             sleep_status;
230         int                             suspend_charge_current;
231         int                             resume_soc;
232         int                             bat_res;
233         bool                            bat_res_updated;
234         bool                            charge_smooth_status;
235         bool                            resume;
236         unsigned long                   last_plugin_time;
237         bool                            sys_wakeup;
238
239         unsigned long                   charging_time;
240         unsigned long                   discharging_time;
241         unsigned long                   finish_time;
242
243         u32                             charge_min;
244         u32                             discharge_min;
245         u32                             finish_min;
246         struct notifier_block           battery_nb;
247         struct workqueue_struct         *wq;
248         struct delayed_work             battery_monitor_work;
249         struct delayed_work             charge_check_work;
250         int                             charge_otg;
251
252         struct wake_lock                resume_wake_lock;
253         unsigned long                   sys_on_base;
254         unsigned long                   chrg_time_base;
255         int                             chrg_time2_full;
256         int                             chrg_cap2_full;
257
258         bool                            is_first_poweron;
259         int                             first_on_cap;
260
261
262         int                             fg_drv_mode;
263         int                             test_chrg_current;
264         int                             test_chrg_ilmt;
265         int                             debug_finish_real_soc;
266         int                             debug_finish_temp_soc;
267         int                             chrg_min[10];
268         int                             chg_v_lmt;
269         int                             chg_i_lmt;
270         int                             chg_i_cur;
271
272 };
273
274 struct battery_info *g_battery;
275 u32 support_uboot_chrg, support_usb_adp, support_dc_adp;
276 static void rk81x_update_battery_info(struct battery_info *di);
277
278 static bool rk81x_support_adp_type(enum hw_support_adp_t type)
279 {
280         bool bl = false;
281
282         switch (type) {
283         case HW_ADP_TYPE_USB:
284                 if (support_usb_adp)
285                         bl = true;
286                 break;
287         case HW_ADP_TYPE_DC:
288                 if (support_dc_adp)
289                         bl = true;
290                 break;
291         case HW_ADP_TYPE_DUAL:
292                 if (support_usb_adp && support_dc_adp)
293                         bl = true;
294                 break;
295         default:
296                         break;
297         }
298
299         return bl;
300 }
301
302 static u32 interpolate(int value, u32 *table, int size)
303 {
304         uint8_t i;
305         uint16_t d;
306
307         for (i = 0; i < size; i++) {
308                 if (value < table[i])
309                         break;
310         }
311
312         if ((i > 0) && (i < size)) {
313                 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
314                 d /= table[i] - table[i-1];
315                 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
316         } else {
317                 d = i * ((INTERPOLATE_MAX+size/2)/size);
318         }
319
320         if (d > 1000)
321                 d = 1000;
322
323         return d;
324 }
325 /* Returns (a * b) / c */
326 static int32_t ab_div_c(u32 a, u32 b, u32 c)
327 {
328         bool sign;
329         u32 ans = MAX_INT;
330         int32_t tmp;
331
332         sign = ((((a^b)^c) & 0x80000000) != 0);
333
334         if (c != 0) {
335                 if (sign)
336                         c = -c;
337
338                 tmp = ((int32_t) a*b + (c>>1)) / c;
339
340                 if (tmp < MAX_INT)
341                         ans = tmp;
342         }
343
344         if (sign)
345                 ans = -ans;
346
347         return ans;
348 }
349
350 static  int32_t abs_int(int32_t x)
351 {
352         return (x > 0) ? x : -x;
353 }
354
355 static  int abs32_int(int x)
356 {
357         return (x > 0) ? x : -x;
358 }
359
360 static int div(int val)
361 {
362         return (val == 0) ? 1 : val;
363 }
364
365 static int battery_read(struct rk818 *rk818, u8 reg,
366                         u8 buf[], unsigned len)
367 {
368         int ret;
369
370         ret = rk818_i2c_read(rk818, reg, len, buf);
371         return ret;
372 }
373
374 static int battery_write(struct rk818 *rk818, u8 reg,
375                          u8 const buf[], unsigned len)
376 {
377         int ret;
378
379         ret = rk818_i2c_write(rk818, reg, (int)len, *buf);
380         return ret;
381 }
382
383 static void rk81x_set_bit(struct battery_info *di, u8 reg, u8 shift)
384 {
385         rk818_set_bits(di->rk818, reg, 1 << shift, 1 << shift);
386 }
387
388 static void rk81x_clr_bit(struct battery_info *di, u8 reg, u8 shift)
389 {
390         rk818_set_bits(di->rk818, reg, 1 << shift, 0 << shift);
391 }
392
393 static u8 rk81x_read_bit(struct battery_info *di, u8 reg, u8 shift)
394 {
395         u8 buf;
396         u8 val;
397
398         battery_read(di->rk818, reg, &buf, 1);
399         val = (buf & BIT(shift)) >> shift;
400         return val;
401 }
402
403 static void dump_gauge_register(struct battery_info *di)
404 {
405         int i = 0;
406         char buf;
407
408         DBG("%s dump charger register start:\n", __func__);
409         for (i = 0xAC; i < 0xDF; i++) {
410                 battery_read(di->rk818, i, &buf, 1);
411                 DBG(" the register is  0x%02x, the value is 0x%02x\n", i, buf);
412         }
413         DBG("demp end!\n");
414 }
415
416 static void dump_charger_register(struct battery_info *di)
417 {
418         int i = 0;
419         char buf;
420
421         DBG("%s dump the register start:\n", __func__);
422         for (i = 0x99; i < 0xAB; i++) {
423                 battery_read(di->rk818, i, &buf, 1);
424                 DBG(" the register is  0x%02x, the value is 0x%02x\n", i, buf);
425         }
426         DBG("demp end!\n");
427 }
428
429 #if RK818_SYS_DBG
430
431 static void  _capacity_init(struct battery_info *di, u32 capacity);
432
433 /*
434  * interface for debug: do rsoc_first_poweron_init() without unloading battery
435  */
436 static ssize_t bat_calib_read(struct device *dev,
437                               struct device_attribute *attr, char *buf)
438 {
439         struct battery_info *di = g_battery;
440         int val;
441
442         val = rk81x_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
443         return sprintf(buf, "%d\n", val);
444 }
445
446 static ssize_t bat_calib_write(struct device *dev,
447                                struct device_attribute *attr,
448                                const char *buf, size_t count)
449 {
450         u8 val;
451         int ret;
452         struct battery_info *di = g_battery;
453
454         ret = kstrtou8(buf, 0, &val);
455         if (ret < 0)
456                 return ret;
457
458         if (val)
459                 rk81x_set_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
460         else
461                 rk81x_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
462         return count;
463 }
464
465 /*
466  * interface for debug: force battery to over discharge
467  */
468 static ssize_t bat_test_power_read(struct device *dev,
469                                    struct device_attribute *attr, char *buf)
470 {
471         struct battery_info *di = g_battery;
472
473         return sprintf(buf, "%d\n", di->fg_drv_mode);
474 }
475
476 static ssize_t bat_test_power_write(struct device *dev,
477                                     struct device_attribute *attr,
478                                     const char *buf, size_t count)
479 {
480         u8 val;
481         int ret;
482         struct battery_info *di = g_battery;
483
484         ret = kstrtou8(buf, 0, &val);
485         if (ret < 0)
486                 return ret;
487
488         if (val == 1)
489                 di->fg_drv_mode = TEST_POWER_MODE;
490         else
491                 di->fg_drv_mode = FG_NORMAL_MODE;
492
493         return count;
494 }
495
496
497 static ssize_t bat_state_read(struct device *dev,
498                               struct device_attribute *attr, char *buf)
499 {
500         struct battery_info *di = g_battery;
501
502         return sprintf(buf, "dsoc = %d, rsoc = %d\n",
503                                 di->real_soc, di->temp_soc);
504 }
505
506 static ssize_t bat_fcc_read(struct device *dev,
507                             struct device_attribute *attr, char *buf)
508 {
509         struct battery_info *di = g_battery;
510
511         return sprintf(buf, "%d", di->fcc);
512 }
513
514 static ssize_t bat_fcc_write(struct device *dev,
515                              struct device_attribute *attr,
516                              const char *buf, size_t count)
517 {
518         u16 val;
519         int ret;
520         struct battery_info *di = g_battery;
521
522         ret = kstrtou16(buf, 0, &val);
523         if (ret < 0)
524                 return ret;
525
526         di->fcc = val;
527
528         return count;
529 }
530
531
532 static ssize_t bat_soc_read(struct device *dev,
533                             struct device_attribute *attr, char *buf)
534 {
535         struct battery_info *di = g_battery;
536
537         return sprintf(buf, "%d", di->real_soc);
538 }
539
540 static ssize_t bat_soc_write(struct device *dev,
541                              struct device_attribute *attr,
542                              const char *buf, size_t count)
543 {
544         u8 val;
545         int ret;
546         struct battery_info *di = g_battery;
547
548         ret = kstrtou8(buf, 0, &val);
549         if (ret < 0)
550                 return ret;
551
552         di->real_soc = val;
553
554         return count;
555 }
556 static ssize_t bat_temp_soc_read(struct device *dev,
557                                  struct device_attribute *attr, char *buf)
558 {
559         struct battery_info *di = g_battery;
560
561         return sprintf(buf, "%d", di->temp_soc);
562 }
563
564 static ssize_t bat_temp_soc_write(struct device *dev,
565                                   struct device_attribute *attr,
566                                   const char *buf, size_t count)
567 {
568         u8 val;
569         int ret;
570         u32 capacity;
571         struct battery_info *di = g_battery;
572
573         ret = kstrtou8(buf, 0, &val);
574         if (ret < 0)
575                 return ret;
576
577         capacity = di->fcc*val/100;
578         _capacity_init(di, capacity);
579
580         return count;
581 }
582
583 static ssize_t bat_voltage_read(struct device *dev,
584                                 struct device_attribute *attr, char *buf)
585 {
586         struct battery_info *di = g_battery;
587
588         return sprintf(buf, "%d", di->voltage);
589 }
590
591 static ssize_t bat_avr_current_read(struct device *dev,
592                                     struct device_attribute *attr, char *buf)
593 {
594         struct battery_info *di = g_battery;
595
596         return sprintf(buf, "%d", di->current_avg);
597 }
598
599 static ssize_t bat_remain_cap_read(struct device *dev,
600                                    struct device_attribute *attr,
601                                    char *buf)
602 {
603         struct battery_info *di = g_battery;
604
605         return sprintf(buf, "%d", di->remain_capacity);
606 }
607
608 /*
609  * interface for debug: debug info switch
610  */
611 static ssize_t bat_debug_write(struct device *dev,
612                                struct device_attribute *attr,
613                                const char *buf, size_t count)
614 {
615         u8 val;
616         int ret;
617
618         ret = kstrtou8(buf, 0, &val);
619         if (ret < 0)
620                 return ret;
621
622         dbg_enable = val;
623
624         return count;
625 }
626
627 static ssize_t bat_regs_read(struct device *dev,
628                              struct device_attribute *attr, char *buf)
629 {
630         u32 i;
631         u32 start_offset = 0x0;
632         u32 end_offset = 0xf2;
633         struct battery_info *di = g_battery;
634         u8 val;
635         char *str = buf;
636
637         str += sprintf(str, "start from add=0x%x, offset=0x%x\n",
638                        start_offset, end_offset);
639
640         for (i = start_offset; i <= end_offset; ) {
641                 battery_read(di->rk818, i, &val, 1);
642                 str += sprintf(str, "0x%x=0x%x", i, val);
643
644                 if (i % 4 == 0) {
645                         str += sprintf(str, "\n");
646                 } else {
647                         if (i != end_offset)
648                                 str += sprintf(str, "   ");
649                         else
650                                 str += sprintf(str, "\n");
651                 }
652                 i++;
653         }
654         return (str - buf);
655 }
656
657
658 static struct device_attribute rk818_bat_attr[] = {
659         __ATTR(fcc, 0664, bat_fcc_read, bat_fcc_write),
660         __ATTR(soc, 0664, bat_soc_read, bat_soc_write),
661         __ATTR(temp_soc, 0664, bat_temp_soc_read, bat_temp_soc_write),
662         __ATTR(voltage, 0664, bat_voltage_read, NULL),
663         __ATTR(avr_current, 0664, bat_avr_current_read, NULL),
664         __ATTR(remain_capacity, 0664, bat_remain_cap_read, NULL),
665         __ATTR(debug, 0664, NULL, bat_debug_write),
666         __ATTR(regs, 0664, bat_regs_read, NULL),
667         __ATTR(state, 0664, bat_state_read, NULL),
668         __ATTR(test_power, 0664, bat_test_power_read, bat_test_power_write),
669         __ATTR(calib, 0664, bat_calib_read, bat_calib_write),
670 };
671
672 #endif
673
674 static uint16_t get_relax_voltage(struct battery_info *di);
675
676 static ssize_t show_state_attrs(struct device *dev,
677                                 struct device_attribute *attr, char *buf)
678 {
679         struct battery_info *data = g_battery;
680
681         if (0 == get_relax_voltage(data)) {
682                 return sprintf(buf,
683                         "voltage = %d, remain_capacity = %d, status = %d\n",
684                         data->voltage, data->remain_capacity,
685                         data->status);
686
687         } else
688                 return sprintf(buf,
689                         "voltage = %d, remain_capacity = %d, status = %d\n",
690                         get_relax_voltage(data), data->remain_capacity,
691                         data->status);
692 }
693
694 static ssize_t restore_state_attrs(struct device *dev,
695                                    struct device_attribute *attr,
696                                    const char *buf, size_t size)
697 {
698         return size;
699 }
700 static struct device_attribute rkbatt_attrs[] = {
701         __ATTR(state, 0664, show_state_attrs, restore_state_attrs),
702 };
703
704 static int create_sysfs_interfaces(struct device *dev)
705 {
706         int i;
707
708         for (i = 0; i < ARRAY_SIZE(rkbatt_attrs); i++) {
709                 if (device_create_file(dev, rkbatt_attrs + i))
710                         goto error;
711         }
712
713         return 0;
714
715 error:
716         for (; i >= 0; i--)
717                 device_remove_file(dev, rkbatt_attrs + i);
718
719         dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
720         return -1;
721 }
722
723 static int  _gauge_enable(struct battery_info *di)
724 {
725         int ret;
726         u8 buf;
727
728
729         ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
730         if (ret < 0) {
731                 dev_err(di->dev, "error reading TS_CTRL_REG");
732                 return ret;
733         }
734         if (!(buf & GG_EN)) {
735                 buf |= GG_EN;
736                 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1);/*enable*/
737                 ret = battery_read(di->rk818, TS_CTRL_REG, &buf, 1);
738                 return 0;
739         }
740
741         DBG("%s, %d\n", __func__, buf);
742         return 0;
743 }
744
745 static void save_level(struct  battery_info *di, u8 save_soc)
746 {
747         u8 soc;
748
749         soc = save_soc;
750         battery_write(di->rk818, UPDAT_LEVE_REG, &soc, 1);
751 }
752 static u8 get_level(struct  battery_info *di)
753 {
754         u8 soc;
755
756         battery_read(di->rk818, UPDAT_LEVE_REG, &soc, 1);
757         return soc;
758 }
759
760 static int _get_vcalib0(struct battery_info *di)
761 {
762         int ret;
763         int temp = 0;
764         u8 buf;
765
766         ret = battery_read(di->rk818, VCALIB0_REGL, &buf, 1);
767         temp = buf;
768         ret = battery_read(di->rk818, VCALIB0_REGH, &buf, 1);
769         temp |= buf<<8;
770
771         DBG("%s voltage0 offset vale is %d\n", __func__, temp);
772         return temp;
773 }
774
775 static int _get_vcalib1(struct  battery_info *di)
776 {
777         int ret;
778         int temp = 0;
779         u8 buf;
780
781         ret = battery_read(di->rk818, VCALIB1_REGL, &buf, 1);
782         temp = buf;
783         ret = battery_read(di->rk818, VCALIB1_REGH, &buf, 1);
784         temp |= buf<<8;
785
786         DBG("%s voltage1 offset vale is %d\n", __func__, temp);
787         return temp;
788 }
789
790 static int _get_ioffset(struct battery_info *di)
791 {
792         int ret;
793         int temp = 0;
794         u8 buf;
795
796         ret = battery_read(di->rk818, IOFFSET_REGL, &buf, 1);
797         temp = buf;
798         ret = battery_read(di->rk818, IOFFSET_REGH, &buf, 1);
799         temp |= buf<<8;
800
801         return temp;
802 }
803
804 static uint16_t  _get_cal_offset(struct battery_info *di)
805 {
806         int ret;
807         uint16_t temp = 0;
808         u8 buf;
809
810         ret = battery_read(di->rk818, CAL_OFFSET_REGL, &buf, 1);
811         temp = buf;
812         ret = battery_read(di->rk818, CAL_OFFSET_REGH, &buf, 1);
813         temp |= buf<<8;
814
815         return temp;
816 }
817 static int _set_cal_offset(struct battery_info *di, u32 value)
818 {
819         int ret;
820         u8 buf;
821
822         buf = value&0xff;
823         ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);
824         buf = (value >> 8)&0xff;
825         ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);
826
827         return 0;
828 }
829 static void _get_voltage_offset_value(struct battery_info *di)
830 {
831         int vcalib0, vcalib1;
832
833         vcalib0 = _get_vcalib0(di);
834         vcalib1 = _get_vcalib1(di);
835
836         di->voltage_k = (4200 - 3000)*1000/div((vcalib1 - vcalib0));
837         di->voltage_b = 4200 - (di->voltage_k*vcalib1)/1000;
838         DBG("voltage_k=%d(x1000),voltage_b=%d\n", di->voltage_k, di->voltage_b);
839 }
840 static uint16_t _get_OCV_voltage(struct battery_info *di)
841 {
842         int ret;
843         u8 buf;
844         uint16_t temp;
845         uint16_t voltage_now = 0;
846         int i;
847         int val[3];
848
849         for (i = 0; i < 3; i++) {
850                 ret = battery_read(di->rk818, BAT_OCV_REGL, &buf, 1);
851                 val[i] = buf;
852                 ret = battery_read(di->rk818, BAT_OCV_REGH, &buf, 1);
853                 val[i] |= buf<<8;
854
855                 if (ret < 0) {
856                         dev_err(di->dev, "error read BAT_OCV_REGH");
857                         return ret;
858                 }
859         }
860
861         if (val[0] == val[1])
862                 temp = val[0];
863         else
864                 temp = val[2];
865
866         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
867
868         return voltage_now;
869 }
870
871 static int _get_battery_voltage(struct battery_info *di)
872 {
873         int ret;
874         int voltage_now = 0;
875         u8 buf;
876         int temp;
877         int val[3];
878         int i;
879
880         for (i = 0; i < 3; i++) {
881                 ret = battery_read(di->rk818, BAT_VOL_REGL, &buf, 1);
882                 val[i] = buf;
883                 ret = battery_read(di->rk818, BAT_VOL_REGH, &buf, 1);
884                 val[i] |= buf<<8;
885
886                 if (ret < 0) {
887                         dev_err(di->dev, "error read BAT_VOL_REGH");
888                         return ret;
889                 }
890         }
891         /*check value*/
892         if (val[0] == val[1])
893                 temp = val[0];
894         else
895                 temp = val[2];
896
897         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
898
899         return voltage_now;
900 }
901
902 /* OCV Lookup table
903  * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
904  * based on the voltage.
905  */
906 static int _voltage_to_capacity(struct battery_info *di, int voltage)
907 {
908         u32 *ocv_table;
909         int ocv_size;
910         u32 tmp;
911         int ocv_soc;
912
913         ocv_table = di->platform_data->battery_ocv;
914         ocv_size = di->platform_data->ocv_size;
915         di->warnning_voltage = ocv_table[3];
916         tmp = interpolate(voltage, ocv_table, ocv_size);
917         ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
918         di->temp_nac = ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
919
920         return ocv_soc;
921 }
922
923 static uint16_t _get_relax_vol1(struct battery_info *di)
924 {
925         int ret;
926         u8 buf;
927         uint16_t temp = 0, voltage_now;
928
929         ret = battery_read(di->rk818, RELAX_VOL1_REGL, &buf, 1);
930         temp = buf;
931         ret = battery_read(di->rk818, RELAX_VOL1_REGH, &buf, 1);
932         temp |= (buf<<8);
933
934         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
935
936         return voltage_now;
937 }
938
939 static uint16_t _get_relax_vol2(struct battery_info *di)
940 {
941         int ret;
942         uint16_t temp = 0, voltage_now;
943         u8 buf;
944
945         ret = battery_read(di->rk818, RELAX_VOL2_REGL, &buf, 1);
946         temp = buf;
947         ret = battery_read(di->rk818, RELAX_VOL2_REGH, &buf, 1);
948         temp |= (buf<<8);
949
950         voltage_now = di->voltage_k*temp/1000 + di->voltage_b;
951
952         return voltage_now;
953 }
954
955 static int  _get_raw_adc_current(struct battery_info *di)
956 {
957         u8 buf;
958         int ret;
959         int current_now;
960
961         ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
962         if (ret < 0) {
963                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
964                 return ret;
965         }
966         current_now = buf;
967         ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
968         if (ret < 0) {
969                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
970                 return ret;
971         }
972         current_now |= (buf<<8);
973
974         if (ret < 0) {
975                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
976                 return ret;
977         }
978
979         return current_now;
980 }
981
982 static void reset_zero_var(struct battery_info *di)
983 {
984         di->update_k = 0;
985         di->q_err = 0;
986         di->voltage_old = 0;
987         di->display_soc = 0;
988 }
989
990 static void ioffset_sample_time(struct battery_info *di, int time)
991 {
992         u8 ggcon;
993
994         battery_read(di->rk818, GGCON, &ggcon, 1);
995         ggcon &= ~(0x30); /*clear <5:4>*/
996         ggcon |= time;
997         battery_write(di->rk818, GGCON, &ggcon, 1);
998 }
999
1000 static void update_cal_offset(struct battery_info *di)
1001 {
1002         int mod = di->queue_work_cnt % TIME_10MIN_SEC;
1003         u8 pcb_offset;
1004
1005         battery_read(di->rk818, PCB_IOFFSET_REG, &pcb_offset, 1);
1006         DBG("<%s>, queue_work_cnt = %lu, mod = %d\n",
1007             __func__, di->queue_work_cnt, mod);
1008         if ((!mod) && (di->pcb_ioffset_updated)) {
1009                 _set_cal_offset(di, _get_ioffset(di)+pcb_offset);
1010                 DBG("<%s>. 10min update cal_offset = %d",
1011                     __func__, di->pcb_ioffset+_get_ioffset(di));
1012         }
1013 }
1014
1015 /*
1016  * when charger finish signal comes, we need calibrate the current, make it
1017  * close to 0.
1018  */
1019 static void zero_current_calib(struct battery_info *di)
1020 {
1021         int adc_value;
1022         uint16_t C0;
1023         uint16_t C1;
1024         int ioffset;
1025         u8 pcb_offset;
1026         u8 retry = 0;
1027
1028         if ((di->charge_status == CHARGE_FINISH) &&
1029             (abs32_int(di->current_avg) > 4)) {
1030                 for (retry = 0; retry < 5; retry++) {
1031                         adc_value = _get_raw_adc_current(di);
1032                         if (adc_value > 2047)
1033                                 adc_value -= 4096;
1034
1035                         DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1036                         C0 = _get_cal_offset(di);
1037                         C1 = adc_value + C0;
1038                         DBG("<%s>. C0(cal_offset) = %d, C1 = %d\n",
1039                             __func__, C0, C1);
1040                         _set_cal_offset(di, C1);
1041                         DBG("<%s>. new cal_offset = %d\n",
1042                             __func__, _get_cal_offset(di));
1043                         msleep(2000);
1044
1045                         adc_value = _get_raw_adc_current(di);
1046                         DBG("<%s>. adc_value = %d\n", __func__, adc_value);
1047                         if (adc_value < 4) {
1048                                 if (_get_cal_offset(di) < 0x7ff)
1049                                         _set_cal_offset(di, di->current_offset+
1050                                                         42);
1051                                 else {
1052                                         ioffset = _get_ioffset(di);
1053                                         pcb_offset = C1 - ioffset;
1054                                         di->pcb_ioffset = pcb_offset;
1055                                         di->pcb_ioffset_updated  = true;
1056                                         battery_write(di->rk818,
1057                                                       PCB_IOFFSET_REG,
1058                                                       &pcb_offset, 1);
1059                                 }
1060                                 DBG("<%s>. update the cal_offset, C1 = %d\n"
1061                                     "i_offset = %d, pcb_offset = %d\n",
1062                                         __func__, C1, ioffset, pcb_offset);
1063                                 break;
1064                         } else {
1065                                 di->pcb_ioffset_updated  = false;
1066                         }
1067                 }
1068         }
1069 }
1070
1071
1072 static bool  _is_relax_mode(struct battery_info *di)
1073 {
1074         int ret;
1075         u8 status;
1076
1077         ret = battery_read(di->rk818, GGSTS, &status, 1);
1078
1079         if ((!(status&RELAX_VOL1_UPD)) || (!(status&RELAX_VOL2_UPD)))
1080                 return false;
1081         else
1082                 return true;
1083 }
1084
1085 static uint16_t get_relax_voltage(struct battery_info *di)
1086 {
1087         int ret;
1088         u8 status;
1089         uint16_t relax_vol1, relax_vol2;
1090         u8 ggcon;
1091
1092         ret = battery_read(di->rk818, GGSTS, &status, 1);
1093         ret = battery_read(di->rk818, GGCON, &ggcon, 1);
1094
1095         relax_vol1 = _get_relax_vol1(di);
1096         relax_vol2 = _get_relax_vol2(di);
1097         DBG("<%s>. GGSTS=0x%x, GGCON=0x%x, relax_vol1=%d, relax_vol2=%d\n",
1098             __func__, status, ggcon, relax_vol1, relax_vol2);
1099
1100         if (_is_relax_mode(di))
1101                 return relax_vol1 > relax_vol2 ? relax_vol1 : relax_vol2;
1102         else
1103                 return 0;
1104 }
1105
1106 static void  _set_relax_thres(struct battery_info *di)
1107 {
1108         u8 buf;
1109         int enter_thres, exit_thres;
1110         struct cell_state *cell = &di->cell;
1111
1112         enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
1113         exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
1114         DBG("<%s>. sleep_enter_current = %d, sleep_exit_current = %d\n",
1115             __func__, cell->config->ocv->sleep_enter_current,
1116         cell->config->ocv->sleep_exit_current);
1117
1118         buf  = enter_thres&0xff;
1119         battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf, 1);
1120         buf = (enter_thres>>8)&0xff;
1121         battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf, 1);
1122
1123         buf  = exit_thres&0xff;
1124         battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf, 1);
1125         buf = (exit_thres>>8)&0xff;
1126         battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf, 1);
1127
1128         /* set sample time */
1129         battery_read(di->rk818, GGCON, &buf, 1);
1130         buf &= ~(3<<2);/*8min*/
1131         buf &= ~0x01; /* clear bat_res calc*/
1132         battery_write(di->rk818, GGCON, &buf, 1);
1133 }
1134
1135 static void restart_relax(struct battery_info *di)
1136 {
1137         u8 ggcon;/* chrg_ctrl_reg2;*/
1138         u8 ggsts;
1139
1140         battery_read(di->rk818, GGCON, &ggcon, 1);
1141         ggcon &= ~0x0c;
1142         battery_write(di->rk818, GGCON, &ggcon, 1);
1143
1144         battery_read(di->rk818, GGSTS, &ggsts, 1);
1145         ggsts &= ~0x0c;
1146         battery_write(di->rk818, GGSTS, &ggsts, 1);
1147 }
1148
1149 static int  _get_average_current(struct battery_info *di)
1150 {
1151         u8  buf;
1152         int ret;
1153         int current_now;
1154         int temp;
1155         int val[3];
1156         int i;
1157
1158         for (i = 0; i < 3; i++) {
1159                 ret = battery_read(di->rk818, BAT_CUR_AVG_REGL, &buf, 1);
1160                 if (ret < 0) {
1161                         dev_err(di->dev, "error read BAT_CUR_AVG_REGL");
1162                         return ret;
1163                 }
1164                 val[i] = buf;
1165
1166                 ret = battery_read(di->rk818, BAT_CUR_AVG_REGH, &buf, 1);
1167                 if (ret < 0) {
1168                         dev_err(di->dev, "error read BAT_CUR_AVG_REGH");
1169                         return ret;
1170                 }
1171                 val[i] |= (buf<<8);
1172         }
1173         /*check value*/
1174         if (val[0] == val[1])
1175                 current_now = val[0];
1176         else
1177                 current_now = val[2];
1178
1179         if (current_now & 0x800)
1180                 current_now -= 4096;
1181
1182         temp = current_now*1506/1000;/*1000*90/14/4096*500/521;*/
1183
1184         return temp;
1185 }
1186
1187 static int is_rk81x_bat_exist(struct  battery_info *di)
1188 {
1189         u8 buf;
1190
1191         battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1192         return (buf & 0x80) ? 1 : 0;
1193 }
1194
1195 static bool _is_first_poweron(struct  battery_info *di)
1196 {
1197         u8 buf;
1198         u8 temp;
1199
1200         battery_read(di->rk818, GGSTS, &buf, 1);
1201         DBG("%s GGSTS value is 0x%2x\n", __func__, buf);
1202         /*di->pwron_bat_con = buf;*/
1203         if (buf&BAT_CON) {
1204                 buf &= ~(BAT_CON);
1205                 do {
1206                         battery_write(di->rk818, GGSTS, &buf, 1);
1207                         battery_read(di->rk818, GGSTS, &temp, 1);
1208                 } while (temp&BAT_CON);
1209                 return true;
1210         }
1211
1212         return false;
1213 }
1214 static void flatzone_voltage_init(struct battery_info *di)
1215 {
1216         u32 *ocv_table;
1217         int ocv_size;
1218         int temp_table[21];
1219         int i, j;
1220
1221         ocv_table = di->platform_data->battery_ocv;
1222         ocv_size = di->platform_data->ocv_size;
1223
1224         for (j = 0; j < 21; j++)
1225                 temp_table[j] = 0;
1226
1227         j = 0;
1228         for (i = 1; i < ocv_size-1; i++) {
1229                 if (ocv_table[i+1] < ocv_table[i] + 20)
1230                         temp_table[j++] = i;
1231         }
1232
1233         temp_table[j] = temp_table[j-1]+1;
1234         i = temp_table[0];
1235         di->enter_flatzone = ocv_table[i];
1236         j = 0;
1237
1238
1239         for (i = 0; i < 20; i++) {
1240                 if (temp_table[i] < temp_table[i+1])
1241                         j = i+1;
1242         }
1243
1244         i = temp_table[j];
1245         di->exit_flatzone = ocv_table[i];
1246
1247         DBG("enter_flatzone = %d exit_flatzone = %d\n",
1248             di->enter_flatzone, di->exit_flatzone);
1249 }
1250
1251 static void power_on_save(struct   battery_info *di, int ocv_voltage)
1252 {
1253         u8 ocv_valid, first_pwron;
1254         u8 save_soc;
1255         u8 ocv_soc;
1256
1257         /*buf==1: OCV_VOL is valid*/
1258         ocv_valid = rk81x_read_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1259         first_pwron = rk81x_read_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1260         DBG("readbit: ocv_valid=%d, first_pwron=%d\n", ocv_valid, first_pwron);
1261
1262         if (first_pwron == 1 || ocv_valid == 1) {
1263                 DBG("<%s> enter.\n", __func__);
1264                 ocv_soc = _voltage_to_capacity(di, ocv_voltage);
1265                 if (ocv_soc < 20) {
1266                         di->dod0_voltage = ocv_voltage;
1267                         di->dod0_capacity = di->nac;
1268                         di->dod0_status = 1;
1269                         di->dod0 = ocv_soc;
1270                         di->dod0_level = 80;
1271
1272                         if (ocv_soc <= 0)
1273                                 di->dod0_level = 100;
1274                         else if (ocv_soc < 5)
1275                                 di->dod0_level = 95;
1276                         else if (ocv_soc < 10)
1277                                 di->dod0_level = 90;
1278                         /* save_soc = di->dod0_level; */
1279                         save_soc = get_level(di);
1280                         if (save_soc <  di->dod0_level)
1281                                 save_soc = di->dod0_level;
1282                         save_level(di, save_soc);
1283                         DBG("<%s>: dod0_vol:%d, dod0_cap:%d, dod0:%d, level:%d",
1284                             __func__, di->dod0_voltage, di->dod0_capacity,
1285                             ocv_soc, save_soc);
1286                 }
1287         }
1288 }
1289
1290
1291 static int _get_soc(struct   battery_info *di)
1292 {
1293         return di->remain_capacity * 100 / div(di->fcc);
1294 }
1295
1296 static enum power_supply_property rk_battery_props[] = {
1297         POWER_SUPPLY_PROP_STATUS,
1298         POWER_SUPPLY_PROP_CURRENT_NOW,
1299         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1300         POWER_SUPPLY_PROP_PRESENT,
1301         POWER_SUPPLY_PROP_HEALTH,
1302         POWER_SUPPLY_PROP_CAPACITY,
1303 };
1304
1305 #define to_device_info(x) container_of((x), \
1306                                 struct battery_info, bat)
1307
1308 static int rk81x_battery_get_property(struct power_supply *psy,
1309                                       enum power_supply_property psp,
1310                                       union power_supply_propval *val)
1311 {
1312         struct battery_info *di = to_device_info(psy);
1313
1314         switch (psp) {
1315         case POWER_SUPPLY_PROP_CURRENT_NOW:
1316                 val->intval = di->current_avg*1000;/*uA*/
1317                 if (di->fg_drv_mode == TEST_POWER_MODE)
1318                         val->intval = TEST_CURRENT*1000;
1319                 break;
1320
1321         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1322                 val->intval = di->voltage*1000;/*uV*/
1323                 if (di->fg_drv_mode == TEST_POWER_MODE)
1324                         val->intval = TEST_VOLTAGE*1000;
1325
1326                 break;
1327
1328         case POWER_SUPPLY_PROP_PRESENT:
1329                 val->intval = is_rk81x_bat_exist(di);
1330                 if (di->fg_drv_mode == TEST_POWER_MODE)
1331                         val->intval = TEST_PRESET;
1332
1333                 break;
1334
1335         case POWER_SUPPLY_PROP_CAPACITY:
1336                 val->intval = di->real_soc;
1337                 if (di->fg_drv_mode == TEST_POWER_MODE)
1338                         val->intval = TEST_SOC;
1339
1340                 DBG("<%s>, report dsoc: %d\n", __func__, val->intval);
1341                 break;
1342
1343         case POWER_SUPPLY_PROP_HEALTH:
1344                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1345                 break;
1346
1347         case POWER_SUPPLY_PROP_STATUS:
1348                 val->intval = di->status;
1349                 if (di->fg_drv_mode == TEST_POWER_MODE)
1350                         val->intval = TEST_STATUS;
1351
1352                 break;
1353
1354         default:
1355                 return -EINVAL;
1356         }
1357
1358         return 0;
1359 }
1360
1361
1362 static enum power_supply_property rk_battery_ac_props[] = {
1363         POWER_SUPPLY_PROP_ONLINE,
1364 };
1365 static enum power_supply_property rk_battery_usb_props[] = {
1366         POWER_SUPPLY_PROP_ONLINE,
1367 };
1368
1369
1370 #define to_ac_device_info(x) container_of((x), \
1371                                 struct battery_info, ac)
1372
1373 static int rk81x_battery_ac_get_property(struct power_supply *psy,
1374                                          enum power_supply_property psp,
1375                                          union power_supply_propval *val)
1376 {
1377         int ret = 0;
1378         struct battery_info *di = to_ac_device_info(psy);
1379
1380         switch (psp) {
1381         case POWER_SUPPLY_PROP_ONLINE:
1382                 val->intval = di->ac_online;    /*discharging*/
1383                 if (di->fg_drv_mode == TEST_POWER_MODE)
1384                         val->intval = TEST_AC_ONLINE;
1385
1386                 break;
1387
1388         default:
1389                 ret = -EINVAL;
1390                 break;
1391         }
1392         return ret;
1393 }
1394
1395 #define to_usb_device_info(x) container_of((x), \
1396                                 struct battery_info, usb)
1397
1398 static int rk81x_battery_usb_get_property(struct power_supply *psy,
1399                                           enum power_supply_property psp,
1400                                           union power_supply_propval *val)
1401 {
1402         int ret = 0;
1403         struct battery_info *di = to_usb_device_info(psy);
1404
1405         switch (psp) {
1406         case POWER_SUPPLY_PROP_ONLINE:
1407                 if ((strstr(saved_command_line, "charger") == NULL) &&
1408                     (di->real_soc == 0) && (di->work_on == 1))
1409                         val->intval = 0;
1410                 else
1411                         val->intval = di->usb_online;
1412
1413                 if (di->fg_drv_mode == TEST_POWER_MODE)
1414                         val->intval = TEST_USB_ONLINE;
1415                 break;
1416
1417         default:
1418                 ret = -EINVAL;
1419                 break;
1420         }
1421
1422         return ret;
1423 }
1424
1425
1426 static void battery_power_supply_init(struct battery_info *di)
1427 {
1428         di->bat.name = "BATTERY";
1429         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1430         di->bat.properties = rk_battery_props;
1431         di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1432         di->bat.get_property = rk81x_battery_get_property;
1433
1434         di->ac.name = "AC";
1435         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1436         di->ac.properties = rk_battery_ac_props;
1437         di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1438         di->ac.get_property = rk81x_battery_ac_get_property;
1439
1440         di->usb.name = "USB";
1441         di->usb.type = POWER_SUPPLY_TYPE_USB;
1442         di->usb.properties = rk_battery_usb_props;
1443         di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1444         di->usb.get_property = rk81x_battery_usb_get_property;
1445 }
1446
1447 static int battery_power_supply_register(struct battery_info *di)
1448 {
1449         int ret;
1450         struct device *dev = di->dev;
1451
1452         ret = power_supply_register(dev, &di->bat);
1453         if (ret) {
1454                 dev_err(dev, "failed to register main battery\n");
1455                 goto batt_failed;
1456         }
1457         ret = power_supply_register(dev, &di->usb);
1458         if (ret) {
1459                 dev_err(dev, "failed to register usb power supply\n");
1460                 goto usb_failed;
1461         }
1462         ret = power_supply_register(dev, &di->ac);
1463         if (ret) {
1464                 dev_err(dev, "failed to register ac power supply\n");
1465                 goto ac_failed;
1466         }
1467
1468         return 0;
1469
1470 ac_failed:
1471         power_supply_unregister(&di->ac);
1472 usb_failed:
1473         power_supply_unregister(&di->usb);
1474 batt_failed:
1475         power_supply_unregister(&di->bat);
1476
1477         return ret;
1478 }
1479
1480 static void  _capacity_init(struct battery_info *di, u32 capacity)
1481 {
1482         u8 buf;
1483         u32 capacity_ma;
1484         int delta_cap;
1485
1486         delta_cap = capacity - di->remain_capacity;
1487         di->adjust_cap += delta_cap;
1488
1489         reset_zero_var(di);
1490
1491         capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1492         do {
1493                 buf = (capacity_ma>>24)&0xff;
1494                 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1495                 buf = (capacity_ma>>16)&0xff;
1496                 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1497                 buf = (capacity_ma>>8)&0xff;
1498                 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1499                 buf = (capacity_ma&0xff) | 0x01;
1500                 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1501                 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1502
1503         } while (buf == 0);
1504 }
1505
1506
1507 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
1508 {
1509         u8 buf;
1510         u32 capacity_ma;
1511
1512         if (capacity >= di->qmax)
1513                 capacity = di->qmax;
1514
1515         if (capacity <= 0)
1516                 capacity = 0;
1517
1518         capacity_ma = capacity;
1519
1520         buf = (capacity_ma>>24)&0xff;
1521         battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1522         buf = (capacity_ma>>16)&0xff;
1523         battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1524         buf = (capacity_ma>>8)&0xff;
1525         battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1526         buf = (capacity_ma&0xff) | 0x01;
1527         battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1528 }
1529
1530 static int _get_remain_capacity(struct battery_info *di)
1531 {
1532         int ret;
1533         u8 buf;
1534         u32 capacity;
1535         int i;
1536         int val[3];
1537
1538         for (i = 0; i < 3; i++) {
1539                 ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1540                 val[i] = buf << 24;
1541                 ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1542                 val[i] |= buf << 16;
1543                 ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1544                 val[i] |= buf << 8;
1545                 ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1546                 val[i] |= buf;
1547         }
1548
1549         if (val[0] == val[1])
1550                 capacity = val[0];
1551         else
1552                 capacity = val[2];
1553
1554         return capacity;
1555 }
1556
1557
1558 static void  _save_FCC_capacity(struct battery_info *di, u32 capacity)
1559 {
1560         u8 buf;
1561         u32 capacity_ma;
1562
1563         capacity_ma = capacity;
1564         buf = (capacity_ma>>24)&0xff;
1565         battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1566         buf = (capacity_ma>>16)&0xff;
1567         battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1568         buf = (capacity_ma>>8)&0xff;
1569         battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1570         buf = (capacity_ma&0xff) | 0x01;
1571         battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1572 }
1573
1574 static int _get_FCC_capacity(struct battery_info *di)
1575 {
1576         int ret;
1577         int temp = 0;
1578         u8 buf;
1579         u32 capacity;
1580
1581         ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1582         temp = buf << 24;
1583         ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1584         temp |= buf << 16;
1585         ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1586         temp |= buf << 8;
1587         ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1588         temp |= buf;
1589
1590         if (temp > 1)
1591                 capacity = temp-1;/* 4096*900/14/36*500/521 */
1592         else
1593                 capacity = temp;
1594         DBG("%s NEW_FCC_REG %d  capacity = %d\n", __func__, temp, capacity);
1595
1596         return capacity;
1597 }
1598
1599 static int _get_realtime_capacity(struct battery_info *di)
1600 {
1601         int ret;
1602         int temp = 0;
1603         u8 buf;
1604         u32 capacity;
1605         int i;
1606         int val[3];
1607
1608         for (i = 0; i < 3; i++) {
1609                 ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1610                 val[i] = buf << 24;
1611                 ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1612                 val[i] |= buf << 16;
1613                 ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1614                 val[i] |= buf << 8;
1615                 ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1616                 val[i] |= buf;
1617         }
1618         if (val[0] == val[1])
1619                 temp = val[0];
1620         else
1621                 temp = val[2];
1622
1623         capacity = temp/2390;/* 4096*900/14/36*500/521; */
1624
1625         return capacity;
1626 }
1627
1628 static int _copy_soc(struct  battery_info *di, u8 save_soc)
1629 {
1630         u8 soc;
1631
1632         soc = save_soc;
1633         battery_write(di->rk818, SOC_REG, &soc, 1);
1634         return 0;
1635 }
1636
1637 static int copy_reboot_cnt(struct  battery_info *di, u8 save_cnt)
1638 {
1639         u8 cnt;
1640
1641         cnt = save_cnt;
1642         battery_write(di->rk818, REBOOT_CNT_REG, &cnt, 1);
1643         return 0;
1644 }
1645
1646 static bool support_uboot_charge(void)
1647 {
1648         return support_uboot_chrg ? true : false;
1649 }
1650
1651
1652 /*
1653 * There are three ways to detect dc_adp:
1654 *       1. hardware only support dc_adp: by reg VB_MOD_REG of rk818,
1655 *          do not care about whether define dc_det_pin or not;
1656 *       2. define de_det_pin: check gpio level;
1657 *       3. support usb_adp and dc_adp: by VB_MOD_REG and usb interface.
1658 *          case that: gpio invalid or not define.
1659 */
1660 static enum charger_type_t rk81x_get_dc_state(struct battery_info *di)
1661 {
1662         enum charger_type_t charger_type;
1663         u8 buf;
1664         int ret;
1665
1666         battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1667
1668         /*only HW_ADP_TYPE_DC: det by rk818 is easily and will be successful*/
1669          if (!rk81x_support_adp_type(HW_ADP_TYPE_USB)) {
1670                 if ((buf & PLUG_IN_STS) != 0)
1671                         charger_type = DC_CHARGER;
1672                 else
1673                         charger_type = NO_CHARGER;
1674
1675                 return charger_type;
1676          }
1677
1678 #if 1
1679         /*det by gpio level*/
1680         if (gpio_is_valid(di->dc_det_pin)) {
1681                 ret = gpio_request(di->dc_det_pin, "rk818_dc_det");
1682                 if (ret < 0) {
1683                         pr_err("Failed to request gpio %d with ret:""%d\n",
1684                                di->dc_det_pin, ret);
1685                         return NO_CHARGER;
1686                 }
1687
1688                 gpio_direction_input(di->dc_det_pin);
1689                 ret = gpio_get_value(di->dc_det_pin);
1690                 if (ret == di->dc_det_level)
1691                         charger_type = DC_CHARGER;
1692                 else
1693                         charger_type = NO_CHARGER;
1694
1695                 gpio_free(di->dc_det_pin);
1696                 DBG("**********rk818 dc_det_pin=%d\n", ret);
1697
1698                 return charger_type;
1699         }
1700 #endif
1701         /*HW_ADP_TYPE_DUAL: det by rk818 and usb*/
1702         else if (rk81x_support_adp_type(HW_ADP_TYPE_DUAL)) {
1703                 if ((buf & PLUG_IN_STS) != 0) {
1704                         charger_type = dwc_otg_check_dpdm();
1705                         if (charger_type == 0)
1706                                 charger_type = DC_CHARGER;
1707                         else
1708                                 charger_type = NO_CHARGER;
1709                 }
1710         }
1711
1712         return charger_type;
1713 }
1714
1715 static enum charger_type_t rk81x_get_usbac_state(struct battery_info *di)
1716 {
1717         enum charger_type_t charger_type;
1718         int usb_id, gadget_flag;
1719
1720         usb_id = dwc_otg_check_dpdm();
1721         switch (usb_id) {
1722         case 0:
1723                 charger_type = NO_CHARGER;
1724                 break;
1725         case 1:
1726         case 3:
1727                 charger_type = USB_CHARGER;
1728                 break;
1729         case 2:
1730                 charger_type = AC_CHARGER;
1731                 break;
1732         default:
1733                 charger_type = NO_CHARGER;
1734         }
1735
1736         DBG("<%s>. DWC_OTG = %d\n", __func__, usb_id);
1737         if (charger_type == USB_CHARGER) {
1738                 gadget_flag = get_gadget_connect_flag();
1739                 DBG("<%s>. gadget_flag=%d, check_cnt=%d\n",
1740                     __func__, gadget_flag, di->check_count);
1741
1742                 if (0 == gadget_flag) {
1743                         if (++di->check_count >= 5) {
1744                                 charger_type = AC_CHARGER;
1745                                 DBG("<%s>. turn to AC_CHARGER, check_cnt=%d\n",
1746                                     __func__, di->check_count);
1747                         } else {
1748                                 charger_type = USB_CHARGER;
1749                         }
1750                 } else {
1751                         charger_type = USB_CHARGER;
1752                 }
1753         } else {
1754                 di->check_count = 0;
1755         }
1756
1757         return charger_type;
1758 }
1759
1760 /*
1761  * it is first time for battery to be weld, init by ocv table
1762  */
1763 static void rsoc_first_poweron_init(struct battery_info *di)
1764 {
1765         _save_FCC_capacity(di, di->design_capacity);
1766         di->fcc = _get_FCC_capacity(di);
1767
1768         di->temp_soc = _voltage_to_capacity(di, di->voltage_ocv);
1769         di->real_soc = di->temp_soc;
1770         di->nac      = di->temp_nac;
1771         di->first_on_cap = di->nac;
1772
1773         rk81x_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1774         rk81x_set_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);/*save*/
1775         DBG("<%s>.this is first poweron: OCV-SOC:%d, OCV-CAP:%d, FCC:%d\n",
1776             __func__, di->real_soc, di->nac, di->fcc);
1777 }
1778
1779 /*
1780  * it is not first time for battery to be weld, init by last record info
1781  */
1782 static void rsoc_not_first_poweron_init(struct battery_info *di)
1783 {
1784         u8 pwron_soc;
1785         u8 init_soc;
1786         u8 last_shtd_time;
1787         u8 curr_shtd_time;
1788         int remain_capacity;
1789         int ocv_soc;
1790         enum charger_type_t charger_type;
1791
1792         rk81x_clr_bit(di, MISC_MARK_REG, FIRST_PWRON_SHIFT);
1793         battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1794         init_soc = pwron_soc;
1795         DBG("<%s> Not first pwron, SOC_REG = %d\n", __func__, pwron_soc);
1796
1797         if (rk81x_support_adp_type(HW_ADP_TYPE_USB)) {
1798                 charger_type = rk81x_get_usbac_state(di);
1799                 if ((pwron_soc == 0) && (charger_type == USB_CHARGER)) {
1800                         init_soc = 1;
1801                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1802                 }
1803         }
1804
1805         remain_capacity = _get_remain_capacity(di);
1806         /* check if support uboot charge,
1807          * if support, uboot charge driver should have done init work,
1808          * so here we should skip init work
1809          */
1810         if (support_uboot_charge())
1811                 goto out;
1812
1813         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG,
1814                      &curr_shtd_time, 1);
1815         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1816                      &last_shtd_time, 1);
1817         battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1818                       &curr_shtd_time, 1);
1819         DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n",
1820             __func__, curr_shtd_time, last_shtd_time, charger_type);
1821
1822         ocv_soc = _voltage_to_capacity(di, di->voltage_ocv);
1823         DBG("<%s>, Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n",
1824             __func__, remain_capacity, di->temp_nac);
1825
1826         /* if plugin, make sure current shtd_time diff from last_shtd_time.*/
1827         if (last_shtd_time != curr_shtd_time) {
1828                 if (curr_shtd_time > 30) {
1829                         rk81x_set_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1830
1831                         remain_capacity = di->temp_nac;
1832                         di->first_on_cap = remain_capacity;
1833                         DBG("<%s>pwroff > 30 minute, remain_cap = %d\n",
1834                             __func__, remain_capacity);
1835
1836                 } else if ((curr_shtd_time > 5) &&
1837                                 (abs32_int(ocv_soc - init_soc) >= 10)) {
1838                         if (remain_capacity >= di->temp_nac*120/100)
1839                                 remain_capacity = di->temp_nac*110/100;
1840                         else if (remain_capacity < di->temp_nac*8/10)
1841                                 remain_capacity = di->temp_nac*9/10;
1842                         DBG("<%s> pwroff > 3 minute, remain_cap = %d\n",
1843                             __func__, remain_capacity);
1844                 }
1845         } else {
1846                 rk81x_clr_bit(di, MISC_MARK_REG, OCV_VALID_SHIFT);
1847         }
1848 out:
1849         di->real_soc = init_soc;
1850         di->nac = remain_capacity;
1851         if (di->nac <= 0)
1852                 di->nac = 0;
1853         DBG("<%s> init_soc = %d, init_capacity=%d\n",
1854             __func__, di->real_soc, di->nac);
1855 }
1856
1857 static u8 get_sys_pwroff_min(struct battery_info *di)
1858 {
1859         u8 curr_shtd_time, last_shtd_time;
1860
1861         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG,
1862                      &curr_shtd_time, 1);
1863         battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE,
1864                      &last_shtd_time, 1);
1865
1866         return (curr_shtd_time != last_shtd_time) ? curr_shtd_time : 0;
1867 }
1868
1869 static int _rsoc_init(struct battery_info *di)
1870 {
1871         u8 pwroff_min;
1872         u8 calib_en;/*debug*/
1873
1874         di->voltage  = _get_battery_voltage(di);
1875         di->voltage_ocv = _get_OCV_voltage(di);
1876         pwroff_min = get_sys_pwroff_min(di);
1877
1878         DBG("OCV voltage=%d, voltage=%d, pwroff_min=%d\n",
1879             di->voltage_ocv, di->voltage, pwroff_min);
1880
1881         calib_en = rk81x_read_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
1882         DBG("readbit: calib_en=%d\n", calib_en);
1883         if (_is_first_poweron(di) ||
1884             ((pwroff_min >= 30) && (calib_en == 1))) {
1885                 rsoc_first_poweron_init(di);
1886                 rk81x_clr_bit(di, MISC_MARK_REG, OCV_CALIB_SHIFT);
1887
1888         } else {
1889                 rsoc_not_first_poweron_init(di);
1890         }
1891
1892         return 0;
1893 }
1894
1895
1896 static u8 rk81x_get_charge_status(struct battery_info *di)
1897 {
1898         u8 status;
1899         u8 ret = 0;
1900
1901         battery_read(di->rk818, SUP_STS_REG, &status, 1);
1902         status &= (0x70);
1903         switch (status) {
1904         case CHARGE_OFF:
1905                 ret = CHARGE_OFF;
1906                 DBG("  CHARGE-OFF ...\n");
1907                 break;
1908
1909         case DEAD_CHARGE:
1910                 ret = DEAD_CHARGE;
1911                 DBG("  DEAD CHARGE ...\n");
1912                 break;
1913
1914         case  TRICKLE_CHARGE:
1915                 ret = DEAD_CHARGE;
1916                 DBG("  TRICKLE CHARGE ...\n ");
1917                 break;
1918
1919         case  CC_OR_CV:
1920                 ret = CC_OR_CV;
1921                 DBG("  CC or CV ...\n");
1922                 break;
1923
1924         case  CHARGE_FINISH:
1925                 ret = CHARGE_FINISH;
1926                 DBG("  CHARGE FINISH ...\n");
1927                 break;
1928
1929         case  USB_OVER_VOL:
1930                 ret = USB_OVER_VOL;
1931                 DBG("  USB OVER VOL ...\n");
1932                 break;
1933
1934         case  BAT_TMP_ERR:
1935                 ret = BAT_TMP_ERR;
1936                 DBG("  BAT TMP ERROR ...\n");
1937                 break;
1938
1939         case  TIMER_ERR:
1940                 ret = TIMER_ERR;
1941                 DBG("  TIMER ERROR ...\n");
1942                 break;
1943
1944         case  USB_EXIST:
1945                 ret = USB_EXIST;
1946                 DBG("  USB EXIST ...\n");
1947                 break;
1948
1949         case  USB_EFF:
1950                 ret = USB_EFF;
1951                 DBG("  USB EFF...\n");
1952                 break;
1953
1954         default:
1955                 return -EINVAL;
1956         }
1957
1958         return ret;
1959 }
1960
1961 static void set_charge_current(struct battery_info *di, int charge_current)
1962 {
1963         u8 usb_ctrl_reg;
1964
1965         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1966         usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1967         usb_ctrl_reg |= (charge_current | CHRG_CT_EN);
1968         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1969 }
1970
1971 static void rk81x_fg_match_param(struct battery_info *di, int chg_vol,
1972                                  int chg_ilim, int chg_cur)
1973 {
1974         int i;
1975
1976         di->chg_v_lmt = DEF_CHRG_VOL;
1977         di->chg_i_lmt = DEF_CHRG_CURR_LMT;
1978         di->chg_i_cur = DEF_CHRG_CURR_SEL;
1979
1980         for (i = 0; i < ARRAY_SIZE(CHG_V_LMT); i++) {
1981                 if (chg_vol < CHG_V_LMT[i])
1982                         break;
1983                 else
1984                         di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1985         }
1986
1987         for (i = 0; i < ARRAY_SIZE(CHG_I_LMT); i++) {
1988                 if (chg_ilim < CHG_I_LMT[i])
1989                         break;
1990                 else
1991                         di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1992         }
1993
1994         for (i = 0; i < ARRAY_SIZE(CHG_I_CUR); i++) {
1995                 if (chg_cur < CHG_I_CUR[i])
1996                         break;
1997                 else
1998                         di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1999         }
2000         DBG("<%s>. vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
2001             __func__, di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
2002 }
2003
2004 static u8 rk81x_chose_finish_ma(int fcc)
2005 {
2006         u8 ma = FINISH_150MA;
2007
2008         if (fcc < 3000)
2009                 ma = FINISH_100MA;
2010
2011         else if (fcc >= 3000 && fcc <= 4000)
2012                 ma = FINISH_150MA;
2013
2014         else if (fcc > 4000 && fcc <= 5000)
2015                 ma = FINISH_200MA;
2016
2017         else/*fcc > 5000*/
2018                 ma = FINISH_250MA;
2019
2020         return ma;
2021 }
2022
2023 static void rk81x_battery_charger_init(struct  battery_info *di)
2024 {
2025         u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
2026         u8 sup_sts_reg, thremal_reg;
2027         int chg_vol, chg_cur, chg_ilim;
2028         u8 finish_ma;
2029
2030         chg_vol = di->rk818->battery_data->max_charger_voltagemV;
2031
2032         if (di->fg_drv_mode == TEST_POWER_MODE) {
2033                 chg_cur = di->test_chrg_current;
2034                 chg_ilim = di->test_chrg_ilmt;
2035         } else {
2036                 chg_cur = di->rk818->battery_data->max_charger_currentmA;
2037                 chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
2038         }
2039
2040         rk81x_fg_match_param(di, chg_vol, chg_ilim, chg_cur);
2041         finish_ma = rk81x_chose_finish_ma(di->fcc);
2042
2043         battery_read(di->rk818, THERMAL_REG, &thremal_reg, 1);
2044         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2045         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2046         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2047         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
2048         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2049
2050
2051         usb_ctrl_reg &= (~0x0f);
2052
2053         if (rk81x_support_adp_type(HW_ADP_TYPE_USB))
2054                 usb_ctrl_reg |= (CHRG_CT_EN | ILIM_450MA);/*en temp feed back*/
2055         else
2056                 usb_ctrl_reg |= (CHRG_CT_EN | di->chg_i_lmt);
2057
2058         thremal_reg &= (~0x0c);
2059         thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2060
2061         chrg_ctrl_reg1 &= (0x00);
2062         chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
2063
2064         chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
2065         chrg_ctrl_reg2 &= ~(0xc7);
2066         chrg_ctrl_reg2 |= finish_ma | CHG_CCCV_6HOUR;
2067
2068         sup_sts_reg &= ~(0x01 << 3);
2069         sup_sts_reg |= (0x01 << 2);
2070
2071         thremal_reg &= (~0x0c);
2072         thremal_reg |= TEMP_105C;/*temp feed back: 105c*/
2073
2074         battery_write(di->rk818, THERMAL_REG, &thremal_reg, 1);
2075         battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2076         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2077         battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2078         battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2079         battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
2080 }
2081
2082 void charge_disable_open_otg(int value)
2083 {
2084         struct  battery_info *di = g_battery;
2085
2086         if (value == 1) {
2087                 DBG("charge disable, enable OTG.\n");
2088                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
2089                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7);
2090         }
2091         if (value == 0) {
2092                 DBG("charge enable, disable OTG.\n");
2093                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7);
2094                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
2095         }
2096 }
2097
2098 static void rk81x_low_waring_init(struct battery_info *di)
2099 {
2100         u8 vb_mon_reg;
2101         u8 vb_mon_reg_init;
2102
2103         battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
2104
2105         /* 3.4v: interrupt*/
2106         vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06);
2107         battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
2108 }
2109
2110 static void rk81x_fg_init(struct battery_info *di)
2111 {
2112         u8 adc_ctrl_val;
2113         u8 buf = 0;
2114         u8 pcb_offset;
2115         int cal_offset;
2116
2117         adc_ctrl_val = 0x30;
2118         battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
2119
2120         _gauge_enable(di);
2121         /* get the volatege offset */
2122         _get_voltage_offset_value(di);
2123         rk81x_battery_charger_init(di);
2124         _set_relax_thres(di);
2125
2126         /* get the current offset , the value write to the CAL_OFFSET */
2127         di->current_offset = _get_ioffset(di);
2128         battery_read(di->rk818, PCB_IOFFSET_REG, &pcb_offset, 1);
2129         DBG("<%s>. pcb_offset = 0x%x\n", __func__, pcb_offset);
2130         DBG("<%s>. io_offset = 0x%x\n", __func__, di->current_offset);
2131
2132         _set_cal_offset(di, di->current_offset+pcb_offset);
2133         cal_offset = _get_cal_offset(di);
2134         if ((cal_offset < 0x7ff) || (pcb_offset == 0))
2135                 _set_cal_offset(di, di->current_offset+42);
2136
2137         _rsoc_init(di);
2138         _capacity_init(di, di->nac);
2139
2140         di->remain_capacity = _get_realtime_capacity(di);
2141         di->current_avg = _get_average_current(di);
2142
2143         rk81x_low_waring_init(di);
2144         restart_relax(di);
2145         power_on_save(di, di->voltage_ocv);
2146         battery_write(di->rk818, OCV_VOL_VALID_REG, &buf, 1);
2147
2148         /* set sample time for cal_offset interval*/
2149         ioffset_sample_time(di, SAMP_TIME_8MIN);
2150         dump_gauge_register(di);
2151         dump_charger_register(di);
2152
2153         DBG("<%s> :\n"
2154             "nac = %d , remain_capacity = %d\n"
2155             "OCV_voltage = %d, voltage = %d\n"
2156             "SOC = %d, fcc = %d\n, current=%d\n"
2157             "cal_offset = 0x%x\n",
2158             __func__,
2159             di->nac, di->remain_capacity,
2160             di->voltage_ocv, di->voltage,
2161             di->real_soc, di->fcc, di->current_avg,
2162             cal_offset);
2163 }
2164 /*
2165  * this is a very important algorithm to avoid over discharge.
2166  */
2167 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
2168 static void zero_get_soc(struct battery_info *di)
2169 {
2170         int dead_voltage, ocv_voltage;
2171         int temp_soc = -1, real_soc;
2172         int currentold, currentnow, voltage;
2173         int i;
2174         int voltage_k;
2175         int count_num = 0;
2176         int q_ocv;
2177         int ocv_soc;
2178
2179         DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
2180         do {
2181                 currentold = _get_average_current(di);
2182                 _get_cal_offset(di);
2183                 _get_ioffset(di);
2184                 msleep(100);
2185                 currentnow = _get_average_current(di);
2186                 count_num++;
2187         } while ((currentold == currentnow) && (count_num < 11));
2188
2189         voltage  = 0;
2190         for (i = 0; i < 10 ; i++)
2191                 voltage += _get_battery_voltage(di);
2192         voltage /= 10;
2193
2194         if (di->voltage_old == 0)
2195                 di->voltage_old = voltage;
2196         voltage_k = voltage;
2197         voltage = (di->voltage_old*2 + 8*voltage)/10;
2198         di->voltage_old = voltage;
2199         currentnow = _get_average_current(di);
2200
2201         dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
2202         /* 65 mo power-path mos */
2203         ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
2204         DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
2205             dead_voltage, ocv_voltage);
2206
2207         ocv_soc = _voltage_to_capacity(di, dead_voltage);
2208         di->q_dead = di->temp_nac;
2209         DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
2210             ocv_soc, di->q_dead);
2211
2212         ocv_soc = _voltage_to_capacity(di, ocv_voltage);
2213         q_ocv = di->temp_nac;
2214         DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
2215             ocv_soc, q_ocv);
2216
2217         /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
2218         di->q_err = di->remain_capacity - q_ocv;
2219         DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
2220             di->q_err, di->remain_capacity, q_ocv);
2221
2222         if (di->display_soc == 0)
2223                 di->display_soc = di->real_soc*1000;
2224         real_soc = di->display_soc;
2225
2226         DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
2227             di->remain_capacity, di->q_dead, di->q_err);
2228         /*[temp_nac]:dead_voltage*/
2229         if (q_ocv > di->q_dead) {
2230                 DBG("first: q_ocv > di->q_dead\n");
2231
2232                 /*initical K0*/
2233                 if ((di->update_k == 0) || (di->zero_cycle >= 500)) {
2234                         DBG("[K == 0]\n");
2235                         di->zero_cycle = 0;
2236                         di->update_k = 1;
2237                         /* ZQ = Q_ded +  Qerr */
2238                         /*[temp_nac]:dead_voltage*/
2239                         di->q_shtd = di->q_dead + di->q_err;
2240                         temp_soc = (di->remain_capacity - di->q_shtd)*
2241                                         1000/div(di->fcc);
2242                         if (temp_soc == 0)
2243                                 di->update_k = 0;
2244                         else
2245                                 di->line_k = (real_soc + temp_soc/2)
2246                                                 /div(temp_soc);
2247                 /* recalc K0*/
2248                 } else if (di->zero_updated && di->update_k >= 10) {
2249                         DBG("[K >= 10].\n");
2250                         di->update_k = 1;
2251                         _voltage_to_capacity(di, dead_voltage);
2252                         di->q_dead = di->temp_nac;
2253                         di->q_shtd = di->q_dead + di->q_err;
2254                         temp_soc = ((di->remain_capacity - di->q_shtd)*
2255                                 1000 + di->fcc/2)/div(di->fcc); /* z1 */
2256                         if (temp_soc == 0)
2257                                 di->update_k = 0;
2258                         else
2259                                 di->line_k = (real_soc + temp_soc/2)
2260                                                 /div(temp_soc);
2261
2262                         DBG("[K >= 10]. new:line_k = %d\n", di->line_k);
2263                         DBG("[K >= 10]. new:Y0(dis_soc)=%d\n", di->display_soc);
2264                         DBG("[K >= 10]. new:X0(temp) = %d\n", temp_soc);
2265
2266                 } else { /*update_k[1~9]*/
2267                         DBG("[K1~9]\n");
2268                         di->zero_cycle++;
2269                         di->update_k++;
2270                         DBG("[K1~9]. (old)Y0=%d, Y0=%d\n",
2271                             di->old_display_soc, di->display_soc);
2272                         if (di->update_k == 2)
2273                                 di->old_display_soc = di->display_soc;
2274
2275                         temp_soc = ((di->remain_capacity - di->q_shtd)*
2276                                 1000 + di->fcc/2)/div(di->fcc);
2277                         real_soc = di->line_k*temp_soc;
2278                         di->display_soc = real_soc;
2279
2280                         /* make sure display_soc change at least once*/
2281                         if (di->display_soc >= di->old_display_soc)
2282                                 di->zero_updated = false;
2283                         else
2284                                 di->zero_updated = true;
2285
2286                         DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
2287                         DBG("[K1~9]. line_k = %d\n", di->line_k);
2288                         DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
2289                             di->display_soc, di->real_soc);
2290
2291                         if ((di->display_soc+500)/1000 < di->real_soc) {
2292                                 /*special for 0%*/
2293                                 if ((di->real_soc == 1) &&
2294                                     (di->display_soc < 100))
2295                                         di->real_soc--;
2296                                 else
2297                                         di->real_soc--;
2298                                 /*di->odd_capacity = 0;*/
2299                         }
2300                 }
2301         } else {
2302                 DBG("second: q_ocv < di->q_dead\n");
2303                 di->update_k++;
2304
2305                 if (di->voltage < 3400) {
2306                         DBG("second: voltage < 3400\n");
2307                         di->real_soc--;
2308                 } else {
2309                         if (di->update_k > 10) {
2310                                 di->update_k = 0;
2311                                 di->real_soc--;
2312                                 di->odd_capacity = 0;
2313                         }
2314                 }
2315         }
2316
2317         if (di->line_k <= 0) {
2318                 reset_zero_var(di);
2319                 DBG("ZERO: line_k <= 0, Update line_k!\n");
2320         }
2321
2322         DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
2323         DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
2324         DBG("ZERO: remain_cap - q_shtd=%d\n",
2325             (di->remain_capacity - di->q_shtd));
2326         DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
2327             di->line_k, di->display_soc, temp_soc);
2328         DBG("ZERO: zero_cycle=%d,(old)Y0=%d, zero_updated=%d, update_k=%d\n",
2329             di->zero_cycle, di->old_display_soc,
2330             di->zero_updated, di->update_k);
2331
2332         DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
2333             di->remain_capacity, di->q_shtd, di->q_err);
2334         DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
2335             di->warnning_voltage, _get_soc(di), di->real_soc);
2336 }
2337
2338
2339 static int estimate_bat_ocv_vol(struct battery_info *di)
2340 {
2341         return (di->voltage -
2342                                 (di->bat_res * di->current_avg) / 1000);
2343 }
2344
2345 static int estimate_bat_ocv_soc(struct battery_info *di)
2346 {
2347         int ocv_soc, ocv_voltage;
2348
2349         ocv_voltage = estimate_bat_ocv_vol(di);
2350         ocv_soc = _voltage_to_capacity(di, ocv_voltage);
2351
2352         return ocv_soc;
2353 }
2354
2355 /* we will estimate a ocv voltage to get a ocv soc.
2356  * if there is a big offset between ocv_soc and rsoc,
2357  * we will decide whether we should reinit capacity or not
2358  */
2359 static void rsoc_dischrg_calib(struct battery_info *di)
2360 {
2361         int ocv_soc = di->est_ocv_soc;
2362         int ocv_volt = di->est_ocv_vol;
2363         int temp_soc = _get_soc(di);
2364         int max_volt = di->rk818->battery_data->max_charger_voltagemV;
2365
2366         if (ocv_volt > max_volt)
2367                 goto out;
2368
2369         if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
2370                 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
2371                     (di->temp_soc == 0) ||
2372                     (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
2373                         di->err_chck_cnt++;
2374                         di->err_soc_sum += ocv_soc;
2375                 } else {
2376                         goto out;
2377                 }
2378                 DBG("<%s>. rsoc err_chck_cnt = %d\n",
2379                     __func__, di->err_chck_cnt);
2380                 DBG("<%s>. rsoc err_soc_sum = %d\n",
2381                     __func__, di->err_soc_sum);
2382
2383                 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2384                         ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2385                         if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
2386                                 ocv_soc += RSOC_COMPS;
2387
2388                         di->temp_nac = ocv_soc * di->fcc / 100;
2389                         _capacity_init(di, di->temp_nac);
2390                         di->temp_soc = _get_soc(di);
2391                         di->remain_capacity = _get_realtime_capacity(di);
2392                         di->err_soc_sum = 0;
2393                         di->err_chck_cnt = 0;
2394                         DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2395                 }
2396          } else {
2397 out:
2398                 di->err_chck_cnt = 0;
2399                 di->err_soc_sum = 0;
2400         }
2401 }
2402
2403 static void rsoc_realtime_calib(struct battery_info *di)
2404 {
2405         u8 status = di->status;
2406
2407         if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2408             (status == POWER_SUPPLY_STATUS_FULL)) {
2409                 if ((di->current_avg < -10) &&
2410                     (di->charge_status != CHARGE_FINISH))
2411                         rsoc_dischrg_calib(di);
2412                 /*
2413                 else
2414                         rsoc_chrg_calib(di);
2415                 */
2416
2417         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2418                 rsoc_dischrg_calib(di);
2419         }
2420 }
2421
2422 /*
2423  * when there is a big offset between dsoc and rsoc, dsoc needs to
2424  * speed up to keep pace witch rsoc.
2425  */
2426 static bool do_ac_charger_emulator(struct battery_info *di)
2427 {
2428         int delta_soc = di->temp_soc - di->real_soc;
2429         u32 soc_time;
2430
2431         if ((di->charge_status != CHARGE_FINISH) &&
2432             (di->ac_online == ONLINE) &&
2433             (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)) {
2434                 if (di->current_avg < DSOC_CHRG_EMU_CURR)
2435                         soc_time = di->fcc*3600/100/
2436                                         (abs_int(DSOC_CHRG_EMU_CURR));
2437                 else
2438                         soc_time = di->fcc*3600/100/
2439                                         div(abs_int(di->current_avg));
2440                 di->emu_chg_cnt++;
2441                 if  (di->emu_chg_cnt > soc_time) {
2442                         di->real_soc++;
2443                         di->emu_chg_cnt = 0;
2444                 }
2445                 DBG("<%s>. soc_time=%d, emu_cnt=%d\n",
2446                     __func__, soc_time, di->emu_chg_cnt);
2447
2448                 return true;
2449         }
2450
2451         return false;
2452 }
2453
2454 /* check voltage and current when dsoc is close to full.
2455  * we will do a fake charge to adjust charing speed which
2456  * aims to make battery full charged and match finish signal.
2457  */
2458 static bool do_term_chrg_calib(struct battery_info *di)
2459 {
2460         u32 soc_time;
2461         u32 *ocv_table = di->platform_data->battery_ocv;
2462
2463         /*check current and voltage*/
2464         if ((di->ac_online == ONLINE && di->real_soc >= 90) &&
2465             ((di->current_avg > DSOC_CHG_TERM_CURR) ||
2466              (di->voltage < ocv_table[18]+20))) {
2467                 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2468                 di->term_chg_cnt++;
2469                 if  (di->term_chg_cnt > soc_time) {
2470                         di->real_soc++;
2471                         di->term_chg_cnt = 0;
2472                 }
2473                 DBG("<%s>. soc_time=%d, term_cnt=%d\n",
2474                     __func__, soc_time, di->term_chg_cnt);
2475
2476                 return true;
2477         }
2478
2479         return false;
2480 }
2481
2482 static void normal_discharge(struct battery_info *di)
2483 {
2484         int soc_time = 0;
2485         int now_current = di->current_avg;
2486         int delta_soc = di->real_soc - di->temp_soc;
2487
2488         if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE) {
2489                 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2490                 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2491                     __func__, delta_soc);
2492         } else {
2493                 soc_time = di->fcc*3600/100/div(abs_int(now_current));
2494         }
2495
2496         if (di->temp_soc == di->real_soc) {
2497                 DBG("<%s>. temp_soc == real_soc\n", __func__);
2498
2499         } else if (di->temp_soc > di->real_soc) {
2500                 DBG("<%s>. temp_soc > real_soc\n", __func__);
2501                 di->vol_smooth_time++;
2502                 if (di->vol_smooth_time > soc_time*3/2) {
2503                         di->real_soc--;
2504                         di->vol_smooth_time = 0;
2505                 }
2506
2507         } else {
2508                 DBG("<%s>. temp_soc < real_soc\n", __func__);
2509                 if (di->real_soc == (di->temp_soc + 1)) {
2510                         di->change_timer = di->soc_timer;
2511                         di->real_soc = di->temp_soc;
2512                 } else {
2513                         di->vol_smooth_time++;
2514                         if (di->vol_smooth_time > soc_time*3/4) {
2515                                 di->real_soc--;
2516                                 di->vol_smooth_time  = 0;
2517                         }
2518                 }
2519         }
2520         reset_zero_var(di);
2521         DBG("<%s>, temp_soc = %d, real_soc = %d\n",
2522             __func__, di->temp_soc, di->real_soc);
2523         DBG("<%s>, vol_smooth_time = %d, soc_time = %d\n",
2524             __func__, di->vol_smooth_time, soc_time);
2525 }
2526
2527 static void rk81x_battery_discharge_smooth(struct battery_info *di)
2528 {
2529         int ocv_soc;
2530
2531         ocv_soc = _voltage_to_capacity(di, 3800);
2532         di->temp_soc = _get_soc(di);
2533
2534         DBG("<%s>. temp_soc = %d, real_soc = %d\n",
2535             __func__, di->temp_soc, di->real_soc);
2536
2537         if (di->voltage < 3800)
2538
2539                 zero_get_soc(di);
2540         else
2541                 normal_discharge(di);
2542 }
2543
2544 static int get_charging_time(struct battery_info *di)
2545 {
2546         return (di->charging_time/60);
2547 }
2548
2549 static int get_discharging_time(struct battery_info *di)
2550 {
2551         return (di->discharging_time/60);
2552 }
2553
2554 static int get_finish_time(struct battery_info *di)
2555 {
2556         return (di->finish_time/60);
2557 }
2558
2559 static void upd_time_table(struct battery_info *di);
2560 static void collect_debug_info(struct battery_info *di)
2561 {
2562         if ((di->ac_online == ONLINE) || (di->usb_online == ONLINE)) {
2563                 di->charging_time++;
2564                 di->discharging_time = 0;
2565         } else {
2566                 di->charging_time = 0;
2567                 if (di->voltage < 3800)
2568                         di->discharging_time += 2;
2569                 else
2570                         di->discharging_time++;
2571         }
2572         if (di->charge_status == CHARGE_FINISH)
2573                 di->finish_time++;
2574         else
2575                 di->finish_time = 0;
2576
2577         di->charge_min = get_charging_time(di);
2578         di->discharge_min = get_discharging_time(di);
2579         di->finish_min = get_finish_time(di);
2580
2581         upd_time_table(di);
2582 }
2583
2584 static void dump_debug_info(struct battery_info *di)
2585 {
2586         u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2587         u8 usb_ctrl_reg, chrg_ctrl_reg1, thremal_reg;
2588         u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val, misc_reg;
2589
2590         collect_debug_info(di);
2591
2592         battery_read(di->rk818, MISC_MARK_REG, &misc_reg, 1);
2593         battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2594         battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2595         battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2596         battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2597         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2598         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2599         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2600         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2601         battery_read(di->rk818, 0x00, &rtc_val, 1);
2602         battery_read(di->rk818, THERMAL_REG, &thremal_reg, 1);
2603
2604         DBG("\n------------- dump_debug_regs -----------------\n"
2605             "GGCON = 0x%2x, GGSTS = 0x%2x, RTC  = 0x%2x\n"
2606             "SUP_STS_REG  = 0x%2x, VB_MOD_REG   = 0x%2x\n"
2607             "USB_CTRL_REG  = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2608             "THERMAL_REG = 0x%2x, MISC_MARK_REG = 0x%x\n"
2609             "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2610             ggcon_reg, ggsts_reg, rtc_val,
2611             sup_tst_reg, vb_mod_reg,
2612             usb_ctrl_reg, chrg_ctrl_reg1,
2613             thremal_reg, misc_reg,
2614             chrg_ctrl_reg2, chrg_ctrl_reg3
2615            );
2616
2617         DBG(
2618             "########################## [read] 3.0############################\n"
2619             "--------------------------------------------------------------\n"
2620             "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2621             "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2622             "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2623             "diplay_soc = %d, cpapacity_soc = %d, test_mode = %d\n"
2624             "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2625             "finish_real_soc = %d, finish_temp_soc = %d\n"
2626             "i_offset=0x%x, cal_offset=0x%x, adjust_cap=%d\n"
2627             "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2628             get_relax_voltage(di),
2629             di->voltage, di->current_avg,
2630             di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2631             di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2632             di->real_soc, _get_soc(di), di->fg_drv_mode,
2633             di->ac_online, di->usb_online, di->status,
2634             di->debug_finish_real_soc, di->debug_finish_temp_soc,
2635             _get_ioffset(di), _get_cal_offset(di), di->adjust_cap,
2636             get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2637            );
2638         rk81x_get_charge_status(di);
2639         DBG("###########################################################\n");
2640 }
2641
2642 static void update_fcc_capacity(struct battery_info *di)
2643 {
2644         int fcc0;
2645         int remain_cap;
2646
2647         remain_cap = di->remain_capacity + di->adjust_cap - di->first_on_cap;
2648         DBG("%s: remain_cap:%d, ajust_cap:%d, first_on_cap=%d\n",
2649             __func__, remain_cap, di->adjust_cap, di->first_on_cap);
2650
2651         if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2652                 DBG("%s: dod0:%d, dod0_cap:%d, dod0_level:%d\n",
2653                     __func__, di->dod0, di->dod0_capacity, di->dod0_level);
2654
2655                 if (get_level(di) >= di->dod0_level) {
2656                         fcc0 = (remain_cap - di->dod0_capacity)*100
2657                                         /(100-di->dod0);
2658                         if (fcc0 > di->qmax)
2659                                 fcc0 = di->qmax;
2660
2661                         DBG("%s: fcc0:%d, fcc:%d\n", __func__, fcc0, di->fcc);
2662                         if ((fcc0 < di->fcc) && (fcc0 > 1000)) {
2663                                 di->fcc = fcc0;
2664                                 _capacity_init(di, di->fcc);
2665                                 _save_FCC_capacity(di, di->fcc);
2666                                 DBG("%s: new fcc0:%d\n", __func__, di->fcc);
2667                         }
2668                 }
2669                 di->dod0_status = 0;
2670         }
2671 }
2672
2673 static void debug_get_finish_soc(struct battery_info *di)
2674 {
2675         if (di->charge_status == CHARGE_FINISH) {
2676                 di->debug_finish_real_soc = di->real_soc;
2677                 di->debug_finish_temp_soc = di->temp_soc;
2678         }
2679 }
2680
2681 static void wait_charge_finish_signal(struct battery_info *di)
2682 {
2683         if ((di->charge_status == CHARGE_FINISH) &&
2684             (di->voltage > CHG_FINISH_VOL))
2685                 update_fcc_capacity(di);/* save new fcc*/
2686
2687         /* debug msg*/
2688         debug_get_finish_soc(di);
2689 }
2690
2691 static void charge_finish_routine(struct battery_info *di)
2692 {
2693         if ((di->charge_status == CHARGE_FINISH) &&
2694             (di->voltage > CHG_FINISH_VOL)) {
2695                 _capacity_init(di, di->fcc);
2696                 zero_current_calib(di);
2697
2698                 if (di->real_soc < 100) {
2699                         DBG("<%s>,CHARGE_FINISH:real_soc<100,real_soc=%d\n",
2700                             __func__, di->real_soc);
2701
2702                         if ((di->soc_counter < 80)) {
2703                                 di->soc_counter++;
2704                         } else {
2705                                 di->soc_counter = 0;
2706                                 di->real_soc++;
2707                         }
2708                 }
2709         }
2710 }
2711
2712 static void normal_charge(struct battery_info *di)
2713 {
2714         int now_current, soc_time;
2715
2716         now_current = _get_average_current(di);
2717         soc_time = di->fcc*3600/100/div(abs_int(now_current));   /* 1%  time; */
2718         di->temp_soc = _get_soc(di);
2719
2720         DBG("<%s>. temp_soc = %d, real_soc = %d\n",
2721             __func__, di->temp_soc, di->real_soc);
2722
2723         if (di->real_soc == di->temp_soc) {
2724                 DBG("<%s>. temp_soc == real_soc\n", __func__);
2725                     di->temp_soc = _get_soc(di);
2726         }
2727         if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2728                 if (di->temp_soc < di->real_soc + 1) {
2729                         DBG("<%s>. temp_soc < real_soc\n", __func__);
2730                         di->charge_smooth_time++;
2731                         if  (di->charge_smooth_time > soc_time*3/2) {
2732                                 di->real_soc++;
2733                                 di->charge_smooth_time  = 0;
2734                         }
2735                         di->charge_smooth_status = true;
2736                 }
2737
2738                 else if (di->temp_soc > di->real_soc + 1) {
2739                         DBG("<%s>. temp_soc > real_soc\n", __func__);
2740                         di->charge_smooth_time++;
2741                         if  (di->charge_smooth_time > soc_time*3/4) {
2742                                 di->real_soc++;
2743                                 di->charge_smooth_time  = 0;
2744                         }
2745                         di->charge_smooth_status = true;
2746
2747                 } else if (di->temp_soc == di->real_soc + 1) {
2748                         DBG("<%s>. temp_soc == real_soc + 1\n", __func__);
2749                         if (di->charge_smooth_status) {
2750                                 di->charge_smooth_time++;
2751                                 if (di->charge_smooth_time > soc_time*3/4) {
2752                                         di->real_soc = di->temp_soc;
2753                                         di->charge_smooth_time  = 0;
2754                                         di->charge_smooth_status = false;
2755                                 }
2756
2757                         } else {
2758                                 di->real_soc = di->temp_soc;
2759                                 di->charge_smooth_status = false;
2760                         }
2761                 }
2762         }
2763
2764         DBG("<%s>, temp_soc = %d, real_soc = %d\n",
2765             __func__, di->temp_soc, di->real_soc);
2766         DBG("<%s>, vol_smooth_time = %d, soc_time = %d\n",
2767             __func__, di->charge_smooth_time, soc_time);
2768 }
2769
2770
2771
2772 static void rk81x_battery_charge_smooth(struct battery_info *di)
2773 {
2774         reset_zero_var(di);
2775         /*calibrate: aim to match finish signal*/
2776         if (do_term_chrg_calib(di))
2777                 return;
2778
2779         /*calibrate: aim to calib error*/
2780         di->term_chg_cnt = 0;
2781         if (do_ac_charger_emulator(di))
2782                 return;
2783
2784         normal_charge(di);
2785 }
2786
2787 static void rk81x_battery_display_smooth(struct battery_info *di)
2788 {
2789         int status;
2790         u8  charge_status;
2791
2792         status = di->status;
2793         charge_status = di->charge_status;
2794         if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2795             (status == POWER_SUPPLY_STATUS_FULL)) {
2796                 if ((di->current_avg < -10) &&
2797                     (charge_status != CHARGE_FINISH))
2798                         rk81x_battery_discharge_smooth(di);
2799                 else
2800                         rk81x_battery_charge_smooth(di);
2801
2802         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2803                 rk81x_battery_discharge_smooth(di);
2804                 if (di->real_soc == 1) {
2805                         di->time2empty++;
2806                         if (di->time2empty >= 300)
2807                                 di->real_soc = 0;
2808                 } else {
2809                         di->time2empty = 0;
2810                 }
2811         }
2812 }
2813
2814 /*
2815  * update rsoc by relax voltage
2816  */
2817 static void resume_relax_calib(struct battery_info *di)
2818 {
2819         int relax_vol = di->relax_voltage;
2820         int ocv_soc, capacity;
2821
2822         ocv_soc = _voltage_to_capacity(di, relax_vol);
2823         capacity = (ocv_soc * di->fcc / 100);
2824         _capacity_init(di, capacity);
2825         di->remain_capacity = _get_realtime_capacity(di);
2826         di->temp_soc = _get_soc(di);
2827         DBG("%s, RSOC=%d, CAP=%d\n", __func__, ocv_soc, capacity);
2828 }
2829
2830 /* condition:
2831  * 1: must do it
2832  * 0: when neccessary
2833  */
2834 static void resume_vol_calib(struct battery_info *di, int condition)
2835 {
2836         int ocv_vol = di->est_ocv_vol;
2837         int ocv_soc = 0, capacity = 0;
2838
2839         ocv_soc = _voltage_to_capacity(di, ocv_vol);
2840         capacity = (ocv_soc * di->fcc / 100);
2841         if (condition || (abs(ocv_soc-di->temp_soc) >= RSOC_RESUME_ERR)) {
2842                 _capacity_init(di, capacity);
2843                 di->remain_capacity = _get_realtime_capacity(di);
2844                 di->temp_soc = _get_soc(di);
2845                 DBG("<%s>, rsoc updated!\n", __func__);
2846         }
2847         DBG("<%s>, OCV_VOL=%d,OCV_SOC=%d, CAP=%d\n",
2848             __func__, ocv_vol, ocv_soc, capacity);
2849 }
2850
2851 /*
2852  * when support HW_ADP_TYPE_DUAL, and at the moment that usb_adp
2853  * and dc_adp are plugined in together, the dc_apt has high priority.
2854  * so we check dc_apt first and return rigth away if it's found.
2855  */
2856 static enum charger_type_t rk81x_get_adp_type(struct battery_info *di)
2857 {
2858         u8 buf;
2859         enum charger_type_t charger_type = NO_CHARGER;
2860
2861         /*check by ic hardware: this check make check work safer*/
2862         battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2863         if ((buf & PLUG_IN_STS) == 0)
2864                 return NO_CHARGER;
2865
2866         /*check DC first*/
2867         if (rk81x_support_adp_type(HW_ADP_TYPE_DC)) {
2868                 charger_type = rk81x_get_dc_state(di);
2869                 if (charger_type == DC_CHARGER)
2870                         return charger_type;
2871         }
2872
2873         /*HW_ADP_TYPE_USB*/
2874         charger_type = rk81x_get_usbac_state(di);
2875
2876         return charger_type;
2877 }
2878
2879 static void rk81x_sleep_discharge(struct battery_info *di)
2880 {
2881         int delta_cap;
2882         int delta_soc;
2883         int sleep_min;
2884         unsigned long sleep_sec;
2885         int enter_rsoc;
2886
2887         enter_rsoc = di->real_soc;
2888         sleep_sec = BASE_TO_SEC(di->suspend_time_start);
2889         sleep_min = BASE_TO_MIN(di->suspend_time_start);
2890         delta_cap = di->suspend_cap - di->remain_capacity;
2891         delta_soc = di->suspend_rsoc - _get_soc(di);
2892         di->sum_suspend_cap += delta_cap;
2893
2894         DBG("<%s>, slp_sec(s)=%lu, slp_min=%d\n"
2895             "delta_cap(s)=%d, delta_soc=%d, sum_cap=%d\n"
2896             "remain_cap=%d, rsoc=%d, dsoc=%d\n"
2897             "relax_vol=%d, vol=%d, curr=%d\n",
2898             __func__, sleep_sec, sleep_min,
2899             delta_cap, delta_soc, di->sum_suspend_cap,
2900             di->remain_capacity, _get_soc(di), di->real_soc,
2901             di->relax_voltage, di->voltage, _get_average_current(di));
2902
2903         /*handle rsoc*/
2904         if ((sleep_min >= 30) &&
2905             (di->relax_voltage >= di->voltage)) {
2906                 resume_relax_calib(di);
2907                 restart_relax(di);
2908
2909         /* current_avg < 0: make sure the system is not
2910          * wakeup by charger plugin.
2911          */
2912
2913         /* even if relax voltage is not caught rightly, realtime voltage
2914          * is quite close to relax voltage, we should not do nothing after
2915          * sleep 30min
2916          */
2917         } else if ((sleep_min >= 30) && (di->current_avg < 0)) {
2918                 resume_vol_calib(di, 1);
2919         } else if ((sleep_min >= 3) && (di->current_avg < 0)) {
2920                 resume_vol_calib(di, 0);
2921         }
2922
2923         /*handle dsoc*/
2924         delta_soc = di->sum_suspend_cap/(di->fcc/100);
2925
2926         DBG("<%s>. sum_cap ==> delta_soc = %d\n", __func__, delta_soc);
2927         if (delta_soc > 0) {
2928                 if (di->real_soc-(delta_soc*1/3) <= di->temp_soc)
2929                         di->real_soc -= (delta_soc*1/3);
2930                 else if (di->real_soc-(delta_soc*1/2) < di->temp_soc)
2931                         di->real_soc -= (delta_soc*1/2);
2932                 else
2933                         di->real_soc -= delta_soc;
2934
2935                 /*di->sum_suspend_cap %= (di->fcc/100);*/
2936                 if (di->real_soc != enter_rsoc)
2937                         di->sum_suspend_cap = 0;
2938
2939         } else if (delta_soc < 0) {
2940                 di->real_soc--;
2941         }
2942         DBG("<%s>, out: dsoc=%d, rsoc=%d, sum_cap=%d\n",
2943             __func__, di->real_soc, di->temp_soc, di->sum_suspend_cap);
2944 }
2945
2946 static void rk81x_sleep_charge(struct battery_info *di)
2947 {
2948         unsigned long sleep_soc;
2949         unsigned long sleep_sec;
2950         int delta_cap;
2951         int delta_soc;
2952         int sleep_min;
2953         u8 charge_status = di->charge_status;
2954
2955         if ((di->suspend_charge_current >= 0) ||
2956             (rk81x_get_charge_status(di) == CHARGE_FINISH)) {
2957                 sleep_sec = BASE_TO_SEC(di->suspend_time_start);
2958                 sleep_min = BASE_TO_MIN(di->suspend_time_start);
2959                 delta_cap = di->suspend_cap - di->remain_capacity;
2960                 delta_soc = di->suspend_rsoc - _get_soc(di);
2961
2962                 DBG("<%s>, ac=%d, usb=%d, slp_curr=%d\n",
2963                     __func__, di->ac_online, di->usb_online,
2964                     di->suspend_charge_current);
2965                 if (((di->suspend_charge_current < 800) &&
2966                      (di->ac_online == ONLINE)) ||
2967                      (charge_status == CHARGE_FINISH)) {
2968                         DBG("<%s>,sleep: ac online current < 800\n", __func__);
2969                         if (sleep_sec > 0) {
2970                                 /*default charge current: 1000mA*/
2971                                 di->count_sleep_time += sleep_sec;
2972                                 sleep_soc = 1000*di->count_sleep_time*100
2973                                                         /3600/div(di->fcc);
2974                                 DBG("<%s> sleep_soc=%lu, real_soc=%d\n",
2975                                     __func__, sleep_soc, di->real_soc);
2976                                 if (sleep_soc > 0)
2977                                         di->count_sleep_time = 0;
2978                                 di->real_soc += sleep_soc;
2979                                 if (di->real_soc > 100)
2980                                         di->real_soc = 100;
2981                         }
2982                 } else {
2983                         DBG("<%s>, usb charge\n", __func__);
2984                         if ((di->temp_soc - di->suspend_rsoc) > 0)
2985                                 di->real_soc +=
2986                                         (di->temp_soc - di->suspend_rsoc);
2987                 }
2988
2989                 DBG("<%s>, out: dsoc=%d, rsoc=%d\n",
2990                     __func__, di->real_soc, di->temp_soc);
2991         }
2992 }
2993
2994 /*
2995  * we need flag "sys_wakeup" to make sure that the system is reall power up.
2996  * because there is fake system power up which causes suspend param be cleaned.
2997  */
2998 static void update_resume_state(struct battery_info *di)
2999 {
3000         if (di->resume) {
3001                 di->resume = false;
3002                 di->sys_wakeup = true;
3003                 /*update the info first*/
3004                 rk81x_update_battery_info(di);
3005                 reset_zero_var(di);
3006
3007                 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING)
3008                         rk81x_sleep_discharge(di);
3009
3010                 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING)
3011                         rk81x_sleep_charge(di);
3012         }
3013 }
3014
3015 static void rk81x_set_charger_current(struct battery_info *di,
3016                                       enum charger_type_t charger_type)
3017 {
3018         switch (charger_type) {
3019         case NO_CHARGER:
3020         case USB_CHARGER:
3021                 set_charge_current(di, ILIM_450MA);
3022                 break;
3023
3024         case AC_CHARGER:
3025         case DC_CHARGER:
3026                 set_charge_current(di, di->chg_i_lmt);
3027                 break;
3028         default:
3029                 set_charge_current(di, ILIM_450MA);
3030         }
3031 }
3032
3033
3034 static void rk81x_set_power_supply_state(struct battery_info *di,
3035                                          enum charger_type_t  charger_type)
3036 {
3037         di->usb_online = OFFLINE;
3038         di->ac_online = OFFLINE;
3039         di->dc_online = OFFLINE;
3040
3041         switch (charger_type) {
3042         case NO_CHARGER:
3043                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3044                 break;
3045
3046         case USB_CHARGER:
3047                 di->usb_online = ONLINE;
3048                 di->status = POWER_SUPPLY_STATUS_CHARGING;
3049                 break;
3050
3051         case DC_CHARGER:/*treat dc as ac*/
3052                 di->dc_online = ONLINE;
3053         case AC_CHARGER:
3054                 di->ac_online = ONLINE;
3055                 di->status = POWER_SUPPLY_STATUS_CHARGING;
3056                 break;
3057         default:
3058                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3059         }
3060
3061         if (di->real_soc >= 100)
3062                 di->status = POWER_SUPPLY_STATUS_FULL;
3063 }
3064
3065 static void rk81x_check_battery_status(struct battery_info *di)
3066 {
3067         enum charger_type_t  charger_type;
3068
3069         charger_type = rk81x_get_adp_type(di);
3070         rk81x_set_charger_current(di, charger_type);
3071         rk81x_set_power_supply_state(di, charger_type);
3072 }
3073
3074
3075 /* high load: current < 0 with charger in.
3076  * System will not shutdown while dsoc=0% with charging state(ac_online),
3077  * which will cause over discharge, so oppose status before report states.
3078  */
3079 static void last_check_report(struct battery_info *di)
3080 {
3081         static u32 time;
3082
3083         if ((di->real_soc == 0) &&
3084             (di->status == POWER_SUPPLY_STATUS_CHARGING) &&
3085            di->current_avg < 0) {
3086                 if (BASE_TO_SEC(time) > 60)
3087                         rk81x_set_power_supply_state(di, NO_CHARGER);
3088
3089                 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
3090                 DBG("status=%d, ac_online=%d, usb_online=%d\n",
3091                     di->status, di->ac_online, di->usb_online);
3092
3093         } else {
3094                 time = get_seconds();
3095         }
3096 }
3097 /*
3098  * only do report when there is a change.
3099  *
3100  * if ((di->real_soc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE)):
3101  * when real_soc == 0, we must do report. But it will generate too much android
3102  * info when we enter test_power mode without battery, so we add a fg_drv_mode
3103  * ajudgement.
3104  */
3105 static void report_power_supply_changed(struct battery_info *di)
3106 {
3107         static u32 old_soc;
3108         static u32 old_ac_status;
3109         static u32 old_usb_status;
3110         static u32 old_charge_status;
3111         bool state_changed;
3112
3113         state_changed = false;
3114         if ((di->real_soc == 0) && (di->fg_drv_mode == FG_NORMAL_MODE))
3115                 state_changed = true;
3116         else if (di->real_soc != old_soc)
3117                 state_changed = true;
3118         else if (di->ac_online != old_ac_status)
3119                 state_changed = true;
3120         else if (di->usb_online != old_usb_status)
3121                 state_changed = true;
3122         else if (old_charge_status != di->status)
3123                 state_changed = true;
3124
3125         if (state_changed) {
3126                 power_supply_changed(&di->bat);
3127                 power_supply_changed(&di->usb);
3128                 power_supply_changed(&di->ac);
3129                 old_soc = di->real_soc;
3130                 old_ac_status = di->ac_online;
3131                 old_usb_status = di->usb_online;
3132                 old_charge_status = di->status;
3133                 DBG("<%s>. report: dsoc=%d, rsoc=%d\n",
3134                     __func__, di->real_soc, di->temp_soc);
3135         }
3136 }
3137
3138 static void upd_time_table(struct battery_info *di)
3139 {
3140         u8 i;
3141         static int old_index;
3142         static int old_min;
3143         u32 time;
3144         int mod = di->real_soc % 10;
3145         int index = di->real_soc / 10;
3146
3147         if (di->ac_online == ONLINE || di->usb_online == ONLINE)
3148                 time = di->charge_min;
3149         else
3150                 time = di->discharge_min;
3151
3152         if ((mod == 0) && (index > 0) && (old_index != index)) {
3153                 di->chrg_min[index-1] = time - old_min;
3154                 old_min = time;
3155                 old_index = index;
3156         }
3157
3158         for (i = 1; i < 11; i++)
3159                 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
3160         DBG("\n");
3161 }
3162
3163 /*
3164  * there is a timer inside rk81x to calc how long the battery is in charging
3165  * state. rk81x will close PowerPath inside IC when timer reach, which will
3166  * stop the charging work. we have to reset the corresponding bits to restart
3167  * the timer to avoid that case.
3168  */
3169 static void rk81x_init_chrg_timer(struct battery_info *di)
3170 {
3171         u8 buf;
3172
3173         battery_read(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3174         buf &= ~(0x4);
3175         battery_write(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3176         battery_read(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3177         DBG("%s: clr: CHRG_CTRL_REG3<2> = 0x%x", __func__, buf);
3178         buf |= 0x04;
3179         battery_write(di->rk818, CHRG_CTRL_REG3, &buf, 1);
3180 }
3181
3182 static u8 get_cvcc_charge_hour(struct battery_info *di)
3183 {
3184         u8 hour, buf;
3185
3186         battery_read(di->rk818, CHRG_CTRL_REG2, &buf, 1);
3187         hour = buf & 0x07;
3188
3189         return CHG_CVCC_HOUR[hour];
3190 }
3191
3192 /* we have to estimate the charging finish time from now, to decide
3193  * whether we should reset the timer or not.
3194  */
3195 static void rk81x_check_chrg_over_time(struct battery_info *di)
3196 {
3197         u8 cvcc_hour;
3198
3199         cvcc_hour = get_cvcc_charge_hour(di);
3200         DBG("CHG_TIME(min): %ld, cvcc hour: %d",
3201             BASE_TO_MIN(di->chrg_time_base), cvcc_hour);
3202
3203         if (BASE_TO_MIN(di->chrg_time_base) >= (cvcc_hour-2)*60) {
3204                 di->chrg_cap2_full = di->fcc - di->remain_capacity;
3205                 if (di->current_avg <= 0)
3206                         di->current_avg = 1;
3207
3208                 di->chrg_time2_full = di->chrg_cap2_full*3600/
3209                                         div(abs_int(di->current_avg));
3210
3211                 DBG("CHG_TIME2FULL(min):%d, chrg_cap2_full=%d, current=%d\n",
3212                     SEC_TO_MIN(di->chrg_time2_full), di->chrg_cap2_full,
3213                     di->current_avg);
3214
3215                 if (SEC_TO_MIN(di->chrg_time2_full) > 60) {
3216                         rk81x_init_chrg_timer(di);
3217                         di->chrg_time_base = get_seconds();
3218                         DBG("%s: reset charge timer\n", __func__);
3219                 }
3220         }
3221 }
3222
3223 /*
3224  * in case that we will do reboot stress test, we need a special way
3225  * to ajust the dsoc.
3226  */
3227 static void rk81x_check_reboot(struct battery_info *di)
3228 {
3229         u8 rsoc = di->temp_soc;
3230         u8 dsoc = di->real_soc;
3231         u8 status = di->status;
3232         u8 cnt;
3233         int unit_time;
3234         int smooth_time;
3235
3236         battery_read(di->rk818, REBOOT_CNT_REG, &cnt, 1);
3237         cnt++;
3238
3239         unit_time = di->fcc*3600/100/1200;/*1200mA default*/
3240         smooth_time = cnt*BASE_TO_SEC(di->sys_on_base);
3241
3242         DBG("%s: cnt:%d, unit:%d, sm:%d, sec:%lu, dsoc:%d, rsoc:%d\n",
3243             __func__, cnt, unit_time, smooth_time,
3244             BASE_TO_SEC(di->sys_on_base), dsoc, rsoc);
3245
3246         if (((status == POWER_SUPPLY_STATUS_CHARGING) ||
3247              (status == POWER_SUPPLY_STATUS_FULL)) && (di->current_avg > 0)) {
3248                 DBG("chrg, sm:%d, aim:%d\n", smooth_time, unit_time*3/5);
3249                 if ((dsoc < rsoc-1) && (smooth_time > unit_time*3/5)) {
3250                         cnt = 0;
3251                         dsoc++;
3252                         if (dsoc >= 100)
3253                                 dsoc = 100;
3254                         _copy_soc(di, dsoc);
3255                 }
3256         } else {/*status == POWER_SUPPLY_STATUS_DISCHARGING*/
3257
3258                 DBG("dischrg, sm:%d, aim:%d\n", smooth_time, unit_time*3/5);
3259                 if ((dsoc > rsoc) && (smooth_time > unit_time*3/5)) {
3260                         cnt = 0;
3261                         dsoc--;
3262                         if (dsoc <= 0)
3263                                 dsoc = 0;
3264                         _copy_soc(di, dsoc);
3265                 }
3266         }
3267
3268         copy_reboot_cnt(di, cnt);
3269 }
3270
3271
3272 static void rk81x_update_battery_info(struct battery_info *di)
3273 {
3274         int round_off_dsoc;
3275
3276         di->remain_capacity = _get_realtime_capacity(di);
3277         if (di->remain_capacity > di->fcc)
3278                 _capacity_init(di, di->fcc);
3279
3280         if (di->real_soc > 100)
3281                 di->real_soc = 100;
3282         else if (di->real_soc < 0)
3283                 di->real_soc = 0;
3284
3285         if (di->chrg_time_base == 0)
3286                 di->chrg_time_base = get_seconds();
3287
3288         if (di->sys_on_base == 0)
3289                 di->sys_on_base = get_seconds();
3290
3291         if (di->status == POWER_SUPPLY_STATUS_DISCHARGING) {
3292                 di->chrg_time_base = get_seconds();
3293
3294                 /*round off dsoc = 100*/
3295                 round_off_dsoc = (di->remain_capacity+di->fcc/100/2)*
3296                                         100/div(di->fcc);
3297                 if (round_off_dsoc >= 100 && di->real_soc >= 99)
3298                         di->real_soc = 100;
3299                 DBG("<%s>. round_off_dsoc = %d", __func__, round_off_dsoc);
3300         }
3301
3302         di->work_on = 1;
3303         di->voltage  = _get_battery_voltage(di);
3304         di->current_avg = _get_average_current(di);
3305         di->remain_capacity = _get_realtime_capacity(di);
3306         di->voltage_ocv = _get_OCV_voltage(di);
3307         di->charge_status = rk81x_get_charge_status(di);
3308         di->relax_voltage = get_relax_voltage(di);
3309         di->temp_soc = _get_soc(di);
3310         di->est_ocv_vol = estimate_bat_ocv_vol(di);
3311         di->est_ocv_soc = estimate_bat_ocv_soc(di);
3312         rk81x_check_battery_status(di);/* ac_online, usb_online, status*/
3313         rk81x_check_chrg_over_time(di);
3314         update_cal_offset(di);
3315 }
3316
3317 static void rk81x_battery_work(struct work_struct *work)
3318 {
3319         struct battery_info *di = container_of(work,
3320                         struct battery_info, battery_monitor_work.work);
3321
3322         update_resume_state(di);
3323         wait_charge_finish_signal(di);
3324         charge_finish_routine(di);
3325
3326         rk81x_battery_display_smooth(di);
3327         rk81x_update_battery_info(di);
3328         rsoc_realtime_calib(di);
3329         last_check_report(di);
3330         report_power_supply_changed(di);
3331         _copy_soc(di, di->real_soc);
3332         _save_remain_capacity(di, di->remain_capacity);
3333
3334         dump_debug_info(di);
3335         di->queue_work_cnt++;
3336         queue_delayed_work(di->wq, &di->battery_monitor_work,
3337                            msecs_to_jiffies(TIMER_MS_COUNTS));
3338 }
3339
3340 static void rk81x_battery_charge_check_work(struct work_struct *work)
3341 {
3342         struct battery_info *di = container_of(work,
3343                         struct battery_info, charge_check_work.work);
3344
3345         DBG("rk81x_battery_charge_check_work\n");
3346         charge_disable_open_otg(di->charge_otg);
3347 }
3348
3349 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
3350
3351 int register_battery_notifier(struct notifier_block *nb)
3352 {
3353         return blocking_notifier_chain_register(&battery_chain_head, nb);
3354 }
3355 EXPORT_SYMBOL_GPL(register_battery_notifier);
3356
3357 int unregister_battery_notifier(struct notifier_block *nb)
3358 {
3359         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
3360 }
3361 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
3362
3363 int battery_notifier_call_chain(unsigned long val)
3364 {
3365         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
3366                 == NOTIFY_BAD) ? -EINVAL : 0;
3367 }
3368 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
3369
3370 static void poweron_lowerpoer_handle(struct battery_info *di)
3371 {
3372 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
3373         if ((di->real_soc <= 2) &&
3374             (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
3375                 mdelay(1500);
3376                 /* kernel_power_off(); */
3377         }
3378 #endif
3379 }
3380
3381 static int battery_notifier_call(struct notifier_block *nb,
3382                                  unsigned long event, void *data)
3383 {
3384         struct battery_info *di =
3385             container_of(nb, struct battery_info, battery_nb);
3386
3387         switch (event) {
3388         case 0:
3389                 DBG(" CHARGE enable\n");
3390                 di->charge_otg = 0;
3391                 queue_delayed_work(di->wq, &di->charge_check_work,
3392                                    msecs_to_jiffies(50));
3393                 break;
3394
3395         case 1:
3396                 di->charge_otg  = 1;
3397                 queue_delayed_work(di->wq, &di->charge_check_work,
3398                                    msecs_to_jiffies(50));
3399                 DBG("charge disable OTG enable\n");
3400                 break;
3401
3402         case 2:
3403                 poweron_lowerpoer_handle(di);
3404                 break;
3405
3406         default:
3407                 return NOTIFY_OK;
3408         }
3409         return NOTIFY_OK;
3410 }
3411
3412 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
3413 {
3414         pr_info("<%s>lower power warning!\n", __func__);
3415
3416         rk_send_wakeup_key();
3417         kernel_power_off();
3418         return IRQ_HANDLED;
3419 }
3420
3421 static void disable_vbat_low_irq(struct battery_info *di)
3422 {
3423         /* mask vbat low */
3424         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
3425         /*clr vbat low interrupt */
3426         /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
3427 }
3428 static void enable_vbat_low_irq(struct battery_info *di)
3429 {
3430         /* clr vbat low interrupt */
3431         rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
3432         /* mask vbat low */
3433         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
3434 }
3435
3436 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
3437 {
3438         pr_info("\n------- %s:irq = %d\n", __func__, irq);
3439         g_battery->chrg_time_base = get_seconds();
3440         rk_send_wakeup_key();
3441         return IRQ_HANDLED;
3442 }
3443 static irqreturn_t rk818_vbat_plug_out(int irq, void  *di)
3444 {
3445         pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3446         charge_disable_open_otg(0);
3447         rk_send_wakeup_key();
3448         return IRQ_HANDLED;
3449 }
3450
3451 static irqreturn_t rk818_vbat_charge_ok(int irq, void  *di)
3452 {
3453         pr_info("---------- %s:irq = %d\n", __func__, irq);
3454         rk_send_wakeup_key();
3455         return IRQ_HANDLED;
3456 }
3457
3458 static int rk81x_battery_sysfs_init(struct battery_info *di, struct device *dev)
3459 {
3460         int ret;
3461         int i;
3462         struct kobject *rk818_fg_kobj;
3463
3464         ret = create_sysfs_interfaces(dev);
3465         if (ret < 0) {
3466                 ret = -EINVAL;
3467                 dev_err(dev, "device RK818 battery sysfs register failed\n");
3468                 goto err_sysfs;
3469         }
3470
3471         rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3472         if (!rk818_fg_kobj)
3473                 return -ENOMEM;
3474         for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3475                 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3476                 if (ret != 0) {
3477                         dev_err(dev, "create rk818_battery node error\n");
3478                         goto err_sysfs;
3479                 }
3480         }
3481
3482         return ret;
3483
3484 err_sysfs:
3485         power_supply_unregister(&di->ac);
3486         power_supply_unregister(&di->usb);
3487         power_supply_unregister(&di->bat);
3488
3489         return ret;
3490 }
3491
3492 static void rk81x_battery_irq_init(struct battery_info *di)
3493 {
3494         int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3495         int ret;
3496         struct rk818 *chip = di->rk818;
3497
3498         vb_lo_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3499         plug_in_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3500         plug_out_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3501         chg_ok_irq = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3502
3503         ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3504                                    IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3505         if (ret != 0)
3506                 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3507
3508         di->irq = vb_lo_irq;
3509         enable_irq_wake(di->irq);
3510         disable_vbat_low_irq(di);
3511
3512         ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3513                                    IRQF_TRIGGER_RISING, "rk818_vbat_plug_in",
3514                                    chip);
3515         if (ret != 0)
3516                 dev_err(chip->dev, "plug_in_irq request failed!\n");
3517
3518
3519         ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3520                                    IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out",
3521                                    chip);
3522         if (ret != 0)
3523                 dev_err(chip->dev, "plug_out_irq request failed!\n");
3524
3525
3526         ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3527                                    IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok",
3528                                    chip);
3529         if (ret != 0)
3530                 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3531 }
3532
3533
3534 static void rk81x_battery_info_init(struct battery_info *di, struct rk818 *chip)
3535 {
3536         int fcc_capacity;
3537         u8 i;
3538
3539         g_battery = di;
3540         di->platform_data = chip->battery_data;
3541         di->cell.config = di->platform_data->cell_cfg;
3542         di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3543         di->qmax = di->platform_data->cell_cfg->design_qmax;
3544         di->fcc = di->design_capacity;
3545         di->vol_smooth_time = 0;
3546         di->charge_smooth_time = 0;
3547         di->charge_smooth_status = false;
3548         di->sleep_status = 0;
3549         di->work_on = 0;
3550         di->sys_wakeup = true;
3551         di->pcb_ioffset = 0;
3552         di->pcb_ioffset_updated = false;
3553         di->queue_work_cnt = 0;
3554         di->update_k = 0;
3555         di->voltage_old = 0;
3556         di->display_soc = 0;
3557         di->bat_res = 0;
3558         di->resume = false;
3559         di->sys_wakeup = true;
3560         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3561         di->finish_min = 0;
3562         di->charge_min = 0;
3563         di->discharge_min = 0;
3564         di->charging_time = 0;
3565         di->discharging_time = 0;
3566         di->finish_time = 0;
3567         di->q_dead = 0;
3568         di->q_err = 0;
3569         di->q_shtd = 0;
3570         di->odd_capacity = 0;
3571         di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3572         di->term_chg_cnt = 0;
3573         di->emu_chg_cnt = 0;
3574         di->zero_cycle = 0;
3575         di->chrg_time_base = 0;
3576         di->sys_on_base = 0;
3577         di->sum_suspend_cap = 0;
3578         di->adjust_cap = 0;
3579         di->first_on_cap = 0;
3580         di->fg_drv_mode = FG_NORMAL_MODE;
3581
3582         for (i = 0; i < 10; i++)
3583                 di->chrg_min[i] = 0;
3584
3585         di->debug_finish_real_soc = 0;
3586         di->debug_finish_temp_soc = 0;
3587
3588         fcc_capacity = _get_FCC_capacity(di);
3589         if (fcc_capacity > 1000)
3590                 di->fcc = fcc_capacity;
3591         else
3592                 di->fcc = di->design_capacity;
3593 }
3594 /*
3595 static struct of_device_id rk818_battery_of_match[] = {
3596 { .compatible = "rk818_battery" },
3597 { }
3598 };
3599
3600 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3601 */
3602
3603
3604 /*
3605  * dc_det_pullup_inside:
3606  *
3607  *  0:  thers is resistance in the pcb to pull the pin up;
3608  *  1:  there is no resistance in the pcb to pull the pin up.
3609  *      we have to use inside pullup resistance function,
3610  *      so we have to define pinctrl info in DTS and analyze it
3611  */
3612 static void rk81x_dc_det_init(struct battery_info *di,
3613                               struct device_node *np)
3614 {
3615         struct device *dev = di->dev;
3616         struct rk818 *rk818 = di->rk818;
3617         enum of_gpio_flags flags;
3618         int ret;
3619
3620         /*thers is resistance in the pcb to pull the pin up*/
3621         if (!di->dc_det_pullup_inside)
3622                 goto out;
3623
3624         /*there is no resistance in the pcb to pull the pin up.*/
3625         di->pinctrl = devm_pinctrl_get(rk818->dev);
3626         if (IS_ERR(di->pinctrl)) {
3627                 dev_err(dev, "No pinctrl used!\n");
3628                 return;
3629         }
3630
3631         /* lookup default state */
3632         di->pins_default = pinctrl_lookup_state(di->pinctrl, "default");
3633         if (IS_ERR(di->pins_default)) {
3634                 dev_err(dev, "No default pinctrl found!\n");
3635         } else {
3636                 ret = pinctrl_select_state(di->pinctrl, di->pins_default);
3637                 if (ret < 0) {
3638                         dev_err(dev, "Default pinctrl setting failed!\n");
3639                 } else {
3640 out:
3641                         di->dc_det_pin = of_get_named_gpio_flags(np,
3642                                                 "dc_det_gpio", 0, &flags);
3643                         if (di->dc_det_pin == -EPROBE_DEFER)
3644                                 dev_err(dev, "dc_det_gpio error\n");
3645                         if (gpio_is_valid(di->dc_det_pin))
3646                                 di->dc_det_level =
3647                                         (flags & OF_GPIO_ACTIVE_LOW) ?
3648                                                 RK818_DC_IN : RK818_DC_OUT;
3649                 }
3650         }
3651 }
3652
3653 #ifdef CONFIG_OF
3654 static int rk81x_battery_parse_dt(struct battery_info *di)
3655 {
3656         struct device_node *regs, *rk818_pmic_np, *test_np;
3657         struct battery_platform_data *data;
3658         struct cell_config *cell_cfg;
3659         struct ocv_config *ocv_cfg;
3660         struct property *prop;
3661         struct rk818 *rk818 = di->rk818;
3662         struct device *dev = di->dev;
3663         u32 out_value;
3664         int length, ret;
3665
3666         rk818_pmic_np = of_node_get(rk818->dev->of_node);
3667         if (!rk818_pmic_np) {
3668                 dev_err(dev, "could not find pmic sub-node\n");
3669                 return -EINVAL;
3670         }
3671
3672         regs = of_find_node_by_name(rk818_pmic_np, "battery");
3673         if (!regs) {
3674                 dev_err(dev, "battery node not found!\n");
3675                 return -EINVAL;
3676         }
3677
3678         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3679         if (!data) {
3680                 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3681                 return -ENOMEM;
3682         }
3683
3684         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3685         if (!cell_cfg) {
3686                 dev_err(dev, "kzalloc for cell_config failed!\n");
3687                 return -ENOMEM;
3688         }
3689         ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3690         if (!ocv_cfg) {
3691                 dev_err(dev, "kzalloc for ocv_config failed!\n");
3692                 return -ENOMEM;
3693         }
3694
3695         prop = of_find_property(regs, "ocv_table", &length);
3696         if (!prop) {
3697                 dev_err(dev, "ocv_table not found!\n");
3698                 return -EINVAL;
3699         }
3700         data->ocv_size = length / sizeof(u32);
3701
3702         if (data->ocv_size > 0) {
3703                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3704
3705                 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3706                 if (!data->battery_ocv) {
3707                         dev_err(dev, "kzalloc for ocv_table failed!\n");
3708                         return -ENOMEM;
3709                 }
3710                 ret = of_property_read_u32_array(regs, "ocv_table",
3711                                                  data->battery_ocv,
3712                                                  data->ocv_size);
3713                 if (ret < 0)
3714                         return ret;
3715         }
3716
3717         /******************** charger param  ****************************/
3718         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3719         if (ret < 0) {
3720                 dev_err(dev, "max_charge_currentmA not found!\n");
3721                 out_value = DEFAULT_ICUR;
3722         }
3723         data->max_charger_currentmA = out_value;
3724
3725         ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3726         if (ret < 0) {
3727                 dev_err(dev, "max_charger_ilimitmA not found!\n");
3728                 out_value = DEFAULT_ILMT;
3729         }
3730         data->max_charger_ilimitmA = out_value;
3731
3732         ret = of_property_read_u32(regs, "bat_res", &out_value);
3733         if (ret < 0) {
3734                 dev_err(dev, "bat_res not found!\n");
3735                 out_value = DEFAULT_BAT_RES;
3736         }
3737         data->sense_resistor_mohm = out_value;
3738
3739         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3740         if (ret < 0) {
3741                 dev_err(dev, "max_charge_voltagemV not found!\n");
3742                 out_value = DEFAULT_VLMT;
3743         }
3744         data->max_charger_voltagemV = out_value;
3745
3746         ret = of_property_read_u32(regs, "design_capacity", &out_value);
3747         if (ret < 0) {
3748                 dev_err(dev, "design_capacity not found!\n");
3749                 return ret;
3750         }
3751         cell_cfg->design_capacity  = out_value;
3752
3753         ret = of_property_read_u32(regs, "design_qmax", &out_value);
3754         if (ret < 0) {
3755                 dev_err(dev, "design_qmax not found!\n");
3756                 return ret;
3757         }
3758         cell_cfg->design_qmax = out_value;
3759
3760         ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3761         if (ret < 0) {
3762                 dev_err(dev, "sleep_enter_current not found!\n");
3763                 return ret;
3764         }
3765         ocv_cfg->sleep_enter_current = out_value;
3766
3767         ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3768         if (ret < 0) {
3769                 dev_err(dev, "sleep_exit_current not found!\n");
3770                 return ret;
3771         }
3772         ocv_cfg->sleep_exit_current = out_value;
3773
3774         /********************  test power param ****************************/
3775         test_np = of_find_node_by_name(regs, "test_power");
3776         if (!regs) {
3777                 dev_err(dev, "test-power node not found!\n");
3778                 di->test_chrg_current = DEF_TEST_CURRENT_MA;
3779                 di->test_chrg_ilmt = DEF_TEST_ILMT_MA;
3780         } else {
3781                 ret = of_property_read_u32(test_np, "test_charge_currentmA",
3782                                            &out_value);
3783                 if (ret < 0) {
3784                         dev_err(dev, "test_charge_currentmA not found!\n");
3785                         out_value = DEF_TEST_CURRENT_MA;
3786                 }
3787                 di->test_chrg_current = out_value;
3788
3789                 ret = of_property_read_u32(test_np, "test_charge_ilimitmA",
3790                                            &out_value);
3791                 if (ret < 0) {
3792                         dev_err(dev, "test_charge_ilimitmA not found!\n");
3793                         out_value = DEF_TEST_ILMT_MA;
3794                 }
3795                 di->test_chrg_ilmt = out_value;
3796         }
3797
3798         /*************  charger support adp types **********************/
3799         ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3800         ret = of_property_read_u32(regs, "support_usb_adp", &support_usb_adp);
3801         ret = of_property_read_u32(regs, "support_dc_adp", &support_dc_adp);
3802         ret = of_property_read_u32(regs, "dc_det_pullup_inside", &out_value);
3803         if (ret < 0)
3804                 out_value = 0;
3805         di->dc_det_pullup_inside = out_value;
3806
3807         if (!support_usb_adp && !support_dc_adp) {
3808                 dev_err(dev, "miss both: usb_adp and dc_adp,default:usb_adp!\n");
3809                 support_usb_adp = 1;
3810         }
3811
3812         if (support_dc_adp)
3813                 rk81x_dc_det_init(di, regs);
3814
3815         cell_cfg->ocv = ocv_cfg;
3816         data->cell_cfg = cell_cfg;
3817         rk818->battery_data = data;
3818
3819         DBG("\n--------- the battery OCV TABLE dump:\n");
3820         DBG("bat_res :%d\n", data->sense_resistor_mohm);
3821         DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3822         DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3823         DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3824         DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3825         DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3826         DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3827         DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3828         DBG("support_uboot_chrg = %d\n", support_uboot_chrg);
3829         DBG("support_usb_adp = %d\n", support_usb_adp);
3830         DBG("support_dc_adp= %d\n", support_dc_adp);
3831         DBG("test_charge_currentmA = %d\n", di->test_chrg_current);
3832         DBG("test_charge_ilimitmA = %d\n", di->test_chrg_ilmt);
3833         DBG("dc_det_pullup_inside = %d\n", di->dc_det_pullup_inside);
3834         DBG("--------- rk818_battery dt_parse ok.\n");
3835         return 0;
3836 }
3837
3838 #else
3839 static int rk81x_battery_parse_dt(struct battery_info *di)
3840 {
3841         return -ENODEV;
3842 }
3843 #endif
3844
3845
3846 static int rk81x_battery_probe(struct platform_device *pdev)
3847 {
3848         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3849         struct battery_info *di;
3850         int ret;
3851
3852         DBG("battery driver version %s\n", DRIVER_VERSION);
3853         di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3854         if (!di) {
3855                 dev_err(&pdev->dev, "kzalloc di failed!\n");
3856                 return -ENOMEM;
3857         }
3858         di->rk818 = chip;
3859         di->dev = &pdev->dev;
3860         platform_set_drvdata(pdev, di);
3861
3862         ret = rk81x_battery_parse_dt(di);
3863         if (ret < 0) {
3864                 dev_err(&pdev->dev, "rk81x battery parse dt failed!\n");
3865                 return ret;
3866         }
3867         rk81x_battery_info_init(di, chip);
3868         if (!is_rk81x_bat_exist(di)) {
3869                 pr_info("not find Li-ion battery, test power mode\n");
3870                 rk81x_battery_charger_init(di);
3871                 di->fg_drv_mode = TEST_POWER_MODE;
3872         }
3873
3874         battery_power_supply_init(di);
3875         ret = battery_power_supply_register(di);
3876         if (ret) {
3877                 dev_err(&pdev->dev, "rk81x power supply register failed!\n");
3878                 return ret;
3879         }
3880
3881         rk81x_battery_irq_init(di);
3882         rk81x_battery_sysfs_init(di, &pdev->dev);
3883
3884         rk81x_fg_init(di);
3885         wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND,
3886                        "resume_charging");
3887         flatzone_voltage_init(di);
3888         rk81x_check_battery_status(di);
3889
3890         di->wq = create_singlethread_workqueue("rk81x-battery-work");
3891         INIT_DELAYED_WORK(&di->battery_monitor_work, rk81x_battery_work);
3892         queue_delayed_work(di->wq, &di->battery_monitor_work,
3893                            msecs_to_jiffies(TIMER_MS_COUNTS*5));
3894         INIT_DELAYED_WORK(&di->charge_check_work,
3895                           rk81x_battery_charge_check_work);
3896         di->battery_nb.notifier_call = battery_notifier_call;
3897         register_battery_notifier(&di->battery_nb);
3898
3899         DBG("rk81x battery probe ok!\n");
3900
3901         return ret;
3902 }
3903
3904
3905 #ifdef CONFIG_PM
3906
3907 static int rk81x_battery_suspend(struct platform_device *dev,
3908                                  pm_message_t state)
3909 {
3910         struct battery_info *di = platform_get_drvdata(dev);
3911
3912         enable_vbat_low_irq(di);
3913         di->sleep_status = di->status;
3914
3915         /* avoid abrupt wakeup which will clean the variable*/
3916         if (di->sys_wakeup) {
3917                 di->suspend_cap = di->remain_capacity;
3918                 di->suspend_rsoc = _get_soc(di);
3919                 di->suspend_time_start = get_seconds();
3920                 di->sys_wakeup = false;
3921         }
3922
3923         pr_info("rk81x-battery suspend: v=%d ld=%d lr=%d c=%d chg=%d\n",
3924                 _get_battery_voltage(di), di->real_soc, _get_soc(di),
3925                 _get_average_current(di), di->status);
3926
3927         cancel_delayed_work(&di->battery_monitor_work);
3928
3929         return 0;
3930 }
3931
3932 static int rk81x_battery_resume(struct platform_device *dev)
3933 {
3934         struct battery_info *di = platform_get_drvdata(dev);
3935
3936         di->resume = true;
3937         disable_vbat_low_irq(di);
3938         queue_delayed_work(di->wq, &di->battery_monitor_work,
3939                            msecs_to_jiffies(TIMER_MS_COUNTS/2));
3940
3941         if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3942             di->real_soc <= 5)
3943                 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3944
3945         pr_info("rk81x-battery resume: v=%d  rv=%d ld=%d lr=%d c=%d chg=%d\n",
3946                 _get_battery_voltage(di), get_relax_voltage(di),
3947                 di->real_soc, _get_soc(di), _get_average_current(di),
3948                 di->status);
3949         return 0;
3950 }
3951 static int rk81x_battery_remove(struct platform_device *dev)
3952 {
3953         struct battery_info *di = platform_get_drvdata(dev);
3954
3955         cancel_delayed_work_sync(&di->battery_monitor_work);
3956         return 0;
3957 }
3958 static void rk81x_battery_shutdown(struct platform_device *dev)
3959 {
3960         struct battery_info *di = platform_get_drvdata(dev);
3961
3962         cancel_delayed_work_sync(&di->battery_monitor_work);
3963         if (BASE_TO_MIN(di->sys_on_base) <= REBOOT_INTER_MIN)
3964                 rk81x_check_reboot(di);
3965         else
3966                 copy_reboot_cnt(di, 0);
3967         DBG("rk818 shutdown!");
3968 }
3969 #endif
3970
3971 static struct platform_driver rk81x_battery_driver = {
3972         .driver     = {
3973                 .name   = "rk818-battery",
3974                 .owner  = THIS_MODULE,
3975         },
3976
3977         .probe      = rk81x_battery_probe,
3978         .remove     = rk81x_battery_remove,
3979         .suspend    = rk81x_battery_suspend,
3980         .resume     = rk81x_battery_resume,
3981         .shutdown   = rk81x_battery_shutdown,
3982 };
3983
3984 static int __init battery_init(void)
3985 {
3986         return platform_driver_register(&rk81x_battery_driver);
3987 }
3988
3989 fs_initcall_sync(battery_init);
3990 static void __exit battery_exit(void)
3991 {
3992         platform_driver_unregister(&rk81x_battery_driver);
3993 }
3994 module_exit(battery_exit);
3995
3996 MODULE_LICENSE("GPL");
3997 MODULE_ALIAS("platform:rk818-battery");
3998 MODULE_AUTHOR("ROCKCHIP");