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