1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013 Texas Instruments, Inc.
7 * Tero Kristo <t-kristo@ti.com>
10 #include <linux/clk-provider.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
14 #include <linux/of_address.h>
15 #include <linux/clk/ti.h>
19 #define pr_fmt(fmt) "%s: " fmt, __func__
21 static unsigned int _get_table_div(const struct clk_div_table *table,
24 const struct clk_div_table *clkt;
26 for (clkt = table; clkt->div; clkt++)
32 static void _setup_mask(struct clk_omap_divider *divider)
36 const struct clk_div_table *clkt;
41 for (clkt = divider->table; clkt->div; clkt++)
42 if (clkt->val > max_val)
45 max_val = divider->max;
47 if (!(divider->flags & CLK_DIVIDER_ONE_BASED) &&
48 !(divider->flags & CLK_DIVIDER_POWER_OF_TWO))
52 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
53 mask = fls(max_val) - 1;
57 divider->mask = (1 << fls(mask)) - 1;
60 static unsigned int _get_div(struct clk_omap_divider *divider, unsigned int val)
62 if (divider->flags & CLK_DIVIDER_ONE_BASED)
64 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
67 return _get_table_div(divider->table, val);
71 static unsigned int _get_table_val(const struct clk_div_table *table,
74 const struct clk_div_table *clkt;
76 for (clkt = table; clkt->div; clkt++)
82 static unsigned int _get_val(struct clk_omap_divider *divider, u8 div)
84 if (divider->flags & CLK_DIVIDER_ONE_BASED)
86 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
89 return _get_table_val(divider->table, div);
93 static unsigned long ti_clk_divider_recalc_rate(struct clk_hw *hw,
94 unsigned long parent_rate)
96 struct clk_omap_divider *divider = to_clk_omap_divider(hw);
97 unsigned int div, val;
99 val = ti_clk_ll_ops->clk_readl(÷r->reg) >> divider->shift;
100 val &= divider->mask;
102 div = _get_div(divider, val);
104 WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
105 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
106 clk_hw_get_name(hw));
110 return DIV_ROUND_UP(parent_rate, div);
114 * The reverse of DIV_ROUND_UP: The maximum number which
117 #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
119 static bool _is_valid_table_div(const struct clk_div_table *table,
122 const struct clk_div_table *clkt;
124 for (clkt = table; clkt->div; clkt++)
125 if (clkt->div == div)
130 static bool _is_valid_div(struct clk_omap_divider *divider, unsigned int div)
132 if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
133 return is_power_of_2(div);
135 return _is_valid_table_div(divider->table, div);
139 static int _div_round_up(const struct clk_div_table *table,
140 unsigned long parent_rate, unsigned long rate)
142 const struct clk_div_table *clkt;
144 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
146 for (clkt = table; clkt->div; clkt++) {
147 if (clkt->div == div)
149 else if (clkt->div < div)
152 if ((clkt->div - div) < (up - div))
159 static int _div_round(const struct clk_div_table *table,
160 unsigned long parent_rate, unsigned long rate)
163 return DIV_ROUND_UP(parent_rate, rate);
165 return _div_round_up(table, parent_rate, rate);
168 static int ti_clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
169 unsigned long *best_parent_rate)
171 struct clk_omap_divider *divider = to_clk_omap_divider(hw);
173 unsigned long parent_rate, best = 0, now, maxdiv;
174 unsigned long parent_rate_saved = *best_parent_rate;
179 maxdiv = divider->max;
181 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
182 parent_rate = *best_parent_rate;
183 bestdiv = _div_round(divider->table, parent_rate, rate);
184 bestdiv = bestdiv == 0 ? 1 : bestdiv;
185 bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
190 * The maximum divider we can use without overflowing
191 * unsigned long in rate * i below
193 maxdiv = min(ULONG_MAX / rate, maxdiv);
195 for (i = 1; i <= maxdiv; i++) {
196 if (!_is_valid_div(divider, i))
198 if (rate * i == parent_rate_saved) {
200 * It's the most ideal case if the requested rate can be
201 * divided from parent clock without needing to change
202 * parent rate, so return the divider immediately.
204 *best_parent_rate = parent_rate_saved;
207 parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
208 MULT_ROUND_UP(rate, i));
209 now = DIV_ROUND_UP(parent_rate, i);
210 if (now <= rate && now > best) {
213 *best_parent_rate = parent_rate;
218 bestdiv = divider->max;
220 clk_hw_round_rate(clk_hw_get_parent(hw), 1);
226 static long ti_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
227 unsigned long *prate)
230 div = ti_clk_divider_bestdiv(hw, rate, prate);
232 return DIV_ROUND_UP(*prate, div);
235 static int ti_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
236 unsigned long parent_rate)
238 struct clk_omap_divider *divider;
239 unsigned int div, value;
245 divider = to_clk_omap_divider(hw);
247 div = DIV_ROUND_UP(parent_rate, rate);
249 if (div > divider->max)
251 if (div < divider->min)
254 value = _get_val(divider, div);
256 val = ti_clk_ll_ops->clk_readl(÷r->reg);
257 val &= ~(divider->mask << divider->shift);
258 val |= value << divider->shift;
259 ti_clk_ll_ops->clk_writel(val, ÷r->reg);
261 ti_clk_latch(÷r->reg, divider->latch);
267 * clk_divider_save_context - Save the divider value
268 * @hw: pointer struct clk_hw
270 * Save the divider value
272 static int clk_divider_save_context(struct clk_hw *hw)
274 struct clk_omap_divider *divider = to_clk_omap_divider(hw);
277 val = ti_clk_ll_ops->clk_readl(÷r->reg) >> divider->shift;
278 divider->context = val & divider->mask;
284 * clk_divider_restore_context - restore the saved the divider value
285 * @hw: pointer struct clk_hw
287 * Restore the saved the divider value
289 static void clk_divider_restore_context(struct clk_hw *hw)
291 struct clk_omap_divider *divider = to_clk_omap_divider(hw);
294 val = ti_clk_ll_ops->clk_readl(÷r->reg);
295 val &= ~(divider->mask << divider->shift);
296 val |= divider->context << divider->shift;
297 ti_clk_ll_ops->clk_writel(val, ÷r->reg);
300 const struct clk_ops ti_clk_divider_ops = {
301 .recalc_rate = ti_clk_divider_recalc_rate,
302 .round_rate = ti_clk_divider_round_rate,
303 .set_rate = ti_clk_divider_set_rate,
304 .save_context = clk_divider_save_context,
305 .restore_context = clk_divider_restore_context,
308 static struct clk *_register_divider(struct device_node *node,
310 struct clk_omap_divider *div)
313 struct clk_init_data init;
314 const char *parent_name;
317 parent_name = of_clk_get_parent_name(node, 0);
319 name = ti_dt_clk_name(node);
321 init.ops = &ti_clk_divider_ops;
323 init.parent_names = (parent_name ? &parent_name : NULL);
324 init.num_parents = (parent_name ? 1 : 0);
326 div->hw.init = &init;
328 /* register the clock */
329 clk = of_ti_clk_register(node, &div->hw, name);
337 int ti_clk_parse_divider_data(int *div_table, int num_dividers, int max_div,
338 u8 flags, struct clk_omap_divider *divider)
342 struct clk_div_table *tmp;
347 divider->max = max_div;
348 _setup_mask(divider);
354 while (!num_dividers || i < num_dividers) {
355 if (div_table[i] == -1)
364 tmp = kcalloc(valid_div + 1, sizeof(*tmp), GFP_KERNEL);
370 for (i = 0; i < num_dividers; i++)
371 if (div_table[i] > 0) {
372 tmp[valid_div].div = div_table[i];
373 tmp[valid_div].val = i;
375 if (div_table[i] > max_div)
376 max_div = div_table[i];
377 if (!min_div || div_table[i] < min_div)
378 min_div = div_table[i];
381 divider->min = min_div;
382 divider->max = max_div;
383 divider->table = tmp;
384 _setup_mask(divider);
389 static int __init ti_clk_get_div_table(struct device_node *node,
390 struct clk_omap_divider *div)
392 struct clk_div_table *table;
393 const __be32 *divspec;
399 divspec = of_get_property(node, "ti,dividers", &num_div);
408 /* Determine required size for divider table */
409 for (i = 0; i < num_div; i++) {
410 of_property_read_u32_index(node, "ti,dividers", i, &val);
416 pr_err("no valid dividers for %pOFn table\n", node);
420 table = kcalloc(valid_div + 1, sizeof(*table), GFP_KERNEL);
426 for (i = 0; i < num_div; i++) {
427 of_property_read_u32_index(node, "ti,dividers", i, &val);
429 table[valid_div].div = val;
430 table[valid_div].val = i;
440 static int _populate_divider_min_max(struct device_node *node,
441 struct clk_omap_divider *divider)
446 const struct clk_div_table *clkt;
448 if (!divider->table) {
449 /* Clk divider table not provided, determine min/max divs */
450 if (of_property_read_u32(node, "ti,min-div", &min_div))
453 if (of_property_read_u32(node, "ti,max-div", &max_div)) {
454 pr_err("no max-div for %pOFn!\n", node);
459 for (clkt = divider->table; clkt->div; clkt++) {
463 if (!min_div || val < min_div)
468 divider->min = min_div;
469 divider->max = max_div;
470 _setup_mask(divider);
475 static int __init ti_clk_divider_populate(struct device_node *node,
476 struct clk_omap_divider *div,
482 ret = ti_clk_get_reg_addr(node, 0, &div->reg);
486 if (!of_property_read_u32(node, "ti,bit-shift", &val))
491 if (!of_property_read_u32(node, "ti,latch-bit", &val))
494 div->latch = -EINVAL;
499 if (of_property_read_bool(node, "ti,index-starts-at-one"))
500 div->flags |= CLK_DIVIDER_ONE_BASED;
502 if (of_property_read_bool(node, "ti,index-power-of-two"))
503 div->flags |= CLK_DIVIDER_POWER_OF_TWO;
505 if (of_property_read_bool(node, "ti,set-rate-parent"))
506 *flags |= CLK_SET_RATE_PARENT;
508 ret = ti_clk_get_div_table(node, div);
512 return _populate_divider_min_max(node, div);
516 * of_ti_divider_clk_setup - Setup function for simple div rate clock
517 * @node: device node for this clock
519 * Sets up a basic divider clock.
521 static void __init of_ti_divider_clk_setup(struct device_node *node)
525 struct clk_omap_divider *div;
527 div = kzalloc(sizeof(*div), GFP_KERNEL);
531 if (ti_clk_divider_populate(node, div, &flags))
534 clk = _register_divider(node, flags, div);
536 of_clk_add_provider(node, of_clk_src_simple_get, clk);
537 of_ti_clk_autoidle_setup(node);
545 CLK_OF_DECLARE(divider_clk, "ti,divider-clock", of_ti_divider_clk_setup);
547 static void __init of_ti_composite_divider_clk_setup(struct device_node *node)
549 struct clk_omap_divider *div;
552 div = kzalloc(sizeof(*div), GFP_KERNEL);
556 if (ti_clk_divider_populate(node, div, &tmp))
559 if (!ti_clk_add_component(node, &div->hw, CLK_COMPONENT_TYPE_DIVIDER))
566 CLK_OF_DECLARE(ti_composite_divider_clk, "ti,composite-divider-clock",
567 of_ti_composite_divider_clk_setup);