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>
52 #include <asm/arch/omap-alsa.h>
53 #include "omap-alsa-dma.h"
55 MODULE_AUTHOR("Mika Laitio, Daniel Petrini, David Cohen, Anderson Briglia - INdT");
56 MODULE_LICENSE("GPL");
57 MODULE_DESCRIPTION("OMAP driver for ALSA");
58 MODULE_ALIAS("omap_alsa_mcbsp.1");
60 static char *id = NULL;
61 static struct snd_card_omap_codec *alsa_codec = NULL;
62 static struct omap_alsa_codec_config *alsa_codec_config = NULL;
65 * HW interface start and stop helper functions
67 static int audio_ifc_start(void)
69 omap_mcbsp_start(AUDIO_MCBSP);
73 static int audio_ifc_stop(void)
75 omap_mcbsp_stop(AUDIO_MCBSP);
79 static void omap_alsa_audio_init(struct snd_card_omap_codec *omap_alsa)
82 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].id = "Alsa omap out";
83 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id =
84 SNDRV_PCM_STREAM_PLAYBACK;
85 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev =
87 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].hw_start =
89 omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK].hw_stop =
92 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].id = "Alsa omap in";
93 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].stream_id =
94 SNDRV_PCM_STREAM_CAPTURE;
95 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev =
97 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].hw_start =
99 omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE].hw_stop =
105 * Depends on omap-alsa-dma.c functions and (omap) dma.c
108 static int audio_dma_request(struct audio_stream *s,
109 void (*callback) (void *))
114 err = omap_request_alsa_sound_dma(s->dma_dev, s->id, s, &s->lch);
116 printk(KERN_ERR "Unable to grab audio dma 0x%x\n", s->dma_dev);
120 static int audio_dma_free(struct audio_stream *s)
125 err = omap_free_alsa_sound_dma(s, &s->lch);
127 printk(KERN_ERR "Unable to free audio dma channels!\n");
132 * This function should calculate the current position of the dma in the
133 * buffer. It will help alsa middle layer to continue update the buffer.
134 * Its correctness is crucial for good functioning.
136 static u_int audio_get_dma_pos(struct audio_stream *s)
138 snd_pcm_substream_t *substream = s->stream;
139 snd_pcm_runtime_t *runtime = substream->runtime;
145 /* this must be called w/ interrupts locked as requested in dma.c */
146 spin_lock_irqsave(&s->dma_lock, flags);
148 /* For the current period let's see where we are */
149 count = omap_get_dma_src_addr_counter(s->lch[s->dma_q_head]);
151 spin_unlock_irqrestore(&s->dma_lock, flags);
153 /* Now, the position related to the end of that period */
154 offset = bytes_to_frames(runtime, s->offset) - bytes_to_frames(runtime, count);
156 if (offset >= runtime->buffer_size)
163 * this stops the dma and clears the dma ptrs
165 static void audio_stop_dma(struct audio_stream *s)
170 spin_lock_irqsave(&s->dma_lock, flags);
175 /* this stops the dma channel and clears the buffer ptrs */
176 omap_stop_alsa_sound_dma(s);
178 omap_clear_alsa_sound_dma(s);
180 spin_unlock_irqrestore(&s->dma_lock, flags);
184 * Main dma routine, requests dma according where you are in main alsa buffer
186 static void audio_process_dma(struct audio_stream *s)
188 snd_pcm_substream_t *substream = s->stream;
189 snd_pcm_runtime_t *runtime;
190 unsigned int dma_size;
193 #ifdef CONFIG_MACH_OMAP_H6300
198 runtime = substream->runtime;
200 dma_size = frames_to_bytes(runtime, runtime->period_size);
201 offset = dma_size * s->period;
202 snd_assert(dma_size <= DMA_BUF_SIZE,);
203 #ifdef CONFIG_MACH_OMAP_H6300
204 spin_lock_irqsave(&s->dma_lock, flags);
205 omap_stop_alsa_sound_dma(s);
206 spin_unlock_irqrestore(&s->dma_lock, flags);
208 ret = omap_start_alsa_sound_dma(s,
209 (dma_addr_t) runtime->dma_area +
213 "audio_process_dma: cannot queue DMA buffer (%i)\n",
219 s->period %= runtime->periods;
226 * This is called when dma IRQ occurs at the end of each transmited block
228 void callback_omap_alsa_sound_dma(void *data)
230 struct audio_stream *s = data;
234 * If we are getting a callback for an active stream then we inform
235 * the PCM middle layer we've finished a period
238 snd_pcm_period_elapsed(s->stream);
240 spin_lock(&s->dma_lock);
244 audio_process_dma(s);
245 spin_unlock(&s->dma_lock);
250 * PCM settings and callbacks
252 static int snd_omap_alsa_trigger(snd_pcm_substream_t * substream, int cmd)
254 struct snd_card_omap_codec *chip =
255 snd_pcm_substream_chip(substream);
256 int stream_id = substream->pstr->stream;
257 struct audio_stream *s = &chip->s[stream_id];
261 /* note local interrupts are already disabled in the midlevel code */
262 spin_lock(&s->dma_lock);
264 case SNDRV_PCM_TRIGGER_START:
265 /* requested stream startup */
267 audio_process_dma(s);
269 case SNDRV_PCM_TRIGGER_STOP:
270 /* requested stream shutdown */
277 spin_unlock(&s->dma_lock);
282 static int snd_omap_alsa_prepare(snd_pcm_substream_t * substream)
284 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
285 snd_pcm_runtime_t *runtime = substream->runtime;
286 struct audio_stream *s = &chip->s[substream->pstr->stream];
289 /* set requested samplerate */
290 alsa_codec_config->codec_set_samplerate(runtime->rate);
291 chip->samplerate = runtime->rate;
299 static snd_pcm_uframes_t snd_omap_alsa_pointer(snd_pcm_substream_t *substream)
301 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
304 return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
307 static int snd_card_omap_alsa_open(snd_pcm_substream_t * substream)
309 struct snd_card_omap_codec *chip =
310 snd_pcm_substream_chip(substream);
311 snd_pcm_runtime_t *runtime = substream->runtime;
312 int stream_id = substream->pstr->stream;
316 chip->s[stream_id].stream = substream;
317 alsa_codec_config->codec_clock_on();
318 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
319 runtime->hw = *(alsa_codec_config->snd_omap_alsa_playback);
321 runtime->hw = *(alsa_codec_config->snd_omap_alsa_capture);
323 if ((err = snd_pcm_hw_constraint_integer(runtime,
324 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
327 if ((err = snd_pcm_hw_constraint_list(runtime,
329 SNDRV_PCM_HW_PARAM_RATE,
330 alsa_codec_config->hw_constraints_rates)) < 0)
336 static int snd_card_omap_alsa_close(snd_pcm_substream_t * substream)
338 struct snd_card_omap_codec *chip = snd_pcm_substream_chip(substream);
341 alsa_codec_config->codec_clock_off();
342 chip->s[substream->pstr->stream].stream = NULL;
347 /* HW params & free */
348 static int snd_omap_alsa_hw_params(snd_pcm_substream_t * substream,
349 snd_pcm_hw_params_t * hw_params)
351 return snd_pcm_lib_malloc_pages(substream,
352 params_buffer_bytes(hw_params));
355 static int snd_omap_alsa_hw_free(snd_pcm_substream_t * substream)
357 return snd_pcm_lib_free_pages(substream);
361 static snd_pcm_ops_t snd_card_omap_alsa_playback_ops = {
362 .open = snd_card_omap_alsa_open,
363 .close = snd_card_omap_alsa_close,
364 .ioctl = snd_pcm_lib_ioctl,
365 .hw_params = snd_omap_alsa_hw_params,
366 .hw_free = snd_omap_alsa_hw_free,
367 .prepare = snd_omap_alsa_prepare,
368 .trigger = snd_omap_alsa_trigger,
369 .pointer = snd_omap_alsa_pointer,
372 static snd_pcm_ops_t snd_card_omap_alsa_capture_ops = {
373 .open = snd_card_omap_alsa_open,
374 .close = snd_card_omap_alsa_close,
375 .ioctl = snd_pcm_lib_ioctl,
376 .hw_params = snd_omap_alsa_hw_params,
377 .hw_free = snd_omap_alsa_hw_free,
378 .prepare = snd_omap_alsa_prepare,
379 .trigger = snd_omap_alsa_trigger,
380 .pointer = snd_omap_alsa_pointer,
384 * Alsa init and exit section
386 * Inits pcm alsa structures, allocate the alsa buffer, suspend, resume
388 static int __init snd_card_omap_alsa_pcm(struct snd_card_omap_codec *omap_alsa,
395 if ((err = snd_pcm_new(omap_alsa->card, "OMAP PCM", device, 1, 1, &pcm)) < 0)
398 /* sets up initial buffer with continuous allocation */
399 snd_pcm_lib_preallocate_pages_for_all(pcm,
400 SNDRV_DMA_TYPE_CONTINUOUS,
401 snd_dma_continuous_data
403 128 * 1024, 128 * 1024);
405 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
406 &snd_card_omap_alsa_playback_ops);
407 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
408 &snd_card_omap_alsa_capture_ops);
409 pcm->private_data = omap_alsa;
411 strcpy(pcm->name, "omap alsa pcm");
413 omap_alsa_audio_init(omap_alsa);
415 /* setup DMA controller */
416 audio_dma_request(&omap_alsa->s[SNDRV_PCM_STREAM_PLAYBACK],
417 callback_omap_alsa_sound_dma);
418 audio_dma_request(&omap_alsa->s[SNDRV_PCM_STREAM_CAPTURE],
419 callback_omap_alsa_sound_dma);
421 omap_alsa->pcm = pcm;
429 * Driver suspend/resume - calls alsa functions. Some hints from aaci.c
431 int snd_omap_alsa_suspend(struct platform_device *pdev, pm_message_t state)
433 struct snd_card_omap_codec *chip;
434 snd_card_t *card = platform_get_drvdata(pdev);
436 if (card->power_state != SNDRV_CTL_POWER_D3hot) {
437 chip = card->private_data;
438 if (chip->card->power_state != SNDRV_CTL_POWER_D3hot) {
439 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
440 snd_pcm_suspend_all(chip->pcm);
441 /* Mutes and turn clock off */
442 alsa_codec_config->codec_clock_off();
443 snd_omap_suspend_mixer();
449 int snd_omap_alsa_resume(struct platform_device *pdev)
451 struct snd_card_omap_codec *chip;
452 snd_card_t *card = platform_get_drvdata(pdev);
454 if (card->power_state != SNDRV_CTL_POWER_D0) {
455 chip = card->private_data;
456 if (chip->card->power_state != SNDRV_CTL_POWER_D0) {
457 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
458 alsa_codec_config->codec_clock_on();
459 snd_omap_resume_mixer();
465 #endif /* CONFIG_PM */
467 void snd_omap_alsa_free(snd_card_t * card)
469 struct snd_card_omap_codec *chip = card->private_data;
473 * Turn off codec after it is done.
474 * Can't do it immediately, since it may still have
477 schedule_timeout_interruptible(2);
479 omap_mcbsp_stop(AUDIO_MCBSP);
480 omap_mcbsp_free(AUDIO_MCBSP);
482 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
483 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
486 /* module init & exit */
489 * Inits alsa soudcard structure.
490 * Called by the probe method in codec after function pointers has been set.
492 int snd_omap_alsa_post_probe(struct platform_device *pdev, struct omap_alsa_codec_config *config)
499 alsa_codec_config = config;
501 alsa_codec_config->codec_clock_setup();
502 alsa_codec_config->codec_clock_on();
504 omap_mcbsp_request(AUDIO_MCBSP);
505 omap_mcbsp_stop(AUDIO_MCBSP);
506 omap_mcbsp_config(AUDIO_MCBSP, alsa_codec_config->mcbsp_regs_alsa);
507 omap_mcbsp_start(AUDIO_MCBSP);
509 if (alsa_codec_config && alsa_codec_config->codec_configure_dev)
510 alsa_codec_config->codec_configure_dev();
512 alsa_codec_config->codec_clock_off();
514 /* register the soundcard */
515 card = snd_card_new(-1, id, THIS_MODULE, sizeof(alsa_codec));
519 alsa_codec = kcalloc(1, sizeof(*alsa_codec), GFP_KERNEL);
520 if (alsa_codec == NULL)
523 card->private_data = (void *)alsa_codec;
524 card->private_free = snd_omap_alsa_free;
526 alsa_codec->card = card;
527 def_rate = alsa_codec_config->get_default_samplerate();
528 alsa_codec->samplerate = def_rate;
530 spin_lock_init(&alsa_codec->s[0].dma_lock);
531 spin_lock_init(&alsa_codec->s[1].dma_lock);
534 if ((err = snd_omap_mixer(alsa_codec)) < 0)
538 if ((err = snd_card_omap_alsa_pcm(alsa_codec, 0)) < 0)
541 strcpy(card->driver, "OMAP_ALSA");
542 strcpy(card->shortname, alsa_codec_config->name);
543 sprintf(card->longname, alsa_codec_config->name);
545 snd_omap_init_mixer();
546 snd_card_set_dev(card, &pdev->dev);
548 if ((err = snd_card_register(card)) == 0) {
549 printk(KERN_INFO "audio support initialized\n");
550 platform_set_drvdata(pdev, card);
559 omap_mcbsp_stop(AUDIO_MCBSP);
560 omap_mcbsp_free(AUDIO_MCBSP);
565 int snd_omap_alsa_remove(struct platform_device *pdev)
567 snd_card_t *card = platform_get_drvdata(pdev);
568 struct snd_card_omap_codec *chip = card->private_data;
573 card->private_data = NULL;
576 platform_set_drvdata(pdev, NULL);