qemu-option: Factor out helper find_default_by_name()
[qemu/ar7.git] / hw / display / ads7846.c
blob56bf82fe079d54b3a502b7c410fc9af64225c4cb
1 /*
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.
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.
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"
20 typedef struct {
21 SSISlave ssidev;
22 qemu_irq interrupt;
24 int input[8];
25 int pressure;
26 int noise;
28 int cycle;
29 int output;
30 } ADS7846State;
32 #define TYPE_ADS7846 "ads7846"
33 #define ADS7846(obj) OBJECT_CHECK(ADS7846State, (obj), TYPE_ADS7846)
35 /* Control-byte bitfields */
36 #define CB_PD0 (1 << 0)
37 #define CB_PD1 (1 << 1)
38 #define CB_SER (1 << 2)
39 #define CB_MODE (1 << 3)
40 #define CB_A0 (1 << 4)
41 #define CB_A1 (1 << 5)
42 #define CB_A2 (1 << 6)
43 #define CB_START (1 << 7)
45 #define X_AXIS_DMAX 3470
46 #define X_AXIS_MIN 290
47 #define Y_AXIS_DMAX 3450
48 #define Y_AXIS_MIN 200
50 #define ADS_VBAT 2000
51 #define ADS_VAUX 2000
52 #define ADS_TEMP0 2000
53 #define ADS_TEMP1 3000
54 #define ADS_XPOS(x, y) (X_AXIS_MIN + ((X_AXIS_DMAX * (x)) >> 15))
55 #define ADS_YPOS(x, y) (Y_AXIS_MIN + ((Y_AXIS_DMAX * (y)) >> 15))
56 #define ADS_Z1POS(x, y) 600
57 #define ADS_Z2POS(x, y) (600 + 6000 / ADS_XPOS(x, y))
59 static void ads7846_int_update(ADS7846State *s)
61 if (s->interrupt)
62 qemu_set_irq(s->interrupt, s->pressure == 0);
65 static uint32_t ads7846_transfer(SSISlave *dev, uint32_t value)
67 ADS7846State *s = ADS7846(dev);
69 switch (s->cycle ++) {
70 case 0:
71 if (!(value & CB_START)) {
72 s->cycle = 0;
73 break;
76 s->output = s->input[(value >> 4) & 7];
78 /* Imitate the ADC noise, some drivers expect this. */
79 s->noise = (s->noise + 3) & 7;
80 switch ((value >> 4) & 7) {
81 case 1: s->output += s->noise ^ 2; break;
82 case 3: s->output += s->noise ^ 0; break;
83 case 4: s->output += s->noise ^ 7; break;
84 case 5: s->output += s->noise ^ 5; break;
87 if (value & CB_MODE)
88 s->output >>= 4; /* 8 bits instead of 12 */
90 break;
91 case 1:
92 s->cycle = 0;
93 break;
95 return s->output;
98 static void ads7846_ts_event(void *opaque,
99 int x, int y, int z, int buttons_state)
101 ADS7846State *s = opaque;
103 if (buttons_state) {
104 x = 0x7fff - x;
105 s->input[1] = ADS_XPOS(x, y);
106 s->input[3] = ADS_Z1POS(x, y);
107 s->input[4] = ADS_Z2POS(x, y);
108 s->input[5] = ADS_YPOS(x, y);
111 if (s->pressure == !buttons_state) {
112 s->pressure = !!buttons_state;
114 ads7846_int_update(s);
118 static int ads7856_post_load(void *opaque, int version_id)
120 ADS7846State *s = opaque;
122 s->pressure = 0;
123 ads7846_int_update(s);
124 return 0;
127 static const VMStateDescription vmstate_ads7846 = {
128 .name = "ads7846",
129 .version_id = 1,
130 .minimum_version_id = 1,
131 .post_load = ads7856_post_load,
132 .fields = (VMStateField[]) {
133 VMSTATE_SSI_SLAVE(ssidev, ADS7846State),
134 VMSTATE_INT32_ARRAY(input, ADS7846State, 8),
135 VMSTATE_INT32(noise, ADS7846State),
136 VMSTATE_INT32(cycle, ADS7846State),
137 VMSTATE_INT32(output, ADS7846State),
138 VMSTATE_END_OF_LIST()
142 static void ads7846_realize(SSISlave *d, Error **errp)
144 DeviceState *dev = DEVICE(d);
145 ADS7846State *s = ADS7846(d);
147 qdev_init_gpio_out(dev, &s->interrupt, 1);
149 s->input[0] = ADS_TEMP0; /* TEMP0 */
150 s->input[2] = ADS_VBAT; /* VBAT */
151 s->input[6] = ADS_VAUX; /* VAUX */
152 s->input[7] = ADS_TEMP1; /* TEMP1 */
154 /* We want absolute coordinates */
155 qemu_add_mouse_event_handler(ads7846_ts_event, s, 1,
156 "QEMU ADS7846-driven Touchscreen");
158 ads7846_int_update(s);
160 vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_ads7846, s);
163 static void ads7846_class_init(ObjectClass *klass, void *data)
165 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
167 k->realize = ads7846_realize;
168 k->transfer = ads7846_transfer;
171 static const TypeInfo ads7846_info = {
172 .name = TYPE_ADS7846,
173 .parent = TYPE_SSI_SLAVE,
174 .instance_size = sizeof(ADS7846State),
175 .class_init = ads7846_class_init,
178 static void ads7846_register_types(void)
180 type_register_static(&ads7846_info);
183 type_init(ads7846_register_types)