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 unsigned old_flags
= 0;
9 ktime_t old_debounce_delay
;
10 ktime_t old_settle_time
;
11 ktime_t old_poll_time
;
12 static struct gpio_event_matrix_info
*gpio_evmi
= NULL
;
14 static int find_ms2_dev(struct device
*dev
, void *data
)
16 if (!strncmp((char*)data
, dev_name(dev
), strlen((char*)data
))) {
17 printk(KERN_INFO PREFIX
"Found it\n");
23 static ssize_t
show_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
28 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->debounce_delay
.tv
.nsec
/ NSEC_PER_MSEC
));
31 static void set_debounce_delay(long delay
)
33 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= delay
* NSEC_PER_MSEC
) {
34 printk(KERN_INFO PREFIX
"Changing debounce_delay\n");
35 gpio_evmi
->debounce_delay
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
36 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
40 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= 0) {
41 if (!(gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
)) {
42 printk(KERN_INFO PREFIX
"Activating debounce\n");
43 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
46 if (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) {
47 printk(KERN_INFO PREFIX
"Deactivating debounce\n");
48 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
54 static ssize_t
store_debounce_delay(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
61 sscanf(buf
, "%ld", &delay
);
62 set_debounce_delay(delay
);
67 static ssize_t
show_settle_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
72 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->settle_time
.tv
.nsec
/ NSEC_PER_USEC
));
75 static ssize_t
store_settle_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
82 sscanf(buf
, "%ld", &delay
);
83 gpio_evmi
->settle_time
.tv
.nsec
= delay
* NSEC_PER_USEC
;
88 static ssize_t
show_poll_time(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
93 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (gpio_evmi
->poll_time
.tv
.nsec
/ NSEC_PER_MSEC
));
96 static ssize_t
store_poll_time(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
103 sscanf(buf
, "%ld", &delay
);
104 gpio_evmi
->poll_time
.tv
.nsec
= delay
* NSEC_PER_MSEC
;
109 static ssize_t
show_flags(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
114 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", gpio_evmi
->flags
);
117 static ssize_t
store_flags(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
124 sscanf(buf
, "0x%x", &flags
);
126 printk(KERN_INFO PREFIX
"flags: 0x%x\n", flags
);
128 gpio_evmi
->flags
= flags
;
133 static ssize_t
show_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
138 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DEBOUNCE
) ? 1 : 0);
141 static ssize_t
store_debounce_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
148 sscanf(buf
, "%u", &flag
);
151 gpio_evmi
->flags
|= GPIOKPF_DEBOUNCE
;
153 gpio_evmi
->flags
&= ~GPIOKPF_DEBOUNCE
;
159 static ssize_t
show_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
164 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
) ? 1 : 0);
167 static ssize_t
store_remove_some_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
174 sscanf(buf
, "%u", &flag
);
177 gpio_evmi
->flags
|= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
179 gpio_evmi
->flags
&= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS
;
185 static ssize_t
show_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
190 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_UNMAPPED_KEYS
) ? 1 : 0);
193 static ssize_t
store_print_unmapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
200 sscanf(buf
, "%u", &flag
);
203 gpio_evmi
->flags
|= GPIOKPF_PRINT_UNMAPPED_KEYS
;
205 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_UNMAPPED_KEYS
;
211 static ssize_t
show_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
216 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_MAPPED_KEYS
) ? 1 : 0);
219 static ssize_t
store_print_mapped_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
226 sscanf(buf
, "%u", &flag
);
229 gpio_evmi
->flags
|= GPIOKPF_PRINT_MAPPED_KEYS
;
231 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_MAPPED_KEYS
;
237 static ssize_t
show_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
242 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_PRINT_PHANTOM_KEYS
) ? 1 : 0);
245 static ssize_t
store_print_phantom_keys_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
252 sscanf(buf
, "%u", &flag
);
255 gpio_evmi
->flags
|= GPIOKPF_PRINT_PHANTOM_KEYS
;
257 gpio_evmi
->flags
&= ~GPIOKPF_PRINT_PHANTOM_KEYS
;
263 static ssize_t
show_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
268 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_ACTIVE_HIGH
) ? 1 : 0);
271 static ssize_t
store_active_high_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
278 sscanf(buf
, "%u", &flag
);
281 gpio_evmi
->flags
|= GPIOKPF_ACTIVE_HIGH
;
283 gpio_evmi
->flags
&= ~GPIOKPF_ACTIVE_HIGH
;
289 static ssize_t
show_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
294 return snprintf(buf
, PAGE_SIZE
, "%u\n", (gpio_evmi
->flags
& GPIOKPF_DRIVE_INACTIVE
) ? 1 : 0);
297 static ssize_t
store_drive_inactive_flag(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
304 sscanf(buf
, "%u", &flag
);
307 gpio_evmi
->flags
|= GPIOKPF_DRIVE_INACTIVE
;
309 gpio_evmi
->flags
&= ~GPIOKPF_DRIVE_INACTIVE
;
315 static DEVICE_ATTR(debounce_delay
, (S_IRUGO
| S_IWUGO
), show_debounce_delay
, store_debounce_delay
);
316 static DEVICE_ATTR(settle_time
, (S_IRUGO
| S_IWUGO
), show_settle_time
, store_settle_time
);
317 static DEVICE_ATTR(poll_time
, (S_IRUGO
| S_IWUGO
), show_poll_time
, store_poll_time
);
318 static DEVICE_ATTR(flags
, (S_IRUGO
), show_flags
, store_flags
);
319 static DEVICE_ATTR(debounce_flag
, (S_IRUGO
| S_IWUGO
), show_debounce_flag
, store_debounce_flag
);
320 static DEVICE_ATTR(remove_some_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_remove_some_phantom_keys_flag
, store_remove_some_phantom_keys_flag
);
321 static DEVICE_ATTR(print_unmapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_unmapped_keys_flag
, store_print_unmapped_keys_flag
);
322 static DEVICE_ATTR(print_mapped_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_mapped_keys_flag
, store_print_mapped_keys_flag
);
323 static DEVICE_ATTR(print_phantom_keys_flag
, (S_IRUGO
| S_IWUGO
), show_print_phantom_keys_flag
, store_print_phantom_keys_flag
);
324 static DEVICE_ATTR(active_high_flag
, (S_IRUGO
), show_active_high_flag
, store_active_high_flag
);
325 static DEVICE_ATTR(drive_inactive_flag
, (S_IRUGO
| S_IWUGO
), show_drive_inactive_flag
, store_drive_inactive_flag
);
327 static void debounce_release(struct device
*dev
)
331 static struct device debounce_device
= {
332 .init_name
= "debounce",
333 .release
= debounce_release
,
336 static int __init
debounce_init(void)
338 struct device
*event_dev
= NULL
;
339 struct gpio_event_platform_data
*gpio_epd
;
340 struct gpio_event_info
*gpio_ei
;
343 printk(KERN_INFO PREFIX
"Searching for " GPIO_EVENT_DEV_NAME
"...\n");
345 event_dev
= device_find_child(&platform_bus
, GPIO_EVENT_DEV_NAME
, find_ms2_dev
);
346 if (event_dev
== NULL
)
349 gpio_epd
= (struct gpio_event_platform_data
*)event_dev
->platform_data
;
350 printk(KERN_INFO PREFIX
"And there is a %s connected...\n", gpio_epd
->name
);
351 if (strcmp(gpio_epd
->name
, "sholes-keypad"))
354 gpio_ei
= (struct gpio_event_info
*)gpio_epd
->info
[0];
355 gpio_evmi
= container_of(gpio_ei
, struct gpio_event_matrix_info
, info
);
357 err
= device_register(&debounce_device
);
362 err
= device_create_file(&debounce_device
, &dev_attr_debounce_delay
);
363 err
= device_create_file(&debounce_device
, &dev_attr_settle_time
);
364 err
= device_create_file(&debounce_device
, &dev_attr_poll_time
);
365 err
= device_create_file(&debounce_device
, &dev_attr_flags
);
366 err
= device_create_file(&debounce_device
, &dev_attr_debounce_flag
);
367 err
= device_create_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
368 err
= device_create_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
369 err
= device_create_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
370 err
= device_create_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
371 err
= device_create_file(&debounce_device
, &dev_attr_active_high_flag
);
372 err
= device_create_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
374 printk(KERN_INFO PREFIX
"settle_time: %u\n", gpio_evmi
->settle_time
.tv
.nsec
);
375 printk(KERN_INFO PREFIX
"poll_time: %u\n", gpio_evmi
->poll_time
.tv
.nsec
);
376 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
377 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
379 old_debounce_delay
= gpio_evmi
->debounce_delay
;
380 old_settle_time
= gpio_evmi
->settle_time
;
381 old_poll_time
= gpio_evmi
->poll_time
;
382 old_flags
= gpio_evmi
->flags
;
384 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
389 static void __exit
debounce_exit(void)
392 if (gpio_evmi
->debounce_delay
.tv
.nsec
!= old_debounce_delay
.tv
.nsec
) {
393 printk(KERN_INFO PREFIX
"Restoring debounce_delay\n");
394 gpio_evmi
->debounce_delay
= old_debounce_delay
;
395 printk(KERN_INFO PREFIX
"debounce_delay: %u\n", gpio_evmi
->debounce_delay
.tv
.nsec
);
397 if (gpio_evmi
->flags
!= old_flags
) {
398 printk(KERN_INFO PREFIX
"Restoring flags\n");
399 gpio_evmi
->flags
= old_flags
;
400 printk(KERN_INFO PREFIX
"flags: 0x%x\n", gpio_evmi
->flags
);
402 gpio_evmi
->settle_time
= old_settle_time
;
403 gpio_evmi
->poll_time
= old_poll_time
;
405 device_remove_file(&debounce_device
, &dev_attr_debounce_delay
);
406 device_remove_file(&debounce_device
, &dev_attr_settle_time
);
407 device_remove_file(&debounce_device
, &dev_attr_poll_time
);
408 device_remove_file(&debounce_device
, &dev_attr_flags
);
409 device_remove_file(&debounce_device
, &dev_attr_debounce_flag
);
410 device_remove_file(&debounce_device
, &dev_attr_remove_some_phantom_keys_flag
);
411 device_remove_file(&debounce_device
, &dev_attr_print_unmapped_keys_flag
);
412 device_remove_file(&debounce_device
, &dev_attr_print_mapped_keys_flag
);
413 device_remove_file(&debounce_device
, &dev_attr_print_phantom_keys_flag
);
414 device_remove_file(&debounce_device
, &dev_attr_active_high_flag
);
415 device_remove_file(&debounce_device
, &dev_attr_drive_inactive_flag
);
416 device_unregister(&debounce_device
);
419 module_init(debounce_init
);
420 module_exit(debounce_exit
);
422 MODULE_LICENSE("GPL");
423 MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>");