Skip to contentSkip to conversation

dm: input: driver for buttons with linux, code declaration

PATCH In review Dzmitry Sankouski 3 participants 14 messages Jan 22, 2023 – Feb 10, 2023

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 cover dm: input: driver for buttons with linux, code declaration

Bootmenu requires an input device with arrows and enter key. A common smartphone luckily has power, volume up/down buttons, which may be used for controlling bootmenu.

Button keyboard driver relies on button driver - iterates over all button with linux,code, checks state and sends events. Add support for linux,code in button driver.
Fix qcom pwr-key gpio driver to work with button driver.

Dzmitry Sankouski (5):
gpio: qcom: add direction functions for pwrkey
dts: add missing linux,code in gpio-keys
test: create dedicated fdt node for ofnode_for_each_prop test
dm: button: add support for linux_code in button-gpio.c driver
dm: input: add button_kbd driver

 arch/arm/dts/am3517-evm-ui.dtsi               |   2 +-
 .../dts/imx6ul-phytec-segin-peb-eval-01.dtsi  |   2 +-
 arch/arm/dts/rk3288-popmetal.dtsi             |   2 +
 arch/arm/dts/rk3288-tinker.dtsi               |   2 +
 arch/sandbox/dts/sandbox.dtsi                 |   4 +
 arch/sandbox/dts/test.dts                     |  16 +++
 drivers/button/button-gpio.c                  |  17 ++-
 drivers/button/button-uclass.c                |  10 ++
 drivers/gpio/qcom_pmic_gpio.c                 |  16 +++
 drivers/input/Kconfig                         |   9 ++
 drivers/input/Makefile                        |   1 +
 drivers/input/button_kbd.c                    | 126 ++++++++++++++++++
 include/button.h                              |  16 +++
 test/dm/button.c                              |  13 ++
 test/dm/ofnode.c                              |   2 +-
 15 files changed, 234 insertions(+), 4 deletions(-)
 create mode 100644 drivers/input/button_kbd.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/5 gpio: qcom: add direction functions for pwrkey

GPIO button driver requires direction functions to probe button gpio. Those functions are blank, since pwrkey is not really gpio, and don't support direction settings.

---
Changes for v2:
- none
Changes for v3:
- KDPWR and RESIN not gpio: fix comment and commit message

 drivers/gpio/qcom_pmic_gpio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/gpio/qcom_pmic_gpio.c +16 -0
index 3be1be8692..65feb453eb 100644
@@ -303,9 +303,25 @@ static int qcom_pwrkey_get_value(struct udevice *dev, unsigned offset)
 	}
 }
 
+/*
+ * Since pmic buttons modelled as GPIO, we need empty direction functions
+ * to trick u-boot button driver
+ */
+static int qcom_pwrkey_direction_input(struct udevice *dev, unsigned int offset)
+{
+	return 0;
+}
+
+static int qcom_pwrkey_direction_output(struct udevice *dev, unsigned int offset, int value)
+{
+	return -EOPNOTSUPP;
+}
+
 static const struct dm_gpio_ops qcom_pwrkey_ops = {
 	.get_value		= qcom_pwrkey_get_value,
 	.get_function		= qcom_pwrkey_get_function,
+	.direction_input	= qcom_pwrkey_direction_input,
+	.direction_output	= qcom_pwrkey_direction_output,
 };
 
 static int qcom_pwrkey_probe(struct udevice *dev)
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v3 1/5] gpio: qcom: add direction functions for pwrkey

Quoted 6 lines · Dzmitry Sankouski

GPIO button driver requires direction functions to probe button gpio. Those functions are blank, since pwrkey is not really gpio, and don't support direction settings.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#3 lore raw

PATCH v3 2/5 dts: add missing linux,code in gpio-keys

In reply to Dzmitry Sankouski

gpio-keys linux driver enforces user to specify linux,code. Add missing linux,code before implementing button input support.

- arch/arm/dts/rk3288-popmetal.dtsi -> KEY_POWER
- arch/arm/dts/rk3288-tinker.dtsi -> KEY_POWER
- arch/arm/dts/am3517-evm-ui.dtsi -> KEY_RECORD
- sandbox/dts/sandbox.dtsi -> BTN_1
- sandbox/dts/sandbox.dts -> BTN_1

---
Changes for v2:
- N/A
Changes for v3:
- import input.h in dts to provide event constants

 arch/arm/dts/am3517-evm-ui.dtsi                   | 2 +-
 arch/arm/dts/imx6ul-phytec-segin-peb-eval-01.dtsi | 2 +-
 arch/arm/dts/rk3288-popmetal.dtsi                 | 2 ++
 arch/arm/dts/rk3288-tinker.dtsi                   | 2 ++
 arch/sandbox/dts/sandbox.dtsi                     | 4 ++++
 5 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/dts/am3517-evm-ui.dtsi +1 -1
