matrix_keypad.c 14 KB

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