xref: /linux/drivers/input/keyboard/matrix_keypad.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
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 #include <linux/of_gpio.h>
24 #include <linux/of_platform.h>
25 
26 struct matrix_keypad {
27 	const struct matrix_keypad_platform_data *pdata;
28 	struct input_dev *input_dev;
29 	unsigned int row_shift;
30 
31 	unsigned int row_irqs[MATRIX_MAX_ROWS];
32 	unsigned int num_row_irqs;
33 	DECLARE_BITMAP(wakeup_enabled_irqs, MATRIX_MAX_ROWS);
34 
35 	uint32_t last_key_state[MATRIX_MAX_COLS];
36 	struct delayed_work work;
37 	spinlock_t lock;
38 	bool scan_pending;
39 	bool stopped;
40 };
41 
42 /*
43  * NOTE: If drive_inactive_cols is false, then the GPIO has to be put into
44  * HiZ when de-activated to cause minmal side effect when scanning other
45  * columns. In that case it is configured here to be input, otherwise it is
46  * driven with the inactive value.
47  */
48 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
49 			   int col, bool on)
50 {
51 	bool level_on = !pdata->active_low;
52 
53 	if (on) {
54 		gpio_direction_output(pdata->col_gpios[col], level_on);
55 	} else {
56 		gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
57 		if (!pdata->drive_inactive_cols)
58 			gpio_direction_input(pdata->col_gpios[col]);
59 	}
60 }
61 
62 static void activate_col(const struct matrix_keypad_platform_data *pdata,
63 			 int col, bool on)
64 {
65 	__activate_col(pdata, col, on);
66 
67 	if (on && pdata->col_scan_delay_us)
68 		udelay(pdata->col_scan_delay_us);
69 }
70 
71 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
72 			      bool on)
73 {
74 	int col;
75 
76 	for (col = 0; col < pdata->num_col_gpios; col++)
77 		__activate_col(pdata, col, on);
78 }
79 
80 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
81 			 int row)
82 {
83 	return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
84 			!pdata->active_low : pdata->active_low;
85 }
86 
87 static void enable_row_irqs(struct matrix_keypad *keypad)
88 {
89 	int i;
90 
91 	for (i = 0; i < keypad->num_row_irqs; i++)
92 		enable_irq(keypad->row_irqs[i]);
93 }
94 
95 static void disable_row_irqs(struct matrix_keypad *keypad)
96 {
97 	int i;
98 
99 	for (i = 0; i < keypad->num_row_irqs; i++)
100 		disable_irq_nosync(keypad->row_irqs[i]);
101 }
102 
103 /*
104  * This gets the keys from keyboard and reports it to input subsystem
105  */
106 static void matrix_keypad_scan(struct work_struct *work)
107 {
108 	struct matrix_keypad *keypad =
109 		container_of(work, struct matrix_keypad, work.work);
110 	struct input_dev *input_dev = keypad->input_dev;
111 	const unsigned short *keycodes = input_dev->keycode;
112 	const struct matrix_keypad_platform_data *pdata = keypad->pdata;
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(pdata, false);
118 
119 	memset(new_state, 0, sizeof(new_state));
120 
121 	for (row = 0; row < pdata->num_row_gpios; row++)
122 		gpio_direction_input(pdata->row_gpios[row]);
123 
124 	/* assert each column and read the row status out */
125 	for (col = 0; col < pdata->num_col_gpios; col++) {
126 
127 		activate_col(pdata, col, true);
128 
129 		for (row = 0; row < pdata->num_row_gpios; row++)
130 			new_state[col] |=
131 				row_asserted(pdata, row) ? (1 << row) : 0;
132 
133 		activate_col(pdata, col, false);
134 	}
135 
136 	for (col = 0; col < pdata->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 < pdata->num_row_gpios; row++) {
144 			if ((bits_changed & (1 << row)) == 0)
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(pdata, 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 
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->pdata->debounce_ms));
186 
187 out:
188 	spin_unlock_irqrestore(&keypad->lock, flags);
189 	return IRQ_HANDLED;
190 }
191 
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 
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 
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, keypad->num_row_irqs)
229 		if (enable_irq_wake(keypad->row_irqs[i]) == 0)
230 			__set_bit(i, keypad->wakeup_enabled_irqs);
231 }
232 
233 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
234 {
235 	int i;
236 
237 	for_each_set_bit(i, keypad->wakeup_enabled_irqs, keypad->num_row_irqs) {
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 	const struct matrix_keypad_platform_data *pdata = keypad->pdata;
276 	int i, irq, err;
277 
278 	/* initialized strobe lines as outputs, activated */
279 	for (i = 0; i < pdata->num_col_gpios; i++) {
280 		err = devm_gpio_request(&pdev->dev,
281 					pdata->col_gpios[i], "matrix_kbd_col");
282 		if (err) {
283 			dev_err(&pdev->dev,
284 				"failed to request GPIO%d for COL%d\n",
285 				pdata->col_gpios[i], i);
286 			return err;
287 		}
288 
289 		gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
290 	}
291 
292 	for (i = 0; i < pdata->num_row_gpios; i++) {
293 		err = devm_gpio_request(&pdev->dev,
294 					pdata->row_gpios[i], "matrix_kbd_row");
295 		if (err) {
296 			dev_err(&pdev->dev,
297 				"failed to request GPIO%d for ROW%d\n",
298 				pdata->row_gpios[i], i);
299 			return err;
300 		}
301 
302 		gpio_direction_input(pdata->row_gpios[i]);
303 	}
304 
305 	if (pdata->clustered_irq > 0) {
306 		err = devm_request_any_context_irq(&pdev->dev,
307 				pdata->clustered_irq,
308 				matrix_keypad_interrupt,
309 				pdata->clustered_irq_flags,
310 				"matrix-keypad", keypad);
311 		if (err < 0) {
312 			dev_err(&pdev->dev,
313 				"Unable to acquire clustered interrupt\n");
314 			return err;
315 		}
316 
317 		keypad->row_irqs[0] = pdata->clustered_irq;
318 		keypad->num_row_irqs = 1;
319 	} else {
320 		for (i = 0; i < pdata->num_row_gpios; i++) {
321 			irq = gpio_to_irq(pdata->row_gpios[i]);
322 			if (irq < 0) {
323 				err = irq;
324 				dev_err(&pdev->dev,
325 					"Unable to convert GPIO line %i to irq: %d\n",
326 					pdata->row_gpios[i], err);
327 				return err;
328 			}
329 
330 			err = devm_request_any_context_irq(&pdev->dev,
331 					irq,
332 					matrix_keypad_interrupt,
333 					IRQF_TRIGGER_RISING |
334 						IRQF_TRIGGER_FALLING,
335 					"matrix-keypad", keypad);
336 			if (err < 0) {
337 				dev_err(&pdev->dev,
338 					"Unable to acquire interrupt for GPIO line %i\n",
339 					pdata->row_gpios[i]);
340 				return err;
341 			}
342 
343 			keypad->row_irqs[i] = irq;
344 		}
345 
346 		keypad->num_row_irqs = pdata->num_row_gpios;
347 	}
348 
349 	/* initialized as disabled - enabled by input->open */
350 	disable_row_irqs(keypad);
351 
352 	return 0;
353 }
354 
355 #ifdef CONFIG_OF
356 static struct matrix_keypad_platform_data *
357 matrix_keypad_parse_dt(struct device *dev)
358 {
359 	struct matrix_keypad_platform_data *pdata;
360 	struct device_node *np = dev->of_node;
361 	unsigned int *gpios;
362 	int ret, i, nrow, ncol;
363 
364 	if (!np) {
365 		dev_err(dev, "device lacks DT data\n");
366 		return ERR_PTR(-ENODEV);
367 	}
368 
369 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
370 	if (!pdata) {
371 		dev_err(dev, "could not allocate memory for platform data\n");
372 		return ERR_PTR(-ENOMEM);
373 	}
374 
375 	pdata->num_row_gpios = nrow = gpiod_count(dev, "row");
376 	pdata->num_col_gpios = ncol = gpiod_count(dev, "col");
377 	if (nrow < 0 || ncol < 0) {
378 		dev_err(dev, "number of keypad rows/columns not specified\n");
379 		return ERR_PTR(-EINVAL);
380 	}
381 
382 	pdata->no_autorepeat = of_property_read_bool(np, "linux,no-autorepeat");
383 
384 	pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
385 			of_property_read_bool(np, "linux,wakeup"); /* legacy */
386 
387 	pdata->active_low = of_property_read_bool(np, "gpio-activelow");
388 
389 	pdata->drive_inactive_cols =
390 		of_property_read_bool(np, "drive-inactive-cols");
391 
392 	of_property_read_u32(np, "debounce-delay-ms", &pdata->debounce_ms);
393 	of_property_read_u32(np, "col-scan-delay-us",
394 						&pdata->col_scan_delay_us);
395 
396 	gpios = devm_kcalloc(dev,
397 			     pdata->num_row_gpios + pdata->num_col_gpios,
398 			     sizeof(unsigned int),
399 			     GFP_KERNEL);
400 	if (!gpios) {
401 		dev_err(dev, "could not allocate memory for gpios\n");
402 		return ERR_PTR(-ENOMEM);
403 	}
404 
405 	for (i = 0; i < nrow; i++) {
406 		ret = of_get_named_gpio(np, "row-gpios", i);
407 		if (ret < 0)
408 			return ERR_PTR(ret);
409 		gpios[i] = ret;
410 	}
411 
412 	for (i = 0; i < ncol; i++) {
413 		ret = of_get_named_gpio(np, "col-gpios", i);
414 		if (ret < 0)
415 			return ERR_PTR(ret);
416 		gpios[nrow + i] = ret;
417 	}
418 
419 	pdata->row_gpios = gpios;
420 	pdata->col_gpios = &gpios[pdata->num_row_gpios];
421 
422 	return pdata;
423 }
424 #else
425 static inline struct matrix_keypad_platform_data *
426 matrix_keypad_parse_dt(struct device *dev)
427 {
428 	dev_err(dev, "no platform data defined\n");
429 
430 	return ERR_PTR(-EINVAL);
431 }
432 #endif
433 
434 static int matrix_keypad_probe(struct platform_device *pdev)
435 {
436 	const struct matrix_keypad_platform_data *pdata;
437 	struct matrix_keypad *keypad;
438 	struct input_dev *input_dev;
439 	int err;
440 
441 	pdata = dev_get_platdata(&pdev->dev);
442 	if (!pdata) {
443 		pdata = matrix_keypad_parse_dt(&pdev->dev);
444 		if (IS_ERR(pdata))
445 			return PTR_ERR(pdata);
446 	} else if (!pdata->keymap_data) {
447 		dev_err(&pdev->dev, "no keymap data defined\n");
448 		return -EINVAL;
449 	}
450 
451 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
452 	if (!keypad)
453 		return -ENOMEM;
454 
455 	input_dev = devm_input_allocate_device(&pdev->dev);
456 	if (!input_dev)
457 		return -ENOMEM;
458 
459 	keypad->input_dev = input_dev;
460 	keypad->pdata = pdata;
461 	keypad->row_shift = get_count_order(pdata->num_col_gpios);
462 	keypad->stopped = true;
463 	INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
464 	spin_lock_init(&keypad->lock);
465 
466 	input_dev->name		= pdev->name;
467 	input_dev->id.bustype	= BUS_HOST;
468 	input_dev->open		= matrix_keypad_start;
469 	input_dev->close	= matrix_keypad_stop;
470 
471 	err = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
472 					 pdata->num_row_gpios,
473 					 pdata->num_col_gpios,
474 					 NULL, input_dev);
475 	if (err) {
476 		dev_err(&pdev->dev, "failed to build keymap\n");
477 		return -ENOMEM;
478 	}
479 
480 	if (!pdata->no_autorepeat)
481 		__set_bit(EV_REP, input_dev->evbit);
482 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
483 	input_set_drvdata(input_dev, keypad);
484 
485 	err = matrix_keypad_init_gpio(pdev, keypad);
486 	if (err)
487 		return err;
488 
489 	err = input_register_device(keypad->input_dev);
490 	if (err)
491 		return err;
492 
493 	device_init_wakeup(&pdev->dev, pdata->wakeup);
494 	platform_set_drvdata(pdev, keypad);
495 
496 	return 0;
497 }
498 
499 #ifdef CONFIG_OF
500 static const struct of_device_id matrix_keypad_dt_match[] = {
501 	{ .compatible = "gpio-matrix-keypad" },
502 	{ }
503 };
504 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
505 #endif
506 
507 static struct platform_driver matrix_keypad_driver = {
508 	.probe		= matrix_keypad_probe,
509 	.driver		= {
510 		.name	= "matrix-keypad",
511 		.pm	= pm_sleep_ptr(&matrix_keypad_pm_ops),
512 		.of_match_table = of_match_ptr(matrix_keypad_dt_match),
513 	},
514 };
515 module_platform_driver(matrix_keypad_driver);
516 
517 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
518 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
519 MODULE_LICENSE("GPL v2");
520 MODULE_ALIAS("platform:matrix-keypad");
521