]> cvs.zerfleddert.de Git - ms2-fixes/blobdiff - debounce.c
add (disabled) fixup-handler
[ms2-fixes] / debounce.c
index cefa1d9b2837773bec4ea1eb8ea599023a146a2d..399e97bbdb28fdecf0b572fced0dc995487f5cb3 100644 (file)
@@ -2,19 +2,20 @@
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/gpio_event.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <mach/gpio.h>
 
 #define PREFIX "debounce: "
 
-static int debounce_delay = 15;
-
 static unsigned old_flags = 0;
-ktime_t old_debounce_delay;
-ktime_t old_settle_time;
-ktime_t old_poll_time;
+static ktime_t old_debounce_delay;
+static ktime_t old_settle_time;
+static ktime_t old_poll_time;
+static int (*old_sw_fixup)(int index);
 static struct gpio_event_matrix_info *gpio_evmi = NULL;
-
-module_param(debounce_delay, int, S_IRUSR | S_IRGRP | S_IROTH);
-MODULE_PARM_DESC(debounce_delay, "debouncing delay (ms), default: 15");
+static int hw_debounce = 0;
+static int hw_debounce_time = 0;
 
 static int find_ms2_dev(struct device *dev, void *data)
 {
@@ -25,6 +26,65 @@ static int find_ms2_dev(struct device *dev, void *data)
        return 0;
 }
 
+/* hardware debounce: (time + 1) * 31us */
+static void hw_debounce_set(int enable, int time) {
+       int i;
+
+       if (gpio_evmi == NULL)
+               return;
+
+       for (i = 0; i < gpio_evmi->ninputs; i++) {
+               int gpio = gpio_evmi->input_gpios[i];
+
+               if ((time != -1) && (time != hw_debounce_time) && hw_debounce) {
+                       printk(KERN_INFO PREFIX "Setting hardware debounce time for GPIO %d to %d (%dus)\n", gpio, time, (time+1)*31);
+                       omap_set_gpio_debounce_time(gpio, time);
+               }
+
+               if ((enable != -1) && (enable != hw_debounce)) {
+                       printk(KERN_INFO PREFIX "%sabling hardware debounce for GPIO %d\n", (enable?"En":"Dis"), gpio);
+                       omap_set_gpio_debounce(gpio, enable);
+               }
+       }
+}
+
+static void set_irq_types(void) {
+       int err;
+       unsigned int irq;
+       unsigned long type;
+       int i;
+
+       if (gpio_evmi == NULL)
+               return;
+
+       switch (gpio_evmi->flags & (GPIOKPF_ACTIVE_HIGH|GPIOKPF_LEVEL_TRIGGERED_IRQ)) {
+       default:
+               type = IRQ_TYPE_EDGE_FALLING;
+               break;
+       case GPIOKPF_ACTIVE_HIGH:
+               type = IRQ_TYPE_EDGE_RISING;
+               break;
+       case GPIOKPF_LEVEL_TRIGGERED_IRQ:
+               type = IRQ_TYPE_LEVEL_LOW;
+               break;
+       case GPIOKPF_LEVEL_TRIGGERED_IRQ | GPIOKPF_ACTIVE_HIGH:
+               type = IRQ_TYPE_LEVEL_HIGH;
+               break;
+       }
+
+       printk(KERN_INFO PREFIX "Settinhg IRQ type to 0x%lx\n", type);
+
+       for (i = 0; i < gpio_evmi->ninputs; i++) {
+
+               err = irq = gpio_to_irq(gpio_evmi->input_gpios[i]);
+
+               if (err < 0)
+                       return;
+
+               err = set_irq_type(irq, type);
+       }
+}
+
 static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
 {
        if (!gpio_evmi) 
@@ -38,10 +98,10 @@ static void set_debounce_delay(long delay)
        if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) {
                printk(KERN_INFO PREFIX "Changing debounce_delay\n");
                gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC;
-               debounce_delay = delay;
                printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
        }
 
+#if 0
        if (gpio_evmi->debounce_delay.tv.nsec != 0) {
                if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
                        printk(KERN_INFO PREFIX "Activating debounce\n");
@@ -53,6 +113,7 @@ static void set_debounce_delay(long delay)
                        gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
                }
        }
+#endif
 }
 
 static ssize_t store_debounce_delay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
@@ -65,7 +126,7 @@ static ssize_t store_debounce_delay(struct device *dev, struct device_attribute
        sscanf(buf, "%ld", &delay);
        set_debounce_delay(delay);
 
-       return strnlen(buf, PAGE_SIZE);
+       return count;
 }
 
 static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf)
@@ -86,7 +147,7 @@ static ssize_t store_settle_time(struct device *dev, struct device_attribute *at
        sscanf(buf, "%ld", &delay);
        gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC;
 
-       return strnlen(buf, PAGE_SIZE);
+       return count;
 }
 
 static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf)
