1 #ifndef QEMU_I2C_H 2 #define QEMU_I2C_H 3 4 #include "hw/qdev-core.h" 5 #include "qom/object.h" 6 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). */ 11 12 enum i2c_event { 13 I2C_START_RECV, 14 I2C_START_SEND, 15 I2C_FINISH, 16 I2C_NACK /* Masker NACKed a receive byte. */ 17 }; 18 19 typedef struct I2CNodeList I2CNodeList; 20 21 #define TYPE_I2C_SLAVE "i2c-slave" 22 OBJECT_DECLARE_TYPE(I2CSlave, I2CSlaveClass, 23 I2C_SLAVE) 24 25 struct I2CSlaveClass { 26 DeviceClass parent_class; 27 28 /* Master to slave. Returns non-zero for a NAK, 0 for success. */ 29 int (*send)(I2CSlave *s, uint8_t data); 30 31 /* 32 * Slave to master. This cannot fail, the device should always 33 * return something here. 34 */ 35 uint8_t (*recv)(I2CSlave *s); 36 37 /* 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. 41 */ 42 int (*event)(I2CSlave *s, enum i2c_event event); 43 44 /* 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. 48 * 49 * If broadcast is true, match should add the device and return true. 50 */ 51 bool (*match_and_add)(I2CSlave *candidate, uint8_t address, bool broadcast, 52 I2CNodeList *current_devs); 53 }; 54 55 struct I2CSlave { 56 DeviceState qdev; 57 58 /* Remaining fields for internal use by the I2C code. */ 59 uint8_t address; 60 }; 61 62 #define TYPE_I2C_BUS "i2c-bus" 63 OBJECT_DECLARE_SIMPLE_TYPE(I2CBus, I2C_BUS) 64 65 typedef struct I2CNode I2CNode; 66 67 struct I2CNode { 68 I2CSlave *elt; 69 QLIST_ENTRY(I2CNode) next; 70 }; 71 72 typedef struct I2CPendingMaster I2CPendingMaster; 73 74 struct I2CPendingMaster { 75 QEMUBH *bh; 76 QSIMPLEQ_ENTRY(I2CPendingMaster) entry; 77 }; 78 79 typedef QLIST_HEAD(I2CNodeList, I2CNode) I2CNodeList; 80 typedef QSIMPLEQ_HEAD(I2CPendingMasters, I2CPendingMaster) I2CPendingMasters; 81 82 struct I2CBus { 83 BusState qbus; 84 I2CNodeList current_devs; 85 I2CPendingMasters pending_masters; 86 uint8_t saved_address; 87 bool broadcast; 88 89 /* Set from slave currently mastering the bus. */ 90 QEMUBH *bh; 91 }; 92 93 I2CBus *i2c_init_bus(DeviceState *parent, const char *name); 94 int i2c_bus_busy(I2CBus *bus); 95 96 /** 97 * i2c_start_transfer: start a transfer on an I2C bus. 98 * 99 * @bus: #I2CBus to be used 100 * @address: address of the slave 101 * @is_recv: indicates the transfer direction 102 * 103 * When @is_recv is a known boolean constant, use the 104 * i2c_start_recv() or i2c_start_send() helper instead. 105 * 106 * Returns: 0 on success, -1 on error 107 */ 108 int i2c_start_transfer(I2CBus *bus, uint8_t address, bool is_recv); 109 110 /** 111 * i2c_start_recv: start a 'receive' transfer on an I2C bus. 112 * 113 * @bus: #I2CBus to be used 114 * @address: address of the slave 115 * 116 * Returns: 0 on success, -1 on error 117 */ 118 int i2c_start_recv(I2CBus *bus, uint8_t address); 119 120 /** 121 * i2c_start_send: start a 'send' transfer on an I2C bus. 122 * 123 * @bus: #I2CBus to be used 124 * @address: address of the slave 125 * 126 * Returns: 0 on success, -1 on error 127 */ 128 int i2c_start_send(I2CBus *bus, uint8_t address); 129 130 void i2c_end_transfer(I2CBus *bus); 131 void i2c_nack(I2CBus *bus); 132 void i2c_bus_master(I2CBus *bus, QEMUBH *bh); 133 void i2c_bus_release(I2CBus *bus); 134 int i2c_send(I2CBus *bus, uint8_t data); 135 uint8_t i2c_recv(I2CBus *bus); 136 bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast, 137 I2CNodeList *current_devs); 138 139 /** 140 * Create an I2C slave device on the heap. 141 * @name: a device type name 142 * @addr: I2C address of the slave when put on a bus 143 * 144 * This only initializes the device state structure and allows 145 * properties to be set. Type @name must exist. The device still 146 * needs to be realized. See qdev-core.h. 147 */ 148 I2CSlave *i2c_slave_new(const char *name, uint8_t addr); 149 150 /** 151 * Create and realize an I2C slave device on the heap. 152 * @bus: I2C bus to put it on 153 * @name: I2C slave device type name 154 * @addr: I2C address of the slave when put on a bus 155 * 156 * Create the device state structure, initialize it, put it on the 157 * specified @bus, and drop the reference to it (the device is realized). 158 */ 159 I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr); 160 161 /** 162 * Realize and drop a reference an I2C slave device 163 * @dev: I2C slave device to realize 164 * @bus: I2C bus to put it on 165 * @addr: I2C address of the slave on the bus 166 * @errp: pointer to NULL initialized error object 167 * 168 * Returns: %true on success, %false on failure. 169 * 170 * Call 'realize' on @dev, put it on the specified @bus, and drop the 171 * reference to it. 172 * 173 * This function is useful if you have created @dev via qdev_new(), 174 * i2c_slave_new() or i2c_slave_try_new() (which take a reference to 175 * the device it returns to you), so that you can set properties on it 176 * before realizing it. If you don't need to set properties then 177 * i2c_slave_create_simple() is probably better (as it does the create, 178 * init and realize in one step). 179 * 180 * If you are embedding the I2C slave into another QOM device and 181 * initialized it via some variant on object_initialize_child() then 182 * do not use this function, because that family of functions arrange 183 * for the only reference to the child device to be held by the parent 184 * via the child<> property, and so the reference-count-drop done here 185 * would be incorrect. (Instead you would want i2c_slave_realize(), 186 * which doesn't currently exist but would be trivial to create if we 187 * had any code that wanted it.) 188 */ 189 bool i2c_slave_realize_and_unref(I2CSlave *dev, I2CBus *bus, Error **errp); 190 191 /** 192 * Set the I2C bus address of a slave device 193 * @dev: I2C slave device 194 * @address: I2C address of the slave when put on a bus 195 */ 196 void i2c_slave_set_address(I2CSlave *dev, uint8_t address); 197 198 extern const VMStateDescription vmstate_i2c_slave; 199 200 #define VMSTATE_I2C_SLAVE(_field, _state) { \ 201 .name = (stringify(_field)), \ 202 .size = sizeof(I2CSlave), \ 203 .vmsd = &vmstate_i2c_slave, \ 204 .flags = VMS_STRUCT, \ 205 .offset = vmstate_offset_value(_state, _field, I2CSlave), \ 206 } 207 208 #endif 209