index 7d8f32bf70..340e68178c 100644
@@ -72,7 +72,7 @@
 
 		record {
 			label = "Record";
-			/* linux,code = <BTN_0>; */
+			linux,code = <KEY_RECORD>;
 			gpios = <&tca6416_2 15 GPIO_ACTIVE_LOW>;
 		};
 
arch/arm/dts/imx6ul-phytec-segin-peb-eval-01.dtsi +1 -1
index 2f3fd32a11..5f760ed698 100644
@@ -8,7 +8,7 @@
 
 / {
 	gpio_keys: gpio-keys {
-		compatible = "gpio-key";
+		compatible = "gpio-keys";
 		pinctrl-names = "default";
 		pinctrl-0 = <&pinctrl_gpio_keys>;
 		status = "disabled";
arch/arm/dts/rk3288-popmetal.dtsi +2 -0
index 63785eb55e..0253933a11 100644
@@ -38,6 +38,7 @@
  *     OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <dt-bindings/input/input.h>
 #include "rk3288.dtsi"
 
 / {
@@ -63,6 +64,7 @@
 		power {
 			gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
 			label = "GPIO Key Power";
+			linux,code = <KEY_POWER>;
 			linux,input-type = <1>;
 			wakeup-source;
 			debounce-interval = <100>;
arch/arm/dts/rk3288-tinker.dtsi +2 -0
index 2f816af47f..46460ae455 100644
@@ -38,6 +38,7 @@
  *     OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <dt-bindings/input/input.h>
 #include "rk3288.dtsi"
 
 / {
@@ -63,6 +64,7 @@
 		button@0 {
 			gpios = <&gpio0 5 GPIO_ACTIVE_LOW>;
 			label = "GPIO Key Power";
+			linux,code = <KEY_POWER>;
 			linux,input-type = <1>;
 			gpio-key,wakeup = <1>;
 			debounce-interval = <100>;
arch/sandbox/dts/sandbox.dtsi +4 -0
index afe598a4f5..c2c71505ce 100644
@@ -4,6 +4,8 @@
  * and sandbox64 builds.
  */
 
+#include <dt-bindings/input/input.h>
+
 #define USB_CLASS_HUB			9
 
 / {
@@ -49,11 +51,13 @@
 		btn1 {
 			gpios = <&gpio_a 3 0>;
 			label = "button1";
+		    linux,code = <BTN_1>;
 		};
 
 		btn2 {
 			gpios = <&gpio_a 4 0>;
 			label = "button2";
+		    linux,code = <BTN_2>;
 		};
 	};
 
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v3 2/5] dts: add missing linux,code in gpio-keys

Quoted 11 lines · Dzmitry Sankouski

gpio-keys linux driver enforces user to specify linux,code. Add missing linux,code before implementing button input support.

- arch/arm/dts/rk3288-popmetal.dtsi -> KEY_POWER
- arch/arm/dts/rk3288-tinker.dtsi -> KEY_POWER
- arch/arm/dts/am3517-evm-ui.dtsi -> KEY_RECORD
- sandbox/dts/sandbox.dtsi -> BTN_1
- sandbox/dts/sandbox.dts -> BTN_1

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#5 lore raw

PATCH v3 3/5 test: create dedicated fdt node for ofnode_for_each_prop test

In reply to Dzmitry Sankouski

Property count may change in /buttons node, if more button tests added, and this will break ofnode_for_each_prop.
Add separate node for mentioned test.

---
Changes for v2: N/A
Changes for v3: N/A

 arch/sandbox/dts/test.dts | 14 ++++++++++++++
 test/dm/ofnode.c          |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)
arch/sandbox/dts/test.dts +14 -0
index 2e580f980f..06ad027638 100644
@@ -1517,6 +1517,20 @@
 		};
 	};
 
+	ofnode-foreach {
+		compatible = "foreach";
+
+		first {
+			prop1 = <1>;
+			prop2 = <2>;
+		};
+
+		second {
+			prop1 = <1>;
+			prop2 = <2>;
+		};
+	};
+
 	osd {
 		compatible = "sandbox,sandbox_osd";
 	};
test/dm/ofnode.c +1 -1
index 8077affabb..473a8cef57 100644
@@ -1046,7 +1046,7 @@ static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
 	struct ofprop prop;
 	int count;
 
-	node = ofnode_path("/buttons");
+	node = ofnode_path("/ofnode-foreach");
 	count = 0;
 
 	/* we expect "compatible" for each node */
Signature · 1 line
2.30.2
Reviewed-by on v3 3/5
#6 lore raw

Re: [PATCH v3 3/5] test: create dedicated fdt node for ofnode_for_each_prop test

Quoted 5 lines · Dzmitry Sankouski

Property count may change in /buttons node, if more button tests added, and this will break ofnode_for_each_prop.
Add separate node for mentioned test.

Quoted 9 lines

---
Changes for v2: N/A
Changes for v3: N/A

 arch/sandbox/dts/test.dts | 14 ++++++++++++++
 test/dm/ofnode.c          |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)
#8 lore raw

PATCH v3 4/5 dm: button: add support for linux_code in button-gpio.c driver

In reply to Dzmitry Sankouski

Linux event code must be used in input devices, using buttons.

---
Changes for v2:
- fail, if linux,code not found
Changes for v3:
- add test for linux,code
- change linux,code type to int
- new line after return
- add specific error code in function docs

 arch/sandbox/dts/test.dts      |  2 ++
 drivers/button/button-gpio.c   | 17 ++++++++++++++++-
 drivers/button/button-uclass.c | 10 ++++++++++
 include/button.h               | 16 ++++++++++++++++
 test/dm/button.c               | 13 +++++++++++++
 5 files changed, 57 insertions(+), 1 deletion(-)
arch/sandbox/dts/test.dts +2 -0
index 06ad027638..f8d59f1ce1 100644
@@ -172,11 +172,13 @@
 		btn1 {
 			gpios = <&gpio_a 3 0>;
 			label = "button1";
+			linux,code = <BTN_1>;
 		};
 
 		btn2 {
 			gpios = <&gpio_a 4 0>;
 			label = "button2";
+			linux,code = <BTN_2>;
 		};
 	};
 
