3 * Texas Instruments, <www.ti.com>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
24 #ifdef CONFIG_TWL6030_POWER
28 /* Functions to read and write from TWL6030 */
29 static inline int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val)
31 return i2c_write(chip_no, reg, 1, &val, 1);
34 static inline int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *val)
36 return i2c_read(chip_no, reg, 1, val, 1);
39 static int twl6030_gpadc_read_channel(u8 channel_no)
45 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
46 GPCH0_LSB + channel_no * 2, &lsb);
50 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC,
51 GPCH0_MSB + channel_no * 2, &msb);
55 return (msb << 8) | lsb;
58 static int twl6030_gpadc_sw2_trigger(void)
63 ret = twl6030_i2c_write_u8(TWL6030_CHIP_ADC, CTRL_P2, CTRL_P2_SP2);
67 /* Waiting until the SW1 conversion ends*/
70 while (!((val & CTRL_P2_EOCP2) && (!(val & CTRL_P2_BUSY)))) {
71 ret = twl6030_i2c_read_u8(TWL6030_CHIP_ADC, CTRL_P2, &val);
80 void twl6030_stop_usb_charging(void)
82 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CONTROLLER_CTRL1, 0);
87 void twl6030_start_usb_charging(void)
89 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
90 CHARGERUSB_VICHRG, CHARGERUSB_VICHRG_1500);
91 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
92 CHARGERUSB_CINLIMIT, CHARGERUSB_CIN_LIMIT_NONE);
93 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
94 CONTROLLER_INT_MASK, MBAT_TEMP);
95 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
96 CHARGERUSB_INT_MASK, MASK_MCHARGERUSB_THMREG);
97 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
98 CHARGERUSB_VOREG, CHARGERUSB_VOREG_4P0);
99 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
100 CHARGERUSB_CTRL2, CHARGERUSB_CTRL2_VITERM_400);
101 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, CHARGERUSB_CTRL1, TERM);
102 /* Enable USB charging */
103 twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER,
104 CONTROLLER_CTRL1, CONTROLLER_CTRL1_EN_CHARGER);
108 int twl6030_get_battery_current(void)
110 int battery_current = 0;
114 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_11, &msb);
115 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, FG_REG_10, &lsb);
116 battery_current = ((msb << 8) | lsb);
118 /* convert 10 bit signed number to 16 bit signed number */
119 if (battery_current >= 0x2000)
120 battery_current = (battery_current - 0x4000);
122 battery_current = battery_current * 3000 / 4096;
123 printf("Battery Current: %d mA\n", battery_current);
125 return battery_current;
128 int twl6030_get_battery_voltage(void)
130 int battery_volt = 0;
133 /* Start GPADC SW conversion */
134 ret = twl6030_gpadc_sw2_trigger();
136 printf("Failed to convert battery voltage\n");
140 /* measure Vbat voltage */
141 battery_volt = twl6030_gpadc_read_channel(7);
142 if (battery_volt < 0) {
143 printf("Failed to read battery voltage\n");
146 battery_volt = (battery_volt * 25 * 1000) >> (10 + 2);
147 printf("Battery Voltage: %d mV\n", battery_volt);
152 void twl6030_init_battery_charging(void)
155 int battery_volt = 0;
158 /* Enable VBAT measurement */
159 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC1, VBAT_MEAS);
161 /* Enable GPADC module */
162 ret = twl6030_i2c_write_u8(TWL6030_CHIP_CHARGER, TOGGLE1, FGS | GPADCS);
164 printf("Failed to enable GPADC\n");
168 battery_volt = twl6030_get_battery_voltage();
169 if (battery_volt < 0)
172 if (battery_volt < 3000)
173 printf("Main battery voltage too low!\n");
175 /* Check for the presence of USB charger */
176 twl6030_i2c_read_u8(TWL6030_CHIP_CHARGER, CONTROLLER_STAT1, &stat1);
178 /* check for battery presence indirectly via Fuel gauge */
179 if ((stat1 & VBUS_DET) && (battery_volt < 3300))
180 twl6030_start_usb_charging();
185 void twl6030_power_mmc_init()
187 /* set voltage to 3.0 and turnon for APP */
188 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_VOLTATE, 0x15);
189 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VMMC_CFG_STATE, 0x21);
192 void twl6030_usb_device_settings()
196 /* Select APP Group and set state to ON */
197 twl6030_i2c_write_u8(TWL6030_CHIP_PM, VUSB_CFG_STATE, 0x21);
199 twl6030_i2c_read_u8(TWL6030_CHIP_PM, MISC2, &data);
202 /* Select the input supply for VBUS regulator */
203 twl6030_i2c_write_u8(TWL6030_CHIP_PM, MISC2, data);