i2c: Fix some brace style issues
[qemu/kevin.git] / hw / i2c / core.c
blob9a54b61c1dbd32dd025d2a3ddbeb2ae3df4709f8
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 "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
13 typedef struct I2CNode I2CNode;
15 struct I2CNode {
16 I2CSlave *elt;
17 QLIST_ENTRY(I2CNode) next;
20 #define I2C_BROADCAST 0x00
22 struct I2CBus {
23 BusState qbus;
24 QLIST_HEAD(, I2CNode) current_devs;
25 uint8_t saved_address;
26 bool broadcast;
29 static Property i2c_props[] = {
30 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
31 DEFINE_PROP_END_OF_LIST(),
34 #define TYPE_I2C_BUS "i2c-bus"
35 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
37 static const TypeInfo i2c_bus_info = {
38 .name = TYPE_I2C_BUS,
39 .parent = TYPE_BUS,
40 .instance_size = sizeof(I2CBus),
43 static int i2c_bus_pre_save(void *opaque)
45 I2CBus *bus = opaque;
47 bus->saved_address = -1;
48 if (!QLIST_EMPTY(&bus->current_devs)) {
49 if (!bus->broadcast) {
50 bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
51 } else {
52 bus->saved_address = I2C_BROADCAST;
56 return 0;
59 static const VMStateDescription vmstate_i2c_bus = {
60 .name = "i2c_bus",
61 .version_id = 1,
62 .minimum_version_id = 1,
63 .pre_save = i2c_bus_pre_save,
64 .fields = (VMStateField[]) {
65 VMSTATE_UINT8(saved_address, I2CBus),
66 VMSTATE_END_OF_LIST()
70 /* Create a new I2C bus. */
71 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
73 I2CBus *bus;
75 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
76 QLIST_INIT(&bus->current_devs);
77 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
78 return bus;
81 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
83 dev->address = address;
86 /* Return nonzero if bus is busy. */
87 int i2c_bus_busy(I2CBus *bus)
89 return !QLIST_EMPTY(&bus->current_devs);
92 /* TODO: Make this handle multiple masters. */
94 * Start or continue an i2c transaction. When this is called for the
95 * first time or after an i2c_end_transfer(), if it returns an error
96 * the bus transaction is terminated (or really never started). If
97 * this is called after another i2c_start_transfer() without an
98 * intervening i2c_end_transfer(), and it returns an error, the
99 * transaction will not be terminated. The caller must do it.
101 * This corresponds with the way real hardware works. The SMBus
102 * protocol uses a start transfer to switch from write to read mode
103 * without releasing the bus. If that fails, the bus is still
104 * in a transaction.
106 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
108 BusChild *kid;
109 I2CSlaveClass *sc;
110 I2CNode *node;
111 bool bus_scanned = false;
113 if (address == I2C_BROADCAST) {
115 * This is a broadcast, the current_devs will be all the devices of the
116 * bus.
118 bus->broadcast = true;
122 * If there are already devices in the list, that means we are in
123 * the middle of a transaction and we shouldn't rescan the bus.
125 * This happens with any SMBus transaction, even on a pure I2C
126 * device. The interface does a transaction start without
127 * terminating the previous transaction.
129 if (QLIST_EMPTY(&bus->current_devs)) {
130 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
131 DeviceState *qdev = kid->child;
132 I2CSlave *candidate = I2C_SLAVE(qdev);
133 if ((candidate->address == address) || (bus->broadcast)) {
134 node = g_malloc(sizeof(struct I2CNode));
135 node->elt = candidate;
136 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
137 if (!bus->broadcast) {
138 break;
142 bus_scanned = true;
145 if (QLIST_EMPTY(&bus->current_devs)) {
146 return 1;
149 QLIST_FOREACH(node, &bus->current_devs, next) {
150 int rv;
152 sc = I2C_SLAVE_GET_CLASS(node->elt);
153 /* If the bus is already busy, assume this is a repeated
154 start condition. */
156 if (sc->event) {
157 rv = sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
158 if (rv && !bus->broadcast) {
159 if (bus_scanned) {
160 /* First call, terminate the transfer. */
161 i2c_end_transfer(bus);
163 return rv;
167 return 0;
170 void i2c_end_transfer(I2CBus *bus)
172 I2CSlaveClass *sc;
173 I2CNode *node, *next;
175 QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
176 sc = I2C_SLAVE_GET_CLASS(node->elt);
177 if (sc->event) {
178 sc->event(node->elt, I2C_FINISH);
180 QLIST_REMOVE(node, next);
181 g_free(node);
183 bus->broadcast = false;
186 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
188 I2CSlaveClass *sc;
189 I2CNode *node;
190 int ret = 0;
192 if (send) {
193 QLIST_FOREACH(node, &bus->current_devs, next) {
194 sc = I2C_SLAVE_GET_CLASS(node->elt);
195 if (sc->send) {
196 ret = ret || sc->send(node->elt, *data);
197 } else {
198 ret = -1;
201 return ret ? -1 : 0;
202 } else {
203 if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
204 return -1;
207 sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
208 if (sc->recv) {
209 ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
210 if (ret < 0) {
211 return ret;
212 } else {
213 *data = ret;
214 return 0;
217 return -1;
221 int i2c_send(I2CBus *bus, uint8_t data)
223 return i2c_send_recv(bus, &data, true);
226 int i2c_recv(I2CBus *bus)
228 uint8_t data;
229 int ret = i2c_send_recv(bus, &data, false);
231 return ret < 0 ? ret : data;
234 void i2c_nack(I2CBus *bus)
236 I2CSlaveClass *sc;
237 I2CNode *node;
239 if (QLIST_EMPTY(&bus->current_devs)) {
240 return;
243 QLIST_FOREACH(node, &bus->current_devs, next) {
244 sc = I2C_SLAVE_GET_CLASS(node->elt);
245 if (sc->event) {
246 sc->event(node->elt, I2C_NACK);
251 static int i2c_slave_post_load(void *opaque, int version_id)
253 I2CSlave *dev = opaque;
254 I2CBus *bus;
255 I2CNode *node;
257 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
258 if ((bus->saved_address == dev->address) ||
259 (bus->saved_address == I2C_BROADCAST)) {
260 node = g_malloc(sizeof(struct I2CNode));
261 node->elt = dev;
262 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
264 return 0;
267 const VMStateDescription vmstate_i2c_slave = {
268 .name = "I2CSlave",
269 .version_id = 1,
270 .minimum_version_id = 1,
271 .post_load = i2c_slave_post_load,
272 .fields = (VMStateField[]) {
273 VMSTATE_UINT8(address, I2CSlave),
274 VMSTATE_END_OF_LIST()
278 static int i2c_slave_qdev_init(DeviceState *dev)
280 I2CSlave *s = I2C_SLAVE(dev);
281 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
283 if (sc->init) {
284 return sc->init(s);
287 return 0;
290 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
292 DeviceState *dev;
294 dev = qdev_create(&bus->qbus, name);
295 qdev_prop_set_uint8(dev, "address", addr);
296 qdev_init_nofail(dev);
297 return dev;
300 static void i2c_slave_class_init(ObjectClass *klass, void *data)
302 DeviceClass *k = DEVICE_CLASS(klass);
303 k->init = i2c_slave_qdev_init;
304 set_bit(DEVICE_CATEGORY_MISC, k->categories);
305 k->bus_type = TYPE_I2C_BUS;
306 k->props = i2c_props;
309 static const TypeInfo i2c_slave_type_info = {
310 .name = TYPE_I2C_SLAVE,
311 .parent = TYPE_DEVICE,
312 .instance_size = sizeof(I2CSlave),
313 .abstract = true,
314 .class_size = sizeof(I2CSlaveClass),
315 .class_init = i2c_slave_class_init,
318 static void i2c_slave_register_types(void)
320 type_register_static(&i2c_bus_info);
321 type_register_static(&i2c_slave_type_info);
324 type_init(i2c_slave_register_types)