2 * pxa-ssp.c -- ALSA Soc Audio Layer
4 * Copyright 2005,2008 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * o Test network mode for > 16bit sample size
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/initval.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/pxa2xx-lib.h>
32 #include <mach/hardware.h>
34 #include <mach/regs-ssp.h>
35 #include <mach/audio.h>
38 #include "pxa2xx-pcm.h"
42 * SSP audio private data
49 struct ssp_state state;
53 static void dump_registers(struct ssp_device *ssp)
55 dev_dbg(&ssp->pdev->dev, "SSCR0 0x%08x SSCR1 0x%08x SSTO 0x%08x\n",
56 ssp_read_reg(ssp, SSCR0), ssp_read_reg(ssp, SSCR1),
57 ssp_read_reg(ssp, SSTO));
59 dev_dbg(&ssp->pdev->dev, "SSPSP 0x%08x SSSR 0x%08x SSACD 0x%08x\n",
60 ssp_read_reg(ssp, SSPSP), ssp_read_reg(ssp, SSSR),
61 ssp_read_reg(ssp, SSACD));
64 struct pxa2xx_pcm_dma_data {
65 struct pxa2xx_pcm_dma_params params;
69 static struct pxa2xx_pcm_dma_params *
70 ssp_get_dma_params(struct ssp_device *ssp, int width4, int out)
72 struct pxa2xx_pcm_dma_data *dma;
74 dma = kzalloc(sizeof(struct pxa2xx_pcm_dma_data), GFP_KERNEL);
78 snprintf(dma->name, 20, "SSP%d PCM %s %s", ssp->port_id,
79 width4 ? "32-bit" : "16-bit", out ? "out" : "in");
81 dma->params.name = dma->name;
82 dma->params.drcmr = &DRCMR(out ? ssp->drcmr_tx : ssp->drcmr_rx);
83 dma->params.dcmd = (out ? (DCMD_INCSRCADDR | DCMD_FLOWTRG) :
84 (DCMD_INCTRGADDR | DCMD_FLOWSRC)) |
85 (width4 ? DCMD_WIDTH4 : DCMD_WIDTH2) | DCMD_BURST16;
86 dma->params.dev_addr = ssp->phys_base + SSDR;
91 static int pxa_ssp_startup(struct snd_pcm_substream *substream,
92 struct snd_soc_dai *dai)
94 struct snd_soc_pcm_runtime *rtd = substream->private_data;
95 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
96 struct ssp_priv *priv = cpu_dai->private_data;
99 if (!cpu_dai->active) {
100 priv->dev.port = cpu_dai->id + 1;
101 priv->dev.irq = NO_IRQ;
102 clk_enable(priv->dev.ssp->clk);
103 ssp_disable(&priv->dev);
106 kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
107 snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
112 static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
113 struct snd_soc_dai *dai)
115 struct snd_soc_pcm_runtime *rtd = substream->private_data;
116 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
117 struct ssp_priv *priv = cpu_dai->private_data;
119 if (!cpu_dai->active) {
120 ssp_disable(&priv->dev);
121 clk_disable(priv->dev.ssp->clk);
124 kfree(snd_soc_dai_get_dma_data(cpu_dai, substream));
125 snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
130 static int pxa_ssp_suspend(struct snd_soc_dai *cpu_dai)
132 struct ssp_priv *priv = cpu_dai->private_data;
134 if (!cpu_dai->active)
135 clk_enable(priv->dev.ssp->clk);
137 ssp_save_state(&priv->dev, &priv->state);
138 clk_disable(priv->dev.ssp->clk);
143 static int pxa_ssp_resume(struct snd_soc_dai *cpu_dai)
145 struct ssp_priv *priv = cpu_dai->private_data;
147 clk_enable(priv->dev.ssp->clk);
148 ssp_restore_state(&priv->dev, &priv->state);
151 ssp_enable(&priv->dev);
153 clk_disable(priv->dev.ssp->clk);
159 #define pxa_ssp_suspend NULL
160 #define pxa_ssp_resume NULL
164 * ssp_set_clkdiv - set SSP clock divider
165 * @div: serial clock rate divider
167 static void ssp_set_scr(struct ssp_device *ssp, u32 div)
169 u32 sscr0 = ssp_read_reg(ssp, SSCR0);
171 if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP) {
172 sscr0 &= ~0x0000ff00;
173 sscr0 |= ((div - 2)/2) << 8; /* 2..512 */
175 sscr0 &= ~0x000fff00;
176 sscr0 |= (div - 1) << 8; /* 1..4096 */
178 ssp_write_reg(ssp, SSCR0, sscr0);
182 * ssp_get_clkdiv - get SSP clock divider
184 static u32 ssp_get_scr(struct ssp_device *ssp)
186 u32 sscr0 = ssp_read_reg(ssp, SSCR0);
189 if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP)
190 div = ((sscr0 >> 8) & 0xff) * 2 + 2;
192 div = ((sscr0 >> 8) & 0xfff) + 1;
197 * Set the SSP ports SYSCLK.
199 static int pxa_ssp_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
200 int clk_id, unsigned int freq, int dir)
202 struct ssp_priv *priv = cpu_dai->private_data;
203 struct ssp_device *ssp = priv->dev.ssp;
206 u32 sscr0 = ssp_read_reg(ssp, SSCR0) &
207 ~(SSCR0_ECS | SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
209 dev_dbg(&ssp->pdev->dev,
210 "pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %u\n",
211 cpu_dai->id, clk_id, freq);
214 case PXA_SSP_CLK_NET_PLL:
217 case PXA_SSP_CLK_PLL:
218 /* Internal PLL is fixed */
220 priv->sysclk = 1843200;
222 priv->sysclk = 13000000;
224 case PXA_SSP_CLK_EXT:
228 case PXA_SSP_CLK_NET:
230 sscr0 |= SSCR0_NCS | SSCR0_MOD;
232 case PXA_SSP_CLK_AUDIO:
241 /* The SSP clock must be disabled when changing SSP clock mode
242 * on PXA2xx. On PXA3xx it must be enabled when doing so. */
243 if (!cpu_is_pxa3xx())
244 clk_disable(priv->dev.ssp->clk);
245 val = ssp_read_reg(ssp, SSCR0) | sscr0;
246 ssp_write_reg(ssp, SSCR0, val);
247 if (!cpu_is_pxa3xx())
248 clk_enable(priv->dev.ssp->clk);
254 * Set the SSP clock dividers.
256 static int pxa_ssp_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
259 struct ssp_priv *priv = cpu_dai->private_data;
260 struct ssp_device *ssp = priv->dev.ssp;
264 case PXA_SSP_AUDIO_DIV_ACDS:
265 val = (ssp_read_reg(ssp, SSACD) & ~0x7) | SSACD_ACDS(div);
266 ssp_write_reg(ssp, SSACD, val);
268 case PXA_SSP_AUDIO_DIV_SCDB:
269 val = ssp_read_reg(ssp, SSACD);
271 #if defined(CONFIG_PXA3xx)
276 case PXA_SSP_CLK_SCDB_1:
279 case PXA_SSP_CLK_SCDB_4:
281 #if defined(CONFIG_PXA3xx)
282 case PXA_SSP_CLK_SCDB_8:
292 ssp_write_reg(ssp, SSACD, val);
294 case PXA_SSP_DIV_SCR:
295 ssp_set_scr(ssp, div);
305 * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
307 static int pxa_ssp_set_dai_pll(struct snd_soc_dai *cpu_dai, int pll_id,
308 int source, unsigned int freq_in, unsigned int freq_out)
310 struct ssp_priv *priv = cpu_dai->private_data;
311 struct ssp_device *ssp = priv->dev.ssp;
312 u32 ssacd = ssp_read_reg(ssp, SSACD) & ~0x70;
314 #if defined(CONFIG_PXA3xx)
316 ssp_write_reg(ssp, SSACDD, 0);
343 /* PXA3xx has a clock ditherer which can be used to generate
344 * a wider range of frequencies - calculate a value for it.
346 if (cpu_is_pxa3xx()) {
350 do_div(tmp, freq_out);
353 val = (val << 16) | 64;
354 ssp_write_reg(ssp, SSACDD, val);
358 dev_dbg(&ssp->pdev->dev,
359 "Using SSACDD %x to supply %uHz\n",
368 ssp_write_reg(ssp, SSACD, ssacd);
374 * Set the active slots in TDM/Network mode
376 static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
377 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
379 struct ssp_priv *priv = cpu_dai->private_data;
380 struct ssp_device *ssp = priv->dev.ssp;
383 sscr0 = ssp_read_reg(ssp, SSCR0);
384 sscr0 &= ~(SSCR0_MOD | SSCR0_SlotsPerFrm(8) | SSCR0_EDSS | SSCR0_DSS);
388 sscr0 |= SSCR0_EDSS | SSCR0_DataSize(slot_width - 16);
390 sscr0 |= SSCR0_DataSize(slot_width);
393 /* enable network mode */
396 /* set number of active slots */
397 sscr0 |= SSCR0_SlotsPerFrm(slots);
399 /* set active slot mask */
400 ssp_write_reg(ssp, SSTSA, tx_mask);
401 ssp_write_reg(ssp, SSRSA, rx_mask);
403 ssp_write_reg(ssp, SSCR0, sscr0);
409 * Tristate the SSP DAI lines
411 static int pxa_ssp_set_dai_tristate(struct snd_soc_dai *cpu_dai,
414 struct ssp_priv *priv = cpu_dai->private_data;
415 struct ssp_device *ssp = priv->dev.ssp;
418 sscr1 = ssp_read_reg(ssp, SSCR1);
423 ssp_write_reg(ssp, SSCR1, sscr1);
429 * Set up the SSP DAI format.
430 * The SSP Port must be inactive before calling this function as the
431 * physical interface format is changed.
433 static int pxa_ssp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
436 struct ssp_priv *priv = cpu_dai->private_data;
437 struct ssp_device *ssp = priv->dev.ssp;
442 /* check if we need to change anything at all */
443 if (priv->dai_fmt == fmt)
446 /* we can only change the settings if the port is not in use */
447 if (ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
448 dev_err(&ssp->pdev->dev,
449 "can't change hardware dai format: stream is in use");
453 /* reset port settings */
454 sscr0 = ssp_read_reg(ssp, SSCR0) &
455 (SSCR0_ECS | SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
456 sscr1 = SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
459 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
460 case SND_SOC_DAIFMT_CBM_CFM:
461 sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR;
463 case SND_SOC_DAIFMT_CBM_CFS:
464 sscr1 |= SSCR1_SCLKDIR;
466 case SND_SOC_DAIFMT_CBS_CFS:
472 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
473 case SND_SOC_DAIFMT_NB_NF:
474 sspsp |= SSPSP_SFRMP;
476 case SND_SOC_DAIFMT_NB_IF:
478 case SND_SOC_DAIFMT_IB_IF:
479 sspsp |= SSPSP_SCMODE(2);
481 case SND_SOC_DAIFMT_IB_NF:
482 sspsp |= SSPSP_SCMODE(2) | SSPSP_SFRMP;
488 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
489 case SND_SOC_DAIFMT_I2S:
491 sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
492 /* See hw_params() */
495 case SND_SOC_DAIFMT_DSP_A:
497 case SND_SOC_DAIFMT_DSP_B:
498 sscr0 |= SSCR0_MOD | SSCR0_PSP;
499 sscr1 |= SSCR1_TRAIL | SSCR1_RWOT;
506 ssp_write_reg(ssp, SSCR0, sscr0);
507 ssp_write_reg(ssp, SSCR1, sscr1);
508 ssp_write_reg(ssp, SSPSP, sspsp);
512 /* Since we are configuring the timings for the format by hand
513 * we have to defer some things until hw_params() where we
514 * know parameters like the sample size.
522 * Set the SSP audio DMA parameters and sample size.
523 * Can be called multiple times by oss emulation.
525 static int pxa_ssp_hw_params(struct snd_pcm_substream *substream,
526 struct snd_pcm_hw_params *params,
527 struct snd_soc_dai *dai)
529 struct snd_soc_pcm_runtime *rtd = substream->private_data;
530 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
531 struct ssp_priv *priv = cpu_dai->private_data;
532 struct ssp_device *ssp = priv->dev.ssp;
533 int chn = params_channels(params);
536 int width = snd_pcm_format_physical_width(params_format(params));
537 int ttsa = ssp_read_reg(ssp, SSTSA) & 0xf;
538 struct pxa2xx_pcm_dma_params *dma_data;
540 dma_data = snd_soc_dai_get_dma_data(dai, substream);
542 /* generate correct DMA params */
545 /* Network mode with one active slot (ttsa == 1) can be used
546 * to force 16-bit frame width on the wire (for S16_LE), even
547 * with two channels. Use 16-bit DMA transfers for this case.
549 dma_data = ssp_get_dma_params(ssp,
550 ((chn == 2) && (ttsa != 1)) || (width == 32),
551 substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
553 snd_soc_dai_set_dma_data(dai, substream, dma_data);
555 /* we can only change the settings if the port is not in use */
556 if (ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
559 /* clear selected SSP bits */
560 sscr0 = ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
561 ssp_write_reg(ssp, SSCR0, sscr0);
564 sscr0 = ssp_read_reg(ssp, SSCR0);
565 switch (params_format(params)) {
566 case SNDRV_PCM_FORMAT_S16_LE:
569 sscr0 |= SSCR0_FPCKE;
571 sscr0 |= SSCR0_DataSize(16);
573 case SNDRV_PCM_FORMAT_S24_LE:
574 sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(8));
576 case SNDRV_PCM_FORMAT_S32_LE:
577 sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(16));
580 ssp_write_reg(ssp, SSCR0, sscr0);
582 switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
583 case SND_SOC_DAIFMT_I2S:
584 sspsp = ssp_read_reg(ssp, SSPSP);
586 if ((ssp_get_scr(ssp) == 4) && (width == 16)) {
587 /* This is a special case where the bitclk is 64fs
588 * and we're not dealing with 2*32 bits of audio
591 * The SSP values used for that are all found out by
592 * trying and failing a lot; some of the registers
593 * needed for that mode are only available on PXA3xx.
597 if (!cpu_is_pxa3xx())
600 sspsp |= SSPSP_SFRMWDTH(width * 2);
601 sspsp |= SSPSP_SFRMDLY(width * 4);
602 sspsp |= SSPSP_EDMYSTOP(3);
603 sspsp |= SSPSP_DMYSTOP(3);
604 sspsp |= SSPSP_DMYSTRT(1);
609 /* The frame width is the width the LRCLK is
610 * asserted for; the delay is expressed in
611 * half cycle units. We need the extra cycle
612 * because the data starts clocking out one BCLK
613 * after LRCLK changes polarity.
615 sspsp |= SSPSP_SFRMWDTH(width + 1);
616 sspsp |= SSPSP_SFRMDLY((width + 1) * 2);
617 sspsp |= SSPSP_DMYSTRT(1);
620 ssp_write_reg(ssp, SSPSP, sspsp);
626 /* When we use a network mode, we always require TDM slots
627 * - complain loudly and fail if they've not been set up yet.
629 if ((sscr0 & SSCR0_MOD) && !ttsa) {
630 dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
639 static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
640 struct snd_soc_dai *dai)
642 struct snd_soc_pcm_runtime *rtd = substream->private_data;
643 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
645 struct ssp_priv *priv = cpu_dai->private_data;
646 struct ssp_device *ssp = priv->dev.ssp;
650 case SNDRV_PCM_TRIGGER_RESUME:
651 ssp_enable(&priv->dev);
653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
654 val = ssp_read_reg(ssp, SSCR1);
655 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
659 ssp_write_reg(ssp, SSCR1, val);
660 val = ssp_read_reg(ssp, SSSR);
661 ssp_write_reg(ssp, SSSR, val);
663 case SNDRV_PCM_TRIGGER_START:
664 val = ssp_read_reg(ssp, SSCR1);
665 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
669 ssp_write_reg(ssp, SSCR1, val);
670 ssp_enable(&priv->dev);
672 case SNDRV_PCM_TRIGGER_STOP:
673 val = ssp_read_reg(ssp, SSCR1);
674 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
678 ssp_write_reg(ssp, SSCR1, val);
680 case SNDRV_PCM_TRIGGER_SUSPEND:
681 ssp_disable(&priv->dev);
683 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
684 val = ssp_read_reg(ssp, SSCR1);
685 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
689 ssp_write_reg(ssp, SSCR1, val);
701 static int pxa_ssp_probe(struct platform_device *pdev,
702 struct snd_soc_dai *dai)
704 struct ssp_priv *priv;
707 priv = kzalloc(sizeof(struct ssp_priv), GFP_KERNEL);
711 priv->dev.ssp = ssp_request(dai->id + 1, "SoC audio");
712 if (priv->dev.ssp == NULL) {
717 priv->dai_fmt = (unsigned int) -1;
718 dai->private_data = priv;
727 static void pxa_ssp_remove(struct platform_device *pdev,
728 struct snd_soc_dai *dai)
730 struct ssp_priv *priv = dai->private_data;
731 ssp_free(priv->dev.ssp);
734 #define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
735 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
736 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
737 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
739 #define PXA_SSP_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
740 SNDRV_PCM_FMTBIT_S24_LE | \
741 SNDRV_PCM_FMTBIT_S32_LE)
743 static struct snd_soc_dai_ops pxa_ssp_dai_ops = {
744 .startup = pxa_ssp_startup,
745 .shutdown = pxa_ssp_shutdown,
746 .trigger = pxa_ssp_trigger,
747 .hw_params = pxa_ssp_hw_params,
748 .set_sysclk = pxa_ssp_set_dai_sysclk,
749 .set_clkdiv = pxa_ssp_set_dai_clkdiv,
750 .set_pll = pxa_ssp_set_dai_pll,
751 .set_fmt = pxa_ssp_set_dai_fmt,
752 .set_tdm_slot = pxa_ssp_set_dai_tdm_slot,
753 .set_tristate = pxa_ssp_set_dai_tristate,
756 struct snd_soc_dai pxa_ssp_dai[] = {
758 .name = "pxa2xx-ssp1",
760 .probe = pxa_ssp_probe,
761 .remove = pxa_ssp_remove,
762 .suspend = pxa_ssp_suspend,
763 .resume = pxa_ssp_resume,
767 .rates = PXA_SSP_RATES,
768 .formats = PXA_SSP_FORMATS,
773 .rates = PXA_SSP_RATES,
774 .formats = PXA_SSP_FORMATS,
776 .ops = &pxa_ssp_dai_ops,
778 { .name = "pxa2xx-ssp2",
780 .probe = pxa_ssp_probe,
781 .remove = pxa_ssp_remove,
782 .suspend = pxa_ssp_suspend,
783 .resume = pxa_ssp_resume,
787 .rates = PXA_SSP_RATES,
788 .formats = PXA_SSP_FORMATS,
793 .rates = PXA_SSP_RATES,
794 .formats = PXA_SSP_FORMATS,
796 .ops = &pxa_ssp_dai_ops,
799 .name = "pxa2xx-ssp3",
801 .probe = pxa_ssp_probe,
802 .remove = pxa_ssp_remove,
803 .suspend = pxa_ssp_suspend,
804 .resume = pxa_ssp_resume,
808 .rates = PXA_SSP_RATES,
809 .formats = PXA_SSP_FORMATS,
814 .rates = PXA_SSP_RATES,
815 .formats = PXA_SSP_FORMATS,
817 .ops = &pxa_ssp_dai_ops,
820 .name = "pxa2xx-ssp4",
822 .probe = pxa_ssp_probe,
823 .remove = pxa_ssp_remove,
824 .suspend = pxa_ssp_suspend,
825 .resume = pxa_ssp_resume,
829 .rates = PXA_SSP_RATES,
830 .formats = PXA_SSP_FORMATS,
835 .rates = PXA_SSP_RATES,
836 .formats = PXA_SSP_FORMATS,
838 .ops = &pxa_ssp_dai_ops,
841 EXPORT_SYMBOL_GPL(pxa_ssp_dai);
843 static int __init pxa_ssp_init(void)
845 return snd_soc_register_dais(pxa_ssp_dai, ARRAY_SIZE(pxa_ssp_dai));
847 module_init(pxa_ssp_init);
849 static void __exit pxa_ssp_exit(void)
851 snd_soc_unregister_dais(pxa_ssp_dai, ARRAY_SIZE(pxa_ssp_dai));
853 module_exit(pxa_ssp_exit);
855 /* Module information */
856 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
857 MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
858 MODULE_LICENSE("GPL");