misc/max111x: Create abstract max111x type
[qemu.git] / hw / misc / max111x.c
1 /*
2 * Maxim MAX1110/1111 ADC 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 GPLv2.
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 "hw/ssi.h"
14
15 typedef struct {
16 SSISlave ssidev;
17 qemu_irq interrupt;
18 uint8_t tb1, rb2, rb3;
19 int cycle;
20
21 uint8_t input[8];
22 int inputs, com;
23 } MAX111xState;
24
25 #define TYPE_MAX_111X "max111x"
26
27 #define TYPE_MAX_1110 "max1110"
28 #define TYPE_MAX_1111 "max1111"
29
30 /* Control-byte bitfields */
31 #define CB_PD0 (1 << 0)
32 #define CB_PD1 (1 << 1)
33 #define CB_SGL (1 << 2)
34 #define CB_UNI (1 << 3)
35 #define CB_SEL0 (1 << 4)
36 #define CB_SEL1 (1 << 5)
37 #define CB_SEL2 (1 << 6)
38 #define CB_START (1 << 7)
39
40 #define CHANNEL_NUM(v, b0, b1, b2) \
41 ((((v) >> (2 + (b0))) & 4) | \
42 (((v) >> (3 + (b1))) & 2) | \
43 (((v) >> (4 + (b2))) & 1))
44
45 static uint32_t max111x_read(MAX111xState *s)
46 {
47 if (!s->tb1)
48 return 0;
49
50 switch (s->cycle ++) {
51 case 1:
52 return s->rb2;
53 case 2:
54 return s->rb3;
55 }
56
57 return 0;
58 }
59
60 /* Interpret a control-byte */
61 static void max111x_write(MAX111xState *s, uint32_t value)
62 {
63 int measure, chan;
64
65 /* Ignore the value if START bit is zero */
66 if (!(value & CB_START))
67 return;
68
69 s->cycle = 0;
70
71 if (!(value & CB_PD1)) {
72 s->tb1 = 0;
73 return;
74 }
75
76 s->tb1 = value;
77
78 if (s->inputs == 8)
79 chan = CHANNEL_NUM(value, 1, 0, 2);
80 else
81 chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
82
83 if (value & CB_SGL)
84 measure = s->input[chan] - s->com;
85 else
86 measure = s->input[chan] - s->input[chan ^ 1];
87
88 if (!(value & CB_UNI))
89 measure ^= 0x80;
90
91 s->rb2 = (measure >> 2) & 0x3f;
92 s->rb3 = (measure << 6) & 0xc0;
93
94 /* FIXME: When should the IRQ be lowered? */
95 qemu_irq_raise(s->interrupt);
96 }
97
98 static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
99 {
100 MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, dev);
101 max111x_write(s, value);
102 return max111x_read(s);
103 }
104
105 static const VMStateDescription vmstate_max111x = {
106 .name = "max111x",
107 .version_id = 1,
108 .minimum_version_id = 1,
109 .minimum_version_id_old = 1,
110 .fields = (VMStateField[]) {
111 VMSTATE_SSI_SLAVE(ssidev, MAX111xState),
112 VMSTATE_UINT8(tb1, MAX111xState),
113 VMSTATE_UINT8(rb2, MAX111xState),
114 VMSTATE_UINT8(rb3, MAX111xState),
115 VMSTATE_INT32_EQUAL(inputs, MAX111xState),
116 VMSTATE_INT32(com, MAX111xState),
117 VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
118 vmstate_info_uint8, uint8_t),
119 VMSTATE_END_OF_LIST()
120 }
121 };
122
123 static int max111x_init(SSISlave *d, int inputs)
124 {
125 DeviceState *dev = DEVICE(d);
126 MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, d);
127
128 qdev_init_gpio_out(dev, &s->interrupt, 1);
129
130 s->inputs = inputs;
131 /* TODO: add a user interface for setting these */
132 s->input[0] = 0xf0;
133 s->input[1] = 0xe0;
134 s->input[2] = 0xd0;
135 s->input[3] = 0xc0;
136 s->input[4] = 0xb0;
137 s->input[5] = 0xa0;
138 s->input[6] = 0x90;
139 s->input[7] = 0x80;
140 s->com = 0;
141
142 vmstate_register(dev, -1, &vmstate_max111x, s);
143 return 0;
144 }
145
146 static int max1110_init(SSISlave *dev)
147 {
148 return max111x_init(dev, 8);
149 }
150
151 static int max1111_init(SSISlave *dev)
152 {
153 return max111x_init(dev, 4);
154 }
155
156 void max111x_set_input(DeviceState *dev, int line, uint8_t value)
157 {
158 MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, SSI_SLAVE_FROM_QDEV(dev));
159 assert(line >= 0 && line < s->inputs);
160 s->input[line] = value;
161 }
162
163 static void max111x_class_init(ObjectClass *klass, void *data)
164 {
165 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
166
167 k->transfer = max111x_transfer;
168 }
169
170 static const TypeInfo max111x_info = {
171 .name = TYPE_MAX_111X,
172 .parent = TYPE_SSI_SLAVE,
173 .instance_size = sizeof(MAX111xState),
174 .class_init = max111x_class_init,
175 .abstract = true,
176 };
177
178 static void max1110_class_init(ObjectClass *klass, void *data)
179 {
180 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
181
182 k->init = max1110_init;
183 }
184
185 static const TypeInfo max1110_info = {
186 .name = TYPE_MAX_1110,
187 .parent = TYPE_MAX_111X,
188 .class_init = max1110_class_init,
189 };
190
191 static void max1111_class_init(ObjectClass *klass, void *data)
192 {
193 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
194
195 k->init = max1111_init;
196 }
197
198 static const TypeInfo max1111_info = {
199 .name = TYPE_MAX_1111,
200 .parent = TYPE_MAX_111X,
201 .class_init = max1111_class_init,
202 };
203
204 static void max111x_register_types(void)
205 {
206 type_register_static(&max111x_info);
207 type_register_static(&max1110_info);
208 type_register_static(&max1111_info);
209 }
210
211 type_init(max111x_register_types)