Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1781C433EF for ; Wed, 2 Feb 2022 18:10:46 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id ABFE2838F8; Wed, 2 Feb 2022 19:10:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=typeblog.net Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=typeblog.net header.i=@typeblog.net header.b="ectoXgCZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 48AA7838D5; Wed, 2 Feb 2022 19:04:13 +0100 (CET) Received: from mail.typeblog.net (mail.typeblog.net [IPv6:2a0b:8bc0:2:13a2::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A4145838D5 for ; Wed, 2 Feb 2022 19:04:10 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=typeblog.net Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=peter@typeblog.net Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 1C471FEDB0; Wed, 2 Feb 2022 19:04:08 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=typeblog.net; s=mailcow; t=1643825049; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding; bh=GAN5vvJkuM0lY39yTS0iQbOfvelN/UeI7Am2+hGLvbI=; b=ectoXgCZrTx1d9YUOMCmyZI/uxRiABsCt/l2iQT/2fuhU62bRqEYUmD8XiF3Le8I5bNYIy NebqoTXe2rYTcF7U2xm1AJn2wAEzJIpKzdEQcM+MZtyuRDnkOl6jVP4HyvEdx/uvdb/NAB lnBBYtoi+GzLPx4zZB+HHbubfKqtI9ab6/GECZxE9e9kSuNkeNoPld0cqZRDebm+dRiER9 dsTOUsGyYQfhNLXtfadnZRgzFx14CkLnirdIvWn2WK6q0Yq+OEWjjKIO80rca++wfj1DZ6 eQnY7+HIWnoCWwUjcp44Vt4+8wafsaogqGbFjS9dcTBDWYlTkcbd7NPvOdxOTg== From: Peter Cai To: u-boot@lists.denx.de Cc: Peter Cai Subject: [PATCH] button: adc: set state to pressed when the voltage is closest to nominal Date: Wed, 2 Feb 2022 13:04:04 -0500 Message-Id: <20220202180404.281744-1-peter@typeblog.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 X-Mailman-Approved-At: Wed, 02 Feb 2022 19:10:43 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean In the Linux implementation of adc-keys (drivers/input/keyboard/adc-keys.c), `press-threshold-microvolt` is not really interpreted as a threshold, but rather as the "nominal voltage" of the button. When the voltage read from the ADC is closest to a button's `press-threshold-microvolt`, the button is considered pressed. This patch reconciles the behavior of button-adc with Linux's adc-keys such that device trees can be synchronized with minimal modifications. Signed-off-by: Peter Cai --- drivers/button/button-adc.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/drivers/button/button-adc.c b/drivers/button/button-adc.c index fd896c76f9d8..9c24c960e6ff 100644 --- a/drivers/button/button-adc.c +++ b/drivers/button/button-adc.c @@ -55,7 +55,7 @@ static int button_adc_of_to_plat(struct udevice *dev) struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev); struct button_adc_priv *priv = dev_get_priv(dev); struct ofnode_phandle_args args; - u32 threshold, up_threshold, t; + u32 down_threshold = 0, up_threshold, voltage, t; ofnode node; int ret; @@ -78,7 +78,7 @@ static int button_adc_of_to_plat(struct udevice *dev) return ret; ret = ofnode_read_u32(dev_ofnode(dev), "press-threshold-microvolt", - &threshold); + &voltage); if (ret) return ret; @@ -87,13 +87,24 @@ static int button_adc_of_to_plat(struct udevice *dev) if (ret) return ret; - if (t > threshold) + if (t > voltage && t < up_threshold) up_threshold = t; + else if (t < voltage && t > down_threshold) + down_threshold = t; } priv->channel = args.args[0]; - priv->min = threshold; - priv->max = up_threshold; + + /* + * Define the voltage range such that the button is only pressed + * when the voltage is closest to its own press-threshold-microvolt + */ + if (down_threshold == 0) + priv->min = 0; + else + priv->min = down_threshold + (voltage - down_threshold) / 2; + + priv->max = voltage + (up_threshold - voltage) / 2; return ret; } -- 2.35.0