Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] gpio: nomadik: Back out some managed resources
@ 2024-03-05  8:26 Linus Walleij
  2024-03-05  9:42 ` Théo Lebrun
  2024-03-05 17:05 ` Andy Shevchenko
  0 siblings, 2 replies; 4+ messages in thread
From: Linus Walleij @ 2024-03-05  8:26 UTC (permalink / raw)
  To: Bartosz Golaszewski, Philipp Zabel, Théo Lebrun,
	Andy Shevchenko
  Cc: linux-gpio, Linus Walleij

Several commits introduce managed resources (devm_*) into the
nmk_gpio_populate_chip() function.

This isn't always working because when called from the Nomadik pin
control driver we just want to populate some states for the device as
the same states are used by the pin control driver.

Some managed resources such as devm_kzalloc() etc will work, as the
passed in platform device will be used for lifecycle management,
but in some cases where we used the looked-up platform device
for the GPIO block, this will cause problems for the combined
pin control and GPIO driver, because it adds managed resources
to the GPIO device before it is probed, which is something that
the device core will not accept, and all of the GPIO blocks will
refuse to probe:

platform 8012e000.gpio: Resources present before probing
platform 8012e080.gpio: Resources present before probing
(...)

Fix this by not tying any managed resources to the looked-up
gpio_pdev/gpio_dev device, let's just live with the fact that
these need imperative resource management for now.

Drop in some notes and use a local *dev variable to clarify the
code.

Cc: Théo Lebrun <theo.lebrun@bootlin.com>
Fixes: 12410e95903c ("gpio: nomadik: use devm_platform_ioremap_resource() helper")
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/gpio/gpio-nomadik.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/drivers/gpio/gpio-nomadik.c b/drivers/gpio/gpio-nomadik.c
index 483086deb397..1ed547491f48 100644
--- a/drivers/gpio/gpio-nomadik.c
+++ b/drivers/gpio/gpio-nomadik.c
@@ -509,9 +509,11 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 {
 	struct nmk_gpio_chip *nmk_chip;
 	struct platform_device *gpio_pdev;
+	struct device *dev = &pdev->dev;
 	struct reset_control *reset;
 	struct device *gpio_dev;
 	struct gpio_chip *chip;
+	struct resource *res;
 	struct clk *clk;
 	void __iomem *base;
 	u32 id, ngpio;
@@ -519,13 +521,13 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 
 	gpio_dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
 	if (!gpio_dev) {
-		dev_err(&pdev->dev, "populate \"%pfwP\": device not found\n", fwnode);
+		dev_err(dev, "populate \"%pfwP\": device not found\n", fwnode);
 		return ERR_PTR(-ENODEV);
 	}
 	gpio_pdev = to_platform_device(gpio_dev);
 
 	if (device_property_read_u32(gpio_dev, "gpio-bank", &id)) {
-		dev_err(&pdev->dev, "populate: gpio-bank property not found\n");
+		dev_err(dev, "populate: gpio-bank property not found\n");
 		platform_device_put(gpio_pdev);
 		return ERR_PTR(-EINVAL);
 	}
@@ -539,7 +541,7 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	}
 #endif
 
