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 <linux/adc.h>
29 #include <linux/delay.h>
30 #include <linux/ktime.h>
31 #include <linux/slab.h>
32 #include <linux/syscalls.h>
34 #include <linux/wakelock.h>
36 #include <linux/string.h>
37 #include <linux/init.h>
38 #include <linux/suspend.h>
39 #include <linux/gpio.h>
40 #include <linux/of_gpio.h>
41 #include <linux/power/rockchip-adc-battery.h>
42 #include <linux/iio/iio.h>
43 #include <linux/iio/machine.h>
44 #include <linux/iio/driver.h>
45 #include <linux/iio/consumer.h>
47 #ifdef CONFIG_EARLYSUSPEND
48 /* kernel/power/earlysuspend.c */
49 extern suspend_state_t get_suspend_state(void);
52 static int rk30_battery_dbg_level;
53 module_param_named(dbg_level, rk30_battery_dbg_level, int, 0644);
54 #define DBG( args...) \
56 if (rk30_battery_dbg_level) {\
61 #define TIMER_MS_COUNTS 1000
62 #define SLOPE_SECOND_COUNTS 15
63 #define DISCHARGE_MIN_SECOND 30
64 #define CHARGE_MIN_SECOND 30
65 #define CHARGE_MID_SECOND 90
66 #define CHARGE_MAX_SECOND 250
67 #define CHARGE_FULL_DELAY_TIMES 10
68 #define USBCHARGE_IDENTIFY_TIMES 2
70 #define NUM_VOLTAGE_SAMPLE ((SLOPE_SECOND_COUNTS * 1000) / TIMER_MS_COUNTS)
71 #define NUM_DISCHARGE_MIN_SAMPLE ((DISCHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)
72 #define NUM_CHARGE_MIN_SAMPLE ((CHARGE_MIN_SECOND * 1000) / TIMER_MS_COUNTS)
73 #define NUM_CHARGE_MID_SAMPLE ((CHARGE_MID_SECOND * 1000) / TIMER_MS_COUNTS)
74 #define NUM_CHARGE_MAX_SAMPLE ((CHARGE_MAX_SECOND * 1000) / TIMER_MS_COUNTS)
75 #define NUM_CHARGE_FULL_DELAY_TIMES ((CHARGE_FULL_DELAY_TIMES * 1000) / TIMER_MS_COUNTS)
76 #define NUM_USBCHARGE_IDENTIFY_TIMES ((USBCHARGE_IDENTIFY_TIMES * 1000) / TIMER_MS_COUNTS)
78 #define CHARGE_IS_OK 1
79 #define INVALID_CHARGE_CHECK -1
81 #if defined(CONFIG_ARCH_RK3066B)
82 #define BAT_DEFINE_VALUE 1800
83 #elif defined(CONFIG_ARCH_RK2928)
84 #define BAT_DEFINE_VALUE 3300
86 #define BAT_DEFINE_VALUE 3300 /*2500*/
89 #define RK30_GPIO_LOW 0
90 #define RK30_GPIO_HIGH 1
94 #define BATT_FILENAME "/data/bat_last_capacity.dat"
100 int battery_dbg_level = 0;
101 int battery_test_flag = 0;
102 int gVoltageCnt = 3400;
103 int gDoubleVoltageCnt = 6800;
104 unsigned long gSecondsCnt = 0;
105 char gDischargeFlag[4] = {"on "};
108 #define BATT_MAX_VOL_VALUE 4250/*Full charge volatge*/
109 #define BATT_ZERO_VOL_VALUE 3500/*power down voltage*/
110 #define BATT_NOMAL_VOL_VALUE 3800
112 static int batt_table[2*BATT_NUM+6] =
114 0x4B434F52,0x7461625F,0x79726574,0,100,100,
115 3496, 3548, 3599, 3626, 3655, 3697, 3751, 3812, 3877, 3949, 4030, //discharge
116 3540, 3785, 3842, 3861, 3915, 3980, 4041, 4135, 4169, 4175, 4185 //charge
118 #define adc_to_voltage(adc_val) ((adc_val * BAT_DEFINE_VALUE * (batt_table[4] +batt_table[5])) / (1024 *batt_table[5]))
120 #define BATT_MAX_VOL_VALUE 8284 //Full charge voltage
121 #define BATT_ZERO_VOL_VALUE 6900 // power down voltage
122 #define BATT_NOMAL_VOL_VALUE 7600
125 static int batt_table[2*BATT_NUM+6] =
127 0x4B434F52,0x7461625F,0x79726574,1,300,100,
128 6800, 7242, 7332, 7404, 7470, 7520, 7610, 7744, 7848, 8016, 8284,//discharge
129 7630, 7754, 7852, 7908, 7956, 8024, 8112, 8220, 8306, 8318, 8328//charge
131 #define adc_to_voltage(adc_val) ((adc_val * BAT_DEFINE_VALUE * (batt_table[4] +batt_table[5])) / (1024 *batt_table[5]))
138 /********************************************************************************/
145 BATTERY_CAPACITY = 3,
146 BATTERY_AC_ONLINE = 4,
147 BATTERY_STATUS_CHANGED = 5,
148 AC_STATUS_CHANGED = 6,
149 BATTERY_INT_STATUS = 7,
150 BATTERY_INT_ENABLE = 8,
161 struct rk30_adc_battery_data {
164 //struct timer_list timer;
165 struct workqueue_struct *wq;
166 struct delayed_work delay_work;
167 struct delayed_work check_work;
168 struct work_struct dcwakeup_work;
169 struct work_struct lowerpower_work;
172 struct rk30_adc_battery_platform_data *pdata;
174 struct adc_client *client;
176 int adc_samples[NUM_VOLTAGE_SAMPLE+2];
186 int old_charge_level;
188 int gBatCapacityDisChargeCnt;
189 int gBatCapacityChargeCnt;
190 int gBatCapacityacChargeCnt;
191 int gBatCapacityusbChargeCnt ;
192 int gBatCapacityusbdisChargeCnt;
194 int suspend_capacity;
195 int gBatUsbChargeCnt;
198 struct power_supply bat;
199 struct power_supply usb;
200 struct power_supply ac;
201 struct power_supply bk_bat;
209 int charge_source_now;
210 int charge_soure_old;
211 int charge_start_capacity;
212 int charge_start_voltage;
213 int start_voltage_status;
214 int charge_up_proprotion;
215 int charge_down_proportion;
216 int voltage_to_local;
217 unsigned long suspend_time;
218 unsigned long resume_time;
222 int charge_full_flag;
224 struct notifier_block battery_nb;
226 int lower_power_flag;
230 static struct rk30_adc_battery_data *gBatteryData;
231 static struct wake_lock batt_wake_lock;
232 static struct wake_lock batt_wake_lock_detect_lowpower;
233 static struct wake_lock charge_display_lock;
234 int system_lowerpower = 0;
235 extern void kernel_power_off(void);
236 extern int dwc_vbus_status(void);
237 extern int get_gadget_connect_flag(void);
238 extern int dwc_otg_check_dpdm(void);
239 static int is_charge_ok(struct rk30_adc_battery_data *bat);
240 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat);
241 extern void rk_send_wakeup_key(void);
242 static bool batt_gpio_is_valid(int number)
244 return number > 0 && number < 256;
247 //#define BAT_ADC_TABLE_LEN 11
248 static ssize_t bat_param_read(struct device *dev,struct device_attribute *attr, char *buf)
251 for(i=0;i<BATT_NUM;i++)
252 printk("i=%d batt_table=%d\n",i+6,batt_table[i+6]);
254 for(i=0;i<BATT_NUM;i++)
255 printk("i=%d batt_table=%d\n",i+17,batt_table[i+17]);
258 DEVICE_ATTR(batparam, 0444, bat_param_read, NULL);
261 static ssize_t rkbatt_show_debug_attrs(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 return sprintf(buf, "%d\n", battery_dbg_level);
267 static ssize_t rkbatt_restore_debug_attrs(struct device *dev,
268 struct device_attribute *attr, const char *buf, size_t size)
272 sscanf(buf, "%d", &liTmp);
274 if(liTmp != 0 && liTmp != 1)
276 dev_err(dev, "rk29adc_restore_debug_attrs err\n");
280 battery_dbg_level = liTmp;
285 static ssize_t rkbatt_show_state_attrs(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 // struct rk30_adc_battery_platform_data *pdata = gBatteryData->pdata;
289 int charge_ok_value =0 ;
290 charge_ok_value = is_charge_ok(gBatteryData) ;
293 "gBatVol=%d,gBatCap=%d,charge_ok=%d,%s\n",
294 gBatteryData->bat_voltage,gBatteryData->bat_capacity,
295 charge_ok_value,gDischargeFlag);
298 static ssize_t rkbatt_restore_state_attrs(struct device *dev,
299 struct device_attribute *attr, const char *buf, size_t size)
304 static ssize_t rkbatt_show_value_attrs(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 return sprintf(buf, "pull_up_res =%d,\npull_down_res=%d\n", batt_table[4],batt_table[5]);
310 static ssize_t rkbatt_restore_value_attrs(struct device *dev,
311 struct device_attribute *attr, const char *buf, size_t size)
316 sscanf(buf, "%d,%d", &liUp,&liDown);
318 if(liUp != 0 && liDown != 0)
320 batt_table[4] = liUp;
321 batt_table[5] = liDown;
326 static ssize_t rkbatt_show_flag_attrs(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 return sprintf(buf, "rk29_battery_test_flag=%d\n", battery_test_flag);
331 static ssize_t rkbatt_restore_flag_attrs(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t size)
336 sscanf(buf, "%d", &liFlag);
340 battery_test_flag = liFlag;
344 static struct device_attribute rkbatt_attrs[] = {
345 __ATTR(state, 0664, rkbatt_show_state_attrs, rkbatt_restore_state_attrs),
346 __ATTR(debug, 0664, rkbatt_show_debug_attrs, rkbatt_restore_debug_attrs),
347 __ATTR(value, 0555, rkbatt_show_value_attrs, rkbatt_restore_value_attrs),
348 __ATTR(flag, 0555, rkbatt_show_flag_attrs, rkbatt_restore_flag_attrs),
351 static int rk_adc_battery_iio_read(struct rk30_adc_battery_platform_data *data)
353 struct iio_channel *channel = data->chan;
356 ret = iio_read_channel_raw(channel, &val);
358 pr_err("read channel() error: %d\n", ret);
365 static int create_sysfs_interfaces(struct device *dev)
368 for (liTmep = 0; liTmep < ARRAY_SIZE(rkbatt_attrs); liTmep++) {
370 if (device_create_file(dev, rkbatt_attrs + liTmep)){
378 for ( ; liTmep >= 0; liTmep--){
379 device_remove_file(dev, rkbatt_attrs + liTmep);
382 dev_err(dev, "%s:Unable to create sysfs interface\n", __func__);
388 static int rk30_adc_battery_load_capacity(void)
391 int* p = (int *)value;
392 long fd = sys_open(BATT_FILENAME,O_RDONLY,0);
395 DBG("rk30_adc_battery_load_capacity: open file /data/bat_last_capacity.dat failed\n");
399 sys_read(fd,(char __user *)value,4);
405 static void rk30_adc_battery_put_capacity(int loadcapacity)
408 int* p = (int *)value;
409 long fd = sys_open(BATT_FILENAME,O_CREAT | O_RDWR,0);
412 DBG("rk30_adc_battery_put_capacity: open file /data/bat_last_capacity.dat failed\n");
417 sys_write(fd, (const char __user *)value, 4);
420 static BLOCKING_NOTIFIER_HEAD(adc_battery_chain_head);
422 int register_adc_battery_notifier(struct notifier_block *nb)
424 return blocking_notifier_chain_register(&adc_battery_chain_head, nb);
426 EXPORT_SYMBOL_GPL(register_adc_battery_notifier);
428 int unregister_adc_battery_notifier(struct notifier_block *nb)
430 return blocking_notifier_chain_unregister(&adc_battery_chain_head, nb);
432 EXPORT_SYMBOL_GPL(unregister_adc_battery_notifier);
434 int adc_battery_notifier_call_chain(unsigned long val)
436 return (blocking_notifier_call_chain(&adc_battery_chain_head, val, NULL)
437 == NOTIFY_BAD) ? -EINVAL : 0;
439 EXPORT_SYMBOL_GPL(adc_battery_notifier_call_chain);
441 static void rk_start_charge(struct rk30_adc_battery_data *bat)
443 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
444 if (batt_gpio_is_valid(pdata->charge_set_pin))
445 gpio_direction_output(pdata->charge_set_pin, pdata->charge_set_level);
448 static void rk_stop_charge(struct rk30_adc_battery_data *bat)
450 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
453 if (batt_gpio_is_valid(pdata->charge_set_pin))
454 gpio_direction_output(pdata->charge_set_pin, 1 - pdata->charge_set_level);
457 static int get_ac_status(struct rk30_adc_battery_data *bat){
459 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
461 if (batt_gpio_is_valid(pdata->dc_det_pin)) {
462 if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level){
468 if(pdata->is_dc_charging){
469 status = pdata->is_dc_charging();
475 #if defined CONFIG_BATTERY_RK30_USB_CHARGE
477 //state of charge ----running
478 static int get_usb_status2(struct rk30_adc_battery_data *bat){
480 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
481 int usb_status = 0; // 0--dischage ,1 ---usb charge, 2 ---ac charge
482 int vbus_status = dwc_vbus_status();
484 if (1 == vbus_status) {
485 if (0 == get_gadget_connect_flag()){
486 if (++bat->gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES){
487 bat->gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
488 usb_status = 2; // non-standard AC charger
489 if(bat ->pdata ->control_usb_charging)
490 bat ->pdata ->control_usb_charging(1);
492 usb_status = 1; // connect to pc
493 if(bat ->pdata ->control_usb_charging)
494 bat ->pdata ->control_usb_charging(0);
498 usb_status = 1; // connect to pc
499 if(bat ->pdata ->control_usb_charging)
500 bat ->pdata ->control_usb_charging(0);
505 bat->gBatUsbChargeCnt = 0;
506 if (2 == vbus_status) {
507 usb_status = 2; //standard AC charger
509 if(bat ->pdata ->control_usb_charging)
510 bat ->pdata ->control_usb_charging(1);
521 static int rk_battery_get_status(struct rk30_adc_battery_data *bat)
524 /* struct rk30_adc_battery_platform_data *pdata = bat->pdata;*/
525 int ac_ac_charging = 0, usb_ac_charging = 0;
528 ac_ac_charging = get_ac_status(bat);
529 if(1 == ac_ac_charging)
532 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
533 if (strstr(saved_command_line,"charger")){
534 wake_lock(&charge_display_lock); //lock
535 if (batt_gpio_is_valid(bat->pdata->usb_det_pin)) {
536 if( gpio_get_value(bat->pdata->usb_det_pin)== bat->pdata->usb_det_level){
537 if(( 1 == usb_ac_charging )||( 1 == ac_ac_charging ))
538 bat -> ac_charging = 1;
539 if(( 1 == bat->usb_charging)||(1 == bat ->ac_charging))
543 if(( 0 == usb_ac_charging )&&( 0 == ac_ac_charging ))
544 bat -> ac_charging = 0;
546 bat->ac_charging = 1;
548 bat->usb_charging = 0;
549 if(1 == bat->ac_charging)
557 if(dwc_otg_check_dpdm() == 0){
558 bat->usb_charging = 0;
560 }else if(dwc_otg_check_dpdm() == 1){
561 bat->usb_charging = 1;
562 if(bat -> pdata ->control_usb_charging)
563 bat -> pdata ->control_usb_charging(0);
564 }else if(dwc_otg_check_dpdm() == 2){
565 bat->usb_charging = 0;
567 if(bat -> pdata ->control_usb_charging)
568 bat -> pdata ->control_usb_charging(1);
570 if((1 == usb_ac_charging)||(1 == ac_ac_charging))
571 bat ->ac_charging = 1;
573 bat ->ac_charging = 0;
575 if(( 0 == bat ->ac_charging )&&(0 == bat->usb_charging )){
586 usb_ac_charging = get_usb_status2(bat); //0 --discharge, 1---usb charging,2----AC charging;
587 if(1 == usb_ac_charging)
588 bat->usb_charging = 1;
590 bat->usb_charging = 0;
593 if((usb_ac_charging == 2)||(ac_ac_charging == 1))
594 bat -> ac_charging = 1;
596 bat -> ac_charging = 0;
598 if((bat->usb_charging == 1)||(bat ->ac_charging ==1))
601 if(1 == bat->ac_charging )
602 bat->charge_source_now = 1; //ac charge
603 else if( 1 == bat->usb_charging){
604 bat->charge_source_now = 2; //ac charge
606 bat->charge_soure_old =0;
607 bat->charge_source_now=0;
609 if(bat->charge_source_now != bat->charge_soure_old){
611 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){ //0.3 s
612 msleep(1); //mdelay --- > msleep
613 rk30_adc_battery_voltage_samples(bat); //get new voltage
616 bat->charge_soure_old = bat->charge_source_now;
619 DBG("ac_status=%d,usb_status=%d bat->bat_change = %d\n",bat -> ac_charging, bat->usb_charging ,bat->bat_change );
624 static int is_charge_ok(struct rk30_adc_battery_data *bat)
626 int charge_is_ok = 0;
627 struct rk30_adc_battery_platform_data *pdata = bat->pdata;
629 if( 1 != bat->charge_level)
632 if ((!(batt_gpio_is_valid(pdata->charge_ok_pin))) && (
633 pdata->charging_ok == NULL))
636 if (batt_gpio_is_valid(pdata->charge_ok_pin)) {
637 if (gpio_get_value(pdata->charge_ok_pin) == pdata->charge_ok_level){
640 }else if( pdata->charging_ok){
642 charge_is_ok = pdata->charging_ok();
650 static int rk30_adc_battery_status_samples(struct rk30_adc_battery_data *bat)
654 charge_level = bat ->charge_level;//rk_battery_get_status(bat);
656 if (charge_level != bat->old_charge_level){
657 bat->old_charge_level = charge_level;
661 rk_start_charge(bat);
667 bat->bat_status_cnt = 0;
669 if(( 1 == charge_level )&&(1 == bat->charge_full_flag) && (bat->bat_capacity < 90)){
670 rk_start_charge(bat); //recharge
671 if(bat->pdata->ctrl_charge_led != NULL)
672 bat->pdata->ctrl_charge_led(0);
674 }else if (charge_level) {
675 rk_start_charge(bat);
682 if(charge_level == 0){
684 bat->charge_full_flag = 0;
686 bat->time_to_full = 0;
687 bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
690 if( is_charge_ok(bat) == INVALID_CHARGE_CHECK){
691 if (bat->bat_capacity == 100){
692 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
693 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
695 bat->charge_full_flag = 1;
696 if(bat->pdata->ctrl_charge_led != NULL)
697 bat->pdata->ctrl_charge_led(1);
702 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
703 if((bat->bat_capacity == 99)&&(bat->time_to_full == 0)){
704 bat->time_to_full = get_seconds();
705 }else if(bat->bat_capacity == 99)
707 if(get_seconds() - bat->time_to_full > 1800){
708 bat->bat_capacity = 100;
709 bat->time_to_full = 0;
714 if(bat->pdata->ctrl_charge_led != NULL)
715 bat->pdata->ctrl_charge_led(0);
717 }else{ // pin of charge_ok_pin
718 if (is_charge_ok(bat) != CHARGE_IS_OK ){
721 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
723 if((bat->bat_capacity == 99)&&(bat->time_to_full == 0)){
724 bat->time_to_full = get_seconds();
725 }else if(bat->bat_capacity == 99)
727 if(get_seconds() - bat->time_to_full > 1800){
728 bat->bat_capacity = 100;
729 bat->time_to_full = 0;
738 if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) {
739 bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
742 if ((bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) && (bat->bat_capacity >= 99)){
743 if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){
744 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
746 bat->bat_capacity = 100;
751 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
759 static int rk_adc_voltage(struct rk30_adc_battery_data *bat, int value)
763 int ref_voltage; //reference_voltage
767 ref_voltage = bat ->pdata->reference_voltage;
768 pullup_res = bat ->pdata->pull_up_res;
769 pulldown_res = bat ->pdata->pull_down_res;
771 if(ref_voltage && pullup_res && pulldown_res){
772 #if defined(CONFIG_ARCH_RK2928) || defined(CONFIG_ARCH_RK3026)
773 ref_voltage = adc_get_curr_ref_volt();
775 voltage = ((value * ref_voltage * (pullup_res + pulldown_res)) / (1024 * pulldown_res));
776 DBG("ref_voltage =%d, voltage=%d \n", ref_voltage,voltage);
780 if(bat ->capacitytmp < 5)
781 ref_voltage = adc_get_curr_ref_volt();
783 ref_voltage = adc_get_def_ref_volt();
785 #if defined(CONFIG_ARCH_RK2928) || defined(CONFIG_ARCH_RK3026)
786 ref_voltage = adc_get_curr_ref_volt();
787 voltage = (value * ref_voltage * (batt_table[4] +batt_table[5])) / (1024 *batt_table[5]);
789 voltage = adc_to_voltage(value);
793 DBG("ref_voltage =%d, voltage=%d \n", ref_voltage,voltage);
797 static void rk_handle_ripple(struct rk30_adc_battery_data *bat, int status)
802 if (bat->pdata->use_board_table){
803 p_table = bat->pdata->board_batt_table;
806 if(bat->bat_voltage >= p_table[2*BATT_NUM +5]+ 10)
807 bat->bat_voltage = p_table[2*BATT_NUM +5] + 10;
808 else if(bat->bat_voltage <= p_table[BATT_NUM +6] - 10)
809 bat->bat_voltage = p_table[BATT_NUM +6] - 10;
812 if(bat->bat_voltage >= p_table[BATT_NUM +5]+ 10)
813 bat->bat_voltage = p_table[BATT_NUM +5] + 10;
814 else if(bat->bat_voltage <= p_table[6] - 10)
815 bat->bat_voltage = p_table[6] - 10;
821 //static int *pSamples;
822 static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat)
825 int i,*pStart = bat->adc_samples, num = 0;
826 int level = bat->charge_level;
829 value = bat->adc_val;
830 bat->adc_val = rk_adc_battery_iio_read(bat->pdata);
832 *(bat->pSamples++) = rk_adc_voltage(bat,value);
834 bat->bat_status_cnt++;
835 if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE) bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1;
837 num = bat->pSamples - pStart;
839 if (num >= NUM_VOLTAGE_SAMPLE){
840 bat ->pSamples = pStart;
841 num = NUM_VOLTAGE_SAMPLE;
846 for (i = 0; i < num; i++){
847 value += bat->adc_samples[i];
849 bat->bat_voltage = value / num;
852 if(battery_test_flag == 0)
854 if(0 == bat->pdata->use_board_table){
856 if(bat->bat_voltage >= batt_table[2*BATT_NUM +5]+ 10)
857 bat->bat_voltage = batt_table[2*BATT_NUM +5] + 10;
858 else if(bat->bat_voltage <= batt_table[BATT_NUM +6] - 10)
859 bat->bat_voltage = batt_table[BATT_NUM +6] - 10;
862 if(bat->bat_voltage >= batt_table[BATT_NUM +5]+ 10)
863 bat->bat_voltage = batt_table[BATT_NUM +5] + 10;
864 else if(bat->bat_voltage <= batt_table[6] - 10)
865 bat->bat_voltage = batt_table[6] - 10;
868 rk_handle_ripple(bat, level);
871 }else if(battery_test_flag == 2){
873 if(batt_table[3] == 0){
874 if(bat->bat_voltage < 3400){
875 if((get_seconds() - gSecondsCnt) > 30){
876 gSecondsCnt = get_seconds();
877 if((gVoltageCnt - bat->bat_voltage) > 15){
878 strncpy(gDischargeFlag, "off" ,4);
880 gVoltageCnt = bat->bat_voltage;
885 if(bat->bat_voltage < 3400){
886 bat->bat_voltage = 3400;
890 if(bat->bat_voltage < 6800){
891 if((get_seconds() - gSecondsCnt) > 30){
892 gSecondsCnt = get_seconds();
893 if((gDoubleVoltageCnt - bat->bat_voltage) > 30){
894 strncpy(gDischargeFlag, "off" ,4);
896 gDoubleVoltageCnt =bat->bat_voltage;
899 if(bat->bat_voltage < 6800){
900 bat->bat_voltage = 6800;
906 static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage)
913 if (bat->pdata->use_board_table)
914 p = bat->pdata->board_batt_table;
915 else if (bat->pdata->dts_batt_table)
916 p = bat->pdata->dts_batt_table;
921 if (1 == bat->charge_level){ //charge
922 if(0 == bat->start_voltage_status ){
923 if(BatVoltage >= (p[2*BATT_NUM +5])){
927 if(BatVoltage <= (p[BATT_NUM +6])){
931 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++){
933 if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){
935 capacity = (i-(BATT_NUM +6))*10 + ((BatVoltage - p[i]) * 10)/ (p[i+1]- p[i]);
944 DBG("start_voltage=%d,start_capacity =%d\n", bat->charge_start_voltage, bat->charge_start_capacity);
945 DBG("charge_down_proportion =%d,charge_up_proprotion=%d\n",bat ->charge_down_proportion,bat ->charge_up_proprotion);
946 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++)
947 if(((p[i]) <= bat->charge_start_voltage) && (bat->charge_start_voltage < (p[i+1])))
948 bat->voltage_to_local = i;
949 if(BatVoltage >= (p[2*BATT_NUM +5])){
953 if(BatVoltage <= (p[BATT_NUM +6])){
957 if(BatVoltage <bat->charge_start_voltage){
958 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++)
959 if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){
960 if( p[i+1] < bat->charge_start_voltage ){
961 capacity =bat->charge_start_capacity - ((p[i+1] -BatVoltage) * bat->charge_start_capacity/(bat->voltage_to_local -17+1))/ (p[i+1]- p[i]) - (bat->voltage_to_local- (i ))*bat->charge_start_capacity/(bat->voltage_to_local -17+1);
962 DBG("1<<<<<<< %d bat->voltage_to_local =%d capacity = %d BatVoltage =%d p[i] = %d,p[i+1] = %d \n", i, bat->voltage_to_local,capacity,BatVoltage,p[i], p[i+1]);
966 capacity =bat->charge_start_capacity - ((bat->charge_start_voltage -BatVoltage) * bat->charge_start_capacity/(bat->voltage_to_local -17+1) )/ (bat->charge_start_voltage - p[i]);
967 DBG("2<<<<<< %d capacity = %d BatVoltage =%d p[i] = %d,p[i+1] = %d \n", i,capacity,BatVoltage,p[i], p[i+1]);
975 if(BatVoltage > bat->charge_start_voltage){
976 for(i = BATT_NUM +6; i <2*BATT_NUM +5; i++)
977 if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){
978 if( p[i] > bat->charge_start_voltage ){
979 capacity = bat->charge_start_capacity + (i +1- (bat->voltage_to_local))*(100- bat->charge_start_capacity )/( 10 - (bat->voltage_to_local - 17)) + (BatVoltage - p[i]) * (100- bat->charge_start_capacity )/( 10 - (bat->voltage_to_local -17))/ (p[i+1]- p[i]);
980 DBG("3<<<<<<<< %d bat->voltage_to_local =%d capacity = %d BatVoltage =%d p[i] = %d,p[i+1] = %d \n", i, bat->voltage_to_local,capacity,BatVoltage,p[i], p[i+1]);
983 capacity = bat->charge_start_capacity + (BatVoltage - bat->charge_start_voltage) * (100- bat->charge_start_capacity )/( 10 - (bat->voltage_to_local-17 )) /(p[i+1] - bat->charge_start_voltage );
984 DBG(" 4<<<<<<<<<%d bat->voltage_to_local =%d capacity = %d BatVoltage =%d p[i] = %d,p[i+1] = %d \n", i,bat->voltage_to_local,capacity,BatVoltage,p[i], p[i+1]);
991 if(BatVoltage == bat->charge_start_voltage)
992 capacity = bat ->charge_start_capacity;
1002 if(BatVoltage >= (p[BATT_NUM +5])){
1006 if(BatVoltage <= (p[6])){
1010 for(i = 6; i < BATT_NUM +5; i++){
1011 if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){
1012 capacity = (i-6)*10+ ((BatVoltage - p[i]) *10 )/ (p[i+1]- p[i]) ;
1023 DBG("real_voltage_to_capacity =%d\n" ,capacity);
1027 #if defined CONFIG_BATTERY_RK30_USB_CHARGE
1029 static void rk_usb_charger(struct rk30_adc_battery_data *bat)
1032 int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
1033 int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
1035 if((1 == bat ->charge_level)&&( 0 == bat ->start_voltage_status)){
1036 bat ->charge_start_voltage = bat ->bat_voltage;
1037 bat ->start_voltage_status = 1;
1038 bat ->charge_start_capacity = bat ->bat_capacity;
1039 if(bat ->charge_start_capacity%10 != 0){
1040 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
1041 bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
1043 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10;
1044 bat ->charge_down_proportion = bat ->charge_start_capacity/10;
1050 capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1052 if (capacity > bat->bat_capacity){
1053 if(capacity > bat->bat_capacity + 10 )
1054 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10; //5s
1055 else if(capacity > bat->bat_capacity + 7 )
1056 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
1057 else if(capacity > bat->bat_capacity + 3 )
1058 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
1059 if (++(bat->gBatCapacityusbChargeCnt) >= timer_of_charge_sample){
1060 bat->gBatCapacityusbChargeCnt = 0;
1061 if (bat->bat_capacity < 99){
1062 bat->bat_capacity++;
1063 bat->bat_change = 1;
1066 bat->gBatCapacityChargeCnt = 0;
1067 bat ->gBatCapacityusbdisChargeCnt = 0;//get_suspend_state(void)
1068 }else //if(( get_suspend_state() != PM_SUSPEND_MEM)&&(capacity < bat->bat_capacity)){
1069 // if((gpio_get_value (bat->pdata->back_light_pin) == 1)&&(capacity < bat->bat_capacity)){
1070 if((capacity < bat->bat_capacity)){
1071 DBG("USB CHARGE DOWN\n");
1073 // if (capacity < bat->bat_capacity){
1075 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 40; // 13
1076 }else if(capacity < 20){
1077 if(capacity + 3 > bat->bat_capacity )
1078 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5; //5s
1079 else if(capacity + 7 > bat->bat_capacity )
1080 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
1081 else if(capacity + 10> bat->bat_capacity )
1082 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -25; // 13
1084 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 35; // 13
1086 if(capacity + 3 > bat->bat_capacity )
1087 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -5; //5s
1088 else if(capacity + 7 > bat->bat_capacity )
1089 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE -10; //10s
1090 else if(capacity + 10> bat->bat_capacity )
1091 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 15; // 13
1093 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 20; // 13
1096 if (++(bat->gBatCapacityusbdisChargeCnt) >= timer_of_discharge_sample){
1097 bat->gBatCapacityusbdisChargeCnt = 0;
1098 if (bat->bat_capacity > 0){
1099 bat->bat_capacity-- ;
1100 bat->bat_change = 1;
1105 bat->gBatCapacityusbChargeCnt = 0;
1108 else //if(get_suspend_state() == PM_SUSPEND_MEM){
1109 //if(gpio_get_value (bat->pdata->back_light_pin) == 0){
1113 bat->gBatCapacityusbdisChargeCnt = 0;
1114 // (bat->gBatCapacityusbChargeCnt)++;
1115 if( is_charge_ok(bat) != INVALID_CHARGE_CHECK){
1116 if( is_charge_ok(bat) == CHARGE_IS_OK){
1118 if (++bat->gBatCapacityusbChargeCnt >= timer_of_charge_sample-30){
1119 bat->gBatCapacityusbChargeCnt = 0;
1120 if (bat->bat_capacity <= 99){
1121 bat->bat_capacity++;
1122 bat->bat_change = 1;
1126 if (capacity > bat->capacitytmp){
1127 bat->gBatCapacityChargeCnt = 0;
1130 if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
1131 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1133 if (bat->bat_capacity < 99){
1134 bat->bat_capacity++;
1135 bat->bat_change = 1;
1143 if (capacity > bat->capacitytmp){
1144 bat->gBatCapacityChargeCnt = 0;
1146 if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityusbChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
1147 bat->gBatCapacityusbChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1148 if (bat->bat_capacity <= 99){
1149 bat->bat_capacity++;
1150 bat->bat_change = 1;
1157 bat->capacitytmp = capacity;
1161 static void rk_ac_charger(struct rk30_adc_battery_data *bat)
1164 int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
1166 if((1 == bat->charge_level)&&( 0 == bat->start_voltage_status)){
1167 bat->charge_start_voltage = bat->bat_voltage;
1168 bat->start_voltage_status = 1;
1169 bat->charge_start_capacity = bat->bat_capacity;
1170 if(bat ->charge_start_capacity%10 != 0){
1171 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
1172 bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
1174 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10;
1175 bat ->charge_down_proportion = bat ->charge_start_capacity/10;
1180 capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1181 if (capacity > bat->bat_capacity){
1182 if(capacity > bat->bat_capacity + 10 )
1183 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -10; //5s
1184 else if(capacity > bat->bat_capacity + 7 )
1185 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE -5; //10s
1186 else if(capacity > bat->bat_capacity + 3 )
1187 timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE - 2; // 13
1188 if (++(bat->gBatCapacityacChargeCnt) >= timer_of_charge_sample){
1189 bat->gBatCapacityacChargeCnt = 0;
1190 if (bat->bat_capacity < 99){
1191 bat->bat_capacity++;
1192 bat->bat_change = 1;
1195 bat->gBatCapacityChargeCnt = 0;
1198 bat->gBatCapacityacChargeCnt = 0;
1199 (bat->gBatCapacityChargeCnt)++;
1200 if( is_charge_ok(bat) != INVALID_CHARGE_CHECK){
1201 if( is_charge_ok(bat) == CHARGE_IS_OK){
1202 if (bat->gBatCapacityChargeCnt >= timer_of_charge_sample){
1203 bat->gBatCapacityChargeCnt = 0;
1204 if (bat->bat_capacity < 99){
1205 bat->bat_capacity++;
1206 bat->bat_change = 1;
1210 if (capacity > bat->capacitytmp){
1211 bat->gBatCapacityChargeCnt = 0;
1215 if ((bat->bat_capacity >= 85) &&((bat->gBatCapacityChargeCnt) > NUM_CHARGE_MAX_SAMPLE)){
1216 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1218 if (bat->bat_capacity < 99){
1219 bat->bat_capacity++;
1220 bat->bat_change = 1;
1227 if (capacity > bat->capacitytmp){
1228 bat->gBatCapacityChargeCnt = 0;
1232 if ((bat->bat_capacity >= 85) &&(bat->gBatCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)){
1233 bat->gBatCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
1235 if (bat->bat_capacity <= 99){
1236 bat->bat_capacity++;
1237 bat->bat_change = 1;
1245 bat->capacitytmp = capacity;
1247 static void rk_battery_charger(struct rk30_adc_battery_data *bat)
1251 int timer_of_discharge_sample = DISCHARGE_MIN_SECOND;
1253 capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1255 if (capacity < bat->bat_capacity){
1258 timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE - 40; // 13
1262 if(capacity + 3 > bat->bat_capacity )
1263 timer_of_discharge_sample = DISCHARGE_MIN_SECOND -5; //5s
1264 else if(capacity + 7 > bat->bat_capacity )
1265 timer_of_discharge_sample = DISCHARGE_MIN_SECOND -10; //10s
1266 else if(capacity + 10> bat->bat_capacity )
1267 timer_of_discharge_sample = DISCHARGE_MIN_SECOND -25; // 13
1269 timer_of_discharge_sample = DISCHARGE_MIN_SECOND - 35; // 13
1271 if(capacity + 3 > bat->bat_capacity )
1272 timer_of_discharge_sample = DISCHARGE_MIN_SECOND -5; //5s
1273 else if(capacity + 7 > bat->bat_capacity )
1274 timer_of_discharge_sample = DISCHARGE_MIN_SECOND -10; //10s
1275 else if(capacity + 10> bat->bat_capacity )
1276 timer_of_discharge_sample = DISCHARGE_MIN_SECOND - 15; // 13
1278 timer_of_discharge_sample = DISCHARGE_MIN_SECOND - 20; // 13
1281 if (++(bat->gBatCapacityDisChargeCnt) >= timer_of_discharge_sample){
1282 bat->gBatCapacityDisChargeCnt = 0;
1283 if (bat->bat_capacity > 0){
1284 bat->bat_capacity-- ;
1285 bat->bat_change = 1;
1290 bat->gBatCapacityDisChargeCnt = 0;
1292 bat->gBatCapacityChargeCnt = 0;
1293 bat->gBatCapacityusbdisChargeCnt=0 ;
1294 bat->gBatCapacityusbChargeCnt =0;
1295 bat->gBatCapacityacChargeCnt = 0;
1296 bat->start_voltage_status = 0;
1298 bat->capacitytmp = capacity;
1304 static void rk30_adc_battery_capacity_samples(struct rk30_adc_battery_data *bat)
1306 // int capacity = 0;
1307 // int timer_of_charge_sample = NUM_CHARGE_MIN_SAMPLE;
1308 // int timer_of_discharge_sample = NUM_CHARGE_MIN_SAMPLE;
1310 if (bat->bat_status_cnt < NUM_VOLTAGE_SAMPLE) {
1311 bat->gBatCapacityDisChargeCnt = 0;
1312 bat->gBatCapacityChargeCnt = 0;
1313 bat->gBatCapacityacChargeCnt = 0;
1317 if(1 == bat->charge_level){
1318 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1319 if(1 == bat->usb_charging)
1320 rk_usb_charger(bat);
1327 rk_battery_charger(bat);
1333 //static int poweron_check = 0;
1334 static void rk30_adc_battery_poweron_capacity_check(struct rk30_adc_battery_data *bat)
1337 int new_capacity, old_capacity;
1340 new_capacity = bat ->bat_capacity;
1343 old_capacity = rk30_adc_battery_load_capacity();
1344 if( old_capacity >= 0 ){
1350 if ((old_capacity < 0) || (old_capacity > 100)){
1351 old_capacity = new_capacity;
1354 if (bat ->bat_status == POWER_SUPPLY_STATUS_FULL){
1355 if (new_capacity > 80){
1356 bat ->bat_capacity = 100;
1359 else if (bat ->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
1362 if( bat ->pdata->is_reboot_charging == 1)
1363 bat ->bat_capacity = (old_capacity < 10) ?(old_capacity+2):old_capacity;
1365 bat ->bat_capacity = (new_capacity > old_capacity) ? new_capacity : old_capacity;
1367 if(new_capacity > old_capacity + 50 )
1368 bat ->bat_capacity = old_capacity + 5;
1370 bat ->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; //avoid the value of capacity increase
1371 if(bat->bat_capacity == 100)
1372 bat->bat_capacity = 99;
1373 if(bat->bat_capacity == 0)
1374 bat->bat_capacity =1;
1376 DBG("oldcapacity %d,newcapacity %d,capacity %d\n", old_capacity
1377 , new_capacity, bat->bat_capacity);
1379 bat ->bat_change = 1;
1381 #if defined CONFIG_BATTERY_RK30_USB_CHARGE
1382 #define to_battery_usb_device_info(x) container_of((x), \
1383 struct rk30_adc_battery_data, usb);
1385 static int rk30_adc_battery_get_usb_property(struct power_supply *psy,
1386 enum power_supply_property psp,
1387 union power_supply_propval *val)
1389 struct rk30_adc_battery_data *bat= to_battery_usb_device_info(psy);
1392 case POWER_SUPPLY_PROP_ONLINE:
1393 if (psy->type == POWER_SUPPLY_TYPE_USB){
1394 val->intval = bat ->usb_charging;
1395 if (strstr(saved_command_line,"charger") == NULL){
1396 if( 1 == bat->charge_full_flag)
1410 static enum power_supply_property rk30_adc_battery_usb_props[] = {
1412 POWER_SUPPLY_PROP_ONLINE,
1415 static struct power_supply rk30_usb_supply =
1418 .type = POWER_SUPPLY_TYPE_USB,
1420 .get_property = rk30_adc_battery_get_usb_property,
1422 .properties = rk30_adc_battery_usb_props,
1423 .num_properties = ARRAY_SIZE(rk30_adc_battery_usb_props),
1426 static irqreturn_t rk30_adc_battery_dc_wakeup(int irq, void *dev_id)
1428 disable_irq_nosync(irq);
1429 queue_work(gBatteryData->wq, &gBatteryData->dcwakeup_work);
1433 #define to_battery_ac_device_info(x) container_of((x), \
1434 struct rk30_adc_battery_data, ac);
1436 static int rk30_adc_battery_get_ac_property(struct power_supply *psy,
1437 enum power_supply_property psp,
1438 union power_supply_propval *val)
1442 struct rk30_adc_battery_data *bat = to_battery_ac_device_info(psy);
1445 case POWER_SUPPLY_PROP_ONLINE:
1446 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
1447 val->intval = bat ->ac_charging;
1448 if (strstr(saved_command_line,"charger") == NULL ){
1449 if( 1 == bat->charge_full_flag)
1463 static enum power_supply_property rk30_adc_battery_ac_props[] =
1465 POWER_SUPPLY_PROP_ONLINE,
1468 static struct power_supply rk30_ac_supply =
1471 .type = POWER_SUPPLY_TYPE_MAINS,
1473 .get_property = rk30_adc_battery_get_ac_property,
1475 .properties = rk30_adc_battery_ac_props,
1476 .num_properties = ARRAY_SIZE(rk30_adc_battery_ac_props),
1478 static void rk30_adc_battery_dcdet_delaywork(struct work_struct *work)
1482 struct rk30_adc_battery_platform_data *pdata;
1485 struct rk30_adc_battery_data *bat = container_of((work), \
1486 struct rk30_adc_battery_data, dcwakeup_work);
1489 irq = gpio_to_irq(pdata->dc_det_pin);
1490 free_irq(irq, NULL);
1492 irq_flag = gpio_get_value (pdata->dc_det_pin) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1493 ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);// reinitialize the DC irq
1495 free_irq(irq, NULL);
1498 power_supply_changed(&bat ->ac);
1499 rk_send_wakeup_key();
1501 bat ->bat_status_cnt = 0; //the state of battery is change
1506 static int rk30_adc_battery_get_status(struct rk30_adc_battery_data *bat)
1508 return (bat->bat_status);
1511 static int rk30_adc_battery_get_health(struct rk30_adc_battery_data *bat)
1513 return POWER_SUPPLY_HEALTH_GOOD;
1516 static int rk30_adc_battery_get_present(struct rk30_adc_battery_data *bat)
1518 return (bat->bat_voltage < BATT_MAX_VOL_VALUE) ? 0 : 1;
1521 static int rk30_adc_battery_get_voltage(struct rk30_adc_battery_data *bat)
1523 return (bat->bat_voltage );
1526 static int rk30_adc_battery_get_capacity(struct rk30_adc_battery_data *bat)
1528 return (bat->bat_capacity);
1531 static int rk30_adc_battery_get_property(struct power_supply *psy,
1532 enum power_supply_property psp,
1533 union power_supply_propval *val)
1537 struct rk30_adc_battery_data *bat = container_of((psy), \
1538 struct rk30_adc_battery_data, bat);
1540 case POWER_SUPPLY_PROP_STATUS:
1541 val->intval = rk30_adc_battery_get_status(bat);
1542 DBG("gBatStatus=%d\n",val->intval);
1544 case POWER_SUPPLY_PROP_HEALTH:
1545 val->intval = rk30_adc_battery_get_health(bat);
1546 DBG("gBatHealth=%d\n",val->intval);
1548 case POWER_SUPPLY_PROP_PRESENT:
1549 val->intval = rk30_adc_battery_get_present(bat);
1550 DBG("gBatPresent=%d\n",val->intval);
1552 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1553 // val ->intval = rk30_adc_battery_get_voltage(bat);
1554 voltage = rk30_adc_battery_get_voltage(bat);
1555 val->intval = voltage*1000;
1556 DBG("gBatVoltage=%d\n",val->intval);
1558 case POWER_SUPPLY_PROP_CAPACITY:
1559 if(battery_test_flag == 2)
1562 val->intval = rk30_adc_battery_get_capacity(bat);
1563 DBG("gBatCapacity=%d%%\n",val->intval);
1565 case POWER_SUPPLY_PROP_TECHNOLOGY:
1566 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1568 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1569 val->intval = BATT_MAX_VOL_VALUE;
1571 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1572 val->intval = BATT_ZERO_VOL_VALUE;
1582 static enum power_supply_property rk30_adc_battery_props[] = {
1584 POWER_SUPPLY_PROP_STATUS,
1585 POWER_SUPPLY_PROP_HEALTH,
1586 POWER_SUPPLY_PROP_PRESENT,
1587 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1588 POWER_SUPPLY_PROP_TECHNOLOGY,
1589 POWER_SUPPLY_PROP_CAPACITY,
1590 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1591 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1594 static struct power_supply rk30_battery_supply =
1597 .type = POWER_SUPPLY_TYPE_BATTERY,
1599 .get_property = rk30_adc_battery_get_property,
1601 .properties = rk30_adc_battery_props,
1602 .num_properties = ARRAY_SIZE(rk30_adc_battery_props),
1606 static void rk30_adc_battery_resume_check(struct rk30_adc_battery_data *bat)
1610 int new_capacity, old_capacity;
1611 // struct rk30_adc_battery_data *bat = gBatteryData;
1613 bat ->old_charge_level = -1;
1614 bat ->pSamples = bat->adc_samples;
1616 bat->adc_val = rk_adc_battery_iio_read(bat->pdata);
1618 level = oldlevel = rk_battery_get_status(bat);//rk30_adc_battery_status_samples(bat);//init charge status
1620 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++) { //0.3 s
1623 rk30_adc_battery_voltage_samples(bat); //get voltage
1624 level = rk_battery_get_status(bat);// rk30_adc_battery_status_samples(bat); //check charge status
1625 if (oldlevel != level){
1626 oldlevel = level; //if charge status changed, reset sample
1630 new_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage);
1631 old_capacity =bat-> suspend_capacity;
1633 //if (bat->bat_status != POWER_SUPPLY_STATUS_NOT_CHARGING){
1636 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity;
1639 bat->bat_capacity = (new_capacity < old_capacity) ? new_capacity : old_capacity; // aviod the value of capacity increase dicharge
1642 if (batt_gpio_is_valid(bat->pdata->batt_low_pin)) {
1643 if (gpio_get_value(bat ->pdata->batt_low_pin) == bat ->pdata->batt_low_level)
1644 bat->bat_capacity = 0;
1650 static int rk30_adc_battery_suspend(struct platform_device *dev, pm_message_t state)
1653 struct rk30_adc_battery_data *data = platform_get_drvdata(dev);
1655 data ->suspend_capacity = data->bat_capacity;
1656 data ->suspend_time = get_seconds();
1657 cancel_delayed_work(&data ->delay_work);
1659 if (batt_gpio_is_valid(data->pdata->batt_low_pin)) {
1661 irq = gpio_to_irq(data ->pdata->batt_low_pin);
1663 enable_irq_wake(irq);
1669 static int rk30_adc_battery_resume(struct platform_device *dev)
1672 struct rk30_adc_battery_data *data = platform_get_drvdata(dev);
1673 if( data->bat_capacity < 10 ){
1674 wake_lock_timeout(&batt_wake_lock_detect_lowpower,15*HZ);
1675 data->bat_change = 1;
1677 data ->resume_time = get_seconds();
1678 data ->resume = true;
1679 queue_delayed_work(data->wq, &data ->delay_work, msecs_to_jiffies(100));
1680 if (batt_gpio_is_valid(data->pdata->batt_low_pin)) {
1682 irq = gpio_to_irq(data ->pdata ->batt_low_pin);
1683 disable_irq_wake(irq);
1689 #define rk30_adc_battery_suspend NULL
1690 #define rk30_adc_battery_resume NULL
1694 unsigned long AdcTestCnt = 0;
1695 static void rk30_adc_battery_timer_work(struct work_struct *work)
1697 struct rk30_adc_battery_data *bat = container_of((work), struct
1698 rk30_adc_battery_data, delay_work.work);
1702 // if( (bat->resume_time - bat->suspend_time) >= 1800 )
1703 rk30_adc_battery_resume_check(bat);
1705 //bat->bat_capacity = bat->suspend_capacity;
1706 bat ->resume = false;
1707 bat ->bat_change =1;
1710 bat->stop_check = 1;
1711 if( 1 == bat ->lower_power_flag ){
1712 bat ->bat_capacity = 0;
1713 bat ->bat_change =1;
1715 if (bat ->poweron_check){
1716 bat ->poweron_check = 0;
1717 rk30_adc_battery_poweron_capacity_check(bat);
1720 bat ->charge_level = rk_battery_get_status(bat);
1721 DBG("bat ->charge_level =%d\n", bat ->charge_level);
1722 rk30_adc_battery_status_samples(bat);
1723 rk30_adc_battery_voltage_samples(bat);
1724 rk30_adc_battery_capacity_samples(bat);
1726 if( 0 == bat ->pdata ->charging_sleep){
1727 if( 1 == bat->charge_level){ // charge
1728 if(0 == bat->status_lock ){
1729 wake_lock(&batt_wake_lock); //lock
1730 bat ->status_lock = 1;
1734 if(1 == bat ->status_lock ){
1735 wake_unlock(&batt_wake_lock); //unlock
1736 bat ->status_lock = 0;
1742 /*update battery parameter after adc and capacity has been changed*/
1743 if(bat ->bat_change){
1744 bat ->bat_change= 0;
1745 if (strstr(saved_command_line,"charger") == NULL){ //when low charging-current,not in charging-displays
1746 if(0 == bat ->bat_capacity){
1747 bat ->ac_charging = 0;
1748 bat ->usb_charging = 0;
1752 rk30_adc_battery_put_capacity(bat ->bat_capacity);
1753 power_supply_changed(&bat ->bat);
1754 power_supply_changed(&bat ->ac);
1755 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1756 power_supply_changed(&bat ->usb);
1761 //if (rk30_battery_dbg_level){
1762 if (++AdcTestCnt >= 2)
1766 DBG("Status = %d, RealAdcVal = %d, RealVol = %d,gBatVol = %d, gBatCap = %d, RealCapacity = %d, batt_dischargecnt = %d\n, chargecnt = %d,ac_count = %d, usb_count =%d ,usb_dischargecount =%d\n",
1767 bat ->bat_status, bat ->adc_val, rk_adc_voltage(bat, bat ->adc_val),
1768 bat ->bat_voltage, bat ->bat_capacity, bat ->capacitytmp, bat ->gBatCapacityDisChargeCnt, bat ->gBatCapacityChargeCnt,
1769 bat ->gBatCapacityacChargeCnt, bat ->gBatCapacityusbChargeCnt, bat ->gBatCapacityusbdisChargeCnt);
1773 queue_delayed_work(bat ->wq, &bat ->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
1778 static int rk30_adc_battery_io_init(struct rk30_adc_battery_platform_data *pdata)
1782 if (batt_gpio_is_valid(pdata->dc_det_pin)) {
1783 ret = gpio_request(pdata->dc_det_pin, NULL);
1785 pr_info("failed to request dc_det gpio\n");
1789 ret = gpio_direction_input(pdata->dc_det_pin);
1791 pr_info("failed to set gpio dc_det input\n");
1797 if (batt_gpio_is_valid(pdata->charge_ok_pin)) {
1798 ret = gpio_request(pdata->charge_ok_pin, NULL);
1800 pr_err("failed to request charge_ok gpio\n");
1804 ret = gpio_direction_input(pdata->charge_ok_pin);
1806 pr_err("failed to set gpio charge_ok input\n");
1811 if (batt_gpio_is_valid(pdata->batt_low_pin)) {
1812 ret = gpio_request(pdata->batt_low_pin, NULL);
1814 pr_err("failed to request batt_low_pin gpio\n");
1818 ret = gpio_direction_input(pdata->batt_low_pin);
1820 pr_err("failed to set gpio batt_low_pin input\n");
1830 extern void kernel_power_off(void);
1831 int get_battery_status(void)
1833 return system_lowerpower;
1835 static int rk_adc_battery_poweron_status(struct rk30_adc_battery_data *bat)
1838 #if defined CONFIG_BATTERY_RK30_USB_CHARGE
1842 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1844 if (get_ac_status(bat) ){
1845 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1846 bat -> ac_charging = 1;
1847 if (is_charge_ok(bat) == 1){
1848 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1849 bat->bat_capacity = 100;
1851 power_supply_changed(&bat ->ac);
1854 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1855 otg_status = dwc_otg_check_dpdm();
1856 if( 0 != otg_status ){
1857 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1858 if (is_charge_ok(bat) == 1){
1859 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1860 bat->bat_capacity = 100;
1864 if(otg_status == 1){
1865 bat->usb_charging = 1;
1866 if(bat -> pdata ->control_usb_charging)
1867 bat -> pdata ->control_usb_charging(0);
1868 }else if(otg_status == 2){
1869 bat->usb_charging = 0;
1870 bat -> ac_charging = 1;
1871 if(bat -> pdata ->control_usb_charging)
1872 bat -> pdata ->control_usb_charging(1);
1874 bat->usb_charging = 0;
1876 printk("charge_status = %d\n",otg_status);
1878 power_supply_changed(&bat ->ac);
1879 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1880 power_supply_changed(&bat ->usb);
1882 if((bat -> ac_charging == 1)||(bat->usb_charging == 1)){
1883 bat ->old_charge_level =1;
1884 bat->charge_level = 1;
1887 bat ->old_charge_level =0;
1888 bat->charge_level = 0;
1895 static void rk30_adc_battery_check(struct rk30_adc_battery_data *bat)
1899 // struct rk30_adc_battery_platform_data *pdata = bat->pdata;
1900 int check_data[NUM_VOLTAGE_SAMPLE];
1901 //bat ->old_charge_level
1902 // pSamples = bat->adc_samples;
1905 bat->adc_val = rk_adc_battery_iio_read(bat->pdata);
1907 DBG("first_adc_value is %d\n", bat->adc_val);
1908 level = oldlevel = rk_adc_battery_poweron_status(bat);// rk30_adc_battery_status_samples(bat);//init charge status
1910 for (i = 0; i < NUM_VOLTAGE_SAMPLE; i++){ //0.3 s
1912 rk30_adc_battery_voltage_samples(bat); //get voltage
1913 //level = rk30_adc_battery_status_samples(bat); //check charge status
1915 level = rk_adc_battery_poweron_status(bat);
1917 if (oldlevel != level){
1918 oldlevel = level; //if charge status changed, reset sample
1923 for(i=0; i< NUM_VOLTAGE_SAMPLE; i++){
1925 check_data[i] = bat->adc_val;
1927 bat->adc_val = rk_adc_battery_iio_read(bat->pdata);
1928 bat->adc_value += check_data[i];
1930 bat->adc_value /= NUM_VOLTAGE_SAMPLE;
1932 bat->bat_capacity = rk30_adc_battery_voltage_to_capacity(bat, bat->bat_voltage); //init bat_capacity
1933 bat->capacitytmp = bat->bat_capacity;
1934 level = rk_adc_battery_poweron_status(bat);
1935 if((1 == level)&&(0 == bat->start_voltage_status )){
1936 bat->charge_start_voltage = bat->bat_voltage;
1937 bat->start_voltage_status = 1;
1938 bat->charge_start_capacity = bat->bat_capacity;
1939 bat ->charge_up_proprotion = (100 - bat ->charge_start_capacity)/10+1;
1940 bat ->charge_down_proportion = bat ->charge_start_capacity/10+1;
1943 if (get_ac_status(bat) ){
1944 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1945 bat -> ac_charging = 1;
1946 if (is_charge_ok(bat) == 1){
1947 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1948 bat->bat_capacity = 100;
1951 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1952 if( 0 != dwc_otg_check_dpdm() ){
1953 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
1954 if (is_charge_ok(bat) == 1){
1955 bat->bat_status = POWER_SUPPLY_STATUS_FULL;
1956 bat->bat_capacity = 100;
1960 if(dwc_otg_check_dpdm() == 1){
1961 bat->usb_charging = 1;
1962 if(bat -> pdata ->control_usb_charging)
1963 bat -> pdata ->control_usb_charging(0);
1964 }else if(dwc_otg_check_dpdm() == 2){
1965 bat->usb_charging = 0;
1966 bat -> ac_charging = 1;
1967 if(bat -> pdata ->control_usb_charging)
1968 bat -> pdata ->control_usb_charging(1);
1970 bat->usb_charging = 0;
1973 power_supply_changed(&bat ->ac);
1974 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
1975 power_supply_changed(&bat ->usb);
1977 if((bat -> ac_charging == 1)||(bat->usb_charging == 1)){
1978 bat ->old_charge_level =1;
1979 bat->charge_level = 1;
1984 if (bat->bat_capacity == 0) {
1985 bat->bat_capacity = 1;
1986 system_lowerpower = 1;
1990 if(0 !=bat ->pdata->low_voltage_protection ){
1991 if (bat->bat_voltage <= bat->pdata->low_voltage_protection) {
1992 system_lowerpower = 1;
1993 printk("protection lower power .....\n");
1996 if (bat->bat_voltage <= BATT_ZERO_VOL_VALUE) {
1997 system_lowerpower = 1;
1998 pr_info("lower power bat->bat_voltage = %d\n"
1999 , bat->bat_voltage);
2004 if ((bat->bat_voltage <= BATT_ZERO_VOL_VALUE)&&(bat->bat_status != POWER_SUPPLY_STATUS_CHARGING)){
2011 static void rk30_adc_battery_lowerpower_delaywork(struct work_struct *work)
2015 struct rk30_adc_battery_data *bat = container_of((work), \
2016 struct rk30_adc_battery_data, lowerpower_work);
2018 if (batt_gpio_is_valid(bat->pdata->batt_low_pin)) {
2019 irq = gpio_to_irq(bat ->pdata ->batt_low_pin);
2023 printk("lowerpower\n");
2028 static irqreturn_t rk30_adc_battery_low_wakeup(int irq,void *dev_id)
2030 queue_work(gBatteryData->wq, &gBatteryData->lowerpower_work);
2033 static void rk_lowerpower_check(struct rk30_adc_battery_data *bat)
2037 int check_data[NUM_VOLTAGE_SAMPLE];
2039 for(i=0; i< NUM_VOLTAGE_SAMPLE; i++){
2041 bat->adc_val = rk_adc_battery_iio_read(bat->pdata);
2042 check_data[i] = bat->adc_val;
2044 adc_val += check_data[i];
2046 adc_val /=NUM_VOLTAGE_SAMPLE;
2048 DBG(">>>>>>>>>>>one>>>%d, two<<<<<%d<<<<\n",bat->adc_value,adc_val);
2049 DBG(">>>>>>>>>>>firset-value>>>%d, second-value<<<<<%d<<<<\n",rk_adc_voltage(bat, bat->adc_value),rk_adc_voltage(bat, adc_val));
2051 if((adc_val >= bat->adc_value+5) &&(bat->bat_status == POWER_SUPPLY_STATUS_NOT_CHARGING ) )//
2053 printk("%d,%d\n",adc_val,bat->adc_value);
2054 printk("lower-power shutdown");
2059 static void rk_adc_battery_check_work(struct work_struct *work)
2061 struct rk30_adc_battery_data *bat =
2062 container_of((work), struct
2063 rk30_adc_battery_data, check_work.work);
2065 if(1 == get_ac_status(bat)){
2066 bat->bat_status = POWER_SUPPLY_STATUS_CHARGING;
2067 bat -> ac_charging = 1;
2070 power_supply_changed(&bat ->ac);
2071 if(bat->stop_check != 1)
2072 queue_delayed_work(bat ->wq, &bat ->check_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2075 static void poweron_lowerpoer_handle(struct rk30_adc_battery_data *bat)
2077 #ifdef CONFIG_LOGO_LOWERPOWER_WARNING
2078 if((1 == get_battery_status())&&(bat->bat_status == POWER_SUPPLY_STATUS_NOT_CHARGING )){
2085 static int battery_notifier_call(struct notifier_block *nb,
2086 unsigned long event, void *data)
2088 struct rk30_adc_battery_data *bat=
2089 container_of(nb, struct rk30_adc_battery_data, battery_nb);
2093 rk_lowerpower_check(bat);
2097 poweron_lowerpoer_handle(bat);
2106 static int rk31_adcbat_parse_dt(struct platform_device *pdev, struct
2107 rk30_adc_battery_platform_data *data)
2109 struct device_node *node = pdev->dev.of_node;
2110 enum of_gpio_flags flags;
2111 struct property *prop;
2117 struct iio_channel *chan;
2122 chan = iio_channel_get(&pdev->dev, NULL);
2124 pr_err("iio_channel_get fail\n");
2127 /* determine the number of config info */
2128 prop = of_find_property(node, "bat_table", &length);
2132 length /= sizeof(u32);
2135 size = (sizeof(*data->dts_batt_table)) * length;
2136 data->dts_batt_table =
2137 devm_kzalloc(&(pdev->dev), size, GFP_KERNEL);
2138 if (!data->dts_batt_table)
2141 ret = of_property_read_u32_array(node
2142 , "bat_table", data->dts_batt_table, length);
2147 for (i = 4; i < length; i++) {
2148 batt_table[i] = data->dts_batt_table[i];
2150 pr_info("data->dts_batt_table[ %d ] %d %d\n", i
2151 , data->dts_batt_table[i], batt_table[i]);
2153 data->dc_det_pin = of_get_named_gpio_flags(node
2154 , "dc_det_gpio", 0, &flags);
2155 if (data->dc_det_pin == -EPROBE_DEFER)
2156 pr_info("%s dc_det_gpio error\n", __func__);
2158 if (batt_gpio_is_valid(data->dc_det_pin))
2159 data->dc_det_level = (flags & OF_GPIO_ACTIVE_LOW)
2160 ? RK30_GPIO_LOW : RK30_GPIO_HIGH;
2162 data->batt_low_pin = of_get_named_gpio_flags(node
2163 , "bat_low_gpio", 0, &flags);
2164 if (data->batt_low_pin == -EPROBE_DEFER)
2165 pr_info("%s bat_low_gpio error\n", __func__);
2167 if (batt_gpio_is_valid(data->batt_low_pin))
2168 data->batt_low_level = (flags & OF_GPIO_ACTIVE_LOW)
2169 ? RK30_GPIO_LOW : RK30_GPIO_HIGH;
2171 data->charge_ok_pin = of_get_named_gpio_flags(node
2172 , "chg_ok_gpio", 0, &flags);
2173 if (data->charge_ok_pin == -EPROBE_DEFER)
2174 pr_info("%s chg_ok_gpio error\n", __func__);
2176 if (batt_gpio_is_valid(data->charge_ok_pin))
2177 data->charge_ok_level = (flags & OF_GPIO_ACTIVE_LOW)
2178 ? RK30_GPIO_LOW : RK30_GPIO_HIGH;
2180 ret = of_property_read_u32(node, "is_dc_charge", &value);
2182 pr_info("%s:hardware unsupport dc charge\n", __func__);
2185 data->is_dc_charge = value;
2187 ret = of_property_read_u32(node, "is_usb_charge"
2190 pr_err("%s:hardware unsupport usb charge\n", __func__);
2193 data->is_usb_charge = value;
2195 pr_info("rk30 battery:support %s %s charger\n",
2196 data->is_dc_charge ? "DC" : ""
2197 , data->is_usb_charge ? "USB" : "");
2202 static int rk31_adcbat_parse_dt(struct platform_device
2203 *dev, struct rk30_adc_battery_platform_data *data)
2210 static int rk30_adc_battery_probe(struct platform_device *pdev)
2216 struct rk30_adc_battery_data *data;
2217 struct rk30_adc_battery_platform_data *pdata;
2218 #ifdef CONFIG_MACH_RK_FAC
2220 for(i=0;i<BATT_NUM;i++)
2222 batt_table[6+i]=pdata->chargeArray[i];
2223 batt_table[BATT_NUM+6+i]=pdata->chargeArray[i];
2226 gSecondsCnt = get_seconds();
2227 /*data = kzalloc(sizeof(*data), GFP_KERNEL);*/
2228 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2231 goto err_data_alloc_failed;
2234 /*pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);*/
2235 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2236 if (pdata == NULL) {
2238 goto err_pdata_alloc_failed;
2240 memset(data, 0, sizeof(struct rk30_adc_battery_data));
2241 gBatteryData = data;
2242 platform_set_drvdata(pdev, data);
2244 data->pdata = pdata;
2245 data->status_lock = 0;
2246 data->old_charge_level = -1;
2247 data->capacitytmp = 0;
2248 data->suspend_capacity = 0;
2249 data->ac_charging = 0;
2250 data->usb_charging = 0;
2251 data->full_times = 0;
2252 data->gBatCapacityDisChargeCnt =0;
2253 data->gBatCapacityChargeCnt=0;
2254 data->gBatCapacityusbdisChargeCnt=0 ;
2255 data->gBatCapacityusbChargeCnt =0;
2256 data->gBatCapacityacChargeCnt = 0;
2257 data->charge_source_now = 0;
2258 data->charge_soure_old = 0;
2259 data->start_voltage_status = 0;
2260 data->charge_full_flag =0;
2261 data->pSamples = data->adc_samples;
2262 data->lower_power_flag = 0;
2263 data->capacitytmp = 0;
2264 data->time_to_full = 0;
2265 data->stop_check = 0;
2266 data->voltage_to_local = 0;
2268 data->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
2269 wake_lock_init(&batt_wake_lock, WAKE_LOCK_SUSPEND, "batt_lock");
2270 wake_lock_init(&charge_display_lock, WAKE_LOCK_SUSPEND, "charge_display_lock"); //charge_display_lock
2271 wake_lock_init(&batt_wake_lock_detect_lowpower
2272 , WAKE_LOCK_SUSPEND, "lowpower_lock");
2274 ret = rk31_adcbat_parse_dt(pdev, data->pdata);
2275 /*ret = rk31_adcbat_parse_dt(pdev, pdata);*/
2277 pr_err("failed to find rk30 adc battery platform data\n");
2281 ret = rk30_adc_battery_io_init(data->pdata);
2287 memset(data->adc_samples, 0, sizeof(int)*(NUM_VOLTAGE_SAMPLE + 2));
2289 //register adc for battery sample
2290 data->wq = create_singlethread_workqueue("adc_battd");
2292 /*data->client = client;*/
2293 data->adc_val = rk_adc_battery_iio_read(data->pdata);
2295 data ->bat = rk30_battery_supply;
2296 ret = power_supply_register(&pdev->dev,&data ->bat);
2299 printk(KERN_INFO "fail to battery power_supply_register\n");
2300 goto err_battery_failed;
2302 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
2303 data ->usb = rk30_usb_supply;
2304 ret = power_supply_register(&pdev->dev, &data ->usb);
2307 printk(KERN_INFO "fail to usb power_supply_register\n");
2308 goto err_usb_failed;
2311 data ->ac = rk30_ac_supply;
2312 ret = power_supply_register(&pdev->dev, &data ->ac);
2315 printk(KERN_INFO "fail to ac power_supply_register\n");
2319 //init dc dectet irq & delay work
2320 if (batt_gpio_is_valid(data->pdata->dc_det_pin)) {
2321 INIT_WORK(&data->dcwakeup_work, rk30_adc_battery_dcdet_delaywork);
2323 irq = gpio_to_irq(data->pdata->dc_det_pin);
2324 irq_flag = gpio_get_value(data->pdata->dc_det_pin)
2325 ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
2326 ret = request_irq(irq, rk30_adc_battery_dc_wakeup, irq_flag, "ac_charge_irq", NULL);
2329 printk("failed to request dc det irq\n");
2330 goto err_dcirq_failed;
2332 enable_irq_wake(irq);
2337 ret = device_create_file(&pdev->dev,&dev_attr_batparam);
2340 printk(KERN_ERR "failed to create bat param file\n");
2341 goto err_battery_failed;
2344 ret = create_sysfs_interfaces(&pdev->dev);
2349 "device rk30_adc_batterry sysfs register failed\n");
2353 //Power on Battery detect
2354 rk30_adc_battery_check(data);
2356 // data->wq = create_singlethread_workqueue("adc_battd");
2357 INIT_DELAYED_WORK(&data->delay_work, rk30_adc_battery_timer_work);
2359 if (1 == data->pdata->save_capacity) {
2360 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS*10));
2361 data ->poweron_check = 1;
2363 queue_delayed_work(data->wq, &data->delay_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2364 data ->poweron_check = 0;
2366 INIT_DELAYED_WORK(&data->check_work, rk_adc_battery_check_work);
2367 queue_delayed_work(data ->wq, &data ->check_work, msecs_to_jiffies(TIMER_MS_COUNTS));
2368 if (batt_gpio_is_valid(data->pdata->batt_low_pin)) {
2369 if (0 == gpio_get_value(data->pdata->batt_low_pin)) {
2371 if (gpio_get_value(data->pdata->batt_low_pin) == 0) {
2372 printk("lower power\n");
2377 INIT_WORK(&data->lowerpower_work, rk30_adc_battery_lowerpower_delaywork);
2378 irq = gpio_to_irq(data->pdata->batt_low_pin);
2379 ret = request_irq(irq, rk30_adc_battery_low_wakeup, IRQF_TRIGGER_LOW, "batt_low_irq", NULL);
2382 printk("failed to request batt_low_irq irq\n");
2383 goto err_lowpowerirq_failed;
2388 data->battery_nb.notifier_call = battery_notifier_call;
2389 register_adc_battery_notifier(&data->battery_nb);
2391 printk(KERN_INFO "rk30_adc_battery: driver initialized\n");
2395 power_supply_unregister(&data ->usb);
2397 power_supply_unregister(&data ->ac);
2400 power_supply_unregister(&data ->bat);
2403 if (batt_gpio_is_valid(data->pdata->dc_det_pin))
2404 free_irq(gpio_to_irq(data->pdata->dc_det_pin), data);
2406 err_lowpowerirq_failed:
2407 if (batt_gpio_is_valid(data->pdata->batt_low_pin))
2408 free_irq(gpio_to_irq(data->pdata->batt_low_pin), data);
2413 err_pdata_alloc_failed:
2415 err_data_alloc_failed:
2418 printk("rk30_adc_battery: error!\n");
2423 static int rk30_adc_battery_remove(struct platform_device *pdev)
2425 struct rk30_adc_battery_data *data = platform_get_drvdata(pdev);
2426 struct rk30_adc_battery_platform_data *pdata = data->pdata;
2428 cancel_delayed_work_sync(&data->delay_work);
2429 if (batt_gpio_is_valid(pdata->dc_det_pin))
2430 cancel_work_sync(&data->dcwakeup_work);
2431 cancel_delayed_work_sync(&data->check_work);
2433 if (batt_gpio_is_valid(pdata->batt_low_pin))
2434 cancel_work_sync(&data->lowerpower_work);
2435 #if defined (CONFIG_BATTERY_RK30_USB_CHARGE)
2436 power_supply_unregister(&data ->usb);
2438 power_supply_unregister(&data ->ac);
2439 power_supply_unregister(&data ->bat);
2440 if (batt_gpio_is_valid(pdata->dc_det_pin))
2441 free_irq(gpio_to_irq(pdata->dc_det_pin), data);
2449 static const struct of_device_id rk30_adc_battery_dt_match[] = {
2450 { .compatible = "rk30-adc-battery",},
2453 MODULE_DEVICE_TABLE(of, rk30_adc_battery_dt_match);
2457 static struct platform_driver rk30_adc_battery_driver = {
2458 .probe = rk30_adc_battery_probe,
2459 .remove = rk30_adc_battery_remove,
2460 .suspend = rk30_adc_battery_suspend,
2461 .resume = rk30_adc_battery_resume,
2463 .name = "rk30-adc-battery",
2464 .owner = THIS_MODULE,
2465 .of_match_table = of_match_ptr(rk30_adc_battery_dt_match),
2469 static int __init rk30_adc_battery_init(void)
2471 return platform_driver_register(&rk30_adc_battery_driver);
2474 static void __exit rk30_adc_battery_exit(void)
2476 platform_driver_unregister(&rk30_adc_battery_driver);
2478 //module_init(rk30_adc_battery_init);//module_init(rk30_adc_battery_init);//
2479 subsys_initcall(rk30_adc_battery_init);
2480 //fs_initcall(rk30_adc_battery_init);
2481 module_exit(rk30_adc_battery_exit);
2485 MODULE_DESCRIPTION("Battery detect driver for the rk30");
2486 MODULE_AUTHOR("luowei lw@rock-chips.com");
2487 MODULE_LICENSE("GPL");