i2cbus: remove unused dev field
[qemu.git] / hw / i2c / core.c
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 struct I2CBus
14 {
15 BusState qbus;
16 I2CSlave *current_dev;
17 uint8_t saved_address;
18 };
19
20 static Property i2c_props[] = {
21 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
22 DEFINE_PROP_END_OF_LIST(),
23 };
24
25 #define TYPE_I2C_BUS "i2c-bus"
26 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
27
28 static const TypeInfo i2c_bus_info = {
29 .name = TYPE_I2C_BUS,
30 .parent = TYPE_BUS,
31 .instance_size = sizeof(I2CBus),
32 };
33
34 static void i2c_bus_pre_save(void *opaque)
35 {
36 I2CBus *bus = opaque;
37
38 bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
39 }
40
41 static int i2c_bus_post_load(void *opaque, int version_id)
42 {
43 I2CBus *bus = opaque;
44
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;
49 }
50
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()
60 }
61 };
62
63 /* Create a new I2C bus. */
64 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
65 {
66 I2CBus *bus;
67
68 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
69 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
70 return bus;
71 }
72
73 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
74 {
75 dev->address = address;
76 }
77
78 /* Return nonzero if bus is busy. */
79 int i2c_bus_busy(I2CBus *bus)
80 {
81 return bus->current_dev != NULL;
82 }
83
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)
87 {
88 BusChild *kid;
89 I2CSlave *slave = NULL;
90 I2CSlaveClass *sc;
91
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;
98 }
99 }
100
101 if (!slave) {
102 return 1;
103 }
104
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);
111 }
112 return 0;
113 }
114
115 void i2c_end_transfer(I2CBus *bus)
116 {
117 I2CSlave *dev = bus->current_dev;
118 I2CSlaveClass *sc;
119
120 if (!dev) {
121 return;
122 }
123
124 sc = I2C_SLAVE_GET_CLASS(dev);
125 if (sc->event) {
126 sc->event(dev, I2C_FINISH);
127 }
128
129 bus->current_dev = NULL;
130 }
131
132 int i2c_send(I2CBus *bus, uint8_t data)
133 {
134 I2CSlave *dev = bus->current_dev;
135 I2CSlaveClass *sc;
136
137 if (!dev) {
138 return -1;
139 }
140
141 sc = I2C_SLAVE_GET_CLASS(dev);
142 if (sc->send) {
143 return sc->send(dev, data);
144 }
145
146 return -1;
147 }
148
149 int i2c_recv(I2CBus *bus)
150 {
151 I2CSlave *dev = bus->current_dev;
152 I2CSlaveClass *sc;
153
154 if (!dev) {
155 return -1;
156 }
157
158 sc = I2C_SLAVE_GET_CLASS(dev);
159 if (sc->recv) {
160 return sc->recv(dev);
161 }
162
163 return -1;
164 }
165
166 void i2c_nack(I2CBus *bus)
167 {
168 I2CSlave *dev = bus->current_dev;
169 I2CSlaveClass *sc;
170
171 if (!dev) {
172 return;
173 }
174
175 sc = I2C_SLAVE_GET_CLASS(dev);
176 if (sc->event) {
177 sc->event(dev, I2C_NACK);
178 }
179 }
180
181 static int i2c_slave_post_load(void *opaque, int version_id)
182 {
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;
188 }
189 return 0;
190 }
191
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()
200 }
201 };
202
203 static int i2c_slave_qdev_init(DeviceState *dev)
204 {
205 I2CSlave *s = I2C_SLAVE(dev);
206 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
207
208 return sc->init(s);
209 }
210
211 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
212 {
213 DeviceState *dev;
214
215 dev = qdev_create(&bus->qbus, name);
216 qdev_prop_set_uint8(dev, "address", addr);
217 qdev_init_nofail(dev);
218 return dev;
219 }
220
221 static void i2c_slave_class_init(ObjectClass *klass, void *data)
222 {
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;
228 }
229
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,
237 };
238
239 static void i2c_slave_register_types(void)
240 {
241 type_register_static(&i2c_bus_info);
242 type_register_static(&i2c_slave_type_info);
243 }
244
245 type_init(i2c_slave_register_types)