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