linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Philip Chen <philipchen@chromium.org>
To: LKML <linux-kernel@vger.kernel.org>
Cc: dtor@chromium.org, swboyd@chromium.org, dianders@chromium.org,
	rajatja@chromium.org, Philip Chen <philipchen@chromium.org>,
	Benson Leung <bleung@chromium.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Enric Balletbo i Serra <enric.balletbo@collabora.com>,
	Guenter Roeck <groeck@chromium.org>,
	Lee Jones <lee.jones@linaro.org>,
	linux-input@vger.kernel.org
Subject: [PATCH 2/3] Input: cros_ec_keyb - Support custom top-row keys
Date: Mon, 21 Dec 2020 17:47:58 -0800	[thread overview]
Message-ID: <20201221174751.2.If8dc0ec9d1a60e436d1e852eba1316313f45ac0e@changeid> (raw)
In-Reply-To: <20201221174751.1.I025fb861cd5fa0ef5286b7dce514728e9df7ae74@changeid>

The function keys in a keyboard's top row are usually intended for
certain actions such as "Browser back" and "Fullscreen".

As of now, when a top-row key is pressed, cros_ec_keyb sends function
key code (e.g. KEY_F1) instead of action key code (e.g. KEY_BACK) to
applications. Because `linux,keymap` defined in cros-ec-keyboard.dtsi
maps the scanlines of the top-row keys to the function key code.

Therefore, an application can only convert each function key to
different action based on a fixed mapping.

This patch aims to support a more flexible keyboard top-row design. If
a board specifies a custom layout for the top row keys in dt binding,
cros_ec_keyb will explicitly sends action key code to applications when
any top-row key is pressed, so the applications no longer have to make
assumptions.

Signed-off-by: Philip Chen <philipchen@chromium.org>
---

 drivers/input/keyboard/cros_ec_keyb.c | 71 +++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)

diff --git a/drivers/input/keyboard/cros_ec_keyb.c b/drivers/input/keyboard/cros_ec_keyb.c
index b379ed7628781..c997ec5c5d469 100644
--- a/drivers/input/keyboard/cros_ec_keyb.c
+++ b/drivers/input/keyboard/cros_ec_keyb.c
@@ -27,6 +27,34 @@
 
 #include <asm/unaligned.h>
 
+#define MAX_NUM_TOP_ROW_KEYS   15
+
+/*
+ * Row/column (in the scan matrix) of the function keys (T1-T15)
+ * defined in Chrome OS keyboard spec
+ */
+static const struct key_pos {
+	u8 row;
+	u8 col;
+} top_row_key_pos[] = {
+	{.row = 0, .col = 2},	/* T1 */
+	{.row = 3, .col = 2},	/* T2 */
+	{.row = 2, .col = 2},	/* T3 */
+	{.row = 1, .col = 2},	/* T4 */
+	{.row = 3, .col = 4},	/* T5 */
+	{.row = 2, .col = 4},	/* T6 */
+	{.row = 1, .col = 4},	/* T7 */
+	{.row = 2, .col = 9},	/* T8 */
+	{.row = 1, .col = 9},	/* T9 */
+	{.row = 0, .col = 4},	/* T10 */
+	{.row = 0, .col = 1},	/* T11 */
+	{.row = 1, .col = 5},	/* T12 */
+	{.row = 3, .col = 5},	/* T13 */
+	{.row = 0, .col = 9},	/* T14 */
+	{.row = 0, .col = 11},	/* T15 */
+};
+BUILD_ASSERT(ARRAY_SIZE(top_row_key_pos) == MAX_NUM_TOP_ROW_KEYS);
+
 /**
  * struct cros_ec_keyb - Structure representing EC keyboard device
  *
@@ -42,6 +70,7 @@
  * @idev: The input device for the matrix keys.
  * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
  * @notifier: interrupt event notifier for transport devices
+ * @num_function_row_keys: The number of top row keys in a custom keyboard
  */
 struct cros_ec_keyb {
 	unsigned int rows;
@@ -58,6 +87,8 @@ struct cros_ec_keyb {
 	struct input_dev *idev;
 	struct input_dev *bs_idev;
 	struct notifier_block notifier;
+
+	uint8_t num_function_row_keys;
 };
 
 /**
@@ -511,6 +542,44 @@ static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
 	return 0;
 }
 
+/**
+ * cros_ec_keyb_update_custom_keymap
+ *
+ * Update the keymap if the board has custom top row keys.
+ *
+ * @ckdev: The keyboard device
+ */
+
+static void cros_ec_keyb_update_custom_keymap(struct cros_ec_keyb *ckdev)
+{
+	u8 i;
+	u16 code;
+	u16 top_row_key_code[MAX_NUM_TOP_ROW_KEYS] = {0};
+	struct input_dev *idev = ckdev->idev;
+	unsigned short *keymap = idev->keycode;
+
+	if (of_property_read_variable_u16_array(ckdev->dev->of_node,
+						"google,custom-keyb-top-row",
+						top_row_key_code,
+						0,
+						MAX_NUM_TOP_ROW_KEYS) > 0) {
+		for (i = 0; i < MAX_NUM_TOP_ROW_KEYS; i++) {
+			if (!top_row_key_code[i])
+				break;
+			code = MATRIX_SCAN_CODE(top_row_key_pos[i].row,
+						top_row_key_pos[i].col,
+						ckdev->row_shift);
+			/*
+			 * Add the action key code for a top row key
+			 * into the keymap.
+			 */
+			keymap[code] = top_row_key_code[i];
+			__set_bit(keymap[code], idev->keybit);
+		}
+		ckdev->num_function_row_keys = i;
+	}
+}
+
 /**
  * cros_ec_keyb_register_bs - Register matrix keys
  *
@@ -576,6 +645,8 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
 	input_set_capability(idev, EV_MSC, MSC_SCAN);
 	input_set_drvdata(idev, ckdev);
 	ckdev->idev = idev;
+
+	cros_ec_keyb_update_custom_keymap(ckdev);
 	cros_ec_keyb_compute_valid_keys(ckdev);
 
 	err = input_register_device(ckdev->idev);
-- 
2.26.2


  reply	other threads:[~2020-12-22  1:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22  1:47 [PATCH 1/3] dt-bindings: input: cros-ec-keyb: Add a new property Philip Chen
2020-12-22  1:47 ` Philip Chen [this message]
2020-12-22  1:47 ` [PATCH 3/3] Input: cros-ec-keyb - Expose function row physical map to userspace Philip Chen
2020-12-29  6:18 ` [PATCH 1/3] dt-bindings: input: cros-ec-keyb: Add a new property Dmitry Torokhov
2021-01-02 19:39   ` Philip Chen
2021-01-02 21:04     ` Dmitry Torokhov
2021-01-03  4:53       ` Philip Chen
2021-01-03  6:11         ` Philip Chen
2021-01-03 22:48           ` Dmitry Torokhov
2021-01-04 23:03             ` Philip Chen

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=20201221174751.2.If8dc0ec9d1a60e436d1e852eba1316313f45ac0e@changeid \
    --to=philipchen@chromium.org \
    --cc=bleung@chromium.org \
    --cc=dianders@chromium.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=dtor@chromium.org \
    --cc=enric.balletbo@collabora.com \
    --cc=groeck@chromium.org \
    --cc=lee.jones@linaro.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rajatja@chromium.org \
    --cc=swboyd@chromium.org \
    /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;
as well as URLs for NNTP newsgroup(s).