1 /*
2 * Copyright 2021 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_codec_cs42448_adapter.h"
10 #include "fsl_codec_common.h"
11 /*******************************************************************************
12 * Definitions
13 ******************************************************************************/
14 /*! @brief CS42448 play capability*/
15 #define HAL_CS42448_PLAY_CAPABILITY \
16 kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0 | kCODEC_SupportPlayChannelLeft1 | \
17 kCODEC_SupportPlayChannelRight1 | kCODEC_SupportPlayChannelLeft2 | kCODEC_SupportPlayChannelRight2 | \
18 kCODEC_SupportPlayChannelLeft3 | kCODEC_SupportPlayChannelRight3
19 #define HAL_CS42448_VOLUME_CAPABILITY \
20 kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0 | kCODEC_SupportPlayChannelLeft1 | \
21 kCODEC_SupportPlayChannelRight1 | kCODEC_SupportPlayChannelLeft2 | kCODEC_SupportPlayChannelRight2 | \
22 kCODEC_SupportPlayChannelLeft3 | kCODEC_SupportPlayChannelRight3
23 /*******************************************************************************
24 * Prototypes
25 ******************************************************************************/
26
27 /*******************************************************************************
28 * Variables
29 ******************************************************************************/
30 static const codec_capability_t s_cs42448_capability = {
31 .codecPlayCapability = HAL_CS42448_PLAY_CAPABILITY,
32 .codecVolumeCapability = HAL_CS42448_VOLUME_CAPABILITY,
33 };
34 /*******************************************************************************
35 * Code
36 ******************************************************************************/
37 /*!
38 * brief Codec initilization.
39 *
40 * param handle codec handle.
41 * param config codec configuration.
42 * return kStatus_Success is success, else initial failed.
43 */
HAL_CODEC_CS42448_Init(void * handle,void * config)44 status_t HAL_CODEC_CS42448_Init(void *handle, void *config)
45 {
46 assert((config != NULL) && (handle != NULL));
47
48 status_t ret = kStatus_Success;
49 codec_config_t *codecConfig = (codec_config_t *)config;
50
51 cs42448_config_t *devConfig = (cs42448_config_t *)(codecConfig->codecDevConfig);
52 cs42448_handle_t *devHandle = (cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle));
53
54 /* load codec capability */
55 ((codec_handle_t *)handle)->codecCapability = &s_cs42448_capability;
56 /* codec device initialization */
57 ret = CS42448_Init(devHandle, devConfig);
58 if (ret != kStatus_Success)
59 {
60 return ret;
61 }
62
63 return kStatus_Success;
64 }
65
66 /*!
67 * brief Codec de-initilization.
68 *
69 * param handle codec handle.
70 * return kStatus_Success is success, else de-initial failed.
71 */
HAL_CODEC_CS42448_Deinit(void * handle)72 status_t HAL_CODEC_CS42448_Deinit(void *handle)
73 {
74 assert(handle != NULL);
75
76 return CS42448_Deinit((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)));
77 }
78
79 /*!
80 * brief set audio data format.
81 *
82 * param handle codec handle.
83 * param mclk master clock frequency in HZ.
84 * param sampleRate sample rate in HZ.
85 * param bitWidth bit width.
86 * return kStatus_Success is success, else configure failed.
87 */
HAL_CODEC_CS42448_SetFormat(void * handle,uint32_t mclk,uint32_t sampleRate,uint32_t bitWidth)88 status_t HAL_CODEC_CS42448_SetFormat(void *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
89 {
90 assert(handle != NULL);
91
92 return CS42448_ConfigDataFormat((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)), mclk,
93 sampleRate, bitWidth);
94 }
95
96 /*!
97 * brief set audio codec module volume.
98 *
99 * param handle codec handle.
100 * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
101 * param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
102 * return kStatus_Success is success, else configure failed.
103 */
HAL_CODEC_CS42448_SetVolume(void * handle,uint32_t playChannel,uint32_t volume)104 status_t HAL_CODEC_CS42448_SetVolume(void *handle, uint32_t playChannel, uint32_t volume)
105 {
106 assert(handle != NULL);
107 uint8_t i = 0U;
108 status_t ret = kStatus_Success;
109 uint8_t mappedVolume = 0;
110
111 for (i = 0U; i < (uint8_t)kCS42448_AOUT8; i++)
112 {
113 if ((playChannel & (1UL << i)) == 0U)
114 {
115 continue;
116 }
117
118 if (volume == 0U)
119 {
120 ret = CS42448_SetChannelMute((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
121 (uint8_t)(i + 1U), true);
122 }
123 else
124 {
125 /* 1 is mapped t0 255, 100 is mapped to 0 */
126 mappedVolume = (uint8_t)(CS42448_AOUT_MAX_VOLUME_VALUE -
127 ((volume - 1U) * (CS42448_AOUT_MAX_VOLUME_VALUE + 3U)) / 100U);
128
129 ret = CS42448_SetAOUTVolume((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
130 (uint8_t)(i + 1U), mappedVolume);
131 /* unmute the channel */
132 if (ret == kStatus_Success)
133 {
134 ret =
135 CS42448_SetChannelMute((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
136 (uint8_t)(i + 1U), false);
137 }
138 }
139
140 if (ret != kStatus_Success)
141 {
142 return ret;
143 }
144 }
145
146 return kStatus_Success;
147 }
148
149 /*!
150 * brief set audio codec module mute.
151 *
152 * param handle codec handle.
153 * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
154 * param isMute true is mute, false is unmute.
155 * return kStatus_Success is success, else configure failed.
156 */
HAL_CODEC_CS42448_SetMute(void * handle,uint32_t playChannel,bool isMute)157 status_t HAL_CODEC_CS42448_SetMute(void *handle, uint32_t playChannel, bool isMute)
158 {
159 assert(handle != NULL);
160 uint8_t i = 0U;
161 status_t ret = kStatus_Success;
162
163 for (i = 0U; i < (uint8_t)kCS42448_AOUT8; i++)
164 {
165 if ((playChannel & (1UL << i)) == 0U)
166 {
167 continue;
168 }
169
170 ret = CS42448_SetChannelMute((cs42448_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)),
171 (uint8_t)(i + 1U), isMute);
172 if (ret != kStatus_Success)
173 {
174 return ret;
175 }
176 }
177
178 return kStatus_Success;
179 }
180
181 /*!
182 * brief set audio codec module power.
183 *
184 * param handle codec handle.
185 * param module audio codec module.
186 * param powerOn true is power on, false is power down.
187 * return kStatus_Success is success, else configure failed.
188 */
HAL_CODEC_CS42448_SetPower(void * handle,uint32_t module,bool powerOn)189 status_t HAL_CODEC_CS42448_SetPower(void *handle, uint32_t module, bool powerOn)
190 {
191 return kStatus_CODEC_NotSupport;
192 }
193
194 /*!
195 * brief codec set record source.
196 *
197 * param handle codec handle.
198 * param source audio codec record source, can be a value or combine value of _codec_record_source.
199 *
200 * return kStatus_Success is success, else configure failed.
201 */
HAL_CODEC_CS42448_SetRecord(void * handle,uint32_t recordSource)202 status_t HAL_CODEC_CS42448_SetRecord(void *handle, uint32_t recordSource)
203 {
204 return kStatus_CODEC_NotSupport;
205 }
206
207 /*!
208 * brief codec set record channel.
209 *
210 * param handle codec handle.
211 * param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value or combine value
212 of member in _codec_record_channel.
213 * param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
214 member in _codec_record_channel.
215
216 * return kStatus_Success is success, else configure failed.
217 */
HAL_CODEC_CS42448_SetRecordChannel(void * handle,uint32_t leftRecordChannel,uint32_t rightRecordChannel)218 status_t HAL_CODEC_CS42448_SetRecordChannel(void *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
219 {
220 return kStatus_CODEC_NotSupport;
221 }
222
223 /*!
224 * brief codec set play source.
225 *
226 * param handle codec handle.
227 * param playSource audio codec play source, can be a value or combine value of _codec_play_source.
228 *
229 * return kStatus_Success is success, else configure failed.
230 */
HAL_CODEC_CS42448_SetPlay(void * handle,uint32_t playSource)231 status_t HAL_CODEC_CS42448_SetPlay(void *handle, uint32_t playSource)
232 {
233 return kStatus_CODEC_NotSupport;
234 }
235
236 /*!
237 * brief codec module control.
238 *
239 * param handle codec handle.
240 * param cmd module control cmd, reference _codec_module_ctrl_cmd.
241 * param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
242 * of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
243 * codec specific driver for detail configurations.
244 * return kStatus_Success is success, else configure failed.
245 */
HAL_CODEC_CS42448_ModuleControl(void * handle,uint32_t cmd,uint32_t data)246 status_t HAL_CODEC_CS42448_ModuleControl(void *handle, uint32_t cmd, uint32_t data)
247 {
248 return kStatus_CODEC_NotSupport;
249 }
250