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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include "audio/audio.h"
26 #include "qemu-timer.h"
28 #include "omap.h" /* For struct i2s_codec_s and struct uwire_slave_s */
31 #define TSC_DATA_REGISTERS_PAGE 0x0
32 #define TSC_CONTROL_REGISTERS_PAGE 0x1
33 #define TSC_AUDIO_REGISTERS_PAGE 0x2
37 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - resolution[p]))
39 struct tsc210x_state_s
{
45 struct uwire_slave_s chip
;
46 struct i2s_codec_s codec
;
47 uint8_t in_fifo
[16384];
48 uint8_t out_fifo
[16384];
54 int state
, page
, offset
, irq
;
55 uint16_t command
, dav
;
75 int64_t volume_change
;
79 uint16_t filter_data
[0x14];
82 SWVoiceIn
*adc_voice
[1];
83 SWVoiceOut
*dac_voice
[1];
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(struct tsc210x_state_s
*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
);
215 struct tsc210x_rate_info_s
{
221 /* { rate, dsor, fsref } */
222 static const struct tsc210x_rate_info_s tsc2101_rates
[] = {
251 /* { rate, dsor, fsref } */
252 static const struct tsc210x_rate_info_s tsc2102_rates
[] = {
281 static inline void tsc210x_out_flush(struct tsc210x_state_s
*s
, int len
)
283 uint8_t *data
= s
->codec
.out
.fifo
+ s
->codec
.out
.start
;
284 uint8_t *end
= data
+ len
;
287 data
+= AUD_write(s
->dac_voice
[0], data
, end
- data
) ?: (end
- data
);
289 s
->codec
.out
.len
-= len
;
290 if (s
->codec
.out
.len
)
291 memmove(s
->codec
.out
.fifo
, end
, s
->codec
.out
.len
);
292 s
->codec
.out
.start
= 0;
295 static void tsc210x_audio_out_cb(struct tsc210x_state_s
*s
, int free_b
)
297 if (s
->codec
.out
.len
>= free_b
) {
298 tsc210x_out_flush(s
, free_b
);
302 s
->codec
.out
.size
= MIN(free_b
, 16384);
303 qemu_irq_raise(s
->codec
.tx_start
);
306 static void tsc2102_audio_rate_update(struct tsc210x_state_s
*s
)
308 const struct tsc210x_rate_info_s
*rate
;
310 s
->codec
.tx_rate
= 0;
311 s
->codec
.rx_rate
= 0;
312 if (s
->dac_power
& (1 << 15)) /* PWDNC */
315 for (rate
= tsc2102_rates
; rate
->rate
; rate
++)
316 if (rate
->dsor
== (s
->audio_ctrl1
& 0x3f) && /* DACFS */
317 rate
->fsref
== ((s
->audio_ctrl3
>> 13) & 1))/* REFFS */
320 printf("%s: unknown sampling rate configured\n", __FUNCTION__
);
324 s
->codec
.tx_rate
= rate
->rate
;
327 static void tsc2102_audio_output_update(struct tsc210x_state_s
*s
)
330 struct audsettings fmt
;
332 if (s
->dac_voice
[0]) {
333 tsc210x_out_flush(s
, s
->codec
.out
.len
);
334 s
->codec
.out
.size
= 0;
335 AUD_set_active_out(s
->dac_voice
[0], 0);
336 AUD_close_out(&s
->card
, s
->dac_voice
[0]);
342 (~s
->dac_power
& (1 << 15)) && /* PWDNC */
343 (~s
->dac_power
& (1 << 10)); /* DAPWDN */
344 if (!enable
|| !s
->codec
.tx_rate
)
347 /* Force our own sampling rate even in slave DAC mode */
350 fmt
.freq
= s
->codec
.tx_rate
;
351 fmt
.fmt
= AUD_FMT_S16
;
353 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
354 "tsc2102.sink", s
, (void *) tsc210x_audio_out_cb
, &fmt
);
355 if (s
->dac_voice
[0]) {
357 AUD_set_active_out(s
->dac_voice
[0], 1);
361 static uint16_t tsc2102_data_register_read(struct tsc210x_state_s
*s
, int reg
)
366 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
372 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
377 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
382 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
385 case 0x04: /* KPData */
386 if ((s
->model
& 0xff00) == 0x2300) {
387 if (s
->kb
.intr
&& (s
->kb
.mode
& 2)) {
389 qemu_irq_raise(s
->kbint
);
396 case 0x05: /* BAT1 */
398 return TSC_CUT_RESOLUTION(BAT1_VAL
, s
->precision
) +
401 case 0x06: /* BAT2 */
403 return TSC_CUT_RESOLUTION(BAT2_VAL
, s
->precision
);
405 case 0x07: /* AUX1 */
407 return TSC_CUT_RESOLUTION(AUX1_VAL
, s
->precision
);
409 case 0x08: /* AUX2 */
413 case 0x09: /* TEMP1 */
415 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
418 case 0x0a: /* TEMP2 */
420 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
429 fprintf(stderr
, "tsc2102_data_register_read: "
430 "no such register: 0x%02x\n", reg
);
436 static uint16_t tsc2102_control_register_read(
437 struct tsc210x_state_s
*s
, int reg
)
440 case 0x00: /* TSC ADC */
441 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
442 (s
->nextfunction
<< 10) | (s
->nextprecision
<< 8) | s
->filter
;
444 case 0x01: /* Status / Keypad Control */
445 if ((s
->model
& 0xff00) == 0x2100)
446 return (s
->pin_func
<< 14) | ((!s
->enabled
) << 13) |
447 (s
->host_mode
<< 12) | ((!!s
->dav
) << 11) | s
->dav
;
449 return (s
->kb
.intr
<< 15) | ((s
->kb
.scan
|| !s
->kb
.down
) << 14) |
450 (s
->kb
.debounce
<< 11);
452 case 0x02: /* DAC Control */
453 if ((s
->model
& 0xff00) == 0x2300)
454 return s
->dac_power
& 0x8000;
458 case 0x03: /* Reference */
461 case 0x04: /* Reset */
464 case 0x05: /* Configuration */
467 case 0x06: /* Secondary configuration */
468 if ((s
->model
& 0xff00) == 0x2100)
470 return ((!s
->dav
) << 15) | ((s
->kb
.mode
& 1) << 14) | s
->pll
[2];
472 case 0x10: /* Keypad Mask */
473 if ((s
->model
& 0xff00) == 0x2100)
480 fprintf(stderr
, "tsc2102_control_register_read: "
481 "no such register: 0x%02x\n", reg
);
487 static uint16_t tsc2102_audio_register_read(struct tsc210x_state_s
*s
, int reg
)
493 case 0x00: /* Audio Control 1 */
494 return s
->audio_ctrl1
;
499 case 0x02: /* DAC Volume Control */
505 case 0x04: /* Audio Control 2 */
508 if (s
->softstep
&& !(s
->dac_power
& (1 << 10))) {
509 l_ch
= (qemu_get_clock(vm_clock
) >
510 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
511 r_ch
= (qemu_get_clock(vm_clock
) >
512 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
515 return s
->audio_ctrl2
| (l_ch
<< 3) | (r_ch
<< 2);
517 case 0x05: /* Stereo DAC Power Control */
518 return 0x2aa0 | s
->dac_power
|
519 (((s
->dac_power
& (1 << 10)) &&
520 (qemu_get_clock(vm_clock
) >
521 s
->powerdown
+ TSC_POWEROFF_DELAY
)) << 6);
523 case 0x06: /* Audio Control 3 */
524 val
= s
->audio_ctrl3
| 0x0001;
525 s
->audio_ctrl3
&= 0xff3f;
528 case 0x07: /* LCH_BASS_BOOST_N0 */
529 case 0x08: /* LCH_BASS_BOOST_N1 */
530 case 0x09: /* LCH_BASS_BOOST_N2 */
531 case 0x0a: /* LCH_BASS_BOOST_N3 */
532 case 0x0b: /* LCH_BASS_BOOST_N4 */
533 case 0x0c: /* LCH_BASS_BOOST_N5 */
534 case 0x0d: /* LCH_BASS_BOOST_D1 */
535 case 0x0e: /* LCH_BASS_BOOST_D2 */
536 case 0x0f: /* LCH_BASS_BOOST_D4 */
537 case 0x10: /* LCH_BASS_BOOST_D5 */
538 case 0x11: /* RCH_BASS_BOOST_N0 */
539 case 0x12: /* RCH_BASS_BOOST_N1 */
540 case 0x13: /* RCH_BASS_BOOST_N2 */
541 case 0x14: /* RCH_BASS_BOOST_N3 */
542 case 0x15: /* RCH_BASS_BOOST_N4 */
543 case 0x16: /* RCH_BASS_BOOST_N5 */
544 case 0x17: /* RCH_BASS_BOOST_D1 */
545 case 0x18: /* RCH_BASS_BOOST_D2 */
546 case 0x19: /* RCH_BASS_BOOST_D4 */
547 case 0x1a: /* RCH_BASS_BOOST_D5 */
548 return s
->filter_data
[reg
- 0x07];
550 case 0x1b: /* PLL Programmability 1 */
553 case 0x1c: /* PLL Programmability 2 */
556 case 0x1d: /* Audio Control 4 */
557 return (!s
->softstep
) << 14;
561 fprintf(stderr
, "tsc2102_audio_register_read: "
562 "no such register: 0x%02x\n", reg
);
568 static void tsc2102_data_register_write(
569 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
576 case 0x05: /* BAT1 */
577 case 0x06: /* BAT2 */
578 case 0x07: /* AUX1 */
579 case 0x08: /* AUX2 */
580 case 0x09: /* TEMP1 */
581 case 0x0a: /* TEMP2 */
586 fprintf(stderr
, "tsc2102_data_register_write: "
587 "no such register: 0x%02x\n", reg
);
592 static void tsc2102_control_register_write(
593 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
596 case 0x00: /* TSC ADC */
597 s
->host_mode
= value
>> 15;
598 s
->enabled
= !(value
& 0x4000);
599 if (s
->busy
&& !s
->enabled
)
600 qemu_del_timer(s
->timer
);
601 s
->busy
&= s
->enabled
;
602 s
->nextfunction
= (value
>> 10) & 0xf;
603 s
->nextprecision
= (value
>> 8) & 3;
604 s
->filter
= value
& 0xff;
607 case 0x01: /* Status / Keypad Control */
608 if ((s
->model
& 0xff00) == 0x2100)
609 s
->pin_func
= value
>> 14;
611 s
->kb
.scan
= (value
>> 14) & 1;
612 s
->kb
.debounce
= (value
>> 11) & 7;
613 if (s
->kb
.intr
&& s
->kb
.scan
) {
615 qemu_irq_raise(s
->kbint
);
620 case 0x02: /* DAC Control */
621 if ((s
->model
& 0xff00) == 0x2300) {
622 s
->dac_power
&= 0x7fff;
623 s
->dac_power
|= 0x8000 & value
;
628 case 0x03: /* Reference */
629 s
->ref
= value
& 0x1f;
632 case 0x04: /* Reset */
633 if (value
== 0xbb00) {
635 qemu_del_timer(s
->timer
);
639 fprintf(stderr
, "tsc2102_control_register_write: "
640 "wrong value written into RESET\n");
645 case 0x05: /* Configuration */
646 s
->timing
= value
& 0x3f;
649 fprintf(stderr
, "tsc2102_control_register_write: "
650 "wrong value written into CONFIG\n");
654 case 0x06: /* Secondary configuration */
655 if ((s
->model
& 0xff00) == 0x2100)
657 s
->kb
.mode
= value
>> 14;
658 s
->pll
[2] = value
& 0x3ffff;
661 case 0x10: /* Keypad Mask */
662 if ((s
->model
& 0xff00) == 0x2100)
670 fprintf(stderr
, "tsc2102_control_register_write: "
671 "no such register: 0x%02x\n", reg
);
676 static void tsc2102_audio_register_write(
677 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
680 case 0x00: /* Audio Control 1 */
681 s
->audio_ctrl1
= value
& 0x0f3f;
683 if ((value
& ~0x0f3f) || ((value
& 7) != ((value
>> 3) & 7)))
684 fprintf(stderr
, "tsc2102_audio_register_write: "
685 "wrong value written into Audio 1\n");
687 tsc2102_audio_rate_update(s
);
689 tsc2102_audio_output_update(s
);
695 fprintf(stderr
, "tsc2102_audio_register_write: "
696 "wrong value written into reg 0x01\n");
700 case 0x02: /* DAC Volume Control */
702 s
->volume_change
= qemu_get_clock(vm_clock
);
708 fprintf(stderr
, "tsc2102_audio_register_write: "
709 "wrong value written into reg 0x03\n");
713 case 0x04: /* Audio Control 2 */
714 s
->audio_ctrl2
= value
& 0xf7f2;
717 fprintf(stderr
, "tsc2102_audio_register_write: "
718 "wrong value written into Audio 2\n");
722 case 0x05: /* Stereo DAC Power Control */
723 if ((value
& ~s
->dac_power
) & (1 << 10))
724 s
->powerdown
= qemu_get_clock(vm_clock
);
726 s
->dac_power
= value
& 0x9543;
728 if ((value
& ~0x9543) != 0x2aa0)
729 fprintf(stderr
, "tsc2102_audio_register_write: "
730 "wrong value written into Power\n");
732 tsc2102_audio_rate_update(s
);
734 tsc2102_audio_output_update(s
);
737 case 0x06: /* Audio Control 3 */
738 s
->audio_ctrl3
&= 0x00c0;
739 s
->audio_ctrl3
|= value
& 0xf800;
742 fprintf(stderr
, "tsc2102_audio_register_write: "
743 "wrong value written into Audio 3\n");
746 tsc2102_audio_output_update(s
);
749 case 0x07: /* LCH_BASS_BOOST_N0 */
750 case 0x08: /* LCH_BASS_BOOST_N1 */
751 case 0x09: /* LCH_BASS_BOOST_N2 */
752 case 0x0a: /* LCH_BASS_BOOST_N3 */
753 case 0x0b: /* LCH_BASS_BOOST_N4 */
754 case 0x0c: /* LCH_BASS_BOOST_N5 */
755 case 0x0d: /* LCH_BASS_BOOST_D1 */
756 case 0x0e: /* LCH_BASS_BOOST_D2 */
757 case 0x0f: /* LCH_BASS_BOOST_D4 */
758 case 0x10: /* LCH_BASS_BOOST_D5 */
759 case 0x11: /* RCH_BASS_BOOST_N0 */
760 case 0x12: /* RCH_BASS_BOOST_N1 */
761 case 0x13: /* RCH_BASS_BOOST_N2 */
762 case 0x14: /* RCH_BASS_BOOST_N3 */
763 case 0x15: /* RCH_BASS_BOOST_N4 */
764 case 0x16: /* RCH_BASS_BOOST_N5 */
765 case 0x17: /* RCH_BASS_BOOST_D1 */
766 case 0x18: /* RCH_BASS_BOOST_D2 */
767 case 0x19: /* RCH_BASS_BOOST_D4 */
768 case 0x1a: /* RCH_BASS_BOOST_D5 */
769 s
->filter_data
[reg
- 0x07] = value
;
772 case 0x1b: /* PLL Programmability 1 */
773 s
->pll
[0] = value
& 0xfffc;
776 fprintf(stderr
, "tsc2102_audio_register_write: "
777 "wrong value written into PLL 1\n");
781 case 0x1c: /* PLL Programmability 2 */
782 s
->pll
[1] = value
& 0xfffc;
785 fprintf(stderr
, "tsc2102_audio_register_write: "
786 "wrong value written into PLL 2\n");
790 case 0x1d: /* Audio Control 4 */
791 s
->softstep
= !(value
& 0x4000);
794 fprintf(stderr
, "tsc2102_audio_register_write: "
795 "wrong value written into Audio 4\n");
801 fprintf(stderr
, "tsc2102_audio_register_write: "
802 "no such register: 0x%02x\n", reg
);
807 /* This handles most of the chip logic. */
808 static void tsc210x_pin_update(struct tsc210x_state_s
*s
)
813 switch (s
->pin_func
) {
815 pin_state
= s
->pressure
;
818 pin_state
= !!s
->dav
;
822 pin_state
= s
->pressure
&& !s
->dav
;
828 if (pin_state
!= s
->irq
) {
830 qemu_set_irq(s
->pint
, !s
->irq
);
833 switch (s
->nextfunction
) {
834 case TSC_MODE_XY_SCAN
:
835 case TSC_MODE_XYZ_SCAN
:
855 case TSC_MODE_AUX_SCAN
:
856 case TSC_MODE_PORT_SCAN
:
859 case TSC_MODE_NO_SCAN
:
860 case TSC_MODE_XX_DRV
:
861 case TSC_MODE_YY_DRV
:
862 case TSC_MODE_YX_DRV
:
867 if (!s
->enabled
|| s
->busy
|| s
->dav
)
871 s
->precision
= s
->nextprecision
;
872 s
->function
= s
->nextfunction
;
873 expires
= qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 10);
874 qemu_mod_timer(s
->timer
, expires
);
877 static uint16_t tsc210x_read(struct tsc210x_state_s
*s
)
879 uint16_t ret
= 0x0000;
882 fprintf(stderr
, "tsc210x_read: SPI underrun!\n");
885 case TSC_DATA_REGISTERS_PAGE
:
886 ret
= tsc2102_data_register_read(s
, s
->offset
);
888 qemu_irq_raise(s
->davint
);
890 case TSC_CONTROL_REGISTERS_PAGE
:
891 ret
= tsc2102_control_register_read(s
, s
->offset
);
893 case TSC_AUDIO_REGISTERS_PAGE
:
894 ret
= tsc2102_audio_register_read(s
, s
->offset
);
897 cpu_abort(cpu_single_env
, "tsc210x_read: wrong memory page\n");
900 tsc210x_pin_update(s
);
902 /* Allow sequential reads. */
908 static void tsc210x_write(struct tsc210x_state_s
*s
, uint16_t value
)
911 * This is a two-state state machine for reading
912 * command and data every second time.
915 s
->command
= value
>> 15;
916 s
->page
= (value
>> 11) & 0x0f;
917 s
->offset
= (value
>> 5) & 0x3f;
921 fprintf(stderr
, "tsc210x_write: SPI overrun!\n");
924 case TSC_DATA_REGISTERS_PAGE
:
925 tsc2102_data_register_write(s
, s
->offset
, value
);
927 case TSC_CONTROL_REGISTERS_PAGE
:
928 tsc2102_control_register_write(s
, s
->offset
, value
);
930 case TSC_AUDIO_REGISTERS_PAGE
:
931 tsc2102_audio_register_write(s
, s
->offset
, value
);
934 cpu_abort(cpu_single_env
,
935 "tsc210x_write: wrong memory page\n");
938 tsc210x_pin_update(s
);
943 uint32_t tsc210x_txrx(void *opaque
, uint32_t value
, int len
)
945 struct tsc210x_state_s
*s
= opaque
;
949 cpu_abort(cpu_single_env
, "%s: FIXME: bad SPI word width %i\n",
952 /* TODO: sequential reads etc - how do we make sure the host doesn't
953 * unintentionally read out a conversion result from a register while
954 * transmitting the command word of the next command? */
955 if (!value
|| (s
->state
&& s
->command
))
956 ret
= tsc210x_read(s
);
957 if (value
|| (s
->state
&& !s
->command
))
958 tsc210x_write(s
, value
);
963 static void tsc210x_timer_tick(void *opaque
)
965 struct tsc210x_state_s
*s
= opaque
;
967 /* Timer ticked -- a set of conversions has been finished. */
973 s
->dav
|= mode_regs
[s
->function
];
974 tsc210x_pin_update(s
);
975 qemu_irq_lower(s
->davint
);
978 static void tsc210x_touchscreen_event(void *opaque
,
979 int x
, int y
, int z
, int buttons_state
)
981 struct tsc210x_state_s
*s
= opaque
;
988 s
->pressure
= !!buttons_state
;
991 * Note: We would get better responsiveness in the guest by
992 * signaling TS events immediately, but for now we simulate
993 * the first conversion delay for sake of correctness.
995 if (p
!= s
->pressure
)
996 tsc210x_pin_update(s
);
999 static void tsc210x_i2s_swallow(struct tsc210x_state_s
*s
)
1001 if (s
->dac_voice
[0])
1002 tsc210x_out_flush(s
, s
->codec
.out
.len
);
1004 s
->codec
.out
.len
= 0;
1007 static void tsc210x_i2s_set_rate(struct tsc210x_state_s
*s
, int in
, int out
)
1009 s
->i2s_tx_rate
= out
;
1010 s
->i2s_rx_rate
= in
;
1013 static void tsc210x_save(QEMUFile
*f
, void *opaque
)
1015 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) opaque
;
1016 int64_t now
= qemu_get_clock(vm_clock
);
1019 qemu_put_be16(f
, s
->x
);
1020 qemu_put_be16(f
, s
->y
);
1021 qemu_put_byte(f
, s
->pressure
);
1023 qemu_put_byte(f
, s
->state
);
1024 qemu_put_byte(f
, s
->page
);
1025 qemu_put_byte(f
, s
->offset
);
1026 qemu_put_byte(f
, s
->command
);
1028 qemu_put_byte(f
, s
->irq
);
1029 qemu_put_be16s(f
, &s
->dav
);
1031 qemu_put_timer(f
, s
->timer
);
1032 qemu_put_byte(f
, s
->enabled
);
1033 qemu_put_byte(f
, s
->host_mode
);
1034 qemu_put_byte(f
, s
->function
);
1035 qemu_put_byte(f
, s
->nextfunction
);
1036 qemu_put_byte(f
, s
->precision
);
1037 qemu_put_byte(f
, s
->nextprecision
);
1038 qemu_put_byte(f
, s
->filter
);
1039 qemu_put_byte(f
, s
->pin_func
);
1040 qemu_put_byte(f
, s
->ref
);
1041 qemu_put_byte(f
, s
->timing
);
1042 qemu_put_be32(f
, s
->noise
);
1044 qemu_put_be16s(f
, &s
->audio_ctrl1
);
1045 qemu_put_be16s(f
, &s
->audio_ctrl2
);
1046 qemu_put_be16s(f
, &s
->audio_ctrl3
);
1047 qemu_put_be16s(f
, &s
->pll
[0]);
1048 qemu_put_be16s(f
, &s
->pll
[1]);
1049 qemu_put_be16s(f
, &s
->volume
);
1050 qemu_put_sbe64(f
, (s
->volume_change
- now
));
1051 qemu_put_sbe64(f
, (s
->powerdown
- now
));
1052 qemu_put_byte(f
, s
->softstep
);
1053 qemu_put_be16s(f
, &s
->dac_power
);
1055 for (i
= 0; i
< 0x14; i
++)
1056 qemu_put_be16s(f
, &s
->filter_data
[i
]);
1059 static int tsc210x_load(QEMUFile
*f
, void *opaque
, int version_id
)
1061 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) opaque
;
1062 int64_t now
= qemu_get_clock(vm_clock
);
1065 s
->x
= qemu_get_be16(f
);
1066 s
->y
= qemu_get_be16(f
);
1067 s
->pressure
= qemu_get_byte(f
);
1069 s
->state
= qemu_get_byte(f
);
1070 s
->page
= qemu_get_byte(f
);
1071 s
->offset
= qemu_get_byte(f
);
1072 s
->command
= qemu_get_byte(f
);
1074 s
->irq
= qemu_get_byte(f
);
1075 qemu_get_be16s(f
, &s
->dav
);
1077 qemu_get_timer(f
, s
->timer
);
1078 s
->enabled
= qemu_get_byte(f
);
1079 s
->host_mode
= qemu_get_byte(f
);
1080 s
->function
= qemu_get_byte(f
);
1081 s
->nextfunction
= qemu_get_byte(f
);
1082 s
->precision
= qemu_get_byte(f
);
1083 s
->nextprecision
= qemu_get_byte(f
);
1084 s
->filter
= qemu_get_byte(f
);
1085 s
->pin_func
= qemu_get_byte(f
);
1086 s
->ref
= qemu_get_byte(f
);
1087 s
->timing
= qemu_get_byte(f
);
1088 s
->noise
= qemu_get_be32(f
);
1090 qemu_get_be16s(f
, &s
->audio_ctrl1
);
1091 qemu_get_be16s(f
, &s
->audio_ctrl2
);
1092 qemu_get_be16s(f
, &s
->audio_ctrl3
);
1093 qemu_get_be16s(f
, &s
->pll
[0]);
1094 qemu_get_be16s(f
, &s
->pll
[1]);
1095 qemu_get_be16s(f
, &s
->volume
);
1096 s
->volume_change
= qemu_get_sbe64(f
) + now
;
1097 s
->powerdown
= qemu_get_sbe64(f
) + now
;
1098 s
->softstep
= qemu_get_byte(f
);
1099 qemu_get_be16s(f
, &s
->dac_power
);
1101 for (i
= 0; i
< 0x14; i
++)
1102 qemu_get_be16s(f
, &s
->filter_data
[i
]);
1104 s
->busy
= qemu_timer_pending(s
->timer
);
1105 qemu_set_irq(s
->pint
, !s
->irq
);
1106 qemu_set_irq(s
->davint
, !s
->dav
);
1111 struct uwire_slave_s
*tsc2102_init(qemu_irq pint
, AudioState
*audio
)
1113 struct tsc210x_state_s
*s
;
1115 s
= (struct tsc210x_state_s
*)
1116 qemu_mallocz(sizeof(struct tsc210x_state_s
));
1117 memset(s
, 0, sizeof(struct tsc210x_state_s
));
1121 s
->precision
= s
->nextprecision
= 0;
1122 s
->timer
= qemu_new_timer(vm_clock
, tsc210x_timer_tick
, s
);
1125 s
->name
= "tsc2102";
1138 s
->chip
.send
= (void *) tsc210x_write
;
1139 s
->chip
.receive
= (void *) tsc210x_read
;
1141 s
->codec
.opaque
= s
;
1142 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1143 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1144 s
->codec
.in
.fifo
= s
->in_fifo
;
1145 s
->codec
.out
.fifo
= s
->out_fifo
;
1149 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1150 "QEMU TSC2102-driven Touchscreen");
1153 AUD_register_card(s
->audio
, s
->name
, &s
->card
);
1155 qemu_register_reset((void *) tsc210x_reset
, s
);
1156 register_savevm(s
->name
, -1, 0,
1157 tsc210x_save
, tsc210x_load
, s
);
1162 struct uwire_slave_s
*tsc2301_init(qemu_irq penirq
, qemu_irq kbirq
,
1163 qemu_irq dav
, AudioState
*audio
)
1165 struct tsc210x_state_s
*s
;
1167 s
= (struct tsc210x_state_s
*)
1168 qemu_mallocz(sizeof(struct tsc210x_state_s
));
1169 memset(s
, 0, sizeof(struct tsc210x_state_s
));
1173 s
->precision
= s
->nextprecision
= 0;
1174 s
->timer
= qemu_new_timer(vm_clock
, tsc210x_timer_tick
, s
);
1179 s
->name
= "tsc2301";
1192 s
->chip
.send
= (void *) tsc210x_write
;
1193 s
->chip
.receive
= (void *) tsc210x_read
;
1195 s
->codec
.opaque
= s
;
1196 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1197 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1198 s
->codec
.in
.fifo
= s
->in_fifo
;
1199 s
->codec
.out
.fifo
= s
->out_fifo
;
1203 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1204 "QEMU TSC2301-driven Touchscreen");
1207 AUD_register_card(s
->audio
, s
->name
, &s
->card
);
1209 qemu_register_reset((void *) tsc210x_reset
, s
);
1210 register_savevm(s
->name
, -1, 0, tsc210x_save
, tsc210x_load
, s
);
1215 struct i2s_codec_s
*tsc210x_codec(struct uwire_slave_s
*chip
)
1217 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1223 * Use tslib generated calibration data to generate ADC input values
1224 * from the touchscreen. Assuming 12-bit precision was used during
1225 * tslib calibration.
1227 void tsc210x_set_transform(struct uwire_slave_s
*chip
,
1228 struct mouse_transform_info_s
*info
)
1230 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1234 ltr
[0] = (int64_t) info
->a
[1] * info
->y
;
1235 ltr
[1] = (int64_t) info
->a
[4] * info
->x
;
1236 ltr
[2] = (int64_t) info
->a
[1] * info
->a
[3] -
1237 (int64_t) info
->a
[4] * info
->a
[0];
1238 ltr
[3] = (int64_t) info
->a
[2] * info
->a
[4] -
1239 (int64_t) info
->a
[5] * info
->a
[1];
1240 ltr
[4] = (int64_t) info
->a
[0] * info
->y
;
1241 ltr
[5] = (int64_t) info
->a
[3] * info
->x
;
1242 ltr
[6] = (int64_t) info
->a
[4] * info
->a
[0] -
1243 (int64_t) info
->a
[1] * info
->a
[3];
1244 ltr
[7] = (int64_t) info
->a
[2] * info
->a
[3] -
1245 (int64_t) info
->a
[5] * info
->a
[0];
1247 /* Avoid integer overflow */
1248 s
->tr
[0] = ltr
[0] >> 11;
1249 s
->tr
[1] = ltr
[1] >> 11;
1250 s
->tr
[2] = muldiv64(ltr
[2], 1, info
->a
[6]);
1251 s
->tr
[3] = muldiv64(ltr
[3], 1 << 4, ltr
[2]);
1252 s
->tr
[4] = ltr
[4] >> 11;
1253 s
->tr
[5] = ltr
[5] >> 11;
1254 s
->tr
[6] = muldiv64(ltr
[6], 1, info
->a
[6]);
1255 s
->tr
[7] = muldiv64(ltr
[7], 1 << 4, ltr
[6]);
1258 /* This version assumes touchscreen X & Y axis are parallel or
1259 * perpendicular to LCD's X & Y axis in some way. */
1260 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
1262 s
->tr
[1] = -info
->a
[6] * info
->x
;
1263 s
->tr
[2] = info
->a
[0];
1264 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
1265 s
->tr
[4] = info
->a
[6] * info
->y
;
1267 s
->tr
[6] = info
->a
[4];
1268 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
1270 s
->tr
[0] = info
->a
[6] * info
->y
;
1272 s
->tr
[2] = info
->a
[1];
1273 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
1275 s
->tr
[5] = -info
->a
[6] * info
->x
;
1276 s
->tr
[6] = info
->a
[3];
1277 s
->tr
[7] = -info
->a
[5] / info
->a
[3];
1289 void tsc210x_key_event(struct uwire_slave_s
*chip
, int key
, int down
)
1291 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1294 s
->kb
.down
|= 1 << key
;
1296 s
->kb
.down
&= ~(1 << key
);
1298 if (down
&& (s
->kb
.down
& ~s
->kb
.mask
) && !s
->kb
.intr
) {
1300 qemu_irq_lower(s
->kbint
);
1301 } else if (s
->kb
.intr
&& !(s
->kb
.down
& ~s
->kb
.mask
) &&
1302 !(s
->kb
.mode
& 1)) {
1304 qemu_irq_raise(s
->kbint
);