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