1 /*
2  * Copyright  2017- 2019 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_CODEC_COMMON_H_
10 #define _FSL_CODEC_COMMON_H_
11 
12 #include "fsl_common.h"
13 #include "fsl_codec_adapter.h"
14 
15 /*!
16  * @addtogroup codec_common
17  * @{
18  */
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief CLOCK driver version 2.3.1. */
25 #define FSL_CODEC_DRIVER_VERSION (MAKE_VERSION(2, 3, 1))
26 /*@}*/
27 
28 /*! @brief codec maximum volume range */
29 #define CODEC_VOLUME_MAX_VALUE (100U)
30 
31 /*! @brief CODEC status
32  * @anchor _codec_status
33  */
34 enum
35 {
36     kStatus_CODEC_NotSupport          = MAKE_STATUS(kStatusGroup_CODEC, 0U), /*!< CODEC not support status */
37     kStatus_CODEC_DeviceNotRegistered = MAKE_STATUS(kStatusGroup_CODEC, 1U), /*!< CODEC device register failed status */
38     kStatus_CODEC_I2CBusInitialFailed =
39         MAKE_STATUS(kStatusGroup_CODEC, 2U), /*!< CODEC i2c bus initialization failed status */
40     kStatus_CODEC_I2CCommandTransferFailed =
41         MAKE_STATUS(kStatusGroup_CODEC, 3U), /*!< CODEC i2c bus command transfer failed status */
42 };
43 
44 /*! @brief AUDIO format definition. */
45 typedef enum _codec_audio_protocol
46 {
47     kCODEC_BusI2S            = 0U, /*!< I2S type */
48     kCODEC_BusLeftJustified  = 1U, /*!< Left justified mode */
49     kCODEC_BusRightJustified = 2U, /*!< Right justified mode */
50     kCODEC_BusPCMA           = 3U, /*!< DSP/PCM A mode */
51     kCODEC_BusPCMB           = 4U, /*!< DSP/PCM B mode */
52     kCODEC_BusTDM            = 5U, /*!< TDM mode */
53 } codec_audio_protocol_t;
54 
55 /*! @brief audio sample rate definition
56  * @anchor _codec_audio_sample_rate
57  */
58 enum
59 {
60     kCODEC_AudioSampleRate8KHz    = 8000U,   /*!< Sample rate 8000 Hz */
61     kCODEC_AudioSampleRate11025Hz = 11025U,  /*!< Sample rate 11025 Hz */
62     kCODEC_AudioSampleRate12KHz   = 12000U,  /*!< Sample rate 12000 Hz */
63     kCODEC_AudioSampleRate16KHz   = 16000U,  /*!< Sample rate 16000 Hz */
64     kCODEC_AudioSampleRate22050Hz = 22050U,  /*!< Sample rate 22050 Hz */
65     kCODEC_AudioSampleRate24KHz   = 24000U,  /*!< Sample rate 24000 Hz */
66     kCODEC_AudioSampleRate32KHz   = 32000U,  /*!< Sample rate 32000 Hz */
67     kCODEC_AudioSampleRate44100Hz = 44100U,  /*!< Sample rate 44100 Hz */
68     kCODEC_AudioSampleRate48KHz   = 48000U,  /*!< Sample rate 48000 Hz */
69     kCODEC_AudioSampleRate96KHz   = 96000U,  /*!< Sample rate 96000 Hz */
70     kCODEC_AudioSampleRate192KHz  = 192000U, /*!< Sample rate 192000 Hz */
71     kCODEC_AudioSampleRate384KHz  = 384000U, /*!< Sample rate 384000 Hz */
72 };
73 
74 /*! @brief audio bit width
75  * @anchor _codec_audio_bit_width
76  */
77 enum
78 {
79     kCODEC_AudioBitWidth16bit = 16U, /*!< audio bit width 16 */
80     kCODEC_AudioBitWidth20bit = 20U, /*!< audio bit width 20 */
81     kCODEC_AudioBitWidth24bit = 24U, /*!< audio bit width 24 */
82     kCODEC_AudioBitWidth32bit = 32U, /*!< audio bit width 32 */
83 };
84 
85 /*! @brief audio codec module*/
86 typedef enum _codec_module
87 {
88     kCODEC_ModuleADC       = 0U,  /*!< codec module ADC */
89     kCODEC_ModuleDAC       = 1U,  /*!< codec module DAC */
90     kCODEC_ModulePGA       = 2U,  /*!< codec module PGA */
91     kCODEC_ModuleHeadphone = 3U,  /*!< codec module headphone */
92     kCODEC_ModuleSpeaker   = 4U,  /*!< codec module speaker */
93     kCODEC_ModuleLinein    = 5U,  /*!< codec module linein */
94     kCODEC_ModuleLineout   = 6U,  /*!< codec module lineout */
95     kCODEC_ModuleVref      = 7U,  /*!< codec module VREF */
96     kCODEC_ModuleMicbias   = 8U,  /*!< codec module MIC BIAS */
97     kCODEC_ModuleMic       = 9U,  /*!< codec module MIC */
98     kCODEC_ModuleI2SIn     = 10U, /*!< codec module I2S in */
99     kCODEC_ModuleI2SOut    = 11U, /*!< codec module I2S out */
100     kCODEC_ModuleMixer     = 12U, /*!< codec module mixer */
101 } codec_module_t;
102 
103 /*! @brief audio codec module control cmd */
104 typedef enum _codec_module_ctrl_cmd
105 {
106     kCODEC_ModuleSwitchI2SInInterface = 0U, /*!< module digital interface siwtch. */
107 } codec_module_ctrl_cmd_t;
108 
109 /*! @brief audio codec module digital interface
110  * @anchor _codec_module_ctrl_i2s_in_interface
111  */
112 enum
113 {
114     kCODEC_ModuleI2SInInterfacePCM = 0U, /*!< Pcm interface*/
115     kCODEC_ModuleI2SInInterfaceDSD = 1U, /*!< DSD interface */
116 };
117 
118 /*! @brief audio codec module record source value
119  * @anchor _codec_record_source
120  */
121 enum
122 {
123     kCODEC_RecordSourceDifferentialLine = 1U,  /*!< record source from differential line */
124     kCODEC_RecordSourceLineInput        = 2U,  /*!< record source from line input */
125     kCODEC_RecordSourceDifferentialMic  = 4U,  /*!< record source from differential mic */
126     kCODEC_RecordSourceDigitalMic       = 8U,  /*!< record source from digital microphone */
127     kCODEC_RecordSourceSingleEndMic     = 16U, /*!< record source from single microphone */
128 };
129 
130 /*! @brief audio codec record channel
131  * @anchor _codec_reocrd_channel
132  */
133 enum
134 {
135     kCODEC_RecordChannelLeft1                 = 1U,  /*!< left record channel 1 */
136     kCODEC_RecordChannelLeft2                 = 2U,  /*!< left record channel 2 */
137     kCODEC_RecordChannelLeft3                 = 4U,  /*!< left record channel 3 */
138     kCODEC_RecordChannelRight1                = 1U,  /*!< right record channel 1 */
139     kCODEC_RecordChannelRight2                = 2U,  /*!< right record channel 2 */
140     kCODEC_RecordChannelRight3                = 4U,  /*!< right record channel 3 */
141     kCODEC_RecordChannelDifferentialPositive1 = 1U,  /*!< differential positive record channel 1 */
142     kCODEC_RecordChannelDifferentialPositive2 = 2U,  /*!< differential positive record channel 2 */
143     kCODEC_RecordChannelDifferentialPositive3 = 4U,  /*!< differential positive record channel 3 */
144     kCODEC_RecordChannelDifferentialNegative1 = 8U,  /*!< differential negative record channel 1 */
145     kCODEC_RecordChannelDifferentialNegative2 = 16U, /*!< differential negative record channel 2 */
146     kCODEC_RecordChannelDifferentialNegative3 = 32U, /*!< differential negative record channel 3 */
147 };
148 
149 /*! @brief audio codec module play source value
150  * @anchor _codec_play_source
151  */
152 enum
153 {
154     kCODEC_PlaySourcePGA          = 1U, /*!< play source PGA, bypass ADC */
155     kCODEC_PlaySourceInput        = 2U, /*!< play source Input3 */
156     kCODEC_PlaySourceDAC          = 4U, /*!< play source DAC */
157     kCODEC_PlaySourceMixerIn      = 1U, /*!< play source mixer in */
158     kCODEC_PlaySourceMixerInLeft  = 2U, /*!< play source mixer in left */
159     kCODEC_PlaySourceMixerInRight = 4U, /*!< play source mixer in right */
160     kCODEC_PlaySourceAux          = 8U, /*!< play source mixer in AUx */
161 };
162 
163 /*! @brief codec play channel
164  * @anchor _codec_play_channel
165  */
166 enum
167 {
168     kCODEC_PlayChannelHeadphoneLeft  = 1U,  /*!< play channel headphone left */
169     kCODEC_PlayChannelHeadphoneRight = 2U,  /*!< play channel headphone right */
170     kCODEC_PlayChannelSpeakerLeft    = 4U,  /*!< play channel speaker left */
171     kCODEC_PlayChannelSpeakerRight   = 8U,  /*!< play channel speaker right */
172     kCODEC_PlayChannelLineOutLeft    = 16U, /*!< play channel lineout left */
173     kCODEC_PlayChannelLineOutRight   = 32U, /*!< play channel lineout right */
174 
175     kCODEC_PlayChannelLeft0  = 1U,   /*!< play channel left0 */
176     kCODEC_PlayChannelRight0 = 2U,   /*!< play channel right0 */
177     kCODEC_PlayChannelLeft1  = 4U,   /*!< play channel left1 */
178     kCODEC_PlayChannelRight1 = 8U,   /*!< play channel right1 */
179     kCODEC_PlayChannelLeft2  = 16U,  /*!< play channel left2 */
180     kCODEC_PlayChannelRight2 = 32U,  /*!< play channel right2 */
181     kCODEC_PlayChannelLeft3  = 64U,  /*!< play channel left3 */
182     kCODEC_PlayChannelRight3 = 128U, /*!< play channel right3 */
183 };
184 
185 /*! @brief codec volume setting
186  * @anchor _codec_volume_capability
187  */
188 enum
189 {
190     kCODEC_VolumeHeadphoneLeft  = 1U,  /*!< headphone left volume */
191     kCODEC_VolumeHeadphoneRight = 2U,  /*!< headphone right volume */
192     kCODEC_VolumeSpeakerLeft    = 4U,  /*!< speaker left volume */
193     kCODEC_VolumeSpeakerRight   = 8U,  /*!< speaker right volume */
194     kCODEC_VolumeLineOutLeft    = 16U, /*!< lineout left volume */
195     kCODEC_VolumeLineOutRight   = 32U, /*!< lineout right volume */
196 
197     kCODEC_VolumeLeft0  = 1UL << 0U, /*!< left0 volume */
198     kCODEC_VolumeRight0 = 1UL << 1U, /*!< right0 volume */
199     kCODEC_VolumeLeft1  = 1UL << 2U, /*!< left1 volume */
200     kCODEC_VolumeRight1 = 1UL << 3U, /*!< right1 volume */
201     kCODEC_VolumeLeft2  = 1UL << 4U, /*!< left2 volume */
202     kCODEC_VolumeRight2 = 1UL << 5U, /*!< right2 volume */
203     kCODEC_VolumeLeft3  = 1UL << 6U, /*!< left3 volume */
204     kCODEC_VolumeRight3 = 1UL << 7U, /*!< right3 volume */
205 
206     kCODEC_VolumeDAC = 1UL << 8U, /*!< dac volume */
207 };
208 
209 /*! @brief audio codec capability
210  * @anchor _codec_capability_flag
211  */
212 enum
213 {
214     kCODEC_SupportModuleADC                  = 1U << 0U,  /*!< codec capability of module ADC */
215     kCODEC_SupportModuleDAC                  = 1U << 1U,  /*!< codec capability of module DAC */
216     kCODEC_SupportModulePGA                  = 1U << 2U,  /*!< codec capability of module PGA */
217     kCODEC_SupportModuleHeadphone            = 1U << 3U,  /*!< codec capability of module headphone */
218     kCODEC_SupportModuleSpeaker              = 1U << 4U,  /*!< codec capability of module speaker */
219     kCODEC_SupportModuleLinein               = 1U << 5U,  /*!< codec capability of module linein */
220     kCODEC_SupportModuleLineout              = 1U << 6U,  /*!< codec capability of module lineout */
221     kCODEC_SupportModuleVref                 = 1U << 7U,  /*!< codec capability of module vref */
222     kCODEC_SupportModuleMicbias              = 1U << 8U,  /*!< codec capability of module mic bias */
223     kCODEC_SupportModuleMic                  = 1U << 9U,  /*!< codec capability of module mic bias */
224     kCODEC_SupportModuleI2SIn                = 1U << 10U, /*!< codec capability of module I2S in */
225     kCODEC_SupportModuleI2SOut               = 1U << 11U, /*!< codec capability of module I2S out */
226     kCODEC_SupportModuleMixer                = 1U << 12U, /*!< codec capability of module mixer */
227     kCODEC_SupportModuleI2SInSwitchInterface = 1U << 13U, /*!< codec capability of module I2S in switch interface */
228 
229     kCODEC_SupportPlayChannelLeft0  = 1U << 0U, /*!< codec capability of play channel left 0 */
230     kCODEC_SupportPlayChannelRight0 = 1U << 1U, /*!< codec capability of play channel right 0 */
231     kCODEC_SupportPlayChannelLeft1  = 1U << 2U, /*!< codec capability of play channel left 1 */
232     kCODEC_SupportPlayChannelRight1 = 1U << 3U, /*!< codec capability of play channel right 1 */
233     kCODEC_SupportPlayChannelLeft2  = 1U << 4U, /*!< codec capability of play channel left 2 */
234     kCODEC_SupportPlayChannelRight2 = 1U << 5U, /*!< codec capability of play channel right 2 */
235     kCODEC_SupportPlayChannelLeft3  = 1U << 6U, /*!< codec capability of play channel left 3 */
236     kCODEC_SupportPlayChannelRight3 = 1U << 7U, /*!< codec capability of play channel right 3 */
237 
238     kCODEC_SupportPlaySourcePGA          = 1U << 8U,  /*!< codec capability of set playback source PGA */
239     kCODEC_SupportPlaySourceInput        = 1U << 9U,  /*!< codec capability of set playback source INPUT */
240     kCODEC_SupportPlaySourceDAC          = 1U << 10U, /*!< codec capability of set playback source DAC */
241     kCODEC_SupportPlaySourceMixerIn      = 1U << 11U, /*!< codec capability of set play source Mixer in */
242     kCODEC_SupportPlaySourceMixerInLeft  = 1U << 12U, /*!< codec capability of set play source Mixer in left */
243     kCODEC_SupportPlaySourceMixerInRight = 1U << 13U, /*!< codec capability of set play source Mixer in right */
244     kCODEC_SupportPlaySourceAux          = 1U << 14U, /*!< codec capability of set play source aux */
245 
246     kCODEC_SupportRecordSourceDifferentialLine = 1U << 0U,  /*!< codec capability of record source differential line */
247     kCODEC_SupportRecordSourceLineInput        = 1U << 1U,  /*!< codec capability of record source line input */
248     kCODEC_SupportRecordSourceDifferentialMic  = 1U << 2U,  /*!< codec capability of record source differential mic */
249     kCODEC_SupportRecordSourceDigitalMic       = 1U << 3U,  /*!< codec capability of record digital mic */
250     kCODEC_SupportRecordSourceSingleEndMic     = 1U << 4U,  /*!< codec capability of single end mic */
251     kCODEC_SupportRecordChannelLeft1           = 1U << 6U,  /*!< left record channel 1 */
252     kCODEC_SupportRecordChannelLeft2           = 1U << 7U,  /*!< left record channel 2 */
253     kCODEC_SupportRecordChannelLeft3           = 1U << 8U,  /*!< left record channel 3 */
254     kCODEC_SupportRecordChannelRight1          = 1U << 9U,  /*!< right record channel 1 */
255     kCODEC_SupportRecordChannelRight2          = 1U << 10U, /*!< right record channel 2 */
256     kCODEC_SupportRecordChannelRight3          = 1U << 11U, /*!< right record channel 3 */
257 };
258 
259 /*!@brief codec handle declaration */
260 typedef struct _codec_handle codec_handle_t;
261 
262 /*! @brief Initialize structure of the codec */
263 typedef struct _codec_config
264 {
265     uint32_t codecDevType; /*!< codec type */
266     void *codecDevConfig;  /*!< Codec device specific configuration */
267 } codec_config_t;
268 
269 /*! @brief codec capability */
270 typedef struct _codec_capability
271 {
272     uint32_t codecModuleCapability; /*!< codec module capability */
273     uint32_t codecPlayCapability;   /*!< codec play capability */
274     uint32_t codecRecordCapability; /*!< codec record capability */
275     uint32_t codecVolumeCapability; /*!< codec volume capability */
276 } codec_capability_t;
277 
278 /*! @brief Codec handle definition.
279  * * Application should allocate a buffer with CODEC_HANDLE_SIZE for handle definition, such as
280  * uint8_t codecHandleBuffer[CODEC_HANDLE_SIZE];
281  * codec_handle_t *codecHandle = codecHandleBuffer;
282  */
283 struct _codec_handle
284 {
285     codec_config_t *codecConfig;                    /*!< codec configuration function pointer */
286     const codec_capability_t *codecCapability;      /*!< codec capability */
287     uint8_t codecDevHandle[HAL_CODEC_HANDLER_SIZE]; /*!< codec device handle */
288 };
289 
290 /*******************************************************************************
291  * API
292  ******************************************************************************/
293 
294 #if defined(__cplusplus)
295 extern "C" {
296 #endif
297 /*!
298  * @brief Codec initilization.
299  *
300  * @param handle codec handle.
301  * @param config codec configurations.
302  * @return kStatus_Success is success, else de-initial failed.
303  */
304 status_t CODEC_Init(codec_handle_t *handle, codec_config_t *config);
305 
306 /*!
307  * @brief Codec de-initilization.
308  *
309  * @param handle codec handle.
310  * @return kStatus_Success is success, else de-initial failed.
311  */
312 status_t CODEC_Deinit(codec_handle_t *handle);
313 
314 /*!
315  * @brief set audio data format.
316  *
317  * @param handle codec handle.
318  * @param mclk master clock frequency in HZ.
319  * @param sampleRate sample rate in HZ.
320  * @param bitWidth bit width.
321  * @return kStatus_Success is success, else configure failed.
322  */
323 status_t CODEC_SetFormat(codec_handle_t *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth);
324 
325 /*!
326  * @brief codec module control.
327  *
328  * This function is used for codec module control, support switch digital interface cmd, can be expand to support codec
329  * module specific feature.
330  *
331  * @param handle codec handle.
332  * @param cmd module control cmd, reference _codec_module_ctrl_cmd.
333  * @param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
334  *  of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
335  *  codec specific driver for detail configurations.
336  * @return kStatus_Success is success, else configure failed.
337  */
338 status_t CODEC_ModuleControl(codec_handle_t *handle, codec_module_ctrl_cmd_t cmd, uint32_t data);
339 
340 /*!
341  * @brief set audio codec pl volume.
342  *
343  * @param handle codec handle.
344  * @param channel audio codec volume channel, can be a value or combine value of _codec_volume_capability or
345  * _codec_play_channel.
346  * @param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
347  * @return kStatus_Success is success, else configure failed.
348  */
349 status_t CODEC_SetVolume(codec_handle_t *handle, uint32_t channel, uint32_t volume);
350 
351 /*!
352  * @brief set audio codec module mute.
353  *
354  * @param handle codec handle.
355  * @param channel audio codec volume channel, can be a value or combine value of _codec_volume_capability or
356  * _codec_play_channel.
357  * @param mute true is mute, false is unmute.
358  * @return kStatus_Success is success, else configure failed.
359  */
360 status_t CODEC_SetMute(codec_handle_t *handle, uint32_t channel, bool mute);
361 
362 /*!
363  * @brief set audio codec power.
364  *
365  * @param handle codec handle.
366  * @param module audio codec module.
367  * @param powerOn true is power on, false is power down.
368  * @return kStatus_Success is success, else configure failed.
369  */
370 status_t CODEC_SetPower(codec_handle_t *handle, codec_module_t module, bool powerOn);
371 
372 /*!
373  * @brief codec set record source.
374  *
375  * @param handle codec handle.
376  * @param recordSource audio codec record source, can be a value or combine value of _codec_record_source.
377  *
378  * @return kStatus_Success is success, else configure failed.
379  */
380 status_t CODEC_SetRecord(codec_handle_t *handle, uint32_t recordSource);
381 
382 /*!
383  * @brief codec set record channel.
384  *
385  * @param handle codec handle.
386  * @param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
387  member in _codec_record_channel.
388  * @param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
389  member in _codec_record_channel.
390 
391  * @return kStatus_Success is success, else configure failed.
392  */
393 status_t CODEC_SetRecordChannel(codec_handle_t *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel);
394 
395 /*!
396  * @brief codec set play source.
397  *
398  * @param handle codec handle.
399  * @param playSource audio codec play source, can be a value or combine value of _codec_play_source.
400  *
401  * @return kStatus_Success is success, else configure failed.
402  */
403 status_t CODEC_SetPlay(codec_handle_t *handle, uint32_t playSource);
404 
405 #if defined(__cplusplus)
406 }
407 #endif
408 /*! @} */
409 
410 #endif /* _FSL_CODEC_COMMON_H_ */
411