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 I2CSlave I2CSlave; 20 21 #define TYPE_I2C_SLAVE "i2c-slave" 22 typedef struct I2CSlaveClass I2CSlaveClass; 23 #define I2C_SLAVE(obj) \ 24 OBJECT_CHECK(I2CSlave, (obj), TYPE_I2C_SLAVE) 25 #define I2C_SLAVE_CLASS(klass) \ 26 OBJECT_CLASS_CHECK(I2CSlaveClass, (klass), TYPE_I2C_SLAVE) 27 #define I2C_SLAVE_GET_CLASS(obj) \ 28 OBJECT_GET_CLASS(I2CSlaveClass, (obj), TYPE_I2C_SLAVE) 29 30 struct I2CSlaveClass { 31 DeviceClass parent_class; 32 33 /* Master to slave. Returns non-zero for a NAK, 0 for success. */ 34 int (*send)(I2CSlave *s, uint8_t data); 35 36 /* 37 * Slave to master. This cannot fail, the device should always 38 * return something here. 39 */ 40 uint8_t (*recv)(I2CSlave *s); 41 42 /* 43 * Notify the slave of a bus state change. For start event, 44 * returns non-zero to NAK an operation. For other events the 45 * return code is not used and should be zero. 46 */ 47 int (*event)(I2CSlave *s, enum i2c_event event); 48 }; 49 50 struct I2CSlave { 51 DeviceState qdev; 52 53 /* Remaining fields for internal use by the I2C code. */ 54 uint8_t address; 55 }; 56 57 #define TYPE_I2C_BUS "i2c-bus" 58 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS) 59 60 typedef struct I2CNode I2CNode; 61 62 struct I2CNode { 63 I2CSlave *elt; 64 QLIST_ENTRY(I2CNode) next; 65 }; 66 67 struct I2CBus { 68 BusState qbus; 69 QLIST_HEAD(, I2CNode) current_devs; 70 uint8_t saved_address; 71 bool broadcast; 72 }; 73 74 I2CBus *i2c_init_bus(DeviceState *parent, const char *name); 75 void i2c_set_slave_address(I2CSlave *dev, uint8_t address); 76 int i2c_bus_busy(I2CBus *bus); 77 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv); 78 void i2c_end_transfer(I2CBus *bus); 79 void i2c_nack(I2CBus *bus); 80 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send); 81 int i2c_send(I2CBus *bus, uint8_t data); 82 uint8_t i2c_recv(I2CBus *bus); 83 84 /** 85 * Create an I2C slave device on the heap. 86 * @name: a device type name 87 * @addr: I2C address of the slave when put on a bus 88 * 89 * This only initializes the device state structure and allows 90 * properties to be set. Type @name must exist. The device still 91 * needs to be realized. See qdev-core.h. 92 */ 93 I2CSlave *i2c_slave_new(const char *name, uint8_t addr); 94 95 /** 96 * Create and realize an I2C slave device on the heap. 97 * @bus: I2C bus to put it on 98 * @name: I2C slave device type name 99 * @addr: I2C address of the slave when put on a bus 100 * 101 * Create the device state structure, initialize it, put it on the 102 * specified @bus, and drop the reference to it (the device is realized). 103 */ 104 I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr); 105 106 /** 107 * Realize and drop a reference an I2C slave device 108 * @dev: I2C slave device to realize 109 * @bus: I2C bus to put it on 110 * @addr: I2C address of the slave on the bus 111 * @errp: pointer to NULL initialized error object 112 * 113 * Returns: %true on success, %false on failure. 114 * 115 * Call 'realize' on @dev, put it on the specified @bus, and drop the 116 * reference to it. 117 * 118 * This function is useful if you have created @dev via qdev_new(), 119 * i2c_slave_new() or i2c_slave_try_new() (which take a reference to 120 * the device it returns to you), so that you can set properties on it 121 * before realizing it. If you don't need to set properties then 122 * i2c_slave_create_simple() is probably better (as it does the create, 123 * init and realize in one step). 124 * 125 * If you are embedding the I2C slave into another QOM device and 126 * initialized it via some variant on object_initialize_child() then 127 * do not use this function, because that family of functions arrange 128 * for the only reference to the child device to be held by the parent 129 * via the child<> property, and so the reference-count-drop done here 130 * would be incorrect. (Instead you would want i2c_slave_realize(), 131 * which doesn't currently exist but would be trivial to create if we 132 * had any code that wanted it.) 133 */ 134 bool i2c_slave_realize_and_unref(I2CSlave *dev, I2CBus *bus, Error **errp); 135 136 /* lm832x.c */ 137 void lm832x_key_event(DeviceState *dev, int key, int state); 138 139 extern const VMStateDescription vmstate_i2c_slave; 140 141 #define VMSTATE_I2C_SLAVE(_field, _state) { \ 142 .name = (stringify(_field)), \ 143 .size = sizeof(I2CSlave), \ 144 .vmsd = &vmstate_i2c_slave, \ 145 .flags = VMS_STRUCT, \ 146 .offset = vmstate_offset_value(_state, _field, I2CSlave), \ 147 } 148 149 #endif 150