Lines Matching +full:gpio +full:- +full:cfg

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>
29 #include <linux/gpio/consumer.h>
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()
163 /* Fetch GPIO init levels */ 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()
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()
205 &config->states[i].value); 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", &regtype); 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()
223 if (of_property_present(np, "vin-supply")) in of_get_gpio_regulator_config()
224 config->input_supply = "vin"; in of_get_gpio_regulator_config()
236 struct device *dev = &pdev->dev; in gpio_regulator_probe()
238 struct device_node *np = dev->of_node; in gpio_regulator_probe()
240 struct regulator_config cfg = { }; in gpio_regulator_probe() local
248 return -ENOMEM; in gpio_regulator_probe()
252 &drvdata->desc); in gpio_regulator_probe()
257 drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL); in gpio_regulator_probe()
258 if (drvdata->desc.name == NULL) { in gpio_regulator_probe()
260 return -ENOMEM; in gpio_regulator_probe()
263 drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *), in gpio_regulator_probe()
266 if (config->input_supply) { in gpio_regulator_probe()
267 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, in gpio_regulator_probe()
268 config->input_supply, in gpio_regulator_probe()
270 if (!drvdata->desc.supply_name) { in gpio_regulator_probe()
271 dev_err(&pdev->dev, in gpio_regulator_probe()
273 return -ENOMEM; in gpio_regulator_probe()
277 if (!drvdata->gpiods) in gpio_regulator_probe()
278 return -ENOMEM; in gpio_regulator_probe()
279 for (i = 0; i < config->ngpios; i++) { in gpio_regulator_probe()
280 drvdata->gpiods[i] = devm_gpiod_get_index(dev, in gpio_regulator_probe()
283 config->gflags[i]); in gpio_regulator_probe()
284 if (IS_ERR(drvdata->gpiods[i])) in gpio_regulator_probe()
285 return PTR_ERR(drvdata->gpiods[i]); in gpio_regulator_probe()
287 gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name); in gpio_regulator_probe()
289 drvdata->nr_gpios = config->ngpios; in gpio_regulator_probe()
291 drvdata->states = devm_kmemdup(dev, in gpio_regulator_probe()
292 config->states, in gpio_regulator_probe()
293 config->nr_states * in gpio_regulator_probe()
296 if (drvdata->states == NULL) { in gpio_regulator_probe()
298 return -ENOMEM; in gpio_regulator_probe()
300 drvdata->nr_states = config->nr_states; in gpio_regulator_probe()
302 drvdata->desc.owner = THIS_MODULE; in gpio_regulator_probe()
303 drvdata->desc.enable_time = config->startup_delay; in gpio_regulator_probe()
306 switch (config->type) { in gpio_regulator_probe()
308 drvdata->desc.type = REGULATOR_VOLTAGE; in gpio_regulator_probe()
309 drvdata->desc.ops = &gpio_regulator_voltage_ops; in gpio_regulator_probe()
310 drvdata->desc.n_voltages = config->nr_states; in gpio_regulator_probe()
313 drvdata->desc.type = REGULATOR_CURRENT; in gpio_regulator_probe()
314 drvdata->desc.ops = &gpio_regulator_current_ops; in gpio_regulator_probe()
318 return -EINVAL; in gpio_regulator_probe()
321 /* build initial state from gpio init data. */ in gpio_regulator_probe()
323 for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { in gpio_regulator_probe()
324 if (config->gflags[ptr] == GPIOD_OUT_HIGH) in gpio_regulator_probe()
327 drvdata->state = state; in gpio_regulator_probe()
329 cfg.dev = dev; in gpio_regulator_probe()
330 cfg.init_data = config->init_data; in gpio_regulator_probe()
331 cfg.driver_data = drvdata; in gpio_regulator_probe()
332 cfg.of_node = np; in gpio_regulator_probe()
335 * The signal will be inverted by the GPIO core if flagged so in the in gpio_regulator_probe()
338 if (config->enabled_at_boot) in gpio_regulator_probe()
343 cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags); in gpio_regulator_probe()
344 if (IS_ERR(cfg.ena_gpiod)) in gpio_regulator_probe()
345 return PTR_ERR(cfg.ena_gpiod); in gpio_regulator_probe()
347 rdev = devm_regulator_register(dev, &drvdata->desc, &cfg); in gpio_regulator_probe()
361 { .compatible = "regulator-gpio", },
370 .name = "gpio-regulator",
389 MODULE_DESCRIPTION("gpio voltage regulator");
391 MODULE_ALIAS("platform:gpio-regulator");