change source file mode to 0644 instead of 0755
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / vbat_check.c
1 #include <common.h>
2 #include <asm/arch/adc_drvapi.h>
3 #include <boot_mode.h>
4 unsigned int get_bat_low_level(void);
5 unsigned int get_bat_low_level_chg(void);
6 int charger_connected(void);
7
8 #define _BUF_SIZE 10
9 uint32_t vbat_buf[_BUF_SIZE];
10 void put_vbat_value(uint32_t vbat)
11 {
12     int i;
13     for(i=0;i<_BUF_SIZE -1;i++){
14         vbat_buf[i] = vbat_buf[i+1];
15     }
16
17     vbat_buf[_BUF_SIZE-1] = vbat;
18 }
19
20 uint32_t get_vbat_value(void)
21 {
22     unsigned long sum=0;
23     int i;
24     for(i=0; i < _BUF_SIZE; i++)
25       sum += vbat_buf[i];
26
27     return sum/_BUF_SIZE;
28 }
29
30
31 int is_bat_low(void)
32 {
33     int adc_value = 0;
34     unsigned int comp_vbat = 0;
35     int i;
36 #ifdef CONFIG_SPX20
37 //#ifdef CONFIG_FPGA
38         return 0;
39 //#endif
40 #endif
41     if(charger_connected()){
42         comp_vbat = get_bat_low_level_chg();
43     }else{
44         comp_vbat = get_bat_low_level();
45     }
46
47     ADC_Init();
48
49     for(i=0;i<_BUF_SIZE;i++){
50 retry_adc:
51         adc_value = ADC_GetValue(ADC_CHANNEL_VBAT, false);
52         if(adc_value < 0){
53             printf("ADC read error\n");
54             udelay(10);
55             goto retry_adc;
56         }else{
57             put_vbat_value(adc_value);
58         }
59     }
60
61     adc_value = get_vbat_value();
62         
63     printf("is_bat_low adc_value:%d,comp_vbat:%d\n",adc_value,comp_vbat);
64
65     if(sprdbat_auxadc2vbatvol (adc_value) < comp_vbat)
66       return 1;
67     else
68       return 0;
69         
70 }