51a2ae7
From 25bb14c1e78e641049fd1ee0c404a9ccd2755e44 Mon Sep 17 00:00:00 2001
51a2ae7
From: Hans de Goede <hdegoede@redhat.com>
51a2ae7
Date: Sat, 22 Jul 2017 13:00:05 +0200
51a2ae7
Subject: [PATCH 1/2] Input: gpio_keys - Allow suppression of input events for
51a2ae7
 wakeup button presses
51a2ae7
51a2ae7
In some cases it is undesirable for a wakeup button to send input events
51a2ae7
to userspace if pressed to wakeup the system (if pressed during suspend).
51a2ae7
51a2ae7
A typical example of this is the power-button on laptops / tablets,
51a2ae7
sending a KEY_POWER event to userspace when woken up with the power-button
51a2ae7
will cause userspace to immediately suspend the system again which is
51a2ae7
undesirable.
51a2ae7
51a2ae7
For power-buttons attached to a PMIC, or handled by e.g. ACPI, not sending
51a2ae7
an input event in this case is take care of by the PMIC / ACPI hardware /
51a2ae7
code. But in the case of a GPIO button we need to explicitly suppress the
51a2ae7
sending of the input event.
51a2ae7
51a2ae7
This commit adds support for this by adding a no_wakeup_events bool to
51a2ae7
struct gpio_keys_button, which platform code can set to suppress the
51a2ae7
input events for presses of wakeup keys during suspend.
51a2ae7
51a2ae7
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
51a2ae7
---
51a2ae7
Changes in v2:
51a2ae7
-This is a rewrite if my "Input: gpio_keys - Do not report wake button
51a2ae7
 presses as evdev events" patch.
51a2ae7
-Instead of unconditionally ignoring presses of all wake-up buttons during
51a2ae7
 suspend, this rewrite makes this configurable per button
51a2ae7
-This version uses a timer to delay clearing the suspended flag for software
51a2ae7
 debouncing, rather then jiffy compare magic
51a2ae7
---
51a2ae7
 drivers/input/keyboard/gpio_keys.c | 33 +++++++++++++++++++++++++++++++--
51a2ae7
 include/linux/gpio_keys.h          |  3 +++
51a2ae7
 2 files changed, 34 insertions(+), 2 deletions(-)
51a2ae7
51a2ae7
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
51a2ae7
index a047b9af8369..fa3a58620407 100644
51a2ae7
--- a/drivers/input/keyboard/gpio_keys.c
51a2ae7
+++ b/drivers/input/keyboard/gpio_keys.c
51a2ae7
@@ -38,6 +38,7 @@ struct gpio_button_data {
51a2ae7
 
51a2ae7
 	unsigned short *code;
51a2ae7
 
51a2ae7
+	struct timer_list unsuspend_timer;
51a2ae7
 	struct timer_list release_timer;
51a2ae7
 	unsigned int release_delay;	/* in msecs, for IRQ-only buttons */
51a2ae7
 
51a2ae7
@@ -371,6 +372,9 @@ static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
51a2ae7
 		return;
51a2ae7
 	}
51a2ae7
 
