Skip to contentSkip to conversation

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

PATCH Superseded Dzmitry Sankouski 4 participants 9 messages Jan 19, 2023 – Jan 21, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 (4):
gpio: qcom: add direction functions for pwrkey
dts: add missing linux,code in gpio-keys
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             |   1 +
 arch/arm/dts/rk3288-tinker.dtsi               |   1 +
 arch/sandbox/dts/sandbox.dtsi                 |   2 +
 drivers/button/button-gpio.c                  |  16 ++-
 drivers/button/button-uclass.c                |  10 ++
 drivers/gpio/qcom_pmic_gpio.c                 |  15 +++
 drivers/input/Kconfig                         |   9 ++
 drivers/input/Makefile                        |   1 +
 drivers/input/button_kbd.c                    | 126 ++++++++++++++++++
 include/button.h                              |  16 +++
 12 files changed, 198 insertions(+), 3 deletions(-)
 create mode 100644 drivers/input/button_kbd.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/4 gpio: qcom: add direction functions for pwrkey

GPIO button driver requires direction functions to probe button gpio. Those functions are blank, since pwrkey gpio configured earlier not by u-boot.

---
Changes for v2:
- none

 drivers/gpio/qcom_pmic_gpio.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpio/qcom_pmic_gpio.c +15 -0
index 3be1be8692..e46377ce3b 100644
@@ -303,9 +303,24 @@ static int qcom_pwrkey_get_value(struct udevice *dev, unsigned offset)
 	}
 }
 
+/*
+ * Power button already configured as input by previous bootloader.
+ */
+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
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] gpio: qcom: add direction functions for pwrkey

Quoted 5 lines · Dzmitry Sankouski

GPIO button driver requires direction functions to probe button gpio. Those functions are blank, since pwrkey gpio configured earlier not by u-boot.

Quoted 8 lines

---
Changes for v2:
- none

 drivers/gpio/qcom_pmic_gpio.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

-Sumit

Quoted 32 lines
drivers/gpio/qcom_pmic_gpio.c +15 -1
index 3be1be8692..e46377ce3b 100644
@@ -303,9 +303,24 @@ static int qcom_pwrkey_get_value(struct udevice *dev, unsigned offset)
        }
 }
 
+/*
+ * Power button already configured as input by previous bootloader.
+ */
+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)
--

2.30.2

#3 lore raw

Re: [PATCH v2 1/4] gpio: qcom: add direction functions for pwrkey

In reply to Dzmitry Sankouski

Hi!

Quoted 3 lines · Dzmitry Sankouski

GPIO button driver requires direction functions to probe button gpio. Those functions are blank, since pwrkey gpio configured earlier not by u-boot.

...

drivers/gpio/qcom_pmic_gpio.c static int qcom_pwrkey_get_value(struct udevice *dev, unsigned offset) · line 309

Quoted 4 lines
drivers/gpio/qcom_pmic_gpio.c +4 -0
+/*
+ * Power button already configured as input by previous bootloader.
+ */
+static int qcom_pwrkey_direction_input(struct udevice *dev, unsigned int offset)

I think this explanation and comment are a bit misleading. As far as I understand, KDPWR and RESIN on qcom PMICs are not real GPIOs, they are only modeled as such in U-Boot (probably because there were no keyboard class drivers before?). Unlike other GPIOs exposed by the PMIC, which are real GPIOs.

The comment in this file on line 276 says literally this [1]:

/* Add pmic buttons as GPIO as well - there is no generic way for now */

So these functions should be empty not because those were configured by first-stage bootloader, but because these buttons are not really GPIOs and they simply do not support setting directions.

Thanks for working on this! This is really useful, if not you, I'd do this myself. :)

[1] https://elixir.bootlin.com/u-boot/v2023.01/source/drivers/gpio/qcom_pmic_gpio.c#L276

#4 lore raw

PATCH v2 2/4 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

 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                 | 1 +
 arch/arm/dts/rk3288-tinker.dtsi                   | 1 +
 arch/sandbox/dts/sandbox.dtsi                     | 2 ++
 5 files changed, 6 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 +1 -0
index 63785eb55e..cfdfe5e44c 100644
@@ -63,6 +63,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 +1 -0
index 2f816af47f..a7eeb56bf7 100644
@@ -63,6 +63,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 +2 -0
index afe598a4f5..f7f570be6f 100644
@@ -49,11 +49,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
Reviewed-by on v2 2/4
#5 lore raw

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

Quoted 15 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

---
Changes for v2:
- N/A

Quoted 7 lines
 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                 | 1 +
 arch/arm/dts/rk3288-tinker.dtsi                   | 1 +
 arch/sandbox/dts/sandbox.dtsi                     | 2 ++
 5 files changed, 6 insertions(+), 2 deletions(-)
#6 lore raw

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

