regulator: Report actual configured voltage to set_voltage()
[linux-2.6.git] / drivers / regulator / wm8994-regulator.c
1 /*
2  * wm8994-regulator.c  --  Regulator driver for the WM8994
3  *
4  * Copyright 2009 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 modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23
24 #include <linux/mfd/wm8994/core.h>
25 #include <linux/mfd/wm8994/registers.h>
26 #include <linux/mfd/wm8994/pdata.h>
27
28 struct wm8994_ldo {
29         int enable;
30         bool is_enabled;
31         struct regulator_dev *regulator;
32         struct wm8994 *wm8994;
33 };
34
35 #define WM8994_LDO1_MAX_SELECTOR 0x7
36 #define WM8994_LDO2_MAX_SELECTOR 0x3
37
38 static int wm8994_ldo_enable(struct regulator_dev *rdev)
39 {
40         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
41
42         /* If we have no soft control assume that the LDO is always enabled. */
43         if (!ldo->enable)
44                 return 0;
45
46         gpio_set_value(ldo->enable, 1);
47         ldo->is_enabled = true;
48
49         return 0;
50 }
51
52 static int wm8994_ldo_disable(struct regulator_dev *rdev)
53 {
54         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
55
56         /* If we have no soft control assume that the LDO is always enabled. */
57         if (!ldo->enable)
58                 return -EINVAL;
59
60         gpio_set_value(ldo->enable, 0);
61         ldo->is_enabled = false;
62
63         return 0;
64 }
65
66 static int wm8994_ldo_is_enabled(struct regulator_dev *rdev)
67 {
68         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
69
70         return ldo->is_enabled;
71 }
72
73 static int wm8994_ldo_enable_time(struct regulator_dev *rdev)
74 {
75         /* 3ms is fairly conservative but this shouldn't be too performance
76          * critical; can be tweaked per-system if required. */
77         return 3000;
78 }
79
80 static int wm8994_ldo1_list_voltage(struct regulator_dev *rdev,
81                                     unsigned int selector)
82 {
83         if (selector > WM8994_LDO1_MAX_SELECTOR)
84                 return -EINVAL;
85
86         return (selector * 100000) + 2400000;
87 }
88
89 static int wm8994_ldo1_get_voltage(struct regulator_dev *rdev)
90 {
91         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
92         int val;
93
94         val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_1);
95         if (val < 0)
96                 return val;
97
98         val = (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
99
100         return wm8994_ldo1_list_voltage(rdev, val);
101 }
102
103 static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
104                                    int min_uV, int max_uV, unsigned *s)
105 {
106         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
107         int selector, v;
108
109         selector = (min_uV - 2400000) / 100000;
110         v = wm8994_ldo1_list_voltage(rdev, selector);
111         if (v < 0 || v > max_uV)
112                 return -EINVAL;
113
114         *s = selector;
115         selector <<= WM8994_LDO1_VSEL_SHIFT;
116
117         return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
118                                WM8994_LDO1_VSEL_MASK, selector);
119 }
120
121 static struct regulator_ops wm8994_ldo1_ops = {
122         .enable = wm8994_ldo_enable,
123         .disable = wm8994_ldo_disable,
124         .is_enabled = wm8994_ldo_is_enabled,
125         .enable_time = wm8994_ldo_enable_time,
126
127         .list_voltage = wm8994_ldo1_list_voltage,
128         .get_voltage = wm8994_ldo1_get_voltage,
129         .set_voltage = wm8994_ldo1_set_voltage,
130 };
131
132 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
133                                     unsigned int selector)
134 {
135         if (selector > WM8994_LDO2_MAX_SELECTOR)
136                 return -EINVAL;
137
138         return (selector * 100000) + 900000;
139 }
140
141 static int wm8994_ldo2_get_voltage(struct regulator_dev *rdev)
142 {
143         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
144         int val;
145
146         val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
147         if (val < 0)
148                 return val;
149
150         val = (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
151
152         return wm8994_ldo2_list_voltage(rdev, val);
153 }
154
155 static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
156                                    int min_uV, int max_uV, unsigned *s)
157 {
158         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
159         int selector, v;
160
161         selector = (min_uV - 900000) / 100000;
162         v = wm8994_ldo2_list_voltage(rdev, selector);
163         if (v < 0 || v > max_uV)
164                 return -EINVAL;
165
166         *s = selector;
167         selector <<= WM8994_LDO2_VSEL_SHIFT;
168
169         return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
170                                WM8994_LDO2_VSEL_MASK, selector);
171 }
172
173 static struct regulator_ops wm8994_ldo2_ops = {
174         .enable = wm8994_ldo_enable,
175         .disable = wm8994_ldo_disable,
176         .is_enabled = wm8994_ldo_is_enabled,
177         .enable_time = wm8994_ldo_enable_time,
178
179         .list_voltage = wm8994_ldo2_list_voltage,
180         .get_voltage = wm8994_ldo2_get_voltage,
181         .set_voltage = wm8994_ldo2_set_voltage,
182 };
183
184 static struct regulator_desc wm8994_ldo_desc[] = {
185         {
186                 .name = "LDO1",
187                 .id = 1,
188                 .type = REGULATOR_VOLTAGE,
189                 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
190                 .ops = &wm8994_ldo1_ops,
191                 .owner = THIS_MODULE,
192         },
193         {
194                 .name = "LDO2",
195                 .id = 2,
196                 .type = REGULATOR_VOLTAGE,
197                 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
198                 .ops = &wm8994_ldo2_ops,
199                 .owner = THIS_MODULE,
200         },
201 };
202
203 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
204 {
205         struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
206         struct wm8994_pdata *pdata = wm8994->dev->platform_data;
207         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
208         struct wm8994_ldo *ldo;
209         int ret;
210
211         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
212
213         if (!pdata)
214                 return -ENODEV;
215
216         ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
217         if (ldo == NULL) {
218                 dev_err(&pdev->dev, "Unable to allocate private data\n");
219                 return -ENOMEM;
220         }
221
222         ldo->wm8994 = wm8994;
223
224         if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
225                 ldo->enable = pdata->ldo[id].enable;
226
227                 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
228                 if (ret < 0) {
229                         dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
230                                 ret);
231                         goto err;
232                 }
233
234                 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
235                 if (ret < 0) {
236                         dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
237                                 ret);
238                         goto err_gpio;
239                 }
240         } else
241                 ldo->is_enabled = true;
242
243         ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
244                                              pdata->ldo[id].init_data, ldo);
245         if (IS_ERR(ldo->regulator)) {
246                 ret = PTR_ERR(ldo->regulator);
247                 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
248                         id + 1, ret);
249                 goto err_gpio;
250         }
251
252         platform_set_drvdata(pdev, ldo);
253
254         return 0;
255
256 err_gpio:
257         if (gpio_is_valid(ldo->enable))
258                 gpio_free(ldo->enable);
259 err:
260         kfree(ldo);
261         return ret;
262 }
263
264 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
265 {
266         struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
267
268         platform_set_drvdata(pdev, NULL);
269
270         regulator_unregister(ldo->regulator);
271         if (gpio_is_valid(ldo->enable))
272                 gpio_free(ldo->enable);
273         kfree(ldo);
274
275         return 0;
276 }
277
278 static struct platform_driver wm8994_ldo_driver = {
279         .probe = wm8994_ldo_probe,
280         .remove = __devexit_p(wm8994_ldo_remove),
281         .driver         = {
282                 .name   = "wm8994-ldo",
283                 .owner  = THIS_MODULE,
284         },
285 };
286
287 static int __init wm8994_ldo_init(void)
288 {
289         int ret;
290
291         ret = platform_driver_register(&wm8994_ldo_driver);
292         if (ret != 0)
293                 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
294
295         return ret;
296 }
297 subsys_initcall(wm8994_ldo_init);
298
299 static void __exit wm8994_ldo_exit(void)
300 {
301         platform_driver_unregister(&wm8994_ldo_driver);
302 }
303 module_exit(wm8994_ldo_exit);
304
305 /* Module information */
306 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
307 MODULE_DESCRIPTION("WM8994 LDO driver");
308 MODULE_LICENSE("GPL");
309 MODULE_ALIAS("platform:wm8994-ldo");