4 * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
8 * Copyright 2008 Wolfson Microelectronics PLC.
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * Copyright (c) 2009 Nokia Corporation
13 * Roger Quadros <ext-roger.quadros@nokia.com>
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.
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.
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>
35 struct gpio_regulator_data {
36 struct regulator_desc desc;
37 struct regulator_dev *dev;
42 unsigned startup_delay;
47 struct gpio_regulator_state *states;
53 static int gpio_regulator_is_enabled(struct regulator_dev *dev)
55 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
57 return data->is_enabled;
60 static int gpio_regulator_enable(struct regulator_dev *dev)
62 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
64 if (gpio_is_valid(data->enable_gpio)) {
65 gpio_set_value_cansleep(data->enable_gpio, data->enable_high);
66 data->is_enabled = true;
72 static int gpio_regulator_disable(struct regulator_dev *dev)
74 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
76 if (gpio_is_valid(data->enable_gpio)) {
77 gpio_set_value_cansleep(data->enable_gpio, !data->enable_high);
78 data->is_enabled = false;
84 static int gpio_regulator_enable_time(struct regulator_dev *dev)
86 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
88 return data->startup_delay;
91 static int gpio_regulator_get_value(struct regulator_dev *dev)
93 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
96 for (ptr = 0; ptr < data->nr_states; ptr++)
97 if (data->states[ptr].gpios == data->state)
98 return data->states[ptr].value;
103 static int gpio_regulator_set_value(struct regulator_dev *dev,
104 int min, int max, unsigned *selector)
106 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
107 int ptr, target = 0, state, best_val = INT_MAX;
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 best_val = data->states[ptr].value;
119 if (best_val == INT_MAX)
122 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
123 state = (target & (1 << ptr)) >> ptr;
124 gpio_set_value(data->gpios[ptr].gpio, state);
126 data->state = target;
131 static int gpio_regulator_set_voltage(struct regulator_dev *dev,
132 int min_uV, int max_uV,
135 return gpio_regulator_set_value(dev, min_uV, max_uV, selector);
138 static int gpio_regulator_list_voltage(struct regulator_dev *dev,
141 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
143 if (selector >= data->nr_states)
146 return data->states[selector].value;
149 static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
150 int min_uA, int max_uA)
152 return gpio_regulator_set_value(dev, min_uA, max_uA, NULL);
155 static struct regulator_ops gpio_regulator_voltage_ops = {
156 .is_enabled = gpio_regulator_is_enabled,
157 .enable = gpio_regulator_enable,
158 .disable = gpio_regulator_disable,
159 .enable_time = gpio_regulator_enable_time,
160 .get_voltage = gpio_regulator_get_value,
161 .set_voltage = gpio_regulator_set_voltage,
162 .list_voltage = gpio_regulator_list_voltage,
165 static struct regulator_ops gpio_regulator_current_ops = {
166 .is_enabled = gpio_regulator_is_enabled,
167 .enable = gpio_regulator_enable,
168 .disable = gpio_regulator_disable,
169 .enable_time = gpio_regulator_enable_time,
170 .get_current_limit = gpio_regulator_get_value,
171 .set_current_limit = gpio_regulator_set_current_limit,
174 static int __devinit gpio_regulator_probe(struct platform_device *pdev)
176 struct gpio_regulator_config *config = pdev->dev.platform_data;
177 struct gpio_regulator_data *drvdata;
178 struct regulator_config cfg = { };
181 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
183 if (drvdata == NULL) {
184 dev_err(&pdev->dev, "Failed to allocate device data\n");
188 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
189 if (drvdata->desc.name == NULL) {
190 dev_err(&pdev->dev, "Failed to allocate supply name\n");
195 drvdata->gpios = kmemdup(config->gpios,
196 config->nr_gpios * sizeof(struct gpio),
198 if (drvdata->gpios == NULL) {
199 dev_err(&pdev->dev, "Failed to allocate gpio data\n");
204 drvdata->states = kmemdup(config->states,
206 sizeof(struct gpio_regulator_state),
208 if (drvdata->states == NULL) {
209 dev_err(&pdev->dev, "Failed to allocate state data\n");
213 drvdata->nr_states = config->nr_states;
215 drvdata->desc.owner = THIS_MODULE;
217 /* handle regulator type*/
218 switch (config->type) {
219 case REGULATOR_VOLTAGE:
220 drvdata->desc.type = REGULATOR_VOLTAGE;
221 drvdata->desc.ops = &gpio_regulator_voltage_ops;
222 drvdata->desc.n_voltages = config->nr_states;
224 case REGULATOR_CURRENT:
225 drvdata->desc.type = REGULATOR_CURRENT;
226 drvdata->desc.ops = &gpio_regulator_current_ops;
229 dev_err(&pdev->dev, "No regulator type set\n");
235 drvdata->enable_gpio = config->enable_gpio;
236 drvdata->startup_delay = config->startup_delay;
238 if (gpio_is_valid(config->enable_gpio)) {
239 drvdata->enable_high = config->enable_high;
241 ret = gpio_request(config->enable_gpio, config->supply_name);
244 "Could not obtain regulator enable GPIO %d: %d\n",
245 config->enable_gpio, ret);
249 /* set output direction without changing state
252 if (config->enabled_at_boot) {
253 drvdata->is_enabled = true;
254 ret = gpio_direction_output(config->enable_gpio,
255 config->enable_high);
257 drvdata->is_enabled = false;
258 ret = gpio_direction_output(config->enable_gpio,
259 !config->enable_high);
264 "Could not configure regulator enable GPIO %d direction: %d\n",
265 config->enable_gpio, ret);
269 /* Regulator without GPIO control is considered
272 drvdata->is_enabled = true;
275 drvdata->nr_gpios = config->nr_gpios;
276 ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
279 "Could not obtain regulator setting GPIOs: %d\n", ret);
283 /* build initial state from gpio init data. */
285 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
286 if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
289 drvdata->state = state;
291 cfg.dev = &pdev->dev;
292 cfg.init_data = config->init_data;
293 cfg.driver_data = drvdata;
295 drvdata->dev = regulator_register(&drvdata->desc, &cfg);
296 if (IS_ERR(drvdata->dev)) {
297 ret = PTR_ERR(drvdata->dev);
298 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
302 platform_set_drvdata(pdev, drvdata);
307 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
309 if (gpio_is_valid(config->enable_gpio))
310 gpio_free(config->enable_gpio);
312 kfree(drvdata->states);
314 kfree(drvdata->gpios);
316 kfree(drvdata->desc.name);
321 static int __devexit gpio_regulator_remove(struct platform_device *pdev)
323 struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
325 regulator_unregister(drvdata->dev);
327 gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
329 kfree(drvdata->states);
330 kfree(drvdata->gpios);
332 if (gpio_is_valid(drvdata->enable_gpio))
333 gpio_free(drvdata->enable_gpio);
335 kfree(drvdata->desc.name);
340 static struct platform_driver gpio_regulator_driver = {
341 .probe = gpio_regulator_probe,
342 .remove = __devexit_p(gpio_regulator_remove),
344 .name = "gpio-regulator",
345 .owner = THIS_MODULE,
349 static int __init gpio_regulator_init(void)
351 return platform_driver_register(&gpio_regulator_driver);
353 subsys_initcall(gpio_regulator_init);
355 static void __exit gpio_regulator_exit(void)
357 platform_driver_unregister(&gpio_regulator_driver);
359 module_exit(gpio_regulator_exit);
361 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
362 MODULE_DESCRIPTION("gpio voltage regulator");
363 MODULE_LICENSE("GPL");
364 MODULE_ALIAS("platform:gpio-regulator");