clock.c 6.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  linux/arch/arm/plat-omap/clock.c
L
Linus Torvalds 已提交
3
 *
4
 *  Copyright (C) 2004 - 2005 Nokia corporation
L
Linus Torvalds 已提交
5 6
 *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
 *
7 8
 *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
 *
L
Linus Torvalds 已提交
9 10 11 12
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
13 14
#include <linux/version.h>
#include <linux/config.h>
L
Linus Torvalds 已提交
15
#include <linux/kernel.h>
16 17
#include <linux/init.h>
#include <linux/module.h>
L
Linus Torvalds 已提交
18 19 20
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/err.h>
T
Tim Schmielau 已提交
21
#include <linux/string.h>
22
#include <linux/clk.h>
L
Linus Torvalds 已提交
23

24
#include <asm/io.h>
L
Linus Torvalds 已提交
25 26
#include <asm/semaphore.h>

27
#include <asm/arch/clock.h>
L
Linus Torvalds 已提交
28

29
LIST_HEAD(clocks);
L
Linus Torvalds 已提交
30
static DECLARE_MUTEX(clocks_sem);
31
DEFINE_SPINLOCK(clockfw_lock);
L
Linus Torvalds 已提交
32

33
static struct clk_functions *arch_clock;
L
Linus Torvalds 已提交
34

35 36 37
/*-------------------------------------------------------------------------
 * Standard clock functions defined in asm/hardware/clock.h
 *-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
38

39
struct clk * clk_get(struct device *dev, const char *id)
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	struct clk *p, *clk = ERR_PTR(-ENOENT);

	down(&clocks_sem);
	list_for_each_entry(p, &clocks, node) {
		if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
			clk = p;
			break;
		}
	}
	up(&clocks_sem);

	return clk;
}
EXPORT_SYMBOL(clk_get);

int clk_enable(struct clk *clk)
{
	unsigned long flags;
59
	int ret = 0;
L
Linus Torvalds 已提交
60 61

	spin_lock_irqsave(&clockfw_lock, flags);
62 63 64 65 66 67
	if (clk->enable)
		ret = clk->enable(clk);
	else if (arch_clock->clk_enable)
		ret = arch_clock->clk_enable(clk);
	else
		printk(KERN_ERR "Could not enable clock %s\n", clk->name);
L
Linus Torvalds 已提交
68
	spin_unlock_irqrestore(&clockfw_lock, flags);
69

L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78
	return ret;
}
EXPORT_SYMBOL(clk_enable);

void clk_disable(struct clk *clk)
{
	unsigned long flags;

	spin_lock_irqsave(&clockfw_lock, flags);
79 80 81 82 83 84
	if (clk->disable)
		clk->disable(clk);
	else if (arch_clock->clk_disable)
		arch_clock->clk_disable(clk);
	else
		printk(KERN_ERR "Could not disable clock %s\n", clk->name);
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94
	spin_unlock_irqrestore(&clockfw_lock, flags);
}
EXPORT_SYMBOL(clk_disable);

int clk_use(struct clk *clk)
{
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&clockfw_lock, flags);
95 96
	if (arch_clock->clk_use)
		ret = arch_clock->clk_use(clk);
L
Linus Torvalds 已提交
97
	spin_unlock_irqrestore(&clockfw_lock, flags);
98

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107
	return ret;
}
EXPORT_SYMBOL(clk_use);

void clk_unuse(struct clk *clk)
{
	unsigned long flags;

	spin_lock_irqsave(&clockfw_lock, flags);
108 109
	if (arch_clock->clk_unuse)
		arch_clock->clk_unuse(clk);
L
Linus Torvalds 已提交
110 111 112 113 114 115
	spin_unlock_irqrestore(&clockfw_lock, flags);
}
EXPORT_SYMBOL(clk_unuse);

int clk_get_usecount(struct clk *clk)
{
116 117
	unsigned long flags;
	int ret = 0;
L
Linus Torvalds 已提交
118

119 120 121
	spin_lock_irqsave(&clockfw_lock, flags);
	ret = clk->usecount;
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
122

123
	return ret;
L
Linus Torvalds 已提交
124
}
125
EXPORT_SYMBOL(clk_get_usecount);
L
Linus Torvalds 已提交
126

127
unsigned long clk_get_rate(struct clk *clk)
L
Linus Torvalds 已提交
128
{
129 130
	unsigned long flags;
	unsigned long ret = 0;
L
Linus Torvalds 已提交
131

132 133 134
	spin_lock_irqsave(&clockfw_lock, flags);
	ret = clk->rate;
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
135

136
	return ret;
L
Linus Torvalds 已提交
137
}
138
EXPORT_SYMBOL(clk_get_rate);
L
Linus Torvalds 已提交
139

140
void clk_put(struct clk *clk)
141
{
142 143
	if (clk && !IS_ERR(clk))
		module_put(clk->owner);
144
}
145
EXPORT_SYMBOL(clk_put);
146

147 148 149
/*-------------------------------------------------------------------------
 * Optional clock functions defined in asm/hardware/clock.h
 *-------------------------------------------------------------------------*/