-	nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL);
+	nmk_chip = devm_kzalloc(dev, sizeof(*nmk_chip), GFP_KERNEL);
 	if (!nmk_chip) {
 		platform_device_put(gpio_pdev);
 		return ERR_PTR(-ENOMEM);
@@ -547,7 +549,7 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 
 	if (device_property_read_u32(gpio_dev, "ngpios", &ngpio)) {
 		ngpio = NMK_GPIO_PER_CHIP;
-		dev_dbg(&pdev->dev, "populate: using default ngpio (%d)\n", ngpio);
+		dev_dbg(dev, "populate: using default ngpio (%d)\n", ngpio);
 	}
 
 	nmk_chip->is_mobileye_soc = device_is_compatible(gpio_dev,
@@ -559,14 +561,17 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	chip->label = dev_name(gpio_dev);
 	chip->parent = gpio_dev;
 
-	base = devm_platform_ioremap_resource(pdev, 0);
+	/* NOTE: different devices! No devm_platform_ioremap_resource() here! */
+	res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
+	base = devm_ioremap_resource(dev, res);
 	if (IS_ERR(base)) {
 		platform_device_put(gpio_pdev);
 		return ERR_CAST(base);
 	}
 	nmk_chip->addr = base;
 
-	clk = devm_clk_get_optional(gpio_dev, NULL);
+	/* NOTE: do not use devm_ here! */
+	clk = clk_get_optional(gpio_dev, NULL);
 	if (IS_ERR(clk)) {
 		platform_device_put(gpio_pdev);
 		return (void *)clk;
@@ -574,9 +579,10 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	clk_prepare(clk);
 	nmk_chip->clk = clk;
 
-	reset = devm_reset_control_get_optional_shared(gpio_dev, NULL);
+	/* NOTE: do not use devm_ here! */
+	reset = reset_control_get_optional_shared(gpio_dev, NULL);
 	if (IS_ERR(reset)) {
-		dev_err(&pdev->dev, "failed getting reset control: %ld\n",
+		dev_err(dev, "failed getting reset control: %ld\n",
 			PTR_ERR(reset));
 		return ERR_CAST(reset);
 	}
@@ -588,7 +594,7 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
 	 */
 	ret = reset_control_deassert(reset);
 	if (ret) {
-		dev_err(&pdev->dev, "failed reset deassert: %d\n", ret);
+		dev_err(dev, "failed reset deassert: %d\n", ret);
 		return ERR_PTR(ret);
 	}
 

---
base-commit: caddc92c57451d983c7e31e60b961c5aae4ece63
change-id: 20240305-fix-nomadik-gpio-50f2dddaa2dd

Best regards,
-- 
Linus Walleij <linus.walleij@linaro.org>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: nomadik: Back out some managed resources
  2024-03-05  8:26 [PATCH] gpio: nomadik: Back out some managed resources Linus Walleij
@ 2024-03-05  9:42 ` Théo Lebrun
  2024-03-05 17:05 ` Andy Shevchenko
  1 sibling, 0 replies; 4+ messages in thread
From: Théo Lebrun @ 2024-03-05  9:42 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Philipp Zabel,
	Andy Shevchenko
  Cc: linux-gpio

Hello,

On Tue Mar 5, 2024 at 9:26 AM CET, Linus Walleij wrote:
> Several commits introduce managed resources (devm_*) into the
> nmk_gpio_populate_chip() function.
>
> This isn't always working because when called from the Nomadik pin
> control driver we just want to populate some states for the device as
> the same states are used by the pin control driver.
>
> Some managed resources such as devm_kzalloc() etc will work, as the
> passed in platform device will be used for lifecycle management,
> but in some cases where we used the looked-up platform device
> for the GPIO block, this will cause problems for the combined
> pin control and GPIO driver, because it adds managed resources
> to the GPIO device before it is probed, which is something that
> the device core will not accept, and all of the GPIO blocks will
> refuse to probe:
>
> platform 8012e000.gpio: Resources present before probing
> platform 8012e080.gpio: Resources present before probing
> (...)
>
> Fix this by not tying any managed resources to the looked-up
> gpio_pdev/gpio_dev device, let's just live with the fact that
> these need imperative resource management for now.
>
> Drop in some notes and use a local *dev variable to clarify the
> code.
>
> Cc: Théo Lebrun <theo.lebrun@bootlin.com>
> Fixes: 12410e95903c ("gpio: nomadik: use devm_platform_ioremap_resource() helper")
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Thanks Linus for this follow-up. Makes complete sense. Tested on
Mobileye hardware.

This nmk_gpio_populate_chip() function being called by two different
platform drivers makes for a complex setup. I should have been more
wary when modifying it.

Tested-by: Théo Lebrun <theo.lebrun@bootlin.com>

Regards,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: nomadik: Back out some managed resources
  2024-03-05  8:26 [PATCH] gpio: nomadik: Back out some managed resources Linus Walleij
  2024-03-05  9:42 ` Théo Lebrun
@ 2024-03-05 17:05 ` Andy Shevchenko
  2024-03-05 17:43   ` Théo Lebrun
  1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2024-03-05 17:05 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Bartosz Golaszewski, Philipp Zabel, Théo Lebrun, linux-gpio

On Tue, Mar 5, 2024 at 10:26 AM Linus Walleij <linus.walleij@linaro.org> wrote:
>
> Several commits introduce managed resources (devm_*) into the
> nmk_gpio_populate_chip() function.
>
> This isn't always working because when called from the Nomadik pin
> control driver we just want to populate some states for the device as
> the same states are used by the pin control driver.
>
> Some managed resources such as devm_kzalloc() etc will work, as the
> passed in platform device will be used for lifecycle management,
> but in some cases where we used the looked-up platform device
> for the GPIO block, this will cause problems for the combined
> pin control and GPIO driver, because it adds managed resources
> to the GPIO device before it is probed, which is something that
> the device core will not accept, and all of the GPIO blocks will
> refuse to probe:
>
> platform 8012e000.gpio: Resources present before probing
> platform 8012e080.gpio: Resources present before probing
> (...)
>
> Fix this by not tying any managed resources to the looked-up
> gpio_pdev/gpio_dev device, let's just live with the fact that
> these need imperative resource management for now.

...

> -       clk = devm_clk_get_optional(gpio_dev, NULL);
> +       /* NOTE: do not use devm_ here! */
> +       clk = clk_get_optional(gpio_dev, NULL);
>         if (IS_ERR(clk)) {
>                 platform_device_put(gpio_pdev);
>                 return (void *)clk;

> -       reset = devm_reset_control_get_optional_shared(gpio_dev, NULL);
> +       /* NOTE: do not use devm_ here! */
> +       reset = reset_control_get_optional_shared(gpio_dev, NULL);
>         if (IS_ERR(reset)) {
> -               dev_err(&pdev->dev, "failed getting reset control: %ld\n",
> +               dev_err(dev, "failed getting reset control: %ld\n",
>                         PTR_ERR(reset));
>                 return ERR_CAST(reset);
>         }
> @@ -588,7 +594,7 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
>          */
>         ret = reset_control_deassert(reset);
>         if (ret) {
> -               dev_err(&pdev->dev, "failed reset deassert: %d\n", ret);
> +               dev_err(dev, "failed reset deassert: %d\n", ret);
>                 return ERR_PTR(ret);
>         }

Yeah, but you forgot to update the error path as it needs to unroll
the changes, no?

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] gpio: nomadik: Back out some managed resources
  2024-03-05 17:05 ` Andy Shevchenko
@ 2024-03-05 17:43   ` Théo Lebrun
  0 siblings, 0 replies; 4+ messages in thread
From: Théo Lebrun @ 2024-03-05 17:43 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij
  Cc: Bartosz Golaszewski, Philipp Zabel, linux-gpio

Hello,

On Tue Mar 5, 2024 at 6:05 PM CET, Andy Shevchenko wrote:
> On Tue, Mar 5, 2024 at 10:26 AM Linus Walleij <linus.walleij@linaro.org> wrote:
> >
> > Several commits introduce managed resources (devm_*) into the
> > nmk_gpio_populate_chip() function.
> >
> > This isn't always working because when called from the Nomadik pin
> > control driver we just want to populate some states for the device as
> > the same states are used by the pin control driver.
> >
> > Some managed resources such as devm_kzalloc() etc will work, as the
> > passed in platform device will be used for lifecycle management,
> > but in some cases where we used the looked-up platform device
> > for the GPIO block, this will cause problems for the combined
> > pin control and GPIO driver, because it adds managed resources
> > to the GPIO device before it is probed, which is something that
> > the device core will not accept, and all of the GPIO blocks will
> > refuse to probe:
> >
> > platform 8012e000.gpio: Resources present before probing
> > platform 8012e080.gpio: Resources present before probing
> > (...)
> >
> > Fix this by not tying any managed resources to the looked-up
> > gpio_pdev/gpio_dev device, let's just live with the fact that
> > these need imperative resource management for now.
>
> ...
>
> > -       clk = devm_clk_get_optional(gpio_dev, NULL);
> > +       /* NOTE: do not use devm_ here! */
> > +       clk = clk_get_optional(gpio_dev, NULL);
> >         if (IS_ERR(clk)) {
> >                 platform_device_put(gpio_pdev);
> >                 return (void *)clk;
>
> > -       reset = devm_reset_control_get_optional_shared(gpio_dev, NULL);
> > +       /* NOTE: do not use devm_ here! */
> > +       reset = reset_control_get_optional_shared(gpio_dev, NULL);
> >         if (IS_ERR(reset)) {
> > -               dev_err(&pdev->dev, "failed getting reset control: %ld\n",
> > +               dev_err(dev, "failed getting reset control: %ld\n",
> >                         PTR_ERR(reset));
> >                 return ERR_CAST(reset);
> >         }
> > @@ -588,7 +594,7 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
> >          */
> >         ret = reset_control_deassert(reset);
> >         if (ret) {
> > -               dev_err(&pdev->dev, "failed reset deassert: %d\n", ret);
> > +               dev_err(dev, "failed reset deassert: %d\n", ret);
> >                 return ERR_PTR(ret);
> >         }
>
> Yeah, but you forgot to update the error path as it needs to unroll
> the changes, no?

About error cases: platform_device_put() calls are missing from the last
two error cases (reset-related).

Regards,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-05 17:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-05  8:26 [PATCH] gpio: nomadik: Back out some managed resources Linus Walleij
2024-03-05  9:42 ` Théo Lebrun
2024-03-05 17:05 ` Andy Shevchenko
2024-03-05 17:43   ` Théo Lebrun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox