-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyled.c
127 lines (106 loc) · 3.07 KB
/
myled.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
/*Copyright (c) 2018 Masaharu Takahashi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <asm/delay.h>
MODULE_AUTHOR("Masaharu TAKAHASHI");
MODULE_DESCRIPTION("driver for LED control");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
static dev_t dev;
static struct cdev cdv;
static struct class *cls = NULL;
static volatile u32 *gpio_base = NULL;
static ssize_t led_write(struct file* filp, const char* buf, size_t count, loff_t* pos)
{
char c;
unsigned int time =0;
unsigned int def_time = 2000;
if(copy_from_user(&c,buf,sizeof(char)))
return -EFAULT;
if(c == '0'){
gpio_base[10] = 1 <<25;
}
else if(c == '1'){
gpio_base[7] = 1 << 25;
}
else if(c == '2'){
for(time = 0; time < def_time; time++){
gpio_base[10] = 1 << 25;
udelay(def_time - time);
gpio_base[7] = 1 << 25;
udelay(time);
}
}
else if(c == '3'){
for(time = 0; time < def_time; time++){
gpio_base[7] = 1 << 25;
udelay(def_time - time);
gpio_base[10] = 1 << 25;
udelay(time);
}
}
return 1;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.write = led_write
};
static int __init init_mod(void)
{
int retval;
gpio_base = ioremap_nocache(0x3f200000, 0xA0); //0xA0: region to map
const u32 led = 25;
const u32 index = led/10; //GPFSEL2
const u32 shift = (led%10)*3; //15bit
const u32 mask = ~(0x7 << shift);
gpio_base[index] = (gpio_base[index] & mask) | (0x1 << shift);
retval = alloc_chrdev_region(&dev, 0, 1, "myled");
if(retval < 0){
printk(KERN_ERR "alloc_chrdev_region failed.\n");
return retval;
}
printk(KERN_INFO "%s is loaded. major:%d\n",__FILE__,MAJOR(dev));
cdev_init(&cdv, &led_fops);
cdv.owner = THIS_MODULE;
retval = cdev_add(&cdv, dev, 1);
if(retval < 0){
printk(KERN_ERR "cdev_add failed. major:%d, minor:%d",MAJOR(dev),MINOR(dev));
return retval;
}
cls = class_create(THIS_MODULE, "myled");
if(IS_ERR(cls)){
printk(KERN_ERR "class_create failed.");
return PTR_ERR(cls);
}
else{
printk(KERN_NOTICE "class_create succeed.");
}
device_create(cls, NULL, dev, NULL, "myled%d",MINOR(dev));
return 0;
}
static void __exit cleanup_mod(void)
{
cdev_del(&cdv);
device_destroy(cls, dev);
class_destroy(cls);
unregister_chrdev_region(dev, 1);
printk(KERN_INFO "%s is unloaded. major:%d\n",__FILE__,MAJOR(dev));
iounmap(gpio_base);
}
module_init(init_mod);
module_exit(cleanup_mod);