blob: 2c38bea5065ea2f7ff34712f44582eee579c3599 [file] [log] [blame]
Heiko Stübner3f0292a2011-10-05 12:27:05 +02001/*
2 * gpio-regulator.c
3 *
4 * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
5 *
6 * based on fixed.c
7 *
8 * Copyright 2008 Wolfson Microelectronics PLC.
9 *
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * Copyright (c) 2009 Nokia Corporation
13 * Roger Quadros <ext-roger.quadros@nokia.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
19 *
20 * This is useful for systems with mixed controllable and
21 * non-controllable regulators, as well as for allowing testing on
22 * systems with no controllable regulators.
23 */
24
25#include <linux/err.h>
26#include <linux/mutex.h>
Mark Brownecc37ed2011-10-11 13:59:13 +010027#include <linux/module.h>
Heiko Stübner3f0292a2011-10-05 12:27:05 +020028#include <linux/platform_device.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
31#include <linux/regulator/gpio-regulator.h>
32#include <linux/gpio.h>
Heiko Stübner3f0292a2011-10-05 12:27:05 +020033#include <linux/slab.h>
34
35struct gpio_regulator_data {
36 struct regulator_desc desc;
37 struct regulator_dev *dev;
38
39 int enable_gpio;
40 bool enable_high;
41 bool is_enabled;
42 unsigned startup_delay;
43
44 struct gpio *gpios;
45 int nr_gpios;
46
47 struct gpio_regulator_state *states;
48 int nr_states;
49
50 int state;
51};
52
53static int gpio_regulator_is_enabled(struct regulator_dev *dev)
54{
55 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
56
57 return data->is_enabled;
58}
59
60static int gpio_regulator_enable(struct regulator_dev *dev)
61{
62 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
63
64 if (gpio_is_valid(data->enable_gpio)) {
65 gpio_set_value_cansleep(data->enable_gpio, data->enable_high);
66 data->is_enabled = true;
67 }
68
69 return 0;
70}
71
72static int gpio_regulator_disable(struct regulator_dev *dev)
73{
74 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
75
76 if (gpio_is_valid(data->enable_gpio)) {
77 gpio_set_value_cansleep(data->enable_gpio, !data->enable_high);
78 data->is_enabled = false;
79 }
80
81 return 0;
82}
83
84static int gpio_regulator_enable_time(struct regulator_dev *dev)
85{
86 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
87
88 return data->startup_delay;
89}
90
91static int gpio_regulator_get_value(struct regulator_dev *dev)
92{
93 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
94 int ptr;
95
96 for (ptr = 0; ptr < data->nr_states; ptr++)
97 if (data->states[ptr].gpios == data->state)
98 return data->states[ptr].value;
99
100 return -EINVAL;
101}
102
103static int gpio_regulator_set_value(struct regulator_dev *dev,
104 int min, int max)
105{
106 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
Heiko Stübner00926362012-06-03 21:31:09 +0200107 int ptr, target = 0, state, best_val = INT_MAX;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200108
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200109 for (ptr = 0; ptr < data->nr_states; ptr++)
Axel Lin4dbd8f62012-03-22 14:08:04 +0800110 if (data->states[ptr].value < best_val &&
111 data->states[ptr].value >= min &&
Heiko Stübner00926362012-06-03 21:31:09 +0200112 data->states[ptr].value <= max) {
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200113 target = data->states[ptr].gpios;
Heiko Stübner00926362012-06-03 21:31:09 +0200114 best_val = data->states[ptr].value;
115 }
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200116
Axel Lin4dbd8f62012-03-22 14:08:04 +0800117 if (best_val == INT_MAX)
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200118 return -EINVAL;
119
120 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
121 state = (target & (1 << ptr)) >> ptr;
122 gpio_set_value(data->gpios[ptr].gpio, state);
123 }
124 data->state = target;
125
126 return 0;
127}
128
129static int gpio_regulator_set_voltage(struct regulator_dev *dev,
130 int min_uV, int max_uV,
131 unsigned *selector)
132{
133 return gpio_regulator_set_value(dev, min_uV, max_uV);
134}
135
136static int gpio_regulator_list_voltage(struct regulator_dev *dev,
137 unsigned selector)
138{
139 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
140
141 if (selector >= data->nr_states)
142 return -EINVAL;
143
144 return data->states[selector].value;
145}
146
147static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
148 int min_uA, int max_uA)
149{
150 return gpio_regulator_set_value(dev, min_uA, max_uA);
151}
152
153static struct regulator_ops gpio_regulator_voltage_ops = {
154 .is_enabled = gpio_regulator_is_enabled,
155 .enable = gpio_regulator_enable,
156 .disable = gpio_regulator_disable,
157 .enable_time = gpio_regulator_enable_time,
158 .get_voltage = gpio_regulator_get_value,
159 .set_voltage = gpio_regulator_set_voltage,
160 .list_voltage = gpio_regulator_list_voltage,
161};
162
163static struct regulator_ops gpio_regulator_current_ops = {
164 .is_enabled = gpio_regulator_is_enabled,
165 .enable = gpio_regulator_enable,
166 .disable = gpio_regulator_disable,
167 .enable_time = gpio_regulator_enable_time,
168 .get_current_limit = gpio_regulator_get_value,
169 .set_current_limit = gpio_regulator_set_current_limit,
170};
171
172static int __devinit gpio_regulator_probe(struct platform_device *pdev)
173{
174 struct gpio_regulator_config *config = pdev->dev.platform_data;
175 struct gpio_regulator_data *drvdata;
Mark Brownc1727082012-04-04 00:50:22 +0100176 struct regulator_config cfg = { };
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200177 int ptr, ret, state;
178
Mark Brown02b552162012-04-03 23:20:56 +0100179 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
180 GFP_KERNEL);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200181 if (drvdata == NULL) {
182 dev_err(&pdev->dev, "Failed to allocate device data\n");
183 return -ENOMEM;
184 }
185
186 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
187 if (drvdata->desc.name == NULL) {
188 dev_err(&pdev->dev, "Failed to allocate supply name\n");
189 ret = -ENOMEM;
190 goto err;
191 }
192
193 drvdata->gpios = kmemdup(config->gpios,
194 config->nr_gpios * sizeof(struct gpio),
195 GFP_KERNEL);
196 if (drvdata->gpios == NULL) {
197 dev_err(&pdev->dev, "Failed to allocate gpio data\n");
198 ret = -ENOMEM;
199 goto err_name;
200 }
201
202 drvdata->states = kmemdup(config->states,
203 config->nr_states *
204 sizeof(struct gpio_regulator_state),
205 GFP_KERNEL);
206 if (drvdata->states == NULL) {
207 dev_err(&pdev->dev, "Failed to allocate state data\n");
208 ret = -ENOMEM;
209 goto err_memgpio;
210 }
211 drvdata->nr_states = config->nr_states;
212
213 drvdata->desc.owner = THIS_MODULE;
214
215 /* handle regulator type*/
216 switch (config->type) {
217 case REGULATOR_VOLTAGE:
218 drvdata->desc.type = REGULATOR_VOLTAGE;
219 drvdata->desc.ops = &gpio_regulator_voltage_ops;
220 drvdata->desc.n_voltages = config->nr_states;
221 break;
222 case REGULATOR_CURRENT:
223 drvdata->desc.type = REGULATOR_CURRENT;
224 drvdata->desc.ops = &gpio_regulator_current_ops;
225 break;
226 default:
227 dev_err(&pdev->dev, "No regulator type set\n");
228 ret = -EINVAL;
229 goto err_memgpio;
230 break;
231 }
232
233 drvdata->enable_gpio = config->enable_gpio;
234 drvdata->startup_delay = config->startup_delay;
235
236 if (gpio_is_valid(config->enable_gpio)) {
237 drvdata->enable_high = config->enable_high;
238
239 ret = gpio_request(config->enable_gpio, config->supply_name);
240 if (ret) {
241 dev_err(&pdev->dev,
242 "Could not obtain regulator enable GPIO %d: %d\n",
243 config->enable_gpio, ret);
244 goto err_memstate;
245 }
246
247 /* set output direction without changing state
248 * to prevent glitch
249 */
250 if (config->enabled_at_boot) {
251 drvdata->is_enabled = true;
252 ret = gpio_direction_output(config->enable_gpio,
253 config->enable_high);
254 } else {
255 drvdata->is_enabled = false;
256 ret = gpio_direction_output(config->enable_gpio,
257 !config->enable_high);
258 }
259
260 if (ret) {
261 dev_err(&pdev->dev,
262 "Could not configure regulator enable GPIO %d direction: %d\n",
263 config->enable_gpio, ret);
264 goto err_enablegpio;
265 }
266 } else {
267 /* Regulator without GPIO control is considered
268 * always enabled
269 */
270 drvdata->is_enabled = true;
271 }
272
273 drvdata->nr_gpios = config->nr_gpios;
274 ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
275 if (ret) {
276 dev_err(&pdev->dev,
277 "Could not obtain regulator setting GPIOs: %d\n", ret);
278 goto err_enablegpio;
279 }
280
281 /* build initial state from gpio init data. */
282 state = 0;
283 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
284 if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
285 state |= (1 << ptr);
286 }
287 drvdata->state = state;
288
Mark Brownc1727082012-04-04 00:50:22 +0100289 cfg.dev = &pdev->dev;
290 cfg.init_data = config->init_data;
Heiko Stübner7d4be2f2012-06-03 21:30:33 +0200291 cfg.driver_data = drvdata;
Mark Brownc1727082012-04-04 00:50:22 +0100292
293 drvdata->dev = regulator_register(&drvdata->desc, &cfg);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200294 if (IS_ERR(drvdata->dev)) {
295 ret = PTR_ERR(drvdata->dev);
296 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
297 goto err_stategpio;
298 }
299
300 platform_set_drvdata(pdev, drvdata);
301
302 return 0;
303
304err_stategpio:
305 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
306err_enablegpio:
307 if (gpio_is_valid(config->enable_gpio))
308 gpio_free(config->enable_gpio);
309err_memstate:
310 kfree(drvdata->states);
311err_memgpio:
312 kfree(drvdata->gpios);
313err_name:
314 kfree(drvdata->desc.name);
315err:
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200316 return ret;
317}
318
319static int __devexit gpio_regulator_remove(struct platform_device *pdev)
320{
321 struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
322
323 regulator_unregister(drvdata->dev);
324
325 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
326
327 kfree(drvdata->states);
328 kfree(drvdata->gpios);
329
330 if (gpio_is_valid(drvdata->enable_gpio))
331 gpio_free(drvdata->enable_gpio);
332
333 kfree(drvdata->desc.name);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200334
335 return 0;
336}
337
338static struct platform_driver gpio_regulator_driver = {
339 .probe = gpio_regulator_probe,
340 .remove = __devexit_p(gpio_regulator_remove),
341 .driver = {
342 .name = "gpio-regulator",
343 .owner = THIS_MODULE,
344 },
345};
346
347static int __init gpio_regulator_init(void)
348{
349 return platform_driver_register(&gpio_regulator_driver);
350}
351subsys_initcall(gpio_regulator_init);
352
353static void __exit gpio_regulator_exit(void)
354{
355 platform_driver_unregister(&gpio_regulator_driver);
356}
357module_exit(gpio_regulator_exit);
358
359MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
360MODULE_DESCRIPTION("gpio voltage regulator");
361MODULE_LICENSE("GPL");
362MODULE_ALIAS("platform:gpio-regulator");