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/list.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/debugfs.h>
25 #include <linux/slab.h>
26 #include <linux/seq_file.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/clkdev.h>
34 static LIST_HEAD(clocks
);
36 static DEFINE_SPINLOCK(clock_lock
);
37 static DEFINE_MUTEX(dvfs_lock
);
39 static int clk_is_dvfs(struct clk
*c
)
41 return (c
->dvfs
!= NULL
);
44 static int dvfs_set_rate(struct dvfs
*d
, unsigned long rate
)
51 for (t
= d
->table
; t
->rate
!= 0; t
++) {
52 if (rate
<= t
->rate
) {
56 return regulator_set_voltage(d
->reg
,
58 d
->max_millivolts
* 1000);
65 static void dvfs_init(struct clk
*c
)
69 struct dvfs_table
*table
;
71 process_id
= c
->dvfs
->cpu
? tegra_core_process_id() :
72 tegra_cpu_process_id();
74 for (i
= 0; i
< c
->dvfs
->process_id_table_length
; i
++)
75 if (process_id
== c
->dvfs
->process_id_table
[i
].process_id
)
76 c
->dvfs
->table
= c
->dvfs
->process_id_table
[i
].table
;
78 if (c
->dvfs
->table
== NULL
) {
79 pr_err("Failed to find dvfs table for clock %s process %d\n",
84 c
->dvfs
->max_millivolts
= 0;
85 for (table
= c
->dvfs
->table
; table
->rate
!= 0; table
++)
86 if (c
->dvfs
->max_millivolts
< table
->millivolts
)
87 c
->dvfs
->max_millivolts
= table
->millivolts
;
89 c
->dvfs
->reg
= regulator_get(NULL
, c
->dvfs
->reg_id
);
91 if (IS_ERR(c
->dvfs
->reg
)) {
92 pr_err("Failed to get regulator %s for clock %s\n",
93 c
->dvfs
->reg_id
, c
->name
);
99 dvfs_set_rate(c
->dvfs
, c
->rate
);
102 struct clk
*tegra_get_clock_by_name(const char *name
)
105 struct clk
*ret
= NULL
;
107 spin_lock_irqsave(&clock_lock
, flags
);
108 list_for_each_entry(c
, &clocks
, node
) {
109 if (strcmp(c
->name
, name
) == 0) {
114 spin_unlock_irqrestore(&clock_lock
, flags
);
118 static void clk_recalculate_rate(struct clk
*c
)
125 rate
= c
->parent
->rate
;
127 if (c
->mul
!= 0 && c
->div
!= 0) {
128 rate
= rate
* c
->mul
;
129 do_div(rate
, c
->div
);
132 if (rate
> c
->max_rate
)
133 pr_warn("clocks: Set clock %s to rate %llu, max is %lu\n",
134 c
->name
, rate
, c
->max_rate
);
139 int clk_reparent(struct clk
*c
, struct clk
*parent
)
141 pr_debug("%s: %s\n", __func__
, c
->name
);
143 list_del(&c
->sibling
);
144 list_add_tail(&c
->sibling
, &parent
->children
);
148 static void propagate_rate(struct clk
*c
)
151 pr_debug("%s: %s\n", __func__
, c
->name
);
152 list_for_each_entry(clkp
, &c
->children
, sibling
) {
153 pr_debug(" %s\n", clkp
->name
);
154 clk_recalculate_rate(clkp
);
155 propagate_rate(clkp
);
159 void clk_init(struct clk
*c
)
163 pr_debug("%s: %s\n", __func__
, c
->name
);
165 spin_lock_irqsave(&clock_lock
, flags
);
167 INIT_LIST_HEAD(&c
->children
);
168 INIT_LIST_HEAD(&c
->sibling
);
170 if (c
->ops
&& c
->ops
->init
)
173 clk_recalculate_rate(c
);
175 list_add(&c
->node
, &clocks
);
178 list_add_tail(&c
->sibling
, &c
->parent
->children
);
180 spin_unlock_irqrestore(&clock_lock
, flags
);
183 int clk_enable_locked(struct clk
*c
)
186 pr_debug("%s: %s\n", __func__
, c
->name
);
187 if (c
->refcnt
== 0) {
189 ret
= clk_enable_locked(c
->parent
);
194 if (c
->ops
&& c
->ops
->enable
) {
195 ret
= c
->ops
->enable(c
);
198 clk_disable_locked(c
->parent
);
202 #ifdef CONFIG_DEBUG_FS
212 int clk_enable_cansleep(struct clk
*c
)
217 mutex_lock(&dvfs_lock
);
219 if (clk_is_dvfs(c
) && c
->refcnt
> 0)
220 dvfs_set_rate(c
->dvfs
, c
->rate
);
222 spin_lock_irqsave(&clock_lock
, flags
);
223 ret
= clk_enable_locked(c
);
224 spin_unlock_irqrestore(&clock_lock
, flags
);
226 mutex_unlock(&dvfs_lock
);
230 EXPORT_SYMBOL(clk_enable_cansleep
);
232 int clk_enable(struct clk
*c
)
240 spin_lock_irqsave(&clock_lock
, flags
);
241 ret
= clk_enable_locked(c
);
242 spin_unlock_irqrestore(&clock_lock
, flags
);
246 EXPORT_SYMBOL(clk_enable
);
248 void clk_disable_locked(struct clk
*c
)
250 pr_debug("%s: %s\n", __func__
, c
->name
);
251 if (c
->refcnt
== 0) {
252 WARN(1, "Attempting to disable clock %s with refcnt 0", c
->name
);
255 if (c
->refcnt
== 1) {
256 if (c
->ops
&& c
->ops
->disable
)
260 clk_disable_locked(c
->parent
);
267 void clk_disable_cansleep(struct clk
*c
)
271 mutex_lock(&dvfs_lock
);
273 spin_lock_irqsave(&clock_lock
, flags
);
274 clk_disable_locked(c
);
275 spin_unlock_irqrestore(&clock_lock
, flags
);
277 if (clk_is_dvfs(c
) && c
->refcnt
== 0)
278 dvfs_set_rate(c
->dvfs
, c
->rate
);
280 mutex_unlock(&dvfs_lock
);
282 EXPORT_SYMBOL(clk_disable_cansleep
);
284 void clk_disable(struct clk
*c
)
291 spin_lock_irqsave(&clock_lock
, flags
);
292 clk_disable_locked(c
);
293 spin_unlock_irqrestore(&clock_lock
, flags
);
295 EXPORT_SYMBOL(clk_disable
);
297 int clk_set_parent_locked(struct clk
*c
, struct clk
*parent
)
301 pr_debug("%s: %s\n", __func__
, c
->name
);
303 if (!c
->ops
|| !c
->ops
->set_parent
)
306 ret
= c
->ops
->set_parent(c
, parent
);
311 clk_recalculate_rate(c
);
318 int clk_set_parent(struct clk
*c
, struct clk
*parent
)
322 spin_lock_irqsave(&clock_lock
, flags
);
323 ret
= clk_set_parent_locked(c
, parent
);
324 spin_unlock_irqrestore(&clock_lock
, flags
);
327 EXPORT_SYMBOL(clk_set_parent
);
329 struct clk
*clk_get_parent(struct clk
*c
)
333 EXPORT_SYMBOL(clk_get_parent
);
335 int clk_set_rate_locked(struct clk
*c
, unsigned long rate
)
339 if (rate
> c
->max_rate
)
342 if (!c
->ops
|| !c
->ops
->set_rate
)
345 ret
= c
->ops
->set_rate(c
, rate
);
350 clk_recalculate_rate(c
);
357 int clk_set_rate_cansleep(struct clk
*c
, unsigned long rate
)
362 pr_debug("%s: %s\n", __func__
, c
->name
);
364 mutex_lock(&dvfs_lock
);
367 ret
= dvfs_set_rate(c
->dvfs
, rate
);
371 spin_lock_irqsave(&clock_lock
, flags
);
372 ret
= clk_set_rate_locked(c
, rate
);
373 spin_unlock_irqrestore(&clock_lock
, flags
);
378 ret
= dvfs_set_rate(c
->dvfs
, rate
);
381 mutex_unlock(&dvfs_lock
);
384 EXPORT_SYMBOL(clk_set_rate_cansleep
);
386 int clk_set_rate(struct clk
*c
, unsigned long rate
)
391 pr_debug("%s: %s\n", __func__
, c
->name
);
396 spin_lock_irqsave(&clock_lock
, flags
);
397 ret
= clk_set_rate_locked(c
, rate
);
398 spin_unlock_irqrestore(&clock_lock
, flags
);
402 EXPORT_SYMBOL(clk_set_rate
);
404 unsigned long clk_get_rate(struct clk
*c
)
409 spin_lock_irqsave(&clock_lock
, flags
);
411 pr_debug("%s: %s\n", __func__
, c
->name
);
415 spin_unlock_irqrestore(&clock_lock
, flags
);
418 EXPORT_SYMBOL(clk_get_rate
);
420 long clk_round_rate(struct clk
*c
, unsigned long rate
)
422 pr_debug("%s: %s\n", __func__
, c
->name
);
424 if (!c
->ops
|| !c
->ops
->round_rate
)
427 if (rate
> c
->max_rate
)
430 return c
->ops
->round_rate(c
, rate
);
432 EXPORT_SYMBOL(clk_round_rate
);
434 static int tegra_clk_init_one_from_table(struct tegra_clk_init_table
*table
)
441 c
= tegra_get_clock_by_name(table
->name
);
444 pr_warning("Unable to initialize clock %s\n",
450 p
= tegra_get_clock_by_name(table
->parent
);
452 pr_warning("Unable to find parent %s of clock %s\n",
453 table
->parent
, table
->name
);
457 if (c
->parent
!= p
) {
458 ret
= clk_set_parent(c
, p
);
460 pr_warning("Unable to set parent %s of clock %s: %d\n",
461 table
->parent
, table
->name
, ret
);
467 if (table
->rate
&& table
->rate
!= clk_get_rate(c
)) {
468 ret
= clk_set_rate(c
, table
->rate
);
470 pr_warning("Unable to set clock %s to rate %lu: %d\n",
471 table
->name
, table
->rate
, ret
);
476 if (table
->enabled
) {
479 pr_warning("Unable to enable clock %s: %d\n",
488 void tegra_clk_init_from_table(struct tegra_clk_init_table
*table
)
490 for (; table
->name
; table
++)
491 tegra_clk_init_one_from_table(table
);
493 EXPORT_SYMBOL(tegra_clk_init_from_table
);
495 void tegra_periph_reset_deassert(struct clk
*c
)
497 tegra2_periph_reset_deassert(c
);
499 EXPORT_SYMBOL(tegra_periph_reset_deassert
);
501 void tegra_periph_reset_assert(struct clk
*c
)
503 tegra2_periph_reset_assert(c
);
505 EXPORT_SYMBOL(tegra_periph_reset_assert
);
507 void __init
tegra_init_clock(void)
509 tegra2_init_clocks();
512 int __init
tegra_init_dvfs(void)
514 struct clk
*c
, *safe
;
516 mutex_lock(&dvfs_lock
);
518 list_for_each_entry_safe(c
, safe
, &clocks
, node
)
522 mutex_unlock(&dvfs_lock
);
527 late_initcall(tegra_init_dvfs
);
529 #ifdef CONFIG_DEBUG_FS
530 static struct dentry
*clk_debugfs_root
;
533 static void clock_tree_show_one(struct seq_file
*s
, struct clk
*c
, int level
)
537 const char *state
= "uninit";
542 else if (c
->state
== OFF
)
545 if (c
->mul
!= 0 && c
->div
!= 0) {
546 if (c
->mul
> c
->div
) {
547 int mul
= c
->mul
/ c
->div
;
548 int mul2
= (c
->mul
* 10 / c
->div
) % 10;
549 int mul3
= (c
->mul
* 10) % c
->div
;
550 if (mul2
== 0 && mul3
== 0)
551 snprintf(div
, sizeof(div
), "x%d", mul
);
553 snprintf(div
, sizeof(div
), "x%d.%d", mul
, mul2
);
555 snprintf(div
, sizeof(div
), "x%d.%d..", mul
, mul2
);
557 snprintf(div
, sizeof(div
), "%d%s", c
->div
/ c
->mul
,
558 (c
->div
% c
->mul
) ? ".5" : "");
562 seq_printf(s
, "%*s%c%c%-*s %-6s %-3d %-8s %-10lu\n",
564 c
->rate
> c
->max_rate
? '!' : ' ',
566 30 - level
* 3, c
->name
,
567 state
, c
->refcnt
, div
, c
->rate
);
568 list_for_each_entry_safe(child
, safe
, &c
->children
, sibling
) {
569 clock_tree_show_one(s
, child
, level
+ 1);
573 static int clock_tree_show(struct seq_file
*s
, void *data
)
577 seq_printf(s
, " clock state ref div rate\n");
578 seq_printf(s
, "--------------------------------------------------------------\n");
579 spin_lock_irqsave(&clock_lock
, flags
);
580 list_for_each_entry(c
, &clocks
, node
)
581 if (c
->parent
== NULL
)
582 clock_tree_show_one(s
, c
, 0);
583 spin_unlock_irqrestore(&clock_lock
, flags
);
587 static int clock_tree_open(struct inode
*inode
, struct file
*file
)
589 return single_open(file
, clock_tree_show
, inode
->i_private
);
592 static const struct file_operations clock_tree_fops
= {
593 .open
= clock_tree_open
,
596 .release
= single_release
,
599 static int possible_parents_show(struct seq_file
*s
, void *data
)
601 struct clk
*c
= s
->private;
604 for (i
= 0; c
->inputs
[i
].input
; i
++) {
605 char *first
= (i
== 0) ? "" : " ";
606 seq_printf(s
, "%s%s", first
, c
->inputs
[i
].input
->name
);
612 static int possible_parents_open(struct inode
*inode
, struct file
*file
)
614 return single_open(file
, possible_parents_show
, inode
->i_private
);
617 static const struct file_operations possible_parents_fops
= {
618 .open
= possible_parents_open
,
621 .release
= single_release
,
624 static int clk_debugfs_register_one(struct clk
*c
)
626 struct dentry
*d
, *child
, *child_tmp
;
628 d
= debugfs_create_dir(c
->name
, clk_debugfs_root
);
633 d
= debugfs_create_u8("refcnt", S_IRUGO
, c
->dent
, (u8
*)&c
->refcnt
);
637 d
= debugfs_create_u32("rate", S_IRUGO
, c
->dent
, (u32
*)&c
->rate
);
641 d
= debugfs_create_x32("flags", S_IRUGO
, c
->dent
, (u32
*)&c
->flags
);
646 d
= debugfs_create_file("possible_parents", S_IRUGO
, c
->dent
,
647 c
, &possible_parents_fops
);
656 list_for_each_entry_safe(child
, child_tmp
, &d
->d_subdirs
, d_u
.d_child
)
657 debugfs_remove(child
);
658 debugfs_remove(c
->dent
);
662 static int clk_debugfs_register(struct clk
*c
)
665 struct clk
*pa
= c
->parent
;
667 if (pa
&& !pa
->dent
) {
668 err
= clk_debugfs_register(pa
);
674 err
= clk_debugfs_register_one(c
);
681 static int __init
clk_debugfs_init(void)
687 d
= debugfs_create_dir("clock", NULL
);
690 clk_debugfs_root
= d
;
692 d
= debugfs_create_file("clock_tree", S_IRUGO
, clk_debugfs_root
, NULL
,
697 list_for_each_entry(c
, &clocks
, node
) {
698 err
= clk_debugfs_register(c
);
704 debugfs_remove_recursive(clk_debugfs_root
);
708 late_initcall(clk_debugfs_init
);