matrix_keypad.c 11 KB

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