The discovered bit in PGCCSR register indicates if the device has been
[linux-2.6/next.git] / drivers / regulator / wm831x-ldo.c
blob6709710a059e6e78c2267c376903b04ea26820fa
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;
314 struct wm831x_ldo *ldo;
315 struct resource *res;
316 int ret, irq;
318 if (pdata && pdata->wm831x_num)
319 id = (pdata->wm831x_num * 10) + 1;
320 else
321 id = 0;
322 id = pdev->id - id;
324 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
326 if (pdata == NULL || pdata->ldo[id] == NULL)
327 return -ENODEV;
329 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
330 if (ldo == NULL) {
331 dev_err(&pdev->dev, "Unable to allocate private data\n");
332 return -ENOMEM;
335 ldo->wm831x = wm831x;
337 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
338 if (res == NULL) {
339 dev_err(&pdev->dev, "No I/O resource\n");
340 ret = -EINVAL;
341 goto err;
343 ldo->base = res->start;
345 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
346 ldo->desc.name = ldo->name;
347 ldo->desc.id = id;
348 ldo->desc.type = REGULATOR_VOLTAGE;
349 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
350 ldo->desc.ops = &wm831x_gp_ldo_ops;
351 ldo->desc.owner = THIS_MODULE;
353 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
354 pdata->ldo[id], ldo);
355 if (IS_ERR(ldo->regulator)) {
356 ret = PTR_ERR(ldo->regulator);
357 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
358 id + 1, ret);
359 goto err;
362 irq = platform_get_irq_byname(pdev, "UV");
363 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
364 IRQF_TRIGGER_RISING, ldo->name,
365 ldo);
366 if (ret != 0) {
367 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
368 irq, ret);
369 goto err_regulator;
372 platform_set_drvdata(pdev, ldo);
374 return 0;
376 err_regulator:
377 regulator_unregister(ldo->regulator);
378 err:
379 kfree(ldo);
380 return ret;
383 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
385 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
387 platform_set_drvdata(pdev, NULL);
389 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
390 regulator_unregister(ldo->regulator);
391 kfree(ldo);
393 return 0;
396 static struct platform_driver wm831x_gp_ldo_driver = {
397 .probe = wm831x_gp_ldo_probe,
398 .remove = __devexit_p(wm831x_gp_ldo_remove),
399 .driver = {
400 .name = "wm831x-ldo",
401 .owner = THIS_MODULE,
406 * Analogue LDOs
410 #define WM831X_ALDO_SELECTOR_LOW 0xc
411 #define WM831X_ALDO_MAX_SELECTOR 0x1f
413 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
414 unsigned int selector)
416 /* 1-1.6V in 50mV steps */
417 if (selector <= WM831X_ALDO_SELECTOR_LOW)
418 return 1000000 + (selector * 50000);
419 /* 1.7-3.5V in 50mV steps */
420 if (selector <= WM831X_ALDO_MAX_SELECTOR)
421 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
422 * 100000);
423 return -EINVAL;
426 static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
427 int min_uV, int max_uV,
428 unsigned *selector)
430 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
431 struct wm831x *wm831x = ldo->wm831x;
432 int vsel, ret;
434 if (min_uV < 1000000)
435 vsel = 0;
436 else if (min_uV < 1700000)
437 vsel = ((min_uV - 1000000) / 50000);
438 else
439 vsel = ((min_uV - 1700000) / 100000)
440 + WM831X_ALDO_SELECTOR_LOW + 1;
442 ret = wm831x_aldo_list_voltage(rdev, vsel);
443 if (ret < 0)
444 return ret;
445 if (ret < min_uV || ret > max_uV)
446 return -EINVAL;
448 *selector = vsel;
450 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
453 static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
454 int min_uV, int max_uV, unsigned *selector)
456 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
457 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
459 return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
460 selector);
463 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
464 int uV)
466 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
467 int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
468 unsigned int selector;
470 return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
473 static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
475 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
476 struct wm831x *wm831x = ldo->wm831x;
477 int reg = ldo->base + WM831X_LDO_ON_CONTROL;
478 int ret;
480 ret = wm831x_reg_read(wm831x, reg);
481 if (ret < 0)
482 return ret;
484 ret &= WM831X_LDO7_ON_VSEL_MASK;
486 return ret;
489 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
491 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
492 struct wm831x *wm831x = ldo->wm831x;
493 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
494 int ret;
496 ret = wm831x_reg_read(wm831x, on_reg);
497 if (ret < 0)
498 return 0;
500 if (ret & WM831X_LDO7_ON_MODE)
501 return REGULATOR_MODE_IDLE;
502 else
503 return REGULATOR_MODE_NORMAL;
506 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
507 unsigned int mode)
509 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
510 struct wm831x *wm831x = ldo->wm831x;
511 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
512 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
513 int ret;
516 switch (mode) {
517 case REGULATOR_MODE_NORMAL:
518 ret = wm831x_set_bits(wm831x, on_reg,
519 WM831X_LDO7_ON_MODE, 0);
520 if (ret < 0)
521 return ret;
522 break;
524 case REGULATOR_MODE_IDLE:
525 ret = wm831x_set_bits(wm831x, ctrl_reg,
526 WM831X_LDO7_ON_MODE,
527 WM831X_LDO7_ON_MODE);
528 if (ret < 0)
529 return ret;
530 break;
532 default:
533 return -EINVAL;
536 return 0;
539 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
541 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
542 struct wm831x *wm831x = ldo->wm831x;
543 int mask = 1 << rdev_get_id(rdev);
544 int ret;
546 /* Is the regulator on? */
547 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
548 if (ret < 0)
549 return ret;
550 if (!(ret & mask))
551 return REGULATOR_STATUS_OFF;
553 /* Is it reporting under voltage? */
554 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
555 if (ret & mask)
556 return REGULATOR_STATUS_ERROR;
558 ret = wm831x_aldo_get_mode(rdev);
559 if (ret < 0)
560 return ret;
561 else
562 return regulator_mode_to_status(ret);
565 static struct regulator_ops wm831x_aldo_ops = {
566 .list_voltage = wm831x_aldo_list_voltage,
567 .get_voltage_sel = wm831x_aldo_get_voltage_sel,
568 .set_voltage = wm831x_aldo_set_voltage,
569 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
570 .get_mode = wm831x_aldo_get_mode,
571 .set_mode = wm831x_aldo_set_mode,
572 .get_status = wm831x_aldo_get_status,
574 .is_enabled = wm831x_ldo_is_enabled,
575 .enable = wm831x_ldo_enable,
576 .disable = wm831x_ldo_disable,
579 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
581 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
582 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
583 int id;
584 struct wm831x_ldo *ldo;
585 struct resource *res;
586 int ret, irq;
588 if (pdata && pdata->wm831x_num)
589 id = (pdata->wm831x_num * 10) + 1;
590 else
591 id = 0;
592 id = pdev->id - id;
594 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
596 if (pdata == NULL || pdata->ldo[id] == NULL)
597 return -ENODEV;
599 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
600 if (ldo == NULL) {
601 dev_err(&pdev->dev, "Unable to allocate private data\n");
602 return -ENOMEM;
605 ldo->wm831x = wm831x;
607 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
608 if (res == NULL) {
609 dev_err(&pdev->dev, "No I/O resource\n");
610 ret = -EINVAL;
611 goto err;
613 ldo->base = res->start;
615 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
616 ldo->desc.name = ldo->name;
617 ldo->desc.id = id;
618 ldo->desc.type = REGULATOR_VOLTAGE;
619 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
620 ldo->desc.ops = &wm831x_aldo_ops;
621 ldo->desc.owner = THIS_MODULE;
623 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
624 pdata->ldo[id], ldo);
625 if (IS_ERR(ldo->regulator)) {
626 ret = PTR_ERR(ldo->regulator);
627 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
628 id + 1, ret);
629 goto err;
632 irq = platform_get_irq_byname(pdev, "UV");
633 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
634 IRQF_TRIGGER_RISING, ldo->name, ldo);
635 if (ret != 0) {
636 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
637 irq, ret);
638 goto err_regulator;
641 platform_set_drvdata(pdev, ldo);
643 return 0;
645 err_regulator:
646 regulator_unregister(ldo->regulator);
647 err:
648 kfree(ldo);
649 return ret;
652 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
654 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
656 free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
657 regulator_unregister(ldo->regulator);
658 kfree(ldo);
660 return 0;
663 static struct platform_driver wm831x_aldo_driver = {
664 .probe = wm831x_aldo_probe,
665 .remove = __devexit_p(wm831x_aldo_remove),
666 .driver = {
667 .name = "wm831x-aldo",
668 .owner = THIS_MODULE,
673 * Alive LDO
676 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
678 static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
679 unsigned int selector)
681 /* 0.8-1.55V in 50mV steps */
682 if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
683 return 800000 + (selector * 50000);
684 return -EINVAL;
687 static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
688 int reg,
689 int min_uV, int max_uV,
690 unsigned *selector)
692 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
693 struct wm831x *wm831x = ldo->wm831x;
694 int vsel, ret;
696 vsel = (min_uV - 800000) / 50000;
698 ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
699 if (ret < 0)
700 return ret;
701 if (ret < min_uV || ret > max_uV)
702 return -EINVAL;
704 *selector = vsel;
706 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
709 static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
710 int min_uV, int max_uV,
711 unsigned *selector)
713 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
714 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
716 return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
717 selector);
720 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
721 int uV)
723 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
724 int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
725 unsigned selector;
727 return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
730 static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
732 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
733 struct wm831x *wm831x = ldo->wm831x;
734 int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
735 int ret;
737 ret = wm831x_reg_read(wm831x, reg);
738 if (ret < 0)
739 return ret;
741 ret &= WM831X_LDO11_ON_VSEL_MASK;
743 return ret;
746 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
748 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
749 struct wm831x *wm831x = ldo->wm831x;
750 int mask = 1 << rdev_get_id(rdev);
751 int ret;
753 /* Is the regulator on? */
754 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
755 if (ret < 0)
756 return ret;
757 if (ret & mask)
758 return REGULATOR_STATUS_ON;
759 else
760 return REGULATOR_STATUS_OFF;
763 static struct regulator_ops wm831x_alive_ldo_ops = {
764 .list_voltage = wm831x_alive_ldo_list_voltage,
765 .get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
766 .set_voltage = wm831x_alive_ldo_set_voltage,
767 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
768 .get_status = wm831x_alive_ldo_get_status,
770 .is_enabled = wm831x_ldo_is_enabled,
771 .enable = wm831x_ldo_enable,
772 .disable = wm831x_ldo_disable,
775 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
777 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
778 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
779 int id;
780 struct wm831x_ldo *ldo;
781 struct resource *res;
782 int ret;
784 if (pdata && pdata->wm831x_num)
785 id = (pdata->wm831x_num * 10) + 1;
786 else
787 id = 0;
788 id = pdev->id - id;
791 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
793 if (pdata == NULL || pdata->ldo[id] == NULL)
794 return -ENODEV;
796 ldo = kzalloc(sizeof(struct wm831x_ldo), GFP_KERNEL);
797 if (ldo == NULL) {
798 dev_err(&pdev->dev, "Unable to allocate private data\n");
799 return -ENOMEM;
802 ldo->wm831x = wm831x;
804 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
805 if (res == NULL) {
806 dev_err(&pdev->dev, "No I/O resource\n");
807 ret = -EINVAL;
808 goto err;
810 ldo->base = res->start;
812 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
813 ldo->desc.name = ldo->name;
814 ldo->desc.id = id;
815 ldo->desc.type = REGULATOR_VOLTAGE;
816 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
817 ldo->desc.ops = &wm831x_alive_ldo_ops;
818 ldo->desc.owner = THIS_MODULE;
820 ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
821 pdata->ldo[id], ldo);
822 if (IS_ERR(ldo->regulator)) {
823 ret = PTR_ERR(ldo->regulator);
824 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
825 id + 1, ret);
826 goto err;
829 platform_set_drvdata(pdev, ldo);
831 return 0;
833 err:
834 kfree(ldo);
835 return ret;
838 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
840 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
842 regulator_unregister(ldo->regulator);
843 kfree(ldo);
845 return 0;
848 static struct platform_driver wm831x_alive_ldo_driver = {
849 .probe = wm831x_alive_ldo_probe,
850 .remove = __devexit_p(wm831x_alive_ldo_remove),
851 .driver = {
852 .name = "wm831x-alive-ldo",
853 .owner = THIS_MODULE,
857 static int __init wm831x_ldo_init(void)
859 int ret;
861 ret = platform_driver_register(&wm831x_gp_ldo_driver);
862 if (ret != 0)
863 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
865 ret = platform_driver_register(&wm831x_aldo_driver);
866 if (ret != 0)
867 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
869 ret = platform_driver_register(&wm831x_alive_ldo_driver);
870 if (ret != 0)
871 pr_err("Failed to register WM831x alive LDO driver: %d\n",
872 ret);
874 return 0;
876 subsys_initcall(wm831x_ldo_init);
878 static void __exit wm831x_ldo_exit(void)
880 platform_driver_unregister(&wm831x_alive_ldo_driver);
881 platform_driver_unregister(&wm831x_aldo_driver);
882 platform_driver_unregister(&wm831x_gp_ldo_driver);
884 module_exit(wm831x_ldo_exit);
886 /* Module information */
887 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
888 MODULE_DESCRIPTION("WM831x LDO driver");
889 MODULE_LICENSE("GPL");
890 MODULE_ALIAS("platform:wm831x-ldo");
891 MODULE_ALIAS("platform:wm831x-aldo");
892 MODULE_ALIAS("platform:wm831x-aliveldo");