Commit | Line | Data |
---|---|---|
6c742509 SG |
1 | /* |
2 | * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC. | |
9aaca968 | 3 | * |
6c742509 SG |
4 | * Copyright (C) 2005 SAN People |
5 | * Copyright (C) 2008 Atmel | |
6 | * | |
7 | * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com> | |
8 | * | |
9 | * Based on at91-pcm. by: | |
10 | * Frank Mandarino <fmandarino@endrelia.com> | |
11 | * Copyright 2006 Endrelia Technologies Inc. | |
12 | * | |
13 | * Based on pxa2xx-pcm.c by: | |
14 | * | |
15 | * Author: Nicolas Pitre | |
16 | * Created: Nov 30, 2004 | |
17 | * Copyright: (C) 2004 MontaVista Software, Inc. | |
9aaca968 GW |
18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
6c742509 SG |
20 | * it under the terms of the GNU General Public License as published by |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
9aaca968 | 23 | * |
6c742509 SG |
24 | * This program is distributed in the hope that it will be useful, |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License | |
30 | * along with this program; if not, write to the Free Software | |
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
9aaca968 GW |
32 | */ |
33 | ||
34 | #include <linux/module.h> | |
9aaca968 | 35 | #include <linux/dma-mapping.h> |
9aaca968 | 36 | #include <sound/pcm.h> |
9aaca968 | 37 | #include <sound/soc.h> |
6c742509 | 38 | #include "atmel-pcm.h" |
9aaca968 | 39 | |
6c742509 SG |
40 | static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
41 | int stream) | |
9aaca968 GW |
42 | { |
43 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; | |
6c742509 | 44 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
92dfa619 | 45 | size_t size = ATMEL_SSC_DMABUF_SIZE; |
6c742509 SG |
46 | |
47 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
48 | buf->dev.dev = pcm->card->dev; | |
49 | buf->private_data = NULL; | |
50 | buf->area = dma_alloc_coherent(pcm->card->dev, size, | |
92dfa619 | 51 | &buf->addr, GFP_KERNEL); |
153f5a18 | 52 | pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n", |
3a429eea | 53 | (void *)buf->area, (void *)(long)buf->addr, size); |
6c742509 SG |
54 | |
55 | if (!buf->area) | |
9aaca968 GW |
56 | return -ENOMEM; |
57 | ||
6c742509 | 58 | buf->bytes = size; |
9aaca968 GW |
59 | return 0; |
60 | } | |
9aaca968 | 61 | |
92dfa619 | 62 | int atmel_pcm_mmap(struct snd_pcm_substream *substream, |
6c742509 | 63 | struct vm_area_struct *vma) |
9aaca968 GW |
64 | { |
65 | return remap_pfn_range(vma, vma->vm_start, | |
6c742509 SG |
66 | substream->dma_buffer.addr >> PAGE_SHIFT, |
67 | vma->vm_end - vma->vm_start, vma->vm_page_prot); | |
9aaca968 | 68 | } |
92dfa619 | 69 | EXPORT_SYMBOL_GPL(atmel_pcm_mmap); |
9aaca968 | 70 | |
92dfa619 | 71 | int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd) |
9aaca968 | 72 | { |
552d1ef6 | 73 | struct snd_card *card = rtd->card->snd_card; |
552d1ef6 | 74 | struct snd_pcm *pcm = rtd->pcm; |
c9bd5e69 | 75 | int ret; |
9aaca968 | 76 | |
c9bd5e69 RK |
77 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); |
78 | if (ret) | |
79 | return ret; | |
9aaca968 | 80 | |
25e9e756 | 81 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
92dfa619 | 82 | pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n"); |
6c742509 SG |
83 | ret = atmel_pcm_preallocate_dma_buffer(pcm, |
84 | SNDRV_PCM_STREAM_PLAYBACK); | |
9aaca968 GW |
85 | if (ret) |
86 | goto out; | |
87 | } | |
88 | ||
25e9e756 | 89 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
92dfa619 | 90 | pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n"); |
6c742509 SG |
91 | ret = atmel_pcm_preallocate_dma_buffer(pcm, |
92 | SNDRV_PCM_STREAM_CAPTURE); | |
9aaca968 GW |
93 | if (ret) |
94 | goto out; | |
95 | } | |
6c742509 | 96 | out: |
9aaca968 GW |
97 | return ret; |
98 | } | |
92dfa619 | 99 | EXPORT_SYMBOL_GPL(atmel_pcm_new); |
9aaca968 | 100 | |
92dfa619 | 101 | void atmel_pcm_free(struct snd_pcm *pcm) |
9aaca968 GW |
102 | { |
103 | struct snd_pcm_substream *substream; | |
104 | struct snd_dma_buffer *buf; | |
105 | int stream; | |
106 | ||
107 | for (stream = 0; stream < 2; stream++) { | |
108 | substream = pcm->streams[stream].substream; | |
6c742509 | 109 | if (!substream) |
9aaca968 GW |
110 | continue; |
111 | ||
112 | buf = &substream->dma_buffer; | |
113 | if (!buf->area) | |
114 | continue; | |
115 | dma_free_coherent(pcm->card->dev, buf->bytes, | |
116 | buf->area, buf->addr); | |
117 | buf->area = NULL; | |
118 | } | |
119 | } | |
92dfa619 | 120 | EXPORT_SYMBOL_GPL(atmel_pcm_free); |
9aaca968 | 121 |