matrix_keypad.c 12 KB

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