Skip to contentSkip to conversation

VIM3: add support for checking 'Function' button state

PATCH Superseded Marek Szyprowski 4 participants 10 messages Feb 11, 2021 – Feb 18, 2021 1 message not in the archive Arm/Amlogic Meson SoC support

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20210211084804eucas1p111b64b0251fd991f2aa6f2470341634a@eucas1p1.samsung.com

#1 lore raw

v6 cover VIM3: add support for checking 'Function' button state

Hi All,

This patchset adds all building blocks needed for checking the 'Function' button state in the boot script on Amlogic A311D based VIM3 board. This button is connected to the ADC line of the SoC, so it required to enable meson SARADC, the clocks needed for it and a simple button-adc drivers.

Once applied, one can use following commands in the boot scripts:
-->8---
echo Checking Func button state: \\c
if button Function
then

	echo Selected alternative boot
	...

fi
--->8---

Best regards
Marek Szyprowski
Samsung R&D Institute Poland

Changelog:
v6:
- added a simple sandbox test for adc-keys
- use of_to_plat and adc_raw_to_uV to simplify code in the adc button driver

v5: https://lists.denx.de/pipermail/u-boot/2021-January/438751.html
- rebased onto latest uboot-amlogic/u-boot-amlogic-next branch
- synchronized adc-keys binding with the recent version from the Linux kernel
- updated adc-keys driver to match behavior from dt-bindings
- added a patch for meson-saradc driver to register vdd reference supply to the ADC framework

v4: https://lists.denx.de/pipermail/u-boot/2020-December/435641.html
- rebased onto uboot-amlogic/u-boot-amlogic-next and dropped merged patches
- added adc-keys bindings docs (copied from Linux kernel)
- minor code adjustments pointed by Simon
- enabled driver also in khadas-vim3l_defconfig

v3: https://lists.denx.de/pipermail/u-boot/2020-December/435072.html
- removed 'button' env variable
- extended kconfig and patch descriptions

v2: https://lists.denx.de/pipermail/u-boot/2020-December/434991.html
- removed Change-Id tags
- split defconfig changes into ADC and button related

v1: https://lists.denx.de/pipermail/u-boot/2020-December/434875.html
- initial submission

Patch summary:

Marek Szyprowski (5):
dt-bindings: input: adc-keys bindings documentation
button: add a simple Analog to Digital Converter device based button
driver
adc: meson-saradc: add support for getting reference voltage value
configs: khadas-vim3(l): enable Function button support
test: add a simple test for the adc-keys button driver

 arch/sandbox/dts/test.dts                   |  24 +++-
 configs/khadas-vim3_defconfig               |   2 +
 configs/khadas-vim3l_defconfig              |   2 +
 configs/sandbox_defconfig                   |   1 +
 doc/device-tree-bindings/input/adc-keys.txt |  67 +++++++++
 drivers/adc/meson-saradc.c                  |  21 +++
 drivers/button/Kconfig                      |   8 ++
 drivers/button/Makefile                     |   1 +
 drivers/button/button-adc.c                 | 146 ++++++++++++++++++++
 test/dm/button.c                            |  50 ++++++-
 10 files changed, 319 insertions(+), 3 deletions(-)
 create mode 100644 doc/device-tree-bindings/input/adc-keys.txt
 create mode 100644 drivers/button/button-adc.c
Signature · 1 line
2.17.1
#2 lore raw

PATCH v6 1/5 dt-bindings: input: adc-keys bindings documentation

Dump adc-keys bindings documentation from Linux kernel source tree from commit 698dc0cf9447 ("dt-bindings: input: adc-keys: clarify description").

---
 doc/device-tree-bindings/input/adc-keys.txt | 67 +++++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 doc/device-tree-bindings/input/adc-keys.txt
