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