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