1 /*
2  * Copyright  2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_codec_tfa9xxx_adapter.h"
10 #include "fsl_codec_common.h"
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 
15 #define HAL_TFA9XXX_PLAY_CAPABILITY   (kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0)
16 #define HAL_TFA9XXX_VOLUME_CAPABILITY (kCODEC_SupportPlayChannelLeft0 | kCODEC_SupportPlayChannelRight0)
17 
18 /*******************************************************************************
19  * Prototypes
20  ******************************************************************************/
21 
22 /*******************************************************************************
23  * Variables
24  ******************************************************************************/
25 static const codec_capability_t s_tfa9xxx_capability = {
26     .codecPlayCapability   = HAL_TFA9XXX_PLAY_CAPABILITY,
27     .codecVolumeCapability = HAL_TFA9XXX_VOLUME_CAPABILITY,
28 };
29 /*******************************************************************************
30  * Code
31  ******************************************************************************/
32 /*!
33  * brief Codec initilization.
34  *
35  * param handle codec handle.
36  * param config codec configuration.
37  * return kStatus_Success is success, else initial failed.
38  */
HAL_CODEC_TFA9XXX_Init(void * handle,void * config)39 status_t HAL_CODEC_TFA9XXX_Init(void *handle, void *config)
40 {
41     assert((config != NULL) && (handle != NULL));
42 
43     codec_config_t *codecConfig = (codec_config_t *)config;
44 
45     tfa9xxx_config_t *devConfig = (tfa9xxx_config_t *)(codecConfig->codecDevConfig);
46     tfa9xxx_handle_t *devHandle = (tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle));
47 
48     ((codec_handle_t *)handle)->codecCapability = &s_tfa9xxx_capability;
49 
50     /* codec device initialization */
51     return TFA9XXX_Init(devHandle, devConfig);
52 }
53 
54 /*!
55  * brief Codec de-initilization.
56  *
57  * param handle codec handle.
58  * return kStatus_Success is success, else de-initial failed.
59  */
HAL_CODEC_TFA9XXX_Deinit(void * handle)60 status_t HAL_CODEC_TFA9XXX_Deinit(void *handle)
61 {
62     assert(handle != NULL);
63 
64     return TFA9XXX_Deinit((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)));
65 }
66 
67 /*!
68  * brief set audio data format.
69  *
70  * param handle codec handle.
71  * param mclk master clock frequency in HZ.
72  * param sampleRate sample rate in HZ.
73  * param bitWidth bit width.
74  * return kStatus_Success is success, else configure failed.
75  */
HAL_CODEC_TFA9XXX_SetFormat(void * handle,uint32_t mclk,uint32_t sampleRate,uint32_t bitWidth)76 status_t HAL_CODEC_TFA9XXX_SetFormat(void *handle, uint32_t mclk, uint32_t sampleRate, uint32_t bitWidth)
77 {
78     assert(handle != NULL);
79 
80     return TFA9XXX_ConfigDataFormat((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)), mclk,
81                                     sampleRate, bitWidth);
82 }
83 
84 /*!
85  * brief set audio codec module volume.
86  *
87  * param handle codec handle.
88  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
89  * param volume volume value, support 0 ~ 100, 0 is mute, 100 is the maximum volume value.
90  * return kStatus_Success is success, else configure failed.
91  */
HAL_CODEC_TFA9XXX_SetVolume(void * handle,uint32_t playChannel,uint32_t volume)92 status_t HAL_CODEC_TFA9XXX_SetVolume(void *handle, uint32_t playChannel, uint32_t volume)
93 {
94     assert(handle != NULL);
95 
96     status_t retVal = kStatus_Success;
97 
98     retVal = TFA9XXX_SetVolume((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)), volume);
99 
100     return retVal;
101 }
102 
103 /*!
104  * brief set audio codec module mute.
105  *
106  * param handle codec handle.
107  * param channel audio codec play channel, can be a value or combine value of _codec_play_channel.
108  * param isMute true is mute, false is unmute.
109  * return kStatus_Success is success, else configure failed.
110  */
HAL_CODEC_TFA9XXX_SetMute(void * handle,uint32_t playChannel,bool isMute)111 status_t HAL_CODEC_TFA9XXX_SetMute(void *handle, uint32_t playChannel, bool isMute)
112 {
113     assert(handle != NULL);
114 
115     status_t retVal = kStatus_Success;
116 
117     retVal = TFA9XXX_SetMute((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)), isMute);
118 
119     return retVal;
120 }
121 
122 /*!
123  * brief set audio codec module power.
124  *
125  * param handle codec handle.
126  * param module audio codec module.
127  * param powerOn true is power on, false is power down.
128  * return kStatus_Success is success, else configure failed.
129  */
HAL_CODEC_TFA9XXX_SetPower(void * handle,uint32_t module,bool powerOn)130 status_t HAL_CODEC_TFA9XXX_SetPower(void *handle, uint32_t module, bool powerOn)
131 {
132     assert(handle != NULL);
133 
134     status_t retVal = kStatus_Success;
135 
136     if (powerOn)
137     {
138         retVal = TFA9XXX_Start((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)));
139     }
140     else
141     {
142         retVal = TFA9XXX_Stop((tfa9xxx_handle_t *)((uint32_t)(((codec_handle_t *)handle)->codecDevHandle)));
143     }
144 
145     return retVal;
146 }
147 
148 /*!
149  * brief codec set record channel.
150  *
151  * param handle codec handle.
152  * param leftRecordChannel audio codec record channel, reference _codec_record_channel, can be a value or combine value
153  of member in _codec_record_channel.
154  * param rightRecordChannel audio codec record channel, reference _codec_record_channel, can be a value combine of
155  member in _codec_record_channel.
156 
157  * return kStatus_Success is success, else configure failed.
158  */
HAL_CODEC_TFA9XXX_SetRecordChannel(void * handle,uint32_t leftRecordChannel,uint32_t rightRecordChannel)159 status_t HAL_CODEC_TFA9XXX_SetRecordChannel(void *handle, uint32_t leftRecordChannel, uint32_t rightRecordChannel)
160 {
161     return kStatus_CODEC_NotSupport;
162 }
163 
164 /*!
165  * brief codec set record source.
166  *
167  * param handle codec handle.
168  * param source audio codec record source, can be a value or combine value of _codec_record_source.
169  *
170  * @return kStatus_Success is success, else configure failed.
171  */
HAL_CODEC_TFA9XXX_SetRecord(void * handle,uint32_t recordSource)172 status_t HAL_CODEC_TFA9XXX_SetRecord(void *handle, uint32_t recordSource)
173 {
174     return kStatus_CODEC_NotSupport;
175 }
176 
177 /*!
178  * brief codec set play source.
179  *
180  * param handle codec handle.
181  * param playSource audio codec play source, can be a value or combine value of _codec_play_source.
182  *
183  * return kStatus_Success is success, else configure failed.
184  */
HAL_CODEC_TFA9XXX_SetPlay(void * handle,uint32_t playSource)185 status_t HAL_CODEC_TFA9XXX_SetPlay(void *handle, uint32_t playSource)
186 {
187     return kStatus_CODEC_NotSupport;
188 }
189 
190 /*!
191  * brief codec module control.
192  *
193  * param handle codec handle.
194  * param cmd module control cmd, reference _codec_module_ctrl_cmd.
195  * param data value to write, when cmd is kCODEC_ModuleRecordSourceChannel, the data should be a value combine
196  *  of channel and source, please reference macro CODEC_MODULE_RECORD_SOURCE_CHANNEL(source, LP, LN, RP, RN), reference
197  *  codec specific driver for detail configurations.
198  * return kStatus_Success is success, else configure failed.
199  */
HAL_CODEC_TFA9XXX_ModuleControl(void * handle,uint32_t cmd,uint32_t data)200 status_t HAL_CODEC_TFA9XXX_ModuleControl(void *handle, uint32_t cmd, uint32_t data)
201 {
202     return kStatus_CODEC_NotSupport;
203 }
204