2 * s3c2443-ac97.c -- ALSA Soc Audio Layer
4 * (c) 2007 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
7 * Copyright (C) 2005, Sean Choi <sh428.choi@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * 21st Mar 2007 Initial Version
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/wait.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
26 #include <sound/driver.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/ac97_codec.h>
30 #include <sound/initval.h>
31 #include <sound/soc.h>
33 #include <asm/hardware.h>
35 #include <asm/arch/regs-ac97.h>
36 #include <asm/arch/regs-gpio.h>
37 #include <asm/arch/regs-clock.h>
38 #include <asm/arch/audio.h>
40 #include <asm/arch/dma.h>
42 #include "s3c24xx-pcm.h"
43 #include "s3c24xx-ac97.h"
45 struct s3c24xx_ac97_info {
49 static struct s3c24xx_ac97_info s3c24xx_ac97;
51 DECLARE_COMPLETION(ac97_completion);
52 static u32 codec_ready;
53 static DECLARE_MUTEX(ac97_mutex);
55 static unsigned short s3c2443_ac97_read(struct snd_ac97 *ac97,
64 codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
65 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
66 ac_codec_cmd = S3C_AC97_CODEC_CMD_READ | AC_CMD_ADDR(reg);
67 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
71 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
72 ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
73 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
75 wait_for_completion(&ac97_completion);
77 stat = readl(s3c24xx_ac97.regs + S3C_AC97_STAT);
78 addr = (stat >> 16) & 0x7f;
79 data = (stat & 0xffff);
82 printk(KERN_ERR "s3c24xx-ac97: req addr = %02x,"
83 " rep addr = %02x\n", reg, addr);
87 return (unsigned short)data;
90 static void s3c2443_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
98 codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
99 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
100 ac_codec_cmd = AC_CMD_ADDR(reg) | AC_CMD_DATA(val);
101 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
105 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
106 ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
107 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
109 wait_for_completion(&ac97_completion);
111 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
112 ac_codec_cmd |= S3C_AC97_CODEC_CMD_READ;
113 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
119 static void s3c2443_ac97_warm_reset(struct snd_ac97 *ac97)
123 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
124 ac_glbctrl = S3C_AC97_GLBCTRL_WARMRESET;
125 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
129 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
133 static void s3c2443_ac97_cold_reset(struct snd_ac97 *ac97)
137 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
138 ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
139 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
143 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
146 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
147 ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
148 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
151 ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
152 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
155 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA |
156 S3C_AC97_GLBCTRL_PCMINTM_DMA | S3C_AC97_GLBCTRL_MICINTM_DMA;
157 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
160 static irqreturn_t s3c2443_ac97_irq(int irq, void *dev_id)
165 status = readl(s3c24xx_ac97.regs + S3C_AC97_GLBSTAT) & codec_ready;
168 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
169 ac_glbctrl &= ~S3C_AC97_GLBCTRL_CODECREADYIE;
170 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
171 complete(&ac97_completion);
176 struct snd_ac97_bus_ops soc_ac97_ops = {
177 .read = s3c2443_ac97_read,
178 .write = s3c2443_ac97_write,
179 .warm_reset = s3c2443_ac97_warm_reset,
180 .reset = s3c2443_ac97_cold_reset,
183 static struct s3c2410_dma_client s3c2443_dma_client_out = {
184 .name = "AC97 PCM Stereo out"
187 static struct s3c2410_dma_client s3c2443_dma_client_in = {
188 .name = "AC97 PCM Stereo in"
191 static struct s3c2410_dma_client s3c2443_dma_client_micin = {
192 .name = "AC97 Mic Mono in"
195 static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_out = {
196 .client = &s3c2443_dma_client_out,
197 .channel = DMACH_PCM_OUT,
198 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
202 static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_in = {
203 .client = &s3c2443_dma_client_in,
204 .channel = DMACH_PCM_IN,
205 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
209 static struct s3c24xx_pcm_dma_params s3c2443_ac97_mic_mono_in = {
210 .client = &s3c2443_dma_client_micin,
211 .channel = DMACH_MIC_IN,
212 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_MIC_DATA,
216 static int s3c2443_ac97_probe(struct platform_device *pdev)
221 s3c24xx_ac97.regs = ioremap(S3C2440_PA_AC97, 0x100);
222 if (s3c24xx_ac97.regs == NULL)
225 s3c24xx_ac97.ac97_clk = clk_get(&pdev->dev, "ac97");
226 if (s3c24xx_ac97.ac97_clk == NULL) {
227 printk(KERN_ERR "s3c2443-ac97 failed to get ac97_clock\n");
228 iounmap(s3c24xx_ac97.regs);
231 clk_enable(s3c24xx_ac97.ac97_clk);
233 s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2443_GPE0_AC_nRESET);
234 s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2443_GPE1_AC_SYNC);
235 s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2443_GPE2_AC_BITCLK);
236 s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2443_GPE3_AC_SDI);
237 s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2443_GPE4_AC_SDO);
239 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
240 ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
241 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
245 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
248 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
249 ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
250 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
253 ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
254 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
256 ret = request_irq(IRQ_S3C2443_AC97, s3c2443_ac97_irq,
257 IRQF_DISABLED, "AC97", NULL);
259 printk(KERN_ERR "s3c24xx-ac97: interrupt request failed.\n");
260 clk_disable(s3c24xx_ac97.ac97_clk);
261 clk_put(s3c24xx_ac97.ac97_clk);
262 iounmap(s3c24xx_ac97.regs);
267 static void s3c2443_ac97_remove(struct platform_device *pdev)
269 free_irq(IRQ_S3C2443_AC97, NULL);
270 clk_disable(s3c24xx_ac97.ac97_clk);
271 clk_put(s3c24xx_ac97.ac97_clk);
272 iounmap(s3c24xx_ac97.regs);
275 static int s3c2443_ac97_hw_params(struct snd_pcm_substream *substream,
276 struct snd_pcm_hw_params *params)
278 struct snd_soc_pcm_runtime *rtd = substream->private_data;
279 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
281 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
282 cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_out;
284 cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_in;
289 static int s3c2443_ac97_trigger(struct snd_pcm_substream *substream, int cmd)
293 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
295 case SNDRV_PCM_TRIGGER_START:
296 case SNDRV_PCM_TRIGGER_RESUME:
297 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
298 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
299 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
301 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
303 case SNDRV_PCM_TRIGGER_STOP:
304 case SNDRV_PCM_TRIGGER_SUSPEND:
305 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
306 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
307 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
309 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
312 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
317 static int s3c2443_ac97_hw_mic_params(struct snd_pcm_substream *substream,
318 struct snd_pcm_hw_params *params)
320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
326 cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
331 static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
336 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
338 case SNDRV_PCM_TRIGGER_START:
339 case SNDRV_PCM_TRIGGER_RESUME:
340 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
341 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
343 case SNDRV_PCM_TRIGGER_STOP:
344 case SNDRV_PCM_TRIGGER_SUSPEND:
345 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
346 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
348 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
353 #define s3c2443_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
354 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
355 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
357 struct snd_soc_cpu_dai s3c2443_ac97_dai[] = {
359 .name = "s3c2443-ac97",
361 .type = SND_SOC_DAI_AC97,
362 .probe = s3c2443_ac97_probe,
363 .remove = s3c2443_ac97_remove,
365 .stream_name = "AC97 Playback",
368 .rates = s3c2443_AC97_RATES,
369 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
371 .stream_name = "AC97 Capture",
374 .rates = s3c2443_AC97_RATES,
375 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
377 .hw_params = s3c2443_ac97_hw_params,
378 .trigger = s3c2443_ac97_trigger},
381 .name = "pxa2xx-ac97-mic",
383 .type = SND_SOC_DAI_AC97,
385 .stream_name = "AC97 Mic Capture",
388 .rates = s3c2443_AC97_RATES,
389 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
391 .hw_params = s3c2443_ac97_hw_mic_params,
392 .trigger = s3c2443_ac97_mic_trigger,},
396 EXPORT_SYMBOL_GPL(s3c2443_ac97_dai);
397 EXPORT_SYMBOL_GPL(soc_ac97_ops);
399 MODULE_AUTHOR("Graeme Gregory");
400 MODULE_DESCRIPTION("AC97 driver for the Samsung s3c2443 chip");
401 MODULE_LICENSE("GPL");