Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 2 | |
| 3 | #ifndef _DEVICE_I2C_BUS_H_ |
| 4 | #define _DEVICE_I2C_BUS_H_ |
| 5 | |
Elyes HAOUAS | 5817c56 | 2020-07-12 09:03:22 +0200 | [diff] [blame] | 6 | #include <stddef.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 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 { |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 13 | int (*transfer)(struct device *, const struct i2c_msg *, size_t count); |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 14 | }; |
| 15 | |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 16 | /* |
| 17 | * Returns the first upstream facing link whose bus implements either |
| 18 | * `i2c_bus_operations` *or* `smbus_bus_operations`. |
| 19 | * |
| 20 | * If not NULL, guarantees that `->dev`, `->dev->ops` and either |
| 21 | * `->dev->ops->ops_i2c_bus` or `->dev->ops->ops_smbus_bus` are |
| 22 | * not NULL. |
| 23 | */ |
| 24 | struct bus *i2c_link(struct device *); |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 25 | |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Shorthand for `i2c_link(dev)->dev`. |
| 28 | * |
| 29 | * Returns NULL if i2c_link(dev) returns NULL. |
| 30 | */ |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 31 | static inline DEVTREE_CONST struct device *i2c_busdev(struct device *dev) |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 32 | { |
| 33 | struct bus *const link = i2c_link(dev); |
| 34 | return link ? link->dev : NULL; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Slave driver interface functions. These will look for the next |
| 39 | * `i2c_bus_operations` *or* `smbus_bus_operations` and perform the |
| 40 | * respective transfers. |
| 41 | * |
| 42 | * The interface is limited to what current slave drivers demand. |
| 43 | * Extend as required. |
| 44 | * |
| 45 | * All functions return a negative `enum cb_err` value on error. |
| 46 | * Either CB_ERR, CB_ERR_ARG or any CB_I2C_* (cf. include/types.h). |
| 47 | */ |
| 48 | |
| 49 | /* |
| 50 | * Reads one byte. |
| 51 | * Compatible to smbus_recv_byte(). |
| 52 | * |
| 53 | * Returns the read byte on success, negative `enum cb_err` value on error. |
| 54 | */ |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 55 | int i2c_dev_readb(struct device *); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Writes the byte `val`. |
| 59 | * Compatible to smbus_send_byte(). |
| 60 | * |
| 61 | * Returns 0 on success, negative `enum cb_err` value on error. |
| 62 | */ |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 63 | int i2c_dev_writeb(struct device *, uint8_t val); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Sends the register offset `off` and reads one byte. |
| 67 | * Compatible to smbus_read_byte(). |
| 68 | * |
| 69 | * Returns the read byte on success, negative `enum cb_err` value on error. |
| 70 | */ |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 71 | int i2c_dev_readb_at(struct device *, uint8_t off); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Sends the register offset `off` followed by the byte `val`. |
| 75 | * Compatible to smbus_write_byte(). |
| 76 | * |
| 77 | * Returns 0 on success, negative `enum cb_err` value on error. |
| 78 | */ |
Aaron Durbin | 439cee9 | 2018-01-22 21:24:35 -0700 | [diff] [blame] | 79 | int i2c_dev_writeb_at(struct device *, uint8_t off, uint8_t val); |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 80 | |
Nico Huber | 9734af6 | 2017-11-01 13:15:33 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Sends the 16-bit register offset `off` and reads `len` bytes into `buf`. |
| 83 | * |
| 84 | * Returns the number of bytes read on success, negative `enum cb_err` |
| 85 | * value on error. |
| 86 | */ |
| 87 | int i2c_dev_read_at16(struct device *, uint8_t *buf, size_t len, uint16_t off); |
| 88 | |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 89 | #endif /* _DEVICE_I2C_BUS_H_ */ |