2 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/initval.h>
22 #include <sound/soc.h>
24 #include <asm/immap_86xx.h>
29 * FSLSSI_I2S_RATES: sample rates supported by the I2S
31 * This driver currently only supports the SSI running in I2S slave mode,
32 * which means the codec determines the sample rate. Therefore, we tell
33 * ALSA that we support all rates and let the codec driver decide what rates
34 * are really supported.
36 #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
37 SNDRV_PCM_RATE_CONTINUOUS)
40 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
42 * This driver currently only supports the SSI running in I2S slave mode.
44 * The SSI has a limitation in that the samples must be in the same byte
45 * order as the host CPU. This is because when multiple bytes are written
46 * to the STX register, the bytes and bits must be written in the same
47 * order. The STX is a shift register, so all the bits need to be aligned
48 * (bit-endianness must match byte-endianness). Processors typically write
49 * the bits within a byte in the same order that the bytes of a word are
50 * written in. So if the host CPU is big-endian, then only big-endian
51 * samples will be written to STX properly.
54 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
55 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
56 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
58 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
59 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
60 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
63 /* SIER bitflag of interrupts to enable */
64 #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
65 CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
66 CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
67 CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
68 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
71 * fsl_ssi_private: per-SSI private data
73 * @name: short name for this device ("SSI0", "SSI1", etc)
74 * @ssi: pointer to the SSI's registers
75 * @ssi_phys: physical address of the SSI registers
76 * @irq: IRQ of this SSI
77 * @first_stream: pointer to the stream that was opened first
78 * @second_stream: pointer to second stream
79 * @dev: struct device pointer
80 * @playback: the number of playback streams opened
81 * @capture: the number of capture streams opened
82 * @asynchronous: 0=synchronous mode, 1=asynchronous mode
83 * @cpu_dai: the CPU DAI for this device
84 * @dev_attr: the sysfs device attribute structure
85 * @stats: SSI statistics
87 struct fsl_ssi_private {
89 struct ccsr_ssi __iomem *ssi;
92 struct snd_pcm_substream *first_stream;
93 struct snd_pcm_substream *second_stream;
95 unsigned int playback;
98 struct snd_soc_dai cpu_dai;
99 struct device_attribute dev_attr;
127 * fsl_ssi_isr: SSI interrupt handler
129 * Although it's possible to use the interrupt handler to send and receive
130 * data to/from the SSI, we use the DMA instead. Programming is more
131 * complicated, but the performance is much better.
133 * This interrupt handler is used only to gather statistics.
135 * @irq: IRQ of the SSI device
136 * @dev_id: pointer to the ssi_private structure for this SSI device
138 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
140 struct fsl_ssi_private *ssi_private = dev_id;
141 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
142 irqreturn_t ret = IRQ_NONE;
146 /* We got an interrupt, so read the status register to see what we
147 were interrupted for. We mask it with the Interrupt Enable register
148 so that we only check for events that we're interested in.
150 sisr = in_be32(&ssi->sisr) & SIER_FLAGS;
152 if (sisr & CCSR_SSI_SISR_RFRC) {
153 ssi_private->stats.rfrc++;
154 sisr2 |= CCSR_SSI_SISR_RFRC;
158 if (sisr & CCSR_SSI_SISR_TFRC) {
159 ssi_private->stats.tfrc++;
160 sisr2 |= CCSR_SSI_SISR_TFRC;
164 if (sisr & CCSR_SSI_SISR_CMDAU) {
165 ssi_private->stats.cmdau++;
169 if (sisr & CCSR_SSI_SISR_CMDDU) {
170 ssi_private->stats.cmddu++;
174 if (sisr & CCSR_SSI_SISR_RXT) {
175 ssi_private->stats.rxt++;
179 if (sisr & CCSR_SSI_SISR_RDR1) {
180 ssi_private->stats.rdr1++;
184 if (sisr & CCSR_SSI_SISR_RDR0) {
185 ssi_private->stats.rdr0++;
189 if (sisr & CCSR_SSI_SISR_TDE1) {
190 ssi_private->stats.tde1++;
194 if (sisr & CCSR_SSI_SISR_TDE0) {
195 ssi_private->stats.tde0++;
199 if (sisr & CCSR_SSI_SISR_ROE1) {
200 ssi_private->stats.roe1++;
201 sisr2 |= CCSR_SSI_SISR_ROE1;
205 if (sisr & CCSR_SSI_SISR_ROE0) {
206 ssi_private->stats.roe0++;
207 sisr2 |= CCSR_SSI_SISR_ROE0;
211 if (sisr & CCSR_SSI_SISR_TUE1) {
212 ssi_private->stats.tue1++;
213 sisr2 |= CCSR_SSI_SISR_TUE1;
217 if (sisr & CCSR_SSI_SISR_TUE0) {
218 ssi_private->stats.tue0++;
219 sisr2 |= CCSR_SSI_SISR_TUE0;
223 if (sisr & CCSR_SSI_SISR_TFS) {
224 ssi_private->stats.tfs++;
228 if (sisr & CCSR_SSI_SISR_RFS) {
229 ssi_private->stats.rfs++;
233 if (sisr & CCSR_SSI_SISR_TLS) {
234 ssi_private->stats.tls++;
238 if (sisr & CCSR_SSI_SISR_RLS) {
239 ssi_private->stats.rls++;
243 if (sisr & CCSR_SSI_SISR_RFF1) {
244 ssi_private->stats.rff1++;
248 if (sisr & CCSR_SSI_SISR_RFF0) {
249 ssi_private->stats.rff0++;
253 if (sisr & CCSR_SSI_SISR_TFE1) {
254 ssi_private->stats.tfe1++;
258 if (sisr & CCSR_SSI_SISR_TFE0) {
259 ssi_private->stats.tfe0++;
263 /* Clear the bits that we set */
265 out_be32(&ssi->sisr, sisr2);
271 * fsl_ssi_startup: create a new substream
273 * This is the first function called when a stream is opened.
275 * If this is the first stream open, then grab the IRQ and program most of
278 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
279 struct snd_soc_dai *dai)
281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
282 struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
285 * If this is the first stream opened, then request the IRQ
286 * and initialize the SSI registers.
288 if (!ssi_private->playback && !ssi_private->capture) {
289 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
292 ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0,
293 ssi_private->name, ssi_private);
295 dev_err(substream->pcm->card->dev,
296 "could not claim irq %u\n", ssi_private->irq);
301 * Section 16.5 of the MPC8610 reference manual says that the
302 * SSI needs to be disabled before updating the registers we set
305 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
308 * Program the SSI into I2S Slave Non-Network Synchronous mode.
309 * Also enable the transmit and receive FIFO.
311 * FIXME: Little-endian samples require a different shift dir
313 clrsetbits_be32(&ssi->scr,
314 CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
315 CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE
316 | (ssi_private->asynchronous ? 0 : CCSR_SSI_SCR_SYN));
319 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
320 CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
321 CCSR_SSI_STCR_TSCKP);
324 CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
325 CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
326 CCSR_SSI_SRCR_RSCKP);
329 * The DC and PM bits are only used if the SSI is the clock
333 /* 4. Enable the interrupts and DMA requests */
334 out_be32(&ssi->sier, SIER_FLAGS);
337 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We
338 * don't use FIFO 1. Since the SSI only supports stereo, the
339 * watermark should never be an odd number.
341 out_be32(&ssi->sfcsr,
342 CCSR_SSI_SFCSR_TFWM0(6) | CCSR_SSI_SFCSR_RFWM0(2));
345 * We keep the SSI disabled because if we enable it, then the
346 * DMA controller will start. It's not supposed to start until
347 * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The
348 * DMA controller will transfer one "BWC" of data (i.e. the
349 * amount of data that the MR.BWC bits are set to). The reason
350 * this is bad is because at this point, the PCM driver has not
351 * finished initializing the DMA controller.
355 if (!ssi_private->first_stream)
356 ssi_private->first_stream = substream;
358 /* This is the second stream open, so we need to impose sample
359 * rate and maybe sample size constraints. Note that this can
360 * cause a race condition if the second stream is opened before
361 * the first stream is fully initialized.
363 * We provide some protection by checking to make sure the first
364 * stream is initialized, but it's not perfect. ALSA sometimes
365 * re-initializes the driver with a different sample rate or
366 * size. If the second stream is opened before the first stream
367 * has received its final parameters, then the second stream may
368 * be constrained to the wrong sample rate or size.
370 * FIXME: This code does not handle opening and closing streams
371 * repeatedly. If you open two streams and then close the first
372 * one, you may not be able to open another stream until you
373 * close the second one as well.
375 struct snd_pcm_runtime *first_runtime =
376 ssi_private->first_stream->runtime;
378 if (!first_runtime->rate || !first_runtime->sample_bits) {
379 dev_err(substream->pcm->card->dev,
380 "set sample rate and size in %s stream first\n",
381 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
382 ? "capture" : "playback");
386 snd_pcm_hw_constraint_minmax(substream->runtime,
387 SNDRV_PCM_HW_PARAM_RATE,
388 first_runtime->rate, first_runtime->rate);
390 /* If we're in synchronous mode, then we need to constrain
391 * the sample size as well. We don't support independent sample
392 * rates in asynchronous mode.
394 if (!ssi_private->asynchronous)
395 snd_pcm_hw_constraint_minmax(substream->runtime,
396 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
397 first_runtime->sample_bits,
398 first_runtime->sample_bits);
400 ssi_private->second_stream = substream;
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 ssi_private->playback++;
406 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
407 ssi_private->capture++;
413 * fsl_ssi_hw_params - program the sample size
415 * Most of the SSI registers have been programmed in the startup function,
416 * but the word length must be programmed here. Unfortunately, programming
417 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
418 * cause a problem with supporting simultaneous playback and capture. If
419 * the SSI is already playing a stream, then that stream may be temporarily
420 * stopped when you start capture.
422 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
425 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
426 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
428 struct fsl_ssi_private *ssi_private = cpu_dai->private_data;
430 if (substream == ssi_private->first_stream) {
431 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
432 unsigned int sample_size =
433 snd_pcm_format_width(params_format(hw_params));
434 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
436 /* The SSI should always be disabled at this points (SSIEN=0) */
438 /* In synchronous mode, the SSI uses STCCR for capture */
439 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
440 !ssi_private->asynchronous)
441 clrsetbits_be32(&ssi->stccr,
442 CCSR_SSI_SxCCR_WL_MASK, wl);
444 clrsetbits_be32(&ssi->srccr,
445 CCSR_SSI_SxCCR_WL_MASK, wl);
452 * fsl_ssi_trigger: start and stop the DMA transfer.
454 * This function is called by ALSA to start, stop, pause, and resume the DMA
457 * The DMA channel is in external master start and pause mode, which
458 * means the SSI completely controls the flow of data.
460 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
461 struct snd_soc_dai *dai)
463 struct snd_soc_pcm_runtime *rtd = substream->private_data;
464 struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
465 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
468 case SNDRV_PCM_TRIGGER_START:
469 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
470 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
471 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
473 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
476 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
479 case SNDRV_PCM_TRIGGER_STOP:
480 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
481 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
482 clrbits32(&ssi->scr, CCSR_SSI_SCR_TE);
484 clrbits32(&ssi->scr, CCSR_SSI_SCR_RE);
495 * fsl_ssi_shutdown: shutdown the SSI
497 * Shutdown the SSI if there are no other substreams open.
499 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
500 struct snd_soc_dai *dai)
502 struct snd_soc_pcm_runtime *rtd = substream->private_data;
503 struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data;
505 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
506 ssi_private->playback--;
508 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
509 ssi_private->capture--;
511 if (ssi_private->first_stream == substream)
512 ssi_private->first_stream = ssi_private->second_stream;
514 ssi_private->second_stream = NULL;
517 * If this is the last active substream, disable the SSI and release
520 if (!ssi_private->playback && !ssi_private->capture) {
521 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
523 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
525 free_irq(ssi_private->irq, ssi_private);
530 * fsl_ssi_set_sysclk: set the clock frequency and direction
532 * This function is called by the machine driver to tell us what the clock
533 * frequency and direction are.
535 * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
536 * and we don't care about the frequency. Return an error if the direction
537 * is not SND_SOC_CLOCK_IN.
539 * @clk_id: reserved, should be zero
540 * @freq: the frequency of the given clock ID, currently ignored
541 * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
543 static int fsl_ssi_set_sysclk(struct snd_soc_dai *cpu_dai,
544 int clk_id, unsigned int freq, int dir)
547 return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL;
551 * fsl_ssi_set_fmt: set the serial format.
553 * This function is called by the machine driver to tell us what serial
556 * Currently, we only support I2S mode. Return an error if the format is
557 * not SND_SOC_DAIFMT_I2S.
559 * @format: one of SND_SOC_DAIFMT_xxx
561 static int fsl_ssi_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format)
563 return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL;
567 * fsl_ssi_dai_template: template CPU DAI for the SSI
569 static struct snd_soc_dai_ops fsl_ssi_dai_ops = {
570 .startup = fsl_ssi_startup,
571 .hw_params = fsl_ssi_hw_params,
572 .shutdown = fsl_ssi_shutdown,
573 .trigger = fsl_ssi_trigger,
574 .set_sysclk = fsl_ssi_set_sysclk,
575 .set_fmt = fsl_ssi_set_fmt,
578 static struct snd_soc_dai fsl_ssi_dai_template = {
580 /* The SSI does not support monaural audio. */
583 .rates = FSLSSI_I2S_RATES,
584 .formats = FSLSSI_I2S_FORMATS,
589 .rates = FSLSSI_I2S_RATES,
590 .formats = FSLSSI_I2S_FORMATS,
592 .ops = &fsl_ssi_dai_ops,
595 /* Show the statistics of a flag only if its interrupt is enabled. The
596 * compiler will optimze this code to a no-op if the interrupt is not
599 #define SIER_SHOW(flag, name) \
601 if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \
602 length += sprintf(buf + length, #name "=%u\n", \
603 ssi_private->stats.name); \
608 * fsl_sysfs_ssi_show: display SSI statistics
610 * Display the statistics for the current SSI device. To avoid confusion,
611 * we only show those counts that are enabled.
613 static ssize_t fsl_sysfs_ssi_show(struct device *dev,
614 struct device_attribute *attr, char *buf)
616 struct fsl_ssi_private *ssi_private =
617 container_of(attr, struct fsl_ssi_private, dev_attr);
620 SIER_SHOW(RFRC_EN, rfrc);
621 SIER_SHOW(TFRC_EN, tfrc);
622 SIER_SHOW(CMDAU_EN, cmdau);
623 SIER_SHOW(CMDDU_EN, cmddu);
624 SIER_SHOW(RXT_EN, rxt);
625 SIER_SHOW(RDR1_EN, rdr1);
626 SIER_SHOW(RDR0_EN, rdr0);
627 SIER_SHOW(TDE1_EN, tde1);
628 SIER_SHOW(TDE0_EN, tde0);
629 SIER_SHOW(ROE1_EN, roe1);
630 SIER_SHOW(ROE0_EN, roe0);
631 SIER_SHOW(TUE1_EN, tue1);
632 SIER_SHOW(TUE0_EN, tue0);
633 SIER_SHOW(TFS_EN, tfs);
634 SIER_SHOW(RFS_EN, rfs);
635 SIER_SHOW(TLS_EN, tls);
636 SIER_SHOW(RLS_EN, rls);
637 SIER_SHOW(RFF1_EN, rff1);
638 SIER_SHOW(RFF0_EN, rff0);
639 SIER_SHOW(TFE1_EN, tfe1);
640 SIER_SHOW(TFE0_EN, tfe0);
646 * fsl_ssi_create_dai: create a snd_soc_dai structure
648 * This function is called by the machine driver to create a snd_soc_dai
649 * structure. The function creates an ssi_private object, which contains
650 * the snd_soc_dai. It also creates the sysfs statistics device.
652 struct snd_soc_dai *fsl_ssi_create_dai(struct fsl_ssi_info *ssi_info)
654 struct snd_soc_dai *fsl_ssi_dai;
655 struct fsl_ssi_private *ssi_private;
657 struct device_attribute *dev_attr;
659 ssi_private = kzalloc(sizeof(struct fsl_ssi_private), GFP_KERNEL);
661 dev_err(ssi_info->dev, "could not allocate DAI object\n");
664 memcpy(&ssi_private->cpu_dai, &fsl_ssi_dai_template,
665 sizeof(struct snd_soc_dai));
667 fsl_ssi_dai = &ssi_private->cpu_dai;
668 dev_attr = &ssi_private->dev_attr;
670 sprintf(ssi_private->name, "ssi%u", (u8) ssi_info->id);
671 ssi_private->ssi = ssi_info->ssi;
672 ssi_private->ssi_phys = ssi_info->ssi_phys;
673 ssi_private->irq = ssi_info->irq;
674 ssi_private->dev = ssi_info->dev;
675 ssi_private->asynchronous = ssi_info->asynchronous;
677 ssi_private->dev->driver_data = fsl_ssi_dai;
679 /* Initialize the the device_attribute structure */
680 dev_attr->attr.name = "ssi-stats";
681 dev_attr->attr.mode = S_IRUGO;
682 dev_attr->show = fsl_sysfs_ssi_show;
684 ret = device_create_file(ssi_private->dev, dev_attr);
686 dev_err(ssi_info->dev, "could not create sysfs %s file\n",
687 ssi_private->dev_attr.attr.name);
692 fsl_ssi_dai->private_data = ssi_private;
693 fsl_ssi_dai->name = ssi_private->name;
694 fsl_ssi_dai->id = ssi_info->id;
695 fsl_ssi_dai->dev = ssi_info->dev;
697 ret = snd_soc_register_dai(fsl_ssi_dai);
699 dev_err(ssi_info->dev, "failed to register DAI: %d\n", ret);
706 EXPORT_SYMBOL_GPL(fsl_ssi_create_dai);
709 * fsl_ssi_destroy_dai: destroy the snd_soc_dai object
711 * This function undoes the operations of fsl_ssi_create_dai()
713 void fsl_ssi_destroy_dai(struct snd_soc_dai *fsl_ssi_dai)
715 struct fsl_ssi_private *ssi_private =
716 container_of(fsl_ssi_dai, struct fsl_ssi_private, cpu_dai);
718 device_remove_file(ssi_private->dev, &ssi_private->dev_attr);
720 snd_soc_unregister_dai(&ssi_private->cpu_dai);
724 EXPORT_SYMBOL_GPL(fsl_ssi_destroy_dai);
726 static int __init fsl_ssi_init(void)
728 printk(KERN_INFO "Freescale Synchronous Serial Interface (SSI) ASoC Driver\n");
732 module_init(fsl_ssi_init);
734 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
735 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
736 MODULE_LICENSE("GPL");