Skip to contentSkip to conversation

Input: matrix-keypad: Various performance improvements

PATCH Superseded Markus Burri 1 participant 8 messages Jan 8, 2025

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 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.13-rc6

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

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 (7):
Input: matrix_keypad - use fsleep for variable delay duration
Input: matrix_keypad - add function for reading row state
dt-bindings: input: matrix_keypad - convert to YAML
dt-bindings: input: matrix_keypad - add missing property
dt-bindings: input: matrix_keypad - add settle time after enable all
columns
Input: matrix_keypad - add settle time after enable all columns
Input: matrix_keypad - detect change during scan

 .../bindings/input/gpio-matrix-keypad.txt     |  49 ---------
 .../bindings/input/gpio-matrix-keypad.yaml    | 100 ++++++++++++++++++
 .../bindings/power/wakeup-source.txt          |   2 +-
 drivers/input/keyboard/matrix_keypad.c        |  38 ++++++-
 4 files changed, 135 insertions(+), 54 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/input/gpio-matrix-keypad.txt
 create mode 100644 Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml
Signature · 1 line
2.39.5
#1 lore raw

PATCH v4 1/7 Input: matrix_keypad - use fsleep for variable delay duration

The delay is retrieved from a device-tree property, so the duration is variable. fsleep guesses the best delay function based on duration.

---
 drivers/input/keyboard/matrix_keypad.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/keyboard/matrix_keypad.c +1 -1
index 2a3b3bf..5571d2e 100644
@@ -68,7 +68,7 @@ static void activate_col(struct matrix_keypad *keypad, int col, bool on)
 	__activate_col(keypad, col, on);
 
 	if (on && keypad->col_scan_delay_us)
-		udelay(keypad->col_scan_delay_us);
+		fsleep(keypad->col_scan_delay_us);
 }
 
 static void activate_all_cols(struct matrix_keypad *keypad, bool on)
Signature · 1 line
2.39.5
#2 lore raw

PATCH v4 2/7 Input: matrix_keypad - add function for reading row state

In reply to Markus Burri

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 5571d2e..90148d3 100644
@@ -100,6 +100,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
  */
@@ -125,9 +135,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
#3 lore raw

PATCH v4 3/7 dt-bindings: input: matrix_keypad - convert to YAML

In reply to Markus Burri

Convert the gpio-matrix-keypad bindings from text to DT schema.

---
 .../bindings/input/gpio-matrix-keypad.txt     | 49 -----------
 .../bindings/input/gpio-matrix-keypad.yaml    | 88 +++++++++++++++++++
 .../bindings/power/wakeup-source.txt          |  2 +-
 3 files changed, 89 insertions(+), 50 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/input/gpio-matrix-keypad.txt
 create mode 100644 Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml
Documentation/devicetree/bindings/input/gpio-matrix-keypad.txt deleted +0 -49
deleted file mode 100644
index 570dc10..0000000
@@ -1,49 +0,0 @@
-* GPIO driven matrix keypad device tree bindings
-
-GPIO driven matrix keypad is used to interface a SoC with a matrix keypad.
-The matrix keypad supports multiple row and column lines, a key can be
-placed at each intersection of a unique row and a unique column. The matrix
-keypad can sense a key-press and key-release by means of GPIO lines and
-report the event using GPIO interrupts to the cpu.
-
-Required Properties:
-- compatible:		Should be "gpio-matrix-keypad"
-- row-gpios:		List of gpios used as row lines. The gpio specifier
-			for this property depends on the gpio controller to
-			which these row lines are connected.
-- col-gpios:		List of gpios used as column lines. The gpio specifier
-			for this property depends on the gpio controller to
-			which these column lines are connected.
-- linux,keymap:		The definition can be found at
-			bindings/input/matrix-keymap.txt
-
-Optional Properties:
-- linux,no-autorepeat:	do no enable autorepeat feature.
-- wakeup-source:	use any event on keypad as wakeup event.
-			(Legacy property supported: "linux,wakeup")
-- debounce-delay-ms:	debounce interval in milliseconds
-- col-scan-delay-us:	delay, measured in microseconds, that is needed
-			before we can scan keypad after activating column gpio
-- drive-inactive-cols:	drive inactive columns during scan,
-			default is to turn inactive columns into inputs.
-
-Example:
-	matrix-keypad {
-		compatible = "gpio-matrix-keypad";
-		debounce-delay-ms = <5>;
-		col-scan-delay-us = <2>;
-
-		row-gpios = <&gpio2 25 0
-			     &gpio2 26 0
-			     &gpio2 27 0>;
-
-		col-gpios = <&gpio2 21 0
-			     &gpio2 22 0>;
-
-		linux,keymap = <0x0000008B
-				0x0100009E
-				0x02000069
-				0x0001006A
-				0x0101001C
-				0x0201006C>;
-	};
Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml new file +88 -0