doc/device-tree-bindings/input/adc-keys.txt new file +67 -0
new file mode 100644
index 0000000000..6c8be6a9ac
@@ -0,0 +1,67 @@
+ADC attached resistor ladder buttons
+------------------------------------
+
+Required properties:
+ - compatible: "adc-keys"
+ - io-channels: Phandle to an ADC channel
+ - io-channel-names = "buttons";
+ - keyup-threshold-microvolt: Voltage above or equal to which all the keys are
+			      considered up.
+
+Optional properties:
+	- poll-interval: Poll interval time in milliseconds
+	- autorepeat: Boolean, Enable auto repeat feature of Linux input
+	  subsystem.
+
+Each button (key) is represented as a sub-node of "adc-keys":
+
+Required subnode-properties:
+	- label: Descriptive name of the key.
+	- linux,code: Keycode to emit.
+	- press-threshold-microvolt: voltage above or equal to which this key is
+				     considered pressed.
+
+No two values of press-threshold-microvolt may be the same.
+All values of press-threshold-microvolt must be less than
+keyup-threshold-microvolt.
+
+Example:
+
+#include <dt-bindings/input/input.h>
+
+	adc-keys {
+		compatible = "adc-keys";
+		io-channels = <&lradc 0>;
+		io-channel-names = "buttons";
+		keyup-threshold-microvolt = <2000000>;
+
+		button-up {
+			label = "Volume Up";
+			linux,code = <KEY_VOLUMEUP>;
+			press-threshold-microvolt = <1500000>;
+		};
+
+		button-down {
+			label = "Volume Down";
+			linux,code = <KEY_VOLUMEDOWN>;
+			press-threshold-microvolt = <1000000>;
+		};
+
+		button-enter {
+			label = "Enter";
+			linux,code = <KEY_ENTER>;
+			press-threshold-microvolt = <500000>;
+		};
+	};
+
++--------------------------------+------------------------+
+| 2.000.000 <= value             | no key pressed         |
++--------------------------------+------------------------+
+| 1.500.000 <= value < 2.000.000 | KEY_VOLUMEUP pressed   |
++--------------------------------+------------------------+
+| 1.000.000 <= value < 1.500.000 | KEY_VOLUMEDOWN pressed |
++--------------------------------+------------------------+
+|   500.000 <= value < 1.000.000 | KEY_ENTER pressed      |
++--------------------------------+------------------------+
+|              value <   500.000 | no key pressed         |
++--------------------------------+------------------------+
Signature · 1 line
2.17.1
#3 lore raw

PATCH v6 2/5 button: add a simple Analog to Digital Converter device based button driver

In reply to Marek Szyprowski

Add a simple Analog to Digital Converter device based button driver. This driver binds to the 'adc-keys' device tree node.

---
 drivers/button/Kconfig      |   8 ++
 drivers/button/Makefile     |   1 +
 drivers/button/button-adc.c | 146 ++++++++++++++++++++++++++++++++++++
 3 files changed, 155 insertions(+)
 create mode 100644 drivers/button/button-adc.c
drivers/button/Kconfig +8 -0
index 6b3ec7e55d..6db3c5e93a 100644
@@ -9,6 +9,14 @@ config BUTTON
 	  can provide access to board-specific buttons. Use of the device tree
 	  for configuration is encouraged.
 
+config BUTTON_ADC
+	bool "Button adc"
+	depends on BUTTON
+	help
+	  Enable support for buttons which are connected to Analog to Digital
+	  Converter device. The ADC driver must use driver model. Buttons are
+	  configured using the device tree.
+
 config BUTTON_GPIO
 	bool "Button gpio"
 	depends on BUTTON
drivers/button/Makefile +1 -0
index fcc10ebe8d..bbd18af149 100644
@@ -3,4 +3,5 @@
 # Copyright (C) 2020 Philippe Reynes <[email protected]>
 
 obj-$(CONFIG_BUTTON) += button-uclass.o
+obj-$(CONFIG_BUTTON_ADC) += button-adc.o
 obj-$(CONFIG_BUTTON_GPIO) += button-gpio.o
drivers/button/button-adc.c new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

PATCH v6 2/5 button: add a simple Analog to Digital Converter device based button driver

Quoted 11 lines · Marek Szyprowski

Add a simple Analog to Digital Converter device based button driver. This driver binds to the 'adc-keys' device tree node.

