2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 #include "hw/qdev-properties.h"
13 #include "migration/vmstate.h"
14 #include "qemu/module.h"
17 #define I2C_BROADCAST 0x00
19 static Property i2c_props
[] = {
20 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
21 DEFINE_PROP_END_OF_LIST(),
24 static const TypeInfo i2c_bus_info
= {
27 .instance_size
= sizeof(I2CBus
),
30 static int i2c_bus_pre_save(void *opaque
)
34 bus
->saved_address
= -1;
35 if (!QLIST_EMPTY(&bus
->current_devs
)) {
36 if (!bus
->broadcast
) {
37 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
39 bus
->saved_address
= I2C_BROADCAST
;
46 static const VMStateDescription vmstate_i2c_bus
= {
49 .minimum_version_id
= 1,
50 .pre_save
= i2c_bus_pre_save
,
51 .fields
= (VMStateField
[]) {
52 VMSTATE_UINT8(saved_address
, I2CBus
),
57 /* Create a new I2C bus. */
58 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
62 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
63 QLIST_INIT(&bus
->current_devs
);
64 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
68 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
70 dev
->address
= address
;
73 /* Return nonzero if bus is busy. */
74 int i2c_bus_busy(I2CBus
*bus
)
76 return !QLIST_EMPTY(&bus
->current_devs
);
79 /* TODO: Make this handle multiple masters. */
81 * Start or continue an i2c transaction. When this is called for the
82 * first time or after an i2c_end_transfer(), if it returns an error
83 * the bus transaction is terminated (or really never started). If
84 * this is called after another i2c_start_transfer() without an
85 * intervening i2c_end_transfer(), and it returns an error, the
86 * transaction will not be terminated. The caller must do it.
88 * This corresponds with the way real hardware works. The SMBus
89 * protocol uses a start transfer to switch from write to read mode
90 * without releasing the bus. If that fails, the bus is still
93 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, int recv
)
98 bool bus_scanned
= false;
100 if (address
== I2C_BROADCAST
) {
102 * This is a broadcast, the current_devs will be all the devices of the
105 bus
->broadcast
= true;
109 * If there are already devices in the list, that means we are in
110 * the middle of a transaction and we shouldn't rescan the bus.
112 * This happens with any SMBus transaction, even on a pure I2C
113 * device. The interface does a transaction start without
114 * terminating the previous transaction.
116 if (QLIST_EMPTY(&bus
->current_devs
)) {
117 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
118 DeviceState
*qdev
= kid
->child
;
119 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
120 if ((candidate
->address
== address
) || (bus
->broadcast
)) {
121 node
= g_malloc(sizeof(struct I2CNode
));
122 node
->elt
= candidate
;
123 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
124 if (!bus
->broadcast
) {
132 if (QLIST_EMPTY(&bus
->current_devs
)) {
136 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
137 I2CSlave
*s
= node
->elt
;
140 sc
= I2C_SLAVE_GET_CLASS(s
);
141 /* If the bus is already busy, assume this is a repeated
145 trace_i2c_event("start", s
->address
);
146 rv
= sc
->event(s
, recv
? I2C_START_RECV
: I2C_START_SEND
);
147 if (rv
&& !bus
->broadcast
) {
149 /* First call, terminate the transfer. */
150 i2c_end_transfer(bus
);
159 void i2c_end_transfer(I2CBus
*bus
)
162 I2CNode
*node
, *next
;
164 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
165 I2CSlave
*s
= node
->elt
;
166 sc
= I2C_SLAVE_GET_CLASS(s
);
168 trace_i2c_event("finish", s
->address
);
169 sc
->event(s
, I2C_FINISH
);
171 QLIST_REMOVE(node
, next
);
174 bus
->broadcast
= false;
177 int i2c_send_recv(I2CBus
*bus
, uint8_t *data
, bool send
)
185 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
187 sc
= I2C_SLAVE_GET_CLASS(s
);
189 trace_i2c_send(s
->address
, *data
);
190 ret
= ret
|| sc
->send(s
, *data
);
198 if (!QLIST_EMPTY(&bus
->current_devs
) && !bus
->broadcast
) {
199 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
201 s
= QLIST_FIRST(&bus
->current_devs
)->elt
;
203 trace_i2c_recv(s
->address
, ret
);
211 int i2c_send(I2CBus
*bus
, uint8_t data
)
213 return i2c_send_recv(bus
, &data
, true);
216 uint8_t i2c_recv(I2CBus
*bus
)
220 i2c_send_recv(bus
, &data
, false);
224 void i2c_nack(I2CBus
*bus
)
229 if (QLIST_EMPTY(&bus
->current_devs
)) {
233 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
234 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
236 trace_i2c_event("nack", node
->elt
->address
);
237 sc
->event(node
->elt
, I2C_NACK
);
242 static int i2c_slave_post_load(void *opaque
, int version_id
)
244 I2CSlave
*dev
= opaque
;
248 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
249 if ((bus
->saved_address
== dev
->address
) ||
250 (bus
->saved_address
== I2C_BROADCAST
)) {
251 node
= g_malloc(sizeof(struct I2CNode
));
253 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
258 const VMStateDescription vmstate_i2c_slave
= {
261 .minimum_version_id
= 1,
262 .post_load
= i2c_slave_post_load
,
263 .fields
= (VMStateField
[]) {
264 VMSTATE_UINT8(address
, I2CSlave
),
265 VMSTATE_END_OF_LIST()
269 DeviceState
*i2c_create_slave(I2CBus
*bus
, const char *name
, uint8_t addr
)
273 dev
= qdev_create(&bus
->qbus
, name
);
274 qdev_prop_set_uint8(dev
, "address", addr
);
275 qdev_init_nofail(dev
);
279 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
281 DeviceClass
*k
= DEVICE_CLASS(klass
);
282 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
283 k
->bus_type
= TYPE_I2C_BUS
;
284 k
->props
= i2c_props
;
287 static const TypeInfo i2c_slave_type_info
= {
288 .name
= TYPE_I2C_SLAVE
,
289 .parent
= TYPE_DEVICE
,
290 .instance_size
= sizeof(I2CSlave
),
292 .class_size
= sizeof(I2CSlaveClass
),
293 .class_init
= i2c_slave_class_init
,
296 static void i2c_slave_register_types(void)
298 type_register_static(&i2c_bus_info
);
299 type_register_static(&i2c_slave_type_info
);
302 type_init(i2c_slave_register_types
)