Re: [PATCH] can: flexcan: enable PER clock before obtaining its rate

From: Marc Kleine-Budde
Date: Mon Apr 14 2025 - 05:56:54 EST


On 14.04.2025 10:36:46, Ciprian Costea wrote:
> From: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
>
> The FlexCan driver assumes that the frequency of the 'per' clock can be
> obtained even on disabled clocks, which is not always true.
>
> According to 'clk_get_rate' documentation, it is only valid once the clock
> source has been enabled.

In commit bde8870cd8c3 ("clk: Clarify clk_get_rate() expectations")
Maxime Ripard changed the documentation of the of the function in clk.c
to say it's allowed. However clk.h states "This is only valid once the
clock source has been enabled.".

I've added the common clock maintainers to Cc.

Which documentation is correct? Is the clk.h correct for archs not using
the common clock framework?

regards,
Marc

> Co-developed-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
> Signed-off-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
> Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
> ---
> drivers/net/can/flexcan/flexcan-core.c | 22 +++++++++++++++++++++-
> 1 file changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/can/flexcan/flexcan-core.c b/drivers/net/can/flexcan/flexcan-core.c
> index 6d80c341b26f..b142aa60620e 100644
> --- a/drivers/net/can/flexcan/flexcan-core.c
> +++ b/drivers/net/can/flexcan/flexcan-core.c
> @@ -2056,6 +2056,26 @@ static int flexcan_setup_stop_mode(struct platform_device *pdev)
> return 0;
> }
>
> +static unsigned long get_per_clk_rate(struct clk *clk)
> +{
> + unsigned long rate;
> + int err;
> +
> + rate = clk_get_rate(clk);
> + if (rate)
> + return rate;
> +
> + /* Just in case this clock is disabled by default */
> + err = clk_prepare_enable(clk);
> + if (err)
> + return 0;
> +
> + rate = clk_get_rate(clk);
> + clk_disable_unprepare(clk);
> +
> + return rate;
> +}
> +
> static const struct of_device_id flexcan_of_match[] = {
> { .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, },
> { .compatible = "fsl,imx8mp-flexcan", .data = &fsl_imx8mp_devtype_data, },
> @@ -2137,7 +2157,7 @@ static int flexcan_probe(struct platform_device *pdev)
> dev_err(&pdev->dev, "no per clock defined\n");
> return PTR_ERR(clk_per);
> }
> - clock_freq = clk_get_rate(clk_per);
> + clock_freq = get_per_clk_rate(clk_per);
> }
>
> irq = platform_get_irq(pdev, 0);
> --
> 2.45.2
>
>

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |

Attachment: signature.asc
Description: PGP signature