4
* Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
5
* Copyright (C) 2008 Nokia Corporation
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.
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.
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/>.
22
#include "qemu/timer.h"
23
#include "ui/console.h"
24
#include "hw/devices.h"
26
#define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
29
qemu_irq pint; /* Combination of the nPENIRQ and DAV signals */
36
int state, reg, irq, command;
61
TSC_MODE_XYZ_SCAN = 0x0,
79
static const uint16_t mode_regs[16] = {
80
0xf000, /* X, Y, Z scan */
81
0xc000, /* X, Y scan */
88
0x0800, /* AUX scan */
91
0x0080, /* Short-circuit test */
92
0x0000, /* Reserved */
93
0x0000, /* X+, X- drivers */
94
0x0000, /* Y+, Y- drivers */
95
0x0000, /* Y+, X- drivers */
98
#define X_TRANSFORM(s) \
99
((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
100
#define Y_TRANSFORM(s) \
101
((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
103
#define Z1_TRANSFORM(s) \
104
(((s)->z1_cons - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
105
#define Z2_TRANSFORM(s) \
106
(((s)->z2_cons + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
108
/* these simpler forms work much better */
109
#define Z1_TRANSFORM(s) (((s)->z1_cons) << 4)
110
#define Z2_TRANSFORM(s) (((s)->z2_cons) << 4)
112
#define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
113
#define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
114
#define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
116
static uint16_t tsc2005_read(TSC2005State *s, int reg)
122
s->dav &= ~mode_regs[TSC_MODE_X];
123
return TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
126
s->dav &= ~mode_regs[TSC_MODE_Y];
128
return TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
132
return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
136
return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
140
s->dav &= ~mode_regs[TSC_MODE_AUX];
141
return TSC_CUT_RESOLUTION(AUX_VAL, s->precision);
143
case 0x5: /* TEMP1 */
144
s->dav &= ~mode_regs[TSC_MODE_TEMP1];
145
return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
147
case 0x6: /* TEMP2 */
149
s->dav &= ~mode_regs[TSC_MODE_TEMP2];
150
return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
153
case 0x7: /* Status */
154
ret = s->dav | (s->reset << 7) | (s->pdst << 2) | 0x0;
155
s->dav &= ~(mode_regs[TSC_MODE_X_TEST] | mode_regs[TSC_MODE_Y_TEST] |
156
mode_regs[TSC_MODE_TS_TEST]);
160
case 0x8: /* AUX high treshold */
161
return s->aux_thr[1];
162
case 0x9: /* AUX low treshold */
163
return s->aux_thr[0];
165
case 0xa: /* TEMP high treshold */
166
return s->temp_thr[1];
167
case 0xb: /* TEMP low treshold */
168
return s->temp_thr[0];
171
return (s->pressure << 15) | ((!s->busy) << 14) |
172
(s->nextprecision << 13) | s->timing[0];
176
return (s->pin_func << 14) | s->filter;
178
case 0xf: /* Function select status */
179
return s->function >= 0 ? 1 << s->function : 0;
182
/* Never gets here */
186
static void tsc2005_write(TSC2005State *s, int reg, uint16_t data)
189
case 0x8: /* AUX high treshold */
190
s->aux_thr[1] = data;
192
case 0x9: /* AUX low treshold */
193
s->aux_thr[0] = data;
196
case 0xa: /* TEMP high treshold */
197
s->temp_thr[1] = data;
199
case 0xb: /* TEMP low treshold */
200
s->temp_thr[0] = data;
204
s->host_mode = data >> 15;
205
if (s->enabled != !(data & 0x4000)) {
206
s->enabled = !(data & 0x4000);
207
fprintf(stderr, "%s: touchscreen sense %sabled\n",
208
__FUNCTION__, s->enabled ? "en" : "dis");
209
if (s->busy && !s->enabled)
211
s->busy &= s->enabled;
213
s->nextprecision = (data >> 13) & 1;
214
s->timing[0] = data & 0x1fff;
215
if ((s->timing[0] >> 11) == 3)
216
fprintf(stderr, "%s: illegal conversion clock setting\n",
220
s->timing[1] = data & 0xf07;
223
s->pin_func = (data >> 14) & 3;
224
s->filter = data & 0x3fff;
228
fprintf(stderr, "%s: write into read-only register %x\n",
233
/* This handles most of the chip's logic. */
234
static void tsc2005_pin_update(TSC2005State *s)
239
switch (s->pin_func) {
241
pin_state = !s->pressure && !!s->dav;
249
pin_state = !s->pressure;
252
if (pin_state != s->irq) {
254
qemu_set_irq(s->pint, s->irq);
257
switch (s->nextfunction) {
258
case TSC_MODE_XYZ_SCAN:
259
case TSC_MODE_XY_SCAN:
260
if (!s->host_mode && s->dav)
265
case TSC_MODE_AUX_SCAN:
277
case TSC_MODE_X_TEST:
278
case TSC_MODE_Y_TEST:
279
case TSC_MODE_TS_TEST:
284
case TSC_MODE_RESERVED:
285
case TSC_MODE_XX_DRV:
286
case TSC_MODE_YY_DRV:
287
case TSC_MODE_YX_DRV:
292
if (!s->enabled || s->busy)
296
s->precision = s->nextprecision;
297
s->function = s->nextfunction;
298
s->pdst = !s->pnd0; /* Synchronised on internal clock */
299
expires = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (get_ticks_per_sec() >> 7);
300
timer_mod(s->timer, expires);
303
static void tsc2005_reset(TSC2005State *s)
309
s->nextprecision = 0;
319
s->temp_thr[0] = 0x000;
320
s->temp_thr[1] = 0xfff;
321
s->aux_thr[0] = 0x000;
322
s->aux_thr[1] = 0xfff;
324
tsc2005_pin_update(s);
327
static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
329
TSC2005State *s = opaque;
332
switch (s->state ++) {
336
if (value & (1 << 1))
339
s->nextfunction = (value >> 3) & 0xf;
340
s->nextprecision = (value >> 2) & 1;
341
if (s->enabled != !(value & 1)) {
342
s->enabled = !(value & 1);
343
fprintf(stderr, "%s: touchscreen sense %sabled\n",
344
__FUNCTION__, s->enabled ? "en" : "dis");
345
if (s->busy && !s->enabled)
347
s->busy &= s->enabled;
349
tsc2005_pin_update(s);
355
s->reg = (value >> 3) & 0xf;
356
s->pnd0 = (value >> 1) & 1;
357
s->command = value & 1;
361
s->data = tsc2005_read(s, s->reg);
362
tsc2005_pin_update(s);
371
ret = (s->data >> 8) & 0xff;
373
s->data |= value << 8;
378
ret = s->data & 0xff;
381
tsc2005_write(s, s->reg, s->data);
382
tsc2005_pin_update(s);
392
uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
399
ret |= tsc2005_txrx_word(opaque, (value >> len) & 0xff) << len;
405
static void tsc2005_timer_tick(void *opaque)
407
TSC2005State *s = opaque;
409
/* Timer ticked -- a set of conversions has been finished. */
415
s->dav |= mode_regs[s->function];
417
tsc2005_pin_update(s);
420
static void tsc2005_touchscreen_event(void *opaque,
421
int x, int y, int z, int buttons_state)
423
TSC2005State *s = opaque;
430
s->pressure = !!buttons_state;
433
* Note: We would get better responsiveness in the guest by
434
* signaling TS events immediately, but for now we simulate
435
* the first conversion delay for sake of correctness.
437
if (p != s->pressure)
438
tsc2005_pin_update(s);
441
static void tsc2005_save(QEMUFile *f, void *opaque)
443
TSC2005State *s = (TSC2005State *) opaque;
446
qemu_put_be16(f, s->x);
447
qemu_put_be16(f, s->y);
448
qemu_put_byte(f, s->pressure);
450
qemu_put_byte(f, s->state);
451
qemu_put_byte(f, s->reg);
452
qemu_put_byte(f, s->command);
454
qemu_put_byte(f, s->irq);
455
qemu_put_be16s(f, &s->dav);
456
qemu_put_be16s(f, &s->data);
458
timer_put(f, s->timer);
459
qemu_put_byte(f, s->enabled);
460
qemu_put_byte(f, s->host_mode);
461
qemu_put_byte(f, s->function);
462
qemu_put_byte(f, s->nextfunction);
463
qemu_put_byte(f, s->precision);
464
qemu_put_byte(f, s->nextprecision);
465
qemu_put_be16(f, s->filter);
466
qemu_put_byte(f, s->pin_func);
467
qemu_put_be16(f, s->timing[0]);
468
qemu_put_be16(f, s->timing[1]);
469
qemu_put_be16s(f, &s->temp_thr[0]);
470
qemu_put_be16s(f, &s->temp_thr[1]);
471
qemu_put_be16s(f, &s->aux_thr[0]);
472
qemu_put_be16s(f, &s->aux_thr[1]);
473
qemu_put_be32(f, s->noise);
474
qemu_put_byte(f, s->reset);
475
qemu_put_byte(f, s->pdst);
476
qemu_put_byte(f, s->pnd0);
478
for (i = 0; i < 8; i ++)
479
qemu_put_be32(f, s->tr[i]);
482
static int tsc2005_load(QEMUFile *f, void *opaque, int version_id)
484
TSC2005State *s = (TSC2005State *) opaque;
487
s->x = qemu_get_be16(f);
488
s->y = qemu_get_be16(f);
489
s->pressure = qemu_get_byte(f);
491
s->state = qemu_get_byte(f);
492
s->reg = qemu_get_byte(f);
493
s->command = qemu_get_byte(f);
495
s->irq = qemu_get_byte(f);
496
qemu_get_be16s(f, &s->dav);
497
qemu_get_be16s(f, &s->data);
499
timer_get(f, s->timer);
500
s->enabled = qemu_get_byte(f);
501
s->host_mode = qemu_get_byte(f);
502
s->function = qemu_get_byte(f);
503
s->nextfunction = qemu_get_byte(f);
504
s->precision = qemu_get_byte(f);
505
s->nextprecision = qemu_get_byte(f);
506
s->filter = qemu_get_be16(f);
507
s->pin_func = qemu_get_byte(f);
508
s->timing[0] = qemu_get_be16(f);
509
s->timing[1] = qemu_get_be16(f);
510
qemu_get_be16s(f, &s->temp_thr[0]);
511
qemu_get_be16s(f, &s->temp_thr[1]);
512
qemu_get_be16s(f, &s->aux_thr[0]);
513
qemu_get_be16s(f, &s->aux_thr[1]);
514
s->noise = qemu_get_be32(f);
515
s->reset = qemu_get_byte(f);
516
s->pdst = qemu_get_byte(f);
517
s->pnd0 = qemu_get_byte(f);
519
for (i = 0; i < 8; i ++)
520
s->tr[i] = qemu_get_be32(f);
522
s->busy = timer_pending(s->timer);
523
tsc2005_pin_update(s);
528
void *tsc2005_init(qemu_irq pintdav)
533
g_malloc0(sizeof(TSC2005State));
537
s->precision = s->nextprecision = 0;
538
s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, tsc2005_timer_tick, s);
556
qemu_add_mouse_event_handler(tsc2005_touchscreen_event, s, 1,
557
"QEMU TSC2005-driven Touchscreen");
559
qemu_register_reset((void *) tsc2005_reset, s);
560
register_savevm(NULL, "tsc2005", -1, 0, tsc2005_save, tsc2005_load, s);
566
* Use tslib generated calibration data to generate ADC input values
567
* from the touchscreen. Assuming 12-bit precision was used during
570
void tsc2005_set_transform(void *opaque, MouseTransformInfo *info,
571
int z1_cons, int z2_cons)
573
TSC2005State *s = (TSC2005State *) opaque;
575
/* This version assumes touchscreen X & Y axis are parallel or
576
* perpendicular to LCD's X & Y axis in some way. */
577
if (abs(info->a[0]) > abs(info->a[1])) {
579
s->tr[1] = -info->a[6] * info->x;
580
s->tr[2] = info->a[0];
581
s->tr[3] = -info->a[2] / info->a[0];
582
s->tr[4] = info->a[6] * info->y;
584
s->tr[6] = info->a[4];
585
s->tr[7] = -info->a[5] / info->a[4];
587
s->tr[0] = info->a[6] * info->y;
589
s->tr[2] = info->a[1];
590
s->tr[3] = -info->a[2] / info->a[1];
592
s->tr[5] = -info->a[6] * info->x;
593
s->tr[6] = info->a[3];
594
s->tr[7] = -info->a[5] / info->a[3];
604
s->z1_cons = z1_cons;
605
s->z2_cons = z2_cons;