2 * Regulator support for WM8400
4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/mfd/wm8400-private.h>
21 static int wm8400_ldo_is_enabled(struct regulator_dev
*dev
)
23 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
26 val
= wm8400_reg_read(wm8400
, WM8400_LDO1_CONTROL
+ rdev_get_id(dev
));
27 return (val
& WM8400_LDO1_ENA
) != 0;
30 static int wm8400_ldo_enable(struct regulator_dev
*dev
)
32 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
34 return wm8400_set_bits(wm8400
, WM8400_LDO1_CONTROL
+ rdev_get_id(dev
),
35 WM8400_LDO1_ENA
, WM8400_LDO1_ENA
);
38 static int wm8400_ldo_disable(struct regulator_dev
*dev
)
40 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
42 return wm8400_set_bits(wm8400
, WM8400_LDO1_CONTROL
+ rdev_get_id(dev
),
46 static int wm8400_ldo_list_voltage(struct regulator_dev
*dev
,
49 if (selector
> WM8400_LDO1_VSEL_MASK
)
53 return 900000 + (selector
* 50000);
55 return 1600000 + ((selector
- 14) * 100000);
58 static int wm8400_ldo_get_voltage(struct regulator_dev
*dev
)
60 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
63 val
= wm8400_reg_read(wm8400
, WM8400_LDO1_CONTROL
+ rdev_get_id(dev
));
64 val
&= WM8400_LDO1_VSEL_MASK
;
66 return wm8400_ldo_list_voltage(dev
, val
);
69 static int wm8400_ldo_set_voltage(struct regulator_dev
*dev
,
70 int min_uV
, int max_uV
)
72 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
75 if (min_uV
< 900000 || min_uV
> 3300000)
78 if (min_uV
< 1700000) {
79 /* Steps of 50mV from 900mV; */
80 val
= (min_uV
- 850001) / 50000;
82 if ((val
* 50000) + 900000 > max_uV
)
84 BUG_ON((val
* 50000) + 900000 < min_uV
);
86 /* Steps of 100mV from 1700mV */
87 val
= ((min_uV
- 1600001) / 100000);
89 if ((val
* 100000) + 1700000 > max_uV
)
91 BUG_ON((val
* 100000) + 1700000 < min_uV
);
96 return wm8400_set_bits(wm8400
, WM8400_LDO1_CONTROL
+ rdev_get_id(dev
),
97 WM8400_LDO1_VSEL_MASK
, val
);
100 static struct regulator_ops wm8400_ldo_ops
= {
101 .is_enabled
= wm8400_ldo_is_enabled
,
102 .enable
= wm8400_ldo_enable
,
103 .disable
= wm8400_ldo_disable
,
104 .list_voltage
= wm8400_ldo_list_voltage
,
105 .get_voltage
= wm8400_ldo_get_voltage
,
106 .set_voltage
= wm8400_ldo_set_voltage
,
109 static int wm8400_dcdc_is_enabled(struct regulator_dev
*dev
)
111 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
112 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
115 val
= wm8400_reg_read(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
);
116 return (val
& WM8400_DC1_ENA
) != 0;
119 static int wm8400_dcdc_enable(struct regulator_dev
*dev
)
121 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
122 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
124 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
125 WM8400_DC1_ENA
, WM8400_DC1_ENA
);
128 static int wm8400_dcdc_disable(struct regulator_dev
*dev
)
130 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
131 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
133 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
137 static int wm8400_dcdc_list_voltage(struct regulator_dev
*dev
,
140 if (selector
> WM8400_DC1_VSEL_MASK
)
143 return 850000 + (selector
* 25000);
146 static int wm8400_dcdc_get_voltage(struct regulator_dev
*dev
)
148 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
150 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
152 val
= wm8400_reg_read(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
);
153 val
&= WM8400_DC1_VSEL_MASK
;
155 return 850000 + (25000 * val
);
158 static int wm8400_dcdc_set_voltage(struct regulator_dev
*dev
,
159 int min_uV
, int max_uV
)
161 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
163 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
168 val
= (min_uV
- 825001) / 25000;
170 if (850000 + (25000 * val
) > max_uV
)
172 BUG_ON(850000 + (25000 * val
) < min_uV
);
174 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
175 WM8400_DC1_VSEL_MASK
, val
);
178 static unsigned int wm8400_dcdc_get_mode(struct regulator_dev
*dev
)
180 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
181 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
185 ret
= wm8400_block_read(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
, 2,
190 /* Datasheet: hibernate */
191 if (data
[0] & WM8400_DC1_SLEEP
)
192 return REGULATOR_MODE_STANDBY
;
194 /* Datasheet: standby */
195 if (!(data
[0] & WM8400_DC1_ACTIVE
))
196 return REGULATOR_MODE_IDLE
;
198 /* Datasheet: active with or without force PWM */
199 if (data
[1] & WM8400_DC1_FRC_PWM
)
200 return REGULATOR_MODE_FAST
;
202 return REGULATOR_MODE_NORMAL
;
205 static int wm8400_dcdc_set_mode(struct regulator_dev
*dev
, unsigned int mode
)
207 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
208 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
212 case REGULATOR_MODE_FAST
:
213 /* Datasheet: active with force PWM */
214 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
215 WM8400_DC1_FRC_PWM
, WM8400_DC1_FRC_PWM
);
219 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
220 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
223 case REGULATOR_MODE_NORMAL
:
224 /* Datasheet: active */
225 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
226 WM8400_DC1_FRC_PWM
, 0);
230 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
231 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
234 case REGULATOR_MODE_IDLE
:
235 /* Datasheet: standby */
236 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
237 WM8400_DC1_ACTIVE
, 0);
240 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
241 WM8400_DC1_SLEEP
, 0);
248 static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev
*dev
,
249 int input_uV
, int output_uV
,
252 return REGULATOR_MODE_NORMAL
;
255 static struct regulator_ops wm8400_dcdc_ops
= {
256 .is_enabled
= wm8400_dcdc_is_enabled
,
257 .enable
= wm8400_dcdc_enable
,
258 .disable
= wm8400_dcdc_disable
,
259 .list_voltage
= wm8400_dcdc_list_voltage
,
260 .get_voltage
= wm8400_dcdc_get_voltage
,
261 .set_voltage
= wm8400_dcdc_set_voltage
,
262 .get_mode
= wm8400_dcdc_get_mode
,
263 .set_mode
= wm8400_dcdc_set_mode
,
264 .get_optimum_mode
= wm8400_dcdc_get_optimum_mode
,
267 static struct regulator_desc regulators
[] = {
271 .ops
= &wm8400_ldo_ops
,
272 .n_voltages
= WM8400_LDO1_VSEL_MASK
+ 1,
273 .type
= REGULATOR_VOLTAGE
,
274 .owner
= THIS_MODULE
,
279 .ops
= &wm8400_ldo_ops
,
280 .n_voltages
= WM8400_LDO2_VSEL_MASK
+ 1,
281 .type
= REGULATOR_VOLTAGE
,
282 .owner
= THIS_MODULE
,
287 .ops
= &wm8400_ldo_ops
,
288 .n_voltages
= WM8400_LDO3_VSEL_MASK
+ 1,
289 .type
= REGULATOR_VOLTAGE
,
290 .owner
= THIS_MODULE
,
295 .ops
= &wm8400_ldo_ops
,
296 .n_voltages
= WM8400_LDO4_VSEL_MASK
+ 1,
297 .type
= REGULATOR_VOLTAGE
,
298 .owner
= THIS_MODULE
,
303 .ops
= &wm8400_dcdc_ops
,
304 .n_voltages
= WM8400_DC1_VSEL_MASK
+ 1,
305 .type
= REGULATOR_VOLTAGE
,
306 .owner
= THIS_MODULE
,
311 .ops
= &wm8400_dcdc_ops
,
312 .n_voltages
= WM8400_DC2_VSEL_MASK
+ 1,
313 .type
= REGULATOR_VOLTAGE
,
314 .owner
= THIS_MODULE
,
318 static int __devinit
wm8400_regulator_probe(struct platform_device
*pdev
)
320 struct wm8400
*wm8400
= container_of(pdev
, struct wm8400
, regulators
[pdev
->id
]);
321 struct regulator_dev
*rdev
;
323 rdev
= regulator_register(®ulators
[pdev
->id
], &pdev
->dev
,
324 pdev
->dev
.platform_data
, wm8400
);
327 return PTR_ERR(rdev
);
329 platform_set_drvdata(pdev
, rdev
);
334 static int __devexit
wm8400_regulator_remove(struct platform_device
*pdev
)
336 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
338 platform_set_drvdata(pdev
, NULL
);
339 regulator_unregister(rdev
);
344 static struct platform_driver wm8400_regulator_driver
= {
346 .name
= "wm8400-regulator",
348 .probe
= wm8400_regulator_probe
,
349 .remove
= __devexit_p(wm8400_regulator_remove
),
353 * wm8400_register_regulator - enable software control of a WM8400 regulator
355 * This function enables software control of a WM8400 regulator via
356 * the regulator API. It is intended to be called from the
357 * platform_init() callback of the WM8400 MFD driver.
359 * @param dev The WM8400 device to operate on.
360 * @param reg The regulator to control.
361 * @param initdata Regulator initdata for the regulator.
363 int wm8400_register_regulator(struct device
*dev
, int reg
,
364 struct regulator_init_data
*initdata
)
366 struct wm8400
*wm8400
= dev_get_drvdata(dev
);
368 if (wm8400
->regulators
[reg
].name
)
371 initdata
->driver_data
= wm8400
;
373 wm8400
->regulators
[reg
].name
= "wm8400-regulator";
374 wm8400
->regulators
[reg
].id
= reg
;
375 wm8400
->regulators
[reg
].dev
.parent
= dev
;
376 wm8400
->regulators
[reg
].dev
.platform_data
= initdata
;
378 return platform_device_register(&wm8400
->regulators
[reg
]);
380 EXPORT_SYMBOL_GPL(wm8400_register_regulator
);
382 static int __init
wm8400_regulator_init(void)
384 return platform_driver_register(&wm8400_regulator_driver
);
386 subsys_initcall(wm8400_regulator_init
);
388 static void __exit
wm8400_regulator_exit(void)
390 platform_driver_unregister(&wm8400_regulator_driver
);
392 module_exit(wm8400_regulator_exit
);
394 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
395 MODULE_DESCRIPTION("WM8400 regulator driver");
396 MODULE_LICENSE("GPL");
397 MODULE_ALIAS("platform:wm8400-regulator");