diff options
author | Magnus Damm <damm@opensource.se> | 2010-05-11 09:29:25 -0400 |
---|---|---|
committer | Paul Mundt <lethal@linux-sh.org> | 2010-05-13 04:39:14 -0400 |
commit | 8b5ee113e1b97097e992a0301d0cac2530b31fc2 (patch) | |
tree | a51016a7cd0cc138354fd221040c883da8731d54 /drivers/sh | |
parent | d28bdf05f72238d626c8d06b61049f6df8d78e70 (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 'drivers/sh')
-rw-r--r-- | drivers/sh/Makefile | 1 | ||||
-rw-r--r-- | drivers/sh/clk.c | 548 |
2 files changed, 549 insertions, 0 deletions
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 @@ | |||
4 | obj-$(CONFIG_SUPERHYWAY) += superhyway/ | 4 | obj-$(CONFIG_SUPERHYWAY) += superhyway/ |
5 | obj-$(CONFIG_MAPLE) += maple/ | 5 | obj-$(CONFIG_MAPLE) += maple/ |
6 | obj-$(CONFIG_GENERIC_GPIO) += pfc.o | 6 | obj-$(CONFIG_GENERIC_GPIO) += pfc.o |
7 | obj-$(CONFIG_SUPERH) += clk.o | ||
7 | obj-y += intc.o | 8 | obj-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 | |||
32 | static LIST_HEAD(clock_list); | ||
33 | static DEFINE_SPINLOCK(clock_lock); | ||
34 | static DEFINE_MUTEX(clock_list_sem); | ||
35 | |||
36 | void 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 | |||
70 | long 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 | |||
110 | int 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 */ | ||
130 | unsigned long followparent_recalc(struct clk *clk) | ||
131 | { | ||
132 | return clk->parent ? clk->parent->rate : 0; | ||
133 | } | ||
134 | |||
135 | int 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 */ | ||
149 | void 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 | |||
161 | static 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 | |||
178 | void 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 | } | ||
189 | EXPORT_SYMBOL_GPL(clk_disable); | ||
190 | |||
191 | static 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; | ||
213 | err: | ||
214 | clk->usecount--; | ||
215 | return ret; | ||
216 | } | ||
217 | |||
218 | int 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 | } | ||
232 | EXPORT_SYMBOL_GPL(clk_enable); | ||
233 | |||
234 | static 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 | */ | ||
243 | void 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 | |||
254 | int 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 | } | ||
282 | EXPORT_SYMBOL_GPL(clk_register); | ||
283 | |||
284 | void 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 | } | ||
291 | EXPORT_SYMBOL_GPL(clk_unregister); | ||
292 | |||
293 | void 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 | |||
302 | unsigned long clk_get_rate(struct clk *clk) | ||
303 | { | ||
304 | return clk->rate; | ||
305 | } | ||
306 | EXPORT_SYMBOL_GPL(clk_get_rate); | ||
307 | |||
308 | int clk_set_rate(struct clk *clk, unsigned long rate) | ||
309 | { | ||
310 | return clk_set_rate_ex(clk, rate, 0); | ||
311 | } | ||
312 | EXPORT_SYMBOL_GPL(clk_set_rate); | ||
313 | |||
314 | int 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 | |||
335 | out_unlock: | ||
336 | spin_unlock_irqrestore(&clock_lock, flags); | ||
337 | |||
338 | return ret; | ||
339 | } | ||
340 | EXPORT_SYMBOL_GPL(clk_set_rate_ex); | ||
341 | |||
342 | int 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 | } | ||
372 | EXPORT_SYMBOL_GPL(clk_set_parent); | ||
373 | |||
374 | struct clk *clk_get_parent(struct clk *clk) | ||
375 | { | ||
376 | return clk->parent; | ||
377 | } | ||
378 | EXPORT_SYMBOL_GPL(clk_get_parent); | ||
379 | |||
380 | long 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 | } | ||
394 | EXPORT_SYMBOL_GPL(clk_round_rate); | ||
395 | |||
396 | #ifdef CONFIG_PM | ||
397 | static 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 | |||
433 | static int clks_sysdev_resume(struct sys_device *dev) | ||
434 | { | ||
435 | return clks_sysdev_suspend(dev, PMSG_ON); | ||
436 | } | ||
437 | |||
438 | static struct sysdev_class clks_sysdev_class = { | ||
439 | .name = "clks", | ||
440 | }; | ||
441 | |||
442 | static struct sysdev_driver clks_sysdev_driver = { | ||
443 | .suspend = clks_sysdev_suspend, | ||
444 | .resume = clks_sysdev_resume, | ||
445 | }; | ||
446 | |||
447 | static struct sys_device clks_sysdev_dev = { | ||
448 | .cls = &clks_sysdev_class, | ||
449 | }; | ||
450 | |||
451 | static 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 | } | ||
459 | subsys_initcall(clk_sysdev_init); | ||
460 | #endif | ||
461 | |||
462 | /* | ||
463 | * debugfs support to trace clock tree hierarchy and attributes | ||
464 | */ | ||
465 | static struct dentry *clk_debugfs_root; | ||
466 | |||
467 | static 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 | |||
500 | err_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 | |||
508 | static 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 | |||
527 | static 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; | ||
544 | err_out: | ||
545 | debugfs_remove_recursive(clk_debugfs_root); | ||
546 | return err; | ||
547 | } | ||
548 | late_initcall(clk_debugfs_init); | ||