gpio-button-hotplug: gpio-keys: fix always missing first event
[openwrt/openwrt.git] / package / kernel / gpio-button-hotplug / src / gpio-button-hotplug.c
index 6b69d2751c7c4470bf39afc4068580a02b9e08af..daa4b2a4f71951e7a73585e63d194bae0b19cbae 100644 (file)
@@ -1,12 +1,12 @@
 /*
  *  GPIO Button Hotplug driver
  *
- *  Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ *  Copyright (C) 2012 Felix Fietkau <nbd@nbd.name>
  *  Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
  *
  *  Based on the diag.c - GPIO interface driver for Broadcom boards
  *    Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
- *    Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
+ *    Copyright (C) 2006-2007 Felix Fietkau <nbd@nbd.name>
  *    Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
  *
  *  This program is free software; you can redistribute it and/or modify it
 #include <linux/interrupt.h>
 #include <linux/platform_device.h>
 #include <linux/of_gpio.h>
+#include <linux/of_irq.h>
 #include <linux/gpio_keys.h>
 
-#define DRV_NAME       "gpio-keys"
-
 #define BH_SKB_SIZE    2048
 
+#define DRV_NAME       "gpio-keys"
 #define PFX    DRV_NAME ": "
 
-#undef BH_DEBUG
-
-#ifdef BH_DEBUG
-#define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
-#else
-#define BH_DBG(fmt, args...) do {} while (0)
-#endif
-
-#define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
-
 struct bh_priv {
        unsigned long           seen;
 };
@@ -70,7 +60,10 @@ struct gpio_keys_button_data {
        int count;
        int threshold;
        int can_sleep;
-       struct gpio_keys_button *b;
+       int irq;
+       unsigned int software_debounce;
+       struct gpio_desc *gpiod;
+       const struct gpio_keys_button *b;
 };
 
 extern u64 uevent_next_seqnum(void);
@@ -82,27 +75,37 @@ extern u64 uevent_next_seqnum(void);
        }
 
 static struct bh_map button_map[] = {
-       BH_MAP(BTN_0,           "BTN_0"),
-       BH_MAP(BTN_1,           "BTN_1"),
-       BH_MAP(BTN_2,           "BTN_2"),
-       BH_MAP(BTN_3,           "BTN_3"),
-       BH_MAP(BTN_4,           "BTN_4"),
-       BH_MAP(BTN_5,           "BTN_5"),
-       BH_MAP(BTN_6,           "BTN_6"),
-       BH_MAP(BTN_7,           "BTN_7"),
-       BH_MAP(BTN_8,           "BTN_8"),
-       BH_MAP(BTN_9,           "BTN_9"),
-       BH_MAP(KEY_POWER,       "power"),
-       BH_MAP(KEY_RESTART,     "reset"),
-       BH_MAP(KEY_RFKILL,      "rfkill"),
-       BH_MAP(KEY_WPS_BUTTON,  "wps"),
-       BH_MAP(KEY_WIMAX,       "wwan"),
+       BH_MAP(BTN_0,                   "BTN_0"),
+       BH_MAP(BTN_1,                   "BTN_1"),
+       BH_MAP(BTN_2,                   "BTN_2"),
+       BH_MAP(BTN_3,                   "BTN_3"),
+       BH_MAP(BTN_4,                   "BTN_4"),
+       BH_MAP(BTN_5,                   "BTN_5"),
+       BH_MAP(BTN_6,                   "BTN_6"),
+       BH_MAP(BTN_7,                   "BTN_7"),
+       BH_MAP(BTN_8,                   "BTN_8"),
+       BH_MAP(BTN_9,                   "BTN_9"),
+       BH_MAP(KEY_BRIGHTNESS_ZERO,     "brightness_zero"),
+       BH_MAP(KEY_CONFIG,              "config"),
+       BH_MAP(KEY_COPY,                "copy"),
+       BH_MAP(KEY_EJECTCD,             "eject"),
+       BH_MAP(KEY_HELP,                "help"),
+       BH_MAP(KEY_LIGHTS_TOGGLE,       "lights_toggle"),
+       BH_MAP(KEY_PHONE,               "phone"),
+       BH_MAP(KEY_POWER,               "power"),
+       BH_MAP(KEY_POWER2,              "reboot"),
+       BH_MAP(KEY_RESTART,             "reset"),
+       BH_MAP(KEY_RFKILL,              "rfkill"),
+       BH_MAP(KEY_VIDEO,               "video"),
+       BH_MAP(KEY_WIMAX,               "wwan"),
+       BH_MAP(KEY_WLAN,                "wlan"),
+       BH_MAP(KEY_WPS_BUTTON,          "wps"),
 };
 
 /* -------------------------------------------------------------------------*/
 
