blob: 2db5580048d8d254ddc12c3af1fb79152c355d85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tony Lindgrenb9158552005-07-10 19:58:14 +01002 * linux/arch/arm/plat-omap/clock.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00004 * Copyright (C) 2004 - 2005 Nokia corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6 *
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00007 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000013#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000015#include <linux/init.h>
16#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/string.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000022#include <linux/mutex.h>
Tony Lindgrenb824efa2006-04-02 17:46:20 +010023#include <linux/platform_device.h>
Russell Kingb851cb22008-05-22 16:38:50 +010024#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +010026#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000028#include <asm/arch/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Juha Yrjola7df34502006-06-26 16:16:22 -070030static LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000031static DEFINE_MUTEX(clocks_mutex);
Juha Yrjola7df34502006-06-26 16:16:22 -070032static DEFINE_SPINLOCK(clockfw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000034static struct clk_functions *arch_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Juha Yrjola0ce33562006-12-06 17:13:49 -080036#ifdef CONFIG_PM_DEBUG
37
38static void print_parents(struct clk *clk)
39{
40 struct clk *p;
41 int printed = 0;
42
43 list_for_each_entry(p, &clocks, node) {
44 if (p->parent == clk && p->usecount) {
45 if (!clk->usecount && !printed) {
46 printk("MISMATCH: %s\n", clk->name);
47 printed = 1;
48 }
49 printk("\t%-15s\n", p->name);
50 }
51 }
52}
53
54void clk_print_usecounts(void)
55{
56 unsigned long flags;
57 struct clk *p;
58
59 spin_lock_irqsave(&clockfw_lock, flags);
60 list_for_each_entry(p, &clocks, node) {
61 if (p->usecount)
62 printk("%-15s: %d\n", p->name, p->usecount);
63 print_parents(p);
64
65 }
66 spin_unlock_irqrestore(&clockfw_lock, flags);
67}
68
69#endif
70
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000071/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -080072 * Standard clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000073 *-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tony Lindgrenb824efa2006-04-02 17:46:20 +010075/*
76 * Returns a clock. Note that we first try to use device id on the bus
77 * and clock name. If this fails, we try to use clock name only.
78 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000079struct clk * clk_get(struct device *dev, const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct clk *p, *clk = ERR_PTR(-ENOENT);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010082 int idno;
83
84 if (dev == NULL || dev->bus != &platform_bus_type)
85 idno = -1;
86 else
87 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Arjan van de Ven00431702006-01-12 18:42:23 +000089 mutex_lock(&clocks_mutex);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010090
91 list_for_each_entry(p, &clocks, node) {
92 if (p->id == idno &&
93 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
94 clk = p;
Tony Lindgren67d4d832006-04-09 22:21:05 +010095 goto found;
Tony Lindgrenb824efa2006-04-02 17:46:20 +010096 }
97 }
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 list_for_each_entry(p, &clocks, node) {
100 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
101 clk = p;
102 break;
103 }
104 }
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100105
Tony Lindgren67d4d832006-04-09 22:21:05 +0100106found:
Arjan van de Ven00431702006-01-12 18:42:23 +0000107 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 return clk;
110}
111EXPORT_SYMBOL(clk_get);
112
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000113int clk_enable(struct clk *clk)
114{
115 unsigned long flags;
116 int ret = 0;
117
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100118 if (clk == NULL || IS_ERR(clk))
119 return -EINVAL;
120
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000121 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800122 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000123 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000124 spin_unlock_irqrestore(&clockfw_lock, flags);
125
126 return ret;
127}
128EXPORT_SYMBOL(clk_enable);
129
130void clk_disable(struct clk *clk)
131{
132 unsigned long flags;
133
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100134 if (clk == NULL || IS_ERR(clk))
135 return;
136
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000137 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700138 if (clk->usecount == 0) {
139 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
140 clk->name);
141 WARN_ON(1);
142 goto out;
143 }
144
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800145 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000146 arch_clock->clk_disable(clk);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700147
148out:
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000149 spin_unlock_irqrestore(&clockfw_lock, flags);
150}
151EXPORT_SYMBOL(clk_disable);
152
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000153int clk_get_usecount(struct clk *clk)
154{
155 unsigned long flags;
156 int ret = 0;
157
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100158 if (clk == NULL || IS_ERR(clk))
159 return 0;
160
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000161 spin_lock_irqsave(&clockfw_lock, flags);
162 ret = clk->usecount;
163 spin_unlock_irqrestore(&clockfw_lock, flags);
164
165 return ret;
166}
167EXPORT_SYMBOL(clk_get_usecount);
168
169unsigned long clk_get_rate(struct clk *clk)
170{
171 unsigned long flags;
172 unsigned long ret = 0;
173
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100174 if (clk == NULL || IS_ERR(clk))
175 return 0;
176
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000177 spin_lock_irqsave(&clockfw_lock, flags);
178 ret = clk->rate;
179 spin_unlock_irqrestore(&clockfw_lock, flags);
180
181 return ret;
182}
183EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185void clk_put(struct clk *clk)
186{
187 if (clk && !IS_ERR(clk))
188 module_put(clk->owner);
189}
190EXPORT_SYMBOL(clk_put);
191
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000192/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800193 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000194 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196long clk_round_rate(struct clk *clk, unsigned long rate)
197{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000198 unsigned long flags;
199 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100201 if (clk == NULL || IS_ERR(clk))
202 return ret;
203
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000204 spin_lock_irqsave(&clockfw_lock, flags);
205 if (arch_clock->clk_round_rate)
206 ret = arch_clock->clk_round_rate(clk, rate);
207 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211EXPORT_SYMBOL(clk_round_rate);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213int clk_set_rate(struct clk *clk, unsigned long rate)
214{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000215 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100216 int ret = -EINVAL;
217
218 if (clk == NULL || IS_ERR(clk))
219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000221 spin_lock_irqsave(&clockfw_lock, flags);
222 if (arch_clock->clk_set_rate)
223 ret = arch_clock->clk_set_rate(clk, rate);
224 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return ret;
227}
228EXPORT_SYMBOL(clk_set_rate);
229
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000230int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000232 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100233 int ret = -EINVAL;
234
235 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000238 spin_lock_irqsave(&clockfw_lock, flags);
239 if (arch_clock->clk_set_parent)
240 ret = arch_clock->clk_set_parent(clk, parent);
241 spin_unlock_irqrestore(&clockfw_lock, flags);
242
243 return ret;
244}
245EXPORT_SYMBOL(clk_set_parent);
246
247struct clk *clk_get_parent(struct clk *clk)
248{
249 unsigned long flags;
250 struct clk * ret = NULL;
251
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100252 if (clk == NULL || IS_ERR(clk))
253 return ret;
254
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000255 spin_lock_irqsave(&clockfw_lock, flags);
256 if (arch_clock->clk_get_parent)
257 ret = arch_clock->clk_get_parent(clk);
258 spin_unlock_irqrestore(&clockfw_lock, flags);
259
260 return ret;
261}
262EXPORT_SYMBOL(clk_get_parent);
263
264/*-------------------------------------------------------------------------
265 * OMAP specific clock functions shared between omap1 and omap2
266 *-------------------------------------------------------------------------*/
267
268unsigned int __initdata mpurate;
269
270/*
271 * By default we use the rate set by the bootloader.
272 * You can override this with mpurate= cmdline option.
273 */
274static int __init omap_clk_setup(char *str)
275{
276 get_option(&str, &mpurate);
277
278 if (!mpurate)
279 return 1;
280
281 if (mpurate < 1000)
282 mpurate *= 1000000;
283
284 return 1;
285}
286__setup("mpurate=", omap_clk_setup);
287
288/* Used for clocks that always have same value as the parent clock */
289void followparent_recalc(struct clk *clk)
290{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100291 if (clk == NULL || IS_ERR(clk))
292 return;
293
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000294 clk->rate = clk->parent->rate;
Imre Deakb1465bf2007-03-06 03:52:01 -0800295 if (unlikely(clk->flags & RATE_PROPAGATES))
296 propagate_rate(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000297}
298
299/* Propagate rate to children */
300void propagate_rate(struct clk * tclk)
301{
302 struct clk *clkp;
303
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100304 if (tclk == NULL || IS_ERR(tclk))
305 return;
306
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000307 list_for_each_entry(clkp, &clocks, node) {
308 if (likely(clkp->parent != tclk))
309 continue;
310 if (likely((u32)clkp->recalc))
311 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200315/**
316 * recalculate_root_clocks - recalculate and propagate all root clocks
317 *
318 * Recalculates all root clocks (clocks with no parent), which if the
319 * clock's .recalc is set correctly, should also propagate their rates.
320 * Called at init.
321 */
322void recalculate_root_clocks(void)
323{
324 struct clk *clkp;
325
326 list_for_each_entry(clkp, &clocks, node) {
327 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
328 clkp->recalc(clkp);
329 }
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332int clk_register(struct clk *clk)
333{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100334 if (clk == NULL || IS_ERR(clk))
335 return -EINVAL;
336
Arjan van de Ven00431702006-01-12 18:42:23 +0000337 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_add(&clk->node, &clocks);
339 if (clk->init)
340 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000341 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345EXPORT_SYMBOL(clk_register);
346
347void clk_unregister(struct clk *clk)
348{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100349 if (clk == NULL || IS_ERR(clk))
350 return;
351
Arjan van de Ven00431702006-01-12 18:42:23 +0000352 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000354 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356EXPORT_SYMBOL(clk_unregister);
357
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000358void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100359{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000360 unsigned long flags;
361
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100362 if (clk == NULL || IS_ERR(clk))
363 return;
364
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000365 spin_lock_irqsave(&clockfw_lock, flags);
366 if (arch_clock->clk_deny_idle)
367 arch_clock->clk_deny_idle(clk);
368 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100369}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000370EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000372void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000374 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100376 if (clk == NULL || IS_ERR(clk))
377 return;
378
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000379 spin_lock_irqsave(&clockfw_lock, flags);
380 if (arch_clock->clk_allow_idle)
381 arch_clock->clk_allow_idle(clk);
382 spin_unlock_irqrestore(&clockfw_lock, flags);
383}
384EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100385
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200386void clk_enable_init_clocks(void)
387{
388 struct clk *clkp;
389
390 list_for_each_entry(clkp, &clocks, node) {
391 if (clkp->flags & ENABLE_ON_INIT)
392 clk_enable(clkp);
393 }
394}
395EXPORT_SYMBOL(clk_enable_init_clocks);
396
397#ifdef CONFIG_CPU_FREQ
398void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
399{
400 unsigned long flags;
401
402 spin_lock_irqsave(&clockfw_lock, flags);
403 if (arch_clock->clk_init_cpufreq_table)
404 arch_clock->clk_init_cpufreq_table(table);
405 spin_unlock_irqrestore(&clockfw_lock, flags);
406}
407EXPORT_SYMBOL(clk_init_cpufreq_table);
408#endif
409
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000410/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300412#ifdef CONFIG_OMAP_RESET_CLOCKS
413/*
414 * Disable any unused clocks left on by the bootloader
415 */
416static int __init clk_disable_unused(void)
417{
418 struct clk *ck;
419 unsigned long flags;
420
421 list_for_each_entry(ck, &clocks, node) {
422 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
423 ck->enable_reg == 0)
424 continue;
425
426 spin_lock_irqsave(&clockfw_lock, flags);
427 if (arch_clock->clk_disable_unused)
428 arch_clock->clk_disable_unused(ck);
429 spin_unlock_irqrestore(&clockfw_lock, flags);
430 }
431
432 return 0;
433}
434late_initcall(clk_disable_unused);
435#endif
436
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000437int __init clk_init(struct clk_functions * custom_clocks)
438{
439 if (!custom_clocks) {
440 printk(KERN_ERR "No custom clock functions registered\n");
441 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000444 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 return 0;
447}
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200448