[v2,1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

Message ID 20221108133853.61884-1-andriy.shevchenko@linux.intel.com
State New
Headers
Series [v2,1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode |

Commit Message

Andy Shevchenko Nov. 8, 2022, 1:38 p.m. UTC
  GPIO library is getting rid of of_node, fwnode should be utilized instead.
Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
v2: added tag (Dmitry)
 drivers/gpio/gpiolib-of.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)


base-commit: 80280df758c1498485988b30cf6887fde7796056
  

Comments

Linus Walleij Nov. 9, 2022, 8:59 a.m. UTC | #1
On Tue, Nov 8, 2022 at 2:38 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> GPIO library is getting rid of of_node, fwnode should be utilized instead.
> Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Reviewed-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
  
Thierry Reding Nov. 10, 2022, 1:22 p.m. UTC | #2
On Tue, Nov 08, 2022 at 03:38:52PM +0200, Andy Shevchenko wrote:
> GPIO library is getting rid of of_node, fwnode should be utilized instead.
> Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Reviewed-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
> v2: added tag (Dmitry)
>  drivers/gpio/gpiolib-of.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
> index be9c34cca322..000020eb78d8 100644
> --- a/drivers/gpio/gpiolib-of.c
> +++ b/drivers/gpio/gpiolib-of.c
> @@ -1104,9 +1104,11 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
>  
>  int of_gpiochip_add(struct gpio_chip *chip)
>  {
> +	struct device_node *np;
>  	int ret;
>  
> -	if (!chip->of_node)
> +	np = to_of_node(chip->fwnode);
> +	if (!np)

This breaks a number of GPIO controllers on Tegra where chip->fwnode
ends up never getting set. I also see this break drivers like the MFD-
based gpio-max77620, so I don't think this is anything specific to the
Tegra drivers.

Looking at how fwnode handling works, it seems like we're checking the
wrong value here, since chip->fwnode is only for explicit overrides of
the fwnode value.

The below patch fixes the regression for me:

--- >8 ---
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 4be3c21aa718..760f018ae7de 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -1067,7 +1067,7 @@ int of_gpiochip_add(struct gpio_chip *chip)
        struct device_node *np;
        int ret;

-       np = to_of_node(chip->fwnode);
+       np = to_of_node(chip->gpiodev->dev.fwnode);
        if (!np)
                return 0;

--- >8 ---

That uses the GPIO device's fwnode, which can be chip->fwnode if
chip->fwnode was explicitly specified. Otherwise this defaults to 

See gpiochip_add_data_with_key() in drivers/gpio/gpiolib.c:

    677 |	/*
    678 |	 * Assign fwnode depending on the result of the previous calls,
    679 |	 * if none of them succeed, assign it to the parent's one.
    680 |	 */
    681 |	gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;

Looks like this is only important to make sure gdev->dev.fwnode is valid
for OF, for ACPI this should be a no-op.

Thierry
  
Andy Shevchenko Nov. 10, 2022, 1:53 p.m. UTC | #3
On Thu, Nov 10, 2022 at 02:22:40PM +0100, Thierry Reding wrote:
> On Tue, Nov 08, 2022 at 03:38:52PM +0200, Andy Shevchenko wrote:

...

> > +	np = to_of_node(chip->fwnode);
> 
> This breaks a number of GPIO controllers on Tegra where chip->fwnode
> ends up never getting set. I also see this break drivers like the MFD-
> based gpio-max77620, so I don't think this is anything specific to the
> Tegra drivers.
> 
> Looking at how fwnode handling works, it seems like we're checking the
> wrong value here, since chip->fwnode is only for explicit overrides of
> the fwnode value.
> 
> The below patch fixes the regression for me:

Thank you! Can you submit it as a formal fix? (Also see below)
Of if Bart prefers I can respin fixed verison. Bart?

...

> -       np = to_of_node(chip->fwnode);
> +       np = to_of_node(chip->gpiodev->dev.fwnode);

dev_fwnode(&chip->gpiodev->dev)

...


Your report makes me wonder if I can Cc you the patch that changes that logic,
so you can help with a testing on OF platforms.
  
Marek Szyprowski Nov. 10, 2022, 3:07 p.m. UTC | #4
Hi Andy,

On 10.11.2022 14:53, Andy Shevchenko wrote:
> On Thu, Nov 10, 2022 at 02:22:40PM +0100, Thierry Reding wrote:
>> On Tue, Nov 08, 2022 at 03:38:52PM +0200, Andy Shevchenko wrote:
> ...
>>> +	np = to_of_node(chip->fwnode);
>> This breaks a number of GPIO controllers on Tegra where chip->fwnode
>> ends up never getting set. I also see this break drivers like the MFD-
>> based gpio-max77620, so I don't think this is anything specific to the
>> Tegra drivers.
>>
>> Looking at how fwnode handling works, it seems like we're checking the
>> wrong value here, since chip->fwnode is only for explicit overrides of
>> the fwnode value.
>>
>> The below patch fixes the regression for me:
> Thank you! Can you submit it as a formal fix? (Also see below)
> Of if Bart prefers I can respin fixed verison. Bart?
>
> ...
>> -       np = to_of_node(chip->fwnode);
>> +       np = to_of_node(chip->gpiodev->dev.fwnode);
> dev_fwnode(&chip->gpiodev->dev)
>
> ...
>
>
> Your report makes me wonder if I can Cc you the patch that changes that logic,
> so you can help with a testing on OF platforms.

I've also found this issue with today's linux-next and bisected to this 
patch. I confirm that the above change fixes the boot issue on Raspberry 
Pi 4B and Odroid-M1 boards. Feel free to add:

Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>


Best regards
  
Bartosz Golaszewski Nov. 10, 2022, 3:29 p.m. UTC | #5
On Thu, Nov 10, 2022 at 2:53 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Thu, Nov 10, 2022 at 02:22:40PM +0100, Thierry Reding wrote:
> > On Tue, Nov 08, 2022 at 03:38:52PM +0200, Andy Shevchenko wrote:
>
> ...
>
> > > +   np = to_of_node(chip->fwnode);
> >
> > This breaks a number of GPIO controllers on Tegra where chip->fwnode
> > ends up never getting set. I also see this break drivers like the MFD-
> > based gpio-max77620, so I don't think this is anything specific to the
> > Tegra drivers.
> >
> > Looking at how fwnode handling works, it seems like we're checking the
> > wrong value here, since chip->fwnode is only for explicit overrides of
> > the fwnode value.
> >
> > The below patch fixes the regression for me:
>
> Thank you! Can you submit it as a formal fix? (Also see below)
> Of if Bart prefers I can respin fixed verison. Bart?
>

Let's have a fix on top of your changes. Thierry: can you send the
patch to the list?

Bart
  

Patch

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index be9c34cca322..000020eb78d8 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -1104,9 +1104,11 @@  static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
 
 int of_gpiochip_add(struct gpio_chip *chip)
 {
+	struct device_node *np;
 	int ret;
 
-	if (!chip->of_node)
+	np = to_of_node(chip->fwnode);
+	if (!np)
 		return 0;
 
 	if (!chip->of_xlate) {
@@ -1123,18 +1125,18 @@  int of_gpiochip_add(struct gpio_chip *chip)
 	if (ret)
 		return ret;
 
-	of_node_get(chip->of_node);
+	fwnode_handle_get(chip->fwnode);
 
 	ret = of_gpiochip_scan_gpios(chip);
 	if (ret)
-		of_node_put(chip->of_node);
+		fwnode_handle_put(chip->fwnode);
 
 	return ret;
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
 {
-	of_node_put(chip->of_node);
+	fwnode_handle_put(chip->fwnode);
 }
 
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)