150

L
Linus Torvalds 已提交
151 152
long clk_round_rate(struct clk *clk, unsigned long rate)
{
153 154
	unsigned long flags;
	long ret = 0;
L
Linus Torvalds 已提交
155

156 157 158 159
	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_round_rate)
		ret = arch_clock->clk_round_rate(clk, rate);
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
160

161
	return ret;
L
Linus Torvalds 已提交
162 163 164
}
EXPORT_SYMBOL(clk_round_rate);

165
int clk_set_rate(struct clk *clk, unsigned long rate)
L
Linus Torvalds 已提交
166
{
167 168
	unsigned long flags;
	int ret = 0;
169

170 171 172 173
	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_set_rate)
		ret = arch_clock->clk_set_rate(clk, rate);
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
174

175
	return ret;
L
Linus Torvalds 已提交
176
}
177
EXPORT_SYMBOL(clk_set_rate);
L
Linus Torvalds 已提交
178

179
int clk_set_parent(struct clk *clk, struct clk *parent)
L
Linus Torvalds 已提交
180
{
181 182
	unsigned long flags;
	int ret = 0;
L
Linus Torvalds 已提交
183

184 185 186 187
	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_set_parent)
		ret =  arch_clock->clk_set_parent(clk, parent);
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
188

189
	return ret;
L
Linus Torvalds 已提交
190
}
191
EXPORT_SYMBOL(clk_set_parent);
L
Linus Torvalds 已提交
192

193
struct clk *clk_get_parent(struct clk *clk)
L
Linus Torvalds 已提交
194
{
195 196
	unsigned long flags;
	struct clk * ret = NULL;
L
Linus Torvalds 已提交
197

198 199 200 201
	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_get_parent)
		ret = arch_clock->clk_get_parent(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
202 203 204

	return ret;
}
205
EXPORT_SYMBOL(clk_get_parent);
L
Linus Torvalds 已提交
206

207 208 209
/*-------------------------------------------------------------------------
 * OMAP specific clock functions shared between omap1 and omap2
 *-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
210

211
unsigned int __initdata mpurate;
L
Linus Torvalds 已提交
212

213 214 215 216 217
/*
 * By default we use the rate set by the bootloader.
 * You can override this with mpurate= cmdline option.
 */
static int __init omap_clk_setup(char *str)
L
Linus Torvalds 已提交
218
{
219
	get_option(&str, &mpurate);
L
Linus Torvalds 已提交
220

221 222
	if (!mpurate)
		return 1;
L
Linus Torvalds 已提交
223

224 225
	if (mpurate < 1000)
		mpurate *= 1000000;
L
Linus Torvalds 已提交
226

227
	return 1;
L
Linus Torvalds 已提交
228
}
229
__setup("mpurate=", omap_clk_setup);
L
Linus Torvalds 已提交
230

231 232
/* Used for clocks that always have same value as the parent clock */
void followparent_recalc(struct clk *clk)
L
Linus Torvalds 已提交
233
{
234
	clk->rate = clk->parent->rate;
L
Linus Torvalds 已提交
235 236
}

237 238
/* Propagate rate to children */
void propagate_rate(struct clk * tclk)
L
Linus Torvalds 已提交
239
{
240
	struct clk *clkp;
L
Linus Torvalds 已提交
241

242 243 244 245 246 247
	list_for_each_entry(clkp, &clocks, node) {
		if (likely(clkp->parent != tclk))
			continue;
		if (likely((u32)clkp->recalc))
			clkp->recalc(clkp);
	}
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256
}

int clk_register(struct clk *clk)
{
	down(&clocks_sem);
	list_add(&clk->node, &clocks);
	if (clk->init)
		clk->init(clk);
	up(&clocks_sem);
257

L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269
	return 0;
}
EXPORT_SYMBOL(clk_register);

void clk_unregister(struct clk *clk)
{
	down(&clocks_sem);
	list_del(&clk->node);
	up(&clocks_sem);
}
EXPORT_SYMBOL(clk_unregister);

270
void clk_deny_idle(struct clk *clk)
271
{
272 273 274 275 276 277
	unsigned long flags;

	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_deny_idle)
		arch_clock->clk_deny_idle(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);
278
}
279
EXPORT_SYMBOL(clk_deny_idle);
L
Linus Torvalds 已提交
280

281
void clk_allow_idle(struct clk *clk)
L
Linus Torvalds 已提交
282
{
283
	unsigned long flags;
L
Linus Torvalds 已提交
284

285 286 287 288
	spin_lock_irqsave(&clockfw_lock, flags);
	if (arch_clock->clk_allow_idle)
		arch_clock->clk_allow_idle(clk);
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
289
}
290
EXPORT_SYMBOL(clk_allow_idle);
291

292
/*-------------------------------------------------------------------------*/
293

294
int __init clk_init(struct clk_functions * custom_clocks)
295
{
296 297 298
	if (!custom_clocks) {
		printk(KERN_ERR "No custom clock functions registered\n");
		BUG();
299 300
	}

301 302
	arch_clock = custom_clocks;

303 304
	return 0;
}