-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled.c
More file actions
275 lines (227 loc) · 6.05 KB
/
led.c
File metadata and controls
275 lines (227 loc) · 6.05 KB
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define BCM2708_PERI_BASE 0x3f000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
#define GPIO_REGION_SIZE 0x3c
#define GPSET_OFFSET 0x1c
#define GPCLR_OFFSET 0x28
#define GPLEV_OFFSET 0x34
#define GPIO_DEFAULT 6
#define NUM_GPIOS 28
#define HIGH 1
#define LOW 0
#define MODULE_NAME "simple-led"
#define NUM_DEVICES 1
#define BUF_SIZE 2
/*
* Device file operations
*/
static ssize_t
led_read(struct file *filep, char __user *buf, size_t len, loff_t *off);
static ssize_t
led_write(struct file *filep, const char __user *buf, size_t len, loff_t *off);
static int led_open(struct inode *inodep, struct file *filep);
static int led_release(struct inode *inodep, struct file *filep);
/*
* Helper functions
*/
static bool is_gpio_valid(void);
static void save_gpio_func_select(void);
static void restore_gpio_func_select(void);
static void pin_direction_output(void);
static void set_pin(void);
static void unset_pin(void);
static void read_pin(void);
static dev_t devt;
static struct cdev led_cdev;
static struct class *led_class;
static struct device *led_device;
/*
* iomap will point to the first GPIO register. Since this is a void pointer,
* incrementing it will increment the address by one byte.
* Strictly speaking, such arithmetic is not defined in the C standard; it is
* allowed by GCC by means of an extension.
* See: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
*/
static void __iomem *iomap;
static int func_select_reg_offset;
static int func_select_bit_offset;
static char pin_value[BUF_SIZE] = "";
static int func_select_initial_val;
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.read = led_read,
.write = led_write,
};
/* Only one process at a time will be allowed to work with the LED */
static DEFINE_MUTEX(led_mutex);
static int gpio_num = GPIO_DEFAULT;
module_param(gpio_num, int, S_IRUGO);
MODULE_PARM_DESC(gpio_num, "The gpio where the LED is connected (default = 6)");
static int __init init_led(void)
{
int ret;
if (!is_gpio_valid()) {
pr_err("%s: Invalid GPIO (%d)\n", MODULE_NAME, gpio_num);
ret = -EINVAL;
goto out;
}
ret = alloc_chrdev_region(&devt, 0, NUM_DEVICES, MODULE_NAME);
if (ret) {
pr_err("%s: Failed to allocate char device region.\n",
MODULE_NAME);
goto out;
}
cdev_init(&led_cdev, &fops);
led_cdev.owner = THIS_MODULE;
ret = cdev_add(&led_cdev, devt, NUM_DEVICES);
if (ret) {
pr_err("%s: Failed to add cdev.\n", MODULE_NAME);
goto cdev_err;
}
led_class = class_create(THIS_MODULE, "led-test");
if (IS_ERR(led_class)) {
pr_err("%s: class_create() failed.\n", MODULE_NAME);
ret = PTR_ERR(led_class);
goto class_err;
}
led_device = device_create(led_class, NULL, devt, NULL, MODULE_NAME);
if (IS_ERR(led_device)) {
pr_err("%s: device_create() failed.\n", MODULE_NAME);
ret = PTR_ERR(led_device);
goto dev_err;
}
iomap = ioremap(GPIO_BASE, GPIO_REGION_SIZE);
if (!iomap) {
pr_err("%s: ioremap() failed.\n", MODULE_NAME);
ret = -EINVAL;
goto remap_err;
}
func_select_reg_offset = 4 * (gpio_num / 10);
func_select_bit_offset = (gpio_num % 10) * 3;
save_gpio_func_select();
pin_direction_output();
mutex_init(&led_mutex);
pr_info("%s: Module loaded\n", MODULE_NAME);
goto out;
remap_err:
device_destroy(led_class, devt);
dev_err:
class_unregister(led_class);
class_destroy(led_class);
class_err:
cdev_del(&led_cdev);
cdev_err:
unregister_chrdev_region(devt, NUM_DEVICES);
out:
return ret;
}
static void __exit exit_led(void)
{
unset_pin();
restore_gpio_func_select();
mutex_destroy(&led_mutex);
iounmap(iomap);
device_destroy(led_class, devt);
class_unregister(led_class);
class_destroy(led_class);
cdev_del(&led_cdev);
unregister_chrdev_region(devt, NUM_DEVICES);
pr_info("%s: Module unloaded\n", MODULE_NAME);
}
static int led_open(struct inode *inodep, struct file *filep)
{
mutex_lock(&led_mutex);
return 0;
}
static int led_release(struct inode *inodep, struct file *filep)
{
mutex_unlock(&led_mutex);
return 0;
}
static ssize_t
led_read(struct file *filep, char __user *buf, size_t len, loff_t *off)
{
int err;
if (!access_ok(VERIFY_WRITE, buf, len)) {
pr_err("%s: Cannot access user buffer for writing\n",
MODULE_NAME);
return -EFAULT;
}
read_pin();
pr_info("%s: led_read = %s\n", MODULE_NAME, pin_value);
err = copy_to_user(buf, pin_value, BUF_SIZE);
if (err)
return -EFAULT;
return 0;
}
static ssize_t
led_write(struct file *filep, const char __user *buf, size_t len, loff_t *off)
{
char kbuf[BUF_SIZE];
int err;
err = copy_from_user(kbuf, buf, BUF_SIZE);
kbuf[1] = '\0';
if (strcmp(kbuf, "0") == 0) {
unset_pin();
} else {
set_pin();
}
return BUF_SIZE;
}
static bool is_gpio_valid(void)
{
return 0 <= gpio_num && gpio_num < NUM_GPIOS;
}
static void save_gpio_func_select(void)
{
int val;
val = ioread32(iomap + func_select_reg_offset);
func_select_initial_val = (val >> func_select_bit_offset) & 7;
}
static void restore_gpio_func_select(void)
{
int val;
val = ioread32(iomap + func_select_reg_offset);
val &= ~(7 << func_select_bit_offset);
val |= func_select_initial_val << func_select_bit_offset;
iowrite32(val, iomap + func_select_reg_offset);
}
static void pin_direction_output(void)
{
int val;
val = ioread32(iomap + func_select_reg_offset);
val &= ~(6 << func_select_bit_offset);
val |= 1 << func_select_bit_offset;
iowrite32(val, iomap + func_select_reg_offset);
}
static void set_pin(void)
{
iowrite32(1 << gpio_num, iomap + GPSET_OFFSET);
}
static void unset_pin(void)
{
iowrite32(1 << gpio_num, iomap + GPCLR_OFFSET);
}
static void read_pin(void)
{
int val;
val = ioread32(iomap + GPLEV_OFFSET);
val = (val >> gpio_num) & 1;
pin_value[0] = val ? '1' : '0';
}
module_init(init_led);
module_exit(exit_led);
MODULE_AUTHOR("Filip Kolev");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("Very simple driver to control a LED using ioremap()");