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_emul.h>
21 
22 /** Working data for the device */
23 struct spi_emul_data {
24 	/* List of struct spi_emul associated with the device */
25 	sys_slist_t emuls;
26 	/* SPI host configuration */
27 	uint32_t config;
28 };
29 
spi_emul_get_config(const struct device * dev)30 uint32_t spi_emul_get_config(const struct device *dev)
31 {
32 	struct spi_emul_data *data = dev->data;
33 
34 	return data->config;
35 }
36 
37 /**
38  * Find an emulator for a SPI bus
39  *
40  * At present only a single emulator is supported on the bus, since we do not
41  * support chip selects, despite there being a chipsel field. It cannot be
42  * implemented until we have a GPIO emulator.
43  *
44  * @param dev SPI emulation controller device
45  * @param chipsel Chip-select value
46  * @return emulator to use
47  * @return NULL if not found
48  */
spi_emul_find(const struct device * dev,unsigned int chipsel)49 static struct spi_emul *spi_emul_find(const struct device *dev, unsigned int chipsel)
50 {
51 	struct spi_emul_data *data = dev->data;
52 	sys_snode_t *node;
53 
54 	SYS_SLIST_FOR_EACH_NODE(&data->emuls, node) {
55 		struct spi_emul *emul;
56 
57 		emul = CONTAINER_OF(node, struct spi_emul, node);
58 		if (emul->chipsel == chipsel) {
59 			return emul;
60 		}
61 	}
62 
63 	return NULL;
64 }
65 
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)66 static int spi_emul_io(const struct device *dev, const struct spi_config *config,
67 		       const struct spi_buf_set *tx_bufs, const struct spi_buf_set *rx_bufs)
68 {
69 	struct spi_emul *emul;
70 	const struct spi_emul_api *api;
71 
72 	emul = spi_emul_find(dev, config->slave);
73 	if (!emul) {
74 		return -EIO;
75 	}
76 
77 	api = emul->api;
78 	__ASSERT_NO_MSG(emul->api);
79 	__ASSERT_NO_MSG(emul->api->io);
80 
81 	return api->io(emul->target, config, tx_bufs, rx_bufs);
82 }
83 
84 /**
85  * Set up a new emulator and add it to the list
86  *
87  * @param dev SPI emulation controller device
88  */
spi_emul_init(const struct device * dev)89 static int spi_emul_init(const struct device *dev)
90 {
91 	struct spi_emul_data *data = dev->data;
92 
93 	sys_slist_init(&data->emuls);
94 
95 	return emul_init_for_bus(dev);
96 }
97 
spi_emul_register(const struct device * dev,struct spi_emul * emul)98 int spi_emul_register(const struct device *dev, struct spi_emul *emul)
99 {
100 	struct spi_emul_data *data = dev->data;
101 	const char *name = emul->target->dev->name;
102 
103 	sys_slist_append(&data->emuls, &emul->node);
104 
105 	LOG_INF("Register emulator '%s' at cs %u\n", name, emul->chipsel);
106 
107 	return 0;
108 }
109 
110 /* Device instantiation */
111 
112 static struct spi_driver_api spi_emul_api = {
113 	.transceive = spi_emul_io,
114 };
115 
116 #define EMUL_LINK_AND_COMMA(node_id)                                                               \
117 	{                                                                                          \
118 		.dev = DEVICE_DT_GET(node_id),                                                     \
119 	},
120 
121 #define SPI_EMUL_INIT(n)                                                                           \
122 	static const struct emul_link_for_bus emuls_##n[] = { DT_FOREACH_CHILD(                    \
123 		DT_DRV_INST(0), EMUL_LINK_AND_COMMA) };                                            \
124 	static struct emul_list_for_bus spi_emul_cfg_##n = {                                       \
125 		.children = emuls_##n,                                                             \
126 		.num_children = ARRAY_SIZE(emuls_##n),                                             \
127 	};                                                                                         \
128 	static struct spi_emul_data spi_emul_data_##n;                                             \
129 	DEVICE_DT_INST_DEFINE(n, spi_emul_init, NULL, &spi_emul_data_##n, &spi_emul_cfg_##n,       \
130 			      POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, &spi_emul_api);
131 
132 DT_INST_FOREACH_STATUS_OKAY(SPI_EMUL_INIT)
133