Lines Matching +full:gpios +full:- +full:states
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * gpio-regulator.c
14 * Roger Quadros <ext-roger.quadros@nokia.com>
17 * non-controllable regulators, as well as for allowing testing on
28 #include <linux/regulator/gpio-regulator.h>
39 struct gpio_regulator_state *states; member
50 for (ptr = 0; ptr < data->nr_states; ptr++) in gpio_regulator_get_value()
51 if (data->states[ptr].gpios == data->state) in gpio_regulator_get_value()
52 return data->states[ptr].value; in gpio_regulator_get_value()
54 return -EINVAL; in gpio_regulator_get_value()
64 for (ptr = 0; ptr < data->nr_states; ptr++) in gpio_regulator_set_voltage()
65 if (data->states[ptr].value < best_val && in gpio_regulator_set_voltage()
66 data->states[ptr].value >= min_uV && in gpio_regulator_set_voltage()
67 data->states[ptr].value <= max_uV) { in gpio_regulator_set_voltage()
68 target = data->states[ptr].gpios; in gpio_regulator_set_voltage()
69 best_val = data->states[ptr].value; in gpio_regulator_set_voltage()
75 return -EINVAL; in gpio_regulator_set_voltage()
77 for (ptr = 0; ptr < data->nr_gpios; ptr++) { in gpio_regulator_set_voltage()
79 gpiod_set_value_cansleep(data->gpiods[ptr], state); in gpio_regulator_set_voltage()
81 data->state = target; in gpio_regulator_set_voltage()
91 if (selector >= data->nr_states) in gpio_regulator_list_voltage()
92 return -EINVAL; in gpio_regulator_list_voltage()
94 return data->states[selector].value; in gpio_regulator_list_voltage()
103 for (ptr = 0; ptr < data->nr_states; ptr++) in gpio_regulator_set_current_limit()
104 if (data->states[ptr].value > best_val && in gpio_regulator_set_current_limit()
105 data->states[ptr].value >= min_uA && in gpio_regulator_set_current_limit()
106 data->states[ptr].value <= max_uA) { in gpio_regulator_set_current_limit()
107 target = data->states[ptr].gpios; in gpio_regulator_set_current_limit()
108 best_val = data->states[ptr].value; in gpio_regulator_set_current_limit()
112 return -EINVAL; in gpio_regulator_set_current_limit()
114 for (ptr = 0; ptr < data->nr_gpios; ptr++) { in gpio_regulator_set_current_limit()
116 gpiod_set_value_cansleep(data->gpiods[ptr], state); in gpio_regulator_set_current_limit()
118 data->state = target; in gpio_regulator_set_current_limit()
143 return ERR_PTR(-ENOMEM); in of_get_gpio_regulator_config()
145 config->init_data = of_get_regulator_init_data(dev, np, desc); in of_get_gpio_regulator_config()
146 if (!config->init_data) in of_get_gpio_regulator_config()
147 return ERR_PTR(-EINVAL); in of_get_gpio_regulator_config()
149 config->supply_name = config->init_data->constraints.name; in of_get_gpio_regulator_config()
151 if (config->init_data->constraints.boot_on) in of_get_gpio_regulator_config()
152 config->enabled_at_boot = true; in of_get_gpio_regulator_config()
158 if (of_property_read_bool(np, "enable-at-boot")) in of_get_gpio_regulator_config()
159 config->enabled_at_boot = true; in of_get_gpio_regulator_config()
161 of_property_read_u32(np, "startup-delay-us", &config->startup_delay); in of_get_gpio_regulator_config()
166 config->gflags = devm_kzalloc(dev, in of_get_gpio_regulator_config()
170 if (!config->gflags) in of_get_gpio_regulator_config()
171 return ERR_PTR(-ENOMEM); in of_get_gpio_regulator_config()
176 ret = of_property_read_u32_index(np, "gpios-states", i, in of_get_gpio_regulator_config()
181 config->gflags[i] = GPIOD_OUT_HIGH; in of_get_gpio_regulator_config()
183 config->gflags[i] = in of_get_gpio_regulator_config()
187 config->ngpios = ngpios; in of_get_gpio_regulator_config()
189 /* Fetch states. */ in of_get_gpio_regulator_config()
190 proplen = of_property_count_u32_elems(np, "states"); in of_get_gpio_regulator_config()
192 dev_err(dev, "No 'states' property found\n"); in of_get_gpio_regulator_config()
193 return ERR_PTR(-EINVAL); in of_get_gpio_regulator_config()
196 config->states = devm_kcalloc(dev, in of_get_gpio_regulator_config()
200 if (!config->states) in of_get_gpio_regulator_config()
201 return ERR_PTR(-ENOMEM); in of_get_gpio_regulator_config()
204 of_property_read_u32_index(np, "states", i * 2, in of_get_gpio_regulator_config()
205 &config->states[i].value); in of_get_gpio_regulator_config()
206 of_property_read_u32_index(np, "states", i * 2 + 1, in of_get_gpio_regulator_config()
207 &config->states[i].gpios); in of_get_gpio_regulator_config()
209 config->nr_states = i; in of_get_gpio_regulator_config()
211 config->type = REGULATOR_VOLTAGE; in of_get_gpio_regulator_config()
212 ret = of_property_read_string(np, "regulator-type", ®type); in of_get_gpio_regulator_config()
215 config->type = REGULATOR_VOLTAGE; in of_get_gpio_regulator_config()
217 config->type = REGULATOR_CURRENT; in of_get_gpio_regulator_config()
219 dev_warn(dev, "Unknown regulator-type '%s'\n", in of_get_gpio_regulator_config()
233 struct device *dev = &pdev->dev; in gpio_regulator_probe()
235 struct device_node *np = dev->of_node; in gpio_regulator_probe()
245 return -ENOMEM; in gpio_regulator_probe()
249 &drvdata->desc); in gpio_regulator_probe()
254 drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL); in gpio_regulator_probe()
255 if (drvdata->desc.name == NULL) { in gpio_regulator_probe()
257 return -ENOMEM; in gpio_regulator_probe()
260 drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *), in gpio_regulator_probe()
262 if (!drvdata->gpiods) in gpio_regulator_probe()
263 return -ENOMEM; in gpio_regulator_probe()
264 for (i = 0; i < config->ngpios; i++) { in gpio_regulator_probe()
265 drvdata->gpiods[i] = devm_gpiod_get_index(dev, in gpio_regulator_probe()
268 config->gflags[i]); in gpio_regulator_probe()
269 if (IS_ERR(drvdata->gpiods[i])) in gpio_regulator_probe()
270 return PTR_ERR(drvdata->gpiods[i]); in gpio_regulator_probe()
272 gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name); in gpio_regulator_probe()
274 drvdata->nr_gpios = config->ngpios; in gpio_regulator_probe()
276 drvdata->states = devm_kmemdup(dev, in gpio_regulator_probe()
277 config->states, in gpio_regulator_probe()
278 config->nr_states * in gpio_regulator_probe()
281 if (drvdata->states == NULL) { in gpio_regulator_probe()
283 return -ENOMEM; in gpio_regulator_probe()
285 drvdata->nr_states = config->nr_states; in gpio_regulator_probe()
287 drvdata->desc.owner = THIS_MODULE; in gpio_regulator_probe()
288 drvdata->desc.enable_time = config->startup_delay; in gpio_regulator_probe()
291 switch (config->type) { in gpio_regulator_probe()
293 drvdata->desc.type = REGULATOR_VOLTAGE; in gpio_regulator_probe()
294 drvdata->desc.ops = &gpio_regulator_voltage_ops; in gpio_regulator_probe()
295 drvdata->desc.n_voltages = config->nr_states; in gpio_regulator_probe()
298 drvdata->desc.type = REGULATOR_CURRENT; in gpio_regulator_probe()
299 drvdata->desc.ops = &gpio_regulator_current_ops; in gpio_regulator_probe()
303 return -EINVAL; in gpio_regulator_probe()
308 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { in gpio_regulator_probe()
309 if (config->gflags[ptr] == GPIOD_OUT_HIGH) in gpio_regulator_probe()
312 drvdata->state = state; in gpio_regulator_probe()
315 cfg.init_data = config->init_data; in gpio_regulator_probe()
323 if (config->enabled_at_boot) in gpio_regulator_probe()
332 rdev = devm_regulator_register(dev, &drvdata->desc, &cfg); in gpio_regulator_probe()
346 { .compatible = "regulator-gpio", },
355 .name = "gpio-regulator",
375 MODULE_ALIAS("platform:gpio-regulator");