locomokbd.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 long flags;
  96. unsigned int num_pressed;
  97. unsigned long membase = locomokbd->base;
  98. spin_lock_irqsave(&locomokbd->lock, flags);
  99. locomokbd_charge_all(membase);
  100. num_pressed = 0;
  101. for (col = 0; col < KB_COLS; col++) {
  102. locomokbd_activate_col(membase, col);
  103. udelay(KB_DELAY);
  104. rowd = ~locomo_readl(membase + LOCOMO_KIB);
  105. for (row = 0; row < KB_ROWS; row++) {
  106. unsigned int scancode, pressed, key;
  107. scancode = SCANCODE(col, row);
  108. pressed = rowd & KB_ROWMASK(row);
  109. key = locomokbd->keycode[scancode];
  110. input_report_key(locomokbd->input, key, pressed);
  111. if (likely(!pressed))
  112. continue;
  113. num_pressed++;
  114. /* The "Cancel/ESC" key is labeled "On/Off" on
  115. * Collie and Poodle and should suspend the device
  116. * if it was pressed for more than a second. */
  117. if (unlikely(key == KEY_ESC)) {
  118. if (!time_after(jiffies,
  119. locomokbd->suspend_jiffies + HZ))
  120. continue;
  121. if (locomokbd->count_cancel++
  122. != (HZ/SCAN_INTERVAL + 1))
  123. continue;
  124. input_event(locomokbd->input, EV_PWR,
  125. KEY_SUSPEND, 1);
  126. locomokbd->suspend_jiffies = jiffies;
  127. } else
  128. locomokbd->count_cancel = 0;
  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. else
  138. locomokbd->count_cancel = 0;
  139. spin_unlock_irqrestore(&locomokbd->lock, flags);
  140. }
  141. /*
  142. * LoCoMo keyboard interrupt handler.
  143. */
  144. static irqreturn_t locomokbd_interrupt(int irq, void *dev_id)
  145. {
  146. struct locomokbd *locomokbd = dev_id;
  147. u16 r;
  148. r = locomo_readl(locomokbd->base + LOCOMO_KIC);
  149. if ((r & 0x0001) == 0)
  150. return IRQ_HANDLED;
  151. locomo_writel(r & ~0x0100, locomokbd->base + LOCOMO_KIC); /* Ack */
  152. /** wait chattering delay **/
  153. udelay(100);
  154. locomokbd_scankeyboard(locomokbd);
  155. return IRQ_HANDLED;
  156. }
  157. /*
  158. * LoCoMo timer checking for released keys
  159. */
  160. static void locomokbd_timer_callback(struct timer_list *t)
  161. {
  162. struct locomokbd *locomokbd = from_timer(locomokbd, t, timer);
  163. locomokbd_scankeyboard(locomokbd);
  164. }
  165. static int locomokbd_open(struct input_dev *dev)
  166. {
  167. struct locomokbd *locomokbd = input_get_drvdata(dev);
  168. u16 r;
  169. r = locomo_readl(locomokbd->base + LOCOMO_KIC) | 0x0010;
  170. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  171. return 0;
  172. }
  173. static void locomokbd_close(struct input_dev *dev)
  174. {
  175. struct locomokbd *locomokbd = input_get_drvdata(dev);
  176. u16 r;
  177. r = locomo_readl(locomokbd->base + LOCOMO_KIC) & ~0x0010;
  178. locomo_writel(r, locomokbd->base + LOCOMO_KIC);
  179. }
  180. static int locomokbd_probe(struct locomo_dev *dev)
  181. {
  182. struct locomokbd *locomokbd;
  183. struct input_dev *input_dev;
  184. int i, err;
  185. locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
  186. input_dev = input_allocate_device();
  187. if (!locomokbd || !input_dev) {
  188. err = -ENOMEM;
  189. goto err_free_mem;
  190. }
  191. /* try and claim memory region */
  192. if (!request_mem_region((unsigned long) dev->mapbase,
  193. dev->length,
  194. LOCOMO_DRIVER_NAME(dev))) {
  195. err = -EBUSY;
  196. printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
  197. goto err_free_mem;
  198. }
  199. locomo_set_drvdata(dev, locomokbd);
  200. locomokbd->base = (unsigned long) dev->mapbase;
  201. spin_lock_init(&locomokbd->lock);
  202. timer_setup(&locomokbd->timer, locomokbd_timer_callback, 0);
  203. locomokbd->suspend_jiffies = jiffies;
  204. locomokbd->input = input_dev;
  205. strcpy(locomokbd->phys, "locomokbd/input0");
  206. input_dev->name = "LoCoMo keyboard";
  207. input_dev->phys = locomokbd->phys;
  208. input_dev->id.bustype = BUS_HOST;
  209. input_dev->id.vendor = 0x0001;
  210. input_dev->id.product = 0x0001;
  211. input_dev->id.version = 0x0100;
  212. input_dev->open = locomokbd_open;
  213. input_dev->close = locomokbd_close;
  214. input_dev->dev.parent = &dev->dev;
  215. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
  216. BIT_MASK(EV_PWR);
  217. input_dev->keycode = locomokbd->keycode;
  218. input_dev->keycodesize = sizeof(locomokbd_keycode[0]);
  219. input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
  220. input_set_drvdata(input_dev, locomokbd);
  221. memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
  222. for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
  223. set_bit(locomokbd->keycode[i], input_dev->keybit);
  224. clear_bit(0, input_dev->keybit);
  225. /* attempt to get the interrupt */
  226. err = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
  227. if (err) {
  228. printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
  229. goto err_release_region;
  230. }
  231. err = input_register_device(locomokbd->input);
  232. if (err)
  233. goto err_free_irq;
  234. return 0;
  235. err_free_irq:
  236. free_irq(dev->irq[0], locomokbd);
  237. err_release_region:
  238. release_mem_region((unsigned long) dev->mapbase, dev->length);
  239. locomo_set_drvdata(dev, NULL);
  240. err_free_mem:
  241. input_free_device(input_dev);
  242. kfree(locomokbd);
  243. return err;
  244. }
  245. static int locomokbd_remove(struct locomo_dev *dev)
  246. {
  247. struct locomokbd *locomokbd = locomo_get_drvdata(dev);
  248. free_irq(dev->irq[0], locomokbd);
  249. del_timer_sync(&locomokbd->timer);
  250. input_unregister_device(locomokbd->input);
  251. locomo_set_drvdata(dev, NULL);
  252. release_mem_region((unsigned long) dev->mapbase, dev->length);
  253. kfree(locomokbd);
  254. return 0;
  255. }
  256. static struct locomo_driver keyboard_driver = {
  257. .drv = {
  258. .name = "locomokbd"
  259. },
  260. .devid = LOCOMO_DEVID_KEYBOARD,
  261. .probe = locomokbd_probe,
  262. .remove = locomokbd_remove,
  263. };
  264. static int __init locomokbd_init(void)
  265. {
  266. return locomo_driver_register(&keyboard_driver);
  267. }
  268. static void __exit locomokbd_exit(void)
  269. {
  270. locomo_driver_unregister(&keyboard_driver);
  271. }
  272. module_init(locomokbd_init);
  273. module_exit(locomokbd_exit);