mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib
@ 2026-12-05  6:43 Pufan Jin
  2026-12-05  6:43 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

As discussed on v1, drivers/reset/core.c should not call
gpio_device_get_chip() or chip->of_xlate() to parse reset-gpios
phandles.

This series moves fwnode GPIO specifier lookup and translation into
gpiolib:

- Patch 1 adds gpio_device_find_by_fwnode_args() for OF and software
  nodes, translating specifiers and flags in gpiolib.
- Patch 2 makes swnode_find_gpio() match a consumer's parent
  gpio_device when its fwnode matches, so multi-bank controllers sharing
  one fwnode resolve the bank selected by of_xlate().
- Patch 3 updates gpio_shared_dev_is_reset_gpio() to match the
  reset-gpio software node offset against entry->offset.
- Patch 4 makes reset-gpio read fwnode_reset_n_cells from platform_data.
- Patch 5 converts __reset_add_reset_gpio_device() to use
  gpio_device_find_by_fwnode_args(), removing the #gpio-cells == 2
  restriction for devicetree providers and the flag check in
  drivers/reset/core.c.

Changes since v1:
- Moved specifier translation from drivers/reset/core.c into gpiolib.
- Added support for non-2-cell devicetree GPIO bindings, multi-bank
  single-fwnode controllers, and OF flag conversion.
- Updated gpiolib-shared reset-gpio matching to use entry->offset.
- Derived fwnode_reset_n_cells from platform_data in reset-gpio.c.

Tested on NVIDIA Jetson TX2 with tegra186-gpio and BCM4354 Wi-Fi
reset-gpios, resolving to PN.00 / offset 85 with ACTIVE_LOW, and with
KUnit covering 2-cell of_xlate, multi-bank 3-cell reset-gpio,
invalid specifiers, and software node lookups.

