8 * Copyright 2008 Wolfson Microelectronics PLC.
12 * Copyright (c) 2009 Nokia Corporation
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/of_regulator.h>
32 #include <linux/regulator/gpio-regulator.h>
33 #include <linux/gpio/consumer.h>
34 #include <linux/slab.h>
37 struct gpio_regulator_data {
38 struct regulator_desc desc;
40 struct gpio_desc **gpiods;
43 struct gpio_regulator_state *states;
49 static int gpio_regulator_get_value(struct regulator_dev *dev)
51 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
54 for (ptr = 0; ptr < data->nr_states; ptr++)
55 if (data->states[ptr].gpios == data->state)
56 return data->states[ptr].value;
61 static int gpio_regulator_set_voltage(struct regulator_dev *dev,
62 int min_uV, int max_uV,
65 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
66 int ptr, target = 0, state, best_val = INT_MAX;
68 for (ptr = 0; ptr < data->nr_states; ptr++)
69 if (data->states[ptr].value < best_val &&
70 data->states[ptr].value >= min_uV &&
71 data->states[ptr].value <= max_uV) {
72 target = data->states[ptr].gpios;
73 best_val = data->states[ptr].value;
78 if (best_val == INT_MAX)
81 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
82 state = (target & (1 << ptr)) >> ptr;
83 gpiod_set_value_cansleep(data->gpiods[ptr], state);
90 static int gpio_regulator_list_voltage(struct regulator_dev *dev,
93 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
95 if (selector >= data->nr_states)
98 return data->states[selector].value;
101 static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
102 int min_uA, int max_uA)
104 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
105 int ptr, target = 0, state, best_val = 0;
107 for (ptr = 0; ptr < data->nr_states; ptr++)
108 if (data->states[ptr].value > best_val &&
109 data->states[ptr].value >= min_uA &&
110 data->states[ptr].value <= max_uA) {
111 target = data->states[ptr].gpios;
112 best_val = data->states[ptr].value;
118 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
119 state = (target & (1 << ptr)) >> ptr;
120 gpiod_set_value_cansleep(data->gpiods[ptr], state);
122 data->state = target;
127 static const struct regulator_ops gpio_regulator_voltage_ops = {
128 .get_voltage = gpio_regulator_get_value,
129 .set_voltage = gpio_regulator_set_voltage,
130 .list_voltage = gpio_regulator_list_voltage,
133 static struct gpio_regulator_config *
134 of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
135 const struct regulator_desc *desc)
137 struct gpio_regulator_config *config;
143 config = devm_kzalloc(dev,
144 sizeof(struct gpio_regulator_config),
147 return ERR_PTR(-ENOMEM);
149 config->init_data = of_get_regulator_init_data(dev, np, desc);
150 if (!config->init_data)
151 return ERR_PTR(-EINVAL);
153 config->supply_name = config->init_data->constraints.name;
155 if (of_property_read_bool(np, "enable-at-boot"))
156 config->enabled_at_boot = true;
158 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
160 /* Fetch GPIO init levels */
161 ngpios = gpiod_count(dev, NULL);
163 config->gflags = devm_kzalloc(dev,
164 sizeof(enum gpiod_flags)
168 return ERR_PTR(-ENOMEM);
170 for (i = 0; i < ngpios; i++) {
173 ret = of_property_read_u32_index(np, "gpios-states", i,
176 /* Default to high per specification */
178 config->gflags[i] = GPIOD_OUT_HIGH;
181 val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
184 config->ngpios = ngpios;
187 proplen = of_property_count_u32_elems(np, "states");
189 dev_err(dev, "No 'states' property found\n");
190 return ERR_PTR(-EINVAL);
193 config->states = devm_kcalloc(dev,
195 sizeof(struct gpio_regulator_state),
198 return ERR_PTR(-ENOMEM);
200 for (i = 0; i < proplen / 2; i++) {
201 of_property_read_u32_index(np, "states", i * 2,
202 &config->states[i].value);
203 of_property_read_u32_index(np, "states", i * 2 + 1,
204 &config->states[i].gpios);
206 config->nr_states = i;
208 config->type = REGULATOR_VOLTAGE;
209 ret = of_property_read_string(np, "regulator-type", ®type);
211 if (!strncmp("voltage", regtype, 7))
212 config->type = REGULATOR_VOLTAGE;
213 else if (!strncmp("current", regtype, 7))
214 config->type = REGULATOR_CURRENT;
216 dev_warn(dev, "Unknown regulator-type '%s'\n",
223 static const struct regulator_ops gpio_regulator_current_ops = {
224 .get_current_limit = gpio_regulator_get_value,
225 .set_current_limit = gpio_regulator_set_current_limit,
228 static int gpio_regulator_probe(struct platform_device *pdev)
230 struct device *dev = &pdev->dev;
231 struct gpio_regulator_config *config = dev_get_platdata(dev);
232 struct device_node *np = dev->of_node;
233 struct gpio_regulator_data *drvdata;
234 struct regulator_config cfg = { };
235 struct regulator_dev *rdev;
236 enum gpiod_flags gflags;
237 int ptr, ret, state, i;
239 drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
245 config = of_get_gpio_regulator_config(dev, np,
248 return PTR_ERR(config);
251 drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
252 if (drvdata->desc.name == NULL) {
253 dev_err(dev, "Failed to allocate supply name\n");
257 drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
259 if (!drvdata->gpiods)
261 for (i = 0; i < config->ngpios; i++) {
262 drvdata->gpiods[i] = devm_gpiod_get_index(dev,
266 if (IS_ERR(drvdata->gpiods[i]))
267 return PTR_ERR(drvdata->gpiods[i]);
268 /* This is good to know */
269 gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
271 drvdata->nr_gpios = config->ngpios;
273 drvdata->states = devm_kmemdup(dev,
276 sizeof(struct gpio_regulator_state),
278 if (drvdata->states == NULL) {
279 dev_err(dev, "Failed to allocate state data\n");
282 drvdata->nr_states = config->nr_states;
284 drvdata->desc.owner = THIS_MODULE;
285 drvdata->desc.enable_time = config->startup_delay;
287 /* handle regulator type*/
288 switch (config->type) {
289 case REGULATOR_VOLTAGE:
290 drvdata->desc.type = REGULATOR_VOLTAGE;
291 drvdata->desc.ops = &gpio_regulator_voltage_ops;
292 drvdata->desc.n_voltages = config->nr_states;
294 case REGULATOR_CURRENT:
295 drvdata->desc.type = REGULATOR_CURRENT;
296 drvdata->desc.ops = &gpio_regulator_current_ops;
299 dev_err(dev, "No regulator type set\n");
303 /* build initial state from gpio init data. */
305 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
306 if (config->gflags[ptr] == GPIOD_OUT_HIGH)
309 drvdata->state = state;
312 cfg.init_data = config->init_data;
313 cfg.driver_data = drvdata;
317 * The signal will be inverted by the GPIO core if flagged so in the
320 if (config->enabled_at_boot)
321 gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
323 gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
325 cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
326 if (IS_ERR(cfg.ena_gpiod))
327 return PTR_ERR(cfg.ena_gpiod);
329 rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
332 dev_err(dev, "Failed to register regulator: %d\n", ret);
336 platform_set_drvdata(pdev, drvdata);
341 #if defined(CONFIG_OF)
342 static const struct of_device_id regulator_gpio_of_match[] = {
343 { .compatible = "regulator-gpio", },
346 MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
349 static struct platform_driver gpio_regulator_driver = {
350 .probe = gpio_regulator_probe,
352 .name = "gpio-regulator",
353 .of_match_table = of_match_ptr(regulator_gpio_of_match),
357 static int __init gpio_regulator_init(void)
359 return platform_driver_register(&gpio_regulator_driver);
361 subsys_initcall(gpio_regulator_init);
363 static void __exit gpio_regulator_exit(void)
365 platform_driver_unregister(&gpio_regulator_driver);
367 module_exit(gpio_regulator_exit);
370 MODULE_DESCRIPTION("gpio voltage regulator");
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS("platform:gpio-regulator");