1 /**
2  * Copyright 2024 NXP
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/kernel.h>
7 #include <zephyr/init.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/logging/log.h>
10 #include <zephyr/drivers/gpio.h>
11 
12 LOG_MODULE_REGISTER(board_control, CONFIG_BOARD_MIMX93_EVK_LOG_LEVEL);
13 
14 #if DT_HAS_COMPAT_STATUS_OKAY(imx93evk_exp_sel) && IS_ENABLED(CONFIG_BOARD_MIMX93_EVK_EXP_SEL_INIT)
15 
16 #define BOARD_EXP_SEL_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(imx93evk_exp_sel)
17 
18 #define BOARD_EXP_SEL_MUX_A (0U)
19 #define BOARD_EXP_SEL_MUX_B (1U)
20 
board_init_exp_sel(void)21 static int board_init_exp_sel(void)
22 {
23 	int rc = 0;
24 	const struct gpio_dt_spec mux = GPIO_DT_SPEC_GET(BOARD_EXP_SEL_NODE, mux_gpios);
25 	uint32_t pin_state = DT_ENUM_IDX(BOARD_EXP_SEL_NODE, mux);
26 
27 	if (!gpio_is_ready_dt(&mux)) {
28 		LOG_ERR("EXP_SEL Pin port is not ready");
29 		return -ENODEV;
30 	}
31 
32 #if defined(CONFIG_CAN)
33 	if (pin_state != BOARD_EXP_SEL_MUX_A) {
34 		LOG_WRN("CAN is enabled, EXP_SEL overrides to A");
35 		pin_state = BOARD_EXP_SEL_MUX_A;
36 	}
37 #endif /* CONFIG_CAN */
38 
39 	rc = gpio_pin_configure_dt(&mux, pin_state);
40 	if (rc) {
41 		LOG_ERR("Write EXP_SEL Pin error %d", rc);
42 		return rc;
43 	}
44 	LOG_INF("EXP_SEL mux %c with priority %d", pin_state ? 'B' : 'A',
45 		CONFIG_BOARD_MIMX93_EVK_EXP_SEL_INIT_PRIO);
46 
47 	return 0;
48 }
49 
50 SYS_INIT(board_init_exp_sel, POST_KERNEL, CONFIG_BOARD_MIMX93_EVK_EXP_SEL_INIT_PRIO);
51 
52 #endif
53 /*
54  * DT_HAS_COMPAT_STATUS_OKAY(imx93evk_exp_sel) && \
55  * IS_ENABLED(CONFIG_BOARD_MIMX93_EVK_EXP_SEL_INIT)
56  */
57