1 /*
2  * Copyright  2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_codec_tfa9896_adapter.h"
10 #include "fsl_codec_common.h"
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 /*! @brief TFA9896 play capability*/
15 #define HAL_TFA9896_PLAY_CAPABILITY \
16     kCODEC_SupportModuleSpeaker | kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0
17 #define HAL_TFA9896_VOLUME_CAPABILITY kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0
18 /*******************************************************************************
19  * Prototypes
20  ******************************************************************************/
21 
22 /*******************************************************************************
23  * Variables
24  ******************************************************************************/
25 static const codec_capability_t s_tfa9896_capability = {
26     .codecPlayCapability   = (uint32_t)HAL_TFA9896_PLAY_CAPABILITY,
27     .codecVolumeCapability = (uint32_t)HAL_TFA9896_VOLUME_CAPABILITY,
28 };
29 
30 /*******************************************************************************
31  * Code
32  ******************************************************************************/
33 /*!
34  * brief Codec initilization.
35  *
36  * param handle codec handle.
37  * param config codec configuration.
38  * return kStatus_Success is success, else initial failed.
39  */
HAL_CODEC_TFA9896_Init(void * handle,void * config)40 status_t HAL_CODEC_TFA9896_Init(void *handle, void *config)
41 {
42     assert((config != NULL) && (handle != NULL));
43 
44     codec_config_t *codecConfig = (codec_config_t *)config;
45     status_t ret                = kStatus_Success;
46 
47     tfa9896_config_t *devConfig = (tfa9896_config_t *)(codecConfig->codecDevConfig);
48     tfa9896_handle_t *devHandle = (tfa9896_handle_t *)((uint32_t) & (((codec_handle_t *)handle)->codecDevHandle));
49     ((codec_handle_t *)handle)->codecCapability = &s_tfa9896_capability;
50     /* codec device initialization */
51     ret = TFA9896_Init(devHandle, devConfig);
52     if (ret != kStatus_TFA9896_Ok)
53     {
54         ret = kStatus_Fail;
55     }
56 
57     return ret;
58 }
59 
60 /*!
61  * brief Codec de-initilization.
62  *
63  * param handle codec handle.
64  * return kStatus_Success is success, else de-initial failed.
65  */
HAL_CODEC_TFA9896_Deinit(void * handle)66 status_t HAL_CODEC_TFA9896_Deinit(void *handle)
67 {
68     assert(handle != NULL);
69 
70     return TFA9896_Deinit((tfa9896_handle_t *)((uint32_t) & (((codec_handle_t *)handle)->codecDevHandle)));
71 }
72 
73 /*!
74  * brief set audio data format.
75  *
76  * param handle codec handle.
77  * param mclk master clock frequency in HZ.
78  * param sampleRate sample rate in HZ.
79  * param bitWidth bit width.
80  * return kStatus_Success is success, else configure failed.
81  */
HAL_CODEC_TFA9896_SetFormat(void * handle,uint32_t mclk,uint32_t sampleRate,uint32_t bitWidth)82 status_t HAL_CODEC_TFA9896_SetFormat(void *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
83 {
84     assert(handle != NULL);
85 
86     return kStatus_CODEC_NotSupport;
87 }
88 
89 /*!
90  * brief set audio codec module volume.
91  *
92  * param handle codec handle.
93  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
94  * param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
95  * return kStatus_Success is success, else configure failed.
96  */
HAL_CODEC_TFA9896_SetVolume(void * handle,uint32_t playChannel,uint32_t volume)97 status_t HAL_CODEC_TFA9896_SetVolume(void *handle, uint32_t playChannel, uint32_t volume)
98 {
99     assert(handle != NULL);
100     status_t ret                    = kStatus_Success;
101     uint8_t mappedVolume            = 0U;
102     tfa9896_handle_t *tfa9896Handle = (tfa9896_handle_t *)((uint32_t) & (((codec_handle_t *)handle)->codecDevHandle));
103     /* Set Volume 0(min) to 100 (max) */
104 
105     /* Example volume table for the TFA9896
106      * Volume level index and corresponding VOL value(dec) from TFA9896 register 0x06
107      * Vol_lvl  0    10   20   30   40   50   60   70   80   90   100(Maximum volume)
108      * Volume   255  40   30   24   16   10   6    4    2    1    0  (zero attenuation )*/
109 
110     switch (volume / 10U)
111     {
112         case 0U:
113             mappedVolume = 255U;
114             break;
115         case 1U:
116             mappedVolume = 40U;
117             break;
118         case 2U:
119             mappedVolume = 30U;
120             break;
121         case 3U:
122             mappedVolume = 24U;
123             break;
124         case 4U:
125             mappedVolume = 16U;
126             break;
127         case 5U:
128             mappedVolume = 10U;
129             break;
130         case 6U:
131             mappedVolume = 6U;
132             break;
133         case 7U:
134             mappedVolume = 4U;
135             break;
136         case 8U:
137             mappedVolume = 2U;
138             break;
139         case 9U:
140             mappedVolume = 1U;
141             break;
142         case 10U:
143             mappedVolume = 0U;
144             break;
145         default:
146             ret = kStatus_InvalidArgument;
147             break;
148     }
149 
150     if (ret == kStatus_Success)
151     {
152         ret = TFA9896_SetVolume(tfa9896Handle, mappedVolume);
153     }
154 
155     return ret;
156 }
157 
158 /*!
159  * brief set audio codec module mute.
160  *
161  * param handle codec handle.
162  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
163  * param isMute true is mute, false is unmute.
164  * return kStatus_Success is success, else configure failed.
165  */
HAL_CODEC_TFA9896_SetMute(void * handle,uint32_t playChannel,bool isMute)166 status_t HAL_CODEC_TFA9896_SetMute(void *handle, uint32_t playChannel, bool isMute)
167 {
168     assert(handle != NULL);
169     status_t ret = kStatus_Success;
170 
171     ret =
172         TFA9896_SetMute((tfa9896_handle_t *)((uint32_t) & (((codec_handle_t *)handle)->codecDevHandle)), Mute_Digital);
173     if (ret != kStatus_TFA9896_Ok)
174     {
175         ret = kStatus_Fail;
176     }
177 
178     return ret;
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_TFA9896_SetPower(void * handle,uint32_t module,bool powerOn)189 status_t HAL_CODEC_TFA9896_SetPower(void *handle, uint32_t module, bool powerOn)
190 {
191     return kStatus_CODEC_NotSupport;
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  */
HAL_CODEC_TFA9896_SetRecordChannel(void * handle,uint32_t leftRecordChannel,uint32_t rightRecordChannel)205 status_t HAL_CODEC_TFA9896_SetRecordChannel(void *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
206 {
207     return kStatus_CODEC_NotSupport;
208 }
209 
210 /*!
211  * brief codec set record source.
212  *
213  * param handle codec handle.
214  * param source audio codec record source, can be a value or combine value of _codec_record_source.
215  *
216  * return kStatus_Success is success, else configure failed.
217  */
HAL_CODEC_TFA9896_SetRecord(void * handle,uint32_t recordSource)218 status_t HAL_CODEC_TFA9896_SetRecord(void *handle, uint32_t recordSource)
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_TFA9896_SetPlay(void * handle,uint32_t playSource)231 status_t HAL_CODEC_TFA9896_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_TFA9896_ModuleControl(void * handle,uint32_t cmd,uint32_t data)246 status_t HAL_CODEC_TFA9896_ModuleControl(void *handle, uint32_t cmd, uint32_t data)
247 {
248     return kStatus_CODEC_NotSupport;
249 }
250