imx_keypad.c 16 KB

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