51a2ae7
+	if (state && bdata->button->no_wakeup_events && bdata->suspended)
51a2ae7
+		return;
51a2ae7
+
51a2ae7
 	if (type == EV_ABS) {
51a2ae7
 		if (state)
51a2ae7
 			input_event(input, type, button->code, button->value);
51a2ae7
@@ -400,6 +404,9 @@ static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
51a2ae7
 	if (bdata->button->wakeup) {
51a2ae7
 		const struct gpio_keys_button *button = bdata->button;
51a2ae7
 
51a2ae7
+		if (bdata->button->no_wakeup_events && bdata->suspended)
51a2ae7
+			return IRQ_HANDLED;
51a2ae7
+
51a2ae7
 		pm_stay_awake(bdata->input->dev.parent);
51a2ae7
 		if (bdata->suspended  &&
51a2ae7
 		    (button->type == 0 || button->type == EV_KEY)) {
51a2ae7
@@ -445,9 +452,13 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
51a2ae7
 	spin_lock_irqsave(&bdata->lock, flags);
51a2ae7
 
51a2ae7
 	if (!bdata->key_pressed) {
51a2ae7
-		if (bdata->button->wakeup)
51a2ae7
+		if (bdata->button->wakeup) {
51a2ae7
 			pm_wakeup_event(bdata->input->dev.parent, 0);
51a2ae7
 
51a2ae7
+			if (bdata->button->no_wakeup_events && bdata->suspended)
51a2ae7
+				goto out;
51a2ae7
+		}
51a2ae7
+
51a2ae7
 		input_event(input, EV_KEY, *bdata->code, 1);
51a2ae7
 		input_sync(input);
51a2ae7
 
51a2ae7
@@ -468,6 +479,13 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
51a2ae7
 	return IRQ_HANDLED;
51a2ae7
 }
51a2ae7
 
51a2ae7
+static void gpio_keys_unsuspend_timer(unsigned long _data)
51a2ae7
+{
51a2ae7
+	struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
51a2ae7
+
51a2ae7
+	bdata->suspended = false;
51a2ae7
+}
51a2ae7
+
51a2ae7
 static void gpio_keys_quiesce_key(void *data)
51a2ae7
 {
51a2ae7
 	struct gpio_button_data *bdata = data;
51a2ae7
@@ -476,6 +494,8 @@ static void gpio_keys_quiesce_key(void *data)
51a2ae7
 		cancel_delayed_work_sync(&bdata->work);
51a2ae7
 	else
51a2ae7
 		del_timer_sync(&bdata->release_timer);
51a2ae7
+
51a2ae7
+	del_timer_sync(&bdata->unsuspend_timer);
51a2ae7
 }
51a2ae7
 
51a2ae7
 static int gpio_keys_setup_key(struct platform_device *pdev,
51a2ae7
@@ -496,6 +516,8 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
51a2ae7
 	bdata->input = input;
51a2ae7
 	bdata->button = button;
51a2ae7
 	spin_lock_init(&bdata->lock);
51a2ae7
+	setup_timer(&bdata->unsuspend_timer, gpio_keys_unsuspend_timer,
51a2ae7
+		    (unsigned long)bdata);
51a2ae7
 
51a2ae7
 	if (child) {
51a2ae7
 		bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL,
51a2ae7
@@ -868,6 +890,7 @@ static int __maybe_unused gpio_keys_suspend(struct device *dev)
51a2ae7
 			struct gpio_button_data *bdata = &ddata->data[i];
51a2ae7
 			if (bdata->button->wakeup)
51a2ae7
 				enable_irq_wake(bdata->irq);
51a2ae7
+			del_timer_sync(&bdata->unsuspend_timer);
51a2ae7
 			bdata->suspended = true;
51a2ae7
 		}
51a2ae7
 	} else {
51a2ae7
@@ -892,7 +915,13 @@ static int __maybe_unused gpio_keys_resume(struct device *dev)
51a2ae7
 			struct gpio_button_data *bdata = &ddata->data[i];
51a2ae7
 			if (bdata->button->wakeup)
51a2ae7
 				disable_irq_wake(bdata->irq);
51a2ae7
-			bdata->suspended = false;
51a2ae7
+			if (bdata->button->no_wakeup_events) {
51a2ae7
+				mod_timer(&bdata->unsuspend_timer, jiffies +
51a2ae7
+					  msecs_to_jiffies(
51a2ae7
+						    bdata->software_debounce));
51a2ae7
+			} else {
51a2ae7
+				bdata->suspended = false;
51a2ae7
+			}
51a2ae7
 		}
51a2ae7
 	} else {
51a2ae7
 		mutex_lock(&input->mutex);
51a2ae7
diff --git a/include/linux/gpio_keys.h b/include/linux/gpio_keys.h
51a2ae7
index 0b71024c082c..d8a85e52b6bb 100644
51a2ae7
--- a/include/linux/gpio_keys.h
51a2ae7
+++ b/include/linux/gpio_keys.h
51a2ae7
@@ -15,6 +15,8 @@ struct device;
51a2ae7
  * @debounce_interval:	debounce ticks interval in msecs
51a2ae7
  * @can_disable:	%true indicates that userspace is allowed to
51a2ae7
  *			disable button via sysfs
51a2ae7
+ * @no_wakeup_events:	For wake-up source buttons only, if %true then no input
51a2ae7
+ *			events will be generated if pressed while suspended
51a2ae7
  * @value:		axis value for %EV_ABS
51a2ae7
  * @irq:		Irq number in case of interrupt keys
51a2ae7
  */
51a2ae7
@@ -27,6 +29,7 @@ struct gpio_keys_button {
51a2ae7
 	int wakeup;
51a2ae7
 	int debounce_interval;
51a2ae7
 	bool can_disable;
51a2ae7
+	bool no_wakeup_events;
51a2ae7
 	int value;
51a2ae7
 	unsigned int irq;
51a2ae7
 };
51a2ae7
-- 
51a2ae7
2.13.4
51a2ae7