/*
* 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;
};
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);
}
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(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;
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;
}
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:
{
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)
/* -------------------------------------------------------------------------*/
-#ifdef CONFIG_HOTPLUG
static int button_get_index(unsigned int code)
{
int i;
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;
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;
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;
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);
+ int state = gpio_button_get_value(bdata);
+
+ if (state != bdata->last_state) {
+ unsigned int type = bdata->b->type ?: EV_KEY;
+
+ if (bdata->last_state != -1 || type == EV_SW)
+ button_hotplug_event(bdata, type, state);
+
+ bdata->last_state = state;
+ }
+}
+
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;
}
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",
dev_err(dev, "missing platform data\n");
return -EINVAL;
}
- pdev->dev.platform_data = pdata;
}
if (polled && !pdata->poll_interval) {
gpio, error);
return error;
}
+ bdata->gpiod = gpio_to_desc(gpio);
+ if (!bdata->gpiod)
+ return -EINVAL;
error = gpio_direction_input(gpio);
if (error) {
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];
}
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));
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);
if (bdev->polled)
gpio_keys_polled_close(bdev);
+ else
+ gpio_keys_irq_close(bdev);
return 0;
}
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);