locomokbd.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * LoCoMo keyboard driver for Linux-based ARM PDAs:
  3. * - SHARP Zaurus Collie (SL-5500)
  4. * - SHARP Zaurus Poodle (SL-5600)
  5. *
  6. * Copyright (c) 2005 John Lenz
  7. * Based on from xtkbd.c
  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 const unsigned char
  43. 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 KB_DELAY 8
  63. #define SCAN_INTERVAL (HZ/10)
  64. struct locomokbd {
  65. unsigned char keycode[LOCOMOKBD_NUMKEYS];
  66. struct input_dev *input;
  67. char phys[32];
  68. unsigned long base;
  69. spinlock_t lock;
  70. struct timer_list timer;
  71. unsigned long suspend_jiffies;
  72. unsigned int count_cancel;
  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;
  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. unsigned int scancode, pressed, key;
  122. scancode = SCANCODE(col, row);
  123. pressed = rowd & KB_ROWMASK(row);
  124. key = locomokbd->keycode[scancode];
  125. input_report_key(locomokbd->input, key, pressed);
  126. if (likely(!pressed))
  127. continue;
  128. num_pressed++;
  129. /* The "Cancel/ESC" key is labeled "On/Off" on
  130. * Collie and Poodle and should suspend the device
  131. * if it was pressed for more than a second. */
  132. if (unlikely(key == KEY_ESC)) {
  133. if (!time_after(jiffies,
  134. locomokbd->suspend_jiffies + HZ))
  135. continue;
  136. if (locomokbd->count_cancel++
  137. != (HZ/SCAN_INTERVAL + 1))
  138. continue;
  139. input_event(locomokbd->input, EV_PWR,
  140. KEY_SUSPEND, 1);
  141. locomokbd->suspend_jiffies = jiffies;
  142. } else
  143. locomokbd->count_cancel = 0;
  144. }
  145. locomokbd_reset_col(membase, col);
  146. }
  147. locomokbd_activate_all(membase);
  148. input_sync(locomokbd->input);
  149. /* if any keys are pressed, enable the timer */
  150. if (num_pressed)
  151. mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
  152. else
  153. locomokbd->count_cancel = 0;
  154. spin_unlock_irqrestore(&locomokbd->lock, flags);
  155. }
  156. /*
  157. * LoCoMo keyboard interrupt handler.
  158. */
  159. static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)
  160. {
  161. struct locomokbd *locomokbd = dev_id;
  162. u16 r;
  163. r = locomo_readl(locomokbd->base + LOCOMO_KIC);
  164. if ((r & 0x0001) == 0)
  165. return IRQ_HANDLED;
  166. locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */
  167. /** wait chattering delay **/
  168. udelay(100);
  169. locomokbd_scankeyboard(locomokbd);
  170. return IRQ_HANDLED;
  171. }
  172. /*
  173. * LoCoMo timer checking for released keys
  174. */
  175. static void locomokbd_timer_callback(unsigned long data)
  176. {
  177. struct locomokbd *locomokbd = (struct locomokbd *) data;
  178. locomokbd_scankeyboard(locomokbd);
  179. }
  180. static int locomokbd_open(struct input_dev *dev)
  181. {
  182. struct locomokbd *locomokbd = input_get_drvdata(dev);
  183. u16 r;
  184. r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010;
  185. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  186. return 0;
  187. }
  188. static void locomokbd_close(struct input_dev *dev)
  189. {
  190. struct locomokbd *locomokbd = input_get_drvdata(dev);
  191. u16 r;
  192. r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010;
  193. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  194. }
  195. static int locomokbd_probe(struct locomo_dev *dev)
  196. {
  197. struct locomokbd *locomokbd;
  198. struct input_dev *input_dev;
  199. int i, err;
  200. locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
  201. input_dev = input_allocate_device();
  202. if (!locomokbd || !input_dev) {
  203. err = -ENOMEM;
  204. goto err_free_mem;
  205. }
  206. /* try and claim memory region */
  207. if (!request_mem_region((unsigned long) dev->mapbase,
  208. dev->length,
  209. LOCOMO_DRIVER_NAME(dev))) {
  210. err = -EBUSY;
  211. printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
  212. goto err_free_mem;
  213. }
  214. locomo_set_drvdata(dev, locomokbd);
  215. locomokbd->base = (unsigned long) dev->mapbase;
  216. spin_lock_init(&locomokbd->lock);
  217. init_timer(&locomokbd->timer);
  218. locomokbd->timer.function = locomokbd_timer_callback;
  219. locomokbd->timer.data = (unsigned long) locomokbd;
  220. locomokbd->suspend_jiffies = jiffies;
  221. locomokbd->input = input_dev;
  222. strcpy(locomokbd->phys, "locomokbd/input0");
  223. input_dev->name = "LoCoMo keyboard";
  224. input_dev->phys = locomokbd->phys;
  225. input_dev->id.bustype = BUS_HOST;
  226. input_dev->id.vendor = 0x0001;
  227. input_dev->id.product = 0x0001;
  228. input_dev->id.version = 0x0100;
  229. input_dev->open = locomokbd_open;
  230. input_dev->close = locomokbd_close;
  231. input_dev->dev.parent = &dev->dev;
  232. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
  233. BIT_MASK(EV_PWR);
  234. input_dev->keycode = locomokbd->keycode;
  235. input_dev->keycodesize = sizeof(locomokbd_keycode[0]);
  236. input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
  237. input_set_drvdata(input_dev, locomokbd);
  238. memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
  239. for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
  240. set_bit(locomokbd->keycode[i], input_dev->keybit);
  241. clear_bit(0, input_dev->keybit);
  242. /* attempt to get the interrupt */
  243. err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
  244. if (err) {
  245. printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
  246. goto err_release_region;
  247. }
  248. err = input_register_device(locomokbd->input);
  249. if (err)
  250. goto err_free_irq;
  251. return 0;
  252. err_free_irq:
  253. free_irq(dev->irq[0], locomokbd);
  254. err_release_region:
  255. release_mem_region((unsigned long) dev->mapbase, dev->length);
  256. locomo_set_drvdata(dev, NULL);
  257. err_free_mem:
  258. input_free_device(input_dev);
  259. kfree(locomokbd);
  260. return err;
  261. }
  262. static int locomokbd_remove(struct locomo_dev *dev)
  263. {
  264. struct locomokbd *locomokbd = locomo_get_drvdata(dev);
  265. free_irq(dev->irq[0], locomokbd);
  266. del_timer_sync(&locomokbd->timer);
  267. input_unregister_device(locomokbd->input);
  268. locomo_set_drvdata(dev, NULL);
  269. release_mem_region((unsigned long) dev->mapbase, dev->length);
  270. kfree(locomokbd);
  271. return 0;
  272. }
  273. static struct locomo_driver keyboard_driver = {
  274. .drv = {
  275. .name = "locomokbd"
  276. },
  277. .devid = LOCOMO_DEVID_KEYBOARD,
  278. .probe = locomokbd_probe,
  279. .remove = locomokbd_remove,
  280. };
  281. static int __init locomokbd_init(void)
  282. {
  283. return locomo_driver_register(&keyboard_driver);
  284. }
  285. static void __exit locomokbd_exit(void)
  286. {
  287. locomo_driver_unregister(&keyboard_driver);
  288. }
  289. module_init(locomokbd_init);
  290. module_exit(locomokbd_exit);