-static int bh_event_add_var(struct bh_event *event, int argv,
-               const char *format, ...)
+static __printf(3, 4)
+int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...)
 {
        static char buf[128];
        char *s;
@@ -117,15 +120,14 @@ static int bh_event_add_var(struct bh_event *event, int argv,
        va_end(args);
 
        if (len >= sizeof(buf)) {
-               BH_ERR("buffer size too small\n");
-               WARN_ON(1);
+               WARN(1, "buffer size too small");
                return -ENOMEM;
        }
 
        s = skb_put(event->skb, len + 1);
        strcpy(s, buf);
 
-       BH_DBG("added variable '%s'\n", s);
+       pr_debug(PFX "added variable '%s'\n", s);
 
        return 0;
 }
@@ -192,7 +194,7 @@ static void button_hotplug_work(struct work_struct *work)
 
  out_free_skb:
        if (ret) {
-               BH_ERR("work error %d\n", ret);
+               pr_err(PFX "work error %d\n", ret);
                kfree_skb(event->skb);
        }
  out_free_event:
@@ -204,8 +206,8 @@ static int button_hotplug_create_event(const char *name, unsigned int type,
 {
        struct bh_event *event;
 
-       BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
-               name, seen, pressed);
+       pr_debug(PFX "create event, name=%s, seen=%lu, pressed=%d\n",
+                name, seen, pressed);
 
        event = kzalloc(sizeof(*event), GFP_KERNEL);
        if (!event)
@@ -224,7 +226,6 @@ static int button_hotplug_create_event(const char *name, unsigned int type,
 
 /* -------------------------------------------------------------------------*/
 
-#ifdef CONFIG_HOTPLUG
 static int button_get_index(unsigned int code)
 {
        int i;
@@ -243,7 +244,7 @@ static void button_hotplug_event(struct gpio_keys_button_data *data,
        unsigned long seen = jiffies;
        int btn;
 
-       BH_DBG("event type=%u, code=%u, value=%d\n", type, data->b->code, value);
+       pr_debug(PFX "event type=%u, code=%u, value=%d\n", type, data->b->code, value);
 
        if ((type != EV_KEY) && (type != EV_SW))
                return;
@@ -252,16 +253,13 @@ static void button_hotplug_event(struct gpio_keys_button_data *data,
        if (btn < 0)
                return;
 
+       if (priv->seen == 0)
+               priv->seen = seen;
+
        button_hotplug_create_event(button_map[btn].name, type,
                        (seen - priv->seen) / HZ, value);
        priv->seen = seen;
 }
-#else
-static void button_hotplug_event(struct gpio_keys_button_data *data,
-                          unsigned int type, int value)
-{
-}
-#endif /* CONFIG_HOTPLUG */
 
 struct gpio_keys_button_dev {
        int polled;
@@ -296,7 +294,7 @@ static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata)
                        return;
                }
 
-               if ((bdata->last_state != -1) || (type == EV_SW))
+               if (bdata->last_state != -1 || type == EV_SW)
                        button_hotplug_event(bdata, type, state);
 
                bdata->last_state = state;
@@ -338,11 +336,22 @@ static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
                pdata->disable(bdev->dev);
 }
 
+static void gpio_keys_irq_work_func(struct work_struct *work)
+{
+       struct gpio_keys_button_data *bdata = container_of(work,
+               struct gpio_keys_button_data, work.work);
+
+       button_hotplug_event(bdata, bdata->b->type ?: EV_KEY,
+                            gpio_button_get_value(bdata));
+}
+
 static irqreturn_t button_handle_irq(int irq, void *_bdata)
 {
-       struct gpio_keys_button_data *bdata = (struct gpio_keys_button_data *) _bdata;
+       struct gpio_keys_button_data *bdata =
+               (struct gpio_keys_button_data *) _bdata;
 
-       button_hotplug_event(bdata, bdata->b->type ?: EV_KEY, gpio_button_get_value(bdata));
+       schedule_delayed_work(&bdata->work,
+                             msecs_to_jiffies(bdata->software_debounce));
 
        return IRQ_HANDLED;
 }
@@ -388,10 +397,23 @@ gpio_keys_get_devtree_pdata(struct device *dev)
                        continue;
                }
 
-               button = &pdata->buttons[i++];
+               button = (struct gpio_keys_button *)(&pdata->buttons[i++]);
+
+               button->irq = irq_of_parse_and_map(pp, 0);
 
                button->gpio = of_get_gpio_flags(pp, 0, &flags);
-               button->active_low = flags & OF_GPIO_ACTIVE_LOW;
+               if (button->gpio < 0) {
+                       error = button->gpio;
+                       if (error != -ENOENT) {
+                               if (error != -EPROBE_DEFER)
+                                       dev_err(dev,
+                                               "Failed to get gpio flags, error: %d\n",
+                                               error);
+                               return ERR_PTR(error);
+                       }
+               } else {
+                       button->active_low = flags & OF_GPIO_ACTIVE_LOW;
+               }
 
                if (of_property_read_u32(pp, "linux,code", &button->code)) {
                        dev_err(dev, "Button without keycode: 0x%x\n",
@@ -462,7 +484,6 @@ static int gpio_keys_button_probe(struct platform_device *pdev,
                        dev_err(dev, "missing platform data\n");
                        return -EINVAL;
                }
-               pdev->dev.platform_data = pdata;
        }
 
        if (polled && !pdata->poll_interval) {
@@ -505,6 +526,9 @@ static int gpio_keys_button_probe(struct platform_device *pdev,
                                gpio, error);
                        return error;
                }
+               bdata->gpiod = gpio_to_desc(gpio);
+               if (!bdata->gpiod)
+                       return -EINVAL;
 
                error = gpio_direction_input(gpio);
                if (error) {
@@ -517,12 +541,26 @@ static int gpio_keys_button_probe(struct platform_device *pdev,
                bdata->can_sleep = gpio_cansleep(gpio);
                bdata->last_state = -1;
 
-               if (bdev->polled)
+               if (bdev->polled) {
                        bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
-                                               pdata->poll_interval);
-               else
+                                                       pdata->poll_interval);
+               } else {
                        bdata->threshold = 1;
 
+                       if (button->debounce_interval) {
+                               error = gpiod_set_debounce(bdata->gpiod,
+                                       button->debounce_interval * 1000);
+                               /*
+                                * use timer if gpiolib doesn't provide
+                                * debounce.
+                                */
+                               if (error < 0) {
+                                       bdata->software_debounce =
+                                               button->debounce_interval;
+                               }
+                       }
+               }
+
                bdata->b = &pdata->buttons[i];
        }
 
@@ -547,28 +585,41 @@ static int gpio_keys_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
-       pdata = pdev->dev.platform_data;
+       pdata = bdev->pdata;
        for (i = 0; i < pdata->nbuttons; i++) {
-               struct gpio_keys_button *button = &pdata->buttons[i];
+               const struct gpio_keys_button *button = &pdata->buttons[i];
                struct gpio_keys_button_data *bdata = &bdev->data[i];
+               unsigned long irqflags = IRQF_ONESHOT;
 
-               if (bdata->can_sleep) {
-                       dev_err(&pdev->dev, "skipping gpio:%d, it can sleep\n", button->gpio);
-                       continue;
+               if (!button->irq) {
+                       bdata->irq = gpio_to_irq(button->gpio);
+
+                       if (bdata->irq < 0) {
+                               dev_err(&pdev->dev, "failed to get irq for gpio:%d\n",
+                                       button->gpio);
+                               continue;
+                       }
+
+                       irqflags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
+               } else {
+                       bdata->irq = button->irq;
                }
-               if (!button->irq)
-                       button->irq = gpio_to_irq(button->gpio);
-               if (button->irq < 0) {
-                       dev_err(&pdev->dev, "failed to get irq for gpio:%d\n", button->gpio);
+
+               INIT_DELAYED_WORK(&bdata->work, gpio_keys_irq_work_func);
+
+               ret = devm_request_threaded_irq(&pdev->dev,
+                       bdata->irq, NULL, button_handle_irq,
+                       irqflags, dev_name(&pdev->dev), bdata);
+
+               if (ret < 0) {
+                       bdata->irq = 0;
+                       dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n",
+                               bdata->irq, button->gpio);
                        continue;
+               } else {
+                       dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n",
+                               button->gpio, bdata->irq);
                }
-               ret = devm_request_irq(&pdev->dev, button->irq, button_handle_irq,
-                                       IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
-                                       dev_name(&pdev->dev), bdata);
-               if (ret)
-                       dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n", button->irq, button->gpio);
-               else
-                       dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n", button->gpio, button->irq);
 
                if (bdata->b->type == EV_SW)
                        button_hotplug_event(bdata, EV_SW, gpio_button_get_value(bdata));
@@ -604,6 +655,19 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
        return ret;
 }
 
+static void gpio_keys_irq_close(struct gpio_keys_button_dev *bdev)
+{
+       struct gpio_keys_platform_data *pdata = bdev->pdata;
+       size_t i;
+
+       for (i = 0; i < pdata->nbuttons; i++) {
+               struct gpio_keys_button_data *bdata = &bdev->data[i];
+
+               disable_irq(bdata->irq);
+               cancel_delayed_work_sync(&bdata->work);
+       }
+}
+
 static int gpio_keys_remove(struct platform_device *pdev)
 {
        struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev);
@@ -612,6 +676,8 @@ static int gpio_keys_remove(struct platform_device *pdev)
 
        if (bdev->polled)
                gpio_keys_polled_close(bdev);
+       else
+               gpio_keys_irq_close(bdev);
 
        return 0;
 }
@@ -661,7 +727,7 @@ module_init(gpio_button_init);
 module_exit(gpio_button_exit);
 
 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
-MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
+MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
 MODULE_LICENSE("GPL v2");
 MODULE_ALIAS("platform:" DRV_NAME);