aboutsummaryrefslogtreecommitdiffstats
path: root/arch/sh/kernel/cpu/clock.c
diff options
context:
space:
mode:
authorMagnus Damm <damm@opensource.se>2010-05-11 09:29:25 -0400
committerPaul Mundt <lethal@linux-sh.org>2010-05-13 04:39:14 -0400
commit8b5ee113e1b97097e992a0301d0cac2530b31fc2 (patch)
treea51016a7cd0cc138354fd221040c883da8731d54 /arch/sh/kernel/cpu/clock.c
parentd28bdf05f72238d626c8d06b61049f6df8d78e70 (diff)
sh: move sh clock.c contents to drivers/sh/clk.
This patch is V2 of the SH clock framework move from arch/sh/kernel/cpu/clock.c to drivers/sh/clk.c. All code except the following functions are moved: clk_init(), clk_get() and clk_put(). The init function is still kept in clock.c since it depends on the SH-specific machvec implementation. The symbols clk_get() and clk_put() already exist in the common ARM clkdev code, those symbols are left in the SH tree to avoid duplicating them for SH-Mobile ARM. Signed-off-by: Magnus Damm <damm@opensource.se> Signed-off-by: Paul Mundt <lethal@linux-sh.org>
Diffstat (limited to 'arch/sh/kernel/cpu/clock.c')
-rw-r--r--arch/sh/kernel/cpu/clock.c543
1 files changed, 8 insertions, 535 deletions
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);