deactivate debounce when delay is 0
[ms2-fixes] / debounce.c
CommitLineData
b422e333 1#include <linux/module.h>
d1ff9643
MG
2#include <linux/device.h>
3#include <linux/platform_device.h>
4#include <linux/gpio_event.h>
5
ab5ed215
MG
6#define PREFIX "debounce: "
7
a4838b14 8static int debounce_delay = 15;
ab5ed215 9
1e65c113 10static int old_delay = 0;
a4838b14 11static unsigned old_flags = 0;
1e65c113
MG
12static struct gpio_event_matrix_info *gpio_evmi = NULL;
13
b2b792eb 14module_param(debounce_delay, int, S_IRUSR | S_IRGRP | S_IROTH);
a4838b14 15MODULE_PARM_DESC(debounce_delay, "debouncing delay (ms), default: 15");
ab5ed215
MG
16
17static int find_ms2_dev(struct device *dev, void *data)
d1ff9643
MG
18{
19 if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) {
ab5ed215 20 printk(KERN_INFO PREFIX "Found it\n");
d1ff9643
MG
21 return 1;
22 }
23 return 0;
24}
b422e333
MG
25
26static int __init debounce_init(void)
27{
d1ff9643
MG
28 struct device *event_dev = NULL;
29 struct gpio_event_platform_data *gpio_epd;
30 struct gpio_event_info *gpio_ei;
d1ff9643 31
ab5ed215 32 printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
d1ff9643
MG
33
34
35 event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
36 if (event_dev == NULL)
37 return -ENODEV;
38
39 gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data;
ab5ed215 40 printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name);
d1ff9643
MG
41 if (strcmp(gpio_epd->name, "sholes-keypad"))
42 return -ENODEV;
43
44 gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
45 gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
46
ab5ed215
MG
47 printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
48 printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
49 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
50 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
20bf1c9e 51
1e65c113
MG
52 old_delay = gpio_evmi->debounce_delay.tv.nsec;
53
b2b792eb 54 if (gpio_evmi->debounce_delay.tv.nsec != debounce_delay * NSEC_PER_MSEC) {
ab5ed215 55 printk(KERN_INFO PREFIX "Changing debounce_delay\n");
b2b792eb 56 gpio_evmi->debounce_delay.tv.nsec = debounce_delay * NSEC_PER_MSEC;
ab5ed215 57 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
d1ff9643
MG
58 }
59
a4838b14
MG
60 old_flags = gpio_evmi->flags;
61
62 if (gpio_evmi->debounce_delay.tv.nsec != 0) {
63 /* GPIOKPF_DEBOUNCE should already be set by GPIOKPF_REMOVE_PHANTOM_KEYS */
64 if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
65 printk(KERN_INFO PREFIX "Activating debounce\n");
66 gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
67 }
68 } else {
69 /* Deactivating GPIOKPF_DEBOUNCE */
70 printk(KERN_INFO PREFIX "Deactivating debounce\n");
71 gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
72 }
73 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
74
b422e333
MG
75 return 0;
76}
77
78static void __exit debounce_exit(void)
79{
1e65c113
MG
80 if (gpio_evmi) {
81 if (gpio_evmi->debounce_delay.tv.nsec != old_delay) {
82 printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
83 gpio_evmi->debounce_delay.tv.nsec = old_delay;
84 printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
85 }
a4838b14
MG
86 if (gpio_evmi->flags != old_flags) {
87 printk(KERN_INFO PREFIX "Restoring flags\n");
88 gpio_evmi->flags = old_flags;
89 printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
90 }
1e65c113 91 }
b422e333
MG
92}
93
94module_init(debounce_init);
95module_exit(debounce_exit);
96
97MODULE_LICENSE("GPL");
98MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");
Impressum, Datenschutz