drivers/button/button-gpio.c +16 -1
index dbb000622c..7b5b3affe2 100644
@@ -13,6 +13,7 @@
 
 struct button_gpio_priv {
 	struct gpio_desc gpio;
+	int linux_code;
 };
 
 static enum button_state_t button_gpio_get_state(struct udevice *dev)
@@ -29,6 +30,17 @@ static enum button_state_t button_gpio_get_state(struct udevice *dev)
 	return ret ? BUTTON_ON : BUTTON_OFF;
 }
 
+static int button_gpio_get_code(struct udevice *dev)
+{
+	struct button_gpio_priv *priv = dev_get_priv(dev);
+	int code = priv->linux_code;
+
+	if (!code)
+		return -ENODATA;
+
+	return code;
+}
+
 static int button_gpio_probe(struct udevice *dev)
 {
 	struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
@@ -43,7 +55,9 @@ static int button_gpio_probe(struct udevice *dev)
 	if (ret)
 		return ret;
 
-	return 0;
+	ret = dev_read_u32(dev, "linux,code", &priv->linux_code);
+
+	return ret;
 }
 
 static int button_gpio_remove(struct udevice *dev)
@@ -92,6 +106,7 @@ static int button_gpio_bind(struct udevice *parent)
 
 static const struct button_ops button_gpio_ops = {
 	.get_state	= button_gpio_get_state,
+	.get_code	= button_gpio_get_code,
 };
 
 static const struct udevice_id button_gpio_ids[] = {
drivers/button/button-uclass.c +10 -0
index e33ed7d01d..032191d61a 100644
@@ -38,6 +38,16 @@ enum button_state_t button_get_state(struct udevice *dev)
 	return ops->get_state(dev);
 }
 
