xref: /linux/drivers/input/keyboard/matrix_keypad.c (revision 0e7b4bc31d171856fcb753f653e0f00855763bf3)
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  */
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 
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 
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 
83 static bool row_asserted(struct matrix_keypad *keypad, int row)
84 {
85 	return gpiod_get_value_cansleep(keypad->row_gpios[row]);
86 }
87 
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 
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  */
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 	scoped_guard(spinlock_irq, &keypad->lock) {
162 		keypad->scan_pending = false;
163 		enable_row_irqs(keypad);
164 	}
165 }
166 
167 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
168 {
169 	struct matrix_keypad *keypad = id;
170 
171 	guard(spinlock_irqsave)(&keypad->lock);
172 
173 	/*
174 	 * See if another IRQ beaten us to it and scheduled the
175 	 * scan already. In that case we should not try to
176 	 * disable IRQs again.
177 	 */
178 	if (unlikely(keypad->scan_pending || keypad->stopped))
179 		goto out;
180 
181 	disable_row_irqs(keypad);
182 	keypad->scan_pending = true;
183 	schedule_delayed_work(&keypad->work,
184 			      msecs_to_jiffies(keypad->debounce_ms));
185 
186 out:
187 	return IRQ_HANDLED;
188 }
189 
190 static int matrix_keypad_start(struct input_dev *dev)
191 {
192 	struct matrix_keypad *keypad = input_get_drvdata(dev);
193 
194 	keypad->stopped = false;
195 	mb();
196 
197 	/*
198 	 * Schedule an immediate key scan to capture current key state;
199 	 * columns will be activated and IRQs be enabled after the scan.
200 	 */
201 	schedule_delayed_work(&keypad->work, 0);
202 
203 	return 0;
204 }
205 
206 static void matrix_keypad_stop(struct input_dev *dev)
207 {
208 	struct matrix_keypad *keypad = input_get_drvdata(dev);
209 
210 	scoped_guard(spinlock_irq, &keypad->lock) {
211 		keypad->stopped = true;
212 	}
213 
214 	flush_delayed_work(&keypad->work);
215 	/*
216 	 * matrix_keypad_scan() will leave IRQs enabled;
217 	 * we should disable them now.
218 	 */
219 	disable_row_irqs(keypad);
220 }
221 
222 static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
223 {
224 	int i;
225 
226 	for_each_clear_bit(i, keypad->wakeup_enabled_irqs,
227 			   keypad->num_row_gpios)
228 		if (enable_irq_wake(keypad->row_irqs[i]) == 0)
229 			__set_bit(i, keypad->wakeup_enabled_irqs);
230 }
231 
232 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
233 {
234 	int i;
235 
236 	for_each_set_bit(i, keypad->wakeup_enabled_irqs,
237 			 keypad->num_row_gpios) {
238 		disable_irq_wake(keypad->row_irqs[i]);
239 		__clear_bit(i, keypad->wakeup_enabled_irqs);
240 	}
241 }
242 
243 static int matrix_keypad_suspend(struct device *dev)
244 {
245 	struct platform_device *pdev = to_platform_device(dev);
246 	struct matrix_keypad *keypad = platform_get_drvdata(pdev);
247 
248 	matrix_keypad_stop(keypad->input_dev);
249 
250 	if (device_may_wakeup(&pdev->dev))
251 		matrix_keypad_enable_wakeup(keypad);
252 
253 	return 0;
254 }
255 
256 static int matrix_keypad_resume(struct device *dev)
257 {
258 	struct platform_device *pdev = to_platform_device(dev);
259 	struct matrix_keypad *keypad = platform_get_drvdata(pdev);
260 
261 	if (device_may_wakeup(&pdev->dev))
262 		matrix_keypad_disable_wakeup(keypad);
263 
264 	matrix_keypad_start(keypad->input_dev);
265 
266 	return 0;
267 }
268 
269 static DEFINE_SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
270 				matrix_keypad_suspend, matrix_keypad_resume);
271 
272 static int matrix_keypad_init_gpio(struct platform_device *pdev,
273 				   struct matrix_keypad *keypad)
274 {
275 	bool active_low;
276 	int nrow, ncol;
277 	int err;
278 	int i;
279 
280 	nrow = gpiod_count(&pdev->dev, "row");
281 	ncol = gpiod_count(&pdev->dev, "col");
282 	if (nrow < 0 || ncol < 0) {
283 		dev_err(&pdev->dev, "missing row or column GPIOs\n");
284 		return -EINVAL;
285 	}
286 
287 	keypad->num_row_gpios = nrow;
288 	keypad->num_col_gpios = ncol;
289 
290 	active_low = device_property_read_bool(&pdev->dev, "gpio-activelow");
291 
292 	/* initialize strobe lines as outputs, activated */
293 	for (i = 0; i < keypad->num_col_gpios; i++) {
294 		keypad->col_gpios[i] = devm_gpiod_get_index(&pdev->dev, "col",
295 							    i, GPIOD_ASIS);
296 		err = PTR_ERR_OR_ZERO(keypad->col_gpios[i]);
297 		if (err) {
298 			dev_err(&pdev->dev,
299 				"failed to request GPIO for COL%d: %d\n",
300 				i, err);
301 			return err;
302 		}
303 
304 		gpiod_set_consumer_name(keypad->col_gpios[i], "matrix_kbd_col");
305 
306 		if (active_low ^ gpiod_is_active_low(keypad->col_gpios[i]))
307 			gpiod_toggle_active_low(keypad->col_gpios[i]);
308 
309 		gpiod_direction_output(keypad->col_gpios[i], 1);
310 	}
311 
312 	for (i = 0; i < keypad->num_row_gpios; i++) {
313 		keypad->row_gpios[i] = devm_gpiod_get_index(&pdev->dev, "row",
314 							    i, GPIOD_IN);
315 		err = PTR_ERR_OR_ZERO(keypad->row_gpios[i]);
316 		if (err) {
317 			dev_err(&pdev->dev,
318 				"failed to request GPIO for ROW%d: %d\n",
319 				i, err);
320 			return err;
321 		}
322 
323 		gpiod_set_consumer_name(keypad->row_gpios[i], "matrix_kbd_row");
324 
325 		if (active_low ^ gpiod_is_active_low(keypad->row_gpios[i]))
326 			gpiod_toggle_active_low(keypad->row_gpios[i]);
327 	}
328 
329 	return 0;
330 }
331 
332 static int matrix_keypad_setup_interrupts(struct platform_device *pdev,
333 					  struct matrix_keypad *keypad)
334 {
335 	int err;
336 	int irq;
337 	int i;
338 
339 	for (i = 0; i < keypad->num_row_gpios; i++) {
340 		irq = gpiod_to_irq(keypad->row_gpios[i]);
341 		if (irq < 0) {
342 			err = irq;
343 			dev_err(&pdev->dev,
344 				"Unable to convert GPIO line %i to irq: %d\n",
345 				i, err);
346 			return err;
347 		}
348 
349 		err = devm_request_any_context_irq(&pdev->dev, irq,
350 						   matrix_keypad_interrupt,
351 						   IRQF_TRIGGER_RISING |
352 							IRQF_TRIGGER_FALLING,
353 						   "matrix-keypad", keypad);
354 		if (err < 0) {
355 			dev_err(&pdev->dev,
356 				"Unable to acquire interrupt for row %i: %d\n",
357 				i, err);
358 			return err;
359 		}
360 
361 		keypad->row_irqs[i] = irq;
362 	}
363 
364 	/* initialized as disabled - enabled by input->open */
365 	disable_row_irqs(keypad);
366 
367 	return 0;
368 }
369 
370 static int matrix_keypad_probe(struct platform_device *pdev)
371 {
372 	struct matrix_keypad *keypad;
373 	struct input_dev *input_dev;
374 	bool wakeup;
375 	int err;
376 
377 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
378 	if (!keypad)
379 		return -ENOMEM;
380 
381 	input_dev = devm_input_allocate_device(&pdev->dev);
382 	if (!input_dev)
383 		return -ENOMEM;
384 
385 	keypad->input_dev = input_dev;
386 	keypad->stopped = true;
387 	INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
388 	spin_lock_init(&keypad->lock);
389 
390 	keypad->drive_inactive_cols =
391 		device_property_read_bool(&pdev->dev, "drive-inactive-cols");
392 	device_property_read_u32(&pdev->dev, "debounce-delay-ms",
393 				 &keypad->debounce_ms);
394 	device_property_read_u32(&pdev->dev, "col-scan-delay-us",
395 				 &keypad->col_scan_delay_us);
396 
397 	err = matrix_keypad_init_gpio(pdev, keypad);
398 	if (err)
399 		return err;
400 
401 	keypad->row_shift = get_count_order(keypad->num_col_gpios);
402 
403 	err = matrix_keypad_setup_interrupts(pdev, keypad);
404 	if (err)
405 		return err;
406 
407 	input_dev->name		= pdev->name;
408 	input_dev->id.bustype	= BUS_HOST;
409 	input_dev->open		= matrix_keypad_start;
410 	input_dev->close	= matrix_keypad_stop;
411 
412 	err = matrix_keypad_build_keymap(NULL, NULL,
413 					 keypad->num_row_gpios,
414 					 keypad->num_col_gpios,
415 					 NULL, input_dev);
416 	if (err) {
417 		dev_err(&pdev->dev, "failed to build keymap\n");
418 		return -ENOMEM;
419 	}
420 
421 	if (!device_property_read_bool(&pdev->dev, "linux,no-autorepeat"))
422 		__set_bit(EV_REP, input_dev->evbit);
423 
424 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
425 	input_set_drvdata(input_dev, keypad);
426 
427 	err = input_register_device(keypad->input_dev);
428 	if (err)
429 		return err;
430 
431 	wakeup = device_property_read_bool(&pdev->dev, "wakeup-source") ||
432 		 /* legacy */
433 		 device_property_read_bool(&pdev->dev, "linux,wakeup");
434 	device_init_wakeup(&pdev->dev, wakeup);
435 
436 	platform_set_drvdata(pdev, keypad);
437 
438 	return 0;
439 }
440 
441 #ifdef CONFIG_OF
442 static const struct of_device_id matrix_keypad_dt_match[] = {
443 	{ .compatible = "gpio-matrix-keypad" },
444 	{ }
445 };
446 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
447 #endif
448 
449 static struct platform_driver matrix_keypad_driver = {
450 	.probe		= matrix_keypad_probe,
451 	.driver		= {
452 		.name	= "matrix-keypad",
453 		.pm	= pm_sleep_ptr(&matrix_keypad_pm_ops),
454 		.of_match_table = of_match_ptr(matrix_keypad_dt_match),
455 	},
456 };
457 module_platform_driver(matrix_keypad_driver);
458 
459 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
460 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
461 MODULE_LICENSE("GPL v2");
462 MODULE_ALIAS("platform:matrix-keypad");
463