Skip to contentSkip to conversation

Add Nuvoton MA35D1 keypad controller support

PATCH In review Feng-Yi Hsueh 3 participants 5 messages Nov 13 – Nov 30

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

v2 cover Add Nuvoton MA35D1 keypad controller support

The Nuvoton MA35D1 SoC provides a hardware matrix keypad controller supporting up to eight rows and eight columns.

This series adds the Device Tree binding and input driver for key press/release events, clock management, and system suspend/resume support.

Tested on a Nuvoton MA35D1 board with a 4x1 keypad configuration and a 1 MHz KPI clock using evtest.

Feng-Yi Hsueh (2):
dt-bindings: input: Add Nuvoton MA35D1 keypad controller
Input: keyboard: Add Nuvoton MA35D1 keypad driver

---
Changes since v1:
- dt-bindings: add optional resets and reference input.yaml.
- dt-bindings: fix interrupt, clock, and keymap formatting.
- input: use guard(mutex) in suspend/resume callbacks.
- input: rename error variables from ret to error and explicitly return 0 on successful resume.
- input: remove the unnecessary linux/mod_devicetable.h include.

 .../bindings/input/nuvoton,ma35d1-keypad.yaml |  75 ++++
 drivers/input/keyboard/Kconfig                |  11 +
 drivers/input/keyboard/Makefile               |   1 +
 drivers/input/keyboard/ma35d1_keypad.c        | 407 ++++++++++++++++++
 4 files changed, 494 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/nuvoton,ma35d1-keypad.yaml
 create mode 100644 drivers/input/keyboard/ma35d1_keypad.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 dt-bindings: input: Add Nuvoton MA35D1 keypad controller

Document the Nuvoton MA35D1 hardware matrix keypad controller.

The controller supports up to eight rows and eight columns and generates interrupts for key press and release events.

---
 .../bindings/input/nuvoton,ma35d1-keypad.yaml | 75 +++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/nuvoton,ma35d1-keypad.yaml
Documentation/devicetree/bindings/input/nuvoton,ma35d1-keypad.yaml new file +75 -0
new file mode 100644
index 000000000000..ff4f94166f0e
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/nuvoton,ma35d1-keypad.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 Matrix Keypad Controller
+
+maintainers:
+  - Feng-Yi Hsueh <[email protected]>
+
+description: |
+  The Nuvoton MA35D1 keypad controller scans a matrix of up to eight rows
+  and eight columns and reports key press and release interrupts.
+
+allOf:
+  - $ref: /schemas/input/matrix-keymap.yaml#
+  - $ref: /schemas/input/input.yaml#
+
+properties:
+  compatible:
+    const: nuvoton,ma35d1-keypad
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+  keypad,num-rows:
+    minimum: 2
+    maximum: 8
+
+  keypad,num-columns:
+    minimum: 1
+    maximum: 8
+
+  autorepeat: true
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - keypad,num-rows
+  - keypad,num-columns
+  - linux,keymap
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+    #include <dt-bindings/input/input.h>
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    keypad@404a0000 {
+        compatible = "nuvoton,ma35d1-keypad";
+        reg = <0x404a0000 0x10000>;
+        interrupts = <GIC_SPI 79 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clk KPI_GATE>;
+
+        keypad,num-rows = <4>;
+        keypad,num-columns = <1>;
+        linux,keymap = <MATRIX_KEY(0, 0, KEY_1)
+                        MATRIX_KEY(1, 0, KEY_2)
+                        MATRIX_KEY(2, 0, KEY_3)
+                        MATRIX_KEY(3, 0, KEY_4)>;
+    };
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 2/2 Input: keyboard: Add Nuvoton MA35D1 keypad driver

In reply to Feng-Yi Hsueh

Add support for the Nuvoton MA35D1 hardware matrix keypad controller.

The driver handles key press and release interrupts, reports key events through the Linux input subsystem, and manages the peripheral clock during device open, close, and system suspend and resume.

---
 drivers/input/keyboard/Kconfig         |  11 +
 drivers/input/keyboard/Makefile        |   1 +
 drivers/input/keyboard/ma35d1_keypad.c | 407 +++++++++++++++++++++++++
 3 files changed, 419 insertions(+)
 create mode 100644 drivers/input/keyboard/ma35d1_keypad.c
drivers/input/keyboard/Kconfig +11 -0
index d8c64f333462..9bbcbed3d494 100644
@@ -358,6 +358,17 @@ config KEYBOARD_LPC32XX
 	  To compile this driver as a module, choose M here: the
 	  module will be called lpc32xx-keys.
 
+config KEYBOARD_MA35D1
+	tristate "Nuvoton MA35D1 keypad controller"
+	depends on ARCH_MA35 || COMPILE_TEST
+	select INPUT_MATRIXKMAP
+	help
+	  Say Y here to enable support for the Nuvoton MA35D1
+	  hardware matrix keypad controller.
+
+	  To compile this driver as a module, choose M here.
+	  The module will be called ma35d1_keypad.
+
 config KEYBOARD_MAPLE
 	tristate "Maple bus keyboard"
 	depends on SH_DREAMCAST && MAPLE
