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 "migration/vmstate.h"
13 #include "qemu/module.h"
16 #define I2C_BROADCAST 0x00
18 static Property i2c_props
[] = {
19 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
20 DEFINE_PROP_END_OF_LIST(),
23 static const TypeInfo i2c_bus_info
= {
26 .instance_size
= sizeof(I2CBus
),
29 static int i2c_bus_pre_save(void *opaque
)
33 bus
->saved_address
= -1;
34 if (!QLIST_EMPTY(&bus
->current_devs
)) {
35 if (!bus
->broadcast
) {
36 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
38 bus
->saved_address
= I2C_BROADCAST
;
45 static const VMStateDescription vmstate_i2c_bus
= {
48 .minimum_version_id
= 1,
49 .pre_save
= i2c_bus_pre_save
,
50 .fields
= (VMStateField
[]) {
51 VMSTATE_UINT8(saved_address
, I2CBus
),
56 /* Create a new I2C bus. */
57 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
61 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
62 QLIST_INIT(&bus
->current_devs
);
63 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
67 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
69 dev
->address
= address
;
72 /* Return nonzero if bus is busy. */
73 int i2c_bus_busy(I2CBus
*bus
)
75 return !QLIST_EMPTY(&bus
->current_devs
);
78 /* TODO: Make this handle multiple masters. */
80 * Start or continue an i2c transaction. When this is called for the
81 * first time or after an i2c_end_transfer(), if it returns an error
82 * the bus transaction is terminated (or really never started). If
83 * this is called after another i2c_start_transfer() without an
84 * intervening i2c_end_transfer(), and it returns an error, the
85 * transaction will not be terminated. The caller must do it.
87 * This corresponds with the way real hardware works. The SMBus
88 * protocol uses a start transfer to switch from write to read mode
89 * without releasing the bus. If that fails, the bus is still
92 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, int recv
)
97 bool bus_scanned
= false;
99 if (address
== I2C_BROADCAST
) {
101 * This is a broadcast, the current_devs will be all the devices of the
104 bus
->broadcast
= true;
108 * If there are already devices in the list, that means we are in
109 * the middle of a transaction and we shouldn't rescan the bus.
111 * This happens with any SMBus transaction, even on a pure I2C
112 * device. The interface does a transaction start without
113 * terminating the previous transaction.
115 if (QLIST_EMPTY(&bus
->current_devs
)) {
116 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
117 DeviceState
*qdev
= kid
->child
;
118 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
119 if ((candidate
->address
== address
) || (bus
->broadcast
)) {
120 node
= g_malloc(sizeof(struct I2CNode
));
121 node
->elt
= candidate
;
122 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
123 if (!bus
->broadcast
) {
131 if (QLIST_EMPTY(&bus
->current_devs
)) {
135 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
136 I2CSlave
*s
= node
->elt
;
139 sc
= I2C_SLAVE_GET_CLASS(s
);
140 /* If the bus is already busy, assume this is a repeated
144 trace_i2c_event("start", s
->address
);
145 rv
= sc
->event(s
, recv
? I2C_START_RECV
: I2C_START_SEND
);
146 if (rv
&& !bus
->broadcast
) {
148 /* First call, terminate the transfer. */
149 i2c_end_transfer(bus
);
158 void i2c_end_transfer(I2CBus
*bus
)
161 I2CNode
*node
, *next
;
163 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
164 I2CSlave
*s
= node
->elt
;
165 sc
= I2C_SLAVE_GET_CLASS(s
);
167 trace_i2c_event("finish", s
->address
);
168 sc
->event(s
, I2C_FINISH
);
170 QLIST_REMOVE(node
, next
);
173 bus
->broadcast
= false;
176 int i2c_send_recv(I2CBus
*bus
, uint8_t *data
, bool send
)
184 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
186 sc
= I2C_SLAVE_GET_CLASS(s
);
188 trace_i2c_send(s
->address
, *data
);
189 ret
= ret
|| sc
->send(s
, *data
);
197 if (!QLIST_EMPTY(&bus
->current_devs
) && !bus
->broadcast
) {
198 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
200 s
= QLIST_FIRST(&bus
->current_devs
)->elt
;
202 trace_i2c_recv(s
->address
, ret
);
210 int i2c_send(I2CBus
*bus
, uint8_t data
)
212 return i2c_send_recv(bus
, &data
, true);
215 uint8_t i2c_recv(I2CBus
*bus
)
219 i2c_send_recv(bus
, &data
, false);
223 void i2c_nack(I2CBus
*bus
)
228 if (QLIST_EMPTY(&bus
->current_devs
)) {
232 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
233 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
235 trace_i2c_event("nack", node
->elt
->address
);
236 sc
->event(node
->elt
, I2C_NACK
);
241 static int i2c_slave_post_load(void *opaque
, int version_id
)
243 I2CSlave
*dev
= opaque
;
247 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
248 if ((bus
->saved_address
== dev
->address
) ||
249 (bus
->saved_address
== I2C_BROADCAST
)) {
250 node
= g_malloc(sizeof(struct I2CNode
));
252 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
257 const VMStateDescription vmstate_i2c_slave
= {
260 .minimum_version_id
= 1,
261 .post_load
= i2c_slave_post_load
,
262 .fields
= (VMStateField
[]) {
263 VMSTATE_UINT8(address
, I2CSlave
),
264 VMSTATE_END_OF_LIST()
268 DeviceState
*i2c_create_slave(I2CBus
*bus
, const char *name
, uint8_t addr
)
272 dev
= qdev_create(&bus
->qbus
, name
);
273 qdev_prop_set_uint8(dev
, "address", addr
);
274 qdev_init_nofail(dev
);
278 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
280 DeviceClass
*k
= DEVICE_CLASS(klass
);
281 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
282 k
->bus_type
= TYPE_I2C_BUS
;
283 k
->props
= i2c_props
;
286 static const TypeInfo i2c_slave_type_info
= {
287 .name
= TYPE_I2C_SLAVE
,
288 .parent
= TYPE_DEVICE
,
289 .instance_size
= sizeof(I2CSlave
),
291 .class_size
= sizeof(I2CSlaveClass
),
292 .class_init
= i2c_slave_class_init
,
295 static void i2c_slave_register_types(void)
297 type_register_static(&i2c_bus_info
);
298 type_register_static(&i2c_slave_type_info
);
301 type_init(i2c_slave_register_types
)