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