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/cpufreq.h>
29 #include <linux/clk.h>
30 #include <linux/sh_clk.h>
32 static LIST_HEAD(clock_list
);
33 static DEFINE_SPINLOCK(clock_lock
);
34 static DEFINE_MUTEX(clock_list_sem
);
36 /* clock disable operations are not passed on to hardware during boot */
37 static int allow_disable
;
39 void clk_rate_table_build(struct clk
*clk
,
40 struct cpufreq_frequency_table
*freq_table
,
42 struct clk_div_mult_table
*src_table
,
43 unsigned long *bitmap
)
45 unsigned long mult
, div
;
49 clk
->nr_freqs
= nr_freqs
;
51 for (i
= 0; i
< nr_freqs
; i
++) {
55 if (src_table
->divisors
&& i
< src_table
->nr_divisors
)
56 div
= src_table
->divisors
[i
];
58 if (src_table
->multipliers
&& i
< src_table
->nr_multipliers
)
59 mult
= src_table
->multipliers
[i
];
61 if (!div
|| !mult
|| (bitmap
&& !test_bit(i
, bitmap
)))
62 freq
= CPUFREQ_ENTRY_INVALID
;
64 freq
= clk
->parent
->rate
* mult
/ div
;
66 freq_table
[i
].driver_data
= i
;
67 freq_table
[i
].frequency
= freq
;
70 /* Termination entry */
71 freq_table
[i
].driver_data
= i
;
72 freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
75 struct clk_rate_round_data
;
77 struct clk_rate_round_data
{
79 unsigned int min
, max
;
80 long (*func
)(unsigned int, struct clk_rate_round_data
*);
84 #define for_each_frequency(pos, r, freq) \
85 for (pos = r->min, freq = r->func(pos, r); \
86 pos <= r->max; pos++, freq = r->func(pos, r)) \
87 if (unlikely(freq == 0)) \
91 static long clk_rate_round_helper(struct clk_rate_round_data
*rounder
)
93 unsigned long rate_error
, rate_error_prev
= ~0UL;
94 unsigned long highest
, lowest
, freq
;
95 long rate_best_fit
= -ENOENT
;
101 for_each_frequency(i
, rounder
, freq
) {
107 rate_error
= abs(freq
- rounder
->rate
);
108 if (rate_error
< rate_error_prev
) {
109 rate_best_fit
= freq
;
110 rate_error_prev
= rate_error
;
117 if (rounder
->rate
>= highest
)
118 rate_best_fit
= highest
;
119 if (rounder
->rate
<= lowest
)
120 rate_best_fit
= lowest
;
122 return rate_best_fit
;
125 static long clk_rate_table_iter(unsigned int pos
,
126 struct clk_rate_round_data
*rounder
)
128 struct cpufreq_frequency_table
*freq_table
= rounder
->arg
;
129 unsigned long freq
= freq_table
[pos
].frequency
;
131 if (freq
== CPUFREQ_ENTRY_INVALID
)
137 long clk_rate_table_round(struct clk
*clk
,
138 struct cpufreq_frequency_table
*freq_table
,
141 struct clk_rate_round_data table_round
= {
143 .max
= clk
->nr_freqs
- 1,
144 .func
= clk_rate_table_iter
,
149 if (clk
->nr_freqs
< 1)
152 return clk_rate_round_helper(&table_round
);
155 static long clk_rate_div_range_iter(unsigned int pos
,
156 struct clk_rate_round_data
*rounder
)
158 return clk_get_rate(rounder
->arg
) / pos
;
161 long clk_rate_div_range_round(struct clk
*clk
, unsigned int div_min
,
162 unsigned int div_max
, unsigned long rate
)
164 struct clk_rate_round_data div_range_round
= {
167 .func
= clk_rate_div_range_iter
,
168 .arg
= clk_get_parent(clk
),
172 return clk_rate_round_helper(&div_range_round
);
175 static long clk_rate_mult_range_iter(unsigned int pos
,
176 struct clk_rate_round_data
*rounder
)
178 return clk_get_rate(rounder
->arg
) * pos
;
181 long clk_rate_mult_range_round(struct clk
*clk
, unsigned int mult_min
,
182 unsigned int mult_max
, unsigned long rate
)
184 struct clk_rate_round_data mult_range_round
= {
187 .func
= clk_rate_mult_range_iter
,
188 .arg
= clk_get_parent(clk
),
192 return clk_rate_round_helper(&mult_range_round
);
195 int clk_rate_table_find(struct clk
*clk
,
196 struct cpufreq_frequency_table
*freq_table
,
199 struct cpufreq_frequency_table
*pos
;
201 cpufreq_for_each_valid_entry(pos
, freq_table
)
202 if (pos
->frequency
== rate
)
203 return pos
- freq_table
;
208 /* Used for clocks that always have same value as the parent clock */
209 unsigned long followparent_recalc(struct clk
*clk
)
211 return clk
->parent
? clk
->parent
->rate
: 0;
214 int clk_reparent(struct clk
*child
, struct clk
*parent
)
216 list_del_init(&child
->sibling
);
218 list_add(&child
->sibling
, &parent
->children
);
219 child
->parent
= parent
;
224 /* Propagate rate to children */
225 void propagate_rate(struct clk
*tclk
)
229 list_for_each_entry(clkp
, &tclk
->children
, sibling
) {
230 if (clkp
->ops
&& clkp
->ops
->recalc
)
231 clkp
->rate
= clkp
->ops
->recalc(clkp
);
233 propagate_rate(clkp
);
237 static void __clk_disable(struct clk
*clk
)
239 if (WARN(!clk
->usecount
, "Trying to disable clock %p with 0 usecount\n",
243 if (!(--clk
->usecount
)) {
244 if (likely(allow_disable
&& clk
->ops
&& clk
->ops
->disable
))
245 clk
->ops
->disable(clk
);
246 if (likely(clk
->parent
))
247 __clk_disable(clk
->parent
);
251 void clk_disable(struct clk
*clk
)
258 spin_lock_irqsave(&clock_lock
, flags
);
260 spin_unlock_irqrestore(&clock_lock
, flags
);
262 EXPORT_SYMBOL_GPL(clk_disable
);
264 static int __clk_enable(struct clk
*clk
)
268 if (clk
->usecount
++ == 0) {
270 ret
= __clk_enable(clk
->parent
);
275 if (clk
->ops
&& clk
->ops
->enable
) {
276 ret
= clk
->ops
->enable(clk
);
279 __clk_disable(clk
->parent
);
291 int clk_enable(struct clk
*clk
)
299 spin_lock_irqsave(&clock_lock
, flags
);
300 ret
= __clk_enable(clk
);
301 spin_unlock_irqrestore(&clock_lock
, flags
);
305 EXPORT_SYMBOL_GPL(clk_enable
);
307 static LIST_HEAD(root_clks
);
310 * recalculate_root_clocks - recalculate and propagate all root clocks
312 * Recalculates all root clocks (clocks with no parent), which if the
313 * clock's .recalc is set correctly, should also propagate their rates.
316 void recalculate_root_clocks(void)
320 list_for_each_entry(clkp
, &root_clks
, sibling
) {
321 if (clkp
->ops
&& clkp
->ops
->recalc
)
322 clkp
->rate
= clkp
->ops
->recalc(clkp
);
323 propagate_rate(clkp
);
327 static struct clk_mapping dummy_mapping
;
329 static struct clk
*lookup_root_clock(struct clk
*clk
)
337 static int clk_establish_mapping(struct clk
*clk
)
339 struct clk_mapping
*mapping
= clk
->mapping
;
342 * Propagate mappings.
348 * dummy mapping for root clocks with no specified ranges
351 clk
->mapping
= &dummy_mapping
;
356 * If we're on a child clock and it provides no mapping of its
357 * own, inherit the mapping from its root clock.
359 clkp
= lookup_root_clock(clk
);
360 mapping
= clkp
->mapping
;
365 * Establish initial mapping.
367 if (!mapping
->base
&& mapping
->phys
) {
368 kref_init(&mapping
->ref
);
370 mapping
->base
= ioremap_nocache(mapping
->phys
, mapping
->len
);
371 if (unlikely(!mapping
->base
))
373 } else if (mapping
->base
) {
375 * Bump the refcount for an existing mapping
377 kref_get(&mapping
->ref
);
380 clk
->mapping
= mapping
;
382 clk
->mapped_reg
= clk
->mapping
->base
;
383 clk
->mapped_reg
+= (phys_addr_t
)clk
->enable_reg
- clk
->mapping
->phys
;
387 static void clk_destroy_mapping(struct kref
*kref
)
389 struct clk_mapping
*mapping
;
391 mapping
= container_of(kref
, struct clk_mapping
, ref
);
393 iounmap(mapping
->base
);
396 static void clk_teardown_mapping(struct clk
*clk
)
398 struct clk_mapping
*mapping
= clk
->mapping
;
401 if (mapping
== &dummy_mapping
)
404 kref_put(&mapping
->ref
, clk_destroy_mapping
);
407 clk
->mapped_reg
= NULL
;
410 int clk_register(struct clk
*clk
)
414 if (IS_ERR_OR_NULL(clk
))
418 * trap out already registered clocks
420 if (clk
->node
.next
|| clk
->node
.prev
)
423 mutex_lock(&clock_list_sem
);
425 INIT_LIST_HEAD(&clk
->children
);
428 ret
= clk_establish_mapping(clk
);
433 list_add(&clk
->sibling
, &clk
->parent
->children
);
435 list_add(&clk
->sibling
, &root_clks
);
437 list_add(&clk
->node
, &clock_list
);
439 #ifdef CONFIG_SH_CLK_CPG_LEGACY
440 if (clk
->ops
&& clk
->ops
->init
)
445 mutex_unlock(&clock_list_sem
);
449 EXPORT_SYMBOL_GPL(clk_register
);
451 void clk_unregister(struct clk
*clk
)
453 mutex_lock(&clock_list_sem
);
454 list_del(&clk
->sibling
);
455 list_del(&clk
->node
);
456 clk_teardown_mapping(clk
);
457 mutex_unlock(&clock_list_sem
);
459 EXPORT_SYMBOL_GPL(clk_unregister
);
461 void clk_enable_init_clocks(void)
465 list_for_each_entry(clkp
, &clock_list
, node
)
466 if (clkp
->flags
& CLK_ENABLE_ON_INIT
)
470 unsigned long clk_get_rate(struct clk
*clk
)
474 EXPORT_SYMBOL_GPL(clk_get_rate
);
476 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
478 int ret
= -EOPNOTSUPP
;
481 spin_lock_irqsave(&clock_lock
, flags
);
483 if (likely(clk
->ops
&& clk
->ops
->set_rate
)) {
484 ret
= clk
->ops
->set_rate(clk
, rate
);
492 if (clk
->ops
&& clk
->ops
->recalc
)
493 clk
->rate
= clk
->ops
->recalc(clk
);
498 spin_unlock_irqrestore(&clock_lock
, flags
);
502 EXPORT_SYMBOL_GPL(clk_set_rate
);
504 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
511 if (clk
->parent
== parent
)
514 spin_lock_irqsave(&clock_lock
, flags
);
515 if (clk
->usecount
== 0) {
516 if (clk
->ops
->set_parent
)
517 ret
= clk
->ops
->set_parent(clk
, parent
);
519 ret
= clk_reparent(clk
, parent
);
522 if (clk
->ops
->recalc
)
523 clk
->rate
= clk
->ops
->recalc(clk
);
524 pr_debug("set parent of %p to %p (new rate %ld)\n",
525 clk
, clk
->parent
, clk
->rate
);
530 spin_unlock_irqrestore(&clock_lock
, flags
);
534 EXPORT_SYMBOL_GPL(clk_set_parent
);
536 struct clk
*clk_get_parent(struct clk
*clk
)
540 EXPORT_SYMBOL_GPL(clk_get_parent
);
542 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
544 if (likely(clk
->ops
&& clk
->ops
->round_rate
)) {
545 unsigned long flags
, rounded
;
547 spin_lock_irqsave(&clock_lock
, flags
);
548 rounded
= clk
->ops
->round_rate(clk
, rate
);
549 spin_unlock_irqrestore(&clock_lock
, flags
);
554 return clk_get_rate(clk
);
556 EXPORT_SYMBOL_GPL(clk_round_rate
);
558 long clk_round_parent(struct clk
*clk
, unsigned long target
,
559 unsigned long *best_freq
, unsigned long *parent_freq
,
560 unsigned int div_min
, unsigned int div_max
)
562 struct cpufreq_frequency_table
*freq
, *best
= NULL
;
563 unsigned long error
= ULONG_MAX
, freq_high
, freq_low
, div
;
564 struct clk
*parent
= clk_get_parent(clk
);
568 *best_freq
= clk_round_rate(clk
, target
);
569 return abs(target
- *best_freq
);
572 cpufreq_for_each_valid_entry(freq
, parent
->freq_table
) {
573 if (unlikely(freq
->frequency
/ target
<= div_min
- 1)) {
574 unsigned long freq_max
;
576 freq_max
= (freq
->frequency
+ div_min
/ 2) / div_min
;
577 if (error
> target
- freq_max
) {
578 error
= target
- freq_max
;
581 *best_freq
= freq_max
;
584 pr_debug("too low freq %u, error %lu\n", freq
->frequency
,
593 if (unlikely(freq
->frequency
/ target
>= div_max
)) {
594 unsigned long freq_min
;
596 freq_min
= (freq
->frequency
+ div_max
/ 2) / div_max
;
597 if (error
> freq_min
- target
) {
598 error
= freq_min
- target
;
601 *best_freq
= freq_min
;
604 pr_debug("too high freq %u, error %lu\n", freq
->frequency
,
613 div
= freq
->frequency
/ target
;
614 freq_high
= freq
->frequency
/ div
;
615 freq_low
= freq
->frequency
/ (div
+ 1);
617 if (freq_high
- target
< error
) {
618 error
= freq_high
- target
;
621 *best_freq
= freq_high
;
624 if (target
- freq_low
< error
) {
625 error
= target
- freq_low
;
628 *best_freq
= freq_low
;
631 pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
632 freq
->frequency
, div
, freq_high
, div
+ 1, freq_low
,
633 *best_freq
, best
->frequency
);
640 *parent_freq
= best
->frequency
;
644 EXPORT_SYMBOL_GPL(clk_round_parent
);
647 static void clks_core_resume(void)
651 list_for_each_entry(clkp
, &clock_list
, node
) {
652 if (likely(clkp
->usecount
&& clkp
->ops
)) {
653 unsigned long rate
= clkp
->rate
;
655 if (likely(clkp
->ops
->set_parent
))
656 clkp
->ops
->set_parent(clkp
,
658 if (likely(clkp
->ops
->set_rate
))
659 clkp
->ops
->set_rate(clkp
, rate
);
660 else if (likely(clkp
->ops
->recalc
))
661 clkp
->rate
= clkp
->ops
->recalc(clkp
);
666 static struct syscore_ops clks_syscore_ops
= {
667 .resume
= clks_core_resume
,
670 static int __init
clk_syscore_init(void)
672 register_syscore_ops(&clks_syscore_ops
);
676 subsys_initcall(clk_syscore_init
);
679 static int __init
clk_late_init(void)
684 /* disable all clocks with zero use count */
685 mutex_lock(&clock_list_sem
);
686 spin_lock_irqsave(&clock_lock
, flags
);
688 list_for_each_entry(clk
, &clock_list
, node
)
689 if (!clk
->usecount
&& clk
->ops
&& clk
->ops
->disable
)
690 clk
->ops
->disable(clk
);
692 /* from now on allow clock disable operations */
695 spin_unlock_irqrestore(&clock_lock
, flags
);
696 mutex_unlock(&clock_list_sem
);
699 late_initcall(clk_late_init
);