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 "qemu/module.h"
15 #define I2C_BROADCAST 0x00
17 static Property i2c_props
[] = {
18 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
19 DEFINE_PROP_END_OF_LIST(),
22 static const TypeInfo i2c_bus_info
= {
25 .instance_size
= sizeof(I2CBus
),
28 static int i2c_bus_pre_save(void *opaque
)
32 bus
->saved_address
= -1;
33 if (!QLIST_EMPTY(&bus
->current_devs
)) {
34 if (!bus
->broadcast
) {
35 bus
->saved_address
= QLIST_FIRST(&bus
->current_devs
)->elt
->address
;
37 bus
->saved_address
= I2C_BROADCAST
;
44 static const VMStateDescription vmstate_i2c_bus
= {
47 .minimum_version_id
= 1,
48 .pre_save
= i2c_bus_pre_save
,
49 .fields
= (VMStateField
[]) {
50 VMSTATE_UINT8(saved_address
, I2CBus
),
55 /* Create a new I2C bus. */
56 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
60 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
61 QLIST_INIT(&bus
->current_devs
);
62 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
66 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
68 dev
->address
= address
;
71 /* Return nonzero if bus is busy. */
72 int i2c_bus_busy(I2CBus
*bus
)
74 return !QLIST_EMPTY(&bus
->current_devs
);
77 /* TODO: Make this handle multiple masters. */
79 * Start or continue an i2c transaction. When this is called for the
80 * first time or after an i2c_end_transfer(), if it returns an error
81 * the bus transaction is terminated (or really never started). If
82 * this is called after another i2c_start_transfer() without an
83 * intervening i2c_end_transfer(), and it returns an error, the
84 * transaction will not be terminated. The caller must do it.
86 * This corresponds with the way real hardware works. The SMBus
87 * protocol uses a start transfer to switch from write to read mode
88 * without releasing the bus. If that fails, the bus is still
91 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, int recv
)
96 bool bus_scanned
= false;
98 if (address
== I2C_BROADCAST
) {
100 * This is a broadcast, the current_devs will be all the devices of the
103 bus
->broadcast
= true;
107 * If there are already devices in the list, that means we are in
108 * the middle of a transaction and we shouldn't rescan the bus.
110 * This happens with any SMBus transaction, even on a pure I2C
111 * device. The interface does a transaction start without
112 * terminating the previous transaction.
114 if (QLIST_EMPTY(&bus
->current_devs
)) {
115 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
116 DeviceState
*qdev
= kid
->child
;
117 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
118 if ((candidate
->address
== address
) || (bus
->broadcast
)) {
119 node
= g_malloc(sizeof(struct I2CNode
));
120 node
->elt
= candidate
;
121 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
122 if (!bus
->broadcast
) {
130 if (QLIST_EMPTY(&bus
->current_devs
)) {
134 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
135 I2CSlave
*s
= node
->elt
;
138 sc
= I2C_SLAVE_GET_CLASS(s
);
139 /* If the bus is already busy, assume this is a repeated
143 trace_i2c_event("start", s
->address
);
144 rv
= sc
->event(s
, recv
? I2C_START_RECV
: I2C_START_SEND
);
145 if (rv
&& !bus
->broadcast
) {
147 /* First call, terminate the transfer. */
148 i2c_end_transfer(bus
);
157 void i2c_end_transfer(I2CBus
*bus
)
160 I2CNode
*node
, *next
;
162 QLIST_FOREACH_SAFE(node
, &bus
->current_devs
, next
, next
) {
163 I2CSlave
*s
= node
->elt
;
164 sc
= I2C_SLAVE_GET_CLASS(s
);
166 trace_i2c_event("finish", s
->address
);
167 sc
->event(s
, I2C_FINISH
);
169 QLIST_REMOVE(node
, next
);
172 bus
->broadcast
= false;
175 int i2c_send_recv(I2CBus
*bus
, uint8_t *data
, bool send
)
183 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
185 sc
= I2C_SLAVE_GET_CLASS(s
);
187 trace_i2c_send(s
->address
, *data
);
188 ret
= ret
|| sc
->send(s
, *data
);
196 if (!QLIST_EMPTY(&bus
->current_devs
) && !bus
->broadcast
) {
197 sc
= I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus
->current_devs
)->elt
);
199 s
= QLIST_FIRST(&bus
->current_devs
)->elt
;
201 trace_i2c_recv(s
->address
, ret
);
209 int i2c_send(I2CBus
*bus
, uint8_t data
)
211 return i2c_send_recv(bus
, &data
, true);
214 uint8_t i2c_recv(I2CBus
*bus
)
218 i2c_send_recv(bus
, &data
, false);
222 void i2c_nack(I2CBus
*bus
)
227 if (QLIST_EMPTY(&bus
->current_devs
)) {
231 QLIST_FOREACH(node
, &bus
->current_devs
, next
) {
232 sc
= I2C_SLAVE_GET_CLASS(node
->elt
);
234 trace_i2c_event("nack", node
->elt
->address
);
235 sc
->event(node
->elt
, I2C_NACK
);
240 static int i2c_slave_post_load(void *opaque
, int version_id
)
242 I2CSlave
*dev
= opaque
;
246 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
247 if ((bus
->saved_address
== dev
->address
) ||
248 (bus
->saved_address
== I2C_BROADCAST
)) {
249 node
= g_malloc(sizeof(struct I2CNode
));
251 QLIST_INSERT_HEAD(&bus
->current_devs
, node
, next
);
256 const VMStateDescription vmstate_i2c_slave
= {
259 .minimum_version_id
= 1,
260 .post_load
= i2c_slave_post_load
,
261 .fields
= (VMStateField
[]) {
262 VMSTATE_UINT8(address
, I2CSlave
),
263 VMSTATE_END_OF_LIST()
267 DeviceState
*i2c_create_slave(I2CBus
*bus
, const char *name
, uint8_t addr
)
271 dev
= qdev_create(&bus
->qbus
, name
);
272 qdev_prop_set_uint8(dev
, "address", addr
);
273 qdev_init_nofail(dev
);
277 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
279 DeviceClass
*k
= DEVICE_CLASS(klass
);
280 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
281 k
->bus_type
= TYPE_I2C_BUS
;
282 k
->props
= i2c_props
;
285 static const TypeInfo i2c_slave_type_info
= {
286 .name
= TYPE_I2C_SLAVE
,
287 .parent
= TYPE_DEVICE
,
288 .instance_size
= sizeof(I2CSlave
),
290 .class_size
= sizeof(I2CSlaveClass
),
291 .class_init
= i2c_slave_class_init
,
294 static void i2c_slave_register_types(void)
296 type_register_static(&i2c_bus_info
);
297 type_register_static(&i2c_slave_type_info
);
300 type_init(i2c_slave_register_types
)