]> git.proxmox.com Git - mirror_qemu.git/blame - hw/display/ads7846.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / hw / display / ads7846.c
CommitLineData
fd5a3b33 1/*
87283515 2 * TI ADS7846 / TSC2046 chip emulation.
fd5a3b33
AZ
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
6 *
7 * This code is licensed under the GNU GPL v2.
6b620ca3
PB
8 *
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
fd5a3b33
AZ
11 */
12
47df5154 13#include "qemu/osdep.h"
8fd06719 14#include "hw/ssi/ssi.h"
0b8fa32f 15#include "qemu/module.h"
28ecbaee 16#include "ui/console.h"
fd5a3b33 17
a984a69e
PB
18typedef struct {
19 SSISlave ssidev;
fd5a3b33
AZ
20 qemu_irq interrupt;
21
22 int input[8];
23 int pressure;
24 int noise;
25
26 int cycle;
27 int output;
a984a69e 28} ADS7846State;
fd5a3b33
AZ
29
30/* Control-byte bitfields */
31#define CB_PD0 (1 << 0)
32#define CB_PD1 (1 << 1)
33#define CB_SER (1 << 2)
34#define CB_MODE (1 << 3)
35#define CB_A0 (1 << 4)
36#define CB_A1 (1 << 5)
37#define CB_A2 (1 << 6)
38#define CB_START (1 << 7)
39
611d7189
AZ
40#define X_AXIS_DMAX 3470
41#define X_AXIS_MIN 290
42#define Y_AXIS_DMAX 3450
43#define Y_AXIS_MIN 200
fd5a3b33
AZ
44
45#define ADS_VBAT 2000
46#define ADS_VAUX 2000
47#define ADS_TEMP0 2000
48#define ADS_TEMP1 3000
49#define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
50#define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
51#define ADS_Z1POS(x, y) 600
52#define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
53
bc24a225 54static void ads7846_int_update(ADS7846State *s)
fd5a3b33
AZ
55{
56 if (s->interrupt)
57 qemu_set_irq(s->interrupt, s->pressure == 0);
58}
59
a984a69e 60static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
fd5a3b33 61{
a984a69e 62 ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, dev);
fd5a3b33
AZ
63
64 switch (s->cycle ++) {
65 case 0:
66 if (!(value & CB_START)) {
67 s->cycle = 0;
68 break;
69 }
70
71 s->output = s->input[(value >> 4) & 7];
72
73 /* Imitate the ADC noise, some drivers expect this. */
74 s->noise = (s->noise + 3) & 7;
75 switch ((value >> 4) & 7) {
76 case 1: s->output += s->noise ^ 2; break;
77 case 3: s->output += s->noise ^ 0; break;
78 case 4: s->output += s->noise ^ 7; break;
79 case 5: s->output += s->noise ^ 5; break;
80 }
81
82 if (value & CB_MODE)
83 s->output >>= 4; /* 8 bits instead of 12 */
84
85 break;
86 case 1:
87 s->cycle = 0;
88 break;
89 }
a984a69e 90 return s->output;
fd5a3b33
AZ
91}
92
93static void ads7846_ts_event(void *opaque,
94 int x, int y, int z, int buttons_state)
95{
bc24a225 96 ADS7846State *s = opaque;
fd5a3b33
AZ
97
98 if (buttons_state) {
611d7189
AZ
99 x = 0x7fff - x;
100 s->input[1] = ADS_XPOS(x, y);
fd5a3b33
AZ
101 s->input[3] = ADS_Z1POS(x, y);
102 s->input[4] = ADS_Z2POS(x, y);
611d7189 103 s->input[5] = ADS_YPOS(x, y);
fd5a3b33
AZ
104 }
105
106 if (s->pressure == !buttons_state) {
107 s->pressure = !!buttons_state;
108
aa941b94 109 ads7846_int_update(s);
fd5a3b33
AZ
110 }
111}
112
aefe2129 113static int ads7856_post_load(void *opaque, int version_id)
aa941b94 114{
aefe2129 115 ADS7846State *s = opaque;
aa941b94
AZ
116
117 s->pressure = 0;
118 ads7846_int_update(s);
aa941b94
AZ
119 return 0;
120}
121
aefe2129
JQ
122static const VMStateDescription vmstate_ads7846 = {
123 .name = "ads7846",
66530953
PC
124 .version_id = 1,
125 .minimum_version_id = 1,
aefe2129 126 .post_load = ads7856_post_load,
8f1e884b 127 .fields = (VMStateField[]) {
66530953 128 VMSTATE_SSI_SLAVE(ssidev, ADS7846State),
aefe2129
JQ
129 VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
130 VMSTATE_INT32(noise, ADS7846State),
131 VMSTATE_INT32(cycle, ADS7846State),
132 VMSTATE_INT32(output, ADS7846State),
133 VMSTATE_END_OF_LIST()
134 }
135};
136
7673bb4c 137static void ads7846_realize(SSISlave *d, Error **errp)
fd5a3b33 138{
1a7d9ee6
PC
139 DeviceState *dev = DEVICE(d);
140 ADS7846State *s = FROM_SSI_SLAVE(ADS7846State, d);
fd5a3b33 141
1a7d9ee6 142 qdev_init_gpio_out(dev, &s->interrupt, 1);
fd5a3b33
AZ
143
144 s->input[0] = ADS_TEMP0; /* TEMP0 */
145 s->input[2] = ADS_VBAT; /* VBAT */
146 s->input[6] = ADS_VAUX; /* VAUX */
147 s->input[7] = ADS_TEMP1; /* TEMP1 */
148
149 /* We want absolute coordinates */
150 qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
151 "QEMU ADS7846-driven Touchscreen");
152
153 ads7846_int_update(s);
aa941b94 154
aefe2129 155 vmstate_register(NULL, -1, &vmstate_ads7846, s);
a984a69e 156}
aa941b94 157
cd6c4cf2
AL
158static void ads7846_class_init(ObjectClass *klass, void *data)
159{
160 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
161
7673bb4c 162 k->realize = ads7846_realize;
cd6c4cf2
AL
163 k->transfer = ads7846_transfer;
164}
165
8c43a6f0 166static const TypeInfo ads7846_info = {
39bffca2
AL
167 .name = "ads7846",
168 .parent = TYPE_SSI_SLAVE,
169 .instance_size = sizeof(ADS7846State),
170 .class_init = ads7846_class_init,
a984a69e
PB
171};
172
83f7d43a 173static void ads7846_register_types(void)
a984a69e 174{
39bffca2 175 type_register_static(&ads7846_info);
fd5a3b33 176}
a984a69e 177
83f7d43a 178type_init(ads7846_register_types)