2 * sound/arm/omap-alsa.c
6 * Copyright (C) 2005 Instituto Nokia de Tecnologia - INdT - Manaus Brazil
7 * Written by Daniel Petrini, David Cohen, Anderson Briglia
8 * {daniel.petrini, david.cohen, anderson.briglia}@indt.org.br
10 * Copyright (C) 2006 Mika Laitio <lamikr@cc.jyu.fi>
12 * Based on sa11xx-uda1341.c,
13 * Copyright (C) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
37 * 2005-07-29 INdT Kernel Team - Alsa driver for omap osk. Creation of new
40 * 2005-12-18 Dirk Behme - Added L/R Channel Interchange fix as proposed
45 #include <linux/platform_device.h>
49 #include <sound/driver.h>
50 #include <sound/core.h>
51 #include <sound/pcm.h>
53 #include <asm/arch/omap-alsa.h>
54 #include "omap-alsa-dma.h"
56 MODULE_AUTHOR("Mika Laitio, Daniel Petrini, David Cohen, Anderson Briglia - INdT");
57 MODULE_LICENSE("GPL");
58 MODULE_DESCRIPTION("OMAP driver for ALSA");
59 MODULE_ALIAS("omap_alsa_mcbsp.1");
61 static char *id = NULL;
62 static struct snd_card_omap_codec *alsa_codec = NULL;
63 static struct omap_alsa_codec_config *alsa_codec_config = NULL;
66 * HW interface start and stop helper functions
68 static int audio_ifc_start(void)
70 omap_mcbsp_start(AUDIO_MCBSP);
74 static int audio_ifc_stop(void)
76 omap_mcbsp_stop(AUDIO_MCBSP);
80 static void omap_alsa_audio_init(struct snd_card_omap_codec *omap_alsa)
83 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].id = "Alsa omap out";
84 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id =
85 SNDRV_PCM_STREAM_PLAYBACK;
86 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev =
88 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].hw_start =
90 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].hw_stop =
93 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].id = "Alsa omap in";
94 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].stream_id =
95 SNDRV_PCM_STREAM_CAPTURE;
96 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev =
98 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].hw_start =
100 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].hw_stop =
106 * Depends on omap-alsa-dma.c functions and (omap) dma.c
109 static int audio_dma_request(struct audio_stream *s,
110 void (*callback) (void *))
115 err = omap_request_alsa_sound_dma(s->dma_dev, s->id, s, &s->lch);
117 printk(KERN_ERR "Unable to grab audio dma 0x%x\n", s->dma_dev);
121 static int audio_dma_free(struct audio_stream *s)
126 err = omap_free_alsa_sound_dma(s, &s->lch);
128 printk(KERN_ERR "Unable to free audio dma channels!\n");
133 * This function should calculate the current position of the dma in the
134 * buffer. It will help alsa middle layer to continue update the buffer.
135 * Its correctness is crucial for good functioning.
137 static u_int audio_get_dma_pos(struct audio_stream *s)
139 struct snd_pcm_substream *substream = s->stream;
140 struct snd_pcm_runtime *runtime = substream->runtime;
146 /* this must be called w/ interrupts locked as requested in dma.c */
147 spin_lock_irqsave(&s->dma_lock, flags);
149 /* For the current period let's see where we are */
150 count = omap_get_dma_src_addr_counter(s->lch[s->dma_q_head]);
152 spin_unlock_irqrestore(&s->dma_lock, flags);
154 /* Now, the position related to the end of that period */
155 offset = bytes_to_frames(runtime, s->offset) - bytes_to_frames(runtime, count);
157 if (offset >= runtime->buffer_size)
164 * this stops the dma and clears the dma ptrs
166 static void audio_stop_dma(struct audio_stream *s)
171 spin_lock_irqsave(&s->dma_lock, flags);
176 /* this stops the dma channel and clears the buffer ptrs */
177 omap_stop_alsa_sound_dma(s);
179 omap_clear_alsa_sound_dma(s);
181 spin_unlock_irqrestore(&s->dma_lock, flags);
185 * Main dma routine, requests dma according where you are in main alsa buffer
187 static void audio_process_dma(struct audio_stream *s)
189 struct snd_pcm_substream *substream = s->stream;
190 struct snd_pcm_runtime *runtime;
191 unsigned int dma_size;
196 runtime = substream->runtime;
198 dma_size = frames_to_bytes(runtime, runtime->period_size);
199 offset = dma_size * s->period;
200 snd_assert(dma_size <= DMA_BUF_SIZE,);
202 * On omap1510 based devices, we need to call the stop_dma
203 * before calling the start_dma or we will not receive the
204 * irq from DMA after the first transfered/played buffer.
205 * (invocation of callback_omap_alsa_sound_dma() method).
207 if (cpu_is_omap1510()) {
208 omap_stop_alsa_sound_dma(s);
210 ret = omap_start_alsa_sound_dma(s,
211 (dma_addr_t)runtime->dma_area + offset,
215 "audio_process_dma: cannot queue DMA buffer (%i)\n",
221 s->period %= runtime->periods;
228 * This is called when dma IRQ occurs at the end of each transmited block
230 void callback_omap_alsa_sound_dma(void *data)
232 struct audio_stream *s = data;
236 * If we are getting a callback for an active stream then we inform
237 * the PCM middle layer we've finished a period
240 snd_pcm_period_elapsed(s->stream);
242 spin_lock(&s->dma_lock);
246 audio_process_dma(s);
247 spin_unlock(&s->dma_lock);
252 * PCM settings and callbacks
254 static int snd_omap_alsa_trigger(struct snd_pcm_substream * substream, int cmd)
256 struct snd_card_omap_codec *chip =
257 snd_pcm_substream_chip(substream);
258 int stream_id = substream->pstr->stream;
259 struct audio_stream *s = &chip->s[stream_id];
263 /* note local interrupts are already disabled in the midlevel code */
264 spin_lock(&s->dma_lock);
266 case SNDRV_PCM_TRIGGER_START:
267 /* requested stream startup */
269 audio_process_dma(s);
271 case SNDRV_PCM_TRIGGER_STOP:
272 /* requested stream shutdown */
279 spin_unlock(&s->dma_lock);
284 static int snd_omap_alsa_prepare(struct snd_pcm_substream * substream)
286 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
287 struct snd_pcm_runtime *runtime = substream->runtime;
288 struct audio_stream *s = &chip->s[substream->pstr->stream];
291 /* set requested samplerate */
292 alsa_codec_config->codec_set_samplerate(runtime->rate);
293 chip->samplerate = runtime->rate;
301 static snd_pcm_uframes_t snd_omap_alsa_pointer(struct snd_pcm_substream *substream)
303 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
306 return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
309 static int snd_card_omap_alsa_open(struct snd_pcm_substream * substream)
311 struct snd_card_omap_codec *chip =
312 snd_pcm_substream_chip(substream);
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 int stream_id = substream->pstr->stream;
318 chip->s[stream_id].stream = substream;
319 alsa_codec_config->codec_clock_on();
320 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
321 runtime->hw = *(alsa_codec_config->snd_omap_alsa_playback);
323 runtime->hw = *(alsa_codec_config->snd_omap_alsa_capture);
325 if ((err = snd_pcm_hw_constraint_integer(runtime,
326 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
329 if ((err = snd_pcm_hw_constraint_list(runtime,
331 SNDRV_PCM_HW_PARAM_RATE,
332 alsa_codec_config->hw_constraints_rates)) < 0)
338 static int snd_card_omap_alsa_close(struct snd_pcm_substream * substream)
340 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
343 alsa_codec_config->codec_clock_off();
344 chip->s[substream->pstr->stream].stream = NULL;
349 /* HW params & free */
350 static int snd_omap_alsa_hw_params(struct snd_pcm_substream * substream,
351 struct snd_pcm_hw_params * hw_params)
353 return snd_pcm_lib_malloc_pages(substream,
354 params_buffer_bytes(hw_params));
357 static int snd_omap_alsa_hw_free(struct snd_pcm_substream * substream)
359 return snd_pcm_lib_free_pages(substream);
363 static struct snd_pcm_ops snd_card_omap_alsa_playback_ops = {
364 .open = snd_card_omap_alsa_open,
365 .close = snd_card_omap_alsa_close,
366 .ioctl = snd_pcm_lib_ioctl,
367 .hw_params = snd_omap_alsa_hw_params,
368 .hw_free = snd_omap_alsa_hw_free,
369 .prepare = snd_omap_alsa_prepare,
370 .trigger = snd_omap_alsa_trigger,
371 .pointer = snd_omap_alsa_pointer,
374 static struct snd_pcm_ops snd_card_omap_alsa_capture_ops = {
375 .open = snd_card_omap_alsa_open,
376 .close = snd_card_omap_alsa_close,
377 .ioctl = snd_pcm_lib_ioctl,
378 .hw_params = snd_omap_alsa_hw_params,
379 .hw_free = snd_omap_alsa_hw_free,
380 .prepare = snd_omap_alsa_prepare,
381 .trigger = snd_omap_alsa_trigger,
382 .pointer = snd_omap_alsa_pointer,
386 * Alsa init and exit section
388 * Inits pcm alsa structures, allocate the alsa buffer, suspend, resume
390 static int __init snd_card_omap_alsa_pcm(struct snd_card_omap_codec *omap_alsa,
397 if ((err = snd_pcm_new(omap_alsa->card, "OMAP PCM", device, 1, 1, &pcm)) < 0)
400 /* sets up initial buffer with continuous allocation */
401 snd_pcm_lib_preallocate_pages_for_all(pcm,
402 SNDRV_DMA_TYPE_CONTINUOUS,
403 snd_dma_continuous_data
405 128 * 1024, 128 * 1024);
407 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
408 &snd_card_omap_alsa_playback_ops);
409 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
410 &snd_card_omap_alsa_capture_ops);
411 pcm->private_data = omap_alsa;
413 strcpy(pcm->name, "omap alsa pcm");
415 omap_alsa_audio_init(omap_alsa);
417 /* setup DMA controller */
418 audio_dma_request(&omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK],
419 callback_omap_alsa_sound_dma);
420 audio_dma_request(&omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE],
421 callback_omap_alsa_sound_dma);
423 omap_alsa->pcm = pcm;
431 * Driver suspend/resume - calls alsa functions. Some hints from aaci.c
433 int snd_omap_alsa_suspend(struct platform_device *pdev, pm_message_t state)
435 struct snd_card_omap_codec *chip;
436 struct snd_card *card = platform_get_drvdata(pdev);
438 if (card->power_state != SNDRV_CTL_POWER_D3hot) {
439 chip = card->private_data;
440 if (chip->card->power_state != SNDRV_CTL_POWER_D3hot) {
441 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
442 snd_pcm_suspend_all(chip->pcm);
443 /* Mutes and turn clock off */
444 alsa_codec_config->codec_clock_off();
445 snd_omap_suspend_mixer();
451 int snd_omap_alsa_resume(struct platform_device *pdev)
453 struct snd_card_omap_codec *chip;
454 struct snd_card *card = platform_get_drvdata(pdev);
456 if (card->power_state != SNDRV_CTL_POWER_D0) {
457 chip = card->private_data;
458 if (chip->card->power_state != SNDRV_CTL_POWER_D0) {
459 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
460 alsa_codec_config->codec_clock_on();
461 snd_omap_resume_mixer();
467 #endif /* CONFIG_PM */
469 void snd_omap_alsa_free(struct snd_card * card)
471 struct snd_card_omap_codec *chip = card->private_data;
475 * Turn off codec after it is done.
476 * Can't do it immediately, since it may still have
479 schedule_timeout_interruptible(2);
481 omap_mcbsp_stop(AUDIO_MCBSP);
482 omap_mcbsp_free(AUDIO_MCBSP);
484 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
485 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
488 /* module init & exit */
491 * Inits alsa soudcard structure.
492 * Called by the probe method in codec after function pointers has been set.
494 int snd_omap_alsa_post_probe(struct platform_device *pdev, struct omap_alsa_codec_config *config)
498 struct snd_card *card;
501 alsa_codec_config = config;
503 alsa_codec_config->codec_clock_setup();
504 alsa_codec_config->codec_clock_on();
506 omap_mcbsp_request(AUDIO_MCBSP);
507 omap_mcbsp_stop(AUDIO_MCBSP);
508 omap_mcbsp_config(AUDIO_MCBSP, alsa_codec_config->mcbsp_regs_alsa);
509 omap_mcbsp_start(AUDIO_MCBSP);
511 if (alsa_codec_config && alsa_codec_config->codec_configure_dev)
512 alsa_codec_config->codec_configure_dev();
514 alsa_codec_config->codec_clock_off();
516 /* register the soundcard */
517 card = snd_card_new(-1, id, THIS_MODULE, sizeof(alsa_codec));
521 alsa_codec = kcalloc(1, sizeof(*alsa_codec), GFP_KERNEL);
522 if (alsa_codec == NULL)
525 card->private_data = (void *)alsa_codec;
526 card->private_free = snd_omap_alsa_free;
528 alsa_codec->card = card;
529 def_rate = alsa_codec_config->get_default_samplerate();
530 alsa_codec->samplerate = def_rate;
532 spin_lock_init(&alsa_codec->s[0].dma_lock);
533 spin_lock_init(&alsa_codec->s[1].dma_lock);
536 if ((err = snd_omap_mixer(alsa_codec)) < 0)
540 if ((err = snd_card_omap_alsa_pcm(alsa_codec, 0)) < 0)
543 strcpy(card->driver, "OMAP_ALSA");
544 strcpy(card->shortname, alsa_codec_config->name);
545 sprintf(card->longname, alsa_codec_config->name);
547 snd_omap_init_mixer();
548 snd_card_set_dev(card, &pdev->dev);
550 if ((err = snd_card_register(card)) == 0) {
551 printk(KERN_INFO "audio support initialized\n");
552 platform_set_drvdata(pdev, card);
561 omap_mcbsp_stop(AUDIO_MCBSP);
562 omap_mcbsp_free(AUDIO_MCBSP);
567 int snd_omap_alsa_remove(struct platform_device *pdev)
569 struct snd_card *card = platform_get_drvdata(pdev);
570 struct snd_card_omap_codec *chip = card->private_data;
575 card->private_data = NULL;
578 platform_set_drvdata(pdev, NULL);