xref: /linux/drivers/regulator/bd96801-regulator.c (revision 7354eb7f1558466e92e926802d36e69e42938ea9)
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 ERRB interrupt and the configurations which should be done when the
9  * PMIC is in STBY mode.
10  *
11  * Supporting the ERRB interrupt would require dropping the regmap-IRQ
12  * usage or working around (or accepting a presense of) a naming conflict
13  * in debugFS IRQs.
14  *
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.
24  *
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.
31  *
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 ;)
35  */
36 
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>
45 #include <linux/of.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>
54 
55 enum {
56 	BD96801_BUCK1,
57 	BD96801_BUCK2,
58 	BD96801_BUCK3,
59 	BD96801_BUCK4,
60 	BD96801_LDO5,
61 	BD96801_LDO6,
62 	BD96801_LDO7,
63 	BD96801_REGULATOR_AMOUNT,
64 };
65 
66 enum {
67 	BD96801_PROT_OVP,
68 	BD96801_PROT_UVP,
69 	BD96801_PROT_OCP,
70 	BD96801_PROT_TEMP,
71 	BD96801_NUM_PROT,
72 };
73 
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)
83 
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
93 
94 #define BD96801_MASK_RAMP_DELAY		0xc0
95 #define BD96801_INT_VOUT_BASE_REG	0x21
96 #define BD96801_BUCK_INT_VOUT_MASK	0xff
97 
98 #define BD96801_BUCK_VOLTS		256
99 #define BD96801_LDO_VOLTS		256
100 
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
109 
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
114 
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
119 
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
124 
125 #define BD96801_REG_LDO5_OCP		0x34
126 #define BD96801_REG_LDO6_OCP		0x34
127 #define BD96801_REG_LDO7_OCP		0x34
128 
129 #define BD96801_MASK_LDO5_OCP_SHIFT	0x00
130 #define BD96801_MASK_LDO6_OCP_SHIFT	0x02
131 #define BD96801_MASK_LDO7_OCP_SHIFT	0x04
132 
133 #define BD96801_MASK_SHD_INTB		BIT(7)
134 #define BD96801_INTB_FATAL		BIT(7)
135 
136 #define BD96801_NUM_REGULATORS		7
137 #define BD96801_NUM_LDOS		4
138 
139 /*
140  * Ramp rates for bucks are controlled by bits [7:6] as follows:
141  * 00 => 1 mV/uS
142  * 01 => 5 mV/uS
143  * 10 => 10 mV/uS
144  * 11 => 20 mV/uS
145  */
146 static const unsigned int buck_ramp_table[] = { 1000, 5000, 10000, 20000 };
147 
148 /*
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.
157  */
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),
161 };
162 
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),
167 };
168 
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),
172 };
173 
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
179 
180 static int ldo_ddr_volt_table[] = {500000, 300000};
181 static int ldo_sd_volt_table[] = {3300000, 1800000};
182 
183 /* Constant IRQ initialization data (templates) */
184 struct bd96801_irqinfo {
185 	int type;
186 	struct regulator_irq_desc irq_desc;
187 	int err_cfg;
188 	int wrn_cfg;
189 	const char *irq_name;
190 };
191 
192 #define BD96801_IRQINFO(_type, _name, _irqoff_ms, _irqname)	\
193 {								\
194 	.type = (_type),					\
195 	.err_cfg = -1,						\
196 	.wrn_cfg = -1,						\
197 	.irq_name = (_irqname),					\
198 	.irq_desc = {						\
199 		.name = (_name),				\
200 		.irq_off_ms = (_irqoff_ms),			\
201 		.map_event = regulator_irq_map_event_simple,	\
202 	},							\
203 }
204 
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")
218 };
219 
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")
233 };
234 
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")
248 };
249 
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")
263 };
264 
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"),
272 };
273 
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"),
281 };
282 
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"),
290 };
291 
292 struct bd96801_irq_desc {
293 	struct bd96801_irqinfo *irqinfo;
294 	int num_irqs;
295 };
296 
297 struct bd96801_regulator_data {
298 	struct regulator_desc desc;
299 	const struct linear_range *init_ranges;
300 	int num_ranges;
301 	struct bd96801_irq_desc irq_desc;
302 	int initial_voltage;
303 	int ldo_vol_lvl;
304 	int ldo_errs;
305 };
306 
307 struct bd96801_pmic_data {
308 	struct bd96801_regulator_data regulator_data[BD96801_NUM_REGULATORS];
309 	struct regmap *regmap;
310 	int fatal_ind;
311 };
312 
313 static int ldo_map_notif(int irq, struct regulator_irq_data *rid,
314 			 unsigned long *dev_mask)
315 {
316 	int i;
317 
318 	for (i = 0; i < rid->num_states; i++) {
319 		struct bd96801_regulator_data *rdata;
320 		struct regulator_dev *rdev;
321 
322 		rdev = rid->states[i].rdev;
323 		rdata = container_of(rdev->desc, struct bd96801_regulator_data,
324 				     desc);
325 		rid->states[i].notifs = regulator_err2notif(rdata->ldo_errs);
326 		rid->states[i].errors = rdata->ldo_errs;
327 		*dev_mask |= BIT(i);
328 	}
329 	return 0;
330 }
331 
332 static int bd96801_list_voltage_lr(struct regulator_dev *rdev,
333 				   unsigned int selector)
334 {
335 	int voltage;
336 	struct bd96801_regulator_data *data;
337 
338 	data = container_of(rdev->desc, struct bd96801_regulator_data, desc);
339 
340 	/*
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.
346 	 *
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.
350 	 *
351 	 * As a result, we increase the tuning voltage which we get based on
352 	 * the selector by the stored initial_voltage.
353 	 */
354 	voltage = regulator_list_voltage_linear_range(rdev, selector);
355 	if (voltage < 0)
356 		return voltage;
357 
358 	return voltage + data->initial_voltage;
359 }
360 
361 
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,
366 };
367 
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,
375 };
376 
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,
381 };
382 
383 static int buck_get_initial_voltage(struct regmap *regmap, struct device *dev,
384 				    struct bd96801_regulator_data *data)
385 {
386 	int ret = 0, sel, initial_uv;
387 	int reg = BD96801_INT_VOUT_BASE_REG + data->desc.id;
388 
389 	if (data->num_ranges) {
390 		ret = regmap_read(regmap, reg, &sel);
391 		sel &= BD96801_BUCK_INT_VOUT_MASK;
392 
393 		ret = linear_range_get_value_array(data->init_ranges,
394 						   data->num_ranges, sel,
395 						   &initial_uv);
396 		if (ret)
397 			return ret;
398 
399 		data->initial_voltage = initial_uv;
400 		dev_dbg(dev, "Tune-scaled initial voltage %u\n",
401 			data->initial_voltage);
402 	}
403 
404 	return 0;
405 }
406 
407 static int get_ldo_initial_voltage(struct regmap *regmap,
408 				   struct device *dev,
409 				   struct bd96801_regulator_data *data)
410 {
411 	int ret;
412 	int cfgreg;
413 
414 	ret = regmap_read(regmap, data->ldo_vol_lvl, &cfgreg);
415 	if (ret)
416 		return ret;
417 
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);
422 		break;
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);
426 		break;
427 	default:
428 		dev_info(dev, "Leaving LDO to normal mode");
429 		return 0;
430 	}
431 
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;
436 
437 	return 0;
438 }
439 
440 static int get_initial_voltage(struct device *dev, struct regmap *regmap,
441 			struct bd96801_regulator_data *data)
442 {
443 	/* BUCK */
444 	if (data->desc.id <= BD96801_BUCK4)
445 		return buck_get_initial_voltage(regmap, dev, data);
446 
447 	/* LDO */
448 	return get_ldo_initial_voltage(regmap, dev, data);
449 }
450 
451 static int bd96801_walk_regulator_dt(struct device *dev, struct regmap *regmap,
452 				     struct bd96801_regulator_data *data,
453 				     int num)
454 {
455 	int i, ret;
456 	struct device_node *np;
457 	struct device_node *nproot = dev->parent->of_node;
458 
459 	nproot = of_get_child_by_name(nproot, "regulators");
460 	if (!nproot) {
461 		dev_err(dev, "failed to find regulators node\n");
462 		return -ENODEV;
463 	}
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))
467 				continue;
468 			/*
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
472 			 * loop.
473 			 */
474 			ret = get_initial_voltage(dev, regmap, &data[i]);
475 			if (ret) {
476 				dev_err(dev,
477 					"Initializing voltages for %s failed\n",
478 					data[i].desc.name);
479 				of_node_put(np);
480 				of_node_put(nproot);
481 
482 				return ret;
483 			}
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);
488 				if (ret) {
489 					dev_err(dev,
490 						"failed to set %s on-at-stby\n",
491 						data[i].desc.name);
492 					of_node_put(np);
493 					of_node_put(nproot);
494 
495 					return ret;
496 				}
497 			}
498 		}
499 	of_node_put(nproot);
500 
501 	return 0;
502 }
503 
504 /*
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.
517  */
518 static const struct bd96801_pmic_data bd96801_data = {
519 	.regulator_data = {
520 	{
521 		.desc = {
522 			.name = "buck1",
523 			.of_match = of_match_ptr("buck1"),
524 			.regulators_node = of_match_ptr("regulators"),
525 			.id = BD96801_BUCK1,
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,
541 		},
542 		.init_ranges = bd96801_buck_init_volts,
543 		.num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
544 		.irq_desc = {
545 			.irqinfo = (struct bd96801_irqinfo *)&buck1_irqinfo[0],
546 			.num_irqs = ARRAY_SIZE(buck1_irqinfo),
547 		},
548 	}, {
549 		.desc = {
550 			.name = "buck2",
551 			.of_match = of_match_ptr("buck2"),
552 			.regulators_node = of_match_ptr("regulators"),
553 			.id = BD96801_BUCK2,
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,
569 		},
570 		.irq_desc = {
571 			.irqinfo = (struct bd96801_irqinfo *)&buck2_irqinfo[0],
572 			.num_irqs = ARRAY_SIZE(buck2_irqinfo),
573 		},
574 		.init_ranges = bd96801_buck_init_volts,
575 		.num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
576 	}, {
577 		.desc = {
578 			.name = "buck3",
579 			.of_match = of_match_ptr("buck3"),
580 			.regulators_node = of_match_ptr("regulators"),
581 			.id = BD96801_BUCK3,
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,
597 		},
598 		.irq_desc = {
599 			.irqinfo = (struct bd96801_irqinfo *)&buck3_irqinfo[0],
600 			.num_irqs = ARRAY_SIZE(buck3_irqinfo),
601 		},
602 		.init_ranges = bd96801_buck_init_volts,
603 		.num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
604 	}, {
605 		.desc = {
606 			.name = "buck4",
607 			.of_match = of_match_ptr("buck4"),
608 			.regulators_node = of_match_ptr("regulators"),
609 			.id = BD96801_BUCK4,
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,
625 		},
626 		.irq_desc = {
627 			.irqinfo = (struct bd96801_irqinfo *)&buck4_irqinfo[0],
628 			.num_irqs = ARRAY_SIZE(buck4_irqinfo),
629 		},
630 		.init_ranges = bd96801_buck_init_volts,
631 		.num_ranges = ARRAY_SIZE(bd96801_buck_init_volts),
632 	}, {
633 		.desc = {
634 			.name = "ldo5",
635 			.of_match = of_match_ptr("ldo5"),
636 			.regulators_node = of_match_ptr("regulators"),
637 			.id = BD96801_LDO5,
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,
649 		},
650 		.irq_desc = {
651 			.irqinfo = (struct bd96801_irqinfo *)&ldo5_irqinfo[0],
652 			.num_irqs = ARRAY_SIZE(ldo5_irqinfo),
653 		},
654 		.ldo_vol_lvl = BD96801_LDO5_VOL_LVL_REG,
655 	}, {
656 		.desc = {
657 			.name = "ldo6",
658 			.of_match = of_match_ptr("ldo6"),
659 			.regulators_node = of_match_ptr("regulators"),
660 			.id = BD96801_LDO6,
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,
672 		},
673 		.irq_desc = {
674 			.irqinfo = (struct bd96801_irqinfo *)&ldo6_irqinfo[0],
675 			.num_irqs = ARRAY_SIZE(ldo6_irqinfo),
676 		},
677 		.ldo_vol_lvl = BD96801_LDO6_VOL_LVL_REG,
678 	}, {
679 		.desc = {
680 			.name = "ldo7",
681 			.of_match = of_match_ptr("ldo7"),
682 			.regulators_node = of_match_ptr("regulators"),
683 			.id = BD96801_LDO7,
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,
695 		},
696 		.irq_desc = {
697 			.irqinfo = (struct bd96801_irqinfo *)&ldo7_irqinfo[0],
698 			.num_irqs = ARRAY_SIZE(ldo7_irqinfo),
699 		},
700 		.ldo_vol_lvl = BD96801_LDO7_VOL_LVL_REG,
701 	},
702 	},
703 };
704 
705 static int initialize_pmic_data(struct device *dev,
706 				struct bd96801_pmic_data *pdata)
707 {
708 	int r, i;
709 
710 	/*
711 	 * Allocate and initialize IRQ data for all of the regulators. We
712 	 * wish to modify IRQ information independently for each driver
713 	 * instance.
714 	 */
715 	for (r = 0; r < BD96801_NUM_REGULATORS; r++) {
716 		const struct bd96801_irqinfo *template;
717 		struct bd96801_irqinfo *new;
718 		int num_infos;
719 
720 		template = pdata->regulator_data[r].irq_desc.irqinfo;
721 		num_infos = pdata->regulator_data[r].irq_desc.num_irqs;
722 
723 		new = devm_kcalloc(dev, num_infos, sizeof(*new), GFP_KERNEL);
724 		if (!new)
725 			return -ENOMEM;
726 
727 		pdata->regulator_data[r].irq_desc.irqinfo = new;
728 
729 		for (i = 0; i < num_infos; i++)
730 			new[i] = template[i];
731 	}
732 
733 	return 0;
734 }
735 
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)
740 {
741 	struct regulator_dev *rdev_arr[1];
742 	void *retp;
743 	int err = 0;
744 	int irq;
745 	int err_flags[] = {
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,
750 
751 	};
752 	int wrn_flags[] = {
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,
757 	};
758 
759 	/*
760 	 * Don't install IRQ handler if both error and warning
761 	 * notifications are explicitly disabled
762 	 */
763 	if (!iinfo->err_cfg && !iinfo->wrn_cfg)
764 		return 0;
765 
766 	if (WARN_ON(iinfo->type >= BD96801_NUM_PROT))
767 		return -EINVAL;
768 
769 	if (iinfo->err_cfg)
770 		err = err_flags[iinfo->type];
771 	else if (iinfo->wrn_cfg)
772 		err = wrn_flags[iinfo->type];
773 
774 	iinfo->irq_desc.data = pdata;
775 	irq = platform_get_irq_byname(pdev, iinfo->irq_name);
776 	if (irq < 0)
777 		return irq;
778 	/* Find notifications for this IRQ (WARN/ERR) */
779 
780 	rdev_arr[0] = rdev;
781 	retp = devm_regulator_irq_helper(&pdev->dev,
782 					 &iinfo->irq_desc, irq,
783 					 0, err, NULL, rdev_arr,
784 					 1);
785 	if (IS_ERR(retp))
786 		return PTR_ERR(retp);
787 
788 	return 0;
789 }
790 
791 
792 
793 static int bd96801_probe(struct platform_device *pdev)
794 {
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;
802 	int i, ret;
803 	void *retp;
804 
805 	parent = pdev->dev.parent;
806 
807 	pdata = devm_kmemdup(&pdev->dev, &bd96801_data, sizeof(bd96801_data),
808 			     GFP_KERNEL);
809 	if (!pdata)
810 		return -ENOMEM;
811 
812 	if (initialize_pmic_data(&pdev->dev, pdata))
813 		return -ENOMEM;
814 
815 	pdata->regmap = dev_get_regmap(parent, NULL);
816 	if (!pdata->regmap) {
817 		dev_err(&pdev->dev, "No register map found\n");
818 		return -ENODEV;
819 	}
820 
821 	rdesc = &pdata->regulator_data[0];
822 
823 	config.driver_data = pdata;
824 	config.regmap = pdata->regmap;
825 	config.dev = parent;
826 
827 	ret = bd96801_walk_regulator_dt(&pdev->dev, pdata->regmap, rdesc,
828 					BD96801_NUM_REGULATORS);
829 	if (ret)
830 		return ret;
831 
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;
835 		int j;
836 
837 		rdev = devm_regulator_register(&pdev->dev,
838 					       &rdesc[i].desc, &config);
839 		if (IS_ERR(rdev)) {
840 			dev_err(&pdev->dev,
841 				"failed to register %s regulator\n",
842 				rdesc[i].desc.name);
843 			return PTR_ERR(rdev);
844 		}
845 		/*
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
849 		 * exceeds threshold.
850 		 */
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;
854 			temp_notif_ldos++;
855 		}
856 		if (!idesc)
857 			continue;
858 
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);
863 			if (ret)
864 				return ret;
865 		}
866 	}
867 	if (temp_notif_ldos) {
868 		int irq;
869 		struct regulator_irq_desc tw_desc = {
870 			.name = "bd96801-core-thermal",
871 			.irq_off_ms = 500,
872 			.map_event = ldo_map_notif,
873 		};
874 
875 		irq = platform_get_irq_byname(pdev, "bd96801-core-thermal");
876 		if (irq < 0)
877 			return irq;
878 
879 		retp = devm_regulator_irq_helper(&pdev->dev, &tw_desc, irq, 0,
880 						 0, &ldo_errs_arr[0],
881 						 &ldo_errs_rdev_arr[0],
882 						 temp_notif_ldos);
883 		if (IS_ERR(retp))
884 			return PTR_ERR(retp);
885 	}
886 
887 	return 0;
888 }
889 
890 static const struct platform_device_id bd96801_pmic_id[] = {
891 	{ "bd96801-regulator", },
892 	{ }
893 };
894 MODULE_DEVICE_TABLE(platform, bd96801_pmic_id);
895 
896 static struct platform_driver bd96801_regulator = {
897 	.driver = {
898 		.name = "bd96801-pmic"
899 	},
900 	.probe = bd96801_probe,
901 	.id_table = bd96801_pmic_id,
902 };
903 
904 module_platform_driver(bd96801_regulator);
905 
906 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
907 MODULE_DESCRIPTION("BD96801 voltage regulator driver");
908 MODULE_LICENSE("GPL");
909