14 #include <linux/module.h>
15 #include <linux/slab.h>
18 #include <linux/string.h>
30 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
32 #define div_mask(d) ((1 << (d->width)) - 1)
33 #define is_power_of_two(i) !(i & ~i)
37 unsigned int maxdiv = 0;
40 for (clkt = table; clkt->div; clkt++)
41 if (clkt->div > maxdiv)
46 static unsigned int _get_maxdiv(
struct clk_divider *divider)
48 if (divider->flags & CLK_DIVIDER_ONE_BASED)
50 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
53 return _get_table_maxdiv(divider->table);
62 for (clkt = table; clkt->div; clkt++)
68 static unsigned int _get_div(
struct clk_divider *divider,
unsigned int val)
70 if (divider->flags & CLK_DIVIDER_ONE_BASED)
72 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
75 return _get_table_div(divider->table, val);
79 static unsigned int _get_table_val(
const struct clk_div_table *table,
84 for (clkt = table; clkt->div; clkt++)
90 static unsigned int _get_val(
struct clk_divider *divider,
u8 div)
92 if (divider->flags & CLK_DIVIDER_ONE_BASED)
94 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
97 return _get_table_val(divider->table, div);
101 static unsigned long clk_divider_recalc_rate(
struct clk_hw *
hw,
102 unsigned long parent_rate)
107 val =
readl(divider->reg) >> divider->shift;
110 div = _get_div(divider, val);
112 WARN(1,
"%s: Invalid divisor for clock %s\n", __func__,
117 return parent_rate /
div;
124 #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
126 static bool _is_valid_table_div(
const struct clk_div_table *table,
131 for (clkt = table; clkt->div; clkt++)
132 if (clkt->div == div)
137 static bool _is_valid_div(
struct clk_divider *divider,
unsigned int div)
139 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
142 return _is_valid_table_div(divider->table, div);
146 static int clk_divider_bestdiv(
struct clk_hw *hw,
unsigned long rate,
147 unsigned long *best_parent_rate)
151 unsigned long parent_rate, best = 0, now, maxdiv;
156 maxdiv = _get_maxdiv(divider);
159 parent_rate = *best_parent_rate;
161 bestdiv = bestdiv == 0 ? 1 : bestdiv;
162 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
172 for (i = 1; i <= maxdiv; i++) {
173 if (!_is_valid_div(divider, i))
177 now = parent_rate /
i;
178 if (now <= rate && now > best) {
181 *best_parent_rate = parent_rate;
186 bestdiv = _get_maxdiv(divider);
193 static long clk_divider_round_rate(
struct clk_hw *hw,
unsigned long rate,
194 unsigned long *
prate)
197 div = clk_divider_bestdiv(hw, rate, prate);
202 static int clk_divider_set_rate(
struct clk_hw *hw,
unsigned long rate,
203 unsigned long parent_rate)
207 unsigned long flags = 0;
210 div = parent_rate /
rate;
211 value = _get_val(divider, div);
219 val =
readl(divider->reg);
220 val &= ~(
div_mask(divider) << divider->shift);
221 val |= value << divider->shift;
222 writel(val, divider->reg);
225 spin_unlock_irqrestore(divider->lock, flags);
231 .recalc_rate = clk_divider_recalc_rate,
232 .round_rate = clk_divider_round_rate,
233 .set_rate = clk_divider_set_rate,
238 const char *parent_name,
unsigned long flags,
243 struct clk_divider *
div;
245 struct clk_init_data
init;
248 div = kzalloc(
sizeof(
struct clk_divider),
GFP_KERNEL);
250 pr_err(
"%s: could not allocate divider clk\n", __func__);
256 init.flags = flags | CLK_IS_BASIC;
257 init.parent_names = (parent_name ? &parent_name:
NULL);
258 init.num_parents = (parent_name ? 1 : 0);
264 div->flags = clk_divider_flags;
266 div->hw.init = &
init;
291 const char *parent_name,
unsigned long flags,
295 return _register_divider(dev, name, parent_name, flags, reg, shift,
296 width, clk_divider_flags,
NULL, lock);
314 const char *parent_name,
unsigned long flags,
319 return _register_divider(dev, name, parent_name, flags, reg, shift,
320 width, clk_divider_flags, table, lock);