imx_keypad.c 16 KB

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