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