clock.c 6.7 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>
23
#include <linux/mutex.h>
L
Linus Torvalds 已提交
24

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

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

30
LIST_HEAD(clocks);
31
static DEFINE_MUTEX(clocks_mutex);
32
DEFINE_SPINLOCK(clockfw_lock);
L
Linus Torvalds 已提交
33

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

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

40
struct clk * clk_get(struct device *dev, const char *id)
L
Linus Torvalds 已提交
41 42 43
{
	struct clk *p, *clk = ERR_PTR(-ENOENT);

44
	mutex_lock(&clocks_mutex);
L
Linus Torvalds 已提交
45 46 47 48 49 50
	list_for_each_entry(p, &clocks, node) {
		if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
			clk = p;
			break;
		}
	}
51
	mutex_unlock(&clocks_mutex);
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59

	return clk;
}
EXPORT_SYMBOL(clk_get);

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

	spin_lock_irqsave(&clockfw_lock, flags);
63 64 65 66 67 68
	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 已提交
69
	spin_unlock_irqrestore(&clockfw_lock, flags);
70

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

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

	spin_lock_irqsave(&clockfw_lock, flags);
80 81 82 83 84 85
	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 已提交
86 87 88 89 90 91 92 93 94 95
	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);
96 97
	if (arch_clock->clk_use)
		ret = arch_clock->clk_use(clk);
L
Linus Torvalds 已提交
98
	spin_unlock_irqrestore(&clockfw_lock, flags);
99

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

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

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

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

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

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

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

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

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

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

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

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

157 158 159 160
	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 已提交
161

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

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

171 172 173 174
	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 已提交
175

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

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

185 186 187 188
	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 已提交
189

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

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

199 200 201 202
	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 已提交
203 204 205

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

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

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

214 215 216 217 218
/*
 * 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 已提交
219
{
220
	get_option(&str, &mpurate);
L
Linus Torvalds 已提交
221

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

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

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

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

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

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

int clk_register(struct clk *clk)
{
253
	mutex_lock(&clocks_mutex);
L
Linus Torvalds 已提交
254 255 256
	list_add(&clk->node, &clocks);
	if (clk->init)
		clk->init(clk);
257
	mutex_unlock(&clocks_mutex);
258

L
Linus Torvalds 已提交
259 260 261 262 263 264
	return 0;
}
EXPORT_SYMBOL(clk_register);

void clk_unregister(struct clk *clk)
{
265
	mutex_lock(&clocks_mutex);
L
Linus Torvalds 已提交
266
	list_del(&clk->node);
267
	mutex_unlock(&clocks_mutex);
L
Linus Torvalds 已提交
268 269 270
}
EXPORT_SYMBOL(clk_unregister);

271
void clk_deny_idle(struct clk *clk)
272
{
273 274 275 276 277 278
	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);
279
}
280
EXPORT_SYMBOL(clk_deny_idle);
L
Linus Torvalds 已提交
281

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

286 287 288 289
	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 已提交
290
}
291
EXPORT_SYMBOL(clk_allow_idle);
292

293
/*-------------------------------------------------------------------------*/
294

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

302 303
	arch_clock = custom_clocks;

304 305
	return 0;
}