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 | |
| 9 | #include <common.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <errno.h> |
| 12 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 15 | #include <power/pmic.h> |
| 16 | #include <power/regulator.h> |
| 17 | #include <power/tps65941.h> |
| 18 | |
| 19 | static const char tps65941_buck_ctrl[TPS65941_BUCK_NUM] = {0x4, 0x6, 0x8, 0xA, |
| 20 | 0xC}; |
| 21 | static const char tps65941_buck_vout[TPS65941_BUCK_NUM] = {0xE, 0x10, 0x12, |
| 22 | 0x14, 0x16}; |
| 23 | static const char tps65941_ldo_ctrl[TPS65941_BUCK_NUM] = {0x1D, 0x1E, 0x1F, |
| 24 | 0x20}; |
| 25 | static const char tps65941_ldo_vout[TPS65941_BUCK_NUM] = {0x23, 0x24, 0x25, |
| 26 | 0x26}; |
| 27 | |
| 28 | static int tps65941_buck_enable(struct udevice *dev, int op, bool *enable) |
| 29 | { |
| 30 | int ret; |
| 31 | unsigned int adr; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 32 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 33 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 34 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 35 | adr = uc_pdata->ctrl_reg; |
| 36 | |
| 37 | ret = pmic_reg_read(dev->parent, adr); |
| 38 | if (ret < 0) |
| 39 | return ret; |
| 40 | |
| 41 | if (op == PMIC_OP_GET) { |
| 42 | ret &= TPS65941_BUCK_MODE_MASK; |
| 43 | |
| 44 | if (ret) |
| 45 | *enable = true; |
| 46 | else |
| 47 | *enable = false; |
| 48 | |
| 49 | return 0; |
| 50 | } else if (op == PMIC_OP_SET) { |
| 51 | if (*enable) |
| 52 | ret |= TPS65941_BUCK_MODE_MASK; |
| 53 | else |
| 54 | ret &= ~TPS65941_BUCK_MODE_MASK; |
| 55 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int tps65941_buck_volt2val(int uV) |
| 64 | { |
| 65 | if (uV > TPS65941_BUCK_VOLT_MAX) |
| 66 | return -EINVAL; |
| 67 | else if (uV > 1650000) |
| 68 | return (uV - 1660000) / 20000 + 0xAB; |
| 69 | else if (uV > 1110000) |
| 70 | return (uV - 1110000) / 10000 + 0x73; |
| 71 | else if (uV > 600000) |
| 72 | return (uV - 600000) / 5000 + 0x0F; |
| 73 | else if (uV >= 300000) |
| 74 | return (uV - 300000) / 20000 + 0x00; |
| 75 | else |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | static int tps65941_buck_val2volt(int val) |
| 80 | { |
| 81 | if (val > TPS65941_BUCK_VOLT_MAX_HEX) |
| 82 | return -EINVAL; |
| 83 | else if (val > 0xAB) |
| 84 | return 1660000 + (val - 0xAB) * 20000; |
| 85 | else if (val > 0x73) |
| 86 | return 1100000 + (val - 0x73) * 10000; |
| 87 | else if (val > 0xF) |
| 88 | return 600000 + (val - 0xF) * 5000; |
| 89 | else if (val >= 0x0) |
| 90 | return 300000 + val * 5000; |
| 91 | else |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
| 95 | int tps65941_lookup_slew(int id) |
| 96 | { |
| 97 | switch (id) { |
| 98 | case 0: |
| 99 | return 33000; |
| 100 | case 1: |
| 101 | return 20000; |
| 102 | case 2: |
| 103 | return 10000; |
| 104 | case 3: |
| 105 | return 5000; |
| 106 | case 4: |
| 107 | return 2500; |
| 108 | case 5: |
| 109 | return 1300; |
| 110 | case 6: |
| 111 | return 630; |
| 112 | case 7: |
| 113 | return 310; |
| 114 | default: |
| 115 | return -1; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static int tps65941_buck_val(struct udevice *dev, int op, int *uV) |
| 120 | { |
| 121 | unsigned int hex, adr; |
| 122 | int ret, delta, uwait, slew; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 123 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 124 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 125 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 126 | |
| 127 | if (op == PMIC_OP_GET) |
| 128 | *uV = 0; |
| 129 | |
| 130 | adr = uc_pdata->volt_reg; |
| 131 | |
| 132 | ret = pmic_reg_read(dev->parent, adr); |
| 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
| 136 | ret &= TPS65941_BUCK_VOLT_MASK; |
| 137 | ret = tps65941_buck_val2volt(ret); |
| 138 | if (ret < 0) |
| 139 | return ret; |
| 140 | |
| 141 | if (op == PMIC_OP_GET) { |
| 142 | *uV = ret; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Compute the delta voltage, find the slew rate and wait |
| 148 | * for the appropriate amount of time after voltage switch |
| 149 | */ |
| 150 | if (*uV > ret) |
| 151 | delta = *uV - ret; |
| 152 | else |
| 153 | delta = ret - *uV; |
| 154 | |
| 155 | slew = pmic_reg_read(dev->parent, uc_pdata->ctrl_reg + 1); |
| 156 | if (slew < 0) |
| 157 | return ret; |
| 158 | |
| 159 | slew &= TP65941_BUCK_CONF_SLEW_MASK; |
| 160 | slew = tps65941_lookup_slew(slew); |
| 161 | if (slew <= 0) |
| 162 | return ret; |
| 163 | |
| 164 | uwait = delta / slew; |
| 165 | |
| 166 | hex = tps65941_buck_volt2val(*uV); |
| 167 | if (hex < 0) |
| 168 | return hex; |
| 169 | |
| 170 | ret &= 0x0; |
| 171 | ret = hex; |
| 172 | |
| 173 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 174 | |
| 175 | udelay(uwait); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static int tps65941_ldo_enable(struct udevice *dev, int op, bool *enable) |
| 181 | { |
| 182 | int ret; |
| 183 | unsigned int adr; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 184 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 185 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 186 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 187 | adr = uc_pdata->ctrl_reg; |
| 188 | |
| 189 | ret = pmic_reg_read(dev->parent, adr); |
| 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | |
| 193 | if (op == PMIC_OP_GET) { |
| 194 | ret &= TPS65941_LDO_MODE_MASK; |
| 195 | |
| 196 | if (ret) |
| 197 | *enable = true; |
| 198 | else |
| 199 | *enable = false; |
| 200 | |
| 201 | return 0; |
| 202 | } else if (op == PMIC_OP_SET) { |
| 203 | if (*enable) |
| 204 | ret |= TPS65941_LDO_MODE_MASK; |
| 205 | else |
| 206 | ret &= ~TPS65941_LDO_MODE_MASK; |
| 207 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int tps65941_ldo_val2volt(int val) |
| 216 | { |
| 217 | if (val > TPS65941_LDO_VOLT_MAX_HEX || val < TPS65941_LDO_VOLT_MIN_HEX) |
| 218 | return -EINVAL; |
| 219 | else if (val >= TPS65941_LDO_VOLT_MIN_HEX) |
| 220 | return 600000 + (val - TPS65941_LDO_VOLT_MIN_HEX) * 50000; |
| 221 | else |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | static int tps65941_ldo_val(struct udevice *dev, int op, int *uV) |
| 226 | { |
| 227 | unsigned int hex, adr; |
| 228 | int ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 229 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 230 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 231 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 232 | |
| 233 | if (op == PMIC_OP_GET) |
| 234 | *uV = 0; |
| 235 | |
| 236 | adr = uc_pdata->volt_reg; |
| 237 | |
| 238 | ret = pmic_reg_read(dev->parent, adr); |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
| 242 | ret &= TPS65941_LDO_VOLT_MASK; |
| 243 | ret = tps65941_ldo_val2volt(ret); |
| 244 | if (ret < 0) |
| 245 | return ret; |
| 246 | |
| 247 | if (op == PMIC_OP_GET) { |
| 248 | *uV = ret; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | hex = tps65941_buck_volt2val(*uV); |
| 253 | if (hex < 0) |
| 254 | return hex; |
| 255 | |
| 256 | ret &= 0x0; |
| 257 | ret = hex; |
| 258 | |
| 259 | ret = pmic_reg_write(dev->parent, adr, ret); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int tps65941_ldo_probe(struct udevice *dev) |
| 265 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 266 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 267 | int idx; |
| 268 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 269 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 270 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 271 | |
| 272 | idx = dev->driver_data; |
| 273 | if (idx == 1 || idx == 2 || idx == 3 || idx == 4) { |
| 274 | debug("Single phase regulator\n"); |
| 275 | } else { |
| 276 | printf("Wrong ID for regulator\n"); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | uc_pdata->ctrl_reg = tps65941_ldo_ctrl[idx - 1]; |
| 281 | uc_pdata->volt_reg = tps65941_ldo_vout[idx - 1]; |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int tps65941_buck_probe(struct udevice *dev) |
| 287 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 288 | struct dm_regulator_uclass_plat *uc_pdata; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 289 | int idx; |
| 290 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 291 | uc_pdata = dev_get_uclass_plat(dev); |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 292 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 293 | |
| 294 | idx = dev->driver_data; |
| 295 | if (idx == 1 || idx == 2 || idx == 3 || idx == 4 || idx == 5) { |
| 296 | debug("Single phase regulator\n"); |
| 297 | } else if (idx == 12) { |
| 298 | idx = 1; |
| 299 | } else if (idx == 34) { |
| 300 | idx = 3; |
Sinthu Raja | 5f31d4a | 2022-02-09 15:06:47 +0530 | [diff] [blame] | 301 | } else if (idx == 123) { |
| 302 | idx = 1; |
Keerthy | 4a03000 | 2019-10-24 15:00:51 +0530 | [diff] [blame] | 303 | } else if (idx == 1234) { |
| 304 | idx = 1; |
| 305 | } else { |
| 306 | printf("Wrong ID for regulator\n"); |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | uc_pdata->ctrl_reg = tps65941_buck_ctrl[idx - 1]; |
| 311 | uc_pdata->volt_reg = tps65941_buck_vout[idx - 1]; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int ldo_get_value(struct udevice *dev) |
| 317 | { |
| 318 | int uV; |
| 319 | int ret; |
| 320 | |
| 321 | ret = tps65941_ldo_val(dev, PMIC_OP_GET, &uV); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | return uV; |
| 326 | } |
| 327 | |
| 328 | static int ldo_set_value(struct udevice *dev, int uV) |
| 329 | { |
| 330 | return tps65941_ldo_val(dev, PMIC_OP_SET, &uV); |
| 331 | } |
| 332 | |
| 333 | static int ldo_get_enable(struct udevice *dev) |
| 334 | { |
| 335 | bool enable = false; |
| 336 | int ret; |
| 337 | |
| 338 | ret = tps65941_ldo_enable(dev, PMIC_OP_GET, &enable); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
| 342 | return enable; |
| 343 | } |
| 344 | |
| 345 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 346 | { |
| 347 | return tps65941_ldo_enable(dev, PMIC_OP_SET, &enable); |
| 348 | } |
| 349 | |
| 350 | static int buck_get_value(struct udevice *dev) |
| 351 | { |
| 352 | int uV; |
| 353 | int ret; |
| 354 | |
| 355 | ret = tps65941_buck_val(dev, PMIC_OP_GET, &uV); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | |
| 359 | return uV; |
| 360 | } |
| 361 | |
| 362 | static int buck_set_value(struct udevice *dev, int uV) |
| 363 | { |
| 364 | return tps65941_buck_val(dev, PMIC_OP_SET, &uV); |
| 365 | } |
| 366 | |
| 367 | static int buck_get_enable(struct udevice *dev) |
| 368 | { |
| 369 | bool enable = false; |
| 370 | int ret; |
| 371 | |
| 372 | ret = tps65941_buck_enable(dev, PMIC_OP_GET, &enable); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | return enable; |
| 377 | } |
| 378 | |
| 379 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 380 | { |
| 381 | return tps65941_buck_enable(dev, PMIC_OP_SET, &enable); |
| 382 | } |
| 383 | |
| 384 | static const struct dm_regulator_ops tps65941_ldo_ops = { |
| 385 | .get_value = ldo_get_value, |
| 386 | .set_value = ldo_set_value, |
| 387 | .get_enable = ldo_get_enable, |
| 388 | .set_enable = ldo_set_enable, |
| 389 | }; |
| 390 | |
| 391 | U_BOOT_DRIVER(tps65941_ldo) = { |
| 392 | .name = TPS65941_LDO_DRIVER, |
| 393 | .id = UCLASS_REGULATOR, |
| 394 | .ops = &tps65941_ldo_ops, |
| 395 | .probe = tps65941_ldo_probe, |
| 396 | }; |
| 397 | |
| 398 | static const struct dm_regulator_ops tps65941_buck_ops = { |
| 399 | .get_value = buck_get_value, |
| 400 | .set_value = buck_set_value, |
| 401 | .get_enable = buck_get_enable, |
| 402 | .set_enable = buck_set_enable, |
| 403 | }; |
| 404 | |
| 405 | U_BOOT_DRIVER(tps65941_buck) = { |
| 406 | .name = TPS65941_BUCK_DRIVER, |
| 407 | .id = UCLASS_REGULATOR, |
| 408 | .ops = &tps65941_buck_ops, |
| 409 | .probe = tps65941_buck_probe, |
| 410 | }; |