1 /*
2  * Copyright (c) 2018 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _STM32_I2S_H_
8 #define _STM32_I2S_H_
9 
10 struct queue_item {
11 	void *mem_block;
12 	size_t size;
13 };
14 
15 /* Minimal ring buffer implementation */
16 struct ring_buf {
17 	struct queue_item *buf;
18 	uint16_t len;
19 	uint16_t head;
20 	uint16_t tail;
21 };
22 
23 /* Device constant configuration parameters */
24 struct i2s_stm32_cfg {
25 	SPI_TypeDef *i2s;
26 	const struct stm32_pclken *pclken;
27 	size_t pclk_len;
28 	const struct pinctrl_dev_config *pcfg;
29 	void (*irq_config)(const struct device *dev);
30 	bool master_clk_sel;
31 };
32 
33 struct stream {
34 	int32_t state;
35 	struct k_sem sem;
36 
37 	const struct device *dev_dma;
38 	uint32_t dma_channel;
39 	struct dma_config dma_cfg;
40 	uint8_t priority;
41 	bool src_addr_increment;
42 	bool dst_addr_increment;
43 	uint8_t fifo_threshold;
44 	bool tx_stop_for_drain;
45 
46 	struct i2s_config cfg;
47 	struct ring_buf mem_block_queue;
48 	void *mem_block;
49 	bool last_block;
50 	bool master;
51 	int (*stream_start)(struct stream *, const struct device *dev);
52 	void (*stream_disable)(struct stream *, const struct device *dev);
53 	void (*queue_drop)(struct stream *);
54 };
55 
56 /* Device run time data */
57 struct i2s_stm32_data {
58 	struct stream rx;
59 	struct stream tx;
60 };
61 
62 /* checks that DMA Tx packet is fully transmitted over the I2S */
ll_func_i2s_dma_busy(SPI_TypeDef * i2s)63 static inline uint32_t ll_func_i2s_dma_busy(SPI_TypeDef *i2s)
64 {
65 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_i2s)
66 	return LL_SPI_IsActiveFlag_TXC(i2s) == 0;
67 #else
68 	/* the I2S Tx empty and busy flags are needed */
69 	return (LL_SPI_IsActiveFlag_TXE(i2s) &&
70 		!LL_SPI_IsActiveFlag_BSY(i2s));
71 #endif
72 }
73 
74 #endif	/* _STM32_I2S_H_ */
75