---
 drivers/button/Kconfig      |   8 ++
 drivers/button/Makefile     |   1 +
 drivers/button/button-adc.c | 146 ++++++++++++++++++++++++++++++++++++
 3 files changed, 155 insertions(+)
 create mode 100644 drivers/button/button-adc.c
#5 lore raw

PATCH v6 3/5 adc: meson-saradc: add support for getting reference voltage value

In reply to Marek Szyprowski

Add support for getting the 'vref-supply' regulator and register it as ADC's reference voltage regulator, so clients can translate sampled ADC values to the voltage.

---
 drivers/adc/meson-saradc.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/adc/meson-saradc.c +21 -0
index 21db55831d..1a45a3a265 100644
@@ -18,6 +18,7 @@
 #include <linux/delay.h>
 #include <linux/math64.h>
 #include <linux/bitfield.h>
+#include <power/regulator.h>
 
 #define MESON_SAR_ADC_REG0					0x00
 	#define MESON_SAR_ADC_REG0_PANEL_DETECT			BIT(31)
@@ -656,7 +657,10 @@ static int meson_saradc_stop(struct udevice *dev)
 
 static int meson_saradc_probe(struct udevice *dev)
 {
+	struct adc_uclass_plat *uc_pdata = dev_get_uclass_plat(dev);
 	struct meson_saradc_priv *priv = dev_get_priv(dev);
+	struct udevice *vref;
+	int vref_uv;
 	int ret;
 
 	ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
@@ -675,6 +679,23 @@ static int meson_saradc_probe(struct udevice *dev)
 
 	priv->active_channel = -1;
 
+	ret = device_get_supply_regulator(dev, "vref-supply", &vref);
+	if (ret) {
+		printf("can't get vref-supply: %d\n", ret);
+		return ret;
+	}
+
+	vref_uv = regulator_get_value(vref);
+	if (vref_uv < 0) {
+		printf("can't get vref-supply value: %d\n", vref_uv);
+		return vref_uv;
+	}
+
+	/* VDD supplied by common vref pin */
+	uc_pdata->vdd_supply = vref;
+	uc_pdata->vdd_microvolts = vref_uv;
+	uc_pdata->vss_microvolts = 0;
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#6 lore raw

PATCH v6 4/5 configs: khadas-vim3(l): enable Function button support

In reply to Marek Szyprowski

Add options required to check the 'Function' button state.

---
 configs/khadas-vim3_defconfig  | 2 ++
 configs/khadas-vim3l_defconfig | 2 ++
 2 files changed, 4 insertions(+)
configs/khadas-vim3_defconfig +2 -0
index 5d16652fd6..bc17430569 100644
@@ -31,6 +31,8 @@ CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_NET_RANDOM_ETHADDR=y
 CONFIG_ADC=y
 CONFIG_SARADC_MESON=y
+CONFIG_BUTTON=y
+CONFIG_BUTTON_ADC=y
 CONFIG_DM_I2C=y
 CONFIG_SYS_I2C_MESON=y
 CONFIG_DM_MMC=y
configs/khadas-vim3l_defconfig +2 -0
index 6b13ce045c..c1877922c7 100644
@@ -31,6 +31,8 @@ CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_NET_RANDOM_ETHADDR=y
 CONFIG_ADC=y
 CONFIG_SARADC_MESON=y
+CONFIG_BUTTON=y
+CONFIG_BUTTON_ADC=y
 CONFIG_DM_I2C=y
 CONFIG_SYS_I2C_MESON=y
 CONFIG_DM_MMC=y
Signature · 1 line
2.17.1
#7 lore raw

PATCH v6 5/5 test: add a simple test for the adc-keys button driver

In reply to Marek Szyprowski

Add adc-keys device to the sandbox/test.dts and connect it to the channel #3 of the sandbox_adc driver. The default values sampled by sandbox_adc driver determines that button3 and button4 are released and button5 is pressed.

---
 arch/sandbox/dts/test.dts | 24 ++++++++++++++++++-
 configs/sandbox_defconfig |  1 +
 test/dm/button.c          | 50 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 72 insertions(+), 3 deletions(-)
arch/sandbox/dts/test.dts +23 -1
index e95f4631bf..a32b019ae7 100644
@@ -69,6 +69,27 @@
 		};
 	};
 
