Re: [PATCH] watchdog: imgpdc: Use devm_clk_get_enabled() helper
From: Guenter Roeck
Date: Sat Dec 31 2022 - 18:12:00 EST
On Sat, Dec 31, 2022 at 11:53:34AM +0100, 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 simplifies the code and avoids the need of a dedicated function used
> with devm_add_action_or_reset().
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
Reviewed-by: Guenter Roeck <linux@xxxxxxxxxxxx>
> ---
> Note that the order of operations is slightly modified by this patch. The
> "sys" clk is now prepare_enable()'ed before clk_get("wdt").
> ---
> drivers/watchdog/imgpdc_wdt.c | 31 +++----------------------------
> 1 file changed, 3 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/watchdog/imgpdc_wdt.c b/drivers/watchdog/imgpdc_wdt.c
> index b57ff3787052..a55f801895d4 100644
> --- a/drivers/watchdog/imgpdc_wdt.c
> +++ b/drivers/watchdog/imgpdc_wdt.c
> @@ -175,16 +175,11 @@ static const struct watchdog_ops pdc_wdt_ops = {
> .restart = pdc_wdt_restart,
> };
>
> -static void pdc_clk_disable_unprepare(void *data)
> -{
> - clk_disable_unprepare(data);
> -}
> -
> static int pdc_wdt_probe(struct platform_device *pdev)
> {
> struct device *dev = &pdev->dev;
> u64 div;
> - int ret, val;
> + int val;
> unsigned long clk_rate;
> struct pdc_wdt_dev *pdc_wdt;
>
> @@ -196,38 +191,18 @@ static int pdc_wdt_probe(struct platform_device *pdev)
> if (IS_ERR(pdc_wdt->base))
> return PTR_ERR(pdc_wdt->base);
>
> - pdc_wdt->sys_clk = devm_clk_get(dev, "sys");
> + pdc_wdt->sys_clk = devm_clk_get_enabled(dev, "sys");
> if (IS_ERR(pdc_wdt->sys_clk)) {
> dev_err(dev, "failed to get the sys clock\n");
> return PTR_ERR(pdc_wdt->sys_clk);
> }
>
> - pdc_wdt->wdt_clk = devm_clk_get(dev, "wdt");
> + pdc_wdt->wdt_clk = devm_clk_get_enabled(dev, "wdt");
> if (IS_ERR(pdc_wdt->wdt_clk)) {
> dev_err(dev, "failed to get the wdt clock\n");
> return PTR_ERR(pdc_wdt->wdt_clk);
> }
>
> - ret = clk_prepare_enable(pdc_wdt->sys_clk);
> - if (ret) {
> - dev_err(dev, "could not prepare or enable sys clock\n");
> - return ret;
> - }
> - ret = devm_add_action_or_reset(dev, pdc_clk_disable_unprepare,
> - pdc_wdt->sys_clk);
> - if (ret)
> - return ret;
> -
> - ret = clk_prepare_enable(pdc_wdt->wdt_clk);
> - if (ret) {
> - dev_err(dev, "could not prepare or enable wdt clock\n");
> - return ret;
> - }
> - ret = devm_add_action_or_reset(dev, pdc_clk_disable_unprepare,
> - pdc_wdt->wdt_clk);
> - if (ret)
> - return ret;
> -
> /* We use the clock rate to calculate the max timeout */
> clk_rate = clk_get_rate(pdc_wdt->wdt_clk);
> if (clk_rate == 0) {
> --
> 2.34.1
>