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