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 
9 #include <linux/slab.h>
10 #include "gpio_if.h"
11 #include "audio_core.h"
12 #include "audio_platform_base.h"
13 #include "rk3588_dma_ops.h"
14 #include "osal_io.h"
15 #include "osal_mem.h"
16 #include "audio_driver_log.h"
17 
18 #define HDF_LOG_TAG rk3588_platform_adapter
19 
20 struct AudioDmaOps g_dmaDeviceOps = {
21     .DmaBufAlloc = Rk3588DmaBufAlloc,
22     .DmaBufFree = Rk3588DmaBufFree,
23     .DmaRequestChannel = Rk3588DmaRequestChannel,
24     .DmaConfigChannel = Rk3588DmaConfigChannel,
25     .DmaPrep = Rk3588DmaPrep,
26     .DmaSubmit = Rk3588DmaSubmit,
27     .DmaPending = Rk3588DmaPending,
28     .DmaPause = Rk3588DmaPause,
29     .DmaResume = Rk3588DmaResume,
30     .DmaPointer = Rk3588PcmPointer,
31 };
32 
33 struct PlatformData g_platformData = {
34     .PlatformInit = AudioDmaDeviceInit,
35     .ops = &g_dmaDeviceOps,
36 };
37 
38 /* HdfDriverEntry implementations */
PlatformDriverBind(struct HdfDeviceObject *device)39 static int32_t PlatformDriverBind(struct HdfDeviceObject *device)
40 {
41     struct PlatformHost *platformHost = NULL;
42 
43     if (device == NULL) {
44         AUDIO_DEVICE_LOG_ERR("input para is NULL.");
45         return HDF_FAILURE;
46     }
47 
48     platformHost = (struct PlatformHost *)OsalMemCalloc(sizeof(*platformHost));
49     if (platformHost == NULL) {
50         AUDIO_DEVICE_LOG_ERR("malloc host fail!");
51         return HDF_FAILURE;
52     }
53 
54     platformHost->device = device;
55     device->service = &platformHost->service;
56 
57     AUDIO_DEVICE_LOG_DEBUG("success!");
58     return HDF_SUCCESS;
59 }
60 
PlatformGetServiceName(const struct HdfDeviceObject *device)61 static int32_t PlatformGetServiceName(const struct HdfDeviceObject *device)
62 {
63     const struct DeviceResourceNode *node = NULL;
64     struct DeviceResourceIface *drsOps = NULL;
65     int32_t ret;
66 
67     if (device == NULL) {
68         AUDIO_DEVICE_LOG_ERR("para is NULL.");
69         return HDF_FAILURE;
70     }
71 
72     node = device->property;
73     if (node == NULL) {
74         AUDIO_DEVICE_LOG_ERR("node is NULL.");
75         return HDF_FAILURE;
76     }
77 
78     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
79     if (drsOps == NULL || drsOps->GetString == NULL) {
80         AUDIO_DEVICE_LOG_ERR("get drsops object instance fail!");
81         return HDF_FAILURE;
82     }
83 
84     ret = drsOps->GetString(node, "serviceName", &g_platformData.drvPlatformName, 0);
85     if (ret != HDF_SUCCESS) {
86         AUDIO_DEVICE_LOG_ERR("read serviceName fail!");
87         return ret;
88     }
89     AUDIO_DEVICE_LOG_DEBUG("success!");
90 
91     return HDF_SUCCESS;
92 }
93 
PlatformDriverInit(struct HdfDeviceObject *device)94 static int32_t PlatformDriverInit(struct HdfDeviceObject *device)
95 {
96     int32_t ret;
97 
98     if (device == NULL) {
99         AUDIO_DEVICE_LOG_ERR("device is NULL.");
100         return HDF_ERR_INVALID_OBJECT;
101     }
102 
103     ret = PlatformGetServiceName(device);
104     if (ret !=  HDF_SUCCESS) {
105         AUDIO_DEVICE_LOG_ERR("get service name fail.");
106         return ret;
107     }
108 
109     OsalMutexInit(&g_platformData.renderBufInfo.buffMutex);
110     OsalMutexInit(&g_platformData.captureBufInfo.buffMutex);
111     ret = AudioSocRegisterPlatform(device, &g_platformData);
112     if (ret !=  HDF_SUCCESS) {
113         AUDIO_DEVICE_LOG_ERR("register dai fail.");
114         return ret;
115     }
116 
117     AUDIO_DEVICE_LOG_DEBUG("success.\n");
118     return HDF_SUCCESS;
119 }
120 
PlatformDriverRelease(struct HdfDeviceObject *device)121 static void PlatformDriverRelease(struct HdfDeviceObject *device)
122 {
123     struct PlatformHost *platformHost = NULL;
124     if (device == NULL) {
125         AUDIO_DEVICE_LOG_ERR("device is NULL");
126         return;
127     }
128 
129     platformHost = (struct PlatformHost *)device->service;
130     if (platformHost == NULL) {
131         AUDIO_DEVICE_LOG_ERR("platformHost is NULL");
132         return;
133     }
134 
135     OsalMutexDestroy(&g_platformData.renderBufInfo.buffMutex);
136     OsalMutexDestroy(&g_platformData.captureBufInfo.buffMutex);
137     OsalMemFree(platformHost);
138     AUDIO_DEVICE_LOG_DEBUG("success.\n");
139     return;
140 }
141 
142 /* HdfDriverEntry definitions */
143 struct HdfDriverEntry g_platformDriverEntry = {
144     .moduleVersion = 1,
145     .moduleName = "DMA_RK3588",
146     .Bind = PlatformDriverBind,
147     .Init = PlatformDriverInit,
148     .Release = PlatformDriverRelease,
149 };
150 HDF_INIT(g_platformDriverEntry);
151