gpio: add flags argument to gpiod_get*() functions
Subsystems (2)
PATCH 1/1 gpio: add flags argument to gpiod_get*() functions
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the gpiod_get() functions. The integer GPIO API had gpio_request_one() that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly.
A similar feature has been requested for the gpiod API. Since setting the direction of a GPIO is so often the very next action done after obtaining its descriptor, we prefer to extend the existing functions instead of introducing new functions that would raise the number of gpiod getters to 16 (!).
The drawback of this approach is that all gpiod clients need to be updated. To limit the pain, temporary macros are introduced that allow gpiod_get*() to be called with or without the extra flags argument. They will be removed once all consumer code has been updated.
---
This dude can be applied harmlessly to the GPIO tree - then I will go after every gpiod user to update the calls to gpiod_get*() before removing the macros in consumer.h.
Formatting is not fixed everywhere but this is on purpose, to limit the amount of noise in the temporary changes.
Documentation/gpio/consumer.txt | 26 ++++++++++--- drivers/gpio/devres.c | 40 ++++++++++++-------- drivers/gpio/gpiolib.c | 67 +++++++++++++++++++++++----------- include/linux/gpio/consumer.h | 81 +++++++++++++++++++++++++++++++++-------- 4 files changed, 155 insertions(+), 59 deletions(-)
Documentation/gpio/consumer.txt +20 -6
@@ -29,13 +29,24 @@ gpiod_get() functions. Like many other kernel subsystems, gpiod_get() takes the device that will use the GPIO and the function the requested GPIO is supposed to fulfill: - struct gpio_desc *gpiod_get(struct device *dev, const char *con_id) + struct gpio_desc *gpiod_get(struct device *dev, const char *con_id, + enum gpiod_flags flags) If a function is implemented by using several GPIOs together (e.g. a simple LED device that displays digits), an additional index argument can be specified: struct gpio_desc *gpiod_get_index(struct device *dev, - const char *con_id, unsigned int idx) + const char *con_id, unsigned int idx, + enum gpiod_flags flags) + +The flags parameter is used to optionally specify a direction and initial value +for the GPIO. Values can be: + +* GPIOD_ASIS or 0 to not initialize the GPIO at all. The direction must be set + later with one of the dedicated functions. +* GPIOD_IN to initialize the GPIO as input. +* GPIOD_OUT_LOW to initialize the GPIO as output with a value of 0. +* GPIOD_OUT_HIGH to initialize the GPIO as output with a value of 1. Both functions return either a valid GPIO descriptor, or an error code checkable with IS_ERR() (they will never return a NULL pointer). -ENOENT will be returned
@@ -46,11 +57,13 @@ errors and an absence of GPIO for optional GPIO parameters. Device-managed variants of these functions are also defined: - struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id) + struct gpio_desc *devm_gpiod_get(struct device *dev, const char *con_id, + enum gpiod_flags flags) struct gpio_desc *devm_gpiod_get_index(struct device *dev, const char *con_id, - unsigned int idx) + unsigned int idx, + enum gpiod_flags flags) A GPIO descriptor can be disposed of using the gpiod_put() function:
@@ -67,8 +80,9 @@ Using GPIOs Setting Direction ----------------- -The first thing a driver must do with a GPIO is setting its direction. This is -done by invoking one of the gpiod_direction_*() functions: +The first thing a driver must do with a GPIO is setting its direction. If no +direction-setting flags have been given to gpiod_get*(), this is done by +invoking one of the gpiod_direction_*() functions: int gpiod_direction_input(struct gpio_desc *desc) int gpiod_direction_output(struct gpio_desc *desc, int value)
drivers/gpio/devres.c +24 -16
@@ -39,47 +39,53 @@ static int devm_gpiod_match(struct device *dev, void *res, void *data)* devm_gpiod_get - Resource-managed gpiod_get() * @dev: GPIO consumer * @con_id: function within the GPIO consumer +* @flags: optional GPIO initialization flags * * Managed gpiod_get(). GPIO descriptors returned from this function are * automatically disposed on driver detach. See gpiod_get() for detailed * information about behavior and return values. */ -struct gpio_desc *__must_check devm_gpiod_get (struct device *dev, -const char *con_id) +struct gpio_desc *__must_check __devm_gpiod_get (struct device *dev, +const char *con_id, +enum gpiod_flags flags) { -return devm_gpiod_get_index (dev, con_id,0 ); +return devm_gpiod_get_index (dev, con_id,0 , flags); } -EXPORT_SYMBOL (devm_gpiod_get); +EXPORT_SYMBOL (__devm_gpiod_get);/** * devm_gpiod_get_optional - Resource-managed gpiod_get_optional() * @dev: GPIO consumer * @con_id: function within the GPIO consumer +* @flags: optional GPIO initialization flags * * Managed gpiod_get_optional(). GPIO descriptors returned from this function * are automatically disposed on driver detach. See gpiod_get_optional() for * detailed information about behavior and return values. */ -struct gpio_desc *__must_check devm_gpiod_get_optional (struct device *dev, -const char *con_id) +struct gpio_desc *__must_check __devm_gpiod_get_optional (struct device *dev, +const char *con_id, +enum gpiod_flags flags) { -return devm_gpiod_get_index_optional (dev, con_id,0 ); +return devm_gpiod_get_index_optional (dev, con_id,0 , flags); } -EXPORT_SYMBOL (devm_gpiod_get_optional); +EXPORT_SYMBOL (__devm_gpiod_get_optional);/** * devm_gpiod_get_index - Resource-managed gpiod_get_index() * @dev: GPIO consumer * @con_id: function within the GPIO consumer * @idx: index of the GPIO to obtain in the consumer +* @flags: optional GPIO initialization flags * * Managed gpiod_get_index(). GPIO descriptors returned from this function are * automatically disposed on driver detach. See gpiod_get_index() for detailed * information about behavior and return values. */ -struct gpio_desc *__must_check devm_gpiod_get_index (struct device *dev, +struct gpio_desc *__must_check __devm_gpiod_get_index (struct device *dev,const char *con_id, -unsigned int idx) +unsigned int idx, +enum gpiod_flags flags) {struct gpio_desc **dr;struct gpio_desc *desc;
@@ -89,7 +95,7 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,if (!dr)return ERR_PTR (-ENOMEM ); - desc =gpiod_get_index (dev, con_id, idx); + desc =gpiod_get_index (dev, con_id, idx, flags);if (IS_ERR (desc)) {devres_free (dr);return desc;
@@ -100,26 +106,28 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,return desc; } -EXPORT_SYMBOL (devm_gpiod_get_index); +EXPORT_SYMBOL (__devm_gpiod_get_index);/** * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() * @dev: GPIO consumer * @con_id: function within the GPIO consumer * @index: index of the GPIO to obtain in the consumer +* @flags: optional GPIO initialization flags * * Managed gpiod_get_index_optional(). GPIO descriptors returned from this * function are automatically disposed on driver detach. See * gpiod_get_index_optional() for detailed information about behavior and * return values. */ -struct gpio_desc *__must_check devm_gpiod_get_index_optional (struct device *dev, +struct gpio_desc *__must_check __devm_gpiod_get_index_optional (struct device *dev,const char *con_id, -unsigned int index) +unsigned int index, +enum gpiod_flags flags) {struct gpio_desc *desc; - desc =devm_gpiod_get_index (dev, con_id, index); + desc =devm_gpiod_get_index (dev, con_id, index, flags);if (IS_ERR (desc)) {if (PTR_ERR (desc) == -ENOENT )return NULL ;
@@ -127,7 +135,7 @@ struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,return desc; } -EXPORT_SYMBOL (devm_gpiod_get_index_optional); +EXPORT_SYMBOL (__devm_gpiod_get_index_optional);/** * devm_gpiod_put - Resource-managed gpiod_put()
drivers/gpio/gpiolib.c +46 -21
155 lines not on this page · raw message
include/linux/gpio/consumer.h +65 -16
95 lines not on this page · raw message
Signature · 1 line
2.0.2
Quoted 6 lines · Alexandre Courbot
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the
gpiod_get()functions. The integer GPIO API hadgpio_request_one()that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly.
Quoted 18 lines · Alexandre Courbot
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the
gpiod_get()functions. The integer GPIO API hadgpio_request_one()that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly.A similar feature has been requested for the gpiod API. Since setting the direction of a GPIO is so often the very next action done after obtaining its descriptor, we prefer to extend the existing functions instead of introducing new functions that would raise the number of gpiod getters to 16 (!).
The drawback of this approach is that all gpiod clients need to be updated. To limit the pain, temporary macros are introduced that allow gpiod_get*() to be called with or without the extra flags argument. They will be removed once all consumer code has been updated.
Quoted 5 lines
Signed-off-by Alexandre Courbot <[email protected]>---
This dude can be applied harmlessly to the GPIO tree - then I will go after every gpiod user to update the calls to gpiod_get*() before removing the macros inconsumer.h.
OK I trust you. Patch applied with Broonie's review tag.
Just so we don't forget how we should move forward: Alex what do you think about adding a drivers/gpio/TODO.TXT file outlining the overall plan of the gpiod refactoring and clean-up work?
Yours,
Linus Walleij
Quoted 20 lines · Linus Walleij
Quoted 18 lines · Alexandre Courbot
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the
gpiod_get()functions. The integer GPIO API hadgpio_request_one()that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly.A similar feature has been requested for the gpiod API. Since setting the direction of a GPIO is so often the very next action done after obtaining its descriptor, we prefer to extend the existing functions instead of introducing new functions that would raise the number of gpiod getters to 16 (!).
The drawback of this approach is that all gpiod clients need to be updated. To limit the pain, temporary macros are introduced that allow gpiod_get*() to be called with or without the extra flags argument. They will be removed once all consumer code has been updated.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Alexandre Courbot <[email protected]>---
This dude can be applied harmlessly to the GPIO tree - then I will go after every gpiod user to update the calls to gpiod_get*() before removing the macros inconsumer.h.OK I trust you. Patch applied with Broonie's review tag.
Thanks! Unfortunately it is still not in -next due to a build error...
Quoted 3 lines
Just so we don't forget how we should move forward: Alex what do you think about adding a
drivers/gpio/TODO.TXTfile outlining the overall plan of the gpiod refactoring and clean-up work?
I have such a file locally - I'm not sure if checking it into the kernel tree is relevant though. Sounds more like the task of a wiki page.
Quoted 31 lines · Alexandre Courbot
Quoted 20 lines · Linus Walleij
Quoted 18 lines · Alexandre Courbot
The huge majority of GPIOs have their direction and initial value set right after being obtained by one of the
gpiod_get()functions. The integer GPIO API hadgpio_request_one()that took a convenience flags parameter allowing to specify an direction and value applied to the returned GPIO. This feature greatly simplifies client code and ensures errors are always handled properly.A similar feature has been requested for the gpiod API. Since setting the direction of a GPIO is so often the very next action done after obtaining its descriptor, we prefer to extend the existing functions instead of introducing new functions that would raise the number of gpiod getters to 16 (!).
The drawback of this approach is that all gpiod clients need to be updated. To limit the pain, temporary macros are introduced that allow gpiod_get*() to be called with or without the extra flags argument. They will be removed once all consumer code has been updated.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Alexandre Courbot <[email protected]>---
This dude can be applied harmlessly to the GPIO tree - then I will go after every gpiod user to update the calls to gpiod_get*() before removing the macros inconsumer.h.OK I trust you. Patch applied with Broonie's review tag.
Thanks! Unfortunately it is still not in -next due to a build error...
Quoted 7 lines
Quoted 3 lines
Just so we don't forget how we should move forward: Alex what do you think about adding a
drivers/gpio/TODO.TXTfile outlining the overall plan of the gpiod refactoring and clean-up work?I have such a file locally - I'm not sure if checking it into the kernel tree is relevant though. Sounds more like the task of a wiki page.
FWIW I have posted a small list of stuff I intent to do shortly:
https://gist.github.com/Gnurou/a62915acbfe0d0d4a671