1 /*
2  * Copyright  2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_codec_da7212_adapter.h"
10 #include "fsl_codec_common.h"
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 /*! @brief module capability definition */
15 #define HAL_DA7212_MODULE_CAPABILITY \
16     kCODEC_SupportModuleADC | kCODEC_SupportModuleDAC | kCODEC_SupportModuleHeadphone | kCODEC_SupportModuleSpeaker
17 #define HAL_DA7212_PLAY_CAPABILITY                                                                      \
18     kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0 | kCODEC_SupportPlayChannelLeft1 | \
19         kCODEC_SupportPlayChannelRight1
20 #define HAL_DA7212_RECORD_CAPABILITY                                                   \
21     kCODEC_SupportRecordSourceDifferentialMic | kCODEC_SupportRecordSourceDigitalMic | \
22         kCODEC_SupportRecordSourceSingleEndMic | kCODEC_SupportRecordSourceLineInput
23 
24 /*! @brief DA7212 map module */
25 #define HAL_DA7212_MAP_PROTOCOL(protocol)                               \
26     (protocol == kCODEC_BusI2S            ? kDA7212_BusI2S :            \
27      protocol == kCODEC_BusLeftJustified  ? kDA7212_BusLeftJustified :  \
28      protocol == kCODEC_BusRightJustified ? kDA7212_BusRightJustified : \
29      protocol == kCODEC_BusPCMA           ? kDA7212_BusDSPMode :        \
30                                             kDA7212_BusI2S)
31 #define HAL_DA7212_VOLUME_CAPABILITY                                                                    \
32     kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0 | kCODEC_SupportPlayChannelLeft1 | \
33         kCODEC_SupportPlayChannelRight1
34 /*******************************************************************************
35  * Prototypes
36  ******************************************************************************/
37 
38 /*******************************************************************************
39  * Variables
40  ******************************************************************************/
41 static const codec_capability_t s_da7212_capability = {
42     .codecPlayCapability   = HAL_DA7212_PLAY_CAPABILITY,
43     .codecModuleCapability = HAL_DA7212_MODULE_CAPABILITY,
44     .codecRecordCapability = HAL_DA7212_RECORD_CAPABILITY,
45     .codecVolumeCapability = HAL_DA7212_VOLUME_CAPABILITY,
46 };
47 /*******************************************************************************
48  * Code
49  ******************************************************************************/
50 /*!
51  * brief Codec initilization.
52  *
53  * param handle codec handle.
54  * param config codec configuration.
55  * return kStatus_Success is success, else initial failed.
56  */
HAL_CODEC_DA7212_Init(void * handle,void * config)57 status_t HAL_CODEC_DA7212_Init(void *handle, void *config)
58 {
59     assert((config != NULL) && (handle != NULL));
60 
61     codec_config_t *codecConfig = (codec_config_t *)config;
62 
63     da7212_config_t *devConfig = (da7212_config_t *)(codecConfig->codecDevConfig);
64     da7212_handle_t *devHandle = (da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle));
65 
66     ((codec_handle_t *)handle)->codecCapability = &s_da7212_capability;
67 
68     /* codec device initialization */
69     return DA7212_Init(devHandle, devConfig);
70 }
71 
72 /*!
73  * brief Codec de-initilization.
74  *
75  * param handle codec handle.
76  * return kStatus_Success is success, else de-initial failed.
77  */
HAL_CODEC_DA7212_Deinit(void * handle)78 status_t HAL_CODEC_DA7212_Deinit(void *handle)
79 {
80     assert(handle != NULL);
81 
82     return DA7212_Deinit((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)));
83 }
84 
85 /*!
86  * brief set audio data format.
87  *
88  * param handle codec handle.
89  * param mclk master clock frequency in HZ.
90  * param sampleRate sample rate in HZ.
91  * param bitWidth bit width.
92  * return kStatus_Success is success, else configure failed.
93  */
HAL_CODEC_DA7212_SetFormat(void * handle,uint32_t mclk,uint32_t sampleRate,uint32_t bitWidth)94 status_t HAL_CODEC_DA7212_SetFormat(void *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
95 {
96     assert(handle != NULL);
97 
98     return DA7212_ConfigAudioFormat((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)), mclk,
99                                     sampleRate, bitWidth);
100 }
101 
102 /*!
103  * brief set audio codec module volume.
104  *
105  * param handle codec handle.
106  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
107  * param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
108  * return kStatus_Success is success, else configure failed.
109  */
HAL_CODEC_DA7212_SetVolume(void * handle,uint32_t playChannel,uint32_t volume)110 status_t HAL_CODEC_DA7212_SetVolume(void *handle, uint32_t playChannel, uint32_t volume)
111 {
112     assert(handle != NULL);
113 
114     uint32_t mappedVolume = 0;
115     status_t ret          = kStatus_Success;
116 
117     if (volume == 0U)
118     {
119         return DA7212_SetChannelMute((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
120                                      playChannel, true);
121     }
122 
123     mappedVolume = ((volume - 1U) * (DA7212_HEADPHONE_MAX_VOLUME_VALUE + 1U)) / 100U;
124 
125     ret = DA7212_SetChannelVolume((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
126                                   playChannel, mappedVolume);
127 
128     if (ret == kStatus_Success)
129     {
130         ret = DA7212_SetChannelMute((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
131                                     playChannel, false);
132     }
133 
134     return ret;
135 }
136 
137 /*!
138  * brief set audio codec module mute.
139  *
140  * param handle codec handle.
141  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
142  * param isMute true is mute, false is unmute.
143  * return kStatus_Success is success, else configure failed.
144  */
HAL_CODEC_DA7212_SetMute(void * handle,uint32_t playChannel,bool isMute)145 status_t HAL_CODEC_DA7212_SetMute(void *handle, uint32_t playChannel, bool isMute)
146 {
147     assert(handle != NULL);
148 
149     return DA7212_SetChannelMute((da7212_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
150                                  playChannel, isMute);
151 }
152 
153 /*!
154  * brief set audio codec module power.
155  *
156  * param handle codec handle.
157  * param module audio codec module.
158  * param powerOn true is power on, false is power down.
159  * return kStatus_Success is success, else configure failed.
160  */
HAL_CODEC_DA7212_SetPower(void * handle,uint32_t module,bool powerOn)161 status_t HAL_CODEC_DA7212_SetPower(void *handle, uint32_t module, bool powerOn)
162 {
163     return kStatus_CODEC_NotSupport;
164 }
165 
166 /*!
167  * brief codec set record source.
168  *
169  * param handle codec handle.
170  * param source audio codec record source, can be a value or combine value of _codec_record_source.
171  *
172  * return kStatus_Success is success, else configure failed.
173  */
HAL_CODEC_DA7212_SetRecord(void * handle,uint32_t recordSource)174 status_t HAL_CODEC_DA7212_SetRecord(void *handle, uint32_t recordSource)
175 {
176     return kStatus_CODEC_NotSupport;
177 }
178 
179 /*!
180  * brief codec set record channel.
181  *
182  * param handle codec handle.
183  * param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value or combine value
184  of member in _codec_record_channel.
185  * param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
186  member in _codec_record_channel.
187 
188  * return kStatus_Success is success, else configure failed.
189  */
HAL_CODEC_DA7212_SetRecordChannel(void * handle,uint32_t leftRecordChannel,uint32_t rightRecordChannel)190 status_t HAL_CODEC_DA7212_SetRecordChannel(void *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
191 {
192     return kStatus_CODEC_NotSupport;
193 }
194 
195 /*!
196  * brief codec set play source.
197  *
198  * param handle codec handle.
199  * param playSource audio codec play source, can be a value or combine value of _codec_play_source.
200  *
201  * return kStatus_Success is success, else configure failed.
202  */
HAL_CODEC_DA7212_SetPlay(void * handle,uint32_t playSource)203 status_t HAL_CODEC_DA7212_SetPlay(void *handle, uint32_t playSource)
204 {
205     return kStatus_CODEC_NotSupport;
206 }
207 
208 /*!
209  * brief codec module control.
210  *
211  * param handle codec handle.
212  * param cmd module control cmd, reference _codec_module_ctrl_cmd.
213  * param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
214  *  of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
215  *  codec specific driver for detail configurations.
216  * return kStatus_Success is success, else configure failed.
217  */
HAL_CODEC_DA7212_ModuleControl(void * handle,uint32_t cmd,uint32_t data)218 status_t HAL_CODEC_DA7212_ModuleControl(void *handle, uint32_t cmd, uint32_t data)
219 {
220     return kStatus_CODEC_NotSupport;
221 }
222