qapi/error: Check format string argument in error_*prepend()
[qemu/armbru.git] / hw / misc / max111x.c
blob7e6723f3435fccbb05aa24f1af2ff23203bcaea9
1 /*
2 * Maxim MAX1110/1111 ADC chip emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GNU GPLv2.
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/misc/max111x.h"
15 #include "hw/irq.h"
16 #include "migration/vmstate.h"
17 #include "qemu/module.h"
18 #include "hw/qdev-properties.h"
20 /* Control-byte bitfields */
21 #define CB_PD0 (1 << 0)
22 #define CB_PD1 (1 << 1)
23 #define CB_SGL (1 << 2)
24 #define CB_UNI (1 << 3)
25 #define CB_SEL0 (1 << 4)
26 #define CB_SEL1 (1 << 5)
27 #define CB_SEL2 (1 << 6)
28 #define CB_START (1 << 7)
30 #define CHANNEL_NUM(v, b0, b1, b2) \
31 ((((v) >> (2 + (b0))) & 4) | \
32 (((v) >> (3 + (b1))) & 2) | \
33 (((v) >> (4 + (b2))) & 1))
35 static uint32_t max111x_read(MAX111xState *s)
37 if (!s->tb1)
38 return 0;
40 switch (s->cycle ++) {
41 case 1:
42 return s->rb2;
43 case 2:
44 return s->rb3;
47 return 0;
50 /* Interpret a control-byte */
51 static void max111x_write(MAX111xState *s, uint32_t value)
53 int measure, chan;
55 /* Ignore the value if START bit is zero */
56 if (!(value & CB_START))
57 return;
59 s->cycle = 0;
61 if (!(value & CB_PD1)) {
62 s->tb1 = 0;
63 return;
66 s->tb1 = value;
68 if (s->inputs == 8)
69 chan = CHANNEL_NUM(value, 1, 0, 2);
70 else
71 chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
73 if (value & CB_SGL)
74 measure = s->input[chan] - s->com;
75 else
76 measure = s->input[chan] - s->input[chan ^ 1];
78 if (!(value & CB_UNI))
79 measure ^= 0x80;
81 s->rb2 = (measure >> 2) & 0x3f;
82 s->rb3 = (measure << 6) & 0xc0;
84 /* FIXME: When should the IRQ be lowered? */
85 qemu_irq_raise(s->interrupt);
88 static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
90 MAX111xState *s = MAX_111X(dev);
91 max111x_write(s, value);
92 return max111x_read(s);
95 static const VMStateDescription vmstate_max111x = {
96 .name = "max111x",
97 .version_id = 1,
98 .minimum_version_id = 1,
99 .fields = (VMStateField[]) {
100 VMSTATE_SSI_SLAVE(parent_obj, MAX111xState),
101 VMSTATE_UINT8(tb1, MAX111xState),
102 VMSTATE_UINT8(rb2, MAX111xState),
103 VMSTATE_UINT8(rb3, MAX111xState),
104 VMSTATE_INT32_EQUAL(inputs, MAX111xState, NULL),
105 VMSTATE_INT32(com, MAX111xState),
106 VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
107 vmstate_info_uint8, uint8_t),
108 VMSTATE_END_OF_LIST()
112 static void max111x_input_set(void *opaque, int line, int value)
114 MAX111xState *s = MAX_111X(opaque);
116 assert(line >= 0 && line < s->inputs);
117 s->input[line] = value;
120 static int max111x_init(SSISlave *d, int inputs)
122 DeviceState *dev = DEVICE(d);
123 MAX111xState *s = MAX_111X(dev);
125 qdev_init_gpio_out(dev, &s->interrupt, 1);
126 qdev_init_gpio_in(dev, max111x_input_set, inputs);
128 s->inputs = inputs;
130 return 0;
133 static void max1110_realize(SSISlave *dev, Error **errp)
135 max111x_init(dev, 8);
138 static void max1111_realize(SSISlave *dev, Error **errp)
140 max111x_init(dev, 4);
143 static void max111x_reset(DeviceState *dev)
145 MAX111xState *s = MAX_111X(dev);
146 int i;
148 for (i = 0; i < s->inputs; i++) {
149 s->input[i] = s->reset_input[i];
151 s->com = 0;
152 s->tb1 = 0;
153 s->rb2 = 0;
154 s->rb3 = 0;
155 s->cycle = 0;
158 static Property max1110_properties[] = {
159 /* Reset values for ADC inputs */
160 DEFINE_PROP_UINT8("input0", MAX111xState, reset_input[0], 0xf0),
161 DEFINE_PROP_UINT8("input1", MAX111xState, reset_input[1], 0xe0),
162 DEFINE_PROP_UINT8("input2", MAX111xState, reset_input[2], 0xd0),
163 DEFINE_PROP_UINT8("input3", MAX111xState, reset_input[3], 0xc0),
164 DEFINE_PROP_END_OF_LIST(),
167 static Property max1111_properties[] = {
168 /* Reset values for ADC inputs */
169 DEFINE_PROP_UINT8("input0", MAX111xState, reset_input[0], 0xf0),
170 DEFINE_PROP_UINT8("input1", MAX111xState, reset_input[1], 0xe0),
171 DEFINE_PROP_UINT8("input2", MAX111xState, reset_input[2], 0xd0),
172 DEFINE_PROP_UINT8("input3", MAX111xState, reset_input[3], 0xc0),
173 DEFINE_PROP_UINT8("input4", MAX111xState, reset_input[4], 0xb0),
174 DEFINE_PROP_UINT8("input5", MAX111xState, reset_input[5], 0xa0),
175 DEFINE_PROP_UINT8("input6", MAX111xState, reset_input[6], 0x90),
176 DEFINE_PROP_UINT8("input7", MAX111xState, reset_input[7], 0x80),
177 DEFINE_PROP_END_OF_LIST(),
180 static void max111x_class_init(ObjectClass *klass, void *data)
182 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
183 DeviceClass *dc = DEVICE_CLASS(klass);
185 k->transfer = max111x_transfer;
186 dc->reset = max111x_reset;
187 dc->vmsd = &vmstate_max111x;
190 static const TypeInfo max111x_info = {
191 .name = TYPE_MAX_111X,
192 .parent = TYPE_SSI_SLAVE,
193 .instance_size = sizeof(MAX111xState),
194 .class_init = max111x_class_init,
195 .abstract = true,
198 static void max1110_class_init(ObjectClass *klass, void *data)
200 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
201 DeviceClass *dc = DEVICE_CLASS(klass);
203 k->realize = max1110_realize;
204 device_class_set_props(dc, max1110_properties);
207 static const TypeInfo max1110_info = {
208 .name = TYPE_MAX_1110,
209 .parent = TYPE_MAX_111X,
210 .class_init = max1110_class_init,
213 static void max1111_class_init(ObjectClass *klass, void *data)
215 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
216 DeviceClass *dc = DEVICE_CLASS(klass);
218 k->realize = max1111_realize;
219 device_class_set_props(dc, max1111_properties);
222 static const TypeInfo max1111_info = {
223 .name = TYPE_MAX_1111,
224 .parent = TYPE_MAX_111X,
225 .class_init = max1111_class_init,
228 static void max111x_register_types(void)
230 type_register_static(&max111x_info);
231 type_register_static(&max1110_info);
232 type_register_static(&max1111_info);
235 type_init(max111x_register_types)