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);
209 return REGULATOR_STATUS_ERROR;
211 ret = wm831x_gp_ldo_get_mode(rdev);
215 return regulator_mode_to_status(ret);
218 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
220 int output_uV, int load_uA)
223 return REGULATOR_MODE_STANDBY;
225 return REGULATOR_MODE_IDLE;
226 return REGULATOR_MODE_NORMAL;
230 static struct regulator_ops wm831x_gp_ldo_ops = {
231 .list_voltage = wm831x_gp_ldo_list_voltage,
232 .map_voltage = wm831x_gp_ldo_map_voltage,
233 .get_voltage_sel = regulator_get_voltage_sel_regmap,
234 .set_voltage_sel = regulator_set_voltage_sel_regmap,
235 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
236 .get_mode = wm831x_gp_ldo_get_mode,
237 .set_mode = wm831x_gp_ldo_set_mode,
238 .get_status = wm831x_gp_ldo_get_status,
239 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
241 .is_enabled = regulator_is_enabled_regmap,
242 .enable = regulator_enable_regmap,
243 .disable = regulator_disable_regmap,
246 static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
248 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
249 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
250 struct regulator_config config = { };
252 struct wm831x_ldo *ldo;
253 struct resource *res;
256 if (pdata && pdata->wm831x_num)
257 id = (pdata->wm831x_num * 10) + 1;
262 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
264 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
266 dev_err(&pdev->dev, "Unable to allocate private data\n");
270 ldo->wm831x = wm831x;
272 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
274 dev_err(&pdev->dev, "No I/O resource\n");
278 ldo->base = res->start;
280 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
281 ldo->desc.name = ldo->name;
283 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
285 ldo->desc.supply_name = ldo->supply_name;
288 ldo->desc.type = REGULATOR_VOLTAGE;
289 ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
290 ldo->desc.ops = &wm831x_gp_ldo_ops;
291 ldo->desc.owner = THIS_MODULE;
292 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
293 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
294 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
295 ldo->desc.enable_mask = 1 << id;
297 config.dev = pdev->dev.parent;
299 config.init_data = pdata->ldo[id];
300 config.driver_data = ldo;
301 config.regmap = wm831x->regmap;
303 ldo->regulator = regulator_register(&ldo->desc, &config);
304 if (IS_ERR(ldo->regulator)) {
305 ret = PTR_ERR(ldo->regulator);
306 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
311 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
312 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
313 IRQF_TRIGGER_RISING, ldo->name,
316 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
321 platform_set_drvdata(pdev, ldo);
326 regulator_unregister(ldo->regulator);
331 static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
333 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
335 platform_set_drvdata(pdev, NULL);
337 free_irq(wm831x_irq(ldo->wm831x,
338 platform_get_irq_byname(pdev, "UV")), ldo);
339 regulator_unregister(ldo->regulator);
344 static struct platform_driver wm831x_gp_ldo_driver = {
345 .probe = wm831x_gp_ldo_probe,
346 .remove = __devexit_p(wm831x_gp_ldo_remove),
348 .name = "wm831x-ldo",
349 .owner = THIS_MODULE,
358 #define WM831X_ALDO_SELECTOR_LOW 0xc
359 #define WM831X_ALDO_MAX_SELECTOR 0x1f
361 static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
362 unsigned int selector)
364 /* 1-1.6V in 50mV steps */
365 if (selector <= WM831X_ALDO_SELECTOR_LOW)
366 return 1000000 + (selector * 50000);
367 /* 1.7-3.5V in 100mV steps */
368 if (selector <= WM831X_ALDO_MAX_SELECTOR)
369 return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
374 static int wm831x_aldo_map_voltage(struct regulator_dev *rdev,
375 int min_uV, int max_uV)
379 if (min_uV < 1000000)
381 else if (min_uV < 1700000)
382 vsel = ((min_uV - 1000000) / 50000);
384 vsel = ((min_uV - 1700000) / 100000)
385 + WM831X_ALDO_SELECTOR_LOW + 1;
387 volt = wm831x_aldo_list_voltage(rdev, vsel);
388 if (volt < min_uV || volt > max_uV)
395 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
398 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
399 struct wm831x *wm831x = ldo->wm831x;
400 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
402 sel = wm831x_aldo_map_voltage(rdev, uV, uV);
406 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
409 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
411 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
412 struct wm831x *wm831x = ldo->wm831x;
413 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
416 ret = wm831x_reg_read(wm831x, on_reg);
420 if (ret & WM831X_LDO7_ON_MODE)
421 return REGULATOR_MODE_IDLE;
423 return REGULATOR_MODE_NORMAL;
426 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
429 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
430 struct wm831x *wm831x = ldo->wm831x;
431 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
436 case REGULATOR_MODE_NORMAL:
437 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
442 case REGULATOR_MODE_IDLE:
443 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
444 WM831X_LDO7_ON_MODE);
456 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
458 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
459 struct wm831x *wm831x = ldo->wm831x;
460 int mask = 1 << rdev_get_id(rdev);
463 /* Is the regulator on? */
464 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
468 return REGULATOR_STATUS_OFF;
470 /* Is it reporting under voltage? */
471 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
473 return REGULATOR_STATUS_ERROR;
475 ret = wm831x_aldo_get_mode(rdev);
479 return regulator_mode_to_status(ret);
482 static struct regulator_ops wm831x_aldo_ops = {
483 .list_voltage = wm831x_aldo_list_voltage,
484 .map_voltage = wm831x_aldo_map_voltage,
485 .get_voltage_sel = regulator_get_voltage_sel_regmap,
486 .set_voltage_sel = regulator_set_voltage_sel_regmap,
487 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
488 .get_mode = wm831x_aldo_get_mode,
489 .set_mode = wm831x_aldo_set_mode,
490 .get_status = wm831x_aldo_get_status,
492 .is_enabled = regulator_is_enabled_regmap,
493 .enable = regulator_enable_regmap,
494 .disable = regulator_disable_regmap,
497 static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
499 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
500 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
501 struct regulator_config config = { };
503 struct wm831x_ldo *ldo;
504 struct resource *res;
507 if (pdata && pdata->wm831x_num)
508 id = (pdata->wm831x_num * 10) + 1;
513 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
515 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
517 dev_err(&pdev->dev, "Unable to allocate private data\n");
521 ldo->wm831x = wm831x;
523 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
525 dev_err(&pdev->dev, "No I/O resource\n");
529 ldo->base = res->start;
531 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
532 ldo->desc.name = ldo->name;
534 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
536 ldo->desc.supply_name = ldo->supply_name;
539 ldo->desc.type = REGULATOR_VOLTAGE;
540 ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
541 ldo->desc.ops = &wm831x_aldo_ops;
542 ldo->desc.owner = THIS_MODULE;
543 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
544 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
545 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
546 ldo->desc.enable_mask = 1 << id;
548 config.dev = pdev->dev.parent;
550 config.init_data = pdata->ldo[id];
551 config.driver_data = ldo;
552 config.regmap = wm831x->regmap;
554 ldo->regulator = regulator_register(&ldo->desc, &config);
555 if (IS_ERR(ldo->regulator)) {
556 ret = PTR_ERR(ldo->regulator);
557 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
562 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
563 ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
564 IRQF_TRIGGER_RISING, ldo->name, ldo);
566 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
571 platform_set_drvdata(pdev, ldo);
576 regulator_unregister(ldo->regulator);
581 static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
583 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
585 free_irq(wm831x_irq(ldo->wm831x, platform_get_irq_byname(pdev, "UV")),
587 regulator_unregister(ldo->regulator);
592 static struct platform_driver wm831x_aldo_driver = {
593 .probe = wm831x_aldo_probe,
594 .remove = __devexit_p(wm831x_aldo_remove),
596 .name = "wm831x-aldo",
597 .owner = THIS_MODULE,
605 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
607 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
610 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
611 struct wm831x *wm831x = ldo->wm831x;
612 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
614 sel = regulator_map_voltage_linear(rdev, uV, uV);
618 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
621 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
623 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
624 struct wm831x *wm831x = ldo->wm831x;
625 int mask = 1 << rdev_get_id(rdev);
628 /* Is the regulator on? */
629 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
633 return REGULATOR_STATUS_ON;
635 return REGULATOR_STATUS_OFF;
638 static struct regulator_ops wm831x_alive_ldo_ops = {
639 .list_voltage = regulator_list_voltage_linear,
640 .map_voltage = regulator_map_voltage_linear,
641 .get_voltage_sel = regulator_get_voltage_sel_regmap,
642 .set_voltage_sel = regulator_set_voltage_sel_regmap,
643 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
644 .get_status = wm831x_alive_ldo_get_status,
646 .is_enabled = regulator_is_enabled_regmap,
647 .enable = regulator_enable_regmap,
648 .disable = regulator_disable_regmap,
651 static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
653 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
654 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
655 struct regulator_config config = { };
657 struct wm831x_ldo *ldo;
658 struct resource *res;
661 if (pdata && pdata->wm831x_num)
662 id = (pdata->wm831x_num * 10) + 1;
668 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
670 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
672 dev_err(&pdev->dev, "Unable to allocate private data\n");
676 ldo->wm831x = wm831x;
678 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
680 dev_err(&pdev->dev, "No I/O resource\n");
684 ldo->base = res->start;
686 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
687 ldo->desc.name = ldo->name;
689 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
691 ldo->desc.supply_name = ldo->supply_name;
694 ldo->desc.type = REGULATOR_VOLTAGE;
695 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
696 ldo->desc.ops = &wm831x_alive_ldo_ops;
697 ldo->desc.owner = THIS_MODULE;
698 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
699 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
700 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
701 ldo->desc.enable_mask = 1 << id;
702 ldo->desc.min_uV = 800000;
703 ldo->desc.uV_step = 50000;
704 ldo->desc.enable_time = 1000;
706 config.dev = pdev->dev.parent;
708 config.init_data = pdata->ldo[id];
709 config.driver_data = ldo;
710 config.regmap = wm831x->regmap;
712 ldo->regulator = regulator_register(&ldo->desc, &config);
713 if (IS_ERR(ldo->regulator)) {
714 ret = PTR_ERR(ldo->regulator);
715 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
720 platform_set_drvdata(pdev, ldo);
728 static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
730 struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
732 regulator_unregister(ldo->regulator);
737 static struct platform_driver wm831x_alive_ldo_driver = {
738 .probe = wm831x_alive_ldo_probe,
739 .remove = __devexit_p(wm831x_alive_ldo_remove),
741 .name = "wm831x-alive-ldo",
742 .owner = THIS_MODULE,
746 static int __init wm831x_ldo_init(void)
750 ret = platform_driver_register(&wm831x_gp_ldo_driver);
752 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
754 ret = platform_driver_register(&wm831x_aldo_driver);
756 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
758 ret = platform_driver_register(&wm831x_alive_ldo_driver);
760 pr_err("Failed to register WM831x alive LDO driver: %d\n",
765 subsys_initcall(wm831x_ldo_init);
767 static void __exit wm831x_ldo_exit(void)
769 platform_driver_unregister(&wm831x_alive_ldo_driver);
770 platform_driver_unregister(&wm831x_aldo_driver);
771 platform_driver_unregister(&wm831x_gp_ldo_driver);
773 module_exit(wm831x_ldo_exit);
775 /* Module information */
776 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
777 MODULE_DESCRIPTION("WM831x LDO driver");
778 MODULE_LICENSE("GPL");
779 MODULE_ALIAS("platform:wm831x-ldo");
780 MODULE_ALIAS("platform:wm831x-aldo");
781 MODULE_ALIAS("platform:wm831x-aliveldo");