2 * at91-i2s.c -- ALSA SoC I2S Audio Layer Platform driver
4 * Author: Frank Mandarino <fmandarino@endrelia.com>
5 * Endrelia Technologies Inc.
7 * Based on pxa2xx Platform drivers by
8 * Liam Girdwood <liam.girdwood@wolfsonmicro.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 * 3rd Mar 2006 Initial version.
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/device.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/initval.h>
29 #include <sound/soc.h>
31 #include <asm/arch/hardware.h>
32 #include <asm/arch/at91_pmc.h>
33 #include <asm/arch/at91_ssc.h>
34 #include <asm/arch/at91_pdc.h>
39 #define DBG(x...) printk(KERN_DEBUG "at91-i2s:" x)
44 #if defined(CONFIG_ARCH_AT91SAM9260)
45 #define NUM_SSC_DEVICES 1
47 #define NUM_SSC_DEVICES 3
51 #define AT91_I2S_DAIFMT \
52 (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS | SND_SOC_DAIFMT_NB_NF)
54 #define AT91_I2S_DIR \
55 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
57 /* priv is (SSC_CMR.DIV << 16 | SSC_TCMR.PERIOD ) */
58 static struct snd_soc_dai_mode at91_i2s[] = {
60 /* 8k: BCLK = (MCLK/10) = (60MHz/50) = 1.2MHz */
62 .fmt = AT91_I2S_DAIFMT,
63 .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE,
64 .pcmrate = SNDRV_PCM_RATE_8000,
65 .pcmdir = AT91_I2S_DIR,
66 .flags = SND_SOC_DAI_BFS_DIV,
68 .bfs = SND_SOC_FSBD(10),
69 .priv = (25 << 16 | 74),
72 /* 16k: BCLK = (MCLK/3) ~= (60MHz/14) = 4.285714MHz */
74 .fmt = AT91_I2S_DAIFMT,
75 .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE,
76 .pcmrate = SNDRV_PCM_RATE_16000,
77 .pcmdir = AT91_I2S_DIR,
78 .flags = SND_SOC_DAI_BFS_DIV,
80 .bfs = SND_SOC_FSBD(3),
81 .priv = (7 << 16 | 133),
84 /* 32k: BCLK = (MCLK/3) ~= (60MHz/14) = 4.285714MHz */
86 .fmt = AT91_I2S_DAIFMT,
87 .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE,
88 .pcmrate = SNDRV_PCM_RATE_32000,
89 .pcmdir = AT91_I2S_DIR,
90 .flags = SND_SOC_DAI_BFS_DIV,
92 .bfs = SND_SOC_FSBD(3),
93 .priv = (7 << 16 | 66),
96 /* 48k: BCLK = (MCLK/5) ~= (60MHz/26) = 2.3076923MHz */
98 .fmt = AT91_I2S_DAIFMT,
99 .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE,
100 .pcmrate = SNDRV_PCM_RATE_48000,
101 .pcmdir = AT91_I2S_DIR,
102 .flags = SND_SOC_DAI_BFS_DIV,
104 .bfs SND_SOC_FSBD(5),
105 .priv = (13 << 16 | 23),
111 * SSC PDC registers required by the PCM DMA engine.
113 static struct at91_pdc_regs pdc_tx_reg = {
116 .xnpr = AT91_PDC_TNPR,
117 .xncr = AT91_PDC_TNCR,
120 static struct at91_pdc_regs pdc_rx_reg = {
123 .xnpr = AT91_PDC_RNPR,
124 .xncr = AT91_PDC_RNCR,
128 * SSC & PDC status bits for transmit and receive.
130 static struct at91_ssc_mask ssc_tx_mask = {
131 .ssc_enable = AT91_SSC_TXEN,
132 .ssc_disable = AT91_SSC_TXDIS,
133 .ssc_endx = AT91_SSC_ENDTX,
134 .ssc_endbuf = AT91_SSC_TXBUFE,
135 .pdc_enable = AT91_PDC_TXTEN,
136 .pdc_disable = AT91_PDC_TXTDIS,
139 static struct at91_ssc_mask ssc_rx_mask = {
140 .ssc_enable = AT91_SSC_RXEN,
141 .ssc_disable = AT91_SSC_RXDIS,
142 .ssc_endx = AT91_SSC_ENDRX,
143 .ssc_endbuf = AT91_SSC_RXBUFF,
144 .pdc_enable = AT91_PDC_RXTEN,
145 .pdc_disable = AT91_PDC_RXTDIS,
152 static struct at91_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
154 .name = "SSC0/I2S PCM Stereo out",
156 .mask = &ssc_tx_mask,
159 .name = "SSC0/I2S PCM Stereo in",
161 .mask = &ssc_rx_mask,
163 #if NUM_SSC_DEVICES == 3
165 .name = "SSC1/I2S PCM Stereo out",
167 .mask = &ssc_tx_mask,
170 .name = "SSC1/I2S PCM Stereo in",
172 .mask = &ssc_rx_mask,
175 .name = "SSC2/I2S PCM Stereo out",
177 .mask = &ssc_tx_mask,
180 .name = "SSC1/I2S PCM Stereo in",
182 .mask = &ssc_rx_mask,
189 * A MUTEX is used to protect an SSC initialzed flag which allows
190 * the substream hw_params() call to initialize the SSC only if
191 * there are no other substreams open. If there are other
192 * substreams open, the hw_param() call can only check that
193 * it is using the same format and rate.
195 static DECLARE_MUTEX(ssc0_mutex);
196 #if NUM_SSC_DEVICES == 3
197 static DECLARE_MUTEX(ssc1_mutex);
198 static DECLARE_MUTEX(ssc2_mutex);
202 struct at91_ssc_state {
213 static struct at91_ssc_info {
215 struct at91_ssc_periph ssc;
216 spinlock_t lock; /* lock for dir_mask */
217 int dir_mask; /* 0=unused, 1=playback, 2=capture */
218 struct semaphore *mutex;
222 struct at91_pcm_dma_params *dma_params[2];
223 struct at91_ssc_state ssc_state;
225 } ssc_info[NUM_SSC_DEVICES] = {
228 .lock = SPIN_LOCK_UNLOCKED,
230 .mutex = &ssc0_mutex,
233 #if NUM_SSC_DEVICES == 3
236 .lock = SPIN_LOCK_UNLOCKED,
238 .mutex = &ssc1_mutex,
243 .lock = SPIN_LOCK_UNLOCKED,
245 .mutex = &ssc2_mutex,
252 static irqreturn_t at91_i2s_interrupt(int irq, void *dev_id)
254 struct at91_ssc_info *ssc_p = dev_id;
255 struct at91_pcm_dma_params *dma_params;
259 ssc_sr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR)
260 & at91_ssc_read(ssc_p->ssc.base + AT91_SSC_IMR);
263 * Loop through the substreams attached to this SSC. If
264 * a DMA-related interrupt occurred on that substream, call
265 * the DMA interrupt handler function, if one has been
266 * registered in the dma_params structure by the PCM driver.
268 for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
269 dma_params = ssc_p->dma_params[i];
271 if (dma_params != NULL && dma_params->dma_intr_handler != NULL &&
273 (dma_params->mask->ssc_endx | dma_params->mask->ssc_endbuf)))
275 dma_params->dma_intr_handler(ssc_sr, dma_params->substream);
281 static int at91_i2s_startup(struct snd_pcm_substream *substream)
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
284 struct at91_ssc_info *ssc_p = &ssc_info[rtd->cpu_dai->id];
287 DBG("i2s_startup: SSC_SR=0x%08lx\n",
288 at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR));
289 dir_mask = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0x1 : 0x2;
291 spin_lock_irq(&ssc_p->lock);
292 if (ssc_p->dir_mask & dir_mask) {
293 spin_unlock_irq(&ssc_p->lock);
296 ssc_p->dir_mask |= dir_mask;
297 spin_unlock_irq(&ssc_p->lock);
302 static void at91_i2s_shutdown(struct snd_pcm_substream *substream)
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct at91_ssc_info *ssc_p = &ssc_info[rtd->cpu_dai->id];
306 struct at91_pcm_dma_params *dma_params = rtd->cpu_dai->dma_data;
309 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
311 if (dma_params != NULL) {
312 at91_ssc_write(dma_params->ssc_base + AT91_SSC_CR,
313 dma_params->mask->ssc_disable);
314 DBG("%s disabled SSC_SR=0x%08lx\n", (dir ? "receive" : "transmit"),
315 at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR));
317 dma_params->ssc_base = NULL;
318 dma_params->substream = NULL;
319 ssc_p->dma_params[dir] = NULL;
324 spin_lock_irq(&ssc_p->lock);
325 ssc_p->dir_mask &= ~dir_mask;
326 if (!ssc_p->dir_mask) {
327 /* Shutdown the SSC clock. */
328 DBG("Stopping pid %d clock\n", ssc_p->ssc.pid);
329 at91_sys_write(AT91_PMC_PCDR, 1<<ssc_p->ssc.pid);
331 if (ssc_p->initialized)
332 free_irq(ssc_p->ssc.pid, ssc_p);
335 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR, AT91_SSC_SWRST);
337 /* Force a re-init on the next hw_params() call. */
338 ssc_p->initialized = 0;
340 spin_unlock_irq(&ssc_p->lock);
344 static int at91_i2s_suspend(struct platform_device *pdev,
345 struct snd_soc_cpu_dai *dai)
347 struct at91_ssc_info *ssc_p;
352 ssc_p = &ssc_info[dai->id];
354 /* Save the status register before disabling transmit and receive. */
355 ssc_p->state->ssc_sr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR);
356 at91_ssc_write(ssc_p->ssc.base +
357 AT91_SSC_CR, AT91_SSC_TXDIS | AT91_SSC_RXDIS);
359 /* Save the current interrupt mask, then disable unmasked interrupts. */
360 ssc_p->state->ssc_imr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_IMR);
361 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_IDR, ssc_p->state->ssc_imr);
363 ssc_p->state->ssc_cmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_CMR);
364 ssc_p->state->ssc_rcmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RCMR);
365 ssc_p->state->ssc_rfmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RCMR);
366 ssc_p->state->ssc_tcmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RCMR);
367 ssc_p->state->ssc_tfmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RCMR);
372 static int at91_i2s_resume(struct platform_device *pdev,
373 struct snd_soc_cpu_dai *dai)
375 struct at91_ssc_info *ssc_p;
381 ssc_p = &ssc_info[dai->id];
383 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, ssc_p->state->ssc_tfmr);
384 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, ssc_p->state->ssc_tcmr);
385 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, ssc_p->state->ssc_rfmr);
386 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, ssc_p->state->ssc_rcmr);
387 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CMR, ssc_p->state->ssc_cmr);
389 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_IER, ssc_p->state->ssc_imr);
391 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR,
392 ((ssc_p->state->ssc_sr & AT91_SSC_RXENA) ? AT91_SSC_RXEN : 0) |
393 ((ssc_p->state->ssc_sr & AT91_SSC_TXENA) ? AT91_SSC_TXEN : 0));
399 #define at91_i2s_suspend NULL
400 #define at91_i2s_resume NULL
403 static unsigned int at91_i2s_config_sysclk(
404 struct snd_soc_cpu_dai *iface, struct snd_soc_clock_info *info,
407 /* Currently, there is only support for USB (12Mhz) mode */
413 static int at91_i2s_hw_params(struct snd_pcm_substream *substream,
414 struct snd_pcm_hw_params *params)
416 struct snd_soc_pcm_runtime *rtd = substream->private_data;
417 int id = rtd->cpu_dai->id;
418 struct at91_ssc_info *ssc_p = &ssc_info[id];
419 struct at91_pcm_dma_params *dma_params;
420 unsigned int pcmfmt, rate;
421 int dir, channels, bits;
423 u32 div, period, tfmr, rfmr, tcmr, rcmr;
427 * Currently, there is only one set of dma params for
428 * each direction. If more are added, this code will
429 * have to be changed to select the proper set.
431 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
433 dma_params = &ssc_dma_params[id][dir];
434 dma_params->ssc_base = ssc_p->ssc.base;
435 dma_params->substream = substream;
437 ssc_p->dma_params[dir] = dma_params;
438 rtd->cpu_dai->dma_data = dma_params;
440 rate = params_rate(params);
441 channels = params_channels(params);
443 pcmfmt = rtd->cpu_dai->dai_runtime.pcmfmt;
445 case SNDRV_PCM_FMTBIT_S16_LE:
446 /* likely this is all we'll ever support, but ... */
448 dma_params->pdc_xfer_size = 2;
451 printk(KERN_WARNING "at91-i2s: unsupported format %x\n",
456 /* Don't allow both SSC substreams to initialize at the same time. */
460 * If this SSC is alreadly initialized, then this substream must use
461 * the same format and rate.
463 if (ssc_p->initialized) {
464 if (pcmfmt != ssc_p->pcmfmt || rate != ssc_p->rate) {
465 printk(KERN_WARNING "at91-i2s: "
466 "incompatible substream in other direction\n");
471 /* Enable PMC peripheral clock for this SSC */
472 DBG("Starting pid %d clock\n", ssc_p->ssc.pid);
473 at91_sys_write(AT91_PMC_PCER, 1<<ssc_p->ssc.pid);
476 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR, AT91_SSC_SWRST);
478 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_RPR, 0);
479 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_RCR, 0);
480 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_RNPR, 0);
481 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_RNCR, 0);
482 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_TPR, 0);
483 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_TCR, 0);
484 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_TNPR, 0);
485 at91_ssc_write(ssc_p->ssc.base + AT91_PDC_TNCR, 0);
487 div = rtd->cpu_dai->dai_runtime.priv >> 16;
488 period = rtd->cpu_dai->dai_runtime.priv & 0xffff;
490 mck_clk = clk_get(NULL, "mck");
492 DBG("mck %lu fsbd %u bfs %llu bfs_real %u bclk %lu div %u period %u\n",
493 clk_get_rate(mck_clk),
495 rtd->cpu_dai->dai_runtime.bfs,
496 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs),
497 clk_get_rate(mck_clk) / (2 * div),
503 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CMR, div);
506 * Setup the TFMR and RFMR for the proper data format.
509 (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
510 | (( 0 << 23) & AT91_SSC_FSDEN)
511 | (( AT91_SSC_FSOS_NEGATIVE ) & AT91_SSC_FSOS)
512 | (((bits - 1) << 16) & AT91_SSC_FSLEN)
513 | (((channels - 1) << 8) & AT91_SSC_DATNB)
514 | (( 1 << 7) & AT91_SSC_MSBF)
515 | (( 0 << 5) & AT91_SSC_DATDEF)
516 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
517 DBG("SSC_TFMR=0x%08x\n", tfmr);
518 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TFMR, tfmr);
521 (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
522 | (( AT91_SSC_FSOS_NONE ) & AT91_SSC_FSOS)
523 | (( 0 << 16) & AT91_SSC_FSLEN)
524 | (((channels - 1) << 8) & AT91_SSC_DATNB)
525 | (( 1 << 7) & AT91_SSC_MSBF)
526 | (( 0 << 5) & AT91_SSC_LOOP)
527 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
529 DBG("SSC_RFMR=0x%08x\n", rfmr);
530 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RFMR, rfmr);
533 * Setup the TCMR and RCMR to generate the proper BCLK
537 (( period << 24) & AT91_SSC_PERIOD)
538 | (( 1 << 16) & AT91_SSC_STTDLY)
539 | (( AT91_SSC_START_FALLING_RF ) & AT91_SSC_START)
540 | (( AT91_SSC_CKI_FALLING ) & AT91_SSC_CKI)
541 | (( AT91_SSC_CKO_CONTINUOUS ) & AT91_SSC_CKO)
542 | (( AT91_SSC_CKS_DIV ) & AT91_SSC_CKS);
544 DBG("SSC_TCMR=0x%08x\n", tcmr);
545 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TCMR, tcmr);
548 (( 0 << 24) & AT91_SSC_PERIOD)
549 | (( 1 << 16) & AT91_SSC_STTDLY)
550 | (( AT91_SSC_START_TX_RX ) & AT91_SSC_START)
551 | (( AT91_SSC_CK_RISING ) & AT91_SSC_CKI)
552 | (( AT91_SSC_CKO_NONE ) & AT91_SSC_CKO)
553 | (( AT91_SSC_CKS_CLOCK ) & AT91_SSC_CKS);
555 DBG("SSC_RCMR=0x%08x\n", rcmr);
556 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, rcmr);
558 if ((ret = request_irq(ssc_p->ssc.pid, at91_i2s_interrupt,
559 0, ssc_p->name, ssc_p)) < 0) {
560 printk(KERN_WARNING "at91-i2s: request_irq failure\n");
565 * Save the current substream parameters in order to check
566 * that the substream in the opposite direction uses the
569 ssc_p->pcmfmt = pcmfmt;
571 ssc_p->initialized = 1;
573 DBG("hw_params: SSC initialized\n");
582 static int at91_i2s_prepare(struct snd_pcm_substream *substream)
584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
585 struct at91_pcm_dma_params *dma_params = rtd->cpu_dai->dma_data;
587 at91_ssc_write(dma_params->ssc_base + AT91_SSC_CR,
588 dma_params->mask->ssc_enable);
590 DBG("%s enabled SSC_SR=0x%08lx\n",
591 substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? "transmit" : "receive",
592 at91_ssc_read(ssc_info[rtd->cpu_dai->id].ssc.base + AT91_SSC_SR));
597 struct snd_soc_cpu_dai at91_i2s_dai[NUM_SSC_DEVICES] = {
598 { .name = "at91_ssc0/i2s",
600 .type = SND_SOC_DAI_I2S,
601 .suspend = at91_i2s_suspend,
602 .resume = at91_i2s_resume,
603 .config_sysclk = at91_i2s_config_sysclk,
611 .startup = at91_i2s_startup,
612 .shutdown = at91_i2s_shutdown,
613 .prepare = at91_i2s_prepare,
614 .hw_params = at91_i2s_hw_params,},
616 .mode = &at91_i2s[0],
617 .num_modes = ARRAY_SIZE(at91_i2s),},
618 .private_data = &ssc_info[0].ssc,
620 #if NUM_SSC_DEVICES == 3
621 { .name = "at91_ssc1/i2s",
623 .type = SND_SOC_DAI_I2S,
624 .suspend = at91_i2s_suspend,
625 .resume = at91_i2s_resume,
626 .config_sysclk = at91_i2s_config_sysclk,
634 .startup = at91_i2s_startup,
635 .shutdown = at91_i2s_shutdown,
636 .prepare = at91_i2s_prepare,
637 .hw_params = at91_i2s_hw_params,},
639 .mode = &at91_i2s[0],
640 .num_modes = ARRAY_SIZE(at91_i2s),},
641 .private_data = &ssc_info[1].ssc,
643 { .name = "at91_ssc2/i2s",
645 .type = SND_SOC_DAI_I2S,
646 .suspend = at91_i2s_suspend,
647 .resume = at91_i2s_resume,
648 .config_sysclk = at91_i2s_config_sysclk,
656 .startup = at91_i2s_startup,
657 .shutdown = at91_i2s_shutdown,
658 .prepare = at91_i2s_prepare,
659 .hw_params = at91_i2s_hw_params,},
661 .mode = &at91_i2s[0],
662 .num_modes = ARRAY_SIZE(at91_i2s),},
663 .private_data = &ssc_info[2].ssc,
668 EXPORT_SYMBOL_GPL(at91_i2s_dai);
670 /* Module information */
671 MODULE_AUTHOR("Frank Mandarino, fmandarino@endrelia.com, www.endrelia.com");
672 MODULE_DESCRIPTION("AT91 I2S ASoC Interface");
673 MODULE_LICENSE("GPL");