blob: 8b5944f2d7d1ddd36e82e92d786770e469743dfe [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
Heiko Stübner3f0292a2011-10-05 12:27:05 +020039 struct gpio *gpios;
40 int nr_gpios;
41
42 struct gpio_regulator_state *states;
43 int nr_states;
44
45 int state;
46};
47
Heiko Stübner3f0292a2011-10-05 12:27:05 +020048static 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
Heiko Stübnereb0c5682012-08-08 00:50:19 +020060static int gpio_regulator_set_voltage(struct regulator_dev *dev,
61 int min_uV, int max_uV,
62 unsigned *selector)
Heiko Stübner3f0292a2011-10-05 12:27:05 +020063{
64 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
Heiko Stübner00926362012-06-03 21:31:09 +020065 int ptr, target = 0, state, best_val = INT_MAX;
Heiko Stübner3f0292a2011-10-05 12:27:05 +020066
Heiko Stübner3f0292a2011-10-05 12:27:05 +020067 for (ptr = 0; ptr < data->nr_states; ptr++)
Axel Lin4dbd8f62012-03-22 14:08:04 +080068 if (data->states[ptr].value < best_val &&
Heiko Stübnereb0c5682012-08-08 00:50:19 +020069 data->states[ptr].value >= min_uV &&
70 data->states[ptr].value <= max_uV) {
Heiko Stübner3f0292a2011-10-05 12:27:05 +020071 target = data->states[ptr].gpios;
Heiko Stübner00926362012-06-03 21:31:09 +020072 best_val = data->states[ptr].value;
Heiko Stübnerb0e4d7b2012-06-03 21:32:05 +020073 if (selector)
74 *selector = ptr;
Heiko Stübner00926362012-06-03 21:31:09 +020075 }
Heiko Stübner3f0292a2011-10-05 12:27:05 +020076
Axel Lin4dbd8f62012-03-22 14:08:04 +080077 if (best_val == INT_MAX)
Heiko Stübner3f0292a2011-10-05 12:27:05 +020078 return -EINVAL;
79
80 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
81 state = (target & (1 << ptr)) >> ptr;
82 gpio_set_value(data->gpios[ptr].gpio, state);
83 }
84 data->state = target;
85
86 return 0;
87}
88
Heiko Stübner3f0292a2011-10-05 12:27:05 +020089static int gpio_regulator_list_voltage(struct regulator_dev *dev,
90 unsigned selector)
91{
92 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
93
94 if (selector >= data->nr_states)
95 return -EINVAL;
96
97 return data->states[selector].value;
98}
99
100static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
101 int min_uA, int max_uA)
102{
Heiko Stübnereb0c5682012-08-08 00:50:19 +0200103 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
104 int ptr, target = 0, state, best_val = 0;
105
106 for (ptr = 0; ptr < data->nr_states; ptr++)
107 if (data->states[ptr].value > best_val &&
108 data->states[ptr].value >= min_uA &&
109 data->states[ptr].value <= max_uA) {
110 target = data->states[ptr].gpios;
111 best_val = data->states[ptr].value;
112 }
113
114 if (best_val == 0)
115 return -EINVAL;
116
117 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
118 state = (target & (1 << ptr)) >> ptr;
119 gpio_set_value(data->gpios[ptr].gpio, state);
120 }
121 data->state = target;
122
123 return 0;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200124}
125
126static struct regulator_ops gpio_regulator_voltage_ops = {
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200127 .get_voltage = gpio_regulator_get_value,
128 .set_voltage = gpio_regulator_set_voltage,
129 .list_voltage = gpio_regulator_list_voltage,
130};
131
132static struct regulator_ops gpio_regulator_current_ops = {
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200133 .get_current_limit = gpio_regulator_get_value,
134 .set_current_limit = gpio_regulator_set_current_limit,
135};
136
137static int __devinit gpio_regulator_probe(struct platform_device *pdev)
138{
139 struct gpio_regulator_config *config = pdev->dev.platform_data;
140 struct gpio_regulator_data *drvdata;
Mark Brownc1727082012-04-04 00:50:22 +0100141 struct regulator_config cfg = { };
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200142 int ptr, ret, state;
143
Mark Brown02b552162012-04-03 23:20:56 +0100144 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
145 GFP_KERNEL);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200146 if (drvdata == NULL) {
147 dev_err(&pdev->dev, "Failed to allocate device data\n");
148 return -ENOMEM;
149 }
150
151 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
152 if (drvdata->desc.name == NULL) {
153 dev_err(&pdev->dev, "Failed to allocate supply name\n");
154 ret = -ENOMEM;
155 goto err;
156 }
157
158 drvdata->gpios = kmemdup(config->gpios,
159 config->nr_gpios * sizeof(struct gpio),
160 GFP_KERNEL);
161 if (drvdata->gpios == NULL) {
162 dev_err(&pdev->dev, "Failed to allocate gpio data\n");
163 ret = -ENOMEM;
164 goto err_name;
165 }
166
167 drvdata->states = kmemdup(config->states,
168 config->nr_states *
169 sizeof(struct gpio_regulator_state),
170 GFP_KERNEL);
171 if (drvdata->states == NULL) {
172 dev_err(&pdev->dev, "Failed to allocate state data\n");
173 ret = -ENOMEM;
174 goto err_memgpio;
175 }
176 drvdata->nr_states = config->nr_states;
177
178 drvdata->desc.owner = THIS_MODULE;
Axel Lina2a82222012-07-04 10:19:46 +0800179 drvdata->desc.enable_time = config->startup_delay;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200180
181 /* handle regulator type*/
182 switch (config->type) {
183 case REGULATOR_VOLTAGE:
184 drvdata->desc.type = REGULATOR_VOLTAGE;
185 drvdata->desc.ops = &gpio_regulator_voltage_ops;
186 drvdata->desc.n_voltages = config->nr_states;
187 break;
188 case REGULATOR_CURRENT:
189 drvdata->desc.type = REGULATOR_CURRENT;
190 drvdata->desc.ops = &gpio_regulator_current_ops;
191 break;
192 default:
193 dev_err(&pdev->dev, "No regulator type set\n");
194 ret = -EINVAL;
195 goto err_memgpio;
196 break;
197 }
198
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200199 drvdata->nr_gpios = config->nr_gpios;
200 ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
201 if (ret) {
202 dev_err(&pdev->dev,
203 "Could not obtain regulator setting GPIOs: %d\n", ret);
Axel Lin4b7c9482012-07-04 10:20:46 +0800204 goto err_memstate;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200205 }
206
207 /* build initial state from gpio init data. */
208 state = 0;
209 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
210 if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
211 state |= (1 << ptr);
212 }
213 drvdata->state = state;
214
Mark Brownc1727082012-04-04 00:50:22 +0100215 cfg.dev = &pdev->dev;
216 cfg.init_data = config->init_data;
Heiko Stübner7d4be2f2012-06-03 21:30:33 +0200217 cfg.driver_data = drvdata;
Mark Brownc1727082012-04-04 00:50:22 +0100218
Axel Lin4b7c9482012-07-04 10:20:46 +0800219 if (config->enable_gpio >= 0)
220 cfg.ena_gpio = config->enable_gpio;
221 cfg.ena_gpio_invert = !config->enable_high;
222 if (config->enabled_at_boot) {
223 if (config->enable_high)
224 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
225 else
226 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
227 } else {
228 if (config->enable_high)
229 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
230 else
231 cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
232 }
233
Mark Brownc1727082012-04-04 00:50:22 +0100234 drvdata->dev = regulator_register(&drvdata->desc, &cfg);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200235 if (IS_ERR(drvdata->dev)) {
236 ret = PTR_ERR(drvdata->dev);
237 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
238 goto err_stategpio;
239 }
240
241 platform_set_drvdata(pdev, drvdata);
242
243 return 0;
244
245err_stategpio:
246 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200247err_memstate:
248 kfree(drvdata->states);
249err_memgpio:
250 kfree(drvdata->gpios);
251err_name:
252 kfree(drvdata->desc.name);
253err:
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200254 return ret;
255}
256
257static int __devexit gpio_regulator_remove(struct platform_device *pdev)
258{
259 struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
260
261 regulator_unregister(drvdata->dev);
262
263 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
264
265 kfree(drvdata->states);
266 kfree(drvdata->gpios);
267
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200268 kfree(drvdata->desc.name);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200269
270 return 0;
271}
272
273static struct platform_driver gpio_regulator_driver = {
274 .probe = gpio_regulator_probe,
275 .remove = __devexit_p(gpio_regulator_remove),
276 .driver = {
277 .name = "gpio-regulator",
278 .owner = THIS_MODULE,
279 },
280};
281
282static int __init gpio_regulator_init(void)
283{
284 return platform_driver_register(&gpio_regulator_driver);
285}
286subsys_initcall(gpio_regulator_init);
287
288static void __exit gpio_regulator_exit(void)
289{
290 platform_driver_unregister(&gpio_regulator_driver);
291}
292module_exit(gpio_regulator_exit);
293
294MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
295MODULE_DESCRIPTION("gpio voltage regulator");
296MODULE_LICENSE("GPL");
297MODULE_ALIAS("platform:gpio-regulator");