-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetclocks.c
307 lines (243 loc) · 7.38 KB
/
jetclocks.c
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
/* SPDX-License-Identifier: GPL-2.0
*
* jetclocks.c - jetclocks kernel module
*
* Based on the NVIDIA MODS kernel driver by NVIDIA CORPORATION.
* The jetclocks kernel module is a facility that provides a friendly API
* for applications in user space to manipulate the clocks in Nvidia
* Jetson Orin machines.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/reset.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "jetclocks.h"
struct jetclocks {
struct cdev cdev;
struct device *dev;
struct class *cls;
struct clk *clk;
};
static unsigned major;
static int is_clock_enabled(const char *clock, struct jetclocks *dev)
{
int ret;
dev->clk = devm_clk_get(dev->dev, clock);
if (IS_ERR(dev->clk)){
return PTR_ERR(dev->clk);
}
ret = __clk_is_enabled(dev->clk);
return ret;
}
static int clock_enable(const char *clock, struct jetclocks *dev)
{
int ret;
dev->clk = devm_clk_get(dev->dev, clock);
if (IS_ERR(dev->clk))
return PTR_ERR(dev->clk);
ret = clk_prepare_enable(dev->clk);
if (ret) {
dev_err(dev->dev, "Clock: %s prepare/enable failed\n", clock);
return ret;
}
return 0;
}
static int clock_disable(const char *clock, struct jetclocks *dev)
{
dev->clk = devm_clk_get(dev->dev, clock);
if (IS_ERR(dev->clk))
return PTR_ERR(dev->clk);
if(__clk_is_enabled(dev->clk))
clk_disable_unprepare(dev->clk);
return 0;
}
static int clock_set_rate(const char *clock, struct jetclocks *dev, unsigned long rate)
{
int ret;
dev->clk = devm_clk_get(dev->dev, clock);
if (IS_ERR(dev->clk))
return PTR_ERR(dev->clk);
ret = clk_set_rate(dev->clk, rate);
if (ret) {
dev_err(dev->dev, "Clock: %s set rate failed\n", clock);
return -EINVAL;
}
return 0;
}
static unsigned long clock_get_rate(const char *clock, struct jetclocks *dev)
{
unsigned long ret;
dev->clk = devm_clk_get(dev->dev, clock);
if (IS_ERR(dev->clk))
return PTR_ERR(dev->clk);
ret = clk_get_rate(dev->clk);
return ret;
}
static int jetclocks_open(struct inode * inode, struct file * filp)
{
struct jetclocks *jetclocks_dev;
jetclocks_dev = container_of(inode->i_cdev, struct jetclocks, cdev);
filp->private_data = jetclocks_dev;
try_module_get(THIS_MODULE);
return 0;
}
static int jetclocks_release(struct inode * inode, struct file * filp)
{
filp->private_data = NULL;
module_put(THIS_MODULE);
return 0;
}
static long jetclocks_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct jetclocks *jetclocks_dev = filp->private_data;
struct jetclk clock = {0};
int ret = 0;
switch(cmd){
case CLK_ENABLE:
if(copy_from_user(&clock, (struct jetclk *) arg, sizeof(clock))) {
pr_err("jetclocks - Error getting clock name\n");
return -EFAULT;
}
ret = clock_enable(clock.clk, jetclocks_dev);
if (!(ret)) {
pr_info("jetclocks - clock %s enabled\n", clock.clk);
}
break;
case CLK_DISABLE:
if(copy_from_user(&clock, (struct jetclk *) arg, sizeof(clock))) {
pr_err("jetclocks - Error getting clock name\n");
return -EFAULT;
}
ret = clock_disable(clock.clk, jetclocks_dev);
if (!(ret)) {
pr_info("jetclocks - clock %s disabled\n", clock.clk);
}
break;
case CLK_IS_ENABLED:
if(copy_from_user(&clock, (struct jetclk *) arg, sizeof(clock))) {
pr_err("jetclocks - Error getting clock name\n");
return -EFAULT;
}
clock.clk_enabled = is_clock_enabled(clock.clk, jetclocks_dev);
if(copy_to_user((struct jetclk *) arg, &clock, sizeof(clock))) {
pr_err("jetclocks - Error sending clock %s status\n", clock.clk);
return -EFAULT;
}
break;
case CLK_SET_RATE:
if(copy_from_user(&clock, (struct jetclk *) arg, sizeof(clock))) {
pr_err("jetclocks - Error getting clock name\n");
return -EFAULT;
}
ret = clock_set_rate(clock.clk, jetclocks_dev, clock.clk_set_rate);
if (!(ret)) {
pr_info("jetclocks - clock %s rate set\n", clock.clk);
}
break;
case CLK_GET_RATE:
if(copy_from_user(&clock, (struct jetclk *) arg, sizeof(clock))) {
pr_err("jetclocks - Error getting clock name\n");
return -EFAULT;
}
clock.clk_rate = clock_get_rate(clock.clk, jetclocks_dev);
if(copy_to_user((struct jetclk *) arg, &clock, sizeof(clock))) {
pr_err("jetclocks - Error sending clock %s rate\n", clock.clk);
return -EFAULT;
}
break;
default:
return -ENOTTY;
}
return ret;
}
struct file_operations jetclocks_fops = {
owner: THIS_MODULE,
open: jetclocks_open,
release: jetclocks_release,
unlocked_ioctl: jetclocks_ioctl,
};
static int jetclocks_probe(struct platform_device *pdev)
{
struct jetclocks *jetclocks_dev;
dev_t major_devt;
struct device *sysdevice;
int ret = 0;
pr_info("Probing jetclocks\n");
jetclocks_dev = devm_kzalloc(&pdev->dev, sizeof(*jetclocks_dev), GFP_KERNEL);
if (!jetclocks_dev)
return -ENOMEM;
jetclocks_dev->dev = &pdev->dev;
platform_set_drvdata(pdev, jetclocks_dev);
/* Character device */
ret = alloc_chrdev_region(&major_devt, 0, 1, "jetclocks");
if (ret < 0) {
pr_err("Device number could not be allocated\n");
return -EINVAL;
}
major = MAJOR(major_devt);
jetclocks_dev->cls = class_create(THIS_MODULE, "jetclocks");
if (IS_ERR(jetclocks_dev->cls)) {
pr_err("Device class can not be created\n");
unregister_chrdev_region(major_devt, 1);
return PTR_ERR(jetclocks_dev->cls);
}
sysdevice = device_create(jetclocks_dev->cls, NULL, major_devt, NULL, "jetclocks");
if (IS_ERR(sysdevice)) {
pr_err("Cannot create device file\n");
class_destroy(jetclocks_dev->cls);
unregister_chrdev_region(major_devt, 1);
return PTR_ERR(sysdevice);
}
cdev_init(&jetclocks_dev->cdev, &jetclocks_fops);
if (cdev_add(&jetclocks_dev->cdev, major_devt, 1) < 0) {
pr_err("Registering of device to kernel failed\n");
device_destroy(jetclocks_dev->cls, major_devt);
class_destroy(jetclocks_dev->cls);
unregister_chrdev_region(major_devt, 1);
return -ENODEV;
}
pr_info("jetclocks module loaded\n");
return 0;
}
static int jetclocks_remove(struct platform_device *pdev)
{
struct jetclocks *jetclocks_dev;
dev_t major_devt = MKDEV(major, 0);
int ret = 0;
pr_info("Removing jetclocks\n");
jetclocks_dev = platform_get_drvdata(pdev);
cdev_del(&jetclocks_dev->cdev);
device_destroy(jetclocks_dev->cls, major_devt);
class_destroy(jetclocks_dev->cls);
unregister_chrdev_region(major_devt, 1);
pr_info("jetclocks module unloaded\n");
return ret;
}
static const struct of_device_id jetclocks_of_match[] = {
{ .compatible = "nvidia,jetclocks"},
{ }
};
MODULE_DEVICE_TABLE(of, jetclocks_of_match);
static struct platform_driver jetclocks_driver = {
.driver = {
.name = "jetclocks",
.of_match_table = jetclocks_of_match,
},
.probe = jetclocks_probe,
.remove = jetclocks_remove,
};
module_platform_driver(jetclocks_driver);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Rubberazer <[email protected]>");
MODULE_DESCRIPTION("Jetson Orin clocks from user space");