1/* 2 * drivers/regulator/ab3100.c 3 * 4 * Copyright (C) 2008-2009 ST-Ericsson AB 5 * License terms: GNU General Public License (GPL) version 2 6 * Low-level control of the AB3100 IC Low Dropout (LDO) 7 * regulators, external regulator and buck converter 8 * Author: Mattias Wallin <mattias.wallin@stericsson.com> 9 * Author: Linus Walleij <linus.walleij@stericsson.com> 10 */ 11 12#include <linux/module.h> 13#include <linux/kernel.h> 14#include <linux/init.h> 15#include <linux/err.h> 16#include <linux/delay.h> 17#include <linux/platform_device.h> 18#include <linux/regulator/driver.h> 19#include <linux/mfd/abx500.h> 20 21/* LDO registers and some handy masking definitions for AB3100 */ 22#define AB3100_LDO_A 0x40 23#define AB3100_LDO_C 0x41 24#define AB3100_LDO_D 0x42 25#define AB3100_LDO_E 0x43 26#define AB3100_LDO_E_SLEEP 0x44 27#define AB3100_LDO_F 0x45 28#define AB3100_LDO_G 0x46 29#define AB3100_LDO_H 0x47 30#define AB3100_LDO_H_SLEEP_MODE 0 31#define AB3100_LDO_H_SLEEP_EN 2 32#define AB3100_LDO_ON 4 33#define AB3100_LDO_H_VSEL_AC 5 34#define AB3100_LDO_K 0x48 35#define AB3100_LDO_EXT 0x49 36#define AB3100_BUCK 0x4A 37#define AB3100_BUCK_SLEEP 0x4B 38#define AB3100_REG_ON_MASK 0x10 39 40/** 41 * struct ab3100_regulator 42 * A struct passed around the individual regulator functions 43 * @platform_device: platform device holding this regulator 44 * @dev: handle to the device 45 * @plfdata: AB3100 platform data passed in at probe time 46 * @regreg: regulator register number in the AB3100 47 * @fixed_voltage: a fixed voltage for this regulator, if this 48 * 0 the voltages array is used instead. 49 * @typ_voltages: an array of available typical voltages for 50 * this regulator 51 * @voltages_len: length of the array of available voltages 52 */ 53struct ab3100_regulator { 54 struct regulator_dev *rdev; 55 struct device *dev; 56 struct ab3100_platform_data *plfdata; 57 u8 regreg; 58 int fixed_voltage; 59 int const *typ_voltages; 60 u8 voltages_len; 61}; 62 63/* The order in which registers are initialized */ 64static const u8 ab3100_reg_init_order[AB3100_NUM_REGULATORS+2] = { 65 AB3100_LDO_A, 66 AB3100_LDO_C, 67 AB3100_LDO_E, 68 AB3100_LDO_E_SLEEP, 69 AB3100_LDO_F, 70 AB3100_LDO_G, 71 AB3100_LDO_H, 72 AB3100_LDO_K, 73 AB3100_LDO_EXT, 74 AB3100_BUCK, 75 AB3100_BUCK_SLEEP, 76 AB3100_LDO_D, 77}; 78 79/* Preset (hardware defined) voltages for these regulators */ 80#define LDO_A_VOLTAGE 2750000 81#define LDO_C_VOLTAGE 2650000 82#define LDO_D_VOLTAGE 2650000 83 84static const int ldo_e_buck_typ_voltages[] = { 85 1800000, 86 1400000, 87 1300000, 88 1200000, 89 1100000, 90 1050000, 91 900000, 92}; 93 94static const int ldo_f_typ_voltages[] = { 95 1800000, 96 1400000, 97 1300000, 98 1200000, 99 1100000, 100 1050000, 101 2500000, 102 2650000, 103}; 104 105static const int ldo_g_typ_voltages[] = { 106 2850000, 107 2750000, 108 1800000, 109 1500000, 110}; 111 112static const int ldo_h_typ_voltages[] = { 113 2750000, 114 1800000, 115 1500000, 116 1200000, 117}; 118 119static const int ldo_k_typ_voltages[] = { 120 2750000, 121 1800000, 122}; 123 124 125/* The regulator devices */ 126static struct ab3100_regulator 127ab3100_regulators[AB3100_NUM_REGULATORS] = { 128 { 129 .regreg = AB3100_LDO_A, 130 .fixed_voltage = LDO_A_VOLTAGE, 131 }, 132 { 133 .regreg = AB3100_LDO_C, 134 .fixed_voltage = LDO_C_VOLTAGE, 135 }, 136 { 137 .regreg = AB3100_LDO_D, 138 .fixed_voltage = LDO_D_VOLTAGE, 139 }, 140 { 141 .regreg = AB3100_LDO_E, 142 .typ_voltages = ldo_e_buck_typ_voltages, 143 .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages), 144 }, 145 { 146 .regreg = AB3100_LDO_F, 147 .typ_voltages = ldo_f_typ_voltages, 148 .voltages_len = ARRAY_SIZE(ldo_f_typ_voltages), 149 }, 150 { 151 .regreg = AB3100_LDO_G, 152 .typ_voltages = ldo_g_typ_voltages, 153 .voltages_len = ARRAY_SIZE(ldo_g_typ_voltages), 154 }, 155 { 156 .regreg = AB3100_LDO_H, 157 .typ_voltages = ldo_h_typ_voltages, 158 .voltages_len = ARRAY_SIZE(ldo_h_typ_voltages), 159 }, 160 { 161 .regreg = AB3100_LDO_K, 162 .typ_voltages = ldo_k_typ_voltages, 163 .voltages_len = ARRAY_SIZE(ldo_k_typ_voltages), 164 }, 165 { 166 .regreg = AB3100_LDO_EXT, 167 /* No voltages for the external regulator */ 168 }, 169 { 170 .regreg = AB3100_BUCK, 171 .typ_voltages = ldo_e_buck_typ_voltages, 172 .voltages_len = ARRAY_SIZE(ldo_e_buck_typ_voltages), 173 }, 174}; 175 176/* 177 * General functions for enable, disable and is_enabled used for 178 * LDO: A,C,E,F,G,H,K,EXT and BUCK 179 */ 180static int ab3100_enable_regulator(struct regulator_dev *reg) 181{ 182 struct ab3100_regulator *abreg = reg->reg_data; 183 int err; 184 u8 regval; 185 186 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, 187 ®val); 188 if (err) { 189 dev_warn(®->dev, "failed to get regid %d value\n", 190 abreg->regreg); 191 return err; 192 } 193 194 /* The regulator is already on, no reason to go further */ 195 if (regval & AB3100_REG_ON_MASK) 196 return 0; 197 198 regval |= AB3100_REG_ON_MASK; 199 200 err = abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg, 201 regval); 202 if (err) { 203 dev_warn(®->dev, "failed to set regid %d value\n", 204 abreg->regreg); 205 return err; 206 } 207 208 /* Per-regulator power on delay from spec */ 209 switch (abreg->regreg) { 210 case AB3100_LDO_A: /* Fallthrough */ 211 case AB3100_LDO_C: /* Fallthrough */ 212 case AB3100_LDO_D: /* Fallthrough */ 213 case AB3100_LDO_E: /* Fallthrough */ 214 case AB3100_LDO_H: /* Fallthrough */ 215 case AB3100_LDO_K: 216 udelay(200); 217 break; 218 case AB3100_LDO_F: 219 udelay(600); 220 break; 221 case AB3100_LDO_G: 222 udelay(400); 223 break; 224 case AB3100_BUCK: 225 mdelay(1); 226 break; 227 default: 228 break; 229 } 230 231 return 0; 232} 233 234static int ab3100_disable_regulator(struct regulator_dev *reg) 235{ 236 struct ab3100_regulator *abreg = reg->reg_data; 237 int err; 238 u8 regval; 239 240 /* 241 * LDO D is a special regulator. When it is disabled, the entire 242 * system is shut down. So this is handled specially. 243 */ 244 pr_info("Called ab3100_disable_regulator\n"); 245 if (abreg->regreg == AB3100_LDO_D) { 246 dev_info(®->dev, "disabling LDO D - shut down system\n"); 247 /* Setting LDO D to 0x00 cuts the power to the SoC */ 248 return abx500_set_register_interruptible(abreg->dev, 0, 249 AB3100_LDO_D, 0x00U); 250 } 251 252 /* 253 * All other regulators are handled here 254 */ 255 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, 256 ®val); 257 if (err) { 258 dev_err(®->dev, "unable to get register 0x%x\n", 259 abreg->regreg); 260 return err; 261 } 262 regval &= ~AB3100_REG_ON_MASK; 263 return abx500_set_register_interruptible(abreg->dev, 0, abreg->regreg, 264 regval); 265} 266 267static int ab3100_is_enabled_regulator(struct regulator_dev *reg) 268{ 269 struct ab3100_regulator *abreg = reg->reg_data; 270 u8 regval; 271 int err; 272 273 err = abx500_get_register_interruptible(abreg->dev, 0, abreg->regreg, 274 ®val); 275 if (err) { 276 dev_err(®->dev, "unable to get register 0x%x\n", 277 abreg->regreg); 278 return err; 279 } 280 281 return regval & AB3100_REG_ON_MASK; 282} 283 284static int ab3100_list_voltage_regulator(struct regulator_dev *reg, 285 unsigned selector) 286{ 287 struct ab3100_regulator *abreg = reg->reg_data; 288 289 if (selector >= abreg->voltages_len) 290 return -EINVAL; 291 return abreg->typ_voltages[selector]; 292} 293 294static int ab3100_get_voltage_regulator(struct regulator_dev *reg) 295{ 296 struct ab3100_regulator *abreg = reg->reg_data; 297 u8 regval; 298 int err; 299 300 /* Return the voltage for fixed regulators immediately */ 301 if (abreg->fixed_voltage) 302 return abreg->fixed_voltage; 303 304 /* 305 * For variable types, read out setting and index into 306 * supplied voltage list. 307 */ 308 err = abx500_get_register_interruptible(abreg->dev, 0, 309 abreg->regreg, ®val); 310 if (err) { 311 dev_warn(®->dev, 312 "failed to get regulator value in register %02x\n", 313 abreg->regreg); 314 return err; 315 } 316 317 /* The 3 highest bits index voltages */ 318 regval &= 0xE0; 319 regval >>= 5; 320 321 if (regval >= abreg->voltages_len) { 322 dev_err(®->dev, 323 "regulator register %02x contains an illegal voltage setting\n", 324 abreg->regreg); 325 return -EINVAL; 326 } 327 328 return abreg->typ_voltages[regval]; 329} 330 331static int ab3100_get_best_voltage_index(struct regulator_dev *reg, 332 int min_uV, int max_uV) 333{ 334 struct ab3100_regulator *abreg = reg->reg_data; 335 int i; 336 int bestmatch; 337 int bestindex; 338 339 /* 340 * Locate the minimum voltage fitting the criteria on 341 * this regulator. The switchable voltages are not 342 * in strict falling order so we need to check them 343 * all for the best match. 344 */ 345 bestmatch = INT_MAX; 346 bestindex = -1; 347 for (i = 0; i < abreg->voltages_len; i++) { 348 if (abreg->typ_voltages[i] <= max_uV && 349 abreg->typ_voltages[i] >= min_uV && 350 abreg->typ_voltages[i] < bestmatch) { 351 bestmatch = abreg->typ_voltages[i]; 352 bestindex = i; 353 } 354 } 355 356 if (bestindex < 0) { 357 dev_warn(®->dev, "requested %d<=x<=%d uV, out of range!\n", 358 min_uV, max_uV); 359 return -EINVAL; 360 } 361 return bestindex; 362} 363 364static int ab3100_set_voltage_regulator(struct regulator_dev *reg, 365 int min_uV, int max_uV) 366{ 367 struct ab3100_regulator *abreg = reg->reg_data; 368 u8 regval; 369 int err; 370 int bestindex; 371 372 bestindex = ab3100_get_best_voltage_index(reg, min_uV, max_uV); 373 if (bestindex < 0) 374 return bestindex; 375 376 err = abx500_get_register_interruptible(abreg->dev, 0, 377 abreg->regreg, ®val); 378 if (err) { 379 dev_warn(®->dev, 380 "failed to get regulator register %02x\n", 381 abreg->regreg); 382 return err; 383 } 384 385 /* The highest three bits control the variable regulators */ 386 regval &= ~0xE0; 387 regval |= (bestindex << 5); 388 389 err = abx500_set_register_interruptible(abreg->dev, 0, 390 abreg->regreg, regval); 391 if (err) 392 dev_warn(®->dev, "failed to set regulator register %02x\n", 393 abreg->regreg); 394 395 return err; 396} 397 398static int ab3100_set_suspend_voltage_regulator(struct regulator_dev *reg, 399 int uV) 400{ 401 struct ab3100_regulator *abreg = reg->reg_data; 402 u8 regval; 403 int err; 404 int bestindex; 405 u8 targetreg; 406 407 if (abreg->regreg == AB3100_LDO_E) 408 targetreg = AB3100_LDO_E_SLEEP; 409 else if (abreg->regreg == AB3100_BUCK) 410 targetreg = AB3100_BUCK_SLEEP; 411 else 412 return -EINVAL; 413 414 /* LDO E and BUCK have special suspend voltages you can set */ 415 bestindex = ab3100_get_best_voltage_index(reg, uV, uV); 416 417 err = abx500_get_register_interruptible(abreg->dev, 0, 418 targetreg, ®val); 419 if (err) { 420 dev_warn(®->dev, 421 "failed to get regulator register %02x\n", 422 targetreg); 423 return err; 424 } 425 426 /* The highest three bits control the variable regulators */ 427 regval &= ~0xE0; 428 regval |= (bestindex << 5); 429 430 err = abx500_set_register_interruptible(abreg->dev, 0, 431 targetreg, regval); 432 if (err) 433 dev_warn(®->dev, "failed to set regulator register %02x\n", 434 abreg->regreg); 435 436 return err; 437} 438 439/* 440 * The external regulator can just define a fixed voltage. 441 */ 442static int ab3100_get_voltage_regulator_external(struct regulator_dev *reg) 443{ 444 struct ab3100_regulator *abreg = reg->reg_data; 445 446 return abreg->plfdata->external_voltage; 447} 448 449static struct regulator_ops regulator_ops_fixed = { 450 .enable = ab3100_enable_regulator, 451 .disable = ab3100_disable_regulator, 452 .is_enabled = ab3100_is_enabled_regulator, 453 .get_voltage = ab3100_get_voltage_regulator, 454}; 455 456static struct regulator_ops regulator_ops_variable = { 457 .enable = ab3100_enable_regulator, 458 .disable = ab3100_disable_regulator, 459 .is_enabled = ab3100_is_enabled_regulator, 460 .get_voltage = ab3100_get_voltage_regulator, 461 .set_voltage = ab3100_set_voltage_regulator, 462 .list_voltage = ab3100_list_voltage_regulator, 463}; 464 465static struct regulator_ops regulator_ops_variable_sleepable = { 466 .enable = ab3100_enable_regulator, 467 .disable = ab3100_disable_regulator, 468 .is_enabled = ab3100_is_enabled_regulator, 469 .get_voltage = ab3100_get_voltage_regulator, 470 .set_voltage = ab3100_set_voltage_regulator, 471 .set_suspend_voltage = ab3100_set_suspend_voltage_regulator, 472 .list_voltage = ab3100_list_voltage_regulator, 473}; 474 475/* 476 * LDO EXT is an external regulator so it is really 477 * not possible to set any voltage locally here, AB3100 478 * is an on/off switch plain an simple. The external 479 * voltage is defined in the board set-up if any. 480 */ 481static struct regulator_ops regulator_ops_external = { 482 .enable = ab3100_enable_regulator, 483 .disable = ab3100_disable_regulator, 484 .is_enabled = ab3100_is_enabled_regulator, 485 .get_voltage = ab3100_get_voltage_regulator_external, 486}; 487 488static struct regulator_desc 489ab3100_regulator_desc[AB3100_NUM_REGULATORS] = { 490 { 491 .name = "LDO_A", 492 .id = AB3100_LDO_A, 493 .ops = ®ulator_ops_fixed, 494 .type = REGULATOR_VOLTAGE, 495 .owner = THIS_MODULE, 496 }, 497 { 498 .name = "LDO_C", 499 .id = AB3100_LDO_C, 500 .ops = ®ulator_ops_fixed, 501 .type = REGULATOR_VOLTAGE, 502 .owner = THIS_MODULE, 503 }, 504 { 505 .name = "LDO_D", 506 .id = AB3100_LDO_D, 507 .ops = ®ulator_ops_fixed, 508 .type = REGULATOR_VOLTAGE, 509 .owner = THIS_MODULE, 510 }, 511 { 512 .name = "LDO_E", 513 .id = AB3100_LDO_E, 514 .ops = ®ulator_ops_variable_sleepable, 515 .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages), 516 .type = REGULATOR_VOLTAGE, 517 .owner = THIS_MODULE, 518 }, 519 { 520 .name = "LDO_F", 521 .id = AB3100_LDO_F, 522 .ops = ®ulator_ops_variable, 523 .n_voltages = ARRAY_SIZE(ldo_f_typ_voltages), 524 .type = REGULATOR_VOLTAGE, 525 .owner = THIS_MODULE, 526 }, 527 { 528 .name = "LDO_G", 529 .id = AB3100_LDO_G, 530 .ops = ®ulator_ops_variable, 531 .n_voltages = ARRAY_SIZE(ldo_g_typ_voltages), 532 .type = REGULATOR_VOLTAGE, 533 .owner = THIS_MODULE, 534 }, 535 { 536 .name = "LDO_H", 537 .id = AB3100_LDO_H, 538 .ops = ®ulator_ops_variable, 539 .n_voltages = ARRAY_SIZE(ldo_h_typ_voltages), 540 .type = REGULATOR_VOLTAGE, 541 .owner = THIS_MODULE, 542 }, 543 { 544 .name = "LDO_K", 545 .id = AB3100_LDO_K, 546 .ops = ®ulator_ops_variable, 547 .n_voltages = ARRAY_SIZE(ldo_k_typ_voltages), 548 .type = REGULATOR_VOLTAGE, 549 .owner = THIS_MODULE, 550 }, 551 { 552 .name = "LDO_EXT", 553 .id = AB3100_LDO_EXT, 554 .ops = ®ulator_ops_external, 555 .type = REGULATOR_VOLTAGE, 556 .owner = THIS_MODULE, 557 }, 558 { 559 .name = "BUCK", 560 .id = AB3100_BUCK, 561 .ops = ®ulator_ops_variable_sleepable, 562 .n_voltages = ARRAY_SIZE(ldo_e_buck_typ_voltages), 563 .type = REGULATOR_VOLTAGE, 564 .owner = THIS_MODULE, 565 }, 566}; 567 568/* 569 * NOTE: the following functions are regulators pluralis - it is the 570 * binding to the AB3100 core driver and the parent platform device 571 * for all the different regulators. 572 */ 573 574static int __devinit ab3100_regulators_probe(struct platform_device *pdev) 575{ 576 struct ab3100_platform_data *plfdata = pdev->dev.platform_data; 577 int err = 0; 578 u8 data; 579 int i; 580 581 /* Check chip state */ 582 err = abx500_get_register_interruptible(&pdev->dev, 0, 583 AB3100_LDO_D, &data); 584 if (err) { 585 dev_err(&pdev->dev, "could not read initial status of LDO_D\n"); 586 return err; 587 } 588 if (data & 0x10) 589 dev_notice(&pdev->dev, 590 "chip is already in active mode (Warm start)\n"); 591 else 592 dev_notice(&pdev->dev, 593 "chip is in inactive mode (Cold start)\n"); 594 595 /* Set up regulators */ 596 for (i = 0; i < ARRAY_SIZE(ab3100_reg_init_order); i++) { 597 err = abx500_set_register_interruptible(&pdev->dev, 0, 598 ab3100_reg_init_order[i], 599 plfdata->reg_initvals[i]); 600 if (err) { 601 dev_err(&pdev->dev, "regulator initialization failed with error %d\n", 602 err); 603 return err; 604 } 605 } 606 607 /* Register the regulators */ 608 for (i = 0; i < AB3100_NUM_REGULATORS; i++) { 609 struct ab3100_regulator *reg = &ab3100_regulators[i]; 610 struct regulator_dev *rdev; 611 612 /* 613 * Initialize per-regulator struct. 614 * Inherit platform data, this comes down from the 615 * i2c boarddata, from the machine. So if you want to 616 * see what it looks like for a certain machine, go 617 * into the machine I2C setup. 618 */ 619 reg->dev = &pdev->dev; 620 reg->plfdata = plfdata; 621 622 /* 623 * Register the regulator, pass around 624 * the ab3100_regulator struct 625 */ 626 rdev = regulator_register(&ab3100_regulator_desc[i], 627 &pdev->dev, 628 &plfdata->reg_constraints[i], 629 reg); 630 631 if (IS_ERR(rdev)) { 632 err = PTR_ERR(rdev); 633 dev_err(&pdev->dev, 634 "%s: failed to register regulator %s err %d\n", 635 __func__, ab3100_regulator_desc[i].name, 636 err); 637 /* remove the already registered regulators */ 638 while (--i >= 0) 639 regulator_unregister(ab3100_regulators[i].rdev); 640 return err; 641 } 642 643 /* Then set a pointer back to the registered regulator */ 644 reg->rdev = rdev; 645 } 646 647 return 0; 648} 649 650static int __devexit ab3100_regulators_remove(struct platform_device *pdev) 651{ 652 int i; 653 654 for (i = 0; i < AB3100_NUM_REGULATORS; i++) { 655 struct ab3100_regulator *reg = &ab3100_regulators[i]; 656 657 regulator_unregister(reg->rdev); 658 } 659 return 0; 660} 661 662static struct platform_driver ab3100_regulators_driver = { 663 .driver = { 664 .name = "ab3100-regulators", 665 .owner = THIS_MODULE, 666 }, 667 .probe = ab3100_regulators_probe, 668 .remove = __devexit_p(ab3100_regulators_remove), 669}; 670 671static __init int ab3100_regulators_init(void) 672{ 673 return platform_driver_register(&ab3100_regulators_driver); 674} 675 676static __exit void ab3100_regulators_exit(void) 677{ 678 platform_driver_unregister(&ab3100_regulators_driver); 679} 680 681subsys_initcall(ab3100_regulators_init); 682module_exit(ab3100_regulators_exit); 683 684MODULE_AUTHOR("Mattias Wallin <mattias.wallin@stericsson.com>"); 685MODULE_DESCRIPTION("AB3100 Regulator driver"); 686MODULE_LICENSE("GPL"); 687MODULE_ALIAS("platform:ab3100-regulators"); 688