blob: 21fefef02a43a1bd89eb5eb0169c0b4f5eac30ef [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
89static 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
103static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
104 int min_uV, int max_uV)
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 selector <<= WM8994_LDO1_VSEL_SHIFT;
115
116 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
117 WM8994_LDO1_VSEL_MASK, selector);
118}
119
120static struct regulator_ops wm8994_ldo1_ops = {
121 .enable = wm8994_ldo_enable,
122 .disable = wm8994_ldo_disable,
123 .is_enabled = wm8994_ldo_is_enabled,
124 .enable_time = wm8994_ldo_enable_time,
125
126 .list_voltage = wm8994_ldo1_list_voltage,
127 .get_voltage = wm8994_ldo1_get_voltage,
128 .set_voltage = wm8994_ldo1_set_voltage,
129};
130
131static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
132 unsigned int selector)
133{
Mark Brown5a7743e2010-12-13 15:07:40 +0000134 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
135
Mark Brown69dc16c2010-01-25 19:41:57 +0000136 if (selector > WM8994_LDO2_MAX_SELECTOR)
137 return -EINVAL;
138
Mark Brown5a7743e2010-12-13 15:07:40 +0000139 switch (ldo->wm8994->type) {
140 case WM8994:
141 return (selector * 100000) + 900000;
142 case WM8958:
143 return (selector * 100000) + 1000000;
144 default:
145 return -EINVAL;
146 }
Mark Brown69dc16c2010-01-25 19:41:57 +0000147}
148
149static int wm8994_ldo2_get_voltage(struct regulator_dev *rdev)
150{
151 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
152 int val;
153
154 val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
155 if (val < 0)
156 return val;
157
158 val = (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
159
160 return wm8994_ldo2_list_voltage(rdev, val);
161}
162
163static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
164 int min_uV, int max_uV)
165{
166 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
167 int selector, v;
168
Mark Brown5a7743e2010-12-13 15:07:40 +0000169 switch (ldo->wm8994->type) {
170 case WM8994:
171 selector = (min_uV - 900000) / 100000;
172 break;
173 case WM8958:
174 selector = (min_uV - 1000000) / 100000;
175 break;
176 default:
177 return -EINVAL;
178 }
179
Mark Brown69dc16c2010-01-25 19:41:57 +0000180 v = wm8994_ldo2_list_voltage(rdev, selector);
181 if (v < 0 || v > max_uV)
182 return -EINVAL;
183
184 selector <<= WM8994_LDO2_VSEL_SHIFT;
185
186 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
187 WM8994_LDO2_VSEL_MASK, selector);
188}
189
190static struct regulator_ops wm8994_ldo2_ops = {
191 .enable = wm8994_ldo_enable,
192 .disable = wm8994_ldo_disable,
193 .is_enabled = wm8994_ldo_is_enabled,
194 .enable_time = wm8994_ldo_enable_time,
195
196 .list_voltage = wm8994_ldo2_list_voltage,
197 .get_voltage = wm8994_ldo2_get_voltage,
198 .set_voltage = wm8994_ldo2_set_voltage,
199};
200
201static struct regulator_desc wm8994_ldo_desc[] = {
202 {
203 .name = "LDO1",
204 .id = 1,
205 .type = REGULATOR_VOLTAGE,
206 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
207 .ops = &wm8994_ldo1_ops,
208 .owner = THIS_MODULE,
209 },
210 {
211 .name = "LDO2",
212 .id = 2,
213 .type = REGULATOR_VOLTAGE,
214 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
215 .ops = &wm8994_ldo2_ops,
216 .owner = THIS_MODULE,
217 },
218};
219
220static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
221{
222 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
223 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
224 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
225 struct wm8994_ldo *ldo;
226 int ret;
227
228 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
229
230 if (!pdata)
231 return -ENODEV;
232
233 ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
234 if (ldo == NULL) {
235 dev_err(&pdev->dev, "Unable to allocate private data\n");
236 return -ENOMEM;
237 }
238
239 ldo->wm8994 = wm8994;
240
Mark Brown69dc16c2010-01-25 19:41:57 +0000241 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
242 ldo->enable = pdata->ldo[id].enable;
243
244 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
245 if (ret < 0) {
246 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
247 ret);
248 goto err;
249 }
250
251 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
252 if (ret < 0) {
253 dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
254 ret);
255 goto err_gpio;
256 }
Joonyoung Shimc4604e42010-08-06 15:48:24 +0100257 } else
258 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +0000259
260 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
261 pdata->ldo[id].init_data, ldo);
262 if (IS_ERR(ldo->regulator)) {
263 ret = PTR_ERR(ldo->regulator);
264 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
265 id + 1, ret);
266 goto err_gpio;
267 }
268
269 platform_set_drvdata(pdev, ldo);
270
271 return 0;
272
273err_gpio:
274 if (gpio_is_valid(ldo->enable))
275 gpio_free(ldo->enable);
276err:
277 kfree(ldo);
278 return ret;
279}
280
281static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
282{
283 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
284
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800285 platform_set_drvdata(pdev, NULL);
286
Mark Brown69dc16c2010-01-25 19:41:57 +0000287 regulator_unregister(ldo->regulator);
288 if (gpio_is_valid(ldo->enable))
289 gpio_free(ldo->enable);
290 kfree(ldo);
291
292 return 0;
293}
294
295static struct platform_driver wm8994_ldo_driver = {
296 .probe = wm8994_ldo_probe,
297 .remove = __devexit_p(wm8994_ldo_remove),
298 .driver = {
299 .name = "wm8994-ldo",
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800300 .owner = THIS_MODULE,
Mark Brown69dc16c2010-01-25 19:41:57 +0000301 },
302};
303
304static int __init wm8994_ldo_init(void)
305{
306 int ret;
307
308 ret = platform_driver_register(&wm8994_ldo_driver);
309 if (ret != 0)
310 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
311
312 return ret;
313}
314subsys_initcall(wm8994_ldo_init);
315
316static void __exit wm8994_ldo_exit(void)
317{
318 platform_driver_unregister(&wm8994_ldo_driver);
319}
320module_exit(wm8994_ldo_exit);
321
322/* Module information */
323MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
324MODULE_DESCRIPTION("WM8994 LDO driver");
325MODULE_LICENSE("GPL");
326MODULE_ALIAS("platform:wm8994-ldo");