1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2024 ROHM Semiconductors
3 // bd96801-regulator.c ROHM BD96801 regulator driver
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 ERRB interrupt and the configurations which should be done when the
9 * PMIC is in STBY mode.
11 * Supporting the ERRB interrupt would require dropping the regmap-IRQ
12 * usage or working around (or accepting a presense of) a naming conflict
15 * Being able to reliably do the configurations like changing the
16 * regulator safety limits (like limits for the over/under -voltages, over
17 * current, thermal protection) would require the configuring driver to be
18 * synchronized with entity causing the PMIC state transitions. Eg, one
19 * should be able to ensure the PMIC is in STBY state when the
20 * configurations are applied to the hardware. How and when the PMIC state
21 * transitions are to be done is likely to be very system specific, as will
22 * be the need to configure these safety limits. Hence it's not simple to
23 * come up with a generic solution.
25 * Users who require the ERRB handling and STBY state configurations can
26 * have a look at the original RFC:
28 * which implements a workaround to debugFS naming conflict and some of
29 * the safety limit configurations - but leaves the state change handling
30 * and synchronization to be implemented.
32 * It would be great to hear (and receive a patch!) if you implement the
33 * STBY configuration support or a proper fix to the debugFS naming
34 * conflict in your downstream driver ;)
37 #include <linux/delay.h>
38 #include <linux/err.h>
39 #include <linux/interrupt.h>
40 #include <linux/kernel.h>
41 #include <linux/linear_range.h>
42 #include <linux/mfd/rohm-generic.h>
43 #include <linux/mfd/rohm-bd96801.h>
44 #include <linux/module.h>
46 #include <linux/platform_device.h>
47 #include <linux/regmap.h>
48 #include <linux/regulator/coupler.h>
49 #include <linux/regulator/driver.h>
50 #include <linux/regulator/machine.h>
51 #include <linux/regulator/of_regulator.h>
52 #include <linux/slab.h>
53 #include <linux/timer.h>
63 BD96801_REGULATOR_AMOUNT,
74 #define BD96801_ALWAYS_ON_REG 0x3c
75 #define BD96801_REG_ENABLE 0x0b
76 #define BD96801_BUCK1_EN_MASK BIT(0)
77 #define BD96801_BUCK2_EN_MASK BIT(1)
78 #define BD96801_BUCK3_EN_MASK BIT(2)
79 #define BD96801_BUCK4_EN_MASK BIT(3)
80 #define BD96801_LDO5_EN_MASK BIT(4)
81 #define BD96801_LDO6_EN_MASK BIT(5)
82 #define BD96801_LDO7_EN_MASK BIT(6)
84 #define BD96801_BUCK1_VSEL_REG 0x28
85 #define BD96801_BUCK2_VSEL_REG 0x29
86 #define BD96801_BUCK3_VSEL_REG 0x2a
87 #define BD96801_BUCK4_VSEL_REG 0x2b
88 #define BD96801_LDO5_VSEL_REG 0x25
89 #define BD96801_LDO6_VSEL_REG 0x26
90 #define BD96801_LDO7_VSEL_REG 0x27
91 #define BD96801_BUCK_VSEL_MASK 0x1F
92 #define BD96801_LDO_VSEL_MASK 0xff
94 #define BD96801_MASK_RAMP_DELAY 0xc0
95 #define BD96801_INT_VOUT_BASE_REG 0x21
96 #define BD96801_BUCK_INT_VOUT_MASK 0xff
98 #define BD96801_BUCK_VOLTS 256
99 #define BD96801_LDO_VOLTS 256
101 #define BD96801_OVP_MASK 0x03
102 #define BD96801_MASK_BUCK1_OVP_SHIFT 0x00
103 #define BD96801_MASK_BUCK2_OVP_SHIFT 0x02
104 #define BD96801_MASK_BUCK3_OVP_SHIFT 0x04
105 #define BD96801_MASK_BUCK4_OVP_SHIFT 0x06
106 #define BD96801_MASK_LDO5_OVP_SHIFT 0x00
107 #define BD96801_MASK_LDO6_OVP_SHIFT 0x02
108 #define BD96801_MASK_LDO7_OVP_SHIFT 0x04
110 #define BD96801_PROT_LIMIT_OCP_MIN 0x00
111 #define BD96801_PROT_LIMIT_LOW 0x01
112 #define BD96801_PROT_LIMIT_MID 0x02
113 #define BD96801_PROT_LIMIT_HI 0x03
115 #define BD96801_REG_BUCK1_OCP 0x32
116 #define BD96801_REG_BUCK2_OCP 0x32
117 #define BD96801_REG_BUCK3_OCP 0x33
118 #define BD96801_REG_BUCK4_OCP 0x33
120 #define BD96801_MASK_BUCK1_OCP_SHIFT 0x00
121 #define BD96801_MASK_BUCK2_OCP_SHIFT 0x04
122 #define BD96801_MASK_BUCK3_OCP_SHIFT 0x00
123 #define BD96801_MASK_BUCK4_OCP_SHIFT 0x04
125 #define BD96801_REG_LDO5_OCP 0x34
126 #define BD96801_REG_LDO6_OCP 0x34
127 #define BD96801_REG_LDO7_OCP 0x34
129 #define BD96801_MASK_LDO5_OCP_SHIFT 0x00
130 #define BD96801_MASK_LDO6_OCP_SHIFT 0x02
131 #define BD96801_MASK_LDO7_OCP_SHIFT 0x04
133 #define BD96801_MASK_SHD_INTB BIT(7)
134 #define BD96801_INTB_FATAL BIT(7)
136 #define BD96801_NUM_REGULATORS 7
137 #define BD96801_NUM_LDOS 4
140 * Ramp rates for bucks are controlled by bits [7:6] as follows:
146 static const unsigned int buck_ramp_table[] = { 1000, 5000, 10000, 20000 };
149 * This is a voltage range that get's appended to selected
150 * bd96801_buck_init_volts value. The range from 0x0 to 0xF is actually
151 * bd96801_buck_init_volts + 0 ... bd96801_buck_init_volts + 150mV
152 * and the range from 0x10 to 0x1f is bd96801_buck_init_volts - 150mV ...
153 * bd96801_buck_init_volts - 0. But as the members of linear_range
154 * are all unsigned I will apply offset of -150 mV to value in
155 * linear_range - which should increase these ranges with
156 * 150 mV getting all the values to >= 0.
158 static const struct linear_range bd96801_tune_volts[] = {
159 REGULATOR_LINEAR_RANGE(150000, 0x00, 0xF, 10000),
160 REGULATOR_LINEAR_RANGE(0, 0x10, 0x1F, 10000),
163 static const struct linear_range bd96801_buck_init_volts[] = {
164 REGULATOR_LINEAR_RANGE(500000 - 150000, 0x00, 0xc8, 5000),
165 REGULATOR_LINEAR_RANGE(1550000 - 150000, 0xc9, 0xec, 50000),
166 REGULATOR_LINEAR_RANGE(3300000 - 150000, 0xed, 0xff, 0),
169 static const struct linear_range bd96801_ldo_int_volts[] = {
170 REGULATOR_LINEAR_RANGE(300000, 0x00, 0x78, 25000),
171 REGULATOR_LINEAR_RANGE(3300000, 0x79, 0xff, 0),
174 #define BD96801_LDO_SD_VOLT_MASK 0x1
175 #define BD96801_LDO_MODE_MASK 0x6
176 #define BD96801_LDO_MODE_INT 0x0
177 #define BD96801_LDO_MODE_SD 0x2
178 #define BD96801_LDO_MODE_DDR 0x4
180 static int ldo_ddr_volt_table[] = {500000, 300000};
181 static int ldo_sd_volt_table[] = {3300000, 1800000};
183 /* Constant IRQ initialization data (templates) */
184 struct bd96801_irqinfo {
186 struct regulator_irq_desc irq_desc;
189 const char *irq_name;
192 #define BD96801_IRQINFO(_type, _name, _irqoff_ms, _irqname) \
197 .irq_name = (_irqname), \
200 .irq_off_ms = (_irqoff_ms), \
201 .map_event = regulator_irq_map_event_simple, \
205 static const struct bd96801_irqinfo buck1_irqinfo[] = {
206 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-h", 500,
207 "bd96801-buck1-overcurr-h"),
208 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-l", 500,
209 "bd96801-buck1-overcurr-l"),
210 BD96801_IRQINFO(BD96801_PROT_OCP, "buck1-over-curr-n", 500,
211 "bd96801-buck1-overcurr-n"),
212 BD96801_IRQINFO(BD96801_PROT_OVP, "buck1-over-voltage", 500,
213 "bd96801-buck1-overvolt"),
214 BD96801_IRQINFO(BD96801_PROT_UVP, "buck1-under-voltage", 500,
215 "bd96801-buck1-undervolt"),
216 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck1-over-temp", 500,
217 "bd96801-buck1-thermal")
220 static const struct bd96801_irqinfo buck2_irqinfo[] = {
221 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-h", 500,
222 "bd96801-buck2-overcurr-h"),
223 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-l", 500,
224 "bd96801-buck2-overcurr-l"),
225 BD96801_IRQINFO(BD96801_PROT_OCP, "buck2-over-curr-n", 500,
226 "bd96801-buck2-overcurr-n"),
227 BD96801_IRQINFO(BD96801_PROT_OVP, "buck2-over-voltage", 500,
228 "bd96801-buck2-overvolt"),
229 BD96801_IRQINFO(BD96801_PROT_UVP, "buck2-under-voltage", 500,
230 "bd96801-buck2-undervolt"),
231 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck2-over-temp", 500,
232 "bd96801-buck2-thermal")
235 static const struct bd96801_irqinfo buck3_irqinfo[] = {
236 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-h", 500,
237 "bd96801-buck3-overcurr-h"),
238 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-l", 500,
239 "bd96801-buck3-overcurr-l"),
240 BD96801_IRQINFO(BD96801_PROT_OCP, "buck3-over-curr-n", 500,
241 "bd96801-buck3-overcurr-n"),
242 BD96801_IRQINFO(BD96801_PROT_OVP, "buck3-over-voltage", 500,
243 "bd96801-buck3-overvolt"),
244 BD96801_IRQINFO(BD96801_PROT_UVP, "buck3-under-voltage", 500,
245 "bd96801-buck3-undervolt"),
246 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck3-over-temp", 500,
247 "bd96801-buck3-thermal")
250 static const struct bd96801_irqinfo buck4_irqinfo[] = {
251 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-h", 500,
252 "bd96801-buck4-overcurr-h"),
253 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-l", 500,
254 "bd96801-buck4-overcurr-l"),
255 BD96801_IRQINFO(BD96801_PROT_OCP, "buck4-over-curr-n", 500,
256 "bd96801-buck4-overcurr-n"),
257 BD96801_IRQINFO(BD96801_PROT_OVP, "buck4-over-voltage", 500,
258 "bd96801-buck4-overvolt"),
259 BD96801_IRQINFO(BD96801_PROT_UVP, "buck4-under-voltage", 500,
260 "bd96801-buck4-undervolt"),
261 BD96801_IRQINFO(BD96801_PROT_TEMP, "buck4-over-temp", 500,
262 "bd96801-buck4-thermal")
265 static const struct bd96801_irqinfo ldo5_irqinfo[] = {
266 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo5-overcurr", 500,
267 "bd96801-ldo5-overcurr"),
268 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo5-over-voltage", 500,
269 "bd96801-ldo5-overvolt"),
270 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo5-under-voltage", 500,
271 "bd96801-ldo5-undervolt"),
274 static const struct bd96801_irqinfo ldo6_irqinfo[] = {
275 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo6-overcurr", 500,
276 "bd96801-ldo6-overcurr"),
277 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo6-over-voltage", 500,
278 "bd96801-ldo6-overvolt"),
279 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo6-under-voltage", 500,
280 "bd96801-ldo6-undervolt"),
283 static const struct bd96801_irqinfo ldo7_irqinfo[] = {
284 BD96801_IRQINFO(BD96801_PROT_OCP, "ldo7-overcurr", 500,
285 "bd96801-ldo7-overcurr"),
286 BD96801_IRQINFO(BD96801_PROT_OVP, "ldo7-over-voltage", 500,
287 "bd96801-ldo7-overvolt"),
288 BD96801_IRQINFO(BD96801_PROT_UVP, "ldo7-under-voltage", 500,
289 "bd96801-ldo7-undervolt"),
292 struct bd96801_irq_desc {
293 struct bd96801_irqinfo *irqinfo;
297 struct bd96801_regulator_data {
298 struct regulator_desc desc;
299 const struct linear_range *init_ranges;
301 struct bd96801_irq_desc irq_desc;
307 struct bd96801_pmic_data {
308 struct bd96801_regulator_data regulator_data[BD96801_NUM_REGULATORS];
309 struct regmap *regmap;
313 static int ldo_map_notif(int irq, struct regulator_irq_data *rid,
314 unsigned long *dev_mask)
318 for (i = 0; i < rid->num_states; i++) {
319 struct bd96801_regulator_data *rdata;
320 struct regulator_dev *rdev;
322 rdev = rid->states[i].rdev;
323 rdata = container_of(rdev->desc, struct bd96801_regulator_data,
325 rid->states[i].notifs = regulator_err2notif(rdata->ldo_errs);
326 rid->states[i].errors = rdata->ldo_errs;
332 static int bd96801_list_voltage_lr(struct regulator_dev *rdev,
333 unsigned int selector)
336 struct bd96801_regulator_data *data;
338 data = container_of(rdev->desc, struct bd96801_regulator_data, desc);
341 * The BD096801 has voltage setting in two registers. One giving the
342 * "initial voltage" (can be changed only when regulator is disabled.
343 * This driver caches the value and sets it only at startup. The other
344 * register is voltage tuning value which applies -150 mV ... +150 mV
345 * offset to the voltage.
347 * Note that the cached initial voltage stored in regulator data is
348 * 'scaled down' by the 150 mV so that all of our tuning values are
349 * >= 0. This is done because the linear_ranges uses unsigned values.
351 * As a result, we increase the tuning voltage which we get based on
352 * the selector by the stored initial_voltage.
354 voltage = regulator_list_voltage_linear_range(rdev, selector);
358 return voltage + data->initial_voltage;
362 static const struct regulator_ops bd96801_ldo_table_ops = {
363 .is_enabled = regulator_is_enabled_regmap,
364 .list_voltage = regulator_list_voltage_table,
365 .get_voltage_sel = regulator_get_voltage_sel_regmap,
368 static const struct regulator_ops bd96801_buck_ops = {
369 .is_enabled = regulator_is_enabled_regmap,
370 .list_voltage = bd96801_list_voltage_lr,
371 .set_voltage_sel = regulator_set_voltage_sel_regmap,
372 .get_voltage_sel = regulator_get_voltage_sel_regmap,
373 .set_voltage_time_sel = regulator_set_voltage_time_sel,
374 .set_ramp_delay = regulator_set_ramp_delay_regmap,
377 static const struct regulator_ops bd96801_ldo_ops = {
378 .is_enabled = regulator_is_enabled_regmap,
379 .list_voltage = regulator_list_voltage_linear_range,
380 .get_voltage_sel = regulator_get_voltage_sel_regmap,
383 static int buck_get_initial_voltage(struct regmap *regmap, struct device *dev,
384 struct bd96801_regulator_data *data)
386 int ret = 0, sel, initial_uv;
387 int reg = BD96801_INT_VOUT_BASE_REG + data->desc.id;
389 if (data->num_ranges) {
390 ret = regmap_read(regmap, reg, &sel);
391 sel &= BD96801_BUCK_INT_VOUT_MASK;
393 ret = linear_range_get_value_array(data->init_ranges,
394 data->num_ranges, sel,
399 data->initial_voltage = initial_uv;
400 dev_dbg(dev, "Tune-scaled initial voltage %u\n",
401 data->initial_voltage);
407 static int get_ldo_initial_voltage(struct regmap *regmap,
409 struct bd96801_regulator_data *data)
414 ret = regmap_read(regmap, data->ldo_vol_lvl, &cfgreg);
418 switch (cfgreg & BD96801_LDO_MODE_MASK) {
419 case BD96801_LDO_MODE_DDR:
420 data->desc.volt_table = ldo_ddr_volt_table;
421 data->desc.n_voltages = ARRAY_SIZE(ldo_ddr_volt_table);
423 case BD96801_LDO_MODE_SD:
424 data->desc.volt_table = ldo_sd_volt_table;
425 data->desc.n_voltages = ARRAY_SIZE(ldo_sd_volt_table);
428 dev_info(dev, "Leaving LDO to normal mode");
432 /* SD or DDR mode => override default ops */
433 data->desc.ops = &bd96801_ldo_table_ops,
434 data->desc.vsel_mask = 1;
435 data->desc.vsel_reg = data->ldo_vol_lvl;
440 static int get_initial_voltage(struct device *dev, struct regmap *regmap,
441 struct bd96801_regulator_data *data)
444 if (data->desc.id <= BD96801_BUCK4)
445 return buck_get_initial_voltage(regmap, dev, data);
448 return get_ldo_initial_voltage(regmap, dev, data);
451 static int bd96801_walk_regulator_dt(struct device *dev, struct regmap *regmap,
452 struct bd96801_regulator_data *data,
456 struct device_node *np;
457 struct device_node *nproot = dev->parent->of_node;
459 nproot = of_get_child_by_name(nproot, "regulators");
461 dev_err(dev, "failed to find regulators node\n");
464 for_each_child_of_node(nproot, np)
465 for (i = 0; i < num; i++) {
466 if (!of_node_name_eq(np, data[i].desc.of_match))
469 * If STBY configs are supported, we must pass node
470 * here to extract the initial voltages from the DT.
471 * Thus we do the initial voltage getting in this
474 ret = get_initial_voltage(dev, regmap, &data[i]);
477 "Initializing voltages for %s failed\n",
484 if (of_property_read_bool(np, "rohm,keep-on-stby")) {
485 ret = regmap_set_bits(regmap,
486 BD96801_ALWAYS_ON_REG,
487 1 << data[i].desc.id);
490 "failed to set %s on-at-stby\n",
505 * Template for regulator data. Probe will allocate dynamic / driver instance
506 * struct so we should be on a safe side even if there were multiple PMICs to
507 * control. Note that there is a plan to allow multiple PMICs to be used so
508 * systems can scale better. I am however still slightly unsure how the
509 * multi-PMIC case will be handled. I don't know if the processor will have I2C
510 * acces to all of the PMICs or only the first one. I'd guess there will be
511 * access provided to all PMICs for voltage scaling - but the errors will only
512 * be informed via the master PMIC. Eg, we should prepare to support multiple
513 * driver instances - either with or without the IRQs... Well, let's first
514 * just support the simple and clear single-PMIC setup and ponder the multi PMIC
515 * case later. What we can easly do for preparing is to not use static global
516 * data for regulators though.
518 static const struct bd96801_pmic_data bd96801_data = {
523 .of_match = of_match_ptr("buck1"),
524 .regulators_node = of_match_ptr("regulators"),
526 .ops = &bd96801_buck_ops,
527 .type = REGULATOR_VOLTAGE,
528 .linear_ranges = bd96801_tune_volts,
529 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
530 .n_voltages = BD96801_BUCK_VOLTS,
531 .enable_reg = BD96801_REG_ENABLE,
532 .enable_mask = BD96801_BUCK1_EN_MASK,
533 .enable_is_inverted = true,
534 .vsel_reg = BD96801_BUCK1_VSEL_REG,
535 .vsel_mask = BD96801_BUCK_VSEL_MASK,
536 .ramp_reg = BD96801_BUCK1_VSEL_REG,
537 .ramp_mask = BD96801_MASK_RAMP_DELAY,
538 .ramp_delay_table = &buck_ramp_table[0],
539 .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
540 .owner = THIS_MODULE,
542 .init_ranges = bd96801_buck_init_volts,
543 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
545 .irqinfo = (struct bd96801_irqinfo *)&buck1_irqinfo[0],
546 .num_irqs = ARRAY_SIZE(buck1_irqinfo),
551 .of_match = of_match_ptr("buck2"),
552 .regulators_node = of_match_ptr("regulators"),
554 .ops = &bd96801_buck_ops,
555 .type = REGULATOR_VOLTAGE,
556 .linear_ranges = bd96801_tune_volts,
557 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
558 .n_voltages = BD96801_BUCK_VOLTS,
559 .enable_reg = BD96801_REG_ENABLE,
560 .enable_mask = BD96801_BUCK2_EN_MASK,
561 .enable_is_inverted = true,
562 .vsel_reg = BD96801_BUCK2_VSEL_REG,
563 .vsel_mask = BD96801_BUCK_VSEL_MASK,
564 .ramp_reg = BD96801_BUCK2_VSEL_REG,
565 .ramp_mask = BD96801_MASK_RAMP_DELAY,
566 .ramp_delay_table = &buck_ramp_table[0],
567 .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
568 .owner = THIS_MODULE,
571 .irqinfo = (struct bd96801_irqinfo *)&buck2_irqinfo[0],
572 .num_irqs = ARRAY_SIZE(buck2_irqinfo),
574 .init_ranges = bd96801_buck_init_volts,
575 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
579 .of_match = of_match_ptr("buck3"),
580 .regulators_node = of_match_ptr("regulators"),
582 .ops = &bd96801_buck_ops,
583 .type = REGULATOR_VOLTAGE,
584 .linear_ranges = bd96801_tune_volts,
585 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
586 .n_voltages = BD96801_BUCK_VOLTS,
587 .enable_reg = BD96801_REG_ENABLE,
588 .enable_mask = BD96801_BUCK3_EN_MASK,
589 .enable_is_inverted = true,
590 .vsel_reg = BD96801_BUCK3_VSEL_REG,
591 .vsel_mask = BD96801_BUCK_VSEL_MASK,
592 .ramp_reg = BD96801_BUCK3_VSEL_REG,
593 .ramp_mask = BD96801_MASK_RAMP_DELAY,
594 .ramp_delay_table = &buck_ramp_table[0],
595 .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
596 .owner = THIS_MODULE,
599 .irqinfo = (struct bd96801_irqinfo *)&buck3_irqinfo[0],
600 .num_irqs = ARRAY_SIZE(buck3_irqinfo),
602 .init_ranges = bd96801_buck_init_volts,
603 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
607 .of_match = of_match_ptr("buck4"),
608 .regulators_node = of_match_ptr("regulators"),
610 .ops = &bd96801_buck_ops,
611 .type = REGULATOR_VOLTAGE,
612 .linear_ranges = bd96801_tune_volts,
613 .n_linear_ranges = ARRAY_SIZE(bd96801_tune_volts),
614 .n_voltages = BD96801_BUCK_VOLTS,
615 .enable_reg = BD96801_REG_ENABLE,
616 .enable_mask = BD96801_BUCK4_EN_MASK,
617 .enable_is_inverted = true,
618 .vsel_reg = BD96801_BUCK4_VSEL_REG,
619 .vsel_mask = BD96801_BUCK_VSEL_MASK,
620 .ramp_reg = BD96801_BUCK4_VSEL_REG,
621 .ramp_mask = BD96801_MASK_RAMP_DELAY,
622 .ramp_delay_table = &buck_ramp_table[0],
623 .n_ramp_values = ARRAY_SIZE(buck_ramp_table),
624 .owner = THIS_MODULE,
627 .irqinfo = (struct bd96801_irqinfo *)&buck4_irqinfo[0],
628 .num_irqs = ARRAY_SIZE(buck4_irqinfo),
630 .init_ranges = bd96801_buck_init_volts,
631 .num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
635 .of_match = of_match_ptr("ldo5"),
636 .regulators_node = of_match_ptr("regulators"),
638 .ops = &bd96801_ldo_ops,
639 .type = REGULATOR_VOLTAGE,
640 .linear_ranges = bd96801_ldo_int_volts,
641 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
642 .n_voltages = BD96801_LDO_VOLTS,
643 .enable_reg = BD96801_REG_ENABLE,
644 .enable_mask = BD96801_LDO5_EN_MASK,
645 .enable_is_inverted = true,
646 .vsel_reg = BD96801_LDO5_VSEL_REG,
647 .vsel_mask = BD96801_LDO_VSEL_MASK,
648 .owner = THIS_MODULE,
651 .irqinfo = (struct bd96801_irqinfo *)&ldo5_irqinfo[0],
652 .num_irqs = ARRAY_SIZE(ldo5_irqinfo),
654 .ldo_vol_lvl = BD96801_LDO5_VOL_LVL_REG,
658 .of_match = of_match_ptr("ldo6"),
659 .regulators_node = of_match_ptr("regulators"),
661 .ops = &bd96801_ldo_ops,
662 .type = REGULATOR_VOLTAGE,
663 .linear_ranges = bd96801_ldo_int_volts,
664 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
665 .n_voltages = BD96801_LDO_VOLTS,
666 .enable_reg = BD96801_REG_ENABLE,
667 .enable_mask = BD96801_LDO6_EN_MASK,
668 .enable_is_inverted = true,
669 .vsel_reg = BD96801_LDO6_VSEL_REG,
670 .vsel_mask = BD96801_LDO_VSEL_MASK,
671 .owner = THIS_MODULE,
674 .irqinfo = (struct bd96801_irqinfo *)&ldo6_irqinfo[0],
675 .num_irqs = ARRAY_SIZE(ldo6_irqinfo),
677 .ldo_vol_lvl = BD96801_LDO6_VOL_LVL_REG,
681 .of_match = of_match_ptr("ldo7"),
682 .regulators_node = of_match_ptr("regulators"),
684 .ops = &bd96801_ldo_ops,
685 .type = REGULATOR_VOLTAGE,
686 .linear_ranges = bd96801_ldo_int_volts,
687 .n_linear_ranges = ARRAY_SIZE(bd96801_ldo_int_volts),
688 .n_voltages = BD96801_LDO_VOLTS,
689 .enable_reg = BD96801_REG_ENABLE,
690 .enable_mask = BD96801_LDO7_EN_MASK,
691 .enable_is_inverted = true,
692 .vsel_reg = BD96801_LDO7_VSEL_REG,
693 .vsel_mask = BD96801_LDO_VSEL_MASK,
694 .owner = THIS_MODULE,
697 .irqinfo = (struct bd96801_irqinfo *)&ldo7_irqinfo[0],
698 .num_irqs = ARRAY_SIZE(ldo7_irqinfo),
700 .ldo_vol_lvl = BD96801_LDO7_VOL_LVL_REG,
705 static int initialize_pmic_data(struct device *dev,
706 struct bd96801_pmic_data *pdata)
711 * Allocate and initialize IRQ data for all of the regulators. We
712 * wish to modify IRQ information independently for each driver
715 for (r = 0; r < BD96801_NUM_REGULATORS; r++) {
716 const struct bd96801_irqinfo *template;
717 struct bd96801_irqinfo *new;
720 template = pdata->regulator_data[r].irq_desc.irqinfo;
721 num_infos = pdata->regulator_data[r].irq_desc.num_irqs;
723 new = devm_kcalloc(dev, num_infos, sizeof(*new), GFP_KERNEL);
727 pdata->regulator_data[r].irq_desc.irqinfo = new;
729 for (i = 0; i < num_infos; i++)
730 new[i] = template[i];
736 static int bd96801_rdev_intb_irqs(struct platform_device *pdev,
737 struct bd96801_pmic_data *pdata,
738 struct bd96801_irqinfo *iinfo,
739 struct regulator_dev *rdev)
741 struct regulator_dev *rdev_arr[1];
746 [BD96801_PROT_OVP] = REGULATOR_ERROR_REGULATION_OUT,
747 [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE,
748 [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT,
749 [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP,
753 [BD96801_PROT_OVP] = REGULATOR_ERROR_OVER_VOLTAGE_WARN,
754 [BD96801_PROT_UVP] = REGULATOR_ERROR_UNDER_VOLTAGE_WARN,
755 [BD96801_PROT_OCP] = REGULATOR_ERROR_OVER_CURRENT_WARN,
756 [BD96801_PROT_TEMP] = REGULATOR_ERROR_OVER_TEMP_WARN,
760 * Don't install IRQ handler if both error and warning
761 * notifications are explicitly disabled
763 if (!iinfo->err_cfg && !iinfo->wrn_cfg)
766 if (WARN_ON(iinfo->type >= BD96801_NUM_PROT))
770 err = err_flags[iinfo->type];
771 else if (iinfo->wrn_cfg)
772 err = wrn_flags[iinfo->type];
774 iinfo->irq_desc.data = pdata;
775 irq = platform_get_irq_byname(pdev, iinfo->irq_name);
778 /* Find notifications for this IRQ (WARN/ERR) */
781 retp = devm_regulator_irq_helper(&pdev->dev,
782 &iinfo->irq_desc, irq,
783 0, err, NULL, rdev_arr,
786 return PTR_ERR(retp);
793 static int bd96801_probe(struct platform_device *pdev)
795 struct regulator_dev *ldo_errs_rdev_arr[BD96801_NUM_LDOS];
796 struct bd96801_regulator_data *rdesc;
797 struct regulator_config config = {};
798 int ldo_errs_arr[BD96801_NUM_LDOS];
799 struct bd96801_pmic_data *pdata;
800 int temp_notif_ldos = 0;
801 struct device *parent;
805 parent = pdev->dev.parent;
807 pdata = devm_kmemdup(&pdev->dev, &bd96801_data, sizeof(bd96801_data),
812 if (initialize_pmic_data(&pdev->dev, pdata))
815 pdata->regmap = dev_get_regmap(parent, NULL);
816 if (!pdata->regmap) {
817 dev_err(&pdev->dev, "No register map found\n");
821 rdesc = &pdata->regulator_data[0];
823 config.driver_data = pdata;
824 config.regmap = pdata->regmap;
827 ret = bd96801_walk_regulator_dt(&pdev->dev, pdata->regmap, rdesc,
828 BD96801_NUM_REGULATORS);
832 for (i = 0; i < ARRAY_SIZE(pdata->regulator_data); i++) {
833 struct regulator_dev *rdev;
834 struct bd96801_irq_desc *idesc = &rdesc[i].irq_desc;
837 rdev = devm_regulator_register(&pdev->dev,
838 &rdesc[i].desc, &config);
841 "failed to register %s regulator\n",
843 return PTR_ERR(rdev);
846 * LDOs don't have own temperature monitoring. If temperature
847 * notification was requested for this LDO from DT then we will
848 * add the regulator to be notified if central IC temperature
851 if (rdesc[i].ldo_errs) {
852 ldo_errs_rdev_arr[temp_notif_ldos] = rdev;
853 ldo_errs_arr[temp_notif_ldos] = rdesc[i].ldo_errs;
859 /* Register INTB handlers for configured protections */
860 for (j = 0; j < idesc->num_irqs; j++) {
861 ret = bd96801_rdev_intb_irqs(pdev, pdata,
862 &idesc->irqinfo[j], rdev);
867 if (temp_notif_ldos) {
869 struct regulator_irq_desc tw_desc = {
870 .name = "bd96801-core-thermal",
872 .map_event = ldo_map_notif,
875 irq = platform_get_irq_byname(pdev, "bd96801-core-thermal");
879 retp = devm_regulator_irq_helper(&pdev->dev, &tw_desc, irq, 0,
881 &ldo_errs_rdev_arr[0],
884 return PTR_ERR(retp);
890 static const struct platform_device_id bd96801_pmic_id[] = {
891 { "bd96801-regulator", },
894 MODULE_DEVICE_TABLE(platform, bd96801_pmic_id);
896 static struct platform_driver bd96801_regulator = {
898 .name = "bd96801-pmic"
900 .probe = bd96801_probe,
901 .id_table = bd96801_pmic_id,
904 module_platform_driver(bd96801_regulator);
907 MODULE_DESCRIPTION("BD96801 voltage regulator driver");
908 MODULE_LICENSE("GPL");