mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] clk: qcom: clk-rcg2: Fix wrong RCG clock rate for high parent frequencies
@ 2023-07-20  8:33 Devi Priya
  2023-07-20 16:08 ` Konrad Dybcio
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Devi Priya @ 2023-07-20  8:33 UTC (permalink / raw)
  To: andersson, agross, konrad.dybcio, mturquette, sboyd,
	linux-arm-msm, linux-clk, linux-kernel
  Cc: quic_saahtoma

If the parent clock rate is greater than unsigned long max/2 then
integer overflow happens when calculating the clock rate on 32-bit systems.
As RCG2 uses half integer dividers, the clock rate is first being
multiplied by 2 which will overflow the unsigned long max value. So, use
unsigned long long for rate computations to avoid overflow.

Signed-off-by: Devi Priya <quic_devipriy@quicinc.com>
---
 drivers/clk/qcom/clk-rcg2.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c
index e22baf3a7112..42d00b134975 100644
--- a/drivers/clk/qcom/clk-rcg2.c
+++ b/drivers/clk/qcom/clk-rcg2.c
@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)
  *            hid_div       n
  */
 static unsigned long
-calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div)
+calc_rate(unsigned long parent_rate, u32 m, u32 n, u32 mode, u32 hid_div)
 {
+	u64 rate = parent_rate;
+
 	if (hid_div) {
 		rate *= 2;
-		rate /= hid_div + 1;
+		do_div(rate, hid_div + 1);
 	}
 
 	if (mode) {
-		u64 tmp = rate;
-		tmp *= m;
-		do_div(tmp, n);
-		rate = tmp;
+		rate *= m;
+		do_div(rate, n);
 	}
 
 	return rate;
-- 
2.17.1


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

end of thread, other threads:[~2023-08-30 19:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-20  8:33 [PATCH] clk: qcom: clk-rcg2: Fix wrong RCG clock rate for high parent frequencies Devi Priya
2023-07-20 16:08 ` Konrad Dybcio
2023-07-20 16:08   ` Konrad Dybcio
2023-07-20 18:07 ` Stephen Boyd
2023-07-20 19:07 ` Marijn Suijten
2023-08-30  8:40   ` Devi Priya

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®