diff mbox series

[2/4] spi: mt7621: Use the devm_clk_get_enabled() helper to simplify error handling

Message ID 05a7fd22719008c8a905d6328aa9548ce40f2a7a.1661599671.git.christophe.jaillet@wanadoo.fr
State Accepted
Commit 3d6af96814d753f34cf897466e7ddf041d0cdf3b
Headers show
Series spi: mt7621: Fix an erroneous message + clean-ups | expand

Commit Message

Christophe JAILLET Aug. 27, 2022, 11:42 a.m. UTC
The devm_clk_get_enabled() helper:
   - calls devm_clk_get()
   - calls clk_prepare_enable() and registers what is needed in order to
     call clk_disable_unprepare() when needed, as a managed resource.

This helper is well suited for cases where the clock would be kept
prepared or enabled for the whole lifetime of the driver.

This simplifies the error handling a lot.

The order between spi_unregister_controller() (in the remove function) and
the clk_disable_unprepare() (now handle by a  managed resource) is kept
the same.
(see commit 46b5c4fb87ce ("spi: mt7621: Don't leak SPI master in probe
error path") to see why it matters)

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
The order with  devm_spi_release_controller() (which undoes
devm_spi_alloc_master()) is reversed, but I don't think it is an issue.
---
 drivers/spi/spi-mt7621.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

Comments

Matthias Brugger Aug. 29, 2022, 11:14 a.m. UTC | #1
On 27/08/2022 13:42, Christophe JAILLET wrote:
> The devm_clk_get_enabled() helper:
>     - calls devm_clk_get()
>     - calls clk_prepare_enable() and registers what is needed in order to
>       call clk_disable_unprepare() when needed, as a managed resource.
> 
> This helper is well suited for cases where the clock would be kept
> prepared or enabled for the whole lifetime of the driver.
> 
> This simplifies the error handling a lot.
> 
> The order between spi_unregister_controller() (in the remove function) and
> the clk_disable_unprepare() (now handle by a  managed resource) is kept
> the same.
> (see commit 46b5c4fb87ce ("spi: mt7621: Don't leak SPI master in probe
> error path") to see why it matters)
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com>

> ---
> The order with  devm_spi_release_controller() (which undoes
> devm_spi_alloc_master()) is reversed, but I don't think it is an issue.
> ---
>   drivers/spi/spi-mt7621.c | 14 +-------------
>   1 file changed, 1 insertion(+), 13 deletions(-)
> 
> diff --git a/drivers/spi/spi-mt7621.c b/drivers/spi/spi-mt7621.c
> index 351b0ef52bbc..2580b28042be 100644
> --- a/drivers/spi/spi-mt7621.c
> +++ b/drivers/spi/spi-mt7621.c
> @@ -327,7 +327,6 @@ static int mt7621_spi_probe(struct platform_device *pdev)
>   	struct spi_controller *master;
>   	struct mt7621_spi *rs;
>   	void __iomem *base;
> -	int status = 0;
>   	struct clk *clk;
>   	int ret;
>   
> @@ -339,19 +338,14 @@ static int mt7621_spi_probe(struct platform_device *pdev)
>   	if (IS_ERR(base))
>   		return PTR_ERR(base);
>   
> -	clk = devm_clk_get(&pdev->dev, NULL);
> +	clk = devm_clk_get_enabled(&pdev->dev, NULL);
>   	if (IS_ERR(clk))
>   		return dev_err_probe(&pdev->dev, PTR_ERR(clk),
>   				     "unable to get SYS clock\n");
>   
> -	status = clk_prepare_enable(clk);
> -	if (status)
> -		return status;
> -
>   	master = devm_spi_alloc_master(&pdev->dev, sizeof(*rs));
>   	if (!master) {
>   		dev_info(&pdev->dev, "master allocation failed\n");
> -		clk_disable_unprepare(clk);
>   		return -ENOMEM;
>   	}
>   
> @@ -376,13 +370,10 @@ static int mt7621_spi_probe(struct platform_device *pdev)
>   	ret = device_reset(&pdev->dev);
>   	if (ret) {
>   		dev_err(&pdev->dev, "SPI reset failed!\n");
> -		clk_disable_unprepare(clk);
>   		return ret;
>   	}
>   
>   	ret = spi_register_controller(master);
> -	if (ret)
> -		clk_disable_unprepare(clk);
>   
>   	return ret;
>   }
> @@ -390,13 +381,10 @@ static int mt7621_spi_probe(struct platform_device *pdev)
>   static int mt7621_spi_remove(struct platform_device *pdev)
>   {
>   	struct spi_controller *master;
> -	struct mt7621_spi *rs;
>   
>   	master = dev_get_drvdata(&pdev->dev);
> -	rs = spi_controller_get_devdata(master);
>   
>   	spi_unregister_controller(master);
> -	clk_disable_unprepare(rs->clk);
>   
>   	return 0;
>   }
AngeloGioacchino Del Regno Aug. 30, 2022, 9:46 a.m. UTC | #2
Il 27/08/22 13:42, Christophe JAILLET ha scritto:
> The devm_clk_get_enabled() helper:
>     - calls devm_clk_get()
>     - calls clk_prepare_enable() and registers what is needed in order to
>       call clk_disable_unprepare() when needed, as a managed resource.
> 
> This helper is well suited for cases where the clock would be kept
> prepared or enabled for the whole lifetime of the driver.
> 
> This simplifies the error handling a lot.
> 
> The order between spi_unregister_controller() (in the remove function) and
> the clk_disable_unprepare() (now handle by a  managed resource) is kept
> the same.
> (see commit 46b5c4fb87ce ("spi: mt7621: Don't leak SPI master in probe
> error path") to see why it matters)
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com>

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
diff mbox series

Patch

diff --git a/drivers/spi/spi-mt7621.c b/drivers/spi/spi-mt7621.c
index 351b0ef52bbc..2580b28042be 100644
--- a/drivers/spi/spi-mt7621.c
+++ b/drivers/spi/spi-mt7621.c
@@ -327,7 +327,6 @@  static int mt7621_spi_probe(struct platform_device *pdev)
 	struct spi_controller *master;
 	struct mt7621_spi *rs;
 	void __iomem *base;
-	int status = 0;
 	struct clk *clk;
 	int ret;
 
@@ -339,19 +338,14 @@  static int mt7621_spi_probe(struct platform_device *pdev)
 	if (IS_ERR(base))
 		return PTR_ERR(base);
 
-	clk = devm_clk_get(&pdev->dev, NULL);
+	clk = devm_clk_get_enabled(&pdev->dev, NULL);
 	if (IS_ERR(clk))
 		return dev_err_probe(&pdev->dev, PTR_ERR(clk),
 				     "unable to get SYS clock\n");
 
-	status = clk_prepare_enable(clk);
-	if (status)
-		return status;
-
 	master = devm_spi_alloc_master(&pdev->dev, sizeof(*rs));
 	if (!master) {
 		dev_info(&pdev->dev, "master allocation failed\n");
-		clk_disable_unprepare(clk);
 		return -ENOMEM;
 	}
 
@@ -376,13 +370,10 @@  static int mt7621_spi_probe(struct platform_device *pdev)
 	ret = device_reset(&pdev->dev);
 	if (ret) {
 		dev_err(&pdev->dev, "SPI reset failed!\n");
-		clk_disable_unprepare(clk);
 		return ret;
 	}
 
 	ret = spi_register_controller(master);
-	if (ret)
-		clk_disable_unprepare(clk);
 
 	return ret;
 }
@@ -390,13 +381,10 @@  static int mt7621_spi_probe(struct platform_device *pdev)
 static int mt7621_spi_remove(struct platform_device *pdev)
 {
 	struct spi_controller *master;
-	struct mt7621_spi *rs;
 
 	master = dev_get_drvdata(&pdev->dev);
-	rs = spi_controller_get_devdata(master);
 
 	spi_unregister_controller(master);
-	clk_disable_unprepare(rs->clk);
 
 	return 0;
 }