1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Regulator device driver for DA9061 and DA9062.
4 // Copyright (C) 2015-2017  Dialog Semiconductor
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/mfd/da9062/core.h>
18 #include <linux/mfd/da9062/registers.h>
19 
20 /* Regulator IDs */
21 enum {
22 	DA9061_ID_BUCK1,
23 	DA9061_ID_BUCK2,
24 	DA9061_ID_BUCK3,
25 	DA9061_ID_LDO1,
26 	DA9061_ID_LDO2,
27 	DA9061_ID_LDO3,
28 	DA9061_ID_LDO4,
29 	DA9061_MAX_REGULATORS,
30 };
31 
32 enum {
33 	DA9062_ID_BUCK1,
34 	DA9062_ID_BUCK2,
35 	DA9062_ID_BUCK3,
36 	DA9062_ID_BUCK4,
37 	DA9062_ID_LDO1,
38 	DA9062_ID_LDO2,
39 	DA9062_ID_LDO3,
40 	DA9062_ID_LDO4,
41 	DA9062_MAX_REGULATORS,
42 };
43 
44 /* Regulator capabilities and registers description */
45 struct da9062_regulator_info {
46 	struct regulator_desc desc;
47 	/* Main register fields */
48 	struct reg_field mode;
49 	struct reg_field suspend;
50 	struct reg_field sleep;
51 	struct reg_field suspend_sleep;
52 	unsigned int suspend_vsel_reg;
53 	/* Event detection bit */
54 	struct reg_field oc_event;
55 };
56 
57 /* Single regulator settings */
58 struct da9062_regulator {
59 	struct regulator_desc			desc;
60 	struct regulator_dev			*rdev;
61 	struct da9062				*hw;
62 	const struct da9062_regulator_info	*info;
63 
64 	struct regmap_field			*mode;
65 	struct regmap_field			*suspend;
66 	struct regmap_field			*sleep;
67 	struct regmap_field			*suspend_sleep;
68 };
69 
70 /* Encapsulates all information for the regulators driver */
71 struct da9062_regulators {
72 	int					irq_ldo_lim;
73 	unsigned				n_regulators;
74 	/* Array size to be defined during init. Keep at end. */
75 	struct da9062_regulator			regulator[0];
76 };
77 
78 /* BUCK modes */
79 enum {
80 	BUCK_MODE_MANUAL,	/* 0 */
81 	BUCK_MODE_SLEEP,	/* 1 */
82 	BUCK_MODE_SYNC,		/* 2 */
83 	BUCK_MODE_AUTO		/* 3 */
84 };
85 
86 /* Regulator operations */
87 
88 /* Current limits array (in uA)
89  * - DA9061_ID_[BUCK1|BUCK3]
90  * - DA9062_ID_[BUCK1|BUCK2|BUCK4]
91  * Entry indexes corresponds to register values.
92  */
93 static const unsigned int da9062_buck_a_limits[] = {
94 	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
95 	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
96 };
97 
98 /* Current limits array (in uA)
99  * - DA9061_ID_BUCK2
100  * - DA9062_ID_BUCK3
101  * Entry indexes corresponds to register values.
102  */
103 static const unsigned int da9062_buck_b_limits[] = {
104 	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
105 	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
106 };
107 
108 static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
109 {
110 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
111 	unsigned val;
112 
113 	switch (mode) {
114 	case REGULATOR_MODE_FAST:
115 		val = BUCK_MODE_SYNC;
116 		break;
117 	case REGULATOR_MODE_NORMAL:
118 		val = BUCK_MODE_AUTO;
119 		break;
120 	case REGULATOR_MODE_STANDBY:
121 		val = BUCK_MODE_SLEEP;
122 		break;
123 	default:
124 		return -EINVAL;
125 	}
126 
127 	return regmap_field_write(regl->mode, val);
128 }
129 
130 /*
131  * Bucks use single mode register field for normal operation
132  * and suspend state.
133  * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
134  */
135 
136 static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
137 {
138 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
139 	struct regmap_field *field;
140 	unsigned int val, mode = 0;
141 	int ret;
142 
143 	ret = regmap_field_read(regl->mode, &val);
144 	if (ret < 0)
145 		return ret;
146 
147 	switch (val) {
148 	default:
149 	case BUCK_MODE_MANUAL:
150 		mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
151 		/* Sleep flag bit decides the mode */
152 		break;
153 	case BUCK_MODE_SLEEP:
154 		return REGULATOR_MODE_STANDBY;
155 	case BUCK_MODE_SYNC:
156 		return REGULATOR_MODE_FAST;
157 	case BUCK_MODE_AUTO:
158 		return REGULATOR_MODE_NORMAL;
159 	}
160 
161 	/* Detect current regulator state */
162 	ret = regmap_field_read(regl->suspend, &val);
163 	if (ret < 0)
164 		return 0;
165 
166 	/* Read regulator mode from proper register, depending on state */
167 	if (val)
168 		field = regl->suspend_sleep;
169 	else
170 		field = regl->sleep;
171 
172 	ret = regmap_field_read(field, &val);
173 	if (ret < 0)
174 		return 0;
175 
176 	if (val)
177 		mode &= REGULATOR_MODE_STANDBY;
178 	else
179 		mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
180 
181 	return mode;
182 }
183 
184 /*
185  * LDOs use sleep flags - one for normal and one for suspend state.
186  * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
187  */
188 
189 static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
190 {
191 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
192 	unsigned val;
193 
194 	switch (mode) {
195 	case REGULATOR_MODE_NORMAL:
196 		val = 0;
197 		break;
198 	case REGULATOR_MODE_STANDBY:
199 		val = 1;
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	return regmap_field_write(regl->sleep, val);
206 }
207 
208 static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
209 {
210 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
211 	struct regmap_field *field;
212 	int ret, val;
213 
214 	/* Detect current regulator state */
215 	ret = regmap_field_read(regl->suspend, &val);
216 	if (ret < 0)
217 		return 0;
218 
219 	/* Read regulator mode from proper register, depending on state */
220 	if (val)
221 		field = regl->suspend_sleep;
222 	else
223 		field = regl->sleep;
224 
225 	ret = regmap_field_read(field, &val);
226 	if (ret < 0)
227 		return 0;
228 
229 	if (val)
230 		return REGULATOR_MODE_STANDBY;
231 	else
232 		return REGULATOR_MODE_NORMAL;
233 }
234 
235 static int da9062_buck_get_status(struct regulator_dev *rdev)
236 {
237 	int ret = regulator_is_enabled_regmap(rdev);
238 
239 	if (ret == 0) {
240 		ret = REGULATOR_STATUS_OFF;
241 	} else if (ret > 0) {
242 		ret = da9062_buck_get_mode(rdev);
243 		if (ret > 0)
244 			ret = regulator_mode_to_status(ret);
245 		else if (ret == 0)
246 			ret = -EIO;
247 	}
248 
249 	return ret;
250 }
251 
252 static int da9062_ldo_get_status(struct regulator_dev *rdev)
253 {
254 	int ret = regulator_is_enabled_regmap(rdev);
255 
256 	if (ret == 0) {
257 		ret = REGULATOR_STATUS_OFF;
258 	} else if (ret > 0) {
259 		ret = da9062_ldo_get_mode(rdev);
260 		if (ret > 0)
261 			ret = regulator_mode_to_status(ret);
262 		else if (ret == 0)
263 			ret = -EIO;
264 	}
265 
266 	return ret;
267 }
268 
269 static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
270 {
271 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
272 	const struct da9062_regulator_info *rinfo = regl->info;
273 	int ret, sel;
274 
275 	sel = regulator_map_voltage_linear(rdev, uv, uv);
276 	if (sel < 0)
277 		return sel;
278 
279 	sel <<= ffs(rdev->desc->vsel_mask) - 1;
280 
281 	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
282 				 rdev->desc->vsel_mask, sel);
283 
284 	return ret;
285 }
286 
287 static int da9062_suspend_enable(struct regulator_dev *rdev)
288 {
289 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
290 
291 	return regmap_field_write(regl->suspend, 1);
292 }
293 
294 static int da9062_suspend_disable(struct regulator_dev *rdev)
295 {
296 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
297 
298 	return regmap_field_write(regl->suspend, 0);
299 }
300 
301 static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
302 					unsigned mode)
303 {
304 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
305 	int val;
306 
307 	switch (mode) {
308 	case REGULATOR_MODE_FAST:
309 		val = BUCK_MODE_SYNC;
310 		break;
311 	case REGULATOR_MODE_NORMAL:
312 		val = BUCK_MODE_AUTO;
313 		break;
314 	case REGULATOR_MODE_STANDBY:
315 		val = BUCK_MODE_SLEEP;
316 		break;
317 	default:
318 		return -EINVAL;
319 	}
320 
321 	return regmap_field_write(regl->mode, val);
322 }
323 
324 static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
325 						unsigned mode)
326 {
327 	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
328 	unsigned val;
329 
330 	switch (mode) {
331 	case REGULATOR_MODE_NORMAL:
332 		val = 0;
333 		break;
334 	case REGULATOR_MODE_STANDBY:
335 		val = 1;
336 		break;
337 	default:
338 		return -EINVAL;
339 	}
340 
341 	return regmap_field_write(regl->suspend_sleep, val);
342 }
343 
344 static const struct regulator_ops da9062_buck_ops = {
345 	.enable			= regulator_enable_regmap,
346 	.disable		= regulator_disable_regmap,
347 	.is_enabled		= regulator_is_enabled_regmap,
348 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
349 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
350 	.list_voltage		= regulator_list_voltage_linear,
351 	.set_current_limit	= regulator_set_current_limit_regmap,
352 	.get_current_limit	= regulator_get_current_limit_regmap,
353 	.set_mode		= da9062_buck_set_mode,
354 	.get_mode		= da9062_buck_get_mode,
355 	.get_status		= da9062_buck_get_status,
356 	.set_suspend_voltage	= da9062_set_suspend_voltage,
357 	.set_suspend_enable	= da9062_suspend_enable,
358 	.set_suspend_disable	= da9062_suspend_disable,
359 	.set_suspend_mode	= da9062_buck_set_suspend_mode,
360 };
361 
362 static const struct regulator_ops da9062_ldo_ops = {
363 	.enable			= regulator_enable_regmap,
364 	.disable		= regulator_disable_regmap,
365 	.is_enabled		= regulator_is_enabled_regmap,
366 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
367 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
368 	.list_voltage		= regulator_list_voltage_linear,
369 	.set_mode		= da9062_ldo_set_mode,
370 	.get_mode		= da9062_ldo_get_mode,
371 	.get_status		= da9062_ldo_get_status,
372 	.set_suspend_voltage	= da9062_set_suspend_voltage,
373 	.set_suspend_enable	= da9062_suspend_enable,
374 	.set_suspend_disable	= da9062_suspend_disable,
375 	.set_suspend_mode	= da9062_ldo_set_suspend_mode,
376 };
377 
378 /* DA9061 Regulator information */
379 static const struct da9062_regulator_info local_da9061_regulator_info[] = {
380 	{
381 		.desc.id = DA9061_ID_BUCK1,
382 		.desc.name = "DA9061 BUCK1",
383 		.desc.of_match = of_match_ptr("buck1"),
384 		.desc.regulators_node = of_match_ptr("regulators"),
385 		.desc.ops = &da9062_buck_ops,
386 		.desc.min_uV = (300) * 1000,
387 		.desc.uV_step = (10) * 1000,
388 		.desc.n_voltages = ((1570) - (300))/(10) + 1,
389 		.desc.curr_table = da9062_buck_a_limits,
390 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
391 		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
392 		.desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK,
393 		.desc.enable_reg = DA9062AA_BUCK1_CONT,
394 		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
395 		.desc.vsel_reg = DA9062AA_VBUCK1_A,
396 		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
397 		.desc.linear_min_sel = 0,
398 		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
399 			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
400 			sizeof(unsigned int) * 8 -
401 			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
402 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
403 			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
404 			sizeof(unsigned int) * 8 -
405 			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
406 		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
407 		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
408 			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
409 			sizeof(unsigned int) * 8 -
410 			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
411 		.suspend = REG_FIELD(DA9062AA_DVC_1,
412 			__builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
413 			sizeof(unsigned int) * 8 -
414 			__builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
415 	},
416 	{
417 		.desc.id = DA9061_ID_BUCK2,
418 		.desc.name = "DA9061 BUCK2",
419 		.desc.of_match = of_match_ptr("buck2"),
420 		.desc.regulators_node = of_match_ptr("regulators"),
421 		.desc.ops = &da9062_buck_ops,
422 		.desc.min_uV = (800) * 1000,
423 		.desc.uV_step = (20) * 1000,
424 		.desc.n_voltages = ((3340) - (800))/(20) + 1,
425 		.desc.curr_table = da9062_buck_b_limits,
426 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
427 		.desc.csel_reg = DA9062AA_BUCK_ILIM_A,
428 		.desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK,
429 		.desc.enable_reg = DA9062AA_BUCK3_CONT,
430 		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
431 		.desc.vsel_reg = DA9062AA_VBUCK3_A,
432 		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
433 		.desc.linear_min_sel = 0,
434 		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
435 			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
436 			sizeof(unsigned int) * 8 -
437 			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
438 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
439 			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
440 			sizeof(unsigned int) * 8 -
441 			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
442 		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
443 		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
444 			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
445 			sizeof(unsigned int) * 8 -
446 			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
447 		.suspend = REG_FIELD(DA9062AA_DVC_1,
448 			__builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
449 			sizeof(unsigned int) * 8 -
450 			__builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
451 	},
452 	{
453 		.desc.id = DA9061_ID_BUCK3,
454 		.desc.name = "DA9061 BUCK3",
455 		.desc.of_match = of_match_ptr("buck3"),
456 		.desc.regulators_node = of_match_ptr("regulators"),
457 		.desc.ops = &da9062_buck_ops,
458 		.desc.min_uV = (530) * 1000,
459 		.desc.uV_step = (10) * 1000,
460 		.desc.n_voltages = ((1800) - (530))/(10) + 1,
461 		.desc.curr_table = da9062_buck_a_limits,
462 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
463 		.desc.csel_reg = DA9062AA_BUCK_ILIM_B,
464 		.desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK,
465 		.desc.enable_reg = DA9062AA_BUCK4_CONT,
466 		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
467 		.desc.vsel_reg = DA9062AA_VBUCK4_A,
468 		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
469 		.desc.linear_min_sel = 0,
470 		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
471 			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
472 			sizeof(unsigned int) * 8 -
473 			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
474 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
475 			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
476 			sizeof(unsigned int) * 8 -
477 			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
478 		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
479 		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
480 			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
481 			sizeof(unsigned int) * 8 -
482 			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
483 		.suspend = REG_FIELD(DA9062AA_DVC_1,
484 			__builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
485 			sizeof(unsigned int) * 8 -
486 			__builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
487 	},
488 	{
489 		.desc.id = DA9061_ID_LDO1,
490 		.desc.name = "DA9061 LDO1",
491 		.desc.of_match = of_match_ptr("ldo1"),
492 		.desc.regulators_node = of_match_ptr("regulators"),
493 		.desc.ops = &da9062_ldo_ops,
494 		.desc.min_uV = (900) * 1000,
495 		.desc.uV_step = (50) * 1000,
496 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
497 		.desc.enable_reg = DA9062AA_LDO1_CONT,
498 		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
499 		.desc.vsel_reg = DA9062AA_VLDO1_A,
500 		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
501 		.desc.linear_min_sel = 0,
502 		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
503 			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
504 			sizeof(unsigned int) * 8 -
505 			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
506 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
507 			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
508 			sizeof(unsigned int) * 8 -
509 			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
510 		.suspend_vsel_reg = DA9062AA_VLDO1_B,
511 		.suspend = REG_FIELD(DA9062AA_DVC_1,
512 			__builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
513 			sizeof(unsigned int) * 8 -
514 			__builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
515 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
516 			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
517 			sizeof(unsigned int) * 8 -
518 			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
519 	},
520 	{
521 		.desc.id = DA9061_ID_LDO2,
522 		.desc.name = "DA9061 LDO2",
523 		.desc.of_match = of_match_ptr("ldo2"),
524 		.desc.regulators_node = of_match_ptr("regulators"),
525 		.desc.ops = &da9062_ldo_ops,
526 		.desc.min_uV = (900) * 1000,
527 		.desc.uV_step = (50) * 1000,
528 		.desc.n_voltages = ((3600) - (600))/(50) + 1,
529 		.desc.enable_reg = DA9062AA_LDO2_CONT,
530 		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
531 		.desc.vsel_reg = DA9062AA_VLDO2_A,
532 		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
533 		.desc.linear_min_sel = 0,
534 		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
535 			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
536 			sizeof(unsigned int) * 8 -
537 			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
538 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
539 			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
540 			sizeof(unsigned int) * 8 -
541 			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
542 		.suspend_vsel_reg = DA9062AA_VLDO2_B,
543 		.suspend = REG_FIELD(DA9062AA_DVC_1,
544 			__builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
545 			sizeof(unsigned int) * 8 -
546 			__builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
547 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
548 			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
549 			sizeof(unsigned int) * 8 -
550 			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
551 	},
552 	{
553 		.desc.id = DA9061_ID_LDO3,
554 		.desc.name = "DA9061 LDO3",
555 		.desc.of_match = of_match_ptr("ldo3"),
556 		.desc.regulators_node = of_match_ptr("regulators"),
557 		.desc.ops = &da9062_ldo_ops,
558 		.desc.min_uV = (900) * 1000,
559 		.desc.uV_step = (50) * 1000,
560 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
561 		.desc.enable_reg = DA9062AA_LDO3_CONT,
562 		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
563 		.desc.vsel_reg = DA9062AA_VLDO3_A,
564 		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
565 		.desc.linear_min_sel = 0,
566 		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
567 			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
568 			sizeof(unsigned int) * 8 -
569 			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
570 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
571 			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
572 			sizeof(unsigned int) * 8 -
573 			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
574 		.suspend_vsel_reg = DA9062AA_VLDO3_B,
575 		.suspend = REG_FIELD(DA9062AA_DVC_1,
576 			__builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
577 			sizeof(unsigned int) * 8 -
578 			__builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
579 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
580 			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
581 			sizeof(unsigned int) * 8 -
582 			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
583 	},
584 	{
585 		.desc.id = DA9061_ID_LDO4,
586 		.desc.name = "DA9061 LDO4",
587 		.desc.of_match = of_match_ptr("ldo4"),
588 		.desc.regulators_node = of_match_ptr("regulators"),
589 		.desc.ops = &da9062_ldo_ops,
590 		.desc.min_uV = (900) * 1000,
591 		.desc.uV_step = (50) * 1000,
592 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
593 		.desc.enable_reg = DA9062AA_LDO4_CONT,
594 		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
595 		.desc.vsel_reg = DA9062AA_VLDO4_A,
596 		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
597 		.desc.linear_min_sel = 0,
598 		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
599 			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
600 			sizeof(unsigned int) * 8 -
601 			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
602 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
603 			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
604 			sizeof(unsigned int) * 8 -
605 			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
606 		.suspend_vsel_reg = DA9062AA_VLDO4_B,
607 		.suspend = REG_FIELD(DA9062AA_DVC_1,
608 			__builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
609 			sizeof(unsigned int) * 8 -
610 			__builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
611 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
612 			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
613 			sizeof(unsigned int) * 8 -
614 			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
615 	},
616 };
617 
618 /* DA9062 Regulator information */
619 static const struct da9062_regulator_info local_da9062_regulator_info[] = {
620 	{
621 		.desc.id = DA9062_ID_BUCK1,
622 		.desc.name = "DA9062 BUCK1",
623 		.desc.of_match = of_match_ptr("buck1"),
624 		.desc.regulators_node = of_match_ptr("regulators"),
625 		.desc.ops = &da9062_buck_ops,
626 		.desc.min_uV = (300) * 1000,
627 		.desc.uV_step = (10) * 1000,
628 		.desc.n_voltages = ((1570) - (300))/(10) + 1,
629 		.desc.curr_table = da9062_buck_a_limits,
630 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
631 		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
632 		.desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK,
633 		.desc.enable_reg = DA9062AA_BUCK1_CONT,
634 		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
635 		.desc.vsel_reg = DA9062AA_VBUCK1_A,
636 		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
637 		.desc.linear_min_sel = 0,
638 		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
639 			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
640 			sizeof(unsigned int) * 8 -
641 			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
642 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
643 			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
644 			sizeof(unsigned int) * 8 -
645 			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
646 		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
647 		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
648 			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
649 			sizeof(unsigned int) * 8 -
650 			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
651 		.suspend = REG_FIELD(DA9062AA_DVC_1,
652 			__builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
653 			sizeof(unsigned int) * 8 -
654 			__builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
655 	},
656 	{
657 		.desc.id = DA9062_ID_BUCK2,
658 		.desc.name = "DA9062 BUCK2",
659 		.desc.of_match = of_match_ptr("buck2"),
660 		.desc.regulators_node = of_match_ptr("regulators"),
661 		.desc.ops = &da9062_buck_ops,
662 		.desc.min_uV = (300) * 1000,
663 		.desc.uV_step = (10) * 1000,
664 		.desc.n_voltages = ((1570) - (300))/(10) + 1,
665 		.desc.curr_table = da9062_buck_a_limits,
666 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
667 		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
668 		.desc.csel_mask = DA9062AA_BUCK2_ILIM_MASK,
669 		.desc.enable_reg = DA9062AA_BUCK2_CONT,
670 		.desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
671 		.desc.vsel_reg = DA9062AA_VBUCK2_A,
672 		.desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
673 		.desc.linear_min_sel = 0,
674 		.sleep = REG_FIELD(DA9062AA_VBUCK2_A,
675 			__builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
676 			sizeof(unsigned int) * 8 -
677 			__builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
678 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
679 			__builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
680 			sizeof(unsigned int) * 8 -
681 			__builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
682 		.suspend_vsel_reg = DA9062AA_VBUCK2_B,
683 		.mode = REG_FIELD(DA9062AA_BUCK2_CFG,
684 			__builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
685 			sizeof(unsigned int) * 8 -
686 			__builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
687 		.suspend = REG_FIELD(DA9062AA_DVC_1,
688 			__builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
689 			sizeof(unsigned int) * 8 -
690 			__builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
691 	},
692 	{
693 		.desc.id = DA9062_ID_BUCK3,
694 		.desc.name = "DA9062 BUCK3",
695 		.desc.of_match = of_match_ptr("buck3"),
696 		.desc.regulators_node = of_match_ptr("regulators"),
697 		.desc.ops = &da9062_buck_ops,
698 		.desc.min_uV = (800) * 1000,
699 		.desc.uV_step = (20) * 1000,
700 		.desc.n_voltages = ((3340) - (800))/(20) + 1,
701 		.desc.curr_table = da9062_buck_b_limits,
702 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
703 		.desc.csel_reg = DA9062AA_BUCK_ILIM_A,
704 		.desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK,
705 		.desc.enable_reg = DA9062AA_BUCK3_CONT,
706 		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
707 		.desc.vsel_reg = DA9062AA_VBUCK3_A,
708 		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
709 		.desc.linear_min_sel = 0,
710 		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
711 			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
712 			sizeof(unsigned int) * 8 -
713 			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
714 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
715 			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
716 			sizeof(unsigned int) * 8 -
717 			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
718 		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
719 		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
720 			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
721 			sizeof(unsigned int) * 8 -
722 			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
723 		.suspend = REG_FIELD(DA9062AA_DVC_1,
724 			__builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
725 			sizeof(unsigned int) * 8 -
726 			__builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
727 	},
728 	{
729 		.desc.id = DA9062_ID_BUCK4,
730 		.desc.name = "DA9062 BUCK4",
731 		.desc.of_match = of_match_ptr("buck4"),
732 		.desc.regulators_node = of_match_ptr("regulators"),
733 		.desc.ops = &da9062_buck_ops,
734 		.desc.min_uV = (530) * 1000,
735 		.desc.uV_step = (10) * 1000,
736 		.desc.n_voltages = ((1800) - (530))/(10) + 1,
737 		.desc.curr_table = da9062_buck_a_limits,
738 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
739 		.desc.csel_reg = DA9062AA_BUCK_ILIM_B,
740 		.desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK,
741 		.desc.enable_reg = DA9062AA_BUCK4_CONT,
742 		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
743 		.desc.vsel_reg = DA9062AA_VBUCK4_A,
744 		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
745 		.desc.linear_min_sel = 0,
746 		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
747 			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
748 			sizeof(unsigned int) * 8 -
749 			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
750 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
751 			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
752 			sizeof(unsigned int) * 8 -
753 			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
754 		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
755 		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
756 			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
757 			sizeof(unsigned int) * 8 -
758 			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
759 		.suspend = REG_FIELD(DA9062AA_DVC_1,
760 			__builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
761 			sizeof(unsigned int) * 8 -
762 			__builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
763 	},
764 	{
765 		.desc.id = DA9062_ID_LDO1,
766 		.desc.name = "DA9062 LDO1",
767 		.desc.of_match = of_match_ptr("ldo1"),
768 		.desc.regulators_node = of_match_ptr("regulators"),
769 		.desc.ops = &da9062_ldo_ops,
770 		.desc.min_uV = (900) * 1000,
771 		.desc.uV_step = (50) * 1000,
772 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
773 		.desc.enable_reg = DA9062AA_LDO1_CONT,
774 		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
775 		.desc.vsel_reg = DA9062AA_VLDO1_A,
776 		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
777 		.desc.linear_min_sel = 0,
778 		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
779 			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
780 			sizeof(unsigned int) * 8 -
781 			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
782 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
783 			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
784 			sizeof(unsigned int) * 8 -
785 			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
786 		.suspend_vsel_reg = DA9062AA_VLDO1_B,
787 		.suspend = REG_FIELD(DA9062AA_DVC_1,
788 			__builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
789 			sizeof(unsigned int) * 8 -
790 			__builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
791 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
792 			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
793 			sizeof(unsigned int) * 8 -
794 			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
795 	},
796 	{
797 		.desc.id = DA9062_ID_LDO2,
798 		.desc.name = "DA9062 LDO2",
799 		.desc.of_match = of_match_ptr("ldo2"),
800 		.desc.regulators_node = of_match_ptr("regulators"),
801 		.desc.ops = &da9062_ldo_ops,
802 		.desc.min_uV = (900) * 1000,
803 		.desc.uV_step = (50) * 1000,
804 		.desc.n_voltages = ((3600) - (600))/(50) + 1,
805 		.desc.enable_reg = DA9062AA_LDO2_CONT,
806 		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
807 		.desc.vsel_reg = DA9062AA_VLDO2_A,
808 		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
809 		.desc.linear_min_sel = 0,
810 		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
811 			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
812 			sizeof(unsigned int) * 8 -
813 			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
814 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
815 			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
816 			sizeof(unsigned int) * 8 -
817 			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
818 		.suspend_vsel_reg = DA9062AA_VLDO2_B,
819 		.suspend = REG_FIELD(DA9062AA_DVC_1,
820 			__builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
821 			sizeof(unsigned int) * 8 -
822 			__builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
823 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
824 			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
825 			sizeof(unsigned int) * 8 -
826 			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
827 	},
828 	{
829 		.desc.id = DA9062_ID_LDO3,
830 		.desc.name = "DA9062 LDO3",
831 		.desc.of_match = of_match_ptr("ldo3"),
832 		.desc.regulators_node = of_match_ptr("regulators"),
833 		.desc.ops = &da9062_ldo_ops,
834 		.desc.min_uV = (900) * 1000,
835 		.desc.uV_step = (50) * 1000,
836 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
837 		.desc.enable_reg = DA9062AA_LDO3_CONT,
838 		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
839 		.desc.vsel_reg = DA9062AA_VLDO3_A,
840 		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
841 		.desc.linear_min_sel = 0,
842 		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
843 			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
844 			sizeof(unsigned int) * 8 -
845 			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
846 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
847 			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
848 			sizeof(unsigned int) * 8 -
849 			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
850 		.suspend_vsel_reg = DA9062AA_VLDO3_B,
851 		.suspend = REG_FIELD(DA9062AA_DVC_1,
852 			__builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
853 			sizeof(unsigned int) * 8 -
854 			__builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
855 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
856 			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
857 			sizeof(unsigned int) * 8 -
858 			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
859 	},
860 	{
861 		.desc.id = DA9062_ID_LDO4,
862 		.desc.name = "DA9062 LDO4",
863 		.desc.of_match = of_match_ptr("ldo4"),
864 		.desc.regulators_node = of_match_ptr("regulators"),
865 		.desc.ops = &da9062_ldo_ops,
866 		.desc.min_uV = (900) * 1000,
867 		.desc.uV_step = (50) * 1000,
868 		.desc.n_voltages = ((3600) - (900))/(50) + 1,
869 		.desc.enable_reg = DA9062AA_LDO4_CONT,
870 		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
871 		.desc.vsel_reg = DA9062AA_VLDO4_A,
872 		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
873 		.desc.linear_min_sel = 0,
874 		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
875 			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
876 			sizeof(unsigned int) * 8 -
877 			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
878 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
879 			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
880 			sizeof(unsigned int) * 8 -
881 			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
882 		.suspend_vsel_reg = DA9062AA_VLDO4_B,
883 		.suspend = REG_FIELD(DA9062AA_DVC_1,
884 			__builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
885 			sizeof(unsigned int) * 8 -
886 			__builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
887 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
888 			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
889 			sizeof(unsigned int) * 8 -
890 			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
891 	},
892 };
893 
894 /* Regulator interrupt handlers */
895 static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
896 {
897 	struct da9062_regulators *regulators = data;
898 	struct da9062 *hw = regulators->regulator[0].hw;
899 	struct da9062_regulator *regl;
900 	int handled = IRQ_NONE;
901 	int bits, i, ret;
902 
903 	ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
904 	if (ret < 0) {
905 		dev_err(hw->dev,
906 			"Failed to read LDO overcurrent indicator\n");
907 		goto ldo_lim_error;
908 	}
909 
910 	for (i = regulators->n_regulators - 1; i >= 0; i--) {
911 		regl = &regulators->regulator[i];
912 		if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
913 			continue;
914 
915 		if (BIT(regl->info->oc_event.lsb) & bits) {
916 			regulator_lock(regl->rdev);
917 			regulator_notifier_call_chain(regl->rdev,
918 					REGULATOR_EVENT_OVER_CURRENT, NULL);
919 			regulator_unlock(regl->rdev);
920 			handled = IRQ_HANDLED;
921 		}
922 	}
923 
924 ldo_lim_error:
925 	return handled;
926 }
927 
928 static int da9062_regulator_probe(struct platform_device *pdev)
929 {
930 	struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
931 	struct da9062_regulators *regulators;
932 	struct da9062_regulator *regl;
933 	struct regulator_config config = { };
934 	const struct da9062_regulator_info *rinfo;
935 	int irq, n, ret;
936 	int max_regulators;
937 
938 	switch (chip->chip_type) {
939 	case COMPAT_TYPE_DA9061:
940 		max_regulators = DA9061_MAX_REGULATORS;
941 		rinfo = local_da9061_regulator_info;
942 		break;
943 	case COMPAT_TYPE_DA9062:
944 		max_regulators = DA9062_MAX_REGULATORS;
945 		rinfo = local_da9062_regulator_info;
946 		break;
947 	default:
948 		dev_err(chip->dev, "Unrecognised chip type\n");
949 		return -ENODEV;
950 	}
951 
952 	/* Allocate memory required by usable regulators */
953 	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators, regulator,
954 				  max_regulators), GFP_KERNEL);
955 	if (!regulators)
956 		return -ENOMEM;
957 
958 	regulators->n_regulators = max_regulators;
959 	platform_set_drvdata(pdev, regulators);
960 
961 	n = 0;
962 	while (n < regulators->n_regulators) {
963 		/* Initialise regulator structure */
964 		regl = &regulators->regulator[n];
965 		regl->hw = chip;
966 		regl->info = &rinfo[n];
967 		regl->desc = regl->info->desc;
968 		regl->desc.type = REGULATOR_VOLTAGE;
969 		regl->desc.owner = THIS_MODULE;
970 
971 		if (regl->info->mode.reg) {
972 			regl->mode = devm_regmap_field_alloc(
973 					&pdev->dev,
974 					chip->regmap,
975 					regl->info->mode);
976 			if (IS_ERR(regl->mode))
977 				return PTR_ERR(regl->mode);
978 		}
979 
980 		if (regl->info->suspend.reg) {
981 			regl->suspend = devm_regmap_field_alloc(
982 					&pdev->dev,
983 					chip->regmap,
984 					regl->info->suspend);
985 			if (IS_ERR(regl->suspend))
986 				return PTR_ERR(regl->suspend);
987 		}
988 
989 		if (regl->info->sleep.reg) {
990 			regl->sleep = devm_regmap_field_alloc(
991 					&pdev->dev,
992 					chip->regmap,
993 					regl->info->sleep);
994 			if (IS_ERR(regl->sleep))
995 				return PTR_ERR(regl->sleep);
996 		}
997 
998 		if (regl->info->suspend_sleep.reg) {
999 			regl->suspend_sleep = devm_regmap_field_alloc(
1000 					&pdev->dev,
1001 					chip->regmap,
1002 					regl->info->suspend_sleep);
1003 			if (IS_ERR(regl->suspend_sleep))
1004 				return PTR_ERR(regl->suspend_sleep);
1005 		}
1006 
1007 		/* Register regulator */
1008 		memset(&config, 0, sizeof(config));
1009 		config.dev = chip->dev;
1010 		config.driver_data = regl;
1011 		config.regmap = chip->regmap;
1012 
1013 		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1014 						     &config);
1015 		if (IS_ERR(regl->rdev)) {
1016 			dev_err(&pdev->dev,
1017 				"Failed to register %s regulator\n",
1018 				regl->desc.name);
1019 			return PTR_ERR(regl->rdev);
1020 		}
1021 
1022 		n++;
1023 	}
1024 
1025 	/* LDOs overcurrent event support */
1026 	irq = platform_get_irq_byname(pdev, "LDO_LIM");
1027 	if (irq < 0) {
1028 		dev_err(&pdev->dev, "Failed to get IRQ.\n");
1029 		return irq;
1030 	}
1031 	regulators->irq_ldo_lim = irq;
1032 
1033 	ret = devm_request_threaded_irq(&pdev->dev, irq,
1034 					NULL, da9062_ldo_lim_event,
1035 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1036 					"LDO_LIM", regulators);
1037 	if (ret) {
1038 		dev_warn(&pdev->dev,
1039 			 "Failed to request LDO_LIM IRQ.\n");
1040 		regulators->irq_ldo_lim = -ENXIO;
1041 	}
1042 
1043 	return 0;
1044 }
1045 
1046 static struct platform_driver da9062_regulator_driver = {
1047 	.driver = {
1048 		.name = "da9062-regulators",
1049 	},
1050 	.probe = da9062_regulator_probe,
1051 };
1052 
1053 static int __init da9062_regulator_init(void)
1054 {
1055 	return platform_driver_register(&da9062_regulator_driver);
1056 }
1057 subsys_initcall(da9062_regulator_init);
1058 
1059 static void __exit da9062_regulator_cleanup(void)
1060 {
1061 	platform_driver_unregister(&da9062_regulator_driver);
1062 }
1063 module_exit(da9062_regulator_cleanup);
1064 
1065 /* Module information */
1066 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
1067 MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062 and DA9061");
1068 MODULE_LICENSE("GPL");
1069 MODULE_ALIAS("platform:da9062-regulators");
1070