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