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