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 "qapi/error.h"
15 #include "qemu/module.h"
16 #include "qemu/main-loop.h"
19 #define I2C_BROADCAST 0x00
21 static Property i2c_props
[] = {
22 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
23 DEFINE_PROP_END_OF_LIST(),
26 static const TypeInfo i2c_bus_info
= {
29 .instance_size
= sizeof(I2CBus
),
32 static int i2c_bus_pre_save(void *opaque
)
36 bus
->saved_address
= -1;
37 if (!QLIST_EMPTY(&bus
->current_devs
)) {
38 if (!bus
->broadcast
) {
39 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
41 bus
->saved_address
= I2C_BROADCAST
;
48 static const VMStateDescription vmstate_i2c_bus
= {
51 .minimum_version_id
= 1,
52 .pre_save
= i2c_bus_pre_save
,
53 .fields
= (VMStateField
[]) {
54 VMSTATE_UINT8(saved_address
, I2CBus
),
59 /* Create a new I2C bus. */
60 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
64 bus
= I2C_BUS(qbus_new(TYPE_I2C_BUS
, parent
, name
));
65 QLIST_INIT(&bus
->current_devs
);
66 QSIMPLEQ_INIT(&bus
->pending_masters
);
67 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
, &vmstate_i2c_bus
, bus
);
71 void i2c_slave_set_address(I2CSlave
*dev
, uint8_t address
)
73 dev
->address
= address
;
76 /* Return nonzero if bus is busy. */
77 int i2c_bus_busy(I2CBus
*bus
)
79 return !QLIST_EMPTY(&bus
->current_devs
) || bus
->bh
;
82 bool i2c_scan_bus(I2CBus
*bus
, uint8_t address
, bool broadcast
,
83 I2CNodeList
*current_devs
)
87 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
88 DeviceState
*qdev
= kid
->child
;
89 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
90 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(candidate
);
92 if (sc
->match_and_add(candidate
, address
, broadcast
, current_devs
)) {
100 * If broadcast was true, and the list was full or empty, return true. If
101 * broadcast was false, return false.
106 /* TODO: Make this handle multiple masters. */
108 * Start or continue an i2c transaction. When this is called for the
109 * first time or after an i2c_end_transfer(), if it returns an error
110 * the bus transaction is terminated (or really never started). If
111 * this is called after another i2c_start_transfer() without an
112 * intervening i2c_end_transfer(), and it returns an error, the
113 * transaction will not be terminated. The caller must do it.
115 * This corresponds with the way real hardware works. The SMBus
116 * protocol uses a start transfer to switch from write to read mode
117 * without releasing the bus. If that fails, the bus is still
120 * @event must be I2C_START_RECV or I2C_START_SEND.
122 static int i2c_do_start_transfer(I2CBus
*bus
, uint8_t address
,
123 enum i2c_event event
)
127 bool bus_scanned
= false;
129 if (address
== I2C_BROADCAST
) {
131 * This is a broadcast, the current_devs will be all the devices of the
134 bus
->broadcast
= true;
138 * If there are already devices in the list, that means we are in
139 * the middle of a transaction and we shouldn't rescan the bus.
141 * This happens with any SMBus transaction, even on a pure I2C
142 * device. The interface does a transaction start without
143 * terminating the previous transaction.
145 if (QLIST_EMPTY(&bus
->current_devs
)) {
146 /* Disregard whether devices were found. */
147 (void)i2c_scan_bus(bus
, address
, bus
->broadcast
, &bus
->current_devs
);
151 if (QLIST_EMPTY(&bus
->current_devs
)) {
155 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
156 I2CSlave
*s
= node
->elt
;
159 sc
= I2C_SLAVE_GET_CLASS(s
);
160 /* If the bus is already busy, assume this is a repeated
164 trace_i2c_event(event
== I2C_START_SEND
? "start" : "start_async",
166 rv
= sc
->event(s
, event
);
167 if (rv
&& !bus
->broadcast
) {
169 /* First call, terminate the transfer. */
170 i2c_end_transfer(bus
);
179 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, bool is_recv
)
181 return i2c_do_start_transfer(bus
, address
, is_recv
186 void i2c_bus_master(I2CBus
*bus
, QEMUBH
*bh
)
188 if (i2c_bus_busy(bus
)) {
189 I2CPendingMaster
*node
= g_new(struct I2CPendingMaster
, 1);
192 QSIMPLEQ_INSERT_TAIL(&bus
->pending_masters
, node
, entry
);
198 qemu_bh_schedule(bus
->bh
);
201 void i2c_bus_release(I2CBus
*bus
)
206 int i2c_start_recv(I2CBus
*bus
, uint8_t address
)
208 return i2c_do_start_transfer(bus
, address
, I2C_START_RECV
);
211 int i2c_start_send(I2CBus
*bus
, uint8_t address
)
213 return i2c_do_start_transfer(bus
, address
, I2C_START_SEND
);
216 int i2c_start_send_async(I2CBus
*bus
, uint8_t address
)
218 return i2c_do_start_transfer(bus
, address
, I2C_START_SEND_ASYNC
);
221 void i2c_end_transfer(I2CBus
*bus
)
224 I2CNode
*node
, *next
;
226 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
227 I2CSlave
*s
= node
->elt
;
228 sc
= I2C_SLAVE_GET_CLASS(s
);
230 trace_i2c_event("finish", s
->address
);
231 sc
->event(s
, I2C_FINISH
);
233 QLIST_REMOVE(node
, next
);
236 bus
->broadcast
= false;
238 if (!QSIMPLEQ_EMPTY(&bus
->pending_masters
)) {
239 I2CPendingMaster
*node
= QSIMPLEQ_FIRST(&bus
->pending_masters
);
242 QSIMPLEQ_REMOVE_HEAD(&bus
->pending_masters
, entry
);
245 qemu_bh_schedule(bus
->bh
);
249 int i2c_send(I2CBus
*bus
, uint8_t data
)
256 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
258 sc
= I2C_SLAVE_GET_CLASS(s
);
260 trace_i2c_send(s
->address
, data
);
261 ret
= ret
|| sc
->send(s
, data
);
270 int i2c_send_async(I2CBus
*bus
, uint8_t data
)
272 I2CNode
*node
= QLIST_FIRST(&bus
->current_devs
);
273 I2CSlave
*slave
= node
->elt
;
274 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(slave
);
276 if (!sc
->send_async
) {
280 trace_i2c_send_async(slave
->address
, data
);
282 sc
->send_async(slave
, data
);
287 uint8_t i2c_recv(I2CBus
*bus
)
293 if (!QLIST_EMPTY(&bus
->current_devs
) && !bus
->broadcast
) {
294 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
296 s
= QLIST_FIRST(&bus
->current_devs
)->elt
;
298 trace_i2c_recv(s
->address
, data
);
305 void i2c_nack(I2CBus
*bus
)
310 if (QLIST_EMPTY(&bus
->current_devs
)) {
314 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
315 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
317 trace_i2c_event("nack", node
->elt
->address
);
318 sc
->event(node
->elt
, I2C_NACK
);
323 void i2c_ack(I2CBus
*bus
)
331 qemu_bh_schedule(bus
->bh
);
334 static int i2c_slave_post_load(void *opaque
, int version_id
)
336 I2CSlave
*dev
= opaque
;
340 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
341 if ((bus
->saved_address
== dev
->address
) ||
342 (bus
->saved_address
== I2C_BROADCAST
)) {
343 node
= g_new(struct I2CNode
, 1);
345 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
350 const VMStateDescription vmstate_i2c_slave
= {
353 .minimum_version_id
= 1,
354 .post_load
= i2c_slave_post_load
,
355 .fields
= (VMStateField
[]) {
356 VMSTATE_UINT8(address
, I2CSlave
),
357 VMSTATE_END_OF_LIST()
361 I2CSlave
*i2c_slave_new(const char *name
, uint8_t addr
)
365 dev
= qdev_new(name
);
366 qdev_prop_set_uint8(dev
, "address", addr
);
367 return I2C_SLAVE(dev
);
370 bool i2c_slave_realize_and_unref(I2CSlave
*dev
, I2CBus
*bus
, Error
**errp
)
372 return qdev_realize_and_unref(&dev
->qdev
, &bus
->qbus
, errp
);
375 I2CSlave
*i2c_slave_create_simple(I2CBus
*bus
, const char *name
, uint8_t addr
)
377 I2CSlave
*dev
= i2c_slave_new(name
, addr
);
379 i2c_slave_realize_and_unref(dev
, bus
, &error_abort
);
384 static bool i2c_slave_match(I2CSlave
*candidate
, uint8_t address
,
385 bool broadcast
, I2CNodeList
*current_devs
)
387 if ((candidate
->address
== address
) || (broadcast
)) {
388 I2CNode
*node
= g_new(struct I2CNode
, 1);
389 node
->elt
= candidate
;
390 QLIST_INSERT_HEAD(current_devs
, node
, next
);
394 /* Not found and not broadcast. */
398 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
400 DeviceClass
*k
= DEVICE_CLASS(klass
);
401 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
402 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
403 k
->bus_type
= TYPE_I2C_BUS
;
404 device_class_set_props(k
, i2c_props
);
405 sc
->match_and_add
= i2c_slave_match
;
408 static const TypeInfo i2c_slave_type_info
= {
409 .name
= TYPE_I2C_SLAVE
,
410 .parent
= TYPE_DEVICE
,
411 .instance_size
= sizeof(I2CSlave
),
413 .class_size
= sizeof(I2CSlaveClass
),
414 .class_init
= i2c_slave_class_init
,
417 static void i2c_slave_register_types(void)
419 type_register_static(&i2c_bus_info
);
420 type_register_static(&i2c_slave_type_info
);
423 type_init(i2c_slave_register_types
)