xref: /optee_os/core/pta/tests/dt_driver_test.c (revision ba2a6adb764f1310ad3c3091d89de84274f86b02)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2022, Linaro Limited
4  *
5  * Tests introduce dummy test drivers and assiciated devices defined in
6  * dt_driver_test.dtsi file with device resource dependencies.
7  */
8 
9 #include <assert.h>
10 #include <config.h>
11 #include <crypto/crypto.h>
12 #include <drivers/clk.h>
13 #include <drivers/clk_dt.h>
14 #include <drivers/gpio.h>
15 #include <drivers/rstctrl.h>
16 #include <initcall.h>
17 #include <kernel/dt_driver.h>
18 #include <libfdt.h>
19 #include <malloc.h>
20 #include <sys/queue.h>
21 #include <tee_api_defines_extensions.h>
22 #include <tee_api_types.h>
23 
24 #define DT_TEST_MSG(...)	FMSG("(dt-driver-test) " __VA_ARGS__)
25 
26 /* Test state IDs */
27 enum dt_test_sid { DEFAULT = 0, IN_PROGRESS, SUCCESS, FAILED };
28 
29 /*
30  * DT tests state to be reported from PTA_INVOKE_TESTS_CMD_DT_TEST_STATUS
31  * possibly printed to console. A test can be skipped (DEFAULT) or be
32  * successful (SUCCESS) orthewise it has failed (IN_PROGRESS, FAILED).
33  */
34 struct dt_test_state {
35 	enum dt_test_sid probe_deferral;
36 	enum dt_test_sid probe_clocks;
37 	enum dt_test_sid probe_gpios;
38 	enum dt_test_sid probe_resets;
39 	enum dt_test_sid crypto_dependencies;
40 };
41 
42 /*
43  * References allocated from heap to be free once test completed
44  * dt_test_alloc(), dt_test_free(), dt_test_free_all()
45  */
46 struct dt_test_free_ref {
47 	void *p;
48 	SLIST_ENTRY(dt_test_free_ref) link;
49 };
50 
51 static struct dt_test_state dt_test_state;
52 
53 static const char __maybe_unused * const dt_test_str_sid[] = {
54 	[DEFAULT] = "not run",
55 	[IN_PROGRESS] = "in-progress",
56 	[SUCCESS] = "successful",
57 	[FAILED] = "failed",
58 };
59 
60 /* Reference allocations during test for release_init_resource initcall level */
61 static SLIST_HEAD(dt_test_free_refs, dt_test_free_ref) dt_test_free_list =
62 	SLIST_HEAD_INITIALIZER(dt_test_free_list);
63 
64 static void __maybe_unused *dt_test_alloc(size_t size)
65 {
66 	struct dt_test_free_ref *ref = NULL;
67 
68 	ref = calloc(1, sizeof(*ref) + size);
69 	if (!ref)
70 		return NULL;
71 
72 	ref->p = ref + 1;
73 	SLIST_INSERT_HEAD(&dt_test_free_list, ref, link);
74 
75 	return ref->p;
76 }
77 
78 static void __maybe_unused dt_test_free(void *p)
79 {
80 	struct dt_test_free_ref *ref = NULL;
81 	struct dt_test_free_ref *t_ref = NULL;
82 
83 	if (!p)
84 		return;
85 
86 	SLIST_FOREACH_SAFE(ref, &dt_test_free_list, link, t_ref) {
87 		if (ref->p == p) {
88 			SLIST_REMOVE(&dt_test_free_list, ref,
89 				     dt_test_free_ref, link);
90 			free(ref);
91 			return;
92 		}
93 	}
94 
95 	panic();
96 }
97 
98 static void dt_test_free_all(void)
99 {
100 	while (!SLIST_EMPTY(&dt_test_free_list)) {
101 		struct dt_test_free_ref *ref = SLIST_FIRST(&dt_test_free_list);
102 
103 		SLIST_REMOVE(&dt_test_free_list, ref, dt_test_free_ref, link);
104 		free(ref);
105 	}
106 }
107 
108 static TEE_Result dt_test_release(void)
109 {
110 	dt_test_free_all();
111 
112 	DT_TEST_MSG("Probe deferral: %s",
113 		    dt_test_str_sid[dt_test_state.probe_deferral]);
114 	DT_TEST_MSG("Clocks probe: %s",
115 		    dt_test_str_sid[dt_test_state.probe_clocks]);
116 	DT_TEST_MSG("GPIO ctrl probe: %s",
117 		    dt_test_str_sid[dt_test_state.probe_gpios]);
118 	DT_TEST_MSG("Reset ctrl probe: %s",
119 		    dt_test_str_sid[dt_test_state.probe_resets]);
120 	DT_TEST_MSG("Crypto deps.: %s",
121 		    dt_test_str_sid[dt_test_state.crypto_dependencies]);
122 
123 	return dt_driver_test_status();
124 }
125 
126 release_init_resource(dt_test_release);
127 
128 TEE_Result dt_driver_test_status(void)
129 {
130 	TEE_Result res = TEE_SUCCESS;
131 
132 	if (dt_test_state.probe_deferral != SUCCESS) {
133 		EMSG("Probe deferral test failed");
134 		res = TEE_ERROR_GENERIC;
135 	}
136 	if (IS_ENABLED(CFG_DRIVERS_CLK) &&
137 	    dt_test_state.probe_clocks != SUCCESS) {
138 		EMSG("Clocks probing test failed");
139 		res = TEE_ERROR_GENERIC;
140 	}
141 	if (IS_ENABLED(CFG_DRIVERS_GPIOS) &&
142 	    dt_test_state.probe_gpios != SUCCESS) {
143 		EMSG("GPIO controllers probing test failed");
144 		res = TEE_ERROR_GENERIC;
145 	}
146 	if (IS_ENABLED(CFG_DRIVERS_RSTCTRL) &&
147 	    dt_test_state.probe_resets != SUCCESS) {
148 		EMSG("Reset controllers probing test failed");
149 		res = TEE_ERROR_GENERIC;
150 	}
151 	if (dt_test_state.crypto_dependencies != SUCCESS) {
152 		EMSG("Probe deferral on crypto dependencies test failed");
153 		res = TEE_ERROR_GENERIC;
154 	}
155 
156 	return res;
157 }
158 
159 static TEE_Result probe_test_clocks(const void *fdt, int node)
160 {
161 	TEE_Result res = TEE_ERROR_GENERIC;
162 	struct clk *clk0 = NULL;
163 	struct clk *clk1 = NULL;
164 	struct clk *clk = NULL;
165 
166 	DT_TEST_MSG("Probe clocks");
167 	dt_test_state.probe_clocks = IN_PROGRESS;
168 
169 	res = clk_dt_get_by_index(fdt, node, 0, &clk0);
170 	if (res)
171 		goto err;
172 
173 	res = clk_dt_get_by_index(fdt, node, 1, &clk1);
174 	if (res)
175 		goto err;
176 
177 	DT_TEST_MSG("Check valid clock references");
178 
179 	if (clk_enable(clk0)) {
180 		DT_TEST_MSG("Can't enable %s", clk_get_name(clk0));
181 		res = TEE_ERROR_GENERIC;
182 		goto err;
183 	}
184 	clk_disable(clk0);
185 
186 	res = clk_dt_get_by_name(fdt, node, "clk0", &clk);
187 	if (res || clk != clk0) {
188 		DT_TEST_MSG("Unexpected clock reference");
189 		res = TEE_ERROR_GENERIC;
190 		goto err;
191 	}
192 
193 	res = clk_dt_get_by_name(fdt, node, "clk1", &clk);
194 	if (res || clk != clk1) {
195 		DT_TEST_MSG("Unexpected clock reference");
196 		res = TEE_ERROR_GENERIC;
197 		goto err;
198 	}
199 
200 	DT_TEST_MSG("Bad clock reference");
201 
202 	res = clk_dt_get_by_index(fdt, node, 3, &clk);
203 	if (!res) {
204 		DT_TEST_MSG("Unexpected clock found on invalid index");
205 		res = TEE_ERROR_GENERIC;
206 		goto err;
207 	}
208 
209 	res = clk_dt_get_by_name(fdt, node, "clk2", &clk);
210 	if (!res) {
211 		DT_TEST_MSG("Unexpected clock found on invalid name");
212 		res = TEE_ERROR_GENERIC;
213 		goto err;
214 	}
215 
216 	dt_test_state.probe_clocks = SUCCESS;
217 	return TEE_SUCCESS;
218 
219 err:
220 	if (res != TEE_ERROR_DEFER_DRIVER_INIT)
221 		dt_test_state.probe_clocks = FAILED;
222 
223 	return res;
224 }
225 
226 static TEE_Result probe_test_resets(const void *fdt, int node)
227 {
228 	TEE_Result res = TEE_ERROR_GENERIC;
229 	struct rstctrl *rstctrl0 = NULL;
230 	struct rstctrl *rstctrl1 = NULL;
231 	struct rstctrl *rstctrl = NULL;
232 
233 	DT_TEST_MSG("Probe reset controllers");
234 	dt_test_state.probe_resets = IN_PROGRESS;
235 
236 	res = rstctrl_dt_get_by_index(fdt, node, 0, &rstctrl0);
237 	if (res)
238 		goto err;
239 
240 	DT_TEST_MSG("Check valid reset controller");
241 
242 	if (rstctrl_assert(rstctrl0)) {
243 		EMSG("Can't assert rstctrl %s", rstctrl_name(rstctrl0));
244 		res = TEE_ERROR_GENERIC;
245 		goto err;
246 	}
247 
248 	res = rstctrl_dt_get_by_name(fdt, node, "rst0", &rstctrl);
249 	if (res)
250 		goto err;
251 
252 	if (rstctrl != rstctrl0) {
253 		EMSG("Unexpected reset controller reference");
254 		res = TEE_ERROR_GENERIC;
255 		goto err;
256 	}
257 
258 	res = rstctrl_dt_get_by_name(fdt, node, "rst1", &rstctrl1);
259 	if (res)
260 		goto err;
261 
262 	if (!rstctrl1 || rstctrl1 == rstctrl0) {
263 		EMSG("Unexpected reset controller reference");
264 		res = TEE_ERROR_GENERIC;
265 		goto err;
266 	}
267 
268 	dt_test_state.probe_resets = SUCCESS;
269 	return TEE_SUCCESS;
270 
271 err:
272 	if (res != TEE_ERROR_DEFER_DRIVER_INIT)
273 		dt_test_state.probe_resets = FAILED;
274 
275 	return res;
276 }
277 
278 static TEE_Result probe_test_gpios(const void *fdt, int node)
279 {
280 	TEE_Result res = TEE_ERROR_GENERIC;
281 	struct gpio *gpio = NULL;
282 
283 	DT_TEST_MSG("Probe GPIO controllers");
284 	dt_test_state.probe_gpios = IN_PROGRESS;
285 
286 	res = gpio_dt_get_by_index(fdt, node, 0, "test", &gpio);
287 	if (res)
288 		goto err;
289 
290 	if (gpio_get_direction(gpio) != GPIO_DIR_IN) {
291 		EMSG("Unexpected gpio_get_direction() return value");
292 		res = TEE_ERROR_GENERIC;
293 		goto err;
294 	}
295 
296 	/* GPIO is declared as ACTIVE_LOW in device-tree */
297 	if (gpio_get_value(gpio) != GPIO_LEVEL_LOW) {
298 		EMSG("Unexpected gpio_get_value() return value");
299 		res = TEE_ERROR_GENERIC;
300 		goto err;
301 	}
302 
303 	res = gpio_dt_get_by_index(fdt, node, 1, "test", &gpio);
304 	if (res)
305 		goto err;
306 
307 	if (gpio_get_direction(gpio) != GPIO_DIR_IN) {
308 		EMSG("Unexpected gpio_get_direction() return value");
309 		res = TEE_ERROR_GENERIC;
310 		goto err;
311 	}
312 
313 	if (gpio_get_value(gpio) != GPIO_LEVEL_HIGH) {
314 		EMSG("Unexpected gpio_get_value() return value");
315 		res = TEE_ERROR_GENERIC;
316 		goto err;
317 	}
318 
319 	dt_test_state.probe_gpios = SUCCESS;
320 	return TEE_SUCCESS;
321 
322 err:
323 	if (res != TEE_ERROR_DEFER_DRIVER_INIT)
324 		dt_test_state.probe_gpios = FAILED;
325 
326 	return res;
327 }
328 
329 /*
330  * Consumer test driver: instance probed from the compatible
331  * node parsed in the DT. It consumes emulated resource obtained
332  * from DT references. Probe shall succeed only once all resources
333  * are found.
334  */
335 static TEE_Result dt_test_consumer_probe(const void *fdt, int node,
336 					 const void *compat_data __unused)
337 {
338 	TEE_Result res = TEE_ERROR_GENERIC;
339 
340 	if (IS_ENABLED(CFG_DRIVERS_CLK)) {
341 		res = probe_test_clocks(fdt, node);
342 		if (res)
343 			goto err_probe;
344 	}
345 
346 	if (IS_ENABLED(CFG_DRIVERS_RSTCTRL)) {
347 		res = probe_test_resets(fdt, node);
348 		if (res)
349 			goto err_probe;
350 	}
351 
352 	if (IS_ENABLED(CFG_DRIVERS_GPIO)) {
353 		res = probe_test_gpios(fdt, node);
354 		if (res)
355 			goto err_probe;
356 	}
357 
358 	if (dt_test_state.probe_deferral != IN_PROGRESS) {
359 		dt_test_state.probe_deferral = FAILED;
360 		return TEE_ERROR_GENERIC;
361 	}
362 
363 	dt_test_state.probe_deferral = SUCCESS;
364 
365 	return TEE_SUCCESS;
366 
367 err_probe:
368 	assert(res);
369 
370 	if (res == TEE_ERROR_DEFER_DRIVER_INIT &&
371 	    dt_test_state.probe_deferral == DEFAULT) {
372 		/* We expect at least a probe deferral */
373 		dt_test_state.probe_deferral = IN_PROGRESS;
374 	}
375 
376 	return res;
377 }
378 
379 static const struct dt_device_match dt_test_consumer_match_table[] = {
380 	{ .compatible = "linaro,dt-test-consumer", },
381 	{ }
382 };
383 
384 DEFINE_DT_DRIVER(dt_test_consumer_driver) = {
385 	.name = "dt-test-consumer",
386 	.match_table = dt_test_consumer_match_table,
387 	.probe = dt_test_consumer_probe,
388 };
389 
390 static TEE_Result dt_test_crypt_consumer_probe(const void *fdt __unused,
391 					       int node __unused,
392 					       const void *compat_data __unused)
393 {
394 	TEE_Result res = dt_driver_get_crypto();
395 	uint8_t __maybe_unused byte = 0;
396 
397 	if (res == TEE_ERROR_DEFER_DRIVER_INIT &&
398 	    dt_test_state.crypto_dependencies == DEFAULT) {
399 		/* We expect to be deferred */
400 		dt_test_state.crypto_dependencies = IN_PROGRESS;
401 	}
402 
403 	if (res)
404 		return res;
405 
406 	if (dt_test_state.crypto_dependencies == DEFAULT) {
407 		EMSG("Test expects at least a driver probe deferral");
408 		dt_test_state.crypto_dependencies = FAILED;
409 		return TEE_ERROR_GENERIC;
410 	}
411 
412 	if (crypto_rng_read(&byte, sizeof(byte))) {
413 		dt_test_state.crypto_dependencies = FAILED;
414 		return TEE_ERROR_GENERIC;
415 	}
416 
417 	dt_test_state.crypto_dependencies = SUCCESS;
418 	return TEE_SUCCESS;
419 }
420 
421 static const struct dt_device_match dt_test_crypt_consumer_match_table[] = {
422 	{ .compatible = "linaro,dt-test-crypt-consumer", },
423 	{ }
424 };
425 
426 DEFINE_DT_DRIVER(dt_test_consumer_driver) = {
427 	.name = "dt-test-crypt-consumer",
428 	.match_table = dt_test_crypt_consumer_match_table,
429 	.probe = dt_test_crypt_consumer_probe,
430 };
431 
432 #ifdef CFG_DRIVERS_CLK
433 #define DT_TEST_CLK_COUNT		2
434 
435 #define DT_TEST_CLK0_BINDING_ID		3
436 #define DT_TEST_CLK1_BINDING_ID		7
437 
438 static const char *dt_test_clk_name[DT_TEST_CLK_COUNT] = {
439 	"dt_test-clk3",
440 	"dt_test-clk7",
441 };
442 
443 /* Emulating a clock does not require operators */
444 static const struct clk_ops dt_test_clock_provider_ops;
445 
446 static TEE_Result dt_test_get_clk(struct dt_pargs *args, void *data,
447 				  struct clk **out_device)
448 {
449 	struct clk *clk_ref = data;
450 	struct clk *clk = NULL;
451 
452 	if (args->args_count != 1)
453 		return TEE_ERROR_BAD_PARAMETERS;
454 
455 	switch (args->args[0]) {
456 	case DT_TEST_CLK0_BINDING_ID:
457 		clk = clk_ref;
458 		break;
459 	case DT_TEST_CLK1_BINDING_ID:
460 		clk = clk_ref + 1;
461 		break;
462 	default:
463 		EMSG("Unexpected binding ID %"PRIu32, args->args[0]);
464 		return TEE_ERROR_BAD_PARAMETERS;
465 	}
466 
467 	DT_TEST_MSG("Providing clock %s", clk_get_name(clk));
468 
469 	*out_device = clk;
470 	return TEE_SUCCESS;
471 }
472 
473 static TEE_Result dt_test_clock_provider_probe(const void *fdt, int node,
474 					       const void *compat_data __unused)
475 {
476 	TEE_Result res = TEE_ERROR_GENERIC;
477 	struct clk *clk = NULL;
478 	size_t n = 0;
479 
480 	DT_TEST_MSG("Register clocks");
481 
482 	clk = dt_test_alloc(DT_TEST_CLK_COUNT * sizeof(*clk));
483 	if (!clk)
484 		return TEE_ERROR_OUT_OF_MEMORY;
485 
486 	for (n = 0; n < DT_TEST_CLK_COUNT; n++) {
487 		clk[n].ops = &dt_test_clock_provider_ops;
488 		clk[n].name = dt_test_clk_name[n];
489 
490 		res = clk_register(clk + n);
491 		if (res)
492 			goto err;
493 	}
494 
495 	res = clk_dt_register_clk_provider(fdt, node, dt_test_get_clk, clk);
496 	if (res)
497 		goto err;
498 
499 	return TEE_SUCCESS;
500 
501 err:
502 	dt_test_free(clk);
503 	return res;
504 }
505 
506 CLK_DT_DECLARE(dt_test_clock_provider, "linaro,dt-test-provider",
507 	       dt_test_clock_provider_probe);
508 #endif /* CFG_DRIVERS_CLK */
509 
510 #ifdef CFG_DRIVERS_RSTCTRL
511 #define DT_TEST_RSTCTRL_COUNT		2
512 
513 #define DT_TEST_RSTCTRL0_BINDING_ID	5
514 #define DT_TEST_RSTCTRL1_BINDING_ID	35
515 
516 struct dt_test_rstctrl {
517 	unsigned int dt_binding;
518 	struct rstctrl rstctrl;
519 };
520 
521 static struct dt_test_rstctrl *to_test_rstctrl(struct rstctrl *rstctrl)
522 {
523 	return container_of(rstctrl, struct dt_test_rstctrl, rstctrl);
524 }
525 
526 static TEE_Result dt_test_rstctrl_stub(struct rstctrl *rstctrl __maybe_unused,
527 				       unsigned int to_us __unused)
528 {
529 	struct dt_test_rstctrl *dev = to_test_rstctrl(rstctrl);
530 
531 	switch (dev->dt_binding) {
532 	case DT_TEST_RSTCTRL0_BINDING_ID:
533 	case DT_TEST_RSTCTRL1_BINDING_ID:
534 		return TEE_SUCCESS;
535 	default:
536 		EMSG("Unexpected rstctrl reference");
537 		return TEE_ERROR_GENERIC;
538 	}
539 }
540 
541 static const char *dt_test_rstctrl_name(struct rstctrl *rstctrl __maybe_unused)
542 {
543 	static const char *rstctrl_name[DT_TEST_RSTCTRL_COUNT] = {
544 		"dt_test-rstctrl5",
545 		"dt_test-rstctrl35",
546 	};
547 	struct dt_test_rstctrl *dev = to_test_rstctrl(rstctrl);
548 
549 	switch (dev->dt_binding) {
550 	case DT_TEST_RSTCTRL0_BINDING_ID:
551 		return rstctrl_name[0];
552 	case DT_TEST_RSTCTRL1_BINDING_ID:
553 		return rstctrl_name[1];
554 	default:
555 		EMSG("Unexpected rstctrl reference");
556 		return NULL;
557 	}
558 }
559 
560 const struct rstctrl_ops dt_test_rstctrl_ops = {
561 	.assert_level = dt_test_rstctrl_stub,
562 	.deassert_level = dt_test_rstctrl_stub,
563 	.get_name = dt_test_rstctrl_name,
564 };
565 
566 static TEE_Result dt_test_get_rstctrl(struct dt_pargs *args, void *data,
567 				      struct rstctrl **out_device)
568 {
569 	struct dt_test_rstctrl *ref = data;
570 	struct rstctrl *rstctrl = NULL;
571 
572 	if (args->args_count != 1)
573 		return TEE_ERROR_BAD_PARAMETERS;
574 
575 	switch (args->args[0]) {
576 	case DT_TEST_RSTCTRL0_BINDING_ID:
577 		rstctrl = &ref[0].rstctrl;
578 		break;
579 	case DT_TEST_RSTCTRL1_BINDING_ID:
580 		rstctrl = &ref[1].rstctrl;
581 		break;
582 	default:
583 		EMSG("Unexpected binding ID %"PRIu32, args->args[0]);
584 		return TEE_ERROR_BAD_PARAMETERS;
585 	}
586 
587 	DT_TEST_MSG("Providing reset controller %s", rstctrl_name(rstctrl));
588 
589 	*out_device = rstctrl;
590 
591 	return TEE_SUCCESS;
592 }
593 
594 static TEE_Result dt_test_rstctrl_provider_probe(const void *fdt, int offs,
595 						 const void *data __unused)
596 {
597 	TEE_Result res = TEE_ERROR_GENERIC;
598 	struct dt_test_rstctrl *devices = NULL;
599 
600 	DT_TEST_MSG("Register reset controllers");
601 
602 	assert(rstctrl_ops_is_valid(&dt_test_rstctrl_ops));
603 
604 	devices = dt_test_alloc(DT_TEST_RSTCTRL_COUNT * sizeof(*devices));
605 	if (!devices)
606 		return TEE_ERROR_OUT_OF_MEMORY;
607 
608 	devices[0].rstctrl.ops = &dt_test_rstctrl_ops;
609 	devices[0].dt_binding = DT_TEST_RSTCTRL0_BINDING_ID;
610 
611 	devices[1].rstctrl.ops = &dt_test_rstctrl_ops;
612 	devices[1].dt_binding = DT_TEST_RSTCTRL1_BINDING_ID;
613 
614 	res = rstctrl_register_provider(fdt, offs, dt_test_get_rstctrl,
615 					devices);
616 	if (res) {
617 		dt_test_free(devices);
618 		return res;
619 	}
620 
621 	return TEE_SUCCESS;
622 }
623 
624 RSTCTRL_DT_DECLARE(dt_test_rstctrl_provider, "linaro,dt-test-provider",
625 		   dt_test_rstctrl_provider_probe);
626 #endif /* CFG_DRIVERS_RSTCTRL */
627 
628 #ifdef CFG_DRIVERS_GPIO
629 #define DT_TEST_GPIO_COUNT	2
630 
631 #define DT_TEST_GPIO0_PIN	1
632 #define DT_TEST_GPIO0_FLAGS	GPIO_ACTIVE_LOW
633 #define DT_TEST_GPIO1_PIN	2
634 #define DT_TEST_GPIO1_FLAGS	GPIO_PULL_UP
635 
636 struct dt_test_gpio {
637 	unsigned int pin;
638 	unsigned int flags;
639 	struct gpio_chip gpio_chip;
640 };
641 
642 static struct dt_test_gpio *to_test_gpio(struct gpio_chip *chip)
643 {
644 	return container_of(chip, struct dt_test_gpio, gpio_chip);
645 }
646 
647 static enum gpio_dir dt_test_gpio_get_direction(struct gpio_chip *chip,
648 						unsigned int gpio_pin)
649 {
650 	struct dt_test_gpio *dtg = to_test_gpio(chip);
651 
652 	if (dtg->pin != gpio_pin)
653 		panic("Invalid GPIO number");
654 
655 	return GPIO_DIR_IN;
656 }
657 
658 static void dt_test_gpio_set_direction(struct gpio_chip *chip,
659 				       unsigned int gpio_pin,
660 				       enum gpio_dir direction __unused)
661 {
662 	struct dt_test_gpio *dtg = to_test_gpio(chip);
663 
664 	if (dtg->pin != gpio_pin)
665 		panic("Invalid GPIO number");
666 }
667 
668 static enum gpio_level dt_test_gpio_get_value(struct gpio_chip *chip,
669 					      unsigned int gpio_pin)
670 {
671 	struct dt_test_gpio *dtg = to_test_gpio(chip);
672 
673 	if (dtg->pin != gpio_pin)
674 		panic("Invalid GPIO number");
675 
676 	return GPIO_LEVEL_HIGH;
677 }
678 
679 static void dt_test_gpio_set_value(struct gpio_chip *chip,
680 				   unsigned int gpio_pin,
681 				   enum gpio_level value __unused)
682 {
683 	struct dt_test_gpio *dtg = to_test_gpio(chip);
684 
685 	if (dtg->pin != gpio_pin)
686 		panic("Invalid GPIO number");
687 }
688 
689 static const struct gpio_ops dt_test_gpio_ops = {
690 	.get_direction = dt_test_gpio_get_direction,
691 	.set_direction = dt_test_gpio_set_direction,
692 	.get_value = dt_test_gpio_get_value,
693 	.set_value = dt_test_gpio_set_value,
694 };
695 
696 static TEE_Result dt_test_gpio_get_dt(struct dt_pargs *args, void *data,
697 				      struct gpio **out_device)
698 {
699 	TEE_Result res = TEE_ERROR_GENERIC;
700 	struct gpio *gpio = NULL;
701 	struct dt_test_gpio *gpios = (struct dt_test_gpio *)data;
702 
703 	res = gpio_dt_alloc_pin(args, &gpio);
704 	if (res)
705 		return res;
706 
707 	switch (gpio->pin) {
708 	case DT_TEST_GPIO0_PIN:
709 		gpio->chip = &gpios[0].gpio_chip;
710 		if (gpio->dt_flags != gpios[0].flags) {
711 			EMSG("Unexpected dt_flags %#"PRIx32, gpio->dt_flags);
712 			free(gpio);
713 			return TEE_ERROR_GENERIC;
714 		}
715 		break;
716 	case DT_TEST_GPIO1_PIN:
717 		gpio->chip = &gpios[1].gpio_chip;
718 		if (gpio->dt_flags != gpios[1].flags) {
719 			EMSG("Unexpected dt_flags %#"PRIx32, gpio->dt_flags);
720 			free(gpio);
721 			return TEE_ERROR_GENERIC;
722 		}
723 		break;
724 	default:
725 		EMSG("Unexpected pin ID %u", gpio->pin);
726 		free(gpio);
727 		return TEE_ERROR_BAD_PARAMETERS;
728 	};
729 
730 	*out_device = gpio;
731 
732 	return TEE_SUCCESS;
733 }
734 
735 static TEE_Result dt_test_gpio_provider_probe(const void *fdt, int offs,
736 					      const void *data __unused)
737 {
738 	TEE_Result res = TEE_ERROR_GENERIC;
739 	struct dt_test_gpio *gpios = NULL;
740 
741 	DT_TEST_MSG("Register GPIO controllers");
742 
743 	assert(gpio_ops_is_valid(&dt_test_gpio_ops));
744 
745 	gpios = dt_test_alloc(DT_TEST_GPIO_COUNT * sizeof(*gpios));
746 	if (!gpios)
747 		return TEE_ERROR_OUT_OF_MEMORY;
748 
749 	gpios[0].gpio_chip.ops = &dt_test_gpio_ops;
750 	gpios[0].pin = DT_TEST_GPIO0_PIN;
751 	gpios[0].flags = DT_TEST_GPIO0_FLAGS;
752 
753 	gpios[1].gpio_chip.ops = &dt_test_gpio_ops;
754 	gpios[1].pin = DT_TEST_GPIO1_PIN;
755 	gpios[1].flags = DT_TEST_GPIO1_FLAGS;
756 
757 	res = gpio_register_provider(fdt, offs, dt_test_gpio_get_dt, gpios);
758 	if (res) {
759 		dt_test_free(gpios);
760 		return res;
761 	}
762 
763 	return TEE_SUCCESS;
764 }
765 
766 GPIO_DT_DECLARE(dt_test_gpio_provider, "linaro,dt-test-provider",
767 		dt_test_gpio_provider_probe);
768 #endif /* CFG_DRIVERS_GPIO */
769