2 * SuperH clock framework
4 * Copyright (C) 2005 - 2010 Paul Mundt
6 * This clock framework is derived from the OMAP version by:
8 * Copyright (C) 2004 - 2008 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
11 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
17 #define pr_fmt(fmt) "clock: " fmt
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/list.h>
24 #include <linux/syscore_ops.h>
25 #include <linux/seq_file.h>
26 #include <linux/err.h>
28 #include <linux/debugfs.h>
29 #include <linux/cpufreq.h>
30 #include <linux/clk.h>
31 #include <linux/sh_clk.h>
33 static LIST_HEAD(clock_list
);
34 static DEFINE_SPINLOCK(clock_lock
);
35 static DEFINE_MUTEX(clock_list_sem
);
37 /* clock disable operations are not passed on to hardware during boot */
38 static int allow_disable
;
40 void clk_rate_table_build(struct clk
*clk
,
41 struct cpufreq_frequency_table
*freq_table
,
43 struct clk_div_mult_table
*src_table
,
44 unsigned long *bitmap
)
46 unsigned long mult
, div
;
50 clk
->nr_freqs
= nr_freqs
;
52 for (i
= 0; i
< nr_freqs
; i
++) {
56 if (src_table
->divisors
&& i
< src_table
->nr_divisors
)
57 div
= src_table
->divisors
[i
];
59 if (src_table
->multipliers
&& i
< src_table
->nr_multipliers
)
60 mult
= src_table
->multipliers
[i
];
62 if (!div
|| !mult
|| (bitmap
&& !test_bit(i
, bitmap
)))
63 freq
= CPUFREQ_ENTRY_INVALID
;
65 freq
= clk
->parent
->rate
* mult
/ div
;
67 freq_table
[i
].index
= i
;
68 freq_table
[i
].frequency
= freq
;
71 /* Termination entry */
72 freq_table
[i
].index
= i
;
73 freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
76 struct clk_rate_round_data
;
78 struct clk_rate_round_data
{
80 unsigned int min
, max
;
81 long (*func
)(unsigned int, struct clk_rate_round_data
*);
85 #define for_each_frequency(pos, r, freq) \
86 for (pos = r->min, freq = r->func(pos, r); \
87 pos <= r->max; pos++, freq = r->func(pos, r)) \
88 if (unlikely(freq == 0)) \
92 static long clk_rate_round_helper(struct clk_rate_round_data
*rounder
)
94 unsigned long rate_error
, rate_error_prev
= ~0UL;
95 unsigned long highest
, lowest
, freq
;
96 long rate_best_fit
= -ENOENT
;
102 for_each_frequency(i
, rounder
, freq
) {
108 rate_error
= abs(freq
- rounder
->rate
);
109 if (rate_error
< rate_error_prev
) {
110 rate_best_fit
= freq
;
111 rate_error_prev
= rate_error
;
118 if (rounder
->rate
>= highest
)
119 rate_best_fit
= highest
;
120 if (rounder
->rate
<= lowest
)
121 rate_best_fit
= lowest
;
123 return rate_best_fit
;
126 static long clk_rate_table_iter(unsigned int pos
,
127 struct clk_rate_round_data
*rounder
)
129 struct cpufreq_frequency_table
*freq_table
= rounder
->arg
;
130 unsigned long freq
= freq_table
[pos
].frequency
;
132 if (freq
== CPUFREQ_ENTRY_INVALID
)
138 long clk_rate_table_round(struct clk
*clk
,
139 struct cpufreq_frequency_table
*freq_table
,
142 struct clk_rate_round_data table_round
= {
144 .max
= clk
->nr_freqs
- 1,
145 .func
= clk_rate_table_iter
,
150 if (clk
->nr_freqs
< 1)
153 return clk_rate_round_helper(&table_round
);
156 static long clk_rate_div_range_iter(unsigned int pos
,
157 struct clk_rate_round_data
*rounder
)
159 return clk_get_rate(rounder
->arg
) / pos
;
162 long clk_rate_div_range_round(struct clk
*clk
, unsigned int div_min
,
163 unsigned int div_max
, unsigned long rate
)
165 struct clk_rate_round_data div_range_round
= {
168 .func
= clk_rate_div_range_iter
,
169 .arg
= clk_get_parent(clk
),
173 return clk_rate_round_helper(&div_range_round
);
176 int clk_rate_table_find(struct clk
*clk
,
177 struct cpufreq_frequency_table
*freq_table
,
182 for (i
= 0; freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
183 unsigned long freq
= freq_table
[i
].frequency
;
185 if (freq
== CPUFREQ_ENTRY_INVALID
)
195 /* Used for clocks that always have same value as the parent clock */
196 unsigned long followparent_recalc(struct clk
*clk
)
198 return clk
->parent
? clk
->parent
->rate
: 0;
201 int clk_reparent(struct clk
*child
, struct clk
*parent
)
203 list_del_init(&child
->sibling
);
205 list_add(&child
->sibling
, &parent
->children
);
206 child
->parent
= parent
;
208 /* now do the debugfs renaming to reattach the child
209 to the proper parent */
214 /* Propagate rate to children */
215 void propagate_rate(struct clk
*tclk
)
219 list_for_each_entry(clkp
, &tclk
->children
, sibling
) {
220 if (clkp
->ops
&& clkp
->ops
->recalc
)
221 clkp
->rate
= clkp
->ops
->recalc(clkp
);
223 propagate_rate(clkp
);
227 static void __clk_disable(struct clk
*clk
)
229 if (WARN(!clk
->usecount
, "Trying to disable clock %p with 0 usecount\n",
233 if (!(--clk
->usecount
)) {
234 if (likely(allow_disable
&& clk
->ops
&& clk
->ops
->disable
))
235 clk
->ops
->disable(clk
);
236 if (likely(clk
->parent
))
237 __clk_disable(clk
->parent
);
241 void clk_disable(struct clk
*clk
)
248 spin_lock_irqsave(&clock_lock
, flags
);
250 spin_unlock_irqrestore(&clock_lock
, flags
);
252 EXPORT_SYMBOL_GPL(clk_disable
);
254 static int __clk_enable(struct clk
*clk
)
258 if (clk
->usecount
++ == 0) {
260 ret
= __clk_enable(clk
->parent
);
265 if (clk
->ops
&& clk
->ops
->enable
) {
266 ret
= clk
->ops
->enable(clk
);
269 __clk_disable(clk
->parent
);
281 int clk_enable(struct clk
*clk
)
289 spin_lock_irqsave(&clock_lock
, flags
);
290 ret
= __clk_enable(clk
);
291 spin_unlock_irqrestore(&clock_lock
, flags
);
295 EXPORT_SYMBOL_GPL(clk_enable
);
297 static LIST_HEAD(root_clks
);
300 * recalculate_root_clocks - recalculate and propagate all root clocks
302 * Recalculates all root clocks (clocks with no parent), which if the
303 * clock's .recalc is set correctly, should also propagate their rates.
306 void recalculate_root_clocks(void)
310 list_for_each_entry(clkp
, &root_clks
, sibling
) {
311 if (clkp
->ops
&& clkp
->ops
->recalc
)
312 clkp
->rate
= clkp
->ops
->recalc(clkp
);
313 propagate_rate(clkp
);
317 static struct clk_mapping dummy_mapping
;
319 static struct clk
*lookup_root_clock(struct clk
*clk
)
327 static int clk_establish_mapping(struct clk
*clk
)
329 struct clk_mapping
*mapping
= clk
->mapping
;
332 * Propagate mappings.
338 * dummy mapping for root clocks with no specified ranges
341 clk
->mapping
= &dummy_mapping
;
346 * If we're on a child clock and it provides no mapping of its
347 * own, inherit the mapping from its root clock.
349 clkp
= lookup_root_clock(clk
);
350 mapping
= clkp
->mapping
;
355 * Establish initial mapping.
357 if (!mapping
->base
&& mapping
->phys
) {
358 kref_init(&mapping
->ref
);
360 mapping
->base
= ioremap_nocache(mapping
->phys
, mapping
->len
);
361 if (unlikely(!mapping
->base
))
363 } else if (mapping
->base
) {
365 * Bump the refcount for an existing mapping
367 kref_get(&mapping
->ref
);
370 clk
->mapping
= mapping
;
374 static void clk_destroy_mapping(struct kref
*kref
)
376 struct clk_mapping
*mapping
;
378 mapping
= container_of(kref
, struct clk_mapping
, ref
);
380 iounmap(mapping
->base
);
383 static void clk_teardown_mapping(struct clk
*clk
)
385 struct clk_mapping
*mapping
= clk
->mapping
;
388 if (mapping
== &dummy_mapping
)
391 kref_put(&mapping
->ref
, clk_destroy_mapping
);
395 int clk_register(struct clk
*clk
)
399 if (IS_ERR_OR_NULL(clk
))
403 * trap out already registered clocks
405 if (clk
->node
.next
|| clk
->node
.prev
)
408 mutex_lock(&clock_list_sem
);
410 INIT_LIST_HEAD(&clk
->children
);
413 ret
= clk_establish_mapping(clk
);
418 list_add(&clk
->sibling
, &clk
->parent
->children
);
420 list_add(&clk
->sibling
, &root_clks
);
422 list_add(&clk
->node
, &clock_list
);
424 #ifdef CONFIG_SH_CLK_CPG_LEGACY
425 if (clk
->ops
&& clk
->ops
->init
)
430 mutex_unlock(&clock_list_sem
);
434 EXPORT_SYMBOL_GPL(clk_register
);
436 void clk_unregister(struct clk
*clk
)
438 mutex_lock(&clock_list_sem
);
439 list_del(&clk
->sibling
);
440 list_del(&clk
->node
);
441 clk_teardown_mapping(clk
);
442 mutex_unlock(&clock_list_sem
);
444 EXPORT_SYMBOL_GPL(clk_unregister
);
446 void clk_enable_init_clocks(void)
450 list_for_each_entry(clkp
, &clock_list
, node
)
451 if (clkp
->flags
& CLK_ENABLE_ON_INIT
)
455 unsigned long clk_get_rate(struct clk
*clk
)
459 EXPORT_SYMBOL_GPL(clk_get_rate
);
461 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
463 int ret
= -EOPNOTSUPP
;
466 spin_lock_irqsave(&clock_lock
, flags
);
468 if (likely(clk
->ops
&& clk
->ops
->set_rate
)) {
469 ret
= clk
->ops
->set_rate(clk
, rate
);
477 if (clk
->ops
&& clk
->ops
->recalc
)
478 clk
->rate
= clk
->ops
->recalc(clk
);
483 spin_unlock_irqrestore(&clock_lock
, flags
);
487 EXPORT_SYMBOL_GPL(clk_set_rate
);
489 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
496 if (clk
->parent
== parent
)
499 spin_lock_irqsave(&clock_lock
, flags
);
500 if (clk
->usecount
== 0) {
501 if (clk
->ops
->set_parent
)
502 ret
= clk
->ops
->set_parent(clk
, parent
);
504 ret
= clk_reparent(clk
, parent
);
507 if (clk
->ops
->recalc
)
508 clk
->rate
= clk
->ops
->recalc(clk
);
509 pr_debug("set parent of %p to %p (new rate %ld)\n",
510 clk
, clk
->parent
, clk
->rate
);
515 spin_unlock_irqrestore(&clock_lock
, flags
);
519 EXPORT_SYMBOL_GPL(clk_set_parent
);
521 struct clk
*clk_get_parent(struct clk
*clk
)
525 EXPORT_SYMBOL_GPL(clk_get_parent
);
527 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
529 if (likely(clk
->ops
&& clk
->ops
->round_rate
)) {
530 unsigned long flags
, rounded
;
532 spin_lock_irqsave(&clock_lock
, flags
);
533 rounded
= clk
->ops
->round_rate(clk
, rate
);
534 spin_unlock_irqrestore(&clock_lock
, flags
);
539 return clk_get_rate(clk
);
541 EXPORT_SYMBOL_GPL(clk_round_rate
);
543 long clk_round_parent(struct clk
*clk
, unsigned long target
,
544 unsigned long *best_freq
, unsigned long *parent_freq
,
545 unsigned int div_min
, unsigned int div_max
)
547 struct cpufreq_frequency_table
*freq
, *best
= NULL
;
548 unsigned long error
= ULONG_MAX
, freq_high
, freq_low
, div
;
549 struct clk
*parent
= clk_get_parent(clk
);
553 *best_freq
= clk_round_rate(clk
, target
);
554 return abs(target
- *best_freq
);
557 for (freq
= parent
->freq_table
; freq
->frequency
!= CPUFREQ_TABLE_END
;
559 if (freq
->frequency
== CPUFREQ_ENTRY_INVALID
)
562 if (unlikely(freq
->frequency
/ target
<= div_min
- 1)) {
563 unsigned long freq_max
;
565 freq_max
= (freq
->frequency
+ div_min
/ 2) / div_min
;
566 if (error
> target
- freq_max
) {
567 error
= target
- freq_max
;
570 *best_freq
= freq_max
;
573 pr_debug("too low freq %u, error %lu\n", freq
->frequency
,
582 if (unlikely(freq
->frequency
/ target
>= div_max
)) {
583 unsigned long freq_min
;
585 freq_min
= (freq
->frequency
+ div_max
/ 2) / div_max
;
586 if (error
> freq_min
- target
) {
587 error
= freq_min
- target
;
590 *best_freq
= freq_min
;
593 pr_debug("too high freq %u, error %lu\n", freq
->frequency
,
602 div
= freq
->frequency
/ target
;
603 freq_high
= freq
->frequency
/ div
;
604 freq_low
= freq
->frequency
/ (div
+ 1);
606 if (freq_high
- target
< error
) {
607 error
= freq_high
- target
;
610 *best_freq
= freq_high
;
613 if (target
- freq_low
< error
) {
614 error
= target
- freq_low
;
617 *best_freq
= freq_low
;
620 pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
621 freq
->frequency
, div
, freq_high
, div
+ 1, freq_low
,
622 *best_freq
, best
->frequency
);
629 *parent_freq
= best
->frequency
;
633 EXPORT_SYMBOL_GPL(clk_round_parent
);
636 static void clks_core_resume(void)
640 list_for_each_entry(clkp
, &clock_list
, node
) {
641 if (likely(clkp
->usecount
&& clkp
->ops
)) {
642 unsigned long rate
= clkp
->rate
;
644 if (likely(clkp
->ops
->set_parent
))
645 clkp
->ops
->set_parent(clkp
,
647 if (likely(clkp
->ops
->set_rate
))
648 clkp
->ops
->set_rate(clkp
, rate
);
649 else if (likely(clkp
->ops
->recalc
))
650 clkp
->rate
= clkp
->ops
->recalc(clkp
);
655 static struct syscore_ops clks_syscore_ops
= {
656 .resume
= clks_core_resume
,
659 static int __init
clk_syscore_init(void)
661 register_syscore_ops(&clks_syscore_ops
);
665 subsys_initcall(clk_syscore_init
);
669 * debugfs support to trace clock tree hierarchy and attributes
671 static struct dentry
*clk_debugfs_root
;
673 static int clk_debugfs_register_one(struct clk
*c
)
677 struct clk
*pa
= c
->parent
;
681 p
+= sprintf(p
, "%p", c
);
682 d
= debugfs_create_dir(s
, pa
? pa
->dentry
: clk_debugfs_root
);
687 d
= debugfs_create_u8("usecount", S_IRUGO
, c
->dentry
, (u8
*)&c
->usecount
);
692 d
= debugfs_create_u32("rate", S_IRUGO
, c
->dentry
, (u32
*)&c
->rate
);
697 d
= debugfs_create_x32("flags", S_IRUGO
, c
->dentry
, (u32
*)&c
->flags
);
705 debugfs_remove_recursive(c
->dentry
);
709 static int clk_debugfs_register(struct clk
*c
)
712 struct clk
*pa
= c
->parent
;
714 if (pa
&& !pa
->dentry
) {
715 err
= clk_debugfs_register(pa
);
721 err
= clk_debugfs_register_one(c
);
728 static int __init
clk_debugfs_init(void)
734 d
= debugfs_create_dir("clock", NULL
);
737 clk_debugfs_root
= d
;
739 list_for_each_entry(c
, &clock_list
, node
) {
740 err
= clk_debugfs_register(c
);
746 debugfs_remove_recursive(clk_debugfs_root
);
749 late_initcall(clk_debugfs_init
);
751 static int __init
clk_late_init(void)
756 /* disable all clocks with zero use count */
757 mutex_lock(&clock_list_sem
);
758 spin_lock_irqsave(&clock_lock
, flags
);
760 list_for_each_entry(clk
, &clock_list
, node
)
761 if (!clk
->usecount
&& clk
->ops
&& clk
->ops
->disable
)
762 clk
->ops
->disable(clk
);
764 /* from now on allow clock disable operations */
767 spin_unlock_irqrestore(&clock_lock
, flags
);
768 mutex_unlock(&clock_list_sem
);
771 late_initcall(clk_late_init
);