2 * Au12x0/Au1550 PSC ALSA ASoC audio support.
4 * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
5 * Manuel Lauss <mano@roarinelk.homelinux.net>
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/suspend.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
26 #include <asm/mach-au1x00/au1000.h>
27 #include <asm/mach-au1x00/au1xxx_psc.h>
31 /* supported I2S DAI hardware formats */
32 #define AU1XPSC_I2S_DAIFMT \
33 (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | \
36 /* supported I2S direction */
37 #define AU1XPSC_I2S_DIR \
38 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
40 #define AU1XPSC_I2S_RATES \
41 SNDRV_PCM_RATE_8000_192000
43 #define AU1XPSC_I2S_FMTS \
44 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
46 #define I2SSTAT_BUSY(stype) \
47 ((stype) == PCM_TX ? PSC_I2SSTAT_TB : PSC_I2SSTAT_RB)
48 #define I2SPCR_START(stype) \
49 ((stype) == PCM_TX ? PSC_I2SPCR_TS : PSC_I2SPCR_RS)
50 #define I2SPCR_STOP(stype) \
51 ((stype) == PCM_TX ? PSC_I2SPCR_TP : PSC_I2SPCR_RP)
52 #define I2SPCR_CLRFIFO(stype) \
53 ((stype) == PCM_TX ? PSC_I2SPCR_TC : PSC_I2SPCR_RC)
56 /* instance data. There can be only one, MacLeod!!!! */
57 static struct au1xpsc_audio_data *au1xpsc_i2s_workdata;
59 static int au1xpsc_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
62 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
70 ct &= ~(PSC_I2SCFG_XM | PSC_I2SCFG_MLJ); /* left-justified */
71 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
72 case SND_SOC_DAIFMT_I2S:
73 ct |= PSC_I2SCFG_XM; /* enable I2S mode */
75 case SND_SOC_DAIFMT_MSB:
77 case SND_SOC_DAIFMT_LSB:
78 ct |= PSC_I2SCFG_MLJ; /* LSB (right-) justified */
84 ct &= ~(PSC_I2SCFG_BI | PSC_I2SCFG_WI); /* IB-IF */
85 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
86 case SND_SOC_DAIFMT_NB_NF:
87 ct |= PSC_I2SCFG_BI | PSC_I2SCFG_WI;
89 case SND_SOC_DAIFMT_NB_IF:
92 case SND_SOC_DAIFMT_IB_NF:
95 case SND_SOC_DAIFMT_IB_IF:
101 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
102 case SND_SOC_DAIFMT_CBM_CFM: /* CODEC master */
103 ct |= PSC_I2SCFG_MS; /* PSC I2S slave mode */
105 case SND_SOC_DAIFMT_CBS_CFS: /* CODEC slave */
106 ct &= ~PSC_I2SCFG_MS; /* PSC I2S Master mode */
118 static int au1xpsc_i2s_hw_params(struct snd_pcm_substream *substream,
119 struct snd_pcm_hw_params *params,
120 struct snd_soc_dai *dai)
122 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
127 /* check if the PSC is already streaming data */
128 stat = au_readl(I2S_STAT(pscdata));
129 if (stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB)) {
130 /* reject parameters not currently set up in hardware */
131 cfgbits = au_readl(I2S_CFG(pscdata));
132 if ((PSC_I2SCFG_GET_LEN(cfgbits) != params->msbits) ||
133 (params_rate(params) != pscdata->rate))
136 /* set sample bitdepth */
137 pscdata->cfg &= ~(0x1f << 4);
138 pscdata->cfg |= PSC_I2SCFG_SET_LEN(params->msbits);
139 /* remember current rate for other stream */
140 pscdata->rate = params_rate(params);
145 /* Configure PSC late: on my devel systems the codec is I2S master and
146 * supplies the i2sbitclock __AND__ i2sMclk (!) to the PSC unit. ASoC
147 * uses aggressive PM and switches the codec off when it is not in use
148 * which also means the PSC unit doesn't get any clocks and is therefore
149 * dead. That's why this chunk here gets called from the trigger callback
150 * because I can be reasonably certain the codec is driving the clocks.
152 static int au1xpsc_i2s_configure(struct au1xpsc_audio_data *pscdata)
156 /* bring PSC out of sleep, and configure I2S unit */
157 au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
161 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_SR) && tmo)
167 au_writel(0, I2S_CFG(pscdata));
169 au_writel(pscdata->cfg | PSC_I2SCFG_DE_ENABLE, I2S_CFG(pscdata));
172 /* wait for I2S controller to become ready */
174 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_DR) && tmo)
181 au_writel(0, I2S_CFG(pscdata));
182 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
187 static int au1xpsc_i2s_start(struct au1xpsc_audio_data *pscdata, int stype)
189 unsigned long tmo, stat;
194 /* if both TX and RX are idle, configure the PSC */
195 stat = au_readl(I2S_STAT(pscdata));
196 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
197 ret = au1xpsc_i2s_configure(pscdata);
202 au_writel(I2SPCR_CLRFIFO(stype), I2S_PCR(pscdata));
204 au_writel(I2SPCR_START(stype), I2S_PCR(pscdata));
207 /* wait for start confirmation */
209 while (!(au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
213 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
221 static int au1xpsc_i2s_stop(struct au1xpsc_audio_data *pscdata, int stype)
223 unsigned long tmo, stat;
225 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
228 /* wait for stop confirmation */
230 while ((au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
233 /* if both TX and RX are idle, disable PSC */
234 stat = au_readl(I2S_STAT(pscdata));
235 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
236 au_writel(0, I2S_CFG(pscdata));
238 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
244 static int au1xpsc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
245 struct snd_soc_dai *dai)
247 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
248 int ret, stype = SUBSTREAM_TYPE(substream);
251 case SNDRV_PCM_TRIGGER_START:
252 case SNDRV_PCM_TRIGGER_RESUME:
253 ret = au1xpsc_i2s_start(pscdata, stype);
255 case SNDRV_PCM_TRIGGER_STOP:
256 case SNDRV_PCM_TRIGGER_SUSPEND:
257 ret = au1xpsc_i2s_stop(pscdata, stype);
265 static int au1xpsc_i2s_probe(struct platform_device *pdev,
266 struct snd_soc_dai *dai)
272 if (au1xpsc_i2s_workdata)
275 au1xpsc_i2s_workdata =
276 kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
277 if (!au1xpsc_i2s_workdata)
280 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
287 au1xpsc_i2s_workdata->ioarea =
288 request_mem_region(r->start, r->end - r->start + 1,
290 if (!au1xpsc_i2s_workdata->ioarea)
293 au1xpsc_i2s_workdata->mmio = ioremap(r->start, 0xffff);
294 if (!au1xpsc_i2s_workdata->mmio)
297 /* preserve PSC clock source set up by platform (dev.platform_data
298 * is already occupied by soc layer)
300 sel = au_readl(PSC_SEL(au1xpsc_i2s_workdata)) & PSC_SEL_CLK_MASK;
301 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_i2s_workdata));
303 au_writel(PSC_SEL_PS_I2SMODE | sel, PSC_SEL(au1xpsc_i2s_workdata));
304 au_writel(0, I2S_CFG(au1xpsc_i2s_workdata));
307 /* preconfigure: set max rx/tx fifo depths */
308 au1xpsc_i2s_workdata->cfg |=
309 PSC_I2SCFG_RT_FIFO8 | PSC_I2SCFG_TT_FIFO8;
311 /* don't wait for I2S core to become ready now; clocks may not
312 * be running yet; depending on clock input for PSC a wait might
319 release_resource(au1xpsc_i2s_workdata->ioarea);
320 kfree(au1xpsc_i2s_workdata->ioarea);
322 kfree(au1xpsc_i2s_workdata);
323 au1xpsc_i2s_workdata = NULL;
327 static void au1xpsc_i2s_remove(struct platform_device *pdev,
328 struct snd_soc_dai *dai)
330 au_writel(0, I2S_CFG(au1xpsc_i2s_workdata));
332 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_i2s_workdata));
335 iounmap(au1xpsc_i2s_workdata->mmio);
336 release_resource(au1xpsc_i2s_workdata->ioarea);
337 kfree(au1xpsc_i2s_workdata->ioarea);
338 kfree(au1xpsc_i2s_workdata);
339 au1xpsc_i2s_workdata = NULL;
342 static int au1xpsc_i2s_suspend(struct snd_soc_dai *cpu_dai)
344 /* save interesting register and disable PSC */
345 au1xpsc_i2s_workdata->pm[0] =
346 au_readl(PSC_SEL(au1xpsc_i2s_workdata));
348 au_writel(0, I2S_CFG(au1xpsc_i2s_workdata));
350 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_i2s_workdata));
356 static int au1xpsc_i2s_resume(struct snd_soc_dai *cpu_dai)
358 /* select I2S mode and PSC clock */
359 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_i2s_workdata));
361 au_writel(0, PSC_SEL(au1xpsc_i2s_workdata));
363 au_writel(au1xpsc_i2s_workdata->pm[0],
364 PSC_SEL(au1xpsc_i2s_workdata));
370 static struct snd_soc_dai_ops au1xpsc_i2s_dai_ops = {
371 .trigger = au1xpsc_i2s_trigger,
372 .hw_params = au1xpsc_i2s_hw_params,
373 .set_fmt = au1xpsc_i2s_set_fmt,
376 struct snd_soc_dai au1xpsc_i2s_dai = {
377 .name = "au1xpsc_i2s",
378 .probe = au1xpsc_i2s_probe,
379 .remove = au1xpsc_i2s_remove,
380 .suspend = au1xpsc_i2s_suspend,
381 .resume = au1xpsc_i2s_resume,
383 .rates = AU1XPSC_I2S_RATES,
384 .formats = AU1XPSC_I2S_FMTS,
386 .channels_max = 8, /* 2 without external help */
389 .rates = AU1XPSC_I2S_RATES,
390 .formats = AU1XPSC_I2S_FMTS,
392 .channels_max = 8, /* 2 without external help */
394 .ops = &au1xpsc_i2s_dai_ops,
396 EXPORT_SYMBOL(au1xpsc_i2s_dai);
398 static int __init au1xpsc_i2s_init(void)
400 au1xpsc_i2s_workdata = NULL;
401 return snd_soc_register_dai(&au1xpsc_i2s_dai);
404 static void __exit au1xpsc_i2s_exit(void)
406 snd_soc_unregister_dai(&au1xpsc_i2s_dai);
409 module_init(au1xpsc_i2s_init);
410 module_exit(au1xpsc_i2s_exit);
412 MODULE_LICENSE("GPL");
413 MODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
414 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");