1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #define DT_DRV_COMPAT nordic_nrf_vevif_task_tx
7 
8 #include <zephyr/devicetree.h>
9 #include <zephyr/drivers/mbox.h>
10 
11 #include <haly/nrfy_vpr.h>
12 
13 #define TASKS_IDX_MAX NRF_VPR_TASKS_TRIGGER_MAX
14 
15 struct mbox_vevif_task_tx_conf {
16 	NRF_VPR_Type *vpr;
17 	uint32_t tasks_mask;
18 	uint8_t tasks;
19 };
20 
vevif_task_tx_is_valid(const struct device * dev,uint32_t id)21 static inline bool vevif_task_tx_is_valid(const struct device *dev, uint32_t id)
22 {
23 	const struct mbox_vevif_task_tx_conf *config = dev->config;
24 
25 	return ((id <= TASKS_IDX_MAX) && ((config->tasks_mask & BIT(id)) != 0U));
26 }
27 
vevif_task_tx_send(const struct device * dev,uint32_t id,const struct mbox_msg * msg)28 static int vevif_task_tx_send(const struct device *dev, uint32_t id, const struct mbox_msg *msg)
29 {
30 	const struct mbox_vevif_task_tx_conf *config = dev->config;
31 
32 	if (!vevif_task_tx_is_valid(dev, id)) {
33 		return -EINVAL;
34 	}
35 
36 	if (msg != NULL) {
37 		return -EMSGSIZE;
38 	}
39 
40 	nrfy_vpr_task_trigger(config->vpr, nrfy_vpr_trigger_task_get(id));
41 
42 	return 0;
43 }
44 
vevif_task_tx_mtu_get(const struct device * dev)45 static int vevif_task_tx_mtu_get(const struct device *dev)
46 {
47 	ARG_UNUSED(dev);
48 
49 	return 0;
50 }
51 
vevif_task_tx_max_channels_get(const struct device * dev)52 static uint32_t vevif_task_tx_max_channels_get(const struct device *dev)
53 {
54 	const struct mbox_vevif_task_tx_conf *config = dev->config;
55 
56 	return config->tasks;
57 }
58 
59 static DEVICE_API(mbox, vevif_task_tx_driver_api) = {
60 	.send = vevif_task_tx_send,
61 	.mtu_get = vevif_task_tx_mtu_get,
62 	.max_channels_get = vevif_task_tx_max_channels_get,
63 };
64 
65 #define VEVIF_TASK_TX_DEFINE(inst)                                                                 \
66 	BUILD_ASSERT(DT_INST_PROP(inst, nordic_tasks) <= VPR_TASKS_TRIGGER_MaxCount,               \
67 		     "Number of tasks exceeds maximum");                                           \
68                                                                                                    \
69 	static const struct mbox_vevif_task_tx_conf conf##inst = {                                 \
70 		.vpr = (NRF_VPR_Type *)DT_INST_REG_ADDR(inst),                                     \
71 		.tasks = DT_INST_PROP(inst, nordic_tasks),                                         \
72 		.tasks_mask = DT_INST_PROP(inst, nordic_tasks_mask),                               \
73 	};                                                                                         \
74                                                                                                    \
75 	DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &conf##inst, POST_KERNEL,                    \
76 			      CONFIG_MBOX_INIT_PRIORITY, &vevif_task_tx_driver_api);
77 
78 DT_INST_FOREACH_STATUS_OKAY(VEVIF_TASK_TX_DEFINE)
79