rcu: Fix bogus CONFIG_PROVE_LOCKING in comments to reflect reality.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / wm831x-ldo.c
blobe686cdb61b97cd8a54ab7fbecb1312a41e333595
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,
219 WM831X_LDO1_LP_MODE);
220 if (ret < 0)
221 return ret;
223 ret = wm831x_set_bits(wm831x, on_reg,
224 WM831X_LDO1_ON_MODE,
225 WM831X_LDO1_ON_MODE);
226 if (ret < 0)
227 return ret;
229 case REGULATOR_MODE_STANDBY:
230 ret = wm831x_set_bits(wm831x, ctrl_reg,
231 WM831X_LDO1_LP_MODE, 0);
232 if (ret < 0)
233 return ret;
235 ret = wm831x_set_bits(wm831x, on_reg,
236 WM831X_LDO1_ON_MODE,
237 WM831X_LDO1_ON_MODE);
238 if (ret < 0)
239 return ret;
240 break;
242 default:
243 return -EINVAL;
246 return 0;
249 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
251 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
252 struct wm831x *wm831x = ldo->wm831x;
253 int mask = 1 << rdev_get_id(rdev);
254 int ret;
256 /* Is the regulator on? */
257 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
258 if (ret < 0)
259 return ret;
260 if (!(ret & mask))
261 return REGULATOR_STATUS_OFF;
263 /* Is it reporting under voltage? */
264 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
265 if (ret & mask)
266 return REGULATOR_STATUS_ERROR;
268 ret = wm831x_gp_ldo_get_mode(rdev);
269 if (ret < 0)
270 return ret;
271 else
272 return regulator_mode_to_status(ret);
275 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
276 int input_uV,
277 int output_uV, int load_uA)
279 if (load_uA < 20000)
280 return REGULATOR_MODE_STANDBY;
281 if (load_uA < 50000)
282 return REGULATOR_MODE_IDLE;
283 return REGULATOR_MODE_NORMAL;
287 static struct regulator_ops wm831x_gp_ldo_ops = {
288 .list_voltage = wm831x_gp_ldo_list_voltage,
289 .get_voltage = wm831x_gp_ldo_get_voltage,
290 .set_voltage = wm831x_gp_ldo_set_voltage,
291 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
292 .get_mode = wm831x_gp_ldo_get_mode,
293 .set_mode = wm831x_gp_ldo_set_mode,
294 .get_status = wm831x_gp_ldo_get_status,
295 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
297 .is_enabled = wm831x_ldo_is_enabled,
298 .enable = wm831x_ldo_enable,
299 .disable = wm831x_ldo_disable,
302 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
304 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
305 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
306 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
307 struct wm831x_ldo *ldo;
308 struct resource *res;
309 int ret, irq;
311 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
313 if (pdata == NULL || pdata->ldo[id] == NULL)
314 return -ENODEV;
316 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
317 if (ldo == NULL) {
318 dev_err(&pdev->dev, "Unable to allocate private data\n");
319 return -ENOMEM;
322 ldo->wm831x = wm831x;
324 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
325 if (res == NULL) {
326 dev_err(&pdev->dev, "No I/O resource\n");
327 ret = -EINVAL;
328 goto err;
330 ldo->base = res->start;
332 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
333 ldo->desc.name = ldo->name;
334 ldo->desc.id = id;
335 ldo->desc.type = REGULATOR_VOLTAGE;
336 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
337 ldo->desc.ops = &wm831x_gp_ldo_ops;
338 ldo->desc.owner = THIS_MODULE;
340 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
341 pdata->ldo[id], ldo);
342 if (IS_ERR(ldo->regulator)) {
343 ret = PTR_ERR(ldo->regulator);
344 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
345 id + 1, ret);
346 goto err;
349 irq = platform_get_irq_byname(pdev, "UV");
350 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
351 IRQF_TRIGGER_RISING, ldo->name,
352 ldo);
353 if (ret != 0) {
354 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
355 irq, ret);
356 goto err_regulator;
359 platform_set_drvdata(pdev, ldo);
361 return 0;
363 err_regulator:
364 regulator_unregister(ldo->regulator);
365 err:
366 kfree(ldo);
367 return ret;
370 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
372 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
373 struct wm831x *wm831x = ldo->wm831x;
375 platform_set_drvdata(pdev, NULL);
377 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
378 regulator_unregister(ldo->regulator);
379 kfree(ldo);
381 return 0;
384 static struct platform_driver wm831x_gp_ldo_driver = {
385 .probe = wm831x_gp_ldo_probe,
386 .remove = __devexit_p(wm831x_gp_ldo_remove),
387 .driver = {
388 .name = "wm831x-ldo",
389 .owner = THIS_MODULE,
394 * Analogue LDOs
398 #define WM831X_ALDO_SELECTOR_LOW 0xc
399 #define WM831X_ALDO_MAX_SELECTOR 0x1f
401 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
402 unsigned int selector)
404 /* 1-1.6V in 50mV steps */
405 if (selector <= WM831X_ALDO_SELECTOR_LOW)
406 return 1000000 + (selector * 50000);
407 /* 1.7-3.5V in 50mV steps */
408 if (selector <= WM831X_ALDO_MAX_SELECTOR)
409 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
410 * 100000);
411 return -EINVAL;
414 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
415 int min_uV, int max_uV)
417 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
418 struct wm831x *wm831x = ldo->wm831x;
419 int vsel, ret;
421 if (min_uV < 1000000)
422 vsel = 0;
423 else if (min_uV < 1700000)
424 vsel = ((min_uV - 1000000) / 50000);
425 else
426 vsel = ((min_uV - 1700000) / 100000)
427 + WM831X_ALDO_SELECTOR_LOW + 1;
429 ret = wm831x_aldo_list_voltage(rdev, vsel);
430 if (ret < 0)
431 return ret;
432 if (ret < min_uV || ret > max_uV)
433 return -EINVAL;
435 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
438 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
439 int min_uV, int max_uV)
441 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
442 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
444 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV);
447 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
448 int uV)
450 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
451 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
453 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV);
456 static int wm831x_aldo_get_voltage(struct regulator_dev *rdev)
458 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
459 struct wm831x *wm831x = ldo->wm831x;
460 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
461 int ret;
463 ret = wm831x_reg_read(wm831x, reg);
464 if (ret < 0)
465 return ret;
467 ret &= WM831X_LDO7_ON_VSEL_MASK;
469 return wm831x_aldo_list_voltage(rdev, ret);
472 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
474 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
475 struct wm831x *wm831x = ldo->wm831x;
476 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
477 int ret;
479 ret = wm831x_reg_read(wm831x, on_reg);
480 if (ret < 0)
481 return 0;
483 if (ret & WM831X_LDO7_ON_MODE)
484 return REGULATOR_MODE_IDLE;
485 else
486 return REGULATOR_MODE_NORMAL;
489 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
490 unsigned int mode)
492 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
493 struct wm831x *wm831x = ldo->wm831x;
494 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
495 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
496 int ret;
499 switch (mode) {
500 case REGULATOR_MODE_NORMAL:
501 ret = wm831x_set_bits(wm831x, on_reg,
502 WM831X_LDO7_ON_MODE, 0);
503 if (ret < 0)
504 return ret;
505 break;
507 case REGULATOR_MODE_IDLE:
508 ret = wm831x_set_bits(wm831x, ctrl_reg,
509 WM831X_LDO7_ON_MODE,
510 WM831X_LDO7_ON_MODE);
511 if (ret < 0)
512 return ret;
513 break;
515 default:
516 return -EINVAL;
519 return 0;
522 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
524 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
525 struct wm831x *wm831x = ldo->wm831x;
526 int mask = 1 << rdev_get_id(rdev);
527 int ret;
529 /* Is the regulator on? */
530 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
531 if (ret < 0)
532 return ret;
533 if (!(ret & mask))
534 return REGULATOR_STATUS_OFF;
536 /* Is it reporting under voltage? */
537 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
538 if (ret & mask)
539 return REGULATOR_STATUS_ERROR;
541 ret = wm831x_aldo_get_mode(rdev);
542 if (ret < 0)
543 return ret;
544 else
545 return regulator_mode_to_status(ret);
548 static struct regulator_ops wm831x_aldo_ops = {
549 .list_voltage = wm831x_aldo_list_voltage,
550 .get_voltage = wm831x_aldo_get_voltage,
551 .set_voltage = wm831x_aldo_set_voltage,
552 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
553 .get_mode = wm831x_aldo_get_mode,
554 .set_mode = wm831x_aldo_set_mode,
555 .get_status = wm831x_aldo_get_status,
557 .is_enabled = wm831x_ldo_is_enabled,
558 .enable = wm831x_ldo_enable,
559 .disable = wm831x_ldo_disable,
562 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
564 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
565 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
566 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
567 struct wm831x_ldo *ldo;
568 struct resource *res;
569 int ret, irq;
571 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
573 if (pdata == NULL || pdata->ldo[id] == NULL)
574 return -ENODEV;
576 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
577 if (ldo == NULL) {
578 dev_err(&pdev->dev, "Unable to allocate private data\n");
579 return -ENOMEM;
582 ldo->wm831x = wm831x;
584 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
585 if (res == NULL) {
586 dev_err(&pdev->dev, "No I/O resource\n");
587 ret = -EINVAL;
588 goto err;
590 ldo->base = res->start;
592 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
593 ldo->desc.name = ldo->name;
594 ldo->desc.id = id;
595 ldo->desc.type = REGULATOR_VOLTAGE;
596 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
597 ldo->desc.ops = &wm831x_aldo_ops;
598 ldo->desc.owner = THIS_MODULE;
600 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
601 pdata->ldo[id], ldo);
602 if (IS_ERR(ldo->regulator)) {
603 ret = PTR_ERR(ldo->regulator);
604 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
605 id + 1, ret);
606 goto err;
609 irq = platform_get_irq_byname(pdev, "UV");
610 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
611 IRQF_TRIGGER_RISING, ldo->name,
612 ldo);
613 if (ret != 0) {
614 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
615 irq, ret);
616 goto err_regulator;
619 platform_set_drvdata(pdev, ldo);
621 return 0;
623 err_regulator:
624 regulator_unregister(ldo->regulator);
625 err:
626 kfree(ldo);
627 return ret;
630 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
632 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
633 struct wm831x *wm831x = ldo->wm831x;
635 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
636 regulator_unregister(ldo->regulator);
637 kfree(ldo);
639 return 0;
642 static struct platform_driver wm831x_aldo_driver = {
643 .probe = wm831x_aldo_probe,
644 .remove = __devexit_p(wm831x_aldo_remove),
645 .driver = {
646 .name = "wm831x-aldo",
647 .owner = THIS_MODULE,
652 * Alive LDO
655 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
657 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
658 unsigned int selector)
660 /* 0.8-1.55V in 50mV steps */
661 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
662 return 800000 + (selector * 50000);
663 return -EINVAL;
666 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
667 int reg,
668 int min_uV, int max_uV)
670 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
671 struct wm831x *wm831x = ldo->wm831x;
672 int vsel, ret;
674 vsel = (min_uV - 800000) / 50000;
676 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
677 if (ret < 0)
678 return ret;
679 if (ret < min_uV || ret > max_uV)
680 return -EINVAL;
682 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
685 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
686 int min_uV, int max_uV)
688 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
689 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
691 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
694 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
695 int uV)
697 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
698 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
700 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV);
703 static int wm831x_alive_ldo_get_voltage(struct regulator_dev *rdev)
705 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
706 struct wm831x *wm831x = ldo->wm831x;
707 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
708 int ret;
710 ret = wm831x_reg_read(wm831x, reg);
711 if (ret < 0)
712 return ret;
714 ret &= WM831X_LDO11_ON_VSEL_MASK;
716 return wm831x_alive_ldo_list_voltage(rdev, ret);
719 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
721 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
722 struct wm831x *wm831x = ldo->wm831x;
723 int mask = 1 << rdev_get_id(rdev);
724 int ret;
726 /* Is the regulator on? */
727 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
728 if (ret < 0)
729 return ret;
730 if (ret & mask)
731 return REGULATOR_STATUS_ON;
732 else
733 return REGULATOR_STATUS_OFF;
736 static struct regulator_ops wm831x_alive_ldo_ops = {
737 .list_voltage = wm831x_alive_ldo_list_voltage,
738 .get_voltage = wm831x_alive_ldo_get_voltage,
739 .set_voltage = wm831x_alive_ldo_set_voltage,
740 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
741 .get_status = wm831x_alive_ldo_get_status,
743 .is_enabled = wm831x_ldo_is_enabled,
744 .enable = wm831x_ldo_enable,
745 .disable = wm831x_ldo_disable,
748 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
750 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
751 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
752 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
753 struct wm831x_ldo *ldo;
754 struct resource *res;
755 int ret;
757 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
759 if (pdata == NULL || pdata->ldo[id] == NULL)
760 return -ENODEV;
762 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
763 if (ldo == NULL) {
764 dev_err(&pdev->dev, "Unable to allocate private data\n");
765 return -ENOMEM;
768 ldo->wm831x = wm831x;
770 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
771 if (res == NULL) {
772 dev_err(&pdev->dev, "No I/O resource\n");
773 ret = -EINVAL;
774 goto err;
776 ldo->base = res->start;
778 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
779 ldo->desc.name = ldo->name;
780 ldo->desc.id = id;
781 ldo->desc.type = REGULATOR_VOLTAGE;
782 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
783 ldo->desc.ops = &wm831x_alive_ldo_ops;
784 ldo->desc.owner = THIS_MODULE;
786 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
787 pdata->ldo[id], ldo);
788 if (IS_ERR(ldo->regulator)) {
789 ret = PTR_ERR(ldo->regulator);
790 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
791 id + 1, ret);
792 goto err;
795 platform_set_drvdata(pdev, ldo);
797 return 0;
799 err:
800 kfree(ldo);
801 return ret;
804 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
806 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
808 regulator_unregister(ldo->regulator);
809 kfree(ldo);
811 return 0;
814 static struct platform_driver wm831x_alive_ldo_driver = {
815 .probe = wm831x_alive_ldo_probe,
816 .remove = __devexit_p(wm831x_alive_ldo_remove),
817 .driver = {
818 .name = "wm831x-alive-ldo",
819 .owner = THIS_MODULE,
823 static int __init wm831x_ldo_init(void)
825 int ret;
827 ret = platform_driver_register(&wm831x_gp_ldo_driver);
828 if (ret != 0)
829 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
831 ret = platform_driver_register(&wm831x_aldo_driver);
832 if (ret != 0)
833 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
835 ret = platform_driver_register(&wm831x_alive_ldo_driver);
836 if (ret != 0)
837 pr_err("Failed to register WM831x alive LDO driver: %d\n",
838 ret);
840 return 0;
842 subsys_initcall(wm831x_ldo_init);
844 static void __exit wm831x_ldo_exit(void)
846 platform_driver_unregister(&wm831x_alive_ldo_driver);
847 platform_driver_unregister(&wm831x_aldo_driver);
848 platform_driver_unregister(&wm831x_gp_ldo_driver);
850 module_exit(wm831x_ldo_exit);
852 /* Module information */
853 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
854 MODULE_DESCRIPTION("WM831x LDO driver");
855 MODULE_LICENSE("GPL");
856 MODULE_ALIAS("platform:wm831x-ldo");
857 MODULE_ALIAS("platform:wm831x-aldo");
858 MODULE_ALIAS("platform:wm831x-aliveldo");