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 "osal_io.h"
10 #include "audio_core.h"
11 #include "audio_driver_log.h"
12
13 #define HDF_LOG_TAG HDF_AUDIO_KADM
14
15 static char *g_audioDaiControlsList[AUDIO_CTRL_LIST_MAX] = {
16 "Main Playback Volume", "Main Capture Volume",
17 "Playback Mute", "Capture Mute", "Mic Left Gain",
18 "Mic Right Gain", "External Codec Enable",
19 "Internally Codec Enable", "Render Channel Mode", "Captrue Channel Mode"
20 };
21
DaiDataFromCard(const struct AudioCard * card)22 struct DaiData *DaiDataFromCard(const struct AudioCard *card)
23 {
24 if (card == NULL || card->rtd == NULL || card->rtd->cpuDai == NULL) {
25 AUDIO_DRIVER_LOG_ERR("param is null.");
26 return NULL;
27 }
28 return card->rtd->cpuDai->devData;
29 }
30
31
DaiGetConfigInfo(const struct HdfDeviceObject * device,struct DaiData * data)32 int32_t DaiGetConfigInfo(const struct HdfDeviceObject *device, struct DaiData *data)
33 {
34 if (device == NULL || data == NULL) {
35 AUDIO_DRIVER_LOG_ERR("param is null!");
36 return HDF_FAILURE;
37 }
38
39 if (data->regConfig != NULL) {
40 ADM_LOG_INFO("g_codecData regConfig has been parsed!");
41 return HDF_SUCCESS;
42 }
43
44 data->regConfig = (struct AudioRegCfgData *)OsalMemCalloc(sizeof(*(data->regConfig)));
45 if (data->regConfig == NULL) {
46 ADM_LOG_ERR("malloc AudioRegCfgData fail!");
47 return HDF_FAILURE;
48 }
49
50 if (AudioGetRegConfig(device, data->regConfig) != HDF_SUCCESS) {
51 ADM_LOG_ERR("dai GetRegConfig fail!");
52 OsalMemFree(data->regConfig);
53 data->regConfig = NULL;
54 return HDF_FAILURE;
55 }
56
57 return HDF_SUCCESS;
58 }
59
DaiCheckSampleRate(uint32_t sampleRates)60 int32_t DaiCheckSampleRate(uint32_t sampleRates)
61 {
62 bool check = (sampleRates == AUDIO_SAMPLE_RATE_8000 || sampleRates == AUDIO_SAMPLE_RATE_12000 ||
63 sampleRates == AUDIO_SAMPLE_RATE_11025 || sampleRates == AUDIO_SAMPLE_RATE_16000 ||
64 sampleRates == AUDIO_SAMPLE_RATE_22050 || sampleRates == AUDIO_SAMPLE_RATE_24000 ||
65 sampleRates == AUDIO_SAMPLE_RATE_32000 || sampleRates == AUDIO_SAMPLE_RATE_44100 ||
66 sampleRates == AUDIO_SAMPLE_RATE_48000 || sampleRates == AUDIO_SAMPLE_RATE_64000 ||
67 sampleRates == AUDIO_SAMPLE_RATE_96000);
68 if (check) {
69 return HDF_SUCCESS;
70 } else {
71 AUDIO_DRIVER_LOG_ERR("FramatToSampleRate fail: Invalid sampling rate: %u.", sampleRates);
72 return HDF_ERR_NOT_SUPPORT;
73 }
74 }
75
DaiSetConfigInfoOfControls(struct DaiData * data)76 int32_t DaiSetConfigInfoOfControls(struct DaiData *data)
77 {
78 uint16_t index;
79 struct AudioRegCfgGroupNode **regCfgGroup = NULL;
80 struct AudioMixerControl *patRegCfgItemTmp = NULL;
81 struct AudioControlConfig *item = NULL;
82
83 if (data == NULL || data->regConfig == NULL) {
84 AUDIO_DRIVER_LOG_ERR("input para is NULL.");
85 return HDF_FAILURE;
86 }
87
88 regCfgGroup = data->regConfig->audioRegParams;
89 if (regCfgGroup == NULL || regCfgGroup[AUDIO_CTRL_PATAM_GROUP] == NULL ||
90 regCfgGroup[AUDIO_CTRL_CFG_GROUP] == NULL) {
91 AUDIO_DRIVER_LOG_DEBUG("regCfgGroup is null.");
92 return HDF_SUCCESS;
93 }
94
95 patRegCfgItemTmp = regCfgGroup[AUDIO_CTRL_PATAM_GROUP]->regCfgItem;
96 item = regCfgGroup[AUDIO_CTRL_CFG_GROUP]->ctrlCfgItem;
97
98 if (patRegCfgItemTmp == NULL || item == NULL) {
99 AUDIO_DRIVER_LOG_ERR("patRegCfgItemTmp or item is NULL.");
100 return HDF_FAILURE;
101 }
102
103 data->numControls = regCfgGroup[AUDIO_CTRL_CFG_GROUP]->itemNum;
104 data->controls = (struct AudioKcontrol *)OsalMemCalloc(data->numControls * sizeof(struct AudioKcontrol));
105 if (data->controls == NULL) {
106 AUDIO_DRIVER_LOG_ERR("OsalMemCalloc failed.");
107 return HDF_FAILURE;
108 }
109
110 for (index = 0; index < data->numControls; index++) {
111 data->controls[index].iface = item[index].iface;
112 if (item[index].arrayIndex >= AUDIO_CTRL_LIST_MAX) {
113 AUDIO_DRIVER_LOG_ERR("Array super index.");
114 OsalMemFree(data->controls);
115 data->controls = NULL;
116 return HDF_FAILURE;
117 }
118 data->controls[index].name = g_audioDaiControlsList[item[index].arrayIndex];
119 data->controls[index].Info = AudioInfoCtrlOps;
120 data->controls[index].privateValue = (unsigned long)(uintptr_t)(void*)(&patRegCfgItemTmp[index]);
121 data->controls[index].Get = AudioCpuDaiGetCtrlOps;
122 data->controls[index].Set = AudioCpuDaiSetCtrlOps;
123 }
124
125 return HDF_SUCCESS;
126 }
127
DaiDeviceReadReg(const struct DaiDevice * dai,uint32_t reg,uint32_t * val)128 int32_t DaiDeviceReadReg(const struct DaiDevice *dai, uint32_t reg, uint32_t *val)
129 {
130 unsigned long virtualAddress;
131 if (dai == NULL || dai->devData == NULL || val == NULL) {
132 AUDIO_DRIVER_LOG_ERR("param val is null.");
133 return HDF_FAILURE;
134 }
135 virtualAddress = dai->devData->regVirtualAddr;
136 *val = OSAL_READL((void *)((uintptr_t)(virtualAddress + reg)));
137 return HDF_SUCCESS;
138 }
139
DaiDeviceWriteReg(const struct DaiDevice * dai,uint32_t reg,uint32_t value)140 int32_t DaiDeviceWriteReg(const struct DaiDevice *dai, uint32_t reg, uint32_t value)
141 {
142 unsigned long virtualAddress;
143 if (dai == NULL || dai->devData == NULL) {
144 AUDIO_DRIVER_LOG_ERR("param val is null.");
145 return HDF_FAILURE;
146 }
147 virtualAddress = dai->devData->regVirtualAddr;
148 OSAL_WRITEL(value, (void *)((uintptr_t)(virtualAddress + reg)));
149 return HDF_SUCCESS;
150 }
151