regulator: fixed/gpio: Pull inversion/OD into gpiolib
[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/of_regulator.h>
32 #include <linux/regulator/gpio-regulator.h>
33 #include <linux/gpio/consumer.h>
34 #include <linux/slab.h>
35 #include <linux/of.h>
36
37 struct gpio_regulator_data {
38         struct regulator_desc desc;
39         struct regulator_dev *dev;
40
41         struct gpio_desc **gpiods;
42         int nr_gpios;
43
44         struct gpio_regulator_state *states;
45         int nr_states;
46
47         int state;
48 };
49
50 static int gpio_regulator_get_value(struct regulator_dev *dev)
51 {
52         struct gpio_regulator_data *data = rdev_get_drvdata(dev);
53         int ptr;
54
55         for (ptr = 0; ptr < data->nr_states; ptr++)
56                 if (data->states[ptr].gpios == data->state)
57                         return data->states[ptr].value;
58
59         return -EINVAL;
60 }
61
62 static int gpio_regulator_set_voltage(struct regulator_dev *dev,
63                                         int min_uV, int max_uV,
64                                         unsigned *selector)
65 {
66         struct gpio_regulator_data *data = rdev_get_drvdata(dev);
67         int ptr, target = 0, state, best_val = INT_MAX;
68
69         for (ptr = 0; ptr < data->nr_states; ptr++)
70                 if (data->states[ptr].value < best_val &&
71                     data->states[ptr].value >= min_uV &&
72                     data->states[ptr].value <= max_uV) {
73                         target = data->states[ptr].gpios;
74                         best_val = data->states[ptr].value;
75                         if (selector)
76                                 *selector = ptr;
77                 }
78
79         if (best_val == INT_MAX)
80                 return -EINVAL;
81
82         for (ptr = 0; ptr < data->nr_gpios; ptr++) {
83                 state = (target & (1 << ptr)) >> ptr;
84                 gpiod_set_value_cansleep(data->gpiods[ptr], state);
85         }
86         data->state = target;
87
88         return 0;
89 }
90
91 static int gpio_regulator_list_voltage(struct regulator_dev *dev,
92                                       unsigned selector)
93 {
94         struct gpio_regulator_data *data = rdev_get_drvdata(dev);
95
96         if (selector >= data->nr_states)
97                 return -EINVAL;
98
99         return data->states[selector].value;
100 }
101
102 static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
103                                         int min_uA, int max_uA)
104 {
105         struct gpio_regulator_data *data = rdev_get_drvdata(dev);
106         int ptr, target = 0, state, best_val = 0;
107
108         for (ptr = 0; ptr < data->nr_states; ptr++)
109                 if (data->states[ptr].value > best_val &&
110                     data->states[ptr].value >= min_uA &&
111                     data->states[ptr].value <= max_uA) {
112                         target = data->states[ptr].gpios;
113                         best_val = data->states[ptr].value;
114                 }
115
116         if (best_val == 0)
117                 return -EINVAL;
118
119         for (ptr = 0; ptr < data->nr_gpios; ptr++) {
120                 state = (target & (1 << ptr)) >> ptr;
121                 gpiod_set_value_cansleep(data->gpiods[ptr], state);
122         }
123         data->state = target;
124
125         return 0;
126 }
127
128 static struct regulator_ops gpio_regulator_voltage_ops = {
129         .get_voltage = gpio_regulator_get_value,
130         .set_voltage = gpio_regulator_set_voltage,
131         .list_voltage = gpio_regulator_list_voltage,
132 };
133
134 static struct gpio_regulator_config *
135 of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
136                              const struct regulator_desc *desc)
137 {
138         struct gpio_regulator_config *config;
139         const char *regtype;
140         int proplen, i;
141         int ngpios;
142         int ret;
143
144         config = devm_kzalloc(dev,
145                         sizeof(struct gpio_regulator_config),
146                         GFP_KERNEL);
147         if (!config)
148                 return ERR_PTR(-ENOMEM);
149
150         config->init_data = of_get_regulator_init_data(dev, np, desc);
151         if (!config->init_data)
152                 return ERR_PTR(-EINVAL);
153
154         config->supply_name = config->init_data->constraints.name;
155
156         if (of_property_read_bool(np, "enable-at-boot"))
157                 config->enabled_at_boot = true;
158
159         of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
160
161         /* Fetch GPIO init levels */
162         ngpios = gpiod_count(dev, NULL);
163         if (ngpios > 0) {
164                 config->gflags = devm_kzalloc(dev,
165                                               sizeof(enum gpiod_flags)
166                                               * ngpios,
167                                               GFP_KERNEL);
168                 if (!config->gflags)
169                         return ERR_PTR(-ENOMEM);
170
171                 for (i = 0; i < ngpios; i++) {
172                         u32 val;
173
174                         ret = of_property_read_u32_index(np, "gpios-states", i,
175                                                          &val);
176
177                         /* Default to high per specification */
178                         if (ret)
179                                 config->gflags[i] = GPIOD_OUT_HIGH;
180                         else
181                                 config->gflags[i] =
182                                         val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
183                 }
184         }
185         config->ngpios = ngpios;
186
187         /* Fetch states. */
188         proplen = of_property_count_u32_elems(np, "states");
189         if (proplen < 0) {
190                 dev_err(dev, "No 'states' property found\n");
191                 return ERR_PTR(-EINVAL);
192         }
193
194         config->states = devm_kcalloc(dev,
195                                 proplen / 2,
196                                 sizeof(struct gpio_regulator_state),
197                                 GFP_KERNEL);
198         if (!config->states)
199                 return ERR_PTR(-ENOMEM);
200
201         for (i = 0; i < proplen / 2; i++) {
202                 of_property_read_u32_index(np, "states", i * 2,
203                                            &config->states[i].value);
204                 of_property_read_u32_index(np, "states", i * 2 + 1,
205                                            &config->states[i].gpios);
206         }
207         config->nr_states = i;
208
209         config->type = REGULATOR_VOLTAGE;
210         ret = of_property_read_string(np, "regulator-type", &regtype);
211         if (ret >= 0) {
212                 if (!strncmp("voltage", regtype, 7))
213                         config->type = REGULATOR_VOLTAGE;
214                 else if (!strncmp("current", regtype, 7))
215                         config->type = REGULATOR_CURRENT;
216                 else
217                         dev_warn(dev, "Unknown regulator-type '%s'\n",
218                                  regtype);
219         }
220
221         return config;
222 }
223
224 static struct regulator_ops gpio_regulator_current_ops = {
225         .get_current_limit = gpio_regulator_get_value,
226         .set_current_limit = gpio_regulator_set_current_limit,
227 };
228
229 static int gpio_regulator_probe(struct platform_device *pdev)
230 {
231         struct gpio_regulator_config *config = dev_get_platdata(&pdev->dev);
232         struct device_node *np = pdev->dev.of_node;
233         struct gpio_regulator_data *drvdata;
234         struct regulator_config cfg = { };
235         enum gpiod_flags gflags;
236         int ptr, ret, state, i;
237
238         drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
239                                GFP_KERNEL);
240         if (drvdata == NULL)
241                 return -ENOMEM;
242
243         if (np) {
244                 config = of_get_gpio_regulator_config(&pdev->dev, np,
245                                                       &drvdata->desc);
246                 if (IS_ERR(config))
247                         return PTR_ERR(config);
248         }
249
250         drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
251         if (drvdata->desc.name == NULL) {
252                 dev_err(&pdev->dev, "Failed to allocate supply name\n");
253                 return -ENOMEM;
254         }
255
256         drvdata->gpiods = devm_kzalloc(&pdev->dev, sizeof(struct gpio_desc *),
257                                        GFP_KERNEL);
258         if (!drvdata->gpiods)
259                 return -ENOMEM;
260         for (i = 0; i < config->ngpios; i++) {
261                 drvdata->gpiods[i] = devm_gpiod_get_index(&pdev->dev,
262                                                           NULL,
263                                                           i,
264                                                           config->gflags[i]);
265                 if (IS_ERR(drvdata->gpiods[i]))
266                         return PTR_ERR(drvdata->gpiods[i]);
267                 /* This is good to know */
268                 gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
269         }
270         drvdata->nr_gpios = config->ngpios;
271
272         drvdata->states = kmemdup(config->states,
273                                   config->nr_states *
274                                          sizeof(struct gpio_regulator_state),
275                                   GFP_KERNEL);
276         if (drvdata->states == NULL) {
277                 dev_err(&pdev->dev, "Failed to allocate state data\n");
278                 ret = -ENOMEM;
279                 goto err_name;
280         }
281         drvdata->nr_states = config->nr_states;
282
283         drvdata->desc.owner = THIS_MODULE;
284         drvdata->desc.enable_time = config->startup_delay;
285
286         /* handle regulator type*/
287         switch (config->type) {
288         case REGULATOR_VOLTAGE:
289                 drvdata->desc.type = REGULATOR_VOLTAGE;
290                 drvdata->desc.ops = &gpio_regulator_voltage_ops;
291                 drvdata->desc.n_voltages = config->nr_states;
292                 break;
293         case REGULATOR_CURRENT:
294                 drvdata->desc.type = REGULATOR_CURRENT;
295                 drvdata->desc.ops = &gpio_regulator_current_ops;
296                 break;
297         default:
298                 dev_err(&pdev->dev, "No regulator type set\n");
299                 ret = -EINVAL;
300                 goto err_memstate;
301         }
302
303         /* build initial state from gpio init data. */
304         state = 0;
305         for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
306                 if (config->gflags[ptr] == GPIOD_OUT_HIGH)
307                         state |= (1 << ptr);
308         }
309         drvdata->state = state;
310
311         cfg.dev = &pdev->dev;
312         cfg.init_data = config->init_data;
313         cfg.driver_data = drvdata;
314         cfg.of_node = np;
315
316         /*
317          * The signal will be inverted by the GPIO core if flagged so in the
318          * decriptor.
319          */
320         if (config->enabled_at_boot)
321                 gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
322         else
323                 gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
324
325         cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, "enable", gflags);
326         if (IS_ERR(cfg.ena_gpiod)) {
327                 ret = PTR_ERR(cfg.ena_gpiod);
328                 goto err_memstate;
329         }
330
331         drvdata->dev = regulator_register(&drvdata->desc, &cfg);
332         if (IS_ERR(drvdata->dev)) {
333                 ret = PTR_ERR(drvdata->dev);
334                 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
335                 goto err_memstate;
336         }
337
338         platform_set_drvdata(pdev, drvdata);
339
340         return 0;
341
342 err_memstate:
343         kfree(drvdata->states);
344 err_name:
345         kfree(drvdata->desc.name);
346         return ret;
347 }
348
349 static int gpio_regulator_remove(struct platform_device *pdev)
350 {
351         struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
352
353         regulator_unregister(drvdata->dev);
354         kfree(drvdata->states);
355         kfree(drvdata->desc.name);
356
357         return 0;
358 }
359
360 #if defined(CONFIG_OF)
361 static const struct of_device_id regulator_gpio_of_match[] = {
362         { .compatible = "regulator-gpio", },
363         {},
364 };
365 MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
366 #endif
367
368 static struct platform_driver gpio_regulator_driver = {
369         .probe          = gpio_regulator_probe,
370         .remove         = gpio_regulator_remove,
371         .driver         = {
372                 .name           = "gpio-regulator",
373                 .of_match_table = of_match_ptr(regulator_gpio_of_match),
374         },
375 };
376
377 static int __init gpio_regulator_init(void)
378 {
379         return platform_driver_register(&gpio_regulator_driver);
380 }
381 subsys_initcall(gpio_regulator_init);
382
383 static void __exit gpio_regulator_exit(void)
384 {
385         platform_driver_unregister(&gpio_regulator_driver);
386 }
387 module_exit(gpio_regulator_exit);
388
389 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
390 MODULE_DESCRIPTION("gpio voltage regulator");
391 MODULE_LICENSE("GPL");
392 MODULE_ALIAS("platform:gpio-regulator");