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