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