Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Pufan Jin <cyan2254650260@gmail.com>
To: Bartosz Golaszewski <brgl@kernel.org>,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	Linus Walleij <linusw@kernel.org>,
	Philipp Zabel <p.zabel@pengutronix.de>
Cc: Krzysztof Kozlowski <krzk@kernel.org>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args()
Date: Sat,  5 Dec 2026 14:43:51 +0800	[thread overview]
Message-ID: <20261205064355.137581-2-cyan2254650260@gmail.com> (raw)
In-Reply-To: <20261205064355.137581-1-cyan2254650260@gmail.com>

Callers such as drivers/reset/core.c receive parsed
struct fwnode_reference_args referencing a GPIO controller and need the
matching struct gpio_device, chip-relative hardware offset, and Linux
lookup flags without calling gpio_device_get_chip() or chip->of_xlate()
outside gpiolib.

Add gpio_device_find_by_fwnode_args() for OF and software nodes:

- For OF nodes, match and translate inside gpio_device_find() under
  gdev->srcu. Return -EPROBE_DEFER if no controller matches the node, or
  -EINVAL or the negative errno from of_xlate() if the specifier is
  rejected. Convert OF flags via of_convert_gpio_flags().
- For software nodes, resolve the device via swnode_get_gpio_device()
  and validate the 2-cell offset and flags reference against
  gdev->ngpio.

Signed-off-by: Pufan Jin <cyan2254650260@gmail.com>
---
 drivers/gpio/gpiolib-of.c     | 93 +++++++++++++++++++++++++++++++++--
 drivers/gpio/gpiolib-of.h     | 10 ++++
 drivers/gpio/gpiolib-swnode.c | 24 +++++++++
 drivers/gpio/gpiolib-swnode.h |  6 +++
 drivers/gpio/gpiolib.c        | 32 ++++++++++++
 include/linux/gpio/driver.h   | 12 +++++
 6 files changed, 172 insertions(+), 5 deletions(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index ea1dfc3a31..0fa973658a 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -721,6 +721,25 @@ struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id,
 	return desc;
 }
 
+static int
+of_fwnode_args_to_of_phandle_args(const struct fwnode_reference_args *src,
+				  struct of_phandle_args *dst)
+{
+	if (src->nargs > MAX_PHANDLE_ARGS)
+		return -EINVAL;
+
+	dst->np = to_of_node(src->fwnode);
+	dst->args_count = src->nargs;
+
+	for (int i = 0; i < dst->args_count; i++) {
+		if (src->args[i] > U32_MAX)
+			return -EINVAL;
+		dst->args[i] = src->args[i];
+	}
+
+	return 0;
+}
+
 struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip,
 					   struct fwnode_reference_args *gpiospec,
 					   unsigned long *lflags)
@@ -728,12 +747,11 @@ struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip,
 	enum of_gpio_flags xlate_flags = 0;
 	struct of_phandle_args args;
 	struct gpio_desc *desc;
+	int ret;
 
-	args.np = to_of_node(gpiospec->fwnode);
-	args.args_count = gpiospec->nargs;
-
-	for (int i = 0; i < args.args_count; i++)
-		args.args[i] = gpiospec->args[i];
+	ret = of_fwnode_args_to_of_phandle_args(gpiospec, &args);
+	if (ret)
+		return ERR_PTR(ret);
 
 	desc = of_xlate_and_get_gpiod_flags(chip, &args, &xlate_flags);
 	if (IS_ERR(desc))
@@ -744,6 +762,71 @@ struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip,
 	return desc;
 }
 
