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