xref: /linux/drivers/regulator/wm831x-dcdc.c (revision c39f2d9db0fd81ea20bb5cce9b3f082ca63753e2)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // wm831x-dcdc.c  --  DC-DC buck converter driver for the WM831x series
4 //
5 // Copyright 2009 Wolfson Microelectronics PLC.
6 //
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
20 
21 #include <linux/mfd/wm831x/core.h>
22 #include <linux/mfd/wm831x/regulator.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 
25 #define WM831X_BUCKV_MAX_SELECTOR 0x68
26 #define WM831X_BUCKP_MAX_SELECTOR 0x66
27 
28 #define WM831X_DCDC_MODE_FAST    0
29 #define WM831X_DCDC_MODE_NORMAL  1
30 #define WM831X_DCDC_MODE_IDLE    2
31 #define WM831X_DCDC_MODE_STANDBY 3
32 
33 #define WM831X_DCDC_MAX_NAME 9
34 
35 /* Register offsets in control block */
36 #define WM831X_DCDC_CONTROL_1     0
37 #define WM831X_DCDC_CONTROL_2     1
38 #define WM831X_DCDC_ON_CONFIG     2
39 #define WM831X_DCDC_SLEEP_CONTROL 3
40 #define WM831X_DCDC_DVS_CONTROL   4
41 
42 /*
43  * Shared
44  */
45 
46 struct wm831x_dcdc {
47 	char name[WM831X_DCDC_MAX_NAME];
48 	char supply_name[WM831X_DCDC_MAX_NAME];
49 	struct regulator_desc desc;
50 	int base;
51 	struct wm831x *wm831x;
52 	struct regulator_dev *regulator;
53 	int dvs_gpio;
54 	int dvs_gpio_state;
55 	int on_vsel;
56 	int dvs_vsel;
57 };
58 
59 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
60 
61 {
62 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
63 	struct wm831x *wm831x = dcdc->wm831x;
64 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
65 	int val;
66 
67 	val = wm831x_reg_read(wm831x, reg);
68 	if (val < 0)
69 		return val;
70 
71 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
72 
73 	switch (val) {
74 	case WM831X_DCDC_MODE_FAST:
75 		return REGULATOR_MODE_FAST;
76 	case WM831X_DCDC_MODE_NORMAL:
77 		return REGULATOR_MODE_NORMAL;
78 	case WM831X_DCDC_MODE_STANDBY:
79 		return REGULATOR_MODE_STANDBY;
80 	case WM831X_DCDC_MODE_IDLE:
81 		return REGULATOR_MODE_IDLE;
82 	default:
83 		BUG();
84 		return -EINVAL;
85 	}
86 }
87 
88 static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
89 				    unsigned int mode)
90 {
91 	int val;
92 
93 	switch (mode) {
94 	case REGULATOR_MODE_FAST:
95 		val = WM831X_DCDC_MODE_FAST;
96 		break;
97 	case REGULATOR_MODE_NORMAL:
98 		val = WM831X_DCDC_MODE_NORMAL;
99 		break;
100 	case REGULATOR_MODE_STANDBY:
101 		val = WM831X_DCDC_MODE_STANDBY;
102 		break;
103 	case REGULATOR_MODE_IDLE:
104 		val = WM831X_DCDC_MODE_IDLE;
105 		break;
106 	default:
107 		return -EINVAL;
108 	}
109 
110 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
111 			       val << WM831X_DC1_ON_MODE_SHIFT);
112 }
113 
114 static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
115 {
116 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
117 	struct wm831x *wm831x = dcdc->wm831x;
118 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
119 
120 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
121 }
122 
123 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
124 					unsigned int mode)
125 {
126 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
127 	struct wm831x *wm831x = dcdc->wm831x;
128 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
129 
130 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
131 }
132 
133 static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
134 {
135 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
136 	struct wm831x *wm831x = dcdc->wm831x;
137 	int ret;
138 
139 	/* First, check for errors */
140 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
141 	if (ret < 0)
142 		return ret;
143 
144 	if (ret & (1 << rdev_get_id(rdev))) {
145 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
146 			rdev_get_id(rdev) + 1);
147 		return REGULATOR_STATUS_ERROR;
148 	}
149 
150 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
151 	if (rdev_get_id(rdev) < 2) {
152 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
153 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
154 				rdev_get_id(rdev) + 1);
155 			return REGULATOR_STATUS_ERROR;
156 		}
157 
158 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
159 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
160 				rdev_get_id(rdev) + 1);
161 			return REGULATOR_STATUS_ERROR;
162 		}
163 	}
164 
165 	/* Is the regulator on? */
166 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
167 	if (ret < 0)
168 		return ret;
169 	if (!(ret & (1 << rdev_get_id(rdev))))
170 		return REGULATOR_STATUS_OFF;
171 
172 	/* TODO: When we handle hardware control modes so we can report the
173 	 * current mode. */
174 	return REGULATOR_STATUS_ON;
175 }
176 
177 static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
178 {
179 	struct wm831x_dcdc *dcdc = data;
180 
181 	regulator_lock(dcdc->regulator);
182 	regulator_notifier_call_chain(dcdc->regulator,
183 				      REGULATOR_EVENT_UNDER_VOLTAGE,
184 				      NULL);
185 	regulator_unlock(dcdc->regulator);
186 
187 	return IRQ_HANDLED;
188 }
189 
190 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
191 {
192 	struct wm831x_dcdc *dcdc = data;
193 
194 	regulator_lock(dcdc->regulator);
195 	regulator_notifier_call_chain(dcdc->regulator,
196 				      REGULATOR_EVENT_OVER_CURRENT,
197 				      NULL);
198 	regulator_unlock(dcdc->regulator);
199 
200 	return IRQ_HANDLED;
201 }
202 
203 /*
204  * BUCKV specifics
205  */
206 
207 static const struct regulator_linear_range wm831x_buckv_ranges[] = {
208 	REGULATOR_LINEAR_RANGE(600000, 0, 0x7, 0),
209 	REGULATOR_LINEAR_RANGE(600000, 0x8, 0x68, 12500),
210 };
211 
212 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
213 {
214 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
215 
216 	if (state == dcdc->dvs_gpio_state)
217 		return 0;
218 
219 	dcdc->dvs_gpio_state = state;
220 	gpio_set_value(dcdc->dvs_gpio, state);
221 
222 	/* Should wait for DVS state change to be asserted if we have
223 	 * a GPIO for it, for now assume the device is configured
224 	 * for the fastest possible transition.
225 	 */
226 
227 	return 0;
228 }
229 
230 static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev,
231 					unsigned vsel)
232 {
233 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
234 	struct wm831x *wm831x = dcdc->wm831x;
235 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
236 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
237 	int ret;
238 
239 	/* If this value is already set then do a GPIO update if we can */
240 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
241 		return wm831x_buckv_set_dvs(rdev, 0);
242 
243 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
244 		return wm831x_buckv_set_dvs(rdev, 1);
245 
246 	/* Always set the ON status to the minimum voltage */
247 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
248 	if (ret < 0)
249 		return ret;
250 	dcdc->on_vsel = vsel;
251 
252 	if (!dcdc->dvs_gpio)
253 		return ret;
254 
255 	/* Kick the voltage transition now */
256 	ret = wm831x_buckv_set_dvs(rdev, 0);
257 	if (ret < 0)
258 		return ret;
259 
260 	/*
261 	 * If this VSEL is higher than the last one we've seen then
262 	 * remember it as the DVS VSEL.  This is optimised for CPUfreq
263 	 * usage where we want to get to the highest voltage very
264 	 * quickly.
265 	 */
266 	if (vsel > dcdc->dvs_vsel) {
267 		ret = wm831x_set_bits(wm831x, dvs_reg,
268 				      WM831X_DC1_DVS_VSEL_MASK,
269 				      vsel);
270 		if (ret == 0)
271 			dcdc->dvs_vsel = vsel;
272 		else
273 			dev_warn(wm831x->dev,
274 				 "Failed to set DCDC DVS VSEL: %d\n", ret);
275 	}
276 
277 	return 0;
278 }
279 
280 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
281 					    int uV)
282 {
283 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
284 	struct wm831x *wm831x = dcdc->wm831x;
285 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
286 	int vsel;
287 
288 	vsel = regulator_map_voltage_linear_range(rdev, uV, uV);
289 	if (vsel < 0)
290 		return vsel;
291 
292 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
293 }
294 
295 static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
296 {
297 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
298 
299 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
300 		return dcdc->dvs_vsel;
301 	else
302 		return dcdc->on_vsel;
303 }
304 
305 /* Current limit options */
306 static const unsigned int wm831x_dcdc_ilim[] = {
307 	125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000
308 };
309 
310 static const struct regulator_ops wm831x_buckv_ops = {
311 	.set_voltage_sel = wm831x_buckv_set_voltage_sel,
312 	.get_voltage_sel = wm831x_buckv_get_voltage_sel,
313 	.list_voltage = regulator_list_voltage_linear_range,
314 	.map_voltage = regulator_map_voltage_linear_range,
315 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
316 	.set_current_limit = regulator_set_current_limit_regmap,
317 	.get_current_limit = regulator_get_current_limit_regmap,
318 
319 	.is_enabled = regulator_is_enabled_regmap,
320 	.enable = regulator_enable_regmap,
321 	.disable = regulator_disable_regmap,
322 	.get_status = wm831x_dcdc_get_status,
323 	.get_mode = wm831x_dcdc_get_mode,
324 	.set_mode = wm831x_dcdc_set_mode,
325 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
326 };
327 
328 /*
329  * Set up DVS control.  We just log errors since we can still run
330  * (with reduced performance) if we fail.
331  */
332 static void wm831x_buckv_dvs_init(struct platform_device *pdev,
333 				  struct wm831x_dcdc *dcdc,
334 				  struct wm831x_buckv_pdata *pdata)
335 {
336 	struct wm831x *wm831x = dcdc->wm831x;
337 	int ret;
338 	u16 ctrl;
339 
340 	if (!pdata || !pdata->dvs_gpio)
341 		return;
342 
343 	/* gpiolib won't let us read the GPIO status so pick the higher
344 	 * of the two existing voltages so we take it as platform data.
345 	 */
346 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
347 
348 	ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio,
349 				    dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0,
350 				    "DCDC DVS");
351 	if (ret < 0) {
352 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
353 			dcdc->name, ret);
354 		return;
355 	}
356 
357 	dcdc->dvs_gpio = pdata->dvs_gpio;
358 
359 	switch (pdata->dvs_control_src) {
360 	case 1:
361 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
362 		break;
363 	case 2:
364 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
365 		break;
366 	default:
367 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
368 			pdata->dvs_control_src, dcdc->name);
369 		return;
370 	}
371 
372 	/* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
373 	 * to make bootstrapping a bit smoother.
374 	 */
375 	if (!dcdc->dvs_vsel) {
376 		ret = wm831x_set_bits(wm831x,
377 				      dcdc->base + WM831X_DCDC_DVS_CONTROL,
378 				      WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
379 		if (ret == 0)
380 			dcdc->dvs_vsel = dcdc->on_vsel;
381 		else
382 			dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
383 				 ret);
384 	}
385 
386 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
387 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
388 	if (ret < 0) {
389 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
390 			dcdc->name, ret);
391 	}
392 }
393 
394 static int wm831x_buckv_probe(struct platform_device *pdev)
395 {
396 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
397 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
398 	struct regulator_config config = { };
399 	int id;
400 	struct wm831x_dcdc *dcdc;
401 	struct resource *res;
402 	int ret, irq;
403 
404 	if (pdata && pdata->wm831x_num)
405 		id = (pdata->wm831x_num * 10) + 1;
406 	else
407 		id = 0;
408 	id = pdev->id - id;
409 
410 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
411 
412 	dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
413 			    GFP_KERNEL);
414 	if (!dcdc)
415 		return -ENOMEM;
416 
417 	dcdc->wm831x = wm831x;
418 
419 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
420 	if (res == NULL) {
421 		dev_err(&pdev->dev, "No REG resource\n");
422 		ret = -EINVAL;
423 		goto err;
424 	}
425 	dcdc->base = res->start;
426 
427 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
428 	dcdc->desc.name = dcdc->name;
429 
430 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
431 		 "DC%dVDD", id + 1);
432 	dcdc->desc.supply_name = dcdc->supply_name;
433 
434 	dcdc->desc.id = id;
435 	dcdc->desc.type = REGULATOR_VOLTAGE;
436 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
437 	dcdc->desc.linear_ranges = wm831x_buckv_ranges;
438 	dcdc->desc.n_linear_ranges = ARRAY_SIZE(wm831x_buckv_ranges);
439 	dcdc->desc.ops = &wm831x_buckv_ops;
440 	dcdc->desc.owner = THIS_MODULE;
441 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
442 	dcdc->desc.enable_mask = 1 << id;
443 	dcdc->desc.csel_reg = dcdc->base + WM831X_DCDC_CONTROL_2;
444 	dcdc->desc.csel_mask = WM831X_DC1_HC_THR_MASK;
445 	dcdc->desc.n_current_limits = ARRAY_SIZE(wm831x_dcdc_ilim);
446 	dcdc->desc.curr_table = wm831x_dcdc_ilim;
447 
448 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
449 	if (ret < 0) {
450 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
451 		goto err;
452 	}
453 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
454 
455 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
456 	if (ret < 0) {
457 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
458 		goto err;
459 	}
460 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
461 
462 	if (pdata && pdata->dcdc[id])
463 		wm831x_buckv_dvs_init(pdev, dcdc,
464 				      pdata->dcdc[id]->driver_data);
465 
466 	config.dev = pdev->dev.parent;
467 	if (pdata)
468 		config.init_data = pdata->dcdc[id];
469 	config.driver_data = dcdc;
470 	config.regmap = wm831x->regmap;
471 
472 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
473 						  &config);
474 	if (IS_ERR(dcdc->regulator)) {
475 		ret = PTR_ERR(dcdc->regulator);
476 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
477 			id + 1, ret);
478 		goto err;
479 	}
480 
481 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
482 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
483 					wm831x_dcdc_uv_irq,
484 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
485 					dcdc->name, dcdc);
486 	if (ret != 0) {
487 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
488 			irq, ret);
489 		goto err;
490 	}
491 
492 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC"));
493 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
494 					wm831x_dcdc_oc_irq,
495 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
496 					dcdc->name, dcdc);
497 	if (ret != 0) {
498 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
499 			irq, ret);
500 		goto err;
501 	}
502 
503 	platform_set_drvdata(pdev, dcdc);
504 
505 	return 0;
506 
507 err:
508 	return ret;
509 }
510 
511 static struct platform_driver wm831x_buckv_driver = {
512 	.probe = wm831x_buckv_probe,
513 	.driver		= {
514 		.name	= "wm831x-buckv",
515 	},
516 };
517 
518 /*
519  * BUCKP specifics
520  */
521 
522 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV)
523 {
524 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
525 	struct wm831x *wm831x = dcdc->wm831x;
526 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
527 	int sel;
528 
529 	sel = regulator_map_voltage_linear(rdev, uV, uV);
530 	if (sel < 0)
531 		return sel;
532 
533 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel);
534 }
535 
536 static const struct regulator_ops wm831x_buckp_ops = {
537 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
538 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
539 	.list_voltage = regulator_list_voltage_linear,
540 	.map_voltage = regulator_map_voltage_linear,
541 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
542 
543 	.is_enabled = regulator_is_enabled_regmap,
544 	.enable = regulator_enable_regmap,
545 	.disable = regulator_disable_regmap,
546 	.get_status = wm831x_dcdc_get_status,
547 	.get_mode = wm831x_dcdc_get_mode,
548 	.set_mode = wm831x_dcdc_set_mode,
549 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
550 };
551 
552 static int wm831x_buckp_probe(struct platform_device *pdev)
553 {
554 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
555 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
556 	struct regulator_config config = { };
557 	int id;
558 	struct wm831x_dcdc *dcdc;
559 	struct resource *res;
560 	int ret, irq;
561 
562 	if (pdata && pdata->wm831x_num)
563 		id = (pdata->wm831x_num * 10) + 1;
564 	else
565 		id = 0;
566 	id = pdev->id - id;
567 
568 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
569 
570 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
571 			    GFP_KERNEL);
572 	if (!dcdc)
573 		return -ENOMEM;
574 
575 	dcdc->wm831x = wm831x;
576 
577 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
578 	if (res == NULL) {
579 		dev_err(&pdev->dev, "No REG resource\n");
580 		ret = -EINVAL;
581 		goto err;
582 	}
583 	dcdc->base = res->start;
584 
585 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
586 	dcdc->desc.name = dcdc->name;
587 
588 	snprintf(dcdc->supply_name, sizeof(dcdc->supply_name),
589 		 "DC%dVDD", id + 1);
590 	dcdc->desc.supply_name = dcdc->supply_name;
591 
592 	dcdc->desc.id = id;
593 	dcdc->desc.type = REGULATOR_VOLTAGE;
594 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
595 	dcdc->desc.ops = &wm831x_buckp_ops;
596 	dcdc->desc.owner = THIS_MODULE;
597 	dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
598 	dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK;
599 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
600 	dcdc->desc.enable_mask = 1 << id;
601 	dcdc->desc.min_uV = 850000;
602 	dcdc->desc.uV_step = 25000;
603 
604 	config.dev = pdev->dev.parent;
605 	if (pdata)
606 		config.init_data = pdata->dcdc[id];
607 	config.driver_data = dcdc;
608 	config.regmap = wm831x->regmap;
609 
610 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
611 						  &config);
612 	if (IS_ERR(dcdc->regulator)) {
613 		ret = PTR_ERR(dcdc->regulator);
614 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
615 			id + 1, ret);
616 		goto err;
617 	}
618 
619 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
620 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
621 					wm831x_dcdc_uv_irq,
622 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
623 					dcdc->name, dcdc);
624 	if (ret != 0) {
625 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
626 			irq, ret);
627 		goto err;
628 	}
629 
630 	platform_set_drvdata(pdev, dcdc);
631 
632 	return 0;
633 
634 err:
635 	return ret;
636 }
637 
638 static struct platform_driver wm831x_buckp_driver = {
639 	.probe = wm831x_buckp_probe,
640 	.driver		= {
641 		.name	= "wm831x-buckp",
642 	},
643 };
644 
645 /*
646  * DCDC boost convertors
647  */
648 
649 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
650 {
651 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
652 	struct wm831x *wm831x = dcdc->wm831x;
653 	int ret;
654 
655 	/* First, check for errors */
656 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
657 	if (ret < 0)
658 		return ret;
659 
660 	if (ret & (1 << rdev_get_id(rdev))) {
661 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
662 			rdev_get_id(rdev) + 1);
663 		return REGULATOR_STATUS_ERROR;
664 	}
665 
666 	/* Is the regulator on? */
667 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
668 	if (ret < 0)
669 		return ret;
670 	if (ret & (1 << rdev_get_id(rdev)))
671 		return REGULATOR_STATUS_ON;
672 	else
673 		return REGULATOR_STATUS_OFF;
674 }
675 
676 static const struct regulator_ops wm831x_boostp_ops = {
677 	.get_status = wm831x_boostp_get_status,
678 
679 	.is_enabled = regulator_is_enabled_regmap,
680 	.enable = regulator_enable_regmap,
681 	.disable = regulator_disable_regmap,
682 };
683 
684 static int wm831x_boostp_probe(struct platform_device *pdev)
685 {
686 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
687 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
688 	struct regulator_config config = { };
689 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
690 	struct wm831x_dcdc *dcdc;
691 	struct resource *res;
692 	int ret, irq;
693 
694 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
695 
696 	if (pdata == NULL || pdata->dcdc[id] == NULL)
697 		return -ENODEV;
698 
699 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
700 	if (!dcdc)
701 		return -ENOMEM;
702 
703 	dcdc->wm831x = wm831x;
704 
705 	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
706 	if (res == NULL) {
707 		dev_err(&pdev->dev, "No REG resource\n");
708 		return -EINVAL;
709 	}
710 	dcdc->base = res->start;
711 
712 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
713 	dcdc->desc.name = dcdc->name;
714 	dcdc->desc.id = id;
715 	dcdc->desc.type = REGULATOR_VOLTAGE;
716 	dcdc->desc.ops = &wm831x_boostp_ops;
717 	dcdc->desc.owner = THIS_MODULE;
718 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
719 	dcdc->desc.enable_mask = 1 << id;
720 
721 	config.dev = pdev->dev.parent;
722 	if (pdata)
723 		config.init_data = pdata->dcdc[id];
724 	config.driver_data = dcdc;
725 	config.regmap = wm831x->regmap;
726 
727 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
728 						  &config);
729 	if (IS_ERR(dcdc->regulator)) {
730 		ret = PTR_ERR(dcdc->regulator);
731 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
732 			id + 1, ret);
733 		return ret;
734 	}
735 
736 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
737 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
738 					wm831x_dcdc_uv_irq,
739 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
740 					dcdc->name,
741 					dcdc);
742 	if (ret != 0) {
743 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
744 			irq, ret);
745 		return ret;
746 	}
747 
748 	platform_set_drvdata(pdev, dcdc);
749 
750 	return 0;
751 }
752 
753 static struct platform_driver wm831x_boostp_driver = {
754 	.probe = wm831x_boostp_probe,
755 	.driver		= {
756 		.name	= "wm831x-boostp",
757 	},
758 };
759 
760 /*
761  * External Power Enable
762  *
763  * These aren't actually DCDCs but look like them in hardware so share
764  * code.
765  */
766 
767 #define WM831X_EPE_BASE 6
768 
769 static const struct regulator_ops wm831x_epe_ops = {
770 	.is_enabled = regulator_is_enabled_regmap,
771 	.enable = regulator_enable_regmap,
772 	.disable = regulator_disable_regmap,
773 	.get_status = wm831x_dcdc_get_status,
774 };
775 
776 static int wm831x_epe_probe(struct platform_device *pdev)
777 {
778 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
779 	struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
780 	struct regulator_config config = { };
781 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
782 	struct wm831x_dcdc *dcdc;
783 	int ret;
784 
785 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
786 
787 	dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL);
788 	if (!dcdc)
789 		return -ENOMEM;
790 
791 	dcdc->wm831x = wm831x;
792 
793 	/* For current parts this is correct; probably need to revisit
794 	 * in future.
795 	 */
796 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
797 	dcdc->desc.name = dcdc->name;
798 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
799 	dcdc->desc.ops = &wm831x_epe_ops;
800 	dcdc->desc.type = REGULATOR_VOLTAGE;
801 	dcdc->desc.owner = THIS_MODULE;
802 	dcdc->desc.enable_reg = WM831X_DCDC_ENABLE;
803 	dcdc->desc.enable_mask = 1 << dcdc->desc.id;
804 
805 	config.dev = pdev->dev.parent;
806 	if (pdata)
807 		config.init_data = pdata->epe[id];
808 	config.driver_data = dcdc;
809 	config.regmap = wm831x->regmap;
810 
811 	dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc,
812 						  &config);
813 	if (IS_ERR(dcdc->regulator)) {
814 		ret = PTR_ERR(dcdc->regulator);
815 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
816 			id + 1, ret);
817 		goto err;
818 	}
819 
820 	platform_set_drvdata(pdev, dcdc);
821 
822 	return 0;
823 
824 err:
825 	return ret;
826 }
827 
828 static struct platform_driver wm831x_epe_driver = {
829 	.probe = wm831x_epe_probe,
830 	.driver		= {
831 		.name	= "wm831x-epe",
832 	},
833 };
834 
835 static struct platform_driver * const drivers[] = {
836 	&wm831x_buckv_driver,
837 	&wm831x_buckp_driver,
838 	&wm831x_boostp_driver,
839 	&wm831x_epe_driver,
840 };
841 
842 static int __init wm831x_dcdc_init(void)
843 {
844 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
845 }
846 subsys_initcall(wm831x_dcdc_init);
847 
848 static void __exit wm831x_dcdc_exit(void)
849 {
850 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
851 }
852 module_exit(wm831x_dcdc_exit);
853 
854 /* Module information */
855 MODULE_AUTHOR("Mark Brown");
856 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
857 MODULE_LICENSE("GPL");
858 MODULE_ALIAS("platform:wm831x-buckv");
859 MODULE_ALIAS("platform:wm831x-buckp");
860 MODULE_ALIAS("platform:wm831x-boostp");
861 MODULE_ALIAS("platform:wm831x-epe");
862