]> git.proxmox.com Git - mirror_qemu.git/blame - hw/display/ads7846.c
Use DECLARE_*CHECKER* macros
[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"
64552b6b 14#include "hw/irq.h"
8fd06719 15#include "hw/ssi/ssi.h"
d6454270 16#include "migration/vmstate.h"
0b8fa32f 17#include "qemu/module.h"
28ecbaee 18#include "ui/console.h"
db1015e9 19#include "qom/object.h"
fd5a3b33 20
db1015e9 21struct ADS7846State {
a984a69e 22 SSISlave ssidev;
fd5a3b33
AZ
23 qemu_irq interrupt;
24
25 int input[8];
26 int pressure;
27 int noise;
28
29 int cycle;
30 int output;
db1015e9
EH
31};
32typedef struct ADS7846State ADS7846State;
fd5a3b33 33
213f63df 34#define TYPE_ADS7846 "ads7846"
8110fa1d
EH
35DECLARE_INSTANCE_CHECKER(ADS7846State, ADS7846,
36 TYPE_ADS7846)
213f63df 37
fd5a3b33
AZ
38/* Control-byte bitfields */
39#define CB_PD0 (1 << 0)
40#define CB_PD1 (1 << 1)
41#define CB_SER (1 << 2)
42#define CB_MODE (1 << 3)
43#define CB_A0 (1 << 4)
44#define CB_A1 (1 << 5)
45#define CB_A2 (1 << 6)
46#define CB_START (1 << 7)
47
611d7189
AZ
48#define X_AXIS_DMAX 3470
49#define X_AXIS_MIN 290
50#define Y_AXIS_DMAX 3450
51#define Y_AXIS_MIN 200
fd5a3b33
AZ
52
53#define ADS_VBAT 2000
54#define ADS_VAUX 2000
55#define ADS_TEMP0 2000
56#define ADS_TEMP1 3000
57#define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
58#define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
59#define ADS_Z1POS(x, y) 600
60#define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
61
bc24a225 62static void ads7846_int_update(ADS7846State *s)
fd5a3b33
AZ
63{
64 if (s->interrupt)
65 qemu_set_irq(s->interrupt, s->pressure == 0);
66}
67
a984a69e 68static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
fd5a3b33 69{
213f63df 70 ADS7846State *s = ADS7846(dev);
fd5a3b33
AZ
71
72 switch (s->cycle ++) {
73 case 0:
74 if (!(value & CB_START)) {
75 s->cycle = 0;
76 break;
77 }
78
79 s->output = s->input[(value >> 4) & 7];
80
81 /* Imitate the ADC noise, some drivers expect this. */
82 s->noise = (s->noise + 3) & 7;
83 switch ((value >> 4) & 7) {
84 case 1: s->output += s->noise ^ 2; break;
85 case 3: s->output += s->noise ^ 0; break;
86 case 4: s->output += s->noise ^ 7; break;
87 case 5: s->output += s->noise ^ 5; break;
88 }
89
90 if (value & CB_MODE)
91 s->output >>= 4; /* 8 bits instead of 12 */
92
93 break;
94 case 1:
95 s->cycle = 0;
96 break;
97 }
a984a69e 98 return s->output;
fd5a3b33
AZ
99}
100
101static void ads7846_ts_event(void *opaque,
102 int x, int y, int z, int buttons_state)
103{
bc24a225 104 ADS7846State *s = opaque;
fd5a3b33
AZ
105
106 if (buttons_state) {
611d7189
AZ
107 x = 0x7fff - x;
108 s->input[1] = ADS_XPOS(x, y);
fd5a3b33
AZ
109 s->input[3] = ADS_Z1POS(x, y);
110 s->input[4] = ADS_Z2POS(x, y);
611d7189 111 s->input[5] = ADS_YPOS(x, y);
fd5a3b33
AZ
112 }
113
114 if (s->pressure == !buttons_state) {
115 s->pressure = !!buttons_state;
116
aa941b94 117 ads7846_int_update(s);
fd5a3b33
AZ
118 }
119}
120
aefe2129 121static int ads7856_post_load(void *opaque, int version_id)
aa941b94 122{
aefe2129 123 ADS7846State *s = opaque;
aa941b94
AZ
124
125 s->pressure = 0;
126 ads7846_int_update(s);
aa941b94
AZ
127 return 0;
128}
129
aefe2129
JQ
130static const VMStateDescription vmstate_ads7846 = {
131 .name = "ads7846",
66530953
PC
132 .version_id = 1,
133 .minimum_version_id = 1,
aefe2129 134 .post_load = ads7856_post_load,
8f1e884b 135 .fields = (VMStateField[]) {
66530953 136 VMSTATE_SSI_SLAVE(ssidev, ADS7846State),
aefe2129
JQ
137 VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
138 VMSTATE_INT32(noise, ADS7846State),
139 VMSTATE_INT32(cycle, ADS7846State),
140 VMSTATE_INT32(output, ADS7846State),
141 VMSTATE_END_OF_LIST()
142 }
143};
144
7673bb4c 145static void ads7846_realize(SSISlave *d, Error **errp)
fd5a3b33 146{
1a7d9ee6 147 DeviceState *dev = DEVICE(d);
213f63df 148 ADS7846State *s = ADS7846(d);
fd5a3b33 149
1a7d9ee6 150 qdev_init_gpio_out(dev, &s->interrupt, 1);
fd5a3b33
AZ
151
152 s->input[0] = ADS_TEMP0; /* TEMP0 */
153 s->input[2] = ADS_VBAT; /* VBAT */
154 s->input[6] = ADS_VAUX; /* VAUX */
155 s->input[7] = ADS_TEMP1; /* TEMP1 */
156
157 /* We want absolute coordinates */
158 qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
159 "QEMU ADS7846-driven Touchscreen");
160
161 ads7846_int_update(s);
aa941b94 162
1df2c9a2 163 vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_ads7846, s);
a984a69e 164}
aa941b94 165
cd6c4cf2
AL
166static void ads7846_class_init(ObjectClass *klass, void *data)
167{
168 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
169
7673bb4c 170 k->realize = ads7846_realize;
cd6c4cf2
AL
171 k->transfer = ads7846_transfer;
172}
173
8c43a6f0 174static const TypeInfo ads7846_info = {
213f63df 175 .name = TYPE_ADS7846,
39bffca2
AL
176 .parent = TYPE_SSI_SLAVE,
177 .instance_size = sizeof(ADS7846State),
178 .class_init = ads7846_class_init,
a984a69e
PB
179};
180
83f7d43a 181static void ads7846_register_types(void)
a984a69e 182{
39bffca2 183 type_register_static(&ads7846_info);
fd5a3b33 184}
a984a69e 185
83f7d43a 186type_init(ads7846_register_types)