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