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