1 #include <linux/module.h>
2 #include <linux/device.h>
3 #include <linux/platform_device.h>
4 #include <linux/gpio_event.h>
6 #define PREFIX "debounce: "
8 static int debounce_delay
= 15;
10 static unsigned old_flags
= 0;
11 ktime_t old_debounce_delay
;
12 ktime_t old_settle_time
;
13 ktime_t old_poll_time
;
14 static struct gpio_event_matrix_info
*gpio_evmi
= NULL
;
16 module_param(debounce_delay
, int, S_IRUSR
| S_IRGRP
| S_IROTH
);
17 MODULE_PARM_DESC(debounce_delay
, "debouncing delay (ms), default: 15");
19 static int find_ms2_dev(struct device
*dev
, void *data
)
21 if (!strncmp((char*)data
, dev_name(dev
), strlen((char*)data
))) {
22 printk(KERN_INFO PREFIX
"Found it\n");
28 static ssize_t
show_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
33 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->debounce_delay
.tv
.nsec
/ NSEC_PER_MSEC
));
36 static void set_debounce_delay(long delay
)
38 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= delay
* NSEC_PER_MSEC
) {
39 printk(KERN_INFO PREFIX
"Changing debounce_delay\n");
40 gpio_evmi
->debounce_delay
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
41 debounce_delay
= delay
;
42 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
46 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= 0) {
47 if (!(gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
)) {
48 printk(KERN_INFO PREFIX
"Activating debounce\n");
49 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
52 if (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) {
53 printk(KERN_INFO PREFIX
"Deactivating debounce\n");
54 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
60 static ssize_t
store_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
67 sscanf(buf
, "%ld", &delay
);
68 set_debounce_delay(delay
);
73 static ssize_t
show_settle_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
78 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->settle_time
.tv
.nsec
/ NSEC_PER_USEC
));
81 static ssize_t
store_settle_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
88 sscanf(buf
, "%ld", &delay
);
89 gpio_evmi
->settle_time
.tv
.nsec
= delay
* NSEC_PER_USEC
;
94 static ssize_t
show_poll_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
99 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->poll_time
.tv
.nsec
/ NSEC_PER_MSEC
));
102 static ssize_t
store_poll_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
109 sscanf(buf
, "%ld", &delay
);
110 gpio_evmi
->poll_time
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
115 static ssize_t
show_flags(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
120 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", gpio_evmi
->flags
);
123 static ssize_t
store_flags(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
130 sscanf(buf
, "0x%x", &flags
);
132 printk(KERN_INFO PREFIX
"flags: 0x%x\n", flags
);
134 if (flags
& GPIOKPF_DRIVE_INACTIVE
)
137 gpio_evmi
->flags
= flags
;
142 static ssize_t
show_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
147 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) ? 1 : 0);
150 static ssize_t
store_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
157 sscanf(buf
, "%u", &flag
);
160 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
162 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
168 static ssize_t
show_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
173 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
) ? 1 : 0);
176 static ssize_t
store_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
183 sscanf(buf
, "%u", &flag
);
186 gpio_evmi
->flags
|= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
188 gpio_evmi
->flags
&= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
194 static ssize_t
show_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
199 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_UNMAPPED_KEYS
) ? 1 : 0);
202 static ssize_t
store_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
209 sscanf(buf
, "%u", &flag
);
212 gpio_evmi
->flags
|= GPIOKPF_PRINT_UNMAPPED_KEYS
;
214 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_UNMAPPED_KEYS
;
220 static ssize_t
show_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
225 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_MAPPED_KEYS
) ? 1 : 0);
228 static ssize_t
store_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
235 sscanf(buf
, "%u", &flag
);
238 gpio_evmi
->flags
|= GPIOKPF_PRINT_MAPPED_KEYS
;
240 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_MAPPED_KEYS
;
246 static ssize_t
show_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
251 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_PHANTOM_KEYS
) ? 1 : 0);
254 static ssize_t
store_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
261 sscanf(buf
, "%u", &flag
);
264 gpio_evmi
->flags
|= GPIOKPF_PRINT_PHANTOM_KEYS
;
266 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_PHANTOM_KEYS
;
272 static DEVICE_ATTR(debounce_delay
, (S_IRUGO
| S_IWUGO
), show_debounce_delay
, store_debounce_delay
);
273 static DEVICE_ATTR(settle_time
, (S_IRUGO
| S_IWUGO
), show_settle_time
, store_settle_time
);
274 static DEVICE_ATTR(poll_time
, (S_IRUGO
| S_IWUGO
), show_poll_time
, store_poll_time
);
275 static DEVICE_ATTR(flags
, (S_IRUGO
), show_flags
, store_flags
);
276 static DEVICE_ATTR(debounce_flag
, (S_IRUGO
| S_IWUGO
), show_debounce_flag
, store_debounce_flag
);
277 static DEVICE_ATTR(remove_some_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_remove_some_phantom_keys_flag
, store_remove_some_phantom_keys_flag
);
278 static DEVICE_ATTR(print_unmapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_unmapped_keys_flag
, store_print_unmapped_keys_flag
);
279 static DEVICE_ATTR(print_mapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_mapped_keys_flag
, store_print_mapped_keys_flag
);
280 static DEVICE_ATTR(print_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_phantom_keys_flag
, store_print_phantom_keys_flag
);
282 static void debounce_release(struct device
*dev
)
286 static struct device debounce_device
= {
287 .init_name
= "debounce",
288 .release
= debounce_release
,
291 static int __init
debounce_init(void)
293 struct device
*event_dev
= NULL
;
294 struct gpio_event_platform_data
*gpio_epd
;
295 struct gpio_event_info
*gpio_ei
;
298 printk(KERN_INFO PREFIX
"Searching for " GPIO_EVENT_DEV_NAME
"...\n");
300 event_dev
= device_find_child(&platform_bus
, GPIO_EVENT_DEV_NAME
, find_ms2_dev
);
301 if (event_dev
== NULL
)
304 gpio_epd
= (struct gpio_event_platform_data
*)event_dev
->platform_data
;
305 printk(KERN_INFO PREFIX
"And there is a %s connected...\n", gpio_epd
->name
);
306 if (strcmp(gpio_epd
->name
, "sholes-keypad"))
309 gpio_ei
= (struct gpio_event_info
*)gpio_epd
->info
[0];
310 gpio_evmi
= container_of(gpio_ei
, struct gpio_event_matrix_info
, info
);
312 err
= device_register(&debounce_device
);
317 err
= device_create_file(&debounce_device
, &dev_attr_debounce_delay
);
318 err
= device_create_file(&debounce_device
, &dev_attr_settle_time
);
319 err
= device_create_file(&debounce_device
, &dev_attr_poll_time
);
320 err
= device_create_file(&debounce_device
, &dev_attr_flags
);
321 err
= device_create_file(&debounce_device
, &dev_attr_debounce_flag
);
322 err
= device_create_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
323 err
= device_create_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
324 err
= device_create_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
325 err
= device_create_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
327 printk(KERN_INFO PREFIX
"settle_time: %u\n", gpio_evmi
->settle_time
.tv
.nsec
);
328 printk(KERN_INFO PREFIX
"poll_time: %u\n", gpio_evmi
->poll_time
.tv
.nsec
);
329 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
330 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
332 old_debounce_delay
= gpio_evmi
->debounce_delay
;
333 old_settle_time
= gpio_evmi
->settle_time
;
334 old_poll_time
= gpio_evmi
->poll_time
;
335 old_flags
= gpio_evmi
->flags
;
337 set_debounce_delay(debounce_delay
);
339 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
344 static void __exit
debounce_exit(void)
347 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= old_debounce_delay
.tv
.nsec
) {
348 printk(KERN_INFO PREFIX
"Restoring debounce_delay\n");
349 gpio_evmi
->debounce_delay
= old_debounce_delay
;
350 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
352 if (gpio_evmi
->flags
!= old_flags
) {
353 printk(KERN_INFO PREFIX
"Restoring flags\n");
354 gpio_evmi
->flags
= old_flags
;
355 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
357 gpio_evmi
->settle_time
= old_settle_time
;
358 gpio_evmi
->poll_time
= old_poll_time
;
360 device_remove_file(&debounce_device
, &dev_attr_debounce_delay
);
361 device_remove_file(&debounce_device
, &dev_attr_settle_time
);
362 device_remove_file(&debounce_device
, &dev_attr_poll_time
);
363 device_remove_file(&debounce_device
, &dev_attr_flags
);
364 device_remove_file(&debounce_device
, &dev_attr_debounce_flag
);
365 device_remove_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
366 device_remove_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
367 device_remove_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
368 device_remove_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
369 device_unregister(&debounce_device
);
372 module_init(debounce_init
);
373 module_exit(debounce_exit
);
375 MODULE_LICENSE("GPL");
376 MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");