diff options
author | Andrey Smirnov <andrew.smirnov@gmail.com> | 2017-06-03 20:11:52 -0700 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2017-06-06 08:27:15 +0200 |
commit | bbc499914739cd51f51cd23ade0a2dca19359804 (patch) | |
tree | 6b36fb0e96430f15657283e200ec810be16d0158 /drivers | |
parent | 08dd7b2bf240c262ff585546115e8a96ca5b52d7 (diff) | |
download | barebox-bbc499914739cd51f51cd23ade0a2dca19359804.tar.gz barebox-bbc499914739cd51f51cd23ade0a2dca19359804.tar.xz |
gpiolib: Add code to support "active low" GPIOs
So far this particular aspect of various DT-bindings has been handled
on a per-driver basis. With this change, hopefully, we'll have a
single place to handle necessary logic inversions and eventually
would be able to migrate existing users as well as avoiding adding
redundant code to new drivers.
Cc: cphealy@gmail.com
Cc: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/gpio/gpiolib.c | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 1f57c76ec1..28f6a5ebfd 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -13,6 +13,7 @@ static LIST_HEAD(chip_list); struct gpio_info { struct gpio_chip *chip; bool requested; + bool active_low; char *label; }; @@ -45,6 +46,15 @@ static struct gpio_info *gpio_to_desc(unsigned gpio) return NULL; } +static int gpio_adjust_value(struct gpio_info *gi, + int value) +{ + if (value < 0) + return value; + + return !!value ^ gi->active_low; +} + int gpio_request(unsigned gpio, const char *label) { struct gpio_info *gi = gpio_to_desc(gpio); @@ -69,6 +79,7 @@ int gpio_request(unsigned gpio, const char *label) } gi->requested = true; + gi->active_low = false; gi->label = xstrdup(label); done: @@ -93,6 +104,7 @@ void gpio_free(unsigned gpio) gi->chip->ops->free(gi->chip, gpio - gi->chip->base); gi->requested = false; + gi->active_low = false; free(gi->label); gi->label = NULL; } @@ -111,11 +123,20 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) if (err) return err; - if (flags & GPIOF_DIR_IN) + if (flags & GPIOF_ACTIVE_LOW) { + struct gpio_info *gi = gpio_to_desc(gpio); + gi->active_low = true; + } + + if (flags & GPIOF_DIR_IN) { err = gpio_direction_input(gpio); - else + } else if (flags & GPIOF_LOGICAL) { + err = gpio_direction_active(gpio, + !!(flags & GPIOF_INIT_ACTIVE)); + } else { err = gpio_direction_output(gpio, - (flags & GPIOF_INIT_HIGH) ? 1 : 0); + !!(flags & GPIOF_INIT_HIGH)); + } if (err) goto free_gpio; @@ -178,6 +199,13 @@ void gpio_set_value(unsigned gpio, int value) } EXPORT_SYMBOL(gpio_set_value); +void gpio_set_active(unsigned gpio, bool value) +{ + struct gpio_info *gi = gpio_to_desc(gpio); + gpio_set_value(gpio, gpio_adjust_value(gi, value)); +} +EXPORT_SYMBOL(gpio_set_active); + int gpio_get_value(unsigned gpio) { struct gpio_info *gi = gpio_to_desc(gpio); @@ -196,6 +224,13 @@ int gpio_get_value(unsigned gpio) } EXPORT_SYMBOL(gpio_get_value); +int gpio_is_active(unsigned gpio) +{ + struct gpio_info *gi = gpio_to_desc(gpio); + return gpio_adjust_value(gi, gpio_get_value(gpio)); +} +EXPORT_SYMBOL(gpio_is_active); + int gpio_direction_output(unsigned gpio, int value) { struct gpio_info *gi = gpio_to_desc(gpio); @@ -215,6 +250,13 @@ int gpio_direction_output(unsigned gpio, int value) } EXPORT_SYMBOL(gpio_direction_output); +int gpio_direction_active(unsigned gpio, bool value) +{ + struct gpio_info *gi = gpio_to_desc(gpio); + return gpio_direction_output(gpio, gpio_adjust_value(gi, value)); +} +EXPORT_SYMBOL(gpio_direction_active); + int gpio_direction_input(unsigned gpio) { struct gpio_info *gi = gpio_to_desc(gpio); @@ -334,7 +376,7 @@ static int do_gpiolib(int argc, char *argv[]) printf(" GPIO %*d: %*s %*s %*s %s\n", 4, i, 3, (dir < 0) ? "unk" : ((dir == GPIOF_DIR_IN) ? "in" : "out"), 3, (val < 0) ? "unk" : ((val == 0) ? "lo" : "hi"), - 9, gi->requested ? "true" : "false", + 12, gi->requested ? (gi->active_low ? "active low" : "true") : "false", (gi->requested && gi->label) ? gi->label : ""); } |