@@ -107,7 +168,7 @@ static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr
        sscanf(buf, "%ld", &delay);
        gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC;
 
-       return strnlen(buf, PAGE_SIZE);
+       return count;
 }
 
 static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf)
@@ -126,15 +187,303 @@ static ssize_t store_flags(struct device *dev, struct device_attribute *attr, co
                return -ENODEV;
 
        sscanf(buf, "0x%x", &flags);
+
+       printk(KERN_INFO PREFIX "flags: 0x%x\n", flags);
+
        gpio_evmi->flags = flags;
 
-       return strnlen(buf, PAGE_SIZE);
+       return count;
+}
+
+static ssize_t show_debounce_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DEBOUNCE) ? 1 : 0);
+}
+
+static ssize_t store_debounce_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
+       }
+
+       return count;
+}
+
+static ssize_t show_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_REMOVE_SOME_PHANTOM_KEYS) ? 1 : 0);
+}
+
+static ssize_t store_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
+       }
+
+       return count;
 }
 
-static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), &show_debounce_delay, store_debounce_delay);
-static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), &show_settle_time, store_settle_time);
-static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), &show_poll_time, store_poll_time);
-static DEVICE_ATTR(flags, (S_IRUGO | S_IWUGO), &show_flags, store_flags);
+static ssize_t show_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_UNMAPPED_KEYS) ? 1 : 0);
+}
+
+static ssize_t store_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_PRINT_UNMAPPED_KEYS;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_PRINT_UNMAPPED_KEYS;
+       }
+
+       return count;
+}
+
+static ssize_t show_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_MAPPED_KEYS) ? 1 : 0);
+}
+
+static ssize_t store_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_PRINT_MAPPED_KEYS;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_PRINT_MAPPED_KEYS;
+       }
+
+       return count;
+}
+
+static ssize_t show_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_PHANTOM_KEYS) ? 1 : 0);
+}
+
+static ssize_t store_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_PRINT_PHANTOM_KEYS;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_PRINT_PHANTOM_KEYS;
+       }
+
+       return count;
+}
+
+static ssize_t show_active_high_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_ACTIVE_HIGH) ? 1 : 0);
+}
+
+static ssize_t store_active_high_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_ACTIVE_HIGH;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_ACTIVE_HIGH;
+       }
+
+       set_irq_types();
+
+       return count;
+}
+
+static ssize_t show_level_triggered_irq_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_LEVEL_TRIGGERED_IRQ) ? 1 : 0);
+}
+
+static ssize_t store_level_triggered_irq_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_LEVEL_TRIGGERED_IRQ;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_LEVEL_TRIGGERED_IRQ;
+       }
+
+       set_irq_types();
+
+       return count;
+}
+
+static ssize_t show_drive_inactive_flag(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DRIVE_INACTIVE) ? 1 : 0);
+}
+
+static ssize_t store_drive_inactive_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flag;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%u", &flag);
+
+       if (flag) {
+               gpio_evmi->flags |= GPIOKPF_DRIVE_INACTIVE;
+       } else {
+               gpio_evmi->flags &= ~GPIOKPF_DRIVE_INACTIVE;
+       }
+
+       return count;
+}
+
+static ssize_t show_hw_debounce(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%d\n", hw_debounce);
+}
+
+static ssize_t store_hw_debounce(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       int enable;
+
+       sscanf(buf, "%d", &enable);
+
+       if (enable) {
+               hw_debounce_set(1, -1);
+               hw_debounce = 1;
+       }
+       else {
+               hw_debounce_set(-1, 0);
+               hw_debounce_set(0, -1);
+               hw_debounce = 0;
+               hw_debounce_time = 0;
+       }
+
+       return count;
+}
+
+static ssize_t show_hw_debounce_time(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       return snprintf(buf, PAGE_SIZE, "%d\n", hw_debounce_time);
+}
+
+static ssize_t store_hw_debounce_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       int time;
+
+       sscanf(buf, "%d", &time);
+
+       if ((time < 0) || (time > 0xff))
+               return count;
+
+       if (!hw_debounce)
+               return count;
+
+       hw_debounce_set(-1, time);
+       hw_debounce_time = time;
+
+       return count;
+}
+
+static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), show_debounce_delay, store_debounce_delay);
+static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), show_settle_time, store_settle_time);
+static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), show_poll_time, store_poll_time);
+static DEVICE_ATTR(flags, (S_IRUGO), show_flags, store_flags);
+static DEVICE_ATTR(debounce_flag, (S_IRUGO | S_IWUGO), show_debounce_flag, store_debounce_flag);
+static DEVICE_ATTR(remove_some_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_remove_some_phantom_keys_flag, store_remove_some_phantom_keys_flag);
+static DEVICE_ATTR(print_unmapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_unmapped_keys_flag, store_print_unmapped_keys_flag);
+static DEVICE_ATTR(print_mapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_mapped_keys_flag, store_print_mapped_keys_flag);
+static DEVICE_ATTR(print_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_print_phantom_keys_flag, store_print_phantom_keys_flag);
+static DEVICE_ATTR(active_high_flag, (S_IRUGO | S_IWUGO), show_active_high_flag, store_active_high_flag);
+static DEVICE_ATTR(level_triggered_irq_flag, (S_IRUGO | S_IWUGO), show_level_triggered_irq_flag, store_level_triggered_irq_flag);
+static DEVICE_ATTR(drive_inactive_flag, (S_IRUGO | S_IWUGO), show_drive_inactive_flag, store_drive_inactive_flag);
+static DEVICE_ATTR(hw_debounce, (S_IRUGO | S_IWUGO), show_hw_debounce, store_hw_debounce);
+static DEVICE_ATTR(hw_debounce_time, (S_IRUGO | S_IWUGO), show_hw_debounce_time, store_hw_debounce_time);
+
+#if 0
+static int debounce_fixup(int index)
+{
+       int ret;
+
+       printk(KERN_INFO PREFIX "0x%x\n", index);
+
+       ret = old_sw_fixup(index);
+       if (!ret)
+               printk(KERN_INFO PREFIX "IGNORED!\n");
+
+       return ret;
+}
+#endif
 
 static void debounce_release(struct device *dev)
 {
@@ -175,6 +524,16 @@ static int __init debounce_init(void)
        err = device_create_file(&debounce_device, &dev_attr_settle_time);
        err = device_create_file(&debounce_device, &dev_attr_poll_time);
        err = device_create_file(&debounce_device, &dev_attr_flags);
+       err = device_create_file(&debounce_device, &dev_attr_debounce_flag);
+       err = device_create_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
+       err = device_create_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
+       err = device_create_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
+       err = device_create_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
+       err = device_create_file(&debounce_device, &dev_attr_active_high_flag);
+       err = device_create_file(&debounce_device, &dev_attr_level_triggered_irq_flag);
+       err = device_create_file(&debounce_device, &dev_attr_drive_inactive_flag);
+       err = device_create_file(&debounce_device, &dev_attr_hw_debounce);
+       err = device_create_file(&debounce_device, &dev_attr_hw_debounce_time);
 
        printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
        printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
@@ -185,12 +544,12 @@ static int __init debounce_init(void)
        old_settle_time = gpio_evmi->settle_time;
        old_poll_time = gpio_evmi->poll_time;
        old_flags = gpio_evmi->flags;
+       old_sw_fixup = gpio_evmi->sw_fixup;
 
-       set_debounce_delay(debounce_delay);
 #if 0
-       gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
+       printk(KERN_INFO PREFIX "Registering fixup handler\n");
+       gpio_evmi->sw_fixup = debounce_fixup;
 #endif
-       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
 
        return 0;
 }
