Skip to content

Commit 2ca45e5

Browse files
hvilleneuvedoodtor
authored andcommitted
Input: charlieplex_keypad - add GPIO charlieplex keypad
Add support for GPIO-based charlieplex keypad, allowing to control N^2-N keys using N GPIO lines. Reuse matrix keypad keymap to simplify, even if there is no concept of rows and columns in this type of keyboard. Signed-off-by: Hugo Villeneuve <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 0d64bee commit 2ca45e5

4 files changed

Lines changed: 254 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5962,6 +5962,13 @@ S: Maintained
59625962
F: Documentation/hwmon/powerz.rst
59635963
F: drivers/hwmon/powerz.c
59645964

5965+
CHARLIEPLEX KEYPAD DRIVER
5966+
M: Hugo Villeneuve <[email protected]>
5967+
S: Supported
5968+
W: http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/electronic-circuits/matrix-keypad-scan-decode
5969+
F: Documentation/devicetree/bindings/input/gpio-charlieplex-keypad.yaml
5970+
F: drivers/input/keyboard/charlieplex_keypad.c
5971+
59655972
CHECKPATCH
59665973
M: Andy Whitcroft <[email protected]>
59675974
M: Joe Perches <[email protected]>

drivers/input/keyboard/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ config KEYBOARD_MATRIX
289289
To compile this driver as a module, choose M here: the
290290
module will be called matrix_keypad.
291291

292+
config KEYBOARD_CHARLIEPLEX
293+
tristate "GPIO driven charlieplex keypad support"
294+
depends on GPIOLIB || COMPILE_TEST
295+
select INPUT_MATRIXKMAP
296+
help
297+
Enable support for GPIO driven charlieplex keypad. A charlieplex
298+
keypad allows to use fewer GPIO lines to interface to key switches.
299+
For example, an N lines charlieplex keypad can be used to interface
300+
to N^2-N different key switches. However, this type of keypad
301+
cannot detect more than one key press at a time.
302+
303+
To compile this driver as a module, choose M here: the
304+
module will be called charlieplex_keypad.
305+
292306
config KEYBOARD_HIL_OLD
293307
tristate "HP HIL keyboard support (simple driver)"
294308
depends on GSC || HP300

