All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] can: flexcan: enable PER clock before obtaining its rate
@ 2025-04-14  7:36 Ciprian Costea
  2025-04-14  9:55 ` Marc Kleine-Budde
  0 siblings, 1 reply; 4+ messages in thread
From: Ciprian Costea @ 2025-04-14  7:36 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol
  Cc: linux-can, linux-kernel, NXP S32 Linux Team, imx,
	Christophe Lizzi, Alberto Ruiz, Enric Balletbo, Eric Chanudet,
	Ciprian Marian Costea, Ghennadi Procopciuc

From: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>

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.

Co-developed-by: Ghennadi Procopciuc <ghennadi.procopciuc@nxp.com>
Signed-off-by: Ghennadi Procopciuc <ghennadi.procopciuc@nxp.com>
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@oss.nxp.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-15  9:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-14  7:36 [PATCH] can: flexcan: enable PER clock before obtaining its rate Ciprian Costea
2025-04-14  9:55 ` Marc Kleine-Budde
2025-04-15  0:27   ` Stephen Boyd
2025-04-15  9:03     ` Marc Kleine-Budde

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.