blob: 3d017b04784bc76265a4e22841df6e8e7dc1f6b0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +010025#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/semaphore.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);
Juha Yrjoladee45642006-09-25 12:41:47 +0300138 BUG_ON(clk->usecount == 0);
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800139 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000140 arch_clock->clk_disable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000141 spin_unlock_irqrestore(&clockfw_lock, flags);
142}
143EXPORT_SYMBOL(clk_disable);
144
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000145int clk_get_usecount(struct clk *clk)
146{
147 unsigned long flags;
148 int ret = 0;
149
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100150 if (clk == NULL || IS_ERR(clk))
151 return 0;
152
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000153 spin_lock_irqsave(&clockfw_lock, flags);
154 ret = clk->usecount;
155 spin_unlock_irqrestore(&clockfw_lock, flags);
156
157 return ret;
158}
159EXPORT_SYMBOL(clk_get_usecount);
160
161unsigned long clk_get_rate(struct clk *clk)
162{
163 unsigned long flags;
164 unsigned long ret = 0;
165
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100166 if (clk == NULL || IS_ERR(clk))
167 return 0;
168
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000169 spin_lock_irqsave(&clockfw_lock, flags);
170 ret = clk->rate;
171 spin_unlock_irqrestore(&clockfw_lock, flags);
172
173 return ret;
174}
175EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177void clk_put(struct clk *clk)
178{
179 if (clk && !IS_ERR(clk))
180 module_put(clk->owner);
181}
182EXPORT_SYMBOL(clk_put);
183
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000184/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800185 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000186 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188long clk_round_rate(struct clk *clk, unsigned long rate)
189{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000190 unsigned long flags;
191 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100193 if (clk == NULL || IS_ERR(clk))
194 return ret;
195
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000196 spin_lock_irqsave(&clockfw_lock, flags);
197 if (arch_clock->clk_round_rate)
198 ret = arch_clock->clk_round_rate(clk, rate);
199 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203EXPORT_SYMBOL(clk_round_rate);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205int clk_set_rate(struct clk *clk, unsigned long rate)
206{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000207 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100208 int ret = -EINVAL;
209
210 if (clk == NULL || IS_ERR(clk))
211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000213 spin_lock_irqsave(&clockfw_lock, flags);
214 if (arch_clock->clk_set_rate)
215 ret = arch_clock->clk_set_rate(clk, rate);
216 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 return ret;
219}
220EXPORT_SYMBOL(clk_set_rate);
221
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000222int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000224 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100225 int ret = -EINVAL;
226
227 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000230 spin_lock_irqsave(&clockfw_lock, flags);
231 if (arch_clock->clk_set_parent)
232 ret = arch_clock->clk_set_parent(clk, parent);
233 spin_unlock_irqrestore(&clockfw_lock, flags);
234
235 return ret;
236}
237EXPORT_SYMBOL(clk_set_parent);
238
239struct clk *clk_get_parent(struct clk *clk)
240{
241 unsigned long flags;
242 struct clk * ret = NULL;
243
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100244 if (clk == NULL || IS_ERR(clk))
245 return ret;
246
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000247 spin_lock_irqsave(&clockfw_lock, flags);
248 if (arch_clock->clk_get_parent)
249 ret = arch_clock->clk_get_parent(clk);
250 spin_unlock_irqrestore(&clockfw_lock, flags);
251
252 return ret;
253}
254EXPORT_SYMBOL(clk_get_parent);
255
256/*-------------------------------------------------------------------------
257 * OMAP specific clock functions shared between omap1 and omap2
258 *-------------------------------------------------------------------------*/
259
260unsigned int __initdata mpurate;
261
262/*
263 * By default we use the rate set by the bootloader.
264 * You can override this with mpurate= cmdline option.
265 */
266static int __init omap_clk_setup(char *str)
267{
268 get_option(&str, &mpurate);
269
270 if (!mpurate)
271 return 1;
272
273 if (mpurate < 1000)
274 mpurate *= 1000000;
275
276 return 1;
277}
278__setup("mpurate=", omap_clk_setup);
279
280/* Used for clocks that always have same value as the parent clock */
281void followparent_recalc(struct clk *clk)
282{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100283 if (clk == NULL || IS_ERR(clk))
284 return;
285
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000286 clk->rate = clk->parent->rate;
287}
288
289/* Propagate rate to children */
290void propagate_rate(struct clk * tclk)
291{
292 struct clk *clkp;
293
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100294 if (tclk == NULL || IS_ERR(tclk))
295 return;
296
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000297 list_for_each_entry(clkp, &clocks, node) {
298 if (likely(clkp->parent != tclk))
299 continue;
300 if (likely((u32)clkp->recalc))
301 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305int clk_register(struct clk *clk)
306{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100307 if (clk == NULL || IS_ERR(clk))
308 return -EINVAL;
309
Arjan van de Ven00431702006-01-12 18:42:23 +0000310 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 list_add(&clk->node, &clocks);
312 if (clk->init)
313 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000314 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318EXPORT_SYMBOL(clk_register);
319
320void clk_unregister(struct clk *clk)
321{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100322 if (clk == NULL || IS_ERR(clk))
323 return;
324
Arjan van de Ven00431702006-01-12 18:42:23 +0000325 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000327 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329EXPORT_SYMBOL(clk_unregister);
330
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000331void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100332{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000333 unsigned long flags;
334
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100335 if (clk == NULL || IS_ERR(clk))
336 return;
337
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000338 spin_lock_irqsave(&clockfw_lock, flags);
339 if (arch_clock->clk_deny_idle)
340 arch_clock->clk_deny_idle(clk);
341 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100342}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000343EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000345void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000347 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100349 if (clk == NULL || IS_ERR(clk))
350 return;
351
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000352 spin_lock_irqsave(&clockfw_lock, flags);
353 if (arch_clock->clk_allow_idle)
354 arch_clock->clk_allow_idle(clk);
355 spin_unlock_irqrestore(&clockfw_lock, flags);
356}
357EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100358
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000359/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300361#ifdef CONFIG_OMAP_RESET_CLOCKS
362/*
363 * Disable any unused clocks left on by the bootloader
364 */
365static int __init clk_disable_unused(void)
366{
367 struct clk *ck;
368 unsigned long flags;
369
370 list_for_each_entry(ck, &clocks, node) {
371 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
372 ck->enable_reg == 0)
373 continue;
374
375 spin_lock_irqsave(&clockfw_lock, flags);
376 if (arch_clock->clk_disable_unused)
377 arch_clock->clk_disable_unused(ck);
378 spin_unlock_irqrestore(&clockfw_lock, flags);
379 }
380
381 return 0;
382}
383late_initcall(clk_disable_unused);
384#endif
385
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000386int __init clk_init(struct clk_functions * custom_clocks)
387{
388 if (!custom_clocks) {
389 printk(KERN_ERR "No custom clock functions registered\n");
390 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000393 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return 0;
396}