blob: 34c6a52af060526e94d02228b839554ab0228f58 [file] [log] [blame]
Brian Swetland600f7cf2008-09-09 11:04:14 -07001/* arch/arm/mach-msm/clock.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Daniel Walker5e96da52010-05-12 13:43:28 -07004 * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
Brian Swetland600f7cf2008-09-09 11:04:14 -07005 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/version.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/list.h>
22#include <linux/err.h>
23#include <linux/clk.h>
24#include <linux/spinlock.h>
Daniel Walker5e96da52010-05-12 13:43:28 -070025#include <linux/debugfs.h>
26#include <linux/ctype.h>
27#include <linux/pm_qos_params.h>
28#include <mach/clk.h>
Brian Swetland600f7cf2008-09-09 11:04:14 -070029
30#include "clock.h"
31#include "proc_comm.h"
32
33static DEFINE_MUTEX(clocks_mutex);
34static DEFINE_SPINLOCK(clocks_lock);
35static LIST_HEAD(clocks);
Daniel Walker5e96da52010-05-12 13:43:28 -070036struct clk *msm_clocks;
37unsigned msm_num_clocks;
Brian Swetland600f7cf2008-09-09 11:04:14 -070038
39/*
Daniel Walker5e96da52010-05-12 13:43:28 -070040 * Bitmap of enabled clocks, excluding ACPU which is always
41 * enabled
Brian Swetland600f7cf2008-09-09 11:04:14 -070042 */
Daniel Walker5e96da52010-05-12 13:43:28 -070043static DECLARE_BITMAP(clock_map_enabled, NR_CLKS);
44static DEFINE_SPINLOCK(clock_map_lock);
Brian Swetland600f7cf2008-09-09 11:04:14 -070045
46/*
47 * Standard clock functions defined in include/linux/clk.h
48 */
49struct clk *clk_get(struct device *dev, const char *id)
50{
51 struct clk *clk;
52
53 mutex_lock(&clocks_mutex);
54
55 list_for_each_entry(clk, &clocks, list)
56 if (!strcmp(id, clk->name) && clk->dev == dev)
57 goto found_it;
58
59 list_for_each_entry(clk, &clocks, list)
60 if (!strcmp(id, clk->name) && clk->dev == NULL)
61 goto found_it;
62
63 clk = ERR_PTR(-ENOENT);
64found_it:
65 mutex_unlock(&clocks_mutex);
66 return clk;
67}
68EXPORT_SYMBOL(clk_get);
69
70void clk_put(struct clk *clk)
71{
72}
73EXPORT_SYMBOL(clk_put);
74
75int clk_enable(struct clk *clk)
76{
77 unsigned long flags;
78 spin_lock_irqsave(&clocks_lock, flags);
79 clk->count++;
Daniel Walker5e96da52010-05-12 13:43:28 -070080 if (clk->count == 1) {
81 clk->ops->enable(clk->id);
82 spin_lock(&clock_map_lock);
83 clock_map_enabled[BIT_WORD(clk->id)] |= BIT_MASK(clk->id);
84 spin_unlock(&clock_map_lock);
85 }
Brian Swetland600f7cf2008-09-09 11:04:14 -070086 spin_unlock_irqrestore(&clocks_lock, flags);
87 return 0;
88}
89EXPORT_SYMBOL(clk_enable);
90
91void clk_disable(struct clk *clk)
92{
93 unsigned long flags;
94 spin_lock_irqsave(&clocks_lock, flags);
95 BUG_ON(clk->count == 0);
96 clk->count--;
Daniel Walker5e96da52010-05-12 13:43:28 -070097 if (clk->count == 0) {
98 clk->ops->disable(clk->id);
99 spin_lock(&clock_map_lock);
100 clock_map_enabled[BIT_WORD(clk->id)] &= ~BIT_MASK(clk->id);
101 spin_unlock(&clock_map_lock);
102 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700103 spin_unlock_irqrestore(&clocks_lock, flags);
104}
105EXPORT_SYMBOL(clk_disable);
106
Daniel Walker5e96da52010-05-12 13:43:28 -0700107int clk_reset(struct clk *clk, enum clk_reset_action action)
108{
109 if (!clk->ops->reset)
110 clk->ops->reset = &pc_clk_reset;
111 return clk->ops->reset(clk->remote_id, action);
112}
113EXPORT_SYMBOL(clk_reset);
114
Brian Swetland600f7cf2008-09-09 11:04:14 -0700115unsigned long clk_get_rate(struct clk *clk)
116{
Daniel Walker5e96da52010-05-12 13:43:28 -0700117 return clk->ops->get_rate(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700118}
119EXPORT_SYMBOL(clk_get_rate);
120
121int clk_set_rate(struct clk *clk, unsigned long rate)
122{
Daniel Walker5e96da52010-05-12 13:43:28 -0700123 return clk->ops->set_rate(clk->id, rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700124}
125EXPORT_SYMBOL(clk_set_rate);
126
Daniel Walker5e96da52010-05-12 13:43:28 -0700127long clk_round_rate(struct clk *clk, unsigned long rate)
128{
129 return clk->ops->round_rate(clk->id, rate);
130}
131EXPORT_SYMBOL(clk_round_rate);
132
133int clk_set_min_rate(struct clk *clk, unsigned long rate)
134{
135 return clk->ops->set_min_rate(clk->id, rate);
136}
137EXPORT_SYMBOL(clk_set_min_rate);
138
139int clk_set_max_rate(struct clk *clk, unsigned long rate)
140{
141 return clk->ops->set_max_rate(clk->id, rate);
142}
143EXPORT_SYMBOL(clk_set_max_rate);
144
Brian Swetland600f7cf2008-09-09 11:04:14 -0700145int clk_set_parent(struct clk *clk, struct clk *parent)
146{
147 return -ENOSYS;
148}
149EXPORT_SYMBOL(clk_set_parent);
150
151struct clk *clk_get_parent(struct clk *clk)
152{
153 return ERR_PTR(-ENOSYS);
154}
155EXPORT_SYMBOL(clk_get_parent);
156
157int clk_set_flags(struct clk *clk, unsigned long flags)
158{
159 if (clk == NULL || IS_ERR(clk))
160 return -EINVAL;
Daniel Walker5e96da52010-05-12 13:43:28 -0700161 return clk->ops->set_flags(clk->id, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700162}
163EXPORT_SYMBOL(clk_set_flags);
164
Daniel Walker5e96da52010-05-12 13:43:28 -0700165/* EBI1 is the only shared clock that several clients want to vote on as of
166 * this commit. If this changes in the future, then it might be better to
167 * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
168 * generic to support different clocks.
169 */
170static struct clk *ebi1_clk;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700171
Daniel Walker5e96da52010-05-12 13:43:28 -0700172static void __init set_clock_ops(struct clk *clk)
173{
174 if (!clk->ops) {
175 clk->ops = &clk_ops_pcom;
176 clk->id = clk->remote_id;
177 }
178}
179
180void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700181{
182 unsigned n;
183
184 spin_lock_init(&clocks_lock);
185 mutex_lock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700186 msm_clocks = clock_tbl;
187 msm_num_clocks = num_clocks;
188 for (n = 0; n < msm_num_clocks; n++) {
189 set_clock_ops(&msm_clocks[n]);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700190 list_add_tail(&msm_clocks[n].list, &clocks);
Daniel Walker5e96da52010-05-12 13:43:28 -0700191 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700192 mutex_unlock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700193
194 ebi1_clk = clk_get(NULL, "ebi1_clk");
195 BUG_ON(ebi1_clk == NULL);
196
Brian Swetland600f7cf2008-09-09 11:04:14 -0700197}
198
Daniel Walker5e96da52010-05-12 13:43:28 -0700199#if defined(CONFIG_DEBUG_FS)
200static struct clk *msm_clock_get_nth(unsigned index)
201{
202 if (index < msm_num_clocks)
203 return msm_clocks + index;
204 else
205 return 0;
206}
207
208static int clock_debug_rate_set(void *data, u64 val)
209{
210 struct clk *clock = data;
211 int ret;
212
213 /* Only increases to max rate will succeed, but that's actually good
214 * for debugging purposes. So we don't check for error. */
215 if (clock->flags & CLK_MAX)
216 clk_set_max_rate(clock, val);
217 if (clock->flags & CLK_MIN)
218 ret = clk_set_min_rate(clock, val);
219 else
220 ret = clk_set_rate(clock, val);
221 if (ret != 0)
222 printk(KERN_ERR "clk_set%s_rate failed (%d)\n",
223 (clock->flags & CLK_MIN) ? "_min" : "", ret);
224 return ret;
225}
226
227static int clock_debug_rate_get(void *data, u64 *val)
228{
229 struct clk *clock = data;
230 *val = clk_get_rate(clock);
231 return 0;
232}
233
234static int clock_debug_enable_set(void *data, u64 val)
235{
236 struct clk *clock = data;
237 int rc = 0;
238
239 if (val)
240 rc = clock->ops->enable(clock->id);
241 else
242 clock->ops->disable(clock->id);
243
244 return rc;
245}
246
247static int clock_debug_enable_get(void *data, u64 *val)
248{
249 struct clk *clock = data;
250
251 *val = clock->ops->is_enabled(clock->id);
252
253 return 0;
254}
255
256static int clock_debug_local_get(void *data, u64 *val)
257{
258 struct clk *clock = data;
259
260 *val = clock->ops != &clk_ops_pcom;
261
262 return 0;
263}
264
265DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
266 clock_debug_rate_set, "%llu\n");
267DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
268 clock_debug_enable_set, "%llu\n");
269DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
270 NULL, "%llu\n");
271
272static int __init clock_debug_init(void)
273{
274 struct dentry *dent_rate, *dent_enable, *dent_local;
275 struct clk *clock;
276 unsigned n = 0;
277 char temp[50], *ptr;
278
279 dent_rate = debugfs_create_dir("clk_rate", 0);
280 if (IS_ERR(dent_rate))
281 return PTR_ERR(dent_rate);
282
283 dent_enable = debugfs_create_dir("clk_enable", 0);
284 if (IS_ERR(dent_enable))
285 return PTR_ERR(dent_enable);
286
287 dent_local = debugfs_create_dir("clk_local", NULL);
288 if (IS_ERR(dent_local))
289 return PTR_ERR(dent_local);
290
291 while ((clock = msm_clock_get_nth(n++)) != 0) {
292 strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
293 for (ptr = temp; *ptr; ptr++)
294 *ptr = tolower(*ptr);
295 debugfs_create_file(temp, 0644, dent_rate,
296 clock, &clock_rate_fops);
297 debugfs_create_file(temp, 0644, dent_enable,
298 clock, &clock_enable_fops);
299 debugfs_create_file(temp, S_IRUGO, dent_local,
300 clock, &clock_local_fops);
301 }
302 return 0;
303}
304
305device_initcall(clock_debug_init);
306#endif
307
Brian Swetland600f7cf2008-09-09 11:04:14 -0700308/* The bootloader and/or AMSS may have left various clocks enabled.
309 * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
310 * not been explicitly enabled by a clk_enable() call.
311 */
312static int __init clock_late_init(void)
313{
314 unsigned long flags;
315 struct clk *clk;
316 unsigned count = 0;
317
318 mutex_lock(&clocks_mutex);
319 list_for_each_entry(clk, &clocks, list) {
320 if (clk->flags & CLKFLAG_AUTO_OFF) {
321 spin_lock_irqsave(&clocks_lock, flags);
322 if (!clk->count) {
323 count++;
Daniel Walker5e96da52010-05-12 13:43:28 -0700324 clk->ops->auto_off(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700325 }
326 spin_unlock_irqrestore(&clocks_lock, flags);
327 }
328 }
329 mutex_unlock(&clocks_mutex);
330 pr_info("clock_late_init() disabled %d unused clocks\n", count);
331 return 0;
332}
333
334late_initcall(clock_late_init);
Daniel Walker5e96da52010-05-12 13:43:28 -0700335