]> Pileus Git - ~andy/linux/blob - drivers/regulator/gpio-regulator.c
smpboot: Remove leftover declaration
[~andy/linux] / drivers / regulator / gpio-regulator.c
1 /*
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>
27 #include <linux/module.h>
28 #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>
33 #include <linux/slab.h>
34
35 struct 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
53 static 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
60 static 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
72 static 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
84 static 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
91 static 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
103 static 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);
107         int ptr, target, state, best_val = INT_MAX;
108
109         for (ptr = 0; ptr < data->nr_states; ptr++)
110                 if (data->states[ptr].value < best_val &&
111                     data->states[ptr].value >= min &&
112                     data->states[ptr].value <= max)
113                         target = data->states[ptr].gpios;
114
115         if (best_val == INT_MAX)
116                 return -EINVAL;
117
118         for (ptr = 0; ptr < data->nr_gpios; ptr++) {
119                 state = (target & (1 << ptr)) >> ptr;
120                 gpio_set_value(data->gpios[ptr].gpio, state);
121         }
122         data->state = target;
123
124         return 0;
125 }
126
127 static int gpio_regulator_set_voltage(struct regulator_dev *dev,
128                                         int min_uV, int max_uV,
129                                         unsigned *selector)
130 {
131         return gpio_regulator_set_value(dev, min_uV, max_uV);
132 }
133
134 static int gpio_regulator_list_voltage(struct regulator_dev *dev,
135                                       unsigned selector)
136 {
137         struct gpio_regulator_data *data = rdev_get_drvdata(dev);
138
139         if (selector >= data->nr_states)
140                 return -EINVAL;
141
142         return data->states[selector].value;
143 }
144
145 static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
146                                         int min_uA, int max_uA)
147 {
148         return gpio_regulator_set_value(dev, min_uA, max_uA);
149 }
150
151 static struct regulator_ops gpio_regulator_voltage_ops = {
152         .is_enabled = gpio_regulator_is_enabled,
153         .enable = gpio_regulator_enable,
154         .disable = gpio_regulator_disable,
155         .enable_time = gpio_regulator_enable_time,
156         .get_voltage = gpio_regulator_get_value,
157         .set_voltage = gpio_regulator_set_voltage,
158         .list_voltage = gpio_regulator_list_voltage,
159 };
160
161 static struct regulator_ops gpio_regulator_current_ops = {
162         .is_enabled = gpio_regulator_is_enabled,
163         .enable = gpio_regulator_enable,
164         .disable = gpio_regulator_disable,
165         .enable_time = gpio_regulator_enable_time,
166         .get_current_limit = gpio_regulator_get_value,
167         .set_current_limit = gpio_regulator_set_current_limit,
168 };
169
170 static int __devinit gpio_regulator_probe(struct platform_device *pdev)
171 {
172         struct gpio_regulator_config *config = pdev->dev.platform_data;
173         struct gpio_regulator_data *drvdata;
174         struct regulator_config cfg = { };
175         int ptr, ret, state;
176
177         drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
178                                GFP_KERNEL);
179         if (drvdata == NULL) {
180                 dev_err(&pdev->dev, "Failed to allocate device data\n");
181                 return -ENOMEM;
182         }
183
184         drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
185         if (drvdata->desc.name == NULL) {
186                 dev_err(&pdev->dev, "Failed to allocate supply name\n");
187                 ret = -ENOMEM;
188                 goto err;
189         }
190
191         drvdata->gpios = kmemdup(config->gpios,
192                                  config->nr_gpios * sizeof(struct gpio),
193                                  GFP_KERNEL);
194         if (drvdata->gpios == NULL) {
195                 dev_err(&pdev->dev, "Failed to allocate gpio data\n");
196                 ret = -ENOMEM;
197                 goto err_name;
198         }
199
200         drvdata->states = kmemdup(config->states,
201                                   config->nr_states *
202                                          sizeof(struct gpio_regulator_state),
203                                   GFP_KERNEL);
204         if (drvdata->states == NULL) {
205                 dev_err(&pdev->dev, "Failed to allocate state data\n");
206                 ret = -ENOMEM;
207                 goto err_memgpio;
208         }
209         drvdata->nr_states = config->nr_states;
210
211         drvdata->desc.owner = THIS_MODULE;
212
213         /* handle regulator type*/
214         switch (config->type) {
215         case REGULATOR_VOLTAGE:
216                 drvdata->desc.type = REGULATOR_VOLTAGE;
217                 drvdata->desc.ops = &gpio_regulator_voltage_ops;
218                 drvdata->desc.n_voltages = config->nr_states;
219                 break;
220         case REGULATOR_CURRENT:
221                 drvdata->desc.type = REGULATOR_CURRENT;
222                 drvdata->desc.ops = &gpio_regulator_current_ops;
223                 break;
224         default:
225                 dev_err(&pdev->dev, "No regulator type set\n");
226                 ret = -EINVAL;
227                 goto err_memgpio;
228                 break;
229         }
230
231         drvdata->enable_gpio = config->enable_gpio;
232         drvdata->startup_delay = config->startup_delay;
233
234         if (gpio_is_valid(config->enable_gpio)) {
235                 drvdata->enable_high = config->enable_high;
236
237                 ret = gpio_request(config->enable_gpio, config->supply_name);
238                 if (ret) {
239                         dev_err(&pdev->dev,
240                            "Could not obtain regulator enable GPIO %d: %d\n",
241                                                 config->enable_gpio, ret);
242                         goto err_memstate;
243                 }
244
245                 /* set output direction without changing state
246                  * to prevent glitch
247                  */
248                 if (config->enabled_at_boot) {
249                         drvdata->is_enabled = true;
250                         ret = gpio_direction_output(config->enable_gpio,
251                                                     config->enable_high);
252                 } else {
253                         drvdata->is_enabled = false;
254                         ret = gpio_direction_output(config->enable_gpio,
255                                                     !config->enable_high);
256                 }
257
258                 if (ret) {
259                         dev_err(&pdev->dev,
260                            "Could not configure regulator enable GPIO %d direction: %d\n",
261                                                 config->enable_gpio, ret);
262                         goto err_enablegpio;
263                 }
264         } else {
265                 /* Regulator without GPIO control is considered
266                  * always enabled
267                  */
268                 drvdata->is_enabled = true;
269         }
270
271         drvdata->nr_gpios = config->nr_gpios;
272         ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
273         if (ret) {
274                 dev_err(&pdev->dev,
275                    "Could not obtain regulator setting GPIOs: %d\n", ret);
276                 goto err_enablegpio;
277         }
278
279         /* build initial state from gpio init data. */
280         state = 0;
281         for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
282                 if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
283                         state |= (1 << ptr);
284         }
285         drvdata->state = state;
286
287         cfg.dev = &pdev->dev;
288         cfg.init_data = config->init_data;
289         cfg.driver_data = &drvdata;
290
291         drvdata->dev = regulator_register(&drvdata->desc, &cfg);
292         if (IS_ERR(drvdata->dev)) {
293                 ret = PTR_ERR(drvdata->dev);
294                 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
295                 goto err_stategpio;
296         }
297
298         platform_set_drvdata(pdev, drvdata);
299
300         return 0;
301
302 err_stategpio:
303         gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
304 err_enablegpio:
305         if (gpio_is_valid(config->enable_gpio))
306                 gpio_free(config->enable_gpio);
307 err_memstate:
308         kfree(drvdata->states);
309 err_memgpio:
310         kfree(drvdata->gpios);
311 err_name:
312         kfree(drvdata->desc.name);
313 err:
314         return ret;
315 }
316
317 static int __devexit gpio_regulator_remove(struct platform_device *pdev)
318 {
319         struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
320
321         regulator_unregister(drvdata->dev);
322
323         gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
324
325         kfree(drvdata->states);
326         kfree(drvdata->gpios);
327
328         if (gpio_is_valid(drvdata->enable_gpio))
329                 gpio_free(drvdata->enable_gpio);
330
331         kfree(drvdata->desc.name);
332
333         return 0;
334 }
335
336 static struct platform_driver gpio_regulator_driver = {
337         .probe          = gpio_regulator_probe,
338         .remove         = __devexit_p(gpio_regulator_remove),
339         .driver         = {
340                 .name           = "gpio-regulator",
341                 .owner          = THIS_MODULE,
342         },
343 };
344
345 static int __init gpio_regulator_init(void)
346 {
347         return platform_driver_register(&gpio_regulator_driver);
348 }
349 subsys_initcall(gpio_regulator_init);
350
351 static void __exit gpio_regulator_exit(void)
352 {
353         platform_driver_unregister(&gpio_regulator_driver);
354 }
355 module_exit(gpio_regulator_exit);
356
357 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
358 MODULE_DESCRIPTION("gpio voltage regulator");
359 MODULE_LICENSE("GPL");
360 MODULE_ALIAS("platform:gpio-regulator");