imx_keypad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Driver for the IMX keypad port.
  3. * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/input/matrix_keypad.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/timer.h>
  23. /*
  24. * Keypad Controller registers (halfword)
  25. */
  26. #define KPCR 0x00 /* Keypad Control Register */
  27. #define KPSR 0x02 /* Keypad Status Register */
  28. #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
  29. #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
  30. #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
  31. #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
  32. #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
  33. #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
  34. #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
  35. #define KDDR 0x04 /* Keypad Data Direction Register */
  36. #define KPDR 0x06 /* Keypad Data Register */
  37. #define MAX_MATRIX_KEY_ROWS 8
  38. #define MAX_MATRIX_KEY_COLS 8
  39. #define MATRIX_ROW_SHIFT 3
  40. #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
  41. struct imx_keypad {
  42. struct clk *clk;
  43. struct input_dev *input_dev;
  44. void __iomem *mmio_base;
  45. int irq;
  46. struct timer_list check_matrix_timer;
  47. /*
  48. * The matrix is stable only if no changes are detected after
  49. * IMX_KEYPAD_SCANS_FOR_STABILITY scans
  50. */
  51. #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
  52. int stable_count;
  53. bool enabled;
  54. /* Masks for enabled rows/cols */
  55. unsigned short rows_en_mask;
  56. unsigned short cols_en_mask;
  57. unsigned short keycodes[MAX_MATRIX_KEY_NUM];
  58. /*
  59. * Matrix states:
  60. * -stable: achieved after a complete debounce process.
  61. * -unstable: used in the debouncing process.
  62. */
  63. unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS];
  64. unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS];
  65. };
  66. /* Scan the matrix and return the new state in *matrix_volatile_state. */
  67. static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
  68. unsigned short *matrix_volatile_state)
  69. {
  70. int col;
  71. unsigned short reg_val;
  72. for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
  73. if ((keypad->cols_en_mask & (1 << col)) == 0)
  74. continue;
  75. /*
  76. * Discharge keypad capacitance:
  77. * 2. write 1s on column data.
  78. * 3. configure columns as totem-pole to discharge capacitance.
  79. * 4. configure columns as open-drain.
  80. */
  81. reg_val = readw(keypad->mmio_base + KPDR);
  82. reg_val |= 0xff00;
  83. writew(reg_val, keypad->mmio_base + KPDR);
  84. reg_val = readw(keypad->mmio_base + KPCR);
  85. reg_val &= ~((keypad->cols_en_mask & 0xff) << 8);
  86. writew(reg_val, keypad->mmio_base + KPCR);
  87. udelay(2);
  88. reg_val = readw(keypad->mmio_base + KPCR);
  89. reg_val |= (keypad->cols_en_mask & 0xff) << 8;
  90. writew(reg_val, keypad->mmio_base + KPCR);
  91. /*
  92. * 5. Write a single column to 0, others to 1.
  93. * 6. Sample row inputs and save data.
  94. * 7. Repeat steps 2 - 6 for remaining columns.
  95. */
  96. reg_val = readw(keypad->mmio_base + KPDR);
  97. reg_val &= ~(1 << (8 + col));
  98. writew(reg_val, keypad->mmio_base + KPDR);
  99. /*
  100. * Delay added to avoid propagating the 0 from column to row
  101. * when scanning.
  102. */
  103. udelay(5);
  104. /*
  105. * 1s in matrix_volatile_state[col] means key pressures
  106. * throw data from non enabled rows.
  107. */
  108. reg_val = readw(keypad->mmio_base + KPDR);
  109. matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
  110. }
  111. /*
  112. * Return in standby mode:
  113. * 9. write 0s to columns
  114. */
  115. reg_val = readw(keypad->mmio_base + KPDR);
  116. reg_val &= 0x00ff;
  117. writew(reg_val, keypad->mmio_base + KPDR);
  118. }
  119. /*
  120. * Compare the new matrix state (volatile) with the stable one stored in
  121. * keypad->matrix_stable_state and fire events if changes are detected.
  122. */
  123. static void imx_keypad_fire_events(struct imx_keypad *keypad,
  124. unsigned short *matrix_volatile_state)
  125. {
  126. struct input_dev *input_dev = keypad->input_dev;
  127. int row, col;
  128. for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
  129. unsigned short bits_changed;
  130. int code;
  131. if ((keypad->cols_en_mask & (1 << col)) == 0)
  132. continue; /* Column is not enabled */
  133. bits_changed = keypad->matrix_stable_state[col] ^
  134. matrix_volatile_state[col];
  135. if (bits_changed == 0)
  136. continue; /* Column does not contain changes */
  137. for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
  138. if ((keypad->rows_en_mask & (1 << row)) == 0)
  139. continue; /* Row is not enabled */
  140. if ((bits_changed & (1 << row)) == 0)
  141. continue; /* Row does not contain changes */
  142. code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
  143. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  144. input_report_key(input_dev, keypad->keycodes[code],
  145. matrix_volatile_state[col] & (1 << row));
  146. dev_dbg(&input_dev->dev, "Event code: %d, val: %d",
  147. keypad->keycodes[code],
  148. matrix_volatile_state[col] & (1 << row));
  149. }
  150. }
  151. input_sync(input_dev);
  152. }
  153. /*
  154. * imx_keypad_check_for_events is the timer handler.
  155. */
  156. static void imx_keypad_check_for_events(unsigned long data)
  157. {
  158. struct imx_keypad *keypad = (struct imx_keypad *) data;
  159. unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
  160. unsigned short reg_val;
  161. bool state_changed, is_zero_matrix;
  162. int i;
  163. memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
  164. imx_keypad_scan_matrix(keypad, matrix_volatile_state);
  165. state_changed = false;
  166. for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
  167. if ((keypad->cols_en_mask & (1 << i)) == 0)
  168. continue;
  169. if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
  170. state_changed = true;
  171. break;
  172. }
  173. }
  174. /*
  175. * If the matrix state is changed from the previous scan
  176. * (Re)Begin the debouncing process, saving the new state in
  177. * keypad->matrix_unstable_state.
  178. * else
  179. * Increase the count of number of scans with a stable state.
  180. */
  181. if (state_changed) {
  182. memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
  183. sizeof(matrix_volatile_state));
  184. keypad->stable_count = 0;
  185. } else
  186. keypad->stable_count++;
  187. /*
  188. * If the matrix is not as stable as we want reschedule scan
  189. * in the near future.
  190. */
  191. if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
  192. mod_timer(&keypad->check_matrix_timer,
  193. jiffies + msecs_to_jiffies(10));
  194. return;
  195. }
  196. /*
  197. * If the matrix state is stable, fire the events and save the new
  198. * stable state. Note, if the matrix is kept stable for longer
  199. * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
  200. * events have already been generated.
  201. */
  202. if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
  203. imx_keypad_fire_events(keypad, matrix_volatile_state);
  204. memcpy(keypad->matrix_stable_state, matrix_volatile_state,
  205. sizeof(matrix_volatile_state));
  206. }
  207. is_zero_matrix = true;
  208. for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
  209. if (matrix_volatile_state[i] != 0) {
  210. is_zero_matrix = false;
  211. break;
  212. }
  213. }
  214. if (is_zero_matrix) {
  215. /*
  216. * All keys have been released. Enable only the KDI
  217. * interrupt for future key presses (clear the KDI
  218. * status bit and its sync chain before that).
  219. */
  220. reg_val = readw(keypad->mmio_base + KPSR);
  221. reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
  222. writew(reg_val, keypad->mmio_base + KPSR);
  223. reg_val = readw(keypad->mmio_base + KPSR);
  224. reg_val |= KBD_STAT_KDIE;
  225. reg_val &= ~KBD_STAT_KRIE;
  226. writew(reg_val, keypad->mmio_base + KPSR);
  227. } else {
  228. /*
  229. * Some keys are still pressed. Schedule a rescan in
  230. * attempt to detect multiple key presses and enable
  231. * the KRI interrupt to react quickly to key release
  232. * event.
  233. */
  234. mod_timer(&keypad->check_matrix_timer,
  235. jiffies + msecs_to_jiffies(60));
  236. reg_val = readw(keypad->mmio_base + KPSR);
  237. reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
  238. writew(reg_val, keypad->mmio_base + KPSR);
  239. reg_val = readw(keypad->mmio_base + KPSR);
  240. reg_val |= KBD_STAT_KRIE;
  241. reg_val &= ~KBD_STAT_KDIE;
  242. writew(reg_val, keypad->mmio_base + KPSR);
  243. }
  244. }
  245. static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
  246. {
  247. struct imx_keypad *keypad = dev_id;
  248. unsigned short reg_val;
  249. reg_val = readw(keypad->mmio_base + KPSR);
  250. /* Disable both interrupt types */
  251. reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
  252. /* Clear interrupts status bits */
  253. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
  254. writew(reg_val, keypad->mmio_base + KPSR);
  255. if (keypad->enabled) {
  256. /* The matrix is supposed to be changed */
  257. keypad->stable_count = 0;
  258. /* Schedule the scanning procedure near in the future */
  259. mod_timer(&keypad->check_matrix_timer,
  260. jiffies + msecs_to_jiffies(2));
  261. }
  262. return IRQ_HANDLED;
  263. }
  264. static void imx_keypad_config(struct imx_keypad *keypad)
  265. {
  266. unsigned short reg_val;
  267. /*
  268. * Include enabled rows in interrupt generation (KPCR[7:0])
  269. * Configure keypad columns as open-drain (KPCR[15:8])
  270. */
  271. reg_val = readw(keypad->mmio_base + KPCR);
  272. reg_val |= keypad->rows_en_mask & 0xff; /* rows */
  273. reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
  274. writew(reg_val, keypad->mmio_base + KPCR);
  275. /* Write 0's to KPDR[15:8] (Colums) */
  276. reg_val = readw(keypad->mmio_base + KPDR);
  277. reg_val &= 0x00ff;
  278. writew(reg_val, keypad->mmio_base + KPDR);
  279. /* Configure columns as output, rows as input (KDDR[15:0]) */
  280. writew(0xff00, keypad->mmio_base + KDDR);
  281. /*
  282. * Clear Key Depress and Key Release status bit.
  283. * Clear both synchronizer chain.
  284. */
  285. reg_val = readw(keypad->mmio_base + KPSR);
  286. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
  287. KBD_STAT_KDSC | KBD_STAT_KRSS;
  288. writew(reg_val, keypad->mmio_base + KPSR);
  289. /* Enable KDI and disable KRI (avoid false release events). */
  290. reg_val |= KBD_STAT_KDIE;
  291. reg_val &= ~KBD_STAT_KRIE;
  292. writew(reg_val, keypad->mmio_base + KPSR);
  293. }
  294. static void imx_keypad_inhibit(struct imx_keypad *keypad)
  295. {
  296. unsigned short reg_val;
  297. /* Inhibit KDI and KRI interrupts. */
  298. reg_val = readw(keypad->mmio_base + KPSR);
  299. reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
  300. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
  301. writew(reg_val, keypad->mmio_base + KPSR);
  302. /* Colums as open drain and disable all rows */
  303. reg_val = (keypad->cols_en_mask & 0xff) << 8;
  304. writew(reg_val, keypad->mmio_base + KPCR);
  305. }
  306. static void imx_keypad_close(struct input_dev *dev)
  307. {
  308. struct imx_keypad *keypad = input_get_drvdata(dev);
  309. dev_dbg(&dev->dev, ">%s\n", __func__);
  310. /* Mark keypad as being inactive */
  311. keypad->enabled = false;
  312. synchronize_irq(keypad->irq);
  313. del_timer_sync(&keypad->check_matrix_timer);
  314. imx_keypad_inhibit(keypad);
  315. /* Disable clock unit */
  316. clk_disable_unprepare(keypad->clk);
  317. }
  318. static int imx_keypad_open(struct input_dev *dev)
  319. {
  320. struct imx_keypad *keypad = input_get_drvdata(dev);
  321. int error;
  322. dev_dbg(&dev->dev, ">%s\n", __func__);
  323. /* Enable the kpp clock */
  324. error = clk_prepare_enable(keypad->clk);
  325. if (error)
  326. return error;
  327. /* We became active from now */
  328. keypad->enabled = true;
  329. imx_keypad_config(keypad);
  330. /* Sanity control, not all the rows must be actived now. */
  331. if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
  332. dev_err(&dev->dev,
  333. "too many keys pressed, control pins initialisation\n");
  334. goto open_err;
  335. }
  336. return 0;
  337. open_err:
  338. imx_keypad_close(dev);
  339. return -EIO;
  340. }
  341. #ifdef CONFIG_OF
  342. static const struct of_device_id imx_keypad_of_match[] = {
  343. { .compatible = "fsl,imx21-kpp", },
  344. { /* sentinel */ }
  345. };
  346. MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
  347. #endif
  348. static int imx_keypad_probe(struct platform_device *pdev)
  349. {
  350. const struct matrix_keymap_data *keymap_data =
  351. dev_get_platdata(&pdev->dev);
  352. struct imx_keypad *keypad;
  353. struct input_dev *input_dev;
  354. struct resource *res;
  355. int irq, error, i, row, col;
  356. if (!keymap_data && !pdev->dev.of_node) {
  357. dev_err(&pdev->dev, "no keymap defined\n");
  358. return -EINVAL;
  359. }
  360. irq = platform_get_irq(pdev, 0);
  361. if (irq < 0) {
  362. dev_err(&pdev->dev, "no irq defined in platform data\n");
  363. return irq;
  364. }
  365. input_dev = devm_input_allocate_device(&pdev->dev);
  366. if (!input_dev) {
  367. dev_err(&pdev->dev, "failed to allocate the input device\n");
  368. return -ENOMEM;
  369. }
  370. keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
  371. if (!keypad) {
  372. dev_err(&pdev->dev, "not enough memory for driver data\n");
  373. return -ENOMEM;
  374. }
  375. keypad->input_dev = input_dev;
  376. keypad->irq = irq;
  377. keypad->stable_count = 0;
  378. setup_timer(&keypad->check_matrix_timer,
  379. imx_keypad_check_for_events, (unsigned long) keypad);
  380. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  381. keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  382. if (IS_ERR(keypad->mmio_base))
  383. return PTR_ERR(keypad->mmio_base);
  384. keypad->clk = devm_clk_get(&pdev->dev, NULL);
  385. if (IS_ERR(keypad->clk)) {
  386. dev_err(&pdev->dev, "failed to get keypad clock\n");
  387. return PTR_ERR(keypad->clk);
  388. }
  389. /* Init the Input device */
  390. input_dev->name = pdev->name;
  391. input_dev->id.bustype = BUS_HOST;
  392. input_dev->dev.parent = &pdev->dev;
  393. input_dev->open = imx_keypad_open;
  394. input_dev->close = imx_keypad_close;
  395. error = matrix_keypad_build_keymap(keymap_data, NULL,
  396. MAX_MATRIX_KEY_ROWS,
  397. MAX_MATRIX_KEY_COLS,
  398. keypad->keycodes, input_dev);
  399. if (error) {
  400. dev_err(&pdev->dev, "failed to build keymap\n");
  401. return error;
  402. }
  403. /* Search for rows and cols enabled */
  404. for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
  405. for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
  406. i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
  407. if (keypad->keycodes[i] != KEY_RESERVED) {
  408. keypad->rows_en_mask |= 1 << row;
  409. keypad->cols_en_mask |= 1 << col;
  410. }
  411. }
  412. }
  413. dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
  414. dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
  415. __set_bit(EV_REP, input_dev->evbit);
  416. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  417. input_set_drvdata(input_dev, keypad);
  418. /* Ensure that the keypad will stay dormant until opened */
  419. error = clk_prepare_enable(keypad->clk);
  420. if (error)
  421. return error;
  422. imx_keypad_inhibit(keypad);
  423. clk_disable_unprepare(keypad->clk);
  424. error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
  425. pdev->name, keypad);
  426. if (error) {
  427. dev_err(&pdev->dev, "failed to request IRQ\n");
  428. return error;
  429. }
  430. /* Register the input device */
  431. error = input_register_device(input_dev);
  432. if (error) {
  433. dev_err(&pdev->dev, "failed to register input device\n");
  434. return error;
  435. }
  436. platform_set_drvdata(pdev, keypad);
  437. device_init_wakeup(&pdev->dev, 1);
  438. return 0;
  439. }
  440. static int __maybe_unused imx_kbd_suspend(struct device *dev)
  441. {
  442. struct platform_device *pdev = to_platform_device(dev);
  443. struct imx_keypad *kbd = platform_get_drvdata(pdev);
  444. struct input_dev *input_dev = kbd->input_dev;
  445. /* imx kbd can wake up system even clock is disabled */
  446. mutex_lock(&input_dev->mutex);
  447. if (input_dev->users)
  448. clk_disable_unprepare(kbd->clk);
  449. mutex_unlock(&input_dev->mutex);
  450. if (device_may_wakeup(&pdev->dev))
  451. enable_irq_wake(kbd->irq);
  452. return 0;
  453. }
  454. static int __maybe_unused imx_kbd_resume(struct device *dev)
  455. {
  456. struct platform_device *pdev = to_platform_device(dev);
  457. struct imx_keypad *kbd = platform_get_drvdata(pdev);
  458. struct input_dev *input_dev = kbd->input_dev;
  459. int ret = 0;
  460. if (device_may_wakeup(&pdev->dev))
  461. disable_irq_wake(kbd->irq);
  462. mutex_lock(&input_dev->mutex);
  463. if (input_dev->users) {
  464. ret = clk_prepare_enable(kbd->clk);
  465. if (ret)
  466. goto err_clk;
  467. }
  468. err_clk:
  469. mutex_unlock(&input_dev->mutex);
  470. return ret;
  471. }
  472. static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume);
  473. static struct platform_driver imx_keypad_driver = {
  474. .driver = {
  475. .name = "imx-keypad",
  476. .pm = &imx_kbd_pm_ops,
  477. .of_match_table = of_match_ptr(imx_keypad_of_match),
  478. },
  479. .probe = imx_keypad_probe,
  480. };
  481. module_platform_driver(imx_keypad_driver);
  482. MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
  483. MODULE_DESCRIPTION("IMX Keypad Port Driver");
  484. MODULE_LICENSE("GPL v2");
  485. MODULE_ALIAS("platform:imx-keypad");