* [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> @ 2023-01-09 15:58 Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 2/4] gpiolib: use signed int for gpio in gpiod_set_value Ahmad Fatoum ` (3 more replies) 0 siblings, 4 replies; 5+ messages in thread From: Ahmad Fatoum @ 2023-01-09 15:58 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum This header can't be used without types and functions declared in of.h, so include that first. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/of_gpio.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/of_gpio.h b/include/of_gpio.h index 9076c81e5462..30ff204baf9e 100644 --- a/include/of_gpio.h +++ b/include/of_gpio.h @@ -8,6 +8,8 @@ #ifndef __OF_GPIO_H #define __OF_GPIO_H +#include <of.h> + /* * This is Linux-specific flags. By default controllers' and Linux' mapping * match, but GPIO controllers are free to translate their own flags to -- 2.30.2 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/4] gpiolib: use signed int for gpio in gpiod_set_value 2023-01-09 15:58 [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Ahmad Fatoum @ 2023-01-09 15:58 ` Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 3/4] gpiolib: implement dev_gpiod_get_index Ahmad Fatoum ` (2 subsequent siblings) 3 siblings, 0 replies; 5+ messages in thread From: Ahmad Fatoum @ 2023-01-09 15:58 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum barebox gpio identifiers are always in the range of non-negative signed integers with negative values reserved for errors. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/gpiod.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/gpiod.h b/include/gpiod.h index 32180337dc2d..cc3fb966a010 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -23,7 +23,7 @@ enum gpiod_flags { /* returned gpio descriptor can be passed to any normal gpio_* function */ int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags); -static inline void gpiod_set_value(unsigned gpio, bool value) +static inline void gpiod_set_value(int gpio, bool value) { if (gpio != -ENOENT) gpio_direction_active(gpio, value); -- 2.30.2 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/4] gpiolib: implement dev_gpiod_get_index 2023-01-09 15:58 [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 2/4] gpiolib: use signed int for gpio in gpiod_set_value Ahmad Fatoum @ 2023-01-09 15:58 ` Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 4/4] gpiolib: implement gpiod_get_value Ahmad Fatoum 2023-01-10 14:53 ` [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Sascha Hauer 3 siblings, 0 replies; 5+ messages in thread From: Ahmad Fatoum @ 2023-01-09 15:58 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum Linux devm_gpiod_get_index is a superset of devm_gpiod_get. We already support gpiod_get to simplify kernel code porting, so reimplement it in terms of a new dev_gpiod_get_index to simplify porting kernel code using that as well. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- drivers/gpio/gpiolib.c | 23 ++++++++++++++--------- include/gpiod.h | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 773d07c1308f..4e8244b25f00 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -542,17 +542,20 @@ static const char *gpio_suffixes[] = { "gpio", }; +#ifdef CONFIG_OFDEVICE /* Linux compatibility helper: Get a GPIO descriptor from device tree */ -int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags) +int dev_gpiod_get_index(struct device *dev, + struct device_node *np, + const char *_con_id, int index, + enum gpiod_flags flags, + const char *label) { - struct device_node *np = dev->of_node; enum of_gpio_flags of_flags; - const char *label = dev_name(dev); char *buf = NULL, *con_id; int gpio; int ret, i; - if (!IS_ENABLED(CONFIG_OFDEVICE) || !dev->of_node) + if (!np) return -ENODEV; for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { @@ -564,7 +567,7 @@ int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags) if (!con_id) return -ENOMEM; - gpio = of_get_named_gpio_flags(np, con_id, 0, &of_flags); + gpio = of_get_named_gpio_flags(np, con_id, index, &of_flags); free(con_id); if (gpio_is_valid(gpio)) @@ -579,10 +582,11 @@ int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags) buf = NULL; - if (_con_id) { - label = buf = basprintf("%s-%s", dev_name(dev), _con_id); - if (!label) - return -ENOMEM; + if (!label) { + if (con_id) + label = buf = basprintf("%s-%s", dev_name(dev), _con_id); + else + label = dev_name(dev); } ret = gpio_request_one(gpio, flags, label); @@ -590,6 +594,7 @@ int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags) return ret ?: gpio; } +#endif int gpiochip_add(struct gpio_chip *chip) { diff --git a/include/gpiod.h b/include/gpiod.h index cc3fb966a010..f76cc98155e0 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -4,6 +4,7 @@ #include <gpio.h> #include <of_gpio.h> +#include <driver.h> /** * Optional flags that can be passed to one of gpiod_* to configure direction @@ -20,8 +21,41 @@ enum gpiod_flags { GPIOD_OUT_HIGH = GPIOF_OUT_INIT_ACTIVE, }; +#ifdef CONFIG_OFDEVICE + /* returned gpio descriptor can be passed to any normal gpio_* function */ -int gpiod_get(struct device *dev, const char *_con_id, enum gpiod_flags flags); +int dev_gpiod_get_index(struct device *dev, + struct device_node *np, + const char *_con_id, int index, + enum gpiod_flags flags, + const char *label); + +#else +static inline int dev_gpiod_get_index(struct device *dev, + struct device_node *np, + const char *_con_id, int index, + enum gpiod_flags flags, + const char *label) +{ + return -ENODEV; +} +#endif + +static inline int dev_gpiod_get(struct device *dev, + struct device_node *np, + const char *con_id, + enum gpiod_flags flags, + const char *label) +{ + return dev_gpiod_get_index(dev, np, con_id, 0, flags, label); +} + +static inline int gpiod_get(struct device *dev, + const char *_con_id, + enum gpiod_flags flags) +{ + return dev_gpiod_get(dev, dev->of_node, _con_id, flags, NULL); +} static inline void gpiod_set_value(int gpio, bool value) { -- 2.30.2 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 4/4] gpiolib: implement gpiod_get_value 2023-01-09 15:58 [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 2/4] gpiolib: use signed int for gpio in gpiod_set_value Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 3/4] gpiolib: implement dev_gpiod_get_index Ahmad Fatoum @ 2023-01-09 15:58 ` Ahmad Fatoum 2023-01-10 14:53 ` [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Sascha Hauer 3 siblings, 0 replies; 5+ messages in thread From: Ahmad Fatoum @ 2023-01-09 15:58 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum The gpiod_ family of kernel functions always assume the logical state for values (i.e. taking active low/high into account). We already have gpio_set_value, so add gpiod_get_value for symmetry. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/gpiod.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/gpiod.h b/include/gpiod.h index f76cc98155e0..8abf86db8682 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -63,4 +63,12 @@ static inline void gpiod_set_value(int gpio, bool value) gpio_direction_active(gpio, value); } +static inline int gpiod_get_value(int gpio) +{ + if (gpio < 0) + return gpio; + + return gpio_is_active(gpio); +} + #endif -- 2.30.2 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> 2023-01-09 15:58 [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Ahmad Fatoum ` (2 preceding siblings ...) 2023-01-09 15:58 ` [PATCH 4/4] gpiolib: implement gpiod_get_value Ahmad Fatoum @ 2023-01-10 14:53 ` Sascha Hauer 3 siblings, 0 replies; 5+ messages in thread From: Sascha Hauer @ 2023-01-10 14:53 UTC (permalink / raw) To: Ahmad Fatoum; +Cc: barebox On Mon, Jan 09, 2023 at 04:58:33PM +0100, Ahmad Fatoum wrote: > This header can't be used without types and functions declared in of.h, > so include that first. > > Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > --- > include/of_gpio.h | 2 ++ > 1 file changed, 2 insertions(+) Applied, thanks Sascha > > diff --git a/include/of_gpio.h b/include/of_gpio.h > index 9076c81e5462..30ff204baf9e 100644 > --- a/include/of_gpio.h > +++ b/include/of_gpio.h > @@ -8,6 +8,8 @@ > #ifndef __OF_GPIO_H > #define __OF_GPIO_H > > +#include <of.h> > + > /* > * This is Linux-specific flags. By default controllers' and Linux' mapping > * match, but GPIO controllers are free to translate their own flags to > -- > 2.30.2 > > > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-01-10 14:55 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-01-09 15:58 [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 2/4] gpiolib: use signed int for gpio in gpiod_set_value Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 3/4] gpiolib: implement dev_gpiod_get_index Ahmad Fatoum 2023-01-09 15:58 ` [PATCH 4/4] gpiolib: implement gpiod_get_value Ahmad Fatoum 2023-01-10 14:53 ` [PATCH 1/4] of: gpio: include <of.h> in <of_gpio.h> Sascha Hauer
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox