diff mbox series

[06/11] usb: dwc3: imx8mp: use scoped device node handling to simplify error paths

Message ID 20240814-b4-cleanup-h-of-node-put-usb-v1-6-95481b9682bc@linaro.org (mailing list archive)
State Accepted
Commit 34e8df2fb2de4e56fdd91a03ffe466f3c88a67df
Headers show
Series usb: dwc3: various cleanups | expand

Commit Message

Krzysztof Kozlowski Aug. 14, 2024, 10:35 a.m. UTC
Obtain the device node reference with scoped/cleanup.h to reduce error
handling and make the code a bit simpler.  Scoped/cleanup.h coding style
expects variable declaration with initialization, so the
of_get_compatible_child() call has to be moved earlier, before any goto
jumps happen.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 drivers/usb/dwc3/dwc3-imx8mp.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

Comments

Frank Li Aug. 14, 2024, 3:43 p.m. UTC | #1
On Wed, Aug 14, 2024 at 12:35:42PM +0200, Krzysztof Kozlowski wrote:
> Obtain the device node reference with scoped/cleanup.h to reduce error
> handling and make the code a bit simpler.  Scoped/cleanup.h coding style
> expects variable declaration with initialization, so the
> of_get_compatible_child() call has to be moved earlier, before any goto
> jumps happen.
>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Reviewed-by: Frank Li <Frank.Li@nxp.com>

> ---
>  drivers/usb/dwc3/dwc3-imx8mp.c | 20 ++++++++------------
>  1 file changed, 8 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/usb/dwc3/dwc3-imx8mp.c b/drivers/usb/dwc3/dwc3-imx8mp.c
> index 869b5e7c15ed..8b88649b569f 100644
> --- a/drivers/usb/dwc3/dwc3-imx8mp.c
> +++ b/drivers/usb/dwc3/dwc3-imx8mp.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) 2020 NXP.
>   */
>
> +#include <linux/cleanup.h>
>  #include <linux/clk.h>
>  #include <linux/interrupt.h>
>  #include <linux/io.h>
> @@ -147,7 +148,7 @@ static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
>  static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  {
>  	struct device		*dev = &pdev->dev;
> -	struct device_node	*dwc3_np, *node = dev->of_node;
> +	struct device_node	*node = dev->of_node;
>  	struct dwc3_imx8mp	*dwc3_imx;
>  	struct resource		*res;
>  	int			err, irq;
> @@ -193,6 +194,11 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  		return irq;
>  	dwc3_imx->irq = irq;
>
> +	struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
> +										  "snps,dwc3");
> +	if (!dwc3_np)
> +		return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
> +
>  	imx8mp_configure_glue(dwc3_imx);
>
>  	pm_runtime_set_active(dev);
> @@ -201,17 +207,10 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  	if (err < 0)
>  		goto disable_rpm;
>
> -	dwc3_np = of_get_compatible_child(node, "snps,dwc3");
> -	if (!dwc3_np) {
> -		err = -ENODEV;
> -		dev_err(dev, "failed to find dwc3 core child\n");
> -		goto disable_rpm;
> -	}
> -
>  	err = of_platform_populate(node, NULL, NULL, dev);
>  	if (err) {
>  		dev_err(&pdev->dev, "failed to create dwc3 core\n");
> -		goto err_node_put;
> +		goto disable_rpm;
>  	}
>
>  	dwc3_imx->dwc3 = of_find_device_by_node(dwc3_np);
> @@ -220,7 +219,6 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  		err = -ENODEV;
>  		goto depopulate;
>  	}
> -	of_node_put(dwc3_np);
>
>  	err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
>  					IRQF_ONESHOT, dev_name(dev), dwc3_imx);
> @@ -236,8 +234,6 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>
>  depopulate:
>  	of_platform_depopulate(dev);
> -err_node_put:
> -	of_node_put(dwc3_np);
>  disable_rpm:
>  	pm_runtime_disable(dev);
>  	pm_runtime_put_noidle(dev);
>
> --
> 2.43.0
>
Thinh Nguyen Aug. 27, 2024, 1:30 a.m. UTC | #2
On Wed, Aug 14, 2024, Krzysztof Kozlowski wrote:
> Obtain the device node reference with scoped/cleanup.h to reduce error
> handling and make the code a bit simpler.  Scoped/cleanup.h coding style
> expects variable declaration with initialization, so the
> of_get_compatible_child() call has to be moved earlier, before any goto
> jumps happen.
> 
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>  drivers/usb/dwc3/dwc3-imx8mp.c | 20 ++++++++------------
>  1 file changed, 8 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/dwc3-imx8mp.c b/drivers/usb/dwc3/dwc3-imx8mp.c
> index 869b5e7c15ed..8b88649b569f 100644
> --- a/drivers/usb/dwc3/dwc3-imx8mp.c
> +++ b/drivers/usb/dwc3/dwc3-imx8mp.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) 2020 NXP.
>   */
>  
> +#include <linux/cleanup.h>
>  #include <linux/clk.h>
>  #include <linux/interrupt.h>
>  #include <linux/io.h>
> @@ -147,7 +148,7 @@ static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
>  static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  {
>  	struct device		*dev = &pdev->dev;
> -	struct device_node	*dwc3_np, *node = dev->of_node;
> +	struct device_node	*node = dev->of_node;
>  	struct dwc3_imx8mp	*dwc3_imx;
>  	struct resource		*res;
>  	int			err, irq;
> @@ -193,6 +194,11 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  		return irq;
>  	dwc3_imx->irq = irq;
>  
> +	struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
> +										  "snps,dwc3");
> +	if (!dwc3_np)
> +		return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
> +
>  	imx8mp_configure_glue(dwc3_imx);
>  
>  	pm_runtime_set_active(dev);
> @@ -201,17 +207,10 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  	if (err < 0)
>  		goto disable_rpm;
>  
> -	dwc3_np = of_get_compatible_child(node, "snps,dwc3");
> -	if (!dwc3_np) {
> -		err = -ENODEV;
> -		dev_err(dev, "failed to find dwc3 core child\n");
> -		goto disable_rpm;
> -	}
> -
>  	err = of_platform_populate(node, NULL, NULL, dev);
>  	if (err) {
>  		dev_err(&pdev->dev, "failed to create dwc3 core\n");
> -		goto err_node_put;
> +		goto disable_rpm;
>  	}
>  
>  	dwc3_imx->dwc3 = of_find_device_by_node(dwc3_np);
> @@ -220,7 +219,6 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  		err = -ENODEV;
>  		goto depopulate;
>  	}
> -	of_node_put(dwc3_np);
>  
>  	err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
>  					IRQF_ONESHOT, dev_name(dev), dwc3_imx);
> @@ -236,8 +234,6 @@ static int dwc3_imx8mp_probe(struct platform_device *pdev)
>  
>  depopulate:
>  	of_platform_depopulate(dev);
> -err_node_put:
> -	of_node_put(dwc3_np);
>  disable_rpm:
>  	pm_runtime_disable(dev);
>  	pm_runtime_put_noidle(dev);
> 
> -- 
> 2.43.0
> 

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Thanks,
Thinh
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/dwc3-imx8mp.c b/drivers/usb/dwc3/dwc3-imx8mp.c
index 869b5e7c15ed..8b88649b569f 100644
--- a/drivers/usb/dwc3/dwc3-imx8mp.c
+++ b/drivers/usb/dwc3/dwc3-imx8mp.c
@@ -5,6 +5,7 @@ 
  * Copyright (c) 2020 NXP.
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
@@ -147,7 +148,7 @@  static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
 static int dwc3_imx8mp_probe(struct platform_device *pdev)
 {
 	struct device		*dev = &pdev->dev;
-	struct device_node	*dwc3_np, *node = dev->of_node;
+	struct device_node	*node = dev->of_node;
 	struct dwc3_imx8mp	*dwc3_imx;
 	struct resource		*res;
 	int			err, irq;
@@ -193,6 +194,11 @@  static int dwc3_imx8mp_probe(struct platform_device *pdev)
 		return irq;
 	dwc3_imx->irq = irq;
 
+	struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
+										  "snps,dwc3");
+	if (!dwc3_np)
+		return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
+
 	imx8mp_configure_glue(dwc3_imx);
 
 	pm_runtime_set_active(dev);
@@ -201,17 +207,10 @@  static int dwc3_imx8mp_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto disable_rpm;
 
-	dwc3_np = of_get_compatible_child(node, "snps,dwc3");
-	if (!dwc3_np) {
-		err = -ENODEV;
-		dev_err(dev, "failed to find dwc3 core child\n");
-		goto disable_rpm;
-	}
-
 	err = of_platform_populate(node, NULL, NULL, dev);
 	if (err) {
 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
-		goto err_node_put;
+		goto disable_rpm;
 	}
 
 	dwc3_imx->dwc3 = of_find_device_by_node(dwc3_np);
@@ -220,7 +219,6 @@  static int dwc3_imx8mp_probe(struct platform_device *pdev)
 		err = -ENODEV;
 		goto depopulate;
 	}
-	of_node_put(dwc3_np);
 
 	err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
 					IRQF_ONESHOT, dev_name(dev), dwc3_imx);
@@ -236,8 +234,6 @@  static int dwc3_imx8mp_probe(struct platform_device *pdev)
 
 depopulate:
 	of_platform_depopulate(dev);
-err_node_put:
-	of_node_put(dwc3_np);
 disable_rpm:
 	pm_runtime_disable(dev);
 	pm_runtime_put_noidle(dev);