[PATCH RFC v9 6/8] gpiolib: add gpio_device_get_fwnode() helper

From: Miao Wang via B4 Relay

Date: Tue Aug 11 2026 - 04:10:40 EST


From: Miao Wang <shankerwangmiao@xxxxxxxxx>

Add a helper function to retrieve the fwnode associated with a
struct gpio_device. This is useful for drivers that need to access the
fwnode of a GPIO device for various purposes, such as creating software
nodes or handling GPIOs in a platform-specific manner.

Acked-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
Signed-off-by: Miao Wang <shankerwangmiao@xxxxxxxxx>
---
drivers/gpio/gpiolib.c | 17 +++++++++++++++++
include/linux/gpio/driver.h | 1 +
2 files changed, 18 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e5fb60111151f6de20b424551acf213fb058e190..bf3e7f67a90f576d137fd7261b0afc741eea3245 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1583,6 +1583,23 @@ struct device *gpio_device_to_device(struct gpio_device *gdev)
}
EXPORT_SYMBOL_GPL(gpio_device_to_device);

+/**
+ * gpio_device_get_fwnode() - Retrieve the fwnode of the underlying device.
+ * @gdev: GPIO device for which to return the fwnode.
+ *
+ * This does not increase the reference count of the GPIO device nor the
+ * returned fwnode handle, since the caller is expected to hold a reference
+ * to the GPIO device, and fwnode_handle_put() is thus not needed.
+ *
+ * Returns:
+ * The fwnode handle of the underlying device.
+ */
+struct fwnode_handle *gpio_device_get_fwnode(struct gpio_device *gdev)
+{
+ return dev_fwnode(gpio_device_to_device(gdev));
+}
+EXPORT_SYMBOL_GPL(gpio_device_get_fwnode);
+
#ifdef CONFIG_GPIOLIB_IRQCHIP

/*
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 17511434ed077dde8807bd7630c342e146e5230b..4dd9120707ec72a978f5f916cc5473a91b65d2b9 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -641,6 +641,7 @@ DEFINE_FREE(gpio_device_put, struct gpio_device *,
if (!IS_ERR_OR_NULL(_T)) gpio_device_put(_T))

struct device *gpio_device_to_device(struct gpio_device *gdev);
+struct fwnode_handle *gpio_device_get_fwnode(struct gpio_device *gdev);

bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);

--
2.49.0