]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/clock.c
ARM: OMAP: CLKFW: Initial debugfs support for omap clock framework
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / clock.c
1 /*
2  *  linux/arch/arm/plat-omap/clock.c
3  *
4  *  Copyright (C) 2004 - 2008 Nokia corporation
5  *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6  *
7  *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/version.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/clk.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/debugfs.h>
25
26 #include <asm/io.h>
27 #include <asm/semaphore.h>
28
29 #include <asm/arch/clock.h>
30
31 static LIST_HEAD(clocks);
32 static DEFINE_MUTEX(clocks_mutex);
33 static DEFINE_SPINLOCK(clockfw_lock);
34
35 static struct clk_functions *arch_clock;
36
37 #ifdef CONFIG_PM_DEBUG
38
39 static void print_parents(struct clk *clk)
40 {
41         struct clk *p;
42         int printed = 0;
43
44         list_for_each_entry(p, &clocks, node) {
45                 if (p->parent == clk && p->usecount) {
46                         if (!clk->usecount && !printed) {
47                                 printk("MISMATCH: %s\n", clk->name);
48                                 printed = 1;
49                         }
50                         printk("\t%-15s\n", p->name);
51                 }
52         }
53 }
54
55 void clk_print_usecounts(void)
56 {
57         unsigned long flags;
58         struct clk *p;
59
60         spin_lock_irqsave(&clockfw_lock, flags);
61         list_for_each_entry(p, &clocks, node) {
62                 if (p->usecount)
63                         printk("%-15s: %d\n", p->name, p->usecount);
64                 print_parents(p);
65
66         }
67         spin_unlock_irqrestore(&clockfw_lock, flags);
68 }
69
70 #endif
71
72 /*-------------------------------------------------------------------------
73  * Standard clock functions defined in include/linux/clk.h
74  *-------------------------------------------------------------------------*/
75
76 /*
77  * Returns a clock. Note that we first try to use device id on the bus
78  * and clock name. If this fails, we try to use clock name only.
79  */
80 struct clk * clk_get(struct device *dev, const char *id)
81 {
82         struct clk *p, *clk = ERR_PTR(-ENOENT);
83         int idno;
84
85         if (dev == NULL || dev->bus != &platform_bus_type)
86                 idno = -1;
87         else
88                 idno = to_platform_device(dev)->id;
89
90         mutex_lock(&clocks_mutex);
91
92         list_for_each_entry(p, &clocks, node) {
93                 if (p->id == idno &&
94                     strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
95                         clk = p;
96                         goto found;
97                 }
98         }
99
100         list_for_each_entry(p, &clocks, node) {
101                 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
102                         clk = p;
103                         break;
104                 }
105         }
106
107 found:
108         mutex_unlock(&clocks_mutex);
109
110         return clk;
111 }
112 EXPORT_SYMBOL(clk_get);
113
114 int clk_enable(struct clk *clk)
115 {
116         unsigned long flags;
117         int ret = 0;
118
119         if (clk == NULL || IS_ERR(clk))
120                 return -EINVAL;
121
122         spin_lock_irqsave(&clockfw_lock, flags);
123         if (arch_clock->clk_enable)
124                 ret = arch_clock->clk_enable(clk);
125         spin_unlock_irqrestore(&clockfw_lock, flags);
126
127         return ret;
128 }
129 EXPORT_SYMBOL(clk_enable);
130
131 void clk_disable(struct clk *clk)
132 {
133         unsigned long flags;
134
135         if (clk == NULL || IS_ERR(clk))
136                 return;
137
138         spin_lock_irqsave(&clockfw_lock, flags);
139         if (clk->usecount == 0) {
140                 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
141                        clk->name);
142                 WARN_ON(1);
143                 goto out;
144         }
145
146         if (arch_clock->clk_disable)
147                 arch_clock->clk_disable(clk);
148
149 out:
150         spin_unlock_irqrestore(&clockfw_lock, flags);
151 }
152 EXPORT_SYMBOL(clk_disable);
153
154 int clk_get_usecount(struct clk *clk)
155 {
156         unsigned long flags;
157         int ret = 0;
158
159         if (clk == NULL || IS_ERR(clk))
160                 return 0;
161
162         spin_lock_irqsave(&clockfw_lock, flags);
163         ret = clk->usecount;
164         spin_unlock_irqrestore(&clockfw_lock, flags);
165
166         return ret;
167 }
168 EXPORT_SYMBOL(clk_get_usecount);
169
170 unsigned long clk_get_rate(struct clk *clk)
171 {
172         unsigned long flags;
173         unsigned long ret = 0;
174
175         if (clk == NULL || IS_ERR(clk))
176                 return 0;
177
178         spin_lock_irqsave(&clockfw_lock, flags);
179         ret = clk->rate;
180         spin_unlock_irqrestore(&clockfw_lock, flags);
181
182         return ret;
183 }
184 EXPORT_SYMBOL(clk_get_rate);
185
186 void clk_put(struct clk *clk)
187 {
188         if (clk && !IS_ERR(clk))
189                 module_put(clk->owner);
190 }
191 EXPORT_SYMBOL(clk_put);
192
193 /*-------------------------------------------------------------------------
194  * Optional clock functions defined in include/linux/clk.h
195  *-------------------------------------------------------------------------*/
196
197 long clk_round_rate(struct clk *clk, unsigned long rate)
198 {
199         unsigned long flags;
200         long ret = 0;
201
202         if (clk == NULL || IS_ERR(clk))
203                 return ret;
204
205         spin_lock_irqsave(&clockfw_lock, flags);
206         if (arch_clock->clk_round_rate)
207                 ret = arch_clock->clk_round_rate(clk, rate);
208         spin_unlock_irqrestore(&clockfw_lock, flags);
209
210         return ret;
211 }
212 EXPORT_SYMBOL(clk_round_rate);
213
214 int clk_set_rate(struct clk *clk, unsigned long rate)
215 {
216         unsigned long flags;
217         int ret = -EINVAL;
218
219         if (clk == NULL || IS_ERR(clk))
220                 return ret;
221
222         spin_lock_irqsave(&clockfw_lock, flags);
223         if (arch_clock->clk_set_rate)
224                 ret = arch_clock->clk_set_rate(clk, rate);
225         spin_unlock_irqrestore(&clockfw_lock, flags);
226
227         return ret;
228 }
229 EXPORT_SYMBOL(clk_set_rate);
230
231 int clk_set_parent(struct clk *clk, struct clk *parent)
232 {
233         unsigned long flags;
234         int ret = -EINVAL;
235
236         if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
237                 return ret;
238
239         spin_lock_irqsave(&clockfw_lock, flags);
240         if (arch_clock->clk_set_parent)
241                 ret =  arch_clock->clk_set_parent(clk, parent);
242         spin_unlock_irqrestore(&clockfw_lock, flags);
243
244         return ret;
245 }
246 EXPORT_SYMBOL(clk_set_parent);
247
248 struct clk *clk_get_parent(struct clk *clk)
249 {
250         unsigned long flags;
251         struct clk * ret = NULL;
252
253         if (clk == NULL || IS_ERR(clk))
254                 return ret;
255
256         spin_lock_irqsave(&clockfw_lock, flags);
257         if (arch_clock->clk_get_parent)
258                 ret = arch_clock->clk_get_parent(clk);
259         spin_unlock_irqrestore(&clockfw_lock, flags);
260
261         return ret;
262 }
263 EXPORT_SYMBOL(clk_get_parent);
264
265 /*-------------------------------------------------------------------------
266  * OMAP specific clock functions shared between omap1 and omap2
267  *-------------------------------------------------------------------------*/
268
269 unsigned int __initdata mpurate;
270
271 /*
272  * By default we use the rate set by the bootloader.
273  * You can override this with mpurate= cmdline option.
274  */
275 static int __init omap_clk_setup(char *str)
276 {
277         get_option(&str, &mpurate);
278
279         if (!mpurate)
280                 return 1;
281
282         if (mpurate < 1000)
283                 mpurate *= 1000000;
284
285         return 1;
286 }
287 __setup("mpurate=", omap_clk_setup);
288
289 /* Used for clocks that always have same value as the parent clock */
290 void followparent_recalc(struct clk *clk)
291 {
292         if (clk == NULL || IS_ERR(clk))
293                 return;
294
295         clk->rate = clk->parent->rate;
296         if (unlikely(clk->flags & RATE_PROPAGATES))
297                 propagate_rate(clk);
298 }
299
300 /* Propagate rate to children */
301 void propagate_rate(struct clk * tclk)
302 {
303         struct clk *clkp;
304
305         if (tclk == NULL || IS_ERR(tclk))
306                 return;
307
308         list_for_each_entry(clkp, &clocks, node) {
309                 if (likely(clkp->parent != tclk))
310                         continue;
311                 if (likely((u32)clkp->recalc))
312                         clkp->recalc(clkp);
313         }
314 }
315
316 /**
317  * recalculate_root_clocks - recalculate and propagate all root clocks
318  *
319  * Recalculates all root clocks (clocks with no parent), which if the
320  * clock's .recalc is set correctly, should also propagate their rates.
321  * Called at init.
322  */
323 void recalculate_root_clocks(void)
324 {
325         struct clk *clkp;
326
327         list_for_each_entry(clkp, &clocks, node) {
328                 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
329                         clkp->recalc(clkp);
330         }
331 }
332
333 int clk_register(struct clk *clk)
334 {
335         if (clk == NULL || IS_ERR(clk))
336                 return -EINVAL;
337
338         mutex_lock(&clocks_mutex);
339         list_add(&clk->node, &clocks);
340         if (clk->init)
341                 clk->init(clk);
342         mutex_unlock(&clocks_mutex);
343
344         return 0;
345 }
346 EXPORT_SYMBOL(clk_register);
347
348 void clk_unregister(struct clk *clk)
349 {
350         if (clk == NULL || IS_ERR(clk))
351                 return;
352
353         mutex_lock(&clocks_mutex);
354         list_del(&clk->node);
355         mutex_unlock(&clocks_mutex);
356 }
357 EXPORT_SYMBOL(clk_unregister);
358
359 void clk_deny_idle(struct clk *clk)
360 {
361         unsigned long flags;
362
363         if (clk == NULL || IS_ERR(clk))
364                 return;
365
366         spin_lock_irqsave(&clockfw_lock, flags);
367         if (arch_clock->clk_deny_idle)
368                 arch_clock->clk_deny_idle(clk);
369         spin_unlock_irqrestore(&clockfw_lock, flags);
370 }
371 EXPORT_SYMBOL(clk_deny_idle);
372
373 void clk_allow_idle(struct clk *clk)
374 {
375         unsigned long flags;
376
377         if (clk == NULL || IS_ERR(clk))
378                 return;
379
380         spin_lock_irqsave(&clockfw_lock, flags);
381         if (arch_clock->clk_allow_idle)
382                 arch_clock->clk_allow_idle(clk);
383         spin_unlock_irqrestore(&clockfw_lock, flags);
384 }
385 EXPORT_SYMBOL(clk_allow_idle);
386
387 void clk_enable_init_clocks(void)
388 {
389         struct clk *clkp;
390
391         list_for_each_entry(clkp, &clocks, node) {
392                 if (clkp->flags & ENABLE_ON_INIT)
393                         clk_enable(clkp);
394         }
395 }
396 EXPORT_SYMBOL(clk_enable_init_clocks);
397
398 #ifdef CONFIG_CPU_FREQ
399 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
400 {
401         unsigned long flags;
402
403         spin_lock_irqsave(&clockfw_lock, flags);
404         if (arch_clock->clk_init_cpufreq_table)
405                 arch_clock->clk_init_cpufreq_table(table);
406         spin_unlock_irqrestore(&clockfw_lock, flags);
407 }
408 EXPORT_SYMBOL(clk_init_cpufreq_table);
409 #endif
410
411 /*-------------------------------------------------------------------------*/
412
413 #ifdef CONFIG_OMAP_RESET_CLOCKS
414 /*
415  * Disable any unused clocks left on by the bootloader
416  */
417 static int __init clk_disable_unused(void)
418 {
419         struct clk *ck;
420         unsigned long flags;
421
422         list_for_each_entry(ck, &clocks, node) {
423                 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
424                         ck->enable_reg == 0)
425                         continue;
426
427                 spin_lock_irqsave(&clockfw_lock, flags);
428                 if (arch_clock->clk_disable_unused)
429                         arch_clock->clk_disable_unused(ck);
430                 spin_unlock_irqrestore(&clockfw_lock, flags);
431         }
432
433         return 0;
434 }
435 late_initcall(clk_disable_unused);
436 #endif
437
438 int __init clk_init(struct clk_functions * custom_clocks)
439 {
440         if (!custom_clocks) {
441                 printk(KERN_ERR "No custom clock functions registered\n");
442                 BUG();
443         }
444
445         arch_clock = custom_clocks;
446
447         return 0;
448 }
449
450 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_PROC_FS)
451 #include <linux/proc_fs.h>
452 #include <linux/seq_file.h>
453
454 static void *omap_ck_start(struct seq_file *m, loff_t *pos)
455 {
456         return *pos < 1 ? (void *)1 : NULL;
457 }
458
459 static void *omap_ck_next(struct seq_file *m, void *v, loff_t *pos)
460 {
461         ++*pos;
462         return NULL;
463 }
464
465 static void omap_ck_stop(struct seq_file *m, void *v)
466 {
467 }
468
469 int omap_ck_show(struct seq_file *m, void *v)
470 {
471         struct clk *cp;
472
473         list_for_each_entry(cp, &clocks, node)
474                 seq_printf(m,"%s %ld %d\n", cp->name, cp->rate, cp->usecount);
475
476         return 0;
477 }
478
479 static struct seq_operations omap_ck_op = {
480         .start =        omap_ck_start,
481         .next =         omap_ck_next,
482         .stop =         omap_ck_stop,
483         .show =         omap_ck_show
484 };
485
486 static int omap_ck_open(struct inode *inode, struct file *file)
487 {
488         return seq_open(file, &omap_ck_op);
489 }
490
491 static struct file_operations proc_omap_ck_operations = {
492         .open           = omap_ck_open,
493         .read           = seq_read,
494         .llseek         = seq_lseek,
495         .release        = seq_release,
496 };
497
498 int __init omap_ck_init(void)
499 {
500         struct proc_dir_entry *entry;
501
502         entry = create_proc_entry("omap_clocks", 0, NULL);
503         if (entry)
504                 entry->proc_fops = &proc_omap_ck_operations;
505         return 0;
506
507 }
508 __initcall(omap_ck_init);
509 #endif
510
511 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
512 /*
513  *      debugfs support to trace clock tree hierarchy and attributes
514  */
515 static struct dentry *clk_debugfs_root;
516
517 static int clk_debugfs_register_one(struct clk *c)
518 {
519         int err;
520         struct dentry *d, *child;
521         struct clk *pa = c->parent;
522         char s[255];
523         char *p = s;
524
525         p += sprintf(p, "%s", c->name);
526         if (c->id != 0)
527                 sprintf(p, ":%d", c->id);
528         d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
529         if (IS_ERR(d))
530                 return PTR_ERR(d);
531         c->dent = d;
532
533         d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
534         if (IS_ERR(d)) {
535                 err = PTR_ERR(d);
536                 goto err_out;
537         }
538         d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
539         if (IS_ERR(d)) {
540                 err = PTR_ERR(d);
541                 goto err_out;
542         }
543         d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
544         if (IS_ERR(d)) {
545                 err = PTR_ERR(d);
546                 goto err_out;
547         }
548         return 0;
549
550 err_out:
551         d = c->dent;
552         list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
553                 debugfs_remove(child);
554         debugfs_remove(c->dent);
555         return err;
556 }
557
558 static int clk_debugfs_register(struct clk *c)
559 {
560         int err;
561         struct clk *pa = c->parent;
562
563         if (pa && !pa->dent) {
564                 err = clk_debugfs_register(pa);
565                 if (err)
566                         return err;
567         }
568
569         if (!c->dent) {
570                 err = clk_debugfs_register_one(c);
571                 if (err)
572                         return err;
573         }
574         return 0;
575 }
576
577 static int __init clk_debugfs_init(void)
578 {
579         struct clk *c;
580         struct dentry *d;
581         int err;
582
583         d = debugfs_create_dir("clock", NULL);
584         if (IS_ERR(d))
585                 return PTR_ERR(d);
586         clk_debugfs_root = d;
587
588         list_for_each_entry(c, &clocks, node) {
589                 err = clk_debugfs_register(c);
590                 if (err)
591                         goto err_out;
592         }
593         return 0;
594 err_out:
595         debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
596         return err;
597 }
598 late_initcall(clk_debugfs_init);
599
600 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */