1 /*
2  * wm831x-ldo.c  --  LDO 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/slab.h>
23 
24 #include <linux/mfd/wm831x/core.h>
25 #include <linux/mfd/wm831x/regulator.h>
26 #include <linux/mfd/wm831x/pdata.h>
27 
28 #define WM831X_LDO_MAX_NAME 6
29 
30 #define WM831X_LDO_CONTROL       0
31 #define WM831X_LDO_ON_CONTROL    1
32 #define WM831X_LDO_SLEEP_CONTROL 2
33 
34 #define WM831X_ALIVE_LDO_ON_CONTROL    0
35 #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
36 
37 struct wm831x_ldo {
38 	char name[WM831X_LDO_MAX_NAME];
39 	struct regulator_desc desc;
40 	int base;
41 	struct wm831x *wm831x;
42 	struct regulator_dev *regulator;
43 };
44 
45 /*
46  * Shared
47  */
48 
49 static int wm831x_ldo_is_enabled(struct regulator_dev *rdev)
50 {
51 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
52 	struct wm831x *wm831x = ldo->wm831x;
53 	int mask = 1 << rdev_get_id(rdev);
54 	int reg;
55 
56 	reg = wm831x_reg_read(wm831x, WM831X_LDO_ENABLE);
57 	if (reg < 0)
58 		return reg;
59 
60 	if (reg & mask)
61 		return 1;
62 	else
63 		return 0;
64 }
65 
66 static int wm831x_ldo_enable(struct regulator_dev *rdev)
67 {
68 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
69 	struct wm831x *wm831x = ldo->wm831x;
70 	int mask = 1 << rdev_get_id(rdev);
71 
72 	return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, mask);
73 }
74 
75 static int wm831x_ldo_disable(struct regulator_dev *rdev)
76 {
77 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
78 	struct wm831x *wm831x = ldo->wm831x;
79 	int mask = 1 << rdev_get_id(rdev);
80 
81 	return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, 0);
82 }
83 
84 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
85 {
86 	struct wm831x_ldo *ldo = data;
87 
88 	regulator_notifier_call_chain(ldo->regulator,
89 				      REGULATOR_EVENT_UNDER_VOLTAGE,
90 				      NULL);
91 
92 	return IRQ_HANDLED;
93 }
94 
95 /*
96  * General purpose LDOs
97  */
98 
99 #define WM831X_GP_LDO_SELECTOR_LOW 0xe
100 #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
101 
102 static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
103 				      unsigned int selector)
104 {
105 	/* 0.9-1.6V in 50mV steps */
106 	if (selector <= WM831X_GP_LDO_SELECTOR_LOW)
107 		return 900000 + (selector * 50000);
108 	/* 1.7-3.3V in 50mV steps */
109 	if (selector <= WM831X_GP_LDO_MAX_SELECTOR)
110 		return 1600000 + ((selector - WM831X_GP_LDO_SELECTOR_LOW)
111 				  * 100000);
112 	return -EINVAL;
113 }
114 
115 static int wm831x_gp_ldo_set_voltage_int(struct regulator_dev *rdev, int reg,
116 					 int min_uV, int max_uV,
117 					 unsigned *selector)
118 {
119 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
120 	struct wm831x *wm831x = ldo->wm831x;
121 	int vsel, ret;
122 
123 	if (min_uV < 900000)
124 		vsel = 0;
125 	else if (min_uV < 1700000)
126 		vsel = ((min_uV - 900000) / 50000);
127 	else
128 		vsel = ((min_uV - 1700000) / 100000)
129 			+ WM831X_GP_LDO_SELECTOR_LOW + 1;
130 
131 	ret = wm831x_gp_ldo_list_voltage(rdev, vsel);
132 	if (ret < 0)
133 		return ret;
134 	if (ret < min_uV || ret > max_uV)
135 		return -EINVAL;
136 
137 	*selector = vsel;
138 
139 	return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
140 }
141 
142 static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
143 				     int min_uV, int max_uV,
144 				     unsigned *selector)
145 {
146 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
147 	int reg = ldo->base + WM831X_LDO_ON_CONTROL;
148 
149 	return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
150 					     selector);
151 }
152 
153 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
154 					     int uV)
155 {
156 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
157 	int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
158 	unsigned int selector;
159 
160 	return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
161 }
162 
163 static int wm831x_gp_ldo_get_voltage_sel(struct regulator_dev *rdev)
164 {
165 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
166 	struct wm831x *wm831x = ldo->wm831x;
167 	int reg = ldo->base + WM831X_LDO_ON_CONTROL;
168 	int ret;
169 
170 	ret = wm831x_reg_read(wm831x, reg);
171 	if (ret < 0)
172 		return ret;
173 
174 	ret &= WM831X_LDO1_ON_VSEL_MASK;
175 
176 	return ret;
177 }
178 
179 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
180 {
181 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
182 	struct wm831x *wm831x = ldo->wm831x;
183 	int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
184 	int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
185 	int ret;
186 
187 	ret = wm831x_reg_read(wm831x, on_reg);
188 	if (ret < 0)
189 		return ret;
190 
191 	if (!(ret & WM831X_LDO1_ON_MODE))
192 		return REGULATOR_MODE_NORMAL;
193 
194 	ret = wm831x_reg_read(wm831x, ctrl_reg);
195 	if (ret < 0)
196 		return ret;
197 
198 	if (ret & WM831X_LDO1_LP_MODE)
199 		return REGULATOR_MODE_STANDBY;
200 	else
201 		return REGULATOR_MODE_IDLE;
202 }
203 
204 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
205 				  unsigned int mode)
206 {
207 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
208 	struct wm831x *wm831x = ldo->wm831x;
209 	int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
210 	int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
211 	int ret;
212 
213 
214 	switch (mode) {
215 	case REGULATOR_MODE_NORMAL:
216 		ret = wm831x_set_bits(wm831x, on_reg,
217 				      WM831X_LDO1_ON_MODE, 0);
218 		if (ret < 0)
219 			return ret;
220 		break;
221 
222 	case REGULATOR_MODE_IDLE:
223 		ret = wm831x_set_bits(wm831x, ctrl_reg,
224 				      WM831X_LDO1_LP_MODE, 0);
225 		if (ret < 0)
226 			return ret;
227 
228 		ret = wm831x_set_bits(wm831x, on_reg,
229 				      WM831X_LDO1_ON_MODE,
230 				      WM831X_LDO1_ON_MODE);
231 		if (ret < 0)
232 			return ret;
233 		break;
234 
235 	case REGULATOR_MODE_STANDBY:
236 		ret = wm831x_set_bits(wm831x, ctrl_reg,
237 				      WM831X_LDO1_LP_MODE,
238 				      WM831X_LDO1_LP_MODE);
239 		if (ret < 0)
240 			return ret;
241 
242 		ret = wm831x_set_bits(wm831x, on_reg,
243 				      WM831X_LDO1_ON_MODE,
244 				      WM831X_LDO1_ON_MODE);
245 		if (ret < 0)
246 			return ret;
247 		break;
248 
249 	default:
250 		return -EINVAL;
251 	}
252 
253 	return 0;
254 }
255 
256 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
257 {
258 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
259 	struct wm831x *wm831x = ldo->wm831x;
260 	int mask = 1 << rdev_get_id(rdev);
261 	int ret;
262 
263 	/* Is the regulator on? */
264 	ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
265 	if (ret < 0)
266 		return ret;
267 	if (!(ret & mask))
268 		return REGULATOR_STATUS_OFF;
269 
270 	/* Is it reporting under voltage? */
271 	ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
272 	if (ret & mask)
273 		return REGULATOR_STATUS_ERROR;
274 
275 	ret = wm831x_gp_ldo_get_mode(rdev);
276 	if (ret < 0)
277 		return ret;
278 	else
279 		return regulator_mode_to_status(ret);
280 }
281 
282 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
283 						   int input_uV,
284 						   int output_uV, int load_uA)
285 {
286 	if (load_uA < 20000)
287 		return REGULATOR_MODE_STANDBY;
288 	if (load_uA < 50000)
289 		return REGULATOR_MODE_IDLE;
290 	return REGULATOR_MODE_NORMAL;
291 }
292 
293 
294 static struct regulator_ops wm831x_gp_ldo_ops = {
295 	.list_voltage = wm831x_gp_ldo_list_voltage,
296 	.get_voltage_sel = wm831x_gp_ldo_get_voltage_sel,
297 	.set_voltage = wm831x_gp_ldo_set_voltage,
298 	.set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
299 	.get_mode = wm831x_gp_ldo_get_mode,
300 	.set_mode = wm831x_gp_ldo_set_mode,
301 	.get_status = wm831x_gp_ldo_get_status,
302 	.get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
303 
304 	.is_enabled = wm831x_ldo_is_enabled,
305 	.enable = wm831x_ldo_enable,
306 	.disable = wm831x_ldo_disable,
307 };
308 
309 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
310 {
311 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
312 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
313 	int id;
314 	struct wm831x_ldo *ldo;
315 	struct resource *res;
316 	int ret, irq;
317 
318 	if (pdata && pdata->wm831x_num)
319 		id = (pdata->wm831x_num * 10) + 1;
320 	else
321 		id = 0;
322 	id = pdev->id - id;
323 
324 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
325 
326 	if (pdata == NULL || pdata->ldo[id] == NULL)
327 		return -ENODEV;
328 
329 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
330 	if (ldo == NULL) {
331 		dev_err(&pdev->dev, "Unable to allocate private data\n");
332 		return -ENOMEM;
333 	}
334 
335 	ldo->wm831x = wm831x;
336 
337 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
338 	if (res == NULL) {
339 		dev_err(&pdev->dev, "No I/O resource\n");
340 		ret = -EINVAL;
341 		goto err;
342 	}
343 	ldo->base = res->start;
344 
345 	snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
346 	ldo->desc.name = ldo->name;
347 	ldo->desc.id = id;
348 	ldo->desc.type = REGULATOR_VOLTAGE;
349 	ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
350 	ldo->desc.ops = &wm831x_gp_ldo_ops;
351 	ldo->desc.owner = THIS_MODULE;
352 
353 	ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
354 					     pdata->ldo[id], ldo, NULL);
355 	if (IS_ERR(ldo->regulator)) {
356 		ret = PTR_ERR(ldo->regulator);
357 		dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
358 			id + 1, ret);
359 		goto err;
360 	}
361 
362 	irq = platform_get_irq_byname(pdev, "UV");
363 	ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
364 				   IRQF_TRIGGER_RISING, ldo->name,
365 				   ldo);
366 	if (ret != 0) {
367 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
368 			irq, ret);
369 		goto err_regulator;
370 	}
371 
372 	platform_set_drvdata(pdev, ldo);
373 
374 	return 0;
375 
376 err_regulator:
377 	regulator_unregister(ldo->regulator);
378 err:
379 	return ret;
380 }
381 
382 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
383 {
384 	struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
385 
386 	platform_set_drvdata(pdev, NULL);
387 
388 	free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
389 	regulator_unregister(ldo->regulator);
390 
391 	return 0;
392 }
393 
394 static struct platform_driver wm831x_gp_ldo_driver = {
395 	.probe = wm831x_gp_ldo_probe,
396 	.remove = __devexit_p(wm831x_gp_ldo_remove),
397 	.driver		= {
398 		.name	= "wm831x-ldo",
399 		.owner	= THIS_MODULE,
400 	},
401 };
402 
403 /*
404  * Analogue LDOs
405  */
406 
407 
408 #define WM831X_ALDO_SELECTOR_LOW 0xc
409 #define WM831X_ALDO_MAX_SELECTOR 0x1f
410 
411 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
412 				      unsigned int selector)
413 {
414 	/* 1-1.6V in 50mV steps */
415 	if (selector <= WM831X_ALDO_SELECTOR_LOW)
416 		return 1000000 + (selector * 50000);
417 	/* 1.7-3.5V in 50mV steps */
418 	if (selector <= WM831X_ALDO_MAX_SELECTOR)
419 		return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
420 				  * 100000);
421 	return -EINVAL;
422 }
423 
424 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
425 				       int min_uV, int max_uV,
426 				       unsigned *selector)
427 {
428 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
429 	struct wm831x *wm831x = ldo->wm831x;
430 	int vsel, ret;
431 
432 	if (min_uV < 1000000)
433 		vsel = 0;
434 	else if (min_uV < 1700000)
435 		vsel = ((min_uV - 1000000) / 50000);
436 	else
437 		vsel = ((min_uV - 1700000) / 100000)
438 			+ WM831X_ALDO_SELECTOR_LOW + 1;
439 
440 	ret = wm831x_aldo_list_voltage(rdev, vsel);
441 	if (ret < 0)
442 		return ret;
443 	if (ret < min_uV || ret > max_uV)
444 		return -EINVAL;
445 
446 	*selector = vsel;
447 
448 	return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
449 }
450 
451 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
452 				   int min_uV, int max_uV, unsigned *selector)
453 {
454 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
455 	int reg = ldo->base + WM831X_LDO_ON_CONTROL;
456 
457 	return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
458 					   selector);
459 }
460 
461 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
462 					     int uV)
463 {
464 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
465 	int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
466 	unsigned int selector;
467 
468 	return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
469 }
470 
471 static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
472 {
473 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
474 	struct wm831x *wm831x = ldo->wm831x;
475 	int reg = ldo->base + WM831X_LDO_ON_CONTROL;
476 	int ret;
477 
478 	ret = wm831x_reg_read(wm831x, reg);
479 	if (ret < 0)
480 		return ret;
481 
482 	ret &= WM831X_LDO7_ON_VSEL_MASK;
483 
484 	return ret;
485 }
486 
487 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
488 {
489 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
490 	struct wm831x *wm831x = ldo->wm831x;
491 	int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
492 	int ret;
493 
494 	ret = wm831x_reg_read(wm831x, on_reg);
495 	if (ret < 0)
496 		return 0;
497 
498 	if (ret & WM831X_LDO7_ON_MODE)
499 		return REGULATOR_MODE_IDLE;
500 	else
501 		return REGULATOR_MODE_NORMAL;
502 }
503 
504 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
505 				  unsigned int mode)
506 {
507 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
508 	struct wm831x *wm831x = ldo->wm831x;
509 	int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
510 	int ret;
511 
512 
513 	switch (mode) {
514 	case REGULATOR_MODE_NORMAL:
515 		ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
516 		if (ret < 0)
517 			return ret;
518 		break;
519 
520 	case REGULATOR_MODE_IDLE:
521 		ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
522 				      WM831X_LDO7_ON_MODE);
523 		if (ret < 0)
524 			return ret;
525 		break;
526 
527 	default:
528 		return -EINVAL;
529 	}
530 
531 	return 0;
532 }
533 
534 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
535 {
536 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
537 	struct wm831x *wm831x = ldo->wm831x;
538 	int mask = 1 << rdev_get_id(rdev);
539 	int ret;
540 
541 	/* Is the regulator on? */
542 	ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
543 	if (ret < 0)
544 		return ret;
545 	if (!(ret & mask))
546 		return REGULATOR_STATUS_OFF;
547 
548 	/* Is it reporting under voltage? */
549 	ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
550 	if (ret & mask)
551 		return REGULATOR_STATUS_ERROR;
552 
553 	ret = wm831x_aldo_get_mode(rdev);
554 	if (ret < 0)
555 		return ret;
556 	else
557 		return regulator_mode_to_status(ret);
558 }
559 
560 static struct regulator_ops wm831x_aldo_ops = {
561 	.list_voltage = wm831x_aldo_list_voltage,
562 	.get_voltage_sel = wm831x_aldo_get_voltage_sel,
563 	.set_voltage = wm831x_aldo_set_voltage,
564 	.set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
565 	.get_mode = wm831x_aldo_get_mode,
566 	.set_mode = wm831x_aldo_set_mode,
567 	.get_status = wm831x_aldo_get_status,
568 
569 	.is_enabled = wm831x_ldo_is_enabled,
570 	.enable = wm831x_ldo_enable,
571 	.disable = wm831x_ldo_disable,
572 };
573 
574 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
575 {
576 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
577 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
578 	int id;
579 	struct wm831x_ldo *ldo;
580 	struct resource *res;
581 	int ret, irq;
582 
583 	if (pdata && pdata->wm831x_num)
584 		id = (pdata->wm831x_num * 10) + 1;
585 	else
586 		id = 0;
587 	id = pdev->id - id;
588 
589 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
590 
591 	if (pdata == NULL || pdata->ldo[id] == NULL)
592 		return -ENODEV;
593 
594 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
595 	if (ldo == NULL) {
596 		dev_err(&pdev->dev, "Unable to allocate private data\n");
597 		return -ENOMEM;
598 	}
599 
600 	ldo->wm831x = wm831x;
601 
602 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
603 	if (res == NULL) {
604 		dev_err(&pdev->dev, "No I/O resource\n");
605 		ret = -EINVAL;
606 		goto err;
607 	}
608 	ldo->base = res->start;
609 
610 	snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
611 	ldo->desc.name = ldo->name;
612 	ldo->desc.id = id;
613 	ldo->desc.type = REGULATOR_VOLTAGE;
614 	ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
615 	ldo->desc.ops = &wm831x_aldo_ops;
616 	ldo->desc.owner = THIS_MODULE;
617 
618 	ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
619 					     pdata->ldo[id], ldo, NULL);
620 	if (IS_ERR(ldo->regulator)) {
621 		ret = PTR_ERR(ldo->regulator);
622 		dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
623 			id + 1, ret);
624 		goto err;
625 	}
626 
627 	irq = platform_get_irq_byname(pdev, "UV");
628 	ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
629 				   IRQF_TRIGGER_RISING, ldo->name, ldo);
630 	if (ret != 0) {
631 		dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
632 			irq, ret);
633 		goto err_regulator;
634 	}
635 
636 	platform_set_drvdata(pdev, ldo);
637 
638 	return 0;
639 
640 err_regulator:
641 	regulator_unregister(ldo->regulator);
642 err:
643 	return ret;
644 }
645 
646 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
647 {
648 	struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
649 
650 	free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
651 	regulator_unregister(ldo->regulator);
652 
653 	return 0;
654 }
655 
656 static struct platform_driver wm831x_aldo_driver = {
657 	.probe = wm831x_aldo_probe,
658 	.remove = __devexit_p(wm831x_aldo_remove),
659 	.driver		= {
660 		.name	= "wm831x-aldo",
661 		.owner	= THIS_MODULE,
662 	},
663 };
664 
665 /*
666  * Alive LDO
667  */
668 
669 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
670 
671 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
672 				      unsigned int selector)
673 {
674 	/* 0.8-1.55V in 50mV steps */
675 	if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
676 		return 800000 + (selector * 50000);
677 	return -EINVAL;
678 }
679 
680 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
681 					    int reg,
682 					    int min_uV, int max_uV,
683 					    unsigned *selector)
684 {
685 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
686 	struct wm831x *wm831x = ldo->wm831x;
687 	int vsel, ret;
688 
689 	vsel = (min_uV - 800000) / 50000;
690 
691 	ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
692 	if (ret < 0)
693 		return ret;
694 	if (ret < min_uV || ret > max_uV)
695 		return -EINVAL;
696 
697 	*selector = vsel;
698 
699 	return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
700 }
701 
702 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
703 					int min_uV, int max_uV,
704 					unsigned *selector)
705 {
706 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
707 	int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
708 
709 	return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
710 						selector);
711 }
712 
713 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
714 					     int uV)
715 {
716 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
717 	int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
718 	unsigned selector;
719 
720 	return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
721 }
722 
723 static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
724 {
725 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
726 	struct wm831x *wm831x = ldo->wm831x;
727 	int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
728 	int ret;
729 
730 	ret = wm831x_reg_read(wm831x, reg);
731 	if (ret < 0)
732 		return ret;
733 
734 	ret &= WM831X_LDO11_ON_VSEL_MASK;
735 
736 	return ret;
737 }
738 
739 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
740 {
741 	struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
742 	struct wm831x *wm831x = ldo->wm831x;
743 	int mask = 1 << rdev_get_id(rdev);
744 	int ret;
745 
746 	/* Is the regulator on? */
747 	ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
748 	if (ret < 0)
749 		return ret;
750 	if (ret & mask)
751 		return REGULATOR_STATUS_ON;
752 	else
753 		return REGULATOR_STATUS_OFF;
754 }
755 
756 static struct regulator_ops wm831x_alive_ldo_ops = {
757 	.list_voltage = wm831x_alive_ldo_list_voltage,
758 	.get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
759 	.set_voltage = wm831x_alive_ldo_set_voltage,
760 	.set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
761 	.get_status = wm831x_alive_ldo_get_status,
762 
763 	.is_enabled = wm831x_ldo_is_enabled,
764 	.enable = wm831x_ldo_enable,
765 	.disable = wm831x_ldo_disable,
766 };
767 
768 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
769 {
770 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
771 	struct wm831x_pdata *pdata = wm831x->dev->platform_data;
772 	int id;
773 	struct wm831x_ldo *ldo;
774 	struct resource *res;
775 	int ret;
776 
777 	if (pdata && pdata->wm831x_num)
778 		id = (pdata->wm831x_num * 10) + 1;
779 	else
780 		id = 0;
781 	id = pdev->id - id;
782 
783 
784 	dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
785 
786 	if (pdata == NULL || pdata->ldo[id] == NULL)
787 		return -ENODEV;
788 
789 	ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
790 	if (ldo == NULL) {
791 		dev_err(&pdev->dev, "Unable to allocate private data\n");
792 		return -ENOMEM;
793 	}
794 
795 	ldo->wm831x = wm831x;
796 
797 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
798 	if (res == NULL) {
799 		dev_err(&pdev->dev, "No I/O resource\n");
800 		ret = -EINVAL;
801 		goto err;
802 	}
803 	ldo->base = res->start;
804 
805 	snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
806 	ldo->desc.name = ldo->name;
807 	ldo->desc.id = id;
808 	ldo->desc.type = REGULATOR_VOLTAGE;
809 	ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
810 	ldo->desc.ops = &wm831x_alive_ldo_ops;
811 	ldo->desc.owner = THIS_MODULE;
812 
813 	ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
814 					     pdata->ldo[id], ldo, NULL);
815 	if (IS_ERR(ldo->regulator)) {
816 		ret = PTR_ERR(ldo->regulator);
817 		dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
818 			id + 1, ret);
819 		goto err;
820 	}
821 
822 	platform_set_drvdata(pdev, ldo);
823 
824 	return 0;
825 
826 err:
827 	return ret;
828 }
829 
830 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
831 {
832 	struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
833 
834 	regulator_unregister(ldo->regulator);
835 
836 	return 0;
837 }
838 
839 static struct platform_driver wm831x_alive_ldo_driver = {
840 	.probe = wm831x_alive_ldo_probe,
841 	.remove = __devexit_p(wm831x_alive_ldo_remove),
842 	.driver		= {
843 		.name	= "wm831x-alive-ldo",
844 		.owner	= THIS_MODULE,
845 	},
846 };
847 
848 static int __init wm831x_ldo_init(void)
849 {
850 	int ret;
851 
852 	ret = platform_driver_register(&wm831x_gp_ldo_driver);
853 	if (ret != 0)
854 		pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
855 
856 	ret = platform_driver_register(&wm831x_aldo_driver);
857 	if (ret != 0)
858 		pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
859 
860 	ret = platform_driver_register(&wm831x_alive_ldo_driver);
861 	if (ret != 0)
862 		pr_err("Failed to register WM831x alive LDO driver: %d\n",
863 		       ret);
864 
865 	return 0;
866 }
867 subsys_initcall(wm831x_ldo_init);
868 
869 static void __exit wm831x_ldo_exit(void)
870 {
871 	platform_driver_unregister(&wm831x_alive_ldo_driver);
872 	platform_driver_unregister(&wm831x_aldo_driver);
873 	platform_driver_unregister(&wm831x_gp_ldo_driver);
874 }
875 module_exit(wm831x_ldo_exit);
876 
877 /* Module information */
878 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
879 MODULE_DESCRIPTION("WM831x LDO driver");
880 MODULE_LICENSE("GPL");
881 MODULE_ALIAS("platform:wm831x-ldo");
882 MODULE_ALIAS("platform:wm831x-aldo");
883 MODULE_ALIAS("platform:wm831x-aliveldo");
884