da9063-regulator.c (27122B)
1// SPDX-License-Identifier: GPL-2.0+ 2// 3// Regulator driver for DA9063 PMIC series 4// 5// Copyright 2012 Dialog Semiconductors Ltd. 6// Copyright 2013 Philipp Zabel, Pengutronix 7// 8// Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com> 9 10#include <linux/kernel.h> 11#include <linux/module.h> 12#include <linux/init.h> 13#include <linux/err.h> 14#include <linux/slab.h> 15#include <linux/of.h> 16#include <linux/platform_device.h> 17#include <linux/regmap.h> 18#include <linux/regulator/driver.h> 19#include <linux/regulator/machine.h> 20#include <linux/regulator/of_regulator.h> 21#include <linux/mfd/da9063/core.h> 22#include <linux/mfd/da9063/registers.h> 23 24 25/* Definition for registering regmap bit fields using a mask */ 26#define BFIELD(_reg, _mask) \ 27 REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \ 28 sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1) 29 30/* DA9063 and DA9063L regulator IDs */ 31enum { 32 /* BUCKs */ 33 DA9063_ID_BCORE1, 34 DA9063_ID_BCORE2, 35 DA9063_ID_BPRO, 36 DA9063_ID_BMEM, 37 DA9063_ID_BIO, 38 DA9063_ID_BPERI, 39 40 /* BCORE1 and BCORE2 in merged mode */ 41 DA9063_ID_BCORES_MERGED, 42 /* BMEM and BIO in merged mode */ 43 DA9063_ID_BMEM_BIO_MERGED, 44 /* When two BUCKs are merged, they cannot be reused separately */ 45 46 /* LDOs on both DA9063 and DA9063L */ 47 DA9063_ID_LDO3, 48 DA9063_ID_LDO7, 49 DA9063_ID_LDO8, 50 DA9063_ID_LDO9, 51 DA9063_ID_LDO11, 52 53 /* DA9063-only LDOs */ 54 DA9063_ID_LDO1, 55 DA9063_ID_LDO2, 56 DA9063_ID_LDO4, 57 DA9063_ID_LDO5, 58 DA9063_ID_LDO6, 59 DA9063_ID_LDO10, 60}; 61 62/* Old regulator platform data */ 63struct da9063_regulator_data { 64 int id; 65 struct regulator_init_data *initdata; 66}; 67 68struct da9063_regulators_pdata { 69 unsigned int n_regulators; 70 struct da9063_regulator_data *regulator_data; 71}; 72 73/* Regulator capabilities and registers description */ 74struct da9063_regulator_info { 75 struct regulator_desc desc; 76 77 /* DA9063 main register fields */ 78 struct reg_field mode; /* buck mode of operation */ 79 struct reg_field suspend; 80 struct reg_field sleep; 81 struct reg_field suspend_sleep; 82 unsigned int suspend_vsel_reg; 83 84 /* DA9063 event detection bit */ 85 struct reg_field oc_event; 86}; 87 88/* Macros for LDO */ 89#define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \ 90 .desc.id = chip##_ID_##regl_name, \ 91 .desc.name = __stringify(chip##_##regl_name), \ 92 .desc.ops = &da9063_ldo_ops, \ 93 .desc.min_uV = (min_mV) * 1000, \ 94 .desc.uV_step = (step_mV) * 1000, \ 95 .desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \ 96 + (DA9063_V##regl_name##_BIAS)), \ 97 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \ 98 .desc.enable_mask = DA9063_LDO_EN, \ 99 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \ 100 .desc.vsel_mask = DA9063_V##regl_name##_MASK, \ 101 .desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \ 102 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \ 103 .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \ 104 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \ 105 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B 106 107/* Macros for voltage DC/DC converters (BUCKs) */ 108#define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \ 109 creg, cmask) \ 110 .desc.id = chip##_ID_##regl_name, \ 111 .desc.name = __stringify(chip##_##regl_name), \ 112 .desc.ops = &da9063_buck_ops, \ 113 .desc.min_uV = (min_mV) * 1000, \ 114 .desc.uV_step = (step_mV) * 1000, \ 115 .desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \ 116 .desc.csel_reg = (creg), \ 117 .desc.csel_mask = (cmask), \ 118 .desc.curr_table = limits_array, \ 119 .desc.n_current_limits = ARRAY_SIZE(limits_array) 120 121#define DA9063_BUCK_COMMON_FIELDS(regl_name) \ 122 .desc.enable_reg = DA9063_REG_##regl_name##_CONT, \ 123 .desc.enable_mask = DA9063_BUCK_EN, \ 124 .desc.vsel_reg = DA9063_REG_V##regl_name##_A, \ 125 .desc.vsel_mask = DA9063_VBUCK_MASK, \ 126 .desc.linear_min_sel = DA9063_VBUCK_BIAS, \ 127 .sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \ 128 .suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \ 129 .suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \ 130 .suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \ 131 .mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK) 132 133/* Defines asignment of regulators info table to chip model */ 134struct da9063_dev_model { 135 const struct da9063_regulator_info *regulator_info; 136 unsigned int n_regulators; 137 enum da9063_type type; 138}; 139 140/* Single regulator settings */ 141struct da9063_regulator { 142 struct regulator_desc desc; 143 struct regulator_dev *rdev; 144 struct da9063 *hw; 145 const struct da9063_regulator_info *info; 146 147 struct regmap_field *mode; 148 struct regmap_field *suspend; 149 struct regmap_field *sleep; 150 struct regmap_field *suspend_sleep; 151}; 152 153/* Encapsulates all information for the regulators driver */ 154struct da9063_regulators { 155 unsigned int n_regulators; 156 /* Array size to be defined during init. Keep at end. */ 157 struct da9063_regulator regulator[]; 158}; 159 160/* BUCK modes for DA9063 */ 161enum { 162 BUCK_MODE_MANUAL, /* 0 */ 163 BUCK_MODE_SLEEP, /* 1 */ 164 BUCK_MODE_SYNC, /* 2 */ 165 BUCK_MODE_AUTO /* 3 */ 166}; 167 168/* Regulator operations */ 169 170/* 171 * Current limits array (in uA) for BCORE1, BCORE2, BPRO. 172 * Entry indexes corresponds to register values. 173 */ 174static const unsigned int da9063_buck_a_limits[] = { 175 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000, 176 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000 177}; 178 179/* 180 * Current limits array (in uA) for BMEM, BIO, BPERI. 181 * Entry indexes corresponds to register values. 182 */ 183static const unsigned int da9063_buck_b_limits[] = { 184 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000, 185 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000 186}; 187 188/* 189 * Current limits array (in uA) for merged BCORE1 and BCORE2. 190 * Entry indexes corresponds to register values. 191 */ 192static const unsigned int da9063_bcores_merged_limits[] = { 193 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000, 194 2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000 195}; 196 197/* 198 * Current limits array (in uA) for merged BMEM and BIO. 199 * Entry indexes corresponds to register values. 200 */ 201static const unsigned int da9063_bmem_bio_merged_limits[] = { 202 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, 203 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000 204}; 205 206static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode) 207{ 208 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 209 unsigned int val; 210 211 switch (mode) { 212 case REGULATOR_MODE_FAST: 213 val = BUCK_MODE_SYNC; 214 break; 215 case REGULATOR_MODE_NORMAL: 216 val = BUCK_MODE_AUTO; 217 break; 218 case REGULATOR_MODE_STANDBY: 219 val = BUCK_MODE_SLEEP; 220 break; 221 default: 222 return -EINVAL; 223 } 224 225 return regmap_field_write(regl->mode, val); 226} 227 228/* 229 * Bucks use single mode register field for normal operation 230 * and suspend state. 231 * There are 3 modes to map to: FAST, NORMAL, and STANDBY. 232 */ 233 234static unsigned int da9063_buck_get_mode(struct regulator_dev *rdev) 235{ 236 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 237 unsigned int val; 238 int ret; 239 240 ret = regmap_field_read(regl->mode, &val); 241 if (ret < 0) 242 return ret; 243 244 switch (val) { 245 default: 246 case BUCK_MODE_MANUAL: 247 /* Sleep flag bit decides the mode */ 248 break; 249 case BUCK_MODE_SLEEP: 250 return REGULATOR_MODE_STANDBY; 251 case BUCK_MODE_SYNC: 252 return REGULATOR_MODE_FAST; 253 case BUCK_MODE_AUTO: 254 return REGULATOR_MODE_NORMAL; 255 } 256 257 ret = regmap_field_read(regl->sleep, &val); 258 if (ret < 0) 259 return 0; 260 261 if (val) 262 return REGULATOR_MODE_STANDBY; 263 else 264 return REGULATOR_MODE_FAST; 265} 266 267/* 268 * LDOs use sleep flags - one for normal and one for suspend state. 269 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state. 270 */ 271 272static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode) 273{ 274 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 275 unsigned int val; 276 277 switch (mode) { 278 case REGULATOR_MODE_NORMAL: 279 val = 0; 280 break; 281 case REGULATOR_MODE_STANDBY: 282 val = 1; 283 break; 284 default: 285 return -EINVAL; 286 } 287 288 return regmap_field_write(regl->sleep, val); 289} 290 291static unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev) 292{ 293 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 294 int ret, val; 295 296 ret = regmap_field_read(regl->sleep, &val); 297 if (ret < 0) 298 return 0; 299 300 if (val) 301 return REGULATOR_MODE_STANDBY; 302 else 303 return REGULATOR_MODE_NORMAL; 304} 305 306static int da9063_buck_get_status(struct regulator_dev *rdev) 307{ 308 int ret = regulator_is_enabled_regmap(rdev); 309 310 if (ret == 0) { 311 ret = REGULATOR_STATUS_OFF; 312 } else if (ret > 0) { 313 ret = da9063_buck_get_mode(rdev); 314 if (ret > 0) 315 ret = regulator_mode_to_status(ret); 316 else if (ret == 0) 317 ret = -EIO; 318 } 319 320 return ret; 321} 322 323static int da9063_ldo_get_status(struct regulator_dev *rdev) 324{ 325 int ret = regulator_is_enabled_regmap(rdev); 326 327 if (ret == 0) { 328 ret = REGULATOR_STATUS_OFF; 329 } else if (ret > 0) { 330 ret = da9063_ldo_get_mode(rdev); 331 if (ret > 0) 332 ret = regulator_mode_to_status(ret); 333 else if (ret == 0) 334 ret = -EIO; 335 } 336 337 return ret; 338} 339 340static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV) 341{ 342 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 343 const struct da9063_regulator_info *rinfo = regl->info; 344 int ret, sel; 345 346 sel = regulator_map_voltage_linear(rdev, uV, uV); 347 if (sel < 0) 348 return sel; 349 350 sel <<= ffs(rdev->desc->vsel_mask) - 1; 351 352 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg, 353 rdev->desc->vsel_mask, sel); 354 355 return ret; 356} 357 358static int da9063_suspend_enable(struct regulator_dev *rdev) 359{ 360 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 361 362 return regmap_field_write(regl->suspend, 1); 363} 364 365static int da9063_suspend_disable(struct regulator_dev *rdev) 366{ 367 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 368 369 return regmap_field_write(regl->suspend, 0); 370} 371 372static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev, 373 unsigned int mode) 374{ 375 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 376 int val; 377 378 switch (mode) { 379 case REGULATOR_MODE_FAST: 380 val = BUCK_MODE_SYNC; 381 break; 382 case REGULATOR_MODE_NORMAL: 383 val = BUCK_MODE_AUTO; 384 break; 385 case REGULATOR_MODE_STANDBY: 386 val = BUCK_MODE_SLEEP; 387 break; 388 default: 389 return -EINVAL; 390 } 391 392 return regmap_field_write(regl->mode, val); 393} 394 395static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev, 396 unsigned int mode) 397{ 398 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 399 unsigned int val; 400 401 switch (mode) { 402 case REGULATOR_MODE_NORMAL: 403 val = 0; 404 break; 405 case REGULATOR_MODE_STANDBY: 406 val = 1; 407 break; 408 default: 409 return -EINVAL; 410 } 411 412 return regmap_field_write(regl->suspend_sleep, val); 413} 414 415static unsigned int da9063_get_overdrive_mask(const struct regulator_desc *desc) 416{ 417 switch (desc->id) { 418 case DA9063_ID_BCORES_MERGED: 419 case DA9063_ID_BCORE1: 420 return DA9063_BCORE1_OD; 421 case DA9063_ID_BCORE2: 422 return DA9063_BCORE2_OD; 423 case DA9063_ID_BPRO: 424 return DA9063_BPRO_OD; 425 default: 426 return 0; 427 } 428} 429 430static int da9063_buck_set_limit_set_overdrive(struct regulator_dev *rdev, 431 int min_uA, int max_uA, 432 unsigned int overdrive_mask) 433{ 434 /* 435 * When enabling overdrive, do it before changing the current limit to 436 * ensure sufficient supply throughout the switch. 437 */ 438 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 439 int ret; 440 unsigned int orig_overdrive; 441 442 ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H, 443 &orig_overdrive); 444 if (ret < 0) 445 return ret; 446 orig_overdrive &= overdrive_mask; 447 448 if (orig_overdrive == 0) { 449 ret = regmap_set_bits(regl->hw->regmap, DA9063_REG_CONFIG_H, 450 overdrive_mask); 451 if (ret < 0) 452 return ret; 453 } 454 455 ret = regulator_set_current_limit_regmap(rdev, min_uA / 2, max_uA / 2); 456 if (ret < 0 && orig_overdrive == 0) 457 /* 458 * regulator_set_current_limit_regmap may have rejected the 459 * change because of unusable min_uA and/or max_uA inputs. 460 * Attempt to restore original overdrive state, ignore failure- 461 * on-failure. 462 */ 463 regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H, 464 overdrive_mask); 465 466 return ret; 467} 468 469static int da9063_buck_set_limit_clear_overdrive(struct regulator_dev *rdev, 470 int min_uA, int max_uA, 471 unsigned int overdrive_mask) 472{ 473 /* 474 * When disabling overdrive, do it after changing the current limit to 475 * ensure sufficient supply throughout the switch. 476 */ 477 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 478 int ret, orig_limit; 479 480 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &orig_limit); 481 if (ret < 0) 482 return ret; 483 484 ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA); 485 if (ret < 0) 486 return ret; 487 488 ret = regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H, 489 overdrive_mask); 490 if (ret < 0) 491 /* 492 * Attempt to restore original current limit, ignore failure- 493 * on-failure. 494 */ 495 regmap_write(rdev->regmap, rdev->desc->csel_reg, orig_limit); 496 497 return ret; 498} 499 500static int da9063_buck_set_current_limit(struct regulator_dev *rdev, 501 int min_uA, int max_uA) 502{ 503 unsigned int overdrive_mask, n_currents; 504 505 overdrive_mask = da9063_get_overdrive_mask(rdev->desc); 506 if (overdrive_mask) { 507 n_currents = rdev->desc->n_current_limits; 508 if (n_currents == 0) 509 return -EINVAL; 510 511 if (max_uA > rdev->desc->curr_table[n_currents - 1]) 512 return da9063_buck_set_limit_set_overdrive(rdev, min_uA, 513 max_uA, 514 overdrive_mask); 515 516 return da9063_buck_set_limit_clear_overdrive(rdev, min_uA, 517 max_uA, 518 overdrive_mask); 519 } 520 return regulator_set_current_limit_regmap(rdev, min_uA, max_uA); 521} 522 523static int da9063_buck_get_current_limit(struct regulator_dev *rdev) 524{ 525 struct da9063_regulator *regl = rdev_get_drvdata(rdev); 526 int val, ret, limit; 527 unsigned int mask; 528 529 limit = regulator_get_current_limit_regmap(rdev); 530 if (limit < 0) 531 return limit; 532 mask = da9063_get_overdrive_mask(rdev->desc); 533 if (mask) { 534 ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H, &val); 535 if (ret < 0) 536 return ret; 537 if (val & mask) 538 limit *= 2; 539 } 540 return limit; 541} 542 543static const struct regulator_ops da9063_buck_ops = { 544 .enable = regulator_enable_regmap, 545 .disable = regulator_disable_regmap, 546 .is_enabled = regulator_is_enabled_regmap, 547 .get_voltage_sel = regulator_get_voltage_sel_regmap, 548 .set_voltage_sel = regulator_set_voltage_sel_regmap, 549 .list_voltage = regulator_list_voltage_linear, 550 .set_current_limit = da9063_buck_set_current_limit, 551 .get_current_limit = da9063_buck_get_current_limit, 552 .set_mode = da9063_buck_set_mode, 553 .get_mode = da9063_buck_get_mode, 554 .get_status = da9063_buck_get_status, 555 .set_suspend_voltage = da9063_set_suspend_voltage, 556 .set_suspend_enable = da9063_suspend_enable, 557 .set_suspend_disable = da9063_suspend_disable, 558 .set_suspend_mode = da9063_buck_set_suspend_mode, 559}; 560 561static const struct regulator_ops da9063_ldo_ops = { 562 .enable = regulator_enable_regmap, 563 .disable = regulator_disable_regmap, 564 .is_enabled = regulator_is_enabled_regmap, 565 .get_voltage_sel = regulator_get_voltage_sel_regmap, 566 .set_voltage_sel = regulator_set_voltage_sel_regmap, 567 .list_voltage = regulator_list_voltage_linear, 568 .set_mode = da9063_ldo_set_mode, 569 .get_mode = da9063_ldo_get_mode, 570 .get_status = da9063_ldo_get_status, 571 .set_suspend_voltage = da9063_set_suspend_voltage, 572 .set_suspend_enable = da9063_suspend_enable, 573 .set_suspend_disable = da9063_suspend_disable, 574 .set_suspend_mode = da9063_ldo_set_suspend_mode, 575}; 576 577/* Info of regulators for DA9063 */ 578static const struct da9063_regulator_info da9063_regulator_info[] = { 579 { 580 DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570, 581 da9063_buck_a_limits, 582 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK), 583 DA9063_BUCK_COMMON_FIELDS(BCORE1), 584 }, 585 { 586 DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570, 587 da9063_buck_a_limits, 588 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK), 589 DA9063_BUCK_COMMON_FIELDS(BCORE2), 590 }, 591 { 592 DA9063_BUCK(DA9063, BPRO, 530, 10, 1800, 593 da9063_buck_a_limits, 594 DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK), 595 DA9063_BUCK_COMMON_FIELDS(BPRO), 596 }, 597 { 598 DA9063_BUCK(DA9063, BMEM, 800, 20, 3340, 599 da9063_buck_b_limits, 600 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK), 601 DA9063_BUCK_COMMON_FIELDS(BMEM), 602 }, 603 { 604 DA9063_BUCK(DA9063, BIO, 800, 20, 3340, 605 da9063_buck_b_limits, 606 DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK), 607 DA9063_BUCK_COMMON_FIELDS(BIO), 608 }, 609 { 610 DA9063_BUCK(DA9063, BPERI, 800, 20, 3340, 611 da9063_buck_b_limits, 612 DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK), 613 DA9063_BUCK_COMMON_FIELDS(BPERI), 614 }, 615 { 616 DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570, 617 da9063_bcores_merged_limits, 618 DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK), 619 /* BCORES_MERGED uses the same register fields as BCORE1 */ 620 DA9063_BUCK_COMMON_FIELDS(BCORE1), 621 }, 622 { 623 DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340, 624 da9063_bmem_bio_merged_limits, 625 DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK), 626 /* BMEM_BIO_MERGED uses the same register fields as BMEM */ 627 DA9063_BUCK_COMMON_FIELDS(BMEM), 628 }, 629 { 630 DA9063_LDO(DA9063, LDO3, 900, 20, 3440), 631 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM), 632 }, 633 { 634 DA9063_LDO(DA9063, LDO7, 900, 50, 3600), 635 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM), 636 }, 637 { 638 DA9063_LDO(DA9063, LDO8, 900, 50, 3600), 639 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM), 640 }, 641 { 642 DA9063_LDO(DA9063, LDO9, 950, 50, 3600), 643 }, 644 { 645 DA9063_LDO(DA9063, LDO11, 900, 50, 3600), 646 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM), 647 }, 648 649 /* The following LDOs are present only on DA9063, not on DA9063L */ 650 { 651 DA9063_LDO(DA9063, LDO1, 600, 20, 1860), 652 }, 653 { 654 DA9063_LDO(DA9063, LDO2, 600, 20, 1860), 655 }, 656 { 657 DA9063_LDO(DA9063, LDO4, 900, 20, 3440), 658 .oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM), 659 }, 660 { 661 DA9063_LDO(DA9063, LDO5, 900, 50, 3600), 662 }, 663 { 664 DA9063_LDO(DA9063, LDO6, 900, 50, 3600), 665 }, 666 667 { 668 DA9063_LDO(DA9063, LDO10, 900, 50, 3600), 669 }, 670}; 671 672/* Link chip model with regulators info table */ 673static struct da9063_dev_model regulators_models[] = { 674 { 675 .regulator_info = da9063_regulator_info, 676 .n_regulators = ARRAY_SIZE(da9063_regulator_info), 677 .type = PMIC_TYPE_DA9063, 678 }, 679 { 680 .regulator_info = da9063_regulator_info, 681 .n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6, 682 .type = PMIC_TYPE_DA9063L, 683 }, 684 { } 685}; 686 687/* Regulator interrupt handlers */ 688static irqreturn_t da9063_ldo_lim_event(int irq, void *data) 689{ 690 struct da9063_regulators *regulators = data; 691 struct da9063 *hw = regulators->regulator[0].hw; 692 struct da9063_regulator *regl; 693 int bits, i, ret; 694 695 ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits); 696 if (ret < 0) 697 return IRQ_NONE; 698 699 for (i = regulators->n_regulators - 1; i >= 0; i--) { 700 regl = ®ulators->regulator[i]; 701 if (regl->info->oc_event.reg != DA9063_REG_STATUS_D) 702 continue; 703 704 if (BIT(regl->info->oc_event.lsb) & bits) { 705 regulator_notifier_call_chain(regl->rdev, 706 REGULATOR_EVENT_OVER_CURRENT, NULL); 707 } 708 } 709 710 return IRQ_HANDLED; 711} 712 713/* 714 * Probing and Initialisation functions 715 */ 716static const struct regulator_init_data *da9063_get_regulator_initdata( 717 const struct da9063_regulators_pdata *regl_pdata, int id) 718{ 719 int i; 720 721 for (i = 0; i < regl_pdata->n_regulators; i++) { 722 if (id == regl_pdata->regulator_data[i].id) 723 return regl_pdata->regulator_data[i].initdata; 724 } 725 726 return NULL; 727} 728 729static struct of_regulator_match da9063_matches[] = { 730 [DA9063_ID_BCORE1] = { .name = "bcore1" }, 731 [DA9063_ID_BCORE2] = { .name = "bcore2" }, 732 [DA9063_ID_BPRO] = { .name = "bpro", }, 733 [DA9063_ID_BMEM] = { .name = "bmem", }, 734 [DA9063_ID_BIO] = { .name = "bio", }, 735 [DA9063_ID_BPERI] = { .name = "bperi", }, 736 [DA9063_ID_BCORES_MERGED] = { .name = "bcores-merged" }, 737 [DA9063_ID_BMEM_BIO_MERGED] = { .name = "bmem-bio-merged", }, 738 [DA9063_ID_LDO3] = { .name = "ldo3", }, 739 [DA9063_ID_LDO7] = { .name = "ldo7", }, 740 [DA9063_ID_LDO8] = { .name = "ldo8", }, 741 [DA9063_ID_LDO9] = { .name = "ldo9", }, 742 [DA9063_ID_LDO11] = { .name = "ldo11", }, 743 /* The following LDOs are present only on DA9063, not on DA9063L */ 744 [DA9063_ID_LDO1] = { .name = "ldo1", }, 745 [DA9063_ID_LDO2] = { .name = "ldo2", }, 746 [DA9063_ID_LDO4] = { .name = "ldo4", }, 747 [DA9063_ID_LDO5] = { .name = "ldo5", }, 748 [DA9063_ID_LDO6] = { .name = "ldo6", }, 749 [DA9063_ID_LDO10] = { .name = "ldo10", }, 750}; 751 752static struct da9063_regulators_pdata *da9063_parse_regulators_dt( 753 struct platform_device *pdev, 754 struct of_regulator_match **da9063_reg_matches) 755{ 756 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent); 757 struct da9063_regulators_pdata *pdata; 758 struct da9063_regulator_data *rdata; 759 struct device_node *node; 760 int da9063_matches_len = ARRAY_SIZE(da9063_matches); 761 int i, n, num; 762 763 if (da9063->type == PMIC_TYPE_DA9063L) 764 da9063_matches_len -= 6; 765 766 node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators"); 767 if (!node) { 768 dev_err(&pdev->dev, "Regulators device node not found\n"); 769 return ERR_PTR(-ENODEV); 770 } 771 772 num = of_regulator_match(&pdev->dev, node, da9063_matches, 773 da9063_matches_len); 774 of_node_put(node); 775 if (num < 0) { 776 dev_err(&pdev->dev, "Failed to match regulators\n"); 777 return ERR_PTR(-EINVAL); 778 } 779 780 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 781 if (!pdata) 782 return ERR_PTR(-ENOMEM); 783 784 pdata->regulator_data = devm_kcalloc(&pdev->dev, 785 num, sizeof(*pdata->regulator_data), 786 GFP_KERNEL); 787 if (!pdata->regulator_data) 788 return ERR_PTR(-ENOMEM); 789 pdata->n_regulators = num; 790 791 n = 0; 792 for (i = 0; i < da9063_matches_len; i++) { 793 if (!da9063_matches[i].init_data) 794 continue; 795 796 rdata = &pdata->regulator_data[n]; 797 rdata->id = i; 798 rdata->initdata = da9063_matches[i].init_data; 799 800 n++; 801 } 802 803 *da9063_reg_matches = da9063_matches; 804 return pdata; 805} 806 807static int da9063_regulator_probe(struct platform_device *pdev) 808{ 809 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent); 810 struct of_regulator_match *da9063_reg_matches = NULL; 811 struct da9063_regulators_pdata *regl_pdata; 812 const struct da9063_dev_model *model; 813 struct da9063_regulators *regulators; 814 struct da9063_regulator *regl; 815 struct regulator_config config; 816 bool bcores_merged, bmem_bio_merged; 817 int id, irq, n, n_regulators, ret, val; 818 819 regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches); 820 821 if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) { 822 dev_err(&pdev->dev, 823 "No regulators defined for the platform\n"); 824 return -ENODEV; 825 } 826 827 /* Find regulators set for particular device model */ 828 for (model = regulators_models; model->regulator_info; model++) { 829 if (model->type == da9063->type) 830 break; 831 } 832 if (!model->regulator_info) { 833 dev_err(&pdev->dev, "Chip model not recognised (%u)\n", 834 da9063->type); 835 return -ENODEV; 836 } 837 838 ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val); 839 if (ret < 0) { 840 dev_err(&pdev->dev, 841 "Error while reading BUCKs configuration\n"); 842 return ret; 843 } 844 bcores_merged = val & DA9063_BCORE_MERGE; 845 bmem_bio_merged = val & DA9063_BUCK_MERGE; 846 847 n_regulators = model->n_regulators; 848 if (bcores_merged) 849 n_regulators -= 2; /* remove BCORE1, BCORE2 */ 850 else 851 n_regulators--; /* remove BCORES_MERGED */ 852 if (bmem_bio_merged) 853 n_regulators -= 2; /* remove BMEM, BIO */ 854 else 855 n_regulators--; /* remove BMEM_BIO_MERGED */ 856 857 /* Allocate memory required by usable regulators */ 858 regulators = devm_kzalloc(&pdev->dev, struct_size(regulators, 859 regulator, n_regulators), GFP_KERNEL); 860 if (!regulators) 861 return -ENOMEM; 862 863 regulators->n_regulators = n_regulators; 864 platform_set_drvdata(pdev, regulators); 865 866 /* Register all regulators declared in platform information */ 867 n = 0; 868 id = 0; 869 while (n < regulators->n_regulators) { 870 /* Skip regulator IDs depending on merge mode configuration */ 871 switch (id) { 872 case DA9063_ID_BCORE1: 873 case DA9063_ID_BCORE2: 874 if (bcores_merged) { 875 id++; 876 continue; 877 } 878 break; 879 case DA9063_ID_BMEM: 880 case DA9063_ID_BIO: 881 if (bmem_bio_merged) { 882 id++; 883 continue; 884 } 885 break; 886 case DA9063_ID_BCORES_MERGED: 887 if (!bcores_merged) { 888 id++; 889 continue; 890 } 891 break; 892 case DA9063_ID_BMEM_BIO_MERGED: 893 if (!bmem_bio_merged) { 894 id++; 895 continue; 896 } 897 break; 898 } 899 900 /* Initialise regulator structure */ 901 regl = ®ulators->regulator[n]; 902 regl->hw = da9063; 903 regl->info = &model->regulator_info[id]; 904 regl->desc = regl->info->desc; 905 regl->desc.type = REGULATOR_VOLTAGE; 906 regl->desc.owner = THIS_MODULE; 907 908 if (regl->info->mode.reg) { 909 regl->mode = devm_regmap_field_alloc(&pdev->dev, 910 da9063->regmap, regl->info->mode); 911 if (IS_ERR(regl->mode)) 912 return PTR_ERR(regl->mode); 913 } 914 915 if (regl->info->suspend.reg) { 916 regl->suspend = devm_regmap_field_alloc(&pdev->dev, 917 da9063->regmap, regl->info->suspend); 918 if (IS_ERR(regl->suspend)) 919 return PTR_ERR(regl->suspend); 920 } 921 922 if (regl->info->sleep.reg) { 923 regl->sleep = devm_regmap_field_alloc(&pdev->dev, 924 da9063->regmap, regl->info->sleep); 925 if (IS_ERR(regl->sleep)) 926 return PTR_ERR(regl->sleep); 927 } 928 929 if (regl->info->suspend_sleep.reg) { 930 regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev, 931 da9063->regmap, regl->info->suspend_sleep); 932 if (IS_ERR(regl->suspend_sleep)) 933 return PTR_ERR(regl->suspend_sleep); 934 } 935 936 /* Register regulator */ 937 memset(&config, 0, sizeof(config)); 938 config.dev = &pdev->dev; 939 config.init_data = da9063_get_regulator_initdata(regl_pdata, id); 940 config.driver_data = regl; 941 if (da9063_reg_matches) 942 config.of_node = da9063_reg_matches[id].of_node; 943 config.regmap = da9063->regmap; 944 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc, 945 &config); 946 if (IS_ERR(regl->rdev)) { 947 dev_err(&pdev->dev, 948 "Failed to register %s regulator\n", 949 regl->desc.name); 950 return PTR_ERR(regl->rdev); 951 } 952 id++; 953 n++; 954 } 955 956 /* LDOs overcurrent event support */ 957 irq = platform_get_irq_byname(pdev, "LDO_LIM"); 958 if (irq < 0) 959 return irq; 960 961 ret = devm_request_threaded_irq(&pdev->dev, irq, 962 NULL, da9063_ldo_lim_event, 963 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 964 "LDO_LIM", regulators); 965 if (ret) 966 dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n"); 967 968 return ret; 969} 970 971static struct platform_driver da9063_regulator_driver = { 972 .driver = { 973 .name = DA9063_DRVNAME_REGULATORS, 974 }, 975 .probe = da9063_regulator_probe, 976}; 977 978static int __init da9063_regulator_init(void) 979{ 980 return platform_driver_register(&da9063_regulator_driver); 981} 982subsys_initcall(da9063_regulator_init); 983 984static void __exit da9063_regulator_cleanup(void) 985{ 986 platform_driver_unregister(&da9063_regulator_driver); 987} 988module_exit(da9063_regulator_cleanup); 989 990 991/* Module information */ 992MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>"); 993MODULE_DESCRIPTION("DA9063 regulators driver"); 994MODULE_LICENSE("GPL"); 995MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);