1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  * Copyright 2024 NXP
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "test_gpio.h"
9 
10 #define ALL_BITS ((gpio_port_value_t)-1)
11 
12 static const struct device *const dev_in = DEVICE_DT_GET(DEV_IN);
13 static const struct device *const dev_out = DEVICE_DT_GET(DEV_OUT);
14 
15 /* Short-hand for a checked read of PIN_IN raw state */
raw_in(void)16 static bool raw_in(void)
17 {
18 	gpio_port_value_t v;
19 	int rc = gpio_port_get_raw(dev_in, &v);
20 
21 #if CONFIG_READ_DELAY
22 	k_sleep(K_MSEC(CONFIG_READ_DELAY));
23 	rc = gpio_port_get_raw(dev_in, &v);
24 #endif
25 	zassert_equal(rc, 0,
26 		      "raw_in failed");
27 	return (v & BIT(PIN_IN)) ? true : false;
28 }
29 
30 /* Short-hand for a checked read of PIN_IN logical state */
logic_in(void)31 static bool logic_in(void)
32 {
33 	gpio_port_value_t v;
34 	int rc = gpio_port_get(dev_in, &v);
35 
36 #if CONFIG_READ_DELAY
37 	k_sleep(K_MSEC(CONFIG_READ_DELAY));
38 	rc = gpio_port_get(dev_in, &v);
39 #endif
40 	zassert_equal(rc, 0,
41 		      "logic_in failed");
42 	return (v & BIT(PIN_IN)) ? true : false;
43 }
44 
45 /* Short-hand for a checked write of PIN_OUT raw state */
raw_out(bool set)46 static void raw_out(bool set)
47 {
48 	int rc;
49 
50 	if (set) {
51 		rc = gpio_port_set_bits_raw(dev_out, BIT(PIN_OUT));
52 	} else {
53 		rc = gpio_port_clear_bits_raw(dev_out, BIT(PIN_OUT));
54 	}
55 	zassert_equal(rc, 0,
56 		      "raw_out failed");
57 }
58 
59 /* Short-hand for a checked write of PIN_OUT logic state */
logic_out(bool set)60 static void logic_out(bool set)
61 {
62 	int rc;
63 
64 	if (set) {
65 		rc = gpio_port_set_bits(dev_out, BIT(PIN_OUT));
66 	} else {
67 		rc = gpio_port_clear_bits(dev_out, BIT(PIN_OUT));
68 	}
69 	zassert_equal(rc, 0,
70 		      "raw_out failed");
71 }
72 
73 /* Verify device, configure for physical in and out, verify
74  * connection, verify raw_in().
75  */
setup(void)76 static int setup(void)
77 {
78 	int rc;
79 	gpio_port_value_t v1;
80 
81 	TC_PRINT("Validate device %s and %s\n", dev_in->name, dev_out->name);
82 	zassert_true(device_is_ready(dev_out), "GPIO dev is not ready");
83 
84 	TC_PRINT("Check %s output %d connected to %s input %d\n", dev_out->name, PIN_OUT,
85 		 dev_in->name, PIN_IN);
86 
87 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT);
88 	zassert_equal(rc, 0, "pin config input failed");
89 
90 	/* Test output low */
91 	rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_OUTPUT_LOW);
92 	zassert_equal(rc, 0,
93 		      "pin config output low failed");
94 
95 	rc = gpio_port_get_raw(dev_in, &v1);
96 	zassert_equal(rc, 0,
97 		      "get raw low failed");
98 	if (raw_in() != false) {
99 		TC_PRINT("FATAL output pin not wired to input pin? (out low => in high)\n");
100 		while (true) {
101 			k_sleep(K_FOREVER);
102 		}
103 	}
104 
105 	zassert_equal(v1 & BIT(PIN_IN), 0,
106 		      "out low does not read low");
107 
108 	/* Disconnect output */
109 	rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_DISCONNECTED);
110 	if (rc == -ENOTSUP) {
111 		TC_PRINT("NOTE: cannot configure pin as disconnected; trying as input\n");
112 		rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_INPUT);
113 	}
114 	zassert_equal(rc, 0,
115 		      "output disconnect failed");
116 
117 	/* Test output high */
118 	rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_OUTPUT_HIGH | GPIO_PULL_UP);
119 	if (rc == -ENOTSUP) {
120 		TC_PRINT("NOTE: pull-up not supported; trying as output high\n");
121 		rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_OUTPUT_HIGH);
122 	}
123 	zassert_equal(rc, 0,
124 		      "pin config output high failed");
125 
126 	rc = gpio_port_get_raw(dev_in, &v1);
127 	zassert_equal(rc, 0,
128 		      "get raw high failed");
129 	if (raw_in() != true) {
130 		TC_PRINT("FATAL output pin not wired to input pin? (out high => in low)\n");
131 		while (raw_in() != true) {
132 			k_sleep(K_MSEC(100));
133 		}
134 	}
135 	/* rread again in case the gpio changes slow */
136 	gpio_port_get_raw(dev_in, &v1);
137 	zassert_not_equal(v1 & BIT(PIN_IN), 0,
138 			  "out high does not read low");
139 
140 	TC_PRINT("OUT %d to IN %d linkage works\n", PIN_OUT, PIN_IN);
141 	return TC_PASS;
142 }
143 
144 /* gpio_port_set_bits_raw()
145  * gpio_port_clear_bits_raw()
146  * gpio_port_set_masked_raw()
147  * gpio_port_toggle_bits()
148  */
bits_physical(void)149 static int bits_physical(void)
150 {
151 	int rc;
152 
153 	TC_PRINT("- %s\n", __func__);
154 
155 	/* port_set_bits_raw */
156 	rc = gpio_port_set_bits_raw(dev_out, BIT(PIN_OUT));
157 	zassert_equal(rc, 0,
158 		      "port set raw out failed");
159 	zassert_equal(raw_in(), true,
160 		      "raw set mismatch");
161 
162 	/* port_clear_bits_raw */
163 	rc = gpio_port_clear_bits_raw(dev_out, BIT(PIN_OUT));
164 	zassert_equal(rc, 0,
165 		      "port clear raw out failed");
166 	zassert_equal(raw_in(), false,
167 		      "raw clear mismatch");
168 
169 	/* set after clear changes */
170 	rc = gpio_port_set_bits_raw(dev_out, BIT(PIN_OUT));
171 	zassert_equal(rc, 0,
172 		      "port set raw out failed");
173 	zassert_equal(raw_in(), true,
174 		      "raw set mismatch");
175 
176 	/* raw_out() after set works */
177 	raw_out(false);
178 	zassert_equal(raw_in(), false,
179 		      "raw_out() false mismatch");
180 
181 	/* raw_out() set after raw_out() clear works */
182 	raw_out(true);
183 	zassert_equal(raw_in(), true,
184 		      "raw_out() true mismatch");
185 
186 	rc = gpio_port_set_masked_raw(dev_out, BIT(PIN_OUT), 0);
187 	zassert_equal(rc, 0,
188 		      "set_masked_raw low failed");
189 	zassert_equal(raw_in(), false,
190 		      "set_masked_raw low mismatch");
191 
192 	rc = gpio_port_set_masked_raw(dev_out, BIT(PIN_OUT), ALL_BITS);
193 	zassert_equal(rc, 0,
194 		      "set_masked_raw high failed");
195 	zassert_equal(raw_in(), true,
196 		      "set_masked_raw high mismatch");
197 
198 	rc = gpio_port_set_masked_raw(dev_in, BIT(PIN_IN), 0);
199 	zassert_equal(rc, 0,
200 		      "set_masked_raw low failed");
201 	zassert_equal(raw_in(), true,
202 		      "set_masked_raw low affected other pins");
203 
204 	rc = gpio_port_set_clr_bits_raw(dev_out, BIT(PIN_IN), BIT(PIN_OUT));
205 	zassert_equal(rc, 0,
206 		      "set in clear out failed");
207 	zassert_equal(raw_in(), false,
208 		      "set in clear out mismatch");
209 
210 	rc = gpio_port_set_clr_bits_raw(dev_out, BIT(PIN_OUT), BIT(PIN_IN));
211 	zassert_equal(rc, 0,
212 		      "set out clear in failed");
213 	zassert_equal(raw_in(), true,
214 		      "set out clear in mismatch");
215 
216 	/* Conditionally verify that behavior with __ASSERT disabled
217 	 * is to set the bit.
218 	 */
219 	if (false) {
220 		/* preserve set */
221 		rc = gpio_port_set_clr_bits_raw(dev_out, BIT(PIN_OUT), BIT(PIN_OUT));
222 		zassert_equal(rc, 0,
223 			      "s/c dup set failed");
224 		zassert_equal(raw_in(), true,
225 			      "s/c dup set mismatch");
226 
227 		/* do set */
228 		raw_out(false);
229 		rc = gpio_port_set_clr_bits_raw(dev_out, BIT(PIN_OUT), BIT(PIN_OUT));
230 		zassert_equal(rc, 0,
231 			      "s/c dup2 set failed");
232 		zassert_equal(raw_in(), true,
233 			      "s/c dup2 set mismatch");
234 	}
235 
236 	rc = gpio_port_toggle_bits(dev_out, BIT(PIN_OUT));
237 	zassert_equal(rc, 0,
238 		      "toggle_bits high-to-low failed");
239 	zassert_equal(raw_in(), false,
240 		      "toggle_bits high-to-low mismatch");
241 
242 	rc = gpio_port_toggle_bits(dev_out, BIT(PIN_OUT));
243 	zassert_equal(rc, 0,
244 		      "toggle_bits low-to-high failed");
245 	zassert_equal(raw_in(), true,
246 		      "toggle_bits low-to-high mismatch");
247 
248 	return TC_PASS;
249 }
250 
251 /* gpio_pin_get_raw()
252  * gpio_pin_set_raw()
253  */
pin_physical(void)254 static int pin_physical(void)
255 {
256 	int rc;
257 
258 	TC_PRINT("- %s\n", __func__);
259 
260 	raw_out(true);
261 	zassert_equal(gpio_pin_get_raw(dev_in, PIN_IN), raw_in(), "pin_get_raw high failed");
262 
263 	raw_out(false);
264 	zassert_equal(gpio_pin_get_raw(dev_in, PIN_IN), raw_in(), "pin_get_raw low failed");
265 
266 	rc = gpio_pin_set_raw(dev_out, PIN_OUT, 32);
267 	zassert_equal(rc, 0, "pin_set_raw high failed");
268 	zassert_equal(raw_in(), true, "pin_set_raw high failed");
269 
270 	rc = gpio_pin_set_raw(dev_out, PIN_OUT, 0);
271 	zassert_equal(rc, 0,
272 		      "pin_set_raw low failed");
273 	zassert_equal(raw_in(), false,
274 		      "pin_set_raw low failed");
275 
276 	rc = gpio_pin_toggle(dev_out, PIN_OUT);
277 	zassert_equal(rc, 0,
278 		      "pin_toggle low-to-high failed");
279 	zassert_equal(raw_in(), true,
280 		      "pin_toggle low-to-high mismatch");
281 
282 	rc = gpio_pin_toggle(dev_out, PIN_OUT);
283 	zassert_equal(rc, 0,
284 		      "pin_toggle high-to-low failed");
285 	zassert_equal(raw_in(), false,
286 		      "pin_toggle high-to-low mismatch");
287 
288 	return TC_PASS;
289 }
290 
291 /* Verify configure output level is independent of active level, and
292  * raw output is independent of active level.
293  */
check_raw_output_levels(void)294 static int check_raw_output_levels(void)
295 {
296 	int rc;
297 
298 	TC_PRINT("- %s\n", __func__);
299 
300 	rc = gpio_pin_configure(dev_out, PIN_OUT,
301 				GPIO_ACTIVE_HIGH | GPIO_OUTPUT_LOW | PIN_OUT_FLAGS);
302 	zassert_equal(rc, 0,
303 		      "active high output low failed");
304 	zassert_equal(raw_in(), false,
305 		      "active high output low raw mismatch");
306 	raw_out(true);
307 	zassert_equal(raw_in(), true,
308 		      "set high mismatch");
309 
310 	rc = gpio_pin_configure(dev_out, PIN_OUT,
311 				GPIO_ACTIVE_HIGH | GPIO_OUTPUT_HIGH | PIN_OUT_FLAGS);
312 	zassert_equal(rc, 0,
313 		      "active high output high failed");
314 	zassert_equal(raw_in(), true,
315 		      "active high output high raw mismatch");
316 	raw_out(false);
317 	zassert_equal(raw_in(), false,
318 		      "set low mismatch");
319 
320 	rc = gpio_pin_configure(dev_out, PIN_OUT,
321 				GPIO_ACTIVE_LOW | GPIO_OUTPUT_LOW | PIN_OUT_FLAGS);
322 	zassert_equal(rc, 0,
323 		      "active low output low failed");
324 	zassert_equal(raw_in(), false,
325 		      "active low output low raw mismatch");
326 	raw_out(true);
327 	zassert_equal(raw_in(), true,
328 		      "set high mismatch");
329 
330 	rc = gpio_pin_configure(dev_out, PIN_OUT,
331 				GPIO_ACTIVE_LOW | GPIO_OUTPUT_HIGH | PIN_OUT_FLAGS);
332 	zassert_equal(rc, 0,
333 		      "active low output high failed");
334 	zassert_equal(raw_in(), true,
335 		      "active low output high raw mismatch");
336 	raw_out(false);
337 	zassert_equal(raw_in(), false,
338 		      "set low mismatch");
339 
340 	return TC_PASS;
341 }
342 
343 /* Verify configure output level is dependent on active level, and
344  * logic output is dependent on active level.
345  */
check_logic_output_levels(void)346 static int check_logic_output_levels(void)
347 {
348 	int rc;
349 
350 	TC_PRINT("- %s\n", __func__);
351 
352 	rc = gpio_pin_configure(dev_out, PIN_OUT,
353 				GPIO_ACTIVE_HIGH | GPIO_OUTPUT_INACTIVE | PIN_OUT_FLAGS);
354 	zassert_equal(rc, 0,
355 		      "active true output false failed: %d", rc);
356 	zassert_equal(raw_in(), false,
357 		      "active true output false logic mismatch");
358 	logic_out(true);
359 	zassert_equal(raw_in(), true,
360 		      "set true mismatch");
361 
362 	rc = gpio_pin_configure(dev_out, PIN_OUT,
363 				GPIO_ACTIVE_HIGH | GPIO_OUTPUT_ACTIVE | PIN_OUT_FLAGS);
364 	zassert_equal(rc, 0,
365 		      "active true output true failed");
366 	zassert_equal(raw_in(), true,
367 		      "active true output true logic mismatch");
368 	logic_out(false);
369 	zassert_equal(raw_in(), false,
370 		      "set false mismatch");
371 
372 	rc = gpio_pin_configure(dev_out, PIN_OUT,
373 				GPIO_ACTIVE_LOW | GPIO_OUTPUT_ACTIVE | PIN_OUT_FLAGS);
374 	zassert_equal(rc, 0,
375 		      "active low output true failed");
376 
377 	zassert_equal(raw_in(), false,
378 		      "active low output true raw mismatch");
379 	logic_out(false);
380 	zassert_equal(raw_in(), true,
381 		      "set false mismatch");
382 
383 	rc = gpio_pin_configure(dev_out, PIN_OUT,
384 				GPIO_ACTIVE_LOW | GPIO_OUTPUT_INACTIVE | PIN_OUT_FLAGS);
385 	zassert_equal(rc, 0,
386 		      "active low output false failed");
387 	zassert_equal(raw_in(), true,
388 		      "active low output false logic mismatch");
389 	logic_out(true);
390 	zassert_equal(raw_in(), false,
391 		      "set true mismatch");
392 
393 	return TC_PASS;
394 }
395 
396 /* Verify active-high input matches physical level, and active-low
397  * input inverts physical level.
398  */
check_input_levels(void)399 static int check_input_levels(void)
400 {
401 	int rc;
402 
403 	TC_PRINT("- %s\n", __func__);
404 
405 	rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_OUTPUT | PIN_OUT_FLAGS);
406 	zassert_equal(rc, 0,
407 		      "output configure failed");
408 
409 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT);
410 	zassert_equal(rc, 0,
411 		      "active high failed");
412 	raw_out(true);
413 	zassert_equal(raw_in(), true,
414 		      "raw high mismatch");
415 	zassert_equal(logic_in(), true,
416 		      "logic high mismatch");
417 
418 	raw_out(false);
419 	zassert_equal(raw_in(), false,
420 		      "raw low mismatch");
421 	zassert_equal(logic_in(), false,
422 		      "logic low mismatch");
423 
424 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT | GPIO_ACTIVE_LOW);
425 	zassert_equal(rc, 0,
426 		      "active low failed");
427 
428 	raw_out(true);
429 	zassert_equal(raw_in(), true,
430 		      "raw high mismatch");
431 	zassert_equal(logic_in(), false,
432 		      "logic inactive mismatch");
433 
434 	raw_out(false);
435 	zassert_equal(raw_in(), false,
436 		      "raw low mismatch");
437 	zassert_equal(logic_in(), true,
438 		      "logic active mismatch");
439 
440 	return TC_PASS;
441 }
442 
443 /* Delay after pull input config to allow signal to settle.  The value
444  * selected is conservative (higher than may be necessary).
445  */
446 #define PULL_DELAY_US 100U
447 
448 /* Verify that pull-up and pull-down work for a disconnected input. */
check_pulls(void)449 static int check_pulls(void)
450 {
451 	int rc;
452 
453 	TC_PRINT("- %s\n", __func__);
454 
455 	/* Disconnect output */
456 	rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_DISCONNECTED);
457 	if (rc == -ENOTSUP) {
458 		TC_PRINT("NOTE: cannot configure pin as disconnected; trying as input\n");
459 		rc = gpio_pin_configure(dev_out, PIN_OUT, GPIO_INPUT);
460 	}
461 	zassert_equal(rc, 0,
462 		      "output disconnect failed");
463 
464 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT | GPIO_PULL_UP);
465 	k_busy_wait(PULL_DELAY_US);
466 	if (rc == -ENOTSUP) {
467 		TC_PRINT("pull-up not supported\n");
468 		return TC_PASS;
469 	} else if (rc != 0) {
470 		TC_ERROR("input pull-up fail: %d\n", rc);
471 		return TC_FAIL;
472 	}
473 	zassert_equal(raw_in(), true,
474 		      "physical pull-up does not read high");
475 
476 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT | GPIO_PULL_DOWN);
477 	k_busy_wait(PULL_DELAY_US);
478 	if (rc == -ENOTSUP) {
479 		TC_PRINT("pull-down not supported\n");
480 		return TC_PASS;
481 	} else if (rc != 0) {
482 		TC_ERROR("input pull-down fail: %d\n", rc);
483 		return TC_FAIL;
484 	}
485 	zassert_equal(raw_in(), false,
486 		      "physical pull-down does not read low");
487 
488 	/* Test that pull is not affected by active level */
489 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT | GPIO_ACTIVE_LOW | GPIO_PULL_UP);
490 	k_busy_wait(PULL_DELAY_US);
491 	if (rc == -ENOTSUP) {
492 		TC_PRINT("pull-up not supported\n");
493 		return TC_PASS;
494 	} else if (rc != 0) {
495 		TC_ERROR("input pull-up fail: %d\n", rc);
496 		return TC_FAIL;
497 	}
498 	zassert_equal(raw_in(), true,
499 		      "logical pull-up does not read high");
500 	zassert_equal(logic_in(), false,
501 		      "logical pull-up reads true");
502 
503 	rc = gpio_pin_configure(dev_in, PIN_IN, GPIO_INPUT | GPIO_ACTIVE_LOW | GPIO_PULL_DOWN);
504 	k_busy_wait(PULL_DELAY_US);
505 	if (rc == -ENOTSUP) {
506 		TC_PRINT("pull-up not supported\n");
507 		return TC_PASS;
508 	} else if (rc != 0) {
509 		TC_ERROR("input pull-up fail: %d\n", rc);
510 		return TC_FAIL;
511 	}
512 	zassert_equal(raw_in(), false,
513 		      "logical pull-down does not read low");
514 	zassert_equal(logic_in(), true,
515 		      "logical pull-down reads false");
516 
517 	return TC_PASS;
518 }
519 
520 /* gpio_port_set_bits()
521  * gpio_port_clear_bits()
522  * gpio_port_set_masked()
523  * gpio_port_toggle_bits()
524  */
bits_logical(void)525 static int bits_logical(void)
526 {
527 	int rc;
528 
529 	TC_PRINT("- %s\n", __func__);
530 
531 	rc = gpio_pin_configure(dev_out, PIN_OUT,
532 				GPIO_OUTPUT_HIGH | GPIO_ACTIVE_LOW | PIN_OUT_FLAGS);
533 	zassert_equal(rc, 0,
534 		      "output configure failed");
535 	zassert_equal(raw_in(), true,
536 		      "raw out high mismatch");
537 	zassert_equal(logic_in(), !raw_in(),
538 		      "logic in active mismatch");
539 
540 	/* port_set_bits */
541 	rc = gpio_port_set_bits(dev_out, BIT(PIN_OUT));
542 	zassert_equal(rc, 0,
543 		      "port set raw out failed");
544 	zassert_equal(raw_in(), false,
545 		      "raw low set mismatch");
546 	zassert_equal(logic_in(), !raw_in(),
547 		      "logic in inactive mismatch");
548 
549 	/* port_clear_bits */
550 	rc = gpio_port_clear_bits(dev_out, BIT(PIN_OUT));
551 	zassert_equal(rc, 0,
552 		      "port clear raw out failed");
553 	zassert_equal(logic_in(), false,
554 		      "low clear mismatch");
555 
556 	/* set after clear changes */
557 	rc = gpio_port_set_bits_raw(dev_out, BIT(PIN_OUT));
558 	zassert_equal(rc, 0,
559 		      "port set raw out failed");
560 	zassert_equal(logic_in(), false,
561 		      "raw set mismatch");
562 
563 	/* pin_set false */
564 	rc = gpio_pin_set(dev_out, PIN_OUT, 0);
565 	zassert_equal(rc, 0,
566 		      "pin clear failed");
567 	zassert_equal(logic_in(), false,
568 		      "pin clear mismatch");
569 
570 	/* pin_set true */
571 	rc = gpio_pin_set(dev_out, PIN_OUT, 32);
572 	zassert_equal(rc, 0,
573 		      "pin set failed");
574 	zassert_equal(logic_in(), true,
575 		      "pin set mismatch");
576 
577 	rc = gpio_port_set_masked(dev_out, BIT(PIN_OUT), 0);
578 	zassert_equal(rc, 0,
579 		      "set_masked low failed");
580 	zassert_equal(logic_in(), false,
581 		      "set_masked low mismatch");
582 
583 	rc = gpio_port_set_masked(dev_out, BIT(PIN_OUT), ALL_BITS);
584 	zassert_equal(rc, 0,
585 		      "set_masked high failed");
586 	zassert_equal(logic_in(), true,
587 		      "set_masked high mismatch");
588 
589 	rc = gpio_port_set_clr_bits(dev_out, BIT(PIN_IN), BIT(PIN_OUT));
590 	zassert_equal(rc, 0,
591 		      "set in clear out failed");
592 	zassert_equal(logic_in(), false,
593 		      "set in clear out mismatch");
594 
595 	rc = gpio_port_set_clr_bits(dev_out, BIT(PIN_OUT), BIT(PIN_IN));
596 	zassert_equal(rc, 0,
597 		      "set out clear in failed");
598 	zassert_equal(logic_in(), true,
599 		      "set out clear in mismatch");
600 
601 	/* Conditionally verify that behavior with __ASSERT disabled
602 	 * is to set the bit.
603 	 */
604 	if (false) {
605 		/* preserve set */
606 		rc = gpio_port_set_clr_bits(dev_out, BIT(PIN_OUT), BIT(PIN_OUT));
607 		zassert_equal(rc, 0,
608 			      "s/c set toggle failed");
609 		zassert_equal(logic_in(), false,
610 			      "s/c set toggle mismatch");
611 
612 		/* force set */
613 		raw_out(true);
614 		rc = gpio_port_set_clr_bits(dev_out, BIT(PIN_OUT), BIT(PIN_OUT));
615 		zassert_equal(rc, 0,
616 			      "s/c dup set failed");
617 		zassert_equal(logic_in(), false,
618 			      "s/c dup set mismatch");
619 	}
620 
621 	rc = gpio_port_toggle_bits(dev_out, BIT(PIN_OUT));
622 	zassert_equal(rc, 0,
623 		      "toggle_bits active-to-inactive failed");
624 	zassert_equal(logic_in(), false,
625 		      "toggle_bits active-to-inactive mismatch");
626 
627 	rc = gpio_port_toggle_bits(dev_out, BIT(PIN_OUT));
628 	zassert_equal(rc, 0,
629 		      "toggle_bits inactive-to-active failed");
630 	zassert_equal(logic_in(), true,
631 		      "toggle_bits inactive-to-active mismatch");
632 
633 	rc = gpio_pin_toggle(dev_out, PIN_OUT);
634 	zassert_equal(rc, 0,
635 		      "pin_toggle low-to-high failed");
636 	zassert_equal(logic_in(), false,
637 		      "pin_toggle low-to-high mismatch");
638 
639 	return TC_PASS;
640 }
641 
642 /* gpio_pin_get_config()
643  */
pin_get_config(void)644 static int pin_get_config(void)
645 {
646 	gpio_flags_t flags_get = 0;
647 	gpio_flags_t flags_set;
648 	int rc;
649 
650 	flags_set = GPIO_OUTPUT_HIGH;
651 	rc = gpio_pin_configure(dev_out, PIN_OUT, flags_set);
652 	zassert_equal(rc, 0, "pin configure failed");
653 
654 	rc = gpio_pin_get_config(dev_out, PIN_OUT, &flags_get);
655 	if (rc == -ENOSYS) {
656 		return TC_PASS;
657 	}
658 
659 	zassert_equal(rc, 0, "pin get config failed");
660 	zassert_equal(flags_get, flags_set, "flags are different");
661 
662 	return TC_PASS;
663 }
664 
ZTEST(gpio_port,test_gpio_port)665 ZTEST(gpio_port, test_gpio_port)
666 {
667 	zassert_equal(setup(), TC_PASS,
668 		      "device setup failed");
669 	zassert_equal(bits_physical(), TC_PASS,
670 		      "bits_physical failed");
671 	zassert_equal(pin_physical(), TC_PASS,
672 		      "pin_physical failed");
673 	zassert_equal(check_raw_output_levels(), TC_PASS,
674 		      "check_raw_output_levels failed");
675 	zassert_equal(check_logic_output_levels(), TC_PASS,
676 		      "check_logic_output_levels failed");
677 	zassert_equal(check_input_levels(), TC_PASS,
678 		      "check_input_levels failed");
679 	zassert_equal(bits_logical(), TC_PASS,
680 		      "bits_logical failed");
681 	if (!IS_ENABLED(CONFIG_SKIP_PULL_TEST)) {
682 		zassert_equal(check_pulls(), TC_PASS,
683 				"check_pulls failed");
684 	}
685 	if (IS_ENABLED(CONFIG_GPIO_GET_CONFIG)) {
686 		zassert_equal(pin_get_config(), TC_PASS,
687 			      "pin_get_config failed");
688 	}
689 }
690