root / hw / ads7846.c @ f25608e9
History | View | Annotate | Download (4.2 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 | 6b620ca3 | Paolo Bonzini | *
|
9 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
10 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
11 | fd5a3b33 | balrog | */
|
12 | fd5a3b33 | balrog | |
13 | a984a69e | Paul Brook | #include "ssi.h" |
14 | 87ecb68b | pbrook | #include "console.h" |
15 | fd5a3b33 | balrog | |
16 | a984a69e | Paul Brook | typedef struct { |
17 | a984a69e | Paul Brook | SSISlave ssidev; |
18 | fd5a3b33 | balrog | qemu_irq interrupt; |
19 | fd5a3b33 | balrog | |
20 | fd5a3b33 | balrog | int input[8]; |
21 | fd5a3b33 | balrog | int pressure;
|
22 | fd5a3b33 | balrog | int noise;
|
23 | fd5a3b33 | balrog | |
24 | fd5a3b33 | balrog | int cycle;
|
25 | fd5a3b33 | balrog | int output;
|
26 | a984a69e | Paul Brook | } ADS7846State; |
27 | fd5a3b33 | balrog | |
28 | fd5a3b33 | balrog | /* Control-byte bitfields */
|
29 | fd5a3b33 | balrog | #define CB_PD0 (1 << 0) |
30 | fd5a3b33 | balrog | #define CB_PD1 (1 << 1) |
31 | fd5a3b33 | balrog | #define CB_SER (1 << 2) |
32 | fd5a3b33 | balrog | #define CB_MODE (1 << 3) |
33 | fd5a3b33 | balrog | #define CB_A0 (1 << 4) |
34 | fd5a3b33 | balrog | #define CB_A1 (1 << 5) |
35 | fd5a3b33 | balrog | #define CB_A2 (1 << 6) |
36 | fd5a3b33 | balrog | #define CB_START (1 << 7) |
37 | fd5a3b33 | balrog | |
38 | 611d7189 | balrog | #define X_AXIS_DMAX 3470 |
39 | 611d7189 | balrog | #define X_AXIS_MIN 290 |
40 | 611d7189 | balrog | #define Y_AXIS_DMAX 3450 |
41 | 611d7189 | balrog | #define Y_AXIS_MIN 200 |
42 | fd5a3b33 | balrog | |
43 | fd5a3b33 | balrog | #define ADS_VBAT 2000 |
44 | fd5a3b33 | balrog | #define ADS_VAUX 2000 |
45 | fd5a3b33 | balrog | #define ADS_TEMP0 2000 |
46 | fd5a3b33 | balrog | #define ADS_TEMP1 3000 |
47 | fd5a3b33 | balrog | #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15)) |
48 | fd5a3b33 | balrog | #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15)) |
49 | fd5a3b33 | balrog | #define ADS_Z1POS(x, y) 600 |
50 | fd5a3b33 | balrog | #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y)) |
51 | fd5a3b33 | balrog | |
52 | bc24a225 | Paul Brook | static void ads7846_int_update(ADS7846State *s) |
53 | fd5a3b33 | balrog | { |
54 | fd5a3b33 | balrog | if (s->interrupt)
|
55 | fd5a3b33 | balrog | qemu_set_irq(s->interrupt, s->pressure == 0);
|
56 | fd5a3b33 | balrog | } |
57 | fd5a3b33 | balrog | |
58 | a984a69e | Paul Brook | static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
|
59 | fd5a3b33 | balrog | { |
60 | a984a69e | Paul Brook | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
61 | fd5a3b33 | balrog | |
62 | fd5a3b33 | balrog | switch (s->cycle ++) {
|
63 | fd5a3b33 | balrog | case 0: |
64 | fd5a3b33 | balrog | if (!(value & CB_START)) {
|
65 | fd5a3b33 | balrog | s->cycle = 0;
|
66 | fd5a3b33 | balrog | break;
|
67 | fd5a3b33 | balrog | } |
68 | fd5a3b33 | balrog | |
69 | fd5a3b33 | balrog | s->output = s->input[(value >> 4) & 7]; |
70 | fd5a3b33 | balrog | |
71 | fd5a3b33 | balrog | /* Imitate the ADC noise, some drivers expect this. */
|
72 | fd5a3b33 | balrog | s->noise = (s->noise + 3) & 7; |
73 | fd5a3b33 | balrog | switch ((value >> 4) & 7) { |
74 | fd5a3b33 | balrog | case 1: s->output += s->noise ^ 2; break; |
75 | fd5a3b33 | balrog | case 3: s->output += s->noise ^ 0; break; |
76 | fd5a3b33 | balrog | case 4: s->output += s->noise ^ 7; break; |
77 | fd5a3b33 | balrog | case 5: s->output += s->noise ^ 5; break; |
78 | fd5a3b33 | balrog | } |
79 | fd5a3b33 | balrog | |
80 | fd5a3b33 | balrog | if (value & CB_MODE)
|
81 | fd5a3b33 | balrog | s->output >>= 4; /* 8 bits instead of 12 */ |
82 | fd5a3b33 | balrog | |
83 | fd5a3b33 | balrog | break;
|
84 | fd5a3b33 | balrog | case 1: |
85 | fd5a3b33 | balrog | s->cycle = 0;
|
86 | fd5a3b33 | balrog | break;
|
87 | fd5a3b33 | balrog | } |
88 | a984a69e | Paul Brook | return s->output;
|
89 | fd5a3b33 | balrog | } |
90 | fd5a3b33 | balrog | |
91 | fd5a3b33 | balrog | static void ads7846_ts_event(void *opaque, |
92 | fd5a3b33 | balrog | int x, int y, int z, int buttons_state) |
93 | fd5a3b33 | balrog | { |
94 | bc24a225 | Paul Brook | ADS7846State *s = opaque; |
95 | fd5a3b33 | balrog | |
96 | fd5a3b33 | balrog | if (buttons_state) {
|
97 | 611d7189 | balrog | x = 0x7fff - x;
|
98 | 611d7189 | balrog | s->input[1] = ADS_XPOS(x, y);
|
99 | fd5a3b33 | balrog | s->input[3] = ADS_Z1POS(x, y);
|
100 | fd5a3b33 | balrog | s->input[4] = ADS_Z2POS(x, y);
|
101 | 611d7189 | balrog | s->input[5] = ADS_YPOS(x, y);
|
102 | fd5a3b33 | balrog | } |
103 | fd5a3b33 | balrog | |
104 | fd5a3b33 | balrog | if (s->pressure == !buttons_state) {
|
105 | fd5a3b33 | balrog | s->pressure = !!buttons_state; |
106 | fd5a3b33 | balrog | |
107 | aa941b94 | balrog | ads7846_int_update(s); |
108 | fd5a3b33 | balrog | } |
109 | fd5a3b33 | balrog | } |
110 | fd5a3b33 | balrog | |
111 | aefe2129 | Juan Quintela | static int ads7856_post_load(void *opaque, int version_id) |
112 | aa941b94 | balrog | { |
113 | aefe2129 | Juan Quintela | ADS7846State *s = opaque; |
114 | aa941b94 | balrog | |
115 | aa941b94 | balrog | s->pressure = 0;
|
116 | aa941b94 | balrog | ads7846_int_update(s); |
117 | aa941b94 | balrog | return 0; |
118 | aa941b94 | balrog | } |
119 | aa941b94 | balrog | |
120 | aefe2129 | Juan Quintela | static const VMStateDescription vmstate_ads7846 = { |
121 | aefe2129 | Juan Quintela | .name = "ads7846",
|
122 | aefe2129 | Juan Quintela | .version_id = 0,
|
123 | aefe2129 | Juan Quintela | .minimum_version_id = 0,
|
124 | aefe2129 | Juan Quintela | .minimum_version_id_old = 0,
|
125 | aefe2129 | Juan Quintela | .post_load = ads7856_post_load, |
126 | aefe2129 | Juan Quintela | .fields = (VMStateField[]) { |
127 | aefe2129 | Juan Quintela | VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
|
128 | aefe2129 | Juan Quintela | VMSTATE_INT32(noise, ADS7846State), |
129 | aefe2129 | Juan Quintela | VMSTATE_INT32(cycle, ADS7846State), |
130 | aefe2129 | Juan Quintela | VMSTATE_INT32(output, ADS7846State), |
131 | aefe2129 | Juan Quintela | VMSTATE_END_OF_LIST() |
132 | aefe2129 | Juan Quintela | } |
133 | aefe2129 | Juan Quintela | }; |
134 | aefe2129 | Juan Quintela | |
135 | 81a322d4 | Gerd Hoffmann | static int ads7846_init(SSISlave *dev) |
136 | fd5a3b33 | balrog | { |
137 | a984a69e | Paul Brook | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
138 | fd5a3b33 | balrog | |
139 | a984a69e | Paul Brook | qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
|
140 | fd5a3b33 | balrog | |
141 | fd5a3b33 | balrog | s->input[0] = ADS_TEMP0; /* TEMP0 */ |
142 | fd5a3b33 | balrog | s->input[2] = ADS_VBAT; /* VBAT */ |
143 | fd5a3b33 | balrog | s->input[6] = ADS_VAUX; /* VAUX */ |
144 | fd5a3b33 | balrog | s->input[7] = ADS_TEMP1; /* TEMP1 */ |
145 | fd5a3b33 | balrog | |
146 | fd5a3b33 | balrog | /* We want absolute coordinates */
|
147 | fd5a3b33 | balrog | qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
|
148 | fd5a3b33 | balrog | "QEMU ADS7846-driven Touchscreen");
|
149 | fd5a3b33 | balrog | |
150 | fd5a3b33 | balrog | ads7846_int_update(s); |
151 | aa941b94 | balrog | |
152 | aefe2129 | Juan Quintela | vmstate_register(NULL, -1, &vmstate_ads7846, s); |
153 | 81a322d4 | Gerd Hoffmann | return 0; |
154 | a984a69e | Paul Brook | } |
155 | aa941b94 | balrog | |
156 | cd6c4cf2 | Anthony Liguori | static void ads7846_class_init(ObjectClass *klass, void *data) |
157 | cd6c4cf2 | Anthony Liguori | { |
158 | cd6c4cf2 | Anthony Liguori | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); |
159 | cd6c4cf2 | Anthony Liguori | |
160 | cd6c4cf2 | Anthony Liguori | k->init = ads7846_init; |
161 | cd6c4cf2 | Anthony Liguori | k->transfer = ads7846_transfer; |
162 | cd6c4cf2 | Anthony Liguori | } |
163 | cd6c4cf2 | Anthony Liguori | |
164 | 39bffca2 | Anthony Liguori | static TypeInfo ads7846_info = {
|
165 | 39bffca2 | Anthony Liguori | .name = "ads7846",
|
166 | 39bffca2 | Anthony Liguori | .parent = TYPE_SSI_SLAVE, |
167 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(ADS7846State),
|
168 | 39bffca2 | Anthony Liguori | .class_init = ads7846_class_init, |
169 | a984a69e | Paul Brook | }; |
170 | a984a69e | Paul Brook | |
171 | 83f7d43a | Andreas Färber | static void ads7846_register_types(void) |
172 | a984a69e | Paul Brook | { |
173 | 39bffca2 | Anthony Liguori | type_register_static(&ads7846_info); |
174 | fd5a3b33 | balrog | } |
175 | a984a69e | Paul Brook | |
176 | 83f7d43a | Andreas Färber | type_init(ads7846_register_types) |