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 "ui/console.h"
27 #include "hw/arm/omap.h" /* For I2SCodec and uWireSlave */
28 #include "hw/devices.h"
30 #define TSC_DATA_REGISTERS_PAGE 0x0
31 #define TSC_CONTROL_REGISTERS_PAGE 0x1
32 #define TSC_AUDIO_REGISTERS_PAGE 0x2
36 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - resolution[p]))
46 uint8_t in_fifo
[16384];
47 uint8_t out_fifo
[16384];
62 uint8_t function
, nextfunction
;
63 uint8_t precision
, nextprecision
;
75 int64_t volume_change
;
79 uint16_t filter_data
[0x14];
82 SWVoiceIn
*adc_voice
[1];
83 SWVoiceOut
*dac_voice
[1];
97 int64_t now
; /* Time at migration */
100 static const int resolution
[4] = { 12, 8, 10, 12 };
102 #define TSC_MODE_NO_SCAN 0x0
103 #define TSC_MODE_XY_SCAN 0x1
104 #define TSC_MODE_XYZ_SCAN 0x2
105 #define TSC_MODE_X 0x3
106 #define TSC_MODE_Y 0x4
107 #define TSC_MODE_Z 0x5
108 #define TSC_MODE_BAT1 0x6
109 #define TSC_MODE_BAT2 0x7
110 #define TSC_MODE_AUX 0x8
111 #define TSC_MODE_AUX_SCAN 0x9
112 #define TSC_MODE_TEMP1 0xa
113 #define TSC_MODE_PORT_SCAN 0xb
114 #define TSC_MODE_TEMP2 0xc
115 #define TSC_MODE_XX_DRV 0xd
116 #define TSC_MODE_YY_DRV 0xe
117 #define TSC_MODE_YX_DRV 0xf
119 static const uint16_t mode_regs
[16] = {
120 0x0000, /* No scan */
121 0x0600, /* X, Y scan */
122 0x0780, /* X, Y, Z scan */
129 0x0010, /* AUX scan */
131 0x0070, /* Port scan */
133 0x0000, /* X+, X- drivers */
134 0x0000, /* Y+, Y- drivers */
135 0x0000, /* Y+, X- drivers */
138 #define X_TRANSFORM(s) \
139 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
140 #define Y_TRANSFORM(s) \
141 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
142 #define Z1_TRANSFORM(s) \
143 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
144 #define Z2_TRANSFORM(s) \
145 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
147 #define BAT1_VAL 0x8660
148 #define BAT2_VAL 0x0000
149 #define AUX1_VAL 0x35c0
150 #define AUX2_VAL 0xffff
151 #define TEMP1_VAL 0x8c70
152 #define TEMP2_VAL 0xa5b0
154 #define TSC_POWEROFF_DELAY 50
155 #define TSC_SOFTSTEP_DELAY 50
157 static void tsc210x_reset(TSC210xState
*s
)
169 s
->audio_ctrl1
= 0x0000;
170 s
->audio_ctrl2
= 0x4410;
171 s
->audio_ctrl3
= 0x0000;
176 s
->dac_power
= 0x8540;
178 s
->volume_change
= 0;
180 s
->filter_data
[0x00] = 0x6be3;
181 s
->filter_data
[0x01] = 0x9666;
182 s
->filter_data
[0x02] = 0x675d;
183 s
->filter_data
[0x03] = 0x6be3;
184 s
->filter_data
[0x04] = 0x9666;
185 s
->filter_data
[0x05] = 0x675d;
186 s
->filter_data
[0x06] = 0x7d83;
187 s
->filter_data
[0x07] = 0x84ee;
188 s
->filter_data
[0x08] = 0x7d83;
189 s
->filter_data
[0x09] = 0x84ee;
190 s
->filter_data
[0x0a] = 0x6be3;
191 s
->filter_data
[0x0b] = 0x9666;
192 s
->filter_data
[0x0c] = 0x675d;
193 s
->filter_data
[0x0d] = 0x6be3;
194 s
->filter_data
[0x0e] = 0x9666;
195 s
->filter_data
[0x0f] = 0x675d;
196 s
->filter_data
[0x10] = 0x7d83;
197 s
->filter_data
[0x11] = 0x84ee;
198 s
->filter_data
[0x12] = 0x7d83;
199 s
->filter_data
[0x13] = 0x84ee;
210 qemu_set_irq(s
->pint
, !s
->irq
);
211 qemu_set_irq(s
->davint
, !s
->dav
);
212 qemu_irq_raise(s
->kbint
);
221 /* { rate, dsor, fsref } */
222 static const TSC210xRateInfo tsc2102_rates
[] = {
251 static inline void tsc210x_out_flush(TSC210xState
*s
, int len
)
253 uint8_t *data
= s
->codec
.out
.fifo
+ s
->codec
.out
.start
;
254 uint8_t *end
= data
+ len
;
257 data
+= AUD_write(s
->dac_voice
[0], data
, end
- data
) ?: (end
- data
);
259 s
->codec
.out
.len
-= len
;
260 if (s
->codec
.out
.len
)
261 memmove(s
->codec
.out
.fifo
, end
, s
->codec
.out
.len
);
262 s
->codec
.out
.start
= 0;
265 static void tsc210x_audio_out_cb(TSC210xState
*s
, int free_b
)
267 if (s
->codec
.out
.len
>= free_b
) {
268 tsc210x_out_flush(s
, free_b
);
272 s
->codec
.out
.size
= MIN(free_b
, 16384);
273 qemu_irq_raise(s
->codec
.tx_start
);
276 static void tsc2102_audio_rate_update(TSC210xState
*s
)
278 const TSC210xRateInfo
*rate
;
280 s
->codec
.tx_rate
= 0;
281 s
->codec
.rx_rate
= 0;
282 if (s
->dac_power
& (1 << 15)) /* PWDNC */
285 for (rate
= tsc2102_rates
; rate
->rate
; rate
++)
286 if (rate
->dsor
== (s
->audio_ctrl1
& 0x3f) && /* DACFS */
287 rate
->fsref
== ((s
->audio_ctrl3
>> 13) & 1))/* REFFS */
290 printf("%s: unknown sampling rate configured\n", __FUNCTION__
);
294 s
->codec
.tx_rate
= rate
->rate
;
297 static void tsc2102_audio_output_update(TSC210xState
*s
)
300 struct audsettings fmt
;
302 if (s
->dac_voice
[0]) {
303 tsc210x_out_flush(s
, s
->codec
.out
.len
);
304 s
->codec
.out
.size
= 0;
305 AUD_set_active_out(s
->dac_voice
[0], 0);
306 AUD_close_out(&s
->card
, s
->dac_voice
[0]);
307 s
->dac_voice
[0] = NULL
;
312 (~s
->dac_power
& (1 << 15)) && /* PWDNC */
313 (~s
->dac_power
& (1 << 10)); /* DAPWDN */
314 if (!enable
|| !s
->codec
.tx_rate
)
317 /* Force our own sampling rate even in slave DAC mode */
320 fmt
.freq
= s
->codec
.tx_rate
;
321 fmt
.fmt
= AUD_FMT_S16
;
323 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
324 "tsc2102.sink", s
, (void *) tsc210x_audio_out_cb
, &fmt
);
325 if (s
->dac_voice
[0]) {
327 AUD_set_active_out(s
->dac_voice
[0], 1);
331 static uint16_t tsc2102_data_register_read(TSC210xState
*s
, int reg
)
336 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
342 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
347 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
352 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
355 case 0x04: /* KPData */
356 if ((s
->model
& 0xff00) == 0x2300) {
357 if (s
->kb
.intr
&& (s
->kb
.mode
& 2)) {
359 qemu_irq_raise(s
->kbint
);
366 case 0x05: /* BAT1 */
368 return TSC_CUT_RESOLUTION(BAT1_VAL
, s
->precision
) +
371 case 0x06: /* BAT2 */
373 return TSC_CUT_RESOLUTION(BAT2_VAL
, s
->precision
);
375 case 0x07: /* AUX1 */
377 return TSC_CUT_RESOLUTION(AUX1_VAL
, s
->precision
);
379 case 0x08: /* AUX2 */
383 case 0x09: /* TEMP1 */
385 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
388 case 0x0a: /* TEMP2 */
390 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
399 fprintf(stderr
, "tsc2102_data_register_read: "
400 "no such register: 0x%02x\n", reg
);
406 static uint16_t tsc2102_control_register_read(
407 TSC210xState
*s
, int reg
)
410 case 0x00: /* TSC ADC */
411 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
412 (s
->nextfunction
<< 10) | (s
->nextprecision
<< 8) | s
->filter
;
414 case 0x01: /* Status / Keypad Control */
415 if ((s
->model
& 0xff00) == 0x2100)
416 return (s
->pin_func
<< 14) | ((!s
->enabled
) << 13) |
417 (s
->host_mode
<< 12) | ((!!s
->dav
) << 11) | s
->dav
;
419 return (s
->kb
.intr
<< 15) | ((s
->kb
.scan
|| !s
->kb
.down
) << 14) |
420 (s
->kb
.debounce
<< 11);
422 case 0x02: /* DAC Control */
423 if ((s
->model
& 0xff00) == 0x2300)
424 return s
->dac_power
& 0x8000;
428 case 0x03: /* Reference */
431 case 0x04: /* Reset */
434 case 0x05: /* Configuration */
437 case 0x06: /* Secondary configuration */
438 if ((s
->model
& 0xff00) == 0x2100)
440 return ((!s
->dav
) << 15) | ((s
->kb
.mode
& 1) << 14) | s
->pll
[2];
442 case 0x10: /* Keypad Mask */
443 if ((s
->model
& 0xff00) == 0x2100)
450 fprintf(stderr
, "tsc2102_control_register_read: "
451 "no such register: 0x%02x\n", reg
);
457 static uint16_t tsc2102_audio_register_read(TSC210xState
*s
, int reg
)
463 case 0x00: /* Audio Control 1 */
464 return s
->audio_ctrl1
;
469 case 0x02: /* DAC Volume Control */
475 case 0x04: /* Audio Control 2 */
478 if (s
->softstep
&& !(s
->dac_power
& (1 << 10))) {
479 l_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
480 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
481 r_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
482 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
485 return s
->audio_ctrl2
| (l_ch
<< 3) | (r_ch
<< 2);
487 case 0x05: /* Stereo DAC Power Control */
488 return 0x2aa0 | s
->dac_power
|
489 (((s
->dac_power
& (1 << 10)) &&
490 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
491 s
->powerdown
+ TSC_POWEROFF_DELAY
)) << 6);
493 case 0x06: /* Audio Control 3 */
494 val
= s
->audio_ctrl3
| 0x0001;
495 s
->audio_ctrl3
&= 0xff3f;
498 case 0x07: /* LCH_BASS_BOOST_N0 */
499 case 0x08: /* LCH_BASS_BOOST_N1 */
500 case 0x09: /* LCH_BASS_BOOST_N2 */
501 case 0x0a: /* LCH_BASS_BOOST_N3 */
502 case 0x0b: /* LCH_BASS_BOOST_N4 */
503 case 0x0c: /* LCH_BASS_BOOST_N5 */
504 case 0x0d: /* LCH_BASS_BOOST_D1 */
505 case 0x0e: /* LCH_BASS_BOOST_D2 */
506 case 0x0f: /* LCH_BASS_BOOST_D4 */
507 case 0x10: /* LCH_BASS_BOOST_D5 */
508 case 0x11: /* RCH_BASS_BOOST_N0 */
509 case 0x12: /* RCH_BASS_BOOST_N1 */
510 case 0x13: /* RCH_BASS_BOOST_N2 */
511 case 0x14: /* RCH_BASS_BOOST_N3 */
512 case 0x15: /* RCH_BASS_BOOST_N4 */
513 case 0x16: /* RCH_BASS_BOOST_N5 */
514 case 0x17: /* RCH_BASS_BOOST_D1 */
515 case 0x18: /* RCH_BASS_BOOST_D2 */
516 case 0x19: /* RCH_BASS_BOOST_D4 */
517 case 0x1a: /* RCH_BASS_BOOST_D5 */
518 return s
->filter_data
[reg
- 0x07];
520 case 0x1b: /* PLL Programmability 1 */
523 case 0x1c: /* PLL Programmability 2 */
526 case 0x1d: /* Audio Control 4 */
527 return (!s
->softstep
) << 14;
531 fprintf(stderr
, "tsc2102_audio_register_read: "
532 "no such register: 0x%02x\n", reg
);
538 static void tsc2102_data_register_write(
539 TSC210xState
*s
, int reg
, uint16_t value
)
546 case 0x05: /* BAT1 */
547 case 0x06: /* BAT2 */
548 case 0x07: /* AUX1 */
549 case 0x08: /* AUX2 */
550 case 0x09: /* TEMP1 */
551 case 0x0a: /* TEMP2 */
556 fprintf(stderr
, "tsc2102_data_register_write: "
557 "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)
640 fprintf(stderr
, "tsc2102_control_register_write: "
641 "no such register: 0x%02x\n", reg
);
646 static void tsc2102_audio_register_write(
647 TSC210xState
*s
, int reg
, uint16_t value
)
650 case 0x00: /* Audio Control 1 */
651 s
->audio_ctrl1
= value
& 0x0f3f;
653 if ((value
& ~0x0f3f) || ((value
& 7) != ((value
>> 3) & 7)))
654 fprintf(stderr
, "tsc2102_audio_register_write: "
655 "wrong value written into Audio 1\n");
657 tsc2102_audio_rate_update(s
);
658 tsc2102_audio_output_update(s
);
664 fprintf(stderr
, "tsc2102_audio_register_write: "
665 "wrong value written into reg 0x01\n");
669 case 0x02: /* DAC Volume Control */
671 s
->volume_change
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
677 fprintf(stderr
, "tsc2102_audio_register_write: "
678 "wrong value written into reg 0x03\n");
682 case 0x04: /* Audio Control 2 */
683 s
->audio_ctrl2
= value
& 0xf7f2;
686 fprintf(stderr
, "tsc2102_audio_register_write: "
687 "wrong value written into Audio 2\n");
691 case 0x05: /* Stereo DAC Power Control */
692 if ((value
& ~s
->dac_power
) & (1 << 10))
693 s
->powerdown
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
695 s
->dac_power
= value
& 0x9543;
697 if ((value
& ~0x9543) != 0x2aa0)
698 fprintf(stderr
, "tsc2102_audio_register_write: "
699 "wrong value written into Power\n");
701 tsc2102_audio_rate_update(s
);
702 tsc2102_audio_output_update(s
);
705 case 0x06: /* Audio Control 3 */
706 s
->audio_ctrl3
&= 0x00c0;
707 s
->audio_ctrl3
|= value
& 0xf800;
710 fprintf(stderr
, "tsc2102_audio_register_write: "
711 "wrong value written into Audio 3\n");
713 tsc2102_audio_output_update(s
);
716 case 0x07: /* LCH_BASS_BOOST_N0 */
717 case 0x08: /* LCH_BASS_BOOST_N1 */
718 case 0x09: /* LCH_BASS_BOOST_N2 */
719 case 0x0a: /* LCH_BASS_BOOST_N3 */
720 case 0x0b: /* LCH_BASS_BOOST_N4 */
721 case 0x0c: /* LCH_BASS_BOOST_N5 */
722 case 0x0d: /* LCH_BASS_BOOST_D1 */
723 case 0x0e: /* LCH_BASS_BOOST_D2 */
724 case 0x0f: /* LCH_BASS_BOOST_D4 */
725 case 0x10: /* LCH_BASS_BOOST_D5 */
726 case 0x11: /* RCH_BASS_BOOST_N0 */
727 case 0x12: /* RCH_BASS_BOOST_N1 */
728 case 0x13: /* RCH_BASS_BOOST_N2 */
729 case 0x14: /* RCH_BASS_BOOST_N3 */
730 case 0x15: /* RCH_BASS_BOOST_N4 */
731 case 0x16: /* RCH_BASS_BOOST_N5 */
732 case 0x17: /* RCH_BASS_BOOST_D1 */
733 case 0x18: /* RCH_BASS_BOOST_D2 */
734 case 0x19: /* RCH_BASS_BOOST_D4 */
735 case 0x1a: /* RCH_BASS_BOOST_D5 */
736 s
->filter_data
[reg
- 0x07] = value
;
739 case 0x1b: /* PLL Programmability 1 */
740 s
->pll
[0] = value
& 0xfffc;
743 fprintf(stderr
, "tsc2102_audio_register_write: "
744 "wrong value written into PLL 1\n");
748 case 0x1c: /* PLL Programmability 2 */
749 s
->pll
[1] = value
& 0xfffc;
752 fprintf(stderr
, "tsc2102_audio_register_write: "
753 "wrong value written into PLL 2\n");
757 case 0x1d: /* Audio Control 4 */
758 s
->softstep
= !(value
& 0x4000);
761 fprintf(stderr
, "tsc2102_audio_register_write: "
762 "wrong value written into Audio 4\n");
768 fprintf(stderr
, "tsc2102_audio_register_write: "
769 "no such register: 0x%02x\n", reg
);
774 /* This handles most of the chip logic. */
775 static void tsc210x_pin_update(TSC210xState
*s
)
780 switch (s
->pin_func
) {
782 pin_state
= s
->pressure
;
785 pin_state
= !!s
->dav
;
789 pin_state
= s
->pressure
&& !s
->dav
;
795 if (pin_state
!= s
->irq
) {
797 qemu_set_irq(s
->pint
, !s
->irq
);
800 switch (s
->nextfunction
) {
801 case TSC_MODE_XY_SCAN
:
802 case TSC_MODE_XYZ_SCAN
:
822 case TSC_MODE_AUX_SCAN
:
823 case TSC_MODE_PORT_SCAN
:
826 case TSC_MODE_NO_SCAN
:
827 case TSC_MODE_XX_DRV
:
828 case TSC_MODE_YY_DRV
:
829 case TSC_MODE_YX_DRV
:
834 if (!s
->enabled
|| s
->busy
|| s
->dav
)
838 s
->precision
= s
->nextprecision
;
839 s
->function
= s
->nextfunction
;
840 expires
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
841 (NANOSECONDS_PER_SECOND
>> 10);
842 timer_mod(s
->timer
, expires
);
845 static uint16_t tsc210x_read(TSC210xState
*s
)
847 uint16_t ret
= 0x0000;
850 fprintf(stderr
, "tsc210x_read: SPI underrun!\n");
853 case TSC_DATA_REGISTERS_PAGE
:
854 ret
= tsc2102_data_register_read(s
, s
->offset
);
856 qemu_irq_raise(s
->davint
);
858 case TSC_CONTROL_REGISTERS_PAGE
:
859 ret
= tsc2102_control_register_read(s
, s
->offset
);
861 case TSC_AUDIO_REGISTERS_PAGE
:
862 ret
= tsc2102_audio_register_read(s
, s
->offset
);
865 hw_error("tsc210x_read: wrong memory page\n");
868 tsc210x_pin_update(s
);
870 /* Allow sequential reads. */
876 static void tsc210x_write(TSC210xState
*s
, uint16_t value
)
879 * This is a two-state state machine for reading
880 * command and data every second time.
883 s
->command
= (value
>> 15) != 0;
884 s
->page
= (value
>> 11) & 0x0f;
885 s
->offset
= (value
>> 5) & 0x3f;
889 fprintf(stderr
, "tsc210x_write: SPI overrun!\n");
892 case TSC_DATA_REGISTERS_PAGE
:
893 tsc2102_data_register_write(s
, s
->offset
, value
);
895 case TSC_CONTROL_REGISTERS_PAGE
:
896 tsc2102_control_register_write(s
, s
->offset
, value
);
898 case TSC_AUDIO_REGISTERS_PAGE
:
899 tsc2102_audio_register_write(s
, s
->offset
, value
);
902 hw_error("tsc210x_write: wrong memory page\n");
905 tsc210x_pin_update(s
);
910 uint32_t tsc210x_txrx(void *opaque
, uint32_t value
, int len
)
912 TSC210xState
*s
= opaque
;
916 hw_error("%s: FIXME: bad SPI word width %i\n", __FUNCTION__
, len
);
918 /* TODO: sequential reads etc - how do we make sure the host doesn't
919 * unintentionally read out a conversion result from a register while
920 * transmitting the command word of the next command? */
921 if (!value
|| (s
->state
&& s
->command
))
922 ret
= tsc210x_read(s
);
923 if (value
|| (s
->state
&& !s
->command
))
924 tsc210x_write(s
, value
);
929 static void tsc210x_timer_tick(void *opaque
)
931 TSC210xState
*s
= opaque
;
933 /* Timer ticked -- a set of conversions has been finished. */
939 s
->dav
|= mode_regs
[s
->function
];
940 tsc210x_pin_update(s
);
941 qemu_irq_lower(s
->davint
);
944 static void tsc210x_touchscreen_event(void *opaque
,
945 int x
, int y
, int z
, int buttons_state
)
947 TSC210xState
*s
= opaque
;
954 s
->pressure
= !!buttons_state
;
957 * Note: We would get better responsiveness in the guest by
958 * signaling TS events immediately, but for now we simulate
959 * the first conversion delay for sake of correctness.
961 if (p
!= s
->pressure
)
962 tsc210x_pin_update(s
);
965 static void tsc210x_i2s_swallow(TSC210xState
*s
)
968 tsc210x_out_flush(s
, s
->codec
.out
.len
);
970 s
->codec
.out
.len
= 0;
973 static void tsc210x_i2s_set_rate(TSC210xState
*s
, int in
, int out
)
975 s
->i2s_tx_rate
= out
;
979 static void tsc210x_pre_save(void *opaque
)
981 TSC210xState
*s
= (TSC210xState
*) opaque
;
982 s
->now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
985 static int tsc210x_post_load(void *opaque
, int version_id
)
987 TSC210xState
*s
= (TSC210xState
*) opaque
;
988 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
990 if (s
->function
>= ARRAY_SIZE(mode_regs
)) {
993 if (s
->nextfunction
>= ARRAY_SIZE(mode_regs
)) {
996 if (s
->precision
>= ARRAY_SIZE(resolution
)) {
999 if (s
->nextprecision
>= ARRAY_SIZE(resolution
)) {
1003 s
->volume_change
-= s
->now
;
1004 s
->volume_change
+= now
;
1005 s
->powerdown
-= s
->now
;
1006 s
->powerdown
+= now
;
1008 s
->busy
= timer_pending(s
->timer
);
1009 qemu_set_irq(s
->pint
, !s
->irq
);
1010 qemu_set_irq(s
->davint
, !s
->dav
);
1015 static VMStateField vmstatefields_tsc210x
[] = {
1016 VMSTATE_BOOL(enabled
, TSC210xState
),
1017 VMSTATE_BOOL(host_mode
, TSC210xState
),
1018 VMSTATE_BOOL(irq
, TSC210xState
),
1019 VMSTATE_BOOL(command
, TSC210xState
),
1020 VMSTATE_BOOL(pressure
, TSC210xState
),
1021 VMSTATE_BOOL(softstep
, TSC210xState
),
1022 VMSTATE_BOOL(state
, TSC210xState
),
1023 VMSTATE_UINT16(dav
, TSC210xState
),
1024 VMSTATE_INT32(x
, TSC210xState
),
1025 VMSTATE_INT32(y
, TSC210xState
),
1026 VMSTATE_UINT8(offset
, TSC210xState
),
1027 VMSTATE_UINT8(page
, TSC210xState
),
1028 VMSTATE_UINT8(filter
, TSC210xState
),
1029 VMSTATE_UINT8(pin_func
, TSC210xState
),
1030 VMSTATE_UINT8(ref
, TSC210xState
),
1031 VMSTATE_UINT8(timing
, TSC210xState
),
1032 VMSTATE_UINT8(noise
, TSC210xState
),
1033 VMSTATE_UINT8(function
, TSC210xState
),
1034 VMSTATE_UINT8(nextfunction
, TSC210xState
),
1035 VMSTATE_UINT8(precision
, TSC210xState
),
1036 VMSTATE_UINT8(nextprecision
, TSC210xState
),
1037 VMSTATE_UINT16(audio_ctrl1
, TSC210xState
),
1038 VMSTATE_UINT16(audio_ctrl2
, TSC210xState
),
1039 VMSTATE_UINT16(audio_ctrl3
, TSC210xState
),
1040 VMSTATE_UINT16_ARRAY(pll
, TSC210xState
, 3),
1041 VMSTATE_UINT16(volume
, TSC210xState
),
1042 VMSTATE_UINT16(dac_power
, TSC210xState
),
1043 VMSTATE_INT64(volume_change
, TSC210xState
),
1044 VMSTATE_INT64(powerdown
, TSC210xState
),
1045 VMSTATE_INT64(now
, TSC210xState
),
1046 VMSTATE_UINT16_ARRAY(filter_data
, TSC210xState
, 0x14),
1047 VMSTATE_TIMER_PTR(timer
, TSC210xState
),
1048 VMSTATE_END_OF_LIST()
1051 static const VMStateDescription vmstate_tsc2102
= {
1054 .minimum_version_id
= 1,
1055 .pre_save
= tsc210x_pre_save
,
1056 .post_load
= tsc210x_post_load
,
1057 .fields
= vmstatefields_tsc210x
,
1060 static const VMStateDescription vmstate_tsc2301
= {
1063 .minimum_version_id
= 1,
1064 .pre_save
= tsc210x_pre_save
,
1065 .post_load
= tsc210x_post_load
,
1066 .fields
= vmstatefields_tsc210x
,
1069 uWireSlave
*tsc2102_init(qemu_irq pint
)
1073 s
= g_new0(TSC210xState
, 1);
1077 s
->precision
= s
->nextprecision
= 0;
1078 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1081 s
->name
= "tsc2102";
1093 s
->chip
.send
= (void *) tsc210x_write
;
1094 s
->chip
.receive
= (void *) tsc210x_read
;
1096 s
->codec
.opaque
= s
;
1097 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1098 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1099 s
->codec
.in
.fifo
= s
->in_fifo
;
1100 s
->codec
.out
.fifo
= s
->out_fifo
;
1104 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1105 "QEMU TSC2102-driven Touchscreen");
1107 AUD_register_card(s
->name
, &s
->card
);
1109 qemu_register_reset((void *) tsc210x_reset
, s
);
1110 vmstate_register(NULL
, 0, &vmstate_tsc2102
, s
);
1115 uWireSlave
*tsc2301_init(qemu_irq penirq
, qemu_irq kbirq
, qemu_irq dav
)
1119 s
= g_new0(TSC210xState
, 1);
1123 s
->precision
= s
->nextprecision
= 0;
1124 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1129 s
->name
= "tsc2301";
1141 s
->chip
.send
= (void *) tsc210x_write
;
1142 s
->chip
.receive
= (void *) tsc210x_read
;
1144 s
->codec
.opaque
= s
;
1145 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1146 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1147 s
->codec
.in
.fifo
= s
->in_fifo
;
1148 s
->codec
.out
.fifo
= s
->out_fifo
;
1152 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1153 "QEMU TSC2301-driven Touchscreen");
1155 AUD_register_card(s
->name
, &s
->card
);
1157 qemu_register_reset((void *) tsc210x_reset
, s
);
1158 vmstate_register(NULL
, 0, &vmstate_tsc2301
, s
);
1163 I2SCodec
*tsc210x_codec(uWireSlave
*chip
)
1165 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1171 * Use tslib generated calibration data to generate ADC input values
1172 * from the touchscreen. Assuming 12-bit precision was used during
1173 * tslib calibration.
1175 void tsc210x_set_transform(uWireSlave
*chip
,
1176 MouseTransformInfo
*info
)
1178 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1182 ltr
[0] = (int64_t) info
->a
[1] * info
->y
;
1183 ltr
[1] = (int64_t) info
->a
[4] * info
->x
;
1184 ltr
[2] = (int64_t) info
->a
[1] * info
->a
[3] -
1185 (int64_t) info
->a
[4] * info
->a
[0];
1186 ltr
[3] = (int64_t) info
->a
[2] * info
->a
[4] -
1187 (int64_t) info
->a
[5] * info
->a
[1];
1188 ltr
[4] = (int64_t) info
->a
[0] * info
->y
;
1189 ltr
[5] = (int64_t) info
->a
[3] * info
->x
;
1190 ltr
[6] = (int64_t) info
->a
[4] * info
->a
[0] -
1191 (int64_t) info
->a
[1] * info
->a
[3];
1192 ltr
[7] = (int64_t) info
->a
[2] * info
->a
[3] -
1193 (int64_t) info
->a
[5] * info
->a
[0];
1195 /* Avoid integer overflow */
1196 s
->tr
[0] = ltr
[0] >> 11;
1197 s
->tr
[1] = ltr
[1] >> 11;
1198 s
->tr
[2] = muldiv64(ltr
[2], 1, info
->a
[6]);
1199 s
->tr
[3] = muldiv64(ltr
[3], 1 << 4, ltr
[2]);
1200 s
->tr
[4] = ltr
[4] >> 11;
1201 s
->tr
[5] = ltr
[5] >> 11;
1202 s
->tr
[6] = muldiv64(ltr
[6], 1, info
->a
[6]);
1203 s
->tr
[7] = muldiv64(ltr
[7], 1 << 4, ltr
[6]);
1206 /* This version assumes touchscreen X & Y axis are parallel or
1207 * perpendicular to LCD's X & Y axis in some way. */
1208 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
1210 s
->tr
[1] = -info
->a
[6] * info
->x
;
1211 s
->tr
[2] = info
->a
[0];
1212 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
1213 s
->tr
[4] = info
->a
[6] * info
->y
;
1215 s
->tr
[6] = info
->a
[4];
1216 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
1218 s
->tr
[0] = info
->a
[6] * info
->y
;
1220 s
->tr
[2] = info
->a
[1];
1221 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
1223 s
->tr
[5] = -info
->a
[6] * info
->x
;
1224 s
->tr
[6] = info
->a
[3];
1225 s
->tr
[7] = -info
->a
[5] / info
->a
[3];
1237 void tsc210x_key_event(uWireSlave
*chip
, int key
, int down
)
1239 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1242 s
->kb
.down
|= 1 << key
;
1244 s
->kb
.down
&= ~(1 << key
);
1246 if (down
&& (s
->kb
.down
& ~s
->kb
.mask
) && !s
->kb
.intr
) {
1248 qemu_irq_lower(s
->kbint
);
1249 } else if (s
->kb
.intr
&& !(s
->kb
.down
& ~s
->kb
.mask
) &&
1250 !(s
->kb
.mode
& 1)) {
1252 qemu_irq_raise(s
->kbint
);