ads7846.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * TI ADS7846 / TSC2046 chip emulation.
  3. *
  4. * Copyright (c) 2006 Openedhand Ltd.
  5. * Written by Andrzej Zaborowski <balrog@zabor.org>
  6. *
  7. * This code is licensed under the GNU GPL v2.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "hw/irq.h"
  14. #include "hw/ssi/ssi.h"
  15. #include "migration/vmstate.h"
  16. #include "qemu/module.h"
  17. #include "ui/console.h"
  18. typedef struct {
  19. SSISlave ssidev;
  20. qemu_irq interrupt;
  21. int input[8];
  22. int pressure;
  23. int noise;
  24. int cycle;
  25. int output;
  26. } ADS7846State;
  27. #define TYPE_ADS7846 "ads7846"
  28. #define ADS7846(obj) OBJECT_CHECK(ADS7846State, (obj), TYPE_ADS7846)
  29. /* Control-byte bitfields */
  30. #define CB_PD0 (1 << 0)
  31. #define CB_PD1 (1 << 1)
  32. #define CB_SER (1 << 2)
  33. #define CB_MODE (1 << 3)
  34. #define CB_A0 (1 << 4)
  35. #define CB_A1 (1 << 5)
  36. #define CB_A2 (1 << 6)
  37. #define CB_START (1 << 7)
  38. #define X_AXIS_DMAX 3470
  39. #define X_AXIS_MIN 290
  40. #define Y_AXIS_DMAX 3450
  41. #define Y_AXIS_MIN 200
  42. #define ADS_VBAT 2000
  43. #define ADS_VAUX 2000
  44. #define ADS_TEMP0 2000
  45. #define ADS_TEMP1 3000
  46. #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
  47. #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
  48. #define ADS_Z1POS(x, y) 600
  49. #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
  50. static void ads7846_int_update(ADS7846State *s)
  51. {
  52. if (s->interrupt)
  53. qemu_set_irq(s->interrupt, s->pressure == 0);
  54. }
  55. static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
  56. {
  57. ADS7846State *s = ADS7846(dev);
  58. switch (s->cycle ++) {
  59. case 0:
  60. if (!(value & CB_START)) {
  61. s->cycle = 0;
  62. break;
  63. }
  64. s->output = s->input[(value >> 4) & 7];
  65. /* Imitate the ADC noise, some drivers expect this. */
  66. s->noise = (s->noise + 3) & 7;
  67. switch ((value >> 4) & 7) {
  68. case 1: s->output += s->noise ^ 2; break;
  69. case 3: s->output += s->noise ^ 0; break;
  70. case 4: s->output += s->noise ^ 7; break;
  71. case 5: s->output += s->noise ^ 5; break;
  72. }
  73. if (value & CB_MODE)
  74. s->output >>= 4; /* 8 bits instead of 12 */
  75. break;
  76. case 1:
  77. s->cycle = 0;
  78. break;
  79. }
  80. return s->output;
  81. }
  82. static void ads7846_ts_event(void *opaque,
  83. int x, int y, int z, int buttons_state)
  84. {
  85. ADS7846State *s = opaque;
  86. if (buttons_state) {
  87. x = 0x7fff - x;
  88. s->input[1] = ADS_XPOS(x, y);
  89. s->input[3] = ADS_Z1POS(x, y);
  90. s->input[4] = ADS_Z2POS(x, y);
  91. s->input[5] = ADS_YPOS(x, y);
  92. }
  93. if (s->pressure == !buttons_state) {
  94. s->pressure = !!buttons_state;
  95. ads7846_int_update(s);
  96. }
  97. }
  98. static int ads7856_post_load(void *opaque, int version_id)
  99. {
  100. ADS7846State *s = opaque;
  101. s->pressure = 0;
  102. ads7846_int_update(s);
  103. return 0;
  104. }
  105. static const VMStateDescription vmstate_ads7846 = {
  106. .name = "ads7846",
  107. .version_id = 1,
  108. .minimum_version_id = 1,
  109. .post_load = ads7856_post_load,
  110. .fields = (VMStateField[]) {
  111. VMSTATE_SSI_SLAVE(ssidev, ADS7846State),
  112. VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
  113. VMSTATE_INT32(noise, ADS7846State),
  114. VMSTATE_INT32(cycle, ADS7846State),
  115. VMSTATE_INT32(output, ADS7846State),
  116. VMSTATE_END_OF_LIST()
  117. }
  118. };
  119. static void ads7846_realize(SSISlave *d, Error **errp)
  120. {
  121. DeviceState *dev = DEVICE(d);
  122. ADS7846State *s = ADS7846(d);
  123. qdev_init_gpio_out(dev, &s->interrupt, 1);
  124. s->input[0] = ADS_TEMP0; /* TEMP0 */
  125. s->input[2] = ADS_VBAT; /* VBAT */
  126. s->input[6] = ADS_VAUX; /* VAUX */
  127. s->input[7] = ADS_TEMP1; /* TEMP1 */
  128. /* We want absolute coordinates */
  129. qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
  130. "QEMU ADS7846-driven Touchscreen");
  131. ads7846_int_update(s);
  132. vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_ads7846, s);
  133. }
  134. static void ads7846_class_init(ObjectClass *klass, void *data)
  135. {
  136. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  137. k->realize = ads7846_realize;
  138. k->transfer = ads7846_transfer;
  139. }
  140. static const TypeInfo ads7846_info = {
  141. .name = TYPE_ADS7846,
  142. .parent = TYPE_SSI_SLAVE,
  143. .instance_size = sizeof(ADS7846State),
  144. .class_init = ads7846_class_init,
  145. };
  146. static void ads7846_register_types(void)
  147. {
  148. type_register_static(&ads7846_info);
  149. }
  150. type_init(ads7846_register_types)