1 /*
2  * Copyright 2018-2022 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __SRTM_SAI_SDMA_ADAPTER_H__
10 #define __SRTM_SAI_SDMA_ADAPTER_H__
11 
12 #include "srtm_audio_service.h"
13 #include "fsl_sai_sdma.h"
14 /*!
15  * @addtogroup srtm_service
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 #ifndef SRTM_SAI_CONFIG_Rx_Enabled
23 #define SRTM_SAI_CONFIG_Rx_Enabled 1
24 #endif
25 
26 #ifndef SRTM_SAI_CONFIG_SupportLocalBuf
27 #define SRTM_SAI_CONFIG_SupportLocalBuf 1
28 #endif
29 
30 #ifndef SRTM_SAI_SDMA_MAX_LOCAL_BUF_PERIODS
31 #define SRTM_SAI_SDMA_MAX_LOCAL_BUF_PERIODS (4U)
32 #endif
33 #define SRTM_SAI_SDMA_MAX_LOCAL_PERIOD_ALIGNMENT      (4U)
34 #define SRTM_SAI_SDMA_MAX_LOCAL_PERIOD_ALIGNMENT_MASK (SRTM_SAI_SDMA_MAX_LOCAL_PERIOD_ALIGNMENT - 1U)
35 
36 /*!< init audio device */
37 typedef void (*aud_init_dev)(bool enable);
38 /*!< config audio device based on format and sample rate, return mclk HZ */
39 typedef uint32_t (*aud_conf_dev)(srtm_audio_format_type_t format, uint32_t srate);
40 /*!< config audio mclk when opening adapter */
41 typedef void (*aud_conf_mclk)(I2S_Type *base, sai_master_clock_t *config);
42 
43 typedef struct _audio_misc_set
44 {
45     aud_init_dev audioDevInit;
46     aud_conf_dev audioDevConf;
47     aud_conf_mclk audioMclkConf;
48 } audio_misc_set_t;
49 
50 typedef struct _srtm_sai_sdma_config
51 {
52     sai_transceiver_t config;
53     sai_master_clock_t mclkConfig;
54     uint8_t dataLine1; /* SAI data line1 number for transaction, use the dataLine1 by default for PCM music stream. */
55     uint8_t dataLine2; /* SAI data line2 number for transaction, use the dataLine2 as the another channel of DSD music
56                           stream. */
57     uint32_t dmaChannel;
58     uint8_t ChannelPriority;
59     bool stopOnSuspend;
60     uint32_t eventSource;
61     uint32_t guardTime; /* TX ONLY guardTime (ms): M core needs to make sure there is enough time for A core wake up
62                            from suspend and fill the DDR buffer again. The time should not less than the guardTime */
63     uint32_t threshold; /* TX ONLY threshold: under which will trigger periodDone notification. */
64     sdma_context_data_t txcontext;
65 #if SRTM_SAI_CONFIG_Rx_Enabled
66     sdma_context_data_t rxcontext;
67 #endif
68     audio_misc_set_t extendConfig;
69 } srtm_sai_sdma_config_t;
70 
71 #if SRTM_SAI_CONFIG_SupportLocalBuf
72 typedef struct _srtm_sai_sdma_local_buf
73 {
74     uint8_t *buf;
75     uint32_t bufSize;   /* bytes of the whole local buffer */
76     uint32_t periods;   /* periods in local buffer */
77     uint32_t threshold; /* Threshold period number: under which will trigger copy from share buf to local buf
78                            in playback case. */
79 } srtm_sai_sdma_local_buf_t;
80 #endif
81 
82 #if SRTM_SAI_CONFIG_Rx_Enabled
83 /*! @brief The callback function pointer.  Voice data can be passed to application via callback when the host side is
84  * suspend. The callback is also a notification to the application for the host side resumes from suspend when the data
85  * and bytes are 0(NULL). */
86 typedef void (*srtm_sai_sdma_data_callback_t)(srtm_sai_adapter_t adapter, void *data, uint32_t bytes, void *params);
87 #endif
88 
89 /*******************************************************************************
90  * API
91  ******************************************************************************/
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 
96 /*!
97  * @brief Create SAI SDMA adapter.
98  *
99  * @param sai SAI base address.
100  * @param dma DMA base address.
101  * @param txConfig SAI Tx channel configuration.
102  * @param rxConfig SAI Rx channel configuration.
103  * @return SRTM SAI SDMA adapter on success or NULL on failure.
104  */
105 srtm_sai_adapter_t SRTM_SaiSdmaAdapter_Create(I2S_Type *sai,
106                                               SDMAARM_Type *dma,
107                                               srtm_sai_sdma_config_t *txConfig,
108                                               srtm_sai_sdma_config_t *rxConfig);
109 
110 /*!
111  * @brief Destroy SAI SDMA adapter.
112  *
113  * @param adapter SAI SDMA adapter to destroy.
114  */
115 void SRTM_SaiSdmaAdapter_Destroy(srtm_sai_adapter_t adapter);
116 
117 #if SRTM_SAI_CONFIG_SupportLocalBuf
118 /*!
119  * @brief Set local buffer to use in DMA transfer. If local buffer is set, the audio data will be copied
120  * from shared buffer to local buffer and then transfered to I2S interface. Otherwise the data will be
121  * transfered from shared buffer to I2S interface directly.
122  * NOTE: it must be called before service start.
123  *
124  * @param adapter SAI SDMA adapter to set.
125  * @param localBuf Local buffer information to be set to the adapter TX path.
126  */
127 void SRTM_SaiSdmaAdapter_SetTxLocalBuf(srtm_sai_adapter_t adapter, srtm_sai_sdma_local_buf_t *localBuf);
128 
129 #if SRTM_SAI_CONFIG_Rx_Enabled
130 /*!
131  * @brief Set local buffer to use in DMA transfer. If local buffer is set, the audio data will be transfered
132  * from the I2S interface to local buffer and then copied from local buffer to shared buffer. Otherwise the data will be
133  * transfered from I2S interface to shared buffer directly.
134  * NOTE: it must be called before service start.
135  *
136  * @param adapter SAI SDMA adapter to set.
137  * @param localBuf Local buffer information to be set to the adapter RX path.
138  */
139 void SRTM_SaiSdmaAdapter_SetRxLocalBuf(srtm_sai_adapter_t adapter, srtm_sai_sdma_local_buf_t *localBuf);
140 #endif /* SRTM_SAI_CONFIG_Rx_Enabled */
141 #endif /* SRTM_SAI_CONFIG_SupportLocalBuf */
142 /*!
143  * @brief Get the audio service status.
144  *
145  * @param adapter SAI SDMA adapter instance.
146  * @param pTxState Transfer status pointer.
147  * @param pRxState Receiver status pointer.
148  */
149 void SRTM_SaiSdmaAdapter_GetAudioServiceState(srtm_sai_adapter_t adapter,
150                                               srtm_audio_state_t *pTxState,
151                                               srtm_audio_state_t *pRxState);
152 
153 #if SRTM_SAI_CONFIG_Rx_Enabled
154 /*!
155  * @brief When the host driver suspends, voice data can be passed to application via callback.
156           When the host driver is waking up, the notfication is sent via callback.
157  *        The callback is called in SRTM dispatcher task context and should not cost much time.
158  *
159  * @param adapter SAI SDMA adapter instance.
160  * @param cb Callback function pointer.
161  * @param param Callback function argument to be passed back to applicaiton.
162  */
163 void SRTM_SaiSdmaAdapter_SetDataHandlerOnHostSuspend(srtm_sai_adapter_t adapter,
164                                                      srtm_sai_sdma_data_callback_t cb,
165                                                      void *param);
166 
167 /*!
168  * @brief When key word detected, voice data will be sent to host again.
169  *
170  * @param adapter SAI SDMA adapter instance.
171  * @param cb Callback function pointer.
172  * @param params Callback function argument to be passed back to applicaiton.
173  */
174 void SRTM_SaiSdmaAdapter_ResumeHost(srtm_sai_adapter_t adapter);
175 #endif
176 
177 /*******************************************************************************
178  * Definitions from other files
179  ******************************************************************************/
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 /*! @} */
186 
187 #endif /* __SRTM_SAI_SDMA_ADAPTER_H__ */
188