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 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/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>
33 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
36 SNDRV_PCM_RATE_8000_48000
39 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3BE)
41 #define AC97PCR_START(stype) \
42 ((stype) == PCM_TX ? PSC_AC97PCR_TS : PSC_AC97PCR_RS)
43 #define AC97PCR_STOP(stype) \
44 ((stype) == PCM_TX ? PSC_AC97PCR_TP : PSC_AC97PCR_RP)
45 #define AC97PCR_CLRFIFO(stype) \
46 ((stype) == PCM_TX ? PSC_AC97PCR_TC : PSC_AC97PCR_RC)
48 /* instance data. There can be only one, MacLeod!!!! */
49 static struct au1xpsc_audio_data *au1xpsc_ac97_workdata;
51 /* AC97 controller reads codec register */
52 static unsigned short au1xpsc_ac97_read(struct snd_ac97 *ac97,
56 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
57 unsigned short data, tmo;
59 au_writel(PSC_AC97CDC_RD | PSC_AC97CDC_INDX(reg), AC97_CDC(pscdata));
63 while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)) && --tmo)
69 data = au_readl(AC97_CDC(pscdata)) & 0xffff;
71 au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
77 /* AC97 controller writes to codec register */
78 static void au1xpsc_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
82 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
85 au_writel(PSC_AC97CDC_INDX(reg) | (val & 0xffff), AC97_CDC(pscdata));
88 while ((!(au_readl(AC97_EVNT(pscdata)) & PSC_AC97EVNT_CD)) && --tmo)
91 au_writel(PSC_AC97EVNT_CD, AC97_EVNT(pscdata));
95 /* AC97 controller asserts a warm reset */
96 static void au1xpsc_ac97_warm_reset(struct snd_ac97 *ac97)
99 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
101 au_writel(PSC_AC97RST_SNC, AC97_RST(pscdata));
104 au_writel(0, AC97_RST(pscdata));
108 static void au1xpsc_ac97_cold_reset(struct snd_ac97 *ac97)
111 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
114 /* disable PSC during cold reset */
115 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
117 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(pscdata));
120 /* issue cold reset */
121 au_writel(PSC_AC97RST_RST, AC97_RST(pscdata));
124 au_writel(0, AC97_RST(pscdata));
128 au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
131 /* wait for PSC to indicate it's ready */
133 while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_SR)) && (--i))
137 printk(KERN_ERR "au1xpsc-ac97: PSC not ready!\n");
141 /* enable the ac97 function */
142 au_writel(pscdata->cfg | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
145 /* wait for AC97 core to become ready */
147 while (!((au_readl(AC97_STAT(pscdata)) & PSC_AC97STAT_DR)) && (--i))
150 printk(KERN_ERR "au1xpsc-ac97: AC97 ctrl not ready\n");
153 /* AC97 controller operations */
154 struct snd_ac97_bus_ops soc_ac97_ops = {
155 .read = au1xpsc_ac97_read,
156 .write = au1xpsc_ac97_write,
157 .reset = au1xpsc_ac97_cold_reset,
158 .warm_reset = au1xpsc_ac97_warm_reset,
160 EXPORT_SYMBOL_GPL(soc_ac97_ops);
162 static int au1xpsc_ac97_hw_params(struct snd_pcm_substream *substream,
163 struct snd_pcm_hw_params *params)
166 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
167 unsigned long r, stat;
168 int chans, stype = SUBSTREAM_TYPE(substream);
170 chans = params_channels(params);
172 r = au_readl(AC97_CFG(pscdata));
173 stat = au_readl(AC97_STAT(pscdata));
175 /* already active? */
176 if (stat & (PSC_AC97STAT_TB | PSC_AC97STAT_RB)) {
177 /* reject parameters not currently set up */
178 if ((PSC_AC97CFG_GET_LEN(r) != params->msbits) ||
179 (pscdata->rate != params_rate(params)))
182 /* disable AC97 device controller first */
183 au_writel(r & ~PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
186 /* set sample bitdepth: REG[24:21]=(BITS-2)/2 */
187 r &= ~PSC_AC97CFG_LEN_MASK;
188 r |= PSC_AC97CFG_SET_LEN(params->msbits);
190 /* channels: enable slots for front L/R channel */
191 if (stype == PCM_TX) {
192 r &= ~PSC_AC97CFG_TXSLOT_MASK;
193 r |= PSC_AC97CFG_TXSLOT_ENA(3);
194 r |= PSC_AC97CFG_TXSLOT_ENA(4);
196 r &= ~PSC_AC97CFG_RXSLOT_MASK;
197 r |= PSC_AC97CFG_RXSLOT_ENA(3);
198 r |= PSC_AC97CFG_RXSLOT_ENA(4);
201 /* finally enable the AC97 controller again */
202 au_writel(r | PSC_AC97CFG_DE_ENABLE, AC97_CFG(pscdata));
206 pscdata->rate = params_rate(params);
212 static int au1xpsc_ac97_trigger(struct snd_pcm_substream *substream,
216 struct au1xpsc_audio_data *pscdata = au1xpsc_ac97_workdata;
217 int ret, stype = SUBSTREAM_TYPE(substream);
222 case SNDRV_PCM_TRIGGER_START:
223 case SNDRV_PCM_TRIGGER_RESUME:
224 au_writel(AC97PCR_START(stype), AC97_PCR(pscdata));
227 case SNDRV_PCM_TRIGGER_STOP:
228 case SNDRV_PCM_TRIGGER_SUSPEND:
229 au_writel(AC97PCR_STOP(stype), AC97_PCR(pscdata));
238 static int au1xpsc_ac97_probe(struct platform_device *pdev,
239 struct snd_soc_dai *dai)
245 if (au1xpsc_ac97_workdata)
248 au1xpsc_ac97_workdata =
249 kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
250 if (!au1xpsc_ac97_workdata)
253 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
260 au1xpsc_ac97_workdata->ioarea =
261 request_mem_region(r->start, r->end - r->start + 1,
263 if (!au1xpsc_ac97_workdata->ioarea)
266 au1xpsc_ac97_workdata->mmio = ioremap(r->start, 0xffff);
267 if (!au1xpsc_ac97_workdata->mmio)
270 /* configuration: max dma trigger threshold, enable ac97 */
271 au1xpsc_ac97_workdata->cfg = PSC_AC97CFG_RT_FIFO8 |
272 PSC_AC97CFG_TT_FIFO8 |
273 PSC_AC97CFG_DE_ENABLE;
275 /* preserve PSC clock source set up by platform (dev.platform_data
276 * is already occupied by soc layer)
278 sel = au_readl(PSC_SEL(au1xpsc_ac97_workdata)) & PSC_SEL_CLK_MASK;
279 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
281 au_writel(0, PSC_SEL(au1xpsc_ac97_workdata));
283 au_writel(PSC_SEL_PS_AC97MODE | sel, PSC_SEL(au1xpsc_ac97_workdata));
285 /* next up: cold reset. Dont check for PSC-ready now since
286 * there may not be any codec clock yet.
292 release_resource(au1xpsc_ac97_workdata->ioarea);
293 kfree(au1xpsc_ac97_workdata->ioarea);
295 kfree(au1xpsc_ac97_workdata);
296 au1xpsc_ac97_workdata = NULL;
300 static void au1xpsc_ac97_remove(struct platform_device *pdev,
301 struct snd_soc_dai *dai)
303 /* disable PSC completely */
304 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
306 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
309 iounmap(au1xpsc_ac97_workdata->mmio);
310 release_resource(au1xpsc_ac97_workdata->ioarea);
311 kfree(au1xpsc_ac97_workdata->ioarea);
312 kfree(au1xpsc_ac97_workdata);
313 au1xpsc_ac97_workdata = NULL;
316 static int au1xpsc_ac97_suspend(struct platform_device *pdev,
317 struct snd_soc_dai *dai)
319 /* save interesting registers and disable PSC */
320 au1xpsc_ac97_workdata->pm[0] =
321 au_readl(PSC_SEL(au1xpsc_ac97_workdata));
323 au_writel(0, AC97_CFG(au1xpsc_ac97_workdata));
325 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(au1xpsc_ac97_workdata));
331 static int au1xpsc_ac97_resume(struct platform_device *pdev,
332 struct snd_soc_dai *dai)
334 /* restore PSC clock config */
335 au_writel(au1xpsc_ac97_workdata->pm[0] | PSC_SEL_PS_AC97MODE,
336 PSC_SEL(au1xpsc_ac97_workdata));
339 /* after this point the ac97 core will cold-reset the codec.
340 * During cold-reset the PSC is reinitialized and the last
341 * configuration set up in hw_params() is restored.
346 struct snd_soc_dai au1xpsc_ac97_dai = {
347 .name = "au1xpsc_ac97",
348 .type = SND_SOC_DAI_AC97,
349 .probe = au1xpsc_ac97_probe,
350 .remove = au1xpsc_ac97_remove,
351 .suspend = au1xpsc_ac97_suspend,
352 .resume = au1xpsc_ac97_resume,
355 .formats = AC97_FMTS,
361 .formats = AC97_FMTS,
366 .trigger = au1xpsc_ac97_trigger,
367 .hw_params = au1xpsc_ac97_hw_params,
370 EXPORT_SYMBOL_GPL(au1xpsc_ac97_dai);
372 static int __init au1xpsc_ac97_init(void)
374 au1xpsc_ac97_workdata = NULL;
378 static void __exit au1xpsc_ac97_exit(void)
382 module_init(au1xpsc_ac97_init);
383 module_exit(au1xpsc_ac97_exit);
385 MODULE_LICENSE("GPL");
386 MODULE_DESCRIPTION("Au12x0/Au1550 PSC AC97 ALSA ASoC audio driver");
387 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");