Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Nishanth Menon | 6541817 | 2013-03-26 05:20:53 +0000 | [diff] [blame] | 7 | #ifndef TWL6030_H |
| 8 | #define TWL6030_H |
| 9 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 10 | #include <i2c.h> |
| 11 | |
| 12 | /* I2C chip addresses */ |
| 13 | #define TWL6030_CHIP_PM 0x48 |
| 14 | |
| 15 | #define TWL6030_CHIP_USB 0x49 |
| 16 | #define TWL6030_CHIP_ADC 0x49 |
| 17 | #define TWL6030_CHIP_CHARGER 0x49 |
| 18 | #define TWL6030_CHIP_PWM 0x49 |
| 19 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 20 | /* Slave Address 0x48 */ |
Paul Kocialkowski | affa66f | 2016-02-27 19:19:13 +0100 | [diff] [blame] | 21 | #define TWL6030_STS_HW_CONDITIONS 0x21 |
| 22 | |
| 23 | #define TWL6030_STS_HW_CONDITIONS_PWRON (1 << 0) |
| 24 | |
Paul Kocialkowski | c76b97a | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 25 | #define TWL6030_PHOENIX_DEV_ON 0x25 |
| 26 | |
| 27 | #define TWL6030_PHOENIX_APP_DEVOFF (1 << 0) |
| 28 | #define TWL6030_PHOENIX_CON_DEVOFF (1 << 1) |
| 29 | #define TWL6030_PHOENIX_MOD_DEVOFF (1 << 2) |
| 30 | |
Paul Kocialkowski | 1db349f | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 31 | #define TWL6030_PH_STS_BOOT 0x29 |
| 32 | |
| 33 | #define TWL6030_PH_STS_BOOT0 (1 << 0) |
| 34 | #define TWL6030_PH_STS_BOOT1 (1 << 1) |
| 35 | #define TWL6030_PH_STS_BOOT2 (1 << 2) |
| 36 | #define TWL6030_PH_STS_BOOT3 (1 << 3) |
| 37 | |
| 38 | #define TWL6030_VAUX1_CFG_STATE 0x86 |
| 39 | #define TWL6030_VAUX1_CFG_VOLTAGE 0x87 |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 40 | #define TWL6030_VMMC_CFG_STATE 0x9A |
| 41 | #define TWL6030_VMMC_CFG_VOLTAGE 0x9B |
| 42 | #define TWL6030_VUSB_CFG_STATE 0xA2 |
Paul Kocialkowski | 3f3b12b | 2016-02-27 19:19:03 +0100 | [diff] [blame] | 43 | #define TWL6030_VUSB_CFG_VOLTAGE 0xA3 |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 44 | |
| 45 | #define TWL6030_CFG_GRP_P1 (1 << 0) |
| 46 | #define TWL6030_CFG_STATE_ON (1 << 0) |
| 47 | #define TWL6030_CFG_STATE_P1 (TWL6030_CFG_GRP_P1 << 5) |
Paul Kocialkowski | 1db349f | 2016-02-27 19:18:50 +0100 | [diff] [blame] | 48 | #define TWL6030_CFG_VOLTAGE_18 0x09 |
| 49 | #define TWL6030_CFG_VOLTAGE_28 0x13 |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 50 | #define TWL6030_CFG_VOLTAGE_30 0x15 |
Paul Kocialkowski | 3f3b12b | 2016-02-27 19:19:03 +0100 | [diff] [blame] | 51 | #define TWL6030_CFG_VOLTAGE_33 0x18 |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 52 | |
| 53 | #define MISC1 0xE4 |
| 54 | #define VAC_MEAS (1 << 2) |
| 55 | #define VBAT_MEAS (1 << 1) |
| 56 | #define BB_MEAS (1 << 0) |
| 57 | |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 58 | #define TWL6030_MISC2 0xE5 |
Paul Kocialkowski | e1545d9 | 2016-02-27 19:19:04 +0100 | [diff] [blame] | 59 | #define TWL6030_MISC2_VUSB_IN_PMID (1 << 3) |
Paul Kocialkowski | b8421a7 | 2016-02-27 19:18:49 +0100 | [diff] [blame] | 60 | #define TWL6030_MISC2_VUSB_IN_VSYS (1 << 4) |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 61 | |
| 62 | /* Slave Address 0x49 */ |
| 63 | |
Paul Kocialkowski | affa66f | 2016-02-27 19:19:13 +0100 | [diff] [blame] | 64 | #define TWL6030_CONTROLLER_STAT1 0xE3 |
| 65 | |
| 66 | #define TWL6030_CONTROLLER_STAT1_VAC_DET (1 << 3) |
| 67 | #define TWL6030_CONTROLLER_STAT1_VBUS_DET (1 << 2) |
| 68 | |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 69 | /* Battery CHARGER REGISTERS */ |
| 70 | #define CONTROLLER_INT_MASK 0xE0 |
| 71 | #define CONTROLLER_CTRL1 0xE1 |
| 72 | #define CONTROLLER_WDG 0xE2 |
| 73 | #define CONTROLLER_STAT1 0xE3 |
| 74 | #define CHARGERUSB_INT_STATUS 0xE4 |
| 75 | #define CHARGERUSB_INT_MASK 0xE5 |
| 76 | #define CHARGERUSB_STATUS_INT1 0xE6 |
| 77 | #define CHARGERUSB_STATUS_INT2 0xE7 |
| 78 | #define CHARGERUSB_CTRL1 0xE8 |
| 79 | #define CHARGERUSB_CTRL2 0xE9 |
| 80 | #define CHARGERUSB_CTRL3 0xEA |
| 81 | #define CHARGERUSB_STAT1 0xEB |
| 82 | #define CHARGERUSB_VOREG 0xEC |
| 83 | #define CHARGERUSB_VICHRG 0xED |
| 84 | #define CHARGERUSB_CINLIMIT 0xEE |
| 85 | #define CHARGERUSB_CTRLLIMIT1 0xEF |
| 86 | |
| 87 | /* CHARGERUSB_VICHRG */ |
| 88 | #define CHARGERUSB_VICHRG_500 0x4 |
| 89 | #define CHARGERUSB_VICHRG_1500 0xE |
| 90 | /* CHARGERUSB_CINLIMIT */ |
| 91 | #define CHARGERUSB_CIN_LIMIT_100 0x1 |
| 92 | #define CHARGERUSB_CIN_LIMIT_300 0x5 |
| 93 | #define CHARGERUSB_CIN_LIMIT_500 0x9 |
| 94 | #define CHARGERUSB_CIN_LIMIT_NONE 0xF |
| 95 | /* CONTROLLER_INT_MASK */ |
| 96 | #define MVAC_FAULT (1 << 6) |
| 97 | #define MAC_EOC (1 << 5) |
| 98 | #define MBAT_REMOVED (1 << 4) |
| 99 | #define MFAULT_WDG (1 << 3) |
| 100 | #define MBAT_TEMP (1 << 2) |
| 101 | #define MVBUS_DET (1 << 1) |
| 102 | #define MVAC_DET (1 << 0) |
| 103 | /* CHARGERUSB_INT_MASK */ |
| 104 | #define MASK_MCURRENT_TERM (1 << 3) |
| 105 | #define MASK_MCHARGERUSB_STAT (1 << 2) |
| 106 | #define MASK_MCHARGERUSB_THMREG (1 << 1) |
| 107 | #define MASK_MCHARGERUSB_FAULT (1 << 0) |
| 108 | /* CHARGERUSB_VOREG */ |
| 109 | #define CHARGERUSB_VOREG_3P52 0x01 |
| 110 | #define CHARGERUSB_VOREG_4P0 0x19 |
| 111 | #define CHARGERUSB_VOREG_4P2 0x23 |
| 112 | #define CHARGERUSB_VOREG_4P76 0x3F |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 113 | /* CHARGERUSB_CTRL1 */ |
| 114 | #define SUSPEND_BOOT (1 << 7) |
| 115 | #define OPA_MODE (1 << 6) |
| 116 | #define HZ_MODE (1 << 5) |
| 117 | #define TERM (1 << 4) |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 118 | /* CHARGERUSB_CTRL2 */ |
| 119 | #define CHARGERUSB_CTRL2_VITERM_50 (0 << 5) |
| 120 | #define CHARGERUSB_CTRL2_VITERM_100 (1 << 5) |
| 121 | #define CHARGERUSB_CTRL2_VITERM_150 (2 << 5) |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 122 | #define CHARGERUSB_CTRL2_VITERM_400 (7 << 5) |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 123 | /* CONTROLLER_CTRL1 */ |
| 124 | #define CONTROLLER_CTRL1_EN_CHARGER (1 << 4) |
| 125 | #define CONTROLLER_CTRL1_SEL_CHARGER (1 << 3) |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 126 | /* CONTROLLER_STAT1 */ |
| 127 | #define CHRG_EXTCHRG_STATZ (1 << 7) |
| 128 | #define CHRG_DET_N (1 << 5) |
| 129 | #define VAC_DET (1 << 3) |
| 130 | #define VBUS_DET (1 << 2) |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 131 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 132 | #define FG_REG_10 0xCA |
| 133 | #define FG_REG_11 0xCB |
| 134 | |
| 135 | #define TOGGLE1 0x90 |
| 136 | #define FGS (1 << 5) |
| 137 | #define FGR (1 << 4) |
| 138 | #define GPADCS (1 << 1) |
| 139 | #define GPADCR (1 << 0) |
| 140 | |
| 141 | #define CTRL_P2 0x34 |
| 142 | #define CTRL_P2_SP2 (1 << 2) |
| 143 | #define CTRL_P2_EOCP2 (1 << 1) |
| 144 | #define CTRL_P2_BUSY (1 << 0) |
| 145 | |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 146 | #define TWL6032_CTRL_P1 0x36 |
| 147 | #define CTRL_P1_SP1 (1 << 3) |
| 148 | |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 149 | #define GPCH0_LSB 0x57 |
| 150 | #define GPCH0_MSB 0x58 |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 151 | |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 152 | #define TWL6032_GPCH0_LSB 0x3b |
| 153 | |
| 154 | #define TWL6032_GPSELECT_ISB 0x35 |
| 155 | |
| 156 | #define USB_PRODUCT_ID_LSB 0x02 |
| 157 | |
| 158 | #define TWL6030_GPADC_VBAT_CHNL 0x07 |
| 159 | #define TWL6032_GPADC_VBAT_CHNL 0x12 |
| 160 | |
| 161 | #define TWL6030_GPADC_CTRL 0x2e |
| 162 | #define TWL6032_GPADC_CTRL2 0x2f |
| 163 | #define GPADC_CTRL2_CH18_SCALER_EN (1 << 2) |
| 164 | #define GPADC_CTRL_SCALER_DIV4 (1 << 3) |
| 165 | |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 166 | #define TWL6030_VBAT_MULT 40 * 1000 |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 167 | #define TWL6032_VBAT_MULT 25 * 1000 |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 168 | |
| 169 | #define TWL6030_VBAT_SHIFT (10 + 3) |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 170 | #define TWL6032_VBAT_SHIFT (12 + 2) |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 171 | |
| 172 | enum twl603x_chip_type{ |
| 173 | chip_TWL6030, |
Oleg Kosheliev | 5c05da7 | 2013-10-08 15:49:56 +0300 | [diff] [blame] | 174 | chip_TWL6032, |
Oleg Kosheliev | 8cce7a9 | 2013-10-08 15:49:55 +0300 | [diff] [blame] | 175 | chip_TWL603X_cnt |
| 176 | }; |
| 177 | |
| 178 | struct twl6030_data{ |
| 179 | u8 chip_type; |
| 180 | u8 adc_rbase; |
| 181 | u8 adc_ctrl; |
| 182 | u8 adc_enable; |
| 183 | int vbat_mult; |
| 184 | int vbat_shift; |
| 185 | }; |
| 186 | |
Nishanth Menon | 5b04f12 | 2013-03-26 05:20:52 +0000 | [diff] [blame] | 187 | /* Functions to read and write from TWL6030 */ |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 188 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Nishanth Menon | 5b04f12 | 2013-03-26 05:20:52 +0000 | [diff] [blame] | 189 | static inline int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val) |
| 190 | { |
| 191 | return i2c_write(chip_no, reg, 1, &val, 1); |
| 192 | } |
| 193 | |
| 194 | static inline int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *val) |
| 195 | { |
| 196 | return i2c_read(chip_no, reg, 1, val, 1); |
| 197 | } |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 198 | #else |
| 199 | int twl6030_i2c_write_u8(u8 chip_no, u8 reg, u8 val); |
| 200 | int twl6030_i2c_read_u8(u8 chip_no, u8 reg, u8 *val); |
| 201 | #endif |
Nishanth Menon | 5b04f12 | 2013-03-26 05:20:52 +0000 | [diff] [blame] | 202 | |
Paul Kocialkowski | affa66f | 2016-02-27 19:19:13 +0100 | [diff] [blame] | 203 | /* |
| 204 | * Power |
| 205 | */ |
| 206 | |
Paul Kocialkowski | c76b97a | 2016-02-27 19:19:12 +0100 | [diff] [blame] | 207 | void twl6030_power_off(void); |
Steve Sakoman | f8f7c95 | 2010-07-15 12:53:42 -0700 | [diff] [blame] | 208 | void twl6030_init_battery_charging(void); |
| 209 | void twl6030_usb_device_settings(void); |
Balaji T K | 13c3c50 | 2010-11-25 16:22:04 +0530 | [diff] [blame] | 210 | void twl6030_start_usb_charging(void); |
| 211 | void twl6030_stop_usb_charging(void); |
| 212 | int twl6030_get_battery_voltage(void); |
| 213 | int twl6030_get_battery_current(void); |
Paul Kocialkowski | b649748 | 2016-02-27 19:18:51 +0100 | [diff] [blame] | 214 | void twl6030_power_mmc_init(int dev_index); |
Nishanth Menon | 6541817 | 2013-03-26 05:20:53 +0000 | [diff] [blame] | 215 | |
Paul Kocialkowski | affa66f | 2016-02-27 19:19:13 +0100 | [diff] [blame] | 216 | /* |
| 217 | * Input |
| 218 | */ |
| 219 | |
| 220 | int twl6030_input_power_button(void); |
| 221 | int twl6030_input_charger(void); |
| 222 | int twl6030_input_usb(void); |
| 223 | |
Nishanth Menon | 6541817 | 2013-03-26 05:20:53 +0000 | [diff] [blame] | 224 | #endif /* TWL6030_H */ |