msm: clock: Move debugfs code from clock.c to clock-debug.c
[pandora-kernel.git] / arch / arm / mach-msm / clock.c
1 /* arch/arm/mach-msm/clock.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  * Copyright (c) 2007-2010, Code Aurora Forum. All rights reserved.
5  *
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/kernel.h>
18 #include <linux/list.h>
19 #include <linux/err.h>
20 #include <linux/spinlock.h>
21 #include <linux/pm_qos_params.h>
22
23 #include "clock.h"
24 #include "proc_comm.h"
25 #include "clock-7x30.h"
26 #include "clock-pcom.h"
27
28 static DEFINE_MUTEX(clocks_mutex);
29 static DEFINE_SPINLOCK(clocks_lock);
30 static LIST_HEAD(clocks);
31
32 /*
33  * Standard clock functions defined in include/linux/clk.h
34  */
35 struct clk *clk_get(struct device *dev, const char *id)
36 {
37         struct clk *clk;
38
39         mutex_lock(&clocks_mutex);
40
41         list_for_each_entry(clk, &clocks, list)
42                 if (!strcmp(id, clk->name) && clk->dev == dev)
43                         goto found_it;
44
45         list_for_each_entry(clk, &clocks, list)
46                 if (!strcmp(id, clk->name) && clk->dev == NULL)
47                         goto found_it;
48
49         clk = ERR_PTR(-ENOENT);
50 found_it:
51         mutex_unlock(&clocks_mutex);
52         return clk;
53 }
54 EXPORT_SYMBOL(clk_get);
55
56 void clk_put(struct clk *clk)
57 {
58 }
59 EXPORT_SYMBOL(clk_put);
60
61 int clk_enable(struct clk *clk)
62 {
63         unsigned long flags;
64         spin_lock_irqsave(&clocks_lock, flags);
65         clk->count++;
66         if (clk->count == 1)
67                 clk->ops->enable(clk->id);
68         spin_unlock_irqrestore(&clocks_lock, flags);
69         return 0;
70 }
71 EXPORT_SYMBOL(clk_enable);
72
73 void clk_disable(struct clk *clk)
74 {
75         unsigned long flags;
76         spin_lock_irqsave(&clocks_lock, flags);
77         BUG_ON(clk->count == 0);
78         clk->count--;
79         if (clk->count == 0)
80                 clk->ops->disable(clk->id);
81         spin_unlock_irqrestore(&clocks_lock, flags);
82 }
83 EXPORT_SYMBOL(clk_disable);
84
85 int clk_reset(struct clk *clk, enum clk_reset_action action)
86 {
87         if (!clk->ops->reset)
88                 clk->ops->reset = &pc_clk_reset;
89         return clk->ops->reset(clk->remote_id, action);
90 }
91 EXPORT_SYMBOL(clk_reset);
92
93 unsigned long clk_get_rate(struct clk *clk)
94 {
95         return clk->ops->get_rate(clk->id);
96 }
97 EXPORT_SYMBOL(clk_get_rate);
98
99 int clk_set_rate(struct clk *clk, unsigned long rate)
100 {
101         int ret;
102         if (clk->flags & CLKFLAG_MAX) {
103                 ret = clk->ops->set_max_rate(clk->id, rate);
104                 if (ret)
105                         return ret;
106         }
107         if (clk->flags & CLKFLAG_MIN) {
108                 ret = clk->ops->set_min_rate(clk->id, rate);
109                 if (ret)
110                         return ret;
111         }
112
113         if (clk->flags & CLKFLAG_MAX || clk->flags & CLKFLAG_MIN)
114                 return ret;
115
116         return clk->ops->set_rate(clk->id, rate);
117 }
118 EXPORT_SYMBOL(clk_set_rate);
119
120 long clk_round_rate(struct clk *clk, unsigned long rate)
121 {
122         return clk->ops->round_rate(clk->id, rate);
123 }
124 EXPORT_SYMBOL(clk_round_rate);
125
126 int clk_set_min_rate(struct clk *clk, unsigned long rate)
127 {
128         return clk->ops->set_min_rate(clk->id, rate);
129 }
130 EXPORT_SYMBOL(clk_set_min_rate);
131
132 int clk_set_max_rate(struct clk *clk, unsigned long rate)
133 {
134         return clk->ops->set_max_rate(clk->id, rate);
135 }
136 EXPORT_SYMBOL(clk_set_max_rate);
137
138 int clk_set_parent(struct clk *clk, struct clk *parent)
139 {
140         return -ENOSYS;
141 }
142 EXPORT_SYMBOL(clk_set_parent);
143
144 struct clk *clk_get_parent(struct clk *clk)
145 {
146         return ERR_PTR(-ENOSYS);
147 }
148 EXPORT_SYMBOL(clk_get_parent);
149
150 int clk_set_flags(struct clk *clk, unsigned long flags)
151 {
152         if (clk == NULL || IS_ERR(clk))
153                 return -EINVAL;
154         return clk->ops->set_flags(clk->id, flags);
155 }
156 EXPORT_SYMBOL(clk_set_flags);
157
158 /* EBI1 is the only shared clock that several clients want to vote on as of
159  * this commit. If this changes in the future, then it might be better to
160  * make clk_min_rate handle the voting or make ebi1_clk_set_min_rate more
161  * generic to support different clocks.
162  */
163 static struct clk *ebi1_clk;
164
165 static void __init set_clock_ops(struct clk *clk)
166 {
167         if (!clk->ops) {
168                 clk->ops = &clk_ops_pcom;
169                 clk->id = clk->remote_id;
170         }
171 }
172
173 void __init msm_clock_init(struct clk *clock_tbl, unsigned num_clocks)
174 {
175         unsigned n;
176
177         mutex_lock(&clocks_mutex);
178         for (n = 0; n < num_clocks; n++) {
179                 set_clock_ops(&clock_tbl[n]);
180                 list_add_tail(&clock_tbl[n].list, &clocks);
181         }
182         mutex_unlock(&clocks_mutex);
183
184         ebi1_clk = clk_get(NULL, "ebi1_clk");
185         BUG_ON(ebi1_clk == NULL);
186
187 }
188
189 /* The bootloader and/or AMSS may have left various clocks enabled.
190  * Disable any clocks that belong to us (CLKFLAG_AUTO_OFF) but have
191  * not been explicitly enabled by a clk_enable() call.
192  */
193 static int __init clock_late_init(void)
194 {
195         unsigned long flags;
196         struct clk *clk;
197         unsigned count = 0;
198
199         clock_debug_init();
200         mutex_lock(&clocks_mutex);
201         list_for_each_entry(clk, &clocks, list) {
202                 clock_debug_add(clk);
203                 if (clk->flags & CLKFLAG_AUTO_OFF) {
204                         spin_lock_irqsave(&clocks_lock, flags);
205                         if (!clk->count) {
206                                 count++;
207                                 clk->ops->auto_off(clk->id);
208                         }
209                         spin_unlock_irqrestore(&clocks_lock, flags);
210                 }
211         }
212         mutex_unlock(&clocks_mutex);
213         pr_info("clock_late_init() disabled %d unused clocks\n", count);
214         return 0;
215 }
216
217 late_initcall(clock_late_init);
218