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 */
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]))
38 struct tsc210x_state_s
{
44 struct uwire_slave_s chip
;
45 struct i2s_codec_s codec
;
46 uint8_t in_fifo
[16384];
47 uint8_t out_fifo
[16384];
53 int state
, page
, offset
, irq
;
54 uint16_t command
, dav
;
74 int64_t volume_change
;
78 uint16_t filter_data
[0x14];
81 SWVoiceIn
*adc_voice
[1];
82 SWVoiceOut
*dac_voice
[1];
99 static const int resolution
[4] = { 12, 8, 10, 12 };
101 #define TSC_MODE_NO_SCAN 0x0
102 #define TSC_MODE_XY_SCAN 0x1
103 #define TSC_MODE_XYZ_SCAN 0x2
104 #define TSC_MODE_X 0x3
105 #define TSC_MODE_Y 0x4
106 #define TSC_MODE_Z 0x5
107 #define TSC_MODE_BAT1 0x6
108 #define TSC_MODE_BAT2 0x7
109 #define TSC_MODE_AUX 0x8
110 #define TSC_MODE_AUX_SCAN 0x9
111 #define TSC_MODE_TEMP1 0xa
112 #define TSC_MODE_PORT_SCAN 0xb
113 #define TSC_MODE_TEMP2 0xc
114 #define TSC_MODE_XX_DRV 0xd
115 #define TSC_MODE_YY_DRV 0xe
116 #define TSC_MODE_YX_DRV 0xf
118 static const uint16_t mode_regs
[16] = {
119 0x0000, /* No scan */
120 0x0600, /* X, Y scan */
121 0x0780, /* X, Y, Z scan */
128 0x0010, /* AUX scan */
130 0x0070, /* Port scan */
132 0x0000, /* X+, X- drivers */
133 0x0000, /* Y+, Y- drivers */
134 0x0000, /* Y+, X- drivers */
137 #define X_TRANSFORM(s) \
138 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
139 #define Y_TRANSFORM(s) \
140 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
141 #define Z1_TRANSFORM(s) \
142 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
143 #define Z2_TRANSFORM(s) \
144 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
146 #define BAT1_VAL 0x8660
147 #define BAT2_VAL 0x0000
148 #define AUX1_VAL 0x35c0
149 #define AUX2_VAL 0xffff
150 #define TEMP1_VAL 0x8c70
151 #define TEMP2_VAL 0xa5b0
153 #define TSC_POWEROFF_DELAY 50
154 #define TSC_SOFTSTEP_DELAY 50
156 static void tsc210x_reset(struct tsc210x_state_s
*s
)
168 s
->audio_ctrl1
= 0x0000;
169 s
->audio_ctrl2
= 0x4410;
170 s
->audio_ctrl3
= 0x0000;
175 s
->dac_power
= 0x8540;
177 s
->volume_change
= 0;
179 s
->filter_data
[0x00] = 0x6be3;
180 s
->filter_data
[0x01] = 0x9666;
181 s
->filter_data
[0x02] = 0x675d;
182 s
->filter_data
[0x03] = 0x6be3;
183 s
->filter_data
[0x04] = 0x9666;
184 s
->filter_data
[0x05] = 0x675d;
185 s
->filter_data
[0x06] = 0x7d83;
186 s
->filter_data
[0x07] = 0x84ee;
187 s
->filter_data
[0x08] = 0x7d83;
188 s
->filter_data
[0x09] = 0x84ee;
189 s
->filter_data
[0x0a] = 0x6be3;
190 s
->filter_data
[0x0b] = 0x9666;
191 s
->filter_data
[0x0c] = 0x675d;
192 s
->filter_data
[0x0d] = 0x6be3;
193 s
->filter_data
[0x0e] = 0x9666;
194 s
->filter_data
[0x0f] = 0x675d;
195 s
->filter_data
[0x10] = 0x7d83;
196 s
->filter_data
[0x11] = 0x84ee;
197 s
->filter_data
[0x12] = 0x7d83;
198 s
->filter_data
[0x13] = 0x84ee;
209 qemu_set_irq(s
->pint
, !s
->irq
);
210 qemu_set_irq(s
->davint
, !s
->dav
);
211 qemu_irq_raise(s
->kbint
);
214 struct tsc210x_rate_info_s
{
220 /* { rate, dsor, fsref } */
221 static const struct tsc210x_rate_info_s tsc2101_rates
[] = {
250 /* { rate, dsor, fsref } */
251 static const struct tsc210x_rate_info_s tsc2102_rates
[] = {
280 static inline void tsc210x_out_flush(struct tsc210x_state_s
*s
, int len
)
282 uint8_t *data
= s
->codec
.out
.fifo
+ s
->codec
.out
.start
;
283 uint8_t *end
= data
+ len
;
286 data
+= AUD_write(s
->dac_voice
[0], data
, end
- data
) ?: (end
- data
);
288 s
->codec
.out
.len
-= len
;
289 if (s
->codec
.out
.len
)
290 memmove(s
->codec
.out
.fifo
, end
, s
->codec
.out
.len
);
291 s
->codec
.out
.start
= 0;
294 static void tsc210x_audio_out_cb(struct tsc210x_state_s
*s
, int free_b
)
296 if (s
->codec
.out
.len
>= free_b
) {
297 tsc210x_out_flush(s
, free_b
);
301 s
->codec
.out
.size
= MIN(free_b
, 16384);
302 qemu_irq_raise(s
->codec
.tx_start
);
305 static void tsc2102_audio_rate_update(struct tsc210x_state_s
*s
)
307 const struct tsc210x_rate_info_s
*rate
;
309 s
->codec
.tx_rate
= 0;
310 s
->codec
.rx_rate
= 0;
311 if (s
->dac_power
& (1 << 15)) /* PWDNC */
314 for (rate
= tsc2102_rates
; rate
->rate
; rate
++)
315 if (rate
->dsor
== (s
->audio_ctrl1
& 0x3f) && /* DACFS */
316 rate
->fsref
== ((s
->audio_ctrl3
>> 13) & 1))/* REFFS */
319 printf("%s: unknown sampling rate configured\n", __FUNCTION__
);
323 s
->codec
.tx_rate
= rate
->rate
;
326 static void tsc2102_audio_output_update(struct tsc210x_state_s
*s
)
331 if (s
->dac_voice
[0]) {
332 tsc210x_out_flush(s
, s
->codec
.out
.len
);
333 s
->codec
.out
.size
= 0;
334 AUD_set_active_out(s
->dac_voice
[0], 0);
335 AUD_close_out(&s
->card
, s
->dac_voice
[0]);
341 (~s
->dac_power
& (1 << 15)) && /* PWDNC */
342 (~s
->dac_power
& (1 << 10)); /* DAPWDN */
343 if (!enable
|| !s
->codec
.tx_rate
)
346 /* Force our own sampling rate even in slave DAC mode */
349 fmt
.freq
= s
->codec
.tx_rate
;
350 fmt
.fmt
= AUD_FMT_S16
;
352 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
353 "tsc2102.sink", s
, (void *) tsc210x_audio_out_cb
, &fmt
);
354 if (s
->dac_voice
[0]) {
356 AUD_set_active_out(s
->dac_voice
[0], 1);
360 static uint16_t tsc2102_data_register_read(struct tsc210x_state_s
*s
, int reg
)
365 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
371 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
376 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
381 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
384 case 0x04: /* KPData */
385 if ((s
->model
& 0xff00) == 0x2300) {
386 if (s
->kb
.intr
&& (s
->kb
.mode
& 2)) {
388 qemu_irq_raise(s
->kbint
);
395 case 0x05: /* BAT1 */
397 return TSC_CUT_RESOLUTION(BAT1_VAL
, s
->precision
) +
400 case 0x06: /* BAT2 */
402 return TSC_CUT_RESOLUTION(BAT2_VAL
, s
->precision
);
404 case 0x07: /* AUX1 */
406 return TSC_CUT_RESOLUTION(AUX1_VAL
, s
->precision
);
408 case 0x08: /* AUX2 */
412 case 0x09: /* TEMP1 */
414 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
417 case 0x0a: /* TEMP2 */
419 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
428 fprintf(stderr
, "tsc2102_data_register_read: "
429 "no such register: 0x%02x\n", reg
);
435 static uint16_t tsc2102_control_register_read(
436 struct tsc210x_state_s
*s
, int reg
)
439 case 0x00: /* TSC ADC */
440 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
441 (s
->nextfunction
<< 10) | (s
->nextprecision
<< 8) | s
->filter
;
443 case 0x01: /* Status / Keypad Control */
444 if ((s
->model
& 0xff00) == 0x2100)
445 return (s
->pin_func
<< 14) | ((!s
->enabled
) << 13) |
446 (s
->host_mode
<< 12) | ((!!s
->dav
) << 11) | s
->dav
;
448 return (s
->kb
.intr
<< 15) | ((s
->kb
.scan
|| !s
->kb
.down
) << 14) |
449 (s
->kb
.debounce
<< 11);
451 case 0x02: /* DAC Control */
452 if ((s
->model
& 0xff00) == 0x2300)
453 return s
->dac_power
& 0x8000;
457 case 0x03: /* Reference */
460 case 0x04: /* Reset */
463 case 0x05: /* Configuration */
466 case 0x06: /* Secondary configuration */
467 if ((s
->model
& 0xff00) == 0x2100)
469 return ((!s
->dav
) << 15) | ((s
->kb
.mode
& 1) << 14) | s
->pll
[2];
471 case 0x10: /* Keypad Mask */
472 if ((s
->model
& 0xff00) == 0x2100)
479 fprintf(stderr
, "tsc2102_control_register_read: "
480 "no such register: 0x%02x\n", reg
);
486 static uint16_t tsc2102_audio_register_read(struct tsc210x_state_s
*s
, int reg
)
492 case 0x00: /* Audio Control 1 */
493 return s
->audio_ctrl1
;
498 case 0x02: /* DAC Volume Control */
504 case 0x04: /* Audio Control 2 */
507 if (s
->softstep
&& !(s
->dac_power
& (1 << 10))) {
508 l_ch
= (qemu_get_clock(vm_clock
) >
509 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
510 r_ch
= (qemu_get_clock(vm_clock
) >
511 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
514 return s
->audio_ctrl2
| (l_ch
<< 3) | (r_ch
<< 2);
516 case 0x05: /* Stereo DAC Power Control */
517 return 0x2aa0 | s
->dac_power
|
518 (((s
->dac_power
& (1 << 10)) &&
519 (qemu_get_clock(vm_clock
) >
520 s
->powerdown
+ TSC_POWEROFF_DELAY
)) << 6);
522 case 0x06: /* Audio Control 3 */
523 val
= s
->audio_ctrl3
| 0x0001;
524 s
->audio_ctrl3
&= 0xff3f;
527 case 0x07: /* LCH_BASS_BOOST_N0 */
528 case 0x08: /* LCH_BASS_BOOST_N1 */
529 case 0x09: /* LCH_BASS_BOOST_N2 */
530 case 0x0a: /* LCH_BASS_BOOST_N3 */
531 case 0x0b: /* LCH_BASS_BOOST_N4 */
532 case 0x0c: /* LCH_BASS_BOOST_N5 */
533 case 0x0d: /* LCH_BASS_BOOST_D1 */
534 case 0x0e: /* LCH_BASS_BOOST_D2 */
535 case 0x0f: /* LCH_BASS_BOOST_D4 */
536 case 0x10: /* LCH_BASS_BOOST_D5 */
537 case 0x11: /* RCH_BASS_BOOST_N0 */
538 case 0x12: /* RCH_BASS_BOOST_N1 */
539 case 0x13: /* RCH_BASS_BOOST_N2 */
540 case 0x14: /* RCH_BASS_BOOST_N3 */
541 case 0x15: /* RCH_BASS_BOOST_N4 */
542 case 0x16: /* RCH_BASS_BOOST_N5 */
543 case 0x17: /* RCH_BASS_BOOST_D1 */
544 case 0x18: /* RCH_BASS_BOOST_D2 */
545 case 0x19: /* RCH_BASS_BOOST_D4 */
546 case 0x1a: /* RCH_BASS_BOOST_D5 */
547 return s
->filter_data
[reg
- 0x07];
549 case 0x1b: /* PLL Programmability 1 */
552 case 0x1c: /* PLL Programmability 2 */
555 case 0x1d: /* Audio Control 4 */
556 return (!s
->softstep
) << 14;
560 fprintf(stderr
, "tsc2102_audio_register_read: "
561 "no such register: 0x%02x\n", reg
);
567 static void tsc2102_data_register_write(
568 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
575 case 0x05: /* BAT1 */
576 case 0x06: /* BAT2 */
577 case 0x07: /* AUX1 */
578 case 0x08: /* AUX2 */
579 case 0x09: /* TEMP1 */
580 case 0x0a: /* TEMP2 */
585 fprintf(stderr
, "tsc2102_data_register_write: "
586 "no such register: 0x%02x\n", reg
);
591 static void tsc2102_control_register_write(
592 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
595 case 0x00: /* TSC ADC */
596 s
->host_mode
= value
>> 15;
597 s
->enabled
= !(value
& 0x4000);
598 if (s
->busy
&& !s
->enabled
)
599 qemu_del_timer(s
->timer
);
600 s
->busy
&= s
->enabled
;
601 s
->nextfunction
= (value
>> 10) & 0xf;
602 s
->nextprecision
= (value
>> 8) & 3;
603 s
->filter
= value
& 0xff;
606 case 0x01: /* Status / Keypad Control */
607 if ((s
->model
& 0xff00) == 0x2100)
608 s
->pin_func
= value
>> 14;
610 s
->kb
.scan
= (value
>> 14) & 1;
611 s
->kb
.debounce
= (value
>> 11) & 7;
612 if (s
->kb
.intr
&& s
->kb
.scan
) {
614 qemu_irq_raise(s
->kbint
);
619 case 0x02: /* DAC Control */
620 if ((s
->model
& 0xff00) == 0x2300) {
621 s
->dac_power
&= 0x7fff;
622 s
->dac_power
|= 0x8000 & value
;
627 case 0x03: /* Reference */
628 s
->ref
= value
& 0x1f;
631 case 0x04: /* Reset */
632 if (value
== 0xbb00) {
634 qemu_del_timer(s
->timer
);
638 fprintf(stderr
, "tsc2102_control_register_write: "
639 "wrong value written into RESET\n");
644 case 0x05: /* Configuration */
645 s
->timing
= value
& 0x3f;
648 fprintf(stderr
, "tsc2102_control_register_write: "
649 "wrong value written into CONFIG\n");
653 case 0x06: /* Secondary configuration */
654 if ((s
->model
& 0xff00) == 0x2100)
656 s
->kb
.mode
= value
>> 14;
657 s
->pll
[2] = value
& 0x3ffff;
660 case 0x10: /* Keypad Mask */
661 if ((s
->model
& 0xff00) == 0x2100)
669 fprintf(stderr
, "tsc2102_control_register_write: "
670 "no such register: 0x%02x\n", reg
);
675 static void tsc2102_audio_register_write(
676 struct tsc210x_state_s
*s
, int reg
, uint16_t value
)
679 case 0x00: /* Audio Control 1 */
680 s
->audio_ctrl1
= value
& 0x0f3f;
682 if ((value
& ~0x0f3f) || ((value
& 7) != ((value
>> 3) & 7)))
683 fprintf(stderr
, "tsc2102_audio_register_write: "
684 "wrong value written into Audio 1\n");
686 tsc2102_audio_rate_update(s
);
688 tsc2102_audio_output_update(s
);
694 fprintf(stderr
, "tsc2102_audio_register_write: "
695 "wrong value written into reg 0x01\n");
699 case 0x02: /* DAC Volume Control */
701 s
->volume_change
= qemu_get_clock(vm_clock
);
707 fprintf(stderr
, "tsc2102_audio_register_write: "
708 "wrong value written into reg 0x03\n");
712 case 0x04: /* Audio Control 2 */
713 s
->audio_ctrl2
= value
& 0xf7f2;
716 fprintf(stderr
, "tsc2102_audio_register_write: "
717 "wrong value written into Audio 2\n");
721 case 0x05: /* Stereo DAC Power Control */
722 if ((value
& ~s
->dac_power
) & (1 << 10))
723 s
->powerdown
= qemu_get_clock(vm_clock
);
725 s
->dac_power
= value
& 0x9543;
727 if ((value
& ~0x9543) != 0x2aa0)
728 fprintf(stderr
, "tsc2102_audio_register_write: "
729 "wrong value written into Power\n");
731 tsc2102_audio_rate_update(s
);
733 tsc2102_audio_output_update(s
);
736 case 0x06: /* Audio Control 3 */
737 s
->audio_ctrl3
&= 0x00c0;
738 s
->audio_ctrl3
|= value
& 0xf800;
741 fprintf(stderr
, "tsc2102_audio_register_write: "
742 "wrong value written into Audio 3\n");
745 tsc2102_audio_output_update(s
);
748 case 0x07: /* LCH_BASS_BOOST_N0 */
749 case 0x08: /* LCH_BASS_BOOST_N1 */
750 case 0x09: /* LCH_BASS_BOOST_N2 */
751 case 0x0a: /* LCH_BASS_BOOST_N3 */
752 case 0x0b: /* LCH_BASS_BOOST_N4 */
753 case 0x0c: /* LCH_BASS_BOOST_N5 */
754 case 0x0d: /* LCH_BASS_BOOST_D1 */
755 case 0x0e: /* LCH_BASS_BOOST_D2 */
756 case 0x0f: /* LCH_BASS_BOOST_D4 */
757 case 0x10: /* LCH_BASS_BOOST_D5 */
758 case 0x11: /* RCH_BASS_BOOST_N0 */
759 case 0x12: /* RCH_BASS_BOOST_N1 */
760 case 0x13: /* RCH_BASS_BOOST_N2 */
761 case 0x14: /* RCH_BASS_BOOST_N3 */
762 case 0x15: /* RCH_BASS_BOOST_N4 */
763 case 0x16: /* RCH_BASS_BOOST_N5 */
764 case 0x17: /* RCH_BASS_BOOST_D1 */
765 case 0x18: /* RCH_BASS_BOOST_D2 */
766 case 0x19: /* RCH_BASS_BOOST_D4 */
767 case 0x1a: /* RCH_BASS_BOOST_D5 */
768 s
->filter_data
[reg
- 0x07] = value
;
771 case 0x1b: /* PLL Programmability 1 */
772 s
->pll
[0] = value
& 0xfffc;
775 fprintf(stderr
, "tsc2102_audio_register_write: "
776 "wrong value written into PLL 1\n");
780 case 0x1c: /* PLL Programmability 2 */
781 s
->pll
[1] = value
& 0xfffc;
784 fprintf(stderr
, "tsc2102_audio_register_write: "
785 "wrong value written into PLL 2\n");
789 case 0x1d: /* Audio Control 4 */
790 s
->softstep
= !(value
& 0x4000);
793 fprintf(stderr
, "tsc2102_audio_register_write: "
794 "wrong value written into Audio 4\n");
800 fprintf(stderr
, "tsc2102_audio_register_write: "
801 "no such register: 0x%02x\n", reg
);
806 /* This handles most of the chip logic. */
807 static void tsc210x_pin_update(struct tsc210x_state_s
*s
)
812 switch (s
->pin_func
) {
814 pin_state
= s
->pressure
;
817 pin_state
= !!s
->dav
;
821 pin_state
= s
->pressure
&& !s
->dav
;
827 if (pin_state
!= s
->irq
) {
829 qemu_set_irq(s
->pint
, !s
->irq
);
832 switch (s
->nextfunction
) {
833 case TSC_MODE_XY_SCAN
:
834 case TSC_MODE_XYZ_SCAN
:
854 case TSC_MODE_AUX_SCAN
:
855 case TSC_MODE_PORT_SCAN
:
858 case TSC_MODE_NO_SCAN
:
859 case TSC_MODE_XX_DRV
:
860 case TSC_MODE_YY_DRV
:
861 case TSC_MODE_YX_DRV
:
866 if (!s
->enabled
|| s
->busy
|| s
->dav
)
870 s
->precision
= s
->nextprecision
;
871 s
->function
= s
->nextfunction
;
872 expires
= qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 10);
873 qemu_mod_timer(s
->timer
, expires
);
876 static uint16_t tsc210x_read(struct tsc210x_state_s
*s
)
878 uint16_t ret
= 0x0000;
881 fprintf(stderr
, "tsc210x_read: SPI underrun!\n");
884 case TSC_DATA_REGISTERS_PAGE
:
885 ret
= tsc2102_data_register_read(s
, s
->offset
);
887 qemu_irq_raise(s
->davint
);
889 case TSC_CONTROL_REGISTERS_PAGE
:
890 ret
= tsc2102_control_register_read(s
, s
->offset
);
892 case TSC_AUDIO_REGISTERS_PAGE
:
893 ret
= tsc2102_audio_register_read(s
, s
->offset
);
896 cpu_abort(cpu_single_env
, "tsc210x_read: wrong memory page\n");
899 tsc210x_pin_update(s
);
901 /* Allow sequential reads. */
907 static void tsc210x_write(struct tsc210x_state_s
*s
, uint16_t value
)
910 * This is a two-state state machine for reading
911 * command and data every second time.
914 s
->command
= value
>> 15;
915 s
->page
= (value
>> 11) & 0x0f;
916 s
->offset
= (value
>> 5) & 0x3f;
920 fprintf(stderr
, "tsc210x_write: SPI overrun!\n");
923 case TSC_DATA_REGISTERS_PAGE
:
924 tsc2102_data_register_write(s
, s
->offset
, value
);
926 case TSC_CONTROL_REGISTERS_PAGE
:
927 tsc2102_control_register_write(s
, s
->offset
, value
);
929 case TSC_AUDIO_REGISTERS_PAGE
:
930 tsc2102_audio_register_write(s
, s
->offset
, value
);
933 cpu_abort(cpu_single_env
,
934 "tsc210x_write: wrong memory page\n");
937 tsc210x_pin_update(s
);
942 uint32_t tsc210x_txrx(void *opaque
, uint32_t value
, int len
)
944 struct tsc210x_state_s
*s
= opaque
;
948 cpu_abort(cpu_single_env
, "%s: FIXME: bad SPI word width %i\n",
951 /* TODO: sequential reads etc - how do we make sure the host doesn't
952 * unintentionally read out a conversion result from a register while
953 * transmitting the command word of the next command? */
954 if (!value
|| (s
->state
&& s
->command
))
955 ret
= tsc210x_read(s
);
956 if (value
|| (s
->state
&& !s
->command
))
957 tsc210x_write(s
, value
);
962 static void tsc210x_timer_tick(void *opaque
)
964 struct tsc210x_state_s
*s
= opaque
;
966 /* Timer ticked -- a set of conversions has been finished. */
972 s
->dav
|= mode_regs
[s
->function
];
973 tsc210x_pin_update(s
);
974 qemu_irq_lower(s
->davint
);
977 static void tsc210x_touchscreen_event(void *opaque
,
978 int x
, int y
, int z
, int buttons_state
)
980 struct tsc210x_state_s
*s
= opaque
;
987 s
->pressure
= !!buttons_state
;
990 * Note: We would get better responsiveness in the guest by
991 * signaling TS events immediately, but for now we simulate
992 * the first conversion delay for sake of correctness.
994 if (p
!= s
->pressure
)
995 tsc210x_pin_update(s
);
998 static void tsc210x_i2s_swallow(struct tsc210x_state_s
*s
)
1000 if (s
->dac_voice
[0])
1001 tsc210x_out_flush(s
, s
->codec
.out
.len
);
1003 s
->codec
.out
.len
= 0;
1006 static void tsc210x_i2s_set_rate(struct tsc210x_state_s
*s
, int in
, int out
)
1008 s
->i2s_tx_rate
= out
;
1009 s
->i2s_rx_rate
= in
;
1012 static void tsc210x_save(QEMUFile
*f
, void *opaque
)
1014 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) opaque
;
1015 int64_t now
= qemu_get_clock(vm_clock
);
1018 qemu_put_be16(f
, s
->x
);
1019 qemu_put_be16(f
, s
->y
);
1020 qemu_put_byte(f
, s
->pressure
);
1022 qemu_put_byte(f
, s
->state
);
1023 qemu_put_byte(f
, s
->page
);
1024 qemu_put_byte(f
, s
->offset
);
1025 qemu_put_byte(f
, s
->command
);
1027 qemu_put_byte(f
, s
->irq
);
1028 qemu_put_be16s(f
, &s
->dav
);
1030 qemu_put_timer(f
, s
->timer
);
1031 qemu_put_byte(f
, s
->enabled
);
1032 qemu_put_byte(f
, s
->host_mode
);
1033 qemu_put_byte(f
, s
->function
);
1034 qemu_put_byte(f
, s
->nextfunction
);
1035 qemu_put_byte(f
, s
->precision
);
1036 qemu_put_byte(f
, s
->nextprecision
);
1037 qemu_put_byte(f
, s
->filter
);
1038 qemu_put_byte(f
, s
->pin_func
);
1039 qemu_put_byte(f
, s
->ref
);
1040 qemu_put_byte(f
, s
->timing
);
1041 qemu_put_be32(f
, s
->noise
);
1043 qemu_put_be16s(f
, &s
->audio_ctrl1
);
1044 qemu_put_be16s(f
, &s
->audio_ctrl2
);
1045 qemu_put_be16s(f
, &s
->audio_ctrl3
);
1046 qemu_put_be16s(f
, &s
->pll
[0]);
1047 qemu_put_be16s(f
, &s
->pll
[1]);
1048 qemu_put_be16s(f
, &s
->volume
);
1049 qemu_put_be64(f
, (uint64_t) (s
->volume_change
- now
));
1050 qemu_put_be64(f
, (uint64_t) (s
->powerdown
- now
));
1051 qemu_put_byte(f
, s
->softstep
);
1052 qemu_put_be16s(f
, &s
->dac_power
);
1054 for (i
= 0; i
< 0x14; i
++)
1055 qemu_put_be16s(f
, &s
->filter_data
[i
]);
1058 static int tsc210x_load(QEMUFile
*f
, void *opaque
, int version_id
)
1060 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) opaque
;
1061 int64_t now
= qemu_get_clock(vm_clock
);
1064 s
->x
= qemu_get_be16(f
);
1065 s
->y
= qemu_get_be16(f
);
1066 s
->pressure
= qemu_get_byte(f
);
1068 s
->state
= qemu_get_byte(f
);
1069 s
->page
= qemu_get_byte(f
);
1070 s
->offset
= qemu_get_byte(f
);
1071 s
->command
= qemu_get_byte(f
);
1073 s
->irq
= qemu_get_byte(f
);
1074 qemu_get_be16s(f
, &s
->dav
);
1076 qemu_get_timer(f
, s
->timer
);
1077 s
->enabled
= qemu_get_byte(f
);
1078 s
->host_mode
= qemu_get_byte(f
);
1079 s
->function
= qemu_get_byte(f
);
1080 s
->nextfunction
= qemu_get_byte(f
);
1081 s
->precision
= qemu_get_byte(f
);
1082 s
->nextprecision
= qemu_get_byte(f
);
1083 s
->filter
= qemu_get_byte(f
);
1084 s
->pin_func
= qemu_get_byte(f
);
1085 s
->ref
= qemu_get_byte(f
);
1086 s
->timing
= qemu_get_byte(f
);
1087 s
->noise
= qemu_get_be32(f
);
1089 qemu_get_be16s(f
, &s
->audio_ctrl1
);
1090 qemu_get_be16s(f
, &s
->audio_ctrl2
);
1091 qemu_get_be16s(f
, &s
->audio_ctrl3
);
1092 qemu_get_be16s(f
, &s
->pll
[0]);
1093 qemu_get_be16s(f
, &s
->pll
[1]);
1094 qemu_get_be16s(f
, &s
->volume
);
1095 s
->volume_change
= (int64_t) qemu_get_be64(f
) + now
;
1096 s
->powerdown
= (int64_t) qemu_get_be64(f
) + now
;
1097 s
->softstep
= qemu_get_byte(f
);
1098 qemu_get_be16s(f
, &s
->dac_power
);
1100 for (i
= 0; i
< 0x14; i
++)
1101 qemu_get_be16s(f
, &s
->filter_data
[i
]);
1103 s
->busy
= qemu_timer_pending(s
->timer
);
1104 qemu_set_irq(s
->pint
, !s
->irq
);
1105 qemu_set_irq(s
->davint
, !s
->dav
);
1110 static int tsc2102_iid
= 0;
1112 struct uwire_slave_s
*tsc2102_init(qemu_irq pint
, AudioState
*audio
)
1114 struct tsc210x_state_s
*s
;
1116 s
= (struct tsc210x_state_s
*)
1117 qemu_mallocz(sizeof(struct tsc210x_state_s
));
1118 memset(s
, 0, sizeof(struct tsc210x_state_s
));
1122 s
->precision
= s
->nextprecision
= 0;
1123 s
->timer
= qemu_new_timer(vm_clock
, tsc210x_timer_tick
, s
);
1126 s
->name
= "tsc2102";
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 TSC2102-driven Touchscreen");
1154 AUD_register_card(s
->audio
, s
->name
, &s
->card
);
1156 qemu_register_reset((void *) tsc210x_reset
, s
);
1157 register_savevm(s
->name
, tsc2102_iid
++, 0,
1158 tsc210x_save
, tsc210x_load
, s
);
1163 struct uwire_slave_s
*tsc2301_init(qemu_irq penirq
, qemu_irq kbirq
,
1164 qemu_irq dav
, AudioState
*audio
)
1166 struct tsc210x_state_s
*s
;
1168 s
= (struct tsc210x_state_s
*)
1169 qemu_mallocz(sizeof(struct tsc210x_state_s
));
1170 memset(s
, 0, sizeof(struct tsc210x_state_s
));
1174 s
->precision
= s
->nextprecision
= 0;
1175 s
->timer
= qemu_new_timer(vm_clock
, tsc210x_timer_tick
, s
);
1180 s
->name
= "tsc2301";
1193 s
->chip
.send
= (void *) tsc210x_write
;
1194 s
->chip
.receive
= (void *) tsc210x_read
;
1196 s
->codec
.opaque
= s
;
1197 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1198 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1199 s
->codec
.in
.fifo
= s
->in_fifo
;
1200 s
->codec
.out
.fifo
= s
->out_fifo
;
1204 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1205 "QEMU TSC2301-driven Touchscreen");
1208 AUD_register_card(s
->audio
, s
->name
, &s
->card
);
1210 qemu_register_reset((void *) tsc210x_reset
, s
);
1211 register_savevm(s
->name
, tsc2102_iid
++, 0,
1212 tsc210x_save
, tsc210x_load
, s
);
1217 struct i2s_codec_s
*tsc210x_codec(struct uwire_slave_s
*chip
)
1219 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1225 * Use tslib generated calibration data to generate ADC input values
1226 * from the touchscreen. Assuming 12-bit precision was used during
1227 * tslib calibration.
1229 void tsc210x_set_transform(struct uwire_slave_s
*chip
,
1230 struct mouse_transform_info_s
*info
)
1232 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1236 ltr
[0] = (int64_t) info
->a
[1] * info
->y
;
1237 ltr
[1] = (int64_t) info
->a
[4] * info
->x
;
1238 ltr
[2] = (int64_t) info
->a
[1] * info
->a
[3] -
1239 (int64_t) info
->a
[4] * info
->a
[0];
1240 ltr
[3] = (int64_t) info
->a
[2] * info
->a
[4] -
1241 (int64_t) info
->a
[5] * info
->a
[1];
1242 ltr
[4] = (int64_t) info
->a
[0] * info
->y
;
1243 ltr
[5] = (int64_t) info
->a
[3] * info
->x
;
1244 ltr
[6] = (int64_t) info
->a
[4] * info
->a
[0] -
1245 (int64_t) info
->a
[1] * info
->a
[3];
1246 ltr
[7] = (int64_t) info
->a
[2] * info
->a
[3] -
1247 (int64_t) info
->a
[5] * info
->a
[0];
1249 /* Avoid integer overflow */
1250 s
->tr
[0] = ltr
[0] >> 11;
1251 s
->tr
[1] = ltr
[1] >> 11;
1252 s
->tr
[2] = muldiv64(ltr
[2], 1, info
->a
[6]);
1253 s
->tr
[3] = muldiv64(ltr
[3], 1 << 4, ltr
[2]);
1254 s
->tr
[4] = ltr
[4] >> 11;
1255 s
->tr
[5] = ltr
[5] >> 11;
1256 s
->tr
[6] = muldiv64(ltr
[6], 1, info
->a
[6]);
1257 s
->tr
[7] = muldiv64(ltr
[7], 1 << 4, ltr
[6]);
1260 /* This version assumes touchscreen X & Y axis are parallel or
1261 * perpendicular to LCD's X & Y axis in some way. */
1262 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
1264 s
->tr
[1] = -info
->a
[6] * info
->x
;
1265 s
->tr
[2] = info
->a
[0];
1266 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
1267 s
->tr
[4] = info
->a
[6] * info
->y
;
1269 s
->tr
[6] = info
->a
[4];
1270 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
1272 s
->tr
[0] = info
->a
[6] * info
->y
;
1274 s
->tr
[2] = info
->a
[1];
1275 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
1277 s
->tr
[5] = -info
->a
[6] * info
->x
;
1278 s
->tr
[6] = info
->a
[3];
1279 s
->tr
[7] = -info
->a
[5] / info
->a
[3];
1291 void tsc210x_key_event(struct uwire_slave_s
*chip
, int key
, int down
)
1293 struct tsc210x_state_s
*s
= (struct tsc210x_state_s
*) chip
->opaque
;
1296 s
->kb
.down
|= 1 << key
;
1298 s
->kb
.down
&= ~(1 << key
);
1300 if (down
&& (s
->kb
.down
& ~s
->kb
.mask
) && !s
->kb
.intr
) {
1302 qemu_irq_lower(s
->kbint
);
1303 } else if (s
->kb
.intr
&& !(s
->kb
.down
& ~s
->kb
.mask
) &&
1304 !(s
->kb
.mode
& 1)) {
1306 qemu_irq_raise(s
->kbint
);