10ff596d0Spbrook #ifndef QEMU_I2C_H 20ff596d0Spbrook #define QEMU_I2C_H 30ff596d0Spbrook 483c9f4caSPaolo Bonzini #include "hw/qdev.h" 5fe8de492SPaul Brook 60ff596d0Spbrook /* The QEMU I2C implementation only supports simple transfers that complete 70ff596d0Spbrook immediately. It does not support slave devices that need to be able to 80ff596d0Spbrook defer their response (eg. CPU slave interfaces where the data is supplied 90ff596d0Spbrook by the device driver in response to an interrupt). */ 100ff596d0Spbrook 110ff596d0Spbrook enum i2c_event { 120ff596d0Spbrook I2C_START_RECV, 130ff596d0Spbrook I2C_START_SEND, 140ff596d0Spbrook I2C_FINISH, 15aa1f17c1Sths I2C_NACK /* Masker NACKed a receive byte. */ 160ff596d0Spbrook }; 170ff596d0Spbrook 189e07bdf8SAnthony Liguori typedef struct I2CSlave I2CSlave; 190ff596d0Spbrook 20b5ea9327SAnthony Liguori #define TYPE_I2C_SLAVE "i2c-slave" 21b5ea9327SAnthony Liguori #define I2C_SLAVE(obj) \ 22b5ea9327SAnthony Liguori OBJECT_CHECK(I2CSlave, (obj), TYPE_I2C_SLAVE) 23b5ea9327SAnthony Liguori #define I2C_SLAVE_CLASS(klass) \ 24b5ea9327SAnthony Liguori OBJECT_CLASS_CHECK(I2CSlaveClass, (klass), TYPE_I2C_SLAVE) 25b5ea9327SAnthony Liguori #define I2C_SLAVE_GET_CLASS(obj) \ 26b5ea9327SAnthony Liguori OBJECT_GET_CLASS(I2CSlaveClass, (obj), TYPE_I2C_SLAVE) 279e07bdf8SAnthony Liguori 28b5ea9327SAnthony Liguori typedef struct I2CSlaveClass 29b5ea9327SAnthony Liguori { 30b5ea9327SAnthony Liguori DeviceClass parent_class; 3102e2da45SPaul Brook 32fe8de492SPaul Brook /* Callbacks provided by the device. */ 33b5ea9327SAnthony Liguori int (*init)(I2CSlave *dev); 34b5ea9327SAnthony Liguori 35b5ea9327SAnthony Liguori /* Master to slave. */ 36b5ea9327SAnthony Liguori int (*send)(I2CSlave *s, uint8_t data); 37b5ea9327SAnthony Liguori 38b5ea9327SAnthony Liguori /* Slave to master. */ 39b5ea9327SAnthony Liguori int (*recv)(I2CSlave *s); 40b5ea9327SAnthony Liguori 41b5ea9327SAnthony Liguori /* Notify the slave of a bus state change. */ 42b5ea9327SAnthony Liguori void (*event)(I2CSlave *s, enum i2c_event event); 43b5ea9327SAnthony Liguori } I2CSlaveClass; 44fe8de492SPaul Brook 459e07bdf8SAnthony Liguori struct I2CSlave 460ff596d0Spbrook { 47fe8de492SPaul Brook DeviceState qdev; 480ff596d0Spbrook 490ff596d0Spbrook /* Remaining fields for internal use by the I2C code. */ 505b7f5327SJuan Quintela uint8_t address; 510ff596d0Spbrook }; 520ff596d0Spbrook 53a5c82852SAndreas Färber I2CBus *i2c_init_bus(DeviceState *parent, const char *name); 549e07bdf8SAnthony Liguori void i2c_set_slave_address(I2CSlave *dev, uint8_t address); 55a5c82852SAndreas Färber int i2c_bus_busy(I2CBus *bus); 56a5c82852SAndreas Färber int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv); 57a5c82852SAndreas Färber void i2c_end_transfer(I2CBus *bus); 58a5c82852SAndreas Färber void i2c_nack(I2CBus *bus); 59*056fca7bSPeter Crosthwaite int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send); 60a5c82852SAndreas Färber int i2c_send(I2CBus *bus, uint8_t data); 61a5c82852SAndreas Färber int i2c_recv(I2CBus *bus); 620ff596d0Spbrook 63a5c82852SAndreas Färber DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr); 64fe8de492SPaul Brook 65adb86c37Sbalrog /* wm8750.c */ 66cdbe40caSPaul Brook void wm8750_data_req_set(DeviceState *dev, 67adb86c37Sbalrog void (*data_req)(void *, int, int), void *opaque); 68adb86c37Sbalrog void wm8750_dac_dat(void *opaque, uint32_t sample); 69adb86c37Sbalrog uint32_t wm8750_adc_dat(void *opaque); 70662caa6fSbalrog void *wm8750_dac_buffer(void *opaque, int samples); 71662caa6fSbalrog void wm8750_dac_commit(void *opaque); 72b0f74c87Sbalrog void wm8750_set_bclk_in(void *opaque, int new_hz); 73adb86c37Sbalrog 741d4e547bSbalrog /* lm832x.c */ 75c4f05c8cSPeter Maydell void lm832x_key_event(DeviceState *dev, int key, int state); 761d4e547bSbalrog 77701a8f76SPaolo Bonzini extern const VMStateDescription vmstate_i2c_slave; 78701a8f76SPaolo Bonzini 79701a8f76SPaolo Bonzini #define VMSTATE_I2C_SLAVE(_field, _state) { \ 80701a8f76SPaolo Bonzini .name = (stringify(_field)), \ 819e07bdf8SAnthony Liguori .size = sizeof(I2CSlave), \ 82701a8f76SPaolo Bonzini .vmsd = &vmstate_i2c_slave, \ 83701a8f76SPaolo Bonzini .flags = VMS_STRUCT, \ 849e07bdf8SAnthony Liguori .offset = vmstate_offset_value(_state, _field, I2CSlave), \ 85701a8f76SPaolo Bonzini } 86701a8f76SPaolo Bonzini 870ff596d0Spbrook #endif 88