]>
Commit | Line | Data |
---|---|---|
9d9f78ed MT |
1 | /* |
2 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <[email protected]> | |
3 | * Copyright (C) 2011 Richard Zhao, Linaro <[email protected]> | |
4 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * Adjustable divider clock implementation | |
11 | */ | |
12 | ||
13 | #include <linux/clk-provider.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/string.h> | |
1a3cd184 | 19 | #include <linux/log2.h> |
9d9f78ed MT |
20 | |
21 | /* | |
22 | * DOC: basic adjustable divider clock that cannot gate | |
23 | * | |
24 | * Traits of this clock: | |
25 | * prepare - clk_prepare only ensures that parents are prepared | |
26 | * enable - clk_enable only ensures that parents are enabled | |
27 | * rate - rate is adjustable. clk->rate = parent->rate / divisor | |
28 | * parent - fixed parent. No clk_set_parent support | |
29 | */ | |
30 | ||
31 | #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw) | |
32 | ||
1a3cd184 | 33 | #define div_mask(d) ((1 << ((d)->width)) - 1) |
6d9252bd | 34 | |
357c3f0a RN |
35 | static unsigned int _get_table_maxdiv(const struct clk_div_table *table) |
36 | { | |
37 | unsigned int maxdiv = 0; | |
38 | const struct clk_div_table *clkt; | |
39 | ||
40 | for (clkt = table; clkt->div; clkt++) | |
41 | if (clkt->div > maxdiv) | |
42 | maxdiv = clkt->div; | |
43 | return maxdiv; | |
44 | } | |
45 | ||
6d9252bd RN |
46 | static unsigned int _get_maxdiv(struct clk_divider *divider) |
47 | { | |
48 | if (divider->flags & CLK_DIVIDER_ONE_BASED) | |
49 | return div_mask(divider); | |
50 | if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) | |
51 | return 1 << div_mask(divider); | |
357c3f0a RN |
52 | if (divider->table) |
53 | return _get_table_maxdiv(divider->table); | |
6d9252bd RN |
54 | return div_mask(divider) + 1; |
55 | } | |
56 | ||
357c3f0a RN |
57 | static unsigned int _get_table_div(const struct clk_div_table *table, |
58 | unsigned int val) | |
59 | { | |
60 | const struct clk_div_table *clkt; | |
61 | ||
62 | for (clkt = table; clkt->div; clkt++) | |
63 | if (clkt->val == val) | |
64 | return clkt->div; | |
65 | return 0; | |
66 | } | |
67 | ||
6d9252bd RN |
68 | static unsigned int _get_div(struct clk_divider *divider, unsigned int val) |
69 | { | |
70 | if (divider->flags & CLK_DIVIDER_ONE_BASED) | |
71 | return val; | |
72 | if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) | |
73 | return 1 << val; | |
357c3f0a RN |
74 | if (divider->table) |
75 | return _get_table_div(divider->table, val); | |
6d9252bd RN |
76 | return val + 1; |
77 | } | |
78 | ||
357c3f0a RN |
79 | static unsigned int _get_table_val(const struct clk_div_table *table, |
80 | unsigned int div) | |
81 | { | |
82 | const struct clk_div_table *clkt; | |
83 | ||
84 | for (clkt = table; clkt->div; clkt++) | |
85 | if (clkt->div == div) | |
86 | return clkt->val; | |
87 | return 0; | |
88 | } | |
89 | ||
6d9252bd RN |
90 | static unsigned int _get_val(struct clk_divider *divider, u8 div) |
91 | { | |
92 | if (divider->flags & CLK_DIVIDER_ONE_BASED) | |
93 | return div; | |
94 | if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) | |
95 | return __ffs(div); | |
357c3f0a RN |
96 | if (divider->table) |
97 | return _get_table_val(divider->table, div); | |
6d9252bd RN |
98 | return div - 1; |
99 | } | |
9d9f78ed MT |
100 | |
101 | static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, | |
102 | unsigned long parent_rate) | |
103 | { | |
104 | struct clk_divider *divider = to_clk_divider(hw); | |
6d9252bd | 105 | unsigned int div, val; |
9d9f78ed | 106 | |
6d9252bd RN |
107 | val = readl(divider->reg) >> divider->shift; |
108 | val &= div_mask(divider); | |
9d9f78ed | 109 | |
6d9252bd RN |
110 | div = _get_div(divider, val); |
111 | if (!div) { | |
056b2053 SB |
112 | WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO), |
113 | "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", | |
114 | __clk_get_name(hw->clk)); | |
6d9252bd RN |
115 | return parent_rate; |
116 | } | |
9d9f78ed MT |
117 | |
118 | return parent_rate / div; | |
119 | } | |
9d9f78ed MT |
120 | |
121 | /* | |
122 | * The reverse of DIV_ROUND_UP: The maximum number which | |
123 | * divided by m is r | |
124 | */ | |
125 | #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1) | |
126 | ||
357c3f0a RN |
127 | static bool _is_valid_table_div(const struct clk_div_table *table, |
128 | unsigned int div) | |
129 | { | |
130 | const struct clk_div_table *clkt; | |
131 | ||
132 | for (clkt = table; clkt->div; clkt++) | |
133 | if (clkt->div == div) | |
134 | return true; | |
135 | return false; | |
136 | } | |
137 | ||
138 | static bool _is_valid_div(struct clk_divider *divider, unsigned int div) | |
139 | { | |
140 | if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) | |
1a3cd184 | 141 | return is_power_of_2(div); |
357c3f0a RN |
142 | if (divider->table) |
143 | return _is_valid_table_div(divider->table, div); | |
144 | return true; | |
145 | } | |
146 | ||
9d9f78ed MT |
147 | static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate, |
148 | unsigned long *best_parent_rate) | |
149 | { | |
150 | struct clk_divider *divider = to_clk_divider(hw); | |
151 | int i, bestdiv = 0; | |
152 | unsigned long parent_rate, best = 0, now, maxdiv; | |
081c9025 | 153 | unsigned long parent_rate_saved = *best_parent_rate; |
9d9f78ed MT |
154 | |
155 | if (!rate) | |
156 | rate = 1; | |
157 | ||
6d9252bd | 158 | maxdiv = _get_maxdiv(divider); |
9d9f78ed | 159 | |
81536e07 SG |
160 | if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) { |
161 | parent_rate = *best_parent_rate; | |
9d9f78ed MT |
162 | bestdiv = DIV_ROUND_UP(parent_rate, rate); |
163 | bestdiv = bestdiv == 0 ? 1 : bestdiv; | |
164 | bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv; | |
165 | return bestdiv; | |
166 | } | |
167 | ||
168 | /* | |
169 | * The maximum divider we can use without overflowing | |
170 | * unsigned long in rate * i below | |
171 | */ | |
172 | maxdiv = min(ULONG_MAX / rate, maxdiv); | |
173 | ||
174 | for (i = 1; i <= maxdiv; i++) { | |
357c3f0a | 175 | if (!_is_valid_div(divider, i)) |
6d9252bd | 176 | continue; |
081c9025 SG |
177 | if (rate * i == parent_rate_saved) { |
178 | /* | |
179 | * It's the most ideal case if the requested rate can be | |
180 | * divided from parent clock without needing to change | |
181 | * parent rate, so return the divider immediately. | |
182 | */ | |
183 | *best_parent_rate = parent_rate_saved; | |
184 | return i; | |
185 | } | |
9d9f78ed MT |
186 | parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), |
187 | MULT_ROUND_UP(rate, i)); | |
188 | now = parent_rate / i; | |
189 | if (now <= rate && now > best) { | |
190 | bestdiv = i; | |
191 | best = now; | |
192 | *best_parent_rate = parent_rate; | |
193 | } | |
194 | } | |
195 | ||
196 | if (!bestdiv) { | |
6d9252bd | 197 | bestdiv = _get_maxdiv(divider); |
9d9f78ed MT |
198 | *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1); |
199 | } | |
200 | ||
201 | return bestdiv; | |
202 | } | |
203 | ||
204 | static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate, | |
205 | unsigned long *prate) | |
206 | { | |
207 | int div; | |
208 | div = clk_divider_bestdiv(hw, rate, prate); | |
209 | ||
81536e07 | 210 | return *prate / div; |
9d9f78ed | 211 | } |
9d9f78ed | 212 | |
1c0035d7 SG |
213 | static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, |
214 | unsigned long parent_rate) | |
9d9f78ed MT |
215 | { |
216 | struct clk_divider *divider = to_clk_divider(hw); | |
6d9252bd | 217 | unsigned int div, value; |
9d9f78ed MT |
218 | unsigned long flags = 0; |
219 | u32 val; | |
220 | ||
1c0035d7 | 221 | div = parent_rate / rate; |
6d9252bd | 222 | value = _get_val(divider, div); |
9d9f78ed | 223 | |
6d9252bd RN |
224 | if (value > div_mask(divider)) |
225 | value = div_mask(divider); | |
9d9f78ed MT |
226 | |
227 | if (divider->lock) | |
228 | spin_lock_irqsave(divider->lock, flags); | |
229 | ||
230 | val = readl(divider->reg); | |
231 | val &= ~(div_mask(divider) << divider->shift); | |
6d9252bd | 232 | val |= value << divider->shift; |
9d9f78ed MT |
233 | writel(val, divider->reg); |
234 | ||
235 | if (divider->lock) | |
236 | spin_unlock_irqrestore(divider->lock, flags); | |
237 | ||
238 | return 0; | |
239 | } | |
9d9f78ed | 240 | |
822c250e | 241 | const struct clk_ops clk_divider_ops = { |
9d9f78ed MT |
242 | .recalc_rate = clk_divider_recalc_rate, |
243 | .round_rate = clk_divider_round_rate, | |
244 | .set_rate = clk_divider_set_rate, | |
245 | }; | |
246 | EXPORT_SYMBOL_GPL(clk_divider_ops); | |
247 | ||
357c3f0a | 248 | static struct clk *_register_divider(struct device *dev, const char *name, |
9d9f78ed MT |
249 | const char *parent_name, unsigned long flags, |
250 | void __iomem *reg, u8 shift, u8 width, | |
357c3f0a RN |
251 | u8 clk_divider_flags, const struct clk_div_table *table, |
252 | spinlock_t *lock) | |
9d9f78ed MT |
253 | { |
254 | struct clk_divider *div; | |
255 | struct clk *clk; | |
0197b3ea | 256 | struct clk_init_data init; |
9d9f78ed | 257 | |
27d54591 | 258 | /* allocate the divider */ |
9d9f78ed | 259 | div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL); |
9d9f78ed MT |
260 | if (!div) { |
261 | pr_err("%s: could not allocate divider clk\n", __func__); | |
27d54591 | 262 | return ERR_PTR(-ENOMEM); |
9d9f78ed MT |
263 | } |
264 | ||
0197b3ea SK |
265 | init.name = name; |
266 | init.ops = &clk_divider_ops; | |
f7d8caad | 267 | init.flags = flags | CLK_IS_BASIC; |
0197b3ea SK |
268 | init.parent_names = (parent_name ? &parent_name: NULL); |
269 | init.num_parents = (parent_name ? 1 : 0); | |
270 | ||
9d9f78ed MT |
271 | /* struct clk_divider assignments */ |
272 | div->reg = reg; | |
273 | div->shift = shift; | |
274 | div->width = width; | |
275 | div->flags = clk_divider_flags; | |
276 | div->lock = lock; | |
0197b3ea | 277 | div->hw.init = &init; |
357c3f0a | 278 | div->table = table; |
9d9f78ed | 279 | |
27d54591 | 280 | /* register the clock */ |
0197b3ea | 281 | clk = clk_register(dev, &div->hw); |
9d9f78ed | 282 | |
27d54591 MT |
283 | if (IS_ERR(clk)) |
284 | kfree(div); | |
9d9f78ed | 285 | |
27d54591 | 286 | return clk; |
9d9f78ed | 287 | } |
357c3f0a RN |
288 | |
289 | /** | |
290 | * clk_register_divider - register a divider clock with the clock framework | |
291 | * @dev: device registering this clock | |
292 | * @name: name of this clock | |
293 | * @parent_name: name of clock's parent | |
294 | * @flags: framework-specific flags | |
295 | * @reg: register address to adjust divider | |
296 | * @shift: number of bits to shift the bitfield | |
297 | * @width: width of the bitfield | |
298 | * @clk_divider_flags: divider-specific flags for this clock | |
299 | * @lock: shared register lock for this clock | |
300 | */ | |
301 | struct clk *clk_register_divider(struct device *dev, const char *name, | |
302 | const char *parent_name, unsigned long flags, | |
303 | void __iomem *reg, u8 shift, u8 width, | |
304 | u8 clk_divider_flags, spinlock_t *lock) | |
305 | { | |
306 | return _register_divider(dev, name, parent_name, flags, reg, shift, | |
307 | width, clk_divider_flags, NULL, lock); | |
308 | } | |
309 | ||
310 | /** | |
311 | * clk_register_divider_table - register a table based divider clock with | |
312 | * the clock framework | |
313 | * @dev: device registering this clock | |
314 | * @name: name of this clock | |
315 | * @parent_name: name of clock's parent | |
316 | * @flags: framework-specific flags | |
317 | * @reg: register address to adjust divider | |
318 | * @shift: number of bits to shift the bitfield | |
319 | * @width: width of the bitfield | |
320 | * @clk_divider_flags: divider-specific flags for this clock | |
321 | * @table: array of divider/value pairs ending with a div set to 0 | |
322 | * @lock: shared register lock for this clock | |
323 | */ | |
324 | struct clk *clk_register_divider_table(struct device *dev, const char *name, | |
325 | const char *parent_name, unsigned long flags, | |
326 | void __iomem *reg, u8 shift, u8 width, | |
327 | u8 clk_divider_flags, const struct clk_div_table *table, | |
328 | spinlock_t *lock) | |
329 | { | |
330 | return _register_divider(dev, name, parent_name, flags, reg, shift, | |
331 | width, clk_divider_flags, table, lock); | |
332 | } |