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