root / hw / tsc2005.c @ e00e365e
History | View | Annotate | Download (15.2 kB)
1 | 069de562 | balrog | /*
|
---|---|---|---|
2 | 069de562 | balrog | * TI TSC2005 emulator.
|
3 | 069de562 | balrog | *
|
4 | 069de562 | balrog | * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
|
5 | 069de562 | balrog | * Copyright (C) 2008 Nokia Corporation
|
6 | 069de562 | balrog | *
|
7 | 069de562 | balrog | * This program is free software; you can redistribute it and/or
|
8 | 069de562 | balrog | * modify it under the terms of the GNU General Public License as
|
9 | 069de562 | balrog | * published by the Free Software Foundation; either version 2 or
|
10 | 069de562 | balrog | * (at your option) version 3 of the License.
|
11 | 069de562 | balrog | *
|
12 | 069de562 | balrog | * This program is distributed in the hope that it will be useful,
|
13 | 069de562 | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 069de562 | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | 069de562 | balrog | * GNU General Public License for more details.
|
16 | 069de562 | balrog | *
|
17 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
18 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 | 069de562 | balrog | */
|
20 | 069de562 | balrog | |
21 | 069de562 | balrog | #include "hw.h" |
22 | 069de562 | balrog | #include "qemu-timer.h" |
23 | 069de562 | balrog | #include "console.h" |
24 | b1d8e52e | blueswir1 | #include "devices.h" |
25 | 069de562 | balrog | |
26 | 069de562 | balrog | #define TSC_CUT_RESOLUTION(value, p) ((value) >> (16 - (p ? 12 : 10))) |
27 | 069de562 | balrog | |
28 | bc24a225 | Paul Brook | typedef struct { |
29 | 069de562 | balrog | qemu_irq pint; /* Combination of the nPENIRQ and DAV signals */
|
30 | 069de562 | balrog | QEMUTimer *timer; |
31 | 069de562 | balrog | uint16_t model; |
32 | 069de562 | balrog | |
33 | 069de562 | balrog | int x, y;
|
34 | 069de562 | balrog | int pressure;
|
35 | 069de562 | balrog | |
36 | 069de562 | balrog | int state, reg, irq, command;
|
37 | 069de562 | balrog | uint16_t data, dav; |
38 | 069de562 | balrog | |
39 | 069de562 | balrog | int busy;
|
40 | 069de562 | balrog | int enabled;
|
41 | 069de562 | balrog | int host_mode;
|
42 | 069de562 | balrog | int function;
|
43 | 069de562 | balrog | int nextfunction;
|
44 | 069de562 | balrog | int precision;
|
45 | 069de562 | balrog | int nextprecision;
|
46 | 069de562 | balrog | int filter;
|
47 | 069de562 | balrog | int pin_func;
|
48 | 069de562 | balrog | int timing[2]; |
49 | 069de562 | balrog | int noise;
|
50 | 069de562 | balrog | int reset;
|
51 | 069de562 | balrog | int pdst;
|
52 | 069de562 | balrog | int pnd0;
|
53 | 069de562 | balrog | uint16_t temp_thr[2];
|
54 | 069de562 | balrog | uint16_t aux_thr[2];
|
55 | 069de562 | balrog | |
56 | 069de562 | balrog | int tr[8]; |
57 | bc24a225 | Paul Brook | } TSC2005State; |
58 | 069de562 | balrog | |
59 | 069de562 | balrog | enum {
|
60 | 069de562 | balrog | TSC_MODE_XYZ_SCAN = 0x0,
|
61 | 069de562 | balrog | TSC_MODE_XY_SCAN, |
62 | 069de562 | balrog | TSC_MODE_X, |
63 | 069de562 | balrog | TSC_MODE_Y, |
64 | 069de562 | balrog | TSC_MODE_Z, |
65 | 069de562 | balrog | TSC_MODE_AUX, |
66 | 069de562 | balrog | TSC_MODE_TEMP1, |
67 | 069de562 | balrog | TSC_MODE_TEMP2, |
68 | 069de562 | balrog | TSC_MODE_AUX_SCAN, |
69 | 069de562 | balrog | TSC_MODE_X_TEST, |
70 | 069de562 | balrog | TSC_MODE_Y_TEST, |
71 | 069de562 | balrog | TSC_MODE_TS_TEST, |
72 | 069de562 | balrog | TSC_MODE_RESERVED, |
73 | 069de562 | balrog | TSC_MODE_XX_DRV, |
74 | 069de562 | balrog | TSC_MODE_YY_DRV, |
75 | 069de562 | balrog | TSC_MODE_YX_DRV, |
76 | 069de562 | balrog | }; |
77 | 069de562 | balrog | |
78 | 069de562 | balrog | static const uint16_t mode_regs[16] = { |
79 | 069de562 | balrog | 0xf000, /* X, Y, Z scan */ |
80 | 069de562 | balrog | 0xc000, /* X, Y scan */ |
81 | 069de562 | balrog | 0x8000, /* X */ |
82 | 069de562 | balrog | 0x4000, /* Y */ |
83 | 069de562 | balrog | 0x3000, /* Z */ |
84 | 069de562 | balrog | 0x0800, /* AUX */ |
85 | 069de562 | balrog | 0x0400, /* TEMP1 */ |
86 | 069de562 | balrog | 0x0200, /* TEMP2 */ |
87 | 069de562 | balrog | 0x0800, /* AUX scan */ |
88 | 069de562 | balrog | 0x0040, /* X test */ |
89 | 069de562 | balrog | 0x0020, /* Y test */ |
90 | 069de562 | balrog | 0x0080, /* Short-circuit test */ |
91 | 069de562 | balrog | 0x0000, /* Reserved */ |
92 | 069de562 | balrog | 0x0000, /* X+, X- drivers */ |
93 | 069de562 | balrog | 0x0000, /* Y+, Y- drivers */ |
94 | 069de562 | balrog | 0x0000, /* Y+, X- drivers */ |
95 | 069de562 | balrog | }; |
96 | 069de562 | balrog | |
97 | 069de562 | balrog | #define X_TRANSFORM(s) \
|
98 | 069de562 | balrog | ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3]) |
99 | 069de562 | balrog | #define Y_TRANSFORM(s) \
|
100 | 069de562 | balrog | ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7]) |
101 | 069de562 | balrog | #define Z1_TRANSFORM(s) \
|
102 | 069de562 | balrog | ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4) |
103 | 069de562 | balrog | #define Z2_TRANSFORM(s) \
|
104 | 069de562 | balrog | ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4) |
105 | 069de562 | balrog | |
106 | 069de562 | balrog | #define AUX_VAL (700 << 4) /* +/- 3 at 12-bit */ |
107 | 069de562 | balrog | #define TEMP1_VAL (1264 << 4) /* +/- 5 at 12-bit */ |
108 | 069de562 | balrog | #define TEMP2_VAL (1531 << 4) /* +/- 5 at 12-bit */ |
109 | 069de562 | balrog | |
110 | bc24a225 | Paul Brook | static uint16_t tsc2005_read(TSC2005State *s, int reg) |
111 | 069de562 | balrog | { |
112 | 069de562 | balrog | uint16_t ret; |
113 | 069de562 | balrog | |
114 | 069de562 | balrog | switch (reg) {
|
115 | 069de562 | balrog | case 0x0: /* X */ |
116 | 069de562 | balrog | s->dav &= ~mode_regs[TSC_MODE_X]; |
117 | 069de562 | balrog | return TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
|
118 | 069de562 | balrog | (s->noise & 3);
|
119 | 069de562 | balrog | case 0x1: /* Y */ |
120 | 069de562 | balrog | s->dav &= ~mode_regs[TSC_MODE_Y]; |
121 | 069de562 | balrog | s->noise ++; |
122 | 069de562 | balrog | return TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
|
123 | 069de562 | balrog | (s->noise & 3);
|
124 | 069de562 | balrog | case 0x2: /* Z1 */ |
125 | 069de562 | balrog | s->dav &= 0xdfff;
|
126 | 069de562 | balrog | return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
|
127 | 069de562 | balrog | (s->noise & 3);
|
128 | 069de562 | balrog | case 0x3: /* Z2 */ |
129 | 069de562 | balrog | s->dav &= 0xefff;
|
130 | 069de562 | balrog | return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
|
131 | 069de562 | balrog | (s->noise & 3);
|
132 | 069de562 | balrog | |
133 | 069de562 | balrog | case 0x4: /* AUX */ |
134 | 069de562 | balrog | s->dav &= ~mode_regs[TSC_MODE_AUX]; |
135 | 069de562 | balrog | return TSC_CUT_RESOLUTION(AUX_VAL, s->precision);
|
136 | 069de562 | balrog | |
137 | 069de562 | balrog | case 0x5: /* TEMP1 */ |
138 | 069de562 | balrog | s->dav &= ~mode_regs[TSC_MODE_TEMP1]; |
139 | 069de562 | balrog | return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
|
140 | 069de562 | balrog | (s->noise & 5);
|
141 | 069de562 | balrog | case 0x6: /* TEMP2 */ |
142 | 069de562 | balrog | s->dav &= 0xdfff;
|
143 | 069de562 | balrog | s->dav &= ~mode_regs[TSC_MODE_TEMP2]; |
144 | 069de562 | balrog | return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
|
145 | 069de562 | balrog | (s->noise & 3);
|
146 | 069de562 | balrog | |
147 | 069de562 | balrog | case 0x7: /* Status */ |
148 | 069de562 | balrog | ret = s->dav | (s->reset << 7) | (s->pdst << 2) | 0x0; |
149 | 069de562 | balrog | s->dav &= ~(mode_regs[TSC_MODE_X_TEST] | mode_regs[TSC_MODE_Y_TEST] | |
150 | 069de562 | balrog | mode_regs[TSC_MODE_TS_TEST]); |
151 | 069de562 | balrog | s->reset = 1;
|
152 | 069de562 | balrog | return ret;
|
153 | 069de562 | balrog | |
154 | 069de562 | balrog | case 0x8: /* AUX high treshold */ |
155 | 069de562 | balrog | return s->aux_thr[1]; |
156 | 069de562 | balrog | case 0x9: /* AUX low treshold */ |
157 | 069de562 | balrog | return s->aux_thr[0]; |
158 | 069de562 | balrog | |
159 | 069de562 | balrog | case 0xa: /* TEMP high treshold */ |
160 | 069de562 | balrog | return s->temp_thr[1]; |
161 | 069de562 | balrog | case 0xb: /* TEMP low treshold */ |
162 | 069de562 | balrog | return s->temp_thr[0]; |
163 | 069de562 | balrog | |
164 | 069de562 | balrog | case 0xc: /* CFR0 */ |
165 | 069de562 | balrog | return (s->pressure << 15) | ((!s->busy) << 14) | |
166 | 069de562 | balrog | (s->nextprecision << 13) | s->timing[0]; |
167 | 069de562 | balrog | case 0xd: /* CFR1 */ |
168 | 069de562 | balrog | return s->timing[1]; |
169 | 069de562 | balrog | case 0xe: /* CFR2 */ |
170 | 069de562 | balrog | return (s->pin_func << 14) | s->filter; |
171 | 069de562 | balrog | |
172 | 069de562 | balrog | case 0xf: /* Function select status */ |
173 | 069de562 | balrog | return s->function >= 0 ? 1 << s->function : 0; |
174 | 069de562 | balrog | } |
175 | 069de562 | balrog | |
176 | 069de562 | balrog | /* Never gets here */
|
177 | 069de562 | balrog | return 0xffff; |
178 | 069de562 | balrog | } |
179 | 069de562 | balrog | |
180 | bc24a225 | Paul Brook | static void tsc2005_write(TSC2005State *s, int reg, uint16_t data) |
181 | 069de562 | balrog | { |
182 | 069de562 | balrog | switch (reg) {
|
183 | 069de562 | balrog | case 0x8: /* AUX high treshold */ |
184 | 069de562 | balrog | s->aux_thr[1] = data;
|
185 | 069de562 | balrog | break;
|
186 | 069de562 | balrog | case 0x9: /* AUX low treshold */ |
187 | 069de562 | balrog | s->aux_thr[0] = data;
|
188 | 069de562 | balrog | break;
|
189 | 069de562 | balrog | |
190 | 069de562 | balrog | case 0xa: /* TEMP high treshold */ |
191 | 069de562 | balrog | s->temp_thr[1] = data;
|
192 | 069de562 | balrog | break;
|
193 | 069de562 | balrog | case 0xb: /* TEMP low treshold */ |
194 | 069de562 | balrog | s->temp_thr[0] = data;
|
195 | 069de562 | balrog | break;
|
196 | 069de562 | balrog | |
197 | 069de562 | balrog | case 0xc: /* CFR0 */ |
198 | 069de562 | balrog | s->host_mode = data >> 15;
|
199 | 0941041e | balrog | if (s->enabled != !(data & 0x4000)) { |
200 | 0941041e | balrog | s->enabled = !(data & 0x4000);
|
201 | 0941041e | balrog | fprintf(stderr, "%s: touchscreen sense %sabled\n",
|
202 | 0941041e | balrog | __FUNCTION__, s->enabled ? "en" : "dis"); |
203 | 0941041e | balrog | if (s->busy && !s->enabled)
|
204 | 0941041e | balrog | qemu_del_timer(s->timer); |
205 | 0941041e | balrog | s->busy &= s->enabled; |
206 | 0941041e | balrog | } |
207 | 069de562 | balrog | s->nextprecision = (data >> 13) & 1; |
208 | 069de562 | balrog | s->timing[0] = data & 0x1fff; |
209 | 069de562 | balrog | if ((s->timing[0] >> 11) == 3) |
210 | 069de562 | balrog | fprintf(stderr, "%s: illegal conversion clock setting\n",
|
211 | 069de562 | balrog | __FUNCTION__); |
212 | 069de562 | balrog | break;
|
213 | 069de562 | balrog | case 0xd: /* CFR1 */ |
214 | 069de562 | balrog | s->timing[1] = data & 0xf07; |
215 | 069de562 | balrog | break;
|
216 | 069de562 | balrog | case 0xe: /* CFR2 */ |
217 | 069de562 | balrog | s->pin_func = (data >> 14) & 3; |
218 | 069de562 | balrog | s->filter = data & 0x3fff;
|
219 | 069de562 | balrog | break;
|
220 | 069de562 | balrog | |
221 | 069de562 | balrog | default:
|
222 | 069de562 | balrog | fprintf(stderr, "%s: write into read-only register %x\n",
|
223 | 069de562 | balrog | __FUNCTION__, reg); |
224 | 069de562 | balrog | } |
225 | 069de562 | balrog | } |
226 | 069de562 | balrog | |
227 | 069de562 | balrog | /* This handles most of the chip's logic. */
|
228 | bc24a225 | Paul Brook | static void tsc2005_pin_update(TSC2005State *s) |
229 | 069de562 | balrog | { |
230 | 069de562 | balrog | int64_t expires; |
231 | 069de562 | balrog | int pin_state;
|
232 | 069de562 | balrog | |
233 | 069de562 | balrog | switch (s->pin_func) {
|
234 | 069de562 | balrog | case 0: |
235 | 069de562 | balrog | pin_state = !s->pressure && !!s->dav; |
236 | 069de562 | balrog | break;
|
237 | 069de562 | balrog | case 1: |
238 | 069de562 | balrog | case 3: |
239 | 069de562 | balrog | default:
|
240 | 069de562 | balrog | pin_state = !s->dav; |
241 | 069de562 | balrog | break;
|
242 | 069de562 | balrog | case 2: |
243 | 069de562 | balrog | pin_state = !s->pressure; |
244 | 069de562 | balrog | } |
245 | 069de562 | balrog | |
246 | 069de562 | balrog | if (pin_state != s->irq) {
|
247 | 069de562 | balrog | s->irq = pin_state; |
248 | 069de562 | balrog | qemu_set_irq(s->pint, s->irq); |
249 | 069de562 | balrog | } |
250 | 069de562 | balrog | |
251 | 069de562 | balrog | switch (s->nextfunction) {
|
252 | 069de562 | balrog | case TSC_MODE_XYZ_SCAN:
|
253 | 069de562 | balrog | case TSC_MODE_XY_SCAN:
|
254 | e62ab7a1 | balrog | if (!s->host_mode && s->dav)
|
255 | e62ab7a1 | balrog | s->enabled = 0;
|
256 | 069de562 | balrog | if (!s->pressure)
|
257 | 069de562 | balrog | return;
|
258 | 069de562 | balrog | /* Fall through */
|
259 | 069de562 | balrog | case TSC_MODE_AUX_SCAN:
|
260 | 069de562 | balrog | break;
|
261 | 069de562 | balrog | |
262 | 069de562 | balrog | case TSC_MODE_X:
|
263 | 069de562 | balrog | case TSC_MODE_Y:
|
264 | 069de562 | balrog | case TSC_MODE_Z:
|
265 | 069de562 | balrog | if (!s->pressure)
|
266 | 069de562 | balrog | return;
|
267 | 069de562 | balrog | /* Fall through */
|
268 | 069de562 | balrog | case TSC_MODE_AUX:
|
269 | 069de562 | balrog | case TSC_MODE_TEMP1:
|
270 | 069de562 | balrog | case TSC_MODE_TEMP2:
|
271 | 069de562 | balrog | case TSC_MODE_X_TEST:
|
272 | 069de562 | balrog | case TSC_MODE_Y_TEST:
|
273 | 069de562 | balrog | case TSC_MODE_TS_TEST:
|
274 | 069de562 | balrog | if (s->dav)
|
275 | 069de562 | balrog | s->enabled = 0;
|
276 | 069de562 | balrog | break;
|
277 | 069de562 | balrog | |
278 | 069de562 | balrog | case TSC_MODE_RESERVED:
|
279 | 069de562 | balrog | case TSC_MODE_XX_DRV:
|
280 | 069de562 | balrog | case TSC_MODE_YY_DRV:
|
281 | 069de562 | balrog | case TSC_MODE_YX_DRV:
|
282 | 069de562 | balrog | default:
|
283 | 069de562 | balrog | return;
|
284 | 069de562 | balrog | } |
285 | 069de562 | balrog | |
286 | 069de562 | balrog | if (!s->enabled || s->busy)
|
287 | 069de562 | balrog | return;
|
288 | 069de562 | balrog | |
289 | 069de562 | balrog | s->busy = 1;
|
290 | 069de562 | balrog | s->precision = s->nextprecision; |
291 | 069de562 | balrog | s->function = s->nextfunction; |
292 | 069de562 | balrog | s->pdst = !s->pnd0; /* Synchronised on internal clock */
|
293 | 6ee093c9 | Juan Quintela | expires = qemu_get_clock(vm_clock) + (get_ticks_per_sec() >> 7);
|
294 | 069de562 | balrog | qemu_mod_timer(s->timer, expires); |
295 | 069de562 | balrog | } |
296 | 069de562 | balrog | |
297 | bc24a225 | Paul Brook | static void tsc2005_reset(TSC2005State *s) |
298 | 069de562 | balrog | { |
299 | 069de562 | balrog | s->state = 0;
|
300 | 069de562 | balrog | s->pin_func = 0;
|
301 | 069de562 | balrog | s->enabled = 0;
|
302 | 069de562 | balrog | s->busy = 0;
|
303 | 069de562 | balrog | s->nextprecision = 0;
|
304 | 069de562 | balrog | s->nextfunction = 0;
|
305 | 069de562 | balrog | s->timing[0] = 0; |
306 | 069de562 | balrog | s->timing[1] = 0; |
307 | 069de562 | balrog | s->irq = 0;
|
308 | 069de562 | balrog | s->dav = 0;
|
309 | 069de562 | balrog | s->reset = 0;
|
310 | 069de562 | balrog | s->pdst = 1;
|
311 | 069de562 | balrog | s->pnd0 = 0;
|
312 | 069de562 | balrog | s->function = -1;
|
313 | 069de562 | balrog | s->temp_thr[0] = 0x000; |
314 | 069de562 | balrog | s->temp_thr[1] = 0xfff; |
315 | 069de562 | balrog | s->aux_thr[0] = 0x000; |
316 | 069de562 | balrog | s->aux_thr[1] = 0xfff; |
317 | 069de562 | balrog | |
318 | 069de562 | balrog | tsc2005_pin_update(s); |
319 | 069de562 | balrog | } |
320 | 069de562 | balrog | |
321 | b1d8e52e | blueswir1 | static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value) |
322 | 069de562 | balrog | { |
323 | bc24a225 | Paul Brook | TSC2005State *s = opaque; |
324 | 069de562 | balrog | uint32_t ret = 0;
|
325 | 069de562 | balrog | |
326 | 069de562 | balrog | switch (s->state ++) {
|
327 | 069de562 | balrog | case 0: |
328 | 069de562 | balrog | if (value & 0x80) { |
329 | 069de562 | balrog | /* Command */
|
330 | 069de562 | balrog | if (value & (1 << 1)) |
331 | 069de562 | balrog | tsc2005_reset(s); |
332 | 069de562 | balrog | else {
|
333 | 069de562 | balrog | s->nextfunction = (value >> 3) & 0xf; |
334 | 069de562 | balrog | s->nextprecision = (value >> 2) & 1; |
335 | 069de562 | balrog | if (s->enabled != !(value & 1)) { |
336 | 069de562 | balrog | s->enabled = !(value & 1);
|
337 | 069de562 | balrog | fprintf(stderr, "%s: touchscreen sense %sabled\n",
|
338 | 069de562 | balrog | __FUNCTION__, s->enabled ? "en" : "dis"); |
339 | e62ab7a1 | balrog | if (s->busy && !s->enabled)
|
340 | e62ab7a1 | balrog | qemu_del_timer(s->timer); |
341 | e62ab7a1 | balrog | s->busy &= s->enabled; |
342 | 069de562 | balrog | } |
343 | 069de562 | balrog | tsc2005_pin_update(s); |
344 | 069de562 | balrog | } |
345 | 069de562 | balrog | |
346 | 069de562 | balrog | s->state = 0;
|
347 | 069de562 | balrog | } else if (value) { |
348 | 069de562 | balrog | /* Data transfer */
|
349 | 069de562 | balrog | s->reg = (value >> 3) & 0xf; |
350 | 069de562 | balrog | s->pnd0 = (value >> 1) & 1; |
351 | 069de562 | balrog | s->command = value & 1;
|
352 | 069de562 | balrog | |
353 | 069de562 | balrog | if (s->command) {
|
354 | 069de562 | balrog | /* Read */
|
355 | 069de562 | balrog | s->data = tsc2005_read(s, s->reg); |
356 | 069de562 | balrog | tsc2005_pin_update(s); |
357 | 069de562 | balrog | } else
|
358 | 069de562 | balrog | s->data = 0;
|
359 | 069de562 | balrog | } else
|
360 | 069de562 | balrog | s->state = 0;
|
361 | 069de562 | balrog | break;
|
362 | 069de562 | balrog | |
363 | 069de562 | balrog | case 1: |
364 | 069de562 | balrog | if (s->command)
|
365 | 069de562 | balrog | ret = (s->data >> 8) & 0xff; |
366 | 069de562 | balrog | else
|
367 | 069de562 | balrog | s->data |= value << 8;
|
368 | 069de562 | balrog | break;
|
369 | 069de562 | balrog | |
370 | 069de562 | balrog | case 2: |
371 | 069de562 | balrog | if (s->command)
|
372 | 069de562 | balrog | ret = s->data & 0xff;
|
373 | 069de562 | balrog | else {
|
374 | 069de562 | balrog | s->data |= value; |
375 | 069de562 | balrog | tsc2005_write(s, s->reg, s->data); |
376 | 069de562 | balrog | tsc2005_pin_update(s); |
377 | 069de562 | balrog | } |
378 | 069de562 | balrog | |
379 | 069de562 | balrog | s->state = 0;
|
380 | 069de562 | balrog | break;
|
381 | 069de562 | balrog | } |
382 | 069de562 | balrog | |
383 | 069de562 | balrog | return ret;
|
384 | 069de562 | balrog | } |
385 | 069de562 | balrog | |
386 | 069de562 | balrog | uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len) |
387 | 069de562 | balrog | { |
388 | 069de562 | balrog | uint32_t ret = 0;
|
389 | 069de562 | balrog | |
390 | 069de562 | balrog | len &= ~7;
|
391 | 069de562 | balrog | while (len > 0) { |
392 | 069de562 | balrog | len -= 8;
|
393 | 069de562 | balrog | ret |= tsc2005_txrx_word(opaque, (value >> len) & 0xff) << len;
|
394 | 069de562 | balrog | } |
395 | 069de562 | balrog | |
396 | 069de562 | balrog | return ret;
|
397 | 069de562 | balrog | } |
398 | 069de562 | balrog | |
399 | 069de562 | balrog | static void tsc2005_timer_tick(void *opaque) |
400 | 069de562 | balrog | { |
401 | bc24a225 | Paul Brook | TSC2005State *s = opaque; |
402 | 069de562 | balrog | |
403 | 069de562 | balrog | /* Timer ticked -- a set of conversions has been finished. */
|
404 | 069de562 | balrog | |
405 | 069de562 | balrog | if (!s->busy)
|
406 | 069de562 | balrog | return;
|
407 | 069de562 | balrog | |
408 | 069de562 | balrog | s->busy = 0;
|
409 | 069de562 | balrog | s->dav |= mode_regs[s->function]; |
410 | 069de562 | balrog | s->function = -1;
|
411 | 069de562 | balrog | tsc2005_pin_update(s); |
412 | 069de562 | balrog | } |
413 | 069de562 | balrog | |
414 | 069de562 | balrog | static void tsc2005_touchscreen_event(void *opaque, |
415 | 069de562 | balrog | int x, int y, int z, int buttons_state) |
416 | 069de562 | balrog | { |
417 | bc24a225 | Paul Brook | TSC2005State *s = opaque; |
418 | 069de562 | balrog | int p = s->pressure;
|
419 | 069de562 | balrog | |
420 | 069de562 | balrog | if (buttons_state) {
|
421 | 069de562 | balrog | s->x = x; |
422 | 069de562 | balrog | s->y = y; |
423 | 069de562 | balrog | } |
424 | 069de562 | balrog | s->pressure = !!buttons_state; |
425 | 069de562 | balrog | |
426 | 069de562 | balrog | /*
|
427 | 069de562 | balrog | * Note: We would get better responsiveness in the guest by
|
428 | 069de562 | balrog | * signaling TS events immediately, but for now we simulate
|
429 | 069de562 | balrog | * the first conversion delay for sake of correctness.
|
430 | 069de562 | balrog | */
|
431 | 069de562 | balrog | if (p != s->pressure)
|
432 | 069de562 | balrog | tsc2005_pin_update(s); |
433 | 069de562 | balrog | } |
434 | 069de562 | balrog | |
435 | 069de562 | balrog | static void tsc2005_save(QEMUFile *f, void *opaque) |
436 | 069de562 | balrog | { |
437 | bc24a225 | Paul Brook | TSC2005State *s = (TSC2005State *) opaque; |
438 | 069de562 | balrog | int i;
|
439 | 069de562 | balrog | |
440 | 069de562 | balrog | qemu_put_be16(f, s->x); |
441 | 069de562 | balrog | qemu_put_be16(f, s->y); |
442 | 069de562 | balrog | qemu_put_byte(f, s->pressure); |
443 | 069de562 | balrog | |
444 | 069de562 | balrog | qemu_put_byte(f, s->state); |
445 | 069de562 | balrog | qemu_put_byte(f, s->reg); |
446 | 069de562 | balrog | qemu_put_byte(f, s->command); |
447 | 069de562 | balrog | |
448 | 069de562 | balrog | qemu_put_byte(f, s->irq); |
449 | 069de562 | balrog | qemu_put_be16s(f, &s->dav); |
450 | 069de562 | balrog | qemu_put_be16s(f, &s->data); |
451 | 069de562 | balrog | |
452 | 069de562 | balrog | qemu_put_timer(f, s->timer); |
453 | 069de562 | balrog | qemu_put_byte(f, s->enabled); |
454 | 069de562 | balrog | qemu_put_byte(f, s->host_mode); |
455 | 069de562 | balrog | qemu_put_byte(f, s->function); |
456 | 069de562 | balrog | qemu_put_byte(f, s->nextfunction); |
457 | 069de562 | balrog | qemu_put_byte(f, s->precision); |
458 | 069de562 | balrog | qemu_put_byte(f, s->nextprecision); |
459 | 069de562 | balrog | qemu_put_be16(f, s->filter); |
460 | 069de562 | balrog | qemu_put_byte(f, s->pin_func); |
461 | 069de562 | balrog | qemu_put_be16(f, s->timing[0]);
|
462 | 069de562 | balrog | qemu_put_be16(f, s->timing[1]);
|
463 | 069de562 | balrog | qemu_put_be16s(f, &s->temp_thr[0]);
|
464 | 069de562 | balrog | qemu_put_be16s(f, &s->temp_thr[1]);
|
465 | 069de562 | balrog | qemu_put_be16s(f, &s->aux_thr[0]);
|
466 | 069de562 | balrog | qemu_put_be16s(f, &s->aux_thr[1]);
|
467 | 069de562 | balrog | qemu_put_be32(f, s->noise); |
468 | 069de562 | balrog | qemu_put_byte(f, s->reset); |
469 | 069de562 | balrog | qemu_put_byte(f, s->pdst); |
470 | 069de562 | balrog | qemu_put_byte(f, s->pnd0); |
471 | 069de562 | balrog | |
472 | 069de562 | balrog | for (i = 0; i < 8; i ++) |
473 | 069de562 | balrog | qemu_put_be32(f, s->tr[i]); |
474 | 069de562 | balrog | } |
475 | 069de562 | balrog | |
476 | 069de562 | balrog | static int tsc2005_load(QEMUFile *f, void *opaque, int version_id) |
477 | 069de562 | balrog | { |
478 | bc24a225 | Paul Brook | TSC2005State *s = (TSC2005State *) opaque; |
479 | 069de562 | balrog | int i;
|
480 | 069de562 | balrog | |
481 | 069de562 | balrog | s->x = qemu_get_be16(f); |
482 | 069de562 | balrog | s->y = qemu_get_be16(f); |
483 | 069de562 | balrog | s->pressure = qemu_get_byte(f); |
484 | 069de562 | balrog | |
485 | 069de562 | balrog | s->state = qemu_get_byte(f); |
486 | 069de562 | balrog | s->reg = qemu_get_byte(f); |
487 | 069de562 | balrog | s->command = qemu_get_byte(f); |
488 | 069de562 | balrog | |
489 | 069de562 | balrog | s->irq = qemu_get_byte(f); |
490 | 069de562 | balrog | qemu_get_be16s(f, &s->dav); |
491 | 069de562 | balrog | qemu_get_be16s(f, &s->data); |
492 | 069de562 | balrog | |
493 | 069de562 | balrog | qemu_get_timer(f, s->timer); |
494 | 069de562 | balrog | s->enabled = qemu_get_byte(f); |
495 | 069de562 | balrog | s->host_mode = qemu_get_byte(f); |
496 | 069de562 | balrog | s->function = qemu_get_byte(f); |
497 | 069de562 | balrog | s->nextfunction = qemu_get_byte(f); |
498 | 069de562 | balrog | s->precision = qemu_get_byte(f); |
499 | 069de562 | balrog | s->nextprecision = qemu_get_byte(f); |
500 | 069de562 | balrog | s->filter = qemu_get_be16(f); |
501 | 069de562 | balrog | s->pin_func = qemu_get_byte(f); |
502 | 069de562 | balrog | s->timing[0] = qemu_get_be16(f);
|
503 | 069de562 | balrog | s->timing[1] = qemu_get_be16(f);
|
504 | 069de562 | balrog | qemu_get_be16s(f, &s->temp_thr[0]);
|
505 | 069de562 | balrog | qemu_get_be16s(f, &s->temp_thr[1]);
|
506 | 069de562 | balrog | qemu_get_be16s(f, &s->aux_thr[0]);
|
507 | 069de562 | balrog | qemu_get_be16s(f, &s->aux_thr[1]);
|
508 | 069de562 | balrog | s->noise = qemu_get_be32(f); |
509 | 069de562 | balrog | s->reset = qemu_get_byte(f); |
510 | 069de562 | balrog | s->pdst = qemu_get_byte(f); |
511 | 069de562 | balrog | s->pnd0 = qemu_get_byte(f); |
512 | 069de562 | balrog | |
513 | 069de562 | balrog | for (i = 0; i < 8; i ++) |
514 | 069de562 | balrog | s->tr[i] = qemu_get_be32(f); |
515 | 069de562 | balrog | |
516 | 069de562 | balrog | s->busy = qemu_timer_pending(s->timer); |
517 | 069de562 | balrog | tsc2005_pin_update(s); |
518 | 069de562 | balrog | |
519 | 069de562 | balrog | return 0; |
520 | 069de562 | balrog | } |
521 | 069de562 | balrog | |
522 | 069de562 | balrog | void *tsc2005_init(qemu_irq pintdav)
|
523 | 069de562 | balrog | { |
524 | bc24a225 | Paul Brook | TSC2005State *s; |
525 | 069de562 | balrog | |
526 | bc24a225 | Paul Brook | s = (TSC2005State *) |
527 | bc24a225 | Paul Brook | qemu_mallocz(sizeof(TSC2005State));
|
528 | 069de562 | balrog | s->x = 400;
|
529 | 069de562 | balrog | s->y = 240;
|
530 | 069de562 | balrog | s->pressure = 0;
|
531 | 069de562 | balrog | s->precision = s->nextprecision = 0;
|
532 | 069de562 | balrog | s->timer = qemu_new_timer(vm_clock, tsc2005_timer_tick, s); |
533 | 069de562 | balrog | s->pint = pintdav; |
534 | 069de562 | balrog | s->model = 0x2005;
|
535 | 069de562 | balrog | |
536 | 069de562 | balrog | s->tr[0] = 0; |
537 | 069de562 | balrog | s->tr[1] = 1; |
538 | 069de562 | balrog | s->tr[2] = 1; |
539 | 069de562 | balrog | s->tr[3] = 0; |
540 | 069de562 | balrog | s->tr[4] = 1; |
541 | 069de562 | balrog | s->tr[5] = 0; |
542 | 069de562 | balrog | s->tr[6] = 1; |
543 | 069de562 | balrog | s->tr[7] = 0; |
544 | 069de562 | balrog | |
545 | 069de562 | balrog | tsc2005_reset(s); |
546 | 069de562 | balrog | |
547 | 069de562 | balrog | qemu_add_mouse_event_handler(tsc2005_touchscreen_event, s, 1,
|
548 | 069de562 | balrog | "QEMU TSC2005-driven Touchscreen");
|
549 | 069de562 | balrog | |
550 | a08d4367 | Jan Kiszka | qemu_register_reset((void *) tsc2005_reset, s);
|
551 | 18be5187 | pbrook | register_savevm("tsc2005", -1, 0, tsc2005_save, tsc2005_load, s); |
552 | 069de562 | balrog | |
553 | 069de562 | balrog | return s;
|
554 | 069de562 | balrog | } |
555 | 069de562 | balrog | |
556 | 069de562 | balrog | /*
|
557 | 069de562 | balrog | * Use tslib generated calibration data to generate ADC input values
|
558 | 069de562 | balrog | * from the touchscreen. Assuming 12-bit precision was used during
|
559 | 069de562 | balrog | * tslib calibration.
|
560 | 069de562 | balrog | */
|
561 | bc24a225 | Paul Brook | void tsc2005_set_transform(void *opaque, MouseTransformInfo *info) |
562 | 069de562 | balrog | { |
563 | bc24a225 | Paul Brook | TSC2005State *s = (TSC2005State *) opaque; |
564 | 069de562 | balrog | |
565 | 069de562 | balrog | /* This version assumes touchscreen X & Y axis are parallel or
|
566 | 069de562 | balrog | * perpendicular to LCD's X & Y axis in some way. */
|
567 | 069de562 | balrog | if (abs(info->a[0]) > abs(info->a[1])) { |
568 | 069de562 | balrog | s->tr[0] = 0; |
569 | 069de562 | balrog | s->tr[1] = -info->a[6] * info->x; |
570 | 069de562 | balrog | s->tr[2] = info->a[0]; |
571 | 069de562 | balrog | s->tr[3] = -info->a[2] / info->a[0]; |
572 | 069de562 | balrog | s->tr[4] = info->a[6] * info->y; |
573 | 069de562 | balrog | s->tr[5] = 0; |
574 | 069de562 | balrog | s->tr[6] = info->a[4]; |
575 | 069de562 | balrog | s->tr[7] = -info->a[5] / info->a[4]; |
576 | 069de562 | balrog | } else {
|
577 | 069de562 | balrog | s->tr[0] = info->a[6] * info->y; |
578 | 069de562 | balrog | s->tr[1] = 0; |
579 | 069de562 | balrog | s->tr[2] = info->a[1]; |
580 | 069de562 | balrog | s->tr[3] = -info->a[2] / info->a[1]; |
581 | 069de562 | balrog | s->tr[4] = 0; |
582 | 069de562 | balrog | s->tr[5] = -info->a[6] * info->x; |
583 | 069de562 | balrog | s->tr[6] = info->a[3]; |
584 | 069de562 | balrog | s->tr[7] = -info->a[5] / info->a[3]; |
585 | 069de562 | balrog | } |
586 | 069de562 | balrog | |
587 | 069de562 | balrog | s->tr[0] >>= 11; |
588 | 069de562 | balrog | s->tr[1] >>= 11; |
589 | 069de562 | balrog | s->tr[3] <<= 4; |
590 | 069de562 | balrog | s->tr[4] >>= 11; |
591 | 069de562 | balrog | s->tr[5] >>= 11; |
592 | 069de562 | balrog | s->tr[7] <<= 4; |
593 | 069de562 | balrog | } |