Merge remote-tracking branch 'mjt/mjt-iov2' into staging
[qemu-kvm.git] / hw / i2c.c
blob296bece119b3217de03b40e18cfdda812e3464d2
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 "i2c.h"
12 struct i2c_bus
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(i2c_bus, (obj), TYPE_I2C_BUS)
28 static const TypeInfo i2c_bus_info = {
29 .name = TYPE_I2C_BUS,
30 .parent = TYPE_BUS,
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;
48 return 0;
51 static const VMStateDescription vmstate_i2c_bus = {
52 .name = "i2c_bus",
53 .version_id = 1,
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),
60 VMSTATE_END_OF_LIST()
64 /* Create a new I2C bus. */
65 i2c_bus *i2c_init_bus(DeviceState *parent, const char *name)
67 i2c_bus *bus;
69 bus = FROM_QBUS(i2c_bus, qbus_create(TYPE_I2C_BUS, parent, name));
70 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
71 return 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)
89 BusChild *kid;
90 I2CSlave *slave = NULL;
91 I2CSlaveClass *sc;
93 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
94 DeviceState *qdev = kid->child;
95 I2CSlave *candidate = I2C_SLAVE_FROM_QDEV(qdev);
96 if (candidate->address == address) {
97 slave = candidate;
98 break;
102 if (!slave) {
103 return 1;
106 sc = I2C_SLAVE_GET_CLASS(slave);
107 /* If the bus is already busy, assume this is a repeated
108 start condition. */
109 bus->current_dev = slave;
110 if (sc->event) {
111 sc->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
113 return 0;
116 void i2c_end_transfer(i2c_bus *bus)
118 I2CSlave *dev = bus->current_dev;
119 I2CSlaveClass *sc;
121 if (!dev) {
122 return;
125 sc = I2C_SLAVE_GET_CLASS(dev);
126 if (sc->event) {
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;
136 I2CSlaveClass *sc;
138 if (!dev) {
139 return -1;
142 sc = I2C_SLAVE_GET_CLASS(dev);
143 if (sc->send) {
144 return sc->send(dev, data);
147 return -1;
150 int i2c_recv(i2c_bus *bus)
152 I2CSlave *dev = bus->current_dev;
153 I2CSlaveClass *sc;
155 if (!dev) {
156 return -1;
159 sc = I2C_SLAVE_GET_CLASS(dev);
160 if (sc->recv) {
161 return sc->recv(dev);
164 return -1;
167 void i2c_nack(i2c_bus *bus)
169 I2CSlave *dev = bus->current_dev;
170 I2CSlaveClass *sc;
172 if (!dev) {
173 return;
176 sc = I2C_SLAVE_GET_CLASS(dev);
177 if (sc->event) {
178 sc->event(dev, I2C_NACK);
182 static int i2c_slave_post_load(void *opaque, int version_id)
184 I2CSlave *dev = opaque;
185 i2c_bus *bus;
186 bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev));
187 if (bus->saved_address == dev->address) {
188 bus->current_dev = dev;
190 return 0;
193 const VMStateDescription vmstate_i2c_slave = {
194 .name = "I2CSlave",
195 .version_id = 1,
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_FROM_QDEV(dev);
208 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
210 return sc->init(s);
213 DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr)
215 DeviceState *dev;
217 dev = qdev_create(&bus->qbus, name);
218 qdev_prop_set_uint8(dev, "address", addr);
219 qdev_init_nofail(dev);
220 return 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 TypeInfo i2c_slave_type_info = {
232 .name = TYPE_I2C_SLAVE,
233 .parent = TYPE_DEVICE,
234 .instance_size = sizeof(I2CSlave),
235 .abstract = true,
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)