2 * TI ADS7846 / TSC2046 chip emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GNU GPL v2.
14 struct ads7846_state_s
{
25 /* Control-byte bitfields */
26 #define CB_PD0 (1 << 0)
27 #define CB_PD1 (1 << 1)
28 #define CB_SER (1 << 2)
29 #define CB_MODE (1 << 3)
30 #define CB_A0 (1 << 4)
31 #define CB_A1 (1 << 5)
32 #define CB_A2 (1 << 6)
33 #define CB_START (1 << 7)
35 #define X_AXIS_DMAX 3470
36 #define X_AXIS_MIN 290
37 #define Y_AXIS_DMAX 3450
38 #define Y_AXIS_MIN 200
42 #define ADS_TEMP0 2000
43 #define ADS_TEMP1 3000
44 #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
45 #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
46 #define ADS_Z1POS(x, y) 600
47 #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
49 static void ads7846_int_update(struct ads7846_state_s
*s
)
52 qemu_set_irq(s
->interrupt
, s
->pressure
== 0);
55 uint32_t ads7846_read(void *opaque
)
57 struct ads7846_state_s
*s
= (struct ads7846_state_s
*) opaque
;
62 void ads7846_write(void *opaque
, uint32_t value
)
64 struct ads7846_state_s
*s
= (struct ads7846_state_s
*) opaque
;
66 switch (s
->cycle
++) {
68 if (!(value
& CB_START
)) {
73 s
->output
= s
->input
[(value
>> 4) & 7];
75 /* Imitate the ADC noise, some drivers expect this. */
76 s
->noise
= (s
->noise
+ 3) & 7;
77 switch ((value
>> 4) & 7) {
78 case 1: s
->output
+= s
->noise
^ 2; break;
79 case 3: s
->output
+= s
->noise
^ 0; break;
80 case 4: s
->output
+= s
->noise
^ 7; break;
81 case 5: s
->output
+= s
->noise
^ 5; break;
85 s
->output
>>= 4; /* 8 bits instead of 12 */
94 static void ads7846_ts_event(void *opaque
,
95 int x
, int y
, int z
, int buttons_state
)
97 struct ads7846_state_s
*s
= opaque
;
101 s
->input
[1] = ADS_XPOS(x
, y
);
102 s
->input
[3] = ADS_Z1POS(x
, y
);
103 s
->input
[4] = ADS_Z2POS(x
, y
);
104 s
->input
[5] = ADS_YPOS(x
, y
);
107 if (s
->pressure
== !buttons_state
) {
108 s
->pressure
= !!buttons_state
;
110 ads7846_int_update(s
);
114 static void ads7846_save(QEMUFile
*f
, void *opaque
)
116 struct ads7846_state_s
*s
= (struct ads7846_state_s
*) opaque
;
119 for (i
= 0; i
< 8; i
++)
120 qemu_put_be32(f
, s
->input
[i
]);
121 qemu_put_be32(f
, s
->noise
);
122 qemu_put_be32(f
, s
->cycle
);
123 qemu_put_be32(f
, s
->output
);
126 static int ads7846_load(QEMUFile
*f
, void *opaque
, int version_id
)
128 struct ads7846_state_s
*s
= (struct ads7846_state_s
*) opaque
;
131 for (i
= 0; i
< 8; i
++)
132 s
->input
[i
] = qemu_get_be32(f
);
133 s
->noise
= qemu_get_be32(f
);
134 s
->cycle
= qemu_get_be32(f
);
135 s
->output
= qemu_get_be32(f
);
138 ads7846_int_update(s
);
143 static int ads7846_iid
= 0;
145 struct ads7846_state_s
*ads7846_init(qemu_irq penirq
)
147 struct ads7846_state_s
*s
;
148 s
= (struct ads7846_state_s
*)
149 qemu_mallocz(sizeof(struct ads7846_state_s
));
150 memset(s
, 0, sizeof(struct ads7846_state_s
));
152 s
->interrupt
= penirq
;
154 s
->input
[0] = ADS_TEMP0
; /* TEMP0 */
155 s
->input
[2] = ADS_VBAT
; /* VBAT */
156 s
->input
[6] = ADS_VAUX
; /* VAUX */
157 s
->input
[7] = ADS_TEMP1
; /* TEMP1 */
159 /* We want absolute coordinates */
160 qemu_add_mouse_event_handler(ads7846_ts_event
, s
, 1,
161 "QEMU ADS7846-driven Touchscreen");
163 ads7846_int_update(s
);
165 register_savevm("ads7846", ads7846_iid
++, 0,
166 ads7846_save
, ads7846_load
, s
);