pxa27x_keypad.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * linux/drivers/input/keyboard/pxa27x_keypad.c
  3. *
  4. * Driver for the pxa27x matrix keyboard controller.
  5. *
  6. * Created: Feb 22, 2007
  7. * Author: Rodolfo Giometti <giometti@linux.it>
  8. *
  9. * Based on a previous implementations by Kevin O'Connor
  10. * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
  11. * on some suggestions by Nicolas Pitre <nico@cam.org>.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/input.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <asm/mach-types.h>
  27. #include <asm/mach/arch.h>
  28. #include <asm/mach/map.h>
  29. #include <asm/arch/hardware.h>
  30. #include <asm/arch/pxa-regs.h>
  31. #include <asm/arch/irqs.h>
  32. #include <asm/arch/pxa27x_keypad.h>
  33. #define DRIVER_NAME "pxa27x-keypad"
  34. #define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
  35. #define KPAS_RP(n) (((n) >> 4) & 0xf)
  36. #define KPAS_CP(n) ((n) & 0xf)
  37. #define KPASMKP_MKC_MASK (0xff)
  38. #define MAX_MATRIX_KEY_NUM (8 * 8)
  39. struct pxa27x_keypad {
  40. struct pxa27x_keypad_platform_data *pdata;
  41. struct clk *clk;
  42. struct input_dev *input_dev;
  43. /* matrix key code map */
  44. unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
  45. /* state row bits of each column scan */
  46. uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
  47. };
  48. static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
  49. {
  50. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  51. struct input_dev *input_dev = keypad->input_dev;
  52. unsigned int *key;
  53. int i;
  54. key = &pdata->matrix_key_map[0];
  55. for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
  56. int row = ((*key) >> 28) & 0xf;
  57. int col = ((*key) >> 24) & 0xf;
  58. int code = (*key) & 0xffffff;
  59. keypad->matrix_keycodes[(row << 3) + col] = code;
  60. set_bit(code, input_dev->keybit);
  61. }
  62. }
  63. static inline unsigned int lookup_matrix_keycode(
  64. struct pxa27x_keypad *keypad, int row, int col)
  65. {
  66. return keypad->matrix_keycodes[(row << 3) + col];
  67. }
  68. static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
  69. {
  70. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  71. int row, col, num_keys_pressed = 0;
  72. uint32_t new_state[MAX_MATRIX_KEY_COLS];
  73. uint32_t kpas = KPAS;
  74. num_keys_pressed = KPAS_MUKP(kpas);
  75. memset(new_state, 0, sizeof(new_state));
  76. if (num_keys_pressed == 0)
  77. goto scan;
  78. if (num_keys_pressed == 1) {
  79. col = KPAS_CP(kpas);
  80. row = KPAS_RP(kpas);
  81. /* if invalid row/col, treat as no key pressed */
  82. if (col >= pdata->matrix_key_cols ||
  83. row >= pdata->matrix_key_rows)
  84. goto scan;
  85. new_state[col] = (1 << row);
  86. goto scan;
  87. }
  88. if (num_keys_pressed > 1) {
  89. uint32_t kpasmkp0 = KPASMKP0;
  90. uint32_t kpasmkp1 = KPASMKP1;
  91. uint32_t kpasmkp2 = KPASMKP2;
  92. uint32_t kpasmkp3 = KPASMKP3;
  93. new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
  94. new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
  95. new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
  96. new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
  97. new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
  98. new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
  99. new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
  100. new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
  101. }
  102. scan:
  103. for (col = 0; col < pdata->matrix_key_cols; col++) {
  104. uint32_t bits_changed;
  105. bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
  106. if (bits_changed == 0)
  107. continue;
  108. for (row = 0; row < pdata->matrix_key_rows; row++) {
  109. if ((bits_changed & (1 << row)) == 0)
  110. continue;
  111. input_report_key(keypad->input_dev,
  112. lookup_matrix_keycode(keypad, row, col),
  113. new_state[col] & (1 << row));
  114. }
  115. }
  116. input_sync(keypad->input_dev);
  117. memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
  118. }
  119. static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
  120. {
  121. struct pxa27x_keypad *keypad = dev_id;
  122. struct input_dev *input_dev = keypad->input_dev;
  123. unsigned long kpc = KPC;
  124. int rel;
  125. if (kpc & KPC_DI) {
  126. unsigned long kpdk = KPDK;
  127. if (!(kpdk & KPDK_DKP)) {
  128. /* better luck next time */
  129. } else if (kpc & KPC_REE0) {
  130. unsigned long kprec = KPREC;
  131. KPREC = 0x7f;
  132. if (kprec & KPREC_OF0)
  133. rel = (kprec & 0xff) + 0x7f;
  134. else if (kprec & KPREC_UF0)
  135. rel = (kprec & 0xff) - 0x7f - 0xff;
  136. else
  137. rel = (kprec & 0xff) - 0x7f;
  138. if (rel) {
  139. input_report_rel(input_dev, REL_WHEEL, rel);
  140. input_sync(input_dev);
  141. }
  142. }
  143. }
  144. if (kpc & KPC_MI)
  145. pxa27x_keypad_scan_matrix(keypad);
  146. return IRQ_HANDLED;
  147. }
  148. static int pxa27x_keypad_open(struct input_dev *dev)
  149. {
  150. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  151. /* Set keypad control register */
  152. KPC |= (KPC_ASACT |
  153. KPC_MS_ALL |
  154. (2 << 6) | KPC_REE0 | KPC_DK_DEB_SEL |
  155. KPC_ME | KPC_MIE | KPC_DE | KPC_DIE);
  156. KPC &= ~KPC_AS; /* disable automatic scan */
  157. KPC &= ~KPC_IMKP; /* do not ignore multiple keypresses */
  158. /* Set rotary count to mid-point value */
  159. KPREC = 0x7F;
  160. /* Enable unit clock */
  161. clk_enable(keypad->clk);
  162. return 0;
  163. }
  164. static void pxa27x_keypad_close(struct input_dev *dev)
  165. {
  166. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  167. /* Disable clock unit */
  168. clk_disable(keypad->clk);
  169. }
  170. #ifdef CONFIG_PM
  171. static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
  172. {
  173. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  174. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  175. /* Save controller status */
  176. pdata->reg_kpc = KPC;
  177. pdata->reg_kprec = KPREC;
  178. return 0;
  179. }
  180. static int pxa27x_keypad_resume(struct platform_device *pdev)
  181. {
  182. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  183. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  184. struct input_dev *input_dev = keypad->input_dev;
  185. mutex_lock(&input_dev->mutex);
  186. if (input_dev->users) {
  187. /* Restore controller status */
  188. KPC = pdata->reg_kpc;
  189. KPREC = pdata->reg_kprec;
  190. /* Enable unit clock */
  191. clk_enable(keypad->clk);
  192. }
  193. mutex_unlock(&input_dev->mutex);
  194. return 0;
  195. }
  196. #else
  197. #define pxa27x_keypad_suspend NULL
  198. #define pxa27x_keypad_resume NULL
  199. #endif
  200. static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
  201. {
  202. struct pxa27x_keypad *keypad;
  203. struct input_dev *input_dev;
  204. int col, error;
  205. keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
  206. if (keypad == NULL) {
  207. dev_err(&pdev->dev, "failed to allocate driver data\n");
  208. return -ENOMEM;
  209. }
  210. keypad->pdata = pdev->dev.platform_data;
  211. if (keypad->pdata == NULL) {
  212. dev_err(&pdev->dev, "no platform data defined\n");
  213. error = -EINVAL;
  214. goto failed_free;
  215. }
  216. keypad->clk = clk_get(&pdev->dev, "KBDCLK");
  217. if (IS_ERR(keypad->clk)) {
  218. dev_err(&pdev->dev, "failed to get keypad clock\n");
  219. error = PTR_ERR(keypad->clk);
  220. goto failed_free;
  221. }
  222. /* Create and register the input driver. */
  223. input_dev = input_allocate_device();
  224. if (!input_dev) {
  225. dev_err(&pdev->dev, "failed to allocate input device\n");
  226. error = -ENOMEM;
  227. goto failed_put_clk;
  228. }
  229. input_dev->name = DRIVER_NAME;
  230. input_dev->id.bustype = BUS_HOST;
  231. input_dev->open = pxa27x_keypad_open;
  232. input_dev->close = pxa27x_keypad_close;
  233. input_dev->dev.parent = &pdev->dev;
  234. keypad->input_dev = input_dev;
  235. input_set_drvdata(input_dev, keypad);
  236. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
  237. BIT_MASK(EV_REL);
  238. input_dev->relbit[BIT_WORD(REL_WHEEL)] = BIT_MASK(REL_WHEEL);
  239. pxa27x_keypad_build_keycode(keypad);
  240. error = request_irq(IRQ_KEYPAD, pxa27x_keypad_irq_handler, IRQF_DISABLED,
  241. DRIVER_NAME, keypad);
  242. if (error) {
  243. printk(KERN_ERR "Cannot request keypad IRQ\n");
  244. goto err_free_dev;
  245. }
  246. platform_set_drvdata(pdev, keypad);
  247. /* Register the input device */
  248. error = input_register_device(input_dev);
  249. if (error)
  250. goto err_free_irq;
  251. /*
  252. * Store rows/cols info into keyboard registers.
  253. */
  254. KPC |= (keypad->pdata->matrix_key_rows - 1) << 26;
  255. KPC |= (keypad->pdata->matrix_key_cols - 1) << 23;
  256. for (col = 0; col < keypad->pdata->matrix_key_cols; col++)
  257. KPC |= KPC_MS0 << col;
  258. return 0;
  259. err_free_irq:
  260. platform_set_drvdata(pdev, NULL);
  261. free_irq(IRQ_KEYPAD, pdev);
  262. err_free_dev:
  263. input_free_device(input_dev);
  264. failed_put_clk:
  265. clk_put(keypad->clk);
  266. failed_free:
  267. kfree(keypad);
  268. return error;
  269. }
  270. static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
  271. {
  272. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  273. free_irq(IRQ_KEYPAD, pdev);
  274. clk_disable(keypad->clk);
  275. clk_put(keypad->clk);
  276. input_unregister_device(keypad->input_dev);
  277. platform_set_drvdata(pdev, NULL);
  278. kfree(keypad);
  279. return 0;
  280. }
  281. static struct platform_driver pxa27x_keypad_driver = {
  282. .probe = pxa27x_keypad_probe,
  283. .remove = __devexit_p(pxa27x_keypad_remove),
  284. .suspend = pxa27x_keypad_suspend,
  285. .resume = pxa27x_keypad_resume,
  286. .driver = {
  287. .name = DRIVER_NAME,
  288. },
  289. };
  290. static int __init pxa27x_keypad_init(void)
  291. {
  292. return platform_driver_register(&pxa27x_keypad_driver);
  293. }
  294. static void __exit pxa27x_keypad_exit(void)
  295. {
  296. platform_driver_unregister(&pxa27x_keypad_driver);
  297. }
  298. module_init(pxa27x_keypad_init);
  299. module_exit(pxa27x_keypad_exit);
  300. MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
  301. MODULE_LICENSE("GPL");