1 /* sound/soc/samsung/ac97.c
3 * ALSA SoC Audio Layer - S3C AC97 Controller driver
4 * Evolved from s3c2443-ac97.c
6 * Copyright (c) 2010 Samsung Electronics Co. Ltd
7 * Author: Jaswinder Singh <jassi.brar@samsung.com>
8 * Credits: Graeme Gregory, Sean Choi
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.
16 #include <linux/delay.h>
17 #include <linux/clk.h>
19 #include <sound/soc.h>
22 #include <plat/regs-ac97.h>
23 #include <plat/audio.h>
27 #define AC_CMD_ADDR(x) (x << 16)
28 #define AC_CMD_DATA(x) (x & 0xffff)
30 #define S3C_AC97_DAI_PCM 0
31 #define S3C_AC97_DAI_MIC 1
33 struct s3c_ac97_info
{
37 struct completion done
;
39 static struct s3c_ac97_info s3c_ac97
;
41 static struct s3c2410_dma_client s3c_dma_client_out
= {
45 static struct s3c2410_dma_client s3c_dma_client_in
= {
49 static struct s3c2410_dma_client s3c_dma_client_micin
= {
53 static struct s3c_dma_params s3c_ac97_pcm_out
= {
54 .client
= &s3c_dma_client_out
,
58 static struct s3c_dma_params s3c_ac97_pcm_in
= {
59 .client
= &s3c_dma_client_in
,
63 static struct s3c_dma_params s3c_ac97_mic_in
= {
64 .client
= &s3c_dma_client_micin
,
68 static void s3c_ac97_activate(struct snd_ac97
*ac97
)
72 stat
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBSTAT
) & 0x7;
73 if (stat
== S3C_AC97_GLBSTAT_MAINSTATE_ACTIVE
)
74 return; /* Return if already active */
76 INIT_COMPLETION(s3c_ac97
.done
);
78 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
79 ac_glbctrl
= S3C_AC97_GLBCTRL_ACLINKON
;
80 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
83 ac_glbctrl
|= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE
;
84 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
87 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
88 ac_glbctrl
|= S3C_AC97_GLBCTRL_CODECREADYIE
;
89 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
91 if (!wait_for_completion_timeout(&s3c_ac97
.done
, HZ
))
92 pr_err("AC97: Unable to activate!");
95 static unsigned short s3c_ac97_read(struct snd_ac97
*ac97
,
98 u32 ac_glbctrl
, ac_codec_cmd
;
101 mutex_lock(&s3c_ac97
.lock
);
103 s3c_ac97_activate(ac97
);
105 INIT_COMPLETION(s3c_ac97
.done
);
107 ac_codec_cmd
= readl(s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
108 ac_codec_cmd
= S3C_AC97_CODEC_CMD_READ
| AC_CMD_ADDR(reg
);
109 writel(ac_codec_cmd
, s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
113 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
114 ac_glbctrl
|= S3C_AC97_GLBCTRL_CODECREADYIE
;
115 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
117 if (!wait_for_completion_timeout(&s3c_ac97
.done
, HZ
))
118 pr_err("AC97: Unable to read!");
120 stat
= readl(s3c_ac97
.regs
+ S3C_AC97_STAT
);
121 addr
= (stat
>> 16) & 0x7f;
122 data
= (stat
& 0xffff);
125 pr_err("ac97: req addr = %02x, rep addr = %02x\n",
128 mutex_unlock(&s3c_ac97
.lock
);
130 return (unsigned short)data
;
133 static void s3c_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
136 u32 ac_glbctrl
, ac_codec_cmd
;
138 mutex_lock(&s3c_ac97
.lock
);
140 s3c_ac97_activate(ac97
);
142 INIT_COMPLETION(s3c_ac97
.done
);
144 ac_codec_cmd
= readl(s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
145 ac_codec_cmd
= AC_CMD_ADDR(reg
) | AC_CMD_DATA(val
);
146 writel(ac_codec_cmd
, s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
150 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
151 ac_glbctrl
|= S3C_AC97_GLBCTRL_CODECREADYIE
;
152 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
154 if (!wait_for_completion_timeout(&s3c_ac97
.done
, HZ
))
155 pr_err("AC97: Unable to write!");
157 ac_codec_cmd
= readl(s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
158 ac_codec_cmd
|= S3C_AC97_CODEC_CMD_READ
;
159 writel(ac_codec_cmd
, s3c_ac97
.regs
+ S3C_AC97_CODEC_CMD
);
161 mutex_unlock(&s3c_ac97
.lock
);
164 static void s3c_ac97_cold_reset(struct snd_ac97
*ac97
)
166 pr_debug("AC97: Cold reset\n");
167 writel(S3C_AC97_GLBCTRL_COLDRESET
,
168 s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
171 writel(0, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
175 static void s3c_ac97_warm_reset(struct snd_ac97
*ac97
)
179 stat
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBSTAT
) & 0x7;
180 if (stat
== S3C_AC97_GLBSTAT_MAINSTATE_ACTIVE
)
181 return; /* Return if already active */
183 pr_debug("AC97: Warm reset\n");
185 writel(S3C_AC97_GLBCTRL_WARMRESET
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
188 writel(0, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
191 s3c_ac97_activate(ac97
);
194 static irqreturn_t
s3c_ac97_irq(int irq
, void *dev_id
)
196 u32 ac_glbctrl
, ac_glbstat
;
198 ac_glbstat
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBSTAT
);
200 if (ac_glbstat
& S3C_AC97_GLBSTAT_CODECREADY
) {
202 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
203 ac_glbctrl
&= ~S3C_AC97_GLBCTRL_CODECREADYIE
;
204 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
206 complete(&s3c_ac97
.done
);
209 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
210 ac_glbctrl
|= (1<<30); /* Clear interrupt */
211 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
216 struct snd_ac97_bus_ops soc_ac97_ops
= {
217 .read
= s3c_ac97_read
,
218 .write
= s3c_ac97_write
,
219 .warm_reset
= s3c_ac97_warm_reset
,
220 .reset
= s3c_ac97_cold_reset
,
222 EXPORT_SYMBOL_GPL(soc_ac97_ops
);
224 static int s3c_ac97_hw_params(struct snd_pcm_substream
*substream
,
225 struct snd_pcm_hw_params
*params
,
226 struct snd_soc_dai
*dai
)
228 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
229 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
230 struct s3c_dma_params
*dma_data
;
232 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
233 dma_data
= &s3c_ac97_pcm_out
;
235 dma_data
= &s3c_ac97_pcm_in
;
237 snd_soc_dai_set_dma_data(cpu_dai
, substream
, dma_data
);
242 static int s3c_ac97_trigger(struct snd_pcm_substream
*substream
, int cmd
,
243 struct snd_soc_dai
*dai
)
246 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
247 struct s3c_dma_params
*dma_data
=
248 snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
250 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
251 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
252 ac_glbctrl
&= ~S3C_AC97_GLBCTRL_PCMINTM_MASK
;
254 ac_glbctrl
&= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK
;
257 case SNDRV_PCM_TRIGGER_START
:
258 case SNDRV_PCM_TRIGGER_RESUME
:
259 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
260 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
261 ac_glbctrl
|= S3C_AC97_GLBCTRL_PCMINTM_DMA
;
263 ac_glbctrl
|= S3C_AC97_GLBCTRL_PCMOUTTM_DMA
;
266 case SNDRV_PCM_TRIGGER_STOP
:
267 case SNDRV_PCM_TRIGGER_SUSPEND
:
268 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
272 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
274 s3c2410_dma_ctrl(dma_data
->channel
, S3C2410_DMAOP_STARTED
);
279 static int s3c_ac97_hw_mic_params(struct snd_pcm_substream
*substream
,
280 struct snd_pcm_hw_params
*params
,
281 struct snd_soc_dai
*dai
)
283 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
284 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
286 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
289 snd_soc_dai_set_dma_data(cpu_dai
, substream
, &s3c_ac97_mic_in
);
294 static int s3c_ac97_mic_trigger(struct snd_pcm_substream
*substream
,
295 int cmd
, struct snd_soc_dai
*dai
)
298 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
299 struct s3c_dma_params
*dma_data
=
300 snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
302 ac_glbctrl
= readl(s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
303 ac_glbctrl
&= ~S3C_AC97_GLBCTRL_MICINTM_MASK
;
306 case SNDRV_PCM_TRIGGER_START
:
307 case SNDRV_PCM_TRIGGER_RESUME
:
308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
309 ac_glbctrl
|= S3C_AC97_GLBCTRL_MICINTM_DMA
;
312 case SNDRV_PCM_TRIGGER_STOP
:
313 case SNDRV_PCM_TRIGGER_SUSPEND
:
314 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
318 writel(ac_glbctrl
, s3c_ac97
.regs
+ S3C_AC97_GLBCTRL
);
320 s3c2410_dma_ctrl(dma_data
->channel
, S3C2410_DMAOP_STARTED
);
325 static struct snd_soc_dai_ops s3c_ac97_dai_ops
= {
326 .hw_params
= s3c_ac97_hw_params
,
327 .trigger
= s3c_ac97_trigger
,
330 static struct snd_soc_dai_ops s3c_ac97_mic_dai_ops
= {
331 .hw_params
= s3c_ac97_hw_mic_params
,
332 .trigger
= s3c_ac97_mic_trigger
,
335 static struct snd_soc_dai_driver s3c_ac97_dai
[] = {
336 [S3C_AC97_DAI_PCM
] = {
337 .name
= "samsung-ac97",
340 .stream_name
= "AC97 Playback",
343 .rates
= SNDRV_PCM_RATE_8000_48000
,
344 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
346 .stream_name
= "AC97 Capture",
349 .rates
= SNDRV_PCM_RATE_8000_48000
,
350 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
351 .ops
= &s3c_ac97_dai_ops
,
353 [S3C_AC97_DAI_MIC
] = {
354 .name
= "samsung-ac97-mic",
357 .stream_name
= "AC97 Mic Capture",
360 .rates
= SNDRV_PCM_RATE_8000_48000
,
361 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
362 .ops
= &s3c_ac97_mic_dai_ops
,
366 static __devinit
int s3c_ac97_probe(struct platform_device
*pdev
)
368 struct resource
*mem_res
, *dmatx_res
, *dmarx_res
, *dmamic_res
, *irq_res
;
369 struct s3c_audio_pdata
*ac97_pdata
;
372 ac97_pdata
= pdev
->dev
.platform_data
;
373 if (!ac97_pdata
|| !ac97_pdata
->cfg_gpio
) {
374 dev_err(&pdev
->dev
, "cfg_gpio callback not provided!\n");
378 /* Check for availability of necessary resource */
379 dmatx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
381 dev_err(&pdev
->dev
, "Unable to get AC97-TX dma resource\n");
385 dmarx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
387 dev_err(&pdev
->dev
, "Unable to get AC97-RX dma resource\n");
391 dmamic_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 2);
393 dev_err(&pdev
->dev
, "Unable to get AC97-MIC dma resource\n");
397 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
399 dev_err(&pdev
->dev
, "Unable to get register resource\n");
403 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
405 dev_err(&pdev
->dev
, "AC97 IRQ not provided!\n");
409 if (!request_mem_region(mem_res
->start
,
410 resource_size(mem_res
), "ac97")) {
411 dev_err(&pdev
->dev
, "Unable to request register region\n");
415 s3c_ac97_pcm_out
.channel
= dmatx_res
->start
;
416 s3c_ac97_pcm_out
.dma_addr
= mem_res
->start
+ S3C_AC97_PCM_DATA
;
417 s3c_ac97_pcm_in
.channel
= dmarx_res
->start
;
418 s3c_ac97_pcm_in
.dma_addr
= mem_res
->start
+ S3C_AC97_PCM_DATA
;
419 s3c_ac97_mic_in
.channel
= dmamic_res
->start
;
420 s3c_ac97_mic_in
.dma_addr
= mem_res
->start
+ S3C_AC97_MIC_DATA
;
422 init_completion(&s3c_ac97
.done
);
423 mutex_init(&s3c_ac97
.lock
);
425 s3c_ac97
.regs
= ioremap(mem_res
->start
, resource_size(mem_res
));
426 if (s3c_ac97
.regs
== NULL
) {
427 dev_err(&pdev
->dev
, "Unable to ioremap register region\n");
432 s3c_ac97
.ac97_clk
= clk_get(&pdev
->dev
, "ac97");
433 if (IS_ERR(s3c_ac97
.ac97_clk
)) {
434 dev_err(&pdev
->dev
, "ac97 failed to get ac97_clock\n");
438 clk_enable(s3c_ac97
.ac97_clk
);
440 if (ac97_pdata
->cfg_gpio(pdev
)) {
441 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
446 ret
= request_irq(irq_res
->start
, s3c_ac97_irq
,
447 IRQF_DISABLED
, "AC97", NULL
);
449 dev_err(&pdev
->dev
, "ac97: interrupt request failed.\n");
453 ret
= snd_soc_register_dais(&pdev
->dev
, s3c_ac97_dai
,
454 ARRAY_SIZE(s3c_ac97_dai
));
461 free_irq(irq_res
->start
, NULL
);
464 clk_disable(s3c_ac97
.ac97_clk
);
465 clk_put(s3c_ac97
.ac97_clk
);
467 iounmap(s3c_ac97
.regs
);
469 release_mem_region(mem_res
->start
, resource_size(mem_res
));
474 static __devexit
int s3c_ac97_remove(struct platform_device
*pdev
)
476 struct resource
*mem_res
, *irq_res
;
478 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(s3c_ac97_dai
));
480 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
482 free_irq(irq_res
->start
, NULL
);
484 clk_disable(s3c_ac97
.ac97_clk
);
485 clk_put(s3c_ac97
.ac97_clk
);
487 iounmap(s3c_ac97
.regs
);
489 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
491 release_mem_region(mem_res
->start
, resource_size(mem_res
));
496 static struct platform_driver s3c_ac97_driver
= {
497 .probe
= s3c_ac97_probe
,
498 .remove
= s3c_ac97_remove
,
500 .name
= "samsung-ac97",
501 .owner
= THIS_MODULE
,
505 static int __init
s3c_ac97_init(void)
507 return platform_driver_register(&s3c_ac97_driver
);
509 module_init(s3c_ac97_init
);
511 static void __exit
s3c_ac97_exit(void)
513 platform_driver_unregister(&s3c_ac97_driver
);
515 module_exit(s3c_ac97_exit
);
517 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
518 MODULE_DESCRIPTION("AC97 driver for the Samsung SoC");
519 MODULE_LICENSE("GPL");
520 MODULE_ALIAS("platform:samsung-ac97");