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"
31 #include "migration/vmstate.h"
33 #define TSC_DATA_REGISTERS_PAGE 0x0
34 #define TSC_CONTROL_REGISTERS_PAGE 0x1
35 #define TSC_AUDIO_REGISTERS_PAGE 0x2
39 #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - resolution[p]))
49 uint8_t in_fifo
[16384];
50 uint8_t out_fifo
[16384];
65 uint8_t function
, nextfunction
;
66 uint8_t precision
, nextprecision
;
78 int64_t volume_change
;
82 uint16_t filter_data
[0x14];
85 SWVoiceIn
*adc_voice
[1];
86 SWVoiceOut
*dac_voice
[1];
100 int64_t now
; /* Time at migration */
103 static const int resolution
[4] = { 12, 8, 10, 12 };
105 #define TSC_MODE_NO_SCAN 0x0
106 #define TSC_MODE_XY_SCAN 0x1
107 #define TSC_MODE_XYZ_SCAN 0x2
108 #define TSC_MODE_X 0x3
109 #define TSC_MODE_Y 0x4
110 #define TSC_MODE_Z 0x5
111 #define TSC_MODE_BAT1 0x6
112 #define TSC_MODE_BAT2 0x7
113 #define TSC_MODE_AUX 0x8
114 #define TSC_MODE_AUX_SCAN 0x9
115 #define TSC_MODE_TEMP1 0xa
116 #define TSC_MODE_PORT_SCAN 0xb
117 #define TSC_MODE_TEMP2 0xc
118 #define TSC_MODE_XX_DRV 0xd
119 #define TSC_MODE_YY_DRV 0xe
120 #define TSC_MODE_YX_DRV 0xf
122 static const uint16_t mode_regs
[16] = {
123 0x0000, /* No scan */
124 0x0600, /* X, Y scan */
125 0x0780, /* X, Y, Z scan */
132 0x0010, /* AUX scan */
134 0x0070, /* Port scan */
136 0x0000, /* X+, X- drivers */
137 0x0000, /* Y+, Y- drivers */
138 0x0000, /* Y+, X- drivers */
141 #define X_TRANSFORM(s) \
142 ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
143 #define Y_TRANSFORM(s) \
144 ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
145 #define Z1_TRANSFORM(s) \
146 ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
147 #define Z2_TRANSFORM(s) \
148 ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
150 #define BAT1_VAL 0x8660
151 #define BAT2_VAL 0x0000
152 #define AUX1_VAL 0x35c0
153 #define AUX2_VAL 0xffff
154 #define TEMP1_VAL 0x8c70
155 #define TEMP2_VAL 0xa5b0
157 #define TSC_POWEROFF_DELAY 50
158 #define TSC_SOFTSTEP_DELAY 50
160 static void tsc210x_reset(TSC210xState
*s
)
172 s
->audio_ctrl1
= 0x0000;
173 s
->audio_ctrl2
= 0x4410;
174 s
->audio_ctrl3
= 0x0000;
179 s
->dac_power
= 0x8540;
181 s
->volume_change
= 0;
183 s
->filter_data
[0x00] = 0x6be3;
184 s
->filter_data
[0x01] = 0x9666;
185 s
->filter_data
[0x02] = 0x675d;
186 s
->filter_data
[0x03] = 0x6be3;
187 s
->filter_data
[0x04] = 0x9666;
188 s
->filter_data
[0x05] = 0x675d;
189 s
->filter_data
[0x06] = 0x7d83;
190 s
->filter_data
[0x07] = 0x84ee;
191 s
->filter_data
[0x08] = 0x7d83;
192 s
->filter_data
[0x09] = 0x84ee;
193 s
->filter_data
[0x0a] = 0x6be3;
194 s
->filter_data
[0x0b] = 0x9666;
195 s
->filter_data
[0x0c] = 0x675d;
196 s
->filter_data
[0x0d] = 0x6be3;
197 s
->filter_data
[0x0e] = 0x9666;
198 s
->filter_data
[0x0f] = 0x675d;
199 s
->filter_data
[0x10] = 0x7d83;
200 s
->filter_data
[0x11] = 0x84ee;
201 s
->filter_data
[0x12] = 0x7d83;
202 s
->filter_data
[0x13] = 0x84ee;
213 qemu_set_irq(s
->pint
, !s
->irq
);
214 qemu_set_irq(s
->davint
, !s
->dav
);
215 qemu_irq_raise(s
->kbint
);
224 /* { rate, dsor, fsref } */
225 static const TSC210xRateInfo tsc2102_rates
[] = {
254 static inline void tsc210x_out_flush(TSC210xState
*s
, int len
)
256 uint8_t *data
= s
->codec
.out
.fifo
+ s
->codec
.out
.start
;
257 uint8_t *end
= data
+ len
;
260 data
+= AUD_write(s
->dac_voice
[0], data
, end
- data
) ?: (end
- data
);
262 s
->codec
.out
.len
-= len
;
263 if (s
->codec
.out
.len
)
264 memmove(s
->codec
.out
.fifo
, end
, s
->codec
.out
.len
);
265 s
->codec
.out
.start
= 0;
268 static void tsc210x_audio_out_cb(TSC210xState
*s
, int free_b
)
270 if (s
->codec
.out
.len
>= free_b
) {
271 tsc210x_out_flush(s
, free_b
);
275 s
->codec
.out
.size
= MIN(free_b
, 16384);
276 qemu_irq_raise(s
->codec
.tx_start
);
279 static void tsc2102_audio_rate_update(TSC210xState
*s
)
281 const TSC210xRateInfo
*rate
;
283 s
->codec
.tx_rate
= 0;
284 s
->codec
.rx_rate
= 0;
285 if (s
->dac_power
& (1 << 15)) /* PWDNC */
288 for (rate
= tsc2102_rates
; rate
->rate
; rate
++)
289 if (rate
->dsor
== (s
->audio_ctrl1
& 0x3f) && /* DACFS */
290 rate
->fsref
== ((s
->audio_ctrl3
>> 13) & 1))/* REFFS */
293 printf("%s: unknown sampling rate configured\n", __func__
);
297 s
->codec
.tx_rate
= rate
->rate
;
300 static void tsc2102_audio_output_update(TSC210xState
*s
)
303 struct audsettings fmt
;
305 if (s
->dac_voice
[0]) {
306 tsc210x_out_flush(s
, s
->codec
.out
.len
);
307 s
->codec
.out
.size
= 0;
308 AUD_set_active_out(s
->dac_voice
[0], 0);
309 AUD_close_out(&s
->card
, s
->dac_voice
[0]);
310 s
->dac_voice
[0] = NULL
;
315 (~s
->dac_power
& (1 << 15)) && /* PWDNC */
316 (~s
->dac_power
& (1 << 10)); /* DAPWDN */
317 if (!enable
|| !s
->codec
.tx_rate
)
320 /* Force our own sampling rate even in slave DAC mode */
323 fmt
.freq
= s
->codec
.tx_rate
;
324 fmt
.fmt
= AUDIO_FORMAT_S16
;
326 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
327 "tsc2102.sink", s
, (void *) tsc210x_audio_out_cb
, &fmt
);
328 if (s
->dac_voice
[0]) {
330 AUD_set_active_out(s
->dac_voice
[0], 1);
334 static uint16_t tsc2102_data_register_read(TSC210xState
*s
, int reg
)
339 return TSC_CUT_RESOLUTION(X_TRANSFORM(s
), s
->precision
) +
345 return TSC_CUT_RESOLUTION(Y_TRANSFORM(s
), s
->precision
) ^
350 return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s
), s
->precision
) -
355 return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s
), s
->precision
) |
358 case 0x04: /* KPData */
359 if ((s
->model
& 0xff00) == 0x2300) {
360 if (s
->kb
.intr
&& (s
->kb
.mode
& 2)) {
362 qemu_irq_raise(s
->kbint
);
369 case 0x05: /* BAT1 */
371 return TSC_CUT_RESOLUTION(BAT1_VAL
, s
->precision
) +
374 case 0x06: /* BAT2 */
376 return TSC_CUT_RESOLUTION(BAT2_VAL
, s
->precision
);
378 case 0x07: /* AUX1 */
380 return TSC_CUT_RESOLUTION(AUX1_VAL
, s
->precision
);
382 case 0x08: /* AUX2 */
386 case 0x09: /* TEMP1 */
388 return TSC_CUT_RESOLUTION(TEMP1_VAL
, s
->precision
) -
391 case 0x0a: /* TEMP2 */
393 return TSC_CUT_RESOLUTION(TEMP2_VAL
, s
->precision
) ^
402 fprintf(stderr
, "tsc2102_data_register_read: "
403 "no such register: 0x%02x\n", reg
);
409 static uint16_t tsc2102_control_register_read(
410 TSC210xState
*s
, int reg
)
413 case 0x00: /* TSC ADC */
414 return (s
->pressure
<< 15) | ((!s
->busy
) << 14) |
415 (s
->nextfunction
<< 10) | (s
->nextprecision
<< 8) | s
->filter
;
417 case 0x01: /* Status / Keypad Control */
418 if ((s
->model
& 0xff00) == 0x2100)
419 return (s
->pin_func
<< 14) | ((!s
->enabled
) << 13) |
420 (s
->host_mode
<< 12) | ((!!s
->dav
) << 11) | s
->dav
;
422 return (s
->kb
.intr
<< 15) | ((s
->kb
.scan
|| !s
->kb
.down
) << 14) |
423 (s
->kb
.debounce
<< 11);
425 case 0x02: /* DAC Control */
426 if ((s
->model
& 0xff00) == 0x2300)
427 return s
->dac_power
& 0x8000;
431 case 0x03: /* Reference */
434 case 0x04: /* Reset */
437 case 0x05: /* Configuration */
440 case 0x06: /* Secondary configuration */
441 if ((s
->model
& 0xff00) == 0x2100)
443 return ((!s
->dav
) << 15) | ((s
->kb
.mode
& 1) << 14) | s
->pll
[2];
445 case 0x10: /* Keypad Mask */
446 if ((s
->model
& 0xff00) == 0x2100)
453 fprintf(stderr
, "tsc2102_control_register_read: "
454 "no such register: 0x%02x\n", reg
);
460 static uint16_t tsc2102_audio_register_read(TSC210xState
*s
, int reg
)
466 case 0x00: /* Audio Control 1 */
467 return s
->audio_ctrl1
;
472 case 0x02: /* DAC Volume Control */
478 case 0x04: /* Audio Control 2 */
481 if (s
->softstep
&& !(s
->dac_power
& (1 << 10))) {
482 l_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
483 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
484 r_ch
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
485 s
->volume_change
+ TSC_SOFTSTEP_DELAY
);
488 return s
->audio_ctrl2
| (l_ch
<< 3) | (r_ch
<< 2);
490 case 0x05: /* Stereo DAC Power Control */
491 return 0x2aa0 | s
->dac_power
|
492 (((s
->dac_power
& (1 << 10)) &&
493 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) >
494 s
->powerdown
+ TSC_POWEROFF_DELAY
)) << 6);
496 case 0x06: /* Audio Control 3 */
497 val
= s
->audio_ctrl3
| 0x0001;
498 s
->audio_ctrl3
&= 0xff3f;
501 case 0x07: /* LCH_BASS_BOOST_N0 */
502 case 0x08: /* LCH_BASS_BOOST_N1 */
503 case 0x09: /* LCH_BASS_BOOST_N2 */
504 case 0x0a: /* LCH_BASS_BOOST_N3 */
505 case 0x0b: /* LCH_BASS_BOOST_N4 */
506 case 0x0c: /* LCH_BASS_BOOST_N5 */
507 case 0x0d: /* LCH_BASS_BOOST_D1 */
508 case 0x0e: /* LCH_BASS_BOOST_D2 */
509 case 0x0f: /* LCH_BASS_BOOST_D4 */
510 case 0x10: /* LCH_BASS_BOOST_D5 */
511 case 0x11: /* RCH_BASS_BOOST_N0 */
512 case 0x12: /* RCH_BASS_BOOST_N1 */
513 case 0x13: /* RCH_BASS_BOOST_N2 */
514 case 0x14: /* RCH_BASS_BOOST_N3 */
515 case 0x15: /* RCH_BASS_BOOST_N4 */
516 case 0x16: /* RCH_BASS_BOOST_N5 */
517 case 0x17: /* RCH_BASS_BOOST_D1 */
518 case 0x18: /* RCH_BASS_BOOST_D2 */
519 case 0x19: /* RCH_BASS_BOOST_D4 */
520 case 0x1a: /* RCH_BASS_BOOST_D5 */
521 return s
->filter_data
[reg
- 0x07];
523 case 0x1b: /* PLL Programmability 1 */
526 case 0x1c: /* PLL Programmability 2 */
529 case 0x1d: /* Audio Control 4 */
530 return (!s
->softstep
) << 14;
534 fprintf(stderr
, "tsc2102_audio_register_read: "
535 "no such register: 0x%02x\n", reg
);
541 static void tsc2102_data_register_write(
542 TSC210xState
*s
, int reg
, uint16_t value
)
549 case 0x05: /* BAT1 */
550 case 0x06: /* BAT2 */
551 case 0x07: /* AUX1 */
552 case 0x08: /* AUX2 */
553 case 0x09: /* TEMP1 */
554 case 0x0a: /* TEMP2 */
558 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_data_register_write: "
559 "no such register: 0x%02x\n", reg
);
563 static void tsc2102_control_register_write(
564 TSC210xState
*s
, int reg
, uint16_t value
)
567 case 0x00: /* TSC ADC */
568 s
->host_mode
= value
>> 15;
569 s
->enabled
= !(value
& 0x4000);
570 if (s
->busy
&& !s
->enabled
)
572 s
->busy
= s
->busy
&& s
->enabled
;
573 s
->nextfunction
= (value
>> 10) & 0xf;
574 s
->nextprecision
= (value
>> 8) & 3;
575 s
->filter
= value
& 0xff;
578 case 0x01: /* Status / Keypad Control */
579 if ((s
->model
& 0xff00) == 0x2100)
580 s
->pin_func
= value
>> 14;
582 s
->kb
.scan
= (value
>> 14) & 1;
583 s
->kb
.debounce
= (value
>> 11) & 7;
584 if (s
->kb
.intr
&& s
->kb
.scan
) {
586 qemu_irq_raise(s
->kbint
);
591 case 0x02: /* DAC Control */
592 if ((s
->model
& 0xff00) == 0x2300) {
593 s
->dac_power
&= 0x7fff;
594 s
->dac_power
|= 0x8000 & value
;
599 case 0x03: /* Reference */
600 s
->ref
= value
& 0x1f;
603 case 0x04: /* Reset */
604 if (value
== 0xbb00) {
610 fprintf(stderr
, "tsc2102_control_register_write: "
611 "wrong value written into RESET\n");
616 case 0x05: /* Configuration */
617 s
->timing
= value
& 0x3f;
620 fprintf(stderr
, "tsc2102_control_register_write: "
621 "wrong value written into CONFIG\n");
625 case 0x06: /* Secondary configuration */
626 if ((s
->model
& 0xff00) == 0x2100)
628 s
->kb
.mode
= value
>> 14;
629 s
->pll
[2] = value
& 0x3ffff;
632 case 0x10: /* Keypad Mask */
633 if ((s
->model
& 0xff00) == 0x2100)
640 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_control_register_write: "
641 "no such register: 0x%02x\n", reg
);
645 static void tsc2102_audio_register_write(
646 TSC210xState
*s
, int reg
, uint16_t value
)
649 case 0x00: /* Audio Control 1 */
650 s
->audio_ctrl1
= value
& 0x0f3f;
652 if ((value
& ~0x0f3f) || ((value
& 7) != ((value
>> 3) & 7)))
653 fprintf(stderr
, "tsc2102_audio_register_write: "
654 "wrong value written into Audio 1\n");
656 tsc2102_audio_rate_update(s
);
657 tsc2102_audio_output_update(s
);
663 fprintf(stderr
, "tsc2102_audio_register_write: "
664 "wrong value written into reg 0x01\n");
668 case 0x02: /* DAC Volume Control */
670 s
->volume_change
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
676 fprintf(stderr
, "tsc2102_audio_register_write: "
677 "wrong value written into reg 0x03\n");
681 case 0x04: /* Audio Control 2 */
682 s
->audio_ctrl2
= value
& 0xf7f2;
685 fprintf(stderr
, "tsc2102_audio_register_write: "
686 "wrong value written into Audio 2\n");
690 case 0x05: /* Stereo DAC Power Control */
691 if ((value
& ~s
->dac_power
) & (1 << 10))
692 s
->powerdown
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
694 s
->dac_power
= value
& 0x9543;
696 if ((value
& ~0x9543) != 0x2aa0)
697 fprintf(stderr
, "tsc2102_audio_register_write: "
698 "wrong value written into Power\n");
700 tsc2102_audio_rate_update(s
);
701 tsc2102_audio_output_update(s
);
704 case 0x06: /* Audio Control 3 */
705 s
->audio_ctrl3
&= 0x00c0;
706 s
->audio_ctrl3
|= value
& 0xf800;
709 fprintf(stderr
, "tsc2102_audio_register_write: "
710 "wrong value written into Audio 3\n");
712 tsc2102_audio_output_update(s
);
715 case 0x07: /* LCH_BASS_BOOST_N0 */
716 case 0x08: /* LCH_BASS_BOOST_N1 */
717 case 0x09: /* LCH_BASS_BOOST_N2 */
718 case 0x0a: /* LCH_BASS_BOOST_N3 */
719 case 0x0b: /* LCH_BASS_BOOST_N4 */
720 case 0x0c: /* LCH_BASS_BOOST_N5 */
721 case 0x0d: /* LCH_BASS_BOOST_D1 */
722 case 0x0e: /* LCH_BASS_BOOST_D2 */
723 case 0x0f: /* LCH_BASS_BOOST_D4 */
724 case 0x10: /* LCH_BASS_BOOST_D5 */
725 case 0x11: /* RCH_BASS_BOOST_N0 */
726 case 0x12: /* RCH_BASS_BOOST_N1 */
727 case 0x13: /* RCH_BASS_BOOST_N2 */
728 case 0x14: /* RCH_BASS_BOOST_N3 */
729 case 0x15: /* RCH_BASS_BOOST_N4 */
730 case 0x16: /* RCH_BASS_BOOST_N5 */
731 case 0x17: /* RCH_BASS_BOOST_D1 */
732 case 0x18: /* RCH_BASS_BOOST_D2 */
733 case 0x19: /* RCH_BASS_BOOST_D4 */
734 case 0x1a: /* RCH_BASS_BOOST_D5 */
735 s
->filter_data
[reg
- 0x07] = value
;
738 case 0x1b: /* PLL Programmability 1 */
739 s
->pll
[0] = value
& 0xfffc;
742 fprintf(stderr
, "tsc2102_audio_register_write: "
743 "wrong value written into PLL 1\n");
747 case 0x1c: /* PLL Programmability 2 */
748 s
->pll
[1] = value
& 0xfffc;
751 fprintf(stderr
, "tsc2102_audio_register_write: "
752 "wrong value written into PLL 2\n");
756 case 0x1d: /* Audio Control 4 */
757 s
->softstep
= !(value
& 0x4000);
760 fprintf(stderr
, "tsc2102_audio_register_write: "
761 "wrong value written into Audio 4\n");
766 qemu_log_mask(LOG_GUEST_ERROR
, "tsc2102_audio_register_write: "
767 "no such register: 0x%02x\n", reg
);
771 /* This handles most of the chip logic. */
772 static void tsc210x_pin_update(TSC210xState
*s
)
777 switch (s
->pin_func
) {
779 pin_state
= s
->pressure
;
782 pin_state
= !!s
->dav
;
786 pin_state
= s
->pressure
&& !s
->dav
;
792 if (pin_state
!= s
->irq
) {
794 qemu_set_irq(s
->pint
, !s
->irq
);
797 switch (s
->nextfunction
) {
798 case TSC_MODE_XY_SCAN
:
799 case TSC_MODE_XYZ_SCAN
:
819 case TSC_MODE_AUX_SCAN
:
820 case TSC_MODE_PORT_SCAN
:
823 case TSC_MODE_NO_SCAN
:
824 case TSC_MODE_XX_DRV
:
825 case TSC_MODE_YY_DRV
:
826 case TSC_MODE_YX_DRV
:
831 if (!s
->enabled
|| s
->busy
|| s
->dav
)
835 s
->precision
= s
->nextprecision
;
836 s
->function
= s
->nextfunction
;
837 expires
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
838 (NANOSECONDS_PER_SECOND
>> 10);
839 timer_mod(s
->timer
, expires
);
842 static uint16_t tsc210x_read(TSC210xState
*s
)
844 uint16_t ret
= 0x0000;
847 fprintf(stderr
, "tsc210x_read: SPI underrun!\n");
850 case TSC_DATA_REGISTERS_PAGE
:
851 ret
= tsc2102_data_register_read(s
, s
->offset
);
853 qemu_irq_raise(s
->davint
);
855 case TSC_CONTROL_REGISTERS_PAGE
:
856 ret
= tsc2102_control_register_read(s
, s
->offset
);
858 case TSC_AUDIO_REGISTERS_PAGE
:
859 ret
= tsc2102_audio_register_read(s
, s
->offset
);
862 hw_error("tsc210x_read: wrong memory page\n");
865 tsc210x_pin_update(s
);
867 /* Allow sequential reads. */
873 static void tsc210x_write(TSC210xState
*s
, uint16_t value
)
876 * This is a two-state state machine for reading
877 * command and data every second time.
880 s
->command
= (value
>> 15) != 0;
881 s
->page
= (value
>> 11) & 0x0f;
882 s
->offset
= (value
>> 5) & 0x3f;
886 fprintf(stderr
, "tsc210x_write: SPI overrun!\n");
889 case TSC_DATA_REGISTERS_PAGE
:
890 tsc2102_data_register_write(s
, s
->offset
, value
);
892 case TSC_CONTROL_REGISTERS_PAGE
:
893 tsc2102_control_register_write(s
, s
->offset
, value
);
895 case TSC_AUDIO_REGISTERS_PAGE
:
896 tsc2102_audio_register_write(s
, s
->offset
, value
);
899 hw_error("tsc210x_write: wrong memory page\n");
902 tsc210x_pin_update(s
);
907 uint32_t tsc210x_txrx(void *opaque
, uint32_t value
, int len
)
909 TSC210xState
*s
= opaque
;
913 hw_error("%s: FIXME: bad SPI word width %i\n", __func__
, len
);
915 /* TODO: sequential reads etc - how do we make sure the host doesn't
916 * unintentionally read out a conversion result from a register while
917 * transmitting the command word of the next command? */
918 if (!value
|| (s
->state
&& s
->command
))
919 ret
= tsc210x_read(s
);
920 if (value
|| (s
->state
&& !s
->command
))
921 tsc210x_write(s
, value
);
926 static void tsc210x_timer_tick(void *opaque
)
928 TSC210xState
*s
= opaque
;
930 /* Timer ticked -- a set of conversions has been finished. */
936 s
->dav
|= mode_regs
[s
->function
];
937 tsc210x_pin_update(s
);
938 qemu_irq_lower(s
->davint
);
941 static void tsc210x_touchscreen_event(void *opaque
,
942 int x
, int y
, int z
, int buttons_state
)
944 TSC210xState
*s
= opaque
;
951 s
->pressure
= !!buttons_state
;
954 * Note: We would get better responsiveness in the guest by
955 * signaling TS events immediately, but for now we simulate
956 * the first conversion delay for sake of correctness.
958 if (p
!= s
->pressure
)
959 tsc210x_pin_update(s
);
962 static void tsc210x_i2s_swallow(TSC210xState
*s
)
965 tsc210x_out_flush(s
, s
->codec
.out
.len
);
967 s
->codec
.out
.len
= 0;
970 static void tsc210x_i2s_set_rate(TSC210xState
*s
, int in
, int out
)
972 s
->i2s_tx_rate
= out
;
976 static int tsc210x_pre_save(void *opaque
)
978 TSC210xState
*s
= (TSC210xState
*) opaque
;
979 s
->now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
984 static int tsc210x_post_load(void *opaque
, int version_id
)
986 TSC210xState
*s
= (TSC210xState
*) opaque
;
987 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
989 if (s
->function
>= ARRAY_SIZE(mode_regs
)) {
992 if (s
->nextfunction
>= ARRAY_SIZE(mode_regs
)) {
995 if (s
->precision
>= ARRAY_SIZE(resolution
)) {
998 if (s
->nextprecision
>= ARRAY_SIZE(resolution
)) {
1002 s
->volume_change
-= s
->now
;
1003 s
->volume_change
+= now
;
1004 s
->powerdown
-= s
->now
;
1005 s
->powerdown
+= now
;
1007 s
->busy
= timer_pending(s
->timer
);
1008 qemu_set_irq(s
->pint
, !s
->irq
);
1009 qemu_set_irq(s
->davint
, !s
->dav
);
1014 static VMStateField vmstatefields_tsc210x
[] = {
1015 VMSTATE_BOOL(enabled
, TSC210xState
),
1016 VMSTATE_BOOL(host_mode
, TSC210xState
),
1017 VMSTATE_BOOL(irq
, TSC210xState
),
1018 VMSTATE_BOOL(command
, TSC210xState
),
1019 VMSTATE_BOOL(pressure
, TSC210xState
),
1020 VMSTATE_BOOL(softstep
, TSC210xState
),
1021 VMSTATE_BOOL(state
, TSC210xState
),
1022 VMSTATE_UINT16(dav
, TSC210xState
),
1023 VMSTATE_INT32(x
, TSC210xState
),
1024 VMSTATE_INT32(y
, TSC210xState
),
1025 VMSTATE_UINT8(offset
, TSC210xState
),
1026 VMSTATE_UINT8(page
, TSC210xState
),
1027 VMSTATE_UINT8(filter
, TSC210xState
),
1028 VMSTATE_UINT8(pin_func
, TSC210xState
),
1029 VMSTATE_UINT8(ref
, TSC210xState
),
1030 VMSTATE_UINT8(timing
, TSC210xState
),
1031 VMSTATE_UINT8(noise
, TSC210xState
),
1032 VMSTATE_UINT8(function
, TSC210xState
),
1033 VMSTATE_UINT8(nextfunction
, TSC210xState
),
1034 VMSTATE_UINT8(precision
, TSC210xState
),
1035 VMSTATE_UINT8(nextprecision
, TSC210xState
),
1036 VMSTATE_UINT16(audio_ctrl1
, TSC210xState
),
1037 VMSTATE_UINT16(audio_ctrl2
, TSC210xState
),
1038 VMSTATE_UINT16(audio_ctrl3
, TSC210xState
),
1039 VMSTATE_UINT16_ARRAY(pll
, TSC210xState
, 3),
1040 VMSTATE_UINT16(volume
, TSC210xState
),
1041 VMSTATE_UINT16(dac_power
, TSC210xState
),
1042 VMSTATE_INT64(volume_change
, TSC210xState
),
1043 VMSTATE_INT64(powerdown
, TSC210xState
),
1044 VMSTATE_INT64(now
, TSC210xState
),
1045 VMSTATE_UINT16_ARRAY(filter_data
, TSC210xState
, 0x14),
1046 VMSTATE_TIMER_PTR(timer
, TSC210xState
),
1047 VMSTATE_END_OF_LIST()
1050 static const VMStateDescription vmstate_tsc2102
= {
1053 .minimum_version_id
= 1,
1054 .pre_save
= tsc210x_pre_save
,
1055 .post_load
= tsc210x_post_load
,
1056 .fields
= vmstatefields_tsc210x
,
1059 static const VMStateDescription vmstate_tsc2301
= {
1062 .minimum_version_id
= 1,
1063 .pre_save
= tsc210x_pre_save
,
1064 .post_load
= tsc210x_post_load
,
1065 .fields
= vmstatefields_tsc210x
,
1068 uWireSlave
*tsc2102_init(qemu_irq pint
)
1072 s
= g_new0(TSC210xState
, 1);
1076 s
->precision
= s
->nextprecision
= 0;
1077 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1080 s
->name
= "tsc2102";
1092 s
->chip
.send
= (void *) tsc210x_write
;
1093 s
->chip
.receive
= (void *) tsc210x_read
;
1095 s
->codec
.opaque
= s
;
1096 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1097 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1098 s
->codec
.in
.fifo
= s
->in_fifo
;
1099 s
->codec
.out
.fifo
= s
->out_fifo
;
1103 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1104 "QEMU TSC2102-driven Touchscreen");
1106 AUD_register_card(s
->name
, &s
->card
);
1108 qemu_register_reset((void *) tsc210x_reset
, s
);
1109 vmstate_register(NULL
, 0, &vmstate_tsc2102
, s
);
1114 uWireSlave
*tsc2301_init(qemu_irq penirq
, qemu_irq kbirq
, qemu_irq dav
)
1118 s
= g_new0(TSC210xState
, 1);
1122 s
->precision
= s
->nextprecision
= 0;
1123 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tsc210x_timer_tick
, s
);
1128 s
->name
= "tsc2301";
1140 s
->chip
.send
= (void *) tsc210x_write
;
1141 s
->chip
.receive
= (void *) tsc210x_read
;
1143 s
->codec
.opaque
= s
;
1144 s
->codec
.tx_swallow
= (void *) tsc210x_i2s_swallow
;
1145 s
->codec
.set_rate
= (void *) tsc210x_i2s_set_rate
;
1146 s
->codec
.in
.fifo
= s
->in_fifo
;
1147 s
->codec
.out
.fifo
= s
->out_fifo
;
1151 qemu_add_mouse_event_handler(tsc210x_touchscreen_event
, s
, 1,
1152 "QEMU TSC2301-driven Touchscreen");
1154 AUD_register_card(s
->name
, &s
->card
);
1156 qemu_register_reset((void *) tsc210x_reset
, s
);
1157 vmstate_register(NULL
, 0, &vmstate_tsc2301
, s
);
1162 I2SCodec
*tsc210x_codec(uWireSlave
*chip
)
1164 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1170 * Use tslib generated calibration data to generate ADC input values
1171 * from the touchscreen. Assuming 12-bit precision was used during
1172 * tslib calibration.
1174 void tsc210x_set_transform(uWireSlave
*chip
,
1175 MouseTransformInfo
*info
)
1177 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1181 ltr
[0] = (int64_t) info
->a
[1] * info
->y
;
1182 ltr
[1] = (int64_t) info
->a
[4] * info
->x
;
1183 ltr
[2] = (int64_t) info
->a
[1] * info
->a
[3] -
1184 (int64_t) info
->a
[4] * info
->a
[0];
1185 ltr
[3] = (int64_t) info
->a
[2] * info
->a
[4] -
1186 (int64_t) info
->a
[5] * info
->a
[1];
1187 ltr
[4] = (int64_t) info
->a
[0] * info
->y
;
1188 ltr
[5] = (int64_t) info
->a
[3] * info
->x
;
1189 ltr
[6] = (int64_t) info
->a
[4] * info
->a
[0] -
1190 (int64_t) info
->a
[1] * info
->a
[3];
1191 ltr
[7] = (int64_t) info
->a
[2] * info
->a
[3] -
1192 (int64_t) info
->a
[5] * info
->a
[0];
1194 /* Avoid integer overflow */
1195 s
->tr
[0] = ltr
[0] >> 11;
1196 s
->tr
[1] = ltr
[1] >> 11;
1197 s
->tr
[2] = muldiv64(ltr
[2], 1, info
->a
[6]);
1198 s
->tr
[3] = muldiv64(ltr
[3], 1 << 4, ltr
[2]);
1199 s
->tr
[4] = ltr
[4] >> 11;
1200 s
->tr
[5] = ltr
[5] >> 11;
1201 s
->tr
[6] = muldiv64(ltr
[6], 1, info
->a
[6]);
1202 s
->tr
[7] = muldiv64(ltr
[7], 1 << 4, ltr
[6]);
1205 /* This version assumes touchscreen X & Y axis are parallel or
1206 * perpendicular to LCD's X & Y axis in some way. */
1207 if (abs(info
->a
[0]) > abs(info
->a
[1])) {
1209 s
->tr
[1] = -info
->a
[6] * info
->x
;
1210 s
->tr
[2] = info
->a
[0];
1211 s
->tr
[3] = -info
->a
[2] / info
->a
[0];
1212 s
->tr
[4] = info
->a
[6] * info
->y
;
1214 s
->tr
[6] = info
->a
[4];
1215 s
->tr
[7] = -info
->a
[5] / info
->a
[4];
1217 s
->tr
[0] = info
->a
[6] * info
->y
;
1219 s
->tr
[2] = info
->a
[1];
1220 s
->tr
[3] = -info
->a
[2] / info
->a
[1];
1222 s
->tr
[5] = -info
->a
[6] * info
->x
;
1223 s
->tr
[6] = info
->a
[3];
1224 s
->tr
[7] = -info
->a
[5] / info
->a
[3];
1236 void tsc210x_key_event(uWireSlave
*chip
, int key
, int down
)
1238 TSC210xState
*s
= (TSC210xState
*) chip
->opaque
;
1241 s
->kb
.down
|= 1 << key
;
1243 s
->kb
.down
&= ~(1 << key
);
1245 if (down
&& (s
->kb
.down
& ~s
->kb
.mask
) && !s
->kb
.intr
) {
1247 qemu_irq_lower(s
->kbint
);
1248 } else if (s
->kb
.intr
&& !(s
->kb
.down
& ~s
->kb
.mask
) &&
1249 !(s
->kb
.mode
& 1)) {
1251 qemu_irq_raise(s
->kbint
);