small rdc-2.6 fixes (from sn9, thanks!)
[openwrt.git] / target / linux / ar7-2.6 / files / drivers / char / ar7_gpio.c
1 /*
2 * linux/drivers/char/ar7_gpio.c
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <linux/device.h>
22 #include <linux/fs.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/platform_device.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30
31 #include <linux/types.h>
32 #include <linux/cdev.h>
33 #include <gpio.h>
34
35 #define DRVNAME "ar7_gpio"
36 #define LONGNAME "TI AR7 GPIOs Driver"
37
38 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
39 MODULE_DESCRIPTION(LONGNAME);
40 MODULE_LICENSE("GPL");
41
42 static int ar7_gpio_major = 0;
43
44 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
45 size_t len, loff_t *ppos)
46 {
47 int pin = iminor(file->f_dentry->d_inode);
48 size_t i;
49
50 for (i = 0; i < len; ++i) {
51 char c;
52 if (get_user(c, buf + i))
53 return -EFAULT;
54 switch (c) {
55 case '0':
56 gpio_set_value(pin, 0);
57 break;
58 case '1':
59 gpio_set_value(pin, 1);
60 break;
61 case 'd':
62 case 'D':
63 ar7_gpio_disable(pin);
64 break;
65 case 'e':
66 case 'E':
67 ar7_gpio_enable(pin);
68 break;
69 case 'i':
70 case 'I':
71 case '<':
72 gpio_direction_input(pin);
73 break;
74 case 'o':
75 case 'O':
76 case '>':
77 gpio_direction_output(pin);
78 break;
79 default:
80 return -EINVAL;
81 }
82 }
83
84 return len;
85 }
86
87 static ssize_t ar7_gpio_read(struct file *file, char __user * buf,
88 size_t len, loff_t * ppos)
89 {
90 int pin = iminor(file->f_dentry->d_inode);
91 int value;
92
93 value = gpio_get_value(pin);
94 if (put_user(value ? '1' : '0', buf))
95 return -EFAULT;
96
97 return 1;
98 }
99
100 static int ar7_gpio_open(struct inode *inode, struct file *file)
101 {
102 int m = iminor(inode);
103
104 if (m >= AR7_GPIO_MAX)
105 return -EINVAL;
106
107 return nonseekable_open(inode, file);
108 }
109
110 static int ar7_gpio_release(struct inode *inode, struct file *file)
111 {
112 return 0;
113 }
114
115 static const struct file_operations ar7_gpio_fops = {
116 .owner = THIS_MODULE,
117 .write = ar7_gpio_write,
118 .read = ar7_gpio_read,
119 .open = ar7_gpio_open,
120 .release = ar7_gpio_release,
121 .llseek = no_llseek,
122 };
123
124 static struct platform_device *ar7_gpio_device;
125
126 static int __init ar7_gpio_init(void)
127 {
128 int rc;
129
130 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
131 if (!ar7_gpio_device)
132 return -ENOMEM;
133
134 rc = platform_device_add(ar7_gpio_device);
135 if (rc < 0)
136 goto out_put;
137
138 rc = register_chrdev(ar7_gpio_major, DRVNAME, &ar7_gpio_fops);
139 if (rc < 0)
140 goto out_put;
141
142 ar7_gpio_major = rc;
143
144 rc = 0;
145
146 goto out;
147
148 out_put:
149 platform_device_put(ar7_gpio_device);
150 out:
151 return rc;
152 }
153
154 static void __exit ar7_gpio_exit(void)
155 {
156 unregister_chrdev(ar7_gpio_major, DRVNAME);
157 platform_device_unregister(ar7_gpio_device);
158 }
159
160 module_init(ar7_gpio_init);
161 module_exit(ar7_gpio_exit);
This page took 0.049935 seconds and 5 git commands to generate.