root / hw / tsc210x.c @ 38641a52
History | View | Annotate | Download (27.2 kB)
1 | 3efda49d | balrog | /*
|
---|---|---|---|
2 | 7fc42b4b | balrog | * TI TSC2102 (touchscreen/sensors/audio controller) emulator.
|
3 | 3efda49d | balrog | *
|
4 | 3efda49d | balrog | * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
|
5 | 3efda49d | balrog | *
|
6 | 3efda49d | balrog | * This program is free software; you can redistribute it and/or
|
7 | 3efda49d | balrog | * modify it under the terms of the GNU General Public License as
|
8 | 3efda49d | balrog | * published by the Free Software Foundation; either version 2 of
|
9 | 3efda49d | balrog | * the License, or (at your option) any later version.
|
10 | 3efda49d | balrog | *
|
11 | 3efda49d | balrog | * This program is distributed in the hope that it will be useful,
|
12 | 3efda49d | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3efda49d | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 3efda49d | balrog | * GNU General Public License for more details.
|
15 | 3efda49d | balrog | *
|
16 | 3efda49d | balrog | * You should have received a copy of the GNU General Public License
|
17 | 3efda49d | balrog | * along with this program; if not, write to the Free Software
|
18 | 3efda49d | balrog | * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
19 | 3efda49d | balrog | * MA 02111-1307 USA
|
20 | 3efda49d | balrog | */
|
21 | 3efda49d | balrog | |
22 | 3efda49d | balrog | #include "vl.h" |
23 | 3efda49d | balrog | |
24 | 3efda49d | balrog | #define TSC_DATA_REGISTERS_PAGE 0x0 |
25 | 3efda49d | balrog | #define TSC_CONTROL_REGISTERS_PAGE 0x1 |
26 | 3efda49d | balrog | #define TSC_AUDIO_REGISTERS_PAGE 0x2 |
27 | 3efda49d | balrog | |
28 | 3efda49d | balrog | #define TSC_VERBOSE
|
29 | 3efda49d | balrog | |
30 | 3efda49d | balrog | #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - resolution[p])) |
31 | 3efda49d | balrog | |
32 | 3efda49d | balrog | struct tsc210x_state_s {
|
33 | 3efda49d | balrog | qemu_irq pint; |
34 | 3efda49d | balrog | QEMUTimer *timer; |
35 | d8f699cb | balrog | QEMUSoundCard card; |
36 | 3efda49d | balrog | struct uwire_slave_s chip;
|
37 | d8f699cb | balrog | struct i2s_codec_s codec;
|
38 | d8f699cb | balrog | uint8_t in_fifo[16384];
|
39 | d8f699cb | balrog | uint8_t out_fifo[16384];
|
40 | 3efda49d | balrog | |
41 | 3efda49d | balrog | int x, y;
|
42 | 3efda49d | balrog | int pressure;
|
43 | 3efda49d | balrog | |
44 | 3efda49d | balrog | int state, page, offset, irq;
|
45 | 3efda49d | balrog | uint16_t command, dav; |
46 | 3efda49d | balrog | |
47 | 3efda49d | balrog | int busy;
|
48 | 3efda49d | balrog | int enabled;
|
49 | 3efda49d | balrog | int host_mode;
|
50 | 3efda49d | balrog | int function;
|
51 | 3efda49d | balrog | int nextfunction;
|
52 | 3efda49d | balrog | int precision;
|
53 | 3efda49d | balrog | int nextprecision;
|
54 | 3efda49d | balrog | int filter;
|
55 | 3efda49d | balrog | int pin_func;
|
56 | 3efda49d | balrog | int ref;
|
57 | 3efda49d | balrog | int timing;
|
58 | 3efda49d | balrog | int noise;
|
59 | 3efda49d | balrog | |
60 | 3efda49d | balrog | uint16_t audio_ctrl1; |
61 | 3efda49d | balrog | uint16_t audio_ctrl2; |
62 | 3efda49d | balrog | uint16_t audio_ctrl3; |
63 | 3efda49d | balrog | uint16_t pll[2];
|
64 | 3efda49d | balrog | uint16_t volume; |
65 | 3efda49d | balrog | int64_t volume_change; |
66 | 3efda49d | balrog | int softstep;
|
67 | 3efda49d | balrog | uint16_t dac_power; |
68 | 3efda49d | balrog | int64_t powerdown; |
69 | 3efda49d | balrog | uint16_t filter_data[0x14];
|
70 | d8f699cb | balrog | |
71 | d8f699cb | balrog | const char *name; |
72 | d8f699cb | balrog | SWVoiceIn *adc_voice[1];
|
73 | d8f699cb | balrog | SWVoiceOut *dac_voice[1];
|
74 | d8f699cb | balrog | int i2s_rx_rate;
|
75 | d8f699cb | balrog | int i2s_tx_rate;
|
76 | d8f699cb | balrog | AudioState *audio; |
77 | 3efda49d | balrog | }; |
78 | 3efda49d | balrog | |
79 | 3efda49d | balrog | static const int resolution[4] = { 12, 8, 10, 12 }; |
80 | 3efda49d | balrog | |
81 | 3efda49d | balrog | #define TSC_MODE_NO_SCAN 0x0 |
82 | 3efda49d | balrog | #define TSC_MODE_XY_SCAN 0x1 |
83 | 3efda49d | balrog | #define TSC_MODE_XYZ_SCAN 0x2 |
84 | 3efda49d | balrog | #define TSC_MODE_X 0x3 |
85 | 3efda49d | balrog | #define TSC_MODE_Y 0x4 |
86 | 3efda49d | balrog | #define TSC_MODE_Z 0x5 |
87 | 3efda49d | balrog | #define TSC_MODE_BAT1 0x6 |
88 | 3efda49d | balrog | #define TSC_MODE_BAT2 0x7 |
89 | 3efda49d | balrog | #define TSC_MODE_AUX 0x8 |
90 | 3efda49d | balrog | #define TSC_MODE_AUX_SCAN 0x9 |
91 | 3efda49d | balrog | #define TSC_MODE_TEMP1 0xa |
92 | 3efda49d | balrog | #define TSC_MODE_PORT_SCAN 0xb |
93 | 3efda49d | balrog | #define TSC_MODE_TEMP2 0xc |
94 | 3efda49d | balrog | #define TSC_MODE_XX_DRV 0xd |
95 | 3efda49d | balrog | #define TSC_MODE_YY_DRV 0xe |
96 | 3efda49d | balrog | #define TSC_MODE_YX_DRV 0xf |
97 | 3efda49d | balrog | |
98 | 3efda49d | balrog | static const uint16_t mode_regs[16] = { |
99 | 3efda49d | balrog | 0x0000, /* No scan */ |
100 | 3efda49d | balrog | 0x0600, /* X, Y scan */ |
101 | 3efda49d | balrog | 0x0780, /* X, Y, Z scan */ |
102 | 3efda49d | balrog | 0x0400, /* X */ |
103 | 3efda49d | balrog | 0x0200, /* Y */ |
104 | 3efda49d | balrog | 0x0180, /* Z */ |
105 | 3efda49d | balrog | 0x0040, /* BAT1 */ |
106 | 3efda49d | balrog | 0x0030, /* BAT2 */ |
107 | 3efda49d | balrog | 0x0010, /* AUX */ |
108 | 3efda49d | balrog | 0x0010, /* AUX scan */ |
109 | 3efda49d | balrog | 0x0004, /* TEMP1 */ |
110 | 3efda49d | balrog | 0x0070, /* Port scan */ |
111 | 3efda49d | balrog | 0x0002, /* TEMP2 */ |
112 | 3efda49d | balrog | 0x0000, /* X+, X- drivers */ |
113 | 3efda49d | balrog | 0x0000, /* Y+, Y- drivers */ |
114 | 3efda49d | balrog | 0x0000, /* Y+, X- drivers */ |
115 | 3efda49d | balrog | }; |
116 | 3efda49d | balrog | |
117 | 3efda49d | balrog | /*
|
118 | 3efda49d | balrog | * Convert screen coordinates to arbitrary values that the
|
119 | 3efda49d | balrog | * touchscreen in my Palm Tungsten E device returns.
|
120 | 3efda49d | balrog | * This shouldn't really matter (because the guest system
|
121 | 3efda49d | balrog | * should calibrate the touchscreen anyway), but let's
|
122 | 3efda49d | balrog | * imitate some real hardware.
|
123 | 3efda49d | balrog | */
|
124 | 3efda49d | balrog | #define X_TRANSFORM(value) \
|
125 | 3efda49d | balrog | ((3850 - ((int) (value) * (3850 - 250) / 32768)) << 4) |
126 | 3efda49d | balrog | #define Y_TRANSFORM(value) \
|
127 | 3efda49d | balrog | ((150 + ((int) (value) * (3037 - 150) / 32768)) << 4) |
128 | 3efda49d | balrog | #define Z1_TRANSFORM(s) \
|
129 | 3efda49d | balrog | ((400 - (s)->x + ((s)->pressure << 9)) << 4) |
130 | 3efda49d | balrog | #define Z2_TRANSFORM(s) \
|
131 | 3efda49d | balrog | ((4000 + (s)->y - ((s)->pressure << 10)) << 4) |
132 | 3efda49d | balrog | #define BAT1_VAL 0x8660 |
133 | 3efda49d | balrog | #define BAT2_VAL 0x0000 |
134 | 3efda49d | balrog | #define AUX1_VAL 0x35c0 |
135 | 3efda49d | balrog | #define AUX2_VAL 0xffff |
136 | 3efda49d | balrog | #define TEMP1_VAL 0x8c70 |
137 | 3efda49d | balrog | #define TEMP2_VAL 0xa5b0 |
138 | 3efda49d | balrog | |
139 | 3efda49d | balrog | #define TSC_POWEROFF_DELAY 50 |
140 | 3efda49d | balrog | #define TSC_SOFTSTEP_DELAY 50 |
141 | 3efda49d | balrog | |
142 | 3efda49d | balrog | static void tsc210x_reset(struct tsc210x_state_s *s) |
143 | 3efda49d | balrog | { |
144 | 3efda49d | balrog | s->state = 0;
|
145 | 3efda49d | balrog | s->pin_func = 2;
|
146 | 3efda49d | balrog | s->enabled = 0;
|
147 | 3efda49d | balrog | s->busy = 0;
|
148 | 3efda49d | balrog | s->nextfunction = 0;
|
149 | 3efda49d | balrog | s->ref = 0;
|
150 | 3efda49d | balrog | s->timing = 0;
|
151 | 3efda49d | balrog | s->irq = 0;
|
152 | 3efda49d | balrog | s->dav = 0;
|
153 | 3efda49d | balrog | |
154 | 3efda49d | balrog | s->audio_ctrl1 = 0x0000;
|
155 | 3efda49d | balrog | s->audio_ctrl2 = 0x4410;
|
156 | 3efda49d | balrog | s->audio_ctrl3 = 0x0000;
|
157 | 3efda49d | balrog | s->pll[0] = 0x1004; |
158 | 3efda49d | balrog | s->pll[1] = 0x0000; |
159 | 3efda49d | balrog | s->volume = 0xffff;
|
160 | 3efda49d | balrog | s->dac_power = 0x8540;
|
161 | 3efda49d | balrog | s->softstep = 1;
|
162 | 3efda49d | balrog | s->volume_change = 0;
|
163 | 3efda49d | balrog | s->powerdown = 0;
|
164 | 3efda49d | balrog | s->filter_data[0x00] = 0x6be3; |
165 | 3efda49d | balrog | s->filter_data[0x01] = 0x9666; |
166 | 3efda49d | balrog | s->filter_data[0x02] = 0x675d; |
167 | 3efda49d | balrog | s->filter_data[0x03] = 0x6be3; |
168 | 3efda49d | balrog | s->filter_data[0x04] = 0x9666; |
169 | 3efda49d | balrog | s->filter_data[0x05] = 0x675d; |
170 | 3efda49d | balrog | s->filter_data[0x06] = 0x7d83; |
171 | 3efda49d | balrog | s->filter_data[0x07] = 0x84ee; |
172 | 3efda49d | balrog | s->filter_data[0x08] = 0x7d83; |
173 | 3efda49d | balrog | s->filter_data[0x09] = 0x84ee; |
174 | 3efda49d | balrog | s->filter_data[0x0a] = 0x6be3; |
175 | 3efda49d | balrog | s->filter_data[0x0b] = 0x9666; |
176 | 3efda49d | balrog | s->filter_data[0x0c] = 0x675d; |
177 | 3efda49d | balrog | s->filter_data[0x0d] = 0x6be3; |
178 | 3efda49d | balrog | s->filter_data[0x0e] = 0x9666; |
179 | 3efda49d | balrog | s->filter_data[0x0f] = 0x675d; |
180 | 3efda49d | balrog | s->filter_data[0x10] = 0x7d83; |
181 | 3efda49d | balrog | s->filter_data[0x11] = 0x84ee; |
182 | 3efda49d | balrog | s->filter_data[0x12] = 0x7d83; |
183 | 3efda49d | balrog | s->filter_data[0x13] = 0x84ee; |
184 | 3efda49d | balrog | |
185 | d8f699cb | balrog | s->i2s_tx_rate = 0;
|
186 | d8f699cb | balrog | s->i2s_rx_rate = 0;
|
187 | d8f699cb | balrog | |
188 | 7fc42b4b | balrog | qemu_set_irq(s->pint, !s->irq); |
189 | 3efda49d | balrog | } |
190 | 3efda49d | balrog | |
191 | d8f699cb | balrog | struct tsc210x_rate_info_s {
|
192 | d8f699cb | balrog | int rate;
|
193 | d8f699cb | balrog | int dsor;
|
194 | d8f699cb | balrog | int fsref;
|
195 | d8f699cb | balrog | }; |
196 | d8f699cb | balrog | |
197 | d8f699cb | balrog | /* { rate, dsor, fsref } */
|
198 | d8f699cb | balrog | static const struct tsc210x_rate_info_s tsc2101_rates[] = { |
199 | d8f699cb | balrog | /* Fsref / 6.0 */
|
200 | d8f699cb | balrog | { 7350, 7, 1 }, |
201 | d8f699cb | balrog | { 8000, 7, 0 }, |
202 | d8f699cb | balrog | /* Fsref / 5.5 */
|
203 | d8f699cb | balrog | { 8018, 6, 1 }, |
204 | d8f699cb | balrog | { 8727, 6, 0 }, |
205 | d8f699cb | balrog | /* Fsref / 5.0 */
|
206 | d8f699cb | balrog | { 8820, 5, 1 }, |
207 | d8f699cb | balrog | { 9600, 5, 0 }, |
208 | d8f699cb | balrog | /* Fsref / 4.0 */
|
209 | d8f699cb | balrog | { 11025, 4, 1 }, |
210 | d8f699cb | balrog | { 12000, 4, 0 }, |
211 | d8f699cb | balrog | /* Fsref / 3.0 */
|
212 | d8f699cb | balrog | { 14700, 3, 1 }, |
213 | d8f699cb | balrog | { 16000, 3, 0 }, |
214 | d8f699cb | balrog | /* Fsref / 2.0 */
|
215 | d8f699cb | balrog | { 22050, 2, 1 }, |
216 | d8f699cb | balrog | { 24000, 2, 0 }, |
217 | d8f699cb | balrog | /* Fsref / 1.5 */
|
218 | d8f699cb | balrog | { 29400, 1, 1 }, |
219 | d8f699cb | balrog | { 32000, 1, 0 }, |
220 | d8f699cb | balrog | /* Fsref */
|
221 | d8f699cb | balrog | { 44100, 0, 1 }, |
222 | d8f699cb | balrog | { 48000, 0, 0 }, |
223 | d8f699cb | balrog | |
224 | d8f699cb | balrog | { 0, 0, 0 }, |
225 | d8f699cb | balrog | }; |
226 | d8f699cb | balrog | |
227 | d8f699cb | balrog | /* { rate, dsor, fsref } */
|
228 | d8f699cb | balrog | static const struct tsc210x_rate_info_s tsc2102_rates[] = { |
229 | d8f699cb | balrog | /* Fsref / 6.0 */
|
230 | d8f699cb | balrog | { 7350, 63, 1 }, |
231 | d8f699cb | balrog | { 8000, 63, 0 }, |
232 | d8f699cb | balrog | /* Fsref / 6.0 */
|
233 | d8f699cb | balrog | { 7350, 54, 1 }, |
234 | d8f699cb | balrog | { 8000, 54, 0 }, |
235 | d8f699cb | balrog | /* Fsref / 5.0 */
|
236 | d8f699cb | balrog | { 8820, 45, 1 }, |
237 | d8f699cb | balrog | { 9600, 45, 0 }, |
238 | d8f699cb | balrog | /* Fsref / 4.0 */
|
239 | d8f699cb | balrog | { 11025, 36, 1 }, |
240 | d8f699cb | balrog | { 12000, 36, 0 }, |
241 | d8f699cb | balrog | /* Fsref / 3.0 */
|
242 | d8f699cb | balrog | { 14700, 27, 1 }, |
243 | d8f699cb | balrog | { 16000, 27, 0 }, |
244 | d8f699cb | balrog | /* Fsref / 2.0 */
|
245 | d8f699cb | balrog | { 22050, 18, 1 }, |
246 | d8f699cb | balrog | { 24000, 18, 0 }, |
247 | d8f699cb | balrog | /* Fsref / 1.5 */
|
248 | d8f699cb | balrog | { 29400, 9, 1 }, |
249 | d8f699cb | balrog | { 32000, 9, 0 }, |
250 | d8f699cb | balrog | /* Fsref */
|
251 | d8f699cb | balrog | { 44100, 0, 1 }, |
252 | d8f699cb | balrog | { 48000, 0, 0 }, |
253 | d8f699cb | balrog | |
254 | d8f699cb | balrog | { 0, 0, 0 }, |
255 | d8f699cb | balrog | }; |
256 | d8f699cb | balrog | |
257 | d8f699cb | balrog | static inline void tsc210x_out_flush(struct tsc210x_state_s *s, int len) |
258 | d8f699cb | balrog | { |
259 | d8f699cb | balrog | uint8_t *data = s->codec.out.fifo + s->codec.out.start; |
260 | d8f699cb | balrog | uint8_t *end = data + len; |
261 | d8f699cb | balrog | |
262 | d8f699cb | balrog | while (data < end)
|
263 | d8f699cb | balrog | data += AUD_write(s->dac_voice[0], data, end - data) ?: (end - data);
|
264 | d8f699cb | balrog | |
265 | d8f699cb | balrog | s->codec.out.len -= len; |
266 | d8f699cb | balrog | if (s->codec.out.len)
|
267 | d8f699cb | balrog | memmove(s->codec.out.fifo, end, s->codec.out.len); |
268 | d8f699cb | balrog | s->codec.out.start = 0;
|
269 | d8f699cb | balrog | } |
270 | d8f699cb | balrog | |
271 | d8f699cb | balrog | static void tsc210x_audio_out_cb(struct tsc210x_state_s *s, int free_b) |
272 | d8f699cb | balrog | { |
273 | d8f699cb | balrog | if (s->codec.out.len >= free_b) {
|
274 | d8f699cb | balrog | tsc210x_out_flush(s, free_b); |
275 | d8f699cb | balrog | return;
|
276 | d8f699cb | balrog | } |
277 | d8f699cb | balrog | |
278 | d8f699cb | balrog | s->codec.out.size = MIN(free_b, 16384);
|
279 | d8f699cb | balrog | qemu_irq_raise(s->codec.tx_start); |
280 | d8f699cb | balrog | } |
281 | d8f699cb | balrog | |
282 | d8f699cb | balrog | static void tsc2102_audio_set_format(struct tsc210x_state_s *s) |
283 | d8f699cb | balrog | { |
284 | d8f699cb | balrog | int enable;
|
285 | d8f699cb | balrog | const struct tsc210x_rate_info_s *rate; |
286 | d8f699cb | balrog | audsettings_t fmt; |
287 | d8f699cb | balrog | |
288 | d8f699cb | balrog | if (s->dac_voice[0]) { |
289 | d8f699cb | balrog | tsc210x_out_flush(s, s->codec.out.len); |
290 | d8f699cb | balrog | s->codec.out.size = 0;
|
291 | d8f699cb | balrog | AUD_set_active_out(s->dac_voice[0], 0); |
292 | d8f699cb | balrog | AUD_close_out(&s->card, s->dac_voice[0]);
|
293 | d8f699cb | balrog | s->dac_voice[0] = 0; |
294 | d8f699cb | balrog | } |
295 | d8f699cb | balrog | |
296 | d8f699cb | balrog | enable = |
297 | d8f699cb | balrog | (~s->dac_power & (1 << 15)) && /* PWDNC */ |
298 | d8f699cb | balrog | (~s->dac_power & (1 << 10)); /* DAPWDN */ |
299 | d8f699cb | balrog | if (!enable)
|
300 | d8f699cb | balrog | return;
|
301 | d8f699cb | balrog | |
302 | d8f699cb | balrog | for (rate = tsc2102_rates; rate->rate; rate ++)
|
303 | d8f699cb | balrog | if (rate->dsor == (s->audio_ctrl1 & 0x3f) && /* DACFS */ |
304 | d8f699cb | balrog | rate->fsref == ((s->audio_ctrl3 >> 13) & 1))/* REFFS */ |
305 | d8f699cb | balrog | break;
|
306 | d8f699cb | balrog | if (!rate->rate) {
|
307 | d8f699cb | balrog | printf("%s: unknown sampling rate configured\n", __FUNCTION__);
|
308 | d8f699cb | balrog | return;
|
309 | d8f699cb | balrog | } |
310 | d8f699cb | balrog | |
311 | d8f699cb | balrog | /* Force our own sampling rate even in slave DAC mode */
|
312 | d8f699cb | balrog | fmt.endianness = 0;
|
313 | d8f699cb | balrog | fmt.nchannels = 2;
|
314 | d8f699cb | balrog | fmt.freq = rate->rate; |
315 | d8f699cb | balrog | fmt.fmt = AUD_FMT_S16; |
316 | d8f699cb | balrog | |
317 | d8f699cb | balrog | s->dac_voice[0] = AUD_open_out(&s->card, s->dac_voice[0], |
318 | d8f699cb | balrog | "tsc2102.sink", s, (void *) tsc210x_audio_out_cb, &fmt); |
319 | d8f699cb | balrog | if (s->dac_voice[0]) |
320 | d8f699cb | balrog | AUD_set_active_out(s->dac_voice[0], 1); |
321 | d8f699cb | balrog | } |
322 | d8f699cb | balrog | |
323 | 3efda49d | balrog | static uint16_t tsc2102_data_register_read(struct tsc210x_state_s *s, int reg) |
324 | 3efda49d | balrog | { |
325 | 3efda49d | balrog | switch (reg) {
|
326 | 3efda49d | balrog | case 0x00: /* X */ |
327 | 3efda49d | balrog | s->dav &= 0xfbff;
|
328 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(X_TRANSFORM(s->x), s->precision) +
|
329 | 3efda49d | balrog | (s->noise & 3);
|
330 | 3efda49d | balrog | |
331 | 3efda49d | balrog | case 0x01: /* Y */ |
332 | 3efda49d | balrog | s->noise ++; |
333 | 3efda49d | balrog | s->dav &= 0xfdff;
|
334 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(Y_TRANSFORM(s->y), s->precision) ^
|
335 | 3efda49d | balrog | (s->noise & 3);
|
336 | 3efda49d | balrog | |
337 | 3efda49d | balrog | case 0x02: /* Z1 */ |
338 | 3efda49d | balrog | s->dav &= 0xfeff;
|
339 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
|
340 | 3efda49d | balrog | (s->noise & 3);
|
341 | 3efda49d | balrog | |
342 | 3efda49d | balrog | case 0x03: /* Z2 */ |
343 | 3efda49d | balrog | s->dav &= 0xff7f;
|
344 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
|
345 | 3efda49d | balrog | (s->noise & 3);
|
346 | 3efda49d | balrog | |
347 | 3efda49d | balrog | case 0x04: /* KPData */ |
348 | 3efda49d | balrog | return 0xffff; |
349 | 3efda49d | balrog | |
350 | 3efda49d | balrog | case 0x05: /* BAT1 */ |
351 | 3efda49d | balrog | s->dav &= 0xffbf;
|
352 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(BAT1_VAL, s->precision);
|
353 | 3efda49d | balrog | |
354 | 3efda49d | balrog | case 0x06: /* BAT2 */ |
355 | 3efda49d | balrog | s->dav &= 0xffdf;
|
356 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(BAT2_VAL, s->precision);
|
357 | 3efda49d | balrog | |
358 | 3efda49d | balrog | case 0x07: /* AUX1 */ |
359 | 3efda49d | balrog | s->dav &= 0xffef;
|
360 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(AUX1_VAL, s->precision);
|
361 | 3efda49d | balrog | |
362 | 3efda49d | balrog | case 0x08: /* AUX2 */ |
363 | 3efda49d | balrog | s->dav &= 0xfff7;
|
364 | 3efda49d | balrog | return 0xffff; |
365 | 3efda49d | balrog | |
366 | 3efda49d | balrog | case 0x09: /* TEMP1 */ |
367 | 3efda49d | balrog | s->dav &= 0xfffb;
|
368 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision);
|
369 | 3efda49d | balrog | |
370 | 3efda49d | balrog | case 0x0a: /* TEMP2 */ |
371 | 3efda49d | balrog | s->dav &= 0xfffd;
|
372 | 3efda49d | balrog | return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision);
|
373 | 3efda49d | balrog | |
374 | 3efda49d | balrog | case 0x0b: /* DAC */ |
375 | 3efda49d | balrog | s->dav &= 0xfffe;
|
376 | 3efda49d | balrog | return 0xffff; |
377 | 3efda49d | balrog | |
378 | 3efda49d | balrog | default:
|
379 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
380 | 3efda49d | balrog | fprintf(stderr, "tsc2102_data_register_read: "
|
381 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
382 | 3efda49d | balrog | #endif
|
383 | 3efda49d | balrog | return 0xffff; |
384 | 3efda49d | balrog | } |
385 | 3efda49d | balrog | } |
386 | 3efda49d | balrog | |
387 | 3efda49d | balrog | static uint16_t tsc2102_control_register_read(
|
388 | 3efda49d | balrog | struct tsc210x_state_s *s, int reg) |
389 | 3efda49d | balrog | { |
390 | 3efda49d | balrog | switch (reg) {
|
391 | 3efda49d | balrog | case 0x00: /* TSC ADC */ |
392 | 3efda49d | balrog | return (s->pressure << 15) | ((!s->busy) << 14) | |
393 | 3efda49d | balrog | (s->nextfunction << 10) | (s->nextprecision << 8) | s->filter; |
394 | 3efda49d | balrog | |
395 | 3efda49d | balrog | case 0x01: /* Status */ |
396 | 3efda49d | balrog | return (s->pin_func << 14) | ((!s->enabled) << 13) | |
397 | 3efda49d | balrog | (s->host_mode << 12) | ((!!s->dav) << 11) | s->dav; |
398 | 3efda49d | balrog | |
399 | 3efda49d | balrog | case 0x03: /* Reference */ |
400 | 3efda49d | balrog | return s->ref;
|
401 | 3efda49d | balrog | |
402 | 3efda49d | balrog | case 0x04: /* Reset */ |
403 | 3efda49d | balrog | return 0xffff; |
404 | 3efda49d | balrog | |
405 | 3efda49d | balrog | case 0x05: /* Configuration */ |
406 | 3efda49d | balrog | return s->timing;
|
407 | 3efda49d | balrog | |
408 | 3efda49d | balrog | default:
|
409 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
410 | 3efda49d | balrog | fprintf(stderr, "tsc2102_control_register_read: "
|
411 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
412 | 3efda49d | balrog | #endif
|
413 | 3efda49d | balrog | return 0xffff; |
414 | 3efda49d | balrog | } |
415 | 3efda49d | balrog | } |
416 | 3efda49d | balrog | |
417 | 3efda49d | balrog | static uint16_t tsc2102_audio_register_read(struct tsc210x_state_s *s, int reg) |
418 | 3efda49d | balrog | { |
419 | 3efda49d | balrog | int l_ch, r_ch;
|
420 | 3efda49d | balrog | uint16_t val; |
421 | 3efda49d | balrog | |
422 | 3efda49d | balrog | switch (reg) {
|
423 | 3efda49d | balrog | case 0x00: /* Audio Control 1 */ |
424 | 3efda49d | balrog | return s->audio_ctrl1;
|
425 | 3efda49d | balrog | |
426 | 3efda49d | balrog | case 0x01: |
427 | 3efda49d | balrog | return 0xff00; |
428 | 3efda49d | balrog | |
429 | 3efda49d | balrog | case 0x02: /* DAC Volume Control */ |
430 | 3efda49d | balrog | return s->volume;
|
431 | 3efda49d | balrog | |
432 | 3efda49d | balrog | case 0x03: |
433 | 3efda49d | balrog | return 0x8b00; |
434 | 3efda49d | balrog | |
435 | 3efda49d | balrog | case 0x04: /* Audio Control 2 */ |
436 | 3efda49d | balrog | l_ch = 1;
|
437 | 3efda49d | balrog | r_ch = 1;
|
438 | 3efda49d | balrog | if (s->softstep && !(s->dac_power & (1 << 10))) { |
439 | 3efda49d | balrog | l_ch = (qemu_get_clock(vm_clock) > |
440 | 3efda49d | balrog | s->volume_change + TSC_SOFTSTEP_DELAY); |
441 | 3efda49d | balrog | r_ch = (qemu_get_clock(vm_clock) > |
442 | 3efda49d | balrog | s->volume_change + TSC_SOFTSTEP_DELAY); |
443 | 3efda49d | balrog | } |
444 | 3efda49d | balrog | |
445 | 3efda49d | balrog | return s->audio_ctrl2 | (l_ch << 3) | (r_ch << 2); |
446 | 3efda49d | balrog | |
447 | 3efda49d | balrog | case 0x05: /* Stereo DAC Power Control */ |
448 | 3efda49d | balrog | return 0x2aa0 | s->dac_power | |
449 | 3efda49d | balrog | (((s->dac_power & (1 << 10)) && |
450 | 3efda49d | balrog | (qemu_get_clock(vm_clock) > |
451 | 3efda49d | balrog | s->powerdown + TSC_POWEROFF_DELAY)) << 6);
|
452 | 3efda49d | balrog | |
453 | 3efda49d | balrog | case 0x06: /* Audio Control 3 */ |
454 | 3efda49d | balrog | val = s->audio_ctrl3 | 0x0001;
|
455 | 3efda49d | balrog | s->audio_ctrl3 &= 0xff3f;
|
456 | 3efda49d | balrog | return val;
|
457 | 3efda49d | balrog | |
458 | 3efda49d | balrog | case 0x07: /* LCH_BASS_BOOST_N0 */ |
459 | 3efda49d | balrog | case 0x08: /* LCH_BASS_BOOST_N1 */ |
460 | 3efda49d | balrog | case 0x09: /* LCH_BASS_BOOST_N2 */ |
461 | 3efda49d | balrog | case 0x0a: /* LCH_BASS_BOOST_N3 */ |
462 | 3efda49d | balrog | case 0x0b: /* LCH_BASS_BOOST_N4 */ |
463 | 3efda49d | balrog | case 0x0c: /* LCH_BASS_BOOST_N5 */ |
464 | 3efda49d | balrog | case 0x0d: /* LCH_BASS_BOOST_D1 */ |
465 | 3efda49d | balrog | case 0x0e: /* LCH_BASS_BOOST_D2 */ |
466 | 3efda49d | balrog | case 0x0f: /* LCH_BASS_BOOST_D4 */ |
467 | 3efda49d | balrog | case 0x10: /* LCH_BASS_BOOST_D5 */ |
468 | 3efda49d | balrog | case 0x11: /* RCH_BASS_BOOST_N0 */ |
469 | 3efda49d | balrog | case 0x12: /* RCH_BASS_BOOST_N1 */ |
470 | 3efda49d | balrog | case 0x13: /* RCH_BASS_BOOST_N2 */ |
471 | 3efda49d | balrog | case 0x14: /* RCH_BASS_BOOST_N3 */ |
472 | 3efda49d | balrog | case 0x15: /* RCH_BASS_BOOST_N4 */ |
473 | 3efda49d | balrog | case 0x16: /* RCH_BASS_BOOST_N5 */ |
474 | 3efda49d | balrog | case 0x17: /* RCH_BASS_BOOST_D1 */ |
475 | 3efda49d | balrog | case 0x18: /* RCH_BASS_BOOST_D2 */ |
476 | 3efda49d | balrog | case 0x19: /* RCH_BASS_BOOST_D4 */ |
477 | 3efda49d | balrog | case 0x1a: /* RCH_BASS_BOOST_D5 */ |
478 | 3efda49d | balrog | return s->filter_data[reg - 0x07]; |
479 | 3efda49d | balrog | |
480 | 3efda49d | balrog | case 0x1b: /* PLL Programmability 1 */ |
481 | 3efda49d | balrog | return s->pll[0]; |
482 | 3efda49d | balrog | |
483 | 3efda49d | balrog | case 0x1c: /* PLL Programmability 2 */ |
484 | 3efda49d | balrog | return s->pll[1]; |
485 | 3efda49d | balrog | |
486 | 3efda49d | balrog | case 0x1d: /* Audio Control 4 */ |
487 | 3efda49d | balrog | return (!s->softstep) << 14; |
488 | 3efda49d | balrog | |
489 | 3efda49d | balrog | default:
|
490 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
491 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_read: "
|
492 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
493 | 3efda49d | balrog | #endif
|
494 | 3efda49d | balrog | return 0xffff; |
495 | 3efda49d | balrog | } |
496 | 3efda49d | balrog | } |
497 | 3efda49d | balrog | |
498 | 3efda49d | balrog | static void tsc2102_data_register_write( |
499 | 3efda49d | balrog | struct tsc210x_state_s *s, int reg, uint16_t value) |
500 | 3efda49d | balrog | { |
501 | 3efda49d | balrog | switch (reg) {
|
502 | 3efda49d | balrog | case 0x00: /* X */ |
503 | 3efda49d | balrog | case 0x01: /* Y */ |
504 | 3efda49d | balrog | case 0x02: /* Z1 */ |
505 | 3efda49d | balrog | case 0x03: /* Z2 */ |
506 | 3efda49d | balrog | case 0x05: /* BAT1 */ |
507 | 3efda49d | balrog | case 0x06: /* BAT2 */ |
508 | 3efda49d | balrog | case 0x07: /* AUX1 */ |
509 | 3efda49d | balrog | case 0x08: /* AUX2 */ |
510 | 3efda49d | balrog | case 0x09: /* TEMP1 */ |
511 | 3efda49d | balrog | case 0x0a: /* TEMP2 */ |
512 | 3efda49d | balrog | return;
|
513 | 3efda49d | balrog | |
514 | 3efda49d | balrog | default:
|
515 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
516 | 3efda49d | balrog | fprintf(stderr, "tsc2102_data_register_write: "
|
517 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
518 | 3efda49d | balrog | #endif
|
519 | 3efda49d | balrog | } |
520 | 3efda49d | balrog | } |
521 | 3efda49d | balrog | |
522 | 3efda49d | balrog | static void tsc2102_control_register_write( |
523 | 3efda49d | balrog | struct tsc210x_state_s *s, int reg, uint16_t value) |
524 | 3efda49d | balrog | { |
525 | 3efda49d | balrog | switch (reg) {
|
526 | 3efda49d | balrog | case 0x00: /* TSC ADC */ |
527 | 3efda49d | balrog | s->host_mode = value >> 15;
|
528 | 3efda49d | balrog | s->enabled = !(value & 0x4000);
|
529 | 3efda49d | balrog | if (s->busy && !s->enabled)
|
530 | 3efda49d | balrog | qemu_del_timer(s->timer); |
531 | 3efda49d | balrog | s->busy &= s->enabled; |
532 | 3efda49d | balrog | s->nextfunction = (value >> 10) & 0xf; |
533 | 3efda49d | balrog | s->nextprecision = (value >> 8) & 3; |
534 | 3efda49d | balrog | s->filter = value & 0xff;
|
535 | 3efda49d | balrog | return;
|
536 | 3efda49d | balrog | |
537 | 3efda49d | balrog | case 0x01: /* Status */ |
538 | 3efda49d | balrog | s->pin_func = value >> 14;
|
539 | 3efda49d | balrog | return;
|
540 | 3efda49d | balrog | |
541 | 3efda49d | balrog | case 0x03: /* Reference */ |
542 | 3efda49d | balrog | s->ref = value & 0x1f;
|
543 | 3efda49d | balrog | return;
|
544 | 3efda49d | balrog | |
545 | 3efda49d | balrog | case 0x04: /* Reset */ |
546 | 3efda49d | balrog | if (value == 0xbb00) { |
547 | 3efda49d | balrog | if (s->busy)
|
548 | 3efda49d | balrog | qemu_del_timer(s->timer); |
549 | 3efda49d | balrog | tsc210x_reset(s); |
550 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
551 | 3efda49d | balrog | } else {
|
552 | 3efda49d | balrog | fprintf(stderr, "tsc2102_control_register_write: "
|
553 | 3efda49d | balrog | "wrong value written into RESET\n");
|
554 | 3efda49d | balrog | #endif
|
555 | 3efda49d | balrog | } |
556 | 3efda49d | balrog | return;
|
557 | 3efda49d | balrog | |
558 | 3efda49d | balrog | case 0x05: /* Configuration */ |
559 | 3efda49d | balrog | s->timing = value & 0x3f;
|
560 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
561 | 3efda49d | balrog | if (value & ~0x3f) |
562 | 3efda49d | balrog | fprintf(stderr, "tsc2102_control_register_write: "
|
563 | 3efda49d | balrog | "wrong value written into CONFIG\n");
|
564 | 3efda49d | balrog | #endif
|
565 | 3efda49d | balrog | return;
|
566 | 3efda49d | balrog | |
567 | 3efda49d | balrog | default:
|
568 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
569 | 3efda49d | balrog | fprintf(stderr, "tsc2102_control_register_write: "
|
570 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
571 | 3efda49d | balrog | #endif
|
572 | 3efda49d | balrog | } |
573 | 3efda49d | balrog | } |
574 | 3efda49d | balrog | |
575 | 3efda49d | balrog | static void tsc2102_audio_register_write( |
576 | 3efda49d | balrog | struct tsc210x_state_s *s, int reg, uint16_t value) |
577 | 3efda49d | balrog | { |
578 | 3efda49d | balrog | switch (reg) {
|
579 | 3efda49d | balrog | case 0x00: /* Audio Control 1 */ |
580 | 3efda49d | balrog | s->audio_ctrl1 = value & 0x0f3f;
|
581 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
582 | 3efda49d | balrog | if ((value & ~0x0f3f) || ((value & 7) != ((value >> 3) & 7))) |
583 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
584 | 3efda49d | balrog | "wrong value written into Audio 1\n");
|
585 | 3efda49d | balrog | #endif
|
586 | d8f699cb | balrog | if (s->audio)
|
587 | d8f699cb | balrog | tsc2102_audio_set_format(s); |
588 | 3efda49d | balrog | return;
|
589 | 3efda49d | balrog | |
590 | 3efda49d | balrog | case 0x01: |
591 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
592 | 3efda49d | balrog | if (value != 0xff00) |
593 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
594 | 3efda49d | balrog | "wrong value written into reg 0x01\n");
|
595 | 3efda49d | balrog | #endif
|
596 | 3efda49d | balrog | return;
|
597 | 3efda49d | balrog | |
598 | 3efda49d | balrog | case 0x02: /* DAC Volume Control */ |
599 | 3efda49d | balrog | s->volume = value; |
600 | 3efda49d | balrog | s->volume_change = qemu_get_clock(vm_clock); |
601 | 3efda49d | balrog | return;
|
602 | 3efda49d | balrog | |
603 | 3efda49d | balrog | case 0x03: |
604 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
605 | 3efda49d | balrog | if (value != 0x8b00) |
606 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
607 | 3efda49d | balrog | "wrong value written into reg 0x03\n");
|
608 | 3efda49d | balrog | #endif
|
609 | 3efda49d | balrog | return;
|
610 | 3efda49d | balrog | |
611 | 3efda49d | balrog | case 0x04: /* Audio Control 2 */ |
612 | 3efda49d | balrog | s->audio_ctrl2 = value & 0xf7f2;
|
613 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
614 | 3efda49d | balrog | if (value & ~0xf7fd) |
615 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
616 | 3efda49d | balrog | "wrong value written into Audio 2\n");
|
617 | 3efda49d | balrog | #endif
|
618 | 3efda49d | balrog | return;
|
619 | 3efda49d | balrog | |
620 | 3efda49d | balrog | case 0x05: /* Stereo DAC Power Control */ |
621 | 3efda49d | balrog | if ((value & ~s->dac_power) & (1 << 10)) |
622 | 3efda49d | balrog | s->powerdown = qemu_get_clock(vm_clock); |
623 | 3efda49d | balrog | |
624 | 3efda49d | balrog | s->dac_power = value & 0x9543;
|
625 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
626 | 3efda49d | balrog | if ((value & ~0x9543) != 0x2aa0) |
627 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
628 | 3efda49d | balrog | "wrong value written into Power\n");
|
629 | 3efda49d | balrog | #endif
|
630 | d8f699cb | balrog | if (s->audio)
|
631 | d8f699cb | balrog | tsc2102_audio_set_format(s); |
632 | 3efda49d | balrog | return;
|
633 | 3efda49d | balrog | |
634 | 3efda49d | balrog | case 0x06: /* Audio Control 3 */ |
635 | 3efda49d | balrog | s->audio_ctrl3 &= 0x00c0;
|
636 | 3efda49d | balrog | s->audio_ctrl3 |= value & 0xf800;
|
637 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
638 | 3efda49d | balrog | if (value & ~0xf8c7) |
639 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
640 | 3efda49d | balrog | "wrong value written into Audio 3\n");
|
641 | 3efda49d | balrog | #endif
|
642 | d8f699cb | balrog | if (s->audio)
|
643 | d8f699cb | balrog | tsc2102_audio_set_format(s); |
644 | 3efda49d | balrog | return;
|
645 | 3efda49d | balrog | |
646 | 3efda49d | balrog | case 0x07: /* LCH_BASS_BOOST_N0 */ |
647 | 3efda49d | balrog | case 0x08: /* LCH_BASS_BOOST_N1 */ |
648 | 3efda49d | balrog | case 0x09: /* LCH_BASS_BOOST_N2 */ |
649 | 3efda49d | balrog | case 0x0a: /* LCH_BASS_BOOST_N3 */ |
650 | 3efda49d | balrog | case 0x0b: /* LCH_BASS_BOOST_N4 */ |
651 | 3efda49d | balrog | case 0x0c: /* LCH_BASS_BOOST_N5 */ |
652 | 3efda49d | balrog | case 0x0d: /* LCH_BASS_BOOST_D1 */ |
653 | 3efda49d | balrog | case 0x0e: /* LCH_BASS_BOOST_D2 */ |
654 | 3efda49d | balrog | case 0x0f: /* LCH_BASS_BOOST_D4 */ |
655 | 3efda49d | balrog | case 0x10: /* LCH_BASS_BOOST_D5 */ |
656 | 3efda49d | balrog | case 0x11: /* RCH_BASS_BOOST_N0 */ |
657 | 3efda49d | balrog | case 0x12: /* RCH_BASS_BOOST_N1 */ |
658 | 3efda49d | balrog | case 0x13: /* RCH_BASS_BOOST_N2 */ |
659 | 3efda49d | balrog | case 0x14: /* RCH_BASS_BOOST_N3 */ |
660 | 3efda49d | balrog | case 0x15: /* RCH_BASS_BOOST_N4 */ |
661 | 3efda49d | balrog | case 0x16: /* RCH_BASS_BOOST_N5 */ |
662 | 3efda49d | balrog | case 0x17: /* RCH_BASS_BOOST_D1 */ |
663 | 3efda49d | balrog | case 0x18: /* RCH_BASS_BOOST_D2 */ |
664 | 3efda49d | balrog | case 0x19: /* RCH_BASS_BOOST_D4 */ |
665 | 3efda49d | balrog | case 0x1a: /* RCH_BASS_BOOST_D5 */ |
666 | 3efda49d | balrog | s->filter_data[reg - 0x07] = value;
|
667 | 3efda49d | balrog | return;
|
668 | 3efda49d | balrog | |
669 | 3efda49d | balrog | case 0x1b: /* PLL Programmability 1 */ |
670 | 3efda49d | balrog | s->pll[0] = value & 0xfffc; |
671 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
672 | 3efda49d | balrog | if (value & ~0xfffc) |
673 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
674 | 3efda49d | balrog | "wrong value written into PLL 1\n");
|
675 | 3efda49d | balrog | #endif
|
676 | 3efda49d | balrog | return;
|
677 | 3efda49d | balrog | |
678 | 3efda49d | balrog | case 0x1c: /* PLL Programmability 2 */ |
679 | 3efda49d | balrog | s->pll[1] = value & 0xfffc; |
680 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
681 | 3efda49d | balrog | if (value & ~0xfffc) |
682 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
683 | 3efda49d | balrog | "wrong value written into PLL 2\n");
|
684 | 3efda49d | balrog | #endif
|
685 | 3efda49d | balrog | return;
|
686 | 3efda49d | balrog | |
687 | 3efda49d | balrog | case 0x1d: /* Audio Control 4 */ |
688 | 3efda49d | balrog | s->softstep = !(value & 0x4000);
|
689 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
690 | 3efda49d | balrog | if (value & ~0x4000) |
691 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
692 | 3efda49d | balrog | "wrong value written into Audio 4\n");
|
693 | 3efda49d | balrog | #endif
|
694 | 3efda49d | balrog | return;
|
695 | 3efda49d | balrog | |
696 | 3efda49d | balrog | default:
|
697 | 3efda49d | balrog | #ifdef TSC_VERBOSE
|
698 | 3efda49d | balrog | fprintf(stderr, "tsc2102_audio_register_write: "
|
699 | 3efda49d | balrog | "no such register: 0x%02x\n", reg);
|
700 | 3efda49d | balrog | #endif
|
701 | 3efda49d | balrog | } |
702 | 3efda49d | balrog | } |
703 | 3efda49d | balrog | |
704 | 3efda49d | balrog | /* This handles most of the chip logic. */
|
705 | 3efda49d | balrog | static void tsc210x_pin_update(struct tsc210x_state_s *s) |
706 | 3efda49d | balrog | { |
707 | 3efda49d | balrog | int64_t expires; |
708 | 3efda49d | balrog | int pin_state;
|
709 | 3efda49d | balrog | |
710 | 3efda49d | balrog | switch (s->pin_func) {
|
711 | 3efda49d | balrog | case 0: |
712 | 3efda49d | balrog | pin_state = s->pressure; |
713 | 3efda49d | balrog | break;
|
714 | 3efda49d | balrog | case 1: |
715 | 3efda49d | balrog | pin_state = !!s->dav; |
716 | 3efda49d | balrog | break;
|
717 | 3efda49d | balrog | case 2: |
718 | 3efda49d | balrog | default:
|
719 | 3efda49d | balrog | pin_state = s->pressure && !s->dav; |
720 | 3efda49d | balrog | } |
721 | 3efda49d | balrog | |
722 | 3efda49d | balrog | if (!s->enabled)
|
723 | 3efda49d | balrog | pin_state = 0;
|
724 | 3efda49d | balrog | |
725 | 3efda49d | balrog | if (pin_state != s->irq) {
|
726 | 3efda49d | balrog | s->irq = pin_state; |
727 | 7fc42b4b | balrog | qemu_set_irq(s->pint, !s->irq); |
728 | 3efda49d | balrog | } |
729 | 3efda49d | balrog | |
730 | 3efda49d | balrog | switch (s->nextfunction) {
|
731 | 3efda49d | balrog | case TSC_MODE_XY_SCAN:
|
732 | 3efda49d | balrog | case TSC_MODE_XYZ_SCAN:
|
733 | 3efda49d | balrog | if (!s->pressure)
|
734 | 3efda49d | balrog | return;
|
735 | 3efda49d | balrog | break;
|
736 | 3efda49d | balrog | |
737 | 3efda49d | balrog | case TSC_MODE_X:
|
738 | 3efda49d | balrog | case TSC_MODE_Y:
|
739 | 3efda49d | balrog | case TSC_MODE_Z:
|
740 | 3efda49d | balrog | if (!s->pressure)
|
741 | 3efda49d | balrog | return;
|
742 | 3efda49d | balrog | /* Fall through */
|
743 | 3efda49d | balrog | case TSC_MODE_BAT1:
|
744 | 3efda49d | balrog | case TSC_MODE_BAT2:
|
745 | 3efda49d | balrog | case TSC_MODE_AUX:
|
746 | 3efda49d | balrog | case TSC_MODE_TEMP1:
|
747 | 3efda49d | balrog | case TSC_MODE_TEMP2:
|
748 | 3efda49d | balrog | if (s->dav)
|
749 | 3efda49d | balrog | s->enabled = 0;
|
750 | 3efda49d | balrog | break;
|
751 | 3efda49d | balrog | |
752 | 3efda49d | balrog | case TSC_MODE_AUX_SCAN:
|
753 | 3efda49d | balrog | case TSC_MODE_PORT_SCAN:
|
754 | 3efda49d | balrog | break;
|
755 | 3efda49d | balrog | |
756 | 3efda49d | balrog | case TSC_MODE_NO_SCAN:
|
757 | 3efda49d | balrog | case TSC_MODE_XX_DRV:
|
758 | 3efda49d | balrog | case TSC_MODE_YY_DRV:
|
759 | 3efda49d | balrog | case TSC_MODE_YX_DRV:
|
760 | 3efda49d | balrog | default:
|
761 | 3efda49d | balrog | return;
|
762 | 3efda49d | balrog | } |
763 | 3efda49d | balrog | |
764 | 3efda49d | balrog | if (!s->enabled || s->busy)
|
765 | 3efda49d | balrog | return;
|
766 | 3efda49d | balrog | |
767 | 3efda49d | balrog | s->busy = 1;
|
768 | 3efda49d | balrog | s->precision = s->nextprecision; |
769 | 3efda49d | balrog | s->function = s->nextfunction; |
770 | 3efda49d | balrog | expires = qemu_get_clock(vm_clock) + (ticks_per_sec >> 10);
|
771 | 3efda49d | balrog | qemu_mod_timer(s->timer, expires); |
772 | 3efda49d | balrog | } |
773 | 3efda49d | balrog | |
774 | 3efda49d | balrog | static uint16_t tsc210x_read(struct tsc210x_state_s *s) |
775 | 3efda49d | balrog | { |
776 | 3efda49d | balrog | uint16_t ret = 0x0000;
|
777 | 3efda49d | balrog | |
778 | 3efda49d | balrog | if (!s->command)
|
779 | 3efda49d | balrog | fprintf(stderr, "tsc210x_read: SPI underrun!\n");
|
780 | 3efda49d | balrog | |
781 | 3efda49d | balrog | switch (s->page) {
|
782 | 3efda49d | balrog | case TSC_DATA_REGISTERS_PAGE:
|
783 | 3efda49d | balrog | ret = tsc2102_data_register_read(s, s->offset); |
784 | 3efda49d | balrog | break;
|
785 | 3efda49d | balrog | case TSC_CONTROL_REGISTERS_PAGE:
|
786 | 3efda49d | balrog | ret = tsc2102_control_register_read(s, s->offset); |
787 | 3efda49d | balrog | break;
|
788 | 3efda49d | balrog | case TSC_AUDIO_REGISTERS_PAGE:
|
789 | 3efda49d | balrog | ret = tsc2102_audio_register_read(s, s->offset); |
790 | 3efda49d | balrog | break;
|
791 | 3efda49d | balrog | default:
|
792 | 3efda49d | balrog | cpu_abort(cpu_single_env, "tsc210x_read: wrong memory page\n");
|
793 | 3efda49d | balrog | } |
794 | 3efda49d | balrog | |
795 | 3efda49d | balrog | tsc210x_pin_update(s); |
796 | 3efda49d | balrog | |
797 | 3efda49d | balrog | /* Allow sequential reads. */
|
798 | 3efda49d | balrog | s->offset ++; |
799 | 3efda49d | balrog | s->state = 0;
|
800 | 3efda49d | balrog | return ret;
|
801 | 3efda49d | balrog | } |
802 | 3efda49d | balrog | |
803 | 3efda49d | balrog | static void tsc210x_write(struct tsc210x_state_s *s, uint16_t value) |
804 | 3efda49d | balrog | { |
805 | 3efda49d | balrog | /*
|
806 | 3efda49d | balrog | * This is a two-state state machine for reading
|
807 | 3efda49d | balrog | * command and data every second time.
|
808 | 3efda49d | balrog | */
|
809 | 3efda49d | balrog | if (!s->state) {
|
810 | 3efda49d | balrog | s->command = value >> 15;
|
811 | 3efda49d | balrog | s->page = (value >> 11) & 0x0f; |
812 | 3efda49d | balrog | s->offset = (value >> 5) & 0x3f; |
813 | 3efda49d | balrog | s->state = 1;
|
814 | 3efda49d | balrog | } else {
|
815 | 3efda49d | balrog | if (s->command)
|
816 | 3efda49d | balrog | fprintf(stderr, "tsc210x_write: SPI overrun!\n");
|
817 | 3efda49d | balrog | else
|
818 | 3efda49d | balrog | switch (s->page) {
|
819 | 3efda49d | balrog | case TSC_DATA_REGISTERS_PAGE:
|
820 | 3efda49d | balrog | tsc2102_data_register_write(s, s->offset, value); |
821 | 3efda49d | balrog | break;
|
822 | 3efda49d | balrog | case TSC_CONTROL_REGISTERS_PAGE:
|
823 | 3efda49d | balrog | tsc2102_control_register_write(s, s->offset, value); |
824 | 3efda49d | balrog | break;
|
825 | 3efda49d | balrog | case TSC_AUDIO_REGISTERS_PAGE:
|
826 | 3efda49d | balrog | tsc2102_audio_register_write(s, s->offset, value); |
827 | 3efda49d | balrog | break;
|
828 | 3efda49d | balrog | default:
|
829 | 3efda49d | balrog | cpu_abort(cpu_single_env, |
830 | 3efda49d | balrog | "tsc210x_write: wrong memory page\n");
|
831 | 3efda49d | balrog | } |
832 | 3efda49d | balrog | |
833 | 3efda49d | balrog | tsc210x_pin_update(s); |
834 | 3efda49d | balrog | s->state = 0;
|
835 | 3efda49d | balrog | } |
836 | 3efda49d | balrog | } |
837 | 3efda49d | balrog | |
838 | 3efda49d | balrog | static void tsc210x_timer_tick(void *opaque) |
839 | 3efda49d | balrog | { |
840 | 3efda49d | balrog | struct tsc210x_state_s *s = opaque;
|
841 | 3efda49d | balrog | |
842 | 3efda49d | balrog | /* Timer ticked -- a set of conversions has been finished. */
|
843 | 3efda49d | balrog | |
844 | 3efda49d | balrog | if (!s->busy)
|
845 | 3efda49d | balrog | return;
|
846 | 3efda49d | balrog | |
847 | 3efda49d | balrog | s->busy = 0;
|
848 | 3efda49d | balrog | s->dav |= mode_regs[s->function]; |
849 | 3efda49d | balrog | tsc210x_pin_update(s); |
850 | 3efda49d | balrog | } |
851 | 3efda49d | balrog | |
852 | 3efda49d | balrog | static void tsc210x_touchscreen_event(void *opaque, |
853 | 3efda49d | balrog | int x, int y, int z, int buttons_state) |
854 | 3efda49d | balrog | { |
855 | 3efda49d | balrog | struct tsc210x_state_s *s = opaque;
|
856 | 3efda49d | balrog | int p = s->pressure;
|
857 | 3efda49d | balrog | |
858 | 3efda49d | balrog | if (buttons_state) {
|
859 | 3efda49d | balrog | s->x = x; |
860 | 3efda49d | balrog | s->y = y; |
861 | 3efda49d | balrog | } |
862 | 3efda49d | balrog | s->pressure = !!buttons_state; |
863 | 3efda49d | balrog | |
864 | 3efda49d | balrog | /*
|
865 | 3efda49d | balrog | * Note: We would get better responsiveness in the guest by
|
866 | 3efda49d | balrog | * signaling TS events immediately, but for now we simulate
|
867 | 3efda49d | balrog | * the first conversion delay for sake of correctness.
|
868 | 3efda49d | balrog | */
|
869 | 3efda49d | balrog | if (p != s->pressure)
|
870 | 3efda49d | balrog | tsc210x_pin_update(s); |
871 | 3efda49d | balrog | } |
872 | 3efda49d | balrog | |
873 | d8f699cb | balrog | static void tsc210x_i2s_swallow(struct tsc210x_state_s *s) |
874 | d8f699cb | balrog | { |
875 | d8f699cb | balrog | if (s->dac_voice[0]) |
876 | d8f699cb | balrog | tsc210x_out_flush(s, s->codec.out.len); |
877 | d8f699cb | balrog | else
|
878 | d8f699cb | balrog | s->codec.out.len = 0;
|
879 | d8f699cb | balrog | } |
880 | d8f699cb | balrog | |
881 | d8f699cb | balrog | static void tsc210x_i2s_set_rate(struct tsc210x_state_s *s, int in, int out) |
882 | d8f699cb | balrog | { |
883 | d8f699cb | balrog | s->i2s_tx_rate = out; |
884 | d8f699cb | balrog | s->i2s_rx_rate = in; |
885 | d8f699cb | balrog | } |
886 | d8f699cb | balrog | |
887 | 3efda49d | balrog | static void tsc210x_save(QEMUFile *f, void *opaque) |
888 | 3efda49d | balrog | { |
889 | 3efda49d | balrog | struct tsc210x_state_s *s = (struct tsc210x_state_s *) opaque; |
890 | 3efda49d | balrog | int64_t now = qemu_get_clock(vm_clock); |
891 | 3efda49d | balrog | int i;
|
892 | 3efda49d | balrog | |
893 | 3efda49d | balrog | qemu_put_be16(f, s->x); |
894 | 3efda49d | balrog | qemu_put_be16(f, s->y); |
895 | 3efda49d | balrog | qemu_put_byte(f, s->pressure); |
896 | 3efda49d | balrog | |
897 | 3efda49d | balrog | qemu_put_byte(f, s->state); |
898 | 3efda49d | balrog | qemu_put_byte(f, s->page); |
899 | 3efda49d | balrog | qemu_put_byte(f, s->offset); |
900 | 3efda49d | balrog | qemu_put_byte(f, s->command); |
901 | 3efda49d | balrog | |
902 | 3efda49d | balrog | qemu_put_byte(f, s->irq); |
903 | 3efda49d | balrog | qemu_put_be16s(f, &s->dav); |
904 | 3efda49d | balrog | |
905 | 3efda49d | balrog | qemu_put_timer(f, s->timer); |
906 | 3efda49d | balrog | qemu_put_byte(f, s->enabled); |
907 | 3efda49d | balrog | qemu_put_byte(f, s->host_mode); |
908 | 3efda49d | balrog | qemu_put_byte(f, s->function); |
909 | 3efda49d | balrog | qemu_put_byte(f, s->nextfunction); |
910 | 3efda49d | balrog | qemu_put_byte(f, s->precision); |
911 | 3efda49d | balrog | qemu_put_byte(f, s->nextprecision); |
912 | 3efda49d | balrog | qemu_put_byte(f, s->filter); |
913 | 3efda49d | balrog | qemu_put_byte(f, s->pin_func); |
914 | 3efda49d | balrog | qemu_put_byte(f, s->ref); |
915 | 3efda49d | balrog | qemu_put_byte(f, s->timing); |
916 | 3efda49d | balrog | qemu_put_be32(f, s->noise); |
917 | 3efda49d | balrog | |
918 | 3efda49d | balrog | qemu_put_be16s(f, &s->audio_ctrl1); |
919 | 3efda49d | balrog | qemu_put_be16s(f, &s->audio_ctrl2); |
920 | 3efda49d | balrog | qemu_put_be16s(f, &s->audio_ctrl3); |
921 | 3efda49d | balrog | qemu_put_be16s(f, &s->pll[0]);
|
922 | 3efda49d | balrog | qemu_put_be16s(f, &s->pll[1]);
|
923 | 3efda49d | balrog | qemu_put_be16s(f, &s->volume); |
924 | 3efda49d | balrog | qemu_put_be64(f, (uint64_t) (s->volume_change - now)); |
925 | 3efda49d | balrog | qemu_put_be64(f, (uint64_t) (s->powerdown - now)); |
926 | 3efda49d | balrog | qemu_put_byte(f, s->softstep); |
927 | 3efda49d | balrog | qemu_put_be16s(f, &s->dac_power); |
928 | 3efda49d | balrog | |
929 | 3efda49d | balrog | for (i = 0; i < 0x14; i ++) |
930 | 3efda49d | balrog | qemu_put_be16s(f, &s->filter_data[i]); |
931 | 3efda49d | balrog | } |
932 | 3efda49d | balrog | |
933 | 3efda49d | balrog | static int tsc210x_load(QEMUFile *f, void *opaque, int version_id) |
934 | 3efda49d | balrog | { |
935 | 3efda49d | balrog | struct tsc210x_state_s *s = (struct tsc210x_state_s *) opaque; |
936 | 3efda49d | balrog | int64_t now = qemu_get_clock(vm_clock); |
937 | 3efda49d | balrog | int i;
|
938 | 3efda49d | balrog | |
939 | 3efda49d | balrog | s->x = qemu_get_be16(f); |
940 | 3efda49d | balrog | s->y = qemu_get_be16(f); |
941 | 3efda49d | balrog | s->pressure = qemu_get_byte(f); |
942 | 3efda49d | balrog | |
943 | 3efda49d | balrog | s->state = qemu_get_byte(f); |
944 | 3efda49d | balrog | s->page = qemu_get_byte(f); |
945 | 3efda49d | balrog | s->offset = qemu_get_byte(f); |
946 | 3efda49d | balrog | s->command = qemu_get_byte(f); |
947 | 3efda49d | balrog | |
948 | 3efda49d | balrog | s->irq = qemu_get_byte(f); |
949 | 3efda49d | balrog | qemu_get_be16s(f, &s->dav); |
950 | 3efda49d | balrog | |
951 | 3efda49d | balrog | qemu_get_timer(f, s->timer); |
952 | 3efda49d | balrog | s->enabled = qemu_get_byte(f); |
953 | 3efda49d | balrog | s->host_mode = qemu_get_byte(f); |
954 | 3efda49d | balrog | s->function = qemu_get_byte(f); |
955 | 3efda49d | balrog | s->nextfunction = qemu_get_byte(f); |
956 | 3efda49d | balrog | s->precision = qemu_get_byte(f); |
957 | 3efda49d | balrog | s->nextprecision = qemu_get_byte(f); |
958 | 3efda49d | balrog | s->filter = qemu_get_byte(f); |
959 | 3efda49d | balrog | s->pin_func = qemu_get_byte(f); |
960 | 3efda49d | balrog | s->ref = qemu_get_byte(f); |
961 | 3efda49d | balrog | s->timing = qemu_get_byte(f); |
962 | 3efda49d | balrog | s->noise = qemu_get_be32(f); |
963 | 3efda49d | balrog | |
964 | 3efda49d | balrog | qemu_get_be16s(f, &s->audio_ctrl1); |
965 | 3efda49d | balrog | qemu_get_be16s(f, &s->audio_ctrl2); |
966 | 3efda49d | balrog | qemu_get_be16s(f, &s->audio_ctrl3); |
967 | 3efda49d | balrog | qemu_get_be16s(f, &s->pll[0]);
|
968 | 3efda49d | balrog | qemu_get_be16s(f, &s->pll[1]);
|
969 | 3efda49d | balrog | qemu_get_be16s(f, &s->volume); |
970 | 3efda49d | balrog | s->volume_change = (int64_t) qemu_get_be64(f) + now; |
971 | 3efda49d | balrog | s->powerdown = (int64_t) qemu_get_be64(f) + now; |
972 | 3efda49d | balrog | s->softstep = qemu_get_byte(f); |
973 | 3efda49d | balrog | qemu_get_be16s(f, &s->dac_power); |
974 | 3efda49d | balrog | |
975 | 3efda49d | balrog | for (i = 0; i < 0x14; i ++) |
976 | 3efda49d | balrog | qemu_get_be16s(f, &s->filter_data[i]); |
977 | 3efda49d | balrog | |
978 | 3efda49d | balrog | s->busy = qemu_timer_pending(s->timer); |
979 | 7fc42b4b | balrog | qemu_set_irq(s->pint, !s->irq); |
980 | 3efda49d | balrog | |
981 | 3efda49d | balrog | return 0; |
982 | 3efda49d | balrog | } |
983 | 3efda49d | balrog | |
984 | 3efda49d | balrog | static int tsc2102_iid = 0; |
985 | 3efda49d | balrog | |
986 | d8f699cb | balrog | struct uwire_slave_s *tsc2102_init(qemu_irq pint, AudioState *audio)
|
987 | 3efda49d | balrog | { |
988 | 3efda49d | balrog | struct tsc210x_state_s *s;
|
989 | 3efda49d | balrog | |
990 | 3efda49d | balrog | s = (struct tsc210x_state_s *)
|
991 | 3efda49d | balrog | qemu_mallocz(sizeof(struct tsc210x_state_s)); |
992 | 3efda49d | balrog | memset(s, 0, sizeof(struct tsc210x_state_s)); |
993 | 3efda49d | balrog | s->x = 160;
|
994 | 3efda49d | balrog | s->y = 160;
|
995 | 3efda49d | balrog | s->pressure = 0;
|
996 | 3efda49d | balrog | s->precision = s->nextprecision = 0;
|
997 | 3efda49d | balrog | s->timer = qemu_new_timer(vm_clock, tsc210x_timer_tick, s); |
998 | 3efda49d | balrog | s->pint = pint; |
999 | d8f699cb | balrog | s->name = "tsc2102";
|
1000 | d8f699cb | balrog | s->audio = audio; |
1001 | 3efda49d | balrog | |
1002 | 3efda49d | balrog | s->chip.opaque = s; |
1003 | 3efda49d | balrog | s->chip.send = (void *) tsc210x_write;
|
1004 | 3efda49d | balrog | s->chip.receive = (void *) tsc210x_read;
|
1005 | 3efda49d | balrog | |
1006 | d8f699cb | balrog | s->codec.opaque = s; |
1007 | d8f699cb | balrog | s->codec.tx_swallow = (void *) tsc210x_i2s_swallow;
|
1008 | d8f699cb | balrog | s->codec.set_rate = (void *) tsc210x_i2s_set_rate;
|
1009 | d8f699cb | balrog | s->codec.in.fifo = s->in_fifo; |
1010 | d8f699cb | balrog | s->codec.out.fifo = s->out_fifo; |
1011 | d8f699cb | balrog | |
1012 | 3efda49d | balrog | tsc210x_reset(s); |
1013 | 3efda49d | balrog | |
1014 | 3efda49d | balrog | qemu_add_mouse_event_handler(tsc210x_touchscreen_event, s, 1,
|
1015 | 3efda49d | balrog | "QEMU TSC2102-driven Touchscreen");
|
1016 | 3efda49d | balrog | |
1017 | d8f699cb | balrog | if (s->audio)
|
1018 | d8f699cb | balrog | AUD_register_card(s->audio, s->name, &s->card); |
1019 | d8f699cb | balrog | |
1020 | 3efda49d | balrog | qemu_register_reset((void *) tsc210x_reset, s);
|
1021 | d8f699cb | balrog | register_savevm(s->name, tsc2102_iid ++, 0,
|
1022 | 3efda49d | balrog | tsc210x_save, tsc210x_load, s); |
1023 | 3efda49d | balrog | |
1024 | 3efda49d | balrog | return &s->chip;
|
1025 | 3efda49d | balrog | } |
1026 | d8f699cb | balrog | |
1027 | d8f699cb | balrog | struct i2s_codec_s *tsc210x_codec(struct uwire_slave_s *chip) |
1028 | d8f699cb | balrog | { |
1029 | d8f699cb | balrog | struct tsc210x_state_s *s = (struct tsc210x_state_s *) chip->opaque; |
1030 | d8f699cb | balrog | |
1031 | d8f699cb | balrog | return &s->codec;
|
1032 | d8f699cb | balrog | } |