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