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_if.h"
10 #include "devsvc_manager_clnt.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "osal_time.h"
14 #include "securec.h"
15 #include "i2s_core.h"
16
17 #define HDF_LOG_TAG i2s_if
18 #define HOST_NAME_LEN 32
19
I2sGetCntlrByBusNum(uint32_t num)20 static struct I2sCntlr *I2sGetCntlrByBusNum(uint32_t num)
21 {
22 int ret;
23 char *name = NULL;
24 struct I2sCntlr *cntlr = NULL;
25
26 name = (char *)OsalMemCalloc(HOST_NAME_LEN + 1);
27 if (name == NULL) {
28 return NULL;
29 }
30 ret = snprintf_s(name, HOST_NAME_LEN + 1, HOST_NAME_LEN, "HDF_PLATFORM_I2S_%u", num);
31 if (ret < 0) {
32 HDF_LOGE("I2sGetCntlrByBusNum: snprintf_s fail!");
33 OsalMemFree(name);
34 return NULL;
35 }
36 cntlr = (struct I2sCntlr *)DevSvcManagerClntGetService(name);
37 OsalMemFree(name);
38 return cntlr;
39 }
40
I2sEnable(DevHandle handle)41 void I2sEnable(DevHandle handle)
42 {
43 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
44
45 if (cntlr == NULL) {
46 HDF_LOGE("I2sEnable: cntlr is null!");
47 return;
48 }
49
50 int ret = I2sCntlrEnable(cntlr);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("I2sEnable: i2s cntlr enable fail!");
53 return;
54 }
55 }
56
I2sDisable(DevHandle handle)57 void I2sDisable(DevHandle handle)
58 {
59 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
60
61 if (cntlr == NULL) {
62 HDF_LOGE("I2sDisable: cntlr is null!");
63 return;
64 }
65
66 int ret = I2sCntlrDisable(cntlr);
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("I2sDisable: i2s cntlr disable fail!");
69 return;
70 }
71 }
72
I2sStartWrite(DevHandle handle)73 void I2sStartWrite(DevHandle handle)
74 {
75 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
76
77 if (cntlr == NULL) {
78 HDF_LOGE("I2sStartWrite: cntlr is null!");
79 return;
80 }
81
82 int ret = I2sCntlrStartWrite(cntlr);
83 if (ret != HDF_SUCCESS) {
84 HDF_LOGE("I2sStartWrite: i2s cntlr start write fail!");
85 }
86 }
87
I2sStopWrite(DevHandle handle)88 void I2sStopWrite(DevHandle handle)
89 {
90 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
91
92 if (cntlr == NULL) {
93 HDF_LOGE("I2sStopWrite: cntlr is null!");
94 return;
95 }
96
97 int ret = I2sCntlrStopWrite(cntlr);
98 if (ret != HDF_SUCCESS) {
99 HDF_LOGE("I2sStopWrite: i2s cntlr stop write fail!");
100 }
101 }
102
I2sStartRead(DevHandle handle)103 void I2sStartRead(DevHandle handle)
104 {
105 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
106
107 if (cntlr == NULL) {
108 HDF_LOGE("I2sStartRead: cntlr is null!");
109 return;
110 }
111
112 int ret = I2sCntlrStartRead(cntlr);
113 if (ret != HDF_SUCCESS) {
114 HDF_LOGE("I2sStartRead: i2s cntlr start read fail!");
115 }
116 }
117
I2sStopRead(DevHandle handle)118 void I2sStopRead(DevHandle handle)
119 {
120 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
121
122 if (cntlr == NULL) {
123 HDF_LOGE("I2sStopRead: cntlr is null");
124 return;
125 }
126
127 int ret = I2sCntlrStopRead(cntlr);
128 if (ret != HDF_SUCCESS) {
129 HDF_LOGE("I2sStopRead: i2s cntlr stop read fail!");
130 }
131 }
132
I2sWrite(DevHandle handle,uint8_t * buf,uint32_t len,uint32_t * pWlen)133 int32_t I2sWrite(DevHandle handle, uint8_t *buf, uint32_t len, uint32_t *pWlen)
134 {
135 struct I2sMsg msg = {0};
136
137 if (pWlen == NULL) {
138 HDF_LOGE("I2sWrite: pWlen is null!");
139 return HDF_FAILURE;
140 }
141 *pWlen = 0;
142 msg.wbuf = buf;
143 msg.rbuf = NULL;
144 msg.len = len;
145 msg.pRlen = pWlen;
146 do {
147 OsalMSleep(I2S_DATA_TRANSFER_PERIOD);
148 int ret = I2sCntlrTransfer((struct I2sCntlr *)handle, &msg);
149 if (ret != HDF_SUCCESS) {
150 HDF_LOGE("I2sWrite: i2s cntlr transfer fail!");
151 }
152 } while (*pWlen == 0);
153
154 return HDF_SUCCESS;
155 }
156
I2sRead(DevHandle handle,uint8_t * buf,uint32_t len,uint32_t * pRlen)157 int32_t I2sRead(DevHandle handle, uint8_t *buf, uint32_t len, uint32_t *pRlen)
158 {
159 struct I2sMsg msg = {0};
160
161 if (pRlen == NULL) {
162 HDF_LOGE("I2sRead: pRlen is null!");
163 return HDF_FAILURE;
164 }
165 *pRlen = 0;
166 msg.wbuf = NULL;
167 msg.rbuf = buf;
168 msg.len = len;
169 msg.pRlen = pRlen;
170 do {
171 OsalMSleep(I2S_DATA_TRANSFER_PERIOD);
172 int ret = I2sCntlrTransfer((struct I2sCntlr *)handle, &msg);
173 if (ret != HDF_SUCCESS) {
174 HDF_LOGE("I2sRead: i2s cntlr transfer fail!");
175 }
176 } while (*pRlen == 0);
177
178 return HDF_SUCCESS;
179 }
180
I2sOpen(int16_t number)181 DevHandle I2sOpen(int16_t number)
182 {
183 struct I2sCntlr *cntlr = NULL;
184
185 cntlr = I2sGetCntlrByBusNum(number);
186 if (cntlr == NULL) {
187 HDF_LOGE("I2sOpen: cntlr is null!");
188 return NULL;
189 }
190
191 int ret = I2sCntlrOpen(cntlr);
192 if (ret != HDF_SUCCESS) {
193 HDF_LOGE("I2sOpen: i2s cntlr open fail!");
194 return NULL;
195 }
196
197 return (DevHandle)cntlr;
198 }
199
I2sClose(DevHandle handle)200 void I2sClose(DevHandle handle)
201 {
202 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
203
204 if (cntlr == NULL) {
205 HDF_LOGE("I2sClose: cntlr is null!");
206 return;
207 }
208
209 int ret = I2sCntlrClose(cntlr);
210 if (ret != HDF_SUCCESS) {
211 HDF_LOGE("I2sClose: i2s cntlr close fail!");
212 }
213 }
214
I2sSetCfg(DevHandle handle,struct I2sCfg * cfg)215 void I2sSetCfg(DevHandle handle, struct I2sCfg *cfg)
216 {
217 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
218
219 if (cntlr == NULL || cfg == NULL) {
220 HDF_LOGE("I2sSetCfg: cntlr or cfg is null!");
221 return;
222 }
223
224 int ret = I2sCntlrSetCfg(cntlr, cfg);
225 if (ret != HDF_SUCCESS) {
226 HDF_LOGE("I2sSetCfg: i2s cntlr set cfg fail!");
227 }
228 }
I2sGetCfg(DevHandle handle,struct I2sCfg * cfg)229 void I2sGetCfg(DevHandle handle, struct I2sCfg *cfg)
230 {
231 struct I2sCntlr *cntlr = (struct I2sCntlr *)handle;
232
233 if (cntlr == NULL || cfg == NULL) {
234 HDF_LOGE("I2sGetCfg: cntlr or cfg is null!");
235 return;
236 }
237
238 int ret = I2sCntlrGetCfg(cntlr, cfg);
239 if (ret != HDF_SUCCESS) {
240 HDF_LOGE("I2sGetCfg: i2s cntlr get cfg fail!");
241 }
242 }
243