UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / rk30_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
39 #if 0
40 #define DBG(x...)   printk(x)
41 #else
42 #define DBG(x...)
43 #endif
44
45 static int rk30_battery_dbg_level = 0;
46 module_param_named(dbg_level, rk30_battery_dbg_level, int, 0644);
47 #define pr_bat( args...) \
48         do { \
49                 if (rk30_battery_dbg_level) { \
50                         pr_info(args); \
51                 } \
52         } while (0)
53
54
55 /*******************ÒÔϲÎÊý¿ÉÒÔÐÞ¸Ä******************************/
56 #define TIMER_MS_COUNTS          1000   //¶¨Ê±Æ÷µÄ³¤¶Èms
57 //ÒÔϲÎÊýÐèÒª¸ù¾Ýʵ¼Ê²âÊÔµ÷Õû
58 #define SLOPE_SECOND_COUNTS                    15       //ͳ¼ÆµçѹбÂʵÄʱ¼ä¼ä¸ôs
59 #define DISCHARGE_MIN_SECOND                   45       //×î¿ì·Åµçµç1%ʱ¼ä
60 #define CHARGE_MIN_SECOND                      45       //×î¿ì³äµçµç1%ʱ¼ä
61 #define CHARGE_MID_SECOND                      90       //ÆÕͨ³äµçµç1%ʱ¼ä
62 #define CHARGE_MAX_SECOND                      250      //×³äµçµç1%ʱ¼ä
63 #define   CHARGE_FULL_DELAY_TIMES          10          //³äµçÂú¼ì²â·À¶¶Ê±¼ä
64 #define   USBCHARGE_IDENTIFY_TIMES        5           //²åÈëUSB»ìÁ÷£¬pcʶ±ð¼ì²âʱ¼ä
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
79 #define  BAT_DEFINE_VALUE                                            1800
80 #elif defined(CONFIG_ARCH_RK2928)
81 #define  BAT_DEFINE_VALUE                                                3300
82 #else
83 #define  BAT_DEFINE_VALUE                                            2500
84
85
86 #endif
87
88 #define BATT_FILENAME "/data/bat_last_capacity.dat"
89
90 static struct wake_lock batt_wake_lock;
91
92
93 struct batt_vol_cal{
94         u32 disp_cal;
95         u32 dis_charge_vol;
96         u32 charge_vol;
97 };
98
99 #ifdef CONFIG_BATTERY_RK30_VOL3V8
100
101 #define BATT_MAX_VOL_VALUE                             4120                     //ÂúµçʱµÄµç³Øµçѹ       
102 #define BATT_ZERO_VOL_VALUE                            3400                     //¹Ø»úʱµÄµç³Øµçѹ
103 #define BATT_NOMAL_VOL_VALUE                         3800               
104 //divider resistance 
105 #define BAT_PULL_UP_R                                         200
106 #if defined(CONFIG_ARCH_RK3066B)
107 #define BAT_PULL_DOWN_R                                    100
108 #else
109 #define BAT_PULL_DOWN_R                                    200
110 #endif
111 static struct batt_vol_cal  batt_table[] = {
112         {0,3400,3520},{1,3420,3525},{2,3420,3575},{3,3475,3600},{5,3505,3620},{7,3525,3644},
113         {9,3540,3662},{11,3557,3670},{13,3570,3684},{15,3580,3700},{17,3610,3715},
114         {19,3630,3720},{21,3640,3748},{23,3652,3756},{25,3662,3775},{27,3672,3790},
115         {29,3680,3810},{31,3687,3814},{33,3693,3818},{35,3699,3822},{37,3705,3825},
116         {39,3710,3830},{41,3714,3832},{43,3718,3834},{45,3722,3836},{47,3726,3837},
117         {49,3730,3839},{51,3734,3841},{53,3738,3842},{55,3742,3844},{57,3746,3844},
118         {59,3750,3855},{61,3756,3860},{63,3764,3864},{65,3774,3871},{67,3786,3890},
119         {69,3800,3910},{71,3808,3930},{73,3817,3977},{75,3827,3977},{77,3845,3997},
120         {79,3950,4030},{81,3964,4047},{83,3982,4064},{85,4002,4080},{87,4026,4096},
121         {89,4030,4132},{91,4034,4144},{93,4055,4150},{95,4085,4195},{97,4085,4195},{100,4120,4200},
122 };
123 #else
124 #define BATT_MAX_VOL_VALUE                              8284                    //Full charge voltage
125 #define BATT_ZERO_VOL_VALUE                             6800                    // power down voltage 
126 #define BATT_NOMAL_VOL_VALUE                          7600                
127
128 //¶¨ÒåADC²ÉÑù·Öѹµç×裬ÒÔʵ¼ÊֵΪ׼£¬µ¥Î»K
129
130 #define BAT_PULL_UP_R                                         300 
131 #define BAT_PULL_DOWN_R                                    100
132
133 static struct batt_vol_cal  batt_table[] = {
134         {0,6800,7400},    {1,6840,7440},    {2,6880,7480},     {3,6950,7450},       {5,7010,7510},    {7,7050,7550},
135         {9,7080,7580},    {11,7104,7604},   {13,7140,7640},   {15,7160,7660},      {17,7220,7720},
136         {19,7260,7760},  {21,7280,7780},   {23,7304,7802},   {25,7324,7824},      {27,7344,7844},
137         {29,7360,7860},  {31,7374,7874},   {33,7386,7886},   {35,7398,7898},      {37,7410,7910},//500
138         {39,7420,7920},  {41,7424,7928},   {43,7436,7947},   {45,7444,7944},      {47,7450,7958}, //508
139         {49,7460,7965},  {51,7468,7975},   {53, 7476,7990},  {55,7482,8000},      {57,7492,8005}, // 5 14
140         {59,7500,8011},  {61,7510,8033},   {63,7528,8044},   {65,7548,8055},      {67,7560,8066},//506
141         {69,7600,8070},  {71,7618,8075},   {73,7634,8080},   {75,7654,8085},      {77,7690,8100}, //400
142         {79,7900,8180},  {81,7920,8210},   {83,7964,8211},   {85,8000,8214},      {87,8002,8218},//290
143         {89,8012, 8220}, {91,8022,8235},   {93,8110,8260},   {95,8140,8290},       {97,8170,8300},  {100,8200 ,8310},//110
144
145 };
146 #endif
147
148
149 #define BATT_NUM  ARRAY_SIZE(batt_table)
150
151 #define adc_to_voltage(adc_val)                           ((adc_val * BAT_DEFINE_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R))
152
153 /********************************************************************************/
154
155 extern int dwc_vbus_status(void);
156 extern int get_msc_connect_flag(void);
157
158 struct rk30_adc_battery_data {
159         int irq;
160         
161         //struct timer_list       timer;
162         struct workqueue_struct *wq;
163         struct delayed_work         delay_work;
164         struct work_struct          dcwakeup_work;
165         struct work_struct                   lowerpower_work;
166         bool                    resume;
167         
168         struct rk30_adc_battery_platform_data *pdata;
169
170         int                     full_times;
171         
172         struct adc_client       *client; 
173         int                     adc_val;
174         int                     adc_samples[NUM_VOLTAGE_SAMPLE+2];
175         
176         int                     bat_status;
177         int                     bat_status_cnt;
178         int                     bat_health;
179         int                     bat_present;
180         int                     bat_voltage;
181         int                     bat_capacity;
182         int                     bat_change;
183         
184         int                     old_charge_level;
185         int                    *pSamples;
186         int                     gBatCapacityDisChargeCnt;
187         int                     gBatCapacityChargeCnt;
188         int               capacitytmp;
189         int                     poweron_check;
190         int                     suspend_capacity;
191
192         int                     status_lock;
193
194 };
195 static struct rk30_adc_battery_data *gBatteryData;
196
197 enum {
198         BATTERY_STATUS          = 0,
199         BATTERY_HEALTH          = 1,
200         BATTERY_PRESENT         = 2,
201         BATTERY_CAPACITY        = 3,
202         BATTERY_AC_ONLINE       = 4,
203         BATTERY_STATUS_CHANGED  = 5,
204         AC_STATUS_CHANGED       = 6,
205         BATTERY_INT_STATUS          = 7,
206         BATTERY_INT_ENABLE          = 8,
207 };
208
209 typedef enum {
210         CHARGER_BATTERY = 0,
211         CHARGER_USB,
212         CHARGER_AC
213 } charger_type_t;
214
215
216
217
218
219 static int rk30_adc_battery_load_capacity(void)
220 {
221         char value[4];
222         int* p = (int *)value;
223         long fd = sys_open(BATT_FILENAME,O_RDONLY,0);
224
225         if(fd < 0){
226                 pr_bat("rk30_adc_battery_load_capacity: open file /data/bat_last_capacity.dat failed\n");
227                 return -1;
228         }
229
230         sys_read(fd,(char __user *)value,4);
231         sys_close(fd);
232
233         return (*p);
234 }
235
236 static void rk30_adc_battery_put_capacity(int loadcapacity)
237 {
238         char value[4];
239         int* p = (int *)value;
240         long fd = sys_open(BATT_FILENAME,O_CREAT | O_RDWR,0);
241
242         if(fd < 0){
243                 pr_bat("rk30_adc_battery_put_capacity: open file /data/bat_last_capacity.dat failed\n");
244                 return;
245         }
246         
247         *p = loadcapacity;
248         sys_write(fd, (const char __user *)value, 4);
249
250         sys_close(fd);
251 }
252
253 static void rk30_adc_battery_charge_enable(struct rk30_adc_battery_data *bat)
254 {
255         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
256
257         if (pdata->charge_set_pin != INVALID_GPIO){
258                 gpio_direction_output(pdata->charge_set_pin, pdata->charge_set_level);
259         }
260 }
261
262 static void rk30_adc_battery_charge_disable(struct rk30_adc_battery_data *bat)
263 {
264         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
265
266         if (pdata->charge_set_pin != INVALID_GPIO){
267                 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
268         }
269 }
270
271 //extern int suspend_flag;
272 static int rk30_adc_battery_get_charge_level(struct rk30_adc_battery_data *bat)
273 {
274         int charge_on = 0;
275         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
276
277 #if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
278         if (pdata->dc_det_pin != INVALID_GPIO){
279                 if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level){
280                         charge_on = 1;
281                 }
282         }
283         else{
284                 if(pdata->is_dc_charging)
285                         charge_on =pdata->is_dc_charging();
286         }
287         
288                 
289 #endif
290 #if 1
291         if (pdata->spport_usb_charging)  //is usb charging 
292                 if(pdata->is_usb_charging)
293                         charge_on = pdata->is_usb_charging();
294
295 #endif
296         return charge_on;
297 }
298 static int  is_charge_ok(struct rk30_adc_battery_data *bat)
299 {
300         int charge_is_ok = 0;
301         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
302
303         if((pdata->charge_ok_pin == INVALID_GPIO)&& ( pdata->charging_ok == NULL))
304                 return -1;
305         
306         if (pdata->charge_ok_pin != INVALID_GPIO){              
307                 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level)
308                 {
309                         charge_is_ok =1;
310                 }
311         }else if( pdata->charging_ok)
312                 {       
313                 charge_is_ok = pdata->charging_ok();
314                 }
315         return charge_is_ok;
316
317
318 }
319
320 //int old_charge_level;
321 static int rk30_adc_battery_status_samples(struct rk30_adc_battery_data *bat)
322 {
323         int charge_level;
324 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
325         charge_level = rk30_adc_battery_get_charge_level(bat);
326
327         //¼ì²â³äµç״̬±ä»¯Çé¿ö
328         if (charge_level != bat->old_charge_level){
329                 bat->old_charge_level = charge_level;
330                 bat->bat_change  = 1;
331                 
332                 if(charge_level) {            
333                         rk30_adc_battery_charge_enable(bat);
334                 }
335                 else{
336                         rk30_adc_battery_charge_disable(bat);
337                 }
338                 bat->bat_status_cnt = 0;        //״̬±ä»¯¿ªÊ¼¼ÆÊý
339         }
340
341         if(charge_level == 0){   
342         //discharge
343                 bat->full_times = 0;
344                 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
345         }
346         else{
347         //CHARGE        
348                 if( is_charge_ok(bat)  ==  INVALID_CHARGE_CHECK){
349                 //if (pdata->charge_ok_pin == INVALID_GPIO){  //no charge_ok_pin
350
351                         if (bat->bat_capacity == 100){
352                                 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
353                                         bat->bat_status = POWER_SUPPLY_STATUS_FULL;
354                                         bat->bat_change  = 1;
355                                 }
356                         }
357                         else{
358                                 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
359                         }
360                 }
361                 else{  // pin of charge_ok_pin
362                         if (is_charge_ok(bat) != CHARGE_IS_OK ){
363
364                                 bat->full_times = 0;
365                                 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
366                         }
367                         else{
368         //¼ì²âµ½³äµçÂúµçƽ±êÖ¾
369                                 bat->full_times++;
370
371                                 if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) {
372                                         bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
373                                 }
374
375                                 if ((bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) && (bat->bat_capacity >= 95)){
376                                         if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
377                                                 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
378                                                 bat->bat_capacity = 100;
379                                                 bat->bat_change  = 1;
380                                         }
381                                 }
382                                 else{
383                                         bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
384                                 }
385                         }
386                 }
387         }
388
389         return charge_level;
390 }
391 //#define adc_to_voltage(adc_val)                           ((adc_val * BAT_DEFINE_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R))
392
393 static int rk_adc_voltage(int value)
394 {
395         int voltage;
396
397         int ref_voltage; //reference_voltage
398         int pullup_res;
399         int pulldown_res;
400
401         ref_voltage = gBatteryData ->pdata->reference_voltage;
402         pullup_res = gBatteryData ->pdata->pull_up_res;
403         pulldown_res = gBatteryData ->pdata->pull_down_res;
404
405         if(ref_voltage && pullup_res && pulldown_res){
406                 
407                 voltage =  ((value * ref_voltage * (pullup_res + pulldown_res)) / (1024 * pulldown_res));
408                 
409         }else{
410                 voltage = adc_to_voltage(value);        
411         }
412                 
413                 
414         return voltage;
415
416 }
417
418 static int *pSamples;
419 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat)
420 {
421         int value;
422         int i,*pStart = bat->adc_samples, num = 0;
423         int level = rk30_adc_battery_get_charge_level(bat);
424
425
426         value = bat->adc_val;
427         adc_async_read(bat->client);
428
429         *pSamples++ = rk_adc_voltage(value);
430
431         bat->bat_status_cnt++;
432         if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE)  bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1;
433
434         num = pSamples - pStart;
435         
436         if (num >= NUM_VOLTAGE_SAMPLE){
437                 pSamples = pStart;
438                 num = NUM_VOLTAGE_SAMPLE;
439                 
440         }
441
442         value = 0;
443         for (i = 0; i < num; i++){
444                 value += bat->adc_samples[i];
445         }
446         bat->bat_voltage = value / num;
447
448         /*Ïû³ýë´Ìµçѹ*/
449         if(1 == level){
450                 if(bat->bat_voltage >= batt_table[BATT_NUM-1].charge_vol+ 10)
451                         bat->bat_voltage = batt_table[BATT_NUM-1].charge_vol  + 10;
452                 else if(bat->bat_voltage <= batt_table[0].charge_vol  - 10)
453                         bat->bat_voltage =  batt_table[0].charge_vol - 10;
454         }
455         else{
456                 if(bat->bat_voltage >= batt_table[BATT_NUM-1].dis_charge_vol+ 10)
457                         bat->bat_voltage = batt_table[BATT_NUM-1].dis_charge_vol  + 10;
458                 else if(bat->bat_voltage <= batt_table[0].dis_charge_vol  - 10)
459                         bat->bat_voltage =  batt_table[0].dis_charge_vol - 10;
460
461         }
462
463 }
464 static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage)
465 {
466         int i = 0;
467         int capacity = 0;
468
469         struct batt_vol_cal *p;
470         p = batt_table;
471
472         if (rk30_adc_battery_get_charge_level(bat)){  //charge
473                 if(BatVoltage >= (p[BATT_NUM - 1].charge_vol)){
474                         capacity = 99;
475                 }       
476                 else{
477                         if(BatVoltage <= (p[0].charge_vol)){
478                                 capacity = 0;
479                         }
480                         else{
481                                 for(i = 0; i < BATT_NUM - 1; i++){
482
483                                         if(((p[i].charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].charge_vol))){
484                                                 capacity = p[i].disp_cal + ((BatVoltage - p[i].charge_vol) *  (p[i+1].disp_cal -p[i].disp_cal ))/ (p[i+1].charge_vol- p[i].charge_vol);
485                                                 break;
486                                         }
487                                 }
488                         }  
489                 }
490
491         }
492         else{  //discharge
493                 if(BatVoltage >= (p[BATT_NUM - 1].dis_charge_vol)){
494                         capacity = 100;
495                 }       
496                 else{
497                         if(BatVoltage <= (p[0].dis_charge_vol)){
498                                 capacity = 0;
499                         }
500                         else{
501                                 for(i = 0; i < BATT_NUM - 1; i++){
502                                         if(((p[i].dis_charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].dis_charge_vol))){
503                                                 capacity =  p[i].disp_cal+ ((BatVoltage - p[i].dis_charge_vol) * (p[i+1].disp_cal -p[i].disp_cal ) )/ (p[i+1].dis_charge_vol- p[i].dis_charge_vol) ;
504                                                 break;
505                                         }
506                                 }
507                         }  
508
509                 }
510
511
512         }
513     return capacity;
514     }
515
516
517 static void rk30_adc_battery_capacity_samples(struct rk30_adc_battery_data *bat)
518 {
519         int capacity = 0;
520 //      struct rk30_adc_battery_platform_data *pdata = bat->pdata;
521         int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
522         int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
523
524         //³ä·Åµç״̬±ä»¯ºó£¬BufferÌîÂú֮ǰ£¬²»¸üÐÂ
525         if (bat->bat_status_cnt < NUM_VOLTAGE_SAMPLE)  {
526                 bat->gBatCapacityDisChargeCnt = 0;
527                 bat->gBatCapacityChargeCnt    = 0;
528                 return;
529         }
530         
531         capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
532             
533         if (rk30_adc_battery_get_charge_level(bat)){
534                 if (capacity > bat->bat_capacity){
535                         if(capacity > bat->bat_capacity + 10 )
536                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10;  //5s
537                         else if(capacity > bat->bat_capacity + 7 )
538                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
539                                 else if(capacity > bat->bat_capacity + 3 )
540                                         timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
541                         //ʵ¼Ê²ÉÑùµ½µÄÈÝÁ¿±ÈÏÔʾµÄÈÝÁ¿´ó£¬Öð¼¶ÉÏÉý
542                         if (++(bat->gBatCapacityDisChargeCnt) >= timer_of_charge_sample){
543                                 bat->gBatCapacityDisChargeCnt  = 0;
544                                 if (bat->bat_capacity < 99){
545                                         bat->bat_capacity++;
546                                         bat->bat_change  = 1;
547                                 }
548                         }
549                         bat->gBatCapacityChargeCnt = 0;
550                 }
551                 else{  //   Êµ¼ÊµÄÈÝÁ¿±È²ÉÑù±È ÏÔʾµÄÈÝÁ¿Ð¡
552                             bat->gBatCapacityDisChargeCnt = 0;
553                             (bat->gBatCapacityChargeCnt)++;
554                         pr_bat("------------- is_charge_ok(bat)=%d\n", is_charge_ok(bat));
555                                 if( is_charge_ok(bat) != INVALID_CHARGE_CHECK){
556                         //if (pdata->charge_ok_pin != INVALID_GPIO){
557                                 //if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
558                                 if( is_charge_ok(bat) == CHARGE_IS_OK){
559                                         if(capacity > bat->bat_capacity + 10 )
560                                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -13;  //  2s
561                                         else if(capacity > bat->bat_capacity + 7 )
562                                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
563                                         else if(capacity > bat->bat_capacity + 2 )
564                                                 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -8; //7s                                 
565                                 //¼ì²âµ½µç³Ø³äÂú±êÖ¾£¬Í¬Ê±³¤Ê±¼äÄÚ³äµçµçѹÎޱ仯£¬¿ªÊ¼Æô¶¯¼Æʱ³äµç£¬¿ìËÙÉÏÉýÈÝÁ¿
566                                         if (bat->gBatCapacityChargeCnt >= timer_of_charge_sample){
567                                                 bat->gBatCapacityChargeCnt = 0;
568                                                 if (bat->bat_capacity < 99){
569                                                         bat->bat_capacity++;
570                                                         bat->bat_change  = 1;
571                                                 }
572                                         }
573                                 }
574                                 else{
575 #if 0                                   
576                                         if (capacity > capacitytmp){
577                                         //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
578                                                 gBatCapacityChargeCnt = 0;
579                                         }
580                                         else if (/*bat->bat_capacity >= 85) &&*/ (gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
581                                                 gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
582
583                                                 if (bat->bat_capacity < 99){
584                                                 bat->bat_capacity++;
585                                                 bat->bat_change  = 1;
586                                                 }
587                                         }
588                                 }
589 #else                   //  ·ÀÖ¹µç³ØÀÏ»¯ºó³öÏֳ岻ÂúµÄÇé¿ö£¬
590                                         if (capacity > bat->capacitytmp){
591                                         //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
592                                                 bat->gBatCapacityChargeCnt = 0;
593                                         }
594                                         else{
595
596                                                 if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
597                                                         bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
598
599                                                         if (bat->bat_capacity < 99){
600                                                                 bat->bat_capacity++;
601                                                                 bat->bat_change  = 1;
602                                                         }
603                                                 }
604                                         }
605                                 }
606 #endif
607
608                         }
609                         else{
610                         //ûÓгäµçÂú¼ì²â½Å£¬³¤Ê±¼äÄÚµçѹÎޱ仯£¬¶¨Ê±Æ÷Ä£Äâ³äµç
611                                 if (capacity > bat->capacitytmp){
612                                 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
613                                         bat->gBatCapacityChargeCnt = 0;
614                                 }
615                                 else{
616
617                                         if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
618                                                 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
619
620                                                 if (bat->bat_capacity <= 99){
621                                                         bat->bat_capacity++;
622                                                         bat->bat_change  = 1;
623                                                 }
624                                         }
625                                 }
626                                 
627
628                         }            
629                 }
630         }    
631         else{   
632         //·Åµçʱ,Ö»ÔÊÐíµçѹϽµ
633                 if (capacity < bat->bat_capacity){
634                         if(capacity + 10 > bat->bat_capacity  )
635                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10;  //5s
636                         else if(capacity  + 7 > bat->bat_capacity )
637                                 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
638                                 else if(capacity  + 3> bat->bat_capacity )
639                                         timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
640
641                         if (++(bat->gBatCapacityDisChargeCnt) >= timer_of_discharge_sample){
642                                 bat->gBatCapacityDisChargeCnt = 0;
643                                 if (bat->bat_capacity > 0){
644                                         bat->bat_capacity-- ;
645                                         bat->bat_change  = 1;
646                                 }
647                         }
648                 }
649                 else{
650                         bat->gBatCapacityDisChargeCnt = 0;
651                 }
652                 bat->gBatCapacityChargeCnt = 0;
653         }
654                 bat->capacitytmp = capacity;
655 }
656
657 //static int poweron_check = 0;
658 static void rk30_adc_battery_poweron_capacity_check(void)
659 {
660
661         int new_capacity, old_capacity;
662
663         new_capacity = gBatteryData->bat_capacity;
664         old_capacity = rk30_adc_battery_load_capacity();
665
666         pr_bat("------------old_capacity---%d\n",old_capacity);
667         if ((old_capacity <= 0) || (old_capacity >= 100)){
668                 old_capacity = new_capacity;
669         }    
670
671         if (gBatteryData->bat_status == POWER_SUPPLY_STATUS_FULL){
672                 if (new_capacity > 80){
673                         gBatteryData->bat_capacity = 100;
674                 }
675         }
676         else if (gBatteryData->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
677         //chargeing state
678         //ÎÊÌ⣺
679 //      //1£©³¤Ê±¼ä¹Ø»ú·ÅÖú󣬿ª»úºó¶ÁÈ¡µÄÈÝÁ¿Ô¶Ô¶´óÓÚʵ¼ÊÈÝÁ¿Ôõô°ì£¿
680 //      //2£©Èç¹û²»ÕâÑù×ö£¬¶Ìʱ¼ä¹Ø»úÔÙ¿ª»ú£¬Ç°ºóÈÝÁ¿²»Ò»ÖÂÓÖ¸ÃÔõô°ì£¿
681 //      //3£©Ò»ÏÂÄÇÖÖ·½Ê½ºÏÊÊ£¿
682         //gBatteryData->bat_capacity = new_capacity;
683                 gBatteryData->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
684         }else{
685
686                 if(new_capacity > old_capacity + 50 )
687                         gBatteryData->bat_capacity = new_capacity;
688                 else
689                         gBatteryData->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity;  //avoid the value of capacity increase 
690         }
691
692
693         pr_bat("capacity = %d, new_capacity = %d, old_capacity = %d\n",gBatteryData->bat_capacity, new_capacity, old_capacity);
694
695         gBatteryData->bat_change = 1;
696 }
697
698 #if defined(CONFIG_BATTERY_RK30_USB_CHARGE)
699 static int rk30_adc_battery_get_usb_property(struct power_supply *psy, 
700                                     enum power_supply_property psp,
701                                     union power_supply_propval *val)
702 {
703         charger_type_t charger;
704         charger =  CHARGER_USB;
705
706         switch (psp) {
707         case POWER_SUPPLY_PROP_ONLINE:
708                 if (psy->type == POWER_SUPPLY_TYPE_USB)
709                 //      val->intval = get_msc_connect_flag();
710                 if (gBatteryData->pdata->spport_usb_charging)  //is usb charging 
711                         if(gBatteryData->pdata->is_usb_charging)
712                                 val->intval = gBatteryData ->pdata->is_usb_charging();
713
714                 printk("%s:%d\n",__FUNCTION__,val->intval);
715                 break;
716
717         default:
718                 return -EINVAL;
719         }
720         
721         return 0;
722
723 }
724
725 static enum power_supply_property rk30_adc_battery_usb_props[] = {
726     
727         POWER_SUPPLY_PROP_ONLINE,
728 };
729
730 static struct power_supply rk30_usb_supply = 
731 {
732         .name = "usb",
733         .type = POWER_SUPPLY_TYPE_USB,
734
735         .get_property   = rk30_adc_battery_get_usb_property,
736
737         .properties     = rk30_adc_battery_usb_props,
738         .num_properties = ARRAY_SIZE(rk30_adc_battery_usb_props),
739 };
740 #endif
741
742 #if defined(CONFIG_BATTERY_RK30_AC_CHARGE)
743 static irqreturn_t rk30_adc_battery_dc_wakeup(int irq, void *dev_id)
744 {   
745         queue_work(gBatteryData->wq, &gBatteryData->dcwakeup_work);
746         return IRQ_HANDLED;
747 }
748
749
750 static int rk30_adc_battery_get_ac_property(struct power_supply *psy,
751                         enum power_supply_property psp,
752                         union power_supply_propval *val)
753 {
754         int ret = 0;
755         charger_type_t charger;
756         charger =  CHARGER_USB;
757         switch (psp) {
758         case POWER_SUPPLY_PROP_ONLINE:
759                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
760                 {
761                         if (rk30_adc_battery_get_charge_level(gBatteryData))
762                         {
763                                 val->intval = 1;
764                                 }
765                         else
766                                 {
767                                 val->intval = 0;        
768                                 }
769                 }
770                 DBG("%s:%d\n",__FUNCTION__,val->intval);
771                 break;
772                 
773         default:
774                 ret = -EINVAL;
775                 break;
776         }
777
778         return ret;
779 }
780
781 static enum power_supply_property rk30_adc_battery_ac_props[] = 
782 {
783         POWER_SUPPLY_PROP_ONLINE,
784 };
785
786 static struct power_supply rk30_ac_supply = 
787 {
788         .name = "ac",
789         .type = POWER_SUPPLY_TYPE_MAINS,
790
791         .get_property   = rk30_adc_battery_get_ac_property,
792
793         .properties     = rk30_adc_battery_ac_props,
794         .num_properties = ARRAY_SIZE(rk30_adc_battery_ac_props),
795 };
796
797 static void rk30_adc_battery_dcdet_delaywork(struct work_struct *work)
798 {
799         int ret;
800         struct rk30_adc_battery_platform_data *pdata;
801         int irq;
802         int irq_flag;
803         
804         pdata    = gBatteryData->pdata;
805         irq        = gpio_to_irq(pdata->dc_det_pin);
806         irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
807
808         rk28_send_wakeup_key(); // wake up the system
809
810         free_irq(irq, NULL);
811         ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);// reinitialize the DC irq 
812         if (ret) {
813                 free_irq(irq, NULL);
814         }
815
816         power_supply_changed(&rk30_ac_supply);
817
818         gBatteryData->bat_status_cnt = 0;        //the state of battery is change
819
820 }
821
822
823 #endif
824
825 static int rk30_adc_battery_get_status(struct rk30_adc_battery_data *bat)
826 {
827         return (bat->bat_status);
828 }
829
830 static int rk30_adc_battery_get_health(struct rk30_adc_battery_data *bat)
831 {
832         return POWER_SUPPLY_HEALTH_GOOD;
833 }
834
835 static int rk30_adc_battery_get_present(struct rk30_adc_battery_data *bat)
836 {
837         return (bat->bat_voltage < BATT_MAX_VOL_VALUE) ? 0 : 1;
838 }
839
840 static int rk30_adc_battery_get_voltage(struct rk30_adc_battery_data *bat)
841 {
842         return (bat->bat_voltage );
843 }
844
845 static int rk30_adc_battery_get_capacity(struct rk30_adc_battery_data *bat)
846 {
847         return (bat->bat_capacity);
848 }
849
850 static int rk30_adc_battery_get_property(struct power_supply *psy,
851                                  enum power_supply_property psp,
852                                  union power_supply_propval *val)
853 {               
854         int ret = 0;
855
856         switch (psp) {
857                 case POWER_SUPPLY_PROP_STATUS:
858                         val->intval = rk30_adc_battery_get_status(gBatteryData);
859                         DBG("gBatStatus=%d\n",val->intval);
860                         break;
861                 case POWER_SUPPLY_PROP_HEALTH:
862                         val->intval = rk30_adc_battery_get_health(gBatteryData);
863                         DBG("gBatHealth=%d\n",val->intval);
864                         break;
865                 case POWER_SUPPLY_PROP_PRESENT:
866                         val->intval = rk30_adc_battery_get_present(gBatteryData);
867                         DBG("gBatPresent=%d\n",val->intval);
868                         break;
869                 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
870                         val ->intval = rk30_adc_battery_get_voltage(gBatteryData);
871                         DBG("gBatVoltage=%d\n",val->intval);
872                         break;
873                 //      case POWER_SUPPLY_PROP_CURRENT_NOW:
874                 //              val->intval = 1100;
875                 //              break;
876                 case POWER_SUPPLY_PROP_CAPACITY:
877                         val->intval = rk30_adc_battery_get_capacity(gBatteryData);
878                         DBG("gBatCapacity=%d%%\n",val->intval);
879                         break;
880                 case POWER_SUPPLY_PROP_TECHNOLOGY:
881                         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
882                         break;
883                 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
884                         val->intval = BATT_MAX_VOL_VALUE;
885                         break;
886                 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
887                         val->intval = BATT_ZERO_VOL_VALUE;
888                         break;
889                 default:
890                         ret = -EINVAL;
891                         break;
892         }
893
894         return ret;
895 }
896
897 static enum power_supply_property rk30_adc_battery_props[] = {
898
899         POWER_SUPPLY_PROP_STATUS,
900         POWER_SUPPLY_PROP_HEALTH,
901         POWER_SUPPLY_PROP_PRESENT,
902         POWER_SUPPLY_PROP_VOLTAGE_NOW,
903 //      POWER_SUPPLY_PROP_CURRENT_NOW,
904         POWER_SUPPLY_PROP_TECHNOLOGY,
905         POWER_SUPPLY_PROP_CAPACITY,
906         POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
907         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
908 };
909
910 static struct power_supply rk30_battery_supply = 
911 {
912         .name = "battery",
913         .type = POWER_SUPPLY_TYPE_BATTERY,
914
915         .get_property   = rk30_adc_battery_get_property,
916
917         .properties     = rk30_adc_battery_props,
918         .num_properties = ARRAY_SIZE(rk30_adc_battery_props),
919 };
920
921 #ifdef CONFIG_PM
922 static void rk30_adc_battery_resume_check(void)
923 {
924         int i;
925         int level,oldlevel;
926         int new_capacity, old_capacity;
927         struct rk30_adc_battery_data *bat = gBatteryData;
928
929         bat->old_charge_level = -1;
930         pSamples = bat->adc_samples;
931
932         adc_sync_read(bat->client);                             //start adc sample
933         level = oldlevel = rk30_adc_battery_status_samples(bat);//init charge status
934
935         for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++) {               //0.3 s   
936         
937                 mdelay(1);
938                 rk30_adc_battery_voltage_samples(bat);              //get voltage
939                 level = rk30_adc_battery_status_samples(bat);       //check charge status
940                 if (oldlevel != level){         
941                     oldlevel = level;                               //if charge status changed, reset sample
942                     i = 0;
943                 }        
944         }
945         new_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
946         old_capacity =gBatteryData-> suspend_capacity;
947
948         if (bat->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
949         //chargeing state
950                 bat->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
951         }
952         else{
953                 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity;  // aviod the value of capacity increase    dicharge
954         }
955
956 }
957
958 static int rk30_adc_battery_suspend(struct platform_device *dev, pm_message_t state)
959 {
960         int irq;
961         gBatteryData->suspend_capacity = gBatteryData->bat_capacity;
962         cancel_delayed_work(&gBatteryData->delay_work);
963         
964         if( gBatteryData->pdata->batt_low_pin != INVALID_GPIO){
965                 
966                 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
967                 enable_irq(irq);
968                 enable_irq_wake(irq);
969         }
970
971         return 0;
972 }
973
974 static int rk30_adc_battery_resume(struct platform_device *dev)
975 {
976         int irq;
977         gBatteryData->resume = true;
978         queue_delayed_work(gBatteryData->wq, &gBatteryData->delay_work, msecs_to_jiffies(100));
979         if( gBatteryData->pdata->batt_low_pin != INVALID_GPIO){
980                 
981                 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
982                 disable_irq_wake(irq);
983                 disable_irq(irq);
984         }
985         return 0;
986 }
987 #else
988 #define rk30_adc_battery_suspend NULL
989 #define rk30_adc_battery_resume NULL
990 #endif
991
992
993 unsigned long AdcTestCnt = 0;
994 static void rk30_adc_battery_timer_work(struct work_struct *work)
995 {
996 #ifdef CONFIG_PM
997         if (gBatteryData->resume) {
998                 rk30_adc_battery_resume_check();
999                 gBatteryData->resume = false;
1000         }
1001 #endif
1002                 
1003         rk30_adc_battery_status_samples(gBatteryData);
1004
1005         if (gBatteryData->poweron_check){   
1006                 gBatteryData->poweron_check = 0;
1007                 rk30_adc_battery_poweron_capacity_check();
1008         }
1009
1010         rk30_adc_battery_voltage_samples(gBatteryData);
1011         rk30_adc_battery_capacity_samples(gBatteryData);
1012
1013         if( 0 == gBatteryData ->pdata ->charging_sleep){
1014                 if( 1 == rk30_adc_battery_get_charge_level(gBatteryData)){  // charge
1015                         if(0 == gBatteryData->status_lock ){                    
1016                                 wake_lock(&batt_wake_lock);  //lock
1017                                 gBatteryData->status_lock = 1; 
1018                         }
1019                 }
1020                 else{
1021                         if(1 == gBatteryData->status_lock ){                    
1022                                 wake_unlock(&batt_wake_lock);  //unlock
1023                                 gBatteryData->status_lock = 0; 
1024                         }
1025
1026                 }
1027         }
1028         
1029         
1030         /*update battery parameter after adc and capacity has been changed*/
1031         if(gBatteryData->bat_change){
1032                 gBatteryData->bat_change = 0;
1033                 rk30_adc_battery_put_capacity(gBatteryData->bat_capacity);
1034                 power_supply_changed(&rk30_battery_supply);
1035 #if  defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1036                 if (gBatteryData->pdata->dc_det_pin == INVALID_GPIO){
1037                         power_supply_changed(&rk30_ac_supply);
1038                 }
1039 #endif
1040         }
1041
1042         if (rk30_battery_dbg_level){
1043                 if (++AdcTestCnt >= 2)
1044                         {
1045                         AdcTestCnt = 0;
1046
1047                         pr_bat("Status = %d, RealAdcVal = %d, RealVol = %d,gBatVol = %d, gBatCap = %d, RealCapacity = %d, dischargecnt = %d, chargecnt = %d\n", 
1048                         gBatteryData->bat_status, gBatteryData->adc_val, rk_adc_voltage(gBatteryData->adc_val), 
1049                         gBatteryData->bat_voltage, gBatteryData->bat_capacity, gBatteryData->capacitytmp, gBatteryData->gBatCapacityDisChargeCnt,gBatteryData-> gBatCapacityChargeCnt);
1050
1051                 }
1052         }
1053         queue_delayed_work(gBatteryData->wq, &gBatteryData->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1054
1055 }
1056
1057
1058 static int rk30_adc_battery_io_init(struct rk30_adc_battery_platform_data *pdata)
1059 {
1060         int ret = 0;
1061         
1062         if (pdata->io_init) {
1063                 pdata->io_init();
1064                 return 0;
1065         }
1066         
1067         //charge control pin
1068         if (pdata->charge_set_pin != INVALID_GPIO){
1069                 ret = gpio_request(pdata->charge_set_pin, NULL);
1070                 if (ret) {
1071                         printk("failed to request dc_det gpio\n");
1072                         goto error;
1073                         }
1074                 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
1075         }
1076         
1077         //dc charge detect pin
1078         if (pdata->dc_det_pin != INVALID_GPIO){
1079                 ret = gpio_request(pdata->dc_det_pin, NULL);
1080                 if (ret) {
1081                         printk("failed to request dc_det gpio\n");
1082                         goto error;
1083                 }
1084         
1085                 gpio_pull_updown(pdata->dc_det_pin, GPIOPullUp);//important
1086                 ret = gpio_direction_input(pdata->dc_det_pin);
1087                 if (ret) {
1088                         printk("failed to set gpio dc_det input\n");
1089                         goto error;
1090                 }
1091         }
1092         
1093         //charge ok detect
1094         if (pdata->charge_ok_pin != INVALID_GPIO){
1095                 ret = gpio_request(pdata->charge_ok_pin, NULL);
1096                 if (ret) {
1097                         printk("failed to request charge_ok gpio\n");
1098                         goto error;
1099                 }
1100         
1101                 gpio_pull_updown(pdata->charge_ok_pin, GPIOPullUp);//important
1102                 ret = gpio_direction_input(pdata->charge_ok_pin);
1103                 if (ret) {
1104                         printk("failed to set gpio charge_ok input\n");
1105                         goto error;
1106                 }
1107         }
1108         //batt low pin
1109         if( pdata->batt_low_pin != INVALID_GPIO){
1110                 ret = gpio_request(pdata->batt_low_pin, NULL);
1111                 if (ret) {
1112                         printk("failed to request batt_low_pin gpio\n");
1113                         goto error;
1114                 }
1115         
1116                 gpio_pull_updown(pdata->batt_low_pin, GPIOPullUp); 
1117                 ret = gpio_direction_input(pdata->batt_low_pin);
1118                 if (ret) {
1119                         printk("failed to set gpio batt_low_pin input\n");
1120                         goto error;
1121                 }
1122         }
1123     
1124         return 0;
1125 error:
1126         return -1;
1127 }
1128
1129 extern void kernel_power_off(void);
1130 static void rk30_adc_battery_check(struct rk30_adc_battery_data *bat)
1131 {
1132         int i;
1133         int level,oldlevel;
1134         struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1135         //printk("%s--%d:\n",__FUNCTION__,__LINE__);
1136
1137         bat->old_charge_level = -1;
1138         bat->capacitytmp = 0;
1139         bat->suspend_capacity = 0;
1140         
1141         pSamples = bat->adc_samples;
1142
1143         adc_sync_read(bat->client);                             //start adc sample
1144         level = oldlevel = rk30_adc_battery_status_samples(bat);//init charge status
1145
1146         bat->full_times = 0;
1147         for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){                //0.3 s
1148                 mdelay(1);
1149                 rk30_adc_battery_voltage_samples(bat);              //get voltage
1150                 //level = rk30_adc_battery_status_samples(bat);       //check charge status
1151                 level = rk30_adc_battery_get_charge_level(bat);
1152
1153                 if (oldlevel != level){
1154                         oldlevel = level;                               //if charge status changed, reset sample
1155                         i = 0;
1156                 }        
1157         }
1158
1159         bat->bat_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);  //init bat_capacity
1160
1161         
1162         bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1163         if (rk30_adc_battery_get_charge_level(bat)){
1164                 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1165
1166                 if (pdata->charge_ok_pin != INVALID_GPIO){
1167                         if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
1168                                 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1169                                 bat->bat_capacity = 100;
1170                         }
1171                 }
1172         }
1173
1174
1175 #if 0
1176 #if 1
1177         rk30_adc_battery_poweron_capacity_check();
1178 #else
1179         gBatteryData->poweron_check = 1;
1180 #endif
1181         gBatteryData->poweron_check = 0;
1182 #endif
1183 /*******************************************
1184 //¿ª»ú²ÉÑùµ½µÄµçѹºÍÉϴιػú±£´æµçѹÏà²î½Ï´ó£¬Ôõô´¦Àí£¿
1185 if (bat->bat_capacity > old_capacity)
1186 {
1187 if ((bat->bat_capacity - old_capacity) > 20)
1188 {
1189
1190 }
1191 }
1192 else if (bat->bat_capacity < old_capacity)
1193 {
1194 if ((old_capacity > bat->bat_capacity) > 20)
1195 {
1196
1197 }
1198 }
1199 *********************************************/
1200         if (bat->bat_capacity == 0) bat->bat_capacity = 1;
1201
1202
1203         if(1==gBatteryData -> pdata->low_voltage_protection)
1204
1205                 if ((bat->bat_voltage <= batt_table[0].dis_charge_vol+ 50)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING)){
1206                         kernel_power_off();
1207                 }
1208
1209 }
1210
1211 static void rk30_adc_battery_callback(struct adc_client *client, void *param, int result)
1212 {
1213 #if 0
1214         struct rk30_adc_battery_data  *info = container_of(client, struct rk30_adc_battery_data,
1215                 client);
1216         info->adc_val = result;
1217 #endif
1218         if (result < 0){
1219                 pr_bat("adc_battery_callback    resule < 0 , the value ");
1220                 return;
1221         }
1222         else{
1223                 gBatteryData->adc_val = result;
1224                 pr_bat("result = %d, gBatteryData->adc_val = %d\n", result, gBatteryData->adc_val );
1225         }
1226         return;
1227 }
1228
1229 #if 1
1230 static void rk30_adc_battery_lowerpower_delaywork(struct work_struct *work)
1231 {
1232         int irq;
1233         if( gBatteryData->pdata->batt_low_pin != INVALID_GPIO){
1234                 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
1235                 disable_irq(irq);
1236         }
1237
1238         printk("lowerpower\n");
1239         rk28_send_wakeup_key(); // wake up the system   
1240         return;
1241 }
1242
1243
1244 static irqreturn_t rk30_adc_battery_low_wakeup(int irq,void *dev_id)
1245 {
1246         queue_work(gBatteryData->wq, &gBatteryData->lowerpower_work);
1247         return IRQ_HANDLED;
1248 }
1249
1250 #endif
1251
1252 static int rk30_adc_battery_probe(struct platform_device *pdev)
1253 {
1254         int    ret;
1255         int    irq;
1256         int    irq_flag;
1257         struct adc_client                   *client;
1258         struct rk30_adc_battery_data          *data;
1259         struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1260
1261         data = kzalloc(sizeof(*data), GFP_KERNEL);
1262         if (data == NULL) {
1263                 ret = -ENOMEM;
1264                 goto err_data_alloc_failed;
1265         }
1266         
1267         memset(data, 0, sizeof(struct rk30_adc_battery_data));
1268         gBatteryData = data;
1269         platform_set_drvdata(pdev, data);
1270
1271         data->pdata = pdata;
1272         data->status_lock = 0;  
1273         ret = rk30_adc_battery_io_init(pdata);
1274          if (ret) {
1275                 goto err_io_init;
1276         }
1277     
1278         memset(data->adc_samples, 0, sizeof(int)*(NUM_VOLTAGE_SAMPLE + 2));
1279
1280          //register adc for battery sample
1281          if(0 == pdata->adc_channel)
1282                 client = adc_register(0, rk30_adc_battery_callback, NULL);  //pdata->adc_channel = ani0
1283         else
1284                 client = adc_register(pdata->adc_channel, rk30_adc_battery_callback, NULL);  
1285         if(!client)
1286                 goto err_adc_register_failed;
1287             
1288          //variable init
1289         data->client  = client;
1290         data->adc_val = adc_sync_read(client);
1291
1292         ret = power_supply_register(&pdev->dev, &rk30_battery_supply);
1293         if (ret){
1294                 printk(KERN_INFO "fail to battery power_supply_register\n");
1295                 goto err_battery_failed;
1296         }
1297                 
1298
1299 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1300         ret = power_supply_register(&pdev->dev, &rk30_usb_supply);
1301         if (ret){
1302                 printk(KERN_INFO "fail to usb power_supply_register\n");
1303                 goto err_usb_failed;
1304         }
1305 #endif
1306         wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");        
1307
1308         data->wq = create_singlethread_workqueue("adc_battd");
1309         INIT_DELAYED_WORK(&data->delay_work, rk30_adc_battery_timer_work);
1310         
1311         //Power on Battery detect
1312         rk30_adc_battery_check(data);
1313         if(1 == pdata->save_capacity ){
1314                 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS*10));
1315                 gBatteryData->poweron_check = 1;
1316         }else{
1317                 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1318                 gBatteryData->poweron_check = 0;
1319         }
1320
1321 #if  defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1322         ret = power_supply_register(&pdev->dev, &rk30_ac_supply);
1323         if (ret) {
1324                 printk(KERN_INFO "fail to ac power_supply_register\n");
1325                 goto err_ac_failed;
1326         }
1327         //init dc dectet irq & delay work
1328         if (pdata->dc_det_pin != INVALID_GPIO){
1329                 INIT_WORK(&data->dcwakeup_work, rk30_adc_battery_dcdet_delaywork);
1330                 
1331                 irq = gpio_to_irq(pdata->dc_det_pin);           
1332                 irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1333                 ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);
1334                 if (ret) {
1335                         printk("failed to request dc det irq\n");
1336                         goto err_dcirq_failed;
1337                 }
1338                 enable_irq_wake(irq);  
1339         
1340         }
1341 #endif
1342
1343 #if 1
1344         // batt low irq lowerpower_work
1345         if( pdata->batt_low_pin != INVALID_GPIO){
1346                 INIT_WORK(&data->lowerpower_work, rk30_adc_battery_lowerpower_delaywork);
1347                 
1348                 irq = gpio_to_irq(pdata->batt_low_pin);
1349                 ret = request_irq(irq, rk30_adc_battery_low_wakeup, IRQF_TRIGGER_LOW, "batt_low_irq", NULL);
1350
1351                 if (ret) {
1352                         printk("failed to request batt_low_irq irq\n");
1353                         goto err_lowpowerirq_failed;
1354                 }
1355                 disable_irq(irq);
1356         
1357         }
1358 #endif
1359
1360         printk(KERN_INFO "rk30_adc_battery: driver initialized\n");
1361         
1362         return 0;
1363         
1364 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1365 err_usb_failed:
1366         power_supply_unregister(&rk30_usb_supply);
1367 #endif
1368
1369 err_ac_failed:
1370 #if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1371         power_supply_unregister(&rk30_ac_supply);
1372 #endif
1373
1374 err_battery_failed:
1375         power_supply_unregister(&rk30_battery_supply);
1376     
1377 err_dcirq_failed:
1378         free_irq(gpio_to_irq(pdata->dc_det_pin), data);
1379 #if 1
1380  err_lowpowerirq_failed:
1381         free_irq(gpio_to_irq(pdata->batt_low_pin), data);
1382 #endif
1383 err_adc_register_failed:
1384 err_io_init:    
1385 err_data_alloc_failed:
1386         kfree(data);
1387
1388         printk("rk30_adc_battery: error!\n");
1389     
1390         return ret;
1391 }
1392
1393 static int rk30_adc_battery_remove(struct platform_device *pdev)
1394 {
1395         struct rk30_adc_battery_data *data = platform_get_drvdata(pdev);
1396         struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1397
1398         cancel_delayed_work(&gBatteryData->delay_work); 
1399 #if defined(CONFIG_BATTERY_RK30_USB_CHARGE)
1400         power_supply_unregister(&rk30_usb_supply);
1401 #endif
1402 #if defined(CONFIG_BATTERY_RK30_AC_CHARGE)
1403         power_supply_unregister(&rk30_ac_supply);
1404 #endif
1405         power_supply_unregister(&rk30_battery_supply);
1406
1407         free_irq(gpio_to_irq(pdata->dc_det_pin), data);
1408
1409         kfree(data);
1410         
1411         return 0;
1412 }
1413
1414 static struct platform_driver rk30_adc_battery_driver = {
1415         .probe          = rk30_adc_battery_probe,
1416         .remove         = rk30_adc_battery_remove,
1417         .suspend                = rk30_adc_battery_suspend,
1418         .resume         = rk30_adc_battery_resume,
1419         .driver = {
1420                 .name = "rk30-battery",
1421                 .owner  = THIS_MODULE,
1422         }
1423 };
1424
1425 static int __init rk30_adc_battery_init(void)
1426 {
1427         return platform_driver_register(&rk30_adc_battery_driver);
1428 }
1429
1430 static void __exit rk30_adc_battery_exit(void)
1431 {
1432         platform_driver_unregister(&rk30_adc_battery_driver);
1433 }
1434
1435 //subsys_initcall(rk30_adc_battery_init);//subsys_initcall(rk30_adc_battery_init);
1436 module_init(rk30_adc_battery_init);//subsys_initcall(rk30_adc_battery_init);
1437
1438 module_exit(rk30_adc_battery_exit);
1439
1440 MODULE_DESCRIPTION("Battery detect driver for the rk30");
1441 MODULE_AUTHOR("luowei lw@rock-chips.com");
1442 MODULE_LICENSE("GPL");