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