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:
27 * https://lore.kernel.org/all/cover.1712920132.git.mazziesaccount@gmail.com/
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/cleanup.h>
38 #include <linux/delay.h>
39 #include <linux/err.h>
40 #include <linux/interrupt.h>
41 #include <linux/kernel.h>
42 #include <linux/linear_range.h>
43 #include <linux/mfd/rohm-generic.h>
44 #include <linux/mfd/rohm-bd96801.h>
45 #include <linux/module.h>
47 #include <linux/platform_device.h>
48 #include <linux/regmap.h>
49 #include <linux/regulator/coupler.h>
50 #include <linux/regulator/driver.h>
51 #include <linux/regulator/machine.h>
52 #include <linux/regulator/of_regulator.h>
53 #include <linux/slab.h>
54 #include <linux/timer.h>
64 BD96801_REGULATOR_AMOUNT
,
75 #define BD96801_ALWAYS_ON_REG 0x3c
76 #define BD96801_REG_ENABLE 0x0b
77 #define BD96801_BUCK1_EN_MASK BIT(0)
78 #define BD96801_BUCK2_EN_MASK BIT(1)
79 #define BD96801_BUCK3_EN_MASK BIT(2)
80 #define BD96801_BUCK4_EN_MASK BIT(3)
81 #define BD96801_LDO5_EN_MASK BIT(4)
82 #define BD96801_LDO6_EN_MASK BIT(5)
83 #define BD96801_LDO7_EN_MASK BIT(6)
85 #define BD96801_BUCK1_VSEL_REG 0x28
86 #define BD96801_BUCK2_VSEL_REG 0x29
87 #define BD96801_BUCK3_VSEL_REG 0x2a
88 #define BD96801_BUCK4_VSEL_REG 0x2b
89 #define BD96801_LDO5_VSEL_REG 0x25
90 #define BD96801_LDO6_VSEL_REG 0x26
91 #define BD96801_LDO7_VSEL_REG 0x27
92 #define BD96801_BUCK_VSEL_MASK 0x1F
93 #define BD96801_LDO_VSEL_MASK 0xff
95 #define BD96801_MASK_RAMP_DELAY 0xc0
96 #define BD96801_INT_VOUT_BASE_REG 0x21
97 #define BD96801_BUCK_INT_VOUT_MASK 0xff
99 #define BD96801_BUCK_VOLTS 256
100 #define BD96801_LDO_VOLTS 256
102 #define BD96801_OVP_MASK 0x03
103 #define BD96801_MASK_BUCK1_OVP_SHIFT 0x00
104 #define BD96801_MASK_BUCK2_OVP_SHIFT 0x02
105 #define BD96801_MASK_BUCK3_OVP_SHIFT 0x04
106 #define BD96801_MASK_BUCK4_OVP_SHIFT 0x06
107 #define BD96801_MASK_LDO5_OVP_SHIFT 0x00
108 #define BD96801_MASK_LDO6_OVP_SHIFT 0x02
109 #define BD96801_MASK_LDO7_OVP_SHIFT 0x04
111 #define BD96801_PROT_LIMIT_OCP_MIN 0x00
112 #define BD96801_PROT_LIMIT_LOW 0x01
113 #define BD96801_PROT_LIMIT_MID 0x02
114 #define BD96801_PROT_LIMIT_HI 0x03
116 #define BD96801_REG_BUCK1_OCP 0x32
117 #define BD96801_REG_BUCK2_OCP 0x32
118 #define BD96801_REG_BUCK3_OCP 0x33
119 #define BD96801_REG_BUCK4_OCP 0x33
121 #define BD96801_MASK_BUCK1_OCP_SHIFT 0x00
122 #define BD96801_MASK_BUCK2_OCP_SHIFT 0x04
123 #define BD96801_MASK_BUCK3_OCP_SHIFT 0x00
124 #define BD96801_MASK_BUCK4_OCP_SHIFT 0x04
126 #define BD96801_REG_LDO5_OCP 0x34
127 #define BD96801_REG_LDO6_OCP 0x34
128 #define BD96801_REG_LDO7_OCP 0x34
130 #define BD96801_MASK_LDO5_OCP_SHIFT 0x00
131 #define BD96801_MASK_LDO6_OCP_SHIFT 0x02
132 #define BD96801_MASK_LDO7_OCP_SHIFT 0x04
134 #define BD96801_MASK_SHD_INTB BIT(7)
135 #define BD96801_INTB_FATAL BIT(7)
137 #define BD96801_NUM_REGULATORS 7
138 #define BD96801_NUM_LDOS 4
141 * Ramp rates for bucks are controlled by bits [7:6] as follows:
147 static const unsigned int buck_ramp_table
[] = { 1000, 5000, 10000, 20000 };
150 * This is a voltage range that get's appended to selected
151 * bd96801_buck_init_volts value. The range from 0x0 to 0xF is actually
152 * bd96801_buck_init_volts + 0 ... bd96801_buck_init_volts + 150mV
153 * and the range from 0x10 to 0x1f is bd96801_buck_init_volts - 150mV ...
154 * bd96801_buck_init_volts - 0. But as the members of linear_range
155 * are all unsigned I will apply offset of -150 mV to value in
156 * linear_range - which should increase these ranges with
157 * 150 mV getting all the values to >= 0.
159 static const struct linear_range bd96801_tune_volts
[] = {
160 REGULATOR_LINEAR_RANGE(150000, 0x00, 0xF, 10000),
161 REGULATOR_LINEAR_RANGE(0, 0x10, 0x1F, 10000),
164 static const struct linear_range bd96801_buck_init_volts
[] = {
165 REGULATOR_LINEAR_RANGE(500000 - 150000, 0x00, 0xc8, 5000),
166 REGULATOR_LINEAR_RANGE(1550000 - 150000, 0xc9, 0xec, 50000),
167 REGULATOR_LINEAR_RANGE(3300000 - 150000, 0xed, 0xff, 0),
170 static const struct linear_range bd96801_ldo_int_volts
[] = {
171 REGULATOR_LINEAR_RANGE(300000, 0x00, 0x78, 25000),
172 REGULATOR_LINEAR_RANGE(3300000, 0x79, 0xff, 0),
175 #define BD96801_LDO_SD_VOLT_MASK 0x1
176 #define BD96801_LDO_MODE_MASK 0x6
177 #define BD96801_LDO_MODE_INT 0x0
178 #define BD96801_LDO_MODE_SD 0x2
179 #define BD96801_LDO_MODE_DDR 0x4
181 static int ldo_ddr_volt_table
[] = {500000, 300000};
182 static int ldo_sd_volt_table
[] = {3300000, 1800000};
184 /* Constant IRQ initialization data (templates) */
185 struct bd96801_irqinfo
{
187 struct regulator_irq_desc irq_desc
;
190 const char *irq_name
;
193 #define BD96801_IRQINFO(_type, _name, _irqoff_ms, _irqname) \
198 .irq_name = (_irqname), \
201 .irq_off_ms = (_irqoff_ms), \
202 .map_event = regulator_irq_map_event_simple, \
206 static const struct bd96801_irqinfo buck1_irqinfo
[] = {
207 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck1-over-curr-h", 500,
208 "bd96801-buck1-overcurr-h"),
209 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck1-over-curr-l", 500,
210 "bd96801-buck1-overcurr-l"),
211 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck1-over-curr-n", 500,
212 "bd96801-buck1-overcurr-n"),
213 BD96801_IRQINFO(BD96801_PROT_OVP
, "buck1-over-voltage", 500,
214 "bd96801-buck1-overvolt"),
215 BD96801_IRQINFO(BD96801_PROT_UVP
, "buck1-under-voltage", 500,
216 "bd96801-buck1-undervolt"),
217 BD96801_IRQINFO(BD96801_PROT_TEMP
, "buck1-over-temp", 500,
218 "bd96801-buck1-thermal")
221 static const struct bd96801_irqinfo buck2_irqinfo
[] = {
222 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck2-over-curr-h", 500,
223 "bd96801-buck2-overcurr-h"),
224 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck2-over-curr-l", 500,
225 "bd96801-buck2-overcurr-l"),
226 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck2-over-curr-n", 500,
227 "bd96801-buck2-overcurr-n"),
228 BD96801_IRQINFO(BD96801_PROT_OVP
, "buck2-over-voltage", 500,
229 "bd96801-buck2-overvolt"),
230 BD96801_IRQINFO(BD96801_PROT_UVP
, "buck2-under-voltage", 500,
231 "bd96801-buck2-undervolt"),
232 BD96801_IRQINFO(BD96801_PROT_TEMP
, "buck2-over-temp", 500,
233 "bd96801-buck2-thermal")
236 static const struct bd96801_irqinfo buck3_irqinfo
[] = {
237 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck3-over-curr-h", 500,
238 "bd96801-buck3-overcurr-h"),
239 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck3-over-curr-l", 500,
240 "bd96801-buck3-overcurr-l"),
241 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck3-over-curr-n", 500,
242 "bd96801-buck3-overcurr-n"),
243 BD96801_IRQINFO(BD96801_PROT_OVP
, "buck3-over-voltage", 500,
244 "bd96801-buck3-overvolt"),
245 BD96801_IRQINFO(BD96801_PROT_UVP
, "buck3-under-voltage", 500,
246 "bd96801-buck3-undervolt"),
247 BD96801_IRQINFO(BD96801_PROT_TEMP
, "buck3-over-temp", 500,
248 "bd96801-buck3-thermal")
251 static const struct bd96801_irqinfo buck4_irqinfo
[] = {
252 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck4-over-curr-h", 500,
253 "bd96801-buck4-overcurr-h"),
254 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck4-over-curr-l", 500,
255 "bd96801-buck4-overcurr-l"),
256 BD96801_IRQINFO(BD96801_PROT_OCP
, "buck4-over-curr-n", 500,
257 "bd96801-buck4-overcurr-n"),
258 BD96801_IRQINFO(BD96801_PROT_OVP
, "buck4-over-voltage", 500,
259 "bd96801-buck4-overvolt"),
260 BD96801_IRQINFO(BD96801_PROT_UVP
, "buck4-under-voltage", 500,
261 "bd96801-buck4-undervolt"),
262 BD96801_IRQINFO(BD96801_PROT_TEMP
, "buck4-over-temp", 500,
263 "bd96801-buck4-thermal")
266 static const struct bd96801_irqinfo ldo5_irqinfo
[] = {
267 BD96801_IRQINFO(BD96801_PROT_OCP
, "ldo5-overcurr", 500,
268 "bd96801-ldo5-overcurr"),
269 BD96801_IRQINFO(BD96801_PROT_OVP
, "ldo5-over-voltage", 500,
270 "bd96801-ldo5-overvolt"),
271 BD96801_IRQINFO(BD96801_PROT_UVP
, "ldo5-under-voltage", 500,
272 "bd96801-ldo5-undervolt"),
275 static const struct bd96801_irqinfo ldo6_irqinfo
[] = {
276 BD96801_IRQINFO(BD96801_PROT_OCP
, "ldo6-overcurr", 500,
277 "bd96801-ldo6-overcurr"),
278 BD96801_IRQINFO(BD96801_PROT_OVP
, "ldo6-over-voltage", 500,
279 "bd96801-ldo6-overvolt"),
280 BD96801_IRQINFO(BD96801_PROT_UVP
, "ldo6-under-voltage", 500,
281 "bd96801-ldo6-undervolt"),
284 static const struct bd96801_irqinfo ldo7_irqinfo
[] = {
285 BD96801_IRQINFO(BD96801_PROT_OCP
, "ldo7-overcurr", 500,
286 "bd96801-ldo7-overcurr"),
287 BD96801_IRQINFO(BD96801_PROT_OVP
, "ldo7-over-voltage", 500,
288 "bd96801-ldo7-overvolt"),
289 BD96801_IRQINFO(BD96801_PROT_UVP
, "ldo7-under-voltage", 500,
290 "bd96801-ldo7-undervolt"),
293 struct bd96801_irq_desc
{
294 struct bd96801_irqinfo
*irqinfo
;
298 struct bd96801_regulator_data
{
299 struct regulator_desc desc
;
300 const struct linear_range
*init_ranges
;
302 struct bd96801_irq_desc irq_desc
;
308 struct bd96801_pmic_data
{
309 struct bd96801_regulator_data regulator_data
[BD96801_NUM_REGULATORS
];
310 struct regmap
*regmap
;
314 static int ldo_map_notif(int irq
, struct regulator_irq_data
*rid
,
315 unsigned long *dev_mask
)
319 for (i
= 0; i
< rid
->num_states
; i
++) {
320 struct bd96801_regulator_data
*rdata
;
321 struct regulator_dev
*rdev
;
323 rdev
= rid
->states
[i
].rdev
;
324 rdata
= container_of(rdev
->desc
, struct bd96801_regulator_data
,
326 rid
->states
[i
].notifs
= regulator_err2notif(rdata
->ldo_errs
);
327 rid
->states
[i
].errors
= rdata
->ldo_errs
;
333 static int bd96801_list_voltage_lr(struct regulator_dev
*rdev
,
334 unsigned int selector
)
337 struct bd96801_regulator_data
*data
;
339 data
= container_of(rdev
->desc
, struct bd96801_regulator_data
, desc
);
342 * The BD096801 has voltage setting in two registers. One giving the
343 * "initial voltage" (can be changed only when regulator is disabled.
344 * This driver caches the value and sets it only at startup. The other
345 * register is voltage tuning value which applies -150 mV ... +150 mV
346 * offset to the voltage.
348 * Note that the cached initial voltage stored in regulator data is
349 * 'scaled down' by the 150 mV so that all of our tuning values are
350 * >= 0. This is done because the linear_ranges uses unsigned values.
352 * As a result, we increase the tuning voltage which we get based on
353 * the selector by the stored initial_voltage.
355 voltage
= regulator_list_voltage_linear_range(rdev
, selector
);
359 return voltage
+ data
->initial_voltage
;
363 static const struct regulator_ops bd96801_ldo_table_ops
= {
364 .is_enabled
= regulator_is_enabled_regmap
,
365 .list_voltage
= regulator_list_voltage_table
,
366 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
369 static const struct regulator_ops bd96801_buck_ops
= {
370 .is_enabled
= regulator_is_enabled_regmap
,
371 .list_voltage
= bd96801_list_voltage_lr
,
372 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
373 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
374 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
375 .set_ramp_delay
= regulator_set_ramp_delay_regmap
,
378 static const struct regulator_ops bd96801_ldo_ops
= {
379 .is_enabled
= regulator_is_enabled_regmap
,
380 .list_voltage
= regulator_list_voltage_linear_range
,
381 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
384 static int buck_get_initial_voltage(struct regmap
*regmap
, struct device
*dev
,
385 struct bd96801_regulator_data
*data
)
387 int ret
= 0, sel
, initial_uv
;
388 int reg
= BD96801_INT_VOUT_BASE_REG
+ data
->desc
.id
;
390 if (data
->num_ranges
) {
391 ret
= regmap_read(regmap
, reg
, &sel
);
392 sel
&= BD96801_BUCK_INT_VOUT_MASK
;
394 ret
= linear_range_get_value_array(data
->init_ranges
,
395 data
->num_ranges
, sel
,
400 data
->initial_voltage
= initial_uv
;
401 dev_dbg(dev
, "Tune-scaled initial voltage %u\n",
402 data
->initial_voltage
);
408 static int get_ldo_initial_voltage(struct regmap
*regmap
,
410 struct bd96801_regulator_data
*data
)
415 ret
= regmap_read(regmap
, data
->ldo_vol_lvl
, &cfgreg
);
419 switch (cfgreg
& BD96801_LDO_MODE_MASK
) {
420 case BD96801_LDO_MODE_DDR
:
421 data
->desc
.volt_table
= ldo_ddr_volt_table
;
422 data
->desc
.n_voltages
= ARRAY_SIZE(ldo_ddr_volt_table
);
424 case BD96801_LDO_MODE_SD
:
425 data
->desc
.volt_table
= ldo_sd_volt_table
;
426 data
->desc
.n_voltages
= ARRAY_SIZE(ldo_sd_volt_table
);
429 dev_info(dev
, "Leaving LDO to normal mode");
433 /* SD or DDR mode => override default ops */
434 data
->desc
.ops
= &bd96801_ldo_table_ops
,
435 data
->desc
.vsel_mask
= 1;
436 data
->desc
.vsel_reg
= data
->ldo_vol_lvl
;
441 static int get_initial_voltage(struct device
*dev
, struct regmap
*regmap
,
442 struct bd96801_regulator_data
*data
)
445 if (data
->desc
.id
<= BD96801_BUCK4
)
446 return buck_get_initial_voltage(regmap
, dev
, data
);
449 return get_ldo_initial_voltage(regmap
, dev
, data
);
452 static int bd96801_walk_regulator_dt(struct device
*dev
, struct regmap
*regmap
,
453 struct bd96801_regulator_data
*data
,
458 struct device_node
*nproot
__free(device_node
) =
459 of_get_child_by_name(dev
->parent
->of_node
, "regulators");
461 dev_err(dev
, "failed to find regulators node\n");
464 for_each_child_of_node_scoped(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",
482 if (of_property_read_bool(np
, "rohm,keep-on-stby")) {
483 ret
= regmap_set_bits(regmap
,
484 BD96801_ALWAYS_ON_REG
,
485 1 << data
[i
].desc
.id
);
488 "failed to set %s on-at-stby\n",
500 * Template for regulator data. Probe will allocate dynamic / driver instance
501 * struct so we should be on a safe side even if there were multiple PMICs to
502 * control. Note that there is a plan to allow multiple PMICs to be used so
503 * systems can scale better. I am however still slightly unsure how the
504 * multi-PMIC case will be handled. I don't know if the processor will have I2C
505 * acces to all of the PMICs or only the first one. I'd guess there will be
506 * access provided to all PMICs for voltage scaling - but the errors will only
507 * be informed via the master PMIC. Eg, we should prepare to support multiple
508 * driver instances - either with or without the IRQs... Well, let's first
509 * just support the simple and clear single-PMIC setup and ponder the multi PMIC
510 * case later. What we can easly do for preparing is to not use static global
511 * data for regulators though.
513 static const struct bd96801_pmic_data bd96801_data
= {
518 .of_match
= of_match_ptr("buck1"),
519 .regulators_node
= of_match_ptr("regulators"),
521 .ops
= &bd96801_buck_ops
,
522 .type
= REGULATOR_VOLTAGE
,
523 .linear_ranges
= bd96801_tune_volts
,
524 .n_linear_ranges
= ARRAY_SIZE(bd96801_tune_volts
),
525 .n_voltages
= BD96801_BUCK_VOLTS
,
526 .enable_reg
= BD96801_REG_ENABLE
,
527 .enable_mask
= BD96801_BUCK1_EN_MASK
,
528 .enable_is_inverted
= true,
529 .vsel_reg
= BD96801_BUCK1_VSEL_REG
,
530 .vsel_mask
= BD96801_BUCK_VSEL_MASK
,
531 .ramp_reg
= BD96801_BUCK1_VSEL_REG
,
532 .ramp_mask
= BD96801_MASK_RAMP_DELAY
,
533 .ramp_delay_table
= &buck_ramp_table
[0],
534 .n_ramp_values
= ARRAY_SIZE(buck_ramp_table
),
535 .owner
= THIS_MODULE
,
537 .init_ranges
= bd96801_buck_init_volts
,
538 .num_ranges
= ARRAY_SIZE(bd96801_buck_init_volts
),
540 .irqinfo
= (struct bd96801_irqinfo
*)&buck1_irqinfo
[0],
541 .num_irqs
= ARRAY_SIZE(buck1_irqinfo
),
546 .of_match
= of_match_ptr("buck2"),
547 .regulators_node
= of_match_ptr("regulators"),
549 .ops
= &bd96801_buck_ops
,
550 .type
= REGULATOR_VOLTAGE
,
551 .linear_ranges
= bd96801_tune_volts
,
552 .n_linear_ranges
= ARRAY_SIZE(bd96801_tune_volts
),
553 .n_voltages
= BD96801_BUCK_VOLTS
,
554 .enable_reg
= BD96801_REG_ENABLE
,
555 .enable_mask
= BD96801_BUCK2_EN_MASK
,
556 .enable_is_inverted
= true,
557 .vsel_reg
= BD96801_BUCK2_VSEL_REG
,
558 .vsel_mask
= BD96801_BUCK_VSEL_MASK
,
559 .ramp_reg
= BD96801_BUCK2_VSEL_REG
,
560 .ramp_mask
= BD96801_MASK_RAMP_DELAY
,
561 .ramp_delay_table
= &buck_ramp_table
[0],
562 .n_ramp_values
= ARRAY_SIZE(buck_ramp_table
),
563 .owner
= THIS_MODULE
,
566 .irqinfo
= (struct bd96801_irqinfo
*)&buck2_irqinfo
[0],
567 .num_irqs
= ARRAY_SIZE(buck2_irqinfo
),
569 .init_ranges
= bd96801_buck_init_volts
,
570 .num_ranges
= ARRAY_SIZE(bd96801_buck_init_volts
),
574 .of_match
= of_match_ptr("buck3"),
575 .regulators_node
= of_match_ptr("regulators"),
577 .ops
= &bd96801_buck_ops
,
578 .type
= REGULATOR_VOLTAGE
,
579 .linear_ranges
= bd96801_tune_volts
,
580 .n_linear_ranges
= ARRAY_SIZE(bd96801_tune_volts
),
581 .n_voltages
= BD96801_BUCK_VOLTS
,
582 .enable_reg
= BD96801_REG_ENABLE
,
583 .enable_mask
= BD96801_BUCK3_EN_MASK
,
584 .enable_is_inverted
= true,
585 .vsel_reg
= BD96801_BUCK3_VSEL_REG
,
586 .vsel_mask
= BD96801_BUCK_VSEL_MASK
,
587 .ramp_reg
= BD96801_BUCK3_VSEL_REG
,
588 .ramp_mask
= BD96801_MASK_RAMP_DELAY
,
589 .ramp_delay_table
= &buck_ramp_table
[0],
590 .n_ramp_values
= ARRAY_SIZE(buck_ramp_table
),
591 .owner
= THIS_MODULE
,
594 .irqinfo
= (struct bd96801_irqinfo
*)&buck3_irqinfo
[0],
595 .num_irqs
= ARRAY_SIZE(buck3_irqinfo
),
597 .init_ranges
= bd96801_buck_init_volts
,
598 .num_ranges
= ARRAY_SIZE(bd96801_buck_init_volts
),
602 .of_match
= of_match_ptr("buck4"),
603 .regulators_node
= of_match_ptr("regulators"),
605 .ops
= &bd96801_buck_ops
,
606 .type
= REGULATOR_VOLTAGE
,
607 .linear_ranges
= bd96801_tune_volts
,
608 .n_linear_ranges
= ARRAY_SIZE(bd96801_tune_volts
),
609 .n_voltages
= BD96801_BUCK_VOLTS
,
610 .enable_reg
= BD96801_REG_ENABLE
,
611 .enable_mask
= BD96801_BUCK4_EN_MASK
,
612 .enable_is_inverted
= true,
613 .vsel_reg
= BD96801_BUCK4_VSEL_REG
,
614 .vsel_mask
= BD96801_BUCK_VSEL_MASK
,
615 .ramp_reg
= BD96801_BUCK4_VSEL_REG
,
616 .ramp_mask
= BD96801_MASK_RAMP_DELAY
,
617 .ramp_delay_table
= &buck_ramp_table
[0],
618 .n_ramp_values
= ARRAY_SIZE(buck_ramp_table
),
619 .owner
= THIS_MODULE
,
622 .irqinfo
= (struct bd96801_irqinfo
*)&buck4_irqinfo
[0],
623 .num_irqs
= ARRAY_SIZE(buck4_irqinfo
),
625 .init_ranges
= bd96801_buck_init_volts
,
626 .num_ranges
= ARRAY_SIZE(bd96801_buck_init_volts
),
630 .of_match
= of_match_ptr("ldo5"),
631 .regulators_node
= of_match_ptr("regulators"),
633 .ops
= &bd96801_ldo_ops
,
634 .type
= REGULATOR_VOLTAGE
,
635 .linear_ranges
= bd96801_ldo_int_volts
,
636 .n_linear_ranges
= ARRAY_SIZE(bd96801_ldo_int_volts
),
637 .n_voltages
= BD96801_LDO_VOLTS
,
638 .enable_reg
= BD96801_REG_ENABLE
,
639 .enable_mask
= BD96801_LDO5_EN_MASK
,
640 .enable_is_inverted
= true,
641 .vsel_reg
= BD96801_LDO5_VSEL_REG
,
642 .vsel_mask
= BD96801_LDO_VSEL_MASK
,
643 .owner
= THIS_MODULE
,
646 .irqinfo
= (struct bd96801_irqinfo
*)&ldo5_irqinfo
[0],
647 .num_irqs
= ARRAY_SIZE(ldo5_irqinfo
),
649 .ldo_vol_lvl
= BD96801_LDO5_VOL_LVL_REG
,
653 .of_match
= of_match_ptr("ldo6"),
654 .regulators_node
= of_match_ptr("regulators"),
656 .ops
= &bd96801_ldo_ops
,
657 .type
= REGULATOR_VOLTAGE
,
658 .linear_ranges
= bd96801_ldo_int_volts
,
659 .n_linear_ranges
= ARRAY_SIZE(bd96801_ldo_int_volts
),
660 .n_voltages
= BD96801_LDO_VOLTS
,
661 .enable_reg
= BD96801_REG_ENABLE
,
662 .enable_mask
= BD96801_LDO6_EN_MASK
,
663 .enable_is_inverted
= true,
664 .vsel_reg
= BD96801_LDO6_VSEL_REG
,
665 .vsel_mask
= BD96801_LDO_VSEL_MASK
,
666 .owner
= THIS_MODULE
,
669 .irqinfo
= (struct bd96801_irqinfo
*)&ldo6_irqinfo
[0],
670 .num_irqs
= ARRAY_SIZE(ldo6_irqinfo
),
672 .ldo_vol_lvl
= BD96801_LDO6_VOL_LVL_REG
,
676 .of_match
= of_match_ptr("ldo7"),
677 .regulators_node
= of_match_ptr("regulators"),
679 .ops
= &bd96801_ldo_ops
,
680 .type
= REGULATOR_VOLTAGE
,
681 .linear_ranges
= bd96801_ldo_int_volts
,
682 .n_linear_ranges
= ARRAY_SIZE(bd96801_ldo_int_volts
),
683 .n_voltages
= BD96801_LDO_VOLTS
,
684 .enable_reg
= BD96801_REG_ENABLE
,
685 .enable_mask
= BD96801_LDO7_EN_MASK
,
686 .enable_is_inverted
= true,
687 .vsel_reg
= BD96801_LDO7_VSEL_REG
,
688 .vsel_mask
= BD96801_LDO_VSEL_MASK
,
689 .owner
= THIS_MODULE
,
692 .irqinfo
= (struct bd96801_irqinfo
*)&ldo7_irqinfo
[0],
693 .num_irqs
= ARRAY_SIZE(ldo7_irqinfo
),
695 .ldo_vol_lvl
= BD96801_LDO7_VOL_LVL_REG
,
700 static int initialize_pmic_data(struct device
*dev
,
701 struct bd96801_pmic_data
*pdata
)
706 * Allocate and initialize IRQ data for all of the regulators. We
707 * wish to modify IRQ information independently for each driver
710 for (r
= 0; r
< BD96801_NUM_REGULATORS
; r
++) {
711 const struct bd96801_irqinfo
*template;
712 struct bd96801_irqinfo
*new;
715 template = pdata
->regulator_data
[r
].irq_desc
.irqinfo
;
716 num_infos
= pdata
->regulator_data
[r
].irq_desc
.num_irqs
;
718 new = devm_kcalloc(dev
, num_infos
, sizeof(*new), GFP_KERNEL
);
722 pdata
->regulator_data
[r
].irq_desc
.irqinfo
= new;
724 for (i
= 0; i
< num_infos
; i
++)
725 new[i
] = template[i
];
731 static int bd96801_rdev_intb_irqs(struct platform_device
*pdev
,
732 struct bd96801_pmic_data
*pdata
,
733 struct bd96801_irqinfo
*iinfo
,
734 struct regulator_dev
*rdev
)
736 struct regulator_dev
*rdev_arr
[1];
741 [BD96801_PROT_OVP
] = REGULATOR_ERROR_REGULATION_OUT
,
742 [BD96801_PROT_UVP
] = REGULATOR_ERROR_UNDER_VOLTAGE
,
743 [BD96801_PROT_OCP
] = REGULATOR_ERROR_OVER_CURRENT
,
744 [BD96801_PROT_TEMP
] = REGULATOR_ERROR_OVER_TEMP
,
748 [BD96801_PROT_OVP
] = REGULATOR_ERROR_OVER_VOLTAGE_WARN
,
749 [BD96801_PROT_UVP
] = REGULATOR_ERROR_UNDER_VOLTAGE_WARN
,
750 [BD96801_PROT_OCP
] = REGULATOR_ERROR_OVER_CURRENT_WARN
,
751 [BD96801_PROT_TEMP
] = REGULATOR_ERROR_OVER_TEMP_WARN
,
755 * Don't install IRQ handler if both error and warning
756 * notifications are explicitly disabled
758 if (!iinfo
->err_cfg
&& !iinfo
->wrn_cfg
)
761 if (WARN_ON(iinfo
->type
>= BD96801_NUM_PROT
))
765 err
= err_flags
[iinfo
->type
];
766 else if (iinfo
->wrn_cfg
)
767 err
= wrn_flags
[iinfo
->type
];
769 iinfo
->irq_desc
.data
= pdata
;
770 irq
= platform_get_irq_byname(pdev
, iinfo
->irq_name
);
773 /* Find notifications for this IRQ (WARN/ERR) */
776 retp
= devm_regulator_irq_helper(&pdev
->dev
,
777 &iinfo
->irq_desc
, irq
,
778 0, err
, NULL
, rdev_arr
,
781 return PTR_ERR(retp
);
788 static int bd96801_probe(struct platform_device
*pdev
)
790 struct regulator_dev
*ldo_errs_rdev_arr
[BD96801_NUM_LDOS
];
791 struct bd96801_regulator_data
*rdesc
;
792 struct regulator_config config
= {};
793 int ldo_errs_arr
[BD96801_NUM_LDOS
];
794 struct bd96801_pmic_data
*pdata
;
795 int temp_notif_ldos
= 0;
796 struct device
*parent
;
800 parent
= pdev
->dev
.parent
;
802 pdata
= devm_kmemdup(&pdev
->dev
, &bd96801_data
, sizeof(bd96801_data
),
807 if (initialize_pmic_data(&pdev
->dev
, pdata
))
810 pdata
->regmap
= dev_get_regmap(parent
, NULL
);
811 if (!pdata
->regmap
) {
812 dev_err(&pdev
->dev
, "No register map found\n");
816 rdesc
= &pdata
->regulator_data
[0];
818 config
.driver_data
= pdata
;
819 config
.regmap
= pdata
->regmap
;
822 ret
= bd96801_walk_regulator_dt(&pdev
->dev
, pdata
->regmap
, rdesc
,
823 BD96801_NUM_REGULATORS
);
827 for (i
= 0; i
< ARRAY_SIZE(pdata
->regulator_data
); i
++) {
828 struct regulator_dev
*rdev
;
829 struct bd96801_irq_desc
*idesc
= &rdesc
[i
].irq_desc
;
832 rdev
= devm_regulator_register(&pdev
->dev
,
833 &rdesc
[i
].desc
, &config
);
836 "failed to register %s regulator\n",
838 return PTR_ERR(rdev
);
841 * LDOs don't have own temperature monitoring. If temperature
842 * notification was requested for this LDO from DT then we will
843 * add the regulator to be notified if central IC temperature
846 if (rdesc
[i
].ldo_errs
) {
847 ldo_errs_rdev_arr
[temp_notif_ldos
] = rdev
;
848 ldo_errs_arr
[temp_notif_ldos
] = rdesc
[i
].ldo_errs
;
852 /* Register INTB handlers for configured protections */
853 for (j
= 0; j
< idesc
->num_irqs
; j
++) {
854 ret
= bd96801_rdev_intb_irqs(pdev
, pdata
,
855 &idesc
->irqinfo
[j
], rdev
);
860 if (temp_notif_ldos
) {
862 struct regulator_irq_desc tw_desc
= {
863 .name
= "bd96801-core-thermal",
865 .map_event
= ldo_map_notif
,
868 irq
= platform_get_irq_byname(pdev
, "bd96801-core-thermal");
872 retp
= devm_regulator_irq_helper(&pdev
->dev
, &tw_desc
, irq
, 0,
874 &ldo_errs_rdev_arr
[0],
877 return PTR_ERR(retp
);
883 static const struct platform_device_id bd96801_pmic_id
[] = {
884 { "bd96801-regulator", },
887 MODULE_DEVICE_TABLE(platform
, bd96801_pmic_id
);
889 static struct platform_driver bd96801_regulator
= {
891 .name
= "bd96801-pmic"
893 .probe
= bd96801_probe
,
894 .id_table
= bd96801_pmic_id
,
897 module_platform_driver(bd96801_regulator
);
899 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
900 MODULE_DESCRIPTION("BD96801 voltage regulator driver");
901 MODULE_LICENSE("GPL");