[PATCH v2 2/5] gpiolib: swnode: prefer matching parent gpio_device

From: Pufan Jin

Date: Sat Dec 05 2026 - 01:44:21 EST


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@xxxxxxxxx>
---
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