2 * atmel_ssc_dai.c -- ALSA SoC ATMEL SSC Audio Layer Platform driver
4 * Copyright (C) 2005 SAN People
5 * Copyright (C) 2008 Atmel
7 * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com>
10 * Based on at91-ssc.c by
11 * Frank Mandarino <fmandarino@endrelia.com>
12 * Based on pxa2xx Platform drivers by
13 * Liam Girdwood <lrg@slimlogic.co.uk>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/interrupt.h>
33 #include <linux/device.h>
34 #include <linux/delay.h>
35 #include <linux/clk.h>
36 #include <linux/atmel_pdc.h>
38 #include <linux/atmel-ssc.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/initval.h>
43 #include <sound/soc.h>
45 #include "atmel-pcm.h"
46 #include "atmel_ssc_dai.h"
49 #define NUM_SSC_DEVICES 3
52 * SSC PDC registers required by the PCM DMA engine.
54 static struct atmel_pdc_regs pdc_tx_reg
= {
57 .xnpr
= ATMEL_PDC_TNPR
,
58 .xncr
= ATMEL_PDC_TNCR
,
61 static struct atmel_pdc_regs pdc_rx_reg
= {
64 .xnpr
= ATMEL_PDC_RNPR
,
65 .xncr
= ATMEL_PDC_RNCR
,
69 * SSC & PDC status bits for transmit and receive.
71 static struct atmel_ssc_mask ssc_tx_mask
= {
72 .ssc_enable
= SSC_BIT(CR_TXEN
),
73 .ssc_disable
= SSC_BIT(CR_TXDIS
),
74 .ssc_endx
= SSC_BIT(SR_ENDTX
),
75 .ssc_endbuf
= SSC_BIT(SR_TXBUFE
),
76 .ssc_error
= SSC_BIT(SR_OVRUN
),
77 .pdc_enable
= ATMEL_PDC_TXTEN
,
78 .pdc_disable
= ATMEL_PDC_TXTDIS
,
81 static struct atmel_ssc_mask ssc_rx_mask
= {
82 .ssc_enable
= SSC_BIT(CR_RXEN
),
83 .ssc_disable
= SSC_BIT(CR_RXDIS
),
84 .ssc_endx
= SSC_BIT(SR_ENDRX
),
85 .ssc_endbuf
= SSC_BIT(SR_RXBUFF
),
86 .ssc_error
= SSC_BIT(SR_OVRUN
),
87 .pdc_enable
= ATMEL_PDC_RXTEN
,
88 .pdc_disable
= ATMEL_PDC_RXTDIS
,
95 static struct atmel_pcm_dma_params ssc_dma_params
[NUM_SSC_DEVICES
][2] = {
97 .name
= "SSC0 PCM out",
102 .name
= "SSC0 PCM in",
104 .mask
= &ssc_rx_mask
,
107 .name
= "SSC1 PCM out",
109 .mask
= &ssc_tx_mask
,
112 .name
= "SSC1 PCM in",
114 .mask
= &ssc_rx_mask
,
117 .name
= "SSC2 PCM out",
119 .mask
= &ssc_tx_mask
,
122 .name
= "SSC2 PCM in",
124 .mask
= &ssc_rx_mask
,
129 static struct atmel_ssc_info ssc_info
[NUM_SSC_DEVICES
] = {
132 .lock
= __SPIN_LOCK_UNLOCKED(ssc_info
[0].lock
),
133 .dir_mask
= SSC_DIR_MASK_UNUSED
,
138 .lock
= __SPIN_LOCK_UNLOCKED(ssc_info
[1].lock
),
139 .dir_mask
= SSC_DIR_MASK_UNUSED
,
144 .lock
= __SPIN_LOCK_UNLOCKED(ssc_info
[2].lock
),
145 .dir_mask
= SSC_DIR_MASK_UNUSED
,
152 * SSC interrupt handler. Passes PDC interrupts to the DMA
153 * interrupt handler in the PCM driver.
155 static irqreturn_t
atmel_ssc_interrupt(int irq
, void *dev_id
)
157 struct atmel_ssc_info
*ssc_p
= dev_id
;
158 struct atmel_pcm_dma_params
*dma_params
;
160 u32 ssc_substream_mask
;
163 ssc_sr
= (unsigned long)ssc_readl(ssc_p
->ssc
->regs
, SR
)
164 & (unsigned long)ssc_readl(ssc_p
->ssc
->regs
, IMR
);
167 * Loop through the substreams attached to this SSC. If
168 * a DMA-related interrupt occurred on that substream, call
169 * the DMA interrupt handler function, if one has been
170 * registered in the dma_params structure by the PCM driver.
172 for (i
= 0; i
< ARRAY_SIZE(ssc_p
->dma_params
); i
++) {
173 dma_params
= ssc_p
->dma_params
[i
];
175 if ((dma_params
!= NULL
) &&
176 (dma_params
->dma_intr_handler
!= NULL
)) {
177 ssc_substream_mask
= (dma_params
->mask
->ssc_endx
|
178 dma_params
->mask
->ssc_endbuf
);
179 if (ssc_sr
& ssc_substream_mask
) {
180 dma_params
->dma_intr_handler(ssc_sr
,
191 * When the bit clock is input, limit the maximum rate according to the
192 * Serial Clock Ratio Considerations section from the SSC documentation:
194 * The Transmitter and the Receiver can be programmed to operate
195 * with the clock signals provided on either the TK or RK pins.
196 * This allows the SSC to support many slave-mode data transfers.
197 * In this case, the maximum clock speed allowed on the RK pin is:
198 * - Peripheral clock divided by 2 if Receiver Frame Synchro is input
199 * - Peripheral clock divided by 3 if Receiver Frame Synchro is output
200 * In addition, the maximum clock speed allowed on the TK pin is:
201 * - Peripheral clock divided by 6 if Transmit Frame Synchro is input
202 * - Peripheral clock divided by 2 if Transmit Frame Synchro is output
204 * When the bit clock is output, limit the rate according to the
205 * SSC divider restrictions.
207 static int atmel_ssc_hw_rule_rate(struct snd_pcm_hw_params
*params
,
208 struct snd_pcm_hw_rule
*rule
)
210 struct atmel_ssc_info
*ssc_p
= rule
->private;
211 struct ssc_device
*ssc
= ssc_p
->ssc
;
212 struct snd_interval
*i
= hw_param_interval(params
, rule
->var
);
213 struct snd_interval t
;
214 struct snd_ratnum r
= {
219 unsigned int num
= 0, den
= 0;
224 frame_size
= snd_soc_params_to_frame_size(params
);
228 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
229 case SND_SOC_DAIFMT_CBM_CFS
:
230 if ((ssc_p
->dir_mask
& SSC_DIR_MASK_CAPTURE
)
231 && ssc
->clk_from_rk_pin
)
232 /* Receiver Frame Synchro (i.e. capture)
233 * is output (format is _CFS) and the RK pin
234 * is used for input (format is _CBM_).
239 case SND_SOC_DAIFMT_CBM_CFM
:
240 if ((ssc_p
->dir_mask
& SSC_DIR_MASK_PLAYBACK
)
241 && !ssc
->clk_from_rk_pin
)
242 /* Transmit Frame Synchro (i.e. playback)
243 * is input (format is _CFM) and the TK pin
244 * is used for input (format _CBM_ but not
251 switch (ssc_p
->daifmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
252 case SND_SOC_DAIFMT_CBS_CFS
:
253 r
.num
= ssc_p
->mck_rate
/ mck_div
/ frame_size
;
255 ret
= snd_interval_ratnum(i
, 1, &r
, &num
, &den
);
256 if (ret
>= 0 && den
&& rule
->var
== SNDRV_PCM_HW_PARAM_RATE
) {
257 params
->rate_num
= num
;
258 params
->rate_den
= den
;
262 case SND_SOC_DAIFMT_CBM_CFS
:
263 case SND_SOC_DAIFMT_CBM_CFM
:
265 t
.max
= ssc_p
->mck_rate
/ mck_div
/ frame_size
;
266 t
.openmin
= t
.openmax
= 0;
268 ret
= snd_interval_refine(i
, &t
);
279 /*-------------------------------------------------------------------------*\
281 \*-------------------------------------------------------------------------*/
283 * Startup. Only that one substream allowed in each direction.
285 static int atmel_ssc_startup(struct snd_pcm_substream
*substream
,
286 struct snd_soc_dai
*dai
)
288 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
289 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
290 struct atmel_pcm_dma_params
*dma_params
;
294 pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
295 ssc_readl(ssc_p
->ssc
->regs
, SR
));
297 /* Enable PMC peripheral clock for this SSC */
298 pr_debug("atmel_ssc_dai: Starting clock\n");
299 clk_enable(ssc_p
->ssc
->clk
);
300 ssc_p
->mck_rate
= clk_get_rate(ssc_p
->ssc
->clk
);
302 /* Reset the SSC unless initialized to keep it in a clean state */
303 if (!ssc_p
->initialized
)
304 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
306 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
308 dir_mask
= SSC_DIR_MASK_PLAYBACK
;
311 dir_mask
= SSC_DIR_MASK_CAPTURE
;
314 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
315 SNDRV_PCM_HW_PARAM_RATE
,
316 atmel_ssc_hw_rule_rate
,
318 SNDRV_PCM_HW_PARAM_FRAME_BITS
,
319 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
321 dev_err(dai
->dev
, "Failed to specify rate rule: %d\n", ret
);
325 dma_params
= &ssc_dma_params
[pdev
->id
][dir
];
326 dma_params
->ssc
= ssc_p
->ssc
;
327 dma_params
->substream
= substream
;
329 ssc_p
->dma_params
[dir
] = dma_params
;
331 snd_soc_dai_set_dma_data(dai
, substream
, dma_params
);
333 spin_lock_irq(&ssc_p
->lock
);
334 if (ssc_p
->dir_mask
& dir_mask
) {
335 spin_unlock_irq(&ssc_p
->lock
);
338 ssc_p
->dir_mask
|= dir_mask
;
339 spin_unlock_irq(&ssc_p
->lock
);
345 * Shutdown. Clear DMA parameters and shutdown the SSC if there
346 * are no other substreams open.
348 static void atmel_ssc_shutdown(struct snd_pcm_substream
*substream
,
349 struct snd_soc_dai
*dai
)
351 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
352 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
353 struct atmel_pcm_dma_params
*dma_params
;
356 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
361 dma_params
= ssc_p
->dma_params
[dir
];
363 if (dma_params
!= NULL
) {
364 dma_params
->ssc
= NULL
;
365 dma_params
->substream
= NULL
;
366 ssc_p
->dma_params
[dir
] = NULL
;
371 spin_lock_irq(&ssc_p
->lock
);
372 ssc_p
->dir_mask
&= ~dir_mask
;
373 if (!ssc_p
->dir_mask
) {
374 if (ssc_p
->initialized
) {
375 free_irq(ssc_p
->ssc
->irq
, ssc_p
);
376 ssc_p
->initialized
= 0;
380 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
381 /* Clear the SSC dividers */
382 ssc_p
->cmr_div
= ssc_p
->tcmr_period
= ssc_p
->rcmr_period
= 0;
384 spin_unlock_irq(&ssc_p
->lock
);
386 /* Shutdown the SSC clock. */
387 pr_debug("atmel_ssc_dai: Stopping clock\n");
388 clk_disable(ssc_p
->ssc
->clk
);
393 * Record the DAI format for use in hw_params().
395 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
398 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
399 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
406 * Record SSC clock dividers for use in hw_params().
408 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai
*cpu_dai
,
411 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
412 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
415 case ATMEL_SSC_CMR_DIV
:
417 * The same master clock divider is used for both
418 * transmit and receive, so if a value has already
419 * been set, it must match this value.
421 if (ssc_p
->dir_mask
!=
422 (SSC_DIR_MASK_PLAYBACK
| SSC_DIR_MASK_CAPTURE
))
423 ssc_p
->cmr_div
= div
;
424 else if (ssc_p
->cmr_div
== 0)
425 ssc_p
->cmr_div
= div
;
427 if (div
!= ssc_p
->cmr_div
)
431 case ATMEL_SSC_TCMR_PERIOD
:
432 ssc_p
->tcmr_period
= div
;
435 case ATMEL_SSC_RCMR_PERIOD
:
436 ssc_p
->rcmr_period
= div
;
449 static int atmel_ssc_hw_params(struct snd_pcm_substream
*substream
,
450 struct snd_pcm_hw_params
*params
,
451 struct snd_soc_dai
*dai
)
453 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
455 struct atmel_ssc_info
*ssc_p
= &ssc_info
[id
];
456 struct ssc_device
*ssc
= ssc_p
->ssc
;
457 struct atmel_pcm_dma_params
*dma_params
;
458 int dir
, channels
, bits
;
459 u32 tfmr
, rfmr
, tcmr
, rcmr
;
461 int fslen
, fslen_ext
;
464 * Currently, there is only one set of dma params for
465 * each direction. If more are added, this code will
466 * have to be changed to select the proper set.
468 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
473 dma_params
= ssc_p
->dma_params
[dir
];
475 channels
= params_channels(params
);
478 * Determine sample size in bits and the PDC increment.
480 switch (params_format(params
)) {
481 case SNDRV_PCM_FORMAT_S8
:
483 dma_params
->pdc_xfer_size
= 1;
485 case SNDRV_PCM_FORMAT_S16_LE
:
487 dma_params
->pdc_xfer_size
= 2;
489 case SNDRV_PCM_FORMAT_S24_LE
:
491 dma_params
->pdc_xfer_size
= 4;
493 case SNDRV_PCM_FORMAT_S32_LE
:
495 dma_params
->pdc_xfer_size
= 4;
498 printk(KERN_WARNING
"atmel_ssc_dai: unsupported PCM format");
503 * Compute SSC register settings.
505 switch (ssc_p
->daifmt
506 & (SND_SOC_DAIFMT_FORMAT_MASK
| SND_SOC_DAIFMT_MASTER_MASK
)) {
508 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
:
510 * I2S format, SSC provides BCLK and LRC clocks.
512 * The SSC transmit and receive clocks are generated
513 * from the MCK divider, and the BCLK signal
514 * is output on the SSC TK line.
517 if (bits
> 16 && !ssc
->pdata
->has_fslen_ext
) {
519 "sample size %d is too large for SSC device\n",
524 fslen_ext
= (bits
- 1) / 16;
525 fslen
= (bits
- 1) % 16;
527 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
528 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
529 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
530 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
531 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
532 | SSC_BF(RCMR_CKS
, SSC_CKS_DIV
);
534 rfmr
= SSC_BF(RFMR_FSLEN_EXT
, fslen_ext
)
535 | SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
536 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NEGATIVE
)
537 | SSC_BF(RFMR_FSLEN
, fslen
)
538 | SSC_BF(RFMR_DATNB
, (channels
- 1))
540 | SSC_BF(RFMR_LOOP
, 0)
541 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
543 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
544 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
545 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
546 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
547 | SSC_BF(TCMR_CKO
, SSC_CKO_CONTINUOUS
)
548 | SSC_BF(TCMR_CKS
, SSC_CKS_DIV
);
550 tfmr
= SSC_BF(TFMR_FSLEN_EXT
, fslen_ext
)
551 | SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
552 | SSC_BF(TFMR_FSDEN
, 0)
553 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NEGATIVE
)
554 | SSC_BF(TFMR_FSLEN
, fslen
)
555 | SSC_BF(TFMR_DATNB
, (channels
- 1))
557 | SSC_BF(TFMR_DATDEF
, 0)
558 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
561 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBM_CFM
:
562 /* I2S format, CODEC supplies BCLK and LRC clocks. */
563 rcmr
= SSC_BF(RCMR_PERIOD
, 0)
564 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
565 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
566 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
567 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
568 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
569 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
571 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
572 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NONE
)
573 | SSC_BF(RFMR_FSLEN
, 0)
574 | SSC_BF(RFMR_DATNB
, (channels
- 1))
576 | SSC_BF(RFMR_LOOP
, 0)
577 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
579 tcmr
= SSC_BF(TCMR_PERIOD
, 0)
580 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
581 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
582 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
583 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
584 | SSC_BF(TCMR_CKS
, ssc
->clk_from_rk_pin
?
585 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
587 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
588 | SSC_BF(TFMR_FSDEN
, 0)
589 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NONE
)
590 | SSC_BF(TFMR_FSLEN
, 0)
591 | SSC_BF(TFMR_DATNB
, (channels
- 1))
593 | SSC_BF(TFMR_DATDEF
, 0)
594 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
597 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBM_CFS
:
598 /* I2S format, CODEC supplies BCLK, SSC supplies LRCLK. */
599 if (bits
> 16 && !ssc
->pdata
->has_fslen_ext
) {
601 "sample size %d is too large for SSC device\n",
606 fslen_ext
= (bits
- 1) / 16;
607 fslen
= (bits
- 1) % 16;
609 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
610 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
611 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
612 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
613 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
614 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
615 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
617 rfmr
= SSC_BF(RFMR_FSLEN_EXT
, fslen_ext
)
618 | SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
619 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NEGATIVE
)
620 | SSC_BF(RFMR_FSLEN
, fslen
)
621 | SSC_BF(RFMR_DATNB
, (channels
- 1))
623 | SSC_BF(RFMR_LOOP
, 0)
624 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
626 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
627 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
628 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
629 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
630 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
631 | SSC_BF(TCMR_CKS
, ssc
->clk_from_rk_pin
?
632 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
634 tfmr
= SSC_BF(TFMR_FSLEN_EXT
, fslen_ext
)
635 | SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_NEGATIVE
)
636 | SSC_BF(TFMR_FSDEN
, 0)
637 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NEGATIVE
)
638 | SSC_BF(TFMR_FSLEN
, fslen
)
639 | SSC_BF(TFMR_DATNB
, (channels
- 1))
641 | SSC_BF(TFMR_DATDEF
, 0)
642 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
645 case SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_CBS_CFS
:
647 * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
649 * The SSC transmit and receive clocks are generated from the
650 * MCK divider, and the BCLK signal is output
651 * on the SSC TK line.
653 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
654 | SSC_BF(RCMR_STTDLY
, 1)
655 | SSC_BF(RCMR_START
, SSC_START_RISING_RF
)
656 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
657 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
658 | SSC_BF(RCMR_CKS
, SSC_CKS_DIV
);
660 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
661 | SSC_BF(RFMR_FSOS
, SSC_FSOS_POSITIVE
)
662 | SSC_BF(RFMR_FSLEN
, 0)
663 | SSC_BF(RFMR_DATNB
, (channels
- 1))
665 | SSC_BF(RFMR_LOOP
, 0)
666 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
668 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
669 | SSC_BF(TCMR_STTDLY
, 1)
670 | SSC_BF(TCMR_START
, SSC_START_RISING_RF
)
671 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
672 | SSC_BF(TCMR_CKO
, SSC_CKO_CONTINUOUS
)
673 | SSC_BF(TCMR_CKS
, SSC_CKS_DIV
);
675 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
676 | SSC_BF(TFMR_FSDEN
, 0)
677 | SSC_BF(TFMR_FSOS
, SSC_FSOS_POSITIVE
)
678 | SSC_BF(TFMR_FSLEN
, 0)
679 | SSC_BF(TFMR_DATNB
, (channels
- 1))
681 | SSC_BF(TFMR_DATDEF
, 0)
682 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
685 case SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_CBM_CFM
:
687 * DSP/PCM Mode A format, CODEC supplies BCLK and LRC clocks.
689 * Data is transferred on first BCLK after LRC pulse rising
690 * edge.If stereo, the right channel data is contiguous with
691 * the left channel data.
693 rcmr
= SSC_BF(RCMR_PERIOD
, 0)
694 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
695 | SSC_BF(RCMR_START
, SSC_START_RISING_RF
)
696 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
697 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
698 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
699 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
701 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
702 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NONE
)
703 | SSC_BF(RFMR_FSLEN
, 0)
704 | SSC_BF(RFMR_DATNB
, (channels
- 1))
706 | SSC_BF(RFMR_LOOP
, 0)
707 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
709 tcmr
= SSC_BF(TCMR_PERIOD
, 0)
710 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
711 | SSC_BF(TCMR_START
, SSC_START_RISING_RF
)
712 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
713 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
714 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
715 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
717 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
718 | SSC_BF(TFMR_FSDEN
, 0)
719 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NONE
)
720 | SSC_BF(TFMR_FSLEN
, 0)
721 | SSC_BF(TFMR_DATNB
, (channels
- 1))
723 | SSC_BF(TFMR_DATDEF
, 0)
724 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
728 printk(KERN_WARNING
"atmel_ssc_dai: unsupported DAI format 0x%x\n",
732 pr_debug("atmel_ssc_hw_params: "
733 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
734 rcmr
, rfmr
, tcmr
, tfmr
);
736 if (!ssc_p
->initialized
) {
737 if (!ssc_p
->ssc
->pdata
->use_dma
) {
738 ssc_writel(ssc_p
->ssc
->regs
, PDC_RPR
, 0);
739 ssc_writel(ssc_p
->ssc
->regs
, PDC_RCR
, 0);
740 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNPR
, 0);
741 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNCR
, 0);
743 ssc_writel(ssc_p
->ssc
->regs
, PDC_TPR
, 0);
744 ssc_writel(ssc_p
->ssc
->regs
, PDC_TCR
, 0);
745 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNPR
, 0);
746 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNCR
, 0);
749 ret
= request_irq(ssc_p
->ssc
->irq
, atmel_ssc_interrupt
, 0,
753 "atmel_ssc_dai: request_irq failure\n");
754 pr_debug("Atmel_ssc_dai: Stoping clock\n");
755 clk_disable(ssc_p
->ssc
->clk
);
759 ssc_p
->initialized
= 1;
762 /* set SSC clock mode register */
763 ssc_writel(ssc_p
->ssc
->regs
, CMR
, ssc_p
->cmr_div
);
765 /* set receive clock mode and format */
766 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, rcmr
);
767 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, rfmr
);
769 /* set transmit clock mode and format */
770 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, tcmr
);
771 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, tfmr
);
773 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
778 static int atmel_ssc_prepare(struct snd_pcm_substream
*substream
,
779 struct snd_soc_dai
*dai
)
781 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
782 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
783 struct atmel_pcm_dma_params
*dma_params
;
786 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
791 dma_params
= ssc_p
->dma_params
[dir
];
793 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
794 ssc_writel(ssc_p
->ssc
->regs
, IDR
, dma_params
->mask
->ssc_error
);
796 pr_debug("%s enabled SSC_SR=0x%08x\n",
797 dir
? "receive" : "transmit",
798 ssc_readl(ssc_p
->ssc
->regs
, SR
));
802 static int atmel_ssc_trigger(struct snd_pcm_substream
*substream
,
803 int cmd
, struct snd_soc_dai
*dai
)
805 struct platform_device
*pdev
= to_platform_device(dai
->dev
);
806 struct atmel_ssc_info
*ssc_p
= &ssc_info
[pdev
->id
];
807 struct atmel_pcm_dma_params
*dma_params
;
810 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
815 dma_params
= ssc_p
->dma_params
[dir
];
818 case SNDRV_PCM_TRIGGER_START
:
819 case SNDRV_PCM_TRIGGER_RESUME
:
820 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
821 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_enable
);
824 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
832 static int atmel_ssc_suspend(struct snd_soc_dai
*cpu_dai
)
834 struct atmel_ssc_info
*ssc_p
;
835 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
837 if (!cpu_dai
->active
)
840 ssc_p
= &ssc_info
[pdev
->id
];
842 /* Save the status register before disabling transmit and receive */
843 ssc_p
->ssc_state
.ssc_sr
= ssc_readl(ssc_p
->ssc
->regs
, SR
);
844 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
) | SSC_BIT(CR_RXDIS
));
846 /* Save the current interrupt mask, then disable unmasked interrupts */
847 ssc_p
->ssc_state
.ssc_imr
= ssc_readl(ssc_p
->ssc
->regs
, IMR
);
848 ssc_writel(ssc_p
->ssc
->regs
, IDR
, ssc_p
->ssc_state
.ssc_imr
);
850 ssc_p
->ssc_state
.ssc_cmr
= ssc_readl(ssc_p
->ssc
->regs
, CMR
);
851 ssc_p
->ssc_state
.ssc_rcmr
= ssc_readl(ssc_p
->ssc
->regs
, RCMR
);
852 ssc_p
->ssc_state
.ssc_rfmr
= ssc_readl(ssc_p
->ssc
->regs
, RFMR
);
853 ssc_p
->ssc_state
.ssc_tcmr
= ssc_readl(ssc_p
->ssc
->regs
, TCMR
);
854 ssc_p
->ssc_state
.ssc_tfmr
= ssc_readl(ssc_p
->ssc
->regs
, TFMR
);
861 static int atmel_ssc_resume(struct snd_soc_dai
*cpu_dai
)
863 struct atmel_ssc_info
*ssc_p
;
864 struct platform_device
*pdev
= to_platform_device(cpu_dai
->dev
);
867 if (!cpu_dai
->active
)
870 ssc_p
= &ssc_info
[pdev
->id
];
872 /* restore SSC register settings */
873 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, ssc_p
->ssc_state
.ssc_tfmr
);
874 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, ssc_p
->ssc_state
.ssc_tcmr
);
875 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, ssc_p
->ssc_state
.ssc_rfmr
);
876 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, ssc_p
->ssc_state
.ssc_rcmr
);
877 ssc_writel(ssc_p
->ssc
->regs
, CMR
, ssc_p
->ssc_state
.ssc_cmr
);
879 /* re-enable interrupts */
880 ssc_writel(ssc_p
->ssc
->regs
, IER
, ssc_p
->ssc_state
.ssc_imr
);
882 /* Re-enable receive and transmit as appropriate */
885 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_RXEN
)) ? SSC_BIT(CR_RXEN
) : 0;
887 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_TXEN
)) ? SSC_BIT(CR_TXEN
) : 0;
888 ssc_writel(ssc_p
->ssc
->regs
, CR
, cr
);
892 #else /* CONFIG_PM */
893 # define atmel_ssc_suspend NULL
894 # define atmel_ssc_resume NULL
895 #endif /* CONFIG_PM */
897 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
898 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
900 static const struct snd_soc_dai_ops atmel_ssc_dai_ops
= {
901 .startup
= atmel_ssc_startup
,
902 .shutdown
= atmel_ssc_shutdown
,
903 .prepare
= atmel_ssc_prepare
,
904 .trigger
= atmel_ssc_trigger
,
905 .hw_params
= atmel_ssc_hw_params
,
906 .set_fmt
= atmel_ssc_set_dai_fmt
,
907 .set_clkdiv
= atmel_ssc_set_dai_clkdiv
,
910 static struct snd_soc_dai_driver atmel_ssc_dai
= {
911 .suspend
= atmel_ssc_suspend
,
912 .resume
= atmel_ssc_resume
,
916 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
919 .formats
= ATMEL_SSC_FORMATS
,},
923 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
926 .formats
= ATMEL_SSC_FORMATS
,},
927 .ops
= &atmel_ssc_dai_ops
,
930 static const struct snd_soc_component_driver atmel_ssc_component
= {
934 static int asoc_ssc_init(struct device
*dev
)
936 struct platform_device
*pdev
= to_platform_device(dev
);
937 struct ssc_device
*ssc
= platform_get_drvdata(pdev
);
940 ret
= snd_soc_register_component(dev
, &atmel_ssc_component
,
943 dev_err(dev
, "Could not register DAI: %d\n", ret
);
947 if (ssc
->pdata
->use_dma
)
948 ret
= atmel_pcm_dma_platform_register(dev
);
950 ret
= atmel_pcm_pdc_platform_register(dev
);
953 dev_err(dev
, "Could not register PCM: %d\n", ret
);
954 goto err_unregister_dai
;
960 snd_soc_unregister_component(dev
);
965 static void asoc_ssc_exit(struct device
*dev
)
967 struct platform_device
*pdev
= to_platform_device(dev
);
968 struct ssc_device
*ssc
= platform_get_drvdata(pdev
);
970 if (ssc
->pdata
->use_dma
)
971 atmel_pcm_dma_platform_unregister(dev
);
973 atmel_pcm_pdc_platform_unregister(dev
);
975 snd_soc_unregister_component(dev
);
979 * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
981 int atmel_ssc_set_audio(int ssc_id
)
983 struct ssc_device
*ssc
;
986 /* If we can grab the SSC briefly to parent the DAI device off it */
987 ssc
= ssc_request(ssc_id
);
989 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
993 ssc_info
[ssc_id
].ssc
= ssc
;
996 ret
= asoc_ssc_init(&ssc
->pdev
->dev
);
1000 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio
);
1002 void atmel_ssc_put_audio(int ssc_id
)
1004 struct ssc_device
*ssc
= ssc_info
[ssc_id
].ssc
;
1006 asoc_ssc_exit(&ssc
->pdev
->dev
);
1009 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio
);
1011 /* Module information */
1012 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
1013 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
1014 MODULE_LICENSE("GPL");