ads7846.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "hw.h"
  10. #include "devices.h"
  11. #include "console.h"
  12. struct ads7846_state_s {
  13. qemu_irq interrupt;
  14. int input[8];
  15. int pressure;
  16. int noise;
  17. int cycle;
  18. int output;
  19. };
  20. /* Control-byte bitfields */
  21. #define CB_PD0 (1 << 0)
  22. #define CB_PD1 (1 << 1)
  23. #define CB_SER (1 << 2)
  24. #define CB_MODE (1 << 3)
  25. #define CB_A0 (1 << 4)
  26. #define CB_A1 (1 << 5)
  27. #define CB_A2 (1 << 6)
  28. #define CB_START (1 << 7)
  29. #define X_AXIS_DMAX 3470
  30. #define X_AXIS_MIN 290
  31. #define Y_AXIS_DMAX 3450
  32. #define Y_AXIS_MIN 200
  33. #define ADS_VBAT 2000
  34. #define ADS_VAUX 2000
  35. #define ADS_TEMP0 2000
  36. #define ADS_TEMP1 3000
  37. #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
  38. #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
  39. #define ADS_Z1POS(x, y) 600
  40. #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
  41. static void ads7846_int_update(struct ads7846_state_s *s)
  42. {
  43. if (s->interrupt)
  44. qemu_set_irq(s->interrupt, s->pressure == 0);
  45. }
  46. uint32_t ads7846_read(void *opaque)
  47. {
  48. struct ads7846_state_s *s = (struct ads7846_state_s *) opaque;
  49. return s->output;
  50. }
  51. void ads7846_write(void *opaque, uint32_t value)
  52. {
  53. struct ads7846_state_s *s = (struct ads7846_state_s *) opaque;
  54. switch (s->cycle ++) {
  55. case 0:
  56. if (!(value & CB_START)) {
  57. s->cycle = 0;
  58. break;
  59. }
  60. s->output = s->input[(value >> 4) & 7];
  61. /* Imitate the ADC noise, some drivers expect this. */
  62. s->noise = (s->noise + 3) & 7;
  63. switch ((value >> 4) & 7) {
  64. case 1: s->output += s->noise ^ 2; break;
  65. case 3: s->output += s->noise ^ 0; break;
  66. case 4: s->output += s->noise ^ 7; break;
  67. case 5: s->output += s->noise ^ 5; break;
  68. }
  69. if (value & CB_MODE)
  70. s->output >>= 4; /* 8 bits instead of 12 */
  71. break;
  72. case 1:
  73. s->cycle = 0;
  74. break;
  75. }
  76. }
  77. static void ads7846_ts_event(void *opaque,
  78. int x, int y, int z, int buttons_state)
  79. {
  80. struct ads7846_state_s *s = opaque;
  81. if (buttons_state) {
  82. x = 0x7fff - x;
  83. s->input[1] = ADS_XPOS(x, y);
  84. s->input[3] = ADS_Z1POS(x, y);
  85. s->input[4] = ADS_Z2POS(x, y);
  86. s->input[5] = ADS_YPOS(x, y);
  87. }
  88. if (s->pressure == !buttons_state) {
  89. s->pressure = !!buttons_state;
  90. ads7846_int_update(s);
  91. }
  92. }
  93. static void ads7846_save(QEMUFile *f, void *opaque)
  94. {
  95. struct ads7846_state_s *s = (struct ads7846_state_s *) opaque;
  96. int i;
  97. for (i = 0; i < 8; i ++)
  98. qemu_put_be32(f, s->input[i]);
  99. qemu_put_be32(f, s->noise);
  100. qemu_put_be32(f, s->cycle);
  101. qemu_put_be32(f, s->output);
  102. }
  103. static int ads7846_load(QEMUFile *f, void *opaque, int version_id)
  104. {
  105. struct ads7846_state_s *s = (struct ads7846_state_s *) opaque;
  106. int i;
  107. for (i = 0; i < 8; i ++)
  108. s->input[i] = qemu_get_be32(f);
  109. s->noise = qemu_get_be32(f);
  110. s->cycle = qemu_get_be32(f);
  111. s->output = qemu_get_be32(f);
  112. s->pressure = 0;
  113. ads7846_int_update(s);
  114. return 0;
  115. }
  116. struct ads7846_state_s *ads7846_init(qemu_irq penirq)
  117. {
  118. struct ads7846_state_s *s;
  119. s = (struct ads7846_state_s *)
  120. qemu_mallocz(sizeof(struct ads7846_state_s));
  121. memset(s, 0, sizeof(struct ads7846_state_s));
  122. s->interrupt = penirq;
  123. s->input[0] = ADS_TEMP0; /* TEMP0 */
  124. s->input[2] = ADS_VBAT; /* VBAT */
  125. s->input[6] = ADS_VAUX; /* VAUX */
  126. s->input[7] = ADS_TEMP1; /* TEMP1 */
  127. /* We want absolute coordinates */
  128. qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
  129. "QEMU ADS7846-driven Touchscreen");
  130. ads7846_int_update(s);
  131. register_savevm("ads7846", -1, 0, ads7846_save, ads7846_load, s);
  132. return s;
  133. }