diff mbox series

button: qcom-pmic: add software debounce

Message ID 20241113045109.1838241-1-caleb.connolly@linaro.org
State New
Headers show
Series button: qcom-pmic: add software debounce | expand

Commit Message

Caleb Connolly Nov. 13, 2024, 4:51 a.m. UTC
This helps with reliability on some platforms. We should probably also
configure the hardware debounce timer eventually.

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
---
 drivers/button/button-qcom-pmic.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/drivers/button/button-qcom-pmic.c b/drivers/button/button-qcom-pmic.c
index ad7fed3ddaaa..f9f0948ae095 100644
--- a/drivers/button/button-qcom-pmic.c
+++ b/drivers/button/button-qcom-pmic.c
@@ -14,8 +14,9 @@ 
 #include <log.h>
 #include <power/pmic.h>
 #include <spmi/spmi.h>
 #include <linux/bitops.h>
+#include <time.h>
 
 #define REG_TYPE		0x4
 #define REG_SUBTYPE		0x5
 
@@ -30,8 +31,9 @@  struct qcom_pmic_btn_priv {
 	u32 base;
 	u32 status_bit;
 	int code;
 	struct udevice *pmic;
+	ulong last_release_time;
 };
 
 #define PON_INT_RT_STS                        0x10
 #define  PON_KPDPWR_N_SET		0
@@ -41,15 +43,23 @@  struct qcom_pmic_btn_priv {
 
 static enum button_state_t qcom_pwrkey_get_state(struct udevice *dev)
 {
 	struct qcom_pmic_btn_priv *priv = dev_get_priv(dev);
+	bool pressed;
+	int reg;
 
-	int reg = pmic_reg_read(priv->pmic, priv->base + PON_INT_RT_STS);
+	if (get_timer_us(0) - priv->last_release_time < 25000)
+		return BUTTON_OFF;
 
+	reg = pmic_reg_read(priv->pmic, priv->base + PON_INT_RT_STS);
 	if (reg < 0)
 		return 0;
 
-	return (reg & BIT(priv->status_bit)) != 0;
+	pressed = !!(reg & BIT(priv->status_bit));
+	if (!pressed)
+		priv->last_release_time = get_timer_us(0);
+
+	return pressed;
 }
 
 static int qcom_pwrkey_get_code(struct udevice *dev)
 {