2 * pxa2xx-i2s.c -- ALSA Soc Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@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 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/delay.h>
18 #include <linux/clk.h>
19 #include <linux/platform_device.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/initval.h>
23 #include <sound/soc.h>
25 #include <mach/hardware.h>
26 #include <mach/pxa-regs.h>
27 #include <mach/pxa2xx-gpio.h>
28 #include <mach/audio.h>
30 #include "pxa2xx-pcm.h"
31 #include "pxa2xx-i2s.h"
41 static struct pxa_i2s_port pxa_i2s;
42 static struct clk *clk_i2s;
44 static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_out = {
45 .name = "I2S PCM Stereo out",
46 .dev_addr = __PREG(SADR),
47 .drcmr = &DRCMRTXSADR,
48 .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
49 DCMD_BURST32 | DCMD_WIDTH4,
52 static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_in = {
53 .name = "I2S PCM Stereo in",
54 .dev_addr = __PREG(SADR),
55 .drcmr = &DRCMRRXSADR,
56 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
57 DCMD_BURST32 | DCMD_WIDTH4,
60 static struct pxa2xx_gpio gpio_bus[] = {
62 .rx = GPIO29_SDATA_IN_I2S_MD,
63 .tx = GPIO30_SDATA_OUT_I2S_MD,
64 .clk = GPIO28_BITCLK_IN_I2S_MD,
65 .frm = GPIO31_SYNC_I2S_MD,
67 { /* I2S SoC Master */
69 .sys = GPIO113_I2S_SYSCLK_MD,
71 .sys = GPIO32_SYSCLK_I2S_MD,
73 .rx = GPIO29_SDATA_IN_I2S_MD,
74 .tx = GPIO30_SDATA_OUT_I2S_MD,
75 .clk = GPIO28_BITCLK_OUT_I2S_MD,
76 .frm = GPIO31_SYNC_I2S_MD,
80 static int pxa2xx_i2s_startup(struct snd_pcm_substream *substream)
82 struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
86 return PTR_ERR(clk_i2s);
88 if (!cpu_dai->active) {
96 /* wait for I2S controller to be ready */
97 static int pxa_i2s_wait(void)
101 /* flush the Rx FIFO */
102 for(i = 0; i < 16; i++)
107 static int pxa2xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
110 /* interface format */
111 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
112 case SND_SOC_DAIFMT_I2S:
115 case SND_SOC_DAIFMT_LEFT_J:
116 pxa_i2s.fmt = SACR1_AMSL;
120 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
121 case SND_SOC_DAIFMT_CBS_CFS:
124 case SND_SOC_DAIFMT_CBM_CFS:
133 static int pxa2xx_i2s_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
134 int clk_id, unsigned int freq, int dir)
136 if (clk_id != PXA2XX_I2S_SYSCLK)
139 if (pxa_i2s.master && dir == SND_SOC_CLOCK_OUT)
140 pxa_gpio_mode(gpio_bus[pxa_i2s.master].sys);
145 static int pxa2xx_i2s_hw_params(struct snd_pcm_substream *substream,
146 struct snd_pcm_hw_params *params)
148 struct snd_soc_pcm_runtime *rtd = substream->private_data;
149 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
151 pxa_gpio_mode(gpio_bus[pxa_i2s.master].rx);
152 pxa_gpio_mode(gpio_bus[pxa_i2s.master].tx);
153 pxa_gpio_mode(gpio_bus[pxa_i2s.master].frm);
154 pxa_gpio_mode(gpio_bus[pxa_i2s.master].clk);
155 BUG_ON(IS_ERR(clk_i2s));
159 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
160 cpu_dai->dma_data = &pxa2xx_i2s_pcm_stereo_out;
162 cpu_dai->dma_data = &pxa2xx_i2s_pcm_stereo_in;
164 /* is port used by another stream */
165 if (!(SACR0 & SACR0_ENB)) {
172 SACR0 |= SACR0_RFTH(14) | SACR0_TFTH(1);
173 SACR1 |= pxa_i2s.fmt;
175 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
180 switch (params_rate(params)) {
199 case 96000: /* not in manual and possibly slightly inaccurate */
207 static int pxa2xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
212 case SNDRV_PCM_TRIGGER_START:
215 case SNDRV_PCM_TRIGGER_RESUME:
216 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
217 case SNDRV_PCM_TRIGGER_STOP:
218 case SNDRV_PCM_TRIGGER_SUSPEND:
219 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
228 static void pxa2xx_i2s_shutdown(struct snd_pcm_substream *substream)
230 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
238 if (SACR1 & (SACR1_DREC | SACR1_DRPL)) {
241 clk_disable(clk_i2s);
248 static int pxa2xx_i2s_suspend(struct platform_device *dev,
249 struct snd_soc_dai *dai)
254 /* store registers */
255 pxa_i2s.sacr0 = SACR0;
256 pxa_i2s.sacr1 = SACR1;
257 pxa_i2s.saimr = SAIMR;
258 pxa_i2s.sadiv = SADIV;
260 /* deactivate link */
266 static int pxa2xx_i2s_resume(struct platform_device *pdev,
267 struct snd_soc_dai *dai)
274 SACR0 = pxa_i2s.sacr0 &= ~SACR0_ENB;
275 SACR1 = pxa_i2s.sacr1;
276 SAIMR = pxa_i2s.saimr;
277 SADIV = pxa_i2s.sadiv;
284 #define pxa2xx_i2s_suspend NULL
285 #define pxa2xx_i2s_resume NULL
288 #define PXA2XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
289 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
290 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
292 struct snd_soc_dai pxa_i2s_dai = {
293 .name = "pxa2xx-i2s",
295 .type = SND_SOC_DAI_I2S,
296 .suspend = pxa2xx_i2s_suspend,
297 .resume = pxa2xx_i2s_resume,
301 .rates = PXA2XX_I2S_RATES,
302 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
306 .rates = PXA2XX_I2S_RATES,
307 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
309 .startup = pxa2xx_i2s_startup,
310 .shutdown = pxa2xx_i2s_shutdown,
311 .trigger = pxa2xx_i2s_trigger,
312 .hw_params = pxa2xx_i2s_hw_params,},
314 .set_fmt = pxa2xx_i2s_set_dai_fmt,
315 .set_sysclk = pxa2xx_i2s_set_dai_sysclk,
319 EXPORT_SYMBOL_GPL(pxa_i2s_dai);
321 static int pxa2xx_i2s_probe(struct platform_device *dev)
323 clk_i2s = clk_get(&dev->dev, "I2SCLK");
324 return IS_ERR(clk_i2s) ? PTR_ERR(clk_i2s) : 0;
327 static int __devexit pxa2xx_i2s_remove(struct platform_device *dev)
330 clk_i2s = ERR_PTR(-ENOENT);
334 static struct platform_driver pxa2xx_i2s_driver = {
335 .probe = pxa2xx_i2s_probe,
336 .remove = __devexit_p(pxa2xx_i2s_remove),
339 .name = "pxa2xx-i2s",
340 .owner = THIS_MODULE,
344 static int __init pxa2xx_i2s_init(void)
346 clk_i2s = ERR_PTR(-ENOENT);
347 return platform_driver_register(&pxa2xx_i2s_driver);
350 static void __exit pxa2xx_i2s_exit(void)
352 platform_driver_unregister(&pxa2xx_i2s_driver);
355 module_init(pxa2xx_i2s_init);
356 module_exit(pxa2xx_i2s_exit);
358 /* Module information */
359 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
360 MODULE_DESCRIPTION("pxa2xx I2S SoC Interface");
361 MODULE_LICENSE("GPL");