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 const struct regulator_linear_range wm8400_ldo_ranges
[] = {
23 { .min_uV
= 900000, .max_uV
= 1600000, .min_sel
= 0, .max_sel
= 14,
25 { .min_uV
= 1700000, .max_uV
= 3300000, .min_sel
= 15, .max_sel
= 31,
29 static struct regulator_ops wm8400_ldo_ops
= {
30 .is_enabled
= regulator_is_enabled_regmap
,
31 .enable
= regulator_enable_regmap
,
32 .disable
= regulator_disable_regmap
,
33 .list_voltage
= regulator_list_voltage_linear_range
,
34 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
35 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
36 .map_voltage
= regulator_map_voltage_linear_range
,
39 static unsigned int wm8400_dcdc_get_mode(struct regulator_dev
*dev
)
41 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
42 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
46 ret
= wm8400_block_read(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
, 2,
51 /* Datasheet: hibernate */
52 if (data
[0] & WM8400_DC1_SLEEP
)
53 return REGULATOR_MODE_STANDBY
;
55 /* Datasheet: standby */
56 if (!(data
[0] & WM8400_DC1_ACTIVE
))
57 return REGULATOR_MODE_IDLE
;
59 /* Datasheet: active with or without force PWM */
60 if (data
[1] & WM8400_DC1_FRC_PWM
)
61 return REGULATOR_MODE_FAST
;
63 return REGULATOR_MODE_NORMAL
;
66 static int wm8400_dcdc_set_mode(struct regulator_dev
*dev
, unsigned int mode
)
68 struct wm8400
*wm8400
= rdev_get_drvdata(dev
);
69 int offset
= (rdev_get_id(dev
) - WM8400_DCDC1
) * 2;
73 case REGULATOR_MODE_FAST
:
74 /* Datasheet: active with force PWM */
75 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
76 WM8400_DC1_FRC_PWM
, WM8400_DC1_FRC_PWM
);
80 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
81 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
84 case REGULATOR_MODE_NORMAL
:
85 /* Datasheet: active */
86 ret
= wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_2
+ offset
,
87 WM8400_DC1_FRC_PWM
, 0);
91 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
92 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
,
95 case REGULATOR_MODE_IDLE
:
96 /* Datasheet: standby */
97 return wm8400_set_bits(wm8400
, WM8400_DCDC1_CONTROL_1
+ offset
,
98 WM8400_DC1_ACTIVE
| WM8400_DC1_SLEEP
, 0);
104 static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev
*dev
,
105 int input_uV
, int output_uV
,
108 return REGULATOR_MODE_NORMAL
;
111 static struct regulator_ops wm8400_dcdc_ops
= {
112 .is_enabled
= regulator_is_enabled_regmap
,
113 .enable
= regulator_enable_regmap
,
114 .disable
= regulator_disable_regmap
,
115 .list_voltage
= regulator_list_voltage_linear
,
116 .map_voltage
= regulator_map_voltage_linear
,
117 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
118 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
119 .get_mode
= wm8400_dcdc_get_mode
,
120 .set_mode
= wm8400_dcdc_set_mode
,
121 .get_optimum_mode
= wm8400_dcdc_get_optimum_mode
,
124 static struct regulator_desc regulators
[] = {
128 .ops
= &wm8400_ldo_ops
,
129 .enable_reg
= WM8400_LDO1_CONTROL
,
130 .enable_mask
= WM8400_LDO1_ENA
,
131 .n_voltages
= WM8400_LDO1_VSEL_MASK
+ 1,
132 .linear_ranges
= wm8400_ldo_ranges
,
133 .n_linear_ranges
= ARRAY_SIZE(wm8400_ldo_ranges
),
134 .vsel_reg
= WM8400_LDO1_CONTROL
,
135 .vsel_mask
= WM8400_LDO1_VSEL_MASK
,
136 .type
= REGULATOR_VOLTAGE
,
137 .owner
= THIS_MODULE
,
142 .ops
= &wm8400_ldo_ops
,
143 .enable_reg
= WM8400_LDO2_CONTROL
,
144 .enable_mask
= WM8400_LDO2_ENA
,
145 .n_voltages
= WM8400_LDO2_VSEL_MASK
+ 1,
146 .linear_ranges
= wm8400_ldo_ranges
,
147 .n_linear_ranges
= ARRAY_SIZE(wm8400_ldo_ranges
),
148 .type
= REGULATOR_VOLTAGE
,
149 .vsel_reg
= WM8400_LDO2_CONTROL
,
150 .vsel_mask
= WM8400_LDO2_VSEL_MASK
,
151 .owner
= THIS_MODULE
,
156 .ops
= &wm8400_ldo_ops
,
157 .enable_reg
= WM8400_LDO3_CONTROL
,
158 .enable_mask
= WM8400_LDO3_ENA
,
159 .n_voltages
= WM8400_LDO3_VSEL_MASK
+ 1,
160 .linear_ranges
= wm8400_ldo_ranges
,
161 .n_linear_ranges
= ARRAY_SIZE(wm8400_ldo_ranges
),
162 .vsel_reg
= WM8400_LDO3_CONTROL
,
163 .vsel_mask
= WM8400_LDO3_VSEL_MASK
,
164 .type
= REGULATOR_VOLTAGE
,
165 .owner
= THIS_MODULE
,
170 .ops
= &wm8400_ldo_ops
,
171 .enable_reg
= WM8400_LDO4_CONTROL
,
172 .enable_mask
= WM8400_LDO4_ENA
,
173 .n_voltages
= WM8400_LDO4_VSEL_MASK
+ 1,
174 .linear_ranges
= wm8400_ldo_ranges
,
175 .n_linear_ranges
= ARRAY_SIZE(wm8400_ldo_ranges
),
176 .vsel_reg
= WM8400_LDO4_CONTROL
,
177 .vsel_mask
= WM8400_LDO4_VSEL_MASK
,
178 .type
= REGULATOR_VOLTAGE
,
179 .owner
= THIS_MODULE
,
184 .ops
= &wm8400_dcdc_ops
,
185 .enable_reg
= WM8400_DCDC1_CONTROL_1
,
186 .enable_mask
= WM8400_DC1_ENA_MASK
,
187 .n_voltages
= WM8400_DC1_VSEL_MASK
+ 1,
188 .vsel_reg
= WM8400_DCDC1_CONTROL_1
,
189 .vsel_mask
= WM8400_DC1_VSEL_MASK
,
192 .type
= REGULATOR_VOLTAGE
,
193 .owner
= THIS_MODULE
,
198 .ops
= &wm8400_dcdc_ops
,
199 .enable_reg
= WM8400_DCDC2_CONTROL_1
,
200 .enable_mask
= WM8400_DC1_ENA_MASK
,
201 .n_voltages
= WM8400_DC2_VSEL_MASK
+ 1,
202 .vsel_reg
= WM8400_DCDC2_CONTROL_1
,
203 .vsel_mask
= WM8400_DC2_VSEL_MASK
,
206 .type
= REGULATOR_VOLTAGE
,
207 .owner
= THIS_MODULE
,
211 static int wm8400_regulator_probe(struct platform_device
*pdev
)
213 struct wm8400
*wm8400
= container_of(pdev
, struct wm8400
, regulators
[pdev
->id
]);
214 struct regulator_config config
= { };
215 struct regulator_dev
*rdev
;
217 config
.dev
= &pdev
->dev
;
218 config
.init_data
= dev_get_platdata(&pdev
->dev
);
219 config
.driver_data
= wm8400
;
220 config
.regmap
= wm8400
->regmap
;
222 rdev
= regulator_register(®ulators
[pdev
->id
], &config
);
224 return PTR_ERR(rdev
);
226 platform_set_drvdata(pdev
, rdev
);
231 static int wm8400_regulator_remove(struct platform_device
*pdev
)
233 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
235 regulator_unregister(rdev
);
240 static struct platform_driver wm8400_regulator_driver
= {
242 .name
= "wm8400-regulator",
244 .probe
= wm8400_regulator_probe
,
245 .remove
= wm8400_regulator_remove
,
249 * wm8400_register_regulator - enable software control of a WM8400 regulator
251 * This function enables software control of a WM8400 regulator via
252 * the regulator API. It is intended to be called from the
253 * platform_init() callback of the WM8400 MFD driver.
255 * @param dev The WM8400 device to operate on.
256 * @param reg The regulator to control.
257 * @param initdata Regulator initdata for the regulator.
259 int wm8400_register_regulator(struct device
*dev
, int reg
,
260 struct regulator_init_data
*initdata
)
262 struct wm8400
*wm8400
= dev_get_drvdata(dev
);
264 if (wm8400
->regulators
[reg
].name
)
267 initdata
->driver_data
= wm8400
;
269 wm8400
->regulators
[reg
].name
= "wm8400-regulator";
270 wm8400
->regulators
[reg
].id
= reg
;
271 wm8400
->regulators
[reg
].dev
.parent
= dev
;
272 wm8400
->regulators
[reg
].dev
.platform_data
= initdata
;
274 return platform_device_register(&wm8400
->regulators
[reg
]);
276 EXPORT_SYMBOL_GPL(wm8400_register_regulator
);
278 static int __init
wm8400_regulator_init(void)
280 return platform_driver_register(&wm8400_regulator_driver
);
282 subsys_initcall(wm8400_regulator_init
);
284 static void __exit
wm8400_regulator_exit(void)
286 platform_driver_unregister(&wm8400_regulator_driver
);
288 module_exit(wm8400_regulator_exit
);
290 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
291 MODULE_DESCRIPTION("WM8400 regulator driver");
292 MODULE_LICENSE("GPL");
293 MODULE_ALIAS("platform:wm8400-regulator");