4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * Tero Kristo <t-kristo@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk-provider.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
22 #include <linux/of_address.h>
23 #include <linux/clk/ti.h>
26 #define pr_fmt(fmt) "%s: " fmt, __func__
28 #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
30 #define div_mask(d) ((1 << ((d)->width)) - 1)
32 static unsigned int _get_table_maxdiv(const struct clk_div_table
*table
)
34 unsigned int maxdiv
= 0;
35 const struct clk_div_table
*clkt
;
37 for (clkt
= table
; clkt
->div
; clkt
++)
38 if (clkt
->div
> maxdiv
)
43 static unsigned int _get_maxdiv(struct clk_divider
*divider
)
45 if (divider
->flags
& CLK_DIVIDER_ONE_BASED
)
46 return div_mask(divider
);
47 if (divider
->flags
& CLK_DIVIDER_POWER_OF_TWO
)
48 return 1 << div_mask(divider
);
50 return _get_table_maxdiv(divider
->table
);
51 return div_mask(divider
) + 1;
54 static unsigned int _get_table_div(const struct clk_div_table
*table
,
57 const struct clk_div_table
*clkt
;
59 for (clkt
= table
; clkt
->div
; clkt
++)
65 static unsigned int _get_div(struct clk_divider
*divider
, unsigned int val
)
67 if (divider
->flags
& CLK_DIVIDER_ONE_BASED
)
69 if (divider
->flags
& CLK_DIVIDER_POWER_OF_TWO
)
72 return _get_table_div(divider
->table
, val
);
76 static unsigned int _get_table_val(const struct clk_div_table
*table
,
79 const struct clk_div_table
*clkt
;
81 for (clkt
= table
; clkt
->div
; clkt
++)
87 static unsigned int _get_val(struct clk_divider
*divider
, u8 div
)
89 if (divider
->flags
& CLK_DIVIDER_ONE_BASED
)
91 if (divider
->flags
& CLK_DIVIDER_POWER_OF_TWO
)
94 return _get_table_val(divider
->table
, div
);
98 static unsigned long ti_clk_divider_recalc_rate(struct clk_hw
*hw
,
99 unsigned long parent_rate
)
101 struct clk_divider
*divider
= to_clk_divider(hw
);
102 unsigned int div
, val
;
104 val
= ti_clk_ll_ops
->clk_readl(divider
->reg
) >> divider
->shift
;
105 val
&= div_mask(divider
);
107 div
= _get_div(divider
, val
);
109 WARN(!(divider
->flags
& CLK_DIVIDER_ALLOW_ZERO
),
110 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
111 __clk_get_name(hw
->clk
));
115 return parent_rate
/ div
;
119 * The reverse of DIV_ROUND_UP: The maximum number which
122 #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
124 static bool _is_valid_table_div(const struct clk_div_table
*table
,
127 const struct clk_div_table
*clkt
;
129 for (clkt
= table
; clkt
->div
; clkt
++)
130 if (clkt
->div
== div
)
135 static bool _is_valid_div(struct clk_divider
*divider
, unsigned int div
)
137 if (divider
->flags
& CLK_DIVIDER_POWER_OF_TWO
)
138 return is_power_of_2(div
);
140 return _is_valid_table_div(divider
->table
, div
);
144 static int ti_clk_divider_bestdiv(struct clk_hw
*hw
, unsigned long rate
,
145 unsigned long *best_parent_rate
)
147 struct clk_divider
*divider
= to_clk_divider(hw
);
149 unsigned long parent_rate
, best
= 0, now
, maxdiv
;
150 unsigned long parent_rate_saved
= *best_parent_rate
;
155 maxdiv
= _get_maxdiv(divider
);
157 if (!(__clk_get_flags(hw
->clk
) & CLK_SET_RATE_PARENT
)) {
158 parent_rate
= *best_parent_rate
;
159 bestdiv
= DIV_ROUND_UP(parent_rate
, rate
);
160 bestdiv
= bestdiv
== 0 ? 1 : bestdiv
;
161 bestdiv
= bestdiv
> maxdiv
? maxdiv
: bestdiv
;
166 * The maximum divider we can use without overflowing
167 * unsigned long in rate * i below
169 maxdiv
= min(ULONG_MAX
/ rate
, maxdiv
);
171 for (i
= 1; i
<= maxdiv
; i
++) {
172 if (!_is_valid_div(divider
, i
))
174 if (rate
* i
== parent_rate_saved
) {
176 * It's the most ideal case if the requested rate can be
177 * divided from parent clock without needing to change
178 * parent rate, so return the divider immediately.
180 *best_parent_rate
= parent_rate_saved
;
183 parent_rate
= __clk_round_rate(__clk_get_parent(hw
->clk
),
184 MULT_ROUND_UP(rate
, i
));
185 now
= parent_rate
/ i
;
186 if (now
<= rate
&& now
> best
) {
189 *best_parent_rate
= parent_rate
;
194 bestdiv
= _get_maxdiv(divider
);
196 __clk_round_rate(__clk_get_parent(hw
->clk
), 1);
202 static long ti_clk_divider_round_rate(struct clk_hw
*hw
, unsigned long rate
,
203 unsigned long *prate
)
206 div
= ti_clk_divider_bestdiv(hw
, rate
, prate
);
211 static int ti_clk_divider_set_rate(struct clk_hw
*hw
, unsigned long rate
,
212 unsigned long parent_rate
)
214 struct clk_divider
*divider
= to_clk_divider(hw
);
215 unsigned int div
, value
;
216 unsigned long flags
= 0;
219 div
= parent_rate
/ rate
;
220 value
= _get_val(divider
, div
);
222 if (value
> div_mask(divider
))
223 value
= div_mask(divider
);
226 spin_lock_irqsave(divider
->lock
, flags
);
228 if (divider
->flags
& CLK_DIVIDER_HIWORD_MASK
) {
229 val
= div_mask(divider
) << (divider
->shift
+ 16);
231 val
= ti_clk_ll_ops
->clk_readl(divider
->reg
);
232 val
&= ~(div_mask(divider
) << divider
->shift
);
234 val
|= value
<< divider
->shift
;
235 ti_clk_ll_ops
->clk_writel(val
, divider
->reg
);
238 spin_unlock_irqrestore(divider
->lock
, flags
);
243 const struct clk_ops ti_clk_divider_ops
= {
244 .recalc_rate
= ti_clk_divider_recalc_rate
,
245 .round_rate
= ti_clk_divider_round_rate
,
246 .set_rate
= ti_clk_divider_set_rate
,
249 static struct clk
*_register_divider(struct device
*dev
, const char *name
,
250 const char *parent_name
,
251 unsigned long flags
, void __iomem
*reg
,
252 u8 shift
, u8 width
, u8 clk_divider_flags
,
253 const struct clk_div_table
*table
,
256 struct clk_divider
*div
;
258 struct clk_init_data init
;
260 if (clk_divider_flags
& CLK_DIVIDER_HIWORD_MASK
) {
261 if (width
+ shift
> 16) {
262 pr_warn("divider value exceeds LOWORD field\n");
263 return ERR_PTR(-EINVAL
);
267 /* allocate the divider */
268 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
270 pr_err("%s: could not allocate divider clk\n", __func__
);
271 return ERR_PTR(-ENOMEM
);
275 init
.ops
= &ti_clk_divider_ops
;
276 init
.flags
= flags
| CLK_IS_BASIC
;
277 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
278 init
.num_parents
= (parent_name
? 1 : 0);
280 /* struct clk_divider assignments */
284 div
->flags
= clk_divider_flags
;
286 div
->hw
.init
= &init
;
289 /* register the clock */
290 clk
= clk_register(dev
, &div
->hw
);
298 static struct clk_div_table
299 __init
*ti_clk_get_div_table(struct device_node
*node
)
301 struct clk_div_table
*table
;
302 const __be32
*divspec
;
308 divspec
= of_get_property(node
, "ti,dividers", &num_div
);
317 /* Determine required size for divider table */
318 for (i
= 0; i
< num_div
; i
++) {
319 of_property_read_u32_index(node
, "ti,dividers", i
, &val
);
325 pr_err("no valid dividers for %s table\n", node
->name
);
326 return ERR_PTR(-EINVAL
);
329 table
= kzalloc(sizeof(*table
) * (valid_div
+ 1), GFP_KERNEL
);
332 return ERR_PTR(-ENOMEM
);
336 for (i
= 0; i
< num_div
; i
++) {
337 of_property_read_u32_index(node
, "ti,dividers", i
, &val
);
339 table
[valid_div
].div
= val
;
340 table
[valid_div
].val
= i
;
348 static int _get_divider_width(struct device_node
*node
,
349 const struct clk_div_table
*table
,
358 /* Clk divider table not provided, determine min/max divs */
359 if (of_property_read_u32(node
, "ti,min-div", &min_div
))
362 if (of_property_read_u32(node
, "ti,max-div", &max_div
)) {
363 pr_err("no max-div for %s!\n", node
->name
);
367 /* Determine bit width for the field */
368 if (flags
& CLK_DIVIDER_ONE_BASED
)
373 while (div
< max_div
) {
374 if (flags
& CLK_DIVIDER_POWER_OF_TWO
)
383 while (table
[div
].div
) {
384 val
= table
[div
].val
;
392 static int __init
ti_clk_divider_populate(struct device_node
*node
,
393 void __iomem
**reg
, const struct clk_div_table
**table
,
394 u32
*flags
, u8
*div_flags
, u8
*width
, u8
*shift
)
398 *reg
= ti_clk_get_reg_addr(node
, 0);
402 if (!of_property_read_u32(node
, "ti,bit-shift", &val
))
410 if (of_property_read_bool(node
, "ti,index-starts-at-one"))
411 *div_flags
|= CLK_DIVIDER_ONE_BASED
;
413 if (of_property_read_bool(node
, "ti,index-power-of-two"))
414 *div_flags
|= CLK_DIVIDER_POWER_OF_TWO
;
416 if (of_property_read_bool(node
, "ti,set-rate-parent"))
417 *flags
|= CLK_SET_RATE_PARENT
;
419 *table
= ti_clk_get_div_table(node
);
422 return PTR_ERR(*table
);
424 *width
= _get_divider_width(node
, *table
, *div_flags
);
430 * of_ti_divider_clk_setup - Setup function for simple div rate clock
431 * @node: device node for this clock
433 * Sets up a basic divider clock.
435 static void __init
of_ti_divider_clk_setup(struct device_node
*node
)
438 const char *parent_name
;
440 u8 clk_divider_flags
= 0;
443 const struct clk_div_table
*table
= NULL
;
446 parent_name
= of_clk_get_parent_name(node
, 0);
448 if (ti_clk_divider_populate(node
, ®
, &table
, &flags
,
449 &clk_divider_flags
, &width
, &shift
))
452 clk
= _register_divider(NULL
, node
->name
, parent_name
, flags
, reg
,
453 shift
, width
, clk_divider_flags
, table
, NULL
);
456 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
457 of_ti_clk_autoidle_setup(node
);
464 CLK_OF_DECLARE(divider_clk
, "ti,divider-clock", of_ti_divider_clk_setup
);
466 static void __init
of_ti_composite_divider_clk_setup(struct device_node
*node
)
468 struct clk_divider
*div
;
471 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
475 if (ti_clk_divider_populate(node
, &div
->reg
, &div
->table
, &val
,
476 &div
->flags
, &div
->width
, &div
->shift
) < 0)
479 if (!ti_clk_add_component(node
, &div
->hw
, CLK_COMPONENT_TYPE_DIVIDER
))
486 CLK_OF_DECLARE(ti_composite_divider_clk
, "ti,composite-divider-clock",
487 of_ti_composite_divider_clk_setup
);