Skip to content

Commit 40dda35

Browse files
esmilbebarino
authored andcommitted
clk: starfive: jh7100: Don't round divisor up twice
The problem is best illustrated by an example. Suppose a consumer wants a 4MHz clock rate from a divider with a 10MHz parent. It would then call clk_round_rate(clk, 4000000) which would call into our determine_rate() callback that correctly rounds up and finds that a divisor of 3 gives the highest possible frequency below the requested 4MHz and returns 10000000 / 3 = 3333333Hz. However the consumer would then call clk_set_rate(clk, 3333333) but since 3333333 doesn't divide 10000000 evenly our set_rate() callback would again round the divisor up and set it to 4 which results in an unnecessarily low rate of 2.5MHz. Fix it by using DIV_ROUND_CLOSEST in the set_rate() callback. Fixes: 4210be6 ("clk: starfive: Add JH7100 clock generator driver") Signed-off-by: Emil Renner Berthing <kernel@esmil.dk> Link: https://lore.kernel.org/r/20220126173953.1016706-2-kernel@esmil.dk Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent e783362 commit 40dda35

1 file changed

Lines changed: 3 additions & 11 deletions

File tree

drivers/clk/starfive/clk-starfive-jh7100.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,22 +399,13 @@ static unsigned long jh7100_clk_recalc_rate(struct clk_hw *hw,
399399
return div ? parent_rate / div : 0;
400400
}
401401

402-
static unsigned long jh7100_clk_bestdiv(struct jh7100_clk *clk,
403-
unsigned long rate, unsigned long parent)
404-
{
405-
unsigned long max = clk->max_div;
406-
unsigned long div = DIV_ROUND_UP(parent, rate);
407-
408-
return min(div, max);
409-
}
410-
411402
static int jh7100_clk_determine_rate(struct clk_hw *hw,
412403
struct clk_rate_request *req)
413404
{
414405
struct jh7100_clk *clk = jh7100_clk_from(hw);
415406
unsigned long parent = req->best_parent_rate;
416407
unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
417-
unsigned long div = jh7100_clk_bestdiv(clk, rate, parent);
408+
unsigned long div = min_t(unsigned long, DIV_ROUND_UP(parent, rate), clk->max_div);
418409
unsigned long result = parent / div;
419410

420411
/*
@@ -442,7 +433,8 @@ static int jh7100_clk_set_rate(struct clk_hw *hw,
442433
unsigned long parent_rate)
443434
{
444435
struct jh7100_clk *clk = jh7100_clk_from(hw);
445-
unsigned long div = jh7100_clk_bestdiv(clk, rate, parent_rate);
436+
unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate),
437+
1UL, (unsigned long)clk->max_div);
446438

447439
jh7100_clk_reg_rmw(clk, JH7100_CLK_DIV_MASK, div);
448440
return 0;

0 commit comments

Comments
 (0)