d3cd7d96cc71efe68bf7da23b11b23522fc1e190
[openwrt/svn-archive/archive.git] / target / linux / ar7 / 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 <linux/uaccess.h>
29 #include <linux/io.h>
30 #include <linux/types.h>
31 #include <linux/cdev.h>
32 #include <gpio.h>
33
34 #define DRVNAME "ar7_gpio"
35 #define LONGNAME "TI AR7 GPIOs Driver"
36
37 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
38 MODULE_DESCRIPTION(LONGNAME);
39 MODULE_LICENSE("GPL");
40
41 static int ar7_gpio_major;
42
43 static ssize_t ar7_gpio_write(struct file *file, const char __user *buf,
44 size_t len, loff_t *ppos)
45 {
46 int pin = iminor(file->f_dentry->d_inode);
47 size_t i;
48
49 for (i = 0; i < len; ++i) {
50 char c;
51 if (get_user(c, buf + i))
52 return -EFAULT;
53 switch (c) {
54 case '0':
55 gpio_set_value(pin, 0);
56 break;
57 case '1':
58 gpio_set_value(pin, 1);
59 break;
60 case 'd':
61 case 'D':
62 ar7_gpio_disable(pin);
63 break;
64 case 'e':
65 case 'E':
66 ar7_gpio_enable(pin);
67 break;
68 case 'i':
69 case 'I':
70 case '<':
71 gpio_direction_input(pin);
72 break;
73 case 'o':
74 case 'O':
75 case '>':
76 gpio_direction_output(pin);
77 break;
78 default:
79 return -EINVAL;
80 }
81 }
82
83 return len;
84 }
85
86 static ssize_t ar7_gpio_read(struct file *file, char __user *buf,
87 size_t len, loff_t *ppos)
88 {
89 int pin = iminor(file->f_dentry->d_inode);
90 int value;
91
92 value = gpio_get_value(pin);
93 if (put_user(value ? '1' : '0', buf))
94 return -EFAULT;
95
96 return 1;
97 }
98
99 static int ar7_gpio_open(struct inode *inode, struct file *file)
100 {
101 int m = iminor(inode);
102
103 if (m >= AR7_GPIO_MAX)
104 return -EINVAL;
105
106 return nonseekable_open(inode, file);
107 }
108
109 static int ar7_gpio_release(struct inode *inode, struct file *file)
110 {
111 return 0;
112 }
113
114 static const struct file_operations ar7_gpio_fops = {
115 .owner = THIS_MODULE,
116 .write = ar7_gpio_write,
117 .read = ar7_gpio_read,
118 .open = ar7_gpio_open,
119 .release = ar7_gpio_release,
120 .llseek = no_llseek,
121 };
122
123 static struct platform_device *ar7_gpio_device;
124
125 static int __init ar7_gpio_init(void)
126 {
127 int rc;
128
129 ar7_gpio_device = platform_device_alloc(DRVNAME, -1);
130 if (!ar7_gpio_device)
131 return -ENOMEM;
132
133 rc = platform_device_add(ar7_gpio_device);
134 if (rc < 0)
135 goto out_put;
136
137 rc = register_chrdev(ar7_gpio_major, DRVNAME, &ar7_gpio_fops);
138 if (rc < 0)
139 goto out_put;
140
141 ar7_gpio_major = rc;
142
143 rc = 0;
144
145 goto out;
146
147 out_put:
148 platform_device_put(ar7_gpio_device);
149 out:
150 return rc;
151 }
152
153 static void __exit ar7_gpio_exit(void)
154 {
155 unregister_chrdev(ar7_gpio_major, DRVNAME);
156 platform_device_unregister(ar7_gpio_device);
157 }
158
159 module_init(ar7_gpio_init);
160 module_exit(ar7_gpio_exit);