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