FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / regulator / wm831x-ldo.c
blobc94fc5b7cd5b8ab22ef64129a7118e53f2380091
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,
117 unsigned *selector)
119 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
120 struct wm831x *wm831x = ldo->wm831x;
121 int vsel, ret;
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;
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;
137 *selector = vsel;
139 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
142 static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
143 int min_uV, int max_uV,
144 unsigned *selector)
146 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
147 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
149 return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
150 selector);
153 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
154 int uV)
156 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
157 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
158 unsigned int selector;
160 return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
163 static int wm831x_gp_ldo_get_voltage_sel(struct regulator_dev *rdev)
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;
170 ret = wm831x_reg_read(wm831x, reg);
171 if (ret < 0)
172 return ret;
174 ret &= WM831X_LDO1_ON_VSEL_MASK;
176 return ret;
179 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
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;
187 ret = wm831x_reg_read(wm831x, on_reg);
188 if (ret < 0)
189 return ret;
191 if (!(ret & WM831X_LDO1_ON_MODE))
192 return REGULATOR_MODE_NORMAL;
194 ret = wm831x_reg_read(wm831x, ctrl_reg);
195 if (ret < 0)
196 return ret;
198 if (ret & WM831X_LDO1_LP_MODE)
199 return REGULATOR_MODE_STANDBY;
200 else
201 return REGULATOR_MODE_IDLE;
204 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
205 unsigned int mode)
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;
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;
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;
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;
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;
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;
249 default:
250 return -EINVAL;
253 return 0;
256 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
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;
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;
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;
275 ret = wm831x_gp_ldo_get_mode(rdev);
276 if (ret < 0)
277 return ret;
278 else
279 return regulator_mode_to_status(ret);
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)
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;
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,
304 .is_enabled = wm831x_ldo_is_enabled,
305 .enable = wm831x_ldo_enable,
306 .disable = wm831x_ldo_disable,
309 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
311 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
312 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
313 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
314 struct wm831x_ldo *ldo;
315 struct resource *res;
316 int ret, irq;
318 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
320 if (pdata == NULL || pdata->ldo[id] == NULL)
321 return -ENODEV;
323 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
324 if (ldo == NULL) {
325 dev_err(&pdev->dev, "Unable to allocate private data\n");
326 return -ENOMEM;
329 ldo->wm831x = wm831x;
331 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
332 if (res == NULL) {
333 dev_err(&pdev->dev, "No I/O resource\n");
334 ret = -EINVAL;
335 goto err;
337 ldo->base = res->start;
339 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
340 ldo->desc.name = ldo->name;
341 ldo->desc.id = id;
342 ldo->desc.type = REGULATOR_VOLTAGE;
343 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
344 ldo->desc.ops = &wm831x_gp_ldo_ops;
345 ldo->desc.owner = THIS_MODULE;
347 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
348 pdata->ldo[id], ldo);
349 if (IS_ERR(ldo->regulator)) {
350 ret = PTR_ERR(ldo->regulator);
351 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
352 id + 1, ret);
353 goto err;
356 irq = platform_get_irq_byname(pdev, "UV");
357 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
358 IRQF_TRIGGER_RISING, ldo->name,
359 ldo);
360 if (ret != 0) {
361 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
362 irq, ret);
363 goto err_regulator;
366 platform_set_drvdata(pdev, ldo);
368 return 0;
370 err_regulator:
371 regulator_unregister(ldo->regulator);
372 err:
373 kfree(ldo);
374 return ret;
377 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
379 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
380 struct wm831x *wm831x = ldo->wm831x;
382 platform_set_drvdata(pdev, NULL);
384 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
385 regulator_unregister(ldo->regulator);
386 kfree(ldo);
388 return 0;
391 static struct platform_driver wm831x_gp_ldo_driver = {
392 .probe = wm831x_gp_ldo_probe,
393 .remove = __devexit_p(wm831x_gp_ldo_remove),
394 .driver = {
395 .name = "wm831x-ldo",
396 .owner = THIS_MODULE,
401 * Analogue LDOs
405 #define WM831X_ALDO_SELECTOR_LOW 0xc
406 #define WM831X_ALDO_MAX_SELECTOR 0x1f
408 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
409 unsigned int selector)
411 /* 1-1.6V in 50mV steps */
412 if (selector <= WM831X_ALDO_SELECTOR_LOW)
413 return 1000000 + (selector * 50000);
414 /* 1.7-3.5V in 50mV steps */
415 if (selector <= WM831X_ALDO_MAX_SELECTOR)
416 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
417 * 100000);
418 return -EINVAL;
421 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
422 int min_uV, int max_uV,
423 unsigned *selector)
425 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
426 struct wm831x *wm831x = ldo->wm831x;
427 int vsel, ret;
429 if (min_uV < 1000000)
430 vsel = 0;
431 else if (min_uV < 1700000)
432 vsel = ((min_uV - 1000000) / 50000);
433 else
434 vsel = ((min_uV - 1700000) / 100000)
435 + WM831X_ALDO_SELECTOR_LOW + 1;
437 ret = wm831x_aldo_list_voltage(rdev, vsel);
438 if (ret < 0)
439 return ret;
440 if (ret < min_uV || ret > max_uV)
441 return -EINVAL;
443 *selector = vsel;
445 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
448 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
449 int min_uV, int max_uV, unsigned *selector)
451 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
452 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
454 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
455 selector);
458 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
459 int uV)
461 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
462 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
463 unsigned int selector;
465 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
468 static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
470 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
471 struct wm831x *wm831x = ldo->wm831x;
472 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
473 int ret;
475 ret = wm831x_reg_read(wm831x, reg);
476 if (ret < 0)
477 return ret;
479 ret &= WM831X_LDO7_ON_VSEL_MASK;
481 return ret;
484 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
486 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
487 struct wm831x *wm831x = ldo->wm831x;
488 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
489 int ret;
491 ret = wm831x_reg_read(wm831x, on_reg);
492 if (ret < 0)
493 return 0;
495 if (ret & WM831X_LDO7_ON_MODE)
496 return REGULATOR_MODE_IDLE;
497 else
498 return REGULATOR_MODE_NORMAL;
501 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
502 unsigned int mode)
504 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
505 struct wm831x *wm831x = ldo->wm831x;
506 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
507 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
508 int ret;
511 switch (mode) {
512 case REGULATOR_MODE_NORMAL:
513 ret = wm831x_set_bits(wm831x, on_reg,
514 WM831X_LDO7_ON_MODE, 0);
515 if (ret < 0)
516 return ret;
517 break;
519 case REGULATOR_MODE_IDLE:
520 ret = wm831x_set_bits(wm831x, ctrl_reg,
521 WM831X_LDO7_ON_MODE,
522 WM831X_LDO7_ON_MODE);
523 if (ret < 0)
524 return ret;
525 break;
527 default:
528 return -EINVAL;
531 return 0;
534 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
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;
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;
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;
553 ret = wm831x_aldo_get_mode(rdev);
554 if (ret < 0)
555 return ret;
556 else
557 return regulator_mode_to_status(ret);
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,
569 .is_enabled = wm831x_ldo_is_enabled,
570 .enable = wm831x_ldo_enable,
571 .disable = wm831x_ldo_disable,
574 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
576 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
577 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
578 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
579 struct wm831x_ldo *ldo;
580 struct resource *res;
581 int ret, irq;
583 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
585 if (pdata == NULL || pdata->ldo[id] == NULL)
586 return -ENODEV;
588 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
589 if (ldo == NULL) {
590 dev_err(&pdev->dev, "Unable to allocate private data\n");
591 return -ENOMEM;
594 ldo->wm831x = wm831x;
596 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
597 if (res == NULL) {
598 dev_err(&pdev->dev, "No I/O resource\n");
599 ret = -EINVAL;
600 goto err;
602 ldo->base = res->start;
604 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
605 ldo->desc.name = ldo->name;
606 ldo->desc.id = id;
607 ldo->desc.type = REGULATOR_VOLTAGE;
608 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
609 ldo->desc.ops = &wm831x_aldo_ops;
610 ldo->desc.owner = THIS_MODULE;
612 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
613 pdata->ldo[id], ldo);
614 if (IS_ERR(ldo->regulator)) {
615 ret = PTR_ERR(ldo->regulator);
616 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
617 id + 1, ret);
618 goto err;
621 irq = platform_get_irq_byname(pdev, "UV");
622 ret = wm831x_request_irq(wm831x, irq, wm831x_ldo_uv_irq,
623 IRQF_TRIGGER_RISING, ldo->name,
624 ldo);
625 if (ret != 0) {
626 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
627 irq, ret);
628 goto err_regulator;
631 platform_set_drvdata(pdev, ldo);
633 return 0;
635 err_regulator:
636 regulator_unregister(ldo->regulator);
637 err:
638 kfree(ldo);
639 return ret;
642 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
644 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
645 struct wm831x *wm831x = ldo->wm831x;
647 wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), ldo);
648 regulator_unregister(ldo->regulator);
649 kfree(ldo);
651 return 0;
654 static struct platform_driver wm831x_aldo_driver = {
655 .probe = wm831x_aldo_probe,
656 .remove = __devexit_p(wm831x_aldo_remove),
657 .driver = {
658 .name = "wm831x-aldo",
659 .owner = THIS_MODULE,
664 * Alive LDO
667 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
669 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
670 unsigned int selector)
672 /* 0.8-1.55V in 50mV steps */
673 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
674 return 800000 + (selector * 50000);
675 return -EINVAL;
678 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
679 int reg,
680 int min_uV, int max_uV,
681 unsigned *selector)
683 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
684 struct wm831x *wm831x = ldo->wm831x;
685 int vsel, ret;
687 vsel = (min_uV - 800000) / 50000;
689 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
690 if (ret < 0)
691 return ret;
692 if (ret < min_uV || ret > max_uV)
693 return -EINVAL;
695 *selector = vsel;
697 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
700 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
701 int min_uV, int max_uV,
702 unsigned *selector)
704 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
705 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
707 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
708 selector);
711 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
712 int uV)
714 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
715 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
716 unsigned selector;
718 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
721 static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
723 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
724 struct wm831x *wm831x = ldo->wm831x;
725 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
726 int ret;
728 ret = wm831x_reg_read(wm831x, reg);
729 if (ret < 0)
730 return ret;
732 ret &= WM831X_LDO11_ON_VSEL_MASK;
734 return ret;
737 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
739 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
740 struct wm831x *wm831x = ldo->wm831x;
741 int mask = 1 << rdev_get_id(rdev);
742 int ret;
744 /* Is the regulator on? */
745 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
746 if (ret < 0)
747 return ret;
748 if (ret & mask)
749 return REGULATOR_STATUS_ON;
750 else
751 return REGULATOR_STATUS_OFF;
754 static struct regulator_ops wm831x_alive_ldo_ops = {
755 .list_voltage = wm831x_alive_ldo_list_voltage,
756 .get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
757 .set_voltage = wm831x_alive_ldo_set_voltage,
758 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
759 .get_status = wm831x_alive_ldo_get_status,
761 .is_enabled = wm831x_ldo_is_enabled,
762 .enable = wm831x_ldo_enable,
763 .disable = wm831x_ldo_disable,
766 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
768 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
769 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
770 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
771 struct wm831x_ldo *ldo;
772 struct resource *res;
773 int ret;
775 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
777 if (pdata == NULL || pdata->ldo[id] == NULL)
778 return -ENODEV;
780 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
781 if (ldo == NULL) {
782 dev_err(&pdev->dev, "Unable to allocate private data\n");
783 return -ENOMEM;
786 ldo->wm831x = wm831x;
788 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
789 if (res == NULL) {
790 dev_err(&pdev->dev, "No I/O resource\n");
791 ret = -EINVAL;
792 goto err;
794 ldo->base = res->start;
796 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
797 ldo->desc.name = ldo->name;
798 ldo->desc.id = id;
799 ldo->desc.type = REGULATOR_VOLTAGE;
800 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
801 ldo->desc.ops = &wm831x_alive_ldo_ops;
802 ldo->desc.owner = THIS_MODULE;
804 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
805 pdata->ldo[id], ldo);
806 if (IS_ERR(ldo->regulator)) {
807 ret = PTR_ERR(ldo->regulator);
808 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
809 id + 1, ret);
810 goto err;
813 platform_set_drvdata(pdev, ldo);
815 return 0;
817 err:
818 kfree(ldo);
819 return ret;
822 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
824 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
826 regulator_unregister(ldo->regulator);
827 kfree(ldo);
829 return 0;
832 static struct platform_driver wm831x_alive_ldo_driver = {
833 .probe = wm831x_alive_ldo_probe,
834 .remove = __devexit_p(wm831x_alive_ldo_remove),
835 .driver = {
836 .name = "wm831x-alive-ldo",
837 .owner = THIS_MODULE,
841 static int __init wm831x_ldo_init(void)
843 int ret;
845 ret = platform_driver_register(&wm831x_gp_ldo_driver);
846 if (ret != 0)
847 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
849 ret = platform_driver_register(&wm831x_aldo_driver);
850 if (ret != 0)
851 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
853 ret = platform_driver_register(&wm831x_alive_ldo_driver);
854 if (ret != 0)
855 pr_err("Failed to register WM831x alive LDO driver: %d\n",
856 ret);
858 return 0;
860 subsys_initcall(wm831x_ldo_init);
862 static void __exit wm831x_ldo_exit(void)
864 platform_driver_unregister(&wm831x_alive_ldo_driver);
865 platform_driver_unregister(&wm831x_aldo_driver);
866 platform_driver_unregister(&wm831x_gp_ldo_driver);
868 module_exit(wm831x_ldo_exit);
870 /* Module information */
871 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
872 MODULE_DESCRIPTION("WM831x LDO driver");
873 MODULE_LICENSE("GPL");
874 MODULE_ALIAS("platform:wm831x-ldo");
875 MODULE_ALIAS("platform:wm831x-aldo");
876 MODULE_ALIAS("platform:wm831x-aliveldo");