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 "audio_driver_log.h"
10 #include "audio_parse.h"
11 #include "audio_platform_if.h"
12
13 #define HDF_LOG_TAG HDF_AUDIO_KADM
14 #define DMA_TRANSFER_MAX_COUNT 12 // Support 96000 ~ 8000 sampling rate
15
AudioDmaBufAlloc(struct PlatformData * data,enum AudioStreamType streamType)16 int32_t AudioDmaBufAlloc(struct PlatformData *data, enum AudioStreamType streamType)
17 {
18 if (data != NULL && data->ops != NULL && data->ops->DmaBufAlloc != NULL) {
19 return data->ops->DmaBufAlloc(data, streamType);
20 }
21 return HDF_FAILURE;
22 }
23
AudioDmaBufFree(struct PlatformData * data,enum AudioStreamType streamType)24 int32_t AudioDmaBufFree(struct PlatformData *data, enum AudioStreamType streamType)
25 {
26 if (data != NULL && data->ops != NULL && data->ops->DmaBufFree != NULL) {
27 return data->ops->DmaBufFree(data, streamType);
28 }
29 return HDF_FAILURE;
30 }
31
AudioDmaRequestChannel(struct PlatformData * data,enum AudioStreamType streamType)32 int32_t AudioDmaRequestChannel(struct PlatformData *data, enum AudioStreamType streamType)
33 {
34 if (data != NULL && data->ops != NULL && data->ops->DmaRequestChannel != NULL) {
35 return data->ops->DmaRequestChannel(data, streamType);
36 }
37 return HDF_FAILURE;
38 }
39
AudioDmaConfigChannel(struct PlatformData * data,enum AudioStreamType streamType)40 int32_t AudioDmaConfigChannel(struct PlatformData *data, enum AudioStreamType streamType)
41 {
42 if (data != NULL && data->ops != NULL && data->ops->DmaConfigChannel != NULL) {
43 return data->ops->DmaConfigChannel(data, streamType);
44 }
45 return HDF_FAILURE;
46 }
47
AudioDmaPrep(struct PlatformData * data,enum AudioStreamType streamType)48 int32_t AudioDmaPrep(struct PlatformData *data, enum AudioStreamType streamType)
49 {
50 if (data != NULL && data->ops != NULL && data->ops->DmaPrep != NULL) {
51 return data->ops->DmaPrep(data, streamType);
52 }
53 return HDF_FAILURE;
54 }
55
AudioDmaSubmit(struct PlatformData * data,enum AudioStreamType streamType)56 int32_t AudioDmaSubmit(struct PlatformData *data, enum AudioStreamType streamType)
57 {
58 if (data != NULL && data->ops != NULL && data->ops->DmaSubmit != NULL) {
59 return data->ops->DmaSubmit(data, streamType);
60 }
61 return HDF_FAILURE;
62 }
63
AudioDmaPending(struct PlatformData * data,enum AudioStreamType streamType)64 int32_t AudioDmaPending(struct PlatformData *data, enum AudioStreamType streamType)
65 {
66 if (data != NULL && data->ops != NULL && data->ops->DmaPending != NULL) {
67 return data->ops->DmaPending(data, streamType);
68 }
69 return HDF_FAILURE;
70 }
71
AudioDmaPause(struct PlatformData * data,enum AudioStreamType streamType)72 int32_t AudioDmaPause(struct PlatformData *data, enum AudioStreamType streamType)
73 {
74 if (data != NULL && data->ops != NULL && data->ops->DmaPause != NULL) {
75 return data->ops->DmaPause(data, streamType);
76 }
77 return HDF_FAILURE;
78 }
79
AudioDmaResume(struct PlatformData * data,enum AudioStreamType streamType)80 int32_t AudioDmaResume(struct PlatformData *data, enum AudioStreamType streamType)
81 {
82 if (data != NULL && data->ops != NULL && data->ops->DmaResume != NULL) {
83 return data->ops->DmaResume(data, streamType);
84 }
85 return HDF_FAILURE;
86 }
87
AudioDmaPointer(struct PlatformData * data,enum AudioStreamType streamType,uint32_t * pointer)88 int32_t AudioDmaPointer(struct PlatformData *data, enum AudioStreamType streamType, uint32_t *pointer)
89 {
90 if (data != NULL && data->ops != NULL && data->ops->DmaPointer != NULL) {
91 return data->ops->DmaPointer(data, streamType, pointer);
92 }
93 return HDF_FAILURE;
94 }
95
AudioDmaGetConfigInfo(const struct HdfDeviceObject * device,struct PlatformData * data)96 int32_t AudioDmaGetConfigInfo(const struct HdfDeviceObject *device, struct PlatformData *data)
97 {
98 if (device == NULL || data == NULL) {
99 AUDIO_DRIVER_LOG_ERR("param is null!");
100 return HDF_ERR_INVALID_PARAM;
101 }
102
103 if (data->regConfig != NULL) {
104 AUDIO_DRIVER_LOG_INFO("regConfig has been parsed!");
105 return HDF_SUCCESS;
106 }
107
108 data->regConfig = (struct AudioRegCfgData *)OsalMemCalloc(sizeof(struct AudioRegCfgData));
109 if (data->regConfig == NULL) {
110 AUDIO_DRIVER_LOG_ERR("malloc AudioRegCfgData fail!");
111 return HDF_FAILURE;
112 }
113
114 if (AudioGetRegConfig(device, data->regConfig) != HDF_SUCCESS) {
115 AUDIO_DRIVER_LOG_ERR("dai GetRegConfig fail!");
116 OsalMemFree(data->regConfig);
117 data->regConfig = NULL;
118 return HDF_FAILURE;
119 }
120
121 return HDF_SUCCESS;
122 }
123
AudioDmaTransferStatusIsNormal(struct PlatformData * data,enum AudioStreamType streamType)124 bool AudioDmaTransferStatusIsNormal(struct PlatformData *data, enum AudioStreamType streamType)
125 {
126 if (data == NULL) {
127 AUDIO_DRIVER_LOG_ERR("param is null!");
128 return false;
129 }
130
131 if (streamType == AUDIO_RENDER_STREAM) {
132 if (data->renderBufInfo.rbufOffSet == data->renderBufInfo.wbufOffSet) {
133 data->renderBufInfo.trafCompCount++;
134 if (data->renderBufInfo.trafCompCount > DMA_TRANSFER_MAX_COUNT) {
135 AUDIO_DRIVER_LOG_ERR("audio render send data to DMA too slow DMA will stop!");
136 return false;
137 }
138 } else {
139 data->renderBufInfo.rbufOffSet = data->renderBufInfo.wbufOffSet;
140 data->renderBufInfo.trafCompCount = 0;
141 }
142 } else {
143 if (data->captureBufInfo.wbufOffSet == data->captureBufInfo.rbufOffSet) {
144 data->captureBufInfo.trafCompCount++;
145 if (data->captureBufInfo.trafCompCount > DMA_TRANSFER_MAX_COUNT) {
146 AUDIO_DRIVER_LOG_ERR("audio capture retrieve data from DMA too slow DMA will stop!");
147 return false;
148 }
149 } else {
150 data->captureBufInfo.wbufOffSet = data->captureBufInfo.rbufOffSet;
151 data->captureBufInfo.trafCompCount = 0;
152 }
153 }
154
155 return true;
156 }
157