2 * drivers/sh/clk.c - SuperH clock framework
4 * Copyright (C) 2005 - 2009 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 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/list.h>
22 #include <linux/kobject.h>
23 #include <linux/sysdev.h>
24 #include <linux/seq_file.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/debugfs.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 void clk_rate_table_build(struct clk
*clk
,
37 struct cpufreq_frequency_table
*freq_table
,
39 struct clk_div_mult_table
*src_table
,
40 unsigned long *bitmap
)
42 unsigned long mult
, div
;
46 for (i
= 0; i
< nr_freqs
; i
++) {
50 if (src_table
->divisors
&& i
< src_table
->nr_divisors
)
51 div
= src_table
->divisors
[i
];
53 if (src_table
->multipliers
&& i
< src_table
->nr_multipliers
)
54 mult
= src_table
->multipliers
[i
];
56 if (!div
|| !mult
|| (bitmap
&& !test_bit(i
, bitmap
)))
57 freq
= CPUFREQ_ENTRY_INVALID
;
59 freq
= clk
->parent
->rate
* mult
/ div
;
61 freq_table
[i
].index
= i
;
62 freq_table
[i
].frequency
= freq
;
65 /* Termination entry */
66 freq_table
[i
].index
= i
;
67 freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
70 long clk_rate_table_round(struct clk
*clk
,
71 struct cpufreq_frequency_table
*freq_table
,
74 unsigned long rate_error
, rate_error_prev
= ~0UL;
75 unsigned long rate_best_fit
= rate
;
76 unsigned long highest
, lowest
;
81 for (i
= 0; freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
82 unsigned long freq
= freq_table
[i
].frequency
;
84 if (freq
== CPUFREQ_ENTRY_INVALID
)
92 rate_error
= abs(freq
- rate
);
93 if (rate_error
< rate_error_prev
) {
95 rate_error_prev
= rate_error
;
103 rate_best_fit
= highest
;
105 rate_best_fit
= lowest
;
107 return rate_best_fit
;
110 int clk_rate_table_find(struct clk
*clk
,
111 struct cpufreq_frequency_table
*freq_table
,
116 for (i
= 0; freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
117 unsigned long freq
= freq_table
[i
].frequency
;
119 if (freq
== CPUFREQ_ENTRY_INVALID
)
129 /* Used for clocks that always have same value as the parent clock */
130 unsigned long followparent_recalc(struct clk
*clk
)
132 return clk
->parent
? clk
->parent
->rate
: 0;
135 int clk_reparent(struct clk
*child
, struct clk
*parent
)
137 list_del_init(&child
->sibling
);
139 list_add(&child
->sibling
, &parent
->children
);
140 child
->parent
= parent
;
142 /* now do the debugfs renaming to reattach the child
143 to the proper parent */
148 /* Propagate rate to children */
149 void propagate_rate(struct clk
*tclk
)
153 list_for_each_entry(clkp
, &tclk
->children
, sibling
) {
154 if (clkp
->ops
&& clkp
->ops
->recalc
)
155 clkp
->rate
= clkp
->ops
->recalc(clkp
);
157 propagate_rate(clkp
);
161 static void __clk_disable(struct clk
*clk
)
163 if (WARN(!clk
->usecount
, "Trying to disable clock %s with 0 usecount\n",
167 if (!(--clk
->usecount
)) {
168 if (likely(clk
->ops
&& clk
->ops
->disable
))
169 clk
->ops
->disable(clk
);
170 if (likely(clk
->parent
))
171 __clk_disable(clk
->parent
);
175 void clk_disable(struct clk
*clk
)
182 spin_lock_irqsave(&clock_lock
, flags
);
184 spin_unlock_irqrestore(&clock_lock
, flags
);
186 EXPORT_SYMBOL_GPL(clk_disable
);
188 static int __clk_enable(struct clk
*clk
)
192 if (clk
->usecount
++ == 0) {
194 ret
= __clk_enable(clk
->parent
);
199 if (clk
->ops
&& clk
->ops
->enable
) {
200 ret
= clk
->ops
->enable(clk
);
203 __clk_disable(clk
->parent
);
215 int clk_enable(struct clk
*clk
)
223 spin_lock_irqsave(&clock_lock
, flags
);
224 ret
= __clk_enable(clk
);
225 spin_unlock_irqrestore(&clock_lock
, flags
);
229 EXPORT_SYMBOL_GPL(clk_enable
);
231 static LIST_HEAD(root_clks
);
234 * recalculate_root_clocks - recalculate and propagate all root clocks
236 * Recalculates all root clocks (clocks with no parent), which if the
237 * clock's .recalc is set correctly, should also propagate their rates.
240 void recalculate_root_clocks(void)
244 list_for_each_entry(clkp
, &root_clks
, sibling
) {
245 if (clkp
->ops
&& clkp
->ops
->recalc
)
246 clkp
->rate
= clkp
->ops
->recalc(clkp
);
247 propagate_rate(clkp
);
251 int clk_register(struct clk
*clk
)
253 if (clk
== NULL
|| IS_ERR(clk
))
257 * trap out already registered clocks
259 if (clk
->node
.next
|| clk
->node
.prev
)
262 mutex_lock(&clock_list_sem
);
264 INIT_LIST_HEAD(&clk
->children
);
268 list_add(&clk
->sibling
, &clk
->parent
->children
);
270 list_add(&clk
->sibling
, &root_clks
);
272 list_add(&clk
->node
, &clock_list
);
273 if (clk
->ops
&& clk
->ops
->init
)
275 mutex_unlock(&clock_list_sem
);
279 EXPORT_SYMBOL_GPL(clk_register
);
281 void clk_unregister(struct clk
*clk
)
283 mutex_lock(&clock_list_sem
);
284 list_del(&clk
->sibling
);
285 list_del(&clk
->node
);
286 mutex_unlock(&clock_list_sem
);
288 EXPORT_SYMBOL_GPL(clk_unregister
);
290 void clk_enable_init_clocks(void)
294 list_for_each_entry(clkp
, &clock_list
, node
)
295 if (clkp
->flags
& CLK_ENABLE_ON_INIT
)
299 unsigned long clk_get_rate(struct clk
*clk
)
303 EXPORT_SYMBOL_GPL(clk_get_rate
);
305 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
307 return clk_set_rate_ex(clk
, rate
, 0);
309 EXPORT_SYMBOL_GPL(clk_set_rate
);
311 int clk_set_rate_ex(struct clk
*clk
, unsigned long rate
, int algo_id
)
313 int ret
= -EOPNOTSUPP
;
316 spin_lock_irqsave(&clock_lock
, flags
);
318 if (likely(clk
->ops
&& clk
->ops
->set_rate
)) {
319 ret
= clk
->ops
->set_rate(clk
, rate
, algo_id
);
327 if (clk
->ops
&& clk
->ops
->recalc
)
328 clk
->rate
= clk
->ops
->recalc(clk
);
333 spin_unlock_irqrestore(&clock_lock
, flags
);
337 EXPORT_SYMBOL_GPL(clk_set_rate_ex
);
339 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
346 if (clk
->parent
== parent
)
349 spin_lock_irqsave(&clock_lock
, flags
);
350 if (clk
->usecount
== 0) {
351 if (clk
->ops
->set_parent
)
352 ret
= clk
->ops
->set_parent(clk
, parent
);
354 ret
= clk_reparent(clk
, parent
);
357 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
358 clk
->name
, clk
->parent
->name
, clk
->rate
);
359 if (clk
->ops
->recalc
)
360 clk
->rate
= clk
->ops
->recalc(clk
);
365 spin_unlock_irqrestore(&clock_lock
, flags
);
369 EXPORT_SYMBOL_GPL(clk_set_parent
);
371 struct clk
*clk_get_parent(struct clk
*clk
)
375 EXPORT_SYMBOL_GPL(clk_get_parent
);
377 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
379 if (likely(clk
->ops
&& clk
->ops
->round_rate
)) {
380 unsigned long flags
, rounded
;
382 spin_lock_irqsave(&clock_lock
, flags
);
383 rounded
= clk
->ops
->round_rate(clk
, rate
);
384 spin_unlock_irqrestore(&clock_lock
, flags
);
389 return clk_get_rate(clk
);
391 EXPORT_SYMBOL_GPL(clk_round_rate
);
394 static int clks_sysdev_suspend(struct sys_device
*dev
, pm_message_t state
)
396 static pm_message_t prev_state
;
399 switch (state
.event
) {
401 /* Resumeing from hibernation */
402 if (prev_state
.event
!= PM_EVENT_FREEZE
)
405 list_for_each_entry(clkp
, &clock_list
, node
) {
406 if (likely(clkp
->ops
)) {
407 unsigned long rate
= clkp
->rate
;
409 if (likely(clkp
->ops
->set_parent
))
410 clkp
->ops
->set_parent(clkp
,
412 if (likely(clkp
->ops
->set_rate
))
413 clkp
->ops
->set_rate(clkp
,
415 else if (likely(clkp
->ops
->recalc
))
416 clkp
->rate
= clkp
->ops
->recalc(clkp
);
420 case PM_EVENT_FREEZE
:
422 case PM_EVENT_SUSPEND
:
430 static int clks_sysdev_resume(struct sys_device
*dev
)
432 return clks_sysdev_suspend(dev
, PMSG_ON
);
435 static struct sysdev_class clks_sysdev_class
= {
439 static struct sysdev_driver clks_sysdev_driver
= {
440 .suspend
= clks_sysdev_suspend
,
441 .resume
= clks_sysdev_resume
,
444 static struct sys_device clks_sysdev_dev
= {
445 .cls
= &clks_sysdev_class
,
448 static int __init
clk_sysdev_init(void)
450 sysdev_class_register(&clks_sysdev_class
);
451 sysdev_driver_register(&clks_sysdev_class
, &clks_sysdev_driver
);
452 sysdev_register(&clks_sysdev_dev
);
456 subsys_initcall(clk_sysdev_init
);
460 * debugfs support to trace clock tree hierarchy and attributes
462 static struct dentry
*clk_debugfs_root
;
464 static int clk_debugfs_register_one(struct clk
*c
)
467 struct dentry
*d
, *child
, *child_tmp
;
468 struct clk
*pa
= c
->parent
;
472 p
+= sprintf(p
, "%s", c
->name
);
474 sprintf(p
, ":%d", c
->id
);
475 d
= debugfs_create_dir(s
, pa
? pa
->dentry
: clk_debugfs_root
);
480 d
= debugfs_create_u8("usecount", S_IRUGO
, c
->dentry
, (u8
*)&c
->usecount
);
485 d
= debugfs_create_u32("rate", S_IRUGO
, c
->dentry
, (u32
*)&c
->rate
);
490 d
= debugfs_create_x32("flags", S_IRUGO
, c
->dentry
, (u32
*)&c
->flags
);
499 list_for_each_entry_safe(child
, child_tmp
, &d
->d_subdirs
, d_u
.d_child
)
500 debugfs_remove(child
);
501 debugfs_remove(c
->dentry
);
505 static int clk_debugfs_register(struct clk
*c
)
508 struct clk
*pa
= c
->parent
;
510 if (pa
&& !pa
->dentry
) {
511 err
= clk_debugfs_register(pa
);
516 if (!c
->dentry
&& c
->name
) {
517 err
= clk_debugfs_register_one(c
);
524 static int __init
clk_debugfs_init(void)
530 d
= debugfs_create_dir("clock", NULL
);
533 clk_debugfs_root
= d
;
535 list_for_each_entry(c
, &clock_list
, node
) {
536 err
= clk_debugfs_register(c
);
542 debugfs_remove_recursive(clk_debugfs_root
);
545 late_initcall(clk_debugfs_init
);