1 /*
2  * Copyright (c) 2022, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SOC_ARM_NXP_IMX_MCIMX6X_PINCTRL_SOC_H_
8 #define ZEPHYR_SOC_ARM_NXP_IMX_MCIMX6X_PINCTRL_SOC_H_
9 
10 #include <zephyr/devicetree.h>
11 #include <zephyr/types.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE(x) ((x) & 0xF)
18 #define IOMUXC_SW_MUX_CTL_PAD_SION(x) (((x) & 0x1) << 4)
19 #define IOMUXC_SELECT_INPUT_DAISY(x) ((x) & 0x7)
20 
21 #define MCUX_IMX_INPUT_SCHMITT_ENABLE_SHIFT 16
22 #define MCUX_IMX_BIAS_PULL_DOWN_SHIFT 14
23 #define MCUX_IMX_BIAS_PULL_UP_SHIFT 14
24 #define MCUX_IMX_BIAS_BUS_HOLD_SHIFT 13
25 #define MCUX_IMX_PULL_ENABLE_SHIFT 12
26 #define MCUX_IMX_DRIVE_OPEN_DRAIN_SHIFT 11
27 #define MCUX_IMX_SPEED_SHIFT 6
28 #define MCUX_IMX_DRIVE_STRENGTH_SHIFT 3
29 #define MCUX_IMX_SLEW_RATE_SHIFT 0
30 #define MCUX_IMX_INPUT_ENABLE_SHIFT 31 /* Shift to a bit not used by IOMUXC_SW_PAD_CTL */
31 #define MCUX_IMX_INPUT_ENABLE(x) ((x >> MCUX_IMX_INPUT_ENABLE_SHIFT) & 0x1)
32 
33 #define Z_PINCTRL_MCUX_IMX_PINCFG_INIT(node_id)							\
34 	((DT_PROP(node_id, input_schmitt_enable) << MCUX_IMX_INPUT_SCHMITT_ENABLE_SHIFT) |	\
35 	IF_ENABLED(DT_PROP(node_id, bias_pull_up), (DT_ENUM_IDX(node_id, bias_pull_up_value)	\
36 		<< MCUX_IMX_BIAS_PULL_UP_SHIFT) |)						\
37 	IF_ENABLED(DT_PROP(node_id, bias_pull_down), (DT_ENUM_IDX(node_id, bias_pull_down_value)\
38 		<< MCUX_IMX_BIAS_PULL_DOWN_SHIFT) |)					\
39 	((DT_PROP(node_id, bias_pull_down) | DT_PROP(node_id, bias_pull_up))		\
40 		<< MCUX_IMX_BIAS_BUS_HOLD_SHIFT) |					\
41 	 ((!DT_PROP(node_id, bias_disable)) << MCUX_IMX_PULL_ENABLE_SHIFT) |		\
42 	 (DT_PROP(node_id, drive_open_drain) << MCUX_IMX_DRIVE_OPEN_DRAIN_SHIFT) |	\
43 	 (DT_ENUM_IDX(node_id, nxp_speed) << MCUX_IMX_SPEED_SHIFT) |			\
44 	 (DT_ENUM_IDX(node_id, drive_strength) << MCUX_IMX_DRIVE_STRENGTH_SHIFT) |	\
45 	 (DT_ENUM_IDX(node_id, slew_rate) << MCUX_IMX_SLEW_RATE_SHIFT) |		\
46 	 (DT_PROP(node_id, input_enable) << MCUX_IMX_INPUT_ENABLE_SHIFT))
47 
48 
49 /* This struct must be present. It is used by the mcux gpio driver */
50 struct pinctrl_soc_pinmux {
51 	uint32_t mux_register; /*!< IOMUXC SW_PAD_MUX register */
52 	uint32_t config_register; /*!< IOMUXC SW_PAD_CTL register */
53 	uint32_t input_register; /*!< IOMUXC SELECT_INPUT DAISY register */
54 	uint8_t mux_mode: 4; /*!< Mux value for SW_PAD_MUX register */
55 	uint32_t input_daisy:4; /*!< Mux value for SELECT_INPUT_DAISY register */
56 };
57 
58 struct pinctrl_soc_pin {
59 	struct pinctrl_soc_pinmux pinmux;
60 	uint32_t pin_ctrl_flags; /*!< value to write to IOMUXC_SW_PAD_CTL register */
61 };
62 
63 typedef struct pinctrl_soc_pin pinctrl_soc_pin_t;
64 
65 /* This definition must be present. It is used by the mcux gpio driver */
66 #define MCUX_IMX_PINMUX(node_id)						\
67 	{									\
68 	  .mux_register = DT_PROP_BY_IDX(node_id, pinmux, 0),			\
69 	  .config_register = DT_PROP_BY_IDX(node_id, pinmux, 4),		\
70 	  .input_register = DT_PROP_BY_IDX(node_id, pinmux, 2),			\
71 	  .mux_mode = DT_PROP_BY_IDX(node_id, pinmux, 1),			\
72 	  .input_daisy = DT_PROP_BY_IDX(node_id, pinmux, 3),			\
73 	}
74 
75 #define Z_PINCTRL_PINMUX(group_id, pin_prop, idx)				\
76 	MCUX_IMX_PINMUX(DT_PHANDLE_BY_IDX(group_id, pin_prop, idx))
77 
78 #define Z_PINCTRL_STATE_PIN_INIT(group_id, pin_prop, idx)			\
79 	{									\
80 	  .pinmux = Z_PINCTRL_PINMUX(group_id, pin_prop, idx),			\
81 	  .pin_ctrl_flags = Z_PINCTRL_MCUX_IMX_PINCFG_INIT(group_id),		\
82 	},
83 
84 
85 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)			\
86 	{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop),		\
87 		DT_FOREACH_PROP_ELEM, pinmux, Z_PINCTRL_STATE_PIN_INIT)};	\
88 
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #endif /* ZEPHYR_SOC_ARM_NXP_IMX_MCIMX6X_PINCTRL_SOC_H_ */
95