2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
15 I2CSlave
*current_dev
;
17 uint8_t saved_address
;
20 static Property i2c_props
[] = {
21 DEFINE_PROP_UINT8("address", struct I2CSlave
, address
, 0),
22 DEFINE_PROP_END_OF_LIST(),
25 static struct BusInfo i2c_bus_info
= {
27 .size
= sizeof(i2c_bus
),
31 static void i2c_bus_pre_save(void *opaque
)
33 i2c_bus
*bus
= opaque
;
35 bus
->saved_address
= bus
->current_dev
? bus
->current_dev
->address
: -1;
38 static int i2c_bus_post_load(void *opaque
, int version_id
)
40 i2c_bus
*bus
= opaque
;
42 /* The bus is loaded before attached devices, so load and save the
43 current device id. Devices will check themselves as loaded. */
44 bus
->current_dev
= NULL
;
48 static const VMStateDescription vmstate_i2c_bus
= {
51 .minimum_version_id
= 1,
52 .minimum_version_id_old
= 1,
53 .pre_save
= i2c_bus_pre_save
,
54 .post_load
= i2c_bus_post_load
,
55 .fields
= (VMStateField
[]) {
56 VMSTATE_UINT8(saved_address
, i2c_bus
),
61 /* Create a new I2C bus. */
62 i2c_bus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
66 bus
= FROM_QBUS(i2c_bus
, qbus_create(&i2c_bus_info
, parent
, name
));
67 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
71 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
73 dev
->address
= address
;
76 /* Return nonzero if bus is busy. */
77 int i2c_bus_busy(i2c_bus
*bus
)
79 return bus
->current_dev
!= NULL
;
82 /* Returns non-zero if the address is not valid. */
83 /* TODO: Make this handle multiple masters. */
84 int i2c_start_transfer(i2c_bus
*bus
, uint8_t address
, int recv
)
87 I2CSlave
*slave
= NULL
;
90 QTAILQ_FOREACH(qdev
, &bus
->qbus
.children
, sibling
) {
91 I2CSlave
*candidate
= I2C_SLAVE_FROM_QDEV(qdev
);
92 if (candidate
->address
== address
) {
102 sc
= I2C_SLAVE_GET_CLASS(slave
);
103 /* If the bus is already busy, assume this is a repeated
105 bus
->current_dev
= slave
;
107 sc
->event(slave
, recv
? I2C_START_RECV
: I2C_START_SEND
);
112 void i2c_end_transfer(i2c_bus
*bus
)
114 I2CSlave
*dev
= bus
->current_dev
;
121 sc
= I2C_SLAVE_GET_CLASS(dev
);
123 sc
->event(dev
, I2C_FINISH
);
126 bus
->current_dev
= NULL
;
129 int i2c_send(i2c_bus
*bus
, uint8_t data
)
131 I2CSlave
*dev
= bus
->current_dev
;
138 sc
= I2C_SLAVE_GET_CLASS(dev
);
140 return sc
->send(dev
, data
);
146 int i2c_recv(i2c_bus
*bus
)
148 I2CSlave
*dev
= bus
->current_dev
;
155 sc
= I2C_SLAVE_GET_CLASS(dev
);
157 return sc
->recv(dev
);
163 void i2c_nack(i2c_bus
*bus
)
165 I2CSlave
*dev
= bus
->current_dev
;
172 sc
= I2C_SLAVE_GET_CLASS(dev
);
174 sc
->event(dev
, I2C_NACK
);
178 static int i2c_slave_post_load(void *opaque
, int version_id
)
180 I2CSlave
*dev
= opaque
;
182 bus
= FROM_QBUS(i2c_bus
, qdev_get_parent_bus(&dev
->qdev
));
183 if (bus
->saved_address
== dev
->address
) {
184 bus
->current_dev
= dev
;
189 const VMStateDescription vmstate_i2c_slave
= {
192 .minimum_version_id
= 1,
193 .minimum_version_id_old
= 1,
194 .post_load
= i2c_slave_post_load
,
195 .fields
= (VMStateField
[]) {
196 VMSTATE_UINT8(address
, I2CSlave
),
197 VMSTATE_END_OF_LIST()
201 static int i2c_slave_qdev_init(DeviceState
*dev
)
203 I2CSlave
*s
= I2C_SLAVE_FROM_QDEV(dev
);
204 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(s
);
209 DeviceState
*i2c_create_slave(i2c_bus
*bus
, const char *name
, uint8_t addr
)
213 dev
= qdev_create(&bus
->qbus
, name
);
214 qdev_prop_set_uint8(dev
, "address", addr
);
215 qdev_init_nofail(dev
);
219 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
221 DeviceClass
*k
= DEVICE_CLASS(klass
);
222 k
->init
= i2c_slave_qdev_init
;
223 k
->bus_info
= &i2c_bus_info
;
226 static TypeInfo i2c_slave_type_info
= {
227 .name
= TYPE_I2C_SLAVE
,
228 .parent
= TYPE_DEVICE
,
229 .instance_size
= sizeof(I2CSlave
),
231 .class_size
= sizeof(I2CSlaveClass
),
232 .class_init
= i2c_slave_class_init
,
235 static void i2c_slave_register_types(void)
237 type_register_static(&i2c_slave_type_info
);
240 type_init(i2c_slave_register_types
)