1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Regulator driver for DA9063 PMIC series
4 //
5 // Copyright 2012 Dialog Semiconductors Ltd.
6 // Copyright 2013 Philipp Zabel, Pengutronix
7 //
8 // Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/of_regulator.h>
21 #include <linux/mfd/da9063/core.h>
22 #include <linux/mfd/da9063/registers.h>
23 
24 
25 /* Definition for registering regmap bit fields using a mask */
26 #define BFIELD(_reg, _mask) \
27 	REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
28 		sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
29 
30 /* DA9063 and DA9063L regulator IDs */
31 enum {
32 	/* BUCKs */
33 	DA9063_ID_BCORE1,
34 	DA9063_ID_BCORE2,
35 	DA9063_ID_BPRO,
36 	DA9063_ID_BMEM,
37 	DA9063_ID_BIO,
38 	DA9063_ID_BPERI,
39 
40 	/* BCORE1 and BCORE2 in merged mode */
41 	DA9063_ID_BCORES_MERGED,
42 	/* BMEM and BIO in merged mode */
43 	DA9063_ID_BMEM_BIO_MERGED,
44 	/* When two BUCKs are merged, they cannot be reused separately */
45 
46 	/* LDOs on both DA9063 and DA9063L */
47 	DA9063_ID_LDO3,
48 	DA9063_ID_LDO7,
49 	DA9063_ID_LDO8,
50 	DA9063_ID_LDO9,
51 	DA9063_ID_LDO11,
52 
53 	/* DA9063-only LDOs */
54 	DA9063_ID_LDO1,
55 	DA9063_ID_LDO2,
56 	DA9063_ID_LDO4,
57 	DA9063_ID_LDO5,
58 	DA9063_ID_LDO6,
59 	DA9063_ID_LDO10,
60 };
61 
62 /* Old regulator platform data */
63 struct da9063_regulator_data {
64 	int				id;
65 	struct regulator_init_data	*initdata;
66 };
67 
68 struct da9063_regulators_pdata {
69 	unsigned int			n_regulators;
70 	struct da9063_regulator_data	*regulator_data;
71 };
72 
73 /* Regulator capabilities and registers description */
74 struct da9063_regulator_info {
75 	struct regulator_desc desc;
76 
77 	/* DA9063 main register fields */
78 	struct reg_field mode;		/* buck mode of operation */
79 	struct reg_field suspend;
80 	struct reg_field sleep;
81 	struct reg_field suspend_sleep;
82 	unsigned int suspend_vsel_reg;
83 
84 	/* DA9063 event detection bit */
85 	struct reg_field oc_event;
86 
87 	/* DA9063 voltage monitor bit */
88 	struct reg_field vmon;
89 };
90 
91 /* Macros for LDO */
92 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
93 	.desc.id = chip##_ID_##regl_name, \
94 	.desc.name = __stringify(chip##_##regl_name), \
95 	.desc.ops = &da9063_ldo_ops, \
96 	.desc.min_uV = (min_mV) * 1000, \
97 	.desc.uV_step = (step_mV) * 1000, \
98 	.desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
99 		+ (DA9063_V##regl_name##_BIAS)), \
100 	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
101 	.desc.enable_mask = DA9063_LDO_EN, \
102 	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
103 	.desc.vsel_mask = DA9063_V##regl_name##_MASK, \
104 	.desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
105 	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
106 	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \
107 	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
108 	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B
109 
110 /* Macros for voltage DC/DC converters (BUCKs) */
111 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
112 		    creg, cmask) \
113 	.desc.id = chip##_ID_##regl_name, \
114 	.desc.name = __stringify(chip##_##regl_name), \
115 	.desc.ops = &da9063_buck_ops, \
116 	.desc.min_uV = (min_mV) * 1000, \
117 	.desc.uV_step = (step_mV) * 1000, \
118 	.desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
119 	.desc.csel_reg = (creg), \
120 	.desc.csel_mask = (cmask), \
121 	.desc.curr_table = limits_array, \
122 	.desc.n_current_limits = ARRAY_SIZE(limits_array)
123 
124 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
125 	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
126 	.desc.enable_mask = DA9063_BUCK_EN, \
127 	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
128 	.desc.vsel_mask = DA9063_VBUCK_MASK, \
129 	.desc.linear_min_sel = DA9063_VBUCK_BIAS, \
130 	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
131 	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \
132 	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
133 	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
134 	.mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
135 
136 /* Defines asignment of regulators info table to chip model */
137 struct da9063_dev_model {
138 	const struct da9063_regulator_info	*regulator_info;
139 	unsigned int				n_regulators;
140 	enum da9063_type			type;
141 };
142 
143 /* Single regulator settings */
144 struct da9063_regulator {
145 	struct regulator_desc			desc;
146 	struct regulator_dev			*rdev;
147 	struct da9063				*hw;
148 	const struct da9063_regulator_info	*info;
149 
150 	struct regmap_field			*mode;
151 	struct regmap_field			*suspend;
152 	struct regmap_field			*sleep;
153 	struct regmap_field			*suspend_sleep;
154 	struct regmap_field			*vmon;
155 };
156 
157 /* Encapsulates all information for the regulators driver */
158 struct da9063_regulators {
159 	unsigned int				n_regulators;
160 	/* Array size to be defined during init. Keep at end. */
161 	struct da9063_regulator			regulator[];
162 };
163 
164 /* BUCK modes for DA9063 */
165 enum {
166 	BUCK_MODE_MANUAL,	/* 0 */
167 	BUCK_MODE_SLEEP,	/* 1 */
168 	BUCK_MODE_SYNC,		/* 2 */
169 	BUCK_MODE_AUTO		/* 3 */
170 };
171 
172 /* Regulator operations */
173 
174 /*
175  * Current limits array (in uA) for BCORE1, BCORE2, BPRO.
176  * Entry indexes corresponds to register values.
177  */
178 static const unsigned int da9063_buck_a_limits[] = {
179 	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
180 	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
181 };
182 
183 /*
184  * Current limits array (in uA) for BMEM, BIO, BPERI.
185  * Entry indexes corresponds to register values.
186  */
187 static const unsigned int da9063_buck_b_limits[] = {
188 	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
189 	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
190 };
191 
192 /*
193  * Current limits array (in uA) for merged BCORE1 and BCORE2.
194  * Entry indexes corresponds to register values.
195  */
196 static const unsigned int da9063_bcores_merged_limits[] = {
197 	1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
198 	2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
199 };
200 
201 /*
202  * Current limits array (in uA) for merged BMEM and BIO.
203  * Entry indexes corresponds to register values.
204  */
205 static const unsigned int da9063_bmem_bio_merged_limits[] = {
206 	3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
207 	4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
208 };
209 
210 static int da9063_set_xvp(struct regulator_dev *rdev, int lim_uV, int severity, bool enable)
211 {
212 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
213 	struct device *dev = regl->hw->dev;
214 
215 	dev_dbg(dev, "%s: lim: %d, sev: %d, en: %d\n", regl->desc.name, lim_uV, severity, enable);
216 
217 	/*
218 	 * only support enable and disable.
219 	 * the da9063 offers a GPIO (GP_FB2) which is unasserted if an XV happens.
220 	 * therefore ignore severity here, as there might be handlers in hardware.
221 	 */
222 	if (lim_uV)
223 		return -EINVAL;
224 
225 	return regmap_field_write(regl->vmon, enable ? 1 : 0);
226 }
227 
228 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
229 {
230 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
231 	unsigned int val;
232 
233 	switch (mode) {
234 	case REGULATOR_MODE_FAST:
235 		val = BUCK_MODE_SYNC;
236 		break;
237 	case REGULATOR_MODE_NORMAL:
238 		val = BUCK_MODE_AUTO;
239 		break;
240 	case REGULATOR_MODE_STANDBY:
241 		val = BUCK_MODE_SLEEP;
242 		break;
243 	default:
244 		return -EINVAL;
245 	}
246 
247 	return regmap_field_write(regl->mode, val);
248 }
249 
250 /*
251  * Bucks use single mode register field for normal operation
252  * and suspend state.
253  * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
254  */
255 
256 static unsigned int da9063_buck_get_mode(struct regulator_dev *rdev)
257 {
258 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
259 	unsigned int val;
260 	int ret;
261 
262 	ret = regmap_field_read(regl->mode, &val);
263 	if (ret < 0)
264 		return ret;
265 
266 	switch (val) {
267 	default:
268 	case BUCK_MODE_MANUAL:
269 		/* Sleep flag bit decides the mode */
270 		break;
271 	case BUCK_MODE_SLEEP:
272 		return REGULATOR_MODE_STANDBY;
273 	case BUCK_MODE_SYNC:
274 		return REGULATOR_MODE_FAST;
275 	case BUCK_MODE_AUTO:
276 		return REGULATOR_MODE_NORMAL;
277 	}
278 
279 	ret = regmap_field_read(regl->sleep, &val);
280 	if (ret < 0)
281 		return 0;
282 
283 	if (val)
284 		return REGULATOR_MODE_STANDBY;
285 	else
286 		return REGULATOR_MODE_FAST;
287 }
288 
289 /*
290  * LDOs use sleep flags - one for normal and one for suspend state.
291  * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
292  */
293 
294 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
295 {
296 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
297 	unsigned int val;
298 
299 	switch (mode) {
300 	case REGULATOR_MODE_NORMAL:
301 		val = 0;
302 		break;
303 	case REGULATOR_MODE_STANDBY:
304 		val = 1;
305 		break;
306 	default:
307 		return -EINVAL;
308 	}
309 
310 	return regmap_field_write(regl->sleep, val);
311 }
312 
313 static unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev)
314 {
315 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
316 	int ret, val;
317 
318 	ret = regmap_field_read(regl->sleep, &val);
319 	if (ret < 0)
320 		return 0;
321 
322 	if (val)
323 		return REGULATOR_MODE_STANDBY;
324 	else
325 		return REGULATOR_MODE_NORMAL;
326 }
327 
328 static int da9063_buck_get_status(struct regulator_dev *rdev)
329 {
330 	int ret = regulator_is_enabled_regmap(rdev);
331 
332 	if (ret == 0) {
333 		ret = REGULATOR_STATUS_OFF;
334 	} else if (ret > 0) {
335 		ret = da9063_buck_get_mode(rdev);
336 		if (ret > 0)
337 			ret = regulator_mode_to_status(ret);
338 		else if (ret == 0)
339 			ret = -EIO;
340 	}
341 
342 	return ret;
343 }
344 
345 static int da9063_ldo_get_status(struct regulator_dev *rdev)
346 {
347 	int ret = regulator_is_enabled_regmap(rdev);
348 
349 	if (ret == 0) {
350 		ret = REGULATOR_STATUS_OFF;
351 	} else if (ret > 0) {
352 		ret = da9063_ldo_get_mode(rdev);
353 		if (ret > 0)
354 			ret = regulator_mode_to_status(ret);
355 		else if (ret == 0)
356 			ret = -EIO;
357 	}
358 
359 	return ret;
360 }
361 
362 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
363 {
364 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
365 	const struct da9063_regulator_info *rinfo = regl->info;
366 	int ret, sel;
367 
368 	sel = regulator_map_voltage_linear(rdev, uV, uV);
369 	if (sel < 0)
370 		return sel;
371 
372 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
373 
374 	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
375 				 rdev->desc->vsel_mask, sel);
376 
377 	return ret;
378 }
379 
380 static int da9063_suspend_enable(struct regulator_dev *rdev)
381 {
382 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
383 
384 	return regmap_field_write(regl->suspend, 1);
385 }
386 
387 static int da9063_suspend_disable(struct regulator_dev *rdev)
388 {
389 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
390 
391 	return regmap_field_write(regl->suspend, 0);
392 }
393 
394 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev,
395 				unsigned int mode)
396 {
397 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
398 	int val;
399 
400 	switch (mode) {
401 	case REGULATOR_MODE_FAST:
402 		val = BUCK_MODE_SYNC;
403 		break;
404 	case REGULATOR_MODE_NORMAL:
405 		val = BUCK_MODE_AUTO;
406 		break;
407 	case REGULATOR_MODE_STANDBY:
408 		val = BUCK_MODE_SLEEP;
409 		break;
410 	default:
411 		return -EINVAL;
412 	}
413 
414 	return regmap_field_write(regl->mode, val);
415 }
416 
417 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev,
418 				unsigned int mode)
419 {
420 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
421 	unsigned int val;
422 
423 	switch (mode) {
424 	case REGULATOR_MODE_NORMAL:
425 		val = 0;
426 		break;
427 	case REGULATOR_MODE_STANDBY:
428 		val = 1;
429 		break;
430 	default:
431 		return -EINVAL;
432 	}
433 
434 	return regmap_field_write(regl->suspend_sleep, val);
435 }
436 
437 static unsigned int da9063_get_overdrive_mask(const struct regulator_desc *desc)
438 {
439 	switch (desc->id) {
440 	case DA9063_ID_BCORES_MERGED:
441 	case DA9063_ID_BCORE1:
442 		return DA9063_BCORE1_OD;
443 	case DA9063_ID_BCORE2:
444 		return DA9063_BCORE2_OD;
445 	case DA9063_ID_BPRO:
446 		return DA9063_BPRO_OD;
447 	default:
448 		return 0;
449 	}
450 }
451 
452 static int da9063_buck_set_limit_set_overdrive(struct regulator_dev *rdev,
453 					       int min_uA, int max_uA,
454 					       unsigned int overdrive_mask)
455 {
456 	/*
457 	 * When enabling overdrive, do it before changing the current limit to
458 	 * ensure sufficient supply throughout the switch.
459 	 */
460 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
461 	int ret;
462 	unsigned int orig_overdrive;
463 
464 	ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H,
465 			  &orig_overdrive);
466 	if (ret < 0)
467 		return ret;
468 	orig_overdrive &= overdrive_mask;
469 
470 	if (orig_overdrive == 0) {
471 		ret = regmap_set_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
472 				overdrive_mask);
473 		if (ret < 0)
474 			return ret;
475 	}
476 
477 	ret = regulator_set_current_limit_regmap(rdev, min_uA / 2, max_uA / 2);
478 	if (ret < 0 && orig_overdrive == 0)
479 		/*
480 		 * regulator_set_current_limit_regmap may have rejected the
481 		 * change because of unusable min_uA and/or max_uA inputs.
482 		 * Attempt to restore original overdrive state, ignore failure-
483 		 * on-failure.
484 		 */
485 		regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
486 				  overdrive_mask);
487 
488 	return ret;
489 }
490 
491 static int da9063_buck_set_limit_clear_overdrive(struct regulator_dev *rdev,
492 						 int min_uA, int max_uA,
493 						 unsigned int overdrive_mask)
494 {
495 	/*
496 	 * When disabling overdrive, do it after changing the current limit to
497 	 * ensure sufficient supply throughout the switch.
498 	 */
499 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
500 	int ret, orig_limit;
501 
502 	ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &orig_limit);
503 	if (ret < 0)
504 		return ret;
505 
506 	ret = regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
507 	if (ret < 0)
508 		return ret;
509 
510 	ret = regmap_clear_bits(regl->hw->regmap, DA9063_REG_CONFIG_H,
511 				overdrive_mask);
512 	if (ret < 0)
513 		/*
514 		 * Attempt to restore original current limit, ignore failure-
515 		 * on-failure.
516 		 */
517 		regmap_write(rdev->regmap, rdev->desc->csel_reg, orig_limit);
518 
519 	return ret;
520 }
521 
522 static int da9063_buck_set_current_limit(struct regulator_dev *rdev,
523 					 int min_uA, int max_uA)
524 {
525 	unsigned int overdrive_mask, n_currents;
526 
527 	overdrive_mask = da9063_get_overdrive_mask(rdev->desc);
528 	if (overdrive_mask) {
529 		n_currents = rdev->desc->n_current_limits;
530 		if (n_currents == 0)
531 			return -EINVAL;
532 
533 		if (max_uA > rdev->desc->curr_table[n_currents - 1])
534 			return da9063_buck_set_limit_set_overdrive(rdev, min_uA,
535 								   max_uA,
536 								   overdrive_mask);
537 
538 		return da9063_buck_set_limit_clear_overdrive(rdev, min_uA,
539 							     max_uA,
540 							     overdrive_mask);
541 	}
542 	return regulator_set_current_limit_regmap(rdev, min_uA, max_uA);
543 }
544 
545 static int da9063_buck_get_current_limit(struct regulator_dev *rdev)
546 {
547 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
548 	int val, ret, limit;
549 	unsigned int mask;
550 
551 	limit = regulator_get_current_limit_regmap(rdev);
552 	if (limit < 0)
553 		return limit;
554 	mask = da9063_get_overdrive_mask(rdev->desc);
555 	if (mask) {
556 		ret = regmap_read(regl->hw->regmap, DA9063_REG_CONFIG_H, &val);
557 		if (ret < 0)
558 			return ret;
559 		if (val & mask)
560 			limit *= 2;
561 	}
562 	return limit;
563 }
564 
565 static const struct regulator_ops da9063_buck_ops = {
566 	.enable				= regulator_enable_regmap,
567 	.disable			= regulator_disable_regmap,
568 	.is_enabled			= regulator_is_enabled_regmap,
569 	.get_voltage_sel		= regulator_get_voltage_sel_regmap,
570 	.set_voltage_sel		= regulator_set_voltage_sel_regmap,
571 	.list_voltage			= regulator_list_voltage_linear,
572 	.set_current_limit		= da9063_buck_set_current_limit,
573 	.get_current_limit		= da9063_buck_get_current_limit,
574 	.set_mode			= da9063_buck_set_mode,
575 	.get_mode			= da9063_buck_get_mode,
576 	.get_status			= da9063_buck_get_status,
577 	.set_suspend_voltage		= da9063_set_suspend_voltage,
578 	.set_suspend_enable		= da9063_suspend_enable,
579 	.set_suspend_disable		= da9063_suspend_disable,
580 	.set_suspend_mode		= da9063_buck_set_suspend_mode,
581 	.set_over_voltage_protection	= da9063_set_xvp,
582 	.set_under_voltage_protection	= da9063_set_xvp,
583 };
584 
585 static const struct regulator_ops da9063_ldo_ops = {
586 	.enable				= regulator_enable_regmap,
587 	.disable			= regulator_disable_regmap,
588 	.is_enabled			= regulator_is_enabled_regmap,
589 	.get_voltage_sel		= regulator_get_voltage_sel_regmap,
590 	.set_voltage_sel		= regulator_set_voltage_sel_regmap,
591 	.list_voltage			= regulator_list_voltage_linear,
592 	.set_mode			= da9063_ldo_set_mode,
593 	.get_mode			= da9063_ldo_get_mode,
594 	.get_status			= da9063_ldo_get_status,
595 	.set_suspend_voltage		= da9063_set_suspend_voltage,
596 	.set_suspend_enable		= da9063_suspend_enable,
597 	.set_suspend_disable		= da9063_suspend_disable,
598 	.set_suspend_mode		= da9063_ldo_set_suspend_mode,
599 	.set_over_voltage_protection	= da9063_set_xvp,
600 	.set_under_voltage_protection	= da9063_set_xvp,
601 };
602 
603 /* Info of regulators for DA9063 */
604 static const struct da9063_regulator_info da9063_regulator_info[] = {
605 	{
606 		DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
607 			    da9063_buck_a_limits,
608 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
609 		DA9063_BUCK_COMMON_FIELDS(BCORE1),
610 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
611 	},
612 	{
613 		DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
614 			    da9063_buck_a_limits,
615 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
616 		DA9063_BUCK_COMMON_FIELDS(BCORE2),
617 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE2_MON_EN),
618 	},
619 	{
620 		DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
621 			    da9063_buck_a_limits,
622 			    DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
623 		DA9063_BUCK_COMMON_FIELDS(BPRO),
624 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPRO_MON_EN),
625 	},
626 	{
627 		DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
628 			    da9063_buck_b_limits,
629 			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
630 		DA9063_BUCK_COMMON_FIELDS(BMEM),
631 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
632 	},
633 	{
634 		DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
635 			    da9063_buck_b_limits,
636 			    DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
637 		DA9063_BUCK_COMMON_FIELDS(BIO),
638 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BIO_MON_EN),
639 	},
640 	{
641 		DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
642 			    da9063_buck_b_limits,
643 			    DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
644 		DA9063_BUCK_COMMON_FIELDS(BPERI),
645 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BPERI_MON_EN),
646 	},
647 	{
648 		DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
649 			    da9063_bcores_merged_limits,
650 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
651 		/* BCORES_MERGED uses the same register fields as BCORE1 */
652 		DA9063_BUCK_COMMON_FIELDS(BCORE1),
653 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BCORE1_MON_EN),
654 	},
655 	{
656 		DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
657 			    da9063_bmem_bio_merged_limits,
658 			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
659 		/* BMEM_BIO_MERGED uses the same register fields as BMEM */
660 		DA9063_BUCK_COMMON_FIELDS(BMEM),
661 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_4, DA9063_BMEM_MON_EN),
662 	},
663 	{
664 		DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
665 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
666 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO3_MON_EN),
667 	},
668 	{
669 		DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
670 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
671 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO7_MON_EN),
672 	},
673 	{
674 		DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
675 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
676 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO8_MON_EN),
677 	},
678 	{
679 		DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
680 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO9_MON_EN),
681 	},
682 	{
683 		DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
684 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
685 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO11_MON_EN),
686 	},
687 
688 	/* The following LDOs are present only on DA9063, not on DA9063L */
689 	{
690 		DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
691 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO1_MON_EN),
692 	},
693 	{
694 		DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
695 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO2_MON_EN),
696 	},
697 	{
698 		DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
699 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
700 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO4_MON_EN),
701 	},
702 	{
703 		DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
704 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO5_MON_EN),
705 	},
706 	{
707 		DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
708 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_2, DA9063_LDO6_MON_EN),
709 	},
710 
711 	{
712 		DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
713 		.vmon = BFIELD(DA9063_BB_REG_MON_REG_3, DA9063_LDO10_MON_EN),
714 	},
715 };
716 
717 /* Link chip model with regulators info table */
718 static struct da9063_dev_model regulators_models[] = {
719 	{
720 		.regulator_info = da9063_regulator_info,
721 		.n_regulators = ARRAY_SIZE(da9063_regulator_info),
722 		.type = PMIC_TYPE_DA9063,
723 	},
724 	{
725 		.regulator_info = da9063_regulator_info,
726 		.n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
727 		.type = PMIC_TYPE_DA9063L,
728 	},
729 	{ }
730 };
731 
732 /* Regulator interrupt handlers */
733 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
734 {
735 	struct da9063_regulators *regulators = data;
736 	struct da9063 *hw = regulators->regulator[0].hw;
737 	struct da9063_regulator *regl;
738 	int bits, i, ret;
739 
740 	ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
741 	if (ret < 0)
742 		return IRQ_NONE;
743 
744 	for (i = regulators->n_regulators - 1; i >= 0; i--) {
745 		regl = &regulators->regulator[i];
746 		if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
747 			continue;
748 
749 		if (BIT(regl->info->oc_event.lsb) & bits) {
750 			regulator_notifier_call_chain(regl->rdev,
751 					REGULATOR_EVENT_OVER_CURRENT, NULL);
752 		}
753 	}
754 
755 	return IRQ_HANDLED;
756 }
757 
758 /*
759  * Probing and Initialisation functions
760  */
761 static const struct regulator_init_data *da9063_get_regulator_initdata(
762 		const struct da9063_regulators_pdata *regl_pdata, int id)
763 {
764 	int i;
765 
766 	for (i = 0; i < regl_pdata->n_regulators; i++) {
767 		if (id == regl_pdata->regulator_data[i].id)
768 			return regl_pdata->regulator_data[i].initdata;
769 	}
770 
771 	return NULL;
772 }
773 
774 static int da9063_check_xvp_constraints(struct regulator_config *config)
775 {
776 	struct da9063_regulator *regl = config->driver_data;
777 	const struct regulation_constraints *constr = &config->init_data->constraints;
778 	const struct notification_limit *uv_l = &constr->under_voltage_limits;
779 	const struct notification_limit *ov_l = &constr->over_voltage_limits;
780 
781 	if (!config->init_data) /* No config in DT, pointers will be invalid */
782 		return 0;
783 
784 	/* make sure that only one severity is used to clarify if unchanged, enabled or disabled */
785 	if ((!!uv_l->prot + !!uv_l->err + !!uv_l->warn) > 1) {
786 		dev_err(config->dev, "%s: at most one voltage monitoring severity allowed!\n",
787 			regl->desc.name);
788 		return -EINVAL;
789 	}
790 
791 	/* make sure that UV and OV monitoring is set to the same severity and value */
792 	if (uv_l->prot != ov_l->prot) {
793 		dev_err(config->dev,
794 			"%s: protection-microvolt: value must be equal for uv and ov!\n",
795 			regl->desc.name);
796 		return -EINVAL;
797 	}
798 	if (uv_l->err != ov_l->err) {
799 		dev_err(config->dev, "%s: error-microvolt: value must be equal for uv and ov!\n",
800 			regl->desc.name);
801 		return -EINVAL;
802 	}
803 	if (uv_l->warn != ov_l->warn) {
804 		dev_err(config->dev, "%s: warn-microvolt: value must be equal for uv and ov!\n",
805 			regl->desc.name);
806 		return -EINVAL;
807 	}
808 
809 	return 0;
810 }
811 
812 static struct of_regulator_match da9063_matches[] = {
813 	[DA9063_ID_BCORE1]           = { .name = "bcore1"           },
814 	[DA9063_ID_BCORE2]           = { .name = "bcore2"           },
815 	[DA9063_ID_BPRO]             = { .name = "bpro",            },
816 	[DA9063_ID_BMEM]             = { .name = "bmem",            },
817 	[DA9063_ID_BIO]              = { .name = "bio",             },
818 	[DA9063_ID_BPERI]            = { .name = "bperi",           },
819 	[DA9063_ID_BCORES_MERGED]    = { .name = "bcores-merged"    },
820 	[DA9063_ID_BMEM_BIO_MERGED]  = { .name = "bmem-bio-merged", },
821 	[DA9063_ID_LDO3]             = { .name = "ldo3",            },
822 	[DA9063_ID_LDO7]             = { .name = "ldo7",            },
823 	[DA9063_ID_LDO8]             = { .name = "ldo8",            },
824 	[DA9063_ID_LDO9]             = { .name = "ldo9",            },
825 	[DA9063_ID_LDO11]            = { .name = "ldo11",           },
826 	/* The following LDOs are present only on DA9063, not on DA9063L */
827 	[DA9063_ID_LDO1]             = { .name = "ldo1",            },
828 	[DA9063_ID_LDO2]             = { .name = "ldo2",            },
829 	[DA9063_ID_LDO4]             = { .name = "ldo4",            },
830 	[DA9063_ID_LDO5]             = { .name = "ldo5",            },
831 	[DA9063_ID_LDO6]             = { .name = "ldo6",            },
832 	[DA9063_ID_LDO10]            = { .name = "ldo10",           },
833 };
834 
835 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
836 		struct platform_device *pdev,
837 		struct of_regulator_match **da9063_reg_matches)
838 {
839 	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
840 	struct da9063_regulators_pdata *pdata;
841 	struct da9063_regulator_data *rdata;
842 	struct device_node *node;
843 	int da9063_matches_len = ARRAY_SIZE(da9063_matches);
844 	int i, n, num;
845 
846 	if (da9063->type == PMIC_TYPE_DA9063L)
847 		da9063_matches_len -= 6;
848 
849 	node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
850 	if (!node) {
851 		dev_err(&pdev->dev, "Regulators device node not found\n");
852 		return ERR_PTR(-ENODEV);
853 	}
854 
855 	num = of_regulator_match(&pdev->dev, node, da9063_matches,
856 				 da9063_matches_len);
857 	of_node_put(node);
858 	if (num < 0) {
859 		dev_err(&pdev->dev, "Failed to match regulators\n");
860 		return ERR_PTR(-EINVAL);
861 	}
862 
863 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
864 	if (!pdata)
865 		return ERR_PTR(-ENOMEM);
866 
867 	pdata->regulator_data = devm_kcalloc(&pdev->dev,
868 					num, sizeof(*pdata->regulator_data),
869 					GFP_KERNEL);
870 	if (!pdata->regulator_data)
871 		return ERR_PTR(-ENOMEM);
872 	pdata->n_regulators = num;
873 
874 	n = 0;
875 	for (i = 0; i < da9063_matches_len; i++) {
876 		if (!da9063_matches[i].init_data)
877 			continue;
878 
879 		rdata = &pdata->regulator_data[n];
880 		rdata->id = i;
881 		rdata->initdata = da9063_matches[i].init_data;
882 
883 		n++;
884 	}
885 
886 	*da9063_reg_matches = da9063_matches;
887 	return pdata;
888 }
889 
890 static int da9063_regulator_probe(struct platform_device *pdev)
891 {
892 	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
893 	struct of_regulator_match *da9063_reg_matches = NULL;
894 	struct da9063_regulators_pdata *regl_pdata;
895 	const struct da9063_dev_model *model;
896 	struct da9063_regulators *regulators;
897 	struct da9063_regulator *regl;
898 	struct regulator_config config;
899 	bool bcores_merged, bmem_bio_merged;
900 	int id, irq, n, n_regulators, ret, val;
901 
902 	regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
903 
904 	if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
905 		dev_err(&pdev->dev,
906 			"No regulators defined for the platform\n");
907 		return -ENODEV;
908 	}
909 
910 	/* Find regulators set for particular device model */
911 	for (model = regulators_models; model->regulator_info; model++) {
912 		if (model->type == da9063->type)
913 			break;
914 	}
915 	if (!model->regulator_info) {
916 		dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
917 			da9063->type);
918 		return -ENODEV;
919 	}
920 
921 	ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
922 	if (ret < 0) {
923 		dev_err(&pdev->dev,
924 			"Error while reading BUCKs configuration\n");
925 		return ret;
926 	}
927 	bcores_merged = val & DA9063_BCORE_MERGE;
928 	bmem_bio_merged = val & DA9063_BUCK_MERGE;
929 
930 	n_regulators = model->n_regulators;
931 	if (bcores_merged)
932 		n_regulators -= 2; /* remove BCORE1, BCORE2 */
933 	else
934 		n_regulators--;    /* remove BCORES_MERGED */
935 	if (bmem_bio_merged)
936 		n_regulators -= 2; /* remove BMEM, BIO */
937 	else
938 		n_regulators--;    /* remove BMEM_BIO_MERGED */
939 
940 	/* Allocate memory required by usable regulators */
941 	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
942 				  regulator, n_regulators), GFP_KERNEL);
943 	if (!regulators)
944 		return -ENOMEM;
945 
946 	regulators->n_regulators = n_regulators;
947 	platform_set_drvdata(pdev, regulators);
948 
949 	/* Register all regulators declared in platform information */
950 	n = 0;
951 	id = 0;
952 	while (n < regulators->n_regulators) {
953 		/* Skip regulator IDs depending on merge mode configuration */
954 		switch (id) {
955 		case DA9063_ID_BCORE1:
956 		case DA9063_ID_BCORE2:
957 			if (bcores_merged) {
958 				id++;
959 				continue;
960 			}
961 			break;
962 		case DA9063_ID_BMEM:
963 		case DA9063_ID_BIO:
964 			if (bmem_bio_merged) {
965 				id++;
966 				continue;
967 			}
968 			break;
969 		case DA9063_ID_BCORES_MERGED:
970 			if (!bcores_merged) {
971 				id++;
972 				continue;
973 			}
974 			break;
975 		case DA9063_ID_BMEM_BIO_MERGED:
976 			if (!bmem_bio_merged) {
977 				id++;
978 				continue;
979 			}
980 			break;
981 		}
982 
983 		/* Initialise regulator structure */
984 		regl = &regulators->regulator[n];
985 		regl->hw = da9063;
986 		regl->info = &model->regulator_info[id];
987 		regl->desc = regl->info->desc;
988 		regl->desc.type = REGULATOR_VOLTAGE;
989 		regl->desc.owner = THIS_MODULE;
990 
991 		if (regl->info->mode.reg) {
992 			regl->mode = devm_regmap_field_alloc(&pdev->dev,
993 					da9063->regmap, regl->info->mode);
994 			if (IS_ERR(regl->mode))
995 				return PTR_ERR(regl->mode);
996 		}
997 
998 		if (regl->info->suspend.reg) {
999 			regl->suspend = devm_regmap_field_alloc(&pdev->dev,
1000 					da9063->regmap, regl->info->suspend);
1001 			if (IS_ERR(regl->suspend))
1002 				return PTR_ERR(regl->suspend);
1003 		}
1004 
1005 		if (regl->info->sleep.reg) {
1006 			regl->sleep = devm_regmap_field_alloc(&pdev->dev,
1007 					da9063->regmap, regl->info->sleep);
1008 			if (IS_ERR(regl->sleep))
1009 				return PTR_ERR(regl->sleep);
1010 		}
1011 
1012 		if (regl->info->suspend_sleep.reg) {
1013 			regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
1014 				da9063->regmap, regl->info->suspend_sleep);
1015 			if (IS_ERR(regl->suspend_sleep))
1016 				return PTR_ERR(regl->suspend_sleep);
1017 		}
1018 		if (regl->info->vmon.reg) {
1019 			regl->vmon = devm_regmap_field_alloc(&pdev->dev,
1020 				da9063->regmap, regl->info->vmon);
1021 			if (IS_ERR(regl->vmon))
1022 				return PTR_ERR(regl->vmon);
1023 		}
1024 
1025 		/* Register regulator */
1026 		memset(&config, 0, sizeof(config));
1027 		config.dev = &pdev->dev;
1028 		config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
1029 		config.driver_data = regl;
1030 		if (da9063_reg_matches)
1031 			config.of_node = da9063_reg_matches[id].of_node;
1032 		config.regmap = da9063->regmap;
1033 
1034 		ret = da9063_check_xvp_constraints(&config);
1035 		if (ret)
1036 			return ret;
1037 
1038 		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1039 						     &config);
1040 		if (IS_ERR(regl->rdev)) {
1041 			dev_err(&pdev->dev,
1042 				"Failed to register %s regulator\n",
1043 				regl->desc.name);
1044 			return PTR_ERR(regl->rdev);
1045 		}
1046 		id++;
1047 		n++;
1048 	}
1049 
1050 	/* LDOs overcurrent event support */
1051 	irq = platform_get_irq_byname(pdev, "LDO_LIM");
1052 	if (irq < 0)
1053 		return irq;
1054 
1055 	ret = devm_request_threaded_irq(&pdev->dev, irq,
1056 				NULL, da9063_ldo_lim_event,
1057 				IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1058 				"LDO_LIM", regulators);
1059 	if (ret)
1060 		dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
1061 
1062 	return ret;
1063 }
1064 
1065 static struct platform_driver da9063_regulator_driver = {
1066 	.driver = {
1067 		.name = DA9063_DRVNAME_REGULATORS,
1068 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1069 	},
1070 	.probe = da9063_regulator_probe,
1071 };
1072 
1073 static int __init da9063_regulator_init(void)
1074 {
1075 	return platform_driver_register(&da9063_regulator_driver);
1076 }
1077 subsys_initcall(da9063_regulator_init);
1078 
1079 static void __exit da9063_regulator_cleanup(void)
1080 {
1081 	platform_driver_unregister(&da9063_regulator_driver);
1082 }
1083 module_exit(da9063_regulator_cleanup);
1084 
1085 
1086 /* Module information */
1087 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
1088 MODULE_DESCRIPTION("DA9063 regulators driver");
1089 MODULE_LICENSE("GPL");
1090 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);
1091