blob: d421e6005bae177ee27b8a30cb32f98e3afdda43 [file] [log] [blame]
Steve Sakoman516799f2010-07-15 12:53:42 -07001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23#include <config.h>
24#ifdef CONFIG_TWL6030_POWER
25
26#include <twl6030.h>
27
Balaji T K3e664f62010-11-25 16:22:04 +053028static int twl6030_gpadc_read_channel(u8 channel_no)
29{
30 u8 lsb = 0;
31 u8 msb = 0;
32 int ret = 0;
33
Nishanth Menon345ef202013-03-26 05:20:51 +000034 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
35 GPCH0_LSB + channel_no * 2, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +053036 if (ret)
37 return ret;
38
Nishanth Menon345ef202013-03-26 05:20:51 +000039 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
40 GPCH0_MSB + channel_no * 2, &msb);
Balaji T K3e664f62010-11-25 16:22:04 +053041 if (ret)
42 return ret;
43
44 return (msb << 8) | lsb;
45}
46
47static int twl6030_gpadc_sw2_trigger(void)
48{
49 u8 val;
50 int ret = 0;
51
Nishanth Menon345ef202013-03-26 05:20:51 +000052 ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, CTRL_P2, CTRL_P2_SP2);
Balaji T K3e664f62010-11-25 16:22:04 +053053 if (ret)
54 return ret;
55
56 /* Waiting until the SW1 conversion ends*/
57 val = CTRL_P2_BUSY;
58
59 while (!((val & CTRL_P2_EOCP2) && (!(val & CTRL_P2_BUSY)))) {
Nishanth Menon345ef202013-03-26 05:20:51 +000060 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, CTRL_P2, &val);
Balaji T K3e664f62010-11-25 16:22:04 +053061 if (ret)
62 return ret;
63 udelay(1000);
64 }
65
66 return 0;
67}
68
69void twl6030_stop_usb_charging(void)
70{
Nishanth Menon345ef202013-03-26 05:20:51 +000071 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0);
Balaji T K3e664f62010-11-25 16:22:04 +053072
73 return;
74}
75
Steve Sakoman516799f2010-07-15 12:53:42 -070076void twl6030_start_usb_charging(void)
77{
Nishanth Menon345ef202013-03-26 05:20:51 +000078 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
79 CHARGERUSB_VICHRG, CHARGERUSB_VICHRG_1500);
80 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
81 CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_NONE);
82 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
83 CONTROLLER_INT_MASK, MBAT_TEMP);
84 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
85 CHARGERUSB_INT_MASK, MASK_MCHARGERUSB_THMREG);
86 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
87 CHARGERUSB_VOREG, CHARGERUSB_VOREG_4P0);
88 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
89 CHARGERUSB_CTRL2, CHARGERUSB_CTRL2_VITERM_400);
90 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CHARGERUSB_CTRL1, TERM);
Steve Sakoman516799f2010-07-15 12:53:42 -070091 /* Enable USB charging */
Nishanth Menon345ef202013-03-26 05:20:51 +000092 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
93 CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER);
Steve Sakoman516799f2010-07-15 12:53:42 -070094 return;
95}
96
Balaji T K3e664f62010-11-25 16:22:04 +053097int twl6030_get_battery_current(void)
98{
99 int battery_current = 0;
100 u8 msb = 0;
101 u8 lsb = 0;
102
Nishanth Menon345ef202013-03-26 05:20:51 +0000103 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_11, &msb);
104 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_10, &lsb);
Balaji T K3e664f62010-11-25 16:22:04 +0530105 battery_current = ((msb << 8) | lsb);
106
107 /* convert 10 bit signed number to 16 bit signed number */
108 if (battery_current >= 0x2000)
109 battery_current = (battery_current - 0x4000);
110
111 battery_current = battery_current * 3000 / 4096;
112 printf("Battery Current: %d mA\n", battery_current);
113
114 return battery_current;
115}
116
117int twl6030_get_battery_voltage(void)
118{
119 int battery_volt = 0;
120 int ret = 0;
121
122 /* Start GPADC SW conversion */
123 ret = twl6030_gpadc_sw2_trigger();
124 if (ret) {
125 printf("Failed to convert battery voltage\n");
126 return ret;
127 }
128
129 /* measure Vbat voltage */
130 battery_volt = twl6030_gpadc_read_channel(7);
131 if (battery_volt < 0) {
132 printf("Failed to read battery voltage\n");
133 return ret;
134 }
135 battery_volt = (battery_volt * 25 * 1000) >> (10 + 2);
136 printf("Battery Voltage: %d mV\n", battery_volt);
137
138 return battery_volt;
139}
140
Steve Sakoman516799f2010-07-15 12:53:42 -0700141void twl6030_init_battery_charging(void)
142{
Balaji T K3e664f62010-11-25 16:22:04 +0530143 u8 stat1 = 0;
144 int battery_volt = 0;
145 int ret = 0;
146
147 /* Enable VBAT measurement */
Nishanth Menon345ef202013-03-26 05:20:51 +0000148 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC1, VBAT_MEAS);
Balaji T K3e664f62010-11-25 16:22:04 +0530149
150 /* Enable GPADC module */
Nishanth Menon345ef202013-03-26 05:20:51 +0000151 ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS);
Balaji T K3e664f62010-11-25 16:22:04 +0530152 if (ret) {
153 printf("Failed to enable GPADC\n");
154 return;
155 }
156
157 battery_volt = twl6030_get_battery_voltage();
158 if (battery_volt < 0)
159 return;
160
161 if (battery_volt < 3000)
162 printf("Main battery voltage too low!\n");
163
164 /* Check for the presence of USB charger */
Nishanth Menon345ef202013-03-26 05:20:51 +0000165 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &stat1);
Balaji T K3e664f62010-11-25 16:22:04 +0530166
167 /* check for battery presence indirectly via Fuel gauge */
168 if ((stat1 & VBUS_DET) && (battery_volt < 3300))
169 twl6030_start_usb_charging();
170
Steve Sakoman516799f2010-07-15 12:53:42 -0700171 return;
172}
173
Balaji T K14fa2dd2011-09-08 06:34:57 +0000174void twl6030_power_mmc_init()
175{
176 /* set voltage to 3.0 and turnon for APP */
Nishanth Menon345ef202013-03-26 05:20:51 +0000177 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_VOLTATE, 0x15);
178 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_STATE, 0x21);
Balaji T K14fa2dd2011-09-08 06:34:57 +0000179}
180
Steve Sakoman516799f2010-07-15 12:53:42 -0700181void twl6030_usb_device_settings()
182{
183 u8 data = 0;
184
185 /* Select APP Group and set state to ON */
Nishanth Menon345ef202013-03-26 05:20:51 +0000186 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VUSB_CFG_STATE, 0x21);
Steve Sakoman516799f2010-07-15 12:53:42 -0700187
Nishanth Menon345ef202013-03-26 05:20:51 +0000188 twl6030_i2c_read_u8(TWL6030_CHIP_PM, MISC2, &data);
Steve Sakoman516799f2010-07-15 12:53:42 -0700189 data |= 0x10;
190
191 /* Select the input supply for VBUS regulator */
Nishanth Menon345ef202013-03-26 05:20:51 +0000192 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC2, data);
Steve Sakoman516799f2010-07-15 12:53:42 -0700193}
194#endif