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/>.
21 #include "qemu/osdep.h"
24 #include "qemu/timer.h"
25 #include "ui/console.h"
26 #include "hw/devices.h"
29 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10)))
32 qemu_irq pint
; /* Combination of the nPENIRQ and DAV signals */
64 TSC_MODE_XYZ_SCAN
= 0x0,
82 static const uint16_t mode_regs
[16] = {
83 0xf000, /* X, Y, Z scan */
84 0xc000, /* X, Y scan */
91 0x0800, /* AUX scan */
94 0x0080, /* Short-circuit test */
95 0x0000, /* Reserved */
96 0x0000, /* X+, X- drivers */
97 0x0000, /* Y+, Y- drivers */
98 0x0000, /* Y+, X- drivers */
101 #define X_TRANSFORM(s) \
102 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
103 #define Y_TRANSFORM(s) \
104 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
105 #define Z1_TRANSFORM(s) \
106 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
107 #define Z2_TRANSFORM(s) \
108 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
110 #define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */
111 #define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */
112 #define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */
114 static uint16_t tsc2005_read(TSC2005State
*s
, int reg
)
120 s
->dav
&= ~mode_regs
[TSC_MODE_X
];
121 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
124 s
->dav
&= ~mode_regs
[TSC_MODE_Y
];
126 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
130 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
134 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
138 s
->dav
&= ~mode_regs
[TSC_MODE_AUX
];
139 return TSC_CUT_RESOLUTION(AUX_VAL
, s
->precision
);
141 case 0x5: /* TEMP1 */
142 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP1
];
143 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
145 case 0x6: /* TEMP2 */
147 s
->dav
&= ~mode_regs
[TSC_MODE_TEMP2
];
148 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
151 case 0x7: /* Status */
152 ret
= s
->dav
| (s
->reset
<< 7) | (s
->pdst
<< 2) | 0x0;
153 s
->dav
&= ~(mode_regs
[TSC_MODE_X_TEST
] | mode_regs
[TSC_MODE_Y_TEST
] |
154 mode_regs
[TSC_MODE_TS_TEST
]);
158 case 0x8: /* AUX high treshold */
159 return s
->aux_thr
[1];
160 case 0x9: /* AUX low treshold */
161 return s
->aux_thr
[0];
163 case 0xa: /* TEMP high treshold */
164 return s
->temp_thr
[1];
165 case 0xb: /* TEMP low treshold */
166 return s
->temp_thr
[0];
169 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
170 (s
->nextprecision
<< 13) | s
->timing
[0];
174 return (s
->pin_func
<< 14) | s
->filter
;
176 case 0xf: /* Function select status */
177 return s
->function
>= 0 ? 1 << s
->function
: 0;
180 /* Never gets here */
184 static void tsc2005_write(TSC2005State
*s
, int reg
, uint16_t data
)
187 case 0x8: /* AUX high treshold */
188 s
->aux_thr
[1] = data
;
190 case 0x9: /* AUX low treshold */
191 s
->aux_thr
[0] = data
;
194 case 0xa: /* TEMP high treshold */
195 s
->temp_thr
[1] = data
;
197 case 0xb: /* TEMP low treshold */
198 s
->temp_thr
[0] = data
;
202 s
->host_mode
= (data
>> 15) != 0;
203 if (s
->enabled
!= !(data
& 0x4000)) {
204 s
->enabled
= !(data
& 0x4000);
205 trace_tsc2005_sense(s
->enabled
? "enabled" : "disabled");
206 if (s
->busy
&& !s
->enabled
)
208 s
->busy
= s
->busy
&& s
->enabled
;
210 s
->nextprecision
= (data
>> 13) & 1;
211 s
->timing
[0] = data
& 0x1fff;
212 if ((s
->timing
[0] >> 11) == 3) {
213 qemu_log_mask(LOG_GUEST_ERROR
,
214 "tsc2005_write: illegal conversion clock setting\n");
218 s
->timing
[1] = data
& 0xf07;
221 s
->pin_func
= (data
>> 14) & 3;
222 s
->filter
= data
& 0x3fff;
226 qemu_log_mask(LOG_GUEST_ERROR
,
227 "%s: write into read-only register 0x%x\n",
232 /* This handles most of the chip's logic. */
233 static void tsc2005_pin_update(TSC2005State
*s
)
238 switch (s
->pin_func
) {
240 pin_state
= !s
->pressure
&& !!s
->dav
;
248 pin_state
= !s
->pressure
;
251 if (pin_state
!= s
->irq
) {
253 qemu_set_irq(s
->pint
, s
->irq
);
256 switch (s
->nextfunction
) {
257 case TSC_MODE_XYZ_SCAN
:
258 case TSC_MODE_XY_SCAN
:
259 if (!s
->host_mode
&& s
->dav
)
264 case TSC_MODE_AUX_SCAN
:
276 case TSC_MODE_X_TEST
:
277 case TSC_MODE_Y_TEST
:
278 case TSC_MODE_TS_TEST
:
283 case TSC_MODE_RESERVED
:
284 case TSC_MODE_XX_DRV
:
285 case TSC_MODE_YY_DRV
:
286 case TSC_MODE_YX_DRV
:
291 if (!s
->enabled
|| s
->busy
)
295 s
->precision
= s
->nextprecision
;
296 s
->function
= s
->nextfunction
;
297 s
->pdst
= !s
->pnd0
; /* Synchronised on internal clock */
298 expires
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
299 (NANOSECONDS_PER_SECOND
>> 7);
300 timer_mod(s
->timer
, expires
);
303 static void tsc2005_reset(TSC2005State
*s
)
309 s
->nextprecision
= false;
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 trace_tsc2005_sense(s
->enabled
? "enabled" : "disabled");
344 if (s
->busy
&& !s
->enabled
)
346 s
->busy
= s
->busy
&& s
->enabled
;
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 TSC2005State
*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 TSC2005State
*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 int tsc2005_post_load(void *opaque
, int version_id
)
442 TSC2005State
*s
= (TSC2005State
*) opaque
;
444 s
->busy
= timer_pending(s
->timer
);
445 tsc2005_pin_update(s
);
450 static const VMStateDescription vmstate_tsc2005
= {
453 .minimum_version_id
= 2,
454 .post_load
= tsc2005_post_load
,
455 .fields
= (VMStateField
[]) {
456 VMSTATE_BOOL(pressure
, TSC2005State
),
457 VMSTATE_BOOL(irq
, TSC2005State
),
458 VMSTATE_BOOL(command
, TSC2005State
),
459 VMSTATE_BOOL(enabled
, TSC2005State
),
460 VMSTATE_BOOL(host_mode
, TSC2005State
),
461 VMSTATE_BOOL(reset
, TSC2005State
),
462 VMSTATE_BOOL(pdst
, TSC2005State
),
463 VMSTATE_BOOL(pnd0
, TSC2005State
),
464 VMSTATE_BOOL(precision
, TSC2005State
),
465 VMSTATE_BOOL(nextprecision
, TSC2005State
),
466 VMSTATE_UINT8(reg
, TSC2005State
),
467 VMSTATE_UINT8(state
, TSC2005State
),
468 VMSTATE_UINT16(data
, TSC2005State
),
469 VMSTATE_UINT16(dav
, TSC2005State
),
470 VMSTATE_UINT16(filter
, TSC2005State
),
471 VMSTATE_INT8(nextfunction
, TSC2005State
),
472 VMSTATE_INT8(function
, TSC2005State
),
473 VMSTATE_INT32(x
, TSC2005State
),
474 VMSTATE_INT32(y
, TSC2005State
),
475 VMSTATE_TIMER_PTR(timer
, TSC2005State
),
476 VMSTATE_UINT8(pin_func
, TSC2005State
),
477 VMSTATE_UINT16_ARRAY(timing
, TSC2005State
, 2),
478 VMSTATE_UINT8(noise
, TSC2005State
),
479 VMSTATE_UINT16_ARRAY(temp_thr
, TSC2005State
, 2),
480 VMSTATE_UINT16_ARRAY(aux_thr
, TSC2005State
, 2),
481 VMSTATE_INT32_ARRAY(tr
, TSC2005State
, 8),
482 VMSTATE_END_OF_LIST()
486 void *tsc2005_init(qemu_irq pintdav
)
491 g_malloc0(sizeof(TSC2005State
));
495 s
->precision
= s
->nextprecision
= false;
496 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc2005_timer_tick
, s
);
511 qemu_add_mouse_event_handler(tsc2005_touchscreen_event
, s
, 1,
512 "QEMU TSC2005-driven Touchscreen");
514 qemu_register_reset((void *) tsc2005_reset
, s
);
515 vmstate_register(NULL
, 0, &vmstate_tsc2005
, s
);
521 * Use tslib generated calibration data to generate ADC input values
522 * from the touchscreen. Assuming 12-bit precision was used during
525 void tsc2005_set_transform(void *opaque
, MouseTransformInfo
*info
)
527 TSC2005State
*s
= (TSC2005State
*) opaque
;
529 /* This version assumes touchscreen X & Y axis are parallel or
530 * perpendicular to LCD's X & Y axis in some way. */
531 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
533 s
->tr
[1] = -info
->a
[6] * info
->x
;
534 s
->tr
[2] = info
->a
[0];
535 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
536 s
->tr
[4] = info
->a
[6] * info
->y
;
538 s
->tr
[6] = info
->a
[4];
539 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
541 s
->tr
[0] = info
->a
[6] * info
->y
;
543 s
->tr
[2] = info
->a
[1];
544 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
546 s
->tr
[5] = -info
->a
[6] * info
->x
;
547 s
->tr
[6] = info
->a
[3];
548 s
->tr
[7] = -info
->a
[5] / info
->a
[3];