1 // SPDX-License-Identifier: GPL-2.0
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);
75 if (flags & CLK_DIVIDER_EVEN_INTEGERS)
76 return 2 * (clk_div_mask(width) + 1);
78 return _get_table_maxdiv(table, width);
79 return clk_div_mask(width) + 1;
82 static unsigned int _get_table_div(const struct clk_div_table *table,
85 const struct clk_div_table *clkt;
87 for (clkt = table; clkt->div; clkt++)
93 static unsigned int _get_div(const struct clk_div_table *table,
94 unsigned int val, unsigned long flags, u8 width)
96 if (flags & CLK_DIVIDER_ONE_BASED)
98 if (flags & CLK_DIVIDER_POWER_OF_TWO)
100 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
101 return val ? val : clk_div_mask(width) + 1;
102 if (flags & CLK_DIVIDER_EVEN_INTEGERS)
103 return 2 * (val + 1);
105 return _get_table_div(table, val);
109 static unsigned int _get_table_val(const struct clk_div_table *table,
112 const struct clk_div_table *clkt;
114 for (clkt = table; clkt->div; clkt++)
115 if (clkt->div == div)
120 static unsigned int _get_val(const struct clk_div_table *table,
121 unsigned int div, unsigned long flags, u8 width)
123 if (flags & CLK_DIVIDER_ONE_BASED)
125 if (flags & CLK_DIVIDER_POWER_OF_TWO)
127 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
128 return (div == clk_div_mask(width) + 1) ? 0 : div;
129 if (flags & CLK_DIVIDER_EVEN_INTEGERS)
130 return (div >> 1) - 1;
132 return _get_table_val(table, div);
136 unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
138 const struct clk_div_table *table,
139 unsigned long flags, unsigned long width)
143 div = _get_div(table, val, flags, width);
145 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
146 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
147 clk_hw_get_name(hw));
151 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
153 EXPORT_SYMBOL_GPL(divider_recalc_rate);
155 static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
156 unsigned long parent_rate)
158 struct clk_divider *divider = to_clk_divider(hw);
161 val = clk_div_readl(divider) >> divider->shift;
162 val &= clk_div_mask(divider->width);
164 return divider_recalc_rate(hw, parent_rate, val, divider->table,
165 divider->flags, divider->width);
168 static bool _is_valid_table_div(const struct clk_div_table *table,
171 const struct clk_div_table *clkt;
173 for (clkt = table; clkt->div; clkt++)
174 if (clkt->div == div)
179 static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
182 if (flags & CLK_DIVIDER_POWER_OF_TWO)
183 return is_power_of_2(div);
185 return _is_valid_table_div(table, div);
189 static int _round_up_table(const struct clk_div_table *table, int div)
191 const struct clk_div_table *clkt;
194 for (clkt = table; clkt->div; clkt++) {
195 if (clkt->div == div)
197 else if (clkt->div < div)
200 if ((clkt->div - div) < (up - div))
207 static int _round_down_table(const struct clk_div_table *table, int div)
209 const struct clk_div_table *clkt;
210 int down = _get_table_mindiv(table);
212 for (clkt = table; clkt->div; clkt++) {
213 if (clkt->div == div)
215 else if (clkt->div > div)
218 if ((div - clkt->div) < (div - down))
225 static int _div_round_up(const struct clk_div_table *table,
226 unsigned long parent_rate, unsigned long rate,
229 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
231 if (flags & CLK_DIVIDER_POWER_OF_TWO)
232 div = __roundup_pow_of_two(div);
234 div = _round_up_table(table, div);
239 static int _div_round_closest(const struct clk_div_table *table,
240 unsigned long parent_rate, unsigned long rate,
244 unsigned long up_rate, down_rate;
246 up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
247 down = parent_rate / rate;
249 if (flags & CLK_DIVIDER_POWER_OF_TWO) {
250 up = __roundup_pow_of_two(up);
251 down = __rounddown_pow_of_two(down);
253 up = _round_up_table(table, up);
254 down = _round_down_table(table, down);
257 up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
258 down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
260 return (rate - up_rate) <= (down_rate - rate) ? up : down;
263 static int _div_round(const struct clk_div_table *table,
264 unsigned long parent_rate, unsigned long rate,
267 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
268 return _div_round_closest(table, parent_rate, rate, flags);
270 return _div_round_up(table, parent_rate, rate, flags);
273 static bool _is_best_div(unsigned long rate, unsigned long now,
274 unsigned long best, unsigned long flags)
276 if (flags & CLK_DIVIDER_ROUND_CLOSEST)
277 return abs(rate - now) < abs(rate - best);
279 return now <= rate && now > best;
282 static int _next_div(const struct clk_div_table *table, int div,
287 if (flags & CLK_DIVIDER_POWER_OF_TWO)
288 return __roundup_pow_of_two(div);
290 return _round_up_table(table, div);
295 static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
297 unsigned long *best_parent_rate,
298 const struct clk_div_table *table, u8 width,
302 unsigned long parent_rate, best = 0, now, maxdiv;
303 unsigned long parent_rate_saved = *best_parent_rate;
308 maxdiv = _get_maxdiv(table, width, flags);
310 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
311 parent_rate = *best_parent_rate;
312 bestdiv = _div_round(table, parent_rate, rate, flags);
313 bestdiv = bestdiv == 0 ? 1 : bestdiv;
314 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
319 * The maximum divider we can use without overflowing
320 * unsigned long in rate * i below
322 maxdiv = min(ULONG_MAX / rate, maxdiv);
324 for (i = _next_div(table, 0, flags); i <= maxdiv;
325 i = _next_div(table, i, flags)) {
326 if (rate * i == parent_rate_saved) {
328 * It's the most ideal case if the requested rate can be
329 * divided from parent clock without needing to change
330 * parent rate, so return the divider immediately.
332 *best_parent_rate = parent_rate_saved;
335 parent_rate = clk_hw_round_rate(parent, rate * i);
336 now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
337 if (_is_best_div(rate, now, best, flags)) {
340 *best_parent_rate = parent_rate;
345 bestdiv = _get_maxdiv(table, width, flags);
346 *best_parent_rate = clk_hw_round_rate(parent, 1);
352 int divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
353 const struct clk_div_table *table, u8 width,
358 div = clk_divider_bestdiv(hw, req->best_parent_hw, req->rate,
359 &req->best_parent_rate, table, width, flags);
361 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
365 EXPORT_SYMBOL_GPL(divider_determine_rate);
367 int divider_ro_determine_rate(struct clk_hw *hw, struct clk_rate_request *req,
368 const struct clk_div_table *table, u8 width,
369 unsigned long flags, unsigned int val)
373 div = _get_div(table, val, flags, width);
375 /* Even a read-only clock can propagate a rate change */
376 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
377 if (!req->best_parent_hw)
380 req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw,
384 req->rate = DIV_ROUND_UP_ULL((u64)req->best_parent_rate, div);
388 EXPORT_SYMBOL_GPL(divider_ro_determine_rate);
390 long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
391 unsigned long rate, unsigned long *prate,
392 const struct clk_div_table *table,
393 u8 width, unsigned long flags)
395 struct clk_rate_request req;
398 clk_hw_init_rate_request(hw, &req, rate);
399 req.best_parent_rate = *prate;
400 req.best_parent_hw = parent;
402 ret = divider_determine_rate(hw, &req, table, width, flags);
406 *prate = req.best_parent_rate;
410 EXPORT_SYMBOL_GPL(divider_round_rate_parent);
412 long divider_ro_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
413 unsigned long rate, unsigned long *prate,
414 const struct clk_div_table *table, u8 width,
415 unsigned long flags, unsigned int val)
417 struct clk_rate_request req;
420 clk_hw_init_rate_request(hw, &req, rate);
421 req.best_parent_rate = *prate;
422 req.best_parent_hw = parent;
424 ret = divider_ro_determine_rate(hw, &req, table, width, flags, val);
428 *prate = req.best_parent_rate;
432 EXPORT_SYMBOL_GPL(divider_ro_round_rate_parent);
434 static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
435 unsigned long *prate)
437 struct clk_divider *divider = to_clk_divider(hw);
439 /* if read only, just return current value */
440 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
443 val = clk_div_readl(divider) >> divider->shift;
444 val &= clk_div_mask(divider->width);
446 return divider_ro_round_rate(hw, rate, prate, divider->table,
447 divider->width, divider->flags,
451 return divider_round_rate(hw, rate, prate, divider->table,
452 divider->width, divider->flags);
455 static int clk_divider_determine_rate(struct clk_hw *hw,
456 struct clk_rate_request *req)
458 struct clk_divider *divider = to_clk_divider(hw);
460 /* if read only, just return current value */
461 if (divider->flags & CLK_DIVIDER_READ_ONLY) {
464 val = clk_div_readl(divider) >> divider->shift;
465 val &= clk_div_mask(divider->width);
467 return divider_ro_determine_rate(hw, req, divider->table,
469 divider->flags, val);
472 return divider_determine_rate(hw, req, divider->table, divider->width,
476 int divider_get_val(unsigned long rate, unsigned long parent_rate,
477 const struct clk_div_table *table, u8 width,
480 unsigned int div, value;
482 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
484 if (!_is_valid_div(table, div, flags))
487 value = _get_val(table, div, flags, width);
489 return min_t(unsigned int, value, clk_div_mask(width));
491 EXPORT_SYMBOL_GPL(divider_get_val);
493 static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
494 unsigned long parent_rate)
496 struct clk_divider *divider = to_clk_divider(hw);
498 unsigned long flags = 0;
501 value = divider_get_val(rate, parent_rate, divider->table,
502 divider->width, divider->flags);
507 spin_lock_irqsave(divider->lock, flags);
509 __acquire(divider->lock);
511 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
512 val = clk_div_mask(divider->width) << (divider->shift + 16);
514 val = clk_div_readl(divider);
515 val &= ~(clk_div_mask(divider->width) << divider->shift);
517 val |= (u32)value << divider->shift;
518 clk_div_writel(divider, val);
521 spin_unlock_irqrestore(divider->lock, flags);
523 __release(divider->lock);
528 const struct clk_ops clk_divider_ops = {
529 .recalc_rate = clk_divider_recalc_rate,
530 .round_rate = clk_divider_round_rate,
531 .determine_rate = clk_divider_determine_rate,
532 .set_rate = clk_divider_set_rate,
534 EXPORT_SYMBOL_GPL(clk_divider_ops);
536 const struct clk_ops clk_divider_ro_ops = {
537 .recalc_rate = clk_divider_recalc_rate,
538 .round_rate = clk_divider_round_rate,
539 .determine_rate = clk_divider_determine_rate,
541 EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
543 struct clk_hw *__clk_hw_register_divider(struct device *dev,
544 struct device_node *np, const char *name,
545 const char *parent_name, const struct clk_hw *parent_hw,
546 const struct clk_parent_data *parent_data, unsigned long flags,
547 void __iomem *reg, u8 shift, u8 width,
548 unsigned long clk_divider_flags,
549 const struct clk_div_table *table, spinlock_t *lock)
551 struct clk_divider *div;
553 struct clk_init_data init = {};
556 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
557 if (width + shift > 16) {
558 pr_warn("divider value exceeds LOWORD field\n");
559 return ERR_PTR(-EINVAL);
563 /* allocate the divider */
564 div = kzalloc(sizeof(*div), GFP_KERNEL);
566 return ERR_PTR(-ENOMEM);
569 if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
570 init.ops = &clk_divider_ro_ops;
572 init.ops = &clk_divider_ops;
574 init.parent_names = parent_name ? &parent_name : NULL;
575 init.parent_hws = parent_hw ? &parent_hw : NULL;
576 init.parent_data = parent_data;
577 if (parent_name || parent_hw || parent_data)
578 init.num_parents = 1;
580 init.num_parents = 0;
582 /* struct clk_divider assignments */
586 div->flags = clk_divider_flags;
588 div->hw.init = &init;
591 /* register the clock */
593 ret = clk_hw_register(dev, hw);
601 EXPORT_SYMBOL_GPL(__clk_hw_register_divider);
604 * clk_register_divider_table - register a table based divider clock with
605 * the clock framework
606 * @dev: device registering this clock
607 * @name: name of this clock
608 * @parent_name: name of clock's parent
609 * @flags: framework-specific flags
610 * @reg: register address to adjust divider
611 * @shift: number of bits to shift the bitfield
612 * @width: width of the bitfield
613 * @clk_divider_flags: divider-specific flags for this clock
614 * @table: array of divider/value pairs ending with a div set to 0
615 * @lock: shared register lock for this clock
617 struct clk *clk_register_divider_table(struct device *dev, const char *name,
618 const char *parent_name, unsigned long flags,
619 void __iomem *reg, u8 shift, u8 width,
620 unsigned long clk_divider_flags,
621 const struct clk_div_table *table, spinlock_t *lock)
625 hw = __clk_hw_register_divider(dev, NULL, name, parent_name, NULL,
626 NULL, flags, reg, shift, width, clk_divider_flags,
632 EXPORT_SYMBOL_GPL(clk_register_divider_table);
634 void clk_unregister_divider(struct clk *clk)
636 struct clk_divider *div;
639 hw = __clk_get_hw(clk);
643 div = to_clk_divider(hw);
648 EXPORT_SYMBOL_GPL(clk_unregister_divider);
651 * clk_hw_unregister_divider - unregister a clk divider
652 * @hw: hardware-specific clock data to unregister
654 void clk_hw_unregister_divider(struct clk_hw *hw)
656 struct clk_divider *div;
658 div = to_clk_divider(hw);
660 clk_hw_unregister(hw);
663 EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);
665 static void devm_clk_hw_release_divider(struct device *dev, void *res)
667 clk_hw_unregister_divider(*(struct clk_hw **)res);
670 struct clk_hw *__devm_clk_hw_register_divider(struct device *dev,
671 struct device_node *np, const char *name,
672 const char *parent_name, const struct clk_hw *parent_hw,
673 const struct clk_parent_data *parent_data, unsigned long flags,
674 void __iomem *reg, u8 shift, u8 width,
675 unsigned long clk_divider_flags,
676 const struct clk_div_table *table, spinlock_t *lock)
678 struct clk_hw **ptr, *hw;
680 ptr = devres_alloc(devm_clk_hw_release_divider, sizeof(*ptr), GFP_KERNEL);
682 return ERR_PTR(-ENOMEM);
684 hw = __clk_hw_register_divider(dev, np, name, parent_name, parent_hw,
685 parent_data, flags, reg, shift, width,
686 clk_divider_flags, table, lock);
690 devres_add(dev, ptr);
697 EXPORT_SYMBOL_GPL(__devm_clk_hw_register_divider);