1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2024 ROHM Semiconductors 3 // bd96801-regulator.c ROHM BD96801 regulator driver 4 5 /* 6 * This version of the "BD86801 scalable PMIC"'s driver supports only very 7 * basic set of the PMIC features. Most notably, there is no support for 8 * the configurations which should be done when the PMIC is in STBY mode. 9 * 10 * Being able to reliably do the configurations like changing the 11 * regulator safety limits (like limits for the over/under -voltages, over 12 * current, thermal protection) would require the configuring driver to be 13 * synchronized with entity causing the PMIC state transitions. Eg, one 14 * should be able to ensure the PMIC is in STBY state when the 15 * configurations are applied to the hardware. How and when the PMIC state 16 * transitions are to be done is likely to be very system specific, as will 17 * be the need to configure these safety limits. Hence it's not simple to 18 * come up with a generic solution. 19 * 20 * Users who require the STBY state configurations can have a look at the 21 * original RFC: 22 * https://lore.kernel.org/all/cover.1712920132.git.mazziesaccount@gmail.com/ 23 * which implements some of the safety limit configurations - but leaves the 24 * state change handling and synchronization to be implemented. 25 * 26 * It would be great to hear (and receive a patch!) if you implement the 27 * STBY configuration support in your downstream driver ;) 28 */ 29 30 #include <linux/cleanup.h> 31 #include <linux/delay.h> 32 #include <linux/err.h> 33 #include <linux/interrupt.h> 34 #include <linux/kernel.h> 35 #include <linux/linear_range.h> 36 #include <linux/mfd/rohm-generic.h> 37 #include <linux/mfd/rohm-bd96801.h> 38 #include <linux/module.h> 39 #include <linux/of.h> 40 #include <linux/platform_device.h> 41 #include <linux/regmap.h> 42 #include <linux/regulator/coupler.h> 43 #include <linux/regulator/driver.h> 44 #include <linux/regulator/machine.h> 45 #include <linux/regulator/of_regulator.h> 46 #include <linux/slab.h> 47 #include <linux/timer.h> 48 49 enum { 50 BD96801_BUCK1, 51 BD96801_BUCK2, 52 BD96801_BUCK3, 53 BD96801_BUCK4, 54 BD96801_LDO5, 55 BD96801_LDO6, 56 BD96801_LDO7, 57 BD96801_REGULATOR_AMOUNT, 58 }; 59 60 enum { 61 BD96801_PROT_OVP, 62 BD96801_PROT_UVP, 63 BD96801_PROT_OCP, 64 BD96801_PROT_TEMP, 65 BD96801_NUM_PROT, 66 }; 67 68 #define BD96801_ALWAYS_ON_REG 0x3c 69 #define BD96801_REG_ENABLE 0x0b 70 #define BD96801_BUCK1_EN_MASK BIT(0) 71 #define BD96801_BUCK2_EN_MASK BIT(1) 72 #define BD96801_BUCK3_EN_MASK BIT(2) 73 #define BD96801_BUCK4_EN_MASK BIT(3) 74 #define BD96801_LDO5_EN_MASK BIT(4) 75 #define BD96801_LDO6_EN_MASK BIT(5) 76 #define BD96801_LDO7_EN_MASK BIT(6) 77 78 #define BD96801_BUCK1_VSEL_REG 0x28 79 #define BD96801_BUCK2_VSEL_REG 0x29 80 #define BD96801_BUCK3_VSEL_REG 0x2a 81 #define BD96801_BUCK4_VSEL_REG 0x2b 82 #define BD96801_LDO5_VSEL_REG 0x25 83 #define BD96801_LDO6_VSEL_REG 0x26 84 #define BD96801_LDO7_VSEL_REG 0x27 85 #define BD96801_BUCK_VSEL_MASK 0x1F 86 #define BD96801_LDO_VSEL_MASK 0xff 87 88 #define BD96801_MASK_RAMP_DELAY 0xc0 89 #define BD96801_INT_VOUT_BASE_REG 0x21 90 #define BD96801_BUCK_INT_VOUT_MASK 0xff 91 92 #define BD96801_BUCK_VOLTS 256 93 #define BD96801_LDO_VOLTS 256 94 95 #define BD96801_OVP_MASK 0x03 96 #define BD96801_MASK_BUCK1_OVP_SHIFT 0x00 97 #define BD96801_MASK_BUCK2_OVP_SHIFT 0x02 98 #define BD96801_MASK_BUCK3_OVP_SHIFT 0x04 99 #define BD96801_MASK_BUCK4_OVP_SHIFT 0x06 100 #define BD96801_MASK_LDO5_OVP_SHIFT 0x00 101 #define BD96801_MASK_LDO6_OVP_SHIFT 0x02 102 #define BD96801_MASK_LDO7_OVP_SHIFT 0x04 103 104 #define BD96801_PROT_LIMIT_OCP_MIN 0x00 105 #define BD96801_PROT_LIMIT_LOW 0x01 106 #define BD96801_PROT_LIMIT_MID 0x02 107 #define BD96801_PROT_LIMIT_HI 0x03 108 109 #define BD96801_REG_BUCK1_OCP 0x32 110 #define BD96801_REG_BUCK2_OCP 0x32 111 #define BD96801_REG_BUCK3_OCP 0x33 112 #define BD96801_REG_BUCK4_OCP 0x33 113 114 #define BD96801_MASK_BUCK1_OCP_SHIFT 0x00 115 #define BD96801_MASK_BUCK2_OCP_SHIFT 0x04 116 #define BD96801_MASK_BUCK3_OCP_SHIFT 0x00 117 #define BD96801_MASK_BUCK4_OCP_SHIFT 0x04 118 119 #define BD96801_REG_LDO5_OCP 0x34 120 #define BD96801_REG_LDO6_OCP 0x34 121 #define BD96801_REG_LDO7_OCP 0x34 122 123 #define BD96801_MASK_LDO5_OCP_SHIFT 0x00 124 #define BD96801_MASK_LDO6_OCP_SHIFT 0x02 125 #define BD96801_MASK_LDO7_OCP_SHIFT 0x04 126 127 #define BD96801_MASK_SHD_INTB BIT(7) 128 #define BD96801_INTB_FATAL BIT(7) 129 130 #define BD96801_NUM_REGULATORS 7 131 #define BD96801_NUM_LDOS 4 132 133 /* 134 * Ramp rates for bucks are controlled by bits [7:6] as follows: 135 * 00 => 1 mV/uS 136 * 01 => 5 mV/uS 137 * 10 => 10 mV/uS 138 * 11 => 20 mV/uS 139 */ 140 static const unsigned int buck_ramp_table[] = { 1000, 5000, 10000, 20000 }; 141 142 /* 143 * This is a voltage range that get's appended to selected 144 * bd96801_buck_init_volts value. The range from 0x0 to 0xF is actually 145 * bd96801_buck_init_volts + 0 ... bd96801_buck_init_volts + 150mV 146 * and the range from 0x10 to 0x1f is bd96801_buck_init_volts - 150mV ... 147 * bd96801_buck_init_volts - 0. But as the members of linear_range 148 * are all unsigned I will apply offset of -150 mV to value in 149 * linear_range - which should increase these ranges with 150 * 150 mV getting all the values to >= 0. 151 */ 152 static const struct linear_range bd96801_tune_volts[] = { 153 REGULATOR_LINEAR_RANGE(150000, 0x00, 0xF, 10000), 154 REGULATOR_LINEAR_RANGE(0, 0x10, 0x1F, 10000), 155 }; 156 157 static const struct linear_range bd96801_buck_init_volts[] = { 158 REGULATOR_LINEAR_RANGE(500000 - 150000, 0x00, 0xc8, 5000), 159 REGULATOR_LINEAR_RANGE(1550000 - 150000, 0xc9, 0xec, 50000), 160 REGULATOR_LINEAR_RANGE(3300000 - 150000, 0xed, 0xff, 0), 161 }; 162 163 static const struct linear_range bd96801_ldo_int_volts[] = { 164 REGULATOR_LINEAR_RANGE(300000, 0x00, 0x78, 25000), 165 REGULATOR_LINEAR_RANGE(3300000, 0x79, 0xff, 0), 166 }; 167 168 #define BD96801_LDO_SD_VOLT_MASK 0x1 169 #define BD96801_LDO_MODE_MASK 0x6 170 #define BD96801_LDO_MODE_INT 0x0 171 #define BD96801_LDO_MODE_SD 0x2 172 #define BD96801_LDO_MODE_DDR 0x4 173 174 static int ldo_ddr_volt_table[] = {500000, 300000}; 175 static int ldo_sd_volt_table[] = {3300000, 1800000}; 176 177 /* Constant IRQ initialization data (templates) */ 178 struct bd96801_irqinfo { 179 int type; 180 struct regulator_irq_desc irq_desc; 181 int err_cfg; 182 int wrn_cfg; 183 const char *irq_name; 184 }; 185 186 #define BD96801_IRQINFO(_type, _name, _irqoff_ms, _irqname) \ 187 { \ 188 .type = (_type), \ 189 .err_cfg = -1, \ 190 .wrn_cfg = -1, \ 191 .irq_name = (_irqname), \ 192 .irq_desc = { \ 193 .name = (_name), \ 194 .irq_off_ms = (_irqoff_ms), \ 195 .map_event = regulator_irq_map_event_simple, \ 196 }, \ 197 } 198 199 static const struct bd96801_irqinfo buck1_irqinfo[] = { 200 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-h", 500, 201 "bd96801-buck1-overcurr-h"), 202 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-l", 500, 203 "bd96801-buck1-overcurr-l"), 204 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-n", 500, 205 "bd96801-buck1-overcurr-n"), 206 BD96801_IRQINFO(BD96801_PROT_OVP, "buck1-over-voltage", 500, 207 "bd96801-buck1-overvolt"), 208 BD96801_IRQINFO(BD96801_PROT_UVP, "buck1-under-voltage", 500, 209 "bd96801-buck1-undervolt"), 210 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck1-over-temp", 500, 211 "bd96801-buck1-thermal") 212 }; 213 214 static const struct bd96801_irqinfo buck2_irqinfo[] = { 215 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-h", 500, 216 "bd96801-buck2-overcurr-h"), 217 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-l", 500, 218 "bd96801-buck2-overcurr-l"), 219 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-n", 500, 220 "bd96801-buck2-overcurr-n"), 221 BD96801_IRQINFO(BD96801_PROT_OVP, "buck2-over-voltage", 500, 222 "bd96801-buck2-overvolt"), 223 BD96801_IRQINFO(BD96801_PROT_UVP, "buck2-under-voltage", 500, 224 "bd96801-buck2-undervolt"), 225 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck2-over-temp", 500, 226 "bd96801-buck2-thermal") 227 }; 228 229 static const struct bd96801_irqinfo buck3_irqinfo[] = { 230 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-h", 500, 231 "bd96801-buck3-overcurr-h"), 232 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-l", 500, 233 "bd96801-buck3-overcurr-l"), 234 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-n", 500, 235 "bd96801-buck3-overcurr-n"), 236 BD96801_IRQINFO(BD96801_PROT_OVP, "buck3-over-voltage", 500, 237 "bd96801-buck3-overvolt"), 238 BD96801_IRQINFO(BD96801_PROT_UVP, "buck3-under-voltage", 500, 239 "bd96801-buck3-undervolt"), 240 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck3-over-temp", 500, 241 "bd96801-buck3-thermal") 242 }; 243 244 static const struct bd96801_irqinfo buck4_irqinfo[] = { 245 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-h", 500, 246 "bd96801-buck4-overcurr-h"), 247 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-l", 500, 248 "bd96801-buck4-overcurr-l"), 249 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-n", 500, 250 "bd96801-buck4-overcurr-n"), 251 BD96801_IRQINFO(BD96801_PROT_OVP, "buck4-over-voltage", 500, 252 "bd96801-buck4-overvolt"), 253 BD96801_IRQINFO(BD96801_PROT_UVP, "buck4-under-voltage", 500, 254 "bd96801-buck4-undervolt"), 255 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck4-over-temp", 500, 256 "bd96801-buck4-thermal") 257 }; 258 259 static const struct bd96801_irqinfo ldo5_irqinfo[] = { 260 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo5-overcurr", 500, 261 "bd96801-ldo5-overcurr"), 262 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo5-over-voltage", 500, 263 "bd96801-ldo5-overvolt"), 264 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo5-under-voltage", 500, 265 "bd96801-ldo5-undervolt"), 266 }; 267 268 static const struct bd96801_irqinfo ldo6_irqinfo[] = { 269 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo6-overcurr", 500, 270 "bd96801-ldo6-overcurr"), 271 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo6-over-voltage", 500, 272 "bd96801-ldo6-overvolt"), 273 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo6-under-voltage", 500, 274 "bd96801-ldo6-undervolt"), 275 }; 276 277 static const struct bd96801_irqinfo ldo7_irqinfo[] = { 278 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo7-overcurr", 500, 279 "bd96801-ldo7-overcurr"), 280 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo7-over-voltage", 500, 281 "bd96801-ldo7-overvolt"), 282 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo7-under-voltage", 500, 283 "bd96801-ldo7-undervolt"), 284 }; 285 286 struct bd96801_irq_desc { 287 struct bd96801_irqinfo *irqinfo; 288 int num_irqs; 289 }; 290 291 struct bd96801_regulator_data { 292 struct regulator_desc desc; 293 const struct linear_range *init_ranges; 294 int num_ranges; 295 struct bd96801_irq_desc irq_desc; 296 int initial_voltage; 297 int ldo_vol_lvl; 298 int ldo_errs; 299 }; 300 301 struct bd96801_pmic_data { 302 struct bd96801_regulator_data regulator_data[BD96801_NUM_REGULATORS]; 303 struct regmap *regmap; 304 int fatal_ind; 305 }; 306 307 static int ldo_map_notif(int irq, struct regulator_irq_data *rid, 308 unsigned long *dev_mask) 309 { 310 int i; 311 312 for (i = 0; i < rid->num_states; i++) { 313 struct bd96801_regulator_data *rdata; 314 struct regulator_dev *rdev; 315 316 rdev = rid->states[i].rdev; 317 rdata = container_of(rdev->desc, struct bd96801_regulator_data, 318 desc); 319 rid->states[i].notifs = regulator_err2notif(rdata->ldo_errs); 320 rid->states[i].errors = rdata->ldo_errs; 321 *dev_mask |= BIT(i); 322 } 323 return 0; 324 } 325 326 static int bd96801_list_voltage_lr(struct regulator_dev *rdev, 327 unsigned int selector) 328 { 329 int voltage; 330 struct bd96801_regulator_data *data; 331 332 data = container_of(rdev->desc, struct bd96801_regulator_data, desc); 333 334 /* 335 * The BD096801 has voltage setting in two registers. One giving the 336 * "initial voltage" (can be changed only when regulator is disabled. 337 * This driver caches the value and sets it only at startup. The other 338 * register is voltage tuning value which applies -150 mV ... +150 mV 339 * offset to the voltage. 340 * 341 * Note that the cached initial voltage stored in regulator data is 342 * 'scaled down' by the 150 mV so that all of our tuning values are 343 * >= 0. This is done because the linear_ranges uses unsigned values. 344 * 345 * As a result, we increase the tuning voltage which we get based on 346 * the selector by the stored initial_voltage. 347 */ 348 voltage = regulator_list_voltage_linear_range(rdev, selector); 349 if (voltage < 0) 350 return voltage; 351 352 return voltage + data->initial_voltage; 353 } 354 355 356 static const struct regulator_ops bd96801_ldo_table_ops = { 357 .is_enabled = regulator_is_enabled_regmap, 358 .list_voltage = regulator_list_voltage_table, 359 .get_voltage_sel = regulator_get_voltage_sel_regmap, 360 }; 361 362 static const struct regulator_ops bd96801_buck_ops = { 363 .is_enabled = regulator_is_enabled_regmap, 364 .list_voltage = bd96801_list_voltage_lr, 365 .set_voltage_sel = regulator_set_voltage_sel_regmap, 366 .get_voltage_sel = regulator_get_voltage_sel_regmap, 367 .set_voltage_time_sel = regulator_set_voltage_time_sel, 368 .set_ramp_delay = regulator_set_ramp_delay_regmap, 369 }; 370 371 static const struct regulator_ops bd96801_ldo_ops = { 372 .is_enabled = regulator_is_enabled_regmap, 373 .list_voltage = regulator_list_voltage_linear_range, 374 .get_voltage_sel = regulator_get_voltage_sel_regmap, 375 }; 376 377 static int buck_get_initial_voltage(struct regmap *regmap, struct device *dev, 378 struct bd96801_regulator_data *data) 379 { 380 int ret = 0, sel, initial_uv; 381 int reg = BD96801_INT_VOUT_BASE_REG + data->desc.id; 382 383 if (data->num_ranges) { 384 ret = regmap_read(regmap, reg, &sel); 385 sel &= BD96801_BUCK_INT_VOUT_MASK; 386 387 ret = linear_range_get_value_array(data->init_ranges, 388 data->num_ranges, sel, 389 &initial_uv); 390 if (ret) 391 return ret; 392 393 data->initial_voltage = initial_uv; 394 dev_dbg(dev, "Tune-scaled initial voltage %u\n", 395 data->initial_voltage); 396 } 397 398 return 0; 399 } 400 401 static int get_ldo_initial_voltage(struct regmap *regmap, 402 struct device *dev, 403 struct bd96801_regulator_data *data) 404 { 405 int ret; 406 int cfgreg; 407 408 ret = regmap_read(regmap, data->ldo_vol_lvl, &cfgreg); 409 if (ret) 410 return ret; 411 412 switch (cfgreg & BD96801_LDO_MODE_MASK) { 413 case BD96801_LDO_MODE_DDR: 414 data->desc.volt_table = ldo_ddr_volt_table; 415 data->desc.n_voltages = ARRAY_SIZE(ldo_ddr_volt_table); 416 break; 417 case BD96801_LDO_MODE_SD: 418 data->desc.volt_table = ldo_sd_volt_table; 419 data->desc.n_voltages = ARRAY_SIZE(ldo_sd_volt_table); 420 break; 421 default: 422 dev_info(dev, "Leaving LDO to normal mode"); 423 return 0; 424 } 425 426 /* SD or DDR mode => override default ops */ 427 data->desc.ops = &bd96801_ldo_table_ops, 428 data->desc.vsel_mask = 1; 429 data->desc.vsel_reg = data->ldo_vol_lvl; 430 431 return 0; 432 } 433 434 static int get_initial_voltage(struct device *dev, struct regmap *regmap, 435 struct bd96801_regulator_data *data) 436 { 437 /* BUCK */ 438 if (data->desc.id <= BD96801_BUCK4) 439 return buck_get_initial_voltage(regmap, dev, data); 440 441 /* LDO */ 442 return get_ldo_initial_voltage(regmap, dev, data); 443 } 444 445 static int bd96801_walk_regulator_dt(struct device *dev, struct regmap *regmap, 446 struct bd96801_regulator_data *data, 447 int num) 448 { 449 int i, ret; 450 451 struct device_node *nproot __free(device_node) = 452 of_get_child_by_name(dev->parent->of_node, "regulators"); 453 if (!nproot) { 454 dev_err(dev, "failed to find regulators node\n"); 455 return -ENODEV; 456 } 457 for_each_child_of_node_scoped(nproot, np) { 458 for (i = 0; i < num; i++) { 459 if (!of_node_name_eq(np, data[i].desc.of_match)) 460 continue; 461 /* 462 * If STBY configs are supported, we must pass node 463 * here to extract the initial voltages from the DT. 464 * Thus we do the initial voltage getting in this 465 * loop. 466 */ 467 ret = get_initial_voltage(dev, regmap, &data[i]); 468 if (ret) { 469 dev_err(dev, 470 "Initializing voltages for %s failed\n", 471 data[i].desc.name); 472 return ret; 473 } 474 475 if (of_property_read_bool(np, "rohm,keep-on-stby")) { 476 ret = regmap_set_bits(regmap, 477 BD96801_ALWAYS_ON_REG, 478 1 << data[i].desc.id); 479 if (ret) { 480 dev_err(dev, 481 "failed to set %s on-at-stby\n", 482 data[i].desc.name); 483 return ret; 484 } 485 } 486 } 487 } 488 489 return 0; 490 } 491 492 /* 493 * Template for regulator data. Probe will allocate dynamic / driver instance 494 * struct so we should be on a safe side even if there were multiple PMICs to 495 * control. Note that there is a plan to allow multiple PMICs to be used so 496 * systems can scale better. I am however still slightly unsure how the 497 * multi-PMIC case will be handled. I don't know if the processor will have I2C 498 * acces to all of the PMICs or only the first one. I'd guess there will be 499 * access provided to all PMICs for voltage scaling - but the errors will only 500 * be informed via the master PMIC. Eg, we should prepare to support multiple 501 * driver instances - either with or without the IRQs... Well, let's first 502 * just support the simple and clear single-PMIC setup and ponder the multi PMIC 503 * case later. What we can easly do for preparing is to not use static global 504 * data for regulators though. 505 */ 506 static const struct bd96801_pmic_data bd96801_data = { 507 .regulator_data = { 508 { 509 .desc = { 510 .name = "buck1", 511 .of_match = of_match_ptr("buck1"), 512 .regulators_node = of_match_ptr("regulators"), 513 .id = BD96801_BUCK1, 514 .ops = &bd96801_buck_ops, 515 .type = REGULATOR_VOLTAGE, 516 .linear_ranges = bd96801_tune_volts, 517 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts), 518 .n_voltages = BD96801_BUCK_VOLTS, 519 .enable_reg = BD96801_REG_ENABLE, 520 .enable_mask = BD96801_BUCK1_EN_MASK, 521 .enable_is_inverted = true, 522 .vsel_reg = BD96801_BUCK1_VSEL_REG, 523 .vsel_mask = BD96801_BUCK_VSEL_MASK, 524 .ramp_reg = BD96801_BUCK1_VSEL_REG, 525 .ramp_mask = BD96801_MASK_RAMP_DELAY, 526 .ramp_delay_table = &buck_ramp_table[0], 527 .n_ramp_values = ARRAY_SIZE(buck_ramp_table), 528 .owner = THIS_MODULE, 529 }, 530 .init_ranges = bd96801_buck_init_volts, 531 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts), 532 .irq_desc = { 533 .irqinfo = (struct bd96801_irqinfo *)&buck1_irqinfo[0], 534 .num_irqs = ARRAY_SIZE(buck1_irqinfo), 535 }, 536 }, { 537 .desc = { 538 .name = "buck2", 539 .of_match = of_match_ptr("buck2"), 540 .regulators_node = of_match_ptr("regulators"), 541 .id = BD96801_BUCK2, 542 .ops = &bd96801_buck_ops, 543 .type = REGULATOR_VOLTAGE, 544 .linear_ranges = bd96801_tune_volts, 545 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts), 546 .n_voltages = BD96801_BUCK_VOLTS, 547 .enable_reg = BD96801_REG_ENABLE, 548 .enable_mask = BD96801_BUCK2_EN_MASK, 549 .enable_is_inverted = true, 550 .vsel_reg = BD96801_BUCK2_VSEL_REG, 551 .vsel_mask = BD96801_BUCK_VSEL_MASK, 552 .ramp_reg = BD96801_BUCK2_VSEL_REG, 553 .ramp_mask = BD96801_MASK_RAMP_DELAY, 554 .ramp_delay_table = &buck_ramp_table[0], 555 .n_ramp_values = ARRAY_SIZE(buck_ramp_table), 556 .owner = THIS_MODULE, 557 }, 558 .irq_desc = { 559 .irqinfo = (struct bd96801_irqinfo *)&buck2_irqinfo[0], 560 .num_irqs = ARRAY_SIZE(buck2_irqinfo), 561 }, 562 .init_ranges = bd96801_buck_init_volts, 563 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts), 564 }, { 565 .desc = { 566 .name = "buck3", 567 .of_match = of_match_ptr("buck3"), 568 .regulators_node = of_match_ptr("regulators"), 569 .id = BD96801_BUCK3, 570 .ops = &bd96801_buck_ops, 571 .type = REGULATOR_VOLTAGE, 572 .linear_ranges = bd96801_tune_volts, 573 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts), 574 .n_voltages = BD96801_BUCK_VOLTS, 575 .enable_reg = BD96801_REG_ENABLE, 576 .enable_mask = BD96801_BUCK3_EN_MASK, 577 .enable_is_inverted = true, 578 .vsel_reg = BD96801_BUCK3_VSEL_REG, 579 .vsel_mask = BD96801_BUCK_VSEL_MASK, 580 .ramp_reg = BD96801_BUCK3_VSEL_REG, 581 .ramp_mask = BD96801_MASK_RAMP_DELAY, 582 .ramp_delay_table = &buck_ramp_table[0], 583 .n_ramp_values = ARRAY_SIZE(buck_ramp_table), 584 .owner = THIS_MODULE, 585 }, 586 .irq_desc = { 587 .irqinfo = (struct bd96801_irqinfo *)&buck3_irqinfo[0], 588 .num_irqs = ARRAY_SIZE(buck3_irqinfo), 589 }, 590 .init_ranges = bd96801_buck_init_volts, 591 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts), 592 }, { 593 .desc = { 594 .name = "buck4", 595 .of_match = of_match_ptr("buck4"), 596 .regulators_node = of_match_ptr("regulators"), 597 .id = BD96801_BUCK4, 598 .ops = &bd96801_buck_ops, 599 .type = REGULATOR_VOLTAGE, 600 .linear_ranges = bd96801_tune_volts, 601 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts), 602 .n_voltages = BD96801_BUCK_VOLTS, 603 .enable_reg = BD96801_REG_ENABLE, 604 .enable_mask = BD96801_BUCK4_EN_MASK, 605 .enable_is_inverted = true, 606 .vsel_reg = BD96801_BUCK4_VSEL_REG, 607 .vsel_mask = BD96801_BUCK_VSEL_MASK, 608 .ramp_reg = BD96801_BUCK4_VSEL_REG, 609 .ramp_mask = BD96801_MASK_RAMP_DELAY, 610 .ramp_delay_table = &buck_ramp_table[0], 611 .n_ramp_values = ARRAY_SIZE(buck_ramp_table), 612 .owner = THIS_MODULE, 613 }, 614 .irq_desc = { 615 .irqinfo = (struct bd96801_irqinfo *)&buck4_irqinfo[0], 616 .num_irqs = ARRAY_SIZE(buck4_irqinfo), 617 }, 618 .init_ranges = bd96801_buck_init_volts, 619 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts), 620 }, { 621 .desc = { 622 .name = "ldo5", 623 .of_match = of_match_ptr("ldo5"), 624 .regulators_node = of_match_ptr("regulators"), 625 .id = BD96801_LDO5, 626 .ops = &bd96801_ldo_ops, 627 .type = REGULATOR_VOLTAGE, 628 .linear_ranges = bd96801_ldo_int_volts, 629 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts), 630 .n_voltages = BD96801_LDO_VOLTS, 631 .enable_reg = BD96801_REG_ENABLE, 632 .enable_mask = BD96801_LDO5_EN_MASK, 633 .enable_is_inverted = true, 634 .vsel_reg = BD96801_LDO5_VSEL_REG, 635 .vsel_mask = BD96801_LDO_VSEL_MASK, 636 .owner = THIS_MODULE, 637 }, 638 .irq_desc = { 639 .irqinfo = (struct bd96801_irqinfo *)&ldo5_irqinfo[0], 640 .num_irqs = ARRAY_SIZE(ldo5_irqinfo), 641 }, 642 .ldo_vol_lvl = BD96801_LDO5_VOL_LVL_REG, 643 }, { 644 .desc = { 645 .name = "ldo6", 646 .of_match = of_match_ptr("ldo6"), 647 .regulators_node = of_match_ptr("regulators"), 648 .id = BD96801_LDO6, 649 .ops = &bd96801_ldo_ops, 650 .type = REGULATOR_VOLTAGE, 651 .linear_ranges = bd96801_ldo_int_volts, 652 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts), 653 .n_voltages = BD96801_LDO_VOLTS, 654 .enable_reg = BD96801_REG_ENABLE, 655 .enable_mask = BD96801_LDO6_EN_MASK, 656 .enable_is_inverted = true, 657 .vsel_reg = BD96801_LDO6_VSEL_REG, 658 .vsel_mask = BD96801_LDO_VSEL_MASK, 659 .owner = THIS_MODULE, 660 }, 661 .irq_desc = { 662 .irqinfo = (struct bd96801_irqinfo *)&ldo6_irqinfo[0], 663 .num_irqs = ARRAY_SIZE(ldo6_irqinfo), 664 }, 665 .ldo_vol_lvl = BD96801_LDO6_VOL_LVL_REG, 666 }, { 667 .desc = { 668 .name = "ldo7", 669 .of_match = of_match_ptr("ldo7"), 670 .regulators_node = of_match_ptr("regulators"), 671 .id = BD96801_LDO7, 672 .ops = &bd96801_ldo_ops, 673 .type = REGULATOR_VOLTAGE, 674 .linear_ranges = bd96801_ldo_int_volts, 675 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts), 676 .n_voltages = BD96801_LDO_VOLTS, 677 .enable_reg = BD96801_REG_ENABLE, 678 .enable_mask = BD96801_LDO7_EN_MASK, 679 .enable_is_inverted = true, 680 .vsel_reg = BD96801_LDO7_VSEL_REG, 681 .vsel_mask = BD96801_LDO_VSEL_MASK, 682 .owner = THIS_MODULE, 683 }, 684 .irq_desc = { 685 .irqinfo = (struct bd96801_irqinfo *)&ldo7_irqinfo[0], 686 .num_irqs = ARRAY_SIZE(ldo7_irqinfo), 687 }, 688 .ldo_vol_lvl = BD96801_LDO7_VOL_LVL_REG, 689 }, 690 }, 691 }; 692 693 static int initialize_pmic_data(struct device *dev, 694 struct bd96801_pmic_data *pdata) 695 { 696 int r, i; 697 698 /* 699 * Allocate and initialize IRQ data for all of the regulators. We 700 * wish to modify IRQ information independently for each driver 701 * instance. 702 */ 703 for (r = 0; r < BD96801_NUM_REGULATORS; r++) { 704 const struct bd96801_irqinfo *template; 705 struct bd96801_irqinfo *new; 706 int num_infos; 707 708 template = pdata->regulator_data[r].irq_desc.irqinfo; 709 num_infos = pdata->regulator_data[r].irq_desc.num_irqs; 710 711 new = devm_kcalloc(dev, num_infos, sizeof(*new), GFP_KERNEL); 712 if (!new) 713 return -ENOMEM; 714 715 pdata->regulator_data[r].irq_desc.irqinfo = new; 716 717 for (i = 0; i < num_infos; i++) 718 new[i] = template[i]; 719 } 720 721 return 0; 722 } 723 724 static int bd96801_map_event_all(int irq, struct regulator_irq_data *rid, 725 unsigned long *dev_mask) 726 { 727 int i; 728 729 for (i = 0; i < rid->num_states; i++) { 730 rid->states[i].notifs = REGULATOR_EVENT_FAIL; 731 rid->states[i].errors = REGULATOR_ERROR_FAIL; 732 *dev_mask |= BIT(i); 733 } 734 735 return 0; 736 } 737 738 static int bd96801_rdev_errb_irqs(struct platform_device *pdev, 739 struct regulator_dev *rdev) 740 { 741 int i; 742 void *retp; 743 static const char * const single_out_errb_irqs[] = { 744 "bd96801-%s-pvin-err", "bd96801-%s-ovp-err", 745 "bd96801-%s-uvp-err", "bd96801-%s-shdn-err", 746 }; 747 748 for (i = 0; i < ARRAY_SIZE(single_out_errb_irqs); i++) { 749 struct regulator_irq_desc id = { 750 .map_event = bd96801_map_event_all, 751 .irq_off_ms = 1000, 752 }; 753 struct regulator_dev *rdev_arr[1]; 754 char tmp[255]; 755 int irq; 756 757 snprintf(tmp, 255, single_out_errb_irqs[i], rdev->desc->name); 758 tmp[254] = 0; 759 id.name = tmp; 760 761 irq = platform_get_irq_byname(pdev, tmp); 762 if (irq < 0) 763 continue; 764 765 rdev_arr[0] = rdev; 766 retp = devm_regulator_irq_helper(&pdev->dev, &id, irq, 0, 767 REGULATOR_ERROR_FAIL, NULL, 768 rdev_arr, 1); 769 if (IS_ERR(retp)) 770 return PTR_ERR(retp); 771 772 } 773 return 0; 774 } 775 776 static int bd96801_global_errb_irqs(struct platform_device *pdev, 777 struct regulator_dev **rdev, int num_rdev) 778 { 779 int i, num_irqs; 780 void *retp; 781 static const char * const global_errb_irqs[] = { 782 "bd96801-otp-err", "bd96801-dbist-err", "bd96801-eep-err", 783 "bd96801-abist-err", "bd96801-prstb-err", "bd96801-drmoserr1", 784 "bd96801-drmoserr2", "bd96801-slave-err", "bd96801-vref-err", 785 "bd96801-tsd", "bd96801-uvlo-err", "bd96801-ovlo-err", 786 "bd96801-osc-err", "bd96801-pon-err", "bd96801-poff-err", 787 "bd96801-cmd-shdn-err", "bd96801-int-shdn-err" 788 }; 789 790 num_irqs = ARRAY_SIZE(global_errb_irqs); 791 for (i = 0; i < num_irqs; i++) { 792 int irq; 793 struct regulator_irq_desc id = { 794 .name = global_errb_irqs[i], 795 .map_event = bd96801_map_event_all, 796 .irq_off_ms = 1000, 797 }; 798 799 irq = platform_get_irq_byname(pdev, global_errb_irqs[i]); 800 if (irq < 0) 801 continue; 802 803 retp = devm_regulator_irq_helper(&pdev->dev, &id, irq, 0, 804 REGULATOR_ERROR_FAIL, NULL, 805 rdev, num_rdev); 806 if (IS_ERR(retp)) 807 return PTR_ERR(retp); 808 } 809 810 return 0; 811 } 812 813 static int bd96801_rdev_intb_irqs(struct platform_device *pdev, 814 struct bd96801_pmic_data *pdata, 815 struct bd96801_irqinfo *iinfo, 816 struct regulator_dev *rdev) 817 { 818 struct regulator_dev *rdev_arr[1]; 819 void *retp; 820 int err = 0; 821 int irq; 822 int err_flags[] = { 823 [BD96801_PROT_OVP] = REGULATOR_ERROR_REGULATION_OUT, 824 [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE, 825 [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT, 826 [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP, 827 828 }; 829 int wrn_flags[] = { 830 [BD96801_PROT_OVP] = REGULATOR_ERROR_OVER_VOLTAGE_WARN, 831 [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE_WARN, 832 [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT_WARN, 833 [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP_WARN, 834 }; 835 836 /* 837 * Don't install IRQ handler if both error and warning 838 * notifications are explicitly disabled 839 */ 840 if (!iinfo->err_cfg && !iinfo->wrn_cfg) 841 return 0; 842 843 if (WARN_ON(iinfo->type >= BD96801_NUM_PROT)) 844 return -EINVAL; 845 846 if (iinfo->err_cfg) 847 err = err_flags[iinfo->type]; 848 else if (iinfo->wrn_cfg) 849 err = wrn_flags[iinfo->type]; 850 851 iinfo->irq_desc.data = pdata; 852 irq = platform_get_irq_byname(pdev, iinfo->irq_name); 853 if (irq < 0) 854 return irq; 855 /* Find notifications for this IRQ (WARN/ERR) */ 856 857 rdev_arr[0] = rdev; 858 retp = devm_regulator_irq_helper(&pdev->dev, 859 &iinfo->irq_desc, irq, 860 0, err, NULL, rdev_arr, 861 1); 862 if (IS_ERR(retp)) 863 return PTR_ERR(retp); 864 865 return 0; 866 } 867 868 static int bd96801_probe(struct platform_device *pdev) 869 { 870 struct regulator_dev *ldo_errs_rdev_arr[BD96801_NUM_LDOS]; 871 struct regulator_dev *all_rdevs[BD96801_NUM_REGULATORS]; 872 struct bd96801_regulator_data *rdesc; 873 struct regulator_config config = {}; 874 int ldo_errs_arr[BD96801_NUM_LDOS]; 875 struct bd96801_pmic_data *pdata; 876 int temp_notif_ldos = 0; 877 struct device *parent; 878 int i, ret; 879 bool use_errb; 880 void *retp; 881 882 parent = pdev->dev.parent; 883 884 pdata = devm_kmemdup(&pdev->dev, &bd96801_data, sizeof(bd96801_data), 885 GFP_KERNEL); 886 if (!pdata) 887 return -ENOMEM; 888 889 if (initialize_pmic_data(&pdev->dev, pdata)) 890 return -ENOMEM; 891 892 pdata->regmap = dev_get_regmap(parent, NULL); 893 if (!pdata->regmap) { 894 dev_err(&pdev->dev, "No register map found\n"); 895 return -ENODEV; 896 } 897 898 rdesc = &pdata->regulator_data[0]; 899 900 config.driver_data = pdata; 901 config.regmap = pdata->regmap; 902 config.dev = parent; 903 904 ret = of_property_match_string(pdev->dev.parent->of_node, 905 "interrupt-names", "errb"); 906 if (ret < 0) 907 use_errb = false; 908 else 909 use_errb = true; 910 911 ret = bd96801_walk_regulator_dt(&pdev->dev, pdata->regmap, rdesc, 912 BD96801_NUM_REGULATORS); 913 if (ret) 914 return ret; 915 916 for (i = 0; i < ARRAY_SIZE(pdata->regulator_data); i++) { 917 struct regulator_dev *rdev; 918 struct bd96801_irq_desc *idesc = &rdesc[i].irq_desc; 919 int j; 920 921 rdev = devm_regulator_register(&pdev->dev, 922 &rdesc[i].desc, &config); 923 if (IS_ERR(rdev)) { 924 dev_err(&pdev->dev, 925 "failed to register %s regulator\n", 926 rdesc[i].desc.name); 927 return PTR_ERR(rdev); 928 } 929 all_rdevs[i] = rdev; 930 /* 931 * LDOs don't have own temperature monitoring. If temperature 932 * notification was requested for this LDO from DT then we will 933 * add the regulator to be notified if central IC temperature 934 * exceeds threshold. 935 */ 936 if (rdesc[i].ldo_errs) { 937 ldo_errs_rdev_arr[temp_notif_ldos] = rdev; 938 ldo_errs_arr[temp_notif_ldos] = rdesc[i].ldo_errs; 939 temp_notif_ldos++; 940 } 941 942 /* Register INTB handlers for configured protections */ 943 for (j = 0; j < idesc->num_irqs; j++) { 944 ret = bd96801_rdev_intb_irqs(pdev, pdata, 945 &idesc->irqinfo[j], rdev); 946 if (ret) 947 return ret; 948 } 949 /* Register per regulator ERRB notifiers */ 950 if (use_errb) { 951 ret = bd96801_rdev_errb_irqs(pdev, rdev); 952 if (ret) 953 return ret; 954 } 955 } 956 if (temp_notif_ldos) { 957 int irq; 958 struct regulator_irq_desc tw_desc = { 959 .name = "bd96801-core-thermal", 960 .irq_off_ms = 500, 961 .map_event = ldo_map_notif, 962 }; 963 964 irq = platform_get_irq_byname(pdev, "bd96801-core-thermal"); 965 if (irq < 0) 966 return irq; 967 968 retp = devm_regulator_irq_helper(&pdev->dev, &tw_desc, irq, 0, 969 0, &ldo_errs_arr[0], 970 &ldo_errs_rdev_arr[0], 971 temp_notif_ldos); 972 if (IS_ERR(retp)) 973 return PTR_ERR(retp); 974 } 975 976 if (use_errb) 977 return bd96801_global_errb_irqs(pdev, all_rdevs, 978 ARRAY_SIZE(all_rdevs)); 979 980 return 0; 981 } 982 983 static const struct platform_device_id bd96801_pmic_id[] = { 984 { "bd96801-regulator", }, 985 { } 986 }; 987 MODULE_DEVICE_TABLE(platform, bd96801_pmic_id); 988 989 static struct platform_driver bd96801_regulator = { 990 .driver = { 991 .name = "bd96801-pmic" 992 }, 993 .probe = bd96801_probe, 994 .id_table = bd96801_pmic_id, 995 }; 996 997 module_platform_driver(bd96801_regulator); 998 999 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); 1000 MODULE_DESCRIPTION("BD96801 voltage regulator driver"); 1001 MODULE_LICENSE("GPL"); 1002