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/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
)
268 return au1xpsc_i2s_workdata
? 0 : -ENODEV
;
271 static void au1xpsc_i2s_remove(struct platform_device
*pdev
,
272 struct snd_soc_dai
*dai
)
276 static struct snd_soc_dai_ops au1xpsc_i2s_dai_ops
= {
277 .trigger
= au1xpsc_i2s_trigger
,
278 .hw_params
= au1xpsc_i2s_hw_params
,
279 .set_fmt
= au1xpsc_i2s_set_fmt
,
282 struct snd_soc_dai au1xpsc_i2s_dai
= {
283 .name
= "au1xpsc_i2s",
284 .probe
= au1xpsc_i2s_probe
,
285 .remove
= au1xpsc_i2s_remove
,
287 .rates
= AU1XPSC_I2S_RATES
,
288 .formats
= AU1XPSC_I2S_FMTS
,
290 .channels_max
= 8, /* 2 without external help */
293 .rates
= AU1XPSC_I2S_RATES
,
294 .formats
= AU1XPSC_I2S_FMTS
,
296 .channels_max
= 8, /* 2 without external help */
298 .ops
= &au1xpsc_i2s_dai_ops
,
300 EXPORT_SYMBOL(au1xpsc_i2s_dai
);
302 static int __init
au1xpsc_i2s_drvprobe(struct platform_device
*pdev
)
307 struct au1xpsc_audio_data
*wd
;
309 if (au1xpsc_i2s_workdata
)
312 wd
= kzalloc(sizeof(struct au1xpsc_audio_data
), GFP_KERNEL
);
316 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
323 wd
->ioarea
= request_mem_region(r
->start
, r
->end
- r
->start
+ 1,
328 wd
->mmio
= ioremap(r
->start
, 0xffff);
332 /* preserve PSC clock source set up by platform (dev.platform_data
333 * is already occupied by soc layer)
335 sel
= au_readl(PSC_SEL(wd
)) & PSC_SEL_CLK_MASK
;
336 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(wd
));
338 au_writel(PSC_SEL_PS_I2SMODE
| sel
, PSC_SEL(wd
));
339 au_writel(0, I2S_CFG(wd
));
342 /* preconfigure: set max rx/tx fifo depths */
343 wd
->cfg
|= PSC_I2SCFG_RT_FIFO8
| PSC_I2SCFG_TT_FIFO8
;
345 /* don't wait for I2S core to become ready now; clocks may not
346 * be running yet; depending on clock input for PSC a wait might
350 ret
= snd_soc_register_dai(&au1xpsc_i2s_dai
);
354 /* finally add the DMA device for this PSC */
355 wd
->dmapd
= au1xpsc_pcm_add(pdev
);
357 platform_set_drvdata(pdev
, wd
);
358 au1xpsc_i2s_workdata
= wd
;
362 snd_soc_unregister_dai(&au1xpsc_i2s_dai
);
364 release_resource(wd
->ioarea
);
371 static int __devexit
au1xpsc_i2s_drvremove(struct platform_device
*pdev
)
373 struct au1xpsc_audio_data
*wd
= platform_get_drvdata(pdev
);
376 au1xpsc_pcm_destroy(wd
->dmapd
);
378 snd_soc_unregister_dai(&au1xpsc_i2s_dai
);
380 au_writel(0, I2S_CFG(wd
));
382 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(wd
));
386 release_resource(wd
->ioarea
);
390 au1xpsc_i2s_workdata
= NULL
; /* MDEV */
396 static int au1xpsc_i2s_drvsuspend(struct device
*dev
)
398 struct au1xpsc_audio_data
*wd
= dev_get_drvdata(dev
);
400 /* save interesting register and disable PSC */
401 wd
->pm
[0] = au_readl(PSC_SEL(wd
));
403 au_writel(0, I2S_CFG(wd
));
405 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(wd
));
411 static int au1xpsc_i2s_drvresume(struct device
*dev
)
413 struct au1xpsc_audio_data
*wd
= dev_get_drvdata(dev
);
415 /* select I2S mode and PSC clock */
416 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(wd
));
418 au_writel(0, PSC_SEL(wd
));
420 au_writel(wd
->pm
[0], PSC_SEL(wd
));
426 static struct dev_pm_ops au1xpsci2s_pmops
= {
427 .suspend
= au1xpsc_i2s_drvsuspend
,
428 .resume
= au1xpsc_i2s_drvresume
,
431 #define AU1XPSCI2S_PMOPS &au1xpsci2s_pmops
435 #define AU1XPSCI2S_PMOPS NULL
439 static struct platform_driver au1xpsc_i2s_driver
= {
441 .name
= "au1xpsc_i2s",
442 .owner
= THIS_MODULE
,
443 .pm
= AU1XPSCI2S_PMOPS
,
445 .probe
= au1xpsc_i2s_drvprobe
,
446 .remove
= __devexit_p(au1xpsc_i2s_drvremove
),
449 static int __init
au1xpsc_i2s_load(void)
451 au1xpsc_i2s_workdata
= NULL
;
452 return platform_driver_register(&au1xpsc_i2s_driver
);
455 static void __exit
au1xpsc_i2s_unload(void)
457 platform_driver_unregister(&au1xpsc_i2s_driver
);
460 module_init(au1xpsc_i2s_load
);
461 module_exit(au1xpsc_i2s_unload
);
463 MODULE_LICENSE("GPL");
464 MODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
465 MODULE_AUTHOR("Manuel Lauss");