Skip to content

Commit 9315514

Browse files
Uwe Kleine-KönigMichael Turquette
authored andcommitted
clk: divider: fix calculation of initial best divider when rounding to closest
Similar to the reasoning for the previous commit DIV_ROUND_CLOSEST(parent_rate, rate) might not be the best integer divisor to get a good approximation for rate from parent_rate (given the metric for CLK_DIVIDER_ROUND_CLOSEST). For example assume a parent rate of 1000 Hz and a target rate of 700. Using DIV_ROUND_CLOSEST the suggested divisor gets calculated to 1 resulting in a target rate of 1000 with a delta of 300 to the desired rate. With choosing 2 as divisor however the resulting rate is 500 which is nearer to 700. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Acked-by: Sascha Hauer <s.hauer@pengutronix.de> Acked-by: Maxime Coquelin <maxime.coquelin@st.com> Signed-off-by: Michael Turquette <mturquette@linaro.org>
1 parent 26bac95 commit 9315514

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

drivers/clk/clk-divider.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,18 @@ static int _div_round_closest(const struct clk_div_table *table,
219219
unsigned long parent_rate, unsigned long rate,
220220
unsigned long flags)
221221
{
222-
int up, down, div;
222+
int up, down;
223223
unsigned long up_rate, down_rate;
224224

225-
up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
225+
up = DIV_ROUND_UP(parent_rate, rate);
226+
down = parent_rate / rate;
226227

227228
if (flags & CLK_DIVIDER_POWER_OF_TWO) {
228-
up = __roundup_pow_of_two(div);
229-
down = __rounddown_pow_of_two(div);
229+
up = __roundup_pow_of_two(up);
230+
down = __rounddown_pow_of_two(down);
230231
} else if (table) {
231-
up = _round_up_table(table, div);
232-
down = _round_down_table(table, div);
232+
up = _round_up_table(table, up);
233+
down = _round_down_table(table, down);
233234
}
234235

235236
up_rate = DIV_ROUND_UP(parent_rate, up);

0 commit comments

Comments
 (0)