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