add gpio fixes and reset button supportÂ
[openwrt/openwrt.git] / target / linux / ifxmips / files / drivers / char / ifxmips_gpio.c
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.
6 *
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.
11 *
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.
15 *
16 * Copyright (C) 2005 infineon
17 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
18 *
19 */
20
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 <net/sock.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>
41
42 #define MAX_PORTS 2
43 #define PINS_PER_PORT 16
44
45 static unsigned int ifxmips_gpio_major = 0;
46
47 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
48 #define IFXMIPS_RST_PIN 15
49 #define IFXMIPS_RST_PORT 1
50
51 static struct timer_list rst_button_timer;
52
53 extern struct sock *uevent_sock;
54 extern u64 uevent_next_seqnum(void);
55 static unsigned long seen;
56 static int pressed = 0;
57
58 struct event_t {
59 struct work_struct wq;
60 int set;
61 unsigned long jiffies;
62 };
63 #endif
64
65 /* TODO do we need this ? */
66 static struct semaphore port_sem;
67
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; }
70
71 int
72 ifxmips_port_reserve_pin (unsigned int port, unsigned int pin)
73 {
74 IFXMIPS_GPIO_SANITY;
75 printk("%s : call to obseleted function\n", __func__);
76
77 return 0;
78 }
79 EXPORT_SYMBOL(ifxmips_port_reserve_pin);
80
81 int
82 ifxmips_port_free_pin (unsigned int port, unsigned int pin)
83 {
84 IFXMIPS_GPIO_SANITY;
85 printk("%s : call to obseleted function\n", __func__);
86
87 return 0;
88 }
89 EXPORT_SYMBOL(ifxmips_port_free_pin);
90
91 int
92 ifxmips_port_set_open_drain (unsigned int port, unsigned int pin)
93 {
94 IFXMIPS_GPIO_SANITY;
95 writel(readl(IFXMIPS_GPIO_P0_OD + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_OD + (port * 0xC));
96
97 return 0;
98 }
99 EXPORT_SYMBOL(ifxmips_port_set_open_drain);
100
101 int
102 ifxmips_port_clear_open_drain (unsigned int port, unsigned int pin)
103 {
104 IFXMIPS_GPIO_SANITY;
105 writel(readl(IFXMIPS_GPIO_P0_OD + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_OD + (port * 0xC));
106
107 return 0;
108 }
109 EXPORT_SYMBOL(ifxmips_port_clear_open_drain);
110
111 int
112 ifxmips_port_set_pudsel (unsigned int port, unsigned int pin)
113 {
114 IFXMIPS_GPIO_SANITY;
115 writel(readl(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
116
117 return 0;
118 }
119 EXPORT_SYMBOL(ifxmips_port_set_pudsel);
120
121 int
122 ifxmips_port_clear_pudsel (unsigned int port, unsigned int pin)
123 {
124 IFXMIPS_GPIO_SANITY;
125 writel(readl(IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDSEL + (port * 0xC));
126
127 return 0;
128 }
129 EXPORT_SYMBOL(ifxmips_port_clear_pudsel);
130
131 int
132 ifxmips_port_set_puden (unsigned int port, unsigned int pin)
133 {
134 IFXMIPS_GPIO_SANITY;
135 writel(readl(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
136
137 return 0;
138 }
139 EXPORT_SYMBOL(ifxmips_port_set_puden);
140
141 int
142 ifxmips_port_clear_puden (unsigned int port, unsigned int pin)
143 {
144 IFXMIPS_GPIO_SANITY;
145 writel(readl(IFXMIPS_GPIO_P0_PUDEN + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_PUDEN + (port * 0xC));
146
147 return 0;
148 }
149 EXPORT_SYMBOL(ifxmips_port_clear_puden);
150
151 int
152 ifxmips_port_set_stoff (unsigned int port, unsigned int pin)
153 {
154 IFXMIPS_GPIO_SANITY;
155 writel(readl(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
156
157 return 0;
158 }
159 EXPORT_SYMBOL(ifxmips_port_set_stoff);
160
161 int
162 ifxmips_port_clear_stoff (unsigned int port, unsigned int pin)
163 {
164 IFXMIPS_GPIO_SANITY;
165 writel(readl(IFXMIPS_GPIO_P0_STOFF + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_STOFF + (port * 0xC));
166
167 return 0;
168 }
169 EXPORT_SYMBOL(ifxmips_port_clear_stoff);
170
171 int
172 ifxmips_port_set_dir_out (unsigned int port, unsigned int pin)
173 {
174 IFXMIPS_GPIO_SANITY;
175 writel(readl(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_DIR + (port * 0xC));
176
177 return 0;
178 }
179 EXPORT_SYMBOL(ifxmips_port_set_dir_out);
180
181 int
182 ifxmips_port_set_dir_in (unsigned int port, unsigned int pin)
183 {
184 IFXMIPS_GPIO_SANITY;
185 writel(readl(IFXMIPS_GPIO_P0_DIR + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_DIR + (port * 0xC));
186
187 return 0;
188 }
189 EXPORT_SYMBOL(ifxmips_port_set_dir_in);
190
191 int
192 ifxmips_port_set_output (unsigned int port, unsigned int pin)
193 {
194 IFXMIPS_GPIO_SANITY;
195 writel(readl(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_OUT + (port * 0xC));
196
197 return 0;
198 }
199 EXPORT_SYMBOL(ifxmips_port_set_output);
200
201 int
202 ifxmips_port_clear_output (unsigned int port, unsigned int pin)
203 {
204 IFXMIPS_GPIO_SANITY;
205 writel(readl(IFXMIPS_GPIO_P0_OUT + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_OUT + (port * 0xC));
206
207 return 0;
208 }
209 EXPORT_SYMBOL(ifxmips_port_clear_output);
210
211 int
212 ifxmips_port_get_input (unsigned int port, unsigned int pin)
213 {
214 IFXMIPS_GPIO_SANITY;
215
216 if (readl(IFXMIPS_GPIO_P0_IN + (port * 0xC)) & (1 << pin))
217 return 0;
218 else
219 return 1;
220 }
221 EXPORT_SYMBOL(ifxmips_port_get_input);
222
223 int
224 ifxmips_port_set_altsel0 (unsigned int port, unsigned int pin)
225 {
226 IFXMIPS_GPIO_SANITY;
227 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
228
229 return 0;
230 }
231 EXPORT_SYMBOL(ifxmips_port_set_altsel0);
232
233 int
234 ifxmips_port_clear_altsel0 (unsigned int port, unsigned int pin)
235 {
236 IFXMIPS_GPIO_SANITY;
237 writel(readl(IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL0 + (port * 0xC));
238
239 return 0;
240 }
241 EXPORT_SYMBOL(ifxmips_port_clear_altsel0);
242
243 int
244 ifxmips_port_set_altsel1 (unsigned int port, unsigned int pin)
245 {
246 IFXMIPS_GPIO_SANITY;
247 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) | (1 << pin), IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
248
249 return 0;
250 }
251 EXPORT_SYMBOL(ifxmips_port_set_altsel1);
252
253 int
254 ifxmips_port_clear_altsel1 (unsigned int port, unsigned int pin)
255 {
256 IFXMIPS_GPIO_SANITY;
257 writel(readl(IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC)) & ~(1 << pin), IFXMIPS_GPIO_P0_ALTSEL1 + (port * 0xC));
258
259 return 0;
260 }
261 EXPORT_SYMBOL(ifxmips_port_clear_altsel1);
262
263 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
264 static inline void add_msg(struct sk_buff *skb, char *msg)
265 {
266 char *scratch;
267 scratch = skb_put(skb, strlen(msg) + 1);
268 sprintf(scratch, msg);
269 }
270
271 static void hotplug_button(struct work_struct *wq)
272 {
273 struct sk_buff *skb;
274 struct event_t *event;
275 size_t len;
276 char *scratch, *s;
277 char buf[128];
278
279 event = container_of(wq, struct event_t, wq);
280 if (!uevent_sock)
281 goto done;
282
283 /* allocate message with the maximum possible size */
284 s = event->set ? "pressed" : "released";
285 len = strlen(s) + 2;
286 skb = alloc_skb(len + 2048, GFP_KERNEL);
287 if (!skb)
288 goto done;
289
290 /* add header */
291 scratch = skb_put(skb, len);
292 sprintf(scratch, "%s@",s);
293
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);
301 add_msg(skb, buf);
302 snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
303 add_msg(skb, buf);
304
305 NETLINK_CB(skb).dst_group = 1;
306 netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
307
308 done:
309 kfree(event);
310 }
311
312 static void reset_button_poll(unsigned long unused)
313 {
314 struct event_t *event;
315
316 rst_button_timer.expires = jiffies + HZ;
317 add_timer(&rst_button_timer);
318
319 if (pressed != ifxmips_port_get_input(IFXMIPS_RST_PORT, IFXMIPS_RST_PIN))
320 {
321 if(pressed)
322 pressed = 0;
323 else
324 pressed = 1;
325 printk("button was %s\n", (pressed ? "pressed" : "released"));
326 event = (struct event_t *) kzalloc(sizeof(struct event_t), GFP_ATOMIC);
327 if (!event)
328 {
329 printk("Could not alloc hotplug event\n");
330 return;
331 }
332 event->set = pressed;
333 event->jiffies = jiffies;
334 INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
335 schedule_work(&event->wq);
336 seen = jiffies;
337 }
338 }
339 #endif
340
341 long ifxmips_port_read_procmem_helper(char* tag, u32* in_reg, char *buf)
342 {
343 u32 reg, bit = 0;
344 unsigned int len, t;
345
346 len = sprintf(buf, "\n%s: ", tag);
347 reg = readl(in_reg);
348 bit = 0x80000000;
349 for (t = 0; t < 32; t++) {
350 if ((reg & bit) > 0)
351 len = len + sprintf(buf + len, "X");
352 else
353 len = len + sprintf(buf + len, " ");
354 bit = bit >> 1;
355 }
356
357 return len;
358 }
359
360 int
361 ifxmips_port_read_procmem (char *buf, char **start, off_t offset, int count,
362 int *eof, void *data)
363 {
364 long len = sprintf (buf, "\nIFXMips Port Settings\n");
365
366 len += sprintf (buf + len,
367 " 3 2 1 0\n");
368 len += sprintf (buf + len,
369 " 10987654321098765432109876543210\n");
370 len += sprintf (buf + len,
371 "----------------------------------------\n");
372
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");
392
393 *eof = 1;
394
395 return len;
396 }
397
398 static int
399 ifxmips_port_open (struct inode *inode, struct file *filep)
400 {
401 return 0;
402 }
403
404 static int
405 ifxmips_port_release (struct inode *inode, struct file *filelp)
406 {
407 return 0;
408 }
409
410 static int
411 ifxmips_port_ioctl (struct inode *inode, struct file *filp,
412 unsigned int cmd, unsigned long arg)
413 {
414 int ret = 0;
415 volatile struct ifxmips_port_ioctl_parm parm;
416
417 if (_IOC_TYPE (cmd) != IFXMIPS_PORT_IOC_MAGIC)
418 return -EINVAL;
419
420 if (_IOC_DIR (cmd) & _IOC_WRITE) {
421 if (!access_ok
422 (VERIFY_READ, arg,
423 sizeof (struct ifxmips_port_ioctl_parm)))
424 return -EFAULT;
425 ret = copy_from_user ((void *) &parm, (void *) arg,
426 sizeof (struct ifxmips_port_ioctl_parm));
427 }
428 if (_IOC_DIR (cmd) & _IOC_READ) {
429 if (!access_ok
430 (VERIFY_WRITE, arg,
431 sizeof (struct ifxmips_port_ioctl_parm)))
432 return -EFAULT;
433 }
434
435 if (down_trylock (&port_sem) != 0)
436 return -EBUSY;
437
438 switch (cmd) {
439 case IFXMIPS_PORT_IOCOD:
440 if (parm.value == 0x00)
441 ifxmips_port_clear_open_drain(parm.port, parm.pin);
442 else
443 ifxmips_port_set_open_drain(parm.port, parm.pin);
444 break;
445
446 case IFXMIPS_PORT_IOCPUDSEL:
447 if (parm.value == 0x00)
448 ifxmips_port_clear_pudsel(parm.port, parm.pin);
449 else
450 ifxmips_port_set_pudsel(parm.port, parm.pin);
451 break;
452
453 case IFXMIPS_PORT_IOCPUDEN:
454 if (parm.value == 0x00)
455 ifxmips_port_clear_puden(parm.port, parm.pin);
456 else
457 ifxmips_port_set_puden(parm.port, parm.pin);
458 break;
459
460 case IFXMIPS_PORT_IOCSTOFF:
461 if (parm.value == 0x00)
462 ifxmips_port_clear_stoff(parm.port, parm.pin);
463 else
464 ifxmips_port_set_stoff(parm.port, parm.pin);
465 break;
466
467 case IFXMIPS_PORT_IOCDIR:
468 if (parm.value == 0x00)
469 ifxmips_port_set_dir_in(parm.port, parm.pin);
470 else
471 ifxmips_port_set_dir_out(parm.port, parm.pin);
472 break;
473
474 case IFXMIPS_PORT_IOCOUTPUT:
475 if (parm.value == 0x00)
476 ifxmips_port_clear_output(parm.port, parm.pin);
477 else
478 ifxmips_port_set_output(parm.port, parm.pin);
479 break;
480
481 case IFXMIPS_PORT_IOCALTSEL0:
482 if (parm.value == 0x00)
483 ifxmips_port_clear_altsel0(parm.port, parm.pin);
484 else
485 ifxmips_port_set_altsel0(parm.port, parm.pin);
486 break;
487
488 case IFXMIPS_PORT_IOCALTSEL1:
489 if (parm.value == 0x00)
490 ifxmips_port_clear_altsel1(parm.port, parm.pin);
491 else
492 ifxmips_port_set_altsel1(parm.port, parm.pin);
493 break;
494
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));
499 break;
500
501 default:
502 ret = -EINVAL;
503 }
504
505 up (&port_sem);
506
507 return ret;
508 }
509
510 static struct file_operations port_fops = {
511 .open = ifxmips_port_open,
512 .release = ifxmips_port_release,
513 .ioctl = ifxmips_port_ioctl
514 };
515
516 int __init
517 ifxmips_gpio_init (void)
518 {
519 int retval = 0;
520
521 sema_init (&port_sem, 1);
522
523 ifxmips_gpio_major = register_chrdev(0, "ifxmips_gpio", &port_fops);
524 if (!ifxmips_gpio_major)
525 {
526 printk("ifxmips-port: Error! Could not register port device. #%d\n", ifxmips_gpio_major);
527 retval = -EINVAL;
528 goto out;
529 }
530
531 create_proc_read_entry("ifxmips_gpio", 0, NULL,
532 ifxmips_port_read_procmem, NULL);
533
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);
539
540 seen = jiffies;
541
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);
546 #endif
547
548 printk("registered ifxmips gpio driver\n");
549
550 out:
551 return retval;
552 }
553
554 void __exit
555 ifxmips_gpio_exit (void)
556 {
557 #ifdef CONFIG_IFXMIPS_GPIO_RST_BTN
558 del_timer_sync(&rst_button_timer);
559 #endif
560 unregister_chrdev(ifxmips_gpio_major, "ifxmips_gpio");
561 remove_proc_entry("ifxmips_gpio", NULL);
562 }
563
564 module_init(ifxmips_gpio_init);
565 module_exit(ifxmips_gpio_exit);