root / hw / ads7846.c @ 1ffc346f
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 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "devices.h" |
12 | 87ecb68b | pbrook | #include "console.h" |
13 | fd5a3b33 | balrog | |
14 | fd5a3b33 | balrog | struct ads7846_state_s {
|
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 | fd5a3b33 | balrog | }; |
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 | fd5a3b33 | balrog | static void ads7846_int_update(struct ads7846_state_s *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 | fd5a3b33 | balrog | uint32_t ads7846_read(void *opaque)
|
56 | fd5a3b33 | balrog | { |
57 | fd5a3b33 | balrog | struct ads7846_state_s *s = (struct ads7846_state_s *) opaque; |
58 | fd5a3b33 | balrog | |
59 | fd5a3b33 | balrog | return s->output;
|
60 | fd5a3b33 | balrog | } |
61 | fd5a3b33 | balrog | |
62 | fd5a3b33 | balrog | void ads7846_write(void *opaque, uint32_t value) |
63 | fd5a3b33 | balrog | { |
64 | fd5a3b33 | balrog | struct ads7846_state_s *s = (struct ads7846_state_s *) opaque; |
65 | fd5a3b33 | balrog | |
66 | fd5a3b33 | balrog | switch (s->cycle ++) {
|
67 | fd5a3b33 | balrog | case 0: |
68 | fd5a3b33 | balrog | if (!(value & CB_START)) {
|
69 | fd5a3b33 | balrog | s->cycle = 0;
|
70 | fd5a3b33 | balrog | break;
|
71 | fd5a3b33 | balrog | } |
72 | fd5a3b33 | balrog | |
73 | fd5a3b33 | balrog | s->output = s->input[(value >> 4) & 7]; |
74 | fd5a3b33 | balrog | |
75 | fd5a3b33 | balrog | /* Imitate the ADC noise, some drivers expect this. */
|
76 | fd5a3b33 | balrog | s->noise = (s->noise + 3) & 7; |
77 | fd5a3b33 | balrog | switch ((value >> 4) & 7) { |
78 | fd5a3b33 | balrog | case 1: s->output += s->noise ^ 2; break; |
79 | fd5a3b33 | balrog | case 3: s->output += s->noise ^ 0; break; |
80 | fd5a3b33 | balrog | case 4: s->output += s->noise ^ 7; break; |
81 | fd5a3b33 | balrog | case 5: s->output += s->noise ^ 5; break; |
82 | fd5a3b33 | balrog | } |
83 | fd5a3b33 | balrog | |
84 | fd5a3b33 | balrog | if (value & CB_MODE)
|
85 | fd5a3b33 | balrog | s->output >>= 4; /* 8 bits instead of 12 */ |
86 | fd5a3b33 | balrog | |
87 | fd5a3b33 | balrog | break;
|
88 | fd5a3b33 | balrog | case 1: |
89 | fd5a3b33 | balrog | s->cycle = 0;
|
90 | fd5a3b33 | balrog | break;
|
91 | fd5a3b33 | balrog | } |
92 | fd5a3b33 | balrog | } |
93 | fd5a3b33 | balrog | |
94 | fd5a3b33 | balrog | static void ads7846_ts_event(void *opaque, |
95 | fd5a3b33 | balrog | int x, int y, int z, int buttons_state) |
96 | fd5a3b33 | balrog | { |
97 | fd5a3b33 | balrog | struct ads7846_state_s *s = opaque;
|
98 | fd5a3b33 | balrog | |
99 | fd5a3b33 | balrog | if (buttons_state) {
|
100 | 611d7189 | balrog | x = 0x7fff - x;
|
101 | 611d7189 | balrog | s->input[1] = ADS_XPOS(x, y);
|
102 | fd5a3b33 | balrog | s->input[3] = ADS_Z1POS(x, y);
|
103 | fd5a3b33 | balrog | s->input[4] = ADS_Z2POS(x, y);
|
104 | 611d7189 | balrog | s->input[5] = ADS_YPOS(x, y);
|
105 | fd5a3b33 | balrog | } |
106 | fd5a3b33 | balrog | |
107 | fd5a3b33 | balrog | if (s->pressure == !buttons_state) {
|
108 | fd5a3b33 | balrog | s->pressure = !!buttons_state; |
109 | fd5a3b33 | balrog | |
110 | aa941b94 | balrog | ads7846_int_update(s); |
111 | fd5a3b33 | balrog | } |
112 | fd5a3b33 | balrog | } |
113 | fd5a3b33 | balrog | |
114 | aa941b94 | balrog | static void ads7846_save(QEMUFile *f, void *opaque) |
115 | aa941b94 | balrog | { |
116 | aa941b94 | balrog | struct ads7846_state_s *s = (struct ads7846_state_s *) opaque; |
117 | aa941b94 | balrog | int i;
|
118 | aa941b94 | balrog | |
119 | aa941b94 | balrog | for (i = 0; i < 8; i ++) |
120 | aa941b94 | balrog | qemu_put_be32(f, s->input[i]); |
121 | aa941b94 | balrog | qemu_put_be32(f, s->noise); |
122 | aa941b94 | balrog | qemu_put_be32(f, s->cycle); |
123 | aa941b94 | balrog | qemu_put_be32(f, s->output); |
124 | aa941b94 | balrog | } |
125 | aa941b94 | balrog | |
126 | aa941b94 | balrog | static int ads7846_load(QEMUFile *f, void *opaque, int version_id) |
127 | aa941b94 | balrog | { |
128 | aa941b94 | balrog | struct ads7846_state_s *s = (struct ads7846_state_s *) opaque; |
129 | aa941b94 | balrog | int i;
|
130 | aa941b94 | balrog | |
131 | aa941b94 | balrog | for (i = 0; i < 8; i ++) |
132 | aa941b94 | balrog | s->input[i] = qemu_get_be32(f); |
133 | aa941b94 | balrog | s->noise = qemu_get_be32(f); |
134 | aa941b94 | balrog | s->cycle = qemu_get_be32(f); |
135 | aa941b94 | balrog | s->output = qemu_get_be32(f); |
136 | aa941b94 | balrog | |
137 | aa941b94 | balrog | s->pressure = 0;
|
138 | aa941b94 | balrog | ads7846_int_update(s); |
139 | aa941b94 | balrog | |
140 | aa941b94 | balrog | return 0; |
141 | aa941b94 | balrog | } |
142 | aa941b94 | balrog | |
143 | aa941b94 | balrog | static int ads7846_iid = 0; |
144 | aa941b94 | balrog | |
145 | fd5a3b33 | balrog | struct ads7846_state_s *ads7846_init(qemu_irq penirq)
|
146 | fd5a3b33 | balrog | { |
147 | fd5a3b33 | balrog | struct ads7846_state_s *s;
|
148 | fd5a3b33 | balrog | s = (struct ads7846_state_s *)
|
149 | fd5a3b33 | balrog | qemu_mallocz(sizeof(struct ads7846_state_s)); |
150 | fd5a3b33 | balrog | memset(s, 0, sizeof(struct ads7846_state_s)); |
151 | fd5a3b33 | balrog | |
152 | fd5a3b33 | balrog | s->interrupt = penirq; |
153 | fd5a3b33 | balrog | |
154 | fd5a3b33 | balrog | s->input[0] = ADS_TEMP0; /* TEMP0 */ |
155 | fd5a3b33 | balrog | s->input[2] = ADS_VBAT; /* VBAT */ |
156 | fd5a3b33 | balrog | s->input[6] = ADS_VAUX; /* VAUX */ |
157 | fd5a3b33 | balrog | s->input[7] = ADS_TEMP1; /* TEMP1 */ |
158 | fd5a3b33 | balrog | |
159 | fd5a3b33 | balrog | /* We want absolute coordinates */
|
160 | fd5a3b33 | balrog | qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
|
161 | fd5a3b33 | balrog | "QEMU ADS7846-driven Touchscreen");
|
162 | fd5a3b33 | balrog | |
163 | fd5a3b33 | balrog | ads7846_int_update(s); |
164 | aa941b94 | balrog | |
165 | aa941b94 | balrog | register_savevm("ads7846", ads7846_iid ++, 0, |
166 | aa941b94 | balrog | ads7846_save, ads7846_load, s); |
167 | aa941b94 | balrog | |
168 | fd5a3b33 | balrog | return s;
|
169 | fd5a3b33 | balrog | } |