1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "zephyr/rtio/rtio.h"
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/rtio/rtio_spsc.h>
10 #include <zephyr/sys/__assert.h>
11 
12 const struct rtio_iodev_api i2c_iodev_api = {
13 	.submit = i2c_iodev_submit,
14 };
15 
i2c_rtio_copy(struct rtio * r,struct rtio_iodev * iodev,const struct i2c_msg * msgs,uint8_t num_msgs)16 struct rtio_sqe *i2c_rtio_copy(struct rtio *r,
17 			       struct rtio_iodev *iodev,
18 			       const struct i2c_msg *msgs,
19 			       uint8_t num_msgs)
20 {
21 	__ASSERT(num_msgs > 0, "Expecting at least one message to copy");
22 
23 	struct rtio_sqe *sqe = NULL;
24 
25 	for (uint8_t i = 0; i < num_msgs; i++) {
26 		sqe = rtio_sqe_acquire(r);
27 
28 		if (sqe == NULL) {
29 			rtio_spsc_drop_all(r->sq);
30 			return NULL;
31 		}
32 
33 		if (msgs[i].flags & I2C_MSG_READ) {
34 			rtio_sqe_prep_read(sqe, iodev, RTIO_PRIO_NORM,
35 					   msgs[i].buf, msgs[i].len, NULL);
36 		} else {
37 			rtio_sqe_prep_write(sqe, iodev, RTIO_PRIO_NORM,
38 					    msgs[i].buf, msgs[i].len, NULL);
39 		}
40 		sqe->flags |= RTIO_SQE_TRANSACTION;
41 		sqe->iodev_flags = ((msgs[i].flags & I2C_MSG_STOP) ? RTIO_IODEV_I2C_STOP : 0) |
42 			((msgs[i].flags & I2C_MSG_RESTART) ? RTIO_IODEV_I2C_RESTART : 0) |
43 			((msgs[i].flags & I2C_MSG_ADDR_10_BITS) ? RTIO_IODEV_I2C_10_BITS : 0);
44 	}
45 
46 	sqe->flags &= ~RTIO_SQE_TRANSACTION;
47 
48 	return sqe;
49 }
50