+struct of_gpio_xlate_ctx {
+	const struct of_phandle_args *gpiospec;
+	unsigned int offset;
+	enum of_gpio_flags of_flags;
+	int err;
+};
+
+static int of_gpiochip_match_and_xlate(struct gpio_chip *gc, const void *data)
+{
+	struct of_gpio_xlate_ctx *ctx = (void *)data;
+	enum of_gpio_flags of_flags = 0;
+	int ret;
+
+	if (!device_match_of_node(&gc->gpiodev->dev, ctx->gpiospec->np))
+		return 0;
+
+	if (!gc->of_xlate || gc->of_gpio_n_cells != ctx->gpiospec->args_count) {
+		ctx->err = -EINVAL;
+		return 0;
+	}
+
+	ret = gc->of_xlate(gc, ctx->gpiospec, &of_flags);
+	if (ret < 0) {
+		ctx->err = ret;
+		return 0;
+	}
+
+	if (ret >= gc->ngpio) {
+		ctx->err = -EINVAL;
+		return 0;
+	}
+
+	ctx->offset = ret;
+	ctx->of_flags = of_flags;
+	return 1;
+}
+
+struct gpio_device *
+of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				   unsigned int *offset, unsigned long *lflags)
+{
+	struct of_phandle_args gpiospec;
+	struct of_gpio_xlate_ctx ctx = {
+		.gpiospec = &gpiospec,
+		.err = -EPROBE_DEFER,
+	};
+	struct gpio_device *gdev;
+	int ret;
+
+	ret = of_fwnode_args_to_of_phandle_args(args, &gpiospec);
+	if (ret)
+		return ERR_PTR(ret);
+
+	gdev = gpio_device_find(&ctx, of_gpiochip_match_and_xlate);
+	if (!gdev)
+		return ERR_PTR(ctx.err);
+
+	if (offset)
+		*offset = ctx.offset;
+	if (lflags)
+		*lflags = of_convert_gpio_flags(ctx.of_flags);
+
+	return gdev;
+}
+
 #ifdef CONFIG_OF_DYNAMIC
 /**
  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
diff --git a/drivers/gpio/gpiolib-of.h b/drivers/gpio/gpiolib-of.h
index 347b82f9e3..e9bc405b6d 100644
--- a/drivers/gpio/gpiolib-of.h
+++ b/drivers/gpio/gpiolib-of.h
@@ -28,6 +28,9 @@ int of_gpio_count(const struct fwnode_handle *fwnode, const char *con_id);
 struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip,
 					   struct fwnode_reference_args *gpiospec,
 					   unsigned long *lflags);
+struct gpio_device *
+of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				   unsigned int *offset, unsigned long *lflags);
 #else
 static inline struct gpio_desc *of_find_gpio(struct device_node *np,
 					     const char *con_id,
@@ -55,6 +58,13 @@ of_gpiochip_get_hog_desc(struct gpio_chip *chip,
 {
 	return ERR_PTR(-ENOENT);
 }
+
+static inline struct gpio_device *
+of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				   unsigned int *offset, unsigned long *lflags)
+{
+	return ERR_PTR(-ENOENT);
+}
 #endif /* CONFIG_OF_GPIO */
 
 extern struct notifier_block gpio_of_notifier;
diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c
index 304994c5c7..c9b5b74fdd 100644
--- a/drivers/gpio/gpiolib-swnode.c
+++ b/drivers/gpio/gpiolib-swnode.c
@@ -135,6 +135,30 @@ int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id)
 	return -ENOENT;
 }
 
+struct gpio_device *
+swnode_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				       unsigned int *offset,
+				       unsigned long *lflags)
+{
+	if (args->nargs != 2)
+		return ERR_PTR(-EINVAL);
+
+	struct gpio_device *gdev __free(gpio_device_put) =
+		swnode_get_gpio_device(args->fwnode);
+	if (IS_ERR(gdev))
+		return ERR_CAST(gdev);
+
+	if (args->args[0] >= gdev->ngpio)
+		return ERR_PTR(-EINVAL);
+
+	if (offset)
+		*offset = args->args[0];
+	if (lflags)
+		*lflags = args->args[1];
+
+	return no_free_ptr(gdev);
+}
+
 #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED)
 /*
  * A special node that identifies undefined GPIOs, this is primarily used as
diff --git a/drivers/gpio/gpiolib-swnode.h b/drivers/gpio/gpiolib-swnode.h
index af849e56f6..0e0c5f6217 100644
--- a/drivers/gpio/gpiolib-swnode.h
+++ b/drivers/gpio/gpiolib-swnode.h
@@ -4,11 +4,17 @@
 #define GPIOLIB_SWNODE_H
 
 struct fwnode_handle;
+struct fwnode_reference_args;
 struct gpio_desc;
+struct gpio_device;
 
 struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
 				   const char *con_id, unsigned int idx,
 				   unsigned long *flags);
 int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id);
+struct gpio_device *
+swnode_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				       unsigned int *offset,
+				       unsigned long *lflags);
 
 #endif /* GPIOLIB_SWNODE_H */
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 46a68a6b73..a1da937fab 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1587,6 +1587,38 @@ struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnod
 }
 EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode);
 
