public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Jie Li <lj29312931@gmail.com>
To: wsa@kernel.org, linus.walleij@linaro.org
Cc: linux-i2c@vger.kernel.org, linux-gpio@vger.kernel.org,
	linux-kernel@vger.kernel.org, Jie Li <jie.i.li@nokia.com>
Subject: [PATCH v2 1/2] gpiolib: add gpiod_is_single_ended() helper
Date: Sun, 25 Jan 2026 20:51:22 +0100	[thread overview]
Message-ID: <20260125195123.248798-2-jie.i.li@nokia.com> (raw)
In-Reply-To: <20260125195123.248798-1-jie.i.li@nokia.com>

The direction of a single-ended (open-drain or open-source) GPIO line
cannot always be reliably determined by reading hardware registers.
In true open-drain implementations, the "high" state is achieved by
entering a high-impedance mode, which many hardware controllers report
as "input" even if the software intends to use it as an output.

This creates issues for consumer drivers (like I2C) that rely on
gpiod_get_direction() to decide if a line can be driven.

Introduce gpiod_is_single_ended() to allow consumers to check the
software configuration (GPIO_FLAG_OPEN_DRAIN/GPIO_FLAG_OPEN_SOURCE) of
a descriptor. This provides a robust way to identify lines that are
capable of being driven, regardless of their instantaneous hardware state.

Signed-off-by: Jie Li <jie.i.li@nokia.com>
---
 drivers/gpio/gpiolib.c        | 22 ++++++++++++++++++++++
 include/linux/gpio/consumer.h |  5 +++++
 2 files changed, 27 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 1578cf3a8c74..96c34bf65c7e 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -486,6 +486,28 @@ int gpiod_get_direction(struct gpio_desc *desc)
 }
 EXPORT_SYMBOL_GPL(gpiod_get_direction);
 
+/**
+ * gpiod_is_single_ended - check if the GPIO is configured as single-ended
+ * @desc: the GPIO descriptor to check
+ *
+ * Returns true if the GPIO is configured as either Open Drain or Open Source.
+ * In these modes, the direction of the line cannot always be reliably
+ * determined by reading hardware registers, as the "off" state (High-Z)
+ * is physically indistinguishable from an input state.
+ */
+int gpiod_is_single_ended(struct gpio_desc *desc)
+{
+	if (!desc)
+		return 0;
+
+	if (test_bit(GPIOD_FLAG_OPEN_DRAIN, &desc->flags) ||
+		test_bit(GPIOD_FLAG_OPEN_SOURCE, &desc->flags))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(gpiod_is_single_ended);
+
 /*
  * Add a new chip to the global chips list, keeping the list of chips sorted
  * by range(means [base, base + ngpio - 1]) order.
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index cafeb7a40ad1..c5847c8f66fe 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -109,6 +109,7 @@ void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
 
 int gpiod_get_direction(struct gpio_desc *desc);
+int gpiod_is_single_ended(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
@@ -335,6 +336,10 @@ static inline int gpiod_get_direction(const struct gpio_desc *desc)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
+static inline int gpiod_is_single_ended(struct gpio_desc *desc)
+{
+	return 0;
+}
 static inline int gpiod_direction_input(struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
-- 
2.43.0


  reply	other threads:[~2026-01-25 19:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-14 14:13 [PATCH v1 0/2] i2c: add support for forced SDA recovery Jie Li
2026-01-14 14:13 ` [PATCH v1 1/2] i2c: core: add "force-set-sda" flag for open-drain SDA without "FLAG_IS_OUT" bit Jie Li
2026-01-14 14:13 ` [PATCH v1 2/2] dt-bindings: i2c: add force-set-sda property Jie Li
2026-01-16 14:14   ` Krzysztof Kozlowski
2026-01-15  9:27 ` [PATCH v1 0/2] i2c: add support for forced SDA recovery Linus Walleij
2026-01-15 13:12   ` 李杰
2026-01-16 13:59     ` Linus Walleij
2026-01-25 19:51       ` [PATCH v2 0/2] i2c: improve bus recovery for single-ended GPIOs Jie Li
2026-01-25 19:51         ` Jie Li [this message]
2026-01-27  9:46           ` [PATCH v2 1/2] gpiolib: add gpiod_is_single_ended() helper Linus Walleij
2026-01-25 19:51         ` [PATCH v2 2/2] i2c: core: support recovery for single-ended GPIOs Jie Li
2026-01-27  9:47           ` Linus Walleij
2026-02-01 11:18             ` [PATCH v3 0/2] i2c: improve bus " Jie Li
2026-02-01 11:18               ` [PATCH v3 1/2] gpiolib: add gpiod_is_single_ended() helper Jie Li
2026-02-01 11:18               ` [PATCH v3 2/2] i2c: core: support recovery for single-ended GPIOs Jie Li
2026-05-04 12:14                 ` Wolfram Sang
2026-04-22 18:47               ` [PATCH v3 0/2] i2c: improve bus " 李杰

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=20260125195123.248798-2-jie.i.li@nokia.com \
    --to=lj29312931@gmail.com \
    --cc=jie.i.li@nokia.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wsa@kernel.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