tsc2005.c 15 KB

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