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