matrix_keypad.c 11 KB

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