2 * TI TSC2102 (touchscreen/sensors/audio controller) emulator.
3 * TI TSC2301 (touchscreen/sensors/keypad).
5 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
6 * Copyright (C) 2008 Nokia Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) version 3 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
24 #include "audio/audio.h"
25 #include "qemu/timer.h"
26 #include "sysemu/reset.h"
27 #include "ui/console.h"
28 #include "hw/arm/omap.h" /* For I2SCodec */
29 #include "hw/input/tsc2xxx.h"
32 #define TSC_DATA_REGISTERS_PAGE 0x0
33 #define TSC_CONTROL_REGISTERS_PAGE 0x1
34 #define TSC_AUDIO_REGISTERS_PAGE 0x2
38 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - resolution[p]))
48 uint8_t in_fifo
[16384];
49 uint8_t out_fifo
[16384];
64 uint8_t function
, nextfunction
;
65 uint8_t precision
, nextprecision
;
77 int64_t volume_change
;
81 uint16_t filter_data
[0x14];
84 SWVoiceIn
*adc_voice
[1];
85 SWVoiceOut
*dac_voice
[1];
99 int64_t now
; /* Time at migration */
102 static const int resolution
[4] = { 12, 8, 10, 12 };
104 #define TSC_MODE_NO_SCAN 0x0
105 #define TSC_MODE_XY_SCAN 0x1
106 #define TSC_MODE_XYZ_SCAN 0x2
107 #define TSC_MODE_X 0x3
108 #define TSC_MODE_Y 0x4
109 #define TSC_MODE_Z 0x5
110 #define TSC_MODE_BAT1 0x6
111 #define TSC_MODE_BAT2 0x7
112 #define TSC_MODE_AUX 0x8
113 #define TSC_MODE_AUX_SCAN 0x9
114 #define TSC_MODE_TEMP1 0xa
115 #define TSC_MODE_PORT_SCAN 0xb
116 #define TSC_MODE_TEMP2 0xc
117 #define TSC_MODE_XX_DRV 0xd
118 #define TSC_MODE_YY_DRV 0xe
119 #define TSC_MODE_YX_DRV 0xf
121 static const uint16_t mode_regs
[16] = {
122 0x0000, /* No scan */
123 0x0600, /* X, Y scan */
124 0x0780, /* X, Y, Z scan */
131 0x0010, /* AUX scan */
133 0x0070, /* Port scan */
135 0x0000, /* X+, X- drivers */
136 0x0000, /* Y+, Y- drivers */
137 0x0000, /* Y+, X- drivers */
140 #define X_TRANSFORM(s) \
141 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
142 #define Y_TRANSFORM(s) \
143 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
144 #define Z1_TRANSFORM(s) \
145 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
146 #define Z2_TRANSFORM(s) \
147 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
149 #define BAT1_VAL 0x8660
150 #define BAT2_VAL 0x0000
151 #define AUX1_VAL 0x35c0
152 #define AUX2_VAL 0xffff
153 #define TEMP1_VAL 0x8c70
154 #define TEMP2_VAL 0xa5b0
156 #define TSC_POWEROFF_DELAY 50
157 #define TSC_SOFTSTEP_DELAY 50
159 static void tsc210x_reset(TSC210xState
*s
)
171 s
->audio_ctrl1
= 0x0000;
172 s
->audio_ctrl2
= 0x4410;
173 s
->audio_ctrl3
= 0x0000;
178 s
->dac_power
= 0x8540;
180 s
->volume_change
= 0;
182 s
->filter_data
[0x00] = 0x6be3;
183 s
->filter_data
[0x01] = 0x9666;
184 s
->filter_data
[0x02] = 0x675d;
185 s
->filter_data
[0x03] = 0x6be3;
186 s
->filter_data
[0x04] = 0x9666;
187 s
->filter_data
[0x05] = 0x675d;
188 s
->filter_data
[0x06] = 0x7d83;
189 s
->filter_data
[0x07] = 0x84ee;
190 s
->filter_data
[0x08] = 0x7d83;
191 s
->filter_data
[0x09] = 0x84ee;
192 s
->filter_data
[0x0a] = 0x6be3;
193 s
->filter_data
[0x0b] = 0x9666;
194 s
->filter_data
[0x0c] = 0x675d;
195 s
->filter_data
[0x0d] = 0x6be3;
196 s
->filter_data
[0x0e] = 0x9666;
197 s
->filter_data
[0x0f] = 0x675d;
198 s
->filter_data
[0x10] = 0x7d83;
199 s
->filter_data
[0x11] = 0x84ee;
200 s
->filter_data
[0x12] = 0x7d83;
201 s
->filter_data
[0x13] = 0x84ee;
212 qemu_set_irq(s
->pint
, !s
->irq
);
213 qemu_set_irq(s
->davint
, !s
->dav
);
214 qemu_irq_raise(s
->kbint
);
223 /* { rate, dsor, fsref } */
224 static const TSC210xRateInfo tsc2102_rates
[] = {
253 static inline void tsc210x_out_flush(TSC210xState
*s
, int len
)
255 uint8_t *data
= s
->codec
.out
.fifo
+ s
->codec
.out
.start
;
256 uint8_t *end
= data
+ len
;
259 data
+= AUD_write(s
->dac_voice
[0], data
, end
- data
) ?: (end
- data
);
261 s
->codec
.out
.len
-= len
;
262 if (s
->codec
.out
.len
)
263 memmove(s
->codec
.out
.fifo
, end
, s
->codec
.out
.len
);
264 s
->codec
.out
.start
= 0;
267 static void tsc210x_audio_out_cb(TSC210xState
*s
, int free_b
)
269 if (s
->codec
.out
.len
>= free_b
) {
270 tsc210x_out_flush(s
, free_b
);
274 s
->codec
.out
.size
= MIN(free_b
, 16384);
275 qemu_irq_raise(s
->codec
.tx_start
);
278 static void tsc2102_audio_rate_update(TSC210xState
*s
)
280 const TSC210xRateInfo
*rate
;
282 s
->codec
.tx_rate
= 0;
283 s
->codec
.rx_rate
= 0;
284 if (s
->dac_power
& (1 << 15)) /* PWDNC */
287 for (rate
= tsc2102_rates
; rate
->rate
; rate
++)
288 if (rate
->dsor
== (s
->audio_ctrl1
& 0x3f) && /* DACFS */
289 rate
->fsref
== ((s
->audio_ctrl3
>> 13) & 1))/* REFFS */
292 printf("%s: unknown sampling rate configured\n", __func__
);
296 s
->codec
.tx_rate
= rate
->rate
;
299 static void tsc2102_audio_output_update(TSC210xState
*s
)
302 struct audsettings fmt
;
304 if (s
->dac_voice
[0]) {
305 tsc210x_out_flush(s
, s
->codec
.out
.len
);
306 s
->codec
.out
.size
= 0;
307 AUD_set_active_out(s
->dac_voice
[0], 0);
308 AUD_close_out(&s
->card
, s
->dac_voice
[0]);
309 s
->dac_voice
[0] = NULL
;
314 (~s
->dac_power
& (1 << 15)) && /* PWDNC */
315 (~s
->dac_power
& (1 << 10)); /* DAPWDN */
316 if (!enable
|| !s
->codec
.tx_rate
)
319 /* Force our own sampling rate even in slave DAC mode */
322 fmt
.freq
= s
->codec
.tx_rate
;
323 fmt
.fmt
= AUDIO_FORMAT_S16
;
325 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
326 "tsc2102.sink", s
, (void *) tsc210x_audio_out_cb
, &fmt
);
327 if (s
->dac_voice
[0]) {
329 AUD_set_active_out(s
->dac_voice
[0], 1);
333 static uint16_t tsc2102_data_register_read(TSC210xState
*s
, int reg
)
338 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
344 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
349 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
354 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
357 case 0x04: /* KPData */
358 if ((s
->model
& 0xff00) == 0x2300) {
359 if (s
->kb
.intr
&& (s
->kb
.mode
& 2)) {
361 qemu_irq_raise(s
->kbint
);
368 case 0x05: /* BAT1 */
370 return TSC_CUT_RESOLUTION(BAT1_VAL
, s
->precision
) +
373 case 0x06: /* BAT2 */
375 return TSC_CUT_RESOLUTION(BAT2_VAL
, s
->precision
);
377 case 0x07: /* AUX1 */
379 return TSC_CUT_RESOLUTION(AUX1_VAL
, s
->precision
);
381 case 0x08: /* AUX2 */
385 case 0x09: /* TEMP1 */
387 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
390 case 0x0a: /* TEMP2 */
392 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
401 fprintf(stderr
, "tsc2102_data_register_read: "
402 "no such register: 0x%02x\n", reg
);
408 static uint16_t tsc2102_control_register_read(
409 TSC210xState
*s
, int reg
)
412 case 0x00: /* TSC ADC */
413 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
414 (s
->nextfunction
<< 10) | (s
->nextprecision
<< 8) | s
->filter
;
416 case 0x01: /* Status / Keypad Control */
417 if ((s
->model
& 0xff00) == 0x2100)
418 return (s
->pin_func
<< 14) | ((!s
->enabled
) << 13) |
419 (s
->host_mode
<< 12) | ((!!s
->dav
) << 11) | s
->dav
;
421 return (s
->kb
.intr
<< 15) | ((s
->kb
.scan
|| !s
->kb
.down
) << 14) |
422 (s
->kb
.debounce
<< 11);
424 case 0x02: /* DAC Control */
425 if ((s
->model
& 0xff00) == 0x2300)
426 return s
->dac_power
& 0x8000;
430 case 0x03: /* Reference */
433 case 0x04: /* Reset */
436 case 0x05: /* Configuration */
439 case 0x06: /* Secondary configuration */
440 if ((s
->model
& 0xff00) == 0x2100)
442 return ((!s
->dav
) << 15) | ((s
->kb
.mode
& 1) << 14) | s
->pll
[2];
444 case 0x10: /* Keypad Mask */
445 if ((s
->model
& 0xff00) == 0x2100)
452 fprintf(stderr
, "tsc2102_control_register_read: "
453 "no such register: 0x%02x\n", reg
);
459 static uint16_t tsc2102_audio_register_read(TSC210xState
*s
, int reg
)
465 case 0x00: /* Audio Control 1 */
466 return s
->audio_ctrl1
;
471 case 0x02: /* DAC Volume Control */
477 case 0x04: /* Audio Control 2 */
480 if (s
->softstep
&& !(s
->dac_power
& (1 << 10))) {
481 l_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
482 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
483 r_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
484 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
487 return s
->audio_ctrl2
| (l_ch
<< 3) | (r_ch
<< 2);
489 case 0x05: /* Stereo DAC Power Control */
490 return 0x2aa0 | s
->dac_power
|
491 (((s
->dac_power
& (1 << 10)) &&
492 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
493 s
->powerdown
+ TSC_POWEROFF_DELAY
)) << 6);
495 case 0x06: /* Audio Control 3 */
496 val
= s
->audio_ctrl3
| 0x0001;
497 s
->audio_ctrl3
&= 0xff3f;
500 case 0x07: /* LCH_BASS_BOOST_N0 */
501 case 0x08: /* LCH_BASS_BOOST_N1 */
502 case 0x09: /* LCH_BASS_BOOST_N2 */
503 case 0x0a: /* LCH_BASS_BOOST_N3 */
504 case 0x0b: /* LCH_BASS_BOOST_N4 */
505 case 0x0c: /* LCH_BASS_BOOST_N5 */
506 case 0x0d: /* LCH_BASS_BOOST_D1 */
507 case 0x0e: /* LCH_BASS_BOOST_D2 */
508 case 0x0f: /* LCH_BASS_BOOST_D4 */
509 case 0x10: /* LCH_BASS_BOOST_D5 */
510 case 0x11: /* RCH_BASS_BOOST_N0 */
511 case 0x12: /* RCH_BASS_BOOST_N1 */
512 case 0x13: /* RCH_BASS_BOOST_N2 */
513 case 0x14: /* RCH_BASS_BOOST_N3 */
514 case 0x15: /* RCH_BASS_BOOST_N4 */
515 case 0x16: /* RCH_BASS_BOOST_N5 */
516 case 0x17: /* RCH_BASS_BOOST_D1 */
517 case 0x18: /* RCH_BASS_BOOST_D2 */
518 case 0x19: /* RCH_BASS_BOOST_D4 */
519 case 0x1a: /* RCH_BASS_BOOST_D5 */
520 return s
->filter_data
[reg
- 0x07];
522 case 0x1b: /* PLL Programmability 1 */
525 case 0x1c: /* PLL Programmability 2 */
528 case 0x1d: /* Audio Control 4 */
529 return (!s
->softstep
) << 14;
533 fprintf(stderr
, "tsc2102_audio_register_read: "
534 "no such register: 0x%02x\n", reg
);
540 static void tsc2102_data_register_write(
541 TSC210xState
*s
, int reg
, uint16_t value
)
548 case 0x05: /* BAT1 */
549 case 0x06: /* BAT2 */
550 case 0x07: /* AUX1 */
551 case 0x08: /* AUX2 */
552 case 0x09: /* TEMP1 */
553 case 0x0a: /* TEMP2 */
557 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_data_register_write: "
558 "no such register: 0x%02x\n", reg
);
562 static void tsc2102_control_register_write(
563 TSC210xState
*s
, int reg
, uint16_t value
)
566 case 0x00: /* TSC ADC */
567 s
->host_mode
= value
>> 15;
568 s
->enabled
= !(value
& 0x4000);
569 if (s
->busy
&& !s
->enabled
)
571 s
->busy
= s
->busy
&& s
->enabled
;
572 s
->nextfunction
= (value
>> 10) & 0xf;
573 s
->nextprecision
= (value
>> 8) & 3;
574 s
->filter
= value
& 0xff;
577 case 0x01: /* Status / Keypad Control */
578 if ((s
->model
& 0xff00) == 0x2100)
579 s
->pin_func
= value
>> 14;
581 s
->kb
.scan
= (value
>> 14) & 1;
582 s
->kb
.debounce
= (value
>> 11) & 7;
583 if (s
->kb
.intr
&& s
->kb
.scan
) {
585 qemu_irq_raise(s
->kbint
);
590 case 0x02: /* DAC Control */
591 if ((s
->model
& 0xff00) == 0x2300) {
592 s
->dac_power
&= 0x7fff;
593 s
->dac_power
|= 0x8000 & value
;
598 case 0x03: /* Reference */
599 s
->ref
= value
& 0x1f;
602 case 0x04: /* Reset */
603 if (value
== 0xbb00) {
609 fprintf(stderr
, "tsc2102_control_register_write: "
610 "wrong value written into RESET\n");
615 case 0x05: /* Configuration */
616 s
->timing
= value
& 0x3f;
619 fprintf(stderr
, "tsc2102_control_register_write: "
620 "wrong value written into CONFIG\n");
624 case 0x06: /* Secondary configuration */
625 if ((s
->model
& 0xff00) == 0x2100)
627 s
->kb
.mode
= value
>> 14;
628 s
->pll
[2] = value
& 0x3ffff;
631 case 0x10: /* Keypad Mask */
632 if ((s
->model
& 0xff00) == 0x2100)
639 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_control_register_write: "
640 "no such register: 0x%02x\n", reg
);
644 static void tsc2102_audio_register_write(
645 TSC210xState
*s
, int reg
, uint16_t value
)
648 case 0x00: /* Audio Control 1 */
649 s
->audio_ctrl1
= value
& 0x0f3f;
651 if ((value
& ~0x0f3f) || ((value
& 7) != ((value
>> 3) & 7)))
652 fprintf(stderr
, "tsc2102_audio_register_write: "
653 "wrong value written into Audio 1\n");
655 tsc2102_audio_rate_update(s
);
656 tsc2102_audio_output_update(s
);
662 fprintf(stderr
, "tsc2102_audio_register_write: "
663 "wrong value written into reg 0x01\n");
667 case 0x02: /* DAC Volume Control */
669 s
->volume_change
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
675 fprintf(stderr
, "tsc2102_audio_register_write: "
676 "wrong value written into reg 0x03\n");
680 case 0x04: /* Audio Control 2 */
681 s
->audio_ctrl2
= value
& 0xf7f2;
684 fprintf(stderr
, "tsc2102_audio_register_write: "
685 "wrong value written into Audio 2\n");
689 case 0x05: /* Stereo DAC Power Control */
690 if ((value
& ~s
->dac_power
) & (1 << 10))
691 s
->powerdown
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
693 s
->dac_power
= value
& 0x9543;
695 if ((value
& ~0x9543) != 0x2aa0)
696 fprintf(stderr
, "tsc2102_audio_register_write: "
697 "wrong value written into Power\n");
699 tsc2102_audio_rate_update(s
);
700 tsc2102_audio_output_update(s
);
703 case 0x06: /* Audio Control 3 */
704 s
->audio_ctrl3
&= 0x00c0;
705 s
->audio_ctrl3
|= value
& 0xf800;
708 fprintf(stderr
, "tsc2102_audio_register_write: "
709 "wrong value written into Audio 3\n");
711 tsc2102_audio_output_update(s
);
714 case 0x07: /* LCH_BASS_BOOST_N0 */
715 case 0x08: /* LCH_BASS_BOOST_N1 */
716 case 0x09: /* LCH_BASS_BOOST_N2 */
717 case 0x0a: /* LCH_BASS_BOOST_N3 */
718 case 0x0b: /* LCH_BASS_BOOST_N4 */
719 case 0x0c: /* LCH_BASS_BOOST_N5 */
720 case 0x0d: /* LCH_BASS_BOOST_D1 */
721 case 0x0e: /* LCH_BASS_BOOST_D2 */
722 case 0x0f: /* LCH_BASS_BOOST_D4 */
723 case 0x10: /* LCH_BASS_BOOST_D5 */
724 case 0x11: /* RCH_BASS_BOOST_N0 */
725 case 0x12: /* RCH_BASS_BOOST_N1 */
726 case 0x13: /* RCH_BASS_BOOST_N2 */
727 case 0x14: /* RCH_BASS_BOOST_N3 */
728 case 0x15: /* RCH_BASS_BOOST_N4 */
729 case 0x16: /* RCH_BASS_BOOST_N5 */
730 case 0x17: /* RCH_BASS_BOOST_D1 */
731 case 0x18: /* RCH_BASS_BOOST_D2 */
732 case 0x19: /* RCH_BASS_BOOST_D4 */
733 case 0x1a: /* RCH_BASS_BOOST_D5 */
734 s
->filter_data
[reg
- 0x07] = value
;
737 case 0x1b: /* PLL Programmability 1 */
738 s
->pll
[0] = value
& 0xfffc;
741 fprintf(stderr
, "tsc2102_audio_register_write: "
742 "wrong value written into PLL 1\n");
746 case 0x1c: /* PLL Programmability 2 */
747 s
->pll
[1] = value
& 0xfffc;
750 fprintf(stderr
, "tsc2102_audio_register_write: "
751 "wrong value written into PLL 2\n");
755 case 0x1d: /* Audio Control 4 */
756 s
->softstep
= !(value
& 0x4000);
759 fprintf(stderr
, "tsc2102_audio_register_write: "
760 "wrong value written into Audio 4\n");
765 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_audio_register_write: "
766 "no such register: 0x%02x\n", reg
);
770 /* This handles most of the chip logic. */
771 static void tsc210x_pin_update(TSC210xState
*s
)
776 switch (s
->pin_func
) {
778 pin_state
= s
->pressure
;
781 pin_state
= !!s
->dav
;
785 pin_state
= s
->pressure
&& !s
->dav
;
791 if (pin_state
!= s
->irq
) {
793 qemu_set_irq(s
->pint
, !s
->irq
);
796 switch (s
->nextfunction
) {
797 case TSC_MODE_XY_SCAN
:
798 case TSC_MODE_XYZ_SCAN
:
818 case TSC_MODE_AUX_SCAN
:
819 case TSC_MODE_PORT_SCAN
:
822 case TSC_MODE_NO_SCAN
:
823 case TSC_MODE_XX_DRV
:
824 case TSC_MODE_YY_DRV
:
825 case TSC_MODE_YX_DRV
:
830 if (!s
->enabled
|| s
->busy
|| s
->dav
)
834 s
->precision
= s
->nextprecision
;
835 s
->function
= s
->nextfunction
;
836 expires
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
837 (NANOSECONDS_PER_SECOND
>> 10);
838 timer_mod(s
->timer
, expires
);
841 static uint16_t tsc210x_read(TSC210xState
*s
)
843 uint16_t ret
= 0x0000;
846 fprintf(stderr
, "tsc210x_read: SPI underrun!\n");
849 case TSC_DATA_REGISTERS_PAGE
:
850 ret
= tsc2102_data_register_read(s
, s
->offset
);
852 qemu_irq_raise(s
->davint
);
854 case TSC_CONTROL_REGISTERS_PAGE
:
855 ret
= tsc2102_control_register_read(s
, s
->offset
);
857 case TSC_AUDIO_REGISTERS_PAGE
:
858 ret
= tsc2102_audio_register_read(s
, s
->offset
);
861 hw_error("tsc210x_read: wrong memory page\n");
864 tsc210x_pin_update(s
);
866 /* Allow sequential reads. */
872 static void tsc210x_write(TSC210xState
*s
, uint16_t value
)
875 * This is a two-state state machine for reading
876 * command and data every second time.
879 s
->command
= (value
>> 15) != 0;
880 s
->page
= (value
>> 11) & 0x0f;
881 s
->offset
= (value
>> 5) & 0x3f;
885 fprintf(stderr
, "tsc210x_write: SPI overrun!\n");
888 case TSC_DATA_REGISTERS_PAGE
:
889 tsc2102_data_register_write(s
, s
->offset
, value
);
891 case TSC_CONTROL_REGISTERS_PAGE
:
892 tsc2102_control_register_write(s
, s
->offset
, value
);
894 case TSC_AUDIO_REGISTERS_PAGE
:
895 tsc2102_audio_register_write(s
, s
->offset
, value
);
898 hw_error("tsc210x_write: wrong memory page\n");
901 tsc210x_pin_update(s
);
906 uint32_t tsc210x_txrx(void *opaque
, uint32_t value
, int len
)
908 TSC210xState
*s
= opaque
;
912 hw_error("%s: FIXME: bad SPI word width %i\n", __func__
, len
);
914 /* TODO: sequential reads etc - how do we make sure the host doesn't
915 * unintentionally read out a conversion result from a register while
916 * transmitting the command word of the next command? */
917 if (!value
|| (s
->state
&& s
->command
))
918 ret
= tsc210x_read(s
);
919 if (value
|| (s
->state
&& !s
->command
))
920 tsc210x_write(s
, value
);
925 static void tsc210x_timer_tick(void *opaque
)
927 TSC210xState
*s
= opaque
;
929 /* Timer ticked -- a set of conversions has been finished. */
935 s
->dav
|= mode_regs
[s
->function
];
936 tsc210x_pin_update(s
);
937 qemu_irq_lower(s
->davint
);
940 static void tsc210x_touchscreen_event(void *opaque
,
941 int x
, int y
, int z
, int buttons_state
)
943 TSC210xState
*s
= opaque
;
950 s
->pressure
= !!buttons_state
;
953 * Note: We would get better responsiveness in the guest by
954 * signaling TS events immediately, but for now we simulate
955 * the first conversion delay for sake of correctness.
957 if (p
!= s
->pressure
)
958 tsc210x_pin_update(s
);
961 static void tsc210x_i2s_swallow(TSC210xState
*s
)
964 tsc210x_out_flush(s
, s
->codec
.out
.len
);
966 s
->codec
.out
.len
= 0;
969 static void tsc210x_i2s_set_rate(TSC210xState
*s
, int in
, int out
)
971 s
->i2s_tx_rate
= out
;
975 static int tsc210x_pre_save(void *opaque
)
977 TSC210xState
*s
= (TSC210xState
*) opaque
;
978 s
->now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
983 static int tsc210x_post_load(void *opaque
, int version_id
)
985 TSC210xState
*s
= (TSC210xState
*) opaque
;
986 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
988 if (s
->function
>= ARRAY_SIZE(mode_regs
)) {
991 if (s
->nextfunction
>= ARRAY_SIZE(mode_regs
)) {
994 if (s
->precision
>= ARRAY_SIZE(resolution
)) {
997 if (s
->nextprecision
>= ARRAY_SIZE(resolution
)) {
1001 s
->volume_change
-= s
->now
;
1002 s
->volume_change
+= now
;
1003 s
->powerdown
-= s
->now
;
1004 s
->powerdown
+= now
;
1006 s
->busy
= timer_pending(s
->timer
);
1007 qemu_set_irq(s
->pint
, !s
->irq
);
1008 qemu_set_irq(s
->davint
, !s
->dav
);
1013 static VMStateField vmstatefields_tsc210x
[] = {
1014 VMSTATE_BOOL(enabled
, TSC210xState
),
1015 VMSTATE_BOOL(host_mode
, TSC210xState
),
1016 VMSTATE_BOOL(irq
, TSC210xState
),
1017 VMSTATE_BOOL(command
, TSC210xState
),
1018 VMSTATE_BOOL(pressure
, TSC210xState
),
1019 VMSTATE_BOOL(softstep
, TSC210xState
),
1020 VMSTATE_BOOL(state
, TSC210xState
),
1021 VMSTATE_UINT16(dav
, TSC210xState
),
1022 VMSTATE_INT32(x
, TSC210xState
),
1023 VMSTATE_INT32(y
, TSC210xState
),
1024 VMSTATE_UINT8(offset
, TSC210xState
),
1025 VMSTATE_UINT8(page
, TSC210xState
),
1026 VMSTATE_UINT8(filter
, TSC210xState
),
1027 VMSTATE_UINT8(pin_func
, TSC210xState
),
1028 VMSTATE_UINT8(ref
, TSC210xState
),
1029 VMSTATE_UINT8(timing
, TSC210xState
),
1030 VMSTATE_UINT8(noise
, TSC210xState
),
1031 VMSTATE_UINT8(function
, TSC210xState
),
1032 VMSTATE_UINT8(nextfunction
, TSC210xState
),
1033 VMSTATE_UINT8(precision
, TSC210xState
),
1034 VMSTATE_UINT8(nextprecision
, TSC210xState
),
1035 VMSTATE_UINT16(audio_ctrl1
, TSC210xState
),
1036 VMSTATE_UINT16(audio_ctrl2
, TSC210xState
),
1037 VMSTATE_UINT16(audio_ctrl3
, TSC210xState
),
1038 VMSTATE_UINT16_ARRAY(pll
, TSC210xState
, 3),
1039 VMSTATE_UINT16(volume
, TSC210xState
),
1040 VMSTATE_UINT16(dac_power
, TSC210xState
),
1041 VMSTATE_INT64(volume_change
, TSC210xState
),
1042 VMSTATE_INT64(powerdown
, TSC210xState
),
1043 VMSTATE_INT64(now
, TSC210xState
),
1044 VMSTATE_UINT16_ARRAY(filter_data
, TSC210xState
, 0x14),
1045 VMSTATE_TIMER_PTR(timer
, TSC210xState
),
1046 VMSTATE_END_OF_LIST()
1049 static const VMStateDescription vmstate_tsc2102
= {
1052 .minimum_version_id
= 1,
1053 .pre_save
= tsc210x_pre_save
,
1054 .post_load
= tsc210x_post_load
,
1055 .fields
= vmstatefields_tsc210x
,
1058 static const VMStateDescription vmstate_tsc2301
= {
1061 .minimum_version_id
= 1,
1062 .pre_save
= tsc210x_pre_save
,
1063 .post_load
= tsc210x_post_load
,
1064 .fields
= vmstatefields_tsc210x
,
1067 uWireSlave
*tsc2102_init(qemu_irq pint
)
1071 s
= g_new0(TSC210xState
, 1);
1075 s
->precision
= s
->nextprecision
= 0;
1076 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1079 s
->name
= "tsc2102";
1091 s
->chip
.send
= (void *) tsc210x_write
;
1092 s
->chip
.receive
= (void *) tsc210x_read
;
1094 s
->codec
.opaque
= s
;
1095 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1096 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1097 s
->codec
.in
.fifo
= s
->in_fifo
;
1098 s
->codec
.out
.fifo
= s
->out_fifo
;
1102 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1103 "QEMU TSC2102-driven Touchscreen");
1105 AUD_register_card(s
->name
, &s
->card
);
1107 qemu_register_reset((void *) tsc210x_reset
, s
);
1108 vmstate_register(NULL
, 0, &vmstate_tsc2102
, s
);
1113 uWireSlave
*tsc2301_init(qemu_irq penirq
, qemu_irq kbirq
, qemu_irq dav
)
1117 s
= g_new0(TSC210xState
, 1);
1121 s
->precision
= s
->nextprecision
= 0;
1122 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1127 s
->name
= "tsc2301";
1139 s
->chip
.send
= (void *) tsc210x_write
;
1140 s
->chip
.receive
= (void *) tsc210x_read
;
1142 s
->codec
.opaque
= s
;
1143 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1144 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1145 s
->codec
.in
.fifo
= s
->in_fifo
;
1146 s
->codec
.out
.fifo
= s
->out_fifo
;
1150 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1151 "QEMU TSC2301-driven Touchscreen");
1153 AUD_register_card(s
->name
, &s
->card
);
1155 qemu_register_reset((void *) tsc210x_reset
, s
);
1156 vmstate_register(NULL
, 0, &vmstate_tsc2301
, s
);
1161 I2SCodec
*tsc210x_codec(uWireSlave
*chip
)
1163 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1169 * Use tslib generated calibration data to generate ADC input values
1170 * from the touchscreen. Assuming 12-bit precision was used during
1171 * tslib calibration.
1173 void tsc210x_set_transform(uWireSlave
*chip
,
1174 MouseTransformInfo
*info
)
1176 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1180 ltr
[0] = (int64_t) info
->a
[1] * info
->y
;
1181 ltr
[1] = (int64_t) info
->a
[4] * info
->x
;
1182 ltr
[2] = (int64_t) info
->a
[1] * info
->a
[3] -
1183 (int64_t) info
->a
[4] * info
->a
[0];
1184 ltr
[3] = (int64_t) info
->a
[2] * info
->a
[4] -
1185 (int64_t) info
->a
[5] * info
->a
[1];
1186 ltr
[4] = (int64_t) info
->a
[0] * info
->y
;
1187 ltr
[5] = (int64_t) info
->a
[3] * info
->x
;
1188 ltr
[6] = (int64_t) info
->a
[4] * info
->a
[0] -
1189 (int64_t) info
->a
[1] * info
->a
[3];
1190 ltr
[7] = (int64_t) info
->a
[2] * info
->a
[3] -
1191 (int64_t) info
->a
[5] * info
->a
[0];
1193 /* Avoid integer overflow */
1194 s
->tr
[0] = ltr
[0] >> 11;
1195 s
->tr
[1] = ltr
[1] >> 11;
1196 s
->tr
[2] = muldiv64(ltr
[2], 1, info
->a
[6]);
1197 s
->tr
[3] = muldiv64(ltr
[3], 1 << 4, ltr
[2]);
1198 s
->tr
[4] = ltr
[4] >> 11;
1199 s
->tr
[5] = ltr
[5] >> 11;
1200 s
->tr
[6] = muldiv64(ltr
[6], 1, info
->a
[6]);
1201 s
->tr
[7] = muldiv64(ltr
[7], 1 << 4, ltr
[6]);
1204 /* This version assumes touchscreen X & Y axis are parallel or
1205 * perpendicular to LCD's X & Y axis in some way. */
1206 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
1208 s
->tr
[1] = -info
->a
[6] * info
->x
;
1209 s
->tr
[2] = info
->a
[0];
1210 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
1211 s
->tr
[4] = info
->a
[6] * info
->y
;
1213 s
->tr
[6] = info
->a
[4];
1214 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
1216 s
->tr
[0] = info
->a
[6] * info
->y
;
1218 s
->tr
[2] = info
->a
[1];
1219 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
1221 s
->tr
[5] = -info
->a
[6] * info
->x
;
1222 s
->tr
[6] = info
->a
[3];
1223 s
->tr
[7] = -info
->a
[5] / info
->a
[3];
1235 void tsc210x_key_event(uWireSlave
*chip
, int key
, int down
)
1237 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1240 s
->kb
.down
|= 1 << key
;
1242 s
->kb
.down
&= ~(1 << key
);
1244 if (down
&& (s
->kb
.down
& ~s
->kb
.mask
) && !s
->kb
.intr
) {
1246 qemu_irq_lower(s
->kbint
);
1247 } else if (s
->kb
.intr
&& !(s
->kb
.down
& ~s
->kb
.mask
) &&
1248 !(s
->kb
.mode
& 1)) {
1250 qemu_irq_raise(s
->kbint
);