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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/*
* mods_clock.c - This file is part of NVIDIA MODS kernel driver.
*
* Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA MODS kernel driver 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.
*
* NVIDIA MODS kernel driver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NVIDIA MODS kernel driver.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "mods_internal.h"
#include <linux/clk.h>
#include <mach/clk.h>
#include <../arch/arm/mach-tegra/clock.h>
static struct list_head mods_clock_handles;
static spinlock_t mods_clock_lock;
static NvU32 last_handle;
struct clock_entry {
struct clk *pclk;
NvU32 handle;
struct list_head list;
};
void mods_init_clock_api(void)
{
spin_lock_init(&mods_clock_lock);
INIT_LIST_HEAD(&mods_clock_handles);
last_handle = 0;
}
void mods_shutdown_clock_api(void)
{
struct list_head *head = &mods_clock_handles;
struct list_head *iter;
struct list_head *tmp;
spin_lock(&mods_clock_lock);
list_for_each_safe(iter, tmp, head) {
struct clock_entry *entry
= list_entry(iter, struct clock_entry, list);
list_del(iter);
MEMDBG_FREE(entry);
}
spin_unlock(&mods_clock_lock);
}
static NvU32 mods_get_clock_handle(struct clk *pclk)
{
struct list_head *head = &mods_clock_handles;
struct list_head *iter;
struct clock_entry *entry = 0;
NvU32 handle = 0;
spin_lock(&mods_clock_lock);
list_for_each(iter, head) {
struct clock_entry *cur
= list_entry(iter, struct clock_entry, list);
if (cur->pclk == pclk) {
entry = cur;
handle = cur->handle;
break;
}
}
if (!entry) {
MEMDBG_ALLOC(entry, sizeof(*entry));
if (!unlikely(!entry)) {
entry->pclk = pclk;
entry->handle = ++last_handle;
handle = entry->handle;
list_add(&entry->list, &mods_clock_handles);
}
}
spin_unlock(&mods_clock_lock);
return handle;
}
static struct clk *mods_get_clock(NvU32 handle)
{
struct list_head *head = &mods_clock_handles;
struct list_head *iter;
struct clk *pclk = 0;
spin_lock(&mods_clock_lock);
list_for_each(iter, head) {
struct clock_entry *entry
= list_entry(iter, struct clock_entry, list);
if (entry->handle == handle) {
pclk = entry->pclk;
break;
}
}
spin_unlock(&mods_clock_lock);
return pclk;
}
int esc_mods_get_clock_handle(struct file *pfile,
struct MODS_GET_CLOCK_HANDLE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
p->device_name[sizeof(p->device_name)-1] = 0;
p->controller_name[sizeof(p->controller_name)-1] = 0;
pclk = clk_get_sys(p->device_name, p->controller_name);
if (IS_ERR(pclk)) {
mods_error_printk("invalid clock specified: dev=%s, ctx=%s\n",
p->device_name, p->controller_name);
} else {
p->clock_handle = mods_get_clock_handle(pclk);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_set_clock_rate(struct file *pfile, struct MODS_CLOCK_RATE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
ret = clk_set_rate(pclk, p->clock_rate_hz);
if (ret) {
mods_error_printk(
"unable to set rate %lluHz on clock 0x%x\n",
p->clock_rate_hz, p->clock_handle);
} else {
mods_debug_printk(DEBUG_CLOCK,
"successfuly set rate %lluHz on clock 0x%x\n",
p->clock_rate_hz, p->clock_handle);
}
}
LOG_EXT();
return ret;
}
int esc_mods_get_clock_rate(struct file *pfile, struct MODS_CLOCK_RATE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
p->clock_rate_hz = clk_get_rate(pclk);
mods_debug_printk(DEBUG_CLOCK, "clock 0x%x has rate %lluHz\n",
p->clock_handle, p->clock_rate_hz);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_get_clock_max_rate(struct file *pfile, struct MODS_CLOCK_RATE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else if (!pclk->ops || !pclk->ops->round_rate) {
mods_error_printk(
"unable to detect max rate for clock handle 0x%x\n",
p->clock_handle);
} else {
long rate = pclk->ops->round_rate(pclk, pclk->max_rate);
p->clock_rate_hz = rate < 0 ? pclk->max_rate
: (unsigned long)rate;
mods_debug_printk(DEBUG_CLOCK,
"clock 0x%x has max rate %lluHz\n",
p->clock_handle, p->clock_rate_hz);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_set_clock_max_rate(struct file *pfile, struct MODS_CLOCK_RATE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
#ifdef CONFIG_TEGRA_CLOCK_DEBUG_FUNC
ret = tegra_clk_set_max(pclk, p->clock_rate_hz);
if (ret) {
mods_error_printk(
"unable to override max clock rate %lluHz on clock 0x%x\n",
p->clock_rate_hz, p->clock_handle);
} else {
mods_debug_printk(DEBUG_CLOCK,
"successfuly set max rate %lluHz on clock 0x%x\n",
p->clock_rate_hz, p->clock_handle);
}
#else
mods_error_printk("unable to override max clock rate\n");
mods_error_printk(
"reconfigure kernel with CONFIG_TEGRA_CLOCK_DEBUG_FUNC=y\n");
ret = -ENOSYS;
#endif
}
LOG_EXT();
return ret;
}
int esc_mods_set_clock_parent(struct file *pfile, struct MODS_CLOCK_PARENT *p)
{
struct clk *pclk = 0;
struct clk *pparent = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
pparent = mods_get_clock(p->clock_parent_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else if (!pparent) {
mods_error_printk("unrecognized parent clock handle: 0x%x\n",
p->clock_parent_handle);
} else {
ret = clk_set_parent(pclk, pparent);
if (ret) {
mods_error_printk(
"unable to make clock 0x%x parent of clock 0x%x\n",
p->clock_parent_handle, p->clock_handle);
} else {
mods_debug_printk(DEBUG_CLOCK,
"successfuly made clock 0x%x parent of clock 0x%x\n",
p->clock_parent_handle, p->clock_handle);
}
}
LOG_EXT();
return ret;
}
int esc_mods_get_clock_parent(struct file *pfile, struct MODS_CLOCK_PARENT *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
struct clk *pparent = clk_get_parent(pclk);
p->clock_parent_handle = mods_get_clock_handle(pparent);
mods_debug_printk(DEBUG_CLOCK,
"clock 0x%x is parent of clock 0x%x\n",
p->clock_parent_handle, p->clock_handle);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_enable_clock(struct file *pfile, struct MODS_CLOCK_HANDLE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
ret = clk_enable(pclk);
if (ret) {
mods_error_printk("unable to enable clock 0x%x\n",
p->clock_handle);
} else {
mods_debug_printk(DEBUG_CLOCK, "clock 0x%x enabled\n",
p->clock_handle);
}
}
LOG_EXT();
return ret;
}
int esc_mods_disable_clock(struct file *pfile, struct MODS_CLOCK_HANDLE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
clk_disable(pclk);
mods_debug_printk(DEBUG_CLOCK, "clock 0x%x disabled\n",
p->clock_handle);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_is_clock_enabled(struct file *pfile, struct MODS_CLOCK_ENABLED *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
p->enable_count = pclk->refcnt;
mods_debug_printk(DEBUG_CLOCK,
"clock 0x%x enable count is %u\n",
p->clock_handle, p->enable_count);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_clock_reset_assert(struct file *pfile,
struct MODS_CLOCK_HANDLE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
tegra_periph_reset_assert(pclk);
mods_debug_printk(DEBUG_CLOCK, "clock 0x%x reset asserted\n",
p->clock_handle);
ret = OK;
}
LOG_EXT();
return ret;
}
int esc_mods_clock_reset_deassert(struct file *pfile,
struct MODS_CLOCK_HANDLE *p)
{
struct clk *pclk = 0;
int ret = -EINVAL;
LOG_ENT();
pclk = mods_get_clock(p->clock_handle);
if (!pclk) {
mods_error_printk("unrecognized clock handle: 0x%x\n",
p->clock_handle);
} else {
tegra_periph_reset_deassert(pclk);
mods_debug_printk(DEBUG_CLOCK, "clock 0x%x reset deasserted\n",
p->clock_handle);
ret = OK;
}
LOG_EXT();
return ret;
}
|