blob: 06485c193ee37c7ec342b794120078321170ffae [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;
61 break;
62 }
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
Arjan van de Ven00431702006-01-12 18:42:23 +000072 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return clk;
75}
76EXPORT_SYMBOL(clk_get);
77
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000078int clk_enable(struct clk *clk)
79{
80 unsigned long flags;
81 int ret = 0;
82
Tony Lindgrenb824efa2006-04-02 17:46:20 +010083 if (clk == NULL || IS_ERR(clk))
84 return -EINVAL;
85
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000086 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080087 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000088 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000089 spin_unlock_irqrestore(&clockfw_lock, flags);
90
91 return ret;
92}
93EXPORT_SYMBOL(clk_enable);
94
95void clk_disable(struct clk *clk)
96{
97 unsigned long flags;
98
Tony Lindgrenb824efa2006-04-02 17:46:20 +010099 if (clk == NULL || IS_ERR(clk))
100 return;
101
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000102 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800103 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000104 arch_clock->clk_disable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000105 spin_unlock_irqrestore(&clockfw_lock, flags);
106}
107EXPORT_SYMBOL(clk_disable);
108
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000109int clk_get_usecount(struct clk *clk)
110{
111 unsigned long flags;
112 int ret = 0;
113
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100114 if (clk == NULL || IS_ERR(clk))
115 return 0;
116
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000117 spin_lock_irqsave(&clockfw_lock, flags);
118 ret = clk->usecount;
119 spin_unlock_irqrestore(&clockfw_lock, flags);
120
121 return ret;
122}
123EXPORT_SYMBOL(clk_get_usecount);
124
125unsigned long clk_get_rate(struct clk *clk)
126{
127 unsigned long flags;
128 unsigned long ret = 0;
129
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100130 if (clk == NULL || IS_ERR(clk))
131 return 0;
132
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000133 spin_lock_irqsave(&clockfw_lock, flags);
134 ret = clk->rate;
135 spin_unlock_irqrestore(&clockfw_lock, flags);
136
137 return ret;
138}
139EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141void clk_put(struct clk *clk)
142{
143 if (clk && !IS_ERR(clk))
144 module_put(clk->owner);
145}
146EXPORT_SYMBOL(clk_put);
147
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000148/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800149 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000150 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152long clk_round_rate(struct clk *clk, unsigned long rate)
153{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000154 unsigned long flags;
155 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100157 if (clk == NULL || IS_ERR(clk))
158 return ret;
159
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000160 spin_lock_irqsave(&clockfw_lock, flags);
161 if (arch_clock->clk_round_rate)
162 ret = arch_clock->clk_round_rate(clk, rate);
163 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000165 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167EXPORT_SYMBOL(clk_round_rate);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169int clk_set_rate(struct clk *clk, unsigned long rate)
170{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000171 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100172 int ret = -EINVAL;
173
174 if (clk == NULL || IS_ERR(clk))
175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000177 spin_lock_irqsave(&clockfw_lock, flags);
178 if (arch_clock->clk_set_rate)
179 ret = arch_clock->clk_set_rate(clk, rate);
180 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 return ret;
183}
184EXPORT_SYMBOL(clk_set_rate);
185
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000186int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000188 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100189 int ret = -EINVAL;
190
191 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000194 spin_lock_irqsave(&clockfw_lock, flags);
195 if (arch_clock->clk_set_parent)
196 ret = arch_clock->clk_set_parent(clk, parent);
197 spin_unlock_irqrestore(&clockfw_lock, flags);
198
199 return ret;
200}
201EXPORT_SYMBOL(clk_set_parent);
202
203struct clk *clk_get_parent(struct clk *clk)
204{
205 unsigned long flags;
206 struct clk * ret = NULL;
207
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100208 if (clk == NULL || IS_ERR(clk))
209 return ret;
210
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000211 spin_lock_irqsave(&clockfw_lock, flags);
212 if (arch_clock->clk_get_parent)
213 ret = arch_clock->clk_get_parent(clk);
214 spin_unlock_irqrestore(&clockfw_lock, flags);
215
216 return ret;
217}
218EXPORT_SYMBOL(clk_get_parent);
219
220/*-------------------------------------------------------------------------
221 * OMAP specific clock functions shared between omap1 and omap2
222 *-------------------------------------------------------------------------*/
223
224unsigned int __initdata mpurate;
225
226/*
227 * By default we use the rate set by the bootloader.
228 * You can override this with mpurate= cmdline option.
229 */
230static int __init omap_clk_setup(char *str)
231{
232 get_option(&str, &mpurate);
233
234 if (!mpurate)
235 return 1;
236
237 if (mpurate < 1000)
238 mpurate *= 1000000;
239
240 return 1;
241}
242__setup("mpurate=", omap_clk_setup);
243
244/* Used for clocks that always have same value as the parent clock */
245void followparent_recalc(struct clk *clk)
246{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100247 if (clk == NULL || IS_ERR(clk))
248 return;
249
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000250 clk->rate = clk->parent->rate;
251}
252
253/* Propagate rate to children */
254void propagate_rate(struct clk * tclk)
255{
256 struct clk *clkp;
257
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100258 if (tclk == NULL || IS_ERR(tclk))
259 return;
260
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000261 list_for_each_entry(clkp, &clocks, node) {
262 if (likely(clkp->parent != tclk))
263 continue;
264 if (likely((u32)clkp->recalc))
265 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269int clk_register(struct clk *clk)
270{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100271 if (clk == NULL || IS_ERR(clk))
272 return -EINVAL;
273
Arjan van de Ven00431702006-01-12 18:42:23 +0000274 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 list_add(&clk->node, &clocks);
276 if (clk->init)
277 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000278 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281}
282EXPORT_SYMBOL(clk_register);
283
284void clk_unregister(struct clk *clk)
285{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100286 if (clk == NULL || IS_ERR(clk))
287 return;
288
Arjan van de Ven00431702006-01-12 18:42:23 +0000289 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000291 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293EXPORT_SYMBOL(clk_unregister);
294
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000295void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100296{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000297 unsigned long flags;
298
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100299 if (clk == NULL || IS_ERR(clk))
300 return;
301
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000302 spin_lock_irqsave(&clockfw_lock, flags);
303 if (arch_clock->clk_deny_idle)
304 arch_clock->clk_deny_idle(clk);
305 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100306}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000307EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000309void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000311 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100313 if (clk == NULL || IS_ERR(clk))
314 return;
315
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000316 spin_lock_irqsave(&clockfw_lock, flags);
317 if (arch_clock->clk_allow_idle)
318 arch_clock->clk_allow_idle(clk);
319 spin_unlock_irqrestore(&clockfw_lock, flags);
320}
321EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100322
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000323/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000325int __init clk_init(struct clk_functions * custom_clocks)
326{
327 if (!custom_clocks) {
328 printk(KERN_ERR "No custom clock functions registered\n");
329 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000332 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 return 0;
335}