From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 06/11] gpiolib: store all OF flags into GPIO descriptor
Date: Fri, 9 Aug 2024 16:24:00 +0200 [thread overview]
Message-ID: <20240809142405.315244-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240809142405.315244-1-a.fatoum@pengutronix.de>
The last cell in GPIO OF description usually only holds either
GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW, but some device trees also contain
extra flags like GPIO_PULL_UP or GPIO_OPEN_DRAIN.
To prepare acting on these flags, store all of them into the GPIO descriptor.
Like Linux, we have two kinds of flags, lookup flags from DT and request
flags from the drivers. To avoid confusion, we give gpiodesc_request_one
two parameters for each and have that function worry about combining
them.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/gpio/gpiolib.c | 45 ++++++++++++++++++++++++------------------
1 file changed, 26 insertions(+), 19 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 0c6c0fcc1aeb..539fd3c7c25f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -504,8 +504,20 @@ int gpio_direction_input(unsigned gpio)
}
EXPORT_SYMBOL(gpio_direction_input);
-static int gpiodesc_request_one(struct gpio_desc *desc, unsigned long flags,
- const char *label)
+/**
+ * gpiodesc_request_one - request GPIO descriptor
+ * @desc: GPIO descriptor
+ * @lflags: OF lookup flags for this GPIO or 0 if default
+ * such as GPIOF_ACTIVE_LOW
+ * @dflags: descriptor request flags (GPIOF_*) for this GPIO or 0 if default
+ *
+ * Function is used to request a GPIO descriptor and configure it for use.
+ *
+ * Returns:
+ * 0 on success and a negative error code otherwise.
+ */
+static int gpiodesc_request_one(struct gpio_desc *desc, unsigned long lflags,
+ unsigned long dflags, const char *label)
{
int err;
@@ -513,16 +525,17 @@ static int gpiodesc_request_one(struct gpio_desc *desc, unsigned long flags,
* Not all of the flags below are mulit-bit, but, for the sake
* of consistency, the code is written as if all of them were.
*/
- const bool active_low = (flags & GPIOF_ACTIVE_LOW) == GPIOF_ACTIVE_LOW;
- const bool dir_in = (flags & GPIOF_DIR_IN) == GPIOF_DIR_IN;
- const bool logical = (flags & GPIOF_LOGICAL) == GPIOF_LOGICAL;
- const bool init_active = (flags & GPIOF_INIT_ACTIVE) == GPIOF_INIT_ACTIVE;
- const bool init_high = (flags & GPIOF_INIT_HIGH) == GPIOF_INIT_HIGH;
+ const bool active_low = (dflags & GPIOF_ACTIVE_LOW) == GPIOF_ACTIVE_LOW;
+ const bool dir_in = (dflags & GPIOF_DIR_IN) == GPIOF_DIR_IN;
+ const bool logical = (dflags & GPIOF_LOGICAL) == GPIOF_LOGICAL;
+ const bool init_active = (dflags & GPIOF_INIT_ACTIVE) == GPIOF_INIT_ACTIVE;
+ const bool init_high = (dflags & GPIOF_INIT_HIGH) == GPIOF_INIT_HIGH;
err = gpiodesc_request(desc, label);
if (err)
return err;
+ desc->flags |= lflags;
if (active_low)
desc->flags |= OF_GPIO_ACTIVE_LOW;
@@ -552,7 +565,7 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
if (!desc)
return -ENODEV;
- return gpiodesc_request_one(desc, flags, label);
+ return gpiodesc_request_one(desc, 0, flags, label);
}
EXPORT_SYMBOL_GPL(gpio_request_one);
@@ -656,7 +669,7 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
{
struct device_node *chip_np = chip->dev->of_node;
unsigned long flags = 0;
- u32 gpio_cells, gpio_num, gpio_flags;
+ u32 gpio_cells, gpio_num, of_flags;
int ret;
const char *name = NULL;
@@ -677,13 +690,10 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
return ret;
ret = of_property_read_u32_index(np, "gpios", idx * gpio_cells + 1,
- &gpio_flags);
+ &of_flags);
if (ret)
return ret;
- if (gpio_flags & OF_GPIO_ACTIVE_LOW)
- flags |= GPIOF_ACTIVE_LOW;
-
/*
* Note that, in order to be compatible with Linux, the code
* below interprets 'output-high' as to mean 'output-active'.
@@ -711,7 +721,7 @@ static int of_hog_gpio(struct device_node *np, struct gpio_chip *chip,
name = np->name;
return gpiodesc_request_one(gpiochip_get_desc(chip, gpio_num),
- flags, name);
+ of_flags, flags, name);
}
static int of_gpiochip_scan_hogs(struct gpio_chip *chip)
@@ -901,7 +911,7 @@ static struct property *of_find_gpio_property(struct device_node *np,
struct gpio_desc *dev_gpiod_get_index(struct device *dev,
struct device_node *np,
const char *con_id, int index,
- enum gpiod_flags flags,
+ enum gpiod_flags dflags,
const char *label)
{
struct gpio_desc *desc = NULL;
@@ -925,9 +935,6 @@ struct gpio_desc *dev_gpiod_get_index(struct device *dev,
desc = gpio_to_desc(gpio);
- if (of_flags & OF_GPIO_ACTIVE_LOW)
- flags |= GPIOF_ACTIVE_LOW;
-
buf = NULL;
if (!label) {
@@ -937,7 +944,7 @@ struct gpio_desc *dev_gpiod_get_index(struct device *dev,
label = dev_name(dev);
}
- ret = gpiodesc_request_one(desc, flags, label);
+ ret = gpiodesc_request_one(desc, of_flags, dflags, label);
free(buf);
return ret ? ERR_PTR(ret): desc;
--
2.39.2
next prev parent reply other threads:[~2024-08-09 14:24 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-09 14:23 [PATCH 00/11] gpiolib: add support for OF GPIO configuration binding Ahmad Fatoum
2024-08-09 14:23 ` [PATCH 01/11] gpio: make gpio.h header self-contained Ahmad Fatoum
2024-08-09 14:23 ` [PATCH 02/11] gpiolib: permit GPIO drivers to implement struct gpio_ops::set_config Ahmad Fatoum
2024-08-09 14:23 ` [PATCH 03/11] pinctrl: stm32: implement generic " Ahmad Fatoum
2024-08-09 14:23 ` [PATCH 04/11] gpiolib: turn request/active_low into bit flags Ahmad Fatoum
2024-08-09 14:23 ` [PATCH 05/11] gpiolib: don't use GPIO number API in of_hog_gpio Ahmad Fatoum
2024-08-09 14:24 ` Ahmad Fatoum [this message]
2024-08-09 14:24 ` [PATCH 07/11] gpiolib: add support for OF GPIO configuration binding Ahmad Fatoum
2024-08-09 14:24 ` [PATCH 08/11] gpiolib: use dev_gpiod_get_index device node argument throughout Ahmad Fatoum
2024-08-09 14:24 ` [PATCH 09/11] gpiolib: export function to get struct gpio_desc from index Ahmad Fatoum
2024-08-09 14:24 ` [PATCH 10/11] input: gpio_keys: switch to GPIO descriptor API Ahmad Fatoum
2024-08-09 14:24 ` [PATCH 11/11] input: gpio-keys: request with label in DT if available Ahmad Fatoum
2024-08-14 11:00 ` [PATCH 00/11] gpiolib: add support for OF GPIO configuration binding Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240809142405.315244-7-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox