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