matrix_keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/init.h>
  17. #include <linux/input.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/module.h>
  22. #include <linux/gpio.h>
  23. #include <linux/input/matrix_keypad.h>
  24. struct matrix_keypad {
  25. const struct matrix_keypad_platform_data *pdata;
  26. struct input_dev *input_dev;
  27. unsigned short *keycodes;
  28. unsigned int row_shift;
  29. uint32_t last_key_state[MATRIX_MAX_COLS];
  30. struct delayed_work work;
  31. bool scan_pending;
  32. bool stopped;
  33. spinlock_t lock;
  34. };
  35. /*
  36. * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
  37. * minmal side effect when scanning other columns, here it is configured to
  38. * be input, and it should work on most platforms.
  39. */
  40. static void __activate_col(const struct matrix_keypad_platform_data *pdata,
  41. int col, bool on)
  42. {
  43. bool level_on = !pdata->active_low;
  44. if (on) {
  45. gpio_direction_output(pdata->col_gpios[col], level_on);
  46. } else {
  47. gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
  48. gpio_direction_input(pdata->col_gpios[col]);
  49. }
  50. }
  51. static void activate_col(const struct matrix_keypad_platform_data *pdata,
  52. int col, bool on)
  53. {
  54. __activate_col(pdata, col, on);
  55. if (on && pdata->col_scan_delay_us)
  56. udelay(pdata->col_scan_delay_us);
  57. }
  58. static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
  59. bool on)
  60. {
  61. int col;
  62. for (col = 0; col < pdata->num_col_gpios; col++)
  63. __activate_col(pdata, col, on);
  64. }
  65. static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
  66. int row)
  67. {
  68. return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
  69. !pdata->active_low : pdata->active_low;
  70. }
  71. static void enable_row_irqs(struct matrix_keypad *keypad)
  72. {
  73. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  74. int i;
  75. for (i = 0; i < pdata->num_row_gpios; i++)
  76. enable_irq(gpio_to_irq(pdata->row_gpios[i]));
  77. }
  78. static void disable_row_irqs(struct matrix_keypad *keypad)
  79. {
  80. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  81. int i;
  82. for (i = 0; i < pdata->num_row_gpios; i++)
  83. disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
  84. }
  85. /*
  86. * This gets the keys from keyboard and reports it to input subsystem
  87. */
  88. static void matrix_keypad_scan(struct work_struct *work)
  89. {
  90. struct matrix_keypad *keypad =
  91. container_of(work, struct matrix_keypad, work.work);
  92. struct input_dev *input_dev = keypad->input_dev;
  93. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  94. uint32_t new_state[MATRIX_MAX_COLS];
  95. int row, col, code;
  96. /* de-activate all columns for scanning */
  97. activate_all_cols(pdata, false);
  98. memset(new_state, 0, sizeof(new_state));
  99. /* assert each column and read the row status out */
  100. for (col = 0; col < pdata->num_col_gpios; col++) {
  101. activate_col(pdata, col, true);
  102. for (row = 0; row < pdata->num_row_gpios; row++)
  103. new_state[col] |=
  104. row_asserted(pdata, row) ? (1 << row) : 0;
  105. activate_col(pdata, col, false);
  106. }
  107. for (col = 0; col < pdata->num_col_gpios; col++) {
  108. uint32_t bits_changed;
  109. bits_changed = keypad->last_key_state[col] ^ new_state[col];
  110. if (bits_changed == 0)
  111. continue;
  112. for (row = 0; row < pdata->num_row_gpios; row++) {
  113. if ((bits_changed & (1 << row)) == 0)
  114. continue;
  115. code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  116. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  117. input_report_key(input_dev,
  118. keypad->keycodes[code],
  119. new_state[col] & (1 << row));
  120. }
  121. }
  122. input_sync(input_dev);
  123. memcpy(keypad->last_key_state, new_state, sizeof(new_state));
  124. activate_all_cols(pdata, true);
  125. /* Enable IRQs again */
  126. spin_lock_irq(&keypad->lock);
  127. keypad->scan_pending = false;
  128. enable_row_irqs(keypad);
  129. spin_unlock_irq(&keypad->lock);
  130. }
  131. static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
  132. {
  133. struct matrix_keypad *keypad = id;
  134. unsigned long flags;
  135. spin_lock_irqsave(&keypad->lock, flags);
  136. /*
  137. * See if another IRQ beaten us to it and scheduled the
  138. * scan already. In that case we should not try to
  139. * disable IRQs again.
  140. */
  141. if (unlikely(keypad->scan_pending || keypad->stopped))
  142. goto out;
  143. disable_row_irqs(keypad);
  144. keypad->scan_pending = true;
  145. schedule_delayed_work(&keypad->work,
  146. msecs_to_jiffies(keypad->pdata->debounce_ms));
  147. out:
  148. spin_unlock_irqrestore(&keypad->lock, flags);
  149. return IRQ_HANDLED;
  150. }
  151. static int matrix_keypad_start(struct input_dev *dev)
  152. {
  153. struct matrix_keypad *keypad = input_get_drvdata(dev);
  154. keypad->stopped = false;
  155. mb();
  156. /*
  157. * Schedule an immediate key scan to capture current key state;
  158. * columns will be activated and IRQs be enabled after the scan.
  159. */
  160. schedule_delayed_work(&keypad->work, 0);
  161. return 0;
  162. }
  163. static void matrix_keypad_stop(struct input_dev *dev)
  164. {
  165. struct matrix_keypad *keypad = input_get_drvdata(dev);
  166. keypad->stopped = true;
  167. mb();
  168. flush_work(&keypad->work.work);
  169. /*
  170. * matrix_keypad_scan() will leave IRQs enabled;
  171. * we should disable them now.
  172. */
  173. disable_row_irqs(keypad);
  174. }
  175. #ifdef CONFIG_PM
  176. static int matrix_keypad_suspend(struct platform_device *pdev, pm_message_t state)
  177. {
  178. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  179. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  180. int i;
  181. matrix_keypad_stop(keypad->input_dev);
  182. if (device_may_wakeup(&pdev->dev))
  183. for (i = 0; i < pdata->num_row_gpios; i++)
  184. enable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
  185. return 0;
  186. }
  187. static int matrix_keypad_resume(struct platform_device *pdev)
  188. {
  189. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  190. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  191. int i;
  192. if (device_may_wakeup(&pdev->dev))
  193. for (i = 0; i < pdata->num_row_gpios; i++)
  194. disable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
  195. matrix_keypad_start(keypad->input_dev);
  196. return 0;
  197. }
  198. #else
  199. #define matrix_keypad_suspend NULL
  200. #define matrix_keypad_resume NULL
  201. #endif
  202. static int __devinit init_matrix_gpio(struct platform_device *pdev,
  203. struct matrix_keypad *keypad)
  204. {
  205. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  206. int i, err = -EINVAL;
  207. /* initialized strobe lines as outputs, activated */
  208. for (i = 0; i < pdata->num_col_gpios; i++) {
  209. err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
  210. if (err) {
  211. dev_err(&pdev->dev,
  212. "failed to request GPIO%d for COL%d\n",
  213. pdata->col_gpios[i], i);
  214. goto err_free_cols;
  215. }
  216. gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
  217. }
  218. for (i = 0; i < pdata->num_row_gpios; i++) {
  219. err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
  220. if (err) {
  221. dev_err(&pdev->dev,
  222. "failed to request GPIO%d for ROW%d\n",
  223. pdata->row_gpios[i], i);
  224. goto err_free_rows;
  225. }
  226. gpio_direction_input(pdata->row_gpios[i]);
  227. }
  228. for (i = 0; i < pdata->num_row_gpios; i++) {
  229. err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
  230. matrix_keypad_interrupt,
  231. IRQF_DISABLED |
  232. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  233. "matrix-keypad", keypad);
  234. if (err) {
  235. dev_err(&pdev->dev,
  236. "Unable to acquire interrupt for GPIO line %i\n",
  237. pdata->row_gpios[i]);
  238. goto err_free_irqs;
  239. }
  240. }
  241. /* initialized as disabled - enabled by input->open */
  242. disable_row_irqs(keypad);
  243. return 0;
  244. err_free_irqs:
  245. while (--i >= 0)
  246. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  247. i = pdata->num_row_gpios;
  248. err_free_rows:
  249. while (--i >= 0)
  250. gpio_free(pdata->row_gpios[i]);
  251. i = pdata->num_col_gpios;
  252. err_free_cols:
  253. while (--i >= 0)
  254. gpio_free(pdata->col_gpios[i]);
  255. return err;
  256. }
  257. static int __devinit matrix_keypad_probe(struct platform_device *pdev)
  258. {
  259. const struct matrix_keypad_platform_data *pdata;
  260. const struct matrix_keymap_data *keymap_data;
  261. struct matrix_keypad *keypad;
  262. struct input_dev *input_dev;
  263. unsigned short *keycodes;
  264. unsigned int row_shift;
  265. int err;
  266. pdata = pdev->dev.platform_data;
  267. if (!pdata) {
  268. dev_err(&pdev->dev, "no platform data defined\n");
  269. return -EINVAL;
  270. }
  271. keymap_data = pdata->keymap_data;
  272. if (!keymap_data) {
  273. dev_err(&pdev->dev, "no keymap data defined\n");
  274. return -EINVAL;
  275. }
  276. row_shift = get_count_order(pdata->num_col_gpios);
  277. keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
  278. keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
  279. sizeof(*keycodes),
  280. GFP_KERNEL);
  281. input_dev = input_allocate_device();
  282. if (!keypad || !keycodes || !input_dev) {
  283. err = -ENOMEM;
  284. goto err_free_mem;
  285. }
  286. keypad->input_dev = input_dev;
  287. keypad->pdata = pdata;
  288. keypad->keycodes = keycodes;
  289. keypad->row_shift = row_shift;
  290. keypad->stopped = true;
  291. INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
  292. spin_lock_init(&keypad->lock);
  293. input_dev->name = pdev->name;
  294. input_dev->id.bustype = BUS_HOST;
  295. input_dev->dev.parent = &pdev->dev;
  296. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  297. input_dev->open = matrix_keypad_start;
  298. input_dev->close = matrix_keypad_stop;
  299. input_dev->keycode = keycodes;
  300. input_dev->keycodesize = sizeof(*keycodes);
  301. input_dev->keycodemax = pdata->num_row_gpios << row_shift;
  302. matrix_keypad_build_keymap(keymap_data, row_shift,
  303. input_dev->keycode, input_dev->keybit);
  304. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  305. input_set_drvdata(input_dev, keypad);
  306. err = init_matrix_gpio(pdev, keypad);
  307. if (err)
  308. goto err_free_mem;
  309. err = input_register_device(keypad->input_dev);
  310. if (err)
  311. goto err_free_mem;
  312. device_init_wakeup(&pdev->dev, pdata->wakeup);
  313. platform_set_drvdata(pdev, keypad);
  314. return 0;
  315. err_free_mem:
  316. input_free_device(input_dev);
  317. kfree(keycodes);
  318. kfree(keypad);
  319. return err;
  320. }
  321. static int __devexit matrix_keypad_remove(struct platform_device *pdev)
  322. {
  323. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  324. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  325. int i;
  326. device_init_wakeup(&pdev->dev, 0);
  327. for (i = 0; i < pdata->num_row_gpios; i++) {
  328. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  329. gpio_free(pdata->row_gpios[i]);
  330. }
  331. for (i = 0; i < pdata->num_col_gpios; i++)
  332. gpio_free(pdata->col_gpios[i]);
  333. input_unregister_device(keypad->input_dev);
  334. platform_set_drvdata(pdev, NULL);
  335. kfree(keypad->keycodes);
  336. kfree(keypad);
  337. return 0;
  338. }
  339. static struct platform_driver matrix_keypad_driver = {
  340. .probe = matrix_keypad_probe,
  341. .remove = __devexit_p(matrix_keypad_remove),
  342. .suspend = matrix_keypad_suspend,
  343. .resume = matrix_keypad_resume,
  344. .driver = {
  345. .name = "matrix-keypad",
  346. .owner = THIS_MODULE,
  347. },
  348. };
  349. static int __init matrix_keypad_init(void)
  350. {
  351. return platform_driver_register(&matrix_keypad_driver);
  352. }
  353. static void __exit matrix_keypad_exit(void)
  354. {
  355. platform_driver_unregister(&matrix_keypad_driver);
  356. }
  357. module_init(matrix_keypad_init);
  358. module_exit(matrix_keypad_exit);
  359. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  360. MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
  361. MODULE_LICENSE("GPL v2");
  362. MODULE_ALIAS("platform:matrix-keypad");