1 /*
2  * Copyright 2020 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * This driver creates fake SPI buses which can contain emulated devices,
7  * implemented by a separate emulation driver. The API between this driver and
8  * its emulators is defined by struct spi_emul_driver_api.
9  */
10 
11 #define DT_DRV_COMPAT zephyr_spi_emul_controller
12 
13 #define LOG_LEVEL CONFIG_SPI_LOG_LEVEL
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(spi_emul_ctlr);
16 
17 #include <zephyr/device.h>
18 #include <zephyr/drivers/emul.h>
19 #include <zephyr/drivers/spi.h>
20 #include <zephyr/drivers/spi/rtio.h>
21 #include <zephyr/drivers/spi_emul.h>
22 
23 /** Working data for the device */
24 struct spi_emul_data {
25 	/* List of struct spi_emul associated with the device */
26 	sys_slist_t emuls;
27 	/* SPI host configuration */
28 	uint32_t config;
29 };
30 
spi_emul_get_config(const struct device * dev)31 uint32_t spi_emul_get_config(const struct device *dev)
32 {
33 	struct spi_emul_data *data = dev->data;
34 
35 	return data->config;
36 }
37 
38 /**
39  * Find an emulator for a SPI bus
40  *
41  * At present only a single emulator is supported on the bus, since we do not
42  * support chip selects, despite there being a chipsel field. It cannot be
43  * implemented until we have a GPIO emulator.
44  *
45  * @param dev SPI emulation controller device
46  * @param chipsel Chip-select value
47  * @return emulator to use
48  * @return NULL if not found
49  */
spi_emul_find(const struct device * dev,unsigned int chipsel)50 static struct spi_emul *spi_emul_find(const struct device *dev, unsigned int chipsel)
51 {
52 	struct spi_emul_data *data = dev->data;
53 	sys_snode_t *node;
54 
55 	SYS_SLIST_FOR_EACH_NODE(&data->emuls, node) {
56 		struct spi_emul *emul;
57 
58 		emul = CONTAINER_OF(node, struct spi_emul, node);
59 		if (emul->chipsel == chipsel) {
60 			return emul;
61 		}
62 	}
63 
64 	return NULL;
65 }
66 
spi_emul_io(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs)67 static int spi_emul_io(const struct device *dev, const struct spi_config *config,
68 		       const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs)
69 {
70 	struct spi_emul *emul;
71 	const struct spi_emul_api *api;
72 	int ret;
73 
74 	emul = spi_emul_find(dev, config->slave);
75 	if (!emul) {
76 		return -EIO;
77 	}
78 
79 	api = emul->api;
80 	__ASSERT_NO_MSG(emul->api);
81 	__ASSERT_NO_MSG(emul->api->io);
82 
83 	if (emul->mock_api != NULL && emul->mock_api->io != NULL) {
84 		ret = emul->mock_api->io(emul->target, config, tx_bufs, rx_bufs);
85 		if (ret != -ENOSYS) {
86 			return ret;
87 		}
88 	}
89 
90 	return api->io(emul->target, config, tx_bufs, rx_bufs);
91 }
92 
93 /**
94  * @brief This is a no-op stub of the SPI API's `release` method to protect drivers under test
95  *        from hitting a segmentation fault when using SPI_LOCK_ON plus spi_release()
96  */
spi_emul_release(const struct device * dev,const struct spi_config * config)97 static int spi_emul_release(const struct device *dev, const struct spi_config *config)
98 {
99 	ARG_UNUSED(dev);
100 	ARG_UNUSED(config);
101 
102 	return 0;
103 }
104 
105 /**
106  * Set up a new emulator and add it to the list
107  *
108  * @param dev SPI emulation controller device
109  */
spi_emul_init(const struct device * dev)110 static int spi_emul_init(const struct device *dev)
111 {
112 	struct spi_emul_data *data = dev->data;
113 
114 	sys_slist_init(&data->emuls);
115 
116 	return emul_init_for_bus(dev);
117 }
118 
spi_emul_register(const struct device * dev,struct spi_emul * emul)119 int spi_emul_register(const struct device *dev, struct spi_emul *emul)
120 {
121 	struct spi_emul_data *data = dev->data;
122 	const char *name = emul->target->dev->name;
123 
124 	sys_slist_append(&data->emuls, &emul->node);
125 
126 	LOG_INF("Register emulator '%s' at cs %u\n", name, emul->chipsel);
127 
128 	return 0;
129 }
130 
131 /* Device instantiation */
132 
133 static DEVICE_API(spi, spi_emul_api) = {
134 	.transceive = spi_emul_io,
135 #ifdef CONFIG_SPI_RTIO
136 	.iodev_submit = spi_rtio_iodev_default_submit,
137 #endif
138 	.release = spi_emul_release,
139 };
140 
141 #define EMUL_LINK_AND_COMMA(node_id)                                                               \
142 	{                                                                                          \
143 		.dev = DEVICE_DT_GET(node_id),                                                     \
144 	},
145 
146 #define SPI_EMUL_INIT(n)                                                                           \
147 	static const struct emul_link_for_bus emuls_##n[] = {                                      \
148 		DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(n), EMUL_LINK_AND_COMMA)};                \
149 	static struct emul_list_for_bus spi_emul_cfg_##n = {                                       \
150 		.children = emuls_##n,                                                             \
151 		.num_children = ARRAY_SIZE(emuls_##n),                                             \
152 	};                                                                                         \
153 	static struct spi_emul_data spi_emul_data_##n;                                             \
154 	SPI_DEVICE_DT_INST_DEFINE(n, spi_emul_init, NULL, &spi_emul_data_##n, &spi_emul_cfg_##n,   \
155 				  POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, &spi_emul_api);
156 
157 DT_INST_FOREACH_STATUS_OKAY(SPI_EMUL_INIT)
158