public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Dzmitry Sankouski <dsankouski@gmail.com>
To: u-boot@lists.denx.de
Cc: Dzmitry Sankouski <dsankouski@gmail.com>,
	Simon Glass <sjg@chromium.org>,
	Mark Kettenis <kettenis@openbsd.org>
Subject: [PATCH v3 5/5] dm: input: add button_kbd driver
Date: Sun, 22 Jan 2023 18:21:25 +0300	[thread overview]
Message-ID: <20230122152125.858085-6-dsankouski@gmail.com> (raw)
In-Reply-To: <20230122152125.858085-1-dsankouski@gmail.com>

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.

Signed-off-by: Dzmitry Sankouski <dsankouski@gmail.com>
---
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

diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index 1c534be005..b9a4e443a3 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -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
diff --git a/drivers/input/Makefile b/drivers/input/Makefile
index ded76bddb2..14c0ea7325 100644
--- a/drivers/input/Makefile
+++ b/drivers/input/Makefile
@@ -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
 
diff --git a/drivers/input/button_kbd.c b/drivers/input/button_kbd.c
new file mode 100644
index 0000000000..99e65f12f0
--- /dev/null
+++ b/drivers/input/button_kbd.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
+ */
+
+#include <stdlib.h>
+#include <common.h>
+#include <dm.h>
+#include <fdtdec.h>
+#include <input.h>
+#include <keyboard.h>
+#include <button.h>
+#include <dm/device-internal.h>
+#include <log.h>
+#include <asm/io.h>
+#include <asm/gpio.h>
+#include <linux/delay.h>
+#include <linux/input.h>
+
+/**
+ * struct button_kbd_priv - driver private data
+ *
+ * @input: input configuration
+ * @button_size: number of buttons found
+ * @old_state: a pointer to old button states array. Used to determine button state change.
+ */
+struct button_kbd_priv {
+	struct input_config *input;
+	u32 button_size;
+	u32 *old_state;
+};
+
+static int button_kbd_start(struct udevice *dev)
+{
+	struct button_kbd_priv *priv = dev_get_priv(dev);
+	int i = 0;
+	struct udevice *button_gpio_devp;
+
+	uclass_foreach_dev_probe(UCLASS_BUTTON, button_gpio_devp) {
+		struct button_uc_plat *uc_plat = dev_get_uclass_plat(button_gpio_devp);
+		/* Ignore the top-level button node */
+		if (!uc_plat->label)
+			continue;
+		debug("Found button %s #%d - %s, probing...\n",
+		      uc_plat->label, i, button_gpio_devp->name);
+		i++;
+	}
+
+	priv->button_size = i;
+	priv->old_state = calloc(i, sizeof(int));
+
+	return 0;
+}
+
+int button_read_keys(struct input_config *input)
+{
+	struct button_kbd_priv *priv = dev_get_priv(input->dev);
+	struct udevice *button_gpio_devp;
+	struct uclass *uc;
+	int i = 0;
+	u32 code, state, state_changed = 0;
+
+	uclass_id_foreach_dev(UCLASS_BUTTON, button_gpio_devp, uc) {
+		struct button_uc_plat *uc_plat = dev_get_uclass_plat(button_gpio_devp);
+		/* Ignore the top-level button node */
+		if (!uc_plat->label)
+			continue;
+		code = button_get_code(button_gpio_devp);
+		if (!code)
+			continue;
+
+		state = button_get_state(button_gpio_devp);
+		state_changed = state != priv->old_state[i];
+
+		if (state_changed) {
+			debug("%s: %d\n", uc_plat->label, code);
+			priv->old_state[i] = state;
+			input_add_keycode(input, code, state);
+		}
+		i++;
+	}
+	return 0;
+}
+
+static const struct keyboard_ops button_kbd_ops = {
+	.start	= button_kbd_start,
+};
+
+static int button_kbd_probe(struct udevice *dev)
+{
+	struct button_kbd_priv *priv = dev_get_priv(dev);
+	struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
+	struct stdio_dev *sdev = &uc_priv->sdev;
+	struct input_config *input = &uc_priv->input;
+	int ret = 0;
+
+	input_init(input, false);
+	input_add_tables(input, false);
+
+	/* Register the device. */
+	priv->input = input;
+	input->dev = dev;
+	input->read_keys = button_read_keys;
+	strcpy(sdev->name, "button-kbd");
+	ret = input_stdio_register(sdev);
+	if (ret) {
+		debug("%s: input_stdio_register() failed\n", __func__);
+		return ret;
+	}
+
+	return 0;
+}
+
+static const struct udevice_id button_kbd_ids[] = {
+	{ .compatible = "button-kbd" },
+	{ }
+};
+
+U_BOOT_DRIVER(button_kbd) = {
+	.name		= "button_kbd",
+	.id		= UCLASS_KEYBOARD,
+	.of_match	= button_kbd_ids,
+	.ops		= &button_kbd_ops,
+	.priv_auto	= sizeof(struct button_kbd_priv),
+	.probe		= button_kbd_probe,
+};
-- 
2.30.2


  parent reply	other threads:[~2023-01-22 15:22 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-22 15:21 [PATCH v3 0/5] dm: input: driver for buttons with linux, code declaration Dzmitry Sankouski
2023-01-22 15:21 ` [PATCH v3 1/5] gpio: qcom: add direction functions for pwrkey Dzmitry Sankouski
2023-02-10 18:43   ` Tom Rini
2023-01-22 15:21 ` [PATCH v3 2/5] dts: add missing linux,code in gpio-keys Dzmitry Sankouski
2023-02-10 18:43   ` Tom Rini
2023-01-22 15:21 ` [PATCH v3 3/5] test: create dedicated fdt node for ofnode_for_each_prop test Dzmitry Sankouski
2023-01-23 18:42   ` Simon Glass
2023-02-10 18:43   ` Tom Rini
2023-01-22 15:21 ` [PATCH v3 4/5] dm: button: add support for linux_code in button-gpio.c driver Dzmitry Sankouski
2023-01-23 18:42   ` Simon Glass
2023-02-10 18:43   ` Tom Rini
2023-01-22 15:21 ` Dzmitry Sankouski [this message]
2023-01-23 18:42   ` [PATCH v3 5/5] dm: input: add button_kbd driver Simon Glass
2023-02-10 18:43   ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230122152125.858085-6-dsankouski@gmail.com \
    --to=dsankouski@gmail.com \
    --cc=kettenis@openbsd.org \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox