Skip to content

Commit 1c924fc

Browse files
committed
clk: renesas: div6: Consider all parents for requested rate
Currently the .determine_rate() callback considers only the current parent clock, limiting the range of achievable clock rates on DIV6 clocks with multiple parents, as found on SH/R-Mobile SoCs. Extend the callback to consider all available parent clocks. Signed-off-by: Geert Uytterhoeven <[email protected]> Link: https://lore.kernel.org/r/60e639692b462f99e0b6ab868c3675b3d97dbdb0.1617281699.git.geert+renesas@glider.be
1 parent c9d1b58 commit 1c924fc

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

drivers/clk/renesas/clk-div6.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,39 @@ static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
103103
static int cpg_div6_clock_determine_rate(struct clk_hw *hw,
104104
struct clk_rate_request *req)
105105
{
106-
unsigned int div = cpg_div6_clock_calc_div(req->rate,
107-
req->best_parent_rate);
106+
unsigned long prate, calc_rate, diff, best_rate, best_prate;
107+
unsigned int num_parents = clk_hw_get_num_parents(hw);
108+
struct clk_hw *parent, *best_parent = NULL;
109+
unsigned long min_diff = ULONG_MAX;
110+
unsigned int i, div;
111+
112+
for (i = 0; i < num_parents; i++) {
113+
parent = clk_hw_get_parent_by_index(hw, i);
114+
if (!parent)
115+
continue;
116+
117+
prate = clk_hw_get_rate(parent);
118+
if (!prate)
119+
continue;
120+
121+
div = cpg_div6_clock_calc_div(req->rate, prate);
122+
calc_rate = prate / div;
123+
diff = calc_rate > req->rate ? calc_rate - req->rate
124+
: req->rate - calc_rate;
125+
if (diff < min_diff) {
126+
best_rate = calc_rate;
127+
best_parent = parent;
128+
best_prate = prate;
129+
min_diff = diff;
130+
}
131+
}
132+
133+
if (!best_parent)
134+
return -EINVAL;
108135

109-
req->rate = req->best_parent_rate / div;
136+
req->best_parent_rate = best_prate;
137+
req->best_parent_hw = best_parent;
138+
req->rate = best_rate;
110139
return 0;
111140
}
112141

0 commit comments

Comments
 (0)