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