1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_I2C_RTIO_H_
8 #define ZEPHYR_DRIVERS_I2C_RTIO_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/rtio/rtio.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief Driver context for implementing i2c with rtio
20  */
21 struct i2c_rtio {
22 	struct k_sem lock;
23 	struct k_spinlock slock;
24 	struct rtio *r;
25 	struct mpsc io_q;
26 	struct rtio_iodev iodev;
27 	struct rtio_iodev_sqe *txn_head;
28 	struct rtio_iodev_sqe *txn_curr;
29 	struct i2c_dt_spec dt_spec;
30 };
31 
32 /**
33  * @brief Statically define an i2c_rtio context
34  *
35  * @param _name Symbolic name of the context
36  * @param _sq_sz Submission queue entry pool size
37  * @param _cq_sz Completion queue entry pool size
38  */
39 #define I2C_RTIO_DEFINE(_name, _sq_sz, _cq_sz)		\
40 	RTIO_DEFINE(CONCAT(_name, _r), _sq_sz, _cq_sz);	\
41 	static struct i2c_rtio _name = {		\
42 		.r = &CONCAT(_name, _r),		\
43 	};
44 
45 /**
46  * @brief Copy an array of i2c_msgs to rtio submissions and a transaction
47  *
48  * @retval sqe Last sqe setup in the copy
49  * @retval NULL Not enough memory to copy the transaction
50  */
51 struct rtio_sqe *i2c_rtio_copy(struct rtio *r, struct rtio_iodev *iodev, const struct i2c_msg *msgs,
52 			       uint8_t num_msgs);
53 
54 /**
55  * @brief Initialize an i2c rtio context
56  *
57  * @param ctx I2C RTIO driver context
58  * @param dev I2C bus
59  */
60 void i2c_rtio_init(struct i2c_rtio *ctx, const struct device *dev);
61 
62 /**
63  * @brief Signal that the current (ctx->txn_curr) submission has been completed
64  *
65  * @param ctx I2C RTIO driver context
66  * @param status Completion status, negative values are errors
67  *
68  * @retval true Next submission is ready to start
69  * @retval false No more submissions to work on
70  */
71 bool i2c_rtio_complete(struct i2c_rtio *ctx, int status);
72 
73 /**
74  * @brief Submit, atomically, a submission to work on at some point
75  *
76  * @retval true Next submission is ready to start
77  * @retval false No new submission to start or submissions are in progress already
78  */
79 bool i2c_rtio_submit(struct i2c_rtio *ctx, struct rtio_iodev_sqe *iodev_sqe);
80 
81 /**
82  * @brief Configure the I2C bus controller
83  *
84  * Provides a compatible API for the existing i2c_configure API, and blocks the
85  * caller until the transfer completes.
86  *
87  * See i2c_configure().
88  */
89 int i2c_rtio_configure(struct i2c_rtio *ctx, uint32_t i2c_config);
90 
91 /**
92  * @brief Transfer i2c messages in a blocking call
93  *
94  * Provides a compatible API for the existing i2c_transfer API, and blocks the caller
95  * until the transfer completes.
96  *
97  * See i2c_transfer().
98  */
99 int i2c_rtio_transfer(struct i2c_rtio *ctx, struct i2c_msg *msgs, uint8_t num_msgs, uint16_t addr);
100 
101 /**
102  * @brief Perform an I2C bus recovery in a blocking call
103  *
104  * Provides a compatible API for the existing i2c_recover API, and blocks the caller
105  * until the process completes.
106  *
107  * See i2c_recover().
108  */
109 int i2c_rtio_recover(struct i2c_rtio *ctx);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* ZEPHYR_DRVIERS_I2C_RTIO_H_ */
116