spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / regulator / wm8994-regulator.c
blob435e335d6e67ae372054bcb697e773684ab40d3d
1 /*
2 * wm8994-regulator.c -- Regulator driver for the WM8994
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
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>
24 #include <linux/mfd/wm8994/core.h>
25 #include <linux/mfd/wm8994/registers.h>
26 #include <linux/mfd/wm8994/pdata.h>
28 struct wm8994_ldo {
29 int enable;
30 bool is_enabled;
31 struct regulator_dev *regulator;
32 struct wm8994 *wm8994;
35 #define WM8994_LDO1_MAX_SELECTOR 0x7
36 #define WM8994_LDO2_MAX_SELECTOR 0x3
38 static int wm8994_ldo_enable(struct regulator_dev *rdev)
40 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
42 /* If we have no soft control assume that the LDO is always enabled. */
43 if (!ldo->enable)
44 return 0;
46 gpio_set_value_cansleep(ldo->enable, 1);
47 ldo->is_enabled = true;
49 return 0;
52 static int wm8994_ldo_disable(struct regulator_dev *rdev)
54 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
56 /* If we have no soft control assume that the LDO is always enabled. */
57 if (!ldo->enable)
58 return -EINVAL;
60 gpio_set_value_cansleep(ldo->enable, 0);
61 ldo->is_enabled = false;
63 return 0;
66 static int wm8994_ldo_is_enabled(struct regulator_dev *rdev)
68 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
70 return ldo->is_enabled;
73 static int wm8994_ldo_enable_time(struct regulator_dev *rdev)
75 /* 3ms is fairly conservative but this shouldn't be too performance
76 * critical; can be tweaked per-system if required. */
77 return 3000;
80 static int wm8994_ldo1_list_voltage(struct regulator_dev *rdev,
81 unsigned int selector)
83 if (selector > WM8994_LDO1_MAX_SELECTOR)
84 return -EINVAL;
86 return (selector * 100000) + 2400000;
89 static int wm8994_ldo1_get_voltage_sel(struct regulator_dev *rdev)
91 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
92 int val;
94 val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_1);
95 if (val < 0)
96 return val;
98 return (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
101 static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV, unsigned *s)
104 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
105 int selector, v;
107 selector = (min_uV - 2400000) / 100000;
108 v = wm8994_ldo1_list_voltage(rdev, selector);
109 if (v < 0 || v > max_uV)
110 return -EINVAL;
112 *s = selector;
113 selector <<= WM8994_LDO1_VSEL_SHIFT;
115 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
116 WM8994_LDO1_VSEL_MASK, selector);
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,
125 .list_voltage = wm8994_ldo1_list_voltage,
126 .get_voltage_sel = wm8994_ldo1_get_voltage_sel,
127 .set_voltage = wm8994_ldo1_set_voltage,
130 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
131 unsigned int selector)
133 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
135 if (selector > WM8994_LDO2_MAX_SELECTOR)
136 return -EINVAL;
138 switch (ldo->wm8994->type) {
139 case WM8994:
140 return (selector * 100000) + 900000;
141 case WM8958:
142 return (selector * 100000) + 1000000;
143 case WM1811:
144 switch (selector) {
145 case 0:
146 return -EINVAL;
147 default:
148 return (selector * 100000) + 950000;
150 break;
151 default:
152 return -EINVAL;
156 static int wm8994_ldo2_get_voltage_sel(struct regulator_dev *rdev)
158 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
159 int val;
161 val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
162 if (val < 0)
163 return val;
165 return (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
168 static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
169 int min_uV, int max_uV, unsigned *s)
171 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
172 int selector, v;
174 switch (ldo->wm8994->type) {
175 case WM8994:
176 selector = (min_uV - 900000) / 100000;
177 break;
178 case WM8958:
179 selector = (min_uV - 1000000) / 100000;
180 break;
181 case WM1811:
182 selector = (min_uV - 950000) / 100000;
183 if (selector == 0)
184 selector = 1;
185 break;
186 default:
187 return -EINVAL;
190 v = wm8994_ldo2_list_voltage(rdev, selector);
191 if (v < 0 || v > max_uV)
192 return -EINVAL;
194 *s = selector;
195 selector <<= WM8994_LDO2_VSEL_SHIFT;
197 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
198 WM8994_LDO2_VSEL_MASK, selector);
201 static struct regulator_ops wm8994_ldo2_ops = {
202 .enable = wm8994_ldo_enable,
203 .disable = wm8994_ldo_disable,
204 .is_enabled = wm8994_ldo_is_enabled,
205 .enable_time = wm8994_ldo_enable_time,
207 .list_voltage = wm8994_ldo2_list_voltage,
208 .get_voltage_sel = wm8994_ldo2_get_voltage_sel,
209 .set_voltage = wm8994_ldo2_set_voltage,
212 static struct regulator_desc wm8994_ldo_desc[] = {
214 .name = "LDO1",
215 .id = 1,
216 .type = REGULATOR_VOLTAGE,
217 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
218 .ops = &wm8994_ldo1_ops,
219 .owner = THIS_MODULE,
222 .name = "LDO2",
223 .id = 2,
224 .type = REGULATOR_VOLTAGE,
225 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
226 .ops = &wm8994_ldo2_ops,
227 .owner = THIS_MODULE,
231 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
233 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
234 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
235 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
236 struct wm8994_ldo *ldo;
237 int ret;
239 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
241 if (!pdata)
242 return -ENODEV;
244 ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
245 if (ldo == NULL) {
246 dev_err(&pdev->dev, "Unable to allocate private data\n");
247 return -ENOMEM;
250 ldo->wm8994 = wm8994;
252 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
253 ldo->enable = pdata->ldo[id].enable;
255 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
256 if (ret < 0) {
257 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
258 ret);
259 goto err;
262 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
263 if (ret < 0) {
264 dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
265 ret);
266 goto err_gpio;
268 } else
269 ldo->is_enabled = true;
271 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
272 pdata->ldo[id].init_data, ldo, NULL);
273 if (IS_ERR(ldo->regulator)) {
274 ret = PTR_ERR(ldo->regulator);
275 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
276 id + 1, ret);
277 goto err_gpio;
280 platform_set_drvdata(pdev, ldo);
282 return 0;
284 err_gpio:
285 if (gpio_is_valid(ldo->enable))
286 gpio_free(ldo->enable);
287 err:
288 kfree(ldo);
289 return ret;
292 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
294 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
296 platform_set_drvdata(pdev, NULL);
298 regulator_unregister(ldo->regulator);
299 if (gpio_is_valid(ldo->enable))
300 gpio_free(ldo->enable);
301 kfree(ldo);
303 return 0;
306 static struct platform_driver wm8994_ldo_driver = {
307 .probe = wm8994_ldo_probe,
308 .remove = __devexit_p(wm8994_ldo_remove),
309 .driver = {
310 .name = "wm8994-ldo",
311 .owner = THIS_MODULE,
315 static int __init wm8994_ldo_init(void)
317 int ret;
319 ret = platform_driver_register(&wm8994_ldo_driver);
320 if (ret != 0)
321 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
323 return ret;
325 subsys_initcall(wm8994_ldo_init);
327 static void __exit wm8994_ldo_exit(void)
329 platform_driver_unregister(&wm8994_ldo_driver);
331 module_exit(wm8994_ldo_exit);
333 /* Module information */
334 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
335 MODULE_DESCRIPTION("WM8994 LDO driver");
336 MODULE_LICENSE("GPL");
337 MODULE_ALIAS("platform:wm8994-ldo");