[MIPS] IP27: shutdown_bridge_irq: don't free irq.
[pv_ops_mirror.git] / sound / soc / s3c24xx / s3c2443-ac97.c
blob75acf7ef552809a12ae71e566b5b5525d276602e
1 /*
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>
8 * All rights reserved.
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.
14 * Revision history
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>
34 #include <asm/io.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>
39 #include <asm/dma.h>
40 #include <asm/arch/dma.h>
42 #include "s3c24xx-pcm.h"
43 #include "s3c24xx-ac97.h"
45 struct s3c24xx_ac97_info {
46 void __iomem *regs;
47 struct clk *ac97_clk;
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,
56 unsigned short reg)
58 u32 ac_glbctrl;
59 u32 ac_codec_cmd;
60 u32 stat, addr, data;
62 down(&ac97_mutex);
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);
69 udelay(50);
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);
81 if (addr != reg)
82 printk(KERN_ERR "s3c24xx-ac97: req addr = %02x,"
83 " rep addr = %02x\n", reg, addr);
85 up(&ac97_mutex);
87 return (unsigned short)data;
90 static void s3c2443_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
91 unsigned short val)
93 u32 ac_glbctrl;
94 u32 ac_codec_cmd;
96 down(&ac97_mutex);
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);
103 udelay(50);
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);
115 up(&ac97_mutex);
119 static void s3c2443_ac97_warm_reset(struct snd_ac97 *ac97)
121 u32 ac_glbctrl;
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);
126 msleep(1);
128 ac_glbctrl = 0;
129 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
130 msleep(1);
133 static void s3c2443_ac97_cold_reset(struct snd_ac97 *ac97)
135 u32 ac_glbctrl;
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);
140 msleep(1);
142 ac_glbctrl = 0;
143 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
144 msleep(1);
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);
149 msleep(1);
151 ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
152 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
153 msleep(1);
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)
162 int status;
163 u32 ac_glbctrl;
165 status = readl(s3c24xx_ac97.regs + S3C_AC97_GLBSTAT) & codec_ready;
167 if (status) {
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);
173 return IRQ_HANDLED;
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,
199 .dma_size = 4,
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,
206 .dma_size = 4,
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,
213 .dma_size = 4,
216 static int s3c2443_ac97_probe(struct platform_device *pdev)
218 int ret;
219 u32 ac_glbctrl;
221 s3c24xx_ac97.regs = ioremap(S3C2440_PA_AC97, 0x100);
222 if (s3c24xx_ac97.regs == NULL)
223 return -ENXIO;
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);
229 return -ENODEV;
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);
242 msleep(1);
244 ac_glbctrl = 0;
245 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
246 msleep(1);
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);
251 msleep(1);
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);
258 if (ret < 0) {
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);
264 return ret;
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;
283 else
284 cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_in;
286 return 0;
289 static int s3c2443_ac97_trigger(struct snd_pcm_substream *substream, int cmd)
291 u32 ac_glbctrl;
293 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
294 switch(cmd) {
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;
300 else
301 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
302 break;
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;
308 else
309 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
310 break;
312 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
314 return 0;
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)
324 return -ENODEV;
325 else
326 cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
328 return 0;
331 static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
332 int cmd)
334 u32 ac_glbctrl;
336 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
337 switch(cmd) {
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;
342 break;
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);
350 return 0;
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",
360 .id = 0,
361 .type = SND_SOC_DAI_AC97,
362 .probe = s3c2443_ac97_probe,
363 .remove = s3c2443_ac97_remove,
364 .playback = {
365 .stream_name = "AC97 Playback",
366 .channels_min = 2,
367 .channels_max = 2,
368 .rates = s3c2443_AC97_RATES,
369 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
370 .capture = {
371 .stream_name = "AC97 Capture",
372 .channels_min = 2,
373 .channels_max = 2,
374 .rates = s3c2443_AC97_RATES,
375 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
376 .ops = {
377 .hw_params = s3c2443_ac97_hw_params,
378 .trigger = s3c2443_ac97_trigger},
381 .name = "pxa2xx-ac97-mic",
382 .id = 1,
383 .type = SND_SOC_DAI_AC97,
384 .capture = {
385 .stream_name = "AC97 Mic Capture",
386 .channels_min = 1,
387 .channels_max = 1,
388 .rates = s3c2443_AC97_RATES,
389 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
390 .ops = {
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");