Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / power / rk30_factory_adc_battery.c
1 /* drivers/power/rk30_adc_battery.c
2  *
3  * battery detect driver for the rk30 
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <asm/io.h>
25 #include <asm/mach-types.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach/map.h>
28 #include <mach/gpio.h>
29 #include <linux/adc.h>
30 #include <mach/iomux.h>
31 #include <mach/board.h>
32 #include <linux/delay.h>
33 #include <linux/ktime.h>
34 #include <linux/slab.h>
35 #include <linux/syscalls.h>
36 #include <linux/fs.h>
37 #include <linux/wakelock.h>
38 #include <linux/fs.h>
39 #include <linux/string.h>
40 #include <linux/init.h>
41 #include <linux/earlysuspend.h>
42 #include <linux/suspend.h>
43 #ifdef CONFIG_EARLYSUSPEND
44 /* kernel/power/earlysuspend.c */
45 extern suspend_state_t get_suspend_state(void);
46 #endif
47
48 static int rk30_battery_dbg_level = 0;
49 module_param_named(dbg_level, rk30_battery_dbg_level, int, 0644);
50 #define DBG( args...) \
51         do { \
52                 if (rk30_battery_dbg_level) { \
53                         pr_info(args); \
54                 } \
55         } while (0)
56
57 #define TIMER_MS_COUNTS          1000   
58 #define SLOPE_SECOND_COUNTS                    15       
59 #define DISCHARGE_MIN_SECOND                   45       
60 #define CHARGE_MIN_SECOND                      45       
61 #define CHARGE_MID_SECOND                      90       
62 #define CHARGE_MAX_SECOND                      250
63 #define   CHARGE_FULL_DELAY_TIMES          10     
64 #define   USBCHARGE_IDENTIFY_TIMES        5         
65
66 #define NUM_VOLTAGE_SAMPLE                             ((SLOPE_SECOND_COUNTS * 1000) / TIMER_MS_COUNTS)  
67 #define NUM_DISCHARGE_MIN_SAMPLE                 ((DISCHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)       
68 #define NUM_CHARGE_MIN_SAMPLE            ((CHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)     
69 #define NUM_CHARGE_MID_SAMPLE            ((CHARGE_MID_SECOND * 1000) / TIMER_MS_COUNTS)      
70 #define NUM_CHARGE_MAX_SAMPLE            ((CHARGE_MAX_SECOND * 1000) / TIMER_MS_COUNTS)   
71 #define   NUM_CHARGE_FULL_DELAY_TIMES         ((CHARGE_FULL_DELAY_TIMES * 1000) / TIMER_MS_COUNTS)      
72 #define   NUM_USBCHARGE_IDENTIFY_TIMES      ((USBCHARGE_IDENTIFY_TIMES * 1000) / TIMER_MS_COUNTS)       
73
74 #define   CHARGE_IS_OK                                 1
75 #define   INVALID_CHARGE_CHECK               -1
76
77 #if defined(CONFIG_ARCH_RK3066B)
78 #define  BAT_DEFINE_VALUE                                            1800
79 #elif defined(CONFIG_ARCH_RK2928)
80 #define  BAT_DEFINE_VALUE                                         3300
81 #else
82 #define  BAT_DEFINE_VALUE                                            2500
83 #endif
84
85
86 #define BATT_FILENAME "/data/bat_last_capacity.dat"
87
88
89 #define BATTERY_APK 
90 #ifdef  BATTERY_APK
91 #define BATT_NUM  11
92 int    battery_dbg_level = 0;
93 int    battery_test_flag = 0;
94 int    gVoltageCnt = 3400;
95 int    gDoubleVoltageCnt = 6800;
96 unsigned long gSecondsCnt = 0;
97 char gDischargeFlag[3] = {"on "};
98
99
100 #ifdef CONFIG_BATTERY_RK30_VOL3V8
101 #define BATT_MAX_VOL_VALUE                             4120                     //Full  charge volate    
102 #define BATT_ZERO_VOL_VALUE                            3500                     //power down voltage
103 #define BATT_NOMAL_VOL_VALUE                         3800            
104
105 static int batt_table[2*BATT_NUM+6] =
106 {
107         0x4B434F52,0x7461625F,0x79726574,0,100,100,
108         3496, 3548, 3599, 3626, 3655, 3697, 3751, 3812, 3877, 3949, 4030,  //discharge
109         3540, 3785, 3842, 3861, 3915, 3980, 4041, 4135, 4169, 4175, 4185          //charge
110 };
111 #define adc_to_voltage(adc_val) ((adc_val * BAT_DEFINE_VALUE * (batt_table[4] +batt_table[5])) / (1024 *batt_table[5]))
112 #else
113 #define BATT_MAX_VOL_VALUE                              8284                    //Full charge voltage
114 #define BATT_ZERO_VOL_VALUE                             6800                    // power down voltage 
115 #define BATT_NOMAL_VOL_VALUE                          7600                
116
117
118 static int batt_table[2*BATT_NUM+6] =
119 {
120         0x4B434F52,0x7461625F,0x79726574,1,300,100,
121         6800, 7242, 7332, 7404, 7470, 7520, 7610, 7744, 7848, 8016, 8284,//discharge
122         7630, 7754, 7852, 7908, 7956, 8024, 8112, 8220, 8306, 8318, 8328//charge
123 };
124 #define adc_to_voltage(adc_val) ((adc_val * BAT_DEFINE_VALUE * (batt_table[4] +batt_table[5])) / (1024 *batt_table[5]))
125 #endif
126
127 #endif
128
129
130
131 /********************************************************************************/
132
133
134 enum {
135         BATTERY_STATUS                  = 0,
136         BATTERY_HEALTH                  = 1,
137         BATTERY_PRESENT                 = 2,
138         BATTERY_CAPACITY                        = 3,
139         BATTERY_AC_ONLINE                       = 4,
140         BATTERY_STATUS_CHANGED  = 5,
141         AC_STATUS_CHANGED               = 6,
142         BATTERY_INT_STATUS              = 7,
143         BATTERY_INT_ENABLE              = 8,
144 };
145
146 typedef enum {
147         CHARGER_BATTERY = 0,
148         CHARGER_USB,
149         CHARGER_AC
150 } charger_type_t;
151
152
153
154 struct rk30_adc_battery_data {
155         int irq;
156         
157         //struct timer_list       timer;
158         struct workqueue_struct *wq;
159         struct delayed_work         delay_work;
160         struct delayed_work        check_work;
161         struct work_struct          dcwakeup_work;
162         struct work_struct                   lowerpower_work;
163         bool                    resume;
164         
165         struct rk30_adc_battery_platform_data *pdata;
166         
167         struct adc_client       *client; 
168         int                     adc_val;
169         int                     adc_samples[NUM_VOLTAGE_SAMPLE+2];
170         
171         int                     bat_status;
172         int                     bat_status_cnt;
173         int                     bat_health;
174         int                     bat_present;
175         int                     bat_voltage;
176         int                     bat_capacity;
177         int                     bat_change;
178         
179         int                     old_charge_level;
180         int                    *pSamples;
181         int                     gBatCapacityDisChargeCnt;
182         int                     gBatCapacityChargeCnt;
183         int                     gBatCapacityacChargeCnt;
184         int                     gBatCapacityusbChargeCnt ;
185         int                       gBatCapacityusbdisChargeCnt;
186         int                       capacitytmp;
187         int                     suspend_capacity;
188         int                     gBatUsbChargeCnt;
189         int                     status_lock;
190
191         struct power_supply     bat;
192         struct power_supply     usb;
193         struct power_supply     ac;
194         struct power_supply     bk_bat;
195         
196         int                     poweron_check;
197         int                     usb_charging;
198         int                     ac_charging;
199         int                      charge_check;
200         int                        charge_level;
201         
202         int                     charge_source_now;
203         int                     charge_soure_old;
204         int                     charge_start_capacity;
205         int                     charge_start_voltage;
206         int                     start_voltage_status;
207         int                     charge_up_proprotion;
208         int                     charge_down_proportion;
209         unsigned long          suspend_time;
210         unsigned long           resume_time;
211         
212         int                     full_times;
213         int                         charge_full_flag;
214         int                     stop_check;
215
216
217 };
218 static struct rk30_adc_battery_data *gBatteryData;
219 static struct wake_lock batt_wake_lock;
220 static struct wake_lock charge_display_lock;
221 int system_lowerpower = 0;
222
223 extern int dwc_vbus_status(void);
224 extern int get_gadget_connect_flag(void);
225 extern int dwc_otg_check_dpdm(void);
226 static int  is_charge_ok(struct rk30_adc_battery_data *bat);
227 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat);
228
229
230 #ifdef  BATTERY_APK
231 //#define BAT_ADC_TABLE_LEN               11
232 static ssize_t bat_param_read(struct device *dev,struct device_attribute *attr, char *buf)
233 {
234         int i;
235         for(i=0;i<BATT_NUM;i++)
236                 printk("i=%d batt_table=%d\n",i+6,batt_table[i+6]);
237
238         for(i=0;i<BATT_NUM;i++)
239                 printk("i=%d batt_table=%d\n",i+17,batt_table[i+17]);
240         return 0;
241 }
242 DEVICE_ATTR(batparam, 0664, bat_param_read,NULL);
243
244
245 static ssize_t rkbatt_show_debug_attrs(struct device *dev,
246                                                                               struct device_attribute *attr, char *buf) 
247 {                                
248         return sprintf(buf, "%d\n", battery_dbg_level);
249 }
250
251 static ssize_t rkbatt_restore_debug_attrs(struct device *dev, 
252                                                                                   struct device_attribute *attr, const char *buf, size_t size)
253 {
254         int liTmp;
255         
256         sscanf(buf, "%d", &liTmp);
257         
258         if(liTmp != 0 && liTmp != 1)
259         {
260                 dev_err(dev, "rk29adc_restore_debug_attrs err\n");
261         }
262         else
263         {
264                 battery_dbg_level = liTmp;
265         }
266         return size;
267 }
268
269 static ssize_t rkbatt_show_state_attrs(struct device *dev,
270                                                                                 struct device_attribute *attr, char *buf) 
271 {
272 //      struct rk30_adc_battery_platform_data *pdata = gBatteryData->pdata;
273         int charge_ok_value =0 ;
274         charge_ok_value = is_charge_ok(gBatteryData) ;
275
276         return  sprintf(buf,
277                 "gBatVol=%d,gBatCap=%d,charge_ok=%d,%s\n",
278                 gBatteryData->bat_voltage,gBatteryData->bat_capacity,
279                 charge_ok_value,gDischargeFlag);
280 }
281
282 static ssize_t rkbatt_restore_state_attrs(struct device *dev, 
283                                                                                 struct device_attribute *attr, const char *buf, size_t size)
284 {
285         return size;
286 }
287
288 static ssize_t rkbatt_show_value_attrs(struct device *dev,
289                                                                             struct device_attribute *attr, char *buf) 
290 {                                
291         return sprintf(buf, "pull_up_res =%d,\npull_down_res=%d\n", batt_table[4],batt_table[5]);
292 }
293
294 static ssize_t rkbatt_restore_value_attrs(struct device *dev, 
295                                                                                 struct device_attribute *attr, const char *buf, size_t size)
296 {
297         int liUp        = 0;
298         int liDown      = 0;
299         
300         sscanf(buf, "%d,%d", &liUp,&liDown);
301         
302         if(liUp != 0 && liDown != 0)
303         {
304                 batt_table[4] = liUp;
305                 batt_table[5] = liDown;
306         }
307         return size;
308 }
309
310 static ssize_t rkbatt_show_flag_attrs(struct device *dev,
311                                                                          struct device_attribute *attr, char *buf) 
312 {                                
313         return sprintf(buf, "rk29_battery_test_flag=%d\n", battery_test_flag);
314 }
315 static ssize_t rkbatt_restore_flag_attrs(struct device *dev, 
316                                                                             struct device_attribute *attr, const char *buf, size_t size)
317 {
318         int liFlag;
319         
320         sscanf(buf, "%d", &liFlag);
321         
322         if(liFlag != 0)
323         {
324                 battery_test_flag = liFlag;
325         }
326         return size;
327 }
328 static struct device_attribute rkbatt_attrs[] = {
329         __ATTR(state, 0664, rkbatt_show_state_attrs, rkbatt_restore_state_attrs),
330         __ATTR(debug, 0664, rkbatt_show_debug_attrs, rkbatt_restore_debug_attrs),
331         __ATTR(value, 0666, rkbatt_show_value_attrs, rkbatt_restore_value_attrs),
332         __ATTR(flag,  0666, rkbatt_show_flag_attrs,  rkbatt_restore_flag_attrs),
333 };
334
335 static int create_sysfs_interfaces(struct device *dev)
336 {
337         int liTmep;
338         for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++)   {
339                 
340                 if (device_create_file(dev, rkbatt_attrs + liTmep)){
341                         goto error;
342                 }
343         }
344
345         return 0;
346
347 error:
348         for ( ; liTmep >= 0; liTmep--){
349                 device_remove_file(dev, rkbatt_attrs + liTmep);
350         }
351         
352         dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
353         return -1;
354 }
355
356 #endif
357
358
359 static int rk30_adc_battery_load_capacity(void)
360 {
361         char value[4];
362         int* p = (int *)value;
363         long fd = sys_open(BATT_FILENAME,O_RDONLY,0);
364
365         if(fd < 0){
366                 DBG("rk30_adc_battery_load_capacity: open file /data/bat_last_capacity.dat failed\n");
367                 return -1;
368         }
369
370         sys_read(fd,(char __user *)value,4);
371         sys_close(fd);
372
373         return (*p);
374 }
375
376 static void rk30_adc_battery_put_capacity(int loadcapacity)
377 {
378         char value[4];
379         int* p = (int *)value;
380         long fd = sys_open(BATT_FILENAME,O_CREAT | O_RDWR,0);
381
382         if(fd < 0){
383                 DBG("rk30_adc_battery_put_capacity: open file /data/bat_last_capacity.dat failed\n");
384                 return;
385         }
386         
387         *p = loadcapacity;
388         sys_write(fd, (const char __user *)value, 4);
389         sys_close(fd);
390 }
391
392 static void rk_start_charge(struct rk30_adc_battery_data *bat)
393 {
394         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
395
396         if (pdata->charge_set_pin != INVALID_GPIO){
397                 gpio_direction_output(pdata->charge_set_pin, pdata->charge_set_level);
398         }
399 }
400
401 static void rk_stop_charge(struct rk30_adc_battery_data *bat)
402 {
403         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
404
405         if (pdata->charge_set_pin != INVALID_GPIO){
406                 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
407         }
408 }
409
410 static int  get_ac_status(struct rk30_adc_battery_data *bat){
411         
412         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
413         int status = 0;
414         if (pdata->dc_det_pin != INVALID_GPIO){
415                 if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level){
416                                 status = 1;
417                         }else{
418                                 status = 0;
419                         }
420         }else{
421                 if(pdata->is_dc_charging){
422                         status = pdata->is_dc_charging();
423                 }
424         }
425
426         return status;
427 }
428 // state of charge  --- charge-display
429 static int  get_usb_status1(struct rk30_adc_battery_data *bat){
430
431 }
432 //state of charge ----running
433 static int  get_usb_status2(struct rk30_adc_battery_data *bat){
434
435 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
436         int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
437         
438         if (1 == dwc_vbus_status()) {
439                 if (0 == get_gadget_connect_flag()){ 
440                         if (++bat->gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES){
441                                 bat->gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
442                                 usb_status = 2; // non-standard AC charger
443                                 if(bat ->pdata ->control_usb_charging)
444                                         bat ->pdata ->control_usb_charging(1);
445                         }
446                 }else{
447                                 
448                                 usb_status = 1; // connect to pc        
449                                 if(bat ->pdata ->control_usb_charging)
450                                         bat ->pdata ->control_usb_charging(0);
451
452                 }
453                 
454         }else{
455                 bat->gBatUsbChargeCnt = 0;
456                 if (2 == dwc_vbus_status()) {
457                         usb_status = 2; //standard AC charger
458                         
459                         if(bat ->pdata ->control_usb_charging)
460                                         bat ->pdata ->control_usb_charging(1);
461                 }else{
462                         usb_status = 0; 
463                 }
464
465
466         }
467         return usb_status;
468
469 }
470
471 static int rk_battery_get_status(struct rk30_adc_battery_data *bat)
472 {
473         int charge_on = 0;
474         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
475         int ac_ac_charging = 0, usb_ac_charging = 0;
476         int i=0;
477
478 #if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
479         ac_ac_charging = get_ac_status(bat);
480         if(1 == ac_ac_charging)
481                 charge_on = 1;
482 #endif
483
484 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)   
485                 if (strstr(saved_command_line,"charger")){
486                         wake_lock(&charge_display_lock);  //lock
487                         if( bat->pdata->usb_det_pin  != INVALID_GPIO ){
488                                 if( gpio_get_value(bat->pdata->usb_det_pin)== bat->pdata->usb_det_level){
489                                         if(( 1 == usb_ac_charging )||( 1 == ac_ac_charging ))
490                                                 bat -> ac_charging = 1;
491                                         if(( 1 == bat->usb_charging)||(1 == bat ->ac_charging))
492                                                 charge_on =1;
493                                         return charge_on;
494                                 }else{
495                                         if(( 0 == usb_ac_charging )&&( 0 == ac_ac_charging ))
496                                                 bat -> ac_charging = 0;
497                                         else
498                                                 bat->ac_charging = 1;
499                                         
500                                         bat->usb_charging = 0;
501                                         if(1 == bat->ac_charging)
502                                                 charge_on=1;
503                                         else
504                                                 charge_on = 0;
505                                         return charge_on;
506                                 }
507                         }else{
508
509                                 if(dwc_otg_check_dpdm() == 0){
510                                         bat->usb_charging = 0;
511                                         usb_ac_charging = 0;
512                                 }else if(dwc_otg_check_dpdm() == 1){
513                                         bat->usb_charging = 1;
514                                         if(bat -> pdata ->control_usb_charging)
515                                                 bat -> pdata ->control_usb_charging(0);
516                                 }else if(dwc_otg_check_dpdm() == 2){
517                                         bat->usb_charging = 0;
518                                         usb_ac_charging = 1;
519                                         if(bat -> pdata ->control_usb_charging)
520                                                 bat -> pdata ->control_usb_charging(1); 
521                                 }
522                                 if((1 == usb_ac_charging)||(1 == ac_ac_charging))
523                                         bat ->ac_charging = 1;
524                                 else
525                                         bat ->ac_charging = 0;
526
527                                 if(( 0 == bat ->ac_charging )&&(0 == bat->usb_charging  )){
528                                         charge_on = 0;
529                                         bat->bat_change = 1;
530                                 }
531
532                                 return charge_on;
533
534                         }
535                 }
536
537                 if (charge_on == 0){
538                         usb_ac_charging = get_usb_status2(bat); //0 --discharge, 1---usb charging,2----AC charging;
539                         if(1 == usb_ac_charging)
540                                 bat->usb_charging = 1;
541                         else
542                                 bat->usb_charging = 0;                                          
543                 }
544 #endif
545         if((usb_ac_charging == 2)||(ac_ac_charging == 1))
546                 bat -> ac_charging = 1;
547         else
548                 bat -> ac_charging = 0;
549
550         if((bat->usb_charging == 1)||(bat ->ac_charging ==1))
551                 charge_on =1;
552         
553         if(1 == bat->ac_charging )
554                         bat->charge_source_now = 1; //ac charge
555         else if( 1 == bat->usb_charging){
556                         bat->charge_source_now = 2; //ac charge
557         }else{
558                 bat->charge_soure_old =0;
559                 bat->charge_source_now=0;
560         }
561         if(bat->charge_source_now != bat->charge_soure_old){
562
563                 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){                //0.3 s
564                         msleep(1); //mdelay --- > msleep
565                         rk30_adc_battery_voltage_samples(bat);              //get new voltage
566                 }
567
568                         bat->charge_soure_old = bat->charge_source_now;
569                         bat->bat_change = 1;
570         }
571         DBG("ac_status=%d,usb_status=%d bat->bat_change = %d\n",bat -> ac_charging, bat->usb_charging ,bat->bat_change );
572         
573         return charge_on;
574 }
575
576 static int  is_charge_ok(struct rk30_adc_battery_data *bat)
577 {
578         int charge_is_ok = 0;
579         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
580
581         if( 1 != bat->charge_level)
582                 return -1;
583         
584         if((pdata->charge_ok_pin == INVALID_GPIO)&& ( pdata->charging_ok == NULL))
585                 return -1;
586         
587         if (pdata->charge_ok_pin != INVALID_GPIO){              
588                 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
589                         charge_is_ok =1;
590                 }
591         }else if( pdata->charging_ok){  
592         
593                 charge_is_ok = pdata->charging_ok();
594                 }
595         
596         return charge_is_ok;
597
598
599 }
600
601
602 static int rk30_adc_battery_status_samples(struct rk30_adc_battery_data *bat)
603 {
604         int charge_level;
605
606         charge_level = bat ->charge_level;//rk_battery_get_status(bat);
607
608         if (charge_level != bat->old_charge_level){
609                 bat->old_charge_level = charge_level;
610                 bat->bat_change  = 1;
611 #if 0
612                 if(charge_level) {            
613                         rk_start_charge(bat);
614                 }
615                 else{
616                         rk_stop_charge(bat);
617                 }
618 #endif
619                 bat->bat_status_cnt = 0;        
620         }
621         if(( 1 == charge_level )&&(1 == bat->charge_full_flag) && (bat->bat_capacity < 90)){
622                 rk_start_charge(bat);  //recharge
623                 if(bat->pdata->ctrl_charge_led != NULL)
624                         bat->pdata->ctrl_charge_led(0);
625
626         }else if (charge_level) {
627                 rk_start_charge(bat);
628                 }else{
629                         rk_stop_charge(bat);
630
631                 }
632         
633
634         if(charge_level == 0){   
635         //discharge
636                 bat->charge_full_flag = 0;
637                 bat->full_times = 0;
638                 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
639         }else{
640         //charging          
641                 if( is_charge_ok(bat)  ==  INVALID_CHARGE_CHECK){
642                         if (bat->bat_capacity == 100){
643                                 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
644                                         bat->bat_status = POWER_SUPPLY_STATUS_FULL;
645                                         bat->bat_change  = 1;
646                                         bat->charge_full_flag = 1;
647                                         if(bat->pdata->ctrl_charge_led != NULL)
648                                                 bat->pdata->ctrl_charge_led(1);
649                                         rk_stop_charge(bat);
650                                                 
651                                 }
652                         }else{
653                                 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
654                                 if(bat->pdata->ctrl_charge_led != NULL)
655                                                 bat->pdata->ctrl_charge_led(0);
656                         }
657                 }else{  // pin of charge_ok_pin
658                         if (is_charge_ok(bat) != CHARGE_IS_OK ){
659
660                                 bat->full_times = 0;
661                                 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
662                         }else{
663                                 bat->full_times++;
664
665                                 if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) {
666                                         bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
667                                 }
668
669                                 if ((bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) && (bat->bat_capacity >= 99)){
670                                         if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
671                                                 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
672                                                 rk_stop_charge(bat);
673                                                 bat->bat_capacity = 100;
674                                                 bat->bat_change  = 1;
675                                         }
676                                 }
677                                 else{
678                                         bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
679                                 }
680                         }
681                 }
682         }
683
684         return charge_level;
685 }
686 static int rk_adc_voltage(struct rk30_adc_battery_data *bat, int value)
687 {
688         int voltage;
689
690         int ref_voltage; //reference_voltage
691         int pullup_res;
692         int pulldown_res;
693
694         ref_voltage = bat ->pdata->reference_voltage;
695         pullup_res = bat ->pdata->pull_up_res;
696         pulldown_res = bat ->pdata->pull_down_res;
697
698         if(ref_voltage && pullup_res && pulldown_res){
699                 
700                 voltage =  ((value * ref_voltage * (pullup_res + pulldown_res)) / (1024 * pulldown_res));
701                 
702         }else{
703                 voltage = adc_to_voltage(value);        
704         }
705                 
706                 
707         return voltage;
708
709 }
710 static void rk_handle_ripple(struct rk30_adc_battery_data *bat, int status)
711 {
712         
713         int *p_table;
714
715         if (bat->pdata->use_board_table){
716                 p_table = bat->pdata->board_batt_table; 
717
718                 if(1 == status){
719                         if(bat->bat_voltage >= p_table[2*BATT_NUM +5]+ 10)
720                                 bat->bat_voltage = p_table[2*BATT_NUM +5]  + 10;
721                         else if(bat->bat_voltage <= p_table[BATT_NUM +6]  - 10)
722                                 bat->bat_voltage =  p_table[BATT_NUM +6] - 10;
723                 }
724                 else{
725                         if(bat->bat_voltage >= p_table[BATT_NUM +5]+ 10)
726                                 bat->bat_voltage = p_table[BATT_NUM +5]  + 10;
727                         else if(bat->bat_voltage <= p_table[6]  - 10)
728                                 bat->bat_voltage =  p_table[6] - 10;
729                 }
730         }
731
732 }
733
734 //static int *pSamples;
735 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat)
736 {
737         int value;
738         int i,*pStart = bat->adc_samples, num = 0;
739         int level = bat->charge_level;
740
741
742         value = bat->adc_val;
743         adc_async_read(bat->client);
744
745         *(bat->pSamples++) = rk_adc_voltage(bat,value);
746
747         bat->bat_status_cnt++;
748         if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE)  bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1;
749
750         num = bat->pSamples - pStart;
751         
752         if (num >= NUM_VOLTAGE_SAMPLE){
753                 bat ->pSamples = pStart;
754                 num = NUM_VOLTAGE_SAMPLE;
755                 
756         }
757
758         value = 0;
759         for (i = 0; i < num; i++){
760                 value += bat->adc_samples[i];
761         }
762         bat->bat_voltage = value / num;
763
764         /*handle  ripple */
765         if(battery_test_flag == 0)
766         {
767                 if(0 == bat->pdata->use_board_table){
768                         if(1 == level){
769                                 if(bat->bat_voltage >= batt_table[2*BATT_NUM +5]+ 10)
770                                         bat->bat_voltage = batt_table[2*BATT_NUM +5]  + 10;
771                                 else if(bat->bat_voltage <= batt_table[BATT_NUM +6]  - 10)
772                                         bat->bat_voltage =  batt_table[BATT_NUM +6] - 10;
773                         }
774                         else{
775                                 if(bat->bat_voltage >= batt_table[BATT_NUM +5]+ 10)
776                                         bat->bat_voltage = batt_table[BATT_NUM +5]  + 10;
777                                 else if(bat->bat_voltage <= batt_table[6]  - 10)
778                                         bat->bat_voltage =  batt_table[6] - 10;
779                         }
780                 }else{
781                         rk_handle_ripple(bat, level);
782                 }
783
784         }else if(battery_test_flag == 2){
785         
786                 if(batt_table[3] == 0){
787                         if(bat->bat_voltage < 3400){
788                                 if((get_seconds() - gSecondsCnt) > 30){
789                                         gSecondsCnt = get_seconds();
790                                         if((gVoltageCnt - bat->bat_voltage) > 15){
791                                                 strncpy(gDischargeFlag, "off" ,3);      
792                                         }
793                                         gVoltageCnt = bat->bat_voltage;
794
795                                 }
796                         }
797                         
798                         if(bat->bat_voltage < 3400){
799                                 bat->bat_voltage = 3400;
800                         }
801                 }
802                 else{
803                         if(bat->bat_voltage < 6800){
804                                 if((get_seconds() - gSecondsCnt) > 30){
805                                         gSecondsCnt = get_seconds();
806                                         if((gDoubleVoltageCnt - bat->bat_voltage) > 30){
807                                                 strncpy(gDischargeFlag, "off" ,3);      
808                                         }
809                                         gDoubleVoltageCnt =bat->bat_voltage;
810                                 }
811                         }
812                         if(bat->bat_voltage < 6800){
813                                 bat->bat_voltage = 6800;
814                         }       
815                 }
816         }
817 }
818
819 static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage)
820 {
821         int i = 0;
822         int capacity = 0;
823
824         int  *p;
825         if (bat->pdata->use_board_table)
826                 p = bat->pdata->board_batt_table;       
827         else 
828                 p = batt_table;
829
830         if (1 == bat->charge_level){  //charge
831                 if(0 == bat->start_voltage_status ){
832                         if(BatVoltage >= (p[2*BATT_NUM +5])){
833                                 capacity = 100;
834                         }       
835                         else{
836                                 if(BatVoltage <= (p[BATT_NUM +6])){
837                                         capacity = 0;
838                                 }
839                                 else{
840                                         for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++){
841
842                                                 if(((p[i]) <= BatVoltage) && (BatVoltage <  (p[i+1]))){
843
844                                                                 capacity = (i-(BATT_NUM +6))*10 + ((BatVoltage - p[i]) *  10)/ (p[i+1]- p[i]);
845                                                         break;
846                                                 }
847                                         }
848                                 }  
849                         }
850                 }
851                 else{
852
853                         DBG("start_voltage=%d,start_capacity =%d\n", bat->charge_start_voltage, bat->charge_start_capacity);
854                         DBG("charge_down_proportion =%d,charge_up_proprotion=%d\n",bat ->charge_down_proportion,bat ->charge_up_proprotion);
855                                 if(BatVoltage >= (p[2*BATT_NUM +5])){
856                                         capacity = 100;
857                                 }else{  
858                                 
859                                         if(BatVoltage <= (p[BATT_NUM +6])){
860                                                 capacity = 0;
861                                                 }else{
862
863                                                         if(BatVoltage <bat->charge_start_voltage){
864                                                                 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++)
865                                                                         if(((p[i]) <= BatVoltage) && (BatVoltage <  (p[i+1]))){
866                                                                                 if( p[i+1] < bat->charge_start_voltage ){
867                                                                                         capacity =(i-(BATT_NUM +6))*(bat ->charge_down_proportion) + ((BatVoltage - p[i]) *  bat ->charge_down_proportion)/ (p[i+1]- p[i]);
868                                                                                 }
869                                                                                 else {
870                                                                                         capacity = (i-(BATT_NUM +6))*(bat ->charge_down_proportion) + ((BatVoltage - p[i]) *  bat ->charge_down_proportion)/ (bat->charge_start_voltage - p[i]);
871                                                                                 }
872                                                                                 break;
873                                                                         }
874
875
876                                                         }else{
877
878                                                                         if(BatVoltage > bat->charge_start_voltage){
879                                                                                 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++)
880                                                                                         if(((p[i]) <= BatVoltage) && (BatVoltage <  (p[i+1]))){
881                                                                                                 if( p[i] > bat->charge_start_voltage ){
882                                                                                                         capacity = bat->charge_start_capacity + (i - (BATT_NUM +6)-bat->charge_start_capacity/10)*(bat ->charge_up_proprotion) + ((BatVoltage - p[i]) *  bat ->charge_up_proprotion)/ (p[i+1]- p[i]);
883                                                                                                 }
884                                                                                                 else {
885                                                                                                        capacity = bat->charge_start_capacity + (i - (BATT_NUM +6)-bat->charge_start_capacity/10)*(bat ->charge_up_proprotion) + ((BatVoltage - p[i]) *  bat ->charge_up_proprotion)/ (p[i+1] - bat->charge_start_voltage );
886                                                                                                         }
887                                                                                                 break;
888                                                                                         }
889
890                                                                         }else{
891
892                                                                                 if(BatVoltage  ==  bat->charge_start_voltage)
893                                                                                         capacity = bat ->charge_start_capacity;
894                                                                                 }
895                                                                 }
896                                                         }
897                                         }
898
899                         }
900
901                 }
902                 else{  //discharge
903                         if(BatVoltage >= (p[BATT_NUM +5])){
904                                 capacity = 100;
905                         }       
906                         else{
907                                 if(BatVoltage <= (p[6])){
908                                         capacity = 0;
909                                 }
910                                 else{
911                                         for(i = 6; i < BATT_NUM +5; i++){
912                                                 if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){
913                                                         capacity = (i-6)*10+ ((BatVoltage - p[i]) *10 )/ (p[i+1]- p[i]) ;
914                                                         break;
915                                                 }
916                                         }
917                                 }  
918
919                         }
920
921
922                 }
923
924         DBG("real_voltage_to_capacity =%d\n" ,capacity);
925     return capacity;
926 }
927
928 static void rk_usb_charger(struct rk30_adc_battery_data *bat)
929 {
930
931         int capacity = 0;
932 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
933         int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
934         int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
935
936         if((1 == bat ->charge_level)&&( 0 == bat ->start_voltage_status)){
937                         bat ->charge_start_voltage = bat ->bat_voltage;
938                         bat ->start_voltage_status = 1;
939                         bat ->charge_start_capacity = bat ->bat_capacity;
940                         bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
941                         bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
942         }
943
944         capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
945
946                 if (capacity > bat->bat_capacity){
947                         if(capacity > bat->bat_capacity + 10 )
948                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10;  //5s
949                         else if(capacity > bat->bat_capacity + 7 )
950                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
951                                 else if(capacity > bat->bat_capacity + 3 )
952                                         timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
953                         if (++(bat->gBatCapacityusbChargeCnt) >= timer_of_charge_sample){
954                                 bat->gBatCapacityusbChargeCnt  = 0;
955                                 if (bat->bat_capacity <= 99){
956                                         bat->bat_capacity++;
957                                         bat->bat_change  = 1;
958                                 }
959                         }
960                         bat->gBatCapacityChargeCnt = 0;
961                         bat ->gBatCapacityusbdisChargeCnt = 0;//get_suspend_state(void)
962                 }else //if(( get_suspend_state() != PM_SUSPEND_MEM)&&(capacity < bat->bat_capacity)){
963                 // if((gpio_get_value (bat->pdata->back_light_pin) == 1)&&(capacity < bat->bat_capacity)){
964                 if((capacity < bat->bat_capacity)){
965                                 DBG("USB CHARGE DOWN\n");
966
967                 //      if (capacity < bat->bat_capacity){
968                                 if(capacity <10){
969                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 40; // 13
970                                 }else if(capacity < 20){
971                                         if(capacity + 3 > bat->bat_capacity  )
972                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5;  //5s
973                                         else if(capacity  + 7 > bat->bat_capacity )
974                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
975                                         else if(capacity  + 10> bat->bat_capacity )
976                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -25; // 13
977                                         else
978                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 35; // 13
979                                 }else{
980                                         if(capacity + 3 > bat->bat_capacity  )
981                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5;  //5s
982                                         else if(capacity  + 7 > bat->bat_capacity )
983                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
984                                         else if(capacity  + 10> bat->bat_capacity )
985                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 15; // 13
986                                         else
987                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 20; // 13
988                         }
989                                 
990                                 if (++(bat->gBatCapacityusbdisChargeCnt) >= timer_of_discharge_sample){
991                                         bat->gBatCapacityusbdisChargeCnt = 0;
992                                         if (bat->bat_capacity > 0){
993                                                 bat->bat_capacity-- ;
994                                                 bat->bat_change  = 1;
995                                         }
996                                 }
997                                 
998                         //}
999                         bat->gBatCapacityusbChargeCnt  = 0;
1000
1001                 }
1002                 else //if(get_suspend_state() == PM_SUSPEND_MEM){
1003                         //if(gpio_get_value (bat->pdata->back_light_pin) == 0){
1004                         {
1005
1006
1007                         bat->gBatCapacityusbdisChargeCnt = 0;
1008                         // (bat->gBatCapacityusbChargeCnt)++;
1009                         if( is_charge_ok(bat) != INVALID_CHARGE_CHECK){
1010                                 if( is_charge_ok(bat) == CHARGE_IS_OK){
1011
1012                                         if (++bat->gBatCapacityusbChargeCnt >= timer_of_charge_sample-30){
1013                                                 bat->gBatCapacityusbChargeCnt = 0;
1014                                                         if (bat->bat_capacity <= 99){
1015                                                                 bat->bat_capacity++;
1016                                                                 bat->bat_change  = 1;
1017                                                         }
1018                                         }
1019                                 }else{
1020                                                 if (capacity > bat->capacitytmp){
1021                                                         bat->gBatCapacityChargeCnt = 0;
1022                                                 }else{
1023
1024                                                         if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
1025                                                                 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1026
1027                                                                 if (bat->bat_capacity < 99){
1028                                                                         bat->bat_capacity++;
1029                                                                         bat->bat_change  = 1;
1030                                                                 }
1031                                                         }
1032                                                 }
1033                                         }
1034
1035                                 }
1036                                 else{
1037                                         if (capacity > bat->capacitytmp){
1038                                                         bat->gBatCapacityChargeCnt = 0;
1039                                         }else{
1040                                                 if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityusbChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
1041                                                         bat->gBatCapacityusbChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1042                                                                 if (bat->bat_capacity <= 99){
1043                                                                         bat->bat_capacity++;
1044                                                                         bat->bat_change  = 1;
1045                                                         }
1046                                                 }
1047                                         }
1048                         }
1049
1050                 }
1051                 bat->capacitytmp = capacity;
1052
1053 }
1054 static void rk_ac_charger(struct rk30_adc_battery_data *bat)
1055 {
1056         int capacity = 0;
1057         int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
1058
1059         if((1 == bat->charge_level)&&( 0 == bat->start_voltage_status)){
1060                 bat->charge_start_voltage = bat->bat_voltage;
1061                 bat->start_voltage_status = 1;
1062                 bat->charge_start_capacity = bat->bat_capacity;
1063                 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
1064                 bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
1065         }
1066         capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1067                 if (capacity > bat->bat_capacity){
1068                         if(capacity > bat->bat_capacity + 10 )
1069                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10;  //5s
1070                         else if(capacity > bat->bat_capacity + 7 )
1071                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
1072                                 else if(capacity > bat->bat_capacity + 3 )
1073                                         timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
1074                         if (++(bat->gBatCapacityacChargeCnt) >= timer_of_charge_sample){
1075                                 bat->gBatCapacityacChargeCnt  = 0;
1076                                 if (bat->bat_capacity <= 99){
1077                                         bat->bat_capacity++;
1078                                         bat->bat_change  = 1;
1079                                 }
1080                         }
1081                         bat->gBatCapacityChargeCnt = 0;
1082                 }
1083                 else{  
1084                             bat->gBatCapacityacChargeCnt = 0;
1085                             (bat->gBatCapacityChargeCnt)++;
1086                                 if( is_charge_ok(bat) != INVALID_CHARGE_CHECK){
1087                                         if( is_charge_ok(bat) == CHARGE_IS_OK){
1088                                                 if (bat->gBatCapacityChargeCnt >= timer_of_charge_sample){
1089                                                         bat->gBatCapacityChargeCnt = 0;
1090                                                         if (bat->bat_capacity < 99){
1091                                                                 bat->bat_capacity++;
1092                                                                 bat->bat_change  = 1;
1093                                                         }
1094                                                 }
1095                                         }else{
1096                                                 if (capacity > bat->capacitytmp){
1097                                                         bat->gBatCapacityChargeCnt = 0;
1098                                                 }
1099                                                 else{
1100
1101                                                         if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
1102                                                                 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1103
1104                                                                 if (bat->bat_capacity < 99){
1105                                                                         bat->bat_capacity++;
1106                                                                         bat->bat_change  = 1;
1107                                                                 }
1108                                                         }
1109                                                 }
1110                                         }
1111
1112                         }else{
1113                                 if (capacity > bat->capacitytmp){
1114                                         bat->gBatCapacityChargeCnt = 0;
1115                                 }
1116                                 else{
1117
1118                                         if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
1119                                                 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1120
1121                                                 if (bat->bat_capacity <= 99){
1122                                                         bat->bat_capacity++;
1123                                                         bat->bat_change  = 1;
1124                                                 }
1125                                         }
1126                                 }
1127                                 
1128
1129                         }            
1130                 }
1131                 bat->capacitytmp = capacity;
1132 }
1133 static void rk_battery_charger(struct rk30_adc_battery_data *bat)
1134 {
1135
1136         int capacity = 0;
1137         int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
1138         
1139         capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1140
1141                 if (capacity < bat->bat_capacity){
1142                                 if(capacity <10){
1143                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 40; // 13
1144                                 }else if(capacity < 20){
1145                                         if(capacity + 3 > bat->bat_capacity  )
1146                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5;  //5s
1147                                         else if(capacity  + 7 > bat->bat_capacity )
1148                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
1149                                         else if(capacity  + 10> bat->bat_capacity )
1150                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -25; // 13
1151                                         else
1152                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 35; // 13
1153                                 }else{
1154                                         if(capacity + 3 > bat->bat_capacity  )
1155                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5;  //5s
1156                                         else if(capacity  + 7 > bat->bat_capacity )
1157                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
1158                                         else if(capacity  + 10> bat->bat_capacity )
1159                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 15; // 13
1160                                         else
1161                                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 20; // 13
1162
1163                         }
1164                         if (++(bat->gBatCapacityDisChargeCnt) >= timer_of_discharge_sample){
1165                                 bat->gBatCapacityDisChargeCnt = 0;
1166                                 if (bat->bat_capacity > 0){
1167                                         bat->bat_capacity-- ;
1168                                         bat->bat_change  = 1;
1169                                 }
1170                         }
1171                 }
1172                 else{
1173                         bat->gBatCapacityDisChargeCnt = 0;
1174                 }
1175                 bat->gBatCapacityChargeCnt = 0;
1176                 bat->gBatCapacityusbdisChargeCnt=0 ;
1177                 bat->gBatCapacityusbChargeCnt =0;
1178                 bat->gBatCapacityacChargeCnt = 0;
1179                 bat->start_voltage_status = 0;
1180                 
1181                 bat->capacitytmp = capacity;
1182
1183
1184
1185 }
1186
1187 static void rk30_adc_battery_capacity_samples(struct rk30_adc_battery_data *bat)
1188 {
1189 //      int capacity = 0;
1190 //      int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
1191         int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
1192
1193         if (bat->bat_status_cnt < NUM_VOLTAGE_SAMPLE)  {
1194                 bat->gBatCapacityDisChargeCnt = 0;
1195                 bat->gBatCapacityChargeCnt    = 0;
1196                 bat->gBatCapacityacChargeCnt = 0;
1197                 return;
1198         }
1199         
1200         if(1 == bat->charge_level){
1201 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)   
1202                 if(1 == bat->usb_charging)
1203                         rk_usb_charger(bat);
1204                 else
1205                         rk_ac_charger(bat);
1206 #else
1207                 rk_ac_charger(bat);
1208 #endif
1209         }else{
1210                 rk_battery_charger(bat);
1211
1212         }
1213         
1214 }
1215
1216 //static int poweron_check = 0;
1217 static void rk30_adc_battery_poweron_capacity_check(struct rk30_adc_battery_data *bat)
1218 {
1219
1220         int new_capacity, old_capacity;
1221          int cnt = 50 ;
1222
1223         new_capacity = bat ->bat_capacity;
1224 //#if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1225 //      if(dwc_vbus_status() != 0)
1226 //              bat ->bat_status = POWER_SUPPLY_STATUS_CHARGING; // add for charging.
1227 //#endif
1228                 
1229         while( cnt -- ){
1230             old_capacity = rk30_adc_battery_load_capacity();
1231             if( old_capacity >= 0 ){
1232                 break ;
1233             }
1234             msleep(100);
1235         }
1236
1237         if ((old_capacity < 0) || (old_capacity > 100)){
1238                 old_capacity = new_capacity;
1239         }    
1240
1241         if (bat ->bat_status == POWER_SUPPLY_STATUS_FULL){
1242                 if (new_capacity > 80){
1243                         bat ->bat_capacity = 100;
1244                 }
1245         }
1246         else if (bat ->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
1247         //chargeing state
1248
1249                 if( bat  ->pdata->is_reboot_charging == 1)
1250                         bat ->bat_capacity = (old_capacity < 10) ?(old_capacity+2):old_capacity;
1251                 else
1252                         bat ->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
1253         }else{
1254                 if(new_capacity > old_capacity + 50 )
1255                         bat ->bat_capacity = new_capacity;
1256                 else
1257                         bat ->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity;  //avoid the value of capacity increase 
1258         }
1259
1260
1261         bat ->bat_change = 1;
1262 }
1263 #define to_battery_usb_device_info(x) container_of((x), \
1264                 struct rk30_adc_battery_data, usb);
1265
1266 static int rk30_adc_battery_get_usb_property(struct power_supply *psy, 
1267                                     enum power_supply_property psp,
1268                                     union power_supply_propval *val)
1269 {
1270         struct rk30_adc_battery_data *bat=  to_battery_usb_device_info(psy);
1271
1272         switch (psp) {
1273                 case POWER_SUPPLY_PROP_ONLINE:
1274                         if (psy->type == POWER_SUPPLY_TYPE_USB){
1275                                 val->intval = bat ->usb_charging;
1276                                 if( 1 == bat->charge_full_flag)
1277                                         val->intval = 0;
1278
1279                         }
1280                         break;
1281
1282                 default:
1283                         return -EINVAL;
1284         }
1285         
1286         return 0;
1287
1288 }
1289
1290 static enum power_supply_property rk30_adc_battery_usb_props[] = {
1291     
1292         POWER_SUPPLY_PROP_ONLINE,
1293 };
1294
1295 static struct power_supply rk30_usb_supply = 
1296 {
1297         .name = "usb",
1298         .type = POWER_SUPPLY_TYPE_USB,
1299
1300         .get_property   = rk30_adc_battery_get_usb_property,
1301
1302         .properties     = rk30_adc_battery_usb_props,
1303         .num_properties = ARRAY_SIZE(rk30_adc_battery_usb_props),
1304 };
1305
1306 static irqreturn_t rk30_adc_battery_dc_wakeup(int irq, void *dev_id)
1307 {   
1308         disable_irq_nosync(irq);
1309         queue_work(gBatteryData->wq, &gBatteryData->dcwakeup_work);
1310         return IRQ_HANDLED;
1311 }
1312
1313 #define to_battery_ac_device_info(x) container_of((x), \
1314                 struct rk30_adc_battery_data, ac);
1315
1316 static int rk30_adc_battery_get_ac_property(struct power_supply *psy,
1317                         enum power_supply_property psp,
1318                         union power_supply_propval *val)
1319 {
1320         int ret;
1321
1322         struct rk30_adc_battery_data *bat = to_battery_ac_device_info(psy);
1323
1324         switch (psp) {
1325         case POWER_SUPPLY_PROP_ONLINE:
1326                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
1327                         val->intval = bat ->ac_charging;
1328                         if (strstr(saved_command_line,"charger")  == NULL ){
1329                                 if( 1 == bat->charge_full_flag)
1330                                         val->intval = 0;
1331                         }
1332                 }
1333                 break;
1334                 
1335         default:
1336                 ret = -EINVAL;
1337                 break;
1338         }
1339
1340         return 0;
1341 }
1342
1343 static enum power_supply_property rk30_adc_battery_ac_props[] = 
1344 {
1345         POWER_SUPPLY_PROP_ONLINE,
1346 };
1347
1348 static struct power_supply rk30_ac_supply = 
1349 {
1350         .name = "ac",
1351         .type = POWER_SUPPLY_TYPE_MAINS,
1352
1353         .get_property   = rk30_adc_battery_get_ac_property,
1354
1355         .properties     = rk30_adc_battery_ac_props,
1356         .num_properties = ARRAY_SIZE(rk30_adc_battery_ac_props),
1357 };
1358 static void rk30_adc_battery_dcdet_delaywork(struct work_struct *work)
1359 {
1360
1361         int ret;
1362         struct rk30_adc_battery_platform_data *pdata;
1363         int irq;
1364         int irq_flag;
1365         struct rk30_adc_battery_data  *bat = container_of((work), \
1366                 struct rk30_adc_battery_data, dcwakeup_work);
1367
1368         rk28_send_wakeup_key(); // wake up the system
1369
1370         
1371         pdata    = bat->pdata;
1372         irq        = gpio_to_irq(pdata->dc_det_pin);
1373         free_irq(irq, NULL);
1374         msleep(10);
1375         irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1376         ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);// reinitialize the DC irq 
1377         if (ret) {
1378                 free_irq(irq, NULL);
1379         }
1380
1381         power_supply_changed(&bat ->ac);
1382
1383         bat ->bat_status_cnt = 0;        //the state of battery is change
1384
1385 }
1386
1387
1388 static int rk30_adc_battery_get_status(struct rk30_adc_battery_data *bat)
1389 {
1390         return (bat->bat_status);
1391 }
1392
1393 static int rk30_adc_battery_get_health(struct rk30_adc_battery_data *bat)
1394 {
1395         return POWER_SUPPLY_HEALTH_GOOD;
1396 }
1397
1398 static int rk30_adc_battery_get_present(struct rk30_adc_battery_data *bat)
1399 {
1400         return (bat->bat_voltage < BATT_MAX_VOL_VALUE) ? 0 : 1;
1401 }
1402
1403 static int rk30_adc_battery_get_voltage(struct rk30_adc_battery_data *bat)
1404 {
1405         return (bat->bat_voltage );
1406 }
1407
1408 static int rk30_adc_battery_get_capacity(struct rk30_adc_battery_data *bat)
1409 {
1410         return (bat->bat_capacity);
1411 }
1412
1413 static int rk30_adc_battery_get_property(struct power_supply *psy,
1414                                  enum power_supply_property psp,
1415                                  union power_supply_propval *val)
1416 {               
1417         int ret = 0;
1418         struct rk30_adc_battery_data  *bat = container_of((psy), \
1419                         struct rk30_adc_battery_data, bat);
1420         switch (psp) {
1421                 case POWER_SUPPLY_PROP_STATUS:
1422                         val->intval = rk30_adc_battery_get_status(bat);
1423                         DBG("gBatStatus=%d\n",val->intval);
1424                         break;
1425                 case POWER_SUPPLY_PROP_HEALTH:
1426                         val->intval = rk30_adc_battery_get_health(bat);
1427                         DBG("gBatHealth=%d\n",val->intval);
1428                         break;
1429                 case POWER_SUPPLY_PROP_PRESENT:
1430                         val->intval = rk30_adc_battery_get_present(bat);
1431                         DBG("gBatPresent=%d\n",val->intval);
1432                         break;
1433                 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1434                         val ->intval = rk30_adc_battery_get_voltage(bat);
1435                         DBG("gBatVoltage=%d\n",val->intval);
1436                         break;
1437                 case POWER_SUPPLY_PROP_CAPACITY:
1438                         if(battery_test_flag == 2)
1439                                 val->intval = 50;
1440                         else
1441                                 val->intval = rk30_adc_battery_get_capacity(bat);
1442                         DBG("gBatCapacity=%d%%\n",val->intval);
1443                         break;
1444                 case POWER_SUPPLY_PROP_TECHNOLOGY:
1445                         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
1446                         break;
1447                 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1448                         val->intval = BATT_MAX_VOL_VALUE;
1449                         break;
1450                 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1451                         val->intval = BATT_ZERO_VOL_VALUE;
1452                         break;
1453                 default:
1454                         ret = -EINVAL;
1455                         break;
1456         }
1457
1458         return ret;
1459 }
1460
1461 static enum power_supply_property rk30_adc_battery_props[] = {
1462
1463         POWER_SUPPLY_PROP_STATUS,
1464         POWER_SUPPLY_PROP_HEALTH,
1465         POWER_SUPPLY_PROP_PRESENT,
1466         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1467         POWER_SUPPLY_PROP_TECHNOLOGY,
1468         POWER_SUPPLY_PROP_CAPACITY,
1469         POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1470         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1471 };
1472
1473 static struct power_supply rk30_battery_supply = 
1474 {
1475         .name = "battery",
1476         .type = POWER_SUPPLY_TYPE_BATTERY,
1477
1478         .get_property   = rk30_adc_battery_get_property,
1479
1480         .properties     = rk30_adc_battery_props,
1481         .num_properties = ARRAY_SIZE(rk30_adc_battery_props),
1482 };
1483
1484 #ifdef CONFIG_PM
1485 static void rk30_adc_battery_resume_check(struct rk30_adc_battery_data *bat)
1486 {
1487         int i;
1488         int level,oldlevel;
1489         int new_capacity, old_capacity;
1490 //      struct rk30_adc_battery_data *bat = gBatteryData;
1491
1492         bat ->old_charge_level = -1;
1493         bat ->pSamples = bat->adc_samples;
1494
1495         adc_sync_read(bat->client);                             //start adc sample
1496         level = oldlevel =  rk_battery_get_status(bat);//rk30_adc_battery_status_samples(bat);//init charge status
1497
1498         for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++) {               //0.3 s   
1499         
1500                 mdelay(1);
1501                 rk30_adc_battery_voltage_samples(bat);              //get voltage
1502                 level =          rk_battery_get_status(bat);// rk30_adc_battery_status_samples(bat);       //check charge status
1503                 if (oldlevel != level){         
1504                     oldlevel = level;                               //if charge status changed, reset sample
1505                     i = 0;
1506                 }        
1507         }
1508         new_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1509         old_capacity =bat-> suspend_capacity;
1510
1511         //if (bat->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
1512         if( 1 == level ){
1513         //chargeing state
1514                 bat->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
1515         }
1516         else{
1517                 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity;  // aviod the value of capacity increase    dicharge
1518         }
1519
1520 }
1521
1522 static int rk30_adc_battery_suspend(struct platform_device *dev, pm_message_t state)
1523 {
1524         int irq;
1525         struct rk30_adc_battery_data *data = platform_get_drvdata(dev);
1526
1527         data ->suspend_capacity = data->bat_capacity;
1528         data ->suspend_time = get_seconds();
1529         cancel_delayed_work(&data ->delay_work);
1530         
1531         if( data ->pdata->batt_low_pin != INVALID_GPIO){
1532                 
1533                 irq = gpio_to_irq(data ->pdata->batt_low_pin);
1534                 enable_irq(irq);
1535                 enable_irq_wake(irq);
1536         }
1537
1538         return 0;
1539 }
1540
1541 static int rk30_adc_battery_resume(struct platform_device *dev)
1542 {
1543         int irq;
1544         struct rk30_adc_battery_data *data = platform_get_drvdata(dev);
1545         data ->resume_time = get_seconds();
1546         data ->resume = true;
1547         queue_delayed_work(data->wq, &data ->delay_work, msecs_to_jiffies(100));
1548         if( data ->pdata->batt_low_pin != INVALID_GPIO){
1549                 
1550                 irq = gpio_to_irq(data ->pdata ->batt_low_pin);
1551                 disable_irq_wake(irq);
1552                 disable_irq(irq);
1553         }
1554         return 0;
1555 }
1556 #else
1557 #define rk30_adc_battery_suspend NULL
1558 #define rk30_adc_battery_resume NULL
1559 #endif
1560
1561
1562 unsigned long AdcTestCnt = 0;
1563 static void rk30_adc_battery_timer_work(struct work_struct *work)
1564 {
1565 struct rk30_adc_battery_data  *bat = container_of((work), \
1566                 struct rk30_adc_battery_data, delay_work);
1567
1568 #ifdef CONFIG_PM
1569         if (bat ->resume) {
1570         //      if( (bat->resume_time - bat->suspend_time) >= 1800 )
1571                         rk30_adc_battery_resume_check(bat);
1572                 //else
1573                         //bat->bat_capacity = bat->suspend_capacity;
1574                 bat ->resume = false;
1575                 bat ->bat_change =1;
1576         }
1577 #endif
1578         bat->stop_check = 1;
1579         if (bat ->poweron_check){   
1580                 bat ->poweron_check = 0;
1581                 rk30_adc_battery_poweron_capacity_check(bat);
1582         }
1583
1584         bat ->charge_level = rk_battery_get_status(bat);
1585         rk30_adc_battery_status_samples(bat);
1586         rk30_adc_battery_voltage_samples(bat);
1587         rk30_adc_battery_capacity_samples(bat);
1588
1589         if( 0 == bat ->pdata ->charging_sleep){
1590                 if( 1 == bat->charge_level){  // charge
1591                         if(0 == bat->status_lock ){                     
1592                                 wake_lock(&batt_wake_lock);  //lock
1593                                 bat ->status_lock = 1; 
1594                         }
1595                 }
1596                 else{
1597                         if(1 == bat ->status_lock ){                    
1598                                 wake_unlock(&batt_wake_lock);  //unlock
1599                                 bat ->status_lock = 0; 
1600                         }
1601
1602                 }
1603         }
1604         
1605         /*update battery parameter after adc and capacity has been changed*/
1606         if(bat ->bat_change){
1607                 bat ->bat_change= 0;
1608                 if(0 == bat ->bat_capacity){
1609                         bat ->ac_charging = 0;
1610                         bat ->usb_charging = 0;
1611
1612                 }
1613                 rk30_adc_battery_put_capacity(bat ->bat_capacity);
1614                 power_supply_changed(&bat ->bat);
1615                 power_supply_changed(&bat ->ac);
1616 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1617                 power_supply_changed(&bat ->usb);
1618 #endif
1619
1620         }
1621
1622         //if (rk30_battery_dbg_level){
1623                 if (++AdcTestCnt >= 2)
1624                         {
1625                         AdcTestCnt = 0;
1626
1627                         DBG("Status = %d, RealAdcVal = %d, RealVol = %d,gBatVol = %d, gBatCap = %d, RealCapacity = %d, batt_dischargecnt = %d\n,  chargecnt = %d,ac_count = %d, usb_count =%d ,usb_dischargecount =%d\n", 
1628                         bat ->bat_status, bat ->adc_val, rk_adc_voltage(bat, bat ->adc_val), 
1629                         bat ->bat_voltage, bat ->bat_capacity, bat ->capacitytmp, bat ->gBatCapacityDisChargeCnt, bat ->gBatCapacityChargeCnt,
1630                         bat ->gBatCapacityacChargeCnt, bat ->gBatCapacityusbChargeCnt, bat ->gBatCapacityusbdisChargeCnt);
1631
1632                 }
1633         //}
1634         queue_delayed_work(bat ->wq, &bat ->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1635
1636 }
1637
1638
1639 static int rk30_adc_battery_io_init(struct rk30_adc_battery_platform_data *pdata)
1640 {
1641         int ret = 0;
1642         
1643         if (pdata->io_init) {
1644                 pdata->io_init();
1645                 return 0;
1646         }
1647         
1648         //charge control pin
1649         if (pdata->charge_set_pin != INVALID_GPIO){
1650                 ret = gpio_request(pdata->charge_set_pin, NULL);
1651                 if (ret) {
1652                         printk("failed to request dc_det gpio\n");
1653                         goto error;
1654                         }
1655                 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
1656         }
1657         
1658         //dc charge detect pin
1659         if (pdata->dc_det_pin != INVALID_GPIO){
1660                 ret = gpio_request(pdata->dc_det_pin, NULL);
1661                 if (ret) {
1662                         printk("failed to request dc_det gpio\n");
1663                         goto error;
1664                 }
1665         
1666                 gpio_pull_updown(pdata->dc_det_pin, GPIOPullUp);//important
1667                 ret = gpio_direction_input(pdata->dc_det_pin);
1668                 if (ret) {
1669                         printk("failed to set gpio dc_det input\n");
1670                         goto error;
1671                 }
1672         }
1673         
1674         //charge ok detect
1675         if (pdata->charge_ok_pin != INVALID_GPIO){
1676                 ret = gpio_request(pdata->charge_ok_pin, NULL);
1677                 if (ret) {
1678                         printk("failed to request charge_ok gpio\n");
1679                         goto error;
1680                 }
1681         
1682                 gpio_pull_updown(pdata->charge_ok_pin, GPIOPullUp);//important
1683                 ret = gpio_direction_input(pdata->charge_ok_pin);
1684                 if (ret) {
1685                         printk("failed to set gpio charge_ok input\n");
1686                         goto error;
1687                 }
1688         }
1689         //batt low pin
1690         if( pdata->batt_low_pin != INVALID_GPIO){
1691                 ret = gpio_request(pdata->batt_low_pin, NULL);
1692                 if (ret) {
1693                         printk("failed to request batt_low_pin gpio\n");
1694                         goto error;
1695                 }
1696         
1697                 gpio_pull_updown(pdata->batt_low_pin, GPIOPullUp); 
1698                 ret = gpio_direction_input(pdata->batt_low_pin);
1699                 if (ret) {
1700                         printk("failed to set gpio batt_low_pin input\n");
1701                         goto error;
1702                 }
1703         }
1704     
1705         return 0;
1706 error:
1707         return -1;
1708 }
1709
1710 extern void kernel_power_off(void);
1711 int get_battery_status(void)
1712 {
1713         return system_lowerpower;
1714 }
1715 static void rk30_adc_battery_check(struct rk30_adc_battery_data *bat)
1716 {
1717         int i;
1718         int level,oldlevel;
1719         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1720
1721 //      pSamples = bat->adc_samples;
1722
1723         adc_sync_read(bat->client);                             //start adc sample
1724         level = oldlevel =       rk_battery_get_status(bat);// rk30_adc_battery_status_samples(bat);//init charge status
1725
1726         for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){                //0.3 s
1727                 mdelay(1);
1728                 rk30_adc_battery_voltage_samples(bat);              //get voltage
1729                 //level = rk30_adc_battery_status_samples(bat);       //check charge status
1730                 level = rk_battery_get_status(bat);
1731
1732                 if (oldlevel != level){
1733                         oldlevel = level;                               //if charge status changed, reset sample
1734                         i = 0;
1735                 }    
1736                 bat->charge_level = level;
1737         }
1738
1739
1740         bat->bat_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);  //init bat_capacity
1741
1742         if((1 == level)&&(0 == bat->start_voltage_status )){
1743                         bat->charge_start_voltage = bat->bat_voltage;
1744                         bat->start_voltage_status = 1;
1745                         bat->charge_start_capacity = bat->bat_capacity;
1746                         bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
1747                         bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
1748         }
1749         
1750         if (get_ac_status(bat) || dwc_otg_check_dpdm() ){
1751                 if (is_charge_ok(bat)  == 1){
1752                         bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1753                         bat->bat_capacity = 100;
1754                 }
1755 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1756                 if( 0 != dwc_otg_check_dpdm() )
1757                         bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1758                 if(dwc_otg_check_dpdm() == 0){
1759                         bat->usb_charging = 0;
1760                 }else if(dwc_otg_check_dpdm() == 1){
1761                         bat->usb_charging = 1;
1762                         if(bat -> pdata ->control_usb_charging)
1763                                 bat -> pdata ->control_usb_charging(0);
1764                 }else if(dwc_otg_check_dpdm() == 2){
1765                         bat->usb_charging = 0;
1766                         bat -> ac_charging = 1;
1767                         if(bat -> pdata ->control_usb_charging)
1768                                 bat -> pdata ->control_usb_charging(1); 
1769                 }
1770 #endif
1771                 if(1 == get_ac_status(bat)){
1772                         bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1773                         bat -> ac_charging = 1;
1774                 }
1775                 power_supply_changed(&bat ->ac);
1776 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1777                 power_supply_changed(&bat ->usb);
1778 #endif
1779
1780         }
1781 #if 0
1782         if (bat->bat_capacity == 0) {
1783                 bat->bat_capacity = 1;
1784                 system_lowerpower = 1;
1785         }
1786 #endif
1787         if(0 !=bat ->pdata->low_voltage_protection ){
1788                 if((bat->bat_voltage <=  bat ->pdata->low_voltage_protection)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING))
1789                         system_lowerpower = 1;
1790         }else{
1791                 if((bat->bat_voltage <= BATT_ZERO_VOL_VALUE)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING))
1792                         system_lowerpower = 1;
1793
1794         }       
1795 #if 1
1796                 if ((bat->bat_voltage <= BATT_ZERO_VOL_VALUE)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING)){
1797                         kernel_power_off();
1798                 }
1799 #endif
1800
1801 }
1802
1803 static void rk30_adc_battery_callback(struct adc_client *client, void *param, int result)
1804 {
1805         struct rk30_adc_battery_data  *bat = container_of((client), \
1806                                 struct rk30_adc_battery_data, client);
1807
1808         if (result < 0){
1809                 DBG("adc_battery_callback    resule < 0 , the value ");
1810                 return;
1811         }else{
1812                 gBatteryData->adc_val = result;
1813                 DBG("result = %d, gBatteryData->adc_val = %d\n", result, gBatteryData->adc_val );
1814         }
1815         return;
1816 }
1817
1818 static void rk30_adc_battery_lowerpower_delaywork(struct work_struct *work)
1819 {
1820         int irq;
1821
1822         struct rk30_adc_battery_data  *bat = container_of((work), \
1823                         struct rk30_adc_battery_data, lowerpower_work);
1824
1825         if( bat->pdata->batt_low_pin != INVALID_GPIO){
1826                 irq = gpio_to_irq(bat ->pdata ->batt_low_pin);
1827                 disable_irq(irq);
1828         }
1829
1830         printk("lowerpower\n");
1831         rk28_send_wakeup_key(); // wake up the system   
1832         return;
1833 }
1834
1835
1836 static irqreturn_t rk30_adc_battery_low_wakeup(int irq,void *dev_id)
1837 {
1838         queue_work(gBatteryData->wq, &gBatteryData->lowerpower_work);
1839         return IRQ_HANDLED;
1840 }
1841
1842 static void rk_adc_battery_check_work(struct work_struct *work)
1843 {
1844         struct rk30_adc_battery_data  *bat = container_of((work), \
1845                         struct rk30_adc_battery_data, check_work);
1846         
1847                 if(1 == get_ac_status(bat)){
1848                         bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1849                         bat -> ac_charging = 1;
1850                 }
1851                 
1852                 power_supply_changed(&bat ->ac);
1853         if(bat->stop_check != 1)
1854                 queue_delayed_work(bat ->wq, &bat ->check_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1855
1856 }
1857 static int rk30_adc_battery_probe(struct platform_device *pdev)
1858 {
1859         int    ret;
1860         int    irq;
1861         int    irq_flag;
1862         struct adc_client                   *client;
1863         struct rk30_adc_battery_data          *data;
1864         struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1865         gSecondsCnt = get_seconds();
1866         data = kzalloc(sizeof(*data), GFP_KERNEL);
1867         if (data == NULL) {
1868                 ret = -ENOMEM;
1869                 goto err_data_alloc_failed;
1870         }
1871         memset(data, 0, sizeof(struct rk30_adc_battery_data));
1872         gBatteryData = data;
1873
1874         platform_set_drvdata(pdev, data);
1875
1876         data->pdata = pdata;
1877         data->status_lock = 0;  
1878         data->old_charge_level = -1;
1879         data->capacitytmp = 0;
1880         data->suspend_capacity = 0;
1881         data->ac_charging = 0;
1882         data->usb_charging = 0;
1883         data->full_times = 0;
1884         data->gBatCapacityDisChargeCnt =0;
1885         data->gBatCapacityChargeCnt=0;
1886         data->gBatCapacityusbdisChargeCnt=0 ;
1887         data->gBatCapacityusbChargeCnt =0;
1888         data->gBatCapacityacChargeCnt = 0;
1889         data->charge_source_now = 0;
1890         data->charge_soure_old = 0;
1891         data->start_voltage_status = 0;
1892         data->charge_full_flag =0;
1893         data->pSamples = data->adc_samples;
1894         data->stop_check = 0;
1895
1896         data->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1897         wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");        
1898         wake_lock_init(&charge_display_lock, WAKE_LOCK_SUSPEND, "charge_display_lock"); //charge_display_lock
1899         
1900         ret = rk30_adc_battery_io_init(pdata);
1901          if (ret) {
1902                 ret = -EINVAL;
1903                 goto err_io_init;
1904         }
1905     
1906         memset(data->adc_samples, 0, sizeof(int)*(NUM_VOLTAGE_SAMPLE + 2));
1907
1908          //register adc for battery sample
1909          if(0 == pdata->adc_channel)
1910                 client = adc_register(0, rk30_adc_battery_callback, NULL);  //pdata->adc_channel = ani0
1911         else
1912                 client = adc_register(pdata->adc_channel, rk30_adc_battery_callback, NULL);  
1913         if(!client){
1914                 ret = -EINVAL;
1915                 goto err_adc_register_failed;
1916         }
1917             
1918          //variable init
1919         data->client  = client;
1920         data->adc_val = adc_sync_read(client);
1921
1922         data ->bat = rk30_battery_supply;
1923         ret = power_supply_register(&pdev->dev,&data ->bat);
1924         if (ret){
1925                 ret = -EINVAL;
1926                 printk(KERN_INFO "fail to battery power_supply_register\n");
1927                 goto err_battery_failed;
1928         }
1929 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)   
1930         data ->usb = rk30_usb_supply;
1931         ret = power_supply_register(&pdev->dev, &data ->usb);
1932         if (ret){
1933                 ret = -EINVAL;
1934                 printk(KERN_INFO "fail to usb power_supply_register\n");
1935                 goto err_usb_failed;
1936         }
1937 #endif
1938         data ->ac = rk30_ac_supply;
1939         ret = power_supply_register(&pdev->dev, &data ->ac);
1940         if (ret) {
1941                 ret = -EINVAL;
1942                 printk(KERN_INFO "fail to ac power_supply_register\n");
1943                 goto err_ac_failed;
1944         }
1945
1946 #if  defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1947         //init dc dectet irq & delay work
1948         if (pdata->dc_det_pin != INVALID_GPIO){
1949                 INIT_WORK(&data->dcwakeup_work, rk30_adc_battery_dcdet_delaywork);
1950                 
1951                 irq = gpio_to_irq(pdata->dc_det_pin);           
1952                 irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1953                 ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);
1954                 if (ret) {
1955                         ret = -EINVAL;
1956                         printk("failed to request dc det irq\n");
1957                         goto err_dcirq_failed;
1958                 }
1959                 enable_irq_wake(irq);  
1960         
1961         }
1962 #endif
1963
1964 #ifdef BATTERY_APK
1965         ret = device_create_file(&pdev->dev,&dev_attr_batparam);
1966         if(ret){
1967                 ret = -EINVAL;
1968                 printk(KERN_ERR "failed to create bat param file\n");
1969                 goto err_battery_failed;
1970         }
1971                 
1972         ret = create_sysfs_interfaces(&pdev->dev);
1973         if (ret < 0)
1974         {
1975                 ret = -EINVAL;
1976                 dev_err(&pdev->dev,               
1977                         "device rk30_adc_batterry sysfs register failed\n");
1978                 goto err_sysfs;
1979         }
1980 #endif 
1981         //Power on Battery detect
1982         rk30_adc_battery_check(data);
1983
1984         data->wq = create_singlethread_workqueue("adc_battd");
1985         INIT_DELAYED_WORK(&data->delay_work, rk30_adc_battery_timer_work);
1986
1987         if(1 == pdata->save_capacity ){
1988                 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS*10));
1989                 data ->poweron_check = 1;
1990         }else{
1991                 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1992                 data ->poweron_check = 0;
1993         }
1994         INIT_DELAYED_WORK(&data->check_work, rk_adc_battery_check_work);
1995         queue_delayed_work(data ->wq, &data ->check_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1996         if( pdata->batt_low_pin != INVALID_GPIO){
1997                 
1998                 if (gpio_get_value(pdata->batt_low_pin) ==0){
1999                        mdelay(20);         
2000                        if (gpio_get_value(pdata->batt_low_pin) ==0){
2001                          printk("lower power\n");
2002                                kernel_power_off(); 
2003                        }
2004                 }
2005                 
2006                 INIT_WORK(&data->lowerpower_work, rk30_adc_battery_lowerpower_delaywork);               
2007                 irq = gpio_to_irq(pdata->batt_low_pin);         
2008                 ret = request_irq(irq, rk30_adc_battery_low_wakeup, IRQF_TRIGGER_LOW, "batt_low_irq", NULL);
2009                 if (ret) {
2010                         ret = -EINVAL;
2011                         printk("failed to request batt_low_irq irq\n");
2012                         goto err_lowpowerirq_failed;
2013                 }
2014                 disable_irq(irq);
2015
2016         }
2017
2018         printk(KERN_INFO "rk30_adc_battery: driver initialized\n");
2019         
2020         return 0;
2021 err_sysfs:      
2022 err_usb_failed:
2023         power_supply_unregister(&data ->usb);
2024 err_ac_failed:
2025         power_supply_unregister(&data ->ac);
2026
2027 err_battery_failed:
2028         power_supply_unregister(&data ->bat);
2029     
2030 err_dcirq_failed:
2031         free_irq(gpio_to_irq(pdata->dc_det_pin), data);
2032 #if 1
2033  err_lowpowerirq_failed:
2034         free_irq(gpio_to_irq(pdata->batt_low_pin), data);
2035 #endif
2036 err_adc_register_failed:
2037 err_io_init:    
2038 err_data_alloc_failed:
2039         kfree(data);
2040
2041         printk("rk30_adc_battery: error!\n");
2042     
2043         return ret;
2044 }
2045
2046 static int rk30_adc_battery_remove(struct platform_device *pdev)
2047 {
2048         struct rk30_adc_battery_data *data = platform_get_drvdata(pdev);
2049         struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
2050
2051         cancel_delayed_work(&data->delay_work); 
2052 #if  defined (CONFIG_BATTERY_RK30_AC_CHARGE)
2053     if (pdata->dc_det_pin != INVALID_GPIO)
2054         cancel_delayed_work_sync(&data->dcwakeup_work);
2055 #endif
2056         cancel_delayed_work_sync(&data->check_work);
2057
2058     if( pdata->batt_low_pin != INVALID_GPIO)
2059         cancel_delayed_work_sync(&data->lowerpower_work);
2060 #if  defined (CONFIG_BATTERY_RK30_USB_CHARGE)
2061                 power_supply_unregister(&data ->usb);
2062 #endif
2063         power_supply_unregister(&data ->ac);
2064         power_supply_unregister(&data ->bat);
2065
2066         free_irq(gpio_to_irq(pdata->dc_det_pin), data);
2067
2068         kfree(data);
2069         
2070         return 0;
2071 }
2072
2073 static struct platform_driver rk30_adc_battery_driver = {
2074         .probe          = rk30_adc_battery_probe,
2075         .remove         = rk30_adc_battery_remove,
2076         .suspend                = rk30_adc_battery_suspend,
2077         .resume         = rk30_adc_battery_resume,
2078         .driver = {
2079                 .name = "rk30-battery",
2080                 .owner  = THIS_MODULE,
2081         }
2082 };
2083
2084 static int __init rk30_adc_battery_init(void)
2085 {
2086         return platform_driver_register(&rk30_adc_battery_driver);
2087 }
2088
2089 static void __exit rk30_adc_battery_exit(void)
2090 {
2091         platform_driver_unregister(&rk30_adc_battery_driver);
2092 }
2093 //module_init(rk30_adc_battery_init);//module_init(rk30_adc_battery_init);//
2094 subsys_initcall(rk30_adc_battery_init);
2095 //fs_initcall(rk30_adc_battery_init);
2096 module_exit(rk30_adc_battery_exit);
2097
2098 MODULE_DESCRIPTION("Battery detect driver for the rk30");
2099 MODULE_AUTHOR("luowei lw@rock-chips.com");
2100 MODULE_LICENSE("GPL");