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 #define TYPE_I2C_BUS "i2c-bus"
26 #define I2C_BUS(obj) OBJECT_CHECK(i2c_bus, (obj), TYPE_I2C_BUS)
28 static const TypeInfo i2c_bus_info
= {
31 .instance_size
= sizeof(i2c_bus
),
34 static void i2c_bus_pre_save(void *opaque
)
36 i2c_bus
*bus
= opaque
;
38 bus
->saved_address
= bus
->current_dev
? bus
->current_dev
->address
: -1;
41 static int i2c_bus_post_load(void *opaque
, int version_id
)
43 i2c_bus
*bus
= opaque
;
45 /* The bus is loaded before attached devices, so load and save the
46 current device id. Devices will check themselves as loaded. */
47 bus
->current_dev
= NULL
;
51 static const VMStateDescription vmstate_i2c_bus
= {
54 .minimum_version_id
= 1,
55 .minimum_version_id_old
= 1,
56 .pre_save
= i2c_bus_pre_save
,
57 .post_load
= i2c_bus_post_load
,
58 .fields
= (VMStateField
[]) {
59 VMSTATE_UINT8(saved_address
, i2c_bus
),
64 /* Create a new I2C bus. */
65 i2c_bus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
69 bus
= FROM_QBUS(i2c_bus
, qbus_create(TYPE_I2C_BUS
, parent
, name
));
70 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
74 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
76 dev
->address
= address
;
79 /* Return nonzero if bus is busy. */
80 int i2c_bus_busy(i2c_bus
*bus
)
82 return bus
->current_dev
!= NULL
;
85 /* Returns non-zero if the address is not valid. */
86 /* TODO: Make this handle multiple masters. */
87 int i2c_start_transfer(i2c_bus
*bus
, uint8_t address
, int recv
)
90 I2CSlave
*slave
= NULL
;
93 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
94 DeviceState
*qdev
= kid
->child
;
95 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
96 if (candidate
->address
== address
) {
106 sc
= I2C_SLAVE_GET_CLASS(slave
);
107 /* If the bus is already busy, assume this is a repeated
109 bus
->current_dev
= slave
;
111 sc
->event(slave
, recv
? I2C_START_RECV
: I2C_START_SEND
);
116 void i2c_end_transfer(i2c_bus
*bus
)
118 I2CSlave
*dev
= bus
->current_dev
;
125 sc
= I2C_SLAVE_GET_CLASS(dev
);
127 sc
->event(dev
, I2C_FINISH
);
130 bus
->current_dev
= NULL
;
133 int i2c_send(i2c_bus
*bus
, uint8_t data
)
135 I2CSlave
*dev
= bus
->current_dev
;
142 sc
= I2C_SLAVE_GET_CLASS(dev
);
144 return sc
->send(dev
, data
);
150 int i2c_recv(i2c_bus
*bus
)
152 I2CSlave
*dev
= bus
->current_dev
;
159 sc
= I2C_SLAVE_GET_CLASS(dev
);
161 return sc
->recv(dev
);
167 void i2c_nack(i2c_bus
*bus
)
169 I2CSlave
*dev
= bus
->current_dev
;
176 sc
= I2C_SLAVE_GET_CLASS(dev
);
178 sc
->event(dev
, I2C_NACK
);
182 static int i2c_slave_post_load(void *opaque
, int version_id
)
184 I2CSlave
*dev
= opaque
;
186 bus
= FROM_QBUS(i2c_bus
, qdev_get_parent_bus(&dev
->qdev
));
187 if (bus
->saved_address
== dev
->address
) {
188 bus
->current_dev
= dev
;
193 const VMStateDescription vmstate_i2c_slave
= {
196 .minimum_version_id
= 1,
197 .minimum_version_id_old
= 1,
198 .post_load
= i2c_slave_post_load
,
199 .fields
= (VMStateField
[]) {
200 VMSTATE_UINT8(address
, I2CSlave
),
201 VMSTATE_END_OF_LIST()
205 static int i2c_slave_qdev_init(DeviceState
*dev
)
207 I2CSlave
*s
= I2C_SLAVE(dev
);
208 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(s
);
213 DeviceState
*i2c_create_slave(i2c_bus
*bus
, const char *name
, uint8_t addr
)
217 dev
= qdev_create(&bus
->qbus
, name
);
218 qdev_prop_set_uint8(dev
, "address", addr
);
219 qdev_init_nofail(dev
);
223 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
225 DeviceClass
*k
= DEVICE_CLASS(klass
);
226 k
->init
= i2c_slave_qdev_init
;
227 k
->bus_type
= TYPE_I2C_BUS
;
228 k
->props
= i2c_props
;
231 static const TypeInfo i2c_slave_type_info
= {
232 .name
= TYPE_I2C_SLAVE
,
233 .parent
= TYPE_DEVICE
,
234 .instance_size
= sizeof(I2CSlave
),
236 .class_size
= sizeof(I2CSlaveClass
),
237 .class_init
= i2c_slave_class_init
,
240 static void i2c_slave_register_types(void)
242 type_register_static(&i2c_bus_info
);
243 type_register_static(&i2c_slave_type_info
);
246 type_init(i2c_slave_register_types
)