1 /* 2 * Regulator driver for DA9055 PMIC 3 * 4 * Copyright(c) 2012 Dialog Semiconductor Ltd. 5 * 6 * Author: David Dajun Chen <dchen@diasemi.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 */ 14 15 #include <linux/module.h> 16 #include <linux/init.h> 17 #include <linux/err.h> 18 #include <linux/gpio.h> 19 #include <linux/gpio/consumer.h> 20 #include <linux/platform_device.h> 21 #include <linux/regulator/driver.h> 22 #include <linux/regulator/machine.h> 23 #include <linux/of.h> 24 #include <linux/regulator/of_regulator.h> 25 26 #include <linux/mfd/da9055/core.h> 27 #include <linux/mfd/da9055/reg.h> 28 #include <linux/mfd/da9055/pdata.h> 29 30 #define DA9055_MIN_UA 0 31 #define DA9055_MAX_UA 3 32 33 #define DA9055_LDO_MODE_SYNC 0 34 #define DA9055_LDO_MODE_SLEEP 1 35 36 #define DA9055_BUCK_MODE_SLEEP 1 37 #define DA9055_BUCK_MODE_SYNC 2 38 #define DA9055_BUCK_MODE_AUTO 3 39 40 /* DA9055 REGULATOR IDs */ 41 #define DA9055_ID_BUCK1 0 42 #define DA9055_ID_BUCK2 1 43 #define DA9055_ID_LDO1 2 44 #define DA9055_ID_LDO2 3 45 #define DA9055_ID_LDO3 4 46 #define DA9055_ID_LDO4 5 47 #define DA9055_ID_LDO5 6 48 #define DA9055_ID_LDO6 7 49 50 /* DA9055 BUCK current limit */ 51 static const unsigned int da9055_current_limits[] = { 52 500000, 600000, 700000, 800000 53 }; 54 55 struct da9055_conf_reg { 56 int reg; 57 int sel_mask; 58 int en_mask; 59 }; 60 61 struct da9055_volt_reg { 62 int reg_a; 63 int reg_b; 64 int sl_shift; 65 int v_mask; 66 }; 67 68 struct da9055_mode_reg { 69 int reg; 70 int mask; 71 int shift; 72 }; 73 74 struct da9055_regulator_info { 75 struct regulator_desc reg_desc; 76 struct da9055_conf_reg conf; 77 struct da9055_volt_reg volt; 78 struct da9055_mode_reg mode; 79 }; 80 81 struct da9055_regulator { 82 struct da9055 *da9055; 83 struct da9055_regulator_info *info; 84 struct regulator_dev *rdev; 85 enum gpio_select reg_rselect; 86 }; 87 88 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev) 89 { 90 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 91 struct da9055_regulator_info *info = regulator->info; 92 int ret, mode = 0; 93 94 ret = da9055_reg_read(regulator->da9055, info->mode.reg); 95 if (ret < 0) 96 return ret; 97 98 switch ((ret & info->mode.mask) >> info->mode.shift) { 99 case DA9055_BUCK_MODE_SYNC: 100 mode = REGULATOR_MODE_FAST; 101 break; 102 case DA9055_BUCK_MODE_AUTO: 103 mode = REGULATOR_MODE_NORMAL; 104 break; 105 case DA9055_BUCK_MODE_SLEEP: 106 mode = REGULATOR_MODE_STANDBY; 107 break; 108 } 109 110 return mode; 111 } 112 113 static int da9055_buck_set_mode(struct regulator_dev *rdev, 114 unsigned int mode) 115 { 116 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 117 struct da9055_regulator_info *info = regulator->info; 118 int val = 0; 119 120 switch (mode) { 121 case REGULATOR_MODE_FAST: 122 val = DA9055_BUCK_MODE_SYNC << info->mode.shift; 123 break; 124 case REGULATOR_MODE_NORMAL: 125 val = DA9055_BUCK_MODE_AUTO << info->mode.shift; 126 break; 127 case REGULATOR_MODE_STANDBY: 128 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift; 129 break; 130 } 131 132 return da9055_reg_update(regulator->da9055, info->mode.reg, 133 info->mode.mask, val); 134 } 135 136 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev) 137 { 138 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 139 struct da9055_regulator_info *info = regulator->info; 140 int ret; 141 142 ret = da9055_reg_read(regulator->da9055, info->volt.reg_b); 143 if (ret < 0) 144 return ret; 145 146 if (ret >> info->volt.sl_shift) 147 return REGULATOR_MODE_STANDBY; 148 else 149 return REGULATOR_MODE_NORMAL; 150 } 151 152 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode) 153 { 154 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 155 struct da9055_regulator_info *info = regulator->info; 156 struct da9055_volt_reg volt = info->volt; 157 int val = 0; 158 159 switch (mode) { 160 case REGULATOR_MODE_NORMAL: 161 case REGULATOR_MODE_FAST: 162 val = DA9055_LDO_MODE_SYNC; 163 break; 164 case REGULATOR_MODE_STANDBY: 165 val = DA9055_LDO_MODE_SLEEP; 166 break; 167 } 168 169 return da9055_reg_update(regulator->da9055, volt.reg_b, 170 1 << volt.sl_shift, 171 val << volt.sl_shift); 172 } 173 174 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev) 175 { 176 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 177 struct da9055_regulator_info *info = regulator->info; 178 struct da9055_volt_reg volt = info->volt; 179 int ret, sel; 180 181 /* 182 * There are two voltage register set A & B for voltage ramping but 183 * either one of then can be active therefore we first determine 184 * the active register set. 185 */ 186 ret = da9055_reg_read(regulator->da9055, info->conf.reg); 187 if (ret < 0) 188 return ret; 189 190 ret &= info->conf.sel_mask; 191 192 /* Get the voltage for the active register set A/B */ 193 if (ret == DA9055_REGUALTOR_SET_A) 194 ret = da9055_reg_read(regulator->da9055, volt.reg_a); 195 else 196 ret = da9055_reg_read(regulator->da9055, volt.reg_b); 197 198 if (ret < 0) 199 return ret; 200 201 sel = (ret & volt.v_mask); 202 return sel; 203 } 204 205 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev, 206 unsigned int selector) 207 { 208 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 209 struct da9055_regulator_info *info = regulator->info; 210 int ret; 211 212 /* 213 * Regulator register set A/B is not selected through GPIO therefore 214 * we use default register set A for voltage ramping. 215 */ 216 if (regulator->reg_rselect == NO_GPIO) { 217 /* Select register set A */ 218 ret = da9055_reg_update(regulator->da9055, info->conf.reg, 219 info->conf.sel_mask, DA9055_SEL_REG_A); 220 if (ret < 0) 221 return ret; 222 223 /* Set the voltage */ 224 return da9055_reg_update(regulator->da9055, info->volt.reg_a, 225 info->volt.v_mask, selector); 226 } 227 228 /* 229 * Here regulator register set A/B is selected through GPIO. 230 * Therefore we first determine the selected register set A/B and 231 * then set the desired voltage for that register set A/B. 232 */ 233 ret = da9055_reg_read(regulator->da9055, info->conf.reg); 234 if (ret < 0) 235 return ret; 236 237 ret &= info->conf.sel_mask; 238 239 /* Set the voltage */ 240 if (ret == DA9055_REGUALTOR_SET_A) 241 return da9055_reg_update(regulator->da9055, info->volt.reg_a, 242 info->volt.v_mask, selector); 243 else 244 return da9055_reg_update(regulator->da9055, info->volt.reg_b, 245 info->volt.v_mask, selector); 246 } 247 248 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev, 249 int uV) 250 { 251 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 252 struct da9055_regulator_info *info = regulator->info; 253 int ret; 254 255 /* Select register set B for suspend voltage ramping. */ 256 if (regulator->reg_rselect == NO_GPIO) { 257 ret = da9055_reg_update(regulator->da9055, info->conf.reg, 258 info->conf.sel_mask, DA9055_SEL_REG_B); 259 if (ret < 0) 260 return ret; 261 } 262 263 ret = regulator_map_voltage_linear(rdev, uV, uV); 264 if (ret < 0) 265 return ret; 266 267 return da9055_reg_update(regulator->da9055, info->volt.reg_b, 268 info->volt.v_mask, ret); 269 } 270 271 static int da9055_suspend_enable(struct regulator_dev *rdev) 272 { 273 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 274 struct da9055_regulator_info *info = regulator->info; 275 276 /* Select register set B for voltage ramping. */ 277 if (regulator->reg_rselect == NO_GPIO) 278 return da9055_reg_update(regulator->da9055, info->conf.reg, 279 info->conf.sel_mask, DA9055_SEL_REG_B); 280 else 281 return 0; 282 } 283 284 static int da9055_suspend_disable(struct regulator_dev *rdev) 285 { 286 struct da9055_regulator *regulator = rdev_get_drvdata(rdev); 287 struct da9055_regulator_info *info = regulator->info; 288 289 /* Diselect register set B. */ 290 if (regulator->reg_rselect == NO_GPIO) 291 return da9055_reg_update(regulator->da9055, info->conf.reg, 292 info->conf.sel_mask, DA9055_SEL_REG_A); 293 else 294 return 0; 295 } 296 297 static const struct regulator_ops da9055_buck_ops = { 298 .get_mode = da9055_buck_get_mode, 299 .set_mode = da9055_buck_set_mode, 300 301 .get_current_limit = regulator_get_current_limit_regmap, 302 .set_current_limit = regulator_set_current_limit_regmap, 303 304 .get_voltage_sel = da9055_regulator_get_voltage_sel, 305 .set_voltage_sel = da9055_regulator_set_voltage_sel, 306 .list_voltage = regulator_list_voltage_linear, 307 .map_voltage = regulator_map_voltage_linear, 308 .is_enabled = regulator_is_enabled_regmap, 309 .enable = regulator_enable_regmap, 310 .disable = regulator_disable_regmap, 311 312 .set_suspend_voltage = da9055_regulator_set_suspend_voltage, 313 .set_suspend_enable = da9055_suspend_enable, 314 .set_suspend_disable = da9055_suspend_disable, 315 .set_suspend_mode = da9055_buck_set_mode, 316 }; 317 318 static const struct regulator_ops da9055_ldo_ops = { 319 .get_mode = da9055_ldo_get_mode, 320 .set_mode = da9055_ldo_set_mode, 321 322 .get_voltage_sel = da9055_regulator_get_voltage_sel, 323 .set_voltage_sel = da9055_regulator_set_voltage_sel, 324 .list_voltage = regulator_list_voltage_linear, 325 .map_voltage = regulator_map_voltage_linear, 326 .is_enabled = regulator_is_enabled_regmap, 327 .enable = regulator_enable_regmap, 328 .disable = regulator_disable_regmap, 329 330 .set_suspend_voltage = da9055_regulator_set_suspend_voltage, 331 .set_suspend_enable = da9055_suspend_enable, 332 .set_suspend_disable = da9055_suspend_disable, 333 .set_suspend_mode = da9055_ldo_set_mode, 334 335 }; 336 337 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \ 338 {\ 339 .reg_desc = {\ 340 .name = #_id,\ 341 .ops = &da9055_ldo_ops,\ 342 .type = REGULATOR_VOLTAGE,\ 343 .id = DA9055_ID_##_id,\ 344 .n_voltages = (max - min) / step + 1 + (voffset), \ 345 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ 346 .enable_mask = 1, \ 347 .min_uV = (min) * 1000,\ 348 .uV_step = (step) * 1000,\ 349 .linear_min_sel = (voffset),\ 350 .owner = THIS_MODULE,\ 351 },\ 352 .conf = {\ 353 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ 354 .sel_mask = (1 << 4),\ 355 .en_mask = 1,\ 356 },\ 357 .volt = {\ 358 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \ 359 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \ 360 .sl_shift = 7,\ 361 .v_mask = (1 << (vbits)) - 1,\ 362 },\ 363 } 364 365 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \ 366 {\ 367 .reg_desc = {\ 368 .name = #_id,\ 369 .ops = &da9055_buck_ops,\ 370 .type = REGULATOR_VOLTAGE,\ 371 .id = DA9055_ID_##_id,\ 372 .n_voltages = (max - min) / step + 1 + (voffset), \ 373 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ 374 .enable_mask = 1,\ 375 .min_uV = (min) * 1000,\ 376 .uV_step = (step) * 1000,\ 377 .linear_min_sel = (voffset),\ 378 .owner = THIS_MODULE,\ 379 .curr_table = da9055_current_limits,\ 380 .n_current_limits = ARRAY_SIZE(da9055_current_limits),\ 381 .csel_reg = DA9055_REG_BUCK_LIM,\ 382 .csel_mask = (mbits),\ 383 },\ 384 .conf = {\ 385 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \ 386 .sel_mask = (1 << 4),\ 387 .en_mask = 1,\ 388 },\ 389 .volt = {\ 390 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \ 391 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \ 392 .sl_shift = 7,\ 393 .v_mask = (1 << (vbits)) - 1,\ 394 },\ 395 .mode = {\ 396 .reg = DA9055_REG_BCORE_MODE,\ 397 .mask = (mbits),\ 398 .shift = (sbits),\ 399 },\ 400 } 401 402 static struct da9055_regulator_info da9055_regulator_info[] = { 403 DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2), 404 DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0), 405 DA9055_LDO(LDO1, 50, 900, 3300, 6, 2), 406 DA9055_LDO(LDO2, 50, 900, 3300, 6, 3), 407 DA9055_LDO(LDO3, 50, 900, 3300, 6, 2), 408 DA9055_LDO(LDO4, 50, 900, 3300, 6, 2), 409 DA9055_LDO(LDO5, 50, 900, 2750, 6, 2), 410 DA9055_LDO(LDO6, 20, 900, 3300, 7, 0), 411 }; 412 413 /* 414 * Configures regulator to be controlled either through GPIO 1 or 2. 415 * GPIO can control regulator state and/or select the regulator register 416 * set A/B for voltage ramping. 417 */ 418 static int da9055_gpio_init(struct da9055_regulator *regulator, 419 struct regulator_config *config, 420 struct da9055_pdata *pdata, int id) 421 { 422 struct da9055_regulator_info *info = regulator->info; 423 int ret = 0; 424 425 if (!pdata) 426 return 0; 427 428 if (pdata->gpio_ren && pdata->gpio_ren[id]) { 429 char name[18]; 430 int gpio_mux = pdata->gpio_ren[id]; 431 432 config->ena_gpiod = pdata->ena_gpiods[id]; 433 434 /* 435 * GPI pin is muxed with regulator to control the 436 * regulator state. 437 */ 438 sprintf(name, "DA9055 GPI %d", gpio_mux); 439 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN, 440 name); 441 if (ret < 0) 442 goto err; 443 444 /* 445 * Let the regulator know that its state is controlled 446 * through GPI. 447 */ 448 ret = da9055_reg_update(regulator->da9055, info->conf.reg, 449 DA9055_E_GPI_MASK, 450 pdata->reg_ren[id] 451 << DA9055_E_GPI_SHIFT); 452 if (ret < 0) 453 goto err; 454 } 455 456 if (pdata->gpio_rsel && pdata->gpio_rsel[id]) { 457 char name[18]; 458 int gpio_mux = pdata->gpio_rsel[id]; 459 460 regulator->reg_rselect = pdata->reg_rsel[id]; 461 462 /* 463 * GPI pin is muxed with regulator to select the 464 * regulator register set A/B for voltage ramping. 465 */ 466 sprintf(name, "DA9055 GPI %d", gpio_mux); 467 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN, 468 name); 469 if (ret < 0) 470 goto err; 471 472 /* 473 * Let the regulator know that its register set A/B 474 * will be selected through GPI for voltage ramping. 475 */ 476 ret = da9055_reg_update(regulator->da9055, info->conf.reg, 477 DA9055_V_GPI_MASK, 478 pdata->reg_rsel[id] 479 << DA9055_V_GPI_SHIFT); 480 } 481 482 err: 483 return ret; 484 } 485 486 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data) 487 { 488 struct da9055_regulator *regulator = data; 489 490 regulator_notifier_call_chain(regulator->rdev, 491 REGULATOR_EVENT_OVER_CURRENT, NULL); 492 493 return IRQ_HANDLED; 494 } 495 496 static inline struct da9055_regulator_info *find_regulator_info(int id) 497 { 498 struct da9055_regulator_info *info; 499 int i; 500 501 for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) { 502 info = &da9055_regulator_info[i]; 503 if (info->reg_desc.id == id) 504 return info; 505 } 506 507 return NULL; 508 } 509 510 #ifdef CONFIG_OF 511 static struct of_regulator_match da9055_reg_matches[] = { 512 { .name = "BUCK1", }, 513 { .name = "BUCK2", }, 514 { .name = "LDO1", }, 515 { .name = "LDO2", }, 516 { .name = "LDO3", }, 517 { .name = "LDO4", }, 518 { .name = "LDO5", }, 519 { .name = "LDO6", }, 520 }; 521 522 static int da9055_regulator_dt_init(struct platform_device *pdev, 523 struct da9055_regulator *regulator, 524 struct regulator_config *config, 525 int regid) 526 { 527 struct device_node *nproot, *np; 528 int ret; 529 530 nproot = of_node_get(pdev->dev.parent->of_node); 531 if (!nproot) 532 return -ENODEV; 533 534 np = of_get_child_by_name(nproot, "regulators"); 535 if (!np) 536 return -ENODEV; 537 538 ret = of_regulator_match(&pdev->dev, np, &da9055_reg_matches[regid], 1); 539 of_node_put(nproot); 540 if (ret < 0) { 541 dev_err(&pdev->dev, "Error matching regulator: %d\n", ret); 542 return ret; 543 } 544 545 config->init_data = da9055_reg_matches[regid].init_data; 546 config->of_node = da9055_reg_matches[regid].of_node; 547 548 if (!config->of_node) 549 return -ENODEV; 550 551 return 0; 552 } 553 #else 554 static inline int da9055_regulator_dt_init(struct platform_device *pdev, 555 struct da9055_regulator *regulator, 556 struct regulator_config *config, 557 int regid) 558 { 559 return -ENODEV; 560 } 561 #endif /* CONFIG_OF */ 562 563 static int da9055_regulator_probe(struct platform_device *pdev) 564 { 565 struct regulator_config config = { }; 566 struct da9055_regulator *regulator; 567 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent); 568 struct da9055_pdata *pdata = dev_get_platdata(da9055->dev); 569 int ret, irq; 570 571 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator), 572 GFP_KERNEL); 573 if (!regulator) 574 return -ENOMEM; 575 576 regulator->info = find_regulator_info(pdev->id); 577 if (regulator->info == NULL) { 578 dev_err(&pdev->dev, "invalid regulator ID specified\n"); 579 return -EINVAL; 580 } 581 582 regulator->da9055 = da9055; 583 config.dev = &pdev->dev; 584 config.driver_data = regulator; 585 config.regmap = da9055->regmap; 586 587 if (pdata) { 588 config.init_data = pdata->regulators[pdev->id]; 589 } else { 590 ret = da9055_regulator_dt_init(pdev, regulator, &config, 591 pdev->id); 592 if (ret < 0) 593 return ret; 594 } 595 596 ret = da9055_gpio_init(regulator, &config, pdata, pdev->id); 597 if (ret < 0) 598 return ret; 599 600 regulator->rdev = devm_regulator_register(&pdev->dev, 601 ®ulator->info->reg_desc, 602 &config); 603 if (IS_ERR(regulator->rdev)) { 604 dev_err(&pdev->dev, "Failed to register regulator %s\n", 605 regulator->info->reg_desc.name); 606 return PTR_ERR(regulator->rdev); 607 } 608 609 /* Only LDO 5 and 6 has got the over current interrupt */ 610 if (pdev->id == DA9055_ID_LDO5 || pdev->id == DA9055_ID_LDO6) { 611 irq = platform_get_irq_byname(pdev, "REGULATOR"); 612 if (irq < 0) 613 return irq; 614 615 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 616 da9055_ldo5_6_oc_irq, 617 IRQF_TRIGGER_HIGH | 618 IRQF_ONESHOT | 619 IRQF_PROBE_SHARED, 620 pdev->name, regulator); 621 if (ret != 0) { 622 if (ret != -EBUSY) { 623 dev_err(&pdev->dev, 624 "Failed to request Regulator IRQ %d: %d\n", 625 irq, ret); 626 return ret; 627 } 628 } 629 } 630 631 platform_set_drvdata(pdev, regulator); 632 633 return 0; 634 } 635 636 static struct platform_driver da9055_regulator_driver = { 637 .probe = da9055_regulator_probe, 638 .driver = { 639 .name = "da9055-regulator", 640 }, 641 }; 642 643 static int __init da9055_regulator_init(void) 644 { 645 return platform_driver_register(&da9055_regulator_driver); 646 } 647 subsys_initcall(da9055_regulator_init); 648 649 static void __exit da9055_regulator_exit(void) 650 { 651 platform_driver_unregister(&da9055_regulator_driver); 652 } 653 module_exit(da9055_regulator_exit); 654 655 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); 656 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC"); 657 MODULE_LICENSE("GPL"); 658 MODULE_ALIAS("platform:da9055-regulator"); 659