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