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>
36 #include <asm/uaccess.h>
37 #include <asm/semaphore.h>
38 #include <asm/uaccess.h>
39 #include <asm/ifxmips/ifxmips.h>
40 #include <asm/ifxmips/ifxmips_ioctl.h>
43 #define PINS_PER_PORT 16
45 static unsigned int ifxmips_gpio_major
= 0;
47 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
48 #define IFXMIPS_RST_PIN 15
49 #define IFXMIPS_RST_PORT 1
51 static struct timer_list rst_button_timer
;
53 extern struct sock
*uevent_sock
;
54 extern u64
uevent_next_seqnum(void);
55 static unsigned long seen
;
56 static int pressed
= 0;
59 struct work_struct wq
;
61 unsigned long jiffies
;
65 /* TODO do we need this ? */
66 static struct semaphore port_sem
;
68 /* TODO do we really need this ? return in a define is forbidden by coding style */
69 #define IFXMIPS_GPIO_SANITY {if (port > MAX_PORTS || pin > PINS_PER_PORT) return -EINVAL; }
72 ifxmips_port_reserve_pin (unsigned int port
, unsigned int pin
)
75 printk("%s : call to obseleted function\n", __func__
);
79 EXPORT_SYMBOL(ifxmips_port_reserve_pin
);
82 ifxmips_port_free_pin (unsigned int port
, unsigned int pin
)
85 printk("%s : call to obseleted function\n", __func__
);
89 EXPORT_SYMBOL(ifxmips_port_free_pin
);
92 ifxmips_port_set_open_drain (unsigned int port
, unsigned int pin
)
95 writel(readl(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
99 EXPORT_SYMBOL(ifxmips_port_set_open_drain
);
102 ifxmips_port_clear_open_drain (unsigned int port
, unsigned int pin
)
105 writel(readl(IFXMIPS_GPIO_P0_OD
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_OD
+ (port
* 0xC));
109 EXPORT_SYMBOL(ifxmips_port_clear_open_drain
);
112 ifxmips_port_set_pudsel (unsigned int port
, unsigned int pin
)
115 writel(readl(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
119 EXPORT_SYMBOL(ifxmips_port_set_pudsel
);
122 ifxmips_port_clear_pudsel (unsigned int port
, unsigned int pin
)
125 writel(readl(IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_PUDSEL
+ (port
* 0xC));
129 EXPORT_SYMBOL(ifxmips_port_clear_pudsel
);
132 ifxmips_port_set_puden (unsigned int port
, unsigned int pin
)
135 writel(readl(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
139 EXPORT_SYMBOL(ifxmips_port_set_puden
);
142 ifxmips_port_clear_puden (unsigned int port
, unsigned int pin
)
145 writel(readl(IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_PUDEN
+ (port
* 0xC));
149 EXPORT_SYMBOL(ifxmips_port_clear_puden
);
152 ifxmips_port_set_stoff (unsigned int port
, unsigned int pin
)
155 writel(readl(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
159 EXPORT_SYMBOL(ifxmips_port_set_stoff
);
162 ifxmips_port_clear_stoff (unsigned int port
, unsigned int pin
)
165 writel(readl(IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_STOFF
+ (port
* 0xC));
169 EXPORT_SYMBOL(ifxmips_port_clear_stoff
);
172 ifxmips_port_set_dir_out (unsigned int port
, unsigned int pin
)
175 writel(readl(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
179 EXPORT_SYMBOL(ifxmips_port_set_dir_out
);
182 ifxmips_port_set_dir_in (unsigned int port
, unsigned int pin
)
185 writel(readl(IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_DIR
+ (port
* 0xC));
189 EXPORT_SYMBOL(ifxmips_port_set_dir_in
);
192 ifxmips_port_set_output (unsigned int port
, unsigned int pin
)
195 writel(readl(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
199 EXPORT_SYMBOL(ifxmips_port_set_output
);
202 ifxmips_port_clear_output (unsigned int port
, unsigned int pin
)
205 writel(readl(IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_OUT
+ (port
* 0xC));
209 EXPORT_SYMBOL(ifxmips_port_clear_output
);
212 ifxmips_port_get_input (unsigned int port
, unsigned int pin
)
216 if (readl(IFXMIPS_GPIO_P0_IN
+ (port
* 0xC)) & (1 << pin
))
221 EXPORT_SYMBOL(ifxmips_port_get_input
);
224 ifxmips_port_set_altsel0 (unsigned int port
, unsigned int pin
)
227 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
231 EXPORT_SYMBOL(ifxmips_port_set_altsel0
);
234 ifxmips_port_clear_altsel0 (unsigned int port
, unsigned int pin
)
237 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_ALTSEL0
+ (port
* 0xC));
241 EXPORT_SYMBOL(ifxmips_port_clear_altsel0
);
244 ifxmips_port_set_altsel1 (unsigned int port
, unsigned int pin
)
247 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) | (1 << pin
), IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
251 EXPORT_SYMBOL(ifxmips_port_set_altsel1
);
254 ifxmips_port_clear_altsel1 (unsigned int port
, unsigned int pin
)
257 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC)) & ~(1 << pin
), IFXMIPS_GPIO_P0_ALTSEL1
+ (port
* 0xC));
261 EXPORT_SYMBOL(ifxmips_port_clear_altsel1
);
263 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
264 static inline void add_msg(struct sk_buff
*skb
, char *msg
)
267 scratch
= skb_put(skb
, strlen(msg
) + 1);
268 sprintf(scratch
, msg
);
271 static void hotplug_button(struct work_struct
*wq
)
274 struct event_t
*event
;
279 event
= container_of(wq
, struct event_t
, wq
);
283 /* allocate message with the maximum possible size */
284 s
= event
->set
? "pressed" : "released";
286 skb
= alloc_skb(len
+ 2048, GFP_KERNEL
);
291 scratch
= skb_put(skb
, len
);
292 sprintf(scratch
, "%s@",s
);
294 /* copy keys to our continuous event payload buffer */
295 add_msg(skb
, "HOME=/");
296 add_msg(skb
, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
297 add_msg(skb
, "SUBSYSTEM=button");
298 add_msg(skb
, "BUTTON=reset");
299 add_msg(skb
, (event
->set
? "ACTION=pressed" : "ACTION=released"));
300 sprintf(buf
, "SEEN=%ld", (event
->jiffies
- seen
)/HZ
);
302 snprintf(buf
, 128, "SEQNUM=%llu", uevent_next_seqnum());
305 NETLINK_CB(skb
).dst_group
= 1;
306 netlink_broadcast(uevent_sock
, skb
, 0, 1, GFP_KERNEL
);
312 static void reset_button_poll(unsigned long unused
)
314 struct event_t
*event
;
316 rst_button_timer
.expires
= jiffies
+ HZ
;
317 add_timer(&rst_button_timer
);
319 if (pressed
!= ifxmips_port_get_input(IFXMIPS_RST_PORT
, IFXMIPS_RST_PIN
))
325 printk("button was %s\n", (pressed
? "pressed" : "released"));
326 event
= (struct event_t
*) kzalloc(sizeof(struct event_t
), GFP_ATOMIC
);
329 printk("Could not alloc hotplug event\n");
332 event
->set
= pressed
;
333 event
->jiffies
= jiffies
;
334 INIT_WORK(&event
->wq
, (void *)(void *)hotplug_button
);
335 schedule_work(&event
->wq
);
341 long ifxmips_port_read_procmem_helper(char* tag
, u32
* in_reg
, char *buf
)
346 len
= sprintf(buf
, "\n%s: ", tag
);
349 for (t
= 0; t
< 32; t
++) {
351 len
= len
+ sprintf(buf
+ len
, "X");
353 len
= len
+ sprintf(buf
+ len
, " ");
361 ifxmips_port_read_procmem (char *buf
, char **start
, off_t offset
, int count
,
362 int *eof
, void *data
)
364 long len
= sprintf (buf
, "\nIFXMips Port Settings\n");
366 len
+= sprintf (buf
+ len
,
368 len
+= sprintf (buf
+ len
,
369 " 10987654321098765432109876543210\n");
370 len
+= sprintf (buf
+ len
,
371 "----------------------------------------\n");
373 len
+= ifxmips_port_read_procmem_helper("P0-OUT", IFXMIPS_GPIO_P0_OUT
, &buf
[len
]);
374 len
+= ifxmips_port_read_procmem_helper("P1-OUT", IFXMIPS_GPIO_P1_OUT
, &buf
[len
]);
375 len
+= ifxmips_port_read_procmem_helper("P0-IN ", IFXMIPS_GPIO_P0_IN
, &buf
[len
]);
376 len
+= ifxmips_port_read_procmem_helper("P1-IN ", IFXMIPS_GPIO_P1_IN
, &buf
[len
]);
377 len
+= ifxmips_port_read_procmem_helper("P0-DIR", IFXMIPS_GPIO_P0_DIR
, &buf
[len
]);
378 len
+= ifxmips_port_read_procmem_helper("P1-DIR", IFXMIPS_GPIO_P1_DIR
, &buf
[len
]);
379 len
+= ifxmips_port_read_procmem_helper("P0-STO ", IFXMIPS_GPIO_P0_STOFF
, &buf
[len
]);
380 len
+= ifxmips_port_read_procmem_helper("P1-STO ", IFXMIPS_GPIO_P1_STOFF
, &buf
[len
]);
381 len
+= ifxmips_port_read_procmem_helper("P0-PUDE", IFXMIPS_GPIO_P0_PUDEN
, &buf
[len
]);
382 len
+= ifxmips_port_read_procmem_helper("P1-PUDE", IFXMIPS_GPIO_P1_PUDEN
, &buf
[len
]);
383 len
+= ifxmips_port_read_procmem_helper("P0-OD ", IFXMIPS_GPIO_P0_OD
, &buf
[len
]);
384 len
+= ifxmips_port_read_procmem_helper("P1-OD ", IFXMIPS_GPIO_P1_OD
, &buf
[len
]);
385 len
+= ifxmips_port_read_procmem_helper("P0-PUDS", IFXMIPS_GPIO_P0_PUDSEL
, &buf
[len
]);
386 len
+= ifxmips_port_read_procmem_helper("P1-PUDS", IFXMIPS_GPIO_P1_PUDSEL
, &buf
[len
]);
387 len
+= ifxmips_port_read_procmem_helper("P0-ALT0", IFXMIPS_GPIO_P0_ALTSEL0
, &buf
[len
]);
388 len
+= ifxmips_port_read_procmem_helper("P1-ALT0", IFXMIPS_GPIO_P1_ALTSEL0
, &buf
[len
]);
389 len
+= ifxmips_port_read_procmem_helper("P0-ALT1", IFXMIPS_GPIO_P0_ALTSEL1
, &buf
[len
]);
390 len
+= ifxmips_port_read_procmem_helper("P1-ALT1", IFXMIPS_GPIO_P1_ALTSEL1
, &buf
[len
]);
391 len
= len
+ sprintf (buf
+ len
, "\n\n");
399 ifxmips_port_open (struct inode
*inode
, struct file
*filep
)
405 ifxmips_port_release (struct inode
*inode
, struct file
*filelp
)
411 ifxmips_port_ioctl (struct inode
*inode
, struct file
*filp
,
412 unsigned int cmd
, unsigned long arg
)
415 volatile struct ifxmips_port_ioctl_parm parm
;
417 if (_IOC_TYPE (cmd
) != IFXMIPS_PORT_IOC_MAGIC
)
420 if (_IOC_DIR (cmd
) & _IOC_WRITE
) {
423 sizeof (struct ifxmips_port_ioctl_parm
)))
425 ret
= copy_from_user ((void *) &parm
, (void *) arg
,
426 sizeof (struct ifxmips_port_ioctl_parm
));
428 if (_IOC_DIR (cmd
) & _IOC_READ
) {
431 sizeof (struct ifxmips_port_ioctl_parm
)))
435 if (down_trylock (&port_sem
) != 0)
439 case IFXMIPS_PORT_IOCOD
:
440 if (parm
.value
== 0x00)
441 ifxmips_port_clear_open_drain(parm
.port
, parm
.pin
);
443 ifxmips_port_set_open_drain(parm
.port
, parm
.pin
);
446 case IFXMIPS_PORT_IOCPUDSEL
:
447 if (parm
.value
== 0x00)
448 ifxmips_port_clear_pudsel(parm
.port
, parm
.pin
);
450 ifxmips_port_set_pudsel(parm
.port
, parm
.pin
);
453 case IFXMIPS_PORT_IOCPUDEN
:
454 if (parm
.value
== 0x00)
455 ifxmips_port_clear_puden(parm
.port
, parm
.pin
);
457 ifxmips_port_set_puden(parm
.port
, parm
.pin
);
460 case IFXMIPS_PORT_IOCSTOFF
:
461 if (parm
.value
== 0x00)
462 ifxmips_port_clear_stoff(parm
.port
, parm
.pin
);
464 ifxmips_port_set_stoff(parm
.port
, parm
.pin
);
467 case IFXMIPS_PORT_IOCDIR
:
468 if (parm
.value
== 0x00)
469 ifxmips_port_set_dir_in(parm
.port
, parm
.pin
);
471 ifxmips_port_set_dir_out(parm
.port
, parm
.pin
);
474 case IFXMIPS_PORT_IOCOUTPUT
:
475 if (parm
.value
== 0x00)
476 ifxmips_port_clear_output(parm
.port
, parm
.pin
);
478 ifxmips_port_set_output(parm
.port
, parm
.pin
);
481 case IFXMIPS_PORT_IOCALTSEL0
:
482 if (parm
.value
== 0x00)
483 ifxmips_port_clear_altsel0(parm
.port
, parm
.pin
);
485 ifxmips_port_set_altsel0(parm
.port
, parm
.pin
);
488 case IFXMIPS_PORT_IOCALTSEL1
:
489 if (parm
.value
== 0x00)
490 ifxmips_port_clear_altsel1(parm
.port
, parm
.pin
);
492 ifxmips_port_set_altsel1(parm
.port
, parm
.pin
);
495 case IFXMIPS_PORT_IOCINPUT
:
496 parm
.value
= ifxmips_port_get_input(parm
.port
, parm
.pin
);
497 copy_to_user((void*)arg
, (void*)&parm
,
498 sizeof(struct ifxmips_port_ioctl_parm
));
510 static struct file_operations port_fops
= {
511 .open
= ifxmips_port_open
,
512 .release
= ifxmips_port_release
,
513 .ioctl
= ifxmips_port_ioctl
517 ifxmips_gpio_init (void)
521 sema_init (&port_sem
, 1);
523 ifxmips_gpio_major
= register_chrdev(0, "ifxmips_gpio", &port_fops
);
524 if (!ifxmips_gpio_major
)
526 printk("ifxmips-port: Error! Could not register port device. #%d\n", ifxmips_gpio_major
);
531 create_proc_read_entry("ifxmips_gpio", 0, NULL
,
532 ifxmips_port_read_procmem
, NULL
);
534 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
535 ifxmips_port_set_open_drain(IFXMIPS_RST_PORT
, IFXMIPS_RST_PIN
);
536 ifxmips_port_clear_altsel0(IFXMIPS_RST_PORT
, IFXMIPS_RST_PIN
);
537 ifxmips_port_clear_altsel1(IFXMIPS_RST_PORT
, IFXMIPS_RST_PIN
);
538 ifxmips_port_set_dir_in(IFXMIPS_RST_PORT
, IFXMIPS_RST_PIN
);
542 init_timer(&rst_button_timer
);
543 rst_button_timer
.function
= reset_button_poll
;
544 rst_button_timer
.expires
= jiffies
+ HZ
;
545 add_timer(&rst_button_timer
);
548 printk("registered ifxmips gpio driver\n");
555 ifxmips_gpio_exit (void)
557 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
558 del_timer_sync(&rst_button_timer
);
560 unregister_chrdev(ifxmips_gpio_major
, "ifxmips_gpio");
561 remove_proc_entry("ifxmips_gpio", NULL
);
564 module_init(ifxmips_gpio_init
);
565 module_exit(ifxmips_gpio_exit
);