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 "hi3516_dai_ops.h"
20 #include "audio_core.h"
21 #include "audio_dai_base.h"
22 #include "audio_driver_log.h"
23 #include "osal_io.h"
24
25 #define HDF_LOG_TAG HDF_AUDIO_DRIVER
26
27 struct AudioDaiOps g_hi3516DaiDeviceOps = {
28 .HwParams = Hi3516DaiHwParams,
29 .Trigger = Hi3516DaiTrigger,
30 .Startup = Hi3516DaiStartup,
31 };
32
33 struct DaiData g_hi3516DaiData = {
34 .DaiInit = Hi3516DaiDeviceInit,
35 .Read = DaiDeviceReadReg,
36 .Write = DaiDeviceWriteReg,
37 .ops = &g_hi3516DaiDeviceOps,
38 };
39
40 /* HdfDriverEntry implementations */
DaiDriverBind(struct HdfDeviceObject * device)41 static int32_t DaiDriverBind(struct HdfDeviceObject *device)
42 {
43 if (device == NULL) {
44 AUDIO_DRIVER_LOG_ERR("input para is NULL.");
45 return HDF_FAILURE;
46 }
47
48 struct DaiHost *daiHost = (struct DaiHost *)OsalMemCalloc(sizeof(*daiHost));
49 if (daiHost == NULL) {
50 AUDIO_DRIVER_LOG_ERR("malloc host fail!");
51 return HDF_FAILURE;
52 }
53
54 daiHost->device = device;
55 device->service = &daiHost->service;
56 g_hi3516DaiData.daiInitFlag = false;
57
58 AUDIO_DRIVER_LOG_DEBUG("success!");
59 return HDF_SUCCESS;
60 }
61
DaiGetServiceName(const struct HdfDeviceObject * device)62 static int32_t DaiGetServiceName(const struct HdfDeviceObject *device)
63 {
64 if (device == NULL) {
65 AUDIO_DRIVER_LOG_ERR("input para is nullptr.");
66 return HDF_FAILURE;
67 }
68
69 const struct DeviceResourceNode *node = device->property;
70 if (node == NULL) {
71 AUDIO_DRIVER_LOG_ERR("drs node is nullptr.");
72 return HDF_FAILURE;
73 }
74 struct DeviceResourceIface *drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
75 if (drsOps == NULL || drsOps->GetString == NULL) {
76 AUDIO_DRIVER_LOG_ERR("invalid drs ops fail!");
77 return HDF_FAILURE;
78 }
79
80 int32_t ret = drsOps->GetString(node, "serviceName", &g_hi3516DaiData.drvDaiName, 0);
81 if (ret != HDF_SUCCESS) {
82 AUDIO_DRIVER_LOG_ERR("read serviceName fail!");
83 return ret;
84 }
85
86 return HDF_SUCCESS;
87 }
88
DaiDriverInit(struct HdfDeviceObject * device)89 static int32_t DaiDriverInit(struct HdfDeviceObject *device)
90 {
91 if (device == NULL) {
92 AUDIO_DRIVER_LOG_ERR("device is nullptr.");
93 return HDF_ERR_INVALID_OBJECT;
94 }
95
96 if (DaiGetConfigInfo(device, &g_hi3516DaiData) != HDF_SUCCESS) {
97 AUDIO_DRIVER_LOG_ERR("get dai data fail.");
98 return HDF_FAILURE;
99 }
100
101 if (DaiGetServiceName(device) != HDF_SUCCESS) {
102 AUDIO_DRIVER_LOG_ERR("get service name fail.");
103 return HDF_FAILURE;
104 }
105
106 OsalMutexInit(&g_hi3516DaiData.mutex);
107
108 int32_t ret = AudioSocRegisterDai(device, &g_hi3516DaiData);
109 if (ret != HDF_SUCCESS) {
110 AUDIO_DRIVER_LOG_ERR("register dai fail.");
111 return ret;
112 }
113
114 AUDIO_DRIVER_LOG_DEBUG("success.\n");
115 return HDF_SUCCESS;
116 }
117
DaiDriverRelease(struct HdfDeviceObject * device)118 static void DaiDriverRelease(struct HdfDeviceObject *device)
119 {
120 if (device == NULL) {
121 AUDIO_DRIVER_LOG_ERR("device is NULL");
122 return;
123 }
124
125 OsalMutexDestroy(&g_hi3516DaiData.mutex);
126
127 struct DaiHost *daiHost = (struct DaiHost *)device->service;
128 if (daiHost == NULL) {
129 AUDIO_DRIVER_LOG_ERR("daiHost is NULL");
130 return;
131 }
132 OsalMemFree(daiHost);
133 }
134
135 /* HdfDriverEntry definitions */
136 struct HdfDriverEntry g_daiDriverEntry = {
137 .moduleVersion = 1,
138 .moduleName = "DAI_HI3516",
139 .Bind = DaiDriverBind,
140 .Init = DaiDriverInit,
141 .Release = DaiDriverRelease,
142 };
143 HDF_INIT(g_daiDriverEntry);
144