1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 ROHM Semiconductors
3 // bd71837-regulator.c ROHM BD71837MWV regulator driver
5 #include <linux/delay.h>
7 #include <linux/gpio.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/rohm-bd718x7.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/machine.h>
15 #include <linux/regulator/of_regulator.h>
16 #include <linux/slab.h>
19 struct regulator_desc descs[BD71837_REGULATOR_CNT];
21 struct platform_device *pdev;
22 struct regulator_dev *rdev[BD71837_REGULATOR_CNT];
27 * BUCK1RAMPRATE[1:0] BUCK1 DVS ramp rate setting
28 * 00: 10.00mV/usec 10mV 1uS
29 * 01: 5.00mV/usec 10mV 2uS
30 * 10: 2.50mV/usec 10mV 4uS
31 * 11: 1.25mV/usec 10mV 8uS
33 static int bd71837_buck1234_set_ramp_delay(struct regulator_dev *rdev,
36 struct bd71837_pmic *pmic = rdev_get_drvdata(rdev);
37 struct bd71837 *mfd = pmic->mfd;
38 int id = rdev->desc->id;
39 unsigned int ramp_value = BUCK_RAMPRATE_10P00MV;
41 dev_dbg(&pmic->pdev->dev, "Buck[%d] Set Ramp = %d\n", id + 1,
45 ramp_value = BUCK_RAMPRATE_1P25MV;
48 ramp_value = BUCK_RAMPRATE_2P50MV;
51 ramp_value = BUCK_RAMPRATE_5P00MV;
54 ramp_value = BUCK_RAMPRATE_10P00MV;
57 ramp_value = BUCK_RAMPRATE_10P00MV;
58 dev_err(&pmic->pdev->dev,
59 "%s: ramp_delay: %d not supported, setting 10000mV//us\n",
60 rdev->desc->name, ramp_delay);
63 return regmap_update_bits(mfd->regmap, BD71837_REG_BUCK1_CTRL + id,
64 BUCK_RAMPRATE_MASK, ramp_value << 6);
67 /* Bucks 1 to 4 support DVS. PWM mode is used when voltage is changed.
68 * Bucks 5 to 8 and LDOs can use PFM and must be disabled when voltage
69 * is changed. Hence we return -EBUSY for these if voltage is changed
70 * when BUCK/LDO is enabled.
72 static int bd71837_set_voltage_sel_restricted(struct regulator_dev *rdev,
75 if (regulator_is_enabled_regmap(rdev))
78 return regulator_set_voltage_sel_regmap(rdev, sel);
81 static struct regulator_ops bd71837_ldo_regulator_ops = {
82 .enable = regulator_enable_regmap,
83 .disable = regulator_disable_regmap,
84 .is_enabled = regulator_is_enabled_regmap,
85 .list_voltage = regulator_list_voltage_linear_range,
86 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
87 .get_voltage_sel = regulator_get_voltage_sel_regmap,
90 static struct regulator_ops bd71837_ldo_regulator_nolinear_ops = {
91 .enable = regulator_enable_regmap,
92 .disable = regulator_disable_regmap,
93 .is_enabled = regulator_is_enabled_regmap,
94 .list_voltage = regulator_list_voltage_table,
95 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
96 .get_voltage_sel = regulator_get_voltage_sel_regmap,
99 static struct regulator_ops bd71837_buck_regulator_ops = {
100 .enable = regulator_enable_regmap,
101 .disable = regulator_disable_regmap,
102 .is_enabled = regulator_is_enabled_regmap,
103 .list_voltage = regulator_list_voltage_linear_range,
104 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
105 .get_voltage_sel = regulator_get_voltage_sel_regmap,
106 .set_voltage_time_sel = regulator_set_voltage_time_sel,
109 static struct regulator_ops bd71837_buck_regulator_nolinear_ops = {
110 .enable = regulator_enable_regmap,
111 .disable = regulator_disable_regmap,
112 .is_enabled = regulator_is_enabled_regmap,
113 .list_voltage = regulator_list_voltage_table,
114 .set_voltage_sel = bd71837_set_voltage_sel_restricted,
115 .get_voltage_sel = regulator_get_voltage_sel_regmap,
116 .set_voltage_time_sel = regulator_set_voltage_time_sel,
119 static struct regulator_ops bd71837_buck1234_regulator_ops = {
120 .enable = regulator_enable_regmap,
121 .disable = regulator_disable_regmap,
122 .is_enabled = regulator_is_enabled_regmap,
123 .list_voltage = regulator_list_voltage_linear_range,
124 .set_voltage_sel = regulator_set_voltage_sel_regmap,
125 .get_voltage_sel = regulator_get_voltage_sel_regmap,
126 .set_voltage_time_sel = regulator_set_voltage_time_sel,
127 .set_ramp_delay = bd71837_buck1234_set_ramp_delay,
132 * 0.70 to 1.30V (10mV step)
134 static const struct regulator_linear_range bd71837_buck1234_voltage_ranges[] = {
135 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x3C, 10000),
136 REGULATOR_LINEAR_RANGE(1300000, 0x3D, 0x3F, 0),
143 static const struct regulator_linear_range bd71837_buck5_voltage_ranges[] = {
144 REGULATOR_LINEAR_RANGE(700000, 0x00, 0x03, 100000),
145 REGULATOR_LINEAR_RANGE(1050000, 0x04, 0x05, 50000),
146 REGULATOR_LINEAR_RANGE(1200000, 0x06, 0x07, 150000),
151 * 3.0V to 3.3V (step 100mV)
153 static const struct regulator_linear_range bd71837_buck6_voltage_ranges[] = {
154 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
162 * 011 = 1.8V (Initial)
168 static const unsigned int buck_7_volts[] = {
169 1605000, 1695000, 1755000, 1800000, 1845000, 1905000, 1950000, 1995000
174 * 0.8V to 1.40V (step 10mV)
176 static const struct regulator_linear_range bd71837_buck8_voltage_ranges[] = {
177 REGULATOR_LINEAR_RANGE(800000, 0x00, 0x3C, 10000),
178 REGULATOR_LINEAR_RANGE(1400000, 0x3D, 0x3F, 0),
183 * 3.0 to 3.3V (100mV step)
185 static const struct regulator_linear_range bd71837_ldo1_voltage_ranges[] = {
186 REGULATOR_LINEAR_RANGE(3000000, 0x00, 0x03, 100000),
193 static const unsigned int ldo_2_volts[] = {
199 * 1.8 to 3.3V (100mV step)
201 static const struct regulator_linear_range bd71837_ldo3_voltage_ranges[] = {
202 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
207 * 0.9 to 1.8V (100mV step)
209 static const struct regulator_linear_range bd71837_ldo4_voltage_ranges[] = {
210 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
211 REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
216 * 1.8 to 3.3V (100mV step)
218 static const struct regulator_linear_range bd71837_ldo5_voltage_ranges[] = {
219 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
224 * 0.9 to 1.8V (100mV step)
226 static const struct regulator_linear_range bd71837_ldo6_voltage_ranges[] = {
227 REGULATOR_LINEAR_RANGE(900000, 0x00, 0x09, 100000),
228 REGULATOR_LINEAR_RANGE(1800000, 0x0A, 0x0F, 0),
233 * 1.8 to 3.3V (100mV step)
235 static const struct regulator_linear_range bd71837_ldo7_voltage_ranges[] = {
236 REGULATOR_LINEAR_RANGE(1800000, 0x00, 0x0F, 100000),
239 static const struct regulator_desc bd71837_regulators[] = {
242 .of_match = of_match_ptr("BUCK1"),
243 .regulators_node = of_match_ptr("regulators"),
245 .ops = &bd71837_buck1234_regulator_ops,
246 .type = REGULATOR_VOLTAGE,
247 .n_voltages = BD71837_BUCK1_VOLTAGE_NUM,
248 .linear_ranges = bd71837_buck1234_voltage_ranges,
249 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
250 .vsel_reg = BD71837_REG_BUCK1_VOLT_RUN,
251 .vsel_mask = BUCK1_RUN_MASK,
252 .enable_reg = BD71837_REG_BUCK1_CTRL,
253 .enable_mask = BD71837_BUCK_EN,
254 .owner = THIS_MODULE,
258 .of_match = of_match_ptr("BUCK2"),
259 .regulators_node = of_match_ptr("regulators"),
261 .ops = &bd71837_buck1234_regulator_ops,
262 .type = REGULATOR_VOLTAGE,
263 .n_voltages = BD71837_BUCK2_VOLTAGE_NUM,
264 .linear_ranges = bd71837_buck1234_voltage_ranges,
265 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
266 .vsel_reg = BD71837_REG_BUCK2_VOLT_RUN,
267 .vsel_mask = BUCK2_RUN_MASK,
268 .enable_reg = BD71837_REG_BUCK2_CTRL,
269 .enable_mask = BD71837_BUCK_EN,
270 .owner = THIS_MODULE,
274 .of_match = of_match_ptr("BUCK3"),
275 .regulators_node = of_match_ptr("regulators"),
277 .ops = &bd71837_buck1234_regulator_ops,
278 .type = REGULATOR_VOLTAGE,
279 .n_voltages = BD71837_BUCK3_VOLTAGE_NUM,
280 .linear_ranges = bd71837_buck1234_voltage_ranges,
281 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
282 .vsel_reg = BD71837_REG_BUCK3_VOLT_RUN,
283 .vsel_mask = BUCK3_RUN_MASK,
284 .enable_reg = BD71837_REG_BUCK3_CTRL,
285 .enable_mask = BD71837_BUCK_EN,
286 .owner = THIS_MODULE,
290 .of_match = of_match_ptr("BUCK4"),
291 .regulators_node = of_match_ptr("regulators"),
293 .ops = &bd71837_buck1234_regulator_ops,
294 .type = REGULATOR_VOLTAGE,
295 .n_voltages = BD71837_BUCK4_VOLTAGE_NUM,
296 .linear_ranges = bd71837_buck1234_voltage_ranges,
297 .n_linear_ranges = ARRAY_SIZE(bd71837_buck1234_voltage_ranges),
298 .vsel_reg = BD71837_REG_BUCK4_VOLT_RUN,
299 .vsel_mask = BUCK4_RUN_MASK,
300 .enable_reg = BD71837_REG_BUCK4_CTRL,
301 .enable_mask = BD71837_BUCK_EN,
302 .owner = THIS_MODULE,
306 .of_match = of_match_ptr("BUCK5"),
307 .regulators_node = of_match_ptr("regulators"),
309 .ops = &bd71837_buck_regulator_ops,
310 .type = REGULATOR_VOLTAGE,
311 .n_voltages = BD71837_BUCK5_VOLTAGE_NUM,
312 .linear_ranges = bd71837_buck5_voltage_ranges,
313 .n_linear_ranges = ARRAY_SIZE(bd71837_buck5_voltage_ranges),
314 .vsel_reg = BD71837_REG_BUCK5_VOLT,
315 .vsel_mask = BUCK5_MASK,
316 .enable_reg = BD71837_REG_BUCK5_CTRL,
317 .enable_mask = BD71837_BUCK_EN,
318 .owner = THIS_MODULE,
322 .of_match = of_match_ptr("BUCK6"),
323 .regulators_node = of_match_ptr("regulators"),
325 .ops = &bd71837_buck_regulator_ops,
326 .type = REGULATOR_VOLTAGE,
327 .n_voltages = BD71837_BUCK6_VOLTAGE_NUM,
328 .linear_ranges = bd71837_buck6_voltage_ranges,
329 .n_linear_ranges = ARRAY_SIZE(bd71837_buck6_voltage_ranges),
330 .vsel_reg = BD71837_REG_BUCK6_VOLT,
331 .vsel_mask = BUCK6_MASK,
332 .enable_reg = BD71837_REG_BUCK6_CTRL,
333 .enable_mask = BD71837_BUCK_EN,
334 .owner = THIS_MODULE,
338 .of_match = of_match_ptr("BUCK7"),
339 .regulators_node = of_match_ptr("regulators"),
341 .ops = &bd71837_buck_regulator_nolinear_ops,
342 .type = REGULATOR_VOLTAGE,
343 .volt_table = &buck_7_volts[0],
344 .n_voltages = ARRAY_SIZE(buck_7_volts),
345 .vsel_reg = BD71837_REG_BUCK7_VOLT,
346 .vsel_mask = BUCK7_MASK,
347 .enable_reg = BD71837_REG_BUCK7_CTRL,
348 .enable_mask = BD71837_BUCK_EN,
349 .owner = THIS_MODULE,
353 .of_match = of_match_ptr("BUCK8"),
354 .regulators_node = of_match_ptr("regulators"),
356 .ops = &bd71837_buck_regulator_ops,
357 .type = REGULATOR_VOLTAGE,
358 .n_voltages = BD71837_BUCK8_VOLTAGE_NUM,
359 .linear_ranges = bd71837_buck8_voltage_ranges,
360 .n_linear_ranges = ARRAY_SIZE(bd71837_buck8_voltage_ranges),
361 .vsel_reg = BD71837_REG_BUCK8_VOLT,
362 .vsel_mask = BUCK8_MASK,
363 .enable_reg = BD71837_REG_BUCK8_CTRL,
364 .enable_mask = BD71837_BUCK_EN,
365 .owner = THIS_MODULE,
369 .of_match = of_match_ptr("LDO1"),
370 .regulators_node = of_match_ptr("regulators"),
372 .ops = &bd71837_ldo_regulator_ops,
373 .type = REGULATOR_VOLTAGE,
374 .n_voltages = BD71837_LDO1_VOLTAGE_NUM,
375 .linear_ranges = bd71837_ldo1_voltage_ranges,
376 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo1_voltage_ranges),
377 .vsel_reg = BD71837_REG_LDO1_VOLT,
378 .vsel_mask = LDO1_MASK,
379 .enable_reg = BD71837_REG_LDO1_VOLT,
380 .enable_mask = BD71837_LDO_EN,
381 .owner = THIS_MODULE,
385 .of_match = of_match_ptr("LDO2"),
386 .regulators_node = of_match_ptr("regulators"),
388 .ops = &bd71837_ldo_regulator_nolinear_ops,
389 .type = REGULATOR_VOLTAGE,
390 .volt_table = &ldo_2_volts[0],
391 .vsel_reg = BD71837_REG_LDO2_VOLT,
392 .vsel_mask = LDO2_MASK,
393 .n_voltages = ARRAY_SIZE(ldo_2_volts),
394 .n_voltages = BD71837_LDO2_VOLTAGE_NUM,
395 .enable_reg = BD71837_REG_LDO2_VOLT,
396 .enable_mask = BD71837_LDO_EN,
397 .owner = THIS_MODULE,
401 .of_match = of_match_ptr("LDO3"),
402 .regulators_node = of_match_ptr("regulators"),
404 .ops = &bd71837_ldo_regulator_ops,
405 .type = REGULATOR_VOLTAGE,
406 .n_voltages = BD71837_LDO3_VOLTAGE_NUM,
407 .linear_ranges = bd71837_ldo3_voltage_ranges,
408 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo3_voltage_ranges),
409 .vsel_reg = BD71837_REG_LDO3_VOLT,
410 .vsel_mask = LDO3_MASK,
411 .enable_reg = BD71837_REG_LDO3_VOLT,
412 .enable_mask = BD71837_LDO_EN,
413 .owner = THIS_MODULE,
417 .of_match = of_match_ptr("LDO4"),
418 .regulators_node = of_match_ptr("regulators"),
420 .ops = &bd71837_ldo_regulator_ops,
421 .type = REGULATOR_VOLTAGE,
422 .n_voltages = BD71837_LDO4_VOLTAGE_NUM,
423 .linear_ranges = bd71837_ldo4_voltage_ranges,
424 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo4_voltage_ranges),
425 .vsel_reg = BD71837_REG_LDO4_VOLT,
426 .vsel_mask = LDO4_MASK,
427 .enable_reg = BD71837_REG_LDO4_VOLT,
428 .enable_mask = BD71837_LDO_EN,
429 .owner = THIS_MODULE,
433 .of_match = of_match_ptr("LDO5"),
434 .regulators_node = of_match_ptr("regulators"),
436 .ops = &bd71837_ldo_regulator_ops,
437 .type = REGULATOR_VOLTAGE,
438 .n_voltages = BD71837_LDO5_VOLTAGE_NUM,
439 .linear_ranges = bd71837_ldo5_voltage_ranges,
440 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo5_voltage_ranges),
441 /* LDO5 is supplied by buck6 */
442 .supply_name = "buck6",
443 .vsel_reg = BD71837_REG_LDO5_VOLT,
444 .vsel_mask = LDO5_MASK,
445 .enable_reg = BD71837_REG_LDO5_VOLT,
446 .enable_mask = BD71837_LDO_EN,
447 .owner = THIS_MODULE,
451 .of_match = of_match_ptr("LDO6"),
452 .regulators_node = of_match_ptr("regulators"),
454 .ops = &bd71837_ldo_regulator_ops,
455 .type = REGULATOR_VOLTAGE,
456 .n_voltages = BD71837_LDO6_VOLTAGE_NUM,
457 .linear_ranges = bd71837_ldo6_voltage_ranges,
458 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo6_voltage_ranges),
459 /* LDO6 is supplied by buck7 */
460 .supply_name = "buck7",
461 .vsel_reg = BD71837_REG_LDO6_VOLT,
462 .vsel_mask = LDO6_MASK,
463 .enable_reg = BD71837_REG_LDO6_VOLT,
464 .enable_mask = BD71837_LDO_EN,
465 .owner = THIS_MODULE,
469 .of_match = of_match_ptr("LDO7"),
470 .regulators_node = of_match_ptr("regulators"),
472 .ops = &bd71837_ldo_regulator_ops,
473 .type = REGULATOR_VOLTAGE,
474 .n_voltages = BD71837_LDO7_VOLTAGE_NUM,
475 .linear_ranges = bd71837_ldo7_voltage_ranges,
476 .n_linear_ranges = ARRAY_SIZE(bd71837_ldo7_voltage_ranges),
477 .vsel_reg = BD71837_REG_LDO7_VOLT,
478 .vsel_mask = LDO7_MASK,
479 .enable_reg = BD71837_REG_LDO7_VOLT,
480 .enable_mask = BD71837_LDO_EN,
481 .owner = THIS_MODULE,
490 static int bd71837_probe(struct platform_device *pdev)
492 struct bd71837_pmic *pmic;
493 struct regulator_config config = { 0 };
494 struct reg_init pmic_regulator_inits[] = {
496 .reg = BD71837_REG_BUCK1_CTRL,
497 .mask = BD71837_BUCK_SEL,
499 .reg = BD71837_REG_BUCK2_CTRL,
500 .mask = BD71837_BUCK_SEL,
502 .reg = BD71837_REG_BUCK3_CTRL,
503 .mask = BD71837_BUCK_SEL,
505 .reg = BD71837_REG_BUCK4_CTRL,
506 .mask = BD71837_BUCK_SEL,
508 .reg = BD71837_REG_BUCK5_CTRL,
509 .mask = BD71837_BUCK_SEL,
511 .reg = BD71837_REG_BUCK6_CTRL,
512 .mask = BD71837_BUCK_SEL,
514 .reg = BD71837_REG_BUCK7_CTRL,
515 .mask = BD71837_BUCK_SEL,
517 .reg = BD71837_REG_BUCK8_CTRL,
518 .mask = BD71837_BUCK_SEL,
520 .reg = BD71837_REG_LDO1_VOLT,
521 .mask = BD71837_LDO_SEL,
523 .reg = BD71837_REG_LDO2_VOLT,
524 .mask = BD71837_LDO_SEL,
526 .reg = BD71837_REG_LDO3_VOLT,
527 .mask = BD71837_LDO_SEL,
529 .reg = BD71837_REG_LDO4_VOLT,
530 .mask = BD71837_LDO_SEL,
532 .reg = BD71837_REG_LDO5_VOLT,
533 .mask = BD71837_LDO_SEL,
535 .reg = BD71837_REG_LDO6_VOLT,
536 .mask = BD71837_LDO_SEL,
538 .reg = BD71837_REG_LDO7_VOLT,
539 .mask = BD71837_LDO_SEL,
545 pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
549 memcpy(pmic->descs, bd71837_regulators, sizeof(pmic->descs));
552 pmic->mfd = dev_get_drvdata(pdev->dev.parent);
555 dev_err(&pdev->dev, "No MFD driver data\n");
559 platform_set_drvdata(pdev, pmic);
561 /* Register LOCK release */
562 err = regmap_update_bits(pmic->mfd->regmap, BD71837_REG_REGLOCK,
563 (REGLOCK_PWRSEQ | REGLOCK_VREG), 0);
565 dev_err(&pmic->pdev->dev, "Failed to unlock PMIC (%d)\n", err);
568 dev_dbg(&pmic->pdev->dev, "Unlocked lock register 0x%x\n",
569 BD71837_REG_REGLOCK);
573 * There is a HW quirk in BD71837. The shutdown sequence timings for
574 * bucks/LDOs which are controlled via register interface are changed.
575 * At PMIC poweroff the voltage for BUCK6/7 is cut immediately at the
576 * beginning of shut-down sequence. As bucks 6 and 7 are parent
577 * supplies for LDO5 and LDO6 - this causes LDO5/6 voltage
578 * monitoring to errorneously detect under voltage and force PMIC to
579 * emergency state instead of poweroff. In order to avoid this we
580 * disable voltage monitoring for LDO5 and LDO6
582 err = regmap_update_bits(pmic->mfd->regmap, BD718XX_REG_MVRFLTMASK2,
583 BD718XX_LDO5_VRMON80 | BD718XX_LDO6_VRMON80,
584 BD718XX_LDO5_VRMON80 | BD718XX_LDO6_VRMON80);
586 dev_err(&pmic->pdev->dev,
587 "Failed to disable voltage monitoring\n");
591 for (i = 0; i < ARRAY_SIZE(pmic_regulator_inits); i++) {
593 struct regulator_desc *desc;
594 struct regulator_dev *rdev;
596 desc = &pmic->descs[i];
598 config.dev = pdev->dev.parent;
599 config.driver_data = pmic;
600 config.regmap = pmic->mfd->regmap;
602 rdev = devm_regulator_register(&pdev->dev, desc, &config);
604 dev_err(pmic->mfd->dev,
605 "failed to register %s regulator\n",
610 /* Regulator register gets the regulator constraints and
611 * applies them (set_machine_constraints). This should have
612 * turned the control register(s) to correct values and we
613 * can now switch the control from PMIC state machine to the
616 err = regmap_update_bits(pmic->mfd->regmap,
617 pmic_regulator_inits[i].reg,
618 pmic_regulator_inits[i].mask,
621 dev_err(&pmic->pdev->dev,
622 "Failed to write BUCK/LDO SEL bit for (%s)\n",
627 pmic->rdev[i] = rdev;
634 static struct platform_driver bd71837_regulator = {
636 .name = "bd71837-pmic",
638 .probe = bd71837_probe,
641 module_platform_driver(bd71837_regulator);
643 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
644 MODULE_DESCRIPTION("BD71837 voltage regulator driver");
645 MODULE_LICENSE("GPL");