From patchwork Wed Feb 2 18:04:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Cai X-Patchwork-Id: 1587749 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=typeblog.net header.i=@typeblog.net header.a=rsa-sha256 header.s=mailcow header.b=ectoXgCZ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Jpqbv3n25z9sCD for ; Thu, 3 Feb 2022 05:10:49 +1100 (AEDT) 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) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 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 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; }