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 TSC2005_DEBUG
29
#define TRACE(fmt, ...) fprintf(stderr, "%s@%d: " fmt "\n", \
30
__FUNCTION__, __LINE__, ##__VA_ARGS__)
35
#define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
38
qemu_irq pint; /* Combination of the nPENIRQ and DAV signals */
45
int state, reg, irq, command;
70
TSC_MODE_XYZ_SCAN = 0x0,
88
static const uint16_t mode_regs[16] = {
89
0xf000, /* X, Y, Z scan */
90
0xc000, /* X, Y scan */
97
0x0800, /* AUX scan */
100
0x0080, /* Short-circuit test */
101
0x0000, /* Reserved */
102
0x0000, /* X+, X- drivers */
103
0x0000, /* Y+, Y- drivers */
104
0x0000, /* Y+, X- drivers */
107
#define X_TRANSFORM(s) \
108
((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
109
#define Y_TRANSFORM(s) \
110
((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
112
#define Z1_TRANSFORM(s) \
113
(((s)->z1_cons - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
114
#define Z2_TRANSFORM(s) \
115
(((s)->z2_cons + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
117
/* these simpler forms work much better */
118
#define Z1_TRANSFORM(s) (((s)->z1_cons) << 4)
119
#define Z2_TRANSFORM(s) (((s)->z2_cons) << 4)
121
#define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
122
#define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
123
#define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
125
static uint16_t tsc2005_read(TSC2005State *s, int reg)
131
s->dav &= ~mode_regs[TSC_MODE_X];
132
TRACE("X = %d", TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
134
return TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
137
s->dav &= ~mode_regs[TSC_MODE_Y];
139
TRACE("Y = %d", TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
141
return TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
145
TRACE("Z1 = %d", TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
147
return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
151
TRACE("Z2 = %d", TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
153
return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
157
s->dav &= ~mode_regs[TSC_MODE_AUX];
158
TRACE("AUX = %d", TSC_CUT_RESOLUTION(AUX_VAL, s->precision));
159
return TSC_CUT_RESOLUTION(AUX_VAL, s->precision);
161
case 0x5: /* TEMP1 */
162
s->dav &= ~mode_regs[TSC_MODE_TEMP1];
163
TRACE("TEMP1 = %d", TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
165
return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
167
case 0x6: /* TEMP2 */
169
s->dav &= ~mode_regs[TSC_MODE_TEMP2];
170
TRACE("TEMP2 = %d", TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
172
return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
175
case 0x7: /* Status */
176
ret = s->dav | (s->reset << 7) | (s->pdst << 2) | 0x0;
177
s->dav &= ~(mode_regs[TSC_MODE_X_TEST] | mode_regs[TSC_MODE_Y_TEST] |
178
mode_regs[TSC_MODE_TS_TEST]);
180
TRACE("STATUS = 0x%04x", ret);
183
case 0x8: /* AUX high treshold */
184
TRACE("AUX high threshold = 0x%04x", s->aux_thr[1]);
185
return s->aux_thr[1];
186
case 0x9: /* AUX low treshold */
187
TRACE("AUX low threshold = 0x%04x", s->aux_thr[0]);
188
return s->aux_thr[0];
190
case 0xa: /* TEMP high treshold */
191
TRACE("TEMP high threshold = 0x%04x", s->temp_thr[1]);
192
return s->temp_thr[1];
193
case 0xb: /* TEMP low treshold */
194
TRACE("TEMP low threshold = 0x%04x", s->temp_thr[0]);
195
return s->temp_thr[0];
198
TRACE("CFR0 = 0x%04x", (s->pressure << 15) | ((!s->busy) << 14) |
199
(s->nextprecision << 13) | s->timing[0]);
200
return (s->pressure << 15) | ((!s->busy) << 14) |
201
(s->nextprecision << 13) | s->timing[0];
203
TRACE("CFR1 = 0x%04x", s->timing[1]);
206
TRACE("CFR2 = 0x%04x", (s->pin_func << 14) | s->filter);
207
return (s->pin_func << 14) | s->filter;
209
case 0xf: /* Function select status */
210
TRACE("function select status = 0x%04x",
211
s->function >= 0 ? 1 << s->function : 0);
212
return s->function >= 0 ? 1 << s->function : 0;
215
/* Never gets here */
216
TRACE("unknown register = 0xffff");
220
static void tsc2005_write(TSC2005State *s, int reg, uint16_t data)
223
case 0x8: /* AUX high treshold */
224
TRACE("AUX high threshold = 0x%04x", data);
225
s->aux_thr[1] = data;
227
case 0x9: /* AUX low treshold */
228
TRACE("AUX low threshold = 0x%04x", data);
229
s->aux_thr[0] = data;
232
case 0xa: /* TEMP high treshold */
233
TRACE("TEMP high threshold = 0x%04x", data);
234
s->temp_thr[1] = data;
236
case 0xb: /* TEMP low treshold */
237
TRACE("TEMP low threshold = 0x%04x", data);
238
s->temp_thr[0] = data;
242
TRACE("CFR0 = 0x%04x", data);
243
s->host_mode = data >> 15;
244
if (s->enabled != !(data & 0x4000)) {
245
s->enabled = !(data & 0x4000);
246
TRACE("touchscreen sense %sabled", s->enabled ? "en" : "dis");
247
if (s->busy && !s->enabled) {
248
qemu_del_timer(s->timer);
250
s->busy &= s->enabled;
252
s->nextprecision = (data >> 13) & 1;
253
s->timing[0] = data & 0x1fff;
254
if ((s->timing[0] >> 11) == 3) {
255
TRACE("illegal conversion clock setting");
259
TRACE("CFR1 = 0x%04x", data);
260
s->timing[1] = data & 0xf07;
263
TRACE("CFR2 = 0x%04x", data);
264
s->pin_func = (data >> 14) & 3;
265
s->filter = data & 0x3fff;
269
TRACE("write into read-only register 0x%x, value 0x%04x", reg, data);
273
/* This handles most of the chip's logic. */
274
static void tsc2005_pin_update(TSC2005State *s)
279
switch (s->pin_func) {
281
pin_state = !s->pressure && !!s->dav;
289
pin_state = !s->pressure;
292
if (pin_state != s->irq) {
294
qemu_set_irq(s->pint, s->irq);
297
switch (s->nextfunction) {
298
case TSC_MODE_XYZ_SCAN:
299
case TSC_MODE_XY_SCAN:
300
if (!s->host_mode && s->dav)
305
case TSC_MODE_AUX_SCAN:
317
case TSC_MODE_X_TEST:
318
case TSC_MODE_Y_TEST:
319
case TSC_MODE_TS_TEST:
324
case TSC_MODE_RESERVED:
325
case TSC_MODE_XX_DRV:
326
case TSC_MODE_YY_DRV:
327
case TSC_MODE_YX_DRV:
332
if (!s->enabled || s->busy)
336
s->precision = s->nextprecision;
337
s->function = s->nextfunction;
338
s->pdst = !s->pnd0; /* Synchronised on internal clock */
339
expires = qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() >> 7);
340
qemu_mod_timer(s->timer, expires);
343
static void tsc2005_reset(TSC2005State *s)
349
s->nextprecision = 0;
359
s->temp_thr[0] = 0x000;
360
s->temp_thr[1] = 0xfff;
361
s->aux_thr[0] = 0x000;
362
s->aux_thr[1] = 0xfff;
364
tsc2005_pin_update(s);
367
static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
369
TSC2005State *s = opaque;
371
TRACE("value = 0x%08x, state=%d", value, s->state + 1);
372
switch (s->state ++) {
376
if (value & (1 << 1))
379
s->nextfunction = (value >> 3) & 0xf;
380
s->nextprecision = (value >> 2) & 1;
381
if (s->enabled != !(value & 1)) {
382
s->enabled = !(value & 1);
383
TRACE("touchscreen sense %sabled",
384
s->enabled ? "en" : "dis");
385
if (s->busy && !s->enabled)
386
qemu_del_timer(s->timer);
387
s->busy &= s->enabled;
389
tsc2005_pin_update(s);
395
s->reg = (value >> 3) & 0xf;
396
s->pnd0 = (value >> 1) & 1;
397
s->command = value & 1;
401
s->data = tsc2005_read(s, s->reg);
402
tsc2005_pin_update(s);
411
ret = (s->data >> 8) & 0xff;
413
s->data |= value << 8;
418
ret = s->data & 0xff;
421
tsc2005_write(s, s->reg, s->data);
422
tsc2005_pin_update(s);
432
uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
435
TRACE("value=0x%08x, len=%d", value, len);
439
ret |= tsc2005_txrx_word(opaque, (value >> len) & 0xff) << len;
445
static void tsc2005_timer_tick(void *opaque)
447
TSC2005State *s = opaque;
449
/* Timer ticked -- a set of conversions has been finished. */
452
TRACE("not busy -> exit");
457
s->dav |= mode_regs[s->function];
459
tsc2005_pin_update(s);
462
static void tsc2005_touchscreen_event(void *opaque,
463
int x, int y, int z, int buttons_state)
465
TSC2005State *s = opaque;
472
s->pressure = !!buttons_state;
475
* Note: We would get better responsiveness in the guest by
476
* signaling TS events immediately, but for now we simulate
477
* the first conversion delay for sake of correctness.
479
if (p != s->pressure)
480
tsc2005_pin_update(s);
483
static void tsc2005_save(QEMUFile *f, void *opaque)
485
TSC2005State *s = (TSC2005State *) opaque;
488
qemu_put_be16(f, s->x);
489
qemu_put_be16(f, s->y);
490
qemu_put_byte(f, s->pressure);
492
qemu_put_byte(f, s->state);
493
qemu_put_byte(f, s->reg);
494
qemu_put_byte(f, s->command);
496
qemu_put_byte(f, s->irq);
497
qemu_put_be16s(f, &s->dav);
498
qemu_put_be16s(f, &s->data);
500
qemu_put_timer(f, s->timer);
501
qemu_put_byte(f, s->enabled);
502
qemu_put_byte(f, s->host_mode);
503
qemu_put_byte(f, s->function);
504
qemu_put_byte(f, s->nextfunction);
505
qemu_put_byte(f, s->precision);
506
qemu_put_byte(f, s->nextprecision);
507
qemu_put_be16(f, s->filter);
508
qemu_put_byte(f, s->pin_func);
509
qemu_put_be16(f, s->timing[0]);
510
qemu_put_be16(f, s->timing[1]);
511
qemu_put_be16s(f, &s->temp_thr[0]);
512
qemu_put_be16s(f, &s->temp_thr[1]);
513
qemu_put_be16s(f, &s->aux_thr[0]);
514
qemu_put_be16s(f, &s->aux_thr[1]);
515
qemu_put_be32(f, s->noise);
516
qemu_put_byte(f, s->reset);
517
qemu_put_byte(f, s->pdst);
518
qemu_put_byte(f, s->pnd0);
520
for (i = 0; i < 8; i ++)
521
qemu_put_be32(f, s->tr[i]);
524
static int tsc2005_load(QEMUFile *f, void *opaque, int version_id)
526
TSC2005State *s = (TSC2005State *) opaque;
529
s->x = qemu_get_be16(f);
530
s->y = qemu_get_be16(f);
531
s->pressure = qemu_get_byte(f);
533
s->state = qemu_get_byte(f);
534
s->reg = qemu_get_byte(f);
535
s->command = qemu_get_byte(f);
537
s->irq = qemu_get_byte(f);
538
qemu_get_be16s(f, &s->dav);
539
qemu_get_be16s(f, &s->data);
541
qemu_get_timer(f, s->timer);
542
s->enabled = qemu_get_byte(f);
543
s->host_mode = qemu_get_byte(f);
544
s->function = qemu_get_byte(f);
545
s->nextfunction = qemu_get_byte(f);
546
s->precision = qemu_get_byte(f);
547
s->nextprecision = qemu_get_byte(f);
548
s->filter = qemu_get_be16(f);
549
s->pin_func = qemu_get_byte(f);
550
s->timing[0] = qemu_get_be16(f);
551
s->timing[1] = qemu_get_be16(f);
552
qemu_get_be16s(f, &s->temp_thr[0]);
553
qemu_get_be16s(f, &s->temp_thr[1]);
554
qemu_get_be16s(f, &s->aux_thr[0]);
555
qemu_get_be16s(f, &s->aux_thr[1]);
556
s->noise = qemu_get_be32(f);
557
s->reset = qemu_get_byte(f);
558
s->pdst = qemu_get_byte(f);
559
s->pnd0 = qemu_get_byte(f);
561
for (i = 0; i < 8; i ++)
562
s->tr[i] = qemu_get_be32(f);
564
s->busy = qemu_timer_pending(s->timer);
565
tsc2005_pin_update(s);
570
void *tsc2005_init(qemu_irq pintdav)
575
g_malloc0(sizeof(TSC2005State));
579
s->precision = s->nextprecision = 0;
580
s->timer = qemu_new_timer_ns(vm_clock, tsc2005_timer_tick, s);
598
qemu_add_mouse_event_handler(tsc2005_touchscreen_event, s, 1,
599
"QEMU TSC2005-driven Touchscreen");
601
qemu_register_reset((void *) tsc2005_reset, s);
602
register_savevm(NULL, "tsc2005", -1, 0, tsc2005_save, tsc2005_load, s);
608
* Use tslib generated calibration data to generate ADC input values
609
* from the touchscreen. Assuming 12-bit precision was used during
612
void tsc2005_set_transform(void *opaque, MouseTransformInfo *info,
613
int z1_cons, int z2_cons)
615
TSC2005State *s = (TSC2005State *) opaque;
617
/* This version assumes touchscreen X & Y axis are parallel or
618
* perpendicular to LCD's X & Y axis in some way. */
619
if (abs(info->a[0]) > abs(info->a[1])) {
621
s->tr[1] = -info->a[6] * info->x;
622
s->tr[2] = info->a[0];
623
s->tr[3] = -info->a[2] / info->a[0];
624
s->tr[4] = info->a[6] * info->y;
626
s->tr[6] = info->a[4];
627
s->tr[7] = -info->a[5] / info->a[4];
629
s->tr[0] = info->a[6] * info->y;
631
s->tr[2] = info->a[1];
632
s->tr[3] = -info->a[2] / info->a[1];
634
s->tr[5] = -info->a[6] * info->x;
635
s->tr[6] = info->a[3];
636
s->tr[7] = -info->a[5] / info->a[3];
646
s->z1_cons = z1_cons;
647
s->z2_cons = z2_cons;