blob: 32ec04c58bcd4fb66e4647d63c5cab6fbbb1e758 [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>
14#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000016#include <linux/init.h>
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/list.h>
19#include <linux/errno.h>
20#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021#include <linux/string.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000023#include <linux/mutex.h>
Tony Lindgrenb824efa2006-04-02 17:46:20 +010024#include <linux/platform_device.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#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000029#include <asm/arch/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000031LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000032static DEFINE_MUTEX(clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000033DEFINE_SPINLOCK(clockfw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000035static struct clk_functions *arch_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000037/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -080038 * Standard clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000039 *-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Tony Lindgrenb824efa2006-04-02 17:46:20 +010041/*
42 * Returns a clock. Note that we first try to use device id on the bus
43 * and clock name. If this fails, we try to use clock name only.
44 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000045struct clk * clk_get(struct device *dev, const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct clk *p, *clk = ERR_PTR(-ENOENT);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010048 int idno;
49
50 if (dev == NULL || dev->bus != &platform_bus_type)
51 idno = -1;
52 else
53 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Arjan van de Ven00431702006-01-12 18:42:23 +000055 mutex_lock(&clocks_mutex);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010056
57 list_for_each_entry(p, &clocks, node) {
58 if (p->id == idno &&
59 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
60 clk = p;
Tony Lindgren67d4d832006-04-09 22:21:05 +010061 goto found;
Tony Lindgrenb824efa2006-04-02 17:46:20 +010062 }
63 }
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 list_for_each_entry(p, &clocks, node) {
66 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
67 clk = p;
68 break;
69 }
70 }
Tony Lindgrenb824efa2006-04-02 17:46:20 +010071
Tony Lindgren67d4d832006-04-09 22:21:05 +010072found:
Arjan van de Ven00431702006-01-12 18:42:23 +000073 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 return clk;
76}
77EXPORT_SYMBOL(clk_get);
78
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000079int clk_enable(struct clk *clk)
80{
81 unsigned long flags;
82 int ret = 0;
83
Tony Lindgrenb824efa2006-04-02 17:46:20 +010084 if (clk == NULL || IS_ERR(clk))
85 return -EINVAL;
86
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000087 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080088 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000089 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000090 spin_unlock_irqrestore(&clockfw_lock, flags);
91
92 return ret;
93}
94EXPORT_SYMBOL(clk_enable);
95
96void clk_disable(struct clk *clk)
97{
98 unsigned long flags;
99
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100100 if (clk == NULL || IS_ERR(clk))
101 return;
102
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000103 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800104 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000105 arch_clock->clk_disable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000106 spin_unlock_irqrestore(&clockfw_lock, flags);
107}
108EXPORT_SYMBOL(clk_disable);
109
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000110int clk_get_usecount(struct clk *clk)
111{
112 unsigned long flags;
113 int ret = 0;
114
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100115 if (clk == NULL || IS_ERR(clk))
116 return 0;
117
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000118 spin_lock_irqsave(&clockfw_lock, flags);
119 ret = clk->usecount;
120 spin_unlock_irqrestore(&clockfw_lock, flags);
121
122 return ret;
123}
124EXPORT_SYMBOL(clk_get_usecount);
125
126unsigned long clk_get_rate(struct clk *clk)
127{
128 unsigned long flags;
129 unsigned long ret = 0;
130
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100131 if (clk == NULL || IS_ERR(clk))
132 return 0;
133
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000134 spin_lock_irqsave(&clockfw_lock, flags);
135 ret = clk->rate;
136 spin_unlock_irqrestore(&clockfw_lock, flags);
137
138 return ret;
139}
140EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142void clk_put(struct clk *clk)
143{
144 if (clk && !IS_ERR(clk))
145 module_put(clk->owner);
146}
147EXPORT_SYMBOL(clk_put);
148
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000149/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800150 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000151 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153long clk_round_rate(struct clk *clk, unsigned long rate)
154{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000155 unsigned long flags;
156 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100158 if (clk == NULL || IS_ERR(clk))
159 return ret;
160
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000161 spin_lock_irqsave(&clockfw_lock, flags);
162 if (arch_clock->clk_round_rate)
163 ret = arch_clock->clk_round_rate(clk, rate);
164 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168EXPORT_SYMBOL(clk_round_rate);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170int clk_set_rate(struct clk *clk, unsigned long rate)
171{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000172 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100173 int ret = -EINVAL;
174
175 if (clk == NULL || IS_ERR(clk))
176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000178 spin_lock_irqsave(&clockfw_lock, flags);
179 if (arch_clock->clk_set_rate)
180 ret = arch_clock->clk_set_rate(clk, rate);
181 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return ret;
184}
185EXPORT_SYMBOL(clk_set_rate);
186
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000187int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000189 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100190 int ret = -EINVAL;
191
192 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000195 spin_lock_irqsave(&clockfw_lock, flags);
196 if (arch_clock->clk_set_parent)
197 ret = arch_clock->clk_set_parent(clk, parent);
198 spin_unlock_irqrestore(&clockfw_lock, flags);
199
200 return ret;
201}
202EXPORT_SYMBOL(clk_set_parent);
203
204struct clk *clk_get_parent(struct clk *clk)
205{
206 unsigned long flags;
207 struct clk * ret = NULL;
208
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100209 if (clk == NULL || IS_ERR(clk))
210 return ret;
211
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000212 spin_lock_irqsave(&clockfw_lock, flags);
213 if (arch_clock->clk_get_parent)
214 ret = arch_clock->clk_get_parent(clk);
215 spin_unlock_irqrestore(&clockfw_lock, flags);
216
217 return ret;
218}
219EXPORT_SYMBOL(clk_get_parent);
220
221/*-------------------------------------------------------------------------
222 * OMAP specific clock functions shared between omap1 and omap2
223 *-------------------------------------------------------------------------*/
224
225unsigned int __initdata mpurate;
226
227/*
228 * By default we use the rate set by the bootloader.
229 * You can override this with mpurate= cmdline option.
230 */
231static int __init omap_clk_setup(char *str)
232{
233 get_option(&str, &mpurate);
234
235 if (!mpurate)
236 return 1;
237
238 if (mpurate < 1000)
239 mpurate *= 1000000;
240
241 return 1;
242}
243__setup("mpurate=", omap_clk_setup);
244
245/* Used for clocks that always have same value as the parent clock */
246void followparent_recalc(struct clk *clk)
247{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100248 if (clk == NULL || IS_ERR(clk))
249 return;
250
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000251 clk->rate = clk->parent->rate;
252}
253
254/* Propagate rate to children */
255void propagate_rate(struct clk * tclk)
256{
257 struct clk *clkp;
258
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100259 if (tclk == NULL || IS_ERR(tclk))
260 return;
261
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000262 list_for_each_entry(clkp, &clocks, node) {
263 if (likely(clkp->parent != tclk))
264 continue;
265 if (likely((u32)clkp->recalc))
266 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270int clk_register(struct clk *clk)
271{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100272 if (clk == NULL || IS_ERR(clk))
273 return -EINVAL;
274
Arjan van de Ven00431702006-01-12 18:42:23 +0000275 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 list_add(&clk->node, &clocks);
277 if (clk->init)
278 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000279 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282}
283EXPORT_SYMBOL(clk_register);
284
285void clk_unregister(struct clk *clk)
286{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100287 if (clk == NULL || IS_ERR(clk))
288 return;
289
Arjan van de Ven00431702006-01-12 18:42:23 +0000290 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000292 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294EXPORT_SYMBOL(clk_unregister);
295
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000296void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100297{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000298 unsigned long flags;
299
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100300 if (clk == NULL || IS_ERR(clk))
301 return;
302
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000303 spin_lock_irqsave(&clockfw_lock, flags);
304 if (arch_clock->clk_deny_idle)
305 arch_clock->clk_deny_idle(clk);
306 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100307}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000308EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000310void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000312 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100314 if (clk == NULL || IS_ERR(clk))
315 return;
316
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000317 spin_lock_irqsave(&clockfw_lock, flags);
318 if (arch_clock->clk_allow_idle)
319 arch_clock->clk_allow_idle(clk);
320 spin_unlock_irqrestore(&clockfw_lock, flags);
321}
322EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100323
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000324/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000326int __init clk_init(struct clk_functions * custom_clocks)
327{
328 if (!custom_clocks) {
329 printk(KERN_ERR "No custom clock functions registered\n");
330 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000333 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 return 0;
336}