diff mbox series

button: adc: set state to pressed when the voltage is closest to nominal

Message ID 20220202180404.281744-1-peter@typeblog.net
State Accepted
Commit 80d4c02b9324e7e0049582142474c9cc8630e27c
Delegated to: Tom Rini
Headers show
Series button: adc: set state to pressed when the voltage is closest to nominal | expand

Commit Message

Peter Cai Feb. 2, 2022, 6:04 p.m. UTC
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 <peter@typeblog.net>
---
 drivers/button/button-adc.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

Comments

Tom Rini Feb. 11, 2022, 5:07 p.m. UTC | #1
On Wed, Feb 02, 2022 at 01:04:04PM -0500, Peter Cai wrote:

> 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 <peter@typeblog.net>

Applied to u-boot/master, thanks!
diff mbox series

Patch

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;
 }