Skip to contentSkip to conversation

button: adc: implement get_code()

PATCH Superseded Petr Hodina 2 participants 2 messages Oct 10 – Oct 12

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 button: adc: implement get_code()

Implement .get_code in button_adc_ops and add linux_code to button_adc_priv.

---
The keys on PinePhone Pro are not registered as Volume Up and Down as the keys are not wired to normal GPIO but to ADC and use different resistors to tell them apart.

However, button_adc_ops never sets .get_code unlike button-gpio. As a result button_get_code() returns -ENOSYS for ADC buttons.

The patch reads linux,code and returns it from .get_code mirroring button-gpio.

---
 drivers/button/button-adc.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/button/button-adc.c +15 -0
index da7ddf2a857..681f3f7c09a 100644
@@ -20,12 +20,14 @@
  * @channel: channel of the ADC device to probe the button state.
  * @min: minimal uV value to consider button as pressed.
  * @max: maximal uV value to consider button as pressed.
+ * @linux_code: linux,code devicetree property for this button, or 0 if unset.
  */
 struct button_adc_priv {
 	struct udevice *adc;
 	int channel;
 	int min;
 	int max;
+	int linux_code;
 };
 
 static enum button_state_t button_adc_get_state(struct udevice *dev)
@@ -49,6 +51,16 @@ static enum button_state_t button_adc_get_state(struct udevice *dev)
 	return (uV >= priv->min && uV < priv->max) ? BUTTON_ON : BUTTON_OFF;
 }
 
+static int button_adc_get_code(struct udevice *dev)
+{
+	struct button_adc_priv *priv = dev_get_priv(dev);
+
+	if (!priv->linux_code)
+		return -ENODATA;
+
+	return priv->linux_code;
+}
+
 static int button_adc_of_to_plat(struct udevice *dev)
 {
 	struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
@@ -93,6 +105,8 @@ static int button_adc_of_to_plat(struct udevice *dev)
 	}
 
 	priv->channel = args.args[0];
+	priv->linux_code = ofnode_read_u32_default(dev_ofnode(dev),
+						   "linux,code", 0);
 
 	/*
 	 * Define the voltage range such that the button is only pressed
@@ -138,6 +152,7 @@ static int button_adc_bind(struct udevice *parent)
 
 static const struct button_ops button_adc_ops = {
 	.get_state	= button_adc_get_state,
+	.get_code	= button_adc_get_code,
 };
 
 static const struct udevice_id button_adc_ids[] = {

---
base-commit: ece349ade2973e220f524ce59e59711cc919263f
change-id: 20261010-b4-button-adc-get-code-056f3d455daa

Best regards,

Signature · 1 line
Petr Hodina <[email protected]>
#1 lore raw

Hi Petr,

Quoted 4 lines · On 2026-10-10T12:42:50, Petr Hodina via B4 Relay…

button: adc: implement get_code()

Implement .get_code in button_adc_ops and add linux_code to button_adc_priv.

Please can you add a sentence on why this is needed, e.g. which caller needs the key code from an adc-keys button (button-kbd, bootmenu, a particular board)?

Quoted 8 lines

drivers/button/button-adc.c | 15 +++++++++++++++

 1 file changed, 15 insertions(+)

drivers/button/button-adc.c static enum button_state_t button_adc_get_state(struct udevice *dev) · line 59

Quoted 9 lines
drivers/button/button-adc.c +9 -0
@@ -49,6 +51,16 @@ static enum button_state_t button_adc_get_state(struct udevice *dev)
+static int button_adc_get_code(struct udevice *dev)
+{
+     struct button_adc_priv *priv = dev_get_priv(dev);
+
+     if (!priv->linux_code)
+             return -ENODATA;
+
+     return priv->linux_code;
+}

The sandbox test.dts already has linux,code values (KEY_F3/F4/F5) on the adc-keys buttons. Please can you extend dm_test_button_keys_adc() in test/dm/button.c to check button_get_code() for button3, button4 and button5?

drivers/button/button-adc.c static int button_adc_of_to_plat(struct udevice *dev) · line 106

Quoted 4 lines
drivers/button/button-adc.c +2 -0
@@ -93,6 +105,8 @@ static int button_adc_of_to_plat(struct udevice *dev)
+     priv->linux_code = ofnode_read_u32_default(dev_ofnode(dev),
+                                                "linux,code", 0);

You can use dev_read_u32_default(dev, "linux,code", 0) here, which fits on one line and matches the dev_read_u32() call in button-gpio. Treating the property as optional is fine, even though the Linux adc-keys binding requires it.

Regards,
Simon