aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/sh/include/asm/clock.h3
-rw-r--r--arch/sh/kernel/cpu/clock.c543
-rw-r--r--drivers/sh/Makefile1
-rw-r--r--drivers/sh/clk.c548
-rw-r--r--include/linux/sh_clk.h2
5 files changed, 561 insertions, 536 deletions
diff --git a/arch/sh/include/asm/clock.h b/arch/sh/include/asm/clock.h
index f387e5caee16..803d4c7f09dc 100644
--- a/arch/sh/include/asm/clock.h
+++ b/arch/sh/include/asm/clock.h
@@ -10,4 +10,7 @@ int __init arch_clk_init(void);
10/* arch/sh/kernel/cpu/clock-cpg.c */ 10/* arch/sh/kernel/cpu/clock-cpg.c */
11int __init __deprecated cpg_clk_init(void); 11int __init __deprecated cpg_clk_init(void);
12 12
13/* arch/sh/kernel/cpu/clock.c */
14int clk_init(void);
15
13#endif /* __ASM_SH_CLOCK_H */ 16#endif /* __ASM_SH_CLOCK_H */
diff --git a/arch/sh/kernel/cpu/clock.c b/arch/sh/kernel/cpu/clock.c
index 8cc6935d91ae..50f887dda565 100644
--- a/arch/sh/kernel/cpu/clock.c
+++ b/arch/sh/kernel/cpu/clock.c
@@ -16,467 +16,10 @@
16 */ 16 */
17#include <linux/kernel.h> 17#include <linux/kernel.h>
18#include <linux/init.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> 19#include <linux/clk.h>
30#include <asm/clock.h> 20#include <asm/clock.h>
31#include <asm/machvec.h> 21#include <asm/machvec.h>
32 22
33static LIST_HEAD(clock_list);
34static DEFINE_SPINLOCK(clock_lock);
35static DEFINE_MUTEX(clock_list_sem);
36
37void clk_rate_table_build(struct clk *clk,
38 struct cpufreq_frequency_table *freq_table,
39 int nr_freqs,
40 struct clk_div_mult_table *src_table,
41 unsigned long *bitmap)
42{
43 unsigned long mult, div;
44 unsigned long freq;
45 int i;
46
47 for (i = 0; i < nr_freqs; i++) {
48 div = 1;
49 mult = 1;
50
51 if (src_table->divisors && i < src_table->nr_divisors)
52 div = src_table->divisors[i];
53
54 if (src_table->multipliers && i < src_table->nr_multipliers)
55 mult = src_table->multipliers[i];
56
57 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
58 freq = CPUFREQ_ENTRY_INVALID;
59 else
60 freq = clk->parent->rate * mult / div;
61
62 freq_table[i].index = i;
63 freq_table[i].frequency = freq;
64 }
65
66 /* Termination entry */
67 freq_table[i].index = i;
68 freq_table[i].frequency = CPUFREQ_TABLE_END;
69}
70
71long clk_rate_table_round(struct clk *clk,
72 struct cpufreq_frequency_table *freq_table,
73 unsigned long rate)
74{
75 unsigned long rate_error, rate_error_prev = ~0UL;
76 unsigned long rate_best_fit = rate;
77 unsigned long highest, lowest;
78 int i;
79
80 highest = lowest = 0;
81
82 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
83 unsigned long freq = freq_table[i].frequency;
84
85 if (freq == CPUFREQ_ENTRY_INVALID)
86 continue;
87
88 if (freq > highest)
89 highest = freq;
90 if (freq < lowest)
91 lowest = freq;
92
93 rate_error = abs(freq - rate);
94 if (rate_error < rate_error_prev) {
95 rate_best_fit = freq;
96 rate_error_prev = rate_error;
97 }
98
99 if (rate_error == 0)
100 break;
101 }
102
103 if (rate >= highest)
104 rate_best_fit = highest;
105 if (rate <= lowest)
106 rate_best_fit = lowest;
107
108 return rate_best_fit;
109}
110
111int clk_rate_table_find(struct clk *clk,
112 struct cpufreq_frequency_table *freq_table,
113 unsigned long rate)
114{
115 int i;
116
117 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
118 unsigned long freq = freq_table[i].frequency;
119
120 if (freq == CPUFREQ_ENTRY_INVALID)
121 continue;
122
123 if (freq == rate)
124 return i;
125 }
126
127 return -ENOENT;
128}
129
130/* Used for clocks that always have same value as the parent clock */
131unsigned long followparent_recalc(struct clk *clk)
132{
133 return clk->parent ? clk->parent->rate : 0;
134}
135
136int clk_reparent(struct clk *child, struct clk *parent)
137{
138 list_del_init(&child->sibling);
139 if (parent)
140 list_add(&child->sibling, &parent->children);
141 child->parent = parent;
142
143 /* now do the debugfs renaming to reattach the child
144 to the proper parent */
145
146 return 0;
147}
148
149/* Propagate rate to children */
150void propagate_rate(struct clk *tclk)
151{
152 struct clk *clkp;
153
154 list_for_each_entry(clkp, &tclk->children, sibling) {
155 if (clkp->ops && clkp->ops->recalc)
156 clkp->rate = clkp->ops->recalc(clkp);
157
158 propagate_rate(clkp);
159 }
160}
161
162static void __clk_disable(struct clk *clk)
163{
164 if (clk->usecount == 0) {
165 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
166 clk->name);
167 WARN_ON(1);
168 return;
169 }
170
171 if (!(--clk->usecount)) {
172 if (likely(clk->ops && clk->ops->disable))
173 clk->ops->disable(clk);
174 if (likely(clk->parent))
175 __clk_disable(clk->parent);
176 }
177}
178
179void clk_disable(struct clk *clk)
180{
181 unsigned long flags;
182
183 if (!clk)
184 return;
185
186 spin_lock_irqsave(&clock_lock, flags);
187 __clk_disable(clk);
188 spin_unlock_irqrestore(&clock_lock, flags);
189}
190EXPORT_SYMBOL_GPL(clk_disable);
191
192static int __clk_enable(struct clk *clk)
193{
194 int ret = 0;
195
196 if (clk->usecount++ == 0) {
197 if (clk->parent) {
198 ret = __clk_enable(clk->parent);
199 if (unlikely(ret))
200 goto err;
201 }
202
203 if (clk->ops && clk->ops->enable) {
204 ret = clk->ops->enable(clk);
205 if (ret) {
206 if (clk->parent)
207 __clk_disable(clk->parent);
208 goto err;
209 }
210 }
211 }
212
213 return ret;
214err:
215 clk->usecount--;
216 return ret;
217}
218
219int clk_enable(struct clk *clk)
220{
221 unsigned long flags;
222 int ret;
223
224 if (!clk)
225 return -EINVAL;
226
227 spin_lock_irqsave(&clock_lock, flags);
228 ret = __clk_enable(clk);
229 spin_unlock_irqrestore(&clock_lock, flags);
230
231 return ret;
232}
233EXPORT_SYMBOL_GPL(clk_enable);
234
235static LIST_HEAD(root_clks);
236
237/**
238 * recalculate_root_clocks - recalculate and propagate all root clocks
239 *
240 * Recalculates all root clocks (clocks with no parent), which if the
241 * clock's .recalc is set correctly, should also propagate their rates.
242 * Called at init.
243 */
244void recalculate_root_clocks(void)
245{
246 struct clk *clkp;
247
248 list_for_each_entry(clkp, &root_clks, sibling) {
249 if (clkp->ops && clkp->ops->recalc)
250 clkp->rate = clkp->ops->recalc(clkp);
251 propagate_rate(clkp);
252 }
253}
254
255int clk_register(struct clk *clk)
256{
257 if (clk == NULL || IS_ERR(clk))
258 return -EINVAL;
259
260 /*
261 * trap out already registered clocks
262 */
263 if (clk->node.next || clk->node.prev)
264 return 0;
265
266 mutex_lock(&clock_list_sem);
267
268 INIT_LIST_HEAD(&clk->children);
269 clk->usecount = 0;
270
271 if (clk->parent)
272 list_add(&clk->sibling, &clk->parent->children);
273 else
274 list_add(&clk->sibling, &root_clks);
275
276 list_add(&clk->node, &clock_list);
277 if (clk->ops && clk->ops->init)
278 clk->ops->init(clk);
279 mutex_unlock(&clock_list_sem);
280
281 return 0;
282}
283EXPORT_SYMBOL_GPL(clk_register);
284
285void clk_unregister(struct clk *clk)
286{
287 mutex_lock(&clock_list_sem);
288 list_del(&clk->sibling);
289 list_del(&clk->node);
290 mutex_unlock(&clock_list_sem);
291}
292EXPORT_SYMBOL_GPL(clk_unregister);
293
294static void clk_enable_init_clocks(void)
295{
296 struct clk *clkp;
297
298 list_for_each_entry(clkp, &clock_list, node)
299 if (clkp->flags & CLK_ENABLE_ON_INIT)
300 clk_enable(clkp);
301}
302
303unsigned long clk_get_rate(struct clk *clk)
304{
305 return clk->rate;
306}
307EXPORT_SYMBOL_GPL(clk_get_rate);
308
309int clk_set_rate(struct clk *clk, unsigned long rate)
310{
311 return clk_set_rate_ex(clk, rate, 0);
312}
313EXPORT_SYMBOL_GPL(clk_set_rate);
314
315int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
316{
317 int ret = -EOPNOTSUPP;
318 unsigned long flags;
319
320 spin_lock_irqsave(&clock_lock, flags);
321
322 if (likely(clk->ops && clk->ops->set_rate)) {
323 ret = clk->ops->set_rate(clk, rate, algo_id);
324 if (ret != 0)
325 goto out_unlock;
326 } else {
327 clk->rate = rate;
328 ret = 0;
329 }
330
331 if (clk->ops && clk->ops->recalc)
332 clk->rate = clk->ops->recalc(clk);
333
334 propagate_rate(clk);
335
336out_unlock:
337 spin_unlock_irqrestore(&clock_lock, flags);
338
339 return ret;
340}
341EXPORT_SYMBOL_GPL(clk_set_rate_ex);
342
343int clk_set_parent(struct clk *clk, struct clk *parent)
344{
345 unsigned long flags;
346 int ret = -EINVAL;
347
348 if (!parent || !clk)
349 return ret;
350 if (clk->parent == parent)
351 return 0;
352
353 spin_lock_irqsave(&clock_lock, flags);
354 if (clk->usecount == 0) {
355 if (clk->ops->set_parent)
356 ret = clk->ops->set_parent(clk, parent);
357 else
358 ret = clk_reparent(clk, parent);
359
360 if (ret == 0) {
361 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
362 clk->name, clk->parent->name, clk->rate);
363 if (clk->ops->recalc)
364 clk->rate = clk->ops->recalc(clk);
365 propagate_rate(clk);
366 }
367 } else
368 ret = -EBUSY;
369 spin_unlock_irqrestore(&clock_lock, flags);
370
371 return ret;
372}
373EXPORT_SYMBOL_GPL(clk_set_parent);
374
375struct clk *clk_get_parent(struct clk *clk)
376{
377 return clk->parent;
378}
379EXPORT_SYMBOL_GPL(clk_get_parent);
380
381long clk_round_rate(struct clk *clk, unsigned long rate)
382{
383 if (likely(clk->ops && clk->ops->round_rate)) {
384 unsigned long flags, rounded;
385
386 spin_lock_irqsave(&clock_lock, flags);
387 rounded = clk->ops->round_rate(clk, rate);
388 spin_unlock_irqrestore(&clock_lock, flags);
389
390 return rounded;
391 }
392
393 return clk_get_rate(clk);
394}
395EXPORT_SYMBOL_GPL(clk_round_rate);
396
397/*
398 * Returns a clock. Note that we first try to use device id on the bus
399 * and clock name. If this fails, we try to use clock name only.
400 */
401struct clk *clk_get(struct device *dev, const char *con_id)
402{
403 const char *dev_id = dev ? dev_name(dev) : NULL;
404
405 return clk_get_sys(dev_id, con_id);
406}
407EXPORT_SYMBOL_GPL(clk_get);
408
409void clk_put(struct clk *clk)
410{
411}
412EXPORT_SYMBOL_GPL(clk_put);
413
414#ifdef CONFIG_PM
415static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
416{
417 static pm_message_t prev_state;
418 struct clk *clkp;
419
420 switch (state.event) {
421 case PM_EVENT_ON:
422 /* Resumeing from hibernation */
423 if (prev_state.event != PM_EVENT_FREEZE)
424 break;
425
426 list_for_each_entry(clkp, &clock_list, node) {
427 if (likely(clkp->ops)) {
428 unsigned long rate = clkp->rate;
429
430 if (likely(clkp->ops->set_parent))
431 clkp->ops->set_parent(clkp,
432 clkp->parent);
433 if (likely(clkp->ops->set_rate))
434 clkp->ops->set_rate(clkp,
435 rate, NO_CHANGE);
436 else if (likely(clkp->ops->recalc))
437 clkp->rate = clkp->ops->recalc(clkp);
438 }
439 }
440 break;
441 case PM_EVENT_FREEZE:
442 break;
443 case PM_EVENT_SUSPEND:
444 break;
445 }
446
447 prev_state = state;
448 return 0;
449}
450
451static int clks_sysdev_resume(struct sys_device *dev)
452{
453 return clks_sysdev_suspend(dev, PMSG_ON);
454}
455
456static struct sysdev_class clks_sysdev_class = {
457 .name = "clks",
458};
459
460static struct sysdev_driver clks_sysdev_driver = {
461 .suspend = clks_sysdev_suspend,
462 .resume = clks_sysdev_resume,
463};
464
465static struct sys_device clks_sysdev_dev = {
466 .cls = &clks_sysdev_class,
467};
468
469static int __init clk_sysdev_init(void)
470{
471 sysdev_class_register(&clks_sysdev_class);
472 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
473 sysdev_register(&clks_sysdev_dev);
474
475 return 0;
476}
477subsys_initcall(clk_sysdev_init);
478#endif
479
480int __init clk_init(void) 23int __init clk_init(void)
481{ 24{
482 int ret; 25 int ret;
@@ -506,89 +49,19 @@ int __init clk_init(void)
506} 49}
507 50
508/* 51/*
509 * debugfs support to trace clock tree hierarchy and attributes 52 * Returns a clock. Note that we first try to use device id on the bus
53 * and clock name. If this fails, we try to use clock name only.
510 */ 54 */
511static struct dentry *clk_debugfs_root; 55struct clk *clk_get(struct device *dev, const char *con_id)
512
513static int clk_debugfs_register_one(struct clk *c)
514{ 56{
515 int err; 57 const char *dev_id = dev ? dev_name(dev) : NULL;
516 struct dentry *d, *child, *child_tmp;
517 struct clk *pa = c->parent;
518 char s[255];
519 char *p = s;
520
521 p += sprintf(p, "%s", c->name);
522 if (c->id >= 0)
523 sprintf(p, ":%d", c->id);
524 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
525 if (!d)
526 return -ENOMEM;
527 c->dentry = d;
528
529 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
530 if (!d) {
531 err = -ENOMEM;
532 goto err_out;
533 }
534 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
535 if (!d) {
536 err = -ENOMEM;
537 goto err_out;
538 }
539 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
540 if (!d) {
541 err = -ENOMEM;
542 goto err_out;
543 }
544 return 0;
545 58
546err_out: 59 return clk_get_sys(dev_id, con_id);
547 d = c->dentry;
548 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
549 debugfs_remove(child);
550 debugfs_remove(c->dentry);
551 return err;
552} 60}
61EXPORT_SYMBOL_GPL(clk_get);
553 62
554static int clk_debugfs_register(struct clk *c) 63void clk_put(struct clk *clk)
555{ 64{
556 int err;
557 struct clk *pa = c->parent;
558
559 if (pa && !pa->dentry) {
560 err = clk_debugfs_register(pa);
561 if (err)
562 return err;
563 }
564
565 if (!c->dentry && c->name) {
566 err = clk_debugfs_register_one(c);
567 if (err)
568 return err;
569 }
570 return 0;
571} 65}
66EXPORT_SYMBOL_GPL(clk_put);
572 67
573static int __init clk_debugfs_init(void)
574{
575 struct clk *c;
576 struct dentry *d;
577 int err;
578
579 d = debugfs_create_dir("clock", NULL);
580 if (!d)
581 return -ENOMEM;
582 clk_debugfs_root = d;
583
584 list_for_each_entry(c, &clock_list, node) {
585 err = clk_debugfs_register(c);
586 if (err)
587 goto err_out;
588 }
589 return 0;
590err_out:
591 debugfs_remove_recursive(clk_debugfs_root);
592 return err;
593}
594late_initcall(clk_debugfs_init);
diff --git a/drivers/sh/Makefile b/drivers/sh/Makefile
index 4956bf1f2134..033a949c496a 100644
--- a/drivers/sh/Makefile
+++ b/drivers/sh/Makefile
@@ -4,4 +4,5 @@
4obj-$(CONFIG_SUPERHYWAY) += superhyway/ 4obj-$(CONFIG_SUPERHYWAY) += superhyway/
5obj-$(CONFIG_MAPLE) += maple/ 5obj-$(CONFIG_MAPLE) += maple/
6obj-$(CONFIG_GENERIC_GPIO) += pfc.o 6obj-$(CONFIG_GENERIC_GPIO) += pfc.o
7obj-$(CONFIG_SUPERH) += clk.o
7obj-y += intc.o 8obj-y += intc.o
diff --git a/drivers/sh/clk.c b/drivers/sh/clk.c
new file mode 100644
index 000000000000..c90a3e1e1085
--- /dev/null
+++ b/drivers/sh/clk.c
@@ -0,0 +1,548 @@
1/*
2 * drivers/sh/clk.c - SuperH clock framework
3 *
4 * Copyright (C) 2005 - 2009 Paul Mundt
5 *
6 * This clock framework is derived from the OMAP version by:
7 *
8 * Copyright (C) 2004 - 2008 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10 *
11 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
12 *
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
15 * for more details.
16 */
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>
31
32static LIST_HEAD(clock_list);
33static DEFINE_SPINLOCK(clock_lock);
34static DEFINE_MUTEX(clock_list_sem);
35
36void clk_rate_table_build(struct clk *clk,
37 struct cpufreq_frequency_table *freq_table,
38 int nr_freqs,
39 struct clk_div_mult_table *src_table,
40 unsigned long *bitmap)
41{
42 unsigned long mult, div;
43 unsigned long freq;
44 int i;
45
46 for (i = 0; i < nr_freqs; i++) {
47 div = 1;
48 mult = 1;
49
50 if (src_table->divisors && i < src_table->nr_divisors)
51 div = src_table->divisors[i];
52
53 if (src_table->multipliers && i < src_table->nr_multipliers)
54 mult = src_table->multipliers[i];
55
56 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
57 freq = CPUFREQ_ENTRY_INVALID;
58 else
59 freq = clk->parent->rate * mult / div;
60
61 freq_table[i].index = i;
62 freq_table[i].frequency = freq;
63 }
64
65 /* Termination entry */
66 freq_table[i].index = i;
67 freq_table[i].frequency = CPUFREQ_TABLE_END;
68}
69
70long clk_rate_table_round(struct clk *clk,
71 struct cpufreq_frequency_table *freq_table,
72 unsigned long rate)
73{
74 unsigned long rate_error, rate_error_prev = ~0UL;
75 unsigned long rate_best_fit = rate;
76 unsigned long highest, lowest;
77 int i;
78
79 highest = lowest = 0;
80
81 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
82 unsigned long freq = freq_table[i].frequency;
83
84 if (freq == CPUFREQ_ENTRY_INVALID)
85 continue;
86
87 if (freq > highest)
88 highest = freq;
89 if (freq < lowest)
90 lowest = freq;
91
92 rate_error = abs(freq - rate);
93 if (rate_error < rate_error_prev) {
94 rate_best_fit = freq;
95 rate_error_prev = rate_error;
96 }
97
98 if (rate_error == 0)
99 break;
100 }
101
102 if (rate >= highest)
103 rate_best_fit = highest;
104 if (rate <= lowest)
105 rate_best_fit = lowest;
106
107 return rate_best_fit;
108}
109
110int clk_rate_table_find(struct clk *clk,
111 struct cpufreq_frequency_table *freq_table,
112 unsigned long rate)
113{
114 int i;
115
116 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
117 unsigned long freq = freq_table[i].frequency;
118
119 if (freq == CPUFREQ_ENTRY_INVALID)
120 continue;
121
122 if (freq == rate)
123 return i;
124 }
125
126 return -ENOENT;
127}
128
129/* Used for clocks that always have same value as the parent clock */
130unsigned long followparent_recalc(struct clk *clk)
131{
132 return clk->parent ? clk->parent->rate : 0;
133}
134
135int clk_reparent(struct clk *child, struct clk *parent)
136{
137 list_del_init(&child->sibling);
138 if (parent)
139 list_add(&child->sibling, &parent->children);
140 child->parent = parent;
141
142 /* now do the debugfs renaming to reattach the child
143 to the proper parent */
144
145 return 0;
146}
147
148/* Propagate rate to children */
149void propagate_rate(struct clk *tclk)
150{
151 struct clk *clkp;
152
153 list_for_each_entry(clkp, &tclk->children, sibling) {
154 if (clkp->ops && clkp->ops->recalc)
155 clkp->rate = clkp->ops->recalc(clkp);
156
157 propagate_rate(clkp);
158 }
159}
160
161static void __clk_disable(struct clk *clk)
162{
163 if (clk->usecount == 0) {
164 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
165 clk->name);
166 WARN_ON(1);
167 return;
168 }
169
170 if (!(--clk->usecount)) {
171 if (likely(clk->ops && clk->ops->disable))
172 clk->ops->disable(clk);
173 if (likely(clk->parent))
174 __clk_disable(clk->parent);
175 }
176}
177
178void clk_disable(struct clk *clk)
179{
180 unsigned long flags;
181
182 if (!clk)
183 return;
184
185 spin_lock_irqsave(&clock_lock, flags);
186 __clk_disable(clk);
187 spin_unlock_irqrestore(&clock_lock, flags);
188}
189EXPORT_SYMBOL_GPL(clk_disable);
190
191static int __clk_enable(struct clk *clk)
192{
193 int ret = 0;
194
195 if (clk->usecount++ == 0) {
196 if (clk->parent) {
197 ret = __clk_enable(clk->parent);
198 if (unlikely(ret))
199 goto err;
200 }
201
202 if (clk->ops && clk->ops->enable) {
203 ret = clk->ops->enable(clk);
204 if (ret) {
205 if (clk->parent)
206 __clk_disable(clk->parent);
207 goto err;
208 }
209 }
210 }
211
212 return ret;
213err:
214 clk->usecount--;
215 return ret;
216}
217
218int clk_enable(struct clk *clk)
219{
220 unsigned long flags;
221 int ret;
222
223 if (!clk)
224 return -EINVAL;
225
226 spin_lock_irqsave(&clock_lock, flags);
227 ret = __clk_enable(clk);
228 spin_unlock_irqrestore(&clock_lock, flags);
229
230 return ret;
231}
232EXPORT_SYMBOL_GPL(clk_enable);
233
234static LIST_HEAD(root_clks);
235
236/**
237 * recalculate_root_clocks - recalculate and propagate all root clocks
238 *
239 * Recalculates all root clocks (clocks with no parent), which if the
240 * clock's .recalc is set correctly, should also propagate their rates.
241 * Called at init.
242 */
243void recalculate_root_clocks(void)
244{
245 struct clk *clkp;
246
247 list_for_each_entry(clkp, &root_clks, sibling) {
248 if (clkp->ops && clkp->ops->recalc)
249 clkp->rate = clkp->ops->recalc(clkp);
250 propagate_rate(clkp);
251 }
252}
253
254int clk_register(struct clk *clk)
255{
256 if (clk == NULL || IS_ERR(clk))
257 return -EINVAL;
258
259 /*
260 * trap out already registered clocks
261 */
262 if (clk->node.next || clk->node.prev)
263 return 0;
264
265 mutex_lock(&clock_list_sem);
266
267 INIT_LIST_HEAD(&clk->children);
268 clk->usecount = 0;
269
270 if (clk->parent)
271 list_add(&clk->sibling, &clk->parent->children);
272 else
273 list_add(&clk->sibling, &root_clks);
274
275 list_add(&clk->node, &clock_list);
276 if (clk->ops && clk->ops->init)
277 clk->ops->init(clk);
278 mutex_unlock(&clock_list_sem);
279
280 return 0;
281}
282EXPORT_SYMBOL_GPL(clk_register);
283
284void clk_unregister(struct clk *clk)
285{
286 mutex_lock(&clock_list_sem);
287 list_del(&clk->sibling);
288 list_del(&clk->node);
289 mutex_unlock(&clock_list_sem);
290}
291EXPORT_SYMBOL_GPL(clk_unregister);
292
293void clk_enable_init_clocks(void)
294{
295 struct clk *clkp;
296
297 list_for_each_entry(clkp, &clock_list, node)
298 if (clkp->flags & CLK_ENABLE_ON_INIT)
299 clk_enable(clkp);
300}
301
302unsigned long clk_get_rate(struct clk *clk)
303{
304 return clk->rate;
305}
306EXPORT_SYMBOL_GPL(clk_get_rate);
307
308int clk_set_rate(struct clk *clk, unsigned long rate)
309{
310 return clk_set_rate_ex(clk, rate, 0);
311}
312EXPORT_SYMBOL_GPL(clk_set_rate);
313
314int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
315{
316 int ret = -EOPNOTSUPP;
317 unsigned long flags;
318
319 spin_lock_irqsave(&clock_lock, flags);
320
321 if (likely(clk->ops && clk->ops->set_rate)) {
322 ret = clk->ops->set_rate(clk, rate, algo_id);
323 if (ret != 0)
324 goto out_unlock;
325 } else {
326 clk->rate = rate;
327 ret = 0;
328 }
329
330 if (clk->ops && clk->ops->recalc)
331 clk->rate = clk->ops->recalc(clk);
332
333 propagate_rate(clk);
334
335out_unlock:
336 spin_unlock_irqrestore(&clock_lock, flags);
337
338 return ret;
339}
340EXPORT_SYMBOL_GPL(clk_set_rate_ex);
341
342int clk_set_parent(struct clk *clk, struct clk *parent)
343{
344 unsigned long flags;
345 int ret = -EINVAL;
346
347 if (!parent || !clk)
348 return ret;
349 if (clk->parent == parent)
350 return 0;
351
352 spin_lock_irqsave(&clock_lock, flags);
353 if (clk->usecount == 0) {
354 if (clk->ops->set_parent)
355 ret = clk->ops->set_parent(clk, parent);
356 else
357 ret = clk_reparent(clk, parent);
358
359 if (ret == 0) {
360 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
361 clk->name, clk->parent->name, clk->rate);
362 if (clk->ops->recalc)
363 clk->rate = clk->ops->recalc(clk);
364 propagate_rate(clk);
365 }
366 } else
367 ret = -EBUSY;
368 spin_unlock_irqrestore(&clock_lock, flags);
369
370 return ret;
371}
372EXPORT_SYMBOL_GPL(clk_set_parent);
373
374struct clk *clk_get_parent(struct clk *clk)
375{
376 return clk->parent;
377}
378EXPORT_SYMBOL_GPL(clk_get_parent);
379
380long clk_round_rate(struct clk *clk, unsigned long rate)
381{
382 if (likely(clk->ops && clk->ops->round_rate)) {
383 unsigned long flags, rounded;
384
385 spin_lock_irqsave(&clock_lock, flags);
386 rounded = clk->ops->round_rate(clk, rate);
387 spin_unlock_irqrestore(&clock_lock, flags);
388
389 return rounded;
390 }
391
392 return clk_get_rate(clk);
393}
394EXPORT_SYMBOL_GPL(clk_round_rate);
395
396#ifdef CONFIG_PM
397static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
398{
399 static pm_message_t prev_state;
400 struct clk *clkp;
401
402 switch (state.event) {
403 case PM_EVENT_ON:
404 /* Resumeing from hibernation */
405 if (prev_state.event != PM_EVENT_FREEZE)
406 break;
407
408 list_for_each_entry(clkp, &clock_list, node) {
409 if (likely(clkp->ops)) {
410 unsigned long rate = clkp->rate;
411
412 if (likely(clkp->ops->set_parent))
413 clkp->ops->set_parent(clkp,
414 clkp->parent);
415 if (likely(clkp->ops->set_rate))
416 clkp->ops->set_rate(clkp,
417 rate, NO_CHANGE);
418 else if (likely(clkp->ops->recalc))
419 clkp->rate = clkp->ops->recalc(clkp);
420 }
421 }
422 break;
423 case PM_EVENT_FREEZE:
424 break;
425 case PM_EVENT_SUSPEND:
426 break;
427 }
428
429 prev_state = state;
430 return 0;
431}
432
433static int clks_sysdev_resume(struct sys_device *dev)
434{
435 return clks_sysdev_suspend(dev, PMSG_ON);
436}
437
438static struct sysdev_class clks_sysdev_class = {
439 .name = "clks",
440};
441
442static struct sysdev_driver clks_sysdev_driver = {
443 .suspend = clks_sysdev_suspend,
444 .resume = clks_sysdev_resume,
445};
446
447static struct sys_device clks_sysdev_dev = {
448 .cls = &clks_sysdev_class,
449};
450
451static int __init clk_sysdev_init(void)
452{
453 sysdev_class_register(&clks_sysdev_class);
454 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
455 sysdev_register(&clks_sysdev_dev);
456
457 return 0;
458}
459subsys_initcall(clk_sysdev_init);
460#endif
461
462/*
463 * debugfs support to trace clock tree hierarchy and attributes
464 */
465static struct dentry *clk_debugfs_root;
466
467static int clk_debugfs_register_one(struct clk *c)
468{
469 int err;
470 struct dentry *d, *child, *child_tmp;
471 struct clk *pa = c->parent;
472 char s[255];
473 char *p = s;
474
475 p += sprintf(p, "%s", c->name);
476 if (c->id >= 0)
477 sprintf(p, ":%d", c->id);
478 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
479 if (!d)
480 return -ENOMEM;
481 c->dentry = d;
482
483 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
484 if (!d) {
485 err = -ENOMEM;
486 goto err_out;
487 }
488 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
489 if (!d) {
490 err = -ENOMEM;
491 goto err_out;
492 }
493 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
494 if (!d) {
495 err = -ENOMEM;
496 goto err_out;
497 }
498 return 0;
499
500err_out:
501 d = c->dentry;
502 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
503 debugfs_remove(child);
504 debugfs_remove(c->dentry);
505 return err;
506}
507
508static int clk_debugfs_register(struct clk *c)
509{
510 int err;
511 struct clk *pa = c->parent;
512
513 if (pa && !pa->dentry) {
514 err = clk_debugfs_register(pa);
515 if (err)
516 return err;
517 }
518
519 if (!c->dentry && c->name) {
520 err = clk_debugfs_register_one(c);
521 if (err)
522 return err;
523 }
524 return 0;
525}
526
527static int __init clk_debugfs_init(void)
528{
529 struct clk *c;
530 struct dentry *d;
531 int err;
532
533 d = debugfs_create_dir("clock", NULL);
534 if (!d)
535 return -ENOMEM;
536 clk_debugfs_root = d;
537
538 list_for_each_entry(c, &clock_list, node) {
539 err = clk_debugfs_register(c);
540 if (err)
541 goto err_out;
542 }
543 return 0;
544err_out:
545 debugfs_remove_recursive(clk_debugfs_root);
546 return err;
547}
548late_initcall(clk_debugfs_init);
diff --git a/include/linux/sh_clk.h b/include/linux/sh_clk.h
index de911451c216..6d7de242be1d 100644
--- a/include/linux/sh_clk.h
+++ b/include/linux/sh_clk.h
@@ -47,13 +47,13 @@ struct clk {
47#define CLK_ENABLE_ON_INIT (1 << 0) 47#define CLK_ENABLE_ON_INIT (1 << 0)
48 48
49/* arch/sh/kernel/cpu/clock.c */ 49/* arch/sh/kernel/cpu/clock.c */
50int clk_init(void);
51unsigned long followparent_recalc(struct clk *); 50unsigned long followparent_recalc(struct clk *);
52void recalculate_root_clocks(void); 51void recalculate_root_clocks(void);
53void propagate_rate(struct clk *); 52void propagate_rate(struct clk *);
54int clk_reparent(struct clk *child, struct clk *parent); 53int clk_reparent(struct clk *child, struct clk *parent);
55int clk_register(struct clk *); 54int clk_register(struct clk *);
56void clk_unregister(struct clk *); 55void clk_unregister(struct clk *);
56void clk_enable_init_clocks(void);
57 57
58/* the exported API, in addition to clk_set_rate */ 58/* the exported API, in addition to clk_set_rate */
59/** 59/**