Skip to contentSkip to conversation

Input: matrix-keypad: Various performance improvements

PATCH In review Markus Burri 2 participants 5 messages Feb 26, 2025 – Feb 28, 2025

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

v6 cover Input: matrix-keypad: Various performance improvements

This series is needed to avoid key-loss if the GPIOs are connected via a I2C GPIO MUX that introduces additional latencies between key change and matrix scan.

This series applies on 6.14-rc4
---
Changes in V6:
* adapt dt-bindings description for gpio-activelow
* remove applied patches (https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git/log/)

Changes in V5:
* formatting and typo

Changes in V4:
* deprecate gpio-activelow in gpio-matrix-keypad.yaml
* change reference to linux,keymap in gpio-matrix-keypad.yaml

Changes in V3:
* fix 'references a file that doesn't exist' in wakeup-source.txt
* remove copied and deprecated properties from gpio-matrix-keypad.yaml
* extend patch description to clarify the changes
* rebase to 6.13-rc6

Changes in V2:
* added patch to convert dt-bindings to YAML
* added missing dt-bindings properties description
* removed [PATCH 1/6] Input: matrix_keypad - move gpio-row init to the init it would revert 01c84b03d80aab9f04c4e3e1f9085f4202ff7c29
* removed internally given Tested-by and Reviewed-by tags

[V5] https://lore.kernel.org/lkml/[email protected]/
[V4] https://lore.kernel.org/lkml/[email protected]/
[V3] https://lore.kernel.org/lkml/[email protected]/
[V2] https://lore.kernel.org/lkml/[email protected]/
[V1] https://lore.kernel.org/lkml/[email protected]/

Markus Burri (3):
Input: matrix_keypad - add function for reading row state
dt-bindings: input: matrix_keypad - add missing property
Input: matrix_keypad - detect change during scan

 .../bindings/input/gpio-matrix-keypad.yaml    |  5 ++++
 drivers/input/keyboard/matrix_keypad.c        | 30 +++++++++++++++++--
 2 files changed, 32 insertions(+), 3 deletions(-)

base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
prerequisite-patch-id: bb305aaedfe8c51a577a4995259b03dbc918e22f
prerequisite-patch-id: 952ec63bb9d0c7a79f87d9e47386eb87e2a0075c
prerequisite-patch-id: 6e4d765fc9f3ca7bd204b9b461f64adcafb815c0
prerequisite-patch-id: bf32190799d208ecee6e241204278ebd5835270e

Signature · 1 line
2.39.5
#1 lore raw

PATCH v6 1/3 Input: matrix_keypad - add function for reading row state

Move the evaluation of a row state into separate function. It will be also used by a change later in this series.

---
 drivers/input/keyboard/matrix_keypad.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/input/keyboard/matrix_keypad.c +11 -3
index e46473cb8..fdb349966 100644
@@ -104,6 +104,16 @@ static void disable_row_irqs(struct matrix_keypad *keypad)
 		disable_irq_nosync(keypad->row_irqs[i]);
 }
 
+static uint32_t read_row_state(struct matrix_keypad *keypad)
+{
+	int row;
+	u32 row_state = 0;
+
+	for (row = 0; row < keypad->num_row_gpios; row++)
+		row_state |= row_asserted(keypad, row) ? BIT(row) : 0;
+	return row_state;
+}
+
 /*
  * This gets the keys from keyboard and reports it to input subsystem
  */
@@ -129,9 +139,7 @@ static void matrix_keypad_scan(struct work_struct *work)
 
 		activate_col(keypad, col, true);
 
-		for (row = 0; row < keypad->num_row_gpios; row++)
-			new_state[col] |=
-				row_asserted(keypad, row) ? BIT(row) : 0;
+		new_state[col] = read_row_state(keypad);
 
 		activate_col(keypad, col, false);
 	}
Signature · 1 line
2.39.5
#2 lore raw

PATCH v6 2/3 dt-bindings: input: matrix_keypad - add missing property

In reply to Markus Burri

The property is implemented in the driver but not described in dt-bindings. Add missing property 'gpio-activelow' to DT schema.

---
 .../devicetree/bindings/input/gpio-matrix-keypad.yaml        | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml +5 -0
index 4a5893edf..73bb153ed 100644
@@ -40,6 +40,11 @@ properties:
     type: boolean
     description: Do not enable autorepeat feature.
 
+  gpio-activelow:
+    type: boolean
+    description:
+      Force GPIO polarity to active low.
+      In the absence of this property GPIOs are treated as active high.
 
   debounce-delay-ms:
     description: Debounce interval in milliseconds.
Signature · 1 line
2.39.5
maintainer
#3 lore raw

Re: [PATCH v6 2/3] dt-bindings: input: matrix_keypad - add missing property

Quoted 5 lines · Markus Burri

The property is implemented in the driver but not described in dt-bindings. Add missing property 'gpio-activelow' to DT schema.

Applied but dropped Rob's reviewed-by because the patch is different from what he has reviewed.

Thanks.

Signature · 1 line
Dmitry
#4 lore raw

PATCH v6 3/3 Input: matrix_keypad - detect change during scan

In reply to Markus Burri

For a setup where the matrix keypad is connected over a slow interface (e.g. a gpio-expansion over i2c), the scan can take a longer time to read.

Interrupts need to be disabled during scan. And therefore changes in this period are not detected.
To improve this situation, scan the matrix again if the row state changed during interrupts disabled.
The rescan is repeated until no change is detected anymore.

---
 drivers/input/keyboard/matrix_keypad.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/input/keyboard/matrix_keypad.c +16 -0
index fdb349966..e50a6fea9 100644
@@ -125,6 +125,10 @@ static void matrix_keypad_scan(struct work_struct *work)
 	const unsigned short *keycodes = input_dev->keycode;
 	uint32_t new_state[MATRIX_MAX_COLS];
 	int row, col, code;
+	u32 init_row_state, new_row_state;
+
+	/* read initial row state to detect changes between scan */
+	init_row_state = read_row_state(keypad);
 
 	/* de-activate all columns for scanning */
 	activate_all_cols(keypad, false);
@@ -173,6 +177,18 @@ static void matrix_keypad_scan(struct work_struct *work)
 		keypad->scan_pending = false;
 		enable_row_irqs(keypad);
 	}
+
+	/* read new row state and detect if value has changed */
+	new_row_state = read_row_state(keypad);
+	if (init_row_state != new_row_state) {
+		guard(spinlock_irq)(&keypad->lock);
+		if (unlikely(keypad->scan_pending || keypad->stopped))
+			return;
+		disable_row_irqs(keypad);
+		keypad->scan_pending = true;
+		schedule_delayed_work(&keypad->work,
+				      msecs_to_jiffies(keypad->debounce_ms));
+	}
 }
 
 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
Signature · 1 line
2.39.5