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.
18 uint8_t tb1
, rb2
, rb3
;
25 /* Control-byte bitfields */
26 #define CB_PD0 (1 << 0)
27 #define CB_PD1 (1 << 1)
28 #define CB_SGL (1 << 2)
29 #define CB_UNI (1 << 3)
30 #define CB_SEL0 (1 << 4)
31 #define CB_SEL1 (1 << 5)
32 #define CB_SEL2 (1 << 6)
33 #define CB_START (1 << 7)
35 #define CHANNEL_NUM(v, b0, b1, b2) \
36 ((((v) >> (2 + (b0))) & 4) | \
37 (((v) >> (3 + (b1))) & 2) | \
38 (((v) >> (4 + (b2))) & 1))
40 static uint32_t max111x_read(MAX111xState
*s
)
45 switch (s
->cycle
++) {
55 /* Interpret a control-byte */
56 static void max111x_write(MAX111xState
*s
, uint32_t value
)
60 /* Ignore the value if START bit is zero */
61 if (!(value
& CB_START
))
66 if (!(value
& CB_PD1
)) {
74 chan
= CHANNEL_NUM(value
, 1, 0, 2);
76 chan
= CHANNEL_NUM(value
& ~CB_SEL0
, 0, 1, 2);
79 measure
= s
->input
[chan
] - s
->com
;
81 measure
= s
->input
[chan
] - s
->input
[chan
^ 1];
83 if (!(value
& CB_UNI
))
86 s
->rb2
= (measure
>> 2) & 0x3f;
87 s
->rb3
= (measure
<< 6) & 0xc0;
89 /* FIXME: When should the IRQ be lowered? */
90 qemu_irq_raise(s
->interrupt
);
93 static uint32_t max111x_transfer(SSISlave
*dev
, uint32_t value
)
95 MAX111xState
*s
= FROM_SSI_SLAVE(MAX111xState
, dev
);
96 max111x_write(s
, value
);
97 return max111x_read(s
);
100 static const VMStateDescription vmstate_max111x
= {
103 .minimum_version_id
= 1,
104 .minimum_version_id_old
= 1,
105 .fields
= (VMStateField
[]) {
106 VMSTATE_SSI_SLAVE(ssidev
, MAX111xState
),
107 VMSTATE_UINT8(tb1
, MAX111xState
),
108 VMSTATE_UINT8(rb2
, MAX111xState
),
109 VMSTATE_UINT8(rb3
, MAX111xState
),
110 VMSTATE_INT32_EQUAL(inputs
, MAX111xState
),
111 VMSTATE_INT32(com
, MAX111xState
),
112 VMSTATE_ARRAY_INT32_UNSAFE(input
, MAX111xState
, inputs
,
113 vmstate_info_uint8
, uint8_t),
114 VMSTATE_END_OF_LIST()
118 static int max111x_init(SSISlave
*d
, int inputs
)
120 DeviceState
*dev
= DEVICE(d
);
121 MAX111xState
*s
= FROM_SSI_SLAVE(MAX111xState
, d
);
123 qdev_init_gpio_out(dev
, &s
->interrupt
, 1);
126 /* TODO: add a user interface for setting these */
137 vmstate_register(dev
, -1, &vmstate_max111x
, s
);
141 static int max1110_init(SSISlave
*dev
)
143 return max111x_init(dev
, 8);
146 static int max1111_init(SSISlave
*dev
)
148 return max111x_init(dev
, 4);
151 void max111x_set_input(DeviceState
*dev
, int line
, uint8_t value
)
153 MAX111xState
*s
= FROM_SSI_SLAVE(MAX111xState
, SSI_SLAVE_FROM_QDEV(dev
));
154 assert(line
>= 0 && line
< s
->inputs
);
155 s
->input
[line
] = value
;
158 static void max1110_class_init(ObjectClass
*klass
, void *data
)
160 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
162 k
->init
= max1110_init
;
163 k
->transfer
= max111x_transfer
;
166 static const TypeInfo max1110_info
= {
168 .parent
= TYPE_SSI_SLAVE
,
169 .instance_size
= sizeof(MAX111xState
),
170 .class_init
= max1110_class_init
,
173 static void max1111_class_init(ObjectClass
*klass
, void *data
)
175 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
177 k
->init
= max1111_init
;
178 k
->transfer
= max111x_transfer
;
181 static const TypeInfo max1111_info
= {
183 .parent
= TYPE_SSI_SLAVE
,
184 .instance_size
= sizeof(MAX111xState
),
185 .class_init
= max1111_class_init
,
188 static void max111x_register_types(void)
190 type_register_static(&max1110_info
);
191 type_register_static(&max1111_info
);
194 type_init(max111x_register_types
)