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 "hw/i2c/i2c.h"
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(I2CBus, (obj), TYPE_I2C_BUS)
28 static const TypeInfo i2c_bus_info
= {
31 .instance_size
= sizeof(I2CBus
),
34 static void i2c_bus_pre_save(void *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
)
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 .pre_save
= i2c_bus_pre_save
,
56 .post_load
= i2c_bus_post_load
,
57 .fields
= (VMStateField
[]) {
58 VMSTATE_UINT8(saved_address
, I2CBus
),
63 /* Create a new I2C bus. */
64 I2CBus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
68 bus
= I2C_BUS(qbus_create(TYPE_I2C_BUS
, parent
, name
));
69 vmstate_register(NULL
, -1, &vmstate_i2c_bus
, bus
);
73 void i2c_set_slave_address(I2CSlave
*dev
, uint8_t address
)
75 dev
->address
= address
;
78 /* Return nonzero if bus is busy. */
79 int i2c_bus_busy(I2CBus
*bus
)
81 return bus
->current_dev
!= NULL
;
84 /* Returns non-zero if the address is not valid. */
85 /* TODO: Make this handle multiple masters. */
86 int i2c_start_transfer(I2CBus
*bus
, uint8_t address
, int recv
)
89 I2CSlave
*slave
= NULL
;
92 QTAILQ_FOREACH(kid
, &bus
->qbus
.children
, sibling
) {
93 DeviceState
*qdev
= kid
->child
;
94 I2CSlave
*candidate
= I2C_SLAVE(qdev
);
95 if (candidate
->address
== address
) {
105 sc
= I2C_SLAVE_GET_CLASS(slave
);
106 /* If the bus is already busy, assume this is a repeated
108 bus
->current_dev
= slave
;
110 sc
->event(slave
, recv
? I2C_START_RECV
: I2C_START_SEND
);
115 void i2c_end_transfer(I2CBus
*bus
)
117 I2CSlave
*dev
= bus
->current_dev
;
124 sc
= I2C_SLAVE_GET_CLASS(dev
);
126 sc
->event(dev
, I2C_FINISH
);
129 bus
->current_dev
= NULL
;
132 int i2c_send(I2CBus
*bus
, uint8_t data
)
134 I2CSlave
*dev
= bus
->current_dev
;
141 sc
= I2C_SLAVE_GET_CLASS(dev
);
143 return sc
->send(dev
, data
);
149 int i2c_recv(I2CBus
*bus
)
151 I2CSlave
*dev
= bus
->current_dev
;
158 sc
= I2C_SLAVE_GET_CLASS(dev
);
160 return sc
->recv(dev
);
166 void i2c_nack(I2CBus
*bus
)
168 I2CSlave
*dev
= bus
->current_dev
;
175 sc
= I2C_SLAVE_GET_CLASS(dev
);
177 sc
->event(dev
, I2C_NACK
);
181 static int i2c_slave_post_load(void *opaque
, int version_id
)
183 I2CSlave
*dev
= opaque
;
185 bus
= I2C_BUS(qdev_get_parent_bus(DEVICE(dev
)));
186 if (bus
->saved_address
== dev
->address
) {
187 bus
->current_dev
= dev
;
192 const VMStateDescription vmstate_i2c_slave
= {
195 .minimum_version_id
= 1,
196 .post_load
= i2c_slave_post_load
,
197 .fields
= (VMStateField
[]) {
198 VMSTATE_UINT8(address
, I2CSlave
),
199 VMSTATE_END_OF_LIST()
203 static int i2c_slave_qdev_init(DeviceState
*dev
)
205 I2CSlave
*s
= I2C_SLAVE(dev
);
206 I2CSlaveClass
*sc
= I2C_SLAVE_GET_CLASS(s
);
211 DeviceState
*i2c_create_slave(I2CBus
*bus
, const char *name
, uint8_t addr
)
215 dev
= qdev_create(&bus
->qbus
, name
);
216 qdev_prop_set_uint8(dev
, "address", addr
);
217 qdev_init_nofail(dev
);
221 static void i2c_slave_class_init(ObjectClass
*klass
, void *data
)
223 DeviceClass
*k
= DEVICE_CLASS(klass
);
224 k
->init
= i2c_slave_qdev_init
;
225 set_bit(DEVICE_CATEGORY_MISC
, k
->categories
);
226 k
->bus_type
= TYPE_I2C_BUS
;
227 k
->props
= i2c_props
;
230 static const TypeInfo i2c_slave_type_info
= {
231 .name
= TYPE_I2C_SLAVE
,
232 .parent
= TYPE_DEVICE
,
233 .instance_size
= sizeof(I2CSlave
),
235 .class_size
= sizeof(I2CSlaveClass
),
236 .class_init
= i2c_slave_class_init
,
239 static void i2c_slave_register_types(void)
241 type_register_static(&i2c_bus_info
);
242 type_register_static(&i2c_slave_type_info
);
245 type_init(i2c_slave_register_types
)