1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driven matrix keyboard driver
10 #include <linux/types.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/platform_device.h>
14 #include <linux/input.h>
15 #include <linux/irq.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/module.h>
19 #include <linux/gpio.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/input/matrix_keypad.h>
22 #include <linux/slab.h>
25 struct matrix_keypad {
26 struct input_dev *input_dev;
27 unsigned int row_shift;
29 unsigned int col_scan_delay_us;
30 /* key debounce interval in milli-second */
31 unsigned int debounce_ms;
32 bool drive_inactive_cols;
34 struct gpio_desc *row_gpios[MATRIX_MAX_ROWS];
35 unsigned int num_row_gpios;
37 struct gpio_desc *col_gpios[MATRIX_MAX_ROWS];
38 unsigned int num_col_gpios;
40 unsigned int row_irqs[MATRIX_MAX_ROWS];
41 DECLARE_BITMAP(wakeup_enabled_irqs, MATRIX_MAX_ROWS);
43 uint32_t last_key_state[MATRIX_MAX_COLS];
44 struct delayed_work work;
51 * NOTE: If drive_inactive_cols is false, then the GPIO has to be put into
52 * HiZ when de-activated to cause minmal side effect when scanning other
53 * columns. In that case it is configured here to be input, otherwise it is
54 * driven with the inactive value.
56 static void __activate_col(struct matrix_keypad *keypad, int col, bool on)
59 gpiod_direction_output(keypad->col_gpios[col], 1);
61 gpiod_set_value_cansleep(keypad->col_gpios[col], 0);
62 if (!keypad->drive_inactive_cols)
63 gpiod_direction_input(keypad->col_gpios[col]);
67 static void activate_col(struct matrix_keypad *keypad, int col, bool on)
69 __activate_col(keypad, col, on);
71 if (on && keypad->col_scan_delay_us)
72 udelay(keypad->col_scan_delay_us);
75 static void activate_all_cols(struct matrix_keypad *keypad, bool on)
79 for (col = 0; col < keypad->num_col_gpios; col++)
80 __activate_col(keypad, col, on);
83 static bool row_asserted(struct matrix_keypad *keypad, int row)
85 return gpiod_get_value_cansleep(keypad->row_gpios[row]);
88 static void enable_row_irqs(struct matrix_keypad *keypad)
92 for (i = 0; i < keypad->num_row_gpios; i++)
93 enable_irq(keypad->row_irqs[i]);
96 static void disable_row_irqs(struct matrix_keypad *keypad)
100 for (i = 0; i < keypad->num_row_gpios; i++)
101 disable_irq_nosync(keypad->row_irqs[i]);
105 * This gets the keys from keyboard and reports it to input subsystem
107 static void matrix_keypad_scan(struct work_struct *work)
109 struct matrix_keypad *keypad =
110 container_of(work, struct matrix_keypad, work.work);
111 struct input_dev *input_dev = keypad->input_dev;
112 const unsigned short *keycodes = input_dev->keycode;
113 uint32_t new_state[MATRIX_MAX_COLS];
116 /* de-activate all columns for scanning */
117 activate_all_cols(keypad, false);
119 memset(new_state, 0, sizeof(new_state));
121 for (row = 0; row < keypad->num_row_gpios; row++)
122 gpiod_direction_input(keypad->row_gpios[row]);
124 /* assert each column and read the row status out */
125 for (col = 0; col < keypad->num_col_gpios; col++) {
127 activate_col(keypad, col, true);
129 for (row = 0; row < keypad->num_row_gpios; row++)
131 row_asserted(keypad, row) ? BIT(row) : 0;
133 activate_col(keypad, col, false);
136 for (col = 0; col < keypad->num_col_gpios; col++) {
137 uint32_t bits_changed;
139 bits_changed = keypad->last_key_state[col] ^ new_state[col];
140 if (bits_changed == 0)
143 for (row = 0; row < keypad->num_row_gpios; row++) {
144 if (!(bits_changed & BIT(row)))
147 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
148 input_event(input_dev, EV_MSC, MSC_SCAN, code);
149 input_report_key(input_dev,
151 new_state[col] & (1 << row));
154 input_sync(input_dev);
156 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
158 activate_all_cols(keypad, true);
160 /* Enable IRQs again */
161 spin_lock_irq(&keypad->lock);
162 keypad->scan_pending = false;
163 enable_row_irqs(keypad);
164 spin_unlock_irq(&keypad->lock);
167 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
169 struct matrix_keypad *keypad = id;
172 spin_lock_irqsave(&keypad->lock, flags);
175 * See if another IRQ beaten us to it and scheduled the
176 * scan already. In that case we should not try to
177 * disable IRQs again.
179 if (unlikely(keypad->scan_pending || keypad->stopped))
182 disable_row_irqs(keypad);
183 keypad->scan_pending = true;
184 schedule_delayed_work(&keypad->work,
185 msecs_to_jiffies(keypad->debounce_ms));
188 spin_unlock_irqrestore(&keypad->lock, flags);
192 static int matrix_keypad_start(struct input_dev *dev)
194 struct matrix_keypad *keypad = input_get_drvdata(dev);
196 keypad->stopped = false;
200 * Schedule an immediate key scan to capture current key state;
201 * columns will be activated and IRQs be enabled after the scan.
203 schedule_delayed_work(&keypad->work, 0);
208 static void matrix_keypad_stop(struct input_dev *dev)
210 struct matrix_keypad *keypad = input_get_drvdata(dev);
212 spin_lock_irq(&keypad->lock);
213 keypad->stopped = true;
214 spin_unlock_irq(&keypad->lock);
216 flush_delayed_work(&keypad->work);
218 * matrix_keypad_scan() will leave IRQs enabled;
219 * we should disable them now.
221 disable_row_irqs(keypad);
224 static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
228 for_each_clear_bit(i, keypad->wakeup_enabled_irqs,
229 keypad->num_row_gpios)
230 if (enable_irq_wake(keypad->row_irqs[i]) == 0)
231 __set_bit(i, keypad->wakeup_enabled_irqs);
234 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
238 for_each_set_bit(i, keypad->wakeup_enabled_irqs,
239 keypad->num_row_gpios) {
240 disable_irq_wake(keypad->row_irqs[i]);
241 __clear_bit(i, keypad->wakeup_enabled_irqs);
245 static int matrix_keypad_suspend(struct device *dev)
247 struct platform_device *pdev = to_platform_device(dev);
248 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
250 matrix_keypad_stop(keypad->input_dev);
252 if (device_may_wakeup(&pdev->dev))
253 matrix_keypad_enable_wakeup(keypad);
258 static int matrix_keypad_resume(struct device *dev)
260 struct platform_device *pdev = to_platform_device(dev);
261 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
263 if (device_may_wakeup(&pdev->dev))
264 matrix_keypad_disable_wakeup(keypad);
266 matrix_keypad_start(keypad->input_dev);
271 static DEFINE_SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
272 matrix_keypad_suspend, matrix_keypad_resume);
274 static int matrix_keypad_init_gpio(struct platform_device *pdev,
275 struct matrix_keypad *keypad)
282 nrow = gpiod_count(&pdev->dev, "row");
283 ncol = gpiod_count(&pdev->dev, "col");
284 if (nrow < 0 || ncol < 0) {
285 dev_err(&pdev->dev, "missing row or column GPIOs\n");
289 keypad->num_row_gpios = nrow;
290 keypad->num_col_gpios = ncol;
292 active_low = device_property_read_bool(&pdev->dev, "gpio-activelow");
294 /* initialize strobe lines as outputs, activated */
295 for (i = 0; i < keypad->num_col_gpios; i++) {
296 keypad->col_gpios[i] = devm_gpiod_get_index(&pdev->dev, "col",
298 err = PTR_ERR_OR_ZERO(keypad->col_gpios[i]);
301 "failed to request GPIO for COL%d: %d\n",
306 gpiod_set_consumer_name(keypad->col_gpios[i], "matrix_kbd_col");
308 if (active_low ^ gpiod_is_active_low(keypad->col_gpios[i]))
309 gpiod_toggle_active_low(keypad->col_gpios[i]);
311 gpiod_direction_output(keypad->col_gpios[i], 1);
314 for (i = 0; i < keypad->num_row_gpios; i++) {
315 keypad->row_gpios[i] = devm_gpiod_get_index(&pdev->dev, "row",
317 err = PTR_ERR_OR_ZERO(keypad->row_gpios[i]);
320 "failed to request GPIO for ROW%d: %d\n",
325 gpiod_set_consumer_name(keypad->row_gpios[i], "matrix_kbd_row");
327 if (active_low ^ gpiod_is_active_low(keypad->row_gpios[i]))
328 gpiod_toggle_active_low(keypad->row_gpios[i]);
334 static int matrix_keypad_setup_interrupts(struct platform_device *pdev,
335 struct matrix_keypad *keypad)
341 for (i = 0; i < keypad->num_row_gpios; i++) {
342 irq = gpiod_to_irq(keypad->row_gpios[i]);
346 "Unable to convert GPIO line %i to irq: %d\n",
351 err = devm_request_any_context_irq(&pdev->dev, irq,
352 matrix_keypad_interrupt,
353 IRQF_TRIGGER_RISING |
354 IRQF_TRIGGER_FALLING,
355 "matrix-keypad", keypad);
358 "Unable to acquire interrupt for row %i: %d\n",
363 keypad->row_irqs[i] = irq;
366 /* initialized as disabled - enabled by input->open */
367 disable_row_irqs(keypad);
372 static int matrix_keypad_probe(struct platform_device *pdev)
374 struct matrix_keypad *keypad;
375 struct input_dev *input_dev;
379 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
383 input_dev = devm_input_allocate_device(&pdev->dev);
387 keypad->input_dev = input_dev;
388 keypad->stopped = true;
389 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
390 spin_lock_init(&keypad->lock);
392 keypad->drive_inactive_cols =
393 device_property_read_bool(&pdev->dev, "drive-inactive-cols");
394 device_property_read_u32(&pdev->dev, "debounce-delay-ms",
395 &keypad->debounce_ms);
396 device_property_read_u32(&pdev->dev, "col-scan-delay-us",
397 &keypad->col_scan_delay_us);
399 err = matrix_keypad_init_gpio(pdev, keypad);
403 keypad->row_shift = get_count_order(keypad->num_col_gpios);
405 err = matrix_keypad_setup_interrupts(pdev, keypad);
409 input_dev->name = pdev->name;
410 input_dev->id.bustype = BUS_HOST;
411 input_dev->open = matrix_keypad_start;
412 input_dev->close = matrix_keypad_stop;
414 err = matrix_keypad_build_keymap(NULL, NULL,
415 keypad->num_row_gpios,
416 keypad->num_col_gpios,
419 dev_err(&pdev->dev, "failed to build keymap\n");
423 if (!device_property_read_bool(&pdev->dev, "linux,no-autorepeat"))
424 __set_bit(EV_REP, input_dev->evbit);
426 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
427 input_set_drvdata(input_dev, keypad);
429 err = input_register_device(keypad->input_dev);
433 wakeup = device_property_read_bool(&pdev->dev, "wakeup-source") ||
435 device_property_read_bool(&pdev->dev, "linux,wakeup");
436 device_init_wakeup(&pdev->dev, wakeup);
438 platform_set_drvdata(pdev, keypad);
444 static const struct of_device_id matrix_keypad_dt_match[] = {
445 { .compatible = "gpio-matrix-keypad" },
448 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
451 static struct platform_driver matrix_keypad_driver = {
452 .probe = matrix_keypad_probe,
454 .name = "matrix-keypad",
455 .pm = pm_sleep_ptr(&matrix_keypad_pm_ops),
456 .of_match_table = of_match_ptr(matrix_keypad_dt_match),
459 module_platform_driver(matrix_keypad_driver);
462 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
463 MODULE_LICENSE("GPL v2");
464 MODULE_ALIAS("platform:matrix-keypad");