Skip to content

Commit

Permalink
clk: divider: fix calculation of initial best divider when rounding t…
Browse files Browse the repository at this point in the history
…o 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>
  • Loading branch information
Uwe Kleine-König authored and Michael Turquette committed Mar 9, 2015
1 parent 26bac95 commit 9315514
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions drivers/clk/clk-divider.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,18 @@ static int _div_round_closest(const struct clk_div_table *table,
unsigned long parent_rate, unsigned long rate,
unsigned long flags)
{
int up, down, div;
int up, down;
unsigned long up_rate, down_rate;

up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
up = DIV_ROUND_UP(parent_rate, rate);
down = parent_rate / rate;

if (flags & CLK_DIVIDER_POWER_OF_TWO) {
up = __roundup_pow_of_two(div);
down = __rounddown_pow_of_two(div);
up = __roundup_pow_of_two(up);
down = __rounddown_pow_of_two(down);
} else if (table) {
up = _round_up_table(table, div);
down = _round_down_table(table, div);
up = _round_up_table(table, up);
down = _round_down_table(table, down);
}

up_rate = DIV_ROUND_UP(parent_rate, up);
Expand Down

0 comments on commit 9315514

Please sign in to comment.