1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /* Override __DEPRECATED_MACRO so we don't get twister failures for
8  * deprecated macros:
9  * - DT_LABEL
10  * - DT_BUS_LABEL
11  * - DT_SPI_DEV_CS_GPIOS_LABEL
12  * - DT_GPIO_LABEL
13  * - DT_GPIO_LABEL_BY_IDX
14  * - DT_INST_LABEL
15  * - DT_INST_BUS_LABEL
16  * - DT_INST_SPI_DEV_CS_GPIOS_LABEL
17  * - DT_INST_GPIO_LABEL
18  * - DT_INST_GPIO_LABEL_BY_IDX
19  */
20 #define __DEPRECATED_MACRO
21 
22 #include <zephyr/ztest.h>
23 #include <zephyr/devicetree.h>
24 #include <zephyr/device.h>
25 #include <zephyr/drivers/gpio.h>
26 #include <zephyr/drivers/mbox.h>
27 
28 #define TEST_CHILDREN	DT_PATH(test, test_children)
29 #define TEST_DEADBEEF	DT_PATH(test, gpio_deadbeef)
30 #define TEST_ABCD1234	DT_PATH(test, gpio_abcd1234)
31 #define TEST_ALIAS	DT_ALIAS(test_alias)
32 #define TEST_NODELABEL	DT_NODELABEL(test_nodelabel)
33 #define TEST_INST	DT_INST(0, vnd_gpio_device)
34 #define TEST_ARRAYS	DT_NODELABEL(test_arrays)
35 #define TEST_PH	DT_NODELABEL(test_phandles)
36 #define TEST_IRQ	DT_NODELABEL(test_irq)
37 #define TEST_TEMP	DT_NODELABEL(test_temp_sensor)
38 #define TEST_REG	DT_NODELABEL(test_reg)
39 #define TEST_VENDOR	DT_NODELABEL(test_vendor)
40 #define TEST_MODEL	DT_NODELABEL(test_vendor)
41 #define TEST_ENUM_0	DT_NODELABEL(test_enum_0)
42 
43 #define TEST_I2C DT_NODELABEL(test_i2c)
44 #define TEST_I2C_DEV DT_PATH(test, i2c_11112222, test_i2c_dev_10)
45 #define TEST_I2C_BUS DT_BUS(TEST_I2C_DEV)
46 
47 #define TEST_I2C_MUX DT_NODELABEL(test_i2c_mux)
48 #define TEST_I2C_MUX_CTLR_1 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_1)
49 #define TEST_I2C_MUX_CTLR_2 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_2)
50 #define TEST_MUXED_I2C_DEV_1 DT_NODELABEL(test_muxed_i2c_dev_1)
51 #define TEST_MUXED_I2C_DEV_2 DT_NODELABEL(test_muxed_i2c_dev_2)
52 
53 #define TEST_I3C DT_NODELABEL(test_i3c)
54 #define TEST_I3C_DEV DT_PATH(test, i3c_88889999, test_i3c_dev_420000abcd12345678)
55 #define TEST_I3C_BUS DT_BUS(TEST_I3C_DEV)
56 
57 #define TEST_GPIO_1 DT_NODELABEL(test_gpio_1)
58 #define TEST_GPIO_2 DT_NODELABEL(test_gpio_2)
59 
60 #define TEST_GPIO_HOG_1 DT_PATH(test, gpio_deadbeef, test_gpio_hog_1)
61 #define TEST_GPIO_HOG_2 DT_PATH(test, gpio_deadbeef, test_gpio_hog_2)
62 #define TEST_GPIO_HOG_3 DT_PATH(test, gpio_abcd1234, test_gpio_hog_3)
63 
64 #define TEST_SPI DT_NODELABEL(test_spi)
65 
66 #define TEST_SPI_DEV_0 DT_PATH(test, spi_33334444, test_spi_dev_0)
67 #define TEST_SPI_BUS_0 DT_BUS(TEST_SPI_DEV_0)
68 
69 #define TEST_SPI_DEV_1 DT_PATH(test, spi_33334444, test_spi_dev_1)
70 #define TEST_SPI_BUS_1 DT_BUS(TEST_SPI_DEV_1)
71 
72 #define TEST_SPI_NO_CS DT_NODELABEL(test_spi_no_cs)
73 #define TEST_SPI_DEV_NO_CS DT_NODELABEL(test_spi_no_cs)
74 
75 #define TEST_PWM_CTLR_1 DT_NODELABEL(test_pwm1)
76 #define TEST_PWM_CTLR_2 DT_NODELABEL(test_pwm2)
77 
78 #define TEST_CAN_CTRL_0 DT_NODELABEL(test_can0)
79 #define TEST_CAN_CTRL_1 DT_NODELABEL(test_can1)
80 
81 #define TEST_DMA_CTLR_1 DT_NODELABEL(test_dma1)
82 #define TEST_DMA_CTLR_2 DT_NODELABEL(test_dma2)
83 
84 #define TEST_IO_CHANNEL_CTLR_1 DT_NODELABEL(test_adc_1)
85 #define TEST_IO_CHANNEL_CTLR_2 DT_NODELABEL(test_adc_2)
86 
87 #define TEST_RANGES_PCIE  DT_NODELABEL(test_ranges_pcie)
88 #define TEST_RANGES_OTHER DT_NODELABEL(test_ranges_other)
89 
90 #define ZEPHYR_USER DT_PATH(zephyr_user)
91 
92 #define TA_HAS_COMPAT(compat) DT_NODE_HAS_COMPAT(TEST_ARRAYS, compat)
93 
94 #define TO_STRING(x) TO_STRING_(x)
95 #define TO_STRING_(x) #x
96 
ZTEST(devicetree_api,test_path_props)97 ZTEST(devicetree_api, test_path_props)
98 {
99 	zassert_true(!strcmp(DT_LABEL(TEST_DEADBEEF), "TEST_GPIO_1"), "");
100 	zassert_equal(DT_NUM_REGS(TEST_DEADBEEF), 1, "");
101 	zassert_equal(DT_REG_ADDR(TEST_DEADBEEF), 0xdeadbeef, "");
102 	zassert_equal(DT_REG_SIZE(TEST_DEADBEEF), 0x1000, "");
103 	zassert_equal(DT_PROP(TEST_DEADBEEF, gpio_controller), 1, "");
104 	zassert_equal(DT_PROP(TEST_DEADBEEF, ngpios), 32, "");
105 	zassert_true(!strcmp(DT_PROP(TEST_DEADBEEF, status), "okay"), "");
106 	zassert_equal(DT_PROP_LEN(TEST_DEADBEEF, compatible), 1, "");
107 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_DEADBEEF, compatible, 0),
108 			     "vnd,gpio-device"), "");
109 	zassert_true(DT_NODE_HAS_PROP(TEST_DEADBEEF, status), "");
110 	zassert_false(DT_NODE_HAS_PROP(TEST_DEADBEEF, foobar), "");
111 
112 	zassert_true(DT_SAME_NODE(TEST_ABCD1234, TEST_GPIO_2), "");
113 	zassert_equal(DT_NUM_REGS(TEST_ABCD1234), 2, "");
114 	zassert_equal(DT_PROP(TEST_ABCD1234, gpio_controller), 1, "");
115 	zassert_equal(DT_PROP(TEST_ABCD1234, ngpios), 32, "");
116 	zassert_true(!strcmp(DT_PROP(TEST_ABCD1234, status), "okay"), "");
117 	zassert_equal(DT_PROP_LEN(TEST_ABCD1234, compatible), 1, "");
118 	zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, compatible, 4), 1, "");
119 	zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, invalid_property, 0), 0, "");
120 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ABCD1234, compatible, 0),
121 			     "vnd,gpio-device"), "");
122 }
123 
ZTEST(devicetree_api,test_alias_props)124 ZTEST(devicetree_api, test_alias_props)
125 {
126 	zassert_equal(DT_NUM_REGS(TEST_ALIAS), 1, "");
127 	zassert_equal(DT_REG_ADDR(TEST_ALIAS), 0xdeadbeef, "");
128 	zassert_equal(DT_REG_SIZE(TEST_ALIAS), 0x1000, "");
129 	zassert_true(DT_SAME_NODE(TEST_ALIAS, TEST_GPIO_1), "");
130 	zassert_equal(DT_PROP(TEST_ALIAS, gpio_controller), 1, "");
131 	zassert_equal(DT_PROP(TEST_ALIAS, ngpios), 32, "");
132 	zassert_true(!strcmp(DT_PROP(TEST_ALIAS, status), "okay"), "");
133 	zassert_equal(DT_PROP_LEN(TEST_ALIAS, compatible), 1, "");
134 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ALIAS, compatible, 0),
135 			     "vnd,gpio-device"), "");
136 }
137 
ZTEST(devicetree_api,test_nodelabel_props)138 ZTEST(devicetree_api, test_nodelabel_props)
139 {
140 	zassert_equal(DT_NUM_REGS(TEST_NODELABEL), 1, "");
141 	zassert_equal(DT_REG_ADDR(TEST_NODELABEL), 0xdeadbeef, "");
142 	zassert_equal(DT_REG_SIZE(TEST_NODELABEL), 0x1000, "");
143 	zassert_true(!strcmp(DT_LABEL(TEST_NODELABEL), "TEST_GPIO_1"), "");
144 	zassert_equal(DT_PROP(TEST_NODELABEL, gpio_controller), 1, "");
145 	zassert_equal(DT_PROP(TEST_NODELABEL, ngpios), 32, "");
146 	zassert_true(!strcmp(DT_PROP(TEST_NODELABEL, status), "okay"), "");
147 	zassert_equal(DT_PROP_LEN(TEST_NODELABEL, compatible), 1, "");
148 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_NODELABEL, compatible, 0),
149 			     "vnd,gpio-device"), "");
150 	zassert_equal(DT_PROP_LEN(TEST_ENUM_0, val), 1, "");
151 }
152 
153 #undef DT_DRV_COMPAT
154 #define DT_DRV_COMPAT vnd_gpio_device
ZTEST(devicetree_api,test_inst_props)155 ZTEST(devicetree_api, test_inst_props)
156 {
157 	const char *label_startswith = "TEST_GPIO_";
158 
159 	/*
160 	 * Careful:
161 	 *
162 	 * We can only test properties that are shared across all
163 	 * instances of this compatible here. This includes instances
164 	 * with status "disabled".
165 	 */
166 
167 	zassert_equal(DT_PROP(TEST_INST, gpio_controller), 1, "");
168 	zassert_true(!strcmp(DT_PROP(TEST_INST, status), "okay") ||
169 		     !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
170 	zassert_equal(DT_PROP_LEN(TEST_INST, compatible), 1, "");
171 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_INST, compatible, 0),
172 			     "vnd,gpio-device"), "");
173 
174 	zassert_equal(DT_INST_NODE_HAS_PROP(0, gpio_controller), 1, "");
175 	zassert_equal(DT_INST_PROP(0, gpio_controller), 1, "");
176 	zassert_equal(DT_INST_NODE_HAS_PROP(0, xxxx), 0, "");
177 	zassert_true(!strcmp(DT_INST_PROP(0, status), "okay") ||
178 		     !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
179 	zassert_equal(DT_INST_PROP_LEN(0, compatible), 1, "");
180 	zassert_true(!strcmp(DT_INST_PROP_BY_IDX(0, compatible, 0),
181 			     "vnd,gpio-device"), "");
182 	zassert_true(!strncmp(label_startswith, DT_INST_LABEL(0),
183 			      strlen(label_startswith)), "");
184 }
185 
186 #undef DT_DRV_COMPAT
187 #define DT_DRV_COMPAT vnd_device_with_props
ZTEST(devicetree_api,test_any_inst_prop)188 ZTEST(devicetree_api, test_any_inst_prop)
189 {
190 	zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo), 1, "");
191 	zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(bar), 1, "");
192 	zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz), 0, "");
193 	zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(does_not_exist), 0, "");
194 }
195 
ZTEST(devicetree_api,test_default_prop_access)196 ZTEST(devicetree_api, test_default_prop_access)
197 {
198 	/*
199 	 * The APIs guarantee that the default_value is not expanded
200 	 * if the relevant property or cell is defined. This "X" macro
201 	 * is meant as poison which causes (hopefully) easy to
202 	 * understand build errors if this guarantee is not met due to
203 	 * a regression.
204 	 */
205 #undef X
206 #define X do.not.expand.this.argument
207 
208 	/* Node identifier variants. */
209 	zassert_equal(DT_PROP_OR(TEST_REG, misc_prop, X), 1234, "");
210 	zassert_equal(DT_PROP_OR(TEST_REG, not_a_property, -1), -1, "");
211 
212 	zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, channel, X), 3, "");
213 	zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, not_a_cell, -1), -1,
214 		      "");
215 
216 	zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, channel, X), 1, "");
217 	zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, not_a_cell, -1), -1, "");
218 
219 	zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, channel, X), 1,
220 		      "");
221 	zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, not_a_cell, -1),
222 		      -1, "");
223 
224 	/* Instance number variants. */
225 #undef DT_DRV_COMPAT
226 #define DT_DRV_COMPAT vnd_reg_holder
227 	zassert_equal(DT_INST_PROP_OR(0, misc_prop, X), 1234, "");
228 	zassert_equal(DT_INST_PROP_OR(0, not_a_property, -1), -1, "");
229 
230 #undef DT_DRV_COMPAT
231 #define DT_DRV_COMPAT vnd_array_holder
232 	zassert_equal(DT_INST_PROP_LEN_OR(0, a, X), 3, "");
233 	zassert_equal(DT_INST_PROP_LEN_OR(0, not_a_property, -1), -1, "");
234 
235 #undef DT_DRV_COMPAT
236 #define DT_DRV_COMPAT vnd_adc_temp_sensor
237 	zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, channel, X), 3, "");
238 	zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, not_a_cell, -1), -1,
239 		      "");
240 
241 	zassert_equal(DT_INST_PHA_OR(0, dmas, channel, X), 1, "");
242 	zassert_equal(DT_INST_PHA_OR(0, dmas, not_a_cell, -1), -1, "");
243 
244 	zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, channel, X), 1,
245 		      "");
246 	zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, not_a_cell, -1), -1,
247 		      "");
248 
249 #undef X
250 }
251 
ZTEST(devicetree_api,test_has_path)252 ZTEST(devicetree_api, test_has_path)
253 {
254 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_0), okay), 0, "");
255 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_deadbeef), okay), 1,
256 		      "");
257 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_abcd1234), okay), 1,
258 		      "");
259 }
260 
ZTEST(devicetree_api,test_has_alias)261 ZTEST(devicetree_api, test_has_alias)
262 {
263 	zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_alias), okay), 1, "");
264 	zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_undef), okay), 0, "");
265 }
266 
ZTEST(devicetree_api,test_inst_checks)267 ZTEST(devicetree_api, test_inst_checks)
268 {
269 	zassert_equal(DT_NODE_EXISTS(DT_INST(0, vnd_gpio_device)), 1, "");
270 	zassert_equal(DT_NODE_EXISTS(DT_INST(1, vnd_gpio_device)), 1, "");
271 	zassert_equal(DT_NODE_EXISTS(DT_INST(2, vnd_gpio_device)), 1, "");
272 
273 	zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio_device), 2, "");
274 	zassert_equal(DT_NUM_INST_STATUS_OKAY(xxxx), 0, "");
275 }
276 
ZTEST(devicetree_api,test_has_nodelabel)277 ZTEST(devicetree_api, test_has_nodelabel)
278 {
279 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay), 0,
280 		      "");
281 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel), okay), 1,
282 		      "");
283 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel_allcaps),
284 					 okay),
285 		      1, "");
286 }
287 
ZTEST(devicetree_api,test_has_compat)288 ZTEST(devicetree_api, test_has_compat)
289 {
290 	unsigned int compats;
291 
292 	zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio_device), "");
293 	zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio_device), "");
294 	zassert_false(DT_HAS_COMPAT_STATUS_OKAY(vnd_disabled_compat), "");
295 
296 	zassert_equal(TA_HAS_COMPAT(vnd_array_holder), 1, "");
297 	zassert_equal(TA_HAS_COMPAT(vnd_undefined_compat), 1, "");
298 	zassert_equal(TA_HAS_COMPAT(vnd_not_a_test_array_compat), 0, "");
299 	compats = ((TA_HAS_COMPAT(vnd_array_holder) << 0) |
300 		   (TA_HAS_COMPAT(vnd_undefined_compat) << 1) |
301 		   (TA_HAS_COMPAT(vnd_not_a_test_array_compat) << 2));
302 	zassert_equal(compats, 0x3, "");
303 }
304 
ZTEST(devicetree_api,test_has_status)305 ZTEST(devicetree_api, test_has_status)
306 {
307 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), okay),
308 		      1, "");
309 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), disabled),
310 		      0, "");
311 
312 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), okay),
313 		      1, "");
314 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), disabled),
315 		      0, "");
316 
317 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), disabled),
318 		      1, "");
319 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay),
320 		      0, "");
321 }
322 
ZTEST(devicetree_api,test_bus)323 ZTEST(devicetree_api, test_bus)
324 {
325 	/* common prefixes of expected labels: */
326 	const char *i2c_bus = "TEST_I2C_CTLR";
327 	const char *i2c_dev = "TEST_I2C_DEV";
328 	const char *i3c_bus = "TEST_I3C_CTLR";
329 	const char *i3c_dev = "TEST_I3C_DEV";
330 	const char *i3c_i2c_bus = "TEST_I3C_CTLR";
331 	const char *i3c_i2c_dev = "TEST_I3C_I2C_DEV";
332 	const char *spi_bus = "TEST_SPI_CTLR";
333 	const char *spi_dev = "TEST_SPI_DEV";
334 	const char *gpio = "TEST_GPIO_";
335 	int pin, flags;
336 
337 	zassert_true(DT_SAME_NODE(TEST_I3C_BUS, TEST_I3C), "");
338 	zassert_true(DT_SAME_NODE(TEST_I2C_BUS, TEST_I2C), "");
339 	zassert_true(DT_SAME_NODE(TEST_SPI_BUS_0, TEST_SPI), "");
340 	zassert_true(DT_SAME_NODE(TEST_SPI_BUS_1, TEST_SPI), "");
341 
342 	zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_0), 1, "");
343 	zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_NO_CS), 0, "");
344 
345 	/* Test a nested I2C bus using vnd,i2c-mux. */
346 	zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_1,
347 				  DT_BUS(TEST_MUXED_I2C_DEV_1)), "");
348 	zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_2,
349 				  DT_BUS(TEST_MUXED_I2C_DEV_2)), "");
350 
351 #undef DT_DRV_COMPAT
352 #define DT_DRV_COMPAT vnd_spi_device_2
353 	/* there is only one instance, and it has no CS */
354 	zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 0, "");
355 	/* since there's only one instance, we also know its bus. */
356 	zassert_true(DT_SAME_NODE(TEST_SPI_NO_CS, DT_INST_BUS(0)),
357 		     "expected TEST_SPI_NO_CS as bus for vnd,spi-device-2");
358 
359 #undef DT_DRV_COMPAT
360 #define DT_DRV_COMPAT vnd_spi_device
361 	/*
362 	 * DT_INST_SPI_DEV: use with care here. We could be matching
363 	 * either vnd,spi-device.
364 	 */
365 	zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 1, "");
366 
367 #define CTLR_NODE DT_INST_SPI_DEV_CS_GPIOS_CTLR(0)
368 	zassert_true(DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_1)) ||
369 		     DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_2)), "");
370 #undef CTLR_NODE
371 
372 	zassert_true(!strncmp(gpio, DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
373 			      strlen(gpio)), "");
374 
375 	pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
376 	zassert_true((pin == 0x10) || (pin == 0x30), "");
377 
378 	flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
379 	zassert_true((flags == 0x20) || (flags == 0x40), "");
380 
381 	zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, spi), 1, "");
382 	zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, i2c), 0, "");
383 	zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, i3c), 0, "");
384 
385 	zassert_equal(DT_ON_BUS(TEST_I2C_DEV, i2c), 1, "");
386 	zassert_equal(DT_ON_BUS(TEST_I2C_DEV, i3c), 0, "");
387 	zassert_equal(DT_ON_BUS(TEST_I2C_DEV, spi), 0, "");
388 
389 	zassert_true(!strcmp(DT_BUS_LABEL(TEST_I2C_DEV), "TEST_I2C_CTLR"), "");
390 
391 #undef DT_DRV_COMPAT
392 #define DT_DRV_COMPAT vnd_spi_device
393 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
394 
395 	zassert_equal(DT_INST_ON_BUS(0, spi), 1, "");
396 	zassert_equal(DT_INST_ON_BUS(0, i2c), 0, "");
397 	zassert_equal(DT_INST_ON_BUS(0, i3c), 0, "");
398 
399 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 1, "");
400 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 0, "");
401 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 0, "");
402 
403 	zassert_true(!strncmp(spi_dev, DT_INST_LABEL(0), strlen(spi_dev)), "");
404 	zassert_true(!strncmp(spi_bus, DT_INST_BUS_LABEL(0), strlen(spi_bus)),
405 		     "");
406 
407 #undef DT_DRV_COMPAT
408 #define DT_DRV_COMPAT vnd_i2c_device
409 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
410 
411 	zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
412 	zassert_equal(DT_INST_ON_BUS(0, i3c), 0, "");
413 	zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
414 
415 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
416 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 0, "");
417 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
418 
419 	zassert_true(!strncmp(i2c_dev, DT_INST_LABEL(0), strlen(i2c_dev)), "");
420 	zassert_true(!strncmp(i2c_bus, DT_INST_BUS_LABEL(0), strlen(i2c_bus)),
421 		     "");
422 
423 #undef DT_DRV_COMPAT
424 #define DT_DRV_COMPAT vnd_i3c_device
425 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
426 
427 	zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
428 	zassert_equal(DT_INST_ON_BUS(0, i3c), 1, "");
429 	zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
430 
431 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
432 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 1, "");
433 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
434 
435 	zassert_true(!strncmp(i3c_dev, DT_INST_LABEL(0), strlen(i3c_dev)), "");
436 	zassert_true(!strncmp(i3c_bus, DT_INST_BUS_LABEL(0), strlen(i3c_bus)),
437 		     "");
438 
439 #undef DT_DRV_COMPAT
440 #define DT_DRV_COMPAT vnd_i3c_i2c_device
441 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
442 
443 	zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
444 	zassert_equal(DT_INST_ON_BUS(0, i3c), 1, "");
445 	zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
446 
447 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
448 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 1, "");
449 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
450 
451 	zassert_true(!strncmp(i3c_i2c_dev, DT_INST_LABEL(0), strlen(i3c_i2c_dev)), "");
452 	zassert_true(!strncmp(i3c_i2c_bus, DT_INST_BUS_LABEL(0), strlen(i3c_i2c_bus)),
453 		     "");
454 
455 #undef DT_DRV_COMPAT
456 
457 	/*
458 	 * Make sure the underlying DT_COMPAT_ON_BUS_INTERNAL used by
459 	 * DT_ANY_INST_ON_BUS works without DT_DRV_COMPAT defined.
460 	 */
461 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_spi_device, spi), 1);
462 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_spi_device, i2c), 0);
463 
464 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_i2c_device, i2c), 1);
465 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_i2c_device, spi), 0);
466 
467 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_gpio_expander, i2c), 1,
468 		      NULL);
469 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_gpio_expander, spi), 1,
470 		      NULL);
471 }
472 
473 #undef DT_DRV_COMPAT
474 #define DT_DRV_COMPAT vnd_vendor
475 
476 #define VND_VENDOR "A stand-in for a real vendor which can be used in examples and tests"
477 #define ZEP_VENDOR "Zephyr-specific binding"
478 
ZTEST(devicetree_api,test_vendor)479 ZTEST(devicetree_api, test_vendor)
480 {
481 	/* DT_NODE_VENDOR_HAS_IDX */
482 	zassert_true(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 0), "");
483 	zassert_false(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 1), "");
484 	zassert_true(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 2), "");
485 	zassert_false(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 3), "");
486 
487 	/* DT_NODE_VENDOR_BY_IDX */
488 	zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX(TEST_VENDOR, 0), VND_VENDOR), "");
489 	zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX(TEST_VENDOR, 2), ZEP_VENDOR), "");
490 
491 	/* DT_NODE_VENDOR_BY_IDX_OR */
492 	zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 0, NULL), VND_VENDOR), "");
493 	zassert_is_null(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 1, NULL), "");
494 	zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 2, NULL), ZEP_VENDOR), "");
495 	zassert_is_null(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 3, NULL), "");
496 
497 	/* DT_NODE_VENDOR_OR */
498 	zassert_true(!strcmp(DT_NODE_VENDOR_OR(TEST_VENDOR, NULL), VND_VENDOR), "");
499 }
500 
501 #define VND_MODEL "model1"
502 #define ZEP_MODEL "model2"
503 
ZTEST(devicetree_api,test_model)504 ZTEST(devicetree_api, test_model)
505 {
506 	/* DT_NODE_MODEL_HAS_IDX */
507 	zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 0), "");
508 	zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 1), "");
509 	zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 2), "");
510 	zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 3), "");
511 
512 	/* DT_NODE_MODEL_BY_IDX */
513 	zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 0), VND_MODEL), "");
514 	zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 2), ZEP_MODEL), "");
515 
516 	/* DT_NODE_MODEL_BY_IDX_OR */
517 	zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 0, NULL), VND_MODEL), "");
518 	zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 1, NULL), "");
519 	zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 2, NULL), ZEP_MODEL), "");
520 	zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 3, NULL), "");
521 
522 	/* DT_NODE_MODEL_OR */
523 	zassert_true(!strcmp(DT_NODE_MODEL_OR(TEST_MODEL, NULL), VND_MODEL), "");
524 }
525 
526 #undef ZEP_MODEL
527 #undef VND_MODEL
528 
529 #undef DT_DRV_COMPAT
530 #define DT_DRV_COMPAT vnd_reg_holder
ZTEST(devicetree_api,test_reg)531 ZTEST(devicetree_api, test_reg)
532 {
533 	/* DT_REG_HAS_IDX */
534 	zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 0), "");
535 	zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 1), "");
536 	zassert_false(DT_REG_HAS_IDX(TEST_ABCD1234, 2), "");
537 
538 	/* DT_REG_ADDR_BY_IDX */
539 	zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 0), 0xabcd1234, "");
540 	zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 1), 0x98765432, "");
541 
542 	/* DT_REG_SIZE_BY_IDX */
543 	zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 0), 0x500, "");
544 	zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 1), 0xff, "");
545 
546 	/* DT_REG_ADDR */
547 	zassert_equal(DT_REG_ADDR(TEST_ABCD1234), 0xabcd1234, "");
548 
549 	/* DT_REG_SIZE */
550 	zassert_equal(DT_REG_SIZE(TEST_ABCD1234), 0x500, "");
551 
552 	/* DT_REG_ADDR_BY_NAME */
553 	zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, one), 0xabcd1234, "");
554 	zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, two), 0x98765432, "");
555 
556 	/* DT_REG_SIZE_BY_NAME */
557 	zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, one), 0x500, "");
558 	zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, two), 0xff, "");
559 
560 	/* DT_INST */
561 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
562 
563 	/* DT_INST_REG_HAS_IDX */
564 	zassert_true(DT_INST_REG_HAS_IDX(0, 0), "");
565 	zassert_true(DT_INST_REG_HAS_IDX(0, 1), "");
566 	zassert_false(DT_INST_REG_HAS_IDX(0, 2), "");
567 
568 	/* DT_INST_REG_ADDR_BY_IDX */
569 	zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 0), 0x9999aaaa, "");
570 	zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 1), 0xbbbbcccc, "");
571 
572 	/* DT_INST_REG_SIZE_BY_IDX */
573 	zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 0), 0x1000, "");
574 	zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 1), 0x3f, "");
575 
576 	/* DT_INST_REG_ADDR */
577 	zassert_equal(DT_INST_REG_ADDR(0), 0x9999aaaa, "");
578 
579 	/* DT_INST_REG_SIZE */
580 	zassert_equal(DT_INST_REG_SIZE(0), 0x1000, "");
581 
582 	/* DT_INST_REG_ADDR_BY_NAME */
583 	zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, first), 0x9999aaaa, "");
584 	zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, second), 0xbbbbcccc, "");
585 
586 	/* DT_INST_REG_SIZE_BY_NAME */
587 	zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, first), 0x1000, "");
588 	zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, second), 0x3f, "");
589 }
590 
591 #undef DT_DRV_COMPAT
592 #define DT_DRV_COMPAT vnd_interrupt_holder
ZTEST(devicetree_api,test_irq)593 ZTEST(devicetree_api, test_irq)
594 {
595 	/* DT_NUM_IRQS */
596 	zassert_equal(DT_NUM_IRQS(TEST_DEADBEEF), 1, "");
597 	zassert_equal(DT_NUM_IRQS(TEST_I2C_BUS), 2, "");
598 	zassert_equal(DT_NUM_IRQS(TEST_SPI), 3, "");
599 
600 	/* DT_IRQ_HAS_IDX */
601 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 0), "");
602 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 1), "");
603 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 2), "");
604 	zassert_false(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 3), "");
605 
606 	zassert_true(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 0), "");
607 	zassert_false(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 1), "");
608 
609 	zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 0), "");
610 	zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 1), "");
611 	zassert_false(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 2), "");
612 
613 	/* DT_IRQ_BY_IDX */
614 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, irq), 8, "");
615 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, irq), 9, "");
616 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, irq), 10, "");
617 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, priority), 3, "");
618 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, priority), 0, "");
619 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, priority), 1, "");
620 
621 	/* DT_IRQ_BY_NAME */
622 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, irq), 6, "");
623 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, irq), 7, "");
624 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, priority), 2, "");
625 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, priority), 1, "");
626 
627 	/* DT_IRQ_HAS_CELL_AT_IDX */
628 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, irq), "");
629 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, priority), "");
630 	zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, foo), 0, "");
631 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, irq), "");
632 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, priority), "");
633 	zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, foo), "");
634 
635 	/* DT_IRQ_HAS_CELL */
636 	zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, irq), "");
637 	zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, priority), "");
638 	zassert_false(DT_IRQ_HAS_CELL(TEST_IRQ, foo), "");
639 
640 	/* DT_IRQ_HAS_NAME */
641 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, err), "");
642 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, stat), "");
643 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, done), "");
644 	zassert_false(DT_IRQ_HAS_NAME(TEST_IRQ, alpha), "");
645 
646 	/* DT_IRQ */
647 	zassert_equal(DT_IRQ(TEST_I2C_BUS, irq), 6, "");
648 	zassert_equal(DT_IRQ(TEST_I2C_BUS, priority), 2, "");
649 
650 	/* DT_IRQN */
651 	zassert_equal(DT_IRQN(TEST_I2C_BUS), 6, "");
652 
653 	/* DT_INST */
654 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
655 
656 	/* DT_INST_IRQ_HAS_IDX */
657 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 0), 1, "");
658 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 1), 1, "");
659 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 2), 1, "");
660 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 3), 0, "");
661 
662 	/* DT_INST_IRQ_BY_IDX */
663 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, irq), 30, "");
664 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, irq), 40, "");
665 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, irq), 60, "");
666 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, priority), 3, "");
667 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, priority), 5, "");
668 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, priority), 7, "");
669 
670 	/* DT_INST_IRQ_BY_NAME */
671 	zassert_equal(DT_INST_IRQ_BY_NAME(0, err, irq), 30, "");
672 	zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, irq), 40, "");
673 	zassert_equal(DT_INST_IRQ_BY_NAME(0, done, irq), 60, "");
674 	zassert_equal(DT_INST_IRQ_BY_NAME(0, err, priority), 3, "");
675 	zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, priority), 5, "");
676 	zassert_equal(DT_INST_IRQ_BY_NAME(0, done, priority), 7, "");
677 
678 	/* DT_INST_IRQ */
679 	zassert_equal(DT_INST_IRQ(0, irq), 30, "");
680 	zassert_equal(DT_INST_IRQ(0, priority), 3, "");
681 
682 	/* DT_INST_IRQN */
683 	zassert_equal(DT_INST_IRQN(0), 30, "");
684 
685 	/* DT_INST_IRQ_HAS_CELL_AT_IDX */
686 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, irq), "");
687 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, priority), "");
688 	zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, foo), "");
689 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, irq), "");
690 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, priority), "");
691 	zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, foo), "");
692 
693 	/* DT_INST_IRQ_HAS_CELL */
694 	zassert_true(DT_INST_IRQ_HAS_CELL(0, irq), "");
695 	zassert_true(DT_INST_IRQ_HAS_CELL(0, priority), "");
696 	zassert_false(DT_INST_IRQ_HAS_CELL(0, foo), "");
697 
698 	/* DT_INST_IRQ_HAS_NAME */
699 	zassert_true(DT_INST_IRQ_HAS_NAME(0, err), "");
700 	zassert_true(DT_INST_IRQ_HAS_NAME(0, stat), "");
701 	zassert_true(DT_INST_IRQ_HAS_NAME(0, done), "");
702 	zassert_false(DT_INST_IRQ_HAS_NAME(0, alpha), "");
703 }
704 
705 struct gpios_struct {
706 	const char *name;
707 	gpio_pin_t pin;
708 	gpio_flags_t flags;
709 };
710 
711 /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
712 #define DT_PROP_ELEM_BY_PHANDLE(idx, node_id, ph_prop, prop) \
713 	DT_PROP_BY_PHANDLE_IDX(node_id, ph_prop, idx, prop)
714 #define DT_PHANDLE_LISTIFY(node_id, ph_prop, prop) \
715 	{ \
716 	  LISTIFY(DT_PROP_LEN(node_id, ph_prop), \
717 		  DT_PROP_ELEM_BY_PHANDLE, (,), \
718 		  node_id, \
719 		  ph_prop, \
720 		  label) \
721 	}
722 
723 /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
724 #define DT_GPIO_ELEM(idx, node_id, prop) \
725 	{ \
726 		DT_PROP(DT_PHANDLE_BY_IDX(node_id, prop, idx), label), \
727 		DT_PHA_BY_IDX(node_id, prop, idx, pin),\
728 		DT_PHA_BY_IDX(node_id, prop, idx, flags),\
729 	}
730 #define DT_GPIO_LISTIFY(node_id, prop) \
731 	{ LISTIFY(DT_PROP_LEN(node_id, prop), DT_GPIO_ELEM, (,), \
732 		  node_id, prop) }
733 
734 #undef DT_DRV_COMPAT
735 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_phandles)736 ZTEST(devicetree_api, test_phandles)
737 {
738 	const char *ph_label = DT_PROP_BY_PHANDLE(TEST_PH, ph, label);
739 	const char *phs_labels[] = DT_PHANDLE_LISTIFY(TEST_PH, phs, label);
740 	struct gpios_struct gps[] = DT_GPIO_LISTIFY(TEST_PH, gpios);
741 
742 	/* phandle */
743 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, ph), "");
744 	zassert_true(DT_SAME_NODE(DT_PROP(TEST_PH, ph),
745 				  DT_NODELABEL(test_gpio_1)), "");
746 	zassert_equal(DT_PROP_LEN(TEST_PH, ph), 1, "");
747 	zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, ph, 0),
748 				  DT_NODELABEL(test_gpio_1)), "");
749 	/* DT_PROP_BY_PHANDLE */
750 	zassert_true(!strcmp(ph_label, "TEST_GPIO_1"), "");
751 
752 	/* phandles */
753 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, phs), "");
754 	zassert_equal(ARRAY_SIZE(phs_labels), 3, "");
755 	zassert_equal(DT_PROP_LEN(TEST_PH, phs), 3, "");
756 	zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, phs, 1),
757 				  DT_NODELABEL(test_gpio_2)), "");
758 
759 	/* DT_PROP_BY_PHANDLE_IDX */
760 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 0, label),
761 			     "TEST_GPIO_1"), "");
762 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 1, label),
763 			     "TEST_GPIO_2"), "");
764 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 2, label),
765 			     "TEST_I2C_CTLR"), "");
766 	zassert_true(!strcmp(phs_labels[0], "TEST_GPIO_1"), "");
767 	zassert_true(!strcmp(phs_labels[1], "TEST_GPIO_2"), "");
768 	zassert_true(!strcmp(phs_labels[2], "TEST_I2C_CTLR"), "");
769 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0,
770 					     gpio_controller), 1, "");
771 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1,
772 					     gpio_controller), 1, "");
773 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0, label),
774 			     "TEST_GPIO_1"), "");
775 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1, label),
776 			     "TEST_GPIO_2"), "");
777 
778 	/* DT_PROP_BY_PHANDLE_IDX_OR */
779 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 0,
780 						val, "zero"), "one"), "");
781 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 1,
782 						val, "zero"), "zero"), "");
783 
784 	/* phandle-array */
785 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, gpios), "");
786 	zassert_equal(ARRAY_SIZE(gps), 2, "");
787 	zassert_equal(DT_PROP_LEN(TEST_PH, gpios), 2, "");
788 
789 	/* DT_PROP_HAS_IDX */
790 	zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 0), "");
791 	zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 1), "");
792 	zassert_false(DT_PROP_HAS_IDX(TEST_PH, gpios, 2), "");
793 
794 	/* DT_PROP_HAS_NAME */
795 	zassert_false(DT_PROP_HAS_NAME(TEST_PH, foos, A), "");
796 	zassert_true(DT_PROP_HAS_NAME(TEST_PH, foos, a), "");
797 	zassert_false(DT_PROP_HAS_NAME(TEST_PH, foos, b-c), "");
798 	zassert_true(DT_PROP_HAS_NAME(TEST_PH, foos, b_c), "");
799 	zassert_false(DT_PROP_HAS_NAME(TEST_PH, bazs, jane), "");
800 
801 	/* DT_PHA_HAS_CELL_AT_IDX */
802 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, pin), "");
803 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, flags), "");
804 	/* pha-gpios index 1 has nothing, not even a phandle */
805 	zassert_false(DT_PROP_HAS_IDX(TEST_PH, pha_gpios, 1), "");
806 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, pin), "");
807 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, flags),
808 		      "");
809 	/* index 2 only has a pin cell, no flags */
810 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, pin), "");
811 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, flags),
812 		      "");
813 	/* index 3 has both pin and flags cells*/
814 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, pin), "");
815 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, flags), "");
816 	/* even though index 1 has nothing, the length is still 4 */
817 	zassert_equal(DT_PROP_LEN(TEST_PH, pha_gpios), 4, "");
818 
819 	/* DT_PHA_HAS_CELL */
820 	zassert_true(DT_PHA_HAS_CELL(TEST_PH, gpios, flags), "");
821 	zassert_false(DT_PHA_HAS_CELL(TEST_PH, gpios, bar), "");
822 
823 	/* DT_PHANDLE_BY_IDX */
824 	zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 0), TEST_GPIO_1), "");
825 	zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 1), TEST_GPIO_2), "");
826 
827 	/* DT_PHANDLE */
828 	zassert_true(DT_SAME_NODE(DT_PHANDLE(TEST_PH, gpios), TEST_GPIO_1), "");
829 
830 	/* DT_PHA */
831 	zassert_equal(DT_PHA(TEST_PH, gpios, pin), 10, "");
832 	zassert_equal(DT_PHA(TEST_PH, gpios, flags), 20, "");
833 
834 	/* DT_PHA_BY_IDX */
835 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, pin), 10, "");
836 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, flags), 20, "");
837 
838 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, pin), 30, "");
839 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, flags), 40, "");
840 
841 	/* DT_PHA_BY_NAME */
842 	zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, a, foocell), 100, "");
843 	zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, b_c, foocell), 110, "");
844 
845 	/* DT_PHANDLE_BY_NAME */
846 	zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_NAME(TEST_PH, foos, a), TEST_GPIO_1), "");
847 	zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_NAME(TEST_PH, foos, b_c), TEST_GPIO_2), "");
848 
849 	/* array initializers */
850 	zassert_true(!strcmp(gps[0].name, "TEST_GPIO_1"), "");
851 	zassert_equal(gps[0].pin, 10, "");
852 	zassert_equal(gps[0].flags, 20, "");
853 
854 	zassert_true(!strcmp(gps[1].name, "TEST_GPIO_2"), "");
855 	zassert_equal(gps[1].pin, 30, "");
856 	zassert_equal(gps[1].flags, 40, "");
857 
858 	/* DT_INST */
859 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
860 
861 	/* DT_INST_PROP_BY_PHANDLE */
862 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE(0, ph, label),
863 			     "TEST_GPIO_1"), "");
864 
865 	zassert_true(!strcmp(ph_label, "TEST_GPIO_1"), "");
866 
867 	/* DT_INST_PROP_BY_PHANDLE_IDX */
868 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 0, label),
869 			     "TEST_GPIO_1"), "");
870 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 1, label),
871 			     "TEST_GPIO_2"), "");
872 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 2, label),
873 			     "TEST_I2C_CTLR"), "");
874 	zassert_true(!strcmp(phs_labels[0], "TEST_GPIO_1"), "");
875 	zassert_true(!strcmp(phs_labels[1], "TEST_GPIO_2"), "");
876 	zassert_true(!strcmp(phs_labels[2], "TEST_I2C_CTLR"), "");
877 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0,
878 					     gpio_controller),
879 		      1, "");
880 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1,
881 					     gpio_controller),
882 		      1, "");
883 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0, label),
884 			     "TEST_GPIO_1"), "");
885 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1, label),
886 			     "TEST_GPIO_2"), "");
887 
888 	/* DT_INST_PROP_HAS_IDX */
889 	zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 0), "");
890 	zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 1), "");
891 	zassert_false(DT_INST_PROP_HAS_IDX(0, gpios, 2), "");
892 
893 	/* DT_INST_PROP_HAS_NAME */
894 	zassert_false(DT_INST_PROP_HAS_NAME(0, foos, A), "");
895 	zassert_true(DT_INST_PROP_HAS_NAME(0, foos, a), "");
896 	zassert_false(DT_INST_PROP_HAS_NAME(0, foos, b-c), "");
897 	zassert_true(DT_INST_PROP_HAS_NAME(0, foos, b_c), "");
898 	zassert_false(DT_INST_PROP_HAS_NAME(0, bazs, jane), "");
899 
900 	/* DT_INST_PHA_HAS_CELL_AT_IDX */
901 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, pin), "");
902 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, flags), "");
903 	/* index 1 has nothing, not even a phandle */
904 	zassert_false(DT_INST_PROP_HAS_IDX(0, pha_gpios, 1), "");
905 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, pin), "");
906 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, flags), "");
907 	/* index 2 only has pin, no flags */
908 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, pin), "");
909 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, flags), "");
910 	/* index 3 has both pin and flags */
911 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, pin), "");
912 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, flags), "");
913 	/* even though index 1 has nothing, the length is still 4 */
914 	zassert_equal(DT_INST_PROP_LEN(0, pha_gpios), 4, "");
915 
916 	/* DT_INST_PHA_HAS_CELL */
917 	zassert_true(DT_INST_PHA_HAS_CELL(0, gpios, flags), "");
918 	zassert_false(DT_INST_PHA_HAS_CELL(0, gpios, bar), "");
919 
920 	/* DT_INST_PHANDLE_BY_IDX */
921 	zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_IDX(0, gpios, 0), TEST_GPIO_1), "");
922 	zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_IDX(0, gpios, 1), TEST_GPIO_2), "");
923 
924 	/* DT_INST_PHANDLE */
925 	zassert_true(DT_SAME_NODE(DT_INST_PHANDLE(0, gpios), TEST_GPIO_1), "");
926 
927 	/* DT_INST_PHA */
928 	zassert_equal(DT_INST_PHA(0, gpios, pin), 10, "");
929 	zassert_equal(DT_INST_PHA(0, gpios, flags), 20, "");
930 
931 	/* DT_INST_PHA_BY_IDX */
932 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, pin), 10, "");
933 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, flags), 20, "");
934 
935 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, pin), 30, "");
936 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, flags), 40, "");
937 
938 	/* DT_INST_PHA_BY_NAME */
939 	zassert_equal(DT_INST_PHA_BY_NAME(0, foos, a, foocell), 100, "");
940 	zassert_equal(DT_INST_PHA_BY_NAME(0, foos, b_c, foocell), 110, "");
941 
942 	/* DT_INST_PHANDLE_BY_NAME */
943 	zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_NAME(0, foos, a), TEST_GPIO_1), "");
944 	zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_NAME(0, foos, b_c), TEST_GPIO_2), "");
945 }
946 
947 #undef DT_DRV_COMPAT
948 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_gpio)949 ZTEST(devicetree_api, test_gpio)
950 {
951 	/* DT_GPIO_CTLR_BY_IDX */
952 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 0)),
953 			     TO_STRING(DT_NODELABEL(test_gpio_1))), "");
954 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 1)),
955 			     TO_STRING(DT_NODELABEL(test_gpio_2))), "");
956 
957 	/* DT_GPIO_CTLR */
958 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR(TEST_PH, gpios)),
959 			     TO_STRING(DT_NODELABEL(test_gpio_1))), "");
960 
961 	/* DT_GPIO_LABEL_BY_IDX */
962 	zassert_true(!strcmp(DT_GPIO_LABEL_BY_IDX(TEST_PH, gpios, 0),
963 			     "TEST_GPIO_1"), "");
964 	zassert_true(!strcmp(DT_GPIO_LABEL_BY_IDX(TEST_PH, gpios, 1),
965 			     "TEST_GPIO_2"), "");
966 
967 	/* DT_GPIO_LABEL */
968 	zassert_true(!strcmp(DT_GPIO_LABEL(TEST_PH, gpios), "TEST_GPIO_1"), "");
969 
970 	/* DT_GPIO_PIN_BY_IDX */
971 	zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 0), 10, "");
972 	zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 1), 30, "");
973 
974 	/* DT_GPIO_PIN */
975 	zassert_equal(DT_GPIO_PIN(TEST_PH, gpios), 10, "");
976 
977 	/* DT_GPIO_FLAGS_BY_IDX */
978 	zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 0), 20, "");
979 	zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 1), 40, "");
980 
981 	/* DT_GPIO_FLAGS */
982 	zassert_equal(DT_GPIO_FLAGS(TEST_PH, gpios), 20, "");
983 
984 	/* DT_NUM_GPIO_HOGS */
985 	zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_1), 2, "");
986 	zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_2), 1, "");
987 	zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_3), 1, "");
988 
989 	/* DT_GPIO_HOG_PIN_BY_IDX */
990 	zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_1, 0), 0, "");
991 	zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_1, 1), 1, "");
992 	zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_2, 0), 3, "");
993 	zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_3, 0), 4, "");
994 
995 	/* DT_GPIO_HOG_FLAGS_BY_IDX */
996 	zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_1, 0), 0x00, "");
997 	zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_1, 1), 0x10, "");
998 	zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_2, 0), 0x20, "");
999 	zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_3, 0), 0x30, "");
1000 
1001 	/* DT_INST */
1002 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1003 
1004 	/* DT_INST_GPIO_LABEL_BY_IDX */
1005 	zassert_true(!strcmp(DT_INST_GPIO_LABEL_BY_IDX(0, gpios, 0),
1006 			     "TEST_GPIO_1"), "");
1007 	zassert_true(!strcmp(DT_INST_GPIO_LABEL_BY_IDX(0, gpios, 1),
1008 			     "TEST_GPIO_2"), "");
1009 
1010 	/* DT_INST_GPIO_LABEL */
1011 	zassert_true(!strcmp(DT_INST_GPIO_LABEL(0, gpios), "TEST_GPIO_1"), "");
1012 
1013 	/* DT_INST_GPIO_PIN_BY_IDX */
1014 	zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 0), 10, "");
1015 	zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 1), 30, "");
1016 
1017 	/* DT_INST_GPIO_PIN */
1018 	zassert_equal(DT_INST_GPIO_PIN(0, gpios), 10, "");
1019 
1020 	/* DT_INST_GPIO_FLAGS_BY_IDX */
1021 	zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 0), 20, "");
1022 	zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 1), 40, "");
1023 
1024 	/* DT_INST_GPIO_FLAGS */
1025 	zassert_equal(DT_INST_GPIO_FLAGS(0, gpios), 20, "");
1026 }
1027 
1028 #undef DT_DRV_COMPAT
1029 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_io_channels)1030 ZTEST(devicetree_api, test_io_channels)
1031 {
1032 	/* DT_IO_CHANNELS_CTLR_BY_IDX */
1033 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 0),
1034 				  TEST_IO_CHANNEL_CTLR_1), "");
1035 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 1),
1036 				  TEST_IO_CHANNEL_CTLR_2), "");
1037 
1038 	/* DT_IO_CHANNELS_CTLR_BY_NAME */
1039 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch1),
1040 				  TEST_IO_CHANNEL_CTLR_1), "");
1041 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch2),
1042 				  TEST_IO_CHANNEL_CTLR_2), "");
1043 
1044 	/* DT_IO_CHANNELS_CTLR */
1045 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR(TEST_TEMP),
1046 				  TEST_IO_CHANNEL_CTLR_1), "");
1047 
1048 	/* DT_INST_IO_CHANNELS_CTLR_BY_IDX */
1049 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 0),
1050 				  TEST_IO_CHANNEL_CTLR_1), "");
1051 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 1),
1052 				  TEST_IO_CHANNEL_CTLR_2), "");
1053 
1054 	/* DT_INST_IO_CHANNELS_CTLR_BY_NAME */
1055 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch1),
1056 				  TEST_IO_CHANNEL_CTLR_1), "");
1057 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch2),
1058 				  TEST_IO_CHANNEL_CTLR_2), "");
1059 
1060 	/* DT_INST_IO_CHANNELS_CTLR */
1061 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR(0),
1062 				  TEST_IO_CHANNEL_CTLR_1), "");
1063 
1064 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 0), 10, "");
1065 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 1), 20, "");
1066 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch1), 10, "");
1067 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch2), 20, "");
1068 	zassert_equal(DT_IO_CHANNELS_INPUT(TEST_TEMP), 10, "");
1069 
1070 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 0), 10, "");
1071 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 1), 20, "");
1072 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch1), 10, "");
1073 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch2), 20, "");
1074 	zassert_equal(DT_INST_IO_CHANNELS_INPUT(0), 10, "");
1075 }
1076 
1077 #undef DT_DRV_COMPAT
1078 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_dma)1079 ZTEST(devicetree_api, test_dma)
1080 {
1081 	/* DT_DMAS_CTLR_BY_IDX */
1082 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 0),
1083 				  TEST_DMA_CTLR_1), "");
1084 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 1),
1085 				  TEST_DMA_CTLR_2), "");
1086 
1087 	/* DT_DMAS_CTLR_BY_NAME */
1088 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, tx),
1089 				  TEST_DMA_CTLR_1), "");
1090 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, rx),
1091 				  TEST_DMA_CTLR_2), "");
1092 
1093 	/* DT_DMAS_CTLR */
1094 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR(TEST_TEMP),
1095 				  TEST_DMA_CTLR_1), "");
1096 
1097 	/* DT_INST_DMAS_CTLR_BY_IDX */
1098 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 0),
1099 				  TEST_DMA_CTLR_1), "");
1100 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 1),
1101 				  TEST_DMA_CTLR_2), "");
1102 
1103 	/* DT_INST_DMAS_CTLR_BY_NAME */
1104 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, tx),
1105 				  TEST_DMA_CTLR_1), "");
1106 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, rx),
1107 				  TEST_DMA_CTLR_2), "");
1108 
1109 	/* DT_INST_DMAS_CTLR */
1110 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR(0), TEST_DMA_CTLR_1), "");
1111 
1112 	zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, channel), 3, "");
1113 	zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, channel), 3, "");
1114 	zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, slot), 4, "");
1115 	zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, slot), 4, "");
1116 
1117 	zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, channel), 3, "");
1118 	zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, channel), 3, "");
1119 	zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, slot), 4, "");
1120 	zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, slot), 4, "");
1121 
1122 	zassert_true(DT_DMAS_HAS_NAME(TEST_TEMP, tx), "");
1123 	zassert_true(DT_INST_DMAS_HAS_NAME(0, tx), "");
1124 	zassert_false(DT_DMAS_HAS_NAME(TEST_TEMP, output), "");
1125 	zassert_false(DT_INST_DMAS_HAS_NAME(0, output), "");
1126 
1127 	zassert_true(DT_DMAS_HAS_IDX(TEST_TEMP, 1), "");
1128 	zassert_true(DT_INST_DMAS_HAS_IDX(0, 1), "");
1129 	zassert_false(DT_DMAS_HAS_IDX(TEST_TEMP, 2), "");
1130 	zassert_false(DT_INST_DMAS_HAS_IDX(0, 2), "");
1131 }
1132 
1133 #undef DT_DRV_COMPAT
1134 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_pwms)1135 ZTEST(devicetree_api, test_pwms)
1136 {
1137 	/* DT_PWMS_CTLR_BY_IDX */
1138 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 0),
1139 				  TEST_PWM_CTLR_1), "");
1140 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 1),
1141 				  TEST_PWM_CTLR_2), "");
1142 
1143 	/* DT_PWMS_CTLR_BY_NAME */
1144 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, red),
1145 				  TEST_PWM_CTLR_1), "");
1146 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, green),
1147 				  TEST_PWM_CTLR_2), "");
1148 
1149 	/* DT_PWMS_CTLR */
1150 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR(TEST_PH),
1151 				  TEST_PWM_CTLR_1), "");
1152 
1153 	/* DT_PWMS_CELL_BY_IDX */
1154 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, channel), 5, "");
1155 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, period), 100, "");
1156 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, flags), 1, "");
1157 
1158 	/* DT_PWMS_CELL_BY_NAME */
1159 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, channel), 8, "");
1160 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, period), 200, "");
1161 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, flags), 3, "");
1162 
1163 	/* DT_PWMS_CELL */
1164 	zassert_equal(DT_PWMS_CELL(TEST_PH, channel), 8, "");
1165 	zassert_equal(DT_PWMS_CELL(TEST_PH, period), 200, "");
1166 	zassert_equal(DT_PWMS_CELL(TEST_PH, flags), 3, "");
1167 
1168 	/* DT_PWMS_CHANNEL_BY_IDX */
1169 	zassert_equal(DT_PWMS_CHANNEL_BY_IDX(TEST_PH, 1), 5, "");
1170 
1171 	/* DT_PWMS_CHANNEL_BY_NAME */
1172 	zassert_equal(DT_PWMS_CHANNEL_BY_NAME(TEST_PH, green), 5, "");
1173 
1174 	/* DT_PWMS_CHANNEL */
1175 	zassert_equal(DT_PWMS_CHANNEL(TEST_PH), 8, "");
1176 
1177 	/* DT_PWMS_PERIOD_BY_IDX */
1178 	zassert_equal(DT_PWMS_PERIOD_BY_IDX(TEST_PH, 1), 100, "");
1179 
1180 	/* DT_PWMS_PERIOD_BY_NAME */
1181 	zassert_equal(DT_PWMS_PERIOD_BY_NAME(TEST_PH, green), 100, "");
1182 
1183 	/* DT_PWMS_PERIOD */
1184 	zassert_equal(DT_PWMS_PERIOD(TEST_PH), 200, "");
1185 
1186 	/* DT_PWMS_FLAGS_BY_IDX */
1187 	zassert_equal(DT_PWMS_FLAGS_BY_IDX(TEST_PH, 1), 1, "");
1188 
1189 	/* DT_PWMS_FLAGS_BY_NAME */
1190 	zassert_equal(DT_PWMS_FLAGS_BY_NAME(TEST_PH, green), 1, "");
1191 
1192 	/* DT_PWMS_FLAGS */
1193 	zassert_equal(DT_PWMS_FLAGS(TEST_PH), 3, "");
1194 
1195 	/* DT_INST */
1196 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1197 
1198 	/* DT_INST_PWMS_CTLR_BY_IDX */
1199 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 0),
1200 				  TEST_PWM_CTLR_1), "");
1201 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 1),
1202 				  TEST_PWM_CTLR_2), "");
1203 
1204 	/* DT_INST_PWMS_CTLR_BY_NAME */
1205 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, red),
1206 				  TEST_PWM_CTLR_1), "");
1207 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, green),
1208 				  TEST_PWM_CTLR_2), "");
1209 
1210 	/* DT_INST_PWMS_CTLR */
1211 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR(0), TEST_PWM_CTLR_1), "");
1212 
1213 	/* DT_INST_PWMS_CELL_BY_IDX */
1214 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, channel), 5, "");
1215 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, period), 100, "");
1216 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, flags), 1, "");
1217 
1218 	/* DT_INST_PWMS_CELL_BY_NAME */
1219 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, channel), 5, "");
1220 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, period), 100, "");
1221 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, flags), 1, "");
1222 
1223 	/* DT_INST_PWMS_CELL */
1224 	zassert_equal(DT_INST_PWMS_CELL(0, channel), 8, "");
1225 	zassert_equal(DT_INST_PWMS_CELL(0, period), 200, "");
1226 	zassert_equal(DT_INST_PWMS_CELL(0, flags), 3, "");
1227 
1228 	/* DT_INST_PWMS_CHANNEL_BY_IDX */
1229 	zassert_equal(DT_INST_PWMS_CHANNEL_BY_IDX(0, 1), 5, "");
1230 
1231 	/* DT_INST_PWMS_CHANNEL_BY_NAME */
1232 	zassert_equal(DT_INST_PWMS_CHANNEL_BY_NAME(0, green), 5, "");
1233 
1234 	/* DT_INST_PWMS_CHANNEL */
1235 	zassert_equal(DT_INST_PWMS_CHANNEL(0), 8, "");
1236 
1237 	/* DT_INST_PWMS_PERIOD_BY_IDX */
1238 	zassert_equal(DT_INST_PWMS_PERIOD_BY_IDX(0, 1), 100, "");
1239 
1240 	/* DT_INST_PWMS_PERIOD_BY_NAME */
1241 	zassert_equal(DT_INST_PWMS_PERIOD_BY_NAME(0, red), 200, "");
1242 
1243 	/* DT_INST_PWMS_PERIOD */
1244 	zassert_equal(DT_INST_PWMS_PERIOD(0), 200, "");
1245 
1246 	/* DT_INST_PWMS_FLAGS_BY_IDX */
1247 	zassert_equal(DT_INST_PWMS_FLAGS_BY_IDX(0, 1), 1, "");
1248 
1249 	/* DT_INST_PWMS_FLAGS_BY_NAME */
1250 	zassert_equal(DT_INST_PWMS_FLAGS_BY_NAME(0, red), 3, "");
1251 
1252 	/* DT_INST_PWMS_FLAGS */
1253 	zassert_equal(DT_INST_PWMS_FLAGS(0), 3, "");
1254 }
1255 
1256 #undef DT_DRV_COMPAT
1257 #define DT_DRV_COMPAT vnd_can_controller
ZTEST(devicetree_api,test_can)1258 ZTEST(devicetree_api, test_can)
1259 {
1260 	/* DT_CAN_TRANSCEIVER_MAX_BITRATE */
1261 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 1000000), 1000000, "");
1262 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 5000000), 5000000, "");
1263 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 8000000), 5000000, "");
1264 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 1250000), 1250000, "");
1265 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 2000000), 2000000, "");
1266 	zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 5000000), 2000000, "");
1267 
1268 	/* DT_INST_CAN_TRANSCEIVER_MAX_BITRATE */
1269 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 1000000), 1000000, "");
1270 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 5000000), 5000000, "");
1271 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 8000000), 5000000, "");
1272 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 1250000), 1250000, "");
1273 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 2000000), 2000000, "");
1274 	zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 5000000), 2000000, "");
1275 }
1276 
ZTEST(devicetree_api,test_macro_names)1277 ZTEST(devicetree_api, test_macro_names)
1278 {
1279 	/* white box */
1280 	zassert_true(!strcmp(TO_STRING(DT_PATH(test, gpio_deadbeef)),
1281 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1282 	zassert_true(!strcmp(TO_STRING(DT_ALIAS(test_alias)),
1283 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1284 	zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel)),
1285 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1286 	zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel_allcaps)),
1287 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1288 
1289 #define CHILD_NODE_ID DT_CHILD(DT_PATH(test, i2c_11112222), test_i2c_dev_10)
1290 #define FULL_PATH_ID DT_PATH(test, i2c_11112222, test_i2c_dev_10)
1291 
1292 	zassert_true(!strcmp(TO_STRING(CHILD_NODE_ID),
1293 			     TO_STRING(FULL_PATH_ID)), "");
1294 
1295 #undef CHILD_NODE_ID
1296 #undef FULL_PATH_ID
1297 }
1298 
1299 static int a[] = DT_PROP(TEST_ARRAYS, a);
1300 static unsigned char b[] = DT_PROP(TEST_ARRAYS, b);
1301 static char *c[] = DT_PROP(TEST_ARRAYS, c);
1302 
ZTEST(devicetree_api,test_arrays)1303 ZTEST(devicetree_api, test_arrays)
1304 {
1305 	int ok;
1306 
1307 	zassert_equal(ARRAY_SIZE(a), 3, "");
1308 	zassert_equal(ARRAY_SIZE(b), 4, "");
1309 	zassert_equal(ARRAY_SIZE(c), 2, "");
1310 
1311 	zassert_equal(a[0], 1000, "");
1312 	zassert_equal(a[1], 2000, "");
1313 	zassert_equal(a[2], 3000, "");
1314 
1315 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), "");
1316 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 1), "");
1317 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 2), "");
1318 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), "");
1319 
1320 	/*
1321 	 * Verify that DT_PROP_HAS_IDX can be used with COND_CODE_1()
1322 	 * and COND_CODE_0(), i.e. its expansion is a literal 1 or 0,
1323 	 * not an equivalent expression that evaluates to 1 or 0.
1324 	 */
1325 	ok = 0;
1326 	COND_CODE_1(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), (ok = 1;), ());
1327 	zassert_equal(ok, 1, "");
1328 	ok = 0;
1329 	COND_CODE_0(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), (ok = 1;), ());
1330 	zassert_equal(ok, 1, "");
1331 
1332 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 0), a[0], "");
1333 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 1), a[1], "");
1334 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 2), a[2], "");
1335 
1336 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, a), 3, "");
1337 
1338 	zassert_equal(b[0], 0xaa, "");
1339 	zassert_equal(b[1], 0xbb, "");
1340 	zassert_equal(b[2], 0xcc, "");
1341 	zassert_equal(b[3], 0xdd, "");
1342 
1343 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 0), "");
1344 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 1), "");
1345 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 2), "");
1346 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 3), "");
1347 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 4), "");
1348 
1349 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 0), b[0], "");
1350 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 1), b[1], "");
1351 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 2), b[2], "");
1352 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 3), b[3], "");
1353 
1354 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, b), 4, "");
1355 
1356 	zassert_true(!strcmp(c[0], "bar"), "");
1357 	zassert_true(!strcmp(c[1], "baz"), "");
1358 
1359 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 0), "");
1360 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 1), "");
1361 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 2), "");
1362 
1363 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 0), c[0]), "");
1364 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 1), c[1]), "");
1365 
1366 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, c), 2, "");
1367 }
1368 
ZTEST(devicetree_api,test_foreach)1369 ZTEST(devicetree_api, test_foreach)
1370 {
1371 	/*
1372 	 * We don't know what platform we are running on, so settle for
1373 	 * some basic checks related to nodes we know are in our overlay.
1374 	 */
1375 #define IS_ALIASES(node_id) + DT_SAME_NODE(DT_PATH(aliases), node_id)
1376 #define IS_DISABLED_GPIO(node_id) + DT_SAME_NODE(DT_NODELABEL(disabled_gpio), \
1377 						node_id)
1378 	zassert_equal(1, DT_FOREACH_NODE(IS_ALIASES), "");
1379 	zassert_equal(1, DT_FOREACH_NODE(IS_DISABLED_GPIO), "");
1380 	zassert_equal(1, DT_FOREACH_STATUS_OKAY_NODE(IS_ALIASES), "");
1381 	zassert_equal(0, DT_FOREACH_STATUS_OKAY_NODE(IS_DISABLED_GPIO), "");
1382 
1383 #undef IS_ALIASES
1384 #undef IS_DISABLED_GPIO
1385 }
1386 
1387 #undef DT_DRV_COMPAT
1388 #define DT_DRV_COMPAT vnd_gpio_device
ZTEST(devicetree_api,test_foreach_status_okay)1389 ZTEST(devicetree_api, test_foreach_status_okay)
1390 {
1391 	/*
1392 	 * For-each-node type macro tests.
1393 	 *
1394 	 * See test_foreach_prop_elem*() for tests of
1395 	 * for-each-property type macros.
1396 	 */
1397 	unsigned int val;
1398 	const char *str;
1399 
1400 	/* This should expand to something like:
1401 	 *
1402 	 * "/test/enum-0" "/test/enum-1"
1403 	 *
1404 	 * but there is no guarantee about the order of nodes in the
1405 	 * expansion, so we test both.
1406 	 */
1407 	str = DT_FOREACH_STATUS_OKAY(vnd_enum_holder, DT_NODE_PATH);
1408 	zassert_true(!strcmp(str, "/test/enum-0/test/enum-1") ||
1409 		     !strcmp(str, "/test/enum-1/test/enum-0"), "");
1410 
1411 #undef MY_FN
1412 #define MY_FN(node_id, operator) DT_ENUM_IDX(node_id, val) operator
1413 	/* This should expand to something like:
1414 	 *
1415 	 * 0 + 2 + 3
1416 	 *
1417 	 * and order of expansion doesn't matter, since we're adding
1418 	 * the values all up.
1419 	 */
1420 	val = DT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
1421 	zassert_equal(val, 5, "");
1422 
1423 	/*
1424 	 * Make sure DT_INST_FOREACH_STATUS_OKAY can be called from functions
1425 	 * using macros with side effects in the current scope.
1426 	 */
1427 	val = 0;
1428 #define INC(inst_ignored) do { val++; } while (0);
1429 	DT_INST_FOREACH_STATUS_OKAY(INC)
1430 	zassert_equal(val, 2, "");
1431 #undef INC
1432 
1433 	val = 0;
1434 #define INC_ARG(arg) do { val++; val += arg; } while (0)
1435 #define INC(inst_ignored, arg) INC_ARG(arg);
1436 	DT_INST_FOREACH_STATUS_OKAY_VARGS(INC, 1)
1437 	zassert_equal(val, 4, "");
1438 #undef INC_ARG
1439 #undef INC
1440 
1441 	/*
1442 	 * Make sure DT_INST_FOREACH_STATUS_OKAY works with 0 instances, and does
1443 	 * not expand its argument at all.
1444 	 */
1445 #undef DT_DRV_COMPAT
1446 #define DT_DRV_COMPAT xxxx
1447 #define BUILD_BUG_ON_EXPANSION (there is a bug in devicetree.h)
1448 	DT_INST_FOREACH_STATUS_OKAY(BUILD_BUG_ON_EXPANSION)
1449 #undef BUILD_BUG_ON_EXPANSION
1450 
1451 #undef DT_DRV_COMPAT
1452 #define DT_DRV_COMPAT xxxx
1453 #define BUILD_BUG_ON_EXPANSION(arg) (there is a bug in devicetree.h)
1454 	DT_INST_FOREACH_STATUS_OKAY_VARGS(BUILD_BUG_ON_EXPANSION, 1)
1455 #undef BUILD_BUG_ON_EXPANSION
1456 }
1457 
ZTEST(devicetree_api,test_foreach_prop_elem)1458 ZTEST(devicetree_api, test_foreach_prop_elem)
1459 {
1460 #define TIMES_TWO(node_id, prop, idx) \
1461 	(2 * DT_PROP_BY_IDX(node_id, prop, idx)),
1462 #define BY_IDX_COMMA(node_id, prop, idx) DT_PROP_BY_IDX(node_id, prop, idx),
1463 
1464 	int array_a[] = {
1465 		DT_FOREACH_PROP_ELEM(TEST_ARRAYS, a, TIMES_TWO)
1466 	};
1467 
1468 	zassert_equal(ARRAY_SIZE(array_a), 3, "");
1469 	zassert_equal(array_a[0], 2000, "");
1470 	zassert_equal(array_a[1], 4000, "");
1471 	zassert_equal(array_a[2], 6000, "");
1472 
1473 	int array_b[] = {
1474 		DT_FOREACH_PROP_ELEM(TEST_ARRAYS, b, TIMES_TWO)
1475 	};
1476 
1477 	zassert_equal(ARRAY_SIZE(array_b), 4, "");
1478 	zassert_equal(array_b[0], 2 * 0xAA, "");
1479 	zassert_equal(array_b[1], 2 * 0xBB, "");
1480 	zassert_equal(array_b[2], 2 * 0xCC, "");
1481 	zassert_equal(array_b[3], 2 * 0xDD, "");
1482 
1483 	static const char * const array_c[] = {
1484 		DT_FOREACH_PROP_ELEM(TEST_ARRAYS, c, BY_IDX_COMMA)
1485 	};
1486 
1487 	zassert_equal(ARRAY_SIZE(array_c), 2, "");
1488 	zassert_true(!strcmp(array_c[0], "bar"), "");
1489 	zassert_true(!strcmp(array_c[1], "baz"), "");
1490 
1491 	static const char * const array_val[] = {
1492 		DT_FOREACH_PROP_ELEM(TEST_ENUM_0, val, BY_IDX_COMMA)
1493 	};
1494 
1495 	zassert_equal(ARRAY_SIZE(array_val), 1, "");
1496 	zassert_true(!strcmp(array_val[0], "zero"), "");
1497 
1498 	static const char * const string_zephyr_user[] = {
1499 		DT_FOREACH_PROP_ELEM(ZEPHYR_USER, string, BY_IDX_COMMA)
1500 	};
1501 
1502 	zassert_equal(ARRAY_SIZE(string_zephyr_user), 1, "");
1503 	zassert_true(!strcmp(string_zephyr_user[0], "foo"), "");
1504 
1505 #undef BY_IDX_COMMA
1506 
1507 #define PATH_COMMA(node_id, prop, idx) \
1508 	DT_NODE_PATH(DT_PROP_BY_IDX(node_id, prop, idx)),
1509 
1510 	static const char * const array_ph[] = {
1511 		DT_FOREACH_PROP_ELEM(TEST_PH, ph, PATH_COMMA)
1512 	};
1513 
1514 	zassert_equal(ARRAY_SIZE(array_ph), 1, "");
1515 	zassert_true(!strcmp(array_ph[0], DT_NODE_PATH(TEST_GPIO_1)), "");
1516 
1517 	static const char * const array_zephyr_user_ph[] = {
1518 		DT_FOREACH_PROP_ELEM(ZEPHYR_USER, ph, PATH_COMMA)
1519 	};
1520 
1521 	zassert_equal(ARRAY_SIZE(array_zephyr_user_ph), 1, "");
1522 	zassert_true(!strcmp(array_zephyr_user_ph[0],
1523 			     DT_NODE_PATH(TEST_GPIO_1)), "");
1524 
1525 	static const char * const array_phs[] = {
1526 		DT_FOREACH_PROP_ELEM(TEST_PH, phs, PATH_COMMA)
1527 	};
1528 
1529 	zassert_equal(ARRAY_SIZE(array_phs), 3, "");
1530 	zassert_true(!strcmp(array_phs[0], DT_NODE_PATH(TEST_GPIO_1)), "");
1531 	zassert_true(!strcmp(array_phs[1], DT_NODE_PATH(TEST_GPIO_2)), "");
1532 	zassert_true(!strcmp(array_phs[2], DT_NODE_PATH(TEST_I2C)), "");
1533 
1534 #undef PATH_COMMA
1535 
1536 #define PIN_COMMA(node_id, prop, idx) DT_GPIO_PIN_BY_IDX(node_id, prop, idx),
1537 
1538 	int array_gpios[] = {
1539 		DT_FOREACH_PROP_ELEM(TEST_PH, gpios, PIN_COMMA)
1540 	};
1541 
1542 	zassert_equal(ARRAY_SIZE(array_gpios), 2, "");
1543 	zassert_equal(array_gpios[0], 10, "");
1544 	zassert_equal(array_gpios[1], 30, "");
1545 
1546 #undef PATH_COMMA
1547 
1548 	int array_sep[] = {
1549 		DT_FOREACH_PROP_ELEM_SEP(TEST_ARRAYS, a, DT_PROP_BY_IDX, (,))
1550 	};
1551 
1552 	zassert_equal(ARRAY_SIZE(array_sep), 3, "");
1553 	zassert_equal(array_sep[0], 1000, "");
1554 	zassert_equal(array_sep[1], 2000, "");
1555 	zassert_equal(array_sep[2], 3000, "");
1556 
1557 #undef DT_DRV_COMPAT
1558 #define DT_DRV_COMPAT vnd_array_holder
1559 
1560 	int inst_array[] = {
1561 		DT_INST_FOREACH_PROP_ELEM(0, a, TIMES_TWO)
1562 	};
1563 
1564 	zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array_a), "");
1565 	zassert_equal(inst_array[0], array_a[0], "");
1566 	zassert_equal(inst_array[1], array_a[1], "");
1567 	zassert_equal(inst_array[2], array_a[2], "");
1568 
1569 	int inst_array_sep[] = {
1570 		DT_INST_FOREACH_PROP_ELEM_SEP(0, a, DT_PROP_BY_IDX, (,))
1571 	};
1572 
1573 	zassert_equal(ARRAY_SIZE(inst_array_sep), ARRAY_SIZE(array_sep), "");
1574 	zassert_equal(inst_array_sep[0], array_sep[0], "");
1575 	zassert_equal(inst_array_sep[1], array_sep[1], "");
1576 	zassert_equal(inst_array_sep[2], array_sep[2], "");
1577 #undef TIMES_TWO
1578 }
1579 
ZTEST(devicetree_api,test_foreach_prop_elem_varg)1580 ZTEST(devicetree_api, test_foreach_prop_elem_varg)
1581 {
1582 #define TIMES_TWO_ADD(node_id, prop, idx, arg) \
1583 	((2 * DT_PROP_BY_IDX(node_id, prop, idx)) + arg),
1584 
1585 	int array[] = {
1586 		DT_FOREACH_PROP_ELEM_VARGS(TEST_ARRAYS, a, TIMES_TWO_ADD, 3)
1587 	};
1588 
1589 	zassert_equal(ARRAY_SIZE(array), 3, "");
1590 	zassert_equal(array[0], 2003, "");
1591 	zassert_equal(array[1], 4003, "");
1592 	zassert_equal(array[2], 6003, "");
1593 
1594 #define PROP_PLUS_ARG(node_id, prop, idx, arg) \
1595 	(DT_PROP_BY_IDX(node_id, prop, idx) + arg)
1596 
1597 	int array_sep[] = {
1598 		DT_FOREACH_PROP_ELEM_SEP_VARGS(TEST_ARRAYS, a, PROP_PLUS_ARG,
1599 					       (,), 3)
1600 	};
1601 
1602 	zassert_equal(ARRAY_SIZE(array_sep), 3, "");
1603 	zassert_equal(array_sep[0], 1003, "");
1604 	zassert_equal(array_sep[1], 2003, "");
1605 	zassert_equal(array_sep[2], 3003, "");
1606 
1607 #undef DT_DRV_COMPAT
1608 #define DT_DRV_COMPAT vnd_array_holder
1609 
1610 	int inst_array[] = {
1611 		DT_INST_FOREACH_PROP_ELEM_VARGS(0, a, TIMES_TWO_ADD, 3)
1612 	};
1613 
1614 	zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array), "");
1615 	zassert_equal(inst_array[0], array[0], "");
1616 	zassert_equal(inst_array[1], array[1], "");
1617 	zassert_equal(inst_array[2], array[2], "");
1618 
1619 	int inst_array_sep[] = {
1620 		DT_INST_FOREACH_PROP_ELEM_SEP_VARGS(0, a, PROP_PLUS_ARG, (,),
1621 						    3)
1622 	};
1623 
1624 	zassert_equal(ARRAY_SIZE(inst_array_sep), ARRAY_SIZE(array_sep), "");
1625 	zassert_equal(inst_array_sep[0], array_sep[0], "");
1626 	zassert_equal(inst_array_sep[1], array_sep[1], "");
1627 	zassert_equal(inst_array_sep[2], array_sep[2], "");
1628 #undef TIMES_TWO
1629 }
1630 
1631 struct test_gpio_info {
1632 	uint32_t reg_addr;
1633 	uint32_t reg_len;
1634 };
1635 
1636 struct test_gpio_data {
1637 	bool init_called;
1638 	bool is_gpio_ctlr;
1639 };
1640 
test_gpio_init(const struct device * dev)1641 static int test_gpio_init(const struct device *dev)
1642 {
1643 	struct test_gpio_data *data = dev->data;
1644 
1645 	data->init_called = 1;
1646 	return 0;
1647 }
1648 
1649 #define INST(num) DT_INST(num, vnd_gpio_device)
1650 #undef DT_DRV_COMPAT
1651 #define DT_DRV_COMPAT vnd_gpio_device
1652 
1653 static const struct gpio_driver_api test_api;
1654 
1655 #define TEST_GPIO_INIT(num)					\
1656 	static struct test_gpio_data gpio_data_##num = {	\
1657 		.is_gpio_ctlr = DT_PROP(INST(num),		\
1658 					gpio_controller),	\
1659 	};							\
1660 	static const struct test_gpio_info gpio_info_##num = {	\
1661 		.reg_addr = DT_REG_ADDR(INST(num)),		\
1662 		.reg_len = DT_REG_SIZE(INST(num)),		\
1663 	};							\
1664 	DEVICE_DT_DEFINE(INST(num),				\
1665 			    test_gpio_init,			\
1666 			    NULL,				\
1667 			    &gpio_data_##num,			\
1668 			    &gpio_info_##num,			\
1669 			    POST_KERNEL,			\
1670 			    CONFIG_APPLICATION_INIT_PRIORITY,	\
1671 			    &test_api);
1672 
1673 DT_INST_FOREACH_STATUS_OKAY(TEST_GPIO_INIT)
1674 
ZTEST(devicetree_api,test_devices)1675 ZTEST(devicetree_api, test_devices)
1676 {
1677 	const struct device *devs[3];
1678 	int i = 0;
1679 	const struct device *dev_abcd;
1680 	struct test_gpio_data *data_dev0;
1681 	struct test_gpio_data *data_dev1;
1682 	const struct test_gpio_info *config_abdc;
1683 
1684 	zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio_device), 2, "");
1685 
1686 	devs[i] = device_get_binding(DT_LABEL(INST(0)));
1687 	if (devs[i]) {
1688 		i++;
1689 	}
1690 	devs[i] = device_get_binding(DT_LABEL(INST(1)));
1691 	if (devs[i]) {
1692 		i++;
1693 	}
1694 	devs[i] = device_get_binding(DT_LABEL(INST(2)));
1695 	if (devs[i]) {
1696 		i++;
1697 	}
1698 
1699 	data_dev0 = devs[0]->data;
1700 	data_dev1 = devs[1]->data;
1701 
1702 	zassert_not_null(devs[0], "");
1703 	zassert_not_null(devs[1], "");
1704 	zassert_true(devs[2] == NULL, "");
1705 
1706 	zassert_true(data_dev0->is_gpio_ctlr, "");
1707 	zassert_true(data_dev1->is_gpio_ctlr, "");
1708 	zassert_true(data_dev0->init_called, "");
1709 	zassert_true(data_dev1->init_called, "");
1710 
1711 	dev_abcd = DEVICE_DT_GET(TEST_ABCD1234);
1712 	config_abdc = dev_abcd->config;
1713 	zassert_not_null(dev_abcd, "");
1714 	zassert_equal(config_abdc->reg_addr, 0xabcd1234, "");
1715 	zassert_equal(config_abdc->reg_len, 0x500, "");
1716 }
1717 
ZTEST(devicetree_api,test_cs_gpios)1718 ZTEST(devicetree_api, test_cs_gpios)
1719 {
1720 	zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
1721 	zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
1722 
1723 	zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI), 1, "");
1724 	zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI), 3, "");
1725 
1726 	zassert_equal(DT_DEP_ORD(DT_SPI_DEV_CS_GPIOS_CTLR(TEST_SPI_DEV_0)),
1727 		      DT_DEP_ORD(DT_NODELABEL(test_gpio_1)),
1728 		     "dev 0 cs gpio controller");
1729 	zassert_true(!strcmp(DT_SPI_DEV_CS_GPIOS_LABEL(TEST_SPI_DEV_0),
1730 			     "TEST_GPIO_1"), "");
1731 	zassert_equal(DT_SPI_DEV_CS_GPIOS_PIN(TEST_SPI_DEV_0), 0x10, "");
1732 	zassert_equal(DT_SPI_DEV_CS_GPIOS_FLAGS(TEST_SPI_DEV_0), 0x20, "");
1733 }
1734 
ZTEST(devicetree_api,test_chosen)1735 ZTEST(devicetree_api, test_chosen)
1736 {
1737 	zassert_equal(DT_HAS_CHOSEN(ztest_xxxx), 0, "");
1738 	zassert_equal(DT_HAS_CHOSEN(ztest_gpio), 1, "");
1739 	zassert_true(!strcmp(TO_STRING(DT_CHOSEN(ztest_gpio)),
1740 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1741 }
1742 
1743 #define TO_MY_ENUM(token) TO_MY_ENUM_2(token) /* force another expansion */
1744 #define TO_MY_ENUM_2(token) MY_ENUM_ ## token
ZTEST(devicetree_api,test_enums)1745 ZTEST(devicetree_api, test_enums)
1746 {
1747 	enum {
1748 		MY_ENUM_zero = 0xff,
1749 		MY_ENUM_ZERO = 0xaa,
1750 	};
1751 
1752 	/* DT_ENUM_IDX and DT_ENUM_HAS_VALUE on string enum */
1753 	zassert_equal(DT_ENUM_IDX(TEST_ENUM_0, val), 0, "0");
1754 	zassert_true(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, zero), "");
1755 	zassert_false(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, one), "");
1756 	zassert_false(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, two), "");
1757 
1758 	/* DT_ENUM_IDX and DT_ENUM_HAS_VALUE on int enum */
1759 	zassert_equal(DT_ENUM_IDX(DT_NODELABEL(test_enum_int_default_0), val), 0, "0");
1760 	zassert_true(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 5), "");
1761 	zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 6), "");
1762 	zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 7), "");
1763 }
1764 #undef TO_MY_ENUM
1765 #undef TO_MY_ENUM_2
1766 
ZTEST(devicetree_api,test_enums_required_false)1767 ZTEST(devicetree_api, test_enums_required_false)
1768 {
1769 	/* DT_ENUM_IDX_OR on string value */
1770 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_0), val, 2),
1771 		      1, "");
1772 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_1), val, 2),
1773 		      2, "");
1774 	/* DT_ENUM_IDX_OR on int value */
1775 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_0),
1776 				     val, 4),
1777 		      0, "");
1778 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_1),
1779 				     val, 4),
1780 		      4, "");
1781 }
1782 
ZTEST(devicetree_api,test_inst_enums)1783 ZTEST(devicetree_api, test_inst_enums)
1784 {
1785 #undef DT_DRV_COMPAT
1786 #define DT_DRV_COMPAT vnd_enum_holder_inst
1787 	zassert_equal(DT_INST_ENUM_IDX(0, val), 0, "");
1788 	zassert_equal(DT_INST_ENUM_IDX_OR(0, val, 2), 0, "");
1789 	zassert_true(DT_INST_ENUM_HAS_VALUE(0, val, zero), "");
1790 	zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, one), "");
1791 	zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, two), "");
1792 
1793 #undef DT_DRV_COMPAT
1794 #define DT_DRV_COMPAT vnd_enum_required_false_holder_inst
1795 	zassert_equal(DT_INST_ENUM_IDX_OR(0, val, 2), 2, "");
1796 	zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, zero), "");
1797 	zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, one), "");
1798 	zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, two), "");
1799 }
1800 
1801 #undef DT_DRV_COMPAT
1802 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_clocks)1803 ZTEST(devicetree_api, test_clocks)
1804 {
1805 	/* DT_CLOCKS_CTLR_BY_IDX */
1806 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_IDX(TEST_TEMP, 1),
1807 				  DT_NODELABEL(test_fixed_clk)), "");
1808 
1809 	/* DT_CLOCKS_CTLR */
1810 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR(TEST_TEMP),
1811 				  DT_NODELABEL(test_clk)), "");
1812 
1813 	/* DT_CLOCKS_CTLR_BY_NAME */
1814 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_NAME(TEST_TEMP, clk_b),
1815 				  DT_NODELABEL(test_clk)), "");
1816 
1817 	/* DT_NUM_CLOCKS */
1818 	zassert_equal(DT_NUM_CLOCKS(TEST_TEMP), 3, "");
1819 
1820 	/* DT_CLOCKS_HAS_IDX */
1821 	zassert_true(DT_CLOCKS_HAS_IDX(TEST_TEMP, 2), "");
1822 	zassert_false(DT_CLOCKS_HAS_IDX(TEST_TEMP, 3), "");
1823 
1824 	/* DT_CLOCKS_HAS_NAME */
1825 	zassert_true(DT_CLOCKS_HAS_NAME(TEST_TEMP, clk_a), "");
1826 	zassert_false(DT_CLOCKS_HAS_NAME(TEST_TEMP, clk_z), "");
1827 
1828 	/* DT_CLOCKS_CELL_BY_IDX */
1829 	zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bits), 2, "");
1830 	zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bus), 8, "");
1831 
1832 	/* DT_CLOCKS_CELL_BY_NAME */
1833 	zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_a, bits), 7, "");
1834 	zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_b, bus), 8, "");
1835 
1836 	/* DT_CLOCKS_CELL */
1837 	zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bits), 7, "");
1838 	zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bus), 3, "");
1839 
1840 	/* clock-freq on fixed clock */
1841 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_TEMP, clocks, 1,
1842 					     clock_frequency),
1843 		      25000000, "");
1844 
1845 	/* DT_INST */
1846 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1847 
1848 	/* DT_INST_CLOCKS_CTLR_BY_IDX */
1849 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_IDX(0, 1),
1850 				  DT_NODELABEL(test_fixed_clk)), "");
1851 
1852 	/* DT_INST_CLOCKS_CTLR */
1853 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR(0),
1854 				  DT_NODELABEL(test_clk)), "");
1855 
1856 	/* DT_INST_CLOCKS_CTLR_BY_NAME */
1857 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_NAME(0, clk_b),
1858 				  DT_NODELABEL(test_clk)), "");
1859 
1860 	/* DT_INST_NUM_CLOCKS */
1861 	zassert_equal(DT_INST_NUM_CLOCKS(0), 3, "");
1862 
1863 	/* DT_INST_CLOCKS_HAS_IDX */
1864 	zassert_true(DT_INST_CLOCKS_HAS_IDX(0, 2), "");
1865 	zassert_false(DT_INST_CLOCKS_HAS_IDX(0, 3), "");
1866 
1867 	/* DT_INST_CLOCKS_HAS_NAME */
1868 	zassert_true(DT_INST_CLOCKS_HAS_NAME(0, clk_a), "");
1869 	zassert_false(DT_INST_CLOCKS_HAS_NAME(0, clk_z), "");
1870 
1871 	/* DT_INST_CLOCKS_CELL_BY_IDX */
1872 	zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bits), 2, "");
1873 	zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bus), 8, "");
1874 
1875 	/* DT_INST_CLOCKS_CELL_BY_NAME */
1876 	zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_a, bits), 7, "");
1877 	zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_b, bus), 8, "");
1878 
1879 	/* DT_INST_CLOCKS_CELL */
1880 	zassert_equal(DT_INST_CLOCKS_CELL(0, bits), 7, "");
1881 	zassert_equal(DT_INST_CLOCKS_CELL(0, bus), 3, "");
1882 
1883 	/* clock-freq on fixed clock */
1884 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, clocks, 1,
1885 						  clock_frequency),
1886 		      25000000, "");
1887 }
1888 
1889 #undef DT_DRV_COMPAT
1890 #define DT_DRV_COMPAT vnd_spi_device
ZTEST(devicetree_api,test_parent)1891 ZTEST(devicetree_api, test_parent)
1892 {
1893 	zassert_true(DT_SAME_NODE(DT_PARENT(TEST_SPI_DEV_0), TEST_SPI_BUS_0), "");
1894 
1895 	/*
1896 	 * The parent's label for the first instance of vnd,spi-device,
1897 	 * child of TEST_SPI, is the same as TEST_SPI.
1898 	 */
1899 	zassert_true(DT_SAME_NODE(DT_INST_PARENT(0), TEST_SPI), "");
1900 	/*
1901 	 * We should be able to use DT_PARENT() even with nodes, like /test,
1902 	 * that have no matching compatible.
1903 	 */
1904 	zassert_true(DT_SAME_NODE(DT_CHILD(DT_PARENT(TEST_SPI_BUS_0), spi_33334444),
1905 				  TEST_SPI_BUS_0), "");
1906 }
1907 
1908 #undef DT_DRV_COMPAT
1909 #define DT_DRV_COMPAT vnd_i2c_mux_controller
ZTEST(devicetree_api,test_gparent)1910 ZTEST(devicetree_api, test_gparent)
1911 {
1912 	zassert_true(DT_SAME_NODE(DT_GPARENT(TEST_I2C_MUX_CTLR_1), TEST_I2C), "");
1913 	zassert_true(DT_SAME_NODE(DT_INST_GPARENT(0), TEST_I2C), "");
1914 	zassert_true(DT_SAME_NODE(DT_INST_GPARENT(1), TEST_I2C), "");
1915 }
1916 
1917 #undef DT_DRV_COMPAT
1918 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_children)1919 ZTEST(devicetree_api, test_children)
1920 {
1921 	zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_a),
1922 			      val), 0, "");
1923 	zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_b),
1924 			      val), 1, "");
1925 	zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_c),
1926 			      val), 2, "");
1927 
1928 	zassert_equal(DT_PROP(DT_INST_CHILD(0, child_a), val), 0, "");
1929 	zassert_equal(DT_PROP(DT_INST_CHILD(0, child_b), val), 1, "");
1930 	zassert_equal(DT_PROP(DT_INST_CHILD(0, child_c), val), 2, "");
1931 }
1932 
1933 #undef DT_DRV_COMPAT
1934 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_child_nodes_list)1935 ZTEST(devicetree_api, test_child_nodes_list)
1936 {
1937 	#define TEST_FUNC(child) { DT_PROP(child, val) }
1938 	#define TEST_FUNC_AND_COMMA(child) TEST_FUNC(child),
1939 	#define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
1940 
1941 	struct vnd_child_binding {
1942 		int val;
1943 	};
1944 
1945 	struct vnd_child_binding vals[] = {
1946 		DT_FOREACH_CHILD(TEST_PARENT, TEST_FUNC_AND_COMMA)
1947 	};
1948 
1949 	struct vnd_child_binding vals_sep[] = {
1950 		DT_FOREACH_CHILD_SEP(TEST_PARENT, TEST_FUNC, (,))
1951 	};
1952 
1953 	struct vnd_child_binding vals_inst[] = {
1954 		DT_INST_FOREACH_CHILD(0, TEST_FUNC_AND_COMMA)
1955 	};
1956 
1957 	struct vnd_child_binding vals_inst_sep[] = {
1958 		DT_INST_FOREACH_CHILD_SEP(0, TEST_FUNC, (,))
1959 	};
1960 
1961 	struct vnd_child_binding vals_status_okay[] = {
1962 		DT_FOREACH_CHILD_STATUS_OKAY(TEST_PARENT, TEST_FUNC_AND_COMMA)
1963 	};
1964 
1965 	struct vnd_child_binding vals_status_okay_inst[] = {
1966 		DT_INST_FOREACH_CHILD_STATUS_OKAY(0, TEST_FUNC_AND_COMMA)
1967 	};
1968 
1969 	struct vnd_child_binding vals_status_okay_sep[] = {
1970 		DT_FOREACH_CHILD_STATUS_OKAY_SEP(TEST_PARENT, TEST_FUNC, (,))
1971 	};
1972 
1973 	struct vnd_child_binding vals_status_okay_inst_sep[] = {
1974 		DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TEST_FUNC, (,))
1975 	};
1976 
1977 	zassert_equal(ARRAY_SIZE(vals), 3, "");
1978 	zassert_equal(ARRAY_SIZE(vals_sep), 3, "");
1979 	zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
1980 	zassert_equal(ARRAY_SIZE(vals_inst_sep), 3, "");
1981 	zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
1982 	zassert_equal(ARRAY_SIZE(vals_status_okay_inst), 2, "");
1983 	zassert_equal(ARRAY_SIZE(vals_status_okay_sep), 2, "");
1984 	zassert_equal(ARRAY_SIZE(vals_status_okay_inst_sep), 2, "");
1985 
1986 	zassert_equal(vals[0].val, 0, "");
1987 	zassert_equal(vals[1].val, 1, "");
1988 	zassert_equal(vals[2].val, 2, "");
1989 	zassert_equal(vals_sep[0].val, 0, "");
1990 	zassert_equal(vals_sep[1].val, 1, "");
1991 	zassert_equal(vals_sep[2].val, 2, "");
1992 	zassert_equal(vals_inst[0].val, 0, "");
1993 	zassert_equal(vals_inst[1].val, 1, "");
1994 	zassert_equal(vals_inst[2].val, 2, "");
1995 	zassert_equal(vals_inst_sep[0].val, 0, "");
1996 	zassert_equal(vals_inst_sep[1].val, 1, "");
1997 	zassert_equal(vals_inst_sep[2].val, 2, "");
1998 	zassert_equal(vals_status_okay[0].val, 0, "");
1999 	zassert_equal(vals_status_okay[1].val, 1, "");
2000 	zassert_equal(vals_status_okay_inst[0].val, 0, "");
2001 	zassert_equal(vals_status_okay_inst[1].val, 1, "");
2002 	zassert_equal(vals_status_okay_sep[0].val, 0, "");
2003 	zassert_equal(vals_status_okay_sep[1].val, 1, "");
2004 	zassert_equal(vals_status_okay_inst_sep[0].val, 0, "");
2005 	zassert_equal(vals_status_okay_inst_sep[1].val, 1, "");
2006 
2007 	#undef TEST_PARENT
2008 	#undef TEST_FUNC_AND_COMMA
2009 	#undef TEST_FUNC
2010 }
2011 
2012 #undef DT_DRV_COMPAT
2013 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_child_nodes_list_varg)2014 ZTEST(devicetree_api, test_child_nodes_list_varg)
2015 {
2016 	#define TEST_FUNC(child, arg) { DT_PROP(child, val) + arg }
2017 	#define TEST_FUNC_AND_COMMA(child, arg) TEST_FUNC(child, arg),
2018 	#define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
2019 
2020 	struct vnd_child_binding {
2021 		int val;
2022 	};
2023 
2024 	struct vnd_child_binding vals[] = {
2025 		DT_FOREACH_CHILD_VARGS(TEST_PARENT, TEST_FUNC_AND_COMMA, 1)
2026 	};
2027 
2028 	struct vnd_child_binding vals_sep[] = {
2029 		DT_FOREACH_CHILD_SEP_VARGS(TEST_PARENT, TEST_FUNC, (,), 1)
2030 	};
2031 
2032 	struct vnd_child_binding vals_inst[] = {
2033 		DT_INST_FOREACH_CHILD_VARGS(0, TEST_FUNC_AND_COMMA, 1)
2034 	};
2035 
2036 	struct vnd_child_binding vals_inst_sep[] = {
2037 		DT_INST_FOREACH_CHILD_SEP_VARGS(0, TEST_FUNC, (,), 1)
2038 	};
2039 
2040 	struct vnd_child_binding vals_status_okay[] = {
2041 		DT_FOREACH_CHILD_STATUS_OKAY_VARGS(TEST_PARENT, TEST_FUNC_AND_COMMA, 1)
2042 	};
2043 
2044 	struct vnd_child_binding vals_status_okay_inst[] = {
2045 		DT_INST_FOREACH_CHILD_STATUS_OKAY_VARGS(0, TEST_FUNC_AND_COMMA, 1)
2046 	};
2047 
2048 	struct vnd_child_binding vals_status_okay_sep[] = {
2049 		DT_FOREACH_CHILD_STATUS_OKAY_SEP_VARGS(TEST_PARENT, TEST_FUNC, (,), 1)
2050 	};
2051 
2052 	struct vnd_child_binding vals_status_okay_inst_sep[] = {
2053 		DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP_VARGS(0, TEST_FUNC, (,), 1)
2054 	};
2055 
2056 	zassert_equal(ARRAY_SIZE(vals), 3, "");
2057 	zassert_equal(ARRAY_SIZE(vals_sep), 3, "");
2058 	zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
2059 	zassert_equal(ARRAY_SIZE(vals_inst_sep), 3, "");
2060 	zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
2061 	zassert_equal(ARRAY_SIZE(vals_status_okay_inst), 2, "");
2062 	zassert_equal(ARRAY_SIZE(vals_status_okay_sep), 2, "");
2063 	zassert_equal(ARRAY_SIZE(vals_status_okay_inst_sep), 2, "");
2064 
2065 	zassert_equal(vals[0].val, 1, "");
2066 	zassert_equal(vals[1].val, 2, "");
2067 	zassert_equal(vals[2].val, 3, "");
2068 	zassert_equal(vals_sep[0].val, 1, "");
2069 	zassert_equal(vals_sep[1].val, 2, "");
2070 	zassert_equal(vals_sep[2].val, 3, "");
2071 	zassert_equal(vals_inst[0].val, 1, "");
2072 	zassert_equal(vals_inst[1].val, 2, "");
2073 	zassert_equal(vals_inst[2].val, 3, "");
2074 	zassert_equal(vals_inst_sep[0].val, 1, "");
2075 	zassert_equal(vals_inst_sep[1].val, 2, "");
2076 	zassert_equal(vals_inst_sep[2].val, 3, "");
2077 	zassert_equal(vals_status_okay[0].val, 1, "");
2078 	zassert_equal(vals_status_okay[1].val, 2, "");
2079 	zassert_equal(vals_status_okay_inst[0].val, 1, "");
2080 	zassert_equal(vals_status_okay_inst[1].val, 2, "");
2081 	zassert_equal(vals_status_okay_sep[0].val, 1, "");
2082 	zassert_equal(vals_status_okay_sep[1].val, 2, "");
2083 	zassert_equal(vals_status_okay_inst_sep[0].val, 1, "");
2084 	zassert_equal(vals_status_okay_inst_sep[1].val, 2, "");
2085 
2086 	#undef TEST_PARENT
2087 	#undef TEST_FUNC_AND_COMMA
2088 	#undef TEST_FUNC
2089 }
2090 
ZTEST(devicetree_api,test_great_grandchild)2091 ZTEST(devicetree_api, test_great_grandchild)
2092 {
2093 	zassert_equal(DT_PROP(DT_NODELABEL(test_ggc), ggc_prop), 42, "");
2094 }
2095 
2096 #undef DT_DRV_COMPAT
2097 #define DT_DRV_COMPAT vnd_test_ranges_pcie
ZTEST(devicetree_api,test_ranges_pcie)2098 ZTEST(devicetree_api, test_ranges_pcie)
2099 {
2100 #define FLAGS(node_id, idx)				\
2101 	DT_RANGES_CHILD_BUS_FLAGS_BY_IDX(node_id, idx),
2102 #define CHILD_BUS_ADDR(node_id, idx)				\
2103 	DT_RANGES_CHILD_BUS_ADDRESS_BY_IDX(node_id, idx),
2104 #define PARENT_BUS_ADDR(node_id, idx)				\
2105 	DT_RANGES_PARENT_BUS_ADDRESS_BY_IDX(node_id, idx),
2106 #define LENGTH(node_id, idx) DT_RANGES_LENGTH_BY_IDX(node_id, idx),
2107 
2108 	unsigned int count = DT_NUM_RANGES(TEST_RANGES_PCIE);
2109 
2110 	const uint64_t ranges_pcie_flags[] = {
2111 		DT_FOREACH_RANGE(TEST_RANGES_PCIE, FLAGS)
2112 	};
2113 
2114 	const uint64_t ranges_child_bus_addr[] = {
2115 		DT_FOREACH_RANGE(TEST_RANGES_PCIE, CHILD_BUS_ADDR)
2116 	};
2117 
2118 	const uint64_t ranges_parent_bus_addr[] = {
2119 		DT_FOREACH_RANGE(TEST_RANGES_PCIE, PARENT_BUS_ADDR)
2120 	};
2121 
2122 	const uint64_t ranges_length[] = {
2123 		DT_FOREACH_RANGE(TEST_RANGES_PCIE, LENGTH)
2124 	};
2125 
2126 	zassert_equal(count, 3, "");
2127 
2128 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 0), 1, "");
2129 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 1), 1, "");
2130 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 2), 1, "");
2131 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 3), 0, "");
2132 
2133 	zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 0),
2134 		      1, "");
2135 	zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 1),
2136 		      1, "");
2137 	zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 2),
2138 		      1, "");
2139 	zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 3),
2140 		      0, "");
2141 
2142 	zassert_equal(ranges_pcie_flags[0], 0x1000000, "");
2143 	zassert_equal(ranges_pcie_flags[1], 0x2000000, "");
2144 	zassert_equal(ranges_pcie_flags[2], 0x3000000, "");
2145 	zassert_equal(ranges_child_bus_addr[0], 0, "");
2146 	zassert_equal(ranges_child_bus_addr[1], 0x10000000, "");
2147 	zassert_equal(ranges_child_bus_addr[2], 0x8000000000, "");
2148 	zassert_equal(ranges_parent_bus_addr[0], 0x3eff0000, "");
2149 	zassert_equal(ranges_parent_bus_addr[1], 0x10000000, "");
2150 	zassert_equal(ranges_parent_bus_addr[2], 0x8000000000, "");
2151 	zassert_equal(ranges_length[0], 0x10000, "");
2152 	zassert_equal(ranges_length[1], 0x2eff0000, "");
2153 	zassert_equal(ranges_length[2], 0x8000000000, "");
2154 
2155 #undef FLAGS
2156 #undef CHILD_BUS_ADDR
2157 #undef PARENT_BUS_ADDR
2158 #undef LENGTH
2159 }
2160 
ZTEST(devicetree_api,test_ranges_other)2161 ZTEST(devicetree_api, test_ranges_other)
2162 {
2163 #define HAS_FLAGS(node_id, idx) \
2164 	DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(node_id, idx)
2165 #define FLAGS(node_id, idx) \
2166 	DT_RANGES_CHILD_BUS_FLAGS_BY_IDX(node_id, idx),
2167 #define CHILD_BUS_ADDR(node_id, idx) \
2168 	DT_RANGES_CHILD_BUS_ADDRESS_BY_IDX(node_id, idx),
2169 #define PARENT_BUS_ADDR(node_id, idx) \
2170 	DT_RANGES_PARENT_BUS_ADDRESS_BY_IDX(node_id, idx),
2171 #define LENGTH(node_id, idx) DT_RANGES_LENGTH_BY_IDX(node_id, idx),
2172 
2173 	unsigned int count = DT_NUM_RANGES(TEST_RANGES_OTHER);
2174 
2175 	const uint32_t ranges_child_bus_addr[] = {
2176 		DT_FOREACH_RANGE(TEST_RANGES_OTHER, CHILD_BUS_ADDR)
2177 	};
2178 
2179 	const uint32_t ranges_parent_bus_addr[] = {
2180 		DT_FOREACH_RANGE(TEST_RANGES_OTHER, PARENT_BUS_ADDR)
2181 	};
2182 
2183 	const uint32_t ranges_length[] = {
2184 		DT_FOREACH_RANGE(TEST_RANGES_OTHER, LENGTH)
2185 	};
2186 
2187 	zassert_equal(count, 2, "");
2188 
2189 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 0), 1, "");
2190 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 1), 1, "");
2191 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 2), 0, "");
2192 	zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 3), 0, "");
2193 
2194 	zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 0), 0, "");
2195 	zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 1), 0, "");
2196 	zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 2), 0, "");
2197 	zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 3), 0, "");
2198 
2199 	zassert_equal(ranges_child_bus_addr[0], 0, "");
2200 	zassert_equal(ranges_child_bus_addr[1], 0x10000000, "");
2201 	zassert_equal(ranges_parent_bus_addr[0], 0x3eff0000, "");
2202 	zassert_equal(ranges_parent_bus_addr[1], 0x10000000, "");
2203 	zassert_equal(ranges_length[0], 0x10000, "");
2204 	zassert_equal(ranges_length[1], 0x2eff0000, "");
2205 
2206 #undef HAS_FLAGS
2207 #undef FLAGS
2208 #undef CHILD_BUS_ADDR
2209 #undef PARENT_BUS_ADDR
2210 #undef LENGTH
2211 }
2212 
ZTEST(devicetree_api,test_compat_get_any_status_okay)2213 ZTEST(devicetree_api, test_compat_get_any_status_okay)
2214 {
2215 	zassert_true(
2216 		DT_SAME_NODE(
2217 			DT_COMPAT_GET_ANY_STATUS_OKAY(vnd_reg_holder),
2218 			TEST_REG),
2219 		"");
2220 
2221 	/*
2222 	 * DT_SAME_NODE requires that both its arguments are valid
2223 	 * node identifiers, so we can't pass it DT_INVALID_NODE,
2224 	 * which is what this DT_COMPAT_GET_ANY_STATUS_OKAY() expands to.
2225 	 */
2226 	zassert_false(
2227 		DT_NODE_EXISTS(
2228 			DT_COMPAT_GET_ANY_STATUS_OKAY(this_is_not_a_real_compat)),
2229 		"");
2230 }
2231 
ord_in_array(unsigned int ord,unsigned int * array,size_t array_size)2232 static bool ord_in_array(unsigned int ord, unsigned int *array,
2233 			 size_t array_size)
2234 {
2235 	size_t i;
2236 
2237 	for (i = 0; i < array_size; i++) {
2238 		if (array[i] == ord) {
2239 			return true;
2240 		}
2241 	}
2242 
2243 	return false;
2244 }
2245 
2246 /* Magic numbers used by COMBINED_ORD_ARRAY. Must be invalid dependency
2247  * ordinals.
2248  */
2249 #define ORD_LIST_SEP		0xFFFF0000
2250 #define ORD_LIST_END		0xFFFF0001
2251 #define INJECTED_DEP_0		0xFFFF0002
2252 #define INJECTED_DEP_1		0xFFFF0003
2253 
2254 #define DEP_ORD_AND_COMMA(node_id) DT_DEP_ORD(node_id),
2255 #define CHILD_ORDINALS(node_id) DT_FOREACH_CHILD(node_id, DEP_ORD_AND_COMMA)
2256 
2257 #define COMBINED_ORD_ARRAY(node_id)		\
2258 	{					\
2259 		DT_DEP_ORD(node_id),		\
2260 		DT_DEP_ORD(DT_PARENT(node_id)),	\
2261 		CHILD_ORDINALS(node_id)		\
2262 		ORD_LIST_SEP,			\
2263 		DT_REQUIRES_DEP_ORDS(node_id)	\
2264 		INJECTED_DEP_0,			\
2265 		INJECTED_DEP_1,			\
2266 		ORD_LIST_SEP,			\
2267 		DT_SUPPORTS_DEP_ORDS(node_id)	\
2268 		ORD_LIST_END			\
2269 	}
2270 
ZTEST(devicetree_api,test_dep_ord)2271 ZTEST(devicetree_api, test_dep_ord)
2272 {
2273 #define ORD_IN_ARRAY(ord, array) ord_in_array(ord, array, ARRAY_SIZE(array))
2274 
2275 	unsigned int root_ord = DT_DEP_ORD(DT_ROOT),
2276 		test_ord = DT_DEP_ORD(DT_PATH(test)),
2277 		root_requires[] = { DT_REQUIRES_DEP_ORDS(DT_ROOT) },
2278 		test_requires[] = { DT_REQUIRES_DEP_ORDS(DT_PATH(test)) },
2279 		root_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_ROOT) },
2280 		test_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_PATH(test)) },
2281 		children_ords[] = {
2282 			DT_FOREACH_CHILD(TEST_CHILDREN, DEP_ORD_AND_COMMA)
2283 		},
2284 		children_combined_ords[] = COMBINED_ORD_ARRAY(TEST_CHILDREN),
2285 		child_a_combined_ords[] =
2286 			COMBINED_ORD_ARRAY(DT_NODELABEL(test_child_a));
2287 	size_t i;
2288 
2289 	/* DT_DEP_ORD */
2290 	zassert_equal(root_ord, 0, "");
2291 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_child_a)) >
2292 		     DT_DEP_ORD(DT_NODELABEL(test_children)), "");
2293 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_irq)) >
2294 		     DT_DEP_ORD(DT_NODELABEL(test_intc)), "");
2295 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_phandles)) >
2296 		     DT_DEP_ORD(DT_NODELABEL(test_gpio_1)), "");
2297 
2298 	/* DT_REQUIRES_DEP_ORDS */
2299 	zassert_equal(ARRAY_SIZE(root_requires), 0, "");
2300 	zassert_true(ORD_IN_ARRAY(root_ord, test_requires), "");
2301 
2302 	/* DT_SUPPORTS_DEP_ORDS */
2303 	zassert_true(ORD_IN_ARRAY(test_ord, root_supports), "");
2304 	zassert_false(ORD_IN_ARRAY(root_ord, test_supports), "");
2305 
2306 	unsigned int children_combined_ords_expected[] = {
2307 		/*
2308 		 * Combined ordinals for /test/test-children are from
2309 		 * these nodes in this order:
2310 		 */
2311 		DT_DEP_ORD(TEST_CHILDREN),		/* node */
2312 		DT_DEP_ORD(DT_PATH(test)),		/* parent */
2313 		DT_DEP_ORD(DT_NODELABEL(test_child_a)),	/* children */
2314 		DT_DEP_ORD(DT_NODELABEL(test_child_b)),
2315 		DT_DEP_ORD(DT_NODELABEL(test_child_c)),
2316 		ORD_LIST_SEP,				/* separator */
2317 		DT_DEP_ORD(DT_PATH(test)),		/* requires */
2318 		INJECTED_DEP_0,				/* injected
2319 							 * dependencies
2320 							 */
2321 		INJECTED_DEP_1,
2322 		ORD_LIST_SEP,				/* separator */
2323 		DT_DEP_ORD(DT_NODELABEL(test_child_a)),	/* supports */
2324 		DT_DEP_ORD(DT_NODELABEL(test_child_b)),
2325 		DT_DEP_ORD(DT_NODELABEL(test_child_c)),
2326 		ORD_LIST_END,				/* terminator */
2327 	};
2328 	zassert_equal(ARRAY_SIZE(children_combined_ords),
2329 		      ARRAY_SIZE(children_combined_ords_expected),
2330 		      "%zu", ARRAY_SIZE(children_combined_ords));
2331 	for (i = 0; i < ARRAY_SIZE(children_combined_ords); i++) {
2332 		zassert_equal(children_combined_ords[i],
2333 			      children_combined_ords_expected[i],
2334 			      "test-children at %zu", i);
2335 	}
2336 
2337 	unsigned int child_a_combined_ords_expected[] = {
2338 		/*
2339 		 * Combined ordinals for /test/test-children/child-a
2340 		 * are from these nodes in this order:
2341 		 */
2342 		DT_DEP_ORD(DT_NODELABEL(test_child_a)), /* node */
2343 		DT_DEP_ORD(TEST_CHILDREN),		/* parent */
2344 		/* children (none) */
2345 		ORD_LIST_SEP,				/* separator */
2346 		DT_DEP_ORD(TEST_CHILDREN),		/* requires */
2347 		INJECTED_DEP_0,				/* injected
2348 							 * dependencies
2349 							 */
2350 		INJECTED_DEP_1,
2351 		ORD_LIST_SEP,				/* separator */
2352 		/* supports (none) */
2353 		ORD_LIST_END,				/* terminator */
2354 	};
2355 	zassert_equal(ARRAY_SIZE(child_a_combined_ords),
2356 		      ARRAY_SIZE(child_a_combined_ords_expected),
2357 		      "%zu", ARRAY_SIZE(child_a_combined_ords));
2358 	for (i = 0; i < ARRAY_SIZE(child_a_combined_ords); i++) {
2359 		zassert_equal(child_a_combined_ords[i],
2360 			      child_a_combined_ords_expected[i],
2361 			      "child-a at %zu", i);
2362 	}
2363 
2364 #undef DT_DRV_COMPAT
2365 #define DT_DRV_COMPAT vnd_child_bindings
2366 
2367 	/* DT_INST_DEP_ORD */
2368 	zassert_equal(DT_INST_DEP_ORD(0),
2369 		      DT_DEP_ORD(DT_NODELABEL(test_children)), "");
2370 
2371 	/* DT_INST_REQUIRES_DEP_ORDS */
2372 	unsigned int inst_requires[] = { DT_INST_REQUIRES_DEP_ORDS(0) };
2373 
2374 	zassert_equal(ARRAY_SIZE(inst_requires), 1, "");
2375 	zassert_equal(inst_requires[0], test_ord, "");
2376 
2377 	/* DT_INST_SUPPORTS_DEP_ORDS */
2378 	unsigned int inst_supports[] = { DT_INST_SUPPORTS_DEP_ORDS(0) };
2379 
2380 	zassert_equal(ARRAY_SIZE(inst_supports), 3, "");
2381 	for (i = 0; i < ARRAY_SIZE(inst_supports); i++) {
2382 		zassert_true(ORD_IN_ARRAY(inst_supports[i], children_ords), "");
2383 	}
2384 }
2385 
ZTEST(devicetree_api,test_path)2386 ZTEST(devicetree_api, test_path)
2387 {
2388 	zassert_true(!strcmp(DT_NODE_PATH(DT_ROOT), "/"), "");
2389 	zassert_true(!strcmp(DT_NODE_PATH(TEST_DEADBEEF),
2390 			     "/test/gpio@deadbeef"), "");
2391 }
2392 
ZTEST(devicetree_api,test_node_name)2393 ZTEST(devicetree_api, test_node_name)
2394 {
2395 	zassert_true(!strcmp(DT_NODE_FULL_NAME(DT_ROOT), "/"), "");
2396 	zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_DEADBEEF),
2397 			     "gpio@deadbeef"), "");
2398 	zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_TEMP),
2399 			     "temperature-sensor"), "");
2400 	zassert_true(strcmp(DT_NODE_FULL_NAME(TEST_REG),
2401 			     "reg-holder"), "");
2402 }
2403 
ZTEST(devicetree_api,test_node_child_idx)2404 ZTEST(devicetree_api, test_node_child_idx)
2405 {
2406 	zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_a)), 0, "");
2407 	zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_b)), 1, "");
2408 	zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_c)), 2, "");
2409 }
2410 
ZTEST(devicetree_api,test_same_node)2411 ZTEST(devicetree_api, test_same_node)
2412 {
2413 	zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
2414 	zassert_false(DT_SAME_NODE(TEST_DEADBEEF, TEST_ABCD1234), "");
2415 }
2416 
ZTEST(devicetree_api,test_pinctrl)2417 ZTEST(devicetree_api, test_pinctrl)
2418 {
2419 #undef DT_DRV_COMPAT
2420 #define DT_DRV_COMPAT vnd_adc_temp_sensor
2421 	/*
2422 	 * Tests when a node does have pinctrl properties.
2423 	 */
2424 
2425 	/*
2426 	 * node_id versions:
2427 	 */
2428 
2429 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 0, 1),
2430 				  DT_NODELABEL(test_pincfg_b)), "");
2431 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 1, 0),
2432 				  DT_NODELABEL(test_pincfg_c)), "");
2433 
2434 	zassert_true(DT_SAME_NODE(DT_PINCTRL_0(TEST_TEMP, 0),
2435 				  DT_NODELABEL(test_pincfg_a)), "");
2436 
2437 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, default, 1),
2438 				  DT_NODELABEL(test_pincfg_b)), "");
2439 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, sleep, 0),
2440 				  DT_NODELABEL(test_pincfg_c)), "");
2441 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, f_o_o2, 0),
2442 				  DT_NODELABEL(test_pincfg_d)), "");
2443 
2444 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, default), 0, "");
2445 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, sleep), 1, "");
2446 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, f_o_o2), 2, "");
2447 
2448 	zassert_equal(DT_NUM_PINCTRLS_BY_IDX(TEST_TEMP, 0), 2, "");
2449 
2450 	zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, default), 2, "");
2451 	zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, f_o_o2), 1, "");
2452 
2453 	zassert_equal(DT_NUM_PINCTRL_STATES(TEST_TEMP), 3, "");
2454 
2455 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 0), 1, "");
2456 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 1), 1, "");
2457 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 2), 1, "");
2458 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 3), 0, "");
2459 
2460 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, default), 1, "");
2461 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, sleep), 1, "");
2462 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, f_o_o2), 1, "");
2463 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, bar), 0, "");
2464 
2465 #undef MAKE_TOKEN
2466 #define MAKE_TOKEN(pc_idx)						\
2467 	_CONCAT(NODE_ID_ENUM_,						\
2468 		DT_PINCTRL_IDX_TO_NAME_TOKEN(TEST_TEMP, pc_idx))
2469 #undef MAKE_UPPER_TOKEN
2470 #define MAKE_UPPER_TOKEN(pc_idx)					\
2471 	_CONCAT(NODE_ID_ENUM_,						\
2472 		DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(TEST_TEMP, pc_idx))
2473 	enum {
2474 		MAKE_TOKEN(0) = 10,
2475 		MAKE_TOKEN(1) = 11,
2476 		MAKE_TOKEN(2) = 12,
2477 		MAKE_TOKEN(3) = 13,
2478 
2479 		MAKE_UPPER_TOKEN(0) = 20,
2480 		MAKE_UPPER_TOKEN(1) = 21,
2481 		MAKE_UPPER_TOKEN(2) = 22,
2482 		MAKE_UPPER_TOKEN(3) = 23,
2483 	};
2484 
2485 	zassert_equal(NODE_ID_ENUM_default, 10, "");
2486 	zassert_equal(NODE_ID_ENUM_sleep, 11, "");
2487 	zassert_equal(NODE_ID_ENUM_f_o_o2, 12, "");
2488 
2489 	zassert_equal(NODE_ID_ENUM_DEFAULT, 20, "");
2490 	zassert_equal(NODE_ID_ENUM_SLEEP, 21, "");
2491 	zassert_equal(NODE_ID_ENUM_F_O_O2, 22, "");
2492 
2493 	/*
2494 	 * inst versions:
2495 	 */
2496 
2497 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 0, 1),
2498 				  DT_NODELABEL(test_pincfg_b)), "");
2499 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 1, 0),
2500 				  DT_NODELABEL(test_pincfg_c)), "");
2501 
2502 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_0(0, 0),
2503 				  DT_NODELABEL(test_pincfg_a)), "");
2504 
2505 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, default, 1),
2506 				  DT_NODELABEL(test_pincfg_b)), "");
2507 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, sleep, 0),
2508 				  DT_NODELABEL(test_pincfg_c)), "");
2509 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, f_o_o2, 0),
2510 				  DT_NODELABEL(test_pincfg_d)), "");
2511 
2512 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, default), 0, "");
2513 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, sleep), 1, "");
2514 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, f_o_o2), 2, "");
2515 
2516 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_IDX(0, 0), 2, "");
2517 
2518 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, default), 2, "");
2519 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, f_o_o2), 1, "");
2520 
2521 	zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 3, "");
2522 
2523 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 1, "");
2524 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 1), 1, "");
2525 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 2), 1, "");
2526 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 3), 0, "");
2527 
2528 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, default), 1, "");
2529 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, sleep), 1, "");
2530 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 1, "");
2531 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, bar), 0, "");
2532 
2533 #undef MAKE_TOKEN
2534 #define MAKE_TOKEN(pc_idx)						\
2535 	_CONCAT(INST_ENUM_,						\
2536 			DT_INST_PINCTRL_IDX_TO_NAME_TOKEN(0, pc_idx))
2537 #undef MAKE_UPPER_TOKEN
2538 #define MAKE_UPPER_TOKEN(pc_idx)					\
2539 	_CONCAT(INST_ENUM_,						\
2540 			DT_INST_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(0, pc_idx))
2541 	enum {
2542 		MAKE_TOKEN(0) = 10,
2543 		MAKE_TOKEN(1) = 11,
2544 		MAKE_TOKEN(2) = 12,
2545 
2546 		MAKE_UPPER_TOKEN(0) = 20,
2547 		MAKE_UPPER_TOKEN(1) = 21,
2548 		MAKE_UPPER_TOKEN(2) = 22,
2549 	};
2550 
2551 	zassert_equal(INST_ENUM_default, 10, "");
2552 	zassert_equal(INST_ENUM_sleep, 11, "");
2553 	zassert_equal(INST_ENUM_f_o_o2, 12, "");
2554 
2555 	zassert_equal(INST_ENUM_DEFAULT, 20, "");
2556 	zassert_equal(INST_ENUM_SLEEP, 21, "");
2557 	zassert_equal(INST_ENUM_F_O_O2, 22, "");
2558 
2559 #undef DT_DRV_COMPAT
2560 #define DT_DRV_COMPAT vnd_reg_holder
2561 	/*
2562 	 * Tests when a node does NOT have any pinctrl properties.
2563 	 */
2564 
2565 	/* node_id versions */
2566 	zassert_equal(DT_NUM_PINCTRL_STATES(TEST_REG), 0, "");
2567 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_REG, 0), 0, "");
2568 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_REG, f_o_o2), 0, "");
2569 
2570 	/* inst versions */
2571 	zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 0, "");
2572 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 0, "");
2573 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 0, "");
2574 }
2575 
2576 DEVICE_DT_DEFINE(DT_NODELABEL(test_mbox), NULL, NULL, NULL, NULL, POST_KERNEL,
2577 		 90, NULL);
2578 DEVICE_DT_DEFINE(DT_NODELABEL(test_mbox_zero_cell), NULL, NULL, NULL, NULL,
2579 		 POST_KERNEL, 90, NULL);
2580 
ZTEST(devicetree_api,test_mbox)2581 ZTEST(devicetree_api, test_mbox)
2582 {
2583 #undef DT_DRV_COMPAT
2584 #define DT_DRV_COMPAT vnd_adc_temp_sensor
2585 
2586 	const struct mbox_channel channel_tx = MBOX_DT_CHANNEL_GET(TEST_TEMP, tx);
2587 	const struct mbox_channel channel_rx = MBOX_DT_CHANNEL_GET(TEST_TEMP, rx);
2588 
2589 	zassert_equal(channel_tx.id, 1, "");
2590 	zassert_equal(channel_rx.id, 2, "");
2591 
2592 	zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, tx), 1, "");
2593 	zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, rx), 2, "");
2594 
2595 	zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, tx),
2596 				  DT_NODELABEL(test_mbox)), "");
2597 	zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, rx),
2598 				  DT_NODELABEL(test_mbox)), "");
2599 
2600 	zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, tx), 1, "");
2601 	zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, rx), 2, "");
2602 
2603 	const struct mbox_channel channel_zero = MBOX_DT_CHANNEL_GET(TEST_TEMP, zero);
2604 
2605 	zassert_equal(channel_zero.id, 0, "");
2606 
2607 	zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, zero), 0, "");
2608 
2609 	zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, zero),
2610 				  DT_NODELABEL(test_mbox_zero_cell)), "");
2611 }
2612 
ZTEST(devicetree_api,test_string_token)2613 ZTEST(devicetree_api, test_string_token)
2614 {
2615 #undef DT_DRV_COMPAT
2616 #define DT_DRV_COMPAT vnd_string_token
2617 	enum {
2618 		token_zero,
2619 		token_one,
2620 		token_two,
2621 		token_no_inst,
2622 	};
2623 	enum {
2624 		TOKEN_ZERO = token_no_inst + 1,
2625 		TOKEN_ONE,
2626 		TOKEN_TWO,
2627 		TOKEN_NO_INST,
2628 	};
2629 	int i;
2630 
2631 	/* Test DT_INST_STRING_TOKEN */
2632 #define STRING_TOKEN_TEST_INST_EXPANSION(inst) \
2633 	DT_INST_STRING_TOKEN(inst, val),
2634 	int array_inst[] = {
2635 		DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_TEST_INST_EXPANSION)
2636 	};
2637 
2638 	for (i = 0; i < ARRAY_SIZE(array_inst); i++) {
2639 		zassert_between_inclusive(array_inst[i], token_zero, token_two, "");
2640 	}
2641 
2642 	/* Test DT_INST_STRING_UPPER_TOKEN */
2643 #define STRING_UPPER_TOKEN_TEST_INST_EXPANSION(inst) \
2644 	DT_INST_STRING_UPPER_TOKEN(inst, val),
2645 	int array_inst_upper[] = {
2646 		DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_TEST_INST_EXPANSION)
2647 	};
2648 
2649 	for (i = 0; i < ARRAY_SIZE(array_inst_upper); i++) {
2650 		zassert_between_inclusive(array_inst_upper[i], TOKEN_ZERO, TOKEN_TWO, "");
2651 	}
2652 
2653 	/* Test DT_INST_STRING_TOKEN_OR when property is found */
2654 #define STRING_TOKEN_OR_TEST_INST_EXPANSION(inst) \
2655 	DT_INST_STRING_TOKEN_OR(inst, val, token_no_inst),
2656 	int array_inst_or[] = {
2657 		DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_OR_TEST_INST_EXPANSION)
2658 	};
2659 
2660 	for (i = 0; i < ARRAY_SIZE(array_inst_or); i++) {
2661 		zassert_between_inclusive(array_inst_or[i], token_zero, token_two, "");
2662 	}
2663 
2664 	/* Test DT_INST_STRING_UPPER_TOKEN_OR when property is found */
2665 #define STRING_UPPER_TOKEN_OR_TEST_INST_EXPANSION(inst)	\
2666 	DT_INST_STRING_UPPER_TOKEN_OR(inst, val, TOKEN_NO_INST),
2667 	int array_inst_upper_or[] = {
2668 		DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_OR_TEST_INST_EXPANSION)
2669 	};
2670 
2671 	for (i = 0; i < ARRAY_SIZE(array_inst_upper_or); i++) {
2672 		zassert_between_inclusive(array_inst_upper_or[i], TOKEN_ZERO, TOKEN_TWO, "");
2673 	}
2674 
2675 	/* Test DT_STRING_TOKEN_OR when property is found */
2676 	zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_0),
2677 					 val, token_one), token_zero, "");
2678 	zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_1),
2679 					 val, token_two), token_one, "");
2680 
2681 	/* Test DT_STRING_TOKEN_OR is not found */
2682 	zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_1),
2683 					 no_inst, token_zero), token_zero, "");
2684 
2685 	/* Test DT_STRING_UPPER_TOKEN_OR when property is found */
2686 	zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_0),
2687 					       val, TOKEN_ONE), TOKEN_ZERO, "");
2688 	zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_1),
2689 					       val, TOKEN_TWO), TOKEN_ONE, "");
2690 
2691 	/* Test DT_STRING_UPPER_TOKEN_OR is not found */
2692 	zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_1),
2693 					       no_inst, TOKEN_ZERO), TOKEN_ZERO, "");
2694 
2695 	/* Test DT_INST_STRING_TOKEN_OR when property is not found */
2696 #define STRING_TOKEN_TEST_NO_INST_EXPANSION(inst) \
2697 	DT_INST_STRING_TOKEN_OR(inst, no_inst, token_no_inst),
2698 	int array_no_inst_or[] = {
2699 		DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_TEST_NO_INST_EXPANSION)
2700 	};
2701 	for (i = 0; i < ARRAY_SIZE(array_no_inst_or); i++) {
2702 		zassert_equal(array_no_inst_or[i], token_no_inst, "");
2703 	}
2704 
2705 	/* Test DT_INST_STRING_UPPER_TOKEN_OR when property is not found */
2706 #define STRING_UPPER_TOKEN_TEST_NO_INST_EXPANSION(inst)	\
2707 	DT_INST_STRING_UPPER_TOKEN_OR(inst, no_inst, TOKEN_NO_INST),
2708 	int array_no_inst_upper_or[] = {
2709 		DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_TEST_NO_INST_EXPANSION)
2710 	};
2711 	for (i = 0; i < ARRAY_SIZE(array_no_inst_upper_or); i++) {
2712 		zassert_equal(array_no_inst_upper_or[i], TOKEN_NO_INST, "");
2713 	}
2714 }
2715 
2716 #undef DT_DRV_COMPAT
2717 #define DT_DRV_COMPAT vnd_string_array_token
ZTEST(devicetree_api,test_string_idx_token)2718 ZTEST(devicetree_api, test_string_idx_token)
2719 {
2720 	enum token_string_idx {
2721 		/* Tokens */
2722 		token_first_idx_zero,
2723 		token_first_idx_one,
2724 		token_first_idx_two,
2725 		token_second_idx_zero,
2726 		token_second_idx_one,
2727 		token_second_idx_two,
2728 		token_second_idx_three,
2729 		/* Upper tokens */
2730 		TOKEN_FIRST_IDX_ZERO,
2731 		TOKEN_FIRST_IDX_ONE,
2732 		TOKEN_FIRST_IDX_TWO,
2733 		TOKEN_SECOND_IDX_ZERO,
2734 		TOKEN_SECOND_IDX_ONE,
2735 		TOKEN_SECOND_IDX_TWO,
2736 		TOKEN_SECOND_IDX_THREE
2737 	};
2738 
2739 	/* Test direct idx access */
2740 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
2741 			token_first_idx_zero, "");
2742 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
2743 			token_first_idx_one, "");
2744 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
2745 			token_first_idx_two, "");
2746 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
2747 			token_second_idx_zero, "");
2748 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
2749 			token_second_idx_one, "");
2750 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
2751 			token_second_idx_two, "");
2752 	zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
2753 			token_second_idx_three, "");
2754 
2755 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
2756 			TOKEN_FIRST_IDX_ZERO, "");
2757 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
2758 			TOKEN_FIRST_IDX_ONE, "");
2759 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
2760 			TOKEN_FIRST_IDX_TWO, "");
2761 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
2762 			TOKEN_SECOND_IDX_ZERO, "");
2763 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
2764 			TOKEN_SECOND_IDX_ONE, "");
2765 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
2766 			TOKEN_SECOND_IDX_TWO, "");
2767 	zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
2768 			TOKEN_SECOND_IDX_THREE, "");
2769 
2770 	/* Test instances */
2771 #define STRING_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_token_, node_id)
2772 #define STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
2773 	enum token_string_idx STRING_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
2774 		DT_INST_STRING_TOKEN_BY_IDX(inst, val, 0), \
2775 		DT_INST_STRING_TOKEN_BY_IDX(inst, val, 1), \
2776 		DT_INST_STRING_TOKEN_BY_IDX(inst, val, 2)  \
2777 	};
2778 	DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION);
2779 
2780 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
2781 			token_first_idx_zero, "");
2782 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
2783 			token_first_idx_one, "");
2784 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
2785 			token_first_idx_two, "");
2786 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
2787 			token_second_idx_zero, "");
2788 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
2789 			token_second_idx_one, "");
2790 	zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
2791 			token_second_idx_two, "");
2792 
2793 #define STRING_UPPER_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_upper_token, node_id)
2794 #define STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
2795 	enum token_string_idx STRING_UPPER_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
2796 		DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 0), \
2797 		DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 1), \
2798 		DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 2)  \
2799 	};
2800 	DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION);
2801 
2802 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
2803 			TOKEN_FIRST_IDX_ZERO, "");
2804 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
2805 			TOKEN_FIRST_IDX_ONE, "");
2806 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
2807 			TOKEN_FIRST_IDX_TWO, "");
2808 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
2809 			TOKEN_SECOND_IDX_ZERO, "");
2810 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
2811 			TOKEN_SECOND_IDX_ONE, "");
2812 	zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
2813 			TOKEN_SECOND_IDX_TWO, "");
2814 }
2815 
2816 #undef DT_DRV_COMPAT
2817 #define DT_DRV_COMPAT vnd_string_unquoted
ZTEST(devicetree_api,test_string_unquoted)2818 ZTEST(devicetree_api, test_string_unquoted)
2819 {
2820 #define XA 12.0
2821 #define XB 34.0
2822 #define XPLUS +
2823 	const double f0_expected = 0.1234;
2824 	const double f1_expected = 0.9e-3;
2825 	const double delta = 0.1e-4;
2826 
2827 	/* Test DT_STRING_UNQUOTED */
2828 	zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_f0), val),
2829 		       f0_expected, delta, "");
2830 	zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_f1), val),
2831 		       f1_expected, delta, "");
2832 	zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_t), val),
2833 		       XA XPLUS XB, delta, "");
2834 	/* Test DT_STRING_UNQUOTED_OR */
2835 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), val, (0.0)),
2836 		       f0_expected, delta, "");
2837 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), val, (0.0)),
2838 		       f1_expected, delta, "");
2839 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), val, (0.0)),
2840 		       XA XPLUS XB, delta, "");
2841 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), nak, (0.0)),
2842 		       0.0, delta, "");
2843 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), nak, (0.0)),
2844 		       0.0, delta, "");
2845 	zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), nak, (0.0)),
2846 		       0.0, delta, "");
2847 	/* Test DT_INST_STRING_UNQUOTED */
2848 #define STRING_UNQUOTED_VAR(node_id) _CONCAT(var_, node_id)
2849 #define STRING_UNQUOTED_TEST_INST_EXPANSION(inst) \
2850 	double STRING_UNQUOTED_VAR(DT_DRV_INST(inst)) = DT_INST_STRING_UNQUOTED(inst, val);
2851 	DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_TEST_INST_EXPANSION);
2852 
2853 	zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_f0)),
2854 		       f0_expected, delta, "");
2855 	zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_f1)),
2856 		       f1_expected, delta, "");
2857 	zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_t)), XA XPLUS XB,
2858 		       delta, "");
2859 
2860 	/* Test DT_INST_STRING_UNQUOTED_OR */
2861 #define STRING_UNQUOTED_OR_VAR(node_id) _CONCAT(var_or_, node_id)
2862 #define STRING_UNQUOTED_OR_TEST_INST_EXPANSION(inst)       \
2863 	double STRING_UNQUOTED_OR_VAR(DT_DRV_INST(inst))[2] = { \
2864 		DT_INST_STRING_UNQUOTED_OR(inst, val, (1.0e10)),   \
2865 		DT_INST_STRING_UNQUOTED_OR(inst, dummy, (1.0e10))  \
2866 	};
2867 	DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_OR_TEST_INST_EXPANSION);
2868 
2869 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f0))[0],
2870 		       f0_expected, delta, "");
2871 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f1))[0],
2872 		       f1_expected, delta, "");
2873 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[0],
2874 		       XA XPLUS XB, delta, "");
2875 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f0))[1],
2876 		       1.0e10, delta, "");
2877 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f1))[1],
2878 		       1.0e10, delta, "");
2879 	zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[1],
2880 		       1.0e10, delta, "");
2881 #undef XA
2882 #undef XB
2883 #undef XPLUS
2884 }
2885 
2886 #undef DT_DRV_COMPAT
2887 #define DT_DRV_COMPAT vnd_string_array_unquoted
ZTEST(devicetree_api,test_string_idx_unquoted)2888 ZTEST(devicetree_api, test_string_idx_unquoted)
2889 {
2890 #define XA 12.0
2891 #define XB 34.0
2892 #define XC 56.0
2893 #define XD 78.0
2894 #define XPLUS +
2895 #define XMINUS -
2896 	const double delta = 0.1e-4;
2897 
2898 	/* DT_STRING_UNQUOTED_BY_IDX */
2899 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 0),
2900 		       1.0e2, delta, "");
2901 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 1),
2902 		       2.0e2, delta, "");
2903 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 2),
2904 		       3.0e2, delta, "");
2905 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 3),
2906 		       4.0e2, delta, "");
2907 
2908 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 0),
2909 		       0.01, delta, "");
2910 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 1),
2911 		       0.1,  delta, "");
2912 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 2),
2913 		       1.0,  delta, "");
2914 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 3),
2915 		       10.0, delta, "");
2916 
2917 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 0),
2918 		       XA XPLUS XB, delta, "");
2919 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 1),
2920 		       XC XPLUS XD,  delta, "");
2921 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 2),
2922 		       XA XMINUS XB,  delta, "");
2923 	zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 3),
2924 		       XC XMINUS XD, delta, "");
2925 
2926 #define STRING_UNQUOTED_BY_IDX_VAR(node_id) _CONCAT(var_, node_id)
2927 #define STRING_UNQUOTED_BY_IDX_TEST_INST_EXPANSION(inst)       \
2928 	double STRING_UNQUOTED_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
2929 		DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 0),          \
2930 		DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 1),          \
2931 		DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 2),          \
2932 		DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 3),          \
2933 	};
2934 	DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_BY_IDX_TEST_INST_EXPANSION);
2935 
2936 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[0],
2937 		       1.0e2, delta, "");
2938 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[1],
2939 		       2.0e2, delta, "");
2940 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[2],
2941 		       3.0e2, delta, "");
2942 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[3],
2943 		       4.0e2, delta, "");
2944 
2945 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[0],
2946 		       0.01, delta, "");
2947 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[1],
2948 		       0.1,  delta, "");
2949 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[2],
2950 		       1.0,  delta, "");
2951 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[3],
2952 		       10.0, delta, "");
2953 
2954 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[0],
2955 		       XA XPLUS XB, delta, "");
2956 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[1],
2957 		       XC XPLUS XD,  delta, "");
2958 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[2],
2959 		       XA XMINUS XB,  delta, "");
2960 	zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[3],
2961 		       XC XMINUS XD, delta, "");
2962 #undef XA
2963 #undef XB
2964 #undef XC
2965 #undef XD
2966 #undef XPLUS
2967 #undef XMINUS
2968 }
2969 
2970 #undef DT_DRV_COMPAT
2971 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_reset)2972 ZTEST(devicetree_api, test_reset)
2973 {
2974 	/* DT_RESET_CTLR_BY_IDX */
2975 	zassert_true(DT_SAME_NODE(DT_RESET_CTLR_BY_IDX(TEST_TEMP, 1),
2976 				  DT_NODELABEL(test_reset)), "");
2977 
2978 	/* DT_RESET_CTLR */
2979 	zassert_true(DT_SAME_NODE(DT_RESET_CTLR(TEST_TEMP),
2980 				  DT_NODELABEL(test_reset)), "");
2981 
2982 	/* DT_RESET_CTLR_BY_NAME */
2983 	zassert_true(DT_SAME_NODE(DT_RESET_CTLR_BY_NAME(TEST_TEMP, reset_b),
2984 				  DT_NODELABEL(test_reset)), "");
2985 
2986 	/* DT_RESET_CELL_BY_IDX */
2987 	zassert_equal(DT_RESET_CELL_BY_IDX(TEST_TEMP, 1, id), 20, "");
2988 	zassert_equal(DT_RESET_CELL_BY_IDX(TEST_TEMP, 0, id), 10, "");
2989 
2990 	/* DT_RESET_CELL_BY_NAME */
2991 	zassert_equal(DT_RESET_CELL_BY_NAME(TEST_TEMP, reset_a, id), 10, "");
2992 	zassert_equal(DT_RESET_CELL_BY_NAME(TEST_TEMP, reset_b, id), 20, "");
2993 
2994 	/* DT_RESET_CELL */
2995 	zassert_equal(DT_RESET_CELL(TEST_TEMP, id), 10, "");
2996 
2997 	/* reg-width on reset */
2998 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_TEMP, resets, 1, reg_width), 4, "");
2999 
3000 	/* DT_INST */
3001 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
3002 
3003 	/* DT_INST_RESET_CTLR_BY_IDX */
3004 	zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR_BY_IDX(0, 1),
3005 				  DT_NODELABEL(test_reset)), "");
3006 
3007 	/* DT_INST_RESET_CTLR */
3008 	zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR(0),
3009 				  DT_NODELABEL(test_reset)), "");
3010 
3011 	/* DT_INST_RESET_CTLR_BY_NAME */
3012 	zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR_BY_NAME(0, reset_b),
3013 				  DT_NODELABEL(test_reset)), "");
3014 
3015 	/* DT_INST_RESET_CELL_BY_IDX */
3016 	zassert_equal(DT_INST_RESET_CELL_BY_IDX(0, 1, id), 20, "");
3017 	zassert_equal(DT_INST_RESET_CELL_BY_IDX(0, 0, id), 10, "");
3018 
3019 	/* DT_INST_RESET_CELL_BY_NAME */
3020 	zassert_equal(DT_INST_RESET_CELL_BY_NAME(0, reset_a, id), 10, "");
3021 	zassert_equal(DT_INST_RESET_CELL_BY_NAME(0, reset_b, id), 20, "");
3022 
3023 	/* DT_INST_RESET_CELL */
3024 	zassert_equal(DT_INST_RESET_CELL(0, id), 10, "");
3025 
3026 	/* reg-width on reset */
3027 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, resets, 1, reg_width), 4, "");
3028 
3029 	/* DT_RESET_ID_BY_IDX */
3030 	zassert_equal(DT_RESET_ID_BY_IDX(TEST_TEMP, 0), 10, "");
3031 	zassert_equal(DT_RESET_ID_BY_IDX(TEST_TEMP, 1), 20, "");
3032 
3033 	/* DT_RESET_ID */
3034 	zassert_equal(DT_RESET_ID(TEST_TEMP), 10, "");
3035 
3036 	/* DT_INST_RESET_ID_BY_IDX */
3037 	zassert_equal(DT_INST_RESET_ID_BY_IDX(0, 0), 10, "");
3038 	zassert_equal(DT_INST_RESET_ID_BY_IDX(0, 1), 20, "");
3039 
3040 	/* DT_INST_RESET_ID */
3041 	zassert_equal(DT_INST_RESET_ID(0), 10, "");
3042 }
3043 
3044 ZTEST_SUITE(devicetree_api, NULL, NULL, NULL, NULL, NULL);
3045