1 #include <linux/module.h>
2 #include <linux/device.h>
3 #include <linux/platform_device.h>
4 #include <linux/gpio_event.h>
5 #include <linux/interrupt.h>
9 #define PREFIX "debounce: "
11 static unsigned old_flags
= 0;
12 static ktime_t old_debounce_delay
;
13 static ktime_t old_settle_time
;
14 static ktime_t old_poll_time
;
15 static int (*old_sw_fixup
)(int index
);
16 static struct gpio_event_matrix_info
*gpio_evmi
= NULL
;
17 static int hw_debounce
= 0;
18 static int hw_debounce_time
= 0;
20 static int find_ms2_dev(struct device
*dev
, void *data
)
22 if (!strncmp((char*)data
, dev_name(dev
), strlen((char*)data
))) {
23 printk(KERN_INFO PREFIX
"Found it\n");
29 /* hardware debounce: (time + 1) * 31us */
30 static void hw_debounce_set(int enable
, int time
) {
33 if (gpio_evmi
== NULL
)
36 for (i
= 0; i
< gpio_evmi
->ninputs
; i
++) {
37 int gpio
= gpio_evmi
->input_gpios
[i
];
39 if ((time
!= -1) && (time
!= hw_debounce_time
) && hw_debounce
) {
40 printk(KERN_INFO PREFIX
"Setting hardware debounce time for GPIO %d to %d (%dus)\n", gpio
, time
, (time
+1)*31);
41 omap_set_gpio_debounce_time(gpio
, time
);
44 if ((enable
!= -1) && (enable
!= hw_debounce
)) {
45 printk(KERN_INFO PREFIX
"%sabling hardware debounce for GPIO %d\n", (enable
?"En":"Dis"), gpio
);
46 omap_set_gpio_debounce(gpio
, enable
);
51 static void set_irq_types(void) {
57 if (gpio_evmi
== NULL
)
60 switch (gpio_evmi
->flags
& (GPIOKPF_ACTIVE_HIGH
|GPIOKPF_LEVEL_TRIGGERED_IRQ
)) {
62 type
= IRQ_TYPE_EDGE_FALLING
;
64 case GPIOKPF_ACTIVE_HIGH
:
65 type
= IRQ_TYPE_EDGE_RISING
;
67 case GPIOKPF_LEVEL_TRIGGERED_IRQ
:
68 type
= IRQ_TYPE_LEVEL_LOW
;
70 case GPIOKPF_LEVEL_TRIGGERED_IRQ
| GPIOKPF_ACTIVE_HIGH
:
71 type
= IRQ_TYPE_LEVEL_HIGH
;
75 printk(KERN_INFO PREFIX
"Settinhg IRQ type to 0x%lx\n", type
);
77 for (i
= 0; i
< gpio_evmi
->ninputs
; i
++) {
79 err
= irq
= gpio_to_irq(gpio_evmi
->input_gpios
[i
]);
84 err
= set_irq_type(irq
, type
);
88 static ssize_t
show_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
93 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->debounce_delay
.tv
.nsec
/ NSEC_PER_MSEC
));
96 static void set_debounce_delay(long delay
)
98 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= delay
* NSEC_PER_MSEC
) {
99 printk(KERN_INFO PREFIX
"Changing debounce_delay\n");
100 gpio_evmi
->debounce_delay
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
101 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
105 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= 0) {
106 if (!(gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
)) {
107 printk(KERN_INFO PREFIX
"Activating debounce\n");
108 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
111 if (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) {
112 printk(KERN_INFO PREFIX
"Deactivating debounce\n");
113 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
119 static ssize_t
store_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
126 sscanf(buf
, "%ld", &delay
);
127 set_debounce_delay(delay
);
132 static ssize_t
show_settle_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
137 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->settle_time
.tv
.nsec
/ NSEC_PER_USEC
));
140 static ssize_t
store_settle_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
147 sscanf(buf
, "%ld", &delay
);
148 gpio_evmi
->settle_time
.tv
.nsec
= delay
* NSEC_PER_USEC
;
153 static ssize_t
show_poll_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
158 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->poll_time
.tv
.nsec
/ NSEC_PER_MSEC
));
161 static ssize_t
store_poll_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
168 sscanf(buf
, "%ld", &delay
);
169 gpio_evmi
->poll_time
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
174 static ssize_t
show_flags(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
179 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", gpio_evmi
->flags
);
182 static ssize_t
store_flags(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
189 sscanf(buf
, "0x%x", &flags
);
191 printk(KERN_INFO PREFIX
"flags: 0x%x\n", flags
);
193 gpio_evmi
->flags
= flags
;
198 static ssize_t
show_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
203 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) ? 1 : 0);
206 static ssize_t
store_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
213 sscanf(buf
, "%u", &flag
);
216 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
218 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
224 static ssize_t
show_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
229 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
) ? 1 : 0);
232 static ssize_t
store_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
239 sscanf(buf
, "%u", &flag
);
242 gpio_evmi
->flags
|= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
244 gpio_evmi
->flags
&= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
250 static ssize_t
show_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
255 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_UNMAPPED_KEYS
) ? 1 : 0);
258 static ssize_t
store_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
265 sscanf(buf
, "%u", &flag
);
268 gpio_evmi
->flags
|= GPIOKPF_PRINT_UNMAPPED_KEYS
;
270 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_UNMAPPED_KEYS
;
276 static ssize_t
show_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
281 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_MAPPED_KEYS
) ? 1 : 0);
284 static ssize_t
store_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
291 sscanf(buf
, "%u", &flag
);
294 gpio_evmi
->flags
|= GPIOKPF_PRINT_MAPPED_KEYS
;
296 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_MAPPED_KEYS
;
302 static ssize_t
show_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
307 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_PHANTOM_KEYS
) ? 1 : 0);
310 static ssize_t
store_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
317 sscanf(buf
, "%u", &flag
);
320 gpio_evmi
->flags
|= GPIOKPF_PRINT_PHANTOM_KEYS
;
322 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_PHANTOM_KEYS
;
328 static ssize_t
show_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
333 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_ACTIVE_HIGH
) ? 1 : 0);
336 static ssize_t
store_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
343 sscanf(buf
, "%u", &flag
);
346 gpio_evmi
->flags
|= GPIOKPF_ACTIVE_HIGH
;
348 gpio_evmi
->flags
&= ~GPIOKPF_ACTIVE_HIGH
;
356 static ssize_t
show_level_triggered_irq_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
361 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_LEVEL_TRIGGERED_IRQ
) ? 1 : 0);
364 static ssize_t
store_level_triggered_irq_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
371 sscanf(buf
, "%u", &flag
);
374 gpio_evmi
->flags
|= GPIOKPF_LEVEL_TRIGGERED_IRQ
;
376 gpio_evmi
->flags
&= ~GPIOKPF_LEVEL_TRIGGERED_IRQ
;
384 static ssize_t
show_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
389 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DRIVE_INACTIVE
) ? 1 : 0);
392 static ssize_t
store_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
399 sscanf(buf
, "%u", &flag
);
402 gpio_evmi
->flags
|= GPIOKPF_DRIVE_INACTIVE
;
404 gpio_evmi
->flags
&= ~GPIOKPF_DRIVE_INACTIVE
;
410 static ssize_t
show_hw_debounce(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
412 return snprintf(buf
, PAGE_SIZE
, "%d\n", hw_debounce
);
415 static ssize_t
store_hw_debounce(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
419 sscanf(buf
, "%d", &enable
);
422 hw_debounce_set(1, -1);
426 hw_debounce_set(-1, 0);
427 hw_debounce_set(0, -1);
429 hw_debounce_time
= 0;
435 static ssize_t
show_hw_debounce_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
437 return snprintf(buf
, PAGE_SIZE
, "%d\n", hw_debounce_time
);
440 static ssize_t
store_hw_debounce_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
444 sscanf(buf
, "%d", &time
);
446 if ((time
< 0) || (time
> 0xff))
452 hw_debounce_set(-1, time
);
453 hw_debounce_time
= time
;
458 static DEVICE_ATTR(debounce_delay
, (S_IRUGO
| S_IWUGO
), show_debounce_delay
, store_debounce_delay
);
459 static DEVICE_ATTR(settle_time
, (S_IRUGO
| S_IWUGO
), show_settle_time
, store_settle_time
);
460 static DEVICE_ATTR(poll_time
, (S_IRUGO
| S_IWUGO
), show_poll_time
, store_poll_time
);
461 static DEVICE_ATTR(flags
, (S_IRUGO
), show_flags
, store_flags
);
462 static DEVICE_ATTR(debounce_flag
, (S_IRUGO
| S_IWUGO
), show_debounce_flag
, store_debounce_flag
);
463 static DEVICE_ATTR(remove_some_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_remove_some_phantom_keys_flag
, store_remove_some_phantom_keys_flag
);
464 static DEVICE_ATTR(print_unmapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_unmapped_keys_flag
, store_print_unmapped_keys_flag
);
465 static DEVICE_ATTR(print_mapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_mapped_keys_flag
, store_print_mapped_keys_flag
);
466 static DEVICE_ATTR(print_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_phantom_keys_flag
, store_print_phantom_keys_flag
);
467 static DEVICE_ATTR(active_high_flag
, (S_IRUGO
| S_IWUGO
), show_active_high_flag
, store_active_high_flag
);
468 static DEVICE_ATTR(level_triggered_irq_flag
, (S_IRUGO
| S_IWUGO
), show_level_triggered_irq_flag
, store_level_triggered_irq_flag
);
469 static DEVICE_ATTR(drive_inactive_flag
, (S_IRUGO
| S_IWUGO
), show_drive_inactive_flag
, store_drive_inactive_flag
);
470 static DEVICE_ATTR(hw_debounce
, (S_IRUGO
| S_IWUGO
), show_hw_debounce
, store_hw_debounce
);
471 static DEVICE_ATTR(hw_debounce_time
, (S_IRUGO
| S_IWUGO
), show_hw_debounce_time
, store_hw_debounce_time
);
474 static int debounce_fixup(int index
)
478 printk(KERN_INFO PREFIX
"0x%x\n", index
);
480 ret
= old_sw_fixup(index
);
482 printk(KERN_INFO PREFIX
"IGNORED!\n");
488 static void debounce_release(struct device
*dev
)
492 static struct device debounce_device
= {
493 .init_name
= "debounce",
494 .release
= debounce_release
,
497 static int __init
debounce_init(void)
499 struct device
*event_dev
= NULL
;
500 struct gpio_event_platform_data
*gpio_epd
;
501 struct gpio_event_info
*gpio_ei
;
504 printk(KERN_INFO PREFIX
"Searching for " GPIO_EVENT_DEV_NAME
"...\n");
506 event_dev
= device_find_child(&platform_bus
, GPIO_EVENT_DEV_NAME
, find_ms2_dev
);
507 if (event_dev
== NULL
)
510 gpio_epd
= (struct gpio_event_platform_data
*)event_dev
->platform_data
;
511 printk(KERN_INFO PREFIX
"And there is a %s connected...\n", gpio_epd
->name
);
512 if (strcmp(gpio_epd
->name
, "sholes-keypad"))
515 gpio_ei
= (struct gpio_event_info
*)gpio_epd
->info
[0];
516 gpio_evmi
= container_of(gpio_ei
, struct gpio_event_matrix_info
, info
);
518 err
= device_register(&debounce_device
);
523 err
= device_create_file(&debounce_device
, &dev_attr_debounce_delay
);
524 err
= device_create_file(&debounce_device
, &dev_attr_settle_time
);
525 err
= device_create_file(&debounce_device
, &dev_attr_poll_time
);
526 err
= device_create_file(&debounce_device
, &dev_attr_flags
);
527 err
= device_create_file(&debounce_device
, &dev_attr_debounce_flag
);
528 err
= device_create_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
529 err
= device_create_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
530 err
= device_create_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
531 err
= device_create_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
532 err
= device_create_file(&debounce_device
, &dev_attr_active_high_flag
);
533 err
= device_create_file(&debounce_device
, &dev_attr_level_triggered_irq_flag
);
534 err
= device_create_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
535 err
= device_create_file(&debounce_device
, &dev_attr_hw_debounce
);
536 err
= device_create_file(&debounce_device
, &dev_attr_hw_debounce_time
);
538 printk(KERN_INFO PREFIX
"settle_time: %u\n", gpio_evmi
->settle_time
.tv
.nsec
);
539 printk(KERN_INFO PREFIX
"poll_time: %u\n", gpio_evmi
->poll_time
.tv
.nsec
);
540 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
541 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
543 old_debounce_delay
= gpio_evmi
->debounce_delay
;
544 old_settle_time
= gpio_evmi
->settle_time
;
545 old_poll_time
= gpio_evmi
->poll_time
;
546 old_flags
= gpio_evmi
->flags
;
547 old_sw_fixup
= gpio_evmi
->sw_fixup
;
550 printk(KERN_INFO PREFIX
"Registering fixup handler\n");
551 gpio_evmi
->sw_fixup
= debounce_fixup
;
557 static void __exit
debounce_exit(void)
560 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= old_debounce_delay
.tv
.nsec
) {
561 printk(KERN_INFO PREFIX
"Restoring debounce_delay\n");
562 gpio_evmi
->debounce_delay
= old_debounce_delay
;
563 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
565 if (gpio_evmi
->flags
!= old_flags
) {
566 printk(KERN_INFO PREFIX
"Restoring flags\n");
567 gpio_evmi
->flags
= old_flags
;
568 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
571 gpio_evmi
->settle_time
= old_settle_time
;
572 gpio_evmi
->poll_time
= old_poll_time
;
574 if (gpio_evmi
->sw_fixup
!= old_sw_fixup
) {
575 printk(KERN_INFO PREFIX
"Restoring fixup handler\n");
576 gpio_evmi
->sw_fixup
= old_sw_fixup
;
579 hw_debounce_set(0, 0);
580 device_remove_file(&debounce_device
, &dev_attr_debounce_delay
);
581 device_remove_file(&debounce_device
, &dev_attr_settle_time
);
582 device_remove_file(&debounce_device
, &dev_attr_poll_time
);
583 device_remove_file(&debounce_device
, &dev_attr_flags
);
584 device_remove_file(&debounce_device
, &dev_attr_debounce_flag
);
585 device_remove_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
586 device_remove_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
587 device_remove_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
588 device_remove_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
589 device_remove_file(&debounce_device
, &dev_attr_active_high_flag
);
590 device_remove_file(&debounce_device
, &dev_attr_level_triggered_irq_flag
);
591 device_remove_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
592 device_remove_file(&debounce_device
, &dev_attr_hw_debounce
);
593 device_remove_file(&debounce_device
, &dev_attr_hw_debounce_time
);
594 device_unregister(&debounce_device
);
597 module_init(debounce_init
);
598 module_exit(debounce_exit
);
600 MODULE_LICENSE("GPL");
601 MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");