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 "i2s_core.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12
13 #define HDF_LOG_TAG i2s_core
14
I2sCntlrOpen(struct I2sCntlr * cntlr)15 int32_t I2sCntlrOpen(struct I2sCntlr *cntlr)
16 {
17 int32_t ret;
18
19 if (cntlr == NULL) {
20 HDF_LOGE("%s: invalid parameter", __func__);
21 return HDF_ERR_INVALID_PARAM;
22 }
23 if (cntlr->method == NULL || cntlr->method->Open == NULL) {
24 HDF_LOGE("%s: Open not support", __func__);
25 return HDF_ERR_NOT_SUPPORT;
26 }
27 (void)OsalMutexLock(&(cntlr->lock));
28 ret = cntlr->method->Open(cntlr);
29 (void)OsalMutexUnlock(&(cntlr->lock));
30 return ret;
31 }
32
I2sCntlrClose(struct I2sCntlr * cntlr)33 int32_t I2sCntlrClose(struct I2sCntlr *cntlr)
34 {
35 int32_t ret;
36
37 if (cntlr == NULL) {
38 HDF_LOGE("%s: invalid parameter", __func__);
39 return HDF_ERR_INVALID_PARAM;
40 }
41 if (cntlr->method == NULL || cntlr->method->Close == NULL) {
42 HDF_LOGE("%s: Open not support", __func__);
43 return HDF_ERR_NOT_SUPPORT;
44 }
45 (void)OsalMutexLock(&(cntlr->lock));
46 ret = cntlr->method->Close(cntlr);
47 (void)OsalMutexUnlock(&(cntlr->lock));
48 return ret;
49 }
50
I2sCntlrEnable(struct I2sCntlr * cntlr)51 int32_t I2sCntlrEnable(struct I2sCntlr *cntlr)
52 {
53 int32_t ret;
54
55 if (cntlr == NULL) {
56 HDF_LOGE("%s: invalid parameter", __func__);
57 return HDF_ERR_INVALID_PARAM;
58 }
59 if (cntlr->method == NULL || cntlr->method->Enable == NULL) {
60 HDF_LOGE("%s: Open not support", __func__);
61 return HDF_ERR_NOT_SUPPORT;
62 }
63 (void)OsalMutexLock(&(cntlr->lock));
64 ret = cntlr->method->Enable(cntlr);
65 (void)OsalMutexUnlock(&(cntlr->lock));
66 return ret;
67 }
68
I2sCntlrDisable(struct I2sCntlr * cntlr)69 int32_t I2sCntlrDisable(struct I2sCntlr *cntlr)
70 {
71 int32_t ret;
72
73 if (cntlr == NULL) {
74 HDF_LOGE("%s: invalid parameter", __func__);
75 return HDF_ERR_INVALID_PARAM;
76 }
77 if (cntlr->method == NULL || cntlr->method->Disable == NULL) {
78 HDF_LOGE("%s: Open not support", __func__);
79 return HDF_ERR_NOT_SUPPORT;
80 }
81 (void)OsalMutexLock(&(cntlr->lock));
82 ret = cntlr->method->Disable(cntlr);
83 (void)OsalMutexUnlock(&(cntlr->lock));
84 return ret;
85 }
86
I2sCntlrStartRead(struct I2sCntlr * cntlr)87 int32_t I2sCntlrStartRead(struct I2sCntlr *cntlr)
88 {
89 int32_t ret;
90
91 if (cntlr == NULL) {
92 HDF_LOGE("%s: invalid parameter", __func__);
93 return HDF_ERR_INVALID_PARAM;
94 }
95 if (cntlr->method == NULL || cntlr->method->StartRead == NULL) {
96 HDF_LOGE("%s: Open not support", __func__);
97 return HDF_ERR_NOT_SUPPORT;
98 }
99 (void)OsalMutexLock(&(cntlr->lock));
100 ret = cntlr->method->StartRead(cntlr);
101 (void)OsalMutexUnlock(&(cntlr->lock));
102 return ret;
103 }
104
105
I2sCntlrStopRead(struct I2sCntlr * cntlr)106 int32_t I2sCntlrStopRead(struct I2sCntlr *cntlr)
107 {
108 int32_t ret;
109
110 if (cntlr == NULL) {
111 HDF_LOGE("%s: invalid parameter", __func__);
112 return HDF_ERR_INVALID_PARAM;
113 }
114 if (cntlr->method == NULL || cntlr->method->StopRead == NULL) {
115 HDF_LOGE("%s: Open not support", __func__);
116 return HDF_ERR_NOT_SUPPORT;
117 }
118 (void)OsalMutexLock(&(cntlr->lock));
119 ret = cntlr->method->StopRead(cntlr);
120 (void)OsalMutexUnlock(&(cntlr->lock));
121 return ret;
122 }
123
I2sCntlrStartWrite(struct I2sCntlr * cntlr)124 int32_t I2sCntlrStartWrite(struct I2sCntlr *cntlr)
125 {
126 int32_t ret;
127
128 if (cntlr == NULL) {
129 HDF_LOGE("%s: invalid parameter", __func__);
130 return HDF_ERR_INVALID_PARAM;
131 }
132 if (cntlr->method == NULL || cntlr->method->StartWrite == NULL) {
133 HDF_LOGE("%s: Open not support", __func__);
134 return HDF_ERR_NOT_SUPPORT;
135 }
136 (void)OsalMutexLock(&(cntlr->lock));
137 ret = cntlr->method->StartWrite(cntlr);
138 (void)OsalMutexUnlock(&(cntlr->lock));
139 return ret;
140 }
141
I2sCntlrStopWrite(struct I2sCntlr * cntlr)142 int32_t I2sCntlrStopWrite(struct I2sCntlr *cntlr)
143 {
144 int32_t ret;
145
146 if (cntlr == NULL) {
147 HDF_LOGE("%s: invalid parameter", __func__);
148 return HDF_ERR_INVALID_PARAM;
149 }
150 if (cntlr->method == NULL || cntlr->method->StopWrite == NULL) {
151 HDF_LOGE("%s: Open not support", __func__);
152 return HDF_ERR_NOT_SUPPORT;
153 }
154 (void)OsalMutexLock(&(cntlr->lock));
155 ret = cntlr->method->StopWrite(cntlr);
156 (void)OsalMutexUnlock(&(cntlr->lock));
157 return ret;
158 }
159
I2sCntlrSetCfg(struct I2sCntlr * cntlr,struct I2sCfg * cfg)160 int32_t I2sCntlrSetCfg(struct I2sCntlr *cntlr, struct I2sCfg *cfg)
161 {
162 int32_t ret;
163
164 if (cntlr == NULL) {
165 HDF_LOGE("%s: invalid parameter", __func__);
166 return HDF_ERR_INVALID_PARAM;
167 }
168
169 if (cntlr->method == NULL || cntlr->method->SetCfg == NULL) {
170 HDF_LOGE("%s: not support", __func__);
171 return HDF_ERR_NOT_SUPPORT;
172 }
173
174 (void)OsalMutexLock(&(cntlr->lock));
175 ret = cntlr->method->SetCfg(cntlr, cfg);
176 (void)OsalMutexUnlock(&(cntlr->lock));
177 return ret;
178 }
179
I2sCntlrGetCfg(struct I2sCntlr * cntlr,struct I2sCfg * cfg)180 int32_t I2sCntlrGetCfg(struct I2sCntlr *cntlr, struct I2sCfg *cfg)
181 {
182 int32_t ret;
183
184 if (cntlr == NULL) {
185 HDF_LOGE("%s: invalid parameter", __func__);
186 return HDF_ERR_INVALID_PARAM;
187 }
188
189 if (cntlr->method == NULL || cntlr->method->GetCfg == NULL) {
190 HDF_LOGE("%s: not support", __func__);
191 return HDF_ERR_NOT_SUPPORT;
192 }
193
194 (void)OsalMutexLock(&(cntlr->lock));
195 ret = cntlr->method->GetCfg(cntlr, cfg);
196 (void)OsalMutexUnlock(&(cntlr->lock));
197 return ret;
198 }
199
I2sCntlrTransfer(struct I2sCntlr * cntlr,struct I2sMsg * msg)200 int32_t I2sCntlrTransfer(struct I2sCntlr *cntlr, struct I2sMsg *msg)
201 {
202 int32_t ret;
203
204 if (cntlr == NULL || msg == NULL) {
205 HDF_LOGE("%s: invalid parameter", __func__);
206 return HDF_ERR_INVALID_PARAM;
207 }
208 if (cntlr->method == NULL || cntlr->method->Transfer == NULL) {
209 HDF_LOGE("%s: transfer not support", __func__);
210 return HDF_ERR_NOT_SUPPORT;
211 }
212
213 (void)OsalMutexLock(&(cntlr->lock));
214 ret = cntlr->method->Transfer(cntlr, msg);
215 (void)OsalMutexUnlock(&(cntlr->lock));
216 return ret;
217 }
218
I2sCntlrCreate(struct HdfDeviceObject * device)219 struct I2sCntlr *I2sCntlrCreate(struct HdfDeviceObject *device)
220 {
221 struct I2sCntlr *cntlr = NULL;
222
223 if (device == NULL) {
224 HDF_LOGE("%s: invalid parameter", __func__);
225 return NULL;
226 }
227
228 cntlr = (struct I2sCntlr *)OsalMemCalloc(sizeof(*cntlr));
229 if (cntlr == NULL) {
230 HDF_LOGE("%s: OsalMemCalloc error", __func__);
231 return NULL;
232 }
233 cntlr->device = device;
234 device->service = &(cntlr->service);
235 (void)OsalMutexInit(&cntlr->lock);
236 cntlr->priv = NULL;
237 cntlr->method = NULL;
238 return cntlr;
239 }
240
I2sCntlrDestroy(struct I2sCntlr * cntlr)241 void I2sCntlrDestroy(struct I2sCntlr *cntlr)
242 {
243 if (cntlr == NULL) {
244 HDF_LOGE("%s: invalid parameter", __func__);
245 return;
246 }
247
248 (void)OsalMutexDestroy(&(cntlr->lock));
249 cntlr->device = NULL;
250 cntlr->method = NULL;
251 cntlr->priv = NULL;
252 OsalMemFree(cntlr);
253 }
254