2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
15 i2c_slave
*current_dev
;
20 static struct BusInfo i2c_bus_info
= {
22 .size
= sizeof(i2c_bus
),
25 static void i2c_bus_save(QEMUFile
*f
, void *opaque
)
27 i2c_bus
*bus
= (i2c_bus
*)opaque
;
29 qemu_put_byte(f
, bus
->current_dev
? bus
->current_dev
->address
: -1);
32 static int i2c_bus_load(QEMUFile
*f
, void *opaque
, int version_id
)
34 i2c_bus
*bus
= (i2c_bus
*)opaque
;
39 /* The bus is loaded before attached devices, so load and save the
40 current device id. Devices will check themselves as loaded. */
41 bus
->saved_address
= (int8_t) qemu_get_byte(f
);
42 bus
->current_dev
= NULL
;
47 /* Create a new I2C bus. */
48 i2c_bus
*i2c_init_bus(DeviceState
*parent
, const char *name
)
52 bus
= FROM_QBUS(i2c_bus
, qbus_create(&i2c_bus_info
, parent
, name
));
53 register_savevm("i2c_bus", -1, 1, i2c_bus_save
, i2c_bus_load
, bus
);
57 void i2c_set_slave_address(i2c_slave
*dev
, int address
)
59 dev
->address
= address
;
62 /* Return nonzero if bus is busy. */
63 int i2c_bus_busy(i2c_bus
*bus
)
65 return bus
->current_dev
!= NULL
;
68 /* Returns non-zero if the address is not valid. */
69 /* TODO: Make this handle multiple masters. */
70 int i2c_start_transfer(i2c_bus
*bus
, int address
, int recv
)
73 i2c_slave
*slave
= NULL
;
75 LIST_FOREACH(qdev
, &bus
->qbus
.children
, sibling
) {
76 slave
= I2C_SLAVE_FROM_QDEV(qdev
);
77 if (slave
->address
== address
)
84 /* If the bus is already busy, assume this is a repeated
86 bus
->current_dev
= slave
;
87 slave
->info
->event(slave
, recv
? I2C_START_RECV
: I2C_START_SEND
);
91 void i2c_end_transfer(i2c_bus
*bus
)
93 i2c_slave
*dev
= bus
->current_dev
;
98 dev
->info
->event(dev
, I2C_FINISH
);
100 bus
->current_dev
= NULL
;
103 int i2c_send(i2c_bus
*bus
, uint8_t data
)
105 i2c_slave
*dev
= bus
->current_dev
;
110 return dev
->info
->send(dev
, data
);
113 int i2c_recv(i2c_bus
*bus
)
115 i2c_slave
*dev
= bus
->current_dev
;
120 return dev
->info
->recv(dev
);
123 void i2c_nack(i2c_bus
*bus
)
125 i2c_slave
*dev
= bus
->current_dev
;
130 dev
->info
->event(dev
, I2C_NACK
);
133 void i2c_slave_save(QEMUFile
*f
, i2c_slave
*dev
)
135 qemu_put_byte(f
, dev
->address
);
138 void i2c_slave_load(QEMUFile
*f
, i2c_slave
*dev
)
141 bus
= FROM_QBUS(i2c_bus
, qdev_get_parent_bus(&dev
->qdev
));
142 dev
->address
= qemu_get_byte(f
);
143 if (bus
->saved_address
== dev
->address
) {
144 bus
->current_dev
= dev
;
148 static void i2c_slave_qdev_init(DeviceState
*dev
, DeviceInfo
*base
)
150 I2CSlaveInfo
*info
= container_of(base
, I2CSlaveInfo
, qdev
);
151 i2c_slave
*s
= I2C_SLAVE_FROM_QDEV(dev
);
154 s
->address
= qdev_get_prop_int(dev
, "address", 0);
159 void i2c_register_slave(I2CSlaveInfo
*info
)
161 assert(info
->qdev
.size
>= sizeof(i2c_slave
));
162 info
->qdev
.init
= i2c_slave_qdev_init
;
163 info
->qdev
.bus_info
= &i2c_bus_info
;
164 qdev_register(&info
->qdev
);
167 DeviceState
*i2c_create_slave(i2c_bus
*bus
, const char *name
, int addr
)
171 dev
= qdev_create(&bus
->qbus
, name
);
172 qdev_set_prop_int(dev
, "address", addr
);