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