jornada680_kbd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * drivers/input/keyboard/jornada680_kbd.c
  3. *
  4. * HP Jornada 620/660/680/690 scan keyboard platform driver
  5. * Copyright (C) 2007 Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
  6. *
  7. * Based on hp680_keyb.c
  8. * Copyright (C) 2006 Paul Mundt
  9. * Copyright (C) 2005 Andriy Skulysh
  10. * Split from drivers/input/keyboard/hp600_keyb.c
  11. * Copyright (C) 2000 Yaegashi Takeshi (hp6xx kbd scan routine and translation table)
  12. * Copyright (C) 2000 Niibe Yutaka (HP620 Keyb translation table)
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/input.h>
  20. #include <linux/input-polldev.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <asm/delay.h>
  28. #include <asm/io.h>
  29. #define PCCR 0xa4000104
  30. #define PDCR 0xa4000106
  31. #define PECR 0xa4000108
  32. #define PFCR 0xa400010a
  33. #define PCDR 0xa4000124
  34. #define PDDR 0xa4000126
  35. #define PEDR 0xa4000128
  36. #define PFDR 0xa400012a
  37. #define PGDR 0xa400012c
  38. #define PHDR 0xa400012e
  39. #define PJDR 0xa4000130
  40. #define PKDR 0xa4000132
  41. #define PLDR 0xa4000134
  42. static const unsigned short jornada_scancodes[] = {
  43. /* PTD1 */ KEY_CAPSLOCK, KEY_MACRO, KEY_LEFTCTRL, 0, KEY_ESC, KEY_KP5, 0, 0, /* 1 -> 8 */
  44. KEY_F1, KEY_F2, KEY_F3, KEY_F8, KEY_F7, KEY_F6, KEY_F4, KEY_F5, /* 9 -> 16 */
  45. /* PTD5 */ KEY_SLASH, KEY_APOSTROPHE, KEY_ENTER, 0, KEY_Z, 0, 0, 0, /* 17 -> 24 */
  46. KEY_X, KEY_C, KEY_V, KEY_DOT, KEY_COMMA, KEY_M, KEY_B, KEY_N, /* 25 -> 32 */
  47. /* PTD7 */ KEY_KP2, KEY_KP6, KEY_KP3, 0, 0, 0, 0, 0, /* 33 -> 40 */
  48. KEY_F10, KEY_RO, KEY_F9, KEY_KP4, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_LEFTALT, KEY_HANJA, /* 41 -> 48 */
  49. /* PTE0 */ KEY_KATAKANA, KEY_KP0, KEY_GRAVE, 0, KEY_FINANCE, 0, 0, 0, /* 49 -> 56 */
  50. KEY_KPMINUS, KEY_HIRAGANA, KEY_SPACE, KEY_KPDOT, KEY_VOLUMEUP, 249, 0, 0, /* 57 -> 64 */
  51. /* PTE1 */ KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_BACKSLASH, 0, KEY_A, 0, 0, 0, /* 65 -> 72 */
  52. KEY_S, KEY_D, KEY_F, KEY_L, KEY_K, KEY_J, KEY_G, KEY_H, /* 73 -> 80 */
  53. /* PTE3 */ KEY_KP8, KEY_LEFTMETA, KEY_RIGHTSHIFT, 0, KEY_TAB, 0, 0, 0, /* 81 -> 88 */
  54. 0, KEY_LEFTSHIFT, KEY_KP7, KEY_KP9, KEY_KP1, KEY_F11, KEY_KPPLUS, KEY_KPASTERISK, /* 89 -> 96 */
  55. /* PTE6 */ KEY_P, KEY_LEFTBRACE, KEY_BACKSPACE, 0, KEY_Q, 0, 0, 0, /* 97 -> 104 */
  56. KEY_W, KEY_E, KEY_R, KEY_O, KEY_I, KEY_U, KEY_T, KEY_Y, /* 105 -> 112 */
  57. /* PTE7 */ KEY_0, KEY_MINUS, KEY_EQUAL, 0, KEY_1, 0, 0, 0, /* 113 -> 120 */
  58. KEY_2, KEY_3, KEY_4, KEY_9, KEY_8, KEY_7, KEY_5, KEY_6, /* 121 -> 128 */
  59. /* **** */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0
  61. };
  62. #define JORNADA_SCAN_SIZE 18
  63. struct jornadakbd {
  64. struct input_polled_dev *poll_dev;
  65. unsigned short keymap[ARRAY_SIZE(jornada_scancodes)];
  66. unsigned char length;
  67. unsigned char old_scan[JORNADA_SCAN_SIZE];
  68. unsigned char new_scan[JORNADA_SCAN_SIZE];
  69. };
  70. static void jornada_parse_kbd(struct jornadakbd *jornadakbd)
  71. {
  72. struct input_dev *input_dev = jornadakbd->poll_dev->input;
  73. unsigned short *keymap = jornadakbd->keymap;
  74. unsigned int sync_me = 0;
  75. unsigned int i, j;
  76. for (i = 0; i < JORNADA_SCAN_SIZE; i++) {
  77. unsigned char new = jornadakbd->new_scan[i];
  78. unsigned char old = jornadakbd->old_scan[i];
  79. unsigned int xor = new ^ old;
  80. if (xor == 0)
  81. continue;
  82. for (j = 0; j < 8; j++) {
  83. unsigned int bit = 1 << j;
  84. if (xor & bit) {
  85. unsigned int scancode = (i << 3) + j;
  86. input_event(input_dev,
  87. EV_MSC, MSC_SCAN, scancode);
  88. input_report_key(input_dev,
  89. keymap[scancode],
  90. !(new & bit));
  91. sync_me = 1;
  92. }
  93. }
  94. }
  95. if (sync_me)
  96. input_sync(input_dev);
  97. }
  98. static void jornada_scan_keyb(unsigned char *s)
  99. {
  100. int i;
  101. unsigned short ec_static, dc_static; /* = UINT16_t */
  102. unsigned char matrix_switch[] = {
  103. 0xfd, 0xff, /* PTD1 PD(1) */
  104. 0xdf, 0xff, /* PTD5 PD(5) */
  105. 0x7f, 0xff, /* PTD7 PD(7) */
  106. 0xff, 0xfe, /* PTE0 PE(0) */
  107. 0xff, 0xfd, /* PTE1 PE(1) */
  108. 0xff, 0xf7, /* PTE3 PE(3) */
  109. 0xff, 0xbf, /* PTE6 PE(6) */
  110. 0xff, 0x7f, /* PTE7 PE(7) */
  111. }, *t = matrix_switch;
  112. /* PD(x) :
  113. 1. 0xcc0c & (1~(1 << (2*(x)+1)))))
  114. 2. (0xf0cf & 0xfffff) */
  115. /* PE(x) :
  116. 1. 0xcc0c & 0xffff
  117. 2. 0xf0cf & (1~(1 << (2*(x)+1))))) */
  118. unsigned short matrix_PDE[] = {
  119. 0xcc04, 0xf0cf, /* PD(1) */
  120. 0xc40c, 0xf0cf, /* PD(5) */
  121. 0x4c0c, 0xf0cf, /* PD(7) */
  122. 0xcc0c, 0xf0cd, /* PE(0) */
  123. 0xcc0c, 0xf0c7, /* PE(1) */
  124. 0xcc0c, 0xf04f, /* PE(3) */
  125. 0xcc0c, 0xd0cf, /* PE(6) */
  126. 0xcc0c, 0x70cf, /* PE(7) */
  127. }, *y = matrix_PDE;
  128. /* Save these control reg bits */
  129. dc_static = (__raw_readw(PDCR) & (~0xcc0c));
  130. ec_static = (__raw_readw(PECR) & (~0xf0cf));
  131. for (i = 0; i < 8; i++) {
  132. /* disable output for all but the one we want to scan */
  133. __raw_writew((dc_static | *y++), PDCR);
  134. __raw_writew((ec_static | *y++), PECR);
  135. udelay(5);
  136. /* Get scanline row */
  137. __raw_writeb(*t++, PDDR);
  138. __raw_writeb(*t++, PEDR);
  139. udelay(50);
  140. /* Read data */
  141. *s++ = __raw_readb(PCDR);
  142. *s++ = __raw_readb(PFDR);
  143. }
  144. /* Scan no lines */
  145. __raw_writeb(0xff, PDDR);
  146. __raw_writeb(0xff, PEDR);
  147. /* Enable all scanlines */
  148. __raw_writew((dc_static | (0x5555 & 0xcc0c)),PDCR);
  149. __raw_writew((ec_static | (0x5555 & 0xf0cf)),PECR);
  150. /* Ignore extra keys and events */
  151. *s++ = __raw_readb(PGDR);
  152. *s++ = __raw_readb(PHDR);
  153. }
  154. static void jornadakbd680_poll(struct input_polled_dev *dev)
  155. {
  156. struct jornadakbd *jornadakbd = dev->private;
  157. jornada_scan_keyb(jornadakbd->new_scan);
  158. jornada_parse_kbd(jornadakbd);
  159. memcpy(jornadakbd->old_scan, jornadakbd->new_scan, JORNADA_SCAN_SIZE);
  160. }
  161. static int jornada680kbd_probe(struct platform_device *pdev)
  162. {
  163. struct jornadakbd *jornadakbd;
  164. struct input_polled_dev *poll_dev;
  165. struct input_dev *input_dev;
  166. int i, error;
  167. jornadakbd = devm_kzalloc(&pdev->dev, sizeof(struct jornadakbd),
  168. GFP_KERNEL);
  169. if (!jornadakbd)
  170. return -ENOMEM;
  171. poll_dev = devm_input_allocate_polled_device(&pdev->dev);
  172. if (!poll_dev) {
  173. dev_err(&pdev->dev, "failed to allocate polled input device\n");
  174. return -ENOMEM;
  175. }
  176. platform_set_drvdata(pdev, jornadakbd);
  177. jornadakbd->poll_dev = poll_dev;
  178. memcpy(jornadakbd->keymap, jornada_scancodes,
  179. sizeof(jornadakbd->keymap));
  180. poll_dev->private = jornadakbd;
  181. poll_dev->poll = jornadakbd680_poll;
  182. poll_dev->poll_interval = 50; /* msec */
  183. input_dev = poll_dev->input;
  184. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  185. input_dev->name = "HP Jornada 680 keyboard";
  186. input_dev->phys = "jornadakbd/input0";
  187. input_dev->keycode = jornadakbd->keymap;
  188. input_dev->keycodesize = sizeof(unsigned short);
  189. input_dev->keycodemax = ARRAY_SIZE(jornada_scancodes);
  190. input_dev->dev.parent = &pdev->dev;
  191. input_dev->id.bustype = BUS_HOST;
  192. for (i = 0; i < 128; i++)
  193. if (jornadakbd->keymap[i])
  194. __set_bit(jornadakbd->keymap[i], input_dev->keybit);
  195. __clear_bit(KEY_RESERVED, input_dev->keybit);
  196. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  197. error = input_register_polled_device(jornadakbd->poll_dev);
  198. if (error) {
  199. dev_err(&pdev->dev, "failed to register polled input device\n");
  200. return error;
  201. }
  202. return 0;
  203. }
  204. static struct platform_driver jornada680kbd_driver = {
  205. .driver = {
  206. .name = "jornada680_kbd",
  207. },
  208. .probe = jornada680kbd_probe,
  209. };
  210. module_platform_driver(jornada680kbd_driver);
  211. MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>");
  212. MODULE_DESCRIPTION("HP Jornada 620/660/680/690 Keyboard Driver");
  213. MODULE_LICENSE("GPL v2");
  214. MODULE_ALIAS("platform:jornada680_kbd");