diff options
author | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2015-02-21 05:40:24 -0500 |
---|---|---|
committer | Michael Turquette <mturquette@linaro.org> | 2015-03-09 17:19:54 -0400 |
commit | 26bac95aa88c2b1747808c0b885abe7814c0165d (patch) | |
tree | e5b94aca44f42c17cdef858f1c8782a625769c5e | |
parent | da321133b53caf7889ed3ca1dabe4cc368db2604 (diff) |
clk: divider: fix selection of divider when rounding to closest
It's an invalid approach to assume that among two divider values
the one nearer the exact divider is the better one.
Assume a parent rate of 1000 Hz, a divider with CLK_DIVIDER_POWER_OF_TWO
and a target rate of 89 Hz. The exact divider is ~ 11.236 so 8 and 16
are the candidates to choose from yielding rates 125 Hz and 62.5 Hz
respectivly. While 8 is nearer to 11.236 than 16 is, the latter is still
the better divider as 62.5 is nearer to 89 than 125 is.
Fixes: 774b514390b1 (clk: divider: Add round to closest divider)
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>
-rw-r--r-- | drivers/clk/clk-divider.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index a1a029092c8d..78b2e656ff6a 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c | |||
@@ -220,6 +220,7 @@ static int _div_round_closest(const struct clk_div_table *table, | |||
220 | unsigned long flags) | 220 | unsigned long flags) |
221 | { | 221 | { |
222 | int up, down, div; | 222 | int up, down, div; |
223 | unsigned long up_rate, down_rate; | ||
223 | 224 | ||
224 | up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate); | 225 | up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate); |
225 | 226 | ||
@@ -231,7 +232,10 @@ static int _div_round_closest(const struct clk_div_table *table, | |||
231 | down = _round_down_table(table, div); | 232 | down = _round_down_table(table, div); |
232 | } | 233 | } |
233 | 234 | ||
234 | return (up - div) <= (div - down) ? up : down; | 235 | up_rate = DIV_ROUND_UP(parent_rate, up); |
236 | down_rate = DIV_ROUND_UP(parent_rate, down); | ||
237 | |||
238 | return (rate - up_rate) <= (down_rate - rate) ? up : down; | ||
235 | } | 239 | } |
236 | 240 | ||
237 | static int _div_round(const struct clk_div_table *table, | 241 | static int _div_round(const struct clk_div_table *table, |