target/s390x: move get_per_in_range() to misc_helper.c
[qemu/ar7.git] / hw / i2c / core.c
blob2c1234cdffacd2a1a32a766af13c0f5ca24d8e50
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
24 BusState qbus;
25 QLIST_HEAD(, I2CNode) current_devs;
26 uint8_t saved_address;
27 bool broadcast;
30 static Property i2c_props[] = {
31 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
32 DEFINE_PROP_END_OF_LIST(),
35 #define TYPE_I2C_BUS "i2c-bus"
36 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
38 static const TypeInfo i2c_bus_info = {
39 .name = TYPE_I2C_BUS,
40 .parent = TYPE_BUS,
41 .instance_size = sizeof(I2CBus),
44 static void i2c_bus_pre_save(void *opaque)
46 I2CBus *bus = opaque;
48 bus->saved_address = -1;
49 if (!QLIST_EMPTY(&bus->current_devs)) {
50 if (!bus->broadcast) {
51 bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
52 } else {
53 bus->saved_address = I2C_BROADCAST;
58 static const VMStateDescription vmstate_i2c_bus = {
59 .name = "i2c_bus",
60 .version_id = 1,
61 .minimum_version_id = 1,
62 .pre_save = i2c_bus_pre_save,
63 .fields = (VMStateField[]) {
64 VMSTATE_UINT8(saved_address, I2CBus),
65 VMSTATE_END_OF_LIST()
69 /* Create a new I2C bus. */
70 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
72 I2CBus *bus;
74 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
75 QLIST_INIT(&bus->current_devs);
76 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
77 return bus;
80 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
82 dev->address = address;
85 /* Return nonzero if bus is busy. */
86 int i2c_bus_busy(I2CBus *bus)
88 return !QLIST_EMPTY(&bus->current_devs);
91 /* TODO: Make this handle multiple masters. */
93 * Start or continue an i2c transaction. When this is called for the
94 * first time or after an i2c_end_transfer(), if it returns an error
95 * the bus transaction is terminated (or really never started). If
96 * this is called after another i2c_start_transfer() without an
97 * intervening i2c_end_transfer(), and it returns an error, the
98 * transaction will not be terminated. The caller must do it.
100 * This corresponds with the way real hardware works. The SMBus
101 * protocol uses a start transfer to switch from write to read mode
102 * without releasing the bus. If that fails, the bus is still
103 * in a transaction.
105 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
107 BusChild *kid;
108 I2CSlaveClass *sc;
109 I2CNode *node;
110 bool bus_scanned = false;
112 if (address == I2C_BROADCAST) {
114 * This is a broadcast, the current_devs will be all the devices of the
115 * bus.
117 bus->broadcast = true;
121 * If there are already devices in the list, that means we are in
122 * the middle of a transaction and we shouldn't rescan the bus.
124 * This happens with any SMBus transaction, even on a pure I2C
125 * device. The interface does a transaction start without
126 * terminating the previous transaction.
128 if (QLIST_EMPTY(&bus->current_devs)) {
129 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
130 DeviceState *qdev = kid->child;
131 I2CSlave *candidate = I2C_SLAVE(qdev);
132 if ((candidate->address == address) || (bus->broadcast)) {
133 node = g_malloc(sizeof(struct I2CNode));
134 node->elt = candidate;
135 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
136 if (!bus->broadcast) {
137 break;
141 bus_scanned = true;
144 if (QLIST_EMPTY(&bus->current_devs)) {
145 return 1;
148 QLIST_FOREACH(node, &bus->current_devs, next) {
149 int rv;
151 sc = I2C_SLAVE_GET_CLASS(node->elt);
152 /* If the bus is already busy, assume this is a repeated
153 start condition. */
155 if (sc->event) {
156 rv = sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
157 if (rv && !bus->broadcast) {
158 if (bus_scanned) {
159 /* First call, terminate the transfer. */
160 i2c_end_transfer(bus);
162 return rv;
166 return 0;
169 void i2c_end_transfer(I2CBus *bus)
171 I2CSlaveClass *sc;
172 I2CNode *node, *next;
174 QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
175 sc = I2C_SLAVE_GET_CLASS(node->elt);
176 if (sc->event) {
177 sc->event(node->elt, I2C_FINISH);
179 QLIST_REMOVE(node, next);
180 g_free(node);
182 bus->broadcast = false;
185 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
187 I2CSlaveClass *sc;
188 I2CNode *node;
189 int ret = 0;
191 if (send) {
192 QLIST_FOREACH(node, &bus->current_devs, next) {
193 sc = I2C_SLAVE_GET_CLASS(node->elt);
194 if (sc->send) {
195 ret = ret || sc->send(node->elt, *data);
196 } else {
197 ret = -1;
200 return ret ? -1 : 0;
201 } else {
202 if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
203 return -1;
206 sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
207 if (sc->recv) {
208 ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
209 if (ret < 0) {
210 return ret;
211 } else {
212 *data = ret;
213 return 0;
216 return -1;
220 int i2c_send(I2CBus *bus, uint8_t data)
222 return i2c_send_recv(bus, &data, true);
225 int i2c_recv(I2CBus *bus)
227 uint8_t data;
228 int ret = i2c_send_recv(bus, &data, false);
230 return ret < 0 ? ret : data;
233 void i2c_nack(I2CBus *bus)
235 I2CSlaveClass *sc;
236 I2CNode *node;
238 if (QLIST_EMPTY(&bus->current_devs)) {
239 return;
242 QLIST_FOREACH(node, &bus->current_devs, next) {
243 sc = I2C_SLAVE_GET_CLASS(node->elt);
244 if (sc->event) {
245 sc->event(node->elt, I2C_NACK);
250 static int i2c_slave_post_load(void *opaque, int version_id)
252 I2CSlave *dev = opaque;
253 I2CBus *bus;
254 I2CNode *node;
256 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
257 if ((bus->saved_address == dev->address) ||
258 (bus->saved_address == I2C_BROADCAST)) {
259 node = g_malloc(sizeof(struct I2CNode));
260 node->elt = dev;
261 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
263 return 0;
266 const VMStateDescription vmstate_i2c_slave = {
267 .name = "I2CSlave",
268 .version_id = 1,
269 .minimum_version_id = 1,
270 .post_load = i2c_slave_post_load,
271 .fields = (VMStateField[]) {
272 VMSTATE_UINT8(address, I2CSlave),
273 VMSTATE_END_OF_LIST()
277 static int i2c_slave_qdev_init(DeviceState *dev)
279 I2CSlave *s = I2C_SLAVE(dev);
280 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
282 if (sc->init) {
283 return sc->init(s);
286 return 0;
289 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
291 DeviceState *dev;
293 dev = qdev_create(&bus->qbus, name);
294 qdev_prop_set_uint8(dev, "address", addr);
295 qdev_init_nofail(dev);
296 return dev;
299 static void i2c_slave_class_init(ObjectClass *klass, void *data)
301 DeviceClass *k = DEVICE_CLASS(klass);
302 k->init = i2c_slave_qdev_init;
303 set_bit(DEVICE_CATEGORY_MISC, k->categories);
304 k->bus_type = TYPE_I2C_BUS;
305 k->props = i2c_props;
308 static const TypeInfo i2c_slave_type_info = {
309 .name = TYPE_I2C_SLAVE,
310 .parent = TYPE_DEVICE,
311 .instance_size = sizeof(I2CSlave),
312 .abstract = true,
313 .class_size = sizeof(I2CSlaveClass),
314 .class_init = i2c_slave_class_init,
317 static void i2c_slave_register_types(void)
319 type_register_static(&i2c_bus_info);
320 type_register_static(&i2c_slave_type_info);
323 type_init(i2c_slave_register_types)