atl1c: Fix hardware type check for enabling OTP CLK
[zen-stable.git] / drivers / regulator / wm8994-regulator.c
blob03713bc66e4a88b9bfda453a0085eb9132bdad60
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(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(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(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 val = (val & WM8994_LDO1_VSEL_MASK) >> WM8994_LDO1_VSEL_SHIFT;
100 return wm8994_ldo1_list_voltage(rdev, val);
103 static int wm8994_ldo1_set_voltage(struct regulator_dev *rdev,
104 int min_uV, int max_uV)
106 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
107 int selector, v;
109 selector = (min_uV - 2400000) / 100000;
110 v = wm8994_ldo1_list_voltage(rdev, selector);
111 if (v < 0 || v > max_uV)
112 return -EINVAL;
114 selector <<= WM8994_LDO1_VSEL_SHIFT;
116 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_1,
117 WM8994_LDO1_VSEL_MASK, selector);
120 static 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,
126 .list_voltage = wm8994_ldo1_list_voltage,
127 .get_voltage = wm8994_ldo1_get_voltage,
128 .set_voltage = wm8994_ldo1_set_voltage,
131 static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev,
132 unsigned int selector)
134 if (selector > WM8994_LDO2_MAX_SELECTOR)
135 return -EINVAL;
137 return (selector * 100000) + 900000;
140 static int wm8994_ldo2_get_voltage(struct regulator_dev *rdev)
142 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
143 int val;
145 val = wm8994_reg_read(ldo->wm8994, WM8994_LDO_2);
146 if (val < 0)
147 return val;
149 val = (val & WM8994_LDO2_VSEL_MASK) >> WM8994_LDO2_VSEL_SHIFT;
151 return wm8994_ldo2_list_voltage(rdev, val);
154 static int wm8994_ldo2_set_voltage(struct regulator_dev *rdev,
155 int min_uV, int max_uV)
157 struct wm8994_ldo *ldo = rdev_get_drvdata(rdev);
158 int selector, v;
160 selector = (min_uV - 900000) / 100000;
161 v = wm8994_ldo2_list_voltage(rdev, selector);
162 if (v < 0 || v > max_uV)
163 return -EINVAL;
165 selector <<= WM8994_LDO2_VSEL_SHIFT;
167 return wm8994_set_bits(ldo->wm8994, WM8994_LDO_2,
168 WM8994_LDO2_VSEL_MASK, selector);
171 static struct regulator_ops wm8994_ldo2_ops = {
172 .enable = wm8994_ldo_enable,
173 .disable = wm8994_ldo_disable,
174 .is_enabled = wm8994_ldo_is_enabled,
175 .enable_time = wm8994_ldo_enable_time,
177 .list_voltage = wm8994_ldo2_list_voltage,
178 .get_voltage = wm8994_ldo2_get_voltage,
179 .set_voltage = wm8994_ldo2_set_voltage,
182 static struct regulator_desc wm8994_ldo_desc[] = {
184 .name = "LDO1",
185 .id = 1,
186 .type = REGULATOR_VOLTAGE,
187 .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1,
188 .ops = &wm8994_ldo1_ops,
189 .owner = THIS_MODULE,
192 .name = "LDO2",
193 .id = 2,
194 .type = REGULATOR_VOLTAGE,
195 .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1,
196 .ops = &wm8994_ldo2_ops,
197 .owner = THIS_MODULE,
201 static __devinit int wm8994_ldo_probe(struct platform_device *pdev)
203 struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent);
204 struct wm8994_pdata *pdata = wm8994->dev->platform_data;
205 int id = pdev->id % ARRAY_SIZE(pdata->ldo);
206 struct wm8994_ldo *ldo;
207 int ret;
209 dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
211 if (!pdata)
212 return -ENODEV;
214 ldo = kzalloc(sizeof(struct wm8994_ldo), GFP_KERNEL);
215 if (ldo == NULL) {
216 dev_err(&pdev->dev, "Unable to allocate private data\n");
217 return -ENOMEM;
220 ldo->wm8994 = wm8994;
222 if (pdata->ldo[id].enable && gpio_is_valid(pdata->ldo[id].enable)) {
223 ldo->enable = pdata->ldo[id].enable;
225 ret = gpio_request(ldo->enable, "WM8994 LDO enable");
226 if (ret < 0) {
227 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n",
228 ret);
229 goto err;
232 ret = gpio_direction_output(ldo->enable, ldo->is_enabled);
233 if (ret < 0) {
234 dev_err(&pdev->dev, "Failed to set GPIO up: %d\n",
235 ret);
236 goto err_gpio;
238 } else
239 ldo->is_enabled = true;
241 ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &pdev->dev,
242 pdata->ldo[id].init_data, ldo);
243 if (IS_ERR(ldo->regulator)) {
244 ret = PTR_ERR(ldo->regulator);
245 dev_err(wm8994->dev, "Failed to register LDO%d: %d\n",
246 id + 1, ret);
247 goto err_gpio;
250 platform_set_drvdata(pdev, ldo);
252 return 0;
254 err_gpio:
255 if (gpio_is_valid(ldo->enable))
256 gpio_free(ldo->enable);
257 err:
258 kfree(ldo);
259 return ret;
262 static __devexit int wm8994_ldo_remove(struct platform_device *pdev)
264 struct wm8994_ldo *ldo = platform_get_drvdata(pdev);
266 platform_set_drvdata(pdev, NULL);
268 regulator_unregister(ldo->regulator);
269 if (gpio_is_valid(ldo->enable))
270 gpio_free(ldo->enable);
271 kfree(ldo);
273 return 0;
276 static struct platform_driver wm8994_ldo_driver = {
277 .probe = wm8994_ldo_probe,
278 .remove = __devexit_p(wm8994_ldo_remove),
279 .driver = {
280 .name = "wm8994-ldo",
281 .owner = THIS_MODULE,
285 static int __init wm8994_ldo_init(void)
287 int ret;
289 ret = platform_driver_register(&wm8994_ldo_driver);
290 if (ret != 0)
291 pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret);
293 return ret;
295 subsys_initcall(wm8994_ldo_init);
297 static void __exit wm8994_ldo_exit(void)
299 platform_driver_unregister(&wm8994_ldo_driver);
301 module_exit(wm8994_ldo_exit);
303 /* Module information */
304 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
305 MODULE_DESCRIPTION("WM8994 LDO driver");
306 MODULE_LICENSE("GPL");
307 MODULE_ALIAS("platform:wm8994-ldo");