Statistics
| Branch: | Revision:

root / hw / i2c.c @ d8c6d07f

History | View | Annotate | Download (5.2 kB)

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