tsc40.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * TSC-40 serial touchscreen driver. It should be compatible with
  3. * TSC-10 and 25.
  4. *
  5. * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  6. * License: GPLv2 as published by the FSF.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/input.h>
  12. #include <linux/serio.h>
  13. #define PACKET_LENGTH 5
  14. struct tsc_ser {
  15. struct input_dev *dev;
  16. struct serio *serio;
  17. u32 idx;
  18. unsigned char data[PACKET_LENGTH];
  19. char phys[32];
  20. };
  21. static void tsc_process_data(struct tsc_ser *ptsc)
  22. {
  23. struct input_dev *dev = ptsc->dev;
  24. u8 *data = ptsc->data;
  25. u32 x;
  26. u32 y;
  27. x = ((data[1] & 0x03) << 8) | data[2];
  28. y = ((data[3] & 0x03) << 8) | data[4];
  29. input_report_abs(dev, ABS_X, x);
  30. input_report_abs(dev, ABS_Y, y);
  31. input_report_key(dev, BTN_TOUCH, 1);
  32. input_sync(dev);
  33. }
  34. static irqreturn_t tsc_interrupt(struct serio *serio,
  35. unsigned char data, unsigned int flags)
  36. {
  37. struct tsc_ser *ptsc = serio_get_drvdata(serio);
  38. struct input_dev *dev = ptsc->dev;
  39. ptsc->data[ptsc->idx] = data;
  40. switch (ptsc->idx++) {
  41. case 0:
  42. if (unlikely((data & 0x3e) != 0x10)) {
  43. dev_dbg(&serio->dev,
  44. "unsynchronized packet start (0x%02x)\n", data);
  45. ptsc->idx = 0;
  46. } else if (!(data & 0x01)) {
  47. input_report_key(dev, BTN_TOUCH, 0);
  48. input_sync(dev);
  49. ptsc->idx = 0;
  50. }
  51. break;
  52. case 1:
  53. case 3:
  54. if (unlikely(data & 0xfc)) {
  55. dev_dbg(&serio->dev,
  56. "unsynchronized data 0x%02x at offset %d\n",
  57. data, ptsc->idx - 1);
  58. ptsc->idx = 0;
  59. }
  60. break;
  61. case 4:
  62. tsc_process_data(ptsc);
  63. ptsc->idx = 0;
  64. break;
  65. }
  66. return IRQ_HANDLED;
  67. }
  68. static int tsc_connect(struct serio *serio, struct serio_driver *drv)
  69. {
  70. struct tsc_ser *ptsc;
  71. struct input_dev *input_dev;
  72. int error;
  73. ptsc = kzalloc(sizeof(struct tsc_ser), GFP_KERNEL);
  74. input_dev = input_allocate_device();
  75. if (!ptsc || !input_dev) {
  76. error = -ENOMEM;
  77. goto fail1;
  78. }
  79. ptsc->serio = serio;
  80. ptsc->dev = input_dev;
  81. snprintf(ptsc->phys, sizeof(ptsc->phys), "%s/input0", serio->phys);
  82. input_dev->name = "TSC-10/25/40 Serial TouchScreen";
  83. input_dev->phys = ptsc->phys;
  84. input_dev->id.bustype = BUS_RS232;
  85. input_dev->id.vendor = SERIO_TSC40;
  86. input_dev->id.product = 40;
  87. input_dev->id.version = 0x0001;
  88. input_dev->dev.parent = &serio->dev;
  89. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  90. __set_bit(BTN_TOUCH, input_dev->keybit);
  91. input_set_abs_params(ptsc->dev, ABS_X, 0, 0x3ff, 0, 0);
  92. input_set_abs_params(ptsc->dev, ABS_Y, 0, 0x3ff, 0, 0);
  93. serio_set_drvdata(serio, ptsc);
  94. error = serio_open(serio, drv);
  95. if (error)
  96. goto fail2;
  97. error = input_register_device(ptsc->dev);
  98. if (error)
  99. goto fail3;
  100. return 0;
  101. fail3:
  102. serio_close(serio);
  103. fail2:
  104. serio_set_drvdata(serio, NULL);
  105. fail1:
  106. input_free_device(input_dev);
  107. kfree(ptsc);
  108. return error;
  109. }
  110. static void tsc_disconnect(struct serio *serio)
  111. {
  112. struct tsc_ser *ptsc = serio_get_drvdata(serio);
  113. serio_close(serio);
  114. input_unregister_device(ptsc->dev);
  115. kfree(ptsc);
  116. serio_set_drvdata(serio, NULL);
  117. }
  118. static struct serio_device_id tsc_serio_ids[] = {
  119. {
  120. .type = SERIO_RS232,
  121. .proto = SERIO_TSC40,
  122. .id = SERIO_ANY,
  123. .extra = SERIO_ANY,
  124. },
  125. { 0 }
  126. };
  127. MODULE_DEVICE_TABLE(serio, tsc_serio_ids);
  128. #define DRIVER_DESC "TSC-10/25/40 serial touchscreen driver"
  129. static struct serio_driver tsc_drv = {
  130. .driver = {
  131. .name = "tsc40",
  132. },
  133. .description = DRIVER_DESC,
  134. .id_table = tsc_serio_ids,
  135. .interrupt = tsc_interrupt,
  136. .connect = tsc_connect,
  137. .disconnect = tsc_disconnect,
  138. };
  139. module_serio_driver(tsc_drv);
  140. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
  141. MODULE_DESCRIPTION(DRIVER_DESC);
  142. MODULE_LICENSE("GPL v2");