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/gpio/consumer.h>
21 #include <linux/input/matrix_keypad.h>
22 #include <linux/slab.h>
23 #include <linux/of.h>
24
25 struct matrix_keypad {
26 struct input_dev *input_dev;
27 unsigned int row_shift;
28
29 unsigned int col_scan_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 udelay(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
row_asserted(struct matrix_keypad * keypad,int row)83 static bool row_asserted(struct matrix_keypad *keypad, int row)
84 {
85 return gpiod_get_value_cansleep(keypad->row_gpios[row]);
86 }
87
enable_row_irqs(struct matrix_keypad * keypad)88 static void enable_row_irqs(struct matrix_keypad *keypad)
89 {
90 int i;
91
92 for (i = 0; i < keypad->num_row_gpios; i++)
93 enable_irq(keypad->row_irqs[i]);
94 }
95
disable_row_irqs(struct matrix_keypad * keypad)96 static void disable_row_irqs(struct matrix_keypad *keypad)
97 {
98 int i;
99
100 for (i = 0; i < keypad->num_row_gpios; i++)
101 disable_irq_nosync(keypad->row_irqs[i]);
102 }
103
104 /*
105 * This gets the keys from keyboard and reports it to input subsystem
106 */
matrix_keypad_scan(struct work_struct * work)107 static void matrix_keypad_scan(struct work_struct *work)
108 {
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];
114 int row, col, code;
115
116 /* de-activate all columns for scanning */
117 activate_all_cols(keypad, false);
118
119 memset(new_state, 0, sizeof(new_state));
120
121 for (row = 0; row < keypad->num_row_gpios; row++)
122 gpiod_direction_input(keypad->row_gpios[row]);
123
124 /* assert each column and read the row status out */
125 for (col = 0; col < keypad->num_col_gpios; col++) {
126
127 activate_col(keypad, col, true);
128
129 for (row = 0; row < keypad->num_row_gpios; row++)
130 new_state[col] |=
131 row_asserted(keypad, row) ? BIT(row) : 0;
132
133 activate_col(keypad, col, false);
134 }
135
136 for (col = 0; col < keypad->num_col_gpios; col++) {
137 uint32_t bits_changed;
138
139 bits_changed = keypad->last_key_state[col] ^ new_state[col];
140 if (bits_changed == 0)
141 continue;
142
143 for (row = 0; row < keypad->num_row_gpios; row++) {
144 if (!(bits_changed & BIT(row)))
145 continue;
146
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,
150 keycodes[code],
151 new_state[col] & (1 << row));
152 }
153 }
154 input_sync(input_dev);
155
156 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
157
158 activate_all_cols(keypad, true);
159
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);
165 }
166
matrix_keypad_interrupt(int irq,void * id)167 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
168 {
169 struct matrix_keypad *keypad = id;
170 unsigned long flags;
171
172 spin_lock_irqsave(&keypad->lock, flags);
173
174 /*
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.
178 */
179 if (unlikely(keypad->scan_pending || keypad->stopped))
180 goto out;
181
182 disable_row_irqs(keypad);
183 keypad->scan_pending = true;
184 schedule_delayed_work(&keypad->work,
185 msecs_to_jiffies(keypad->debounce_ms));
186
187 out:
188 spin_unlock_irqrestore(&keypad->lock, flags);
189 return IRQ_HANDLED;
190 }
191
matrix_keypad_start(struct input_dev * dev)192 static int matrix_keypad_start(struct input_dev *dev)
193 {
194 struct matrix_keypad *keypad = input_get_drvdata(dev);
195
196 keypad->stopped = false;
197 mb();
198
199 /*
200 * Schedule an immediate key scan to capture current key state;
201 * columns will be activated and IRQs be enabled after the scan.
202 */
203 schedule_delayed_work(&keypad->work, 0);
204
205 return 0;
206 }
207
matrix_keypad_stop(struct input_dev * dev)208 static void matrix_keypad_stop(struct input_dev *dev)
209 {
210 struct matrix_keypad *keypad = input_get_drvdata(dev);
211
212 spin_lock_irq(&keypad->lock);
213 keypad->stopped = true;
214 spin_unlock_irq(&keypad->lock);
215
216 flush_delayed_work(&keypad->work);
217 /*
218 * matrix_keypad_scan() will leave IRQs enabled;
219 * we should disable them now.
220 */
221 disable_row_irqs(keypad);
222 }
223
matrix_keypad_enable_wakeup(struct matrix_keypad * keypad)224 static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
225 {
226 int i;
227
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);
232 }
233
matrix_keypad_disable_wakeup(struct matrix_keypad * keypad)234 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
235 {
236 int i;
237
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);
242 }
243 }
244
matrix_keypad_suspend(struct device * dev)245 static int matrix_keypad_suspend(struct device *dev)
246 {
247 struct platform_device *pdev = to_platform_device(dev);
248 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
249
250 matrix_keypad_stop(keypad->input_dev);
251
252 if (device_may_wakeup(&pdev->dev))
253 matrix_keypad_enable_wakeup(keypad);
254
255 return 0;
256 }
257
matrix_keypad_resume(struct device * dev)258 static int matrix_keypad_resume(struct device *dev)
259 {
260 struct platform_device *pdev = to_platform_device(dev);
261 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
262
263 if (device_may_wakeup(&pdev->dev))
264 matrix_keypad_disable_wakeup(keypad);
265
266 matrix_keypad_start(keypad->input_dev);
267
268 return 0;
269 }
270
271 static DEFINE_SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
272 matrix_keypad_suspend, matrix_keypad_resume);
273
matrix_keypad_init_gpio(struct platform_device * pdev,struct matrix_keypad * keypad)274 static int matrix_keypad_init_gpio(struct platform_device *pdev,
275 struct matrix_keypad *keypad)
276 {
277 bool active_low;
278 int nrow, ncol;
279 int err;
280 int i;
281
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");
286 return -EINVAL;
287 }
288
289 keypad->num_row_gpios = nrow;
290 keypad->num_col_gpios = ncol;
291
292 active_low = device_property_read_bool(&pdev->dev, "gpio-activelow");
293
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",
297 i, GPIOD_ASIS);
298 err = PTR_ERR_OR_ZERO(keypad->col_gpios[i]);
299 if (err) {
300 dev_err(&pdev->dev,
301 "failed to request GPIO for COL%d: %d\n",
302 i, err);
303 return err;
304 }
305
306 gpiod_set_consumer_name(keypad->col_gpios[i], "matrix_kbd_col");
307
308 if (active_low ^ gpiod_is_active_low(keypad->col_gpios[i]))
309 gpiod_toggle_active_low(keypad->col_gpios[i]);
310
311 gpiod_direction_output(keypad->col_gpios[i], 1);
312 }
313
314 for (i = 0; i < keypad->num_row_gpios; i++) {
315 keypad->row_gpios[i] = devm_gpiod_get_index(&pdev->dev, "row",
316 i, GPIOD_IN);
317 err = PTR_ERR_OR_ZERO(keypad->row_gpios[i]);
318 if (err) {
319 dev_err(&pdev->dev,
320 "failed to request GPIO for ROW%d: %d\n",
321 i, err);
322 return err;
323 }
324
325 gpiod_set_consumer_name(keypad->row_gpios[i], "matrix_kbd_row");
326
327 if (active_low ^ gpiod_is_active_low(keypad->row_gpios[i]))
328 gpiod_toggle_active_low(keypad->row_gpios[i]);
329 }
330
331 return 0;
332 }
333
matrix_keypad_setup_interrupts(struct platform_device * pdev,struct matrix_keypad * keypad)334 static int matrix_keypad_setup_interrupts(struct platform_device *pdev,
335 struct matrix_keypad *keypad)
336 {
337 int err;
338 int irq;
339 int i;
340
341 for (i = 0; i < keypad->num_row_gpios; i++) {
342 irq = gpiod_to_irq(keypad->row_gpios[i]);
343 if (irq < 0) {
344 err = irq;
345 dev_err(&pdev->dev,
346 "Unable to convert GPIO line %i to irq: %d\n",
347 i, err);
348 return err;
349 }
350
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);
356 if (err < 0) {
357 dev_err(&pdev->dev,
358 "Unable to acquire interrupt for row %i: %d\n",
359 i, err);
360 return err;
361 }
362
363 keypad->row_irqs[i] = irq;
364 }
365
366 /* initialized as disabled - enabled by input->open */
367 disable_row_irqs(keypad);
368
369 return 0;
370 }
371
matrix_keypad_probe(struct platform_device * pdev)372 static int matrix_keypad_probe(struct platform_device *pdev)
373 {
374 struct matrix_keypad *keypad;
375 struct input_dev *input_dev;
376 bool wakeup;
377 int err;
378
379 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
380 if (!keypad)
381 return -ENOMEM;
382
383 input_dev = devm_input_allocate_device(&pdev->dev);
384 if (!input_dev)
385 return -ENOMEM;
386
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);
391
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);
398
399 err = matrix_keypad_init_gpio(pdev, keypad);
400 if (err)
401 return err;
402
403 keypad->row_shift = get_count_order(keypad->num_col_gpios);
404
405 err = matrix_keypad_setup_interrupts(pdev, keypad);
406 if (err)
407 return err;
408
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;
413
414 err = matrix_keypad_build_keymap(NULL, NULL,
415 keypad->num_row_gpios,
416 keypad->num_col_gpios,
417 NULL, input_dev);
418 if (err) {
419 dev_err(&pdev->dev, "failed to build keymap\n");
420 return -ENOMEM;
421 }
422
423 if (!device_property_read_bool(&pdev->dev, "linux,no-autorepeat"))
424 __set_bit(EV_REP, input_dev->evbit);
425
426 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
427 input_set_drvdata(input_dev, keypad);
428
429 err = input_register_device(keypad->input_dev);
430 if (err)
431 return err;
432
433 wakeup = device_property_read_bool(&pdev->dev, "wakeup-source") ||
434 /* legacy */
435 device_property_read_bool(&pdev->dev, "linux,wakeup");
436 device_init_wakeup(&pdev->dev, wakeup);
437
438 platform_set_drvdata(pdev, keypad);
439
440 return 0;
441 }
442
443 #ifdef CONFIG_OF
444 static const struct of_device_id matrix_keypad_dt_match[] = {
445 { .compatible = "gpio-matrix-keypad" },
446 { }
447 };
448 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
449 #endif
450
451 static struct platform_driver matrix_keypad_driver = {
452 .probe = matrix_keypad_probe,
453 .driver = {
454 .name = "matrix-keypad",
455 .pm = pm_sleep_ptr(&matrix_keypad_pm_ops),
456 .of_match_table = of_match_ptr(matrix_keypad_dt_match),
457 },
458 };
459 module_platform_driver(matrix_keypad_driver);
460
461 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
462 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
463 MODULE_LICENSE("GPL v2");
464 MODULE_ALIAS("platform:matrix-keypad");
465