1 // SPDX-License-Identifier: GPL-2.0+
3 // wm831x-ldo.c -- LDO driver for the WM831x series
5 // Copyright 2009 Wolfson Microelectronics PLC.
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/bitops.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/slab.h>
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/regulator.h>
21 #include <linux/mfd/wm831x/pdata.h>
23 #define WM831X_LDO_MAX_NAME 9
25 #define WM831X_LDO_CONTROL 0
26 #define WM831X_LDO_ON_CONTROL 1
27 #define WM831X_LDO_SLEEP_CONTROL 2
29 #define WM831X_ALIVE_LDO_ON_CONTROL 0
30 #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
33 char name[WM831X_LDO_MAX_NAME];
34 char supply_name[WM831X_LDO_MAX_NAME];
35 struct regulator_desc desc;
37 struct wm831x *wm831x;
38 struct regulator_dev *regulator;
45 static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
47 struct wm831x_ldo *ldo = data;
49 regulator_lock(ldo->regulator);
50 regulator_notifier_call_chain(ldo->regulator,
51 REGULATOR_EVENT_UNDER_VOLTAGE,
53 regulator_unlock(ldo->regulator);
59 * General purpose LDOs
62 static const struct regulator_linear_range wm831x_gp_ldo_ranges[] = {
63 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
64 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
67 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
70 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
71 struct wm831x *wm831x = ldo->wm831x;
72 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
74 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
78 return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, sel);
81 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
83 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
84 struct wm831x *wm831x = ldo->wm831x;
85 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
86 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
89 ret = wm831x_reg_read(wm831x, on_reg);
93 if (!(ret & WM831X_LDO1_ON_MODE))
94 return REGULATOR_MODE_NORMAL;
96 ret = wm831x_reg_read(wm831x, ctrl_reg);
100 if (ret & WM831X_LDO1_LP_MODE)
101 return REGULATOR_MODE_STANDBY;
103 return REGULATOR_MODE_IDLE;
106 static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
109 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
110 struct wm831x *wm831x = ldo->wm831x;
111 int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
112 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
117 case REGULATOR_MODE_NORMAL:
118 ret = wm831x_set_bits(wm831x, on_reg,
119 WM831X_LDO1_ON_MODE, 0);
124 case REGULATOR_MODE_IDLE:
125 ret = wm831x_set_bits(wm831x, ctrl_reg,
126 WM831X_LDO1_LP_MODE, 0);
130 ret = wm831x_set_bits(wm831x, on_reg,
132 WM831X_LDO1_ON_MODE);
137 case REGULATOR_MODE_STANDBY:
138 ret = wm831x_set_bits(wm831x, ctrl_reg,
140 WM831X_LDO1_LP_MODE);
144 ret = wm831x_set_bits(wm831x, on_reg,
146 WM831X_LDO1_ON_MODE);
158 static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
160 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
161 struct wm831x *wm831x = ldo->wm831x;
162 int mask = 1 << rdev_get_id(rdev);
165 /* Is the regulator on? */
166 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
170 return REGULATOR_STATUS_OFF;
172 /* Is it reporting under voltage? */
173 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
177 return REGULATOR_STATUS_ERROR;
179 ret = wm831x_gp_ldo_get_mode(rdev);
183 return regulator_mode_to_status(ret);
186 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
188 int output_uV, int load_uA)
191 return REGULATOR_MODE_STANDBY;
193 return REGULATOR_MODE_IDLE;
194 return REGULATOR_MODE_NORMAL;
198 static const struct regulator_ops wm831x_gp_ldo_ops = {
199 .list_voltage = regulator_list_voltage_linear_range,
200 .map_voltage = regulator_map_voltage_linear_range,
201 .get_voltage_sel = regulator_get_voltage_sel_regmap,
202 .set_voltage_sel = regulator_set_voltage_sel_regmap,
203 .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
204 .get_mode = wm831x_gp_ldo_get_mode,
205 .set_mode = wm831x_gp_ldo_set_mode,
206 .get_status = wm831x_gp_ldo_get_status,
207 .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
208 .get_bypass = regulator_get_bypass_regmap,
209 .set_bypass = regulator_set_bypass_regmap,
211 .is_enabled = regulator_is_enabled_regmap,
212 .enable = regulator_enable_regmap,
213 .disable = regulator_disable_regmap,
216 static int wm831x_gp_ldo_probe(struct platform_device *pdev)
218 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
219 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
220 struct regulator_config config = { };
222 struct wm831x_ldo *ldo;
223 struct resource *res;
226 if (pdata && pdata->wm831x_num)
227 id = (pdata->wm831x_num * 10) + 1;
232 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
234 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
238 ldo->wm831x = wm831x;
240 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
242 dev_err(&pdev->dev, "No REG resource\n");
246 ldo->base = res->start;
248 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
249 ldo->desc.name = ldo->name;
251 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
253 ldo->desc.supply_name = ldo->supply_name;
256 ldo->desc.type = REGULATOR_VOLTAGE;
257 ldo->desc.n_voltages = 32;
258 ldo->desc.ops = &wm831x_gp_ldo_ops;
259 ldo->desc.owner = THIS_MODULE;
260 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
261 ldo->desc.vsel_mask = WM831X_LDO1_ON_VSEL_MASK;
262 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
263 ldo->desc.enable_mask = 1 << id;
264 ldo->desc.bypass_reg = ldo->base;
265 ldo->desc.bypass_mask = WM831X_LDO1_SWI;
266 ldo->desc.linear_ranges = wm831x_gp_ldo_ranges;
267 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_gp_ldo_ranges);
269 config.dev = pdev->dev.parent;
271 config.init_data = pdata->ldo[id];
272 config.driver_data = ldo;
273 config.regmap = wm831x->regmap;
275 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
277 if (IS_ERR(ldo->regulator)) {
278 ret = PTR_ERR(ldo->regulator);
279 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
284 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
285 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
287 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
291 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
296 platform_set_drvdata(pdev, ldo);
304 static struct platform_driver wm831x_gp_ldo_driver = {
305 .probe = wm831x_gp_ldo_probe,
307 .name = "wm831x-ldo",
315 static const struct regulator_linear_range wm831x_aldo_ranges[] = {
316 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
317 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
320 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
323 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
324 struct wm831x *wm831x = ldo->wm831x;
325 int sel, reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
327 sel = regulator_map_voltage_linear_range(rdev, uV, uV);
331 return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, sel);
334 static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
336 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
337 struct wm831x *wm831x = ldo->wm831x;
338 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
341 ret = wm831x_reg_read(wm831x, on_reg);
345 if (ret & WM831X_LDO7_ON_MODE)
346 return REGULATOR_MODE_IDLE;
348 return REGULATOR_MODE_NORMAL;
351 static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
354 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
355 struct wm831x *wm831x = ldo->wm831x;
356 int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
361 case REGULATOR_MODE_NORMAL:
362 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
367 case REGULATOR_MODE_IDLE:
368 ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
369 WM831X_LDO7_ON_MODE);
381 static int wm831x_aldo_get_status(struct regulator_dev *rdev)
383 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
384 struct wm831x *wm831x = ldo->wm831x;
385 int mask = 1 << rdev_get_id(rdev);
388 /* Is the regulator on? */
389 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
393 return REGULATOR_STATUS_OFF;
395 /* Is it reporting under voltage? */
396 ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
400 return REGULATOR_STATUS_ERROR;
402 ret = wm831x_aldo_get_mode(rdev);
406 return regulator_mode_to_status(ret);
409 static const struct regulator_ops wm831x_aldo_ops = {
410 .list_voltage = regulator_list_voltage_linear_range,
411 .map_voltage = regulator_map_voltage_linear_range,
412 .get_voltage_sel = regulator_get_voltage_sel_regmap,
413 .set_voltage_sel = regulator_set_voltage_sel_regmap,
414 .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
415 .get_mode = wm831x_aldo_get_mode,
416 .set_mode = wm831x_aldo_set_mode,
417 .get_status = wm831x_aldo_get_status,
418 .set_bypass = regulator_set_bypass_regmap,
419 .get_bypass = regulator_get_bypass_regmap,
421 .is_enabled = regulator_is_enabled_regmap,
422 .enable = regulator_enable_regmap,
423 .disable = regulator_disable_regmap,
426 static int wm831x_aldo_probe(struct platform_device *pdev)
428 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
429 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
430 struct regulator_config config = { };
432 struct wm831x_ldo *ldo;
433 struct resource *res;
436 if (pdata && pdata->wm831x_num)
437 id = (pdata->wm831x_num * 10) + 1;
442 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
444 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
448 ldo->wm831x = wm831x;
450 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
452 dev_err(&pdev->dev, "No REG resource\n");
456 ldo->base = res->start;
458 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
459 ldo->desc.name = ldo->name;
461 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
463 ldo->desc.supply_name = ldo->supply_name;
466 ldo->desc.type = REGULATOR_VOLTAGE;
467 ldo->desc.n_voltages = 32;
468 ldo->desc.linear_ranges = wm831x_aldo_ranges;
469 ldo->desc.n_linear_ranges = ARRAY_SIZE(wm831x_aldo_ranges);
470 ldo->desc.ops = &wm831x_aldo_ops;
471 ldo->desc.owner = THIS_MODULE;
472 ldo->desc.vsel_reg = ldo->base + WM831X_LDO_ON_CONTROL;
473 ldo->desc.vsel_mask = WM831X_LDO7_ON_VSEL_MASK;
474 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
475 ldo->desc.enable_mask = 1 << id;
476 ldo->desc.bypass_reg = ldo->base;
477 ldo->desc.bypass_mask = WM831X_LDO7_SWI;
479 config.dev = pdev->dev.parent;
481 config.init_data = pdata->ldo[id];
482 config.driver_data = ldo;
483 config.regmap = wm831x->regmap;
485 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
487 if (IS_ERR(ldo->regulator)) {
488 ret = PTR_ERR(ldo->regulator);
489 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
494 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV"));
495 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
497 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
500 dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
505 platform_set_drvdata(pdev, ldo);
513 static struct platform_driver wm831x_aldo_driver = {
514 .probe = wm831x_aldo_probe,
516 .name = "wm831x-aldo",
524 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
526 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
529 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
530 struct wm831x *wm831x = ldo->wm831x;
531 int sel, reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
533 sel = regulator_map_voltage_linear(rdev, uV, uV);
537 return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, sel);
540 static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
542 struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
543 struct wm831x *wm831x = ldo->wm831x;
544 int mask = 1 << rdev_get_id(rdev);
547 /* Is the regulator on? */
548 ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
552 return REGULATOR_STATUS_ON;
554 return REGULATOR_STATUS_OFF;
557 static const struct regulator_ops wm831x_alive_ldo_ops = {
558 .list_voltage = regulator_list_voltage_linear,
559 .map_voltage = regulator_map_voltage_linear,
560 .get_voltage_sel = regulator_get_voltage_sel_regmap,
561 .set_voltage_sel = regulator_set_voltage_sel_regmap,
562 .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
563 .get_status = wm831x_alive_ldo_get_status,
565 .is_enabled = regulator_is_enabled_regmap,
566 .enable = regulator_enable_regmap,
567 .disable = regulator_disable_regmap,
570 static int wm831x_alive_ldo_probe(struct platform_device *pdev)
572 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
573 struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev);
574 struct regulator_config config = { };
576 struct wm831x_ldo *ldo;
577 struct resource *res;
580 if (pdata && pdata->wm831x_num)
581 id = (pdata->wm831x_num * 10) + 1;
587 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
589 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
593 ldo->wm831x = wm831x;
595 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
597 dev_err(&pdev->dev, "No REG resource\n");
601 ldo->base = res->start;
603 snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
604 ldo->desc.name = ldo->name;
606 snprintf(ldo->supply_name, sizeof(ldo->supply_name),
608 ldo->desc.supply_name = ldo->supply_name;
611 ldo->desc.type = REGULATOR_VOLTAGE;
612 ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
613 ldo->desc.ops = &wm831x_alive_ldo_ops;
614 ldo->desc.owner = THIS_MODULE;
615 ldo->desc.vsel_reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
616 ldo->desc.vsel_mask = WM831X_LDO11_ON_VSEL_MASK;
617 ldo->desc.enable_reg = WM831X_LDO_ENABLE;
618 ldo->desc.enable_mask = 1 << id;
619 ldo->desc.min_uV = 800000;
620 ldo->desc.uV_step = 50000;
621 ldo->desc.enable_time = 1000;
623 config.dev = pdev->dev.parent;
625 config.init_data = pdata->ldo[id];
626 config.driver_data = ldo;
627 config.regmap = wm831x->regmap;
629 ldo->regulator = devm_regulator_register(&pdev->dev, &ldo->desc,
631 if (IS_ERR(ldo->regulator)) {
632 ret = PTR_ERR(ldo->regulator);
633 dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
638 platform_set_drvdata(pdev, ldo);
646 static struct platform_driver wm831x_alive_ldo_driver = {
647 .probe = wm831x_alive_ldo_probe,
649 .name = "wm831x-alive-ldo",
653 static struct platform_driver * const drivers[] = {
654 &wm831x_gp_ldo_driver,
656 &wm831x_alive_ldo_driver,
659 static int __init wm831x_ldo_init(void)
661 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
663 subsys_initcall(wm831x_ldo_init);
665 static void __exit wm831x_ldo_exit(void)
667 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
669 module_exit(wm831x_ldo_exit);
671 /* Module information */
672 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
673 MODULE_DESCRIPTION("WM831x LDO driver");
674 MODULE_LICENSE("GPL");
675 MODULE_ALIAS("platform:wm831x-ldo");
676 MODULE_ALIAS("platform:wm831x-aldo");
677 MODULE_ALIAS("platform:wm831x-aliveldo");