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