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