regulator: gpio: Convert to use descriptors

This converts the GPIO regulator driver to use decriptors only.

We have to let go of the array gpio handling: the fetched descriptors
are handled individually anyway, and the array retrieveal function
does not make it possible to retrieve each GPIO descriptor with
unique flags. Instead get them one by one.

We request the "enable" GPIO separately as before, and make sure
that this line is requested as nonexclusive since enable lines can
be shared and the regulator core expects this.

Most users of the GPIO regulator are using device tree.

There are two boards in the kernel using the gpio regulator from a
non-devicetree path: PXA hx4700 and magician. Make sure to switch
these over to use descriptors as well.

Cc: Philipp Zabel <p.zabel@pengutronix.de> # Magician
Cc: Petr Cvek <petr.cvek@tul.cz> # Magician
Cc: Robert Jarzmik <robert.jarzmik@free.fr> # PXA
Cc: Paul Parsons <lost.distance@yahoo.com> # hx4700
Cc: Kevin Hilman <khilman@baylibre.com> # Meson
Cc: Neil Armstrong <narmstrong@baylibre.com> # Meson
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
This commit is contained in:
Linus Walleij 2019-01-29 11:31:52 +01:00 committed by Mark Brown
parent 4f297062b2
commit d6cd33ad71
No known key found for this signature in database
GPG Key ID: 24D68B725D5487D0
4 changed files with 95 additions and 113 deletions

View File

