imx_keypad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Driver for the IMX keypad port.
  4. // Copyright (C) 2009 Alberto Panizzo <[email protected]>
  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 = from_timer(keypad, t, check_matrix_timer);
  156. unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
  157. unsigned short reg_val;
  158. bool state_changed, is_zero_matrix;
  159. int i;
  160. memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
  161. imx_keypad_scan_matrix(keypad, matrix_volatile_state);
  162. state_changed = false;
  163. for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
  164. if ((keypad->cols_en_mask & (1 << i)) == 0)
  165. continue;
  166. if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
  167. state_changed = true;
  168. break;
  169. }
  170. }
  171. /*
  172. * If the matrix state is changed from the previous scan
  173. * (Re)Begin the debouncing process, saving the new state in
  174. * keypad->matrix_unstable_state.
  175. * else
  176. * Increase the count of number of scans with a stable state.
  177. */
  178. if (state_changed) {
  179. memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
  180. sizeof(matrix_volatile_state));
  181. keypad->stable_count = 0;
  182. } else
  183. keypad->stable_count++;
  184. /*
  185. * If the matrix is not as stable as we want reschedule scan
  186. * in the near future.
  187. */
  188. if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
  189. mod_timer(&keypad->check_matrix_timer,
  190. jiffies + msecs_to_jiffies(10));
  191. return;
  192. }
  193. /*
  194. * If the matrix state is stable, fire the events and save the new
  195. * stable state. Note, if the matrix is kept stable for longer
  196. * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
  197. * events have already been generated.
  198. */
  199. if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
  200. imx_keypad_fire_events(keypad, matrix_volatile_state);
  201. memcpy(keypad->matrix_stable_state, matrix_volatile_state,
  202. sizeof(matrix_volatile_state));
  203. }
  204. is_zero_matrix = true;
  205. for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
  206. if (matrix_volatile_state[i] != 0) {
  207. is_zero_matrix = false;
  208. break;
  209. }
  210. }
  211. if (is_zero_matrix) {
  212. /*
  213. * All keys have been released. Enable only the KDI
  214. * interrupt for future key presses (clear the KDI
  215. * status bit and its sync chain before that).
  216. */
  217. reg_val = readw(keypad->mmio_base + KPSR);
  218. reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
  219. writew(reg_val, keypad->mmio_base + KPSR);
  220. reg_val = readw(keypad->mmio_base + KPSR);
  221. reg_val |= KBD_STAT_KDIE;
  222. reg_val &= ~KBD_STAT_KRIE;
  223. writew(reg_val, keypad->mmio_base + KPSR);
  224. } else {
  225. /*
  226. * Some keys are still pressed. Schedule a rescan in
  227. * attempt to detect multiple key presses and enable
  228. * the KRI interrupt to react quickly to key release
  229. * event.
  230. */
  231. mod_timer(&keypad->check_matrix_timer,
  232. jiffies + msecs_to_jiffies(60));
  233. reg_val = readw(keypad->mmio_base + KPSR);
  234. reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
  235. writew(reg_val, keypad->mmio_base + KPSR);
  236. reg_val = readw(keypad->mmio_base + KPSR);
  237. reg_val |= KBD_STAT_KRIE;
  238. reg_val &= ~KBD_STAT_KDIE;
  239. writew(reg_val, keypad->mmio_base + KPSR);
  240. }
  241. }
  242. static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
  243. {
  244. struct imx_keypad *keypad = dev_id;
  245. unsigned short reg_val;
  246. reg_val = readw(keypad->mmio_base + KPSR);
  247. /* Disable both interrupt types */
  248. reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
  249. /* Clear interrupts status bits */
  250. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
  251. writew(reg_val, keypad->mmio_base + KPSR);
  252. if (keypad->enabled) {
  253. /* The matrix is supposed to be changed */
  254. keypad->stable_count = 0;
  255. /* Schedule the scanning procedure near in the future */
  256. mod_timer(&keypad->check_matrix_timer,
  257. jiffies + msecs_to_jiffies(2));
  258. }
  259. return IRQ_HANDLED;
  260. }
  261. static void imx_keypad_config(struct imx_keypad *keypad)
  262. {
  263. unsigned short reg_val;
  264. /*
  265. * Include enabled rows in interrupt generation (KPCR[7:0])
  266. * Configure keypad columns as open-drain (KPCR[15:8])
  267. */
  268. reg_val = readw(keypad->mmio_base + KPCR);
  269. reg_val |= keypad->rows_en_mask & 0xff; /* rows */
  270. reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
  271. writew(reg_val, keypad->mmio_base + KPCR);
  272. /* Write 0's to KPDR[15:8] (Colums) */
  273. reg_val = readw(keypad->mmio_base + KPDR);
  274. reg_val &= 0x00ff;
  275. writew(reg_val, keypad->mmio_base + KPDR);
  276. /* Configure columns as output, rows as input (KDDR[15:0]) */
  277. writew(0xff00, keypad->mmio_base + KDDR);
  278. /*
  279. * Clear Key Depress and Key Release status bit.
  280. * Clear both synchronizer chain.
  281. */
  282. reg_val = readw(keypad->mmio_base + KPSR);
  283. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
  284. KBD_STAT_KDSC | KBD_STAT_KRSS;
  285. writew(reg_val, keypad->mmio_base + KPSR);
  286. /* Enable KDI and disable KRI (avoid false release events). */
  287. reg_val |= KBD_STAT_KDIE;
  288. reg_val &= ~KBD_STAT_KRIE;
  289. writew(reg_val, keypad->mmio_base + KPSR);
  290. }
  291. static void imx_keypad_inhibit(struct imx_keypad *keypad)
  292. {
  293. unsigned short reg_val;
  294. /* Inhibit KDI and KRI interrupts. */
  295. reg_val = readw(keypad->mmio_base + KPSR);
  296. reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
  297. reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
  298. writew(reg_val, keypad->mmio_base + KPSR);
  299. /* Colums as open drain and disable all rows */
  300. reg_val = (keypad->cols_en_mask & 0xff) << 8;
  301. writew(reg_val, keypad->mmio_base + KPCR);
  302. }
  303. static void imx_keypad_close(struct input_dev *dev)
  304. {
  305. struct imx_keypad *keypad = input_get_drvdata(dev);
  306. dev_dbg(&dev->dev, ">%s\n", __func__);
  307. /* Mark keypad as being inactive */
  308. keypad->enabled = false;
  309. synchronize_irq(keypad->irq);
  310. del_timer_sync(&keypad->check_matrix_timer);
  311. imx_keypad_inhibit(keypad);
  312. /* Disable clock unit */
  313. clk_disable_unprepare(keypad->clk);
  314. }
  315. static int imx_keypad_open(struct input_dev *dev)
  316. {
  317. struct imx_keypad *keypad = input_get_drvdata(dev);
  318. int error;
  319. dev_dbg(&dev->dev, ">%s\n", __func__);
  320. /* Enable the kpp clock */
  321. error = clk_prepare_enable(keypad->clk);
  322. if (error)
  323. return error;
  324. /* We became active from now */
  325. keypad->enabled = true;
  326. imx_keypad_config(keypad);
  327. /* Sanity control, not all the rows must be actived now. */
  328. if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
  329. dev_err(&dev->dev,
  330. "too many keys pressed, control pins initialisation\n");
  331. goto open_err;
  332. }
  333. return 0;
  334. open_err:
  335. imx_keypad_close(dev);
  336. return -EIO;
  337. }
  338. static const struct of_device_id imx_keypad_of_match[] = {
  339. { .compatible = "fsl,imx21-kpp", },
  340. { /* sentinel */ }
  341. };
  342. MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
  343. static int imx_keypad_probe(struct platform_device *pdev)
  344. {
  345. struct imx_keypad *keypad;
  346. struct input_dev *input_dev;
  347. int irq, error, i, row, col;
  348. irq = platform_get_irq(pdev, 0);
  349. if (irq < 0)
  350. return irq;
  351. input_dev = devm_input_allocate_device(&pdev->dev);
  352. if (!input_dev) {
  353. dev_err(&pdev->dev, "failed to allocate the input device\n");
  354. return -ENOMEM;
  355. }
  356. keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
  357. if (!keypad) {
  358. dev_err(&pdev->dev, "not enough memory for driver data\n");
  359. return -ENOMEM;
  360. }
  361. keypad->input_dev = input_dev;
  362. keypad->irq = irq;
  363. keypad->stable_count = 0;
  364. timer_setup(&keypad->check_matrix_timer,
  365. imx_keypad_check_for_events, 0);
  366. keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0);
  367. if (IS_ERR(keypad->mmio_base))
  368. return PTR_ERR(keypad->mmio_base);
  369. keypad->clk = devm_clk_get(&pdev->dev, NULL);
  370. if (IS_ERR(keypad->clk)) {
  371. dev_err(&pdev->dev, "failed to get keypad clock\n");
  372. return PTR_ERR(keypad->clk);
  373. }
  374. /* Init the Input device */
  375. input_dev->name = pdev->name;
  376. input_dev->id.bustype = BUS_HOST;
  377. input_dev->dev.parent = &pdev->dev;
  378. input_dev->open = imx_keypad_open;
  379. input_dev->close = imx_keypad_close;
  380. error = matrix_keypad_build_keymap(NULL, NULL,
  381. MAX_MATRIX_KEY_ROWS,
  382. MAX_MATRIX_KEY_COLS,
  383. keypad->keycodes, input_dev);
  384. if (error) {
  385. dev_err(&pdev->dev, "failed to build keymap\n");
  386. return error;
  387. }
  388. /* Search for rows and cols enabled */
  389. for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
  390. for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
  391. i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
  392. if (keypad->keycodes[i] != KEY_RESERVED) {
  393. keypad->rows_en_mask |= 1 << row;
  394. keypad->cols_en_mask |= 1 << col;
  395. }
  396. }
  397. }
  398. dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
  399. dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
  400. __set_bit(EV_REP, input_dev->evbit);
  401. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  402. input_set_drvdata(input_dev, keypad);
  403. /* Ensure that the keypad will stay dormant until opened */
  404. error = clk_prepare_enable(keypad->clk);
  405. if (error)
  406. return error;
  407. imx_keypad_inhibit(keypad);
  408. clk_disable_unprepare(keypad->clk);
  409. error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
  410. pdev->name, keypad);
  411. if (error) {
  412. dev_err(&pdev->dev, "failed to request IRQ\n");
  413. return error;
  414. }
  415. /* Register the input device */
  416. error = input_register_device(input_dev);
  417. if (error) {
  418. dev_err(&pdev->dev, "failed to register input device\n");
  419. return error;
  420. }
  421. platform_set_drvdata(pdev, keypad);
  422. device_init_wakeup(&pdev->dev, 1);
  423. return 0;
  424. }
  425. static int __maybe_unused imx_kbd_noirq_suspend(struct device *dev)
  426. {
  427. struct platform_device *pdev = to_platform_device(dev);
  428. struct imx_keypad *kbd = platform_get_drvdata(pdev);
  429. struct input_dev *input_dev = kbd->input_dev;
  430. unsigned short reg_val = readw(kbd->mmio_base + KPSR);
  431. /* imx kbd can wake up system even clock is disabled */
  432. mutex_lock(&input_dev->mutex);
  433. if (input_device_enabled(input_dev))
  434. clk_disable_unprepare(kbd->clk);
  435. mutex_unlock(&input_dev->mutex);
  436. if (device_may_wakeup(&pdev->dev)) {
  437. if (reg_val & KBD_STAT_KPKD)
  438. reg_val |= KBD_STAT_KRIE;
  439. if (reg_val & KBD_STAT_KPKR)
  440. reg_val |= KBD_STAT_KDIE;
  441. writew(reg_val, kbd->mmio_base + KPSR);
  442. enable_irq_wake(kbd->irq);
  443. }
  444. return 0;
  445. }
  446. static int __maybe_unused imx_kbd_noirq_resume(struct device *dev)
  447. {
  448. struct platform_device *pdev = to_platform_device(dev);
  449. struct imx_keypad *kbd = platform_get_drvdata(pdev);
  450. struct input_dev *input_dev = kbd->input_dev;
  451. int ret = 0;
  452. if (device_may_wakeup(&pdev->dev))
  453. disable_irq_wake(kbd->irq);
  454. mutex_lock(&input_dev->mutex);
  455. if (input_device_enabled(input_dev)) {
  456. ret = clk_prepare_enable(kbd->clk);
  457. if (ret)
  458. goto err_clk;
  459. }
  460. err_clk:
  461. mutex_unlock(&input_dev->mutex);
  462. return ret;
  463. }
  464. static const struct dev_pm_ops imx_kbd_pm_ops = {
  465. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend, imx_kbd_noirq_resume)
  466. };
  467. static struct platform_driver imx_keypad_driver = {
  468. .driver = {
  469. .name = "imx-keypad",
  470. .pm = &imx_kbd_pm_ops,
  471. .of_match_table = imx_keypad_of_match,
  472. },
  473. .probe = imx_keypad_probe,
  474. };
  475. module_platform_driver(imx_keypad_driver);
  476. MODULE_AUTHOR("Alberto Panizzo <[email protected]>");
  477. MODULE_DESCRIPTION("IMX Keypad Port Driver");
  478. MODULE_LICENSE("GPL v2");
  479. MODULE_ALIAS("platform:imx-keypad");