fixes ifxmips pci support and adds GENERIC_GPIO
[openwrt_comio.git] / target / linux / ifxmips / files / drivers / char / ifxmips_gpio.c
blobc1eefa5a9cbada8bbe115a81775ab1b0daf6c151
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2005 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/proc_fs.h>
25 #include <linux/init.h>
26 #include <linux/ioctl.h>
27 #include <linux/timer.h>
28 #include <linux/module.h>
29 #include <linux/timer.h>
30 #include <linux/interrupt.h>
31 #include <linux/kobject.h>
32 #include <linux/workqueue.h>
33 #include <linux/skbuff.h>
34 #include <linux/netlink.h>
35 #include <linux/platform_device.h>
36 #include <net/sock.h>
37 #include <asm/uaccess.h>
38 #include <asm/semaphore.h>
39 #include <asm/uaccess.h>
40 #include <asm/ifxmips/ifxmips.h>
41 #include <asm/ifxmips/ifxmips_ioctl.h>
43 #define MAX_PORTS 2
44 #define PINS_PER_PORT 16
46 #define DRVNAME "ifxmips_gpio"
48 static unsigned int ifxmips_gpio_major = 0;
50 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
51 #define IFXMIPS_RST_PIN 15
52 #define IFXMIPS_RST_PORT 1
54 static struct timer_list rst_button_timer;
56 extern struct sock *uevent_sock;
57 extern u64 uevent_next_seqnum(void);
58 static unsigned long seen;
59 static int pressed = 0;
61 struct event_t {
62 struct work_struct wq;
63 int set;
64 unsigned long jiffies;
66 #endif
68 /* TODO do we need this ? */
69 static struct semaphore port_sem;
71 /* TODO do we really need this ? return in a define is forbidden by coding style */
72 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
74 int
75 ifxmips_port_reserve_pin (unsigned int port, unsigned int pin)
77 IFXMIPS_GPIO_SANITY;
78 printk("%s : call to obseleted function\n", __func__);
80 return 0;
82 EXPORT_SYMBOL(ifxmips_port_reserve_pin);
84 int
85 ifxmips_port_free_pin (unsigned int port, unsigned int pin)
87 IFXMIPS_GPIO_SANITY;
88 printk("%s : call to obseleted function\n", __func__);
90 return 0;
92 EXPORT_SYMBOL(ifxmips_port_free_pin);
94 int
95 ifxmips_port_set_open_drain (unsigned int port, unsigned int pin)
97 IFXMIPS_GPIO_SANITY;
98 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_OD + (port * 0xC));
100 return 0;
102 EXPORT_SYMBOL(ifxmips_port_set_open_drain);
105 ifxmips_port_clear_open_drain (unsigned int port, unsigned int pin)
107 IFXMIPS_GPIO_SANITY;
108 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OD + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_OD + (port * 0xC));
110 return 0;
112 EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
115 ifxmips_port_set_pudsel (unsigned int port, unsigned int pin)
117 IFXMIPS_GPIO_SANITY;
118 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
120 return 0;
122 EXPORT_SYMBOL(ifxmips_port_set_pudsel);
125 ifxmips_port_clear_pudsel (unsigned int port, unsigned int pin)
127 IFXMIPS_GPIO_SANITY;
128 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
130 return 0;
132 EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
135 ifxmips_port_set_puden (unsigned int port, unsigned int pin)
137 IFXMIPS_GPIO_SANITY;
138 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
140 return 0;
142 EXPORT_SYMBOL(ifxmips_port_set_puden);
145 ifxmips_port_clear_puden (unsigned int port, unsigned int pin)
147 IFXMIPS_GPIO_SANITY;
148 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
150 return 0;
152 EXPORT_SYMBOL(ifxmips_port_clear_puden);
155 ifxmips_port_set_stoff (unsigned int port, unsigned int pin)
157 IFXMIPS_GPIO_SANITY;
158 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
160 return 0;
162 EXPORT_SYMBOL(ifxmips_port_set_stoff);
165 ifxmips_port_clear_stoff (unsigned int port, unsigned int pin)
167 IFXMIPS_GPIO_SANITY;
168 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
170 return 0;
172 EXPORT_SYMBOL(ifxmips_port_clear_stoff);
175 ifxmips_port_set_dir_out (unsigned int port, unsigned int pin)
177 IFXMIPS_GPIO_SANITY;
178 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_DIR + (port * 0xC));
180 return 0;
182 EXPORT_SYMBOL(ifxmips_port_set_dir_out);
185 ifxmips_port_set_dir_in (unsigned int port, unsigned int pin)
187 IFXMIPS_GPIO_SANITY;
188 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_DIR + (port * 0xC));
190 return 0;
192 EXPORT_SYMBOL(ifxmips_port_set_dir_in);
195 ifxmips_port_set_output (unsigned int port, unsigned int pin)
197 IFXMIPS_GPIO_SANITY;
198 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_OUT + (port * 0xC));
200 return 0;
202 EXPORT_SYMBOL(ifxmips_port_set_output);
205 ifxmips_port_clear_output (unsigned int port, unsigned int pin)
207 IFXMIPS_GPIO_SANITY;
208 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_OUT + (port * 0xC));
210 return 0;
212 EXPORT_SYMBOL(ifxmips_port_clear_output);
215 ifxmips_port_get_input (unsigned int port, unsigned int pin)
217 IFXMIPS_GPIO_SANITY;
219 if (ifxmips_r32(IFXMIPS_GPIO_P0_IN + (port * 0xC)) & (1 << pin))
220 return 0;
221 else
222 return 1;
224 EXPORT_SYMBOL(ifxmips_port_get_input);
227 ifxmips_port_set_altsel0 (unsigned int port, unsigned int pin)
229 IFXMIPS_GPIO_SANITY;
230 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
232 return 0;
234 EXPORT_SYMBOL(ifxmips_port_set_altsel0);
237 ifxmips_port_clear_altsel0 (unsigned int port, unsigned int pin)
239 IFXMIPS_GPIO_SANITY;
240 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
242 return 0;
244 EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
247 ifxmips_port_set_altsel1 (unsigned int port, unsigned int pin)
249 IFXMIPS_GPIO_SANITY;
250 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
252 return 0;
254 EXPORT_SYMBOL(ifxmips_port_set_altsel1);
257 ifxmips_port_clear_altsel1 (unsigned int port, unsigned int pin)
259 IFXMIPS_GPIO_SANITY;
260 ifxmips_w32(ifxmips_r32(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
262 return 0;
264 EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
266 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
267 static inline void add_msg(struct sk_buff *skb, char *msg)
269 char *scratch;
270 scratch = skb_put(skb, strlen(msg) + 1);
271 sprintf(scratch, msg);
274 static void hotplug_button(struct work_struct *wq)
276 struct sk_buff *skb;
277 struct event_t *event;
278 size_t len;
279 char *scratch, *s;
280 char buf[128];
282 event = container_of(wq, struct event_t, wq);
283 if (!uevent_sock)
284 goto done;
286 /* allocate message with the maximum possible size */
287 s = event->set ? "pressed" : "released";
288 len = strlen(s) + 2;
289 skb = alloc_skb(len + 2048, GFP_KERNEL);
290 if (!skb)
291 goto done;
293 /* add header */
294 scratch = skb_put(skb, len);
295 sprintf(scratch, "%s@",s);
297 /* copy keys to our continuous event payload buffer */
298 add_msg(skb, "HOME=/");
299 add_msg(skb, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
300 add_msg(skb, "SUBSYSTEM=button");
301 add_msg(skb, "BUTTON=reset");
302 add_msg(skb, (event->set ? "ACTION=pressed" : "ACTION=released"));
303 sprintf(buf, "SEEN=%ld", (event->jiffies - seen)/HZ);
304 add_msg(skb, buf);
305 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
306 add_msg(skb, buf);
308 NETLINK_CB(skb).dst_group = 1;
309 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
311 done:
312 kfree(event);
315 static void reset_button_poll(unsigned long unused)
317 struct event_t *event;
319 rst_button_timer.expires = jiffies + HZ;
320 add_timer(&rst_button_timer);
322 if (pressed != ifxmips_port_get_input(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN))
324 if(pressed)
325 pressed = 0;
326 else
327 pressed = 1;
328 printk("button was %s\n", (pressed ? "pressed" : "released"));
329 event = (struct event_t *) kzalloc(sizeof(struct event_t), GFP_ATOMIC);
330 if (!event)
332 printk("Could not alloc hotplug event\n");
333 return;
335 event->set = pressed;
336 event->jiffies = jiffies;
337 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
338 schedule_work(&event->wq);
339 seen = jiffies;
342 #endif
344 long ifxmips_port_read_procmem_helper(char* tag, u32* in_reg, char *buf)
346 u32 reg, bit = 0;
347 unsigned int len, t;
349 len = sprintf(buf, "\n%s: ", tag);
350 reg = ifxmips_r32(in_reg);
351 bit = 0x80000000;
352 for (t = 0; t < 32; t++) {
353 if ((reg & bit) > 0)
354 len = len + sprintf(buf + len, "X");
355 else
356 len = len + sprintf(buf + len, " ");
357 bit = bit >> 1;
360 return len;
364 ifxmips_port_read_procmem (char *buf, char **start, off_t offset, int count,
365 int *eof, void *data)
367 long len = sprintf (buf, "\nIFXMips Port Settings\n");
369 len += sprintf (buf + len,
370 " 3 2 1 0\n");
371 len += sprintf (buf + len,
372 " 10987654321098765432109876543210\n");
373 len += sprintf (buf + len,
374 "----------------------------------------\n");
376 len += ifxmips_port_read_procmem_helper("P0-OUT", IFXMIPS_GPIO_P0_OUT, &buf[len]);
377 len += ifxmips_port_read_procmem_helper("P1-OUT", IFXMIPS_GPIO_P1_OUT, &buf[len]);
378 len += ifxmips_port_read_procmem_helper("P0-IN ", IFXMIPS_GPIO_P0_IN, &buf[len]);
379 len += ifxmips_port_read_procmem_helper("P1-IN ", IFXMIPS_GPIO_P1_IN, &buf[len]);
380 len += ifxmips_port_read_procmem_helper("P0-DIR", IFXMIPS_GPIO_P0_DIR, &buf[len]);
381 len += ifxmips_port_read_procmem_helper("P1-DIR", IFXMIPS_GPIO_P1_DIR, &buf[len]);
382 len += ifxmips_port_read_procmem_helper("P0-STO ", IFXMIPS_GPIO_P0_STOFF, &buf[len]);
383 len += ifxmips_port_read_procmem_helper("P1-STO ", IFXMIPS_GPIO_P1_STOFF, &buf[len]);
384 len += ifxmips_port_read_procmem_helper("P0-PUDE", IFXMIPS_GPIO_P0_PUDEN, &buf[len]);
385 len += ifxmips_port_read_procmem_helper("P1-PUDE", IFXMIPS_GPIO_P1_PUDEN, &buf[len]);
386 len += ifxmips_port_read_procmem_helper("P0-OD ", IFXMIPS_GPIO_P0_OD, &buf[len]);
387 len += ifxmips_port_read_procmem_helper("P1-OD ", IFXMIPS_GPIO_P1_OD, &buf[len]);
388 len += ifxmips_port_read_procmem_helper("P0-PUDS", IFXMIPS_GPIO_P0_PUDSEL, &buf[len]);
389 len += ifxmips_port_read_procmem_helper("P1-PUDS", IFXMIPS_GPIO_P1_PUDSEL, &buf[len]);
390 len += ifxmips_port_read_procmem_helper("P0-ALT0", IFXMIPS_GPIO_P0_ALTSEL0, &buf[len]);
391 len += ifxmips_port_read_procmem_helper("P1-ALT0", IFXMIPS_GPIO_P1_ALTSEL0, &buf[len]);
392 len += ifxmips_port_read_procmem_helper("P0-ALT1", IFXMIPS_GPIO_P0_ALTSEL1, &buf[len]);
393 len += ifxmips_port_read_procmem_helper("P1-ALT1", IFXMIPS_GPIO_P1_ALTSEL1, &buf[len]);
394 len = len + sprintf (buf + len, "\n\n");
396 *eof = 1;
398 return len;
401 static int
402 ifxmips_port_open (struct inode *inode, struct file *filep)
404 return 0;
407 static int
408 ifxmips_port_release (struct inode *inode, struct file *filelp)
410 return 0;
413 static int
414 ifxmips_port_ioctl (struct inode *inode, struct file *filp,
415 unsigned int cmd, unsigned long arg)
417 int ret = 0;
418 volatile struct ifxmips_port_ioctl_parm parm;
420 if (_IOC_TYPE (cmd) != IFXMIPS_PORT_IOC_MAGIC)
421 return -EINVAL;
423 if (_IOC_DIR (cmd) & _IOC_WRITE) {
424 if (!access_ok
425 (VERIFY_READ, arg,
426 sizeof (struct ifxmips_port_ioctl_parm)))
427 return -EFAULT;
428 ret = copy_from_user ((void *) &parm, (void *) arg,
429 sizeof (struct ifxmips_port_ioctl_parm));
431 if (_IOC_DIR (cmd) & _IOC_READ) {
432 if (!access_ok
433 (VERIFY_WRITE, arg,
434 sizeof (struct ifxmips_port_ioctl_parm)))
435 return -EFAULT;
438 if (down_trylock (&port_sem) != 0)
439 return -EBUSY;
441 switch (cmd) {
442 case IFXMIPS_PORT_IOCOD:
443 if (parm.value == 0x00)
444 ifxmips_port_clear_open_drain(parm.port, parm.pin);
445 else
446 ifxmips_port_set_open_drain(parm.port, parm.pin);
447 break;
449 case IFXMIPS_PORT_IOCPUDSEL:
450 if (parm.value == 0x00)
451 ifxmips_port_clear_pudsel(parm.port, parm.pin);
452 else
453 ifxmips_port_set_pudsel(parm.port, parm.pin);
454 break;
456 case IFXMIPS_PORT_IOCPUDEN:
457 if (parm.value == 0x00)
458 ifxmips_port_clear_puden(parm.port, parm.pin);
459 else
460 ifxmips_port_set_puden(parm.port, parm.pin);
461 break;
463 case IFXMIPS_PORT_IOCSTOFF:
464 if (parm.value == 0x00)
465 ifxmips_port_clear_stoff(parm.port, parm.pin);
466 else
467 ifxmips_port_set_stoff(parm.port, parm.pin);
468 break;
470 case IFXMIPS_PORT_IOCDIR:
471 if (parm.value == 0x00)
472 ifxmips_port_set_dir_in(parm.port, parm.pin);
473 else
474 ifxmips_port_set_dir_out(parm.port, parm.pin);
475 break;
477 case IFXMIPS_PORT_IOCOUTPUT:
478 if (parm.value == 0x00)
479 ifxmips_port_clear_output(parm.port, parm.pin);
480 else
481 ifxmips_port_set_output(parm.port, parm.pin);
482 break;
484 case IFXMIPS_PORT_IOCALTSEL0:
485 if (parm.value == 0x00)
486 ifxmips_port_clear_altsel0(parm.port, parm.pin);
487 else
488 ifxmips_port_set_altsel0(parm.port, parm.pin);
489 break;
491 case IFXMIPS_PORT_IOCALTSEL1:
492 if (parm.value == 0x00)
493 ifxmips_port_clear_altsel1(parm.port, parm.pin);
494 else
495 ifxmips_port_set_altsel1(parm.port, parm.pin);
496 break;
498 case IFXMIPS_PORT_IOCINPUT:
499 parm.value = ifxmips_port_get_input(parm.port, parm.pin);
500 copy_to_user((void*)arg, (void*)&parm,
501 sizeof(struct ifxmips_port_ioctl_parm));
502 break;
504 default:
505 ret = -EINVAL;
508 up (&port_sem);
510 return ret;
513 static struct file_operations port_fops = {
514 .open = ifxmips_port_open,
515 .release = ifxmips_port_release,
516 .ioctl = ifxmips_port_ioctl
519 static int
520 ifxmips_gpio_probe (struct platform_device *dev)
522 int retval = 0;
524 sema_init (&port_sem, 1);
526 ifxmips_gpio_major = register_chrdev(0, DRVNAME, &port_fops);
527 if (!ifxmips_gpio_major)
529 printk(KERN_INFO DRVNAME ": Error! Could not register port device. #%d\n", ifxmips_gpio_major);
530 retval = -EINVAL;
531 goto out;
534 create_proc_read_entry(DRVNAME, 0, NULL, ifxmips_port_read_procmem, NULL);
536 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
537 ifxmips_port_set_open_drain(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN);
538 ifxmips_port_clear_altsel0(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN);
539 ifxmips_port_clear_altsel1(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN);
540 ifxmips_port_set_dir_in(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN);
542 seen = jiffies;
544 init_timer(&rst_button_timer);
545 rst_button_timer.function = reset_button_poll;
546 rst_button_timer.expires = jiffies + HZ;
547 add_timer(&rst_button_timer);
548 #endif
550 printk(KERN_INFO DRVNAME ": device successfully initialized #%d.\n", ifxmips_gpio_major);
552 out:
553 return retval;
556 static int
557 ifxmips_gpio_remove (struct platform_device *pdev)
559 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
560 del_timer_sync(&rst_button_timer);
561 #endif
562 unregister_chrdev(ifxmips_gpio_major, DRVNAME);
563 remove_proc_entry(DRVNAME, NULL);
565 return 0;
568 static struct
569 platform_driver ifxmips_gpio_driver = {
570 .probe = ifxmips_gpio_probe,
571 .remove = ifxmips_gpio_remove,
572 .driver = {
573 .name = DRVNAME,
574 .owner = THIS_MODULE,
578 int __init
579 ifxmips_gpio_init (void)
581 int ret = platform_driver_register(&ifxmips_gpio_driver);
582 if (ret)
583 printk(KERN_INFO DRVNAME ": Error registering platfom driver!");
584 return ret;
587 void __exit
588 ifxmips_gpio_exit (void)
590 platform_driver_unregister(&ifxmips_gpio_driver);
593 module_init(ifxmips_gpio_init);
594 module_exit(ifxmips_gpio_exit);