1 /**
2  * @file    i2s.c
3  * @brief   Inter-Integrated Sound (I2S) driver implementation.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #include <stdio.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include "mxc_device.h"
30 #include "mxc_assert.h"
31 #include "mxc_lock.h"
32 #include "mxc_sys.h"
33 #include "mxc_delay.h"
34 #include "dma.h"
35 #include "i2s.h"
36 #include "i2s_reva.h"
37 #include "i2s_regs.h"
38 
MXC_I2S_Init(mxc_i2s_req_t * req)39 int MXC_I2S_Init(mxc_i2s_req_t *req)
40 {
41     MXC_I2S_Shutdown();
42 
43     // Use ERFO clock by default
44     MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
45 
46     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2S);
47     MXC_GPIO_Config(&gpio_cfg_i2s0);
48 
49     return MXC_I2S_RevA_Init((mxc_i2s_reva_regs_t *)MXC_I2S, req);
50 }
51 
MXC_I2S_Shutdown(void)52 int MXC_I2S_Shutdown(void)
53 {
54     MXC_I2S_RevA_Shutdown((mxc_i2s_reva_regs_t *)MXC_I2S);
55 
56     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2S);
57     MXC_SYS_Reset_Periph(MXC_SYS_RESET1_I2S);
58 
59     return E_NO_ERROR;
60 }
61 
MXC_I2S_ConfigData(mxc_i2s_req_t * req)62 int MXC_I2S_ConfigData(mxc_i2s_req_t *req)
63 {
64     return MXC_I2S_RevA_ConfigData((mxc_i2s_reva_regs_t *)MXC_I2S, req);
65 }
66 
MXC_I2S_TXEnable()67 void MXC_I2S_TXEnable()
68 {
69     MXC_I2S_RevA_TXEnable((mxc_i2s_reva_regs_t *)MXC_I2S);
70 }
71 
MXC_I2S_TXDisable()72 void MXC_I2S_TXDisable()
73 {
74     MXC_I2S_RevA_TXDisable((mxc_i2s_reva_regs_t *)MXC_I2S);
75 }
76 
MXC_I2S_RXEnable()77 void MXC_I2S_RXEnable()
78 {
79     MXC_I2S_RevA_RXEnable((mxc_i2s_reva_regs_t *)MXC_I2S);
80 }
81 
MXC_I2S_RXDisable()82 void MXC_I2S_RXDisable()
83 {
84     MXC_I2S_RevA_RXDisable((mxc_i2s_reva_regs_t *)MXC_I2S);
85 }
86 
MXC_I2S_SetRXThreshold(uint8_t threshold)87 int MXC_I2S_SetRXThreshold(uint8_t threshold)
88 {
89     return MXC_I2S_RevA_SetRXThreshold((mxc_i2s_reva_regs_t *)MXC_I2S, threshold);
90 }
91 
MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode,uint16_t clkdiv)92 int MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode, uint16_t clkdiv)
93 {
94     return MXC_I2S_RevA_SetFrequency((mxc_i2s_reva_regs_t *)MXC_I2S, mode, clkdiv);
95 }
96 
MXC_I2S_SetSampleRate(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)97 int MXC_I2S_SetSampleRate(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
98 {
99     return MXC_I2S_RevA_SetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
100                                       ERFO_FREQ);
101 }
102 
MXC_I2S_GetSampleRate(void)103 int MXC_I2S_GetSampleRate(void)
104 {
105     return MXC_I2S_RevA_GetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, ERFO_FREQ);
106 }
107 
MXC_I2S_CalculateClockDiv(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)108 int MXC_I2S_CalculateClockDiv(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
109 {
110     return MXC_I2S_RevA_CalculateClockDiv((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
111                                           ERFO_FREQ);
112 }
113 
MXC_I2S_Flush(void)114 void MXC_I2S_Flush(void)
115 {
116     MXC_I2S_RevA_Flush((mxc_i2s_reva_regs_t *)MXC_I2S);
117 }
118 
MXC_I2S_FillTXFIFO(void * txData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)119 int MXC_I2S_FillTXFIFO(void *txData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
120 {
121     return MXC_I2S_RevA_FillTXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, txData, wordSize, len, smpl_cnt);
122 }
123 
MXC_I2S_ReadRXFIFO(void * rxData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)124 int MXC_I2S_ReadRXFIFO(void *rxData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
125 {
126     return MXC_I2S_RevA_ReadRXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, rxData, wordSize, len, smpl_cnt);
127 }
128 
MXC_I2S_EnableInt(uint32_t flags)129 void MXC_I2S_EnableInt(uint32_t flags)
130 {
131     MXC_I2S_RevA_EnableInt((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
132 }
133 
MXC_I2S_DisableInt(uint32_t flags)134 void MXC_I2S_DisableInt(uint32_t flags)
135 {
136     MXC_I2S_RevA_DisableInt((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
137 }
138 
MXC_I2S_GetFlags()139 int MXC_I2S_GetFlags()
140 {
141     return MXC_I2S_RevA_GetFlags((mxc_i2s_reva_regs_t *)MXC_I2S);
142 }
143 
MXC_I2S_ClearFlags(uint32_t flags)144 void MXC_I2S_ClearFlags(uint32_t flags)
145 {
146     MXC_I2S_RevA_ClearFlags((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
147 }
148 
MXC_I2S_Transaction(mxc_i2s_req_t * i2s_req)149 int MXC_I2S_Transaction(mxc_i2s_req_t *i2s_req)
150 {
151     return MXC_I2S_RevA_Transaction((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
152 }
153 
MXC_I2S_TransactionAsync(mxc_i2s_req_t * i2s_req)154 int MXC_I2S_TransactionAsync(mxc_i2s_req_t *i2s_req)
155 {
156     return MXC_I2S_RevA_TransactionAsync((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
157 }
158 
MXC_I2S_TXDMAConfig(void * src_addr,int len)159 int MXC_I2S_TXDMAConfig(void *src_addr, int len)
160 {
161     return MXC_I2S_RevA_TXDMAConfig((mxc_i2s_reva_regs_t *)MXC_I2S, src_addr, len);
162 }
163 
MXC_I2S_RXDMAConfig(void * dest_addr,int len)164 int MXC_I2S_RXDMAConfig(void *dest_addr, int len)
165 {
166     return MXC_I2S_RevA_RXDMAConfig((mxc_i2s_reva_regs_t *)MXC_I2S, dest_addr, len);
167 }
168 
MXC_I2S_Handler(void)169 void MXC_I2S_Handler(void)
170 {
171     MXC_I2S_RevA_Handler((mxc_i2s_reva_regs_t *)MXC_I2S);
172 }
173 
MXC_I2S_RegisterDMACallback(void (* callback)(int,int))174 void MXC_I2S_RegisterDMACallback(void (*callback)(int, int))
175 {
176     MXC_I2S_RevA_RegisterDMACallback(callback);
177 }
178 
MXC_I2S_RegisterAsyncCallback(void (* callback)(int))179 void MXC_I2S_RegisterAsyncCallback(void (*callback)(int))
180 {
181     MXC_I2S_RevA_RegisterAsyncCallback(callback);
182 }
183