2e41b08025a51e2b14a4dd21d07c9bcb6a233a04
[platform/kernel/linux-starfive.git] / sound / soc / starfive / starfive_i2s.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (ST) 2012 Rajeev Kumar (rajeevkumar.linux@gmail.com)
4  *
5  * This file is licensed under the terms of the GNU General Public
6  * License version 2. This program is licensed "as is" without any
7  * warranty of any kind, whether express or implied.
8  */
9
10 #ifndef __STARFIVE_I2S_H
11 #define __STARFIVE_I2S_H
12
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/types.h>
16 #include <sound/dmaengine_pcm.h>
17 #include <sound/pcm.h>
18 #include <sound/designware_i2s.h>
19
20 /* common register for all channel */
21 #define IER             0x000
22 #define IRER            0x004
23 #define ITER            0x008
24 #define CER             0x00C
25 #define CCR             0x010
26 #define RXFFR           0x014
27 #define TXFFR           0x018
28
29 /* Interrupt status register fields */
30 #define ISR_TXFO        BIT(5)
31 #define ISR_TXFE        BIT(4)
32 #define ISR_RXFO        BIT(1)
33 #define ISR_RXDA        BIT(0)
34
35 /* I2STxRxRegisters for all channels */
36 #define LRBR_LTHR(x)    (0x40 * x + 0x020)
37 #define RRBR_RTHR(x)    (0x40 * x + 0x024)
38 #define RER(x)          (0x40 * x + 0x028)
39 #define TER(x)          (0x40 * x + 0x02C)
40 #define RCR(x)          (0x40 * x + 0x030)
41 #define TCR(x)          (0x40 * x + 0x034)
42 #define ISR(x)          (0x40 * x + 0x038)
43 #define IMR(x)          (0x40 * x + 0x03C)
44 #define ROR(x)          (0x40 * x + 0x040)
45 #define TOR(x)          (0x40 * x + 0x044)
46 #define RFCR(x)         (0x40 * x + 0x048)
47 #define TFCR(x)         (0x40 * x + 0x04C)
48 #define RFF(x)          (0x40 * x + 0x050)
49 #define TFF(x)          (0x40 * x + 0x054)
50
51 /* I2SCOMPRegisters */
52 #define I2S_COMP_PARAM_2        0x01F0
53 #define I2S_COMP_PARAM_1        0x01F4
54 #define I2S_COMP_VERSION        0x01F8
55 #define I2S_COMP_TYPE           0x01FC
56
57 /*
58  * Component parameter register fields - define the I2S block's
59  * configuration.
60  */
61 #define COMP1_TX_WORDSIZE_3(r)  (((r) & GENMASK(27, 25)) >> 25)
62 #define COMP1_TX_WORDSIZE_2(r)  (((r) & GENMASK(24, 22)) >> 22)
63 #define COMP1_TX_WORDSIZE_1(r)  (((r) & GENMASK(21, 19)) >> 19)
64 #define COMP1_TX_WORDSIZE_0(r)  (((r) & GENMASK(18, 16)) >> 16)
65 #define COMP1_TX_CHANNELS(r)    (((r) & GENMASK(10, 9)) >> 9)
66 #define COMP1_RX_CHANNELS(r)    (((r) & GENMASK(8, 7)) >> 7)
67 #define COMP1_RX_ENABLED(r)     (((r) & BIT(6)) >> 6)
68 #define COMP1_TX_ENABLED(r)     (((r) & BIT(5)) >> 5)
69 #define COMP1_MODE_EN(r)        (((r) & BIT(4)) >> 4)
70 #define COMP1_FIFO_DEPTH_GLOBAL(r)      (((r) & GENMASK(3, 2)) >> 2)
71 #define COMP1_APB_DATA_WIDTH(r) (((r) & GENMASK(1, 0)) >> 0)
72
73 #define COMP2_RX_WORDSIZE_3(r)  (((r) & GENMASK(12, 10)) >> 10)
74 #define COMP2_RX_WORDSIZE_2(r)  (((r) & GENMASK(9, 7)) >> 7)
75 #define COMP2_RX_WORDSIZE_1(r)  (((r) & GENMASK(5, 3)) >> 3)
76 #define COMP2_RX_WORDSIZE_0(r)  (((r) & GENMASK(2, 0)) >> 0)
77
78 /* Number of entries in WORDSIZE and DATA_WIDTH parameter registers */
79 #define COMP_MAX_WORDSIZE       (1 << 3)
80 #define COMP_MAX_DATA_WIDTH     (1 << 2)
81
82 #define MAX_CHANNEL_NUM         8
83 #define MIN_CHANNEL_NUM         2
84
85 /* SYSCON Registers */
86 #define I2SRX_3CH_ADC_MASK      0x2
87 #define I2SRX_3CH_ADC_EN        BIT(1)
88 #define AUDIO_SDIN_MUX_MASK     0x3FC00
89 #define I2SRX_DATA_SRC_PDM      (0x91 << 10)
90
91 union dw_i2s_snd_dma_data {
92         struct i2s_dma_data pd;
93         struct snd_dmaengine_dai_dma_data dt;
94 };
95
96 struct dw_i2s_dev {
97         void __iomem *i2s_base;
98         struct regmap *syscon_base;
99         int active;
100         unsigned int capability;
101         unsigned int quirks;
102         unsigned int i2s_reg_comp1;
103         unsigned int i2s_reg_comp2;
104         struct device *dev;
105         u32 ccr;
106         u32 xfer_resolution;
107         u32 fifo_th;
108
109         struct clk *clk_i2s_apb;
110         struct clk *clk_i2s_bclk_mst;
111         struct clk *clk_i2s_lrck_mst;
112         struct clk *clk_i2s_bclk;
113         struct clk *clk_i2s_lrck;
114         struct clk *clk_mclk;
115         struct clk *clk_mclk_ext;
116         struct clk *clk_mclk_inner;
117         struct reset_control *rst_i2s_apb;
118         struct reset_control *rst_i2s_bclk;
119
120         /* data related to DMA transfers b/w i2s and DMAC */
121         union dw_i2s_snd_dma_data play_dma_data;
122         union dw_i2s_snd_dma_data capture_dma_data;
123         struct i2s_clk_config_data config;
124         int (*i2s_clk_cfg)(struct i2s_clk_config_data *config);
125
126         /* data related to PIO transfers */
127         bool use_pio;
128         struct snd_pcm_substream __rcu *tx_substream;
129         struct snd_pcm_substream __rcu *rx_substream;
130         unsigned int (*tx_fn)(struct dw_i2s_dev *dev,
131                         struct snd_pcm_runtime *runtime, unsigned int tx_ptr,
132                         bool *period_elapsed);
133         unsigned int (*rx_fn)(struct dw_i2s_dev *dev,
134                         struct snd_pcm_runtime *runtime, unsigned int rx_ptr,
135                         bool *period_elapsed);
136         unsigned int tx_ptr;
137         unsigned int rx_ptr;
138 };
139
140 #if IS_ENABLED(CONFIG_SND_DESIGNWARE_PCM)
141 void dw_pcm_push_tx(struct dw_i2s_dev *dev);
142 void dw_pcm_pop_rx(struct dw_i2s_dev *dev);
143 int dw_pcm_register(struct platform_device *pdev);
144 #else
145 static inline void dw_pcm_push_tx(struct dw_i2s_dev *dev) { }
146 static inline void dw_pcm_pop_rx(struct dw_i2s_dev *dev) { }
147 static inline int dw_pcm_register(struct platform_device *pdev)
148 {
149         return -EINVAL;
150 }
151 #endif
152
153 #endif