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