Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2015 Samsung Electronics |
| 3 | * |
| 4 | * Rajeshwari Shinde <rajeshwari.s@samsung.com> |
| 5 | * Przemyslaw Marczak <p.marczak@samsung.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <fdtdec.h> |
| 12 | #include <errno.h> |
| 13 | #include <dm.h> |
| 14 | #include <i2c.h> |
| 15 | #include <power/pmic.h> |
| 16 | #include <power/regulator.h> |
| 17 | #include <power/max77686_pmic.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | #define MODE(_id, _val, _name) { \ |
| 22 | .id = _id, \ |
| 23 | .register_value = _val, \ |
| 24 | .name = _name, \ |
| 25 | } |
| 26 | |
| 27 | /* LDO: 1,3,4,5,9,17,18,19,20,21,22,23,24,26,26,27 */ |
| 28 | static struct dm_regulator_mode max77686_ldo_mode_standby1[] = { |
| 29 | MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"), |
| 30 | MODE(OPMODE_LPM, MAX77686_LDO_MODE_LPM, "LPM"), |
| 31 | MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"), |
| 32 | MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"), |
| 33 | }; |
| 34 | |
| 35 | /* LDO: 2,6,7,8,10,11,12,14,15,16 */ |
| 36 | static struct dm_regulator_mode max77686_ldo_mode_standby2[] = { |
| 37 | MODE(OPMODE_OFF, MAX77686_LDO_MODE_OFF, "OFF"), |
| 38 | MODE(OPMODE_STANDBY, MAX77686_LDO_MODE_STANDBY, "ON/OFF"), |
| 39 | MODE(OPMODE_STANDBY_LPM, MAX77686_LDO_MODE_STANDBY_LPM, "ON/LPM"), |
| 40 | MODE(OPMODE_ON, MAX77686_LDO_MODE_ON, "ON"), |
| 41 | }; |
| 42 | |
| 43 | /* Buck: 1 */ |
| 44 | static struct dm_regulator_mode max77686_buck_mode_standby[] = { |
| 45 | MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"), |
| 46 | MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"), |
| 47 | MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"), |
| 48 | }; |
| 49 | |
| 50 | /* Buck: 2,3,4 */ |
| 51 | static struct dm_regulator_mode max77686_buck_mode_lpm[] = { |
| 52 | MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"), |
| 53 | MODE(OPMODE_STANDBY, MAX77686_BUCK_MODE_STANDBY, "ON/OFF"), |
| 54 | MODE(OPMODE_LPM, MAX77686_BUCK_MODE_LPM, "LPM"), |
| 55 | MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"), |
| 56 | }; |
| 57 | |
| 58 | /* Buck: 5,6,7,8,9 */ |
| 59 | static struct dm_regulator_mode max77686_buck_mode_onoff[] = { |
| 60 | MODE(OPMODE_OFF, MAX77686_BUCK_MODE_OFF, "OFF"), |
| 61 | MODE(OPMODE_ON, MAX77686_BUCK_MODE_ON, "ON"), |
| 62 | }; |
| 63 | |
Simon Glass | 2ede005 | 2015-07-02 18:16:04 -0600 | [diff] [blame] | 64 | static const char max77686_buck_ctrl[] = { |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 65 | 0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38 |
| 66 | }; |
| 67 | |
Simon Glass | 2ede005 | 2015-07-02 18:16:04 -0600 | [diff] [blame] | 68 | static const char max77686_buck_out[] = { |
| 69 | 0xff, 0x11, 0x14, 0x1e, 0x28, 0x31, 0x33, 0x35, 0x37, 0x39 |
| 70 | }; |
| 71 | |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 72 | static int max77686_buck_volt2hex(int buck, int uV) |
| 73 | { |
| 74 | unsigned int hex = 0; |
| 75 | unsigned int hex_max = 0; |
| 76 | |
| 77 | switch (buck) { |
| 78 | case 2: |
| 79 | case 3: |
| 80 | case 4: |
| 81 | /* hex = (uV - 600000) / 12500; */ |
| 82 | hex = (uV - MAX77686_BUCK_UV_LMIN) / MAX77686_BUCK_UV_LSTEP; |
| 83 | hex_max = MAX77686_BUCK234_VOLT_MAX_HEX; |
Simon Glass | fdcc59b | 2015-08-03 08:19:25 -0600 | [diff] [blame] | 84 | break; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 85 | default: |
Simon Glass | fdcc59b | 2015-08-03 08:19:25 -0600 | [diff] [blame] | 86 | /* |
| 87 | * hex = (uV - 750000) / 50000. We assume that dynamic voltage |
| 88 | * scaling via GPIOs is not enabled and don't support that. |
| 89 | * If this is enabled then the driver will need to take that |
Simon Glass | 7fc93c4 | 2015-08-09 09:10:57 -0600 | [diff] [blame] | 90 | * into account and check different registers depending on |
| 91 | * the current setting. See the datasheet for details. |
Simon Glass | fdcc59b | 2015-08-03 08:19:25 -0600 | [diff] [blame] | 92 | */ |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 93 | hex = (uV - MAX77686_BUCK_UV_HMIN) / MAX77686_BUCK_UV_HSTEP; |
| 94 | hex_max = MAX77686_BUCK_VOLT_MAX_HEX; |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | if (hex >= 0 && hex <= hex_max) |
| 99 | return hex; |
| 100 | |
| 101 | error("Value: %d uV is wrong for BUCK%d", uV, buck); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | static int max77686_buck_hex2volt(int buck, int hex) |
| 106 | { |
| 107 | unsigned uV = 0; |
| 108 | unsigned int hex_max = 0; |
| 109 | |
| 110 | if (hex < 0) |
| 111 | goto bad_hex; |
| 112 | |
| 113 | switch (buck) { |
| 114 | case 2: |
| 115 | case 3: |
| 116 | case 4: |
| 117 | hex_max = MAX77686_BUCK234_VOLT_MAX_HEX; |
| 118 | if (hex > hex_max) |
| 119 | goto bad_hex; |
| 120 | |
| 121 | /* uV = hex * 12500 + 600000; */ |
| 122 | uV = hex * MAX77686_BUCK_UV_LSTEP + MAX77686_BUCK_UV_LMIN; |
| 123 | break; |
| 124 | default: |
| 125 | hex_max = MAX77686_BUCK_VOLT_MAX_HEX; |
| 126 | if (hex > hex_max) |
| 127 | goto bad_hex; |
| 128 | |
| 129 | /* uV = hex * 50000 + 750000; */ |
| 130 | uV = hex * MAX77686_BUCK_UV_HSTEP + MAX77686_BUCK_UV_HMIN; |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | return uV; |
| 135 | |
| 136 | bad_hex: |
| 137 | error("Value: %#x is wrong for BUCK%d", hex, buck); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | static int max77686_ldo_volt2hex(int ldo, int uV) |
| 142 | { |
| 143 | unsigned int hex = 0; |
| 144 | |
| 145 | switch (ldo) { |
| 146 | case 1: |
| 147 | case 2: |
| 148 | case 6: |
| 149 | case 7: |
| 150 | case 8: |
| 151 | case 15: |
| 152 | hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_LSTEP; |
| 153 | /* hex = (uV - 800000) / 25000; */ |
| 154 | break; |
| 155 | default: |
| 156 | hex = (uV - MAX77686_LDO_UV_MIN) / MAX77686_LDO_UV_HSTEP; |
| 157 | /* hex = (uV - 800000) / 50000; */ |
| 158 | } |
| 159 | |
| 160 | if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX) |
| 161 | return hex; |
| 162 | |
| 163 | error("Value: %d uV is wrong for LDO%d", uV, ldo); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | |
| 167 | static int max77686_ldo_hex2volt(int ldo, int hex) |
| 168 | { |
| 169 | unsigned int uV = 0; |
| 170 | |
| 171 | if (hex > MAX77686_LDO_VOLT_MAX_HEX) |
| 172 | goto bad_hex; |
| 173 | |
| 174 | switch (ldo) { |
| 175 | case 1: |
| 176 | case 2: |
| 177 | case 6: |
| 178 | case 7: |
| 179 | case 8: |
| 180 | case 15: |
| 181 | /* uV = hex * 25000 + 800000; */ |
| 182 | uV = hex * MAX77686_LDO_UV_LSTEP + MAX77686_LDO_UV_MIN; |
| 183 | break; |
| 184 | default: |
| 185 | /* uV = hex * 50000 + 800000; */ |
| 186 | uV = hex * MAX77686_LDO_UV_HSTEP + MAX77686_LDO_UV_MIN; |
| 187 | } |
| 188 | |
| 189 | return uV; |
| 190 | |
| 191 | bad_hex: |
| 192 | error("Value: %#x is wrong for ldo%d", hex, ldo); |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | |
| 196 | static int max77686_ldo_hex2mode(int ldo, int hex) |
| 197 | { |
| 198 | if (hex > MAX77686_LDO_MODE_MASK) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | switch (hex) { |
| 202 | case MAX77686_LDO_MODE_OFF: |
| 203 | return OPMODE_OFF; |
| 204 | case MAX77686_LDO_MODE_LPM: /* == MAX77686_LDO_MODE_STANDBY: */ |
| 205 | /* The same mode values but different meaning for each ldo */ |
| 206 | switch (ldo) { |
| 207 | case 2: |
| 208 | case 6: |
| 209 | case 7: |
| 210 | case 8: |
| 211 | case 10: |
| 212 | case 11: |
| 213 | case 12: |
| 214 | case 14: |
| 215 | case 15: |
| 216 | case 16: |
| 217 | return OPMODE_STANDBY; |
| 218 | default: |
| 219 | return OPMODE_LPM; |
| 220 | } |
| 221 | case MAX77686_LDO_MODE_STANDBY_LPM: |
| 222 | return OPMODE_STANDBY_LPM; |
| 223 | case MAX77686_LDO_MODE_ON: |
| 224 | return OPMODE_ON; |
| 225 | default: |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int max77686_buck_hex2mode(int buck, int hex) |
| 231 | { |
| 232 | if (hex > MAX77686_BUCK_MODE_MASK) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | switch (hex) { |
| 236 | case MAX77686_BUCK_MODE_OFF: |
| 237 | return OPMODE_OFF; |
| 238 | case MAX77686_BUCK_MODE_ON: |
| 239 | return OPMODE_ON; |
| 240 | case MAX77686_BUCK_MODE_STANDBY: |
| 241 | switch (buck) { |
| 242 | case 1: |
| 243 | case 2: |
| 244 | case 3: |
| 245 | case 4: |
| 246 | return OPMODE_STANDBY; |
| 247 | default: |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | case MAX77686_BUCK_MODE_LPM: |
| 251 | switch (buck) { |
| 252 | case 2: |
| 253 | case 3: |
| 254 | case 4: |
| 255 | return OPMODE_LPM; |
| 256 | default: |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | default: |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static int max77686_buck_modes(int buck, struct dm_regulator_mode **modesp) |
| 265 | { |
| 266 | int ret = -EINVAL; |
| 267 | |
| 268 | if (buck < 1 || buck > MAX77686_BUCK_NUM) |
| 269 | return ret; |
| 270 | |
| 271 | switch (buck) { |
| 272 | case 1: |
| 273 | *modesp = max77686_buck_mode_standby; |
| 274 | ret = ARRAY_SIZE(max77686_buck_mode_standby); |
| 275 | break; |
| 276 | case 2: |
| 277 | case 3: |
| 278 | case 4: |
| 279 | *modesp = max77686_buck_mode_lpm; |
| 280 | ret = ARRAY_SIZE(max77686_buck_mode_lpm); |
| 281 | break; |
| 282 | default: |
| 283 | *modesp = max77686_buck_mode_onoff; |
| 284 | ret = ARRAY_SIZE(max77686_buck_mode_onoff); |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int max77686_ldo_modes(int ldo, struct dm_regulator_mode **modesp, |
| 291 | struct udevice *dev) |
| 292 | { |
| 293 | int ret = -EINVAL; |
| 294 | |
| 295 | if (ldo < 1 || ldo > MAX77686_LDO_NUM) |
| 296 | return ret; |
| 297 | |
| 298 | switch (ldo) { |
| 299 | case 2: |
| 300 | case 6: |
| 301 | case 7: |
| 302 | case 8: |
| 303 | case 10: |
| 304 | case 11: |
| 305 | case 12: |
| 306 | case 14: |
| 307 | case 15: |
| 308 | case 16: |
| 309 | *modesp = max77686_ldo_mode_standby2; |
| 310 | ret = ARRAY_SIZE(max77686_ldo_mode_standby2); |
| 311 | break; |
| 312 | default: |
| 313 | *modesp = max77686_ldo_mode_standby1; |
| 314 | ret = ARRAY_SIZE(max77686_ldo_mode_standby1); |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int max77686_ldo_val(struct udevice *dev, int op, int *uV) |
| 321 | { |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 322 | unsigned int hex, adr; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 323 | unsigned char val; |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 324 | int ldo, ret; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 325 | |
| 326 | if (op == PMIC_OP_GET) |
| 327 | *uV = 0; |
| 328 | |
| 329 | ldo = dev->driver_data; |
| 330 | if (ldo < 1 || ldo > MAX77686_LDO_NUM) { |
| 331 | error("Wrong ldo number: %d", ldo); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1; |
| 336 | |
| 337 | ret = pmic_read(dev->parent, adr, &val, 1); |
| 338 | if (ret) |
| 339 | return ret; |
| 340 | |
| 341 | if (op == PMIC_OP_GET) { |
| 342 | val &= MAX77686_LDO_VOLT_MASK; |
| 343 | ret = max77686_ldo_hex2volt(ldo, val); |
| 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | *uV = ret; |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | hex = max77686_ldo_volt2hex(ldo, *uV); |
| 351 | if (hex < 0) |
| 352 | return hex; |
| 353 | |
| 354 | val &= ~MAX77686_LDO_VOLT_MASK; |
| 355 | val |= hex; |
| 356 | ret = pmic_write(dev->parent, adr, &val, 1); |
| 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | static int max77686_buck_val(struct udevice *dev, int op, int *uV) |
| 362 | { |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 363 | unsigned int hex, mask, adr; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 364 | unsigned char val; |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 365 | int buck, ret; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 366 | |
| 367 | buck = dev->driver_data; |
| 368 | if (buck < 1 || buck > MAX77686_BUCK_NUM) { |
| 369 | error("Wrong buck number: %d", buck); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
| 373 | if (op == PMIC_OP_GET) |
| 374 | *uV = 0; |
| 375 | |
| 376 | /* &buck_out = ctrl + 1 */ |
Simon Glass | 2ede005 | 2015-07-02 18:16:04 -0600 | [diff] [blame] | 377 | adr = max77686_buck_out[buck]; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 378 | |
| 379 | /* mask */ |
| 380 | switch (buck) { |
| 381 | case 2: |
| 382 | case 3: |
| 383 | case 4: |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 384 | mask = MAX77686_BUCK234_VOLT_MASK; |
Simon Glass | fdcc59b | 2015-08-03 08:19:25 -0600 | [diff] [blame] | 385 | break; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 386 | default: |
| 387 | mask = MAX77686_BUCK_VOLT_MASK; |
Simon Glass | fdcc59b | 2015-08-03 08:19:25 -0600 | [diff] [blame] | 388 | break; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | ret = pmic_read(dev->parent, adr, &val, 1); |
| 392 | if (ret) |
| 393 | return ret; |
| 394 | |
| 395 | if (op == PMIC_OP_GET) { |
| 396 | val &= mask; |
| 397 | ret = max77686_buck_hex2volt(buck, val); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | *uV = ret; |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | hex = max77686_buck_volt2hex(buck, *uV); |
| 405 | if (hex < 0) |
| 406 | return hex; |
| 407 | |
| 408 | val &= ~mask; |
| 409 | val |= hex; |
| 410 | ret = pmic_write(dev->parent, adr, &val, 1); |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int max77686_ldo_mode(struct udevice *dev, int op, int *opmode) |
| 416 | { |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 417 | unsigned int adr, mode; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 418 | unsigned char val; |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 419 | int ldo, ret; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 420 | |
| 421 | if (op == PMIC_OP_GET) |
| 422 | *opmode = -EINVAL; |
| 423 | |
| 424 | ldo = dev->driver_data; |
| 425 | if (ldo < 1 || ldo > MAX77686_LDO_NUM) { |
| 426 | error("Wrong ldo number: %d", ldo); |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | |
| 430 | adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1; |
| 431 | |
| 432 | ret = pmic_read(dev->parent, adr, &val, 1); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
| 436 | if (op == PMIC_OP_GET) { |
| 437 | val &= MAX77686_LDO_MODE_MASK; |
| 438 | ret = max77686_ldo_hex2mode(ldo, val); |
| 439 | if (ret < 0) |
| 440 | return ret; |
| 441 | *opmode = ret; |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | /* mode */ |
| 446 | switch (*opmode) { |
| 447 | case OPMODE_OFF: |
| 448 | mode = MAX77686_LDO_MODE_OFF; |
| 449 | break; |
| 450 | case OPMODE_LPM: |
| 451 | switch (ldo) { |
| 452 | case 2: |
| 453 | case 6: |
| 454 | case 7: |
| 455 | case 8: |
| 456 | case 10: |
| 457 | case 11: |
| 458 | case 12: |
| 459 | case 14: |
| 460 | case 15: |
| 461 | case 16: |
| 462 | return -EINVAL; |
| 463 | default: |
| 464 | mode = MAX77686_LDO_MODE_LPM; |
| 465 | } |
| 466 | break; |
| 467 | case OPMODE_STANDBY: |
| 468 | switch (ldo) { |
| 469 | case 2: |
| 470 | case 6: |
| 471 | case 7: |
| 472 | case 8: |
| 473 | case 10: |
| 474 | case 11: |
| 475 | case 12: |
| 476 | case 14: |
| 477 | case 15: |
| 478 | case 16: |
| 479 | mode = MAX77686_LDO_MODE_STANDBY; |
| 480 | break; |
| 481 | default: |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | break; |
| 485 | case OPMODE_STANDBY_LPM: |
| 486 | mode = MAX77686_LDO_MODE_STANDBY_LPM; |
| 487 | break; |
| 488 | case OPMODE_ON: |
| 489 | mode = MAX77686_LDO_MODE_ON; |
| 490 | break; |
| 491 | default: |
| 492 | mode = 0xff; |
| 493 | } |
| 494 | |
| 495 | if (mode == 0xff) { |
| 496 | error("Wrong mode: %d for ldo%d", *opmode, ldo); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
| 500 | val &= ~MAX77686_LDO_MODE_MASK; |
| 501 | val |= mode; |
| 502 | ret = pmic_write(dev->parent, adr, &val, 1); |
| 503 | |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static int max77686_ldo_enable(struct udevice *dev, int op, bool *enable) |
| 508 | { |
| 509 | int ret, on_off; |
| 510 | |
| 511 | if (op == PMIC_OP_GET) { |
| 512 | ret = max77686_ldo_mode(dev, op, &on_off); |
| 513 | if (ret) |
| 514 | return ret; |
| 515 | |
| 516 | switch (on_off) { |
| 517 | case OPMODE_OFF: |
| 518 | *enable = 0; |
| 519 | break; |
| 520 | case OPMODE_ON: |
| 521 | *enable = 1; |
| 522 | break; |
| 523 | default: |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | } else if (op == PMIC_OP_SET) { |
| 527 | switch (*enable) { |
| 528 | case 0: |
| 529 | on_off = OPMODE_OFF; |
| 530 | break; |
| 531 | case 1: |
| 532 | on_off = OPMODE_ON; |
| 533 | break; |
| 534 | default: |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
| 538 | ret = max77686_ldo_mode(dev, op, &on_off); |
| 539 | if (ret) |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int max77686_buck_mode(struct udevice *dev, int op, int *opmode) |
| 547 | { |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 548 | unsigned int mask, adr, mode, mode_shift; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 549 | unsigned char val; |
Peng Fan | e4cbfa6 | 2015-07-28 22:47:08 +0800 | [diff] [blame] | 550 | int buck, ret; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 551 | |
| 552 | buck = dev->driver_data; |
| 553 | if (buck < 1 || buck > MAX77686_BUCK_NUM) { |
| 554 | error("Wrong buck number: %d", buck); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
Simon Glass | 2ede005 | 2015-07-02 18:16:04 -0600 | [diff] [blame] | 558 | adr = max77686_buck_ctrl[buck]; |
Przemyslaw Marczak | afee81e | 2015-04-20 20:07:47 +0200 | [diff] [blame] | 559 | |
| 560 | /* mask */ |
| 561 | switch (buck) { |
| 562 | case 2: |
| 563 | case 3: |
| 564 | case 4: |
| 565 | mode_shift = MAX77686_BUCK_MODE_SHIFT_2; |
| 566 | break; |
| 567 | default: |
| 568 | mode_shift = MAX77686_BUCK_MODE_SHIFT_1; |
| 569 | } |
| 570 | |
| 571 | mask = MAX77686_BUCK_MODE_MASK << mode_shift; |
| 572 | |
| 573 | ret = pmic_read(dev->parent, adr, &val, 1); |
| 574 | if (ret) |
| 575 | return ret; |
| 576 | |
| 577 | if (op == PMIC_OP_GET) { |
| 578 | val &= mask; |
| 579 | val >>= mode_shift; |
| 580 | ret = max77686_buck_hex2mode(buck, val); |
| 581 | if (ret < 0) |
| 582 | return ret; |
| 583 | *opmode = ret; |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* mode */ |
| 588 | switch (*opmode) { |
| 589 | case OPMODE_OFF: |
| 590 | mode = MAX77686_BUCK_MODE_OFF; |
| 591 | break; |
| 592 | case OPMODE_STANDBY: |
| 593 | switch (buck) { |
| 594 | case 1: |
| 595 | case 2: |
| 596 | case 3: |
| 597 | case 4: |
| 598 | mode = MAX77686_BUCK_MODE_STANDBY << mode_shift; |
| 599 | break; |
| 600 | default: |
| 601 | mode = 0xff; |
| 602 | } |
| 603 | break; |
| 604 | case OPMODE_LPM: |
| 605 | switch (buck) { |
| 606 | case 2: |
| 607 | case 3: |
| 608 | case 4: |
| 609 | mode = MAX77686_BUCK_MODE_LPM << mode_shift; |
| 610 | break; |
| 611 | default: |
| 612 | mode = 0xff; |
| 613 | } |
| 614 | break; |
| 615 | case OPMODE_ON: |
| 616 | mode = MAX77686_BUCK_MODE_ON << mode_shift; |
| 617 | break; |
| 618 | default: |
| 619 | mode = 0xff; |
| 620 | } |
| 621 | |
| 622 | if (mode == 0xff) { |
| 623 | error("Wrong mode: %d for buck: %d\n", *opmode, buck); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
| 627 | val &= ~mask; |
| 628 | val |= mode; |
| 629 | ret = pmic_write(dev->parent, adr, &val, 1); |
| 630 | |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | static int max77686_buck_enable(struct udevice *dev, int op, bool *enable) |
| 635 | { |
| 636 | int ret, on_off; |
| 637 | |
| 638 | if (op == PMIC_OP_GET) { |
| 639 | ret = max77686_buck_mode(dev, op, &on_off); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | |
| 643 | switch (on_off) { |
| 644 | case OPMODE_OFF: |
| 645 | *enable = false; |
| 646 | break; |
| 647 | case OPMODE_ON: |
| 648 | *enable = true; |
| 649 | break; |
| 650 | default: |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | } else if (op == PMIC_OP_SET) { |
| 654 | switch (*enable) { |
| 655 | case 0: |
| 656 | on_off = OPMODE_OFF; |
| 657 | break; |
| 658 | case 1: |
| 659 | on_off = OPMODE_ON; |
| 660 | break; |
| 661 | default: |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | ret = max77686_buck_mode(dev, op, &on_off); |
| 666 | if (ret) |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int max77686_ldo_probe(struct udevice *dev) |
| 674 | { |
| 675 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 676 | |
| 677 | uc_pdata = dev_get_uclass_platdata(dev); |
| 678 | |
| 679 | uc_pdata->type = REGULATOR_TYPE_LDO; |
| 680 | uc_pdata->mode_count = max77686_ldo_modes(dev->driver_data, |
| 681 | &uc_pdata->mode, dev); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int ldo_get_value(struct udevice *dev) |
| 687 | { |
| 688 | int uV; |
| 689 | int ret; |
| 690 | |
| 691 | ret = max77686_ldo_val(dev, PMIC_OP_GET, &uV); |
| 692 | if (ret) |
| 693 | return ret; |
| 694 | |
| 695 | return uV; |
| 696 | } |
| 697 | |
| 698 | static int ldo_set_value(struct udevice *dev, int uV) |
| 699 | { |
| 700 | return max77686_ldo_val(dev, PMIC_OP_SET, &uV); |
| 701 | } |
| 702 | |
| 703 | static bool ldo_get_enable(struct udevice *dev) |
| 704 | { |
| 705 | bool enable = false; |
| 706 | int ret; |
| 707 | |
| 708 | ret = max77686_ldo_enable(dev, PMIC_OP_GET, &enable); |
| 709 | if (ret) |
| 710 | return ret; |
| 711 | |
| 712 | return enable; |
| 713 | } |
| 714 | |
| 715 | static int ldo_set_enable(struct udevice *dev, bool enable) |
| 716 | { |
| 717 | return max77686_ldo_enable(dev, PMIC_OP_SET, &enable); |
| 718 | } |
| 719 | |
| 720 | static int ldo_get_mode(struct udevice *dev) |
| 721 | { |
| 722 | int mode; |
| 723 | int ret; |
| 724 | |
| 725 | ret = max77686_ldo_mode(dev, PMIC_OP_GET, &mode); |
| 726 | if (ret) |
| 727 | return ret; |
| 728 | |
| 729 | return mode; |
| 730 | } |
| 731 | |
| 732 | static int ldo_set_mode(struct udevice *dev, int mode) |
| 733 | { |
| 734 | return max77686_ldo_mode(dev, PMIC_OP_SET, &mode); |
| 735 | } |
| 736 | |
| 737 | static int max77686_buck_probe(struct udevice *dev) |
| 738 | { |
| 739 | struct dm_regulator_uclass_platdata *uc_pdata; |
| 740 | |
| 741 | uc_pdata = dev_get_uclass_platdata(dev); |
| 742 | |
| 743 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 744 | uc_pdata->mode_count = max77686_buck_modes(dev->driver_data, |
| 745 | &uc_pdata->mode); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | static int buck_get_value(struct udevice *dev) |
| 751 | { |
| 752 | int uV; |
| 753 | int ret; |
| 754 | |
| 755 | ret = max77686_buck_val(dev, PMIC_OP_GET, &uV); |
| 756 | if (ret) |
| 757 | return ret; |
| 758 | |
| 759 | return uV; |
| 760 | } |
| 761 | |
| 762 | static int buck_set_value(struct udevice *dev, int uV) |
| 763 | { |
| 764 | return max77686_buck_val(dev, PMIC_OP_SET, &uV); |
| 765 | } |
| 766 | |
| 767 | static bool buck_get_enable(struct udevice *dev) |
| 768 | { |
| 769 | bool enable = false; |
| 770 | int ret; |
| 771 | |
| 772 | ret = max77686_buck_enable(dev, PMIC_OP_GET, &enable); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | |
| 776 | return enable; |
| 777 | } |
| 778 | |
| 779 | static int buck_set_enable(struct udevice *dev, bool enable) |
| 780 | { |
| 781 | return max77686_buck_enable(dev, PMIC_OP_SET, &enable); |
| 782 | } |
| 783 | |
| 784 | static int buck_get_mode(struct udevice *dev) |
| 785 | { |
| 786 | int mode; |
| 787 | int ret; |
| 788 | |
| 789 | ret = max77686_buck_mode(dev, PMIC_OP_GET, &mode); |
| 790 | if (ret) |
| 791 | return ret; |
| 792 | |
| 793 | return mode; |
| 794 | } |
| 795 | |
| 796 | static int buck_set_mode(struct udevice *dev, int mode) |
| 797 | { |
| 798 | return max77686_buck_mode(dev, PMIC_OP_SET, &mode); |
| 799 | } |
| 800 | |
| 801 | static const struct dm_regulator_ops max77686_ldo_ops = { |
| 802 | .get_value = ldo_get_value, |
| 803 | .set_value = ldo_set_value, |
| 804 | .get_enable = ldo_get_enable, |
| 805 | .set_enable = ldo_set_enable, |
| 806 | .get_mode = ldo_get_mode, |
| 807 | .set_mode = ldo_set_mode, |
| 808 | }; |
| 809 | |
| 810 | U_BOOT_DRIVER(max77686_ldo) = { |
| 811 | .name = MAX77686_LDO_DRIVER, |
| 812 | .id = UCLASS_REGULATOR, |
| 813 | .ops = &max77686_ldo_ops, |
| 814 | .probe = max77686_ldo_probe, |
| 815 | }; |
| 816 | |
| 817 | static const struct dm_regulator_ops max77686_buck_ops = { |
| 818 | .get_value = buck_get_value, |
| 819 | .set_value = buck_set_value, |
| 820 | .get_enable = buck_get_enable, |
| 821 | .set_enable = buck_set_enable, |
| 822 | .get_mode = buck_get_mode, |
| 823 | .set_mode = buck_set_mode, |
| 824 | }; |
| 825 | |
| 826 | U_BOOT_DRIVER(max77686_buck) = { |
| 827 | .name = MAX77686_BUCK_DRIVER, |
| 828 | .id = UCLASS_REGULATOR, |
| 829 | .ops = &max77686_buck_ops, |
| 830 | .probe = max77686_buck_probe, |
| 831 | }; |