1 /* 2 * Driver for Regulator part of Palmas PMIC Chips 3 * 4 * Copyright 2011-2013 Texas Instruments Inc. 5 * 6 * Author: Graeme Gregory <gg@slimlogic.co.uk> 7 * Author: Ian Lartey <ian@slimlogic.co.uk> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 * 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/init.h> 19 #include <linux/err.h> 20 #include <linux/platform_device.h> 21 #include <linux/regulator/driver.h> 22 #include <linux/regulator/machine.h> 23 #include <linux/slab.h> 24 #include <linux/regmap.h> 25 #include <linux/mfd/palmas.h> 26 #include <linux/of.h> 27 #include <linux/of_platform.h> 28 #include <linux/regulator/of_regulator.h> 29 30 struct regs_info { 31 char *name; 32 char *sname; 33 u8 vsel_addr; 34 u8 ctrl_addr; 35 u8 tstep_addr; 36 }; 37 38 static const struct regs_info palmas_regs_info[] = { 39 { 40 .name = "SMPS12", 41 .sname = "smps1-in", 42 .vsel_addr = PALMAS_SMPS12_VOLTAGE, 43 .ctrl_addr = PALMAS_SMPS12_CTRL, 44 .tstep_addr = PALMAS_SMPS12_TSTEP, 45 }, 46 { 47 .name = "SMPS123", 48 .sname = "smps1-in", 49 .vsel_addr = PALMAS_SMPS12_VOLTAGE, 50 .ctrl_addr = PALMAS_SMPS12_CTRL, 51 .tstep_addr = PALMAS_SMPS12_TSTEP, 52 }, 53 { 54 .name = "SMPS3", 55 .sname = "smps3-in", 56 .vsel_addr = PALMAS_SMPS3_VOLTAGE, 57 .ctrl_addr = PALMAS_SMPS3_CTRL, 58 }, 59 { 60 .name = "SMPS45", 61 .sname = "smps4-in", 62 .vsel_addr = PALMAS_SMPS45_VOLTAGE, 63 .ctrl_addr = PALMAS_SMPS45_CTRL, 64 .tstep_addr = PALMAS_SMPS45_TSTEP, 65 }, 66 { 67 .name = "SMPS457", 68 .sname = "smps4-in", 69 .vsel_addr = PALMAS_SMPS45_VOLTAGE, 70 .ctrl_addr = PALMAS_SMPS45_CTRL, 71 .tstep_addr = PALMAS_SMPS45_TSTEP, 72 }, 73 { 74 .name = "SMPS6", 75 .sname = "smps6-in", 76 .vsel_addr = PALMAS_SMPS6_VOLTAGE, 77 .ctrl_addr = PALMAS_SMPS6_CTRL, 78 .tstep_addr = PALMAS_SMPS6_TSTEP, 79 }, 80 { 81 .name = "SMPS7", 82 .sname = "smps7-in", 83 .vsel_addr = PALMAS_SMPS7_VOLTAGE, 84 .ctrl_addr = PALMAS_SMPS7_CTRL, 85 }, 86 { 87 .name = "SMPS8", 88 .sname = "smps8-in", 89 .vsel_addr = PALMAS_SMPS8_VOLTAGE, 90 .ctrl_addr = PALMAS_SMPS8_CTRL, 91 .tstep_addr = PALMAS_SMPS8_TSTEP, 92 }, 93 { 94 .name = "SMPS9", 95 .sname = "smps9-in", 96 .vsel_addr = PALMAS_SMPS9_VOLTAGE, 97 .ctrl_addr = PALMAS_SMPS9_CTRL, 98 }, 99 { 100 .name = "SMPS10", 101 .sname = "smps10-in", 102 .ctrl_addr = PALMAS_SMPS10_CTRL, 103 }, 104 { 105 .name = "LDO1", 106 .sname = "ldo1-in", 107 .vsel_addr = PALMAS_LDO1_VOLTAGE, 108 .ctrl_addr = PALMAS_LDO1_CTRL, 109 }, 110 { 111 .name = "LDO2", 112 .sname = "ldo2-in", 113 .vsel_addr = PALMAS_LDO2_VOLTAGE, 114 .ctrl_addr = PALMAS_LDO2_CTRL, 115 }, 116 { 117 .name = "LDO3", 118 .sname = "ldo3-in", 119 .vsel_addr = PALMAS_LDO3_VOLTAGE, 120 .ctrl_addr = PALMAS_LDO3_CTRL, 121 }, 122 { 123 .name = "LDO4", 124 .sname = "ldo4-in", 125 .vsel_addr = PALMAS_LDO4_VOLTAGE, 126 .ctrl_addr = PALMAS_LDO4_CTRL, 127 }, 128 { 129 .name = "LDO5", 130 .sname = "ldo5-in", 131 .vsel_addr = PALMAS_LDO5_VOLTAGE, 132 .ctrl_addr = PALMAS_LDO5_CTRL, 133 }, 134 { 135 .name = "LDO6", 136 .sname = "ldo6-in", 137 .vsel_addr = PALMAS_LDO6_VOLTAGE, 138 .ctrl_addr = PALMAS_LDO6_CTRL, 139 }, 140 { 141 .name = "LDO7", 142 .sname = "ldo7-in", 143 .vsel_addr = PALMAS_LDO7_VOLTAGE, 144 .ctrl_addr = PALMAS_LDO7_CTRL, 145 }, 146 { 147 .name = "LDO8", 148 .sname = "ldo8-in", 149 .vsel_addr = PALMAS_LDO8_VOLTAGE, 150 .ctrl_addr = PALMAS_LDO8_CTRL, 151 }, 152 { 153 .name = "LDO9", 154 .sname = "ldo9-in", 155 .vsel_addr = PALMAS_LDO9_VOLTAGE, 156 .ctrl_addr = PALMAS_LDO9_CTRL, 157 }, 158 { 159 .name = "LDOLN", 160 .sname = "ldoln-in", 161 .vsel_addr = PALMAS_LDOLN_VOLTAGE, 162 .ctrl_addr = PALMAS_LDOLN_CTRL, 163 }, 164 { 165 .name = "LDOUSB", 166 .sname = "ldousb-in", 167 .vsel_addr = PALMAS_LDOUSB_VOLTAGE, 168 .ctrl_addr = PALMAS_LDOUSB_CTRL, 169 }, 170 { 171 .name = "REGEN1", 172 .ctrl_addr = PALMAS_REGEN1_CTRL, 173 }, 174 { 175 .name = "REGEN2", 176 .ctrl_addr = PALMAS_REGEN2_CTRL, 177 }, 178 { 179 .name = "REGEN3", 180 .ctrl_addr = PALMAS_REGEN3_CTRL, 181 }, 182 { 183 .name = "SYSEN1", 184 .ctrl_addr = PALMAS_SYSEN1_CTRL, 185 }, 186 { 187 .name = "SYSEN2", 188 .ctrl_addr = PALMAS_SYSEN2_CTRL, 189 }, 190 }; 191 192 static unsigned int palmas_smps_ramp_delay[4] = {0, 10000, 5000, 2500}; 193 194 #define SMPS_CTRL_MODE_OFF 0x00 195 #define SMPS_CTRL_MODE_ON 0x01 196 #define SMPS_CTRL_MODE_ECO 0x02 197 #define SMPS_CTRL_MODE_PWM 0x03 198 199 /* These values are derived from the data sheet. And are the number of steps 200 * where there is a voltage change, the ranges at beginning and end of register 201 * max/min values where there are no change are ommitted. 202 * 203 * So they are basically (maxV-minV)/stepV 204 */ 205 #define PALMAS_SMPS_NUM_VOLTAGES 117 206 #define PALMAS_SMPS10_NUM_VOLTAGES 2 207 #define PALMAS_LDO_NUM_VOLTAGES 50 208 209 #define SMPS10_VSEL (1<<3) 210 #define SMPS10_BOOST_EN (1<<2) 211 #define SMPS10_BYPASS_EN (1<<1) 212 #define SMPS10_SWITCH_EN (1<<0) 213 214 #define REGULATOR_SLAVE 0 215 216 static int palmas_smps_read(struct palmas *palmas, unsigned int reg, 217 unsigned int *dest) 218 { 219 unsigned int addr; 220 221 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg); 222 223 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest); 224 } 225 226 static int palmas_smps_write(struct palmas *palmas, unsigned int reg, 227 unsigned int value) 228 { 229 unsigned int addr; 230 231 addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg); 232 233 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value); 234 } 235 236 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg, 237 unsigned int *dest) 238 { 239 unsigned int addr; 240 241 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg); 242 243 return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest); 244 } 245 246 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg, 247 unsigned int value) 248 { 249 unsigned int addr; 250 251 addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg); 252 253 return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value); 254 } 255 256 static int palmas_is_enabled_smps(struct regulator_dev *dev) 257 { 258 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 259 int id = rdev_get_id(dev); 260 unsigned int reg; 261 262 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®); 263 264 reg &= PALMAS_SMPS12_CTRL_STATUS_MASK; 265 reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT; 266 267 return !!(reg); 268 } 269 270 static int palmas_enable_smps(struct regulator_dev *dev) 271 { 272 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 273 int id = rdev_get_id(dev); 274 unsigned int reg; 275 276 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®); 277 278 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 279 if (pmic->current_reg_mode[id]) 280 reg |= pmic->current_reg_mode[id]; 281 else 282 reg |= SMPS_CTRL_MODE_ON; 283 284 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg); 285 286 return 0; 287 } 288 289 static int palmas_disable_smps(struct regulator_dev *dev) 290 { 291 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 292 int id = rdev_get_id(dev); 293 unsigned int reg; 294 295 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®); 296 297 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 298 299 palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg); 300 301 return 0; 302 } 303 304 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode) 305 { 306 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 307 int id = rdev_get_id(dev); 308 unsigned int reg; 309 bool rail_enable = true; 310 311 palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®); 312 reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 313 314 if (reg == SMPS_CTRL_MODE_OFF) 315 rail_enable = false; 316 317 switch (mode) { 318 case REGULATOR_MODE_NORMAL: 319 reg |= SMPS_CTRL_MODE_ON; 320 break; 321 case REGULATOR_MODE_IDLE: 322 reg |= SMPS_CTRL_MODE_ECO; 323 break; 324 case REGULATOR_MODE_FAST: 325 reg |= SMPS_CTRL_MODE_PWM; 326 break; 327 default: 328 return -EINVAL; 329 } 330 331 pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 332 if (rail_enable) 333 palmas_smps_write(pmic->palmas, 334 palmas_regs_info[id].ctrl_addr, reg); 335 return 0; 336 } 337 338 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev) 339 { 340 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 341 int id = rdev_get_id(dev); 342 unsigned int reg; 343 344 reg = pmic->current_reg_mode[id] & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 345 346 switch (reg) { 347 case SMPS_CTRL_MODE_ON: 348 return REGULATOR_MODE_NORMAL; 349 case SMPS_CTRL_MODE_ECO: 350 return REGULATOR_MODE_IDLE; 351 case SMPS_CTRL_MODE_PWM: 352 return REGULATOR_MODE_FAST; 353 } 354 355 return 0; 356 } 357 358 static int palmas_list_voltage_smps(struct regulator_dev *dev, 359 unsigned selector) 360 { 361 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 362 int id = rdev_get_id(dev); 363 int mult = 1; 364 365 /* Read the multiplier set in VSEL register to return 366 * the correct voltage. 367 */ 368 if (pmic->range[id]) 369 mult = 2; 370 371 if (selector == 0) 372 return 0; 373 else if (selector < 6) 374 return 500000 * mult; 375 else 376 /* Voltage is linear mapping starting from selector 6, 377 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE 378 * RANGE is either x1 or x2 379 */ 380 return (490000 + ((selector - 5) * 10000)) * mult; 381 } 382 383 static int palmas_map_voltage_smps(struct regulator_dev *rdev, 384 int min_uV, int max_uV) 385 { 386 struct palmas_pmic *pmic = rdev_get_drvdata(rdev); 387 int id = rdev_get_id(rdev); 388 int ret, voltage; 389 390 if (min_uV == 0) 391 return 0; 392 393 if (pmic->range[id]) { /* RANGE is x2 */ 394 if (min_uV < 1000000) 395 min_uV = 1000000; 396 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6; 397 } else { /* RANGE is x1 */ 398 if (min_uV < 500000) 399 min_uV = 500000; 400 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6; 401 } 402 403 /* Map back into a voltage to verify we're still in bounds */ 404 voltage = palmas_list_voltage_smps(rdev, ret); 405 if (voltage < min_uV || voltage > max_uV) 406 return -EINVAL; 407 408 return ret; 409 } 410 411 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev *rdev, 412 unsigned int old_selector, unsigned int new_selector) 413 { 414 struct palmas_pmic *pmic = rdev_get_drvdata(rdev); 415 int id = rdev_get_id(rdev); 416 int old_uv, new_uv; 417 unsigned int ramp_delay = pmic->ramp_delay[id]; 418 419 if (!ramp_delay) 420 return 0; 421 422 old_uv = palmas_list_voltage_smps(rdev, old_selector); 423 if (old_uv < 0) 424 return old_uv; 425 426 new_uv = palmas_list_voltage_smps(rdev, new_selector); 427 if (new_uv < 0) 428 return new_uv; 429 430 return DIV_ROUND_UP(abs(old_uv - new_uv), ramp_delay); 431 } 432 433 static int palmas_smps_set_ramp_delay(struct regulator_dev *rdev, 434 int ramp_delay) 435 { 436 struct palmas_pmic *pmic = rdev_get_drvdata(rdev); 437 int id = rdev_get_id(rdev); 438 unsigned int reg = 0; 439 unsigned int addr = palmas_regs_info[id].tstep_addr; 440 int ret; 441 442 /* SMPS3 and SMPS7 do not have tstep_addr setting */ 443 switch (id) { 444 case PALMAS_REG_SMPS3: 445 case PALMAS_REG_SMPS7: 446 return 0; 447 } 448 449 if (ramp_delay <= 0) 450 reg = 0; 451 else if (ramp_delay <= 2500) 452 reg = 3; 453 else if (ramp_delay <= 5000) 454 reg = 2; 455 else 456 reg = 1; 457 458 ret = palmas_smps_write(pmic->palmas, addr, reg); 459 if (ret < 0) { 460 dev_err(pmic->palmas->dev, "TSTEP write failed: %d\n", ret); 461 return ret; 462 } 463 464 pmic->ramp_delay[id] = palmas_smps_ramp_delay[reg]; 465 return ret; 466 } 467 468 static struct regulator_ops palmas_ops_smps = { 469 .is_enabled = palmas_is_enabled_smps, 470 .enable = palmas_enable_smps, 471 .disable = palmas_disable_smps, 472 .set_mode = palmas_set_mode_smps, 473 .get_mode = palmas_get_mode_smps, 474 .get_voltage_sel = regulator_get_voltage_sel_regmap, 475 .set_voltage_sel = regulator_set_voltage_sel_regmap, 476 .list_voltage = palmas_list_voltage_smps, 477 .map_voltage = palmas_map_voltage_smps, 478 .set_voltage_time_sel = palma_smps_set_voltage_smps_time_sel, 479 .set_ramp_delay = palmas_smps_set_ramp_delay, 480 }; 481 482 static struct regulator_ops palmas_ops_smps10 = { 483 .is_enabled = regulator_is_enabled_regmap, 484 .enable = regulator_enable_regmap, 485 .disable = regulator_disable_regmap, 486 .get_voltage_sel = regulator_get_voltage_sel_regmap, 487 .set_voltage_sel = regulator_set_voltage_sel_regmap, 488 .list_voltage = regulator_list_voltage_linear, 489 .map_voltage = regulator_map_voltage_linear, 490 }; 491 492 static int palmas_is_enabled_ldo(struct regulator_dev *dev) 493 { 494 struct palmas_pmic *pmic = rdev_get_drvdata(dev); 495 int id = rdev_get_id(dev); 496 unsigned int reg; 497 498 palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, ®); 499 500 reg &= PALMAS_LDO1_CTRL_STATUS; 501 502 return !!(reg); 503 } 504 505 static struct regulator_ops palmas_ops_ldo = { 506 .is_enabled = palmas_is_enabled_ldo, 507 .enable = regulator_enable_regmap, 508 .disable = regulator_disable_regmap, 509 .get_voltage_sel = regulator_get_voltage_sel_regmap, 510 .set_voltage_sel = regulator_set_voltage_sel_regmap, 511 .list_voltage = regulator_list_voltage_linear, 512 .map_voltage = regulator_map_voltage_linear, 513 }; 514 515 static struct regulator_ops palmas_ops_extreg = { 516 .is_enabled = regulator_is_enabled_regmap, 517 .enable = regulator_enable_regmap, 518 .disable = regulator_disable_regmap, 519 }; 520 521 /* 522 * setup the hardware based sleep configuration of the SMPS/LDO regulators 523 * from the platform data. This is different to the software based control 524 * supported by the regulator framework as it is controlled by toggling 525 * pins on the PMIC such as PREQ, SYSEN, ... 526 */ 527 static int palmas_smps_init(struct palmas *palmas, int id, 528 struct palmas_reg_init *reg_init) 529 { 530 unsigned int reg; 531 unsigned int addr; 532 int ret; 533 534 addr = palmas_regs_info[id].ctrl_addr; 535 536 ret = palmas_smps_read(palmas, addr, ®); 537 if (ret) 538 return ret; 539 540 switch (id) { 541 case PALMAS_REG_SMPS10: 542 reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK; 543 if (reg_init->mode_sleep) 544 reg |= reg_init->mode_sleep << 545 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT; 546 break; 547 default: 548 if (reg_init->warm_reset) 549 reg |= PALMAS_SMPS12_CTRL_WR_S; 550 else 551 reg &= ~PALMAS_SMPS12_CTRL_WR_S; 552 553 if (reg_init->roof_floor) 554 reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN; 555 else 556 reg &= ~PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN; 557 558 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK; 559 if (reg_init->mode_sleep) 560 reg |= reg_init->mode_sleep << 561 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT; 562 } 563 564 ret = palmas_smps_write(palmas, addr, reg); 565 if (ret) 566 return ret; 567 568 if (palmas_regs_info[id].vsel_addr && reg_init->vsel) { 569 addr = palmas_regs_info[id].vsel_addr; 570 571 reg = reg_init->vsel; 572 573 ret = palmas_smps_write(palmas, addr, reg); 574 if (ret) 575 return ret; 576 } 577 578 579 return 0; 580 } 581 582 static int palmas_ldo_init(struct palmas *palmas, int id, 583 struct palmas_reg_init *reg_init) 584 { 585 unsigned int reg; 586 unsigned int addr; 587 int ret; 588 589 addr = palmas_regs_info[id].ctrl_addr; 590 591 ret = palmas_ldo_read(palmas, addr, ®); 592 if (ret) 593 return ret; 594 595 if (reg_init->warm_reset) 596 reg |= PALMAS_LDO1_CTRL_WR_S; 597 else 598 reg &= ~PALMAS_LDO1_CTRL_WR_S; 599 600 if (reg_init->mode_sleep) 601 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP; 602 else 603 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP; 604 605 ret = palmas_ldo_write(palmas, addr, reg); 606 if (ret) 607 return ret; 608 609 return 0; 610 } 611 612 static int palmas_extreg_init(struct palmas *palmas, int id, 613 struct palmas_reg_init *reg_init) 614 { 615 unsigned int addr; 616 int ret; 617 unsigned int val = 0; 618 619 addr = palmas_regs_info[id].ctrl_addr; 620 621 if (reg_init->mode_sleep) 622 val = PALMAS_REGEN1_CTRL_MODE_SLEEP; 623 624 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE, 625 addr, PALMAS_REGEN1_CTRL_MODE_SLEEP, val); 626 if (ret < 0) { 627 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n", 628 addr, ret); 629 return ret; 630 } 631 return 0; 632 } 633 634 static void palmas_enable_ldo8_track(struct palmas *palmas) 635 { 636 unsigned int reg; 637 unsigned int addr; 638 int ret; 639 640 addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr; 641 642 ret = palmas_ldo_read(palmas, addr, ®); 643 if (ret) { 644 dev_err(palmas->dev, "Error in reading ldo8 control reg\n"); 645 return; 646 } 647 648 reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN; 649 ret = palmas_ldo_write(palmas, addr, reg); 650 if (ret < 0) { 651 dev_err(palmas->dev, "Error in enabling tracking mode\n"); 652 return; 653 } 654 /* 655 * When SMPS45 is set to off and LDO8 tracking is enabled, the LDO8 656 * output is defined by the LDO8_VOLTAGE.VSEL register divided by two, 657 * and can be set from 0.45 to 1.65 V. 658 */ 659 addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr; 660 ret = palmas_ldo_read(palmas, addr, ®); 661 if (ret) { 662 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n"); 663 return; 664 } 665 666 reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK; 667 ret = palmas_ldo_write(palmas, addr, reg); 668 if (ret < 0) 669 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n"); 670 671 return; 672 } 673 674 static struct of_regulator_match palmas_matches[] = { 675 { .name = "smps12", }, 676 { .name = "smps123", }, 677 { .name = "smps3", }, 678 { .name = "smps45", }, 679 { .name = "smps457", }, 680 { .name = "smps6", }, 681 { .name = "smps7", }, 682 { .name = "smps8", }, 683 { .name = "smps9", }, 684 { .name = "smps10", }, 685 { .name = "ldo1", }, 686 { .name = "ldo2", }, 687 { .name = "ldo3", }, 688 { .name = "ldo4", }, 689 { .name = "ldo5", }, 690 { .name = "ldo6", }, 691 { .name = "ldo7", }, 692 { .name = "ldo8", }, 693 { .name = "ldo9", }, 694 { .name = "ldoln", }, 695 { .name = "ldousb", }, 696 { .name = "regen1", }, 697 { .name = "regen2", }, 698 { .name = "regen3", }, 699 { .name = "sysen1", }, 700 { .name = "sysen2", }, 701 }; 702 703 static void palmas_dt_to_pdata(struct device *dev, 704 struct device_node *node, 705 struct palmas_pmic_platform_data *pdata) 706 { 707 struct device_node *regulators; 708 u32 prop; 709 int idx, ret; 710 711 node = of_node_get(node); 712 regulators = of_find_node_by_name(node, "regulators"); 713 if (!regulators) { 714 dev_info(dev, "regulator node not found\n"); 715 return; 716 } 717 718 ret = of_regulator_match(dev, regulators, palmas_matches, 719 PALMAS_NUM_REGS); 720 of_node_put(regulators); 721 if (ret < 0) { 722 dev_err(dev, "Error parsing regulator init data: %d\n", ret); 723 return; 724 } 725 726 for (idx = 0; idx < PALMAS_NUM_REGS; idx++) { 727 if (!palmas_matches[idx].init_data || 728 !palmas_matches[idx].of_node) 729 continue; 730 731 pdata->reg_data[idx] = palmas_matches[idx].init_data; 732 733 pdata->reg_init[idx] = devm_kzalloc(dev, 734 sizeof(struct palmas_reg_init), GFP_KERNEL); 735 736 pdata->reg_init[idx]->warm_reset = 737 of_property_read_bool(palmas_matches[idx].of_node, 738 "ti,warm-reset"); 739 740 pdata->reg_init[idx]->roof_floor = 741 of_property_read_bool(palmas_matches[idx].of_node, 742 "ti,roof-floor"); 743 744 ret = of_property_read_u32(palmas_matches[idx].of_node, 745 "ti,mode-sleep", &prop); 746 if (!ret) 747 pdata->reg_init[idx]->mode_sleep = prop; 748 749 ret = of_property_read_bool(palmas_matches[idx].of_node, 750 "ti,smps-range"); 751 if (ret) 752 pdata->reg_init[idx]->vsel = 753 PALMAS_SMPS12_VOLTAGE_RANGE; 754 755 if (idx == PALMAS_REG_LDO8) 756 pdata->enable_ldo8_tracking = of_property_read_bool( 757 palmas_matches[idx].of_node, 758 "ti,enable-ldo8-tracking"); 759 } 760 761 pdata->ldo6_vibrator = of_property_read_bool(node, "ti,ldo6-vibrator"); 762 } 763 764 765 static int palmas_regulators_probe(struct platform_device *pdev) 766 { 767 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); 768 struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data; 769 struct device_node *node = pdev->dev.of_node; 770 struct regulator_dev *rdev; 771 struct regulator_config config = { }; 772 struct palmas_pmic *pmic; 773 struct palmas_reg_init *reg_init; 774 int id = 0, ret; 775 unsigned int addr, reg; 776 777 if (node && !pdata) { 778 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 779 780 if (!pdata) 781 return -ENOMEM; 782 783 palmas_dt_to_pdata(&pdev->dev, node, pdata); 784 } 785 786 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL); 787 if (!pmic) 788 return -ENOMEM; 789 790 pmic->dev = &pdev->dev; 791 pmic->palmas = palmas; 792 palmas->pmic = pmic; 793 platform_set_drvdata(pdev, pmic); 794 795 ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, ®); 796 if (ret) 797 return ret; 798 799 if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN) 800 pmic->smps123 = 1; 801 802 if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN) 803 pmic->smps457 = 1; 804 805 config.regmap = palmas->regmap[REGULATOR_SLAVE]; 806 config.dev = &pdev->dev; 807 config.driver_data = pmic; 808 809 for (id = 0; id < PALMAS_REG_LDO1; id++) { 810 bool ramp_delay_support = false; 811 812 /* 813 * Miss out regulators which are not available due 814 * to slaving configurations. 815 */ 816 switch (id) { 817 case PALMAS_REG_SMPS12: 818 case PALMAS_REG_SMPS3: 819 if (pmic->smps123) 820 continue; 821 if (id == PALMAS_REG_SMPS12) 822 ramp_delay_support = true; 823 break; 824 case PALMAS_REG_SMPS123: 825 if (!pmic->smps123) 826 continue; 827 ramp_delay_support = true; 828 break; 829 case PALMAS_REG_SMPS45: 830 case PALMAS_REG_SMPS7: 831 if (pmic->smps457) 832 continue; 833 if (id == PALMAS_REG_SMPS45) 834 ramp_delay_support = true; 835 break; 836 case PALMAS_REG_SMPS457: 837 if (!pmic->smps457) 838 continue; 839 ramp_delay_support = true; 840 break; 841 case PALMAS_REG_SMPS10: 842 if (!PALMAS_PMIC_HAS(palmas, SMPS10_BOOST)) 843 continue; 844 } 845 846 if ((id == PALMAS_REG_SMPS6) || (id == PALMAS_REG_SMPS8)) 847 ramp_delay_support = true; 848 849 if (ramp_delay_support) { 850 addr = palmas_regs_info[id].tstep_addr; 851 ret = palmas_smps_read(pmic->palmas, addr, ®); 852 if (ret < 0) { 853 dev_err(&pdev->dev, 854 "reading TSTEP reg failed: %d\n", ret); 855 goto err_unregister_regulator; 856 } 857 pmic->desc[id].ramp_delay = 858 palmas_smps_ramp_delay[reg & 0x3]; 859 pmic->ramp_delay[id] = pmic->desc[id].ramp_delay; 860 } 861 862 /* Initialise sleep/init values from platform data */ 863 if (pdata && pdata->reg_init[id]) { 864 reg_init = pdata->reg_init[id]; 865 ret = palmas_smps_init(palmas, id, reg_init); 866 if (ret) 867 goto err_unregister_regulator; 868 } 869 870 /* Register the regulators */ 871 pmic->desc[id].name = palmas_regs_info[id].name; 872 pmic->desc[id].id = id; 873 874 switch (id) { 875 case PALMAS_REG_SMPS10: 876 pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES; 877 pmic->desc[id].ops = &palmas_ops_smps10; 878 pmic->desc[id].vsel_reg = 879 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, 880 PALMAS_SMPS10_CTRL); 881 pmic->desc[id].vsel_mask = SMPS10_VSEL; 882 pmic->desc[id].enable_reg = 883 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, 884 PALMAS_SMPS10_CTRL); 885 pmic->desc[id].enable_mask = SMPS10_BOOST_EN; 886 pmic->desc[id].min_uV = 3750000; 887 pmic->desc[id].uV_step = 1250000; 888 break; 889 default: 890 /* 891 * Read and store the RANGE bit for later use 892 * This must be done before regulator is probed, 893 * otherwise we error in probe with unsupportable 894 * ranges. Read the current smps mode for later use. 895 */ 896 addr = palmas_regs_info[id].vsel_addr; 897 898 ret = palmas_smps_read(pmic->palmas, addr, ®); 899 if (ret) 900 goto err_unregister_regulator; 901 if (reg & PALMAS_SMPS12_VOLTAGE_RANGE) 902 pmic->range[id] = 1; 903 904 pmic->desc[id].ops = &palmas_ops_smps; 905 pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES; 906 pmic->desc[id].vsel_reg = 907 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, 908 palmas_regs_info[id].vsel_addr); 909 pmic->desc[id].vsel_mask = 910 PALMAS_SMPS12_VOLTAGE_VSEL_MASK; 911 912 /* Read the smps mode for later use. */ 913 addr = palmas_regs_info[id].ctrl_addr; 914 ret = palmas_smps_read(pmic->palmas, addr, ®); 915 if (ret) 916 goto err_unregister_regulator; 917 pmic->current_reg_mode[id] = reg & 918 PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK; 919 } 920 921 pmic->desc[id].type = REGULATOR_VOLTAGE; 922 pmic->desc[id].owner = THIS_MODULE; 923 924 if (pdata) 925 config.init_data = pdata->reg_data[id]; 926 else 927 config.init_data = NULL; 928 929 pmic->desc[id].supply_name = palmas_regs_info[id].sname; 930 config.of_node = palmas_matches[id].of_node; 931 932 rdev = regulator_register(&pmic->desc[id], &config); 933 if (IS_ERR(rdev)) { 934 dev_err(&pdev->dev, 935 "failed to register %s regulator\n", 936 pdev->name); 937 ret = PTR_ERR(rdev); 938 goto err_unregister_regulator; 939 } 940 941 /* Save regulator for cleanup */ 942 pmic->rdev[id] = rdev; 943 } 944 945 /* Start this loop from the id left from previous loop */ 946 for (; id < PALMAS_NUM_REGS; id++) { 947 948 /* Miss out regulators which are not available due 949 * to alternate functions. 950 */ 951 952 /* Register the regulators */ 953 pmic->desc[id].name = palmas_regs_info[id].name; 954 pmic->desc[id].id = id; 955 pmic->desc[id].type = REGULATOR_VOLTAGE; 956 pmic->desc[id].owner = THIS_MODULE; 957 958 if (id < PALMAS_REG_REGEN1) { 959 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES; 960 pmic->desc[id].ops = &palmas_ops_ldo; 961 pmic->desc[id].min_uV = 900000; 962 pmic->desc[id].uV_step = 50000; 963 pmic->desc[id].linear_min_sel = 1; 964 pmic->desc[id].vsel_reg = 965 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, 966 palmas_regs_info[id].vsel_addr); 967 pmic->desc[id].vsel_mask = 968 PALMAS_LDO1_VOLTAGE_VSEL_MASK; 969 pmic->desc[id].enable_reg = 970 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, 971 palmas_regs_info[id].ctrl_addr); 972 pmic->desc[id].enable_mask = 973 PALMAS_LDO1_CTRL_MODE_ACTIVE; 974 975 /* Check if LDO8 is in tracking mode or not */ 976 if (pdata && (id == PALMAS_REG_LDO8) && 977 pdata->enable_ldo8_tracking) { 978 palmas_enable_ldo8_track(palmas); 979 pmic->desc[id].min_uV = 450000; 980 pmic->desc[id].uV_step = 25000; 981 } 982 } else { 983 pmic->desc[id].n_voltages = 1; 984 pmic->desc[id].ops = &palmas_ops_extreg; 985 pmic->desc[id].enable_reg = 986 PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE, 987 palmas_regs_info[id].ctrl_addr); 988 pmic->desc[id].enable_mask = 989 PALMAS_REGEN1_CTRL_MODE_ACTIVE; 990 } 991 992 if (pdata) 993 config.init_data = pdata->reg_data[id]; 994 else 995 config.init_data = NULL; 996 997 pmic->desc[id].supply_name = palmas_regs_info[id].sname; 998 config.of_node = palmas_matches[id].of_node; 999 1000 rdev = regulator_register(&pmic->desc[id], &config); 1001 if (IS_ERR(rdev)) { 1002 dev_err(&pdev->dev, 1003 "failed to register %s regulator\n", 1004 pdev->name); 1005 ret = PTR_ERR(rdev); 1006 goto err_unregister_regulator; 1007 } 1008 1009 /* Save regulator for cleanup */ 1010 pmic->rdev[id] = rdev; 1011 1012 /* Initialise sleep/init values from platform data */ 1013 if (pdata) { 1014 reg_init = pdata->reg_init[id]; 1015 if (reg_init) { 1016 if (id < PALMAS_REG_REGEN1) 1017 ret = palmas_ldo_init(palmas, 1018 id, reg_init); 1019 else 1020 ret = palmas_extreg_init(palmas, 1021 id, reg_init); 1022 if (ret) { 1023 regulator_unregister(pmic->rdev[id]); 1024 goto err_unregister_regulator; 1025 } 1026 } 1027 } 1028 } 1029 1030 1031 return 0; 1032 1033 err_unregister_regulator: 1034 while (--id >= 0) 1035 regulator_unregister(pmic->rdev[id]); 1036 return ret; 1037 } 1038 1039 static int palmas_regulators_remove(struct platform_device *pdev) 1040 { 1041 struct palmas_pmic *pmic = platform_get_drvdata(pdev); 1042 int id; 1043 1044 for (id = 0; id < PALMAS_NUM_REGS; id++) 1045 regulator_unregister(pmic->rdev[id]); 1046 return 0; 1047 } 1048 1049 static struct of_device_id of_palmas_match_tbl[] = { 1050 { .compatible = "ti,palmas-pmic", }, 1051 { .compatible = "ti,twl6035-pmic", }, 1052 { .compatible = "ti,twl6036-pmic", }, 1053 { .compatible = "ti,twl6037-pmic", }, 1054 { .compatible = "ti,tps65913-pmic", }, 1055 { .compatible = "ti,tps65914-pmic", }, 1056 { .compatible = "ti,tps80036-pmic", }, 1057 { .compatible = "ti,tps659038-pmic", }, 1058 { /* end */ } 1059 }; 1060 1061 static struct platform_driver palmas_driver = { 1062 .driver = { 1063 .name = "palmas-pmic", 1064 .of_match_table = of_palmas_match_tbl, 1065 .owner = THIS_MODULE, 1066 }, 1067 .probe = palmas_regulators_probe, 1068 .remove = palmas_regulators_remove, 1069 }; 1070 1071 static int __init palmas_init(void) 1072 { 1073 return platform_driver_register(&palmas_driver); 1074 } 1075 subsys_initcall(palmas_init); 1076 1077 static void __exit palmas_exit(void) 1078 { 1079 platform_driver_unregister(&palmas_driver); 1080 } 1081 module_exit(palmas_exit); 1082 1083 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>"); 1084 MODULE_DESCRIPTION("Palmas voltage regulator driver"); 1085 MODULE_LICENSE("GPL"); 1086 MODULE_ALIAS("platform:palmas-pmic"); 1087 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl); 1088