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 2/5] gpiolib: swnode: prefer matching parent gpio_device
Date: Sat, 5 Dec 2026 14:43:52 +0800 [thread overview]
Message-ID: <20261205064355.137581-3-cyan2254650260@gmail.com> (raw)
In-Reply-To: <20261205064355.137581-1-cyan2254650260@gmail.com>
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
next prev parent 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 ` [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Pufan Jin
2026-12-05 6:43 ` Pufan Jin [this message]
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-3-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