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