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_CLOCKS_LABEL_BY_IDX
10  * - DT_CLOCKS_LABEL_BY_NAME
11  * - DT_CLOCKS_LABEL
12  * - DT_PWMS_LABEL_BY_IDX
13  * - DT_PWMS_LABEL_BY_NAME
14  * - DT_PWMS_LABEL
15  * - DT_IO_CHANNELS_LABEL_BY_IDX
16  * - DT_IO_CHANNELS_LABEL_BY_NAME
17  * - DT_IO_CHANNELS_LABEL
18  * - DT_DMAS_LABEL_BY_IDX
19  * - DT_DMAS_LABEL_BY_NAME
20  * - DT_INST_CLOCKS_LABEL_BY_IDX
21  * - DT_INST_CLOCKS_LABEL_BY_NAME
22  * - DT_INST_CLOCKS_LABEL
23  * - DT_INST_PWMS_LABEL_BY_IDX
24  * - DT_INST_PWMS_LABEL_BY_NAME
25  * - DT_INST_PWMS_LABEL
26  * - DT_INST_IO_CHANNELS_LABEL_BY_IDX
27  * - DT_INST_IO_CHANNELS_LABEL_BY_NAME
28  * - DT_INST_IO_CHANNELS_LABEL
29  * - DT_INST_DMAS_LABEL_BY_IDX
30  * - DT_INST_DMAS_LABEL_BY_NAME
31  * - DT_ENUM_TOKEN
32  * - DT_ENUM_UPPER_TOKEN
33  */
34 #define __DEPRECATED_MACRO
35 
36 #include <ztest.h>
37 #include <devicetree.h>
38 #include <device.h>
39 #include <drivers/gpio.h>
40 
41 #define TEST_CHILDREN	DT_PATH(test, test_children)
42 #define TEST_DEADBEEF	DT_PATH(test, gpio_deadbeef)
43 #define TEST_ABCD1234	DT_PATH(test, gpio_abcd1234)
44 #define TEST_ALIAS	DT_ALIAS(test_alias)
45 #define TEST_NODELABEL	DT_NODELABEL(test_nodelabel)
46 #define TEST_INST	DT_INST(0, vnd_gpio)
47 #define TEST_ARRAYS	DT_NODELABEL(test_arrays)
48 #define TEST_PH	DT_NODELABEL(test_phandles)
49 #define TEST_IRQ	DT_NODELABEL(test_irq)
50 #define TEST_TEMP	DT_NODELABEL(test_temp_sensor)
51 #define TEST_REG	DT_NODELABEL(test_reg)
52 #define TEST_ENUM_0	DT_NODELABEL(test_enum_0)
53 
54 #define TEST_I2C_DEV DT_PATH(test, i2c_11112222, test_i2c_dev_10)
55 #define TEST_I2C_BUS DT_BUS(TEST_I2C_DEV)
56 
57 #define TEST_I2C_MUX DT_NODELABEL(test_i2c_mux)
58 #define TEST_I2C_MUX_CTLR_1 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_1)
59 #define TEST_I2C_MUX_CTLR_2 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_2)
60 #define TEST_MUXED_I2C_DEV_1 DT_NODELABEL(test_muxed_i2c_dev_1)
61 #define TEST_MUXED_I2C_DEV_2 DT_NODELABEL(test_muxed_i2c_dev_2)
62 
63 #define TEST_SPI DT_NODELABEL(test_spi)
64 
65 #define TEST_SPI_DEV_0 DT_PATH(test, spi_33334444, test_spi_dev_0)
66 #define TEST_SPI_BUS_0 DT_BUS(TEST_SPI_DEV_0)
67 
68 #define TEST_SPI_DEV_1 DT_PATH(test, spi_33334444, test_spi_dev_1)
69 #define TEST_SPI_BUS_1 DT_BUS(TEST_SPI_DEV_1)
70 
71 #define TEST_SPI_NO_CS DT_NODELABEL(test_spi_no_cs)
72 #define TEST_SPI_DEV_NO_CS DT_NODELABEL(test_spi_no_cs)
73 
74 #define TEST_PWM_CTLR_1 DT_NODELABEL(test_pwm1)
75 #define TEST_PWM_CTLR_2 DT_NODELABEL(test_pwm2)
76 
77 #define TEST_DMA_CTLR_1 DT_NODELABEL(test_dma1)
78 #define TEST_DMA_CTLR_2 DT_NODELABEL(test_dma2)
79 
80 #define TEST_IO_CHANNEL_CTLR_1 DT_NODELABEL(test_adc_1)
81 #define TEST_IO_CHANNEL_CTLR_2 DT_NODELABEL(test_adc_2)
82 
83 #define TA_HAS_COMPAT(compat) DT_NODE_HAS_COMPAT(TEST_ARRAYS, compat)
84 
85 #define TO_STRING(x) TO_STRING_(x)
86 #define TO_STRING_(x) #x
87 
test_path_props(void)88 static void test_path_props(void)
89 {
90 	zassert_true(!strcmp(DT_LABEL(TEST_DEADBEEF), "TEST_GPIO_1"), "");
91 	zassert_equal(DT_NUM_REGS(TEST_DEADBEEF), 1, "");
92 	zassert_equal(DT_REG_ADDR(TEST_DEADBEEF), 0xdeadbeef, "");
93 	zassert_equal(DT_REG_SIZE(TEST_DEADBEEF), 0x1000, "");
94 	zassert_equal(DT_PROP(TEST_DEADBEEF, gpio_controller), 1, "");
95 	zassert_equal(DT_PROP(TEST_DEADBEEF, ngpios), 32, "");
96 	zassert_true(!strcmp(DT_PROP(TEST_DEADBEEF, status), "okay"), "");
97 	zassert_equal(DT_PROP_LEN(TEST_DEADBEEF, compatible), 1, "");
98 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_DEADBEEF, compatible, 0),
99 			     "vnd,gpio"), "");
100 	zassert_true(DT_NODE_HAS_PROP(TEST_DEADBEEF, status), "");
101 	zassert_false(DT_NODE_HAS_PROP(TEST_DEADBEEF, foobar), "");
102 
103 	zassert_true(!strcmp(DT_LABEL(TEST_ABCD1234), "TEST_GPIO_2"), "");
104 	zassert_equal(DT_NUM_REGS(TEST_ABCD1234), 2, "");
105 	zassert_equal(DT_PROP(TEST_ABCD1234, gpio_controller), 1, "");
106 	zassert_equal(DT_PROP(TEST_ABCD1234, ngpios), 32, "");
107 	zassert_true(!strcmp(DT_PROP(TEST_ABCD1234, status), "okay"), "");
108 	zassert_equal(DT_PROP_LEN(TEST_ABCD1234, compatible), 1, "");
109 	zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, compatible, 4), 1, "");
110 	zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, invalid_property, 0), 0, "");
111 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ABCD1234, compatible, 0),
112 			     "vnd,gpio"), "");
113 }
114 
test_alias_props(void)115 static void test_alias_props(void)
116 {
117 	zassert_equal(DT_NUM_REGS(TEST_ALIAS), 1, "");
118 	zassert_equal(DT_REG_ADDR(TEST_ALIAS), 0xdeadbeef, "");
119 	zassert_equal(DT_REG_SIZE(TEST_ALIAS), 0x1000, "");
120 	zassert_true(!strcmp(DT_LABEL(TEST_ALIAS), "TEST_GPIO_1"), "");
121 	zassert_equal(DT_PROP(TEST_ALIAS, gpio_controller), 1, "");
122 	zassert_equal(DT_PROP(TEST_ALIAS, ngpios), 32, "");
123 	zassert_true(!strcmp(DT_PROP(TEST_ALIAS, status), "okay"), "");
124 	zassert_equal(DT_PROP_LEN(TEST_ALIAS, compatible), 1, "");
125 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ALIAS, compatible, 0),
126 			     "vnd,gpio"), "");
127 }
128 
test_nodelabel_props(void)129 static void test_nodelabel_props(void)
130 {
131 	zassert_equal(DT_NUM_REGS(TEST_NODELABEL), 1, "");
132 	zassert_equal(DT_REG_ADDR(TEST_NODELABEL), 0xdeadbeef, "");
133 	zassert_equal(DT_REG_SIZE(TEST_NODELABEL), 0x1000, "");
134 	zassert_true(!strcmp(DT_LABEL(TEST_NODELABEL), "TEST_GPIO_1"), "");
135 	zassert_equal(DT_PROP(TEST_NODELABEL, gpio_controller), 1, "");
136 	zassert_equal(DT_PROP(TEST_NODELABEL, ngpios), 32, "");
137 	zassert_true(!strcmp(DT_PROP(TEST_NODELABEL, status), "okay"), "");
138 	zassert_equal(DT_PROP_LEN(TEST_NODELABEL, compatible), 1, "");
139 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_NODELABEL, compatible, 0),
140 			     "vnd,gpio"), "");
141 }
142 
143 #undef DT_DRV_COMPAT
144 #define DT_DRV_COMPAT vnd_gpio
test_inst_props(void)145 static void test_inst_props(void)
146 {
147 	const char *label_startswith = "TEST_GPIO_";
148 
149 	/*
150 	 * Careful:
151 	 *
152 	 * We can only test properties that are shared across all
153 	 * instances of this compatible here. This includes instances
154 	 * with status "disabled".
155 	 */
156 
157 	zassert_equal(DT_PROP(TEST_INST, gpio_controller), 1, "");
158 	zassert_true(!strcmp(DT_PROP(TEST_INST, status), "okay") ||
159 		     !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
160 	zassert_equal(DT_PROP_LEN(TEST_INST, compatible), 1, "");
161 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_INST, compatible, 0),
162 			     "vnd,gpio"), "");
163 
164 	zassert_equal(DT_INST_NODE_HAS_PROP(0, gpio_controller), 1, "");
165 	zassert_equal(DT_INST_PROP(0, gpio_controller), 1, "");
166 	zassert_equal(DT_INST_NODE_HAS_PROP(0, xxxx), 0, "");
167 	zassert_true(!strcmp(DT_INST_PROP(0, status), "okay") ||
168 		     !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
169 	zassert_equal(DT_INST_PROP_LEN(0, compatible), 1, "");
170 	zassert_true(!strcmp(DT_INST_PROP_BY_IDX(0, compatible, 0),
171 			     "vnd,gpio"), "");
172 	zassert_true(!strncmp(label_startswith, DT_INST_LABEL(0),
173 			      strlen(label_startswith)), "");
174 }
175 
test_default_prop_access(void)176 static void test_default_prop_access(void)
177 {
178 	/*
179 	 * The APIs guarantee that the default_value is not expanded
180 	 * if the relevant property or cell is defined. This "X" macro
181 	 * is meant as poison which causes (hopefully) easy to
182 	 * understand build errors if this guarantee is not met due to
183 	 * a regression.
184 	 */
185 #undef X
186 #define X do.not.expand.this.argument
187 
188 	/* Node identifier variants. */
189 	zassert_equal(DT_PROP_OR(TEST_REG, misc_prop, X), 1234, "");
190 	zassert_equal(DT_PROP_OR(TEST_REG, not_a_property, -1), -1, "");
191 
192 	zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, channel, X), 3, "");
193 	zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, not_a_cell, -1), -1,
194 		      "");
195 
196 	zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, channel, X), 1, "");
197 	zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, not_a_cell, -1), -1, "");
198 
199 	zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, channel, X), 1,
200 		      "");
201 	zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, not_a_cell, -1),
202 		      -1, "");
203 
204 	/* Instance number variants. */
205 #undef DT_DRV_COMPAT
206 #define DT_DRV_COMPAT vnd_reg_holder
207 	zassert_equal(DT_INST_PROP_OR(0, misc_prop, X), 1234, "");
208 	zassert_equal(DT_INST_PROP_OR(0, not_a_property, -1), -1, "");
209 
210 #undef DT_DRV_COMPAT
211 #define DT_DRV_COMPAT vnd_adc_temp_sensor
212 	zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, channel, X), 3, "");
213 	zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, not_a_cell, -1), -1,
214 		      "");
215 
216 	zassert_equal(DT_INST_PHA_OR(0, dmas, channel, X), 1, "");
217 	zassert_equal(DT_INST_PHA_OR(0, dmas, not_a_cell, -1), -1, "");
218 
219 	zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, channel, X), 1,
220 		      "");
221 	zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, not_a_cell, -1), -1,
222 		      "");
223 
224 #undef X
225 }
226 
test_has_path(void)227 static void test_has_path(void)
228 {
229 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_0), okay), 0, "");
230 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_deadbeef), okay), 1,
231 		      "");
232 	zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_abcd1234), okay), 1,
233 		      "");
234 }
235 
test_has_alias(void)236 static void test_has_alias(void)
237 {
238 	zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_alias), okay), 1, "");
239 	zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_undef), okay), 0, "");
240 }
241 
test_inst_checks(void)242 static void test_inst_checks(void)
243 {
244 	zassert_equal(DT_NODE_EXISTS(DT_INST(0, vnd_gpio)), 1, "");
245 	zassert_equal(DT_NODE_EXISTS(DT_INST(1, vnd_gpio)), 1, "");
246 	zassert_equal(DT_NODE_EXISTS(DT_INST(2, vnd_gpio)), 1, "");
247 
248 	zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio), 2, "");
249 	zassert_equal(DT_NUM_INST_STATUS_OKAY(xxxx), 0, "");
250 }
251 
test_has_nodelabel(void)252 static void test_has_nodelabel(void)
253 {
254 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay), 0,
255 		      "");
256 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel), okay), 1,
257 		      "");
258 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel_allcaps),
259 					 okay),
260 		      1, "");
261 }
262 
test_has_compat(void)263 static void test_has_compat(void)
264 {
265 	unsigned int compats;
266 
267 	zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio), "");
268 	zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio), "");
269 	zassert_false(DT_HAS_COMPAT_STATUS_OKAY(vnd_disabled_compat), "");
270 
271 	zassert_equal(TA_HAS_COMPAT(vnd_array_holder), 1, "");
272 	zassert_equal(TA_HAS_COMPAT(vnd_undefined_compat), 1, "");
273 	zassert_equal(TA_HAS_COMPAT(vnd_not_a_test_array_compat), 0, "");
274 	compats = ((TA_HAS_COMPAT(vnd_array_holder) << 0) |
275 		   (TA_HAS_COMPAT(vnd_undefined_compat) << 1) |
276 		   (TA_HAS_COMPAT(vnd_not_a_test_array_compat) << 2));
277 	zassert_equal(compats, 0x3, "");
278 }
279 
test_has_status(void)280 static void test_has_status(void)
281 {
282 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), okay),
283 		      1, "");
284 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), disabled),
285 		      0, "");
286 
287 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), okay),
288 		      1, "");
289 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), disabled),
290 		      0, "");
291 
292 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), disabled),
293 		      1, "");
294 	zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay),
295 		      0, "");
296 }
297 
test_bus(void)298 static void test_bus(void)
299 {
300 	/* common prefixes of expected labels: */
301 	const char *i2c_bus = "TEST_I2C_CTLR";
302 	const char *i2c_dev = "TEST_I2C_DEV";
303 	const char *spi_bus = "TEST_SPI_CTLR";
304 	const char *spi_dev = "TEST_SPI_DEV";
305 	const char *gpio = "TEST_GPIO_";
306 	int pin, flags;
307 
308 	zassert_true(!strcmp(DT_LABEL(TEST_I2C_BUS), "TEST_I2C_CTLR"), "");
309 	zassert_true(!strcmp(DT_LABEL(TEST_SPI_BUS_0), "TEST_SPI_CTLR"), "");
310 	zassert_true(!strcmp(DT_LABEL(TEST_SPI_BUS_1), "TEST_SPI_CTLR"), "");
311 
312 	zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_0), 1, "");
313 	zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_NO_CS), 0, "");
314 
315 	/* Test a nested I2C bus using vnd,i2c-mux. */
316 	zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_1,
317 				  DT_BUS(TEST_MUXED_I2C_DEV_1)), "");
318 	zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_2,
319 				  DT_BUS(TEST_MUXED_I2C_DEV_2)), "");
320 
321 #undef DT_DRV_COMPAT
322 #define DT_DRV_COMPAT vnd_spi_device_2
323 	/* there is only one instance, and it has no CS */
324 	zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 0, "");
325 	/* since there's only one instance, we also know its bus. */
326 	zassert_true(DT_SAME_NODE(TEST_SPI_NO_CS, DT_INST_BUS(0)),
327 		     "expected TEST_SPI_NO_CS as bus for vnd,spi-device-2");
328 
329 #undef DT_DRV_COMPAT
330 #define DT_DRV_COMPAT vnd_spi_device
331 	/*
332 	 * DT_INST_SPI_DEV: use with care here. We could be matching
333 	 * either vnd,spi-device.
334 	 */
335 	zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 1, "");
336 
337 #define CTLR_NODE DT_INST_SPI_DEV_CS_GPIOS_CTLR(0)
338 	zassert_true(DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_1)) ||
339 		     DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_2)), "");
340 #undef CTLR_NODE
341 
342 	zassert_true(!strncmp(gpio, DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
343 			      strlen(gpio)), "");
344 
345 	pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
346 	zassert_true((pin == 0x10) || (pin == 0x30), "");
347 
348 	flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
349 	zassert_true((flags == 0x20) || (flags == 0x40), "");
350 
351 	zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, spi), 1, "");
352 	zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, i2c), 0, "");
353 
354 	zassert_equal(DT_ON_BUS(TEST_I2C_DEV, i2c), 1, "");
355 	zassert_equal(DT_ON_BUS(TEST_I2C_DEV, spi), 0, "");
356 
357 	zassert_true(!strcmp(DT_BUS_LABEL(TEST_I2C_DEV), "TEST_I2C_CTLR"), "");
358 
359 #undef DT_DRV_COMPAT
360 #define DT_DRV_COMPAT vnd_spi_device
361 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
362 
363 	zassert_equal(DT_INST_ON_BUS(0, spi), 1, "");
364 	zassert_equal(DT_INST_ON_BUS(0, i2c), 0, "");
365 
366 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 1, "");
367 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 0, "");
368 
369 	zassert_true(!strncmp(spi_dev, DT_INST_LABEL(0), strlen(spi_dev)), "");
370 	zassert_true(!strncmp(spi_bus, DT_INST_BUS_LABEL(0), strlen(spi_bus)),
371 		     "");
372 
373 #undef DT_DRV_COMPAT
374 #define DT_DRV_COMPAT vnd_i2c_device
375 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
376 
377 	zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
378 	zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
379 
380 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
381 	zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
382 
383 	zassert_true(!strncmp(i2c_dev, DT_INST_LABEL(0), strlen(i2c_dev)), "");
384 	zassert_true(!strncmp(i2c_bus, DT_INST_BUS_LABEL(0), strlen(i2c_bus)),
385 		     "");
386 
387 #undef DT_DRV_COMPAT
388 	/*
389 	 * Make sure the underlying DT_COMPAT_ON_BUS_INTERNAL used by
390 	 * DT_ANY_INST_ON_BUS works without DT_DRV_COMPAT defined.
391 	 */
392 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_spi_device, spi), 1, NULL);
393 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_spi_device, i2c), 0, NULL);
394 
395 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_i2c_device, i2c), 1, NULL);
396 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_i2c_device, spi), 0, NULL);
397 
398 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_gpio_expander, i2c), 1,
399 		      NULL);
400 	zassert_equal(DT_COMPAT_ON_BUS_INTERNAL(vnd_gpio_expander, spi), 1,
401 		      NULL);
402 }
403 
404 #undef DT_DRV_COMPAT
405 #define DT_DRV_COMPAT vnd_reg_holder
test_reg(void)406 static void test_reg(void)
407 {
408 	/* DT_REG_HAS_IDX */
409 	zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 0), "");
410 	zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 1), "");
411 	zassert_false(DT_REG_HAS_IDX(TEST_ABCD1234, 2), "");
412 
413 	/* DT_REG_ADDR_BY_IDX */
414 	zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 0), 0xabcd1234, "");
415 	zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 1), 0x98765432, "");
416 
417 	/* DT_REG_SIZE_BY_IDX */
418 	zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 0), 0x500, "");
419 	zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 1), 0xff, "");
420 
421 	/* DT_REG_ADDR */
422 	zassert_equal(DT_REG_ADDR(TEST_ABCD1234), 0xabcd1234, "");
423 
424 	/* DT_REG_SIZE */
425 	zassert_equal(DT_REG_SIZE(TEST_ABCD1234), 0x500, "");
426 
427 	/* DT_REG_ADDR_BY_NAME */
428 	zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, one), 0xabcd1234, "");
429 	zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, two), 0x98765432, "");
430 
431 	/* DT_REG_SIZE_BY_NAME */
432 	zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, one), 0x500, "");
433 	zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, two), 0xff, "");
434 
435 	/* DT_INST */
436 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
437 
438 	/* DT_INST_REG_HAS_IDX */
439 	zassert_true(DT_INST_REG_HAS_IDX(0, 0), "");
440 	zassert_true(DT_INST_REG_HAS_IDX(0, 1), "");
441 	zassert_false(DT_INST_REG_HAS_IDX(0, 2), "");
442 
443 	/* DT_INST_REG_ADDR_BY_IDX */
444 	zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 0), 0x9999aaaa, "");
445 	zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 1), 0xbbbbcccc, "");
446 
447 	/* DT_INST_REG_SIZE_BY_IDX */
448 	zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 0), 0x1000, "");
449 	zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 1), 0x3f, "");
450 
451 	/* DT_INST_REG_ADDR */
452 	zassert_equal(DT_INST_REG_ADDR(0), 0x9999aaaa, "");
453 
454 	/* DT_INST_REG_SIZE */
455 	zassert_equal(DT_INST_REG_SIZE(0), 0x1000, "");
456 
457 	/* DT_INST_REG_ADDR_BY_NAME */
458 	zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, first), 0x9999aaaa, "");
459 	zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, second), 0xbbbbcccc, "");
460 
461 	/* DT_INST_REG_SIZE_BY_NAME */
462 	zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, first), 0x1000, "");
463 	zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, second), 0x3f, "");
464 }
465 
466 #undef DT_DRV_COMPAT
467 #define DT_DRV_COMPAT vnd_interrupt_holder
test_irq(void)468 static void test_irq(void)
469 {
470 	/* DT_NUM_IRQS */
471 	zassert_equal(DT_NUM_IRQS(TEST_DEADBEEF), 1, "");
472 	zassert_equal(DT_NUM_IRQS(TEST_I2C_BUS), 2, "");
473 	zassert_equal(DT_NUM_IRQS(TEST_SPI), 3, "");
474 
475 	/* DT_IRQ_HAS_IDX */
476 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 0), "");
477 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 1), "");
478 	zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 2), "");
479 	zassert_false(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 3), "");
480 
481 	zassert_true(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 0), "");
482 	zassert_false(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 1), "");
483 
484 	zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 0), "");
485 	zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 1), "");
486 	zassert_false(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 2), "");
487 
488 	/* DT_IRQ_BY_IDX */
489 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, irq), 8, "");
490 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, irq), 9, "");
491 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, irq), 10, "");
492 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, priority), 3, "");
493 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, priority), 0, "");
494 	zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, priority), 1, "");
495 
496 	/* DT_IRQ_BY_NAME */
497 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, irq), 6, "");
498 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, irq), 7, "");
499 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, priority), 2, "");
500 	zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, priority), 1, "");
501 
502 	/* DT_IRQ_HAS_CELL_AT_IDX */
503 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, irq), "");
504 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, priority), "");
505 	zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, foo), 0, "");
506 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, irq), "");
507 	zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, priority), "");
508 	zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, foo), "");
509 
510 	/* DT_IRQ_HAS_CELL */
511 	zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, irq), "");
512 	zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, priority), "");
513 	zassert_false(DT_IRQ_HAS_CELL(TEST_IRQ, foo), "");
514 
515 	/* DT_IRQ_HAS_NAME */
516 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, err), "");
517 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, stat), "");
518 	zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, done), "");
519 	zassert_false(DT_IRQ_HAS_NAME(TEST_IRQ, alpha), "");
520 
521 	/* DT_IRQ */
522 	zassert_equal(DT_IRQ(TEST_I2C_BUS, irq), 6, "");
523 	zassert_equal(DT_IRQ(TEST_I2C_BUS, priority), 2, "");
524 
525 	/* DT_IRQN */
526 	zassert_equal(DT_IRQN(TEST_I2C_BUS), 6, "");
527 
528 	/* DT_INST */
529 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
530 
531 	/* DT_INST_IRQ_HAS_IDX */
532 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 0), 1, "");
533 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 1), 1, "");
534 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 2), 1, "");
535 	zassert_equal(DT_INST_IRQ_HAS_IDX(0, 3), 0, "");
536 
537 	/* DT_INST_IRQ_BY_IDX */
538 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, irq), 30, "");
539 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, irq), 40, "");
540 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, irq), 60, "");
541 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, priority), 3, "");
542 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, priority), 5, "");
543 	zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, priority), 7, "");
544 
545 	/* DT_INST_IRQ_BY_NAME */
546 	zassert_equal(DT_INST_IRQ_BY_NAME(0, err, irq), 30, "");
547 	zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, irq), 40, "");
548 	zassert_equal(DT_INST_IRQ_BY_NAME(0, done, irq), 60, "");
549 	zassert_equal(DT_INST_IRQ_BY_NAME(0, err, priority), 3, "");
550 	zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, priority), 5, "");
551 	zassert_equal(DT_INST_IRQ_BY_NAME(0, done, priority), 7, "");
552 
553 	/* DT_INST_IRQ */
554 	zassert_equal(DT_INST_IRQ(0, irq), 30, "");
555 	zassert_equal(DT_INST_IRQ(0, priority), 3, "");
556 
557 	/* DT_INST_IRQN */
558 	zassert_equal(DT_INST_IRQN(0), 30, "");
559 
560 	/* DT_INST_IRQ_HAS_CELL_AT_IDX */
561 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, irq), "");
562 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, priority), "");
563 	zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, foo), "");
564 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, irq), "");
565 	zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, priority), "");
566 	zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, foo), "");
567 
568 	/* DT_INST_IRQ_HAS_CELL */
569 	zassert_true(DT_INST_IRQ_HAS_CELL(0, irq), "");
570 	zassert_true(DT_INST_IRQ_HAS_CELL(0, priority), "");
571 	zassert_false(DT_INST_IRQ_HAS_CELL(0, foo), "");
572 
573 	/* DT_INST_IRQ_HAS_NAME */
574 	zassert_true(DT_INST_IRQ_HAS_NAME(0, err), "");
575 	zassert_true(DT_INST_IRQ_HAS_NAME(0, stat), "");
576 	zassert_true(DT_INST_IRQ_HAS_NAME(0, done), "");
577 	zassert_false(DT_INST_IRQ_HAS_NAME(0, alpha), "");
578 }
579 
580 struct gpios_struct {
581 	const char *name;
582 	gpio_pin_t pin;
583 	gpio_flags_t flags;
584 };
585 
586 /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
587 #define DT_PROP_ELEM_BY_PHANDLE(idx, node_id, ph_prop, prop) \
588 	DT_PROP_BY_PHANDLE_IDX(node_id, ph_prop, idx, prop),
589 #define DT_PHANDLE_LISTIFY(node_id, ph_prop, prop) \
590 	{ \
591 	  UTIL_LISTIFY(DT_PROP_LEN(node_id, ph_prop), \
592 		       DT_PROP_ELEM_BY_PHANDLE, \
593 		       node_id, \
594 		       ph_prop, \
595 		       label) \
596 	}
597 
598 /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
599 #define DT_GPIO_ELEM(idx, node_id, prop) \
600 	{ \
601 		DT_PROP(DT_PHANDLE_BY_IDX(node_id, prop, idx), label), \
602 		DT_PHA_BY_IDX(node_id, prop, idx, pin),\
603 		DT_PHA_BY_IDX(node_id, prop, idx, flags),\
604 	},
605 #define DT_GPIO_LISTIFY(node_id, prop) \
606 	{ UTIL_LISTIFY(DT_PROP_LEN(node_id, prop), DT_GPIO_ELEM, \
607 		       node_id, prop) }
608 
609 #undef DT_DRV_COMPAT
610 #define DT_DRV_COMPAT vnd_phandle_holder
test_phandles(void)611 static void test_phandles(void)
612 {
613 	const char *ph_label = DT_PROP_BY_PHANDLE(TEST_PH, ph, label);
614 	const char *phs_labels[] = DT_PHANDLE_LISTIFY(TEST_PH, phs, label);
615 	struct gpios_struct gps[] = DT_GPIO_LISTIFY(TEST_PH, gpios);
616 
617 	/* phandle */
618 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, ph), "");
619 	zassert_true(DT_SAME_NODE(DT_PROP(TEST_PH, ph),
620 				  DT_NODELABEL(test_gpio_1)), "");
621 	zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, ph, 0),
622 				  DT_NODELABEL(test_gpio_1)), "");
623 	/* DT_PROP_BY_PHANDLE */
624 	zassert_true(!strcmp(ph_label, "TEST_GPIO_1"), "");
625 
626 	/* phandles */
627 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, phs), "");
628 	zassert_equal(ARRAY_SIZE(phs_labels), 3, "");
629 	zassert_equal(DT_PROP_LEN(TEST_PH, phs), 3, "");
630 	zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, phs, 1),
631 				  DT_NODELABEL(test_gpio_2)), "");
632 
633 	/* DT_PROP_BY_PHANDLE_IDX */
634 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 0, label),
635 			     "TEST_GPIO_1"), "");
636 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 1, label),
637 			     "TEST_GPIO_2"), "");
638 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 2, label),
639 			     "TEST_I2C_CTLR"), "");
640 	zassert_true(!strcmp(phs_labels[0], "TEST_GPIO_1"), "");
641 	zassert_true(!strcmp(phs_labels[1], "TEST_GPIO_2"), "");
642 	zassert_true(!strcmp(phs_labels[2], "TEST_I2C_CTLR"), "");
643 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0,
644 					     gpio_controller), 1, "");
645 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1,
646 					     gpio_controller), 1, "");
647 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0, label),
648 			     "TEST_GPIO_1"), "");
649 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1, label),
650 			     "TEST_GPIO_2"), "");
651 
652 	/* DT_PROP_BY_PHANDLE_IDX_OR */
653 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 0,
654 						val, "zero"), "one"), "");
655 	zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 1,
656 						val, "zero"), "zero"), "");
657 
658 	/* phandle-array */
659 	zassert_true(DT_NODE_HAS_PROP(TEST_PH, gpios), "");
660 	zassert_equal(ARRAY_SIZE(gps), 2, "");
661 	zassert_equal(DT_PROP_LEN(TEST_PH, gpios), 2, "");
662 
663 	/* DT_PROP_HAS_IDX */
664 	zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 0), "");
665 	zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 1), "");
666 	zassert_false(DT_PROP_HAS_IDX(TEST_PH, gpios, 2), "");
667 
668 	/* DT_PHA_HAS_CELL_AT_IDX */
669 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, pin), "");
670 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, flags), "");
671 	/* pha-gpios index 1 has nothing, not even a phandle */
672 	zassert_false(DT_PROP_HAS_IDX(TEST_PH, pha_gpios, 1), "");
673 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, pin), "");
674 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, flags),
675 		      "");
676 	/* index 2 only has a pin cell, no flags */
677 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, pin), "");
678 	zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, flags),
679 		      "");
680 	/* index 3 has both pin and flags cells*/
681 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, pin), "");
682 	zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, flags), "");
683 	/* even though index 1 has nothing, the length is still 4 */
684 	zassert_equal(DT_PROP_LEN(TEST_PH, pha_gpios), 4, "");
685 
686 	/* DT_PHA_HAS_CELL */
687 	zassert_true(DT_PHA_HAS_CELL(TEST_PH, gpios, flags), "");
688 	zassert_false(DT_PHA_HAS_CELL(TEST_PH, gpios, bar), "");
689 
690 	/* DT_PHANDLE_BY_IDX */
691 	zassert_true(!strcmp(DT_LABEL(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 0)),
692 			     "TEST_GPIO_1"), "");
693 	zassert_true(!strcmp(DT_LABEL(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 1)),
694 			     "TEST_GPIO_2"), "");
695 
696 	/* DT_PHANDLE */
697 	zassert_true(!strcmp(DT_LABEL(DT_PHANDLE(TEST_PH, gpios)),
698 			     "TEST_GPIO_1"), "");
699 
700 	/* DT_PHA */
701 	zassert_equal(DT_PHA(TEST_PH, gpios, pin), 10, "");
702 	zassert_equal(DT_PHA(TEST_PH, gpios, flags), 20, "");
703 
704 	/* DT_PHA_BY_IDX */
705 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, pin), 10, "");
706 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, flags), 20, "");
707 
708 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, pin), 30, "");
709 	zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, flags), 40, "");
710 
711 	/* DT_PHA_BY_NAME */
712 	zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, a, foocell), 100, "");
713 	zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, b_c, foocell), 110, "");
714 
715 	/* DT_PHANDLE_BY_NAME */
716 	zassert_true(!strcmp(DT_LABEL(DT_PHANDLE_BY_NAME(TEST_PH, foos, a)),
717 			     "TEST_GPIO_1"), "");
718 	zassert_true(!strcmp(DT_LABEL(DT_PHANDLE_BY_NAME(TEST_PH, foos, b_c)),
719 			     "TEST_GPIO_2"), "");
720 
721 	/* array initializers */
722 	zassert_true(!strcmp(gps[0].name, "TEST_GPIO_1"), "");
723 	zassert_equal(gps[0].pin, 10, "");
724 	zassert_equal(gps[0].flags, 20, "");
725 
726 	zassert_true(!strcmp(gps[1].name, "TEST_GPIO_2"), "");
727 	zassert_equal(gps[1].pin, 30, "");
728 	zassert_equal(gps[1].flags, 40, "");
729 
730 	/* DT_INST */
731 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
732 
733 	/* DT_INST_PROP_BY_PHANDLE */
734 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE(0, ph, label),
735 			     "TEST_GPIO_1"), "");
736 
737 	zassert_true(!strcmp(ph_label, "TEST_GPIO_1"), "");
738 
739 	/* DT_INST_PROP_BY_PHANDLE_IDX */
740 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 0, label),
741 			     "TEST_GPIO_1"), "");
742 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 1, label),
743 			     "TEST_GPIO_2"), "");
744 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 2, label),
745 			     "TEST_I2C_CTLR"), "");
746 	zassert_true(!strcmp(phs_labels[0], "TEST_GPIO_1"), "");
747 	zassert_true(!strcmp(phs_labels[1], "TEST_GPIO_2"), "");
748 	zassert_true(!strcmp(phs_labels[2], "TEST_I2C_CTLR"), "");
749 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0,
750 					     gpio_controller),
751 		      1, "");
752 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1,
753 					     gpio_controller),
754 		      1, "");
755 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0, label),
756 			     "TEST_GPIO_1"), "");
757 	zassert_true(!strcmp(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1, label),
758 			     "TEST_GPIO_2"), "");
759 
760 	/* DT_INST_PROP_HAS_IDX */
761 	zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 0), "");
762 	zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 1), "");
763 	zassert_false(DT_INST_PROP_HAS_IDX(0, gpios, 2), "");
764 
765 	/* DT_INST_PHA_HAS_CELL_AT_IDX */
766 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, pin), "");
767 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, flags), "");
768 	/* index 1 has nothing, not even a phandle */
769 	zassert_false(DT_INST_PROP_HAS_IDX(0, pha_gpios, 1), "");
770 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, pin), "");
771 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, flags), "");
772 	/* index 2 only has pin, no flags */
773 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, pin), "");
774 	zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, flags), "");
775 	/* index 3 has both pin and flags */
776 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, pin), "");
777 	zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, flags), "");
778 	/* even though index 1 has nothing, the length is still 4 */
779 	zassert_equal(DT_INST_PROP_LEN(0, pha_gpios), 4, "");
780 
781 	/* DT_INST_PHA_HAS_CELL */
782 	zassert_true(DT_INST_PHA_HAS_CELL(0, gpios, flags), "");
783 	zassert_false(DT_INST_PHA_HAS_CELL(0, gpios, bar), "");
784 
785 	/* DT_INST_PHANDLE_BY_IDX */
786 	zassert_true(!strcmp(DT_LABEL(DT_INST_PHANDLE_BY_IDX(0, gpios, 0)),
787 			     "TEST_GPIO_1"), "");
788 	zassert_true(!strcmp(DT_LABEL(DT_INST_PHANDLE_BY_IDX(0, gpios, 1)),
789 			     "TEST_GPIO_2"), "");
790 
791 	/* DT_INST_PHANDLE */
792 	zassert_true(!strcmp(DT_LABEL(DT_INST_PHANDLE(0, gpios)),
793 			     "TEST_GPIO_1"), "");
794 
795 	/* DT_INST_PHA */
796 	zassert_equal(DT_INST_PHA(0, gpios, pin), 10, "");
797 	zassert_equal(DT_INST_PHA(0, gpios, flags), 20, "");
798 
799 	/* DT_INST_PHA_BY_IDX */
800 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, pin), 10, "");
801 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, flags), 20, "");
802 
803 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, pin), 30, "");
804 	zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, flags), 40, "");
805 
806 	/* DT_INST_PHA_BY_NAME */
807 	zassert_equal(DT_INST_PHA_BY_NAME(0, foos, a, foocell), 100, "");
808 	zassert_equal(DT_INST_PHA_BY_NAME(0, foos, b_c, foocell), 110, "");
809 
810 	/* DT_INST_PHANDLE_BY_NAME */
811 	zassert_true(!strcmp(DT_LABEL(DT_INST_PHANDLE_BY_NAME(0, foos, a)),
812 			     "TEST_GPIO_1"), "");
813 	zassert_true(!strcmp(DT_LABEL(DT_INST_PHANDLE_BY_NAME(0, foos, b_c)),
814 			     "TEST_GPIO_2"), "");
815 }
816 
817 #undef DT_DRV_COMPAT
818 #define DT_DRV_COMPAT vnd_phandle_holder
test_gpio(void)819 static void test_gpio(void)
820 {
821 	/* DT_GPIO_CTLR_BY_IDX */
822 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 0)),
823 			     TO_STRING(DT_NODELABEL(test_gpio_1))), "");
824 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 1)),
825 			     TO_STRING(DT_NODELABEL(test_gpio_2))), "");
826 
827 	/* DT_GPIO_CTLR */
828 	zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR(TEST_PH, gpios)),
829 			     TO_STRING(DT_NODELABEL(test_gpio_1))), "");
830 
831 	/* DT_GPIO_LABEL_BY_IDX */
832 	zassert_true(!strcmp(DT_GPIO_LABEL_BY_IDX(TEST_PH, gpios, 0),
833 			     "TEST_GPIO_1"), "");
834 	zassert_true(!strcmp(DT_GPIO_LABEL_BY_IDX(TEST_PH, gpios, 1),
835 			     "TEST_GPIO_2"), "");
836 
837 	/* DT_GPIO_LABEL */
838 	zassert_true(!strcmp(DT_GPIO_LABEL(TEST_PH, gpios), "TEST_GPIO_1"), "");
839 
840 	/* DT_GPIO_PIN_BY_IDX */
841 	zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 0), 10, "");
842 	zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 1), 30, "");
843 
844 	/* DT_GPIO_PIN */
845 	zassert_equal(DT_GPIO_PIN(TEST_PH, gpios), 10, "");
846 
847 	/* DT_GPIO_FLAGS_BY_IDX */
848 	zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 0), 20, "");
849 	zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 1), 40, "");
850 
851 	/* DT_GPIO_FLAGS */
852 	zassert_equal(DT_GPIO_FLAGS(TEST_PH, gpios), 20, "");
853 
854 	/* DT_INST */
855 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
856 
857 	/* DT_INST_GPIO_LABEL_BY_IDX */
858 	zassert_true(!strcmp(DT_INST_GPIO_LABEL_BY_IDX(0, gpios, 0),
859 			     "TEST_GPIO_1"), "");
860 	zassert_true(!strcmp(DT_INST_GPIO_LABEL_BY_IDX(0, gpios, 1),
861 			     "TEST_GPIO_2"), "");
862 
863 	/* DT_INST_GPIO_LABEL */
864 	zassert_true(!strcmp(DT_INST_GPIO_LABEL(0, gpios), "TEST_GPIO_1"), "");
865 
866 	/* DT_INST_GPIO_PIN_BY_IDX */
867 	zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 0), 10, "");
868 	zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 1), 30, "");
869 
870 	/* DT_INST_GPIO_PIN */
871 	zassert_equal(DT_INST_GPIO_PIN(0, gpios), 10, "");
872 
873 	/* DT_INST_GPIO_FLAGS_BY_IDX */
874 	zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 0), 20, "");
875 	zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 1), 40, "");
876 
877 	/* DT_INST_GPIO_FLAGS */
878 	zassert_equal(DT_INST_GPIO_FLAGS(0, gpios), 20, "");
879 }
880 
881 #undef DT_DRV_COMPAT
882 #define DT_DRV_COMPAT vnd_adc_temp_sensor
test_io_channels(void)883 static void test_io_channels(void)
884 {
885 	zassert_true(!strcmp(DT_IO_CHANNELS_LABEL_BY_IDX(TEST_TEMP, 0),
886 			     "TEST_ADC_1"), "");
887 	zassert_true(!strcmp(DT_IO_CHANNELS_LABEL_BY_IDX(TEST_TEMP, 1),
888 			     "TEST_ADC_2"), "");
889 	zassert_true(!strcmp(DT_IO_CHANNELS_LABEL_BY_NAME(TEST_TEMP, ch1),
890 			     "TEST_ADC_1"), "");
891 	zassert_true(!strcmp(DT_IO_CHANNELS_LABEL_BY_NAME(TEST_TEMP, ch2),
892 			     "TEST_ADC_2"), "");
893 	zassert_true(!strcmp(DT_IO_CHANNELS_LABEL(TEST_TEMP),
894 			     "TEST_ADC_1"), "");
895 
896 	zassert_true(!strcmp(DT_INST_IO_CHANNELS_LABEL_BY_IDX(0, 0),
897 			     "TEST_ADC_1"), "");
898 	zassert_true(!strcmp(DT_INST_IO_CHANNELS_LABEL_BY_IDX(0, 1),
899 			     "TEST_ADC_2"), "");
900 	zassert_true(!strcmp(DT_INST_IO_CHANNELS_LABEL_BY_NAME(0, ch1),
901 			     "TEST_ADC_1"), "");
902 	zassert_true(!strcmp(DT_INST_IO_CHANNELS_LABEL_BY_NAME(0, ch2),
903 			     "TEST_ADC_2"), "");
904 	zassert_true(!strcmp(DT_INST_IO_CHANNELS_LABEL(0),
905 			     "TEST_ADC_1"), "");
906 
907 	/* DT_IO_CHANNELS_CTLR_BY_IDX */
908 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 0),
909 				  TEST_IO_CHANNEL_CTLR_1), "");
910 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 1),
911 				  TEST_IO_CHANNEL_CTLR_2), "");
912 
913 	/* DT_IO_CHANNELS_CTLR_BY_NAME */
914 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch1),
915 				  TEST_IO_CHANNEL_CTLR_1), "");
916 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch2),
917 				  TEST_IO_CHANNEL_CTLR_2), "");
918 
919 	/* DT_IO_CHANNELS_CTLR */
920 	zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR(TEST_TEMP),
921 				  TEST_IO_CHANNEL_CTLR_1), "");
922 
923 	/* DT_INST_IO_CHANNELS_CTLR_BY_IDX */
924 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 0),
925 				  TEST_IO_CHANNEL_CTLR_1), "");
926 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 1),
927 				  TEST_IO_CHANNEL_CTLR_2), "");
928 
929 	/* DT_INST_IO_CHANNELS_CTLR_BY_NAME */
930 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch1),
931 				  TEST_IO_CHANNEL_CTLR_1), "");
932 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch2),
933 				  TEST_IO_CHANNEL_CTLR_2), "");
934 
935 	/* DT_INST_IO_CHANNELS_CTLR */
936 	zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR(0),
937 				  TEST_IO_CHANNEL_CTLR_1), "");
938 
939 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 0), 10, "");
940 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 1), 20, "");
941 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch1), 10, "");
942 	zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch2), 20, "");
943 	zassert_equal(DT_IO_CHANNELS_INPUT(TEST_TEMP), 10, "");
944 
945 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 0), 10, "");
946 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 1), 20, "");
947 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch1), 10, "");
948 	zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch2), 20, "");
949 	zassert_equal(DT_INST_IO_CHANNELS_INPUT(0), 10, "");
950 }
951 
952 #undef DT_DRV_COMPAT
953 #define DT_DRV_COMPAT vnd_adc_temp_sensor
test_dma(void)954 static void test_dma(void)
955 {
956 	zassert_true(!strcmp(DT_DMAS_LABEL_BY_NAME(TEST_TEMP, rx),
957 			     "TEST_DMA_CTRL_2"), "");
958 	zassert_true(!strcmp(DT_INST_DMAS_LABEL_BY_NAME(0, rx),
959 			     "TEST_DMA_CTRL_2"), "");
960 	zassert_true(!strcmp(DT_DMAS_LABEL_BY_NAME(TEST_TEMP, tx),
961 			     "TEST_DMA_CTRL_1"), "");
962 	zassert_true(!strcmp(DT_INST_DMAS_LABEL_BY_NAME(0, tx),
963 			     "TEST_DMA_CTRL_1"), "");
964 
965 	zassert_true(!strcmp(DT_DMAS_LABEL_BY_IDX(TEST_TEMP, 1),
966 			     "TEST_DMA_CTRL_2"), "");
967 	zassert_true(!strcmp(DT_INST_DMAS_LABEL_BY_IDX(0, 1),
968 			     "TEST_DMA_CTRL_2"), "");
969 	zassert_true(!strcmp(DT_DMAS_LABEL_BY_IDX(TEST_TEMP, 0),
970 			     "TEST_DMA_CTRL_1"), "");
971 	zassert_true(!strcmp(DT_INST_DMAS_LABEL_BY_IDX(0, 0),
972 			     "TEST_DMA_CTRL_1"), "");
973 
974 	/* DT_DMAS_CTLR_BY_IDX */
975 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 0),
976 				  TEST_DMA_CTLR_1), "");
977 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 1),
978 				  TEST_DMA_CTLR_2), "");
979 
980 	/* DT_DMAS_CTLR_BY_NAME */
981 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, tx),
982 				  TEST_DMA_CTLR_1), "");
983 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, rx),
984 				  TEST_DMA_CTLR_2), "");
985 
986 	/* DT_DMAS_CTLR */
987 	zassert_true(DT_SAME_NODE(DT_DMAS_CTLR(TEST_TEMP),
988 				  TEST_DMA_CTLR_1), "");
989 
990 	/* DT_INST_DMAS_CTLR_BY_IDX */
991 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 0),
992 				  TEST_DMA_CTLR_1), "");
993 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 1),
994 				  TEST_DMA_CTLR_2), "");
995 
996 	/* DT_INST_DMAS_CTLR_BY_NAME */
997 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, tx),
998 				  TEST_DMA_CTLR_1), "");
999 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, rx),
1000 				  TEST_DMA_CTLR_2), "");
1001 
1002 	/* DT_INST_DMAS_CTLR */
1003 	zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR(0), TEST_DMA_CTLR_1), "");
1004 
1005 	zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, channel), 3, "");
1006 	zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, channel), 3, "");
1007 	zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, slot), 4, "");
1008 	zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, slot), 4, "");
1009 
1010 	zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, channel), 3, "");
1011 	zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, channel), 3, "");
1012 	zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, slot), 4, "");
1013 	zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, slot), 4, "");
1014 
1015 	zassert_true(DT_DMAS_HAS_NAME(TEST_TEMP, tx), "");
1016 	zassert_true(DT_INST_DMAS_HAS_NAME(0, tx), "");
1017 	zassert_false(DT_DMAS_HAS_NAME(TEST_TEMP, output), "");
1018 	zassert_false(DT_INST_DMAS_HAS_NAME(0, output), "");
1019 
1020 	zassert_true(DT_DMAS_HAS_IDX(TEST_TEMP, 1), "");
1021 	zassert_true(DT_INST_DMAS_HAS_IDX(0, 1), "");
1022 	zassert_false(DT_DMAS_HAS_IDX(TEST_TEMP, 2), "");
1023 	zassert_false(DT_INST_DMAS_HAS_IDX(0, 2), "");
1024 }
1025 
1026 #undef DT_DRV_COMPAT
1027 #define DT_DRV_COMPAT vnd_phandle_holder
test_pwms(void)1028 static void test_pwms(void)
1029 {
1030 	/* DT_PWMS_LABEL_BY_IDX */
1031 	zassert_true(!strcmp(DT_PWMS_LABEL_BY_IDX(TEST_PH, 0),
1032 			     "TEST_PWM_CTRL_1"), "");
1033 
1034 	/* DT_PWMS_LABEL_BY_NAME */
1035 	zassert_true(!strcmp(DT_PWMS_LABEL_BY_NAME(TEST_PH, red),
1036 			     "TEST_PWM_CTRL_1"), "");
1037 
1038 	/* DT_PWMS_LABEL */
1039 	zassert_true(!strcmp(DT_PWMS_LABEL(TEST_PH), "TEST_PWM_CTRL_1"), "");
1040 
1041 	/* DT_PWMS_CTLR_BY_IDX */
1042 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 0),
1043 				  TEST_PWM_CTLR_1), "");
1044 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 1),
1045 				  TEST_PWM_CTLR_2), "");
1046 
1047 	/* DT_PWMS_CTLR_BY_NAME */
1048 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, red),
1049 				  TEST_PWM_CTLR_1), "");
1050 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, green),
1051 				  TEST_PWM_CTLR_2), "");
1052 
1053 	/* DT_PWMS_CTLR */
1054 	zassert_true(DT_SAME_NODE(DT_PWMS_CTLR(TEST_PH),
1055 				  TEST_PWM_CTLR_1), "");
1056 
1057 	/* DT_PWMS_CELL_BY_IDX */
1058 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, channel), 5, "");
1059 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, period), 100, "");
1060 	zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, flags), 1, "");
1061 
1062 	/* DT_PWMS_CELL_BY_NAME */
1063 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, channel), 8, "");
1064 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, period), 200, "");
1065 	zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, flags), 3, "");
1066 
1067 	/* DT_PWMS_CELL */
1068 	zassert_equal(DT_PWMS_CELL(TEST_PH, channel), 8, "");
1069 	zassert_equal(DT_PWMS_CELL(TEST_PH, period), 200, "");
1070 	zassert_equal(DT_PWMS_CELL(TEST_PH, flags), 3, "");
1071 
1072 	/* DT_PWMS_CHANNEL_BY_IDX */
1073 	zassert_equal(DT_PWMS_CHANNEL_BY_IDX(TEST_PH, 1), 5, "");
1074 
1075 	/* DT_PWMS_CHANNEL_BY_NAME */
1076 	zassert_equal(DT_PWMS_CHANNEL_BY_NAME(TEST_PH, green), 5, "");
1077 
1078 	/* DT_PWMS_CHANNEL */
1079 	zassert_equal(DT_PWMS_CHANNEL(TEST_PH), 8, "");
1080 
1081 	/* DT_PWMS_PERIOD_BY_IDX */
1082 	zassert_equal(DT_PWMS_PERIOD_BY_IDX(TEST_PH, 1), 100, "");
1083 
1084 	/* DT_PWMS_PERIOD_BY_NAME */
1085 	zassert_equal(DT_PWMS_PERIOD_BY_NAME(TEST_PH, green), 100, "");
1086 
1087 	/* DT_PWMS_PERIOD */
1088 	zassert_equal(DT_PWMS_PERIOD(TEST_PH), 200, "");
1089 
1090 	/* DT_PWMS_FLAGS_BY_IDX */
1091 	zassert_equal(DT_PWMS_FLAGS_BY_IDX(TEST_PH, 1), 1, "");
1092 
1093 	/* DT_PWMS_FLAGS_BY_NAME */
1094 	zassert_equal(DT_PWMS_FLAGS_BY_NAME(TEST_PH, green), 1, "");
1095 
1096 	/* DT_PWMS_FLAGS */
1097 	zassert_equal(DT_PWMS_FLAGS(TEST_PH), 3, "");
1098 
1099 	/* DT_INST */
1100 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1101 
1102 	/* DT_INST_PWMS_LABEL_BY_IDX */
1103 	zassert_true(!strcmp(DT_INST_PWMS_LABEL_BY_IDX(0, 0),
1104 			     "TEST_PWM_CTRL_1"), "");
1105 
1106 	/* DT_INST_PWMS_LABEL_BY_NAME */
1107 	zassert_true(!strcmp(DT_INST_PWMS_LABEL_BY_NAME(0, green),
1108 			     "TEST_PWM_CTRL_2"), "");
1109 
1110 	/* DT_INST_PWMS_LABEL */
1111 	zassert_true(!strcmp(DT_INST_PWMS_LABEL(0), "TEST_PWM_CTRL_1"), "");
1112 
1113 	/* DT_INST_PWMS_CTLR_BY_IDX */
1114 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 0),
1115 				  TEST_PWM_CTLR_1), "");
1116 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 1),
1117 				  TEST_PWM_CTLR_2), "");
1118 
1119 	/* DT_INST_PWMS_CTLR_BY_NAME */
1120 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, red),
1121 				  TEST_PWM_CTLR_1), "");
1122 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, green),
1123 				  TEST_PWM_CTLR_2), "");
1124 
1125 	/* DT_INST_PWMS_CTLR */
1126 	zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR(0), TEST_PWM_CTLR_1), "");
1127 
1128 	/* DT_INST_PWMS_CELL_BY_IDX */
1129 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, channel), 5, "");
1130 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, period), 100, "");
1131 	zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, flags), 1, "");
1132 
1133 	/* DT_INST_PWMS_CELL_BY_NAME */
1134 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, channel), 5, "");
1135 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, period), 100, "");
1136 	zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, flags), 1, "");
1137 
1138 	/* DT_INST_PWMS_CELL */
1139 	zassert_equal(DT_INST_PWMS_CELL(0, channel), 8, "");
1140 	zassert_equal(DT_INST_PWMS_CELL(0, period), 200, "");
1141 	zassert_equal(DT_INST_PWMS_CELL(0, flags), 3, "");
1142 
1143 	/* DT_INST_PWMS_CHANNEL_BY_IDX */
1144 	zassert_equal(DT_INST_PWMS_CHANNEL_BY_IDX(0, 1), 5, "");
1145 
1146 	/* DT_INST_PWMS_CHANNEL_BY_NAME */
1147 	zassert_equal(DT_INST_PWMS_CHANNEL_BY_NAME(0, green), 5, "");
1148 
1149 	/* DT_INST_PWMS_CHANNEL */
1150 	zassert_equal(DT_INST_PWMS_CHANNEL(0), 8, "");
1151 
1152 	/* DT_INST_PWMS_PERIOD_BY_IDX */
1153 	zassert_equal(DT_INST_PWMS_PERIOD_BY_IDX(0, 1), 100, "");
1154 
1155 	/* DT_INST_PWMS_PERIOD_BY_NAME */
1156 	zassert_equal(DT_INST_PWMS_PERIOD_BY_NAME(0, red), 200, "");
1157 
1158 	/* DT_INST_PWMS_PERIOD */
1159 	zassert_equal(DT_INST_PWMS_PERIOD(0), 200, "");
1160 
1161 	/* DT_INST_PWMS_FLAGS_BY_IDX */
1162 	zassert_equal(DT_INST_PWMS_FLAGS_BY_IDX(0, 1), 1, "");
1163 
1164 	/* DT_INST_PWMS_FLAGS_BY_NAME */
1165 	zassert_equal(DT_INST_PWMS_FLAGS_BY_NAME(0, red), 3, "");
1166 
1167 	/* DT_INST_PWMS_FLAGS */
1168 	zassert_equal(DT_INST_PWMS_FLAGS(0), 3, "");
1169 }
1170 
test_macro_names(void)1171 static void test_macro_names(void)
1172 {
1173 	/* white box */
1174 	zassert_true(!strcmp(TO_STRING(DT_PATH(test, gpio_deadbeef)),
1175 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1176 	zassert_true(!strcmp(TO_STRING(DT_ALIAS(test_alias)),
1177 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1178 	zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel)),
1179 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1180 	zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel_allcaps)),
1181 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1182 
1183 #define CHILD_NODE_ID DT_CHILD(DT_PATH(test, i2c_11112222), test_i2c_dev_10)
1184 #define FULL_PATH_ID DT_PATH(test, i2c_11112222, test_i2c_dev_10)
1185 
1186 	zassert_true(!strcmp(TO_STRING(CHILD_NODE_ID),
1187 			     TO_STRING(FULL_PATH_ID)), "");
1188 
1189 #undef CHILD_NODE_ID
1190 #undef FULL_PATH_ID
1191 }
1192 
1193 static int a[] = DT_PROP(TEST_ARRAYS, a);
1194 static unsigned char b[] = DT_PROP(TEST_ARRAYS, b);
1195 static char *c[] = DT_PROP(TEST_ARRAYS, c);
1196 
test_arrays(void)1197 static void test_arrays(void)
1198 {
1199 	int ok;
1200 
1201 	zassert_equal(ARRAY_SIZE(a), 3, "");
1202 	zassert_equal(ARRAY_SIZE(b), 4, "");
1203 	zassert_equal(ARRAY_SIZE(c), 2, "");
1204 
1205 	zassert_equal(a[0], 1000, "");
1206 	zassert_equal(a[1], 2000, "");
1207 	zassert_equal(a[2], 3000, "");
1208 
1209 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), "");
1210 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 1), "");
1211 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 2), "");
1212 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), "");
1213 
1214 	/*
1215 	 * Verify that DT_PROP_HAS_IDX can be used with COND_CODE_1()
1216 	 * and COND_CODE_0(), i.e. its expansion is a literal 1 or 0,
1217 	 * not an equivalent expression that evaluates to 1 or 0.
1218 	 */
1219 	ok = 0;
1220 	COND_CODE_1(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), (ok = 1;), ());
1221 	zassert_equal(ok, 1, "");
1222 	ok = 0;
1223 	COND_CODE_0(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), (ok = 1;), ());
1224 	zassert_equal(ok, 1, "");
1225 
1226 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 0), a[0], "");
1227 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 1), a[1], "");
1228 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 2), a[2], "");
1229 
1230 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, a), 3, "");
1231 
1232 	zassert_equal(b[0], 0xaa, "");
1233 	zassert_equal(b[1], 0xbb, "");
1234 	zassert_equal(b[2], 0xcc, "");
1235 	zassert_equal(b[3], 0xdd, "");
1236 
1237 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 0), "");
1238 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 1), "");
1239 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 2), "");
1240 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 3), "");
1241 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 4), "");
1242 
1243 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 0), b[0], "");
1244 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 1), b[1], "");
1245 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 2), b[2], "");
1246 	zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 3), b[3], "");
1247 
1248 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, b), 4, "");
1249 
1250 	zassert_true(!strcmp(c[0], "bar"), "");
1251 	zassert_true(!strcmp(c[1], "baz"), "");
1252 
1253 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 0), "");
1254 	zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 1), "");
1255 	zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 2), "");
1256 
1257 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 0), c[0]), "");
1258 	zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 1), c[1]), "");
1259 
1260 	zassert_equal(DT_PROP_LEN(TEST_ARRAYS, c), 2, "");
1261 }
1262 
1263 #undef DT_DRV_COMPAT
1264 #define DT_DRV_COMPAT vnd_gpio
test_foreach_status_okay(void)1265 static void test_foreach_status_okay(void)
1266 {
1267 	/*
1268 	 * For-each-node type macro tests.
1269 	 *
1270 	 * See test_foreach_prop_elem*() for tests of
1271 	 * for-each-property type macros.
1272 	 */
1273 	unsigned int val;
1274 	const char *str;
1275 
1276 	/* This should expand to something like:
1277 	 *
1278 	 * "/test/enum-0" "/test/enum-1"
1279 	 *
1280 	 * but there is no guarantee about the order of nodes in the
1281 	 * expansion, so we test both.
1282 	 */
1283 	str = DT_FOREACH_STATUS_OKAY(vnd_enum_holder, DT_NODE_PATH);
1284 	zassert_true(!strcmp(str, "/test/enum-0/test/enum-1") ||
1285 		     !strcmp(str, "/test/enum-1/test/enum-0"), "");
1286 
1287 #undef MY_FN
1288 #define MY_FN(node_id, operator) DT_ENUM_IDX(node_id, val) operator
1289 	/* This should expand to something like:
1290 	 *
1291 	 * 0 + 2 + 3
1292 	 *
1293 	 * and order of expansion doesn't matter, since we're adding
1294 	 * the values all up.
1295 	 */
1296 	val = DT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
1297 	zassert_equal(val, 5, "");
1298 
1299 	/*
1300 	 * Make sure DT_INST_FOREACH_STATUS_OKAY can be called from functions
1301 	 * using macros with side effects in the current scope.
1302 	 */
1303 	val = 0;
1304 #define INC(inst_ignored) do { val++; } while (0);
1305 	DT_INST_FOREACH_STATUS_OKAY(INC)
1306 	zassert_equal(val, 2, "");
1307 #undef INC
1308 
1309 	val = 0;
1310 #define INC_ARG(arg) do { val++; val += arg; } while (0)
1311 #define INC(inst_ignored, arg) INC_ARG(arg);
1312 	DT_INST_FOREACH_STATUS_OKAY_VARGS(INC, 1)
1313 	zassert_equal(val, 4, "");
1314 #undef INC_ARG
1315 #undef INC
1316 
1317 	/*
1318 	 * Make sure DT_INST_FOREACH_STATUS_OKAY works with 0 instances, and does
1319 	 * not expand its argument at all.
1320 	 */
1321 #undef DT_DRV_COMPAT
1322 #define DT_DRV_COMPAT xxxx
1323 #define BUILD_BUG_ON_EXPANSION (there is a bug in devicetree.h)
1324 	DT_INST_FOREACH_STATUS_OKAY(BUILD_BUG_ON_EXPANSION)
1325 #undef BUILD_BUG_ON_EXPANSION
1326 
1327 #undef DT_DRV_COMPAT
1328 #define DT_DRV_COMPAT xxxx
1329 #define BUILD_BUG_ON_EXPANSION(arg) (there is a bug in devicetree.h)
1330 	DT_INST_FOREACH_STATUS_OKAY_VARGS(BUILD_BUG_ON_EXPANSION, 1)
1331 #undef BUILD_BUG_ON_EXPANSION
1332 }
1333 
test_foreach_prop_elem(void)1334 static void test_foreach_prop_elem(void)
1335 {
1336 #define TIMES_TWO(node_id, prop, idx) \
1337 	(2 * DT_PROP_BY_IDX(node_id, prop, idx)),
1338 
1339 	int array[] = {
1340 		DT_FOREACH_PROP_ELEM(TEST_ARRAYS, a, TIMES_TWO)
1341 	};
1342 
1343 	zassert_equal(ARRAY_SIZE(array), 3, "");
1344 	zassert_equal(array[0], 2000, "");
1345 	zassert_equal(array[1], 4000, "");
1346 	zassert_equal(array[2], 6000, "");
1347 
1348 #undef DT_DRV_COMPAT
1349 #define DT_DRV_COMPAT vnd_array_holder
1350 
1351 	int inst_array[] = {
1352 		DT_INST_FOREACH_PROP_ELEM(0, a, TIMES_TWO)
1353 	};
1354 
1355 	zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array), "");
1356 	zassert_equal(inst_array[0], array[0], "");
1357 	zassert_equal(inst_array[1], array[1], "");
1358 	zassert_equal(inst_array[2], array[2], "");
1359 #undef TIMES_TWO
1360 }
1361 
test_foreach_prop_elem_varg(void)1362 static void test_foreach_prop_elem_varg(void)
1363 {
1364 #define TIMES_TWO_ADD(node_id, prop, idx, arg) \
1365 	((2 * DT_PROP_BY_IDX(node_id, prop, idx)) + arg),
1366 
1367 	int array[] = {
1368 		DT_FOREACH_PROP_ELEM_VARGS(TEST_ARRAYS, a, TIMES_TWO_ADD, 3)
1369 	};
1370 
1371 	zassert_equal(ARRAY_SIZE(array), 3, "");
1372 	zassert_equal(array[0], 2003, "");
1373 	zassert_equal(array[1], 4003, "");
1374 	zassert_equal(array[2], 6003, "");
1375 
1376 #undef DT_DRV_COMPAT
1377 #define DT_DRV_COMPAT vnd_array_holder
1378 
1379 	int inst_array[] = {
1380 		DT_INST_FOREACH_PROP_ELEM_VARGS(0, a, TIMES_TWO_ADD, 3)
1381 	};
1382 
1383 	zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array), "");
1384 	zassert_equal(inst_array[0], array[0], "");
1385 	zassert_equal(inst_array[1], array[1], "");
1386 	zassert_equal(inst_array[2], array[2], "");
1387 #undef TIMES_TWO
1388 }
1389 
1390 struct test_gpio_info {
1391 	uint32_t reg_addr;
1392 	uint32_t reg_len;
1393 };
1394 
1395 struct test_gpio_data {
1396 	bool init_called;
1397 	bool is_gpio_ctlr;
1398 };
1399 
test_gpio_init(const struct device * dev)1400 static int test_gpio_init(const struct device *dev)
1401 {
1402 	struct test_gpio_data *data = dev->data;
1403 
1404 	data->init_called = 1;
1405 	return 0;
1406 }
1407 
1408 #define INST(num) DT_INST(num, vnd_gpio)
1409 #undef DT_DRV_COMPAT
1410 #define DT_DRV_COMPAT vnd_gpio
1411 
1412 static const struct gpio_driver_api test_api;
1413 
1414 #define TEST_GPIO_INIT(num)					\
1415 	static struct test_gpio_data gpio_data_##num = {	\
1416 		.is_gpio_ctlr = DT_PROP(INST(num),		\
1417 					gpio_controller),	\
1418 	};							\
1419 	static const struct test_gpio_info gpio_info_##num = {	\
1420 		.reg_addr = DT_REG_ADDR(INST(num)),		\
1421 		.reg_len = DT_REG_SIZE(INST(num)),		\
1422 	};							\
1423 	DEVICE_DT_DEFINE(INST(num),				\
1424 			    test_gpio_init,			\
1425 			    NULL,				\
1426 			    &gpio_data_##num,			\
1427 			    &gpio_info_##num,			\
1428 			    POST_KERNEL,			\
1429 			    CONFIG_APPLICATION_INIT_PRIORITY,	\
1430 			    &test_api);
1431 
DT_INST_FOREACH_STATUS_OKAY(TEST_GPIO_INIT)1432 DT_INST_FOREACH_STATUS_OKAY(TEST_GPIO_INIT)
1433 
1434 static inline struct test_gpio_data *to_data(const struct device *dev)
1435 {
1436 	return (struct test_gpio_data *)dev->data;
1437 }
1438 
to_info(const struct device * dev)1439 static inline const struct test_gpio_info *to_info(const struct device *dev)
1440 {
1441 	return (const struct test_gpio_info *)dev->config;
1442 }
1443 
test_devices(void)1444 static void test_devices(void)
1445 {
1446 	const struct device *devs[3];
1447 	int i = 0;
1448 	const struct device *dev_abcd;
1449 
1450 	zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio), 2, "");
1451 
1452 	devs[i] = device_get_binding(DT_LABEL(INST(0)));
1453 	if (devs[i]) {
1454 		i++;
1455 	}
1456 	devs[i] = device_get_binding(DT_LABEL(INST(1)));
1457 	if (devs[i]) {
1458 		i++;
1459 	}
1460 	devs[i] = device_get_binding(DT_LABEL(INST(2)));
1461 	if (devs[i]) {
1462 		i++;
1463 	}
1464 
1465 	zassert_not_null(devs[0], "");
1466 	zassert_not_null(devs[1], "");
1467 	zassert_true(devs[2] == NULL, "");
1468 
1469 	zassert_true(to_data(devs[0])->is_gpio_ctlr, "");
1470 	zassert_true(to_data(devs[1])->is_gpio_ctlr, "");
1471 	zassert_true(to_data(devs[0])->init_called, "");
1472 	zassert_true(to_data(devs[1])->init_called, "");
1473 
1474 	dev_abcd = device_get_binding(DT_LABEL(TEST_ABCD1234));
1475 	zassert_not_null(dev_abcd, "");
1476 	zassert_equal(to_info(dev_abcd)->reg_addr, 0xabcd1234, "");
1477 	zassert_equal(to_info(dev_abcd)->reg_len, 0x500, "");
1478 }
1479 
test_cs_gpios(void)1480 static void test_cs_gpios(void)
1481 {
1482 	zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
1483 	zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
1484 
1485 	zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI), 1, "");
1486 	zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI), 3, "");
1487 
1488 	zassert_equal(DT_DEP_ORD(DT_SPI_DEV_CS_GPIOS_CTLR(TEST_SPI_DEV_0)),
1489 		      DT_DEP_ORD(DT_NODELABEL(test_gpio_1)),
1490 		     "dev 0 cs gpio controller");
1491 	zassert_true(!strcmp(DT_SPI_DEV_CS_GPIOS_LABEL(TEST_SPI_DEV_0),
1492 			     "TEST_GPIO_1"), "");
1493 	zassert_equal(DT_SPI_DEV_CS_GPIOS_PIN(TEST_SPI_DEV_0), 0x10, "");
1494 	zassert_equal(DT_SPI_DEV_CS_GPIOS_FLAGS(TEST_SPI_DEV_0), 0x20, "");
1495 }
1496 
test_chosen(void)1497 static void test_chosen(void)
1498 {
1499 	zassert_equal(DT_HAS_CHOSEN(ztest_xxxx), 0, "");
1500 	zassert_equal(DT_HAS_CHOSEN(ztest_gpio), 1, "");
1501 	zassert_true(!strcmp(TO_STRING(DT_CHOSEN(ztest_gpio)),
1502 			     "DT_N_S_test_S_gpio_deadbeef"), "");
1503 }
1504 
1505 #define TO_MY_ENUM(token) TO_MY_ENUM_2(token) /* force another expansion */
1506 #define TO_MY_ENUM_2(token) MY_ENUM_ ## token
test_enums(void)1507 static void test_enums(void)
1508 {
1509 	enum {
1510 		MY_ENUM_zero = 0xff,
1511 		MY_ENUM_ZERO = 0xaa,
1512 	};
1513 
1514 	zassert_equal(DT_ENUM_IDX(TEST_ENUM_0, val), 0, "0");
1515 	zassert_equal(TO_MY_ENUM(DT_ENUM_TOKEN(TEST_ENUM_0, val)), 0xff, "");
1516 	zassert_equal(TO_MY_ENUM(DT_ENUM_UPPER_TOKEN(TEST_ENUM_0, val)),
1517 		      0xaa, "");
1518 }
1519 #undef TO_MY_ENUM
1520 #undef TO_MY_ENUM_2
1521 
test_enums_required_false(void)1522 static void test_enums_required_false(void)
1523 {
1524 	/* DT_ENUM_IDX_OR on string value */
1525 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_0), val, 2),
1526 		      1, "");
1527 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_1), val, 2),
1528 		      2, "");
1529 	/* DT_ENUM_IDX_OR on int value */
1530 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_0),
1531 				     val, 4),
1532 		      0, "");
1533 	zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_1),
1534 				     val, 4),
1535 		      4, "");
1536 }
1537 
1538 #undef DT_DRV_COMPAT
1539 #define DT_DRV_COMPAT vnd_adc_temp_sensor
test_clocks(void)1540 static void test_clocks(void)
1541 {
1542 	/* DT_CLOCKS_CTLR_BY_IDX */
1543 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_IDX(TEST_TEMP, 1),
1544 				  DT_NODELABEL(test_fixed_clk)), "");
1545 
1546 	/* DT_CLOCKS_CTLR */
1547 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR(TEST_TEMP),
1548 				  DT_NODELABEL(test_clk)), "");
1549 
1550 	/* DT_CLOCKS_CTLR_BY_NAME */
1551 	zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_NAME(TEST_TEMP, clk_b),
1552 				  DT_NODELABEL(test_clk)), "");
1553 
1554 	/* DT_CLOCKS_LABEL_BY_IDX */
1555 	zassert_true(!strcmp(DT_CLOCKS_LABEL_BY_IDX(TEST_TEMP, 0),
1556 			     "TEST_CLOCK"), "");
1557 
1558 	/* DT_CLOCKS_LABEL_BY_NAME */
1559 	zassert_true(!strcmp(DT_CLOCKS_LABEL_BY_NAME(TEST_TEMP, clk_b),
1560 			     "TEST_CLOCK"), "");
1561 
1562 	/* DT_CLOCKS_LABEL */
1563 	zassert_true(!strcmp(DT_CLOCKS_LABEL(TEST_TEMP), "TEST_CLOCK"), "");
1564 
1565 	/* DT_CLOCKS_CELL_BY_IDX */
1566 	zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bits), 2, "");
1567 	zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bus), 8, "");
1568 
1569 	/* DT_CLOCKS_CELL_BY_NAME */
1570 	zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_a, bits), 7, "");
1571 	zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_b, bus), 8, "");
1572 
1573 	/* DT_CLOCKS_CELL */
1574 	zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bits), 7, "");
1575 	zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bus), 3, "");
1576 
1577 	/* clock-freq on fixed clock */
1578 	zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_TEMP, clocks, 1,
1579 					     clock_frequency),
1580 		      25000000, "");
1581 
1582 	/* DT_INST */
1583 	zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1584 
1585 	/* DT_INST_CLOCKS_CTLR_BY_IDX */
1586 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_IDX(0, 1),
1587 				  DT_NODELABEL(test_fixed_clk)), "");
1588 
1589 	/* DT_INST_CLOCKS_CTLR */
1590 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR(0),
1591 				  DT_NODELABEL(test_clk)), "");
1592 
1593 	/* DT_INST_CLOCKS_CTLR_BY_NAME */
1594 	zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_NAME(0, clk_b),
1595 				  DT_NODELABEL(test_clk)), "");
1596 
1597 	/* DT_INST_CLOCKS_LABEL_BY_IDX */
1598 	zassert_true(!strcmp(DT_INST_CLOCKS_LABEL_BY_IDX(0, 0),
1599 			     "TEST_CLOCK"), "");
1600 
1601 	/* DT_INST_CLOCKS_LABEL_BY_NAME */
1602 	zassert_true(!strcmp(DT_INST_CLOCKS_LABEL_BY_NAME(0, clk_b),
1603 			     "TEST_CLOCK"), "");
1604 
1605 	/* DT_INST_CLOCKS_LABEL */
1606 	zassert_true(!strcmp(DT_INST_CLOCKS_LABEL(0), "TEST_CLOCK"), "");
1607 
1608 	/* DT_INST_CLOCKS_CELL_BY_IDX */
1609 	zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bits), 2, "");
1610 	zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bus), 8, "");
1611 
1612 	/* DT_INST_CLOCKS_CELL_BY_NAME */
1613 	zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_a, bits), 7, "");
1614 	zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_b, bus), 8, "");
1615 
1616 	/* DT_INST_CLOCKS_CELL */
1617 	zassert_equal(DT_INST_CLOCKS_CELL(0, bits), 7, "");
1618 	zassert_equal(DT_INST_CLOCKS_CELL(0, bus), 3, "");
1619 
1620 	/* clock-freq on fixed clock */
1621 	zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, clocks, 1,
1622 						  clock_frequency),
1623 		      25000000, "");
1624 }
1625 
test_parent(void)1626 static void test_parent(void)
1627 {
1628 	/* The label of a child node's parent is the label of the parent. */
1629 	zassert_true(!strcmp(DT_LABEL(DT_PARENT(TEST_SPI_DEV_0)),
1630 			     DT_LABEL(TEST_SPI_BUS_0)), "");
1631 	/*
1632 	 * We should be able to use DT_PARENT() even with nodes, like /test,
1633 	 * that have no matching compatible.
1634 	 */
1635 	zassert_true(!strcmp(DT_LABEL(DT_CHILD(DT_PARENT(TEST_SPI_BUS_0),
1636 					       spi_33334444)),
1637 			     DT_LABEL(TEST_SPI_BUS_0)), "");
1638 }
1639 
1640 #undef DT_DRV_COMPAT
1641 #define DT_DRV_COMPAT vnd_child_bindings
test_child_nodes_list(void)1642 static void test_child_nodes_list(void)
1643 {
1644 	#define TEST_FUNC(child) { DT_PROP(child, val) },
1645 	#define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
1646 
1647 	struct vnd_child_binding {
1648 		int val;
1649 	};
1650 
1651 	struct vnd_child_binding vals[] = {
1652 		DT_FOREACH_CHILD(TEST_PARENT, TEST_FUNC)
1653 	};
1654 
1655 	struct vnd_child_binding vals_inst[] = {
1656 		DT_INST_FOREACH_CHILD(0, TEST_FUNC)
1657 	};
1658 
1659 	struct vnd_child_binding vals_status_okay[] = {
1660 		DT_FOREACH_CHILD_STATUS_OKAY(TEST_PARENT, TEST_FUNC)
1661 	};
1662 
1663 	zassert_equal(ARRAY_SIZE(vals), 3, "");
1664 	zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
1665 	zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
1666 
1667 	zassert_equal(vals[0].val, 0, "");
1668 	zassert_equal(vals[1].val, 1, "");
1669 	zassert_equal(vals[2].val, 2, "");
1670 	zassert_equal(vals_inst[0].val, 0, "");
1671 	zassert_equal(vals_inst[1].val, 1, "");
1672 	zassert_equal(vals_inst[2].val, 2, "");
1673 	zassert_equal(vals_status_okay[0].val, 0, "");
1674 	zassert_equal(vals_status_okay[1].val, 1, "");
1675 
1676 	#undef TEST_PARENT
1677 	#undef TEST_FUNC
1678 }
1679 
1680 #undef DT_DRV_COMPAT
1681 #define DT_DRV_COMPAT vnd_child_bindings
test_child_nodes_list_varg(void)1682 static void test_child_nodes_list_varg(void)
1683 {
1684 	#define TEST_FUNC(child, arg) { DT_PROP(child, val) + arg },
1685 	#define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
1686 
1687 	struct vnd_child_binding {
1688 		int val;
1689 	};
1690 
1691 	struct vnd_child_binding vals[] = {
1692 		DT_FOREACH_CHILD_VARGS(TEST_PARENT, TEST_FUNC, 1)
1693 	};
1694 
1695 	struct vnd_child_binding vals_inst[] = {
1696 		DT_INST_FOREACH_CHILD_VARGS(0, TEST_FUNC, 1)
1697 	};
1698 
1699 	struct vnd_child_binding vals_status_okay[] = {
1700 		DT_FOREACH_CHILD_STATUS_OKAY_VARGS(TEST_PARENT, TEST_FUNC, 1)
1701 	};
1702 
1703 	zassert_equal(ARRAY_SIZE(vals), 3, "");
1704 	zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
1705 	zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
1706 
1707 	zassert_equal(vals[0].val, 1, "");
1708 	zassert_equal(vals[1].val, 2, "");
1709 	zassert_equal(vals[2].val, 3, "");
1710 	zassert_equal(vals_inst[0].val, 1, "");
1711 	zassert_equal(vals_inst[1].val, 2, "");
1712 	zassert_equal(vals_inst[2].val, 3, "");
1713 	zassert_equal(vals_status_okay[0].val, 1, "");
1714 	zassert_equal(vals_status_okay[1].val, 2, "");
1715 
1716 	#undef TEST_PARENT
1717 	#undef TEST_FUNC
1718 }
1719 
test_great_grandchild(void)1720 static void test_great_grandchild(void)
1721 {
1722 	zassert_equal(DT_PROP(DT_NODELABEL(test_ggc), ggc_prop), 42, "");
1723 }
1724 
test_compat_get_any_status_okay(void)1725 static void test_compat_get_any_status_okay(void)
1726 {
1727 	zassert_true(
1728 		DT_SAME_NODE(
1729 			DT_COMPAT_GET_ANY_STATUS_OKAY(vnd_reg_holder),
1730 			TEST_REG),
1731 		"");
1732 
1733 	/*
1734 	 * DT_SAME_NODE requires that both its arguments are valid
1735 	 * node identifiers, so we can't pass it DT_INVALID_NODE,
1736 	 * which is what this DT_COMPAT_GET_ANY_STATUS_OKAY() expands to.
1737 	 */
1738 	zassert_false(
1739 		DT_NODE_EXISTS(
1740 			DT_COMPAT_GET_ANY_STATUS_OKAY(this_is_not_a_real_compat)),
1741 		"");
1742 }
1743 
ord_in_array(unsigned int ord,unsigned int * array,size_t array_size)1744 static bool ord_in_array(unsigned int ord, unsigned int *array,
1745 			 size_t array_size)
1746 {
1747 	size_t i;
1748 
1749 	for (i = 0; i < array_size; i++) {
1750 		if (array[i] == ord) {
1751 			return true;
1752 		}
1753 	}
1754 
1755 	return false;
1756 }
1757 
1758 /* Magic numbers used by COMBINED_ORD_ARRAY. Must be invalid dependency
1759  * ordinals.
1760  */
1761 #define ORD_LIST_SEP		0xFFFF0000
1762 #define ORD_LIST_END		0xFFFF0001
1763 #define INJECTED_DEP_0		0xFFFF0002
1764 #define INJECTED_DEP_1		0xFFFF0003
1765 
1766 #define DEP_ORD_AND_COMMA(node_id) DT_DEP_ORD(node_id),
1767 #define CHILD_ORDINALS(node_id) DT_FOREACH_CHILD(node_id, DEP_ORD_AND_COMMA)
1768 
1769 #define COMBINED_ORD_ARRAY(node_id)		\
1770 	{					\
1771 		DT_DEP_ORD(node_id),		\
1772 		DT_DEP_ORD(DT_PARENT(node_id)),	\
1773 		CHILD_ORDINALS(node_id)		\
1774 		ORD_LIST_SEP,			\
1775 		DT_REQUIRES_DEP_ORDS(node_id)	\
1776 		INJECTED_DEP_0,			\
1777 		INJECTED_DEP_1,			\
1778 		ORD_LIST_SEP,			\
1779 		DT_SUPPORTS_DEP_ORDS(node_id)	\
1780 		ORD_LIST_END			\
1781 	}
1782 
test_dep_ord(void)1783 static void test_dep_ord(void)
1784 {
1785 #define ORD_IN_ARRAY(ord, array) ord_in_array(ord, array, ARRAY_SIZE(array))
1786 
1787 	unsigned int root_ord = DT_DEP_ORD(DT_ROOT),
1788 		test_ord = DT_DEP_ORD(DT_PATH(test)),
1789 		root_requires[] = { DT_REQUIRES_DEP_ORDS(DT_ROOT) },
1790 		test_requires[] = { DT_REQUIRES_DEP_ORDS(DT_PATH(test)) },
1791 		root_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_ROOT) },
1792 		test_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_PATH(test)) },
1793 		children_ords[] = {
1794 			DT_FOREACH_CHILD(TEST_CHILDREN, DEP_ORD_AND_COMMA)
1795 		},
1796 		children_combined_ords[] = COMBINED_ORD_ARRAY(TEST_CHILDREN),
1797 		child_a_combined_ords[] =
1798 			COMBINED_ORD_ARRAY(DT_NODELABEL(test_child_a));
1799 	size_t i;
1800 
1801 	/* DT_DEP_ORD */
1802 	zassert_equal(root_ord, 0, "");
1803 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_child_a)) >
1804 		     DT_DEP_ORD(DT_NODELABEL(test_children)), "");
1805 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_irq)) >
1806 		     DT_DEP_ORD(DT_NODELABEL(test_intc)), "");
1807 	zassert_true(DT_DEP_ORD(DT_NODELABEL(test_phandles)) >
1808 		     DT_DEP_ORD(DT_NODELABEL(test_gpio_1)), "");
1809 
1810 	/* DT_REQUIRES_DEP_ORDS */
1811 	zassert_equal(ARRAY_SIZE(root_requires), 0, "");
1812 	zassert_true(ORD_IN_ARRAY(root_ord, test_requires), "");
1813 
1814 	/* DT_SUPPORTS_DEP_ORDS */
1815 	zassert_true(ORD_IN_ARRAY(test_ord, root_supports), "");
1816 	zassert_false(ORD_IN_ARRAY(root_ord, test_supports), "");
1817 
1818 	unsigned int children_combined_ords_expected[] = {
1819 		/*
1820 		 * Combined ordinals for /test/test-children are from
1821 		 * these nodes in this order:
1822 		 */
1823 		DT_DEP_ORD(TEST_CHILDREN),		/* node */
1824 		DT_DEP_ORD(DT_PATH(test)),		/* parent */
1825 		DT_DEP_ORD(DT_NODELABEL(test_child_a)),	/* children */
1826 		DT_DEP_ORD(DT_NODELABEL(test_child_b)),
1827 		DT_DEP_ORD(DT_NODELABEL(test_child_c)),
1828 		ORD_LIST_SEP,				/* separator */
1829 		DT_DEP_ORD(DT_PATH(test)),		/* requires */
1830 		INJECTED_DEP_0,				/* injected
1831 							 * dependencies
1832 							 */
1833 		INJECTED_DEP_1,
1834 		ORD_LIST_SEP,				/* separator */
1835 		DT_DEP_ORD(DT_NODELABEL(test_child_a)),	/* supports */
1836 		DT_DEP_ORD(DT_NODELABEL(test_child_b)),
1837 		DT_DEP_ORD(DT_NODELABEL(test_child_c)),
1838 		ORD_LIST_END,				/* terminator */
1839 	};
1840 	zassert_equal(ARRAY_SIZE(children_combined_ords),
1841 		      ARRAY_SIZE(children_combined_ords_expected),
1842 		      "%u", ARRAY_SIZE(children_combined_ords));
1843 	for (i = 0; i < ARRAY_SIZE(children_combined_ords); i++) {
1844 		zassert_equal(children_combined_ords[i],
1845 			      children_combined_ords_expected[i],
1846 			      "test-children at %zu", i);
1847 	}
1848 
1849 	unsigned int child_a_combined_ords_expected[] = {
1850 		/*
1851 		 * Combined ordinals for /test/test-children/child-a
1852 		 * are from these nodes in this order:
1853 		 */
1854 		DT_DEP_ORD(DT_NODELABEL(test_child_a)), /* node */
1855 		DT_DEP_ORD(TEST_CHILDREN),		/* parent */
1856 		/* children (none) */
1857 		ORD_LIST_SEP,				/* separator */
1858 		DT_DEP_ORD(TEST_CHILDREN),		/* requires */
1859 		INJECTED_DEP_0,				/* injected
1860 							 * dependencies
1861 							 */
1862 		INJECTED_DEP_1,
1863 		ORD_LIST_SEP,				/* separator */
1864 		/* supports (none) */
1865 		ORD_LIST_END,				/* terminator */
1866 	};
1867 	zassert_equal(ARRAY_SIZE(child_a_combined_ords),
1868 		      ARRAY_SIZE(child_a_combined_ords_expected),
1869 		      "%u", ARRAY_SIZE(child_a_combined_ords));
1870 	for (i = 0; i < ARRAY_SIZE(child_a_combined_ords); i++) {
1871 		zassert_equal(child_a_combined_ords[i],
1872 			      child_a_combined_ords_expected[i],
1873 			      "child-a at %zu", i);
1874 	}
1875 
1876 #undef DT_DRV_COMPAT
1877 #define DT_DRV_COMPAT vnd_child_bindings
1878 
1879 	/* DT_INST_DEP_ORD */
1880 	zassert_equal(DT_INST_DEP_ORD(0),
1881 		      DT_DEP_ORD(DT_NODELABEL(test_children)), "");
1882 
1883 	/* DT_INST_REQUIRES_DEP_ORDS */
1884 	unsigned int inst_requires[] = { DT_INST_REQUIRES_DEP_ORDS(0) };
1885 
1886 	zassert_equal(ARRAY_SIZE(inst_requires), 1, "");
1887 	zassert_equal(inst_requires[0], test_ord, "");
1888 
1889 	/* DT_INST_SUPPORTS_DEP_ORDS */
1890 	unsigned int inst_supports[] = { DT_INST_SUPPORTS_DEP_ORDS(0) };
1891 
1892 	zassert_equal(ARRAY_SIZE(inst_supports), 3, "");
1893 	for (i = 0; i < ARRAY_SIZE(inst_supports); i++) {
1894 		zassert_true(ORD_IN_ARRAY(inst_supports[i], children_ords), "");
1895 	}
1896 }
1897 
test_path(void)1898 static void test_path(void)
1899 {
1900 	zassert_true(!strcmp(DT_NODE_PATH(DT_ROOT), "/"), "");
1901 	zassert_true(!strcmp(DT_NODE_PATH(TEST_DEADBEEF),
1902 			     "/test/gpio@deadbeef"), "");
1903 }
1904 
test_node_name(void)1905 static void test_node_name(void)
1906 {
1907 	zassert_true(!strcmp(DT_NODE_FULL_NAME(DT_ROOT), "/"), "");
1908 	zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_DEADBEEF),
1909 			     "gpio@deadbeef"), "");
1910 	zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_TEMP),
1911 			     "temperature-sensor"), "");
1912 	zassert_true(strcmp(DT_NODE_FULL_NAME(TEST_REG),
1913 			     "reg-holder"), "");
1914 }
1915 
test_same_node(void)1916 static void test_same_node(void)
1917 {
1918 	zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
1919 	zassert_false(DT_SAME_NODE(TEST_DEADBEEF, TEST_ABCD1234), "");
1920 }
1921 
test_pinctrl(void)1922 static void test_pinctrl(void)
1923 {
1924 #undef DT_DRV_COMPAT
1925 #define DT_DRV_COMPAT vnd_adc_temp_sensor
1926 	/*
1927 	 * Tests when a node does have pinctrl properties.
1928 	 */
1929 
1930 	/*
1931 	 * node_id versions:
1932 	 */
1933 
1934 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 0, 1),
1935 				  DT_NODELABEL(test_pincfg_b)), "");
1936 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 1, 0),
1937 				  DT_NODELABEL(test_pincfg_c)), "");
1938 
1939 	zassert_true(DT_SAME_NODE(DT_PINCTRL_0(TEST_TEMP, 0),
1940 				  DT_NODELABEL(test_pincfg_a)), "");
1941 
1942 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, default, 1),
1943 				  DT_NODELABEL(test_pincfg_b)), "");
1944 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, sleep, 0),
1945 				  DT_NODELABEL(test_pincfg_c)), "");
1946 	zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, f_o_o2, 0),
1947 				  DT_NODELABEL(test_pincfg_d)), "");
1948 
1949 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, default), 0, "");
1950 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, sleep), 1, "");
1951 	zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, f_o_o2), 2, "");
1952 
1953 	zassert_equal(DT_NUM_PINCTRLS_BY_IDX(TEST_TEMP, 0), 2, "");
1954 
1955 	zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, default), 2, "");
1956 	zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, f_o_o2), 1, "");
1957 
1958 	zassert_equal(DT_NUM_PINCTRL_STATES(TEST_TEMP), 3, "");
1959 
1960 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 0), 1, "");
1961 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 1), 1, "");
1962 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 2), 1, "");
1963 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 3), 0, "");
1964 
1965 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, default), 1, "");
1966 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, sleep), 1, "");
1967 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, f_o_o2), 1, "");
1968 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, bar), 0, "");
1969 
1970 #undef MAKE_TOKEN
1971 #define MAKE_TOKEN(pc_idx)						\
1972 	_CONCAT(NODE_ID_ENUM_,						\
1973 		DT_PINCTRL_IDX_TO_NAME_TOKEN(TEST_TEMP, pc_idx))
1974 #undef MAKE_UPPER_TOKEN
1975 #define MAKE_UPPER_TOKEN(pc_idx)					\
1976 	_CONCAT(NODE_ID_ENUM_,						\
1977 		DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(TEST_TEMP, pc_idx))
1978 	enum {
1979 		MAKE_TOKEN(0) = 10,
1980 		MAKE_TOKEN(1) = 11,
1981 		MAKE_TOKEN(2) = 12,
1982 		MAKE_TOKEN(3) = 13,
1983 
1984 		MAKE_UPPER_TOKEN(0) = 20,
1985 		MAKE_UPPER_TOKEN(1) = 21,
1986 		MAKE_UPPER_TOKEN(2) = 22,
1987 		MAKE_UPPER_TOKEN(3) = 23,
1988 	};
1989 
1990 	zassert_equal(NODE_ID_ENUM_default, 10, "");
1991 	zassert_equal(NODE_ID_ENUM_sleep, 11, "");
1992 	zassert_equal(NODE_ID_ENUM_f_o_o2, 12, "");
1993 
1994 	zassert_equal(NODE_ID_ENUM_DEFAULT, 20, "");
1995 	zassert_equal(NODE_ID_ENUM_SLEEP, 21, "");
1996 	zassert_equal(NODE_ID_ENUM_F_O_O2, 22, "");
1997 
1998 	/*
1999 	 * inst versions:
2000 	 */
2001 
2002 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 0, 1),
2003 				  DT_NODELABEL(test_pincfg_b)), "");
2004 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 1, 0),
2005 				  DT_NODELABEL(test_pincfg_c)), "");
2006 
2007 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_0(0, 0),
2008 				  DT_NODELABEL(test_pincfg_a)), "");
2009 
2010 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, default, 1),
2011 				  DT_NODELABEL(test_pincfg_b)), "");
2012 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, sleep, 0),
2013 				  DT_NODELABEL(test_pincfg_c)), "");
2014 	zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, f_o_o2, 0),
2015 				  DT_NODELABEL(test_pincfg_d)), "");
2016 
2017 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, default), 0, "");
2018 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, sleep), 1, "");
2019 	zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, f_o_o2), 2, "");
2020 
2021 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_IDX(0, 0), 2, "");
2022 
2023 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, default), 2, "");
2024 	zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, f_o_o2), 1, "");
2025 
2026 	zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 3, "");
2027 
2028 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 1, "");
2029 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 1), 1, "");
2030 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 2), 1, "");
2031 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 3), 0, "");
2032 
2033 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, default), 1, "");
2034 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, sleep), 1, "");
2035 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 1, "");
2036 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, bar), 0, "");
2037 
2038 #undef MAKE_TOKEN
2039 #define MAKE_TOKEN(pc_idx)						\
2040 	_CONCAT(INST_ENUM_,						\
2041 			DT_INST_PINCTRL_IDX_TO_NAME_TOKEN(0, pc_idx))
2042 #undef MAKE_UPPER_TOKEN
2043 #define MAKE_UPPER_TOKEN(pc_idx)					\
2044 	_CONCAT(INST_ENUM_,						\
2045 			DT_INST_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(0, pc_idx))
2046 	enum {
2047 		MAKE_TOKEN(0) = 10,
2048 		MAKE_TOKEN(1) = 11,
2049 		MAKE_TOKEN(2) = 12,
2050 
2051 		MAKE_UPPER_TOKEN(0) = 20,
2052 		MAKE_UPPER_TOKEN(1) = 21,
2053 		MAKE_UPPER_TOKEN(2) = 22,
2054 	};
2055 
2056 	zassert_equal(INST_ENUM_default, 10, "");
2057 	zassert_equal(INST_ENUM_sleep, 11, "");
2058 	zassert_equal(INST_ENUM_f_o_o2, 12, "");
2059 
2060 	zassert_equal(INST_ENUM_DEFAULT, 20, "");
2061 	zassert_equal(INST_ENUM_SLEEP, 21, "");
2062 	zassert_equal(INST_ENUM_F_O_O2, 22, "");
2063 
2064 #undef DT_DRV_COMPAT
2065 #define DT_DRV_COMPAT vnd_reg_holder
2066 	/*
2067 	 * Tests when a node does NOT have any pinctrl properties.
2068 	 */
2069 
2070 	/* node_id versions */
2071 	zassert_equal(DT_NUM_PINCTRL_STATES(TEST_REG), 0, "");
2072 	zassert_equal(DT_PINCTRL_HAS_IDX(TEST_REG, 0), 0, "");
2073 	zassert_equal(DT_PINCTRL_HAS_NAME(TEST_REG, f_o_o2), 0, "");
2074 
2075 	/* inst versions */
2076 	zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 0, "");
2077 	zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 0, "");
2078 	zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 0, "");
2079 }
2080 
test_main(void)2081 void test_main(void)
2082 {
2083 	ztest_test_suite(devicetree_api,
2084 			 ztest_unit_test(test_path_props),
2085 			 ztest_unit_test(test_alias_props),
2086 			 ztest_unit_test(test_nodelabel_props),
2087 			 ztest_unit_test(test_inst_props),
2088 			 ztest_unit_test(test_default_prop_access),
2089 			 ztest_unit_test(test_has_path),
2090 			 ztest_unit_test(test_has_alias),
2091 			 ztest_unit_test(test_inst_checks),
2092 			 ztest_unit_test(test_has_nodelabel),
2093 			 ztest_unit_test(test_has_compat),
2094 			 ztest_unit_test(test_has_status),
2095 			 ztest_unit_test(test_bus),
2096 			 ztest_unit_test(test_reg),
2097 			 ztest_unit_test(test_irq),
2098 			 ztest_unit_test(test_phandles),
2099 			 ztest_unit_test(test_gpio),
2100 			 ztest_unit_test(test_io_channels),
2101 			 ztest_unit_test(test_dma),
2102 			 ztest_unit_test(test_pwms),
2103 			 ztest_unit_test(test_macro_names),
2104 			 ztest_unit_test(test_arrays),
2105 			 ztest_unit_test(test_foreach_status_okay),
2106 			 ztest_unit_test(test_foreach_prop_elem),
2107 			 ztest_unit_test(test_foreach_prop_elem_varg),
2108 			 ztest_unit_test(test_devices),
2109 			 ztest_unit_test(test_cs_gpios),
2110 			 ztest_unit_test(test_chosen),
2111 			 ztest_unit_test(test_enums),
2112 			 ztest_unit_test(test_enums_required_false),
2113 			 ztest_unit_test(test_clocks),
2114 			 ztest_unit_test(test_parent),
2115 			 ztest_unit_test(test_child_nodes_list),
2116 			 ztest_unit_test(test_child_nodes_list_varg),
2117 			 ztest_unit_test(test_great_grandchild),
2118 			 ztest_unit_test(test_compat_get_any_status_okay),
2119 			 ztest_unit_test(test_dep_ord),
2120 			 ztest_unit_test(test_path),
2121 			 ztest_unit_test(test_node_name),
2122 			 ztest_unit_test(test_same_node),
2123 			 ztest_unit_test(test_pinctrl)
2124 		);
2125 	ztest_run_test_suite(devicetree_api);
2126 }
2127