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