mk712.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ICS MK712 touchscreen controller driver
  4. *
  5. * Copyright (c) 1999-2002 Transmeta Corporation
  6. * Copyright (c) 2005 Rick Koch <[email protected]>
  7. * Copyright (c) 2005 Vojtech Pavlik <[email protected]>
  8. */
  9. /*
  10. * This driver supports the ICS MicroClock MK712 TouchScreen controller,
  11. * found in Gateway AOL Connected Touchpad computers.
  12. *
  13. * Documentation for ICS MK712 can be found at:
  14. * https://www.idt.com/general-parts/mk712-touch-screen-controller
  15. */
  16. /*
  17. * 1999-12-18: original version, Daniel Quinlan
  18. * 1999-12-19: added anti-jitter code, report pen-up events, fixed mk712_poll
  19. * to use queue_empty, Nathan Laredo
  20. * 1999-12-20: improved random point rejection, Nathan Laredo
  21. * 2000-01-05: checked in new anti-jitter code, changed mouse protocol, fixed
  22. * queue code, added module options, other fixes, Daniel Quinlan
  23. * 2002-03-15: Clean up for kernel merge <[email protected]>
  24. * Fixed multi open race, fixed memory checks, fixed resource
  25. * allocation, fixed close/powerdown bug, switched to new init
  26. * 2005-01-18: Ported to 2.6 from 2.4.28, Rick Koch
  27. * 2005-02-05: Rewritten for the input layer, Vojtech Pavlik
  28. *
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/errno.h>
  34. #include <linux/delay.h>
  35. #include <linux/ioport.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/input.h>
  38. #include <asm/io.h>
  39. MODULE_AUTHOR("Daniel Quinlan <[email protected]>, Vojtech Pavlik <[email protected]>");
  40. MODULE_DESCRIPTION("ICS MicroClock MK712 TouchScreen driver");
  41. MODULE_LICENSE("GPL");
  42. static unsigned int mk712_io = 0x260; /* Also 0x200, 0x208, 0x300 */
  43. module_param_hw_named(io, mk712_io, uint, ioport, 0);
  44. MODULE_PARM_DESC(io, "I/O base address of MK712 touchscreen controller");
  45. static unsigned int mk712_irq = 10; /* Also 12, 14, 15 */
  46. module_param_hw_named(irq, mk712_irq, uint, irq, 0);
  47. MODULE_PARM_DESC(irq, "IRQ of MK712 touchscreen controller");
  48. /* eight 8-bit registers */
  49. #define MK712_STATUS 0
  50. #define MK712_X 2
  51. #define MK712_Y 4
  52. #define MK712_CONTROL 6
  53. #define MK712_RATE 7
  54. /* status */
  55. #define MK712_STATUS_TOUCH 0x10
  56. #define MK712_CONVERSION_COMPLETE 0x80
  57. /* control */
  58. #define MK712_ENABLE_INT 0x01
  59. #define MK712_INT_ON_CONVERSION_COMPLETE 0x02
  60. #define MK712_INT_ON_CHANGE_IN_TOUCH_STATUS 0x04
  61. #define MK712_ENABLE_PERIODIC_CONVERSIONS 0x10
  62. #define MK712_READ_ONE_POINT 0x20
  63. #define MK712_POWERUP 0x40
  64. static struct input_dev *mk712_dev;
  65. static DEFINE_SPINLOCK(mk712_lock);
  66. static irqreturn_t mk712_interrupt(int irq, void *dev_id)
  67. {
  68. unsigned char status;
  69. static int debounce = 1;
  70. static unsigned short last_x;
  71. static unsigned short last_y;
  72. spin_lock(&mk712_lock);
  73. status = inb(mk712_io + MK712_STATUS);
  74. if (~status & MK712_CONVERSION_COMPLETE) {
  75. debounce = 1;
  76. goto end;
  77. }
  78. if (~status & MK712_STATUS_TOUCH) {
  79. debounce = 1;
  80. input_report_key(mk712_dev, BTN_TOUCH, 0);
  81. goto end;
  82. }
  83. if (debounce) {
  84. debounce = 0;
  85. goto end;
  86. }
  87. input_report_key(mk712_dev, BTN_TOUCH, 1);
  88. input_report_abs(mk712_dev, ABS_X, last_x);
  89. input_report_abs(mk712_dev, ABS_Y, last_y);
  90. end:
  91. last_x = inw(mk712_io + MK712_X) & 0x0fff;
  92. last_y = inw(mk712_io + MK712_Y) & 0x0fff;
  93. input_sync(mk712_dev);
  94. spin_unlock(&mk712_lock);
  95. return IRQ_HANDLED;
  96. }
  97. static int mk712_open(struct input_dev *dev)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&mk712_lock, flags);
  101. outb(0, mk712_io + MK712_CONTROL); /* Reset */
  102. outb(MK712_ENABLE_INT | MK712_INT_ON_CONVERSION_COMPLETE |
  103. MK712_INT_ON_CHANGE_IN_TOUCH_STATUS |
  104. MK712_ENABLE_PERIODIC_CONVERSIONS |
  105. MK712_POWERUP, mk712_io + MK712_CONTROL);
  106. outb(10, mk712_io + MK712_RATE); /* 187 points per second */
  107. spin_unlock_irqrestore(&mk712_lock, flags);
  108. return 0;
  109. }
  110. static void mk712_close(struct input_dev *dev)
  111. {
  112. unsigned long flags;
  113. spin_lock_irqsave(&mk712_lock, flags);
  114. outb(0, mk712_io + MK712_CONTROL);
  115. spin_unlock_irqrestore(&mk712_lock, flags);
  116. }
  117. static int __init mk712_init(void)
  118. {
  119. int err;
  120. if (!request_region(mk712_io, 8, "mk712")) {
  121. printk(KERN_WARNING "mk712: unable to get IO region\n");
  122. return -ENODEV;
  123. }
  124. outb(0, mk712_io + MK712_CONTROL);
  125. if ((inw(mk712_io + MK712_X) & 0xf000) || /* Sanity check */
  126. (inw(mk712_io + MK712_Y) & 0xf000) ||
  127. (inw(mk712_io + MK712_STATUS) & 0xf333)) {
  128. printk(KERN_WARNING "mk712: device not present\n");
  129. err = -ENODEV;
  130. goto fail1;
  131. }
  132. mk712_dev = input_allocate_device();
  133. if (!mk712_dev) {
  134. printk(KERN_ERR "mk712: not enough memory\n");
  135. err = -ENOMEM;
  136. goto fail1;
  137. }
  138. mk712_dev->name = "ICS MicroClock MK712 TouchScreen";
  139. mk712_dev->phys = "isa0260/input0";
  140. mk712_dev->id.bustype = BUS_ISA;
  141. mk712_dev->id.vendor = 0x0005;
  142. mk712_dev->id.product = 0x0001;
  143. mk712_dev->id.version = 0x0100;
  144. mk712_dev->open = mk712_open;
  145. mk712_dev->close = mk712_close;
  146. mk712_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  147. mk712_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  148. input_set_abs_params(mk712_dev, ABS_X, 0, 0xfff, 88, 0);
  149. input_set_abs_params(mk712_dev, ABS_Y, 0, 0xfff, 88, 0);
  150. if (request_irq(mk712_irq, mk712_interrupt, 0, "mk712", mk712_dev)) {
  151. printk(KERN_WARNING "mk712: unable to get IRQ\n");
  152. err = -EBUSY;
  153. goto fail1;
  154. }
  155. err = input_register_device(mk712_dev);
  156. if (err)
  157. goto fail2;
  158. return 0;
  159. fail2: free_irq(mk712_irq, mk712_dev);
  160. fail1: input_free_device(mk712_dev);
  161. release_region(mk712_io, 8);
  162. return err;
  163. }
  164. static void __exit mk712_exit(void)
  165. {
  166. input_unregister_device(mk712_dev);
  167. free_irq(mk712_irq, mk712_dev);
  168. release_region(mk712_io, 8);
  169. }
  170. module_init(mk712_init);
  171. module_exit(mk712_exit);