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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include "qemu-timer.h"
27 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
29 struct tsc2005_state_s
{
30 qemu_irq pint
; /* Combination of the nPENIRQ and DAV signals */
37 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])
102 #define Z1_TRANSFORM(s) \
103 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
104 #define Z2_TRANSFORM(s) \
105 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
107 #define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
108 #define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
109 #define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
111 static uint16_t tsc2005_read(struct tsc2005_state_s
*s
, int reg
)
117 s
->dav
&= ~mode_regs
[TSC_MODE_X
];
118 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
121 s
->dav
&= ~mode_regs
[TSC_MODE_Y
];
123 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
127 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
131 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
135 s
->dav
&= ~mode_regs
[TSC_MODE_AUX
];
136 return TSC_CUT_RESOLUTION(AUX_VAL
, s
->precision
);
138 case 0x5: /* TEMP1 */
139 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP1
];
140 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
142 case 0x6: /* TEMP2 */
144 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP2
];
145 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
148 case 0x7: /* Status */
149 ret
= s
->dav
| (s
->reset
<< 7) | (s
->pdst
<< 2) | 0x0;
150 s
->dav
&= ~(mode_regs
[TSC_MODE_X_TEST
] | mode_regs
[TSC_MODE_Y_TEST
] |
151 mode_regs
[TSC_MODE_TS_TEST
]);
155 case 0x8: /* AUX high treshold */
156 return s
->aux_thr
[1];
157 case 0x9: /* AUX low treshold */
158 return s
->aux_thr
[0];
160 case 0xa: /* TEMP high treshold */
161 return s
->temp_thr
[1];
162 case 0xb: /* TEMP low treshold */
163 return s
->temp_thr
[0];
166 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
167 (s
->nextprecision
<< 13) | s
->timing
[0];
171 return (s
->pin_func
<< 14) | s
->filter
;
173 case 0xf: /* Function select status */
174 return s
->function
>= 0 ? 1 << s
->function
: 0;
177 /* Never gets here */
181 static void tsc2005_write(struct tsc2005_state_s
*s
, int reg
, uint16_t data
)
184 case 0x8: /* AUX high treshold */
185 s
->aux_thr
[1] = data
;
187 case 0x9: /* AUX low treshold */
188 s
->aux_thr
[0] = data
;
191 case 0xa: /* TEMP high treshold */
192 s
->temp_thr
[1] = data
;
194 case 0xb: /* TEMP low treshold */
195 s
->temp_thr
[0] = data
;
199 s
->host_mode
= data
>> 15;
200 s
->enabled
= !(data
& 0x4000);
201 if (s
->busy
&& !s
->enabled
)
202 qemu_del_timer(s
->timer
);
203 s
->busy
&= s
->enabled
;
204 s
->nextprecision
= (data
>> 13) & 1;
205 s
->timing
[0] = data
& 0x1fff;
206 if ((s
->timing
[0] >> 11) == 3)
207 fprintf(stderr
, "%s: illegal conversion clock setting\n",
211 s
->timing
[1] = data
& 0xf07;
214 s
->pin_func
= (data
>> 14) & 3;
215 s
->filter
= data
& 0x3fff;
219 fprintf(stderr
, "%s: write into read-only register %x\n",
224 /* This handles most of the chip's logic. */
225 static void tsc2005_pin_update(struct tsc2005_state_s
*s
)
230 switch (s
->pin_func
) {
232 pin_state
= !s
->pressure
&& !!s
->dav
;
240 pin_state
= !s
->pressure
;
243 if (pin_state
!= s
->irq
) {
245 qemu_set_irq(s
->pint
, s
->irq
);
248 switch (s
->nextfunction
) {
249 case TSC_MODE_XYZ_SCAN
:
250 case TSC_MODE_XY_SCAN
:
251 if (!s
->host_mode
&& s
->dav
)
256 case TSC_MODE_AUX_SCAN
:
268 case TSC_MODE_X_TEST
:
269 case TSC_MODE_Y_TEST
:
270 case TSC_MODE_TS_TEST
:
275 case TSC_MODE_RESERVED
:
276 case TSC_MODE_XX_DRV
:
277 case TSC_MODE_YY_DRV
:
278 case TSC_MODE_YX_DRV
:
283 if (!s
->enabled
|| s
->busy
)
287 s
->precision
= s
->nextprecision
;
288 s
->function
= s
->nextfunction
;
289 s
->pdst
= !s
->pnd0
; /* Synchronised on internal clock */
290 expires
= qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 7);
291 qemu_mod_timer(s
->timer
, expires
);
294 static void tsc2005_reset(struct tsc2005_state_s
*s
)
300 s
->nextprecision
= 0;
310 s
->temp_thr
[0] = 0x000;
311 s
->temp_thr
[1] = 0xfff;
312 s
->aux_thr
[0] = 0x000;
313 s
->aux_thr
[1] = 0xfff;
315 tsc2005_pin_update(s
);
318 uint8_t tsc2005_txrx_word(void *opaque
, uint8_t value
)
320 struct tsc2005_state_s
*s
= opaque
;
323 switch (s
->state
++) {
327 if (value
& (1 << 1))
330 s
->nextfunction
= (value
>> 3) & 0xf;
331 s
->nextprecision
= (value
>> 2) & 1;
332 if (s
->enabled
!= !(value
& 1)) {
333 s
->enabled
= !(value
& 1);
334 fprintf(stderr
, "%s: touchscreen sense %sabled\n",
335 __FUNCTION__
, s
->enabled
? "en" : "dis");
336 if (s
->busy
&& !s
->enabled
)
337 qemu_del_timer(s
->timer
);
338 s
->busy
&= s
->enabled
;
340 tsc2005_pin_update(s
);
346 s
->reg
= (value
>> 3) & 0xf;
347 s
->pnd0
= (value
>> 1) & 1;
348 s
->command
= value
& 1;
352 s
->data
= tsc2005_read(s
, s
->reg
);
353 tsc2005_pin_update(s
);
362 ret
= (s
->data
>> 8) & 0xff;
364 s
->data
|= value
<< 8;
369 ret
= s
->data
& 0xff;
372 tsc2005_write(s
, s
->reg
, s
->data
);
373 tsc2005_pin_update(s
);
383 uint32_t tsc2005_txrx(void *opaque
, uint32_t value
, int len
)
390 ret
|= tsc2005_txrx_word(opaque
, (value
>> len
) & 0xff) << len
;
396 static void tsc2005_timer_tick(void *opaque
)
398 struct tsc2005_state_s
*s
= opaque
;
400 /* Timer ticked -- a set of conversions has been finished. */
406 s
->dav
|= mode_regs
[s
->function
];
408 tsc2005_pin_update(s
);
411 static void tsc2005_touchscreen_event(void *opaque
,
412 int x
, int y
, int z
, int buttons_state
)
414 struct tsc2005_state_s
*s
= opaque
;
421 s
->pressure
= !!buttons_state
;
424 * Note: We would get better responsiveness in the guest by
425 * signaling TS events immediately, but for now we simulate
426 * the first conversion delay for sake of correctness.
428 if (p
!= s
->pressure
)
429 tsc2005_pin_update(s
);
432 static void tsc2005_save(QEMUFile
*f
, void *opaque
)
434 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
437 qemu_put_be16(f
, s
->x
);
438 qemu_put_be16(f
, s
->y
);
439 qemu_put_byte(f
, s
->pressure
);
441 qemu_put_byte(f
, s
->state
);
442 qemu_put_byte(f
, s
->reg
);
443 qemu_put_byte(f
, s
->command
);
445 qemu_put_byte(f
, s
->irq
);
446 qemu_put_be16s(f
, &s
->dav
);
447 qemu_put_be16s(f
, &s
->data
);
449 qemu_put_timer(f
, s
->timer
);
450 qemu_put_byte(f
, s
->enabled
);
451 qemu_put_byte(f
, s
->host_mode
);
452 qemu_put_byte(f
, s
->function
);
453 qemu_put_byte(f
, s
->nextfunction
);
454 qemu_put_byte(f
, s
->precision
);
455 qemu_put_byte(f
, s
->nextprecision
);
456 qemu_put_be16(f
, s
->filter
);
457 qemu_put_byte(f
, s
->pin_func
);
458 qemu_put_be16(f
, s
->timing
[0]);
459 qemu_put_be16(f
, s
->timing
[1]);
460 qemu_put_be16s(f
, &s
->temp_thr
[0]);
461 qemu_put_be16s(f
, &s
->temp_thr
[1]);
462 qemu_put_be16s(f
, &s
->aux_thr
[0]);
463 qemu_put_be16s(f
, &s
->aux_thr
[1]);
464 qemu_put_be32(f
, s
->noise
);
465 qemu_put_byte(f
, s
->reset
);
466 qemu_put_byte(f
, s
->pdst
);
467 qemu_put_byte(f
, s
->pnd0
);
469 for (i
= 0; i
< 8; i
++)
470 qemu_put_be32(f
, s
->tr
[i
]);
473 static int tsc2005_load(QEMUFile
*f
, void *opaque
, int version_id
)
475 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
478 s
->x
= qemu_get_be16(f
);
479 s
->y
= qemu_get_be16(f
);
480 s
->pressure
= qemu_get_byte(f
);
482 s
->state
= qemu_get_byte(f
);
483 s
->reg
= qemu_get_byte(f
);
484 s
->command
= qemu_get_byte(f
);
486 s
->irq
= qemu_get_byte(f
);
487 qemu_get_be16s(f
, &s
->dav
);
488 qemu_get_be16s(f
, &s
->data
);
490 qemu_get_timer(f
, s
->timer
);
491 s
->enabled
= qemu_get_byte(f
);
492 s
->host_mode
= qemu_get_byte(f
);
493 s
->function
= qemu_get_byte(f
);
494 s
->nextfunction
= qemu_get_byte(f
);
495 s
->precision
= qemu_get_byte(f
);
496 s
->nextprecision
= qemu_get_byte(f
);
497 s
->filter
= qemu_get_be16(f
);
498 s
->pin_func
= qemu_get_byte(f
);
499 s
->timing
[0] = qemu_get_be16(f
);
500 s
->timing
[1] = qemu_get_be16(f
);
501 qemu_get_be16s(f
, &s
->temp_thr
[0]);
502 qemu_get_be16s(f
, &s
->temp_thr
[1]);
503 qemu_get_be16s(f
, &s
->aux_thr
[0]);
504 qemu_get_be16s(f
, &s
->aux_thr
[1]);
505 s
->noise
= qemu_get_be32(f
);
506 s
->reset
= qemu_get_byte(f
);
507 s
->pdst
= qemu_get_byte(f
);
508 s
->pnd0
= qemu_get_byte(f
);
510 for (i
= 0; i
< 8; i
++)
511 s
->tr
[i
] = qemu_get_be32(f
);
513 s
->busy
= qemu_timer_pending(s
->timer
);
514 tsc2005_pin_update(s
);
519 static int tsc2005_iid
= 0;
521 void *tsc2005_init(qemu_irq pintdav
)
523 struct tsc2005_state_s
*s
;
525 s
= (struct tsc2005_state_s
*)
526 qemu_mallocz(sizeof(struct tsc2005_state_s
));
530 s
->precision
= s
->nextprecision
= 0;
531 s
->timer
= qemu_new_timer(vm_clock
, tsc2005_timer_tick
, s
);
546 qemu_add_mouse_event_handler(tsc2005_touchscreen_event
, s
, 1,
547 "QEMU TSC2005-driven Touchscreen");
549 qemu_register_reset((void *) tsc2005_reset
, s
);
550 register_savevm("tsc2005", tsc2005_iid
++, 0,
551 tsc2005_save
, tsc2005_load
, s
);
557 * Use tslib generated calibration data to generate ADC input values
558 * from the touchscreen. Assuming 12-bit precision was used during
561 void tsc2005_set_transform(void *opaque
, struct mouse_transform_info_s
*info
)
563 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
565 /* This version assumes touchscreen X & Y axis are parallel or
566 * perpendicular to LCD's X & Y axis in some way. */
567 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
569 s
->tr
[1] = -info
->a
[6] * info
->x
;
570 s
->tr
[2] = info
->a
[0];
571 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
572 s
->tr
[4] = info
->a
[6] * info
->y
;
574 s
->tr
[6] = info
->a
[4];
575 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
577 s
->tr
[0] = info
->a
[6] * info
->y
;
579 s
->tr
[2] = info
->a
[1];
580 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
582 s
->tr
[5] = -info
->a
[6] * info
->x
;
583 s
->tr
[6] = info
->a
[3];
584 s
->tr
[7] = -info
->a
[5] / info
->a
[3];