1 /*
2  * wm831x-dcdc.c  --  DC-DC buck convertor driver for the WM831x series
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/gpio.h>
24 
25 #include <linux/mfd/wm831x/core.h>
26 #include <linux/mfd/wm831x/regulator.h>
27 #include <linux/mfd/wm831x/pdata.h>
28 
29 #define WM831X_BUCKV_MAX_SELECTOR 0x68
30 #define WM831X_BUCKP_MAX_SELECTOR 0x66
31 
32 #define WM831X_DCDC_MODE_FAST    0
33 #define WM831X_DCDC_MODE_NORMAL  1
34 #define WM831X_DCDC_MODE_IDLE    2
35 #define WM831X_DCDC_MODE_STANDBY 3
36 
37 #define WM831X_DCDC_MAX_NAME 6
38 
39 /* Register offsets in control block */
40 #define WM831X_DCDC_CONTROL_1     0
41 #define WM831X_DCDC_CONTROL_2     1
42 #define WM831X_DCDC_ON_CONFIG     2
43 #define WM831X_DCDC_SLEEP_CONTROL 3
44 #define WM831X_DCDC_DVS_CONTROL   4
45 
46 /*
47  * Shared
48  */
49 
50 struct wm831x_dcdc {
51 	char name[WM831X_DCDC_MAX_NAME];
52 	struct regulator_desc desc;
53 	int base;
54 	struct wm831x *wm831x;
55 	struct regulator_dev *regulator;
56 	int dvs_gpio;
57 	int dvs_gpio_state;
58 	int on_vsel;
59 	int dvs_vsel;
60 };
61 
62 static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
63 {
64 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
65 	struct wm831x *wm831x = dcdc->wm831x;
66 	int mask = 1 << rdev_get_id(rdev);
67 	int reg;
68 
69 	reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
70 	if (reg < 0)
71 		return reg;
72 
73 	if (reg & mask)
74 		return 1;
75 	else
76 		return 0;
77 }
78 
79 static int wm831x_dcdc_enable(struct regulator_dev *rdev)
80 {
81 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
82 	struct wm831x *wm831x = dcdc->wm831x;
83 	int mask = 1 << rdev_get_id(rdev);
84 
85 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
86 }
87 
88 static int wm831x_dcdc_disable(struct regulator_dev *rdev)
89 {
90 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
91 	struct wm831x *wm831x = dcdc->wm831x;
92 	int mask = 1 << rdev_get_id(rdev);
93 
94 	return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
95 }
96 
97 static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
98 
99 {
100 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
101 	struct wm831x *wm831x = dcdc->wm831x;
102 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
103 	int val;
104 
105 	val = wm831x_reg_read(wm831x, reg);
106 	if (val < 0)
107 		return val;
108 
109 	val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
110 
111 	switch (val) {
112 	case WM831X_DCDC_MODE_FAST:
113 		return REGULATOR_MODE_FAST;
114 	case WM831X_DCDC_MODE_NORMAL:
115 		return REGULATOR_MODE_NORMAL;
116 	case WM831X_DCDC_MODE_STANDBY:
117 		return REGULATOR_MODE_STANDBY;
118 	case WM831X_DCDC_MODE_IDLE:
119 		return REGULATOR_MODE_IDLE;
120 	default:
121 		BUG();
122 	}
123 }
124 
125 static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
126 				    unsigned int mode)
127 {
128 	int val;
129 
130 	switch (mode) {
131 	case REGULATOR_MODE_FAST:
132 		val = WM831X_DCDC_MODE_FAST;
133 		break;
134 	case REGULATOR_MODE_NORMAL:
135 		val = WM831X_DCDC_MODE_NORMAL;
136 		break;
137 	case REGULATOR_MODE_STANDBY:
138 		val = WM831X_DCDC_MODE_STANDBY;
139 		break;
140 	case REGULATOR_MODE_IDLE:
141 		val = WM831X_DCDC_MODE_IDLE;
142 		break;
143 	default:
144 		return -EINVAL;
145 	}
146 
147 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
148 			       val << WM831X_DC1_ON_MODE_SHIFT);
149 }
150 
151 static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
152 {
153 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
154 	struct wm831x *wm831x = dcdc->wm831x;
155 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
156 
157 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
158 }
159 
160 static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
161 					unsigned int mode)
162 {
163 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
164 	struct wm831x *wm831x = dcdc->wm831x;
165 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
166 
167 	return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
168 }
169 
170 static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
171 {
172 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
173 	struct wm831x *wm831x = dcdc->wm831x;
174 	int ret;
175 
176 	/* First, check for errors */
177 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
178 	if (ret < 0)
179 		return ret;
180 
181 	if (ret & (1 << rdev_get_id(rdev))) {
182 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
183 			rdev_get_id(rdev) + 1);
184 		return REGULATOR_STATUS_ERROR;
185 	}
186 
187 	/* DCDC1 and DCDC2 can additionally detect high voltage/current */
188 	if (rdev_get_id(rdev) < 2) {
189 		if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
190 			dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
191 				rdev_get_id(rdev) + 1);
192 			return REGULATOR_STATUS_ERROR;
193 		}
194 
195 		if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
196 			dev_dbg(wm831x->dev, "DCDC%d over current\n",
197 				rdev_get_id(rdev) + 1);
198 			return REGULATOR_STATUS_ERROR;
199 		}
200 	}
201 
202 	/* Is the regulator on? */
203 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
204 	if (ret < 0)
205 		return ret;
206 	if (!(ret & (1 << rdev_get_id(rdev))))
207 		return REGULATOR_STATUS_OFF;
208 
209 	/* TODO: When we handle hardware control modes so we can report the
210 	 * current mode. */
211 	return REGULATOR_STATUS_ON;
212 }
213 
214 static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
215 {
216 	struct wm831x_dcdc *dcdc = data;
217 
218 	regulator_notifier_call_chain(dcdc->regulator,
219 				      REGULATOR_EVENT_UNDER_VOLTAGE,
220 				      NULL);
221 
222 	return IRQ_HANDLED;
223 }
224 
225 static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
226 {
227 	struct wm831x_dcdc *dcdc = data;
228 
229 	regulator_notifier_call_chain(dcdc->regulator,
230 				      REGULATOR_EVENT_OVER_CURRENT,
231 				      NULL);
232 
233 	return IRQ_HANDLED;
234 }
235 
236 /*
237  * BUCKV specifics
238  */
239 
240 static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
241 				      unsigned selector)
242 {
243 	if (selector <= 0x8)
244 		return 600000;
245 	if (selector <= WM831X_BUCKV_MAX_SELECTOR)
246 		return 600000 + ((selector - 0x8) * 12500);
247 	return -EINVAL;
248 }
249 
250 static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
251 					   int min_uV, int max_uV)
252 {
253 	u16 vsel;
254 
255 	if (min_uV < 600000)
256 		vsel = 0;
257 	else if (min_uV <= 1800000)
258 		vsel = ((min_uV - 600000) / 12500) + 8;
259 	else
260 		return -EINVAL;
261 
262 	if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
263 		return -EINVAL;
264 
265 	return vsel;
266 }
267 
268 static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
269 					   int min_uV, int max_uV)
270 {
271 	u16 vsel;
272 
273 	if (max_uV < 600000 || max_uV > 1800000)
274 		return -EINVAL;
275 
276 	vsel = ((max_uV - 600000) / 12500) + 8;
277 
278 	if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
279 	    wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
280 		return -EINVAL;
281 
282 	return vsel;
283 }
284 
285 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
286 {
287 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
288 
289 	if (state == dcdc->dvs_gpio_state)
290 		return 0;
291 
292 	dcdc->dvs_gpio_state = state;
293 	gpio_set_value(dcdc->dvs_gpio, state);
294 
295 	/* Should wait for DVS state change to be asserted if we have
296 	 * a GPIO for it, for now assume the device is configured
297 	 * for the fastest possible transition.
298 	 */
299 
300 	return 0;
301 }
302 
303 static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
304 				    int min_uV, int max_uV)
305 {
306 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
307 	struct wm831x *wm831x = dcdc->wm831x;
308 	int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
309 	int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
310 	int vsel, ret;
311 
312 	vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
313 	if (vsel < 0)
314 		return vsel;
315 
316 	/* If this value is already set then do a GPIO update if we can */
317 	if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
318 		return wm831x_buckv_set_dvs(rdev, 0);
319 
320 	if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
321 		return wm831x_buckv_set_dvs(rdev, 1);
322 
323 	/* Always set the ON status to the minimum voltage */
324 	ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
325 	if (ret < 0)
326 		return ret;
327 	dcdc->on_vsel = vsel;
328 
329 	if (!dcdc->dvs_gpio)
330 		return ret;
331 
332 	/* Kick the voltage transition now */
333 	ret = wm831x_buckv_set_dvs(rdev, 0);
334 	if (ret < 0)
335 		return ret;
336 
337 	/* Set the high voltage as the DVS voltage.  This is optimised
338 	 * for CPUfreq usage, most processors will keep the maximum
339 	 * voltage constant and lower the minimum with the frequency. */
340 	vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
341 	if (vsel < 0) {
342 		/* This should never happen - at worst the same vsel
343 		 * should be chosen */
344 		WARN_ON(vsel < 0);
345 		return 0;
346 	}
347 
348 	/* Don't bother if it's the same VSEL we're already using */
349 	if (vsel == dcdc->on_vsel)
350 		return 0;
351 
352 	ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
353 	if (ret == 0)
354 		dcdc->dvs_vsel = vsel;
355 	else
356 		dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
357 			 ret);
358 
359 	return 0;
360 }
361 
362 static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
363 					    int uV)
364 {
365 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
366 	struct wm831x *wm831x = dcdc->wm831x;
367 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
368 	int vsel;
369 
370 	vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
371 	if (vsel < 0)
372 		return vsel;
373 
374 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
375 }
376 
377 static int wm831x_buckv_get_voltage(struct regulator_dev *rdev)
378 {
379 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
380 
381 	if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
382 		return wm831x_buckv_list_voltage(rdev, dcdc->dvs_vsel);
383 	else
384 		return wm831x_buckv_list_voltage(rdev, dcdc->on_vsel);
385 }
386 
387 /* Current limit options */
388 static u16 wm831x_dcdc_ilim[] = {
389 	125, 250, 375, 500, 625, 750, 875, 1000
390 };
391 
392 static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
393 					   int min_uA, int max_uA)
394 {
395 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
396 	struct wm831x *wm831x = dcdc->wm831x;
397 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
398 	int i;
399 
400 	for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
401 		if (max_uA <= wm831x_dcdc_ilim[i])
402 			break;
403 	}
404 	if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
405 		return -EINVAL;
406 
407 	return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
408 }
409 
410 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
411 {
412 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
413 	struct wm831x *wm831x = dcdc->wm831x;
414 	u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
415 	int val;
416 
417 	val = wm831x_reg_read(wm831x, reg);
418 	if (val < 0)
419 		return val;
420 
421 	return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
422 }
423 
424 static struct regulator_ops wm831x_buckv_ops = {
425 	.set_voltage = wm831x_buckv_set_voltage,
426 	.get_voltage = wm831x_buckv_get_voltage,
427 	.list_voltage = wm831x_buckv_list_voltage,
428 	.set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
429 	.set_current_limit = wm831x_buckv_set_current_limit,
430 	.get_current_limit = wm831x_buckv_get_current_limit,
431 
432 	.is_enabled = wm831x_dcdc_is_enabled,
433 	.enable = wm831x_dcdc_enable,
434 	.disable = wm831x_dcdc_disable,
435 	.get_status = wm831x_dcdc_get_status,
436 	.get_mode = wm831x_dcdc_get_mode,
437 	.set_mode = wm831x_dcdc_set_mode,
438 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
439 };
440 
441 /*
442  * Set up DVS control.  We just log errors since we can still run
443  * (with reduced performance) if we fail.
444  */
445 static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
446 					    struct wm831x_buckv_pdata *pdata)
447 {
448 	struct wm831x *wm831x = dcdc->wm831x;
449 	int ret;
450 	u16 ctrl;
451 
452 	if (!pdata || !pdata->dvs_gpio)
453 		return;
454 
455 	switch (pdata->dvs_control_src) {
456 	case 1:
457 		ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
458 		break;
459 	case 2:
460 		ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
461 		break;
462 	default:
463 		dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
464 			pdata->dvs_control_src, dcdc->name);
465 		return;
466 	}
467 
468 	ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
469 			      WM831X_DC1_DVS_SRC_MASK, ctrl);
470 	if (ret < 0) {
471 		dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
472 			dcdc->name, ret);
473 		return;
474 	}
475 
476 	ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
477 	if (ret < 0) {
478 		dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
479 			dcdc->name, ret);
480 		return;
481 	}
482 
483 	/* gpiolib won't let us read the GPIO status so pick the higher
484 	 * of the two existing voltages so we take it as platform data.
485 	 */
486 	dcdc->dvs_gpio_state = pdata->dvs_init_state;
487 
488 	ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
489 	if (ret < 0) {
490 		dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
491 			dcdc->name, ret);
492 		gpio_free(pdata->dvs_gpio);
493 		return;
494 	}
495 
496 	dcdc->dvs_gpio = pdata->dvs_gpio;
497 }
498 
499 static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
500 {
501 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
502 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
503 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
504 	struct wm831x_dcdc *dcdc;
505 	struct resource *res;
506 	int ret, irq;
507 
508 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
509 
510 	if (pdata == NULL || pdata->dcdc[id] == NULL)
511 		return -ENODEV;
512 
513 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
514 	if (dcdc == NULL) {
515 		dev_err(&pdev->dev, "Unable to allocate private data\n");
516 		return -ENOMEM;
517 	}
518 
519 	dcdc->wm831x = wm831x;
520 
521 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
522 	if (res == NULL) {
523 		dev_err(&pdev->dev, "No I/O resource\n");
524 		ret = -EINVAL;
525 		goto err;
526 	}
527 	dcdc->base = res->start;
528 
529 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
530 	dcdc->desc.name = dcdc->name;
531 	dcdc->desc.id = id;
532 	dcdc->desc.type = REGULATOR_VOLTAGE;
533 	dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
534 	dcdc->desc.ops = &wm831x_buckv_ops;
535 	dcdc->desc.owner = THIS_MODULE;
536 
537 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
538 	if (ret < 0) {
539 		dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
540 		goto err;
541 	}
542 	dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
543 
544 	ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
545 	if (ret < 0) {
546 		dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
547 		goto err;
548 	}
549 	dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
550 
551 	if (pdata->dcdc[id])
552 		wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
553 
554 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
555 					     pdata->dcdc[id], dcdc);
556 	if (IS_ERR(dcdc->regulator)) {
557 		ret = PTR_ERR(dcdc->regulator);
558 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
559 			id + 1, ret);
560 		goto err;
561 	}
562 
563 	irq = platform_get_irq_byname(pdev, "UV");
564 	ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
565 				 IRQF_TRIGGER_RISING, dcdc->name,
566 				 dcdc);
567 	if (ret != 0) {
568 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
569 			irq, ret);
570 		goto err_regulator;
571 	}
572 
573 	irq = platform_get_irq_byname(pdev, "HC");
574 	ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq,
575 				 IRQF_TRIGGER_RISING, dcdc->name,
576 				 dcdc);
577 	if (ret != 0) {
578 		dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
579 			irq, ret);
580 		goto err_uv;
581 	}
582 
583 	platform_set_drvdata(pdev, dcdc);
584 
585 	return 0;
586 
587 err_uv:
588 	wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
589 err_regulator:
590 	regulator_unregister(dcdc->regulator);
591 err:
592 	if (dcdc->dvs_gpio)
593 		gpio_free(dcdc->dvs_gpio);
594 	kfree(dcdc);
595 	return ret;
596 }
597 
598 static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
599 {
600 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
601 	struct wm831x *wm831x = dcdc->wm831x;
602 
603 	wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc);
604 	wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
605 	regulator_unregister(dcdc->regulator);
606 	if (dcdc->dvs_gpio)
607 		gpio_free(dcdc->dvs_gpio);
608 	kfree(dcdc);
609 
610 	return 0;
611 }
612 
613 static struct platform_driver wm831x_buckv_driver = {
614 	.probe = wm831x_buckv_probe,
615 	.remove = __devexit_p(wm831x_buckv_remove),
616 	.driver		= {
617 		.name	= "wm831x-buckv",
618 	},
619 };
620 
621 /*
622  * BUCKP specifics
623  */
624 
625 static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
626 				      unsigned selector)
627 {
628 	if (selector <= WM831X_BUCKP_MAX_SELECTOR)
629 		return 850000 + (selector * 25000);
630 	else
631 		return -EINVAL;
632 }
633 
634 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
635 					int min_uV, int max_uV)
636 {
637 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
638 	struct wm831x *wm831x = dcdc->wm831x;
639 	u16 vsel;
640 
641 	if (min_uV <= 34000000)
642 		vsel = (min_uV - 850000) / 25000;
643 	else
644 		return -EINVAL;
645 
646 	if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
647 		return -EINVAL;
648 
649 	return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
650 }
651 
652 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
653 				    int min_uV, int max_uV)
654 {
655 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
656 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
657 
658 	return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV);
659 }
660 
661 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
662 					    int uV)
663 {
664 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
665 	u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
666 
667 	return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV);
668 }
669 
670 static int wm831x_buckp_get_voltage(struct regulator_dev *rdev)
671 {
672 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
673 	struct wm831x *wm831x = dcdc->wm831x;
674 	u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
675 	int val;
676 
677 	val = wm831x_reg_read(wm831x, reg);
678 	if (val < 0)
679 		return val;
680 
681 	return wm831x_buckp_list_voltage(rdev, val & WM831X_DC3_ON_VSEL_MASK);
682 }
683 
684 static struct regulator_ops wm831x_buckp_ops = {
685 	.set_voltage = wm831x_buckp_set_voltage,
686 	.get_voltage = wm831x_buckp_get_voltage,
687 	.list_voltage = wm831x_buckp_list_voltage,
688 	.set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
689 
690 	.is_enabled = wm831x_dcdc_is_enabled,
691 	.enable = wm831x_dcdc_enable,
692 	.disable = wm831x_dcdc_disable,
693 	.get_status = wm831x_dcdc_get_status,
694 	.get_mode = wm831x_dcdc_get_mode,
695 	.set_mode = wm831x_dcdc_set_mode,
696 	.set_suspend_mode = wm831x_dcdc_set_suspend_mode,
697 };
698 
699 static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
700 {
701 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
702 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
703 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
704 	struct wm831x_dcdc *dcdc;
705 	struct resource *res;
706 	int ret, irq;
707 
708 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
709 
710 	if (pdata == NULL || pdata->dcdc[id] == NULL)
711 		return -ENODEV;
712 
713 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
714 	if (dcdc == NULL) {
715 		dev_err(&pdev->dev, "Unable to allocate private data\n");
716 		return -ENOMEM;
717 	}
718 
719 	dcdc->wm831x = wm831x;
720 
721 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
722 	if (res == NULL) {
723 		dev_err(&pdev->dev, "No I/O resource\n");
724 		ret = -EINVAL;
725 		goto err;
726 	}
727 	dcdc->base = res->start;
728 
729 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
730 	dcdc->desc.name = dcdc->name;
731 	dcdc->desc.id = id;
732 	dcdc->desc.type = REGULATOR_VOLTAGE;
733 	dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
734 	dcdc->desc.ops = &wm831x_buckp_ops;
735 	dcdc->desc.owner = THIS_MODULE;
736 
737 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
738 					     pdata->dcdc[id], dcdc);
739 	if (IS_ERR(dcdc->regulator)) {
740 		ret = PTR_ERR(dcdc->regulator);
741 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
742 			id + 1, ret);
743 		goto err;
744 	}
745 
746 	irq = platform_get_irq_byname(pdev, "UV");
747 	ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
748 				 IRQF_TRIGGER_RISING, dcdc->name,
749 				 dcdc);
750 	if (ret != 0) {
751 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
752 			irq, ret);
753 		goto err_regulator;
754 	}
755 
756 	platform_set_drvdata(pdev, dcdc);
757 
758 	return 0;
759 
760 err_regulator:
761 	regulator_unregister(dcdc->regulator);
762 err:
763 	kfree(dcdc);
764 	return ret;
765 }
766 
767 static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
768 {
769 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
770 	struct wm831x *wm831x = dcdc->wm831x;
771 
772 	wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
773 	regulator_unregister(dcdc->regulator);
774 	kfree(dcdc);
775 
776 	return 0;
777 }
778 
779 static struct platform_driver wm831x_buckp_driver = {
780 	.probe = wm831x_buckp_probe,
781 	.remove = __devexit_p(wm831x_buckp_remove),
782 	.driver		= {
783 		.name	= "wm831x-buckp",
784 	},
785 };
786 
787 /*
788  * DCDC boost convertors
789  */
790 
791 static int wm831x_boostp_get_status(struct regulator_dev *rdev)
792 {
793 	struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
794 	struct wm831x *wm831x = dcdc->wm831x;
795 	int ret;
796 
797 	/* First, check for errors */
798 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
799 	if (ret < 0)
800 		return ret;
801 
802 	if (ret & (1 << rdev_get_id(rdev))) {
803 		dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
804 			rdev_get_id(rdev) + 1);
805 		return REGULATOR_STATUS_ERROR;
806 	}
807 
808 	/* Is the regulator on? */
809 	ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
810 	if (ret < 0)
811 		return ret;
812 	if (ret & (1 << rdev_get_id(rdev)))
813 		return REGULATOR_STATUS_ON;
814 	else
815 		return REGULATOR_STATUS_OFF;
816 }
817 
818 static struct regulator_ops wm831x_boostp_ops = {
819 	.get_status = wm831x_boostp_get_status,
820 
821 	.is_enabled = wm831x_dcdc_is_enabled,
822 	.enable = wm831x_dcdc_enable,
823 	.disable = wm831x_dcdc_disable,
824 };
825 
826 static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
827 {
828 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
829 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
830 	int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
831 	struct wm831x_dcdc *dcdc;
832 	struct resource *res;
833 	int ret, irq;
834 
835 	dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
836 
837 	if (pdata == NULL || pdata->dcdc[id] == NULL)
838 		return -ENODEV;
839 
840 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
841 	if (dcdc == NULL) {
842 		dev_err(&pdev->dev, "Unable to allocate private data\n");
843 		return -ENOMEM;
844 	}
845 
846 	dcdc->wm831x = wm831x;
847 
848 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
849 	if (res == NULL) {
850 		dev_err(&pdev->dev, "No I/O resource\n");
851 		ret = -EINVAL;
852 		goto err;
853 	}
854 	dcdc->base = res->start;
855 
856 	snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
857 	dcdc->desc.name = dcdc->name;
858 	dcdc->desc.id = id;
859 	dcdc->desc.type = REGULATOR_VOLTAGE;
860 	dcdc->desc.ops = &wm831x_boostp_ops;
861 	dcdc->desc.owner = THIS_MODULE;
862 
863 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
864 					     pdata->dcdc[id], dcdc);
865 	if (IS_ERR(dcdc->regulator)) {
866 		ret = PTR_ERR(dcdc->regulator);
867 		dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
868 			id + 1, ret);
869 		goto err;
870 	}
871 
872 	irq = platform_get_irq_byname(pdev, "UV");
873 	ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
874 				 IRQF_TRIGGER_RISING, dcdc->name,
875 				 dcdc);
876 	if (ret != 0) {
877 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
878 			irq, ret);
879 		goto err_regulator;
880 	}
881 
882 	platform_set_drvdata(pdev, dcdc);
883 
884 	return 0;
885 
886 err_regulator:
887 	regulator_unregister(dcdc->regulator);
888 err:
889 	kfree(dcdc);
890 	return ret;
891 }
892 
893 static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
894 {
895 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
896 	struct wm831x *wm831x = dcdc->wm831x;
897 
898 	wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
899 	regulator_unregister(dcdc->regulator);
900 	kfree(dcdc);
901 
902 	return 0;
903 }
904 
905 static struct platform_driver wm831x_boostp_driver = {
906 	.probe = wm831x_boostp_probe,
907 	.remove = __devexit_p(wm831x_boostp_remove),
908 	.driver		= {
909 		.name	= "wm831x-boostp",
910 	},
911 };
912 
913 /*
914  * External Power Enable
915  *
916  * These aren't actually DCDCs but look like them in hardware so share
917  * code.
918  */
919 
920 #define WM831X_EPE_BASE 6
921 
922 static struct regulator_ops wm831x_epe_ops = {
923 	.is_enabled = wm831x_dcdc_is_enabled,
924 	.enable = wm831x_dcdc_enable,
925 	.disable = wm831x_dcdc_disable,
926 	.get_status = wm831x_dcdc_get_status,
927 };
928 
929 static __devinit int wm831x_epe_probe(struct platform_device *pdev)
930 {
931 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
932 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
933 	int id = pdev->id % ARRAY_SIZE(pdata->epe);
934 	struct wm831x_dcdc *dcdc;
935 	int ret;
936 
937 	dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
938 
939 	if (pdata == NULL || pdata->epe[id] == NULL)
940 		return -ENODEV;
941 
942 	dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
943 	if (dcdc == NULL) {
944 		dev_err(&pdev->dev, "Unable to allocate private data\n");
945 		return -ENOMEM;
946 	}
947 
948 	dcdc->wm831x = wm831x;
949 
950 	/* For current parts this is correct; probably need to revisit
951 	 * in future.
952 	 */
953 	snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
954 	dcdc->desc.name = dcdc->name;
955 	dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
956 	dcdc->desc.ops = &wm831x_epe_ops;
957 	dcdc->desc.type = REGULATOR_VOLTAGE;
958 	dcdc->desc.owner = THIS_MODULE;
959 
960 	dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
961 					     pdata->epe[id], dcdc);
962 	if (IS_ERR(dcdc->regulator)) {
963 		ret = PTR_ERR(dcdc->regulator);
964 		dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
965 			id + 1, ret);
966 		goto err;
967 	}
968 
969 	platform_set_drvdata(pdev, dcdc);
970 
971 	return 0;
972 
973 err:
974 	kfree(dcdc);
975 	return ret;
976 }
977 
978 static __devexit int wm831x_epe_remove(struct platform_device *pdev)
979 {
980 	struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
981 
982 	regulator_unregister(dcdc->regulator);
983 	kfree(dcdc);
984 
985 	return 0;
986 }
987 
988 static struct platform_driver wm831x_epe_driver = {
989 	.probe = wm831x_epe_probe,
990 	.remove = __devexit_p(wm831x_epe_remove),
991 	.driver		= {
992 		.name	= "wm831x-epe",
993 	},
994 };
995 
996 static int __init wm831x_dcdc_init(void)
997 {
998 	int ret;
999 	ret = platform_driver_register(&wm831x_buckv_driver);
1000 	if (ret != 0)
1001 		pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
1002 
1003 	ret = platform_driver_register(&wm831x_buckp_driver);
1004 	if (ret != 0)
1005 		pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
1006 
1007 	ret = platform_driver_register(&wm831x_boostp_driver);
1008 	if (ret != 0)
1009 		pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
1010 
1011 	ret = platform_driver_register(&wm831x_epe_driver);
1012 	if (ret != 0)
1013 		pr_err("Failed to register WM831x EPE driver: %d\n", ret);
1014 
1015 	return 0;
1016 }
1017 subsys_initcall(wm831x_dcdc_init);
1018 
1019 static void __exit wm831x_dcdc_exit(void)
1020 {
1021 	platform_driver_unregister(&wm831x_epe_driver);
1022 	platform_driver_unregister(&wm831x_boostp_driver);
1023 	platform_driver_unregister(&wm831x_buckp_driver);
1024 	platform_driver_unregister(&wm831x_buckv_driver);
1025 }
1026 module_exit(wm831x_dcdc_exit);
1027 
1028 /* Module information */
1029 MODULE_AUTHOR("Mark Brown");
1030 MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
1031 MODULE_LICENSE("GPL");
1032 MODULE_ALIAS("platform:wm831x-buckv");
1033 MODULE_ALIAS("platform:wm831x-buckp");
1034