xref: /openbmc/qemu/include/hw/i2c/i2c.h (revision 373b8ac794291c9a20198ac671728dbd74ac3771)
1 #ifndef QEMU_I2C_H
2 #define QEMU_I2C_H
3 
4 #include "hw/qdev.h"
5 
6 /* The QEMU I2C implementation only supports simple transfers that complete
7    immediately.  It does not support slave devices that need to be able to
8    defer their response (eg. CPU slave interfaces where the data is supplied
9    by the device driver in response to an interrupt).  */
10 
11 enum i2c_event {
12     I2C_START_RECV,
13     I2C_START_SEND,
14     I2C_FINISH,
15     I2C_NACK /* Masker NACKed a receive byte.  */
16 };
17 
18 typedef struct I2CSlave I2CSlave;
19 
20 #define TYPE_I2C_SLAVE "i2c-slave"
21 #define I2C_SLAVE(obj) \
22      OBJECT_CHECK(I2CSlave, (obj), TYPE_I2C_SLAVE)
23 #define I2C_SLAVE_CLASS(klass) \
24      OBJECT_CLASS_CHECK(I2CSlaveClass, (klass), TYPE_I2C_SLAVE)
25 #define I2C_SLAVE_GET_CLASS(obj) \
26      OBJECT_GET_CLASS(I2CSlaveClass, (obj), TYPE_I2C_SLAVE)
27 
28 typedef struct I2CSlaveClass {
29     DeviceClass parent_class;
30 
31     /* Callbacks provided by the device.  */
32     int (*init)(I2CSlave *dev);
33 
34     /* Master to slave. Returns non-zero for a NAK, 0 for success. */
35     int (*send)(I2CSlave *s, uint8_t data);
36 
37     /*
38      * Slave to master.  This cannot fail, the device should always
39      * return something here.  Negative values probably result in 0xff
40      * and a possible log from the driver, and shouldn't be used.
41      */
42     int (*recv)(I2CSlave *s);
43 
44     /*
45      * Notify the slave of a bus state change.  For start event,
46      * returns non-zero to NAK an operation.  For other events the
47      * return code is not used and should be zero.
48      */
49     int (*event)(I2CSlave *s, enum i2c_event event);
50 } I2CSlaveClass;
51 
52 struct I2CSlave {
53     DeviceState qdev;
54 
55     /* Remaining fields for internal use by the I2C code.  */
56     uint8_t address;
57 };
58 
59 I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
60 void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
61 int i2c_bus_busy(I2CBus *bus);
62 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv);
63 void i2c_end_transfer(I2CBus *bus);
64 void i2c_nack(I2CBus *bus);
65 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
66 int i2c_send(I2CBus *bus, uint8_t data);
67 int i2c_recv(I2CBus *bus);
68 
69 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
70 
71 /* lm832x.c */
72 void lm832x_key_event(DeviceState *dev, int key, int state);
73 
74 extern const VMStateDescription vmstate_i2c_slave;
75 
76 #define VMSTATE_I2C_SLAVE(_field, _state) {                          \
77     .name       = (stringify(_field)),                               \
78     .size       = sizeof(I2CSlave),                                  \
79     .vmsd       = &vmstate_i2c_slave,                                \
80     .flags      = VMS_STRUCT,                                        \
81     .offset     = vmstate_offset_value(_state, _field, I2CSlave),    \
82 }
83 
84 #endif
85