Merge tag 'cpumask-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty...
[deliverable/linux.git] / drivers / regulator / gpio-regulator.c
CommitLineData
3f0292ae
HS
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>
ecc37edf 27#include <linux/module.h>
3f0292ae
HS
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>
3f0292ae
HS
33#include <linux/slab.h>
34
35struct gpio_regulator_data {
36 struct regulator_desc desc;
37 struct regulator_dev *dev;
38
3f0292ae
HS
39 struct gpio *gpios;
40 int nr_gpios;
41
42 struct gpio_regulator_state *states;
43 int nr_states;
44
45 int state;
46};
47
3f0292ae
HS
48static int gpio_regulator_get_value(struct regulator_dev *dev)
49{
50 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
51 int ptr;
52
53 for (ptr = 0; ptr < data->nr_states; ptr++)
54 if (data->states[ptr].gpios == data->state)
55 return data->states[ptr].value;
56
57 return -EINVAL;
58}
59
60static int gpio_regulator_set_value(struct regulator_dev *dev,
b0e4d7bf 61 int min, int max, unsigned *selector)
3f0292ae
HS
62{
63 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
00926369 64 int ptr, target = 0, state, best_val = INT_MAX;
3f0292ae 65
3f0292ae 66 for (ptr = 0; ptr < data->nr_states; ptr++)
4dbd8f63
AL
67 if (data->states[ptr].value < best_val &&
68 data->states[ptr].value >= min &&
00926369 69 data->states[ptr].value <= max) {
3f0292ae 70 target = data->states[ptr].gpios;
00926369 71 best_val = data->states[ptr].value;
b0e4d7bf
HS
72 if (selector)
73 *selector = ptr;
00926369 74 }
3f0292ae 75
4dbd8f63 76 if (best_val == INT_MAX)
3f0292ae
HS
77 return -EINVAL;
78
79 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
80 state = (target & (1 << ptr)) >> ptr;
81 gpio_set_value(data->gpios[ptr].gpio, state);
82 }
83 data->state = target;
84
85 return 0;
86}
87
88static int gpio_regulator_set_voltage(struct regulator_dev *dev,
89 int min_uV, int max_uV,
90 unsigned *selector)
91{
b0e4d7bf 92 return gpio_regulator_set_value(dev, min_uV, max_uV, selector);
3f0292ae
HS
93}
94
95static int gpio_regulator_list_voltage(struct regulator_dev *dev,
96 unsigned selector)
97{
98 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
99
100 if (selector >= data->nr_states)
101 return -EINVAL;
102
103 return data->states[selector].value;
104}
105
106static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
107 int min_uA, int max_uA)
108{
b0e4d7bf 109 return gpio_regulator_set_value(dev, min_uA, max_uA, NULL);
3f0292ae
HS
110}
111
112static struct regulator_ops gpio_regulator_voltage_ops = {
3f0292ae
HS
113 .get_voltage = gpio_regulator_get_value,
114 .set_voltage = gpio_regulator_set_voltage,
115 .list_voltage = gpio_regulator_list_voltage,
116};
117
118static struct regulator_ops gpio_regulator_current_ops = {
3f0292ae
HS
119 .get_current_limit = gpio_regulator_get_value,
120 .set_current_limit = gpio_regulator_set_current_limit,
121};
122
123static int __devinit gpio_regulator_probe(struct platform_device *pdev)
124{
125 struct gpio_regulator_config *config = pdev->dev.platform_data;
126 struct gpio_regulator_data *drvdata;
c172708d 127 struct regulator_config cfg = { };
3f0292ae
HS
128 int ptr, ret, state;
129
02b55216
MB
130 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
131 GFP_KERNEL);
3f0292ae
HS
132 if (drvdata == NULL) {
133 dev_err(&pdev->dev, "Failed to allocate device data\n");
134 return -ENOMEM;
135 }
136
137 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
138 if (drvdata->desc.name == NULL) {
139 dev_err(&pdev->dev, "Failed to allocate supply name\n");
140 ret = -ENOMEM;
141 goto err;
142 }
143
144 drvdata->gpios = kmemdup(config->gpios,
145 config->nr_gpios * sizeof(struct gpio),
146 GFP_KERNEL);
147 if (drvdata->gpios == NULL) {
148 dev_err(&pdev->dev, "Failed to allocate gpio data\n");
149 ret = -ENOMEM;
150 goto err_name;
151 }
152
153 drvdata->states = kmemdup(config->states,
154 config->nr_states *
155 sizeof(struct gpio_regulator_state),
156 GFP_KERNEL);
157 if (drvdata->states == NULL) {
158 dev_err(&pdev->dev, "Failed to allocate state data\n");
159 ret = -ENOMEM;
160 goto err_memgpio;
161 }
162 drvdata->nr_states = config->nr_states;
163
164 drvdata->desc.owner = THIS_MODULE;
a2a8222b 165 drvdata->desc.enable_time = config->startup_delay;
3f0292ae
HS
166
167 /* handle regulator type*/
168 switch (config->type) {
169 case REGULATOR_VOLTAGE:
170 drvdata->desc.type = REGULATOR_VOLTAGE;
171 drvdata->desc.ops = &gpio_regulator_voltage_ops;
172 drvdata->desc.n_voltages = config->nr_states;
173 break;
174 case REGULATOR_CURRENT:
175 drvdata->desc.type = REGULATOR_CURRENT;
176 drvdata->desc.ops = &gpio_regulator_current_ops;
177 break;
178 default:
179 dev_err(&pdev->dev, "No regulator type set\n");
180 ret = -EINVAL;
181 goto err_memgpio;
182 break;
183 }
184
3f0292ae
HS
185 drvdata->nr_gpios = config->nr_gpios;
186 ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
187 if (ret) {
188 dev_err(&pdev->dev,
189 "Could not obtain regulator setting GPIOs: %d\n", ret);
4b7c948f 190 goto err_memstate;
3f0292ae
HS
191 }
192
193 /* build initial state from gpio init data. */
194 state = 0;
195 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
196 if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
197 state |= (1 << ptr);
198 }
199 drvdata->state = state;
200
c172708d
MB
201 cfg.dev = &pdev->dev;
202 cfg.init_data = config->init_data;
7d4be2f5 203 cfg.driver_data = drvdata;
c172708d 204
4b7c948f
AL
205 if (config->enable_gpio >= 0)
206 cfg.ena_gpio = config->enable_gpio;
207 cfg.ena_gpio_invert = !config->enable_high;
208 if (config->enabled_at_boot) {
209 if (config->enable_high)
210 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
211 else
212 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
213 } else {
214 if (config->enable_high)
215 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
216 else
217 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
218 }
219
c172708d 220 drvdata->dev = regulator_register(&drvdata->desc, &cfg);
3f0292ae
HS
221 if (IS_ERR(drvdata->dev)) {
222 ret = PTR_ERR(drvdata->dev);
223 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
224 goto err_stategpio;
225 }
226
227 platform_set_drvdata(pdev, drvdata);
228
229 return 0;
230
231err_stategpio:
232 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
3f0292ae
HS
233err_memstate:
234 kfree(drvdata->states);
235err_memgpio:
236 kfree(drvdata->gpios);
237err_name:
238 kfree(drvdata->desc.name);
239err:
3f0292ae
HS
240 return ret;
241}
242
243static int __devexit gpio_regulator_remove(struct platform_device *pdev)
244{
245 struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
246
247 regulator_unregister(drvdata->dev);
248
249 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
250
251 kfree(drvdata->states);
252 kfree(drvdata->gpios);
253
3f0292ae 254 kfree(drvdata->desc.name);
3f0292ae
HS
255
256 return 0;
257}
258
259static struct platform_driver gpio_regulator_driver = {
260 .probe = gpio_regulator_probe,
261 .remove = __devexit_p(gpio_regulator_remove),
262 .driver = {
263 .name = "gpio-regulator",
264 .owner = THIS_MODULE,
265 },
266};
267
268static int __init gpio_regulator_init(void)
269{
270 return platform_driver_register(&gpio_regulator_driver);
271}
272subsys_initcall(gpio_regulator_init);
273
274static void __exit gpio_regulator_exit(void)
275{
276 platform_driver_unregister(&gpio_regulator_driver);
277}
278module_exit(gpio_regulator_exit);
279
280MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
281MODULE_DESCRIPTION("gpio voltage regulator");
282MODULE_LICENSE("GPL");
283MODULE_ALIAS("platform:gpio-regulator");
This page took 0.074396 seconds and 5 git commands to generate.