-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpio.c
485 lines (446 loc) · 14.6 KB
/
gpio.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <uapi/asm-generic/errno-base.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/time.h>
//------------------------------------------------------/* User-defined macros */-------------------------------------------
#define NUM_GPIO_PINS 19
#define MAX_GPIO_NUMBER 117
#define DEVICE_NAME "bbb-gpio"
#define BUF_SIZE 512
#define INTERRUPT_DEVICE_NAME "gpio interrupt"
//------------------------------------------------------/* User-defined data types */--------------------------------------------
enum state {low, high};
enum direction {in, out};
/*
* struct bbb_gpio_dev -Per gpio pin data structure
* @cdev:instance of struct cdev
* @pin:instance of struct gpio
* @state:logic state (low, high) of a GPIO pin
* @dir:direction of a GPIO pin
* @irq_perm: used to enable/disable interrupt on GPIO pin
* @irq_flag:used to indicate rising/falling edge trigger
* @lock:used to protect atomic code section
*/
struct bbb_gpio_dev
{
struct cdev cdev;
struct gpio pin;
enum state state;
enum direction dir;
bool irq_perm;
unsigned long irq_flag;
unsigned int irq_counter;
spinlock_t lock;
};
//------------------------------------------------------------------/* Declaration of entry points */-------------------------
static int bbb_gpio_open(struct inode *inode, struct file *filp);
static ssize_t bbb_gpio_read (struct file *filp, char *buf, size_t count, loff_t *f_pos);
static ssize_t bbb_gpio_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos);
static int bbb_gpio_release(struct inode *inode, struct file *filp);
//-----------------------------------------------------------------/* File operation structure */------------------------------
static struct file_operations bbb_gpio_fops = {
.owner = THIS_MODULE,
.open= bbb_gpio_open,
.release= bbb_gpio_release,
.read= bbb_gpio_read,
.write= bbb_gpio_write,
};
static int bbb_gpio_init(void);
static void bbb_gpio_exit(void);
unsigned int millis (void);
static irqreturn_t irq_handler(int irq, void *arg);
//----------------------------------------------------------------/*global variables*/---------------------------------------
struct bbb_gpio_dev *bbb_gpio_devp[NUM_GPIO_PINS];
static dev_t first;
static struct class *bbb_gpio_class;
static unsigned int last_interrupt_time = 0;
static uint64_t epochMilli;
/*
* millis - Get current time
*
* This function returns current time in ms. It is primarily used for
* debouncing
*/
unsigned int millis (void)
{
struct timeval timeval ;
uint64_t timeNow ;
do_gettimeofday(&timeval) ;
timeNow=(uint64_t)timeval.tv_sec * (uint64_t)1000 + (uint64_t)(timeval.tv_usec/1000);
return (uint32_t)(timeNow - epochMilli) ;
}
/*
* irq_handler - Interrupt request handler for GPIO pin
*
* This feature is pretty experiment, so more work needs to
* be done to make the feature useful for application
*/
//---------------------------------------------------------/*INT HANDLER*/-----------------------------------------
static irqreturn_t irq_handler(int irq, void *arg)
{
unsigned long flags;
unsigned int interrupt_time = millis();
if (interrupt_time - last_interrupt_time < 200)
{
printk(KERN_NOTICE "Ignored Interrupt [%d]\n", irq);
return IRQ_HANDLED;
}
last_interrupt_time = interrupt_time;
local_irq_save(flags);
printk(KERN_NOTICE "Interrupt [%d] was triggered\n", irq);
local_irq_restore(flags);
return IRQ_HANDLED;
}
/*
* bbb_gpio_open
-
Open GPIO device node in /dev
*
* This function allocates GPIO interrupt resource when requested
* on the condition that interrupt flag is enabled and pin direction
* set to input, then allow the
specified GPIO pin to set interrupt.
*/
//-----------------------------------------------------------/*OPEN METHOD*/-----------------------------------------
static int bbb_gpio_open (struct inode *inode, struct file *filp)
{
struct bbb_gpio_dev *bbb_gpio_devp;
unsigned int gpio;
int err, irq;
unsigned long flags;
gpio = iminor(inode);
printk(KERN_INFO "GPIO[%d] opened\n", gpio);
bbb_gpio_devp = container_of(inode->i_cdev, struct bbb_gpio_dev,cdev);
filp->private_data = bbb_gpio_devp;
if ((bbb_gpio_devp->irq_perm == true) && (bbb_gpio_devp->dir == in))
{
if ((bbb_gpio_devp->irq_counter++ == 0))
{
irq = gpio_to_irq(gpio);
if (bbb_gpio_devp->irq_flag == IRQF_TRIGGER_RISING)
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
err = request_irq(irq,irq_handler,IRQF_SHARED | IRQF_TRIGGER_RISING,
INTERRUPT_DEVICE_NAME,bbb_gpio_devp);
printk(KERN_INFO "interrupt requested\n");
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
else
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
err = request_irq(irq,irq_handler,IRQF_SHARED | IRQF_TRIGGER_FALLING,
INTERRUPT_DEVICE_NAME,bbb_gpio_devp);
printk(KERN_INFO "interrupt requested\n");
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
if (err != 0)
{
printk(KERN_ERR "unable to claim irq: %d, error %d\n", irq,err);
return err;
}
}
}
return 0;
}
//---------------------------------------------------------------------RELEASE METHOD--------------------------------------
/*
* bbb_gpio_release - Release GPIO pin
*
* This functions releases GPIO interrupt resource when the device is
last closed. When requested to disable interrupt, it releases GPIO interrupt
resource regardless of how many devices are using interrupt.
*/
static int bbb_gpio_release (struct inode *inode, struct file *filp)
{
unsigned int gpio;
struct bbb_gpio_dev *bbb_gpio_devp;
bbb_gpio_devp = container_of(inode->i_cdev, struct bbb_gpio_dev,cdev);
gpio = iminor(inode);
printk(KERN_INFO "Closing GPIO %d\n", gpio);
spin_lock(&bbb_gpio_devp->lock);
if (bbb_gpio_devp->irq_perm == true)
{
if (bbb_gpio_devp->irq_counter > 0)
{
bbb_gpio_devp->irq_counter--;
if (bbb_gpio_devp->irq_counter == 0)
{
printk(KERN_INFO "interrupt on gpio[%d] released\n", gpio);
free_irq(gpio_to_irq(gpio), bbb_gpio_devp);
}
}
}
spin_unlock(&bbb_gpio_devp->lock);
if (bbb_gpio_devp->irq_perm == false && bbb_gpio_devp->irq_counter > 0)
{
spin_lock(&bbb_gpio_devp->lock);
free_irq(gpio_to_irq(gpio), bbb_gpio_devp);
bbb_gpio_devp->irq_counter = 0;
spin_unlock(&bbb_gpio_devp->lock);
printk(KERN_INFO "interrupt on gpio[%d] disabled\n", gpio);
}
return 0;
}
//----------------------------------------------------------------------------READ METHOD--------------------------------
/*
* bbb_gpio_read -
Read the state of GPIO pins
*
* This functions allows to read the logic state of input GPIO pins and
output GPIO pins. Since it multiple processes can read the logic state
of the GPIO, spin lock is not used here.
*/
static ssize_t bbb_gpio_read (struct file *filp,char *buf,size_t count,loff_t *f_pos)
{
unsigned int gpio;
ssize_t retval;
char byte;
gpio = iminor(filp->f_path.dentry->d_inode);
for (retval = 0; retval < count; ++retval)
{
byte = '0' + gpio_get_value(gpio);
if(put_user(byte, buf+retval))
break;
}
return retval;
}
/*
* bbb_gpio_write -Write to GPIO pin
*
* This function allows to set GPIO pin direction (input/out),to
set GPIO pin logic level (high/low), and to enable/disable edge-
triggered interrupt on a GPIO pin (rising/falling)
*Set logic level (high/low) to an input GPIO pin is not permitted
* The command set for setting GPIO pins is as follows
* Command Description
* "out">> Set GPIO direction to output via gpio_direction_ouput
* "in">> Set GPIO direction to input via gpio_direction_input
* "1">> Set GPIO pin logic level to high
* "0">> Set GPIO pin logic level to low
* "rising">> Enable rising edge trigger
* "falling">> Enable falling edge trigger
* "disable-irq">> Disable interrupt on a GPIO pin
*/
//---------------------------------------------------------------------------WRITE METHOD-------------------------------
static ssize_t bbb_gpio_write (struct file *filp,const char *buf,size_t count,loff_t *f_pos)
{
unsigned int gpio, len = 0, value = 0;
char kbuf[BUF_SIZE];
struct bbb_gpio_dev *bbb_gpio_devp = filp->private_data;
unsigned long flags;
gpio = iminor(filp->f_path.dentry->d_inode);
len = count < BUF_SIZE ? count-1 : BUF_SIZE-1;
if(copy_from_user(kbuf, buf, len) != 0)
return -EFAULT;
kbuf[len] = '\0';
printk(KERN_INFO "Request from user: %s\n", kbuf);
// Check the content of kbuf and set GPIO pin accordingly
if (strcmp(kbuf, "out") == 0)
{
printk(KERN_ALERT "gpio[%d] direction set to ouput\n", gpio);
if (bbb_gpio_devp->dir != out)
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
gpio_direction_output(gpio, low);
bbb_gpio_devp->dir = out;
bbb_gpio_devp->state = low;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
}
else if (strcmp(kbuf, "in") == 0)
{
if (bbb_gpio_devp->dir != in)
{
printk(KERN_INFO "Set gpio[%d] direction: input\n", gpio);
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
gpio_direction_input(gpio);
bbb_gpio_devp->dir = in;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
}
else if ((strcmp(kbuf, "1") == 0) || (strcmp(kbuf, "0") == 0))
{
sscanf(kbuf, "%d", &value);
if (bbb_gpio_devp->dir == in)
{
printk("Cannot set GPIO %d, direction: input\n", gpio);
return -EPERM;
}
if (bbb_gpio_devp->dir == out)
{
if (value > 0)
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
gpio_set_value(gpio, high);
bbb_gpio_devp->state = high;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
else
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
gpio_set_value(gpio, low);
bbb_gpio_devp->state = low;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
}
}
else if ((strcmp(kbuf, "rising") == 0) || (strcmp(kbuf, "falling")== 0))
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
gpio_direction_input(gpio);
bbb_gpio_devp->dir = in;
bbb_gpio_devp->irq_perm = true;
if (strcmp(kbuf, "rising") == 0)
bbb_gpio_devp->irq_flag = IRQF_TRIGGER_RISING;
else
bbb_gpio_devp->irq_flag = IRQF_TRIGGER_FALLING;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
else if (strcmp(kbuf, "disable-irq") == 0)
{
spin_lock_irqsave(&bbb_gpio_devp->lock, flags);
bbb_gpio_devp->irq_perm = false;
spin_unlock_irqrestore(&bbb_gpio_devp->lock, flags);
}
else
{
printk(KERN_ERR "Invalid value\n");
return -EINVAL;
}
*f_pos += count;
return count;
}
/*
* bbb_gpio_init -Initialize GPIO device driver
*
* This function performs the following tasks:
* Dynamically register a character device major
* Create "bbb-gpio" class
* Claim GPIO resource
* Initialize the per-device data structure bbb_gpio_dev
* Initialize spin lock used for synchronization
* Register character device to the kernel
* Create device nodes to expose GPIO resource
*/
//---------------------------------------------------------------------------INIT METHOD-------------------------------
static int __init bbb_gpio_init(void)
{
int i, ret, index = 0;
struct timeval tv ;
if (alloc_chrdev_region(&first, 0, NUM_GPIO_PINS, DEVICE_NAME) < 0)
{
printk(KERN_DEBUG "Cannot register device\n");
return -1;
}
if ((bbb_gpio_class = class_create(THIS_MODULE, DEVICE_NAME)) == NULL)
{
printk(KERN_DEBUG "Cannot create class %s\n", DEVICE_NAME);
unregister_chrdev_region(first, NUM_GPIO_PINS);
return -EINVAL;
}
for (i = 0; i < MAX_GPIO_NUMBER; i++)
{
/*if (i==20 || i==26 || i==27 || i==44 || i==45 || i==46 || i==47 || i==48 ||i==49 || i==60 || i==65 || i==66 ||i==67 || i==68 || i==69 || i==81 ||i==112 || i==115 || i==117)*/
if(i==49 || i==48)
{
bbb_gpio_devp[index] = kmalloc(sizeof(struct bbb_gpio_dev),GFP_KERNEL);
if (!bbb_gpio_devp[index])
{
printk("error in allocating memory\n");
return -ENOMEM;
}
if (gpio_request_one(i, GPIOF_OUT_INIT_LOW, NULL) < 0)
{
printk(KERN_ALERT "Error requesting GPIO %d\n", i);
return -ENODEV;
}
bbb_gpio_devp[index]->dir = out;
bbb_gpio_devp[index]->state = low;
bbb_gpio_devp[index]->irq_perm = false;
bbb_gpio_devp[index]->irq_flag = IRQF_TRIGGER_RISING;
bbb_gpio_devp[index]->irq_counter = 0;
bbb_gpio_devp[index]->cdev.owner = THIS_MODULE;
spin_lock_init(&bbb_gpio_devp[index]->lock);
cdev_init(&bbb_gpio_devp[index]->cdev, &bbb_gpio_fops);
if ((ret = cdev_add(&bbb_gpio_devp[index]->cdev, (first + i), 1)))
{
printk (KERN_ALERT "Error %d adding cdev\n", ret);
for (i = 0; i < MAX_GPIO_NUMBER; i++)
{
/*if (i==20 || i==26 || i==27 || i==44 || i==45 || i==46 || i==47 || i==48 ||i==49 || i==60 || i==65 || i==66 ||i==67 || i==68 || i==69 || i==81 ||i==112 || i==115 || i==117)*/
if(i==49 || i==48)
{
device_destroy (bbb_gpio_class,MKDEV(MAJOR(first),MINOR(first) + i));
}
}
class_destroy(bbb_gpio_class);
unregister_chrdev_region(first, NUM_GPIO_PINS);
return ret;
}
if (device_create(bbb_gpio_class,NULL,MKDEV(MAJOR(first), MINOR(first)+i),NULL,"bbbGpio%d",i) == NULL)
{
class_destroy(bbb_gpio_class);
unregister_chrdev_region(first, NUM_GPIO_PINS);
return -1;
}
index++;
}
}
// Configure interrupt
do_gettimeofday(&tv) ;
epochMilli =(uint64_t)tv.tv_sec *(uint64_t)1000 +(uint64_t)(tv.tv_usec/1000);
printk("BBB GPIO driver initialized\n");
return 0;
}
/*
* bbb_gpio_exit -Clean up GPIO device driver when unloaded
*
* This functions performs the following tasks:
* Release major number
* Release device nodes in /dev
* Release per-device structure arrays
* Detroy class in /sys
* Set all GPIO pins to output, low level
*/
//---------------------------------------------------------------------------EXIT METHOD-------------------------------
static void __exit bbb_gpio_exit(void)
{
int i = 0;
unregister_chrdev_region(first, NUM_GPIO_PINS);
for(i = 0; i < NUM_GPIO_PINS; i++)
kfree(bbb_gpio_devp[i]);
for (i = 0; i < MAX_GPIO_NUMBER; i++)
{
/*if (i==20 || i==26 || i==27 || i==44 ||
i==45 || i==46 || i==47 || i==48 ||
i==49 || i==60 || i==65 || i==66 ||
i==67 || i==68 || i==69 || i==81 ||
i==112 || i==115 || i==117)*/
if(i==49 || i==48)
{
gpio_direction_output(i, 0);
device_destroy (bbb_gpio_class,MKDEV(MAJOR(first), MINOR(first) + i));gpio_free(i);
}
}
class_destroy(bbb_gpio_class);
printk(KERN_INFO "BBB GPIO driver removed\n");
}
//-----------------------------------------------------------------------------------------------------------------------
module_init(bbb_gpio_init);
module_exit(bbb_gpio_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Group 19");
MODULE_DESCRIPTION("GPIO device driver for BBB platform");