In reply to Dzmitry Sankouski

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

---
Changes for v2:
- fail, if linux,code not found

 drivers/button/button-gpio.c   | 16 +++++++++++++++-
 drivers/button/button-uclass.c | 10 ++++++++++
 include/button.h               | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
drivers/button/button-gpio.c +15 -1
index dbb000622c..cfed9f17d1 100644
@@ -13,6 +13,7 @@
 
 struct button_gpio_priv {
 	struct gpio_desc gpio;
+	u32 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 u32 button_gpio_get_code(struct udevice *dev)
+{
+	struct button_gpio_priv *priv = dev_get_priv(dev);
+	u32 code = priv->linux_code;
+
+	if (!code)
+		return 0;
+
+	return code;
+}
+
 static int button_gpio_probe(struct udevice *dev)
 {
 	struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
@@ -43,7 +55,8 @@ 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 +105,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..6d0c6f69c5 100644
@@ -38,6 +38,16 @@ enum button_state_t button_get_state(struct udevice *dev)
 	return ops->get_state(dev);
 }
 
+u32 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..27af4a6a1a 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 -ve on error
+	 */
+	u32 (*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
+ */
+u32 button_get_code(struct udevice *dev);
+
 #endif
Signature · 1 line
2.30.2
#7 lore raw

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

Hi Dzmitry,

Quoted 2 lines · Dzmitry Sankouski

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

Do you mean 'must be used' ?

Quoted 24 lines

---
Changes for v2:
- fail, if linux,code not found

 drivers/button/button-gpio.c   | 16 +++++++++++++++-
 drivers/button/button-uclass.c | 10 ++++++++++
 include/button.h               | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
drivers/button/button-gpio.c +1 -0
index dbb000622c..cfed9f17d1 100644
@@ -13,6 +13,7 @@
 
 struct button_gpio_priv {
        struct gpio_desc gpio;
+       u32 linux_code;
 };
 
 static enum button_state_t button_gpio_get_state(struct udevice *dev)

drivers/button/button-gpio.c static enum button_state_t button_gpio_get_state(struct udevice *dev) · line 33

Quoted 4 lines
drivers/button/button-gpio.c +1 -0
@@ -29,6 +30,17 @@ static enum button_state_t button_gpio_get_state(struct udevice *dev)
        return ret ? BUTTON_ON : BUTTON_OFF;
 }
 
+static u32 button_gpio_get_code(struct udevice *dev)

Can you use int instead? There is no need to use smaller types on 64-bit machines, and 'int' is more commonly used for DM functions.

drivers/button/button-gpio.c static enum button_state_t button_gpio_get_state(struct udevice *dev) · line 39

Quoted 6 lines
drivers/button/button-gpio.c +6 -0
+{
+       struct button_gpio_priv *priv = dev_get_priv(dev);
+       u32 code = priv->linux_code;
+
+       if (!code)
+               return 0;

Is that an error? If so, perhaps return -ENODATA or something like that?

drivers/button/button-gpio.c static int button_gpio_probe(struct udevice *dev) · line 58

Quoted 13 lines
drivers/button/button-gpio.c +5 -1
+
+       return code;
+}
+
 static int button_gpio_probe(struct udevice *dev)
 {
        struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
@@ -43,7 +55,8 @@ static int button_gpio_probe(struct udevice *dev)
        if (ret)
                return ret;
 
-       return 0;
+       ret = dev_read_u32(dev, "linux,code", &priv->linux_code);

blank line here

drivers/button/button-gpio.c static int button_gpio_probe(struct udevice *dev) · line 59

Quoted 1 line
drivers/button/button-gpio.c +1 -0
+       return ret;

Hmm, so it will not probe if there is no code?

Quoted 37 lines
drivers/button/button-gpio.c +1 -0
 }
 
 static int button_gpio_remove(struct udevice *dev)
@@ -92,6 +105,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..6d0c6f69c5 100644
@@ -38,6 +38,16 @@ enum button_state_t button_get_state(struct udevice *dev)
        return ops->get_state(dev);
 }
 
+u32 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 +0 -0
index 96e6b1901f..27af4a6a1a 100644

include/button.h struct button_ops { · line 45

Quoted 9 lines
include/button.h +6 -0
@@ -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 -ve on error

Could mention -ENODATA here if that is what you use

include/button.h struct button_ops { · line 47

Quoted 2 lines
include/button.h +2 -0
+        */
+       u32 (*get_code)(struct udevice *dev);

Please can you add a test for this? See test/dm/button.c

Quoted 17 lines
include/button.h +8 -1
 };
 
 #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
+ */
+u32 button_get_code(struct udevice *dev);
+
 #endif
--
Quoted 2 lines

2.30.2

Regards,
Simon

#8 lore raw

PATCH v2 4/4 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

 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