4
* Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
5
* Copyright (c) 2009-2010 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)
277
TRACE("nextf=%d, press=%d, ena=%d, busy=%d, dav=0x%04x, irq=%d",
278
s->nextfunction, s->pressure, s->enabled, s->busy, s->dav, s->irq);
279
switch (s->nextfunction) {
280
case TSC_MODE_XYZ_SCAN:
281
case TSC_MODE_XY_SCAN:
282
if (!s->pin_func && !s->dav) {
283
TRACE("all values read, lowering irq");
285
qemu_set_irq(s->pint, s->irq);
287
if (!s->host_mode && s->dav)
292
case TSC_MODE_AUX_SCAN:
304
case TSC_MODE_X_TEST:
305
case TSC_MODE_Y_TEST:
306
case TSC_MODE_TS_TEST:
311
case TSC_MODE_RESERVED:
312
case TSC_MODE_XX_DRV:
313
case TSC_MODE_YY_DRV:
314
case TSC_MODE_YX_DRV:
319
if (s->enabled && !s->busy) {
321
s->precision = s->nextprecision;
322
s->function = s->nextfunction;
323
s->pdst = !s->pnd0; /* Synchronised on internal clock */
324
expires = qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() >> 7);
325
qemu_mod_timer(s->timer, expires);
329
static void tsc2005_reset(TSC2005State *s)
335
s->nextprecision = 0;
345
s->temp_thr[0] = 0x000;
346
s->temp_thr[1] = 0xfff;
347
s->aux_thr[0] = 0x000;
348
s->aux_thr[1] = 0xfff;
350
tsc2005_pin_update(s);
353
static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
355
TSC2005State *s = opaque;
357
TRACE("value = 0x%08x, state=%d", value, s->state + 1);
358
switch (s->state ++) {
362
if (value & (1 << 1))
365
s->nextfunction = (value >> 3) & 0xf;
366
s->nextprecision = (value >> 2) & 1;
367
if (s->enabled != !(value & 1)) {
368
s->enabled = !(value & 1);
369
TRACE("touchscreen sense %sabled",
370
s->enabled ? "en" : "dis");
371
if (s->busy && !s->enabled)
372
qemu_del_timer(s->timer);
373
s->busy &= s->enabled;
375
tsc2005_pin_update(s);
381
s->reg = (value >> 3) & 0xf;
382
s->pnd0 = (value >> 1) & 1;
383
s->command = value & 1;
387
s->data = tsc2005_read(s, s->reg);
388
tsc2005_pin_update(s);
397
ret = (s->data >> 8) & 0xff;
399
s->data |= value << 8;
404
ret = s->data & 0xff;
407
tsc2005_write(s, s->reg, s->data);
408
tsc2005_pin_update(s);
418
uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
421
TRACE("value=0x%08x, len=%d", value, len);
425
ret |= tsc2005_txrx_word(opaque, (value >> len) & 0xff) << len;
431
static void tsc2005_timer_tick(void *opaque)
434
TSC2005State *s = opaque;
436
/* Timer ticked -- a set of conversions has been finished. */
439
TRACE("not busy -> exit");
443
switch (s->pin_func) {
445
pin_state = s->pressure || !s->dav;
453
pin_state = s->pressure;
457
TRACE("report new conversions ready");
458
s->dav |= mode_regs[s->function];
460
TRACE("pin_func=%d, pin_state=%d, pressure=%d, irq=%d, dav=0x%04x",
461
s->pin_func, pin_state, s->pressure, s->irq, s->dav);
463
tsc2005_pin_update(s);
465
if (pin_state != s->irq) {
466
TRACE("changing IRQ state to %d", pin_state);
468
qemu_set_irq(s->pint, s->irq);
473
static void tsc2005_touchscreen_event(void *opaque,
474
int x, int y, int z, int buttons_state)
476
TSC2005State *s = opaque;
483
s->pressure = !!buttons_state;
486
* Note: We would get better responsiveness in the guest by
487
* signaling TS events immediately, but for now we simulate
488
* the first conversion delay for sake of correctness.
490
if (p != s->pressure)
491
tsc2005_pin_update(s);
494
static void tsc2005_save(QEMUFile *f, void *opaque)
496
TSC2005State *s = (TSC2005State *) opaque;
499
qemu_put_be16(f, s->x);
500
qemu_put_be16(f, s->y);
501
qemu_put_byte(f, s->pressure);
503
qemu_put_byte(f, s->state);
504
qemu_put_byte(f, s->reg);
505
qemu_put_byte(f, s->command);
507
qemu_put_byte(f, s->irq);
508
qemu_put_be16s(f, &s->dav);
509
qemu_put_be16s(f, &s->data);
511
qemu_put_timer(f, s->timer);
512
qemu_put_byte(f, s->enabled);
513
qemu_put_byte(f, s->host_mode);
514
qemu_put_byte(f, s->function);
515
qemu_put_byte(f, s->nextfunction);
516
qemu_put_byte(f, s->precision);
517
qemu_put_byte(f, s->nextprecision);
518
qemu_put_be16(f, s->filter);
519
qemu_put_byte(f, s->pin_func);
520
qemu_put_be16(f, s->timing[0]);
521
qemu_put_be16(f, s->timing[1]);
522
qemu_put_be16s(f, &s->temp_thr[0]);
523
qemu_put_be16s(f, &s->temp_thr[1]);
524
qemu_put_be16s(f, &s->aux_thr[0]);
525
qemu_put_be16s(f, &s->aux_thr[1]);
526
qemu_put_be32(f, s->noise);
527
qemu_put_byte(f, s->reset);
528
qemu_put_byte(f, s->pdst);
529
qemu_put_byte(f, s->pnd0);
531
for (i = 0; i < 8; i ++)
532
qemu_put_be32(f, s->tr[i]);
535
static int tsc2005_load(QEMUFile *f, void *opaque, int version_id)
537
TSC2005State *s = (TSC2005State *) opaque;
540
s->x = qemu_get_be16(f);
541
s->y = qemu_get_be16(f);
542
s->pressure = qemu_get_byte(f);
544
s->state = qemu_get_byte(f);
545
s->reg = qemu_get_byte(f);
546
s->command = qemu_get_byte(f);
548
s->irq = qemu_get_byte(f);
549
qemu_get_be16s(f, &s->dav);
550
qemu_get_be16s(f, &s->data);
552
qemu_get_timer(f, s->timer);
553
s->enabled = qemu_get_byte(f);
554
s->host_mode = qemu_get_byte(f);
555
s->function = qemu_get_byte(f);
556
s->nextfunction = qemu_get_byte(f);
557
s->precision = qemu_get_byte(f);
558
s->nextprecision = qemu_get_byte(f);
559
s->filter = qemu_get_be16(f);
560
s->pin_func = qemu_get_byte(f);
561
s->timing[0] = qemu_get_be16(f);
562
s->timing[1] = qemu_get_be16(f);
563
qemu_get_be16s(f, &s->temp_thr[0]);
564
qemu_get_be16s(f, &s->temp_thr[1]);
565
qemu_get_be16s(f, &s->aux_thr[0]);
566
qemu_get_be16s(f, &s->aux_thr[1]);
567
s->noise = qemu_get_be32(f);
568
s->reset = qemu_get_byte(f);
569
s->pdst = qemu_get_byte(f);
570
s->pnd0 = qemu_get_byte(f);
572
for (i = 0; i < 8; i ++)
573
s->tr[i] = qemu_get_be32(f);
575
s->busy = qemu_timer_pending(s->timer);
576
tsc2005_pin_update(s);
581
void *tsc2005_init(qemu_irq pintdav)
586
g_malloc0(sizeof(TSC2005State));
590
s->precision = s->nextprecision = 0;
591
s->timer = qemu_new_timer_ns(vm_clock, tsc2005_timer_tick, s);
609
qemu_add_mouse_event_handler(tsc2005_touchscreen_event, s, 1,
610
"QEMU TSC2005-driven Touchscreen");
612
qemu_register_reset((void *) tsc2005_reset, s);
613
register_savevm(NULL, "tsc2005", -1, 0, tsc2005_save, tsc2005_load, s);
619
* Use tslib generated calibration data to generate ADC input values
620
* from the touchscreen. Assuming 12-bit precision was used during
623
void tsc2005_set_transform(void *opaque, MouseTransformInfo *info,
624
int z1_cons, int z2_cons)
626
TSC2005State *s = (TSC2005State *) opaque;
628
/* This version assumes touchscreen X & Y axis are parallel or
629
* perpendicular to LCD's X & Y axis in some way. */
630
if (abs(info->a[0]) > abs(info->a[1])) {
632
s->tr[1] = -info->a[6] * info->x;
633
s->tr[2] = info->a[0];
634
s->tr[3] = -info->a[2] / info->a[0];
635
s->tr[4] = info->a[6] * info->y;
637
s->tr[6] = info->a[4];
638
s->tr[7] = -info->a[5] / info->a[4];
640
s->tr[0] = info->a[6] * info->y;
642
s->tr[2] = info->a[1];
643
s->tr[3] = -info->a[2] / info->a[1];
645
s->tr[5] = -info->a[6] * info->x;
646
s->tr[6] = info->a[3];
647
s->tr[7] = -info->a[5] / info->a[3];
657
s->z1_cons = z1_cons;
658
s->z2_cons = z2_cons;