Skip to content

Commit a2b2315

Browse files
claudiubezneageertu
authored andcommitted
clk: renesas: rzg2l: Fix computation formula
According to the hardware manual for RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf), the computation formula for PLL rate is as follows: Fout = ((m + k/65536) * Fin) / (p * 2^s) and k has values in the range [-32768, 32767]. Dividing k by 65536 with integer arithmetic gives zero all the time, causing slight differences b/w what has been set vs. what is displayed. Thus, get rid of this and decompose the formula before dividing k by 65536. Fixes: ef3c613 ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") Signed-off-by: Claudiu Beznea <[email protected]> Reviewed-by: Geert Uytterhoeven <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Geert Uytterhoeven <[email protected]>
1 parent bf51d3b commit a2b2315

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

drivers/clk/renesas/rzg2l-cpg.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#define GET_SHIFT(val) ((val >> 12) & 0xff)
4343
#define GET_WIDTH(val) ((val >> 8) & 0xf)
4444

45-
#define KDIV(val) FIELD_GET(GENMASK(31, 16), val)
45+
#define KDIV(val) ((s16)FIELD_GET(GENMASK(31, 16), val))
4646
#define MDIV(val) FIELD_GET(GENMASK(15, 6), val)
4747
#define PDIV(val) FIELD_GET(GENMASK(5, 0), val)
4848
#define SDIV(val) FIELD_GET(GENMASK(2, 0), val)
@@ -695,18 +695,18 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw,
695695
struct pll_clk *pll_clk = to_pll(hw);
696696
struct rzg2l_cpg_priv *priv = pll_clk->priv;
697697
unsigned int val1, val2;
698-
unsigned int mult = 1;
699-
unsigned int div = 1;
698+
u64 rate;
700699

701700
if (pll_clk->type != CLK_TYPE_SAM_PLL)
702701
return parent_rate;
703702

704703
val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf));
705704
val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf));
706-
mult = MDIV(val1) + KDIV(val1) / 65536;
707-
div = PDIV(val1) << SDIV(val2);
708705

709-
return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div);
706+
rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16) + KDIV(val1),
707+
16 + SDIV(val2));
708+
709+
return DIV_ROUND_CLOSEST_ULL(rate, PDIV(val1));
710710
}
711711

712712
static const struct clk_ops rzg2l_cpg_pll_ops = {

0 commit comments

Comments
 (0)