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/input/matrix_keypad.h>
21 #include <linux/slab.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_platform.h>
26 struct matrix_keypad {
27 const struct matrix_keypad_platform_data *pdata;
28 struct input_dev *input_dev;
29 unsigned int row_shift;
31 unsigned int row_irqs[MATRIX_MAX_ROWS];
32 unsigned int num_row_irqs;
33 DECLARE_BITMAP(wakeup_enabled_irqs, MATRIX_MAX_ROWS);
35 uint32_t last_key_state[MATRIX_MAX_COLS];
36 struct delayed_work work;
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.
48 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
51 bool level_on = !pdata->active_low;
54 gpio_direction_output(pdata->col_gpios[col], level_on);
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]);
62 static void activate_col(const struct matrix_keypad_platform_data *pdata,
65 __activate_col(pdata, col, on);
67 if (on && pdata->col_scan_delay_us)
68 udelay(pdata->col_scan_delay_us);
71 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
76 for (col = 0; col < pdata->num_col_gpios; col++)
77 __activate_col(pdata, col, on);
80 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
83 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
84 !pdata->active_low : pdata->active_low;
87 static void enable_row_irqs(struct matrix_keypad *keypad)
91 for (i = 0; i < keypad->num_row_irqs; i++)
92 enable_irq(keypad->row_irqs[i]);
95 static void disable_row_irqs(struct matrix_keypad *keypad)
99 for (i = 0; i < keypad->num_row_irqs; i++)
100 disable_irq_nosync(keypad->row_irqs[i]);
104 * This gets the keys from keyboard and reports it to input subsystem
106 static void matrix_keypad_scan(struct work_struct *work)
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];
116 /* de-activate all columns for scanning */
117 activate_all_cols(pdata, false);
119 memset(new_state, 0, sizeof(new_state));
121 for (row = 0; row < pdata->num_row_gpios; row++)
122 gpio_direction_input(pdata->row_gpios[row]);
124 /* assert each column and read the row status out */
125 for (col = 0; col < pdata->num_col_gpios; col++) {
127 activate_col(pdata, col, true);
129 for (row = 0; row < pdata->num_row_gpios; row++)
131 row_asserted(pdata, row) ? (1 << row) : 0;
133 activate_col(pdata, col, false);
136 for (col = 0; col < pdata->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 < pdata->num_row_gpios; row++) {
144 if ((bits_changed & (1 << row)) == 0)
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(pdata, 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->pdata->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, keypad->num_row_irqs)
229 if (enable_irq_wake(keypad->row_irqs[i]) == 0)
230 __set_bit(i, keypad->wakeup_enabled_irqs);
233 static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
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);
243 static int matrix_keypad_suspend(struct device *dev)
245 struct platform_device *pdev = to_platform_device(dev);
246 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
248 matrix_keypad_stop(keypad->input_dev);
250 if (device_may_wakeup(&pdev->dev))
251 matrix_keypad_enable_wakeup(keypad);
256 static int matrix_keypad_resume(struct device *dev)
258 struct platform_device *pdev = to_platform_device(dev);
259 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
261 if (device_may_wakeup(&pdev->dev))
262 matrix_keypad_disable_wakeup(keypad);
264 matrix_keypad_start(keypad->input_dev);
269 static DEFINE_SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
270 matrix_keypad_suspend, matrix_keypad_resume);
272 static int matrix_keypad_init_gpio(struct platform_device *pdev,
273 struct matrix_keypad *keypad)
275 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
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");
284 "failed to request GPIO%d for COL%d\n",
285 pdata->col_gpios[i], i);
289 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
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");
297 "failed to request GPIO%d for ROW%d\n",
298 pdata->row_gpios[i], i);
302 gpio_direction_input(pdata->row_gpios[i]);
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);
313 "Unable to acquire clustered interrupt\n");
317 keypad->row_irqs[0] = pdata->clustered_irq;
318 keypad->num_row_irqs = 1;
320 for (i = 0; i < pdata->num_row_gpios; i++) {
321 irq = gpio_to_irq(pdata->row_gpios[i]);
325 "Unable to convert GPIO line %i to irq: %d\n",
326 pdata->row_gpios[i], err);
330 err = devm_request_any_context_irq(&pdev->dev,
332 matrix_keypad_interrupt,
333 IRQF_TRIGGER_RISING |
334 IRQF_TRIGGER_FALLING,
335 "matrix-keypad", keypad);
338 "Unable to acquire interrupt for GPIO line %i\n",
339 pdata->row_gpios[i]);
343 keypad->row_irqs[i] = irq;
346 keypad->num_row_irqs = pdata->num_row_gpios;
349 /* initialized as disabled - enabled by input->open */
350 disable_row_irqs(keypad);
356 static struct matrix_keypad_platform_data *
357 matrix_keypad_parse_dt(struct device *dev)
359 struct matrix_keypad_platform_data *pdata;
360 struct device_node *np = dev->of_node;
362 int ret, i, nrow, ncol;
365 dev_err(dev, "device lacks DT data\n");
366 return ERR_PTR(-ENODEV);
369 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
371 dev_err(dev, "could not allocate memory for platform data\n");
372 return ERR_PTR(-ENOMEM);
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);
382 pdata->no_autorepeat = of_property_read_bool(np, "linux,no-autorepeat");
384 pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
385 of_property_read_bool(np, "linux,wakeup"); /* legacy */
387 pdata->active_low = of_property_read_bool(np, "gpio-activelow");
389 pdata->drive_inactive_cols =
390 of_property_read_bool(np, "drive-inactive-cols");
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);
396 gpios = devm_kcalloc(dev,
397 pdata->num_row_gpios + pdata->num_col_gpios,
398 sizeof(unsigned int),
401 dev_err(dev, "could not allocate memory for gpios\n");
402 return ERR_PTR(-ENOMEM);
405 for (i = 0; i < nrow; i++) {
406 ret = of_get_named_gpio(np, "row-gpios", i);
412 for (i = 0; i < ncol; i++) {
413 ret = of_get_named_gpio(np, "col-gpios", i);
416 gpios[nrow + i] = ret;
419 pdata->row_gpios = gpios;
420 pdata->col_gpios = &gpios[pdata->num_row_gpios];
425 static inline struct matrix_keypad_platform_data *
426 matrix_keypad_parse_dt(struct device *dev)
428 dev_err(dev, "no platform data defined\n");
430 return ERR_PTR(-EINVAL);
434 static int matrix_keypad_probe(struct platform_device *pdev)
436 const struct matrix_keypad_platform_data *pdata;
437 struct matrix_keypad *keypad;
438 struct input_dev *input_dev;
441 pdata = dev_get_platdata(&pdev->dev);
443 pdata = matrix_keypad_parse_dt(&pdev->dev);
445 return PTR_ERR(pdata);
446 } else if (!pdata->keymap_data) {
447 dev_err(&pdev->dev, "no keymap data defined\n");
451 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
455 input_dev = devm_input_allocate_device(&pdev->dev);
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);
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;
471 err = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
472 pdata->num_row_gpios,
473 pdata->num_col_gpios,
476 dev_err(&pdev->dev, "failed to build keymap\n");
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);
485 err = matrix_keypad_init_gpio(pdev, keypad);
489 err = input_register_device(keypad->input_dev);
493 device_init_wakeup(&pdev->dev, pdata->wakeup);
494 platform_set_drvdata(pdev, keypad);
500 static const struct of_device_id matrix_keypad_dt_match[] = {
501 { .compatible = "gpio-matrix-keypad" },
504 MODULE_DEVICE_TABLE(of, matrix_keypad_dt_match);
507 static struct platform_driver matrix_keypad_driver = {
508 .probe = matrix_keypad_probe,
510 .name = "matrix-keypad",
511 .pm = pm_sleep_ptr(&matrix_keypad_pm_ops),
512 .of_match_table = of_match_ptr(matrix_keypad_dt_match),
515 module_platform_driver(matrix_keypad_driver);
518 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
519 MODULE_LICENSE("GPL v2");
520 MODULE_ALIAS("platform:matrix-keypad");