matrix_keypad.c 14 KB

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