hw/i2c: Remove confusing i2c_send_recv()
[qemu/ar7.git] / include / hw / i2c / i2c.h
blob99635b837a512a9faf98f682e88db8a0471a5ddb
1 #ifndef QEMU_I2C_H
2 #define QEMU_I2C_H
4 #include "hw/qdev-core.h"
5 #include "qom/object.h"
7 /* The QEMU I2C implementation only supports simple transfers that complete
8 immediately. It does not support slave devices that need to be able to
9 defer their response (eg. CPU slave interfaces where the data is supplied
10 by the device driver in response to an interrupt). */
12 enum i2c_event {
13 I2C_START_RECV,
14 I2C_START_SEND,
15 I2C_FINISH,
16 I2C_NACK /* Masker NACKed a receive byte. */
19 typedef struct I2CNodeList I2CNodeList;
21 #define TYPE_I2C_SLAVE "i2c-slave"
22 OBJECT_DECLARE_TYPE(I2CSlave, I2CSlaveClass,
23 I2C_SLAVE)
25 struct I2CSlaveClass {
26 DeviceClass parent_class;
28 /* Master to slave. Returns non-zero for a NAK, 0 for success. */
29 int (*send)(I2CSlave *s, uint8_t data);
32 * Slave to master. This cannot fail, the device should always
33 * return something here.
35 uint8_t (*recv)(I2CSlave *s);
38 * Notify the slave of a bus state change. For start event,
39 * returns non-zero to NAK an operation. For other events the
40 * return code is not used and should be zero.
42 int (*event)(I2CSlave *s, enum i2c_event event);
45 * Check if this device matches the address provided. Returns bool of
46 * true if it matches (or broadcast), and updates the device list, false
47 * otherwise.
49 * If broadcast is true, match should add the device and return true.
51 bool (*match_and_add)(I2CSlave *candidate, uint8_t address, bool broadcast,
52 I2CNodeList *current_devs);
55 struct I2CSlave {
56 DeviceState qdev;
58 /* Remaining fields for internal use by the I2C code. */
59 uint8_t address;
62 #define TYPE_I2C_BUS "i2c-bus"
63 OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS)
65 typedef struct I2CNode I2CNode;
67 struct I2CNode {
68 I2CSlave *elt;
69 QLIST_ENTRY(I2CNode) next;
72 typedef QLIST_HEAD(I2CNodeList, I2CNode) I2CNodeList;
74 struct I2CBus {
75 BusState qbus;
76 I2CNodeList current_devs;
77 uint8_t saved_address;
78 bool broadcast;
81 I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
82 void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
83 int i2c_bus_busy(I2CBus *bus);
84 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv);
85 void i2c_end_transfer(I2CBus *bus);
86 void i2c_nack(I2CBus *bus);
87 int i2c_send(I2CBus *bus, uint8_t data);
88 uint8_t i2c_recv(I2CBus *bus);
89 bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast,
90 I2CNodeList *current_devs);
92 /**
93 * Create an I2C slave device on the heap.
94 * @name: a device type name
95 * @addr: I2C address of the slave when put on a bus
97 * This only initializes the device state structure and allows
98 * properties to be set. Type @name must exist. The device still
99 * needs to be realized. See qdev-core.h.
101 I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
104 * Create and realize an I2C slave device on the heap.
105 * @bus: I2C bus to put it on
106 * @name: I2C slave device type name
107 * @addr: I2C address of the slave when put on a bus
109 * Create the device state structure, initialize it, put it on the
110 * specified @bus, and drop the reference to it (the device is realized).
112 I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr);
115 * Realize and drop a reference an I2C slave device
116 * @dev: I2C slave device to realize
117 * @bus: I2C bus to put it on
118 * @addr: I2C address of the slave on the bus
119 * @errp: pointer to NULL initialized error object
121 * Returns: %true on success, %false on failure.
123 * Call 'realize' on @dev, put it on the specified @bus, and drop the
124 * reference to it.
126 * This function is useful if you have created @dev via qdev_new(),
127 * i2c_slave_new() or i2c_slave_try_new() (which take a reference to
128 * the device it returns to you), so that you can set properties on it
129 * before realizing it. If you don't need to set properties then
130 * i2c_slave_create_simple() is probably better (as it does the create,
131 * init and realize in one step).
133 * If you are embedding the I2C slave into another QOM device and
134 * initialized it via some variant on object_initialize_child() then
135 * do not use this function, because that family of functions arrange
136 * for the only reference to the child device to be held by the parent
137 * via the child<> property, and so the reference-count-drop done here
138 * would be incorrect. (Instead you would want i2c_slave_realize(),
139 * which doesn't currently exist but would be trivial to create if we
140 * had any code that wanted it.)
142 bool i2c_slave_realize_and_unref(I2CSlave *dev, I2CBus *bus, Error **errp);
144 extern const VMStateDescription vmstate_i2c_slave;
146 #define VMSTATE_I2C_SLAVE(_field, _state) { \
147 .name = (stringify(_field)), \
148 .size = sizeof(I2CSlave), \
149 .vmsd = &vmstate_i2c_slave, \
150 .flags = VMS_STRUCT, \
151 .offset = vmstate_offset_value(_state, _field, I2CSlave), \
154 #endif