rk818 battery update:low power check and statu reg setting,uboot
[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         printk("_get_full_soc:%d,%d\n",di->fcc,di->remain_capacity);
1072         if(abs_int(di->fcc - di->remain_capacity) < di->fcc/100)
1073                 return 100;
1074         else
1075                 return di->remain_capacity * 100 / di->fcc;
1076 }
1077 static int _get_soc(struct   battery_info *di)
1078 {
1079         return di->remain_capacity * 100 / di->fcc;
1080 }
1081
1082 static enum power_supply_property rk_battery_props[] = {
1083
1084         POWER_SUPPLY_PROP_STATUS,
1085         POWER_SUPPLY_PROP_CURRENT_NOW,
1086         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1087         POWER_SUPPLY_PROP_PRESENT,
1088         POWER_SUPPLY_PROP_HEALTH,
1089         POWER_SUPPLY_PROP_CAPACITY,
1090 };
1091
1092 #define to_device_info(x) container_of((x), \
1093                                 struct battery_info, bat)
1094
1095 static int rk_battery_get_property(struct power_supply *psy,
1096         enum power_supply_property psp,
1097         union power_supply_propval *val)
1098 {
1099         u8 buf;
1100         struct battery_info *di = to_device_info(psy);
1101
1102         switch (psp) {
1103         case POWER_SUPPLY_PROP_CURRENT_NOW:
1104                 val->intval = di->current_avg*1000;/*uA*/
1105                 break;
1106
1107         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1108                 val->intval = di->voltage*1000;/*uV*/
1109                 break;
1110
1111         case POWER_SUPPLY_PROP_PRESENT:
1112                 /*val->intval = val->intval <= 0 ? 0 : 1;*/
1113                 battery_read(di->rk818, SUP_STS_REG, &buf, 1);
1114                 val->intval = (buf >> 7); /*bit7:BAT_EX*/
1115                 break;
1116
1117
1118         case POWER_SUPPLY_PROP_CAPACITY:
1119                 val->intval = di->real_soc;
1120                 break;
1121
1122         case POWER_SUPPLY_PROP_HEALTH:
1123                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1124                 break;
1125
1126         case POWER_SUPPLY_PROP_STATUS:
1127                 val->intval = di->status;
1128                 break;
1129
1130         default:
1131                 return -EINVAL;
1132         }
1133
1134         return 0;
1135 }
1136
1137
1138 static enum power_supply_property rk_battery_ac_props[] = {
1139         POWER_SUPPLY_PROP_ONLINE,
1140 };
1141 static enum power_supply_property rk_battery_usb_props[] = {
1142         POWER_SUPPLY_PROP_ONLINE,
1143 };
1144
1145
1146 #define to_ac_device_info(x) container_of((x), \
1147                                 struct battery_info, ac)
1148
1149 static int rk_battery_ac_get_property(struct power_supply *psy,
1150         enum power_supply_property psp,
1151         union power_supply_propval *val)
1152 {
1153         int ret = 0;
1154         struct battery_info *di = to_ac_device_info(psy);
1155
1156         switch (psp) {
1157         case POWER_SUPPLY_PROP_ONLINE:
1158                 val->intval = di->ac_online;    /*discharging*/
1159                 break;
1160
1161         default:
1162                 ret = -EINVAL;
1163                 break;
1164         }
1165         return ret;
1166 }
1167
1168 #define to_usb_device_info(x) container_of((x), \
1169                                 struct battery_info, usb)
1170
1171 static int rk_battery_usb_get_property(struct power_supply *psy,
1172         enum power_supply_property psp,
1173         union power_supply_propval *val)
1174 {
1175         int ret = 0;
1176         struct battery_info *di = to_usb_device_info(psy);
1177
1178         switch (psp) {
1179         case POWER_SUPPLY_PROP_ONLINE:
1180                 if ((strstr(saved_command_line, "charger") == NULL) && (di->real_soc == 0) && (di->work_on == 1))
1181                         val->intval = 0;
1182                 else
1183                         val->intval = di->usb_online;
1184                 break;
1185
1186         default:
1187                 ret = -EINVAL;
1188                 break;
1189         }
1190
1191         return ret;
1192 }
1193
1194
1195 static void battery_power_supply_init(struct battery_info *di)
1196 {
1197         di->bat.name = "BATTERY";
1198         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1199         di->bat.properties = rk_battery_props;
1200         di->bat.num_properties = ARRAY_SIZE(rk_battery_props);
1201         di->bat.get_property = rk_battery_get_property;
1202
1203         di->ac.name = "AC";
1204         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
1205         di->ac.properties = rk_battery_ac_props;
1206         di->ac.num_properties = ARRAY_SIZE(rk_battery_ac_props);
1207         di->ac.get_property = rk_battery_ac_get_property;
1208
1209         di->usb.name = "USB";
1210         di->usb.type = POWER_SUPPLY_TYPE_USB;
1211         di->usb.properties = rk_battery_usb_props;
1212         di->usb.num_properties = ARRAY_SIZE(rk_battery_usb_props);
1213         di->usb.get_property = rk_battery_usb_get_property;
1214 }
1215
1216 static int battery_power_supply_register(struct battery_info *di, struct device *dev)
1217 {
1218         int ret;
1219
1220         ret = power_supply_register(dev, &di->bat);
1221         if (ret) {
1222                 dev_err(dev, "failed to register main battery\n");
1223                 goto batt_failed;
1224         }
1225         ret = power_supply_register(dev, &di->usb);
1226         if (ret) {
1227                 dev_err(dev, "failed to register usb power supply\n");
1228                 goto usb_failed;
1229         }
1230         ret = power_supply_register(dev, &di->ac);
1231         if (ret) {
1232                 dev_err(dev, "failed to register ac power supply\n");
1233                 goto ac_failed;
1234         }
1235
1236         return 0;
1237
1238 ac_failed:
1239         power_supply_unregister(&di->ac);
1240 usb_failed:
1241         power_supply_unregister(&di->usb);
1242 batt_failed:
1243         power_supply_unregister(&di->bat);
1244
1245         return ret;
1246 }
1247
1248 static void  _capacity_init(struct battery_info *di, u32 capacity)
1249 {
1250         u8 buf;
1251         u32 capacity_ma;
1252
1253         reset_zero_var(di);
1254
1255         capacity_ma = capacity*2390;/* 2134;//36*14/900*4096/521*500; */
1256         do {
1257                 buf = (capacity_ma>>24)&0xff;
1258                 battery_write(di->rk818, GASCNT_CAL_REG3, &buf, 1);
1259                 buf = (capacity_ma>>16)&0xff;
1260                 battery_write(di->rk818, GASCNT_CAL_REG2, &buf, 1);
1261                 buf = (capacity_ma>>8)&0xff;
1262                 battery_write(di->rk818, GASCNT_CAL_REG1, &buf, 1);
1263                 buf = (capacity_ma&0xff) | 0x01;
1264                 battery_write(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1265                 battery_read(di->rk818, GASCNT_CAL_REG0, &buf, 1);
1266
1267         } while (buf == 0);
1268 }
1269
1270
1271 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
1272 {
1273         u8 buf;
1274         u32 capacity_ma;
1275
1276         if (capacity >= di->qmax)
1277                 capacity = di->qmax;
1278
1279         capacity_ma = capacity;
1280
1281         buf = (capacity_ma>>24)&0xff;
1282         battery_write(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1283         buf = (capacity_ma>>16)&0xff;
1284         battery_write(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1285         buf = (capacity_ma>>8)&0xff;
1286         battery_write(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1287         buf = (capacity_ma&0xff) | 0x01;
1288         battery_write(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1289 }
1290
1291 static int _get_remain_capacity(struct battery_info *di)
1292 {
1293         int ret;
1294         int temp = 0;
1295         u8 buf;
1296         u32 capacity;
1297
1298         ret = battery_read(di->rk818, REMAIN_CAP_REG3, &buf, 1);
1299         temp = buf << 24;
1300         ret = battery_read(di->rk818, REMAIN_CAP_REG2, &buf, 1);
1301         temp |= buf << 16;
1302         ret = battery_read(di->rk818, REMAIN_CAP_REG1, &buf, 1);
1303         temp |= buf << 8;
1304         ret = battery_read(di->rk818, REMAIN_CAP_REG0, &buf, 1);
1305         temp |= buf;
1306
1307         capacity = temp;/* /4096*900/14/36*500/521; */
1308
1309         return capacity;
1310 }
1311
1312
1313 static void  _save_FCC_capacity(struct battery_info *di, u32 capacity)
1314 {
1315         u8 buf;
1316         u32 capacity_ma;
1317
1318         capacity_ma = capacity;
1319         buf = (capacity_ma>>24)&0xff;
1320         battery_write(di->rk818, NEW_FCC_REG3, &buf, 1);
1321         buf = (capacity_ma>>16)&0xff;
1322         battery_write(di->rk818, NEW_FCC_REG2, &buf, 1);
1323         buf = (capacity_ma>>8)&0xff;
1324         battery_write(di->rk818, NEW_FCC_REG1, &buf, 1);
1325         buf = (capacity_ma&0xff) | 0x01;
1326         battery_write(di->rk818, NEW_FCC_REG0, &buf, 1);
1327 }
1328
1329 static int _get_FCC_capacity(struct battery_info *di)
1330 {
1331         int ret;
1332         int temp = 0;
1333         u8 buf;
1334         u32 capacity;
1335
1336         ret = battery_read(di->rk818, NEW_FCC_REG3, &buf, 1);
1337         temp = buf << 24;
1338         ret = battery_read(di->rk818, NEW_FCC_REG2, &buf, 1);
1339         temp |= buf << 16;
1340         ret = battery_read(di->rk818, NEW_FCC_REG1, &buf, 1);
1341         temp |= buf << 8;
1342         ret = battery_read(di->rk818, NEW_FCC_REG0, &buf, 1);
1343         temp |= buf;
1344
1345         if (temp > 1)
1346                 capacity = temp-1;/* 4096*900/14/36*500/521 */
1347         else
1348                 capacity = temp;
1349         DBG("%s NEW_FCC_REG %d  capacity = %d\n", __func__, temp, capacity);
1350
1351         return capacity;
1352 }
1353
1354 static int _get_realtime_capacity(struct battery_info *di)
1355 {
1356         int ret;
1357         int temp = 0;
1358         u8 buf;
1359         u32 capacity;
1360
1361         ret = battery_read(di->rk818, GASCNT3, &buf, 1);
1362         temp = buf << 24;
1363         ret = battery_read(di->rk818, GASCNT2, &buf, 1);
1364         temp |= buf << 16;
1365         ret = battery_read(di->rk818, GASCNT1, &buf, 1);
1366         temp |= buf << 8;
1367         ret = battery_read(di->rk818, GASCNT0, &buf, 1);
1368         temp |= buf;
1369
1370         capacity = temp/2390;/* 4096*900/14/36*500/521; */
1371
1372         return capacity;
1373 }
1374
1375 static void relax_volt_update_remain_capacity(struct battery_info *di, uint16_t relax_voltage, int sleep_min)
1376 {
1377         int remain_capacity;
1378         int relax_capacity;
1379         int now_temp_soc;
1380         int relax_soc;
1381         int abs_soc;
1382         int min, soc_time;
1383         int now_current;
1384
1385         now_temp_soc = _get_soc(di);
1386         _voltage_to_capacity(di, relax_voltage);
1387         relax_soc = di->temp_soc;
1388         relax_capacity = di->temp_nac;
1389         abs_soc = abs32_int(relax_soc - now_temp_soc);
1390
1391         DBG("<%s>. suspend_temp_soc=%d, temp_soc=%d, ,real_soc = %d\n", __func__, di->suspend_temp_soc, now_temp_soc, di->real_soc);
1392         DBG("<%s>. relax_soc = %d, abs_soc = %d\n", __func__, relax_soc, abs_soc);
1393
1394         /*handle temp_soc*/
1395         if (abs32_int(di->real_soc - relax_soc) <= 5) {
1396                 remain_capacity = relax_capacity;
1397                 DBG("<%s>. real-soc is close to relax-soc, set:  temp_soc = relax_soc\n", __func__);
1398         } else {
1399                 if (abs_soc == 0)
1400                         remain_capacity = _get_realtime_capacity(di);
1401                 else if (abs_soc <= 10)
1402                         remain_capacity = relax_capacity;
1403                 else if (abs_soc <= 20)
1404                         remain_capacity = relax_capacity*70/100+di->remain_capacity*30/100;
1405                 else
1406                         remain_capacity = relax_capacity*50/100+di->remain_capacity*50/100;
1407         }
1408         _capacity_init(di, remain_capacity);
1409         di->temp_soc = _get_soc(di);
1410         di->remain_capacity  = _get_realtime_capacity(di);
1411
1412         /*handle real_soc*/
1413         DBG("<%s>. real_soc = %d, adjust delta = %d\n", __func__, di->real_soc, di->suspend_temp_soc - relax_soc);
1414         if (relax_soc < now_temp_soc) {
1415                 if (di->suspend_temp_soc - relax_soc <= 5)
1416                         di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc);
1417                 else if (di->suspend_temp_soc - relax_soc <= 10)
1418                         di->real_soc = di->real_soc - 5;
1419                 else
1420                         di->real_soc = di->real_soc - (di->suspend_temp_soc - relax_soc)/2;
1421         } else {
1422                 now_current = _get_average_current(di);
1423                 soc_time = di->fcc*3600/100/(abs_int(now_current));/*1% time cost*/
1424                 min = soc_time / 60;
1425                 if (sleep_min > min)
1426                         di->real_soc--;
1427         }
1428
1429         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);
1430 }
1431
1432
1433 static int _copy_soc(struct  battery_info *di, u8 save_soc)
1434 {
1435         u8 soc;
1436
1437         soc = save_soc;
1438         battery_write(di->rk818, SOC_REG, &soc, 1);
1439         return 0;
1440 }
1441
1442 static bool support_uboot_charge(void)
1443 {
1444         return support_uboot_chrg?true:false;
1445 }
1446
1447 static int _rsoc_init(struct  battery_info *di)
1448 {
1449         u8 pwron_soc;
1450         u8 init_soc;
1451         u32 remain_capacity;
1452         u8 last_shtd_time;
1453         u8 curr_shtd_time;
1454 #ifdef SUPPORT_USB_CHARGE
1455         int otg_status;
1456 #else
1457         u8 buf;
1458 #endif
1459         di->voltage  = rk_battery_voltage(di);
1460         di->voltage_ocv = _get_OCV_voltage(di);
1461         DBG("OCV voltage = %d\n" , di->voltage_ocv);
1462
1463         if (_is_first_poweron(di)) {
1464                 _save_FCC_capacity(di, di->design_capacity);
1465                 di->fcc = _get_FCC_capacity(di);
1466
1467                 _voltage_to_capacity(di, di->voltage_ocv);
1468                 di->real_soc = di->temp_soc;
1469                 di->nac      = di->temp_nac;
1470                 DBG("<%s>.this is first poweron: OCV-SOC = %d, OCV-CAPACITY = %d, FCC = %d\n", __func__, di->real_soc, di->nac, di->fcc);
1471
1472         } else {
1473                 battery_read(di->rk818, SOC_REG, &pwron_soc, 1);
1474                 init_soc = pwron_soc;
1475                 DBG("<%s>this is NOT first poweron.SOC_REG = %d\n", __func__, pwron_soc);
1476
1477 #ifdef SUPPORT_USB_CHARGE
1478                 otg_status = dwc_otg_check_dpdm();
1479                 if ((pwron_soc == 0) && (otg_status == 1)) { /*usb charging*/
1480                         init_soc = 1;
1481                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1482                 }
1483 #else
1484                 battery_read(di->rk818, VB_MOD_REG, &buf, 1);
1485                 if ((pwron_soc == 0) && ((buf&PLUG_IN_STS) != 0)) {
1486                         init_soc = 1;
1487                         battery_write(di->rk818, SOC_REG, &init_soc, 1);
1488                 }
1489 #endif
1490                 remain_capacity = _get_remain_capacity(di);
1491                 
1492                 if (support_uboot_charge())
1493                         goto out;
1494
1495                 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG, &curr_shtd_time, 1);
1496                 battery_read(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &last_shtd_time, 1);
1497                 battery_write(di->rk818, NON_ACT_TIMER_CNT_REG_SAVE, &curr_shtd_time, 1);
1498                 DBG("<%s>, now_shtd_time = %d, last_shtd_time = %d, otg_status = %d\n", __func__, curr_shtd_time, last_shtd_time, otg_status);
1499
1500                 //if (!support_uboot_charge()) {
1501                 {
1502                         _voltage_to_capacity(di, di->voltage_ocv);
1503                         DBG("<%s>Not first pwron, real_remain_cap = %d, ocv-remain_cp=%d\n", __func__, remain_capacity, di->temp_nac);
1504
1505                         /* if plugin, make sure current shtd_time different from last_shtd_time.*/
1506                         if (last_shtd_time != curr_shtd_time) {
1507
1508                                 if (curr_shtd_time > 30) {
1509                                         remain_capacity = di->temp_nac;
1510                                         DBG("<%s>shutdown_time > 30 minute,  remain_cap = %d\n", __func__, remain_capacity);
1511
1512                                 } else if ((curr_shtd_time > 5) && (abs32_int(di->temp_soc - init_soc) >= 10)) {
1513                                         if (remain_capacity >= di->temp_nac*120/100)
1514                                                 remain_capacity = di->temp_nac*110/100;
1515                                         else if (remain_capacity < di->temp_nac*8/10)
1516                                                 remain_capacity = di->temp_nac*9/10;
1517
1518                                         DBG("<%s> shutdown_time > 3 minute,  remain_cap = %d\n", __func__, remain_capacity);
1519                                 }
1520                         }
1521                 }
1522 out:
1523                 di->real_soc = init_soc;
1524                 di->nac = remain_capacity;
1525                 if (di->nac <= 0)
1526                         di->nac = 0;
1527                 DBG("<%s> init_soc = %d, init_capacity=%d\n", __func__, di->real_soc, di->nac);
1528         }
1529         return 0;
1530 }
1531
1532
1533 static u8 get_charge_status(struct battery_info *di)
1534 {
1535         u8 status;
1536         u8 ret = 0;
1537
1538         battery_read(di->rk818, SUP_STS_REG, &status, 1);
1539         status &= (0x70);
1540         switch (status) {
1541         case CHARGE_OFF:
1542                 ret = CHARGE_OFF;
1543                 DBG("  CHARGE-OFF ...\n");
1544                 break;
1545
1546         case DEAD_CHARGE:
1547                 ret = DEAD_CHARGE;
1548                 DBG("  DEAD CHARGE ...\n");
1549                 break;
1550
1551         case  TRICKLE_CHARGE:                           /* (0x02 << 4) */
1552                 ret = DEAD_CHARGE;
1553                 DBG("  TRICKLE CHARGE ...\n ");
1554                 break;
1555
1556         case  CC_OR_CV:                                 /* (0x03 << 4) */
1557                 ret = CC_OR_CV;
1558                 DBG("  CC or CV ...\n");
1559                 break;
1560
1561         case  CHARGE_FINISH:                            /* (0x04 << 4) */
1562                 ret = CHARGE_FINISH;
1563                 DBG("  CHARGE FINISH ...\n");
1564                 break;
1565
1566         case  USB_OVER_VOL:                                     /* (0x05 << 4) */
1567                 ret = USB_OVER_VOL;
1568                 DBG("  USB OVER VOL ...\n");
1569                 break;
1570
1571         case  BAT_TMP_ERR:                                      /* (0x06 << 4) */
1572                 ret = BAT_TMP_ERR;
1573                 DBG("  BAT TMP ERROR ...\n");
1574                 break;
1575
1576         case  TIMER_ERR:                                        /* (0x07 << 4) */
1577                 ret = TIMER_ERR;
1578                 DBG("  TIMER ERROR ...\n");
1579                 break;
1580
1581         case  USB_EXIST:                                        /* (1 << 1)// usb is exists */
1582                 ret = USB_EXIST;
1583                 DBG("  USB EXIST ...\n");
1584                 break;
1585
1586         case  USB_EFF:                                          /* (1 << 0)// usb is effective */
1587                 ret = USB_EFF;
1588                 DBG("  USB EFF...\n");
1589                 break;
1590
1591         default:
1592                 return -EINVAL;
1593         }
1594
1595         return ret;
1596
1597 }
1598 static void set_charge_current(struct battery_info *di, int charge_current)
1599 {
1600         u8 usb_ctrl_reg;
1601
1602         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1603         usb_ctrl_reg &= (~0x0f);/* (VLIM_4400MV | ILIM_1200MA) |(0x01 << 7); */
1604         usb_ctrl_reg |= (charge_current);
1605         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1606 }
1607
1608 static void fg_match_param(struct battery_info *di, int chg_vol, int chg_ilim, int chg_cur)
1609 {
1610         int i;
1611
1612         di->chg_v_lmt = CHRG_VOL4200;
1613         di->chg_i_lmt = ILIM_1750MA;
1614         di->chg_i_cur = CHRG_CUR1400mA;
1615         
1616         for (i=0; i<ARRAY_SIZE(CHG_V_LMT); i++){
1617                 if (chg_vol < CHG_V_LMT[i])
1618                         break;
1619                 else
1620                         di->chg_v_lmt = (i << CHG_VOL_SHIFT);
1621         }
1622
1623         for (i=0; i<ARRAY_SIZE(CHG_I_LMT); i++){
1624                 if (chg_ilim < CHG_I_LMT[i])
1625                         break;
1626                 else
1627                         di->chg_i_lmt = (i << CHG_ILIM_SHIFT);
1628         }
1629
1630         for (i=0; i<ARRAY_SIZE(CHG_I_CUR); i++){
1631                 if (chg_cur < CHG_I_CUR[i])
1632                         break;
1633                 else
1634                         di->chg_i_cur = (i << CHG_ICUR_SHIFT);
1635         }
1636         DBG("vol = 0x%x, i_lim = 0x%x, cur=0x%x\n",
1637                 di->chg_v_lmt, di->chg_i_lmt, di->chg_i_cur);
1638 }
1639
1640 static void rk_battery_charger_init(struct  battery_info *di)
1641 {
1642         u8 chrg_ctrl_reg1, usb_ctrl_reg, chrg_ctrl_reg2, chrg_ctrl_reg3;
1643         u8 sup_sts_reg;
1644
1645         int chg_vol = di->rk818->battery_data->max_charger_voltagemV;
1646         int chg_cur = di->rk818->battery_data->max_charger_currentmA;
1647         int chg_ilim = di->rk818->battery_data->max_charger_ilimitmA;
1648         fg_match_param(di, chg_vol, chg_ilim, chg_cur);
1649         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1650         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1651         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1652         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1653         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1654
1655         DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
1656         usb_ctrl_reg &= (~0x0f);
1657 #ifdef SUPPORT_USB_CHARGE
1658         usb_ctrl_reg |= (ILIM_450MA);
1659 #else
1660         usb_ctrl_reg |= (di->chg_i_lmt);
1661 #endif
1662         chrg_ctrl_reg1 &= (0x00);
1663         chrg_ctrl_reg1 |= (CHRG_EN) | (di->chg_v_lmt | di->chg_i_cur);
1664
1665         chrg_ctrl_reg3 |= CHRG_TERM_DIG_SIGNAL;/* digital finish mode*/
1666         chrg_ctrl_reg2 &= ~(0xc0);
1667         chrg_ctrl_reg2 |= FINISH_100MA;
1668
1669         sup_sts_reg &= ~(0x01 << 3);
1670         sup_sts_reg |= (0x01 << 2);
1671
1672         battery_write(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
1673         battery_write(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
1674         battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
1675         battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1676         battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1677
1678         debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1679         debug_reg(di, SUP_STS_REG, "SUP_STS_REG");
1680         debug_reg(di, USB_CTRL_REG, "USB_CTRL_REG");
1681         debug_reg(di, CHRG_CTRL_REG1, "CHRG_CTRL_REG1");
1682
1683         DBG("%s  end\n", __func__);
1684 }
1685
1686 void charge_disable_open_otg(int value)
1687 {
1688         struct  battery_info *di = g_battery;
1689
1690         if (value == 1) {
1691                 DBG("charge disable, enable OTG.\n");
1692                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0 << 7);
1693                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); /*  enable OTG */
1694         }
1695         if (value == 0) {
1696                 DBG("charge enable, disable OTG.\n");
1697                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); /* disable OTG */
1698                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7);
1699         }
1700 }
1701
1702 static void low_waring_init(struct battery_info *di)
1703 {
1704         u8 vb_mon_reg;
1705         u8 vb_mon_reg_init;
1706
1707         battery_read(di->rk818, VB_MOD_REG, &vb_mon_reg, 1);
1708
1709         /* 2.8v~3.5v, interrupt */
1710         vb_mon_reg_init = (((vb_mon_reg | (1 << 4)) & (~0x07)) | 0x06);  /* 3400mV*/
1711         battery_write(di->rk818, VB_MOD_REG, &vb_mon_reg_init, 1);
1712 }
1713
1714 static int set_low_power_interrupt(struct battery_info *di)
1715 {
1716         int ret;
1717         u8 buf;
1718
1719         ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1720         buf =(buf&0xE8)|(1<<3)|0x110;
1721         ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1722
1723         return 0;       
1724         
1725 }
1726
1727 //set power off voltage 3.0v
1728 static int set_low_power_shutdown(struct battery_info *di)
1729 {
1730         int ret;
1731         u8 buf;
1732
1733         ret = battery_read(di->rk818, RK818_VB_MON_REG, &buf, 1);
1734         buf =(buf&0xE8)|0x10 ;
1735         ret = battery_write(di->rk818, RK818_VB_MON_REG, &buf, 1);
1736
1737         return 0;
1738 }
1739
1740 static void  fg_init(struct battery_info *di)
1741 {
1742         u8 adc_ctrl_val;
1743
1744         adc_ctrl_val = 0x30;
1745         battery_write(di->rk818, ADC_CTRL_REG, &adc_ctrl_val, 1);
1746
1747         _gauge_enable(di);
1748         /* get the volatege offset */
1749         _get_voltage_offset_value(di);
1750         rk_battery_charger_init(di);
1751         _set_relax_thres(di);
1752         /* get the current offset , the value write to the CAL_OFFSET */
1753         di->current_offset = _get_ioffset(di);
1754         _set_cal_offset(di, di->current_offset+42);
1755         _rsoc_init(di);
1756         _capacity_init(di, di->nac);
1757
1758         di->remain_capacity = _get_realtime_capacity(di);
1759         di->current_avg = _get_average_current(di);
1760
1761         low_waring_init(di);
1762         restart_relax(di);
1763         power_on_save(di, di->voltage_ocv);
1764         /* set sample time for cal_offset interval*/
1765         ioffset_sample_time(di, SAMP_TIME_8MIN);
1766         set_low_power_shutdown(di);
1767         dump_gauge_register(di);
1768         dump_charger_register(di);
1769
1770         DBG("<%s> :\n"
1771             "nac = %d , remain_capacity = %d\n"
1772             "OCV_voltage = %d, voltage = %d\n"
1773             "SOC = %d, fcc = %d\n, current=%d",
1774             __func__,
1775             di->nac, di->remain_capacity,
1776             di->voltage_ocv, di->voltage,
1777             di->real_soc, di->fcc, di->current_avg);
1778 }
1779
1780
1781 /* int R_soc, D_soc, r_soc, zq, k, Q_err, Q_ocv; */
1782 static void  zero_get_soc(struct   battery_info *di)
1783 {
1784         int dead_voltage, ocv_voltage;
1785         int temp_soc = -1, real_soc;
1786         int currentold, currentnow, voltage;
1787         int i;
1788         int voltage_k;
1789         int count_num = 0;
1790         int q_ocv;
1791         int soc_time;
1792
1793         DBG("\n\n+++++++zero mode++++++display soc+++++++++++\n");
1794         do {
1795                 currentold = _get_average_current(di);
1796                 _get_cal_offset(di);
1797                 _get_ioffset(di);
1798                 msleep(100);
1799                 currentnow = _get_average_current(di);
1800                 count_num++;
1801         } while ((currentold == currentnow) && (count_num < 11));
1802
1803         voltage  = 0;
1804         for (i = 0; i < 10 ; i++)
1805                 voltage += rk_battery_voltage(di);
1806         voltage /= 10;
1807
1808         if (di->voltage_old == 0)
1809                 di->voltage_old = voltage;
1810         voltage_k = voltage;
1811         voltage = (di->voltage_old*2 + 8*voltage)/10;
1812         di->voltage_old = voltage;
1813         currentnow = _get_average_current(di);
1814
1815         dead_voltage = 3400 + abs32_int(currentnow)*(di->bat_res+65)/1000;
1816         /* 65 mo power-path mos */
1817         ocv_voltage = voltage + abs32_int(currentnow)*di->bat_res/1000;
1818         DBG("ZERO: dead_voltage(shtd) = %d, ocv_voltage(now) = %d\n",
1819                         dead_voltage, ocv_voltage);
1820
1821         _voltage_to_capacity(di, dead_voltage);
1822         di->q_dead = di->temp_nac;
1823         DBG("ZERO: dead_voltage_soc = %d, q_dead = %d\n",
1824                 di->temp_soc, di->q_dead);
1825
1826         _voltage_to_capacity(di, ocv_voltage);
1827         q_ocv = di->temp_nac;
1828         DBG("ZERO: ocv_voltage_soc = %d, q_ocv = %d\n",
1829                 di->temp_soc, q_ocv);
1830
1831         /*[Q_err]: Qerr, [temp_nac]:check_voltage_nac*/
1832         di->q_err = di->remain_capacity - q_ocv;
1833         DBG("q_err=%d, [remain_capacity]%d - [q_ocv]%d",
1834                 di->q_err, di->remain_capacity, q_ocv);
1835
1836         if (di->display_soc == 0)
1837                 di->display_soc = di->real_soc*1000;
1838         real_soc = di->display_soc;
1839
1840         DBG("remain_capacity = %d, q_dead = %d, q_err = %d\n",
1841                 di->remain_capacity, di->q_dead, di->q_err);
1842         /*[temp_nac]:dead_voltage*/
1843         if (q_ocv > di->q_dead) {
1844                 DBG("first: q_ocv > di->q_dead\n");
1845
1846                 if (di->update_k == 0 || di->update_k >= 10) {
1847                         if (di->update_k == 0) {
1848                                 DBG("[K == 0]\n");
1849                                 /* ZQ = Q_ded +  Qerr */
1850                                 /*[temp_nac]:dead_voltage*/
1851                                 di->q_shtd = di->q_dead + di->q_err;
1852                                 temp_soc = (di->remain_capacity - di->q_shtd)*
1853                                                 1000/di->fcc;
1854                                 if (temp_soc == 0)
1855                                         di->update_k = 0;
1856                                 else
1857                                         di->line_k = (real_soc + temp_soc/2)
1858                                                         /temp_soc;
1859                         } else {
1860                                 DBG("[K >= 10].\n");
1861                                 temp_soc = ((di->remain_capacity - di->q_shtd)*
1862                                         1000 + di->fcc/2)/di->fcc; /* x1 10 */
1863
1864                                 real_soc = (di->line_k*temp_soc); /*y1=k0*x1*/
1865                                 di->display_soc = real_soc;
1866                                 DBG("[K >= 10]. (temp_soc)X0 = %d\n", temp_soc);
1867                                 DBG("[K >= 10]. in:line_k = %d\n", di->line_k);
1868                                 DBG("[K >= 10]. (dis-soc)Y0=%d,real-soc=%d\n",
1869                                         di->display_soc, di->real_soc);
1870
1871                                 if ((real_soc+500)/1000 < di->real_soc){
1872                                         di->real_soc--;
1873                                         di->odd_capacity = 0;
1874                                 }
1875                                 else if (((real_soc+500))/1000 ==
1876                                                 di->real_soc) {
1877                                          /*dec 1% LSB*/
1878                                         real_soc -= di->odd_capacity;
1879                                         if ((real_soc+500)/1000 <
1880                                                         di->real_soc) {
1881                                                 di->real_soc--;
1882                                                 di->odd_capacity = 0;
1883                                         } else
1884                                                 di->odd_capacity +=
1885                                                                 real_soc/3000+2;
1886                                         DBG("[k >= 10]. odd_capacity=%d\n",
1887                                                 di->odd_capacity);
1888                                 }else
1889                                         di->odd_capacity = 0;
1890                                 _voltage_to_capacity(di, dead_voltage);
1891                                 di->q_dead = di->temp_nac;
1892                                 di->q_shtd = di->q_dead + di->q_err;
1893                                 temp_soc = ((di->remain_capacity - di->q_shtd)*
1894                                         1000 + di->fcc/2)/di->fcc; /* z1 */
1895                                 if (temp_soc == 0)
1896                                         di->update_k = 0;
1897                                 else
1898                                         di->line_k = (di->display_soc +
1899                                                 temp_soc/2)/temp_soc;
1900                                 DBG("[K >= 10]. out:line_k = %d\n", di->line_k);
1901                         }
1902                         di->update_k = 1;
1903                         goto out;
1904                 }
1905
1906                 else { /*update_k[1~9]*/
1907
1908                         di->update_k++;
1909
1910                         DBG("[K1~9]\n");
1911                         temp_soc = ((di->remain_capacity - di->q_shtd)*
1912                                 1000 + di->fcc/2)/di->fcc;
1913                         di->display_soc = di->line_k*temp_soc;
1914                         DBG("[K1~9]. (temp_soc)X0 = %d\n", temp_soc);
1915                         DBG("[K1~9]. line_k = %d\n", di->line_k);
1916                         DBG("[K1~9]. (dis-soc)Y0=%d,real-soc=%d\n",
1917                                 di->display_soc, di->real_soc);
1918                         if ((di->display_soc+500)/1000 < di->real_soc){
1919                                 di->real_soc--;
1920                                 di->odd_capacity = 0;
1921                         }
1922                         else if ((real_soc+500)/1000 == di->real_soc) {
1923                                 /*dec 1% LSB*/
1924                                 real_soc -= di->odd_capacity;
1925                                 if ((real_soc+500)/1000 < di->real_soc) {
1926                                         di->real_soc--;
1927                                         di->odd_capacity = 0;
1928                                 } else
1929                                         di->odd_capacity += real_soc/3000+2;
1930                                 DBG("[K1~9]. odd_capacity=%d\n",
1931                                 di->odd_capacity);
1932                         }else
1933                                 di->odd_capacity = 0;
1934                 }
1935         } else {
1936                 DBG("second: q_ocv < di->q_dead\n");
1937                 di->update_k++;
1938                 if ((di->voltage < 3400) && (di->real_soc > 10)) {
1939                         /*di->real_soc = 10;*/
1940
1941                 } else if (di->voltage < 3400) {
1942                         /*10 -(3.4-Vbat)*100*I*/
1943                         if (di->current_avg < 1000)
1944                                 soc_time = 10-((3400-di->voltage)/10*
1945                                         abs32_int(di->current_avg))/1000;
1946
1947                         DBG("<%s>. ZERO: decrease sec = %d\n",
1948                         __func__, soc_time/2);
1949                         if (di->update_k > soc_time/2) {
1950                                 di->update_k = 0;
1951                                 di->real_soc--;
1952                         }
1953                 } else {
1954                         if (di->update_k > 10) {
1955                                 di->update_k = 0;
1956                                 di->real_soc--;
1957                         }
1958                 }
1959         }
1960 out:
1961         if (di->line_k <= 0) {
1962                 reset_zero_var(di);
1963                 DBG("ZERO: line_k <= 0, Update line_k!\n");
1964         }
1965
1966         DBG("ZERO: update_k=%d, odd_cap=%d\n", di->update_k, di->odd_capacity);
1967         DBG("ZERO: q_ocv - q_dead=%d\n", (q_ocv-di->q_dead));
1968         DBG("ZERO: remain_cap - q_shtd=%d\n",
1969         (di->remain_capacity - di->q_shtd));
1970         DBG("ZERO: (line_k)K0 = %d,(disp-soc)Y0 = %d, (temp_soc)X0 = %d\n",
1971                 di->line_k, di->display_soc, temp_soc);
1972         DBG("ZERO: remain_capacity=%d, q_shtd(nac)=%d, q_err(Q_rm-q_ocv)=%d\n",
1973                 di->remain_capacity, di->q_shtd, di->q_err);
1974         DBG("ZERO: Warn_voltage=%d,temp_soc=%d,real_soc=%d\n\n",
1975                 di->warnning_voltage, _get_soc(di), di->real_soc);
1976 }
1977
1978
1979 static int estimate_bat_ocv_vol(struct battery_info *di)
1980 {
1981         return (di->voltage -
1982                                 (di->bat_res * di->current_avg) / 1000);
1983 }
1984
1985 static int estimate_bat_ocv_soc(struct battery_info *di)
1986 {
1987         int ocv_soc, ocv_voltage;
1988         
1989         ocv_voltage = estimate_bat_ocv_vol(di);
1990         _voltage_to_capacity(di, ocv_voltage);
1991         ocv_soc = di->temp_soc;
1992
1993         return ocv_soc;
1994 }
1995
1996 static void rsoc_dischrg_calib(struct battery_info *di)
1997 {
1998         int ocv_soc = di->est_ocv_soc;
1999         int ocv_volt = di->est_ocv_vol;
2000         int temp_soc = _get_soc(di);
2001         int max_volt = di->rk818->battery_data->max_charger_voltagemV;
2002
2003         if (ocv_volt > max_volt)
2004                 goto out;
2005
2006         if (di->discharge_min >= RSOC_CALIB_DISCHGR_TIME) {
2007                 if ((ocv_soc-temp_soc >= RSOC_DISCHG_ERR_LOWER) ||
2008                         (di->temp_soc == 0) ||
2009                         (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)) {
2010
2011                         di->err_chck_cnt++;
2012                         di->err_soc_sum += ocv_soc;
2013                 } else
2014                         goto out;
2015
2016                 DBG("<%s>. rsoc err_chck_cnt = %d\n",
2017                 __func__, di->err_chck_cnt);
2018                 DBG("<%s>. rsoc err_soc_sum = %d\n",
2019                 __func__, di->err_soc_sum);
2020
2021                 if (di->err_chck_cnt >= RSOC_ERR_CHCK_CNT) {
2022
2023                         ocv_soc = di->err_soc_sum / RSOC_ERR_CHCK_CNT;
2024                         if (temp_soc-ocv_soc >= RSOC_DISCHG_ERR_UPPER)
2025                                 ocv_soc += RSOC_COMPS;
2026
2027                         di->temp_nac = ocv_soc * di->fcc / 100;
2028                         _capacity_init(di, di->temp_nac);
2029                         di->temp_soc = _get_soc(di);
2030                         di->remain_capacity = _get_realtime_capacity(di);
2031                         di->err_soc_sum = 0;
2032                         di->err_chck_cnt = 0;
2033                         DBG("<%s>. update: rsoc = %d\n", __func__, ocv_soc);
2034                 }
2035          } else {
2036 out:
2037                 di->err_chck_cnt = 0;
2038                 di->err_soc_sum = 0;
2039         }
2040
2041 }
2042
2043 static void rsoc_realtime_calib(struct battery_info *di)
2044 {
2045         u8 status = di->status;
2046
2047         if ((status == POWER_SUPPLY_STATUS_CHARGING) ||
2048                 (status == POWER_SUPPLY_STATUS_FULL)) {
2049
2050                 if ((di->current_avg < -10) &&
2051                         (di->charge_status != CHARGE_FINISH))
2052                         rsoc_dischrg_calib(di);
2053                 /*
2054                 else
2055                         rsoc_chrg_calib(di);
2056                 */
2057
2058         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2059                 rsoc_dischrg_calib(di);
2060         }
2061 }
2062
2063 static bool do_ac_charger_emulator(struct battery_info *di)
2064 {
2065         int delta_soc = di->temp_soc - di->real_soc;
2066         u32 soc_time;
2067
2068         if ((di->charge_status != CHARGE_FINISH)
2069                 && (di->ac_online)
2070                 && (delta_soc >= DSOC_CHRG_FAST_EER_RANGE)){
2071                 
2072                 soc_time = di->fcc*3600/100/(abs_int(DSOC_CHRG_EMU_CURR));
2073                 di->emu_chg_cnt++;
2074                 if  (di->emu_chg_cnt > soc_time) {
2075                         di->real_soc++;
2076                         di->emu_chg_cnt = 0;
2077                 }
2078                 DBG("<%s>. soc_time=%d, emu_cnt=%d\n", 
2079                 __func__, soc_time, di->emu_chg_cnt);
2080
2081                 return true;
2082         }
2083
2084         return false;
2085 }
2086
2087 static bool do_term_chrg_cali(struct battery_info *di)
2088 {
2089         u32 soc_time;
2090
2091         if (di->ac_online &&
2092             (di->real_soc >= 90)&& 
2093             (di->current_avg > 600)){
2094
2095                 soc_time = di->fcc*3600/100/(abs32_int(DSOC_CHG_TERM_CURR));
2096                 di->term_chg_cnt++;
2097                 if  (di->term_chg_cnt > soc_time) {
2098                         di->real_soc++;
2099                         di->term_chg_cnt = 0;
2100                 }
2101                 DBG("<%s>. soc_time=%d, term_cnt=%d\n", 
2102                 __func__, soc_time, di->term_chg_cnt);
2103
2104                 return true;
2105         }
2106         
2107         return false;
2108 }
2109
2110 static void voltage_to_soc_discharge_smooth(struct battery_info *di)
2111 {
2112         int voltage;
2113         int now_current, soc_time = -1;
2114         int volt_to_soc;
2115         int delta_soc = di->real_soc - di->temp_soc;
2116
2117         voltage = di->voltage;
2118         now_current = di->current_avg;
2119         if (now_current == 0)
2120                 now_current = 1;
2121
2122         if (delta_soc > DSOC_DISCHRG_FAST_EER_RANGE){
2123                 soc_time = DSOC_DISCHRG_FAST_DEC_SEC;
2124                 DBG("<%s>. dsoc decrease fast! delta_soc = %d\n",
2125                         __func__, delta_soc);
2126         } else 
2127                 soc_time = di->fcc*3600/100/(abs_int(now_current));
2128         _voltage_to_capacity(di, 3800);
2129         volt_to_soc = di->temp_soc;
2130         di->temp_soc = _get_full_soc(di);
2131
2132         printk("<%s>. 3.8v ocv_to_soc = %d\n", __func__, volt_to_soc);
2133         printk("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2134         if ((di->voltage < 3800) || (di->voltage > 3800 && di->real_soc < volt_to_soc)) {  /* di->warnning_voltage) */
2135                 zero_get_soc(di);
2136                 return;
2137
2138         } else if (di->temp_soc == di->real_soc) {
2139                 printk("<%s>. di->temp_soc == di->real_soc\n", __func__);
2140         } else if (di->temp_soc > di->real_soc) {
2141                 printk("<%s>. di->temp_soc > di->real_soc\n", __func__);
2142                 di->vol_smooth_time++;
2143                 if (di->vol_smooth_time > soc_time*3/2) {
2144                         di->real_soc--;
2145                         di->vol_smooth_time = 0;
2146                 }
2147
2148         } else {
2149                 printk("<%s>. di->temp_soc < di->real_soc\n", __func__);
2150                 if (di->real_soc == (di->temp_soc + 1)) {
2151                         di->change_timer = di->soc_timer;
2152                         di->real_soc = di->temp_soc;
2153                 } else {
2154                         di->vol_smooth_time++;
2155                         //low power speed
2156                         if(di->temp_soc<5){
2157                                 if (di->vol_smooth_time > soc_time*1/4) {
2158                                         di->real_soc--;
2159                                         di->vol_smooth_time  = 0;
2160                                 }
2161                         }else{                  
2162                                 if (di->vol_smooth_time > soc_time*3/4) {
2163                                         di->real_soc--;
2164                                         di->vol_smooth_time  = 0;
2165                                 }
2166                         }
2167                 }
2168         }
2169         reset_zero_var(di);
2170         DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2171         DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->vol_smooth_time, soc_time);
2172 }
2173
2174 static int get_charging_time(struct battery_info *di)
2175 {
2176         return (di->charging_time/60);
2177 }
2178
2179 static int get_discharging_time(struct battery_info *di)
2180 {
2181         return (di->discharging_time/60);
2182 }
2183
2184 static int get_finish_time(struct battery_info *di)
2185 {
2186         return (di->finish_time/60);
2187 }
2188
2189 static void dump_debug_info(struct battery_info *di)
2190 {
2191         u8 sup_tst_reg, ggcon_reg, ggsts_reg, vb_mod_reg;
2192         u8 usb_ctrl_reg, chrg_ctrl_reg1;
2193         u8 chrg_ctrl_reg2, chrg_ctrl_reg3, rtc_val;
2194
2195         battery_read(di->rk818, GGCON, &ggcon_reg, 1);
2196         battery_read(di->rk818, GGSTS, &ggsts_reg, 1);
2197         battery_read(di->rk818, SUP_STS_REG, &sup_tst_reg, 1);
2198         battery_read(di->rk818, VB_MOD_REG, &vb_mod_reg, 1);
2199         battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2200         battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2201         battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
2202         battery_read(di->rk818, CHRG_CTRL_REG3, &chrg_ctrl_reg3, 1);
2203         battery_read(di->rk818, 0x00, &rtc_val, 1);
2204
2205         DBG("\n------------- dump_debug_regs -----------------\n"
2206             "GGCON = 0x%2x, GGSTS = 0x%2x, RTC  = 0x%2x\n"
2207             "SUP_STS_REG  = 0x%2x, VB_MOD_REG   = 0x%2x\n"
2208             "USB_CTRL_REG  = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n"
2209             "CHRG_CTRL_REG2 = 0x%2x, CHRG_CTRL_REG3 = 0x%2x\n\n",
2210             ggcon_reg, ggsts_reg, rtc_val,
2211             sup_tst_reg, vb_mod_reg,
2212             usb_ctrl_reg, chrg_ctrl_reg1,
2213             chrg_ctrl_reg2, chrg_ctrl_reg3
2214            );
2215
2216         DBG(
2217             "########################## [read] ################################\n"
2218             "-----------------------------------------------------------------\n"
2219             "realx-voltage = %d, voltage = %d, current-avg = %d\n"
2220             "fcc = %d, remain_capacity = %d, ocv_volt = %d\n"
2221             "check_ocv = %d, check_soc = %d, bat_res = %d\n"
2222             "diplay_soc = %d, cpapacity_soc = %d\n"
2223             "AC-ONLINE = %d, USB-ONLINE = %d, charging_status = %d\n"
2224             "finish_real_soc = %d, finish_temp_soc = %d\n"
2225             "chrg_time = %d, dischrg_time = %d, finish_time = %d\n",
2226             get_relax_voltage(di),
2227             di->voltage, di->current_avg,
2228             di->fcc, di->remain_capacity, _get_OCV_voltage(di),
2229             di->est_ocv_vol, di->est_ocv_soc, di->bat_res,
2230             di->real_soc, _get_soc(di),
2231             di->ac_online, di->usb_online, di->status,
2232             di->debug_finish_real_soc, di->debug_finish_temp_soc,
2233             get_charging_time(di), get_discharging_time(di), get_finish_time(di)
2234            );
2235         get_charge_status(di);
2236         DBG("################################################################\n");
2237 }
2238
2239 static void update_fcc_capacity(struct battery_info *di)
2240 {
2241         if ((di->charge_status == CHARGE_FINISH) && (di->dod0_status == 1)) {
2242                 if (get_level(di) >= di->dod0_level) {
2243                         di->fcc = (di->remain_capacity - di->dod0_capacity)*100/(100-di->dod0);
2244                         if (di->fcc > di->qmax)
2245                                 di->fcc = di->qmax;
2246
2247                         _capacity_init(di, di->fcc);
2248                         _save_FCC_capacity(di, di->fcc);
2249                 }
2250                 di->dod0_status = 0;
2251         }
2252 }
2253
2254 static void debug_get_finish_soc(struct battery_info *di)
2255 {
2256         if (di->charge_status == CHARGE_FINISH) {
2257                 di->debug_finish_real_soc = di->real_soc;
2258                 di->debug_finish_temp_soc = di->temp_soc;
2259         }
2260 }
2261
2262 static void wait_charge_finish_signal(struct battery_info *di)
2263 {
2264         if (di->charge_status == CHARGE_FINISH)
2265                 update_fcc_capacity(di);/* save new fcc*/
2266
2267         /* debug msg*/
2268         debug_get_finish_soc(di);
2269 }
2270
2271 static void charge_finish_routine(struct battery_info *di)
2272 {
2273         if ((di->charge_status == CHARGE_FINISH)&&
2274                 (di->finish_min >= 1)) {
2275                 _capacity_init(di, di->fcc);
2276                 zero_current_calibration(di);
2277
2278                 if (di->real_soc < 100) {
2279                         DBG("<%s>,CHARGE_FINISH  di->real_soc < 100, real_soc=%d\n", __func__, di->real_soc);
2280                         if ((di->soc_counter < 80)) {
2281                                 di->soc_counter++;
2282                         } else {
2283                                 di->soc_counter = 0;
2284                                 di->real_soc++;
2285                         }
2286                 }
2287         }
2288 }
2289
2290 static void voltage_to_soc_charge_smooth(struct battery_info *di)
2291 {
2292         int now_current, soc_time;
2293
2294         reset_zero_var(di);
2295         /*calibrate: aim to match finish signal*/
2296         if (do_term_chrg_cali(di))
2297                 return;
2298
2299         /*calibrate: aim to calib error*/
2300         di->term_chg_cnt = 0;
2301         if (do_ac_charger_emulator(di))
2302                 return;
2303
2304         di->emu_chg_cnt = 0;
2305         now_current = _get_average_current(di);
2306         if (now_current == 0)
2307                 now_current = 1;
2308
2309         soc_time = di->fcc*3600/100/(abs_int(now_current));   /* 1%  time; */
2310         di->temp_soc = _get_soc(di);
2311
2312         DBG("<%s>. di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2313
2314         if (di->real_soc == di->temp_soc) {
2315                 DBG("<%s>. di->temp_soc == di->real_soc\n", __func__);
2316                 di->temp_soc = _get_soc(di);
2317         }
2318         if ((di->temp_soc != di->real_soc) && (now_current != 0)) {
2319
2320                 if (di->temp_soc < di->real_soc + 1) {
2321                         DBG("<%s>. di->temp_soc < di->real_soc\n", __func__);
2322                         di->charge_smooth_time++;
2323                         if  (di->charge_smooth_time > soc_time*3/2) {
2324                                 di->real_soc++;
2325                                 di->charge_smooth_time  = 0;
2326                         }
2327                         di->charge_smooth_status = true;
2328                 }
2329
2330                 else if (di->temp_soc > di->real_soc + 1) {
2331                         DBG("<%s>. di->temp_soc > di->real_soc\n", __func__);
2332                         di->charge_smooth_time++;
2333                         if  (di->charge_smooth_time > soc_time*3/4) {
2334                                 di->real_soc++;
2335                                 di->charge_smooth_time  = 0;
2336                         }
2337                         di->charge_smooth_status = true;
2338
2339                 } else if (di->temp_soc == di->real_soc + 1) {
2340                         DBG("<%s>. di->temp_soc == di->real_soc + 1\n", __func__);
2341                         if (di->charge_smooth_status) {
2342                                 di->charge_smooth_time++;
2343                                 if (di->charge_smooth_time > soc_time*3/4) {
2344                                         di->real_soc = di->temp_soc;
2345                                         di->charge_smooth_time  = 0;
2346                                         di->charge_smooth_status = false;
2347                                 }
2348
2349                         } else {
2350                                 di->real_soc = di->temp_soc;
2351                                 di->charge_smooth_status = false;
2352
2353                         }
2354                 }
2355         }
2356
2357         DBG("<%s>, di->temp_soc = %d, di->real_soc = %d\n", __func__, di->temp_soc, di->real_soc);
2358         DBG("<%s>, di->vol_smooth_time = %d, soc_time = %d\n", __func__, di->charge_smooth_time, soc_time);
2359 }
2360
2361 static void rk_battery_display_smooth(struct battery_info *di)
2362 {
2363         int status;
2364         u8  charge_status;
2365
2366         status = di->status;
2367         charge_status = di->charge_status;
2368         if ((status == POWER_SUPPLY_STATUS_CHARGING) || (status == POWER_SUPPLY_STATUS_FULL)) {
2369
2370                 if ((di->current_avg < -10) && (charge_status != CHARGE_FINISH))
2371                         voltage_to_soc_discharge_smooth(di);
2372                 else
2373                         voltage_to_soc_charge_smooth(di);
2374
2375         } else if (status == POWER_SUPPLY_STATUS_DISCHARGING) {
2376                 voltage_to_soc_discharge_smooth(di);
2377                 if (di->real_soc == 1) {
2378                         di->time2empty++;
2379                         if (di->time2empty >= 200)
2380                                 di->real_soc = 0;
2381                 } else {
2382                         di->time2empty = 0;
2383                 }
2384         }
2385
2386 }
2387
2388 #if 0
2389 static void software_recharge(struct battery_info *di, int max_cnt)
2390 {
2391         static int recharge_cnt;
2392         u8 chrg_ctrl_reg1;
2393
2394         if ((CHARGE_FINISH == get_charge_status(di)) && (rk_battery_voltage(di) < 4100) && (recharge_cnt < max_cnt)) {
2395                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2396                 chrg_ctrl_reg1 &= ~(1 << 7);
2397                 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2398                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2399                 DBG("recharge, clear bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2400                 msleep(400);
2401                 chrg_ctrl_reg1 |= (1 << 7);
2402                 battery_write(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2403                 battery_read(di->rk818, CHRG_CTRL_REG1, &chrg_ctrl_reg1, 1);
2404                 DBG("recharge, set bit7, CHRG_CTRL_REG1 = 0x%x\n", chrg_ctrl_reg1);
2405
2406                 recharge_cnt++;
2407         }
2408 }
2409 #endif
2410
2411 #if 0
2412 static int estimate_battery_resister(struct battery_info *di)
2413 {
2414         int i;
2415         int avr_voltage1 = 0, avr_current1;
2416         int avr_voltage2 = 0, avr_current2;
2417         u8 usb_ctrl_reg;
2418         int bat_res, ocv_votage;
2419         static unsigned long last_time;
2420         unsigned long delta_time;
2421         int charge_ocv_voltage1, charge_ocv_voltage2;
2422         int charge_ocv_soc1, charge_ocv_soc2;
2423
2424         delta_time = get_seconds() - last_time;
2425         DBG("<%s>--- delta_time = %lu\n", __func__, delta_time);
2426         if (delta_time >= 20) {/*20s*/
2427
2428                 /*first sample*/
2429                 set_charge_current(di, ILIM_450MA);/*450mA*/
2430                 msleep(1000);
2431                 for (i = 0; i < 10 ; i++) {
2432                         msleep(100);
2433                         avr_voltage1 += rk_battery_voltage(di);
2434                 }
2435                 avr_voltage1 /= 10;
2436                 avr_current1 = _get_average_current(di);
2437                 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2438                 DBG("------------------------------------------------------------------------------------------\n");
2439                 DBG("avr_voltage1 = %d, avr_current1 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage1, avr_current1, usb_ctrl_reg);
2440
2441                 /*second sample*/
2442                 set_charge_current(di, ILIM_3000MA);
2443                 msleep(1000);
2444                 for (i = 0; i < 10 ; i++) {
2445                         msleep(100);
2446                         avr_voltage2 += rk_battery_voltage(di);
2447                 }
2448                 avr_voltage2 /= 10;
2449                 avr_current2 = _get_average_current(di);
2450                 battery_read(di->rk818, USB_CTRL_REG, &usb_ctrl_reg, 1);
2451                 DBG("avr_voltage2 = %d, avr_current2 = %d, USB_CTRL_REG = 0x%x\n", avr_voltage2, avr_current2, usb_ctrl_reg);
2452
2453                 /*calc resister and ocv_votage ocv*/
2454                 bat_res = (avr_voltage1 - avr_voltage2)*1000/(avr_current1 - avr_current2);
2455                 ocv_votage = avr_voltage1 - (bat_res * avr_current1) / 1000;
2456                 DBG("bat_res = %d, OCV = %d\n", bat_res, ocv_votage);
2457
2458                 /*calc sample voltage ocv*/
2459                 charge_ocv_voltage1 = avr_voltage1 - avr_current1*200/1000;
2460                 charge_ocv_voltage2 = avr_voltage2 - avr_current2*200/1000;
2461                 _voltage_to_capacity(di, charge_ocv_voltage1);
2462                 charge_ocv_soc1 = di->temp_soc;
2463                 _voltage_to_capacity(di, charge_ocv_voltage2);
2464                 charge_ocv_soc2 = di->temp_soc;
2465
2466                 DBG("charge_ocv_voltage1 = %d, charge_ocv_soc1 = %d\n", charge_ocv_voltage1, charge_ocv_soc1);
2467                 DBG("charge_ocv_voltage2 = %d, charge_ocv_soc2 = %d\n", charge_ocv_voltage2, charge_ocv_soc2);
2468                 DBG("------------------------------------------------------------------------------------------\n");
2469                 last_time = get_seconds();
2470
2471                 return bat_res;
2472         }
2473
2474         return 0;
2475 }
2476 #endif
2477
2478 #if 0
2479 static int update_battery_resister(struct battery_info *di)
2480 {
2481         int tmp_res;
2482
2483         if ((get_charging_time(di) > 5) && (!di->bat_res_updated)) {/*charge at least 8min*/
2484
2485                 if ((di->temp_soc >= 80) && (di->bat_res_update_cnt < 10)) {
2486                         tmp_res = estimate_battery_resister(di);
2487                         if (tmp_res != 0)
2488                                 di->bat_res_update_cnt++;
2489                         di->bat_res += tmp_res;
2490                         DBG("<%s>. tmp_bat_res = %d, bat_res_update_cnt = %d\n", __func__, tmp_res, di->bat_res_update_cnt);
2491                         if (di->bat_res_update_cnt == 10) {
2492                                 di->bat_res_updated = true;
2493                                 di->bat_res /= 10;
2494                         }
2495                         DBG("<%s>. bat_res = %d, bat_res_update_cnt = %d\n", __func__, di->bat_res, di->bat_res_update_cnt);
2496                 }
2497         }
2498
2499         return tmp_res;
2500 }
2501 #endif
2502
2503 #if 0
2504 static void charge_soc_check_routine(struct battery_info *di)
2505 {
2506         int min;
2507         int ocv_voltage;
2508         int old_temp_soc;
2509         int ocv_temp_soc;
2510         int remain_capcity;
2511
2512         if (di->status == POWER_SUPPLY_STATUS_CHARGING) {
2513                 min = get_charging_time(di);
2514                 update_battery_resister(di);
2515         if (0)
2516                 if ((min >= 30) && (di->bat_res_updated)) {
2517
2518                         old_temp_soc = di->temp_soc;
2519                         ocv_voltage = di->voltage + di->bat_res*abs(di->current_avg);
2520                         _voltage_to_capacity(di, ocv_voltage);
2521                         ocv_temp_soc = di->temp_soc;
2522
2523                         DBG("<%s>. charge_soc_updated_point0 = %d, charge_soc_updated_point1 = %d\n", __func__, di->charge_soc_updated_point0, di->charge_soc_updated_point1);
2524                         DBG("<%s>. ocv_voltage = %d, ocv_soc = %d\n", __func__, ocv_voltage, ocv_temp_soc);
2525                         DBG("<%s>. voltage = %d, temp_soc = %d\n", __func__, di->voltage, old_temp_soc);
2526
2527                         if (abs32_int(ocv_temp_soc - old_temp_soc) > 10)
2528                                 di->temp_soc = ocv_temp_soc;
2529                         else
2530                                 di->temp_soc = old_temp_soc*50/100 + ocv_temp_soc*50/100;
2531
2532                         remain_capcity = di->temp_soc * di->fcc / 100;
2533                         _capacity_init(di, remain_capcity);
2534                         di->remain_capacity = _get_realtime_capacity(di);
2535                         DBG("<%s>. old_temp_soc = %d, updated_temp_soc = %d\n", __func__, old_temp_soc, di->temp_soc);
2536                 }
2537         }
2538
2539 }
2540 #endif
2541
2542 #if 1
2543 static void update_resume_status_relax_voltage(struct battery_info *di)
2544 {
2545         unsigned long sleep_soc;
2546         unsigned long sum_sleep_soc;
2547         unsigned long sleep_sec;
2548         int relax_voltage;
2549         u8 charge_status;
2550         int delta_capacity;
2551         int delta_soc;
2552         int sum_sleep_avr_current;
2553         int sleep_min;
2554
2555         if (di->resume) {
2556                 update_battery_info(di);
2557                 di->resume = false;
2558                 di->sys_wakeup = true;
2559
2560                 DBG("<%s>, resume----------checkstart\n", __func__);
2561                 sleep_sec = get_seconds() - di->suspend_time_start;
2562                 sleep_min = sleep_sec  / 60;
2563
2564                 DBG("<%s>, resume, sleep_sec(s) = %lu, sleep_min = %d\n",
2565                         __func__, sleep_sec, sleep_min);
2566
2567                 if (di->sleep_status == POWER_SUPPLY_STATUS_DISCHARGING) {
2568                         DBG("<%s>, resume, POWER_SUPPLY_STATUS_DISCHARGING\n", __func__);
2569
2570                         delta_capacity =  di->suspend_capacity - di->remain_capacity;
2571                         delta_soc = di->suspend_temp_soc - _get_soc(di);
2572                         di->dischrg_sum_sleep_capacity += delta_capacity;
2573                         di->dischrg_sum_sleep_sec += sleep_sec;
2574
2575                         sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2576                         sum_sleep_avr_current = di->dischrg_sum_sleep_capacity * 3600 / di->dischrg_sum_sleep_sec;
2577
2578                         DBG("<%s>, resume, suspend_capacity=%d, resume_capacity=%d, real_soc = %d\n",
2579                                 __func__, di->suspend_capacity, di->remain_capacity, di->real_soc);
2580                         DBG("<%s>, resume, delta_soc=%d, delta_capacity=%d, sum_sleep_avr_current=%d mA\n",
2581                                 __func__, delta_soc, delta_capacity, sum_sleep_avr_current);
2582                         DBG("<%s>, resume, sum_sleep_soc=%lu, dischrg_sum_sleep_capacity=%lu, dischrg_sum_sleep_sec=%lu\n",
2583                                 __func__, sum_sleep_soc, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2584                         DBG("<%s>, relax_voltage=%d, voltage = %d\n", __func__, di->relax_voltage, di->voltage);
2585
2586                         /*large suspend current*/
2587                         if (sum_sleep_avr_current > 20) {
2588                                 sum_sleep_soc = di->dischrg_sum_sleep_capacity * 100 / di->fcc;
2589                                 di->real_soc -= sum_sleep_soc;
2590                                 DBG("<%s>. resume, sleep_avr_current is Over 20mA, sleep_soc = %lu, updated real_soc = %d\n",
2591                                         __func__, sum_sleep_soc, di->real_soc);
2592
2593                         /* small suspend current*/
2594                         } else if ((sum_sleep_avr_current >= 0) && (sum_sleep_avr_current <= 20)) {
2595
2596                                 relax_voltage = get_relax_voltage(di);
2597                                 di->voltage  = rk_battery_voltage(di);
2598
2599                                 if ((sleep_min >= 30) && (relax_voltage > di->voltage)) { /* sleep_min >= 30, update by relax voltage*/
2600                                         DBG("<%s>, resume, sleep_min > 30 min\n", __func__);
2601                                         relax_volt_update_remain_capacity(di, relax_voltage, sleep_sec);
2602
2603                                 } else {
2604                                         DBG("<%s>, resume, sleep_min < 30 min\n", __func__);
2605                                         if (sum_sleep_soc > 0)
2606                                                 di->real_soc -= sum_sleep_soc;
2607                                 }
2608                         }
2609
2610                         if ((sum_sleep_soc > 0) || (sleep_min >= 30)) { /*Íê³ÉÁËÒ»´ÎrelaxУ׼*/
2611                                 di->dischrg_sum_sleep_capacity = 0;
2612                                 di->dischrg_sum_sleep_sec = 0;
2613                         }
2614                         DBG("<%s>--------- resume DISCHARGE end\n", __func__);
2615                         DBG("<%s>. dischrg_sum_sleep_capacity = %lu, dischrg_sum_sleep_sec = %lu\n", __func__, di->dischrg_sum_sleep_capacity, di->dischrg_sum_sleep_sec);
2616                 }
2617
2618                 else if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING) {
2619                         DBG("<%s>, resume, POWER_SUPPLY_STATUS_CHARGING\n", __func__);
2620                         if ((di->suspend_charge_current >= 0) || (get_charge_status(di) == CHARGE_FINISH)) {
2621                                 di->temp_soc = _get_soc(di);
2622                                 charge_status = get_charge_status(di);
2623
2624                                 DBG("<%s>, resume, ac-online = %d, usb-online = %d, sleep_current=%d\n", __func__, di->ac_online, di->usb_online, di->suspend_charge_current);
2625                                 if (((di->suspend_charge_current < 800) && (di->ac_online == 1)) || (charge_status == CHARGE_FINISH)) {
2626                                         DBG("resume, sleep : ac online charge current < 1000\n");
2627                                         if (sleep_sec > 0) {
2628                                                 di->count_sleep_time += sleep_sec;
2629                                                 sleep_soc = 1000*di->count_sleep_time*100/3600/di->fcc;
2630                                                 DBG("<%s>, resume, sleep_soc=%lu, real_soc=%d\n", __func__, sleep_soc, di->real_soc);
2631                                                 if (sleep_soc > 0)
2632                                                         di->count_sleep_time = 0;
2633                                                 di->real_soc += sleep_soc;
2634                                                 if (di->real_soc > 100)
2635                                                         di->real_soc = 100;
2636                                         }
2637                                 } else {
2638
2639                                         DBG("<%s>, usb charging\n", __func__);
2640                                         if (di->suspend_temp_soc + 15 < di->temp_soc)
2641                                                 di->real_soc += (di->temp_soc - di->suspend_temp_soc)*3/2;
2642                                         else
2643                                                 di->real_soc += (di->temp_soc - di->suspend_temp_soc);
2644                                 }
2645
2646                                 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);
2647                         }
2648                 }
2649         }
2650 }
2651 #endif
2652
2653 #ifdef SUPPORT_USB_CHARGE
2654 static int  get_charging_status_type(struct battery_info *di)
2655 {
2656         int otg_status = dwc_otg_check_dpdm();
2657
2658         if (0 == otg_status) {
2659                 di->usb_online = 0;
2660                 di->ac_online = 1;
2661                 di->check_count = 0;
2662
2663         } else if (1 == otg_status) {
2664                 if (0 == get_gadget_connect_flag()) {
2665                         if (++di->check_count >= 5) {
2666                                 di->ac_online = 1;
2667                                 di->usb_online = 0;
2668                         } else {
2669                                 di->ac_online = 0;
2670                                 di->usb_online = 1;
2671                         }
2672                 } else {
2673                         di->ac_online = 0;
2674                         di->usb_online = 1;
2675                 }
2676
2677         } else if (2 == otg_status) {
2678                 di->ac_online = 1;
2679                 di->usb_online = 0;
2680                 di->check_count = 0;
2681         }
2682
2683         if (di->ac_online == 1)
2684                 set_charge_current(di, di->chg_i_lmt);
2685         else
2686                 set_charge_current(di, ILIM_450MA);
2687         return otg_status;
2688 }
2689
2690 #endif
2691
2692 static void battery_poweron_status_init(struct battery_info *di)
2693 {
2694         int otg_status;
2695
2696 #ifndef SUPPORT_USB_CHARGE
2697         u8 buf;
2698 #endif
2699
2700 #ifdef SUPPORT_USB_CHARGE
2701
2702         otg_status = dwc_otg_check_dpdm();
2703         if (otg_status == 1) {
2704                 di->usb_online = 1;
2705                 di->ac_online = 0;
2706                 set_charge_current(di, ILIM_450MA);
2707                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2708                 DBG("++++++++ILIM_450MA++++++\n");
2709
2710         } else if (otg_status == 2) {
2711                 di->usb_online = 0;
2712                 di->ac_online = 1;
2713                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2714                 set_charge_current(di, di->chg_i_lmt);
2715                 DBG("++++++++ILIM_1000MA++++++\n");
2716         }
2717         DBG(" CHARGE: SUPPORT_USB_CHARGE. charge_status = %d\n", otg_status);
2718
2719 #else
2720
2721         battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2722         if (buf&PLUG_IN_STS) {
2723                 di->ac_online = 1;
2724                 di->usb_online = 0;
2725                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2726                 if (di->real_soc == 100)
2727                         di->status = POWER_SUPPLY_STATUS_FULL;
2728         } else {
2729                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2730                 di->ac_online = 0;
2731                 di->usb_online = 0;
2732         }
2733         DBG(" CHARGE: NOT SUPPORT_USB_CHARGE\n");
2734 #endif
2735 }
2736 static void check_battery_status(struct battery_info *di)
2737 {
2738         u8 buf;
2739         int ret;
2740
2741         ret = battery_read(di->rk818, VB_MOD_REG, &buf, 1);
2742 #ifdef SUPPORT_USB_CHARGE
2743
2744         if (strstr(saved_command_line, "charger")) {
2745                 if ((buf&PLUG_IN_STS) == 0) {
2746                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2747                         di->ac_online = 0;
2748                         di->usb_online = 0;
2749                 }
2750
2751         } else {
2752                 if (buf&PLUG_IN_STS) {
2753                         get_charging_status_type(di);
2754
2755                         di->status = POWER_SUPPLY_STATUS_CHARGING;
2756                         if (di->real_soc == 100)
2757                                 di->status = POWER_SUPPLY_STATUS_FULL;
2758                 } else {
2759                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2760                         di->ac_online = 0;
2761                         di->usb_online = 0;
2762                 }
2763         }
2764 #else
2765
2766         if (buf & PLUG_IN_STS) {
2767                 di->ac_online = 1;
2768                 di->usb_online = 0;
2769                 di->status = POWER_SUPPLY_STATUS_CHARGING;
2770                 if (di->real_soc == 100)
2771                         di->status = POWER_SUPPLY_STATUS_FULL;
2772         } else {
2773                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2774                 di->ac_online = 0;
2775                 di->usb_online = 0;
2776         }
2777 #endif
2778 }
2779
2780 static void last_check_report(struct battery_info *di)
2781 {
2782 /* high load: current < 0 with charger in.
2783  * System will not shutdown when dsoc=0% with charging state(ac_online), 
2784  * which will cause over discharge, so oppose status. 
2785  */
2786         static u32 time;
2787
2788         if ((di->real_soc == 0) && (di->status == POWER_SUPPLY_STATUS_CHARGING)
2789                 && di->current_avg < 0){
2790                 if (get_seconds() - time > 60){
2791                         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
2792                         di->ac_online = 0;
2793                         di->usb_online = 0;
2794                 }
2795                 DBG("dsoc=0, time=%ld\n", get_seconds() - time);
2796                 DBG("status=%d, ac_online=%d, usb_online=%d\n", 
2797                 di->status, di->ac_online, di->usb_online);
2798
2799         } else
2800                 time = get_seconds();
2801 }
2802
2803 static void report_power_supply_changed(struct battery_info *di)
2804 {
2805         static u32 old_soc;
2806         static u32 old_ac_status;
2807         static u32 old_usb_status;
2808         static u32 old_charge_status;
2809         bool state_changed;
2810
2811         state_changed = false;
2812         if (di->real_soc == 0)
2813                 state_changed = true;
2814         else if (di->real_soc == 100)
2815                 state_changed = true;
2816         else if (di->real_soc != old_soc)
2817                 state_changed = true;
2818         else if (di->ac_online != old_ac_status)
2819                 state_changed = true;
2820         else if (di->usb_online != old_usb_status)
2821                 state_changed = true;
2822         else if (old_charge_status != di->status)
2823                 state_changed = true;
2824
2825         if (state_changed) {
2826                 power_supply_changed(&di->bat);
2827                 power_supply_changed(&di->usb);
2828                 power_supply_changed(&di->ac);
2829                 old_soc = di->real_soc;
2830                 old_ac_status = di->ac_online;
2831                 old_usb_status = di->usb_online;
2832                 old_charge_status = di->status;
2833         }
2834 }
2835
2836 static void upd_time_table(struct battery_info *di)
2837 {
2838         u8 i;
2839         static int old_index = 0;
2840         static int old_min = 0;
2841         u32 time;
2842         int mod = di->real_soc % 10;
2843         int index = di->real_soc / 10;
2844         
2845         if (di->ac_online || di->usb_online)
2846                 time = di->charge_min;
2847         else
2848                 time = di->discharge_min;
2849
2850         if ((mod == 0) && (index > 0) && (old_index != index)) {
2851                 di->chrg_min[index-1] = time - old_min;
2852                 old_min = time;
2853                 old_index = index;
2854         }
2855
2856         for (i=1; i<11; i++)
2857                 DBG("Time[%d]=%d, ", (i*10), di->chrg_min[i-1]);
2858         DBG("\n");
2859
2860 }
2861
2862 static void update_battery_info(struct battery_info *di)
2863 {
2864         di->remain_capacity = _get_realtime_capacity(di);
2865         if (di->remain_capacity > di->fcc)
2866                 _capacity_init(di, di->fcc);
2867
2868         if (di->real_soc > 100)
2869                 di->real_soc = 100;
2870         else if (di->real_soc < 0)
2871                 di->real_soc = 0;
2872
2873         if ((di->ac_online) || (di->usb_online)) {/*charging*/
2874                 di->charging_time++;
2875                 di->discharging_time = 0;
2876         } else {
2877                 di->charging_time = 0;
2878                 if (di->voltage < 3800)
2879                         di->discharging_time += 2;
2880                 else
2881                         di->discharging_time++;
2882         }
2883         if (di->charge_status == CHARGE_FINISH)
2884                 di->finish_time++;
2885         else
2886                 di->finish_time = 0;
2887
2888         di->charge_min = get_charging_time(di);
2889         di->discharge_min = get_discharging_time(di);
2890         di->finish_min = get_finish_time(di);
2891
2892         di->work_on = 1;
2893         di->est_ocv_vol = estimate_bat_ocv_vol(di);
2894         di->est_ocv_soc = estimate_bat_ocv_soc(di);
2895         di->voltage  = rk_battery_voltage(di);
2896         di->current_avg = _get_average_current(di);
2897         di->remain_capacity = _get_realtime_capacity(di);
2898         di->voltage_ocv = _get_OCV_voltage(di);
2899         di->charge_status = get_charge_status(di);
2900         di->otg_status = dwc_otg_check_dpdm();
2901         di->relax_voltage = get_relax_voltage(di);
2902         di->temp_soc = _get_soc(di);
2903         check_battery_status(di);/* ac_online, usb_online, status*/
2904         update_cal_offset(di);
2905         upd_time_table(di);
2906 }
2907
2908 static void rk_battery_work(struct work_struct *work)
2909 {
2910         struct battery_info *di = container_of(work,
2911                         struct battery_info, battery_monitor_work.work);
2912         
2913         update_resume_status_relax_voltage(di);
2914         wait_charge_finish_signal(di);
2915         charge_finish_routine(di);
2916
2917         rk_battery_display_smooth(di);
2918         update_battery_info(di);
2919         rsoc_realtime_calib(di);
2920         last_check_report(di);
2921         report_power_supply_changed(di);
2922         _copy_soc(di, di->real_soc);
2923         _save_remain_capacity(di, di->remain_capacity);
2924
2925         dump_debug_info(di);
2926         di->queue_work_cnt++;
2927         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2928 }
2929
2930 static void rk_battery_charge_check_work(struct work_struct *work)
2931 {
2932         struct battery_info *di = container_of(work,
2933                         struct battery_info, charge_check_work.work);
2934
2935         DBG("rk_battery_charge_check_work\n");
2936         charge_disable_open_otg(di->charge_otg);
2937 }
2938
2939 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
2940
2941 int register_battery_notifier(struct notifier_block *nb)
2942 {
2943         return blocking_notifier_chain_register(&battery_chain_head, nb);
2944 }
2945 EXPORT_SYMBOL_GPL(register_battery_notifier);
2946
2947 int unregister_battery_notifier(struct notifier_block *nb)
2948 {
2949         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
2950 }
2951 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
2952
2953 int battery_notifier_call_chain(unsigned long val)
2954 {
2955         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
2956                 == NOTIFY_BAD) ? -EINVAL : 0;
2957 }
2958 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
2959
2960 static void poweron_lowerpoer_handle(struct battery_info *di)
2961 {
2962 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2963         if ((di->real_soc <= 2) && (di->status == POWER_SUPPLY_STATUS_DISCHARGING)) {
2964                 mdelay(1500);
2965                 /* kernel_power_off(); */
2966         }
2967 #endif
2968 }
2969
2970 static int battery_notifier_call(struct notifier_block *nb,
2971                                                                 unsigned long event, void *data)
2972 {
2973         struct battery_info *di =
2974             container_of(nb, struct battery_info, battery_nb);
2975
2976         switch (event) {
2977         case 0:
2978                 DBG(" CHARGE enable\n");
2979                 di->charge_otg = 0;
2980                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2981                 break;
2982
2983         case 1:
2984                 di->charge_otg  = 1;
2985                 queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
2986                 DBG("charge disable OTG enable\n");
2987                 break;
2988
2989         case 2:
2990                 poweron_lowerpoer_handle(di);
2991                 break;
2992
2993         default:
2994                 return NOTIFY_OK;
2995         }
2996         return NOTIFY_OK;
2997 }
2998
2999 static irqreturn_t rk818_vbat_lo_irq(int irq, void *di)
3000 {
3001         pr_info("<%s>lower power warning!\n", __func__);
3002
3003         _copy_soc(g_battery, 0);
3004         _capacity_init(g_battery, 0);
3005         rk_send_wakeup_key();
3006         kernel_power_off();
3007         return IRQ_HANDLED;
3008 }
3009
3010 static void disable_vbat_low_irq(struct battery_info *di)
3011 {
3012         /* mask vbat low */
3013         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x1 << 1));
3014         /*clr vbat low interrupt */
3015         /* rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));*/
3016 }
3017 static void enable_vbat_low_irq(struct battery_info *di)
3018 {
3019         /* clr vbat low interrupt */
3020         rk818_set_bits(di->rk818, 0x4c, (0x1 << 1), (0x1 << 1));
3021         /* mask vbat low */
3022         rk818_set_bits(di->rk818, 0x4d, (0x1 << 1), (0x0 << 1));
3023 }
3024
3025 static irqreturn_t rk818_vbat_plug_in(int irq, void *di)
3026 {
3027         pr_info("\n------- %s:irq = %d\n", __func__, irq);
3028         rk_send_wakeup_key();
3029         return IRQ_HANDLED;
3030 }
3031 static irqreturn_t rk818_vbat_plug_out(int irq, void  *di)
3032 {
3033         pr_info("\n-------- %s:irq = %d\n", __func__, irq);
3034         charge_disable_open_otg(0);
3035         rk_send_wakeup_key();
3036         return IRQ_HANDLED;
3037 }
3038
3039 static irqreturn_t rk818_vbat_charge_ok(int irq, void  *di)
3040 {
3041         pr_info("---------- %s:irq = %d\n", __func__, irq);
3042         rk_send_wakeup_key();
3043         return IRQ_HANDLED;
3044 }
3045
3046
3047
3048 static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
3049 {
3050         int ret;
3051         int i;
3052         struct kobject *rk818_fg_kobj;
3053
3054         ret = create_sysfs_interfaces(dev);
3055         if (ret < 0) {
3056                 ret = -EINVAL;
3057                 dev_err(dev, "device RK818 battery sysfs register failed\n");
3058                 goto err_sysfs;
3059         }
3060
3061         rk818_fg_kobj = kobject_create_and_add("rk818_battery", NULL);
3062         if (!rk818_fg_kobj)
3063                 return -ENOMEM;
3064         for (i = 0; i < ARRAY_SIZE(rk818_bat_attr); i++) {
3065                 ret = sysfs_create_file(rk818_fg_kobj, &rk818_bat_attr[i].attr);
3066                 if (ret != 0) {
3067                         dev_err(dev, "create rk818_battery node error\n");
3068                         goto err_sysfs;
3069                 }
3070         }
3071
3072         return ret;
3073
3074 err_sysfs:
3075         power_supply_unregister(&di->ac);
3076         power_supply_unregister(&di->usb);
3077         power_supply_unregister(&di->bat);
3078
3079         return ret;
3080 }
3081
3082 static void rk818_battery_irq_init(struct battery_info *di)
3083 {
3084         int plug_in_irq, plug_out_irq, chg_ok_irq, vb_lo_irq;
3085         int ret;
3086         struct rk818 *chip = di->rk818;
3087
3088         vb_lo_irq               = irq_create_mapping(chip->irq_domain, RK818_IRQ_VB_LO);
3089         plug_in_irq     = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_IN);
3090         plug_out_irq    = irq_create_mapping(chip->irq_domain, RK818_IRQ_PLUG_OUT);
3091         chg_ok_irq      = irq_create_mapping(chip->irq_domain, RK818_IRQ_CHG_OK);
3092
3093         ret = request_threaded_irq(vb_lo_irq, NULL, rk818_vbat_lo_irq,
3094                                         IRQF_TRIGGER_HIGH, "rk818_vbatlow", chip);
3095         if (ret != 0)
3096                 dev_err(chip->dev, "vb_lo_irq request failed!\n");
3097
3098         di->irq = vb_lo_irq;
3099         enable_irq_wake(di->irq);
3100         disable_vbat_low_irq(di);
3101
3102         ret = request_threaded_irq(plug_in_irq, NULL, rk818_vbat_plug_in,
3103                                         IRQF_TRIGGER_RISING, "rk818_vbat_plug_in", chip);
3104         if (ret != 0)
3105                 dev_err(chip->dev, "plug_in_irq request failed!\n");
3106
3107
3108         ret = request_threaded_irq(plug_out_irq, NULL, rk818_vbat_plug_out,
3109                                         IRQF_TRIGGER_FALLING, "rk818_vbat_plug_out", chip);
3110         if (ret != 0)
3111                 dev_err(chip->dev, "plug_out_irq request failed!\n");
3112
3113
3114         ret = request_threaded_irq(chg_ok_irq, NULL, rk818_vbat_charge_ok,
3115                                         IRQF_TRIGGER_RISING, "rk818_vbat_charge_ok", chip);
3116         if (ret != 0)
3117                 dev_err(chip->dev, "chg_ok_irq request failed!\n");
3118 }
3119
3120 static void battery_info_init(struct battery_info *di, struct rk818 *chip)
3121 {
3122         int fcc_capacity;
3123         u8 i;
3124         di->rk818 = chip;
3125         g_battery = di;
3126         di->platform_data = chip->battery_data;
3127         di->cell.config = di->platform_data->cell_cfg;
3128         di->design_capacity = di->platform_data->cell_cfg->design_capacity;
3129         di->qmax = di->platform_data->cell_cfg->design_qmax;
3130         di->fcc = di->design_capacity;
3131         di->vol_smooth_time = 0;
3132         di->charge_smooth_time = 0;
3133         di->charge_smooth_status = false;
3134         di->sleep_status = 0;
3135         di->work_on = 0;
3136         di->sys_wakeup = true;
3137         di->pcb_ioffset = 0;
3138         di->pcb_ioffset_updated = false;
3139         di->queue_work_cnt = 0;
3140         di->update_k = 0;
3141         di->voltage_old = 0;
3142         di->display_soc = 0;
3143         di->bat_res = 0;
3144         di->bat_res_updated = false;
3145         di->resume = false;
3146         di->sys_wakeup = true;
3147         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
3148         di->finish_min = 0;
3149         di->charge_min = 0;
3150         di->discharge_min = 0;
3151         di->charging_time = 0;
3152         di->discharging_time = 0;
3153         di->finish_time = 0;
3154         di->q_dead = 0;
3155         di->q_err = 0;
3156         di->q_shtd = 0;
3157         di->odd_capacity = 0;
3158         di->bat_res = di->rk818->battery_data->sense_resistor_mohm;
3159         di->term_chg_cnt = 0;
3160         di->emu_chg_cnt = 0;
3161
3162         for (i=0; i<10; i++)
3163                 di->chrg_min[i] = -1;
3164
3165         di->debug_finish_real_soc = 0;
3166         di->debug_finish_temp_soc = 0;
3167
3168         fcc_capacity = _get_FCC_capacity(di);
3169         if (fcc_capacity > 1000)
3170                 di->fcc = fcc_capacity;
3171         else
3172                 di->fcc = di->design_capacity;
3173 }
3174 /*
3175 static struct of_device_id rk818_battery_of_match[] = {
3176 { .compatible = "rk818_battery" },
3177 { }
3178 };
3179
3180 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
3181 */
3182 #ifdef CONFIG_OF
3183 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3184 {
3185         struct device_node *regs, *rk818_pmic_np;
3186         struct battery_platform_data *data;
3187         struct cell_config *cell_cfg;
3188         struct ocv_config *ocv_cfg;
3189         struct property *prop;
3190         u32 out_value;
3191         int length, ret;
3192
3193         rk818_pmic_np = of_node_get(rk818->dev->of_node);
3194         if (!rk818_pmic_np) {
3195                 dev_err(dev, "could not find pmic sub-node\n");
3196                 return -EINVAL;
3197         }
3198
3199         regs = of_find_node_by_name(rk818_pmic_np, "battery");
3200         if (!regs) {
3201                 dev_err(dev, "battery node not found!\n");
3202                 return -EINVAL;
3203         }
3204
3205         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
3206         if (!data) {
3207                 dev_err(dev, "kzalloc for battery_platform_data failed!\n");
3208                 return -ENOMEM;
3209         }
3210
3211         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
3212         if (!cell_cfg) {
3213                 dev_err(dev, "kzalloc for cell_config failed!\n");
3214                 return -ENOMEM;
3215         }
3216         ocv_cfg = devm_kzalloc(rk818->dev, sizeof(*ocv_cfg), GFP_KERNEL);
3217         if (!ocv_cfg) {
3218                 dev_err(dev, "kzalloc for ocv_config failed!\n");
3219                 return -ENOMEM;
3220         }
3221
3222         prop = of_find_property(regs, "ocv_table", &length);
3223         if (!prop) {
3224                 dev_err(dev, "ocv_table not found!\n");
3225                 return -EINVAL;
3226         }
3227         data->ocv_size = length / sizeof(u32);
3228
3229         if (data->ocv_size > 0) {
3230                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
3231
3232                 data->battery_ocv = devm_kzalloc(rk818->dev, size, GFP_KERNEL);
3233                 if (!data->battery_ocv) {
3234                         dev_err(dev, "kzalloc for ocv_table failed!\n");
3235                         return -ENOMEM;
3236                 }
3237                 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
3238                 if (ret < 0)
3239                         return ret;
3240         }
3241
3242         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
3243         if (ret < 0) {
3244                 dev_err(dev, "max_charge_currentmA not found!\n");
3245                 out_value = DEFAULT_ICUR;
3246         }
3247         data->max_charger_currentmA = out_value;
3248
3249         ret = of_property_read_u32(regs, "max_charge_ilimitmA", &out_value);
3250         if (ret < 0) {
3251                 dev_err(dev, "max_charger_ilimitmA not found!\n");
3252                 out_value = DEFAULT_ILMT;
3253         }
3254         data->max_charger_ilimitmA = out_value;
3255
3256         ret = of_property_read_u32(regs, "bat_res", &out_value);
3257         if (ret < 0) {
3258                 dev_err(dev, "bat_res not found!\n");
3259                 out_value = DEFAULT_BAT_RES;
3260         }
3261         data->sense_resistor_mohm = out_value;
3262
3263         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
3264         if (ret < 0) {
3265                 dev_err(dev, "max_charge_voltagemV not found!\n");
3266                 out_value = DEFAULT_VLMT;
3267         }
3268         data->max_charger_voltagemV = out_value;
3269
3270         ret = of_property_read_u32(regs, "design_capacity", &out_value);
3271         if (ret < 0) {
3272                 dev_err(dev, "design_capacity not found!\n");
3273                 return ret;
3274         }
3275         cell_cfg->design_capacity  = out_value;
3276
3277         ret = of_property_read_u32(regs, "design_qmax", &out_value);
3278         if (ret < 0) {
3279                 dev_err(dev, "design_qmax not found!\n");
3280                 return ret;
3281         }
3282         cell_cfg->design_qmax = out_value;
3283
3284         ret = of_property_read_u32(regs, "sleep_enter_current", &out_value);
3285         if (ret < 0) {
3286                 dev_err(dev, "sleep_enter_current not found!\n");
3287                 return ret;
3288         }
3289         ocv_cfg->sleep_enter_current = out_value;
3290
3291         ret = of_property_read_u32(regs, "sleep_exit_current", &out_value);
3292         if (ret < 0) {
3293                 dev_err(dev, "sleep_exit_current not found!\n");
3294                 return ret;
3295         }
3296         ocv_cfg->sleep_exit_current = out_value;
3297
3298         ret = of_property_read_u32(regs, "support_uboot_chrg", &support_uboot_chrg);
3299
3300         cell_cfg->ocv = ocv_cfg;
3301         data->cell_cfg = cell_cfg;
3302         rk818->battery_data = data;
3303
3304         DBG("\n--------- the battery OCV TABLE dump:\n");
3305         DBG("bat_res :%d\n", data->sense_resistor_mohm);
3306         DBG("max_charge_ilimitmA :%d\n", data->max_charger_ilimitmA);
3307         DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
3308         DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
3309         DBG("design_capacity :%d\n", cell_cfg->design_capacity);
3310         DBG("design_qmax :%d\n", cell_cfg->design_qmax);
3311         DBG("sleep_enter_current :%d\n", cell_cfg->ocv->sleep_enter_current);
3312         DBG("sleep_exit_current :%d\n", cell_cfg->ocv->sleep_exit_current);
3313         DBG("uboot chrg = %d\n", support_uboot_chrg);
3314         DBG("\n--------- rk818_battery dt_parse ok.\n");
3315         return 0;
3316 }
3317
3318 #else
3319 static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
3320 {
3321         return -ENODEV;
3322 }
3323 #endif
3324
3325
3326 static int battery_probe(struct platform_device *pdev)
3327 {
3328         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
3329         struct battery_info *di;
3330         int ret;
3331
3332         DBG("battery driver version %s\n", DRIVER_VERSION);
3333         di = kzalloc(sizeof(*di), GFP_KERNEL);
3334         if (!di) {
3335                 dev_err(&pdev->dev, "kzalloc battery_info memory failed!\n");
3336                 return -ENOMEM;
3337         }
3338         ret = rk_battery_parse_dt(chip, &pdev->dev);
3339         if (ret < 0) {
3340                 dev_err(&pdev->dev, "rk_battery_parse_dt failed!\n");
3341                 return -EINVAL;
3342         }
3343
3344         platform_set_drvdata(pdev, di);
3345         battery_info_init(di, chip);
3346         if (!is_bat_exist(di)) {
3347                 dev_err(&pdev->dev, "could not find Li-ion battery!\n");
3348                 return -ENODEV;
3349         }
3350         fg_init(di);
3351
3352         wake_lock_init(&di->resume_wake_lock, WAKE_LOCK_SUSPEND, "resume_charging");
3353
3354         flatzone_voltage_init(di);
3355         battery_poweron_status_init(di);
3356         battery_power_supply_init(di);
3357         ret = battery_power_supply_register(di, &pdev->dev);
3358         if (ret) {
3359                 dev_err(&pdev->dev, "rk power supply register failed!\n");
3360                 return ret;
3361         }
3362         di->wq = create_singlethread_workqueue("battery-work");
3363         INIT_DELAYED_WORK(&di->battery_monitor_work, rk_battery_work);
3364         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
3365         INIT_DELAYED_WORK(&di->charge_check_work, rk_battery_charge_check_work);
3366
3367         di->battery_nb.notifier_call = battery_notifier_call;
3368         register_battery_notifier(&di->battery_nb);
3369
3370         rk818_battery_irq_init(di);
3371         rk818_battery_sysfs_init(di, &pdev->dev);
3372         DBG("------ RK81x battery_probe ok!-------\n");
3373         return ret;
3374 }
3375
3376
3377 #ifdef CONFIG_PM
3378
3379 static int battery_suspend(struct platform_device *dev, pm_message_t state)
3380 {
3381         struct battery_info *di = platform_get_drvdata(dev);
3382
3383         enable_vbat_low_irq(di);
3384         di->sleep_status = di->status;
3385         di->suspend_charge_current = _get_average_current(di);
3386
3387         /* avoid abrupt wakeup which will clean the variable*/
3388         if (di->sys_wakeup) {
3389                 di->suspend_capacity = di->remain_capacity;
3390                 di->suspend_temp_soc = _get_soc(di);
3391                 di->suspend_time_start = get_seconds();
3392                 di->sys_wakeup = false;
3393         }
3394
3395         cancel_delayed_work(&di->battery_monitor_work);
3396         DBG("<%s>. suspend_temp_soc,=%d, suspend_charge_current=%d, suspend_cap=%d, sleep_status=%d\n",
3397             __func__, di->suspend_temp_soc, di->suspend_charge_current,
3398             di->suspend_capacity, di->sleep_status);
3399         
3400         set_low_power_interrupt(di);
3401         return 0;
3402 }
3403
3404 static int battery_resume(struct platform_device *dev)
3405 {
3406         struct battery_info *di = platform_get_drvdata(dev);
3407
3408         set_low_power_interrupt(di);
3409         di->resume = true;
3410         DBG("<%s>\n", __func__);
3411         disable_vbat_low_irq(di);
3412         queue_delayed_work(di->wq, &di->battery_monitor_work,
3413                                         msecs_to_jiffies(TIMER_MS_COUNTS/2));
3414
3415         if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
3416                         di->real_soc <= 5)
3417                 wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
3418
3419
3420         return 0;
3421 }
3422 static int battery_remove(struct platform_device *dev)
3423 {
3424         struct battery_info *di = platform_get_drvdata(dev);
3425
3426         cancel_delayed_work_sync(&di->battery_monitor_work);
3427         return 0;
3428 }
3429 static void battery_shutdown(struct platform_device *dev)
3430 {
3431         struct battery_info *di = platform_get_drvdata(dev);
3432
3433         cancel_delayed_work_sync(&di->battery_monitor_work);
3434         DBG("rk818 shutdown!");
3435 }
3436 #endif
3437
3438 static struct platform_driver battery_driver = {
3439         .driver     = {
3440                 .name   = "rk818-battery",
3441                 .owner  = THIS_MODULE,
3442         },
3443
3444         .probe      = battery_probe,
3445         .remove     = battery_remove,
3446         .suspend    = battery_suspend,
3447         .resume     = battery_resume,
3448         .shutdown  = battery_shutdown,
3449 };
3450
3451 static int __init battery_init(void)
3452 {
3453         return platform_driver_register(&battery_driver);
3454 }
3455
3456 fs_initcall_sync(battery_init);
3457 static void __exit battery_exit(void)
3458 {
3459         platform_driver_unregister(&battery_driver);
3460 }
3461 module_exit(battery_exit);
3462
3463 MODULE_LICENSE("GPL");
3464 MODULE_ALIAS("platform:rk818-battery");
3465 MODULE_AUTHOR("ROCKCHIP");
3466