blob: 3f03d5ddfacda0b3392c0fe3b0fd487332d33052 [file] [log] [blame]
Graeme Gregory050f05e2007-05-14 11:02:51 +02001/*
2 * s3c2443-ac97.c -- ALSA Soc Audio Layer
3 *
4 * (c) 2007 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
6 *
7 * Copyright (C) 2005, Sean Choi <sh428.choi@samsung.com>
8 * All rights reserved.
9 *
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.
Graeme Gregory050f05e2007-05-14 11:02:51 +020013 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/interrupt.h>
Mark Brownccfdd6c2008-04-30 17:19:07 +020019#include <linux/io.h>
Graeme Gregory050f05e2007-05-14 11:02:51 +020020#include <linux/wait.h>
21#include <linux/delay.h>
Ben Dooksec976d62009-05-13 22:52:24 +010022#include <linux/gpio.h>
Graeme Gregory050f05e2007-05-14 11:02:51 +020023#include <linux/clk.h>
24
Graeme Gregory050f05e2007-05-14 11:02:51 +020025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/ac97_codec.h>
28#include <sound/initval.h>
29#include <sound/soc.h>
30
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/hardware.h>
Ben Dooksf74c95c2008-10-30 10:14:36 +000032#include <plat/regs-ac97.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/regs-gpio.h>
34#include <mach/regs-clock.h>
Ben Dooks899e6cf2009-03-04 00:49:28 +000035#include <plat/audio.h>
Graeme Gregory050f05e2007-05-14 11:02:51 +020036#include <asm/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/dma.h>
Graeme Gregory050f05e2007-05-14 11:02:51 +020038
39#include "s3c24xx-pcm.h"
40#include "s3c24xx-ac97.h"
41
42struct s3c24xx_ac97_info {
43 void __iomem *regs;
44 struct clk *ac97_clk;
45};
46static struct s3c24xx_ac97_info s3c24xx_ac97;
47
Mark Brownccfdd6c2008-04-30 17:19:07 +020048static DECLARE_COMPLETION(ac97_completion);
Graeme Gregory050f05e2007-05-14 11:02:51 +020049static u32 codec_ready;
50static DECLARE_MUTEX(ac97_mutex);
51
52static unsigned short s3c2443_ac97_read(struct snd_ac97 *ac97,
53 unsigned short reg)
54{
55 u32 ac_glbctrl;
56 u32 ac_codec_cmd;
57 u32 stat, addr, data;
58
59 down(&ac97_mutex);
60
61 codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
62 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
63 ac_codec_cmd = S3C_AC97_CODEC_CMD_READ | AC_CMD_ADDR(reg);
64 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
65
66 udelay(50);
67
68 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
69 ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
70 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
71
72 wait_for_completion(&ac97_completion);
73
74 stat = readl(s3c24xx_ac97.regs + S3C_AC97_STAT);
75 addr = (stat >> 16) & 0x7f;
76 data = (stat & 0xffff);
77
78 if (addr != reg)
79 printk(KERN_ERR "s3c24xx-ac97: req addr = %02x,"
80 " rep addr = %02x\n", reg, addr);
81
82 up(&ac97_mutex);
83
84 return (unsigned short)data;
85}
86
87static void s3c2443_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
88 unsigned short val)
89{
90 u32 ac_glbctrl;
91 u32 ac_codec_cmd;
92
93 down(&ac97_mutex);
94
95 codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
96 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
97 ac_codec_cmd = AC_CMD_ADDR(reg) | AC_CMD_DATA(val);
98 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
99
100 udelay(50);
101
102 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
103 ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
104 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
105
106 wait_for_completion(&ac97_completion);
107
108 ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
109 ac_codec_cmd |= S3C_AC97_CODEC_CMD_READ;
110 writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
111
112 up(&ac97_mutex);
113
114}
115
116static void s3c2443_ac97_warm_reset(struct snd_ac97 *ac97)
117{
118 u32 ac_glbctrl;
119
120 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
121 ac_glbctrl = S3C_AC97_GLBCTRL_WARMRESET;
122 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
123 msleep(1);
124
125 ac_glbctrl = 0;
126 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
127 msleep(1);
128}
129
130static void s3c2443_ac97_cold_reset(struct snd_ac97 *ac97)
131{
132 u32 ac_glbctrl;
133
134 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
135 ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
136 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
137 msleep(1);
138
139 ac_glbctrl = 0;
140 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
141 msleep(1);
142
143 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
144 ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
145 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
146 msleep(1);
147
148 ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
149 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
150 msleep(1);
151
152 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA |
153 S3C_AC97_GLBCTRL_PCMINTM_DMA | S3C_AC97_GLBCTRL_MICINTM_DMA;
154 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
155}
156
157static irqreturn_t s3c2443_ac97_irq(int irq, void *dev_id)
158{
159 int status;
160 u32 ac_glbctrl;
161
162 status = readl(s3c24xx_ac97.regs + S3C_AC97_GLBSTAT) & codec_ready;
163
164 if (status) {
165 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
166 ac_glbctrl &= ~S3C_AC97_GLBCTRL_CODECREADYIE;
167 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
168 complete(&ac97_completion);
169 }
170 return IRQ_HANDLED;
171}
172
173struct snd_ac97_bus_ops soc_ac97_ops = {
174 .read = s3c2443_ac97_read,
175 .write = s3c2443_ac97_write,
176 .warm_reset = s3c2443_ac97_warm_reset,
177 .reset = s3c2443_ac97_cold_reset,
178};
179
180static struct s3c2410_dma_client s3c2443_dma_client_out = {
181 .name = "AC97 PCM Stereo out"
182};
183
184static struct s3c2410_dma_client s3c2443_dma_client_in = {
185 .name = "AC97 PCM Stereo in"
186};
187
188static struct s3c2410_dma_client s3c2443_dma_client_micin = {
189 .name = "AC97 Mic Mono in"
190};
191
192static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_out = {
193 .client = &s3c2443_dma_client_out,
194 .channel = DMACH_PCM_OUT,
195 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
196 .dma_size = 4,
197};
198
199static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_in = {
200 .client = &s3c2443_dma_client_in,
201 .channel = DMACH_PCM_IN,
202 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
203 .dma_size = 4,
204};
205
206static struct s3c24xx_pcm_dma_params s3c2443_ac97_mic_mono_in = {
207 .client = &s3c2443_dma_client_micin,
208 .channel = DMACH_MIC_IN,
209 .dma_addr = S3C2440_PA_AC97 + S3C_AC97_MIC_DATA,
210 .dma_size = 4,
211};
212
Mark Brownbdb92872008-06-11 13:47:10 +0100213static int s3c2443_ac97_probe(struct platform_device *pdev,
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100214 struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200215{
216 int ret;
217 u32 ac_glbctrl;
218
219 s3c24xx_ac97.regs = ioremap(S3C2440_PA_AC97, 0x100);
220 if (s3c24xx_ac97.regs == NULL)
221 return -ENXIO;
222
223 s3c24xx_ac97.ac97_clk = clk_get(&pdev->dev, "ac97");
224 if (s3c24xx_ac97.ac97_clk == NULL) {
225 printk(KERN_ERR "s3c2443-ac97 failed to get ac97_clock\n");
226 iounmap(s3c24xx_ac97.regs);
227 return -ENODEV;
228 }
229 clk_enable(s3c24xx_ac97.ac97_clk);
230
231 s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2443_GPE0_AC_nRESET);
232 s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2443_GPE1_AC_SYNC);
233 s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2443_GPE2_AC_BITCLK);
234 s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2443_GPE3_AC_SDI);
235 s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2443_GPE4_AC_SDO);
236
237 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
238 ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
239 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
240 msleep(1);
241
242 ac_glbctrl = 0;
243 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
244 msleep(1);
245
246 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
247 ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
248 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
249 msleep(1);
250
251 ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
252 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
253
Ivan Kuten040956f2007-10-26 14:53:47 +0200254 ret = request_irq(IRQ_S3C244x_AC97, s3c2443_ac97_irq,
Graeme Gregory050f05e2007-05-14 11:02:51 +0200255 IRQF_DISABLED, "AC97", NULL);
256 if (ret < 0) {
257 printk(KERN_ERR "s3c24xx-ac97: interrupt request failed.\n");
258 clk_disable(s3c24xx_ac97.ac97_clk);
259 clk_put(s3c24xx_ac97.ac97_clk);
260 iounmap(s3c24xx_ac97.regs);
261 }
262 return ret;
263}
264
Mark Brownbdb92872008-06-11 13:47:10 +0100265static void s3c2443_ac97_remove(struct platform_device *pdev,
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100266 struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200267{
Ivan Kuten040956f2007-10-26 14:53:47 +0200268 free_irq(IRQ_S3C244x_AC97, NULL);
Graeme Gregory050f05e2007-05-14 11:02:51 +0200269 clk_disable(s3c24xx_ac97.ac97_clk);
270 clk_put(s3c24xx_ac97.ac97_clk);
271 iounmap(s3c24xx_ac97.regs);
272}
273
274static int s3c2443_ac97_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000275 struct snd_pcm_hw_params *params,
276 struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200277{
278 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100279 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Graeme Gregory050f05e2007-05-14 11:02:51 +0200280
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;
285
286 return 0;
287}
288
Takashi Iwai682d5872008-12-01 20:03:54 +0100289static int s3c2443_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
290 struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200291{
292 u32 ac_glbctrl;
293
294 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
Mark Brownccfdd6c2008-04-30 17:19:07 +0200295 switch (cmd) {
Graeme Gregory050f05e2007-05-14 11:02:51 +0200296 case SNDRV_PCM_TRIGGER_START:
297 case SNDRV_PCM_TRIGGER_RESUME:
298 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
299 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
300 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
301 else
302 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
303 break;
304 case SNDRV_PCM_TRIGGER_STOP:
305 case SNDRV_PCM_TRIGGER_SUSPEND:
306 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
307 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
308 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
309 else
310 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
311 break;
312 }
313 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
314
315 return 0;
316}
317
318static int s3c2443_ac97_hw_mic_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000319 struct snd_pcm_hw_params *params,
320 struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200321{
322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100323 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Graeme Gregory050f05e2007-05-14 11:02:51 +0200324
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
326 return -ENODEV;
327 else
328 cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
329
330 return 0;
331}
332
333static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000334 int cmd, struct snd_soc_dai *dai)
Graeme Gregory050f05e2007-05-14 11:02:51 +0200335{
336 u32 ac_glbctrl;
337
338 ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
Mark Brownccfdd6c2008-04-30 17:19:07 +0200339 switch (cmd) {
Graeme Gregory050f05e2007-05-14 11:02:51 +0200340 case SNDRV_PCM_TRIGGER_START:
341 case SNDRV_PCM_TRIGGER_RESUME:
342 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
343 ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
344 break;
345 case SNDRV_PCM_TRIGGER_STOP:
346 case SNDRV_PCM_TRIGGER_SUSPEND:
347 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
348 ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
349 }
350 writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
351
352 return 0;
353}
354
355#define s3c2443_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
356 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
357 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
358
Eric Miao6335d052009-03-03 09:41:00 +0800359static struct snd_soc_dai_ops s3c2443_ac97_dai_ops = {
360 .hw_params = s3c2443_ac97_hw_params,
361 .trigger = s3c2443_ac97_trigger,
362};
363
Mark Brown65ec1cd2009-03-11 16:51:31 +0000364static struct snd_soc_dai_ops s3c2443_ac97_mic_dai_ops = {
365 .hw_params = s3c2443_ac97_hw_mic_params,
366 .trigger = s3c2443_ac97_mic_trigger,
367};
368
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100369struct snd_soc_dai s3c2443_ac97_dai[] = {
Graeme Gregory050f05e2007-05-14 11:02:51 +0200370{
371 .name = "s3c2443-ac97",
372 .id = 0,
Mark Brown3ba9e102008-11-24 18:01:05 +0000373 .ac97_control = 1,
Graeme Gregory050f05e2007-05-14 11:02:51 +0200374 .probe = s3c2443_ac97_probe,
375 .remove = s3c2443_ac97_remove,
376 .playback = {
377 .stream_name = "AC97 Playback",
378 .channels_min = 2,
379 .channels_max = 2,
380 .rates = s3c2443_AC97_RATES,
381 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
382 .capture = {
383 .stream_name = "AC97 Capture",
384 .channels_min = 2,
385 .channels_max = 2,
386 .rates = s3c2443_AC97_RATES,
387 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
Eric Miao6335d052009-03-03 09:41:00 +0800388 .ops = &s3c2443_ac97_dai_ops,
Graeme Gregory050f05e2007-05-14 11:02:51 +0200389},
390{
391 .name = "pxa2xx-ac97-mic",
392 .id = 1,
Mark Brown3ba9e102008-11-24 18:01:05 +0000393 .ac97_control = 1,
Graeme Gregory050f05e2007-05-14 11:02:51 +0200394 .capture = {
395 .stream_name = "AC97 Mic Capture",
396 .channels_min = 1,
397 .channels_max = 1,
398 .rates = s3c2443_AC97_RATES,
399 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
Mark Brown65ec1cd2009-03-11 16:51:31 +0000400 .ops = &s3c2443_ac97_mic_dai_ops,
Graeme Gregory050f05e2007-05-14 11:02:51 +0200401},
402};
Graeme Gregory050f05e2007-05-14 11:02:51 +0200403EXPORT_SYMBOL_GPL(s3c2443_ac97_dai);
404EXPORT_SYMBOL_GPL(soc_ac97_ops);
405
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100406static int __init s3c2443_ac97_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000407{
Takashi Iwai6de45d52008-12-11 10:28:18 +0100408 return snd_soc_register_dais(s3c2443_ac97_dai,
409 ARRAY_SIZE(s3c2443_ac97_dai));
Mark Brown3f4b7832008-12-03 19:26:35 +0000410}
411module_init(s3c2443_ac97_init);
412
413static void __exit s3c2443_ac97_exit(void)
414{
Takashi Iwai6de45d52008-12-11 10:28:18 +0100415 snd_soc_unregister_dais(s3c2443_ac97_dai,
416 ARRAY_SIZE(s3c2443_ac97_dai));
Mark Brown3f4b7832008-12-03 19:26:35 +0000417}
418module_exit(s3c2443_ac97_exit);
419
420
Graeme Gregory050f05e2007-05-14 11:02:51 +0200421MODULE_AUTHOR("Graeme Gregory");
422MODULE_DESCRIPTION("AC97 driver for the Samsung s3c2443 chip");
423MODULE_LICENSE("GPL");