Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging
[qemu/ar7.git] / hw / i2c / core.c
blob5a64026347ab73e60628001a0cf0dfa54db13112
1 /*
2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
8 */
10 #include "hw/i2c/i2c.h"
12 struct I2CBus
14 BusState qbus;
15 I2CSlave *current_dev;
16 I2CSlave *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 = {
29 .name = TYPE_I2C_BUS,
30 .parent = TYPE_BUS,
31 .instance_size = sizeof(I2CBus),
34 static void i2c_bus_pre_save(void *opaque)
36 I2CBus *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 I2CBus *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;
48 return 0;
51 static const VMStateDescription vmstate_i2c_bus = {
52 .name = "i2c_bus",
53 .version_id = 1,
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),
59 VMSTATE_END_OF_LIST()
63 /* Create a new I2C bus. */
64 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
66 I2CBus *bus;
68 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
69 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
70 return 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)
88 BusChild *kid;
89 I2CSlave *slave = NULL;
90 I2CSlaveClass *sc;
92 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
93 DeviceState *qdev = kid->child;
94 I2CSlave *candidate = I2C_SLAVE(qdev);
95 if (candidate->address == address) {
96 slave = candidate;
97 break;
101 if (!slave) {
102 return 1;
105 sc = I2C_SLAVE_GET_CLASS(slave);
106 /* If the bus is already busy, assume this is a repeated
107 start condition. */
108 bus->current_dev = slave;
109 if (sc->event) {
110 sc->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
112 return 0;
115 void i2c_end_transfer(I2CBus *bus)
117 I2CSlave *dev = bus->current_dev;
118 I2CSlaveClass *sc;
120 if (!dev) {
121 return;
124 sc = I2C_SLAVE_GET_CLASS(dev);
125 if (sc->event) {
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;
135 I2CSlaveClass *sc;
137 if (!dev) {
138 return -1;
141 sc = I2C_SLAVE_GET_CLASS(dev);
142 if (sc->send) {
143 return sc->send(dev, data);
146 return -1;
149 int i2c_recv(I2CBus *bus)
151 I2CSlave *dev = bus->current_dev;
152 I2CSlaveClass *sc;
154 if (!dev) {
155 return -1;
158 sc = I2C_SLAVE_GET_CLASS(dev);
159 if (sc->recv) {
160 return sc->recv(dev);
163 return -1;
166 void i2c_nack(I2CBus *bus)
168 I2CSlave *dev = bus->current_dev;
169 I2CSlaveClass *sc;
171 if (!dev) {
172 return;
175 sc = I2C_SLAVE_GET_CLASS(dev);
176 if (sc->event) {
177 sc->event(dev, I2C_NACK);
181 static int i2c_slave_post_load(void *opaque, int version_id)
183 I2CSlave *dev = opaque;
184 I2CBus *bus;
185 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
186 if (bus->saved_address == dev->address) {
187 bus->current_dev = dev;
189 return 0;
192 const VMStateDescription vmstate_i2c_slave = {
193 .name = "I2CSlave",
194 .version_id = 1,
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);
208 return sc->init(s);
211 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
213 DeviceState *dev;
215 dev = qdev_create(&bus->qbus, name);
216 qdev_prop_set_uint8(dev, "address", addr);
217 qdev_init_nofail(dev);
218 return 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),
234 .abstract = true,
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)