1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Regulator device driver for DA9061 and DA9062. 4 // Copyright (C) 2015-2017 Dialog Semiconductor 5 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/err.h> 10 #include <linux/slab.h> 11 #include <linux/of.h> 12 #include <linux/platform_device.h> 13 #include <linux/regmap.h> 14 #include <linux/regulator/driver.h> 15 #include <linux/regulator/machine.h> 16 #include <linux/regulator/of_regulator.h> 17 #include <linux/mfd/da9062/core.h> 18 #include <linux/mfd/da9062/registers.h> 19 20 /* Regulator IDs */ 21 enum { 22 DA9061_ID_BUCK1, 23 DA9061_ID_BUCK2, 24 DA9061_ID_BUCK3, 25 DA9061_ID_LDO1, 26 DA9061_ID_LDO2, 27 DA9061_ID_LDO3, 28 DA9061_ID_LDO4, 29 DA9061_MAX_REGULATORS, 30 }; 31 32 enum { 33 DA9062_ID_BUCK1, 34 DA9062_ID_BUCK2, 35 DA9062_ID_BUCK3, 36 DA9062_ID_BUCK4, 37 DA9062_ID_LDO1, 38 DA9062_ID_LDO2, 39 DA9062_ID_LDO3, 40 DA9062_ID_LDO4, 41 DA9062_MAX_REGULATORS, 42 }; 43 44 /* Regulator capabilities and registers description */ 45 struct da9062_regulator_info { 46 struct regulator_desc desc; 47 /* Main register fields */ 48 struct reg_field mode; 49 struct reg_field suspend; 50 struct reg_field sleep; 51 struct reg_field suspend_sleep; 52 unsigned int suspend_vsel_reg; 53 /* Event detection bit */ 54 struct reg_field oc_event; 55 }; 56 57 /* Single regulator settings */ 58 struct da9062_regulator { 59 struct regulator_desc desc; 60 struct regulator_dev *rdev; 61 struct da9062 *hw; 62 const struct da9062_regulator_info *info; 63 64 struct regmap_field *mode; 65 struct regmap_field *suspend; 66 struct regmap_field *sleep; 67 struct regmap_field *suspend_sleep; 68 }; 69 70 /* Encapsulates all information for the regulators driver */ 71 struct da9062_regulators { 72 int irq_ldo_lim; 73 unsigned n_regulators; 74 /* Array size to be defined during init. Keep at end. */ 75 struct da9062_regulator regulator[0]; 76 }; 77 78 /* BUCK modes */ 79 enum { 80 BUCK_MODE_MANUAL, /* 0 */ 81 BUCK_MODE_SLEEP, /* 1 */ 82 BUCK_MODE_SYNC, /* 2 */ 83 BUCK_MODE_AUTO /* 3 */ 84 }; 85 86 /* Regulator operations */ 87 88 /* Current limits array (in uA) 89 * - DA9061_ID_[BUCK1|BUCK3] 90 * - DA9062_ID_[BUCK1|BUCK2|BUCK4] 91 * Entry indexes corresponds to register values. 92 */ 93 static const unsigned int da9062_buck_a_limits[] = { 94 500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000, 95 1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000 96 }; 97 98 /* Current limits array (in uA) 99 * - DA9061_ID_BUCK2 100 * - DA9062_ID_BUCK3 101 * Entry indexes corresponds to register values. 102 */ 103 static const unsigned int da9062_buck_b_limits[] = { 104 1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000, 105 2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000 106 }; 107 108 static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode) 109 { 110 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 111 unsigned val; 112 113 switch (mode) { 114 case REGULATOR_MODE_FAST: 115 val = BUCK_MODE_SYNC; 116 break; 117 case REGULATOR_MODE_NORMAL: 118 val = BUCK_MODE_AUTO; 119 break; 120 case REGULATOR_MODE_STANDBY: 121 val = BUCK_MODE_SLEEP; 122 break; 123 default: 124 return -EINVAL; 125 } 126 127 return regmap_field_write(regl->mode, val); 128 } 129 130 /* 131 * Bucks use single mode register field for normal operation 132 * and suspend state. 133 * There are 3 modes to map to: FAST, NORMAL, and STANDBY. 134 */ 135 136 static unsigned da9062_buck_get_mode(struct regulator_dev *rdev) 137 { 138 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 139 unsigned int val, mode = 0; 140 int ret; 141 142 ret = regmap_field_read(regl->mode, &val); 143 if (ret < 0) 144 return ret; 145 146 switch (val) { 147 default: 148 case BUCK_MODE_MANUAL: 149 mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY; 150 /* Sleep flag bit decides the mode */ 151 break; 152 case BUCK_MODE_SLEEP: 153 return REGULATOR_MODE_STANDBY; 154 case BUCK_MODE_SYNC: 155 return REGULATOR_MODE_FAST; 156 case BUCK_MODE_AUTO: 157 return REGULATOR_MODE_NORMAL; 158 } 159 160 ret = regmap_field_read(regl->sleep, &val); 161 if (ret < 0) 162 return 0; 163 164 if (val) 165 mode &= REGULATOR_MODE_STANDBY; 166 else 167 mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST; 168 169 return mode; 170 } 171 172 /* 173 * LDOs use sleep flags - one for normal and one for suspend state. 174 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state. 175 */ 176 177 static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode) 178 { 179 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 180 unsigned val; 181 182 switch (mode) { 183 case REGULATOR_MODE_NORMAL: 184 val = 0; 185 break; 186 case REGULATOR_MODE_STANDBY: 187 val = 1; 188 break; 189 default: 190 return -EINVAL; 191 } 192 193 return regmap_field_write(regl->sleep, val); 194 } 195 196 static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev) 197 { 198 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 199 int ret, val; 200 201 ret = regmap_field_read(regl->sleep, &val); 202 if (ret < 0) 203 return 0; 204 205 if (val) 206 return REGULATOR_MODE_STANDBY; 207 else 208 return REGULATOR_MODE_NORMAL; 209 } 210 211 static int da9062_buck_get_status(struct regulator_dev *rdev) 212 { 213 int ret = regulator_is_enabled_regmap(rdev); 214 215 if (ret == 0) { 216 ret = REGULATOR_STATUS_OFF; 217 } else if (ret > 0) { 218 ret = da9062_buck_get_mode(rdev); 219 if (ret > 0) 220 ret = regulator_mode_to_status(ret); 221 else if (ret == 0) 222 ret = -EIO; 223 } 224 225 return ret; 226 } 227 228 static int da9062_ldo_get_status(struct regulator_dev *rdev) 229 { 230 int ret = regulator_is_enabled_regmap(rdev); 231 232 if (ret == 0) { 233 ret = REGULATOR_STATUS_OFF; 234 } else if (ret > 0) { 235 ret = da9062_ldo_get_mode(rdev); 236 if (ret > 0) 237 ret = regulator_mode_to_status(ret); 238 else if (ret == 0) 239 ret = -EIO; 240 } 241 242 return ret; 243 } 244 245 static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv) 246 { 247 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 248 const struct da9062_regulator_info *rinfo = regl->info; 249 int ret, sel; 250 251 sel = regulator_map_voltage_linear(rdev, uv, uv); 252 if (sel < 0) 253 return sel; 254 255 sel <<= ffs(rdev->desc->vsel_mask) - 1; 256 257 ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg, 258 rdev->desc->vsel_mask, sel); 259 260 return ret; 261 } 262 263 static int da9062_suspend_enable(struct regulator_dev *rdev) 264 { 265 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 266 267 return regmap_field_write(regl->suspend, 1); 268 } 269 270 static int da9062_suspend_disable(struct regulator_dev *rdev) 271 { 272 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 273 274 return regmap_field_write(regl->suspend, 0); 275 } 276 277 static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev, 278 unsigned mode) 279 { 280 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 281 int val; 282 283 switch (mode) { 284 case REGULATOR_MODE_FAST: 285 val = BUCK_MODE_SYNC; 286 break; 287 case REGULATOR_MODE_NORMAL: 288 val = BUCK_MODE_AUTO; 289 break; 290 case REGULATOR_MODE_STANDBY: 291 val = BUCK_MODE_SLEEP; 292 break; 293 default: 294 return -EINVAL; 295 } 296 297 return regmap_field_write(regl->mode, val); 298 } 299 300 static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev, 301 unsigned mode) 302 { 303 struct da9062_regulator *regl = rdev_get_drvdata(rdev); 304 unsigned val; 305 306 switch (mode) { 307 case REGULATOR_MODE_NORMAL: 308 val = 0; 309 break; 310 case REGULATOR_MODE_STANDBY: 311 val = 1; 312 break; 313 default: 314 return -EINVAL; 315 } 316 317 return regmap_field_write(regl->suspend_sleep, val); 318 } 319 320 static const struct regulator_ops da9062_buck_ops = { 321 .enable = regulator_enable_regmap, 322 .disable = regulator_disable_regmap, 323 .is_enabled = regulator_is_enabled_regmap, 324 .get_voltage_sel = regulator_get_voltage_sel_regmap, 325 .set_voltage_sel = regulator_set_voltage_sel_regmap, 326 .list_voltage = regulator_list_voltage_linear, 327 .set_current_limit = regulator_set_current_limit_regmap, 328 .get_current_limit = regulator_get_current_limit_regmap, 329 .set_mode = da9062_buck_set_mode, 330 .get_mode = da9062_buck_get_mode, 331 .get_status = da9062_buck_get_status, 332 .set_suspend_voltage = da9062_set_suspend_voltage, 333 .set_suspend_enable = da9062_suspend_enable, 334 .set_suspend_disable = da9062_suspend_disable, 335 .set_suspend_mode = da9062_buck_set_suspend_mode, 336 }; 337 338 static const struct regulator_ops da9062_ldo_ops = { 339 .enable = regulator_enable_regmap, 340 .disable = regulator_disable_regmap, 341 .is_enabled = regulator_is_enabled_regmap, 342 .get_voltage_sel = regulator_get_voltage_sel_regmap, 343 .set_voltage_sel = regulator_set_voltage_sel_regmap, 344 .list_voltage = regulator_list_voltage_linear, 345 .set_mode = da9062_ldo_set_mode, 346 .get_mode = da9062_ldo_get_mode, 347 .get_status = da9062_ldo_get_status, 348 .set_suspend_voltage = da9062_set_suspend_voltage, 349 .set_suspend_enable = da9062_suspend_enable, 350 .set_suspend_disable = da9062_suspend_disable, 351 .set_suspend_mode = da9062_ldo_set_suspend_mode, 352 }; 353 354 /* DA9061 Regulator information */ 355 static const struct da9062_regulator_info local_da9061_regulator_info[] = { 356 { 357 .desc.id = DA9061_ID_BUCK1, 358 .desc.name = "DA9061 BUCK1", 359 .desc.of_match = of_match_ptr("buck1"), 360 .desc.regulators_node = of_match_ptr("regulators"), 361 .desc.ops = &da9062_buck_ops, 362 .desc.min_uV = (300) * 1000, 363 .desc.uV_step = (10) * 1000, 364 .desc.n_voltages = ((1570) - (300))/(10) + 1, 365 .desc.curr_table = da9062_buck_a_limits, 366 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits), 367 .desc.csel_reg = DA9062AA_BUCK_ILIM_C, 368 .desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK, 369 .desc.enable_reg = DA9062AA_BUCK1_CONT, 370 .desc.enable_mask = DA9062AA_BUCK1_EN_MASK, 371 .desc.vsel_reg = DA9062AA_VBUCK1_A, 372 .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK, 373 .desc.linear_min_sel = 0, 374 .sleep = REG_FIELD(DA9062AA_VBUCK1_A, 375 __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1, 376 sizeof(unsigned int) * 8 - 377 __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1), 378 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B, 379 __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1, 380 sizeof(unsigned int) * 8 - 381 __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1), 382 .suspend_vsel_reg = DA9062AA_VBUCK1_B, 383 .mode = REG_FIELD(DA9062AA_BUCK1_CFG, 384 __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1, 385 sizeof(unsigned int) * 8 - 386 __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1), 387 .suspend = REG_FIELD(DA9062AA_BUCK1_CONT, 388 __builtin_ffs((int)DA9062AA_BUCK1_CONF_MASK) - 1, 389 sizeof(unsigned int) * 8 - 390 __builtin_clz(DA9062AA_BUCK1_CONF_MASK) - 1), 391 }, 392 { 393 .desc.id = DA9061_ID_BUCK2, 394 .desc.name = "DA9061 BUCK2", 395 .desc.of_match = of_match_ptr("buck2"), 396 .desc.regulators_node = of_match_ptr("regulators"), 397 .desc.ops = &da9062_buck_ops, 398 .desc.min_uV = (800) * 1000, 399 .desc.uV_step = (20) * 1000, 400 .desc.n_voltages = ((3340) - (800))/(20) + 1, 401 .desc.curr_table = da9062_buck_b_limits, 402 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits), 403 .desc.csel_reg = DA9062AA_BUCK_ILIM_A, 404 .desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK, 405 .desc.enable_reg = DA9062AA_BUCK3_CONT, 406 .desc.enable_mask = DA9062AA_BUCK3_EN_MASK, 407 .desc.vsel_reg = DA9062AA_VBUCK3_A, 408 .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK, 409 .desc.linear_min_sel = 0, 410 .sleep = REG_FIELD(DA9062AA_VBUCK3_A, 411 __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1, 412 sizeof(unsigned int) * 8 - 413 __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1), 414 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B, 415 __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1, 416 sizeof(unsigned int) * 8 - 417 __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1), 418 .suspend_vsel_reg = DA9062AA_VBUCK3_B, 419 .mode = REG_FIELD(DA9062AA_BUCK3_CFG, 420 __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1, 421 sizeof(unsigned int) * 8 - 422 __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1), 423 .suspend = REG_FIELD(DA9062AA_BUCK3_CONT, 424 __builtin_ffs((int)DA9062AA_BUCK3_CONF_MASK) - 1, 425 sizeof(unsigned int) * 8 - 426 __builtin_clz(DA9062AA_BUCK3_CONF_MASK) - 1), 427 }, 428 { 429 .desc.id = DA9061_ID_BUCK3, 430 .desc.name = "DA9061 BUCK3", 431 .desc.of_match = of_match_ptr("buck3"), 432 .desc.regulators_node = of_match_ptr("regulators"), 433 .desc.ops = &da9062_buck_ops, 434 .desc.min_uV = (530) * 1000, 435 .desc.uV_step = (10) * 1000, 436 .desc.n_voltages = ((1800) - (530))/(10) + 1, 437 .desc.curr_table = da9062_buck_a_limits, 438 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits), 439 .desc.csel_reg = DA9062AA_BUCK_ILIM_B, 440 .desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK, 441 .desc.enable_reg = DA9062AA_BUCK4_CONT, 442 .desc.enable_mask = DA9062AA_BUCK4_EN_MASK, 443 .desc.vsel_reg = DA9062AA_VBUCK4_A, 444 .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK, 445 .desc.linear_min_sel = 0, 446 .sleep = REG_FIELD(DA9062AA_VBUCK4_A, 447 __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1, 448 sizeof(unsigned int) * 8 - 449 __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1), 450 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B, 451 __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1, 452 sizeof(unsigned int) * 8 - 453 __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1), 454 .suspend_vsel_reg = DA9062AA_VBUCK4_B, 455 .mode = REG_FIELD(DA9062AA_BUCK4_CFG, 456 __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1, 457 sizeof(unsigned int) * 8 - 458 __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1), 459 .suspend = REG_FIELD(DA9062AA_BUCK4_CONT, 460 __builtin_ffs((int)DA9062AA_BUCK4_CONF_MASK) - 1, 461 sizeof(unsigned int) * 8 - 462 __builtin_clz(DA9062AA_BUCK4_CONF_MASK) - 1), 463 }, 464 { 465 .desc.id = DA9061_ID_LDO1, 466 .desc.name = "DA9061 LDO1", 467 .desc.of_match = of_match_ptr("ldo1"), 468 .desc.regulators_node = of_match_ptr("regulators"), 469 .desc.ops = &da9062_ldo_ops, 470 .desc.min_uV = (900) * 1000, 471 .desc.uV_step = (50) * 1000, 472 .desc.n_voltages = ((3600) - (900))/(50) + 1 473 + DA9062AA_VLDO_A_MIN_SEL, 474 .desc.enable_reg = DA9062AA_LDO1_CONT, 475 .desc.enable_mask = DA9062AA_LDO1_EN_MASK, 476 .desc.vsel_reg = DA9062AA_VLDO1_A, 477 .desc.vsel_mask = DA9062AA_VLDO1_A_MASK, 478 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 479 .sleep = REG_FIELD(DA9062AA_VLDO1_A, 480 __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1, 481 sizeof(unsigned int) * 8 - 482 __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1), 483 .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B, 484 __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1, 485 sizeof(unsigned int) * 8 - 486 __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1), 487 .suspend_vsel_reg = DA9062AA_VLDO1_B, 488 .suspend = REG_FIELD(DA9062AA_LDO1_CONT, 489 __builtin_ffs((int)DA9062AA_LDO1_CONF_MASK) - 1, 490 sizeof(unsigned int) * 8 - 491 __builtin_clz(DA9062AA_LDO1_CONF_MASK) - 1), 492 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 493 __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1, 494 sizeof(unsigned int) * 8 - 495 __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1), 496 }, 497 { 498 .desc.id = DA9061_ID_LDO2, 499 .desc.name = "DA9061 LDO2", 500 .desc.of_match = of_match_ptr("ldo2"), 501 .desc.regulators_node = of_match_ptr("regulators"), 502 .desc.ops = &da9062_ldo_ops, 503 .desc.min_uV = (900) * 1000, 504 .desc.uV_step = (50) * 1000, 505 .desc.n_voltages = ((3600) - (900))/(50) + 1 506 + DA9062AA_VLDO_A_MIN_SEL, 507 .desc.enable_reg = DA9062AA_LDO2_CONT, 508 .desc.enable_mask = DA9062AA_LDO2_EN_MASK, 509 .desc.vsel_reg = DA9062AA_VLDO2_A, 510 .desc.vsel_mask = DA9062AA_VLDO2_A_MASK, 511 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 512 .sleep = REG_FIELD(DA9062AA_VLDO2_A, 513 __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1, 514 sizeof(unsigned int) * 8 - 515 __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1), 516 .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B, 517 __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1, 518 sizeof(unsigned int) * 8 - 519 __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1), 520 .suspend_vsel_reg = DA9062AA_VLDO2_B, 521 .suspend = REG_FIELD(DA9062AA_LDO2_CONT, 522 __builtin_ffs((int)DA9062AA_LDO2_CONF_MASK) - 1, 523 sizeof(unsigned int) * 8 - 524 __builtin_clz(DA9062AA_LDO2_CONF_MASK) - 1), 525 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 526 __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1, 527 sizeof(unsigned int) * 8 - 528 __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1), 529 }, 530 { 531 .desc.id = DA9061_ID_LDO3, 532 .desc.name = "DA9061 LDO3", 533 .desc.of_match = of_match_ptr("ldo3"), 534 .desc.regulators_node = of_match_ptr("regulators"), 535 .desc.ops = &da9062_ldo_ops, 536 .desc.min_uV = (900) * 1000, 537 .desc.uV_step = (50) * 1000, 538 .desc.n_voltages = ((3600) - (900))/(50) + 1 539 + DA9062AA_VLDO_A_MIN_SEL, 540 .desc.enable_reg = DA9062AA_LDO3_CONT, 541 .desc.enable_mask = DA9062AA_LDO3_EN_MASK, 542 .desc.vsel_reg = DA9062AA_VLDO3_A, 543 .desc.vsel_mask = DA9062AA_VLDO3_A_MASK, 544 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 545 .sleep = REG_FIELD(DA9062AA_VLDO3_A, 546 __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1, 547 sizeof(unsigned int) * 8 - 548 __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1), 549 .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B, 550 __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1, 551 sizeof(unsigned int) * 8 - 552 __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1), 553 .suspend_vsel_reg = DA9062AA_VLDO3_B, 554 .suspend = REG_FIELD(DA9062AA_LDO3_CONT, 555 __builtin_ffs((int)DA9062AA_LDO3_CONF_MASK) - 1, 556 sizeof(unsigned int) * 8 - 557 __builtin_clz(DA9062AA_LDO3_CONF_MASK) - 1), 558 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 559 __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1, 560 sizeof(unsigned int) * 8 - 561 __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1), 562 }, 563 { 564 .desc.id = DA9061_ID_LDO4, 565 .desc.name = "DA9061 LDO4", 566 .desc.of_match = of_match_ptr("ldo4"), 567 .desc.regulators_node = of_match_ptr("regulators"), 568 .desc.ops = &da9062_ldo_ops, 569 .desc.min_uV = (900) * 1000, 570 .desc.uV_step = (50) * 1000, 571 .desc.n_voltages = ((3600) - (900))/(50) + 1 572 + DA9062AA_VLDO_A_MIN_SEL, 573 .desc.enable_reg = DA9062AA_LDO4_CONT, 574 .desc.enable_mask = DA9062AA_LDO4_EN_MASK, 575 .desc.vsel_reg = DA9062AA_VLDO4_A, 576 .desc.vsel_mask = DA9062AA_VLDO4_A_MASK, 577 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 578 .sleep = REG_FIELD(DA9062AA_VLDO4_A, 579 __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1, 580 sizeof(unsigned int) * 8 - 581 __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1), 582 .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B, 583 __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1, 584 sizeof(unsigned int) * 8 - 585 __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1), 586 .suspend_vsel_reg = DA9062AA_VLDO4_B, 587 .suspend = REG_FIELD(DA9062AA_LDO4_CONT, 588 __builtin_ffs((int)DA9062AA_LDO4_CONF_MASK) - 1, 589 sizeof(unsigned int) * 8 - 590 __builtin_clz(DA9062AA_LDO4_CONF_MASK) - 1), 591 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 592 __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1, 593 sizeof(unsigned int) * 8 - 594 __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1), 595 }, 596 }; 597 598 /* DA9062 Regulator information */ 599 static const struct da9062_regulator_info local_da9062_regulator_info[] = { 600 { 601 .desc.id = DA9062_ID_BUCK1, 602 .desc.name = "DA9062 BUCK1", 603 .desc.of_match = of_match_ptr("buck1"), 604 .desc.regulators_node = of_match_ptr("regulators"), 605 .desc.ops = &da9062_buck_ops, 606 .desc.min_uV = (300) * 1000, 607 .desc.uV_step = (10) * 1000, 608 .desc.n_voltages = ((1570) - (300))/(10) + 1, 609 .desc.curr_table = da9062_buck_a_limits, 610 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits), 611 .desc.csel_reg = DA9062AA_BUCK_ILIM_C, 612 .desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK, 613 .desc.enable_reg = DA9062AA_BUCK1_CONT, 614 .desc.enable_mask = DA9062AA_BUCK1_EN_MASK, 615 .desc.vsel_reg = DA9062AA_VBUCK1_A, 616 .desc.vsel_mask = DA9062AA_VBUCK1_A_MASK, 617 .desc.linear_min_sel = 0, 618 .sleep = REG_FIELD(DA9062AA_VBUCK1_A, 619 __builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1, 620 sizeof(unsigned int) * 8 - 621 __builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1), 622 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B, 623 __builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1, 624 sizeof(unsigned int) * 8 - 625 __builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1), 626 .suspend_vsel_reg = DA9062AA_VBUCK1_B, 627 .mode = REG_FIELD(DA9062AA_BUCK1_CFG, 628 __builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1, 629 sizeof(unsigned int) * 8 - 630 __builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1), 631 .suspend = REG_FIELD(DA9062AA_BUCK1_CONT, 632 __builtin_ffs((int)DA9062AA_BUCK1_CONF_MASK) - 1, 633 sizeof(unsigned int) * 8 - 634 __builtin_clz(DA9062AA_BUCK1_CONF_MASK) - 1), 635 }, 636 { 637 .desc.id = DA9062_ID_BUCK2, 638 .desc.name = "DA9062 BUCK2", 639 .desc.of_match = of_match_ptr("buck2"), 640 .desc.regulators_node = of_match_ptr("regulators"), 641 .desc.ops = &da9062_buck_ops, 642 .desc.min_uV = (300) * 1000, 643 .desc.uV_step = (10) * 1000, 644 .desc.n_voltages = ((1570) - (300))/(10) + 1, 645 .desc.curr_table = da9062_buck_a_limits, 646 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits), 647 .desc.csel_reg = DA9062AA_BUCK_ILIM_C, 648 .desc.csel_mask = DA9062AA_BUCK2_ILIM_MASK, 649 .desc.enable_reg = DA9062AA_BUCK2_CONT, 650 .desc.enable_mask = DA9062AA_BUCK2_EN_MASK, 651 .desc.vsel_reg = DA9062AA_VBUCK2_A, 652 .desc.vsel_mask = DA9062AA_VBUCK2_A_MASK, 653 .desc.linear_min_sel = 0, 654 .sleep = REG_FIELD(DA9062AA_VBUCK2_A, 655 __builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1, 656 sizeof(unsigned int) * 8 - 657 __builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1), 658 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B, 659 __builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1, 660 sizeof(unsigned int) * 8 - 661 __builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1), 662 .suspend_vsel_reg = DA9062AA_VBUCK2_B, 663 .mode = REG_FIELD(DA9062AA_BUCK2_CFG, 664 __builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1, 665 sizeof(unsigned int) * 8 - 666 __builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1), 667 .suspend = REG_FIELD(DA9062AA_BUCK2_CONT, 668 __builtin_ffs((int)DA9062AA_BUCK2_CONF_MASK) - 1, 669 sizeof(unsigned int) * 8 - 670 __builtin_clz(DA9062AA_BUCK2_CONF_MASK) - 1), 671 }, 672 { 673 .desc.id = DA9062_ID_BUCK3, 674 .desc.name = "DA9062 BUCK3", 675 .desc.of_match = of_match_ptr("buck3"), 676 .desc.regulators_node = of_match_ptr("regulators"), 677 .desc.ops = &da9062_buck_ops, 678 .desc.min_uV = (800) * 1000, 679 .desc.uV_step = (20) * 1000, 680 .desc.n_voltages = ((3340) - (800))/(20) + 1, 681 .desc.curr_table = da9062_buck_b_limits, 682 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits), 683 .desc.csel_reg = DA9062AA_BUCK_ILIM_A, 684 .desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK, 685 .desc.enable_reg = DA9062AA_BUCK3_CONT, 686 .desc.enable_mask = DA9062AA_BUCK3_EN_MASK, 687 .desc.vsel_reg = DA9062AA_VBUCK3_A, 688 .desc.vsel_mask = DA9062AA_VBUCK3_A_MASK, 689 .desc.linear_min_sel = 0, 690 .sleep = REG_FIELD(DA9062AA_VBUCK3_A, 691 __builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1, 692 sizeof(unsigned int) * 8 - 693 __builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1), 694 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B, 695 __builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1, 696 sizeof(unsigned int) * 8 - 697 __builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1), 698 .suspend_vsel_reg = DA9062AA_VBUCK3_B, 699 .mode = REG_FIELD(DA9062AA_BUCK3_CFG, 700 __builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1, 701 sizeof(unsigned int) * 8 - 702 __builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1), 703 .suspend = REG_FIELD(DA9062AA_BUCK3_CONT, 704 __builtin_ffs((int)DA9062AA_BUCK3_CONF_MASK) - 1, 705 sizeof(unsigned int) * 8 - 706 __builtin_clz(DA9062AA_BUCK3_CONF_MASK) - 1), 707 }, 708 { 709 .desc.id = DA9062_ID_BUCK4, 710 .desc.name = "DA9062 BUCK4", 711 .desc.of_match = of_match_ptr("buck4"), 712 .desc.regulators_node = of_match_ptr("regulators"), 713 .desc.ops = &da9062_buck_ops, 714 .desc.min_uV = (530) * 1000, 715 .desc.uV_step = (10) * 1000, 716 .desc.n_voltages = ((1800) - (530))/(10) + 1, 717 .desc.curr_table = da9062_buck_a_limits, 718 .desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits), 719 .desc.csel_reg = DA9062AA_BUCK_ILIM_B, 720 .desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK, 721 .desc.enable_reg = DA9062AA_BUCK4_CONT, 722 .desc.enable_mask = DA9062AA_BUCK4_EN_MASK, 723 .desc.vsel_reg = DA9062AA_VBUCK4_A, 724 .desc.vsel_mask = DA9062AA_VBUCK4_A_MASK, 725 .desc.linear_min_sel = 0, 726 .sleep = REG_FIELD(DA9062AA_VBUCK4_A, 727 __builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1, 728 sizeof(unsigned int) * 8 - 729 __builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1), 730 .suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B, 731 __builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1, 732 sizeof(unsigned int) * 8 - 733 __builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1), 734 .suspend_vsel_reg = DA9062AA_VBUCK4_B, 735 .mode = REG_FIELD(DA9062AA_BUCK4_CFG, 736 __builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1, 737 sizeof(unsigned int) * 8 - 738 __builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1), 739 .suspend = REG_FIELD(DA9062AA_BUCK4_CONT, 740 __builtin_ffs((int)DA9062AA_BUCK4_CONF_MASK) - 1, 741 sizeof(unsigned int) * 8 - 742 __builtin_clz(DA9062AA_BUCK4_CONF_MASK) - 1), 743 }, 744 { 745 .desc.id = DA9062_ID_LDO1, 746 .desc.name = "DA9062 LDO1", 747 .desc.of_match = of_match_ptr("ldo1"), 748 .desc.regulators_node = of_match_ptr("regulators"), 749 .desc.ops = &da9062_ldo_ops, 750 .desc.min_uV = (900) * 1000, 751 .desc.uV_step = (50) * 1000, 752 .desc.n_voltages = ((3600) - (900))/(50) + 1 753 + DA9062AA_VLDO_A_MIN_SEL, 754 .desc.enable_reg = DA9062AA_LDO1_CONT, 755 .desc.enable_mask = DA9062AA_LDO1_EN_MASK, 756 .desc.vsel_reg = DA9062AA_VLDO1_A, 757 .desc.vsel_mask = DA9062AA_VLDO1_A_MASK, 758 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 759 .sleep = REG_FIELD(DA9062AA_VLDO1_A, 760 __builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1, 761 sizeof(unsigned int) * 8 - 762 __builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1), 763 .suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B, 764 __builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1, 765 sizeof(unsigned int) * 8 - 766 __builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1), 767 .suspend_vsel_reg = DA9062AA_VLDO1_B, 768 .suspend = REG_FIELD(DA9062AA_LDO1_CONT, 769 __builtin_ffs((int)DA9062AA_LDO1_CONF_MASK) - 1, 770 sizeof(unsigned int) * 8 - 771 __builtin_clz(DA9062AA_LDO1_CONF_MASK) - 1), 772 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 773 __builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1, 774 sizeof(unsigned int) * 8 - 775 __builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1), 776 }, 777 { 778 .desc.id = DA9062_ID_LDO2, 779 .desc.name = "DA9062 LDO2", 780 .desc.of_match = of_match_ptr("ldo2"), 781 .desc.regulators_node = of_match_ptr("regulators"), 782 .desc.ops = &da9062_ldo_ops, 783 .desc.min_uV = (900) * 1000, 784 .desc.uV_step = (50) * 1000, 785 .desc.n_voltages = ((3600) - (900))/(50) + 1 786 + DA9062AA_VLDO_A_MIN_SEL, 787 .desc.enable_reg = DA9062AA_LDO2_CONT, 788 .desc.enable_mask = DA9062AA_LDO2_EN_MASK, 789 .desc.vsel_reg = DA9062AA_VLDO2_A, 790 .desc.vsel_mask = DA9062AA_VLDO2_A_MASK, 791 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 792 .sleep = REG_FIELD(DA9062AA_VLDO2_A, 793 __builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1, 794 sizeof(unsigned int) * 8 - 795 __builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1), 796 .suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B, 797 __builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1, 798 sizeof(unsigned int) * 8 - 799 __builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1), 800 .suspend_vsel_reg = DA9062AA_VLDO2_B, 801 .suspend = REG_FIELD(DA9062AA_LDO2_CONT, 802 __builtin_ffs((int)DA9062AA_LDO2_CONF_MASK) - 1, 803 sizeof(unsigned int) * 8 - 804 __builtin_clz(DA9062AA_LDO2_CONF_MASK) - 1), 805 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 806 __builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1, 807 sizeof(unsigned int) * 8 - 808 __builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1), 809 }, 810 { 811 .desc.id = DA9062_ID_LDO3, 812 .desc.name = "DA9062 LDO3", 813 .desc.of_match = of_match_ptr("ldo3"), 814 .desc.regulators_node = of_match_ptr("regulators"), 815 .desc.ops = &da9062_ldo_ops, 816 .desc.min_uV = (900) * 1000, 817 .desc.uV_step = (50) * 1000, 818 .desc.n_voltages = ((3600) - (900))/(50) + 1 819 + DA9062AA_VLDO_A_MIN_SEL, 820 .desc.enable_reg = DA9062AA_LDO3_CONT, 821 .desc.enable_mask = DA9062AA_LDO3_EN_MASK, 822 .desc.vsel_reg = DA9062AA_VLDO3_A, 823 .desc.vsel_mask = DA9062AA_VLDO3_A_MASK, 824 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 825 .sleep = REG_FIELD(DA9062AA_VLDO3_A, 826 __builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1, 827 sizeof(unsigned int) * 8 - 828 __builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1), 829 .suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B, 830 __builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1, 831 sizeof(unsigned int) * 8 - 832 __builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1), 833 .suspend_vsel_reg = DA9062AA_VLDO3_B, 834 .suspend = REG_FIELD(DA9062AA_LDO3_CONT, 835 __builtin_ffs((int)DA9062AA_LDO3_CONF_MASK) - 1, 836 sizeof(unsigned int) * 8 - 837 __builtin_clz(DA9062AA_LDO3_CONF_MASK) - 1), 838 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 839 __builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1, 840 sizeof(unsigned int) * 8 - 841 __builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1), 842 }, 843 { 844 .desc.id = DA9062_ID_LDO4, 845 .desc.name = "DA9062 LDO4", 846 .desc.of_match = of_match_ptr("ldo4"), 847 .desc.regulators_node = of_match_ptr("regulators"), 848 .desc.ops = &da9062_ldo_ops, 849 .desc.min_uV = (900) * 1000, 850 .desc.uV_step = (50) * 1000, 851 .desc.n_voltages = ((3600) - (900))/(50) + 1 852 + DA9062AA_VLDO_A_MIN_SEL, 853 .desc.enable_reg = DA9062AA_LDO4_CONT, 854 .desc.enable_mask = DA9062AA_LDO4_EN_MASK, 855 .desc.vsel_reg = DA9062AA_VLDO4_A, 856 .desc.vsel_mask = DA9062AA_VLDO4_A_MASK, 857 .desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL, 858 .sleep = REG_FIELD(DA9062AA_VLDO4_A, 859 __builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1, 860 sizeof(unsigned int) * 8 - 861 __builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1), 862 .suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B, 863 __builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1, 864 sizeof(unsigned int) * 8 - 865 __builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1), 866 .suspend_vsel_reg = DA9062AA_VLDO4_B, 867 .suspend = REG_FIELD(DA9062AA_LDO4_CONT, 868 __builtin_ffs((int)DA9062AA_LDO4_CONF_MASK) - 1, 869 sizeof(unsigned int) * 8 - 870 __builtin_clz(DA9062AA_LDO4_CONF_MASK) - 1), 871 .oc_event = REG_FIELD(DA9062AA_STATUS_D, 872 __builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1, 873 sizeof(unsigned int) * 8 - 874 __builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1), 875 }, 876 }; 877 878 /* Regulator interrupt handlers */ 879 static irqreturn_t da9062_ldo_lim_event(int irq, void *data) 880 { 881 struct da9062_regulators *regulators = data; 882 struct da9062 *hw = regulators->regulator[0].hw; 883 struct da9062_regulator *regl; 884 int handled = IRQ_NONE; 885 int bits, i, ret; 886 887 ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits); 888 if (ret < 0) { 889 dev_err(hw->dev, 890 "Failed to read LDO overcurrent indicator\n"); 891 goto ldo_lim_error; 892 } 893 894 for (i = regulators->n_regulators - 1; i >= 0; i--) { 895 regl = ®ulators->regulator[i]; 896 if (regl->info->oc_event.reg != DA9062AA_STATUS_D) 897 continue; 898 899 if (BIT(regl->info->oc_event.lsb) & bits) { 900 regulator_lock(regl->rdev); 901 regulator_notifier_call_chain(regl->rdev, 902 REGULATOR_EVENT_OVER_CURRENT, NULL); 903 regulator_unlock(regl->rdev); 904 handled = IRQ_HANDLED; 905 } 906 } 907 908 ldo_lim_error: 909 return handled; 910 } 911 912 static int da9062_regulator_probe(struct platform_device *pdev) 913 { 914 struct da9062 *chip = dev_get_drvdata(pdev->dev.parent); 915 struct da9062_regulators *regulators; 916 struct da9062_regulator *regl; 917 struct regulator_config config = { }; 918 const struct da9062_regulator_info *rinfo; 919 int irq, n, ret; 920 int max_regulators; 921 922 switch (chip->chip_type) { 923 case COMPAT_TYPE_DA9061: 924 max_regulators = DA9061_MAX_REGULATORS; 925 rinfo = local_da9061_regulator_info; 926 break; 927 case COMPAT_TYPE_DA9062: 928 max_regulators = DA9062_MAX_REGULATORS; 929 rinfo = local_da9062_regulator_info; 930 break; 931 default: 932 dev_err(chip->dev, "Unrecognised chip type\n"); 933 return -ENODEV; 934 } 935 936 /* Allocate memory required by usable regulators */ 937 regulators = devm_kzalloc(&pdev->dev, struct_size(regulators, regulator, 938 max_regulators), GFP_KERNEL); 939 if (!regulators) 940 return -ENOMEM; 941 942 regulators->n_regulators = max_regulators; 943 platform_set_drvdata(pdev, regulators); 944 945 n = 0; 946 while (n < regulators->n_regulators) { 947 /* Initialise regulator structure */ 948 regl = ®ulators->regulator[n]; 949 regl->hw = chip; 950 regl->info = &rinfo[n]; 951 regl->desc = regl->info->desc; 952 regl->desc.type = REGULATOR_VOLTAGE; 953 regl->desc.owner = THIS_MODULE; 954 955 if (regl->info->mode.reg) { 956 regl->mode = devm_regmap_field_alloc( 957 &pdev->dev, 958 chip->regmap, 959 regl->info->mode); 960 if (IS_ERR(regl->mode)) 961 return PTR_ERR(regl->mode); 962 } 963 964 if (regl->info->suspend.reg) { 965 regl->suspend = devm_regmap_field_alloc( 966 &pdev->dev, 967 chip->regmap, 968 regl->info->suspend); 969 if (IS_ERR(regl->suspend)) 970 return PTR_ERR(regl->suspend); 971 } 972 973 if (regl->info->sleep.reg) { 974 regl->sleep = devm_regmap_field_alloc( 975 &pdev->dev, 976 chip->regmap, 977 regl->info->sleep); 978 if (IS_ERR(regl->sleep)) 979 return PTR_ERR(regl->sleep); 980 } 981 982 if (regl->info->suspend_sleep.reg) { 983 regl->suspend_sleep = devm_regmap_field_alloc( 984 &pdev->dev, 985 chip->regmap, 986 regl->info->suspend_sleep); 987 if (IS_ERR(regl->suspend_sleep)) 988 return PTR_ERR(regl->suspend_sleep); 989 } 990 991 /* Register regulator */ 992 memset(&config, 0, sizeof(config)); 993 config.dev = chip->dev; 994 config.driver_data = regl; 995 config.regmap = chip->regmap; 996 997 regl->rdev = devm_regulator_register(&pdev->dev, ®l->desc, 998 &config); 999 if (IS_ERR(regl->rdev)) { 1000 dev_err(&pdev->dev, 1001 "Failed to register %s regulator\n", 1002 regl->desc.name); 1003 return PTR_ERR(regl->rdev); 1004 } 1005 1006 n++; 1007 } 1008 1009 /* LDOs overcurrent event support */ 1010 irq = platform_get_irq_byname(pdev, "LDO_LIM"); 1011 if (irq < 0) 1012 return irq; 1013 regulators->irq_ldo_lim = irq; 1014 1015 ret = devm_request_threaded_irq(&pdev->dev, irq, 1016 NULL, da9062_ldo_lim_event, 1017 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 1018 "LDO_LIM", regulators); 1019 if (ret) { 1020 dev_warn(&pdev->dev, 1021 "Failed to request LDO_LIM IRQ.\n"); 1022 regulators->irq_ldo_lim = -ENXIO; 1023 } 1024 1025 return 0; 1026 } 1027 1028 static struct platform_driver da9062_regulator_driver = { 1029 .driver = { 1030 .name = "da9062-regulators", 1031 }, 1032 .probe = da9062_regulator_probe, 1033 }; 1034 1035 static int __init da9062_regulator_init(void) 1036 { 1037 return platform_driver_register(&da9062_regulator_driver); 1038 } 1039 subsys_initcall(da9062_regulator_init); 1040 1041 static void __exit da9062_regulator_cleanup(void) 1042 { 1043 platform_driver_unregister(&da9062_regulator_driver); 1044 } 1045 module_exit(da9062_regulator_cleanup); 1046 1047 /* Module information */ 1048 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>"); 1049 MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062 and DA9061"); 1050 MODULE_LICENSE("GPL"); 1051 MODULE_ALIAS("platform:da9062-regulators"); 1052