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