+/**
+ * gpio_device_find_by_fwnode_args() - find a GPIO device and translate a
+ *                                     firmware node reference
+ * @args: Firmware reference arguments describing the GPIO specifier
+ * @offset: Optional pointer to store the chip-relative hardware GPIO offset
+ * @lflags: Optional pointer to store the Linux GPIO lookup flags
+ *
+ * Context: Process context. May sleep.
+ *
+ * Returns:
+ * Reference to the matching struct gpio_device on success, which must be
+ * released with gpio_device_put(), or ERR_PTR() on error: %-EPROBE_DEFER if
+ * no controller matching @args->fwnode is registered yet, %-EINVAL if @args
+ * or the specifier is invalid, or %-ENOENT if @args->fwnode is unsupported.
+ */
+struct gpio_device *
+gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				unsigned int *offset, unsigned long *lflags)
+{
+	if (!args || !args->fwnode)
+		return ERR_PTR(-EINVAL);
+
+	if (is_of_node(args->fwnode))
+		return of_gpio_device_find_by_fwnode_args(args, offset, lflags);
+
+	if (is_software_node(args->fwnode))
+		return swnode_gpio_device_find_by_fwnode_args(args, offset, lflags);
+
+	return ERR_PTR(-ENOENT);
+}
+EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode_args);
+
 /**
  * gpio_device_get() - Increase the reference count of this GPIO device
  * @gdev: GPIO device to increase the refcount for
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 4077dc678c..057c2c3016 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -23,6 +23,7 @@
 #include "defs.h"
 
 struct device;
+struct fwnode_reference_args;
 struct irq_chip;
 struct irq_data;
 struct module;
@@ -836,6 +837,9 @@ const char *gpio_device_get_label(struct gpio_device *gdev);
 
 struct gpio_device *gpio_device_find_by_label(const char *label);
 struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode);
+struct gpio_device *
+gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				unsigned int *offset, unsigned long *lflags);
 
 #else /* CONFIG_GPIOLIB */
 
@@ -878,6 +882,14 @@ static inline struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode
 	return NULL;
 }
 
+static inline struct gpio_device *
+gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
+				unsigned int *offset, unsigned long *lflags)
+{
+	WARN_ON(1);
+	return ERR_PTR(-ENODEV);
+}
+
 static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
 				       unsigned int offset)
 {
-- 
2.56.1


  reply	other threads:[~2026-12-05  6:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
2026-12-05  6:43 ` Pufan Jin [this message]
2026-12-05  6:43 ` [PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device Pufan Jin
2026-12-05  6:43 ` [PATCH v2 3/5] gpiolib: shared: match reset-gpio against translated entry offset Pufan Jin
2026-12-05  6:43 ` [PATCH v2 4/5] reset: gpio: derive fwnode_reset_n_cells from platform data Pufan Jin
2026-12-05  6:43 ` [PATCH v2 5/5] reset: core: translate GPIO specifier via gpio_device_find_by_fwnode_args() Pufan Jin

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=20261205064355.137581-2-cyan2254650260@gmail.com \
    --to=cyan2254650260@gmail.com \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=krzk@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=p.zabel@pengutronix.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