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