1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2021 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_inputmux.h"
10
11 /*******************************************************************************
12 * Definitions
13 ******************************************************************************/
14
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.inputmux"
18 #endif
19
20 /*******************************************************************************
21 * Code
22 ******************************************************************************/
23
24 /*!
25 * brief Initialize INPUTMUX peripheral.
26
27 * This function enables the INPUTMUX clock.
28 *
29 * param base Base address of the INPUTMUX peripheral.
30 *
31 * retval None.
32 */
INPUTMUX_Init(INPUTMUX_Type * base)33 void INPUTMUX_Init(INPUTMUX_Type *base)
34 {
35 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
36 #if defined(FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE) && FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE
37 #if (defined(FSL_FEATURE_SOC_SCT_COUNT) && (FSL_FEATURE_SOC_SCT_COUNT > 0))
38 CLOCK_EnableClock(kCLOCK_Sct);
39 #endif /* FSL_FEATURE_SOC_SCT_COUNT */
40 CLOCK_EnableClock(kCLOCK_Dma);
41 #else
42 CLOCK_EnableClock(kCLOCK_InputMux);
43 #endif /* FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE */
44 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
45 }
46
47 /*!
48 * brief Attaches a signal
49 *
50 * This function gates the INPUTPMUX clock.
51 *
52 * param base Base address of the INPUTMUX peripheral.
53 * param index Destination peripheral to attach the signal to.
54 * param connection Selects connection.
55 *
56 * retval None.
57 */
INPUTMUX_AttachSignal(INPUTMUX_Type * base,uint32_t index,inputmux_connection_t connection)58 void INPUTMUX_AttachSignal(INPUTMUX_Type *base, uint32_t index, inputmux_connection_t connection)
59 {
60 uint32_t pmux_id;
61 uint32_t output_id;
62
63 /* extract pmux to be used */
64 pmux_id = ((uint32_t)(connection)) >> PMUX_SHIFT;
65 /* extract function number */
66 output_id = ((uint32_t)(connection)) & ((1UL << PMUX_SHIFT) - 1U);
67 /* programm signal */
68 *(volatile uint32_t *)(((uint32_t)base) + pmux_id + (index * 4U)) = output_id;
69 }
70
71 #if defined(FSL_FEATURE_INPUTMUX_HAS_SIGNAL_ENA)
72 /*!
73 * brief Enable/disable a signal
74 *
75 * This function gates the INPUTPMUX clock.
76 *
77 * param base Base address of the INPUTMUX peripheral.
78 * param signal Enable signal register id and bit offset.
79 * param enable Selects enable or disable.
80 *
81 * retval None.
82 */
INPUTMUX_EnableSignal(INPUTMUX_Type * base,inputmux_signal_t signal,bool enable)83 void INPUTMUX_EnableSignal(INPUTMUX_Type *base, inputmux_signal_t signal, bool enable)
84 {
85 uint32_t ena_id;
86 uint32_t ena_id_mask = (1UL << (32U - ENA_SHIFT)) - 1U;
87 uint32_t bit_offset;
88
89 #if defined(FSL_FEATURE_INPUTMUX_HAS_CHANNEL_MUX) && FSL_FEATURE_INPUTMUX_HAS_CHANNEL_MUX
90 uint32_t chmux_offset;
91 uint32_t chmux_value;
92
93 /* Only enable need to update channel mux */
94 if (enable && ((((uint32_t)signal) & (1UL << CHMUX_AVL_SHIFT)) != 0U))
95 {
96 chmux_offset = (((uint32_t)signal) >> CHMUX_OFF_SHIFT) & ((1UL << (CHMUX_AVL_SHIFT - CHMUX_OFF_SHIFT)) - 1UL);
97 chmux_value = (((uint32_t)signal) >> CHMUX_VAL_SHIFT) & ((1UL << (CHMUX_OFF_SHIFT - CHMUX_VAL_SHIFT)) - 1UL);
98 *(volatile uint32_t *)(((uint32_t)base) + chmux_offset) = chmux_value;
99 }
100 ena_id_mask = (1UL << (CHMUX_VAL_SHIFT - ENA_SHIFT)) - 1U;
101 #endif
102 /* extract enable register to be used */
103 ena_id = (((uint32_t)signal) >> ENA_SHIFT) & ena_id_mask;
104 /* extract enable bit offset */
105 bit_offset = ((uint32_t)signal) & ((1UL << ENA_SHIFT) - 1U);
106 /* set signal */
107 if (enable)
108 {
109 *(volatile uint32_t *)(((uint32_t)base) + ena_id) |= (1UL << bit_offset);
110 }
111 else
112 {
113 *(volatile uint32_t *)(((uint32_t)base) + ena_id) &= ~(1UL << bit_offset);
114 }
115 }
116 #endif
117
118 /*!
119 * brief Deinitialize INPUTMUX peripheral.
120
121 * This function disables the INPUTMUX clock.
122 *
123 * param base Base address of the INPUTMUX peripheral.
124 *
125 * retval None.
126 */
INPUTMUX_Deinit(INPUTMUX_Type * base)127 void INPUTMUX_Deinit(INPUTMUX_Type *base)
128 {
129 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
130 #if defined(FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE) && FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE
131 #if (defined(FSL_FEATURE_SOC_SCT_COUNT) && (FSL_FEATURE_SOC_SCT_COUNT > 0))
132 CLOCK_DisableClock(kCLOCK_Sct);
133 #endif /* FSL_FEATURE_SOC_SCT_COUNT */
134 CLOCK_DisableClock(kCLOCK_Dma);
135 #else
136 CLOCK_DisableClock(kCLOCK_InputMux);
137 #endif /* FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE */
138 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
139 }
140