ads7846.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* Control-byte bitfields */
  28. #define CB_PD0 (1 << 0)
  29. #define CB_PD1 (1 << 1)
  30. #define CB_SER (1 << 2)
  31. #define CB_MODE (1 << 3)
  32. #define CB_A0 (1 << 4)
  33. #define CB_A1 (1 << 5)
  34. #define CB_A2 (1 << 6)
  35. #define CB_START (1 << 7)
  36. #define X_AXIS_DMAX 3470
  37. #define X_AXIS_MIN 290
  38. #define Y_AXIS_DMAX 3450
  39. #define Y_AXIS_MIN 200
  40. #define ADS_VBAT 2000
  41. #define ADS_VAUX 2000
  42. #define ADS_TEMP0 2000
  43. #define ADS_TEMP1 3000
  44. #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
  45. #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
  46. #define ADS_Z1POS(x, y) 600
  47. #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
  48. static void ads7846_int_update(ADS7846State *s)
  49. {
  50. if (s->interrupt)
  51. qemu_set_irq(s->interrupt, s->pressure == 0);
  52. }
  53. static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
  54. {
  55. ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev);
  56. switch (s->cycle ++) {
  57. case 0:
  58. if (!(value & CB_START)) {
  59. s->cycle = 0;
  60. break;
  61. }
  62. s->output = s->input[(value >> 4) & 7];
  63. /* Imitate the ADC noise, some drivers expect this. */
  64. s->noise = (s->noise + 3) & 7;
  65. switch ((value >> 4) & 7) {
  66. case 1: s->output += s->noise ^ 2; break;
  67. case 3: s->output += s->noise ^ 0; break;
  68. case 4: s->output += s->noise ^ 7; break;
  69. case 5: s->output += s->noise ^ 5; break;
  70. }
  71. if (value & CB_MODE)
  72. s->output >>= 4; /* 8 bits instead of 12 */
  73. break;
  74. case 1:
  75. s->cycle = 0;
  76. break;
  77. }
  78. return s->output;
  79. }
  80. static void ads7846_ts_event(void *opaque,
  81. int x, int y, int z, int buttons_state)
  82. {
  83. ADS7846State *s = opaque;
  84. if (buttons_state) {
  85. x = 0x7fff - x;
  86. s->input[1] = ADS_XPOS(x, y);
  87. s->input[3] = ADS_Z1POS(x, y);
  88. s->input[4] = ADS_Z2POS(x, y);
  89. s->input[5] = ADS_YPOS(x, y);
  90. }
  91. if (s->pressure == !buttons_state) {
  92. s->pressure = !!buttons_state;
  93. ads7846_int_update(s);
  94. }
  95. }
  96. static int ads7856_post_load(void *opaque, int version_id)
  97. {
  98. ADS7846State *s = opaque;
  99. s->pressure = 0;
  100. ads7846_int_update(s);
  101. return 0;
  102. }
  103. static const VMStateDescription vmstate_ads7846 = {
  104. .name = "ads7846",
  105. .version_id = 1,
  106. .minimum_version_id = 1,
  107. .post_load = ads7856_post_load,
  108. .fields = (VMStateField[]) {
  109. VMSTATE_SSI_SLAVE(ssidev, ADS7846State),
  110. VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
  111. VMSTATE_INT32(noise, ADS7846State),
  112. VMSTATE_INT32(cycle, ADS7846State),
  113. VMSTATE_INT32(output, ADS7846State),
  114. VMSTATE_END_OF_LIST()
  115. }
  116. };
  117. static void ads7846_realize(SSISlave *d, Error **errp)
  118. {
  119. DeviceState *dev = DEVICE(d);
  120. ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, d);
  121. qdev_init_gpio_out(dev, &s->interrupt, 1);
  122. s->input[0] = ADS_TEMP0; /* TEMP0 */
  123. s->input[2] = ADS_VBAT; /* VBAT */
  124. s->input[6] = ADS_VAUX; /* VAUX */
  125. s->input[7] = ADS_TEMP1; /* TEMP1 */
  126. /* We want absolute coordinates */
  127. qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
  128. "QEMU ADS7846-driven Touchscreen");
  129. ads7846_int_update(s);
  130. vmstate_register(NULL, -1, &vmstate_ads7846, s);
  131. }
  132. static void ads7846_class_init(ObjectClass *klass, void *data)
  133. {
  134. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  135. k->realize = ads7846_realize;
  136. k->transfer = ads7846_transfer;
  137. }
  138. static const TypeInfo ads7846_info = {
  139. .name = "ads7846",
  140. .parent = TYPE_SSI_SLAVE,
  141. .instance_size = sizeof(ADS7846State),
  142. .class_init = ads7846_class_init,
  143. };
  144. static void ads7846_register_types(void)
  145. {
  146. type_register_static(&ads7846_info);
  147. }
  148. type_init(ads7846_register_types)