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>
8 #include <linux/earlysuspend.h>
9 #include <linux/wakelock.h>
11 #define PREFIX "debounce: "
13 static unsigned old_flags
= 0;
14 static ktime_t old_debounce_delay
;
15 static ktime_t old_settle_time
;
16 static ktime_t old_poll_time
;
17 static int (*old_sw_fixup
)(int index
);
18 static struct gpio_event_matrix_info
*gpio_evmi
= NULL
;
19 static int hw_debounce
= 0;
20 static int hw_debounce_time
= 0;
23 struct gpio_event_input_devs
*input_devs
;
24 const struct gpio_event_platform_data
*info
;
25 struct early_suspend early_suspend
;
30 struct gpio_event_input_devs
*input_devs
;
31 struct gpio_event_matrix_info
*keypad_info
;
33 struct wake_lock wake_lock
;
35 unsigned int use_irq
:1;
36 unsigned int key_state_changed
:1;
37 unsigned int last_key_state_changed
:1;
38 unsigned int some_keys_pressed
:2;
39 unsigned long keys_pressed
[0];
42 static struct gpio_kp
*gpio_kp_state
= NULL
;
44 static int find_ms2_dev(struct device
*dev
, void *data
)
46 if (!strncmp((char*)data
, dev_name(dev
), strlen((char*)data
))) {
47 printk(KERN_INFO PREFIX
"Found it\n");
53 /* hardware debounce: (time + 1) * 31us */
54 static void hw_debounce_set(int enable
, int time
) {
57 if (gpio_evmi
== NULL
)
60 for (i
= 0; i
< gpio_evmi
->ninputs
; i
++) {
61 int gpio
= gpio_evmi
->input_gpios
[i
];
63 if ((time
!= -1) && (time
!= hw_debounce_time
) && hw_debounce
) {
64 printk(KERN_INFO PREFIX
"Setting hardware debounce time for GPIO %d to %d (%dus)\n", gpio
, time
, (time
+1)*31);
65 omap_set_gpio_debounce_time(gpio
, time
);
68 if ((enable
!= -1) && (enable
!= hw_debounce
)) {
69 printk(KERN_INFO PREFIX
"%sabling hardware debounce for GPIO %d\n", (enable
?"En":"Dis"), gpio
);
70 omap_set_gpio_debounce(gpio
, enable
);
75 static void set_irq_types(void) {
81 if (gpio_evmi
== NULL
)
84 switch (gpio_evmi
->flags
& (GPIOKPF_ACTIVE_HIGH
|GPIOKPF_LEVEL_TRIGGERED_IRQ
)) {
86 type
= IRQ_TYPE_EDGE_FALLING
;
88 case GPIOKPF_ACTIVE_HIGH
:
89 type
= IRQ_TYPE_EDGE_RISING
;
91 case GPIOKPF_LEVEL_TRIGGERED_IRQ
:
92 type
= IRQ_TYPE_LEVEL_LOW
;
94 case GPIOKPF_LEVEL_TRIGGERED_IRQ
| GPIOKPF_ACTIVE_HIGH
:
95 type
= IRQ_TYPE_LEVEL_HIGH
;
99 printk(KERN_INFO PREFIX
"Settinhg IRQ type to 0x%lx\n", type
);
101 for (i
= 0; i
< gpio_evmi
->ninputs
; i
++) {
103 err
= irq
= gpio_to_irq(gpio_evmi
->input_gpios
[i
]);
108 err
= set_irq_type(irq
, type
);
112 static ssize_t
show_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
117 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->debounce_delay
.tv
.nsec
/ NSEC_PER_MSEC
));
120 static void set_debounce_delay(long delay
)
122 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= delay
* NSEC_PER_MSEC
) {
123 printk(KERN_INFO PREFIX
"Changing debounce_delay\n");
124 gpio_evmi
->debounce_delay
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
125 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
129 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= 0) {
130 if (!(gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
)) {
131 printk(KERN_INFO PREFIX
"Activating debounce\n");
132 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
135 if (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) {
136 printk(KERN_INFO PREFIX
"Deactivating debounce\n");
137 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
143 static ssize_t
store_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
150 sscanf(buf
, "%ld", &delay
);
151 set_debounce_delay(delay
);
156 static ssize_t
show_settle_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
161 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->settle_time
.tv
.nsec
/ NSEC_PER_USEC
));
164 static ssize_t
store_settle_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
171 sscanf(buf
, "%ld", &delay
);
172 gpio_evmi
->settle_time
.tv
.nsec
= delay
* NSEC_PER_USEC
;
177 static ssize_t
show_poll_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
182 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->poll_time
.tv
.nsec
/ NSEC_PER_MSEC
));
185 static ssize_t
store_poll_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
192 sscanf(buf
, "%ld", &delay
);
193 gpio_evmi
->poll_time
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
198 static ssize_t
show_flags(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
203 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", gpio_evmi
->flags
);
206 static ssize_t
store_flags(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
213 sscanf(buf
, "0x%x", &flags
);
215 printk(KERN_INFO PREFIX
"flags: 0x%x\n", flags
);
217 gpio_evmi
->flags
= flags
;
222 static ssize_t
show_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
227 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) ? 1 : 0);
230 static ssize_t
store_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
237 sscanf(buf
, "%u", &flag
);
240 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
242 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
248 static ssize_t
show_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
253 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
) ? 1 : 0);
256 static ssize_t
store_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
263 sscanf(buf
, "%u", &flag
);
266 gpio_evmi
->flags
|= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
268 gpio_evmi
->flags
&= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
274 static ssize_t
show_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
279 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_UNMAPPED_KEYS
) ? 1 : 0);
282 static ssize_t
store_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
289 sscanf(buf
, "%u", &flag
);
292 gpio_evmi
->flags
|= GPIOKPF_PRINT_UNMAPPED_KEYS
;
294 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_UNMAPPED_KEYS
;
300 static ssize_t
show_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
305 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_MAPPED_KEYS
) ? 1 : 0);
308 static ssize_t
store_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
315 sscanf(buf
, "%u", &flag
);
318 gpio_evmi
->flags
|= GPIOKPF_PRINT_MAPPED_KEYS
;
320 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_MAPPED_KEYS
;
326 static ssize_t
show_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
331 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_PHANTOM_KEYS
) ? 1 : 0);
334 static ssize_t
store_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
341 sscanf(buf
, "%u", &flag
);
344 gpio_evmi
->flags
|= GPIOKPF_PRINT_PHANTOM_KEYS
;
346 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_PHANTOM_KEYS
;
352 static ssize_t
show_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
357 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_ACTIVE_HIGH
) ? 1 : 0);
360 static ssize_t
store_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
367 sscanf(buf
, "%u", &flag
);
370 gpio_evmi
->flags
|= GPIOKPF_ACTIVE_HIGH
;
372 gpio_evmi
->flags
&= ~GPIOKPF_ACTIVE_HIGH
;
380 static ssize_t
show_level_triggered_irq_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
385 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_LEVEL_TRIGGERED_IRQ
) ? 1 : 0);
388 static ssize_t
store_level_triggered_irq_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
395 sscanf(buf
, "%u", &flag
);
398 gpio_evmi
->flags
|= GPIOKPF_LEVEL_TRIGGERED_IRQ
;
400 gpio_evmi
->flags
&= ~GPIOKPF_LEVEL_TRIGGERED_IRQ
;
408 static ssize_t
show_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
413 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DRIVE_INACTIVE
) ? 1 : 0);
416 static ssize_t
store_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
423 sscanf(buf
, "%u", &flag
);
426 gpio_evmi
->flags
|= GPIOKPF_DRIVE_INACTIVE
;
428 gpio_evmi
->flags
&= ~GPIOKPF_DRIVE_INACTIVE
;
434 static ssize_t
show_hw_debounce(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
436 return snprintf(buf
, PAGE_SIZE
, "%d\n", hw_debounce
);
439 static ssize_t
store_hw_debounce(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
443 sscanf(buf
, "%d", &enable
);
446 hw_debounce_set(1, -1);
450 hw_debounce_set(-1, 0);
451 hw_debounce_set(0, -1);
453 hw_debounce_time
= 0;
459 static ssize_t
show_hw_debounce_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
461 return snprintf(buf
, PAGE_SIZE
, "%d\n", hw_debounce_time
);
464 static ssize_t
store_hw_debounce_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
468 sscanf(buf
, "%d", &time
);
470 if ((time
< 0) || (time
> 0xff))
476 hw_debounce_set(-1, time
);
477 hw_debounce_time
= time
;
482 static DEVICE_ATTR(debounce_delay
, (S_IRUGO
| S_IWUGO
), show_debounce_delay
, store_debounce_delay
);
483 static DEVICE_ATTR(settle_time
, (S_IRUGO
| S_IWUGO
), show_settle_time
, store_settle_time
);
484 static DEVICE_ATTR(poll_time
, (S_IRUGO
| S_IWUGO
), show_poll_time
, store_poll_time
);
485 static DEVICE_ATTR(flags
, (S_IRUGO
), show_flags
, store_flags
);
486 static DEVICE_ATTR(debounce_flag
, (S_IRUGO
| S_IWUGO
), show_debounce_flag
, store_debounce_flag
);
487 static DEVICE_ATTR(remove_some_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_remove_some_phantom_keys_flag
, store_remove_some_phantom_keys_flag
);
488 static DEVICE_ATTR(print_unmapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_unmapped_keys_flag
, store_print_unmapped_keys_flag
);
489 static DEVICE_ATTR(print_mapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_mapped_keys_flag
, store_print_mapped_keys_flag
);
490 static DEVICE_ATTR(print_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_phantom_keys_flag
, store_print_phantom_keys_flag
);
491 static DEVICE_ATTR(active_high_flag
, (S_IRUGO
| S_IWUGO
), show_active_high_flag
, store_active_high_flag
);
492 static DEVICE_ATTR(level_triggered_irq_flag
, (S_IRUGO
| S_IWUGO
), show_level_triggered_irq_flag
, store_level_triggered_irq_flag
);
493 static DEVICE_ATTR(drive_inactive_flag
, (S_IRUGO
| S_IWUGO
), show_drive_inactive_flag
, store_drive_inactive_flag
);
494 static DEVICE_ATTR(hw_debounce
, (S_IRUGO
| S_IWUGO
), show_hw_debounce
, store_hw_debounce
);
495 static DEVICE_ATTR(hw_debounce_time
, (S_IRUGO
| S_IWUGO
), show_hw_debounce_time
, store_hw_debounce_time
);
498 static int debounce_fixup(int index
)
502 printk(KERN_INFO PREFIX
"key_state_changed: %d, last_key_state_changed: %d, some_keys_pressed: %d\n",
503 gpio_kp_state
->key_state_changed
,
504 gpio_kp_state
->last_key_state_changed
,
505 gpio_kp_state
->some_keys_pressed
);
507 ret
= old_sw_fixup(index
);
509 printk(KERN_INFO PREFIX
"Index 0x%x ignored!\n", index
);
515 static void debounce_release(struct device
*dev
)
519 static struct device debounce_device
= {
520 .init_name
= "debounce",
521 .release
= debounce_release
,
524 static int __init
debounce_init(void)
526 struct device
*event_dev
= NULL
;
527 struct platform_device
*pdev
= NULL
;
528 struct gpio_event_platform_data
*gpio_epd
;
529 struct gpio_event_info
*gpio_ei
;
530 struct gpio_event
*gpio_e
;
534 printk(KERN_INFO PREFIX
"Searching for " GPIO_EVENT_DEV_NAME
"...\n");
536 event_dev
= device_find_child(&platform_bus
, GPIO_EVENT_DEV_NAME
, find_ms2_dev
);
537 if (event_dev
== NULL
)
540 pdev
= container_of(event_dev
, struct platform_device
, dev
);
544 gpio_epd
= (struct gpio_event_platform_data
*)event_dev
->platform_data
;
545 printk(KERN_INFO PREFIX
"And there is a %s connected...\n", gpio_epd
->name
);
546 if (strcmp(gpio_epd
->name
, "sholes-keypad"))
549 gpio_ei
= (struct gpio_event_info
*)gpio_epd
->info
[0];
550 gpio_evmi
= container_of(gpio_ei
, struct gpio_event_matrix_info
, info
);
552 gpio_e
= platform_get_drvdata(pdev
);
553 printk(KERN_INFO PREFIX
"Number of states: %d\n", gpio_e
->info
->info_count
);
555 /* Search for correct gpio_event state */
556 for (i
= 0; i
< gpio_e
->info
->info_count
; i
++) {
557 if (gpio_e
->info
->info
[i
]->func
== gpio_ei
->func
) {
558 printk(KERN_INFO PREFIX
"Keypad state: %d\n", i
);
559 gpio_kp_state
= gpio_e
->state
[i
];
563 if (!gpio_kp_state
) {
564 printk(KERN_ERR PREFIX
"Can't determine correct keypad state!\n");
568 printk(KERN_INFO PREFIX
"kp_use_irq: %d\n", gpio_kp_state
->use_irq
);
570 gpio_kp_state
->use_irq
=0;
571 hrtimer_start(&(gpio_kp_state
->timer
), gpio_evmi
->poll_time
, HRTIMER_MODE_REL
);
572 printk(KERN_INFO PREFIX
"kp_use_irq: %d\n", gpio_kp_state
->use_irq
);
575 err
= device_register(&debounce_device
);
580 err
= device_create_file(&debounce_device
, &dev_attr_debounce_delay
);
581 err
= device_create_file(&debounce_device
, &dev_attr_settle_time
);
582 err
= device_create_file(&debounce_device
, &dev_attr_poll_time
);
583 err
= device_create_file(&debounce_device
, &dev_attr_flags
);
584 err
= device_create_file(&debounce_device
, &dev_attr_debounce_flag
);
585 err
= device_create_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
586 err
= device_create_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
587 err
= device_create_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
588 err
= device_create_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
589 err
= device_create_file(&debounce_device
, &dev_attr_active_high_flag
);
590 err
= device_create_file(&debounce_device
, &dev_attr_level_triggered_irq_flag
);
591 err
= device_create_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
592 err
= device_create_file(&debounce_device
, &dev_attr_hw_debounce
);
593 err
= device_create_file(&debounce_device
, &dev_attr_hw_debounce_time
);
595 printk(KERN_INFO PREFIX
"settle_time: %u\n", gpio_evmi
->settle_time
.tv
.nsec
);
596 printk(KERN_INFO PREFIX
"poll_time: %u\n", gpio_evmi
->poll_time
.tv
.nsec
);
597 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
598 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
600 old_debounce_delay
= gpio_evmi
->debounce_delay
;
601 old_settle_time
= gpio_evmi
->settle_time
;
602 old_poll_time
= gpio_evmi
->poll_time
;
603 old_flags
= gpio_evmi
->flags
;
604 old_sw_fixup
= gpio_evmi
->sw_fixup
;
607 printk(KERN_INFO PREFIX
"Registering fixup handler\n");
608 gpio_evmi
->sw_fixup
= debounce_fixup
;
614 static void __exit
debounce_exit(void)
617 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= old_debounce_delay
.tv
.nsec
) {
618 printk(KERN_INFO PREFIX
"Restoring debounce_delay\n");
619 gpio_evmi
->debounce_delay
= old_debounce_delay
;
620 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
622 if (gpio_evmi
->flags
!= old_flags
) {
623 printk(KERN_INFO PREFIX
"Restoring flags\n");
624 gpio_evmi
->flags
= old_flags
;
625 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
628 gpio_evmi
->settle_time
= old_settle_time
;
629 gpio_evmi
->poll_time
= old_poll_time
;
631 if (gpio_evmi
->sw_fixup
!= old_sw_fixup
) {
632 printk(KERN_INFO PREFIX
"Restoring fixup handler\n");
633 gpio_evmi
->sw_fixup
= old_sw_fixup
;
636 hw_debounce_set(0, 0);
637 device_remove_file(&debounce_device
, &dev_attr_debounce_delay
);
638 device_remove_file(&debounce_device
, &dev_attr_settle_time
);
639 device_remove_file(&debounce_device
, &dev_attr_poll_time
);
640 device_remove_file(&debounce_device
, &dev_attr_flags
);
641 device_remove_file(&debounce_device
, &dev_attr_debounce_flag
);
642 device_remove_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
643 device_remove_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
644 device_remove_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
645 device_remove_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
646 device_remove_file(&debounce_device
, &dev_attr_active_high_flag
);
647 device_remove_file(&debounce_device
, &dev_attr_level_triggered_irq_flag
);
648 device_remove_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
649 device_remove_file(&debounce_device
, &dev_attr_hw_debounce
);
650 device_remove_file(&debounce_device
, &dev_attr_hw_debounce_time
);
651 device_unregister(&debounce_device
);
654 module_init(debounce_init
);
655 module_exit(debounce_exit
);
657 MODULE_LICENSE("GPL");
658 MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");