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