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