drivers/input/keyboard/Makefile +1 -0
index 60bb7baf802f..fad34b7afff7 100644
@@ -39,6 +39,7 @@ obj-$(CONFIG_KEYBOARD_LM8323)		+= lm8323.o
 obj-$(CONFIG_KEYBOARD_LM8333)		+= lm8333.o
 obj-$(CONFIG_KEYBOARD_LOCOMO)		+= locomokbd.o
 obj-$(CONFIG_KEYBOARD_LPC32XX)		+= lpc32xx-keys.o
+obj-$(CONFIG_KEYBOARD_MA35D1) 		+= ma35d1_keypad.o
 obj-$(CONFIG_KEYBOARD_MAPLE)		+= maple_keyb.o
 obj-$(CONFIG_KEYBOARD_MATRIX)		+= matrix_keypad.o
 obj-$(CONFIG_KEYBOARD_MAX7359)		+= max7359_keypad.o
drivers/input/keyboard/ma35d1_keypad.c new file +407 -0

407 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH v2 2/2] Input: keyboard: Add Nuvoton MA35D1 keypad driver

Hi Feng-Yi,

Quoted 9 lines · Feng-Yi Hsueh

Add support for the Nuvoton MA35D1 hardware matrix keypad controller.

The driver handles key press and release interrupts, reports key events through the Linux input subsystem, and manages the peripheral clock during device open, close, and system suspend and resume.

Thank you for the driver, a few comments below.

...

Quoted 2 lines

obj-$(CONFIG_KEYBOARD_LPC32XX) += lpc32xx-keys.o
+obj-$(CONFIG_KEYBOARD_MA35D1) += ma35d1_keypad.o

Stray space before the tabs.

...

drivers/input/keyboard/ma35d1_keypad.c line 76

Quoted 3 lines
drivers/input/keyboard/ma35d1_keypad.c new file +3 -0
+	int irq;
+	bool running;	/* protected by input->mutex */
+};

Why do we need "running"? Input core pairs open() and close() under input->mutex on input_device_enabled() transitions, and suspend() and resume() both check input_device_enabled() while userspace is frozen. The only case where it makes a difference is if clk_prepare_enable() fails on resume and a later close() or suspend() calls ma35d1_keypad_disable() again. If that is what you are guarding against, please add a brief comment; otherwise please drop it.

...

Quoted 13 lines
drivers/input/keyboard/ma35d1_keypad.c new file +13 -0
+/* Release software key states when hardware scanning is stopped. */
+static void ma35d1_keypad_release_keys(struct ma35d1_keypad *keypad)
+{
+	struct input_dev *input = keypad->input;
+	unsigned int row, col, scan, code;
+	bool reported = false;
+
+	for (row = 0; row < keypad->rows; row++) {
+		for (col = 0; col < keypad->cols; col++) {
+			scan = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
+			code = keypad->keycodes[scan];
+			if (code == KEY_RESERVED || !test_bit(code, input->key))
+				continue;

drivers/input/keyboard/ma35d1_keypad.c line 120

Quoted 10 lines
drivers/input/keyboard/ma35d1_keypad.c new file +10 -0
+
+			input_event(input, EV_MSC, MSC_SCAN, scan);
+			input_report_key(input, code, 0);
+			reported = true;
+		}
+	}
+
+	if (reported)
+		input_sync(input);
+}

Input core already releases pressed keys on suspend, freeze, inhibit, and disconnect. Only plain close() does not, and scanning keycodes[] against input->key here will emit a bogus MSC_SCAN if two matrix positions map to the same keycode (it also checks input->key and keycodes[] outside input->event_lock). If you want to release keys on close, dropping the synthetic MSC_SCAN would be cleaner, or you can just drop this helper and let input core handle it.

...

drivers/input/keyboard/ma35d1_keypad.c line 220

Quoted 3 lines
drivers/input/keyboard/ma35d1_keypad.c new file +3 -0
+		while (events) {
+			bit = __ffs(events);
+			events &= ~BIT(bit);

Making "events" an unsigned long and using for_each_set_bit(bit, &events, 32) would be cleaner than iterating with __ffs() manually.

...

drivers/input/keyboard/ma35d1_keypad.c line 307

Quoted 1 line
drivers/input/keyboard/ma35d1_keypad.c new file +1 -0
+	keypad = devm_kzalloc(dev, sizeof(*keypad), GFP_KERNEL);

Please use devm_kzalloc_obj(dev, *keypad).

...

drivers/input/keyboard/ma35d1_keypad.c line 327

Quoted 4 lines
drivers/input/keyboard/ma35d1_keypad.c new file +4 -0
+	keypad->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(keypad->clk))
+		return dev_err_probe(dev, PTR_ERR(keypad->clk),
+				     "failed to get KPI clock\n");

Patch 1/2 adds an optional "resets" property to the binding, but the driver does not handle it at all. Should probe acquire and deassert it with devm_reset_control_get_optional_exclusive_deasserted(), or is there a reason to leave reset handling out?

Thanks.

Signature · 1 line
Dmitry