1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
8 #include <linux/dmaengine.h>
9 #include <linux/module.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_platform.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/dmaengine_pcm.h>
14 #include <sound/pcm_params.h>
19 #define FSL_ESAI_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
20 SNDRV_PCM_FMTBIT_S16_LE | \
21 SNDRV_PCM_FMTBIT_S20_3LE | \
22 SNDRV_PCM_FMTBIT_S24_LE)
25 * fsl_esai: ESAI private data
27 * @dma_params_rx: DMA parameters for receive channel
28 * @dma_params_tx: DMA parameters for transmit channel
29 * @pdev: platform device pointer
30 * @regmap: regmap handler
31 * @coreclk: clock source to access register
32 * @extalclk: esai clock source to derive HCK, SCK and FS
33 * @fsysclk: system clock source to derive HCK, SCK and FS
34 * @spbaclk: SPBA clock (optional, depending on SoC design)
35 * @task: tasklet to handle the reset operation
36 * @lock: spin lock between hw_reset() and trigger()
37 * @fifo_depth: depth of tx/rx FIFO
38 * @slot_width: width of each DAI slot
39 * @slots: number of slots
40 * @channels: channel num for tx or rx
41 * @hck_rate: clock rate of desired HCKx clock
42 * @sck_rate: clock rate of desired SCKx clock
43 * @hck_dir: the direction of HCKx pads
44 * @sck_div: if using PSR/PM dividers for SCKx clock
45 * @slave_mode: if fully using DAI slave mode
46 * @synchronous: if using tx/rx synchronous mode
47 * @reset_at_xrun: flags for enable reset operaton
51 struct snd_dmaengine_dai_dma_data dma_params_rx
;
52 struct snd_dmaengine_dai_dma_data dma_params_tx
;
53 struct platform_device
*pdev
;
54 struct regmap
*regmap
;
59 struct tasklet_struct task
;
60 spinlock_t lock
; /* Protect hw_reset and trigger */
77 static irqreturn_t
esai_isr(int irq
, void *devid
)
79 struct fsl_esai
*esai_priv
= (struct fsl_esai
*)devid
;
80 struct platform_device
*pdev
= esai_priv
->pdev
;
84 regmap_read(esai_priv
->regmap
, REG_ESAI_ESR
, &esr
);
85 regmap_read(esai_priv
->regmap
, REG_ESAI_SAISR
, &saisr
);
87 if ((saisr
& (ESAI_SAISR_TUE
| ESAI_SAISR_ROE
)) &&
88 esai_priv
->reset_at_xrun
) {
89 dev_dbg(&pdev
->dev
, "reset module for xrun\n");
90 tasklet_schedule(&esai_priv
->task
);
93 if (esr
& ESAI_ESR_TINIT_MASK
)
94 dev_dbg(&pdev
->dev
, "isr: Transmission Initialized\n");
96 if (esr
& ESAI_ESR_RFF_MASK
)
97 dev_warn(&pdev
->dev
, "isr: Receiving overrun\n");
99 if (esr
& ESAI_ESR_TFE_MASK
)
100 dev_warn(&pdev
->dev
, "isr: Transmission underrun\n");
102 if (esr
& ESAI_ESR_TLS_MASK
)
103 dev_dbg(&pdev
->dev
, "isr: Just transmitted the last slot\n");
105 if (esr
& ESAI_ESR_TDE_MASK
)
106 dev_dbg(&pdev
->dev
, "isr: Transmission data exception\n");
108 if (esr
& ESAI_ESR_TED_MASK
)
109 dev_dbg(&pdev
->dev
, "isr: Transmitting even slots\n");
111 if (esr
& ESAI_ESR_TD_MASK
)
112 dev_dbg(&pdev
->dev
, "isr: Transmitting data\n");
114 if (esr
& ESAI_ESR_RLS_MASK
)
115 dev_dbg(&pdev
->dev
, "isr: Just received the last slot\n");
117 if (esr
& ESAI_ESR_RDE_MASK
)
118 dev_dbg(&pdev
->dev
, "isr: Receiving data exception\n");
120 if (esr
& ESAI_ESR_RED_MASK
)
121 dev_dbg(&pdev
->dev
, "isr: Receiving even slots\n");
123 if (esr
& ESAI_ESR_RD_MASK
)
124 dev_dbg(&pdev
->dev
, "isr: Receiving data\n");
130 * This function is used to calculate the divisors of psr, pm, fp and it is
131 * supposed to be called in set_dai_sysclk() and set_bclk().
133 * @ratio: desired overall ratio for the paticipating dividers
134 * @usefp: for HCK setting, there is no need to set fp divider
135 * @fp: bypass other dividers by setting fp directly if fp != 0
136 * @tx: current setting is for playback or capture
138 static int fsl_esai_divisor_cal(struct snd_soc_dai
*dai
, bool tx
, u32 ratio
,
141 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
142 u32 psr
, pm
= 999, maxfp
, prod
, sub
, savesub
, i
, j
;
144 maxfp
= usefp
? 16 : 1;
149 if (ratio
> 2 * 8 * 256 * maxfp
|| ratio
< 2) {
150 dev_err(dai
->dev
, "the ratio is out of range (2 ~ %d)\n",
151 2 * 8 * 256 * maxfp
);
153 } else if (ratio
% 2) {
154 dev_err(dai
->dev
, "the raio must be even if using upper divider\n");
160 psr
= ratio
<= 256 * maxfp
? ESAI_xCCR_xPSR_BYPASS
: ESAI_xCCR_xPSR_DIV8
;
162 /* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */
169 /* Set the max fluctuation -- 0.1% of the max devisor */
170 savesub
= (psr
? 1 : 8) * 256 * maxfp
/ 1000;
172 /* Find the best value for PM */
173 for (i
= 1; i
<= 256; i
++) {
174 for (j
= 1; j
<= maxfp
; j
++) {
175 /* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */
176 prod
= (psr
? 1 : 8) * i
* j
;
180 else if (prod
/ ratio
== 1)
182 else if (ratio
/ prod
== 1)
187 /* Calculate the fraction */
188 sub
= sub
* 1000 / ratio
;
202 dev_err(dai
->dev
, "failed to calculate proper divisors\n");
207 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCCR(tx
),
208 ESAI_xCCR_xPSR_MASK
| ESAI_xCCR_xPM_MASK
,
209 psr
| ESAI_xCCR_xPM(pm
));
212 /* Bypass fp if not being required */
216 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCCR(tx
),
217 ESAI_xCCR_xFP_MASK
, ESAI_xCCR_xFP(fp
));
223 * This function mainly configures the clock frequency of MCLK (HCKT/HCKR)
226 * clk_id: The clock source of HCKT/HCKR
227 * (Input from outside; output from inside, FSYS or EXTAL)
228 * freq: The required clock rate of HCKT/HCKR
229 * dir: The clock direction of HCKT/HCKR
231 * Note: If the direction is input, we do not care about clk_id.
233 static int fsl_esai_set_dai_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
234 unsigned int freq
, int dir
)
236 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
237 struct clk
*clksrc
= esai_priv
->extalclk
;
238 bool tx
= (clk_id
<= ESAI_HCKT_EXTAL
|| esai_priv
->synchronous
);
239 bool in
= dir
== SND_SOC_CLOCK_IN
;
241 unsigned long clk_rate
;
245 dev_err(dai
->dev
, "%sput freq of HCK%c should not be 0Hz\n",
246 in
? "in" : "out", tx
? 'T' : 'R');
250 /* Bypass divider settings if the requirement doesn't change */
251 if (freq
== esai_priv
->hck_rate
[tx
] && dir
== esai_priv
->hck_dir
[tx
])
254 /* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */
255 esai_priv
->sck_div
[tx
] = true;
257 /* Set the direction of HCKT/HCKR pins */
258 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCCR(tx
),
259 ESAI_xCCR_xHCKD
, in
? 0 : ESAI_xCCR_xHCKD
);
267 clksrc
= esai_priv
->fsysclk
;
269 case ESAI_HCKT_EXTAL
:
272 case ESAI_HCKR_EXTAL
:
273 ecr
|= esai_priv
->synchronous
? ESAI_ECR_ETI
: ESAI_ECR_ERI
;
279 if (IS_ERR(clksrc
)) {
280 dev_err(dai
->dev
, "no assigned %s clock\n",
281 clk_id
% 2 ? "extal" : "fsys");
282 return PTR_ERR(clksrc
);
284 clk_rate
= clk_get_rate(clksrc
);
286 ratio
= clk_rate
/ freq
;
287 if (ratio
* freq
> clk_rate
)
288 ret
= ratio
* freq
- clk_rate
;
289 else if (ratio
* freq
< clk_rate
)
290 ret
= clk_rate
- ratio
* freq
;
294 /* Block if clock source can not be divided into the required rate */
295 if (ret
!= 0 && clk_rate
/ ret
< 1000) {
296 dev_err(dai
->dev
, "failed to derive required HCK%c rate\n",
301 /* Only EXTAL source can be output directly without using PSR and PM */
302 if (ratio
== 1 && clksrc
== esai_priv
->extalclk
) {
303 /* Bypass all the dividers if not being needed */
304 ecr
|= tx
? ESAI_ECR_ETO
: ESAI_ECR_ERO
;
306 } else if (ratio
< 2) {
307 /* The ratio should be no less than 2 if using other sources */
308 dev_err(dai
->dev
, "failed to derive required HCK%c rate\n",
313 ret
= fsl_esai_divisor_cal(dai
, tx
, ratio
, false, 0);
317 esai_priv
->sck_div
[tx
] = false;
320 esai_priv
->hck_dir
[tx
] = dir
;
321 esai_priv
->hck_rate
[tx
] = freq
;
323 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_ECR
,
324 tx
? ESAI_ECR_ETI
| ESAI_ECR_ETO
:
325 ESAI_ECR_ERI
| ESAI_ECR_ERO
, ecr
);
331 * This function configures the related dividers according to the bclk rate
333 static int fsl_esai_set_bclk(struct snd_soc_dai
*dai
, bool tx
, u32 freq
)
335 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
336 u32 hck_rate
= esai_priv
->hck_rate
[tx
];
337 u32 sub
, ratio
= hck_rate
/ freq
;
340 /* Don't apply for fully slave mode or unchanged bclk */
341 if (esai_priv
->slave_mode
|| esai_priv
->sck_rate
[tx
] == freq
)
344 if (ratio
* freq
> hck_rate
)
345 sub
= ratio
* freq
- hck_rate
;
346 else if (ratio
* freq
< hck_rate
)
347 sub
= hck_rate
- ratio
* freq
;
351 /* Block if clock source can not be divided into the required rate */
352 if (sub
!= 0 && hck_rate
/ sub
< 1000) {
353 dev_err(dai
->dev
, "failed to derive required SCK%c rate\n",
358 /* The ratio should be contented by FP alone if bypassing PM and PSR */
359 if (!esai_priv
->sck_div
[tx
] && (ratio
> 16 || ratio
== 0)) {
360 dev_err(dai
->dev
, "the ratio is out of range (1 ~ 16)\n");
364 ret
= fsl_esai_divisor_cal(dai
, tx
, ratio
, true,
365 esai_priv
->sck_div
[tx
] ? 0 : ratio
);
369 /* Save current bclk rate */
370 esai_priv
->sck_rate
[tx
] = freq
;
375 static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai
*dai
, u32 tx_mask
,
376 u32 rx_mask
, int slots
, int slot_width
)
378 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
380 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCCR
,
381 ESAI_xCCR_xDC_MASK
, ESAI_xCCR_xDC(slots
));
383 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCCR
,
384 ESAI_xCCR_xDC_MASK
, ESAI_xCCR_xDC(slots
));
386 esai_priv
->slot_width
= slot_width
;
387 esai_priv
->slots
= slots
;
388 esai_priv
->tx_mask
= tx_mask
;
389 esai_priv
->rx_mask
= rx_mask
;
394 static int fsl_esai_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
396 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
397 u32 xcr
= 0, xccr
= 0, mask
;
400 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
401 case SND_SOC_DAIFMT_I2S
:
402 /* Data on rising edge of bclk, frame low, 1clk before data */
403 xcr
|= ESAI_xCR_xFSR
;
404 xccr
|= ESAI_xCCR_xFSP
| ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
406 case SND_SOC_DAIFMT_LEFT_J
:
407 /* Data on rising edge of bclk, frame high */
408 xccr
|= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
410 case SND_SOC_DAIFMT_RIGHT_J
:
411 /* Data on rising edge of bclk, frame high, right aligned */
412 xccr
|= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
415 case SND_SOC_DAIFMT_DSP_A
:
416 /* Data on rising edge of bclk, frame high, 1clk before data */
417 xcr
|= ESAI_xCR_xFSL
| ESAI_xCR_xFSR
;
418 xccr
|= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
420 case SND_SOC_DAIFMT_DSP_B
:
421 /* Data on rising edge of bclk, frame high */
422 xcr
|= ESAI_xCR_xFSL
;
423 xccr
|= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
429 /* DAI clock inversion */
430 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
431 case SND_SOC_DAIFMT_NB_NF
:
432 /* Nothing to do for both normal cases */
434 case SND_SOC_DAIFMT_IB_NF
:
435 /* Invert bit clock */
436 xccr
^= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
;
438 case SND_SOC_DAIFMT_NB_IF
:
439 /* Invert frame clock */
440 xccr
^= ESAI_xCCR_xFSP
;
442 case SND_SOC_DAIFMT_IB_IF
:
443 /* Invert both clocks */
444 xccr
^= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
| ESAI_xCCR_xFSP
;
450 esai_priv
->slave_mode
= false;
452 /* DAI clock master masks */
453 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
454 case SND_SOC_DAIFMT_CBM_CFM
:
455 esai_priv
->slave_mode
= true;
457 case SND_SOC_DAIFMT_CBS_CFM
:
458 xccr
|= ESAI_xCCR_xCKD
;
460 case SND_SOC_DAIFMT_CBM_CFS
:
461 xccr
|= ESAI_xCCR_xFSD
;
463 case SND_SOC_DAIFMT_CBS_CFS
:
464 xccr
|= ESAI_xCCR_xFSD
| ESAI_xCCR_xCKD
;
470 mask
= ESAI_xCR_xFSL
| ESAI_xCR_xFSR
| ESAI_xCR_xWA
;
471 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCR
, mask
, xcr
);
472 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCR
, mask
, xcr
);
474 mask
= ESAI_xCCR_xCKP
| ESAI_xCCR_xHCKP
| ESAI_xCCR_xFSP
|
475 ESAI_xCCR_xFSD
| ESAI_xCCR_xCKD
;
476 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCCR
, mask
, xccr
);
477 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCCR
, mask
, xccr
);
482 static int fsl_esai_startup(struct snd_pcm_substream
*substream
,
483 struct snd_soc_dai
*dai
)
485 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
488 /* Set synchronous mode */
489 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_SAICR
,
490 ESAI_SAICR_SYNC
, esai_priv
->synchronous
?
491 ESAI_SAICR_SYNC
: 0);
493 /* Set a default slot number -- 2 */
494 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCCR
,
495 ESAI_xCCR_xDC_MASK
, ESAI_xCCR_xDC(2));
496 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCCR
,
497 ESAI_xCCR_xDC_MASK
, ESAI_xCCR_xDC(2));
504 static int fsl_esai_hw_params(struct snd_pcm_substream
*substream
,
505 struct snd_pcm_hw_params
*params
,
506 struct snd_soc_dai
*dai
)
508 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
509 bool tx
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
510 u32 width
= params_width(params
);
511 u32 channels
= params_channels(params
);
512 u32 pins
= DIV_ROUND_UP(channels
, esai_priv
->slots
);
513 u32 slot_width
= width
;
517 /* Override slot_width if being specifically set */
518 if (esai_priv
->slot_width
)
519 slot_width
= esai_priv
->slot_width
;
521 bclk
= params_rate(params
) * slot_width
* esai_priv
->slots
;
523 ret
= fsl_esai_set_bclk(dai
, esai_priv
->synchronous
|| tx
, bclk
);
527 mask
= ESAI_xCR_xSWS_MASK
;
528 val
= ESAI_xCR_xSWS(slot_width
, width
);
530 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
), mask
, val
);
531 /* Recording in synchronous mode needs to set TCR also */
532 if (!tx
&& esai_priv
->synchronous
)
533 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCR
, mask
, val
);
535 /* Use Normal mode to support monaural audio */
536 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
),
537 ESAI_xCR_xMOD_MASK
, params_channels(params
) > 1 ?
538 ESAI_xCR_xMOD_NETWORK
: 0);
540 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xFCR(tx
),
541 ESAI_xFCR_xFR_MASK
, ESAI_xFCR_xFR
);
543 mask
= ESAI_xFCR_xFR_MASK
| ESAI_xFCR_xWA_MASK
| ESAI_xFCR_xFWM_MASK
|
544 (tx
? ESAI_xFCR_TE_MASK
| ESAI_xFCR_TIEN
: ESAI_xFCR_RE_MASK
);
545 val
= ESAI_xFCR_xWA(width
) | ESAI_xFCR_xFWM(esai_priv
->fifo_depth
) |
546 (tx
? ESAI_xFCR_TE(pins
) | ESAI_xFCR_TIEN
: ESAI_xFCR_RE(pins
));
548 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xFCR(tx
), mask
, val
);
551 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCR
,
552 ESAI_xCR_PADC
, ESAI_xCR_PADC
);
554 /* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
555 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PRRC
,
556 ESAI_PRRC_PDC_MASK
, ESAI_PRRC_PDC(ESAI_GPIO
));
557 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PCRC
,
558 ESAI_PCRC_PC_MASK
, ESAI_PCRC_PC(ESAI_GPIO
));
562 static int fsl_esai_hw_init(struct fsl_esai
*esai_priv
)
564 struct platform_device
*pdev
= esai_priv
->pdev
;
567 /* Reset ESAI unit */
568 ret
= regmap_update_bits(esai_priv
->regmap
, REG_ESAI_ECR
,
569 ESAI_ECR_ESAIEN_MASK
| ESAI_ECR_ERST_MASK
,
570 ESAI_ECR_ESAIEN
| ESAI_ECR_ERST
);
572 dev_err(&pdev
->dev
, "failed to reset ESAI: %d\n", ret
);
577 * We need to enable ESAI so as to access some of its registers.
578 * Otherwise, we would fail to dump regmap from user space.
580 ret
= regmap_update_bits(esai_priv
->regmap
, REG_ESAI_ECR
,
581 ESAI_ECR_ESAIEN_MASK
| ESAI_ECR_ERST_MASK
,
584 dev_err(&pdev
->dev
, "failed to enable ESAI: %d\n", ret
);
588 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PRRC
,
589 ESAI_PRRC_PDC_MASK
, 0);
590 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PCRC
,
591 ESAI_PCRC_PC_MASK
, 0);
596 static int fsl_esai_register_restore(struct fsl_esai
*esai_priv
)
600 /* FIFO reset for safety */
601 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TFCR
,
602 ESAI_xFCR_xFR
, ESAI_xFCR_xFR
);
603 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RFCR
,
604 ESAI_xFCR_xFR
, ESAI_xFCR_xFR
);
606 regcache_mark_dirty(esai_priv
->regmap
);
607 ret
= regcache_sync(esai_priv
->regmap
);
611 /* FIFO reset done */
612 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TFCR
, ESAI_xFCR_xFR
, 0);
613 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RFCR
, ESAI_xFCR_xFR
, 0);
618 static void fsl_esai_trigger_start(struct fsl_esai
*esai_priv
, bool tx
)
620 u8 i
, channels
= esai_priv
->channels
[tx
];
621 u32 pins
= DIV_ROUND_UP(channels
, esai_priv
->slots
);
624 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xFCR(tx
),
625 ESAI_xFCR_xFEN_MASK
, ESAI_xFCR_xFEN
);
627 /* Write initial words reqiured by ESAI as normal procedure */
628 for (i
= 0; tx
&& i
< channels
; i
++)
629 regmap_write(esai_priv
->regmap
, REG_ESAI_ETDR
, 0x0);
632 * When set the TE/RE in the end of enablement flow, there
633 * will be channel swap issue for multi data line case.
634 * In order to workaround this issue, we switch the bit
635 * enablement sequence to below sequence
636 * 1) clear the xSMB & xSMA: which is done in probe and
640 * 4) set xSMA: xSMA is the last one in this flow, which
641 * will trigger esai to start.
643 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
),
644 tx
? ESAI_xCR_TE_MASK
: ESAI_xCR_RE_MASK
,
645 tx
? ESAI_xCR_TE(pins
) : ESAI_xCR_RE(pins
));
646 mask
= tx
? esai_priv
->tx_mask
: esai_priv
->rx_mask
;
648 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xSMB(tx
),
649 ESAI_xSMB_xS_MASK
, ESAI_xSMB_xS(mask
));
650 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xSMA(tx
),
651 ESAI_xSMA_xS_MASK
, ESAI_xSMA_xS(mask
));
653 /* Enable Exception interrupt */
654 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
),
655 ESAI_xCR_xEIE_MASK
, ESAI_xCR_xEIE
);
658 static void fsl_esai_trigger_stop(struct fsl_esai
*esai_priv
, bool tx
)
660 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
),
661 ESAI_xCR_xEIE_MASK
, 0);
663 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xCR(tx
),
664 tx
? ESAI_xCR_TE_MASK
: ESAI_xCR_RE_MASK
, 0);
665 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xSMA(tx
),
666 ESAI_xSMA_xS_MASK
, 0);
667 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xSMB(tx
),
668 ESAI_xSMB_xS_MASK
, 0);
670 /* Disable and reset FIFO */
671 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xFCR(tx
),
672 ESAI_xFCR_xFR
| ESAI_xFCR_xFEN
, ESAI_xFCR_xFR
);
673 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_xFCR(tx
),
677 static void fsl_esai_hw_reset(unsigned long arg
)
679 struct fsl_esai
*esai_priv
= (struct fsl_esai
*)arg
;
680 bool tx
= true, rx
= false, enabled
[2];
681 unsigned long lock_flags
;
684 spin_lock_irqsave(&esai_priv
->lock
, lock_flags
);
685 /* Save the registers */
686 regmap_read(esai_priv
->regmap
, REG_ESAI_TFCR
, &tfcr
);
687 regmap_read(esai_priv
->regmap
, REG_ESAI_RFCR
, &rfcr
);
688 enabled
[tx
] = tfcr
& ESAI_xFCR_xFEN
;
689 enabled
[rx
] = rfcr
& ESAI_xFCR_xFEN
;
691 /* Stop the tx & rx */
692 fsl_esai_trigger_stop(esai_priv
, tx
);
693 fsl_esai_trigger_stop(esai_priv
, rx
);
695 /* Reset the esai, and ignore return value */
696 fsl_esai_hw_init(esai_priv
);
698 /* Enforce ESAI personal resets for both TX and RX */
699 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCR
,
700 ESAI_xCR_xPR_MASK
, ESAI_xCR_xPR
);
701 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCR
,
702 ESAI_xCR_xPR_MASK
, ESAI_xCR_xPR
);
704 /* Restore registers by regcache_sync, and ignore return value */
705 fsl_esai_register_restore(esai_priv
);
707 /* Remove ESAI personal resets by configuring PCRC and PRRC also */
708 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_TCR
,
709 ESAI_xCR_xPR_MASK
, 0);
710 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_RCR
,
711 ESAI_xCR_xPR_MASK
, 0);
712 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PRRC
,
713 ESAI_PRRC_PDC_MASK
, ESAI_PRRC_PDC(ESAI_GPIO
));
714 regmap_update_bits(esai_priv
->regmap
, REG_ESAI_PCRC
,
715 ESAI_PCRC_PC_MASK
, ESAI_PCRC_PC(ESAI_GPIO
));
717 /* Restart tx / rx, if they already enabled */
719 fsl_esai_trigger_start(esai_priv
, tx
);
721 fsl_esai_trigger_start(esai_priv
, rx
);
723 spin_unlock_irqrestore(&esai_priv
->lock
, lock_flags
);
726 static int fsl_esai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
727 struct snd_soc_dai
*dai
)
729 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
730 bool tx
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
731 unsigned long lock_flags
;
733 esai_priv
->channels
[tx
] = substream
->runtime
->channels
;
736 case SNDRV_PCM_TRIGGER_START
:
737 case SNDRV_PCM_TRIGGER_RESUME
:
738 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
739 spin_lock_irqsave(&esai_priv
->lock
, lock_flags
);
740 fsl_esai_trigger_start(esai_priv
, tx
);
741 spin_unlock_irqrestore(&esai_priv
->lock
, lock_flags
);
743 case SNDRV_PCM_TRIGGER_SUSPEND
:
744 case SNDRV_PCM_TRIGGER_STOP
:
745 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
746 spin_lock_irqsave(&esai_priv
->lock
, lock_flags
);
747 fsl_esai_trigger_stop(esai_priv
, tx
);
748 spin_unlock_irqrestore(&esai_priv
->lock
, lock_flags
);
757 static const struct snd_soc_dai_ops fsl_esai_dai_ops
= {
758 .startup
= fsl_esai_startup
,
759 .trigger
= fsl_esai_trigger
,
760 .hw_params
= fsl_esai_hw_params
,
761 .set_sysclk
= fsl_esai_set_dai_sysclk
,
762 .set_fmt
= fsl_esai_set_dai_fmt
,
763 .set_tdm_slot
= fsl_esai_set_dai_tdm_slot
,
766 static int fsl_esai_dai_probe(struct snd_soc_dai
*dai
)
768 struct fsl_esai
*esai_priv
= snd_soc_dai_get_drvdata(dai
);
770 snd_soc_dai_init_dma_data(dai
, &esai_priv
->dma_params_tx
,
771 &esai_priv
->dma_params_rx
);
776 static struct snd_soc_dai_driver fsl_esai_dai
= {
777 .probe
= fsl_esai_dai_probe
,
779 .stream_name
= "CPU-Playback",
782 .rates
= SNDRV_PCM_RATE_8000_192000
,
783 .formats
= FSL_ESAI_FORMATS
,
786 .stream_name
= "CPU-Capture",
789 .rates
= SNDRV_PCM_RATE_8000_192000
,
790 .formats
= FSL_ESAI_FORMATS
,
792 .ops
= &fsl_esai_dai_ops
,
795 static const struct snd_soc_component_driver fsl_esai_component
= {
799 static const struct reg_default fsl_esai_reg_defaults
[] = {
800 {REG_ESAI_ETDR
, 0x00000000},
801 {REG_ESAI_ECR
, 0x00000000},
802 {REG_ESAI_TFCR
, 0x00000000},
803 {REG_ESAI_RFCR
, 0x00000000},
804 {REG_ESAI_TX0
, 0x00000000},
805 {REG_ESAI_TX1
, 0x00000000},
806 {REG_ESAI_TX2
, 0x00000000},
807 {REG_ESAI_TX3
, 0x00000000},
808 {REG_ESAI_TX4
, 0x00000000},
809 {REG_ESAI_TX5
, 0x00000000},
810 {REG_ESAI_TSR
, 0x00000000},
811 {REG_ESAI_SAICR
, 0x00000000},
812 {REG_ESAI_TCR
, 0x00000000},
813 {REG_ESAI_TCCR
, 0x00000000},
814 {REG_ESAI_RCR
, 0x00000000},
815 {REG_ESAI_RCCR
, 0x00000000},
816 {REG_ESAI_TSMA
, 0x0000ffff},
817 {REG_ESAI_TSMB
, 0x0000ffff},
818 {REG_ESAI_RSMA
, 0x0000ffff},
819 {REG_ESAI_RSMB
, 0x0000ffff},
820 {REG_ESAI_PRRC
, 0x00000000},
821 {REG_ESAI_PCRC
, 0x00000000},
824 static bool fsl_esai_readable_reg(struct device
*dev
, unsigned int reg
)
856 static bool fsl_esai_volatile_reg(struct device
*dev
, unsigned int reg
)
874 static bool fsl_esai_writeable_reg(struct device
*dev
, unsigned int reg
)
905 static const struct regmap_config fsl_esai_regmap_config
= {
910 .max_register
= REG_ESAI_PCRC
,
911 .reg_defaults
= fsl_esai_reg_defaults
,
912 .num_reg_defaults
= ARRAY_SIZE(fsl_esai_reg_defaults
),
913 .readable_reg
= fsl_esai_readable_reg
,
914 .volatile_reg
= fsl_esai_volatile_reg
,
915 .writeable_reg
= fsl_esai_writeable_reg
,
916 .cache_type
= REGCACHE_FLAT
,
919 static int fsl_esai_probe(struct platform_device
*pdev
)
921 struct device_node
*np
= pdev
->dev
.of_node
;
922 struct fsl_esai
*esai_priv
;
923 struct resource
*res
;
928 esai_priv
= devm_kzalloc(&pdev
->dev
, sizeof(*esai_priv
), GFP_KERNEL
);
932 esai_priv
->pdev
= pdev
;
933 snprintf(esai_priv
->name
, sizeof(esai_priv
->name
), "%pOFn", np
);
935 if (of_device_is_compatible(np
, "fsl,vf610-esai") ||
936 of_device_is_compatible(np
, "fsl,imx35-esai"))
937 esai_priv
->reset_at_xrun
= true;
939 /* Get the addresses and IRQ */
940 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
941 regs
= devm_ioremap_resource(&pdev
->dev
, res
);
943 return PTR_ERR(regs
);
945 esai_priv
->regmap
= devm_regmap_init_mmio_clk(&pdev
->dev
,
946 "core", regs
, &fsl_esai_regmap_config
);
947 if (IS_ERR(esai_priv
->regmap
)) {
948 dev_err(&pdev
->dev
, "failed to init regmap: %ld\n",
949 PTR_ERR(esai_priv
->regmap
));
950 return PTR_ERR(esai_priv
->regmap
);
953 esai_priv
->coreclk
= devm_clk_get(&pdev
->dev
, "core");
954 if (IS_ERR(esai_priv
->coreclk
)) {
955 dev_err(&pdev
->dev
, "failed to get core clock: %ld\n",
956 PTR_ERR(esai_priv
->coreclk
));
957 return PTR_ERR(esai_priv
->coreclk
);
960 esai_priv
->extalclk
= devm_clk_get(&pdev
->dev
, "extal");
961 if (IS_ERR(esai_priv
->extalclk
))
962 dev_warn(&pdev
->dev
, "failed to get extal clock: %ld\n",
963 PTR_ERR(esai_priv
->extalclk
));
965 esai_priv
->fsysclk
= devm_clk_get(&pdev
->dev
, "fsys");
966 if (IS_ERR(esai_priv
->fsysclk
))
967 dev_warn(&pdev
->dev
, "failed to get fsys clock: %ld\n",
968 PTR_ERR(esai_priv
->fsysclk
));
970 esai_priv
->spbaclk
= devm_clk_get(&pdev
->dev
, "spba");
971 if (IS_ERR(esai_priv
->spbaclk
))
972 dev_warn(&pdev
->dev
, "failed to get spba clock: %ld\n",
973 PTR_ERR(esai_priv
->spbaclk
));
975 irq
= platform_get_irq(pdev
, 0);
979 ret
= devm_request_irq(&pdev
->dev
, irq
, esai_isr
, 0,
980 esai_priv
->name
, esai_priv
);
982 dev_err(&pdev
->dev
, "failed to claim irq %u\n", irq
);
986 /* Set a default slot number */
987 esai_priv
->slots
= 2;
989 /* Set a default master/slave state */
990 esai_priv
->slave_mode
= true;
992 /* Determine the FIFO depth */
993 iprop
= of_get_property(np
, "fsl,fifo-depth", NULL
);
995 esai_priv
->fifo_depth
= be32_to_cpup(iprop
);
997 esai_priv
->fifo_depth
= 64;
999 esai_priv
->dma_params_tx
.maxburst
= 16;
1000 esai_priv
->dma_params_rx
.maxburst
= 16;
1001 esai_priv
->dma_params_tx
.addr
= res
->start
+ REG_ESAI_ETDR
;
1002 esai_priv
->dma_params_rx
.addr
= res
->start
+ REG_ESAI_ERDR
;
1004 esai_priv
->synchronous
=
1005 of_property_read_bool(np
, "fsl,esai-synchronous");
1007 /* Implement full symmetry for synchronous mode */
1008 if (esai_priv
->synchronous
) {
1009 fsl_esai_dai
.symmetric_rates
= 1;
1010 fsl_esai_dai
.symmetric_channels
= 1;
1011 fsl_esai_dai
.symmetric_samplebits
= 1;
1014 dev_set_drvdata(&pdev
->dev
, esai_priv
);
1016 spin_lock_init(&esai_priv
->lock
);
1017 ret
= fsl_esai_hw_init(esai_priv
);
1021 esai_priv
->tx_mask
= 0xFFFFFFFF;
1022 esai_priv
->rx_mask
= 0xFFFFFFFF;
1024 /* Clear the TSMA, TSMB, RSMA, RSMB */
1025 regmap_write(esai_priv
->regmap
, REG_ESAI_TSMA
, 0);
1026 regmap_write(esai_priv
->regmap
, REG_ESAI_TSMB
, 0);
1027 regmap_write(esai_priv
->regmap
, REG_ESAI_RSMA
, 0);
1028 regmap_write(esai_priv
->regmap
, REG_ESAI_RSMB
, 0);
1030 ret
= devm_snd_soc_register_component(&pdev
->dev
, &fsl_esai_component
,
1033 dev_err(&pdev
->dev
, "failed to register DAI: %d\n", ret
);
1037 tasklet_init(&esai_priv
->task
, fsl_esai_hw_reset
,
1038 (unsigned long)esai_priv
);
1040 pm_runtime_enable(&pdev
->dev
);
1042 regcache_cache_only(esai_priv
->regmap
, true);
1044 ret
= imx_pcm_dma_init(pdev
, IMX_ESAI_DMABUF_SIZE
);
1046 dev_err(&pdev
->dev
, "failed to init imx pcm dma: %d\n", ret
);
1051 static int fsl_esai_remove(struct platform_device
*pdev
)
1053 struct fsl_esai
*esai_priv
= platform_get_drvdata(pdev
);
1055 pm_runtime_disable(&pdev
->dev
);
1056 tasklet_kill(&esai_priv
->task
);
1061 static const struct of_device_id fsl_esai_dt_ids
[] = {
1062 { .compatible
= "fsl,imx35-esai", },
1063 { .compatible
= "fsl,vf610-esai", },
1064 { .compatible
= "fsl,imx6ull-esai", },
1067 MODULE_DEVICE_TABLE(of
, fsl_esai_dt_ids
);
1070 static int fsl_esai_runtime_resume(struct device
*dev
)
1072 struct fsl_esai
*esai
= dev_get_drvdata(dev
);
1076 * Some platforms might use the same bit to gate all three or two of
1077 * clocks, so keep all clocks open/close at the same time for safety
1079 ret
= clk_prepare_enable(esai
->coreclk
);
1082 if (!IS_ERR(esai
->spbaclk
)) {
1083 ret
= clk_prepare_enable(esai
->spbaclk
);
1087 if (!IS_ERR(esai
->extalclk
)) {
1088 ret
= clk_prepare_enable(esai
->extalclk
);
1092 if (!IS_ERR(esai
->fsysclk
)) {
1093 ret
= clk_prepare_enable(esai
->fsysclk
);
1098 regcache_cache_only(esai
->regmap
, false);
1100 ret
= fsl_esai_register_restore(esai
);
1102 goto err_regcache_sync
;
1107 if (!IS_ERR(esai
->fsysclk
))
1108 clk_disable_unprepare(esai
->fsysclk
);
1110 if (!IS_ERR(esai
->extalclk
))
1111 clk_disable_unprepare(esai
->extalclk
);
1113 if (!IS_ERR(esai
->spbaclk
))
1114 clk_disable_unprepare(esai
->spbaclk
);
1116 clk_disable_unprepare(esai
->coreclk
);
1121 static int fsl_esai_runtime_suspend(struct device
*dev
)
1123 struct fsl_esai
*esai
= dev_get_drvdata(dev
);
1125 regcache_cache_only(esai
->regmap
, true);
1127 if (!IS_ERR(esai
->fsysclk
))
1128 clk_disable_unprepare(esai
->fsysclk
);
1129 if (!IS_ERR(esai
->extalclk
))
1130 clk_disable_unprepare(esai
->extalclk
);
1131 if (!IS_ERR(esai
->spbaclk
))
1132 clk_disable_unprepare(esai
->spbaclk
);
1133 clk_disable_unprepare(esai
->coreclk
);
1137 #endif /* CONFIG_PM */
1139 static const struct dev_pm_ops fsl_esai_pm_ops
= {
1140 SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend
,
1141 fsl_esai_runtime_resume
,
1143 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1144 pm_runtime_force_resume
)
1147 static struct platform_driver fsl_esai_driver
= {
1148 .probe
= fsl_esai_probe
,
1149 .remove
= fsl_esai_remove
,
1151 .name
= "fsl-esai-dai",
1152 .pm
= &fsl_esai_pm_ops
,
1153 .of_match_table
= fsl_esai_dt_ids
,
1157 module_platform_driver(fsl_esai_driver
);
1159 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1160 MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
1161 MODULE_LICENSE("GPL v2");
1162 MODULE_ALIAS("platform:fsl-esai-dai");