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 				+ DA9062AA_VLDO_A_MIN_SEL,
498 		.desc.enable_reg = DA9062AA_LDO1_CONT,
499 		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
500 		.desc.vsel_reg = DA9062AA_VLDO1_A,
501 		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
502 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
503 		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
504 			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
505 			sizeof(unsigned int) * 8 -
506 			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
507 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
508 			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
509 			sizeof(unsigned int) * 8 -
510 			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
511 		.suspend_vsel_reg = DA9062AA_VLDO1_B,
512 		.suspend = REG_FIELD(DA9062AA_DVC_1,
513 			__builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
514 			sizeof(unsigned int) * 8 -
515 			__builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
516 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
517 			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
518 			sizeof(unsigned int) * 8 -
519 			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
520 	},
521 	{
522 		.desc.id = DA9061_ID_LDO2,
523 		.desc.name = "DA9061 LDO2",
524 		.desc.of_match = of_match_ptr("ldo2"),
525 		.desc.regulators_node = of_match_ptr("regulators"),
526 		.desc.ops = &da9062_ldo_ops,
527 		.desc.min_uV = (900) * 1000,
528 		.desc.uV_step = (50) * 1000,
529 		.desc.n_voltages = ((3600) - (900))/(50) + 1
530 				+ DA9062AA_VLDO_A_MIN_SEL,
531 		.desc.enable_reg = DA9062AA_LDO2_CONT,
532 		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
533 		.desc.vsel_reg = DA9062AA_VLDO2_A,
534 		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
535 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
536 		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
537 			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
538 			sizeof(unsigned int) * 8 -
539 			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
540 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
541 			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
542 			sizeof(unsigned int) * 8 -
543 			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
544 		.suspend_vsel_reg = DA9062AA_VLDO2_B,
545 		.suspend = REG_FIELD(DA9062AA_DVC_1,
546 			__builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
547 			sizeof(unsigned int) * 8 -
548 			__builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
549 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
550 			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
551 			sizeof(unsigned int) * 8 -
552 			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
553 	},
554 	{
555 		.desc.id = DA9061_ID_LDO3,
556 		.desc.name = "DA9061 LDO3",
557 		.desc.of_match = of_match_ptr("ldo3"),
558 		.desc.regulators_node = of_match_ptr("regulators"),
559 		.desc.ops = &da9062_ldo_ops,
560 		.desc.min_uV = (900) * 1000,
561 		.desc.uV_step = (50) * 1000,
562 		.desc.n_voltages = ((3600) - (900))/(50) + 1
563 				+ DA9062AA_VLDO_A_MIN_SEL,
564 		.desc.enable_reg = DA9062AA_LDO3_CONT,
565 		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
566 		.desc.vsel_reg = DA9062AA_VLDO3_A,
567 		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
568 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
569 		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
570 			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
571 			sizeof(unsigned int) * 8 -
572 			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
573 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
574 			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
575 			sizeof(unsigned int) * 8 -
576 			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
577 		.suspend_vsel_reg = DA9062AA_VLDO3_B,
578 		.suspend = REG_FIELD(DA9062AA_DVC_1,
579 			__builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
580 			sizeof(unsigned int) * 8 -
581 			__builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
582 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
583 			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
584 			sizeof(unsigned int) * 8 -
585 			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
586 	},
587 	{
588 		.desc.id = DA9061_ID_LDO4,
589 		.desc.name = "DA9061 LDO4",
590 		.desc.of_match = of_match_ptr("ldo4"),
591 		.desc.regulators_node = of_match_ptr("regulators"),
592 		.desc.ops = &da9062_ldo_ops,
593 		.desc.min_uV = (900) * 1000,
594 		.desc.uV_step = (50) * 1000,
595 		.desc.n_voltages = ((3600) - (900))/(50) + 1
596 				+ DA9062AA_VLDO_A_MIN_SEL,
597 		.desc.enable_reg = DA9062AA_LDO4_CONT,
598 		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
599 		.desc.vsel_reg = DA9062AA_VLDO4_A,
600 		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
601 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
602 		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
603 			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
604 			sizeof(unsigned int) * 8 -
605 			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
606 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
607 			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
608 			sizeof(unsigned int) * 8 -
609 			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
610 		.suspend_vsel_reg = DA9062AA_VLDO4_B,
611 		.suspend = REG_FIELD(DA9062AA_DVC_1,
612 			__builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
613 			sizeof(unsigned int) * 8 -
614 			__builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
615 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
616 			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
617 			sizeof(unsigned int) * 8 -
618 			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
619 	},
620 };
621 
622 /* DA9062 Regulator information */
623 static const struct da9062_regulator_info local_da9062_regulator_info[] = {
624 	{
625 		.desc.id = DA9062_ID_BUCK1,
626 		.desc.name = "DA9062 BUCK1",
627 		.desc.of_match = of_match_ptr("buck1"),
628 		.desc.regulators_node = of_match_ptr("regulators"),
629 		.desc.ops = &da9062_buck_ops,
630 		.desc.min_uV = (300) * 1000,
631 		.desc.uV_step = (10) * 1000,
632 		.desc.n_voltages = ((1570) - (300))/(10) + 1,
633 		.desc.curr_table = da9062_buck_a_limits,
634 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
635 		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
636 		.desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK,
637 		.desc.enable_reg = DA9062AA_BUCK1_CONT,
638 		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
639 		.desc.vsel_reg = DA9062AA_VBUCK1_A,
640 		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
641 		.desc.linear_min_sel = 0,
642 		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
643 			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
644 			sizeof(unsigned int) * 8 -
645 			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
646 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
647 			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
648 			sizeof(unsigned int) * 8 -
649 			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
650 		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
651 		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
652 			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
653 			sizeof(unsigned int) * 8 -
654 			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
655 		.suspend = REG_FIELD(DA9062AA_DVC_1,
656 			__builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
657 			sizeof(unsigned int) * 8 -
658 			__builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
659 	},
660 	{
661 		.desc.id = DA9062_ID_BUCK2,
662 		.desc.name = "DA9062 BUCK2",
663 		.desc.of_match = of_match_ptr("buck2"),
664 		.desc.regulators_node = of_match_ptr("regulators"),
665 		.desc.ops = &da9062_buck_ops,
666 		.desc.min_uV = (300) * 1000,
667 		.desc.uV_step = (10) * 1000,
668 		.desc.n_voltages = ((1570) - (300))/(10) + 1,
669 		.desc.curr_table = da9062_buck_a_limits,
670 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
671 		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
672 		.desc.csel_mask = DA9062AA_BUCK2_ILIM_MASK,
673 		.desc.enable_reg = DA9062AA_BUCK2_CONT,
674 		.desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
675 		.desc.vsel_reg = DA9062AA_VBUCK2_A,
676 		.desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
677 		.desc.linear_min_sel = 0,
678 		.sleep = REG_FIELD(DA9062AA_VBUCK2_A,
679 			__builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
680 			sizeof(unsigned int) * 8 -
681 			__builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
682 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
683 			__builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
684 			sizeof(unsigned int) * 8 -
685 			__builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
686 		.suspend_vsel_reg = DA9062AA_VBUCK2_B,
687 		.mode = REG_FIELD(DA9062AA_BUCK2_CFG,
688 			__builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
689 			sizeof(unsigned int) * 8 -
690 			__builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
691 		.suspend = REG_FIELD(DA9062AA_DVC_1,
692 			__builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
693 			sizeof(unsigned int) * 8 -
694 			__builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
695 	},
696 	{
697 		.desc.id = DA9062_ID_BUCK3,
698 		.desc.name = "DA9062 BUCK3",
699 		.desc.of_match = of_match_ptr("buck3"),
700 		.desc.regulators_node = of_match_ptr("regulators"),
701 		.desc.ops = &da9062_buck_ops,
702 		.desc.min_uV = (800) * 1000,
703 		.desc.uV_step = (20) * 1000,
704 		.desc.n_voltages = ((3340) - (800))/(20) + 1,
705 		.desc.curr_table = da9062_buck_b_limits,
706 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
707 		.desc.csel_reg = DA9062AA_BUCK_ILIM_A,
708 		.desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK,
709 		.desc.enable_reg = DA9062AA_BUCK3_CONT,
710 		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
711 		.desc.vsel_reg = DA9062AA_VBUCK3_A,
712 		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
713 		.desc.linear_min_sel = 0,
714 		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
715 			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
716 			sizeof(unsigned int) * 8 -
717 			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
718 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
719 			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
720 			sizeof(unsigned int) * 8 -
721 			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
722 		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
723 		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
724 			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
725 			sizeof(unsigned int) * 8 -
726 			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
727 		.suspend = REG_FIELD(DA9062AA_DVC_1,
728 			__builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
729 			sizeof(unsigned int) * 8 -
730 			__builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
731 	},
732 	{
733 		.desc.id = DA9062_ID_BUCK4,
734 		.desc.name = "DA9062 BUCK4",
735 		.desc.of_match = of_match_ptr("buck4"),
736 		.desc.regulators_node = of_match_ptr("regulators"),
737 		.desc.ops = &da9062_buck_ops,
738 		.desc.min_uV = (530) * 1000,
739 		.desc.uV_step = (10) * 1000,
740 		.desc.n_voltages = ((1800) - (530))/(10) + 1,
741 		.desc.curr_table = da9062_buck_a_limits,
742 		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
743 		.desc.csel_reg = DA9062AA_BUCK_ILIM_B,
744 		.desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK,
745 		.desc.enable_reg = DA9062AA_BUCK4_CONT,
746 		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
747 		.desc.vsel_reg = DA9062AA_VBUCK4_A,
748 		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
749 		.desc.linear_min_sel = 0,
750 		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
751 			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
752 			sizeof(unsigned int) * 8 -
753 			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
754 		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
755 			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
756 			sizeof(unsigned int) * 8 -
757 			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
758 		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
759 		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
760 			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
761 			sizeof(unsigned int) * 8 -
762 			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
763 		.suspend = REG_FIELD(DA9062AA_DVC_1,
764 			__builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
765 			sizeof(unsigned int) * 8 -
766 			__builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
767 	},
768 	{
769 		.desc.id = DA9062_ID_LDO1,
770 		.desc.name = "DA9062 LDO1",
771 		.desc.of_match = of_match_ptr("ldo1"),
772 		.desc.regulators_node = of_match_ptr("regulators"),
773 		.desc.ops = &da9062_ldo_ops,
774 		.desc.min_uV = (900) * 1000,
775 		.desc.uV_step = (50) * 1000,
776 		.desc.n_voltages = ((3600) - (900))/(50) + 1
777 				+ DA9062AA_VLDO_A_MIN_SEL,
778 		.desc.enable_reg = DA9062AA_LDO1_CONT,
779 		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
780 		.desc.vsel_reg = DA9062AA_VLDO1_A,
781 		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
782 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
783 		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
784 			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
785 			sizeof(unsigned int) * 8 -
786 			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
787 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
788 			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
789 			sizeof(unsigned int) * 8 -
790 			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
791 		.suspend_vsel_reg = DA9062AA_VLDO1_B,
792 		.suspend = REG_FIELD(DA9062AA_DVC_1,
793 			__builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
794 			sizeof(unsigned int) * 8 -
795 			__builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
796 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
797 			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
798 			sizeof(unsigned int) * 8 -
799 			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
800 	},
801 	{
802 		.desc.id = DA9062_ID_LDO2,
803 		.desc.name = "DA9062 LDO2",
804 		.desc.of_match = of_match_ptr("ldo2"),
805 		.desc.regulators_node = of_match_ptr("regulators"),
806 		.desc.ops = &da9062_ldo_ops,
807 		.desc.min_uV = (900) * 1000,
808 		.desc.uV_step = (50) * 1000,
809 		.desc.n_voltages = ((3600) - (900))/(50) + 1
810 				+ DA9062AA_VLDO_A_MIN_SEL,
811 		.desc.enable_reg = DA9062AA_LDO2_CONT,
812 		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
813 		.desc.vsel_reg = DA9062AA_VLDO2_A,
814 		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
815 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
816 		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
817 			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
818 			sizeof(unsigned int) * 8 -
819 			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
820 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
821 			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
822 			sizeof(unsigned int) * 8 -
823 			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
824 		.suspend_vsel_reg = DA9062AA_VLDO2_B,
825 		.suspend = REG_FIELD(DA9062AA_DVC_1,
826 			__builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
827 			sizeof(unsigned int) * 8 -
828 			__builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
829 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
830 			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
831 			sizeof(unsigned int) * 8 -
832 			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
833 	},
834 	{
835 		.desc.id = DA9062_ID_LDO3,
836 		.desc.name = "DA9062 LDO3",
837 		.desc.of_match = of_match_ptr("ldo3"),
838 		.desc.regulators_node = of_match_ptr("regulators"),
839 		.desc.ops = &da9062_ldo_ops,
840 		.desc.min_uV = (900) * 1000,
841 		.desc.uV_step = (50) * 1000,
842 		.desc.n_voltages = ((3600) - (900))/(50) + 1
843 				+ DA9062AA_VLDO_A_MIN_SEL,
844 		.desc.enable_reg = DA9062AA_LDO3_CONT,
845 		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
846 		.desc.vsel_reg = DA9062AA_VLDO3_A,
847 		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
848 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
849 		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
850 			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
851 			sizeof(unsigned int) * 8 -
852 			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
853 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
854 			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
855 			sizeof(unsigned int) * 8 -
856 			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
857 		.suspend_vsel_reg = DA9062AA_VLDO3_B,
858 		.suspend = REG_FIELD(DA9062AA_DVC_1,
859 			__builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
860 			sizeof(unsigned int) * 8 -
861 			__builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
862 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
863 			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
864 			sizeof(unsigned int) * 8 -
865 			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
866 	},
867 	{
868 		.desc.id = DA9062_ID_LDO4,
869 		.desc.name = "DA9062 LDO4",
870 		.desc.of_match = of_match_ptr("ldo4"),
871 		.desc.regulators_node = of_match_ptr("regulators"),
872 		.desc.ops = &da9062_ldo_ops,
873 		.desc.min_uV = (900) * 1000,
874 		.desc.uV_step = (50) * 1000,
875 		.desc.n_voltages = ((3600) - (900))/(50) + 1
876 				+ DA9062AA_VLDO_A_MIN_SEL,
877 		.desc.enable_reg = DA9062AA_LDO4_CONT,
878 		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
879 		.desc.vsel_reg = DA9062AA_VLDO4_A,
880 		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
881 		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
882 		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
883 			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
884 			sizeof(unsigned int) * 8 -
885 			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
886 		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
887 			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
888 			sizeof(unsigned int) * 8 -
889 			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
890 		.suspend_vsel_reg = DA9062AA_VLDO4_B,
891 		.suspend = REG_FIELD(DA9062AA_DVC_1,
892 			__builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
893 			sizeof(unsigned int) * 8 -
894 			__builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
895 		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
896 			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
897 			sizeof(unsigned int) * 8 -
898 			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
899 	},
900 };
901 
902 /* Regulator interrupt handlers */
903 static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
904 {
905 	struct da9062_regulators *regulators = data;
906 	struct da9062 *hw = regulators->regulator[0].hw;
907 	struct da9062_regulator *regl;
908 	int handled = IRQ_NONE;
909 	int bits, i, ret;
910 
911 	ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
912 	if (ret < 0) {
913 		dev_err(hw->dev,
914 			"Failed to read LDO overcurrent indicator\n");
915 		goto ldo_lim_error;
916 	}
917 
918 	for (i = regulators->n_regulators - 1; i >= 0; i--) {
919 		regl = &regulators->regulator[i];
920 		if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
921 			continue;
922 
923 		if (BIT(regl->info->oc_event.lsb) & bits) {
924 			regulator_lock(regl->rdev);
925 			regulator_notifier_call_chain(regl->rdev,
926 					REGULATOR_EVENT_OVER_CURRENT, NULL);
927 			regulator_unlock(regl->rdev);
928 			handled = IRQ_HANDLED;
929 		}
930 	}
931 
932 ldo_lim_error:
933 	return handled;
934 }
935 
936 static int da9062_regulator_probe(struct platform_device *pdev)
937 {
938 	struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
939 	struct da9062_regulators *regulators;
940 	struct da9062_regulator *regl;
941 	struct regulator_config config = { };
942 	const struct da9062_regulator_info *rinfo;
943 	int irq, n, ret;
944 	int max_regulators;
945 
946 	switch (chip->chip_type) {
947 	case COMPAT_TYPE_DA9061:
948 		max_regulators = DA9061_MAX_REGULATORS;
949 		rinfo = local_da9061_regulator_info;
950 		break;
951 	case COMPAT_TYPE_DA9062:
952 		max_regulators = DA9062_MAX_REGULATORS;
953 		rinfo = local_da9062_regulator_info;
954 		break;
955 	default:
956 		dev_err(chip->dev, "Unrecognised chip type\n");
957 		return -ENODEV;
958 	}
959 
960 	/* Allocate memory required by usable regulators */
961 	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators, regulator,
962 				  max_regulators), GFP_KERNEL);
963 	if (!regulators)
964 		return -ENOMEM;
965 
966 	regulators->n_regulators = max_regulators;
967 	platform_set_drvdata(pdev, regulators);
968 
969 	n = 0;
970 	while (n < regulators->n_regulators) {
971 		/* Initialise regulator structure */
972 		regl = &regulators->regulator[n];
973 		regl->hw = chip;
974 		regl->info = &rinfo[n];
975 		regl->desc = regl->info->desc;
976 		regl->desc.type = REGULATOR_VOLTAGE;
977 		regl->desc.owner = THIS_MODULE;
978 
979 		if (regl->info->mode.reg) {
980 			regl->mode = devm_regmap_field_alloc(
981 					&pdev->dev,
982 					chip->regmap,
983 					regl->info->mode);
984 			if (IS_ERR(regl->mode))
985 				return PTR_ERR(regl->mode);
986 		}
987 
988 		if (regl->info->suspend.reg) {
989 			regl->suspend = devm_regmap_field_alloc(
990 					&pdev->dev,
991 					chip->regmap,
992 					regl->info->suspend);
993 			if (IS_ERR(regl->suspend))
994 				return PTR_ERR(regl->suspend);
995 		}
996 
997 		if (regl->info->sleep.reg) {
998 			regl->sleep = devm_regmap_field_alloc(
999 					&pdev->dev,
1000 					chip->regmap,
1001 					regl->info->sleep);
1002 			if (IS_ERR(regl->sleep))
1003 				return PTR_ERR(regl->sleep);
1004 		}
1005 
1006 		if (regl->info->suspend_sleep.reg) {
1007 			regl->suspend_sleep = devm_regmap_field_alloc(
1008 					&pdev->dev,
1009 					chip->regmap,
1010 					regl->info->suspend_sleep);
1011 			if (IS_ERR(regl->suspend_sleep))
1012 				return PTR_ERR(regl->suspend_sleep);
1013 		}
1014 
1015 		/* Register regulator */
1016 		memset(&config, 0, sizeof(config));
1017 		config.dev = chip->dev;
1018 		config.driver_data = regl;
1019 		config.regmap = chip->regmap;
1020 
1021 		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1022 						     &config);
1023 		if (IS_ERR(regl->rdev)) {
1024 			dev_err(&pdev->dev,
1025 				"Failed to register %s regulator\n",
1026 				regl->desc.name);
1027 			return PTR_ERR(regl->rdev);
1028 		}
1029 
1030 		n++;
1031 	}
1032 
1033 	/* LDOs overcurrent event support */
1034 	irq = platform_get_irq_byname(pdev, "LDO_LIM");
1035 	if (irq < 0) {
1036 		dev_err(&pdev->dev, "Failed to get IRQ.\n");
1037 		return irq;
1038 	}
1039 	regulators->irq_ldo_lim = irq;
1040 
1041 	ret = devm_request_threaded_irq(&pdev->dev, irq,
1042 					NULL, da9062_ldo_lim_event,
1043 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1044 					"LDO_LIM", regulators);
1045 	if (ret) {
1046 		dev_warn(&pdev->dev,
1047 			 "Failed to request LDO_LIM IRQ.\n");
1048 		regulators->irq_ldo_lim = -ENXIO;
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 static struct platform_driver da9062_regulator_driver = {
1055 	.driver = {
1056 		.name = "da9062-regulators",
1057 	},
1058 	.probe = da9062_regulator_probe,
1059 };
1060 
1061 static int __init da9062_regulator_init(void)
1062 {
1063 	return platform_driver_register(&da9062_regulator_driver);
1064 }
1065 subsys_initcall(da9062_regulator_init);
1066 
1067 static void __exit da9062_regulator_cleanup(void)
1068 {
1069 	platform_driver_unregister(&da9062_regulator_driver);
1070 }
1071 module_exit(da9062_regulator_cleanup);
1072 
1073 /* Module information */
1074 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
1075 MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062 and DA9061");
1076 MODULE_LICENSE("GPL");
1077 MODULE_ALIAS("platform:da9062-regulators");
1078