+	buttons2 {
+		compatible = "adc-keys";
+		io-channels = <&adc 3>;
+		keyup-threshold-microvolt = <3000000>;
+
+		button-up {
+			label = "button3";
+			press-threshold-microvolt = <1500000>;
+		};
+
+		button-down {
+			label = "button4";
+			press-threshold-microvolt = <1000000>;
+		};
+
+		button-enter {
+			label = "button5";
+			press-threshold-microvolt = <500000>;
+		};
+	};
+
 	cros_ec: cros-ec {
 		reg = <0 0>;
 		compatible = "google,cros-ec-sandbox";
@@ -587,8 +608,9 @@
 		i2c-eeprom = <&bootcount_i2c>;
 	};
 
-	adc at 0 {
+	adc: adc at 0 {
 		compatible = "sandbox,adc";
+		#io-channel-cells = <1>;
 		vdd-supply = <&buck2>;
 		vss-microvolts = <0>;
 	};
configs/sandbox_defconfig +1 -0
index 0c7674efc9..3731bf05ff 100644
@@ -122,6 +122,7 @@ CONFIG_DM_BOOTCOUNT=y
 CONFIG_DM_BOOTCOUNT_RTC=y
 CONFIG_DM_BOOTCOUNT_I2C_EEPROM=y
 CONFIG_BUTTON=y
+CONFIG_BUTTON_ADC=y
 CONFIG_BUTTON_GPIO=y
 CONFIG_CLK=y
 CONFIG_CLK_COMPOSITE_CCF=y
test/dm/button.c +48 -2

69 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

PATCH v6 5/5 test: add a simple test for the adc-keys button driver

Quoted 18 lines · Marek Szyprowski

Add adc-keys device to the sandbox/test.dts and connect it to the channel #3 of the sandbox_adc driver. The default values sampled by sandbox_adc driver determines that button3 and button4 are released and button5 is pressed.

---
 arch/sandbox/dts/test.dts | 24 ++++++++++++++++++-
 configs/sandbox_defconfig |  1 +
 test/dm/button.c          | 50 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 72 insertions(+), 3 deletions(-)
arch/sandbox/dts/test.dts +0 -0
index e95f4631bf..a32b019ae7 100644
@@ -69,6 +69,27 @@
 		};

arch/sandbox/dts/test.dts line 79

Quoted 10 lines
arch/sandbox/dts/test.dts +8 -0
 	};
 
+	buttons2 {
+		compatible = "adc-keys";
+		io-channels = <&adc 3>;
+		keyup-threshold-microvolt = <3000000>;
+
+		button-up {
+			label = "button3";
+			press-threshold-microvolt = <1500000>;

In patch 1/5 you wrote that "linux,code" is a required property.

Even if you don't use the property currently, I suggest to keep test.dts compliant.

Best regards

Heinrich

Quoted 116 lines
arch/sandbox/dts/test.dts +15 -1
+		};
+
+		button-down {
+			label = "button4";
+			press-threshold-microvolt = <1000000>;
+		};
+
+		button-enter {
+			label = "button5";
+			press-threshold-microvolt = <500000>;
+		};
+	};
+
 	cros_ec: cros-ec {
 		reg = <0 0>;
 		compatible = "google,cros-ec-sandbox";
@@ -587,8 +608,9 @@
 		i2c-eeprom = <&bootcount_i2c>;
 	};
 
