xref: /qemu/hw/i2c/core.c (revision 814bb12a)
1 /*
2  * QEMU I2C bus interface.
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the LGPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 
13 typedef struct I2CNode I2CNode;
14 
15 struct I2CNode {
16     I2CSlave *elt;
17     QLIST_ENTRY(I2CNode) next;
18 };
19 
20 #define I2C_BROADCAST 0x00
21 
22 struct I2CBus
23 {
24     BusState qbus;
25     QLIST_HEAD(, I2CNode) current_devs;
26     uint8_t saved_address;
27     bool broadcast;
28 };
29 
30 static Property i2c_props[] = {
31     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
32     DEFINE_PROP_END_OF_LIST(),
33 };
34 
35 #define TYPE_I2C_BUS "i2c-bus"
36 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
37 
38 static const TypeInfo i2c_bus_info = {
39     .name = TYPE_I2C_BUS,
40     .parent = TYPE_BUS,
41     .instance_size = sizeof(I2CBus),
42 };
43 
44 static void i2c_bus_pre_save(void *opaque)
45 {
46     I2CBus *bus = opaque;
47 
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;
54         }
55     }
56 }
57 
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()
66     }
67 };
68 
69 /* Create a new I2C bus.  */
70 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
71 {
72     I2CBus *bus;
73 
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;
78 }
79 
80 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
81 {
82     dev->address = address;
83 }
84 
85 /* Return nonzero if bus is busy.  */
86 int i2c_bus_busy(I2CBus *bus)
87 {
88     return !QLIST_EMPTY(&bus->current_devs);
89 }
90 
91 /*
92  * Returns non-zero if the address is not valid.  If this is called
93  * again without an intervening i2c_end_transfer(), like in the SMBus
94  * case where the operation is switched from write to read, this
95  * function will not rescan the bus and thus cannot fail.
96  */
97 /* TODO: Make this handle multiple masters.  */
98 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
99 {
100     BusChild *kid;
101     I2CSlaveClass *sc;
102     I2CNode *node;
103 
104     if (address == I2C_BROADCAST) {
105         /*
106          * This is a broadcast, the current_devs will be all the devices of the
107          * bus.
108          */
109         bus->broadcast = true;
110     }
111 
112     /*
113      * If there are already devices in the list, that means we are in
114      * the middle of a transaction and we shouldn't rescan the bus.
115      *
116      * This happens with any SMBus transaction, even on a pure I2C
117      * device.  The interface does a transaction start without
118      * terminating the previous transaction.
119      */
120     if (QLIST_EMPTY(&bus->current_devs)) {
121         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
122             DeviceState *qdev = kid->child;
123             I2CSlave *candidate = I2C_SLAVE(qdev);
124             if ((candidate->address == address) || (bus->broadcast)) {
125                 node = g_malloc(sizeof(struct I2CNode));
126                 node->elt = candidate;
127                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
128                 if (!bus->broadcast) {
129                     break;
130                 }
131             }
132         }
133     }
134 
135     if (QLIST_EMPTY(&bus->current_devs)) {
136         return 1;
137     }
138 
139     QLIST_FOREACH(node, &bus->current_devs, next) {
140         sc = I2C_SLAVE_GET_CLASS(node->elt);
141         /* If the bus is already busy, assume this is a repeated
142            start condition.  */
143         if (sc->event) {
144             sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
145         }
146     }
147     return 0;
148 }
149 
150 void i2c_end_transfer(I2CBus *bus)
151 {
152     I2CSlaveClass *sc;
153     I2CNode *node, *next;
154 
155     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
156         sc = I2C_SLAVE_GET_CLASS(node->elt);
157         if (sc->event) {
158             sc->event(node->elt, I2C_FINISH);
159         }
160         QLIST_REMOVE(node, next);
161         g_free(node);
162     }
163     bus->broadcast = false;
164 }
165 
166 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
167 {
168     I2CSlaveClass *sc;
169     I2CNode *node;
170     int ret = 0;
171 
172     if (send) {
173         QLIST_FOREACH(node, &bus->current_devs, next) {
174             sc = I2C_SLAVE_GET_CLASS(node->elt);
175             if (sc->send) {
176                 ret = ret || sc->send(node->elt, *data);
177             } else {
178                 ret = -1;
179             }
180         }
181         return ret ? -1 : 0;
182     } else {
183         if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
184             return -1;
185         }
186 
187         sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
188         if (sc->recv) {
189             ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
190             if (ret < 0) {
191                 return ret;
192             } else {
193                 *data = ret;
194                 return 0;
195             }
196         }
197         return -1;
198     }
199 }
200 
201 int i2c_send(I2CBus *bus, uint8_t data)
202 {
203     return i2c_send_recv(bus, &data, true);
204 }
205 
206 int i2c_recv(I2CBus *bus)
207 {
208     uint8_t data;
209     int ret = i2c_send_recv(bus, &data, false);
210 
211     return ret < 0 ? ret : data;
212 }
213 
214 void i2c_nack(I2CBus *bus)
215 {
216     I2CSlaveClass *sc;
217     I2CNode *node;
218 
219     if (QLIST_EMPTY(&bus->current_devs)) {
220         return;
221     }
222 
223     QLIST_FOREACH(node, &bus->current_devs, next) {
224         sc = I2C_SLAVE_GET_CLASS(node->elt);
225         if (sc->event) {
226             sc->event(node->elt, I2C_NACK);
227         }
228     }
229 }
230 
231 static int i2c_slave_post_load(void *opaque, int version_id)
232 {
233     I2CSlave *dev = opaque;
234     I2CBus *bus;
235     I2CNode *node;
236 
237     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
238     if ((bus->saved_address == dev->address) ||
239         (bus->saved_address == I2C_BROADCAST)) {
240         node = g_malloc(sizeof(struct I2CNode));
241         node->elt = dev;
242         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
243     }
244     return 0;
245 }
246 
247 const VMStateDescription vmstate_i2c_slave = {
248     .name = "I2CSlave",
249     .version_id = 1,
250     .minimum_version_id = 1,
251     .post_load = i2c_slave_post_load,
252     .fields = (VMStateField[]) {
253         VMSTATE_UINT8(address, I2CSlave),
254         VMSTATE_END_OF_LIST()
255     }
256 };
257 
258 static int i2c_slave_qdev_init(DeviceState *dev)
259 {
260     I2CSlave *s = I2C_SLAVE(dev);
261     I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
262 
263     return sc->init(s);
264 }
265 
266 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
267 {
268     DeviceState *dev;
269 
270     dev = qdev_create(&bus->qbus, name);
271     qdev_prop_set_uint8(dev, "address", addr);
272     qdev_init_nofail(dev);
273     return dev;
274 }
275 
276 static void i2c_slave_class_init(ObjectClass *klass, void *data)
277 {
278     DeviceClass *k = DEVICE_CLASS(klass);
279     k->init = i2c_slave_qdev_init;
280     set_bit(DEVICE_CATEGORY_MISC, k->categories);
281     k->bus_type = TYPE_I2C_BUS;
282     k->props = i2c_props;
283 }
284 
285 static const TypeInfo i2c_slave_type_info = {
286     .name = TYPE_I2C_SLAVE,
287     .parent = TYPE_DEVICE,
288     .instance_size = sizeof(I2CSlave),
289     .abstract = true,
290     .class_size = sizeof(I2CSlaveClass),
291     .class_init = i2c_slave_class_init,
292 };
293 
294 static void i2c_slave_register_types(void)
295 {
296     type_register_static(&i2c_bus_info);
297     type_register_static(&i2c_slave_type_info);
298 }
299 
300 type_init(i2c_slave_register_types)
301