1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
5 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
7 * Adjustable divider clock implementation
10 #include <linux/clk-provider.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/string.h>
17 #include <linux/log2.h>
20 * DOC: basic adjustable divider clock that cannot gate
22 * Traits of this clock:
23 * prepare - clk_prepare only ensures that parents are prepared
24 * enable - clk_enable only ensures that parents are enabled
25 * rate - rate is adjustable. clk->rate = ceiling(parent->rate / divisor)
26 * parent - fixed parent. No clk_set_parent support
29 static inline u32 clk_div_readl(struct clk_divider *divider)
31 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
32 return ioread32be(divider->reg);
34 return readl(divider->reg);
37 static inline void clk_div_writel(struct clk_divider *divider, u32 val)
39 if (divider->flags & CLK_DIVIDER_BIG_ENDIAN)
40 iowrite32be(val, divider->reg);
42 writel(val, divider->reg);
45 static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
48 unsigned int maxdiv = 0, mask = clk_div_mask(width);
49 const struct clk_div_table *clkt;
51 for (clkt = table; clkt->div; clkt++)
52 if (clkt->div > maxdiv && clkt->val <= mask)
57 static unsigned int _get_table_mindiv(const struct clk_div_table *table)
59 unsigned int mindiv = UINT_MAX;
60 const struct clk_div_table *clkt;
62 for (clkt = table; clkt->div; clkt++)
63 if (clkt->div < mindiv)
68 static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
71 if (flags & CLK_DIVIDER_ONE_BASED)
72 return clk_div_mask(width);
73 if (flags & CLK_DIVIDER_POWER_OF_TWO)
74 return 1 << clk_div_mask(width);
76 return _get_table_maxdiv(table, width);
77 return clk_div_mask(width) + 1;
80 static unsigned int _get_table_div(const struct clk_div_table *table,
83 const struct clk_div_table *clkt;
85 for (clkt = table; clkt->div; clkt++)
91 static unsigned int _get_div(const struct clk_div_table *table,
92 unsigned int val, unsigned long flags, u8 width)
94 if (flags & CLK_DIVIDER_ONE_BASED)
96 if (flags & CLK_DIVIDER_POWER_OF_TWO)
98 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
99 return val ? val : clk_div_mask(width) + 1;
101 return _get_table_div(table, val);
105 static unsigned int _get_table_val(const struct clk_div_table *table,
108 const struct clk_div_table *clkt;
110 for (clkt = table; clkt->div; clkt++)
111 if (clkt->div == div)
116 static unsigned int _get_val(const struct clk_div_table *table,
117 unsigned int div, unsigned long flags, u8 width)
119 if (flags & CLK_DIVIDER_ONE_BASED)
121 if (flags & CLK_DIVIDER_POWER_OF_TWO)
123 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
124 return (div == clk_div_mask(width) + 1) ? 0 : div;
126 return _get_table_val(table, div);
130 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
132 const struct clk_div_table *table,
133 unsigned long flags, unsigned long width)
137 div = _get_div(table, val, flags, width);
139 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
140 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
141 clk_hw_get_name(hw));
145 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
147 EXPORT_SYMBOL_GPL(divider_recalc_rate);
149 static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
150 unsigned long parent_rate)
152 struct clk_divider *divider = to_clk_divider(hw);
155 val = clk_div_readl(divider) >> divider->shift;
156 val &= clk_div_mask(divider->width);
158 return divider_recalc_rate(hw, parent_rate, val, divider->table,
159 divider->flags, divider->width);
162 static bool _is_valid_table_div(const struct clk_div_table *table,
165 const struct clk_div_table *clkt;
167 for (clkt = table; clkt->div; clkt++)
168 if (clkt->div == div)
173 static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
176 if (flags & CLK_DIVIDER_POWER_OF_TWO)
177 return is_power_of_2(div);
179 return _is_valid_table_div(table, div);
183 static int _round_up_table(const struct clk_div_table *table, int div)
185 const struct clk_div_table *clkt;
188 for (clkt = table; clkt->div; clkt++) {
189 if (clkt->div == div)
191 else if (clkt->div < div)
194 if ((clkt->div - div) < (up - div))
201 static int _round_down_table(const struct clk_div_table *table, int div)
203 const struct clk_div_table *clkt;
204 int down = _get_table_mindiv(table);
206 for (clkt = table; clkt->div; clkt++) {
207 if (clkt->div == div)
209 else if (clkt->div > div)
212 if ((div - clkt->div) < (div - down))
219 static int _div_round_up(const struct clk_div_table *table,
220 unsigned long parent_rate, unsigned long rate,
223 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
225 if (flags & CLK_DIVIDER_POWER_OF_TWO)
226 div = __roundup_pow_of_two(div);
228 div = _round_up_table(table, div);
233 static int _div_round_closest(const struct clk_div_table *table,
234 unsigned long parent_rate, unsigned long rate,
238 unsigned long up_rate, down_rate;
240 up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
241 down = parent_rate / rate;
243 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
244 up = __roundup_pow_of_two(up);
245 down = __rounddown_pow_of_two(down);
247 up = _round_up_table(table, up);
248 down = _round_down_table(table, down);
251 up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
252 down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
254 return (rate - up_rate) <= (down_rate - rate) ? up : down;
257 static int _div_round(const struct clk_div_table *table,
258 unsigned long parent_rate, unsigned long rate,
261 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
262 return _div_round_closest(table, parent_rate, rate, flags);
264 return _div_round_up(table, parent_rate, rate, flags);
267 static bool _is_best_div(unsigned long rate, unsigned long now,
268 unsigned long best, unsigned long flags)
270 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
271 return abs(rate - now) < abs(rate - best);
273 return now <= rate && now > best;
276 static int _next_div(const struct clk_div_table *table, int div,
281 if (flags & CLK_DIVIDER_POWER_OF_TWO)
282 return __roundup_pow_of_two(div);
284 return _round_up_table(table, div);
289 static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
291 unsigned long *best_parent_rate,
292 const struct clk_div_table *table, u8 width,
296 unsigned long parent_rate, best = 0, now, maxdiv;
297 unsigned long parent_rate_saved = *best_parent_rate;
302 maxdiv = _get_maxdiv(table, width, flags);
304 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
305 parent_rate = *best_parent_rate;
306 bestdiv = _div_round(table, parent_rate, rate, flags);
307 bestdiv = bestdiv == 0 ? 1 : bestdiv;
308 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
313 * The maximum divider we can use without overflowing
314 * unsigned long in rate * i below
316 maxdiv = min(ULONG_MAX / rate, maxdiv);
318 for (i = _next_div(table, 0, flags); i <= maxdiv;
319 i = _next_div(table, i, flags)) {
320 if (rate * i == parent_rate_saved) {
322 * It's the most ideal case if the requested rate can be
323 * divided from parent clock without needing to change
324 * parent rate, so return the divider immediately.
326 *best_parent_rate = parent_rate_saved;
329 parent_rate = clk_hw_round_rate(parent, rate * i);
330 now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
331 if (_is_best_div(rate, now, best, flags)) {
334 *best_parent_rate = parent_rate;
339 bestdiv = _get_maxdiv(table, width, flags);
340 *best_parent_rate = clk_hw_round_rate(parent, 1);
346 int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
347 const struct clk_div_table *table, u8 width,
352 div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate,
353 &req->best_parent_rate, table, width, flags);
355 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
359 EXPORT_SYMBOL_GPL(divider_determine_rate);
361 int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
362 const struct clk_div_table *table, u8 width,
363 unsigned long flags, unsigned int val)
367 div = _get_div(table, val, flags, width);
369 /* Even a read-only clock can propagate a rate change */
370 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
371 if (!req->best_parent_hw)
374 req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw,
378 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
382 EXPORT_SYMBOL_GPL(divider_ro_determine_rate);
384 long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
385 unsigned long rate, unsigned long *prate,
386 const struct clk_div_table *table,
387 u8 width, unsigned long flags)
389 struct clk_rate_request req;
392 clk_hw_init_rate_request(hw, &req, rate);
393 req.best_parent_rate = *prate;
394 req.best_parent_hw = parent;
396 ret = divider_determine_rate(hw, &req, table, width, flags);
400 *prate = req.best_parent_rate;
404 EXPORT_SYMBOL_GPL(divider_round_rate_parent);
406 long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
407 unsigned long rate, unsigned long *prate,
408 const struct clk_div_table *table, u8 width,
409 unsigned long flags, unsigned int val)
411 struct clk_rate_request req;
414 clk_hw_init_rate_request(hw, &req, rate);
415 req.best_parent_rate = *prate;
416 req.best_parent_hw = parent;
418 ret = divider_ro_determine_rate(hw, &req, table, width, flags, val);
422 *prate = req.best_parent_rate;
426 EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
428 static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
429 unsigned long *prate)
431 struct clk_divider *divider = to_clk_divider(hw);
433 /* if read only, just return current value */
434 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
437 val = clk_div_readl(divider) >> divider->shift;
438 val &= clk_div_mask(divider->width);
440 return divider_ro_round_rate(hw, rate, prate, divider->table,
441 divider->width, divider->flags,
445 return divider_round_rate(hw, rate, prate, divider->table,
446 divider->width, divider->flags);
449 static int clk_divider_determine_rate(struct clk_hw *hw,
450 struct clk_rate_request *req)
452 struct clk_divider *divider = to_clk_divider(hw);
454 /* if read only, just return current value */
455 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
458 val = clk_div_readl(divider) >> divider->shift;
459 val &= clk_div_mask(divider->width);
461 return divider_ro_determine_rate(hw, req, divider->table,
463 divider->flags, val);
466 return divider_determine_rate(hw, req, divider->table, divider->width,
470 int divider_get_val(unsigned long rate, unsigned long parent_rate,
471 const struct clk_div_table *table, u8 width,
474 unsigned int div, value;
476 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
478 if (!_is_valid_div(table, div, flags))
481 value = _get_val(table, div, flags, width);
483 return min_t(unsigned int, value, clk_div_mask(width));
485 EXPORT_SYMBOL_GPL(divider_get_val);
487 static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
488 unsigned long parent_rate)
490 struct clk_divider *divider = to_clk_divider(hw);
492 unsigned long flags = 0;
495 value = divider_get_val(rate, parent_rate, divider->table,
496 divider->width, divider->flags);
501 spin_lock_irqsave(divider->lock, flags);
503 __acquire(divider->lock);
505 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
506 val = clk_div_mask(divider->width) << (divider->shift + 16);
508 val = clk_div_readl(divider);
509 val &= ~(clk_div_mask(divider->width) << divider->shift);
511 val |= (u32)value << divider->shift;
512 clk_div_writel(divider, val);
515 spin_unlock_irqrestore(divider->lock, flags);
517 __release(divider->lock);
522 const struct clk_ops clk_divider_ops = {
523 .recalc_rate = clk_divider_recalc_rate,
524 .round_rate = clk_divider_round_rate,
525 .determine_rate = clk_divider_determine_rate,
526 .set_rate = clk_divider_set_rate,
528 EXPORT_SYMBOL_GPL(clk_divider_ops);
530 const struct clk_ops clk_divider_ro_ops = {
531 .recalc_rate = clk_divider_recalc_rate,
532 .round_rate = clk_divider_round_rate,
533 .determine_rate = clk_divider_determine_rate,
535 EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
537 struct clk_hw *__clk_hw_register_divider(struct device *dev,
538 struct device_node *np, const char *name,
539 const char *parent_name, const struct clk_hw *parent_hw,
540 const struct clk_parent_data *parent_data, unsigned long flags,
541 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
542 const struct clk_div_table *table, spinlock_t *lock)
544 struct clk_divider *div;
546 struct clk_init_data init = {};
549 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
550 if (width + shift > 16) {
551 pr_warn("divider value exceeds LOWORD field\n");
552 return ERR_PTR(-EINVAL);
556 /* allocate the divider */
557 div = kzalloc(sizeof(*div), GFP_KERNEL);
559 return ERR_PTR(-ENOMEM);
562 if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
563 init.ops = &clk_divider_ro_ops;
565 init.ops = &clk_divider_ops;
567 init.parent_names = parent_name ? &parent_name : NULL;
568 init.parent_hws = parent_hw ? &parent_hw : NULL;
569 init.parent_data = parent_data;
570 if (parent_name || parent_hw || parent_data)
571 init.num_parents = 1;
573 init.num_parents = 0;
575 /* struct clk_divider assignments */
579 div->flags = clk_divider_flags;
581 div->hw.init = &init;
584 /* register the clock */
586 ret = clk_hw_register(dev, hw);
594 EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
597 * clk_register_divider_table - register a table based divider clock with
598 * the clock framework
599 * @dev: device registering this clock
600 * @name: name of this clock
601 * @parent_name: name of clock's parent
602 * @flags: framework-specific flags
603 * @reg: register address to adjust divider
604 * @shift: number of bits to shift the bitfield
605 * @width: width of the bitfield
606 * @clk_divider_flags: divider-specific flags for this clock
607 * @table: array of divider/value pairs ending with a div set to 0
608 * @lock: shared register lock for this clock
610 struct clk *clk_register_divider_table(struct device *dev, const char *name,
611 const char *parent_name, unsigned long flags,
612 void __iomem *reg, u8 shift, u8 width,
613 u8 clk_divider_flags, const struct clk_div_table *table,
618 hw = __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
619 NULL, flags, reg, shift, width, clk_divider_flags,
625 EXPORT_SYMBOL_GPL(clk_register_divider_table);
627 void clk_unregister_divider(struct clk *clk)
629 struct clk_divider *div;
632 hw = __clk_get_hw(clk);
636 div = to_clk_divider(hw);
641 EXPORT_SYMBOL_GPL(clk_unregister_divider);
644 * clk_hw_unregister_divider - unregister a clk divider
645 * @hw: hardware-specific clock data to unregister
647 void clk_hw_unregister_divider(struct clk_hw *hw)
649 struct clk_divider *div;
651 div = to_clk_divider(hw);
653 clk_hw_unregister(hw);
656 EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);
658 static void devm_clk_hw_release_divider(struct device *dev, void *res)
660 clk_hw_unregister_divider(*(struct clk_hw **)res);
663 struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
664 struct device_node *np, const char *name,
665 const char *parent_name, const struct clk_hw *parent_hw,
666 const struct clk_parent_data *parent_data, unsigned long flags,
667 void __iomem *reg, u8 shift, u8 width, u8 clk_divider_flags,
668 const struct clk_div_table *table, spinlock_t *lock)
670 struct clk_hw **ptr, *hw;
672 ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL);
674 return ERR_PTR(-ENOMEM);
676 hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw,
677 parent_data, flags, reg, shift, width,
678 clk_divider_flags, table, lock);
682 devres_add(dev, ptr);
689 EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider);