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