]> git.proxmox.com Git - qemu.git/blame - hw/i2c/core.c
microdrive: Coding Style cleanups
[qemu.git] / hw / i2c / core.c
CommitLineData
5fafdf24 1/*
0ff596d0
PB
2 * QEMU I2C bus interface.
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the LGPL.
0ff596d0
PB
8 */
9
0d09e41a 10#include "hw/i2c/i2c.h"
0ff596d0
PB
11
12struct i2c_bus
13{
02e2da45 14 BusState qbus;
9e07bdf8
AL
15 I2CSlave *current_dev;
16 I2CSlave *dev;
5b7f5327 17 uint8_t saved_address;
0ff596d0
PB
18};
19
3cb75a7c
PB
20static Property i2c_props[] = {
21 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
22 DEFINE_PROP_END_OF_LIST(),
23};
24
0d936928
AL
25#define TYPE_I2C_BUS "i2c-bus"
26#define I2C_BUS(obj) OBJECT_CHECK(i2c_bus, (obj), TYPE_I2C_BUS)
27
28static const TypeInfo i2c_bus_info = {
29 .name = TYPE_I2C_BUS,
30 .parent = TYPE_BUS,
31 .instance_size = sizeof(i2c_bus),
10c4c98a
GH
32};
33
8d0eb050 34static void i2c_bus_pre_save(void *opaque)
c701b35b 35{
8d0eb050 36 i2c_bus *bus = opaque;
c701b35b 37
8d0eb050 38 bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
c701b35b
PB
39}
40
8d0eb050 41static int i2c_bus_post_load(void *opaque, int version_id)
c701b35b 42{
8d0eb050 43 i2c_bus *bus = opaque;
c701b35b
PB
44
45 /* The bus is loaded before attached devices, so load and save the
46 current device id. Devices will check themselves as loaded. */
c701b35b 47 bus->current_dev = NULL;
c701b35b
PB
48 return 0;
49}
50
8d0eb050
JQ
51static 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()
61 }
62};
63
0ff596d0 64/* Create a new I2C bus. */
02e2da45 65i2c_bus *i2c_init_bus(DeviceState *parent, const char *name)
0ff596d0
PB
66{
67 i2c_bus *bus;
68
fef7fbc9 69 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
0be71e32 70 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
0ff596d0
PB
71 return bus;
72}
73
9e07bdf8 74void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
0ff596d0
PB
75{
76 dev->address = address;
77}
78
79/* Return nonzero if bus is busy. */
80int i2c_bus_busy(i2c_bus *bus)
81{
82 return bus->current_dev != NULL;
83}
84
4a2c8ac2 85/* Returns non-zero if the address is not valid. */
0ff596d0 86/* TODO: Make this handle multiple masters. */
5b7f5327 87int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv)
0ff596d0 88{
0866aca1 89 BusChild *kid;
9e07bdf8 90 I2CSlave *slave = NULL;
b5ea9327 91 I2CSlaveClass *sc;
0ff596d0 92
0866aca1
AL
93 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
94 DeviceState *qdev = kid->child;
8aae84a1 95 I2CSlave *candidate = I2C_SLAVE(qdev);
b3a21988
JR
96 if (candidate->address == address) {
97 slave = candidate;
0ff596d0 98 break;
b3a21988 99 }
0ff596d0
PB
100 }
101
b5ea9327 102 if (!slave) {
0ff596d0 103 return 1;
b5ea9327 104 }
0ff596d0 105
b5ea9327 106 sc = I2C_SLAVE_GET_CLASS(slave);
0ff596d0
PB
107 /* If the bus is already busy, assume this is a repeated
108 start condition. */
02e2da45 109 bus->current_dev = slave;
b5ea9327
AL
110 if (sc->event) {
111 sc->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
112 }
0ff596d0
PB
113 return 0;
114}
115
116void i2c_end_transfer(i2c_bus *bus)
117{
9e07bdf8 118 I2CSlave *dev = bus->current_dev;
b5ea9327 119 I2CSlaveClass *sc;
0ff596d0 120
b5ea9327 121 if (!dev) {
0ff596d0 122 return;
b5ea9327 123 }
0ff596d0 124
b5ea9327
AL
125 sc = I2C_SLAVE_GET_CLASS(dev);
126 if (sc->event) {
127 sc->event(dev, I2C_FINISH);
128 }
0ff596d0
PB
129
130 bus->current_dev = NULL;
131}
132
133int i2c_send(i2c_bus *bus, uint8_t data)
134{
9e07bdf8 135 I2CSlave *dev = bus->current_dev;
b5ea9327 136 I2CSlaveClass *sc;
0ff596d0 137
b5ea9327 138 if (!dev) {
0ff596d0 139 return -1;
b5ea9327 140 }
0ff596d0 141
b5ea9327
AL
142 sc = I2C_SLAVE_GET_CLASS(dev);
143 if (sc->send) {
144 return sc->send(dev, data);
145 }
146
147 return -1;
0ff596d0
PB
148}
149
150int i2c_recv(i2c_bus *bus)
151{
9e07bdf8 152 I2CSlave *dev = bus->current_dev;
b5ea9327 153 I2CSlaveClass *sc;
0ff596d0 154
b5ea9327 155 if (!dev) {
0ff596d0 156 return -1;
b5ea9327
AL
157 }
158
159 sc = I2C_SLAVE_GET_CLASS(dev);
160 if (sc->recv) {
161 return sc->recv(dev);
162 }
0ff596d0 163
b5ea9327 164 return -1;
0ff596d0
PB
165}
166
167void i2c_nack(i2c_bus *bus)
168{
9e07bdf8 169 I2CSlave *dev = bus->current_dev;
b5ea9327 170 I2CSlaveClass *sc;
0ff596d0 171
b5ea9327 172 if (!dev) {
0ff596d0 173 return;
b5ea9327 174 }
0ff596d0 175
b5ea9327
AL
176 sc = I2C_SLAVE_GET_CLASS(dev);
177 if (sc->event) {
178 sc->event(dev, I2C_NACK);
179 }
0ff596d0
PB
180}
181
bcbe8068 182static int i2c_slave_post_load(void *opaque, int version_id)
aa941b94 183{
9e07bdf8 184 I2CSlave *dev = opaque;
fe8de492 185 i2c_bus *bus;
fef7fbc9 186 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
fe8de492
PB
187 if (bus->saved_address == dev->address) {
188 bus->current_dev = dev;
189 }
bcbe8068
JQ
190 return 0;
191}
192
1894839f 193const VMStateDescription vmstate_i2c_slave = {
9e07bdf8 194 .name = "I2CSlave",
bcbe8068
JQ
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 []) {
9e07bdf8 200 VMSTATE_UINT8(address, I2CSlave),
bcbe8068
JQ
201 VMSTATE_END_OF_LIST()
202 }
203};
204
d307af79 205static int i2c_slave_qdev_init(DeviceState *dev)
fe8de492 206{
8aae84a1 207 I2CSlave *s = I2C_SLAVE(dev);
b5ea9327 208 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
fe8de492 209
b5ea9327
AL
210 return sc->init(s);
211}
fe8de492 212
5b7f5327 213DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr)
fe8de492
PB
214{
215 DeviceState *dev;
216
02e2da45 217 dev = qdev_create(&bus->qbus, name);
5b7f5327 218 qdev_prop_set_uint8(dev, "address", addr);
e23a1b33 219 qdev_init_nofail(dev);
fe8de492 220 return dev;
aa941b94 221}
b5ea9327 222
39bffca2
AL
223static void i2c_slave_class_init(ObjectClass *klass, void *data)
224{
225 DeviceClass *k = DEVICE_CLASS(klass);
226 k->init = i2c_slave_qdev_init;
125ee0ed 227 set_bit(DEVICE_CATEGORY_MISC, k->categories);
0d936928 228 k->bus_type = TYPE_I2C_BUS;
bce54474 229 k->props = i2c_props;
39bffca2
AL
230}
231
8c43a6f0 232static const TypeInfo i2c_slave_type_info = {
b5ea9327
AL
233 .name = TYPE_I2C_SLAVE,
234 .parent = TYPE_DEVICE,
235 .instance_size = sizeof(I2CSlave),
236 .abstract = true,
237 .class_size = sizeof(I2CSlaveClass),
39bffca2 238 .class_init = i2c_slave_class_init,
b5ea9327
AL
239};
240
83f7d43a 241static void i2c_slave_register_types(void)
b5ea9327 242{
0d936928 243 type_register_static(&i2c_bus_info);
b5ea9327
AL
244 type_register_static(&i2c_slave_type_info);
245}
246
83f7d43a 247type_init(i2c_slave_register_types)