Debian Codegraph @

sound/soc/fsl/fsl_sai.c

raw
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Freescale ALSA SoC Digital Audio Interface (SAI) driver.
4 //
5 // Copyright 2012-2015 Freescale Semiconductor, Inc.
6
7 #include <linux/clk.h>
8 #include <linux/delay.h>
9 #include <linux/dmaengine.h>
10 #include <linux/firmware/imx/sm.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/pinctrl/consumer.h>
14 #include <linux/pm_qos.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <linux/time.h>
19 #include <sound/core.h>
20 #include <sound/dmaengine_pcm.h>
21 #include <sound/pcm_params.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
24
25 #include "fsl_sai.h"
26 #include "fsl_utils.h"
27 #include "imx-pcm.h"
28
29 #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
30 FSL_SAI_CSR_FEIE)
31
32 static const unsigned int fsl_sai_rates[] = {
33 8000, 11025, 12000, 16000, 22050,
34 24000, 32000, 44100, 48000, 64000,
35 88200, 96000, 176400, 192000, 352800,
36 384000, 705600, 768000, 1411200, 2822400,
37 };
38
39 static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
40 .count = ARRAY_SIZE(fsl_sai_rates),
41 .list = fsl_sai_rates,
42 };
43
44 /**
45 * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream
46 *
47 * SAI supports synchronous mode using bit/frame clocks of either Transmitter's
48 * or Receiver's for both streams. This function is used to check if clocks of
49 * the stream's are synced by the opposite stream.
50 *
51 * @sai: SAI context
52 * @dir: stream direction
53 */
54 static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir)
55 {
56 int adir = (dir == TX) ? RX : TX;
57
58 /* current dir in async mode while opposite dir in sync mode */
59 return !sai->synchronous[dir] && sai->synchronous[adir];
60 }
61
62 static struct pinctrl_state *fsl_sai_get_pins_state(struct fsl_sai *sai, u32 bclk)
63 {
64 struct pinctrl_state *state = NULL;
65
66 if (sai->is_pdm_mode) {
67 /* [email protected], DSD512@48kHz */
68 if (bclk >= 22579200)
69 state = pinctrl_lookup_state(sai->pinctrl, "dsd512");
70
71 /* Get default DSD state */
72 if (IS_ERR_OR_NULL(state))
73 state = pinctrl_lookup_state(sai->pinctrl, "dsd");
74 } else {
75 /* 706k32b2c, 768k32b2c, etc */
76 if (bclk >= 45158400)
77 state = pinctrl_lookup_state(sai->pinctrl, "pcm_b2m");
78 }
79
80 /* Get default state */
81 if (IS_ERR_OR_NULL(state))
82 state = pinctrl_lookup_state(sai->pinctrl, "default");
83
84 return state;
85 }
86
87 static irqreturn_t fsl_sai_isr(int irq, void *devid)
88 {
89 struct fsl_sai *sai = (struct fsl_sai *)devid;
90 unsigned int ofs = sai->soc_data->reg_offset;
91 struct device *dev = &sai->pdev->dev;
92 u32 flags, xcsr, mask;
93 irqreturn_t iret = IRQ_NONE;
94
95 /*
96 * Both IRQ status bits and IRQ mask bits are in the xCSR but
97 * different shifts. And we here create a mask only for those
98 * IRQs that we activated.
99 */
100 mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
101
102 /* Tx IRQ */
103 regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr);
104 flags = xcsr & mask;
105
106 if (flags)
107 iret = IRQ_HANDLED;
108 else
109 goto irq_rx;
110
111 if (flags & FSL_SAI_CSR_WSF)
112 dev_dbg(dev, "isr: Start of Tx word detected\n");
113
114 if (flags & FSL_SAI_CSR_SEF)
115 dev_dbg(dev, "isr: Tx Frame sync error detected\n");
116
117 if (flags & FSL_SAI_CSR_FEF)
118 dev_dbg(dev, "isr: Transmit underrun detected\n");
119
120 if (flags & FSL_SAI_CSR_FWF)
121 dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
122
123 if (flags & FSL_SAI_CSR_FRF)
124 dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
125
126 flags &= FSL_SAI_CSR_xF_W_MASK;
127 xcsr &= ~FSL_SAI_CSR_xF_MASK;
128
129 if (flags)
130 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr);
131
132 irq_rx:
133 /* Rx IRQ */
134 regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr);
135 flags = xcsr & mask;
136
137 if (flags)
138 iret = IRQ_HANDLED;
139 else
140 goto out;
141
142 if (flags & FSL_SAI_CSR_WSF)
143 dev_dbg(dev, "isr: Start of Rx word detected\n");
144
145 if (flags & FSL_SAI_CSR_SEF)
146 dev_dbg(dev, "isr: Rx Frame sync error detected\n");
147
148 if (flags & FSL_SAI_CSR_FEF)
149 dev_dbg(dev, "isr: Receive overflow detected\n");
150
151 if (flags & FSL_SAI_CSR_FWF)
152 dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
153
154 if (flags & FSL_SAI_CSR_FRF)
155 dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
156
157 flags &= FSL_SAI_CSR_xF_W_MASK;
158 xcsr &= ~FSL_SAI_CSR_xF_MASK;
159
160 if (flags)
161 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr);
162
163 out:
164 return iret;
165 }
166
167 static int fsl_sai_set_dai_tdm_slot_tx(struct snd_soc_dai *cpu_dai, u32 tx_mask,
168 u32 rx_mask, int slots, int slot_width)
169 {
170 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
171 bool tx = true;
172
173 sai->slots[tx] = slots;
174 sai->slot_width[tx] = slot_width;
175
176 return 0;
177 }
178
179 static int fsl_sai_set_dai_tdm_slot_rx(struct snd_soc_dai *cpu_dai, u32 tx_mask,
180 u32 rx_mask, int slots, int slot_width)
181 {
182 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
183 bool tx = false;
184
185 sai->slots[tx] = slots;
186 sai->slot_width[tx] = slot_width;
187
188 return 0;
189 }
190
191 static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
192 u32 rx_mask, int slots, int slot_width)
193 {
194 int ret;
195
196 ret = fsl_sai_set_dai_tdm_slot_tx(cpu_dai, tx_mask, rx_mask, slots, slot_width);
197 if (ret)
198 return ret;
199
200 return fsl_sai_set_dai_tdm_slot_rx(cpu_dai, tx_mask, rx_mask, slots, slot_width);
201 }
202
203 static int fsl_sai_xlate_tdm_slot_mask(unsigned int slots,
204 unsigned int *tx_mask, unsigned int *rx_mask)
205 {
206 /* Leave it empty, don't change the value of tx_mask and rx_mask */
207 return 0;
208 }
209
210 static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai,
211 unsigned int ratio)
212 {
213 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
214
215 sai->bclk_ratio = ratio;
216
217 return 0;
218 }
219
220 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
221 int clk_id, unsigned int freq, bool tx)
222 {
223 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
224 unsigned int ofs = sai->soc_data->reg_offset;
225 u32 val_cr2 = 0;
226
227 switch (clk_id) {
228 case FSL_SAI_CLK_BUS:
229 val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
230 break;
231 case FSL_SAI_CLK_MAST1:
232 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
233 break;
234 case FSL_SAI_CLK_MAST2:
235 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
236 break;
237 case FSL_SAI_CLK_MAST3:
238 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
239 break;
240 default:
241 return -EINVAL;
242 }
243
244 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
245 FSL_SAI_CR2_MSEL_MASK, val_cr2);
246
247 return 0;
248 }
249
250 static int fsl_sai_set_mclk_rate(struct snd_soc_dai *dai, int clk_id, unsigned int freq)
251 {
252 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
253 int ret;
254
255 fsl_asoc_reparent_pll_clocks(dai->dev, sai->mclk_clk[clk_id],
256 sai->pll8k_clk, sai->pll11k_clk, freq);
257
258 ret = clk_set_rate(sai->mclk_clk[clk_id], freq);
259 if (ret < 0)
260 dev_err(dai->dev, "failed to set clock rate (%u): %d\n", freq, ret);
261
262 return ret;
263 }
264
265 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
266 int clk_id, unsigned int freq, int dir)
267 {
268 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
269 int ret;
270
271 if (dir == SND_SOC_CLOCK_IN)
272 return 0;
273
274 if (clk_id < 0 || clk_id >= FSL_SAI_MCLK_MAX) {
275 dev_err(cpu_dai->dev, "Unknown clock id: %d\n", clk_id);
276 return -EINVAL;
277 }
278
279 if (IS_ERR_OR_NULL(sai->mclk_clk[clk_id])) {
280 dev_err(cpu_dai->dev, "Unassigned clock: %d\n", clk_id);
281 return -EINVAL;
282 }
283
284 if (sai->mclk_streams == 0 && freq > 0) {
285 ret = fsl_sai_set_mclk_rate(cpu_dai,
286 clk_id ? clk_id : FSL_SAI_CLK_MAST1,
287 freq);
288 if (ret < 0)
289 return ret;
290 }
291
292 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, true);
293 if (ret) {
294 dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
295 return ret;
296 }
297
298 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, false);
299 if (ret)
300 dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
301
302 return ret;
303 }
304
305 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
306 unsigned int fmt, bool tx)
307 {
308 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
309 unsigned int ofs = sai->soc_data->reg_offset;
310 u32 val_cr2 = 0, val_cr4 = 0;
311
312 if (!sai->is_lsb_first)
313 val_cr4 |= FSL_SAI_CR4_MF;
314
315 sai->is_pdm_mode = false;
316 sai->is_dsp_mode[tx] = false;
317 /* DAI mode */
318 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
319 case SND_SOC_DAIFMT_I2S:
320 /*
321 * Frame low, 1clk before data, one word length for frame sync,
322 * frame sync starts one serial clock cycle earlier,
323 * that is, together with the last bit of the previous
324 * data word.
325 */
326 val_cr2 |= FSL_SAI_CR2_BCP;
327 val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
328 break;
329 case SND_SOC_DAIFMT_LEFT_J:
330 /*
331 * Frame high, one word length for frame sync,
332 * frame sync asserts with the first bit of the frame.
333 */
334 val_cr2 |= FSL_SAI_CR2_BCP;
335 break;
336 case SND_SOC_DAIFMT_DSP_A:
337 /*
338 * Frame high, 1clk before data, one bit for frame sync,
339 * frame sync starts one serial clock cycle earlier,
340 * that is, together with the last bit of the previous
341 * data word.
342 */
343 val_cr2 |= FSL_SAI_CR2_BCP;
344 val_cr4 |= FSL_SAI_CR4_FSE;
345 sai->is_dsp_mode[tx] = true;
346 break;
347 case SND_SOC_DAIFMT_DSP_B:
348 /*
349 * Frame high, one bit for frame sync,
350 * frame sync asserts with the first bit of the frame.
351 */
352 val_cr2 |= FSL_SAI_CR2_BCP;
353 sai->is_dsp_mode[tx] = true;
354 break;
355 case SND_SOC_DAIFMT_PDM:
356 val_cr2 |= FSL_SAI_CR2_BCP;
357 sai->is_pdm_mode = true;
358 break;
359 case SND_SOC_DAIFMT_RIGHT_J:
360 /* To be done */
361 default:
362 return -EINVAL;
363 }
364
365 /* DAI clock inversion */
366 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
367 case SND_SOC_DAIFMT_IB_IF:
368 /* Invert both clocks */
369 val_cr2 ^= FSL_SAI_CR2_BCP;
370 val_cr4 ^= FSL_SAI_CR4_FSP;
371 break;
372 case SND_SOC_DAIFMT_IB_NF:
373 /* Invert bit clock */
374 val_cr2 ^= FSL_SAI_CR2_BCP;
375 break;
376 case SND_SOC_DAIFMT_NB_IF:
377 /* Invert frame clock */
378 val_cr4 ^= FSL_SAI_CR4_FSP;
379 break;
380 case SND_SOC_DAIFMT_NB_NF:
381 /* Nothing to do for both normal cases */
382 break;
383 default:
384 return -EINVAL;
385 }
386
387 /* DAI clock provider masks */
388 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
389 case SND_SOC_DAIFMT_BP_FP:
390 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
391 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
392 sai->is_consumer_mode[tx] = false;
393 break;
394 case SND_SOC_DAIFMT_BC_FC:
395 sai->is_consumer_mode[tx] = true;
396 break;
397 case SND_SOC_DAIFMT_BP_FC:
398 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
399 sai->is_consumer_mode[tx] = false;
400 break;
401 case SND_SOC_DAIFMT_BC_FP:
402 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
403 sai->is_consumer_mode[tx] = true;
404 break;
405 default:
406 return -EINVAL;
407 }
408
409 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
410 FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
411 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
412 FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
413 FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
414
415 return 0;
416 }
417
418 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
419 {
420 int ret;
421
422 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, true);
423 if (ret) {
424 dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
425 return ret;
426 }
427
428 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, false);
429 if (ret)
430 dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
431
432 return ret;
433 }
434
435 static int fsl_sai_set_dai_fmt_tx(struct snd_soc_dai *cpu_dai, unsigned int fmt)
436 {
437 return fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, true);
438 }
439
440 static int fsl_sai_set_dai_fmt_rx(struct snd_soc_dai *cpu_dai, unsigned int fmt)
441 {
442 return fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, false);
443 }
444
445 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
446 {
447 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
448 unsigned int reg, ofs = sai->soc_data->reg_offset;
449 unsigned long clk_rate;
450 u32 savediv = 0, ratio, bestdiff = freq;
451 int adir = tx ? RX : TX;
452 int dir = tx ? TX : RX;
453 u32 id;
454 bool support_1_1_ratio = sai->verid.version >= 0x0301;
455
456 /* Don't apply to consumer mode */
457 if (sai->is_consumer_mode[tx])
458 return 0;
459
460 /*
461 * There is no point in polling MCLK0 if it is identical to MCLK1.
462 * And given that MQS use case has to use MCLK1 though two clocks
463 * are the same, we simply skip MCLK0 and start to find from MCLK1.
464 */
465 id = sai->soc_data->mclk0_is_mclk1 ? 1 : 0;
466
467 for (; id < FSL_SAI_MCLK_MAX; id++) {
468 int diff;
469
470 clk_rate = clk_get_rate(sai->mclk_clk[id]);
471 if (!clk_rate)
472 continue;
473
474 ratio = DIV_ROUND_CLOSEST(clk_rate, freq);
475 if (!ratio || ratio > 512)
476 continue;
477 if (ratio == 1 && !support_1_1_ratio)
478 continue;
479 if ((ratio & 1) && ratio > 1)
480 continue;
481
482 diff = abs((long)clk_rate - ratio * freq);
483
484 /*
485 * Drop the source that can not be
486 * divided into the required rate.
487 */
488 if (diff != 0 && clk_rate / diff < 1000)
489 continue;
490
491 dev_dbg(dai->dev,
492 "ratio %d for freq %dHz based on clock %ldHz\n",
493 ratio, freq, clk_rate);
494
495
496 if (diff < bestdiff) {
497 savediv = ratio;
498 sai->mclk_id[tx] = id;
499 bestdiff = diff;
500 }
501
502 if (diff == 0)
503 break;
504 }
505
506 if (savediv == 0) {
507 dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
508 tx ? 'T' : 'R', freq);
509 return -EINVAL;
510 }
511
512 dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
513 sai->mclk_id[tx], savediv, bestdiff);
514
515 /*
516 * 1) For Asynchronous mode, we must set RCR2 register for capture, and
517 * set TCR2 register for playback.
518 * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
519 * and capture.
520 * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
521 * and capture.
522 * 4) For Tx and Rx are both Synchronous with another SAI, we just
523 * ignore it.
524 */
525 if (fsl_sai_dir_is_synced(sai, adir))
526 reg = FSL_SAI_xCR2(!tx, ofs);
527 else if (!sai->synchronous[dir])
528 reg = FSL_SAI_xCR2(tx, ofs);
529 else
530 return 0;
531
532 regmap_update_bits(sai->regmap, reg, FSL_SAI_CR2_MSEL_MASK,
533 FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
534
535 if (savediv == 1) {
536 regmap_update_bits(sai->regmap, reg,
537 FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
538 FSL_SAI_CR2_BYP);
539 if (fsl_sai_dir_is_synced(sai, adir))
540 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
541 FSL_SAI_CR2_BCI, FSL_SAI_CR2_BCI);
542 else
543 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
544 FSL_SAI_CR2_BCI, 0);
545 } else {
546 regmap_update_bits(sai->regmap, reg,
547 FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
548 savediv / 2 - 1);
549 }
550
551 return 0;
552 }
553
554 static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params *params,
556 struct snd_soc_dai *cpu_dai)
557 {
558 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
559 unsigned int ofs = sai->soc_data->reg_offset;
560 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
561 unsigned int channels = params_channels(params);
562 struct snd_dmaengine_dai_dma_data *dma_params;
563 struct fsl_sai_dl_cfg *dl_cfg = sai->dl_cfg;
564 u32 word_width = params_width(params);
565 int trce_mask = 0, dl_cfg_idx = 0;
566 int dl_cfg_cnt = sai->dl_cfg_cnt;
567 u32 dl_type = FSL_SAI_DL_I2S;
568 u32 val_cr4 = 0, val_cr5 = 0;
569 u32 slots = (channels == 1) ? 2 : channels;
570 u32 slot_width = word_width;
571 int adir = tx ? RX : TX;
572 u32 pins, bclk;
573 u32 watermark;
574 int ret, i;
575
576 if (sai->slot_width[tx])
577 slot_width = sai->slot_width[tx];
578
579 if (sai->slots[tx])
580 slots = sai->slots[tx];
581 else if (sai->bclk_ratio)
582 slots = sai->bclk_ratio / slot_width;
583
584 pins = DIV_ROUND_UP(channels, slots);
585
586 /*
587 * PDM mode, channels are independent
588 * each channels are on one dataline/FIFO.
589 */
590 if (sai->is_pdm_mode) {
591 pins = channels;
592 dl_type = FSL_SAI_DL_PDM;
593 }
594
595 for (i = 0; i < dl_cfg_cnt; i++) {
596 if (dl_cfg[i].type == dl_type && dl_cfg[i].pins[tx] == pins) {
597 dl_cfg_idx = i;
598 break;
599 }
600 }
601
602 if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) < pins) {
603 dev_err(cpu_dai->dev, "channel not supported\n");
604 return -EINVAL;
605 }
606
607 bclk = params_rate(params) * (sai->bclk_ratio ? sai->bclk_ratio : slots * slot_width);
608
609 if (!IS_ERR_OR_NULL(sai->pinctrl)) {
610 sai->pins_state = fsl_sai_get_pins_state(sai, bclk);
611 if (!IS_ERR_OR_NULL(sai->pins_state)) {
612 ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
613 if (ret) {
614 dev_err(cpu_dai->dev, "failed to set proper pins state: %d\n", ret);
615 return ret;
616 }
617 }
618 }
619
620 if (!sai->is_consumer_mode[tx]) {
621 ret = fsl_sai_set_bclk(cpu_dai, tx, bclk);
622 if (ret)
623 return ret;
624
625 /* Do not enable the clock if it is already enabled */
626 if (!(sai->mclk_streams & BIT(substream->stream))) {
627 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
628 if (ret)
629 return ret;
630
631 sai->mclk_streams |= BIT(substream->stream);
632 }
633 }
634
635 if (!sai->is_dsp_mode[tx] && !sai->is_pdm_mode)
636 val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
637
638 val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
639 val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
640
641 if (sai->is_lsb_first)
642 val_cr5 |= FSL_SAI_CR5_FBT(0);
643 else
644 val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
645
646 val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
647
648 /* Set to avoid channel swap */
649 val_cr4 |= FSL_SAI_CR4_FCONT;
650
651 /* Set to output mode to avoid tri-stated data pins */
652 if (tx)
653 val_cr4 |= FSL_SAI_CR4_CHMOD;
654
655 /*
656 * When Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will provide bclk and
657 * frame clock for Tx(Rx). We should set RCR4(TCR4), RCR5(TCR5)
658 * for playback(capture), or there will be sync error.
659 */
660
661 if (fsl_sai_dir_is_synced(sai, adir)) {
662 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs),
663 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
664 FSL_SAI_CR4_CHMOD_MASK,
665 val_cr4);
666 regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs),
667 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
668 FSL_SAI_CR5_FBT_MASK, val_cr5);
669 }
670
671 /*
672 * Combine mode has limation:
673 * - Can't used for singel dataline/FIFO case except the FIFO0
674 * - Can't used for multi dataline/FIFO case except the enabled FIFOs
675 * are successive and start from FIFO0
676 *
677 * So for common usage, all multi fifo case disable the combine mode.
678 */
679 if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) <= 1 || sai->is_multi_fifo_dma)
680 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
681 FSL_SAI_CR4_FCOMB_MASK, 0);
682 else
683 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
684 FSL_SAI_CR4_FCOMB_MASK, FSL_SAI_CR4_FCOMB_SOFT);
685
686 dma_params = tx ? &sai->dma_params_tx : &sai->dma_params_rx;
687 dma_params->addr = sai->res->start + FSL_SAI_xDR0(tx) +
688 dl_cfg[dl_cfg_idx].start_off[tx] * 0x4;
689
690 if (sai->is_multi_fifo_dma) {
691 sai->audio_config[tx].words_per_fifo = min(slots, channels);
692 if (tx) {
693 sai->audio_config[tx].n_fifos_dst = pins;
694 sai->audio_config[tx].stride_fifos_dst = dl_cfg[dl_cfg_idx].next_off[tx];
695 } else {
696 sai->audio_config[tx].n_fifos_src = pins;
697 sai->audio_config[tx].stride_fifos_src = dl_cfg[dl_cfg_idx].next_off[tx];
698 }
699 dma_params->maxburst = sai->audio_config[tx].words_per_fifo * pins;
700 dma_params->peripheral_config = &sai->audio_config[tx];
701 dma_params->peripheral_size = sizeof(sai->audio_config[tx]);
702
703 watermark = tx ? (sai->soc_data->fifo_depth - dma_params->maxburst) :
704 (dma_params->maxburst - 1);
705 regmap_update_bits(sai->regmap, FSL_SAI_xCR1(tx, ofs),
706 FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
707 watermark);
708 }
709
710 /* Find a proper tcre setting */
711 for (i = 0; i < sai->soc_data->pins; i++) {
712 trce_mask = (1 << (i + 1)) - 1;
713 if (hweight8(dl_cfg[dl_cfg_idx].mask[tx] & trce_mask) == pins)
714 break;
715 }
716
717 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
718 FSL_SAI_CR3_TRCE_MASK,
719 FSL_SAI_CR3_TRCE((dl_cfg[dl_cfg_idx].mask[tx] & trce_mask)));
720
721 /*
722 * When the TERE and FSD_MSTR enabled before configuring the word width
723 * There will be no frame sync clock issue, because word width impact
724 * the generation of frame sync clock.
725 *
726 * TERE enabled earlier only for i.MX8MP case for the hardware limitation,
727 * We need to disable FSD_MSTR before configuring word width, then enable
728 * FSD_MSTR bit for this specific case.
729 */
730 if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
731 !sai->is_consumer_mode[tx])
732 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
733 FSL_SAI_CR4_FSD_MSTR, 0);
734
735 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
736 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
737 FSL_SAI_CR4_CHMOD_MASK | FSL_SAI_CR4_FCONT_MASK,
738 val_cr4);
739 regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs),
740 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
741 FSL_SAI_CR5_FBT_MASK, val_cr5);
742
743 /* Enable FSD_MSTR after configuring word width */
744 if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output &&
745 !sai->is_consumer_mode[tx])
746 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
747 FSL_SAI_CR4_FSD_MSTR, FSL_SAI_CR4_FSD_MSTR);
748
749 regmap_write(sai->regmap, FSL_SAI_xMR(tx),
750 ~0UL - ((1 << min(channels, slots)) - 1));
751
752 return 0;
753 }
754
755 static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
756 struct snd_soc_dai *cpu_dai)
757 {
758 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
759 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
760 unsigned int ofs = sai->soc_data->reg_offset;
761
762 /* Clear xMR to avoid channel swap with mclk_with_tere enabled case */
763 regmap_write(sai->regmap, FSL_SAI_xMR(tx), 0);
764
765 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
766 FSL_SAI_CR3_TRCE_MASK, 0);
767
768 if (!sai->is_consumer_mode[tx] &&
769 sai->mclk_streams & BIT(substream->stream)) {
770 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
771 sai->mclk_streams &= ~BIT(substream->stream);
772 }
773
774 return 0;
775 }
776
777 static void fsl_sai_config_disable(struct fsl_sai *sai, int dir)
778 {
779 unsigned int ofs = sai->soc_data->reg_offset;
780 bool tx = dir == TX;
781 u32 xcsr, count = 100, mask;
782
783 if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
784 mask = FSL_SAI_CSR_TERE;
785 else
786 mask = FSL_SAI_CSR_TERE | FSL_SAI_CSR_BCE;
787
788 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
789 mask, 0);
790
791 /* TERE will remain set till the end of current frame */
792 do {
793 udelay(10);
794 regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr);
795 } while (--count && xcsr & FSL_SAI_CSR_TERE);
796
797 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
798 FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
799
800 /*
801 * For sai master mode, after several open/close sai,
802 * there will be no frame clock, and can't recover
803 * anymore. Add software reset to fix this issue.
804 * This is a hardware bug, and will be fix in the
805 * next sai version.
806 *
807 * In consumer mode, this can happen even after a
808 * single open/close, especially if both tx and rx
809 * are running concurrently.
810 */
811 /* Software Reset */
812 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR, FSL_SAI_CSR_SR);
813 /* Clear SR bit to finish the reset */
814 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR, 0);
815 }
816
817 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
818 struct snd_soc_dai *cpu_dai)
819 {
820 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
821 unsigned int ofs = sai->soc_data->reg_offset;
822
823 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
824 int adir = tx ? RX : TX;
825 int dir = tx ? TX : RX;
826 u32 xcsr;
827
828 /*
829 * Asynchronous mode: Clear SYNC for both Tx and Rx.
830 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
831 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
832 */
833 regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC,
834 sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
835 regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC,
836 sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
837
838 /*
839 * It is recommended that the transmitter is the last enabled
840 * and the first disabled.
841 */
842 switch (cmd) {
843 case SNDRV_PCM_TRIGGER_START:
844 case SNDRV_PCM_TRIGGER_RESUME:
845 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
846 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
847 FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
848
849 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
850 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
851 /*
852 * Enable the opposite direction for synchronous mode
853 * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx
854 * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx
855 *
856 * RM recommends to enable RE after TE for case 1 and to enable
857 * TE after RE for case 2, but we here may not always guarantee
858 * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables
859 * TE after RE, which is against what RM recommends but should
860 * be safe to do, judging by years of testing results.
861 */
862 if (fsl_sai_dir_is_synced(sai, adir))
863 regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs),
864 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
865
866 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
867 FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
868 break;
869 case SNDRV_PCM_TRIGGER_STOP:
870 case SNDRV_PCM_TRIGGER_SUSPEND:
871 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
872 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
873 FSL_SAI_CSR_FRDE, 0);
874 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
875 FSL_SAI_CSR_xIE_MASK, 0);
876
877 /* Check if the opposite FRDE is also disabled */
878 regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr);
879
880 /*
881 * If opposite stream provides clocks for synchronous mode and
882 * it is inactive, disable it before disabling the current one
883 */
884 if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE))
885 fsl_sai_config_disable(sai, adir);
886
887 /*
888 * Disable current stream if either of:
889 * 1. current stream doesn't provide clocks for synchronous mode
890 * 2. current stream provides clocks for synchronous mode but no
891 * more stream is active.
892 */
893 if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE))
894 fsl_sai_config_disable(sai, dir);
895
896 break;
897 default:
898 return -EINVAL;
899 }
900
901 return 0;
902 }
903
904 static int fsl_sai_startup(struct snd_pcm_substream *substream,
905 struct snd_soc_dai *cpu_dai)
906 {
907 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
908 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
909 int ret;
910
911 /*
912 * EDMA controller needs period size to be a multiple of
913 * tx/rx maxburst
914 */
915 if (sai->soc_data->use_edma)
916 snd_pcm_hw_constraint_step(substream->runtime, 0,
917 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
918 tx ? sai->dma_params_tx.maxburst :
919 sai->dma_params_rx.maxburst);
920
921 if (sai->is_consumer_mode[tx])
922 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
923 SNDRV_PCM_HW_PARAM_RATE,
924 &fsl_sai_rate_constraints);
925 else
926 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
927 SNDRV_PCM_HW_PARAM_RATE,
928 &sai->constraint_rates);
929
930 return ret;
931 }
932
933 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
934 {
935 struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
936 unsigned int ofs = sai->soc_data->reg_offset;
937
938 /* Software Reset for both Tx and Rx */
939 regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR, FSL_SAI_CSR_SR);
940 regmap_update_bits(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR, FSL_SAI_CSR_SR);
941 /* Clear SR bit to finish the reset */
942 regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR, 0);
943 regmap_update_bits(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR, 0);
944
945 regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs),
946 FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
947 sai->soc_data->fifo_depth - sai->dma_params_tx.maxburst);
948 regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs),
949 FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
950 sai->dma_params_rx.maxburst - 1);
951
952 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
953 &sai->dma_params_rx);
954
955 return 0;
956 }
957
958 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
959 .probe = fsl_sai_dai_probe,
960 .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio,
961 .set_sysclk = fsl_sai_set_dai_sysclk,
962 .set_fmt = fsl_sai_set_dai_fmt,
963 .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
964 .hw_params = fsl_sai_hw_params,
965 .hw_free = fsl_sai_hw_free,
966 .trigger = fsl_sai_trigger,
967 .startup = fsl_sai_startup,
968 };
969
970 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_tx_ops = {
971 .probe = fsl_sai_dai_probe,
972 .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio,
973 .set_sysclk = fsl_sai_set_dai_sysclk,
974 .set_fmt = fsl_sai_set_dai_fmt_tx,
975 .set_tdm_slot = fsl_sai_set_dai_tdm_slot_tx,
976 .xlate_tdm_slot_mask = fsl_sai_xlate_tdm_slot_mask,
977 .hw_params = fsl_sai_hw_params,
978 .hw_free = fsl_sai_hw_free,
979 .trigger = fsl_sai_trigger,
980 .startup = fsl_sai_startup,
981 };
982
983 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_rx_ops = {
984 .probe = fsl_sai_dai_probe,
985 .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio,
986 .set_sysclk = fsl_sai_set_dai_sysclk,
987 .set_fmt = fsl_sai_set_dai_fmt_rx,
988 .set_tdm_slot = fsl_sai_set_dai_tdm_slot_rx,
989 .xlate_tdm_slot_mask = fsl_sai_xlate_tdm_slot_mask,
990 .hw_params = fsl_sai_hw_params,
991 .hw_free = fsl_sai_hw_free,
992 .trigger = fsl_sai_trigger,
993 .startup = fsl_sai_startup,
994 };
995
996 static int fsl_sai_dai_resume(struct snd_soc_component *component)
997 {
998 struct fsl_sai *sai = snd_soc_component_get_drvdata(component);
999 struct device *dev = &sai->pdev->dev;
1000 int ret;
1001
1002 if (!IS_ERR_OR_NULL(sai->pinctrl) && !IS_ERR_OR_NULL(sai->pins_state)) {
1003 ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
1004 if (ret) {
1005 dev_err(dev, "failed to set proper pins state: %d\n", ret);
1006 return ret;
1007 }
1008 }
1009
1010 return 0;
1011 }
1012
1013 static struct snd_soc_dai_driver fsl_sai_dai_template[] = {
1014 {
1015 .name = "sai-tx-rx",
1016 .playback = {
1017 .stream_name = "CPU-Playback",
1018 .channels_min = 1,
1019 .channels_max = 32,
1020 .rate_min = 8000,
1021 .rate_max = 2822400,
1022 .rates = SNDRV_PCM_RATE_KNOT,
1023 .formats = FSL_SAI_FORMATS,
1024 },
1025 .capture = {
1026 .stream_name = "CPU-Capture",
1027 .channels_min = 1,
1028 .channels_max = 32,
1029 .rate_min = 8000,
1030 .rate_max = 2822400,
1031 .rates = SNDRV_PCM_RATE_KNOT,
1032 .formats = FSL_SAI_FORMATS,
1033 },
1034 .ops = &fsl_sai_pcm_dai_ops,
1035 },
1036 {
1037 .name = "sai-tx",
1038 .playback = {
1039 .stream_name = "SAI-Playback",
1040 .channels_min = 1,
1041 .channels_max = 32,
1042 .rate_min = 8000,
1043 .rate_max = 2822400,
1044 .rates = SNDRV_PCM_RATE_KNOT,
1045 .formats = FSL_SAI_FORMATS,
1046 },
1047 .ops = &fsl_sai_pcm_dai_tx_ops,
1048 },
1049 {
1050 .name = "sai-rx",
1051 .capture = {
1052 .stream_name = "SAI-Capture",
1053 .channels_min = 1,
1054 .channels_max = 32,
1055 .rate_min = 8000,
1056 .rate_max = 2822400,
1057 .rates = SNDRV_PCM_RATE_KNOT,
1058 .formats = FSL_SAI_FORMATS,
1059 },
1060 .ops = &fsl_sai_pcm_dai_rx_ops,
1061 },
1062 };
1063
1064 static const struct snd_soc_component_driver fsl_component = {
1065 .name = "fsl-sai",
1066 .resume = fsl_sai_dai_resume,
1067 .legacy_dai_naming = 1,
1068 };
1069
1070 static const struct reg_default fsl_sai_reg_defaults_ofs0[] = {
1071 {FSL_SAI_TCR1(0), 0},
1072 {FSL_SAI_TCR2(0), 0},
1073 {FSL_SAI_TCR3(0), 0},
1074 {FSL_SAI_TCR4(0), 0},
1075 {FSL_SAI_TCR5(0), 0},
1076 {FSL_SAI_TDR0, 0},
1077 {FSL_SAI_TDR1, 0},
1078 {FSL_SAI_TDR2, 0},
1079 {FSL_SAI_TDR3, 0},
1080 {FSL_SAI_TDR4, 0},
1081 {FSL_SAI_TDR5, 0},
1082 {FSL_SAI_TDR6, 0},
1083 {FSL_SAI_TDR7, 0},
1084 {FSL_SAI_TMR, 0},
1085 {FSL_SAI_TTCTL, 0},
1086 {FSL_SAI_RCR1(0), 0},
1087 {FSL_SAI_RCR2(0), 0},
1088 {FSL_SAI_RCR3(0), 0},
1089 {FSL_SAI_RCR4(0), 0},
1090 {FSL_SAI_RCR5(0), 0},
1091 {FSL_SAI_RMR, 0},
1092 };
1093
1094 static const struct reg_default fsl_sai_reg_defaults_ofs8[] = {
1095 {FSL_SAI_TCR1(8), 0},
1096 {FSL_SAI_TCR2(8), 0},
1097 {FSL_SAI_TCR3(8), 0},
1098 {FSL_SAI_TCR4(8), 0},
1099 {FSL_SAI_TCR5(8), 0},
1100 {FSL_SAI_TDR0, 0},
1101 {FSL_SAI_TDR1, 0},
1102 {FSL_SAI_TDR2, 0},
1103 {FSL_SAI_TDR3, 0},
1104 {FSL_SAI_TDR4, 0},
1105 {FSL_SAI_TDR5, 0},
1106 {FSL_SAI_TDR6, 0},
1107 {FSL_SAI_TDR7, 0},
1108 {FSL_SAI_TMR, 0},
1109 {FSL_SAI_TTCTL, 0},
1110 {FSL_SAI_RCR1(8), 0},
1111 {FSL_SAI_RCR2(8), 0},
1112 {FSL_SAI_RCR3(8), 0},
1113 {FSL_SAI_RCR4(8), 0},
1114 {FSL_SAI_RCR5(8), 0},
1115 {FSL_SAI_RMR, 0},
1116 {FSL_SAI_RTCTL, 0},
1117 {FSL_SAI_MCTL, 0},
1118 {FSL_SAI_MDIV, 0},
1119 };
1120
1121 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
1122 {
1123 struct fsl_sai *sai = dev_get_drvdata(dev);
1124 unsigned int ofs = sai->soc_data->reg_offset;
1125
1126 if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
1127 return true;
1128
1129 if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
1130 return true;
1131
1132 switch (reg) {
1133 case FSL_SAI_TFR0:
1134 case FSL_SAI_TFR1:
1135 case FSL_SAI_TFR2:
1136 case FSL_SAI_TFR3:
1137 case FSL_SAI_TFR4:
1138 case FSL_SAI_TFR5:
1139 case FSL_SAI_TFR6:
1140 case FSL_SAI_TFR7:
1141 case FSL_SAI_TMR:
1142 case FSL_SAI_RDR0:
1143 case FSL_SAI_RDR1:
1144 case FSL_SAI_RDR2:
1145 case FSL_SAI_RDR3:
1146 case FSL_SAI_RDR4:
1147 case FSL_SAI_RDR5:
1148 case FSL_SAI_RDR6:
1149 case FSL_SAI_RDR7:
1150 case FSL_SAI_RFR0:
1151 case FSL_SAI_RFR1:
1152 case FSL_SAI_RFR2:
1153 case FSL_SAI_RFR3:
1154 case FSL_SAI_RFR4:
1155 case FSL_SAI_RFR5:
1156 case FSL_SAI_RFR6:
1157 case FSL_SAI_RFR7:
1158 case FSL_SAI_RMR:
1159 case FSL_SAI_MCTL:
1160 case FSL_SAI_MDIV:
1161 case FSL_SAI_VERID:
1162 case FSL_SAI_PARAM:
1163 case FSL_SAI_TTCTN:
1164 case FSL_SAI_RTCTN:
1165 case FSL_SAI_TTCTL:
1166 case FSL_SAI_TBCTN:
1167 case FSL_SAI_TTCAP:
1168 case FSL_SAI_RTCTL:
1169 case FSL_SAI_RBCTN:
1170 case FSL_SAI_RTCAP:
1171 return true;
1172 default:
1173 return false;
1174 }
1175 }
1176
1177 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
1178 {
1179 struct fsl_sai *sai = dev_get_drvdata(dev);
1180 unsigned int ofs = sai->soc_data->reg_offset;
1181
1182 if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs))
1183 return true;
1184
1185 /* Set VERID and PARAM be volatile for reading value in probe */
1186 if (ofs == 8 && (reg == FSL_SAI_VERID || reg == FSL_SAI_PARAM))
1187 return true;
1188
1189 switch (reg) {
1190 case FSL_SAI_TFR0:
1191 case FSL_SAI_TFR1:
1192 case FSL_SAI_TFR2:
1193 case FSL_SAI_TFR3:
1194 case FSL_SAI_TFR4:
1195 case FSL_SAI_TFR5:
1196 case FSL_SAI_TFR6:
1197 case FSL_SAI_TFR7:
1198 case FSL_SAI_RFR0:
1199 case FSL_SAI_RFR1:
1200 case FSL_SAI_RFR2:
1201 case FSL_SAI_RFR3:
1202 case FSL_SAI_RFR4:
1203 case FSL_SAI_RFR5:
1204 case FSL_SAI_RFR6:
1205 case FSL_SAI_RFR7:
1206 case FSL_SAI_RDR0:
1207 case FSL_SAI_RDR1:
1208 case FSL_SAI_RDR2:
1209 case FSL_SAI_RDR3:
1210 case FSL_SAI_RDR4:
1211 case FSL_SAI_RDR5:
1212 case FSL_SAI_RDR6:
1213 case FSL_SAI_RDR7:
1214 return true;
1215 default:
1216 return false;
1217 }
1218 }
1219
1220 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
1221 {
1222 struct fsl_sai *sai = dev_get_drvdata(dev);
1223 unsigned int ofs = sai->soc_data->reg_offset;
1224
1225 if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
1226 return true;
1227
1228 if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
1229 return true;
1230
1231 switch (reg) {
1232 case FSL_SAI_TDR0:
1233 case FSL_SAI_TDR1:
1234 case FSL_SAI_TDR2:
1235 case FSL_SAI_TDR3:
1236 case FSL_SAI_TDR4:
1237 case FSL_SAI_TDR5:
1238 case FSL_SAI_TDR6:
1239 case FSL_SAI_TDR7:
1240 case FSL_SAI_TMR:
1241 case FSL_SAI_RMR:
1242 case FSL_SAI_MCTL:
1243 case FSL_SAI_MDIV:
1244 case FSL_SAI_TTCTL:
1245 case FSL_SAI_RTCTL:
1246 return true;
1247 default:
1248 return false;
1249 }
1250 }
1251
1252 static struct regmap_config fsl_sai_regmap_config = {
1253 .reg_bits = 32,
1254 .reg_stride = 4,
1255 .val_bits = 32,
1256
1257 .max_register = FSL_SAI_RMR,
1258 .reg_defaults = fsl_sai_reg_defaults_ofs0,
1259 .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0),
1260 .readable_reg = fsl_sai_readable_reg,
1261 .volatile_reg = fsl_sai_volatile_reg,
1262 .writeable_reg = fsl_sai_writeable_reg,
1263 .cache_type = REGCACHE_FLAT,
1264 };
1265
1266 static int fsl_sai_check_version(struct device *dev)
1267 {
1268 struct fsl_sai *sai = dev_get_drvdata(dev);
1269 unsigned char ofs = sai->soc_data->reg_offset;
1270 unsigned int val;
1271 int ret;
1272
1273 if (FSL_SAI_TCSR(ofs) == FSL_SAI_VERID)
1274 return 0;
1275
1276 ret = regmap_read(sai->regmap, FSL_SAI_VERID, &val);
1277 if (ret < 0)
1278 return ret;
1279
1280 dev_dbg(dev, "VERID: 0x%016X\n", val);
1281
1282 sai->verid.version = val &
1283 (FSL_SAI_VERID_MAJOR_MASK | FSL_SAI_VERID_MINOR_MASK);
1284 sai->verid.version >>= FSL_SAI_VERID_MINOR_SHIFT;
1285 sai->verid.feature = val & FSL_SAI_VERID_FEATURE_MASK;
1286
1287 ret = regmap_read(sai->regmap, FSL_SAI_PARAM, &val);
1288 if (ret < 0)
1289 return ret;
1290
1291 dev_dbg(dev, "PARAM: 0x%016X\n", val);
1292
1293 /* Max slots per frame, power of 2 */
1294 sai->param.slot_num = 1 <<
1295 ((val & FSL_SAI_PARAM_SPF_MASK) >> FSL_SAI_PARAM_SPF_SHIFT);
1296
1297 /* Words per fifo, power of 2 */
1298 sai->param.fifo_depth = 1 <<
1299 ((val & FSL_SAI_PARAM_WPF_MASK) >> FSL_SAI_PARAM_WPF_SHIFT);
1300
1301 /* Number of datalines implemented */
1302 sai->param.dataline = val & FSL_SAI_PARAM_DLN_MASK;
1303
1304 return 0;
1305 }
1306
1307 /*
1308 * Calculate the offset between first two datalines, don't
1309 * different offset in one case.
1310 */
1311 static unsigned int fsl_sai_calc_dl_off(unsigned long dl_mask)
1312 {
1313 int fbidx, nbidx, offset;
1314
1315 fbidx = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1316 nbidx = find_next_bit(&dl_mask, FSL_SAI_DL_NUM, fbidx + 1);
1317 offset = nbidx - fbidx - 1;
1318
1319 return (offset < 0 || offset >= (FSL_SAI_DL_NUM - 1) ? 0 : offset);
1320 }
1321
1322 /*
1323 * read the fsl,dataline property from dts file.
1324 * It has 3 value for each configuration, first one means the type:
1325 * I2S(1) or PDM(2), second one is dataline mask for 'rx', third one is
1326 * dataline mask for 'tx'. for example
1327 *
1328 * fsl,dataline = <1 0xff 0xff 2 0xff 0x11>,
1329 *
1330 * It means I2S type rx mask is 0xff, tx mask is 0xff, PDM type
1331 * rx mask is 0xff, tx mask is 0x11 (dataline 1 and 4 enabled).
1332 *
1333 */
1334 static int fsl_sai_read_dlcfg(struct fsl_sai *sai)
1335 {
1336 struct platform_device *pdev = sai->pdev;
1337 struct device_node *np = pdev->dev.of_node;
1338 struct device *dev = &pdev->dev;
1339 int ret, elems, i, index, num_cfg;
1340 char *propname = "fsl,dataline";
1341 struct fsl_sai_dl_cfg *cfg;
1342 unsigned long dl_mask;
1343 unsigned int soc_dl;
1344 u32 rx, tx, type;
1345
1346 elems = of_property_count_u32_elems(np, propname);
1347
1348 if (elems <= 0) {
1349 elems = 0;
1350 } else if (elems % 3) {
1351 dev_err(dev, "Number of elements must be divisible to 3.\n");
1352 return -EINVAL;
1353 }
1354
1355 num_cfg = elems / 3;
1356 /* Add one more for default value */
1357 cfg = devm_kcalloc(&pdev->dev, num_cfg + 1, sizeof(*cfg), GFP_KERNEL);
1358 if (!cfg)
1359 return -ENOMEM;
1360
1361 /* Consider default value "0 0xFF 0xFF" if property is missing */
1362 soc_dl = BIT(sai->soc_data->pins) - 1;
1363 cfg[0].type = FSL_SAI_DL_DEFAULT;
1364 cfg[0].pins[0] = sai->soc_data->pins;
1365 cfg[0].mask[0] = soc_dl;
1366 cfg[0].start_off[0] = 0;
1367 cfg[0].next_off[0] = 0;
1368
1369 cfg[0].pins[1] = sai->soc_data->pins;
1370 cfg[0].mask[1] = soc_dl;
1371 cfg[0].start_off[1] = 0;
1372 cfg[0].next_off[1] = 0;
1373 for (i = 1, index = 0; i < num_cfg + 1; i++) {
1374 /*
1375 * type of dataline
1376 * 0 means default mode
1377 * 1 means I2S mode
1378 * 2 means PDM mode
1379 */
1380 ret = of_property_read_u32_index(np, propname, index++, &type);
1381 if (ret)
1382 return -EINVAL;
1383
1384 ret = of_property_read_u32_index(np, propname, index++, &rx);
1385 if (ret)
1386 return -EINVAL;
1387
1388 ret = of_property_read_u32_index(np, propname, index++, &tx);
1389 if (ret)
1390 return -EINVAL;
1391
1392 if ((rx & ~soc_dl) || (tx & ~soc_dl)) {
1393 dev_err(dev, "dataline cfg[%d] setting error, mask is 0x%x\n", i, soc_dl);
1394 return -EINVAL;
1395 }
1396
1397 rx = rx & soc_dl;
1398 tx = tx & soc_dl;
1399
1400 cfg[i].type = type;
1401 cfg[i].pins[0] = hweight8(rx);
1402 cfg[i].mask[0] = rx;
1403 dl_mask = rx;
1404 cfg[i].start_off[0] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1405 cfg[i].next_off[0] = fsl_sai_calc_dl_off(rx);
1406
1407 cfg[i].pins[1] = hweight8(tx);
1408 cfg[i].mask[1] = tx;
1409 dl_mask = tx;
1410 cfg[i].start_off[1] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1411 cfg[i].next_off[1] = fsl_sai_calc_dl_off(tx);
1412 }
1413
1414 sai->dl_cfg = cfg;
1415 sai->dl_cfg_cnt = num_cfg + 1;
1416 return 0;
1417 }
1418
1419 static int fsl_sai_runtime_suspend(struct device *dev);
1420 static int fsl_sai_runtime_resume(struct device *dev);
1421
1422 static int fsl_sai_probe(struct platform_device *pdev)
1423 {
1424 struct device_node *np = pdev->dev.of_node;
1425 struct device *dev = &pdev->dev;
1426 struct fsl_sai *sai;
1427 struct regmap *gpr;
1428 void __iomem *base;
1429 const char *str = NULL;
1430 char tmp[8];
1431 int irq, ret, i;
1432 int index;
1433 u32 dmas[4];
1434 u32 val;
1435
1436 sai = devm_kzalloc(dev, sizeof(*sai), GFP_KERNEL);
1437 if (!sai)
1438 return -ENOMEM;
1439
1440 sai->pdev = pdev;
1441 sai->soc_data = of_device_get_match_data(dev);
1442
1443 sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
1444
1445 base = devm_platform_get_and_ioremap_resource(pdev, 0, &sai->res);
1446 if (IS_ERR(base))
1447 return PTR_ERR(base);
1448
1449 if (sai->soc_data->reg_offset == 8) {
1450 fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8;
1451 fsl_sai_regmap_config.max_register = FSL_SAI_MDIV;
1452 fsl_sai_regmap_config.num_reg_defaults =
1453 ARRAY_SIZE(fsl_sai_reg_defaults_ofs8);
1454 }
1455
1456 sai->regmap = devm_regmap_init_mmio(dev, base, &fsl_sai_regmap_config);
1457 if (IS_ERR(sai->regmap)) {
1458 dev_err(dev, "regmap init failed\n");
1459 return PTR_ERR(sai->regmap);
1460 }
1461
1462 sai->bus_clk = devm_clk_get(dev, "bus");
1463 /* Compatible with old DTB cases */
1464 if (IS_ERR(sai->bus_clk) && PTR_ERR(sai->bus_clk) != -EPROBE_DEFER)
1465 sai->bus_clk = devm_clk_get(dev, "sai");
1466 if (IS_ERR(sai->bus_clk)) {
1467 dev_err(dev, "failed to get bus clock: %ld\n",
1468 PTR_ERR(sai->bus_clk));
1469 /* -EPROBE_DEFER */
1470 return PTR_ERR(sai->bus_clk);
1471 }
1472
1473 for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
1474 sprintf(tmp, "mclk%d", i);
1475 sai->mclk_clk[i] = devm_clk_get(dev, tmp);
1476 if (IS_ERR(sai->mclk_clk[i])) {
1477 dev_err(dev, "failed to get mclk%d clock: %ld\n",
1478 i, PTR_ERR(sai->mclk_clk[i]));
1479 sai->mclk_clk[i] = NULL;
1480 }
1481 }
1482
1483 if (sai->soc_data->mclk0_is_mclk1)
1484 sai->mclk_clk[0] = sai->mclk_clk[1];
1485 else
1486 sai->mclk_clk[0] = sai->bus_clk;
1487
1488 fsl_asoc_get_pll_clocks(&pdev->dev, &sai->pll8k_clk,
1489 &sai->pll11k_clk);
1490
1491 fsl_asoc_constrain_rates(&sai->constraint_rates,
1492 &fsl_sai_rate_constraints,
1493 sai->pll8k_clk, sai->pll11k_clk, NULL,
1494 sai->constraint_rates_list);
1495
1496 /* Use Multi FIFO mode depending on the support from SDMA script */
1497 ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1498 if (!sai->soc_data->use_edma && !ret && dmas[2] == IMX_DMATYPE_MULTI_SAI)
1499 sai->is_multi_fifo_dma = true;
1500
1501 /* read dataline mask for rx and tx*/
1502 ret = fsl_sai_read_dlcfg(sai);
1503 if (ret < 0) {
1504 dev_err(dev, "failed to read dlcfg %d\n", ret);
1505 return ret;
1506 }
1507
1508 irq = platform_get_irq(pdev, 0);
1509 if (irq < 0)
1510 return irq;
1511
1512 ret = devm_request_irq(dev, irq, fsl_sai_isr, IRQF_SHARED,
1513 np->name, sai);
1514 if (ret) {
1515 dev_err(dev, "failed to claim irq %u\n", irq);
1516 return ret;
1517 }
1518
1519 memcpy(&sai->cpu_dai_drv, fsl_sai_dai_template,
1520 sizeof(*fsl_sai_dai_template) * ARRAY_SIZE(fsl_sai_dai_template));
1521
1522 /* Sync Tx with Rx as default by following old DT binding */
1523 sai->synchronous[RX] = true;
1524 sai->synchronous[TX] = false;
1525 sai->cpu_dai_drv[0].symmetric_rate = 1;
1526 sai->cpu_dai_drv[0].symmetric_channels = 1;
1527 sai->cpu_dai_drv[0].symmetric_sample_bits = 1;
1528
1529 if (of_property_read_bool(np, "fsl,sai-synchronous-rx") &&
1530 of_property_read_bool(np, "fsl,sai-asynchronous")) {
1531 /* error out if both synchronous and asynchronous are present */
1532 dev_err(dev, "invalid binding for synchronous mode\n");
1533 return -EINVAL;
1534 }
1535
1536 if (of_property_read_bool(np, "fsl,sai-synchronous-rx")) {
1537 /* Sync Rx with Tx */
1538 sai->synchronous[RX] = false;
1539 sai->synchronous[TX] = true;
1540 } else if (of_property_read_bool(np, "fsl,sai-asynchronous")) {
1541 /* Discard all settings for asynchronous mode */
1542 sai->synchronous[RX] = false;
1543 sai->synchronous[TX] = false;
1544 sai->cpu_dai_drv[0].symmetric_rate = 0;
1545 sai->cpu_dai_drv[0].symmetric_channels = 0;
1546 sai->cpu_dai_drv[0].symmetric_sample_bits = 0;
1547 }
1548
1549 sai->mclk_direction_output = of_property_read_bool(np, "fsl,sai-mclk-direction-output");
1550
1551 if (sai->mclk_direction_output &&
1552 of_device_is_compatible(np, "fsl,imx6ul-sai")) {
1553 gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
1554 if (IS_ERR(gpr)) {
1555 dev_err(dev, "cannot find iomuxc registers\n");
1556 return PTR_ERR(gpr);
1557 }
1558
1559 index = of_alias_get_id(np, "sai");
1560 if (index < 0)
1561 return index;
1562
1563 regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
1564 MCLK_DIR(index));
1565 }
1566
1567 sai->dma_params_rx.addr = sai->res->start + FSL_SAI_RDR0;
1568 sai->dma_params_tx.addr = sai->res->start + FSL_SAI_TDR0;
1569 sai->dma_params_rx.maxburst =
1570 sai->soc_data->max_burst[RX] ? sai->soc_data->max_burst[RX] : FSL_SAI_MAXBURST_RX;
1571 sai->dma_params_tx.maxburst =
1572 sai->soc_data->max_burst[TX] ? sai->soc_data->max_burst[TX] : FSL_SAI_MAXBURST_TX;
1573
1574 sai->pinctrl = devm_pinctrl_get(&pdev->dev);
1575
1576 platform_set_drvdata(pdev, sai);
1577 pm_runtime_enable(dev);
1578 if (!pm_runtime_enabled(dev)) {
1579 ret = fsl_sai_runtime_resume(dev);
1580 if (ret)
1581 goto err_pm_disable;
1582 }
1583
1584 ret = pm_runtime_resume_and_get(dev);
1585 if (ret < 0)
1586 goto err_pm_get_sync;
1587
1588 /* Get sai version */
1589 ret = fsl_sai_check_version(dev);
1590 if (ret < 0)
1591 dev_warn(dev, "Error reading SAI version: %d\n", ret);
1592
1593 /* Select MCLK direction */
1594 if (sai->mclk_direction_output &&
1595 sai->soc_data->max_register >= FSL_SAI_MCTL) {
1596 regmap_update_bits(sai->regmap, FSL_SAI_MCTL,
1597 FSL_SAI_MCTL_MCLK_EN, FSL_SAI_MCTL_MCLK_EN);
1598 }
1599
1600 ret = pm_runtime_put_sync(dev);
1601 if (ret < 0 && ret != -ENOSYS)
1602 goto err_pm_get_sync;
1603
1604 if (of_device_is_compatible(np, "fsl,imx952-sai") &&
1605 !of_property_read_string(np, "fsl,sai-amix-mode", &str)) {
1606 if (!strcmp(str, "bypass"))
1607 val = FSL_SAI_AMIX_BYPASS;
1608 else if (!strcmp(str, "audmix"))
1609 val = FSL_SAI_AMIX_AUDMIX;
1610 else
1611 val = FSL_SAI_AMIX_NONE;
1612
1613 if (val < FSL_SAI_AMIX_NONE) {
1614 ret = scmi_imx_misc_ctrl_set(SCMI_IMX952_CTRL_BYPASS_AUDMIX, val);
1615 if (ret) {
1616 dev_err_probe(dev, ret, "Error setting audmix mode\n");
1617 goto err_pm_get_sync;
1618 }
1619 }
1620 }
1621
1622 /*
1623 * Register platform component before registering cpu dai for there
1624 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
1625 */
1626 if (sai->soc_data->use_imx_pcm) {
1627 ret = imx_pcm_dma_init(pdev);
1628 if (ret) {
1629 dev_err_probe(dev, ret, "PCM DMA init failed\n");
1630 if (!IS_ENABLED(CONFIG_SND_SOC_IMX_PCM_DMA))
1631 dev_err(dev, "Error: You must enable the imx-pcm-dma support!\n");
1632 goto err_pm_get_sync;
1633 }
1634 } else {
1635 ret = devm_snd_dmaengine_pcm_register(dev, NULL, 0);
1636 if (ret) {
1637 dev_err_probe(dev, ret, "Registering PCM dmaengine failed\n");
1638 goto err_pm_get_sync;
1639 }
1640 }
1641
1642 ret = devm_snd_soc_register_component(dev, &fsl_component,
1643 sai->cpu_dai_drv, ARRAY_SIZE(fsl_sai_dai_template));
1644 if (ret)
1645 goto err_pm_get_sync;
1646
1647 return ret;
1648
1649 err_pm_get_sync:
1650 if (!pm_runtime_status_suspended(dev))
1651 fsl_sai_runtime_suspend(dev);
1652 err_pm_disable:
1653 pm_runtime_disable(dev);
1654
1655 return ret;
1656 }
1657
1658 static void fsl_sai_remove(struct platform_device *pdev)
1659 {
1660 pm_runtime_disable(&pdev->dev);
1661 if (!pm_runtime_status_suspended(&pdev->dev))
1662 fsl_sai_runtime_suspend(&pdev->dev);
1663 }
1664
1665 static const struct fsl_sai_soc_data fsl_sai_vf610_data = {
1666 .use_imx_pcm = false,
1667 .use_edma = false,
1668 .fifo_depth = 32,
1669 .pins = 1,
1670 .reg_offset = 0,
1671 .mclk0_is_mclk1 = false,
1672 .flags = 0,
1673 .max_register = FSL_SAI_RMR,
1674 };
1675
1676 static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = {
1677 .use_imx_pcm = true,
1678 .use_edma = false,
1679 .fifo_depth = 32,
1680 .pins = 1,
1681 .reg_offset = 0,
1682 .mclk0_is_mclk1 = true,
1683 .flags = 0,
1684 .max_register = FSL_SAI_RMR,
1685 };
1686
1687 static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = {
1688 .use_imx_pcm = true,
1689 .use_edma = false,
1690 .fifo_depth = 16,
1691 .pins = 2,
1692 .reg_offset = 8,
1693 .mclk0_is_mclk1 = false,
1694 .flags = PMQOS_CPU_LATENCY,
1695 .max_register = FSL_SAI_RMR,
1696 };
1697
1698 static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = {
1699 .use_imx_pcm = true,
1700 .use_edma = false,
1701 .fifo_depth = 128,
1702 .pins = 8,
1703 .reg_offset = 8,
1704 .mclk0_is_mclk1 = false,
1705 .flags = 0,
1706 .max_register = FSL_SAI_RMR,
1707 };
1708
1709 static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = {
1710 .use_imx_pcm = true,
1711 .use_edma = true,
1712 .fifo_depth = 64,
1713 .pins = 4,
1714 .reg_offset = 0,
1715 .mclk0_is_mclk1 = false,
1716 .flags = 0,
1717 .max_register = FSL_SAI_RMR,
1718 };
1719
1720 static const struct fsl_sai_soc_data fsl_sai_imx8mm_data = {
1721 .use_imx_pcm = true,
1722 .use_edma = false,
1723 .fifo_depth = 128,
1724 .reg_offset = 8,
1725 .mclk0_is_mclk1 = false,
1726 .pins = 8,
1727 .flags = 0,
1728 .max_register = FSL_SAI_MCTL,
1729 };
1730
1731 static const struct fsl_sai_soc_data fsl_sai_imx8mn_data = {
1732 .use_imx_pcm = true,
1733 .use_edma = false,
1734 .fifo_depth = 128,
1735 .reg_offset = 8,
1736 .mclk0_is_mclk1 = false,
1737 .pins = 8,
1738 .flags = 0,
1739 .max_register = FSL_SAI_MDIV,
1740 };
1741
1742 static const struct fsl_sai_soc_data fsl_sai_imx8mp_data = {
1743 .use_imx_pcm = true,
1744 .use_edma = false,
1745 .fifo_depth = 128,
1746 .reg_offset = 8,
1747 .mclk0_is_mclk1 = false,
1748 .pins = 8,
1749 .flags = 0,
1750 .max_register = FSL_SAI_MDIV,
1751 .mclk_with_tere = true,
1752 };
1753
1754 static const struct fsl_sai_soc_data fsl_sai_imx8ulp_data = {
1755 .use_imx_pcm = true,
1756 .use_edma = true,
1757 .fifo_depth = 16,
1758 .reg_offset = 8,
1759 .mclk0_is_mclk1 = false,
1760 .pins = 4,
1761 .flags = PMQOS_CPU_LATENCY,
1762 .max_register = FSL_SAI_RTCAP,
1763 };
1764
1765 static const struct fsl_sai_soc_data fsl_sai_imx93_data = {
1766 .use_imx_pcm = true,
1767 .use_edma = true,
1768 .fifo_depth = 128,
1769 .reg_offset = 8,
1770 .mclk0_is_mclk1 = false,
1771 .pins = 4,
1772 .flags = 0,
1773 .max_register = FSL_SAI_MCTL,
1774 .max_burst = {8, 8},
1775 };
1776
1777 static const struct fsl_sai_soc_data fsl_sai_imx95_data = {
1778 .use_imx_pcm = true,
1779 .use_edma = true,
1780 .fifo_depth = 128,
1781 .reg_offset = 8,
1782 .mclk0_is_mclk1 = false,
1783 .pins = 8,
1784 .flags = 0,
1785 .max_register = FSL_SAI_MCTL,
1786 .max_burst = {8, 8},
1787 };
1788
1789 static const struct of_device_id fsl_sai_ids[] = {
1790 { .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data },
1791 { .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data },
1792 { .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data },
1793 { .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data },
1794 { .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data },
1795 { .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data },
1796 { .compatible = "fsl,imx8mm-sai", .data = &fsl_sai_imx8mm_data },
1797 { .compatible = "fsl,imx8mp-sai", .data = &fsl_sai_imx8mp_data },
1798 { .compatible = "fsl,imx8ulp-sai", .data = &fsl_sai_imx8ulp_data },
1799 { .compatible = "fsl,imx8mn-sai", .data = &fsl_sai_imx8mn_data },
1800 { .compatible = "fsl,imx93-sai", .data = &fsl_sai_imx93_data },
1801 { .compatible = "fsl,imx95-sai", .data = &fsl_sai_imx95_data },
1802 { /* sentinel */ }
1803 };
1804 MODULE_DEVICE_TABLE(of, fsl_sai_ids);
1805
1806 static int fsl_sai_runtime_suspend(struct device *dev)
1807 {
1808 struct fsl_sai *sai = dev_get_drvdata(dev);
1809
1810 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1811 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1812
1813 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1814 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1815
1816 clk_disable_unprepare(sai->bus_clk);
1817
1818 if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
1819 cpu_latency_qos_remove_request(&sai->pm_qos_req);
1820
1821 regcache_cache_only(sai->regmap, true);
1822
1823 return 0;
1824 }
1825
1826 static int fsl_sai_runtime_resume(struct device *dev)
1827 {
1828 struct fsl_sai *sai = dev_get_drvdata(dev);
1829 unsigned int ofs = sai->soc_data->reg_offset;
1830 int ret;
1831
1832 ret = clk_prepare_enable(sai->bus_clk);
1833 if (ret) {
1834 dev_err(dev, "failed to enable bus clock: %d\n", ret);
1835 return ret;
1836 }
1837
1838 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) {
1839 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]);
1840 if (ret)
1841 goto disable_bus_clk;
1842 }
1843
1844 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) {
1845 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]);
1846 if (ret)
1847 goto disable_tx_clk;
1848 }
1849
1850 if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
1851 cpu_latency_qos_add_request(&sai->pm_qos_req, 0);
1852
1853 regcache_cache_only(sai->regmap, false);
1854 regcache_mark_dirty(sai->regmap);
1855 regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR, FSL_SAI_CSR_SR);
1856 regmap_update_bits(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR, FSL_SAI_CSR_SR);
1857 usleep_range(1000, 2000);
1858 regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR, 0);
1859 regmap_update_bits(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR, 0);
1860
1861 ret = regcache_sync(sai->regmap);
1862 if (ret)
1863 goto disable_rx_clk;
1864
1865 if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
1866 regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
1867 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
1868
1869 return 0;
1870
1871 disable_rx_clk:
1872 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1873 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1874 disable_tx_clk:
1875 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1876 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1877 disable_bus_clk:
1878 clk_disable_unprepare(sai->bus_clk);
1879
1880 return ret;
1881 }
1882
1883 static const struct dev_pm_ops fsl_sai_pm_ops = {
1884 RUNTIME_PM_OPS(fsl_sai_runtime_suspend, fsl_sai_runtime_resume, NULL)
1885 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1886 };
1887
1888 static struct platform_driver fsl_sai_driver = {
1889 .probe = fsl_sai_probe,
1890 .remove = fsl_sai_remove,
1891 .driver = {
1892 .name = "fsl-sai",
1893 .pm = pm_ptr(&fsl_sai_pm_ops),
1894 .of_match_table = fsl_sai_ids,
1895 },
1896 };
1897 module_platform_driver(fsl_sai_driver);
1898
1899 MODULE_DESCRIPTION("Freescale Soc SAI Interface");
1900 MODULE_AUTHOR("Xiubo Li, <[email protected]>");
1901 MODULE_ALIAS("platform:fsl-sai");
1902 MODULE_LICENSE("GPL");