1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/fs.h>
5 #include <linux/init.h>
6 #include <linux/devfs_fs_kernel.h>
7 #include <linux/miscdevice.h>
8 #include <linux/delay.h>
9 #include <asm/irq.h>
10 #include <asm/arch/regs-gpio.h>
11 #include <asm/hardware.h>
12 #include <asm/uaccess.h>
13 #include <linux/errno.h>
14
15 #define DEVICE_NAME "leds"
16 #define LED_MAJOR 231
17 /* GPIO作为LED的引脚 ,通过此可以知道地址*/
18 static unsigned long led_table [] = {
19 S3C2410_GPB5, /* (32*1) + 5 */
20 S3C2410_GPB6, /* (32*1) + 6 */
21 S3C2410_GPB7, /* (32*1) + 7 */
22 S3C2410_GPB8, /* (32*1) + 8 */
23 };
24 /* GPIO引脚的工作方式,01为输出 */
25 static unsigned int led_cfg_table [] = {
26 S3C2410_GPB5_OUTP, /* (0x01 << 10) */
27 S3C2410_GPB6_OUTP, /* (0x01 << 12) */
28 S3C2410_GPB7_OUTP, /* (0x01 << 14) */
29 S3C2410_GPB8_OUTP, /* (0x01 << 16) */
30 };
31
32
33
34 static int s3c2440_leds_open(struct inode *inode,struct file *file)
35 {
36 int i;
37 /* 循环设置4个LED引脚,将其设为输出方式*/
38 for (i = 0; i < 4; i++) {
39 s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
40 }
41
42 return 0;
43 }
44
45 static int s3c2440_leds_ioctl(
46 struct inode *inode,
47 struct file *file,
48 unsigned int cmd,
49 unsigned long arg)
50 {
51 switch(cmd) {
52 case 0:
53 case 1:
54 if (arg > 4) {
55 return -EINVAL;
56 }
57 s3c2410_gpio_setpin(led_table[arg], !cmd);
58 return 0;
59 default:
60 return -EINVAL;
61 }
62 }
63
64 static int s3c2440_leds_read(
65 struct file *filp,
66 char __user *buff,
67 size_t count,
68 loff_t *offp)
69 {
70 /* 读取寄存器中的值返回 */
71 char temp[4];
72 int i;
73
74 for (i = 0; i < 4; i++) {
75 if (s3c2410_gpio_getpin(led_table[i])) /*读取引脚的状态*/
76 temp[i] = '1';
77 else
78 temp[i] = '0';
79 }
80
81 if (copy_to_user(buff, (void *)temp, 4))
82 return -EFAULT;
83
84 return 4;
85 }
86
87 static int s3c2440_leds_write(
88 struct file *filp,
89 const char __user *buff,
90 size_t count,
91 loff_t *offp)
92 {
93 /* 向寄存器中写入传入的值 */
94 char temp[5];
95 int i;
96
97 if (copy_from_user(temp, buff, 5))
98 return -EFAULT;
99 for (i = 0; i < 4; i++) { /* 设置引脚的状态 */
100 if (temp[i] != '0')
101 s3c2410_gpio_setpin(led_table[i], 1);
102 else
103 s3c2410_gpio_setpin(led_table[i], 0);
104 }
105 return count;
106 }
107
108 static struct file_operations s3c2440_leds_fops = {
109 .owner = THIS_MODULE,
110 .ioctl = s3c2440_leds_ioctl,
111 .open = s3c2440_leds_open,
112 .read = s3c2440_leds_read,
113 .write = s3c2440_leds_write
114 };
115
116 static int __init s3c2440_leds_init(void)
117 {
118 int ret;
119
120 ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c2440_leds_fops);
121 if (ret < 0) {
122 printk(DEVICE_NAME " can't register major number\n");
123 return ret;
124 }
125
126 devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
127 printk(DEVICE_NAME " initialized\n");
128
129 return 0;
130 }
131
132 static void __exit s3c2440_leds_exit(void)
133 {
134 devfs_remove(DEVICE_NAME);
135 unregister_chrdev(LED_MAJOR, DEVICE_NAME);
136 }
137
138 module_init(s3c2440_leds_init);
139 module_exit(s3c2440_leds_exit);
140 MODULE_LICENSE("Dual BSD/GPL");
|