imx_keypad.c 16 KB

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