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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*
* Copyright (C) 2009 ST-Ericsson
* Copyright (C) 2009 STMicroelectronics
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <linux/mfd/ab8500/sysctrl.h>
#include <mach/prcmu.h>
#include "clock.h"
#define PRCC_PCKEN 0x0
#define PRCC_PCKDIS 0x4
#define PRCC_KCKEN 0x8
#define PRCC_KCKDIS 0xC
#define PRCC_PCKSR 0x10
#define PRCC_KCKSR 0x14
DEFINE_MUTEX(clk_opp100_mutex);
static DEFINE_SPINLOCK(clk_spin_lock);
#define NO_LOCK &clk_spin_lock
static void __iomem *prcmu_base;
static void __clk_lock(struct clk *clk, void *last_lock, unsigned long *flags)
{
if (clk->mutex != last_lock) {
if (clk->mutex == NULL)
spin_lock_irqsave(&clk_spin_lock, *flags);
else
mutex_lock(clk->mutex);
}
}
static void __clk_unlock(struct clk *clk, void *last_lock, unsigned long flags)
{
if (clk->mutex != last_lock) {
if (clk->mutex == NULL)
spin_unlock_irqrestore(&clk_spin_lock, flags);
else
mutex_unlock(clk->mutex);
}
}
void __clk_disable(struct clk *clk, void *current_lock)
{
unsigned long flags;
if (clk == NULL)
return;
__clk_lock(clk, current_lock, &flags);
if (clk->enabled && (--clk->enabled == 0)) {
if ((clk->ops != NULL) && (clk->ops->disable != NULL))
clk->ops->disable(clk);
__clk_disable(clk->parent, clk->mutex);
__clk_disable(clk->bus_parent, clk->mutex);
}
__clk_unlock(clk, current_lock, flags);
return;
}
int __clk_enable(struct clk *clk, void *current_lock)
{
int err;
unsigned long flags;
if (clk == NULL)
return 0;
__clk_lock(clk, current_lock, &flags);
if (!clk->enabled) {
err = __clk_enable(clk->bus_parent, clk->mutex);
if (unlikely(err))
goto bus_parent_error;
err = __clk_enable(clk->parent, clk->mutex);
if (unlikely(err))
goto parent_error;
if ((clk->ops != NULL) && (clk->ops->enable != NULL)) {
err = clk->ops->enable(clk);
if (unlikely(err))
goto enable_error;
}
}
clk->enabled++;
__clk_unlock(clk, current_lock, flags);
return 0;
enable_error:
__clk_disable(clk->parent, clk->mutex);
parent_error:
__clk_disable(clk->bus_parent, clk->mutex);
bus_parent_error:
__clk_unlock(clk, current_lock, flags);
return err;
}
unsigned long __clk_get_rate(struct clk *clk, void *current_lock)
{
unsigned long rate;
unsigned long flags;
if (clk == NULL)
return 0;
__clk_lock(clk, current_lock, &flags);
if ((clk->ops != NULL) && (clk->ops->get_rate != NULL))
rate = clk->ops->get_rate(clk);
else if (clk->rate)
rate = clk->rate;
else
rate = __clk_get_rate(clk->parent, clk->mutex);
__clk_unlock(clk, current_lock, flags);
return rate;
}
static unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
{
if ((clk->ops != NULL) && (clk->ops->round_rate != NULL))
return clk->ops->round_rate(clk, rate);
return -ENOSYS;
}
static int __clk_set_rate(struct clk *clk, unsigned long rate)
{
if ((clk->ops != NULL) && (clk->ops->set_rate != NULL))
return clk->ops->set_rate(clk, rate);
return -ENOSYS;
}
int clk_enable(struct clk *clk)
{
if (clk == NULL)
return -EINVAL;
return __clk_enable(clk, NO_LOCK);
}
EXPORT_SYMBOL(clk_enable);
void clk_disable(struct clk *clk)
{
if (clk == NULL)
return;
WARN_ON(!clk->enabled);
__clk_disable(clk, NO_LOCK);
}
EXPORT_SYMBOL(clk_disable);
unsigned long clk_get_rate(struct clk *clk)
{
if (clk == NULL)
return 0;
return __clk_get_rate(clk, NO_LOCK);
}
EXPORT_SYMBOL(clk_get_rate);
long clk_round_rate(struct clk *clk, unsigned long rate)
{
unsigned long flags;
if (clk == NULL)
return -EINVAL;
__clk_lock(clk, NO_LOCK, &flags);
rate = __clk_round_rate(clk, rate);
__clk_unlock(clk, NO_LOCK, flags);
return rate;
}
EXPORT_SYMBOL(clk_round_rate);
int clk_set_rate(struct clk *clk, unsigned long rate)
{
int err;
unsigned long flags;
if (clk == NULL)
return -EINVAL;
__clk_lock(clk, NO_LOCK, &flags);
err = __clk_set_rate(clk, rate);
__clk_unlock(clk, NO_LOCK, flags);
return err;
}
EXPORT_SYMBOL(clk_set_rate);
int clk_set_parent(struct clk *clk, struct clk *parent)
{
int err = 0;
unsigned long flags;
struct clk **p;
if ((clk == NULL) || (clk->parents == NULL))
return -EINVAL;
for (p = clk->parents; *p != parent; p++) {
if (*p == NULL) /* invalid parent */
return -EINVAL;
}
__clk_lock(clk, NO_LOCK, &flags);
if ((clk->ops != NULL) && (clk->ops->set_parent != NULL)) {
err = clk->ops->set_parent(clk, parent);
if (err)
goto unlock_and_return;
} else if (clk->enabled) {
err = __clk_enable(parent, clk->mutex);
if (err)
goto unlock_and_return;
__clk_disable(clk->parent, clk->mutex);
}
clk->parent = parent;
unlock_and_return:
__clk_unlock(clk, NO_LOCK, flags);
return err;
}
/* PRCMU clock operations. */
static int prcmu_clk_enable(struct clk *clk)
{
return prcmu_request_clock(clk->cg_sel, true);
}
static void prcmu_clk_disable(struct clk *clk)
{
if (prcmu_request_clock(clk->cg_sel, false)) {
pr_err("clock: %s failed to disable %s.\n", __func__,
clk->name);
}
}
static int request_ape_opp100(bool enable)
{
static unsigned int requests;
if (enable) {
if (0 == requests++) {
return prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP,
"clock", 100);
}
} else if (1 == requests--) {
prcmu_qos_remove_requirement(PRCMU_QOS_APE_OPP, "clock");
}
return 0;
}
static int prcmu_opp100_clk_enable(struct clk *clk)
{
int r;
r = request_ape_opp100(true);
if (r) {
pr_err("clock: %s failed to request APE OPP 100%% for %s.\n",
__func__, clk->name);
return r;
}
return prcmu_request_clock(clk->cg_sel, true);
}
static void prcmu_opp100_clk_disable(struct clk *clk)
{
if (prcmu_request_clock(clk->cg_sel, false))
goto out_error;
if (request_ape_opp100(false))
goto out_error;
return;
out_error:
pr_err("clock: %s failed to disable %s.\n", __func__, clk->name);
}
struct clkops prcmu_clk_ops = {
.enable = prcmu_clk_enable,
.disable = prcmu_clk_disable,
};
struct clkops prcmu_opp100_clk_ops = {
.enable = prcmu_opp100_clk_enable,
.disable = prcmu_opp100_clk_disable,
};
/* PRCC clock operations. */
static int prcc_pclk_enable(struct clk *clk)
{
void __iomem *io_base = __io_address(clk->io_base);
writel(clk->cg_sel, (io_base + PRCC_PCKEN));
while (!(readl(io_base + PRCC_PCKSR) & clk->cg_sel))
cpu_relax();
return 0;
}
static void prcc_pclk_disable(struct clk *clk)
{
void __iomem *io_base = __io_address(clk->io_base);
writel(clk->cg_sel, (io_base + PRCC_PCKDIS));
}
struct clkops prcc_pclk_ops = {
.enable = prcc_pclk_enable,
.disable = prcc_pclk_disable,
};
static int prcc_kclk_enable(struct clk *clk)
{
int err;
void __iomem *io_base = __io_address(clk->io_base);
err = __clk_enable(clk->clock, clk->mutex);
if (err)
return err;
writel(clk->cg_sel, (io_base + PRCC_KCKEN));
while (!(readl(io_base + PRCC_KCKSR) & clk->cg_sel))
cpu_relax();
__clk_disable(clk->clock, clk->mutex);
return 0;
}
static void prcc_kclk_disable(struct clk *clk)
{
void __iomem *io_base = __io_address(clk->io_base);
(void)__clk_enable(clk->clock, clk->mutex);
writel(clk->cg_sel, (io_base + PRCC_KCKDIS));
__clk_disable(clk->clock, clk->mutex);
}
struct clkops prcc_kclk_ops = {
.enable = prcc_kclk_enable,
.disable = prcc_kclk_disable,
};
void clks_register(struct clk_lookup *clks, size_t num)
{
unsigned int i;
for (i = 0; i < num; i++)
clkdev_add(&clks[i]);
}
int __init clk_init(void)
{
if (cpu_is_u8500()) {
prcmu_base = __io_address(U8500_PRCMU_BASE);
} else if (cpu_is_u5500()) {
prcmu_base = __io_address(U5500_PRCMU_BASE);
} else {
pr_err("clock: Unknown DB Asic.\n");
return -EIO;
}
if (cpu_is_u8500())
db8500_clk_init();
else if (cpu_is_u5500())
db5500_clk_init();
return 0;
}
|