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 "dma.h"
30 #include "i2s.h"
31 #include "i2s_reva.h"
32 #include "i2s_regs.h"
33 #include "mxc_assert.h"
34 #include "mxc_delay.h"
35 #include "mxc_device.h"
36 #include "mxc_lock.h"
37 #include "mxc_pins.h"
38 #include "mxc_sys.h"
39 
MXC_I2S_Init(mxc_i2s_req_t * req)40 int MXC_I2S_Init(mxc_i2s_req_t *req)
41 {
42     MXC_I2S_Shutdown();
43 
44     MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
45     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_I2S);
46     MXC_GPIO_Config(&gpio_cfg_i2s);
47 
48     return MXC_I2S_RevA_Init((mxc_i2s_reva_regs_t *)MXC_I2S, req);
49 }
50 
MXC_I2S_Shutdown(void)51 int MXC_I2S_Shutdown(void)
52 {
53     MXC_I2S_RevA_Shutdown((mxc_i2s_reva_regs_t *)MXC_I2S);
54 
55     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2S);
56     MXC_SYS_Reset_Periph(MXC_SYS_RESET1_I2S);
57 
58     return E_NO_ERROR;
59 }
60 
MXC_I2S_ConfigData(mxc_i2s_req_t * req)61 int MXC_I2S_ConfigData(mxc_i2s_req_t *req)
62 {
63     return MXC_I2S_RevA_ConfigData((mxc_i2s_reva_regs_t *)MXC_I2S, req);
64 }
65 
MXC_I2S_TXEnable()66 void MXC_I2S_TXEnable()
67 {
68     MXC_I2S_RevA_TXEnable((mxc_i2s_reva_regs_t *)MXC_I2S);
69 }
70 
MXC_I2S_TXDisable()71 void MXC_I2S_TXDisable()
72 {
73     MXC_I2S_RevA_TXDisable((mxc_i2s_reva_regs_t *)MXC_I2S);
74 }
75 
MXC_I2S_RXEnable()76 void MXC_I2S_RXEnable()
77 {
78     MXC_I2S_RevA_RXEnable((mxc_i2s_reva_regs_t *)MXC_I2S);
79 }
80 
MXC_I2S_RXDisable()81 void MXC_I2S_RXDisable()
82 {
83     MXC_I2S_RevA_RXDisable((mxc_i2s_reva_regs_t *)MXC_I2S);
84 }
85 
MXC_I2S_SetRXThreshold(uint8_t threshold)86 int MXC_I2S_SetRXThreshold(uint8_t threshold)
87 {
88     return MXC_I2S_RevA_SetRXThreshold((mxc_i2s_reva_regs_t *)MXC_I2S, threshold);
89 }
90 
MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode,uint16_t clkdiv)91 int MXC_I2S_SetFrequency(mxc_i2s_ch_mode_t mode, uint16_t clkdiv)
92 {
93     return MXC_I2S_RevA_SetFrequency((mxc_i2s_reva_regs_t *)MXC_I2S, mode, clkdiv);
94 }
95 
MXC_I2S_SetSampleRate(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)96 int MXC_I2S_SetSampleRate(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
97 {
98     return MXC_I2S_RevA_SetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
99                                       ERFO_FREQ);
100 }
101 
MXC_I2S_GetSampleRate(void)102 int MXC_I2S_GetSampleRate(void)
103 {
104     return MXC_I2S_RevA_GetSampleRate((mxc_i2s_reva_regs_t *)MXC_I2S, ERFO_FREQ);
105 }
106 
MXC_I2S_CalculateClockDiv(uint32_t smpl_rate,mxc_i2s_wsize_t smpl_sz)107 int MXC_I2S_CalculateClockDiv(uint32_t smpl_rate, mxc_i2s_wsize_t smpl_sz)
108 {
109     return MXC_I2S_RevA_CalculateClockDiv((mxc_i2s_reva_regs_t *)MXC_I2S, smpl_rate, smpl_sz,
110                                           ERFO_FREQ);
111 }
112 
MXC_I2S_Flush(void)113 void MXC_I2S_Flush(void)
114 {
115     MXC_I2S_RevA_Flush((mxc_i2s_reva_regs_t *)MXC_I2S);
116 }
117 
MXC_I2S_FillTXFIFO(void * txData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)118 int MXC_I2S_FillTXFIFO(void *txData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
119 {
120     return MXC_I2S_RevA_FillTXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, txData, wordSize, len, smpl_cnt);
121 }
122 
MXC_I2S_ReadRXFIFO(void * rxData,mxc_i2s_wsize_t wordSize,int len,int smpl_cnt)123 int MXC_I2S_ReadRXFIFO(void *rxData, mxc_i2s_wsize_t wordSize, int len, int smpl_cnt)
124 {
125     return MXC_I2S_RevA_ReadRXFIFO((mxc_i2s_reva_regs_t *)MXC_I2S, rxData, wordSize, len, smpl_cnt);
126 }
127 
MXC_I2S_EnableInt(uint32_t flags)128 void MXC_I2S_EnableInt(uint32_t flags)
129 {
130     MXC_I2S_RevA_EnableInt((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
131 }
132 
MXC_I2S_DisableInt(uint32_t flags)133 void MXC_I2S_DisableInt(uint32_t flags)
134 {
135     MXC_I2S_RevA_DisableInt((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
136 }
137 
MXC_I2S_GetFlags()138 int MXC_I2S_GetFlags()
139 {
140     return MXC_I2S_RevA_GetFlags((mxc_i2s_reva_regs_t *)MXC_I2S);
141 }
142 
MXC_I2S_ClearFlags(uint32_t flags)143 void MXC_I2S_ClearFlags(uint32_t flags)
144 {
145     MXC_I2S_RevA_ClearFlags((mxc_i2s_reva_regs_t *)MXC_I2S, flags);
146 }
147 
MXC_I2S_Transaction(mxc_i2s_req_t * i2s_req)148 int MXC_I2S_Transaction(mxc_i2s_req_t *i2s_req)
149 {
150     return MXC_I2S_RevA_Transaction((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
151 }
152 
MXC_I2S_TransactionAsync(mxc_i2s_req_t * i2s_req)153 int MXC_I2S_TransactionAsync(mxc_i2s_req_t *i2s_req)
154 {
155     return MXC_I2S_RevA_TransactionAsync((mxc_i2s_reva_regs_t *)MXC_I2S, i2s_req);
156 }
157 
MXC_I2S_TXDMAConfig(void * src_addr,int len)158 int MXC_I2S_TXDMAConfig(void *src_addr, int len)
159 {
160     return MXC_I2S_RevA_TXDMAConfig((mxc_i2s_reva_regs_t *)MXC_I2S, src_addr, len);
161 }
162 
MXC_I2S_RXDMAConfig(void * dest_addr,int len)163 int MXC_I2S_RXDMAConfig(void *dest_addr, int len)
164 {
165     return MXC_I2S_RevA_RXDMAConfig((mxc_i2s_reva_regs_t *)MXC_I2S, dest_addr, len);
166 }
167 
MXC_I2S_Handler(void)168 void MXC_I2S_Handler(void)
169 {
170     MXC_I2S_RevA_Handler((mxc_i2s_reva_regs_t *)MXC_I2S);
171 }
172 
MXC_I2S_RegisterDMACallback(void (* callback)(int,int))173 void MXC_I2S_RegisterDMACallback(void (*callback)(int, int))
174 {
175     MXC_I2S_RevA_RegisterDMACallback(callback);
176 }
177 
MXC_I2S_RegisterAsyncCallback(void (* callback)(int))178 void MXC_I2S_RegisterAsyncCallback(void (*callback)(int))
179 {
180     MXC_I2S_RevA_RegisterAsyncCallback(callback);
181 }
182