@ -19,6 +19,7 @@
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/fb.h> #include <linux/fb.h>
#include <linux/gpio/machine.h>
#include <linux/gpio.h> #include <linux/gpio.h>
#include <linux/gpio_keys.h> #include <linux/gpio_keys.h>
#include <linux/input.h> #include <linux/input.h>
@ -702,9 +703,7 @@ static struct regulator_init_data bq24022_init_data = {
.consumer_supplies = bq24022_consumers, .consumer_supplies = bq24022_consumers,
}; };
static struct gpio bq24022_gpios[] = { static enum gpiod_flags bq24022_gpiod_gflags[] = { GPIOD_OUT_LOW };
{ GPIO96_HX4700_BQ24022_ISET2, GPIOF_OUT_INIT_LOW, "bq24022_iset2" },
};
static struct gpio_regulator_state bq24022_states[] = { static struct gpio_regulator_state bq24022_states[] = {
{ .value = 100000, .gpios = (0 << 0) }, { .value = 100000, .gpios = (0 << 0) },
@ -714,12 +713,10 @@ static struct gpio_regulator_state bq24022_states[] = {
static struct gpio_regulator_config bq24022_info = { static struct gpio_regulator_config bq24022_info = {
.supply_name = "bq24022", .supply_name = "bq24022",
.enable_gpio = GPIO72_HX4700_BQ24022_nCHARGE_EN,
.enable_high = 0,
.enabled_at_boot = 0, .enabled_at_boot = 0,
.gpios = bq24022_gpios, .gflags = bq24022_gpiod_gflags,
.nr_gpios = ARRAY_SIZE(bq24022_gpios), .ngpios = ARRAY_SIZE(bq24022_gpiod_gflags),
.states = bq24022_states, .states = bq24022_states,
.nr_states = ARRAY_SIZE(bq24022_states), .nr_states = ARRAY_SIZE(bq24022_states),
@ -736,6 +733,17 @@ static struct platform_device bq24022 = {
}, },
}; };
static struct gpiod_lookup_table bq24022_gpiod_table = {
.dev_id = "gpio-regulator",
.table = {
GPIO_LOOKUP("gpio-pxa", GPIO96_HX4700_BQ24022_ISET2,
NULL, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP("gpio-pxa", GPIO72_HX4700_BQ24022_nCHARGE_EN,
"enable", GPIO_ACTIVE_LOW),
{ },
},
};
/* /*
* StrataFlash * StrataFlash
*/ */
@ -878,6 +886,7 @@ static void __init hx4700_init(void)
pxa_set_btuart_info(NULL); pxa_set_btuart_info(NULL);
pxa_set_stuart_info(NULL); pxa_set_stuart_info(NULL);
gpiod_add_lookup_table(&bq24022_gpiod_table);
platform_add_devices(devices, ARRAY_SIZE(devices)); platform_add_devices(devices, ARRAY_SIZE(devices));
pwm_add_table(hx4700_pwm_lookup, ARRAY_SIZE(hx4700_pwm_lookup)); pwm_add_table(hx4700_pwm_lookup, ARRAY_SIZE(hx4700_pwm_lookup));

View File

@ -645,9 +645,8 @@ static struct regulator_init_data bq24022_init_data = {
.consumer_supplies = bq24022_consumers, .consumer_supplies = bq24022_consumers,
}; };
static struct gpio bq24022_gpios[] = {
{ EGPIO_MAGICIAN_BQ24022_ISET2, GPIOF_OUT_INIT_LOW, "bq24022_iset2" }, static enum gpiod_flags bq24022_gpiod_gflags[] = { GPIOD_OUT_LOW };
};
static struct gpio_regulator_state bq24022_states[] = { static struct gpio_regulator_state bq24022_states[] = {
{ .value = 100000, .gpios = (0 << 0) }, { .value = 100000, .gpios = (0 << 0) },
@ -657,12 +656,10 @@ static struct gpio_regulator_state bq24022_states[] = {
static struct gpio_regulator_config bq24022_info = { static struct gpio_regulator_config bq24022_info = {
.supply_name = "bq24022", .supply_name = "bq24022",
.enable_gpio = GPIO30_MAGICIAN_BQ24022_nCHARGE_EN,
.enable_high = 0,
.enabled_at_boot = 1, .enabled_at_boot = 1,
.gpios = bq24022_gpios, .gflags = bq24022_gpiod_gflags,
.nr_gpios = ARRAY_SIZE(bq24022_gpios), .ngpios = ARRAY_SIZE(bq24022_gpiod_gflags),
.states = bq24022_states, .states = bq24022_states,
.nr_states = ARRAY_SIZE(bq24022_states), .nr_states = ARRAY_SIZE(bq24022_states),
@ -679,6 +676,17 @@ static struct platform_device bq24022 = {
}, },
}; };
static struct gpiod_lookup_table bq24022_gpiod_table = {
.dev_id = "gpio-regulator",
.table = {
GPIO_LOOKUP("gpio-pxa", EGPIO_MAGICIAN_BQ24022_ISET2,
NULL, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP("gpio-pxa", GPIO30_MAGICIAN_BQ24022_nCHARGE_EN,
"enable", GPIO_ACTIVE_LOW),
{ },
},
};
/* /*
* fixed regulator for ads7846 * fixed regulator for ads7846
*/ */
@ -1027,6 +1035,7 @@ static void __init magician_init(void)
regulator_register_always_on(0, "power", pwm_backlight_supply, regulator_register_always_on(0, "power", pwm_backlight_supply,
ARRAY_SIZE(pwm_backlight_supply), 5000000); ARRAY_SIZE(pwm_backlight_supply), 5000000);
gpiod_add_lookup_table(&bq24022_gpiod_table);
platform_add_devices(ARRAY_AND_SIZE(devices)); platform_add_devices(ARRAY_AND_SIZE(devices));
} }

View File

@ -30,16 +30,15 @@
#include <linux/regulator/machine.h> #include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h> #include <linux/regulator/of_regulator.h>
#include <linux/regulator/gpio-regulator.h> #include <linux/regulator/gpio-regulator.h>
#include <linux/gpio.h> #include <linux/gpio/consumer.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_gpio.h>
struct gpio_regulator_data { struct gpio_regulator_data {
struct regulator_desc desc; struct regulator_desc desc;
struct regulator_dev *dev; struct regulator_dev *dev;
struct gpio *gpios; struct gpio_desc **gpiods;
int nr_gpios; int nr_gpios;
struct gpio_regulator_state *states; struct gpio_regulator_state *states;
@ -82,7 +81,7 @@ static int gpio_regulator_set_voltage(struct regulator_dev *dev,
for (ptr = 0; ptr < data->nr_gpios; ptr++) { for (ptr = 0; ptr < data->nr_gpios; ptr++) {
state = (target & (1 << ptr)) >> ptr; state = (target & (1 << ptr)) >> ptr;
gpio_set_value_cansleep(data->gpios[ptr].gpio, state); gpiod_set_value_cansleep(data->gpiods[ptr], state);
} }
data->state = target; data->state = target;
@ -119,7 +118,7 @@ static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
for (ptr = 0; ptr < data->nr_gpios; ptr++) { for (ptr = 0; ptr < data->nr_gpios; ptr++) {
state = (target & (1 << ptr)) >> ptr; state = (target & (1 << ptr)) >> ptr;
gpio_set_value_cansleep(data->gpios[ptr].gpio, state); gpiod_set_value_cansleep(data->gpiods[ptr], state);
} }
data->state = target; data->state = target;
@ -138,7 +137,8 @@ of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
{ {
struct gpio_regulator_config *config; struct gpio_regulator_config *config;
const char *regtype; const char *regtype;
int proplen, gpio, i; int proplen, i;
int ngpios;
int ret; int ret;
config = devm_kzalloc(dev, config = devm_kzalloc(dev,
@ -153,59 +153,36 @@ of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
config->supply_name = config->init_data->constraints.name; config->supply_name = config->init_data->constraints.name;
if (of_property_read_bool(np, "enable-active-high"))
config->enable_high = true;
if (of_property_read_bool(np, "enable-at-boot")) if (of_property_read_bool(np, "enable-at-boot"))
config->enabled_at_boot = true; config->enabled_at_boot = true;
of_property_read_u32(np, "startup-delay-us", &config->startup_delay); of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
config->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0); /* Fetch GPIO init levels */
if (config->enable_gpio < 0 && config->enable_gpio != -ENOENT) ngpios = gpiod_count(dev, NULL);
return ERR_PTR(config->enable_gpio); if (ngpios > 0) {
config->gflags = devm_kzalloc(dev,
/* Fetch GPIOs. - optional property*/ sizeof(enum gpiod_flags)
ret = of_gpio_count(np); * ngpios,
if ((ret < 0) && (ret != -ENOENT)) GFP_KERNEL);
return ERR_PTR(ret); if (!config->gflags)
if (ret > 0) {
config->nr_gpios = ret;
config->gpios = devm_kcalloc(dev,
config->nr_gpios, sizeof(struct gpio),
GFP_KERNEL);
if (!config->gpios)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
proplen = of_property_count_u32_elems(np, "gpios-states"); for (i = 0; i < ngpios; i++) {
/* optional property */ u32 val;
if (proplen < 0)
proplen = 0;
if (proplen > 0 && proplen != config->nr_gpios) { ret = of_property_read_u32_index(np, "gpios-states", i,
dev_warn(dev, "gpios <-> gpios-states mismatch\n"); &val);
proplen = 0;
}
for (i = 0; i < config->nr_gpios; i++) { /* Default to high per specification */
gpio = of_get_named_gpio(np, "gpios", i); if (ret)
if (gpio < 0) { config->gflags[i] = GPIOD_OUT_HIGH;
if (gpio != -ENOENT) else
return ERR_PTR(gpio); config->gflags[i] =
break; val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
}
config->gpios[i].gpio = gpio;
config->gpios[i].label = config->supply_name;
if (proplen > 0) {
of_property_read_u32_index(np, "gpios-states",
i, &ret);
if (ret)
config->gpios[i].flags =
GPIOF_OUT_INIT_HIGH;
}
} }
} }
config->ngpios = ngpios;
/* Fetch states. */ /* Fetch states. */
proplen = of_property_count_u32_elems(np, "states"); proplen = of_property_count_u32_elems(np, "states");
@ -255,7 +232,8 @@ static int gpio_regulator_probe(struct platform_device *pdev)
struct device_node *np = pdev->dev.of_node; struct device_node *np = pdev->dev.of_node;
struct gpio_regulator_data *drvdata; struct gpio_regulator_data *drvdata;
struct regulator_config cfg = { }; struct regulator_config cfg = { };
int ptr, ret, state; enum gpiod_flags gflags;
int ptr, ret, state, i;
drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data), drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
GFP_KERNEL); GFP_KERNEL);
@ -275,26 +253,21 @@ static int gpio_regulator_probe(struct platform_device *pdev)
return -ENOMEM; return -ENOMEM;
} }
if (config->nr_gpios != 0) { drvdata->gpiods = devm_kzalloc(&pdev->dev, sizeof(struct gpio_desc *),
drvdata->gpios = kmemdup(config->gpios, GFP_KERNEL);
config->nr_gpios * sizeof(struct gpio), if (!drvdata->gpiods)
GFP_KERNEL); return -ENOMEM;
if (drvdata->gpios == NULL) { for (i = 0; i < config->ngpios; i++) {
dev_err(&pdev->dev, "Failed to allocate gpio data\n"); drvdata->gpiods[i] = devm_gpiod_get_index(&pdev->dev,
ret = -ENOMEM; NULL,
goto err_name; i,
} config->gflags[i]);
if (IS_ERR(drvdata->gpiods[i]))
drvdata->nr_gpios = config->nr_gpios; return PTR_ERR(drvdata->gpiods[i]);
ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios); /* This is good to know */
if (ret) { gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev,
"Could not obtain regulator setting GPIOs: %d\n",
ret);
goto err_memgpio;
}
} }
drvdata->nr_gpios = config->ngpios;
drvdata->states = kmemdup(config->states, drvdata->states = kmemdup(config->states,
config->nr_states * config->nr_states *
@ -303,7 +276,7 @@ static int gpio_regulator_probe(struct platform_device *pdev)
if (drvdata->states == NULL) { if (drvdata->states == NULL) {
dev_err(&pdev->dev, "Failed to allocate state data\n"); dev_err(&pdev->dev, "Failed to allocate state data\n");
ret = -ENOMEM; ret = -ENOMEM;
goto err_stategpio; goto err_name;
} }
drvdata->nr_states = config->nr_states; drvdata->nr_states = config->nr_states;
@ -330,7 +303,7 @@ static int gpio_regulator_probe(struct platform_device *pdev)
/* build initial state from gpio init data. */ /* build initial state from gpio init data. */
state = 0; state = 0;
for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) { for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH) if (config->gflags[ptr] == GPIOD_OUT_HIGH)
state |= (1 << ptr); state |= (1 << ptr);
} }
drvdata->state = state; drvdata->state = state;
@ -340,21 +313,19 @@ static int gpio_regulator_probe(struct platform_device *pdev)
cfg.driver_data = drvdata; cfg.driver_data = drvdata;
cfg.of_node = np; cfg.of_node = np;
if (gpio_is_valid(config->enable_gpio)) { /*
cfg.ena_gpio = config->enable_gpio; * The signal will be inverted by the GPIO core if flagged so in the
cfg.ena_gpio_initialized = true; * decriptor.
} */
cfg.ena_gpio_invert = !config->enable_high; if (config->enabled_at_boot)
if (config->enabled_at_boot) { gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
if (config->enable_high) else
cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
else
cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, "enable", gflags);
} else { if (IS_ERR(cfg.ena_gpiod)) {
if (config->enable_high) ret = PTR_ERR(cfg.ena_gpiod);
cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW; goto err_memstate;
else
cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
} }
drvdata->dev = regulator_register(&drvdata->desc, &cfg); drvdata->dev = regulator_register(&drvdata->desc, &cfg);
@ -370,10 +341,6 @@ static int gpio_regulator_probe(struct platform_device *pdev)
err_memstate: err_memstate:
kfree(drvdata->states); kfree(drvdata->states);
err_stategpio:
gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
err_memgpio:
kfree(drvdata->gpios);
err_name: err_name:
kfree(drvdata->desc.name); kfree(drvdata->desc.name);
return ret; return ret;
@ -384,12 +351,7 @@ static int gpio_regulator_remove(struct platform_device *pdev)
struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev); struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
regulator_unregister(drvdata->dev); regulator_unregister(drvdata->dev);
gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
kfree(drvdata->states); kfree(drvdata->states);
kfree(drvdata->gpios);
kfree(drvdata->desc.name); kfree(drvdata->desc.name);
return 0; return 0;

