locomokbd.c 8.0 KB

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