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