locomokbd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LoCoMo keyboard driver for Linux-based ARM PDAs:
  4. * - SHARP Zaurus Collie (SL-5500)
  5. * - SHARP Zaurus Poodle (SL-5600)
  6. *
  7. * Copyright (c) 2005 John Lenz
  8. * Based on from xtkbd.c
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/input.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <asm/hardware/locomo.h>
  19. #include <asm/irq.h>
  20. MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
  21. MODULE_DESCRIPTION("LoCoMo keyboard driver");
  22. MODULE_LICENSE("GPL");
  23. #define LOCOMOKBD_NUMKEYS 128
  24. #define KEY_ACTIVITY KEY_F16
  25. #define KEY_CONTACT KEY_F18
  26. #define KEY_CENTER KEY_F15
  27. static const unsigned char
  28. locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
  29. 0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0, /* 0 - 9 */
  30. 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT, /* 10 - 19 */
  31. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 29 */
  32. 0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0, /* 30 - 39 */
  33. 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT, /* 40 - 49 */
  34. KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T, /* 50 - 59 */
  35. KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0, /* 60 - 69 */
  36. KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0, /* 70 - 79 */
  37. 0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J, /* 80 - 89 */
  38. KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0, /* 90 - 99 */
  39. 0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A, /* 100 - 109 */
  40. KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0, /* 110 - 119 */
  41. KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */
  42. };
  43. #define KB_ROWS 16
  44. #define KB_COLS 8
  45. #define KB_ROWMASK(r) (1 << (r))
  46. #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 )
  47. #define KB_DELAY 8
  48. #define SCAN_INTERVAL (HZ/10)
  49. struct locomokbd {
  50. unsigned char keycode[LOCOMOKBD_NUMKEYS];
  51. struct input_dev *input;
  52. char phys[32];
  53. unsigned long base;
  54. spinlock_t lock;
  55. struct timer_list timer;
  56. unsigned long suspend_jiffies;
  57. unsigned int count_cancel;
  58. };
  59. /* helper functions for reading the keyboard matrix */
  60. static inline void locomokbd_charge_all(unsigned long membase)
  61. {
  62. locomo_writel(0x00FF, membase + LOCOMO_KSC);
  63. }
  64. static inline void locomokbd_activate_all(unsigned long membase)
  65. {
  66. unsigned long r;
  67. locomo_writel(0, membase + LOCOMO_KSC);
  68. r = locomo_readl(membase + LOCOMO_KIC);
  69. r &= 0xFEFF;
  70. locomo_writel(r, membase + LOCOMO_KIC);
  71. }
  72. static inline void locomokbd_activate_col(unsigned long membase, int col)
  73. {
  74. unsigned short nset;
  75. unsigned short nbset;
  76. nset = 0xFF & ~(1 << col);
  77. nbset = (nset << 8) + nset;
  78. locomo_writel(nbset, membase + LOCOMO_KSC);
  79. }
  80. static inline void locomokbd_reset_col(unsigned long membase, int col)
  81. {
  82. unsigned short nbset;
  83. nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
  84. locomo_writel(nbset, membase + LOCOMO_KSC);
  85. }
  86. /*
  87. * The LoCoMo keyboard only generates interrupts when a key is pressed.
  88. * So when a key is pressed, we enable a timer. This timer scans the
  89. * keyboard, and this is how we detect when the key is released.
  90. */
  91. /* Scan the hardware keyboard and push any changes up through the input layer */
  92. static void locomokbd_scankeyboard(struct locomokbd *locomokbd)
  93. {
  94. unsigned int row, col, rowd;
  95. unsigned int num_pressed;
  96. unsigned long membase = locomokbd->base;
  97. guard(spinlock_irqsave)(&locomokbd->lock);
  98. locomokbd_charge_all(membase);
  99. num_pressed = 0;
  100. for (col = 0; col < KB_COLS; col++) {
  101. locomokbd_activate_col(membase, col);
  102. udelay(KB_DELAY);
  103. rowd = ~locomo_readl(membase + LOCOMO_KIB);
  104. for (row = 0; row < KB_ROWS; row++) {
  105. unsigned int scancode, pressed, key;
  106. scancode = SCANCODE(col, row);
  107. pressed = rowd & KB_ROWMASK(row);
  108. key = locomokbd->keycode[scancode];
  109. input_report_key(locomokbd->input, key, pressed);
  110. if (likely(!pressed))
  111. continue;
  112. num_pressed++;
  113. /* The "Cancel/ESC" key is labeled "On/Off" on
  114. * Collie and Poodle and should suspend the device
  115. * if it was pressed for more than a second. */
  116. if (unlikely(key == KEY_ESC)) {
  117. if (!time_after(jiffies,
  118. locomokbd->suspend_jiffies + HZ))
  119. continue;
  120. if (locomokbd->count_cancel++
  121. != (HZ/SCAN_INTERVAL + 1))
  122. continue;
  123. input_event(locomokbd->input, EV_PWR,
  124. KEY_SUSPEND, 1);
  125. locomokbd->suspend_jiffies = jiffies;
  126. } else
  127. locomokbd->count_cancel = 0;
  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. else
  137. locomokbd->count_cancel = 0;
  138. }
  139. /*
  140. * LoCoMo keyboard interrupt handler.
  141. */
  142. static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)
  143. {
  144. struct locomokbd *locomokbd = dev_id;
  145. u16 r;
  146. r = locomo_readl(locomokbd->base + LOCOMO_KIC);
  147. if ((r & 0x0001) == 0)
  148. return IRQ_HANDLED;
  149. locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */
  150. /** wait chattering delay **/
  151. udelay(100);
  152. locomokbd_scankeyboard(locomokbd);
  153. return IRQ_HANDLED;
  154. }
  155. /*
  156. * LoCoMo timer checking for released keys
  157. */
  158. static void locomokbd_timer_callback(struct timer_list *t)
  159. {
  160. struct locomokbd *locomokbd = timer_container_of(locomokbd, t, timer);
  161. locomokbd_scankeyboard(locomokbd);
  162. }
  163. static int locomokbd_open(struct input_dev *dev)
  164. {
  165. struct locomokbd *locomokbd = input_get_drvdata(dev);
  166. u16 r;
  167. r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010;
  168. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  169. return 0;
  170. }
  171. static void locomokbd_close(struct input_dev *dev)
  172. {
  173. struct locomokbd *locomokbd = input_get_drvdata(dev);
  174. u16 r;
  175. r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010;
  176. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  177. }
  178. static int locomokbd_probe(struct locomo_dev *dev)
  179. {
  180. struct locomokbd *locomokbd;
  181. struct input_dev *input_dev;
  182. int i, err;
  183. locomokbd = kzalloc_obj(*locomokbd);
  184. input_dev = input_allocate_device();
  185. if (!locomokbd || !input_dev) {
  186. err = -ENOMEM;
  187. goto err_free_mem;
  188. }
  189. /* try and claim memory region */
  190. if (!request_mem_region((unsigned long) dev->mapbase,
  191. dev->length,
  192. LOCOMO_DRIVER_NAME(dev))) {
  193. err = -EBUSY;
  194. printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
  195. goto err_free_mem;
  196. }
  197. locomo_set_drvdata(dev, locomokbd);
  198. locomokbd->base = (unsigned long) dev->mapbase;
  199. spin_lock_init(&locomokbd->lock);
  200. timer_setup(&locomokbd->timer, locomokbd_timer_callback, 0);
  201. locomokbd->suspend_jiffies = jiffies;
  202. locomokbd->input = input_dev;
  203. strcpy(locomokbd->phys, "locomokbd/input0");
  204. input_dev->name = "LoCoMo keyboard";
  205. input_dev->phys = locomokbd->phys;
  206. input_dev->id.bustype = BUS_HOST;
  207. input_dev->id.vendor = 0x0001;
  208. input_dev->id.product = 0x0001;
  209. input_dev->id.version = 0x0100;
  210. input_dev->open = locomokbd_open;
  211. input_dev->close = locomokbd_close;
  212. input_dev->dev.parent = &dev->dev;
  213. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
  214. BIT_MASK(EV_PWR);
  215. input_dev->keycode = locomokbd->keycode;
  216. input_dev->keycodesize = sizeof(locomokbd_keycode[0]);
  217. input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
  218. input_set_drvdata(input_dev, locomokbd);
  219. memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
  220. for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
  221. set_bit(locomokbd->keycode[i], input_dev->keybit);
  222. clear_bit(0, input_dev->keybit);
  223. /* attempt to get the interrupt */
  224. err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
  225. if (err) {
  226. printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
  227. goto err_release_region;
  228. }
  229. err = input_register_device(locomokbd->input);
  230. if (err)
  231. goto err_free_irq;
  232. return 0;
  233. err_free_irq:
  234. free_irq(dev->irq[0], locomokbd);
  235. err_release_region:
  236. release_mem_region((unsigned long) dev->mapbase, dev->length);
  237. locomo_set_drvdata(dev, NULL);
  238. err_free_mem:
  239. input_free_device(input_dev);
  240. kfree(locomokbd);
  241. return err;
  242. }
  243. static void locomokbd_remove(struct locomo_dev *dev)
  244. {
  245. struct locomokbd *locomokbd = locomo_get_drvdata(dev);
  246. free_irq(dev->irq[0], locomokbd);
  247. timer_shutdown_sync(&locomokbd->timer);
  248. input_unregister_device(locomokbd->input);
  249. locomo_set_drvdata(dev, NULL);
  250. release_mem_region((unsigned long) dev->mapbase, dev->length);
  251. kfree(locomokbd);
  252. }
  253. static struct locomo_driver keyboard_driver = {
  254. .drv = {
  255. .name = "locomokbd"
  256. },
  257. .devid = LOCOMO_DEVID_KEYBOARD,
  258. .probe = locomokbd_probe,
  259. .remove = locomokbd_remove,
  260. };
  261. static int __init locomokbd_init(void)
  262. {
  263. return locomo_driver_register(&keyboard_driver);
  264. }
  265. static void __exit locomokbd_exit(void)
  266. {
  267. locomo_driver_unregister(&keyboard_driver);
  268. }
  269. module_init(locomokbd_init);
  270. module_exit(locomokbd_exit);