Skip to content

Commit 774b514

Browse files
mcoquelin-stm32Mike Turquette
authored andcommitted
clk: divider: Add round to closest divider
In some cases, we want to be able to round the divider to the closest one, instead than rounding up. This patch adds a new CLK_DIVIDER_ROUND_CLOSEST flag to specify the divider has to round to closest div, keeping rounding up as de default behaviour. Signed-off-by: Maxime Coquelin <maxime.coquelin@st.com> Signed-off-by: Mike Turquette <mturquette@linaro.org>
1 parent 874f224 commit 774b514

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

drivers/clk/clk-divider.c

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
4343
return maxdiv;
4444
}
4545

46+
static unsigned int _get_table_mindiv(const struct clk_div_table *table)
47+
{
48+
unsigned int mindiv = UINT_MAX;
49+
const struct clk_div_table *clkt;
50+
51+
for (clkt = table; clkt->div; clkt++)
52+
if (clkt->div < mindiv)
53+
mindiv = clkt->div;
54+
return mindiv;
55+
}
56+
4657
static unsigned int _get_maxdiv(struct clk_divider *divider)
4758
{
4859
if (divider->flags & CLK_DIVIDER_ONE_BASED)
@@ -162,6 +173,24 @@ static int _round_up_table(const struct clk_div_table *table, int div)
162173
return up;
163174
}
164175

176+
static int _round_down_table(const struct clk_div_table *table, int div)
177+
{
178+
const struct clk_div_table *clkt;
179+
int down = _get_table_mindiv(table);
180+
181+
for (clkt = table; clkt->div; clkt++) {
182+
if (clkt->div == div)
183+
return clkt->div;
184+
else if (clkt->div > div)
185+
continue;
186+
187+
if ((div - clkt->div) < (div - down))
188+
down = clkt->div;
189+
}
190+
191+
return down;
192+
}
193+
165194
static int _div_round_up(struct clk_divider *divider,
166195
unsigned long parent_rate, unsigned long rate)
167196
{
@@ -175,6 +204,42 @@ static int _div_round_up(struct clk_divider *divider,
175204
return div;
176205
}
177206

207+
static int _div_round_closest(struct clk_divider *divider,
208+
unsigned long parent_rate, unsigned long rate)
209+
{
210+
int up, down, div;
211+
212+
up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
213+
214+
if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) {
215+
up = __roundup_pow_of_two(div);
216+
down = __rounddown_pow_of_two(div);
217+
} else if (divider->table) {
218+
up = _round_up_table(divider->table, div);
219+
down = _round_down_table(divider->table, div);
220+
}
221+
222+
return (up - div) <= (div - down) ? up : down;
223+
}
224+
225+
static int _div_round(struct clk_divider *divider, unsigned long parent_rate,
226+
unsigned long rate)
227+
{
228+
if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
229+
return _div_round_closest(divider, parent_rate, rate);
230+
231+
return _div_round_up(divider, parent_rate, rate);
232+
}
233+
234+
static bool _is_best_div(struct clk_divider *divider,
235+
int rate, int now, int best)
236+
{
237+
if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
238+
return abs(rate - now) < abs(rate - best);
239+
240+
return now <= rate && now > best;
241+
}
242+
178243
static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
179244
unsigned long *best_parent_rate)
180245
{
@@ -190,7 +255,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
190255

191256
if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
192257
parent_rate = *best_parent_rate;
193-
bestdiv = _div_round_up(divider, parent_rate, rate);
258+
bestdiv = _div_round(divider, parent_rate, rate);
194259
bestdiv = bestdiv == 0 ? 1 : bestdiv;
195260
bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
196261
return bestdiv;
@@ -217,7 +282,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
217282
parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
218283
MULT_ROUND_UP(rate, i));
219284
now = DIV_ROUND_UP(parent_rate, i);
220-
if (now <= rate && now > best) {
285+
if (_is_best_div(divider, rate, now, best)) {
221286
bestdiv = i;
222287
best = now;
223288
*best_parent_rate = parent_rate;

include/linux/clk-provider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ struct clk_div_table {
312312
* of this register, and mask of divider bits are in higher 16-bit of this
313313
* register. While setting the divider bits, higher 16-bit should also be
314314
* updated to indicate changing divider bits.
315+
* CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
316+
* to the closest integer instead of the up one.
315317
*/
316318
struct clk_divider {
317319
struct clk_hw hw;
@@ -327,6 +329,7 @@ struct clk_divider {
327329
#define CLK_DIVIDER_POWER_OF_TWO BIT(1)
328330
#define CLK_DIVIDER_ALLOW_ZERO BIT(2)
329331
#define CLK_DIVIDER_HIWORD_MASK BIT(3)
332+
#define CLK_DIVIDER_ROUND_CLOSEST BIT(4)
330333

331334
extern const struct clk_ops clk_divider_ops;
332335
struct clk *clk_register_divider(struct device *dev, const char *name,

0 commit comments

Comments
 (0)