@@ -207,14 +566,31 @@ static void __exit debounce_exit(void)
                        printk(KERN_INFO PREFIX "Restoring flags\n");
                        gpio_evmi->flags = old_flags;
                        printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
+                       set_irq_types();
                }
                gpio_evmi->settle_time = old_settle_time;
                gpio_evmi->poll_time = old_poll_time;
+
+               if (gpio_evmi->sw_fixup != old_sw_fixup) {
+                       printk(KERN_INFO PREFIX "Restoring fixup handler\n");
+                       gpio_evmi->sw_fixup = old_sw_fixup;
+               }
        }
+       hw_debounce_set(0, 0);
        device_remove_file(&debounce_device, &dev_attr_debounce_delay);
        device_remove_file(&debounce_device, &dev_attr_settle_time);
        device_remove_file(&debounce_device, &dev_attr_poll_time);
        device_remove_file(&debounce_device, &dev_attr_flags);
+       device_remove_file(&debounce_device, &dev_attr_debounce_flag);
+       device_remove_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag);
+       device_remove_file(&debounce_device, &dev_attr_print_unmapped_keys_flag);
+       device_remove_file(&debounce_device, &dev_attr_print_mapped_keys_flag);
+       device_remove_file(&debounce_device, &dev_attr_print_phantom_keys_flag);
+       device_remove_file(&debounce_device, &dev_attr_active_high_flag);
+       device_remove_file(&debounce_device, &dev_attr_level_triggered_irq_flag);
+       device_remove_file(&debounce_device, &dev_attr_drive_inactive_flag);
+       device_remove_file(&debounce_device, &dev_attr_hw_debounce);
+       device_remove_file(&debounce_device, &dev_attr_hw_debounce_time);
        device_unregister(&debounce_device);
 }
 
Impressum, Datenschutz