]>
Commit | Line | Data |
---|---|---|
42fad570 MB |
1 | /* |
2 | * Regulator support for WM8400 | |
3 | * | |
4 | * Copyright 2008 Wolfson Microelectronics PLC. | |
5 | * | |
6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
7 | * | |
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. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/bug.h> | |
16 | #include <linux/err.h> | |
17 | #include <linux/kernel.h> | |
65602c32 | 18 | #include <linux/module.h> |
42fad570 MB |
19 | #include <linux/regulator/driver.h> |
20 | #include <linux/mfd/wm8400-private.h> | |
21 | ||
216765d9 MB |
22 | static int wm8400_ldo_list_voltage(struct regulator_dev *dev, |
23 | unsigned selector) | |
24 | { | |
25 | if (selector > WM8400_LDO1_VSEL_MASK) | |
26 | return -EINVAL; | |
27 | ||
28 | if (selector < 15) | |
29 | return 900000 + (selector * 50000); | |
30 | else | |
74e20e56 | 31 | return 1700000 + ((selector - 15) * 100000); |
216765d9 MB |
32 | } |
33 | ||
c54a155d MB |
34 | static int wm8400_ldo_map_voltage(struct regulator_dev *dev, |
35 | int min_uV, int max_uV) | |
42fad570 | 36 | { |
42fad570 | 37 | u16 val; |
38c20eb2 | 38 | int volt; |
42fad570 MB |
39 | |
40 | if (min_uV < 900000 || min_uV > 3300000) | |
41 | return -EINVAL; | |
42 | ||
38c20eb2 | 43 | if (min_uV < 1700000) /* Steps of 50mV from 900mV; */ |
e9a15c8c | 44 | val = DIV_ROUND_UP(min_uV - 900000, 50000); |
38c20eb2 AL |
45 | else /* Steps of 100mV from 1700mV */ |
46 | val = DIV_ROUND_UP(min_uV - 1700000, 100000) + 15; | |
42fad570 | 47 | |
38c20eb2 AL |
48 | volt = wm8400_ldo_list_voltage(dev, val); |
49 | if (volt < min_uV || volt > max_uV) | |
50 | return -EINVAL; | |
42fad570 | 51 | |
c54a155d | 52 | return val; |
42fad570 MB |
53 | } |
54 | ||
55 | static struct regulator_ops wm8400_ldo_ops = { | |
c54a155d MB |
56 | .is_enabled = regulator_is_enabled_regmap, |
57 | .enable = regulator_enable_regmap, | |
58 | .disable = regulator_disable_regmap, | |
216765d9 | 59 | .list_voltage = wm8400_ldo_list_voltage, |
c54a155d MB |
60 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
61 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
62 | .map_voltage = wm8400_ldo_map_voltage, | |
42fad570 MB |
63 | }; |
64 | ||
42fad570 MB |
65 | static unsigned int wm8400_dcdc_get_mode(struct regulator_dev *dev) |
66 | { | |
67 | struct wm8400 *wm8400 = rdev_get_drvdata(dev); | |
68 | int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; | |
69 | u16 data[2]; | |
70 | int ret; | |
71 | ||
72 | ret = wm8400_block_read(wm8400, WM8400_DCDC1_CONTROL_1 + offset, 2, | |
73 | data); | |
74 | if (ret != 0) | |
75 | return 0; | |
76 | ||
77 | /* Datasheet: hibernate */ | |
78 | if (data[0] & WM8400_DC1_SLEEP) | |
79 | return REGULATOR_MODE_STANDBY; | |
80 | ||
81 | /* Datasheet: standby */ | |
82 | if (!(data[0] & WM8400_DC1_ACTIVE)) | |
83 | return REGULATOR_MODE_IDLE; | |
84 | ||
85 | /* Datasheet: active with or without force PWM */ | |
86 | if (data[1] & WM8400_DC1_FRC_PWM) | |
87 | return REGULATOR_MODE_FAST; | |
88 | else | |
89 | return REGULATOR_MODE_NORMAL; | |
90 | } | |
91 | ||
92 | static int wm8400_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode) | |
93 | { | |
94 | struct wm8400 *wm8400 = rdev_get_drvdata(dev); | |
95 | int offset = (rdev_get_id(dev) - WM8400_DCDC1) * 2; | |
96 | int ret; | |
97 | ||
98 | switch (mode) { | |
99 | case REGULATOR_MODE_FAST: | |
100 | /* Datasheet: active with force PWM */ | |
101 | ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, | |
102 | WM8400_DC1_FRC_PWM, WM8400_DC1_FRC_PWM); | |
103 | if (ret != 0) | |
104 | return ret; | |
105 | ||
106 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, | |
107 | WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, | |
108 | WM8400_DC1_ACTIVE); | |
109 | ||
110 | case REGULATOR_MODE_NORMAL: | |
111 | /* Datasheet: active */ | |
112 | ret = wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_2 + offset, | |
113 | WM8400_DC1_FRC_PWM, 0); | |
114 | if (ret != 0) | |
115 | return ret; | |
116 | ||
117 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, | |
118 | WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, | |
119 | WM8400_DC1_ACTIVE); | |
120 | ||
121 | case REGULATOR_MODE_IDLE: | |
122 | /* Datasheet: standby */ | |
42fad570 | 123 | return wm8400_set_bits(wm8400, WM8400_DCDC1_CONTROL_1 + offset, |
4001376e | 124 | WM8400_DC1_ACTIVE | WM8400_DC1_SLEEP, 0); |
42fad570 MB |
125 | default: |
126 | return -EINVAL; | |
127 | } | |
128 | } | |
129 | ||
130 | static unsigned int wm8400_dcdc_get_optimum_mode(struct regulator_dev *dev, | |
131 | int input_uV, int output_uV, | |
132 | int load_uA) | |
133 | { | |
134 | return REGULATOR_MODE_NORMAL; | |
135 | } | |
136 | ||
137 | static struct regulator_ops wm8400_dcdc_ops = { | |
c54a155d MB |
138 | .is_enabled = regulator_is_enabled_regmap, |
139 | .enable = regulator_enable_regmap, | |
140 | .disable = regulator_disable_regmap, | |
141 | .list_voltage = regulator_list_voltage_linear, | |
27eeabb7 | 142 | .map_voltage = regulator_map_voltage_linear, |
c54a155d MB |
143 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
144 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
42fad570 MB |
145 | .get_mode = wm8400_dcdc_get_mode, |
146 | .set_mode = wm8400_dcdc_set_mode, | |
147 | .get_optimum_mode = wm8400_dcdc_get_optimum_mode, | |
148 | }; | |
149 | ||
150 | static struct regulator_desc regulators[] = { | |
151 | { | |
152 | .name = "LDO1", | |
153 | .id = WM8400_LDO1, | |
154 | .ops = &wm8400_ldo_ops, | |
c54a155d MB |
155 | .enable_reg = WM8400_LDO1_CONTROL, |
156 | .enable_mask = WM8400_LDO1_ENA, | |
216765d9 | 157 | .n_voltages = WM8400_LDO1_VSEL_MASK + 1, |
c54a155d MB |
158 | .vsel_reg = WM8400_LDO1_CONTROL, |
159 | .vsel_mask = WM8400_LDO1_VSEL_MASK, | |
42fad570 MB |
160 | .type = REGULATOR_VOLTAGE, |
161 | .owner = THIS_MODULE, | |
162 | }, | |
163 | { | |
164 | .name = "LDO2", | |
165 | .id = WM8400_LDO2, | |
166 | .ops = &wm8400_ldo_ops, | |
c54a155d MB |
167 | .enable_reg = WM8400_LDO2_CONTROL, |
168 | .enable_mask = WM8400_LDO2_ENA, | |
216765d9 | 169 | .n_voltages = WM8400_LDO2_VSEL_MASK + 1, |
42fad570 | 170 | .type = REGULATOR_VOLTAGE, |
c54a155d MB |
171 | .vsel_reg = WM8400_LDO2_CONTROL, |
172 | .vsel_mask = WM8400_LDO2_VSEL_MASK, | |
42fad570 MB |
173 | .owner = THIS_MODULE, |
174 | }, | |
175 | { | |
176 | .name = "LDO3", | |
177 | .id = WM8400_LDO3, | |
178 | .ops = &wm8400_ldo_ops, | |
c54a155d MB |
179 | .enable_reg = WM8400_LDO3_CONTROL, |
180 | .enable_mask = WM8400_LDO3_ENA, | |
216765d9 | 181 | .n_voltages = WM8400_LDO3_VSEL_MASK + 1, |
c54a155d MB |
182 | .vsel_reg = WM8400_LDO3_CONTROL, |
183 | .vsel_mask = WM8400_LDO3_VSEL_MASK, | |
42fad570 MB |
184 | .type = REGULATOR_VOLTAGE, |
185 | .owner = THIS_MODULE, | |
186 | }, | |
187 | { | |
188 | .name = "LDO4", | |
189 | .id = WM8400_LDO4, | |
190 | .ops = &wm8400_ldo_ops, | |
c54a155d MB |
191 | .enable_reg = WM8400_LDO4_CONTROL, |
192 | .enable_mask = WM8400_LDO4_ENA, | |
216765d9 | 193 | .n_voltages = WM8400_LDO4_VSEL_MASK + 1, |
c54a155d MB |
194 | .vsel_reg = WM8400_LDO4_CONTROL, |
195 | .vsel_mask = WM8400_LDO4_VSEL_MASK, | |
42fad570 MB |
196 | .type = REGULATOR_VOLTAGE, |
197 | .owner = THIS_MODULE, | |
198 | }, | |
199 | { | |
200 | .name = "DCDC1", | |
201 | .id = WM8400_DCDC1, | |
202 | .ops = &wm8400_dcdc_ops, | |
c54a155d MB |
203 | .enable_reg = WM8400_DCDC1_CONTROL_1, |
204 | .enable_mask = WM8400_DC1_ENA_MASK, | |
216765d9 | 205 | .n_voltages = WM8400_DC1_VSEL_MASK + 1, |
c54a155d MB |
206 | .vsel_reg = WM8400_DCDC1_CONTROL_1, |
207 | .vsel_mask = WM8400_DC1_VSEL_MASK, | |
208 | .min_uV = 850000, | |
209 | .uV_step = 25000, | |
42fad570 MB |
210 | .type = REGULATOR_VOLTAGE, |
211 | .owner = THIS_MODULE, | |
212 | }, | |
213 | { | |
214 | .name = "DCDC2", | |
215 | .id = WM8400_DCDC2, | |
216 | .ops = &wm8400_dcdc_ops, | |
c54a155d MB |
217 | .enable_reg = WM8400_DCDC2_CONTROL_1, |
218 | .enable_mask = WM8400_DC1_ENA_MASK, | |
216765d9 | 219 | .n_voltages = WM8400_DC2_VSEL_MASK + 1, |
c54a155d MB |
220 | .vsel_reg = WM8400_DCDC2_CONTROL_1, |
221 | .vsel_mask = WM8400_DC2_VSEL_MASK, | |
222 | .min_uV = 850000, | |
223 | .uV_step = 25000, | |
42fad570 MB |
224 | .type = REGULATOR_VOLTAGE, |
225 | .owner = THIS_MODULE, | |
226 | }, | |
227 | }; | |
228 | ||
a5023574 | 229 | static int wm8400_regulator_probe(struct platform_device *pdev) |
42fad570 | 230 | { |
1ad02bbc | 231 | struct wm8400 *wm8400 = container_of(pdev, struct wm8400, regulators[pdev->id]); |
c172708d | 232 | struct regulator_config config = { }; |
42fad570 MB |
233 | struct regulator_dev *rdev; |
234 | ||
c172708d MB |
235 | config.dev = &pdev->dev; |
236 | config.init_data = pdev->dev.platform_data; | |
237 | config.driver_data = wm8400; | |
c54a155d | 238 | config.regmap = wm8400->regmap; |
42fad570 | 239 | |
c172708d | 240 | rdev = regulator_register(®ulators[pdev->id], &config); |
42fad570 MB |
241 | if (IS_ERR(rdev)) |
242 | return PTR_ERR(rdev); | |
243 | ||
1ad02bbc DT |
244 | platform_set_drvdata(pdev, rdev); |
245 | ||
42fad570 MB |
246 | return 0; |
247 | } | |
248 | ||
8dc995f5 | 249 | static int wm8400_regulator_remove(struct platform_device *pdev) |
42fad570 MB |
250 | { |
251 | struct regulator_dev *rdev = platform_get_drvdata(pdev); | |
252 | ||
253 | regulator_unregister(rdev); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | static struct platform_driver wm8400_regulator_driver = { | |
259 | .driver = { | |
260 | .name = "wm8400-regulator", | |
261 | }, | |
262 | .probe = wm8400_regulator_probe, | |
5eb9f2b9 | 263 | .remove = wm8400_regulator_remove, |
42fad570 MB |
264 | }; |
265 | ||
266 | /** | |
267 | * wm8400_register_regulator - enable software control of a WM8400 regulator | |
268 | * | |
269 | * This function enables software control of a WM8400 regulator via | |
270 | * the regulator API. It is intended to be called from the | |
271 | * platform_init() callback of the WM8400 MFD driver. | |
272 | * | |
273 | * @param dev The WM8400 device to operate on. | |
274 | * @param reg The regulator to control. | |
275 | * @param initdata Regulator initdata for the regulator. | |
276 | */ | |
277 | int wm8400_register_regulator(struct device *dev, int reg, | |
278 | struct regulator_init_data *initdata) | |
279 | { | |
1909e2f6 | 280 | struct wm8400 *wm8400 = dev_get_drvdata(dev); |
42fad570 MB |
281 | |
282 | if (wm8400->regulators[reg].name) | |
283 | return -EBUSY; | |
284 | ||
285 | initdata->driver_data = wm8400; | |
286 | ||
287 | wm8400->regulators[reg].name = "wm8400-regulator"; | |
288 | wm8400->regulators[reg].id = reg; | |
289 | wm8400->regulators[reg].dev.parent = dev; | |
42fad570 MB |
290 | wm8400->regulators[reg].dev.platform_data = initdata; |
291 | ||
292 | return platform_device_register(&wm8400->regulators[reg]); | |
293 | } | |
294 | EXPORT_SYMBOL_GPL(wm8400_register_regulator); | |
295 | ||
296 | static int __init wm8400_regulator_init(void) | |
297 | { | |
298 | return platform_driver_register(&wm8400_regulator_driver); | |
299 | } | |
5a1b22be | 300 | subsys_initcall(wm8400_regulator_init); |
42fad570 MB |
301 | |
302 | static void __exit wm8400_regulator_exit(void) | |
303 | { | |
304 | platform_driver_unregister(&wm8400_regulator_driver); | |
305 | } | |
306 | module_exit(wm8400_regulator_exit); | |
307 | ||
308 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | |
309 | MODULE_DESCRIPTION("WM8400 regulator driver"); | |
310 | MODULE_LICENSE("GPL"); | |
311 | MODULE_ALIAS("platform:wm8400-regulator"); |