mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/4] gpiolib: implement dev_gpiod_get_index
Date: Mon,  9 Jan 2023 16:58:35 +0100	[thread overview]
Message-ID: <20230109155836.2220277-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230109155836.2220277-1-a.fatoum@pengutronix.de>

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




  parent reply	other threads:[~2023-01-09 16:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=20230109155836.2220277-3-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