blob: f2912f1528ccc1ef7477f1b7fca55e39d48e8b72 [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
Mark Brown2ae36362011-05-24 23:14:40 +080046 gpio_set_value_cansleep(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
Mark Brown2ae36362011-05-24 23:14:40 +080060 gpio_set_value_cansleep(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
Mark Brown9f29c9e2012-04-15 11:29:29 +0100101static int wm8994_ldo1_set_voltage_sel(struct regulator_dev *rdev,
102 unsigned selector)
Mark Brown69dc16c2010-01-25 19:41:57 +0000103{
104 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
Mark Brown69dc16c2010-01-25 19:41:57 +0000105
Mark Brown69dc16c2010-01-25 19:41:57 +0000106 selector <<= WM8994_LDO1_VSEL_SHIFT;
107
108 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
109 WM8994_LDO1_VSEL_MASK, selector);
110}
111
112static struct regulator_ops wm8994_ldo1_ops = {
113 .enable = wm8994_ldo_enable,
114 .disable = wm8994_ldo_disable,
115 .is_enabled = wm8994_ldo_is_enabled,
116 .enable_time = wm8994_ldo_enable_time,
117
118 .list_voltage = wm8994_ldo1_list_voltage,
Mark Brownd9f0f282010-12-12 14:07:06 +0000119 .get_voltage_sel = wm8994_ldo1_get_voltage_sel,
Mark Brown9f29c9e2012-04-15 11:29:29 +0100120 .set_voltage_sel = wm8994_ldo1_set_voltage_sel,
Mark Brown69dc16c2010-01-25 19:41:57 +0000121};
122
123static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
124 unsigned int selector)
125{
Mark Brown5a7743e2010-12-13 15:07:40 +0000126 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
127
Mark Brown69dc16c2010-01-25 19:41:57 +0000128 if (selector > WM8994_LDO2_MAX_SELECTOR)
129 return -EINVAL;
130
Mark Brown5a7743e2010-12-13 15:07:40 +0000131 switch (ldo->wm8994->type) {
132 case WM8994:
133 return (selector * 100000) + 900000;
134 case WM8958:
135 return (selector * 100000) + 1000000;
Mark Browna1ff89e2011-07-24 12:40:48 +0100136 case WM1811:
137 switch (selector) {
138 case 0:
139 return -EINVAL;
140 default:
141 return (selector * 100000) + 950000;
142 }
143 break;
Mark Brown5a7743e2010-12-13 15:07:40 +0000144 default:
145 return -EINVAL;
146 }
Mark Brown69dc16c2010-01-25 19:41:57 +0000147}
148
Mark Brownd9f0f282010-12-12 14:07:06 +0000149static int wm8994_ldo2_get_voltage_sel(struct regulator_dev *rdev)
Mark Brown69dc16c2010-01-25 19:41:57 +0000150{
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
Mark Brownd9f0f282010-12-12 14:07:06 +0000158 return (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
Mark Brown69dc16c2010-01-25 19:41:57 +0000159}
160
Mark Brown9f29c9e2012-04-15 11:29:29 +0100161static int wm8994_ldo2_set_voltage_sel(struct regulator_dev *rdev,
162 unsigned selector)
Mark Brown69dc16c2010-01-25 19:41:57 +0000163{
164 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
Mark Brown69dc16c2010-01-25 19:41:57 +0000165
Mark Brown69dc16c2010-01-25 19:41:57 +0000166 selector <<= WM8994_LDO2_VSEL_SHIFT;
167
168 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
169 WM8994_LDO2_VSEL_MASK, selector);
170}
171
172static struct regulator_ops wm8994_ldo2_ops = {
173 .enable = wm8994_ldo_enable,
174 .disable = wm8994_ldo_disable,
175 .is_enabled = wm8994_ldo_is_enabled,
176 .enable_time = wm8994_ldo_enable_time,
177
178 .list_voltage = wm8994_ldo2_list_voltage,
Mark Brownd9f0f282010-12-12 14:07:06 +0000179 .get_voltage_sel = wm8994_ldo2_get_voltage_sel,
Mark Brown9f29c9e2012-04-15 11:29:29 +0100180 .set_voltage_sel = wm8994_ldo2_set_voltage_sel,
Mark Brown69dc16c2010-01-25 19:41:57 +0000181};
182
Mark Brown25e4d602012-04-03 20:47:27 +0100183static const struct regulator_desc wm8994_ldo_desc[] = {
Mark Brown69dc16c2010-01-25 19:41:57 +0000184 {
185 .name = "LDO1",
186 .id = 1,
187 .type = REGULATOR_VOLTAGE,
188 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
189 .ops = &wm8994_ldo1_ops,
190 .owner = THIS_MODULE,
191 },
192 {
193 .name = "LDO2",
194 .id = 2,
195 .type = REGULATOR_VOLTAGE,
196 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
197 .ops = &wm8994_ldo2_ops,
198 .owner = THIS_MODULE,
199 },
200};
201
202static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
203{
204 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
205 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
206 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
Mark Brownc172708d2012-04-04 00:50:22 +0100207 struct regulator_config config = { };
Mark Brown69dc16c2010-01-25 19:41:57 +0000208 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
Mark Brownb9e03482012-02-15 23:12:17 -0800216 ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL);
Mark Brown69dc16c2010-01-25 19:41:57 +0000217 if (ldo == NULL) {
218 dev_err(&pdev->dev, "Unable to allocate private data\n");
219 return -ENOMEM;
220 }
221
222 ldo->wm8994 = wm8994;
223
Mark Brown69dc16c2010-01-25 19:41:57 +0000224 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
225 ldo->enable = pdata->ldo[id].enable;
226
Mark Brown09e3fd22012-05-07 18:18:45 +0100227 ret = gpio_request_one(ldo->enable, 0, "WM8994 LDO enable");
Mark Brown69dc16c2010-01-25 19:41:57 +0000228 if (ret < 0) {
229 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
230 ret);
231 goto err;
232 }
Joonyoung Shimc4604e42010-08-06 15:48:24 +0100233 } else
234 ldo->is_enabled = true;
Mark Brown69dc16c2010-01-25 19:41:57 +0000235
Mark Brown5d0526e2012-05-08 17:45:31 +0100236 config.dev = wm8994->dev;
Mark Brownc172708d2012-04-04 00:50:22 +0100237 config.init_data = pdata->ldo[id].init_data;
238 config.driver_data = ldo;
239
240 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config);
Mark Brown69dc16c2010-01-25 19:41:57 +0000241 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
252err_gpio:
253 if (gpio_is_valid(ldo->enable))
254 gpio_free(ldo->enable);
255err:
Mark Brown69dc16c2010-01-25 19:41:57 +0000256 return ret;
257}
258
259static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
260{
261 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
262
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800263 platform_set_drvdata(pdev, NULL);
264
Mark Brown69dc16c2010-01-25 19:41:57 +0000265 regulator_unregister(ldo->regulator);
266 if (gpio_is_valid(ldo->enable))
267 gpio_free(ldo->enable);
Mark Brown69dc16c2010-01-25 19:41:57 +0000268
269 return 0;
270}
271
272static struct platform_driver wm8994_ldo_driver = {
273 .probe = wm8994_ldo_probe,
274 .remove = __devexit_p(wm8994_ldo_remove),
275 .driver = {
276 .name = "wm8994-ldo",
Dmitry Torokhov598b3572010-02-23 23:38:44 -0800277 .owner = THIS_MODULE,
Mark Brown69dc16c2010-01-25 19:41:57 +0000278 },
279};
280
281static int __init wm8994_ldo_init(void)
282{
283 int ret;
284
285 ret = platform_driver_register(&wm8994_ldo_driver);
286 if (ret != 0)
287 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
288
289 return ret;
290}
291subsys_initcall(wm8994_ldo_init);
292
293static void __exit wm8994_ldo_exit(void)
294{
295 platform_driver_unregister(&wm8994_ldo_driver);
296}
297module_exit(wm8994_ldo_exit);
298
299/* Module information */
300MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
301MODULE_DESCRIPTION("WM8994 LDO driver");
302MODULE_LICENSE("GPL");
303MODULE_ALIAS("platform:wm8994-ldo");