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_lock(ldo
->regulator
);
55 regulator_notifier_call_chain(ldo
->regulator
,
56 REGULATOR_EVENT_UNDER_VOLTAGE
,
58 regulator_unlock(ldo
->regulator
);
64 * General purpose LDOs
67 static const struct regulator_linear_range wm831x_gp_ldo_ranges
[] = {
68 REGULATOR_LINEAR_RANGE(900000, 0, 14, 50000),
69 REGULATOR_LINEAR_RANGE(1700000, 15, 31, 100000),
72 static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev
*rdev
,
75 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
76 struct wm831x
*wm831x
= ldo
->wm831x
;
77 int sel
, reg
= ldo
->base
+ WM831X_LDO_SLEEP_CONTROL
;
79 sel
= regulator_map_voltage_linear_range(rdev
, uV
, uV
);
83 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO1_ON_VSEL_MASK
, sel
);
86 static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev
*rdev
)
88 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
89 struct wm831x
*wm831x
= ldo
->wm831x
;
90 int ctrl_reg
= ldo
->base
+ WM831X_LDO_CONTROL
;
91 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
94 ret
= wm831x_reg_read(wm831x
, on_reg
);
98 if (!(ret
& WM831X_LDO1_ON_MODE
))
99 return REGULATOR_MODE_NORMAL
;
101 ret
= wm831x_reg_read(wm831x
, ctrl_reg
);
105 if (ret
& WM831X_LDO1_LP_MODE
)
106 return REGULATOR_MODE_STANDBY
;
108 return REGULATOR_MODE_IDLE
;
111 static int wm831x_gp_ldo_set_mode(struct regulator_dev
*rdev
,
114 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
115 struct wm831x
*wm831x
= ldo
->wm831x
;
116 int ctrl_reg
= ldo
->base
+ WM831X_LDO_CONTROL
;
117 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
122 case REGULATOR_MODE_NORMAL
:
123 ret
= wm831x_set_bits(wm831x
, on_reg
,
124 WM831X_LDO1_ON_MODE
, 0);
129 case REGULATOR_MODE_IDLE
:
130 ret
= wm831x_set_bits(wm831x
, ctrl_reg
,
131 WM831X_LDO1_LP_MODE
, 0);
135 ret
= wm831x_set_bits(wm831x
, on_reg
,
137 WM831X_LDO1_ON_MODE
);
142 case REGULATOR_MODE_STANDBY
:
143 ret
= wm831x_set_bits(wm831x
, ctrl_reg
,
145 WM831X_LDO1_LP_MODE
);
149 ret
= wm831x_set_bits(wm831x
, on_reg
,
151 WM831X_LDO1_ON_MODE
);
163 static int wm831x_gp_ldo_get_status(struct regulator_dev
*rdev
)
165 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
166 struct wm831x
*wm831x
= ldo
->wm831x
;
167 int mask
= 1 << rdev_get_id(rdev
);
170 /* Is the regulator on? */
171 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
175 return REGULATOR_STATUS_OFF
;
177 /* Is it reporting under voltage? */
178 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_UV_STATUS
);
182 return REGULATOR_STATUS_ERROR
;
184 ret
= wm831x_gp_ldo_get_mode(rdev
);
188 return regulator_mode_to_status(ret
);
191 static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev
*rdev
,
193 int output_uV
, int load_uA
)
196 return REGULATOR_MODE_STANDBY
;
198 return REGULATOR_MODE_IDLE
;
199 return REGULATOR_MODE_NORMAL
;
203 static const struct regulator_ops wm831x_gp_ldo_ops
= {
204 .list_voltage
= regulator_list_voltage_linear_range
,
205 .map_voltage
= regulator_map_voltage_linear_range
,
206 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
207 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
208 .set_suspend_voltage
= wm831x_gp_ldo_set_suspend_voltage
,
209 .get_mode
= wm831x_gp_ldo_get_mode
,
210 .set_mode
= wm831x_gp_ldo_set_mode
,
211 .get_status
= wm831x_gp_ldo_get_status
,
212 .get_optimum_mode
= wm831x_gp_ldo_get_optimum_mode
,
213 .get_bypass
= regulator_get_bypass_regmap
,
214 .set_bypass
= regulator_set_bypass_regmap
,
216 .is_enabled
= regulator_is_enabled_regmap
,
217 .enable
= regulator_enable_regmap
,
218 .disable
= regulator_disable_regmap
,
221 static int wm831x_gp_ldo_probe(struct platform_device
*pdev
)
223 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
224 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
225 struct regulator_config config
= { };
227 struct wm831x_ldo
*ldo
;
228 struct resource
*res
;
231 if (pdata
&& pdata
->wm831x_num
)
232 id
= (pdata
->wm831x_num
* 10) + 1;
237 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
239 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
243 ldo
->wm831x
= wm831x
;
245 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
247 dev_err(&pdev
->dev
, "No REG resource\n");
251 ldo
->base
= res
->start
;
253 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
254 ldo
->desc
.name
= ldo
->name
;
256 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
258 ldo
->desc
.supply_name
= ldo
->supply_name
;
261 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
262 ldo
->desc
.n_voltages
= 32;
263 ldo
->desc
.ops
= &wm831x_gp_ldo_ops
;
264 ldo
->desc
.owner
= THIS_MODULE
;
265 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
266 ldo
->desc
.vsel_mask
= WM831X_LDO1_ON_VSEL_MASK
;
267 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
268 ldo
->desc
.enable_mask
= 1 << id
;
269 ldo
->desc
.bypass_reg
= ldo
->base
;
270 ldo
->desc
.bypass_mask
= WM831X_LDO1_SWI
;
271 ldo
->desc
.linear_ranges
= wm831x_gp_ldo_ranges
;
272 ldo
->desc
.n_linear_ranges
= ARRAY_SIZE(wm831x_gp_ldo_ranges
);
274 config
.dev
= pdev
->dev
.parent
;
276 config
.init_data
= pdata
->ldo
[id
];
277 config
.driver_data
= ldo
;
278 config
.regmap
= wm831x
->regmap
;
280 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
282 if (IS_ERR(ldo
->regulator
)) {
283 ret
= PTR_ERR(ldo
->regulator
);
284 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
289 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"));
290 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
292 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
296 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
301 platform_set_drvdata(pdev
, ldo
);
309 static struct platform_driver wm831x_gp_ldo_driver
= {
310 .probe
= wm831x_gp_ldo_probe
,
312 .name
= "wm831x-ldo",
320 static const struct regulator_linear_range wm831x_aldo_ranges
[] = {
321 REGULATOR_LINEAR_RANGE(1000000, 0, 12, 50000),
322 REGULATOR_LINEAR_RANGE(1700000, 13, 31, 100000),
325 static int wm831x_aldo_set_suspend_voltage(struct regulator_dev
*rdev
,
328 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
329 struct wm831x
*wm831x
= ldo
->wm831x
;
330 int sel
, reg
= ldo
->base
+ WM831X_LDO_SLEEP_CONTROL
;
332 sel
= regulator_map_voltage_linear_range(rdev
, uV
, uV
);
336 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO7_ON_VSEL_MASK
, sel
);
339 static unsigned int wm831x_aldo_get_mode(struct regulator_dev
*rdev
)
341 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
342 struct wm831x
*wm831x
= ldo
->wm831x
;
343 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
346 ret
= wm831x_reg_read(wm831x
, on_reg
);
350 if (ret
& WM831X_LDO7_ON_MODE
)
351 return REGULATOR_MODE_IDLE
;
353 return REGULATOR_MODE_NORMAL
;
356 static int wm831x_aldo_set_mode(struct regulator_dev
*rdev
,
359 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
360 struct wm831x
*wm831x
= ldo
->wm831x
;
361 int on_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
366 case REGULATOR_MODE_NORMAL
:
367 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_LDO7_ON_MODE
, 0);
372 case REGULATOR_MODE_IDLE
:
373 ret
= wm831x_set_bits(wm831x
, on_reg
, WM831X_LDO7_ON_MODE
,
374 WM831X_LDO7_ON_MODE
);
386 static int wm831x_aldo_get_status(struct regulator_dev
*rdev
)
388 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
389 struct wm831x
*wm831x
= ldo
->wm831x
;
390 int mask
= 1 << rdev_get_id(rdev
);
393 /* Is the regulator on? */
394 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
398 return REGULATOR_STATUS_OFF
;
400 /* Is it reporting under voltage? */
401 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_UV_STATUS
);
405 return REGULATOR_STATUS_ERROR
;
407 ret
= wm831x_aldo_get_mode(rdev
);
411 return regulator_mode_to_status(ret
);
414 static const struct regulator_ops wm831x_aldo_ops
= {
415 .list_voltage
= regulator_list_voltage_linear_range
,
416 .map_voltage
= regulator_map_voltage_linear_range
,
417 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
418 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
419 .set_suspend_voltage
= wm831x_aldo_set_suspend_voltage
,
420 .get_mode
= wm831x_aldo_get_mode
,
421 .set_mode
= wm831x_aldo_set_mode
,
422 .get_status
= wm831x_aldo_get_status
,
423 .set_bypass
= regulator_set_bypass_regmap
,
424 .get_bypass
= regulator_get_bypass_regmap
,
426 .is_enabled
= regulator_is_enabled_regmap
,
427 .enable
= regulator_enable_regmap
,
428 .disable
= regulator_disable_regmap
,
431 static int wm831x_aldo_probe(struct platform_device
*pdev
)
433 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
434 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
435 struct regulator_config config
= { };
437 struct wm831x_ldo
*ldo
;
438 struct resource
*res
;
441 if (pdata
&& pdata
->wm831x_num
)
442 id
= (pdata
->wm831x_num
* 10) + 1;
447 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
449 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
453 ldo
->wm831x
= wm831x
;
455 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
457 dev_err(&pdev
->dev
, "No REG resource\n");
461 ldo
->base
= res
->start
;
463 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
464 ldo
->desc
.name
= ldo
->name
;
466 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
468 ldo
->desc
.supply_name
= ldo
->supply_name
;
471 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
472 ldo
->desc
.n_voltages
= 32;
473 ldo
->desc
.linear_ranges
= wm831x_aldo_ranges
;
474 ldo
->desc
.n_linear_ranges
= ARRAY_SIZE(wm831x_aldo_ranges
);
475 ldo
->desc
.ops
= &wm831x_aldo_ops
;
476 ldo
->desc
.owner
= THIS_MODULE
;
477 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_LDO_ON_CONTROL
;
478 ldo
->desc
.vsel_mask
= WM831X_LDO7_ON_VSEL_MASK
;
479 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
480 ldo
->desc
.enable_mask
= 1 << id
;
481 ldo
->desc
.bypass_reg
= ldo
->base
;
482 ldo
->desc
.bypass_mask
= WM831X_LDO7_SWI
;
484 config
.dev
= pdev
->dev
.parent
;
486 config
.init_data
= pdata
->ldo
[id
];
487 config
.driver_data
= ldo
;
488 config
.regmap
= wm831x
->regmap
;
490 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
492 if (IS_ERR(ldo
->regulator
)) {
493 ret
= PTR_ERR(ldo
->regulator
);
494 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
499 irq
= wm831x_irq(wm831x
, platform_get_irq_byname(pdev
, "UV"));
500 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
502 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
505 dev_err(&pdev
->dev
, "Failed to request UV IRQ %d: %d\n",
510 platform_set_drvdata(pdev
, ldo
);
518 static struct platform_driver wm831x_aldo_driver
= {
519 .probe
= wm831x_aldo_probe
,
521 .name
= "wm831x-aldo",
529 #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
531 static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev
*rdev
,
534 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
535 struct wm831x
*wm831x
= ldo
->wm831x
;
536 int sel
, reg
= ldo
->base
+ WM831X_ALIVE_LDO_SLEEP_CONTROL
;
538 sel
= regulator_map_voltage_linear(rdev
, uV
, uV
);
542 return wm831x_set_bits(wm831x
, reg
, WM831X_LDO11_ON_VSEL_MASK
, sel
);
545 static int wm831x_alive_ldo_get_status(struct regulator_dev
*rdev
)
547 struct wm831x_ldo
*ldo
= rdev_get_drvdata(rdev
);
548 struct wm831x
*wm831x
= ldo
->wm831x
;
549 int mask
= 1 << rdev_get_id(rdev
);
552 /* Is the regulator on? */
553 ret
= wm831x_reg_read(wm831x
, WM831X_LDO_STATUS
);
557 return REGULATOR_STATUS_ON
;
559 return REGULATOR_STATUS_OFF
;
562 static const struct regulator_ops wm831x_alive_ldo_ops
= {
563 .list_voltage
= regulator_list_voltage_linear
,
564 .map_voltage
= regulator_map_voltage_linear
,
565 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
566 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
567 .set_suspend_voltage
= wm831x_alive_ldo_set_suspend_voltage
,
568 .get_status
= wm831x_alive_ldo_get_status
,
570 .is_enabled
= regulator_is_enabled_regmap
,
571 .enable
= regulator_enable_regmap
,
572 .disable
= regulator_disable_regmap
,
575 static int wm831x_alive_ldo_probe(struct platform_device
*pdev
)
577 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
578 struct wm831x_pdata
*pdata
= dev_get_platdata(wm831x
->dev
);
579 struct regulator_config config
= { };
581 struct wm831x_ldo
*ldo
;
582 struct resource
*res
;
585 if (pdata
&& pdata
->wm831x_num
)
586 id
= (pdata
->wm831x_num
* 10) + 1;
592 dev_dbg(&pdev
->dev
, "Probing LDO%d\n", id
+ 1);
594 ldo
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_ldo
), GFP_KERNEL
);
598 ldo
->wm831x
= wm831x
;
600 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
602 dev_err(&pdev
->dev
, "No REG resource\n");
606 ldo
->base
= res
->start
;
608 snprintf(ldo
->name
, sizeof(ldo
->name
), "LDO%d", id
+ 1);
609 ldo
->desc
.name
= ldo
->name
;
611 snprintf(ldo
->supply_name
, sizeof(ldo
->supply_name
),
613 ldo
->desc
.supply_name
= ldo
->supply_name
;
616 ldo
->desc
.type
= REGULATOR_VOLTAGE
;
617 ldo
->desc
.n_voltages
= WM831X_ALIVE_LDO_MAX_SELECTOR
+ 1;
618 ldo
->desc
.ops
= &wm831x_alive_ldo_ops
;
619 ldo
->desc
.owner
= THIS_MODULE
;
620 ldo
->desc
.vsel_reg
= ldo
->base
+ WM831X_ALIVE_LDO_ON_CONTROL
;
621 ldo
->desc
.vsel_mask
= WM831X_LDO11_ON_VSEL_MASK
;
622 ldo
->desc
.enable_reg
= WM831X_LDO_ENABLE
;
623 ldo
->desc
.enable_mask
= 1 << id
;
624 ldo
->desc
.min_uV
= 800000;
625 ldo
->desc
.uV_step
= 50000;
626 ldo
->desc
.enable_time
= 1000;
628 config
.dev
= pdev
->dev
.parent
;
630 config
.init_data
= pdata
->ldo
[id
];
631 config
.driver_data
= ldo
;
632 config
.regmap
= wm831x
->regmap
;
634 ldo
->regulator
= devm_regulator_register(&pdev
->dev
, &ldo
->desc
,
636 if (IS_ERR(ldo
->regulator
)) {
637 ret
= PTR_ERR(ldo
->regulator
);
638 dev_err(wm831x
->dev
, "Failed to register LDO%d: %d\n",
643 platform_set_drvdata(pdev
, ldo
);
651 static struct platform_driver wm831x_alive_ldo_driver
= {
652 .probe
= wm831x_alive_ldo_probe
,
654 .name
= "wm831x-alive-ldo",
658 static struct platform_driver
* const drivers
[] = {
659 &wm831x_gp_ldo_driver
,
661 &wm831x_alive_ldo_driver
,
664 static int __init
wm831x_ldo_init(void)
666 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
668 subsys_initcall(wm831x_ldo_init
);
670 static void __exit
wm831x_ldo_exit(void)
672 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
674 module_exit(wm831x_ldo_exit
);
676 /* Module information */
677 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
678 MODULE_DESCRIPTION("WM831x LDO driver");
679 MODULE_LICENSE("GPL");
680 MODULE_ALIAS("platform:wm831x-ldo");
681 MODULE_ALIAS("platform:wm831x-aldo");
682 MODULE_ALIAS("platform:wm831x-aliveldo");