matrix_keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 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. bool gpio_all_disabled;
  36. unsigned short keycodes[];
  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_SLEEP
  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. #endif
  243. static SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
  244. matrix_keypad_suspend, matrix_keypad_resume);
  245. static int __devinit matrix_keypad_init_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;
  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_TRIGGER_RISING |
  286. IRQF_TRIGGER_FALLING,
  287. "matrix-keypad", keypad);
  288. if (err) {
  289. dev_err(&pdev->dev,
  290. "Unable to acquire interrupt for GPIO line %i\n",
  291. pdata->row_gpios[i]);
  292. goto err_free_irqs;
  293. }
  294. }
  295. }
  296. /* initialized as disabled - enabled by input->open */
  297. disable_row_irqs(keypad);
  298. return 0;
  299. err_free_irqs:
  300. while (--i >= 0)
  301. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  302. i = pdata->num_row_gpios;
  303. err_free_rows:
  304. while (--i >= 0)
  305. gpio_free(pdata->row_gpios[i]);
  306. i = pdata->num_col_gpios;
  307. err_free_cols:
  308. while (--i >= 0)
  309. gpio_free(pdata->col_gpios[i]);
  310. return err;
  311. }
  312. static void matrix_keypad_free_gpio(struct matrix_keypad *keypad)
  313. {
  314. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  315. int i;
  316. if (pdata->clustered_irq > 0) {
  317. free_irq(pdata->clustered_irq, keypad);
  318. } else {
  319. for (i = 0; i < pdata->num_row_gpios; i++)
  320. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  321. }
  322. for (i = 0; i < pdata->num_row_gpios; i++)
  323. gpio_free(pdata->row_gpios[i]);
  324. for (i = 0; i < pdata->num_col_gpios; i++)
  325. gpio_free(pdata->col_gpios[i]);
  326. }
  327. static int __devinit matrix_keypad_probe(struct platform_device *pdev)
  328. {
  329. const struct matrix_keypad_platform_data *pdata;
  330. const struct matrix_keymap_data *keymap_data;
  331. struct matrix_keypad *keypad;
  332. struct input_dev *input_dev;
  333. unsigned int row_shift;
  334. size_t keymap_size;
  335. int err;
  336. pdata = pdev->dev.platform_data;
  337. if (!pdata) {
  338. dev_err(&pdev->dev, "no platform data defined\n");
  339. return -EINVAL;
  340. }
  341. keymap_data = pdata->keymap_data;
  342. if (!keymap_data) {
  343. dev_err(&pdev->dev, "no keymap data defined\n");
  344. return -EINVAL;
  345. }
  346. row_shift = get_count_order(pdata->num_col_gpios);
  347. keymap_size = (pdata->num_row_gpios << row_shift) *
  348. sizeof(keypad->keycodes[0]);
  349. keypad = kzalloc(sizeof(struct matrix_keypad) + keymap_size,
  350. GFP_KERNEL);
  351. input_dev = input_allocate_device();
  352. if (!keypad || !input_dev) {
  353. err = -ENOMEM;
  354. goto err_free_mem;
  355. }
  356. keypad->input_dev = input_dev;
  357. keypad->pdata = pdata;
  358. keypad->row_shift = row_shift;
  359. keypad->stopped = true;
  360. INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
  361. spin_lock_init(&keypad->lock);
  362. input_dev->name = pdev->name;
  363. input_dev->id.bustype = BUS_HOST;
  364. input_dev->dev.parent = &pdev->dev;
  365. input_dev->open = matrix_keypad_start;
  366. input_dev->close = matrix_keypad_stop;
  367. err = matrix_keypad_build_keymap(keymap_data, NULL,
  368. pdata->num_row_gpios,
  369. pdata->num_col_gpios,
  370. keypad->keycodes, input_dev);
  371. if (err)
  372. goto err_free_mem;
  373. if (!pdata->no_autorepeat)
  374. __set_bit(EV_REP, input_dev->evbit);
  375. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  376. input_set_drvdata(input_dev, keypad);
  377. err = matrix_keypad_init_gpio(pdev, keypad);
  378. if (err)
  379. goto err_free_mem;
  380. err = input_register_device(keypad->input_dev);
  381. if (err)
  382. goto err_free_gpio;
  383. device_init_wakeup(&pdev->dev, pdata->wakeup);
  384. platform_set_drvdata(pdev, keypad);
  385. return 0;
  386. err_free_gpio:
  387. matrix_keypad_free_gpio(keypad);
  388. err_free_mem:
  389. input_free_device(input_dev);
  390. kfree(keypad);
  391. return err;
  392. }
  393. static int __devexit matrix_keypad_remove(struct platform_device *pdev)
  394. {
  395. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  396. device_init_wakeup(&pdev->dev, 0);
  397. matrix_keypad_free_gpio(keypad);
  398. input_unregister_device(keypad->input_dev);
  399. kfree(keypad);
  400. platform_set_drvdata(pdev, NULL);
  401. return 0;
  402. }
  403. static struct platform_driver matrix_keypad_driver = {
  404. .probe = matrix_keypad_probe,
  405. .remove = __devexit_p(matrix_keypad_remove),
  406. .driver = {
  407. .name = "matrix-keypad",
  408. .owner = THIS_MODULE,
  409. .pm = &matrix_keypad_pm_ops,
  410. },
  411. };
  412. module_platform_driver(matrix_keypad_driver);
  413. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  414. MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
  415. MODULE_LICENSE("GPL v2");
  416. MODULE_ALIAS("platform:matrix-keypad");