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_codec_ops.h"
10 #include "audio_codec_base.h"
11 #include "audio_core.h"
12 #include "audio_driver_log.h"
13
14 #define HDF_LOG_TAG hi3516_codec_adapter
15
16 struct CodecData g_codecData = {
17 .Init = CodecDeviceInit,
18 .Read = AudioDeviceReadReg,
19 .Write = AudioDeviceWriteReg,
20 };
21
22 struct AudioDaiOps g_codecDaiDeviceOps = {
23 .Startup = CodecDaiStartup,
24 .HwParams = CodecDaiHwParams,
25 };
26
27 struct DaiData g_codecDaiData = {
28 .DaiInit = CodecDaiDeviceInit,
29 .ops = &g_codecDaiDeviceOps,
30 };
31
32 /* HdfDriverEntry implementations */
CodecDriverBind(struct HdfDeviceObject * device)33 static int32_t CodecDriverBind(struct HdfDeviceObject *device)
34 {
35 if (device == NULL) {
36 AUDIO_DRIVER_LOG_ERR("input para is NULL.");
37 return HDF_FAILURE;
38 }
39
40 struct CodecHost *codecHost = (struct CodecHost *)OsalMemCalloc(sizeof(*codecHost));
41 if (codecHost == NULL) {
42 AUDIO_DRIVER_LOG_ERR("malloc codecHost fail!");
43 return HDF_FAILURE;
44 }
45 codecHost->device = device;
46 device->service = &codecHost->service;
47
48 return HDF_SUCCESS;
49 }
50
CodecDriverInit(struct HdfDeviceObject * device)51 static int32_t CodecDriverInit(struct HdfDeviceObject *device)
52 {
53 if (device == NULL) {
54 AUDIO_DRIVER_LOG_ERR("device is NULL.");
55 return HDF_ERR_INVALID_OBJECT;
56 }
57
58 if (CodecGetConfigInfo(device, &g_codecData) != HDF_SUCCESS) {
59 return HDF_FAILURE;
60 }
61
62 if (CodecSetConfigInfo(&g_codecData, &g_codecDaiData) != HDF_SUCCESS) {
63 return HDF_FAILURE;
64 }
65
66 if (CodecGetServiceName(device, &g_codecData.drvCodecName) != HDF_SUCCESS) {
67 return HDF_FAILURE;
68 }
69
70 if (CodecGetDaiName(device, &g_codecDaiData.drvDaiName) != HDF_SUCCESS) {
71 return HDF_FAILURE;
72 }
73
74 if (AudioRegisterCodec(device, &g_codecData, &g_codecDaiData) != HDF_SUCCESS) {
75 return HDF_FAILURE;
76 }
77
78 return HDF_SUCCESS;
79 }
80
CodecDriverRelease(struct HdfDeviceObject * device)81 static void CodecDriverRelease(struct HdfDeviceObject *device)
82 {
83 if (device == NULL) {
84 AUDIO_DRIVER_LOG_ERR("device is NULL");
85 return;
86 }
87
88 if (device->priv != NULL) {
89 OsalMemFree(device->priv);
90 }
91 struct CodecHost *codecHost = (struct CodecHost *)device->service;
92 if (codecHost == NULL) {
93 HDF_LOGE("CodecDriverRelease: codecHost is NULL");
94 return;
95 }
96 OsalMemFree(codecHost);
97 }
98
99 /* HdfDriverEntry definitions */
100 struct HdfDriverEntry g_codecDriverEntry = {
101 .moduleVersion = 1,
102 .moduleName = "CODEC_HI3516",
103 .Bind = CodecDriverBind,
104 .Init = CodecDriverInit,
105 .Release = CodecDriverRelease,
106 };
107 HDF_INIT(g_codecDriverEntry);
108