matrix_keypad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * GPIO driven matrix keyboard driver
  3. *
  4. * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
  5. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  6. *
  7. * Based on corgikbd.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/types.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/init.h>
  18. #include <linux/input.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/module.h>
  23. #include <linux/gpio.h>
  24. #include <linux/input/matrix_keypad.h>
  25. #include <linux/slab.h>
  26. #ifdef CONFIG_SEC_PATEK_PROJECT
  27. static int check_key_press;
  28. #endif
  29. struct matrix_keypad {
  30. const struct matrix_keypad_platform_data *pdata;
  31. struct input_dev *input_dev;
  32. unsigned short *keycodes;
  33. unsigned int row_shift;
  34. DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
  35. uint32_t last_key_state[MATRIX_MAX_COLS];
  36. struct delayed_work work;
  37. struct mutex lock;
  38. bool scan_pending;
  39. bool stopped;
  40. bool gpio_all_disabled;
  41. };
  42. /*
  43. * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
  44. * minmal side effect when scanning other columns, here it is configured to
  45. * be input, and it should work on most platforms.
  46. */
  47. static void __activate_col(const struct matrix_keypad_platform_data *pdata,
  48. int col, bool on)
  49. {
  50. bool level_on = !pdata->active_low;
  51. if (on) {
  52. gpio_direction_output(pdata->col_gpios[col], level_on);
  53. } else {
  54. gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
  55. gpio_direction_input(pdata->col_gpios[col]);
  56. }
  57. }
  58. static void activate_col(const struct matrix_keypad_platform_data *pdata,
  59. int col, bool on)
  60. {
  61. __activate_col(pdata, col, on);
  62. if (on && pdata->col_scan_delay_us)
  63. udelay(pdata->col_scan_delay_us);
  64. }
  65. static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
  66. bool on)
  67. {
  68. int col;
  69. for (col = 0; col < pdata->num_col_gpios; col++)
  70. __activate_col(pdata, col, on);
  71. }
  72. static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
  73. int row)
  74. {
  75. return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
  76. !pdata->active_low : pdata->active_low;
  77. }
  78. static void enable_row_irqs(struct matrix_keypad *keypad)
  79. {
  80. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  81. int i;
  82. if (pdata->clustered_irq > 0)
  83. enable_irq(pdata->clustered_irq);
  84. else {
  85. for (i = 0; i < pdata->num_row_gpios; i++)
  86. enable_irq(gpio_to_irq(pdata->row_gpios[i]));
  87. }
  88. }
  89. static void disable_row_irqs(struct matrix_keypad *keypad)
  90. {
  91. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  92. int i;
  93. if (pdata->clustered_irq > 0)
  94. disable_irq_nosync(pdata->clustered_irq);
  95. else {
  96. for (i = 0; i < pdata->num_row_gpios; i++)
  97. disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
  98. }
  99. }
  100. /*
  101. * This gets the keys from keyboard and reports it to input subsystem
  102. */
  103. static void matrix_keypad_scan(struct work_struct *work)
  104. {
  105. struct matrix_keypad *keypad =
  106. container_of(work, struct matrix_keypad, work.work);
  107. struct input_dev *input_dev = keypad->input_dev;
  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. #ifdef CONFIG_SEC_PATEK_PROJECT
  133. pr_info("%s: [key] [%d:%d] %x, %s\n",__func__,row,col,
  134. (new_state[col] & ( 1 << row )),
  135. !(!(new_state[col] & (1 << row))) ? "pressed" : "released");
  136. if(!(!(new_state[col] & (1 << row)))){
  137. check_key_press++;
  138. }else{
  139. check_key_press--;
  140. }
  141. #endif
  142. input_report_key(input_dev,
  143. keypad->keycodes[code],
  144. new_state[col] & (1 << row));
  145. }
  146. }
  147. input_sync(input_dev);
  148. memcpy(keypad->last_key_state, new_state, sizeof(new_state));
  149. activate_all_cols(pdata, true);
  150. mutex_lock(&keypad->lock);
  151. keypad->scan_pending = false;
  152. enable_row_irqs(keypad);
  153. mutex_unlock(&keypad->lock);
  154. }
  155. #ifdef CONFIG_SEC_PATEK_PROJECT
  156. int check_short_key(void)
  157. {
  158. int ret;
  159. ret = !(!check_key_press);
  160. return ret;
  161. }
  162. EXPORT_SYMBOL(check_short_key);
  163. #endif
  164. static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
  165. {
  166. struct matrix_keypad *keypad = id;
  167. mutex_lock(&keypad->lock);
  168. /*
  169. * See if another IRQ beaten us to it and scheduled the
  170. * scan already. In that case we should not try to
  171. * disable IRQs again.
  172. */
  173. if (unlikely(keypad->scan_pending || keypad->stopped))
  174. goto out;
  175. disable_row_irqs(keypad);
  176. keypad->scan_pending = true;
  177. schedule_delayed_work(&keypad->work,
  178. msecs_to_jiffies(keypad->pdata->debounce_ms));
  179. out:
  180. mutex_unlock(&keypad->lock);
  181. return IRQ_HANDLED;
  182. }
  183. static int matrix_keypad_start(struct input_dev *dev)
  184. {
  185. struct matrix_keypad *keypad = input_get_drvdata(dev);
  186. keypad->stopped = false;
  187. mb();
  188. /*
  189. * Schedule an immediate key scan to capture current key state;
  190. * columns will be activated and IRQs be enabled after the scan.
  191. */
  192. schedule_delayed_work(&keypad->work, 0);
  193. return 0;
  194. }
  195. static void matrix_keypad_stop(struct input_dev *dev)
  196. {
  197. struct matrix_keypad *keypad = input_get_drvdata(dev);
  198. keypad->stopped = true;
  199. mb();
  200. flush_work(&keypad->work.work);
  201. /*
  202. * matrix_keypad_scan() will leave IRQs enabled;
  203. * we should disable them now.
  204. */
  205. disable_row_irqs(keypad);
  206. }
  207. #ifdef CONFIG_PM
  208. static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
  209. {
  210. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  211. unsigned int gpio;
  212. int i;
  213. if (pdata->clustered_irq > 0) {
  214. if (enable_irq_wake(pdata->clustered_irq) == 0)
  215. keypad->gpio_all_disabled = true;
  216. } else {
  217. for (i = 0; i < pdata->num_row_gpios; i++) {
  218. if (!test_bit(i, keypad->disabled_gpios)) {
  219. gpio = pdata->row_gpios[i];
  220. if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
  221. __set_bit(i, keypad->disabled_gpios);
  222. }
  223. }
  224. }
  225. }
  226. static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
  227. {
  228. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  229. unsigned int gpio;
  230. int i;
  231. if (pdata->clustered_irq > 0) {
  232. if (keypad->gpio_all_disabled) {
  233. disable_irq_wake(pdata->clustered_irq);
  234. keypad->gpio_all_disabled = false;
  235. }
  236. } else {
  237. for (i = 0; i < pdata->num_row_gpios; i++) {
  238. if (test_and_clear_bit(i, keypad->disabled_gpios)) {
  239. gpio = pdata->row_gpios[i];
  240. disable_irq_wake(gpio_to_irq(gpio));
  241. }
  242. }
  243. }
  244. }
  245. static int matrix_keypad_suspend(struct device *dev)
  246. {
  247. struct platform_device *pdev = to_platform_device(dev);
  248. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  249. matrix_keypad_stop(keypad->input_dev);
  250. if (device_may_wakeup(&pdev->dev))
  251. matrix_keypad_enable_wakeup(keypad);
  252. return 0;
  253. }
  254. static int matrix_keypad_resume(struct device *dev)
  255. {
  256. struct platform_device *pdev = to_platform_device(dev);
  257. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  258. if (device_may_wakeup(&pdev->dev))
  259. matrix_keypad_disable_wakeup(keypad);
  260. matrix_keypad_start(keypad->input_dev);
  261. return 0;
  262. }
  263. static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
  264. matrix_keypad_suspend, matrix_keypad_resume);
  265. #endif
  266. extern int system_rev;
  267. static int __devinit init_matrix_gpio(struct platform_device *pdev,
  268. struct matrix_keypad *keypad)
  269. {
  270. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  271. int i, err = -EINVAL;
  272. /* initialized strobe lines as outputs, activated */
  273. for (i = 0; i < pdata->num_col_gpios; i++) {
  274. err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
  275. if (err) {
  276. dev_err(&pdev->dev,
  277. "failed to request GPIO%d for COL%d\n",
  278. pdata->col_gpios[i], i);
  279. goto err_free_cols;
  280. }
  281. #ifdef CONFIG_SEC_PATEK_PROJECT
  282. gpio_tlmm_config(GPIO_CFG((pdata->col_gpios[i]), 0,
  283. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  284. gpio_set_value((pdata->col_gpios[i]), 0);
  285. #else
  286. gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
  287. #endif
  288. }
  289. #ifdef CONFIG_SEC_PATEK_PROJECT
  290. if (system_rev <= 0x01){
  291. pdata->row_gpios[3] = 28;
  292. pr_info("[keypad] KBC(3) : gpio 77 -> 28 (board_rev = %x)\n", system_rev);
  293. }
  294. #endif
  295. for (i = 0; i < pdata->num_row_gpios; i++) {
  296. err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
  297. if (err) {
  298. dev_err(&pdev->dev,
  299. "failed to request GPIO%d for ROW%d\n",
  300. pdata->row_gpios[i], i);
  301. goto err_free_rows;
  302. }
  303. #ifdef CONFIG_SEC_PATEK_PROJECT
  304. gpio_tlmm_config(GPIO_CFG((pdata->row_gpios[i]), 0,
  305. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  306. #else
  307. gpio_direction_input(pdata->row_gpios[i]);
  308. #endif
  309. }
  310. if (pdata->clustered_irq > 0) {
  311. err = request_irq(pdata->clustered_irq,
  312. matrix_keypad_interrupt,
  313. pdata->clustered_irq_flags,
  314. "matrix-keypad", keypad);
  315. if (err < 0) {
  316. dev_err(&pdev->dev,
  317. "Unable to acquire clustered interrupt\n");
  318. goto err_free_rows;
  319. }
  320. } else {
  321. for (i = 0; i < pdata->num_row_gpios; i++) {
  322. err = request_threaded_irq(
  323. gpio_to_irq(pdata->row_gpios[i]),
  324. NULL,
  325. matrix_keypad_interrupt,
  326. IRQF_DISABLED | IRQF_ONESHOT |
  327. IRQF_TRIGGER_RISING |
  328. IRQF_TRIGGER_FALLING,
  329. "matrix-keypad", keypad);
  330. if (err < 0) {
  331. dev_err(&pdev->dev,
  332. "Unable to acquire interrupt "
  333. "for GPIO line %i\n",
  334. pdata->row_gpios[i]);
  335. goto err_free_irqs;
  336. }
  337. }
  338. }
  339. /* initialized as disabled - enabled by input->open */
  340. disable_row_irqs(keypad);
  341. return 0;
  342. err_free_irqs:
  343. while (--i >= 0)
  344. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  345. i = pdata->num_row_gpios;
  346. err_free_rows:
  347. while (--i >= 0)
  348. gpio_free(pdata->row_gpios[i]);
  349. i = pdata->num_col_gpios;
  350. err_free_cols:
  351. while (--i >= 0)
  352. gpio_free(pdata->col_gpios[i]);
  353. return err;
  354. }
  355. static int __devinit matrix_keypad_probe(struct platform_device *pdev)
  356. {
  357. const struct matrix_keypad_platform_data *pdata;
  358. const struct matrix_keymap_data *keymap_data;
  359. struct matrix_keypad *keypad;
  360. struct input_dev *input_dev;
  361. unsigned short *keycodes;
  362. unsigned int row_shift;
  363. int err;
  364. pdata = pdev->dev.platform_data;
  365. if (!pdata) {
  366. dev_err(&pdev->dev, "no platform data defined\n");
  367. return -EINVAL;
  368. }
  369. keymap_data = pdata->keymap_data;
  370. if (!keymap_data) {
  371. dev_err(&pdev->dev, "no keymap data defined\n");
  372. return -EINVAL;
  373. }
  374. row_shift = get_count_order(pdata->num_col_gpios);
  375. keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
  376. keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
  377. sizeof(*keycodes),
  378. GFP_KERNEL);
  379. input_dev = input_allocate_device();
  380. if (!keypad || !keycodes || !input_dev) {
  381. err = -ENOMEM;
  382. goto err_free_mem;
  383. }
  384. keypad->input_dev = input_dev;
  385. keypad->pdata = pdata;
  386. keypad->keycodes = keycodes;
  387. keypad->row_shift = row_shift;
  388. keypad->stopped = true;
  389. INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
  390. mutex_init(&keypad->lock);
  391. input_dev->name = pdev->name;
  392. input_dev->id.bustype = BUS_HOST;
  393. input_dev->dev.parent = &pdev->dev;
  394. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  395. if (!pdata->no_autorepeat)
  396. input_dev->evbit[0] |= BIT_MASK(EV_REP);
  397. input_dev->open = matrix_keypad_start;
  398. input_dev->close = matrix_keypad_stop;
  399. input_dev->keycode = keycodes;
  400. input_dev->keycodesize = sizeof(*keycodes);
  401. input_dev->keycodemax = pdata->num_row_gpios << row_shift;
  402. matrix_keypad_build_keymap(keymap_data, row_shift,
  403. input_dev->keycode, input_dev->keybit);
  404. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  405. input_set_drvdata(input_dev, keypad);
  406. err = init_matrix_gpio(pdev, keypad);
  407. if (err)
  408. goto err_free_mem;
  409. err = input_register_device(keypad->input_dev);
  410. if (err)
  411. goto err_free_mem;
  412. device_init_wakeup(&pdev->dev, pdata->wakeup);
  413. platform_set_drvdata(pdev, keypad);
  414. return 0;
  415. err_free_mem:
  416. input_free_device(input_dev);
  417. kfree(keycodes);
  418. kfree(keypad);
  419. return err;
  420. }
  421. static int __devexit matrix_keypad_remove(struct platform_device *pdev)
  422. {
  423. struct matrix_keypad *keypad = platform_get_drvdata(pdev);
  424. const struct matrix_keypad_platform_data *pdata = keypad->pdata;
  425. int i;
  426. device_init_wakeup(&pdev->dev, 0);
  427. if (pdata->clustered_irq > 0) {
  428. free_irq(pdata->clustered_irq, keypad);
  429. } else {
  430. for (i = 0; i < pdata->num_row_gpios; i++)
  431. free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
  432. }
  433. for (i = 0; i < pdata->num_row_gpios; i++)
  434. gpio_free(pdata->row_gpios[i]);
  435. for (i = 0; i < pdata->num_col_gpios; i++)
  436. gpio_free(pdata->col_gpios[i]);
  437. mutex_destroy(&keypad->lock);
  438. input_unregister_device(keypad->input_dev);
  439. platform_set_drvdata(pdev, NULL);
  440. kfree(keypad->keycodes);
  441. kfree(keypad);
  442. return 0;
  443. }
  444. static struct platform_driver matrix_keypad_driver = {
  445. .probe = matrix_keypad_probe,
  446. .remove = __devexit_p(matrix_keypad_remove),
  447. .driver = {
  448. #ifdef CONFIG_SEC_PATEK_PROJECT
  449. .name = "patek_3x4_keypad",
  450. #else
  451. .name = "matrix-keypad",
  452. #endif
  453. .owner = THIS_MODULE,
  454. #ifdef CONFIG_PM
  455. .pm = &matrix_keypad_pm_ops,
  456. #endif
  457. },
  458. };
  459. module_platform_driver(matrix_keypad_driver);
  460. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  461. MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
  462. MODULE_LICENSE("GPL v2");
  463. MODULE_ALIAS("platform:matrix-keypad");