1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 HDF_AUDIO_DRIVER
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("DspDaiHwParams: channels = %d, rate = %d, periodSize = %d, \
170 periodCount = %d, format = %d, cardServiceName = %s \n",
171 param->channels, param->rate, param->periodSize,
172 param->periodCount, (uint32_t)param->format, param->cardServiceName);
173 AUDIO_DRIVER_LOG_DEBUG("success.");
174 return HDF_SUCCESS;
175 }
176
DspPowerEnable(void)177 static int DspPowerEnable(void)
178 {
179 return HDF_SUCCESS;
180 }
181
DspGpioPinInit(void)182 static int DspGpioPinInit(void)
183 {
184 return HDF_SUCCESS;
185 }
186
DspI2cPinInit(void)187 static int DspI2cPinInit(void)
188 {
189 return HDF_SUCCESS;
190 }
191
DspI2sInit(void)192 static int DspI2sInit(void)
193 {
194 return HDF_SUCCESS;
195 }
196
DspI2cInit(void)197 static int DspI2cInit(void)
198 {
199 return HDF_SUCCESS;
200 }
201
202 /* not init dsp gpio */
DspSpiPinInit(void)203 static int DspSpiPinInit(void)
204 {
205 return HDF_SUCCESS;
206 }
207
DspSpiOpen(const struct SpiDevInfo *info)208 static DevHandle DspSpiOpen(const struct SpiDevInfo *info)
209 {
210 if (info == NULL) {
211 AUDIO_DRIVER_LOG_ERR("DspSpiOpen fail");
212 return NULL;
213 }
214 AUDIO_DRIVER_LOG_INFO("DspSpiOpen success");
215 return OsalMemCalloc(1);
216 }
217
DspSpiClose(DevHandle handle)218 static void DspSpiClose(DevHandle handle)
219 {
220 if (handle == NULL) {
221 AUDIO_DRIVER_LOG_ERR("DspSpiClose fail");
222 return;
223 }
224 OsalMemFree(handle);
225 AUDIO_DRIVER_LOG_DEBUG("DspSpiClose success");
226 }
DspSpiTransfer(DevHandle handle, const uint8_t *msgs, const uint32_t count)227 static int32_t DspSpiTransfer(DevHandle handle, const uint8_t *msgs, const uint32_t count)
228 {
229 if (handle == NULL || msgs == NULL || count == 0) {
230 AUDIO_DRIVER_LOG_ERR("DspSpiTransfer fail");
231 return HDF_FAILURE;
232 }
233 AUDIO_DRIVER_LOG_DEBUG("DspSpiTransfer success");
234 return HDF_SUCCESS;
235 }
DspSpiRead(DevHandle handle, const uint8_t *buf, const uint32_t len)236 static int32_t DspSpiRead(DevHandle handle, const uint8_t *buf, const uint32_t len)
237 {
238 if (handle == NULL || buf == NULL || len == 0) {
239 AUDIO_DRIVER_LOG_ERR("DspSpiRead fail");
240 return HDF_FAILURE;
241 }
242 AUDIO_DRIVER_LOG_DEBUG("DspSpiRead success");
243 return HDF_SUCCESS;
244 }
245
DspSpiSetCfg(DevHandle handle, struct SpiCfg *cfg)246 static int32_t DspSpiSetCfg(DevHandle handle, struct SpiCfg *cfg)
247 {
248 if (handle == NULL || cfg == NULL) {
249 AUDIO_DRIVER_LOG_ERR("DspSpiSetCfg fail");
250 return HDF_FAILURE;
251 }
252 AUDIO_DRIVER_LOG_DEBUG("DspSpiSetCfg success");
253 return HDF_SUCCESS;
254 }
DspSpiGetCfg(DevHandle handle, struct SpiCfg *cfg)255 static int32_t DspSpiGetCfg(DevHandle handle, struct SpiCfg *cfg)
256 {
257 if (handle == NULL || cfg == NULL) {
258 AUDIO_DRIVER_LOG_ERR("DspSpiGetCfg fail");
259 return HDF_FAILURE;
260 }
261
262 AUDIO_DRIVER_LOG_DEBUG("DspSpiGetCfg success");
263 return HDF_SUCCESS;
264 }
265
DspDeviceInit(const struct DspDevice *device)266 int32_t DspDeviceInit(const struct DspDevice *device)
267 {
268 DevHandle devHandle;
269 struct SpiCfg devCfg = {
270 .maxSpeedHz = DEFAULT_SPEED,
271 .mode = SPI_CLK_POLARITY,
272 .transferMode = SPI_DMA_TRANSFER,
273 .bitsPerWord = BITS_PER_WORD_EIGHT,
274 };
275
276 if (DspPowerEnable() != HDF_SUCCESS) {
277 AUDIO_DRIVER_LOG_ERR("DspPowerEnable: return Error!");
278 return HDF_FAILURE;
279 }
280
281 if (DspGpioPinInit() != HDF_SUCCESS) {
282 AUDIO_DRIVER_LOG_ERR("DspGpioPinInit: return Error!");
283 return HDF_FAILURE;
284 }
285
286 if (DspI2cPinInit() != HDF_SUCCESS) {
287 AUDIO_DRIVER_LOG_ERR("DspI2cPinInit: return Error!");
288 return HDF_FAILURE;
289 }
290
291 if (DspSpiPinInit() == HDF_SUCCESS) {
292 devHandle = DspSpiOpen(&g_devInfo);
293 if (devHandle == NULL) {
294 AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
295 return HDF_FAILURE;
296 }
297
298 if (DspSpiSetCfg(devHandle, &devCfg) != HDF_SUCCESS) {
299 DspSpiClose(devHandle);
300 AUDIO_DRIVER_LOG_ERR("DspDeviceCfg: spi failed!");
301 return HDF_FAILURE;
302 }
303 DspSpiClose(devHandle);
304 } else {
305 AUDIO_DRIVER_LOG_ERR("Dsp Gpio Pin: not init!");
306 }
307
308 if (DspI2cInit() != HDF_SUCCESS) {
309 return HDF_FAILURE;
310 }
311
312 if (DspI2sInit() != HDF_SUCCESS) {
313 return HDF_FAILURE;
314 }
315
316 return HDF_SUCCESS;
317 }
318
DspDeviceReadReg(const struct DspDevice *device, const void *msgs, const uint32_t len)319 int32_t DspDeviceReadReg(const struct DspDevice *device, const void *msgs, const uint32_t len)
320 {
321 int32_t ret;
322 if (msgs == NULL || len == 0) {
323 AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
324 return HDF_FAILURE;
325 }
326
327 DevHandle devHandle = DspSpiOpen(&g_devInfo);
328 if (devHandle == NULL) {
329 AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
330 return HDF_FAILURE;
331 }
332
333 ret = DspSpiRead(devHandle, msgs, len);
334 if (ret != HDF_SUCCESS) {
335 AUDIO_DRIVER_LOG_ERR("DspDeviceRead: spi failed!");
336 DspSpiClose(devHandle);
337 return HDF_FAILURE;
338 }
339
340 DspSpiClose(devHandle);
341
342 return HDF_SUCCESS;
343 }
344
DspDeviceWriteReg(const struct DspDevice *device, const void *msgs, const uint32_t len)345 int32_t DspDeviceWriteReg(const struct DspDevice *device, const void *msgs, const uint32_t len)
346 {
347 int32_t ret;
348
349 if (msgs == NULL || len == 0) {
350 AUDIO_DRIVER_LOG_ERR("input param is nullptr.");
351 return HDF_FAILURE;
352 }
353
354 DevHandle devHandle = DspSpiOpen(&g_devInfo);
355 if (devHandle == NULL) {
356 AUDIO_DRIVER_LOG_ERR("DspDeviceOpen: Spi failed!");
357 return HDF_FAILURE;
358 }
359
360 ret = DspSpiTransfer(devHandle, msgs, len);
361 if (ret != HDF_SUCCESS) {
362 AUDIO_DRIVER_LOG_ERR("DspDeviceRead: spi failed!");
363 DspSpiClose(devHandle);
364 return HDF_FAILURE;
365 }
366
367 DspSpiClose(devHandle);
368
369 return HDF_SUCCESS;
370 }
371
DspDaiDeviceInit(struct AudioCard *card, const struct DaiDevice *device)372 int32_t DspDaiDeviceInit(struct AudioCard *card, const struct DaiDevice *device)
373 {
374 if (device == NULL || device->devDaiName == NULL) {
375 AUDIO_DRIVER_LOG_ERR("input para is nullptr.");
376 return HDF_FAILURE;
377 }
378 AUDIO_DRIVER_LOG_DEBUG("dsp Dai device name: %s\n", device->devDaiName);
379 (void)card;
380 return HDF_SUCCESS;
381 }
382
DspDecodeAudioStream(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)383 int32_t DspDecodeAudioStream(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)
384 {
385 (void)card;
386 (void)buf;
387 (void)device;
388 AUDIO_DRIVER_LOG_DEBUG("decode run!!!");
389 return HDF_SUCCESS;
390 }
391
DspEncodeAudioStream(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)392 int32_t DspEncodeAudioStream(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("encode run!!!");
398 return HDF_SUCCESS;
399 }
400
401
DspEqualizerActive(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)402 int32_t DspEqualizerActive(const struct AudioCard *card, const uint8_t *buf, const struct DspDevice *device)
403 {
404 (void)card;
405 (void)buf;
406 (void)device;
407 AUDIO_DRIVER_LOG_DEBUG("equalizer run!!!");
408 return HDF_SUCCESS;
409 }
410