1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * GPIO driven matrix keyboard driver
4 *
5 * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on corgikbd.c
8 */
9
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/input/matrix_keypad.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23
24 struct matrix_keypad {
25 struct input_dev *input_dev;
26 unsigned int row_shift;
27
28 unsigned int col_scan_delay_us;
29 unsigned int all_cols_on_delay_us;
30 /* key debounce interval in milli-second */
31 unsigned int debounce_ms;
32 bool drive_inactive_cols;
33
34 struct gpio_desc *row_gpios[MATRIX_MAX_ROWS];
35 unsigned int num_row_gpios;
36
37 struct gpio_desc *col_gpios[MATRIX_MAX_ROWS];
38 unsigned int num_col_gpios;
39
40 unsigned int row_irqs[MATRIX_MAX_ROWS];
41 DECLARE_BITMAP(wakeup_enabled_irqs, MATRIX_MAX_ROWS);
42
43 uint32_t last_key_state[MATRIX_MAX_COLS];
44 struct delayed_work work;
45 spinlock_t lock;
46 bool scan_pending;
47 bool stopped;
48 };
49
50 /*
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.
55 */
__activate_col(struct matrix_keypad * keypad,int col,bool on)56 static void __activate_col(struct matrix_keypad *keypad, int col, bool on)
57 {
58 if (on) {
59 gpiod_direction_output(keypad->col_gpios[col], 1);
60 } else {
61 gpiod_set_value_cansleep(keypad->col_gpios[col], 0);
62 if (!keypad->drive_inactive_cols)
63 gpiod_direction_input(keypad->col_gpios[col]);
64 }
65 }
66
activate_col(struct matrix_keypad * keypad,int col,bool on)67 static void activate_col(struct matrix_keypad *keypad, int col, bool on)
68 {
69 __activate_col(keypad, col, on);
70
71 if (on && keypad->col_scan_delay_us)
72 fsleep(keypad->col_scan_delay_us);
73 }
74
activate_all_cols(struct matrix_keypad * keypad,bool on)75 static void activate_all_cols(struct matrix_keypad *keypad, bool on)
76 {
77 int col;
78
79 for (col = 0; col < keypad->num_col_gpios; col++)
80 __activate_col(keypad, col, on);
81
82 if (on && keypad->all_cols_on_delay_us)
83 fsleep(keypad->all_cols_on_delay_us);
84 }
85
row_asserted(struct matrix_keypad * keypad,int row)86 static bool row_asserted(struct matrix_keypad *keypad, int row)
87 {
88 return gpiod_get_value_cansleep(keypad->row_gpios[row]);
89 }
90
enable_row_irqs(struct matrix_keypad * keypad)91 static void enable_row_irqs(struct matrix_keypad *keypad)
92 {
93 int i;
94
95 for (i = 0; i < keypad->num_row_gpios; i++)
96 enable_irq(keypad->row_irqs[i]);
97 }
98
disable_row_irqs(struct matrix_keypad * keypad)99 static void disable_row_irqs(struct matrix_keypad *keypad)
100 {
101 int i;
102
103 for (i = 0; i < keypad->num_row_gpios; i++)
104 disable_irq_nosync(keypad->row_irqs[i]);
105 }
106
107 /*
108 * This gets the keys from keyboard and reports it to input subsystem
109 */
matrix_keypad_scan(struct work_struct * work)110 static void matrix_keypad_scan(struct work_struct *work)
111 {
112 struct matrix_keypad *keypad =
113 container_of(work, struct matrix_keypad, work.work);
114 struct input_dev *input_dev = keypad->input_dev;
115 const unsigned short *keycodes = input_dev->keycode;
116 uint32_t new_state[MATRIX_MAX_COLS];
117 int row, col, code;
118
119 /* de-activate all columns for scanning */
120 activate_all_cols(keypad, false);
121
122 memset(new_state, 0, sizeof(new_state));
123
124 for (row = 0; row < keypad->num_row_gpios; row++)
125 gpiod_direction_input(keypad->row_gpios[row]);
126
127 /* assert each column and read the row status out */
128 for (col = 0; col < keypad->num_col_gpios; col++) {
129
130 activate_col(keypad, col, true);
131
132 for (row = 0; row < keypad->num_row_gpios; row++)
133 new_state[col] |=
134 row_asserted(keypad, row) ? BIT(row) : 0;
135
136 activate_col(keypad, col, false);
137 }
138
139 for (col = 0; col < keypad->num_col_gpios; col++) {
140 uint32_t bits_changed;
141
142 bits_changed = keypad->last_key_state[col] ^ new_state[col];
143 if (bits_changed == 0)
144 continue;
145
146 for (row = 0; row < keypad->num_row_gpios; row++) {
147 if (!(bits_changed & BIT(row)))
148 continue;
149
150 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
151 input_event(input_dev, EV_MSC, MSC_SCAN, code);
152 input_report_key(input_dev,
153 keycodes[code],
154 new_state[col] & (1 << row));
155 }
156 }
157 input_sync(input_dev);
158
159 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
160
161 activate_all_cols(keypad, true);
162
163 /* Enable IRQs again */
164 scoped_guard(spinlock_irq, &keypad->lock) {
165 keypad->scan_pending = false;
166 enable_row_irqs(keypad);
167 }
168 }
169
matrix_keypad_interrupt(int irq,void * id)170 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
171 {
172 struct matrix_keypad *keypad = id;
173
174 guard(spinlock_irqsave)(&keypad->lock);
175
176 /*
177 * See if another IRQ beaten us to it and scheduled the
178 * scan already. In that case we should not try to
179 * disable IRQs again.
180 */
181 if (unlikely(keypad->scan_pending || keypad->stopped))
182 goto out;
183
184 disable_row_irqs(keypad);
185 keypad->scan_pending = true;
186 schedule_delayed_work(&keypad->work,
187 msecs_to_jiffies(keypad->debounce_ms));
188
189 out:
190 return IRQ_HANDLED;
191 }
192
matrix_keypad_start(struct input_dev * dev)193 static int matrix_keypad_start(struct input_dev *dev)
194 {
195 struct matrix_keypad *keypad = input_get_drvdata(dev);
196
197 keypad->stopped = false;
198 mb();
199
200 /*
201 * Schedule an immediate key scan to capture current key state;
202 * columns will be activated and IRQs be enabled after the scan.
203 */
204 schedule_delayed_work(&keypad->work, 0);
205
206 return 0;
207 }
208
matrix_keypad_stop(struct input_dev * dev)209 static void matrix_keypad_stop(struct input_dev *dev)
210 {
211 struct matrix_keypad *keypad = input_get_drvdata(dev);
212
213 scoped_guard(spinlock_irq, &keypad->lock) {
214 keypad->stopped = true;
215 }
216
217 flush_delayed_work(&keypad->work);
218 /*
219 * matrix_keypad_scan() will leave IRQs enabled;
220 * we should disable them now.
221 */
222 disable_row_irqs(keypad);
223 }
224
matrix_keypad_enable_wakeup(struct matrix_keypad * keypad)225 static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
226 {
227 int i;
228
229 for_each_clear_bit(i, keypad->wakeup_enabled_irqs,
230 keypad->num_row_gpios)
231 if (enable_irq_wake(keypad->row_irqs[i]) == 0)
232 __set_bit(i, keypad->wakeup_enabled_irqs);
233 }
234
matrix_keypad_disable_wakeup(struct matrix_keypad * keypad)235 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
236 {
237 int i;
238
239 for_each_set_bit(i, keypad->wakeup_enabled_irqs,
240 keypad->num_row_gpios) {
241 disable_irq_wake(keypad->row_irqs[i]);
242 __clear_bit(i, keypad->wakeup_enabled_irqs);
243 }
244 }
245
matrix_keypad_suspend(struct device * dev)246 static int matrix_keypad_suspend(struct device *dev)
247 {
248 struct platform_device *pdev = to_platform_device(dev);
249 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
250
251 matrix_keypad_stop(keypad->input_dev);
252
253 if (device_may_wakeup(&pdev->dev))
254 matrix_keypad_enable_wakeup(keypad);
255
256 return 0;
257 }
258
matrix_keypad_resume(struct device * dev)259 static int matrix_keypad_resume(struct device *dev)
260 {
261 struct platform_device *pdev = to_platform_device(dev);
262 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
263
264 if (device_may_wakeup(&pdev->dev))
265 matrix_keypad_disable_wakeup(keypad);
266
267 matrix_keypad_start(keypad->input_dev);
268
269 return 0;
270 }
271
272 static DEFINE_SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
273 matrix_keypad_suspend, matrix_keypad_resume);
274
matrix_keypad_init_gpio(struct platform_device * pdev,struct matrix_keypad * keypad)275 static int matrix_keypad_init_gpio(struct platform_device *pdev,
276 struct matrix_keypad *keypad)
277 {
278 bool active_low;
279 int nrow, ncol;
280 int err;
281 int i;
282
283 nrow = gpiod_count(&pdev->dev, "row");
284 ncol = gpiod_count(&pdev->dev, "col");
285 if (nrow < 0 || ncol < 0) {
286 dev_err(&pdev->dev, "missing row or column GPIOs\n");
287 return -EINVAL;
288 }
289
290 keypad->num_row_gpios = nrow;
291 keypad->num_col_gpios = ncol;
292
293 active_low = device_property_read_bool(&pdev->dev, "gpio-activelow");
294
295 /* initialize strobe lines as outputs, activated */
296 for (i = 0; i < keypad->num_col_gpios; i++) {
297 keypad->col_gpios[i] = devm_gpiod_get_index(&pdev->dev, "col",
298 i, GPIOD_ASIS);
299 err = PTR_ERR_OR_ZERO(keypad->col_gpios[i]);
300 if (err) {
301 dev_err(&pdev->dev,
302 "failed to request GPIO for COL%d: %d\n",
303 i, err);
304 return err;
305 }
306
307 gpiod_set_consumer_name(keypad->col_gpios[i], "matrix_kbd_col");
308
309 if (active_low ^ gpiod_is_active_low(keypad->col_gpios[i]))
310 gpiod_toggle_active_low(keypad->col_gpios[i]);
311
312 gpiod_direction_output(keypad->col_gpios[i], 1);
313 }
314
315 for (i = 0; i < keypad->num_row_gpios; i++) {
316 keypad->row_gpios[i] = devm_gpiod_get_index(&pdev->dev, "row",
317 i, GPIOD_IN);
318 err = PTR_ERR_OR_ZERO(keypad->row_gpios[i]);
319 if (err) {
320 dev_err(&pdev->dev,
321 "failed to request GPIO for ROW%d: %d\n",
322 i, err);
323 return err;
324 }
325
326 gpiod_set_consumer_name(keypad->row_gpios[i], "matrix_kbd_row");
327
328 if (active_low ^ gpiod_is_active_low(keypad->row_gpios[i]))
329 gpiod_toggle_active_low(keypad->row_gpios[i]);
330 }
331
332 return 0;
333 }
334
matrix_keypad_setup_interrupts(struct platform_device * pdev,struct matrix_keypad * keypad)335 static int matrix_keypad_setup_interrupts(struct platform_device *pdev,
336 struct matrix_keypad *keypad)
337 {
338 int err;
339 int irq;
340 int i;
341
342 for (i = 0; i < keypad->num_row_gpios; i++) {
343 irq = gpiod_to_irq(keypad->row_gpios[i]);
344 if (irq < 0) {
345 err = irq;
346 dev_err(&pdev->dev,
347 "Unable to convert GPIO line %i to irq: %d\n",
348 i, err);
349 return err;
350 }
351
352 err = devm_request_any_context_irq(&pdev->dev, irq,
353 matrix_keypad_interrupt,
354 IRQF_TRIGGER_RISING |
355 IRQF_TRIGGER_FALLING,
356 "matrix-keypad", keypad);
357 if (err < 0) {
358 dev_err(&pdev->dev,
359 "Unable to acquire interrupt for row %i: %d\n",
360 i, err);
361 return err;
362 }
363
364 keypad->row_irqs[i] = irq;
365 }
366
367 /* initialized as disabled - enabled by input->open */
368 disable_row_irqs(keypad);
369
370 return 0;
371 }
372
matrix_keypad_probe(struct platform_device * pdev)373 static int matrix_keypad_probe(struct platform_device *pdev)
374 {
375 struct matrix_keypad *keypad;
376 struct input_dev *input_dev;
377 bool wakeup;
378 int err;
379
380 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
381 if (!keypad)
382 return -ENOMEM;
383
384 input_dev = devm_input_allocate_device(&pdev->dev);
385 if (!input_dev)
386 return -ENOMEM;
387
388 keypad->input_dev = input_dev;
389 keypad->stopped = true;
390 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
391 spin_lock_init(&keypad->lock);
392
393 keypad->drive_inactive_cols =
394 device_property_read_bool(&pdev->dev, "drive-inactive-cols");
395 device_property_read_u32(&pdev->dev, "debounce-delay-ms",
396 &keypad->debounce_ms);
397 device_property_read_u32(&pdev->dev, "col-scan-delay-us",
398 &keypad->col_scan_delay_us);
399 device_property_read_u32(&pdev->dev, "all-cols-on-delay-us",
400 &keypad->all_cols_on_delay_us);
401
402 err = matrix_keypad_init_gpio(pdev, keypad);
403 if (err)
404 return err;
405
406 keypad->row_shift = get_count_order(keypad->num_col_gpios);
407
408 err = matrix_keypad_setup_interrupts(pdev, keypad);
409 if (err)
410 return err;
411
412 input_dev->name = pdev->name;
413 input_dev->id.bustype = BUS_HOST;
414 input_dev->open = matrix_keypad_start;
415 input_dev->close = matrix_keypad_stop;
416
417 err = matrix_keypad_build_keymap(NULL, NULL,
418 keypad->num_row_gpios,
419 keypad->num_col_gpios,
420 NULL, input_dev);
421 if (err) {
422 dev_err(&pdev->dev, "failed to build keymap\n");
423 return -ENOMEM;
424 }
425
426 if (!device_property_read_bool(&pdev->dev, "linux,no-autorepeat"))
427 __set_bit(EV_REP, input_dev->evbit);
428
429 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
430 input_set_drvdata(input_dev, keypad);
431
432 err = input_register_device(keypad->input_dev);
433 if (err)
434 return err;
435
436 wakeup = device_property_read_bool(&pdev->dev, "wakeup-source") ||
437 /* legacy */
438 device_property_read_bool(&pdev->dev, "linux,wakeup");
439 device_init_wakeup(&pdev->dev, wakeup);
440
441 platform_set_drvdata(pdev, keypad);
442
443 return 0;
444 }
445
446 #ifdef CONFIG_OF
447 static const struct of_device_id matrix_keypad_dt_match[] = {
448 { .compatible = "gpio-matrix-keypad" },
449 { }
450 };
451 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
452 #endif
453
454 static struct platform_driver matrix_keypad_driver = {
455 .probe = matrix_keypad_probe,
456 .driver = {
457 .name = "matrix-keypad",
458 .pm = pm_sleep_ptr(&matrix_keypad_pm_ops),
459 .of_match_table = of_match_ptr(matrix_keypad_dt_match),
460 },
461 };
462 module_platform_driver(matrix_keypad_driver);
463
464 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
465 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
466 MODULE_LICENSE("GPL v2");
467 MODULE_ALIAS("platform:matrix-keypad");
468