3 * Copyright (C) 2010 Google, Inc.
6 * Colin Cross <ccross@google.com>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/clk.h>
21 #include <linux/clkdev.h>
22 #include <linux/debugfs.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
39 * Each struct clk has a spinlock.
41 * To avoid AB-BA locking problems, locks must always be traversed from child
42 * clock to parent clock. For example, when enabling a clock, the clock's lock
43 * is taken, and then clk_enable is called on the parent, which take's the
44 * parent clock's lock. There is one exceptions to this ordering: When dumping
45 * the clock tree through debugfs. In this case, clk_lock_all is called,
46 * which attemps to iterate through the entire list of clocks and take every
47 * clock lock. If any call to spin_trylock fails, all locked clocks are
48 * unlocked, and the process is retried. When all the locks are held,
49 * the only clock operation that can be called is clk_get_rate_all_locked.
51 * Within a single clock, no clock operation can call another clock operation
52 * on itself, except for clk_get_rate_locked and clk_set_rate_locked. Any
53 * clock operation can call any other clock operation on any of it's possible
56 * An additional mutex, clock_list_lock, is used to protect the list of all
59 * The clock operations must lock internally to protect against
60 * read-modify-write on registers that are shared by multiple clocks
62 static DEFINE_MUTEX(clock_list_lock
);
63 static LIST_HEAD(clocks
);
65 struct clk
*tegra_get_clock_by_name(const char *name
)
68 struct clk
*ret
= NULL
;
69 mutex_lock(&clock_list_lock
);
70 list_for_each_entry(c
, &clocks
, node
) {
71 if (strcmp(c
->name
, name
) == 0) {
76 mutex_unlock(&clock_list_lock
);
80 /* Must be called with c->spinlock held */
81 static unsigned long clk_predict_rate_from_parent(struct clk
*c
, struct clk
*p
)
85 rate
= clk_get_rate(p
);
87 if (c
->mul
!= 0 && c
->div
!= 0) {
95 /* Must be called with c->spinlock held */
96 unsigned long clk_get_rate_locked(struct clk
*c
)
101 rate
= clk_predict_rate_from_parent(c
, c
->parent
);
108 unsigned long clk_get_rate(struct clk
*c
)
113 spin_lock_irqsave(&c
->spinlock
, flags
);
115 rate
= clk_get_rate_locked(c
);
117 spin_unlock_irqrestore(&c
->spinlock
, flags
);
121 EXPORT_SYMBOL(clk_get_rate
);
123 int clk_reparent(struct clk
*c
, struct clk
*parent
)
129 void clk_init(struct clk
*c
)
131 spin_lock_init(&c
->spinlock
);
133 if (c
->ops
&& c
->ops
->init
)
136 if (!c
->ops
|| !c
->ops
->enable
) {
140 c
->state
= c
->parent
->state
;
145 mutex_lock(&clock_list_lock
);
146 list_add(&c
->node
, &clocks
);
147 mutex_unlock(&clock_list_lock
);
150 int clk_enable(struct clk
*c
)
155 spin_lock_irqsave(&c
->spinlock
, flags
);
157 if (c
->refcnt
== 0) {
159 ret
= clk_enable(c
->parent
);
164 if (c
->ops
&& c
->ops
->enable
) {
165 ret
= c
->ops
->enable(c
);
168 clk_disable(c
->parent
);
177 spin_unlock_irqrestore(&c
->spinlock
, flags
);
180 EXPORT_SYMBOL(clk_enable
);
182 void clk_disable(struct clk
*c
)
186 spin_lock_irqsave(&c
->spinlock
, flags
);
188 if (c
->refcnt
== 0) {
189 WARN(1, "Attempting to disable clock %s with refcnt 0", c
->name
);
190 spin_unlock_irqrestore(&c
->spinlock
, flags
);
193 if (c
->refcnt
== 1) {
194 if (c
->ops
&& c
->ops
->disable
)
198 clk_disable(c
->parent
);
204 spin_unlock_irqrestore(&c
->spinlock
, flags
);
206 EXPORT_SYMBOL(clk_disable
);
208 int clk_set_parent(struct clk
*c
, struct clk
*parent
)
212 unsigned long new_rate
;
213 unsigned long old_rate
;
215 spin_lock_irqsave(&c
->spinlock
, flags
);
217 if (!c
->ops
|| !c
->ops
->set_parent
) {
222 new_rate
= clk_predict_rate_from_parent(c
, parent
);
223 old_rate
= clk_get_rate_locked(c
);
225 ret
= c
->ops
->set_parent(c
, parent
);
230 spin_unlock_irqrestore(&c
->spinlock
, flags
);
233 EXPORT_SYMBOL(clk_set_parent
);
235 struct clk
*clk_get_parent(struct clk
*c
)
239 EXPORT_SYMBOL(clk_get_parent
);
241 int clk_set_rate_locked(struct clk
*c
, unsigned long rate
)
243 if (!c
->ops
|| !c
->ops
->set_rate
)
246 if (rate
> c
->max_rate
)
249 return c
->ops
->set_rate(c
, rate
);
252 int clk_set_rate(struct clk
*c
, unsigned long rate
)
257 spin_lock_irqsave(&c
->spinlock
, flags
);
259 ret
= clk_set_rate_locked(c
, rate
);
261 spin_unlock_irqrestore(&c
->spinlock
, flags
);
265 EXPORT_SYMBOL(clk_set_rate
);
268 /* Must be called with clocks lock and all indvidual clock locks held */
269 unsigned long clk_get_rate_all_locked(struct clk
*c
)
278 if (c
->mul
!= 0 && c
->div
!= 0) {
292 long clk_round_rate(struct clk
*c
, unsigned long rate
)
297 spin_lock_irqsave(&c
->spinlock
, flags
);
299 if (!c
->ops
|| !c
->ops
->round_rate
) {
304 if (rate
> c
->max_rate
)
307 ret
= c
->ops
->round_rate(c
, rate
);
310 spin_unlock_irqrestore(&c
->spinlock
, flags
);
313 EXPORT_SYMBOL(clk_round_rate
);
315 static int tegra_clk_init_one_from_table(struct tegra_clk_init_table
*table
)
322 c
= tegra_get_clock_by_name(table
->name
);
325 pr_warning("Unable to initialize clock %s\n",
331 p
= tegra_get_clock_by_name(table
->parent
);
333 pr_warning("Unable to find parent %s of clock %s\n",
334 table
->parent
, table
->name
);
338 if (c
->parent
!= p
) {
339 ret
= clk_set_parent(c
, p
);
341 pr_warning("Unable to set parent %s of clock %s: %d\n",
342 table
->parent
, table
->name
, ret
);
348 if (table
->rate
&& table
->rate
!= clk_get_rate(c
)) {
349 ret
= clk_set_rate(c
, table
->rate
);
351 pr_warning("Unable to set clock %s to rate %lu: %d\n",
352 table
->name
, table
->rate
, ret
);
357 if (table
->enabled
) {
360 pr_warning("Unable to enable clock %s: %d\n",
369 void tegra_clk_init_from_table(struct tegra_clk_init_table
*table
)
371 for (; table
->name
; table
++)
372 tegra_clk_init_one_from_table(table
);
374 EXPORT_SYMBOL(tegra_clk_init_from_table
);
376 void tegra_periph_reset_deassert(struct clk
*c
)
378 tegra2_periph_reset_deassert(c
);
380 EXPORT_SYMBOL(tegra_periph_reset_deassert
);
382 void tegra_periph_reset_assert(struct clk
*c
)
384 tegra2_periph_reset_assert(c
);
386 EXPORT_SYMBOL(tegra_periph_reset_assert
);
388 void __init
tegra_init_clock(void)
390 tegra2_init_clocks();
393 #ifdef CONFIG_DEBUG_FS
395 static int __clk_lock_all_spinlocks(void)
399 list_for_each_entry(c
, &clocks
, node
)
400 if (!spin_trylock(&c
->spinlock
))
401 goto unlock_spinlocks
;
406 list_for_each_entry_continue_reverse(c
, &clocks
, node
)
407 spin_unlock(&c
->spinlock
);
412 static void __clk_unlock_all_spinlocks(void)
416 list_for_each_entry_reverse(c
, &clocks
, node
)
417 spin_unlock(&c
->spinlock
);
421 * This function retries until it can take all locks, and may take
422 * an arbitrarily long time to complete.
423 * Must be called with irqs enabled, returns with irqs disabled
424 * Must be called with clock_list_lock held
426 static void clk_lock_all(void)
432 ret
= __clk_lock_all_spinlocks();
434 goto failed_spinlocks
;
436 /* All locks taken successfully, return */
446 * Unlocks all clocks after a clk_lock_all
447 * Must be called with irqs disabled, returns with irqs enabled
448 * Must be called with clock_list_lock held
450 static void clk_unlock_all(void)
452 __clk_unlock_all_spinlocks();
457 static struct dentry
*clk_debugfs_root
;
460 static void clock_tree_show_one(struct seq_file
*s
, struct clk
*c
, int level
)
463 const char *state
= "uninit";
468 else if (c
->state
== OFF
)
471 if (c
->mul
!= 0 && c
->div
!= 0) {
472 if (c
->mul
> c
->div
) {
473 int mul
= c
->mul
/ c
->div
;
474 int mul2
= (c
->mul
* 10 / c
->div
) % 10;
475 int mul3
= (c
->mul
* 10) % c
->div
;
476 if (mul2
== 0 && mul3
== 0)
477 snprintf(div
, sizeof(div
), "x%d", mul
);
479 snprintf(div
, sizeof(div
), "x%d.%d", mul
, mul2
);
481 snprintf(div
, sizeof(div
), "x%d.%d..", mul
, mul2
);
483 snprintf(div
, sizeof(div
), "%d%s", c
->div
/ c
->mul
,
484 (c
->div
% c
->mul
) ? ".5" : "");
488 seq_printf(s
, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
490 c
->rate
> c
->max_rate
? '!' : ' ',
492 30 - level
* 3, c
->name
,
493 state
, c
->refcnt
, div
, clk_get_rate_all_locked(c
));
495 list_for_each_entry(child
, &clocks
, node
) {
496 if (child
->parent
!= c
)
499 clock_tree_show_one(s
, child
, level
+ 1);
503 static int clock_tree_show(struct seq_file
*s
, void *data
)
506 seq_printf(s
, " clock state ref div rate\n");
507 seq_printf(s
, "--------------------------------------------------------------\n");
509 mutex_lock(&clock_list_lock
);
513 list_for_each_entry(c
, &clocks
, node
)
514 if (c
->parent
== NULL
)
515 clock_tree_show_one(s
, c
, 0);
519 mutex_unlock(&clock_list_lock
);
523 static int clock_tree_open(struct inode
*inode
, struct file
*file
)
525 return single_open(file
, clock_tree_show
, inode
->i_private
);
528 static const struct file_operations clock_tree_fops
= {
529 .open
= clock_tree_open
,
532 .release
= single_release
,
535 static int possible_parents_show(struct seq_file
*s
, void *data
)
537 struct clk
*c
= s
->private;
540 for (i
= 0; c
->inputs
[i
].input
; i
++) {
541 char *first
= (i
== 0) ? "" : " ";
542 seq_printf(s
, "%s%s", first
, c
->inputs
[i
].input
->name
);
548 static int possible_parents_open(struct inode
*inode
, struct file
*file
)
550 return single_open(file
, possible_parents_show
, inode
->i_private
);
553 static const struct file_operations possible_parents_fops
= {
554 .open
= possible_parents_open
,
557 .release
= single_release
,
560 static int clk_debugfs_register_one(struct clk
*c
)
562 struct dentry
*d
, *child
, *child_tmp
;
564 d
= debugfs_create_dir(c
->name
, clk_debugfs_root
);
569 d
= debugfs_create_u8("refcnt", S_IRUGO
, c
->dent
, (u8
*)&c
->refcnt
);
573 d
= debugfs_create_u32("rate", S_IRUGO
, c
->dent
, (u32
*)&c
->rate
);
577 d
= debugfs_create_x32("flags", S_IRUGO
, c
->dent
, (u32
*)&c
->flags
);
582 d
= debugfs_create_file("possible_parents", S_IRUGO
, c
->dent
,
583 c
, &possible_parents_fops
);
592 list_for_each_entry_safe(child
, child_tmp
, &d
->d_subdirs
, d_u
.d_child
)
593 debugfs_remove(child
);
594 debugfs_remove(c
->dent
);
598 static int clk_debugfs_register(struct clk
*c
)
601 struct clk
*pa
= c
->parent
;
603 if (pa
&& !pa
->dent
) {
604 err
= clk_debugfs_register(pa
);
610 err
= clk_debugfs_register_one(c
);
617 static int __init
clk_debugfs_init(void)
623 d
= debugfs_create_dir("clock", NULL
);
626 clk_debugfs_root
= d
;
628 d
= debugfs_create_file("clock_tree", S_IRUGO
, clk_debugfs_root
, NULL
,
633 list_for_each_entry(c
, &clocks
, node
) {
634 err
= clk_debugfs_register(c
);
640 debugfs_remove_recursive(clk_debugfs_root
);
644 late_initcall(clk_debugfs_init
);