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.
19 uint8_t tb1
, rb2
, rb3
;
26 #define TYPE_MAX_111X "max111x"
28 #define MAX_111X(obj) \
29 OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X)
31 #define TYPE_MAX_1110 "max1110"
32 #define TYPE_MAX_1111 "max1111"
34 /* Control-byte bitfields */
35 #define CB_PD0 (1 << 0)
36 #define CB_PD1 (1 << 1)
37 #define CB_SGL (1 << 2)
38 #define CB_UNI (1 << 3)
39 #define CB_SEL0 (1 << 4)
40 #define CB_SEL1 (1 << 5)
41 #define CB_SEL2 (1 << 6)
42 #define CB_START (1 << 7)
44 #define CHANNEL_NUM(v, b0, b1, b2) \
45 ((((v) >> (2 + (b0))) & 4) | \
46 (((v) >> (3 + (b1))) & 2) | \
47 (((v) >> (4 + (b2))) & 1))
49 static uint32_t max111x_read(MAX111xState
*s
)
54 switch (s
->cycle
++) {
64 /* Interpret a control-byte */
65 static void max111x_write(MAX111xState
*s
, uint32_t value
)
69 /* Ignore the value if START bit is zero */
70 if (!(value
& CB_START
))
75 if (!(value
& CB_PD1
)) {
83 chan
= CHANNEL_NUM(value
, 1, 0, 2);
85 chan
= CHANNEL_NUM(value
& ~CB_SEL0
, 0, 1, 2);
88 measure
= s
->input
[chan
] - s
->com
;
90 measure
= s
->input
[chan
] - s
->input
[chan
^ 1];
92 if (!(value
& CB_UNI
))
95 s
->rb2
= (measure
>> 2) & 0x3f;
96 s
->rb3
= (measure
<< 6) & 0xc0;
98 /* FIXME: When should the IRQ be lowered? */
99 qemu_irq_raise(s
->interrupt
);
102 static uint32_t max111x_transfer(SSISlave
*dev
, uint32_t value
)
104 MAX111xState
*s
= MAX_111X(dev
);
105 max111x_write(s
, value
);
106 return max111x_read(s
);
109 static const VMStateDescription vmstate_max111x
= {
112 .minimum_version_id
= 1,
113 .fields
= (VMStateField
[]) {
114 VMSTATE_SSI_SLAVE(parent_obj
, MAX111xState
),
115 VMSTATE_UINT8(tb1
, MAX111xState
),
116 VMSTATE_UINT8(rb2
, MAX111xState
),
117 VMSTATE_UINT8(rb3
, MAX111xState
),
118 VMSTATE_INT32_EQUAL(inputs
, MAX111xState
),
119 VMSTATE_INT32(com
, MAX111xState
),
120 VMSTATE_ARRAY_INT32_UNSAFE(input
, MAX111xState
, inputs
,
121 vmstate_info_uint8
, uint8_t),
122 VMSTATE_END_OF_LIST()
126 static int max111x_init(SSISlave
*d
, int inputs
)
128 DeviceState
*dev
= DEVICE(d
);
129 MAX111xState
*s
= MAX_111X(dev
);
131 qdev_init_gpio_out(dev
, &s
->interrupt
, 1);
134 /* TODO: add a user interface for setting these */
145 vmstate_register(dev
, -1, &vmstate_max111x
, s
);
149 static int max1110_init(SSISlave
*dev
)
151 return max111x_init(dev
, 8);
154 static int max1111_init(SSISlave
*dev
)
156 return max111x_init(dev
, 4);
159 void max111x_set_input(DeviceState
*dev
, int line
, uint8_t value
)
161 MAX111xState
*s
= MAX_111X(dev
);
162 assert(line
>= 0 && line
< s
->inputs
);
163 s
->input
[line
] = value
;
166 static void max111x_class_init(ObjectClass
*klass
, void *data
)
168 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
170 k
->transfer
= max111x_transfer
;
173 static const TypeInfo max111x_info
= {
174 .name
= TYPE_MAX_111X
,
175 .parent
= TYPE_SSI_SLAVE
,
176 .instance_size
= sizeof(MAX111xState
),
177 .class_init
= max111x_class_init
,
181 static void max1110_class_init(ObjectClass
*klass
, void *data
)
183 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
185 k
->init
= max1110_init
;
188 static const TypeInfo max1110_info
= {
189 .name
= TYPE_MAX_1110
,
190 .parent
= TYPE_MAX_111X
,
191 .class_init
= max1110_class_init
,
194 static void max1111_class_init(ObjectClass
*klass
, void *data
)
196 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
198 k
->init
= max1111_init
;
201 static const TypeInfo max1111_info
= {
202 .name
= TYPE_MAX_1111
,
203 .parent
= TYPE_MAX_111X
,
204 .class_init
= max1111_class_init
,
207 static void max111x_register_types(void)
209 type_register_static(&max111x_info
);
210 type_register_static(&max1110_info
);
211 type_register_static(&max1111_info
);
214 type_init(max111x_register_types
)