Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011-2013 |
| 4 | * Texas Instruments, <www.ti.com> |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 7 | #include <i2c.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 8 | #include <linux/errno.h> |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 9 | #include <power/pmic.h> |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 10 | #include <power/tps65218.h> |
| 11 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 12 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Nikita Kiryanov | a5c71ff | 2016-02-19 19:19:46 +0200 | [diff] [blame] | 13 | int tps65218_reg_read(uchar dest_reg, uchar *dest_val) |
| 14 | { |
| 15 | uchar read_val; |
| 16 | int ret; |
| 17 | |
| 18 | ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1); |
| 19 | if (ret) |
| 20 | return ret; |
| 21 | |
| 22 | *dest_val = read_val; |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 27 | /** |
| 28 | * tps65218_reg_write() - Generic function that can write a TPS65218 PMIC |
| 29 | * register or bit field regardless of protection |
| 30 | * level. |
| 31 | * |
| 32 | * @prot_level: Register password protection. Use |
| 33 | * TPS65218_PROT_LEVEL_NONE, |
| 34 | * TPS65218_PROT_LEVEL_1 or TPS65218_PROT_LEVEL_2 |
| 35 | * @dest_reg: Register address to write. |
| 36 | * @dest_val: Value to write. |
| 37 | * @mask: Bit mask (8 bits) to be applied. Function will only |
| 38 | * change bits that are set in the bit mask. |
| 39 | * |
| 40 | * @return: 0 for success, not 0 on failure, as per the i2c API |
| 41 | */ |
| 42 | int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val, |
| 43 | uchar mask) |
| 44 | { |
| 45 | uchar read_val; |
| 46 | uchar xor_reg; |
| 47 | int ret; |
| 48 | |
| 49 | /* |
| 50 | * If we are affecting only a bit field, read dest_reg and apply the |
| 51 | * mask |
| 52 | */ |
| 53 | if (mask != TPS65218_MASK_ALL_BITS) { |
| 54 | ret = i2c_read(TPS65218_CHIP_PM, dest_reg, 1, &read_val, 1); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | read_val &= (~mask); |
| 58 | read_val |= (dest_val & mask); |
| 59 | dest_val = read_val; |
| 60 | } |
| 61 | |
| 62 | if (prot_level > 0) { |
| 63 | xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK; |
| 64 | ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1, |
| 65 | &xor_reg, 1); |
| 66 | if (ret) |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1); |
| 71 | if (ret) |
| 72 | return ret; |
| 73 | |
| 74 | if (prot_level == TPS65218_PROT_LEVEL_2) { |
| 75 | ret = i2c_write(TPS65218_CHIP_PM, TPS65218_PASSWORD, 1, |
| 76 | &xor_reg, 1); |
| 77 | if (ret) |
| 78 | return ret; |
| 79 | |
| 80 | ret = i2c_write(TPS65218_CHIP_PM, dest_reg, 1, &dest_val, 1); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 87 | #else |
Marek BehĂșn | 4bebdd3 | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 88 | struct udevice *tps65218_dev __section(".data") = NULL; |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 89 | |
| 90 | int tps65218_reg_read(uchar dest_reg, uchar *dest_val) |
| 91 | { |
| 92 | uchar read_val; |
| 93 | int ret; |
| 94 | |
| 95 | if (!tps65218_dev) |
| 96 | return -ENODEV; |
| 97 | |
| 98 | ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
| 102 | *dest_val = read_val; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int tps65218_reg_write(uchar prot_level, uchar dest_reg, uchar dest_val, |
| 108 | uchar mask) |
| 109 | { |
| 110 | uchar read_val; |
| 111 | uchar xor_reg; |
| 112 | int ret; |
| 113 | |
| 114 | if (!tps65218_dev) |
| 115 | return -ENODEV; |
| 116 | |
| 117 | /* |
| 118 | * If we are affecting only a bit field, read dest_reg and apply the |
| 119 | * mask |
| 120 | */ |
| 121 | if (mask != TPS65218_MASK_ALL_BITS) { |
| 122 | ret = dm_i2c_read(tps65218_dev, dest_reg, &read_val, 1); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | |
| 126 | read_val &= (~mask); |
| 127 | read_val |= (dest_val & mask); |
| 128 | dest_val = read_val; |
| 129 | } |
| 130 | |
| 131 | if (prot_level > 0) { |
| 132 | xor_reg = dest_reg ^ TPS65218_PASSWORD_UNLOCK; |
| 133 | ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg, |
| 134 | 1); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | if (prot_level == TPS65218_PROT_LEVEL_2) { |
| 144 | ret = dm_i2c_write(tps65218_dev, TPS65218_PASSWORD, &xor_reg, |
| 145 | 1); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | ret = dm_i2c_write(tps65218_dev, dest_reg, &dest_val, 1); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | #endif |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * tps65218_voltage_update() - Function to change a voltage level, as this |
| 160 | * is a multi-step process. |
| 161 | * @dc_cntrl_reg: DC voltage control register to change. |
| 162 | * @volt_sel: New value for the voltage register |
| 163 | * @return: 0 for success, not 0 on failure. |
| 164 | */ |
| 165 | int tps65218_voltage_update(uchar dc_cntrl_reg, uchar volt_sel) |
| 166 | { |
| 167 | if ((dc_cntrl_reg != TPS65218_DCDC1) && |
Keerthy | b0586ad | 2017-06-02 15:00:30 +0530 | [diff] [blame] | 168 | (dc_cntrl_reg != TPS65218_DCDC2) && |
| 169 | (dc_cntrl_reg != TPS65218_DCDC3)) |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 170 | return 1; |
| 171 | |
| 172 | /* set voltage level */ |
| 173 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, dc_cntrl_reg, volt_sel, |
Keerthy | 7cac656 | 2017-05-24 10:19:27 +0530 | [diff] [blame] | 174 | TPS65218_DCDC_VSEL_MASK)) |
Tom Rini | 0634fc0 | 2014-06-05 11:15:29 -0400 | [diff] [blame] | 175 | return 1; |
| 176 | |
| 177 | /* set GO bit to initiate voltage transition */ |
| 178 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_2, TPS65218_SLEW, |
| 179 | TPS65218_DCDC_GO, TPS65218_DCDC_GO)) |
| 180 | return 1; |
| 181 | |
| 182 | return 0; |
| 183 | } |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 184 | |
Nikita Kiryanov | a5c71ff | 2016-02-19 19:19:46 +0200 | [diff] [blame] | 185 | /** |
| 186 | * tps65218_toggle_fseal() - Perform the sequence that toggles the FSEAL bit. |
| 187 | * |
| 188 | * @return: 0 on success, -EBADE if the sequence was broken |
| 189 | */ |
| 190 | int tps65218_toggle_fseal(void) |
| 191 | { |
| 192 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 193 | 0xb1, TPS65218_MASK_ALL_BITS)) |
| 194 | return -EBADE; |
| 195 | |
| 196 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 197 | 0xfe, TPS65218_MASK_ALL_BITS)) |
| 198 | return -EBADE; |
| 199 | |
| 200 | if (tps65218_reg_write(TPS65218_PROT_LEVEL_NONE, TPS65218_PASSWORD, |
| 201 | 0xa3, TPS65218_MASK_ALL_BITS)) |
| 202 | return -EBADE; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * tps65218_lock_fseal() - Perform the sequence that locks the FSEAL bit to 1. |
| 209 | * |
| 210 | * The FSEAL bit prevents the PMIC from turning off DCDC5 and DCDC6. It can be |
| 211 | * toggled at most 3 times: 0->1, 1->0, and finally 0->1. After the third switch |
| 212 | * its value is locked and can only be reset by powering off the PMIC entirely. |
| 213 | * |
| 214 | * @return: 0 on success, -EBADE if the sequence was broken |
| 215 | */ |
| 216 | int tps65218_lock_fseal(void) |
| 217 | { |
| 218 | int i; |
| 219 | |
| 220 | for (i = 0; i < 3; i++) |
| 221 | if (tps65218_toggle_fseal()) |
| 222 | return -EBADE; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 227 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 228 | int power_tps65218_init(unsigned char bus) |
| 229 | { |
| 230 | static const char name[] = "TPS65218_PMIC"; |
| 231 | struct pmic *p = pmic_alloc(); |
| 232 | |
| 233 | if (!p) { |
| 234 | printf("%s: POWER allocation error!\n", __func__); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | |
| 238 | p->name = name; |
| 239 | p->interface = PMIC_I2C; |
| 240 | p->number_of_regs = TPS65218_PMIC_NUM_OF_REGS; |
| 241 | p->hw.i2c.addr = TPS65218_CHIP_PM; |
| 242 | p->hw.i2c.tx_num = 1; |
| 243 | p->bus = bus; |
| 244 | |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | #else |
| 248 | int power_tps65218_init(unsigned char bus) |
| 249 | { |
| 250 | struct udevice *dev = NULL; |
| 251 | int rc; |
| 252 | |
| 253 | rc = i2c_get_chip_for_busnum(bus, TPS65218_CHIP_PM, 1, &dev); |
| 254 | if (rc) |
| 255 | return rc; |
| 256 | tps65218_dev = dev; |
Tom Rini | 60d2f6f | 2014-06-23 16:06:29 -0400 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Jean-Jacques Hiblot | 77a1397 | 2018-12-07 14:50:46 +0100 | [diff] [blame] | 259 | #endif |