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