matrix_keypad.c 13 KB

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