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 atmel_ssc_info
*ssc_p
= &ssc_info
[dai
->id
];
289 struct atmel_pcm_dma_params
*dma_params
;
293 pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
294 ssc_readl(ssc_p
->ssc
->regs
, SR
));
296 /* Enable PMC peripheral clock for this SSC */
297 pr_debug("atmel_ssc_dai: Starting clock\n");
298 clk_enable(ssc_p
->ssc
->clk
);
299 ssc_p
->mck_rate
= clk_get_rate(ssc_p
->ssc
->clk
);
301 /* Reset the SSC to keep it at a clean status */
302 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
304 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
306 dir_mask
= SSC_DIR_MASK_PLAYBACK
;
309 dir_mask
= SSC_DIR_MASK_CAPTURE
;
312 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
313 SNDRV_PCM_HW_PARAM_RATE
,
314 atmel_ssc_hw_rule_rate
,
316 SNDRV_PCM_HW_PARAM_FRAME_BITS
,
317 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
319 dev_err(dai
->dev
, "Failed to specify rate rule: %d\n", ret
);
323 dma_params
= &ssc_dma_params
[dai
->id
][dir
];
324 dma_params
->ssc
= ssc_p
->ssc
;
325 dma_params
->substream
= substream
;
327 ssc_p
->dma_params
[dir
] = dma_params
;
329 snd_soc_dai_set_dma_data(dai
, substream
, dma_params
);
331 spin_lock_irq(&ssc_p
->lock
);
332 if (ssc_p
->dir_mask
& dir_mask
) {
333 spin_unlock_irq(&ssc_p
->lock
);
336 ssc_p
->dir_mask
|= dir_mask
;
337 spin_unlock_irq(&ssc_p
->lock
);
343 * Shutdown. Clear DMA parameters and shutdown the SSC if there
344 * are no other substreams open.
346 static void atmel_ssc_shutdown(struct snd_pcm_substream
*substream
,
347 struct snd_soc_dai
*dai
)
349 struct atmel_ssc_info
*ssc_p
= &ssc_info
[dai
->id
];
350 struct atmel_pcm_dma_params
*dma_params
;
353 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
358 dma_params
= ssc_p
->dma_params
[dir
];
360 if (dma_params
!= NULL
) {
361 dma_params
->ssc
= NULL
;
362 dma_params
->substream
= NULL
;
363 ssc_p
->dma_params
[dir
] = NULL
;
368 spin_lock_irq(&ssc_p
->lock
);
369 ssc_p
->dir_mask
&= ~dir_mask
;
370 if (!ssc_p
->dir_mask
) {
371 if (ssc_p
->initialized
) {
372 free_irq(ssc_p
->ssc
->irq
, ssc_p
);
373 ssc_p
->initialized
= 0;
377 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_SWRST
));
378 /* Clear the SSC dividers */
379 ssc_p
->cmr_div
= ssc_p
->tcmr_period
= ssc_p
->rcmr_period
= 0;
381 spin_unlock_irq(&ssc_p
->lock
);
383 /* Shutdown the SSC clock. */
384 pr_debug("atmel_ssc_dai: Stopping clock\n");
385 clk_disable(ssc_p
->ssc
->clk
);
390 * Record the DAI format for use in hw_params().
392 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai
*cpu_dai
,
395 struct atmel_ssc_info
*ssc_p
= &ssc_info
[cpu_dai
->id
];
402 * Record SSC clock dividers for use in hw_params().
404 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai
*cpu_dai
,
407 struct atmel_ssc_info
*ssc_p
= &ssc_info
[cpu_dai
->id
];
410 case ATMEL_SSC_CMR_DIV
:
412 * The same master clock divider is used for both
413 * transmit and receive, so if a value has already
414 * been set, it must match this value.
416 if (ssc_p
->dir_mask
!=
417 (SSC_DIR_MASK_PLAYBACK
| SSC_DIR_MASK_CAPTURE
))
418 ssc_p
->cmr_div
= div
;
419 else if (ssc_p
->cmr_div
== 0)
420 ssc_p
->cmr_div
= div
;
422 if (div
!= ssc_p
->cmr_div
)
426 case ATMEL_SSC_TCMR_PERIOD
:
427 ssc_p
->tcmr_period
= div
;
430 case ATMEL_SSC_RCMR_PERIOD
:
431 ssc_p
->rcmr_period
= div
;
444 static int atmel_ssc_hw_params(struct snd_pcm_substream
*substream
,
445 struct snd_pcm_hw_params
*params
,
446 struct snd_soc_dai
*dai
)
449 struct atmel_ssc_info
*ssc_p
= &ssc_info
[id
];
450 struct ssc_device
*ssc
= ssc_p
->ssc
;
451 struct atmel_pcm_dma_params
*dma_params
;
452 int dir
, channels
, bits
;
453 u32 tfmr
, rfmr
, tcmr
, rcmr
;
455 int fslen
, fslen_ext
;
458 * Currently, there is only one set of dma params for
459 * each direction. If more are added, this code will
460 * have to be changed to select the proper set.
462 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
467 dma_params
= ssc_p
->dma_params
[dir
];
469 channels
= params_channels(params
);
472 * Determine sample size in bits and the PDC increment.
474 switch (params_format(params
)) {
475 case SNDRV_PCM_FORMAT_S8
:
477 dma_params
->pdc_xfer_size
= 1;
479 case SNDRV_PCM_FORMAT_S16_LE
:
481 dma_params
->pdc_xfer_size
= 2;
483 case SNDRV_PCM_FORMAT_S24_LE
:
485 dma_params
->pdc_xfer_size
= 4;
487 case SNDRV_PCM_FORMAT_S32_LE
:
489 dma_params
->pdc_xfer_size
= 4;
492 printk(KERN_WARNING
"atmel_ssc_dai: unsupported PCM format");
497 * Compute SSC register settings.
499 switch (ssc_p
->daifmt
500 & (SND_SOC_DAIFMT_FORMAT_MASK
| SND_SOC_DAIFMT_MASTER_MASK
)) {
502 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
:
504 * I2S format, SSC provides BCLK and LRC clocks.
506 * The SSC transmit and receive clocks are generated
507 * from the MCK divider, and the BCLK signal
508 * is output on the SSC TK line.
511 if (bits
> 16 && !ssc
->pdata
->has_fslen_ext
) {
513 "sample size %d is too large for SSC device\n",
518 fslen_ext
= (bits
- 1) / 16;
519 fslen
= (bits
- 1) % 16;
521 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
522 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
523 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
524 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
525 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
526 | SSC_BF(RCMR_CKS
, SSC_CKS_DIV
);
528 rfmr
= SSC_BF(RFMR_FSLEN_EXT
, fslen_ext
)
529 | SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
530 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NEGATIVE
)
531 | SSC_BF(RFMR_FSLEN
, fslen
)
532 | SSC_BF(RFMR_DATNB
, (channels
- 1))
534 | SSC_BF(RFMR_LOOP
, 0)
535 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
537 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
538 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
539 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
540 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
541 | SSC_BF(TCMR_CKO
, SSC_CKO_CONTINUOUS
)
542 | SSC_BF(TCMR_CKS
, SSC_CKS_DIV
);
544 tfmr
= SSC_BF(TFMR_FSLEN_EXT
, fslen_ext
)
545 | SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
546 | SSC_BF(TFMR_FSDEN
, 0)
547 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NEGATIVE
)
548 | SSC_BF(TFMR_FSLEN
, fslen
)
549 | SSC_BF(TFMR_DATNB
, (channels
- 1))
551 | SSC_BF(TFMR_DATDEF
, 0)
552 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
555 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBM_CFM
:
556 /* I2S format, CODEC supplies BCLK and LRC clocks. */
557 rcmr
= SSC_BF(RCMR_PERIOD
, 0)
558 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
559 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
560 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
561 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
562 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
563 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
565 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
566 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NONE
)
567 | SSC_BF(RFMR_FSLEN
, 0)
568 | SSC_BF(RFMR_DATNB
, (channels
- 1))
570 | SSC_BF(RFMR_LOOP
, 0)
571 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
573 tcmr
= SSC_BF(TCMR_PERIOD
, 0)
574 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
575 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
576 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
577 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
578 | SSC_BF(TCMR_CKS
, ssc
->clk_from_rk_pin
?
579 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
581 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
582 | SSC_BF(TFMR_FSDEN
, 0)
583 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NONE
)
584 | SSC_BF(TFMR_FSLEN
, 0)
585 | SSC_BF(TFMR_DATNB
, (channels
- 1))
587 | SSC_BF(TFMR_DATDEF
, 0)
588 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
591 case SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBM_CFS
:
592 /* I2S format, CODEC supplies BCLK, SSC supplies LRCLK. */
593 if (bits
> 16 && !ssc
->pdata
->has_fslen_ext
) {
595 "sample size %d is too large for SSC device\n",
600 fslen_ext
= (bits
- 1) / 16;
601 fslen
= (bits
- 1) % 16;
603 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
604 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
605 | SSC_BF(RCMR_START
, SSC_START_FALLING_RF
)
606 | SSC_BF(RCMR_CKI
, SSC_CKI_RISING
)
607 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
608 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
609 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
611 rfmr
= SSC_BF(RFMR_FSLEN_EXT
, fslen_ext
)
612 | SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
613 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NEGATIVE
)
614 | SSC_BF(RFMR_FSLEN
, fslen
)
615 | SSC_BF(RFMR_DATNB
, (channels
- 1))
617 | SSC_BF(RFMR_LOOP
, 0)
618 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
620 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
621 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
622 | SSC_BF(TCMR_START
, SSC_START_FALLING_RF
)
623 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
624 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
625 | SSC_BF(TCMR_CKS
, ssc
->clk_from_rk_pin
?
626 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
628 tfmr
= SSC_BF(TFMR_FSLEN_EXT
, fslen_ext
)
629 | SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_NEGATIVE
)
630 | SSC_BF(TFMR_FSDEN
, 0)
631 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NEGATIVE
)
632 | SSC_BF(TFMR_FSLEN
, fslen
)
633 | SSC_BF(TFMR_DATNB
, (channels
- 1))
635 | SSC_BF(TFMR_DATDEF
, 0)
636 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
639 case SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_CBS_CFS
:
641 * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
643 * The SSC transmit and receive clocks are generated from the
644 * MCK divider, and the BCLK signal is output
645 * on the SSC TK line.
647 rcmr
= SSC_BF(RCMR_PERIOD
, ssc_p
->rcmr_period
)
648 | SSC_BF(RCMR_STTDLY
, 1)
649 | SSC_BF(RCMR_START
, SSC_START_RISING_RF
)
650 | SSC_BF(RCMR_CKI
, SSC_CKI_FALLING
)
651 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
652 | SSC_BF(RCMR_CKS
, SSC_CKS_DIV
);
654 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
655 | SSC_BF(RFMR_FSOS
, SSC_FSOS_POSITIVE
)
656 | SSC_BF(RFMR_FSLEN
, 0)
657 | SSC_BF(RFMR_DATNB
, (channels
- 1))
659 | SSC_BF(RFMR_LOOP
, 0)
660 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
662 tcmr
= SSC_BF(TCMR_PERIOD
, ssc_p
->tcmr_period
)
663 | SSC_BF(TCMR_STTDLY
, 1)
664 | SSC_BF(TCMR_START
, SSC_START_RISING_RF
)
665 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
666 | SSC_BF(TCMR_CKO
, SSC_CKO_CONTINUOUS
)
667 | SSC_BF(TCMR_CKS
, SSC_CKS_DIV
);
669 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
670 | SSC_BF(TFMR_FSDEN
, 0)
671 | SSC_BF(TFMR_FSOS
, SSC_FSOS_POSITIVE
)
672 | SSC_BF(TFMR_FSLEN
, 0)
673 | SSC_BF(TFMR_DATNB
, (channels
- 1))
675 | SSC_BF(TFMR_DATDEF
, 0)
676 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
679 case SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_CBM_CFM
:
681 * DSP/PCM Mode A format, CODEC supplies BCLK and LRC clocks.
683 * Data is transferred on first BCLK after LRC pulse rising
684 * edge.If stereo, the right channel data is contiguous with
685 * the left channel data.
687 rcmr
= SSC_BF(RCMR_PERIOD
, 0)
688 | SSC_BF(RCMR_STTDLY
, START_DELAY
)
689 | SSC_BF(RCMR_START
, SSC_START_RISING_RF
)
690 | SSC_BF(RCMR_CKI
, SSC_CKI_FALLING
)
691 | SSC_BF(RCMR_CKO
, SSC_CKO_NONE
)
692 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
693 SSC_CKS_PIN
: SSC_CKS_CLOCK
);
695 rfmr
= SSC_BF(RFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
696 | SSC_BF(RFMR_FSOS
, SSC_FSOS_NONE
)
697 | SSC_BF(RFMR_FSLEN
, 0)
698 | SSC_BF(RFMR_DATNB
, (channels
- 1))
700 | SSC_BF(RFMR_LOOP
, 0)
701 | SSC_BF(RFMR_DATLEN
, (bits
- 1));
703 tcmr
= SSC_BF(TCMR_PERIOD
, 0)
704 | SSC_BF(TCMR_STTDLY
, START_DELAY
)
705 | SSC_BF(TCMR_START
, SSC_START_RISING_RF
)
706 | SSC_BF(TCMR_CKI
, SSC_CKI_FALLING
)
707 | SSC_BF(TCMR_CKO
, SSC_CKO_NONE
)
708 | SSC_BF(RCMR_CKS
, ssc
->clk_from_rk_pin
?
709 SSC_CKS_CLOCK
: SSC_CKS_PIN
);
711 tfmr
= SSC_BF(TFMR_FSEDGE
, SSC_FSEDGE_POSITIVE
)
712 | SSC_BF(TFMR_FSDEN
, 0)
713 | SSC_BF(TFMR_FSOS
, SSC_FSOS_NONE
)
714 | SSC_BF(TFMR_FSLEN
, 0)
715 | SSC_BF(TFMR_DATNB
, (channels
- 1))
717 | SSC_BF(TFMR_DATDEF
, 0)
718 | SSC_BF(TFMR_DATLEN
, (bits
- 1));
722 printk(KERN_WARNING
"atmel_ssc_dai: unsupported DAI format 0x%x\n",
726 pr_debug("atmel_ssc_hw_params: "
727 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
728 rcmr
, rfmr
, tcmr
, tfmr
);
730 if (!ssc_p
->initialized
) {
731 if (!ssc_p
->ssc
->pdata
->use_dma
) {
732 ssc_writel(ssc_p
->ssc
->regs
, PDC_RPR
, 0);
733 ssc_writel(ssc_p
->ssc
->regs
, PDC_RCR
, 0);
734 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNPR
, 0);
735 ssc_writel(ssc_p
->ssc
->regs
, PDC_RNCR
, 0);
737 ssc_writel(ssc_p
->ssc
->regs
, PDC_TPR
, 0);
738 ssc_writel(ssc_p
->ssc
->regs
, PDC_TCR
, 0);
739 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNPR
, 0);
740 ssc_writel(ssc_p
->ssc
->regs
, PDC_TNCR
, 0);
743 ret
= request_irq(ssc_p
->ssc
->irq
, atmel_ssc_interrupt
, 0,
747 "atmel_ssc_dai: request_irq failure\n");
748 pr_debug("Atmel_ssc_dai: Stoping clock\n");
749 clk_disable(ssc_p
->ssc
->clk
);
753 ssc_p
->initialized
= 1;
756 /* set SSC clock mode register */
757 ssc_writel(ssc_p
->ssc
->regs
, CMR
, ssc_p
->cmr_div
);
759 /* set receive clock mode and format */
760 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, rcmr
);
761 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, rfmr
);
763 /* set transmit clock mode and format */
764 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, tcmr
);
765 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, tfmr
);
767 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
772 static int atmel_ssc_prepare(struct snd_pcm_substream
*substream
,
773 struct snd_soc_dai
*dai
)
775 struct atmel_ssc_info
*ssc_p
= &ssc_info
[dai
->id
];
776 struct atmel_pcm_dma_params
*dma_params
;
779 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
784 dma_params
= ssc_p
->dma_params
[dir
];
786 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
787 ssc_writel(ssc_p
->ssc
->regs
, IDR
, dma_params
->mask
->ssc_error
);
789 pr_debug("%s enabled SSC_SR=0x%08x\n",
790 dir
? "receive" : "transmit",
791 ssc_readl(ssc_p
->ssc
->regs
, SR
));
795 static int atmel_ssc_trigger(struct snd_pcm_substream
*substream
,
796 int cmd
, struct snd_soc_dai
*dai
)
798 struct atmel_ssc_info
*ssc_p
= &ssc_info
[dai
->id
];
799 struct atmel_pcm_dma_params
*dma_params
;
802 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
807 dma_params
= ssc_p
->dma_params
[dir
];
810 case SNDRV_PCM_TRIGGER_START
:
811 case SNDRV_PCM_TRIGGER_RESUME
:
812 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
813 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_enable
);
816 ssc_writel(ssc_p
->ssc
->regs
, CR
, dma_params
->mask
->ssc_disable
);
824 static int atmel_ssc_suspend(struct snd_soc_dai
*cpu_dai
)
826 struct atmel_ssc_info
*ssc_p
;
828 if (!cpu_dai
->active
)
831 ssc_p
= &ssc_info
[cpu_dai
->id
];
833 /* Save the status register before disabling transmit and receive */
834 ssc_p
->ssc_state
.ssc_sr
= ssc_readl(ssc_p
->ssc
->regs
, SR
);
835 ssc_writel(ssc_p
->ssc
->regs
, CR
, SSC_BIT(CR_TXDIS
) | SSC_BIT(CR_RXDIS
));
837 /* Save the current interrupt mask, then disable unmasked interrupts */
838 ssc_p
->ssc_state
.ssc_imr
= ssc_readl(ssc_p
->ssc
->regs
, IMR
);
839 ssc_writel(ssc_p
->ssc
->regs
, IDR
, ssc_p
->ssc_state
.ssc_imr
);
841 ssc_p
->ssc_state
.ssc_cmr
= ssc_readl(ssc_p
->ssc
->regs
, CMR
);
842 ssc_p
->ssc_state
.ssc_rcmr
= ssc_readl(ssc_p
->ssc
->regs
, RCMR
);
843 ssc_p
->ssc_state
.ssc_rfmr
= ssc_readl(ssc_p
->ssc
->regs
, RFMR
);
844 ssc_p
->ssc_state
.ssc_tcmr
= ssc_readl(ssc_p
->ssc
->regs
, TCMR
);
845 ssc_p
->ssc_state
.ssc_tfmr
= ssc_readl(ssc_p
->ssc
->regs
, TFMR
);
852 static int atmel_ssc_resume(struct snd_soc_dai
*cpu_dai
)
854 struct atmel_ssc_info
*ssc_p
;
857 if (!cpu_dai
->active
)
860 ssc_p
= &ssc_info
[cpu_dai
->id
];
862 /* restore SSC register settings */
863 ssc_writel(ssc_p
->ssc
->regs
, TFMR
, ssc_p
->ssc_state
.ssc_tfmr
);
864 ssc_writel(ssc_p
->ssc
->regs
, TCMR
, ssc_p
->ssc_state
.ssc_tcmr
);
865 ssc_writel(ssc_p
->ssc
->regs
, RFMR
, ssc_p
->ssc_state
.ssc_rfmr
);
866 ssc_writel(ssc_p
->ssc
->regs
, RCMR
, ssc_p
->ssc_state
.ssc_rcmr
);
867 ssc_writel(ssc_p
->ssc
->regs
, CMR
, ssc_p
->ssc_state
.ssc_cmr
);
869 /* re-enable interrupts */
870 ssc_writel(ssc_p
->ssc
->regs
, IER
, ssc_p
->ssc_state
.ssc_imr
);
872 /* Re-enable receive and transmit as appropriate */
875 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_RXEN
)) ? SSC_BIT(CR_RXEN
) : 0;
877 (ssc_p
->ssc_state
.ssc_sr
& SSC_BIT(SR_TXEN
)) ? SSC_BIT(CR_TXEN
) : 0;
878 ssc_writel(ssc_p
->ssc
->regs
, CR
, cr
);
882 #else /* CONFIG_PM */
883 # define atmel_ssc_suspend NULL
884 # define atmel_ssc_resume NULL
885 #endif /* CONFIG_PM */
887 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
888 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
890 static const struct snd_soc_dai_ops atmel_ssc_dai_ops
= {
891 .startup
= atmel_ssc_startup
,
892 .shutdown
= atmel_ssc_shutdown
,
893 .prepare
= atmel_ssc_prepare
,
894 .trigger
= atmel_ssc_trigger
,
895 .hw_params
= atmel_ssc_hw_params
,
896 .set_fmt
= atmel_ssc_set_dai_fmt
,
897 .set_clkdiv
= atmel_ssc_set_dai_clkdiv
,
900 static struct snd_soc_dai_driver atmel_ssc_dai
= {
901 .suspend
= atmel_ssc_suspend
,
902 .resume
= atmel_ssc_resume
,
906 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
909 .formats
= ATMEL_SSC_FORMATS
,},
913 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
916 .formats
= ATMEL_SSC_FORMATS
,},
917 .ops
= &atmel_ssc_dai_ops
,
920 static const struct snd_soc_component_driver atmel_ssc_component
= {
924 static int asoc_ssc_init(struct device
*dev
)
926 struct platform_device
*pdev
= to_platform_device(dev
);
927 struct ssc_device
*ssc
= platform_get_drvdata(pdev
);
930 ret
= snd_soc_register_component(dev
, &atmel_ssc_component
,
933 dev_err(dev
, "Could not register DAI: %d\n", ret
);
937 if (ssc
->pdata
->use_dma
)
938 ret
= atmel_pcm_dma_platform_register(dev
);
940 ret
= atmel_pcm_pdc_platform_register(dev
);
943 dev_err(dev
, "Could not register PCM: %d\n", ret
);
944 goto err_unregister_dai
;
950 snd_soc_unregister_component(dev
);
955 static void asoc_ssc_exit(struct device
*dev
)
957 struct platform_device
*pdev
= to_platform_device(dev
);
958 struct ssc_device
*ssc
= platform_get_drvdata(pdev
);
960 if (ssc
->pdata
->use_dma
)
961 atmel_pcm_dma_platform_unregister(dev
);
963 atmel_pcm_pdc_platform_unregister(dev
);
965 snd_soc_unregister_component(dev
);
969 * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
971 int atmel_ssc_set_audio(int ssc_id
)
973 struct ssc_device
*ssc
;
976 /* If we can grab the SSC briefly to parent the DAI device off it */
977 ssc
= ssc_request(ssc_id
);
979 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
983 ssc_info
[ssc_id
].ssc
= ssc
;
986 ret
= asoc_ssc_init(&ssc
->pdev
->dev
);
990 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio
);
992 void atmel_ssc_put_audio(int ssc_id
)
994 struct ssc_device
*ssc
= ssc_info
[ssc_id
].ssc
;
996 asoc_ssc_exit(&ssc
->pdev
->dev
);
999 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio
);
1001 /* Module information */
1002 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
1003 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
1004 MODULE_LICENSE("GPL");