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_notifier_call_chain(ldo
->regulator
,
50 REGULATOR_EVENT_UNDER_VOLTAGE
,
57 * General purpose LDOs
60 static const struct linear_range wm831x_gp_ldo_ranges
[] = {
61 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
62 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
65 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev
*rdev
,
68 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
69 struct wm831x
*wm831x
= ldo
->wm831x
;
70 int sel
, reg
= ldo
->base
+ WM831X_LDO_SLEEP_CONTROL
;
72 sel
= regulator_map_voltage_linear_range(rdev
, uV
, uV
);
76 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO1_ON_VSEL_MASK
, sel
);
79 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev
*rdev
)
81 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
82 struct wm831x
*wm831x
= ldo
->wm831x
;
83 int ctrl_reg
= ldo
->base
+ WM831X_LDO_CONTROL
;
84 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
87 ret
= wm831x_reg_read(wm831x
, on_reg
);
91 if (!(ret
& WM831X_LDO1_ON_MODE
))
92 return REGULATOR_MODE_NORMAL
;
94 ret
= wm831x_reg_read(wm831x
, ctrl_reg
);
98 if (ret
& WM831X_LDO1_LP_MODE
)
99 return REGULATOR_MODE_STANDBY
;
101 return REGULATOR_MODE_IDLE
;
104 static int wm831x_gp_ldo_set_mode(struct regulator_dev
*rdev
,
107 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
108 struct wm831x
*wm831x
= ldo
->wm831x
;
109 int ctrl_reg
= ldo
->base
+ WM831X_LDO_CONTROL
;
110 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
115 case REGULATOR_MODE_NORMAL
:
116 ret
= wm831x_set_bits(wm831x
, on_reg
,
117 WM831X_LDO1_ON_MODE
, 0);
122 case REGULATOR_MODE_IDLE
:
123 ret
= wm831x_set_bits(wm831x
, ctrl_reg
,
124 WM831X_LDO1_LP_MODE
, 0);
128 ret
= wm831x_set_bits(wm831x
, on_reg
,
130 WM831X_LDO1_ON_MODE
);
135 case REGULATOR_MODE_STANDBY
:
136 ret
= wm831x_set_bits(wm831x
, ctrl_reg
,
138 WM831X_LDO1_LP_MODE
);
142 ret
= wm831x_set_bits(wm831x
, on_reg
,
144 WM831X_LDO1_ON_MODE
);
156 static int wm831x_gp_ldo_get_status(struct regulator_dev
*rdev
)
158 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
159 struct wm831x
*wm831x
= ldo
->wm831x
;
160 int mask
= 1 << rdev_get_id(rdev
);
163 /* Is the regulator on? */
164 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
168 return REGULATOR_STATUS_OFF
;
170 /* Is it reporting under voltage? */
171 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_UV_STATUS
);
175 return REGULATOR_STATUS_ERROR
;
177 ret
= wm831x_gp_ldo_get_mode(rdev
);
181 return regulator_mode_to_status(ret
);
184 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev
*rdev
,
186 int output_uV
, int load_uA
)
189 return REGULATOR_MODE_STANDBY
;
191 return REGULATOR_MODE_IDLE
;
192 return REGULATOR_MODE_NORMAL
;
196 static const struct regulator_ops wm831x_gp_ldo_ops
= {
197 .list_voltage
= regulator_list_voltage_linear_range
,
198 .map_voltage
= regulator_map_voltage_linear_range
,
199 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
200 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
201 .set_suspend_voltage
= wm831x_gp_ldo_set_suspend_voltage
,
202 .get_mode
= wm831x_gp_ldo_get_mode
,
203 .set_mode
= wm831x_gp_ldo_set_mode
,
204 .get_status
= wm831x_gp_ldo_get_status
,
205 .get_optimum_mode
= wm831x_gp_ldo_get_optimum_mode
,
206 .get_bypass
= regulator_get_bypass_regmap
,
207 .set_bypass
= regulator_set_bypass_regmap
,
209 .is_enabled
= regulator_is_enabled_regmap
,
210 .enable
= regulator_enable_regmap
,
211 .disable
= regulator_disable_regmap
,
214 static int wm831x_gp_ldo_probe(struct platform_device
*pdev
)
216 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
217 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
218 struct regulator_config config
= { };
220 struct wm831x_ldo
*ldo
;
221 struct resource
*res
;
224 if (pdata
&& pdata
->wm831x_num
)
225 id
= (pdata
->wm831x_num
* 10) + 1;
230 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
232 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
236 ldo
->wm831x
= wm831x
;
238 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
240 dev_err(&pdev
->dev
, "No REG resource\n");
244 ldo
->base
= res
->start
;
246 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
247 ldo
->desc
.name
= ldo
->name
;
249 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
251 ldo
->desc
.supply_name
= ldo
->supply_name
;
254 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
255 ldo
->desc
.n_voltages
= 32;
256 ldo
->desc
.ops
= &wm831x_gp_ldo_ops
;
257 ldo
->desc
.owner
= THIS_MODULE
;
258 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
259 ldo
->desc
.vsel_mask
= WM831X_LDO1_ON_VSEL_MASK
;
260 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
261 ldo
->desc
.enable_mask
= 1 << id
;
262 ldo
->desc
.bypass_reg
= ldo
->base
;
263 ldo
->desc
.bypass_mask
= WM831X_LDO1_SWI
;
264 ldo
->desc
.linear_ranges
= wm831x_gp_ldo_ranges
;
265 ldo
->desc
.n_linear_ranges
= ARRAY_SIZE(wm831x_gp_ldo_ranges
);
267 config
.dev
= pdev
->dev
.parent
;
269 config
.init_data
= pdata
->ldo
[id
];
270 config
.driver_data
= ldo
;
271 config
.regmap
= wm831x
->regmap
;
273 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
275 if (IS_ERR(ldo
->regulator
)) {
276 ret
= PTR_ERR(ldo
->regulator
);
277 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
282 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"));
283 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
285 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
289 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
294 platform_set_drvdata(pdev
, ldo
);
302 static struct platform_driver wm831x_gp_ldo_driver
= {
303 .probe
= wm831x_gp_ldo_probe
,
305 .name
= "wm831x-ldo",
313 static const struct linear_range wm831x_aldo_ranges
[] = {
314 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
315 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
318 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev
*rdev
,
321 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
322 struct wm831x
*wm831x
= ldo
->wm831x
;
323 int sel
, reg
= ldo
->base
+ WM831X_LDO_SLEEP_CONTROL
;
325 sel
= regulator_map_voltage_linear_range(rdev
, uV
, uV
);
329 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO7_ON_VSEL_MASK
, sel
);
332 static unsigned int wm831x_aldo_get_mode(struct regulator_dev
*rdev
)
334 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
335 struct wm831x
*wm831x
= ldo
->wm831x
;
336 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
339 ret
= wm831x_reg_read(wm831x
, on_reg
);
343 if (ret
& WM831X_LDO7_ON_MODE
)
344 return REGULATOR_MODE_IDLE
;
346 return REGULATOR_MODE_NORMAL
;
349 static int wm831x_aldo_set_mode(struct regulator_dev
*rdev
,
352 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
353 struct wm831x
*wm831x
= ldo
->wm831x
;
354 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
359 case REGULATOR_MODE_NORMAL
:
360 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_LDO7_ON_MODE
, 0);
365 case REGULATOR_MODE_IDLE
:
366 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_LDO7_ON_MODE
,
367 WM831X_LDO7_ON_MODE
);
379 static int wm831x_aldo_get_status(struct regulator_dev
*rdev
)
381 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
382 struct wm831x
*wm831x
= ldo
->wm831x
;
383 int mask
= 1 << rdev_get_id(rdev
);
386 /* Is the regulator on? */
387 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
391 return REGULATOR_STATUS_OFF
;
393 /* Is it reporting under voltage? */
394 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_UV_STATUS
);
398 return REGULATOR_STATUS_ERROR
;
400 ret
= wm831x_aldo_get_mode(rdev
);
404 return regulator_mode_to_status(ret
);
407 static const struct regulator_ops wm831x_aldo_ops
= {
408 .list_voltage
= regulator_list_voltage_linear_range
,
409 .map_voltage
= regulator_map_voltage_linear_range
,
410 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
411 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
412 .set_suspend_voltage
= wm831x_aldo_set_suspend_voltage
,
413 .get_mode
= wm831x_aldo_get_mode
,
414 .set_mode
= wm831x_aldo_set_mode
,
415 .get_status
= wm831x_aldo_get_status
,
416 .set_bypass
= regulator_set_bypass_regmap
,
417 .get_bypass
= regulator_get_bypass_regmap
,
419 .is_enabled
= regulator_is_enabled_regmap
,
420 .enable
= regulator_enable_regmap
,
421 .disable
= regulator_disable_regmap
,
424 static int wm831x_aldo_probe(struct platform_device
*pdev
)
426 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
427 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
428 struct regulator_config config
= { };
430 struct wm831x_ldo
*ldo
;
431 struct resource
*res
;
434 if (pdata
&& pdata
->wm831x_num
)
435 id
= (pdata
->wm831x_num
* 10) + 1;
440 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
442 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
446 ldo
->wm831x
= wm831x
;
448 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
450 dev_err(&pdev
->dev
, "No REG resource\n");
454 ldo
->base
= res
->start
;
456 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
457 ldo
->desc
.name
= ldo
->name
;
459 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
461 ldo
->desc
.supply_name
= ldo
->supply_name
;
464 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
465 ldo
->desc
.n_voltages
= 32;
466 ldo
->desc
.linear_ranges
= wm831x_aldo_ranges
;
467 ldo
->desc
.n_linear_ranges
= ARRAY_SIZE(wm831x_aldo_ranges
);
468 ldo
->desc
.ops
= &wm831x_aldo_ops
;
469 ldo
->desc
.owner
= THIS_MODULE
;
470 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
471 ldo
->desc
.vsel_mask
= WM831X_LDO7_ON_VSEL_MASK
;
472 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
473 ldo
->desc
.enable_mask
= 1 << id
;
474 ldo
->desc
.bypass_reg
= ldo
->base
;
475 ldo
->desc
.bypass_mask
= WM831X_LDO7_SWI
;
477 config
.dev
= pdev
->dev
.parent
;
479 config
.init_data
= pdata
->ldo
[id
];
480 config
.driver_data
= ldo
;
481 config
.regmap
= wm831x
->regmap
;
483 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
485 if (IS_ERR(ldo
->regulator
)) {
486 ret
= PTR_ERR(ldo
->regulator
);
487 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
492 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"));
493 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
495 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
498 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
503 platform_set_drvdata(pdev
, ldo
);
511 static struct platform_driver wm831x_aldo_driver
= {
512 .probe
= wm831x_aldo_probe
,
514 .name
= "wm831x-aldo",
522 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
524 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev
*rdev
,
527 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
528 struct wm831x
*wm831x
= ldo
->wm831x
;
529 int sel
, reg
= ldo
->base
+ WM831X_ALIVE_LDO_SLEEP_CONTROL
;
531 sel
= regulator_map_voltage_linear(rdev
, uV
, uV
);
535 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO11_ON_VSEL_MASK
, sel
);
538 static int wm831x_alive_ldo_get_status(struct regulator_dev
*rdev
)
540 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
541 struct wm831x
*wm831x
= ldo
->wm831x
;
542 int mask
= 1 << rdev_get_id(rdev
);
545 /* Is the regulator on? */
546 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
550 return REGULATOR_STATUS_ON
;
552 return REGULATOR_STATUS_OFF
;
555 static const struct regulator_ops wm831x_alive_ldo_ops
= {
556 .list_voltage
= regulator_list_voltage_linear
,
557 .map_voltage
= regulator_map_voltage_linear
,
558 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
559 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
560 .set_suspend_voltage
= wm831x_alive_ldo_set_suspend_voltage
,
561 .get_status
= wm831x_alive_ldo_get_status
,
563 .is_enabled
= regulator_is_enabled_regmap
,
564 .enable
= regulator_enable_regmap
,
565 .disable
= regulator_disable_regmap
,
568 static int wm831x_alive_ldo_probe(struct platform_device
*pdev
)
570 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
571 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
572 struct regulator_config config
= { };
574 struct wm831x_ldo
*ldo
;
575 struct resource
*res
;
578 if (pdata
&& pdata
->wm831x_num
)
579 id
= (pdata
->wm831x_num
* 10) + 1;
585 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
587 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
591 ldo
->wm831x
= wm831x
;
593 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
595 dev_err(&pdev
->dev
, "No REG resource\n");
599 ldo
->base
= res
->start
;
601 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
602 ldo
->desc
.name
= ldo
->name
;
604 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
606 ldo
->desc
.supply_name
= ldo
->supply_name
;
609 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
610 ldo
->desc
.n_voltages
= WM831X_ALIVE_LDO_MAX_SELECTOR
+ 1;
611 ldo
->desc
.ops
= &wm831x_alive_ldo_ops
;
612 ldo
->desc
.owner
= THIS_MODULE
;
613 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_ALIVE_LDO_ON_CONTROL
;
614 ldo
->desc
.vsel_mask
= WM831X_LDO11_ON_VSEL_MASK
;
615 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
616 ldo
->desc
.enable_mask
= 1 << id
;
617 ldo
->desc
.min_uV
= 800000;
618 ldo
->desc
.uV_step
= 50000;
619 ldo
->desc
.enable_time
= 1000;
621 config
.dev
= pdev
->dev
.parent
;
623 config
.init_data
= pdata
->ldo
[id
];
624 config
.driver_data
= ldo
;
625 config
.regmap
= wm831x
->regmap
;
627 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
629 if (IS_ERR(ldo
->regulator
)) {
630 ret
= PTR_ERR(ldo
->regulator
);
631 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
636 platform_set_drvdata(pdev
, ldo
);
644 static struct platform_driver wm831x_alive_ldo_driver
= {
645 .probe
= wm831x_alive_ldo_probe
,
647 .name
= "wm831x-alive-ldo",
651 static struct platform_driver
* const drivers
[] = {
652 &wm831x_gp_ldo_driver
,
654 &wm831x_alive_ldo_driver
,
657 static int __init
wm831x_ldo_init(void)
659 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
661 subsys_initcall(wm831x_ldo_init
);
663 static void __exit
wm831x_ldo_exit(void)
665 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
667 module_exit(wm831x_ldo_exit
);
669 /* Module information */
670 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
671 MODULE_DESCRIPTION("WM831x LDO driver");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:wm831x-ldo");
674 MODULE_ALIAS("platform:wm831x-aldo");
675 MODULE_ALIAS("platform:wm831x-aliveldo");