1#ifndef QEMU_I2C_H
2#define QEMU_I2C_H
3
4#include "hw/qdev.h"
5
6
7
8
9
10
11enum i2c_event {
12 I2C_START_RECV,
13 I2C_START_SEND,
14 I2C_FINISH,
15 I2C_NACK
16};
17
18typedef 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
28typedef struct I2CSlaveClass {
29 DeviceClass parent_class;
30
31
32 int (*init)(I2CSlave *dev);
33
34
35 int (*send)(I2CSlave *s, uint8_t data);
36
37
38
39
40
41
42 int (*recv)(I2CSlave *s);
43
44
45
46
47
48
49 int (*event)(I2CSlave *s, enum i2c_event event);
50} I2CSlaveClass;
51
52struct I2CSlave {
53 DeviceState qdev;
54
55
56 uint8_t address;
57};
58
59#define TYPE_I2C_BUS "i2c-bus"
60#define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
61
62typedef struct I2CNode I2CNode;
63
64struct I2CNode {
65 I2CSlave *elt;
66 QLIST_ENTRY(I2CNode) next;
67};
68
69struct I2CBus {
70 BusState qbus;
71 QLIST_HEAD(, I2CNode) current_devs;
72 uint8_t saved_address;
73 bool broadcast;
74};
75
76I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
77void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
78int i2c_bus_busy(I2CBus *bus);
79int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv);
80void i2c_end_transfer(I2CBus *bus);
81void i2c_nack(I2CBus *bus);
82int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
83int i2c_send(I2CBus *bus, uint8_t data);
84int i2c_recv(I2CBus *bus);
85
86DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
87
88
89void lm832x_key_event(DeviceState *dev, int key, int state);
90
91extern const VMStateDescription vmstate_i2c_slave;
92
93#define VMSTATE_I2C_SLAVE(_field, _state) { \
94 .name = (stringify(_field)), \
95 .size = sizeof(I2CSlave), \
96 .vmsd = &vmstate_i2c_slave, \
97 .flags = VMS_STRUCT, \
98 .offset = vmstate_offset_value(_state, _field, I2CSlave), \
99}
100
101#endif
102