Skip to content

Commit 5545d50

Browse files
committed
gpio: nomadik: Back out some managed resources
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: 12410e9 ("gpio: nomadik: use devm_platform_ioremap_resource() helper") Link: https://lore.kernel.org/r/20240305-fix-nomadik-gpio-v2-1-e5d1fbdc3f5c@linaro.org [Fixed some last minut print formatting] Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent caddc92 commit 5545d50

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

drivers/gpio/gpio-nomadik.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,23 +509,25 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
509509
{
510510
struct nmk_gpio_chip *nmk_chip;
511511
struct platform_device *gpio_pdev;
512+
struct device *dev = &pdev->dev;
512513
struct reset_control *reset;
513514
struct device *gpio_dev;
514515
struct gpio_chip *chip;
516+
struct resource *res;
515517
struct clk *clk;
516518
void __iomem *base;
517519
u32 id, ngpio;
518520
int ret;
519521

520522
gpio_dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
521523
if (!gpio_dev) {
522-
dev_err(&pdev->dev, "populate \"%pfwP\": device not found\n", fwnode);
524+
dev_err(dev, "populate \"%pfwP\": device not found\n", fwnode);
523525
return ERR_PTR(-ENODEV);
524526
}
525527
gpio_pdev = to_platform_device(gpio_dev);
526528

527529
if (device_property_read_u32(gpio_dev, "gpio-bank", &id)) {
528-
dev_err(&pdev->dev, "populate: gpio-bank property not found\n");
530+
dev_err(dev, "populate: gpio-bank property not found\n");
529531
platform_device_put(gpio_pdev);
530532
return ERR_PTR(-EINVAL);
531533
}
@@ -539,15 +541,15 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
539541
}
540542
#endif
541543

542-
nmk_chip = devm_kzalloc(&pdev->dev, sizeof(*nmk_chip), GFP_KERNEL);
544+
nmk_chip = devm_kzalloc(dev, sizeof(*nmk_chip), GFP_KERNEL);
543545
if (!nmk_chip) {
544546
platform_device_put(gpio_pdev);
545547
return ERR_PTR(-ENOMEM);
546548
}
547549

548550
if (device_property_read_u32(gpio_dev, "ngpios", &ngpio)) {
549551
ngpio = NMK_GPIO_PER_CHIP;
550-
dev_dbg(&pdev->dev, "populate: using default ngpio (%d)\n", ngpio);
552+
dev_dbg(dev, "populate: using default ngpio (%u)\n", ngpio);
551553
}
552554

553555
nmk_chip->is_mobileye_soc = device_is_compatible(gpio_dev,
@@ -559,25 +561,32 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
559561
chip->label = dev_name(gpio_dev);
560562
chip->parent = gpio_dev;
561563

562-
base = devm_platform_ioremap_resource(pdev, 0);
564+
/* NOTE: different devices! No devm_platform_ioremap_resource() here! */
565+
res = platform_get_resource(gpio_pdev, IORESOURCE_MEM, 0);
566+
base = devm_ioremap_resource(dev, res);
563567
if (IS_ERR(base)) {
564568
platform_device_put(gpio_pdev);
565569
return ERR_CAST(base);
566570
}
567571
nmk_chip->addr = base;
568572

569-
clk = devm_clk_get_optional(gpio_dev, NULL);
573+
/* NOTE: do not use devm_ here! */
574+
clk = clk_get_optional(gpio_dev, NULL);
570575
if (IS_ERR(clk)) {
571576
platform_device_put(gpio_pdev);
572-
return (void *)clk;
577+
return ERR_CAST(clk);
573578
}
574579
clk_prepare(clk);
575580
nmk_chip->clk = clk;
576581

577-
reset = devm_reset_control_get_optional_shared(gpio_dev, NULL);
582+
/* NOTE: do not use devm_ here! */
583+
reset = reset_control_get_optional_shared(gpio_dev, NULL);
578584
if (IS_ERR(reset)) {
579-
dev_err(&pdev->dev, "failed getting reset control: %ld\n",
580-
PTR_ERR(reset));
585+
clk_unprepare(clk);
586+
clk_put(clk);
587+
platform_device_put(gpio_pdev);
588+
dev_err(dev, "failed getting reset control: %pe\n",
589+
reset);
581590
return ERR_CAST(reset);
582591
}
583592

@@ -588,7 +597,11 @@ struct nmk_gpio_chip *nmk_gpio_populate_chip(struct fwnode_handle *fwnode,
588597
*/
589598
ret = reset_control_deassert(reset);
590599
if (ret) {
591-
dev_err(&pdev->dev, "failed reset deassert: %d\n", ret);
600+
reset_control_put(reset);
601+
clk_unprepare(clk);
602+
clk_put(clk);
603+
platform_device_put(gpio_pdev);
604+
dev_err(dev, "failed reset deassert: %d\n", ret);
592605
return ERR_PTR(ret);
593606
}
594607

0 commit comments

Comments
 (0)