• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "dsp_ops.h"
20 #include "spi_if.h"
21 #include "audio_dsp_if.h"
22 #include "audio_driver_log.h"
23 #include "audio_codec_base.h"
24 
25 #define HDF_LOG_TAG dsp_ops
26 
27 #define DEFAULT_SPEED                   (2000000)
28 #define BITS_PER_WORD_EIGHT             (8)
29 #define DSP_CS_NUM                      (1)
30 #define DSP_SPI_BUS_NUM                 (1)
31 
32 enum DspI2sFormatRegVal {
33     I2S_SAMPLE_FORMAT_REG_VAL_MSB_24    = 0x2,    /*  MSB-justified data up to 24 bits */
34     I2S_SAMPLE_FORMAT_REG_VAL_24        = 0x3,    /*  I2S data up to 24 bits */
35     I2S_SAMPLE_FORMAT_REG_VAL_LSB_16    = 0x4,    /*  LSB-justified 16-bit data */
36     I2S_SAMPLE_FORMAT_REG_VAL_LSB_18    = 0x5,    /*  LSB-justified 18-bit data */
37     I2S_SAMPLE_FORMAT_REG_VAL_LSB_20    = 0x6,    /*  LSB-justified 20-bit data */
38     I2S_SAMPLE_FORMAT_REG_VAL_LSB_24    = 0x7,    /*  LSB-justified 24-bit data */
39 };
40 
41 struct SpiDevInfo g_devInfo = {
42     .busNum = DSP_SPI_BUS_NUM,
43     .csNum = DSP_CS_NUM,
44 };
45 
DspDaiStartup(const struct AudioCard * card,const struct DaiDevice * device)46 int32_t DspDaiStartup(const struct AudioCard *card, const struct DaiDevice *device)
47 {
48     (void)card;
49     (void)device;
50     return HDF_SUCCESS;
51 }
52 
DspCfgI2sFrequency(uint32_t rate,uint16_t * frequency)53 static int32_t DspCfgI2sFrequency(uint32_t rate, uint16_t *frequency)
54 {
55     if (frequency == NULL) {
56         AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
57         return HDF_ERR_INVALID_PARAM;
58     }
59 
60     switch (rate) {
61         case I2S_SAMPLE_FREQUENCY_8000:
62             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_8000;
63             break;
64         case I2S_SAMPLE_FREQUENCY_11025:
65             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_11025;
66             break;
67         case I2S_SAMPLE_FREQUENCY_12000:
68             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_12000;
69             break;
70         case I2S_SAMPLE_FREQUENCY_16000:
71             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_16000;
72             break;
73         case I2S_SAMPLE_FREQUENCY_22050:
74             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_22050;
75             break;
76         case I2S_SAMPLE_FREQUENCY_24000:
77             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_24000;
78             break;
79         case I2S_SAMPLE_FREQUENCY_32000:
80             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_32000;
81             break;
82         case I2S_SAMPLE_FREQUENCY_44100:
83             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_44100;
84             break;
85         case I2S_SAMPLE_FREQUENCY_48000:
86             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_48000;
87             break;
88         case I2S_SAMPLE_FREQUENCY_64000:
89             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_64000;
90             break;
91         case I2S_SAMPLE_FREQUENCY_88200:
92             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_88200;
93             break;
94         case I2S_SAMPLE_FREQUENCY_96000:
95             *frequency = I2S_SAMPLE_FREQUENCY_REG_VAL_96000;
96             break;
97         default:
98             AUDIO_DRIVER_LOG_ERR("rate: %d is not support.", rate);
99             return HDF_ERR_NOT_SUPPORT;
100     }
101     return HDF_SUCCESS;
102 }
103 
DspSetI2sBitWidth(enum AudioFormat format,uint16_t * bitWidth)104 static int32_t DspSetI2sBitWidth(enum AudioFormat format, uint16_t *bitWidth)
105 {
106     if (bitWidth == NULL) {
107         AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
108         return HDF_ERR_INVALID_PARAM;
109     }
110 
111     switch (format) {
112         case AUDIO_FORMAT_TYPE_PCM_8_BIT:
113             *bitWidth = I2S_SAMPLE_FORMAT_REG_VAL_24;
114             break;
115         case AUDIO_FORMAT_TYPE_PCM_16_BIT:
116             *bitWidth = I2S_SAMPLE_FORMAT_REG_VAL_24;
117             break;
118         case AUDIO_FORMAT_TYPE_PCM_24_BIT:
119             *bitWidth = I2S_SAMPLE_FORMAT_REG_VAL_24;
120             break;
121         default:
122             AUDIO_DRIVER_LOG_ERR("format: %d is not support.", format);
123             return HDF_ERR_NOT_SUPPORT;
124     }
125     return HDF_SUCCESS;
126 }
127 
DspSetI2sFrequency(uint16_t frequencyVal)128 static int DspSetI2sFrequency(uint16_t frequencyVal)
129 {
130     return HDF_SUCCESS;
131 }
132 
DspSetI2sFormat(uint16_t formatVal)133 static int DspSetI2sFormat(uint16_t formatVal)
134 {
135     return HDF_SUCCESS;
136 }
137 
DspDaiHwParams(const struct AudioCard * card,const struct AudioPcmHwParams * param)138 int32_t DspDaiHwParams(const struct AudioCard *card, const struct AudioPcmHwParams *param)
139 {
140     int ret;
141     uint16_t frequency, bitWidth;
142     (void)card;
143 
144     AUDIO_DRIVER_LOG_DEBUG("entry.");
145     if (param == NULL ||  param->cardServiceName == NULL) {
146         AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
147         return HDF_ERR_INVALID_PARAM;
148     }
149     ret = DspCfgI2sFrequency(param->rate, &frequency);
150     if (ret != HDF_SUCCESS) {
151         AUDIO_DRIVER_LOG_ERR("RateToFrequency fail.");
152         return HDF_ERR_NOT_SUPPORT;
153     }
154     ret = DspSetI2sBitWidth(param->format, &bitWidth);
155     if (ret != HDF_SUCCESS) {
156         AUDIO_DRIVER_LOG_ERR("FormatToBitWidth fail.");
157         return HDF_ERR_NOT_SUPPORT;
158     }
159     ret = DspSetI2sFrequency(frequency);
160     if (ret != HDF_SUCCESS) {
161         AUDIO_DRIVER_LOG_ERR("SetDspI2sFs fail.");
162         return HDF_FAILURE;
163     }
164     ret = DspSetI2sFormat(bitWidth);
165     if (ret != HDF_SUCCESS) {
166         AUDIO_DRIVER_LOG_ERR("SetDspI2sFormat fail.");
167         return HDF_FAILURE;
168     }
169     AUDIO_DRIVER_LOG_DEBUG("success.");
170     return HDF_SUCCESS;
171 }
172 
DspPowerEnable(void)173 static int DspPowerEnable(void)
174 {
175     return HDF_SUCCESS;
176 }
177 
DspGpioPinInit(void)178 static int DspGpioPinInit(void)
179 {
180     return HDF_SUCCESS;
181 }
182 
DspI2cPinInit(void)183 static int DspI2cPinInit(void)
184 {
185     return HDF_SUCCESS;
186 }
187 
DspI2sInit(void)188 static int DspI2sInit(void)
189 {
190     return HDF_SUCCESS;
191 }
192 
DspI2cInit(void)193 static int DspI2cInit(void)
194 {
195     return HDF_SUCCESS;
196 }
197 
198 /* not init dsp gpio */
DspSpiPinInit(void)199 static int DspSpiPinInit(void)
200 {
201     return HDF_SUCCESS;
202 }
203 
DspSpiOpen(const struct SpiDevInfo * info)204 DevHandle DspSpiOpen(const struct SpiDevInfo *info)
205 {
206     if (info == NULL) {
207         AUDIO_DRIVER_LOG_ERR("DspSpiOpen fail");
208         return NULL;
209     }
210     AUDIO_DRIVER_LOG_INFO("DspSpiOpen success");
211     return OsalMemCalloc(1);
212 }
213 
DspSpiClose(DevHandle handle)214 void DspSpiClose(DevHandle handle)
215 {
216     if (handle == NULL) {
217         AUDIO_DRIVER_LOG_ERR("DspSpiClose fail");
218         return;
219     }
220     OsalMemFree(handle);
221     AUDIO_DRIVER_LOG_DEBUG("DspSpiClose success");
222 }
DspSpiTransfer(DevHandle handle,const uint8_t * msgs,const uint32_t count)223 int32_t DspSpiTransfer(DevHandle handle, const uint8_t *msgs, const uint32_t count)
224 {
225     if (handle == NULL || msgs == NULL || count == 0) {
226         AUDIO_DRIVER_LOG_ERR("DspSpiTransfer fail");
227         return HDF_FAILURE;
228     }
229     AUDIO_DRIVER_LOG_DEBUG("DspSpiTransfer success");
230     return HDF_SUCCESS;
231 }
DspSpiRead(DevHandle handle,const uint8_t * buf,const uint32_t len)232 int32_t DspSpiRead(DevHandle handle, const uint8_t *buf, const uint32_t len)
233 {
234     if (handle == NULL || buf == NULL || len == 0) {
235         AUDIO_DRIVER_LOG_ERR("DspSpiRead fail");
236         return HDF_FAILURE;
237     }
238     AUDIO_DRIVER_LOG_DEBUG("DspSpiRead success");
239     return HDF_SUCCESS;
240 }
241 
DspSpiSetCfg(DevHandle handle,struct SpiCfg * cfg)242 int32_t DspSpiSetCfg(DevHandle handle, struct SpiCfg *cfg)
243 {
244     if (handle == NULL || cfg == NULL) {
245         AUDIO_DRIVER_LOG_ERR("DspSpiSetCfg fail");
246         return HDF_FAILURE;
247     }
248     AUDIO_DRIVER_LOG_DEBUG("DspSpiSetCfg success");
249     return HDF_SUCCESS;
250 }
DspSpiGetCfg(DevHandle handle,struct SpiCfg * cfg)251 int32_t DspSpiGetCfg(DevHandle handle, struct SpiCfg *cfg)
252 {
253     if (handle == NULL || cfg == NULL) {
254         AUDIO_DRIVER_LOG_ERR("DspSpiGetCfg fail");
255         return HDF_FAILURE;
256     }
257 
258     AUDIO_DRIVER_LOG_DEBUG("DspSpiGetCfg success");
259     return HDF_SUCCESS;
260 }
261 
DspDeviceInit(const struct DspDevice * device)262 int32_t DspDeviceInit(const struct DspDevice *device)
263 {
264     DevHandle devHandle;
265     struct SpiCfg devCfg = {
266         .maxSpeedHz = DEFAULT_SPEED,
267         .mode = SPI_CLK_POLARITY,
268         .transferMode = SPI_DMA_TRANSFER,
269         .bitsPerWord = BITS_PER_WORD_EIGHT,
270     };
271 
272     if (DspPowerEnable() != HDF_SUCCESS) {
273         AUDIO_DRIVER_LOG_ERR("DspPowerEnable: return Error!");
274         return HDF_FAILURE;
275     }
276 
277     if (DspGpioPinInit() != HDF_SUCCESS) {
278         AUDIO_DRIVER_LOG_ERR("DspGpioPinInit: return Error!");
279         return HDF_FAILURE;
280     }
281 
282     if (DspI2cPinInit() != HDF_SUCCESS) {
283         AUDIO_DRIVER_LOG_ERR("DspI2cPinInit: return Error!");
284         return HDF_FAILURE;
285     }
286 
287     if (DspSpiPinInit() == HDF_SUCCESS) {
288         devHandle = DspSpiOpen(&g_devInfo);
289         if (devHandle == NULL) {
290             AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
291             return HDF_FAILURE;
292         }
293 
294         if (DspSpiSetCfg(devHandle, &devCfg) != HDF_SUCCESS) {
295             DspSpiClose(devHandle);
296             AUDIO_DRIVER_LOG_ERR("DspDeviceCfg: spi failed!");
297             return HDF_FAILURE;
298         }
299         DspSpiClose(devHandle);
300     } else {
301         AUDIO_DRIVER_LOG_ERR("Dsp Gpio Pin: not init!");
302     }
303 
304     if (DspI2cInit() != HDF_SUCCESS) {
305         return HDF_FAILURE;
306     }
307 
308     if (DspI2sInit() != HDF_SUCCESS) {
309         return HDF_FAILURE;
310     }
311 
312     return HDF_SUCCESS;
313 }
314 
DspDeviceReadReg(const struct DspDevice * device,const void * msgs,const uint32_t len)315 int32_t DspDeviceReadReg(const struct DspDevice *device, const void *msgs, const uint32_t len)
316 {
317     int32_t ret;
318     if (msgs == NULL || len == 0) {
319         AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
320         return HDF_FAILURE;
321     }
322 
323     DevHandle devHandle = DspSpiOpen(&g_devInfo);
324     if (devHandle == NULL) {
325         AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
326         return HDF_FAILURE;
327     }
328 
329     ret = DspSpiRead(devHandle, msgs, len);
330     if (ret != HDF_SUCCESS) {
331         AUDIO_DRIVER_LOG_ERR("DspDeviceRead: spi failed!");
332         DspSpiClose(devHandle);
333         return HDF_FAILURE;
334     }
335 
336     DspSpiClose(devHandle);
337 
338     return HDF_SUCCESS;
339 }
340 
DspDeviceWriteReg(const struct DspDevice * device,const void * msgs,const uint32_t len)341 int32_t DspDeviceWriteReg(const struct DspDevice *device, const void *msgs, const uint32_t len)
342 {
343     int32_t ret;
344 
345     if (msgs == NULL || len == 0) {
346         AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
347         return HDF_FAILURE;
348     }
349 
350     DevHandle devHandle = DspSpiOpen(&g_devInfo);
351     if (devHandle == NULL) {
352         AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
353         return HDF_FAILURE;
354     }
355 
356     ret = DspSpiTransfer(devHandle, msgs, len);
357     if (ret != HDF_SUCCESS) {
358         AUDIO_DRIVER_LOG_ERR("DspDeviceRead: spi failed!");
359         DspSpiClose(devHandle);
360         return HDF_FAILURE;
361     }
362 
363     DspSpiClose(devHandle);
364 
365     return HDF_SUCCESS;
366 }
367 
DspDaiDeviceInit(struct AudioCard * card,const struct DaiDevice * device)368 int32_t DspDaiDeviceInit(struct AudioCard *card, const struct DaiDevice *device)
369 {
370     if (device == NULL || device->devDaiName == NULL) {
371         AUDIO_DRIVER_LOG_ERR("input para is nullptr.");
372         return HDF_FAILURE;
373     }
374     AUDIO_DRIVER_LOG_DEBUG("dsp Dai device name: %s\n", device->devDaiName);
375     (void)card;
376     return HDF_SUCCESS;
377 }
378 
DspDecodeAudioStream(const struct AudioCard * card,const uint8_t * buf,const struct DspDevice * device)379 int32_t DspDecodeAudioStream(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)
380 {
381     (void)card;
382     (void)buf;
383     (void)device;
384     AUDIO_DRIVER_LOG_DEBUG("decode run!!!");
385     return HDF_SUCCESS;
386 }
387 
DspEncodeAudioStream(const struct AudioCard * card,const uint8_t * buf,const struct DspDevice * device)388 int32_t DspEncodeAudioStream(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)
389 {
390     (void)card;
391     (void)buf;
392     (void)device;
393     AUDIO_DRIVER_LOG_DEBUG("encode run!!!");
394     return HDF_SUCCESS;
395 }
396 
397 
DspEqualizerActive(const struct AudioCard * card,const uint8_t * buf,const struct DspDevice * device)398 int32_t DspEqualizerActive(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)
399 {
400     (void)card;
401     (void)buf;
402     (void)device;
403     AUDIO_DRIVER_LOG_DEBUG("equalizer run!!!");
404     return HDF_SUCCESS;
405 }
406