-	adc at 0 {
+	adc: adc at 0 {
 		compatible = "sandbox,adc";
+		#io-channel-cells = <1>;
 		vdd-supply = <&buck2>;
 		vss-microvolts = <0>;
 	};
configs/sandbox_defconfig +1 -0
index 0c7674efc9..3731bf05ff 100644
@@ -122,6 +122,7 @@ CONFIG_DM_BOOTCOUNT=y
 CONFIG_DM_BOOTCOUNT_RTC=y
 CONFIG_DM_BOOTCOUNT_I2C_EEPROM=y
 CONFIG_BUTTON=y
+CONFIG_BUTTON_ADC=y
 CONFIG_BUTTON_GPIO=y
 CONFIG_CLK=y
 CONFIG_CLK_COMPOSITE_CCF=y
test/dm/button.c +48 -2
index ecaa47cf5f..f8a7fab61d 100644
@@ -7,7 +7,10 @@
 
 #include <common.h>
 #include <dm.h>
+#include <adc.h>
 #include <button.h>
+#include <power/regulator.h>
+#include <power/sandbox_pmic.h>
 #include <asm/gpio.h>
 #include <dm/test.h>
 #include <test/ut.h>
@@ -17,11 +20,20 @@ static int dm_test_button_base(struct unit_test_state *uts)
 {
 	struct udevice *dev;
 
-	/* Get the top-level device */
+	/* Get the top-level gpio buttons device */
 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev));
+	/* Get the 2 gpio buttons */
 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev));
-	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_BUTTON, 3, &dev));
+
+	/* Get the top-level adc buttons device */
+	ut_assertok(uclass_get_device(UCLASS_BUTTON, 3, &dev));
+	/* Get the 3 adc buttons */
+	ut_assertok(uclass_get_device(UCLASS_BUTTON, 4, &dev));
+	ut_assertok(uclass_get_device(UCLASS_BUTTON, 5, &dev));
+	ut_assertok(uclass_get_device(UCLASS_BUTTON, 6, &dev));
+
+	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_BUTTON, 7, &dev));
 
 	return 0;
 }
@@ -72,3 +84,37 @@ static int dm_test_button_label(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/* Test adc-keys driver */
+static int dm_test_button_keys_adc(struct unit_test_state *uts)
+{
+	struct udevice *supply;
+	struct udevice *dev;
+	int uV;
+
+	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc at 0", &dev));
+
+	ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
+	ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
+	ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
+	/* Update ADC plat and get new Vdd value */
+	ut_assertok(adc_vdd_value(dev, &uV));
+	ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
+
+	/*
+	 * sandbox-adc returns constant value on channel 3, is used by adc-keys:
+	 * SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_SET_UV / SANDBOX_ADC_DATA_MASK =
+	 * 0x3000 * 3300000 / 0xffff = 618759uV
+	 * This means that button3 and button4 are released and button5
+	 * is pressed.
+	 */
+	ut_assertok(button_get_by_label("button3", &dev));
+	ut_asserteq(BUTTON_OFF, button_get_state(dev));
+	ut_assertok(button_get_by_label("button4", &dev));
+	ut_asserteq(BUTTON_OFF, button_get_state(dev));
+	ut_assertok(button_get_by_label("button5", &dev));
+	ut_asserteq(BUTTON_ON, button_get_state(dev));
+
+	return 0;
+}
+DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
#9 lore raw

PATCH v6 5/5 test: add a simple test for the adc-keys button driver

In reply to Marek Szyprowski
Quoted 6 lines · Marek Szyprowski

Add adc-keys device to the sandbox/test.dts and connect it to the channel #3 of the sandbox_adc driver. The default values sampled by sandbox_adc driver determines that button3 and button4 are released and button5 is pressed.

Quoted 7 lines
---
 arch/sandbox/dts/test.dts | 24 ++++++++++++++++++-
 configs/sandbox_defconfig |  1 +
 test/dm/button.c          | 50 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 72 insertions(+), 3 deletions(-)
#10 lore raw

PATCH v6 5/5 test: add a simple test for the adc-keys button driver

In reply to Marek Szyprowski

Hi Marek,

Quoted 5 lines · Marek Szyprowski

Add adc-keys device to the sandbox/test.dts and connect it to the channel #3 of the sandbox_adc driver. The default values sampled by sandbox_adc driver determines that button3 and button4 are released and button5 is pressed.

Quoted 7 lines
---
 arch/sandbox/dts/test.dts | 24 ++++++++++++++++++-
 configs/sandbox_defconfig |  1 +
 test/dm/button.c          | 50 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 72 insertions(+), 3 deletions(-)

[...]

Everything is reviewed, can you re-send with the missing "linux,code" ? and I'll push it.

Neil