aa5a2e32e15ac0fb2ed3c8df27f738f3863c3ffc
[openwrt/svn-archive/archive.git] / target / linux / at91-2.6 / patches-2.6.21 / 003-gpio-driver.patch
1 --- linux-2.6.21.1.orig/arch/arm/mach-at91/gpio.c 2007-04-27 23:49:26.000000000 +0200
2 +++ linux-2.6.21.1/arch/arm/mach-at91/gpio.c 2007-05-28 15:30:48.000000000 +0200
3 @@ -27,6 +27,7 @@
4
5 static struct at91_gpio_bank *gpio;
6 static int gpio_banks;
7 +static u32 pio_gpio_pin[4] = { 0, 0, 0, 0 };
8
9
10 static inline void __iomem *pin_to_controller(unsigned pin)
11 @@ -71,9 +72,13 @@
12 {
13 void __iomem *pio = pin_to_controller(pin);
14 unsigned mask = pin_to_mask(pin);
15 + int bank = (pin - PIN_BASE) / 32;
16
17 if (!pio)
18 return -EINVAL;
19 +
20 + pio_gpio_pin[bank] |= mask;
21 +
22 __raw_writel(mask, pio + PIO_IDR);
23 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
24 __raw_writel(mask, pio + PIO_PER);
25 @@ -130,10 +135,13 @@
26 {
27 void __iomem *pio = pin_to_controller(pin);
28 unsigned mask = pin_to_mask(pin);
29 + int bank = (pin - PIN_BASE) / 32;
30
31 if (!pio)
32 return -EINVAL;
33
34 + pio_gpio_pin[bank] |= mask;
35 +
36 __raw_writel(mask, pio + PIO_IDR);
37 __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
38 __raw_writel(mask, pio + PIO_ODR);
39 @@ -151,10 +159,13 @@
40 {
41 void __iomem *pio = pin_to_controller(pin);
42 unsigned mask = pin_to_mask(pin);
43 + int bank = (pin - PIN_BASE) / 32;
44
45 if (!pio)
46 return -EINVAL;
47
48 + pio_gpio_pin[bank] |= mask;
49 +
50 __raw_writel(mask, pio + PIO_IDR);
51 __raw_writel(mask, pio + PIO_PUDR);
52 __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
53 @@ -262,6 +273,18 @@
54 }
55 EXPORT_SYMBOL(at91_get_gpio_value);
56
57 +int at91_is_pin_gpio(unsigned pin)
58 +{
59 + void __iomem *pio = pin_to_controller(pin);
60 + unsigned mask = pin_to_mask(pin);
61 + int bank = (pin - PIN_BASE) / 32;
62 +
63 + if (!pio)
64 + return -EINVAL;
65 + return (pio_gpio_pin[bank] & mask) != 0;
66 +}
67 +EXPORT_SYMBOL(at91_is_pin_gpio);
68 +
69 /*--------------------------------------------------------------------------*/
70
71 #ifdef CONFIG_PM
72 --- linux-2.6.21.1.orig/drivers/char/Kconfig 2007-05-28 12:22:29.000000000 +0200
73 +++ linux-2.6.21.1/drivers/char/Kconfig 2007-05-28 15:37:43.000000000 +0200
74 @@ -1087,5 +1087,12 @@
75 The SPI driver gives user mode access to this serial
76 bus on the AT91RM9200 processor.
77
78 +config AT91_VLIO
79 + tristate "Versalink LED and GPIO interface"
80 + depends on ARCH_AT91RM9200 && MACH_VLINK
81 + default n
82 + help
83 + Provides a handler GPIO's in userspace
84 +
85 endmenu
86
87 --- linux-2.6.21.1.orig/drivers/char/Makefile 2007-05-28 12:22:29.000000000 +0200
88 +++ linux-2.6.21.1/drivers/char/Makefile 2007-05-28 15:38:11.000000000 +0200
89 @@ -95,6 +95,7 @@
90 obj-$(CONFIG_TELCLOCK) += tlclk.o
91 obj-$(CONFIG_AT91_SPI) += at91_spi.o
92 obj-$(CONFIG_AT91_SPIDEV) += at91_spidev.o
93 +obj-$(CONFIG_AT91_VLIO) += vlink_giu.o
94
95 obj-$(CONFIG_WATCHDOG) += watchdog/
96 obj-$(CONFIG_MWAVE) += mwave/
97 --- linux-2.6.21.1.orig/drivers/char/vlink_giu.c 1970-01-01 01:00:00.000000000 +0100
98 +++ linux-2.6.21.1/drivers/char/vlink_giu.c 2007-05-28 15:39:47.000000000 +0200
99 @@ -0,0 +1,256 @@
100 +/*
101 + * Driver for FDL Versalink GPIO
102 + *
103 + * Copyright (C) 2005 Guthrie Consulting
104 + * Author: Hamish Guthrie <hamish@prodigi.ch>
105 + *
106 + * This program is free software; you can redistribute it and/or modify
107 + * it under the terms of the GNU General Public License as published by
108 + * the Free Software Foundation; either version 2 of the License, or
109 + * (at your option) any later version.
110 + *
111 + * This program is distributed in the hope that it will be useful,
112 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
113 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114 + * GNU General Public License for more details.
115 + *
116 + * You should have received a copy of the GNU General Public License
117 + * along with this program; if not, write to the Free Software
118 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
119 + */
120 +
121 +#include <linux/module.h>
122 +#include <linux/moduleparam.h>
123 +#include <linux/init.h>
124 +#include <linux/platform_device.h>
125 +
126 +#include <linux/kernel.h>
127 +#include <linux/slab.h>
128 +#include <linux/fs.h>
129 +#include <linux/errno.h>
130 +#include <linux/init.h>
131 +#include <linux/types.h>
132 +#include <linux/proc_fs.h>
133 +#include <linux/fcntl.h>
134 +#include <linux/seq_file.h>
135 +#include <linux/cdev.h>
136 +#include <asm/arch/gpio.h>
137 +#include <asm/uaccess.h>
138 +
139 +static int major; /* default is dynamic major device number */
140 +module_param(major, int, 0);
141 +MODULE_PARM_DESC(major, "Major device number");
142 +
143 +#define VIO_NR_DEVS 96
144 +
145 +struct vio_dev {
146 + struct cdev cdev;
147 +};
148 +
149 +struct vio_dev *vio_devices;
150 +static struct class *vio_class;
151 +
152 +static ssize_t gpio_read(struct file *file, char __user *buf, size_t len,
153 + loff_t *ppos)
154 +{
155 + unsigned int pin;
156 + int retval;
157 + char value = '0';
158 +
159 + pin = iminor(file->f_dentry->d_inode);
160 +
161 + retval = at91_get_gpio_value(PIN_BASE + pin);
162 + if (retval < 0)
163 + return -EFAULT;
164 +
165 + value = retval + 0x30;
166 + if (put_user(value, buf))
167 + return -EFAULT;
168 +
169 + return 1;
170 +}
171 +
172 +static ssize_t gpio_write(struct file *file, const char __user *data,
173 + size_t len, loff_t *ppos)
174 +{
175 + unsigned int pin;
176 + size_t i;
177 + char c;
178 + int retval = 0;
179 +
180 + pin = iminor(file->f_dentry->d_inode);
181 +
182 + for (i = 0; i < len; i++) {
183 + if (get_user(c, data + i))
184 + return -EFAULT;
185 +
186 + switch (c) {
187 + case '0':
188 + case '1':
189 + retval = at91_set_gpio_value(PIN_BASE + pin, (int)c - 0x30);
190 + if (retval < 0)
191 + return -EFAULT;
192 + break;
193 + default:
194 + break;
195 + }
196 +
197 + if (retval < 0)
198 + break;
199 + }
200 +
201 + return i;
202 +}
203 +
204 +static int gpio_open(struct inode *inode, struct file *file)
205 +{
206 + return nonseekable_open(inode, file);
207 +}
208 +
209 +static int gpio_release(struct inode *inode, struct file *file)
210 +{
211 + return 0;
212 +}
213 +
214 +static struct file_operations vio_fops = {
215 + .owner = THIS_MODULE,
216 + .read = gpio_read,
217 + .write = gpio_write,
218 + .open = gpio_open,
219 + .release = gpio_release,
220 +};
221 +
222 +static void vio_setup_cdev(struct vio_dev *dev, int index)
223 +{
224 + int err, devno = MKDEV(major, index);
225 +
226 + cdev_init(&dev->cdev, &vio_fops);
227 + dev->cdev.owner = THIS_MODULE;
228 + dev->cdev.ops = &vio_fops;
229 + err = cdev_add (&dev->cdev, devno, 1);
230 + if (err)
231 + printk(KERN_NOTICE "vio: Error %d adding vio%d", err, index);
232 +}
233 +
234 +static int vio_remove(struct platform_device *dev)
235 +{
236 + int i;
237 + dev_t devno = MKDEV(major, 0);
238 +
239 + if (vio_devices) {
240 + for(i=0; i<VIO_NR_DEVS; i++) {
241 + int iodev = at91_is_pin_gpio(PIN_BASE + i);
242 + if (iodev) {
243 + cdev_del(&vio_devices[i].cdev);
244 + class_device_destroy(vio_class, MKDEV(major, i));
245 + }
246 + }
247 + kfree(vio_devices);
248 + }
249 +
250 + class_destroy(vio_class);
251 + unregister_chrdev_region(devno, VIO_NR_DEVS);
252 +
253 + platform_set_drvdata(dev, NULL);
254 +
255 + return 0;
256 +}
257 +
258 +static int vio_probe(struct platform_device *dev)
259 +{
260 + int retval, i, j;
261 + dev_t vdev = 0;
262 +
263 + if (major) {
264 + vdev = MKDEV(major, 0);
265 + retval = register_chrdev_region(vdev, VIO_NR_DEVS, "vio");
266 + } else {
267 + retval = alloc_chrdev_region(&vdev, 0, VIO_NR_DEVS, "vio");
268 + major = MAJOR(vdev);
269 + }
270 + if (retval < 0) {
271 + printk(KERN_WARNING "vio: can't get major %d\n", major);
272 + return retval;
273 + }
274 +
275 + if (major == 0) {
276 + major = retval;
277 + printk(KERN_INFO "vio: major number %d\n", major);
278 + }
279 +
280 + vio_class = class_create(THIS_MODULE, "vio");
281 +
282 + if (IS_ERR(vio_class)) {
283 + printk(KERN_ERR "vio: Error creating vio class\n");
284 + vio_remove(dev);
285 + return PTR_ERR(vio_class);
286 + }
287 +
288 + vio_devices = kmalloc(VIO_NR_DEVS * sizeof(struct vio_dev), GFP_KERNEL);
289 + if (!vio_devices) {
290 + retval = -ENOMEM;
291 + goto fail;
292 + }
293 + memset(vio_devices, 0, VIO_NR_DEVS * sizeof(struct vio_dev));
294 +
295 + for (i=0; i<VIO_NR_DEVS/32; i++)
296 + for(j=0; j<32; j++) {
297 + int iodev = at91_is_pin_gpio(PIN_BASE + i*32 + j);
298 + if (iodev) {
299 + vio_setup_cdev(&vio_devices[i*32 + j], i*32 + j);
300 + class_device_create(vio_class, NULL, MKDEV(major, i*32 + j), NULL,
301 + "vio%c%d", i + 'A', j);
302 + }
303 + }
304 +
305 + platform_set_drvdata(dev, vio_devices);
306 +
307 + return 0;
308 +
309 +fail:
310 + vio_remove(dev);
311 + return retval;
312 +}
313 +
314 +static struct platform_device *vio_platform_device;
315 +
316 +static struct platform_driver vio_driver = {
317 + .probe = vio_probe,
318 + .remove = vio_remove,
319 + .driver = {
320 + .name = "vio",
321 + .owner = THIS_MODULE,
322 + },
323 +};
324 +
325 +static int __init vio_init(void)
326 +{
327 + int retval;
328 +
329 + vio_platform_device = platform_device_register_simple("vio", -1, NULL, 0);
330 + if (IS_ERR(vio_platform_device)) {
331 + printk(KERN_WARNING "vio: device registration failed\n");
332 + return PTR_ERR(vio_platform_device);
333 + }
334 +
335 + retval = platform_driver_register(&vio_driver);
336 + if (retval < 0) {
337 + printk(KERN_WARNING "vio: driver registration failed\n");
338 + platform_device_unregister(vio_platform_device);
339 + }
340 +
341 + return retval;
342 +}
343 +
344 +static void __exit vio_exit(void)
345 +{
346 + platform_driver_unregister(&vio_driver);
347 + platform_device_unregister(vio_platform_device);
348 +}
349 +
350 +module_init(vio_init);
351 +module_exit(vio_exit);
352 +
353 +MODULE_AUTHOR("Hamish Guthrie <hamish@prodigi.ch>");
354 +MODULE_DESCRIPTION("FDL Versalink GPIO Driver");
355 +MODULE_LICENSE("GPL");