2 * pxa2xx-i2s.c -- ALSA Soc Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
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>
24 #include <sound/pxa2xx-lib.h>
26 #include <mach/hardware.h>
27 #include <mach/pxa-regs.h>
28 #include <mach/pxa2xx-gpio.h>
29 #include <mach/audio.h>
31 #include "pxa2xx-pcm.h"
32 #include "pxa2xx-i2s.h"
51 static struct pxa_i2s_port pxa_i2s;
52 static struct clk *clk_i2s;
54 static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_out = {
55 .name = "I2S PCM Stereo out",
56 .dev_addr = __PREG(SADR),
57 .drcmr = &DRCMRTXSADR,
58 .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
59 DCMD_BURST32 | DCMD_WIDTH4,
62 static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_in = {
63 .name = "I2S PCM Stereo in",
64 .dev_addr = __PREG(SADR),
65 .drcmr = &DRCMRRXSADR,
66 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
67 DCMD_BURST32 | DCMD_WIDTH4,
70 static struct pxa2xx_gpio gpio_bus[] = {
72 .rx = GPIO29_SDATA_IN_I2S_MD,
73 .tx = GPIO30_SDATA_OUT_I2S_MD,
74 .clk = GPIO28_BITCLK_IN_I2S_MD,
75 .frm = GPIO31_SYNC_I2S_MD,
77 { /* I2S SoC Master */
78 .rx = GPIO29_SDATA_IN_I2S_MD,
79 .tx = GPIO30_SDATA_OUT_I2S_MD,
80 .clk = GPIO28_BITCLK_OUT_I2S_MD,
81 .frm = GPIO31_SYNC_I2S_MD,
85 static int pxa2xx_i2s_startup(struct snd_pcm_substream *substream)
87 struct snd_soc_pcm_runtime *rtd = substream->private_data;
88 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
91 return PTR_ERR(clk_i2s);
93 if (!cpu_dai->active) {
101 /* wait for I2S controller to be ready */
102 static int pxa_i2s_wait(void)
106 /* flush the Rx FIFO */
107 for(i = 0; i < 16; i++)
112 static int pxa2xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
115 /* interface format */
116 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
117 case SND_SOC_DAIFMT_I2S:
120 case SND_SOC_DAIFMT_LEFT_J:
121 pxa_i2s.fmt = SACR1_AMSL;
125 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
126 case SND_SOC_DAIFMT_CBS_CFS:
129 case SND_SOC_DAIFMT_CBM_CFS:
138 static int pxa2xx_i2s_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
139 int clk_id, unsigned int freq, int dir)
141 if (clk_id != PXA2XX_I2S_SYSCLK)
144 if (pxa_i2s.master && dir == SND_SOC_CLOCK_OUT)
145 pxa_gpio_mode(gpio_bus[pxa_i2s.master].sys);
150 static int pxa2xx_i2s_hw_params(struct snd_pcm_substream *substream,
151 struct snd_pcm_hw_params *params)
153 struct snd_soc_pcm_runtime *rtd = substream->private_data;
154 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
156 pxa_gpio_mode(gpio_bus[pxa_i2s.master].rx);
157 pxa_gpio_mode(gpio_bus[pxa_i2s.master].tx);
158 pxa_gpio_mode(gpio_bus[pxa_i2s.master].frm);
159 pxa_gpio_mode(gpio_bus[pxa_i2s.master].clk);
160 BUG_ON(IS_ERR(clk_i2s));
164 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
165 cpu_dai->dma_data = &pxa2xx_i2s_pcm_stereo_out;
167 cpu_dai->dma_data = &pxa2xx_i2s_pcm_stereo_in;
169 /* is port used by another stream */
170 if (!(SACR0 & SACR0_ENB)) {
177 SACR0 |= SACR0_RFTH(14) | SACR0_TFTH(1);
178 SACR1 |= pxa_i2s.fmt;
180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
185 switch (params_rate(params)) {
204 case 96000: /* not in manual and possibly slightly inaccurate */
212 static int pxa2xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
217 case SNDRV_PCM_TRIGGER_START:
220 case SNDRV_PCM_TRIGGER_RESUME:
221 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
222 case SNDRV_PCM_TRIGGER_STOP:
223 case SNDRV_PCM_TRIGGER_SUSPEND:
224 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233 static void pxa2xx_i2s_shutdown(struct snd_pcm_substream *substream)
235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
243 if (SACR1 & (SACR1_DREC | SACR1_DRPL)) {
246 clk_disable(clk_i2s);
253 static int pxa2xx_i2s_suspend(struct platform_device *dev,
254 struct snd_soc_dai *dai)
259 /* store registers */
260 pxa_i2s.sacr0 = SACR0;
261 pxa_i2s.sacr1 = SACR1;
262 pxa_i2s.saimr = SAIMR;
263 pxa_i2s.sadiv = SADIV;
265 /* deactivate link */
271 static int pxa2xx_i2s_resume(struct platform_device *pdev,
272 struct snd_soc_dai *dai)
279 SACR0 = pxa_i2s.sacr0 &= ~SACR0_ENB;
280 SACR1 = pxa_i2s.sacr1;
281 SAIMR = pxa_i2s.saimr;
282 SADIV = pxa_i2s.sadiv;
289 #define pxa2xx_i2s_suspend NULL
290 #define pxa2xx_i2s_resume NULL
293 #define PXA2XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
294 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
295 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
297 struct snd_soc_dai pxa_i2s_dai = {
298 .name = "pxa2xx-i2s",
300 .type = SND_SOC_DAI_I2S,
301 .suspend = pxa2xx_i2s_suspend,
302 .resume = pxa2xx_i2s_resume,
306 .rates = PXA2XX_I2S_RATES,
307 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
311 .rates = PXA2XX_I2S_RATES,
312 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
314 .startup = pxa2xx_i2s_startup,
315 .shutdown = pxa2xx_i2s_shutdown,
316 .trigger = pxa2xx_i2s_trigger,
317 .hw_params = pxa2xx_i2s_hw_params,},
319 .set_fmt = pxa2xx_i2s_set_dai_fmt,
320 .set_sysclk = pxa2xx_i2s_set_dai_sysclk,
324 EXPORT_SYMBOL_GPL(pxa_i2s_dai);
326 static int pxa2xx_i2s_probe(struct platform_device *dev)
328 clk_i2s = clk_get(&dev->dev, "I2SCLK");
329 return IS_ERR(clk_i2s) ? PTR_ERR(clk_i2s) : 0;
332 static int __devexit pxa2xx_i2s_remove(struct platform_device *dev)
335 clk_i2s = ERR_PTR(-ENOENT);
339 static struct platform_driver pxa2xx_i2s_driver = {
340 .probe = pxa2xx_i2s_probe,
341 .remove = __devexit_p(pxa2xx_i2s_remove),
344 .name = "pxa2xx-i2s",
345 .owner = THIS_MODULE,
349 static int __init pxa2xx_i2s_init(void)
352 gpio_bus[1].sys = GPIO113_I2S_SYSCLK_MD;
354 gpio_bus[1].sys = GPIO32_SYSCLK_I2S_MD;
356 clk_i2s = ERR_PTR(-ENOENT);
357 return platform_driver_register(&pxa2xx_i2s_driver);
360 static void __exit pxa2xx_i2s_exit(void)
362 platform_driver_unregister(&pxa2xx_i2s_driver);
365 module_init(pxa2xx_i2s_init);
366 module_exit(pxa2xx_i2s_exit);
368 /* Module information */
369 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
370 MODULE_DESCRIPTION("pxa2xx I2S SoC Interface");
371 MODULE_LICENSE("GPL");