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
)
312 struct clk_init_data init
;
313 const char *parent_name
;
316 parent_name
= of_clk_get_parent_name(node
, 0);
318 name
= ti_dt_clk_name(node
);
320 init
.ops
= &ti_clk_divider_ops
;
322 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
323 init
.num_parents
= (parent_name
? 1 : 0);
325 div
->hw
.init
= &init
;
327 /* register the clock */
328 return of_ti_clk_register(node
, &div
->hw
, name
);
331 int ti_clk_parse_divider_data(int *div_table
, int num_dividers
, int max_div
,
332 u8 flags
, struct clk_omap_divider
*divider
)
336 struct clk_div_table
*tmp
;
341 divider
->max
= max_div
;
342 _setup_mask(divider
);
348 while (!num_dividers
|| i
< num_dividers
) {
349 if (div_table
[i
] == -1)
358 tmp
= kcalloc(valid_div
+ 1, sizeof(*tmp
), GFP_KERNEL
);
364 for (i
= 0; i
< num_dividers
; i
++)
365 if (div_table
[i
] > 0) {
366 tmp
[valid_div
].div
= div_table
[i
];
367 tmp
[valid_div
].val
= i
;
369 if (div_table
[i
] > max_div
)
370 max_div
= div_table
[i
];
371 if (!min_div
|| div_table
[i
] < min_div
)
372 min_div
= div_table
[i
];
375 divider
->min
= min_div
;
376 divider
->max
= max_div
;
377 divider
->table
= tmp
;
378 _setup_mask(divider
);
383 static int __init
ti_clk_get_div_table(struct device_node
*node
,
384 struct clk_omap_divider
*div
)
386 struct clk_div_table
*table
;
387 const __be32
*divspec
;
393 divspec
= of_get_property(node
, "ti,dividers", &num_div
);
402 /* Determine required size for divider table */
403 for (i
= 0; i
< num_div
; i
++) {
404 of_property_read_u32_index(node
, "ti,dividers", i
, &val
);
410 pr_err("no valid dividers for %pOFn table\n", node
);
414 table
= kcalloc(valid_div
+ 1, sizeof(*table
), GFP_KERNEL
);
420 for (i
= 0; i
< num_div
; i
++) {
421 of_property_read_u32_index(node
, "ti,dividers", i
, &val
);
423 table
[valid_div
].div
= val
;
424 table
[valid_div
].val
= i
;
434 static int _populate_divider_min_max(struct device_node
*node
,
435 struct clk_omap_divider
*divider
)
440 const struct clk_div_table
*clkt
;
442 if (!divider
->table
) {
443 /* Clk divider table not provided, determine min/max divs */
444 if (of_property_read_u32(node
, "ti,min-div", &min_div
))
447 if (of_property_read_u32(node
, "ti,max-div", &max_div
)) {
448 pr_err("no max-div for %pOFn!\n", node
);
453 for (clkt
= divider
->table
; clkt
->div
; clkt
++) {
457 if (!min_div
|| val
< min_div
)
462 divider
->min
= min_div
;
463 divider
->max
= max_div
;
464 _setup_mask(divider
);
469 static int __init
ti_clk_divider_populate(struct device_node
*node
,
470 struct clk_omap_divider
*div
,
476 ret
= ti_clk_get_reg_addr(node
, 0, &div
->reg
);
480 div
->shift
= div
->reg
.bit
;
482 if (!of_property_read_u32(node
, "ti,latch-bit", &val
))
485 div
->latch
= -EINVAL
;
490 if (of_property_read_bool(node
, "ti,index-starts-at-one"))
491 div
->flags
|= CLK_DIVIDER_ONE_BASED
;
493 if (of_property_read_bool(node
, "ti,index-power-of-two"))
494 div
->flags
|= CLK_DIVIDER_POWER_OF_TWO
;
496 if (of_property_read_bool(node
, "ti,set-rate-parent"))
497 *flags
|= CLK_SET_RATE_PARENT
;
499 ret
= ti_clk_get_div_table(node
, div
);
503 return _populate_divider_min_max(node
, div
);
507 * of_ti_divider_clk_setup - Setup function for simple div rate clock
508 * @node: device node for this clock
510 * Sets up a basic divider clock.
512 static void __init
of_ti_divider_clk_setup(struct device_node
*node
)
516 struct clk_omap_divider
*div
;
518 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
522 if (ti_clk_divider_populate(node
, div
, &flags
))
525 clk
= _register_divider(node
, flags
, div
);
527 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
528 of_ti_clk_autoidle_setup(node
);
536 CLK_OF_DECLARE(divider_clk
, "ti,divider-clock", of_ti_divider_clk_setup
);
538 static void __init
of_ti_composite_divider_clk_setup(struct device_node
*node
)
540 struct clk_omap_divider
*div
;
543 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
547 if (ti_clk_divider_populate(node
, div
, &tmp
))
550 if (!ti_clk_add_component(node
, &div
->hw
, CLK_COMPONENT_TYPE_DIVIDER
))
557 CLK_OF_DECLARE(ti_composite_divider_clk
, "ti,composite-divider-clock",
558 of_ti_composite_divider_clk_setup
);