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 
88 /* Macros for LDO */
89 #define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
90 	.desc.id = chip##_ID_##regl_name, \
91 	.desc.name = __stringify(chip##_##regl_name), \
92 	.desc.ops = &da9063_ldo_ops, \
93 	.desc.min_uV = (min_mV) * 1000, \
94 	.desc.uV_step = (step_mV) * 1000, \
95 	.desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
96 		+ (DA9063_V##regl_name##_BIAS)), \
97 	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
98 	.desc.enable_mask = DA9063_LDO_EN, \
99 	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
100 	.desc.vsel_mask = DA9063_V##regl_name##_MASK, \
101 	.desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
102 	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
103 	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \
104 	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
105 	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B
106 
107 /* Macros for voltage DC/DC converters (BUCKs) */
108 #define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
109 		    creg, cmask) \
110 	.desc.id = chip##_ID_##regl_name, \
111 	.desc.name = __stringify(chip##_##regl_name), \
112 	.desc.ops = &da9063_buck_ops, \
113 	.desc.min_uV = (min_mV) * 1000, \
114 	.desc.uV_step = (step_mV) * 1000, \
115 	.desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
116 	.desc.csel_reg = (creg), \
117 	.desc.csel_mask = (cmask), \
118 	.desc.curr_table = limits_array, \
119 	.desc.n_current_limits = ARRAY_SIZE(limits_array)
120 
121 #define DA9063_BUCK_COMMON_FIELDS(regl_name) \
122 	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
123 	.desc.enable_mask = DA9063_BUCK_EN, \
124 	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
125 	.desc.vsel_mask = DA9063_VBUCK_MASK, \
126 	.desc.linear_min_sel = DA9063_VBUCK_BIAS, \
127 	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
128 	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \
129 	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
130 	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
131 	.mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
132 
133 /* Defines asignment of regulators info table to chip model */
134 struct da9063_dev_model {
135 	const struct da9063_regulator_info	*regulator_info;
136 	unsigned int				n_regulators;
137 	enum da9063_type			type;
138 };
139 
140 /* Single regulator settings */
141 struct da9063_regulator {
142 	struct regulator_desc			desc;
143 	struct regulator_dev			*rdev;
144 	struct da9063				*hw;
145 	const struct da9063_regulator_info	*info;
146 
147 	struct regmap_field			*mode;
148 	struct regmap_field			*suspend;
149 	struct regmap_field			*sleep;
150 	struct regmap_field			*suspend_sleep;
151 };
152 
153 /* Encapsulates all information for the regulators driver */
154 struct da9063_regulators {
155 	unsigned int				n_regulators;
156 	/* Array size to be defined during init. Keep at end. */
157 	struct da9063_regulator			regulator[];
158 };
159 
160 /* BUCK modes for DA9063 */
161 enum {
162 	BUCK_MODE_MANUAL,	/* 0 */
163 	BUCK_MODE_SLEEP,	/* 1 */
164 	BUCK_MODE_SYNC,		/* 2 */
165 	BUCK_MODE_AUTO		/* 3 */
166 };
167 
168 /* Regulator operations */
169 
170 /*
171  * Current limits array (in uA) for BCORE1, BCORE2, BPRO.
172  * Entry indexes corresponds to register values.
173  */
174 static const unsigned int da9063_buck_a_limits[] = {
175 	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
176 	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
177 };
178 
179 /*
180  * Current limits array (in uA) for BMEM, BIO, BPERI.
181  * Entry indexes corresponds to register values.
182  */
183 static const unsigned int da9063_buck_b_limits[] = {
184 	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
185 	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
186 };
187 
188 /*
189  * Current limits array (in uA) for merged BCORE1 and BCORE2.
190  * Entry indexes corresponds to register values.
191  */
192 static const unsigned int da9063_bcores_merged_limits[] = {
193 	1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
194 	2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
195 };
196 
197 /*
198  * Current limits array (in uA) for merged BMEM and BIO.
199  * Entry indexes corresponds to register values.
200  */
201 static const unsigned int da9063_bmem_bio_merged_limits[] = {
202 	3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
203 	4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
204 };
205 
206 static int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
207 {
208 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
209 	unsigned int val;
210 
211 	switch (mode) {
212 	case REGULATOR_MODE_FAST:
213 		val = BUCK_MODE_SYNC;
214 		break;
215 	case REGULATOR_MODE_NORMAL:
216 		val = BUCK_MODE_AUTO;
217 		break;
218 	case REGULATOR_MODE_STANDBY:
219 		val = BUCK_MODE_SLEEP;
220 		break;
221 	default:
222 		return -EINVAL;
223 	}
224 
225 	return regmap_field_write(regl->mode, val);
226 }
227 
228 /*
229  * Bucks use single mode register field for normal operation
230  * and suspend state.
231  * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
232  */
233 
234 static unsigned int da9063_buck_get_mode(struct regulator_dev *rdev)
235 {
236 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
237 	unsigned int val;
238 	int ret;
239 
240 	ret = regmap_field_read(regl->mode, &val);
241 	if (ret < 0)
242 		return ret;
243 
244 	switch (val) {
245 	default:
246 	case BUCK_MODE_MANUAL:
247 		/* Sleep flag bit decides the mode */
248 		break;
249 	case BUCK_MODE_SLEEP:
250 		return REGULATOR_MODE_STANDBY;
251 	case BUCK_MODE_SYNC:
252 		return REGULATOR_MODE_FAST;
253 	case BUCK_MODE_AUTO:
254 		return REGULATOR_MODE_NORMAL;
255 	}
256 
257 	ret = regmap_field_read(regl->sleep, &val);
258 	if (ret < 0)
259 		return 0;
260 
261 	if (val)
262 		return REGULATOR_MODE_STANDBY;
263 	else
264 		return REGULATOR_MODE_FAST;
265 }
266 
267 /*
268  * LDOs use sleep flags - one for normal and one for suspend state.
269  * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
270  */
271 
272 static int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
273 {
274 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
275 	unsigned int val;
276 
277 	switch (mode) {
278 	case REGULATOR_MODE_NORMAL:
279 		val = 0;
280 		break;
281 	case REGULATOR_MODE_STANDBY:
282 		val = 1;
283 		break;
284 	default:
285 		return -EINVAL;
286 	}
287 
288 	return regmap_field_write(regl->sleep, val);
289 }
290 
291 static unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev)
292 {
293 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
294 	int ret, val;
295 
296 	ret = regmap_field_read(regl->sleep, &val);
297 	if (ret < 0)
298 		return 0;
299 
300 	if (val)
301 		return REGULATOR_MODE_STANDBY;
302 	else
303 		return REGULATOR_MODE_NORMAL;
304 }
305 
306 static int da9063_buck_get_status(struct regulator_dev *rdev)
307 {
308 	int ret = regulator_is_enabled_regmap(rdev);
309 
310 	if (ret == 0) {
311 		ret = REGULATOR_STATUS_OFF;
312 	} else if (ret > 0) {
313 		ret = da9063_buck_get_mode(rdev);
314 		if (ret > 0)
315 			ret = regulator_mode_to_status(ret);
316 		else if (ret == 0)
317 			ret = -EIO;
318 	}
319 
320 	return ret;
321 }
322 
323 static int da9063_ldo_get_status(struct regulator_dev *rdev)
324 {
325 	int ret = regulator_is_enabled_regmap(rdev);
326 
327 	if (ret == 0) {
328 		ret = REGULATOR_STATUS_OFF;
329 	} else if (ret > 0) {
330 		ret = da9063_ldo_get_mode(rdev);
331 		if (ret > 0)
332 			ret = regulator_mode_to_status(ret);
333 		else if (ret == 0)
334 			ret = -EIO;
335 	}
336 
337 	return ret;
338 }
339 
340 static int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
341 {
342 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
343 	const struct da9063_regulator_info *rinfo = regl->info;
344 	int ret, sel;
345 
346 	sel = regulator_map_voltage_linear(rdev, uV, uV);
347 	if (sel < 0)
348 		return sel;
349 
350 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
351 
352 	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
353 				 rdev->desc->vsel_mask, sel);
354 
355 	return ret;
356 }
357 
358 static int da9063_suspend_enable(struct regulator_dev *rdev)
359 {
360 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
361 
362 	return regmap_field_write(regl->suspend, 1);
363 }
364 
365 static int da9063_suspend_disable(struct regulator_dev *rdev)
366 {
367 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
368 
369 	return regmap_field_write(regl->suspend, 0);
370 }
371 
372 static int da9063_buck_set_suspend_mode(struct regulator_dev *rdev,
373 				unsigned int mode)
374 {
375 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
376 	int val;
377 
378 	switch (mode) {
379 	case REGULATOR_MODE_FAST:
380 		val = BUCK_MODE_SYNC;
381 		break;
382 	case REGULATOR_MODE_NORMAL:
383 		val = BUCK_MODE_AUTO;
384 		break;
385 	case REGULATOR_MODE_STANDBY:
386 		val = BUCK_MODE_SLEEP;
387 		break;
388 	default:
389 		return -EINVAL;
390 	}
391 
392 	return regmap_field_write(regl->mode, val);
393 }
394 
395 static int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev,
396 				unsigned int mode)
397 {
398 	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
399 	unsigned int val;
400 
401 	switch (mode) {
402 	case REGULATOR_MODE_NORMAL:
403 		val = 0;
404 		break;
405 	case REGULATOR_MODE_STANDBY:
406 		val = 1;
407 		break;
408 	default:
409 		return -EINVAL;
410 	}
411 
412 	return regmap_field_write(regl->suspend_sleep, val);
413 }
414 
415 static const struct regulator_ops da9063_buck_ops = {
416 	.enable			= regulator_enable_regmap,
417 	.disable		= regulator_disable_regmap,
418 	.is_enabled		= regulator_is_enabled_regmap,
419 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
420 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
421 	.list_voltage		= regulator_list_voltage_linear,
422 	.set_current_limit	= regulator_set_current_limit_regmap,
423 	.get_current_limit	= regulator_get_current_limit_regmap,
424 	.set_mode		= da9063_buck_set_mode,
425 	.get_mode		= da9063_buck_get_mode,
426 	.get_status		= da9063_buck_get_status,
427 	.set_suspend_voltage	= da9063_set_suspend_voltage,
428 	.set_suspend_enable	= da9063_suspend_enable,
429 	.set_suspend_disable	= da9063_suspend_disable,
430 	.set_suspend_mode	= da9063_buck_set_suspend_mode,
431 };
432 
433 static const struct regulator_ops da9063_ldo_ops = {
434 	.enable			= regulator_enable_regmap,
435 	.disable		= regulator_disable_regmap,
436 	.is_enabled		= regulator_is_enabled_regmap,
437 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
438 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
439 	.list_voltage		= regulator_list_voltage_linear,
440 	.set_mode		= da9063_ldo_set_mode,
441 	.get_mode		= da9063_ldo_get_mode,
442 	.get_status		= da9063_ldo_get_status,
443 	.set_suspend_voltage	= da9063_set_suspend_voltage,
444 	.set_suspend_enable	= da9063_suspend_enable,
445 	.set_suspend_disable	= da9063_suspend_disable,
446 	.set_suspend_mode	= da9063_ldo_set_suspend_mode,
447 };
448 
449 /* Info of regulators for DA9063 */
450 static const struct da9063_regulator_info da9063_regulator_info[] = {
451 	{
452 		DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
453 			    da9063_buck_a_limits,
454 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
455 		DA9063_BUCK_COMMON_FIELDS(BCORE1),
456 	},
457 	{
458 		DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
459 			    da9063_buck_a_limits,
460 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
461 		DA9063_BUCK_COMMON_FIELDS(BCORE2),
462 	},
463 	{
464 		DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
465 			    da9063_buck_a_limits,
466 			    DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
467 		DA9063_BUCK_COMMON_FIELDS(BPRO),
468 	},
469 	{
470 		DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
471 			    da9063_buck_b_limits,
472 			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
473 		DA9063_BUCK_COMMON_FIELDS(BMEM),
474 	},
475 	{
476 		DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
477 			    da9063_buck_b_limits,
478 			    DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
479 		DA9063_BUCK_COMMON_FIELDS(BIO),
480 	},
481 	{
482 		DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
483 			    da9063_buck_b_limits,
484 			    DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
485 		DA9063_BUCK_COMMON_FIELDS(BPERI),
486 	},
487 	{
488 		DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
489 			    da9063_bcores_merged_limits,
490 			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
491 		/* BCORES_MERGED uses the same register fields as BCORE1 */
492 		DA9063_BUCK_COMMON_FIELDS(BCORE1),
493 	},
494 	{
495 		DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
496 			    da9063_bmem_bio_merged_limits,
497 			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
498 		/* BMEM_BIO_MERGED uses the same register fields as BMEM */
499 		DA9063_BUCK_COMMON_FIELDS(BMEM),
500 	},
501 	{
502 		DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
503 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
504 	},
505 	{
506 		DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
507 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
508 	},
509 	{
510 		DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
511 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
512 	},
513 	{
514 		DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
515 	},
516 	{
517 		DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
518 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
519 	},
520 
521 	/* The following LDOs are present only on DA9063, not on DA9063L */
522 	{
523 		DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
524 	},
525 	{
526 		DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
527 	},
528 	{
529 		DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
530 		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
531 	},
532 	{
533 		DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
534 	},
535 	{
536 		DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
537 	},
538 
539 	{
540 		DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
541 	},
542 };
543 
544 /* Link chip model with regulators info table */
545 static struct da9063_dev_model regulators_models[] = {
546 	{
547 		.regulator_info = da9063_regulator_info,
548 		.n_regulators = ARRAY_SIZE(da9063_regulator_info),
549 		.type = PMIC_TYPE_DA9063,
550 	},
551 	{
552 		.regulator_info = da9063_regulator_info,
553 		.n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
554 		.type = PMIC_TYPE_DA9063L,
555 	},
556 	{ }
557 };
558 
559 /* Regulator interrupt handlers */
560 static irqreturn_t da9063_ldo_lim_event(int irq, void *data)
561 {
562 	struct da9063_regulators *regulators = data;
563 	struct da9063 *hw = regulators->regulator[0].hw;
564 	struct da9063_regulator *regl;
565 	int bits, i, ret;
566 
567 	ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
568 	if (ret < 0)
569 		return IRQ_NONE;
570 
571 	for (i = regulators->n_regulators - 1; i >= 0; i--) {
572 		regl = &regulators->regulator[i];
573 		if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
574 			continue;
575 
576 		if (BIT(regl->info->oc_event.lsb) & bits) {
577 			regulator_lock(regl->rdev);
578 			regulator_notifier_call_chain(regl->rdev,
579 					REGULATOR_EVENT_OVER_CURRENT, NULL);
580 			regulator_unlock(regl->rdev);
581 		}
582 	}
583 
584 	return IRQ_HANDLED;
585 }
586 
587 /*
588  * Probing and Initialisation functions
589  */
590 static const struct regulator_init_data *da9063_get_regulator_initdata(
591 		const struct da9063_regulators_pdata *regl_pdata, int id)
592 {
593 	int i;
594 
595 	for (i = 0; i < regl_pdata->n_regulators; i++) {
596 		if (id == regl_pdata->regulator_data[i].id)
597 			return regl_pdata->regulator_data[i].initdata;
598 	}
599 
600 	return NULL;
601 }
602 
603 static struct of_regulator_match da9063_matches[] = {
604 	[DA9063_ID_BCORE1]           = { .name = "bcore1"           },
605 	[DA9063_ID_BCORE2]           = { .name = "bcore2"           },
606 	[DA9063_ID_BPRO]             = { .name = "bpro",            },
607 	[DA9063_ID_BMEM]             = { .name = "bmem",            },
608 	[DA9063_ID_BIO]              = { .name = "bio",             },
609 	[DA9063_ID_BPERI]            = { .name = "bperi",           },
610 	[DA9063_ID_BCORES_MERGED]    = { .name = "bcores-merged"    },
611 	[DA9063_ID_BMEM_BIO_MERGED]  = { .name = "bmem-bio-merged", },
612 	[DA9063_ID_LDO3]             = { .name = "ldo3",            },
613 	[DA9063_ID_LDO7]             = { .name = "ldo7",            },
614 	[DA9063_ID_LDO8]             = { .name = "ldo8",            },
615 	[DA9063_ID_LDO9]             = { .name = "ldo9",            },
616 	[DA9063_ID_LDO11]            = { .name = "ldo11",           },
617 	/* The following LDOs are present only on DA9063, not on DA9063L */
618 	[DA9063_ID_LDO1]             = { .name = "ldo1",            },
619 	[DA9063_ID_LDO2]             = { .name = "ldo2",            },
620 	[DA9063_ID_LDO4]             = { .name = "ldo4",            },
621 	[DA9063_ID_LDO5]             = { .name = "ldo5",            },
622 	[DA9063_ID_LDO6]             = { .name = "ldo6",            },
623 	[DA9063_ID_LDO10]            = { .name = "ldo10",           },
624 };
625 
626 static struct da9063_regulators_pdata *da9063_parse_regulators_dt(
627 		struct platform_device *pdev,
628 		struct of_regulator_match **da9063_reg_matches)
629 {
630 	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
631 	struct da9063_regulators_pdata *pdata;
632 	struct da9063_regulator_data *rdata;
633 	struct device_node *node;
634 	int da9063_matches_len = ARRAY_SIZE(da9063_matches);
635 	int i, n, num;
636 
637 	if (da9063->type == PMIC_TYPE_DA9063L)
638 		da9063_matches_len -= 6;
639 
640 	node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
641 	if (!node) {
642 		dev_err(&pdev->dev, "Regulators device node not found\n");
643 		return ERR_PTR(-ENODEV);
644 	}
645 
646 	num = of_regulator_match(&pdev->dev, node, da9063_matches,
647 				 da9063_matches_len);
648 	of_node_put(node);
649 	if (num < 0) {
650 		dev_err(&pdev->dev, "Failed to match regulators\n");
651 		return ERR_PTR(-EINVAL);
652 	}
653 
654 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
655 	if (!pdata)
656 		return ERR_PTR(-ENOMEM);
657 
658 	pdata->regulator_data = devm_kcalloc(&pdev->dev,
659 					num, sizeof(*pdata->regulator_data),
660 					GFP_KERNEL);
661 	if (!pdata->regulator_data)
662 		return ERR_PTR(-ENOMEM);
663 	pdata->n_regulators = num;
664 
665 	n = 0;
666 	for (i = 0; i < da9063_matches_len; i++) {
667 		if (!da9063_matches[i].init_data)
668 			continue;
669 
670 		rdata = &pdata->regulator_data[n];
671 		rdata->id = i;
672 		rdata->initdata = da9063_matches[i].init_data;
673 
674 		n++;
675 	}
676 
677 	*da9063_reg_matches = da9063_matches;
678 	return pdata;
679 }
680 
681 static int da9063_regulator_probe(struct platform_device *pdev)
682 {
683 	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
684 	struct of_regulator_match *da9063_reg_matches = NULL;
685 	struct da9063_regulators_pdata *regl_pdata;
686 	const struct da9063_dev_model *model;
687 	struct da9063_regulators *regulators;
688 	struct da9063_regulator *regl;
689 	struct regulator_config config;
690 	bool bcores_merged, bmem_bio_merged;
691 	int id, irq, n, n_regulators, ret, val;
692 
693 	regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
694 
695 	if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
696 		dev_err(&pdev->dev,
697 			"No regulators defined for the platform\n");
698 		return -ENODEV;
699 	}
700 
701 	/* Find regulators set for particular device model */
702 	for (model = regulators_models; model->regulator_info; model++) {
703 		if (model->type == da9063->type)
704 			break;
705 	}
706 	if (!model->regulator_info) {
707 		dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
708 			da9063->type);
709 		return -ENODEV;
710 	}
711 
712 	ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
713 	if (ret < 0) {
714 		dev_err(&pdev->dev,
715 			"Error while reading BUCKs configuration\n");
716 		return ret;
717 	}
718 	bcores_merged = val & DA9063_BCORE_MERGE;
719 	bmem_bio_merged = val & DA9063_BUCK_MERGE;
720 
721 	n_regulators = model->n_regulators;
722 	if (bcores_merged)
723 		n_regulators -= 2; /* remove BCORE1, BCORE2 */
724 	else
725 		n_regulators--;    /* remove BCORES_MERGED */
726 	if (bmem_bio_merged)
727 		n_regulators -= 2; /* remove BMEM, BIO */
728 	else
729 		n_regulators--;    /* remove BMEM_BIO_MERGED */
730 
731 	/* Allocate memory required by usable regulators */
732 	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
733 				  regulator, n_regulators), GFP_KERNEL);
734 	if (!regulators)
735 		return -ENOMEM;
736 
737 	regulators->n_regulators = n_regulators;
738 	platform_set_drvdata(pdev, regulators);
739 
740 	/* Register all regulators declared in platform information */
741 	n = 0;
742 	id = 0;
743 	while (n < regulators->n_regulators) {
744 		/* Skip regulator IDs depending on merge mode configuration */
745 		switch (id) {
746 		case DA9063_ID_BCORE1:
747 		case DA9063_ID_BCORE2:
748 			if (bcores_merged) {
749 				id++;
750 				continue;
751 			}
752 			break;
753 		case DA9063_ID_BMEM:
754 		case DA9063_ID_BIO:
755 			if (bmem_bio_merged) {
756 				id++;
757 				continue;
758 			}
759 			break;
760 		case DA9063_ID_BCORES_MERGED:
761 			if (!bcores_merged) {
762 				id++;
763 				continue;
764 			}
765 			break;
766 		case DA9063_ID_BMEM_BIO_MERGED:
767 			if (!bmem_bio_merged) {
768 				id++;
769 				continue;
770 			}
771 			break;
772 		}
773 
774 		/* Initialise regulator structure */
775 		regl = &regulators->regulator[n];
776 		regl->hw = da9063;
777 		regl->info = &model->regulator_info[id];
778 		regl->desc = regl->info->desc;
779 		regl->desc.type = REGULATOR_VOLTAGE;
780 		regl->desc.owner = THIS_MODULE;
781 
782 		if (regl->info->mode.reg) {
783 			regl->mode = devm_regmap_field_alloc(&pdev->dev,
784 					da9063->regmap, regl->info->mode);
785 			if (IS_ERR(regl->mode))
786 				return PTR_ERR(regl->mode);
787 		}
788 
789 		if (regl->info->suspend.reg) {
790 			regl->suspend = devm_regmap_field_alloc(&pdev->dev,
791 					da9063->regmap, regl->info->suspend);
792 			if (IS_ERR(regl->suspend))
793 				return PTR_ERR(regl->suspend);
794 		}
795 
796 		if (regl->info->sleep.reg) {
797 			regl->sleep = devm_regmap_field_alloc(&pdev->dev,
798 					da9063->regmap, regl->info->sleep);
799 			if (IS_ERR(regl->sleep))
800 				return PTR_ERR(regl->sleep);
801 		}
802 
803 		if (regl->info->suspend_sleep.reg) {
804 			regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
805 				da9063->regmap, regl->info->suspend_sleep);
806 			if (IS_ERR(regl->suspend_sleep))
807 				return PTR_ERR(regl->suspend_sleep);
808 		}
809 
810 		/* Register regulator */
811 		memset(&config, 0, sizeof(config));
812 		config.dev = &pdev->dev;
813 		config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
814 		config.driver_data = regl;
815 		if (da9063_reg_matches)
816 			config.of_node = da9063_reg_matches[id].of_node;
817 		config.regmap = da9063->regmap;
818 		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
819 						     &config);
820 		if (IS_ERR(regl->rdev)) {
821 			dev_err(&pdev->dev,
822 				"Failed to register %s regulator\n",
823 				regl->desc.name);
824 			return PTR_ERR(regl->rdev);
825 		}
826 		id++;
827 		n++;
828 	}
829 
830 	/* LDOs overcurrent event support */
831 	irq = platform_get_irq_byname(pdev, "LDO_LIM");
832 	if (irq < 0)
833 		return irq;
834 
835 	ret = devm_request_threaded_irq(&pdev->dev, irq,
836 				NULL, da9063_ldo_lim_event,
837 				IRQF_TRIGGER_LOW | IRQF_ONESHOT,
838 				"LDO_LIM", regulators);
839 	if (ret)
840 		dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
841 
842 	return ret;
843 }
844 
845 static struct platform_driver da9063_regulator_driver = {
846 	.driver = {
847 		.name = DA9063_DRVNAME_REGULATORS,
848 	},
849 	.probe = da9063_regulator_probe,
850 };
851 
852 static int __init da9063_regulator_init(void)
853 {
854 	return platform_driver_register(&da9063_regulator_driver);
855 }
856 subsys_initcall(da9063_regulator_init);
857 
858 static void __exit da9063_regulator_cleanup(void)
859 {
860 	platform_driver_unregister(&da9063_regulator_driver);
861 }
862 module_exit(da9063_regulator_cleanup);
863 
864 
865 /* Module information */
866 MODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
867 MODULE_DESCRIPTION("DA9063 regulators driver");
868 MODULE_LICENSE("GPL");
869 MODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);
870