Merge remote-tracking branch 'origin/develop-3.10-next' into develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / power / rk818_battery.c
1 /*
2  * rk818  battery driver
3  *
4  * This package is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9 #include <linux/module.h>
10 #include <linux/param.h>
11 #include <linux/jiffies.h>
12 #include <linux/workqueue.h>
13 #include <linux/delay.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include <linux/proc_fs.h>
21 #include <asm/uaccess.h>
22 #include <linux/mfd/rk818.h>
23 //#include <linux/power/rk818_battery.h>
24 #include <linux/time.h>
25 #include <linux/gpio.h>
26 #include <linux/of_gpio.h>
27
28 static int dbg_enable =0;
29 module_param_named(dbg_level, dbg_enable, int, 0644);
30 #define DBG( args...) \
31         do { \
32                 if (dbg_enable) { \
33                         pr_info(args); \
34                 } \
35         } while (0)
36
37 #define VB_MOD_REG                                      0x21
38
39 #define CHRG_COMP_REG1                          0x99
40 #define CHRG_COMP_REG2                          0x9A
41 #define SUP_STS_REG                                     0xA0
42 #define USB_CTRL_REG                            0xA1
43 #define CHRG_CTRL_REG1                          0xA3
44 #define CHRG_CTRL_REG2                          0xA4
45 #define CHRG_CTRL_REG3                          0xA5
46 #define BAT_CTRL_REG                            0xA6
47 #define BAT_HTS_TS1_REG                 0xA8
48 #define BAT_LTS_TS1_REG                 0xA9
49 #define BAT_HTS_TS2_REG                 0xAA
50 #define BAT_LTS_TS2_REG                 0xAB
51
52
53 #define TS_CTRL_REG                                     0xAC
54 #define ADC_CTRL_REG                            0xAD
55
56 #define ON_SOURCE                                       0xAE
57 #define OFF_SOURCE                                      0xAF
58
59 #define GGCON                                           0xB0
60 #define GGSTS                                           0xB1
61 #define FRAME_SMP_INTERV_REG            0xB2
62 #define AUTO_SLP_CUR_THR_REG            0xB3
63
64 #define GASCNT_CAL_REG3                 0xB4
65 #define GASCNT_CAL_REG2                 0xB5
66 #define GASCNT_CAL_REG1                 0xB6
67 #define GASCNT_CAL_REG0                 0xB7
68 #define GASCNT3                                         0xB8
69 #define GASCNT2                                         0xB9
70 #define GASCNT1                                         0xBA
71 #define GASCNT0                                         0xBB
72
73 #define BAT_CUR_AVG_REGH                        0xBC
74 #define BAT_CUR_AVG_REGL                        0xBD
75
76
77 #define TS1_ADC_REGH                            0xBE
78 #define TS1_ADC_REGL                            0xBF
79 #define TS2_ADC_REGH                            0xC0
80 #define TS2_ADC_REGL                            0xC1
81
82 #define BAT_OCV_REGH                            0xC2
83 #define BAT_OCV_REGL                            0xC3
84 #define BAT_VOL_REGH                            0xC4
85 #define BAT_VOL_REGL                            0xC5
86
87 #define RELAX_ENTRY_THRES_REGH  0xC6
88 #define RELAX_ENTRY_THRES_REGL  0xC7
89 #define RELAX_EXIT_THRES_REGH           0xC8
90 #define RELAX_EXIT_THRES_REGL           0xC9
91
92 #define RELAX_VOL1_REGH                 0xCA
93 #define RELAX_VOL1_REGL                 0xCB
94 #define RELAX_VOL2_REGH                 0xCC
95 #define RELAX_VOL2_REGL                 0xCD
96
97 #define BAT_CUR_R_CALC_REGH             0xCE
98 #define BAT_CUR_R_CALC_REGL             0xCF
99 #define BAT_VOL_R_CALC_REGH             0xD0
100 #define BAT_VOL_R_CALC_REGL             0xD1
101
102 #define CAL_OFFSET_REGH                 0xD2
103 #define CAL_OFFSET_REGL                 0xD3
104
105 #define NON_ACT_TIMER_CNT_REGL  0xD4
106
107 #define VCALIB0_REGH                            0xD5
108 #define VCALIB0_REGL                            0xD6
109 #define VCALIB1_REGH                            0xD7
110 #define VCALIB1_REGL                            0xD8
111
112 #define IOFFSET_REGH                            0xDD
113 #define IOFFSET_REGL                            0xDE
114
115
116 /*0xE0 ~0xF2  data register,*/
117 #define  SOC_REG                                                0xE0
118
119 #define  REMAIN_CAP_REG3                        0xE1
120 #define  REMAIN_CAP_REG2                        0xE2
121 #define  REMAIN_CAP_REG1                        0xE3
122 #define  REMAIN_CAP_REG0                        0xE4
123
124
125
126 #define  FCC_REGL                                       0xE1
127 #define  FCC_REGH                                       0xE2                            
128
129 #define GG_EN                                           1<<7    // gasgauge module enable bit 0: disable  1:enabsle   TS_CTRL_REG  0xAC
130 //ADC_CTRL_REG
131 #define ADC_VOL_EN                                      1<<7    //if GG_EN = 0 , then the ADC of BAT voltage controlled by the bit 0:diabsle 1:enable
132 #define ADC_CUR_EN                                      1<<6    //if GG_EN = 0, then the ADC of BAT current controlled by the bit  0: disable 1: enable
133 #define ADC_TS1_EN                                      1<<5    //the ADC of TS1 controlled by the bit 0:disabsle 1:enable 
134 #define ADC_TS2_EN                                      1<<4    //the ADC of TS2 controlled by the bit 0:disabsle 1:enable 
135 #define ADC_PHASE                                       1<<3    //ADC colock phase  0:normal 1:inverted
136 #define ADC_CLK_SEL                                     7
137 /*******************************************************************
138 #define ADC_CLK_SEL_2M                          0x000
139 #define ADC_CLK_SEL_1M                          0x001
140 #define ADC_CLK_SEL_500K                        0x002
141 #define ADC_CLK_SEL_250K                        0x003
142 #define ADC_CLK_SEL_125K                        0x004
143 **********************************************************************/
144 //GGCON
145 #define CUR_SAMPL_CON_TIMES            3<<6     // ADC bat current continue sample times  00:8  01:16 10:32 11:64
146 #define ADC_OFF_CAL_INTERV                      3<<4    //ADC offset calibreation interval time 00:8min 01:16min 10:32min 11:48min
147 #define OCV_SAMPL_INTERV                        3<<2    //OCV sampling interval time 00:8min 01:16min 10:32min :11:48min
148
149 //????????
150 #define ADC_CUR_VOL_MODE                        1<<1    //ADC working in current voltage collection mode
151 #define ADC_RES_MODE                            1               //ADC working in resistor calculation mode 0:disable 1:enable
152
153 //GGSTS
154 #define RES_CUR_AVG_SEL                3<<5     //average current filter times 00:1/2  01:1/4 10:1/8 11:1/16
155 #define BAT_CON                                         1<<4    //battery first connection,edge trigger 0:NOT  1:YES
156 #define RELAX_VOL1_UPD                          1<<3    //battery voltage1 update in relax status 0: NOT 1:YE
157 #define RELAX_VOL2_UPD                          1<<2    //battery voltage2 update in relax status 0: NOT 1:YE 
158 #define RELAX_STS                                       1<<1    //battery coming into relax status  0: NOT 1:YE
159 #define IV_AVG_UPD_STS                          1<<0    //battery average voltage and current updated status 0: NOT 1:YES
160
161 //FRAME_SMP_INTERV_REG
162 #define AUTO_SLP_EN                                     1<<5    // auto sleep mode 0:disable 1:enable
163 #define FRAME_SMP_INTERV_TIME           0x1F    //
164
165 #define PLUG_IN_STS                                     1<<6
166
167 //SUP_STS_REG
168 #define BAT_EXS                                         (1<<7)
169 #define CHARGE_OFF                                      (0x00<<4)
170 #define DEAD_CHARGE                             (0x01<<4)
171 #define TRICKLE_CHARGE                          (0x02<<4)
172 #define CC_OR_CV                                        (0x03<<4)
173 #define CHARGE_FINISH                           (0x04<<4)
174 #define USB_OVER_VOL                            (0x05<<4)
175 #define BAT_TMP_ERR                                     (0x06<<4)
176 #define TIMER_ERR                                       (0x07<<4)
177 #define USB_EXIST                                       (1<<1)// usb is exists
178 #define USB_EFF                                         (1<<0)// usb is effective
179
180 //USB_CTRL_REG
181 #define CHRG_CT_EN                                      (1<<7)
182 // USB_VLIM_SEL                         
183 #define VLIM_4000MV                                     (0x00<<4)
184 #define VLIM_4100MV                                     (0x01<<4)
185 #define VLIM_4200MV                                     (0x02<<4)
186 #define VLIM_4300MV                                     (0x03<<4)
187 #define VLIM_4400MV                                     (0x04<<4)
188 #define VLIM_4500MV                                     (0x05<<4)
189 #define VLIM_4600MV                                     (0x06<<4)
190 #define VLIM_4700MV                                     (0x07<<4)
191 //USB_ILIM_SEL
192 #define ILIM_45MA                                       (0x00)
193 #define ILIM_300MA                                      (0x01)
194 #define ILIM_80MA                                       (0x02)
195 #define ILIM_820MA                                      (0x03)
196 #define ILIM_1000MA                                     (0x04)
197 #define ILIM_1200MA                                     (0x05)
198 #define ILIM_1400MA                                     (0x06)
199 #define ILIM_1600MA                                     (0x07)
200 #define ILIM_1800MA                                     (0x08)
201 #define ILIM_2000MA                                     (0x09)
202 #define ILIM_2200MA                                     (0x0A)
203 #define ILIM_2400MA                                     (0x0B)
204 #define ILIM_2600MA                                     (0x0C)
205 #define ILIM_2800MA                                     (0x0D)
206 #define ILIM_3000MA                                     (0x0E)
207
208 //CHRG_CTRL_REG
209 #define CHRG_EN                                         (0x01<<7)
210 // CHRG_VOL_SEL
211
212 #define CHRG_VOL4050                            (0x00<<4)
213 #define CHRG_VOL4100                            (0x01<<4)
214 #define CHRG_VOL4150                            (0x02<<4)
215 #define CHRG_VOL4200                            (0x03<<4)
216 #define CHRG_VOL4300                            (0x04<<4)
217 #define CHRG_VOL4350                            (0x05<<4)
218
219 //CHRG_CUR_SEL
220 #define CHRG_CUR1000mA                  (0x00)
221 #define CHRG_CUR1200mA                  (0x01)
222 #define CHRG_CUR1400mA                  (0x02)
223 #define CHRG_CUR1600mA                  (0x03)
224 #define CHRG_CUR1800mA                  (0x04)
225 #define CHRG_CUR2000mA                  (0x05)
226 #define CHRG_CUR2200mA                  (0x06)
227 #define CHRG_CUR2400mA                  (0x07)
228 #define CHRG_CUR2600mA                  (0x08)
229 #define CHRG_CUR2800mA                  (0x09)
230 #define CHRG_CUR3000mA                  (0x0A)
231
232
233 #define DRIVER_VERSION                          "1.0.0"
234 #define ROLEX_SPEED                             100 * 1000
235
236 #define CHARGING                                        0x01
237 #define DISCHARGING                                     0x00
238
239 #define TIMER_MS_COUNTS                 1000
240 #define MAX_CHAR                                        0x7F
241 #define MAX_UNSIGNED_CHAR                       0xFF
242 #define MAX_INT                                         0x7FFFFFFF
243 #define MAX_UNSIGNED_INT                        0xFFFF
244 #define MAX_INT8                                        0x7F
245 #define MAX_UINT8                                       0xFF
246
247 /* Voltage and Current buffers */
248 #define AV_SIZE                                         5
249
250 static int16_t av_v[AV_SIZE];
251 static int16_t av_c[AV_SIZE];
252
253 static uint16_t av_v_index;
254 static uint16_t av_c_index;
255
256 #define INTERPOLATE_MAX         1000
257 //#define OCV_TABLE_SIZE  
258  
259 struct battery_info{
260         struct device           *dev;
261         struct cell_state       cell;
262         struct power_supply     bat;
263         struct power_supply     ac;
264         struct power_supply     usb;
265         struct delayed_work work;
266 //      struct i2c_client       *client;
267
268         struct rk818            *rk818; 
269         struct battery_platform_data *platform_data;
270         struct notifier_block battery_nb;
271         struct workqueue_struct *wq;
272         struct delayed_work     battery_monitor_work;
273         struct delayed_work     charge_check_work;
274
275         int                             ac_online;
276         int                             usb_online;
277         int                             health;
278         int                             tempreture;
279         int                             present;
280         int                             status;
281
282         int                             bat_current;
283         int                             current_avg;
284         int                             current_offset;
285
286         int                             voltage;
287         int                             voltage_avg;
288         int                             voltage_offset;
289         int                             voltage_ocv;
290
291         int                             poweroff_voltage;
292         int                             warnning_voltage;
293         int                             poweron_voltage;
294
295         int                             design_capacity;
296         int                             fcc;
297         int                             new_fcc;
298         u32                             qmax;
299         int                             remain_capacity;
300         int                             warnning_capacity;
301         int                             nac;
302         int                             temp_nac;
303
304         int                             real_soc;
305         int                             display_soc;
306         int                             temp_soc;
307
308         int                             soc_counter;
309
310         int                             dod0;
311         int                             dod0_capacity;
312         int                             dod1;
313         int                             dod1_capacity;
314
315         int                             temperature;
316
317         int                             time2empty;
318         int                             time2full;
319
320         int                             *ocv_table;
321         int                             ocv_size;
322         int                             *res_table;
323
324         int                             current_k;//(ICALIB0,ICALIB1)
325         int                             current_b;
326
327         int                             voltage_k;//VCALIB0 VCALIB1
328         int                             voltage_b;
329         
330         int                             relax_entry_thres;
331         int                             relax_exit_thres;
332
333         int                             relax_vol1;
334         int                             relax_vol2;
335         
336         u8                              sleep_cur;
337         u8                              sleep_smp_time;
338         u8                              check_count;
339 //      u32                             status;
340         struct timeval  soc_timer;
341         struct timeval  change_timer;
342
343         bool                    resume;
344         int                             charge_otg;
345
346 };
347         struct battery_info *data;
348
349
350 u32 interpolate(int value, u32 *table, int size)
351 {
352         uint8_t i;
353         uint16_t d;
354
355         for (i = 0; i < size; i++){
356                 if (value < table[i])
357                         break;
358         }
359
360         if ((i > 0)  && (i < size)) {
361                 d = (value - table[i-1]) * (INTERPOLATE_MAX/(size-1));
362                 d /=  table[i] - table[i-1];
363                 d = d + (i-1) * (INTERPOLATE_MAX/(size-1));
364         } else {
365                 d = i * ((INTERPOLATE_MAX+size/2)/size);
366         }
367
368         if (d > 1000)
369                 d = 1000;
370
371         return d;
372 }
373 /* Returns (a * b) / c */
374 int32_t ab_div_c(u32 a, u32 b, u32 c)
375 {
376         bool sign;
377         u32 ans = MAX_INT;
378         int32_t tmp;
379
380         sign = ((((a^b)^c) & 0x80000000) != 0);
381
382         if (c != 0) {
383                 if (sign)
384                         c = -c;
385
386                 tmp = ((int32_t) a*b + (c>>1)) / c;
387
388                 if (tmp < MAX_INT)
389                         ans = tmp;
390         }
391
392         if (sign)
393                 ans = -ans;
394
395         return ans;
396 }
397
398 static  int32_t abs_int(int32_t x)
399 {
400         return (x > 0) ? x : -x;
401 }
402
403 /* Returns diviation between 'size' array members */
404 uint16_t diff_array(int16_t *arr, uint8_t size)
405 {
406         uint8_t i;
407         uint32_t diff = 0;
408
409         for (i = 0; i < size-1; i++)
410                 diff += abs_int(arr[i] - arr[i+1]);
411
412         if (diff > MAX_UNSIGNED_INT)
413                 diff = MAX_UNSIGNED_INT;
414
415         return (uint16_t) diff;
416 }
417
418
419 static enum power_supply_property rk818_battery_props[] = {
420
421         POWER_SUPPLY_PROP_STATUS,
422         POWER_SUPPLY_PROP_CURRENT_NOW,
423         POWER_SUPPLY_PROP_VOLTAGE_NOW,
424         POWER_SUPPLY_PROP_PRESENT,
425         POWER_SUPPLY_PROP_PRESENT,
426         POWER_SUPPLY_PROP_CAPACITY,
427 #if 0
428         POWER_SUPPLY_PROP_STATUS,
429         POWER_SUPPLY_PROP_PRESENT,
430         POWER_SUPPLY_PROP_VOLTAGE_NOW,
431         POWER_SUPPLY_PROP_CURRENT_NOW,
432         POWER_SUPPLY_PROP_CAPACITY,
433         POWER_SUPPLY_PROP_TEMP,
434         POWER_SUPPLY_PROP_TECHNOLOGY,
435         POWER_SUPPLY_PROP_HEALTH,
436         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
437         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
438         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
439 #endif
440
441 };
442
443 static enum power_supply_property rk818_battery_ac_props[] = {
444         POWER_SUPPLY_PROP_ONLINE,
445 };
446 static enum power_supply_property rk818_battery_usb_props[] = {
447         POWER_SUPPLY_PROP_ONLINE,
448 };
449
450
451 static int battery_read(struct rk818 *rk818, u8 reg, u8 buf[], unsigned len)
452 {
453         int ret;
454         ret = rk818_i2c_read(rk818, reg,  len,buf);
455         return ret; 
456 }
457
458 static int battery_write(struct rk818 *rk818, u8 reg, u8 const buf[], unsigned len)
459 {
460         int ret; 
461         ret = rk818_i2c_write(rk818, reg,(int)len, *buf);
462         return ret;
463 }
464 static void dump_gauge_register(struct battery_info *di)
465 {
466         int i = 0;
467         char buf;
468         DBG("%s dump charger register start: \n",__FUNCTION__);
469         for(i = 0xAC;i < 0xDE; i ++){
470                  battery_read(di ->rk818, i, &buf,1);
471                  DBG(" the register is  0x%02x, the value is 0x%02x\n ", i, buf);
472         }
473         DBG("demp end!\n");
474
475 }
476
477 static void dump_charger_register(struct battery_info *di)
478 {
479
480         int i = 0;
481         char buf;
482         DBG("%s dump the register start: \n",__FUNCTION__);
483         for(i = 0x99;i < 0xAB; i ++){
484                  battery_read(di ->rk818, i, &buf,1);
485                  DBG(" the register is  0x%02x, the value is 0x%02x\n ", i, buf);
486         }
487         DBG("demp end!\n");
488
489 }
490 #if 0
491 //POWER_SUPPLY_PROP_STATUS
492 static int rk818_battery_status(struct battery_info *di)
493 {
494         return di->status;
495 }
496 //POWER_SUPPLY_PROP_PRESENT,
497 static int rk818_battery_present(struct rk818_battery_info *di)
498 {
499         return 1;
500 }
501 #endif
502 /* OCV Lookup table 
503  * Open Circuit Voltage (OCV) correction routine. This function estimates SOC,
504  * based on the voltage.
505  */
506 static int _voltage_to_capacity(struct battery_info * di, int voltage)
507 {
508         u32  *ocv_table;
509         int   ocv_size;
510         u32 tmp;
511         
512         ocv_table = di->platform_data->battery_ocv;
513         ocv_size = di->platform_data->ocv_size;
514  //     ocv_table = di->ocv_table;
515  //     ocv_size = di->ocv_size;
516         tmp = interpolate(voltage, ocv_table, ocv_size);
517         di->temp_soc =  ab_div_c(tmp, MAX_PERCENTAGE, INTERPOLATE_MAX);
518         di->temp_nac= ab_div_c(tmp, di->fcc, INTERPOLATE_MAX);
519         DBG("temp = %d real-soc =%d nac= %d, fcc = %d\n", tmp, di->temp_soc, di->temp_nac,di->fcc);
520         return 0;
521 }
522 //POWER_SUPPLY_PROP_CURRENT_NOW,
523 static int  _get_average_current(struct battery_info *di)
524 {
525         u8 buf;//[2];
526         int ret;
527         int current_now;
528         int temp;
529         
530         ret = battery_read(di->rk818,BAT_CUR_AVG_REGL, &buf, 1);
531         if(ret < 0){
532                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGL");
533                 return ret;
534         }
535         current_now = buf;
536         ret = battery_read(di->rk818,BAT_CUR_AVG_REGH, &buf, 1);
537         if(ret < 0){
538                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
539                 return ret;
540         }
541         current_now |= (buf<<8);
542
543         if(current_now &0x800)
544                 current_now -= 4096;
545         
546 //      temp = current_now*1000*90/14/4096*500/521;
547         temp = current_now*1506/1000;//1000*90/14/4096*500/521;
548
549         if(ret < 0){
550                 dev_err(di->dev, "error reading BAT_CUR_AVG_REGH");
551                 return ret;
552         }
553
554         DBG("%s, average current current_now = %d current = %d\n",__FUNCTION__, current_now, temp);
555         return temp;
556
557 }
558
559 #define to_device_info(x) container_of((x), \
560                                 struct battery_info, bat);
561
562 static int rk818_battery_get_property(struct power_supply *psy,
563                                         enum power_supply_property psp,
564                                         union power_supply_propval *val)
565 {
566         int ret = 0;
567         struct battery_info *di = to_device_info(psy);
568         
569         switch (psp) {
570                 case POWER_SUPPLY_PROP_CURRENT_NOW:
571                         val->intval = di->current_avg;
572                 break;
573         
574         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
575         case POWER_SUPPLY_PROP_PRESENT:
576                 val->intval = di->voltage;// rk818_battery_voltage(di);
577                 if (psp == POWER_SUPPLY_PROP_PRESENT)
578                         val->intval = val->intval <= 0 ? 0 : 1;
579                 break;
580
581         case POWER_SUPPLY_PROP_CAPACITY:
582                 if(di->real_soc < 0)
583                         di->real_soc = 0;
584                 if(di->real_soc > 100)
585                         di->real_soc = 100;
586                 val->intval =di->real_soc;
587                 //DBG("POWER_SUPPLY_PROP_CAPACITY = %d,   val->intval =%d\n", di->real_soc, val->intval);
588                 break;
589         case POWER_SUPPLY_PROP_HEALTH:
590                 val->intval = POWER_SUPPLY_HEALTH_GOOD;//rk818_battery_health(di);
591                 break;
592
593         case POWER_SUPPLY_PROP_STATUS:
594                 val->intval = di->status;
595                 //DBG("gBatStatus=%d\n",val->intval);
596                 break;
597
598         default:
599                 return -EINVAL;
600         }
601
602         return ret;
603 }
604
605 #define to_ac_device_info(x) container_of((x), \
606                                 struct battery_info, ac);
607
608 static int rk818_battery_ac_get_property(struct power_supply *psy,
609                         enum power_supply_property psp,
610                         union power_supply_propval *val)
611 {
612         //DBG("%s:%d psp = %d\n",__FUNCTION__,__LINE__,psp);
613         int ret = 0;
614         struct battery_info *di = to_ac_device_info(psy);
615
616         switch (psp) {
617         case POWER_SUPPLY_PROP_ONLINE:  
618                         val->intval = di->ac_online;    /*discharging*/
619                         //DBG("%s:%d val->intval = %d   di->status = %d\n",__FUNCTION__,__LINE__,val->intval, di->status);
620                 break;
621                 
622         default:
623                 ret = -EINVAL;
624                 break;
625         }
626         return ret;
627 }
628
629 #define to_usb_device_info(x) container_of((x), \
630                                 struct battery_info, usb);
631
632 static int rk818_battery_usb_get_property(struct power_supply *psy,
633                         enum power_supply_property psp,
634                         union power_supply_propval *val)
635 {
636         int ret = 0;
637         struct battery_info *di = to_usb_device_info(psy);
638
639         switch (psp) {
640         case POWER_SUPPLY_PROP_ONLINE:  
641                 val->intval = di->usb_online;   /*discharging*/
642                 //DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
643                 break;
644                 
645         default:
646                 ret = -EINVAL;
647                 break;
648         }
649         return ret;
650 }
651
652
653 static void battery_powersupply_init(struct battery_info *di)
654 {
655         di->bat.name = "BATTERY";
656         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
657         di->bat.properties = rk818_battery_props;
658         di->bat.num_properties = ARRAY_SIZE(rk818_battery_props);
659         di->bat.get_property = rk818_battery_get_property;
660         
661         di->ac.name = "AC";
662         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
663         di->ac.properties = rk818_battery_ac_props;
664         di->ac.num_properties = ARRAY_SIZE(rk818_battery_ac_props);
665         di->ac.get_property = rk818_battery_ac_get_property;
666
667         di->usb.name = "USB";
668         di->usb.type = POWER_SUPPLY_TYPE_USB;
669         di->usb.properties = rk818_battery_usb_props;
670         di->usb.num_properties = ARRAY_SIZE(rk818_battery_usb_props);
671         di->usb.get_property = rk818_battery_usb_get_property;
672 }
673
674 //enabsle GG_EN 
675 static int  _gauge_enable(struct battery_info *di)
676 {
677         int ret;
678         u8 buf;
679         DBG("%s start \n", __FUNCTION__);
680         ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
681         DBG("_gauge_enable read-%d\n", buf);
682         
683         if(ret < 0){
684                 dev_err(di->dev, "error reading TS_CTRL_REG");
685                 return ret;
686         }
687         if(!(buf & GG_EN)){
688                 buf |= GG_EN;
689                 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1);  //enable 
690                 ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
691                 return 0;
692         }
693
694         DBG("%s,%d\n",__FUNCTION__, buf);
695         return 0;
696         
697 }
698
699 #if 0
700
701 static int  _gauge_disable(struct battery_info *di)
702 {
703         int ret;
704         u8 buf;
705
706         ret = battery_read(di->rk818,TS_CTRL_REG, &buf, 1);
707         if(ret < 0){
708                 dev_err(di->dev, "error reading TS_CTRL_REG");
709                 return ret;
710         }
711         if((buf & GG_EN)){
712                 buf &= (~0x80);//GG_EN
713                 ret = battery_write(di->rk818, TS_CTRL_REG, &buf, 1);  //enable 
714                 return 0;
715         }
716         return 0;
717 }
718
719 static int _set_auto_sleep_cur(struct battery_info *di, u8 value)
720 {
721         int ret;
722         u8 buf;
723         buf = value;
724         ret = battery_write(di->rk818, AUTO_SLP_CUR_THR_REG, &buf, 1);  //enable 
725         return 0;
726 }
727 static int _set_sleep_smp_time(struct battery_info *di, u8 value)
728 {
729
730         int ret;
731         u8 temp;
732         u8 buf;
733
734         ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
735         if(ret < 0){
736                 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
737                 return ret;
738         }
739
740         temp = (buf&(AUTO_SLP_EN))|value;
741         ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &temp, 1);  //enable 
742
743         return 0;
744 }
745
746 static int _autosleep_enable(struct battery_info *di)
747 {
748         int ret;
749         u8 buf;
750
751         ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
752         if(ret < 0){
753                 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
754                 return ret;
755         }
756         if(!(buf & AUTO_SLP_EN)){
757                 buf |= AUTO_SLP_EN;
758                 ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &buf, 1);  //enable 
759                 return 0;
760         }
761
762         _set_auto_sleep_cur(di, di->sleep_cur);  // <di->sleep_cur  , into sleep-mode
763         _set_sleep_smp_time(di, di->sleep_smp_time); // time of adc work , sleep-mode
764
765         
766         return 0;
767
768
769 }
770
771 static int _autosleep_disable(struct battery_info *di)
772 {
773         int ret;
774         u8 buf;
775
776         ret = battery_read(di->rk818,FRAME_SMP_INTERV_REG, &buf, 1);
777         if(ret < 0){
778                 dev_err(di->dev, "error reading FRAME_SMP_INTERV_REG");
779                 return ret;
780         }
781         if((buf & AUTO_SLP_EN)){
782                 buf &= (~AUTO_SLP_EN);
783                 ret = battery_write(di->rk818, FRAME_SMP_INTERV_REG, &buf, 1);  //enable 
784                 return 0;
785         }
786         return 0;
787
788
789 }
790
791 #endif
792 static int rk818_battery_voltage(struct battery_info *di)
793 {
794         int ret;
795         int voltage_now = 0;
796         u8 buf;
797         int temp;
798 #if 1
799         ret = battery_read(di->rk818,BAT_VOL_REGL, &buf, 1);
800         temp = buf;
801         ret = battery_read(di->rk818,BAT_VOL_REGH,&buf, 1);
802         temp |= buf<<8;
803 #endif
804
805         //ret = battery_read(di->rk818,BAT_VOL_REGH, buf, 2);
806         if(ret < 0){
807                 dev_err(di->dev, "error reading BAT_VOL_REGH");
808                 return ret;
809         }
810
811         //voltage_now = temp;//(buf[0]<<8)|buf[1];
812         voltage_now = di ->voltage_k*temp + di->voltage_b;
813
814         DBG("the rea-time voltage is %d\n",voltage_now);
815         return voltage_now;
816 }
817
818 static int _get_OCV_voltage(struct battery_info *di)
819 {
820         int ret;
821         int voltage_now = 0;
822         u8 buf;
823         int temp;
824 #if 1
825         ret = battery_read(di->rk818,BAT_OCV_REGL, &buf, 1);
826         temp = buf;
827         ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 1);
828         temp |= buf<<8;
829 #endif 
830
831         //ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 2);
832         if(ret < 0){
833                 dev_err(di->dev, "error reading BAT_OCV_REGH");
834                 return ret;
835         }
836
837         //voltage_now = temp;//(buf[0]<<8)|buf[1];
838         voltage_now = di ->voltage_k*temp + di->voltage_b;
839         DBG("the OCV voltage is %d\n", voltage_now);
840
841         return voltage_now;
842 }
843 #if 0
844 static int _get_ts1_adc(struct battery_info *di)
845 {
846         int ret;
847         int temp = 0;
848         u8 buf;
849
850         ret = battery_read(di->rk818,TS1_ADC_REGL, &buf, 1);
851         temp = buf;
852         ret = battery_read(di->rk818,TS1_ADC_REGH, &buf, 1);
853         temp  = (buf<<8);
854
855         return temp;
856 }
857
858 static int _get_ts2_adc(struct battery_info *di)
859 {
860         int ret;
861         int temp = 0;
862         u8 buf;
863 #if 1
864         ret = battery_read(di->rk818,TS2_ADC_REGL, &buf, 1);
865         temp = buf;
866         ret = battery_read(di->rk818,TS2_ADC_REGH, &buf, 1);
867         temp |= buf<<8;
868 #endif
869
870         return temp;
871 }
872 #endif
873 static void  _capacity_init(struct battery_info *di, u32 capacity)
874 {
875
876         u8 buf;
877         u32 capacity_ma;
878
879         capacity_ma = capacity*2201;//36*14/900*4096/521*500;
880         DBG("%s WRITE GANCNT_CAL_REG  %d\n", __FUNCTION__, capacity_ma);
881         do{
882                 buf =   (capacity_ma>>24)&0xff;
883                 battery_write(di->rk818, GASCNT_CAL_REG3, &buf,1);
884                 buf =   (capacity_ma>>16)&0xff;
885                 battery_write(di->rk818, GASCNT_CAL_REG2, &buf,1);
886                 buf =   (capacity_ma>>8)&0xff;
887                 battery_write(di->rk818, GASCNT_CAL_REG1, &buf,1);
888                 buf  =  (capacity_ma&0xff)|0x01;
889                 battery_write(di->rk818, GASCNT_CAL_REG0, &buf,1);
890                 battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
891
892         }while(buf == 0);
893         return;
894
895 }
896
897 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
898 {
899
900         u8 buf;
901         u32 capacity_ma;
902
903         if(capacity >= di ->qmax){
904             capacity = di ->qmax;       
905         }
906         capacity_ma = capacity;
907 //      DBG("%s WRITE GANCNT_CAL_REG  %d\n", __FUNCTION__, capacity_ma);
908         buf =   (capacity_ma>>24)&0xff;
909         battery_write(di->rk818, REMAIN_CAP_REG3, &buf,1);
910         buf =   (capacity_ma>>16)&0xff;
911         battery_write(di->rk818, REMAIN_CAP_REG2, &buf,1);
912         buf =   (capacity_ma>>8)&0xff;
913         battery_write(di->rk818, REMAIN_CAP_REG1, &buf,1);
914         buf  =  (capacity_ma&0xff)|0x01;
915         battery_write(di->rk818, REMAIN_CAP_REG0, &buf,1);
916         
917         return;
918
919 }
920
921 static int _get_remain_capacity(struct battery_info *di)
922 {
923         int ret;
924         int temp = 0;
925         u8 buf;
926         u32 capacity;
927
928         ret = battery_read(di->rk818,REMAIN_CAP_REG3, &buf, 1);
929         temp = buf<<24;
930         ret = battery_read(di->rk818,REMAIN_CAP_REG2, &buf, 1);
931         temp |= buf<<16;
932         ret = battery_read(di->rk818,REMAIN_CAP_REG1, &buf, 1);
933         temp |= buf<<8;
934         ret = battery_read(di->rk818,REMAIN_CAP_REG0, &buf, 1);
935         temp |= buf;
936
937         capacity = temp;///4096*900/14/36*500/521;
938         DBG("%s GASCNT_CAL_REG %d  capacity =%d \n",__FUNCTION__, temp, capacity);
939         return capacity;
940
941 }
942
943
944 static int _get_capacity(struct battery_info *di)
945 {
946         int ret;
947         int temp = 0;
948         u8 buf;
949         u32 capacity;
950
951         ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 1);
952         temp = buf<<24;
953         ret = battery_read(di->rk818,GASCNT_CAL_REG2, &buf, 1);
954         temp |= buf<<16;
955         ret = battery_read(di->rk818,GASCNT_CAL_REG1, &buf, 1);
956         temp |= buf<<8;
957         ret = battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
958         temp |= buf;
959
960         capacity = temp/2201;///4096*900/14/36*500/521;
961         //DBG("%s GASCNT_CAL_REG %d  capacity =%d \n",__FUNCTION__, temp, capacity);
962         return capacity;
963
964 }
965
966 static int _get_realtime_capacity(struct battery_info *di)
967 {
968
969         int ret;
970         int temp = 0;
971         u8 buf;
972         u32 capacity;
973
974         ret = battery_read(di->rk818,GASCNT3, &buf, 1);
975         temp = buf<<24;
976         ret = battery_read(di->rk818,GASCNT2, &buf, 1);
977         temp |= buf<<16;
978         ret = battery_read(di->rk818,GASCNT1, &buf, 1);
979         temp |= buf<<8;
980         ret = battery_read(di->rk818,GASCNT0, &buf, 1);
981         temp |= buf;
982 //      ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 4);
983 //      temp = buf[0] << 24 | buf[1] << 24 | buf[2] << 24 |buf[3] ;
984         capacity = temp/2201;///4096*900/14/36*500/521;
985         //DBG("%s GASCNT =  0x%4x  capacity =%d \n",__FUNCTION__, temp,capacity);
986         return capacity;
987         
988
989 }
990
991 static int _get_vcalib0(struct battery_info *di)
992 {
993
994         int ret;
995         int temp = 0;
996         u8 buf;
997 #if 1
998         ret = battery_read(di->rk818,VCALIB0_REGL, &buf, 1);
999         temp = buf;
1000         ret = battery_read(di->rk818,VCALIB0_REGH, &buf, 1);
1001         temp |= buf<<8;
1002 #endif
1003         //ret = battery_read(di->rk818,VCALIB0_REGH, &buf,2);
1004         //temp  = (buf[0]<<8)|buf[1];
1005
1006         DBG("%s voltage0 offset vale is %d\n",__FUNCTION__, temp);
1007         return temp;
1008 }
1009
1010 static int _get_vcalib1(struct  battery_info *di)
1011 {
1012
1013         int ret;
1014         int temp = 0;
1015         u8 buf;
1016         #if 1
1017         ret = battery_read(di->rk818,VCALIB1_REGL, &buf, 1);
1018         temp = buf;
1019         ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 1);
1020         temp |= buf<<8;
1021         #endif
1022         //ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 2);
1023         //temp  = (buf[0]<<8)|buf[1];
1024         DBG("%s voltage1 offset vale is %d\n",__FUNCTION__, temp);
1025         return temp;
1026 }
1027
1028 static void _get_voltage_offset_value(struct battery_info *di)
1029 {
1030         int vcalib0,vcalib1;
1031
1032         vcalib0 = _get_vcalib0(di);
1033         vcalib1 = _get_vcalib1(di);
1034
1035         di->voltage_k = (4200 - 3000)/(vcalib1 - vcalib0);
1036         di->voltage_b = 4200 - di->voltage_k*vcalib1;
1037         
1038         return;
1039 }
1040
1041 static int _get_ioffset(struct battery_info *di)
1042 {
1043
1044         int ret;
1045         int temp = 0;
1046         u8 buf;
1047
1048         ret = battery_read(di->rk818,IOFFSET_REGL, &buf, 1);
1049         temp = buf;
1050         ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 1);
1051         temp |= buf<<8;
1052
1053         //ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 2);
1054         //temp  = (buf[0]<<8)|buf[1];
1055
1056         DBG("%s IOFFSET value is %d\n", __FUNCTION__, temp);
1057         return temp;
1058 }
1059
1060 static int _set_cal_offset(struct battery_info *di, u32 value)
1061 {
1062         int ret;
1063         int temp = 0;
1064         u8 buf;
1065         DBG("%s\n",__FUNCTION__);
1066         buf = value&0xff;
1067         ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);  //enable 
1068         buf = (value >> 8)&0xff;
1069         ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);  //enable 
1070         DBG("%s set CAL_OFFSET_REG %d\n",__FUNCTION__, temp);
1071
1072         return 0;
1073 }
1074
1075 static bool _is_first_poweron(struct  battery_info * di)
1076 {
1077         u8 buf;
1078         u8 temp;
1079         u8 ret;
1080
1081         ret = battery_read(di->rk818,GGSTS, &buf, 1);
1082         DBG("%s GGSTS value is %2x \n", __FUNCTION__, buf );
1083         if( buf&BAT_CON){
1084                 buf &=~(BAT_CON);
1085                 do{
1086                         battery_write(di->rk818,GGSTS, &buf, 1);
1087                         battery_read(di->rk818,GGSTS, &temp, 1);
1088                 }while(temp&BAT_CON);
1089                 return true;
1090         }
1091         return false;
1092 }
1093
1094
1095 #if 0
1096 static bool fg_check_relaxed(struct  battery_info * di)//(struct cell_state *cell)
1097 {
1098         struct cell_state *cell = &di->cell;
1099
1100         struct timeval now;
1101
1102         if (!cell->sleep) {
1103                 if (abs_int(di->current_avg) <=
1104                         cell->config->ocv->sleep_enter_current) {
1105                         if (cell->sleep_samples < MAX_UINT8)
1106                                 cell->sleep_samples++;
1107
1108                         if (cell->sleep_samples >=
1109                                 cell->config->ocv->sleep_enter_samples) {
1110                                 /* Entering sleep mode */
1111                                 do_gettimeofday(&cell->sleep_timer);
1112                                 do_gettimeofday(&cell->el_sleep_timer);
1113                                 cell->sleep = true;
1114                                 cell->calibrate = true;
1115                         }
1116                 } else {
1117                         cell->sleep_samples = 0;
1118                 }               
1119         } else {
1120                 /* The battery cell is Sleeping, checking if need to exit
1121                    sleep mode count number of seconds that cell spent in
1122                    sleep */
1123                 do_gettimeofday(&now);
1124                 cell->cumulative_sleep +=
1125                         now.tv_sec + cell->el_sleep_timer.tv_sec;
1126                 do_gettimeofday(&cell->el_sleep_timer);
1127
1128                 /* Check if we need to reset Sleep */
1129                 if (abs_int(di->current_avg) >
1130                         cell->config->ocv->sleep_exit_current) {
1131
1132                         if (abs_int(di->current_avg) >
1133                                 cell->config->ocv->sleep_exit_current) {
1134
1135                                 if (cell->sleep_samples < MAX_UINT8)
1136                                         cell->sleep_samples++;
1137
1138                         } else {
1139                                 cell->sleep_samples = 0;
1140                         }
1141
1142                         /* Check if we need to reset a Sleep timer */
1143                         if (cell->sleep_samples >
1144                                 cell->config->ocv->sleep_exit_samples) {
1145                                 /* Exit sleep mode */
1146
1147                                 cell->sleep_timer.tv_sec = 0;
1148                                 cell->sleep = false;
1149                                 cell->relax = false;
1150                         }
1151                 } else {
1152                         cell->sleep_samples = 0;
1153
1154                         if (!cell->relax) {             
1155
1156                                 if (now.tv_sec-cell->sleep_timer.tv_sec >
1157                                         cell->config->ocv->relax_period) {
1158                                         cell->relax = true;
1159                                         cell->calibrate = true;
1160                                 }
1161                         }
1162                 }
1163         }
1164         
1165         return cell->relax;
1166 }
1167
1168 /* Checks for right conditions for OCV correction */
1169 static bool fg_can_ocv(struct battery_info * di)//(struct cell_state *cell)
1170 {
1171         struct cell_state *cell = &di->cell;
1172 #if  1
1173         /* Voltage should be stable */
1174         if (cell->config->ocv->voltage_diff <= diff_array(av_v, AV_SIZE))
1175                 return false;
1176
1177         /* Current should be stable */
1178         if (cell->config->ocv->current_diff <= diff_array(av_c, AV_SIZE))
1179                 return false;
1180 #endif
1181         /* SOC should be out of Flat Zone */
1182         if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1183                 && (di->real_soc <= cell->config->ocv->flat_zone_high))
1184                         return false;
1185
1186         /* Current should be less then SleepEnterCurrent */
1187         if (abs_int(di->current_avg) >= cell->config->ocv->sleep_enter_current)
1188                 return false;
1189
1190         /* Don't allow OCV below EDV1, unless OCVbelowEDV1 is set */
1191         //if (cell->edv1 && !cell->config->ocv_below_edv1)
1192         //      return false;
1193
1194         return true;
1195 }
1196
1197 #endif
1198
1199 /* Sets the battery Voltage, and recalculates the average voltage */
1200 void fg_set_voltage(int16_t voltage)
1201 {
1202         int16_t i;
1203         int32_t tmp = 0;
1204
1205         /* put voltage reading int the buffer and update average */
1206         av_v_index++;
1207         av_v_index %= AV_SIZE;
1208         av_v[av_v_index] = voltage;
1209         for (i = 0; i < AV_SIZE; i++)
1210                 tmp += av_v[i];
1211 }
1212
1213
1214 /* Sets the battery Current, and recalculates the average current */
1215 void fg_set_current( int16_t cur)
1216 {
1217         int16_t i;
1218         int32_t tmp = 0;
1219
1220         /* put current reading int the buffer and update average */
1221         av_c_index++;
1222         av_c_index %= AV_SIZE;
1223         av_c[av_c_index] = cur;
1224         for (i = 0; i < AV_SIZE; i++)
1225                 tmp += av_c[i];
1226
1227 }
1228
1229 static int _copy_soc(struct  battery_info * di, u8 save_soc)
1230 {
1231         u8 soc;
1232
1233         soc = save_soc;
1234         //soc = 85;
1235         battery_write(di->rk818, SOC_REG, &soc, 1);
1236         battery_read(di->rk818, SOC_REG, &soc, 1);
1237         DBG(" the save soc-reg = %d \n", soc);
1238         
1239         return 0;
1240 }
1241
1242 static int _rsoc_init(struct  battery_info * di)
1243 {
1244         int vol;
1245         u8 temp;
1246         u32 remain_capacity;
1247         
1248         vol = di->voltage_ocv; //_get_OCV_voltage(di);
1249         DBG("OCV voltage = %d\n" , di->voltage_ocv);
1250         if(_is_first_poweron(di)){
1251
1252                 DBG(" %s this is first poweron\n", __FUNCTION__);
1253                  _voltage_to_capacity(di, di->voltage_ocv);
1254                 di->real_soc = di->temp_soc;
1255                 di->nac        = di->temp_nac;
1256         }else{
1257                 DBG(" %s this is  not not not first poweron\n", __FUNCTION__);
1258                 battery_read(di->rk818,SOC_REG, &temp, 1);
1259                 remain_capacity = _get_remain_capacity(di);
1260                 if(remain_capacity >= di->qmax)
1261                         remain_capacity = di->qmax;
1262                 DBG("saved SOC_REG = 0x%8x\n", temp);
1263                 DBG("saved remain_capacity = %d\n", remain_capacity);
1264                 
1265                 
1266                 di->real_soc = temp;
1267                 //di->nac = di->fcc*temp/100;
1268                 di->nac = remain_capacity;
1269         }
1270         return 0;
1271 }
1272
1273 static int _get_soc(struct   battery_info *di)
1274 {
1275
1276         return di->remain_capacity * 100 / di->fcc;
1277 }
1278
1279 static u8 get_charge_status(struct  battery_info * di)
1280 {
1281         u8 status;
1282         u8 ret =0;
1283
1284         battery_read(di->rk818, SUP_STS_REG,  &status, 1);
1285         DBG("%s ----- SUP_STS_REG(0xA0) = 0x%02x\n", __FUNCTION__, status);
1286         status &= ~(0x07<<4);
1287         switch(status){
1288                 case CHARGE_OFF:
1289                         ret =  CHARGE_OFF;
1290                         break;
1291                 case DEAD_CHARGE:
1292                         ret = DEAD_CHARGE;
1293                         break;
1294                 case  TRICKLE_CHARGE://                         (0x02<<4)
1295                         ret = DEAD_CHARGE;
1296                         break;
1297                 case  CC_OR_CV: //                                      (0x03<<4)
1298                         ret = CC_OR_CV;
1299                         break;
1300                 case  CHARGE_FINISH://                          (0x04<<4)
1301                         ret = CHARGE_FINISH;
1302                         break;
1303
1304                 case  USB_OVER_VOL://                           (0x05<<4)
1305                         ret = USB_OVER_VOL;
1306                         break;
1307
1308                 case  BAT_TMP_ERR://                                    (0x06<<4)
1309                         ret = BAT_TMP_ERR;
1310                         break;
1311
1312                 case  TIMER_ERR://                                      (0x07<<4)
1313                         ret = TIMER_ERR;
1314                         break;
1315
1316                 case  USB_EXIST://                                      (1<<1)// usb is exists
1317                         ret = USB_EXIST;
1318                         break;
1319
1320                 case  USB_EFF://                                                (1<<0)// usb is effective
1321                         ret = USB_EFF;
1322                         break;
1323                 default:
1324                         return -EINVAL;
1325         }
1326
1327         return ret;
1328
1329 }
1330
1331 static void rk818_battery_charger_init(struct  battery_info *di)
1332 {
1333         u8 chrg_ctrl_reg1,usb_ctrl_reg;// chrg_ctrl_reg2;
1334         u8 sup_sts_reg;
1335         
1336
1337         DBG("%s  start\n",__FUNCTION__);
1338
1339         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1340         battery_read(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1341 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1342         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1343
1344         DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1345         //usb_ctrl_reg &= (0x01<<7);
1346         usb_ctrl_reg |= (VLIM_4400MV | ILIM_1200MA)|(0x01<<7);
1347         
1348         chrg_ctrl_reg1 &= (0x00);
1349         chrg_ctrl_reg1 |=(0x01<<7)| (CHRG_VOL4200| CHRG_CUR1400mA);
1350         
1351         sup_sts_reg &= ~(0x01<<3);
1352         sup_sts_reg |= (0x01<<2);
1353
1354         battery_write(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1355         
1356         battery_write(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1357         //battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1358         battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1359
1360         
1361         battery_read(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1362 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1363         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1364         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1365         DBG(" new  usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x, SUP_STS_REG=0x%2x\n ",
1366                                                                                                 usb_ctrl_reg, chrg_ctrl_reg1,sup_sts_reg);
1367
1368         DBG("%s  end\n",__FUNCTION__);
1369
1370 }
1371
1372 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
1373
1374 void charge_disable_open_otg(struct  battery_info *di, int value )
1375 {
1376 //      u8 chrg_ctrl_reg1,dcdc_en_reg;
1377         if(value  == 1){
1378                 DBG("1    ---- charge disable \n");
1379                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0<< 7); //ldo9
1380                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); //ldo9
1381         }
1382         if(value == 0){
1383                 DBG("1    ---- charge disable \n");
1384                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); //ldo9
1385                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7); //ldo9
1386         }
1387
1388 }
1389
1390 static void  fg_init(struct battery_info *di)
1391 {
1392         DBG("%s start\n",__FUNCTION__);
1393         _gauge_enable(di);
1394         _get_voltage_offset_value(di); //get the volatege offset
1395 //      _autosleep_enable(di);
1396         rk818_battery_charger_init(di);
1397 //      _set_relax_thres(di);
1398         di->current_offset = _get_ioffset(di); //get the current offset , the value write to the CAL_OFFSET
1399         _set_cal_offset(di,di->current_offset+42);
1400
1401         di->voltage  = rk818_battery_voltage(di);       
1402         di->voltage_ocv = _get_OCV_voltage(di);
1403         _rsoc_init( di);
1404         _capacity_init(di, di->nac);
1405 //      _get_realtime_capacity( di);
1406         di->remain_capacity = _get_capacity(di);
1407         // _get_realtime_capacity( di);
1408         do_gettimeofday(&di->soc_timer);
1409         di->change_timer = di->soc_timer;
1410 #if 0
1411         for (i = 0; i < AV_SIZE; i++) {
1412                 av_v[i] = di->voltage;
1413                 av_c[i] = 0;
1414         }
1415         av_v_index = 0;
1416         av_c_index = 0;
1417 #endif
1418         dump_gauge_register(di);
1419         dump_charger_register(di);
1420         DBG("nac =%d , remain_capacity = %d \n"
1421                 " OCV_voltage =%d, voltage =%d \n",
1422                 di->nac, di->remain_capacity,
1423                 di->voltage_ocv, di->voltage);
1424 }
1425
1426 #if 0
1427 static int capacity_changed(struct battery_info *di)
1428 {
1429         s32 acc_value, samples = 0;
1430         int ret;
1431         int acc_q;
1432         
1433 //      fg_set_voltage(&di->cell, di->voltage_mV);
1434         //fg_set_current(&di->cell, (int16_t)(di->current_uA/1000));
1435
1436
1437         return 0;
1438 }
1439
1440 static void rk818_battery_info(struct battery_info *di)
1441 {
1442         //di->status = rk818_battery_status(di);
1443         //di->voltage = rk818_battery_voltage(di);
1444         di->present = rk818_battery_present(di);
1445         di->bat_current = _get_average_current(di);
1446         di->temp_soc= rk818_battery_soc(di);
1447         di->tempreture =rk818_battery_temperature(di);
1448         di->health = rk818_battery_health(di);
1449 }
1450 #endif
1451
1452 static void rk818_battery_display_smooth(struct battery_info *di)
1453 {
1454         int status;
1455         u8 charge_status;
1456 //      int relaxmode_soc;
1457 //      int coulomp_soc, soc;
1458
1459         status = di->status;
1460         if(status == POWER_SUPPLY_STATUS_CHARGING){
1461                 //DBG("charging smooth ... \n");
1462                 if(1){
1463                         //DBG("   BATTERY NOT RELAX MODE \n");
1464                         DBG("di->remain_capacity =%d, di->fcc  = %d\n", di->remain_capacity,di->fcc);
1465                         di->temp_soc = _get_soc(di);
1466                         charge_status = get_charge_status( di);
1467                         if(di->temp_soc >= 100){
1468                                 di->temp_soc = 100;
1469                                 //di->status = POWER_SUPPLY_STATUS_FULL;
1470                         }
1471                         
1472                         do_gettimeofday(&di->soc_timer);
1473
1474                         if(di->temp_soc!= di->real_soc){                        
1475                                 di->change_timer = di->soc_timer;
1476                                 if(di->real_soc < di->temp_soc)
1477                                         di->real_soc++;
1478                                 else
1479                                         di->real_soc =di->temp_soc;
1480                         }
1481
1482                 //      DBG("charge_status =0x%x\n", charge_status);
1483                         if((charge_status ==CHARGE_FINISH) && (di->real_soc < 100)){            
1484                                 DBG("CHARGE_FINISH  di->real_soc < 100 \n ");
1485                                 if((di->soc_counter < 10)){
1486                                         di->soc_counter ++;
1487                                 }else{
1488                                         di->soc_counter = 0;
1489                                         if(di->real_soc < 100){
1490                                                 di->real_soc ++;
1491                                                 // _save_rsoc_nac( di);
1492                                         }
1493                                 }
1494                         }
1495
1496                 }
1497                 if(di->real_soc <= 0)
1498                         di->real_soc = 0;
1499                 if(di->real_soc >= 100){
1500                         di->real_soc = 100;
1501                         di->status = POWER_SUPPLY_STATUS_FULL;
1502                 }
1503                                 
1504         }
1505         if(status == POWER_SUPPLY_STATUS_DISCHARGING){
1506                 //DBG("discharging smooth ... \n");
1507                 di->temp_soc = _get_soc(di);
1508                 do_gettimeofday(&di->soc_timer);
1509                 if(di->temp_soc!= di->real_soc){
1510                         di->change_timer = di->soc_timer;
1511                         di->real_soc = di->temp_soc;
1512                         // _save_rsoc_nac( di);
1513                 }
1514                 if(di->real_soc <= 0)
1515                         di->real_soc = 0;
1516                 if(di->real_soc >= 100){
1517                         di->real_soc = 100;
1518                 }
1519 #if 0
1520                 if(!_is_relax_mode( di)){
1521                         DBG("   BATTERY NOT RELAX MODE \n");
1522                         di->temp_soc = _get_soc(di);
1523                         do_gettimeofday(&di->soc_timer);
1524                         if(di->temp_soc!= di->real_soc){
1525                                 di->change_timer = di->soc_timer;
1526                                 di->real_soc = di->temp_soc;
1527                                  _save_rsoc_nac( di);
1528                         }
1529
1530                 }else{
1531                         DBG("BATTERY  RELAX MODE\n ");
1532                         //relaxmode_soc = relax_soc(di);
1533                         coulomp_soc    = _get_soc(di);
1534                         soc =coulomp_soc;// (coulomp_soc*20 + relaxmode_soc*80)/100;
1535
1536                         if((soc > di->real_soc)&&(di->soc_counter < 10)){
1537                                 di->soc_counter ++;
1538
1539                         }else{
1540                                 di->soc_counter = 0;
1541                                 if(di->real_soc < 100){
1542                                         di->real_soc --;
1543                                          _save_rsoc_nac( di);
1544                                 }
1545                         }
1546                         DBG(" remaxmode_soc = %d , coulomp-soc =%d  soc = %d\n",relaxmode_soc, coulomp_soc, soc);
1547                 }
1548 #endif
1549
1550         }
1551         //DBG("%s   exit \n", __FUNCTION__);
1552 }
1553
1554 static void rk818_battery_update_status(struct battery_info *di)
1555 {
1556
1557         di->voltage              = rk818_battery_voltage( di);
1558         di->current_avg       = _get_average_current(di);
1559         di->remain_capacity = _get_realtime_capacity( di);
1560          _get_capacity(di);
1561
1562         rk818_battery_display_smooth(di);
1563         
1564         DBG("%s\n"
1565                 "voltage = %d, current-avg = %d\n"      
1566                 "fcc = %d ,remain_capacity =%d\n"
1567                 "real_soc = %d\n",
1568                         __FUNCTION__,
1569                         di->voltage, di->current_avg,
1570                         di->fcc, di->remain_capacity,
1571                         di->real_soc
1572                 );
1573 }
1574 extern int dwc_vbus_status(void);
1575 extern int get_gadget_connect_flag(void);
1576
1577  //state of charge ----running
1578 static int  get_charging_status(struct battery_info *di)
1579 {
1580
1581 ////////////////////////////////////////////
1582 #if 0
1583         u8 usb_ctrl_reg;// chrg_ctrl_reg2;
1584         
1585
1586
1587         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1588 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1589
1590         DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1591         usb_ctrl_reg &= (0x01<<7);
1592         usb_ctrl_reg |= (  ILIM_300MA);
1593 #endif
1594 /////////////////////////////////////////
1595
1596 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1597         int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
1598         int vbus_status =  dwc_vbus_status();
1599         if (1 == vbus_status) {
1600                 if (0 == get_gadget_connect_flag()){ 
1601                         if (++di->check_count >= 5){
1602
1603                                 di->ac_online = 1;
1604                                 di->usb_online = 0;
1605                         }else{
1606                                 di->ac_online =0;
1607                                 di->usb_online = 1;
1608
1609                         }
1610                 }else{
1611
1612                                 di->ac_online =0;
1613                                 di->usb_online = 1;
1614                 }
1615                 
1616         }else{
1617                 if (2 == vbus_status) {
1618
1619                                 di->ac_online = 1;
1620                                 di->usb_online = 0;
1621                 }else{
1622
1623                                 di->ac_online = 0;
1624                                 di->usb_online = 0;
1625                 }
1626                 di->check_count=0;
1627
1628         }
1629         return usb_status;
1630
1631 }
1632
1633 static void get_battery_status(struct battery_info *di)
1634 {
1635
1636         u8 buf;
1637         int ret;
1638         ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1639         //int vbus_status =  dwc_vbus_status();
1640
1641         if(buf&PLUG_IN_STS){
1642         //if(vbus_status != 0){
1643                 get_charging_status(di);
1644                 di->status = POWER_SUPPLY_STATUS_CHARGING;
1645         //      di->ac_online = 1;
1646                 if(di->real_soc == 100)
1647                         di->status = POWER_SUPPLY_STATUS_FULL;
1648         }
1649         else{
1650                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1651                 di->ac_online =0;
1652                 di->usb_online =0;
1653
1654         }
1655         //DBG("%s ,di->status = %d\n",__FUNCTION__, di->status);
1656 }
1657
1658 static void rk818_battery_work(struct work_struct *work)
1659 {
1660         u8 buf;
1661         struct battery_info *di = container_of(work,
1662         struct battery_info, battery_monitor_work.work);
1663         int vbus_status ;
1664         get_battery_status(di);
1665         battery_read(di->rk818,0x00, &buf, 1);
1666         DBG("RTC  =0x%2x\n ", buf);
1667         battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1668         //DBG("VB_MOD_REG  =%2x, the value is %2x\n ", VB_MOD_REG,buf);
1669         battery_read(di->rk818,SUP_STS_REG, &buf, 1);
1670 //      DBG("SUP_STS_REG  =%2x, the value is %2x\n ", SUP_STS_REG,buf);
1671         vbus_status =  dwc_vbus_status();
1672 //      DBG("vbus_status  =%2x\n ", vbus_status);
1673
1674         rk818_battery_update_status(di);
1675
1676         if(di ->resume){
1677                 di ->resume = false;
1678                 di->real_soc = _get_soc(di); 
1679                 if(di->real_soc <= 0)
1680                         di->real_soc = 0;
1681                 if(di->real_soc >= 100)
1682                         di->real_soc = 100;
1683         }
1684         if ((di->ac_online == 0 )&&( di->usb_online ==0)&&(di->remain_capacity > di->qmax +10)){
1685                 _capacity_init(di, di->qmax);
1686                 di->remain_capacity  = _get_realtime_capacity( di);
1687         }
1688
1689         //DBG("soc  =  %d", di->real_soc);
1690          _copy_soc(di, di->real_soc);
1691         _save_remain_capacity(di, di->remain_capacity);
1692         power_supply_changed(&di->bat);
1693 //      power_supply_changed(&di->usb);
1694         power_supply_changed(&di->ac);
1695         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1696
1697 }
1698
1699 static void rk818_battery_charge_check_work(struct work_struct *work)
1700 {
1701         struct battery_info *di = container_of(work,
1702                                                 struct battery_info, charge_check_work.work);
1703         charge_disable_open_otg(di,di->charge_otg);
1704 }
1705
1706 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
1707
1708 int register_battery_notifier(struct notifier_block *nb)
1709 {
1710         return blocking_notifier_chain_register(&battery_chain_head, nb);
1711 }
1712 EXPORT_SYMBOL_GPL(register_battery_notifier);
1713
1714 int unregister_battery_notifier(struct notifier_block *nb)
1715 {
1716         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
1717 }
1718 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
1719
1720 int battery_notifier_call_chain(unsigned long val)
1721 {
1722         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
1723                         == NOTIFY_BAD) ? -EINVAL : 0;
1724 }
1725 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
1726 static int battery_notifier_call(struct notifier_block *nb,
1727                 unsigned long event, void *data)
1728 {
1729         struct battery_info *di=
1730                 container_of(nb, struct battery_info, battery_nb);
1731
1732         switch (event) {
1733                 case 0:
1734                         DBG(" CHARGE enable \n");
1735                         di ->charge_otg = 0;
1736                         queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1737                         break;
1738
1739                 case 1:
1740                         di ->charge_otg  = 1;
1741                         queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1742
1743                         DBG("charge disable OTG enable \n");
1744                         break;
1745                 default:
1746                         return NOTIFY_OK;
1747                 }
1748         return NOTIFY_OK;
1749 }
1750 #ifdef CONFIG_OF
1751 static int rk818_battery_parse_dt(struct rk818 *rk818)
1752 {
1753         struct device_node *regs,*rk818_pmic_np;
1754         struct battery_platform_data *data;
1755         struct cell_config *cell_cfg;
1756         struct property *prop;
1757         u32 out_value;
1758         int i, length, ret;
1759
1760         rk818_pmic_np = of_node_get(rk818->dev->of_node);
1761         if (!rk818_pmic_np) {
1762                 printk("could not find pmic sub-node\n");
1763                 return -EINVAL;
1764         }
1765
1766         regs = of_find_node_by_name(rk818_pmic_np, "battery");
1767         if (!regs){
1768                 printk("could not find battery sub-node\n");
1769                 return -EINVAL;
1770         }
1771
1772         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
1773         memset(data, 0, sizeof(*data));
1774
1775         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
1776         /* determine the number of brightness levels */
1777         prop = of_find_property(regs, "ocv_table", &length);
1778         if (!prop)
1779                 return -EINVAL;
1780         data->ocv_size= length / sizeof(u32);
1781         /* read brightness levels from DT property */
1782         if (data->ocv_size > 0) {
1783                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
1784                 data->battery_ocv= devm_kzalloc(rk818->dev, size, GFP_KERNEL);
1785                 if (!data->battery_ocv)
1786                         return -ENOMEM;
1787                 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
1788                 DBG("the battery OCV TABLE : ");
1789                 for(i =0; i< data->ocv_size; i++ )
1790                         DBG("%d ", data->battery_ocv[i]);
1791                 DBG("\n");
1792                 if (ret < 0)
1793                         return ret;
1794         }
1795         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
1796         if (ret < 0)
1797                 return ret;
1798         data->max_charger_currentmA= out_value;
1799         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
1800         if (ret < 0)
1801                 return ret;
1802         data->max_charger_voltagemV= out_value;
1803         ret = of_property_read_u32(regs, "design_capacity", &out_value);
1804         if (ret < 0)
1805                 return ret;
1806         cell_cfg->design_capacity  = out_value;
1807         ret = of_property_read_u32(regs, "design_qmax", &out_value);
1808         if (ret < 0)
1809                 return ret;
1810         cell_cfg->design_qmax =out_value;
1811         data->cell_cfg =cell_cfg;
1812         rk818->battery_data = data;
1813         DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
1814         DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
1815         DBG("design_capacity :%d\n", cell_cfg->design_capacity);
1816         DBG("design_qmax :%d\n", cell_cfg->design_qmax);
1817
1818         return 0;
1819 }
1820 static struct of_device_id rk818_battery_of_match[] = {
1821         { .compatible = "rk818_battery" },
1822         { }
1823 };
1824
1825 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
1826 #else
1827 static int rk818_battery_parse_dt(struct device *dev)
1828 {
1829         return -ENODEV;
1830 }
1831 #endif
1832
1833 static int  battery_probe(struct platform_device *pdev)
1834 {
1835         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
1836 //      struct battery_platform_data *pdata ;//= rk818_platform_data->battery_data;
1837 //      struct battery_platform_data defdata ;//= rk818_platform_data->battery_data;
1838         struct battery_info *di;
1839         struct ocv_config *ocv;
1840         struct edv_config *edv;
1841         int ret;
1842         
1843         DBG("%s is  the  battery driver version %s\n",__FUNCTION__,DRIVER_VERSION);
1844          rk818_battery_parse_dt(chip);
1845
1846         di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1847         if (!di) {
1848                 dev_err(&pdev->dev, "no memory for state\n");
1849                 ret = -ENOMEM;
1850                 return ret;
1851         }
1852         ocv = devm_kzalloc(&pdev->dev, sizeof(*ocv), GFP_KERNEL);
1853         if (!ocv) {
1854                 dev_err(&pdev->dev, "ocv  no memory for state\n");
1855                 ret = -ENOMEM;
1856                 return ret;
1857         }
1858         edv = devm_kzalloc(&pdev->dev, sizeof(*edv), GFP_KERNEL);
1859         if (!edv) {
1860                 dev_err(&pdev->dev, "edv  no memory for state\n");
1861                 ret = -ENOMEM;
1862                 return ret;
1863         }
1864
1865         di->rk818 = chip;
1866 #if 0
1867         di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
1868         if (!di->platform_data) {
1869                 kfree(di);
1870                 return -ENOMEM;
1871         }
1872 #endif
1873 //      data = di;
1874         platform_set_drvdata(pdev, di);
1875         /*apply battery cell configuration*/
1876         //di->cell.config = di->platform_data->cell_cfg;
1877         di->platform_data = chip->battery_data;
1878         di->platform_data->cell_cfg = chip->battery_data->cell_cfg;
1879         di->platform_data->cell_cfg->ocv = ocv;
1880         di->platform_data->cell_cfg->edv = edv;
1881         di->design_capacity = chip->battery_data->cell_cfg->design_capacity;
1882         di->qmax = chip->battery_data->cell_cfg->design_qmax;
1883         di->fcc = di->design_capacity;
1884         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1885
1886         battery_powersupply_init(di);
1887         fg_init(di);
1888         ret = power_supply_register(&pdev->dev, &di->bat);
1889         if (ret) {
1890                 dev_dbg(&pdev->dev, "failed to register main battery\n");
1891                 goto batt_failed;
1892         }
1893         ret = power_supply_register(&pdev->dev, &di->usb);
1894         if (ret) {
1895                 dev_dbg(&pdev->dev, "failed to register usb power supply\n");
1896                 goto usb_failed;
1897         }
1898         ret = power_supply_register(&pdev->dev, &di->ac);
1899         if (ret) {
1900                 dev_dbg(&pdev->dev, "failed to register ac power supply\n");
1901                 goto ac_failed;
1902         }
1903
1904         di->wq = create_singlethread_workqueue("battery-work");
1905         INIT_DELAYED_WORK(&di->battery_monitor_work,rk818_battery_work);
1906         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1907         //queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1908         INIT_DELAYED_WORK(&di->charge_check_work,rk818_battery_charge_check_work);
1909         
1910         di->battery_nb.notifier_call = battery_notifier_call;
1911         register_battery_notifier(&di->battery_nb);
1912         printk("battery probe ok... \n");
1913         return ret;
1914         
1915 ac_failed:
1916         power_supply_unregister(&di->ac);
1917 usb_failed:
1918         power_supply_unregister(&di->usb);
1919 batt_failed:
1920         power_supply_unregister(&di->bat);
1921         return ret;
1922 }
1923
1924 static int  battery_remove(struct platform_device *dev)
1925 {
1926         return 0;
1927 }
1928 #if 1
1929 static int battery_suspend(struct platform_device *dev,pm_message_t state)
1930 {
1931         struct battery_info *di = platform_get_drvdata(dev);
1932         DBG("%s--------------------\n",__FUNCTION__);
1933         if(di == NULL)
1934                 printk("battery NULL di\n");
1935         cancel_delayed_work(&di ->battery_monitor_work);
1936         DBG("%s---------end--------\n",__FUNCTION__);
1937
1938         return 0;
1939 }
1940
1941 static int battery_resume(struct platform_device *dev)
1942 {
1943         
1944         u8 buf;
1945         int ret;
1946         struct battery_info *di = platform_get_drvdata(dev);
1947
1948         ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1949
1950 //      struct battery_info *di  = platform_get_drvdata(dev);
1951         DBG("%s--------------------\n",__FUNCTION__);
1952         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1953         di ->resume = true;
1954         DBG("charge--status       0x%02x--------------------buf = 0x%02x\n", get_charge_status( di),buf);
1955
1956         return 0;
1957 }
1958 #endif
1959 static struct platform_driver battery_driver = {
1960         .probe          = battery_probe,
1961         .remove         = battery_remove,
1962         .suspend                = battery_suspend,
1963         .resume         = battery_resume,
1964
1965         .driver         = {
1966                 .name   = "rk818-battery",
1967                 //.pm   = &pm_ops,
1968                 .of_match_table = of_match_ptr(rk818_battery_of_match),
1969         },
1970 };
1971
1972 static int __init battery_init(void)
1973 {
1974         return platform_driver_register(&battery_driver);
1975 }
1976 fs_initcall_sync(battery_init);
1977 static void __exit battery_exit(void)
1978 {
1979         platform_driver_unregister(&battery_driver);
1980 }
1981 module_exit(battery_exit);
1982
1983 MODULE_LICENSE("GPL");
1984 MODULE_ALIAS("platform:rk818-battery");
1985 MODULE_AUTHOR("ROCKCHIP");
1986
1987
1988
1989
1990
1991
1992  
1993