root / hw / ads7846.c @ d2d979c6
History | View | Annotate | Download (3.8 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 | aefe2129 | Juan Quintela | static int ads7856_post_load(void *opaque, int version_id) |
109 | aa941b94 | balrog | { |
110 | aefe2129 | Juan Quintela | ADS7846State *s = opaque; |
111 | aa941b94 | balrog | |
112 | aa941b94 | balrog | s->pressure = 0;
|
113 | aa941b94 | balrog | ads7846_int_update(s); |
114 | aa941b94 | balrog | return 0; |
115 | aa941b94 | balrog | } |
116 | aa941b94 | balrog | |
117 | aefe2129 | Juan Quintela | static const VMStateDescription vmstate_ads7846 = { |
118 | aefe2129 | Juan Quintela | .name = "ads7846",
|
119 | aefe2129 | Juan Quintela | .version_id = 0,
|
120 | aefe2129 | Juan Quintela | .minimum_version_id = 0,
|
121 | aefe2129 | Juan Quintela | .minimum_version_id_old = 0,
|
122 | aefe2129 | Juan Quintela | .post_load = ads7856_post_load, |
123 | aefe2129 | Juan Quintela | .fields = (VMStateField[]) { |
124 | aefe2129 | Juan Quintela | VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
|
125 | aefe2129 | Juan Quintela | VMSTATE_INT32(noise, ADS7846State), |
126 | aefe2129 | Juan Quintela | VMSTATE_INT32(cycle, ADS7846State), |
127 | aefe2129 | Juan Quintela | VMSTATE_INT32(output, ADS7846State), |
128 | aefe2129 | Juan Quintela | VMSTATE_END_OF_LIST() |
129 | aefe2129 | Juan Quintela | } |
130 | aefe2129 | Juan Quintela | }; |
131 | aefe2129 | Juan Quintela | |
132 | 81a322d4 | Gerd Hoffmann | static int ads7846_init(SSISlave *dev) |
133 | fd5a3b33 | balrog | { |
134 | a984a69e | Paul Brook | ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev); |
135 | fd5a3b33 | balrog | |
136 | a984a69e | Paul Brook | qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
|
137 | fd5a3b33 | balrog | |
138 | fd5a3b33 | balrog | s->input[0] = ADS_TEMP0; /* TEMP0 */ |
139 | fd5a3b33 | balrog | s->input[2] = ADS_VBAT; /* VBAT */ |
140 | fd5a3b33 | balrog | s->input[6] = ADS_VAUX; /* VAUX */ |
141 | fd5a3b33 | balrog | s->input[7] = ADS_TEMP1; /* TEMP1 */ |
142 | fd5a3b33 | balrog | |
143 | fd5a3b33 | balrog | /* We want absolute coordinates */
|
144 | fd5a3b33 | balrog | qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
|
145 | fd5a3b33 | balrog | "QEMU ADS7846-driven Touchscreen");
|
146 | fd5a3b33 | balrog | |
147 | fd5a3b33 | balrog | ads7846_int_update(s); |
148 | aa941b94 | balrog | |
149 | aefe2129 | Juan Quintela | vmstate_register(NULL, -1, &vmstate_ads7846, s); |
150 | 81a322d4 | Gerd Hoffmann | return 0; |
151 | a984a69e | Paul Brook | } |
152 | aa941b94 | balrog | |
153 | a984a69e | Paul Brook | static SSISlaveInfo ads7846_info = {
|
154 | 074f2fff | Gerd Hoffmann | .qdev.name ="ads7846",
|
155 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(ADS7846State),
|
156 | a984a69e | Paul Brook | .init = ads7846_init, |
157 | a984a69e | Paul Brook | .transfer = ads7846_transfer |
158 | a984a69e | Paul Brook | }; |
159 | a984a69e | Paul Brook | |
160 | a984a69e | Paul Brook | static void ads7846_register_devices(void) |
161 | a984a69e | Paul Brook | { |
162 | 074f2fff | Gerd Hoffmann | ssi_register_slave(&ads7846_info); |
163 | fd5a3b33 | balrog | } |
164 | a984a69e | Paul Brook | |
165 | a984a69e | Paul Brook | device_init(ads7846_register_devices) |