1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * atmel_ssc_dai.c -- ALSA SoC ATMEL SSC Audio Layer Platform driver
5 * Copyright (C) 2005 SAN People
6 * Copyright (C) 2008 Atmel
8 * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com>
11 * Based on at91-ssc.c by
12 * Frank Mandarino <fmandarino@endrelia.com>
13 * Based on pxa2xx Platform drivers by
14 * Liam Girdwood <lrg@slimlogic.co.uk>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/atmel_pdc.h>
25 #include <linux/atmel-ssc.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/initval.h>
30 #include <sound/soc.h>
32 #include "atmel-pcm.h"
33 #include "atmel_ssc_dai.h"
36 #define NUM_SSC_DEVICES 3
39 * SSC PDC registers required by the PCM DMA engine.
41 static struct atmel_pdc_regs pdc_tx_reg
= {
44 .xnpr
= ATMEL_PDC_TNPR
,
45 .xncr
= ATMEL_PDC_TNCR
,
48 static struct atmel_pdc_regs pdc_rx_reg
= {
51 .xnpr
= ATMEL_PDC_RNPR
,
52 .xncr
= ATMEL_PDC_RNCR
,
56 * SSC & PDC status bits for transmit and receive.
58 static struct atmel_ssc_mask ssc_tx_mask
= {
59 .ssc_enable
= SSC_BIT(CR_TXEN
),
60 .ssc_disable
= SSC_BIT(CR_TXDIS
),
61 .ssc_endx
= SSC_BIT(SR_ENDTX
),
62 .ssc_endbuf
= SSC_BIT(SR_TXBUFE
),
63 .ssc_error
= SSC_BIT(SR_OVRUN
),
64 .pdc_enable
= ATMEL_PDC_TXTEN
,
65 .pdc_disable
= ATMEL_PDC_TXTDIS
,
68 static struct atmel_ssc_mask ssc_rx_mask
= {
69 .ssc_enable
= SSC_BIT(CR_RXEN
),
70 .ssc_disable
= SSC_BIT(CR_RXDIS
),
71 .ssc_endx
= SSC_BIT(SR_ENDRX
),
72 .ssc_endbuf
= SSC_BIT(SR_RXBUFF
),
73 .ssc_error
= SSC_BIT(SR_OVRUN
),
74 .pdc_enable
= ATMEL_PDC_RXTEN
,
75 .pdc_disable
= ATMEL_PDC_RXTDIS
,
82 static struct atmel_pcm_dma_params ssc_dma_params
[NUM_SSC_DEVICES
][2] = {
84 .name
= "SSC0 PCM out",
89 .name
= "SSC0 PCM in",
94 .name
= "SSC1 PCM out",
99 .name
= "SSC1 PCM in",
101 .mask
= &ssc_rx_mask
,
104 .name
= "SSC2 PCM out",
106 .mask
= &ssc_tx_mask
,
109 .name
= "SSC2 PCM in",
111 .mask
= &ssc_rx_mask
,
116 static struct atmel_ssc_info ssc_info
[NUM_SSC_DEVICES
] = {
119 .dir_mask
= SSC_DIR_MASK_UNUSED
,
124 .dir_mask
= SSC_DIR_MASK_UNUSED
,
129 .dir_mask
= SSC_DIR_MASK_UNUSED
,
136 * SSC interrupt handler. Passes PDC interrupts to the DMA
137 * interrupt handler in the PCM driver.
139 static irqreturn_t
atmel_ssc_interrupt(int irq
, void *dev_id
)
141 struct atmel_ssc_info
*ssc_p
= dev_id
;
142 struct atmel_pcm_dma_params
*dma_params
;
144 u32 ssc_substream_mask
;
147 ssc_sr
= (unsigned long)ssc_readl(ssc_p
->ssc
->regs
, SR
)
148 & (unsigned long)ssc_readl(ssc_p
->ssc
->regs
, IMR
);
151 * Loop through the substreams attached to this SSC. If
152 * a DMA-related interrupt occurred on that substream, call
153 * the DMA interrupt handler function, if one has been
154 * registered in the dma_params structure by the PCM driver.
156 for (i
= 0; i
< ARRAY_SIZE(ssc_p
->dma_params
); i
++) {
157 dma_params
= ssc_p
->dma_params
[i
];
159 if ((dma_params
!= NULL
) &&
160 (dma_params
->dma_intr_handler
!= NULL
)) {
161 ssc_substream_mask
= (dma_params
->mask
->ssc_endx
|
162 dma_params
->mask
->ssc_endbuf
);
163 if (ssc_sr
& ssc_substream_mask
) {
164 dma_params
->dma_intr_handler(ssc_sr
,
175 * When the bit clock is input, limit the maximum rate according to the
176 * Serial Clock Ratio Considerations section from the SSC documentation:
178 * The Transmitter and the Receiver can be programmed to operate
179 * with the clock signals provided on either the TK or RK pins.
180 * This allows the SSC to support many slave-mode data transfers.
181 * In this case, the maximum clock speed allowed on the RK pin is:
182 * - Peripheral clock divided by 2 if Receiver Frame Synchro is input
183 * - Peripheral clock divided by 3 if Receiver Frame Synchro is output
184 * In addition, the maximum clock speed allowed on the TK pin is:
185 * - Peripheral clock divided by 6 if Transmit Frame Synchro is input
186 * - Peripheral clock divided by 2 if Transmit Frame Synchro is output
188 * When the bit clock is output, limit the rate according to the
189 * SSC divider restrictions.
191 static int atmel_ssc_hw_rule_rate(struct snd_pcm_hw_params
*params
,
192 struct snd_pcm_hw_rule
*rule
)
194 struct atmel_ssc_info
*ssc_p
= rule
->private;
195 struct ssc_device
*ssc
= ssc_p
->ssc
;
196 struct snd_interval
*i
= hw_param_interval(params
, rule
->var
);
197 struct snd_interval t
;
198 struct snd_ratnum r
= {
203 unsigned int num
= 0, den
= 0;
208 frame_size
= snd_soc_params_to_frame_size(params
);
212 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
213 case SND_SOC_DAIFMT_BC_FP
:
214 if ((ssc_p
->dir_mask
& SSC_DIR_MASK_CAPTURE
)
215 && ssc
->clk_from_rk_pin
)
216 /* Receiver Frame Synchro (i.e. capture)
217 * is output (format is _CFS) and the RK pin
218 * is used for input (format is _CBM_).
223 case SND_SOC_DAIFMT_BC_FC
:
224 if ((ssc_p
->dir_mask
& SSC_DIR_MASK_PLAYBACK
)
225 && !ssc
->clk_from_rk_pin
)
226 /* Transmit Frame Synchro (i.e. playback)
227 * is input (format is _CFM) and the TK pin
228 * is used for input (format _CBM_ but not
235 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
236 case SND_SOC_DAIFMT_BP_FP
:
237 r
.num
= ssc_p
->mck_rate
/ mck_div
/ frame_size
;
239 ret
= snd_interval_ratnum(i
, 1, &r
, &num
, &den
);
240 if (ret
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
241 params
->rate_num
= num
;
242 params
->rate_den
= den
;
246 case SND_SOC_DAIFMT_BC_FP
:
247 case SND_SOC_DAIFMT_BC_FC
:
249 t
.max
= ssc_p
->mck_rate
/ mck_div
/ frame_size
;
250 t
.openmin
= t
.openmax
= 0;
252 ret
= snd_interval_refine(i
, &t
);
263 /*-------------------------------------------------------------------------*\
265 \*-------------------------------------------------------------------------*/
267 * Startup. Only that one substream allowed in each direction.
269 static int atmel_ssc_startup(struct snd_pcm_substream
*substream
,
270 struct snd_soc_dai
*dai
)
272 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
273 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
274 struct atmel_pcm_dma_params
*dma_params
;
278 pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
279 ssc_readl(ssc_p
->ssc
->regs
, SR
));
281 /* Enable PMC peripheral clock for this SSC */
282 pr_debug("atmel_ssc_dai: Starting clock\n");
283 ret
= clk_enable(ssc_p
->ssc
->clk
);
287 ssc_p
->mck_rate
= clk_get_rate(ssc_p
->ssc
->clk
);
289 /* Reset the SSC unless initialized to keep it in a clean state */
290 if (!ssc_p
->initialized
)
291 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
293 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
295 dir_mask
= SSC_DIR_MASK_PLAYBACK
;
298 dir_mask
= SSC_DIR_MASK_CAPTURE
;
301 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
302 SNDRV_PCM_HW_PARAM_RATE
,
303 atmel_ssc_hw_rule_rate
,
305 SNDRV_PCM_HW_PARAM_FRAME_BITS
,
306 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
308 dev_err(dai
->dev
, "Failed to specify rate rule: %d\n", ret
);
312 dma_params
= &ssc_dma_params
[pdev
->id
][dir
];
313 dma_params
->ssc
= ssc_p
->ssc
;
314 dma_params
->substream
= substream
;
316 ssc_p
->dma_params
[dir
] = dma_params
;
318 snd_soc_dai_set_dma_data(dai
, substream
, dma_params
);
320 if (ssc_p
->dir_mask
& dir_mask
)
323 ssc_p
->dir_mask
|= dir_mask
;
329 * Shutdown. Clear DMA parameters and shutdown the SSC if there
330 * are no other substreams open.
332 static void atmel_ssc_shutdown(struct snd_pcm_substream
*substream
,
333 struct snd_soc_dai
*dai
)
335 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
336 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
337 struct atmel_pcm_dma_params
*dma_params
;
340 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
345 dma_params
= ssc_p
->dma_params
[dir
];
347 if (dma_params
!= NULL
) {
348 dma_params
->ssc
= NULL
;
349 dma_params
->substream
= NULL
;
350 ssc_p
->dma_params
[dir
] = NULL
;
355 ssc_p
->dir_mask
&= ~dir_mask
;
356 if (!ssc_p
->dir_mask
) {
357 if (ssc_p
->initialized
) {
358 free_irq(ssc_p
->ssc
->irq
, ssc_p
);
359 ssc_p
->initialized
= 0;
363 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
364 /* Clear the SSC dividers */
365 ssc_p
->cmr_div
= ssc_p
->tcmr_period
= ssc_p
->rcmr_period
= 0;
366 ssc_p
->forced_divider
= 0;
369 /* Shutdown the SSC clock. */
370 pr_debug("atmel_ssc_dai: Stopping clock\n");
371 clk_disable(ssc_p
->ssc
->clk
);
376 * Record the DAI format for use in hw_params().
378 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
381 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
382 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
389 * Record SSC clock dividers for use in hw_params().
391 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai
*cpu_dai
,
394 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
395 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
398 case ATMEL_SSC_CMR_DIV
:
400 * The same master clock divider is used for both
401 * transmit and receive, so if a value has already
402 * been set, it must match this value.
404 if (ssc_p
->dir_mask
!=
405 (SSC_DIR_MASK_PLAYBACK
| SSC_DIR_MASK_CAPTURE
))
406 ssc_p
->cmr_div
= div
;
407 else if (ssc_p
->cmr_div
== 0)
408 ssc_p
->cmr_div
= div
;
410 if (div
!= ssc_p
->cmr_div
)
412 ssc_p
->forced_divider
|= BIT(ATMEL_SSC_CMR_DIV
);
415 case ATMEL_SSC_TCMR_PERIOD
:
416 ssc_p
->tcmr_period
= div
;
417 ssc_p
->forced_divider
|= BIT(ATMEL_SSC_TCMR_PERIOD
);
420 case ATMEL_SSC_RCMR_PERIOD
:
421 ssc_p
->rcmr_period
= div
;
422 ssc_p
->forced_divider
|= BIT(ATMEL_SSC_RCMR_PERIOD
);
432 /* Is the cpu-dai master of the frame clock? */
433 static int atmel_ssc_cfs(struct atmel_ssc_info
*ssc_p
)
435 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
436 case SND_SOC_DAIFMT_BC_FP
:
437 case SND_SOC_DAIFMT_BP_FP
:
443 /* Is the cpu-dai master of the bit clock? */
444 static int atmel_ssc_cbs(struct atmel_ssc_info
*ssc_p
)
446 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
447 case SND_SOC_DAIFMT_BP_FC
:
448 case SND_SOC_DAIFMT_BP_FP
:
457 static int atmel_ssc_hw_params(struct snd_pcm_substream
*substream
,
458 struct snd_pcm_hw_params
*params
,
459 struct snd_soc_dai
*dai
)
461 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
463 struct atmel_ssc_info
*ssc_p
= &ssc_info
[id
];
464 struct ssc_device
*ssc
= ssc_p
->ssc
;
465 struct atmel_pcm_dma_params
*dma_params
;
466 int dir
, channels
, bits
;
467 u32 tfmr
, rfmr
, tcmr
, rcmr
;
469 int fslen
, fslen_ext
, fs_osync
, fs_edge
;
475 * Currently, there is only one set of dma params for
476 * each direction. If more are added, this code will
477 * have to be changed to select the proper set.
479 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
485 * If the cpu dai should provide BCLK, but noone has provided the
486 * divider needed for that to work, fall back to something sensible.
488 cmr_div
= ssc_p
->cmr_div
;
489 if (!(ssc_p
->forced_divider
& BIT(ATMEL_SSC_CMR_DIV
)) &&
490 atmel_ssc_cbs(ssc_p
)) {
491 int bclk_rate
= snd_soc_params_to_bclk(params
);
494 dev_err(dai
->dev
, "unable to calculate cmr_div: %d\n",
499 cmr_div
= DIV_ROUND_CLOSEST(ssc_p
->mck_rate
, 2 * bclk_rate
);
503 * If the cpu dai should provide LRCLK, but noone has provided the
504 * dividers needed for that to work, fall back to something sensible.
506 tcmr_period
= ssc_p
->tcmr_period
;
507 rcmr_period
= ssc_p
->rcmr_period
;
508 if (atmel_ssc_cfs(ssc_p
)) {
509 int frame_size
= snd_soc_params_to_frame_size(params
);
511 if (frame_size
< 0) {
513 "unable to calculate tx/rx cmr_period: %d\n",
518 if (!(ssc_p
->forced_divider
& BIT(ATMEL_SSC_TCMR_PERIOD
)))
519 tcmr_period
= frame_size
/ 2 - 1;
520 if (!(ssc_p
->forced_divider
& BIT(ATMEL_SSC_RCMR_PERIOD
)))
521 rcmr_period
= frame_size
/ 2 - 1;
524 dma_params
= ssc_p
->dma_params
[dir
];
526 channels
= params_channels(params
);
529 * Determine sample size in bits and the PDC increment.
531 switch (params_format(params
)) {
532 case SNDRV_PCM_FORMAT_S8
:
534 dma_params
->pdc_xfer_size
= 1;
536 case SNDRV_PCM_FORMAT_S16_LE
:
538 dma_params
->pdc_xfer_size
= 2;
540 case SNDRV_PCM_FORMAT_S24_LE
:
542 dma_params
->pdc_xfer_size
= 4;
544 case SNDRV_PCM_FORMAT_S32_LE
:
546 dma_params
->pdc_xfer_size
= 4;
549 printk(KERN_WARNING
"atmel_ssc_dai: unsupported PCM format");
554 * Compute SSC register settings.
557 fslen_ext
= (bits
- 1) / 16;
558 fslen
= (bits
- 1) % 16;
560 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
562 case SND_SOC_DAIFMT_LEFT_J
:
563 fs_osync
= SSC_FSOS_POSITIVE
;
564 fs_edge
= SSC_START_RISING_RF
;
566 rcmr
= SSC_BF(RCMR_STTDLY
, 0);
567 tcmr
= SSC_BF(TCMR_STTDLY
, 0);
571 case SND_SOC_DAIFMT_I2S
:
572 fs_osync
= SSC_FSOS_NEGATIVE
;
573 fs_edge
= SSC_START_FALLING_RF
;
575 rcmr
= SSC_BF(RCMR_STTDLY
, 1);
576 tcmr
= SSC_BF(TCMR_STTDLY
, 1);
580 case SND_SOC_DAIFMT_DSP_A
:
582 * DSP/PCM Mode A format
584 * Data is transferred on first BCLK after LRC pulse rising
585 * edge.If stereo, the right channel data is contiguous with
586 * the left channel data.
588 fs_osync
= SSC_FSOS_POSITIVE
;
589 fs_edge
= SSC_START_RISING_RF
;
590 fslen
= fslen_ext
= 0;
592 rcmr
= SSC_BF(RCMR_STTDLY
, 1);
593 tcmr
= SSC_BF(TCMR_STTDLY
, 1);
598 printk(KERN_WARNING
"atmel_ssc_dai: unsupported DAI format 0x%x\n",
603 if (!atmel_ssc_cfs(ssc_p
)) {
604 fslen
= fslen_ext
= 0;
605 rcmr_period
= tcmr_period
= 0;
606 fs_osync
= SSC_FSOS_NONE
;
609 rcmr
|= SSC_BF(RCMR_START
, fs_edge
);
610 tcmr
|= SSC_BF(TCMR_START
, fs_edge
);
612 if (atmel_ssc_cbs(ssc_p
)) {
616 * The SSC transmit and receive clocks are generated from the
617 * MCK divider, and the BCLK signal is output
618 * on the SSC TK line.
620 rcmr
|= SSC_BF(RCMR_CKS
, SSC_CKS_DIV
)
621 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
);
623 tcmr
|= SSC_BF(TCMR_CKS
, SSC_CKS_DIV
)
624 | SSC_BF(TCMR_CKO
, SSC_CKO_CONTINUOUS
);
626 rcmr
|= SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
627 SSC_CKS_PIN
: SSC_CKS_CLOCK
)
628 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
);
630 tcmr
|= SSC_BF(TCMR_CKS
, ssc
->clk_from_rk_pin
?
631 SSC_CKS_CLOCK
: SSC_CKS_PIN
)
632 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
);
635 rcmr
|= SSC_BF(RCMR_PERIOD
, rcmr_period
)
636 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
);
638 tcmr
|= SSC_BF(TCMR_PERIOD
, tcmr_period
)
639 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
);
641 rfmr
= SSC_BF(RFMR_FSLEN_EXT
, fslen_ext
)
642 | SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
643 | SSC_BF(RFMR_FSOS
, fs_osync
)
644 | SSC_BF(RFMR_FSLEN
, fslen
)
645 | SSC_BF(RFMR_DATNB
, (channels
- 1))
647 | SSC_BF(RFMR_LOOP
, 0)
648 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
650 tfmr
= SSC_BF(TFMR_FSLEN_EXT
, fslen_ext
)
651 | SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
652 | SSC_BF(TFMR_FSDEN
, 0)
653 | SSC_BF(TFMR_FSOS
, fs_osync
)
654 | SSC_BF(TFMR_FSLEN
, fslen
)
655 | SSC_BF(TFMR_DATNB
, (channels
- 1))
657 | SSC_BF(TFMR_DATDEF
, 0)
658 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
660 if (fslen_ext
&& !ssc
->pdata
->has_fslen_ext
) {
661 dev_err(dai
->dev
, "sample size %d is too large for SSC device\n",
666 pr_debug("atmel_ssc_hw_params: "
667 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
668 rcmr
, rfmr
, tcmr
, tfmr
);
670 if (!ssc_p
->initialized
) {
671 if (!ssc_p
->ssc
->pdata
->use_dma
) {
672 ssc_writel(ssc_p
->ssc
->regs
, PDC_RPR
, 0);
673 ssc_writel(ssc_p
->ssc
->regs
, PDC_RCR
, 0);
674 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNPR
, 0);
675 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNCR
, 0);
677 ssc_writel(ssc_p
->ssc
->regs
, PDC_TPR
, 0);
678 ssc_writel(ssc_p
->ssc
->regs
, PDC_TCR
, 0);
679 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNPR
, 0);
680 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNCR
, 0);
683 ret
= request_irq(ssc_p
->ssc
->irq
, atmel_ssc_interrupt
, 0,
687 "atmel_ssc_dai: request_irq failure\n");
688 pr_debug("Atmel_ssc_dai: Stopping clock\n");
689 clk_disable(ssc_p
->ssc
->clk
);
693 ssc_p
->initialized
= 1;
696 /* set SSC clock mode register */
697 ssc_writel(ssc_p
->ssc
->regs
, CMR
, cmr_div
);
699 /* set receive clock mode and format */
700 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, rcmr
);
701 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, rfmr
);
703 /* set transmit clock mode and format */
704 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, tcmr
);
705 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, tfmr
);
707 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
712 static int atmel_ssc_prepare(struct snd_pcm_substream
*substream
,
713 struct snd_soc_dai
*dai
)
715 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
716 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
717 struct atmel_pcm_dma_params
*dma_params
;
720 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
725 dma_params
= ssc_p
->dma_params
[dir
];
727 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
728 ssc_writel(ssc_p
->ssc
->regs
, IDR
, dma_params
->mask
->ssc_error
);
730 pr_debug("%s enabled SSC_SR=0x%08x\n",
731 dir
? "receive" : "transmit",
732 ssc_readl(ssc_p
->ssc
->regs
, SR
));
736 static int atmel_ssc_trigger(struct snd_pcm_substream
*substream
,
737 int cmd
, struct snd_soc_dai
*dai
)
739 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
740 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
741 struct atmel_pcm_dma_params
*dma_params
;
744 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
749 dma_params
= ssc_p
->dma_params
[dir
];
752 case SNDRV_PCM_TRIGGER_START
:
753 case SNDRV_PCM_TRIGGER_RESUME
:
754 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
755 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_enable
);
758 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
765 static int atmel_ssc_suspend(struct snd_soc_component
*component
)
767 struct atmel_ssc_info
*ssc_p
;
768 struct platform_device
*pdev
= to_platform_device(component
->dev
);
770 if (!snd_soc_component_active(component
))
773 ssc_p
= &ssc_info
[pdev
->id
];
775 /* Save the status register before disabling transmit and receive */
776 ssc_p
->ssc_state
.ssc_sr
= ssc_readl(ssc_p
->ssc
->regs
, SR
);
777 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
) | SSC_BIT(CR_RXDIS
));
779 /* Save the current interrupt mask, then disable unmasked interrupts */
780 ssc_p
->ssc_state
.ssc_imr
= ssc_readl(ssc_p
->ssc
->regs
, IMR
);
781 ssc_writel(ssc_p
->ssc
->regs
, IDR
, ssc_p
->ssc_state
.ssc_imr
);
783 ssc_p
->ssc_state
.ssc_cmr
= ssc_readl(ssc_p
->ssc
->regs
, CMR
);
784 ssc_p
->ssc_state
.ssc_rcmr
= ssc_readl(ssc_p
->ssc
->regs
, RCMR
);
785 ssc_p
->ssc_state
.ssc_rfmr
= ssc_readl(ssc_p
->ssc
->regs
, RFMR
);
786 ssc_p
->ssc_state
.ssc_tcmr
= ssc_readl(ssc_p
->ssc
->regs
, TCMR
);
787 ssc_p
->ssc_state
.ssc_tfmr
= ssc_readl(ssc_p
->ssc
->regs
, TFMR
);
792 static int atmel_ssc_resume(struct snd_soc_component
*component
)
794 struct atmel_ssc_info
*ssc_p
;
795 struct platform_device
*pdev
= to_platform_device(component
->dev
);
798 if (!snd_soc_component_active(component
))
801 ssc_p
= &ssc_info
[pdev
->id
];
803 /* restore SSC register settings */
804 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, ssc_p
->ssc_state
.ssc_tfmr
);
805 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, ssc_p
->ssc_state
.ssc_tcmr
);
806 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, ssc_p
->ssc_state
.ssc_rfmr
);
807 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, ssc_p
->ssc_state
.ssc_rcmr
);
808 ssc_writel(ssc_p
->ssc
->regs
, CMR
, ssc_p
->ssc_state
.ssc_cmr
);
810 /* re-enable interrupts */
811 ssc_writel(ssc_p
->ssc
->regs
, IER
, ssc_p
->ssc_state
.ssc_imr
);
813 /* Re-enable receive and transmit as appropriate */
816 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_RXEN
)) ? SSC_BIT(CR_RXEN
) : 0;
818 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_TXEN
)) ? SSC_BIT(CR_TXEN
) : 0;
819 ssc_writel(ssc_p
->ssc
->regs
, CR
, cr
);
824 /* S24_LE is not supported if more than 2 channels (of TDM slots) are used. */
825 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
826 SNDRV_PCM_FMTBIT_S32_LE)
828 static const struct snd_soc_dai_ops atmel_ssc_dai_ops
= {
829 .startup
= atmel_ssc_startup
,
830 .shutdown
= atmel_ssc_shutdown
,
831 .prepare
= atmel_ssc_prepare
,
832 .trigger
= atmel_ssc_trigger
,
833 .hw_params
= atmel_ssc_hw_params
,
834 .set_fmt
= atmel_ssc_set_dai_fmt
,
835 .set_clkdiv
= atmel_ssc_set_dai_clkdiv
,
838 static struct snd_soc_dai_driver atmel_ssc_dai
= {
840 .stream_name
= "Playback",
843 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
846 .formats
= ATMEL_SSC_FORMATS
,},
848 .stream_name
= "Capture",
851 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
854 .formats
= ATMEL_SSC_FORMATS
,},
855 .ops
= &atmel_ssc_dai_ops
,
858 static const struct snd_soc_component_driver atmel_ssc_component
= {
860 .suspend
= pm_ptr(atmel_ssc_suspend
),
861 .resume
= pm_ptr(atmel_ssc_resume
),
862 .legacy_dai_naming
= 1,
865 static int asoc_ssc_init(struct device
*dev
)
867 struct ssc_device
*ssc
= dev_get_drvdata(dev
);
870 ret
= devm_snd_soc_register_component(dev
, &atmel_ssc_component
,
873 dev_err(dev
, "Could not register DAI: %d\n", ret
);
877 if (ssc
->pdata
->use_dma
)
878 ret
= atmel_pcm_dma_platform_register(dev
);
880 ret
= atmel_pcm_pdc_platform_register(dev
);
883 dev_err(dev
, "Could not register PCM: %d\n", ret
);
891 * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
892 * @ssc_id: SSD ID in [0, NUM_SSC_DEVICES[
894 int atmel_ssc_set_audio(int ssc_id
)
896 struct ssc_device
*ssc
;
898 /* If we can grab the SSC briefly to parent the DAI device off it */
899 ssc
= ssc_request(ssc_id
);
901 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
905 ssc_info
[ssc_id
].ssc
= ssc
;
908 return asoc_ssc_init(&ssc
->pdev
->dev
);
910 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio
);
912 void atmel_ssc_put_audio(int ssc_id
)
914 struct ssc_device
*ssc
= ssc_info
[ssc_id
].ssc
;
918 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio
);
920 /* Module information */
921 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
922 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
923 MODULE_LICENSE("GPL");