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 static const struct regulator_linear_range wm831x_gp_ldo_ranges[] = {
66 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
67 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
70 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
73 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
74 struct wm831x *wm831x = ldo->wm831x;
75 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
77 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
81 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
84 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
86 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
87 struct wm831x *wm831x = ldo->wm831x;
88 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
89 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
92 ret = wm831x_reg_read(wm831x, on_reg);
96 if (!(ret & WM831X_LDO1_ON_MODE))
97 return REGULATOR_MODE_NORMAL;
99 ret = wm831x_reg_read(wm831x, ctrl_reg);
103 if (ret & WM831X_LDO1_LP_MODE)
104 return REGULATOR_MODE_STANDBY;
106 return REGULATOR_MODE_IDLE;
109 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
112 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
113 struct wm831x *wm831x = ldo->wm831x;
114 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
115 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
120 case REGULATOR_MODE_NORMAL:
121 ret = wm831x_set_bits(wm831x, on_reg,
122 WM831X_LDO1_ON_MODE, 0);
127 case REGULATOR_MODE_IDLE:
128 ret = wm831x_set_bits(wm831x, ctrl_reg,
129 WM831X_LDO1_LP_MODE, 0);
133 ret = wm831x_set_bits(wm831x, on_reg,
135 WM831X_LDO1_ON_MODE);
140 case REGULATOR_MODE_STANDBY:
141 ret = wm831x_set_bits(wm831x, ctrl_reg,
143 WM831X_LDO1_LP_MODE);
147 ret = wm831x_set_bits(wm831x, on_reg,
149 WM831X_LDO1_ON_MODE);
161 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
163 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
164 struct wm831x *wm831x = ldo->wm831x;
165 int mask = 1 << rdev_get_id(rdev);
168 /* Is the regulator on? */
169 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
173 return REGULATOR_STATUS_OFF;
175 /* Is it reporting under voltage? */
176 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
180 return REGULATOR_STATUS_ERROR;
182 ret = wm831x_gp_ldo_get_mode(rdev);
186 return regulator_mode_to_status(ret);
189 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
191 int output_uV, int load_uA)
194 return REGULATOR_MODE_STANDBY;
196 return REGULATOR_MODE_IDLE;
197 return REGULATOR_MODE_NORMAL;
201 static struct regulator_ops wm831x_gp_ldo_ops = {
202 .list_voltage = regulator_list_voltage_linear_range,
203 .map_voltage = regulator_map_voltage_linear_range,
204 .get_voltage_sel = regulator_get_voltage_sel_regmap,
205 .set_voltage_sel = regulator_set_voltage_sel_regmap,
206 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
207 .get_mode = wm831x_gp_ldo_get_mode,
208 .set_mode = wm831x_gp_ldo_set_mode,
209 .get_status = wm831x_gp_ldo_get_status,
210 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
211 .get_bypass = regulator_get_bypass_regmap,
212 .set_bypass = regulator_set_bypass_regmap,
214 .is_enabled = regulator_is_enabled_regmap,
215 .enable = regulator_enable_regmap,
216 .disable = regulator_disable_regmap,
219 static int wm831x_gp_ldo_probe(struct platform_device *pdev)
221 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
222 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
223 struct regulator_config config = { };
225 struct wm831x_ldo *ldo;
226 struct resource *res;
229 if (pdata && pdata->wm831x_num)
230 id = (pdata->wm831x_num * 10) + 1;
235 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
237 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
241 ldo->wm831x = wm831x;
243 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
245 dev_err(&pdev->dev, "No REG resource\n");
249 ldo->base = res->start;
251 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
252 ldo->desc.name = ldo->name;
254 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
256 ldo->desc.supply_name = ldo->supply_name;
259 ldo->desc.type = REGULATOR_VOLTAGE;
260 ldo->desc.n_voltages = 32;
261 ldo->desc.ops = &wm831x_gp_ldo_ops;
262 ldo->desc.owner = THIS_MODULE;
263 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
264 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
265 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
266 ldo->desc.enable_mask = 1 << id;
267 ldo->desc.bypass_reg = ldo->base;
268 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
269 ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
270 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
272 config.dev = pdev->dev.parent;
274 config.init_data = pdata->ldo[id];
275 config.driver_data = ldo;
276 config.regmap = wm831x->regmap;
278 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
280 if (IS_ERR(ldo->regulator)) {
281 ret = PTR_ERR(ldo->regulator);
282 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
287 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
288 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
290 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
294 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
299 platform_set_drvdata(pdev, ldo);
307 static struct platform_driver wm831x_gp_ldo_driver = {
308 .probe = wm831x_gp_ldo_probe,
310 .name = "wm831x-ldo",
318 static const struct regulator_linear_range wm831x_aldo_ranges[] = {
319 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
320 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
323 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
326 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
327 struct wm831x *wm831x = ldo->wm831x;
328 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
330 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
334 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
337 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
339 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
340 struct wm831x *wm831x = ldo->wm831x;
341 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
344 ret = wm831x_reg_read(wm831x, on_reg);
348 if (ret & WM831X_LDO7_ON_MODE)
349 return REGULATOR_MODE_IDLE;
351 return REGULATOR_MODE_NORMAL;
354 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
357 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
358 struct wm831x *wm831x = ldo->wm831x;
359 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
364 case REGULATOR_MODE_NORMAL:
365 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
370 case REGULATOR_MODE_IDLE:
371 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
372 WM831X_LDO7_ON_MODE);
384 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
386 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
387 struct wm831x *wm831x = ldo->wm831x;
388 int mask = 1 << rdev_get_id(rdev);
391 /* Is the regulator on? */
392 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
396 return REGULATOR_STATUS_OFF;
398 /* Is it reporting under voltage? */
399 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
403 return REGULATOR_STATUS_ERROR;
405 ret = wm831x_aldo_get_mode(rdev);
409 return regulator_mode_to_status(ret);
412 static struct regulator_ops wm831x_aldo_ops = {
413 .list_voltage = regulator_list_voltage_linear_range,
414 .map_voltage = regulator_map_voltage_linear_range,
415 .get_voltage_sel = regulator_get_voltage_sel_regmap,
416 .set_voltage_sel = regulator_set_voltage_sel_regmap,
417 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
418 .get_mode = wm831x_aldo_get_mode,
419 .set_mode = wm831x_aldo_set_mode,
420 .get_status = wm831x_aldo_get_status,
421 .set_bypass = regulator_set_bypass_regmap,
422 .get_bypass = regulator_get_bypass_regmap,
424 .is_enabled = regulator_is_enabled_regmap,
425 .enable = regulator_enable_regmap,
426 .disable = regulator_disable_regmap,
429 static int wm831x_aldo_probe(struct platform_device *pdev)
431 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
432 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
433 struct regulator_config config = { };
435 struct wm831x_ldo *ldo;
436 struct resource *res;
439 if (pdata && pdata->wm831x_num)
440 id = (pdata->wm831x_num * 10) + 1;
445 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
447 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
451 ldo->wm831x = wm831x;
453 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
455 dev_err(&pdev->dev, "No REG resource\n");
459 ldo->base = res->start;
461 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
462 ldo->desc.name = ldo->name;
464 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
466 ldo->desc.supply_name = ldo->supply_name;
469 ldo->desc.type = REGULATOR_VOLTAGE;
470 ldo->desc.n_voltages = 32;
471 ldo->desc.linear_ranges = wm831x_aldo_ranges;
472 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
473 ldo->desc.ops = &wm831x_aldo_ops;
474 ldo->desc.owner = THIS_MODULE;
475 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
476 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
477 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
478 ldo->desc.enable_mask = 1 << id;
479 ldo->desc.bypass_reg = ldo->base;
480 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
482 config.dev = pdev->dev.parent;
484 config.init_data = pdata->ldo[id];
485 config.driver_data = ldo;
486 config.regmap = wm831x->regmap;
488 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
490 if (IS_ERR(ldo->regulator)) {
491 ret = PTR_ERR(ldo->regulator);
492 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
497 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
498 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
500 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
503 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
508 platform_set_drvdata(pdev, ldo);
516 static struct platform_driver wm831x_aldo_driver = {
517 .probe = wm831x_aldo_probe,
519 .name = "wm831x-aldo",
527 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
529 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
532 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
533 struct wm831x *wm831x = ldo->wm831x;
534 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
536 sel = regulator_map_voltage_linear(rdev, uV, uV);
540 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
543 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
545 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
546 struct wm831x *wm831x = ldo->wm831x;
547 int mask = 1 << rdev_get_id(rdev);
550 /* Is the regulator on? */
551 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
555 return REGULATOR_STATUS_ON;
557 return REGULATOR_STATUS_OFF;
560 static struct regulator_ops wm831x_alive_ldo_ops = {
561 .list_voltage = regulator_list_voltage_linear,
562 .map_voltage = regulator_map_voltage_linear,
563 .get_voltage_sel = regulator_get_voltage_sel_regmap,
564 .set_voltage_sel = regulator_set_voltage_sel_regmap,
565 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
566 .get_status = wm831x_alive_ldo_get_status,
568 .is_enabled = regulator_is_enabled_regmap,
569 .enable = regulator_enable_regmap,
570 .disable = regulator_disable_regmap,
573 static int wm831x_alive_ldo_probe(struct platform_device *pdev)
575 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
576 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
577 struct regulator_config config = { };
579 struct wm831x_ldo *ldo;
580 struct resource *res;
583 if (pdata && pdata->wm831x_num)
584 id = (pdata->wm831x_num * 10) + 1;
590 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
592 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
596 ldo->wm831x = wm831x;
598 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
600 dev_err(&pdev->dev, "No REG resource\n");
604 ldo->base = res->start;
606 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
607 ldo->desc.name = ldo->name;
609 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
611 ldo->desc.supply_name = ldo->supply_name;
614 ldo->desc.type = REGULATOR_VOLTAGE;
615 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
616 ldo->desc.ops = &wm831x_alive_ldo_ops;
617 ldo->desc.owner = THIS_MODULE;
618 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
619 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
620 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
621 ldo->desc.enable_mask = 1 << id;
622 ldo->desc.min_uV = 800000;
623 ldo->desc.uV_step = 50000;
624 ldo->desc.enable_time = 1000;
626 config.dev = pdev->dev.parent;
628 config.init_data = pdata->ldo[id];
629 config.driver_data = ldo;
630 config.regmap = wm831x->regmap;
632 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
634 if (IS_ERR(ldo->regulator)) {
635 ret = PTR_ERR(ldo->regulator);
636 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
641 platform_set_drvdata(pdev, ldo);
649 static struct platform_driver wm831x_alive_ldo_driver = {
650 .probe = wm831x_alive_ldo_probe,
652 .name = "wm831x-alive-ldo",
656 static int __init wm831x_ldo_init(void)
660 ret = platform_driver_register(&wm831x_gp_ldo_driver);
662 pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
664 ret = platform_driver_register(&wm831x_aldo_driver);
666 pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
668 ret = platform_driver_register(&wm831x_alive_ldo_driver);
670 pr_err("Failed to register WM831x alive LDO driver: %d\n",
675 subsys_initcall(wm831x_ldo_init);
677 static void __exit wm831x_ldo_exit(void)
679 platform_driver_unregister(&wm831x_alive_ldo_driver);
680 platform_driver_unregister(&wm831x_aldo_driver);
681 platform_driver_unregister(&wm831x_gp_ldo_driver);
683 module_exit(wm831x_ldo_exit);
685 /* Module information */
686 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
687 MODULE_DESCRIPTION("WM831x LDO driver");
688 MODULE_LICENSE("GPL");
689 MODULE_ALIAS("platform:wm831x-ldo");
690 MODULE_ALIAS("platform:wm831x-aldo");
691 MODULE_ALIAS("platform:wm831x-aliveldo");