U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Samuel Holland <samuel@sholland.org>
To: u-boot@lists.denx.de
Cc: Samuel Holland <samuel@sholland.org>,
	Dario Binacchi <dariobin@libero.it>,
	Jean-Jacques Hiblot <jjhiblot@ti.com>,
	Patrick Delaunay <patrick.delaunay@foss.st.com>,
	Pratyush Yadav <p.yadav@ti.com>, Simon Glass <sjg@chromium.org>
Subject: [PATCH 3/3] gpio: Factor out DT flag translation
Date: Sat, 11 Sep 2021 17:05:53 -0500	[thread overview]
Message-ID: <20210911220553.9427-4-samuel@sholland.org> (raw)
In-Reply-To: <20210911220553.9427-1-samuel@sholland.org>

The generic GPIO flags binding is shared across many drivers, some of
which need their own xlate function. Factor out the flag translation
code from gpio_xlate_offs_flags so it does not need to be duplicated.

Signed-off-by: Samuel Holland <samuel@sholland.org>
---

 drivers/gpio/gpio-uclass.c | 50 ++++++++++++++++++++++----------------
 include/asm-generic/gpio.h |  8 ++++++
 2 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c
index 6f2b1adfdec..b88b4290a45 100644
--- a/drivers/gpio/gpio-uclass.c
+++ b/drivers/gpio/gpio-uclass.c
@@ -185,6 +185,34 @@ int gpio_lookup_name(const char *name, struct udevice **devp,
 	return 0;
 }
 
+unsigned long gpio_flags_xlate(uint32_t arg)
+{
+	unsigned long flags = 0;
+
+	if (arg & GPIO_ACTIVE_LOW)
+		flags |= GPIOD_ACTIVE_LOW;
+
+	/*
+	 * need to test 2 bits for gpio output binding:
+	 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
+	 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
+	 */
+	if (arg & GPIO_SINGLE_ENDED) {
+		if (arg & GPIO_LINE_OPEN_DRAIN)
+			flags |= GPIOD_OPEN_DRAIN;
+		else
+			flags |= GPIOD_OPEN_SOURCE;
+	}
+
+	if (arg & GPIO_PULL_UP)
+		flags |= GPIOD_PULL_UP;
+
+	if (arg & GPIO_PULL_DOWN)
+		flags |= GPIOD_PULL_DOWN;
+
+	return flags;
+}
+
 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
 			  struct ofnode_phandle_args *args)
 {
@@ -200,27 +228,7 @@ int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
 	if (args->args_count < 2)
 		return 0;
 
-	desc->flags = 0;
-	if (args->args[1] & GPIO_ACTIVE_LOW)
-		desc->flags |= GPIOD_ACTIVE_LOW;
-
-	/*
-	 * need to test 2 bits for gpio output binding:
-	 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
-	 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
-	 */
-	if (args->args[1] & GPIO_SINGLE_ENDED) {
-		if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
-			desc->flags |= GPIOD_OPEN_DRAIN;
-		else
-			desc->flags |= GPIOD_OPEN_SOURCE;
-	}
-
-	if (args->args[1] & GPIO_PULL_UP)
-		desc->flags |= GPIOD_PULL_UP;
-
-	if (args->args[1] & GPIO_PULL_DOWN)
-		desc->flags |= GPIOD_PULL_DOWN;
+	desc->flags = gpio_flags_xlate(args->args[1]);
 
 	return 0;
 }
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
index e33cde7abdd..911b11bc389 100644
--- a/include/asm-generic/gpio.h
+++ b/include/asm-generic/gpio.h
@@ -221,6 +221,14 @@ int gpio_requestf(unsigned gpio, const char *fmt, ...)
 
 struct fdtdec_phandle_args;
 
+/**
+ * gpio_flags_xlate() - convert DT flags to internal flags
+ *
+ * This routine converts the GPIO_* flags from the generic DT binding to the
+ * GPIOD_* flags used internally. It can be called from driver xlate functions.
+ */
+unsigned long gpio_flags_xlate(uint32_t arg);
+
 /**
  * gpio_xlate_offs_flags() - implementation for common use of dm_gpio_ops.xlate
  *
-- 
2.31.1


  parent reply	other threads:[~2021-09-11 22:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11 22:05 [PATCH 0/3] gpio: uclass enhancements for DM_GPIO drivers Samuel Holland
2021-09-11 22:05 ` [PATCH 1/3] gpio: Verify validity of pin offsets when looking up names Samuel Holland
2021-09-30  4:08   ` Simon Glass
2021-10-05 22:02   ` Tom Rini
2021-09-11 22:05 ` [PATCH 2/3] gpio: Verify validity of pin offsets from device trees Samuel Holland
2021-09-30  4:08   ` Simon Glass
2021-10-05 22:02   ` Tom Rini
2021-10-05 22:02   ` Tom Rini
2021-09-11 22:05 ` Samuel Holland [this message]
2021-09-30  4:08   ` [PATCH 3/3] gpio: Factor out DT flag translation Simon Glass
2021-10-05 22:02   ` Tom Rini

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=20210911220553.9427-4-samuel@sholland.org \
    --to=samuel@sholland.org \
    --cc=dariobin@libero.it \
    --cc=jjhiblot@ti.com \
    --cc=p.yadav@ti.com \
    --cc=patrick.delaunay@foss.st.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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