]> git.zerfleddert.de Git - ms2-fixes/blobdiff - debounce.c
better hw debounce time
[ms2-fixes] / debounce.c
index ec20fdb55444a8e222e3250955d1e3cd01a908b2..951121217b76f12c975700ef4a00503aa80be24c 100644 (file)
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/gpio_event.h>
+#include <mach/gpio.h>
 
-int find_ms2_dev(struct device *dev, void *data)
+/* hardware debounce: (value + 1) * 31us */
+#define GPIO_DEBOUNCE_TIME              0x3
+
+#define PREFIX "debounce: "
+
+static unsigned old_flags = 0;
+ktime_t old_debounce_delay;
+ktime_t old_settle_time;
+ktime_t old_poll_time;
+static struct gpio_event_matrix_info *gpio_evmi = NULL;
+
+static int find_ms2_dev(struct device *dev, void *data)
 {
        if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) {
-               printk(KERN_INFO "Found it!\n");
+               printk(KERN_INFO PREFIX "Found it\n");
                return 1;
        }
        return 0;
 }
 
+static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC));
+}
+
+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;
+               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");
+                       gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
+               }
+       } else {
+               if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) {
+                       printk(KERN_INFO PREFIX "Deactivating debounce\n");
+                       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)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       set_debounce_delay(delay);
+
+       return count;
+}
+
+static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->settle_time.tv.nsec / NSEC_PER_USEC));
+}
+
+static ssize_t store_settle_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC;
+
+       return count;
+}
+
+static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->poll_time.tv.nsec / NSEC_PER_MSEC));
+}
+
+static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC;
+
+       return count;
+}
+
+static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "0x%x\n", gpio_evmi->flags);
+}
+
+static ssize_t store_flags(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flags;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "0x%x", &flags);
+
+       printk(KERN_INFO PREFIX "flags: 0x%x\n", flags);
+
+       gpio_evmi->flags = flags;
+
+       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 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;
+       }
+
+       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 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), show_active_high_flag, store_active_high_flag);
+static DEVICE_ATTR(drive_inactive_flag, (S_IRUGO | S_IWUGO), show_drive_inactive_flag, store_drive_inactive_flag);
+
+static void debounce_release(struct device *dev)
+{
+}
+
+static struct device debounce_device = {
+       .init_name = "debounce",
+       .release = debounce_release,
+};
+
+static unsigned int mapphone_col_gpios[] = { 43, 53, 54, 55, 56, 57, 58, 63 };
+static unsigned int mapphone_row_gpios[] = { 34, 35, 36, 37, 38, 39, 40, 41 };
+
+static void hw_debounce_pin(int gpio, int enable) {
+       printk(KERN_INFO PREFIX "%sabling hardware debounce for GPIO %d\n", (enable?"En":"Dis"), gpio);
+       if (enable)
+               omap_set_gpio_debounce_time(gpio, GPIO_DEBOUNCE_TIME);
+       omap_set_gpio_debounce(gpio, enable);
+}
+
+static void hw_debounce(int enable) {
+       int i;
+
+       for (i = 0; i < (sizeof(mapphone_col_gpios) / sizeof(mapphone_col_gpios[0])); i++) {
+               hw_debounce_pin(mapphone_col_gpios[i], enable);
+       }
+
+       for (i = 0; i < (sizeof(mapphone_row_gpios) / sizeof(mapphone_row_gpios[0])); i++) {
+               hw_debounce_pin(mapphone_row_gpios[i], enable);
+       }
+}
+
 static int __init debounce_init(void)
 {
        struct device *event_dev = NULL;
        struct gpio_event_platform_data *gpio_epd;
        struct gpio_event_info *gpio_ei;
-       struct gpio_event_matrix_info *gpio_evmi;
-
-       printk(KERN_INFO "Searching for " GPIO_EVENT_DEV_NAME "...\n");
+       int err = 0;
 
+       printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
        
        event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
        if (event_dev == NULL)
                return -ENODEV;
 
        gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data;
-       printk(KERN_INFO "And there is a %s connected...\n", gpio_epd->name);
+       printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name);
        if (strcmp(gpio_epd->name, "sholes-keypad"))
                return -ENODEV;
 
        gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
        gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
 
-       printk(KERN_INFO "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
-       printk(KERN_INFO "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
-       printk(KERN_INFO "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
-       if (!gpio_evmi->debounce_delay.tv.nsec) {
-               printk(KERN_INFO "Activating debounce!\n");
-               gpio_evmi->debounce_delay.tv.nsec = 5 * NSEC_PER_MSEC;
-               printk(KERN_INFO "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
+       err = device_register(&debounce_device);
+       if (err) {
+               return err;
        }
 
+       hw_debounce(1);
+
+       err = device_create_file(&debounce_device, &dev_attr_debounce_delay);
+       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_drive_inactive_flag);
+
+       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);
+       printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
+       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
+
+       old_debounce_delay = gpio_evmi->debounce_delay;
+       old_settle_time = gpio_evmi->settle_time;
+       old_poll_time = gpio_evmi->poll_time;
+       old_flags = gpio_evmi->flags;
+
+       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
+
        return 0;
 }
 
 static void __exit debounce_exit(void)
 {
+       if (gpio_evmi) {
+               if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) {
+                       printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
+                       gpio_evmi->debounce_delay = old_debounce_delay;
+                       printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
+               }
+               if (gpio_evmi->flags != old_flags) {
+                       printk(KERN_INFO PREFIX "Restoring flags\n");
+                       gpio_evmi->flags = old_flags;
+                       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
+               }
+               gpio_evmi->settle_time = old_settle_time;
+               gpio_evmi->poll_time = old_poll_time;
+       }
+       hw_debounce(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_drive_inactive_flag);
+       device_unregister(&debounce_device);
 }
 
 module_init(debounce_init);
Impressum, Datenschutz