matrix_keypad.c 14 KB

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