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 9
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
38 char name[WM831X_LDO_MAX_NAME];
39 char supply_name[WM831X_LDO_MAX_NAME];
40 struct regulator_desc desc;
42 struct wm831x *wm831x;
43 struct regulator_dev *regulator;
50 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
52 struct wm831x_ldo *ldo = data;
54 regulator_notifier_call_chain(ldo->regulator,
55 REGULATOR_EVENT_UNDER_VOLTAGE,
62 * General purpose LDOs
65 #define WM831X_GP_LDO_SELECTOR_LOW 0xe
66 #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
68 static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
69 unsigned int selector)
71 /* 0.9-1.6V in 50mV steps */
72 if (selector <= WM831X_GP_LDO_SELECTOR_LOW)
73 return 900000 + (selector * 50000);
74 /* 1.7-3.3V in 100mV steps */
75 if (selector <= WM831X_GP_LDO_MAX_SELECTOR)
76 return 1600000 + ((selector - WM831X_GP_LDO_SELECTOR_LOW)
81 static int wm831x_gp_ldo_map_voltage(struct regulator_dev *rdev,
82 int min_uV, int max_uV)
88 else if (min_uV < 1700000)
89 vsel = ((min_uV - 900000) / 50000);
91 vsel = ((min_uV - 1700000) / 100000)
92 + WM831X_GP_LDO_SELECTOR_LOW + 1;
94 volt = wm831x_gp_ldo_list_voltage(rdev, vsel);
95 if (volt < min_uV || volt > max_uV)
101 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
104 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
105 struct wm831x *wm831x = ldo->wm831x;
106 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
108 sel = wm831x_gp_ldo_map_voltage(rdev, uV, uV);
112 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
115 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
117 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
118 struct wm831x *wm831x = ldo->wm831x;
119 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
120 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
123 ret = wm831x_reg_read(wm831x, on_reg);
127 if (!(ret & WM831X_LDO1_ON_MODE))
128 return REGULATOR_MODE_NORMAL;
130 ret = wm831x_reg_read(wm831x, ctrl_reg);
134 if (ret & WM831X_LDO1_LP_MODE)
135 return REGULATOR_MODE_STANDBY;
137 return REGULATOR_MODE_IDLE;
140 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
143 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
144 struct wm831x *wm831x = ldo->wm831x;
145 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
146 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
151 case REGULATOR_MODE_NORMAL:
152 ret = wm831x_set_bits(wm831x, on_reg,
153 WM831X_LDO1_ON_MODE, 0);
158 case REGULATOR_MODE_IDLE:
159 ret = wm831x_set_bits(wm831x, ctrl_reg,
160 WM831X_LDO1_LP_MODE, 0);
164 ret = wm831x_set_bits(wm831x, on_reg,
166 WM831X_LDO1_ON_MODE);
171 case REGULATOR_MODE_STANDBY:
172 ret = wm831x_set_bits(wm831x, ctrl_reg,
174 WM831X_LDO1_LP_MODE);
178 ret = wm831x_set_bits(wm831x, on_reg,
180 WM831X_LDO1_ON_MODE);
192 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
194 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
195 struct wm831x *wm831x = ldo->wm831x;
196 int mask = 1 << rdev_get_id(rdev);
199 /* Is the regulator on? */
200 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
204 return REGULATOR_STATUS_OFF;
206 /* Is it reporting under voltage? */
207 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
211 return REGULATOR_STATUS_ERROR;
213 ret = wm831x_gp_ldo_get_mode(rdev);
217 return regulator_mode_to_status(ret);
220 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
222 int output_uV, int load_uA)
225 return REGULATOR_MODE_STANDBY;
227 return REGULATOR_MODE_IDLE;
228 return REGULATOR_MODE_NORMAL;
232 static struct regulator_ops wm831x_gp_ldo_ops = {
233 .list_voltage = wm831x_gp_ldo_list_voltage,
234 .map_voltage = wm831x_gp_ldo_map_voltage,
235 .get_voltage_sel = regulator_get_voltage_sel_regmap,
236 .set_voltage_sel = regulator_set_voltage_sel_regmap,
237 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
238 .get_mode = wm831x_gp_ldo_get_mode,
239 .set_mode = wm831x_gp_ldo_set_mode,
240 .get_status = wm831x_gp_ldo_get_status,
241 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
242 .get_bypass = regulator_get_bypass_regmap,
243 .set_bypass = regulator_set_bypass_regmap,
245 .is_enabled = regulator_is_enabled_regmap,
246 .enable = regulator_enable_regmap,
247 .disable = regulator_disable_regmap,
250 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
252 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
253 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
254 struct regulator_config config = { };
256 struct wm831x_ldo *ldo;
257 struct resource *res;
260 if (pdata && pdata->wm831x_num)
261 id = (pdata->wm831x_num * 10) + 1;
266 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
268 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
270 dev_err(&pdev->dev, "Unable to allocate private data\n");
274 ldo->wm831x = wm831x;
276 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
278 dev_err(&pdev->dev, "No I/O resource\n");
282 ldo->base = res->start;
284 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
285 ldo->desc.name = ldo->name;
287 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
289 ldo->desc.supply_name = ldo->supply_name;
292 ldo->desc.type = REGULATOR_VOLTAGE;
293 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
294 ldo->desc.ops = &wm831x_gp_ldo_ops;
295 ldo->desc.owner = THIS_MODULE;
296 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
297 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
298 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
299 ldo->desc.enable_mask = 1 << id;
300 ldo->desc.bypass_reg = ldo->base;
301 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
303 config.dev = pdev->dev.parent;
305 config.init_data = pdata->ldo[id];
306 config.driver_data = ldo;
307 config.regmap = wm831x->regmap;
309 ldo->regulator = regulator_register(&ldo->desc, &config);
310 if (IS_ERR(ldo->regulator)) {
311 ret = PTR_ERR(ldo->regulator);
312 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
317 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
318 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
319 IRQF_TRIGGER_RISING, ldo->name,
322 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
327 platform_set_drvdata(pdev, ldo);
332 regulator_unregister(ldo->regulator);
337 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
339 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
341 platform_set_drvdata(pdev, NULL);
343 free_irq(wm831x_irq(ldo->wm831x,
344 platform_get_irq_byname(pdev, "UV")), ldo);
345 regulator_unregister(ldo->regulator);
350 static struct platform_driver wm831x_gp_ldo_driver = {
351 .probe = wm831x_gp_ldo_probe,
352 .remove = __devexit_p(wm831x_gp_ldo_remove),
354 .name = "wm831x-ldo",
355 .owner = THIS_MODULE,
364 #define WM831X_ALDO_SELECTOR_LOW 0xc
365 #define WM831X_ALDO_MAX_SELECTOR 0x1f
367 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
368 unsigned int selector)
370 /* 1-1.6V in 50mV steps */
371 if (selector <= WM831X_ALDO_SELECTOR_LOW)
372 return 1000000 + (selector * 50000);
373 /* 1.7-3.5V in 100mV steps */
374 if (selector <= WM831X_ALDO_MAX_SELECTOR)
375 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
380 static int wm831x_aldo_map_voltage(struct regulator_dev *rdev,
381 int min_uV, int max_uV)
385 if (min_uV < 1000000)
387 else if (min_uV < 1700000)
388 vsel = ((min_uV - 1000000) / 50000);
390 vsel = ((min_uV - 1700000) / 100000)
391 + WM831X_ALDO_SELECTOR_LOW + 1;
393 volt = wm831x_aldo_list_voltage(rdev, vsel);
394 if (volt < min_uV || volt > max_uV)
401 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
404 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
405 struct wm831x *wm831x = ldo->wm831x;
406 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
408 sel = wm831x_aldo_map_voltage(rdev, uV, uV);
412 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
415 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
417 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
418 struct wm831x *wm831x = ldo->wm831x;
419 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
422 ret = wm831x_reg_read(wm831x, on_reg);
426 if (ret & WM831X_LDO7_ON_MODE)
427 return REGULATOR_MODE_IDLE;
429 return REGULATOR_MODE_NORMAL;
432 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
435 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
436 struct wm831x *wm831x = ldo->wm831x;
437 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
442 case REGULATOR_MODE_NORMAL:
443 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
448 case REGULATOR_MODE_IDLE:
449 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
450 WM831X_LDO7_ON_MODE);
462 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
464 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
465 struct wm831x *wm831x = ldo->wm831x;
466 int mask = 1 << rdev_get_id(rdev);
469 /* Is the regulator on? */
470 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
474 return REGULATOR_STATUS_OFF;
476 /* Is it reporting under voltage? */
477 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
481 return REGULATOR_STATUS_ERROR;
483 ret = wm831x_aldo_get_mode(rdev);
487 return regulator_mode_to_status(ret);
490 static struct regulator_ops wm831x_aldo_ops = {
491 .list_voltage = wm831x_aldo_list_voltage,
492 .map_voltage = wm831x_aldo_map_voltage,
493 .get_voltage_sel = regulator_get_voltage_sel_regmap,
494 .set_voltage_sel = regulator_set_voltage_sel_regmap,
495 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
496 .get_mode = wm831x_aldo_get_mode,
497 .set_mode = wm831x_aldo_set_mode,
498 .get_status = wm831x_aldo_get_status,
499 .set_bypass = regulator_set_bypass_regmap,
500 .get_bypass = regulator_get_bypass_regmap,
502 .is_enabled = regulator_is_enabled_regmap,
503 .enable = regulator_enable_regmap,
504 .disable = regulator_disable_regmap,
507 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
509 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
510 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
511 struct regulator_config config = { };
513 struct wm831x_ldo *ldo;
514 struct resource *res;
517 if (pdata && pdata->wm831x_num)
518 id = (pdata->wm831x_num * 10) + 1;
523 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
525 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
527 dev_err(&pdev->dev, "Unable to allocate private data\n");
531 ldo->wm831x = wm831x;
533 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
535 dev_err(&pdev->dev, "No I/O resource\n");
539 ldo->base = res->start;
541 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
542 ldo->desc.name = ldo->name;
544 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
546 ldo->desc.supply_name = ldo->supply_name;
549 ldo->desc.type = REGULATOR_VOLTAGE;
550 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
551 ldo->desc.ops = &wm831x_aldo_ops;
552 ldo->desc.owner = THIS_MODULE;
553 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
554 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
555 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
556 ldo->desc.enable_mask = 1 << id;
557 ldo->desc.bypass_reg = ldo->base;
558 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
560 config.dev = pdev->dev.parent;
562 config.init_data = pdata->ldo[id];
563 config.driver_data = ldo;
564 config.regmap = wm831x->regmap;
566 ldo->regulator = regulator_register(&ldo->desc, &config);
567 if (IS_ERR(ldo->regulator)) {
568 ret = PTR_ERR(ldo->regulator);
569 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
574 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
575 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
576 IRQF_TRIGGER_RISING, ldo->name, ldo);
578 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
583 platform_set_drvdata(pdev, ldo);
588 regulator_unregister(ldo->regulator);
593 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
595 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
597 free_irq(wm831x_irq(ldo->wm831x, platform_get_irq_byname(pdev, "UV")),
599 regulator_unregister(ldo->regulator);
604 static struct platform_driver wm831x_aldo_driver = {
605 .probe = wm831x_aldo_probe,
606 .remove = __devexit_p(wm831x_aldo_remove),
608 .name = "wm831x-aldo",
609 .owner = THIS_MODULE,
617 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
619 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
622 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
623 struct wm831x *wm831x = ldo->wm831x;
624 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
626 sel = regulator_map_voltage_linear(rdev, uV, uV);
630 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
633 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
635 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
636 struct wm831x *wm831x = ldo->wm831x;
637 int mask = 1 << rdev_get_id(rdev);
640 /* Is the regulator on? */
641 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
645 return REGULATOR_STATUS_ON;
647 return REGULATOR_STATUS_OFF;
650 static struct regulator_ops wm831x_alive_ldo_ops = {
651 .list_voltage = regulator_list_voltage_linear,
652 .map_voltage = regulator_map_voltage_linear,
653 .get_voltage_sel = regulator_get_voltage_sel_regmap,
654 .set_voltage_sel = regulator_set_voltage_sel_regmap,
655 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
656 .get_status = wm831x_alive_ldo_get_status,
658 .is_enabled = regulator_is_enabled_regmap,
659 .enable = regulator_enable_regmap,
660 .disable = regulator_disable_regmap,
663 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
665 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
666 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
667 struct regulator_config config = { };
669 struct wm831x_ldo *ldo;
670 struct resource *res;
673 if (pdata && pdata->wm831x_num)
674 id = (pdata->wm831x_num * 10) + 1;
680 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
682 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
684 dev_err(&pdev->dev, "Unable to allocate private data\n");
688 ldo->wm831x = wm831x;
690 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
692 dev_err(&pdev->dev, "No I/O resource\n");
696 ldo->base = res->start;
698 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
699 ldo->desc.name = ldo->name;
701 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
703 ldo->desc.supply_name = ldo->supply_name;
706 ldo->desc.type = REGULATOR_VOLTAGE;
707 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
708 ldo->desc.ops = &wm831x_alive_ldo_ops;
709 ldo->desc.owner = THIS_MODULE;
710 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
711 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
712 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
713 ldo->desc.enable_mask = 1 << id;
714 ldo->desc.min_uV = 800000;
715 ldo->desc.uV_step = 50000;
716 ldo->desc.enable_time = 1000;
718 config.dev = pdev->dev.parent;
720 config.init_data = pdata->ldo[id];
721 config.driver_data = ldo;
722 config.regmap = wm831x->regmap;
724 ldo->regulator = regulator_register(&ldo->desc, &config);
725 if (IS_ERR(ldo->regulator)) {
726 ret = PTR_ERR(ldo->regulator);
727 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
732 platform_set_drvdata(pdev, ldo);
740 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
742 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
744 regulator_unregister(ldo->regulator);
749 static struct platform_driver wm831x_alive_ldo_driver = {
750 .probe = wm831x_alive_ldo_probe,
751 .remove = __devexit_p(wm831x_alive_ldo_remove),
753 .name = "wm831x-alive-ldo",
754 .owner = THIS_MODULE,
758 static int __init wm831x_ldo_init(void)
762 ret = platform_driver_register(&wm831x_gp_ldo_driver);
764 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
766 ret = platform_driver_register(&wm831x_aldo_driver);
768 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
770 ret = platform_driver_register(&wm831x_alive_ldo_driver);
772 pr_err("Failed to register WM831x alive LDO driver: %d\n",
777 subsys_initcall(wm831x_ldo_init);
779 static void __exit wm831x_ldo_exit(void)
781 platform_driver_unregister(&wm831x_alive_ldo_driver);
782 platform_driver_unregister(&wm831x_aldo_driver);
783 platform_driver_unregister(&wm831x_gp_ldo_driver);
785 module_exit(wm831x_ldo_exit);
787 /* Module information */
788 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
789 MODULE_DESCRIPTION("WM831x LDO driver");
790 MODULE_LICENSE("GPL");
791 MODULE_ALIAS("platform:wm831x-ldo");
792 MODULE_ALIAS("platform:wm831x-aldo");
793 MODULE_ALIAS("platform:wm831x-aliveldo");