tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / cpu / armv7 / sc8825 / chg_phy.c
1 #include <common.h>
2 #include <asm/io.h>
3
4 #include <asm/arch/regs_adi.h>
5 #include <asm/arch/adi_hal_internal.h>
6 #include <asm/arch/analog_reg_v3.h>
7
8 uint16_t adc_voltage_table[2][2] =
9 {
10     {3750, 4200},
11     {3210, 3600},
12 };
13 uint16_t CHGMNG_AdcvalueToVoltage (uint16_t adcvalue)
14 {
15         int32_t temp;
16         temp = adc_voltage_table[0][1] - adc_voltage_table[1][1];
17         temp = temp * (adcvalue - adc_voltage_table[0][0]);
18         temp = temp / (adc_voltage_table[0][0] - adc_voltage_table[1][0]);
19
20         printf("uboot battery voltage:%d,adc4200:%d,adc3600:%d\n",temp + adc_voltage_table[0][1],
21                 adc_voltage_table[0][0],adc_voltage_table[1][0]);
22
23         return temp + adc_voltage_table[0][1];
24 }
25
26 void CHG_TurnOn (void)
27 {
28     ANA_REG_AND (ANA_CHGR_CTL1,~CHGR_PD_BIT);
29 }
30
31 void CHG_ShutDown (void)
32 {
33     ANA_REG_OR (ANA_CHGR_CTL1,CHGR_PD_BIT);
34 }
35
36 void CHG_SetADCCalTbl (unsigned int *adc_data)
37 {
38         if(adc_data) {
39                 if(((adc_data[2]&0xffff) < 4500 ) && ((adc_data[2]&0xffff) > 3000)
40                         && ((adc_data[3] & 0xffff) < 4500 ) && ((adc_data[3] & 0xffff) > 3000)) {
41                         printf("adc_para = 0x%x 0x%x \n",adc_data[2],adc_data[3]);
42                         adc_voltage_table[0][1]=adc_data[2]&0xffff;
43                         adc_voltage_table[0][0]=(adc_data[2]>>16)&0xffff;
44                         adc_voltage_table[1][1]=adc_data[3]&0xffff;
45                         adc_voltage_table[1][0]=(adc_data[3]>>16)&0xffff;
46                 }
47         }
48 }
49
50 void CHG_SetRecharge (void)
51 {
52         ANA_REG_OR (ANA_CHGR_CTL0,CHGR_RECHG_BIT);
53 }
54
55 void CHG_Init (void)
56 {
57         unsigned int chip_id = 0;
58
59         ANA_REG_MSK_OR(ANA_CHGR_CTL0,CHGR_CC_EN_BIT,(CHGR_CC_EN_BIT | CHGR_CC_EN_RST_BIT));
60         ANA_REG_MSK_OR(ANA_CHGR_CTL1,
61                     (2 << CHGR_CHG_CUR_SHIFT) & CHGR_CHG_CUR_MSK,CHGR_CHG_CUR_MSK); //set charge current 500mA
62         CHG_SetRecharge();
63
64         chip_id = ANA_REG_GET(ANA_CHIP_ID_LOW);
65         chip_id |= (ANA_REG_GET(ANA_CHIP_ID_HIGH) << 16);
66         if (chip_id == 0x8820A001) {    //metalfix
67                 adc_voltage_table[0][0] = 3329;
68                 adc_voltage_table[1][0] = 2855;
69         }
70 #ifdef CONFIG_AP_ADC_CALIBRATION
71         {
72                 extern int read_adc_calibration_data(char *buffer,int size);
73                 unsigned int *adc_data;
74                 int ret=0;
75                 adc_data = malloc(64);
76                 if(adc_data){
77                         ret = read_adc_calibration_data(adc_data,48);
78                         if((ret > 0) &&
79                            ((adc_data[2]&0xffff) < 4500 )&&((adc_data[2]&0xffff) > 3000)&&
80                            ((adc_data[3]&0xffff) < 4500 )&&((adc_data[3]&0xffff) > 3000)){
81                                 printf("adc_para = 0x%x 0x%x \n",adc_data[2],adc_data[3]);
82                                 adc_voltage_table[0][1]=adc_data[2]&0xffff;
83                                 adc_voltage_table[0][0]=(adc_data[2]>>16)&0xffff;
84                                 adc_voltage_table[1][1]=adc_data[3]&0xffff;
85                                 adc_voltage_table[1][0]=(adc_data[3]>>16)&0xffff;
86                         }
87                 }
88         }
89 #endif
90 }
91