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