regulator: Convert WM8994 to use get_voltage_sel()
[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_sel(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         return (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
99 }
100
101 static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
102                                    int min_uV, int max_uV, unsigned *s)
103 {
104         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
105         int selector, v;
106
107         selector = (min_uV - 2400000) / 100000;
108         v = wm8994_ldo1_list_voltage(rdev, selector);
109         if (v < 0 || v > max_uV)
110                 return -EINVAL;
111
112         *s = selector;
113         selector <<= WM8994_LDO1_VSEL_SHIFT;
114
115         return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
116                                WM8994_LDO1_VSEL_MASK, selector);
117 }
118
119 static struct regulator_ops wm8994_ldo1_ops = {
120         .enable = wm8994_ldo_enable,
121         .disable = wm8994_ldo_disable,
122         .is_enabled = wm8994_ldo_is_enabled,
123         .enable_time = wm8994_ldo_enable_time,
124
125         .list_voltage = wm8994_ldo1_list_voltage,
126         .get_voltage_sel = wm8994_ldo1_get_voltage_sel,
127         .set_voltage = wm8994_ldo1_set_voltage,
128 };
129
130 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
131                                     unsigned int selector)
132 {
133         if (selector > WM8994_LDO2_MAX_SELECTOR)
134                 return -EINVAL;
135
136         return (selector * 100000) + 900000;
137 }
138
139 static int wm8994_ldo2_get_voltage_sel(struct regulator_dev *rdev)
140 {
141         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
142         int val;
143
144         val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
145         if (val < 0)
146                 return val;
147
148         return (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
149 }
150
151 static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
152                                    int min_uV, int max_uV, unsigned *s)
153 {
154         struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
155         int selector, v;
156
157         selector = (min_uV - 900000) / 100000;
158         v = wm8994_ldo2_list_voltage(rdev, selector);
159         if (v < 0 || v > max_uV)
160                 return -EINVAL;
161
162         *s = selector;
163         selector <<= WM8994_LDO2_VSEL_SHIFT;
164
165         return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
166                                WM8994_LDO2_VSEL_MASK, selector);
167 }
168
169 static struct regulator_ops wm8994_ldo2_ops = {
170         .enable = wm8994_ldo_enable,
171         .disable = wm8994_ldo_disable,
172         .is_enabled = wm8994_ldo_is_enabled,
173         .enable_time = wm8994_ldo_enable_time,
174
175         .list_voltage = wm8994_ldo2_list_voltage,
176         .get_voltage_sel = wm8994_ldo2_get_voltage_sel,
177         .set_voltage = wm8994_ldo2_set_voltage,
178 };
179
180 static struct regulator_desc wm8994_ldo_desc[] = {
181         {
182                 .name = "LDO1",
183                 .id = 1,
184                 .type = REGULATOR_VOLTAGE,
185                 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
186                 .ops = &wm8994_ldo1_ops,
187                 .owner = THIS_MODULE,
188         },
189         {
190                 .name = "LDO2",
191                 .id = 2,
192                 .type = REGULATOR_VOLTAGE,
193                 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
194                 .ops = &wm8994_ldo2_ops,
195                 .owner = THIS_MODULE,
196         },
197 };
198
199 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
200 {
201         struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
202         struct wm8994_pdata *pdata = wm8994->dev->platform_data;
203         int id = pdev->id % ARRAY_SIZE(pdata->ldo);
204         struct wm8994_ldo *ldo;
205         int ret;
206
207         dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
208
209         if (!pdata)
210                 return -ENODEV;
211
212         ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
213         if (ldo == NULL) {
214                 dev_err(&pdev->dev, "Unable to allocate private data\n");
215                 return -ENOMEM;
216         }
217
218         ldo->wm8994 = wm8994;
219
220         if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
221                 ldo->enable = pdata->ldo[id].enable;
222
223                 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
224                 if (ret < 0) {
225                         dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
226                                 ret);
227                         goto err;
228                 }
229
230                 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
231                 if (ret < 0) {
232                         dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
233                                 ret);
234                         goto err_gpio;
235                 }
236         } else
237                 ldo->is_enabled = true;
238
239         ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
240                                              pdata->ldo[id].init_data, ldo);
241         if (IS_ERR(ldo->regulator)) {
242                 ret = PTR_ERR(ldo->regulator);
243                 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
244                         id + 1, ret);
245                 goto err_gpio;
246         }
247
248         platform_set_drvdata(pdev, ldo);
249
250         return 0;
251
252 err_gpio:
253         if (gpio_is_valid(ldo->enable))
254                 gpio_free(ldo->enable);
255 err:
256         kfree(ldo);
257         return ret;
258 }
259
260 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
261 {
262         struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
263
264         platform_set_drvdata(pdev, NULL);
265
266         regulator_unregister(ldo->regulator);
267         if (gpio_is_valid(ldo->enable))
268                 gpio_free(ldo->enable);
269         kfree(ldo);
270
271         return 0;
272 }
273
274 static struct platform_driver wm8994_ldo_driver = {
275         .probe = wm8994_ldo_probe,
276         .remove = __devexit_p(wm8994_ldo_remove),
277         .driver         = {
278                 .name   = "wm8994-ldo",
279                 .owner  = THIS_MODULE,
280         },
281 };
282
283 static int __init wm8994_ldo_init(void)
284 {
285         int ret;
286
287         ret = platform_driver_register(&wm8994_ldo_driver);
288         if (ret != 0)
289                 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
290
291         return ret;
292 }
293 subsys_initcall(wm8994_ldo_init);
294
295 static void __exit wm8994_ldo_exit(void)
296 {
297         platform_driver_unregister(&wm8994_ldo_driver);
298 }
299 module_exit(wm8994_ldo_exit);
300
301 /* Module information */
302 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
303 MODULE_DESCRIPTION("WM8994 LDO driver");
304 MODULE_LICENSE("GPL");
305 MODULE_ALIAS("platform:wm8994-ldo");