1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Freescale ALSA SoC Digital Audio Interface (SAI) driver.
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright 2012-2015 Freescale Semiconductor, Inc.
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/dmaengine.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of_address.h>
12*4882a593Smuzhiyun #include <linux/of_device.h>
13*4882a593Smuzhiyun #include <linux/pm_runtime.h>
14*4882a593Smuzhiyun #include <linux/regmap.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/time.h>
17*4882a593Smuzhiyun #include <sound/core.h>
18*4882a593Smuzhiyun #include <sound/dmaengine_pcm.h>
19*4882a593Smuzhiyun #include <sound/pcm_params.h>
20*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
21*4882a593Smuzhiyun #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "fsl_sai.h"
24*4882a593Smuzhiyun #include "imx-pcm.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
27*4882a593Smuzhiyun FSL_SAI_CSR_FEIE)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static const unsigned int fsl_sai_rates[] = {
30*4882a593Smuzhiyun 8000, 11025, 12000, 16000, 22050,
31*4882a593Smuzhiyun 24000, 32000, 44100, 48000, 64000,
32*4882a593Smuzhiyun 88200, 96000, 176400, 192000
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
36*4882a593Smuzhiyun .count = ARRAY_SIZE(fsl_sai_rates),
37*4882a593Smuzhiyun .list = fsl_sai_rates,
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * SAI supports synchronous mode using bit/frame clocks of either Transmitter's
44*4882a593Smuzhiyun * or Receiver's for both streams. This function is used to check if clocks of
45*4882a593Smuzhiyun * the stream's are synced by the opposite stream.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * @sai: SAI context
48*4882a593Smuzhiyun * @dir: stream direction
49*4882a593Smuzhiyun */
fsl_sai_dir_is_synced(struct fsl_sai * sai,int dir)50*4882a593Smuzhiyun static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun int adir = (dir == TX) ? RX : TX;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* current dir in async mode while opposite dir in sync mode */
55*4882a593Smuzhiyun return !sai->synchronous[dir] && sai->synchronous[adir];
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
fsl_sai_isr(int irq,void * devid)58*4882a593Smuzhiyun static irqreturn_t fsl_sai_isr(int irq, void *devid)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun struct fsl_sai *sai = (struct fsl_sai *)devid;
61*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
62*4882a593Smuzhiyun struct device *dev = &sai->pdev->dev;
63*4882a593Smuzhiyun u32 flags, xcsr, mask;
64*4882a593Smuzhiyun bool irq_none = true;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * Both IRQ status bits and IRQ mask bits are in the xCSR but
68*4882a593Smuzhiyun * different shifts. And we here create a mask only for those
69*4882a593Smuzhiyun * IRQs that we activated.
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Tx IRQ */
74*4882a593Smuzhiyun regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr);
75*4882a593Smuzhiyun flags = xcsr & mask;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (flags)
78*4882a593Smuzhiyun irq_none = false;
79*4882a593Smuzhiyun else
80*4882a593Smuzhiyun goto irq_rx;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_WSF)
83*4882a593Smuzhiyun dev_dbg(dev, "isr: Start of Tx word detected\n");
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_SEF)
86*4882a593Smuzhiyun dev_dbg(dev, "isr: Tx Frame sync error detected\n");
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FEF) {
89*4882a593Smuzhiyun dev_dbg(dev, "isr: Transmit underrun detected\n");
90*4882a593Smuzhiyun /* FIFO reset for safety */
91*4882a593Smuzhiyun xcsr |= FSL_SAI_CSR_FR;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FWF)
95*4882a593Smuzhiyun dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FRF)
98*4882a593Smuzhiyun dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun flags &= FSL_SAI_CSR_xF_W_MASK;
101*4882a593Smuzhiyun xcsr &= ~FSL_SAI_CSR_xF_MASK;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun if (flags)
104*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun irq_rx:
107*4882a593Smuzhiyun /* Rx IRQ */
108*4882a593Smuzhiyun regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr);
109*4882a593Smuzhiyun flags = xcsr & mask;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (flags)
112*4882a593Smuzhiyun irq_none = false;
113*4882a593Smuzhiyun else
114*4882a593Smuzhiyun goto out;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_WSF)
117*4882a593Smuzhiyun dev_dbg(dev, "isr: Start of Rx word detected\n");
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_SEF)
120*4882a593Smuzhiyun dev_dbg(dev, "isr: Rx Frame sync error detected\n");
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FEF) {
123*4882a593Smuzhiyun dev_dbg(dev, "isr: Receive overflow detected\n");
124*4882a593Smuzhiyun /* FIFO reset for safety */
125*4882a593Smuzhiyun xcsr |= FSL_SAI_CSR_FR;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FWF)
129*4882a593Smuzhiyun dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (flags & FSL_SAI_CSR_FRF)
132*4882a593Smuzhiyun dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun flags &= FSL_SAI_CSR_xF_W_MASK;
135*4882a593Smuzhiyun xcsr &= ~FSL_SAI_CSR_xF_MASK;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (flags)
138*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun out:
141*4882a593Smuzhiyun if (irq_none)
142*4882a593Smuzhiyun return IRQ_NONE;
143*4882a593Smuzhiyun else
144*4882a593Smuzhiyun return IRQ_HANDLED;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
fsl_sai_set_dai_tdm_slot(struct snd_soc_dai * cpu_dai,u32 tx_mask,u32 rx_mask,int slots,int slot_width)147*4882a593Smuzhiyun static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
148*4882a593Smuzhiyun u32 rx_mask, int slots, int slot_width)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun sai->slots = slots;
153*4882a593Smuzhiyun sai->slot_width = slot_width;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai * dai,unsigned int ratio)158*4882a593Smuzhiyun static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai,
159*4882a593Smuzhiyun unsigned int ratio)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun sai->bclk_ratio = ratio;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun return 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai * cpu_dai,int clk_id,unsigned int freq,int fsl_dir)168*4882a593Smuzhiyun static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
169*4882a593Smuzhiyun int clk_id, unsigned int freq, int fsl_dir)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
172*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
173*4882a593Smuzhiyun bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
174*4882a593Smuzhiyun u32 val_cr2 = 0;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun switch (clk_id) {
177*4882a593Smuzhiyun case FSL_SAI_CLK_BUS:
178*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
179*4882a593Smuzhiyun break;
180*4882a593Smuzhiyun case FSL_SAI_CLK_MAST1:
181*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
182*4882a593Smuzhiyun break;
183*4882a593Smuzhiyun case FSL_SAI_CLK_MAST2:
184*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
185*4882a593Smuzhiyun break;
186*4882a593Smuzhiyun case FSL_SAI_CLK_MAST3:
187*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
188*4882a593Smuzhiyun break;
189*4882a593Smuzhiyun default:
190*4882a593Smuzhiyun return -EINVAL;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
194*4882a593Smuzhiyun FSL_SAI_CR2_MSEL_MASK, val_cr2);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return 0;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
fsl_sai_set_dai_sysclk(struct snd_soc_dai * cpu_dai,int clk_id,unsigned int freq,int dir)199*4882a593Smuzhiyun static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
200*4882a593Smuzhiyun int clk_id, unsigned int freq, int dir)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun int ret;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (dir == SND_SOC_CLOCK_IN)
205*4882a593Smuzhiyun return 0;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
208*4882a593Smuzhiyun FSL_FMT_TRANSMITTER);
209*4882a593Smuzhiyun if (ret) {
210*4882a593Smuzhiyun dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
211*4882a593Smuzhiyun return ret;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
215*4882a593Smuzhiyun FSL_FMT_RECEIVER);
216*4882a593Smuzhiyun if (ret)
217*4882a593Smuzhiyun dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return ret;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
fsl_sai_set_dai_fmt_tr(struct snd_soc_dai * cpu_dai,unsigned int fmt,int fsl_dir)222*4882a593Smuzhiyun static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
223*4882a593Smuzhiyun unsigned int fmt, int fsl_dir)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
226*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
227*4882a593Smuzhiyun bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
228*4882a593Smuzhiyun u32 val_cr2 = 0, val_cr4 = 0;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun if (!sai->is_lsb_first)
231*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_MF;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /* DAI mode */
234*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
235*4882a593Smuzhiyun case SND_SOC_DAIFMT_I2S:
236*4882a593Smuzhiyun /*
237*4882a593Smuzhiyun * Frame low, 1clk before data, one word length for frame sync,
238*4882a593Smuzhiyun * frame sync starts one serial clock cycle earlier,
239*4882a593Smuzhiyun * that is, together with the last bit of the previous
240*4882a593Smuzhiyun * data word.
241*4882a593Smuzhiyun */
242*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCP;
243*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
244*4882a593Smuzhiyun break;
245*4882a593Smuzhiyun case SND_SOC_DAIFMT_LEFT_J:
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * Frame high, one word length for frame sync,
248*4882a593Smuzhiyun * frame sync asserts with the first bit of the frame.
249*4882a593Smuzhiyun */
250*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCP;
251*4882a593Smuzhiyun break;
252*4882a593Smuzhiyun case SND_SOC_DAIFMT_DSP_A:
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun * Frame high, 1clk before data, one bit for frame sync,
255*4882a593Smuzhiyun * frame sync starts one serial clock cycle earlier,
256*4882a593Smuzhiyun * that is, together with the last bit of the previous
257*4882a593Smuzhiyun * data word.
258*4882a593Smuzhiyun */
259*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCP;
260*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_FSE;
261*4882a593Smuzhiyun sai->is_dsp_mode = true;
262*4882a593Smuzhiyun break;
263*4882a593Smuzhiyun case SND_SOC_DAIFMT_DSP_B:
264*4882a593Smuzhiyun /*
265*4882a593Smuzhiyun * Frame high, one bit for frame sync,
266*4882a593Smuzhiyun * frame sync asserts with the first bit of the frame.
267*4882a593Smuzhiyun */
268*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCP;
269*4882a593Smuzhiyun sai->is_dsp_mode = true;
270*4882a593Smuzhiyun break;
271*4882a593Smuzhiyun case SND_SOC_DAIFMT_RIGHT_J:
272*4882a593Smuzhiyun /* To be done */
273*4882a593Smuzhiyun default:
274*4882a593Smuzhiyun return -EINVAL;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* DAI clock inversion */
278*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
279*4882a593Smuzhiyun case SND_SOC_DAIFMT_IB_IF:
280*4882a593Smuzhiyun /* Invert both clocks */
281*4882a593Smuzhiyun val_cr2 ^= FSL_SAI_CR2_BCP;
282*4882a593Smuzhiyun val_cr4 ^= FSL_SAI_CR4_FSP;
283*4882a593Smuzhiyun break;
284*4882a593Smuzhiyun case SND_SOC_DAIFMT_IB_NF:
285*4882a593Smuzhiyun /* Invert bit clock */
286*4882a593Smuzhiyun val_cr2 ^= FSL_SAI_CR2_BCP;
287*4882a593Smuzhiyun break;
288*4882a593Smuzhiyun case SND_SOC_DAIFMT_NB_IF:
289*4882a593Smuzhiyun /* Invert frame clock */
290*4882a593Smuzhiyun val_cr4 ^= FSL_SAI_CR4_FSP;
291*4882a593Smuzhiyun break;
292*4882a593Smuzhiyun case SND_SOC_DAIFMT_NB_NF:
293*4882a593Smuzhiyun /* Nothing to do for both normal cases */
294*4882a593Smuzhiyun break;
295*4882a593Smuzhiyun default:
296*4882a593Smuzhiyun return -EINVAL;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /* DAI clock master masks */
300*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
301*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBS_CFS:
302*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
303*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
304*4882a593Smuzhiyun sai->is_slave_mode = false;
305*4882a593Smuzhiyun break;
306*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBM_CFM:
307*4882a593Smuzhiyun sai->is_slave_mode = true;
308*4882a593Smuzhiyun break;
309*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBS_CFM:
310*4882a593Smuzhiyun val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
311*4882a593Smuzhiyun sai->is_slave_mode = false;
312*4882a593Smuzhiyun break;
313*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBM_CFS:
314*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
315*4882a593Smuzhiyun sai->is_slave_mode = true;
316*4882a593Smuzhiyun break;
317*4882a593Smuzhiyun default:
318*4882a593Smuzhiyun return -EINVAL;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
322*4882a593Smuzhiyun FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
323*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
324*4882a593Smuzhiyun FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
325*4882a593Smuzhiyun FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun return 0;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
fsl_sai_set_dai_fmt(struct snd_soc_dai * cpu_dai,unsigned int fmt)330*4882a593Smuzhiyun static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun int ret;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
335*4882a593Smuzhiyun if (ret) {
336*4882a593Smuzhiyun dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
337*4882a593Smuzhiyun return ret;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
341*4882a593Smuzhiyun if (ret)
342*4882a593Smuzhiyun dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return ret;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
fsl_sai_set_bclk(struct snd_soc_dai * dai,bool tx,u32 freq)347*4882a593Smuzhiyun static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
350*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
351*4882a593Smuzhiyun unsigned long clk_rate;
352*4882a593Smuzhiyun u32 savediv = 0, ratio, savesub = freq;
353*4882a593Smuzhiyun int adir = tx ? RX : TX;
354*4882a593Smuzhiyun int dir = tx ? TX : RX;
355*4882a593Smuzhiyun u32 id;
356*4882a593Smuzhiyun int ret = 0;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /* Don't apply to slave mode */
359*4882a593Smuzhiyun if (sai->is_slave_mode)
360*4882a593Smuzhiyun return 0;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
363*4882a593Smuzhiyun clk_rate = clk_get_rate(sai->mclk_clk[id]);
364*4882a593Smuzhiyun if (!clk_rate)
365*4882a593Smuzhiyun continue;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun ratio = clk_rate / freq;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun ret = clk_rate - ratio * freq;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /*
372*4882a593Smuzhiyun * Drop the source that can not be
373*4882a593Smuzhiyun * divided into the required rate.
374*4882a593Smuzhiyun */
375*4882a593Smuzhiyun if (ret != 0 && clk_rate / ret < 1000)
376*4882a593Smuzhiyun continue;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun dev_dbg(dai->dev,
379*4882a593Smuzhiyun "ratio %d for freq %dHz based on clock %ldHz\n",
380*4882a593Smuzhiyun ratio, freq, clk_rate);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
383*4882a593Smuzhiyun ratio /= 2;
384*4882a593Smuzhiyun else
385*4882a593Smuzhiyun continue;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if (ret < savesub) {
388*4882a593Smuzhiyun savediv = ratio;
389*4882a593Smuzhiyun sai->mclk_id[tx] = id;
390*4882a593Smuzhiyun savesub = ret;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun if (ret == 0)
394*4882a593Smuzhiyun break;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun if (savediv == 0) {
398*4882a593Smuzhiyun dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
399*4882a593Smuzhiyun tx ? 'T' : 'R', freq);
400*4882a593Smuzhiyun return -EINVAL;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun /*
404*4882a593Smuzhiyun * 1) For Asynchronous mode, we must set RCR2 register for capture, and
405*4882a593Smuzhiyun * set TCR2 register for playback.
406*4882a593Smuzhiyun * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
407*4882a593Smuzhiyun * and capture.
408*4882a593Smuzhiyun * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
409*4882a593Smuzhiyun * and capture.
410*4882a593Smuzhiyun * 4) For Tx and Rx are both Synchronous with another SAI, we just
411*4882a593Smuzhiyun * ignore it.
412*4882a593Smuzhiyun */
413*4882a593Smuzhiyun if (fsl_sai_dir_is_synced(sai, adir)) {
414*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs),
415*4882a593Smuzhiyun FSL_SAI_CR2_MSEL_MASK,
416*4882a593Smuzhiyun FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
417*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs),
418*4882a593Smuzhiyun FSL_SAI_CR2_DIV_MASK, savediv - 1);
419*4882a593Smuzhiyun } else if (!sai->synchronous[dir]) {
420*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
421*4882a593Smuzhiyun FSL_SAI_CR2_MSEL_MASK,
422*4882a593Smuzhiyun FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
423*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
424*4882a593Smuzhiyun FSL_SAI_CR2_DIV_MASK, savediv - 1);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
428*4882a593Smuzhiyun sai->mclk_id[tx], savediv, savesub);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun return 0;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
fsl_sai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * cpu_dai)433*4882a593Smuzhiyun static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
434*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
435*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
438*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
439*4882a593Smuzhiyun bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
440*4882a593Smuzhiyun unsigned int channels = params_channels(params);
441*4882a593Smuzhiyun u32 word_width = params_width(params);
442*4882a593Smuzhiyun u32 val_cr4 = 0, val_cr5 = 0;
443*4882a593Smuzhiyun u32 slots = (channels == 1) ? 2 : channels;
444*4882a593Smuzhiyun u32 slot_width = word_width;
445*4882a593Smuzhiyun int adir = tx ? RX : TX;
446*4882a593Smuzhiyun u32 pins;
447*4882a593Smuzhiyun int ret;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun if (sai->slots)
450*4882a593Smuzhiyun slots = sai->slots;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun if (sai->slot_width)
453*4882a593Smuzhiyun slot_width = sai->slot_width;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun pins = DIV_ROUND_UP(channels, slots);
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun if (!sai->is_slave_mode) {
458*4882a593Smuzhiyun if (sai->bclk_ratio)
459*4882a593Smuzhiyun ret = fsl_sai_set_bclk(cpu_dai, tx,
460*4882a593Smuzhiyun sai->bclk_ratio *
461*4882a593Smuzhiyun params_rate(params));
462*4882a593Smuzhiyun else
463*4882a593Smuzhiyun ret = fsl_sai_set_bclk(cpu_dai, tx,
464*4882a593Smuzhiyun slots * slot_width *
465*4882a593Smuzhiyun params_rate(params));
466*4882a593Smuzhiyun if (ret)
467*4882a593Smuzhiyun return ret;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /* Do not enable the clock if it is already enabled */
470*4882a593Smuzhiyun if (!(sai->mclk_streams & BIT(substream->stream))) {
471*4882a593Smuzhiyun ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
472*4882a593Smuzhiyun if (ret)
473*4882a593Smuzhiyun return ret;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun sai->mclk_streams |= BIT(substream->stream);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun if (!sai->is_dsp_mode)
480*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
483*4882a593Smuzhiyun val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun if (sai->is_lsb_first)
486*4882a593Smuzhiyun val_cr5 |= FSL_SAI_CR5_FBT(0);
487*4882a593Smuzhiyun else
488*4882a593Smuzhiyun val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /* Set to output mode to avoid tri-stated data pins */
493*4882a593Smuzhiyun if (tx)
494*4882a593Smuzhiyun val_cr4 |= FSL_SAI_CR4_CHMOD;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
498*4882a593Smuzhiyun * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
499*4882a593Smuzhiyun * RCR5(TCR5) for playback(capture), or there will be sync error.
500*4882a593Smuzhiyun */
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun if (!sai->is_slave_mode && fsl_sai_dir_is_synced(sai, adir)) {
503*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs),
504*4882a593Smuzhiyun FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
505*4882a593Smuzhiyun FSL_SAI_CR4_CHMOD_MASK,
506*4882a593Smuzhiyun val_cr4);
507*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs),
508*4882a593Smuzhiyun FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
509*4882a593Smuzhiyun FSL_SAI_CR5_FBT_MASK, val_cr5);
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
513*4882a593Smuzhiyun FSL_SAI_CR3_TRCE_MASK,
514*4882a593Smuzhiyun FSL_SAI_CR3_TRCE((1 << pins) - 1));
515*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
516*4882a593Smuzhiyun FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
517*4882a593Smuzhiyun FSL_SAI_CR4_CHMOD_MASK,
518*4882a593Smuzhiyun val_cr4);
519*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs),
520*4882a593Smuzhiyun FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
521*4882a593Smuzhiyun FSL_SAI_CR5_FBT_MASK, val_cr5);
522*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_xMR(tx),
523*4882a593Smuzhiyun ~0UL - ((1 << min(channels, slots)) - 1));
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun return 0;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
fsl_sai_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * cpu_dai)528*4882a593Smuzhiyun static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
529*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
532*4882a593Smuzhiyun bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
533*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
536*4882a593Smuzhiyun FSL_SAI_CR3_TRCE_MASK, 0);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (!sai->is_slave_mode &&
539*4882a593Smuzhiyun sai->mclk_streams & BIT(substream->stream)) {
540*4882a593Smuzhiyun clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
541*4882a593Smuzhiyun sai->mclk_streams &= ~BIT(substream->stream);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
fsl_sai_config_disable(struct fsl_sai * sai,int dir)547*4882a593Smuzhiyun static void fsl_sai_config_disable(struct fsl_sai *sai, int dir)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
550*4882a593Smuzhiyun bool tx = dir == TX;
551*4882a593Smuzhiyun u32 xcsr, count = 100;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
554*4882a593Smuzhiyun FSL_SAI_CSR_TERE, 0);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /* TERE will remain set till the end of current frame */
557*4882a593Smuzhiyun do {
558*4882a593Smuzhiyun udelay(10);
559*4882a593Smuzhiyun regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr);
560*4882a593Smuzhiyun } while (--count && xcsr & FSL_SAI_CSR_TERE);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
563*4882a593Smuzhiyun FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /*
566*4882a593Smuzhiyun * For sai master mode, after several open/close sai,
567*4882a593Smuzhiyun * there will be no frame clock, and can't recover
568*4882a593Smuzhiyun * anymore. Add software reset to fix this issue.
569*4882a593Smuzhiyun * This is a hardware bug, and will be fix in the
570*4882a593Smuzhiyun * next sai version.
571*4882a593Smuzhiyun */
572*4882a593Smuzhiyun if (!sai->is_slave_mode) {
573*4882a593Smuzhiyun /* Software Reset */
574*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR);
575*4882a593Smuzhiyun /* Clear SR bit to finish the reset */
576*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), 0);
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
fsl_sai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * cpu_dai)580*4882a593Smuzhiyun static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
581*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
584*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
587*4882a593Smuzhiyun int adir = tx ? RX : TX;
588*4882a593Smuzhiyun int dir = tx ? TX : RX;
589*4882a593Smuzhiyun u32 xcsr;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun /*
592*4882a593Smuzhiyun * Asynchronous mode: Clear SYNC for both Tx and Rx.
593*4882a593Smuzhiyun * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
594*4882a593Smuzhiyun * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
595*4882a593Smuzhiyun */
596*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC,
597*4882a593Smuzhiyun sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
598*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC,
599*4882a593Smuzhiyun sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * It is recommended that the transmitter is the last enabled
603*4882a593Smuzhiyun * and the first disabled.
604*4882a593Smuzhiyun */
605*4882a593Smuzhiyun switch (cmd) {
606*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_START:
607*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_RESUME:
608*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
609*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
610*4882a593Smuzhiyun FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
613*4882a593Smuzhiyun FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
614*4882a593Smuzhiyun /*
615*4882a593Smuzhiyun * Enable the opposite direction for synchronous mode
616*4882a593Smuzhiyun * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx
617*4882a593Smuzhiyun * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun * RM recommends to enable RE after TE for case 1 and to enable
620*4882a593Smuzhiyun * TE after RE for case 2, but we here may not always guarantee
621*4882a593Smuzhiyun * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables
622*4882a593Smuzhiyun * TE after RE, which is against what RM recommends but should
623*4882a593Smuzhiyun * be safe to do, judging by years of testing results.
624*4882a593Smuzhiyun */
625*4882a593Smuzhiyun if (fsl_sai_dir_is_synced(sai, adir))
626*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs),
627*4882a593Smuzhiyun FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
630*4882a593Smuzhiyun FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
631*4882a593Smuzhiyun break;
632*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_STOP:
633*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_SUSPEND:
634*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
635*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
636*4882a593Smuzhiyun FSL_SAI_CSR_FRDE, 0);
637*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
638*4882a593Smuzhiyun FSL_SAI_CSR_xIE_MASK, 0);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /* Check if the opposite FRDE is also disabled */
641*4882a593Smuzhiyun regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /*
644*4882a593Smuzhiyun * If opposite stream provides clocks for synchronous mode and
645*4882a593Smuzhiyun * it is inactive, disable it before disabling the current one
646*4882a593Smuzhiyun */
647*4882a593Smuzhiyun if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE))
648*4882a593Smuzhiyun fsl_sai_config_disable(sai, adir);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun /*
651*4882a593Smuzhiyun * Disable current stream if either of:
652*4882a593Smuzhiyun * 1. current stream doesn't provide clocks for synchronous mode
653*4882a593Smuzhiyun * 2. current stream provides clocks for synchronous mode but no
654*4882a593Smuzhiyun * more stream is active.
655*4882a593Smuzhiyun */
656*4882a593Smuzhiyun if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE))
657*4882a593Smuzhiyun fsl_sai_config_disable(sai, dir);
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun break;
660*4882a593Smuzhiyun default:
661*4882a593Smuzhiyun return -EINVAL;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun return 0;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
fsl_sai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * cpu_dai)667*4882a593Smuzhiyun static int fsl_sai_startup(struct snd_pcm_substream *substream,
668*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
671*4882a593Smuzhiyun bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
672*4882a593Smuzhiyun int ret;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /*
675*4882a593Smuzhiyun * EDMA controller needs period size to be a multiple of
676*4882a593Smuzhiyun * tx/rx maxburst
677*4882a593Smuzhiyun */
678*4882a593Smuzhiyun if (sai->soc_data->use_edma)
679*4882a593Smuzhiyun snd_pcm_hw_constraint_step(substream->runtime, 0,
680*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
681*4882a593Smuzhiyun tx ? sai->dma_params_tx.maxburst :
682*4882a593Smuzhiyun sai->dma_params_rx.maxburst);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
685*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun return ret;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
691*4882a593Smuzhiyun .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio,
692*4882a593Smuzhiyun .set_sysclk = fsl_sai_set_dai_sysclk,
693*4882a593Smuzhiyun .set_fmt = fsl_sai_set_dai_fmt,
694*4882a593Smuzhiyun .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
695*4882a593Smuzhiyun .hw_params = fsl_sai_hw_params,
696*4882a593Smuzhiyun .hw_free = fsl_sai_hw_free,
697*4882a593Smuzhiyun .trigger = fsl_sai_trigger,
698*4882a593Smuzhiyun .startup = fsl_sai_startup,
699*4882a593Smuzhiyun };
700*4882a593Smuzhiyun
fsl_sai_dai_probe(struct snd_soc_dai * cpu_dai)701*4882a593Smuzhiyun static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
704*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun /* Software Reset for both Tx and Rx */
707*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
708*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
709*4882a593Smuzhiyun /* Clear SR bit to finish the reset */
710*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
711*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs),
714*4882a593Smuzhiyun FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
715*4882a593Smuzhiyun sai->soc_data->fifo_depth - FSL_SAI_MAXBURST_TX);
716*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs),
717*4882a593Smuzhiyun FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
718*4882a593Smuzhiyun FSL_SAI_MAXBURST_RX - 1);
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
721*4882a593Smuzhiyun &sai->dma_params_rx);
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun snd_soc_dai_set_drvdata(cpu_dai, sai);
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun return 0;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun static struct snd_soc_dai_driver fsl_sai_dai_template = {
729*4882a593Smuzhiyun .probe = fsl_sai_dai_probe,
730*4882a593Smuzhiyun .playback = {
731*4882a593Smuzhiyun .stream_name = "CPU-Playback",
732*4882a593Smuzhiyun .channels_min = 1,
733*4882a593Smuzhiyun .channels_max = 32,
734*4882a593Smuzhiyun .rate_min = 8000,
735*4882a593Smuzhiyun .rate_max = 192000,
736*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_KNOT,
737*4882a593Smuzhiyun .formats = FSL_SAI_FORMATS,
738*4882a593Smuzhiyun },
739*4882a593Smuzhiyun .capture = {
740*4882a593Smuzhiyun .stream_name = "CPU-Capture",
741*4882a593Smuzhiyun .channels_min = 1,
742*4882a593Smuzhiyun .channels_max = 32,
743*4882a593Smuzhiyun .rate_min = 8000,
744*4882a593Smuzhiyun .rate_max = 192000,
745*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_KNOT,
746*4882a593Smuzhiyun .formats = FSL_SAI_FORMATS,
747*4882a593Smuzhiyun },
748*4882a593Smuzhiyun .ops = &fsl_sai_pcm_dai_ops,
749*4882a593Smuzhiyun };
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun static const struct snd_soc_component_driver fsl_component = {
752*4882a593Smuzhiyun .name = "fsl-sai",
753*4882a593Smuzhiyun };
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun static struct reg_default fsl_sai_reg_defaults_ofs0[] = {
756*4882a593Smuzhiyun {FSL_SAI_TCR1(0), 0},
757*4882a593Smuzhiyun {FSL_SAI_TCR2(0), 0},
758*4882a593Smuzhiyun {FSL_SAI_TCR3(0), 0},
759*4882a593Smuzhiyun {FSL_SAI_TCR4(0), 0},
760*4882a593Smuzhiyun {FSL_SAI_TCR5(0), 0},
761*4882a593Smuzhiyun {FSL_SAI_TDR0, 0},
762*4882a593Smuzhiyun {FSL_SAI_TDR1, 0},
763*4882a593Smuzhiyun {FSL_SAI_TDR2, 0},
764*4882a593Smuzhiyun {FSL_SAI_TDR3, 0},
765*4882a593Smuzhiyun {FSL_SAI_TDR4, 0},
766*4882a593Smuzhiyun {FSL_SAI_TDR5, 0},
767*4882a593Smuzhiyun {FSL_SAI_TDR6, 0},
768*4882a593Smuzhiyun {FSL_SAI_TDR7, 0},
769*4882a593Smuzhiyun {FSL_SAI_TMR, 0},
770*4882a593Smuzhiyun {FSL_SAI_RCR1(0), 0},
771*4882a593Smuzhiyun {FSL_SAI_RCR2(0), 0},
772*4882a593Smuzhiyun {FSL_SAI_RCR3(0), 0},
773*4882a593Smuzhiyun {FSL_SAI_RCR4(0), 0},
774*4882a593Smuzhiyun {FSL_SAI_RCR5(0), 0},
775*4882a593Smuzhiyun {FSL_SAI_RMR, 0},
776*4882a593Smuzhiyun };
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun static struct reg_default fsl_sai_reg_defaults_ofs8[] = {
779*4882a593Smuzhiyun {FSL_SAI_TCR1(8), 0},
780*4882a593Smuzhiyun {FSL_SAI_TCR2(8), 0},
781*4882a593Smuzhiyun {FSL_SAI_TCR3(8), 0},
782*4882a593Smuzhiyun {FSL_SAI_TCR4(8), 0},
783*4882a593Smuzhiyun {FSL_SAI_TCR5(8), 0},
784*4882a593Smuzhiyun {FSL_SAI_TDR0, 0},
785*4882a593Smuzhiyun {FSL_SAI_TDR1, 0},
786*4882a593Smuzhiyun {FSL_SAI_TDR2, 0},
787*4882a593Smuzhiyun {FSL_SAI_TDR3, 0},
788*4882a593Smuzhiyun {FSL_SAI_TDR4, 0},
789*4882a593Smuzhiyun {FSL_SAI_TDR5, 0},
790*4882a593Smuzhiyun {FSL_SAI_TDR6, 0},
791*4882a593Smuzhiyun {FSL_SAI_TDR7, 0},
792*4882a593Smuzhiyun {FSL_SAI_TMR, 0},
793*4882a593Smuzhiyun {FSL_SAI_RCR1(8), 0},
794*4882a593Smuzhiyun {FSL_SAI_RCR2(8), 0},
795*4882a593Smuzhiyun {FSL_SAI_RCR3(8), 0},
796*4882a593Smuzhiyun {FSL_SAI_RCR4(8), 0},
797*4882a593Smuzhiyun {FSL_SAI_RCR5(8), 0},
798*4882a593Smuzhiyun {FSL_SAI_RMR, 0},
799*4882a593Smuzhiyun {FSL_SAI_MCTL, 0},
800*4882a593Smuzhiyun {FSL_SAI_MDIV, 0},
801*4882a593Smuzhiyun };
802*4882a593Smuzhiyun
fsl_sai_readable_reg(struct device * dev,unsigned int reg)803*4882a593Smuzhiyun static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
806*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
809*4882a593Smuzhiyun return true;
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
812*4882a593Smuzhiyun return true;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun switch (reg) {
815*4882a593Smuzhiyun case FSL_SAI_TFR0:
816*4882a593Smuzhiyun case FSL_SAI_TFR1:
817*4882a593Smuzhiyun case FSL_SAI_TFR2:
818*4882a593Smuzhiyun case FSL_SAI_TFR3:
819*4882a593Smuzhiyun case FSL_SAI_TFR4:
820*4882a593Smuzhiyun case FSL_SAI_TFR5:
821*4882a593Smuzhiyun case FSL_SAI_TFR6:
822*4882a593Smuzhiyun case FSL_SAI_TFR7:
823*4882a593Smuzhiyun case FSL_SAI_TMR:
824*4882a593Smuzhiyun case FSL_SAI_RDR0:
825*4882a593Smuzhiyun case FSL_SAI_RDR1:
826*4882a593Smuzhiyun case FSL_SAI_RDR2:
827*4882a593Smuzhiyun case FSL_SAI_RDR3:
828*4882a593Smuzhiyun case FSL_SAI_RDR4:
829*4882a593Smuzhiyun case FSL_SAI_RDR5:
830*4882a593Smuzhiyun case FSL_SAI_RDR6:
831*4882a593Smuzhiyun case FSL_SAI_RDR7:
832*4882a593Smuzhiyun case FSL_SAI_RFR0:
833*4882a593Smuzhiyun case FSL_SAI_RFR1:
834*4882a593Smuzhiyun case FSL_SAI_RFR2:
835*4882a593Smuzhiyun case FSL_SAI_RFR3:
836*4882a593Smuzhiyun case FSL_SAI_RFR4:
837*4882a593Smuzhiyun case FSL_SAI_RFR5:
838*4882a593Smuzhiyun case FSL_SAI_RFR6:
839*4882a593Smuzhiyun case FSL_SAI_RFR7:
840*4882a593Smuzhiyun case FSL_SAI_RMR:
841*4882a593Smuzhiyun case FSL_SAI_MCTL:
842*4882a593Smuzhiyun case FSL_SAI_MDIV:
843*4882a593Smuzhiyun case FSL_SAI_VERID:
844*4882a593Smuzhiyun case FSL_SAI_PARAM:
845*4882a593Smuzhiyun case FSL_SAI_TTCTN:
846*4882a593Smuzhiyun case FSL_SAI_RTCTN:
847*4882a593Smuzhiyun case FSL_SAI_TTCTL:
848*4882a593Smuzhiyun case FSL_SAI_TBCTN:
849*4882a593Smuzhiyun case FSL_SAI_TTCAP:
850*4882a593Smuzhiyun case FSL_SAI_RTCTL:
851*4882a593Smuzhiyun case FSL_SAI_RBCTN:
852*4882a593Smuzhiyun case FSL_SAI_RTCAP:
853*4882a593Smuzhiyun return true;
854*4882a593Smuzhiyun default:
855*4882a593Smuzhiyun return false;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
fsl_sai_volatile_reg(struct device * dev,unsigned int reg)859*4882a593Smuzhiyun static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
862*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs))
865*4882a593Smuzhiyun return true;
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /* Set VERID and PARAM be volatile for reading value in probe */
868*4882a593Smuzhiyun if (ofs == 8 && (reg == FSL_SAI_VERID || reg == FSL_SAI_PARAM))
869*4882a593Smuzhiyun return true;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun switch (reg) {
872*4882a593Smuzhiyun case FSL_SAI_TFR0:
873*4882a593Smuzhiyun case FSL_SAI_TFR1:
874*4882a593Smuzhiyun case FSL_SAI_TFR2:
875*4882a593Smuzhiyun case FSL_SAI_TFR3:
876*4882a593Smuzhiyun case FSL_SAI_TFR4:
877*4882a593Smuzhiyun case FSL_SAI_TFR5:
878*4882a593Smuzhiyun case FSL_SAI_TFR6:
879*4882a593Smuzhiyun case FSL_SAI_TFR7:
880*4882a593Smuzhiyun case FSL_SAI_RFR0:
881*4882a593Smuzhiyun case FSL_SAI_RFR1:
882*4882a593Smuzhiyun case FSL_SAI_RFR2:
883*4882a593Smuzhiyun case FSL_SAI_RFR3:
884*4882a593Smuzhiyun case FSL_SAI_RFR4:
885*4882a593Smuzhiyun case FSL_SAI_RFR5:
886*4882a593Smuzhiyun case FSL_SAI_RFR6:
887*4882a593Smuzhiyun case FSL_SAI_RFR7:
888*4882a593Smuzhiyun case FSL_SAI_RDR0:
889*4882a593Smuzhiyun case FSL_SAI_RDR1:
890*4882a593Smuzhiyun case FSL_SAI_RDR2:
891*4882a593Smuzhiyun case FSL_SAI_RDR3:
892*4882a593Smuzhiyun case FSL_SAI_RDR4:
893*4882a593Smuzhiyun case FSL_SAI_RDR5:
894*4882a593Smuzhiyun case FSL_SAI_RDR6:
895*4882a593Smuzhiyun case FSL_SAI_RDR7:
896*4882a593Smuzhiyun return true;
897*4882a593Smuzhiyun default:
898*4882a593Smuzhiyun return false;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
fsl_sai_writeable_reg(struct device * dev,unsigned int reg)902*4882a593Smuzhiyun static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
905*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
908*4882a593Smuzhiyun return true;
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
911*4882a593Smuzhiyun return true;
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun switch (reg) {
914*4882a593Smuzhiyun case FSL_SAI_TDR0:
915*4882a593Smuzhiyun case FSL_SAI_TDR1:
916*4882a593Smuzhiyun case FSL_SAI_TDR2:
917*4882a593Smuzhiyun case FSL_SAI_TDR3:
918*4882a593Smuzhiyun case FSL_SAI_TDR4:
919*4882a593Smuzhiyun case FSL_SAI_TDR5:
920*4882a593Smuzhiyun case FSL_SAI_TDR6:
921*4882a593Smuzhiyun case FSL_SAI_TDR7:
922*4882a593Smuzhiyun case FSL_SAI_TMR:
923*4882a593Smuzhiyun case FSL_SAI_RMR:
924*4882a593Smuzhiyun case FSL_SAI_MCTL:
925*4882a593Smuzhiyun case FSL_SAI_MDIV:
926*4882a593Smuzhiyun case FSL_SAI_TTCTL:
927*4882a593Smuzhiyun case FSL_SAI_RTCTL:
928*4882a593Smuzhiyun return true;
929*4882a593Smuzhiyun default:
930*4882a593Smuzhiyun return false;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun static struct regmap_config fsl_sai_regmap_config = {
935*4882a593Smuzhiyun .reg_bits = 32,
936*4882a593Smuzhiyun .reg_stride = 4,
937*4882a593Smuzhiyun .val_bits = 32,
938*4882a593Smuzhiyun .fast_io = true,
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun .max_register = FSL_SAI_RMR,
941*4882a593Smuzhiyun .reg_defaults = fsl_sai_reg_defaults_ofs0,
942*4882a593Smuzhiyun .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0),
943*4882a593Smuzhiyun .readable_reg = fsl_sai_readable_reg,
944*4882a593Smuzhiyun .volatile_reg = fsl_sai_volatile_reg,
945*4882a593Smuzhiyun .writeable_reg = fsl_sai_writeable_reg,
946*4882a593Smuzhiyun .cache_type = REGCACHE_FLAT,
947*4882a593Smuzhiyun };
948*4882a593Smuzhiyun
fsl_sai_check_version(struct device * dev)949*4882a593Smuzhiyun static int fsl_sai_check_version(struct device *dev)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
952*4882a593Smuzhiyun unsigned char ofs = sai->soc_data->reg_offset;
953*4882a593Smuzhiyun unsigned int val;
954*4882a593Smuzhiyun int ret;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun if (FSL_SAI_TCSR(ofs) == FSL_SAI_VERID)
957*4882a593Smuzhiyun return 0;
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun ret = regmap_read(sai->regmap, FSL_SAI_VERID, &val);
960*4882a593Smuzhiyun if (ret < 0)
961*4882a593Smuzhiyun return ret;
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun dev_dbg(dev, "VERID: 0x%016X\n", val);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun sai->verid.major = (val & FSL_SAI_VERID_MAJOR_MASK) >>
966*4882a593Smuzhiyun FSL_SAI_VERID_MAJOR_SHIFT;
967*4882a593Smuzhiyun sai->verid.minor = (val & FSL_SAI_VERID_MINOR_MASK) >>
968*4882a593Smuzhiyun FSL_SAI_VERID_MINOR_SHIFT;
969*4882a593Smuzhiyun sai->verid.feature = val & FSL_SAI_VERID_FEATURE_MASK;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun ret = regmap_read(sai->regmap, FSL_SAI_PARAM, &val);
972*4882a593Smuzhiyun if (ret < 0)
973*4882a593Smuzhiyun return ret;
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun dev_dbg(dev, "PARAM: 0x%016X\n", val);
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun /* Max slots per frame, power of 2 */
978*4882a593Smuzhiyun sai->param.slot_num = 1 <<
979*4882a593Smuzhiyun ((val & FSL_SAI_PARAM_SPF_MASK) >> FSL_SAI_PARAM_SPF_SHIFT);
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun /* Words per fifo, power of 2 */
982*4882a593Smuzhiyun sai->param.fifo_depth = 1 <<
983*4882a593Smuzhiyun ((val & FSL_SAI_PARAM_WPF_MASK) >> FSL_SAI_PARAM_WPF_SHIFT);
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun /* Number of datalines implemented */
986*4882a593Smuzhiyun sai->param.dataline = val & FSL_SAI_PARAM_DLN_MASK;
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun return 0;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun
fsl_sai_probe(struct platform_device * pdev)991*4882a593Smuzhiyun static int fsl_sai_probe(struct platform_device *pdev)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun struct device_node *np = pdev->dev.of_node;
994*4882a593Smuzhiyun struct fsl_sai *sai;
995*4882a593Smuzhiyun struct regmap *gpr;
996*4882a593Smuzhiyun struct resource *res;
997*4882a593Smuzhiyun void __iomem *base;
998*4882a593Smuzhiyun char tmp[8];
999*4882a593Smuzhiyun int irq, ret, i;
1000*4882a593Smuzhiyun int index;
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
1003*4882a593Smuzhiyun if (!sai)
1004*4882a593Smuzhiyun return -ENOMEM;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun sai->pdev = pdev;
1007*4882a593Smuzhiyun sai->soc_data = of_device_get_match_data(&pdev->dev);
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1012*4882a593Smuzhiyun base = devm_ioremap_resource(&pdev->dev, res);
1013*4882a593Smuzhiyun if (IS_ERR(base))
1014*4882a593Smuzhiyun return PTR_ERR(base);
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun if (sai->soc_data->reg_offset == 8) {
1017*4882a593Smuzhiyun fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8;
1018*4882a593Smuzhiyun fsl_sai_regmap_config.max_register = FSL_SAI_MDIV;
1019*4882a593Smuzhiyun fsl_sai_regmap_config.num_reg_defaults =
1020*4882a593Smuzhiyun ARRAY_SIZE(fsl_sai_reg_defaults_ofs8);
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1024*4882a593Smuzhiyun "bus", base, &fsl_sai_regmap_config);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun /* Compatible with old DTB cases */
1027*4882a593Smuzhiyun if (IS_ERR(sai->regmap) && PTR_ERR(sai->regmap) != -EPROBE_DEFER)
1028*4882a593Smuzhiyun sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
1029*4882a593Smuzhiyun "sai", base, &fsl_sai_regmap_config);
1030*4882a593Smuzhiyun if (IS_ERR(sai->regmap)) {
1031*4882a593Smuzhiyun dev_err(&pdev->dev, "regmap init failed\n");
1032*4882a593Smuzhiyun return PTR_ERR(sai->regmap);
1033*4882a593Smuzhiyun }
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun /* No error out for old DTB cases but only mark the clock NULL */
1036*4882a593Smuzhiyun sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
1037*4882a593Smuzhiyun if (IS_ERR(sai->bus_clk)) {
1038*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
1039*4882a593Smuzhiyun PTR_ERR(sai->bus_clk));
1040*4882a593Smuzhiyun sai->bus_clk = NULL;
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun sai->mclk_clk[0] = sai->bus_clk;
1044*4882a593Smuzhiyun for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
1045*4882a593Smuzhiyun sprintf(tmp, "mclk%d", i);
1046*4882a593Smuzhiyun sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
1047*4882a593Smuzhiyun if (IS_ERR(sai->mclk_clk[i])) {
1048*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
1049*4882a593Smuzhiyun i + 1, PTR_ERR(sai->mclk_clk[i]));
1050*4882a593Smuzhiyun sai->mclk_clk[i] = NULL;
1051*4882a593Smuzhiyun }
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
1055*4882a593Smuzhiyun if (irq < 0)
1056*4882a593Smuzhiyun return irq;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, IRQF_SHARED,
1059*4882a593Smuzhiyun np->name, sai);
1060*4882a593Smuzhiyun if (ret) {
1061*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
1062*4882a593Smuzhiyun return ret;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun memcpy(&sai->cpu_dai_drv, &fsl_sai_dai_template,
1066*4882a593Smuzhiyun sizeof(fsl_sai_dai_template));
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun /* Sync Tx with Rx as default by following old DT binding */
1069*4882a593Smuzhiyun sai->synchronous[RX] = true;
1070*4882a593Smuzhiyun sai->synchronous[TX] = false;
1071*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_rates = 1;
1072*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_channels = 1;
1073*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_samplebits = 1;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
1076*4882a593Smuzhiyun of_find_property(np, "fsl,sai-asynchronous", NULL)) {
1077*4882a593Smuzhiyun /* error out if both synchronous and asynchronous are present */
1078*4882a593Smuzhiyun dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
1079*4882a593Smuzhiyun return -EINVAL;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun
1082*4882a593Smuzhiyun if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
1083*4882a593Smuzhiyun /* Sync Rx with Tx */
1084*4882a593Smuzhiyun sai->synchronous[RX] = false;
1085*4882a593Smuzhiyun sai->synchronous[TX] = true;
1086*4882a593Smuzhiyun } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
1087*4882a593Smuzhiyun /* Discard all settings for asynchronous mode */
1088*4882a593Smuzhiyun sai->synchronous[RX] = false;
1089*4882a593Smuzhiyun sai->synchronous[TX] = false;
1090*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_rates = 0;
1091*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_channels = 0;
1092*4882a593Smuzhiyun sai->cpu_dai_drv.symmetric_samplebits = 0;
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
1096*4882a593Smuzhiyun of_device_is_compatible(np, "fsl,imx6ul-sai")) {
1097*4882a593Smuzhiyun gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
1098*4882a593Smuzhiyun if (IS_ERR(gpr)) {
1099*4882a593Smuzhiyun dev_err(&pdev->dev, "cannot find iomuxc registers\n");
1100*4882a593Smuzhiyun return PTR_ERR(gpr);
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun index = of_alias_get_id(np, "sai");
1104*4882a593Smuzhiyun if (index < 0)
1105*4882a593Smuzhiyun return index;
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
1108*4882a593Smuzhiyun MCLK_DIR(index));
1109*4882a593Smuzhiyun }
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun sai->dma_params_rx.addr = res->start + FSL_SAI_RDR0;
1112*4882a593Smuzhiyun sai->dma_params_tx.addr = res->start + FSL_SAI_TDR0;
1113*4882a593Smuzhiyun sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
1114*4882a593Smuzhiyun sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun platform_set_drvdata(pdev, sai);
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun /* Get sai version */
1119*4882a593Smuzhiyun ret = fsl_sai_check_version(&pdev->dev);
1120*4882a593Smuzhiyun if (ret < 0)
1121*4882a593Smuzhiyun dev_warn(&pdev->dev, "Error reading SAI version: %d\n", ret);
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /* Select MCLK direction */
1124*4882a593Smuzhiyun if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
1125*4882a593Smuzhiyun sai->verid.major >= 3 && sai->verid.minor >= 1) {
1126*4882a593Smuzhiyun regmap_update_bits(sai->regmap, FSL_SAI_MCTL,
1127*4882a593Smuzhiyun FSL_SAI_MCTL_MCLK_EN, FSL_SAI_MCTL_MCLK_EN);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun pm_runtime_enable(&pdev->dev);
1131*4882a593Smuzhiyun regcache_cache_only(sai->regmap, true);
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
1134*4882a593Smuzhiyun &sai->cpu_dai_drv, 1);
1135*4882a593Smuzhiyun if (ret)
1136*4882a593Smuzhiyun goto err_pm_disable;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun if (sai->soc_data->use_imx_pcm) {
1139*4882a593Smuzhiyun ret = imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
1140*4882a593Smuzhiyun if (ret)
1141*4882a593Smuzhiyun goto err_pm_disable;
1142*4882a593Smuzhiyun } else {
1143*4882a593Smuzhiyun ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1144*4882a593Smuzhiyun if (ret)
1145*4882a593Smuzhiyun goto err_pm_disable;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun return ret;
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun err_pm_disable:
1151*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun return ret;
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun
fsl_sai_remove(struct platform_device * pdev)1156*4882a593Smuzhiyun static int fsl_sai_remove(struct platform_device *pdev)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun return 0;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun static const struct fsl_sai_soc_data fsl_sai_vf610_data = {
1164*4882a593Smuzhiyun .use_imx_pcm = false,
1165*4882a593Smuzhiyun .use_edma = false,
1166*4882a593Smuzhiyun .fifo_depth = 32,
1167*4882a593Smuzhiyun .reg_offset = 0,
1168*4882a593Smuzhiyun };
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = {
1171*4882a593Smuzhiyun .use_imx_pcm = true,
1172*4882a593Smuzhiyun .use_edma = false,
1173*4882a593Smuzhiyun .fifo_depth = 32,
1174*4882a593Smuzhiyun .reg_offset = 0,
1175*4882a593Smuzhiyun };
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = {
1178*4882a593Smuzhiyun .use_imx_pcm = true,
1179*4882a593Smuzhiyun .use_edma = false,
1180*4882a593Smuzhiyun .fifo_depth = 16,
1181*4882a593Smuzhiyun .reg_offset = 8,
1182*4882a593Smuzhiyun };
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = {
1185*4882a593Smuzhiyun .use_imx_pcm = true,
1186*4882a593Smuzhiyun .use_edma = false,
1187*4882a593Smuzhiyun .fifo_depth = 128,
1188*4882a593Smuzhiyun .reg_offset = 8,
1189*4882a593Smuzhiyun };
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = {
1192*4882a593Smuzhiyun .use_imx_pcm = true,
1193*4882a593Smuzhiyun .use_edma = true,
1194*4882a593Smuzhiyun .fifo_depth = 64,
1195*4882a593Smuzhiyun .reg_offset = 0,
1196*4882a593Smuzhiyun };
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun static const struct of_device_id fsl_sai_ids[] = {
1199*4882a593Smuzhiyun { .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data },
1200*4882a593Smuzhiyun { .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data },
1201*4882a593Smuzhiyun { .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data },
1202*4882a593Smuzhiyun { .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data },
1203*4882a593Smuzhiyun { .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data },
1204*4882a593Smuzhiyun { .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data },
1205*4882a593Smuzhiyun { /* sentinel */ }
1206*4882a593Smuzhiyun };
1207*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, fsl_sai_ids);
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun #ifdef CONFIG_PM
fsl_sai_runtime_suspend(struct device * dev)1210*4882a593Smuzhiyun static int fsl_sai_runtime_suspend(struct device *dev)
1211*4882a593Smuzhiyun {
1212*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1215*4882a593Smuzhiyun clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1218*4882a593Smuzhiyun clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun clk_disable_unprepare(sai->bus_clk);
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun regcache_cache_only(sai->regmap, true);
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun return 0;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun
fsl_sai_runtime_resume(struct device * dev)1227*4882a593Smuzhiyun static int fsl_sai_runtime_resume(struct device *dev)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun struct fsl_sai *sai = dev_get_drvdata(dev);
1230*4882a593Smuzhiyun unsigned int ofs = sai->soc_data->reg_offset;
1231*4882a593Smuzhiyun int ret;
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun ret = clk_prepare_enable(sai->bus_clk);
1234*4882a593Smuzhiyun if (ret) {
1235*4882a593Smuzhiyun dev_err(dev, "failed to enable bus clock: %d\n", ret);
1236*4882a593Smuzhiyun return ret;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) {
1240*4882a593Smuzhiyun ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]);
1241*4882a593Smuzhiyun if (ret)
1242*4882a593Smuzhiyun goto disable_bus_clk;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) {
1246*4882a593Smuzhiyun ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]);
1247*4882a593Smuzhiyun if (ret)
1248*4882a593Smuzhiyun goto disable_tx_clk;
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun regcache_cache_only(sai->regmap, false);
1252*4882a593Smuzhiyun regcache_mark_dirty(sai->regmap);
1253*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
1254*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
1255*4882a593Smuzhiyun usleep_range(1000, 2000);
1256*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
1257*4882a593Smuzhiyun regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun ret = regcache_sync(sai->regmap);
1260*4882a593Smuzhiyun if (ret)
1261*4882a593Smuzhiyun goto disable_rx_clk;
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun return 0;
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun disable_rx_clk:
1266*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1267*4882a593Smuzhiyun clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1268*4882a593Smuzhiyun disable_tx_clk:
1269*4882a593Smuzhiyun if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1270*4882a593Smuzhiyun clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1271*4882a593Smuzhiyun disable_bus_clk:
1272*4882a593Smuzhiyun clk_disable_unprepare(sai->bus_clk);
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun return ret;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun #endif /* CONFIG_PM */
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun static const struct dev_pm_ops fsl_sai_pm_ops = {
1279*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(fsl_sai_runtime_suspend,
1280*4882a593Smuzhiyun fsl_sai_runtime_resume, NULL)
1281*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1282*4882a593Smuzhiyun pm_runtime_force_resume)
1283*4882a593Smuzhiyun };
1284*4882a593Smuzhiyun
1285*4882a593Smuzhiyun static struct platform_driver fsl_sai_driver = {
1286*4882a593Smuzhiyun .probe = fsl_sai_probe,
1287*4882a593Smuzhiyun .remove = fsl_sai_remove,
1288*4882a593Smuzhiyun .driver = {
1289*4882a593Smuzhiyun .name = "fsl-sai",
1290*4882a593Smuzhiyun .pm = &fsl_sai_pm_ops,
1291*4882a593Smuzhiyun .of_match_table = fsl_sai_ids,
1292*4882a593Smuzhiyun },
1293*4882a593Smuzhiyun };
1294*4882a593Smuzhiyun module_platform_driver(fsl_sai_driver);
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale Soc SAI Interface");
1297*4882a593Smuzhiyun MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
1298*4882a593Smuzhiyun MODULE_ALIAS("platform:fsl-sai");
1299*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1300