Pufan Jin (5):
  gpiolib: add gpio_device_find_by_fwnode_args()
  gpiolib: swnode: prefer matching parent gpio_device
  gpiolib: shared: match reset-gpio against translated entry offset
  reset: gpio: derive fwnode_reset_n_cells from platform data
  reset: core: translate GPIO specifier via
    gpio_device_find_by_fwnode_args()

 drivers/gpio/gpiolib-of.c     | 93 +++++++++++++++++++++++++++++++++--
 drivers/gpio/gpiolib-of.h     | 10 ++++
 drivers/gpio/gpiolib-shared.c | 70 ++++++--------------------
 drivers/gpio/gpiolib-swnode.c | 39 +++++++++++++--
 drivers/gpio/gpiolib-swnode.h | 10 +++-
 drivers/gpio/gpiolib.c        | 80 +++++++++++++++++++++++++++---
 drivers/gpio/gpiolib.h        |  5 ++
 drivers/reset/core.c          | 37 ++------------
 drivers/reset/reset-gpio.c    |  6 ++-
 include/linux/gpio/driver.h   | 12 +++++
 10 files changed, 258 insertions(+), 104 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args()
  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
  2026-12-05  6:43 ` [PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device Pufan Jin
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device
  2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
  2026-12-05  6:43 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
@ 2026-12-05  6:43 ` Pufan Jin
  2026-12-05  6:43 ` [PATCH v2 3/5] gpiolib: shared: match reset-gpio against translated entry offset Pufan Jin
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

Multi-bank GPIO controllers such as gpio-spacemit-k1 register multiple
gpio_device instances sharing one fwnode, distinguished by 3-cell
bindings and of_node_instance_match().

When a child device such as reset-gpio is parented to a resolved
gpio_device and references that fwnode via PROPERTY_ENTRY_GPIO,
swnode_find_gpio() calls gpio_device_find_by_fwnode() and matches the
first registered bank instead of the parent bank.

Pass the consumer device into swnode_find_gpio(). When its parent is a
gpio_device matching the referenced fwnode, look up that parent
gpio_device instance and return -ENODEV if it is no longer registered.

Signed-off-by: Pufan Jin <cyan2254650260@gmail.com>
---
 drivers/gpio/gpiolib-swnode.c | 17 ++++++++++---
 drivers/gpio/gpiolib-swnode.h |  4 ++-
 drivers/gpio/gpiolib.c        | 48 ++++++++++++++++++++++++++++++-----
 drivers/gpio/gpiolib.h        |  5 ++++
 4 files changed, 62 insertions(+), 12 deletions(-)

diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c
index c9b5b74fdd..80ac7d85e7 100644
--- a/drivers/gpio/gpiolib-swnode.c
+++ b/drivers/gpio/gpiolib-swnode.c
@@ -7,6 +7,7 @@
 
 #define pr_fmt(fmt) "gpiolib: swnode: " fmt
 
+#include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/export.h>
@@ -23,7 +24,9 @@
 #include "gpiolib.h"
 #include "gpiolib-swnode.h"
 
-static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
+static struct gpio_device *
+swnode_get_gpio_device(struct device *consumer,
+		       const struct fwnode_handle *fwnode)
 {
 	const struct software_node *gdev_node;
 
@@ -40,6 +43,11 @@ static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode)
 		return ERR_PTR(-ENOENT);
 
 fwnode_lookup:
+	/* Prefer the parent gpio_device if it matches fwnode. */
+	if (consumer && gpio_device_matches_fwnode(consumer->parent, fwnode))
+		return gpio_device_find_by_dev_and_fwnode(consumer->parent, fwnode) ?:
+		       ERR_PTR(-ENODEV);
+
 	return gpio_device_find_by_fwnode(fwnode) ?: ERR_PTR(-EPROBE_DEFER);
 }
 
@@ -54,7 +62,8 @@ static int swnode_gpio_get_reference(const struct fwnode_handle *fwnode,
 	return fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, args);
 }
 
-struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
+struct gpio_desc *swnode_find_gpio(struct device *consumer,
+				   struct fwnode_handle *fwnode,
 				   const char *con_id, unsigned int idx,
 				   unsigned long *flags)
 {
@@ -88,7 +97,7 @@ struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
 	}
 
 	struct gpio_device *gdev __free(gpio_device_put) =
-					swnode_get_gpio_device(args.fwnode);
+					swnode_get_gpio_device(consumer, args.fwnode);
 	fwnode_handle_put(args.fwnode);
 	if (IS_ERR(gdev))
 		return ERR_CAST(gdev);
@@ -144,7 +153,7 @@ swnode_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args,
 		return ERR_PTR(-EINVAL);
 
 	struct gpio_device *gdev __free(gpio_device_put) =
-		swnode_get_gpio_device(args->fwnode);
+		swnode_get_gpio_device(NULL, args->fwnode);
 	if (IS_ERR(gdev))
 		return ERR_CAST(gdev);
 
diff --git a/drivers/gpio/gpiolib-swnode.h b/drivers/gpio/gpiolib-swnode.h
index 0e0c5f6217..1167bc6ad5 100644
--- a/drivers/gpio/gpiolib-swnode.h
+++ b/drivers/gpio/gpiolib-swnode.h
@@ -3,12 +3,14 @@
 #ifndef GPIOLIB_SWNODE_H
 #define GPIOLIB_SWNODE_H
 
+struct device;
 struct fwnode_handle;
 struct fwnode_reference_args;
 struct gpio_desc;
 struct gpio_device;
 
-struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode,
+struct gpio_desc *swnode_find_gpio(struct device *consumer,
+				   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);
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a1da937fab..eba1e2601f 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1558,20 +1558,54 @@ struct gpio_device *gpio_device_find_by_label(const char *label)
 }
 EXPORT_SYMBOL_GPL(gpio_device_find_by_label);
 
-static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
+bool gpio_device_matches_fwnode(struct device *dev,
+				const struct fwnode_handle *fwnode)
 {
-	struct device *dev = &gc->gpiodev->dev;
-	struct fwnode_handle *node = dev_fwnode(dev);
+	struct fwnode_handle *node;
 
-	if (IS_ERR(fwnode))
-		return 0;
+	if (!dev || dev->type != &gpio_dev_type || IS_ERR_OR_NULL(fwnode))
+		return false;
 
 	if (device_match_fwnode(dev, fwnode))
-		return 1;
+		return true;
 
+	node = dev_fwnode(dev);
 	return node && node->secondary == fwnode;
 }
 
+static int gpio_chip_match_by_fwnode(struct gpio_chip *gc, const void *fwnode)
+{
+	return gpio_device_matches_fwnode(&gc->gpiodev->dev, fwnode);
+}
+
+struct gpio_dev_fwnode_match {
+	struct device *dev;
+	const struct fwnode_handle *fwnode;
+};
+
+static int gpio_chip_match_by_dev_and_fwnode(struct gpio_chip *gc,
+					     const void *data)
+{
+	const struct gpio_dev_fwnode_match *m = data;
+
+	if (&gc->gpiodev->dev != m->dev)
+		return 0;
+
+	return gpio_device_matches_fwnode(m->dev, m->fwnode);
+}
+
+struct gpio_device *
+gpio_device_find_by_dev_and_fwnode(struct device *dev,
+				   const struct fwnode_handle *fwnode)
+{
+	struct gpio_dev_fwnode_match match = {
+		.dev = dev,
+		.fwnode = fwnode,
+	};
+
+	return gpio_device_find(&match, gpio_chip_match_by_dev_and_fwnode);
+}
+
 /**
  * gpio_device_find_by_fwnode() - wrapper around gpio_device_find() finding
  *                                the GPIO device by its fwnode
@@ -4869,7 +4903,7 @@ static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode,
 		desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags);
 	} else if (is_software_node(fwnode)) {
 		dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n", fwnode, name);
-		desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags);
+		desc = swnode_find_gpio(consumer, fwnode, con_id, idx, lookupflags);
 	}
 
 	return desc;
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index 8dd8ea747f..f001914f4b 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -282,6 +282,11 @@ int gpiochip_add_hog(struct gpio_chip *gc, struct fwnode_handle *fwnode);
 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev);
 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
 const char *gpiod_get_label(struct gpio_desc *desc);
+bool gpio_device_matches_fwnode(struct device *dev,
+				const struct fwnode_handle *fwnode);
+struct gpio_device *
+gpio_device_find_by_dev_and_fwnode(struct device *dev,
+				   const struct fwnode_handle *fwnode);
 
 /* With descriptor prefix */
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 3/5] gpiolib: shared: match reset-gpio against translated entry offset
  2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
  2026-12-05  6:43 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
  2026-12-05  6:43 ` [PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device Pufan Jin
@ 2026-12-05  6:43 ` 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
  4 siblings, 0 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

gpiochip_setup_shared() translates entry->offset via gc->of_xlate() when
an OF GPIO chip is registered.

gpio_shared_dev_is_reset_gpio() parses the consumer's devicetree
reset-gpios specifier and compares ref_args.args[0] with
aux_args.args[0] from the reset-gpio software node. When reset-gpio
stores the of_xlate()-translated hardware offset in its software node,
this comparison mismatches on controllers with custom of_xlate()
callbacks such as tegra186-gpio, and gpio_shared_add_proxy_lookup()
hits WARN_ON(1) and returns -ENOENT.

Check ref->is_reset_gpio and compare aux_args.args[0] against
entry->offset under entry->lock.

Signed-off-by: Pufan Jin <cyan2254650260@gmail.com>
---
 drivers/gpio/gpiolib-shared.c | 70 ++++++++---------------------------
 1 file changed, 16 insertions(+), 54 deletions(-)

diff --git a/drivers/gpio/gpiolib-shared.c b/drivers/gpio/gpiolib-shared.c
index f34315a205..1753524544 100644
--- a/drivers/gpio/gpiolib-shared.c
+++ b/drivers/gpio/gpiolib-shared.c
@@ -361,14 +361,16 @@ static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
 					  struct gpio_shared_ref *ref)
 {
 	struct fwnode_handle *reset_fwnode = dev_fwnode(consumer);
-	struct fwnode_reference_args ref_args, aux_args;
+	struct fwnode_reference_args aux_args;
 	struct device *parent = consumer->parent;
-	struct gpio_shared_ref *real_ref;
 	bool match;
 	int ret;
 
 	lockdep_assert_held(&ref->lock);
 
+	if (!ref->is_reset_gpio)
+		return false;
+
 	/* The reset-gpio device must have a parent AND a firmware node. */
 	if (!parent || !reset_fwnode)
 		return false;
@@ -380,62 +382,22 @@ static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
 	if (!device_match_fwnode(parent, entry->fwnode))
 		return false;
 
-	/*
-	 * Now we need to find the actual pin we want to assign to this
-	 * reset-gpio device. To that end: iterate over the list of references
-	 * of this entry and see if there's one, whose reset-gpios property's
-	 * arguments match the ones from this consumer's node.
-	 */
-	list_for_each_entry(real_ref, &entry->refs, list) {
-		if (real_ref == ref)
-			continue;
-
-		guard(mutex)(&real_ref->lock);
-
-		if (!real_ref->fwnode)
-			continue;
-
-		/*
-		 * The device associated with the shared reference's firmware
-		 * node is the consumer of the reset control exposed by the
-		 * reset-gpio device. It must have a "reset-gpios" property
-		 * that's referencing the entry's firmware node.
-		 *
-		 * The reference args must agree between the real consumer and
-		 * the auxiliary reset-gpio device.
-		 */
-		ret = fwnode_property_get_reference_args(real_ref->fwnode,
-							 "reset-gpios",
-							 NULL, 2, 0, &ref_args);
-		if (ret)
-			continue;
-
-		ret = fwnode_property_get_reference_args(reset_fwnode, "reset-gpios",
-							 NULL, 2, 0, &aux_args);
-		if (ret) {
-			fwnode_handle_put(ref_args.fwnode);
-			continue;
-		}
-
-		match = ((ref_args.fwnode == entry->fwnode) &&
-			 (aux_args.fwnode == entry->fwnode) &&
-			 (ref_args.args[0] == aux_args.args[0]));
+	ret = fwnode_property_get_reference_args(reset_fwnode, "reset-gpios",
+						 NULL, 2, 0, &aux_args);
+	if (ret)
+		return false;
 
-		fwnode_handle_put(ref_args.fwnode);
-		fwnode_handle_put(aux_args.fwnode);
+	scoped_guard(mutex, &entry->lock)
+		match = (aux_args.fwnode == entry->fwnode) &&
+			(aux_args.args[0] == entry->offset);
 
-		if (!match)
-			continue;
+	fwnode_handle_put(aux_args.fwnode);
 
-		/*
-		 * Reuse the fwnode of the real device, next time we'll use it
-		 * in the normal path.
-		 */
-		ref->fwnode = fwnode_handle_get(reset_fwnode);
-		return true;
-	}
+	if (!match)
+		return false;
 
-	return false;
+	ref->fwnode = fwnode_handle_get(reset_fwnode);
+	return true;
 }
 #else
 static bool gpio_shared_dev_is_reset_gpio(struct device *consumer,
-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 4/5] reset: gpio: derive fwnode_reset_n_cells from platform data
  2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
                   ` (2 preceding siblings ...)
  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 ` 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
  4 siblings, 0 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

reset_gpio_probe() sets priv->rc.fwnode_reset_n_cells = 2. When
drivers/reset/core.c supports devicetree GPIO controllers with cell
counts other than 2, __fwnode_reset_control_get() hits
WARN_ON(args.nargs != rcdev->fwnode_reset_n_cells).

__reset_add_reset_gpio_device() passes &rgpio_dev->ref_args via
adev->dev.platform_data. Set fwnode_reset_n_cells from platdata->nargs,
falling back to 2 if platform_data is NULL, and guard
reset_gpio_fwnode_xlate() when nargs is 0.

Signed-off-by: Pufan Jin <cyan2254650260@gmail.com>
---
 drivers/reset/reset-gpio.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/reset/reset-gpio.c b/drivers/reset/reset-gpio.c
index d11d1d6de4..661ab672b1 100644
--- a/drivers/reset/reset-gpio.c
+++ b/drivers/reset/reset-gpio.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/auxiliary_bus.h>
+#include <linux/fwnode.h>
 #include <linux/gpio/consumer.h>
 #include <linux/module.h>
 #include <linux/property.h>
@@ -48,12 +49,13 @@ static const struct reset_control_ops reset_gpio_ops = {
 static int reset_gpio_fwnode_xlate(struct reset_controller_dev *rcdev,
 				   const struct fwnode_reference_args *reset_spec)
 {
-	return reset_spec->args[0];
+	return reset_spec->nargs ? reset_spec->args[0] : 0;
 }
 
 static int reset_gpio_probe(struct auxiliary_device *adev,
 			    const struct auxiliary_device_id *id)
 {
+	struct fwnode_reference_args *platdata = dev_get_platdata(&adev->dev);
 	struct device *dev = &adev->dev;
 	struct reset_gpio_priv *priv;
 
@@ -71,7 +73,7 @@ static int reset_gpio_probe(struct auxiliary_device *adev,
 	priv->rc.dev = dev;
 
 	/* Cells to match GPIO specifier, but it's not really used */
-	priv->rc.fwnode_reset_n_cells = 2;
+	priv->rc.fwnode_reset_n_cells = platdata ? platdata->nargs : 2;
 	priv->rc.fwnode_xlate = reset_gpio_fwnode_xlate;
 	priv->rc.nr_resets = 1;
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 5/5] reset: core: translate GPIO specifier via gpio_device_find_by_fwnode_args()
  2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
                   ` (3 preceding siblings ...)
  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 ` Pufan Jin
  4 siblings, 0 replies; 6+ messages in thread
From: Pufan Jin @ 2026-12-05  6:43 UTC (permalink / raw)
  To: Bartosz Golaszewski, Bartosz Golaszewski, Linus Walleij, Philipp Zabel
  Cc: Krzysztof Kozlowski, linux-gpio, linux-kernel

__reset_add_reset_gpio_device() reads the GPIO offset and flags from
args->args[0] and args->args[1], rejecting args->nargs != 2 and
flags > GPIO_ACTIVE_LOW.

This fails on devicetree GPIO controllers with a custom of_xlate()
callback such as tegra186-gpio. It also rejects devicetree controllers
with #gpio-cells != 2 and does not translate other devicetree GPIO
flags such as open-drain or transitory.

Use gpio_device_find_by_fwnode_args() to resolve the gpio_device,
hardware offset, and lookup flags. This removes the #gpio-cells == 2
restriction for devicetree providers and the flag check in
drivers/reset/core.c. Software node references remain 2-cell per
swnode_gpio_get_reference() and return -EINVAL instead of -ENOENT when
nargs != 2.

Fixes: c721f189e89c ("reset: Instantiate reset GPIO controller for shared reset-gpios")
Signed-off-by: Pufan Jin <cyan2254650260@gmail.com>
---
 drivers/reset/core.c | 37 +++++--------------------------------
 1 file changed, 5 insertions(+), 32 deletions(-)

diff --git a/drivers/reset/core.c b/drivers/reset/core.c
index 1ae7ca1e85..35e0050f7e 100644
--- a/drivers/reset/core.c
+++ b/drivers/reset/core.c
@@ -14,7 +14,6 @@
 #include <linux/export.h>
 #include <linux/fwnode.h>
 #include <linux/gpio/driver.h>
-#include <linux/gpio/machine.h>
 #include <linux/gpio/property.h>
 #include <linux/idr.h>
 #include <linux/kernel.h>
@@ -1048,20 +1047,12 @@ static int __reset_add_reset_gpio_device(struct fwnode_handle *fwnode,
 					 const struct fwnode_reference_args *args)
 {
 	struct property_entry properties[3] = { };
-	unsigned int offset, flags, lflags;
 	struct reset_gpio_lookup *rgpio_dev;
+	unsigned long lflags;
 	struct device *parent;
+	unsigned int offset;
 	int ret, prop = 0;
 
-	/*
-	 * Currently only #gpio-cells=2 is supported with the meaning of:
-	 * args[0]: GPIO number
-	 * args[1]: GPIO flags
-	 * TODO: Handle other cases.
-	 */
-	if (args->nargs != 2)
-		return -ENOENT;
-
 	/*
 	 * Registering reset-gpio device might cause immediate
 	 * bind, resulting in its probe() registering new reset controller thus
@@ -1069,27 +1060,10 @@ static int __reset_add_reset_gpio_device(struct fwnode_handle *fwnode,
 	 */
 	lockdep_assert_not_held(&reset_list_mutex);
 
-	offset = args->args[0];
-	flags = args->args[1];
-
-	/*
-	 * Later we map GPIO flags between OF and Linux, however not all
-	 * constants from include/dt-bindings/gpio/gpio.h and
-	 * include/linux/gpio/machine.h match each other.
-	 *
-	 * FIXME: Find a better way of translating OF flags to GPIO lookup
-	 * flags.
-	 */
-	if (flags > GPIO_ACTIVE_LOW) {
-		pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
-		       flags, offset);
-		return -EINVAL;
-	}
-
 	struct gpio_device *gdev __free(gpio_device_put) =
-			gpio_device_find_by_fwnode(args->fwnode);
-	if (!gdev)
-		return -EPROBE_DEFER;
+		gpio_device_find_by_fwnode_args(args, &offset, &lflags);
+	if (IS_ERR(gdev))
+		return PTR_ERR(gdev);
 
 	guard(mutex)(&reset_gpio_lookup_mutex);
 
@@ -1104,7 +1078,6 @@ static int __reset_add_reset_gpio_device(struct fwnode_handle *fwnode,
 		}
 	}
 
-	lflags = GPIO_PERSISTENT | (flags & GPIO_ACTIVE_LOW);
 	parent = gpio_device_to_device(gdev);
 	properties[prop++] = PROPERTY_ENTRY_STRING("compatible", "reset-gpio");
 	properties[prop++] = PROPERTY_ENTRY_GPIO("reset-gpios", parent->fwnode, offset, lflags);
-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-05  6:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05  6:43 [PATCH v2 0/5] gpiolib / reset: resolve GPIO specifiers via gpiolib Pufan Jin
2026-12-05  6:43 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®