clk: divider: fix calculation of maximal parent rate for a given divider
[deliverable/linux.git] / drivers / clk / clk-divider.c
1 /*
2 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
3 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
4 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
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>
19 #include <linux/log2.h>
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 = DIV_ROUND_UP(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
33 #define div_mask(width) ((1 << (width)) - 1)
34
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
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
57 static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
58 unsigned long flags)
59 {
60 if (flags & CLK_DIVIDER_ONE_BASED)
61 return div_mask(width);
62 if (flags & CLK_DIVIDER_POWER_OF_TWO)
63 return 1 << div_mask(width);
64 if (table)
65 return _get_table_maxdiv(table);
66 return div_mask(width) + 1;
67 }
68
69 static unsigned int _get_table_div(const struct clk_div_table *table,
70 unsigned int val)
71 {
72 const struct clk_div_table *clkt;
73
74 for (clkt = table; clkt->div; clkt++)
75 if (clkt->val == val)
76 return clkt->div;
77 return 0;
78 }
79
80 static unsigned int _get_div(const struct clk_div_table *table,
81 unsigned int val, unsigned long flags)
82 {
83 if (flags & CLK_DIVIDER_ONE_BASED)
84 return val;
85 if (flags & CLK_DIVIDER_POWER_OF_TWO)
86 return 1 << val;
87 if (table)
88 return _get_table_div(table, val);
89 return val + 1;
90 }
91
92 static unsigned int _get_table_val(const struct clk_div_table *table,
93 unsigned int div)
94 {
95 const struct clk_div_table *clkt;
96
97 for (clkt = table; clkt->div; clkt++)
98 if (clkt->div == div)
99 return clkt->val;
100 return 0;
101 }
102
103 static unsigned int _get_val(const struct clk_div_table *table,
104 unsigned int div, unsigned long flags)
105 {
106 if (flags & CLK_DIVIDER_ONE_BASED)
107 return div;
108 if (flags & CLK_DIVIDER_POWER_OF_TWO)
109 return __ffs(div);
110 if (table)
111 return _get_table_val(table, div);
112 return div - 1;
113 }
114
115 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
116 unsigned int val,
117 const struct clk_div_table *table,
118 unsigned long flags)
119 {
120 unsigned int div;
121
122 div = _get_div(table, val, flags);
123 if (!div) {
124 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
125 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
126 __clk_get_name(hw->clk));
127 return parent_rate;
128 }
129
130 return DIV_ROUND_UP(parent_rate, div);
131 }
132 EXPORT_SYMBOL_GPL(divider_recalc_rate);
133
134 static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
135 unsigned long parent_rate)
136 {
137 struct clk_divider *divider = to_clk_divider(hw);
138 unsigned int val;
139
140 val = clk_readl(divider->reg) >> divider->shift;
141 val &= div_mask(divider->width);
142
143 return divider_recalc_rate(hw, parent_rate, val, divider->table,
144 divider->flags);
145 }
146
147 static bool _is_valid_table_div(const struct clk_div_table *table,
148 unsigned int div)
149 {
150 const struct clk_div_table *clkt;
151
152 for (clkt = table; clkt->div; clkt++)
153 if (clkt->div == div)
154 return true;
155 return false;
156 }
157
158 static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
159 unsigned long flags)
160 {
161 if (flags & CLK_DIVIDER_POWER_OF_TWO)
162 return is_power_of_2(div);
163 if (table)
164 return _is_valid_table_div(table, div);
165 return true;
166 }
167
168 static int _round_up_table(const struct clk_div_table *table, int div)
169 {
170 const struct clk_div_table *clkt;
171 int up = INT_MAX;
172
173 for (clkt = table; clkt->div; clkt++) {
174 if (clkt->div == div)
175 return clkt->div;
176 else if (clkt->div < div)
177 continue;
178
179 if ((clkt->div - div) < (up - div))
180 up = clkt->div;
181 }
182
183 return up;
184 }
185
186 static int _round_down_table(const struct clk_div_table *table, int div)
187 {
188 const struct clk_div_table *clkt;
189 int down = _get_table_mindiv(table);
190
191 for (clkt = table; clkt->div; clkt++) {
192 if (clkt->div == div)
193 return clkt->div;
194 else if (clkt->div > div)
195 continue;
196
197 if ((div - clkt->div) < (div - down))
198 down = clkt->div;
199 }
200
201 return down;
202 }
203
204 static int _div_round_up(const struct clk_div_table *table,
205 unsigned long parent_rate, unsigned long rate,
206 unsigned long flags)
207 {
208 int div = DIV_ROUND_UP(parent_rate, rate);
209
210 if (flags & CLK_DIVIDER_POWER_OF_TWO)
211 div = __roundup_pow_of_two(div);
212 if (table)
213 div = _round_up_table(table, div);
214
215 return div;
216 }
217
218 static int _div_round_closest(const struct clk_div_table *table,
219 unsigned long parent_rate, unsigned long rate,
220 unsigned long flags)
221 {
222 int up, down, div;
223
224 up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
225
226 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
227 up = __roundup_pow_of_two(div);
228 down = __rounddown_pow_of_two(div);
229 } else if (table) {
230 up = _round_up_table(table, div);
231 down = _round_down_table(table, div);
232 }
233
234 return (up - div) <= (div - down) ? up : down;
235 }
236
237 static int _div_round(const struct clk_div_table *table,
238 unsigned long parent_rate, unsigned long rate,
239 unsigned long flags)
240 {
241 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
242 return _div_round_closest(table, parent_rate, rate, flags);
243
244 return _div_round_up(table, parent_rate, rate, flags);
245 }
246
247 static bool _is_best_div(unsigned long rate, unsigned long now,
248 unsigned long best, unsigned long flags)
249 {
250 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
251 return abs(rate - now) < abs(rate - best);
252
253 return now <= rate && now > best;
254 }
255
256 static int _next_div(const struct clk_div_table *table, int div,
257 unsigned long flags)
258 {
259 div++;
260
261 if (flags & CLK_DIVIDER_POWER_OF_TWO)
262 return __roundup_pow_of_two(div);
263 if (table)
264 return _round_up_table(table, div);
265
266 return div;
267 }
268
269 static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
270 unsigned long *best_parent_rate,
271 const struct clk_div_table *table, u8 width,
272 unsigned long flags)
273 {
274 int i, bestdiv = 0;
275 unsigned long parent_rate, best = 0, now, maxdiv;
276 unsigned long parent_rate_saved = *best_parent_rate;
277
278 if (!rate)
279 rate = 1;
280
281 maxdiv = _get_maxdiv(table, width, flags);
282
283 if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
284 parent_rate = *best_parent_rate;
285 bestdiv = _div_round(table, parent_rate, rate, flags);
286 bestdiv = bestdiv == 0 ? 1 : bestdiv;
287 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
288 return bestdiv;
289 }
290
291 /*
292 * The maximum divider we can use without overflowing
293 * unsigned long in rate * i below
294 */
295 maxdiv = min(ULONG_MAX / rate, maxdiv);
296
297 for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) {
298 if (!_is_valid_div(table, i, flags))
299 continue;
300 if (rate * i == parent_rate_saved) {
301 /*
302 * It's the most ideal case if the requested rate can be
303 * divided from parent clock without needing to change
304 * parent rate, so return the divider immediately.
305 */
306 *best_parent_rate = parent_rate_saved;
307 return i;
308 }
309 parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
310 rate * i);
311 now = DIV_ROUND_UP(parent_rate, i);
312 if (_is_best_div(rate, now, best, flags)) {
313 bestdiv = i;
314 best = now;
315 *best_parent_rate = parent_rate;
316 }
317 }
318
319 if (!bestdiv) {
320 bestdiv = _get_maxdiv(table, width, flags);
321 *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
322 }
323
324 return bestdiv;
325 }
326
327 long divider_round_rate(struct clk_hw *hw, unsigned long rate,
328 unsigned long *prate, const struct clk_div_table *table,
329 u8 width, unsigned long flags)
330 {
331 int div;
332
333 div = clk_divider_bestdiv(hw, rate, prate, table, width, flags);
334
335 return DIV_ROUND_UP(*prate, div);
336 }
337 EXPORT_SYMBOL_GPL(divider_round_rate);
338
339 static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
340 unsigned long *prate)
341 {
342 struct clk_divider *divider = to_clk_divider(hw);
343 int bestdiv;
344
345 /* if read only, just return current value */
346 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
347 bestdiv = readl(divider->reg) >> divider->shift;
348 bestdiv &= div_mask(divider->width);
349 bestdiv = _get_div(divider->table, bestdiv, divider->flags);
350 return DIV_ROUND_UP(*prate, bestdiv);
351 }
352
353 return divider_round_rate(hw, rate, prate, divider->table,
354 divider->width, divider->flags);
355 }
356
357 int divider_get_val(unsigned long rate, unsigned long parent_rate,
358 const struct clk_div_table *table, u8 width,
359 unsigned long flags)
360 {
361 unsigned int div, value;
362
363 div = DIV_ROUND_UP(parent_rate, rate);
364
365 if (!_is_valid_div(table, div, flags))
366 return -EINVAL;
367
368 value = _get_val(table, div, flags);
369
370 return min_t(unsigned int, value, div_mask(width));
371 }
372 EXPORT_SYMBOL_GPL(divider_get_val);
373
374 static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
375 unsigned long parent_rate)
376 {
377 struct clk_divider *divider = to_clk_divider(hw);
378 unsigned int value;
379 unsigned long flags = 0;
380 u32 val;
381
382 value = divider_get_val(rate, parent_rate, divider->table,
383 divider->width, divider->flags);
384
385 if (divider->lock)
386 spin_lock_irqsave(divider->lock, flags);
387
388 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
389 val = div_mask(divider->width) << (divider->shift + 16);
390 } else {
391 val = clk_readl(divider->reg);
392 val &= ~(div_mask(divider->width) << divider->shift);
393 }
394 val |= value << divider->shift;
395 clk_writel(val, divider->reg);
396
397 if (divider->lock)
398 spin_unlock_irqrestore(divider->lock, flags);
399
400 return 0;
401 }
402
403 const struct clk_ops clk_divider_ops = {
404 .recalc_rate = clk_divider_recalc_rate,
405 .round_rate = clk_divider_round_rate,
406 .set_rate = clk_divider_set_rate,
407 };
408 EXPORT_SYMBOL_GPL(clk_divider_ops);
409
410 static struct clk *_register_divider(struct device *dev, const char *name,
411 const char *parent_name, unsigned long flags,
412 void __iomem *reg, u8 shift, u8 width,
413 u8 clk_divider_flags, const struct clk_div_table *table,
414 spinlock_t *lock)
415 {
416 struct clk_divider *div;
417 struct clk *clk;
418 struct clk_init_data init;
419
420 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
421 if (width + shift > 16) {
422 pr_warn("divider value exceeds LOWORD field\n");
423 return ERR_PTR(-EINVAL);
424 }
425 }
426
427 /* allocate the divider */
428 div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
429 if (!div) {
430 pr_err("%s: could not allocate divider clk\n", __func__);
431 return ERR_PTR(-ENOMEM);
432 }
433
434 init.name = name;
435 init.ops = &clk_divider_ops;
436 init.flags = flags | CLK_IS_BASIC;
437 init.parent_names = (parent_name ? &parent_name: NULL);
438 init.num_parents = (parent_name ? 1 : 0);
439
440 /* struct clk_divider assignments */
441 div->reg = reg;
442 div->shift = shift;
443 div->width = width;
444 div->flags = clk_divider_flags;
445 div->lock = lock;
446 div->hw.init = &init;
447 div->table = table;
448
449 /* register the clock */
450 clk = clk_register(dev, &div->hw);
451
452 if (IS_ERR(clk))
453 kfree(div);
454
455 return clk;
456 }
457
458 /**
459 * clk_register_divider - register a divider clock with the clock framework
460 * @dev: device registering this clock
461 * @name: name of this clock
462 * @parent_name: name of clock's parent
463 * @flags: framework-specific flags
464 * @reg: register address to adjust divider
465 * @shift: number of bits to shift the bitfield
466 * @width: width of the bitfield
467 * @clk_divider_flags: divider-specific flags for this clock
468 * @lock: shared register lock for this clock
469 */
470 struct clk *clk_register_divider(struct device *dev, const char *name,
471 const char *parent_name, unsigned long flags,
472 void __iomem *reg, u8 shift, u8 width,
473 u8 clk_divider_flags, spinlock_t *lock)
474 {
475 return _register_divider(dev, name, parent_name, flags, reg, shift,
476 width, clk_divider_flags, NULL, lock);
477 }
478 EXPORT_SYMBOL_GPL(clk_register_divider);
479
480 /**
481 * clk_register_divider_table - register a table based divider clock with
482 * the clock framework
483 * @dev: device registering this clock
484 * @name: name of this clock
485 * @parent_name: name of clock's parent
486 * @flags: framework-specific flags
487 * @reg: register address to adjust divider
488 * @shift: number of bits to shift the bitfield
489 * @width: width of the bitfield
490 * @clk_divider_flags: divider-specific flags for this clock
491 * @table: array of divider/value pairs ending with a div set to 0
492 * @lock: shared register lock for this clock
493 */
494 struct clk *clk_register_divider_table(struct device *dev, const char *name,
495 const char *parent_name, unsigned long flags,
496 void __iomem *reg, u8 shift, u8 width,
497 u8 clk_divider_flags, const struct clk_div_table *table,
498 spinlock_t *lock)
499 {
500 return _register_divider(dev, name, parent_name, flags, reg, shift,
501 width, clk_divider_flags, table, lock);
502 }
503 EXPORT_SYMBOL_GPL(clk_register_divider_table);
504
505 void clk_unregister_divider(struct clk *clk)
506 {
507 struct clk_divider *div;
508 struct clk_hw *hw;
509
510 hw = __clk_get_hw(clk);
511 if (!hw)
512 return;
513
514 div = to_clk_divider(hw);
515
516 clk_unregister(clk);
517 kfree(div);
518 }
519 EXPORT_SYMBOL_GPL(clk_unregister_divider);
This page took 0.040738 seconds and 6 git commands to generate.