1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef _DEVICE_I2C_BUS_H_
4 #define _DEVICE_I2C_BUS_H_
8 #include <device/i2c.h>
9 #include <device/device.h>
11 /* I2C bus operation for ramstage drivers */
12 struct i2c_bus_operations
{
13 int (*transfer
)(struct device
*, const struct i2c_msg
*, size_t count
);
17 * Determine device presence at a given slave address.
19 bool i2c_dev_detect(struct device
*dev
, unsigned int addr
);
22 * Returns the first upstream facing link whose bus implements either
23 * `i2c_bus_operations` *or* `smbus_bus_operations`.
25 * If not NULL, guarantees that `->dev`, `->dev->ops` and either
26 * `->dev->ops->ops_i2c_bus` or `->dev->ops->ops_smbus_bus` are
29 struct bus
*i2c_link(const struct device
*dev
);
32 * Shorthand for `i2c_link(dev)->dev`.
34 * Returns NULL if i2c_link(dev) returns NULL.
36 static inline DEVTREE_CONST
struct device
*i2c_busdev(const struct device
*dev
)
38 struct bus
*const link
= i2c_link(dev
);
39 return link
? link
->dev
: NULL
;
43 * Slave driver interface functions. These will look for the next
44 * `i2c_bus_operations` *or* `smbus_bus_operations` and perform the
45 * respective transfers.
47 * The interface is limited to what current slave drivers demand.
50 * All functions return a negative `enum cb_err` value on error.
51 * Either CB_ERR, CB_ERR_ARG or any CB_I2C_* (cf. include/types.h).
56 * Compatible to smbus_recv_byte().
58 * Returns the read byte on success, negative `enum cb_err` value on error.
60 int i2c_dev_readb(struct device
*);
63 * Writes the byte `val`.
64 * Compatible to smbus_send_byte().
66 * Returns 0 on success, negative `enum cb_err` value on error.
68 int i2c_dev_writeb(struct device
*, uint8_t val
);
71 * Sends the register offset `off` and reads one byte.
72 * Compatible to smbus_read_byte().
74 * Returns the read byte on success, negative `enum cb_err` value on error.
76 int i2c_dev_readb_at(struct device
*, uint8_t off
);
79 * Sends the register offset `off` followed by the byte `val`.
80 * Compatible to smbus_write_byte().
82 * Returns 0 on success, negative `enum cb_err` value on error.
84 int i2c_dev_writeb_at(struct device
*, uint8_t off
, uint8_t val
);
87 * Sends the 16-bit register offset `off` and reads `len` bytes into `buf`.
89 * Returns the number of bytes read on success, negative `enum cb_err`
92 int i2c_dev_read_at16(struct device
*, uint8_t *buf
, size_t len
, uint16_t off
);
94 * Sends the 8-bit register offset `off` and reads `len` bytes into `buf`.
96 * Returns the number of bytes read on success, negative `enum cb_err`
99 int i2c_dev_read_at(struct device
*, uint8_t *buf
, size_t len
, uint8_t off
);
101 * Sends the 8-bit register offset `off` and writes `len` bytes from `buf`.
103 * Returns the number of bytes written on success, negative `enum cb_err`
106 int i2c_dev_write_at(struct device
*, uint8_t *buf
, size_t len
, uint8_t off
);
108 #endif /* _DEVICE_I2C_BUS_H_ */