1 /* drivers/power/rk30_adc_battery.c
3 * battery detect driver for the rk30
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.
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.
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>
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>
37 #include <linux/wakelock.h>
40 #define DBG(x...) printk(x)
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...) \
49 if (rk30_battery_dbg_level) { \
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ʶ±ð¼ì²âʱ¼ä
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) //³äµçÂú״̬³ÖÐøʱ¼ä³¤¶È
74 #define CHARGE_IS_OK 1
75 #define INVALID_CHARGE_CHECK -1
77 #if defined(CONFIG_ARCH_RK3066B)
79 #define BAT_DEFINE_VALUE 1800
80 #elif defined(CONFIG_ARCH_RK2928)
81 #define BAT_DEFINE_VALUE 3300
83 #define BAT_DEFINE_VALUE 2500
88 #define BATT_FILENAME "/data/bat_last_capacity.dat"
90 static struct wake_lock batt_wake_lock;
99 #ifdef CONFIG_BATTERY_RK30_VOL3V8
101 #define BATT_MAX_VOL_VALUE 4120 //ÂúµçʱµÄµç³Øµçѹ
102 #define BATT_ZERO_VOL_VALUE 3400 //¹Ø»úʱµÄµç³Øµçѹ
103 #define BATT_NOMAL_VOL_VALUE 3800
105 #define BAT_PULL_UP_R 200
106 #if defined(CONFIG_ARCH_RK3066B)
107 #define BAT_PULL_DOWN_R 100
109 #define BAT_PULL_DOWN_R 200
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},
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
128 //¶¨ÒåADC²ÉÑù·Öѹµç×裬ÒÔʵ¼ÊֵΪ׼£¬µ¥Î»K
130 #define BAT_PULL_UP_R 300
131 #define BAT_PULL_DOWN_R 100
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
149 #define BATT_NUM ARRAY_SIZE(batt_table)
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))
153 /********************************************************************************/
155 extern int dwc_vbus_status(void);
156 extern int get_msc_connect_flag(void);
158 struct rk30_adc_battery_data {
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;
168 struct rk30_adc_battery_platform_data *pdata;
172 struct adc_client *client;
174 int adc_samples[NUM_VOLTAGE_SAMPLE+2];
184 int old_charge_level;
186 int gBatCapacityDisChargeCnt;
187 int gBatCapacityChargeCnt;
190 int suspend_capacity;
195 static struct rk30_adc_battery_data *gBatteryData;
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,
219 static int rk30_adc_battery_load_capacity(void)
222 int* p = (int *)value;
223 long fd = sys_open(BATT_FILENAME,O_RDONLY,0);
226 pr_bat("rk30_adc_battery_load_capacity: open file /data/bat_last_capacity.dat failed\n");
230 sys_read(fd,(char __user *)value,4);
236 static void rk30_adc_battery_put_capacity(int loadcapacity)
239 int* p = (int *)value;
240 long fd = sys_open(BATT_FILENAME,O_CREAT | O_RDWR,0);
243 pr_bat("rk30_adc_battery_put_capacity: open file /data/bat_last_capacity.dat failed\n");
248 sys_write(fd, (const char __user *)value, 4);
253 static void rk30_adc_battery_charge_enable(struct rk30_adc_battery_data *bat)
255 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
257 if (pdata->charge_set_pin != INVALID_GPIO){
258 gpio_direction_output(pdata->charge_set_pin, pdata->charge_set_level);
262 static void rk30_adc_battery_charge_disable(struct rk30_adc_battery_data *bat)
264 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
266 if (pdata->charge_set_pin != INVALID_GPIO){
267 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
271 //extern int suspend_flag;
272 static int rk30_adc_battery_get_charge_level(struct rk30_adc_battery_data *bat)
275 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
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){
284 if(pdata->is_dc_charging)
285 charge_on =pdata->is_dc_charging();
291 if (pdata->spport_usb_charging) //is usb charging
292 if(pdata->is_usb_charging)
293 charge_on = pdata->is_usb_charging();
298 static int is_charge_ok(struct rk30_adc_battery_data *bat)
300 int charge_is_ok = 0;
301 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
303 if((pdata->charge_ok_pin == INVALID_GPIO)&& ( pdata->charging_ok == NULL))
306 if (pdata->charge_ok_pin != INVALID_GPIO){
307 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level)
311 }else if( pdata->charging_ok)
313 charge_is_ok = pdata->charging_ok();
320 //int old_charge_level;
321 static int rk30_adc_battery_status_samples(struct rk30_adc_battery_data *bat)
324 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
325 charge_level = rk30_adc_battery_get_charge_level(bat);
327 //¼ì²â³äµç״̬±ä»¯Çé¿ö
328 if (charge_level != bat->old_charge_level){
329 bat->old_charge_level = charge_level;
333 rk30_adc_battery_charge_enable(bat);
336 rk30_adc_battery_charge_disable(bat);
338 bat->bat_status_cnt = 0; //״̬±ä»¯¿ªÊ¼¼ÆÊý
341 if(charge_level == 0){
344 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
348 if( is_charge_ok(bat) == INVALID_CHARGE_CHECK){
349 //if (pdata->charge_ok_pin == INVALID_GPIO){ //no charge_ok_pin
351 if (bat->bat_capacity == 100){
352 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
353 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
358 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
361 else{ // pin of charge_ok_pin
362 if (is_charge_ok(bat) != CHARGE_IS_OK ){
365 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
368 //¼ì²âµ½³äµçÂúµçƽ±êÖ¾
371 if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) {
372 bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
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;
383 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
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))
393 static int rk_adc_voltage(int value)
397 int ref_voltage; //reference_voltage
401 ref_voltage = gBatteryData ->pdata->reference_voltage;
402 pullup_res = gBatteryData ->pdata->pull_up_res;
403 pulldown_res = gBatteryData ->pdata->pull_down_res;
405 if(ref_voltage && pullup_res && pulldown_res){
407 voltage = ((value * ref_voltage * (pullup_res + pulldown_res)) / (1024 * pulldown_res));
410 voltage = adc_to_voltage(value);
418 static int *pSamples;
419 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat)
422 int i,*pStart = bat->adc_samples, num = 0;
423 int level = rk30_adc_battery_get_charge_level(bat);
426 value = bat->adc_val;
427 adc_async_read(bat->client);
429 *pSamples++ = rk_adc_voltage(value);
431 bat->bat_status_cnt++;
432 if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE) bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1;
434 num = pSamples - pStart;
436 if (num >= NUM_VOLTAGE_SAMPLE){
438 num = NUM_VOLTAGE_SAMPLE;
443 for (i = 0; i < num; i++){
444 value += bat->adc_samples[i];
446 bat->bat_voltage = value / num;
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;
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;
464 static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage)
469 struct batt_vol_cal *p;
472 if (rk30_adc_battery_get_charge_level(bat)){ //charge
473 if(BatVoltage >= (p[BATT_NUM - 1].charge_vol)){
477 if(BatVoltage <= (p[0].charge_vol)){
481 for(i = 0; i < BATT_NUM - 1; i++){
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);
493 if(BatVoltage >= (p[BATT_NUM - 1].dis_charge_vol)){
497 if(BatVoltage <= (p[0].dis_charge_vol)){
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) ;
517 static void rk30_adc_battery_capacity_samples(struct rk30_adc_battery_data *bat)
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;
524 //³ä·Åµç״̬±ä»¯ºó£¬BufferÌîÂú֮ǰ£¬²»¸üÐÂ
525 if (bat->bat_status_cnt < NUM_VOLTAGE_SAMPLE) {
526 bat->gBatCapacityDisChargeCnt = 0;
527 bat->gBatCapacityChargeCnt = 0;
531 capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
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){
549 bat->gBatCapacityChargeCnt = 0;
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){
576 if (capacity > capacitytmp){
577 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
578 gBatCapacityChargeCnt = 0;
580 else if (/*bat->bat_capacity >= 85) &&*/ (gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
581 gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
583 if (bat->bat_capacity < 99){
589 #else // ·ÀÖ¹µç³ØÀÏ»¯ºó³öÏֳ岻ÂúµÄÇé¿ö£¬
590 if (capacity > bat->capacitytmp){
591 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
592 bat->gBatCapacityChargeCnt = 0;
596 if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
597 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
599 if (bat->bat_capacity < 99){
610 //ûÓгäµçÂú¼ì²â½Å£¬³¤Ê±¼äÄÚµçѹÎޱ仯£¬¶¨Ê±Æ÷Ä£Äâ³äµç
611 if (capacity > bat->capacitytmp){
612 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
613 bat->gBatCapacityChargeCnt = 0;
617 if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
618 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
620 if (bat->bat_capacity <= 99){
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
641 if (++(bat->gBatCapacityDisChargeCnt) >= timer_of_discharge_sample){
642 bat->gBatCapacityDisChargeCnt = 0;
643 if (bat->bat_capacity > 0){
644 bat->bat_capacity-- ;
650 bat->gBatCapacityDisChargeCnt = 0;
652 bat->gBatCapacityChargeCnt = 0;
654 bat->capacitytmp = capacity;
657 //static int poweron_check = 0;
658 static void rk30_adc_battery_poweron_capacity_check(void)
661 int new_capacity, old_capacity;
663 new_capacity = gBatteryData->bat_capacity;
664 old_capacity = rk30_adc_battery_load_capacity();
666 pr_bat("------------old_capacity---%d\n",old_capacity);
667 if ((old_capacity <= 0) || (old_capacity >= 100)){
668 old_capacity = new_capacity;
671 if (gBatteryData->bat_status == POWER_SUPPLY_STATUS_FULL){
672 if (new_capacity > 80){
673 gBatteryData->bat_capacity = 100;
676 else if (gBatteryData->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
679 // //1£©³¤Ê±¼ä¹Ø»ú·ÅÖú󣬿ª»úºó¶ÁÈ¡µÄÈÝÁ¿Ô¶Ô¶´óÓÚʵ¼ÊÈÝÁ¿Ôõô°ì£¿
680 // //2£©Èç¹û²»ÕâÑù×ö£¬¶Ìʱ¼ä¹Ø»úÔÙ¿ª»ú£¬Ç°ºóÈÝÁ¿²»Ò»ÖÂÓÖ¸ÃÔõô°ì£¿
681 // //3£©Ò»ÏÂÄÇÖÖ·½Ê½ºÏÊÊ£¿
682 //gBatteryData->bat_capacity = new_capacity;
683 gBatteryData->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
686 if(new_capacity > old_capacity + 50 )
687 gBatteryData->bat_capacity = new_capacity;
689 gBatteryData->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; //avoid the value of capacity increase
693 pr_bat("capacity = %d, new_capacity = %d, old_capacity = %d\n",gBatteryData->bat_capacity, new_capacity, old_capacity);
695 gBatteryData->bat_change = 1;
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)
703 charger_type_t charger;
704 charger = CHARGER_USB;
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();
714 printk("%s:%d\n",__FUNCTION__,val->intval);
725 static enum power_supply_property rk30_adc_battery_usb_props[] = {
727 POWER_SUPPLY_PROP_ONLINE,
730 static struct power_supply rk30_usb_supply =
733 .type = POWER_SUPPLY_TYPE_USB,
735 .get_property = rk30_adc_battery_get_usb_property,
737 .properties = rk30_adc_battery_usb_props,
738 .num_properties = ARRAY_SIZE(rk30_adc_battery_usb_props),
742 #if defined(CONFIG_BATTERY_RK30_AC_CHARGE)
743 static irqreturn_t rk30_adc_battery_dc_wakeup(int irq, void *dev_id)
745 queue_work(gBatteryData->wq, &gBatteryData->dcwakeup_work);
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)
755 charger_type_t charger;
756 charger = CHARGER_USB;
758 case POWER_SUPPLY_PROP_ONLINE:
759 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
761 if (rk30_adc_battery_get_charge_level(gBatteryData))
770 DBG("%s:%d\n",__FUNCTION__,val->intval);
781 static enum power_supply_property rk30_adc_battery_ac_props[] =
783 POWER_SUPPLY_PROP_ONLINE,
786 static struct power_supply rk30_ac_supply =
789 .type = POWER_SUPPLY_TYPE_MAINS,
791 .get_property = rk30_adc_battery_get_ac_property,
793 .properties = rk30_adc_battery_ac_props,
794 .num_properties = ARRAY_SIZE(rk30_adc_battery_ac_props),
797 static void rk30_adc_battery_dcdet_delaywork(struct work_struct *work)
800 struct rk30_adc_battery_platform_data *pdata;
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;
808 rk28_send_wakeup_key(); // wake up the system
811 ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);// reinitialize the DC irq
816 power_supply_changed(&rk30_ac_supply);
818 gBatteryData->bat_status_cnt = 0; //the state of battery is change
825 static int rk30_adc_battery_get_status(struct rk30_adc_battery_data *bat)
827 return (bat->bat_status);
830 static int rk30_adc_battery_get_health(struct rk30_adc_battery_data *bat)
832 return POWER_SUPPLY_HEALTH_GOOD;
835 static int rk30_adc_battery_get_present(struct rk30_adc_battery_data *bat)
837 return (bat->bat_voltage < BATT_MAX_VOL_VALUE) ? 0 : 1;
840 static int rk30_adc_battery_get_voltage(struct rk30_adc_battery_data *bat)
842 return (bat->bat_voltage );
845 static int rk30_adc_battery_get_capacity(struct rk30_adc_battery_data *bat)
847 return (bat->bat_capacity);
850 static int rk30_adc_battery_get_property(struct power_supply *psy,
851 enum power_supply_property psp,
852 union power_supply_propval *val)
857 case POWER_SUPPLY_PROP_STATUS:
858 val->intval = rk30_adc_battery_get_status(gBatteryData);
859 DBG("gBatStatus=%d\n",val->intval);
861 case POWER_SUPPLY_PROP_HEALTH:
862 val->intval = rk30_adc_battery_get_health(gBatteryData);
863 DBG("gBatHealth=%d\n",val->intval);
865 case POWER_SUPPLY_PROP_PRESENT:
866 val->intval = rk30_adc_battery_get_present(gBatteryData);
867 DBG("gBatPresent=%d\n",val->intval);
869 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
870 val ->intval = rk30_adc_battery_get_voltage(gBatteryData);
871 DBG("gBatVoltage=%d\n",val->intval);
873 // case POWER_SUPPLY_PROP_CURRENT_NOW:
874 // val->intval = 1100;
876 case POWER_SUPPLY_PROP_CAPACITY:
877 val->intval = rk30_adc_battery_get_capacity(gBatteryData);
878 DBG("gBatCapacity=%d%%\n",val->intval);
880 case POWER_SUPPLY_PROP_TECHNOLOGY:
881 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
883 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
884 val->intval = BATT_MAX_VOL_VALUE;
886 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
887 val->intval = BATT_ZERO_VOL_VALUE;
897 static enum power_supply_property rk30_adc_battery_props[] = {
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,
910 static struct power_supply rk30_battery_supply =
913 .type = POWER_SUPPLY_TYPE_BATTERY,
915 .get_property = rk30_adc_battery_get_property,
917 .properties = rk30_adc_battery_props,
918 .num_properties = ARRAY_SIZE(rk30_adc_battery_props),
922 static void rk30_adc_battery_resume_check(void)
926 int new_capacity, old_capacity;
927 struct rk30_adc_battery_data *bat = gBatteryData;
929 bat->old_charge_level = -1;
930 pSamples = bat->adc_samples;
932 adc_sync_read(bat->client); //start adc sample
933 level = oldlevel = rk30_adc_battery_status_samples(bat);//init charge status
935 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++) { //0.3 s
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
945 new_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
946 old_capacity =gBatteryData-> suspend_capacity;
948 if (bat->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
950 bat->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
953 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; // aviod the value of capacity increase dicharge
958 static int rk30_adc_battery_suspend(struct platform_device *dev, pm_message_t state)
961 gBatteryData->suspend_capacity = gBatteryData->bat_capacity;
962 cancel_delayed_work(&gBatteryData->delay_work);
964 if( gBatteryData->pdata->batt_low_pin != INVALID_GPIO){
966 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
968 enable_irq_wake(irq);
974 static int rk30_adc_battery_resume(struct platform_device *dev)
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){
981 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
982 disable_irq_wake(irq);
988 #define rk30_adc_battery_suspend NULL
989 #define rk30_adc_battery_resume NULL
993 unsigned long AdcTestCnt = 0;
994 static void rk30_adc_battery_timer_work(struct work_struct *work)
997 if (gBatteryData->resume) {
998 rk30_adc_battery_resume_check();
999 gBatteryData->resume = false;
1003 rk30_adc_battery_status_samples(gBatteryData);
1005 if (gBatteryData->poweron_check){
1006 gBatteryData->poweron_check = 0;
1007 rk30_adc_battery_poweron_capacity_check();
1010 rk30_adc_battery_voltage_samples(gBatteryData);
1011 rk30_adc_battery_capacity_samples(gBatteryData);
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;
1021 if(1 == gBatteryData->status_lock ){
1022 wake_unlock(&batt_wake_lock); //unlock
1023 gBatteryData->status_lock = 0;
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);
1042 if (rk30_battery_dbg_level){
1043 if (++AdcTestCnt >= 2)
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);
1053 queue_delayed_work(gBatteryData->wq, &gBatteryData->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1058 static int rk30_adc_battery_io_init(struct rk30_adc_battery_platform_data *pdata)
1062 if (pdata->io_init) {
1067 //charge control pin
1068 if (pdata->charge_set_pin != INVALID_GPIO){
1069 ret = gpio_request(pdata->charge_set_pin, NULL);
1071 printk("failed to request dc_det gpio\n");
1074 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
1077 //dc charge detect pin
1078 if (pdata->dc_det_pin != INVALID_GPIO){
1079 ret = gpio_request(pdata->dc_det_pin, NULL);
1081 printk("failed to request dc_det gpio\n");
1085 gpio_pull_updown(pdata->dc_det_pin, GPIOPullUp);//important
1086 ret = gpio_direction_input(pdata->dc_det_pin);
1088 printk("failed to set gpio dc_det input\n");
1094 if (pdata->charge_ok_pin != INVALID_GPIO){
1095 ret = gpio_request(pdata->charge_ok_pin, NULL);
1097 printk("failed to request charge_ok gpio\n");
1101 gpio_pull_updown(pdata->charge_ok_pin, GPIOPullUp);//important
1102 ret = gpio_direction_input(pdata->charge_ok_pin);
1104 printk("failed to set gpio charge_ok input\n");
1109 if( pdata->batt_low_pin != INVALID_GPIO){
1110 ret = gpio_request(pdata->batt_low_pin, NULL);
1112 printk("failed to request batt_low_pin gpio\n");
1116 gpio_pull_updown(pdata->batt_low_pin, GPIOPullUp);
1117 ret = gpio_direction_input(pdata->batt_low_pin);
1119 printk("failed to set gpio batt_low_pin input\n");
1129 extern void kernel_power_off(void);
1130 static void rk30_adc_battery_check(struct rk30_adc_battery_data *bat)
1134 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1135 //printk("%s--%d:\n",__FUNCTION__,__LINE__);
1137 bat->old_charge_level = -1;
1138 bat->capacitytmp = 0;
1139 bat->suspend_capacity = 0;
1141 pSamples = bat->adc_samples;
1143 adc_sync_read(bat->client); //start adc sample
1144 level = oldlevel = rk30_adc_battery_status_samples(bat);//init charge status
1146 bat->full_times = 0;
1147 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){ //0.3 s
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);
1153 if (oldlevel != level){
1154 oldlevel = level; //if charge status changed, reset sample
1159 bat->bat_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage); //init bat_capacity
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;
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;
1177 rk30_adc_battery_poweron_capacity_check();
1179 gBatteryData->poweron_check = 1;
1181 gBatteryData->poweron_check = 0;
1183 /*******************************************
1184 //¿ª»ú²ÉÑùµ½µÄµçѹºÍÉϴιػú±£´æµçѹÏà²î½Ï´ó£¬Ôõô´¦Àí£¿
1185 if (bat->bat_capacity > old_capacity)
1187 if ((bat->bat_capacity - old_capacity) > 20)
1192 else if (bat->bat_capacity < old_capacity)
1194 if ((old_capacity > bat->bat_capacity) > 20)
1199 *********************************************/
1200 if (bat->bat_capacity == 0) bat->bat_capacity = 1;
1203 if(1==gBatteryData -> pdata->low_voltage_protection)
1205 if ((bat->bat_voltage <= batt_table[0].dis_charge_vol+ 50)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING)){
1211 static void rk30_adc_battery_callback(struct adc_client *client, void *param, int result)
1214 struct rk30_adc_battery_data *info = container_of(client, struct rk30_adc_battery_data,
1216 info->adc_val = result;
1219 pr_bat("adc_battery_callback resule < 0 , the value ");
1223 gBatteryData->adc_val = result;
1224 pr_bat("result = %d, gBatteryData->adc_val = %d\n", result, gBatteryData->adc_val );
1230 static void rk30_adc_battery_lowerpower_delaywork(struct work_struct *work)
1233 if( gBatteryData->pdata->batt_low_pin != INVALID_GPIO){
1234 irq = gpio_to_irq(gBatteryData->pdata->batt_low_pin);
1238 printk("lowerpower\n");
1239 rk28_send_wakeup_key(); // wake up the system
1244 static irqreturn_t rk30_adc_battery_low_wakeup(int irq,void *dev_id)
1246 queue_work(gBatteryData->wq, &gBatteryData->lowerpower_work);
1252 static int rk30_adc_battery_probe(struct platform_device *pdev)
1257 struct adc_client *client;
1258 struct rk30_adc_battery_data *data;
1259 struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1261 data = kzalloc(sizeof(*data), GFP_KERNEL);
1264 goto err_data_alloc_failed;
1267 memset(data, 0, sizeof(struct rk30_adc_battery_data));
1268 gBatteryData = data;
1269 platform_set_drvdata(pdev, data);
1271 data->pdata = pdata;
1272 data->status_lock = 0;
1273 ret = rk30_adc_battery_io_init(pdata);
1278 memset(data->adc_samples, 0, sizeof(int)*(NUM_VOLTAGE_SAMPLE + 2));
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
1284 client = adc_register(pdata->adc_channel, rk30_adc_battery_callback, NULL);
1286 goto err_adc_register_failed;
1289 data->client = client;
1290 data->adc_val = adc_sync_read(client);
1292 ret = power_supply_register(&pdev->dev, &rk30_battery_supply);
1294 printk(KERN_INFO "fail to battery power_supply_register\n");
1295 goto err_battery_failed;
1299 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1300 ret = power_supply_register(&pdev->dev, &rk30_usb_supply);
1302 printk(KERN_INFO "fail to usb power_supply_register\n");
1303 goto err_usb_failed;
1306 wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");
1308 data->wq = create_singlethread_workqueue("adc_battd");
1309 INIT_DELAYED_WORK(&data->delay_work, rk30_adc_battery_timer_work);
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;
1317 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1318 gBatteryData->poweron_check = 0;
1321 #if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1322 ret = power_supply_register(&pdev->dev, &rk30_ac_supply);
1324 printk(KERN_INFO "fail to ac power_supply_register\n");
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);
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);
1335 printk("failed to request dc det irq\n");
1336 goto err_dcirq_failed;
1338 enable_irq_wake(irq);
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);
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);
1352 printk("failed to request batt_low_irq irq\n");
1353 goto err_lowpowerirq_failed;
1360 printk(KERN_INFO "rk30_adc_battery: driver initialized\n");
1364 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1366 power_supply_unregister(&rk30_usb_supply);
1370 #if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
1371 power_supply_unregister(&rk30_ac_supply);
1375 power_supply_unregister(&rk30_battery_supply);
1378 free_irq(gpio_to_irq(pdata->dc_det_pin), data);
1380 err_lowpowerirq_failed:
1381 free_irq(gpio_to_irq(pdata->batt_low_pin), data);
1383 err_adc_register_failed:
1385 err_data_alloc_failed:
1388 printk("rk30_adc_battery: error!\n");
1393 static int rk30_adc_battery_remove(struct platform_device *pdev)
1395 struct rk30_adc_battery_data *data = platform_get_drvdata(pdev);
1396 struct rk30_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1398 cancel_delayed_work(&gBatteryData->delay_work);
1399 #if defined(CONFIG_BATTERY_RK30_USB_CHARGE)
1400 power_supply_unregister(&rk30_usb_supply);
1402 #if defined(CONFIG_BATTERY_RK30_AC_CHARGE)
1403 power_supply_unregister(&rk30_ac_supply);
1405 power_supply_unregister(&rk30_battery_supply);
1407 free_irq(gpio_to_irq(pdata->dc_det_pin), data);
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,
1420 .name = "rk30-battery",
1421 .owner = THIS_MODULE,
1425 static int __init rk30_adc_battery_init(void)
1427 return platform_driver_register(&rk30_adc_battery_driver);
1430 static void __exit rk30_adc_battery_exit(void)
1432 platform_driver_unregister(&rk30_adc_battery_driver);
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);
1438 module_exit(rk30_adc_battery_exit);
1440 MODULE_DESCRIPTION("Battery detect driver for the rk30");
1441 MODULE_AUTHOR("luowei lw@rock-chips.com");
1442 MODULE_LICENSE("GPL");