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