ams_delta_serio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Amstrad E3 (Delta) keyboard port driver
  3. *
  4. * Copyright (c) 2006 Matt Callow
  5. * Copyright (c) 2010 Janusz Krzysztofik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * Thanks to Cliff Lawson for his help
  12. *
  13. * The Amstrad Delta keyboard (aka mailboard) uses normal PC-AT style serial
  14. * transmission. The keyboard port is formed of two GPIO lines, for clock
  15. * and data. Due to strict timing requirements of the interface,
  16. * the serial data stream is read and processed by a FIQ handler.
  17. * The resulting words are fetched by this driver from a circular buffer.
  18. *
  19. * Standard AT keyboard driver (atkbd) is used for handling the keyboard data.
  20. * However, when used with the E3 mailboard that producecs non-standard
  21. * scancodes, a custom key table must be prepared and loaded from userspace.
  22. */
  23. #include <linux/gpio.h>
  24. #include <linux/irq.h>
  25. #include <linux/serio.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <asm/mach-types.h>
  29. #include <mach/board-ams-delta.h>
  30. #include <mach/ams-delta-fiq.h>
  31. MODULE_AUTHOR("Matt Callow");
  32. MODULE_DESCRIPTION("AMS Delta (E3) keyboard port driver");
  33. MODULE_LICENSE("GPL");
  34. static struct serio *ams_delta_serio;
  35. static int check_data(int data)
  36. {
  37. int i, parity = 0;
  38. /* check valid stop bit */
  39. if (!(data & 0x400)) {
  40. dev_warn(&ams_delta_serio->dev,
  41. "invalid stop bit, data=0x%X\n",
  42. data);
  43. return SERIO_FRAME;
  44. }
  45. /* calculate the parity */
  46. for (i = 1; i < 10; i++) {
  47. if (data & (1 << i))
  48. parity++;
  49. }
  50. /* it should be odd */
  51. if (!(parity & 0x01)) {
  52. dev_warn(&ams_delta_serio->dev,
  53. "parity check failed, data=0x%X parity=0x%X\n",
  54. data, parity);
  55. return SERIO_PARITY;
  56. }
  57. return 0;
  58. }
  59. static irqreturn_t ams_delta_serio_interrupt(int irq, void *dev_id)
  60. {
  61. int *circ_buff = &fiq_buffer[FIQ_CIRC_BUFF];
  62. int data, dfl;
  63. u8 scancode;
  64. fiq_buffer[FIQ_IRQ_PEND] = 0;
  65. /*
  66. * Read data from the circular buffer, check it
  67. * and then pass it on the serio
  68. */
  69. while (fiq_buffer[FIQ_KEYS_CNT] > 0) {
  70. data = circ_buff[fiq_buffer[FIQ_HEAD_OFFSET]++];
  71. fiq_buffer[FIQ_KEYS_CNT]--;
  72. if (fiq_buffer[FIQ_HEAD_OFFSET] == fiq_buffer[FIQ_BUF_LEN])
  73. fiq_buffer[FIQ_HEAD_OFFSET] = 0;
  74. dfl = check_data(data);
  75. scancode = (u8) (data >> 1) & 0xFF;
  76. serio_interrupt(ams_delta_serio, scancode, dfl);
  77. }
  78. return IRQ_HANDLED;
  79. }
  80. static int ams_delta_serio_open(struct serio *serio)
  81. {
  82. /* enable keyboard */
  83. gpio_set_value(AMS_DELTA_GPIO_PIN_KEYBRD_PWR, 1);
  84. return 0;
  85. }
  86. static void ams_delta_serio_close(struct serio *serio)
  87. {
  88. /* disable keyboard */
  89. gpio_set_value(AMS_DELTA_GPIO_PIN_KEYBRD_PWR, 0);
  90. }
  91. static const struct gpio ams_delta_gpios[] __initconst_or_module = {
  92. {
  93. .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_DATA,
  94. .flags = GPIOF_DIR_IN,
  95. .label = "serio-data",
  96. },
  97. {
  98. .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK,
  99. .flags = GPIOF_DIR_IN,
  100. .label = "serio-clock",
  101. },
  102. {
  103. .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_PWR,
  104. .flags = GPIOF_OUT_INIT_LOW,
  105. .label = "serio-power",
  106. },
  107. {
  108. .gpio = AMS_DELTA_GPIO_PIN_KEYBRD_DATAOUT,
  109. .flags = GPIOF_OUT_INIT_LOW,
  110. .label = "serio-dataout",
  111. },
  112. };
  113. static int __init ams_delta_serio_init(void)
  114. {
  115. int err;
  116. if (!machine_is_ams_delta())
  117. return -ENODEV;
  118. ams_delta_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  119. if (!ams_delta_serio)
  120. return -ENOMEM;
  121. ams_delta_serio->id.type = SERIO_8042;
  122. ams_delta_serio->open = ams_delta_serio_open;
  123. ams_delta_serio->close = ams_delta_serio_close;
  124. strlcpy(ams_delta_serio->name, "AMS DELTA keyboard adapter",
  125. sizeof(ams_delta_serio->name));
  126. strlcpy(ams_delta_serio->phys, "GPIO/serio0",
  127. sizeof(ams_delta_serio->phys));
  128. err = gpio_request_array(ams_delta_gpios,
  129. ARRAY_SIZE(ams_delta_gpios));
  130. if (err) {
  131. pr_err("ams_delta_serio: Couldn't request gpio pins\n");
  132. goto serio;
  133. }
  134. err = request_irq(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK),
  135. ams_delta_serio_interrupt, IRQ_TYPE_EDGE_RISING,
  136. "ams-delta-serio", 0);
  137. if (err < 0) {
  138. pr_err("ams_delta_serio: couldn't request gpio interrupt %d\n",
  139. gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK));
  140. goto gpio;
  141. }
  142. /*
  143. * Since GPIO register handling for keyboard clock pin is performed
  144. * at FIQ level, switch back from edge to simple interrupt handler
  145. * to avoid bad interaction.
  146. */
  147. irq_set_handler(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK),
  148. handle_simple_irq);
  149. serio_register_port(ams_delta_serio);
  150. dev_info(&ams_delta_serio->dev, "%s\n", ams_delta_serio->name);
  151. return 0;
  152. gpio:
  153. gpio_free_array(ams_delta_gpios,
  154. ARRAY_SIZE(ams_delta_gpios));
  155. serio:
  156. kfree(ams_delta_serio);
  157. return err;
  158. }
  159. module_init(ams_delta_serio_init);
  160. static void __exit ams_delta_serio_exit(void)
  161. {
  162. serio_unregister_port(ams_delta_serio);
  163. free_irq(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK), 0);
  164. gpio_free_array(ams_delta_gpios,
  165. ARRAY_SIZE(ams_delta_gpios));
  166. }
  167. module_exit(ams_delta_serio_exit);