88 lines not on this page · raw message

Documentation/devicetree/bindings/power/wakeup-source.txt +1 -1
index 27f1797..66bb016 100644
@@ -23,7 +23,7 @@ List of legacy properties and respective binding document
 
 1. "gpio-key,wakeup"		Documentation/devicetree/bindings/input/gpio-keys{,-polled}.txt
 2. "has-tpo"			Documentation/devicetree/bindings/rtc/rtc-opal.txt
-3. "linux,wakeup"		Documentation/devicetree/bindings/input/gpio-matrix-keypad.txt
+3. "linux,wakeup"		Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml
 				Documentation/devicetree/bindings/mfd/tc3589x.txt
 				Documentation/devicetree/bindings/input/touchscreen/ti,ads7843.yaml
 4. "linux,keypad-wakeup"	Documentation/devicetree/bindings/input/qcom,pm8921-keypad.yaml
Signature · 1 line
2.39.5
#4 lore raw

PATCH v4 4/7 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       | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml +6 -0
index 0f348b9..0555c1e 100644
@@ -40,6 +40,12 @@ properties:
     type: boolean
     description: Do not enable autorepeat feature.
 
+  gpio-activelow:
+    type: boolean
+    deprecated: true
+    description:
+      The GPIOs are low active (deprecated).
+      Use the GPIO-flags in gpio controller instead of this property.
 
   debounce-delay-ms:
     description: Debounce interval in milliseconds.
Signature · 1 line
2.39.5
#5 lore raw

PATCH v4 5/7 dt-bindings: input: matrix_keypad - add settle time after enable all columns

In reply to Markus Burri

Matrix_keypad with high capacity need a longer settle time after enable all columns.
Add optional property to specify the settle time

---
 .../devicetree/bindings/input/gpio-matrix-keypad.yaml       | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/input/gpio-matrix-keypad.yaml +6 -0
index 0555c1e..408474e 100644
@@ -57,6 +57,12 @@ properties:
       before we can scan keypad after activating column gpio.
     default: 0
 
+  all-cols-on-delay-us:
+    description:
+      Delay, measured in microseconds, that is needed
+      after activating all column gpios.
+    default: 0
+
   drive-inactive-cols:
     type: boolean
     description:
Signature · 1 line
2.39.5
#6 lore raw

PATCH v4 6/7 Input: matrix_keypad - add settle time after enable all columns

In reply to Markus Burri

Matrix_keypad with high capacity need a longer settle time after enable all columns and re-enabling interrupts.
This to give time stable the system and not generate interrupts.

Add a new optional device-tree property to configure the time before enabling interrupts after disable all columns.
The default is no delay.

---
 drivers/input/keyboard/matrix_keypad.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/input/keyboard/matrix_keypad.c +6 -0
index 90148d3..fdb3499 100644
@@ -26,6 +26,7 @@ struct matrix_keypad {
 	unsigned int row_shift;
 
 	unsigned int col_scan_delay_us;
+	unsigned int all_cols_on_delay_us;
 	/* key debounce interval in milli-second */
 	unsigned int debounce_ms;
 	bool drive_inactive_cols;
@@ -77,6 +78,9 @@ static void activate_all_cols(struct matrix_keypad *keypad, bool on)
 
 	for (col = 0; col < keypad->num_col_gpios; col++)
 		__activate_col(keypad, col, on);
+
+	if (on && keypad->all_cols_on_delay_us)
+		fsleep(keypad->all_cols_on_delay_us);
 }
 
 static bool row_asserted(struct matrix_keypad *keypad, int row)
@@ -400,6 +404,8 @@ static int matrix_keypad_probe(struct platform_device *pdev)
 				 &keypad->debounce_ms);
 	device_property_read_u32(&pdev->dev, "col-scan-delay-us",
 				 &keypad->col_scan_delay_us);
+	device_property_read_u32(&pdev->dev, "all-cols-on-delay-us",
+				 &keypad->all_cols_on_delay_us);
 
 	err = matrix_keypad_init_gpio(pdev, keypad);
 	if (err)
Signature · 1 line
2.39.5
#7 lore raw

PATCH v4 7/7 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 fdb3499..86ae4df 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