]>
Commit | Line | Data |
---|---|---|
5fafdf24 | 1 | /* |
0ff596d0 PB |
2 | * QEMU I2C bus interface. |
3 | * | |
4 | * Copyright (c) 2007 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
7 | * This code is licenced under the LGPL. | |
8 | */ | |
9 | ||
87ecb68b | 10 | #include "i2c.h" |
0ff596d0 PB |
11 | |
12 | struct i2c_bus | |
13 | { | |
02e2da45 | 14 | BusState qbus; |
0ff596d0 PB |
15 | i2c_slave *current_dev; |
16 | i2c_slave *dev; | |
c701b35b | 17 | int saved_address; |
0ff596d0 PB |
18 | }; |
19 | ||
10c4c98a GH |
20 | static struct BusInfo i2c_bus_info = { |
21 | .name = "I2C", | |
22 | .size = sizeof(i2c_bus), | |
ee6847d1 | 23 | .props = (Property[]) { |
368eb5d4 GH |
24 | DEFINE_PROP_UINT32("address", struct i2c_slave, address, 0), |
25 | DEFINE_PROP_END_OF_LIST(), | |
ee6847d1 | 26 | } |
10c4c98a GH |
27 | }; |
28 | ||
c701b35b PB |
29 | static void i2c_bus_save(QEMUFile *f, void *opaque) |
30 | { | |
31 | i2c_bus *bus = (i2c_bus *)opaque; | |
32 | ||
33 | qemu_put_byte(f, bus->current_dev ? bus->current_dev->address : -1); | |
34 | } | |
35 | ||
36 | static int i2c_bus_load(QEMUFile *f, void *opaque, int version_id) | |
37 | { | |
38 | i2c_bus *bus = (i2c_bus *)opaque; | |
39 | ||
40 | if (version_id != 1) | |
41 | return -EINVAL; | |
42 | ||
43 | /* The bus is loaded before attached devices, so load and save the | |
44 | current device id. Devices will check themselves as loaded. */ | |
d0c22f49 | 45 | bus->saved_address = (int8_t) qemu_get_byte(f); |
c701b35b PB |
46 | bus->current_dev = NULL; |
47 | ||
48 | return 0; | |
49 | } | |
50 | ||
0ff596d0 | 51 | /* Create a new I2C bus. */ |
02e2da45 | 52 | i2c_bus *i2c_init_bus(DeviceState *parent, const char *name) |
0ff596d0 PB |
53 | { |
54 | i2c_bus *bus; | |
55 | ||
10c4c98a | 56 | bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name)); |
c701b35b | 57 | register_savevm("i2c_bus", -1, 1, i2c_bus_save, i2c_bus_load, bus); |
0ff596d0 PB |
58 | return bus; |
59 | } | |
60 | ||
0ff596d0 PB |
61 | void i2c_set_slave_address(i2c_slave *dev, int address) |
62 | { | |
63 | dev->address = address; | |
64 | } | |
65 | ||
66 | /* Return nonzero if bus is busy. */ | |
67 | int i2c_bus_busy(i2c_bus *bus) | |
68 | { | |
69 | return bus->current_dev != NULL; | |
70 | } | |
71 | ||
4a2c8ac2 | 72 | /* Returns non-zero if the address is not valid. */ |
0ff596d0 PB |
73 | /* TODO: Make this handle multiple masters. */ |
74 | int i2c_start_transfer(i2c_bus *bus, int address, int recv) | |
75 | { | |
02e2da45 PB |
76 | DeviceState *qdev; |
77 | i2c_slave *slave = NULL; | |
0ff596d0 | 78 | |
02e2da45 PB |
79 | LIST_FOREACH(qdev, &bus->qbus.children, sibling) { |
80 | slave = I2C_SLAVE_FROM_QDEV(qdev); | |
81 | if (slave->address == address) | |
0ff596d0 PB |
82 | break; |
83 | } | |
84 | ||
02e2da45 | 85 | if (!slave) |
0ff596d0 PB |
86 | return 1; |
87 | ||
88 | /* If the bus is already busy, assume this is a repeated | |
89 | start condition. */ | |
02e2da45 PB |
90 | bus->current_dev = slave; |
91 | slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); | |
0ff596d0 PB |
92 | return 0; |
93 | } | |
94 | ||
95 | void i2c_end_transfer(i2c_bus *bus) | |
96 | { | |
97 | i2c_slave *dev = bus->current_dev; | |
98 | ||
99 | if (!dev) | |
100 | return; | |
101 | ||
fe8de492 | 102 | dev->info->event(dev, I2C_FINISH); |
0ff596d0 PB |
103 | |
104 | bus->current_dev = NULL; | |
105 | } | |
106 | ||
107 | int i2c_send(i2c_bus *bus, uint8_t data) | |
108 | { | |
109 | i2c_slave *dev = bus->current_dev; | |
110 | ||
111 | if (!dev) | |
112 | return -1; | |
113 | ||
fe8de492 | 114 | return dev->info->send(dev, data); |
0ff596d0 PB |
115 | } |
116 | ||
117 | int i2c_recv(i2c_bus *bus) | |
118 | { | |
119 | i2c_slave *dev = bus->current_dev; | |
120 | ||
121 | if (!dev) | |
122 | return -1; | |
123 | ||
fe8de492 | 124 | return dev->info->recv(dev); |
0ff596d0 PB |
125 | } |
126 | ||
127 | void i2c_nack(i2c_bus *bus) | |
128 | { | |
129 | i2c_slave *dev = bus->current_dev; | |
130 | ||
131 | if (!dev) | |
132 | return; | |
133 | ||
fe8de492 | 134 | dev->info->event(dev, I2C_NACK); |
0ff596d0 PB |
135 | } |
136 | ||
aa941b94 AZ |
137 | void i2c_slave_save(QEMUFile *f, i2c_slave *dev) |
138 | { | |
139 | qemu_put_byte(f, dev->address); | |
140 | } | |
141 | ||
142 | void i2c_slave_load(QEMUFile *f, i2c_slave *dev) | |
143 | { | |
fe8de492 | 144 | i2c_bus *bus; |
02e2da45 | 145 | bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
aa941b94 | 146 | dev->address = qemu_get_byte(f); |
fe8de492 PB |
147 | if (bus->saved_address == dev->address) { |
148 | bus->current_dev = dev; | |
149 | } | |
150 | } | |
151 | ||
81a322d4 | 152 | static int i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base) |
fe8de492 | 153 | { |
02e2da45 | 154 | I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev); |
fe8de492 PB |
155 | i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); |
156 | ||
157 | s->info = info; | |
fe8de492 | 158 | |
81a322d4 | 159 | return info->init(s); |
fe8de492 PB |
160 | } |
161 | ||
074f2fff | 162 | void i2c_register_slave(I2CSlaveInfo *info) |
fe8de492 | 163 | { |
074f2fff | 164 | assert(info->qdev.size >= sizeof(i2c_slave)); |
02e2da45 | 165 | info->qdev.init = i2c_slave_qdev_init; |
10c4c98a | 166 | info->qdev.bus_info = &i2c_bus_info; |
074f2fff | 167 | qdev_register(&info->qdev); |
fe8de492 PB |
168 | } |
169 | ||
170 | DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, int addr) | |
171 | { | |
172 | DeviceState *dev; | |
173 | ||
02e2da45 | 174 | dev = qdev_create(&bus->qbus, name); |
ee6847d1 | 175 | qdev_prop_set_uint32(dev, "address", addr); |
fe8de492 PB |
176 | qdev_init(dev); |
177 | return dev; | |
aa941b94 | 178 | } |