1 /*
2 * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
3 *
4 * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
5 * the the people's Republic of China and other countries.
6 * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
7 *
8 * DISCLAIMER
9 * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
10 * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
11 * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
12 * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
13 * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
14 * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
15 * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
19 * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
20 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
21 * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
22 * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #ifndef __SUNXI_DAUDIO_H_
33 #define __SUNXI_DAUDIO_H_
34 
35 #include <aw_common.h>
36 #include <hal_clk.h>
37 #include <hal_reset.h>
38 #include <hal_gpio.h>
39 #include <sound/snd_core.h>
40 #include <sound/snd_pcm.h>
41 #include <sound/snd_io.h>
42 
43 #define DAUDIO_NAME_LEN     (26)
44 
45 /* DAUDIO register definition */
46 #define SUNXI_DAUDIO_CTL        0x00
47 #define SUNXI_DAUDIO_FMT0       0x04
48 #define SUNXI_DAUDIO_FMT1       0x08
49 #define SUNXI_DAUDIO_INTSTA     0x0C
50 #define SUNXI_DAUDIO_RXFIFO     0x10
51 #define SUNXI_DAUDIO_FIFOCTL        0x14
52 #define SUNXI_DAUDIO_FIFOSTA        0x18
53 #define SUNXI_DAUDIO_INTCTL     0x1C
54 #define SUNXI_DAUDIO_TXFIFO     0x20
55 #define SUNXI_DAUDIO_CLKDIV     0x24
56 #define SUNXI_DAUDIO_TXCNT      0x28
57 #define SUNXI_DAUDIO_RXCNT      0x2C
58 #define SUNXI_DAUDIO_CHCFG      0x30
59 #define SUNXI_DAUDIO_TX0CHSEL       0x34
60 #define SUNXI_DAUDIO_TX1CHSEL       0x38
61 #define SUNXI_DAUDIO_TX2CHSEL       0x3C
62 #define SUNXI_DAUDIO_TX3CHSEL       0x40
63 
64 #define SUNXI_DAUDIO_TX0CHMAP0      0x44
65 #define SUNXI_DAUDIO_TX0CHMAP1      0x48
66 #define SUNXI_DAUDIO_TX1CHMAP0      0x4C
67 #define SUNXI_DAUDIO_TX1CHMAP1      0x50
68 #define SUNXI_DAUDIO_TX2CHMAP0      0x54
69 #define SUNXI_DAUDIO_TX2CHMAP1      0x58
70 #define SUNXI_DAUDIO_TX3CHMAP0      0x5C
71 #define SUNXI_DAUDIO_TX3CHMAP1      0x60
72 #define SUNXI_DAUDIO_RXCHSEL        0x64
73 #define SUNXI_DAUDIO_RXCHMAP0       0x68
74 #define SUNXI_DAUDIO_RXCHMAP1       0x6C
75 #define SUNXI_DAUDIO_RXCHMAP2       0x70
76 #define SUNXI_DAUDIO_RXCHMAP3       0x74
77 #define SUNXI_DAUDIO_DEBUG      0x78
78 #define SUNXI_DAUDIO_REV        0x7C
79 
80 #define SUNXI_DAUDIO_ASRC_MCLKCFG       0x80
81 #define SUNXI_DAUDIO_ASRC_FSOUTCFG      0x84
82 #define SUNXI_DAUDIO_ASRC_FSIN_EXTCFG   0x88
83 #define SUNXI_DAUDIO_ASRC_ASRCEN        0x8C
84 #define SUNXI_DAUDIO_ASRC_MANCFG        0x90
85 #define SUNXI_DAUDIO_ASRC_RATIOSTAT     0x94
86 #define SUNXI_DAUDIO_ASRC_FIFOSTAT      0x98
87 #define SUNXI_DAUDIO_ASRC_MBISTCFG      0x9C
88 #define SUNXI_DAUDIO_ASRC_MBISTSTA      0xA0
89 
90 /* SUNXI_DAUDIO_CTL:0x00 */
91 #define RX_SYNC_EN          21
92 #define RX_EN_MUX           20
93 #define BCLK_OUT            18
94 #define LRCK_OUT            17
95 #define LRCKR_CTL           16
96 #define SDO3_EN             11
97 #define SDO2_EN             10
98 #define SDO1_EN             9
99 #define SDO0_EN             8
100 #define MUTE_CTL            6
101 #define MODE_SEL            4
102 #define LOOP_EN             3
103 #define CTL_TXEN            2
104 #define CTL_RXEN            1
105 #define GLOBAL_EN           0
106 
107 /* SUNXI_DAUDIO_FMT0:0x04 */
108 #define SDI_SYNC_SEL            31
109 #define LRCK_WIDTH          30
110 #define LRCKR_PERIOD            20
111 #define LRCK_POLARITY           19
112 #define LRCK_PERIOD         8
113 #define BRCK_POLARITY           7
114 #define DAUDIO_SAMPLE_RESOLUTION    4
115 #define EDGE_TRANSFER           3
116 #define SLOT_WIDTH          0
117 
118 /* SUNXI_DAUDIO_FMT1:0x08 */
119 #define RX_MLS              7
120 #define TX_MLS              6
121 #define SEXT                4
122 #define RX_PDM              2
123 #define TX_PDM              0
124 
125 /* SUNXI_DAUDIO_INTSTA:0x0C */
126 #define TXU_INT             6
127 #define TXO_INT             5
128 #define TXE_INT             4
129 #define RXU_INT             2
130 #define RXO_INT             1
131 #define RXA_INT             0
132 
133 /* SUNXI_DAUDIO_FIFOCTL:0x14 */
134 #define HUB_EN              31
135 #define FIFO_CTL_FTX            25
136 #define FIFO_CTL_FRX            24
137 #define TXTL                12
138 #define RXTL                4
139 #define TXIM                2
140 #define RXOM                0
141 
142 /* SUNXI_DAUDIO_FIFOSTA:0x18 */
143 #define FIFO_TXE            28
144 #define FIFO_TX_CNT         16
145 #define FIFO_RXA            8
146 #define FIFO_RX_CNT         0
147 
148 /* SUNXI_DAUDIO_INTCTL:0x1C */
149 #define TXDRQEN             7
150 #define TXUI_EN             6
151 #define TXOI_EN             5
152 #define TXEI_EN             4
153 #define RXDRQEN             3
154 #define RXUIEN              2
155 #define RXOIEN              1
156 #define RXAIEN              0
157 
158 /* SUNXI_DAUDIO_CLKDIV:0x24 */
159 #define MCLKOUT_EN          8
160 #define BCLK_DIV            4
161 #define MCLK_DIV            0
162 
163 /* SUNXI_DAUDIO_CHCFG:0x30 */
164 #define TX_SLOT_HIZ         9
165 #define TX_STATE            8
166 #define RX_SLOT_NUM         4
167 #define TX_SLOT_NUM         0
168 
169 /* SUNXI_DAUDIO_TXnCHSEL:0X34+n*0x04 */
170 #define TX_OFFSET           20
171 #define TX_CHSEL            16
172 #define TX_CHEN             0
173 
174 /* SUNXI_DAUDIO_RXCHSEL */
175 #define RX_OFFSET           20
176 #define RX_CHSEL            16
177 
178 /* sun8iw10 CHMAP default setting */
179 #define SUNXI_DEFAULT_CHMAP0        0xFEDCBA98
180 #define SUNXI_DEFAULT_CHMAP1        0x76543210
181 
182 /* RXCHMAP default setting */
183 #define SUNXI_DEFAULT_CHMAP     0x76543210
184 
185 /* Shift & Mask define */
186 
187 /* SUNXI_DAUDIO_CTL:0x00 */
188 #define SUNXI_DAUDIO_MODE_CTL_MASK      3
189 #define SUNXI_DAUDIO_MODE_CTL_PCM       0
190 #define SUNXI_DAUDIO_MODE_CTL_I2S       1
191 #define SUNXI_DAUDIO_MODE_CTL_LEFT      1
192 #define SUNXI_DAUDIO_MODE_CTL_RIGHT     2
193 #define SUNXI_DAUDIO_MODE_CTL_REVD      3
194 /* combine LRCK_CLK & BCLK setting */
195 #define SUNXI_DAUDIO_LRCK_OUT_MASK      3
196 #define SUNXI_DAUDIO_LRCK_OUT_DISABLE       0
197 #define SUNXI_DAUDIO_LRCK_OUT_ENABLE        3
198 
199 /* SUNXI_DAUDIO_FMT0 */
200 #define SUNXI_DAUDIO_LRCK_PERIOD_MASK       0x3FF
201 #define SUNXI_DAUDIO_SLOT_WIDTH_MASK        7
202 /* Left Blank */
203 #define SUNXI_DAUDIO_SR_MASK            7
204 #define SUNXI_DAUDIO_SR_16BIT           3
205 #define SUNXI_DAUDIO_SR_24BIT           5
206 #define SUNXI_DAUDIO_SR_32BIT           7
207 
208 #define SUNXI_DAUDIO_LRCK_POLARITY_NOR      0
209 #define SUNXI_DAUDIO_LRCK_POLARITY_INV      1
210 #define SUNXI_DAUDIO_BCLK_POLARITY_NOR      0
211 #define SUNXI_DAUDIO_BCLK_POLARITY_INV      1
212 
213 /* SUNXI_DAUDIO_FMT1 */
214 #define SUNXI_DAUDIO_FMT1_DEF           0x30
215 
216 /* SUNXI_DAUDIO_FIFOCTL */
217 #define SUNXI_DAUDIO_TXIM_MASK          1
218 #define SUNXI_DAUDIO_TXIM_VALID_MSB     0
219 #define SUNXI_DAUDIO_TXIM_VALID_LSB     1
220 /* Left Blank */
221 #define SUNXI_DAUDIO_RXOM_MASK          3
222 /* Expanding 0 at LSB of RX_FIFO */
223 #define SUNXI_DAUDIO_RXOM_EXP0          0
224 /* Expanding sample bit at MSB of RX_FIFO */
225 #define SUNXI_DAUDIO_RXOM_EXPH          1
226 /* Fill RX_FIFO low word be 0 */
227 #define SUNXI_DAUDIO_RXOM_TUNL          2
228 /* Fill RX_FIFO high word be higher sample bit */
229 #define SUNXI_DAUDIO_RXOM_TUNH          3
230 
231 /* SUNXI_DAUDIO_CLKDIV */
232 #define SUNXI_DAUDIO_BCLK_DIV_MASK      0xF
233 #define SUNXI_DAUDIO_BCLK_DIV_1         1
234 #define SUNXI_DAUDIO_BCLK_DIV_2         2
235 #define SUNXI_DAUDIO_BCLK_DIV_3         3
236 #define SUNXI_DAUDIO_BCLK_DIV_4         4
237 #define SUNXI_DAUDIO_BCLK_DIV_5         5
238 #define SUNXI_DAUDIO_BCLK_DIV_6         6
239 #define SUNXI_DAUDIO_BCLK_DIV_7         7
240 #define SUNXI_DAUDIO_BCLK_DIV_8         8
241 #define SUNXI_DAUDIO_BCLK_DIV_9         9
242 #define SUNXI_DAUDIO_BCLK_DIV_10        10
243 #define SUNXI_DAUDIO_BCLK_DIV_11        11
244 #define SUNXI_DAUDIO_BCLK_DIV_12        12
245 #define SUNXI_DAUDIO_BCLK_DIV_13        13
246 #define SUNXI_DAUDIO_BCLK_DIV_14        14
247 #define SUNXI_DAUDIO_BCLK_DIV_15        15
248 /* Left Blank */
249 #define SUNXI_DAUDIO_MCLK_DIV_MASK      0xF
250 #define SUNXI_DAUDIO_MCLK_DIV_1         1
251 #define SUNXI_DAUDIO_MCLK_DIV_2         2
252 #define SUNXI_DAUDIO_MCLK_DIV_3         3
253 #define SUNXI_DAUDIO_MCLK_DIV_4         4
254 #define SUNXI_DAUDIO_MCLK_DIV_5         5
255 #define SUNXI_DAUDIO_MCLK_DIV_6         6
256 #define SUNXI_DAUDIO_MCLK_DIV_7         7
257 #define SUNXI_DAUDIO_MCLK_DIV_8         8
258 #define SUNXI_DAUDIO_MCLK_DIV_9         9
259 #define SUNXI_DAUDIO_MCLK_DIV_10        10
260 #define SUNXI_DAUDIO_MCLK_DIV_11        11
261 #define SUNXI_DAUDIO_MCLK_DIV_12        12
262 #define SUNXI_DAUDIO_MCLK_DIV_13        13
263 #define SUNXI_DAUDIO_MCLK_DIV_14        14
264 #define SUNXI_DAUDIO_MCLK_DIV_15        15
265 
266 /* SUNXI_DAUDIO_CHCFG */
267 #define SUNXI_DAUDIO_TX_SLOT_MASK       0XF
268 #define SUNXI_DAUDIO_RX_SLOT_MASK       0XF
269 
270 /* SUNXI_DAUDIO_TX0CHSEL: */
271 #define SUNXI_DAUDIO_TX_OFFSET_MASK     3
272 #define SUNXI_DAUDIO_TX_OFFSET_0        0
273 #define SUNXI_DAUDIO_TX_OFFSET_1        1
274 /* Left Blank */
275 #define SUNXI_DAUDIO_TX_CHEN_MASK       0xFFFF
276 #define SUNXI_DAUDIO_TX_CHSEL_MASK      0xF
277 
278 /* SUNXI_DAUDIO_RXCHSEL */
279 #define SUNXI_DAUDIO_RX_OFFSET_MASK     3
280 #define SUNXI_DAUDIO_RX_CHSEL_MASK      0XF
281 
282 #define DAUDIO_RXCH_DEF_MAP(x) (x << ((x%4)<<3))
283 #define DAUDIO_RXCHMAP(x) (0x1f << ((x%4)<<3))
284 
285 /* SUNXI_DAUDIO_ASRC_MCLKCFG:0x80 */
286 #define DAUDIO_ASRC_MCLK_GATE           16
287 #define DAUDIO_ASRC_MCLK_RATIO          0
288 
289 /* SUNXI_DAUDIO_ASRC_FSOUTCFG:0x84 */
290 #define DAUDIO_ASRC_FSOUT_GATE          20
291 #define DAUDIO_ASRC_FSOUT_CLKSRC        16
292 #define DAUDIO_ASRC_FSOUT_CLKDIV1       4
293 #define DAUDIO_ASRC_FSOUT_CLKDIV2       0
294 
295 /* SUNXI_DAUDIO_ASRC_FSIN_EXTCFG:0x88 */
296 #define DAUDIO_ASRC_FSIN_EXTEN          16
297 #define DAUDIO_ASRC_FSIN_EXTCYCLE       0
298 
299 /* SUNXI_DAUDIO_ASRC_ASRCEN:0x8C */
300 #define DAUDIO_ASRC_ASRCEN              0
301 
302 /* SUNXI_DAUDIO_ASRC_MANCFG:0x90 */
303 #define DAUDIO_ASRC_MANRATIOEN          31
304 #define DAUDIO_ASRC_MAN_RATIO           0
305 
306 /* SUNXI_DAUDIO_ASRC_RATIOSTAT:0x94 */
307 /* SUNXI_DAUDIO_ASRC_FIFOSTAT:0x98 */
308 /* SUNXI_DAUDIO_ASRC_MBISTCFG:0x9C */
309 /* SUNXI_DAUDIO_ASRC_MBISTSTA:0xA0 */
310 
311 /* DRQ Type */
312 #ifndef DRQDST_DAUDIO_0_TX
313 #define DRQDST_DAUDIO_0_TX  3
314 #endif
315 #ifndef DRQDST_DAUDIO_1_TX
316 #define DRQDST_DAUDIO_1_TX  4
317 #endif
318 #ifndef DRQDST_DAUDIO_2_TX
319 #define DRQDST_DAUDIO_2_TX  4
320 #endif
321 
322 #ifndef DRQSRC_DAUDIO_0_RX
323 #define DRQSRC_DAUDIO_0_RX  3
324 #endif
325 #ifndef DRQSRC_DAUDIO_1_RX
326 #define DRQSRC_DAUDIO_1_RX  4
327 #endif
328 #ifndef DRQSRC_DAUDIO_2_RX
329 #define DRQSRC_DAUDIO_2_RX  4
330 #endif
331 
332 #define SUNXI_DAUDIO_RATES  (SNDRV_PCM_RATE_8000_192000 \
333                 | SNDRV_PCM_RATE_KNOT)
334 
335 #define SUNXI_DAUDIO_DRQDST(sunxi_daudio, x)            \
336     ((sunxi_daudio)->playback_dma_param.dma_drq_type_num =  \
337                 DRQDST_DAUDIO_##x##_TX)
338 #define SUNXI_DAUDIO_DRQSRC(sunxi_daudio, x)            \
339     ((sunxi_daudio)->capture_dma_param.dma_drq_type_num =   \
340                 DRQSRC_DAUDIO_##x##_RX)
341 
342 /*to clear FIFO*/
343 #define SUNXI_DAUDIO_FTX_TIMES          1
344 
345 /* SUNXI_DAUDIO_HUB_ENABLE: Whether to use the hub mode */
346 #define SUNXI_DAUDIO_HUB_ENABLE
347 
348 enum sunxi_daudio_clk_parent {
349     SUNXI_MODULE_CLK_PLL_AUDIO,
350     SUNXI_MODULE_CLK_PLL_AUDIOX4,
351 };
352 
353 struct sunxi_daudio_param {
354        uint8_t tdm_num;
355        uint8_t daudio_master;
356        uint8_t audio_format;
357        uint8_t signal_inversion;
358        uint16_t pcm_lrck_period;
359        uint8_t msb_lsb_first:1;
360        uint8_t sign_extend:2;
361        uint8_t tx_data_mode:2;
362        uint8_t rx_data_mode:2;
363        uint8_t slot_width_select;
364        uint8_t frametype;
365        uint8_t tdm_config;
366        uint16_t mclk_div;
367        uint16_t playback_pcm_kbytes;
368        uint16_t capture_pcm_kbytes;
369 };
370 
371 struct daudio_pinctrl {
372     gpio_pin_t gpio_pin;
373     uint8_t mux;
374     uint8_t driv_level;
375 };
376 
377 struct sunxi_daudio_info {
378     struct snd_platform *platform;
379 
380     hal_clk_t pllclk;
381     hal_clk_t moduleclk;
382     hal_clk_t busclk;
383     hal_clk_t pllclk1;
384     hal_clk_t asrcclk;
385 
386     struct reset_control *rstclk;
387 
388     struct daudio_pinctrl *pinctrl;
389     uint8_t pinctrl_num;
390 
391     struct sunxi_daudio_param param;
392     struct sunxi_dma_params playback_dma_param;
393     struct sunxi_dma_params capture_dma_param;
394 
395     uint8_t global_enable;
396     unsigned int hub_mode;
397     bool playback_en;
398     bool capture_en;
399     int asrc_en;
400 };
401 
402 #if defined(CONFIG_ARCH_SUN8IW18P1)
403 #include "platforms/daudio-sun8iw18.h"
404 #endif
405 #if defined(CONFIG_ARCH_SUN8IW19)
406 #include "platforms/daudio-sun8iw19.h"
407 #endif
408 #if defined(CONFIG_ARCH_SUN8IW20) || defined(CONFIG_SOC_SUN20IW1)
409 #include "platforms/daudio-sun8iw20.h"
410 #endif
411 
412 
413 #endif  /* __SUNXI_DAUDIO_H_ */
414