matrix_keypad.c 11 KB

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