+int button_get_code(struct udevice *dev)
+{
+	struct button_ops *ops = button_get_ops(dev);
+
+	if (!ops->get_code)
+		return -ENOSYS;
+
+	return ops->get_code(dev);
+}
+
 UCLASS_DRIVER(button) = {
 	.id		= UCLASS_BUTTON,
 	.name		= "button",
include/button.h +16 -0
index 96e6b1901f..207f4a0f4d 100644
@@ -37,6 +37,14 @@ struct button_ops {
 	 * @return button state button_state_t, or -ve on error
 	 */
 	enum button_state_t (*get_state)(struct udevice *dev);
+
+	/**
+	 * get_code() - get linux event code of a button
+	 *
+	 * @dev:	button device to change
+	 * @return button code, or -ENODATA on error
+	 */
+	int (*get_code)(struct udevice *dev);
 };
 
 #define button_get_ops(dev)	((struct button_ops *)(dev)->driver->ops)
@@ -58,4 +66,12 @@ int button_get_by_label(const char *label, struct udevice **devp);
  */
 enum button_state_t button_get_state(struct udevice *dev);
 
+/**
+ * button_get_code() - get linux event code of a button
+ *
+ * @dev:	button device to change
+ * @return button code, or -ve on error
+ */
+int button_get_code(struct udevice *dev);
+
 #endif
test/dm/button.c +13 -0
index e76c1ad030..3318668df2 100644
@@ -13,6 +13,7 @@
 #include <power/sandbox_pmic.h>
 #include <asm/gpio.h>
 #include <dm/test.h>
+#include <dt-bindings/input/input.h>
 #include <test/ut.h>
 
 /* Base test of the button uclass */
@@ -85,6 +86,18 @@ static int dm_test_button_label(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
 
+/* Test button has linux,code */
+static int dm_test_button_linux_code(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+
+	ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
+	ut_asserteq(BTN_1, button_get_code(dev));
+
+	return 0;
+}
+DM_TEST(dm_test_button_linux_code, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
 /* Test adc-keys driver */
 static int dm_test_button_keys_adc(struct unit_test_state *uts)
 {
Signature · 1 line
2.30.2
Reviewed-by on v3 4/5
#9 lore raw

Re: [PATCH v3 4/5] dm: button: add support for linux_code in button-gpio.c driver

Quoted 13 lines · Dzmitry Sankouski

Linux event code must be used in input devices, using buttons.

---
Changes for v2:
- fail, if linux,code not found
Changes for v3:
- add test for linux,code
- change linux,code type to int
- new line after return
- add specific error code in function docs

Quoted 6 lines
 arch/sandbox/dts/test.dts      |  2 ++
 drivers/button/button-gpio.c   | 17 ++++++++++++++++-
 drivers/button/button-uclass.c | 10 ++++++++++
 include/button.h               | 16 ++++++++++++++++
 test/dm/button.c               | 13 +++++++++++++
 5 files changed, 57 insertions(+), 1 deletion(-)
#11 lore raw

PATCH v3 5/5 dm: input: add button_kbd driver

In reply to Dzmitry Sankouski

Bootmenu requires an input device with arrows and enter key. A common smartphone luckily has power, volume up/down buttons, which may be used for controlling bootmenu.
To use driver, add 'button-kbd' to stdin.

---
Changes for v2:
- add doc on driver private data struct
- use calloc instead of malloc
Changes for v3:
- none

 drivers/input/Kconfig      |   9 +++
 drivers/input/Makefile     |   1 +
 drivers/input/button_kbd.c | 126 +++++++++++++++++++++++++++++++++++++
 3 files changed, 136 insertions(+)
 create mode 100644 drivers/input/button_kbd.c
drivers/input/Kconfig +9 -0
index 1c534be005..b9a4e443a3 100644
@@ -46,6 +46,15 @@ config APPLE_SPI_KEYB
 	  laptops based on Apple SoCs. These keyboards use an
 	  Apple-specific HID-over-SPI protocol.
 
+config BUTTON_KEYBOARD
+   	bool "Buttons as keyboard"
+   	depends on BUTTON_GPIO
+   	depends on DM_KEYBOARD
+   	help
+   	  Enable support for mapping buttons to keycode events. Use linux,code button driver
+   	  dt node to define button-event mapping.
+   	  For example, an arrows and enter may be implemented to navigate boot menu.
+
 config CROS_EC_KEYB
 	bool "Enable Chrome OS EC keyboard support"
 	depends on INPUT
drivers/input/Makefile +1 -0
index ded76bddb2..14c0ea7325 100644
@@ -6,6 +6,7 @@
 obj-$(CONFIG_$(SPL_TPL_)CROS_EC_KEYB) += cros_ec_keyb.o
 obj-$(CONFIG_$(SPL_TPL_)OF_CONTROL) += key_matrix.o
 obj-$(CONFIG_$(SPL_TPL_)DM_KEYBOARD) += input.o keyboard-uclass.o
+obj-$(CONFIG_BUTTON_KEYBOARD) += button_kbd.o
 
 ifndef CONFIG_SPL_BUILD
 
drivers/input/button_kbd.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.30.2
Reviewed-by on v3 5/5
#12 lore raw

Re: [PATCH v3 5/5] dm: input: add button_kbd driver

Quoted 14 lines · Dzmitry Sankouski

Bootmenu requires an input device with arrows and enter key. A common smartphone luckily has power, volume up/down buttons, which may be used for controlling bootmenu.
To use driver, add 'button-kbd' to stdin.

---
Changes for v2:
- add doc on driver private data struct
- use calloc instead of malloc
Changes for v3:
- none

Quoted 5 lines
 drivers/input/Kconfig      |   9 +++
 drivers/input/Makefile     |   1 +
 drivers/input/button_kbd.c | 126 +++++++++++++++++++++++++++++++++++++
 3 files changed, 136 insertions(+)
 create mode 100644 drivers/input/button_kbd.c
#13 lore raw

Re: [PATCH v3 5/5] dm: input: add button_kbd driver

In reply to Dzmitry Sankouski
Quoted 7 lines · Dzmitry Sankouski

Bootmenu requires an input device with arrows and enter key. A common smartphone luckily has power, volume up/down buttons, which may be used for controlling bootmenu.
To use driver, add 'button-kbd' to stdin.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom