Merge tag 'lsk-android-14.05' 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 void  _set_relax_thres(struct battery_info *di)
793 {
794         u8 buf;
795         int enter_thres,exit_thres;
796         struct cell_state *cell = &di->cell;
797
798         enter_thres = (cell->config->ocv->sleep_enter_current)*1000/1506;
799         exit_thres = (cell->config->ocv->sleep_exit_current)*1000/1506;
800
801         buf  =  enter_thres&0xff;
802         battery_write(di->rk818, RELAX_ENTRY_THRES_REGL, &buf,1);
803         buf =   (enter_thres>>8)&0xff;
804         battery_write(di->rk818, RELAX_ENTRY_THRES_REGH, &buf,1);
805
806         buf  =  exit_thres&0xff;
807         battery_write(di->rk818, RELAX_EXIT_THRES_REGL, &buf,1);
808         buf  =  (exit_thres>>8)&0xff;
809         battery_write(di->rk818, RELAX_EXIT_THRES_REGH, &buf,1);
810
811         //set sample time
812          battery_read(di->rk818,GGCON, &buf, 1);
813         buf &= ~(3<<2);
814         battery_write(di->rk818, GGCON, &buf,1);
815  }
816
817 static int rk818_battery_voltage(struct battery_info *di)
818 {
819         int ret;
820         int voltage_now = 0;
821         u8 buf;
822         int temp;
823 #if 1
824         ret = battery_read(di->rk818,BAT_VOL_REGL, &buf, 1);
825         temp = buf;
826         ret = battery_read(di->rk818,BAT_VOL_REGH,&buf, 1);
827         temp |= buf<<8;
828 #endif
829
830         //ret = battery_read(di->rk818,BAT_VOL_REGH, buf, 2);
831         if(ret < 0){
832                 dev_err(di->dev, "error reading BAT_VOL_REGH");
833                 return ret;
834         }
835
836         //voltage_now = temp;//(buf[0]<<8)|buf[1];
837         voltage_now = di ->voltage_k*temp + di->voltage_b;
838
839         DBG("the rea-time voltage is %d\n",voltage_now);
840         return voltage_now;
841 }
842
843 static int _get_OCV_voltage(struct battery_info *di)
844 {
845         int ret;
846         int voltage_now = 0;
847         u8 buf;
848         int temp;
849 #if 1
850         ret = battery_read(di->rk818,BAT_OCV_REGL, &buf, 1);
851         temp = buf;
852         ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 1);
853         temp |= buf<<8;
854 #endif 
855
856         //ret = battery_read(di->rk818,BAT_OCV_REGH, &buf, 2);
857         if(ret < 0){
858                 dev_err(di->dev, "error reading BAT_OCV_REGH");
859                 return ret;
860         }
861
862         //voltage_now = temp;//(buf[0]<<8)|buf[1];
863         voltage_now = di ->voltage_k*temp + di->voltage_b;
864         DBG("the OCV voltage is %d\n", voltage_now);
865
866         return voltage_now;
867 }
868 #if 0
869 static int _get_ts1_adc(struct battery_info *di)
870 {
871         int ret;
872         int temp = 0;
873         u8 buf;
874
875         ret = battery_read(di->rk818,TS1_ADC_REGL, &buf, 1);
876         temp = buf;
877         ret = battery_read(di->rk818,TS1_ADC_REGH, &buf, 1);
878         temp  = (buf<<8);
879
880         return temp;
881 }
882
883 static int _get_ts2_adc(struct battery_info *di)
884 {
885         int ret;
886         int temp = 0;
887         u8 buf;
888 #if 1
889         ret = battery_read(di->rk818,TS2_ADC_REGL, &buf, 1);
890         temp = buf;
891         ret = battery_read(di->rk818,TS2_ADC_REGH, &buf, 1);
892         temp |= buf<<8;
893 #endif
894
895         return temp;
896 }
897 #endif
898 static void  _capacity_init(struct battery_info *di, u32 capacity)
899 {
900
901         u8 buf;
902         u32 capacity_ma;
903
904         capacity_ma = capacity*2201;//36*14/900*4096/521*500;
905         DBG("%s WRITE GANCNT_CAL_REG  %d\n", __FUNCTION__, capacity_ma);
906         do{
907                 buf =   (capacity_ma>>24)&0xff;
908                 battery_write(di->rk818, GASCNT_CAL_REG3, &buf,1);
909                 buf =   (capacity_ma>>16)&0xff;
910                 battery_write(di->rk818, GASCNT_CAL_REG2, &buf,1);
911                 buf =   (capacity_ma>>8)&0xff;
912                 battery_write(di->rk818, GASCNT_CAL_REG1, &buf,1);
913                 buf  =  (capacity_ma&0xff)|0x01;
914                 battery_write(di->rk818, GASCNT_CAL_REG0, &buf,1);
915                 battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
916
917         }while(buf == 0);
918         return;
919
920 }
921
922 static void  _save_remain_capacity(struct battery_info *di, u32 capacity)
923 {
924
925         u8 buf;
926         u32 capacity_ma;
927
928         if(capacity >= di ->qmax){
929             capacity = di ->qmax;       
930         }
931         capacity_ma = capacity;
932 //      DBG("%s WRITE GANCNT_CAL_REG  %d\n", __FUNCTION__, capacity_ma);
933         buf =   (capacity_ma>>24)&0xff;
934         battery_write(di->rk818, REMAIN_CAP_REG3, &buf,1);
935         buf =   (capacity_ma>>16)&0xff;
936         battery_write(di->rk818, REMAIN_CAP_REG2, &buf,1);
937         buf =   (capacity_ma>>8)&0xff;
938         battery_write(di->rk818, REMAIN_CAP_REG1, &buf,1);
939         buf  =  (capacity_ma&0xff)|0x01;
940         battery_write(di->rk818, REMAIN_CAP_REG0, &buf,1);
941         
942         return;
943
944 }
945
946 static int _get_remain_capacity(struct battery_info *di)
947 {
948         int ret;
949         int temp = 0;
950         u8 buf;
951         u32 capacity;
952
953         ret = battery_read(di->rk818,REMAIN_CAP_REG3, &buf, 1);
954         temp = buf<<24;
955         ret = battery_read(di->rk818,REMAIN_CAP_REG2, &buf, 1);
956         temp |= buf<<16;
957         ret = battery_read(di->rk818,REMAIN_CAP_REG1, &buf, 1);
958         temp |= buf<<8;
959         ret = battery_read(di->rk818,REMAIN_CAP_REG0, &buf, 1);
960         temp |= buf;
961
962         capacity = temp;///4096*900/14/36*500/521;
963         DBG("%s GASCNT_CAL_REG %d  capacity =%d \n",__FUNCTION__, temp, capacity);
964         return capacity;
965
966 }
967
968
969 static int _get_capacity(struct battery_info *di)
970 {
971         int ret;
972         int temp = 0;
973         u8 buf;
974         u32 capacity;
975
976         ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 1);
977         temp = buf<<24;
978         ret = battery_read(di->rk818,GASCNT_CAL_REG2, &buf, 1);
979         temp |= buf<<16;
980         ret = battery_read(di->rk818,GASCNT_CAL_REG1, &buf, 1);
981         temp |= buf<<8;
982         ret = battery_read(di->rk818,GASCNT_CAL_REG0, &buf, 1);
983         temp |= buf;
984
985         capacity = temp/2201;///4096*900/14/36*500/521;
986         //DBG("%s GASCNT_CAL_REG %d  capacity =%d \n",__FUNCTION__, temp, capacity);
987         return capacity;
988
989 }
990
991 static int _get_realtime_capacity(struct battery_info *di)
992 {
993
994         int ret;
995         int temp = 0;
996         u8 buf;
997         u32 capacity;
998
999         ret = battery_read(di->rk818,GASCNT3, &buf, 1);
1000         temp = buf<<24;
1001         ret = battery_read(di->rk818,GASCNT2, &buf, 1);
1002         temp |= buf<<16;
1003         ret = battery_read(di->rk818,GASCNT1, &buf, 1);
1004         temp |= buf<<8;
1005         ret = battery_read(di->rk818,GASCNT0, &buf, 1);
1006         temp |= buf;
1007 //      ret = battery_read(di->rk818,GASCNT_CAL_REG3, &buf, 4);
1008 //      temp = buf[0] << 24 | buf[1] << 24 | buf[2] << 24 |buf[3] ;
1009         capacity = temp/2201;///4096*900/14/36*500/521;
1010         //DBG("%s GASCNT =  0x%4x  capacity =%d \n",__FUNCTION__, temp,capacity);
1011         return capacity;
1012         
1013
1014 }
1015
1016 static int _get_relax_vol1(struct battery_info *di)
1017 {
1018         int ret;
1019         int temp = 0,voltage_now;
1020         u8 buf;
1021
1022         ret = battery_read(di->rk818,RELAX_VOL1_REGL, &buf, 1);
1023         temp = buf;
1024         ret = battery_read(di->rk818,RELAX_VOL1_REGH, &buf, 1);
1025         temp |= buf<<8;
1026 //      ret = battery_read(di->rk818,RELAX_VOL1_REGH, &buf, 2);
1027 //      temp  = (buf[0]<<8)|buf[1];
1028         voltage_now = di ->voltage_k*temp + di->voltage_b;
1029
1030         return voltage_now;
1031 }
1032
1033 static int _get_relax_vol2(struct battery_info *di)
1034 {
1035         int ret;
1036         int temp = 0,voltage_now;
1037         u8 buf;
1038
1039         ret = battery_read(di->rk818,RELAX_VOL2_REGL, &buf, 1);
1040         temp = buf;
1041         ret = battery_read(di->rk818,RELAX_VOL2_REGH, &buf, 1);
1042         temp |= buf<<8;
1043 //      ret = battery_read(di->rk818,RELAX_VOL2_REGH, &buf, 2);
1044 //      temp  = (buf[0]<<8)|buf[1];
1045         voltage_now = di ->voltage_k*temp + di->voltage_b;
1046
1047         return temp;
1048 }
1049
1050 static bool  _is_relax_mode(struct battery_info *di)
1051 {
1052         int ret;
1053         u8 status;
1054         int relax_vol1,relax_vol2;
1055
1056         struct cell_state *cell = &di->cell;
1057         
1058         ret = battery_read(di->rk818,GGSTS, &status, 1);
1059         DBG(" GGSTS the value is %2x the realsoc = %d \n", status, di->real_soc);
1060         
1061         if(!(status&RELAX_STS))
1062                 return false;
1063         if((!(status&RELAX_VOL1_UPD))||(!(status&RELAX_VOL2_UPD)))
1064                 return false;
1065         else{
1066                 if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1067                         && (di->real_soc <= cell->config->ocv->flat_zone_high))
1068                         return false;                           
1069                 relax_vol1 = _get_relax_vol1(di);
1070                 relax_vol2 = _get_relax_vol2(di);
1071                 DBG("relax_vol1 = %d relax_vol2 =%d \n", relax_vol1,relax_vol2);
1072                 if((abs_int((relax_vol2 - relax_vol1)))/8/60 > 4 )
1073                         return false;
1074         }
1075
1076         return true;
1077 }
1078
1079
1080 static int relax_soc(struct battery_info *di)
1081 {
1082         //int relax_soc;
1083         _voltage_to_capacity( di, di->voltage);
1084         return di->temp_soc;
1085 }
1086
1087 static int _get_vcalib0(struct battery_info *di)
1088 {
1089
1090         int ret;
1091         int temp = 0;
1092         u8 buf;
1093 #if 1
1094         ret = battery_read(di->rk818,VCALIB0_REGL, &buf, 1);
1095         temp = buf;
1096         ret = battery_read(di->rk818,VCALIB0_REGH, &buf, 1);
1097         temp |= buf<<8;
1098 #endif
1099         //ret = battery_read(di->rk818,VCALIB0_REGH, &buf,2);
1100         //temp  = (buf[0]<<8)|buf[1];
1101
1102         DBG("%s voltage0 offset vale is %d\n",__FUNCTION__, temp);
1103         return temp;
1104 }
1105
1106 static int _get_vcalib1(struct  battery_info *di)
1107 {
1108
1109         int ret;
1110         int temp = 0;
1111         u8 buf;
1112         #if 1
1113         ret = battery_read(di->rk818,VCALIB1_REGL, &buf, 1);
1114         temp = buf;
1115         ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 1);
1116         temp |= buf<<8;
1117         #endif
1118         //ret = battery_read(di->rk818,VCALIB1_REGH, &buf, 2);
1119         //temp  = (buf[0]<<8)|buf[1];
1120         DBG("%s voltage1 offset vale is %d\n",__FUNCTION__, temp);
1121         return temp;
1122 }
1123
1124 static void _get_voltage_offset_value(struct battery_info *di)
1125 {
1126         int vcalib0,vcalib1;
1127
1128         vcalib0 = _get_vcalib0(di);
1129         vcalib1 = _get_vcalib1(di);
1130
1131         di->voltage_k = (4200 - 3000)/(vcalib1 - vcalib0);
1132         di->voltage_b = 4200 - di->voltage_k*vcalib1;
1133         
1134         return;
1135 }
1136
1137 static int _get_ioffset(struct battery_info *di)
1138 {
1139
1140         int ret;
1141         int temp = 0;
1142         u8 buf;
1143
1144         ret = battery_read(di->rk818,IOFFSET_REGL, &buf, 1);
1145         temp = buf;
1146         ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 1);
1147         temp |= buf<<8;
1148
1149         //ret = battery_read(di->rk818,IOFFSET_REGH, &buf, 2);
1150         //temp  = (buf[0]<<8)|buf[1];
1151
1152         DBG("%s IOFFSET value is %d\n", __FUNCTION__, temp);
1153         return temp;
1154 }
1155
1156 static int _set_cal_offset(struct battery_info *di, u32 value)
1157 {
1158         int ret;
1159         int temp = 0;
1160         u8 buf;
1161         DBG("%s\n",__FUNCTION__);
1162         buf = value&0xff;
1163         ret = battery_write(di->rk818, CAL_OFFSET_REGL, &buf, 1);  //enable 
1164         buf = (value >> 8)&0xff;
1165         ret = battery_write(di->rk818, CAL_OFFSET_REGH, &buf, 1);  //enable 
1166         DBG("%s set CAL_OFFSET_REG %d\n",__FUNCTION__, temp);
1167
1168         return 0;
1169 }
1170
1171 static bool _is_first_poweron(struct  battery_info * di)
1172 {
1173         u8 buf;
1174         u8 temp;
1175         u8 ret;
1176
1177         ret = battery_read(di->rk818,GGSTS, &buf, 1);
1178         DBG("%s GGSTS value is %2x \n", __FUNCTION__, buf );
1179         if( buf&BAT_CON){
1180                 buf &=~(BAT_CON);
1181                 do{
1182                         battery_write(di->rk818,GGSTS, &buf, 1);
1183                         battery_read(di->rk818,GGSTS, &temp, 1);
1184                 }while(temp&BAT_CON);
1185                 return true;
1186         }
1187         return false;
1188 }
1189
1190
1191 #if 0
1192 static bool fg_check_relaxed(struct  battery_info * di)//(struct cell_state *cell)
1193 {
1194         struct cell_state *cell = &di->cell;
1195
1196         struct timeval now;
1197
1198         if (!cell->sleep) {
1199                 if (abs_int(di->current_avg) <=
1200                         cell->config->ocv->sleep_enter_current) {
1201                         if (cell->sleep_samples < MAX_UINT8)
1202                                 cell->sleep_samples++;
1203
1204                         if (cell->sleep_samples >=
1205                                 cell->config->ocv->sleep_enter_samples) {
1206                                 /* Entering sleep mode */
1207                                 do_gettimeofday(&cell->sleep_timer);
1208                                 do_gettimeofday(&cell->el_sleep_timer);
1209                                 cell->sleep = true;
1210                                 cell->calibrate = true;
1211                         }
1212                 } else {
1213                         cell->sleep_samples = 0;
1214                 }               
1215         } else {
1216                 /* The battery cell is Sleeping, checking if need to exit
1217                    sleep mode count number of seconds that cell spent in
1218                    sleep */
1219                 do_gettimeofday(&now);
1220                 cell->cumulative_sleep +=
1221                         now.tv_sec + cell->el_sleep_timer.tv_sec;
1222                 do_gettimeofday(&cell->el_sleep_timer);
1223
1224                 /* Check if we need to reset Sleep */
1225                 if (abs_int(di->current_avg) >
1226                         cell->config->ocv->sleep_exit_current) {
1227
1228                         if (abs_int(di->current_avg) >
1229                                 cell->config->ocv->sleep_exit_current) {
1230
1231                                 if (cell->sleep_samples < MAX_UINT8)
1232                                         cell->sleep_samples++;
1233
1234                         } else {
1235                                 cell->sleep_samples = 0;
1236                         }
1237
1238                         /* Check if we need to reset a Sleep timer */
1239                         if (cell->sleep_samples >
1240                                 cell->config->ocv->sleep_exit_samples) {
1241                                 /* Exit sleep mode */
1242
1243                                 cell->sleep_timer.tv_sec = 0;
1244                                 cell->sleep = false;
1245                                 cell->relax = false;
1246                         }
1247                 } else {
1248                         cell->sleep_samples = 0;
1249
1250                         if (!cell->relax) {             
1251
1252                                 if (now.tv_sec-cell->sleep_timer.tv_sec >
1253                                         cell->config->ocv->relax_period) {
1254                                         cell->relax = true;
1255                                         cell->calibrate = true;
1256                                 }
1257                         }
1258                 }
1259         }
1260         
1261         return cell->relax;
1262 }
1263
1264 /* Checks for right conditions for OCV correction */
1265 static bool fg_can_ocv(struct battery_info * di)//(struct cell_state *cell)
1266 {
1267         struct cell_state *cell = &di->cell;
1268 #if  1
1269         /* Voltage should be stable */
1270         if (cell->config->ocv->voltage_diff <= diff_array(av_v, AV_SIZE))
1271                 return false;
1272
1273         /* Current should be stable */
1274         if (cell->config->ocv->current_diff <= diff_array(av_c, AV_SIZE))
1275                 return false;
1276 #endif
1277         /* SOC should be out of Flat Zone */
1278         if ((di->real_soc>= cell->config->ocv->flat_zone_low)
1279                 && (di->real_soc <= cell->config->ocv->flat_zone_high))
1280                         return false;
1281
1282         /* Current should be less then SleepEnterCurrent */
1283         if (abs_int(di->current_avg) >= cell->config->ocv->sleep_enter_current)
1284                 return false;
1285
1286         /* Don't allow OCV below EDV1, unless OCVbelowEDV1 is set */
1287         //if (cell->edv1 && !cell->config->ocv_below_edv1)
1288         //      return false;
1289
1290         return true;
1291 }
1292
1293 #endif
1294
1295 /* Sets the battery Voltage, and recalculates the average voltage */
1296 void fg_set_voltage(int16_t voltage)
1297 {
1298         int16_t i;
1299         int32_t tmp = 0;
1300
1301         /* put voltage reading int the buffer and update average */
1302         av_v_index++;
1303         av_v_index %= AV_SIZE;
1304         av_v[av_v_index] = voltage;
1305         for (i = 0; i < AV_SIZE; i++)
1306                 tmp += av_v[i];
1307 }
1308
1309
1310 /* Sets the battery Current, and recalculates the average current */
1311 void fg_set_current( int16_t cur)
1312 {
1313         int16_t i;
1314         int32_t tmp = 0;
1315
1316         /* put current reading int the buffer and update average */
1317         av_c_index++;
1318         av_c_index %= AV_SIZE;
1319         av_c[av_c_index] = cur;
1320         for (i = 0; i < AV_SIZE; i++)
1321                 tmp += av_c[i];
1322
1323 }
1324
1325 static int _copy_soc(struct  battery_info * di, u8 save_soc)
1326 {
1327         u8 soc;
1328
1329         soc = save_soc;
1330         //soc = 85;
1331         battery_write(di->rk818, SOC_REG, &soc, 1);
1332         battery_read(di->rk818, SOC_REG, &soc, 1);
1333         DBG(" the save soc-reg = %d \n", soc);
1334         
1335         return 0;
1336 }
1337 static void  _save_rsoc_nac(struct  battery_info * di)
1338 {
1339         u8 buf;
1340
1341         buf = di->real_soc;
1342         
1343         battery_write(di->rk818, SOC_REG, &buf, 1);
1344         
1345 }
1346
1347 static int _rsoc_init(struct  battery_info * di)
1348 {
1349         int vol;
1350         u8 temp;
1351         u32 remain_capacity;
1352         
1353         vol = di->voltage_ocv; //_get_OCV_voltage(di);
1354         DBG("OCV voltage = %d\n" , di->voltage_ocv);
1355         if(_is_first_poweron(di)){
1356
1357                 DBG(" %s this is first poweron\n", __FUNCTION__);
1358                  _voltage_to_capacity(di, di->voltage_ocv);
1359                 di->real_soc = di->temp_soc;
1360                 di->nac        = di->temp_nac;
1361         }else{
1362                 DBG(" %s this is  not not not first poweron\n", __FUNCTION__);
1363                 battery_read(di->rk818,SOC_REG, &temp, 1);
1364                 remain_capacity = _get_remain_capacity(di);
1365                 if(remain_capacity >= di->qmax)
1366                         remain_capacity = di->qmax;
1367                 DBG("saved SOC_REG = 0x%8x\n", temp);
1368                 DBG("saved remain_capacity = %d\n", remain_capacity);
1369                 
1370                 
1371                 di->real_soc = temp;
1372                 //di->nac = di->fcc*temp/100;
1373                 di->nac = remain_capacity;
1374         }
1375         return 0;
1376 }
1377
1378 static int _get_soc(struct   battery_info *di)
1379 {
1380
1381         return di->remain_capacity * 100 / di->fcc;
1382 }
1383
1384 static u8 get_charge_status(struct  battery_info * di)
1385 {
1386         u8 status;
1387         u8 ret =0;
1388
1389         battery_read(di->rk818, SUP_STS_REG,  &status, 1);
1390         DBG("%s ----- SUP_STS_REG(0xA0) = 0x%02x\n", __FUNCTION__, status);
1391         status &= ~(0x07<<4);
1392         switch(status){
1393                 case CHARGE_OFF:
1394                         ret =  CHARGE_OFF;
1395                         break;
1396                 case DEAD_CHARGE:
1397                         ret = DEAD_CHARGE;
1398                         break;
1399                 case  TRICKLE_CHARGE://                         (0x02<<4)
1400                         ret = DEAD_CHARGE;
1401                         break;
1402                 case  CC_OR_CV: //                                      (0x03<<4)
1403                         ret = CC_OR_CV;
1404                         break;
1405                 case  CHARGE_FINISH://                          (0x04<<4)
1406                         ret = CHARGE_FINISH;
1407                         break;
1408
1409                 case  USB_OVER_VOL://                           (0x05<<4)
1410                         ret = USB_OVER_VOL;
1411                         break;
1412
1413                 case  BAT_TMP_ERR://                                    (0x06<<4)
1414                         ret = BAT_TMP_ERR;
1415                         break;
1416
1417                 case  TIMER_ERR://                                      (0x07<<4)
1418                         ret = TIMER_ERR;
1419                         break;
1420
1421                 case  USB_EXIST://                                      (1<<1)// usb is exists
1422                         ret = USB_EXIST;
1423                         break;
1424
1425                 case  USB_EFF://                                                (1<<0)// usb is effective
1426                         ret = USB_EFF;
1427                         break;
1428                 default:
1429                         return -EINVAL;
1430         }
1431
1432         return ret;
1433
1434 }
1435
1436 static void rk818_battery_charger_init(struct  battery_info *di)
1437 {
1438         u8 chrg_ctrl_reg1,usb_ctrl_reg;// chrg_ctrl_reg2;
1439         u8 sup_sts_reg;
1440         
1441
1442         DBG("%s  start\n",__FUNCTION__);
1443
1444         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1445         battery_read(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1446 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1447         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1448
1449         DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1450         //usb_ctrl_reg &= (0x01<<7);
1451         usb_ctrl_reg |= (VLIM_4400MV | ILIM_1200MA)|(0x01<<7);
1452         
1453         chrg_ctrl_reg1 &= (0x00);
1454         chrg_ctrl_reg1 |=(0x01<<7)| (CHRG_VOL4200| CHRG_CUR1400mA);
1455         
1456         sup_sts_reg &= ~(0x01<<3);
1457         sup_sts_reg |= (0x01<<2);
1458
1459         battery_write(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1460         
1461         battery_write(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1462         //battery_write(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1463         battery_write(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1464
1465         
1466         battery_read(di->rk818, CHRG_CTRL_REG1,  &chrg_ctrl_reg1, 1);
1467 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1468         battery_read(di->rk818, SUP_STS_REG, &sup_sts_reg, 1);
1469         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1470         DBG(" new  usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x, SUP_STS_REG=0x%2x\n ",
1471                                                                                                 usb_ctrl_reg, chrg_ctrl_reg1,sup_sts_reg);
1472
1473         DBG("%s  end\n",__FUNCTION__);
1474
1475 }
1476
1477 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
1478
1479 void charge_disable_open_otg(struct  battery_info *di, int value )
1480 {
1481 //      u8 chrg_ctrl_reg1,dcdc_en_reg;
1482         if(value  == 1){
1483                 DBG("1    ---- charge disable \n");
1484                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 0<< 7); //ldo9
1485                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 1 << 7); //ldo9
1486         }
1487         if(value == 0){
1488                 DBG("1    ---- charge disable \n");
1489                 rk818_set_bits(di->rk818, 0x23, 1 << 7, 0 << 7); //ldo9
1490                 rk818_set_bits(di->rk818, CHRG_CTRL_REG1, 1 << 7, 1 << 7); //ldo9
1491         }
1492
1493 }
1494
1495 static void  fg_init(struct battery_info *di)
1496 {
1497         DBG("%s start\n",__FUNCTION__);
1498         _gauge_enable(di);
1499         _get_voltage_offset_value(di); //get the volatege offset
1500 //      _autosleep_enable(di);
1501         rk818_battery_charger_init(di);
1502 //      _set_relax_thres(di);
1503         di->current_offset = _get_ioffset(di); //get the current offset , the value write to the CAL_OFFSET
1504         _set_cal_offset(di,di->current_offset+42);
1505
1506         di->voltage  = rk818_battery_voltage(di);       
1507         di->voltage_ocv = _get_OCV_voltage(di);
1508         _rsoc_init( di);
1509         _capacity_init(di, di->nac);
1510 //      _get_realtime_capacity( di);
1511         di->remain_capacity = _get_capacity(di);
1512         // _get_realtime_capacity( di);
1513         do_gettimeofday(&di->soc_timer);
1514         di->change_timer = di->soc_timer;
1515 #if 0
1516         for (i = 0; i < AV_SIZE; i++) {
1517                 av_v[i] = di->voltage;
1518                 av_c[i] = 0;
1519         }
1520         av_v_index = 0;
1521         av_c_index = 0;
1522 #endif
1523         dump_gauge_register(di);
1524         dump_charger_register(di);
1525         DBG("nac =%d , remain_capacity = %d \n"
1526                 " OCV_voltage =%d, voltage =%d \n",
1527                 di->nac, di->remain_capacity,
1528                 di->voltage_ocv, di->voltage);
1529 }
1530
1531 #if 0
1532 static int capacity_changed(struct battery_info *di)
1533 {
1534         s32 acc_value, samples = 0;
1535         int ret;
1536         int acc_q;
1537         
1538 //      fg_set_voltage(&di->cell, di->voltage_mV);
1539         //fg_set_current(&di->cell, (int16_t)(di->current_uA/1000));
1540
1541
1542         return 0;
1543 }
1544
1545 static void rk818_battery_info(struct battery_info *di)
1546 {
1547         //di->status = rk818_battery_status(di);
1548         //di->voltage = rk818_battery_voltage(di);
1549         di->present = rk818_battery_present(di);
1550         di->bat_current = _get_average_current(di);
1551         di->temp_soc= rk818_battery_soc(di);
1552         di->tempreture =rk818_battery_temperature(di);
1553         di->health = rk818_battery_health(di);
1554 }
1555 #endif
1556
1557 static void rk818_battery_display_smooth(struct battery_info *di)
1558 {
1559         int status;
1560         u8 charge_status;
1561 //      int relaxmode_soc;
1562 //      int coulomp_soc, soc;
1563
1564         status = di->status;
1565         if(status == POWER_SUPPLY_STATUS_CHARGING){
1566                 //DBG("charging smooth ... \n");
1567                 if(1){
1568                         //DBG("   BATTERY NOT RELAX MODE \n");
1569                         DBG("di->remain_capacity =%d, di->fcc  = %d\n", di->remain_capacity,di->fcc);
1570                         di->temp_soc = _get_soc(di);
1571                         charge_status = get_charge_status( di);
1572                         if(di->temp_soc >= 100){
1573                                 di->temp_soc = 100;
1574                                 //di->status = POWER_SUPPLY_STATUS_FULL;
1575                         }
1576                         
1577                         do_gettimeofday(&di->soc_timer);
1578
1579                         if(di->temp_soc!= di->real_soc){                        
1580                                 di->change_timer = di->soc_timer;
1581                                 if(di->real_soc < di->temp_soc)
1582                                         di->real_soc++;
1583                                 else
1584                                         di->real_soc =di->temp_soc;
1585                         }
1586
1587                 //      DBG("charge_status =0x%x\n", charge_status);
1588                         if((charge_status ==CHARGE_FINISH) && (di->real_soc < 100)){            
1589                                 DBG("CHARGE_FINISH  di->real_soc < 100 \n ");
1590                                 if((di->soc_counter < 10)){
1591                                         di->soc_counter ++;
1592                                 }else{
1593                                         di->soc_counter = 0;
1594                                         if(di->real_soc < 100){
1595                                                 di->real_soc ++;
1596                                                 // _save_rsoc_nac( di);
1597                                         }
1598                                 }
1599                         }
1600
1601                 }
1602                 if(di->real_soc <= 0)
1603                         di->real_soc = 0;
1604                 if(di->real_soc >= 100){
1605                         di->real_soc = 100;
1606                         di->status = POWER_SUPPLY_STATUS_FULL;
1607                 }
1608                                 
1609         }
1610         if(status == POWER_SUPPLY_STATUS_DISCHARGING){
1611                 //DBG("discharging smooth ... \n");
1612                 di->temp_soc = _get_soc(di);
1613                 do_gettimeofday(&di->soc_timer);
1614                 if(di->temp_soc!= di->real_soc){
1615                         di->change_timer = di->soc_timer;
1616                         di->real_soc = di->temp_soc;
1617                         // _save_rsoc_nac( di);
1618                 }
1619                 if(di->real_soc <= 0)
1620                         di->real_soc = 0;
1621                 if(di->real_soc >= 100){
1622                         di->real_soc = 100;
1623                 }
1624 #if 0
1625                 if(!_is_relax_mode( di)){
1626                         DBG("   BATTERY NOT RELAX MODE \n");
1627                         di->temp_soc = _get_soc(di);
1628                         do_gettimeofday(&di->soc_timer);
1629                         if(di->temp_soc!= di->real_soc){
1630                                 di->change_timer = di->soc_timer;
1631                                 di->real_soc = di->temp_soc;
1632                                  _save_rsoc_nac( di);
1633                         }
1634
1635                 }else{
1636                         DBG("BATTERY  RELAX MODE\n ");
1637                         //relaxmode_soc = relax_soc(di);
1638                         coulomp_soc    = _get_soc(di);
1639                         soc =coulomp_soc;// (coulomp_soc*20 + relaxmode_soc*80)/100;
1640
1641                         if((soc > di->real_soc)&&(di->soc_counter < 10)){
1642                                 di->soc_counter ++;
1643
1644                         }else{
1645                                 di->soc_counter = 0;
1646                                 if(di->real_soc < 100){
1647                                         di->real_soc --;
1648                                          _save_rsoc_nac( di);
1649                                 }
1650                         }
1651                         DBG(" remaxmode_soc = %d , coulomp-soc =%d  soc = %d\n",relaxmode_soc, coulomp_soc, soc);
1652                 }
1653 #endif
1654
1655         }
1656         //DBG("%s   exit \n", __FUNCTION__);
1657 }
1658
1659 static void rk818_battery_update_status(struct battery_info *di)
1660 {
1661
1662         di->voltage              = rk818_battery_voltage( di);
1663         di->current_avg       = _get_average_current(di);
1664         di->remain_capacity = _get_realtime_capacity( di);
1665          _get_capacity(di);
1666
1667         rk818_battery_display_smooth(di);
1668         
1669         DBG("%s\n"
1670                 "voltage = %d, current-avg = %d\n"      
1671                 "fcc = %d ,remain_capacity =%d\n"
1672                 "real_soc = %d\n",
1673                         __FUNCTION__,
1674                         di->voltage, di->current_avg,
1675                         di->fcc, di->remain_capacity,
1676                         di->real_soc
1677                 );
1678 }
1679 extern int dwc_vbus_status(void);
1680 extern int get_gadget_connect_flag(void);
1681
1682  //state of charge ----running
1683 static int  get_charging_status(struct battery_info *di)
1684 {
1685
1686 ////////////////////////////////////////////
1687 #if 0
1688         u8 usb_ctrl_reg;// chrg_ctrl_reg2;
1689         
1690
1691
1692         battery_read(di->rk818, USB_CTRL_REG,  &usb_ctrl_reg, 1);
1693 //      battery_read(di->rk818, CHRG_CTRL_REG2, &chrg_ctrl_reg2, 1);
1694
1695         DBG("old usb_ctrl_reg =0x%2x,CHRG_CTRL_REG1=0x%2x\n ",usb_ctrl_reg, chrg_ctrl_reg1);
1696         usb_ctrl_reg &= (0x01<<7);
1697         usb_ctrl_reg |= (  ILIM_300MA);
1698 #endif
1699 /////////////////////////////////////////
1700
1701 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1702         int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
1703         int vbus_status =  dwc_vbus_status();
1704         if (1 == vbus_status) {
1705                 if (0 == get_gadget_connect_flag()){ 
1706                         if (++di->check_count >= 5){
1707
1708                                 di->ac_online = 1;
1709                                 di->usb_online = 0;
1710                         }else{
1711                                 di->ac_online =0;
1712                                 di->usb_online = 1;
1713
1714                         }
1715                 }else{
1716
1717                                 di->ac_online =0;
1718                                 di->usb_online = 1;
1719                 }
1720                 
1721         }else{
1722                 if (2 == vbus_status) {
1723
1724                                 di->ac_online = 1;
1725                                 di->usb_online = 0;
1726                 }else{
1727
1728                                 di->ac_online = 0;
1729                                 di->usb_online = 0;
1730                 }
1731                 di->check_count=0;
1732
1733         }
1734         return usb_status;
1735
1736 }
1737
1738 static void get_battery_status(struct battery_info *di)
1739 {
1740
1741         u8 buf;
1742         int ret;
1743         ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1744         //int vbus_status =  dwc_vbus_status();
1745
1746         if(buf&PLUG_IN_STS){
1747         //if(vbus_status != 0){
1748                 get_charging_status(di);
1749                 di->status = POWER_SUPPLY_STATUS_CHARGING;
1750         //      di->ac_online = 1;
1751                 if(di->real_soc == 100)
1752                         di->status = POWER_SUPPLY_STATUS_FULL;
1753         }
1754         else{
1755                 di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1756                 di->ac_online =0;
1757                 di->usb_online =0;
1758
1759         }
1760         //DBG("%s ,di->status = %d\n",__FUNCTION__, di->status);
1761 }
1762
1763 static void rk818_battery_work(struct work_struct *work)
1764 {
1765         u8 buf;
1766         struct battery_info *di = container_of(work,
1767         struct battery_info, battery_monitor_work.work);
1768         int vbus_status ;
1769         get_battery_status(di);
1770         battery_read(di->rk818,0x00, &buf, 1);
1771         DBG("RTC  =0x%2x\n ", buf);
1772         battery_read(di->rk818,VB_MOD_REG, &buf, 1);
1773         //DBG("VB_MOD_REG  =%2x, the value is %2x\n ", VB_MOD_REG,buf);
1774         battery_read(di->rk818,SUP_STS_REG, &buf, 1);
1775 //      DBG("SUP_STS_REG  =%2x, the value is %2x\n ", SUP_STS_REG,buf);
1776         vbus_status =  dwc_vbus_status();
1777 //      DBG("vbus_status  =%2x\n ", vbus_status);
1778
1779         rk818_battery_update_status(di);
1780
1781         if(di ->resume){
1782                 di ->resume = false;
1783                 di->real_soc = _get_soc(di); 
1784                 if(di->real_soc <= 0)
1785                         di->real_soc = 0;
1786                 if(di->real_soc >= 100)
1787                         di->real_soc = 100;
1788         }
1789         if ((di->ac_online == 0 )&&( di->usb_online ==0)&&(di->remain_capacity > di->qmax +10)){
1790                 _capacity_init(di, di->qmax);
1791                 di->remain_capacity  = _get_realtime_capacity( di);
1792         }
1793
1794         //DBG("soc  =  %d", di->real_soc);
1795          _copy_soc(di, di->real_soc);
1796         _save_remain_capacity(di, di->remain_capacity);
1797         power_supply_changed(&di->bat);
1798 //      power_supply_changed(&di->usb);
1799         power_supply_changed(&di->ac);
1800         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
1801
1802 }
1803
1804 static void rk818_battery_charge_check_work(struct work_struct *work)
1805 {
1806         struct battery_info *di = container_of(work,
1807                                                 struct battery_info, charge_check_work.work);
1808         charge_disable_open_otg(di,di->charge_otg);
1809 }
1810
1811 static BLOCKING_NOTIFIER_HEAD(battery_chain_head);
1812
1813 int register_battery_notifier(struct notifier_block *nb)
1814 {
1815         return blocking_notifier_chain_register(&battery_chain_head, nb);
1816 }
1817 EXPORT_SYMBOL_GPL(register_battery_notifier);
1818
1819 int unregister_battery_notifier(struct notifier_block *nb)
1820 {
1821         return blocking_notifier_chain_unregister(&battery_chain_head, nb);
1822 }
1823 EXPORT_SYMBOL_GPL(unregister_battery_notifier);
1824
1825 int battery_notifier_call_chain(unsigned long val)
1826 {
1827         return (blocking_notifier_call_chain(&battery_chain_head, val, NULL)
1828                         == NOTIFY_BAD) ? -EINVAL : 0;
1829 }
1830 EXPORT_SYMBOL_GPL(battery_notifier_call_chain);
1831 static int battery_notifier_call(struct notifier_block *nb,
1832                 unsigned long event, void *data)
1833 {
1834         struct battery_info *di=
1835                 container_of(nb, struct battery_info, battery_nb);
1836
1837         switch (event) {
1838                 case 0:
1839                         DBG(" CHARGE enable \n");
1840                         di ->charge_otg = 0;
1841                         queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1842                         break;
1843
1844                 case 1:
1845                         di ->charge_otg  = 1;
1846                         queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(50));
1847
1848                         DBG("charge disable OTG enable \n");
1849                         break;
1850                 default:
1851                         return NOTIFY_OK;
1852                 }
1853         return NOTIFY_OK;
1854 }
1855 #ifdef CONFIG_OF
1856 static int rk818_battery_parse_dt(struct rk818 *rk818)
1857 {
1858         struct device_node *regs,*rk818_pmic_np;
1859         struct battery_platform_data *data;
1860         struct cell_config *cell_cfg;
1861         struct property *prop;
1862         u32 out_value;
1863         int i, length, ret;
1864
1865         rk818_pmic_np = of_node_get(rk818->dev->of_node);
1866         if (!rk818_pmic_np) {
1867                 printk("could not find pmic sub-node\n");
1868                 return -EINVAL;
1869         }
1870
1871         regs = of_find_node_by_name(rk818_pmic_np, "battery");
1872         if (!regs){
1873                 printk("could not find battery sub-node\n");
1874                 return -EINVAL;
1875         }
1876
1877         data = devm_kzalloc(rk818->dev, sizeof(*data), GFP_KERNEL);
1878         memset(data, 0, sizeof(*data));
1879
1880         cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
1881         /* determine the number of brightness levels */
1882         prop = of_find_property(regs, "ocv_table", &length);
1883         if (!prop)
1884                 return -EINVAL;
1885         data->ocv_size= length / sizeof(u32);
1886         /* read brightness levels from DT property */
1887         if (data->ocv_size > 0) {
1888                 size_t size = sizeof(*data->battery_ocv) * data->ocv_size;
1889                 data->battery_ocv= devm_kzalloc(rk818->dev, size, GFP_KERNEL);
1890                 if (!data->battery_ocv)
1891                         return -ENOMEM;
1892                 ret = of_property_read_u32_array(regs, "ocv_table", data->battery_ocv, data->ocv_size);
1893                 DBG("the battery OCV TABLE : ");
1894                 for(i =0; i< data->ocv_size; i++ )
1895                         DBG("%d ", data->battery_ocv[i]);
1896                 DBG("\n");
1897                 if (ret < 0)
1898                         return ret;
1899         }
1900         ret = of_property_read_u32(regs, "max_charge_currentmA", &out_value);
1901         if (ret < 0)
1902                 return ret;
1903         data->max_charger_currentmA= out_value;
1904         ret = of_property_read_u32(regs, "max_charge_voltagemV", &out_value);
1905         if (ret < 0)
1906                 return ret;
1907         data->max_charger_voltagemV= out_value;
1908         ret = of_property_read_u32(regs, "design_capacity", &out_value);
1909         if (ret < 0)
1910                 return ret;
1911         cell_cfg->design_capacity  = out_value;
1912         ret = of_property_read_u32(regs, "design_qmax", &out_value);
1913         if (ret < 0)
1914                 return ret;
1915         cell_cfg->design_qmax =out_value;
1916         data->cell_cfg =cell_cfg;
1917         rk818->battery_data = data;
1918         DBG("max_charge_currentmA :%d\n", data->max_charger_currentmA);
1919         DBG("max_charge_voltagemV :%d\n", data->max_charger_voltagemV);
1920         DBG("design_capacity :%d\n", cell_cfg->design_capacity);
1921         DBG("design_qmax :%d\n", cell_cfg->design_qmax);
1922
1923         return 0;
1924 }
1925
1926 static struct of_device_id rk818_battery_of_match[] = {
1927         { .compatible = "rk818_battery" },
1928         { }
1929 };
1930
1931 MODULE_DEVICE_TABLE(of, rk818_battery_of_match);
1932 #else
1933 static int rk818_battery_parse_dt(struct device *dev)
1934 {
1935         return -ENODEV;
1936 }
1937 #endif
1938
1939 static int  battery_probe(struct platform_device *pdev)
1940 {
1941         struct rk818 *chip = dev_get_drvdata(pdev->dev.parent);
1942         struct rk818_platform_data *rk818_platform_data = chip->dev->platform_data;
1943 //      struct battery_platform_data *pdata ;//= rk818_platform_data->battery_data;
1944 //      struct battery_platform_data defdata ;//= rk818_platform_data->battery_data;
1945         struct battery_info *di;
1946         struct ocv_config *ocv;
1947         struct edv_config *edv;
1948         int ret;
1949         
1950         DBG("%s is  the  battery driver version %s\n",__FUNCTION__,DRIVER_VERSION);
1951          rk818_battery_parse_dt(chip);
1952
1953         di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1954         if (!di) {
1955                 dev_err(&pdev->dev, "no memory for state\n");
1956                 ret = -ENOMEM;
1957                 return ret;
1958         }
1959         ocv = devm_kzalloc(&pdev->dev, sizeof(*ocv), GFP_KERNEL);
1960         if (!ocv) {
1961                 dev_err(&pdev->dev, "ocv  no memory for state\n");
1962                 ret = -ENOMEM;
1963                 return ret;
1964         }
1965         edv = devm_kzalloc(&pdev->dev, sizeof(*edv), GFP_KERNEL);
1966         if (!edv) {
1967                 dev_err(&pdev->dev, "edv  no memory for state\n");
1968                 ret = -ENOMEM;
1969                 return ret;
1970         }
1971
1972         di->rk818 = chip;
1973 #if 0
1974         di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
1975         if (!di->platform_data) {
1976                 kfree(di);
1977                 return -ENOMEM;
1978         }
1979 #endif
1980 //      data = di;
1981         platform_set_drvdata(pdev, di);
1982         /*apply battery cell configuration*/
1983         //di->cell.config = di->platform_data->cell_cfg;
1984         di->platform_data = chip->battery_data;
1985         di->platform_data->cell_cfg = chip->battery_data->cell_cfg;
1986         di->platform_data->cell_cfg->ocv = ocv;
1987         di->platform_data->cell_cfg->edv = edv;
1988         di->design_capacity = chip->battery_data->cell_cfg->design_capacity;
1989         di->qmax = chip->battery_data->cell_cfg->design_qmax;
1990         di->fcc = di->design_capacity;
1991         di->status = POWER_SUPPLY_STATUS_DISCHARGING;
1992
1993         battery_powersupply_init(di);
1994         fg_init(di);
1995         ret = power_supply_register(&pdev->dev, &di->bat);
1996         if (ret) {
1997                 dev_dbg(&pdev->dev, "failed to register main battery\n");
1998                 goto batt_failed;
1999         }
2000         ret = power_supply_register(&pdev->dev, &di->usb);
2001         if (ret) {
2002                 dev_dbg(&pdev->dev, "failed to register usb power supply\n");
2003                 goto usb_failed;
2004         }
2005         ret = power_supply_register(&pdev->dev, &di->ac);
2006         if (ret) {
2007                 dev_dbg(&pdev->dev, "failed to register ac power supply\n");
2008                 goto ac_failed;
2009         }
2010
2011         di->wq = create_singlethread_workqueue("battery-work");
2012         INIT_DELAYED_WORK(&di->battery_monitor_work,rk818_battery_work);
2013         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2014         //queue_delayed_work(di->wq, &di->charge_check_work, msecs_to_jiffies(TIMER_MS_COUNTS*5));
2015         INIT_DELAYED_WORK(&di->charge_check_work,rk818_battery_charge_check_work);
2016         
2017         di->battery_nb.notifier_call = battery_notifier_call;
2018         register_battery_notifier(&di->battery_nb);
2019         printk("battery probe ok... \n");
2020         return ret;
2021         
2022 ac_failed:
2023         power_supply_unregister(&di->ac);
2024 usb_failed:
2025         power_supply_unregister(&di->usb);
2026 batt_failed:
2027         power_supply_unregister(&di->bat);
2028         return ret;
2029 }
2030
2031 static int  battery_remove(struct platform_device *dev)
2032 {
2033         return 0;
2034 }
2035 #if 1
2036 static int battery_suspend(struct platform_device *dev,pm_message_t state)
2037 {
2038         int irq;
2039         struct battery_info *di = platform_get_drvdata(dev);
2040         DBG("%s--------------------\n",__FUNCTION__);
2041         if(di == NULL)
2042                 printk("battery NULL di\n");
2043         cancel_delayed_work(&di ->battery_monitor_work);
2044         DBG("%s---------end--------\n",__FUNCTION__);
2045
2046         return 0;
2047 }
2048
2049 static int battery_resume(struct platform_device *dev)
2050 {
2051         int irq;
2052         
2053         u8 buf;
2054         int ret;
2055         struct battery_info *di = platform_get_drvdata(dev);
2056
2057         ret = battery_read(di->rk818,VB_MOD_REG, &buf, 1);
2058
2059 //      struct battery_info *di  = platform_get_drvdata(dev);
2060         DBG("%s--------------------\n",__FUNCTION__);
2061         queue_delayed_work(di->wq, &di->battery_monitor_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2062         di ->resume = true;
2063         DBG("charge--status       0x%02x--------------------buf = 0x%02x\n", get_charge_status( di),buf);
2064
2065         return 0;
2066 }
2067 #endif
2068 static struct platform_driver battery_driver = {
2069         .probe          = battery_probe,
2070         .remove         = battery_remove,
2071         .suspend                = battery_suspend,
2072         .resume         = battery_resume,
2073
2074         .driver         = {
2075                 .name   = "rk818-battery",
2076                 //.pm   = &pm_ops,
2077         //      .of_match_table = of_match_ptr(rk818_battery_parse_dt),
2078         },
2079 };
2080
2081 static int __init battery_init(void)
2082 {
2083         return platform_driver_register(&battery_driver);
2084 }
2085 fs_initcall_sync(battery_init);
2086 static void __exit battery_exit(void)
2087 {
2088         platform_driver_unregister(&battery_driver);
2089 }
2090 module_exit(battery_exit);
2091
2092 MODULE_LICENSE("GPL");
2093 MODULE_ALIAS("platform:rk818-battery");
2094 MODULE_AUTHOR("ROCKCHIP");
2095
2096
2097
2098
2099
2100
2101  
2102