1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/module.h>
35#include <linux/dma-mapping.h>
36#include <sound/pcm.h>
37#include <sound/soc.h>
38#include "atmel-pcm.h"
39
40static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
41 int stream)
42{
43 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
44 struct snd_dma_buffer *buf = &substream->dma_buffer;
45 size_t size = ATMEL_SSC_DMABUF_SIZE;
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,
51 &buf->addr, GFP_KERNEL);
52 pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
53 (void *)buf->area, (void *)buf->addr, size);
54
55 if (!buf->area)
56 return -ENOMEM;
57
58 buf->bytes = size;
59 return 0;
60}
61
62int atmel_pcm_mmap(struct snd_pcm_substream *substream,
63 struct vm_area_struct *vma)
64{
65 return remap_pfn_range(vma, vma->vm_start,
66 substream->dma_buffer.addr >> PAGE_SHIFT,
67 vma->vm_end - vma->vm_start, vma->vm_page_prot);
68}
69EXPORT_SYMBOL_GPL(atmel_pcm_mmap);
70
71static u64 atmel_pcm_dmamask = DMA_BIT_MASK(32);
72
73int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
74{
75 struct snd_card *card = rtd->card->snd_card;
76 struct snd_pcm *pcm = rtd->pcm;
77 int ret = 0;
78
79 if (!card->dev->dma_mask)
80 card->dev->dma_mask = &atmel_pcm_dmamask;
81 if (!card->dev->coherent_dma_mask)
82 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
83
84 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
85 pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
86 ret = atmel_pcm_preallocate_dma_buffer(pcm,
87 SNDRV_PCM_STREAM_PLAYBACK);
88 if (ret)
89 goto out;
90 }
91
92 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
93 pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
94 ret = atmel_pcm_preallocate_dma_buffer(pcm,
95 SNDRV_PCM_STREAM_CAPTURE);
96 if (ret)
97 goto out;
98 }
99 out:
100 return ret;
101}
102EXPORT_SYMBOL_GPL(atmel_pcm_new);
103
104void atmel_pcm_free(struct snd_pcm *pcm)
105{
106 struct snd_pcm_substream *substream;
107 struct snd_dma_buffer *buf;
108 int stream;
109
110 for (stream = 0; stream < 2; stream++) {
111 substream = pcm->streams[stream].substream;
112 if (!substream)
113 continue;
114
115 buf = &substream->dma_buffer;
116 if (!buf->area)
117 continue;
118 dma_free_coherent(pcm->card->dev, buf->bytes,
119 buf->area, buf->addr);
120 buf->area = NULL;
121 }
122}
123EXPORT_SYMBOL_GPL(atmel_pcm_free);
124
125