2 * Au12x0/Au1550 PSC ALSA ASoC audio support.
4 * (c) 2007-2009 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 AC97 glue.
13 * NOTE: all of these drivers can only work with a SINGLE instance
14 * of a PSC. Multiple independent audio devices are impossible
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/mutex.h>
23 #include <linux/suspend.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28 #include <asm/mach-au1x00/au1000.h>
29 #include <asm/mach-au1x00/au1xxx_psc.h>
33 /* how often to retry failed codec register reads/writes */
34 #define AC97_RW_RETRIES 5
37 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
40 SNDRV_PCM_RATE_8000_48000
43 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3BE)
45 #define AC97PCR_START(stype) \
46 ((stype) == PCM_TX ? PSC_AC97PCR_TS : PSC_AC97PCR_RS)
47 #define AC97PCR_STOP(stype) \
48 ((stype) == PCM_TX ? PSC_AC97PCR_TP : PSC_AC97PCR_RP)
49 #define AC97PCR_CLRFIFO(stype) \
50 ((stype) == PCM_TX ? PSC_AC97PCR_TC : PSC_AC97PCR_RC)
52 #define AC97STAT_BUSY(stype) \
53 ((stype) == PCM_TX ? PSC_AC97STAT_TB : PSC_AC97STAT_RB)
55 /* instance data. There can be only one, MacLeod!!!! */
56 static struct au1xpsc_audio_data
*au1xpsc_ac97_workdata
;
58 /* AC97 controller reads codec register */
59 static unsigned short au1xpsc_ac97_read(struct snd_ac97
*ac97
,
63 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
64 unsigned short data
, retry
, tmo
;
66 au_writel(PSC_AC97EVNT_CD
, AC97_EVNT(pscdata
));
69 retry
= AC97_RW_RETRIES
;
71 mutex_lock(&pscdata
->lock
);
73 au_writel(PSC_AC97CDC_RD
| PSC_AC97CDC_INDX(reg
),
78 while ((!(au_readl(AC97_EVNT(pscdata
)) & PSC_AC97EVNT_CD
))
82 data
= au_readl(AC97_CDC(pscdata
)) & 0xffff;
84 au_writel(PSC_AC97EVNT_CD
, AC97_EVNT(pscdata
));
87 mutex_unlock(&pscdata
->lock
);
88 } while (--retry
&& !tmo
);
90 return retry
? data
: 0xffff;
93 /* AC97 controller writes to codec register */
94 static void au1xpsc_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
98 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
99 unsigned int tmo
, retry
;
101 au_writel(PSC_AC97EVNT_CD
, AC97_EVNT(pscdata
));
104 retry
= AC97_RW_RETRIES
;
106 mutex_lock(&pscdata
->lock
);
108 au_writel(PSC_AC97CDC_INDX(reg
) | (val
& 0xffff),
113 while ((!(au_readl(AC97_EVNT(pscdata
)) & PSC_AC97EVNT_CD
))
117 au_writel(PSC_AC97EVNT_CD
, AC97_EVNT(pscdata
));
120 mutex_unlock(&pscdata
->lock
);
121 } while (--retry
&& !tmo
);
124 /* AC97 controller asserts a warm reset */
125 static void au1xpsc_ac97_warm_reset(struct snd_ac97
*ac97
)
128 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
130 au_writel(PSC_AC97RST_SNC
, AC97_RST(pscdata
));
133 au_writel(0, AC97_RST(pscdata
));
137 static void au1xpsc_ac97_cold_reset(struct snd_ac97
*ac97
)
140 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
143 /* disable PSC during cold reset */
144 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata
));
146 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(pscdata
));
149 /* issue cold reset */
150 au_writel(PSC_AC97RST_RST
, AC97_RST(pscdata
));
153 au_writel(0, AC97_RST(pscdata
));
157 au_writel(PSC_CTRL_ENABLE
, PSC_CTRL(pscdata
));
160 /* wait for PSC to indicate it's ready */
162 while (!((au_readl(AC97_STAT(pscdata
)) & PSC_AC97STAT_SR
)) && (--i
))
166 printk(KERN_ERR
"au1xpsc-ac97: PSC not ready!\n");
170 /* enable the ac97 function */
171 au_writel(pscdata
->cfg
| PSC_AC97CFG_DE_ENABLE
, AC97_CFG(pscdata
));
174 /* wait for AC97 core to become ready */
176 while (!((au_readl(AC97_STAT(pscdata
)) & PSC_AC97STAT_DR
)) && (--i
))
179 printk(KERN_ERR
"au1xpsc-ac97: AC97 ctrl not ready\n");
182 /* AC97 controller operations */
183 struct snd_ac97_bus_ops soc_ac97_ops
= {
184 .read
= au1xpsc_ac97_read
,
185 .write
= au1xpsc_ac97_write
,
186 .reset
= au1xpsc_ac97_cold_reset
,
187 .warm_reset
= au1xpsc_ac97_warm_reset
,
189 EXPORT_SYMBOL_GPL(soc_ac97_ops
);
191 static int au1xpsc_ac97_hw_params(struct snd_pcm_substream
*substream
,
192 struct snd_pcm_hw_params
*params
,
193 struct snd_soc_dai
*dai
)
196 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
197 unsigned long r
, ro
, stat
;
198 int chans
, stype
= SUBSTREAM_TYPE(substream
);
200 chans
= params_channels(params
);
202 r
= ro
= au_readl(AC97_CFG(pscdata
));
203 stat
= au_readl(AC97_STAT(pscdata
));
205 /* already active? */
206 if (stat
& (PSC_AC97STAT_TB
| PSC_AC97STAT_RB
)) {
207 /* reject parameters not currently set up */
208 if ((PSC_AC97CFG_GET_LEN(r
) != params
->msbits
) ||
209 (pscdata
->rate
!= params_rate(params
)))
213 /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
214 r
&= ~PSC_AC97CFG_LEN_MASK
;
215 r
|= PSC_AC97CFG_SET_LEN(params
->msbits
);
217 /* channels: enable slots for front L/R channel */
218 if (stype
== PCM_TX
) {
219 r
&= ~PSC_AC97CFG_TXSLOT_MASK
;
220 r
|= PSC_AC97CFG_TXSLOT_ENA(3);
221 r
|= PSC_AC97CFG_TXSLOT_ENA(4);
223 r
&= ~PSC_AC97CFG_RXSLOT_MASK
;
224 r
|= PSC_AC97CFG_RXSLOT_ENA(3);
225 r
|= PSC_AC97CFG_RXSLOT_ENA(4);
228 /* do we need to poke the hardware? */
232 /* ac97 engine is about to be disabled */
233 mutex_lock(&pscdata
->lock
);
235 /* disable AC97 device controller first... */
236 au_writel(r
& ~PSC_AC97CFG_DE_ENABLE
, AC97_CFG(pscdata
));
239 /* ...wait for it... */
240 while (au_readl(AC97_STAT(pscdata
)) & PSC_AC97STAT_DR
)
241 asm volatile ("nop");
243 /* ...write config... */
244 au_writel(r
, AC97_CFG(pscdata
));
247 /* ...enable the AC97 controller again... */
248 au_writel(r
| PSC_AC97CFG_DE_ENABLE
, AC97_CFG(pscdata
));
251 /* ...and wait for ready bit */
252 while (!(au_readl(AC97_STAT(pscdata
)) & PSC_AC97STAT_DR
))
253 asm volatile ("nop");
255 mutex_unlock(&pscdata
->lock
);
258 pscdata
->rate
= params_rate(params
);
265 static int au1xpsc_ac97_trigger(struct snd_pcm_substream
*substream
,
266 int cmd
, struct snd_soc_dai
*dai
)
269 struct au1xpsc_audio_data
*pscdata
= au1xpsc_ac97_workdata
;
270 int ret
, stype
= SUBSTREAM_TYPE(substream
);
275 case SNDRV_PCM_TRIGGER_START
:
276 case SNDRV_PCM_TRIGGER_RESUME
:
277 au_writel(AC97PCR_CLRFIFO(stype
), AC97_PCR(pscdata
));
279 au_writel(AC97PCR_START(stype
), AC97_PCR(pscdata
));
282 case SNDRV_PCM_TRIGGER_STOP
:
283 case SNDRV_PCM_TRIGGER_SUSPEND
:
284 au_writel(AC97PCR_STOP(stype
), AC97_PCR(pscdata
));
287 while (au_readl(AC97_STAT(pscdata
)) & AC97STAT_BUSY(stype
))
288 asm volatile ("nop");
290 au_writel(AC97PCR_CLRFIFO(stype
), AC97_PCR(pscdata
));
300 static int au1xpsc_ac97_probe(struct platform_device
*pdev
,
301 struct snd_soc_dai
*dai
)
307 if (au1xpsc_ac97_workdata
)
310 au1xpsc_ac97_workdata
=
311 kzalloc(sizeof(struct au1xpsc_audio_data
), GFP_KERNEL
);
312 if (!au1xpsc_ac97_workdata
)
315 mutex_init(&au1xpsc_ac97_workdata
->lock
);
317 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
324 au1xpsc_ac97_workdata
->ioarea
=
325 request_mem_region(r
->start
, r
->end
- r
->start
+ 1,
327 if (!au1xpsc_ac97_workdata
->ioarea
)
330 au1xpsc_ac97_workdata
->mmio
= ioremap(r
->start
, 0xffff);
331 if (!au1xpsc_ac97_workdata
->mmio
)
334 /* configuration: max dma trigger threshold, enable ac97 */
335 au1xpsc_ac97_workdata
->cfg
= PSC_AC97CFG_RT_FIFO8
|
336 PSC_AC97CFG_TT_FIFO8
|
337 PSC_AC97CFG_DE_ENABLE
;
339 /* preserve PSC clock source set up by platform (dev.platform_data
340 * is already occupied by soc layer)
342 sel
= au_readl(PSC_SEL(au1xpsc_ac97_workdata
)) & PSC_SEL_CLK_MASK
;
343 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(au1xpsc_ac97_workdata
));
345 au_writel(0, PSC_SEL(au1xpsc_ac97_workdata
));
347 au_writel(PSC_SEL_PS_AC97MODE
| sel
, PSC_SEL(au1xpsc_ac97_workdata
));
349 /* next up: cold reset. Dont check for PSC-ready now since
350 * there may not be any codec clock yet.
356 release_resource(au1xpsc_ac97_workdata
->ioarea
);
357 kfree(au1xpsc_ac97_workdata
->ioarea
);
359 kfree(au1xpsc_ac97_workdata
);
360 au1xpsc_ac97_workdata
= NULL
;
364 static void au1xpsc_ac97_remove(struct platform_device
*pdev
,
365 struct snd_soc_dai
*dai
)
367 /* disable PSC completely */
368 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata
));
370 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(au1xpsc_ac97_workdata
));
373 iounmap(au1xpsc_ac97_workdata
->mmio
);
374 release_resource(au1xpsc_ac97_workdata
->ioarea
);
375 kfree(au1xpsc_ac97_workdata
->ioarea
);
376 kfree(au1xpsc_ac97_workdata
);
377 au1xpsc_ac97_workdata
= NULL
;
380 static int au1xpsc_ac97_suspend(struct snd_soc_dai
*dai
)
382 /* save interesting registers and disable PSC */
383 au1xpsc_ac97_workdata
->pm
[0] =
384 au_readl(PSC_SEL(au1xpsc_ac97_workdata
));
386 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata
));
388 au_writel(PSC_CTRL_DISABLE
, PSC_CTRL(au1xpsc_ac97_workdata
));
394 static int au1xpsc_ac97_resume(struct snd_soc_dai
*dai
)
396 /* restore PSC clock config */
397 au_writel(au1xpsc_ac97_workdata
->pm
[0] | PSC_SEL_PS_AC97MODE
,
398 PSC_SEL(au1xpsc_ac97_workdata
));
401 /* after this point the ac97 core will cold-reset the codec.
402 * During cold-reset the PSC is reinitialized and the last
403 * configuration set up in hw_params() is restored.
408 static struct snd_soc_dai_ops au1xpsc_ac97_dai_ops
= {
409 .trigger
= au1xpsc_ac97_trigger
,
410 .hw_params
= au1xpsc_ac97_hw_params
,
413 struct snd_soc_dai au1xpsc_ac97_dai
= {
414 .name
= "au1xpsc_ac97",
416 .probe
= au1xpsc_ac97_probe
,
417 .remove
= au1xpsc_ac97_remove
,
418 .suspend
= au1xpsc_ac97_suspend
,
419 .resume
= au1xpsc_ac97_resume
,
422 .formats
= AC97_FMTS
,
428 .formats
= AC97_FMTS
,
432 .ops
= &au1xpsc_ac97_dai_ops
,
434 EXPORT_SYMBOL_GPL(au1xpsc_ac97_dai
);
436 static int __init
au1xpsc_ac97_init(void)
438 au1xpsc_ac97_workdata
= NULL
;
439 return snd_soc_register_dai(&au1xpsc_ac97_dai
);
442 static void __exit
au1xpsc_ac97_exit(void)
444 snd_soc_unregister_dai(&au1xpsc_ac97_dai
);
447 module_init(au1xpsc_ac97_init
);
448 module_exit(au1xpsc_ac97_exit
);
450 MODULE_LICENSE("GPL");
451 MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
452 MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");