2 * Au12x0/Au1550 PSC ALSA ASoC audio support.
4 * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
5 * Manuel Lauss <manuel.lauss@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Au1xxx-PSC I2S glue.
13 * NOTE: all of these drivers can only work with a SINGLE instance
14 * of a PSC. Multiple independent audio devices are impossible
16 * NOTE: so far only PSC slave mode (bit- and frameclock) is supported.
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/suspend.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/initval.h>
26 #include <sound/soc.h>
27 #include <asm/mach-au1x00/au1000.h>
28 #include <asm/mach-au1x00/au1xxx_psc.h>
32 /* supported I2S DAI hardware formats */
33 #define AU1XPSC_I2S_DAIFMT \
34 (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | \
37 /* supported I2S direction */
38 #define AU1XPSC_I2S_DIR \
39 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
41 #define AU1XPSC_I2S_RATES \
42 SNDRV_PCM_RATE_8000_192000
44 #define AU1XPSC_I2S_FMTS \
45 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
47 #define I2SSTAT_BUSY(stype) \
48 ((stype) == PCM_TX ? PSC_I2SSTAT_TB : PSC_I2SSTAT_RB)
49 #define I2SPCR_START(stype) \
50 ((stype) == PCM_TX ? PSC_I2SPCR_TS : PSC_I2SPCR_RS)
51 #define I2SPCR_STOP(stype) \
52 ((stype) == PCM_TX ? PSC_I2SPCR_TP : PSC_I2SPCR_RP)
53 #define I2SPCR_CLRFIFO(stype) \
54 ((stype) == PCM_TX ? PSC_I2SPCR_TC : PSC_I2SPCR_RC)
57 /* instance data. There can be only one, MacLeod!!!! */
58 static struct au1xpsc_audio_data *au1xpsc_i2s_workdata;
60 static int au1xpsc_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
63 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
71 ct &= ~(PSC_I2SCFG_XM | PSC_I2SCFG_MLJ); /* left-justified */
72 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
73 case SND_SOC_DAIFMT_I2S:
74 ct |= PSC_I2SCFG_XM; /* enable I2S mode */
76 case SND_SOC_DAIFMT_MSB:
78 case SND_SOC_DAIFMT_LSB:
79 ct |= PSC_I2SCFG_MLJ; /* LSB (right-) justified */
85 ct &= ~(PSC_I2SCFG_BI | PSC_I2SCFG_WI); /* IB-IF */
86 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
87 case SND_SOC_DAIFMT_NB_NF:
88 ct |= PSC_I2SCFG_BI | PSC_I2SCFG_WI;
90 case SND_SOC_DAIFMT_NB_IF:
93 case SND_SOC_DAIFMT_IB_NF:
96 case SND_SOC_DAIFMT_IB_IF:
102 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
103 case SND_SOC_DAIFMT_CBM_CFM: /* CODEC master */
104 ct |= PSC_I2SCFG_MS; /* PSC I2S slave mode */
106 case SND_SOC_DAIFMT_CBS_CFS: /* CODEC slave */
107 ct &= ~PSC_I2SCFG_MS; /* PSC I2S Master mode */
119 static int au1xpsc_i2s_hw_params(struct snd_pcm_substream *substream,
120 struct snd_pcm_hw_params *params,
121 struct snd_soc_dai *dai)
123 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
128 /* check if the PSC is already streaming data */
129 stat = au_readl(I2S_STAT(pscdata));
130 if (stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB)) {
131 /* reject parameters not currently set up in hardware */
132 cfgbits = au_readl(I2S_CFG(pscdata));
133 if ((PSC_I2SCFG_GET_LEN(cfgbits) != params->msbits) ||
134 (params_rate(params) != pscdata->rate))
137 /* set sample bitdepth */
138 pscdata->cfg &= ~(0x1f << 4);
139 pscdata->cfg |= PSC_I2SCFG_SET_LEN(params->msbits);
140 /* remember current rate for other stream */
141 pscdata->rate = params_rate(params);
146 /* Configure PSC late: on my devel systems the codec is I2S master and
147 * supplies the i2sbitclock __AND__ i2sMclk (!) to the PSC unit. ASoC
148 * uses aggressive PM and switches the codec off when it is not in use
149 * which also means the PSC unit doesn't get any clocks and is therefore
150 * dead. That's why this chunk here gets called from the trigger callback
151 * because I can be reasonably certain the codec is driving the clocks.
153 static int au1xpsc_i2s_configure(struct au1xpsc_audio_data *pscdata)
157 /* bring PSC out of sleep, and configure I2S unit */
158 au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
162 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_SR) && tmo)
168 au_writel(0, I2S_CFG(pscdata));
170 au_writel(pscdata->cfg | PSC_I2SCFG_DE_ENABLE, I2S_CFG(pscdata));
173 /* wait for I2S controller to become ready */
175 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_DR) && tmo)
182 au_writel(0, I2S_CFG(pscdata));
183 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
188 static int au1xpsc_i2s_start(struct au1xpsc_audio_data *pscdata, int stype)
190 unsigned long tmo, stat;
195 /* if both TX and RX are idle, configure the PSC */
196 stat = au_readl(I2S_STAT(pscdata));
197 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
198 ret = au1xpsc_i2s_configure(pscdata);
203 au_writel(I2SPCR_CLRFIFO(stype), I2S_PCR(pscdata));
205 au_writel(I2SPCR_START(stype), I2S_PCR(pscdata));
208 /* wait for start confirmation */
210 while (!(au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
214 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
222 static int au1xpsc_i2s_stop(struct au1xpsc_audio_data *pscdata, int stype)
224 unsigned long tmo, stat;
226 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
229 /* wait for stop confirmation */
231 while ((au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
234 /* if both TX and RX are idle, disable PSC */
235 stat = au_readl(I2S_STAT(pscdata));
236 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
237 au_writel(0, I2S_CFG(pscdata));
239 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
245 static int au1xpsc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
246 struct snd_soc_dai *dai)
248 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
249 int ret, stype = SUBSTREAM_TYPE(substream);
252 case SNDRV_PCM_TRIGGER_START:
253 case SNDRV_PCM_TRIGGER_RESUME:
254 ret = au1xpsc_i2s_start(pscdata, stype);
256 case SNDRV_PCM_TRIGGER_STOP:
257 case SNDRV_PCM_TRIGGER_SUSPEND:
258 ret = au1xpsc_i2s_stop(pscdata, stype);
266 static int au1xpsc_i2s_probe(struct snd_soc_dai *dai)
268 return au1xpsc_i2s_workdata ? 0 : -ENODEV;
271 static struct snd_soc_dai_ops au1xpsc_i2s_dai_ops = {
272 .trigger = au1xpsc_i2s_trigger,
273 .hw_params = au1xpsc_i2s_hw_params,
274 .set_fmt = au1xpsc_i2s_set_fmt,
277 static struct snd_soc_dai_driver au1xpsc_i2s_dai = {
278 .probe = au1xpsc_i2s_probe,
280 .rates = AU1XPSC_I2S_RATES,
281 .formats = AU1XPSC_I2S_FMTS,
283 .channels_max = 8, /* 2 without external help */
286 .rates = AU1XPSC_I2S_RATES,
287 .formats = AU1XPSC_I2S_FMTS,
289 .channels_max = 8, /* 2 without external help */
291 .ops = &au1xpsc_i2s_dai_ops,
294 static int __devinit au1xpsc_i2s_drvprobe(struct platform_device *pdev)
299 struct au1xpsc_audio_data *wd;
301 if (au1xpsc_i2s_workdata)
304 wd = kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
308 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
315 if (!request_mem_region(r->start, resource_size(r), pdev->name))
318 wd->mmio = ioremap(r->start, resource_size(r));
322 /* preserve PSC clock source set up by platform (dev.platform_data
323 * is already occupied by soc layer)
325 sel = au_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
326 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
328 au_writel(PSC_SEL_PS_I2SMODE | sel, PSC_SEL(wd));
329 au_writel(0, I2S_CFG(wd));
332 /* preconfigure: set max rx/tx fifo depths */
333 wd->cfg |= PSC_I2SCFG_RT_FIFO8 | PSC_I2SCFG_TT_FIFO8;
335 /* don't wait for I2S core to become ready now; clocks may not
336 * be running yet; depending on clock input for PSC a wait might
340 ret = snd_soc_register_dai(&pdev->dev, &au1xpsc_i2s_dai);
344 /* finally add the DMA device for this PSC */
345 wd->dmapd = au1xpsc_pcm_add(pdev);
347 platform_set_drvdata(pdev, wd);
348 au1xpsc_i2s_workdata = wd;
352 snd_soc_unregister_dai(&pdev->dev);
354 release_mem_region(r->start, resource_size(r));
360 static int __devexit au1xpsc_i2s_drvremove(struct platform_device *pdev)
362 struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
363 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366 au1xpsc_pcm_destroy(wd->dmapd);
368 snd_soc_unregister_dai(&pdev->dev);
370 au_writel(0, I2S_CFG(wd));
372 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
376 release_mem_region(r->start, resource_size(r));
379 au1xpsc_i2s_workdata = NULL; /* MDEV */
385 static int au1xpsc_i2s_drvsuspend(struct device *dev)
387 struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
389 /* save interesting register and disable PSC */
390 wd->pm[0] = au_readl(PSC_SEL(wd));
392 au_writel(0, I2S_CFG(wd));
394 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
400 static int au1xpsc_i2s_drvresume(struct device *dev)
402 struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
404 /* select I2S mode and PSC clock */
405 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
407 au_writel(0, PSC_SEL(wd));
409 au_writel(wd->pm[0], PSC_SEL(wd));
415 static struct dev_pm_ops au1xpsci2s_pmops = {
416 .suspend = au1xpsc_i2s_drvsuspend,
417 .resume = au1xpsc_i2s_drvresume,
420 #define AU1XPSCI2S_PMOPS &au1xpsci2s_pmops
424 #define AU1XPSCI2S_PMOPS NULL
428 static struct platform_driver au1xpsc_i2s_driver = {
431 .owner = THIS_MODULE,
432 .pm = AU1XPSCI2S_PMOPS,
434 .probe = au1xpsc_i2s_drvprobe,
435 .remove = __devexit_p(au1xpsc_i2s_drvremove),
438 static int __init au1xpsc_i2s_load(void)
440 au1xpsc_i2s_workdata = NULL;
441 return platform_driver_register(&au1xpsc_i2s_driver);
444 static void __exit au1xpsc_i2s_unload(void)
446 platform_driver_unregister(&au1xpsc_i2s_driver);
449 module_init(au1xpsc_i2s_load);
450 module_exit(au1xpsc_i2s_unload);
452 MODULE_LICENSE("GPL");
453 MODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
454 MODULE_AUTHOR("Manuel Lauss");