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