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"
18 #define I2C_BROADCAST 0x00
20 static Property i2c_props
[] = {
21 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
22 DEFINE_PROP_END_OF_LIST(),
25 static const TypeInfo i2c_bus_info
= {
28 .instance_size
= sizeof(I2CBus
),
31 static int i2c_bus_pre_save(void *opaque
)
35 bus
->saved_address
= -1;
36 if (!QLIST_EMPTY(&bus
->current_devs
)) {
37 if (!bus
->broadcast
) {
38 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
40 bus
->saved_address
= I2C_BROADCAST
;
47 static const VMStateDescription vmstate_i2c_bus
= {
50 .minimum_version_id
= 1,
51 .pre_save
= i2c_bus_pre_save
,
52 .fields
= (VMStateField
[]) {
53 VMSTATE_UINT8(saved_address
, I2CBus
),
58 /* Create a new I2C bus. */
59 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
63 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
64 QLIST_INIT(&bus
->current_devs
);
65 vmstate_register(NULL
, VMSTATE_INSTANCE_ID_ANY
, &vmstate_i2c_bus
, bus
);
69 void i2c_slave_set_address(I2CSlave
*dev
, uint8_t address
)
71 dev
->address
= address
;
74 /* Return nonzero if bus is busy. */
75 int i2c_bus_busy(I2CBus
*bus
)
77 return !QLIST_EMPTY(&bus
->current_devs
);
80 bool i2c_scan_bus(I2CBus
*bus
, uint8_t address
, bool broadcast
,
81 I2CNodeList
*current_devs
)
85 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
86 DeviceState
*qdev
= kid
->child
;
87 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
88 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(candidate
);
90 if (sc
->match_and_add(candidate
, address
, broadcast
, current_devs
)) {
98 * If broadcast was true, and the list was full or empty, return true. If
99 * broadcast was false, return false.
104 /* TODO: Make this handle multiple masters. */
106 * Start or continue an i2c transaction. When this is called for the
107 * first time or after an i2c_end_transfer(), if it returns an error
108 * the bus transaction is terminated (or really never started). If
109 * this is called after another i2c_start_transfer() without an
110 * intervening i2c_end_transfer(), and it returns an error, the
111 * transaction will not be terminated. The caller must do it.
113 * This corresponds with the way real hardware works. The SMBus
114 * protocol uses a start transfer to switch from write to read mode
115 * without releasing the bus. If that fails, the bus is still
118 * @event must be I2C_START_RECV or I2C_START_SEND.
120 static int i2c_do_start_transfer(I2CBus
*bus
, uint8_t address
,
121 enum i2c_event event
)
125 bool bus_scanned
= false;
127 if (address
== I2C_BROADCAST
) {
129 * This is a broadcast, the current_devs will be all the devices of the
132 bus
->broadcast
= true;
136 * If there are already devices in the list, that means we are in
137 * the middle of a transaction and we shouldn't rescan the bus.
139 * This happens with any SMBus transaction, even on a pure I2C
140 * device. The interface does a transaction start without
141 * terminating the previous transaction.
143 if (QLIST_EMPTY(&bus
->current_devs
)) {
144 /* Disregard whether devices were found. */
145 (void)i2c_scan_bus(bus
, address
, bus
->broadcast
, &bus
->current_devs
);
149 if (QLIST_EMPTY(&bus
->current_devs
)) {
153 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
154 I2CSlave
*s
= node
->elt
;
157 sc
= I2C_SLAVE_GET_CLASS(s
);
158 /* If the bus is already busy, assume this is a repeated
162 trace_i2c_event("start", s
->address
);
163 rv
= sc
->event(s
, event
);
164 if (rv
&& !bus
->broadcast
) {
166 /* First call, terminate the transfer. */
167 i2c_end_transfer(bus
);
176 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, bool is_recv
)
178 return i2c_do_start_transfer(bus
, address
, is_recv
183 int i2c_start_recv(I2CBus
*bus
, uint8_t address
)
185 return i2c_do_start_transfer(bus
, address
, I2C_START_RECV
);
188 int i2c_start_send(I2CBus
*bus
, uint8_t address
)
190 return i2c_do_start_transfer(bus
, address
, I2C_START_SEND
);
193 void i2c_end_transfer(I2CBus
*bus
)
196 I2CNode
*node
, *next
;
198 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
199 I2CSlave
*s
= node
->elt
;
200 sc
= I2C_SLAVE_GET_CLASS(s
);
202 trace_i2c_event("finish", s
->address
);
203 sc
->event(s
, I2C_FINISH
);
205 QLIST_REMOVE(node
, next
);
208 bus
->broadcast
= false;
211 int i2c_send(I2CBus
*bus
, uint8_t data
)
218 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
220 sc
= I2C_SLAVE_GET_CLASS(s
);
222 trace_i2c_send(s
->address
, data
);
223 ret
= ret
|| sc
->send(s
, data
);
232 uint8_t i2c_recv(I2CBus
*bus
)
238 if (!QLIST_EMPTY(&bus
->current_devs
) && !bus
->broadcast
) {
239 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
241 s
= QLIST_FIRST(&bus
->current_devs
)->elt
;
243 trace_i2c_recv(s
->address
, data
);
250 void i2c_nack(I2CBus
*bus
)
255 if (QLIST_EMPTY(&bus
->current_devs
)) {
259 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
260 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
262 trace_i2c_event("nack", node
->elt
->address
);
263 sc
->event(node
->elt
, I2C_NACK
);
268 static int i2c_slave_post_load(void *opaque
, int version_id
)
270 I2CSlave
*dev
= opaque
;
274 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
275 if ((bus
->saved_address
== dev
->address
) ||
276 (bus
->saved_address
== I2C_BROADCAST
)) {
277 node
= g_malloc(sizeof(struct I2CNode
));
279 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
284 const VMStateDescription vmstate_i2c_slave
= {
287 .minimum_version_id
= 1,
288 .post_load
= i2c_slave_post_load
,
289 .fields
= (VMStateField
[]) {
290 VMSTATE_UINT8(address
, I2CSlave
),
291 VMSTATE_END_OF_LIST()
295 I2CSlave
*i2c_slave_new(const char *name
, uint8_t addr
)
299 dev
= qdev_new(name
);
300 qdev_prop_set_uint8(dev
, "address", addr
);
301 return I2C_SLAVE(dev
);
304 bool i2c_slave_realize_and_unref(I2CSlave
*dev
, I2CBus
*bus
, Error
**errp
)
306 return qdev_realize_and_unref(&dev
->qdev
, &bus
->qbus
, errp
);
309 I2CSlave
*i2c_slave_create_simple(I2CBus
*bus
, const char *name
, uint8_t addr
)
311 I2CSlave
*dev
= i2c_slave_new(name
, addr
);
313 i2c_slave_realize_and_unref(dev
, bus
, &error_abort
);
318 static bool i2c_slave_match(I2CSlave
*candidate
, uint8_t address
,
319 bool broadcast
, I2CNodeList
*current_devs
)
321 if ((candidate
->address
== address
) || (broadcast
)) {
322 I2CNode
*node
= g_malloc(sizeof(struct I2CNode
));
323 node
->elt
= candidate
;
324 QLIST_INSERT_HEAD(current_devs
, node
, next
);
328 /* Not found and not broadcast. */
332 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
334 DeviceClass
*k
= DEVICE_CLASS(klass
);
335 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
336 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
337 k
->bus_type
= TYPE_I2C_BUS
;
338 device_class_set_props(k
, i2c_props
);
339 sc
->match_and_add
= i2c_slave_match
;
342 static const TypeInfo i2c_slave_type_info
= {
343 .name
= TYPE_I2C_SLAVE
,
344 .parent
= TYPE_DEVICE
,
345 .instance_size
= sizeof(I2CSlave
),
347 .class_size
= sizeof(I2CSlaveClass
),
348 .class_init
= i2c_slave_class_init
,
351 static void i2c_slave_register_types(void)
353 type_register_static(&i2c_bus_info
);
354 type_register_static(&i2c_slave_type_info
);
357 type_init(i2c_slave_register_types
)