pxa27x_keypad.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 KPC_MKRN(n) ((((n) & 0x7) - 1) << 26) /* matrix key row number */
  35. #define KPC_MKCN(n) ((((n) & 0x7) - 1) << 23) /* matrix key column number */
  36. #define KPC_DKN(n) ((((n) & 0x7) - 1) << 6) /* direct key number */
  37. #define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
  38. #define KPAS_RP(n) (((n) >> 4) & 0xf)
  39. #define KPAS_CP(n) ((n) & 0xf)
  40. #define KPASMKP_MKC_MASK (0xff)
  41. #define MAX_MATRIX_KEY_NUM (8 * 8)
  42. struct pxa27x_keypad {
  43. struct pxa27x_keypad_platform_data *pdata;
  44. struct clk *clk;
  45. struct input_dev *input_dev;
  46. /* matrix key code map */
  47. unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
  48. /* state row bits of each column scan */
  49. uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
  50. };
  51. static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
  52. {
  53. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  54. struct input_dev *input_dev = keypad->input_dev;
  55. unsigned int *key;
  56. int i;
  57. key = &pdata->matrix_key_map[0];
  58. for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
  59. int row = ((*key) >> 28) & 0xf;
  60. int col = ((*key) >> 24) & 0xf;
  61. int code = (*key) & 0xffffff;
  62. keypad->matrix_keycodes[(row << 3) + col] = code;
  63. set_bit(code, input_dev->keybit);
  64. }
  65. }
  66. static inline unsigned int lookup_matrix_keycode(
  67. struct pxa27x_keypad *keypad, int row, int col)
  68. {
  69. return keypad->matrix_keycodes[(row << 3) + col];
  70. }
  71. static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
  72. {
  73. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  74. int row, col, num_keys_pressed = 0;
  75. uint32_t new_state[MAX_MATRIX_KEY_COLS];
  76. uint32_t kpas = KPAS;
  77. num_keys_pressed = KPAS_MUKP(kpas);
  78. memset(new_state, 0, sizeof(new_state));
  79. if (num_keys_pressed == 0)
  80. goto scan;
  81. if (num_keys_pressed == 1) {
  82. col = KPAS_CP(kpas);
  83. row = KPAS_RP(kpas);
  84. /* if invalid row/col, treat as no key pressed */
  85. if (col >= pdata->matrix_key_cols ||
  86. row >= pdata->matrix_key_rows)
  87. goto scan;
  88. new_state[col] = (1 << row);
  89. goto scan;
  90. }
  91. if (num_keys_pressed > 1) {
  92. uint32_t kpasmkp0 = KPASMKP0;
  93. uint32_t kpasmkp1 = KPASMKP1;
  94. uint32_t kpasmkp2 = KPASMKP2;
  95. uint32_t kpasmkp3 = KPASMKP3;
  96. new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
  97. new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
  98. new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
  99. new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
  100. new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
  101. new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
  102. new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
  103. new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
  104. }
  105. scan:
  106. for (col = 0; col < pdata->matrix_key_cols; col++) {
  107. uint32_t bits_changed;
  108. bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
  109. if (bits_changed == 0)
  110. continue;
  111. for (row = 0; row < pdata->matrix_key_rows; row++) {
  112. if ((bits_changed & (1 << row)) == 0)
  113. continue;
  114. input_report_key(keypad->input_dev,
  115. lookup_matrix_keycode(keypad, row, col),
  116. new_state[col] & (1 << row));
  117. }
  118. }
  119. input_sync(keypad->input_dev);
  120. memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
  121. }
  122. #define DEFAULT_KPREC (0x007f007f)
  123. static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
  124. {
  125. struct pxa27x_keypad *keypad = dev_id;
  126. struct input_dev *input_dev = keypad->input_dev;
  127. unsigned long kpc = KPC;
  128. int rel;
  129. if (kpc & KPC_DI) {
  130. unsigned long kpdk = KPDK;
  131. if (!(kpdk & KPDK_DKP)) {
  132. /* better luck next time */
  133. } else if (kpc & KPC_REE0) {
  134. unsigned long kprec = KPREC;
  135. KPREC = 0x7f;
  136. if (kprec & KPREC_OF0)
  137. rel = (kprec & 0xff) + 0x7f;
  138. else if (kprec & KPREC_UF0)
  139. rel = (kprec & 0xff) - 0x7f - 0xff;
  140. else
  141. rel = (kprec & 0xff) - 0x7f;
  142. if (rel) {
  143. input_report_rel(input_dev, REL_WHEEL, rel);
  144. input_sync(input_dev);
  145. }
  146. }
  147. }
  148. if (kpc & KPC_MI)
  149. pxa27x_keypad_scan_matrix(keypad);
  150. return IRQ_HANDLED;
  151. }
  152. static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
  153. {
  154. struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
  155. unsigned long kpc = 0;
  156. /* enable matrix keys with automatic scan */
  157. if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
  158. kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
  159. kpc |= KPC_MKRN(pdata->matrix_key_rows) |
  160. KPC_MKCN(pdata->matrix_key_cols);
  161. }
  162. /* FIXME: hardcoded to enable rotary 0 _only_ */
  163. kpc |= KPC_DKN(2) | KPC_REE0 | KPC_DI | KPC_DIE;
  164. KPC = kpc;
  165. KPREC = DEFAULT_KPREC;
  166. }
  167. static int pxa27x_keypad_open(struct input_dev *dev)
  168. {
  169. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  170. /* Enable unit clock */
  171. clk_enable(keypad->clk);
  172. pxa27x_keypad_config(keypad);
  173. return 0;
  174. }
  175. static void pxa27x_keypad_close(struct input_dev *dev)
  176. {
  177. struct pxa27x_keypad *keypad = input_get_drvdata(dev);
  178. /* Disable clock unit */
  179. clk_disable(keypad->clk);
  180. }
  181. #ifdef CONFIG_PM
  182. static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
  183. {
  184. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  185. clk_disable(keypad->clk);
  186. return 0;
  187. }
  188. static int pxa27x_keypad_resume(struct platform_device *pdev)
  189. {
  190. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  191. struct input_dev *input_dev = keypad->input_dev;
  192. mutex_lock(&input_dev->mutex);
  193. if (input_dev->users) {
  194. /* Enable unit clock */
  195. clk_enable(keypad->clk);
  196. pxa27x_keypad_config(keypad);
  197. }
  198. mutex_unlock(&input_dev->mutex);
  199. return 0;
  200. }
  201. #else
  202. #define pxa27x_keypad_suspend NULL
  203. #define pxa27x_keypad_resume NULL
  204. #endif
  205. static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
  206. {
  207. struct pxa27x_keypad *keypad;
  208. struct input_dev *input_dev;
  209. int error;
  210. keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
  211. if (keypad == NULL) {
  212. dev_err(&pdev->dev, "failed to allocate driver data\n");
  213. return -ENOMEM;
  214. }
  215. keypad->pdata = pdev->dev.platform_data;
  216. if (keypad->pdata == NULL) {
  217. dev_err(&pdev->dev, "no platform data defined\n");
  218. error = -EINVAL;
  219. goto failed_free;
  220. }
  221. keypad->clk = clk_get(&pdev->dev, "KBDCLK");
  222. if (IS_ERR(keypad->clk)) {
  223. dev_err(&pdev->dev, "failed to get keypad clock\n");
  224. error = PTR_ERR(keypad->clk);
  225. goto failed_free;
  226. }
  227. /* Create and register the input driver. */
  228. input_dev = input_allocate_device();
  229. if (!input_dev) {
  230. dev_err(&pdev->dev, "failed to allocate input device\n");
  231. error = -ENOMEM;
  232. goto failed_put_clk;
  233. }
  234. input_dev->name = DRIVER_NAME;
  235. input_dev->id.bustype = BUS_HOST;
  236. input_dev->open = pxa27x_keypad_open;
  237. input_dev->close = pxa27x_keypad_close;
  238. input_dev->dev.parent = &pdev->dev;
  239. keypad->input_dev = input_dev;
  240. input_set_drvdata(input_dev, keypad);
  241. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
  242. BIT_MASK(EV_REL);
  243. input_dev->relbit[BIT_WORD(REL_WHEEL)] = BIT_MASK(REL_WHEEL);
  244. pxa27x_keypad_build_keycode(keypad);
  245. error = request_irq(IRQ_KEYPAD, pxa27x_keypad_irq_handler, IRQF_DISABLED,
  246. DRIVER_NAME, keypad);
  247. if (error) {
  248. printk(KERN_ERR "Cannot request keypad IRQ\n");
  249. goto err_free_dev;
  250. }
  251. platform_set_drvdata(pdev, keypad);
  252. /* Register the input device */
  253. error = input_register_device(input_dev);
  254. if (error)
  255. goto err_free_irq;
  256. return 0;
  257. err_free_irq:
  258. platform_set_drvdata(pdev, NULL);
  259. free_irq(IRQ_KEYPAD, pdev);
  260. err_free_dev:
  261. input_free_device(input_dev);
  262. failed_put_clk:
  263. clk_put(keypad->clk);
  264. failed_free:
  265. kfree(keypad);
  266. return error;
  267. }
  268. static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
  269. {
  270. struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
  271. free_irq(IRQ_KEYPAD, pdev);
  272. clk_disable(keypad->clk);
  273. clk_put(keypad->clk);
  274. input_unregister_device(keypad->input_dev);
  275. platform_set_drvdata(pdev, NULL);
  276. kfree(keypad);
  277. return 0;
  278. }
  279. static struct platform_driver pxa27x_keypad_driver = {
  280. .probe = pxa27x_keypad_probe,
  281. .remove = __devexit_p(pxa27x_keypad_remove),
  282. .suspend = pxa27x_keypad_suspend,
  283. .resume = pxa27x_keypad_resume,
  284. .driver = {
  285. .name = DRIVER_NAME,
  286. },
  287. };
  288. static int __init pxa27x_keypad_init(void)
  289. {
  290. return platform_driver_register(&pxa27x_keypad_driver);
  291. }
  292. static void __exit pxa27x_keypad_exit(void)
  293. {
  294. platform_driver_unregister(&pxa27x_keypad_driver);
  295. }
  296. module_init(pxa27x_keypad_init);
  297. module_exit(pxa27x_keypad_exit);
  298. MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
  299. MODULE_LICENSE("GPL");