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