tsc2005.c 15 KB

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