viafb: package often used basic io functions
[linux/fpc-iii.git] / drivers / regulator / wm831x-ldo.c
blobd2406c1519a195a16437561bd3e2fadde4859f9b
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>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/regulator.h>
25 #include <linux/mfd/wm831x/pdata.h>
27 #define WM831X_LDO_MAX_NAME 6
29 #define WM831X_LDO_CONTROL 0
30 #define WM831X_LDO_ON_CONTROL 1
31 #define WM831X_LDO_SLEEP_CONTROL 2
33 #define WM831X_ALIVE_LDO_ON_CONTROL 0
34 #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
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;
45 * Shared
48 static int wm831x_ldo_is_enabled(struct regulator_dev *rdev)
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;
55 reg = wm831x_reg_read(wm831x, WM831X_LDO_ENABLE);
56 if (reg < 0)
57 return reg;
59 if (reg & mask)
60 return 1;
61 else
62 return 0;
65 static int wm831x_ldo_enable(struct regulator_dev *rdev)
67 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
68 struct wm831x *wm831x = ldo->wm831x;
69 int mask = 1 << rdev_get_id(rdev);
71 return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, mask);
74 static int wm831x_ldo_disable(struct regulator_dev *rdev)
76 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
77 struct wm831x *wm831x = ldo->wm831x;
78 int mask = 1 << rdev_get_id(rdev);
80 return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, 0);
83 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
85 struct wm831x_ldo *ldo = data;
87 regulator_notifier_call_chain(ldo->regulator,
88 REGULATOR_EVENT_UNDER_VOLTAGE,
89 NULL);
91 return IRQ_HANDLED;
95 * General purpose LDOs
98 #define WM831X_GP_LDO_SELECTOR_LOW 0xe
99 #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
101 static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
102 unsigned int selector)
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;
114 static int wm831x_gp_ldo_set_voltage_int(struct regulator_dev *rdev, int reg,
115 int min_uV, int max_uV)
117 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
118 struct wm831x *wm831x = ldo->wm831x;
119 int vsel, ret;
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;
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;
135 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
138 static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
139 int min_uV, int max_uV)
141 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
142 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
144 return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
147 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
148 int uV)
150 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
151 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
153 return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV);
156 static int wm831x_gp_ldo_get_voltage(struct regulator_dev *rdev)
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;
163 ret = wm831x_reg_read(wm831x, reg);
164 if (ret < 0)
165 return ret;
167 ret &= WM831X_LDO1_ON_VSEL_MASK;
169 return wm831x_gp_ldo_list_voltage(rdev, ret);
172 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
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;
180 ret = wm831x_reg_read(wm831x, on_reg);
181 if (ret < 0)
182 return ret;
184 if (!(ret & WM831X_LDO1_ON_MODE))
185 return REGULATOR_MODE_NORMAL;
187 ret = wm831x_reg_read(wm831x, ctrl_reg);
188 if (ret < 0)
189 return ret;
191 if (ret & WM831X_LDO1_LP_MODE)
192 return REGULATOR_MODE_STANDBY;
193 else
194 return REGULATOR_MODE_IDLE;
197 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
198 unsigned int mode)
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;
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;
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;
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;
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;
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;
241 default:
242 return -EINVAL;
245 return 0;
248 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
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;
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;
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;
267 ret = wm831x_gp_ldo_get_mode(rdev);
268 if (ret < 0)
269 return ret;
270 else
271 return regulator_mode_to_status(ret);
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)
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;
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,
296 .is_enabled = wm831x_ldo_is_enabled,
297 .enable = wm831x_ldo_enable,
298 .disable = wm831x_ldo_disable,
301 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
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;
310 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
312 if (pdata == NULL || pdata->ldo[id] == NULL)
313 return -ENODEV;
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;
321 ldo->wm831x = wm831x;
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;
329 ldo->base = res->start;
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;
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;
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;
358 platform_set_drvdata(pdev, ldo);
360 return 0;
362 err_regulator:
363 regulator_unregister(ldo->regulator);
364 err:
365 kfree(ldo);
366 return ret;
369 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
371 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
372 struct wm831x *wm831x = ldo->wm831x;
374 platform_set_drvdata(pdev, NULL);
376 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
377 regulator_unregister(ldo->regulator);
378 kfree(ldo);
380 return 0;
383 static struct platform_driver wm831x_gp_ldo_driver = {
384 .probe = wm831x_gp_ldo_probe,
385 .remove = __devexit_p(wm831x_gp_ldo_remove),
386 .driver = {
387 .name = "wm831x-ldo",
388 .owner = THIS_MODULE,
393 * Analogue LDOs
397 #define WM831X_ALDO_SELECTOR_LOW 0xc
398 #define WM831X_ALDO_MAX_SELECTOR 0x1f
400 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
401 unsigned int selector)
403 /* 1-1.6V in 50mV steps */
404 if (selector <= WM831X_ALDO_SELECTOR_LOW)
405 return 1000000 + (selector * 50000);
406 /* 1.7-3.5V in 50mV steps */
407 if (selector <= WM831X_ALDO_MAX_SELECTOR)
408 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
409 * 100000);
410 return -EINVAL;
413 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
414 int min_uV, int max_uV)
416 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
417 struct wm831x *wm831x = ldo->wm831x;
418 int vsel, ret;
420 if (min_uV < 1000000)
421 vsel = 0;
422 else if (min_uV < 1700000)
423 vsel = ((min_uV - 1000000) / 50000);
424 else
425 vsel = ((min_uV - 1700000) / 100000)
426 + WM831X_ALDO_SELECTOR_LOW + 1;
428 ret = wm831x_aldo_list_voltage(rdev, vsel);
429 if (ret < 0)
430 return ret;
431 if (ret < min_uV || ret > max_uV)
432 return -EINVAL;
434 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
437 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
438 int min_uV, int max_uV)
440 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
441 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
443 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV);
446 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
447 int uV)
449 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
450 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
452 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV);
455 static int wm831x_aldo_get_voltage(struct regulator_dev *rdev)
457 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
458 struct wm831x *wm831x = ldo->wm831x;
459 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
460 int ret;
462 ret = wm831x_reg_read(wm831x, reg);
463 if (ret < 0)
464 return ret;
466 ret &= WM831X_LDO7_ON_VSEL_MASK;
468 return wm831x_aldo_list_voltage(rdev, ret);
471 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
473 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
474 struct wm831x *wm831x = ldo->wm831x;
475 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
476 int ret;
478 ret = wm831x_reg_read(wm831x, on_reg);
479 if (ret < 0)
480 return 0;
482 if (ret & WM831X_LDO7_ON_MODE)
483 return REGULATOR_MODE_IDLE;
484 else
485 return REGULATOR_MODE_NORMAL;
488 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
489 unsigned int mode)
491 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
492 struct wm831x *wm831x = ldo->wm831x;
493 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
494 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
495 int ret;
498 switch (mode) {
499 case REGULATOR_MODE_NORMAL:
500 ret = wm831x_set_bits(wm831x, on_reg,
501 WM831X_LDO7_ON_MODE, 0);
502 if (ret < 0)
503 return ret;
504 break;
506 case REGULATOR_MODE_IDLE:
507 ret = wm831x_set_bits(wm831x, ctrl_reg,
508 WM831X_LDO7_ON_MODE,
509 WM831X_LDO7_ON_MODE);
510 if (ret < 0)
511 return ret;
512 break;
514 default:
515 return -EINVAL;
518 return 0;
521 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
523 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
524 struct wm831x *wm831x = ldo->wm831x;
525 int mask = 1 << rdev_get_id(rdev);
526 int ret;
528 /* Is the regulator on? */
529 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
530 if (ret < 0)
531 return ret;
532 if (!(ret & mask))
533 return REGULATOR_STATUS_OFF;
535 /* Is it reporting under voltage? */
536 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
537 if (ret & mask)
538 return REGULATOR_STATUS_ERROR;
540 ret = wm831x_aldo_get_mode(rdev);
541 if (ret < 0)
542 return ret;
543 else
544 return regulator_mode_to_status(ret);
547 static struct regulator_ops wm831x_aldo_ops = {
548 .list_voltage = wm831x_aldo_list_voltage,
549 .get_voltage = wm831x_aldo_get_voltage,
550 .set_voltage = wm831x_aldo_set_voltage,
551 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
552 .get_mode = wm831x_aldo_get_mode,
553 .set_mode = wm831x_aldo_set_mode,
554 .get_status = wm831x_aldo_get_status,
556 .is_enabled = wm831x_ldo_is_enabled,
557 .enable = wm831x_ldo_enable,
558 .disable = wm831x_ldo_disable,
561 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
563 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
564 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
565 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
566 struct wm831x_ldo *ldo;
567 struct resource *res;
568 int ret, irq;
570 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
572 if (pdata == NULL || pdata->ldo[id] == NULL)
573 return -ENODEV;
575 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
576 if (ldo == NULL) {
577 dev_err(&pdev->dev, "Unable to allocate private data\n");
578 return -ENOMEM;
581 ldo->wm831x = wm831x;
583 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
584 if (res == NULL) {
585 dev_err(&pdev->dev, "No I/O resource\n");
586 ret = -EINVAL;
587 goto err;
589 ldo->base = res->start;
591 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
592 ldo->desc.name = ldo->name;
593 ldo->desc.id = id;
594 ldo->desc.type = REGULATOR_VOLTAGE;
595 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
596 ldo->desc.ops = &wm831x_aldo_ops;
597 ldo->desc.owner = THIS_MODULE;
599 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
600 pdata->ldo[id], ldo);
601 if (IS_ERR(ldo->regulator)) {
602 ret = PTR_ERR(ldo->regulator);
603 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
604 id + 1, ret);
605 goto err;
608 irq = platform_get_irq_byname(pdev, "UV");
609 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
610 IRQF_TRIGGER_RISING, ldo->name,
611 ldo);
612 if (ret != 0) {
613 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
614 irq, ret);
615 goto err_regulator;
618 platform_set_drvdata(pdev, ldo);
620 return 0;
622 err_regulator:
623 regulator_unregister(ldo->regulator);
624 err:
625 kfree(ldo);
626 return ret;
629 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
631 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
632 struct wm831x *wm831x = ldo->wm831x;
634 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
635 regulator_unregister(ldo->regulator);
636 kfree(ldo);
638 return 0;
641 static struct platform_driver wm831x_aldo_driver = {
642 .probe = wm831x_aldo_probe,
643 .remove = __devexit_p(wm831x_aldo_remove),
644 .driver = {
645 .name = "wm831x-aldo",
646 .owner = THIS_MODULE,
651 * Alive LDO
654 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
656 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
657 unsigned int selector)
659 /* 0.8-1.55V in 50mV steps */
660 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
661 return 800000 + (selector * 50000);
662 return -EINVAL;
665 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
666 int reg,
667 int min_uV, int max_uV)
669 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
670 struct wm831x *wm831x = ldo->wm831x;
671 int vsel, ret;
673 vsel = (min_uV - 800000) / 50000;
675 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
676 if (ret < 0)
677 return ret;
678 if (ret < min_uV || ret > max_uV)
679 return -EINVAL;
681 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
684 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
685 int min_uV, int max_uV)
687 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
688 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
690 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV);
693 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
694 int uV)
696 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
697 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
699 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV);
702 static int wm831x_alive_ldo_get_voltage(struct regulator_dev *rdev)
704 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
705 struct wm831x *wm831x = ldo->wm831x;
706 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
707 int ret;
709 ret = wm831x_reg_read(wm831x, reg);
710 if (ret < 0)
711 return ret;
713 ret &= WM831X_LDO11_ON_VSEL_MASK;
715 return wm831x_alive_ldo_list_voltage(rdev, ret);
718 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
720 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
721 struct wm831x *wm831x = ldo->wm831x;
722 int mask = 1 << rdev_get_id(rdev);
723 int ret;
725 /* Is the regulator on? */
726 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
727 if (ret < 0)
728 return ret;
729 if (ret & mask)
730 return REGULATOR_STATUS_ON;
731 else
732 return REGULATOR_STATUS_OFF;
735 static struct regulator_ops wm831x_alive_ldo_ops = {
736 .list_voltage = wm831x_alive_ldo_list_voltage,
737 .get_voltage = wm831x_alive_ldo_get_voltage,
738 .set_voltage = wm831x_alive_ldo_set_voltage,
739 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
740 .get_status = wm831x_alive_ldo_get_status,
742 .is_enabled = wm831x_ldo_is_enabled,
743 .enable = wm831x_ldo_enable,
744 .disable = wm831x_ldo_disable,
747 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
749 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
750 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
751 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
752 struct wm831x_ldo *ldo;
753 struct resource *res;
754 int ret;
756 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
758 if (pdata == NULL || pdata->ldo[id] == NULL)
759 return -ENODEV;
761 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
762 if (ldo == NULL) {
763 dev_err(&pdev->dev, "Unable to allocate private data\n");
764 return -ENOMEM;
767 ldo->wm831x = wm831x;
769 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
770 if (res == NULL) {
771 dev_err(&pdev->dev, "No I/O resource\n");
772 ret = -EINVAL;
773 goto err;
775 ldo->base = res->start;
777 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
778 ldo->desc.name = ldo->name;
779 ldo->desc.id = id;
780 ldo->desc.type = REGULATOR_VOLTAGE;
781 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
782 ldo->desc.ops = &wm831x_alive_ldo_ops;
783 ldo->desc.owner = THIS_MODULE;
785 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
786 pdata->ldo[id], ldo);
787 if (IS_ERR(ldo->regulator)) {
788 ret = PTR_ERR(ldo->regulator);
789 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
790 id + 1, ret);
791 goto err;
794 platform_set_drvdata(pdev, ldo);
796 return 0;
798 err:
799 kfree(ldo);
800 return ret;
803 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
805 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
807 regulator_unregister(ldo->regulator);
808 kfree(ldo);
810 return 0;
813 static struct platform_driver wm831x_alive_ldo_driver = {
814 .probe = wm831x_alive_ldo_probe,
815 .remove = __devexit_p(wm831x_alive_ldo_remove),
816 .driver = {
817 .name = "wm831x-alive-ldo",
818 .owner = THIS_MODULE,
822 static int __init wm831x_ldo_init(void)
824 int ret;
826 ret = platform_driver_register(&wm831x_gp_ldo_driver);
827 if (ret != 0)
828 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
830 ret = platform_driver_register(&wm831x_aldo_driver);
831 if (ret != 0)
832 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
834 ret = platform_driver_register(&wm831x_alive_ldo_driver);
835 if (ret != 0)
836 pr_err("Failed to register WM831x alive LDO driver: %d\n",
837 ret);
839 return 0;
841 subsys_initcall(wm831x_ldo_init);
843 static void __exit wm831x_ldo_exit(void)
845 platform_driver_unregister(&wm831x_alive_ldo_driver);
846 platform_driver_unregister(&wm831x_aldo_driver);
847 platform_driver_unregister(&wm831x_gp_ldo_driver);
849 module_exit(wm831x_ldo_exit);
851 /* Module information */
852 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
853 MODULE_DESCRIPTION("WM831x LDO driver");
854 MODULE_LICENSE("GPL");
855 MODULE_ALIAS("platform:wm831x-ldo");
856 MODULE_ALIAS("platform:wm831x-aldo");
857 MODULE_ALIAS("platform:wm831x-aliveldo");