[RFC/RFT PATCH] gpiolib: reverse-assign the fwnode to struct gpio_chip

From: Bartosz Golaszewski
Date: Fri Oct 06 2023 - 07:52:02 EST


From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>

struct gpio_chip is not only used to carry the information needed to
set-up a GPIO device but is also used in all GPIOLIB callbacks and is
passed to the matching functions of lookup helpers.

In that last case, it is currently impossible to match a GPIO device by
fwnode unless it was explicitly assigned to the chip in the provider
code. If the fwnode is taken from the parent device, the pointer in
struct gpio_chip will remain NULL.

If we have a parent device but gc->fwnode was not assigned by the
provider, let's assign it ourselves so that lookup by fwnode can work in
all cases.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
---
This is something that Dipen reported with one of the tegra drivers where
a GPIO lookup by fwnode does not work because the fwnode pointer in struct
gpio_chip is NULL. This patch addresses this use-case.

drivers/gpio/gpiolib.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 191f9c87b4d0..a0e3d255fb73 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -782,6 +782,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
struct lock_class_key *lock_key,
struct lock_class_key *request_key)
{
+ struct fwnode_handle *parent_fwnode;
struct gpio_device *gdev;
unsigned long flags;
unsigned int i;
@@ -806,10 +807,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
* If the calling driver did not initialize firmware node,
* do it here using the parent device, if any.
*/
- if (gc->fwnode)
+ if (gc->fwnode) {
device_set_node(&gdev->dev, gc->fwnode);
- else if (gc->parent)
- device_set_node(&gdev->dev, dev_fwnode(gc->parent));
+ } else if (gc->parent) {
+ parent_fwnode = dev_fwnode(gc->parent);
+ device_set_node(&gdev->dev, parent_fwnode);
+ gc->fwnode = parent_fwnode;
+ }

gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
if (gdev->id < 0) {
--
2.39.2