matrix_keypad.c 14 KB

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