blob: 9cb1276ab749577fa1689a4fa393415856ff2e04 [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"
Gregory Bean37a298f2010-04-30 22:22:07 -070032#include "clock-7x30.h"
Brian Swetland600f7cf2008-09-09 11:04:14 -070033
34static DEFINE_MUTEX(clocks_mutex);
35static DEFINE_SPINLOCK(clocks_lock);
36static LIST_HEAD(clocks);
Daniel Walker5e96da52010-05-12 13:43:28 -070037struct clk *msm_clocks;
38unsigned msm_num_clocks;
Brian Swetland600f7cf2008-09-09 11:04:14 -070039
40/*
Daniel Walker5e96da52010-05-12 13:43:28 -070041 * Bitmap of enabled clocks, excluding ACPU which is always
42 * enabled
Brian Swetland600f7cf2008-09-09 11:04:14 -070043 */
Daniel Walker5e96da52010-05-12 13:43:28 -070044static DECLARE_BITMAP(clock_map_enabled, NR_CLKS);
45static DEFINE_SPINLOCK(clock_map_lock);
Brian Swetland600f7cf2008-09-09 11:04:14 -070046
47/*
48 * Standard clock functions defined in include/linux/clk.h
49 */
50struct clk *clk_get(struct device *dev, const char *id)
51{
52 struct clk *clk;
53
54 mutex_lock(&clocks_mutex);
55
56 list_for_each_entry(clk, &clocks, list)
57 if (!strcmp(id, clk->name) && clk->dev == dev)
58 goto found_it;
59
60 list_for_each_entry(clk, &clocks, list)
61 if (!strcmp(id, clk->name) && clk->dev == NULL)
62 goto found_it;
63
64 clk = ERR_PTR(-ENOENT);
65found_it:
66 mutex_unlock(&clocks_mutex);
67 return clk;
68}
69EXPORT_SYMBOL(clk_get);
70
71void clk_put(struct clk *clk)
72{
73}
74EXPORT_SYMBOL(clk_put);
75
76int clk_enable(struct clk *clk)
77{
78 unsigned long flags;
79 spin_lock_irqsave(&clocks_lock, flags);
80 clk->count++;
Daniel Walker5e96da52010-05-12 13:43:28 -070081 if (clk->count == 1) {
82 clk->ops->enable(clk->id);
83 spin_lock(&clock_map_lock);
84 clock_map_enabled[BIT_WORD(clk->id)] |= BIT_MASK(clk->id);
85 spin_unlock(&clock_map_lock);
86 }
Brian Swetland600f7cf2008-09-09 11:04:14 -070087 spin_unlock_irqrestore(&clocks_lock, flags);
88 return 0;
89}
90EXPORT_SYMBOL(clk_enable);
91
92void clk_disable(struct clk *clk)
93{
94 unsigned long flags;
95 spin_lock_irqsave(&clocks_lock, flags);
96 BUG_ON(clk->count == 0);
97 clk->count--;
Daniel Walker5e96da52010-05-12 13:43:28 -070098 if (clk->count == 0) {
99 clk->ops->disable(clk->id);
100 spin_lock(&clock_map_lock);
101 clock_map_enabled[BIT_WORD(clk->id)] &= ~BIT_MASK(clk->id);
102 spin_unlock(&clock_map_lock);
103 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700104 spin_unlock_irqrestore(&clocks_lock, flags);
105}
106EXPORT_SYMBOL(clk_disable);
107
Daniel Walker5e96da52010-05-12 13:43:28 -0700108int clk_reset(struct clk *clk, enum clk_reset_action action)
109{
110 if (!clk->ops->reset)
111 clk->ops->reset = &pc_clk_reset;
112 return clk->ops->reset(clk->remote_id, action);
113}
114EXPORT_SYMBOL(clk_reset);
115
Brian Swetland600f7cf2008-09-09 11:04:14 -0700116unsigned long clk_get_rate(struct clk *clk)
117{
Daniel Walker5e96da52010-05-12 13:43:28 -0700118 return clk->ops->get_rate(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700119}
120EXPORT_SYMBOL(clk_get_rate);
121
122int clk_set_rate(struct clk *clk, unsigned long rate)
123{
Daniel Walker5e96da52010-05-12 13:43:28 -0700124 return clk->ops->set_rate(clk->id, rate);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700125}
126EXPORT_SYMBOL(clk_set_rate);
127
Daniel Walker5e96da52010-05-12 13:43:28 -0700128long clk_round_rate(struct clk *clk, unsigned long rate)
129{
130 return clk->ops->round_rate(clk->id, rate);
131}
132EXPORT_SYMBOL(clk_round_rate);
133
134int clk_set_min_rate(struct clk *clk, unsigned long rate)
135{
136 return clk->ops->set_min_rate(clk->id, rate);
137}
138EXPORT_SYMBOL(clk_set_min_rate);
139
140int clk_set_max_rate(struct clk *clk, unsigned long rate)
141{
142 return clk->ops->set_max_rate(clk->id, rate);
143}
144EXPORT_SYMBOL(clk_set_max_rate);
145
Brian Swetland600f7cf2008-09-09 11:04:14 -0700146int clk_set_parent(struct clk *clk, struct clk *parent)
147{
148 return -ENOSYS;
149}
150EXPORT_SYMBOL(clk_set_parent);
151
152struct clk *clk_get_parent(struct clk *clk)
153{
154 return ERR_PTR(-ENOSYS);
155}
156EXPORT_SYMBOL(clk_get_parent);
157
158int clk_set_flags(struct clk *clk, unsigned long flags)
159{
160 if (clk == NULL || IS_ERR(clk))
161 return -EINVAL;
Daniel Walker5e96da52010-05-12 13:43:28 -0700162 return clk->ops->set_flags(clk->id, flags);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700163}
164EXPORT_SYMBOL(clk_set_flags);
165
Daniel Walker5e96da52010-05-12 13:43:28 -0700166/* EBI1 is the only shared clock that several clients want to vote on as of
167 * this commit. If this changes in the future, then it might be better to
168 * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
169 * generic to support different clocks.
170 */
171static struct clk *ebi1_clk;
Brian Swetland600f7cf2008-09-09 11:04:14 -0700172
Daniel Walker5e96da52010-05-12 13:43:28 -0700173static void __init set_clock_ops(struct clk *clk)
174{
175 if (!clk->ops) {
176 clk->ops = &clk_ops_pcom;
177 clk->id = clk->remote_id;
178 }
179}
180
181void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
Brian Swetland600f7cf2008-09-09 11:04:14 -0700182{
183 unsigned n;
184
185 spin_lock_init(&clocks_lock);
186 mutex_lock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700187 msm_clocks = clock_tbl;
188 msm_num_clocks = num_clocks;
189 for (n = 0; n < msm_num_clocks; n++) {
190 set_clock_ops(&msm_clocks[n]);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700191 list_add_tail(&msm_clocks[n].list, &clocks);
Daniel Walker5e96da52010-05-12 13:43:28 -0700192 }
Brian Swetland600f7cf2008-09-09 11:04:14 -0700193 mutex_unlock(&clocks_mutex);
Daniel Walker5e96da52010-05-12 13:43:28 -0700194
195 ebi1_clk = clk_get(NULL, "ebi1_clk");
196 BUG_ON(ebi1_clk == NULL);
197
Brian Swetland600f7cf2008-09-09 11:04:14 -0700198}
199
Daniel Walker5e96da52010-05-12 13:43:28 -0700200#if defined(CONFIG_DEBUG_FS)
201static struct clk *msm_clock_get_nth(unsigned index)
202{
203 if (index < msm_num_clocks)
204 return msm_clocks + index;
205 else
206 return 0;
207}
208
209static int clock_debug_rate_set(void *data, u64 val)
210{
211 struct clk *clock = data;
212 int ret;
213
214 /* Only increases to max rate will succeed, but that's actually good
215 * for debugging purposes. So we don't check for error. */
216 if (clock->flags & CLK_MAX)
217 clk_set_max_rate(clock, val);
218 if (clock->flags & CLK_MIN)
219 ret = clk_set_min_rate(clock, val);
220 else
221 ret = clk_set_rate(clock, val);
222 if (ret != 0)
223 printk(KERN_ERR "clk_set%s_rate failed (%d)\n",
224 (clock->flags & CLK_MIN) ? "_min" : "", ret);
225 return ret;
226}
227
228static int clock_debug_rate_get(void *data, u64 *val)
229{
230 struct clk *clock = data;
231 *val = clk_get_rate(clock);
232 return 0;
233}
234
235static int clock_debug_enable_set(void *data, u64 val)
236{
237 struct clk *clock = data;
238 int rc = 0;
239
240 if (val)
241 rc = clock->ops->enable(clock->id);
242 else
243 clock->ops->disable(clock->id);
244
245 return rc;
246}
247
248static int clock_debug_enable_get(void *data, u64 *val)
249{
250 struct clk *clock = data;
251
252 *val = clock->ops->is_enabled(clock->id);
253
254 return 0;
255}
256
257static int clock_debug_local_get(void *data, u64 *val)
258{
259 struct clk *clock = data;
260
261 *val = clock->ops != &clk_ops_pcom;
262
263 return 0;
264}
265
266DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
267 clock_debug_rate_set, "%llu\n");
268DEFINE_SIMPLE_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
269 clock_debug_enable_set, "%llu\n");
270DEFINE_SIMPLE_ATTRIBUTE(clock_local_fops, clock_debug_local_get,
271 NULL, "%llu\n");
272
273static int __init clock_debug_init(void)
274{
275 struct dentry *dent_rate, *dent_enable, *dent_local;
276 struct clk *clock;
277 unsigned n = 0;
278 char temp[50], *ptr;
279
280 dent_rate = debugfs_create_dir("clk_rate", 0);
281 if (IS_ERR(dent_rate))
282 return PTR_ERR(dent_rate);
283
284 dent_enable = debugfs_create_dir("clk_enable", 0);
285 if (IS_ERR(dent_enable))
286 return PTR_ERR(dent_enable);
287
288 dent_local = debugfs_create_dir("clk_local", NULL);
289 if (IS_ERR(dent_local))
290 return PTR_ERR(dent_local);
291
292 while ((clock = msm_clock_get_nth(n++)) != 0) {
293 strncpy(temp, clock->dbg_name, ARRAY_SIZE(temp)-1);
294 for (ptr = temp; *ptr; ptr++)
295 *ptr = tolower(*ptr);
296 debugfs_create_file(temp, 0644, dent_rate,
297 clock, &clock_rate_fops);
298 debugfs_create_file(temp, 0644, dent_enable,
299 clock, &clock_enable_fops);
300 debugfs_create_file(temp, S_IRUGO, dent_local,
301 clock, &clock_local_fops);
302 }
303 return 0;
304}
305
306device_initcall(clock_debug_init);
307#endif
308
Brian Swetland600f7cf2008-09-09 11:04:14 -0700309/* The bootloader and/or AMSS may have left various clocks enabled.
310 * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
311 * not been explicitly enabled by a clk_enable() call.
312 */
313static int __init clock_late_init(void)
314{
315 unsigned long flags;
316 struct clk *clk;
317 unsigned count = 0;
318
319 mutex_lock(&clocks_mutex);
320 list_for_each_entry(clk, &clocks, list) {
321 if (clk->flags & CLKFLAG_AUTO_OFF) {
322 spin_lock_irqsave(&clocks_lock, flags);
323 if (!clk->count) {
324 count++;
Daniel Walker5e96da52010-05-12 13:43:28 -0700325 clk->ops->auto_off(clk->id);
Brian Swetland600f7cf2008-09-09 11:04:14 -0700326 }
327 spin_unlock_irqrestore(&clocks_lock, flags);
328 }
329 }
330 mutex_unlock(&clocks_mutex);
331 pr_info("clock_late_init() disabled %d unused clocks\n", count);
332 return 0;
333}
334
335late_initcall(clock_late_init);
Daniel Walker5e96da52010-05-12 13:43:28 -0700336