View File

@ -21,6 +21,8 @@
#ifndef __REGULATOR_GPIO_H #ifndef __REGULATOR_GPIO_H
#define __REGULATOR_GPIO_H #define __REGULATOR_GPIO_H
#include <linux/gpio/consumer.h>
struct regulator_init_data; struct regulator_init_data;
enum regulator_type; enum regulator_type;
@ -53,9 +55,9 @@ struct gpio_regulator_state {
* This is used to keep the regulator at * This is used to keep the regulator at
* the default state * the default state
* @startup_delay: Start-up time in microseconds * @startup_delay: Start-up time in microseconds
* @gpios: Array containing the gpios needed to control * @gflags: Array of GPIO configuration flags for initial
* the setting of the regulator * states
* @nr_gpios: Number of gpios * @ngpios: Number of GPIOs and configurations available
* @states: Array of gpio_regulator_state entries describing * @states: Array of gpio_regulator_state entries describing
* the gpio state for specific voltages * the gpio state for specific voltages
* @nr_states: Number of states available * @nr_states: Number of states available
@ -74,8 +76,8 @@ struct gpio_regulator_config {
unsigned enabled_at_boot:1; unsigned enabled_at_boot:1;
unsigned startup_delay; unsigned startup_delay;
struct gpio *gpios; enum gpiod_flags *gflags;
int nr_gpios; int ngpios;
struct gpio_regulator_state *states; struct gpio_regulator_state *states;
int nr_states; int nr_states;