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 CLOCK_EnableClock(kCLOCK_Sct);
38 CLOCK_EnableClock(kCLOCK_Dma);
39 #else
40 CLOCK_EnableClock(kCLOCK_InputMux);
41 #endif /* FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE */
42 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
43 }
44
45 /*!
46 * brief Attaches a signal
47 *
48 * This function gates the INPUTPMUX clock.
49 *
50 * param base Base address of the INPUTMUX peripheral.
51 * param index Destination peripheral to attach the signal to.
52 * param connection Selects connection.
53 *
54 * retval None.
55 */
INPUTMUX_AttachSignal(INPUTMUX_Type * base,uint32_t index,inputmux_connection_t connection)56 void INPUTMUX_AttachSignal(INPUTMUX_Type *base, uint32_t index, inputmux_connection_t connection)
57 {
58 uint32_t pmux_id;
59 uint32_t output_id;
60
61 /* extract pmux to be used */
62 pmux_id = ((uint32_t)(connection)) >> PMUX_SHIFT;
63 /* extract function number */
64 output_id = ((uint32_t)(connection)) & ((1UL << PMUX_SHIFT) - 1U);
65 /* programm signal */
66 *(volatile uint32_t *)(((uint32_t)base) + pmux_id + (index * 4U)) = output_id;
67 }
68
69 #if defined(FSL_FEATURE_INPUTMUX_HAS_SIGNAL_ENA)
70 /*!
71 * brief Enable/disable a signal
72 *
73 * This function gates the INPUTPMUX clock.
74 *
75 * param base Base address of the INPUTMUX peripheral.
76 * param signal Enable signal register id and bit offset.
77 * param enable Selects enable or disable.
78 *
79 * retval None.
80 */
INPUTMUX_EnableSignal(INPUTMUX_Type * base,inputmux_signal_t signal,bool enable)81 void INPUTMUX_EnableSignal(INPUTMUX_Type *base, inputmux_signal_t signal, bool enable)
82 {
83 uint32_t ena_id;
84 uint32_t ena_id_mask = (1UL << (32U - ENA_SHIFT)) - 1U;
85 uint32_t bit_offset;
86
87 #if defined(FSL_FEATURE_INPUTMUX_HAS_CHANNEL_MUX) && FSL_FEATURE_INPUTMUX_HAS_CHANNEL_MUX
88 uint32_t chmux_offset;
89 uint32_t chmux_value;
90
91 /* Only enable need to update channel mux */
92 if (enable && ((((uint32_t)signal) & (1UL << CHMUX_AVL_SHIFT)) != 0U))
93 {
94 chmux_offset = (((uint32_t)signal) >> CHMUX_OFF_SHIFT) & ((1UL << (CHMUX_AVL_SHIFT - CHMUX_OFF_SHIFT)) - 1UL);
95 chmux_value = (((uint32_t)signal) >> CHMUX_VAL_SHIFT) & ((1UL << (CHMUX_OFF_SHIFT - CHMUX_VAL_SHIFT)) - 1UL);
96 *(volatile uint32_t *)(((uint32_t)base) + chmux_offset) = chmux_value;
97 }
98 ena_id_mask = (1UL << (CHMUX_VAL_SHIFT - ENA_SHIFT)) - 1U;
99 #endif
100 /* extract enable register to be used */
101 ena_id = (((uint32_t)signal) >> ENA_SHIFT) & ena_id_mask;
102 /* extract enable bit offset */
103 bit_offset = ((uint32_t)signal) & ((1UL << ENA_SHIFT) - 1U);
104 /* set signal */
105 if (enable)
106 {
107 *(volatile uint32_t *)(((uint32_t)base) + ena_id) |= (1UL << bit_offset);
108 }
109 else
110 {
111 *(volatile uint32_t *)(((uint32_t)base) + ena_id) &= ~(1UL << bit_offset);
112 }
113 }
114 #endif
115
116 /*!
117 * brief Deinitialize INPUTMUX peripheral.
118
119 * This function disables the INPUTMUX clock.
120 *
121 * param base Base address of the INPUTMUX peripheral.
122 *
123 * retval None.
124 */
INPUTMUX_Deinit(INPUTMUX_Type * base)125 void INPUTMUX_Deinit(INPUTMUX_Type *base)
126 {
127 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
128 #if defined(FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE) && FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE
129 CLOCK_DisableClock(kCLOCK_Sct);
130 CLOCK_DisableClock(kCLOCK_Dma);
131 #else
132 CLOCK_DisableClock(kCLOCK_InputMux);
133 #endif /* FSL_FEATURE_INPUTMUX_HAS_NO_INPUTMUX_CLOCK_SOURCE */
134 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
135 }
136