blob: f50d86a661382b9c07eb74101fbba016412297f9 [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>
Lee Jones006694d2012-10-15 14:16:59 +010031#include <linux/regulator/of_regulator.h>
Heiko Stübner3f0292a2011-10-05 12:27:05 +020032#include <linux/regulator/gpio-regulator.h>
Linus Walleijd6cd33a2019-01-29 11:31:52 +010033#include <linux/gpio/consumer.h>
Heiko Stübner3f0292a2011-10-05 12:27:05 +020034#include <linux/slab.h>
Lee Jones006694d2012-10-15 14:16:59 +010035#include <linux/of.h>
Heiko Stübner3f0292a2011-10-05 12:27:05 +020036
37struct gpio_regulator_data {
38 struct regulator_desc desc;
Heiko Stübner3f0292a2011-10-05 12:27:05 +020039
Linus Walleijd6cd33a2019-01-29 11:31:52 +010040 struct gpio_desc **gpiods;
Heiko Stübner3f0292a2011-10-05 12:27:05 +020041 int nr_gpios;
42
43 struct gpio_regulator_state *states;
44 int nr_states;
45
46 int state;
47};
48
Heiko Stübner3f0292a2011-10-05 12:27:05 +020049static int gpio_regulator_get_value(struct regulator_dev *dev)
50{
51 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
52 int ptr;
53
54 for (ptr = 0; ptr < data->nr_states; ptr++)
55 if (data->states[ptr].gpios == data->state)
56 return data->states[ptr].value;
57
58 return -EINVAL;
59}
60
Heiko Stübnereb0c5682012-08-08 00:50:19 +020061static int gpio_regulator_set_voltage(struct regulator_dev *dev,
62 int min_uV, int max_uV,
63 unsigned *selector)
Heiko Stübner3f0292a2011-10-05 12:27:05 +020064{
65 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
Heiko Stübner00926362012-06-03 21:31:09 +020066 int ptr, target = 0, state, best_val = INT_MAX;
Heiko Stübner3f0292a2011-10-05 12:27:05 +020067
Heiko Stübner3f0292a2011-10-05 12:27:05 +020068 for (ptr = 0; ptr < data->nr_states; ptr++)
Axel Lin4dbd8f62012-03-22 14:08:04 +080069 if (data->states[ptr].value < best_val &&
Heiko Stübnereb0c5682012-08-08 00:50:19 +020070 data->states[ptr].value >= min_uV &&
71 data->states[ptr].value <= max_uV) {
Heiko Stübner3f0292a2011-10-05 12:27:05 +020072 target = data->states[ptr].gpios;
Heiko Stübner00926362012-06-03 21:31:09 +020073 best_val = data->states[ptr].value;
Heiko Stübnerb0e4d7b2012-06-03 21:32:05 +020074 if (selector)
75 *selector = ptr;
Heiko Stübner00926362012-06-03 21:31:09 +020076 }
Heiko Stübner3f0292a2011-10-05 12:27:05 +020077
Axel Lin4dbd8f62012-03-22 14:08:04 +080078 if (best_val == INT_MAX)
Heiko Stübner3f0292a2011-10-05 12:27:05 +020079 return -EINVAL;
80
81 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
82 state = (target & (1 << ptr)) >> ptr;
Linus Walleijd6cd33a2019-01-29 11:31:52 +010083 gpiod_set_value_cansleep(data->gpiods[ptr], state);
Heiko Stübner3f0292a2011-10-05 12:27:05 +020084 }
85 data->state = target;
86
87 return 0;
88}
89
Heiko Stübner3f0292a2011-10-05 12:27:05 +020090static int gpio_regulator_list_voltage(struct regulator_dev *dev,
91 unsigned selector)
92{
93 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
94
95 if (selector >= data->nr_states)
96 return -EINVAL;
97
98 return data->states[selector].value;
99}
100
101static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
102 int min_uA, int max_uA)
103{
Heiko Stübnereb0c5682012-08-08 00:50:19 +0200104 struct gpio_regulator_data *data = rdev_get_drvdata(dev);
105 int ptr, target = 0, state, best_val = 0;
106
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;
113 }
114
115 if (best_val == 0)
116 return -EINVAL;
117
118 for (ptr = 0; ptr < data->nr_gpios; ptr++) {
119 state = (target & (1 << ptr)) >> ptr;
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100120 gpiod_set_value_cansleep(data->gpiods[ptr], state);
Heiko Stübnereb0c5682012-08-08 00:50:19 +0200121 }
122 data->state = target;
123
124 return 0;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200125}
126
Axel Lin705e2a92019-03-08 14:15:29 +0800127static const struct regulator_ops gpio_regulator_voltage_ops = {
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200128 .get_voltage = gpio_regulator_get_value,
129 .set_voltage = gpio_regulator_set_voltage,
130 .list_voltage = gpio_regulator_list_voltage,
131};
132
Axel Lina4514052013-01-28 22:17:46 +0800133static struct gpio_regulator_config *
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100134of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
135 const struct regulator_desc *desc)
Lee Jones006694d2012-10-15 14:16:59 +0100136{
137 struct gpio_regulator_config *config;
Lee Jones006694d2012-10-15 14:16:59 +0100138 const char *regtype;
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100139 int proplen, i;
140 int ngpios;
Laurent Pinchart251b9c22013-11-09 13:12:28 +0100141 int ret;
Lee Jones006694d2012-10-15 14:16:59 +0100142
143 config = devm_kzalloc(dev,
144 sizeof(struct gpio_regulator_config),
145 GFP_KERNEL);
146 if (!config)
147 return ERR_PTR(-ENOMEM);
148
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100149 config->init_data = of_get_regulator_init_data(dev, np, desc);
Lee Jones006694d2012-10-15 14:16:59 +0100150 if (!config->init_data)
151 return ERR_PTR(-EINVAL);
152
153 config->supply_name = config->init_data->constraints.name;
154
Lee Jones006694d2012-10-15 14:16:59 +0100155 if (of_property_read_bool(np, "enable-at-boot"))
156 config->enabled_at_boot = true;
157
158 of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
159
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100160 /* Fetch GPIO init levels */
161 ngpios = gpiod_count(dev, NULL);
162 if (ngpios > 0) {
163 config->gflags = devm_kzalloc(dev,
164 sizeof(enum gpiod_flags)
165 * ngpios,
166 GFP_KERNEL);
167 if (!config->gflags)
Richard Fitzgerald9f946092014-11-19 14:13:06 +0000168 return ERR_PTR(-ENOMEM);
Lee Jones006694d2012-10-15 14:16:59 +0100169
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100170 for (i = 0; i < ngpios; i++) {
171 u32 val;
Heiko Stuebner1f5a9622014-02-13 16:34:32 +0100172
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100173 ret = of_property_read_u32_index(np, "gpios-states", i,
174 &val);
Kuninori Morimoto00940502014-01-30 21:25:14 -0800175
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100176 /* Default to high per specification */
177 if (ret)
178 config->gflags[i] = GPIOD_OUT_HIGH;
179 else
180 config->gflags[i] =
181 val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
Heiko Stuebner1f5a9622014-02-13 16:34:32 +0100182 }
Lee Jones006694d2012-10-15 14:16:59 +0100183 }
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100184 config->ngpios = ngpios;
Lee Jones006694d2012-10-15 14:16:59 +0100185
186 /* Fetch states. */
Heiko Stuebner934624d2014-02-12 01:01:08 +0100187 proplen = of_property_count_u32_elems(np, "states");
188 if (proplen < 0) {
Lee Jones216f2b92012-11-14 11:51:36 +0000189 dev_err(dev, "No 'states' property found\n");
190 return ERR_PTR(-EINVAL);
191 }
192
Kees Cooka86854d2018-06-12 14:07:58 -0700193 config->states = devm_kcalloc(dev,
194 proplen / 2,
195 sizeof(struct gpio_regulator_state),
Lee Jones006694d2012-10-15 14:16:59 +0100196 GFP_KERNEL);
197 if (!config->states)
198 return ERR_PTR(-ENOMEM);
199
200 for (i = 0; i < proplen / 2; i++) {
Heiko Stuebner934624d2014-02-12 01:01:08 +0100201 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);
Lee Jones006694d2012-10-15 14:16:59 +0100205 }
206 config->nr_states = i;
207
Mark Brown5b1ada82013-12-05 00:29:57 +0000208 config->type = REGULATOR_VOLTAGE;
Laurent Pinchart251b9c22013-11-09 13:12:28 +0100209 ret = of_property_read_string(np, "regulator-type", &regtype);
Mark Brown5b1ada82013-12-05 00:29:57 +0000210 if (ret >= 0) {
211 if (!strncmp("voltage", regtype, 7))
212 config->type = REGULATOR_VOLTAGE;
213 else if (!strncmp("current", regtype, 7))
214 config->type = REGULATOR_CURRENT;
Mark Brown9eb9d312013-12-05 19:09:55 +0000215 else
216 dev_warn(dev, "Unknown regulator-type '%s'\n",
217 regtype);
Laurent Pinchart251b9c22013-11-09 13:12:28 +0100218 }
Lee Jones006694d2012-10-15 14:16:59 +0100219
Lee Jones006694d2012-10-15 14:16:59 +0100220 return config;
221}
222
Axel Lin705e2a92019-03-08 14:15:29 +0800223static const struct regulator_ops gpio_regulator_current_ops = {
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200224 .get_current_limit = gpio_regulator_get_value,
225 .set_current_limit = gpio_regulator_set_current_limit,
226};
227
Bill Pembertona5023572012-11-19 13:22:22 -0500228static int gpio_regulator_probe(struct platform_device *pdev)
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200229{
Linus Walleijd162d042019-01-29 11:31:55 +0100230 struct device *dev = &pdev->dev;
231 struct gpio_regulator_config *config = dev_get_platdata(dev);
232 struct device_node *np = dev->of_node;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200233 struct gpio_regulator_data *drvdata;
Mark Brownc172708d2012-04-04 00:50:22 +0100234 struct regulator_config cfg = { };
Axel Lin7cdc2ee72019-03-08 14:15:28 +0800235 struct regulator_dev *rdev;
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100236 enum gpiod_flags gflags;
237 int ptr, ret, state, i;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200238
Linus Walleijd162d042019-01-29 11:31:55 +0100239 drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
Mark Brown02b55212012-04-03 23:20:56 +0100240 GFP_KERNEL);
Fabio Estevam9c259602014-01-24 15:48:18 -0200241 if (drvdata == NULL)
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200242 return -ENOMEM;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200243
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100244 if (np) {
Linus Walleijd162d042019-01-29 11:31:55 +0100245 config = of_get_gpio_regulator_config(dev, np,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100246 &drvdata->desc);
247 if (IS_ERR(config))
248 return PTR_ERR(config);
249 }
250
Linus Walleijd162d042019-01-29 11:31:55 +0100251 drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200252 if (drvdata->desc.name == NULL) {
Linus Walleijd162d042019-01-29 11:31:55 +0100253 dev_err(dev, "Failed to allocate supply name\n");
Christophe Jailleted8cffd2018-03-13 21:33:11 +0100254 return -ENOMEM;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200255 }
256
Linus Walleijd162d042019-01-29 11:31:55 +0100257 drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100258 GFP_KERNEL);
259 if (!drvdata->gpiods)
260 return -ENOMEM;
261 for (i = 0; i < config->ngpios; i++) {
Linus Walleijd162d042019-01-29 11:31:55 +0100262 drvdata->gpiods[i] = devm_gpiod_get_index(dev,
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100263 NULL,
264 i,
265 config->gflags[i]);
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);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200270 }
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100271 drvdata->nr_gpios = config->ngpios;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200272
Linus Walleijd162d042019-01-29 11:31:55 +0100273 drvdata->states = devm_kmemdup(dev,
274 config->states,
275 config->nr_states *
276 sizeof(struct gpio_regulator_state),
277 GFP_KERNEL);
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200278 if (drvdata->states == NULL) {
Linus Walleijd162d042019-01-29 11:31:55 +0100279 dev_err(dev, "Failed to allocate state data\n");
280 return -ENOMEM;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200281 }
282 drvdata->nr_states = config->nr_states;
283
284 drvdata->desc.owner = THIS_MODULE;
Axel Lina2a82222012-07-04 10:19:46 +0800285 drvdata->desc.enable_time = config->startup_delay;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200286
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;
293 break;
294 case REGULATOR_CURRENT:
295 drvdata->desc.type = REGULATOR_CURRENT;
296 drvdata->desc.ops = &gpio_regulator_current_ops;
297 break;
298 default:
Linus Walleijd162d042019-01-29 11:31:55 +0100299 dev_err(dev, "No regulator type set\n");
300 return -EINVAL;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200301 }
302
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200303 /* build initial state from gpio init data. */
304 state = 0;
305 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100306 if (config->gflags[ptr] == GPIOD_OUT_HIGH)
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200307 state |= (1 << ptr);
308 }
309 drvdata->state = state;
310
Linus Walleijd162d042019-01-29 11:31:55 +0100311 cfg.dev = dev;
Mark Brownc172708d2012-04-04 00:50:22 +0100312 cfg.init_data = config->init_data;
Heiko Stübner7d4be2f2012-06-03 21:30:33 +0200313 cfg.driver_data = drvdata;
Frank Lif8a9f752012-11-12 17:59:52 +0800314 cfg.of_node = np;
Mark Brownc172708d2012-04-04 00:50:22 +0100315
Linus Walleijd6cd33a2019-01-29 11:31:52 +0100316 /*
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
Linus Walleijd162d042019-01-29 11:31:55 +0100325 cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
326 if (IS_ERR(cfg.ena_gpiod))
327 return PTR_ERR(cfg.ena_gpiod);
Axel Lin4b7c9482012-07-04 10:20:46 +0800328
Axel Lin7cdc2ee72019-03-08 14:15:28 +0800329 rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
330 if (IS_ERR(rdev)) {
331 ret = PTR_ERR(rdev);
Linus Walleijd162d042019-01-29 11:31:55 +0100332 dev_err(dev, "Failed to register regulator: %d\n", ret);
333 return ret;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200334 }
335
336 platform_set_drvdata(pdev, drvdata);
337
338 return 0;
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200339}
340
Axel Linec4f7b82012-12-04 10:32:05 +0800341#if defined(CONFIG_OF)
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800342static const struct of_device_id regulator_gpio_of_match[] = {
Lee Jones006694d2012-10-15 14:16:59 +0100343 { .compatible = "regulator-gpio", },
344 {},
345};
Luis de Bethencourt2f9481e2015-09-18 19:09:24 +0200346MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
Axel Linec4f7b82012-12-04 10:32:05 +0800347#endif
Lee Jones006694d2012-10-15 14:16:59 +0100348
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200349static struct platform_driver gpio_regulator_driver = {
350 .probe = gpio_regulator_probe,
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200351 .driver = {
352 .name = "gpio-regulator",
Axel Linec4f7b82012-12-04 10:32:05 +0800353 .of_match_table = of_match_ptr(regulator_gpio_of_match),
Heiko Stübner3f0292a2011-10-05 12:27:05 +0200354 },
355};
356
357static int __init gpio_regulator_init(void)
358{
359 return platform_driver_register(&gpio_regulator_driver);
360}
361subsys_initcall(gpio_regulator_init);
362
363static void __exit gpio_regulator_exit(void)
364{
365 platform_driver_unregister(&gpio_regulator_driver);
366}
367module_exit(gpio_regulator_exit);
368
369MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
370MODULE_DESCRIPTION("gpio voltage regulator");
371MODULE_LICENSE("GPL");
372MODULE_ALIAS("platform:gpio-regulator");