blob: 332a1798547c9d92ed4ea7631effb1cfb81b26d0 [file] [log] [blame]
Paul Mundt36ddf312006-01-16 22:14:17 -08001/*
2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
3 *
Paul Mundtdb62e5b2007-04-26 12:17:20 +09004 * Copyright (C) 2005, 2006, 2007 Paul Mundt
Paul Mundt36ddf312006-01-16 22:14:17 -08005 *
6 * This clock framework is derived from the OMAP version by:
7 *
Paul Mundt1d118562006-12-01 13:15:14 +09008 * Copyright (C) 2004 - 2005 Nokia Corporation
Paul Mundt36ddf312006-01-16 22:14:17 -08009 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10 *
Paul Mundt1d118562006-12-01 13:15:14 +090011 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
12 *
Paul Mundt36ddf312006-01-16 22:14:17 -080013 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
15 * for more details.
16 */
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/module.h>
Paul Mundt237b98f2006-09-27 17:28:20 +090020#include <linux/mutex.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080021#include <linux/list.h>
22#include <linux/kref.h>
23#include <linux/seq_file.h>
24#include <linux/err.h>
Paul Mundt1d118562006-12-01 13:15:14 +090025#include <linux/platform_device.h>
Paul Mundtdb62e5b2007-04-26 12:17:20 +090026#include <linux/proc_fs.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080027#include <asm/clock.h>
28#include <asm/timer.h>
29
30static LIST_HEAD(clock_list);
31static DEFINE_SPINLOCK(clock_lock);
Paul Mundt237b98f2006-09-27 17:28:20 +090032static DEFINE_MUTEX(clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -080033
34/*
35 * Each subtype is expected to define the init routines for these clocks,
36 * as each subtype (or processor family) will have these clocks at the
37 * very least. These are all provided through the CPG, which even some of
38 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
39 *
40 * The processor-specific code is expected to register any additional
41 * clock sources that are of interest.
42 */
43static struct clk master_clk = {
44 .name = "master_clk",
45 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
Paul Mundt36ddf312006-01-16 22:14:17 -080046 .rate = CONFIG_SH_PCLK_FREQ,
Paul Mundt36ddf312006-01-16 22:14:17 -080047};
48
49static struct clk module_clk = {
50 .name = "module_clk",
51 .parent = &master_clk,
52 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
53};
54
55static struct clk bus_clk = {
56 .name = "bus_clk",
57 .parent = &master_clk,
58 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
59};
60
61static struct clk cpu_clk = {
62 .name = "cpu_clk",
63 .parent = &master_clk,
64 .flags = CLK_ALWAYS_ENABLED,
65};
66
67/*
68 * The ordering of these clocks matters, do not change it.
69 */
70static struct clk *onchip_clocks[] = {
71 &master_clk,
72 &module_clk,
73 &bus_clk,
74 &cpu_clk,
75};
76
77static void propagate_rate(struct clk *clk)
78{
79 struct clk *clkp;
80
81 list_for_each_entry(clkp, &clock_list, node) {
82 if (likely(clkp->parent != clk))
83 continue;
84 if (likely(clkp->ops && clkp->ops->recalc))
85 clkp->ops->recalc(clkp);
Stuart Menefy24eb17e2007-09-28 11:51:52 +090086 if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
87 propagate_rate(clkp);
Paul Mundt36ddf312006-01-16 22:14:17 -080088 }
89}
90
Adrian Bunk4c1cfab2008-06-18 03:36:50 +030091static int __clk_enable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -080092{
93 /*
94 * See if this is the first time we're enabling the clock, some
95 * clocks that are always enabled still require "special"
96 * initialization. This is especially true if the clock mode
97 * changes and the clock needs to hunt for the proper set of
98 * divisors to use before it can effectively recalc.
99 */
100 if (unlikely(atomic_read(&clk->kref.refcount) == 1))
101 if (clk->ops && clk->ops->init)
102 clk->ops->init(clk);
103
dmitry pervushin1929cb32007-04-24 13:39:09 +0900104 kref_get(&clk->kref);
105
Paul Mundt36ddf312006-01-16 22:14:17 -0800106 if (clk->flags & CLK_ALWAYS_ENABLED)
107 return 0;
108
109 if (likely(clk->ops && clk->ops->enable))
110 clk->ops->enable(clk);
111
Paul Mundt36ddf312006-01-16 22:14:17 -0800112 return 0;
113}
114
115int clk_enable(struct clk *clk)
116{
117 unsigned long flags;
118 int ret;
119
Magnus Dammd12cfac2008-10-31 20:13:32 +0900120 if (!clk)
121 return -EINVAL;
122
123 clk_enable(clk->parent);
124
Paul Mundt36ddf312006-01-16 22:14:17 -0800125 spin_lock_irqsave(&clock_lock, flags);
126 ret = __clk_enable(clk);
127 spin_unlock_irqrestore(&clock_lock, flags);
128
129 return ret;
130}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900131EXPORT_SYMBOL_GPL(clk_enable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800132
133static void clk_kref_release(struct kref *kref)
134{
135 /* Nothing to do */
136}
137
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300138static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800139{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900140 int count = kref_put(&clk->kref, clk_kref_release);
141
Paul Mundt36ddf312006-01-16 22:14:17 -0800142 if (clk->flags & CLK_ALWAYS_ENABLED)
143 return;
144
dmitry pervushin1929cb32007-04-24 13:39:09 +0900145 if (!count) { /* count reaches zero, disable the clock */
146 if (likely(clk->ops && clk->ops->disable))
147 clk->ops->disable(clk);
148 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800149}
150
151void clk_disable(struct clk *clk)
152{
153 unsigned long flags;
154
Magnus Dammd12cfac2008-10-31 20:13:32 +0900155 if (!clk)
Paul Mundt00e825c2008-11-18 14:21:34 +0900156 return;
Magnus Dammd12cfac2008-10-31 20:13:32 +0900157
Paul Mundt36ddf312006-01-16 22:14:17 -0800158 spin_lock_irqsave(&clock_lock, flags);
159 __clk_disable(clk);
160 spin_unlock_irqrestore(&clock_lock, flags);
Magnus Dammd12cfac2008-10-31 20:13:32 +0900161
162 clk_disable(clk->parent);
Paul Mundt36ddf312006-01-16 22:14:17 -0800163}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900164EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800165
166int clk_register(struct clk *clk)
167{
Paul Mundt237b98f2006-09-27 17:28:20 +0900168 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800169
170 list_add(&clk->node, &clock_list);
171 kref_init(&clk->kref);
172
Paul Mundt237b98f2006-09-27 17:28:20 +0900173 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800174
dmitry pervushin1929cb32007-04-24 13:39:09 +0900175 if (clk->flags & CLK_ALWAYS_ENABLED) {
176 pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
177 if (clk->ops && clk->ops->init)
178 clk->ops->init(clk);
179 if (clk->ops && clk->ops->enable)
180 clk->ops->enable(clk);
181 pr_debug( "Enabled.");
182 }
183
Paul Mundt36ddf312006-01-16 22:14:17 -0800184 return 0;
185}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900186EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800187
188void clk_unregister(struct clk *clk)
189{
Paul Mundt237b98f2006-09-27 17:28:20 +0900190 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800191 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900192 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800193}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900194EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800195
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900196unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800197{
198 return clk->rate;
199}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900200EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800201
202int clk_set_rate(struct clk *clk, unsigned long rate)
203{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900204 return clk_set_rate_ex(clk, rate, 0);
205}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900206EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900207
208int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
209{
Paul Mundt36ddf312006-01-16 22:14:17 -0800210 int ret = -EOPNOTSUPP;
211
212 if (likely(clk->ops && clk->ops->set_rate)) {
213 unsigned long flags;
214
215 spin_lock_irqsave(&clock_lock, flags);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900216 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt36ddf312006-01-16 22:14:17 -0800217 spin_unlock_irqrestore(&clock_lock, flags);
218 }
219
220 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
221 propagate_rate(clk);
222
223 return ret;
224}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900225EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800226
227void clk_recalc_rate(struct clk *clk)
228{
229 if (likely(clk->ops && clk->ops->recalc)) {
230 unsigned long flags;
231
232 spin_lock_irqsave(&clock_lock, flags);
233 clk->ops->recalc(clk);
234 spin_unlock_irqrestore(&clock_lock, flags);
235 }
236
237 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
238 propagate_rate(clk);
239}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900240EXPORT_SYMBOL_GPL(clk_recalc_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800241
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000242int clk_set_parent(struct clk *clk, struct clk *parent)
243{
244 int ret = -EINVAL;
245 struct clk *old;
246
247 if (!parent || !clk)
248 return ret;
249
250 old = clk->parent;
251 if (likely(clk->ops && clk->ops->set_parent)) {
252 unsigned long flags;
253 spin_lock_irqsave(&clock_lock, flags);
254 ret = clk->ops->set_parent(clk, parent);
255 spin_unlock_irqrestore(&clock_lock, flags);
256 clk->parent = (ret ? old : parent);
257 }
258
259 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
260 propagate_rate(clk);
261 return ret;
262}
263EXPORT_SYMBOL_GPL(clk_set_parent);
264
265struct clk *clk_get_parent(struct clk *clk)
266{
267 return clk->parent;
268}
269EXPORT_SYMBOL_GPL(clk_get_parent);
270
Paul Mundtf6991b02007-07-20 13:29:09 +0900271long clk_round_rate(struct clk *clk, unsigned long rate)
272{
273 if (likely(clk->ops && clk->ops->round_rate)) {
274 unsigned long flags, rounded;
275
276 spin_lock_irqsave(&clock_lock, flags);
277 rounded = clk->ops->round_rate(clk, rate);
278 spin_unlock_irqrestore(&clock_lock, flags);
279
280 return rounded;
281 }
282
283 return clk_get_rate(clk);
284}
285EXPORT_SYMBOL_GPL(clk_round_rate);
286
Paul Mundt1d118562006-12-01 13:15:14 +0900287/*
288 * Returns a clock. Note that we first try to use device id on the bus
289 * and clock name. If this fails, we try to use clock name only.
290 */
291struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800292{
293 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900294 int idno;
295
296 if (dev == NULL || dev->bus != &platform_bus_type)
297 idno = -1;
298 else
299 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800300
Paul Mundt237b98f2006-09-27 17:28:20 +0900301 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800302 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900303 if (p->id == idno &&
304 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
305 clk = p;
306 goto found;
307 }
308 }
309
310 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800311 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
312 clk = p;
313 break;
314 }
315 }
Paul Mundt1d118562006-12-01 13:15:14 +0900316
317found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900318 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800319
320 return clk;
321}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900322EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800323
324void clk_put(struct clk *clk)
325{
326 if (clk && !IS_ERR(clk))
327 module_put(clk->owner);
328}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900329EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800330
331void __init __attribute__ ((weak))
332arch_init_clk_ops(struct clk_ops **ops, int type)
333{
334}
335
Paul Mundtfa439722008-09-04 18:53:58 +0900336int __init __attribute__ ((weak))
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900337arch_clk_init(void)
338{
Paul Mundtfa439722008-09-04 18:53:58 +0900339 return 0;
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900340}
341
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900342static int show_clocks(char *buf, char **start, off_t off,
343 int len, int *eof, void *data)
344{
345 struct clk *clk;
346 char *p = buf;
347
348 list_for_each_entry_reverse(clk, &clock_list, node) {
349 unsigned long rate = clk_get_rate(clk);
350
Magnus Damm152fe362008-07-17 19:05:54 +0900351 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
352 rate / 1000000, (rate % 1000000) / 10000,
353 ((clk->flags & CLK_ALWAYS_ENABLED) ||
354 (atomic_read(&clk->kref.refcount) != 1)) ?
355 "enabled" : "disabled");
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900356 }
357
358 return p - buf;
359}
360
Paul Mundt36ddf312006-01-16 22:14:17 -0800361int __init clk_init(void)
362{
363 int i, ret = 0;
364
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900365 BUG_ON(!master_clk.rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800366
367 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
368 struct clk *clk = onchip_clocks[i];
369
370 arch_init_clk_ops(&clk->ops, i);
371 ret |= clk_register(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800372 }
373
Paul Mundtfa439722008-09-04 18:53:58 +0900374 ret |= arch_clk_init();
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900375
Paul Mundt36ddf312006-01-16 22:14:17 -0800376 /* Kick the child clocks.. */
377 propagate_rate(&master_clk);
378 propagate_rate(&bus_clk);
379
380 return ret;
381}
382
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900383static int __init clk_proc_init(void)
Paul Mundt36ddf312006-01-16 22:14:17 -0800384{
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900385 struct proc_dir_entry *p;
386 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
387 show_clocks, NULL);
388 if (unlikely(!p))
389 return -EINVAL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800390
391 return 0;
392}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900393subsys_initcall(clk_proc_init);