root / hw / ads7846.c @ 7fc2f2c0
History | View | Annotate | Download (3.9 kB)
1 | fd5a3b33 | balrog | /*
|
---|---|---|---|
2 | 87283515 | balrog | * TI ADS7846 / TSC2046 chip emulation.
|
3 | fd5a3b33 | balrog | *
|
4 | fd5a3b33 | balrog | * Copyright (c) 2006 Openedhand Ltd.
|
5 | fd5a3b33 | balrog | * Written by Andrzej Zaborowski <balrog@zabor.org>
|
6 | fd5a3b33 | balrog | *
|
7 | fd5a3b33 | balrog | * This code is licensed under the GNU GPL v2.
|
8 | fd5a3b33 | balrog | */
|
9 | fd5a3b33 | balrog | |
10 | a984a69e | Paul Brook | #include "ssi.h" |
11 | 87ecb68b | pbrook | #include "console.h" |
12 | fd5a3b33 | balrog | |
13 | a984a69e | Paul Brook | typedef struct { |
14 | a984a69e | Paul Brook | SSISlave ssidev; |
15 | fd5a3b33 | balrog | qemu_irq interrupt; |
16 | fd5a3b33 | balrog | |
17 | fd5a3b33 | balrog | int input[8]; |
18 | fd5a3b33 | balrog | int pressure;
|
19 | fd5a3b33 | balrog | int noise;
|
20 | fd5a3b33 | balrog | |
21 | fd5a3b33 | balrog | int cycle;
|
22 | fd5a3b33 | balrog | int output;
|
23 | a984a69e | Paul Brook | } ADS7846State; |
24 | fd5a3b33 | balrog | |
25 | fd5a3b33 | balrog | /* Control-byte bitfields */
|
26 | fd5a3b33 | balrog | #define CB_PD0 (1 << 0) |
27 | fd5a3b33 | balrog | #define CB_PD1 (1 << 1) |
28 | fd5a3b33 | balrog | #define CB_SER (1 << 2) |
29 | fd5a3b33 | balrog | #define CB_MODE (1 << 3) |
30 | fd5a3b33 | balrog | #define CB_A0 (1 << 4) |
31 | fd5a3b33 | balrog | #define CB_A1 (1 << 5) |
32 | fd5a3b33 | balrog | #define CB_A2 (1 << 6) |
33 | fd5a3b33 | balrog | #define CB_START (1 << 7) |
34 | fd5a3b33 | balrog | |
35 | 611d7189 | balrog | #define X_AXIS_DMAX 3470 |
36 | 611d7189 | balrog | #define X_AXIS_MIN 290 |
37 | 611d7189 | balrog | #define Y_AXIS_DMAX 3450 |
38 | 611d7189 | balrog | #define Y_AXIS_MIN 200 |
39 | fd5a3b33 | balrog | |
40 | fd5a3b33 | balrog | #define ADS_VBAT 2000 |
41 | fd5a3b33 | balrog | #define ADS_VAUX 2000 |
42 | fd5a3b33 | balrog | #define ADS_TEMP0 2000 |
43 | fd5a3b33 | balrog | #define ADS_TEMP1 3000 |
44 | fd5a3b33 | balrog | #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15)) |
45 | fd5a3b33 | balrog | #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15)) |
46 | fd5a3b33 | balrog | #define ADS_Z1POS(x, y) 600 |
47 | fd5a3b33 | balrog | #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y)) |
48 | fd5a3b33 | balrog | |
49 | bc24a225 | Paul Brook | static void ads7846_int_update(ADS7846State *s) |
50 | fd5a3b33 | balrog | { |
51 | fd5a3b33 | balrog | if (s->interrupt)
|
52 | fd5a3b33 | balrog | qemu_set_irq(s->interrupt, s->pressure == 0);
|
53 | fd5a3b33 | balrog | } |
54 | fd5a3b33 | balrog | |
55 | a984a69e | Paul Brook | static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
|
56 | fd5a3b33 | balrog | { |
57 | a984a69e | Paul Brook | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
58 | fd5a3b33 | balrog | |
59 | fd5a3b33 | balrog | switch (s->cycle ++) {
|
60 | fd5a3b33 | balrog | case 0: |
61 | fd5a3b33 | balrog | if (!(value & CB_START)) {
|
62 | fd5a3b33 | balrog | s->cycle = 0;
|
63 | fd5a3b33 | balrog | break;
|
64 | fd5a3b33 | balrog | } |
65 | fd5a3b33 | balrog | |
66 | fd5a3b33 | balrog | s->output = s->input[(value >> 4) & 7]; |
67 | fd5a3b33 | balrog | |
68 | fd5a3b33 | balrog | /* Imitate the ADC noise, some drivers expect this. */
|
69 | fd5a3b33 | balrog | s->noise = (s->noise + 3) & 7; |
70 | fd5a3b33 | balrog | switch ((value >> 4) & 7) { |
71 | fd5a3b33 | balrog | case 1: s->output += s->noise ^ 2; break; |
72 | fd5a3b33 | balrog | case 3: s->output += s->noise ^ 0; break; |
73 | fd5a3b33 | balrog | case 4: s->output += s->noise ^ 7; break; |
74 | fd5a3b33 | balrog | case 5: s->output += s->noise ^ 5; break; |
75 | fd5a3b33 | balrog | } |
76 | fd5a3b33 | balrog | |
77 | fd5a3b33 | balrog | if (value & CB_MODE)
|
78 | fd5a3b33 | balrog | s->output >>= 4; /* 8 bits instead of 12 */ |
79 | fd5a3b33 | balrog | |
80 | fd5a3b33 | balrog | break;
|
81 | fd5a3b33 | balrog | case 1: |
82 | fd5a3b33 | balrog | s->cycle = 0;
|
83 | fd5a3b33 | balrog | break;
|
84 | fd5a3b33 | balrog | } |
85 | a984a69e | Paul Brook | return s->output;
|
86 | fd5a3b33 | balrog | } |
87 | fd5a3b33 | balrog | |
88 | fd5a3b33 | balrog | static void ads7846_ts_event(void *opaque, |
89 | fd5a3b33 | balrog | int x, int y, int z, int buttons_state) |
90 | fd5a3b33 | balrog | { |
91 | bc24a225 | Paul Brook | ADS7846State *s = opaque; |
92 | fd5a3b33 | balrog | |
93 | fd5a3b33 | balrog | if (buttons_state) {
|
94 | 611d7189 | balrog | x = 0x7fff - x;
|
95 | 611d7189 | balrog | s->input[1] = ADS_XPOS(x, y);
|
96 | fd5a3b33 | balrog | s->input[3] = ADS_Z1POS(x, y);
|
97 | fd5a3b33 | balrog | s->input[4] = ADS_Z2POS(x, y);
|
98 | 611d7189 | balrog | s->input[5] = ADS_YPOS(x, y);
|
99 | fd5a3b33 | balrog | } |
100 | fd5a3b33 | balrog | |
101 | fd5a3b33 | balrog | if (s->pressure == !buttons_state) {
|
102 | fd5a3b33 | balrog | s->pressure = !!buttons_state; |
103 | fd5a3b33 | balrog | |
104 | aa941b94 | balrog | ads7846_int_update(s); |
105 | fd5a3b33 | balrog | } |
106 | fd5a3b33 | balrog | } |
107 | fd5a3b33 | balrog | |
108 | aa941b94 | balrog | static void ads7846_save(QEMUFile *f, void *opaque) |
109 | aa941b94 | balrog | { |
110 | bc24a225 | Paul Brook | ADS7846State *s = (ADS7846State *) opaque; |
111 | aa941b94 | balrog | int i;
|
112 | aa941b94 | balrog | |
113 | aa941b94 | balrog | for (i = 0; i < 8; i ++) |
114 | aa941b94 | balrog | qemu_put_be32(f, s->input[i]); |
115 | aa941b94 | balrog | qemu_put_be32(f, s->noise); |
116 | aa941b94 | balrog | qemu_put_be32(f, s->cycle); |
117 | aa941b94 | balrog | qemu_put_be32(f, s->output); |
118 | aa941b94 | balrog | } |
119 | aa941b94 | balrog | |
120 | aa941b94 | balrog | static int ads7846_load(QEMUFile *f, void *opaque, int version_id) |
121 | aa941b94 | balrog | { |
122 | bc24a225 | Paul Brook | ADS7846State *s = (ADS7846State *) opaque; |
123 | aa941b94 | balrog | int i;
|
124 | aa941b94 | balrog | |
125 | aa941b94 | balrog | for (i = 0; i < 8; i ++) |
126 | aa941b94 | balrog | s->input[i] = qemu_get_be32(f); |
127 | aa941b94 | balrog | s->noise = qemu_get_be32(f); |
128 | aa941b94 | balrog | s->cycle = qemu_get_be32(f); |
129 | aa941b94 | balrog | s->output = qemu_get_be32(f); |
130 | aa941b94 | balrog | |
131 | aa941b94 | balrog | s->pressure = 0;
|
132 | aa941b94 | balrog | ads7846_int_update(s); |
133 | aa941b94 | balrog | |
134 | aa941b94 | balrog | return 0; |
135 | aa941b94 | balrog | } |
136 | aa941b94 | balrog | |
137 | 81a322d4 | Gerd Hoffmann | static int ads7846_init(SSISlave *dev) |
138 | fd5a3b33 | balrog | { |
139 | a984a69e | Paul Brook | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
140 | fd5a3b33 | balrog | |
141 | a984a69e | Paul Brook | qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
|
142 | fd5a3b33 | balrog | |
143 | fd5a3b33 | balrog | s->input[0] = ADS_TEMP0; /* TEMP0 */ |
144 | fd5a3b33 | balrog | s->input[2] = ADS_VBAT; /* VBAT */ |
145 | fd5a3b33 | balrog | s->input[6] = ADS_VAUX; /* VAUX */ |
146 | fd5a3b33 | balrog | s->input[7] = ADS_TEMP1; /* TEMP1 */ |
147 | fd5a3b33 | balrog | |
148 | fd5a3b33 | balrog | /* We want absolute coordinates */
|
149 | fd5a3b33 | balrog | qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
|
150 | fd5a3b33 | balrog | "QEMU ADS7846-driven Touchscreen");
|
151 | fd5a3b33 | balrog | |
152 | fd5a3b33 | balrog | ads7846_int_update(s); |
153 | aa941b94 | balrog | |
154 | 18be5187 | pbrook | register_savevm("ads7846", -1, 0, ads7846_save, ads7846_load, s); |
155 | 81a322d4 | Gerd Hoffmann | return 0; |
156 | a984a69e | Paul Brook | } |
157 | aa941b94 | balrog | |
158 | a984a69e | Paul Brook | static SSISlaveInfo ads7846_info = {
|
159 | 074f2fff | Gerd Hoffmann | .qdev.name ="ads7846",
|
160 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(ADS7846State),
|
161 | a984a69e | Paul Brook | .init = ads7846_init, |
162 | a984a69e | Paul Brook | .transfer = ads7846_transfer |
163 | a984a69e | Paul Brook | }; |
164 | a984a69e | Paul Brook | |
165 | a984a69e | Paul Brook | static void ads7846_register_devices(void) |
166 | a984a69e | Paul Brook | { |
167 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&ads7846_info); |
168 | fd5a3b33 | balrog | } |
169 | a984a69e | Paul Brook | |
170 | a984a69e | Paul Brook | device_init(ads7846_register_devices) |