matrix_keypad.c 14 KB

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