1 /*
2 * Copyright 2017-2019 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_codec_common.h"
10 /*******************************************************************************
11 * Definitions
12 ******************************************************************************/
13 /*! @brief codec play and record capability */
14 #define GET_PLAY_CHANNEL_CAPABILITY(capability) ((capability)&0xFFU)
15 #define GET_VOLUME_CAPABILITY(capability) ((capability)&0x1FFU)
16 #define GET_PLAY_SOURCE_CAPABILITY(capability) ((capability) >> 8U)
17 #define GET_RECORD_SOURCE_CAPABILITY(capability) ((capability)&0x3FU)
18 #define GET_RECORD_CHANNEL_CAPABILITY(capability) ((capability) >> 6U)
19 /*******************************************************************************
20 * Variables
21 ******************************************************************************/
22
23 /*******************************************************************************
24 * Code
25 ******************************************************************************/
26 /*!
27 * brief Codec initilization.
28 *
29 * param handle codec handle.
30 * param config codec configuration.
31 * return kStatus_Success is success, else initial failed.
32 */
CODEC_Init(codec_handle_t * handle,codec_config_t * config)33 status_t CODEC_Init(codec_handle_t *handle, codec_config_t *config)
34 {
35 assert((config != NULL) && (handle != NULL));
36
37 /* Set the handle information */
38 handle->codecConfig = config;
39
40 return HAL_CODEC_Init(handle, config);
41 }
42
43 /*!
44 * brief Codec de-initilization.
45 *
46 * param handle codec handle.
47 * return kStatus_Success is success, else de-initial failed.
48 */
CODEC_Deinit(codec_handle_t * handle)49 status_t CODEC_Deinit(codec_handle_t *handle)
50 {
51 assert((handle != NULL) && (handle->codecConfig != NULL));
52
53 return HAL_CODEC_Deinit(handle);
54 }
55
56 /*!
57 * brief set audio data format.
58 *
59 * param handle codec handle.
60 * param mclk master clock frequency in HZ.
61 * param sampleRate sample rate in HZ.
62 * param bitWidth bit width.
63 * return kStatus_Success is success, else configure failed.
64 */
CODEC_SetFormat(codec_handle_t * handle,uint32_t mclk,uint32_t sampleRate,uint32_t bitWidth)65 status_t CODEC_SetFormat(codec_handle_t *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
66 {
67 assert((handle != NULL) && (handle->codecConfig != NULL));
68
69 return HAL_CODEC_SetFormat(handle, mclk, sampleRate, bitWidth);
70 }
71
72 /*!
73 * brief codec module control.
74 *
75 * This function is used for codec module control, support switch digital interface cmd, can be expand to support codec
76 * module specific feature
77 *
78 * param handle codec handle.
79 * param cmd module control cmd, reference _codec_module_ctrl_cmd.
80 * param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
81 * of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
82 * codec specific driver for detail configurations.
83 * return kStatus_Success is success, else configure failed.
84 */
CODEC_ModuleControl(codec_handle_t * handle,codec_module_ctrl_cmd_t cmd,uint32_t data)85 status_t CODEC_ModuleControl(codec_handle_t *handle, codec_module_ctrl_cmd_t cmd, uint32_t data)
86 {
87 assert((handle != NULL) && (handle->codecConfig != NULL));
88 assert(handle->codecCapability != NULL);
89
90 if (cmd == kCODEC_ModuleSwitchI2SInInterface)
91 {
92 if ((handle->codecCapability->codecModuleCapability & (uint32_t)kCODEC_SupportModuleI2SInSwitchInterface) == 0U)
93 {
94 return kStatus_CODEC_NotSupport;
95 }
96 }
97 else
98 {
99 return kStatus_CODEC_NotSupport;
100 }
101
102 return HAL_CODEC_ModuleControl(handle, (uint32_t)cmd, data);
103 }
104
105 /*!
106 * brief set audio codec module volume.
107 *
108 * param handle codec handle.
109 * param channel audio codec volume channel, can be a value or combine value of _codec_play_channel or
110 * _codec_volume_capability. param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
111 * return kStatus_Success is success, else configure failed.
112 */
CODEC_SetVolume(codec_handle_t * handle,uint32_t channel,uint32_t volume)113 status_t CODEC_SetVolume(codec_handle_t *handle, uint32_t channel, uint32_t volume)
114 {
115 assert((handle != NULL) && (handle->codecConfig != NULL));
116 assert(volume <= CODEC_VOLUME_MAX_VALUE);
117 assert(handle->codecCapability != NULL);
118
119 /* check capability of set volume */
120 if ((GET_VOLUME_CAPABILITY(handle->codecCapability->codecVolumeCapability) & channel) == 0U)
121 {
122 return kStatus_CODEC_NotSupport;
123 }
124
125 return HAL_CODEC_SetVolume(handle, channel, volume);
126 }
127
128 /*!
129 * brief set audio codec module mute.
130 *
131 * param handle codec handle.
132 * param channel audio codec volume channel, can be a value or combine value of _codec_play_channel or
133 * _codec_volume_capability. param mute true is mute, false is unmute. return kStatus_Success is success, else configure
134 * failed.
135 */
CODEC_SetMute(codec_handle_t * handle,uint32_t channel,bool mute)136 status_t CODEC_SetMute(codec_handle_t *handle, uint32_t channel, bool mute)
137 {
138 assert((handle != NULL) && (handle->codecConfig != NULL));
139 assert(handle->codecCapability != NULL);
140
141 /* check capability of mute */
142 if ((GET_VOLUME_CAPABILITY(handle->codecCapability->codecVolumeCapability) & channel) == 0U)
143 {
144 return kStatus_CODEC_NotSupport;
145 }
146
147 return HAL_CODEC_SetMute(handle, channel, mute);
148 }
149
150 /*!
151 * brief set audio codec module power.
152 *
153 * param handle codec handle.
154 * param module audio codec module.
155 * param powerOn true is power on, false is power down.
156 * return kStatus_Success is success, else configure failed.
157 */
CODEC_SetPower(codec_handle_t * handle,codec_module_t module,bool powerOn)158 status_t CODEC_SetPower(codec_handle_t *handle, codec_module_t module, bool powerOn)
159 {
160 assert((handle != NULL) && (handle->codecConfig != NULL));
161 assert(handle->codecCapability != NULL);
162
163 /* check capability of power switch */
164 if ((handle->codecCapability->codecModuleCapability & (1UL << (uint32_t)module)) == 0U)
165 {
166 return kStatus_CODEC_NotSupport;
167 }
168
169 return HAL_CODEC_SetPower(handle, (uint32_t)module, powerOn);
170 }
171
172 /*!
173 * brief codec set record source.
174 *
175 * param handle codec handle.
176 * param source audio codec record source, can be a value or combine value of _codec_record_source.
177 *
178 * return kStatus_Success is success, else configure failed.
179 */
CODEC_SetRecord(codec_handle_t * handle,uint32_t recordSource)180 status_t CODEC_SetRecord(codec_handle_t *handle, uint32_t recordSource)
181 {
182 assert((handle != NULL) && (handle->codecConfig != NULL));
183 assert(handle->codecCapability != NULL);
184
185 /* check capability of record capability */
186 if ((GET_RECORD_SOURCE_CAPABILITY(handle->codecCapability->codecRecordCapability) & recordSource) == 0U)
187 {
188 return kStatus_CODEC_NotSupport;
189 }
190
191 return HAL_CODEC_SetRecord(handle, recordSource);
192 }
193
194 /*!
195 * brief codec set record channel.
196 *
197 * param handle codec handle.
198 * param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value or combine value
199 of member in _codec_record_channel.
200 * param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
201 member in _codec_record_channel.
202
203 * return kStatus_Success is success, else configure failed.
204 */
CODEC_SetRecordChannel(codec_handle_t * handle,uint32_t leftRecordChannel,uint32_t rightRecordChannel)205 status_t CODEC_SetRecordChannel(codec_handle_t *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
206 {
207 assert((handle != NULL) && (handle->codecConfig != NULL));
208 assert(handle->codecCapability != NULL);
209
210 /* check capability of record capability */
211 if ((GET_RECORD_CHANNEL_CAPABILITY(handle->codecCapability->codecRecordCapability) & leftRecordChannel) == 0U)
212 {
213 return kStatus_CODEC_NotSupport;
214 }
215
216 if ((GET_RECORD_CHANNEL_CAPABILITY(handle->codecCapability->codecRecordCapability) & rightRecordChannel) == 0U)
217 {
218 return kStatus_CODEC_NotSupport;
219 }
220
221 return HAL_CODEC_SetRecordChannel(handle, leftRecordChannel, rightRecordChannel);
222 }
223
224 /*!
225 * brief codec set play source.
226 *
227 * param handle codec handle.
228 * param playSource audio codec play source, can be a value or combine value of _codec_play_source.
229 *
230 * return kStatus_Success is success, else configure failed.
231 */
CODEC_SetPlay(codec_handle_t * handle,uint32_t playSource)232 status_t CODEC_SetPlay(codec_handle_t *handle, uint32_t playSource)
233 {
234 assert((handle != NULL) && (handle->codecConfig != NULL));
235 assert(handle->codecCapability != NULL);
236
237 /* check capability of record capability */
238 if ((GET_PLAY_SOURCE_CAPABILITY(handle->codecCapability->codecPlayCapability) & playSource) == 0U)
239 {
240 return kStatus_CODEC_NotSupport;
241 }
242
243 return HAL_CODEC_SetPlay(handle, playSource);
244 }
245