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"
13 typedef struct I2CNode I2CNode
;
17 QLIST_ENTRY(I2CNode
) next
;
20 #define I2C_BROADCAST 0x00
24 QLIST_HEAD(, I2CNode
) current_devs
;
25 uint8_t saved_address
;
29 static Property i2c_props
[] = {
30 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
31 DEFINE_PROP_END_OF_LIST(),
34 #define TYPE_I2C_BUS "i2c-bus"
35 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
37 static const TypeInfo i2c_bus_info
= {
40 .instance_size
= sizeof(I2CBus
),
43 static int i2c_bus_pre_save(void *opaque
)
47 bus
->saved_address
= -1;
48 if (!QLIST_EMPTY(&bus
->current_devs
)) {
49 if (!bus
->broadcast
) {
50 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
52 bus
->saved_address
= I2C_BROADCAST
;
59 static const VMStateDescription vmstate_i2c_bus
= {
62 .minimum_version_id
= 1,
63 .pre_save
= i2c_bus_pre_save
,
64 .fields
= (VMStateField
[]) {
65 VMSTATE_UINT8(saved_address
, I2CBus
),
70 /* Create a new I2C bus. */
71 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
75 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
76 QLIST_INIT(&bus
->current_devs
);
77 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
81 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
83 dev
->address
= address
;
86 /* Return nonzero if bus is busy. */
87 int i2c_bus_busy(I2CBus
*bus
)
89 return !QLIST_EMPTY(&bus
->current_devs
);
92 /* TODO: Make this handle multiple masters. */
94 * Start or continue an i2c transaction. When this is called for the
95 * first time or after an i2c_end_transfer(), if it returns an error
96 * the bus transaction is terminated (or really never started). If
97 * this is called after another i2c_start_transfer() without an
98 * intervening i2c_end_transfer(), and it returns an error, the
99 * transaction will not be terminated. The caller must do it.
101 * This corresponds with the way real hardware works. The SMBus
102 * protocol uses a start transfer to switch from write to read mode
103 * without releasing the bus. If that fails, the bus is still
106 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, int recv
)
111 bool bus_scanned
= false;
113 if (address
== I2C_BROADCAST
) {
115 * This is a broadcast, the current_devs will be all the devices of the
118 bus
->broadcast
= true;
122 * If there are already devices in the list, that means we are in
123 * the middle of a transaction and we shouldn't rescan the bus.
125 * This happens with any SMBus transaction, even on a pure I2C
126 * device. The interface does a transaction start without
127 * terminating the previous transaction.
129 if (QLIST_EMPTY(&bus
->current_devs
)) {
130 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
131 DeviceState
*qdev
= kid
->child
;
132 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
133 if ((candidate
->address
== address
) || (bus
->broadcast
)) {
134 node
= g_malloc(sizeof(struct I2CNode
));
135 node
->elt
= candidate
;
136 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
137 if (!bus
->broadcast
) {
145 if (QLIST_EMPTY(&bus
->current_devs
)) {
149 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
152 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
153 /* If the bus is already busy, assume this is a repeated
157 rv
= sc
->event(node
->elt
, recv
? I2C_START_RECV
: I2C_START_SEND
);
158 if (rv
&& !bus
->broadcast
) {
160 /* First call, terminate the transfer. */
161 i2c_end_transfer(bus
);
170 void i2c_end_transfer(I2CBus
*bus
)
173 I2CNode
*node
, *next
;
175 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
176 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
178 sc
->event(node
->elt
, I2C_FINISH
);
180 QLIST_REMOVE(node
, next
);
183 bus
->broadcast
= false;
186 int i2c_send_recv(I2CBus
*bus
, uint8_t *data
, bool send
)
193 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
194 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
196 ret
= ret
|| sc
->send(node
->elt
, *data
);
203 if ((QLIST_EMPTY(&bus
->current_devs
)) || (bus
->broadcast
)) {
207 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
209 ret
= sc
->recv(QLIST_FIRST(&bus
->current_devs
)->elt
);
221 int i2c_send(I2CBus
*bus
, uint8_t data
)
223 return i2c_send_recv(bus
, &data
, true);
226 int i2c_recv(I2CBus
*bus
)
229 int ret
= i2c_send_recv(bus
, &data
, false);
231 return ret
< 0 ? ret
: data
;
234 void i2c_nack(I2CBus
*bus
)
239 if (QLIST_EMPTY(&bus
->current_devs
)) {
243 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
244 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
246 sc
->event(node
->elt
, I2C_NACK
);
251 static int i2c_slave_post_load(void *opaque
, int version_id
)
253 I2CSlave
*dev
= opaque
;
257 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
258 if ((bus
->saved_address
== dev
->address
) ||
259 (bus
->saved_address
== I2C_BROADCAST
)) {
260 node
= g_malloc(sizeof(struct I2CNode
));
262 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
267 const VMStateDescription vmstate_i2c_slave
= {
270 .minimum_version_id
= 1,
271 .post_load
= i2c_slave_post_load
,
272 .fields
= (VMStateField
[]) {
273 VMSTATE_UINT8(address
, I2CSlave
),
274 VMSTATE_END_OF_LIST()
278 static int i2c_slave_qdev_init(DeviceState
*dev
)
280 I2CSlave
*s
= I2C_SLAVE(dev
);
281 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(s
);
290 DeviceState
*i2c_create_slave(I2CBus
*bus
, const char *name
, uint8_t addr
)
294 dev
= qdev_create(&bus
->qbus
, name
);
295 qdev_prop_set_uint8(dev
, "address", addr
);
296 qdev_init_nofail(dev
);
300 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
302 DeviceClass
*k
= DEVICE_CLASS(klass
);
303 k
->init
= i2c_slave_qdev_init
;
304 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
305 k
->bus_type
= TYPE_I2C_BUS
;
306 k
->props
= i2c_props
;
309 static const TypeInfo i2c_slave_type_info
= {
310 .name
= TYPE_I2C_SLAVE
,
311 .parent
= TYPE_DEVICE
,
312 .instance_size
= sizeof(I2CSlave
),
314 .class_size
= sizeof(I2CSlaveClass
),
315 .class_init
= i2c_slave_class_init
,
318 static void i2c_slave_register_types(void)
320 type_register_static(&i2c_bus_info
);
321 type_register_static(&i2c_slave_type_info
);
324 type_init(i2c_slave_register_types
)