drivers/input/keyboard/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ obj-$(CONFIG_KEYBOARD_ATARI) += atakbd.o
1515
obj-$(CONFIG_KEYBOARD_ATKBD) += atkbd.o
1616
obj-$(CONFIG_KEYBOARD_BCM) += bcm-keypad.o
1717
obj-$(CONFIG_KEYBOARD_CAP11XX) += cap11xx.o
18+
obj-$(CONFIG_KEYBOARD_CHARLIEPLEX) += charlieplex_keypad.o
1819
obj-$(CONFIG_KEYBOARD_CLPS711X) += clps711x-keypad.o
1920
obj-$(CONFIG_KEYBOARD_CROS_EC) += cros_ec_keyb.o
2021
obj-$(CONFIG_KEYBOARD_CYPRESS_SF) += cypress-sf.o
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* GPIO driven charlieplex keypad driver
4+
*
5+
* Copyright (c) 2026 Hugo Villeneuve <[email protected]>
6+
*
7+
* Based on matrix_keyboard.c
8+
*/
9+
10+
#include <linux/bitops.h>
11+
#include <linux/delay.h>
12+
#include <linux/dev_printk.h>
13+
#include <linux/device/devres.h>
14+
#include <linux/err.h>
15+
#include <linux/gpio/consumer.h>
16+
#include <linux/input.h>
17+
#include <linux/input/matrix_keypad.h>
18+
#include <linux/math.h>
19+
#include <linux/module.h>
20+
#include <linux/mod_devicetable.h>
21+
#include <linux/platform_device.h>
22+
#include <linux/property.h>
23+
#include <linux/string_helpers.h>
24+
#include <linux/types.h>
25+
26+
struct charlieplex_keypad {
27+
struct input_dev *input_dev;
28+
struct gpio_descs *line_gpios;
29+
unsigned int nlines;
30+
unsigned int settling_time_us;
31+
unsigned int debounce_threshold;
32+
unsigned int debounce_count;
33+
int debounce_code;
34+
int current_code;
35+
};
36+
37+
static void charlieplex_keypad_report_key(struct input_dev *input)
38+
{
39+
struct charlieplex_keypad *keypad = input_get_drvdata(input);
40+
const unsigned short *keycodes = input->keycode;
41+
42+
if (keypad->current_code > 0) {
43+
input_event(input, EV_MSC, MSC_SCAN, keypad->current_code);
44+
input_report_key(input, keycodes[keypad->current_code], 0);
45+
input_sync(input);
46+
}
47+
48+
if (keypad->debounce_code) {
49+
input_event(input, EV_MSC, MSC_SCAN, keypad->debounce_code);
50+
input_report_key(input, keycodes[keypad->debounce_code], 1);
51+
input_sync(input);
52+
}
53+
54+
keypad->current_code = keypad->debounce_code;
55+
}
56+
57+
static void charlieplex_keypad_check_switch_change(struct input_dev *input,
58+
unsigned int code)
59+
{
60+
struct charlieplex_keypad *keypad = input_get_drvdata(input);
61+
62+
if (code != keypad->debounce_code) {
63+
keypad->debounce_count = 0;
64+
keypad->debounce_code = code;
65+
}
66+
67+
if (keypad->debounce_code != keypad->current_code) {
68+
if (keypad->debounce_count++ >= keypad->debounce_threshold)
69+
charlieplex_keypad_report_key(input);
70+
}
71+
}
72+
73+
static int charlieplex_keypad_scan_line(struct charlieplex_keypad *keypad,
74+
unsigned int oline)
75+
{
76+
struct gpio_descs *line_gpios = keypad->line_gpios;
77+
DECLARE_BITMAP(values, MATRIX_MAX_ROWS);
78+
int err;
79+
80+
/* Activate only one line as output at a time. */
81+
gpiod_direction_output(line_gpios->desc[oline], 1);
82+
83+
if (keypad->settling_time_us)
84+
fsleep(keypad->settling_time_us);
85+
86+
/* Read input on all other lines. */
87+
err = gpiod_get_array_value_cansleep(line_gpios->ndescs, line_gpios->desc,
88+
line_gpios->info, values);
89+
90+
gpiod_direction_input(line_gpios->desc[oline]);
91+
92+
if (err)
93+
return err;
94+
95+
for (unsigned int iline = 0; iline < keypad->nlines; iline++) {
96+
if (iline == oline)
97+
continue; /* Do not read active output line. */
98+
99+
/* Check if GPIO is asserted. */
100+
if (test_bit(iline, values))
101+
return MATRIX_SCAN_CODE(oline, iline,
102+
get_count_order(keypad->nlines));
103+
}
104+
105+
return 0;
106+
}
107+
108+
static void charlieplex_keypad_poll(struct input_dev *input)
109+
{
110+
struct charlieplex_keypad *keypad = input_get_drvdata(input);
111+
int code = 0;
112+
113+
for (unsigned int oline = 0; oline < keypad->nlines; oline++) {
114+
code = charlieplex_keypad_scan_line(keypad, oline);
115+
if (code != 0)
116+
break;
117+
}
118+
119+
if (code >= 0)
120+
charlieplex_keypad_check_switch_change(input, code);
121+
}
122+
123+
static int charlieplex_keypad_init_gpio(struct platform_device *pdev,
124+
struct charlieplex_keypad *keypad)
125+
{
126+
char **pin_names;
127+
char label[32];
128+
129+
snprintf(label, sizeof(label), "%s-pin", pdev->name);
130+
131+
keypad->line_gpios = devm_gpiod_get_array(&pdev->dev, "line", GPIOD_IN);
132+
if (IS_ERR(keypad->line_gpios))
133+
return PTR_ERR(keypad->line_gpios);
134+
135+
keypad->nlines = keypad->line_gpios->ndescs;
136+
137+
if (keypad->nlines > MATRIX_MAX_ROWS)
138+
return -EINVAL;
139+
140+
pin_names = devm_kasprintf_strarray(&pdev->dev, label, keypad->nlines);
141+
if (IS_ERR(pin_names))
142+
return PTR_ERR(pin_names);
143+
144+
for (unsigned int i = 0; i < keypad->line_gpios->ndescs; i++)
145+
gpiod_set_consumer_name(keypad->line_gpios->desc[i], pin_names[i]);
146+
147+
return 0;
148+
}
149+
150+
static int charlieplex_keypad_probe(struct platform_device *pdev)
151+
{
152+
struct charlieplex_keypad *keypad;
153+
struct input_dev *input_dev;
154+
unsigned int debounce_interval_ms = 5;
155+
unsigned int poll_interval_ms;
156+
int err;
157+
158+
keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
159+
if (!keypad)
160+
return -ENOMEM;
161+
162+
input_dev = devm_input_allocate_device(&pdev->dev);
163+
if (!input_dev)
164+
return -ENOMEM;
165+
166+
keypad->input_dev = input_dev;
167+
168+
err = device_property_read_u32(&pdev->dev, "poll-interval", &poll_interval_ms);
169+
if (err)
170+
return dev_err_probe(&pdev->dev, err,
171+
"failed to parse 'poll-interval' property\n");
172+
173+
if (poll_interval_ms == 0)
174+
return dev_err_probe(&pdev->dev, -EINVAL, "invalid 'poll-interval' value\n");
175+
176+
device_property_read_u32(&pdev->dev, "debounce-delay-ms", &debounce_interval_ms);
177+
device_property_read_u32(&pdev->dev, "settling-time-us", &keypad->settling_time_us);
178+
179+
keypad->current_code = -1;
180+
keypad->debounce_code = -1;
181+
keypad->debounce_threshold = DIV_ROUND_UP(debounce_interval_ms, poll_interval_ms);
182+
183+
err = charlieplex_keypad_init_gpio(pdev, keypad);
184+
if (err)
185+
return err;
186+
187+
input_dev->name = pdev->name;
188+
input_dev->id.bustype = BUS_HOST;
189+
190+
err = matrix_keypad_build_keymap(NULL, NULL, keypad->nlines,
191+
keypad->nlines, NULL, input_dev);
192+
if (err)
193+
return dev_err_probe(&pdev->dev, err, "failed to build keymap\n");
194+
195+
if (device_property_read_bool(&pdev->dev, "autorepeat"))
196+
__set_bit(EV_REP, input_dev->evbit);
197+
198+
input_set_capability(input_dev, EV_MSC, MSC_SCAN);
199+
200+
err = input_setup_polling(input_dev, charlieplex_keypad_poll);
201+
if (err)
202+
return dev_err_probe(&pdev->dev, err, "unable to set up polling\n");
203+
204+
input_set_poll_interval(input_dev, poll_interval_ms);
205+
206+
input_set_drvdata(input_dev, keypad);
207+
208+
err = input_register_device(keypad->input_dev);
209+
if (err)
210+
return err;
211+
212+
return 0;
213+
}
214+
215+
static const struct of_device_id charlieplex_keypad_dt_match[] = {
216+
{ .compatible = "gpio-charlieplex-keypad" },
217+
{ }
218+
};
219+
MODULE_DEVICE_TABLE(of, charlieplex_keypad_dt_match);
220+
221+
static struct platform_driver charlieplex_keypad_driver = {
222+
.probe = charlieplex_keypad_probe,
223+
.driver = {
224+
.name = "charlieplex-keypad",
225+
.of_match_table = charlieplex_keypad_dt_match,
226+
},
227+
};
228+
module_platform_driver(charlieplex_keypad_driver);
229+
230+
MODULE_AUTHOR("Hugo Villeneuve <[email protected]>");
231+
MODULE_DESCRIPTION("GPIO driven charlieplex keypad driver");
232+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)