blob: 296bece119b3217de03b40e18cfdda812e3464d2 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook0ff596d2007-05-23 00:03:59 +00002 * QEMU I2C bus interface.
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the LGPL.
pbrook0ff596d2007-05-23 00:03:59 +00008 */
9
pbrook87ecb682007-11-17 17:14:51 +000010#include "i2c.h"
pbrook0ff596d2007-05-23 00:03:59 +000011
12struct i2c_bus
13{
Paul Brook02e2da42009-05-23 00:05:19 +010014 BusState qbus;
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060015 I2CSlave *current_dev;
16 I2CSlave *dev;
Juan Quintela5b7f5322009-09-29 22:48:26 +020017 uint8_t saved_address;
pbrook0ff596d2007-05-23 00:03:59 +000018};
19
Paolo Bonzini3cb75a72012-03-28 18:01:36 +020020static Property i2c_props[] = {
21 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
22 DEFINE_PROP_END_OF_LIST(),
23};
24
Anthony Liguori0d936922012-05-02 09:00:20 +020025#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),
Gerd Hoffmann10c4c982009-06-30 14:12:08 +020032};
33
Juan Quintela8d0eb052009-09-29 22:48:27 +020034static void i2c_bus_pre_save(void *opaque)
pbrookc701b352008-07-01 23:16:53 +000035{
Juan Quintela8d0eb052009-09-29 22:48:27 +020036 i2c_bus *bus = opaque;
pbrookc701b352008-07-01 23:16:53 +000037
Juan Quintela8d0eb052009-09-29 22:48:27 +020038 bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
pbrookc701b352008-07-01 23:16:53 +000039}
40
Juan Quintela8d0eb052009-09-29 22:48:27 +020041static int i2c_bus_post_load(void *opaque, int version_id)
pbrookc701b352008-07-01 23:16:53 +000042{
Juan Quintela8d0eb052009-09-29 22:48:27 +020043 i2c_bus *bus = opaque;
pbrookc701b352008-07-01 23:16:53 +000044
45 /* The bus is loaded before attached devices, so load and save the
46 current device id. Devices will check themselves as loaded. */
pbrookc701b352008-07-01 23:16:53 +000047 bus->current_dev = NULL;
pbrookc701b352008-07-01 23:16:53 +000048 return 0;
49}
50
Juan Quintela8d0eb052009-09-29 22:48:27 +020051static 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
pbrook0ff596d2007-05-23 00:03:59 +000064/* Create a new I2C bus. */
Paul Brook02e2da42009-05-23 00:05:19 +010065i2c_bus *i2c_init_bus(DeviceState *parent, const char *name)
pbrook0ff596d2007-05-23 00:03:59 +000066{
67 i2c_bus *bus;
68
Anthony Liguori0d936922012-05-02 09:00:20 +020069 bus = FROM_QBUS(i2c_bus, qbus_create(TYPE_I2C_BUS, parent, name));
Alex Williamson0be71e32010-06-25 11:09:07 -060070 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
pbrook0ff596d2007-05-23 00:03:59 +000071 return bus;
72}
73
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060074void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
pbrook0ff596d2007-05-23 00:03:59 +000075{
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
balrog4a2c8ac2007-11-03 00:51:03 +000085/* Returns non-zero if the address is not valid. */
pbrook0ff596d2007-05-23 00:03:59 +000086/* TODO: Make this handle multiple masters. */
Juan Quintela5b7f5322009-09-29 22:48:26 +020087int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv)
pbrook0ff596d2007-05-23 00:03:59 +000088{
Anthony Liguori0866aca2011-12-23 15:34:39 -060089 BusChild *kid;
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060090 I2CSlave *slave = NULL;
Anthony Liguorib5ea9322011-12-04 20:39:20 -060091 I2CSlaveClass *sc;
pbrook0ff596d2007-05-23 00:03:59 +000092
Anthony Liguori0866aca2011-12-23 15:34:39 -060093 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
94 DeviceState *qdev = kid->child;
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060095 I2CSlave *candidate = I2C_SLAVE_FROM_QDEV(qdev);
Juha Riihimäkib3a21982009-06-08 09:27:19 +030096 if (candidate->address == address) {
97 slave = candidate;
pbrook0ff596d2007-05-23 00:03:59 +000098 break;
Juha Riihimäkib3a21982009-06-08 09:27:19 +030099 }
pbrook0ff596d2007-05-23 00:03:59 +0000100 }
101
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600102 if (!slave) {
pbrook0ff596d2007-05-23 00:03:59 +0000103 return 1;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600104 }
pbrook0ff596d2007-05-23 00:03:59 +0000105
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600106 sc = I2C_SLAVE_GET_CLASS(slave);
pbrook0ff596d2007-05-23 00:03:59 +0000107 /* If the bus is already busy, assume this is a repeated
108 start condition. */
Paul Brook02e2da42009-05-23 00:05:19 +0100109 bus->current_dev = slave;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600110 if (sc->event) {
111 sc->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
112 }
pbrook0ff596d2007-05-23 00:03:59 +0000113 return 0;
114}
115
116void i2c_end_transfer(i2c_bus *bus)
117{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600118 I2CSlave *dev = bus->current_dev;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600119 I2CSlaveClass *sc;
pbrook0ff596d2007-05-23 00:03:59 +0000120
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600121 if (!dev) {
pbrook0ff596d2007-05-23 00:03:59 +0000122 return;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600123 }
pbrook0ff596d2007-05-23 00:03:59 +0000124
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600125 sc = I2C_SLAVE_GET_CLASS(dev);
126 if (sc->event) {
127 sc->event(dev, I2C_FINISH);
128 }
pbrook0ff596d2007-05-23 00:03:59 +0000129
130 bus->current_dev = NULL;
131}
132
133int i2c_send(i2c_bus *bus, uint8_t data)
134{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600135 I2CSlave *dev = bus->current_dev;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600136 I2CSlaveClass *sc;
pbrook0ff596d2007-05-23 00:03:59 +0000137
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600138 if (!dev) {
pbrook0ff596d2007-05-23 00:03:59 +0000139 return -1;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600140 }
pbrook0ff596d2007-05-23 00:03:59 +0000141
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600142 sc = I2C_SLAVE_GET_CLASS(dev);
143 if (sc->send) {
144 return sc->send(dev, data);
145 }
146
147 return -1;
pbrook0ff596d2007-05-23 00:03:59 +0000148}
149
150int i2c_recv(i2c_bus *bus)
151{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600152 I2CSlave *dev = bus->current_dev;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600153 I2CSlaveClass *sc;
pbrook0ff596d2007-05-23 00:03:59 +0000154
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600155 if (!dev) {
pbrook0ff596d2007-05-23 00:03:59 +0000156 return -1;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600157 }
pbrook0ff596d2007-05-23 00:03:59 +0000158
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600159 sc = I2C_SLAVE_GET_CLASS(dev);
160 if (sc->recv) {
161 return sc->recv(dev);
162 }
163
164 return -1;
pbrook0ff596d2007-05-23 00:03:59 +0000165}
166
167void i2c_nack(i2c_bus *bus)
168{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600169 I2CSlave *dev = bus->current_dev;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600170 I2CSlaveClass *sc;
pbrook0ff596d2007-05-23 00:03:59 +0000171
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600172 if (!dev) {
pbrook0ff596d2007-05-23 00:03:59 +0000173 return;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600174 }
pbrook0ff596d2007-05-23 00:03:59 +0000175
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600176 sc = I2C_SLAVE_GET_CLASS(dev);
177 if (sc->event) {
178 sc->event(dev, I2C_NACK);
179 }
pbrook0ff596d2007-05-23 00:03:59 +0000180}
181
Juan Quintelabcbe8062009-09-29 22:48:28 +0200182static int i2c_slave_post_load(void *opaque, int version_id)
183{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600184 I2CSlave *dev = opaque;
Juan Quintelabcbe8062009-09-29 22:48:28 +0200185 i2c_bus *bus;
186 bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev));
187 if (bus->saved_address == dev->address) {
188 bus->current_dev = dev;
189 }
190 return 0;
191}
192
Juan Quintela18948392009-09-29 22:48:30 +0200193const VMStateDescription vmstate_i2c_slave = {
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600194 .name = "I2CSlave",
Juan Quintelabcbe8062009-09-29 22:48:28 +0200195 .version_id = 1,
196 .minimum_version_id = 1,
197 .minimum_version_id_old = 1,
198 .post_load = i2c_slave_post_load,
199 .fields = (VMStateField []) {
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600200 VMSTATE_UINT8(address, I2CSlave),
Juan Quintelabcbe8062009-09-29 22:48:28 +0200201 VMSTATE_END_OF_LIST()
202 }
203};
204
Anthony Liguorid307af72011-12-09 15:02:56 -0600205static int i2c_slave_qdev_init(DeviceState *dev)
Paul Brookfe8de492009-05-14 22:35:08 +0100206{
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600207 I2CSlave *s = I2C_SLAVE_FROM_QDEV(dev);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600208 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
Paul Brookfe8de492009-05-14 22:35:08 +0100209
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600210 return sc->init(s);
Paul Brookfe8de492009-05-14 22:35:08 +0100211}
212
Juan Quintela5b7f5322009-09-29 22:48:26 +0200213DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr)
Paul Brookfe8de492009-05-14 22:35:08 +0100214{
215 DeviceState *dev;
216
Paul Brook02e2da42009-05-23 00:05:19 +0100217 dev = qdev_create(&bus->qbus, name);
Juan Quintela5b7f5322009-09-29 22:48:26 +0200218 qdev_prop_set_uint8(dev, "address", addr);
Markus Armbrustere23a1b32009-10-07 01:15:58 +0200219 qdev_init_nofail(dev);
Paul Brookfe8de492009-05-14 22:35:08 +0100220 return dev;
balrogaa941b92007-05-24 18:50:09 +0000221}
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600222
Anthony Liguori39bffca2011-12-07 21:34:16 -0600223static void i2c_slave_class_init(ObjectClass *klass, void *data)
224{
225 DeviceClass *k = DEVICE_CLASS(klass);
226 k->init = i2c_slave_qdev_init;
Anthony Liguori0d936922012-05-02 09:00:20 +0200227 k->bus_type = TYPE_I2C_BUS;
Paolo Bonzinibce54472012-03-28 18:12:47 +0200228 k->props = i2c_props;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600229}
230
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600231static TypeInfo i2c_slave_type_info = {
232 .name = TYPE_I2C_SLAVE,
233 .parent = TYPE_DEVICE,
234 .instance_size = sizeof(I2CSlave),
235 .abstract = true,
236 .class_size = sizeof(I2CSlaveClass),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600237 .class_init = i2c_slave_class_init,
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600238};
239
Andreas Färber83f7d432012-02-09 15:20:55 +0100240static void i2c_slave_register_types(void)
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600241{
Anthony Liguori0d936922012-05-02 09:00:20 +0200242 type_register_static(&i2c_bus_info);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600243 type_register_static(&i2c_slave_type_info);
244}
245
Andreas Färber83f7d432012-02-09 15:20:55 +0100246type_init(i2c_slave_register_types)