2022-11-08 14:27:23

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

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 <[email protected]>
Reviewed-by: Dmitry Torokhov <[email protected]>
---
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)
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)

base-commit: 80280df758c1498485988b30cf6887fde7796056
--
2.35.1



2022-11-08 14:47:52

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v2 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

In preparation to complete fwnode switch, integrate
of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask().

Signed-off-by: Andy Shevchenko <[email protected]>
Reviewed-by: Dmitry Torokhov <[email protected]>
---
v2: added tag (Dmitry), made sure size is validated before use (Dmitry)
drivers/gpio/gpiolib-of.c | 42 ------------------------------
drivers/gpio/gpiolib-of.h | 5 ----
drivers/gpio/gpiolib.c | 54 ++++++++++++++++++++++++++++++++++++++-
3 files changed, 53 insertions(+), 48 deletions(-)

diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index 000020eb78d8..4be3c21aa718 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -112,24 +112,6 @@ static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
return gpiochip_get_desc(chip, ret);
}

-/**
- * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
- * to set the .valid_mask
- * @gc: the target gpio_chip
- *
- * Return: true if the valid mask needs to be set
- */
-bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
-{
- int size;
- const struct device_node *np = gc->of_node;
-
- size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
- if (size > 0 && size % 2 == 0)
- return true;
- return false;
-}
-
/*
* Overrides stated polarity of a gpio line and warns when there is a
* discrepancy.
@@ -989,28 +971,6 @@ void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
}
EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);

-static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
-{
- int len, i;
- u32 start, count;
- struct device_node *np = chip->of_node;
-
- len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
- if (len < 0 || len % 2 != 0)
- return;
-
- for (i = 0; i < len; i += 2) {
- of_property_read_u32_index(np, "gpio-reserved-ranges",
- i, &start);
- of_property_read_u32_index(np, "gpio-reserved-ranges",
- i + 1, &count);
- if (start >= chip->ngpio || start + count > chip->ngpio)
- continue;
-
- bitmap_clear(chip->valid_mask, start, count);
- }
-};
-
#ifdef CONFIG_PINCTRL
static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
{
@@ -1119,8 +1079,6 @@ int of_gpiochip_add(struct gpio_chip *chip)
if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
return -EINVAL;

- of_gpiochip_init_valid_mask(chip);
-
ret = of_gpiochip_add_pin_range(chip);
if (ret)
return ret;
diff --git a/drivers/gpio/gpiolib-of.h b/drivers/gpio/gpiolib-of.h
index 8af2bc899aab..2c32a332ede5 100644
--- a/drivers/gpio/gpiolib-of.h
+++ b/drivers/gpio/gpiolib-of.h
@@ -14,7 +14,6 @@ struct gpio_desc *of_find_gpio(struct device *dev,
int of_gpiochip_add(struct gpio_chip *gc);
void of_gpiochip_remove(struct gpio_chip *gc);
int of_gpio_get_count(struct device *dev, const char *con_id);
-bool of_gpio_need_valid_mask(const struct gpio_chip *gc);
void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev);
#else
static inline struct gpio_desc *of_find_gpio(struct device *dev,
@@ -30,10 +29,6 @@ static inline int of_gpio_get_count(struct device *dev, const char *con_id)
{
return 0;
}
-static inline bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
-{
- return false;
-}
static inline void of_gpio_dev_init(struct gpio_chip *gc,
struct gpio_device *gdev)
{
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e8faedca6b14..11fb7ec883e9 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -445,9 +445,21 @@ static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
return p;
}

+static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
+{
+ int size;
+
+ /* Format is "start, count, ..." */
+ size = fwnode_property_count_u32(gc->fwnode, "gpio-reserved-ranges");
+ if (size > 0 && size % 2 == 0)
+ return size;
+
+ return 0;
+}
+
static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
{
- if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
+ if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
return 0;

gc->valid_mask = gpiochip_allocate_mask(gc);
@@ -457,8 +469,48 @@ static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
return 0;
}

+static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc)
+{
+ unsigned int size;
+ u32 *ranges;
+ int ret;
+
+ size = gpiochip_count_reserved_ranges(gc);
+ if (size == 0)
+ return 0;
+
+ ranges = kmalloc_array(size, sizeof(*ranges), GFP_KERNEL);
+ if (!ranges)
+ return -ENOMEM;
+
+ ret = fwnode_property_read_u32_array(gc->fwnode, "gpio-reserved-ranges", ranges, size);
+ if (ret) {
+ kfree(ranges);
+ return ret;
+ }
+
+ while (size) {
+ u32 count = ranges[--size];
+ u32 start = ranges[--size];
+
+ if (start >= gc->ngpio || start + count > gc->ngpio)
+ continue;
+
+ bitmap_clear(gc->valid_mask, start, count);
+ }
+
+ kfree(ranges);
+ return 0;
+}
+
static int gpiochip_init_valid_mask(struct gpio_chip *gc)
{
+ int ret;
+
+ ret = gpiochip_apply_reserved_ranges(gc);
+ if (ret)
+ return ret;
+
if (gc->init_valid_mask)
return gc->init_valid_mask(gc,
gc->valid_mask,
--
2.35.1


2022-11-09 09:15:47

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

On Tue, Nov 8, 2022 at 2:38 PM Andy Shevchenko
<[email protected]> 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 <[email protected]>
> Reviewed-by: Dmitry Torokhov <[email protected]>

Reviewed-by: Linus Walleij <[email protected]>

Yours,
Linus Walleij

2022-11-09 09:40:42

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

On Tue, Nov 8, 2022 at 2:38 PM Andy Shevchenko
<[email protected]> wrote:

> In preparation to complete fwnode switch, integrate
> of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask().
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> Reviewed-by: Dmitry Torokhov <[email protected]>

Reviewed-by: Linus Walleij <[email protected]>

Yours,
Linus Walleij

2022-11-10 14:13:45

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

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.

--
With Best Regards,
Andy Shevchenko



2022-11-10 14:32:09

by Thierry Reding

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

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 <[email protected]>
> Reviewed-by: Dmitry Torokhov <[email protected]>
> ---
> 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


Attachments:
(No filename) (2.36 kB)
signature.asc (849.00 B)
Download all attachments

2022-11-10 15:27:00

by Marek Szyprowski

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

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 <[email protected]>


Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland


2022-11-10 15:34:22

by Bartosz Golaszewski

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

On Thu, Nov 10, 2022 at 2:53 PM Andy Shevchenko
<[email protected]> 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