1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/clk.h>
28#include <linux/err.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/io.h>
32#include <linux/kernel.h>
33#include <linux/list.h>
34#include <linux/module.h>
35#include <linux/mutex.h>
36#include <linux/platform_device.h>
37#include <linux/proc_fs.h>
38#include <linux/semaphore.h>
39#include <linux/string.h>
40
41#include <mach/clock.h>
42#include <mach/hardware.h>
43
44static LIST_HEAD(clocks);
45static DEFINE_MUTEX(clocks_mutex);
46
47
48
49
50
51static void __clk_disable(struct clk *clk)
52{
53 if (clk == NULL || IS_ERR(clk))
54 return;
55 WARN_ON(!clk->usecount);
56
57 if (!(--clk->usecount)) {
58 if (clk->disable)
59 clk->disable(clk);
60 __clk_disable(clk->parent);
61 __clk_disable(clk->secondary);
62 }
63}
64
65static int __clk_enable(struct clk *clk)
66{
67 if (clk == NULL || IS_ERR(clk))
68 return -EINVAL;
69
70 if (clk->usecount++ == 0) {
71 __clk_enable(clk->parent);
72 __clk_enable(clk->secondary);
73
74 if (clk->enable)
75 clk->enable(clk);
76 }
77 return 0;
78}
79
80
81
82
83int clk_enable(struct clk *clk)
84{
85 int ret = 0;
86
87 if (clk == NULL || IS_ERR(clk))
88 return -EINVAL;
89
90 mutex_lock(&clocks_mutex);
91 ret = __clk_enable(clk);
92 mutex_unlock(&clocks_mutex);
93
94 return ret;
95}
96EXPORT_SYMBOL(clk_enable);
97
98
99
100
101
102void clk_disable(struct clk *clk)
103{
104 if (clk == NULL || IS_ERR(clk))
105 return;
106
107 mutex_lock(&clocks_mutex);
108 __clk_disable(clk);
109 mutex_unlock(&clocks_mutex);
110}
111EXPORT_SYMBOL(clk_disable);
112
113
114
115
116
117
118unsigned long clk_get_rate(struct clk *clk)
119{
120 if (clk == NULL || IS_ERR(clk))
121 return 0UL;
122
123 if (clk->get_rate)
124 return clk->get_rate(clk);
125
126 return clk_get_rate(clk->parent);
127}
128EXPORT_SYMBOL(clk_get_rate);
129
130
131
132
133
134long clk_round_rate(struct clk *clk, unsigned long rate)
135{
136 if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
137 return 0;
138
139 return clk->round_rate(clk, rate);
140}
141EXPORT_SYMBOL(clk_round_rate);
142
143
144
145
146int clk_set_rate(struct clk *clk, unsigned long rate)
147{
148 int ret = -EINVAL;
149
150 if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
151 return ret;
152
153 mutex_lock(&clocks_mutex);
154 ret = clk->set_rate(clk, rate);
155 mutex_unlock(&clocks_mutex);
156
157 return ret;
158}
159EXPORT_SYMBOL(clk_set_rate);
160
161
162int clk_set_parent(struct clk *clk, struct clk *parent)
163{
164 int ret = -EINVAL;
165 struct clk *old;
166
167 if (clk == NULL || IS_ERR(clk) || parent == NULL ||
168 IS_ERR(parent) || clk->set_parent == NULL)
169 return ret;
170
171 if (clk->usecount)
172 clk_enable(parent);
173
174 mutex_lock(&clocks_mutex);
175 ret = clk->set_parent(clk, parent);
176 if (ret == 0) {
177 old = clk->parent;
178 clk->parent = parent;
179 } else {
180 old = parent;
181 }
182 mutex_unlock(&clocks_mutex);
183
184 if (clk->usecount)
185 clk_disable(old);
186
187 return ret;
188}
189EXPORT_SYMBOL(clk_set_parent);
190
191
192struct clk *clk_get_parent(struct clk *clk)
193{
194 struct clk *ret = NULL;
195
196 if (clk == NULL || IS_ERR(clk))
197 return ret;
198
199 return clk->parent;
200}
201EXPORT_SYMBOL(clk_get_parent);
202
203
204
205
206
207
208
209
210
211
212unsigned long mxc_decode_pll(unsigned int reg_val, u32 freq)
213{
214 long long ll;
215 int mfn_abs;
216 unsigned int mfi, mfn, mfd, pd;
217
218 mfi = (reg_val >> 10) & 0xf;
219 mfn = reg_val & 0x3ff;
220 mfd = (reg_val >> 16) & 0x3ff;
221 pd = (reg_val >> 26) & 0xf;
222
223 mfi = mfi <= 5 ? 5 : mfi;
224
225 mfn_abs = mfn;
226
227
228
229
230 if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
231 mfn_abs = 0x400 - mfn;
232
233 freq *= 2;
234 freq /= pd + 1;
235
236 ll = (unsigned long long)freq * mfn_abs;
237
238 do_div(ll, mfd + 1);
239
240 if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
241 ll = -ll;
242
243 ll = (freq * mfi) + ll;
244
245 return ll;
246}
247