locomokbd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2005 John Lenz
  3. *
  4. * Based on from xtkbd.c
  5. */
  6. /*
  7. * LoCoMo keyboard driver for Linux/ARM
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/config.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/input.h>
  30. #include <linux/delay.h>
  31. #include <linux/device.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/ioport.h>
  34. #include <asm/hardware/locomo.h>
  35. #include <asm/irq.h>
  36. MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
  37. MODULE_DESCRIPTION("LoCoMo keyboard driver");
  38. MODULE_LICENSE("GPL");
  39. #define LOCOMOKBD_NUMKEYS 128
  40. #define KEY_ACTIVITY KEY_F16
  41. #define KEY_CONTACT KEY_F18
  42. #define KEY_CENTER KEY_F15
  43. static unsigned char locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
  44. 0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */
  45. 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */
  47. 0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */
  49. KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */
  50. KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */
  51. KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */
  52. 0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */
  53. KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */
  54. 0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */
  55. KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */
  56. KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */
  57. };
  58. #define KB_ROWS 16
  59. #define KB_COLS 8
  60. #define KB_ROWMASK(r) (1 << (r))
  61. #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 )
  62. #define NR_SCANCODES 128
  63. #define KB_DELAY 8
  64. #define SCAN_INTERVAL (HZ/10)
  65. #define LOCOMOKBD_PRESSED 1
  66. struct locomokbd {
  67. unsigned char keycode[LOCOMOKBD_NUMKEYS];
  68. struct input_dev input;
  69. char phys[32];
  70. struct locomo_dev *ldev;
  71. unsigned long base;
  72. spinlock_t lock;
  73. struct timer_list timer;
  74. };
  75. /* helper functions for reading the keyboard matrix */
  76. static inline void locomokbd_charge_all(unsigned long membase)
  77. {
  78. locomo_writel(0x00FF, membase + LOCOMO_KSC);
  79. }
  80. static inline void locomokbd_activate_all(unsigned long membase)
  81. {
  82. unsigned long r;
  83. locomo_writel(0, membase + LOCOMO_KSC);
  84. r = locomo_readl(membase + LOCOMO_KIC);
  85. r &= 0xFEFF;
  86. locomo_writel(r, membase + LOCOMO_KIC);
  87. }
  88. static inline void locomokbd_activate_col(unsigned long membase, int col)
  89. {
  90. unsigned short nset;
  91. unsigned short nbset;
  92. nset = 0xFF & ~(1 << col);
  93. nbset = (nset << 8) + nset;
  94. locomo_writel(nbset, membase + LOCOMO_KSC);
  95. }
  96. static inline void locomokbd_reset_col(unsigned long membase, int col)
  97. {
  98. unsigned short nbset;
  99. nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
  100. locomo_writel(nbset, membase + LOCOMO_KSC);
  101. }
  102. /*
  103. * The LoCoMo keyboard only generates interrupts when a key is pressed.
  104. * So when a key is pressed, we enable a timer. This timer scans the
  105. * keyboard, and this is how we detect when the key is released.
  106. */
  107. /* Scan the hardware keyboard and push any changes up through the input layer */
  108. static void locomokbd_scankeyboard(struct locomokbd *locomokbd, struct pt_regs *regs)
  109. {
  110. unsigned int row, col, rowd, scancode;
  111. unsigned long flags;
  112. unsigned int num_pressed;
  113. unsigned long membase = locomokbd->base;
  114. spin_lock_irqsave(&locomokbd->lock, flags);
  115. if (regs)
  116. input_regs(&locomokbd->input, regs);
  117. locomokbd_charge_all(membase);
  118. num_pressed = 0;
  119. for (col = 0; col < KB_COLS; col++) {
  120. locomokbd_activate_col(membase, col);
  121. udelay(KB_DELAY);
  122. rowd = ~locomo_readl(membase + LOCOMO_KIB);
  123. for (row = 0; row < KB_ROWS; row++) {
  124. scancode = SCANCODE(col, row);
  125. if (rowd & KB_ROWMASK(row)) {
  126. num_pressed += 1;
  127. input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 1);
  128. } else {
  129. input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 0);
  130. }
  131. }
  132. locomokbd_reset_col(membase, col);
  133. }
  134. locomokbd_activate_all(membase);
  135. input_sync(&locomokbd->input);
  136. /* if any keys are pressed, enable the timer */
  137. if (num_pressed)
  138. mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
  139. spin_unlock_irqrestore(&locomokbd->lock, flags);
  140. }
  141. /*
  142. * LoCoMo keyboard interrupt handler.
  143. */
  144. static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  145. {
  146. struct locomokbd *locomokbd = dev_id;
  147. /** wait chattering delay **/
  148. udelay(100);
  149. locomokbd_scankeyboard(locomokbd, regs);
  150. return IRQ_HANDLED;
  151. }
  152. /*
  153. * LoCoMo timer checking for released keys
  154. */
  155. static void locomokbd_timer_callback(unsigned long data)
  156. {
  157. struct locomokbd *locomokbd = (struct locomokbd *) data;
  158. locomokbd_scankeyboard(locomokbd, NULL);
  159. }
  160. static int locomokbd_probe(struct locomo_dev *dev)
  161. {
  162. struct locomokbd *locomokbd;
  163. int i, ret;
  164. locomokbd = kmalloc(sizeof(struct locomokbd), GFP_KERNEL);
  165. if (!locomokbd)
  166. return -ENOMEM;
  167. memset(locomokbd, 0, sizeof(struct locomokbd));
  168. /* try and claim memory region */
  169. if (!request_mem_region((unsigned long) dev->mapbase,
  170. dev->length,
  171. LOCOMO_DRIVER_NAME(dev))) {
  172. ret = -EBUSY;
  173. printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
  174. goto free;
  175. }
  176. locomokbd->ldev = dev;
  177. locomo_set_drvdata(dev, locomokbd);
  178. locomokbd->base = (unsigned long) dev->mapbase;
  179. spin_lock_init(&locomokbd->lock);
  180. init_timer(&locomokbd->timer);
  181. locomokbd->timer.function = locomokbd_timer_callback;
  182. locomokbd->timer.data = (unsigned long) locomokbd;
  183. locomokbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  184. init_input_dev(&locomokbd->input);
  185. locomokbd->input.keycode = locomokbd->keycode;
  186. locomokbd->input.keycodesize = sizeof(unsigned char);
  187. locomokbd->input.keycodemax = ARRAY_SIZE(locomokbd_keycode);
  188. locomokbd->input.private = locomokbd;
  189. memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
  190. for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
  191. set_bit(locomokbd->keycode[i], locomokbd->input.keybit);
  192. clear_bit(0, locomokbd->input.keybit);
  193. strcpy(locomokbd->phys, "locomokbd/input0");
  194. locomokbd->input.name = "LoCoMo keyboard";
  195. locomokbd->input.phys = locomokbd->phys;
  196. locomokbd->input.id.bustype = BUS_XTKBD;
  197. locomokbd->input.id.vendor = 0x0001;
  198. locomokbd->input.id.product = 0x0001;
  199. locomokbd->input.id.version = 0x0100;
  200. /* attempt to get the interrupt */
  201. ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
  202. if (ret) {
  203. printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
  204. goto out;
  205. }
  206. input_register_device(&locomokbd->input);
  207. printk(KERN_INFO "input: LoCoMo keyboard on locomokbd\n");
  208. return 0;
  209. out:
  210. release_mem_region((unsigned long) dev->mapbase, dev->length);
  211. locomo_set_drvdata(dev, NULL);
  212. free:
  213. kfree(locomokbd);
  214. return ret;
  215. }
  216. static int locomokbd_remove(struct locomo_dev *dev)
  217. {
  218. struct locomokbd *locomokbd = locomo_get_drvdata(dev);
  219. free_irq(dev->irq[0], locomokbd);
  220. del_timer_sync(&locomokbd->timer);
  221. input_unregister_device(&locomokbd->input);
  222. locomo_set_drvdata(dev, NULL);
  223. release_mem_region((unsigned long) dev->mapbase, dev->length);
  224. kfree(locomokbd);
  225. return 0;
  226. }
  227. static struct locomo_driver keyboard_driver = {
  228. .drv = {
  229. .name = "locomokbd"
  230. },
  231. .devid = LOCOMO_DEVID_KEYBOARD,
  232. .probe = locomokbd_probe,
  233. .remove = locomokbd_remove,
  234. };
  235. static int __init locomokbd_init(void)
  236. {
  237. return locomo_driver_register(&keyboard_driver);
  238. }
  239. static void __exit locomokbd_exit(void)
  240. {
  241. locomo_driver_unregister(&keyboard_driver);
  242. }
  243. module_init(locomokbd_init);
  244. module_exit(locomokbd_exit);