matrix_keypad.c 11 KB

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