ARM: PL08x: ensure loops use cpu_relax()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / wm831x-ldo.c
blob9edf8f692341d89ed3645459da80be221c94eeec
1 /*
2 * wm831x-ldo.c -- LDO driver for the WM831x series
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
24 #include <linux/mfd/wm831x/core.h>
25 #include <linux/mfd/wm831x/regulator.h>
26 #include <linux/mfd/wm831x/pdata.h>
28 #define WM831X_LDO_MAX_NAME 6
30 #define WM831X_LDO_CONTROL 0
31 #define WM831X_LDO_ON_CONTROL 1
32 #define WM831X_LDO_SLEEP_CONTROL 2
34 #define WM831X_ALIVE_LDO_ON_CONTROL 0
35 #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
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;
46 * Shared
49 static int wm831x_ldo_is_enabled(struct regulator_dev *rdev)
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;
56 reg = wm831x_reg_read(wm831x, WM831X_LDO_ENABLE);
57 if (reg < 0)
58 return reg;
60 if (reg & mask)
61 return 1;
62 else
63 return 0;
66 static int wm831x_ldo_enable(struct regulator_dev *rdev)
68 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
69 struct wm831x *wm831x = ldo->wm831x;
70 int mask = 1 << rdev_get_id(rdev);
72 return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, mask);
75 static int wm831x_ldo_disable(struct regulator_dev *rdev)
77 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
78 struct wm831x *wm831x = ldo->wm831x;
79 int mask = 1 << rdev_get_id(rdev);
81 return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, 0);
84 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
86 struct wm831x_ldo *ldo = data;
88 regulator_notifier_call_chain(ldo->regulator,
89 REGULATOR_EVENT_UNDER_VOLTAGE,
90 NULL);
92 return IRQ_HANDLED;
96 * General purpose LDOs
99 #define WM831X_GP_LDO_SELECTOR_LOW 0xe
100 #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
102 static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
103 unsigned int selector)
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;
115 static int wm831x_gp_ldo_set_voltage_int(struct regulator_dev *rdev, int reg,
116 int min_uV, int max_uV)
118 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
119 struct wm831x *wm831x = ldo->wm831x;
120 int vsel, ret;
122 if (min_uV < 900000)
123 vsel = 0;
124 else if (min_uV < 1700000)
125 vsel = ((min_uV - 900000) / 50000);
126 else
127 vsel = ((min_uV - 1700000) / 100000)
128 + WM831X_GP_LDO_SELECTOR_LOW + 1;
130 ret = wm831x_gp_ldo_list_voltage(rdev, vsel);
131 if (ret < 0)
132 return ret;
133 if (ret < min_uV || ret > max_uV)
134 return -EINVAL;
136 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
139 static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
140 int min_uV, int max_uV)
142 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
143 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
145 return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
148 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
149 int uV)
151 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
152 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
154 return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV);
157 static int wm831x_gp_ldo_get_voltage(struct regulator_dev *rdev)
159 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
160 struct wm831x *wm831x = ldo->wm831x;
161 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
162 int ret;
164 ret = wm831x_reg_read(wm831x, reg);
165 if (ret < 0)
166 return ret;
168 ret &= WM831X_LDO1_ON_VSEL_MASK;
170 return wm831x_gp_ldo_list_voltage(rdev, ret);
173 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
175 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
176 struct wm831x *wm831x = ldo->wm831x;
177 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
178 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
179 int ret;
181 ret = wm831x_reg_read(wm831x, on_reg);
182 if (ret < 0)
183 return ret;
185 if (!(ret & WM831X_LDO1_ON_MODE))
186 return REGULATOR_MODE_NORMAL;
188 ret = wm831x_reg_read(wm831x, ctrl_reg);
189 if (ret < 0)
190 return ret;
192 if (ret & WM831X_LDO1_LP_MODE)
193 return REGULATOR_MODE_STANDBY;
194 else
195 return REGULATOR_MODE_IDLE;
198 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
199 unsigned int mode)
201 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
202 struct wm831x *wm831x = ldo->wm831x;
203 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
204 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
205 int ret;
208 switch (mode) {
209 case REGULATOR_MODE_NORMAL:
210 ret = wm831x_set_bits(wm831x, on_reg,
211 WM831X_LDO1_ON_MODE, 0);
212 if (ret < 0)
213 return ret;
214 break;
216 case REGULATOR_MODE_IDLE:
217 ret = wm831x_set_bits(wm831x, ctrl_reg,
218 WM831X_LDO1_LP_MODE, 0);
219 if (ret < 0)
220 return ret;
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 break;
229 case REGULATOR_MODE_STANDBY:
230 ret = wm831x_set_bits(wm831x, ctrl_reg,
231 WM831X_LDO1_LP_MODE,
232 WM831X_LDO1_LP_MODE);
233 if (ret < 0)
234 return ret;
236 ret = wm831x_set_bits(wm831x, on_reg,
237 WM831X_LDO1_ON_MODE,
238 WM831X_LDO1_ON_MODE);
239 if (ret < 0)
240 return ret;
241 break;
243 default:
244 return -EINVAL;
247 return 0;
250 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
252 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
253 struct wm831x *wm831x = ldo->wm831x;
254 int mask = 1 << rdev_get_id(rdev);
255 int ret;
257 /* Is the regulator on? */
258 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
259 if (ret < 0)
260 return ret;
261 if (!(ret & mask))
262 return REGULATOR_STATUS_OFF;
264 /* Is it reporting under voltage? */
265 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
266 if (ret & mask)
267 return REGULATOR_STATUS_ERROR;
269 ret = wm831x_gp_ldo_get_mode(rdev);
270 if (ret < 0)
271 return ret;
272 else
273 return regulator_mode_to_status(ret);
276 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
277 int input_uV,
278 int output_uV, int load_uA)
280 if (load_uA < 20000)
281 return REGULATOR_MODE_STANDBY;
282 if (load_uA < 50000)
283 return REGULATOR_MODE_IDLE;
284 return REGULATOR_MODE_NORMAL;
288 static struct regulator_ops wm831x_gp_ldo_ops = {
289 .list_voltage = wm831x_gp_ldo_list_voltage,
290 .get_voltage = wm831x_gp_ldo_get_voltage,
291 .set_voltage = wm831x_gp_ldo_set_voltage,
292 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
293 .get_mode = wm831x_gp_ldo_get_mode,
294 .set_mode = wm831x_gp_ldo_set_mode,
295 .get_status = wm831x_gp_ldo_get_status,
296 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
298 .is_enabled = wm831x_ldo_is_enabled,
299 .enable = wm831x_ldo_enable,
300 .disable = wm831x_ldo_disable,
303 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
305 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
306 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
307 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
308 struct wm831x_ldo *ldo;
309 struct resource *res;
310 int ret, irq;
312 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
314 if (pdata == NULL || pdata->ldo[id] == NULL)
315 return -ENODEV;
317 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
318 if (ldo == NULL) {
319 dev_err(&pdev->dev, "Unable to allocate private data\n");
320 return -ENOMEM;
323 ldo->wm831x = wm831x;
325 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
326 if (res == NULL) {
327 dev_err(&pdev->dev, "No I/O resource\n");
328 ret = -EINVAL;
329 goto err;
331 ldo->base = res->start;
333 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
334 ldo->desc.name = ldo->name;
335 ldo->desc.id = id;
336 ldo->desc.type = REGULATOR_VOLTAGE;
337 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
338 ldo->desc.ops = &wm831x_gp_ldo_ops;
339 ldo->desc.owner = THIS_MODULE;
341 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
342 pdata->ldo[id], ldo);
343 if (IS_ERR(ldo->regulator)) {
344 ret = PTR_ERR(ldo->regulator);
345 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
346 id + 1, ret);
347 goto err;
350 irq = platform_get_irq_byname(pdev, "UV");
351 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
352 IRQF_TRIGGER_RISING, ldo->name,
353 ldo);
354 if (ret != 0) {
355 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
356 irq, ret);
357 goto err_regulator;
360 platform_set_drvdata(pdev, ldo);
362 return 0;
364 err_regulator:
365 regulator_unregister(ldo->regulator);
366 err:
367 kfree(ldo);
368 return ret;
371 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
373 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
374 struct wm831x *wm831x = ldo->wm831x;
376 platform_set_drvdata(pdev, NULL);
378 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
379 regulator_unregister(ldo->regulator);
380 kfree(ldo);
382 return 0;
385 static struct platform_driver wm831x_gp_ldo_driver = {
386 .probe = wm831x_gp_ldo_probe,
387 .remove = __devexit_p(wm831x_gp_ldo_remove),
388 .driver = {
389 .name = "wm831x-ldo",
390 .owner = THIS_MODULE,
395 * Analogue LDOs
399 #define WM831X_ALDO_SELECTOR_LOW 0xc
400 #define WM831X_ALDO_MAX_SELECTOR 0x1f
402 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
403 unsigned int selector)
405 /* 1-1.6V in 50mV steps */
406 if (selector <= WM831X_ALDO_SELECTOR_LOW)
407 return 1000000 + (selector * 50000);
408 /* 1.7-3.5V in 50mV steps */
409 if (selector <= WM831X_ALDO_MAX_SELECTOR)
410 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
411 * 100000);
412 return -EINVAL;
415 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
416 int min_uV, int max_uV)
418 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
419 struct wm831x *wm831x = ldo->wm831x;
420 int vsel, ret;
422 if (min_uV < 1000000)
423 vsel = 0;
424 else if (min_uV < 1700000)
425 vsel = ((min_uV - 1000000) / 50000);
426 else
427 vsel = ((min_uV - 1700000) / 100000)
428 + WM831X_ALDO_SELECTOR_LOW + 1;
430 ret = wm831x_aldo_list_voltage(rdev, vsel);
431 if (ret < 0)
432 return ret;
433 if (ret < min_uV || ret > max_uV)
434 return -EINVAL;
436 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
439 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
440 int min_uV, int max_uV)
442 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
443 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
445 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV);
448 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
449 int uV)
451 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
452 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
454 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV);
457 static int wm831x_aldo_get_voltage(struct regulator_dev *rdev)
459 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
460 struct wm831x *wm831x = ldo->wm831x;
461 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
462 int ret;
464 ret = wm831x_reg_read(wm831x, reg);
465 if (ret < 0)
466 return ret;
468 ret &= WM831X_LDO7_ON_VSEL_MASK;
470 return wm831x_aldo_list_voltage(rdev, ret);
473 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
475 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
476 struct wm831x *wm831x = ldo->wm831x;
477 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
478 int ret;
480 ret = wm831x_reg_read(wm831x, on_reg);
481 if (ret < 0)
482 return 0;
484 if (ret & WM831X_LDO7_ON_MODE)
485 return REGULATOR_MODE_IDLE;
486 else
487 return REGULATOR_MODE_NORMAL;
490 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
491 unsigned int mode)
493 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
494 struct wm831x *wm831x = ldo->wm831x;
495 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
496 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
497 int ret;
500 switch (mode) {
501 case REGULATOR_MODE_NORMAL:
502 ret = wm831x_set_bits(wm831x, on_reg,
503 WM831X_LDO7_ON_MODE, 0);
504 if (ret < 0)
505 return ret;
506 break;
508 case REGULATOR_MODE_IDLE:
509 ret = wm831x_set_bits(wm831x, ctrl_reg,
510 WM831X_LDO7_ON_MODE,
511 WM831X_LDO7_ON_MODE);
512 if (ret < 0)
513 return ret;
514 break;
516 default:
517 return -EINVAL;
520 return 0;
523 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
525 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
526 struct wm831x *wm831x = ldo->wm831x;
527 int mask = 1 << rdev_get_id(rdev);
528 int ret;
530 /* Is the regulator on? */
531 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
532 if (ret < 0)
533 return ret;
534 if (!(ret & mask))
535 return REGULATOR_STATUS_OFF;
537 /* Is it reporting under voltage? */
538 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
539 if (ret & mask)
540 return REGULATOR_STATUS_ERROR;
542 ret = wm831x_aldo_get_mode(rdev);
543 if (ret < 0)
544 return ret;
545 else
546 return regulator_mode_to_status(ret);
549 static struct regulator_ops wm831x_aldo_ops = {
550 .list_voltage = wm831x_aldo_list_voltage,
551 .get_voltage = wm831x_aldo_get_voltage,
552 .set_voltage = wm831x_aldo_set_voltage,
553 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
554 .get_mode = wm831x_aldo_get_mode,
555 .set_mode = wm831x_aldo_set_mode,
556 .get_status = wm831x_aldo_get_status,
558 .is_enabled = wm831x_ldo_is_enabled,
559 .enable = wm831x_ldo_enable,
560 .disable = wm831x_ldo_disable,
563 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
565 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
566 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
567 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
568 struct wm831x_ldo *ldo;
569 struct resource *res;
570 int ret, irq;
572 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
574 if (pdata == NULL || pdata->ldo[id] == NULL)
575 return -ENODEV;
577 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
578 if (ldo == NULL) {
579 dev_err(&pdev->dev, "Unable to allocate private data\n");
580 return -ENOMEM;
583 ldo->wm831x = wm831x;
585 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
586 if (res == NULL) {
587 dev_err(&pdev->dev, "No I/O resource\n");
588 ret = -EINVAL;
589 goto err;
591 ldo->base = res->start;
593 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
594 ldo->desc.name = ldo->name;
595 ldo->desc.id = id;
596 ldo->desc.type = REGULATOR_VOLTAGE;
597 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
598 ldo->desc.ops = &wm831x_aldo_ops;
599 ldo->desc.owner = THIS_MODULE;
601 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
602 pdata->ldo[id], ldo);
603 if (IS_ERR(ldo->regulator)) {
604 ret = PTR_ERR(ldo->regulator);
605 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
606 id + 1, ret);
607 goto err;
610 irq = platform_get_irq_byname(pdev, "UV");
611 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
612 IRQF_TRIGGER_RISING, ldo->name,
613 ldo);
614 if (ret != 0) {
615 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
616 irq, ret);
617 goto err_regulator;
620 platform_set_drvdata(pdev, ldo);
622 return 0;
624 err_regulator:
625 regulator_unregister(ldo->regulator);
626 err:
627 kfree(ldo);
628 return ret;
631 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
633 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
634 struct wm831x *wm831x = ldo->wm831x;
636 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
637 regulator_unregister(ldo->regulator);
638 kfree(ldo);
640 return 0;
643 static struct platform_driver wm831x_aldo_driver = {
644 .probe = wm831x_aldo_probe,
645 .remove = __devexit_p(wm831x_aldo_remove),
646 .driver = {
647 .name = "wm831x-aldo",
648 .owner = THIS_MODULE,
653 * Alive LDO
656 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
658 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
659 unsigned int selector)
661 /* 0.8-1.55V in 50mV steps */
662 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
663 return 800000 + (selector * 50000);
664 return -EINVAL;
667 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
668 int reg,
669 int min_uV, int max_uV)
671 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
672 struct wm831x *wm831x = ldo->wm831x;
673 int vsel, ret;
675 vsel = (min_uV - 800000) / 50000;
677 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
678 if (ret < 0)
679 return ret;
680 if (ret < min_uV || ret > max_uV)
681 return -EINVAL;
683 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
686 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
687 int min_uV, int max_uV)
689 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
690 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
692 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
695 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
696 int uV)
698 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
699 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
701 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV);
704 static int wm831x_alive_ldo_get_voltage(struct regulator_dev *rdev)
706 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
707 struct wm831x *wm831x = ldo->wm831x;
708 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
709 int ret;
711 ret = wm831x_reg_read(wm831x, reg);
712 if (ret < 0)
713 return ret;
715 ret &= WM831X_LDO11_ON_VSEL_MASK;
717 return wm831x_alive_ldo_list_voltage(rdev, ret);
720 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
722 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
723 struct wm831x *wm831x = ldo->wm831x;
724 int mask = 1 << rdev_get_id(rdev);
725 int ret;
727 /* Is the regulator on? */
728 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
729 if (ret < 0)
730 return ret;
731 if (ret & mask)
732 return REGULATOR_STATUS_ON;
733 else
734 return REGULATOR_STATUS_OFF;
737 static struct regulator_ops wm831x_alive_ldo_ops = {
738 .list_voltage = wm831x_alive_ldo_list_voltage,
739 .get_voltage = wm831x_alive_ldo_get_voltage,
740 .set_voltage = wm831x_alive_ldo_set_voltage,
741 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
742 .get_status = wm831x_alive_ldo_get_status,
744 .is_enabled = wm831x_ldo_is_enabled,
745 .enable = wm831x_ldo_enable,
746 .disable = wm831x_ldo_disable,
749 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
751 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
752 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
753 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
754 struct wm831x_ldo *ldo;
755 struct resource *res;
756 int ret;
758 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
760 if (pdata == NULL || pdata->ldo[id] == NULL)
761 return -ENODEV;
763 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
764 if (ldo == NULL) {
765 dev_err(&pdev->dev, "Unable to allocate private data\n");
766 return -ENOMEM;
769 ldo->wm831x = wm831x;
771 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
772 if (res == NULL) {
773 dev_err(&pdev->dev, "No I/O resource\n");
774 ret = -EINVAL;
775 goto err;
777 ldo->base = res->start;
779 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
780 ldo->desc.name = ldo->name;
781 ldo->desc.id = id;
782 ldo->desc.type = REGULATOR_VOLTAGE;
783 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
784 ldo->desc.ops = &wm831x_alive_ldo_ops;
785 ldo->desc.owner = THIS_MODULE;
787 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
788 pdata->ldo[id], ldo);
789 if (IS_ERR(ldo->regulator)) {
790 ret = PTR_ERR(ldo->regulator);
791 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
792 id + 1, ret);
793 goto err;
796 platform_set_drvdata(pdev, ldo);
798 return 0;
800 err:
801 kfree(ldo);
802 return ret;
805 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
807 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
809 regulator_unregister(ldo->regulator);
810 kfree(ldo);
812 return 0;
815 static struct platform_driver wm831x_alive_ldo_driver = {
816 .probe = wm831x_alive_ldo_probe,
817 .remove = __devexit_p(wm831x_alive_ldo_remove),
818 .driver = {
819 .name = "wm831x-alive-ldo",
820 .owner = THIS_MODULE,
824 static int __init wm831x_ldo_init(void)
826 int ret;
828 ret = platform_driver_register(&wm831x_gp_ldo_driver);
829 if (ret != 0)
830 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
832 ret = platform_driver_register(&wm831x_aldo_driver);
833 if (ret != 0)
834 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
836 ret = platform_driver_register(&wm831x_alive_ldo_driver);
837 if (ret != 0)
838 pr_err("Failed to register WM831x alive LDO driver: %d\n",
839 ret);
841 return 0;
843 subsys_initcall(wm831x_ldo_init);
845 static void __exit wm831x_ldo_exit(void)
847 platform_driver_unregister(&wm831x_alive_ldo_driver);
848 platform_driver_unregister(&wm831x_aldo_driver);
849 platform_driver_unregister(&wm831x_gp_ldo_driver);
851 module_exit(wm831x_ldo_exit);
853 /* Module information */
854 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
855 MODULE_DESCRIPTION("WM831x LDO driver");
856 MODULE_LICENSE("GPL");
857 MODULE_ALIAS("platform:wm831x-ldo");
858 MODULE_ALIAS("platform:wm831x-aldo");
859 MODULE_ALIAS("platform:wm831x-aliveldo");