locomokbd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. input_regs(locomokbd->input, regs);
  116. locomokbd_charge_all(membase);
  117. num_pressed = 0;
  118. for (col = 0; col < KB_COLS; col++) {
  119. locomokbd_activate_col(membase, col);
  120. udelay(KB_DELAY);
  121. rowd = ~locomo_readl(membase + LOCOMO_KIB);
  122. for (row = 0; row < KB_ROWS; row++) {
  123. scancode = SCANCODE(col, row);
  124. if (rowd & KB_ROWMASK(row)) {
  125. num_pressed += 1;
  126. input_report_key(locomokbd->input, locomokbd->keycode[scancode], 1);
  127. } else {
  128. input_report_key(locomokbd->input, locomokbd->keycode[scancode], 0);
  129. }
  130. }
  131. locomokbd_reset_col(membase, col);
  132. }
  133. locomokbd_activate_all(membase);
  134. input_sync(locomokbd->input);
  135. /* if any keys are pressed, enable the timer */
  136. if (num_pressed)
  137. mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
  138. spin_unlock_irqrestore(&locomokbd->lock, flags);
  139. }
  140. /*
  141. * LoCoMo keyboard interrupt handler.
  142. */
  143. static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  144. {
  145. struct locomokbd *locomokbd = dev_id;
  146. /** wait chattering delay **/
  147. udelay(100);
  148. locomokbd_scankeyboard(locomokbd, regs);
  149. return IRQ_HANDLED;
  150. }
  151. /*
  152. * LoCoMo timer checking for released keys
  153. */
  154. static void locomokbd_timer_callback(unsigned long data)
  155. {
  156. struct locomokbd *locomokbd = (struct locomokbd *) data;
  157. locomokbd_scankeyboard(locomokbd, NULL);
  158. }
  159. static int locomokbd_probe(struct locomo_dev *dev)
  160. {
  161. struct locomokbd *locomokbd;
  162. struct input_dev *input_dev;
  163. int i, ret;
  164. locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
  165. input_dev = input_allocate_device();
  166. if (!locomokbd || !input_dev) {
  167. ret = -ENOMEM;
  168. goto free;
  169. }
  170. /* try and claim memory region */
  171. if (!request_mem_region((unsigned long) dev->mapbase,
  172. dev->length,
  173. LOCOMO_DRIVER_NAME(dev))) {
  174. ret = -EBUSY;
  175. printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
  176. goto free;
  177. }
  178. locomokbd->ldev = dev;
  179. locomo_set_drvdata(dev, locomokbd);
  180. locomokbd->base = (unsigned long) dev->mapbase;
  181. spin_lock_init(&locomokbd->lock);
  182. init_timer(&locomokbd->timer);
  183. locomokbd->timer.function = locomokbd_timer_callback;
  184. locomokbd->timer.data = (unsigned long) locomokbd;
  185. locomokbd->input = input_dev;
  186. strcpy(locomokbd->phys, "locomokbd/input0");
  187. input_dev->name = "LoCoMo keyboard";
  188. input_dev->phys = locomokbd->phys;
  189. input_dev->id.bustype = BUS_HOST;
  190. input_dev->id.vendor = 0x0001;
  191. input_dev->id.product = 0x0001;
  192. input_dev->id.version = 0x0100;
  193. input_dev->private = locomokbd;
  194. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  195. input_dev->keycode = locomokbd->keycode;
  196. input_dev->keycodesize = sizeof(unsigned char);
  197. input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
  198. memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
  199. for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
  200. set_bit(locomokbd->keycode[i], input_dev->keybit);
  201. clear_bit(0, input_dev->keybit);
  202. /* attempt to get the interrupt */
  203. ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
  204. if (ret) {
  205. printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
  206. goto out;
  207. }
  208. input_register_device(locomokbd->input);
  209. return 0;
  210. out:
  211. release_mem_region((unsigned long) dev->mapbase, dev->length);
  212. locomo_set_drvdata(dev, NULL);
  213. free:
  214. input_free_device(input_dev);
  215. kfree(locomokbd);
  216. return ret;
  217. }
  218. static int locomokbd_remove(struct locomo_dev *dev)
  219. {
  220. struct locomokbd *locomokbd = locomo_get_drvdata(dev);
  221. free_irq(dev->irq[0], locomokbd);
  222. del_timer_sync(&locomokbd->timer);
  223. input_unregister_device(locomokbd->input);
  224. locomo_set_drvdata(dev, NULL);
  225. release_mem_region((unsigned long) dev->mapbase, dev->length);
  226. kfree(locomokbd);
  227. return 0;
  228. }
  229. static struct locomo_driver keyboard_driver = {
  230. .drv = {
  231. .name = "locomokbd"
  232. },
  233. .devid = LOCOMO_DEVID_KEYBOARD,
  234. .probe = locomokbd_probe,
  235. .remove = locomokbd_remove,
  236. };
  237. static int __init locomokbd_init(void)
  238. {
  239. return locomo_driver_register(&keyboard_driver);
  240. }
  241. static void __exit locomokbd_exit(void)
  242. {
  243. locomo_driver_unregister(&keyboard_driver);
  244. }
  245. module_init(locomokbd_init);
  246. module_exit(locomokbd_exit);