2
0

tsc2005.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * TI TSC2005 emulator.
  3. *
  4. * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) version 3 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/log.h"
  22. #include "qemu/timer.h"
  23. #include "sysemu/reset.h"
  24. #include "ui/console.h"
  25. #include "hw/input/tsc2xxx.h"
  26. #include "hw/irq.h"
  27. #include "migration/vmstate.h"
  28. #include "trace.h"
  29. #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
  30. typedef struct {
  31. qemu_irq pint; /* Combination of the nPENIRQ and DAV signals */
  32. QEMUTimer *timer;
  33. uint16_t model;
  34. int32_t x, y;
  35. bool pressure;
  36. uint8_t reg, state;
  37. bool irq, command;
  38. uint16_t data, dav;
  39. bool busy;
  40. bool enabled;
  41. bool host_mode;
  42. int8_t function;
  43. int8_t nextfunction;
  44. bool precision;
  45. bool nextprecision;
  46. uint16_t filter;
  47. uint8_t pin_func;
  48. uint16_t timing[2];
  49. uint8_t noise;
  50. bool reset;
  51. bool pdst;
  52. bool pnd0;
  53. uint16_t temp_thr[2];
  54. uint16_t aux_thr[2];
  55. int32_t tr[8];
  56. } TSC2005State;
  57. enum {
  58. TSC_MODE_XYZ_SCAN = 0x0,
  59. TSC_MODE_XY_SCAN,
  60. TSC_MODE_X,
  61. TSC_MODE_Y,
  62. TSC_MODE_Z,
  63. TSC_MODE_AUX,
  64. TSC_MODE_TEMP1,
  65. TSC_MODE_TEMP2,
  66. TSC_MODE_AUX_SCAN,
  67. TSC_MODE_X_TEST,
  68. TSC_MODE_Y_TEST,
  69. TSC_MODE_TS_TEST,
  70. TSC_MODE_RESERVED,
  71. TSC_MODE_XX_DRV,
  72. TSC_MODE_YY_DRV,
  73. TSC_MODE_YX_DRV,
  74. };
  75. static const uint16_t mode_regs[16] = {
  76. 0xf000, /* X, Y, Z scan */
  77. 0xc000, /* X, Y scan */
  78. 0x8000, /* X */
  79. 0x4000, /* Y */
  80. 0x3000, /* Z */
  81. 0x0800, /* AUX */
  82. 0x0400, /* TEMP1 */
  83. 0x0200, /* TEMP2 */
  84. 0x0800, /* AUX scan */
  85. 0x0040, /* X test */
  86. 0x0020, /* Y test */
  87. 0x0080, /* Short-circuit test */
  88. 0x0000, /* Reserved */
  89. 0x0000, /* X+, X- drivers */
  90. 0x0000, /* Y+, Y- drivers */
  91. 0x0000, /* Y+, X- drivers */
  92. };
  93. #define X_TRANSFORM(s) \
  94. ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
  95. #define Y_TRANSFORM(s) \
  96. ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
  97. #define Z1_TRANSFORM(s) \
  98. ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
  99. #define Z2_TRANSFORM(s) \
  100. ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
  101. #define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
  102. #define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
  103. #define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
  104. static uint16_t tsc2005_read(TSC2005State *s, int reg)
  105. {
  106. uint16_t ret;
  107. switch (reg) {
  108. case 0x0: /* X */
  109. s->dav &= ~mode_regs[TSC_MODE_X];
  110. return TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
  111. (s->noise & 3);
  112. case 0x1: /* Y */
  113. s->dav &= ~mode_regs[TSC_MODE_Y];
  114. s->noise ++;
  115. return TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
  116. (s->noise & 3);
  117. case 0x2: /* Z1 */
  118. s->dav &= 0xdfff;
  119. return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
  120. (s->noise & 3);
  121. case 0x3: /* Z2 */
  122. s->dav &= 0xefff;
  123. return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
  124. (s->noise & 3);
  125. case 0x4: /* AUX */
  126. s->dav &= ~mode_regs[TSC_MODE_AUX];
  127. return TSC_CUT_RESOLUTION(AUX_VAL, s->precision);
  128. case 0x5: /* TEMP1 */
  129. s->dav &= ~mode_regs[TSC_MODE_TEMP1];
  130. return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
  131. (s->noise & 5);
  132. case 0x6: /* TEMP2 */
  133. s->dav &= 0xdfff;
  134. s->dav &= ~mode_regs[TSC_MODE_TEMP2];
  135. return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
  136. (s->noise & 3);
  137. case 0x7: /* Status */
  138. ret = s->dav | (s->reset << 7) | (s->pdst << 2) | 0x0;
  139. s->dav &= ~(mode_regs[TSC_MODE_X_TEST] | mode_regs[TSC_MODE_Y_TEST] |
  140. mode_regs[TSC_MODE_TS_TEST]);
  141. s->reset = true;
  142. return ret;
  143. case 0x8: /* AUX high treshold */
  144. return s->aux_thr[1];
  145. case 0x9: /* AUX low treshold */
  146. return s->aux_thr[0];
  147. case 0xa: /* TEMP high treshold */
  148. return s->temp_thr[1];
  149. case 0xb: /* TEMP low treshold */
  150. return s->temp_thr[0];
  151. case 0xc: /* CFR0 */
  152. return (s->pressure << 15) | ((!s->busy) << 14) |
  153. (s->nextprecision << 13) | s->timing[0];
  154. case 0xd: /* CFR1 */
  155. return s->timing[1];
  156. case 0xe: /* CFR2 */
  157. return (s->pin_func << 14) | s->filter;
  158. case 0xf: /* Function select status */
  159. return s->function >= 0 ? 1 << s->function : 0;
  160. }
  161. /* Never gets here */
  162. return 0xffff;
  163. }
  164. static void tsc2005_write(TSC2005State *s, int reg, uint16_t data)
  165. {
  166. switch (reg) {
  167. case 0x8: /* AUX high treshold */
  168. s->aux_thr[1] = data;
  169. break;
  170. case 0x9: /* AUX low treshold */
  171. s->aux_thr[0] = data;
  172. break;
  173. case 0xa: /* TEMP high treshold */
  174. s->temp_thr[1] = data;
  175. break;
  176. case 0xb: /* TEMP low treshold */
  177. s->temp_thr[0] = data;
  178. break;
  179. case 0xc: /* CFR0 */
  180. s->host_mode = (data >> 15) != 0;
  181. if (s->enabled != !(data & 0x4000)) {
  182. s->enabled = !(data & 0x4000);
  183. trace_tsc2005_sense(s->enabled ? "enabled" : "disabled");
  184. if (s->busy && !s->enabled)
  185. timer_del(s->timer);
  186. s->busy = s->busy && s->enabled;
  187. }
  188. s->nextprecision = (data >> 13) & 1;
  189. s->timing[0] = data & 0x1fff;
  190. if ((s->timing[0] >> 11) == 3) {
  191. qemu_log_mask(LOG_GUEST_ERROR,
  192. "tsc2005_write: illegal conversion clock setting\n");
  193. }
  194. break;
  195. case 0xd: /* CFR1 */
  196. s->timing[1] = data & 0xf07;
  197. break;
  198. case 0xe: /* CFR2 */
  199. s->pin_func = (data >> 14) & 3;
  200. s->filter = data & 0x3fff;
  201. break;
  202. default:
  203. qemu_log_mask(LOG_GUEST_ERROR,
  204. "%s: write into read-only register 0x%x\n",
  205. __func__, reg);
  206. }
  207. }
  208. /* This handles most of the chip's logic. */
  209. static void tsc2005_pin_update(TSC2005State *s)
  210. {
  211. int64_t expires;
  212. bool pin_state;
  213. switch (s->pin_func) {
  214. case 0:
  215. pin_state = !s->pressure && !!s->dav;
  216. break;
  217. case 1:
  218. case 3:
  219. default:
  220. pin_state = !s->dav;
  221. break;
  222. case 2:
  223. pin_state = !s->pressure;
  224. }
  225. if (pin_state != s->irq) {
  226. s->irq = pin_state;
  227. qemu_set_irq(s->pint, s->irq);
  228. }
  229. switch (s->nextfunction) {
  230. case TSC_MODE_XYZ_SCAN:
  231. case TSC_MODE_XY_SCAN:
  232. if (!s->host_mode && s->dav)
  233. s->enabled = false;
  234. if (!s->pressure)
  235. return;
  236. /* Fall through */
  237. case TSC_MODE_AUX_SCAN:
  238. break;
  239. case TSC_MODE_X:
  240. case TSC_MODE_Y:
  241. case TSC_MODE_Z:
  242. if (!s->pressure)
  243. return;
  244. /* Fall through */
  245. case TSC_MODE_AUX:
  246. case TSC_MODE_TEMP1:
  247. case TSC_MODE_TEMP2:
  248. case TSC_MODE_X_TEST:
  249. case TSC_MODE_Y_TEST:
  250. case TSC_MODE_TS_TEST:
  251. if (s->dav)
  252. s->enabled = false;
  253. break;
  254. case TSC_MODE_RESERVED:
  255. case TSC_MODE_XX_DRV:
  256. case TSC_MODE_YY_DRV:
  257. case TSC_MODE_YX_DRV:
  258. default:
  259. return;
  260. }
  261. if (!s->enabled || s->busy)
  262. return;
  263. s->busy = true;
  264. s->precision = s->nextprecision;
  265. s->function = s->nextfunction;
  266. s->pdst = !s->pnd0; /* Synchronised on internal clock */
  267. expires = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  268. (NANOSECONDS_PER_SECOND >> 7);
  269. timer_mod(s->timer, expires);
  270. }
  271. static void tsc2005_reset(TSC2005State *s)
  272. {
  273. s->state = 0;
  274. s->pin_func = 0;
  275. s->enabled = false;
  276. s->busy = false;
  277. s->nextprecision = false;
  278. s->nextfunction = 0;
  279. s->timing[0] = 0;
  280. s->timing[1] = 0;
  281. s->irq = false;
  282. s->dav = 0;
  283. s->reset = false;
  284. s->pdst = true;
  285. s->pnd0 = false;
  286. s->function = -1;
  287. s->temp_thr[0] = 0x000;
  288. s->temp_thr[1] = 0xfff;
  289. s->aux_thr[0] = 0x000;
  290. s->aux_thr[1] = 0xfff;
  291. tsc2005_pin_update(s);
  292. }
  293. static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
  294. {
  295. TSC2005State *s = opaque;
  296. uint32_t ret = 0;
  297. switch (s->state ++) {
  298. case 0:
  299. if (value & 0x80) {
  300. /* Command */
  301. if (value & (1 << 1))
  302. tsc2005_reset(s);
  303. else {
  304. s->nextfunction = (value >> 3) & 0xf;
  305. s->nextprecision = (value >> 2) & 1;
  306. if (s->enabled != !(value & 1)) {
  307. s->enabled = !(value & 1);
  308. trace_tsc2005_sense(s->enabled ? "enabled" : "disabled");
  309. if (s->busy && !s->enabled)
  310. timer_del(s->timer);
  311. s->busy = s->busy && s->enabled;
  312. }
  313. tsc2005_pin_update(s);
  314. }
  315. s->state = 0;
  316. } else if (value) {
  317. /* Data transfer */
  318. s->reg = (value >> 3) & 0xf;
  319. s->pnd0 = (value >> 1) & 1;
  320. s->command = value & 1;
  321. if (s->command) {
  322. /* Read */
  323. s->data = tsc2005_read(s, s->reg);
  324. tsc2005_pin_update(s);
  325. } else
  326. s->data = 0;
  327. } else
  328. s->state = 0;
  329. break;
  330. case 1:
  331. if (s->command)
  332. ret = (s->data >> 8) & 0xff;
  333. else
  334. s->data |= value << 8;
  335. break;
  336. case 2:
  337. if (s->command)
  338. ret = s->data & 0xff;
  339. else {
  340. s->data |= value;
  341. tsc2005_write(s, s->reg, s->data);
  342. tsc2005_pin_update(s);
  343. }
  344. s->state = 0;
  345. break;
  346. }
  347. return ret;
  348. }
  349. uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
  350. {
  351. uint32_t ret = 0;
  352. len &= ~7;
  353. while (len > 0) {
  354. len -= 8;
  355. ret |= tsc2005_txrx_word(opaque, (value >> len) & 0xff) << len;
  356. }
  357. return ret;
  358. }
  359. static void tsc2005_timer_tick(void *opaque)
  360. {
  361. TSC2005State *s = opaque;
  362. /* Timer ticked -- a set of conversions has been finished. */
  363. if (!s->busy)
  364. return;
  365. s->busy = false;
  366. s->dav |= mode_regs[s->function];
  367. s->function = -1;
  368. tsc2005_pin_update(s);
  369. }
  370. static void tsc2005_touchscreen_event(void *opaque,
  371. int x, int y, int z, int buttons_state)
  372. {
  373. TSC2005State *s = opaque;
  374. int p = s->pressure;
  375. if (buttons_state) {
  376. s->x = x;
  377. s->y = y;
  378. }
  379. s->pressure = !!buttons_state;
  380. /*
  381. * Note: We would get better responsiveness in the guest by
  382. * signaling TS events immediately, but for now we simulate
  383. * the first conversion delay for sake of correctness.
  384. */
  385. if (p != s->pressure)
  386. tsc2005_pin_update(s);
  387. }
  388. static int tsc2005_post_load(void *opaque, int version_id)
  389. {
  390. TSC2005State *s = (TSC2005State *) opaque;
  391. s->busy = timer_pending(s->timer);
  392. tsc2005_pin_update(s);
  393. return 0;
  394. }
  395. static const VMStateDescription vmstate_tsc2005 = {
  396. .name = "tsc2005",
  397. .version_id = 2,
  398. .minimum_version_id = 2,
  399. .post_load = tsc2005_post_load,
  400. .fields = (VMStateField []) {
  401. VMSTATE_BOOL(pressure, TSC2005State),
  402. VMSTATE_BOOL(irq, TSC2005State),
  403. VMSTATE_BOOL(command, TSC2005State),
  404. VMSTATE_BOOL(enabled, TSC2005State),
  405. VMSTATE_BOOL(host_mode, TSC2005State),
  406. VMSTATE_BOOL(reset, TSC2005State),
  407. VMSTATE_BOOL(pdst, TSC2005State),
  408. VMSTATE_BOOL(pnd0, TSC2005State),
  409. VMSTATE_BOOL(precision, TSC2005State),
  410. VMSTATE_BOOL(nextprecision, TSC2005State),
  411. VMSTATE_UINT8(reg, TSC2005State),
  412. VMSTATE_UINT8(state, TSC2005State),
  413. VMSTATE_UINT16(data, TSC2005State),
  414. VMSTATE_UINT16(dav, TSC2005State),
  415. VMSTATE_UINT16(filter, TSC2005State),
  416. VMSTATE_INT8(nextfunction, TSC2005State),
  417. VMSTATE_INT8(function, TSC2005State),
  418. VMSTATE_INT32(x, TSC2005State),
  419. VMSTATE_INT32(y, TSC2005State),
  420. VMSTATE_TIMER_PTR(timer, TSC2005State),
  421. VMSTATE_UINT8(pin_func, TSC2005State),
  422. VMSTATE_UINT16_ARRAY(timing, TSC2005State, 2),
  423. VMSTATE_UINT8(noise, TSC2005State),
  424. VMSTATE_UINT16_ARRAY(temp_thr, TSC2005State, 2),
  425. VMSTATE_UINT16_ARRAY(aux_thr, TSC2005State, 2),
  426. VMSTATE_INT32_ARRAY(tr, TSC2005State, 8),
  427. VMSTATE_END_OF_LIST()
  428. }
  429. };
  430. void *tsc2005_init(qemu_irq pintdav)
  431. {
  432. TSC2005State *s;
  433. s = (TSC2005State *)
  434. g_malloc0(sizeof(TSC2005State));
  435. s->x = 400;
  436. s->y = 240;
  437. s->pressure = false;
  438. s->precision = s->nextprecision = false;
  439. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, tsc2005_timer_tick, s);
  440. s->pint = pintdav;
  441. s->model = 0x2005;
  442. s->tr[0] = 0;
  443. s->tr[1] = 1;
  444. s->tr[2] = 1;
  445. s->tr[3] = 0;
  446. s->tr[4] = 1;
  447. s->tr[5] = 0;
  448. s->tr[6] = 1;
  449. s->tr[7] = 0;
  450. tsc2005_reset(s);
  451. qemu_add_mouse_event_handler(tsc2005_touchscreen_event, s, 1,
  452. "QEMU TSC2005-driven Touchscreen");
  453. qemu_register_reset((void *) tsc2005_reset, s);
  454. vmstate_register(NULL, 0, &vmstate_tsc2005, s);
  455. return s;
  456. }
  457. /*
  458. * Use tslib generated calibration data to generate ADC input values
  459. * from the touchscreen. Assuming 12-bit precision was used during
  460. * tslib calibration.
  461. */
  462. void tsc2005_set_transform(void *opaque, MouseTransformInfo *info)
  463. {
  464. TSC2005State *s = (TSC2005State *) opaque;
  465. /* This version assumes touchscreen X & Y axis are parallel or
  466. * perpendicular to LCD's X & Y axis in some way. */
  467. if (abs(info->a[0]) > abs(info->a[1])) {
  468. s->tr[0] = 0;
  469. s->tr[1] = -info->a[6] * info->x;
  470. s->tr[2] = info->a[0];
  471. s->tr[3] = -info->a[2] / info->a[0];
  472. s->tr[4] = info->a[6] * info->y;
  473. s->tr[5] = 0;
  474. s->tr[6] = info->a[4];
  475. s->tr[7] = -info->a[5] / info->a[4];
  476. } else {
  477. s->tr[0] = info->a[6] * info->y;
  478. s->tr[1] = 0;
  479. s->tr[2] = info->a[1];
  480. s->tr[3] = -info->a[2] / info->a[1];
  481. s->tr[4] = 0;
  482. s->tr[5] = -info->a[6] * info->x;
  483. s->tr[6] = info->a[3];
  484. s->tr[7] = -info->a[5] / info->a[3];
  485. }
  486. s->tr[0] >>= 11;
  487. s->tr[1] >>= 11;
  488. s->tr[3] <<= 4;
  489. s->tr[4] >>= 11;
  490. s->tr[5] >>= 11;
  491. s->tr[7] <<= 4;
  492. }