183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2516799f6SSteve Sakoman /*
3516799f6SSteve Sakoman * (C) Copyright 2010
4516799f6SSteve Sakoman * Texas Instruments, <www.ti.com>
5516799f6SSteve Sakoman */
6516799f6SSteve Sakoman
7502dac55SNishanth Menon #ifndef TWL6030_H
8502dac55SNishanth Menon #define TWL6030_H
9502dac55SNishanth Menon
10516799f6SSteve Sakoman #include <common.h>
11516799f6SSteve Sakoman #include <i2c.h>
12516799f6SSteve Sakoman
13516799f6SSteve Sakoman /* I2C chip addresses */
14516799f6SSteve Sakoman #define TWL6030_CHIP_PM 0x48
15516799f6SSteve Sakoman
16516799f6SSteve Sakoman #define TWL6030_CHIP_USB 0x49
17516799f6SSteve Sakoman #define TWL6030_CHIP_ADC 0x49
18516799f6SSteve Sakoman #define TWL6030_CHIP_CHARGER 0x49
19516799f6SSteve Sakoman #define TWL6030_CHIP_PWM 0x49
20516799f6SSteve Sakoman
213e664f6dSBalaji T K /* Slave Address 0x48 */
22461484c2SPaul Kocialkowski #define TWL6030_STS_HW_CONDITIONS 0x21
23461484c2SPaul Kocialkowski
24461484c2SPaul Kocialkowski #define TWL6030_STS_HW_CONDITIONS_PWRON (1 << 0)
25461484c2SPaul Kocialkowski
26d6a2042dSPaul Kocialkowski #define TWL6030_PHOENIX_DEV_ON 0x25
27d6a2042dSPaul Kocialkowski
28d6a2042dSPaul Kocialkowski #define TWL6030_PHOENIX_APP_DEVOFF (1 << 0)
29d6a2042dSPaul Kocialkowski #define TWL6030_PHOENIX_CON_DEVOFF (1 << 1)
30d6a2042dSPaul Kocialkowski #define TWL6030_PHOENIX_MOD_DEVOFF (1 << 2)
31d6a2042dSPaul Kocialkowski
32d7b6a754SPaul Kocialkowski #define TWL6030_PH_STS_BOOT 0x29
33d7b6a754SPaul Kocialkowski
34d7b6a754SPaul Kocialkowski #define TWL6030_PH_STS_BOOT0 (1 << 0)
35d7b6a754SPaul Kocialkowski #define TWL6030_PH_STS_BOOT1 (1 << 1)
36d7b6a754SPaul Kocialkowski #define TWL6030_PH_STS_BOOT2 (1 << 2)
37d7b6a754SPaul Kocialkowski #define TWL6030_PH_STS_BOOT3 (1 << 3)
38d7b6a754SPaul Kocialkowski
39d7b6a754SPaul Kocialkowski #define TWL6030_VAUX1_CFG_STATE 0x86
40d7b6a754SPaul Kocialkowski #define TWL6030_VAUX1_CFG_VOLTAGE 0x87
41c5dbae7cSPaul Kocialkowski #define TWL6030_VMMC_CFG_STATE 0x9A
42c5dbae7cSPaul Kocialkowski #define TWL6030_VMMC_CFG_VOLTAGE 0x9B
43c5dbae7cSPaul Kocialkowski #define TWL6030_VUSB_CFG_STATE 0xA2
440343f71fSPaul Kocialkowski #define TWL6030_VUSB_CFG_VOLTAGE 0xA3
45c5dbae7cSPaul Kocialkowski
46c5dbae7cSPaul Kocialkowski #define TWL6030_CFG_GRP_P1 (1 << 0)
47c5dbae7cSPaul Kocialkowski #define TWL6030_CFG_STATE_ON (1 << 0)
48c5dbae7cSPaul Kocialkowski #define TWL6030_CFG_STATE_P1 (TWL6030_CFG_GRP_P1 << 5)
49d7b6a754SPaul Kocialkowski #define TWL6030_CFG_VOLTAGE_18 0x09
50d7b6a754SPaul Kocialkowski #define TWL6030_CFG_VOLTAGE_28 0x13
51c5dbae7cSPaul Kocialkowski #define TWL6030_CFG_VOLTAGE_30 0x15
520343f71fSPaul Kocialkowski #define TWL6030_CFG_VOLTAGE_33 0x18
533e664f6dSBalaji T K
543e664f6dSBalaji T K #define MISC1 0xE4
553e664f6dSBalaji T K #define VAC_MEAS (1 << 2)
563e664f6dSBalaji T K #define VBAT_MEAS (1 << 1)
573e664f6dSBalaji T K #define BB_MEAS (1 << 0)
583e664f6dSBalaji T K
59c5dbae7cSPaul Kocialkowski #define TWL6030_MISC2 0xE5
606313c650SPaul Kocialkowski #define TWL6030_MISC2_VUSB_IN_PMID (1 << 3)
61c5dbae7cSPaul Kocialkowski #define TWL6030_MISC2_VUSB_IN_VSYS (1 << 4)
623e664f6dSBalaji T K
633e664f6dSBalaji T K /* Slave Address 0x49 */
643e664f6dSBalaji T K
65461484c2SPaul Kocialkowski #define TWL6030_CONTROLLER_STAT1 0xE3
66461484c2SPaul Kocialkowski
67461484c2SPaul Kocialkowski #define TWL6030_CONTROLLER_STAT1_VAC_DET (1 << 3)
68461484c2SPaul Kocialkowski #define TWL6030_CONTROLLER_STAT1_VBUS_DET (1 << 2)
69461484c2SPaul Kocialkowski
70516799f6SSteve Sakoman /* Battery CHARGER REGISTERS */
71516799f6SSteve Sakoman #define CONTROLLER_INT_MASK 0xE0
72516799f6SSteve Sakoman #define CONTROLLER_CTRL1 0xE1
73516799f6SSteve Sakoman #define CONTROLLER_WDG 0xE2
74516799f6SSteve Sakoman #define CONTROLLER_STAT1 0xE3
75516799f6SSteve Sakoman #define CHARGERUSB_INT_STATUS 0xE4
76516799f6SSteve Sakoman #define CHARGERUSB_INT_MASK 0xE5
77516799f6SSteve Sakoman #define CHARGERUSB_STATUS_INT1 0xE6
78516799f6SSteve Sakoman #define CHARGERUSB_STATUS_INT2 0xE7
79516799f6SSteve Sakoman #define CHARGERUSB_CTRL1 0xE8
80516799f6SSteve Sakoman #define CHARGERUSB_CTRL2 0xE9
81516799f6SSteve Sakoman #define CHARGERUSB_CTRL3 0xEA
82516799f6SSteve Sakoman #define CHARGERUSB_STAT1 0xEB
83516799f6SSteve Sakoman #define CHARGERUSB_VOREG 0xEC
84516799f6SSteve Sakoman #define CHARGERUSB_VICHRG 0xED
85516799f6SSteve Sakoman #define CHARGERUSB_CINLIMIT 0xEE
86516799f6SSteve Sakoman #define CHARGERUSB_CTRLLIMIT1 0xEF
87516799f6SSteve Sakoman
88516799f6SSteve Sakoman /* CHARGERUSB_VICHRG */
89516799f6SSteve Sakoman #define CHARGERUSB_VICHRG_500 0x4
90516799f6SSteve Sakoman #define CHARGERUSB_VICHRG_1500 0xE
91516799f6SSteve Sakoman /* CHARGERUSB_CINLIMIT */
92516799f6SSteve Sakoman #define CHARGERUSB_CIN_LIMIT_100 0x1
93516799f6SSteve Sakoman #define CHARGERUSB_CIN_LIMIT_300 0x5
94516799f6SSteve Sakoman #define CHARGERUSB_CIN_LIMIT_500 0x9
95516799f6SSteve Sakoman #define CHARGERUSB_CIN_LIMIT_NONE 0xF
96516799f6SSteve Sakoman /* CONTROLLER_INT_MASK */
97516799f6SSteve Sakoman #define MVAC_FAULT (1 << 6)
98516799f6SSteve Sakoman #define MAC_EOC (1 << 5)
99516799f6SSteve Sakoman #define MBAT_REMOVED (1 << 4)
100516799f6SSteve Sakoman #define MFAULT_WDG (1 << 3)
101516799f6SSteve Sakoman #define MBAT_TEMP (1 << 2)
102516799f6SSteve Sakoman #define MVBUS_DET (1 << 1)
103516799f6SSteve Sakoman #define MVAC_DET (1 << 0)
104516799f6SSteve Sakoman /* CHARGERUSB_INT_MASK */
105516799f6SSteve Sakoman #define MASK_MCURRENT_TERM (1 << 3)
106516799f6SSteve Sakoman #define MASK_MCHARGERUSB_STAT (1 << 2)
107516799f6SSteve Sakoman #define MASK_MCHARGERUSB_THMREG (1 << 1)
108516799f6SSteve Sakoman #define MASK_MCHARGERUSB_FAULT (1 << 0)
109516799f6SSteve Sakoman /* CHARGERUSB_VOREG */
110516799f6SSteve Sakoman #define CHARGERUSB_VOREG_3P52 0x01
111516799f6SSteve Sakoman #define CHARGERUSB_VOREG_4P0 0x19
112516799f6SSteve Sakoman #define CHARGERUSB_VOREG_4P2 0x23
113516799f6SSteve Sakoman #define CHARGERUSB_VOREG_4P76 0x3F
1143e664f6dSBalaji T K /* CHARGERUSB_CTRL1 */
1153e664f6dSBalaji T K #define SUSPEND_BOOT (1 << 7)
1163e664f6dSBalaji T K #define OPA_MODE (1 << 6)
1173e664f6dSBalaji T K #define HZ_MODE (1 << 5)
1183e664f6dSBalaji T K #define TERM (1 << 4)
119516799f6SSteve Sakoman /* CHARGERUSB_CTRL2 */
120516799f6SSteve Sakoman #define CHARGERUSB_CTRL2_VITERM_50 (0 << 5)
121516799f6SSteve Sakoman #define CHARGERUSB_CTRL2_VITERM_100 (1 << 5)
122516799f6SSteve Sakoman #define CHARGERUSB_CTRL2_VITERM_150 (2 << 5)
1233e664f6dSBalaji T K #define CHARGERUSB_CTRL2_VITERM_400 (7 << 5)
124516799f6SSteve Sakoman /* CONTROLLER_CTRL1 */
125516799f6SSteve Sakoman #define CONTROLLER_CTRL1_EN_CHARGER (1 << 4)
126516799f6SSteve Sakoman #define CONTROLLER_CTRL1_SEL_CHARGER (1 << 3)
1273e664f6dSBalaji T K /* CONTROLLER_STAT1 */
1283e664f6dSBalaji T K #define CHRG_EXTCHRG_STATZ (1 << 7)
1293e664f6dSBalaji T K #define CHRG_DET_N (1 << 5)
1303e664f6dSBalaji T K #define VAC_DET (1 << 3)
1313e664f6dSBalaji T K #define VBUS_DET (1 << 2)
132516799f6SSteve Sakoman
1333e664f6dSBalaji T K #define FG_REG_10 0xCA
1343e664f6dSBalaji T K #define FG_REG_11 0xCB
1353e664f6dSBalaji T K
1363e664f6dSBalaji T K #define TOGGLE1 0x90
1373e664f6dSBalaji T K #define FGS (1 << 5)
1383e664f6dSBalaji T K #define FGR (1 << 4)
1393e664f6dSBalaji T K #define GPADCS (1 << 1)
1403e664f6dSBalaji T K #define GPADCR (1 << 0)
1413e664f6dSBalaji T K
1423e664f6dSBalaji T K #define CTRL_P2 0x34
1433e664f6dSBalaji T K #define CTRL_P2_SP2 (1 << 2)
1443e664f6dSBalaji T K #define CTRL_P2_EOCP2 (1 << 1)
1453e664f6dSBalaji T K #define CTRL_P2_BUSY (1 << 0)
1463e664f6dSBalaji T K
147340e6c83SOleg Kosheliev #define TWL6032_CTRL_P1 0x36
148340e6c83SOleg Kosheliev #define CTRL_P1_SP1 (1 << 3)
149340e6c83SOleg Kosheliev
1503e664f6dSBalaji T K #define GPCH0_LSB 0x57
1513e664f6dSBalaji T K #define GPCH0_MSB 0x58
152516799f6SSteve Sakoman
153340e6c83SOleg Kosheliev #define TWL6032_GPCH0_LSB 0x3b
154340e6c83SOleg Kosheliev
155340e6c83SOleg Kosheliev #define TWL6032_GPSELECT_ISB 0x35
156340e6c83SOleg Kosheliev
157340e6c83SOleg Kosheliev #define USB_PRODUCT_ID_LSB 0x02
158340e6c83SOleg Kosheliev
159340e6c83SOleg Kosheliev #define TWL6030_GPADC_VBAT_CHNL 0x07
160340e6c83SOleg Kosheliev #define TWL6032_GPADC_VBAT_CHNL 0x12
161340e6c83SOleg Kosheliev
162340e6c83SOleg Kosheliev #define TWL6030_GPADC_CTRL 0x2e
163340e6c83SOleg Kosheliev #define TWL6032_GPADC_CTRL2 0x2f
164340e6c83SOleg Kosheliev #define GPADC_CTRL2_CH18_SCALER_EN (1 << 2)
165340e6c83SOleg Kosheliev #define GPADC_CTRL_SCALER_DIV4 (1 << 3)
166340e6c83SOleg Kosheliev
167fc889503SOleg Kosheliev #define TWL6030_VBAT_MULT 40 * 1000
168340e6c83SOleg Kosheliev #define TWL6032_VBAT_MULT 25 * 1000
169fc889503SOleg Kosheliev
170fc889503SOleg Kosheliev #define TWL6030_VBAT_SHIFT (10 + 3)
171340e6c83SOleg Kosheliev #define TWL6032_VBAT_SHIFT (12 + 2)
172fc889503SOleg Kosheliev
173fc889503SOleg Kosheliev enum twl603x_chip_type{
174fc889503SOleg Kosheliev chip_TWL6030,
175340e6c83SOleg Kosheliev chip_TWL6032,
176fc889503SOleg Kosheliev chip_TWL603X_cnt
177fc889503SOleg Kosheliev };
178fc889503SOleg Kosheliev
179fc889503SOleg Kosheliev struct twl6030_data{
180fc889503SOleg Kosheliev u8 chip_type;
181fc889503SOleg Kosheliev u8 adc_rbase;
182fc889503SOleg Kosheliev u8 adc_ctrl;
183fc889503SOleg Kosheliev u8 adc_enable;
184fc889503SOleg Kosheliev int vbat_mult;
185fc889503SOleg Kosheliev int vbat_shift;
186fc889503SOleg Kosheliev };
187fc889503SOleg Kosheliev
188ebce10e5SNishanth Menon /* Functions to read and write from TWL6030 */
189*fb1b7712SJean-Jacques Hiblot #ifndef CONFIG_DM_I2C
twl6030_i2c_write_u8(u8 chip_no,u8 reg,u8 val)190ebce10e5SNishanth Menon static inline int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val)
191ebce10e5SNishanth Menon {
192ebce10e5SNishanth Menon return i2c_write(chip_no, reg, 1, &val, 1);
193ebce10e5SNishanth Menon }
194ebce10e5SNishanth Menon
twl6030_i2c_read_u8(u8 chip_no,u8 reg,u8 * val)195ebce10e5SNishanth Menon static inline int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *val)
196ebce10e5SNishanth Menon {
197ebce10e5SNishanth Menon return i2c_read(chip_no, reg, 1, val, 1);
198ebce10e5SNishanth Menon }
199*fb1b7712SJean-Jacques Hiblot #else
200*fb1b7712SJean-Jacques Hiblot int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val);
201*fb1b7712SJean-Jacques Hiblot int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *val);
202*fb1b7712SJean-Jacques Hiblot #endif
203ebce10e5SNishanth Menon
204461484c2SPaul Kocialkowski /*
205461484c2SPaul Kocialkowski * Power
206461484c2SPaul Kocialkowski */
207461484c2SPaul Kocialkowski
208d6a2042dSPaul Kocialkowski void twl6030_power_off(void);
209516799f6SSteve Sakoman void twl6030_init_battery_charging(void);
210516799f6SSteve Sakoman void twl6030_usb_device_settings(void);
2113e664f6dSBalaji T K void twl6030_start_usb_charging(void);
2123e664f6dSBalaji T K void twl6030_stop_usb_charging(void);
2133e664f6dSBalaji T K int twl6030_get_battery_voltage(void);
2143e664f6dSBalaji T K int twl6030_get_battery_current(void);
215a85362fbSPaul Kocialkowski void twl6030_power_mmc_init(int dev_index);
216502dac55SNishanth Menon
217461484c2SPaul Kocialkowski /*
218461484c2SPaul Kocialkowski * Input
219461484c2SPaul Kocialkowski */
220461484c2SPaul Kocialkowski
221461484c2SPaul Kocialkowski int twl6030_input_power_button(void);
222461484c2SPaul Kocialkowski int twl6030_input_charger(void);
223461484c2SPaul Kocialkowski int twl6030_input_usb(void);
224461484c2SPaul Kocialkowski
225502dac55SNishanth Menon #endif /* TWL6030_H */
226