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/module.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/mfd/wm8400-private.h>
22 static int wm8400_ldo_list_voltage(struct regulator_dev
*dev
,
25 if (selector
> WM8400_LDO1_VSEL_MASK
)
29 return 900000 + (selector
* 50000);
31 return 1600000 + ((selector
- 14) * 100000);
34 static int wm8400_ldo_map_voltage(struct regulator_dev
*dev
,
35 int min_uV
, int max_uV
)
39 if (min_uV
< 900000 || min_uV
> 3300000)
42 if (min_uV
< 1700000) {
43 /* Steps of 50mV from 900mV; */
44 val
= DIV_ROUND_UP(min_uV
- 900000, 50000);
46 if ((val
* 50000) + 900000 > max_uV
)
48 BUG_ON((val
* 50000) + 900000 < min_uV
);
50 /* Steps of 100mV from 1700mV */
51 val
= DIV_ROUND_UP(min_uV
- 1700000, 100000);
53 if ((val
* 100000) + 1700000 > max_uV
)
55 BUG_ON((val
* 100000) + 1700000 < min_uV
);
63 static struct regulator_ops wm8400_ldo_ops
= {
64 .is_enabled
= regulator_is_enabled_regmap
,
65 .enable
= regulator_enable_regmap
,
66 .disable
= regulator_disable_regmap
,
67 .list_voltage
= wm8400_ldo_list_voltage
,
68 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
69 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
70 .map_voltage
= wm8400_ldo_map_voltage
,
73 static unsigned int wm8400_dcdc_get_mode(struct regulator_dev
*dev
)
75 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
76 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
80 ret
= wm8400_block_read(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
, 2,
85 /* Datasheet: hibernate */
86 if (data
[0] & WM8400_DC1_SLEEP
)
87 return REGULATOR_MODE_STANDBY
;
89 /* Datasheet: standby */
90 if (!(data
[0] & WM8400_DC1_ACTIVE
))
91 return REGULATOR_MODE_IDLE
;
93 /* Datasheet: active with or without force PWM */
94 if (data
[1] & WM8400_DC1_FRC_PWM
)
95 return REGULATOR_MODE_FAST
;
97 return REGULATOR_MODE_NORMAL
;
100 static int wm8400_dcdc_set_mode(struct regulator_dev
*dev
, unsigned int mode
)
102 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
103 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
107 case REGULATOR_MODE_FAST
:
108 /* Datasheet: active with force PWM */
109 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
110 WM8400_DC1_FRC_PWM
, WM8400_DC1_FRC_PWM
);
114 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
115 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
118 case REGULATOR_MODE_NORMAL
:
119 /* Datasheet: active */
120 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
121 WM8400_DC1_FRC_PWM
, 0);
125 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
126 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
129 case REGULATOR_MODE_IDLE
:
130 /* Datasheet: standby */
131 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
132 WM8400_DC1_ACTIVE
, 0);
135 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
136 WM8400_DC1_SLEEP
, 0);
143 static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev
*dev
,
144 int input_uV
, int output_uV
,
147 return REGULATOR_MODE_NORMAL
;
150 static struct regulator_ops wm8400_dcdc_ops
= {
151 .is_enabled
= regulator_is_enabled_regmap
,
152 .enable
= regulator_enable_regmap
,
153 .disable
= regulator_disable_regmap
,
154 .list_voltage
= regulator_list_voltage_linear
,
155 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
156 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
157 .get_mode
= wm8400_dcdc_get_mode
,
158 .set_mode
= wm8400_dcdc_set_mode
,
159 .get_optimum_mode
= wm8400_dcdc_get_optimum_mode
,
162 static struct regulator_desc regulators
[] = {
166 .ops
= &wm8400_ldo_ops
,
167 .enable_reg
= WM8400_LDO1_CONTROL
,
168 .enable_mask
= WM8400_LDO1_ENA
,
169 .n_voltages
= WM8400_LDO1_VSEL_MASK
+ 1,
170 .vsel_reg
= WM8400_LDO1_CONTROL
,
171 .vsel_mask
= WM8400_LDO1_VSEL_MASK
,
172 .type
= REGULATOR_VOLTAGE
,
173 .owner
= THIS_MODULE
,
178 .ops
= &wm8400_ldo_ops
,
179 .enable_reg
= WM8400_LDO2_CONTROL
,
180 .enable_mask
= WM8400_LDO2_ENA
,
181 .n_voltages
= WM8400_LDO2_VSEL_MASK
+ 1,
182 .type
= REGULATOR_VOLTAGE
,
183 .vsel_reg
= WM8400_LDO2_CONTROL
,
184 .vsel_mask
= WM8400_LDO2_VSEL_MASK
,
185 .owner
= THIS_MODULE
,
190 .ops
= &wm8400_ldo_ops
,
191 .enable_reg
= WM8400_LDO3_CONTROL
,
192 .enable_mask
= WM8400_LDO3_ENA
,
193 .n_voltages
= WM8400_LDO3_VSEL_MASK
+ 1,
194 .vsel_reg
= WM8400_LDO3_CONTROL
,
195 .vsel_mask
= WM8400_LDO3_VSEL_MASK
,
196 .type
= REGULATOR_VOLTAGE
,
197 .owner
= THIS_MODULE
,
202 .ops
= &wm8400_ldo_ops
,
203 .enable_reg
= WM8400_LDO4_CONTROL
,
204 .enable_mask
= WM8400_LDO4_ENA
,
205 .n_voltages
= WM8400_LDO4_VSEL_MASK
+ 1,
206 .vsel_reg
= WM8400_LDO4_CONTROL
,
207 .vsel_mask
= WM8400_LDO4_VSEL_MASK
,
208 .type
= REGULATOR_VOLTAGE
,
209 .owner
= THIS_MODULE
,
214 .ops
= &wm8400_dcdc_ops
,
215 .enable_reg
= WM8400_DCDC1_CONTROL_1
,
216 .enable_mask
= WM8400_DC1_ENA_MASK
,
217 .n_voltages
= WM8400_DC1_VSEL_MASK
+ 1,
218 .vsel_reg
= WM8400_DCDC1_CONTROL_1
,
219 .vsel_mask
= WM8400_DC1_VSEL_MASK
,
222 .type
= REGULATOR_VOLTAGE
,
223 .owner
= THIS_MODULE
,
228 .ops
= &wm8400_dcdc_ops
,
229 .enable_reg
= WM8400_DCDC2_CONTROL_1
,
230 .enable_mask
= WM8400_DC1_ENA_MASK
,
231 .n_voltages
= WM8400_DC2_VSEL_MASK
+ 1,
232 .vsel_reg
= WM8400_DCDC2_CONTROL_1
,
233 .vsel_mask
= WM8400_DC2_VSEL_MASK
,
236 .type
= REGULATOR_VOLTAGE
,
237 .owner
= THIS_MODULE
,
241 static int __devinit
wm8400_regulator_probe(struct platform_device
*pdev
)
243 struct wm8400
*wm8400
= container_of(pdev
, struct wm8400
, regulators
[pdev
->id
]);
244 struct regulator_config config
= { };
245 struct regulator_dev
*rdev
;
247 config
.dev
= &pdev
->dev
;
248 config
.init_data
= pdev
->dev
.platform_data
;
249 config
.driver_data
= wm8400
;
250 config
.regmap
= wm8400
->regmap
;
252 rdev
= regulator_register(®ulators
[pdev
->id
], &config
);
254 return PTR_ERR(rdev
);
256 platform_set_drvdata(pdev
, rdev
);
261 static int __devexit
wm8400_regulator_remove(struct platform_device
*pdev
)
263 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
265 platform_set_drvdata(pdev
, NULL
);
266 regulator_unregister(rdev
);
271 static struct platform_driver wm8400_regulator_driver
= {
273 .name
= "wm8400-regulator",
275 .probe
= wm8400_regulator_probe
,
276 .remove
= __devexit_p(wm8400_regulator_remove
),
280 * wm8400_register_regulator - enable software control of a WM8400 regulator
282 * This function enables software control of a WM8400 regulator via
283 * the regulator API. It is intended to be called from the
284 * platform_init() callback of the WM8400 MFD driver.
286 * @param dev The WM8400 device to operate on.
287 * @param reg The regulator to control.
288 * @param initdata Regulator initdata for the regulator.
290 int wm8400_register_regulator(struct device
*dev
, int reg
,
291 struct regulator_init_data
*initdata
)
293 struct wm8400
*wm8400
= dev_get_drvdata(dev
);
295 if (wm8400
->regulators
[reg
].name
)
298 initdata
->driver_data
= wm8400
;
300 wm8400
->regulators
[reg
].name
= "wm8400-regulator";
301 wm8400
->regulators
[reg
].id
= reg
;
302 wm8400
->regulators
[reg
].dev
.parent
= dev
;
303 wm8400
->regulators
[reg
].dev
.platform_data
= initdata
;
305 return platform_device_register(&wm8400
->regulators
[reg
]);
307 EXPORT_SYMBOL_GPL(wm8400_register_regulator
);
309 static int __init
wm8400_regulator_init(void)
311 return platform_driver_register(&wm8400_regulator_driver
);
313 subsys_initcall(wm8400_regulator_init
);
315 static void __exit
wm8400_regulator_exit(void)
317 platform_driver_unregister(&wm8400_regulator_driver
);
319 module_exit(wm8400_regulator_exit
);
321 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
322 MODULE_DESCRIPTION("WM8400 regulator driver");
323 MODULE_LICENSE("GPL");
324 MODULE_ALIAS("platform:wm8400-regulator");