1 /* drivers/power/rk29_adc_battery.c
3 * battery detect driver for the rk29
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 rk29_battery_dbg_level = 0;
46 module_param_named(dbg_level, rk29_battery_dbg_level, int, 0644);
48 /*******************ÒÔϲÎÊý¿ÉÒÔÐÞ¸Ä******************************/
49 #define TIMER_MS_COUNTS 1000 //¶¨Ê±Æ÷µÄ³¤¶Èms
50 //ÒÔϲÎÊýÐèÒª¸ù¾Ýʵ¼Ê²âÊÔµ÷Õû
51 #define SLOPE_SECOND_COUNTS 15 //ͳ¼ÆµçѹбÂʵÄʱ¼ä¼ä¸ôs
52 #define DISCHARGE_MIN_SECOND 45 //×î¿ì·Åµçµç1%ʱ¼ä
53 #define CHARGE_MIN_SECOND 45 //×î¿ì³äµçµç1%ʱ¼ä
54 #define CHARGE_MID_SECOND 90 //ÆÕͨ³äµçµç1%ʱ¼ä
55 #define CHARGE_MAX_SECOND 250 //×³äµçµç1%ʱ¼ä
56 #define CHARGE_FULL_DELAY_TIMES 10 //³äµçÂú¼ì²â·À¶¶Ê±¼ä
57 #define USBCHARGE_IDENTIFY_TIMES 5 //²åÈëUSB»ìÁ÷£¬pcʶ±ð¼ì²âʱ¼ä
59 #define NUM_VOLTAGE_SAMPLE ((SLOPE_SECOND_COUNTS * 1000) / TIMER_MS_COUNTS)
60 #define NUM_DISCHARGE_MIN_SAMPLE ((DISCHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)
61 #define NUM_CHARGE_MIN_SAMPLE ((CHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)
62 #define NUM_CHARGE_MID_SAMPLE ((CHARGE_MID_SECOND * 1000) / TIMER_MS_COUNTS)
63 #define NUM_CHARGE_MAX_SAMPLE ((CHARGE_MAX_SECOND * 1000) / TIMER_MS_COUNTS)
64 #define NUM_CHARGE_FULL_DELAY_TIMES ((CHARGE_FULL_DELAY_TIMES * 1000) / TIMER_MS_COUNTS) //³äµçÂú״̬³ÖÐøʱ¼ä³¤¶È
65 #define NUM_USBCHARGE_IDENTIFY_TIMES ((USBCHARGE_IDENTIFY_TIMES * 1000) / TIMER_MS_COUNTS) //³äµçÂú״̬³ÖÐøʱ¼ä³¤¶È
67 #define BAT_2V5_VALUE 2500
69 //¶¨ÒåADC²ÉÑù·Öѹµç×裬ÒÔʵ¼ÊֵΪ׼£¬µ¥Î»K
70 #define BAT_PULL_UP_R 300 ////200
72 #define BAT_PULL_DOWN_R 100// 200
73 #define adc_to_voltage(adc_val) ((adc_val * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R))
75 #define BATT_FILENAME "/data/bat_last_capacity.dat"
77 static struct wake_lock batt_wake_lock;
86 #ifdef CONFIG_BATTERY_RK29_VOL3V8
88 #define BATT_MAX_VOL_VALUE 4200 //ÂúµçʱµÄµç³Øµçѹ
89 #define BATT_ZERO_VOL_VALUE 3400 //¹Ø»úʱµÄµç³Øµçѹ
90 #define BATT_NOMAL_VOL_VALUE 3800
91 static struct batt_vol_cal batt_table[BATT_NUM] = {
104 /*******************************************************************************/
108 #define BATT_MAX_VOL_VALUE 8200 //ÂúµçʱµÄµç³Øµçѹ
109 #define BATT_ZERO_VOL_VALUE 6800 //¹Ø»úʱµÄµç³Øµçѹ
110 #define BATT_NOMAL_VOL_VALUE 7600
111 static struct batt_vol_cal batt_table[BATT_NUM] = {
126 /********************************************************************************/
128 extern int dwc_vbus_status(void);
129 extern int get_gadget_connect_flag(void);
131 struct rk29_adc_battery_data {
134 //struct timer_list timer;
135 struct workqueue_struct *wq;
136 struct delayed_work delay_work;
137 struct work_struct dcwakeup_work;
138 struct work_struct lowerpower_work;
141 struct rk29_adc_battery_platform_data *pdata;
145 struct adc_client *client;
147 int adc_samples[NUM_VOLTAGE_SAMPLE+2];
157 int old_charge_level;
159 int gBatCapacityDisChargeCnt;
160 int gBatCapacityChargeCnt;
163 int suspend_capacity;
166 static struct rk29_adc_battery_data *gBatteryData;
172 BATTERY_CAPACITY = 3,
173 BATTERY_AC_ONLINE = 4,
174 BATTERY_STATUS_CHANGED = 5,
175 AC_STATUS_CHANGED = 6,
176 BATTERY_INT_STATUS = 7,
177 BATTERY_INT_ENABLE = 8,
190 static int rk29_adc_battery_load_capacity(void)
193 int* p = (int *)value;
194 long fd = sys_open(BATT_FILENAME,O_RDONLY,0);
197 printk("rk29_adc_battery_load_capacity: open file /data/bat_last_capacity.dat failed\n");
201 sys_read(fd,(char __user *)value,4);
207 static void rk29_adc_battery_put_capacity(int loadcapacity)
210 int* p = (int *)value;
211 long fd = sys_open(BATT_FILENAME,O_CREAT | O_RDWR,0);
214 printk("rk29_adc_battery_put_capacity: open file /data/bat_last_capacity.dat failed\n");
219 sys_write(fd, (const char __user *)value, 4);
224 static void rk29_adc_battery_charge_enable(struct rk29_adc_battery_data *bat)
226 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
228 if (pdata->charge_set_pin != INVALID_GPIO){
229 gpio_direction_output(pdata->charge_set_pin, pdata->charge_set_level);
233 static void rk29_adc_battery_charge_disable(struct rk29_adc_battery_data *bat)
235 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
237 if (pdata->charge_set_pin != INVALID_GPIO){
238 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
242 //extern int suspend_flag;
243 static int rk29_adc_battery_get_charge_level(struct rk29_adc_battery_data *bat)
246 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
248 #if defined (CONFIG_BATTERY_RK29_AC_CHARGE)
249 if (pdata->dc_det_pin != INVALID_GPIO){
250 if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level){
256 #if defined (CONFIG_BATTERY_RK29_USB_CHARGE)
260 if (1 == dwc_vbus_status()) { //¼ì²âµ½USB²åÈ룬µ«ÊÇÎÞ·¨Ê¶±ðÊÇ·ñÊdzäµçÆ÷
261 //ͨ¹ýÑÓʱ¼ì²âPCʶ±ð±êÖ¾£¬Èç¹û³¬Ê±¼ì²â²»µ½£¬ËµÃ÷Êdzäµç
262 if (1 == get_gadget_connect_flag()){ //²åÈë³äµçÆ÷ʱ¼ä´óÓÚÒ»¶¨Ê±¼äÖ®ºó£¬¿ªÊ¼½øÈë³äµç״̬
263 if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES){
264 gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
267 } //·ñÔò£¬²»½øÈë³äµçģʽ
270 gBatUsbChargeCnt = 0;
271 if (2 == dwc_vbus_status()) {
280 //int old_charge_level;
281 static int rk29_adc_battery_status_samples(struct rk29_adc_battery_data *bat)
285 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
287 charge_level = rk29_adc_battery_get_charge_level(bat);
289 //¼ì²â³äµç״̬±ä»¯Çé¿ö
290 if (charge_level != bat->old_charge_level){
291 bat->old_charge_level = charge_level;
295 rk29_adc_battery_charge_enable(bat);
298 rk29_adc_battery_charge_disable(bat);
300 bat->bat_status_cnt = 0; //״̬±ä»¯¿ªÊ¼¼ÆÊý
303 if(charge_level == 0){
306 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
310 if (pdata->charge_ok_pin == INVALID_GPIO){ //no charge_ok_pin
312 if (bat->bat_capacity == 100){
313 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
314 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
319 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
322 else{ // pin of charge_ok_pin
323 if (gpio_get_value(pdata->charge_ok_pin) != pdata->charge_ok_level){
326 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
329 //¼ì²âµ½³äµçÂúµçƽ±êÖ¾
332 if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) {
333 bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
336 if ((bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) && (bat->bat_capacity >= 99)){
337 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
338 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
339 bat->bat_capacity = 100;
344 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
353 static int *pSamples;
354 static void rk29_adc_battery_voltage_samples(struct rk29_adc_battery_data *bat)
357 int i,*pStart = bat->adc_samples, num = 0;
358 int level = rk29_adc_battery_get_charge_level(bat);
361 value = bat->adc_val;
362 adc_async_read(bat->client);
364 *pSamples++ = adc_to_voltage(value);
366 bat->bat_status_cnt++;
367 if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE) bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1;
369 num = pSamples - pStart;
371 if (num >= NUM_VOLTAGE_SAMPLE){
373 num = NUM_VOLTAGE_SAMPLE;
378 for (i = 0; i < num; i++){
379 value += bat->adc_samples[i];
381 bat->bat_voltage = value / num;
385 if(bat->bat_voltage >= batt_table[BATT_NUM-1].charge_vol+ 10)
386 bat->bat_voltage = batt_table[BATT_NUM-1].charge_vol + 10;
387 else if(bat->bat_voltage <= batt_table[0].charge_vol - 10)
388 bat->bat_voltage = batt_table[0].charge_vol - 10;
391 if(bat->bat_voltage >= batt_table[BATT_NUM-1].dis_charge_vol+ 10)
392 bat->bat_voltage = batt_table[BATT_NUM-1].dis_charge_vol + 10;
393 else if(bat->bat_voltage <= batt_table[0].dis_charge_vol - 10)
394 bat->bat_voltage = batt_table[0].dis_charge_vol - 10;
398 static int rk29_adc_battery_voltage_to_capacity(struct rk29_adc_battery_data *bat, int BatVoltage)
403 struct batt_vol_cal *p;
406 if (rk29_adc_battery_get_charge_level(bat)){ //charge
407 if(BatVoltage >= (p[BATT_NUM - 1].charge_vol)){
411 if(BatVoltage <= (p[0].charge_vol)){
415 for(i = 0; i < BATT_NUM - 1; i++){
417 if(((p[i].charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].charge_vol))){
418 capacity = i * 10 + ((BatVoltage - p[i].charge_vol) * 10) / (p[i+1].charge_vol- p[i].charge_vol);
427 if(BatVoltage >= (p[BATT_NUM - 1].dis_charge_vol)){
431 if(BatVoltage <= (p[0].dis_charge_vol)){
435 for(i = 0; i < BATT_NUM - 1; i++){
436 if(((p[i].dis_charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].dis_charge_vol))){
437 capacity = i * 10 + ((BatVoltage - p[i].dis_charge_vol) * 10) / (p[i+1].dis_charge_vol- p[i].dis_charge_vol); ;
450 static void rk29_adc_battery_capacity_samples(struct rk29_adc_battery_data *bat)
453 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
455 //³ä·Åµç״̬±ä»¯ºó£¬BufferÌîÂú֮ǰ£¬²»¸üÐÂ
456 if (bat->bat_status_cnt < NUM_VOLTAGE_SAMPLE) {
457 bat->gBatCapacityDisChargeCnt = 0;
458 bat->gBatCapacityChargeCnt = 0;
462 capacity = rk29_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
464 if (rk29_adc_battery_get_charge_level(bat)){
465 if (capacity > bat->bat_capacity){
466 //ʵ¼Ê²ÉÑùµ½µÄÈÝÁ¿±ÈÏÔʾµÄÈÝÁ¿´ó£¬Öð¼¶ÉÏÉý
467 if (++(bat->gBatCapacityDisChargeCnt) >= NUM_CHARGE_MIN_SAMPLE){
468 bat->gBatCapacityDisChargeCnt = 0;
469 if (bat->bat_capacity < 99){
474 bat->gBatCapacityChargeCnt = 0;
476 else{ // ʵ¼ÊµÄÈÝÁ¿±È²ÉÑù±È ÏÔʾµÄÈÝÁ¿Ð¡
477 bat->gBatCapacityDisChargeCnt = 0;
478 (bat->gBatCapacityChargeCnt)++;
480 if (pdata->charge_ok_pin != INVALID_GPIO){
481 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
482 //¼ì²âµ½µç³Ø³äÂú±êÖ¾£¬Í¬Ê±³¤Ê±¼äÄÚ³äµçµçѹÎޱ仯£¬¿ªÊ¼Æô¶¯¼Æʱ³äµç£¬¿ìËÙÉÏÉýÈÝÁ¿
483 if (bat->gBatCapacityChargeCnt >= NUM_CHARGE_MIN_SAMPLE){
484 bat->gBatCapacityChargeCnt = 0;
485 if (bat->bat_capacity < 99){
493 if (capacity > capacitytmp){
494 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
495 gBatCapacityChargeCnt = 0;
497 else if (/*bat->bat_capacity >= 85) &&*/ (gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
498 gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
500 if (bat->bat_capacity < 99){
506 #else // ·ÀÖ¹µç³ØÀÏ»¯ºó³öÏֳ岻ÂúµÄÇé¿ö£¬
507 if (capacity > bat->capacitytmp){
508 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
509 bat->gBatCapacityChargeCnt = 0;
513 if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
514 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
516 if (bat->bat_capacity < 99){
527 //ûÓгäµçÂú¼ì²â½Å£¬³¤Ê±¼äÄÚµçѹÎޱ仯£¬¶¨Ê±Æ÷Ä£Äâ³äµç
528 if (capacity > bat->capacitytmp){
529 //¹ý³ÌÖÐÈç¹ûµçѹÓÐÔö³¤£¬¶¨Ê±Æ÷¸´Î»£¬·ÀÖ¹¶¨Ê±Æ÷Ä£Äâ³äµç±Èʵ¼Ê³äµç¿ì
530 bat->gBatCapacityChargeCnt = 0;
534 if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
535 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
537 if (bat->bat_capacity < 99){
549 //·Åµçʱ,Ö»ÔÊÐíµçѹϽµ
550 if (capacity < bat->bat_capacity){
551 if (++(bat->gBatCapacityDisChargeCnt) >= NUM_DISCHARGE_MIN_SAMPLE){
552 bat->gBatCapacityDisChargeCnt = 0;
553 if (bat->bat_capacity > 0){
554 bat->bat_capacity-- ;
560 bat->gBatCapacityDisChargeCnt = 0;
562 bat->gBatCapacityChargeCnt = 0;
564 bat->capacitytmp = capacity;
567 //static int poweron_check = 0;
568 static void rk29_adc_battery_poweron_capacity_check(void)
571 int new_capacity, old_capacity;
573 new_capacity = gBatteryData->bat_capacity;
574 old_capacity = rk29_adc_battery_load_capacity();
575 if ((old_capacity <= 0) || (old_capacity >= 100)){
576 old_capacity = new_capacity;
579 if (gBatteryData->bat_status == POWER_SUPPLY_STATUS_FULL){
580 if (new_capacity > 80){
581 gBatteryData->bat_capacity = 100;
584 else if (gBatteryData->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
587 // //1£©³¤Ê±¼ä¹Ø»ú·ÅÖú󣬿ª»úºó¶ÁÈ¡µÄÈÝÁ¿Ô¶Ô¶´óÓÚʵ¼ÊÈÝÁ¿Ôõô°ì£¿
588 // //2£©Èç¹û²»ÕâÑù×ö£¬¶Ìʱ¼ä¹Ø»úÔÙ¿ª»ú£¬Ç°ºóÈÝÁ¿²»Ò»ÖÂÓÖ¸ÃÔõô°ì£¿
589 // //3£©Ò»ÏÂÄÇÖÖ·½Ê½ºÏÊÊ£¿
590 //gBatteryData->bat_capacity = new_capacity;
591 gBatteryData->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
594 if(new_capacity > old_capacity + 50 )
595 gBatteryData->bat_capacity = new_capacity;
597 gBatteryData->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; //avoid the value of capacity increase
601 //printk("capacity = %d, new_capacity = %d, old_capacity = %d\n",gBatteryData->bat_capacity, new_capacity, old_capacity);
603 gBatteryData->bat_change = 1;
606 static void rk29_adc_battery_scan_timer(unsigned long data)
608 gBatteryData->timer.expires = jiffies + msecs_to_jiffies(TIMER_MS_COUNTS);
609 add_timer(&gBatteryData->timer);
611 schedule_work(&gBatteryData->timer_work);
615 #if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
616 static int rk29_adc_battery_get_usb_property(struct power_supply *psy,
617 enum power_supply_property psp,
618 union power_supply_propval *val)
620 charger_type_t charger;
621 charger = CHARGER_USB;
624 case POWER_SUPPLY_PROP_ONLINE:
625 if (psy->type == POWER_SUPPLY_TYPE_USB)
626 val->intval = get_msc_connect_flag();
627 printk("%s:%d\n",__FUNCTION__,val->intval);
638 static enum power_supply_property rk29_adc_battery_usb_props[] = {
640 POWER_SUPPLY_PROP_ONLINE,
643 static struct power_supply rk29_usb_supply =
646 .type = POWER_SUPPLY_TYPE_USB,
648 .get_property = rk29_adc_battery_get_usb_property,
650 .properties = rk29_adc_battery_usb_props,
651 .num_properties = ARRAY_SIZE(rk29_adc_battery_usb_props),
655 #if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
656 static irqreturn_t rk29_adc_battery_dc_wakeup(int irq, void *dev_id)
658 queue_work(gBatteryData->wq, &gBatteryData->dcwakeup_work);
663 static int rk29_adc_battery_get_ac_property(struct power_supply *psy,
664 enum power_supply_property psp,
665 union power_supply_propval *val)
668 charger_type_t charger;
669 charger = CHARGER_USB;
671 case POWER_SUPPLY_PROP_ONLINE:
672 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
674 // printk("POWER_SUPPLY_TYPE_MAINS\n");
675 if (rk29_adc_battery_get_charge_level(gBatteryData))
684 DBG("%s:%d\n",__FUNCTION__,val->intval);
695 static enum power_supply_property rk29_adc_battery_ac_props[] =
697 POWER_SUPPLY_PROP_ONLINE,
700 static struct power_supply rk29_ac_supply =
703 .type = POWER_SUPPLY_TYPE_MAINS,
705 .get_property = rk29_adc_battery_get_ac_property,
707 .properties = rk29_adc_battery_ac_props,
708 .num_properties = ARRAY_SIZE(rk29_adc_battery_ac_props),
711 static void rk29_adc_battery_dcdet_delaywork(struct work_struct *work)
714 struct rk29_adc_battery_platform_data *pdata;
717 //printk("DC_WAKEUP\n");
718 pdata = gBatteryData->pdata;
719 irq = gpio_to_irq(pdata->dc_det_pin);
720 irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
722 rk28_send_wakeup_key(); // wake up the system
725 ret = request_irq(irq, rk29_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);// reinitialize the DC irq
730 power_supply_changed(&rk29_ac_supply);
732 gBatteryData->bat_status_cnt = 0; //the state of battery is change
734 wake_lock_timeout(&batt_wake_lock, 29 * HZ);
741 static int rk29_adc_battery_get_status(struct rk29_adc_battery_data *bat)
743 return (bat->bat_status);
746 static int rk29_adc_battery_get_health(struct rk29_adc_battery_data *bat)
748 return POWER_SUPPLY_HEALTH_GOOD;
751 static int rk29_adc_battery_get_present(struct rk29_adc_battery_data *bat)
753 return (bat->bat_voltage < BATT_MAX_VOL_VALUE) ? 0 : 1;
756 static int rk29_adc_battery_get_voltage(struct rk29_adc_battery_data *bat)
758 return (bat->bat_voltage );
761 static int rk29_adc_battery_get_capacity(struct rk29_adc_battery_data *bat)
763 return (bat->bat_capacity);
766 static int rk29_adc_battery_get_property(struct power_supply *psy,
767 enum power_supply_property psp,
768 union power_supply_propval *val)
773 case POWER_SUPPLY_PROP_STATUS:
774 val->intval = rk29_adc_battery_get_status(gBatteryData);
775 DBG("gBatStatus=%d\n",val->intval);
777 case POWER_SUPPLY_PROP_HEALTH:
778 val->intval = rk29_adc_battery_get_health(gBatteryData);
779 DBG("gBatHealth=%d\n",val->intval);
781 case POWER_SUPPLY_PROP_PRESENT:
782 val->intval = rk29_adc_battery_get_present(gBatteryData);
783 DBG("gBatPresent=%d\n",val->intval);
785 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
786 val ->intval = rk29_adc_battery_get_voltage(gBatteryData);
787 DBG("gBatVoltage=%d\n",val->intval);
789 // case POWER_SUPPLY_PROP_CURRENT_NOW:
790 // val->intval = 1100;
792 case POWER_SUPPLY_PROP_CAPACITY:
793 val->intval = rk29_adc_battery_get_capacity(gBatteryData);
794 DBG("gBatCapacity=%d%%\n",val->intval);
796 case POWER_SUPPLY_PROP_TECHNOLOGY:
797 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
799 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
800 val->intval = BATT_MAX_VOL_VALUE;
802 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
803 val->intval = BATT_ZERO_VOL_VALUE;
813 static enum power_supply_property rk29_adc_battery_props[] = {
815 POWER_SUPPLY_PROP_STATUS,
816 POWER_SUPPLY_PROP_HEALTH,
817 POWER_SUPPLY_PROP_PRESENT,
818 POWER_SUPPLY_PROP_VOLTAGE_NOW,
819 // POWER_SUPPLY_PROP_CURRENT_NOW,
820 POWER_SUPPLY_PROP_TECHNOLOGY,
821 POWER_SUPPLY_PROP_CAPACITY,
822 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
823 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
826 static struct power_supply rk29_battery_supply =
829 .type = POWER_SUPPLY_TYPE_BATTERY,
831 .get_property = rk29_adc_battery_get_property,
833 .properties = rk29_adc_battery_props,
834 .num_properties = ARRAY_SIZE(rk29_adc_battery_props),
838 //int suspend_capacity = 0;
839 static void rk29_adc_battery_resume_check(void)
843 int new_capacity, old_capacity;
844 struct rk29_adc_battery_data *bat = gBatteryData;
846 bat->old_charge_level = -1;
847 pSamples = bat->adc_samples;
849 adc_sync_read(bat->client); //start adc sample
850 level = oldlevel = rk29_adc_battery_status_samples(bat);//init charge status
852 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++) { //0.3 s
855 rk29_adc_battery_voltage_samples(bat); //get voltage
856 level = rk29_adc_battery_status_samples(bat); //check charge status
857 if (oldlevel != level){
858 oldlevel = level; //if charge status changed, reset sample
862 new_capacity = rk29_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
863 old_capacity =gBatteryData-> suspend_capacity;
865 if (bat->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
867 bat->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
870 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; // aviod the value of capacity increase dicharge
873 //printk("rk29_adc_battery_resume: status = %d, voltage = %d, capacity = %d, new_capacity = %d, old_capacity = %d\n",
874 // bat->bat_status, bat->bat_voltage, bat->bat_capacity, new_capacity, old_capacity); //xsf
876 // wake_lock_timeout(&batt_wake_lock, 5 * HZ); //5s
879 static int rk29_adc_battery_suspend(struct platform_device *dev, pm_message_t state)
881 gBatteryData->suspend_capacity = gBatteryData->bat_capacity;
885 static int rk29_adc_battery_resume(struct platform_device *dev)
887 gBatteryData->resume = true;
891 #define rk29_adc_battery_suspend NULL
892 #define rk29_adc_battery_resume NULL
896 unsigned long AdcTestCnt = 0;
897 static void rk29_adc_battery_timer_work(struct work_struct *work)
900 if (gBatteryData->resume) {
901 rk29_adc_battery_resume_check();
902 gBatteryData->resume = false;
906 rk29_adc_battery_status_samples(gBatteryData);
908 if (gBatteryData->poweron_check){
909 gBatteryData->poweron_check = 0;
910 rk29_adc_battery_poweron_capacity_check();
913 rk29_adc_battery_voltage_samples(gBatteryData);
915 rk29_adc_battery_capacity_samples(gBatteryData);
918 /*update battery parameter after adc and capacity has been changed*/
919 if(gBatteryData->bat_change){
920 gBatteryData->bat_change = 0;
921 rk29_adc_battery_put_capacity(gBatteryData->bat_capacity);
922 power_supply_changed(&rk29_battery_supply);
925 if (rk29_battery_dbg_level)
927 if (++AdcTestCnt >= 2)
931 printk("Status = %d, RealAdcVal = %d, RealVol = %d,gBatVol = %d, gBatCap = %d, RealCapacity = %d, dischargecnt = %d, chargecnt = %d\n",
932 gBatteryData->bat_status, gBatteryData->adc_val, adc_to_voltage(gBatteryData->adc_val),
933 gBatteryData->bat_voltage, gBatteryData->bat_capacity, gBatteryData->capacitytmp, gBatteryData->gBatCapacityDisChargeCnt,gBatteryData-> gBatCapacityChargeCnt);
937 queue_delayed_work(gBatteryData->wq, &gBatteryData->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
942 static int rk29_adc_battery_io_init(struct rk29_adc_battery_platform_data *pdata)
946 if (pdata->io_init) {
951 if (pdata->charge_set_pin != INVALID_GPIO){
952 ret = gpio_request(pdata->charge_set_pin, NULL);
954 printk("failed to request dc_det gpio\n");
957 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
960 //dc charge detect pin
961 if (pdata->dc_det_pin != INVALID_GPIO){
962 ret = gpio_request(pdata->dc_det_pin, NULL);
964 printk("failed to request dc_det gpio\n");
968 gpio_pull_updown(pdata->dc_det_pin, GPIOPullUp);//important
969 ret = gpio_direction_input(pdata->dc_det_pin);
971 printk("failed to set gpio dc_det input\n");
977 if (pdata->charge_ok_pin != INVALID_GPIO){
978 ret = gpio_request(pdata->charge_ok_pin, NULL);
980 printk("failed to request charge_ok gpio\n");
984 gpio_pull_updown(pdata->charge_ok_pin, GPIOPullUp);//important
985 ret = gpio_direction_input(pdata->charge_ok_pin);
987 printk("failed to set gpio charge_ok input\n");
992 if( pdata->batt_low_pin != INVALID_GPIO){
993 ret = gpio_request(pdata->batt_low_pin, NULL);
995 printk("failed to request batt_low_pin gpio\n");
999 gpio_pull_updown(pdata->batt_low_pin, GPIOPullUp);
1000 ret = gpio_direction_input(pdata->batt_low_pin);
1002 printk("failed to set gpio batt_low_pin input\n");
1011 #define POWER_ON_PIN RK29_PIN4_PA4
1012 #define LOOP(loops) do { unsigned int i = loops; barrier(); while (--i) barrier(); } while (0)
1014 //extern void kernel_power_off(void);
1015 static void rk29_adc_battery_check(struct rk29_adc_battery_data *bat)
1020 struct rk29_adc_battery_platform_data *pdata = bat->pdata;
1021 //printk("%s--%d:\n",__FUNCTION__,__LINE__);
1023 bat->old_charge_level = -1;
1024 bat->capacitytmp = 0;
1025 bat->suspend_capacity = 0;
1027 pSamples = bat->adc_samples;
1029 adc_sync_read(bat->client); //start adc sample
1030 level = oldlevel = rk29_adc_battery_status_samples(bat);//init charge status
1032 bat->full_times = 0;
1033 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){ //0.3 s
1035 rk29_adc_battery_voltage_samples(bat); //get voltage
1036 //level = rk29_adc_battery_status_samples(bat); //check charge status
1037 level = rk29_adc_battery_get_charge_level(bat);
1039 if (oldlevel != level){
1040 oldlevel = level; //if charge status changed, reset sample
1045 bat->bat_capacity = rk29_adc_battery_voltage_to_capacity(bat, bat->bat_voltage); //init bat_capacity
1047 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1048 if (rk29_adc_battery_get_charge_level(bat)){
1049 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1051 if (pdata->charge_ok_pin != INVALID_GPIO){
1052 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
1053 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1054 bat->bat_capacity = 100;
1060 rk29_adc_battery_poweron_capacity_check();
1062 gBatteryData->poweron_check = 1;
1064 gBatteryData->poweron_check = 0;
1066 /*******************************************
1067 //¿ª»ú²ÉÑùµ½µÄµçѹºÍÉϴιػú±£´æµçѹÏà²î½Ï´ó£¬Ôõô´¦Àí£¿
1068 if (bat->bat_capacity > old_capacity)
1070 if ((bat->bat_capacity - old_capacity) > 20)
1075 else if (bat->bat_capacity < old_capacity)
1077 if ((old_capacity > bat->bat_capacity) > 20)
1082 *********************************************/
1083 if (bat->bat_capacity == 0) bat->bat_capacity = 1;
1085 if (bat->bat_voltage <= BATT_ZERO_VOL_VALUE + 500){
1086 printk("low battery: powerdown\n");
1087 gpio_direction_output(POWER_ON_PIN, GPIO_LOW);
1090 if(gpio_get_value(POWER_ON_PIN) == GPIO_HIGH){
1091 gpio_set_value(POWER_ON_PIN,GPIO_LOW);
1099 gpio_direction_output(POWER_ON_PIN, GPIO_HIGH);
1103 static void rk29_adc_battery_callback(struct adc_client *client, void *param, int result)
1106 struct rk29_adc_battery_data *info = container_of(client, struct rk29_adc_battery_data,
1108 info->adc_val = result;
1110 gBatteryData->adc_val = result;
1115 static void rk29_adc_battery_lowerpower_delaywork(struct work_struct *work)
1117 struct rk29_adc_battery_platform_data *pdata;
1119 printk("lowerpower\n");
1120 pdata = gBatteryData->pdata;
1121 irq = gpio_to_irq(pdata->dc_det_pin);
1122 rk28_send_wakeup_key(); // wake up the system
1123 free_irq(irq, NULL);
1128 static irqreturn_t rk29_adc_battery_low_wakeup(int irq,void *dev_id)
1131 schedule_work(&gBatteryData->lowerpower_work);
1137 static int rk29_adc_battery_probe(struct platform_device *pdev)
1142 struct adc_client *client;
1143 struct rk29_adc_battery_data *data;
1144 struct rk29_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1146 //printk("%s--%d:\n",__FUNCTION__,__LINE__);
1147 data = kzalloc(sizeof(*data), GFP_KERNEL);
1150 goto err_data_alloc_failed;
1152 gBatteryData = data;
1154 platform_set_drvdata(pdev, data);
1156 data->pdata = pdata;
1158 ret = rk29_adc_battery_io_init(pdata);
1163 memset(data->adc_samples, 0, sizeof(int)*(NUM_VOLTAGE_SAMPLE + 2));
1165 //register adc for battery sample
1166 client = adc_register(0, rk29_adc_battery_callback, NULL); //pdata->adc_channel = ani0
1168 goto err_adc_register_failed;
1171 data->client = client;
1172 data->adc_val = adc_sync_read(client);
1174 ret = power_supply_register(&pdev->dev, &rk29_battery_supply);
1176 printk(KERN_INFO "fail to battery power_supply_register\n");
1177 goto err_battery_failed;
1181 #if defined (CONFIG_BATTERY_RK29_USB_CHARGE)
1182 ret = power_supply_register(&pdev->dev, &rk29_usb_supply);
1184 printk(KERN_INFO "fail to usb power_supply_register\n");
1185 goto err_usb_failed;
1188 wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");
1190 data->wq = create_singlethread_workqueue("adc_battd");
1191 INIT_DELAYED_WORK(&data->delay_work, rk29_adc_battery_timer_work);
1192 //Power on Battery detect
1193 rk29_adc_battery_check(data);
1194 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1196 #if defined (CONFIG_BATTERY_RK29_AC_CHARGE)
1197 ret = power_supply_register(&pdev->dev, &rk29_ac_supply);
1199 printk(KERN_INFO "fail to ac power_supply_register\n");
1202 //init dc dectet irq & delay work
1203 if (pdata->dc_det_pin != INVALID_GPIO){
1204 INIT_WORK(&data->dcwakeup_work, rk29_adc_battery_dcdet_delaywork);
1206 irq = gpio_to_irq(pdata->dc_det_pin);
1207 irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1208 ret = request_irq(irq, rk29_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);
1210 printk("failed to request dc det irq\n");
1211 goto err_dcirq_failed;
1213 enable_irq_wake(irq);
1218 // batt low irq lowerpower_work
1219 if( pdata->batt_low_pin != INVALID_GPIO){
1220 INIT_WORK(&data->lowerpower_work, rk29_adc_battery_lowerpower_delaywork);
1222 irq = gpio_to_irq(pdata->batt_low_pin);
1223 ret = request_irq(irq, rk29_adc_battery_low_wakeup, IRQF_TRIGGER_LOW, "batt_low_irq", NULL);
1226 printk("failed to request batt_low_irq irq\n");
1227 goto err_lowpowerirq_failed;
1229 enable_irq_wake(irq);
1233 // printk(KERN_INFO "rk29_adc_battery: driver initialized\n");
1237 #if defined (CONFIG_BATTERY_RK29_USB_CHARGE)
1239 power_supply_unregister(&rk29_usb_supply);
1242 #if defined (CONFIG_BATTERY_RK29_AC_CHARGE)
1244 power_supply_unregister(&rk29_ac_supply);
1246 free_irq(gpio_to_irq(pdata->dc_det_pin), data);
1250 power_supply_unregister(&rk29_battery_supply);
1253 err_lowpowerirq_failed:
1254 free_irq(gpio_to_irq(pdata->batt_low_pin), data);
1256 err_adc_register_failed:
1258 err_data_alloc_failed:
1261 printk("rk29_adc_battery: error!\n");
1266 static int rk29_adc_battery_remove(struct platform_device *pdev)
1268 struct rk29_adc_battery_data *data = platform_get_drvdata(pdev);
1269 struct rk29_adc_battery_platform_data *pdata = pdev->dev.platform_data;
1271 cancel_delayed_work(&gBatteryData->delay_work);
1272 #if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
1273 power_supply_unregister(&rk29_usb_supply);
1275 #if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
1276 power_supply_unregister(&rk29_ac_supply);
1278 power_supply_unregister(&rk29_battery_supply);
1280 free_irq(gpio_to_irq(pdata->dc_det_pin), data);
1287 static struct platform_driver rk29_adc_battery_driver = {
1288 .probe = rk29_adc_battery_probe,
1289 .remove = rk29_adc_battery_remove,
1290 .suspend = rk29_adc_battery_suspend,
1291 .resume = rk29_adc_battery_resume,
1293 .name = "rk2918-battery",
1294 .owner = THIS_MODULE,
1298 static int __init rk29_adc_battery_init(void)
1300 return platform_driver_register(&rk29_adc_battery_driver);
1303 static void __exit rk29_adc_battery_exit(void)
1305 platform_driver_unregister(&rk29_adc_battery_driver);
1308 subsys_initcall(rk29_adc_battery_init);//subsys_initcall(rk29_adc_battery_init);
1309 module_exit(rk29_adc_battery_exit);
1311 MODULE_DESCRIPTION("Battery detect driver for the rk29");
1312 MODULE_AUTHOR("luowei lw@rock-chips.com");
1313 MODULE_LICENSE("GPL");