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"
28 #define TSC_DATA_REGISTERS_PAGE 0x0
29 #define TSC_CONTROL_REGISTERS_PAGE 0x1
30 #define TSC_AUDIO_REGISTERS_PAGE 0x2
34 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
36 struct tsc2005_state_s
{
37 qemu_irq pint
; /* Combination of the nPENIRQ and DAV signals */
44 int state
, reg
, irq
, command
;
68 TSC_MODE_XYZ_SCAN
= 0x0,
86 static const uint16_t mode_regs
[16] = {
87 0xf000, /* X, Y, Z scan */
88 0xc000, /* X, Y scan */
95 0x0800, /* AUX scan */
98 0x0080, /* Short-circuit test */
99 0x0000, /* Reserved */
100 0x0000, /* X+, X- drivers */
101 0x0000, /* Y+, Y- drivers */
102 0x0000, /* Y+, X- drivers */
105 #define X_TRANSFORM(s) \
106 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
107 #define Y_TRANSFORM(s) \
108 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
109 #define Z1_TRANSFORM(s) \
110 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
111 #define Z2_TRANSFORM(s) \
112 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
114 #define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
115 #define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
116 #define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
118 #define TSC_POWEROFF_DELAY 50
119 #define TSC_SOFTSTEP_DELAY 50
121 static uint16_t tsc2005_read(struct tsc2005_state_s
*s
, int reg
)
127 s
->dav
&= ~mode_regs
[TSC_MODE_X
];
128 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
131 s
->dav
&= ~mode_regs
[TSC_MODE_Y
];
133 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
137 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
141 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
145 s
->dav
&= ~mode_regs
[TSC_MODE_AUX
];
146 return TSC_CUT_RESOLUTION(AUX_VAL
, s
->precision
);
148 case 0x5: /* TEMP1 */
149 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP1
];
150 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
152 case 0x6: /* TEMP2 */
154 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP2
];
155 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
158 case 0x7: /* Status */
159 ret
= s
->dav
| (s
->reset
<< 7) | (s
->pdst
<< 2) | 0x0;
160 s
->dav
&= ~(mode_regs
[TSC_MODE_X_TEST
] | mode_regs
[TSC_MODE_Y_TEST
] |
161 mode_regs
[TSC_MODE_TS_TEST
]);
165 case 0x8: /* AUX high treshold */
166 return s
->aux_thr
[1];
167 case 0x9: /* AUX low treshold */
168 return s
->aux_thr
[0];
170 case 0xa: /* TEMP high treshold */
171 return s
->temp_thr
[1];
172 case 0xb: /* TEMP low treshold */
173 return s
->temp_thr
[0];
176 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
177 (s
->nextprecision
<< 13) | s
->timing
[0];
181 return (s
->pin_func
<< 14) | s
->filter
;
183 case 0xf: /* Function select status */
184 return s
->function
>= 0 ? 1 << s
->function
: 0;
187 /* Never gets here */
191 static void tsc2005_write(struct tsc2005_state_s
*s
, int reg
, uint16_t data
)
194 case 0x8: /* AUX high treshold */
195 s
->aux_thr
[1] = data
;
197 case 0x9: /* AUX low treshold */
198 s
->aux_thr
[0] = data
;
201 case 0xa: /* TEMP high treshold */
202 s
->temp_thr
[1] = data
;
204 case 0xb: /* TEMP low treshold */
205 s
->temp_thr
[0] = data
;
209 s
->host_mode
= data
>> 15;
210 s
->enabled
= !(data
& 0x4000);
211 if (s
->busy
&& !s
->enabled
)
212 qemu_del_timer(s
->timer
);
213 s
->busy
&= s
->enabled
;
214 s
->nextprecision
= (data
>> 13) & 1;
215 s
->timing
[0] = data
& 0x1fff;
216 if ((s
->timing
[0] >> 11) == 3)
217 fprintf(stderr
, "%s: illegal conversion clock setting\n",
221 s
->timing
[1] = data
& 0xf07;
224 s
->pin_func
= (data
>> 14) & 3;
225 s
->filter
= data
& 0x3fff;
229 fprintf(stderr
, "%s: write into read-only register %x\n",
234 /* This handles most of the chip's logic. */
235 static void tsc2005_pin_update(struct tsc2005_state_s
*s
)
240 switch (s
->pin_func
) {
242 pin_state
= !s
->pressure
&& !!s
->dav
;
250 pin_state
= !s
->pressure
;
256 if (pin_state
!= s
->irq
) {
258 qemu_set_irq(s
->pint
, s
->irq
);
261 switch (s
->nextfunction
) {
262 case TSC_MODE_XYZ_SCAN
:
263 case TSC_MODE_XY_SCAN
:
267 case TSC_MODE_AUX_SCAN
:
279 case TSC_MODE_X_TEST
:
280 case TSC_MODE_Y_TEST
:
281 case TSC_MODE_TS_TEST
:
286 case TSC_MODE_RESERVED
:
287 case TSC_MODE_XX_DRV
:
288 case TSC_MODE_YY_DRV
:
289 case TSC_MODE_YX_DRV
:
294 if (!s
->enabled
|| s
->busy
)
298 s
->precision
= s
->nextprecision
;
299 s
->function
= s
->nextfunction
;
300 s
->pdst
= !s
->pnd0
; /* Synchronised on internal clock */
301 expires
= qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 7);
302 qemu_mod_timer(s
->timer
, expires
);
305 static void tsc2005_reset(struct tsc2005_state_s
*s
)
311 s
->nextprecision
= 0;
321 s
->temp_thr
[0] = 0x000;
322 s
->temp_thr
[1] = 0xfff;
323 s
->aux_thr
[0] = 0x000;
324 s
->aux_thr
[1] = 0xfff;
326 tsc2005_pin_update(s
);
329 uint8_t tsc2005_txrx_word(void *opaque
, uint8_t value
)
331 struct tsc2005_state_s
*s
= opaque
;
334 switch (s
->state
++) {
338 if (value
& (1 << 1))
341 s
->nextfunction
= (value
>> 3) & 0xf;
342 s
->nextprecision
= (value
>> 2) & 1;
343 if (s
->enabled
!= !(value
& 1)) {
344 s
->enabled
= !(value
& 1);
345 fprintf(stderr
, "%s: touchscreen sense %sabled\n",
346 __FUNCTION__
, s
->enabled
? "en" : "dis");
348 tsc2005_pin_update(s
);
354 s
->reg
= (value
>> 3) & 0xf;
355 s
->pnd0
= (value
>> 1) & 1;
356 s
->command
= value
& 1;
360 s
->data
= tsc2005_read(s
, s
->reg
);
361 tsc2005_pin_update(s
);
370 ret
= (s
->data
>> 8) & 0xff;
372 s
->data
|= value
<< 8;
377 ret
= s
->data
& 0xff;
380 tsc2005_write(s
, s
->reg
, s
->data
);
381 tsc2005_pin_update(s
);
391 uint32_t tsc2005_txrx(void *opaque
, uint32_t value
, int len
)
398 ret
|= tsc2005_txrx_word(opaque
, (value
>> len
) & 0xff) << len
;
404 static void tsc2005_timer_tick(void *opaque
)
406 struct tsc2005_state_s
*s
= opaque
;
408 /* Timer ticked -- a set of conversions has been finished. */
414 s
->dav
|= mode_regs
[s
->function
];
416 tsc2005_pin_update(s
);
419 static void tsc2005_touchscreen_event(void *opaque
,
420 int x
, int y
, int z
, int buttons_state
)
422 struct tsc2005_state_s
*s
= opaque
;
429 s
->pressure
= !!buttons_state
;
432 * Note: We would get better responsiveness in the guest by
433 * signaling TS events immediately, but for now we simulate
434 * the first conversion delay for sake of correctness.
436 if (p
!= s
->pressure
)
437 tsc2005_pin_update(s
);
440 static void tsc2005_save(QEMUFile
*f
, void *opaque
)
442 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
445 qemu_put_be16(f
, s
->x
);
446 qemu_put_be16(f
, s
->y
);
447 qemu_put_byte(f
, s
->pressure
);
449 qemu_put_byte(f
, s
->state
);
450 qemu_put_byte(f
, s
->reg
);
451 qemu_put_byte(f
, s
->command
);
453 qemu_put_byte(f
, s
->irq
);
454 qemu_put_be16s(f
, &s
->dav
);
455 qemu_put_be16s(f
, &s
->data
);
457 qemu_put_timer(f
, s
->timer
);
458 qemu_put_byte(f
, s
->enabled
);
459 qemu_put_byte(f
, s
->host_mode
);
460 qemu_put_byte(f
, s
->function
);
461 qemu_put_byte(f
, s
->nextfunction
);
462 qemu_put_byte(f
, s
->precision
);
463 qemu_put_byte(f
, s
->nextprecision
);
464 qemu_put_be16(f
, s
->filter
);
465 qemu_put_byte(f
, s
->pin_func
);
466 qemu_put_be16(f
, s
->timing
[0]);
467 qemu_put_be16(f
, s
->timing
[1]);
468 qemu_put_be16s(f
, &s
->temp_thr
[0]);
469 qemu_put_be16s(f
, &s
->temp_thr
[1]);
470 qemu_put_be16s(f
, &s
->aux_thr
[0]);
471 qemu_put_be16s(f
, &s
->aux_thr
[1]);
472 qemu_put_be32(f
, s
->noise
);
473 qemu_put_byte(f
, s
->reset
);
474 qemu_put_byte(f
, s
->pdst
);
475 qemu_put_byte(f
, s
->pnd0
);
477 for (i
= 0; i
< 8; i
++)
478 qemu_put_be32(f
, s
->tr
[i
]);
481 static int tsc2005_load(QEMUFile
*f
, void *opaque
, int version_id
)
483 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
486 s
->x
= qemu_get_be16(f
);
487 s
->y
= qemu_get_be16(f
);
488 s
->pressure
= qemu_get_byte(f
);
490 s
->state
= qemu_get_byte(f
);
491 s
->reg
= qemu_get_byte(f
);
492 s
->command
= qemu_get_byte(f
);
494 s
->irq
= qemu_get_byte(f
);
495 qemu_get_be16s(f
, &s
->dav
);
496 qemu_get_be16s(f
, &s
->data
);
498 qemu_get_timer(f
, s
->timer
);
499 s
->enabled
= qemu_get_byte(f
);
500 s
->host_mode
= qemu_get_byte(f
);
501 s
->function
= qemu_get_byte(f
);
502 s
->nextfunction
= qemu_get_byte(f
);
503 s
->precision
= qemu_get_byte(f
);
504 s
->nextprecision
= qemu_get_byte(f
);
505 s
->filter
= qemu_get_be16(f
);
506 s
->pin_func
= qemu_get_byte(f
);
507 s
->timing
[0] = qemu_get_be16(f
);
508 s
->timing
[1] = qemu_get_be16(f
);
509 qemu_get_be16s(f
, &s
->temp_thr
[0]);
510 qemu_get_be16s(f
, &s
->temp_thr
[1]);
511 qemu_get_be16s(f
, &s
->aux_thr
[0]);
512 qemu_get_be16s(f
, &s
->aux_thr
[1]);
513 s
->noise
= qemu_get_be32(f
);
514 s
->reset
= qemu_get_byte(f
);
515 s
->pdst
= qemu_get_byte(f
);
516 s
->pnd0
= qemu_get_byte(f
);
518 for (i
= 0; i
< 8; i
++)
519 s
->tr
[i
] = qemu_get_be32(f
);
521 s
->busy
= qemu_timer_pending(s
->timer
);
522 tsc2005_pin_update(s
);
527 static int tsc2005_iid
= 0;
529 void *tsc2005_init(qemu_irq pintdav
)
531 struct tsc2005_state_s
*s
;
533 s
= (struct tsc2005_state_s
*)
534 qemu_mallocz(sizeof(struct tsc2005_state_s
));
538 s
->precision
= s
->nextprecision
= 0;
539 s
->timer
= qemu_new_timer(vm_clock
, tsc2005_timer_tick
, s
);
554 qemu_add_mouse_event_handler(tsc2005_touchscreen_event
, s
, 1,
555 "QEMU TSC2005-driven Touchscreen");
557 qemu_register_reset((void *) tsc2005_reset
, s
);
558 register_savevm("tsc2005", tsc2005_iid
++, 0,
559 tsc2005_save
, tsc2005_load
, s
);
565 * Use tslib generated calibration data to generate ADC input values
566 * from the touchscreen. Assuming 12-bit precision was used during
569 void tsc2005_set_transform(void *opaque
, struct mouse_transform_info_s
*info
)
571 struct tsc2005_state_s
*s
= (struct tsc2005_state_s
*) opaque
;
573 /* This version assumes touchscreen X & Y axis are parallel or
574 * perpendicular to LCD's X & Y axis in some way. */
575 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
577 s
->tr
[1] = -info
->a
[6] * info
->x
;
578 s
->tr
[2] = info
->a
[0];
579 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
580 s
->tr
[4] = info
->a
[6] * info
->y
;
582 s
->tr
[6] = info
->a
[4];
583 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
585 s
->tr
[0] = info
->a
[6] * info
->y
;
587 s
->tr
[2] = info
->a
[1];
588 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
590 s
->tr
[5] = -info
->a
[6] * info
->x
;
591 s
->tr
[6] = info
->a
[3];
592 s
->tr
[7] = -info
->a
[5] / info
->a
[3];