1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  * Copyright 2020 Google LLC
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
9 #define ZEPHYR_INCLUDE_DRIVERS_EMUL_H_
10 
11 /**
12  * @brief Emulators used to test drivers and higher-level code that uses them
13  * @defgroup io_emulators Emulator interface
14  * @ingroup testing
15  * @{
16  */
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif /* __cplusplus */
21 
22 struct emul;
23 
24 /* #includes required after forward-declaration of struct emul later defined in this header. */
25 #include <zephyr/device.h>
26 #include <zephyr/devicetree.h>
27 #include <zephyr/drivers/espi_emul.h>
28 #include <zephyr/drivers/i2c_emul.h>
29 #include <zephyr/drivers/spi_emul.h>
30 #include <zephyr/sys/iterable_sections.h>
31 
32 /**
33  * The types of supported buses.
34  */
35 enum emul_bus_type {
36 	EMUL_BUS_TYPE_I2C,
37 	EMUL_BUS_TYPE_ESPI,
38 	EMUL_BUS_TYPE_SPI,
39 };
40 
41 /**
42  * Structure uniquely identifying a device to be emulated
43  */
44 struct emul_link_for_bus {
45 	const struct device *dev;
46 };
47 
48 /** List of emulators attached to a bus */
49 struct emul_list_for_bus {
50 	/** Identifiers for children of the node */
51 	const struct emul_link_for_bus *children;
52 	/** Number of children of the node */
53 	unsigned int num_children;
54 };
55 
56 /**
57  * Standard callback for emulator initialisation providing the initialiser
58  * record and the device that calls the emulator functions.
59  *
60  * @param emul Emulator to init
61  * @param parent Parent device that is using the emulator
62  */
63 typedef int (*emul_init_t)(const struct emul *emul, const struct device *parent);
64 
65 /** An emulator instance - represents the *target* emulated device/peripheral that is
66  * interacted with through an emulated bus. Instances of emulated bus nodes (e.g. i2c_emul)
67  * and emulators (i.e. struct emul) are exactly 1..1
68  */
69 struct emul {
70 	/** function used to initialise the emulator state */
71 	emul_init_t init;
72 	/** handle to the device for which this provides low-level emulation */
73 	const struct device *dev;
74 	/** Emulator-specific configuration data */
75 	const void *cfg;
76 	/** Emulator-specific data */
77 	void *data;
78 	/** The bus type that the emulator is attached to */
79 	enum emul_bus_type bus_type;
80 	/** Pointer to the emulated bus node */
81 	union bus {
82 		struct i2c_emul *i2c;
83 		struct espi_emul *espi;
84 		struct spi_emul *spi;
85 	} bus;
86 	/** Address of the API structure exposed by the emulator instance */
87 	const void *backend_api;
88 };
89 
90 /**
91  * @brief Use the devicetree node identifier as a unique name.
92  *
93  * @param node_id A devicetree node identifier
94  */
95 #define EMUL_DT_NAME_GET(node_id) _CONCAT(__emulreg_, node_id)
96 
97 /* Get a unique identifier based on the given _dev_node_id's reg property and
98  * the bus its on. Intended for use in other internal macros when declaring {bus}_emul
99  * structs in peripheral emulators.
100  */
101 #define Z_EMUL_REG_BUS_IDENTIFIER(_dev_node_id) (_CONCAT(_CONCAT(__emulreg_, _dev_node_id), _bus))
102 
103 /* Conditionally places text based on what bus _dev_node_id is on. */
104 #define Z_EMUL_BUS(_dev_node_id, _i2c, _espi, _spi)                                                \
105 	COND_CODE_1(DT_ON_BUS(_dev_node_id, i2c), (_i2c),                                          \
106 		    (COND_CODE_1(DT_ON_BUS(_dev_node_id, espi), (_espi),                           \
107 				 (COND_CODE_1(DT_ON_BUS(_dev_node_id, spi), (_spi), (-EINVAL))))))
108 /**
109  * @brief Define a new emulator
110  *
111  * This adds a new struct emul to the linker list of emulations. This is
112  * typically used in your emulator's DT_INST_FOREACH_STATUS_OKAY() clause.
113  *
114  * @param node_id Node ID of the driver to emulate (e.g. DT_DRV_INST(n)); the node_id *MUST* have a
115  *        corresponding DEVICE_DT_DEFINE().
116  * @param init_fn function to call to initialise the emulator (see emul_init typedef)
117  * @param data_ptr emulator-specific data
118  * @param cfg_ptr emulator-specific configuration data
119  * @param bus_api emulator-specific bus api
120  * @param _backend_api emulator-specific backend api
121  */
122 #define EMUL_DT_DEFINE(node_id, init_fn, data_ptr, cfg_ptr, bus_api, _backend_api)                 \
123 	static struct Z_EMUL_BUS(node_id, i2c_emul, espi_emul, spi_emul)                           \
124 		Z_EMUL_REG_BUS_IDENTIFIER(node_id) = {                                             \
125 			.api = bus_api,                                                            \
126 			.Z_EMUL_BUS(node_id, addr, chipsel, chipsel) = DT_REG_ADDR(node_id),       \
127 	};                                                                                         \
128 	const STRUCT_SECTION_ITERABLE(emul, EMUL_DT_NAME_GET(node_id))                             \
129 	__used = {                                                                                 \
130 		.init = (init_fn),                                                                 \
131 		.dev = DEVICE_DT_GET(node_id),                                                     \
132 		.cfg = (cfg_ptr),                                                                  \
133 		.data = (data_ptr),                                                                \
134 		.bus_type = Z_EMUL_BUS(node_id, EMUL_BUS_TYPE_I2C, EMUL_BUS_TYPE_ESPI,             \
135 				       EMUL_BUS_TYPE_SPI),                                         \
136 		.bus = {.Z_EMUL_BUS(node_id, i2c, espi, spi) =                                     \
137 				&(Z_EMUL_REG_BUS_IDENTIFIER(node_id))},                            \
138 		.backend_api = (_backend_api),                                                     \
139 	};
140 
141 /**
142  * @brief Like EMUL_DT_DEFINE(), but uses an instance of a DT_DRV_COMPAT compatible instead of a
143  *        node identifier.
144  *
145  * @param inst instance number. The @p node_id argument to EMUL_DT_DEFINE is set to
146  *             <tt>DT_DRV_INST(inst)</tt>.
147  * @param ... other parameters as expected by EMUL_DT_DEFINE.
148  */
149 #define EMUL_DT_INST_DEFINE(inst, ...) EMUL_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
150 
151 /**
152  * @brief Get a <tt>const struct emul*</tt> from a devicetree node identifier
153  *
154  * @details Returns a pointer to an emulator object created from a devicetree
155  * node, if any device was allocated by an emulator implementation.
156  *
157  * If no such device was allocated, this will fail at linker time. If you get an
158  * error that looks like <tt>undefined reference to __device_dts_ord_<N></tt>,
159  * that is what happened. Check to make sure your emulator implementation is
160  * being compiled, usually by enabling the Kconfig options it requires.
161  *
162  * @param node_id A devicetree node identifier
163  * @return A pointer to the emul object created for that node
164  */
165 #define EMUL_DT_GET(node_id) (&EMUL_DT_NAME_GET(node_id))
166 
167 /**
168  * @brief Set up a list of emulators
169  *
170  * @param dev Device the emulators are attached to (e.g. an I2C controller)
171  * @return 0 if OK
172  * @return negative value on error
173  */
174 int emul_init_for_bus(const struct device *dev);
175 
176 /**
177  * @brief Retrieve the emul structure for an emulator by name
178  *
179  * @details Emulator objects are created via the EMUL_DT_DEFINE() macro and placed in memory by the
180  * linker. If the emulator structure is needed for custom API calls, it can be retrieved by the name
181  * that the emulator exposes to the system (this is the devicetree node's label by default).
182  *
183  * @param name Emulator name to search for.  A null pointer, or a pointer to an
184  * empty string, will cause NULL to be returned.
185  *
186  * @return pointer to emulator structure; NULL if not found or cannot be used.
187  */
188 const struct emul *emul_get_binding(const char *name);
189 
190 /**
191  * @}
192  */
193 
194 #if defined(CONFIG_HAS_DTS) || defined(__DOXYGEN__)
195 #define Z_MAYBE_EMUL_DECLARE_INTERNAL(node_id) extern const struct emul EMUL_DT_NAME_GET(node_id);
196 
197 DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL);
198 #endif /* CONFIG_HAS_DTS || __DOXYGEN__ */
199 
200 #ifdef __cplusplus
201 }
202 #endif /* __cplusplus */
203 
204 #endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_H_ */
205