Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 |
| 4 | * Texas Instruments Incorporated, <www.ti.com> |
| 5 | * |
| 6 | * Keerthy <j-keerthy@ti.com> |
| 7 | */ |
| 8 | |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 9 | #include <fdtdec.h> |
| 10 | #include <errno.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 14 | #include <power/pmic.h> |
| 15 | #include <power/regulator.h> |
| 16 | #include <power/tps65941.h> |
| 17 | |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 18 | /* Single Phase Buck IDs */ |
| 19 | #define TPS65941_BUCK_ID_1 1 |
| 20 | #define TPS65941_BUCK_ID_2 2 |
| 21 | #define TPS65941_BUCK_ID_3 3 |
| 22 | #define TPS65941_BUCK_ID_4 4 |
| 23 | #define TPS65941_BUCK_ID_5 5 |
| 24 | |
| 25 | /* Multi Phase Buck IDs */ |
| 26 | #define TPS65941_BUCK_ID_12 12 |
| 27 | #define TPS65941_BUCK_ID_34 34 |
| 28 | #define TPS65941_BUCK_ID_123 123 |
| 29 | #define TPS65941_BUCK_ID_1234 1234 |
| 30 | |
| 31 | /* LDO IDs */ |
| 32 | #define TPS65941_LDO_ID_1 1 |
| 33 | #define TPS65941_LDO_ID_2 2 |
| 34 | #define TPS65941_LDO_ID_3 3 |
| 35 | #define TPS65941_LDO_ID_4 4 |
| 36 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 37 | #define TPS65941_BUCK_CONV_OPS_IDX 0 |
| 38 | #define TPS65941_LDO_CONV_OPS_IDX 0 |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 39 | #define TPS65224_LDO_CONV_OPS_IDX 1 |
| 40 | #define TPS65224_BUCK_CONV_OPS_IDX 1 |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 41 | |
| 42 | struct tps65941_reg_conv_ops { |
| 43 | int volt_mask; |
| 44 | int (*volt2val)(int idx, int uV); |
| 45 | int (*val2volt)(int idx, int volt); |
| 46 | int slew_mask; |
| 47 | int (*lookup_slew)(int id); |
| 48 | }; |
| 49 | |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 50 | static const char tps65941_buck_ctrl[TPS65941_BUCK_NUM] = {0x4, 0x6, 0x8, 0xA, |
| 51 | 0xC}; |
| 52 | static const char tps65941_buck_vout[TPS65941_BUCK_NUM] = {0xE, 0x10, 0x12, |
| 53 | 0x14, 0x16}; |
| 54 | static const char tps65941_ldo_ctrl[TPS65941_BUCK_NUM] = {0x1D, 0x1E, 0x1F, |
| 55 | 0x20}; |
| 56 | static const char tps65941_ldo_vout[TPS65941_BUCK_NUM] = {0x23, 0x24, 0x25, |
| 57 | 0x26}; |
| 58 | |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 59 | static inline int tps65941_get_chip_id(struct udevice *dev) |
| 60 | { |
| 61 | return dev->parent->driver_data; |
| 62 | } |
| 63 | |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 64 | static int tps65941_buck_enable(struct udevice *dev, int op, bool *enable) |
| 65 | { |
| 66 | int ret; |
| 67 | unsigned int adr; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 68 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 69 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 70 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 71 | adr = uc_pdata->ctrl_reg; |
| 72 | |
| 73 | ret = pmic_reg_read(dev->parent, adr); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
| 77 | if (op == PMIC_OP_GET) { |
| 78 | ret &= TPS65941_BUCK_MODE_MASK; |
| 79 | |
| 80 | if (ret) |
| 81 | *enable = true; |
| 82 | else |
| 83 | *enable = false; |
| 84 | |
| 85 | return 0; |
| 86 | } else if (op == PMIC_OP_SET) { |
| 87 | if (*enable) |
| 88 | ret |= TPS65941_BUCK_MODE_MASK; |
| 89 | else |
| 90 | ret &= ~TPS65941_BUCK_MODE_MASK; |
| 91 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 99 | static int tps65941_buck_volt2val(__maybe_unused int idx, int uV) |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 100 | { |
| 101 | if (uV > TPS65941_BUCK_VOLT_MAX) |
| 102 | return -EINVAL; |
| 103 | else if (uV > 1650000) |
| 104 | return (uV - 1660000) / 20000 + 0xAB; |
| 105 | else if (uV > 1110000) |
| 106 | return (uV - 1110000) / 10000 + 0x73; |
| 107 | else if (uV > 600000) |
| 108 | return (uV - 600000) / 5000 + 0x0F; |
| 109 | else if (uV >= 300000) |
| 110 | return (uV - 300000) / 20000 + 0x00; |
| 111 | else |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 115 | static int tps65941_buck_val2volt(__maybe_unused int idx, int val) |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 116 | { |
| 117 | if (val > TPS65941_BUCK_VOLT_MAX_HEX) |
| 118 | return -EINVAL; |
| 119 | else if (val > 0xAB) |
| 120 | return 1660000 + (val - 0xAB) * 20000; |
| 121 | else if (val > 0x73) |
| 122 | return 1100000 + (val - 0x73) * 10000; |
| 123 | else if (val > 0xF) |
| 124 | return 600000 + (val - 0xF) * 5000; |
| 125 | else if (val >= 0x0) |
| 126 | return 300000 + val * 5000; |
| 127 | else |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | int tps65941_lookup_slew(int id) |
| 132 | { |
| 133 | switch (id) { |
| 134 | case 0: |
| 135 | return 33000; |
| 136 | case 1: |
| 137 | return 20000; |
| 138 | case 2: |
| 139 | return 10000; |
| 140 | case 3: |
| 141 | return 5000; |
| 142 | case 4: |
| 143 | return 2500; |
| 144 | case 5: |
| 145 | return 1300; |
| 146 | case 6: |
| 147 | return 630; |
| 148 | case 7: |
| 149 | return 310; |
| 150 | default: |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 155 | static int tps65224_buck_volt2val(int idx, int uV) |
| 156 | { |
| 157 | /* This functions maps a value which is in micro Volts to the VSET value. |
| 158 | * The mapping is as per the datasheet of TPS65224. |
| 159 | */ |
| 160 | |
| 161 | if (uV > TPS65224_BUCK_VOLT_MAX) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | if (idx > 0) { |
| 165 | /* Buck2, Buck3 and Buck4 of TPS65224 has a different schema in |
| 166 | * converting b/w micro_volt and VSET hex values |
| 167 | * |
| 168 | * VSET value starts from 0x00 for 0.5V, and for every increment |
| 169 | * in VSET value the output voltage increases by 25mV. This is upto |
| 170 | * 1.15V where VSET is 0x1A. |
| 171 | * |
| 172 | * For 0x1B the output voltage is 1.2V, and for every increment of |
| 173 | * VSET the output voltage increases by 50mV upto the max voltage of |
| 174 | * 3.3V |
| 175 | * |
| 176 | * | Voltage Ranges | VSET Ranges | Voltage Step | |
| 177 | * +-----------------+--------------+--------------+ |
| 178 | * | 0.5V to 1.50V | 0x00 to 0x1A | 25mV | |
| 179 | * | 1.2V to 3.3V | 0x1B to 0x45 | 50mV | |
| 180 | */ |
| 181 | if (uV >= 1200000) |
| 182 | return (uV - 1200000) / 50000 + 0x1B; |
| 183 | else if (uV >= 500000) |
| 184 | return (uV - 500000) / 25000; |
| 185 | else |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | /* Buck1 and Buck12(dual phase) has a different mapping b/w output |
| 190 | * voltage and VSET value. |
| 191 | * |
| 192 | * | Voltage Ranges | VSET Ranges | Voltage Step | |
| 193 | * +-----------------+--------------+--------------+ |
| 194 | * | 0.5V to 0.58V | 0xA to 0xE | 20mV | |
| 195 | * | 0.6V to 1.095V | 0xF to 0x72 | 5mV | |
| 196 | * | 1.1V to 1.65V | 0x73 to 0xAA | 10mV | |
| 197 | * | 1.6V to 3.3V | 0xAB to 0xFD | 20mV | |
| 198 | * |
| 199 | */ |
| 200 | if (uV >= 1660000) |
| 201 | return (uV - 1660000) / 20000 + 0xAB; |
| 202 | else if (uV >= 1100000) |
| 203 | return (uV - 1100000) / 10000 + 0x73; |
| 204 | else if (uV >= 600000) |
| 205 | return (uV - 600000) / 5000 + 0x0F; |
| 206 | else if (uV >= 500000) |
| 207 | return (uV - 500000) / 20000 + 0x0A; |
| 208 | else |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
| 212 | static int tps65224_buck_val2volt(int idx, int val) |
| 213 | { |
| 214 | /* This function does the opposite to the tps65224_buck_volt2val function |
| 215 | * described above. |
| 216 | * This maps the VSET value to micro volts. Please refer to the ranges |
| 217 | * mentioned the comments of tps65224_buck_volt2val. |
| 218 | */ |
| 219 | |
| 220 | if (idx > 0) { |
| 221 | if (val > TPS65224_BUCK234_VOLT_MAX_HEX) |
| 222 | return -EINVAL; |
| 223 | else if (val >= 0x1B) |
| 224 | return 1200000 + (val - 0x1B) * 50000; |
| 225 | else if (val >= 0x00) |
| 226 | return 500000 + (val - 0x00) * 25000; |
| 227 | else |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | if (val > TPS65224_BUCK1_VOLT_MAX_HEX) |
| 232 | return -EINVAL; |
| 233 | else if (val >= 0xAB) |
| 234 | return 1660000 + (val - 0xAB) * 20000; |
| 235 | else if (val >= 0x73) |
| 236 | return 1100000 + (val - 0x73) * 10000; |
| 237 | else if (val >= 0xF) |
| 238 | return 600000 + (val - 0xF) * 5000; |
| 239 | else if (val >= 0xA) |
| 240 | return 500000 + (val - 0xA) * 20000; |
| 241 | else |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | int tps65224_lookup_slew(int id) |
| 246 | { |
| 247 | switch (id) { |
| 248 | case 0: |
| 249 | return 10000; |
| 250 | case 1: |
| 251 | return 5000; |
| 252 | case 2: |
| 253 | return 2500; |
| 254 | case 3: |
| 255 | return 1250; |
| 256 | default: |
| 257 | return -1; |
| 258 | } |
| 259 | } |
| 260 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 261 | static const struct tps65941_reg_conv_ops buck_conv_ops[] = { |
| 262 | [TPS65941_BUCK_CONV_OPS_IDX] = { |
| 263 | .volt_mask = TPS65941_BUCK_VOLT_MASK, |
| 264 | .volt2val = tps65941_buck_volt2val, |
| 265 | .val2volt = tps65941_buck_val2volt, |
| 266 | .slew_mask = TP65941_BUCK_CONF_SLEW_MASK, |
| 267 | .lookup_slew = tps65941_lookup_slew, |
| 268 | }, |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 269 | [TPS65224_BUCK_CONV_OPS_IDX] = { |
| 270 | .volt_mask = TPS65941_BUCK_VOLT_MASK, |
| 271 | .volt2val = tps65224_buck_volt2val, |
| 272 | .val2volt = tps65224_buck_val2volt, |
| 273 | .slew_mask = TPS65224_BUCK_CONF_SLEW_MASK, |
| 274 | .lookup_slew = tps65224_lookup_slew, |
| 275 | }, |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 278 | static int tps65941_buck_val(struct udevice *dev, int op, int *uV) |
| 279 | { |
| 280 | unsigned int hex, adr; |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 281 | int ret, delta, uwait, slew, idx; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 282 | struct dm_regulator_uclass_plat *uc_pdata; |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 283 | const struct tps65941_reg_conv_ops *conv_ops; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 284 | ulong chip_id; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 285 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 286 | idx = dev->driver_data; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 287 | chip_id = tps65941_get_chip_id(dev); |
| 288 | if (chip_id == TPS65224) { |
| 289 | /* idx is the buck id number as per devicetree node which will be same |
| 290 | * as the regulator name in the datasheet. |
| 291 | * The idx for buck1. buck2, buck3, buck4, buck12 will be 1, 2, 3, 4 |
| 292 | * and 12 respectively. |
| 293 | * In the driver the numbering is from 0. Hence the -1. |
| 294 | */ |
| 295 | idx = (idx == TPS65941_BUCK_ID_12) ? 0 : (idx - 1); |
| 296 | conv_ops = &buck_conv_ops[TPS65224_BUCK_CONV_OPS_IDX]; |
| 297 | } else { |
| 298 | conv_ops = &buck_conv_ops[TPS65941_BUCK_CONV_OPS_IDX]; |
| 299 | } |
| 300 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 301 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 302 | |
| 303 | if (op == PMIC_OP_GET) |
| 304 | *uV = 0; |
| 305 | |
| 306 | adr = uc_pdata->volt_reg; |
| 307 | |
| 308 | ret = pmic_reg_read(dev->parent, adr); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 312 | ret &= conv_ops->volt_mask; |
| 313 | ret = conv_ops->val2volt(idx, ret); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 314 | if (ret < 0) |
| 315 | return ret; |
| 316 | |
| 317 | if (op == PMIC_OP_GET) { |
| 318 | *uV = ret; |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Compute the delta voltage, find the slew rate and wait |
| 324 | * for the appropriate amount of time after voltage switch |
| 325 | */ |
| 326 | if (*uV > ret) |
| 327 | delta = *uV - ret; |
| 328 | else |
| 329 | delta = ret - *uV; |
| 330 | |
| 331 | slew = pmic_reg_read(dev->parent, uc_pdata->ctrl_reg + 1); |
| 332 | if (slew < 0) |
| 333 | return ret; |
| 334 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 335 | slew &= conv_ops->slew_mask; |
| 336 | slew = conv_ops->lookup_slew(slew); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 337 | if (slew <= 0) |
| 338 | return ret; |
| 339 | |
| 340 | uwait = delta / slew; |
| 341 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 342 | hex = conv_ops->volt2val(idx, *uV); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 343 | if (hex < 0) |
| 344 | return hex; |
| 345 | |
| 346 | ret &= 0x0; |
| 347 | ret = hex; |
| 348 | |
| 349 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 350 | |
| 351 | udelay(uwait); |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int tps65941_ldo_enable(struct udevice *dev, int op, bool *enable) |
| 357 | { |
| 358 | int ret; |
| 359 | unsigned int adr; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 360 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 361 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 362 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 363 | adr = uc_pdata->ctrl_reg; |
| 364 | |
| 365 | ret = pmic_reg_read(dev->parent, adr); |
| 366 | if (ret < 0) |
| 367 | return ret; |
| 368 | |
| 369 | if (op == PMIC_OP_GET) { |
| 370 | ret &= TPS65941_LDO_MODE_MASK; |
| 371 | |
| 372 | if (ret) |
| 373 | *enable = true; |
| 374 | else |
| 375 | *enable = false; |
| 376 | |
| 377 | return 0; |
| 378 | } else if (op == PMIC_OP_SET) { |
| 379 | if (*enable) |
| 380 | ret |= TPS65941_LDO_MODE_MASK; |
| 381 | else |
| 382 | ret &= ~TPS65941_LDO_MODE_MASK; |
| 383 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 384 | if (ret) |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 391 | static int tps65941_ldo_val2volt(__maybe_unused int idx, int val) |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 392 | { |
| 393 | if (val > TPS65941_LDO_VOLT_MAX_HEX || val < TPS65941_LDO_VOLT_MIN_HEX) |
| 394 | return -EINVAL; |
| 395 | else if (val >= TPS65941_LDO_VOLT_MIN_HEX) |
| 396 | return 600000 + (val - TPS65941_LDO_VOLT_MIN_HEX) * 50000; |
| 397 | else |
| 398 | return -EINVAL; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int tps65224_ldo_volt2val(int idx, int uV) |
| 402 | { |
| 403 | int base = TPS65224_LDO1_VOLT_MIN; |
| 404 | int max = TPS65224_LDO1_VOLT_MAX; |
| 405 | int offset = TPS65224_LDO1_VOLT_MIN_HEX; |
| 406 | int step = TPS65224_LDO_STEP; |
| 407 | |
| 408 | if (idx > 0) { |
| 409 | base = TPS65224_LDO23_VOLT_MIN; |
| 410 | max = TPS65224_LDO23_VOLT_MAX; |
| 411 | offset = TPS65224_LDO23_VOLT_MIN_HEX; |
| 412 | } |
| 413 | |
| 414 | if (uV > max) |
| 415 | return -EINVAL; |
| 416 | else if (uV >= base) |
| 417 | return (uV - base) / step + offset; |
| 418 | else |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | static int tps65224_ldo_val2volt(int idx, int val) |
| 423 | { |
| 424 | int reg_base = TPS65224_LDO1_VOLT_MIN_HEX; |
| 425 | int reg_max = TPS65224_LDO1_VOLT_MAX_HEX; |
| 426 | int base = TPS65224_LDO1_VOLT_MIN; |
| 427 | int max = TPS65224_LDO1_VOLT_MAX; |
| 428 | int step = TPS65224_LDO_STEP; |
| 429 | /* In LDOx_VOUT reg the BIT0 is reserved and the |
| 430 | * vout value is stored from BIT1 to BIT7. |
| 431 | * Hence the below bit shit is done. |
| 432 | */ |
| 433 | int mask = TPS65224_LDO_VOLT_MASK >> 1; |
| 434 | |
| 435 | if (idx > 0) { |
| 436 | base = TPS65224_LDO23_VOLT_MIN; |
| 437 | max = TPS65224_LDO23_VOLT_MAX; |
| 438 | reg_base = TPS65224_LDO23_VOLT_MIN_HEX; |
| 439 | reg_max = TPS65224_LDO23_VOLT_MAX_HEX; |
| 440 | } |
| 441 | |
| 442 | /* The VSET register of LDO has its 0th bit as reserved |
| 443 | * hence shifting the value to right by 1 bit. |
| 444 | */ |
| 445 | val = val >> 1; |
| 446 | |
| 447 | if (val < 0 || val > mask) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | if (val <= reg_base) |
| 451 | return base; |
| 452 | |
| 453 | if (val >= reg_max) |
| 454 | return max; |
| 455 | |
| 456 | return base + (step * (val - reg_base)); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 457 | } |
| 458 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 459 | static const struct tps65941_reg_conv_ops ldo_conv_ops[] = { |
| 460 | [TPS65941_LDO_CONV_OPS_IDX] = { |
| 461 | .volt_mask = TPS65941_LDO_VOLT_MASK, |
| 462 | .volt2val = tps65941_buck_volt2val, |
| 463 | .val2volt = tps65941_ldo_val2volt, |
| 464 | }, |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 465 | [TPS65224_LDO_CONV_OPS_IDX] = { |
| 466 | .volt_mask = TPS65224_LDO_VOLT_MASK, |
| 467 | .volt2val = tps65224_ldo_volt2val, |
| 468 | .val2volt = tps65224_ldo_val2volt, |
| 469 | }, |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 472 | static int tps65941_ldo_val(struct udevice *dev, int op, int *uV) |
| 473 | { |
| 474 | unsigned int hex, adr; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 475 | int ret, ret_volt, idx; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 476 | struct dm_regulator_uclass_plat *uc_pdata; |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 477 | const struct tps65941_reg_conv_ops *conv_ops; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 478 | ulong chip_id; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 479 | |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 480 | chip_id = tps65941_get_chip_id(dev); |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 481 | idx = dev->driver_data; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 482 | if (chip_id == TPS65224) { |
| 483 | /* idx is the ldo id number as per devicetree node which will be same |
| 484 | * as the regulator name in the datasheet. |
| 485 | * The idx for ldo1, ldo2, ldo3 will be 1, 2 & 3 respectively. |
| 486 | * In the driver the numbering is from 0. Hence the -1. |
| 487 | */ |
| 488 | idx = idx - 1; |
| 489 | conv_ops = &ldo_conv_ops[TPS65224_LDO_CONV_OPS_IDX]; |
| 490 | } else { |
| 491 | conv_ops = &ldo_conv_ops[TPS65941_LDO_CONV_OPS_IDX]; |
| 492 | } |
| 493 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 494 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 495 | |
| 496 | if (op == PMIC_OP_GET) |
| 497 | *uV = 0; |
| 498 | |
| 499 | adr = uc_pdata->volt_reg; |
| 500 | |
| 501 | ret = pmic_reg_read(dev->parent, adr); |
| 502 | if (ret < 0) |
| 503 | return ret; |
| 504 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 505 | ret &= conv_ops->volt_mask; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 506 | ret_volt = conv_ops->val2volt(idx, ret); |
| 507 | if (ret_volt < 0) |
| 508 | return ret_volt; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 509 | |
| 510 | if (op == PMIC_OP_GET) { |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 511 | *uV = ret_volt; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 515 | /* TPS65224 LDO1 in BYPASS mode only supports 2.2V min to 3.6V max */ |
| 516 | if (chip_id == TPS65224 && idx == 0 && (ret & BIT(TPS65224_LDO_BYP_CONFIG)) && |
| 517 | *uV < TPS65224_LDO1_VOLT_BYP_MIN) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | /* TPS65224 LDO2 & LDO3 in BYPASS mode supports 1.5V min to 5.5V max */ |
| 521 | if (chip_id == TPS65224 && idx > 0 && (ret & BIT(TPS65224_LDO_BYP_CONFIG)) && |
| 522 | *uV < TPS65224_LDO23_VOLT_BYP_MIN) |
| 523 | return -EINVAL; |
| 524 | |
Bhargav Raviprakash | 096829d | 2024-04-22 09:49:01 +0000 | [diff] [blame] | 525 | hex = conv_ops->volt2val(idx, *uV); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 526 | if (hex < 0) |
| 527 | return hex; |
| 528 | |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 529 | if (chip_id == TPS65224) { |
| 530 | hex = hex << TPS65941_LDO_MODE_MASK; |
| 531 | ret &= ~TPS65224_LDO_VOLT_MASK; |
| 532 | ret |= hex; |
| 533 | } else { |
| 534 | ret = hex; |
| 535 | } |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 536 | |
| 537 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | static int tps65941_ldo_probe(struct udevice *dev) |
| 543 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 544 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 545 | int idx; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 546 | ulong chip_id; |
| 547 | |
| 548 | chip_id = tps65941_get_chip_id(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 549 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 550 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 551 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 552 | |
| 553 | idx = dev->driver_data; |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 554 | switch (idx) { |
| 555 | case TPS65941_LDO_ID_1: |
| 556 | case TPS65941_LDO_ID_2: |
| 557 | case TPS65941_LDO_ID_3: |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 558 | debug("Single phase regulator\n"); |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 559 | break; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 560 | case TPS65941_LDO_ID_4: |
| 561 | if (chip_id != TPS65224) { |
| 562 | debug("Single phase regulator\n"); |
| 563 | break; |
| 564 | } |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 565 | default: |
| 566 | pr_err("Wrong ID for regulator\n"); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | } |
| 569 | |
| 570 | uc_pdata->ctrl_reg = tps65941_ldo_ctrl[idx - 1]; |
| 571 | uc_pdata->volt_reg = tps65941_ldo_vout[idx - 1]; |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int tps65941_buck_probe(struct udevice *dev) |
| 577 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 578 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 579 | int idx; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 580 | ulong chip_id; |
| 581 | |
| 582 | chip_id = tps65941_get_chip_id(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 583 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 584 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 585 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 586 | |
| 587 | idx = dev->driver_data; |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 588 | switch (idx) { |
| 589 | case TPS65941_BUCK_ID_1: |
| 590 | case TPS65941_BUCK_ID_2: |
| 591 | case TPS65941_BUCK_ID_3: |
| 592 | case TPS65941_BUCK_ID_4: |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 593 | debug("Single phase regulator\n"); |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 594 | break; |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 595 | case TPS65941_BUCK_ID_5: |
| 596 | if (chip_id != TPS65224) { |
| 597 | debug("Single phase regulator\n"); |
| 598 | } else { |
| 599 | pr_err("Wrong ID for regulator\n"); |
| 600 | return -EINVAL; |
| 601 | } |
| 602 | break; |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 603 | case TPS65941_BUCK_ID_12: |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 604 | idx = 1; |
| 605 | break; |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 606 | case TPS65941_BUCK_ID_123: |
| 607 | case TPS65941_BUCK_ID_1234: |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 608 | if (chip_id != TPS65224) { |
| 609 | idx = 1; |
| 610 | } else { |
| 611 | pr_err("Wrong ID for regulator\n"); |
| 612 | return -EINVAL; |
| 613 | } |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 614 | break; |
| 615 | case TPS65941_BUCK_ID_34: |
Bhargav Raviprakash | 1204572 | 2024-04-22 09:49:09 +0000 | [diff] [blame] | 616 | if (chip_id != TPS65224) { |
| 617 | idx = 3; |
| 618 | } else { |
| 619 | pr_err("Wrong ID for regulator\n"); |
| 620 | return -EINVAL; |
| 621 | } |
Bhargav Raviprakash | 8349149 | 2024-04-22 09:48:53 +0000 | [diff] [blame] | 622 | break; |
| 623 | default: |
| 624 | pr_err("Wrong ID for regulator\n"); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 625 | return -EINVAL; |
| 626 | } |
| 627 | |
| 628 | uc_pdata->ctrl_reg = tps65941_buck_ctrl[idx - 1]; |
| 629 | uc_pdata->volt_reg = tps65941_buck_vout[idx - 1]; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int ldo_get_value(struct udevice *dev) |
| 635 | { |
| 636 | int uV; |
| 637 | int ret; |
| 638 | |
| 639 | ret = tps65941_ldo_val(dev, PMIC_OP_GET, &uV); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | |
| 643 | return uV; |
| 644 | } |
| 645 | |
| 646 | static int ldo_set_value(struct udevice *dev, int uV) |
| 647 | { |
| 648 | return tps65941_ldo_val(dev, PMIC_OP_SET, &uV); |
| 649 | } |
| 650 | |
| 651 | static int ldo_get_enable(struct udevice *dev) |
| 652 | { |
| 653 | bool enable = false; |
| 654 | int ret; |
| 655 | |
| 656 | ret = tps65941_ldo_enable(dev, PMIC_OP_GET, &enable); |
| 657 | if (ret) |
| 658 | return ret; |
| 659 | |
| 660 | return enable; |
| 661 | } |
| 662 | |
| 663 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 664 | { |
| 665 | return tps65941_ldo_enable(dev, PMIC_OP_SET, &enable); |
| 666 | } |
| 667 | |
| 668 | static int buck_get_value(struct udevice *dev) |
| 669 | { |
| 670 | int uV; |
| 671 | int ret; |
| 672 | |
| 673 | ret = tps65941_buck_val(dev, PMIC_OP_GET, &uV); |
| 674 | if (ret) |
| 675 | return ret; |
| 676 | |
| 677 | return uV; |
| 678 | } |
| 679 | |
| 680 | static int buck_set_value(struct udevice *dev, int uV) |
| 681 | { |
| 682 | return tps65941_buck_val(dev, PMIC_OP_SET, &uV); |
| 683 | } |
| 684 | |
| 685 | static int buck_get_enable(struct udevice *dev) |
| 686 | { |
| 687 | bool enable = false; |
| 688 | int ret; |
| 689 | |
| 690 | ret = tps65941_buck_enable(dev, PMIC_OP_GET, &enable); |
| 691 | if (ret) |
| 692 | return ret; |
| 693 | |
| 694 | return enable; |
| 695 | } |
| 696 | |
| 697 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 698 | { |
| 699 | return tps65941_buck_enable(dev, PMIC_OP_SET, &enable); |
| 700 | } |
| 701 | |
| 702 | static const struct dm_regulator_ops tps65941_ldo_ops = { |
| 703 | .get_value = ldo_get_value, |
| 704 | .set_value = ldo_set_value, |
| 705 | .get_enable = ldo_get_enable, |
| 706 | .set_enable = ldo_set_enable, |
| 707 | }; |
| 708 | |
| 709 | U_BOOT_DRIVER(tps65941_ldo) = { |
| 710 | .name = TPS65941_LDO_DRIVER, |
| 711 | .id = UCLASS_REGULATOR, |
| 712 | .ops = &tps65941_ldo_ops, |
| 713 | .probe = tps65941_ldo_probe, |
| 714 | }; |
| 715 | |
| 716 | static const struct dm_regulator_ops tps65941_buck_ops = { |
| 717 | .get_value = buck_get_value, |
| 718 | .set_value = buck_set_value, |
| 719 | .get_enable = buck_get_enable, |
| 720 | .set_enable = buck_set_enable, |
| 721 | }; |
| 722 | |
| 723 | U_BOOT_DRIVER(tps65941_buck) = { |
| 724 | .name = TPS65941_BUCK_DRIVER, |
| 725 | .id = UCLASS_REGULATOR, |
| 726 | .ops = &tps65941_buck_ops, |
| 727 | .probe = tps65941_buck_probe, |
| 728 | }; |