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