1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * gpio-regulator.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * based on fixed.c
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright 2008 Wolfson Microelectronics PLC.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Copyright (c) 2009 Nokia Corporation
14*4882a593Smuzhiyun * Roger Quadros <ext-roger.quadros@nokia.com>
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * This is useful for systems with mixed controllable and
17*4882a593Smuzhiyun * non-controllable regulators, as well as for allowing testing on
18*4882a593Smuzhiyun * systems with no controllable regulators.
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <linux/err.h>
22*4882a593Smuzhiyun #include <linux/mutex.h>
23*4882a593Smuzhiyun #include <linux/module.h>
24*4882a593Smuzhiyun #include <linux/platform_device.h>
25*4882a593Smuzhiyun #include <linux/regulator/driver.h>
26*4882a593Smuzhiyun #include <linux/regulator/machine.h>
27*4882a593Smuzhiyun #include <linux/regulator/of_regulator.h>
28*4882a593Smuzhiyun #include <linux/regulator/gpio-regulator.h>
29*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/of.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct gpio_regulator_data {
34*4882a593Smuzhiyun struct regulator_desc desc;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct gpio_desc **gpiods;
37*4882a593Smuzhiyun int nr_gpios;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct gpio_regulator_state *states;
40*4882a593Smuzhiyun int nr_states;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun int state;
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
gpio_regulator_get_value(struct regulator_dev * dev)45*4882a593Smuzhiyun static int gpio_regulator_get_value(struct regulator_dev *dev)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct gpio_regulator_data *data = rdev_get_drvdata(dev);
48*4882a593Smuzhiyun int ptr;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun for (ptr = 0; ptr < data->nr_states; ptr++)
51*4882a593Smuzhiyun if (data->states[ptr].gpios == data->state)
52*4882a593Smuzhiyun return data->states[ptr].value;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun return -EINVAL;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
gpio_regulator_set_voltage(struct regulator_dev * dev,int min_uV,int max_uV,unsigned * selector)57*4882a593Smuzhiyun static int gpio_regulator_set_voltage(struct regulator_dev *dev,
58*4882a593Smuzhiyun int min_uV, int max_uV,
59*4882a593Smuzhiyun unsigned *selector)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun struct gpio_regulator_data *data = rdev_get_drvdata(dev);
62*4882a593Smuzhiyun int ptr, target = 0, state, best_val = INT_MAX;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun for (ptr = 0; ptr < data->nr_states; ptr++)
65*4882a593Smuzhiyun if (data->states[ptr].value < best_val &&
66*4882a593Smuzhiyun data->states[ptr].value >= min_uV &&
67*4882a593Smuzhiyun data->states[ptr].value <= max_uV) {
68*4882a593Smuzhiyun target = data->states[ptr].gpios;
69*4882a593Smuzhiyun best_val = data->states[ptr].value;
70*4882a593Smuzhiyun if (selector)
71*4882a593Smuzhiyun *selector = ptr;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun if (best_val == INT_MAX)
75*4882a593Smuzhiyun return -EINVAL;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun for (ptr = 0; ptr < data->nr_gpios; ptr++) {
78*4882a593Smuzhiyun state = (target & (1 << ptr)) >> ptr;
79*4882a593Smuzhiyun gpiod_set_value_cansleep(data->gpiods[ptr], state);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun data->state = target;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
gpio_regulator_list_voltage(struct regulator_dev * dev,unsigned selector)86*4882a593Smuzhiyun static int gpio_regulator_list_voltage(struct regulator_dev *dev,
87*4882a593Smuzhiyun unsigned selector)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun struct gpio_regulator_data *data = rdev_get_drvdata(dev);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (selector >= data->nr_states)
92*4882a593Smuzhiyun return -EINVAL;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return data->states[selector].value;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
gpio_regulator_set_current_limit(struct regulator_dev * dev,int min_uA,int max_uA)97*4882a593Smuzhiyun static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
98*4882a593Smuzhiyun int min_uA, int max_uA)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun struct gpio_regulator_data *data = rdev_get_drvdata(dev);
101*4882a593Smuzhiyun int ptr, target = 0, state, best_val = 0;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun for (ptr = 0; ptr < data->nr_states; ptr++)
104*4882a593Smuzhiyun if (data->states[ptr].value > best_val &&
105*4882a593Smuzhiyun data->states[ptr].value >= min_uA &&
106*4882a593Smuzhiyun data->states[ptr].value <= max_uA) {
107*4882a593Smuzhiyun target = data->states[ptr].gpios;
108*4882a593Smuzhiyun best_val = data->states[ptr].value;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (best_val == 0)
112*4882a593Smuzhiyun return -EINVAL;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun for (ptr = 0; ptr < data->nr_gpios; ptr++) {
115*4882a593Smuzhiyun state = (target & (1 << ptr)) >> ptr;
116*4882a593Smuzhiyun gpiod_set_value_cansleep(data->gpiods[ptr], state);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun data->state = target;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return 0;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun static const struct regulator_ops gpio_regulator_voltage_ops = {
124*4882a593Smuzhiyun .get_voltage = gpio_regulator_get_value,
125*4882a593Smuzhiyun .set_voltage = gpio_regulator_set_voltage,
126*4882a593Smuzhiyun .list_voltage = gpio_regulator_list_voltage,
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun static struct gpio_regulator_config *
of_get_gpio_regulator_config(struct device * dev,struct device_node * np,const struct regulator_desc * desc)130*4882a593Smuzhiyun of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
131*4882a593Smuzhiyun const struct regulator_desc *desc)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct gpio_regulator_config *config;
134*4882a593Smuzhiyun const char *regtype;
135*4882a593Smuzhiyun int proplen, i;
136*4882a593Smuzhiyun int ngpios;
137*4882a593Smuzhiyun int ret;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun config = devm_kzalloc(dev,
140*4882a593Smuzhiyun sizeof(struct gpio_regulator_config),
141*4882a593Smuzhiyun GFP_KERNEL);
142*4882a593Smuzhiyun if (!config)
143*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun config->init_data = of_get_regulator_init_data(dev, np, desc);
146*4882a593Smuzhiyun if (!config->init_data)
147*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun config->supply_name = config->init_data->constraints.name;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (config->init_data->constraints.boot_on)
152*4882a593Smuzhiyun config->enabled_at_boot = true;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Do not use: undocumented device tree property.
156*4882a593Smuzhiyun * This is kept around solely for device tree ABI stability.
157*4882a593Smuzhiyun */
158*4882a593Smuzhiyun if (of_property_read_bool(np, "enable-at-boot"))
159*4882a593Smuzhiyun config->enabled_at_boot = true;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* Fetch GPIO init levels */
164*4882a593Smuzhiyun ngpios = gpiod_count(dev, NULL);
165*4882a593Smuzhiyun if (ngpios > 0) {
166*4882a593Smuzhiyun config->gflags = devm_kzalloc(dev,
167*4882a593Smuzhiyun sizeof(enum gpiod_flags)
168*4882a593Smuzhiyun * ngpios,
169*4882a593Smuzhiyun GFP_KERNEL);
170*4882a593Smuzhiyun if (!config->gflags)
171*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun for (i = 0; i < ngpios; i++) {
174*4882a593Smuzhiyun u32 val;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun ret = of_property_read_u32_index(np, "gpios-states", i,
177*4882a593Smuzhiyun &val);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Default to high per specification */
180*4882a593Smuzhiyun if (ret)
181*4882a593Smuzhiyun config->gflags[i] = GPIOD_OUT_HIGH;
182*4882a593Smuzhiyun else
183*4882a593Smuzhiyun config->gflags[i] =
184*4882a593Smuzhiyun val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun config->ngpios = ngpios;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* Fetch states. */
190*4882a593Smuzhiyun proplen = of_property_count_u32_elems(np, "states");
191*4882a593Smuzhiyun if (proplen < 0) {
192*4882a593Smuzhiyun dev_err(dev, "No 'states' property found\n");
193*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun config->states = devm_kcalloc(dev,
197*4882a593Smuzhiyun proplen / 2,
198*4882a593Smuzhiyun sizeof(struct gpio_regulator_state),
199*4882a593Smuzhiyun GFP_KERNEL);
200*4882a593Smuzhiyun if (!config->states)
201*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun for (i = 0; i < proplen / 2; i++) {
204*4882a593Smuzhiyun of_property_read_u32_index(np, "states", i * 2,
205*4882a593Smuzhiyun &config->states[i].value);
206*4882a593Smuzhiyun of_property_read_u32_index(np, "states", i * 2 + 1,
207*4882a593Smuzhiyun &config->states[i].gpios);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun config->nr_states = i;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun config->type = REGULATOR_VOLTAGE;
212*4882a593Smuzhiyun ret = of_property_read_string(np, "regulator-type", ®type);
213*4882a593Smuzhiyun if (ret >= 0) {
214*4882a593Smuzhiyun if (!strncmp("voltage", regtype, 7))
215*4882a593Smuzhiyun config->type = REGULATOR_VOLTAGE;
216*4882a593Smuzhiyun else if (!strncmp("current", regtype, 7))
217*4882a593Smuzhiyun config->type = REGULATOR_CURRENT;
218*4882a593Smuzhiyun else
219*4882a593Smuzhiyun dev_warn(dev, "Unknown regulator-type '%s'\n",
220*4882a593Smuzhiyun regtype);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return config;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun static const struct regulator_ops gpio_regulator_current_ops = {
227*4882a593Smuzhiyun .get_current_limit = gpio_regulator_get_value,
228*4882a593Smuzhiyun .set_current_limit = gpio_regulator_set_current_limit,
229*4882a593Smuzhiyun };
230*4882a593Smuzhiyun
gpio_regulator_probe(struct platform_device * pdev)231*4882a593Smuzhiyun static int gpio_regulator_probe(struct platform_device *pdev)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct device *dev = &pdev->dev;
234*4882a593Smuzhiyun struct gpio_regulator_config *config = dev_get_platdata(dev);
235*4882a593Smuzhiyun struct device_node *np = dev->of_node;
236*4882a593Smuzhiyun struct gpio_regulator_data *drvdata;
237*4882a593Smuzhiyun struct regulator_config cfg = { };
238*4882a593Smuzhiyun struct regulator_dev *rdev;
239*4882a593Smuzhiyun enum gpiod_flags gflags;
240*4882a593Smuzhiyun int ptr, ret, state, i;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
243*4882a593Smuzhiyun GFP_KERNEL);
244*4882a593Smuzhiyun if (drvdata == NULL)
245*4882a593Smuzhiyun return -ENOMEM;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun if (np) {
248*4882a593Smuzhiyun config = of_get_gpio_regulator_config(dev, np,
249*4882a593Smuzhiyun &drvdata->desc);
250*4882a593Smuzhiyun if (IS_ERR(config))
251*4882a593Smuzhiyun return PTR_ERR(config);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
255*4882a593Smuzhiyun if (drvdata->desc.name == NULL) {
256*4882a593Smuzhiyun dev_err(dev, "Failed to allocate supply name\n");
257*4882a593Smuzhiyun return -ENOMEM;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
261*4882a593Smuzhiyun GFP_KERNEL);
262*4882a593Smuzhiyun if (!drvdata->gpiods)
263*4882a593Smuzhiyun return -ENOMEM;
264*4882a593Smuzhiyun for (i = 0; i < config->ngpios; i++) {
265*4882a593Smuzhiyun drvdata->gpiods[i] = devm_gpiod_get_index(dev,
266*4882a593Smuzhiyun NULL,
267*4882a593Smuzhiyun i,
268*4882a593Smuzhiyun config->gflags[i]);
269*4882a593Smuzhiyun if (IS_ERR(drvdata->gpiods[i]))
270*4882a593Smuzhiyun return PTR_ERR(drvdata->gpiods[i]);
271*4882a593Smuzhiyun /* This is good to know */
272*4882a593Smuzhiyun gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun drvdata->nr_gpios = config->ngpios;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun drvdata->states = devm_kmemdup(dev,
277*4882a593Smuzhiyun config->states,
278*4882a593Smuzhiyun config->nr_states *
279*4882a593Smuzhiyun sizeof(struct gpio_regulator_state),
280*4882a593Smuzhiyun GFP_KERNEL);
281*4882a593Smuzhiyun if (drvdata->states == NULL) {
282*4882a593Smuzhiyun dev_err(dev, "Failed to allocate state data\n");
283*4882a593Smuzhiyun return -ENOMEM;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun drvdata->nr_states = config->nr_states;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun drvdata->desc.owner = THIS_MODULE;
288*4882a593Smuzhiyun drvdata->desc.enable_time = config->startup_delay;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* handle regulator type*/
291*4882a593Smuzhiyun switch (config->type) {
292*4882a593Smuzhiyun case REGULATOR_VOLTAGE:
293*4882a593Smuzhiyun drvdata->desc.type = REGULATOR_VOLTAGE;
294*4882a593Smuzhiyun drvdata->desc.ops = &gpio_regulator_voltage_ops;
295*4882a593Smuzhiyun drvdata->desc.n_voltages = config->nr_states;
296*4882a593Smuzhiyun break;
297*4882a593Smuzhiyun case REGULATOR_CURRENT:
298*4882a593Smuzhiyun drvdata->desc.type = REGULATOR_CURRENT;
299*4882a593Smuzhiyun drvdata->desc.ops = &gpio_regulator_current_ops;
300*4882a593Smuzhiyun break;
301*4882a593Smuzhiyun default:
302*4882a593Smuzhiyun dev_err(dev, "No regulator type set\n");
303*4882a593Smuzhiyun return -EINVAL;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* build initial state from gpio init data. */
307*4882a593Smuzhiyun state = 0;
308*4882a593Smuzhiyun for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
309*4882a593Smuzhiyun if (config->gflags[ptr] == GPIOD_OUT_HIGH)
310*4882a593Smuzhiyun state |= (1 << ptr);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun drvdata->state = state;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun cfg.dev = dev;
315*4882a593Smuzhiyun cfg.init_data = config->init_data;
316*4882a593Smuzhiyun cfg.driver_data = drvdata;
317*4882a593Smuzhiyun cfg.of_node = np;
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /*
320*4882a593Smuzhiyun * The signal will be inverted by the GPIO core if flagged so in the
321*4882a593Smuzhiyun * descriptor.
322*4882a593Smuzhiyun */
323*4882a593Smuzhiyun if (config->enabled_at_boot)
324*4882a593Smuzhiyun gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
325*4882a593Smuzhiyun else
326*4882a593Smuzhiyun gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
329*4882a593Smuzhiyun if (IS_ERR(cfg.ena_gpiod))
330*4882a593Smuzhiyun return PTR_ERR(cfg.ena_gpiod);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
333*4882a593Smuzhiyun if (IS_ERR(rdev)) {
334*4882a593Smuzhiyun ret = PTR_ERR(rdev);
335*4882a593Smuzhiyun dev_err(dev, "Failed to register regulator: %d\n", ret);
336*4882a593Smuzhiyun return ret;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun platform_set_drvdata(pdev, drvdata);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun return 0;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun #if defined(CONFIG_OF)
345*4882a593Smuzhiyun static const struct of_device_id regulator_gpio_of_match[] = {
346*4882a593Smuzhiyun { .compatible = "regulator-gpio", },
347*4882a593Smuzhiyun {},
348*4882a593Smuzhiyun };
349*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
350*4882a593Smuzhiyun #endif
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun static struct platform_driver gpio_regulator_driver = {
353*4882a593Smuzhiyun .probe = gpio_regulator_probe,
354*4882a593Smuzhiyun .driver = {
355*4882a593Smuzhiyun .name = "gpio-regulator",
356*4882a593Smuzhiyun .of_match_table = of_match_ptr(regulator_gpio_of_match),
357*4882a593Smuzhiyun },
358*4882a593Smuzhiyun };
359*4882a593Smuzhiyun
gpio_regulator_init(void)360*4882a593Smuzhiyun static int __init gpio_regulator_init(void)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun return platform_driver_register(&gpio_regulator_driver);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun subsys_initcall(gpio_regulator_init);
365*4882a593Smuzhiyun
gpio_regulator_exit(void)366*4882a593Smuzhiyun static void __exit gpio_regulator_exit(void)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun platform_driver_unregister(&gpio_regulator_driver);
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun module_exit(gpio_regulator_exit);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
373*4882a593Smuzhiyun MODULE_DESCRIPTION("gpio voltage regulator");
374*4882a593Smuzhiyun MODULE_LICENSE("GPL");
375*4882a593Smuzhiyun MODULE_ALIAS("platform:gpio-regulator");
376