blob: 35b2958d51064a84950deef280a14603b1e4e0da [file] [log] [blame]
Mark Brown69dc16c2010-01-25 19:41:57 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brown69dc16c2010-01-25 19:41:57 +000023
24#include <linux/mfd/wm8994/core.h>
25#include <linux/mfd/wm8994/registers.h>
26#include <linux/mfd/wm8994/pdata.h>
27
28struct wm8994_ldo {
29 int enable;
Dmitry Torokhov598b3572010-02-23 23:38:44 -080030 bool is_enabled;
Mark Brown69dc16c2010-01-25 19:41:57 +000031 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
38static 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);
Dmitry Torokhov598b3572010-02-23 23:38:44 -080047 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +000048
49 return 0;
50}
51
52static 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);
Dmitry Torokhov598b3572010-02-23 23:38:44 -080061 ldo->is_enabled = false;
Mark Brown69dc16c2010-01-25 19:41:57 +000062
63 return 0;
64}
65
66static 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
73static 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
80static 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
Mark Brownd9f0f282010-12-12 14:07:06 +000089static int wm8994_ldo1_get_voltage_sel(struct regulator_dev *rdev)
Mark Brown69dc16c2010-01-25 19:41:57 +000090{
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
Mark Brownd9f0f282010-12-12 14:07:06 +000098 return (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
Mark Brown69dc16c2010-01-25 19:41:57 +000099}
100
101static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000102 int min_uV, int max_uV, unsigned *s)
Mark Brown69dc16c2010-01-25 19:41:57 +0000103{
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
Mark Brown3a93f2a2010-11-10 14:38:29 +0000112 *s = selector;
Mark Brown69dc16c2010-01-25 19:41:57 +0000113 selector <<= WM8994_LDO1_VSEL_SHIFT;
114
115 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
116 WM8994_LDO1_VSEL_MASK, selector);
117}
118
119static 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,
Mark Brownd9f0f282010-12-12 14:07:06 +0000126 .get_voltage_sel = wm8994_ldo1_get_voltage_sel,
Mark Brown69dc16c2010-01-25 19:41:57 +0000127 .set_voltage = wm8994_ldo1_set_voltage,
128};
129
130static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
131 unsigned int selector)
132{
Mark Brown5a7743e2010-12-13 15:07:40 +0000133 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
134
Mark Brown69dc16c2010-01-25 19:41:57 +0000135 if (selector > WM8994_LDO2_MAX_SELECTOR)
136 return -EINVAL;
137
Mark Brown5a7743e2010-12-13 15:07:40 +0000138 switch (ldo->wm8994->type) {
139 case WM8994:
140 return (selector * 100000) + 900000;
141 case WM8958:
142 return (selector * 100000) + 1000000;
143 default:
144 return -EINVAL;
145 }
Mark Brown69dc16c2010-01-25 19:41:57 +0000146}
147
Mark Brownd9f0f282010-12-12 14:07:06 +0000148static int wm8994_ldo2_get_voltage_sel(struct regulator_dev *rdev)
Mark Brown69dc16c2010-01-25 19:41:57 +0000149{
150 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
151 int val;
152
153 val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
154 if (val < 0)
155 return val;
156
Mark Brownd9f0f282010-12-12 14:07:06 +0000157 return (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
Mark Brown69dc16c2010-01-25 19:41:57 +0000158}
159
160static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000161 int min_uV, int max_uV, unsigned *s)
Mark Brown69dc16c2010-01-25 19:41:57 +0000162{
163 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
164 int selector, v;
165
Mark Brown5a7743e2010-12-13 15:07:40 +0000166 switch (ldo->wm8994->type) {
167 case WM8994:
168 selector = (min_uV - 900000) / 100000;
169 break;
170 case WM8958:
171 selector = (min_uV - 1000000) / 100000;
172 break;
173 default:
174 return -EINVAL;
175 }
176
Mark Brown69dc16c2010-01-25 19:41:57 +0000177 v = wm8994_ldo2_list_voltage(rdev, selector);
178 if (v < 0 || v > max_uV)
179 return -EINVAL;
180
Mark Brown3a93f2a2010-11-10 14:38:29 +0000181 *s = selector;
Mark Brown69dc16c2010-01-25 19:41:57 +0000182 selector <<= WM8994_LDO2_VSEL_SHIFT;
183
184 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
185 WM8994_LDO2_VSEL_MASK, selector);
186}
187
188static struct regulator_ops wm8994_ldo2_ops = {
189 .enable = wm8994_ldo_enable,
190 .disable = wm8994_ldo_disable,
191 .is_enabled = wm8994_ldo_is_enabled,
192 .enable_time = wm8994_ldo_enable_time,
193
194 .list_voltage = wm8994_ldo2_list_voltage,
Mark Brownd9f0f282010-12-12 14:07:06 +0000195 .get_voltage_sel = wm8994_ldo2_get_voltage_sel,
Mark Brown69dc16c2010-01-25 19:41:57 +0000196 .set_voltage = wm8994_ldo2_set_voltage,
197};
198
199static struct regulator_desc wm8994_ldo_desc[] = {
200 {
201 .name = "LDO1",
202 .id = 1,
203 .type = REGULATOR_VOLTAGE,
204 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
205 .ops = &wm8994_ldo1_ops,
206 .owner = THIS_MODULE,
207 },
208 {
209 .name = "LDO2",
210 .id = 2,
211 .type = REGULATOR_VOLTAGE,
212 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
213 .ops = &wm8994_ldo2_ops,
214 .owner = THIS_MODULE,
215 },
216};
217
218static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
219{
220 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
221 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
222 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
223 struct wm8994_ldo *ldo;
224 int ret;
225
226 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
227
228 if (!pdata)
229 return -ENODEV;
230
231 ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
232 if (ldo == NULL) {
233 dev_err(&pdev->dev, "Unable to allocate private data\n");
234 return -ENOMEM;
235 }
236
237 ldo->wm8994 = wm8994;
238
Mark Brown69dc16c2010-01-25 19:41:57 +0000239 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
240 ldo->enable = pdata->ldo[id].enable;
241
242 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
243 if (ret < 0) {
244 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
245 ret);
246 goto err;
247 }
248
249 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
250 if (ret < 0) {
251 dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
252 ret);
253 goto err_gpio;
254 }
Joonyoung Shimc4604e42010-08-06 15:48:24 +0100255 } else
256 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +0000257
258 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
259 pdata->ldo[id].init_data, ldo);
260 if (IS_ERR(ldo->regulator)) {
261 ret = PTR_ERR(ldo->regulator);
262 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
263 id + 1, ret);
264 goto err_gpio;
265 }
266
267 platform_set_drvdata(pdev, ldo);
268
269 return 0;
270
271err_gpio:
272 if (gpio_is_valid(ldo->enable))
273 gpio_free(ldo->enable);
274err:
275 kfree(ldo);
276 return ret;
277}
278
279static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
280{
281 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
282
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800283 platform_set_drvdata(pdev, NULL);
284
Mark Brown69dc16c2010-01-25 19:41:57 +0000285 regulator_unregister(ldo->regulator);
286 if (gpio_is_valid(ldo->enable))
287 gpio_free(ldo->enable);
288 kfree(ldo);
289
290 return 0;
291}
292
293static struct platform_driver wm8994_ldo_driver = {
294 .probe = wm8994_ldo_probe,
295 .remove = __devexit_p(wm8994_ldo_remove),
296 .driver = {
297 .name = "wm8994-ldo",
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800298 .owner = THIS_MODULE,
Mark Brown69dc16c2010-01-25 19:41:57 +0000299 },
300};
301
302static int __init wm8994_ldo_init(void)
303{
304 int ret;
305
306 ret = platform_driver_register(&wm8994_ldo_driver);
307 if (ret != 0)
308 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
309
310 return ret;
311}
312subsys_initcall(wm8994_ldo_init);
313
314static void __exit wm8994_ldo_exit(void)
315{
316 platform_driver_unregister(&wm8994_ldo_driver);
317}
318module_exit(wm8994_ldo_exit);
319
320/* Module information */
321MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
322MODULE_DESCRIPTION("WM8994 LDO driver");
323MODULE_LICENSE("GPL");
324MODULE_ALIAS("platform:wm8994-ldo");