| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #ifndef _DEVICE_I2C_BUS_H_ |
| 4 | #define _DEVICE_I2C_BUS_H_ |
| 5 | |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | #include <device/i2c.h> |
| 9 | #include <device/device.h> |
| 10 | |
| 11 | /* I2C bus operation for ramstage drivers */ |
| 12 | struct i2c_bus_operations { |
| 13 | int (*transfer)(struct device *, const struct i2c_msg *, size_t count); |
| 14 | }; |
| 15 | |
| 16 | /** |
| 17 | * Determine device presence at a given slave address. |
| 18 | */ |
| 19 | bool i2c_dev_detect(struct device *dev, unsigned int addr); |
| 20 | |
| 21 | /* |
| 22 | * Returns the first upstream facing link whose bus implements either |
| 23 | * `i2c_bus_operations` *or* `smbus_bus_operations`. |
| 24 | * |
| 25 | * If not NULL, guarantees that `->dev`, `->dev->ops` and either |
| 26 | * `->dev->ops->ops_i2c_bus` or `->dev->ops->ops_smbus_bus` are |
| 27 | * not NULL. |
| 28 | */ |
| 29 | struct bus *i2c_link(const struct device *dev); |
| 30 | |
| 31 | /* |
| 32 | * Shorthand for `i2c_link(dev)->dev`. |
| 33 | * |
| 34 | * Returns NULL if i2c_link(dev) returns NULL. |
| 35 | */ |
| 36 | static inline DEVTREE_CONST struct device *i2c_busdev(const struct device *dev) |
| 37 | { |
| 38 | struct bus *const link = i2c_link(dev); |
| 39 | return link ? link->dev : NULL; |
| 40 | } |
| 41 | |
| 42 | /* |
| 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. |
| 46 | * |
| 47 | * The interface is limited to what current slave drivers demand. |
| 48 | * Extend as required. |
| 49 | * |
| 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). |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * Reads one byte. |
| 56 | * Compatible to smbus_recv_byte(). |
| 57 | * |
| 58 | * Returns the read byte on success, negative `enum cb_err` value on error. |
| 59 | */ |
| 60 | int i2c_dev_readb(struct device *); |
| 61 | |
| 62 | /* |
| 63 | * Writes the byte `val`. |
| 64 | * Compatible to smbus_send_byte(). |
| 65 | * |
| 66 | * Returns 0 on success, negative `enum cb_err` value on error. |
| 67 | */ |
| 68 | int i2c_dev_writeb(struct device *, uint8_t val); |
| 69 | |
| 70 | /* |
| 71 | * Sends the register offset `off` and reads one byte. |
| 72 | * Compatible to smbus_read_byte(). |
| 73 | * |
| 74 | * Returns the read byte on success, negative `enum cb_err` value on error. |
| 75 | */ |
| 76 | int i2c_dev_readb_at(struct device *, uint8_t off); |
| 77 | |
| 78 | /* |
| 79 | * Sends the register offset `off` followed by the byte `val`. |
| 80 | * Compatible to smbus_write_byte(). |
| 81 | * |
| 82 | * Returns 0 on success, negative `enum cb_err` value on error. |
| 83 | */ |
| 84 | int i2c_dev_writeb_at(struct device *, uint8_t off, uint8_t val); |
| 85 | |
| 86 | /* |
| 87 | * Sends the 16-bit register offset `off` and reads `len` bytes into `buf`. |
| 88 | * |
| 89 | * Returns the number of bytes read on success, negative `enum cb_err` |
| 90 | * value on error. |
| 91 | */ |
| 92 | int i2c_dev_read_at16(struct device *, uint8_t *buf, size_t len, uint16_t off); |
| 93 | /* |
| 94 | * Sends the 8-bit register offset `off` and reads `len` bytes into `buf`. |
| 95 | * |
| 96 | * Returns the number of bytes read on success, negative `enum cb_err` |
| 97 | * value on error. |
| 98 | */ |
| 99 | int i2c_dev_read_at(struct device *, uint8_t *buf, size_t len, uint8_t off); |
| 100 | /* |
| 101 | * Sends the 8-bit register offset `off` and writes `len` bytes from `buf`. |
| 102 | * |
| 103 | * Returns the number of bytes written on success, negative `enum cb_err` |
| 104 | * value on error. |
| 105 | */ |
| 106 | int i2c_dev_write_at(struct device *, uint8_t *buf, size_t len, uint8_t off); |
| 107 | |
| 108 | #endif /* _DEVICE_I2C_BUS_H_ */ |