clock.c 6.0 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
 * Standard clock functions defined in include/linux/clk.h
38
 *-------------------------------------------------------------------------*/
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
	if (arch_clock->clk_enable)
64
		ret = arch_clock->clk_enable(clk);
L
Linus Torvalds 已提交
65
	spin_unlock_irqrestore(&clockfw_lock, flags);
66

L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75
	return ret;
}
EXPORT_SYMBOL(clk_enable);

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

	spin_lock_irqsave(&clockfw_lock, flags);
76
	if (arch_clock->clk_disable)
77
		arch_clock->clk_disable(clk);
L
Linus Torvalds 已提交
78 79 80 81 82 83
	spin_unlock_irqrestore(&clockfw_lock, flags);
}
EXPORT_SYMBOL(clk_disable);

int clk_get_usecount(struct clk *clk)
{
84 85
	unsigned long flags;
	int ret = 0;
L
Linus Torvalds 已提交
86

87 88 89
	spin_lock_irqsave(&clockfw_lock, flags);
	ret = clk->usecount;
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
90

91
	return ret;
L
Linus Torvalds 已提交
92
}
93
EXPORT_SYMBOL(clk_get_usecount);
L
Linus Torvalds 已提交
94

95
unsigned long clk_get_rate(struct clk *clk)
L
Linus Torvalds 已提交
96
{
97 98
	unsigned long flags;
	unsigned long ret = 0;
L
Linus Torvalds 已提交
99

100 101 102
	spin_lock_irqsave(&clockfw_lock, flags);
	ret = clk->rate;
	spin_unlock_irqrestore(&clockfw_lock, flags);
L
Linus Torvalds 已提交
103

104
	return ret;
L
Linus Torvalds 已提交
105
}
106
EXPORT_SYMBOL(clk_get_rate);
L
Linus Torvalds 已提交
107

108
void clk_put(struct clk *clk)
109
{
110 111
	if (clk && !IS_ERR(clk))
		module_put(clk->owner);
112
}
113
EXPORT_SYMBOL(clk_put);
114

115
/*-------------------------------------------------------------------------
116
 * Optional clock functions defined in include/linux/clk.h
117
 *-------------------------------------------------------------------------*/
118

L
Linus Torvalds 已提交
119 120
long clk_round_rate(struct clk *clk, unsigned long rate)
{
121 122
	unsigned long flags;
	long ret = 0;
L
Linus Torvalds 已提交
123

124 125 126 127
	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 已提交
128

129
	return ret;
L
Linus Torvalds 已提交
130 131 132
}
EXPORT_SYMBOL(clk_round_rate);

133
int clk_set_rate(struct clk *clk, unsigned long rate)
L
Linus Torvalds 已提交
134
{
135 136
	unsigned long flags;
	int ret = 0;
137

138 139 140 141
	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 已提交
142

143
	return ret;
L
Linus Torvalds 已提交
144
}
145
EXPORT_SYMBOL(clk_set_rate);
L
Linus Torvalds 已提交
146

147
int clk_set_parent(struct clk *clk, struct clk *parent)
L
Linus Torvalds 已提交
148
{
149 150
	unsigned long flags;
	int ret = 0;
L
Linus Torvalds 已提交
151

152 153 154 155
	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 已提交
156

157
	return ret;
L
Linus Torvalds 已提交
158
}
159
EXPORT_SYMBOL(clk_set_parent);
L
Linus Torvalds 已提交
160

161
struct clk *clk_get_parent(struct clk *clk)
L
Linus Torvalds 已提交
162
{
163 164
	unsigned long flags;
	struct clk * ret = NULL;
L
Linus Torvalds 已提交
165

166 167 168 169
	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 已提交
170 171 172

	return ret;
}
173
EXPORT_SYMBOL(clk_get_parent);
L
Linus Torvalds 已提交
174

175 176 177
/*-------------------------------------------------------------------------
 * OMAP specific clock functions shared between omap1 and omap2
 *-------------------------------------------------------------------------*/
L
Linus Torvalds 已提交
178

179
unsigned int __initdata mpurate;
L
Linus Torvalds 已提交
180

181 182 183 184 185
/*
 * 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 已提交
186
{
187
	get_option(&str, &mpurate);
L
Linus Torvalds 已提交
188

189 190
	if (!mpurate)
		return 1;
L
Linus Torvalds 已提交
191

192 193
	if (mpurate < 1000)
		mpurate *= 1000000;
L
Linus Torvalds 已提交
194

195
	return 1;
L
Linus Torvalds 已提交
196
}
197
__setup("mpurate=", omap_clk_setup);
L
Linus Torvalds 已提交
198

199 200
/* Used for clocks that always have same value as the parent clock */
void followparent_recalc(struct clk *clk)
L
Linus Torvalds 已提交
201
{
202
	clk->rate = clk->parent->rate;
L
Linus Torvalds 已提交
203 204
}

205 206
/* Propagate rate to children */
void propagate_rate(struct clk * tclk)
L
Linus Torvalds 已提交
207
{
208
	struct clk *clkp;
L
Linus Torvalds 已提交
209

210 211 212 213 214 215
	list_for_each_entry(clkp, &clocks, node) {
		if (likely(clkp->parent != tclk))
			continue;
		if (likely((u32)clkp->recalc))
			clkp->recalc(clkp);
	}
L
Linus Torvalds 已提交
216 217 218 219
}

int clk_register(struct clk *clk)
{
220
	mutex_lock(&clocks_mutex);
L
Linus Torvalds 已提交
221 222 223
	list_add(&clk->node, &clocks);
	if (clk->init)
		clk->init(clk);
224
	mutex_unlock(&clocks_mutex);
225

L
Linus Torvalds 已提交
226 227 228 229 230 231
	return 0;
}
EXPORT_SYMBOL(clk_register);

void clk_unregister(struct clk *clk)
{
232
	mutex_lock(&clocks_mutex);
L
Linus Torvalds 已提交
233
	list_del(&clk->node);
234
	mutex_unlock(&clocks_mutex);
L
Linus Torvalds 已提交
235 236 237
}
EXPORT_SYMBOL(clk_unregister);

238
void clk_deny_idle(struct clk *clk)
239
{
240 241 242 243 244 245
	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);
246
}
247
EXPORT_SYMBOL(clk_deny_idle);
L
Linus Torvalds 已提交
248

249
void clk_allow_idle(struct clk *clk)
L
Linus Torvalds 已提交
250
{
251
	unsigned long flags;
L
Linus Torvalds 已提交
252

253 254 255 256
	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 已提交
257
}
258
EXPORT_SYMBOL(clk_allow_idle);
259

260
/*-------------------------------------------------------------------------*/
261

262
int __init clk_init(struct clk_functions * custom_clocks)
263
{
264 265 266
	if (!custom_clocks) {
		printk(KERN_ERR "No custom clock functions registered\n");
		BUG();
267 268
	}

269 270
	arch_clock = custom_clocks;

271 272
	return 0;
}