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