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_get_voltage(struct regulator_dev *dev)
48 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
51 val = wm8400_reg_read(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev));
52 val &= WM8400_LDO1_VSEL_MASK;
55 return 900000 + (val * 50000);
57 return 1600000 + ((val - 14) * 100000);
60 static int wm8400_ldo_set_voltage(struct regulator_dev *dev,
61 int min_uV, int max_uV)
63 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
66 if (min_uV < 900000 || min_uV > 3300000)
69 if (min_uV < 1700000) {
70 /* Steps of 50mV from 900mV; */
71 val = (min_uV - 850001) / 50000;
73 if ((val * 50000) + 900000 > max_uV)
75 BUG_ON((val * 50000) + 900000 < min_uV);
77 /* Steps of 100mV from 1700mV */
78 val = ((min_uV - 1600001) / 100000);
80 if ((val * 100000) + 1700000 > max_uV)
82 BUG_ON((val * 100000) + 1700000 < min_uV);
87 return wm8400_set_bits(wm8400, WM8400_LDO1_CONTROL + rdev_get_id(dev),
88 WM8400_LDO1_VSEL_MASK, val);
91 static struct regulator_ops wm8400_ldo_ops = {
92 .is_enabled = wm8400_ldo_is_enabled,
93 .enable = wm8400_ldo_enable,
94 .disable = wm8400_ldo_disable,
95 .get_voltage = wm8400_ldo_get_voltage,
96 .set_voltage = wm8400_ldo_set_voltage,
99 static int wm8400_dcdc_is_enabled(struct regulator_dev *dev)
101 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
102 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
105 val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset);
106 return (val & WM8400_DC1_ENA) != 0;
109 static int wm8400_dcdc_enable(struct regulator_dev *dev)
111 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
112 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
114 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
115 WM8400_DC1_ENA, WM8400_DC1_ENA);
118 static int wm8400_dcdc_disable(struct regulator_dev *dev)
120 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
121 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
123 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
127 static int wm8400_dcdc_get_voltage(struct regulator_dev *dev)
129 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
131 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
133 val = wm8400_reg_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset);
134 val &= WM8400_DC1_VSEL_MASK;
136 return 850000 + (25000 * val);
139 static int wm8400_dcdc_set_voltage(struct regulator_dev *dev,
140 int min_uV, int max_uV)
142 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
144 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
149 val = (min_uV - 825001) / 25000;
151 if (850000 + (25000 * val) > max_uV)
153 BUG_ON(850000 + (25000 * val) < min_uV);
155 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
156 WM8400_DC1_VSEL_MASK, val);
159 static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev)
161 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
162 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
166 ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2,
171 /* Datasheet: hibernate */
172 if (data[0] & WM8400_DC1_SLEEP)
173 return REGULATOR_MODE_STANDBY;
175 /* Datasheet: standby */
176 if (!(data[0] & WM8400_DC1_ACTIVE))
177 return REGULATOR_MODE_IDLE;
179 /* Datasheet: active with or without force PWM */
180 if (data[1] & WM8400_DC1_FRC_PWM)
181 return REGULATOR_MODE_FAST;
183 return REGULATOR_MODE_NORMAL;
186 static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
188 struct wm8400 *wm8400 = rdev_get_drvdata(dev);
189 int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2;
193 case REGULATOR_MODE_FAST:
194 /* Datasheet: active with force PWM */
195 ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
196 WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM);
200 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
201 WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
204 case REGULATOR_MODE_NORMAL:
205 /* Datasheet: active */
206 ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset,
207 WM8400_DC1_FRC_PWM, 0);
211 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
212 WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP,
215 case REGULATOR_MODE_IDLE:
216 /* Datasheet: standby */
217 ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
218 WM8400_DC1_ACTIVE, 0);
221 return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset,
222 WM8400_DC1_SLEEP, 0);
229 static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev,
230 int input_uV, int output_uV,
233 return REGULATOR_MODE_NORMAL;
236 static struct regulator_ops wm8400_dcdc_ops = {
237 .is_enabled = wm8400_dcdc_is_enabled,
238 .enable = wm8400_dcdc_enable,
239 .disable = wm8400_dcdc_disable,
240 .get_voltage = wm8400_dcdc_get_voltage,
241 .set_voltage = wm8400_dcdc_set_voltage,
242 .get_mode = wm8400_dcdc_get_mode,
243 .set_mode = wm8400_dcdc_set_mode,
244 .get_optimum_mode = wm8400_dcdc_get_optimum_mode,
247 static struct regulator_desc regulators[] = {
251 .ops = &wm8400_ldo_ops,
252 .type = REGULATOR_VOLTAGE,
253 .owner = THIS_MODULE,
258 .ops = &wm8400_ldo_ops,
259 .type = REGULATOR_VOLTAGE,
260 .owner = THIS_MODULE,
265 .ops = &wm8400_ldo_ops,
266 .type = REGULATOR_VOLTAGE,
267 .owner = THIS_MODULE,
272 .ops = &wm8400_ldo_ops,
273 .type = REGULATOR_VOLTAGE,
274 .owner = THIS_MODULE,
279 .ops = &wm8400_dcdc_ops,
280 .type = REGULATOR_VOLTAGE,
281 .owner = THIS_MODULE,
286 .ops = &wm8400_dcdc_ops,
287 .type = REGULATOR_VOLTAGE,
288 .owner = THIS_MODULE,
292 static int __devinit wm8400_regulator_probe(struct platform_device *pdev)
294 struct regulator_dev *rdev;
296 rdev = regulator_register(®ulators[pdev->id], &pdev->dev,
297 pdev->dev.driver_data);
300 return PTR_ERR(rdev);
305 static int __devexit wm8400_regulator_remove(struct platform_device *pdev)
307 struct regulator_dev *rdev = platform_get_drvdata(pdev);
309 regulator_unregister(rdev);
314 static struct platform_driver wm8400_regulator_driver = {
316 .name = "wm8400-regulator",
318 .probe = wm8400_regulator_probe,
319 .remove = __devexit_p(wm8400_regulator_remove),
323 * wm8400_register_regulator - enable software control of a WM8400 regulator
325 * This function enables software control of a WM8400 regulator via
326 * the regulator API. It is intended to be called from the
327 * platform_init() callback of the WM8400 MFD driver.
329 * @param dev The WM8400 device to operate on.
330 * @param reg The regulator to control.
331 * @param initdata Regulator initdata for the regulator.
333 int wm8400_register_regulator(struct device *dev, int reg,
334 struct regulator_init_data *initdata)
336 struct wm8400 *wm8400 = dev->driver_data;
338 if (wm8400->regulators[reg].name)
341 initdata->driver_data = wm8400;
343 wm8400->regulators[reg].name = "wm8400-regulator";
344 wm8400->regulators[reg].id = reg;
345 wm8400->regulators[reg].dev.parent = dev;
346 wm8400->regulators[reg].dev.driver_data = wm8400;
347 wm8400->regulators[reg].dev.platform_data = initdata;
349 return platform_device_register(&wm8400->regulators[reg]);
351 EXPORT_SYMBOL_GPL(wm8400_register_regulator);
353 static int __init wm8400_regulator_init(void)
355 return platform_driver_register(&wm8400_regulator_driver);
357 module_init(wm8400_regulator_init);
359 static void __exit wm8400_regulator_exit(void)
361 platform_driver_unregister(&wm8400_regulator_driver);
363 module_exit(wm8400_regulator_exit);
365 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
366 MODULE_DESCRIPTION("WM8400 regulator driver");
367 MODULE_LICENSE("GPL");
368 MODULE_ALIAS("platform:wm8400-regulator");