Merge branch 'topic/omap' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[deliverable/linux.git] / sound / soc / spear / spear_pcm.c
1 /*
2 * ALSA PCM interface for ST SPEAr Processors
3 *
4 * sound/soc/spear/spear_pcm.c
5 *
6 * Copyright (C) 2012 ST Microelectronics
7 * Rajeev Kumar<rajeev-dlh.kumar@st.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14 #include <linux/module.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/dmaengine_pcm.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/spear_dma.h>
27
28 struct snd_pcm_hardware spear_pcm_hardware = {
29 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
30 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
31 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
32 .buffer_bytes_max = 16 * 1024, /* max buffer size */
33 .period_bytes_min = 2 * 1024, /* 1 msec data minimum period size */
34 .period_bytes_max = 2 * 1024, /* maximum period size */
35 .periods_min = 1, /* min # periods */
36 .periods_max = 8, /* max # of periods */
37 .fifo_size = 0, /* fifo size in bytes */
38 };
39
40 static int spear_pcm_hw_params(struct snd_pcm_substream *substream,
41 struct snd_pcm_hw_params *params)
42 {
43 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
44
45 return 0;
46 }
47
48 static int spear_pcm_hw_free(struct snd_pcm_substream *substream)
49 {
50 snd_pcm_set_runtime_buffer(substream, NULL);
51
52 return 0;
53 }
54
55 static int spear_pcm_open(struct snd_pcm_substream *substream)
56 {
57 struct snd_soc_pcm_runtime *rtd = substream->private_data;
58
59 struct spear_dma_data *dma_data = (struct spear_dma_data *)
60 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
61 int ret;
62
63 ret = snd_soc_set_runtime_hwparams(substream, &spear_pcm_hardware);
64 if (ret)
65 return ret;
66
67 return snd_dmaengine_pcm_open(substream, dma_data->filter, dma_data)
68 }
69
70 static int spear_pcm_mmap(struct snd_pcm_substream *substream,
71 struct vm_area_struct *vma)
72 {
73 struct snd_pcm_runtime *runtime = substream->runtime;
74
75 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
76 runtime->dma_area, runtime->dma_addr,
77 runtime->dma_bytes);
78 }
79
80 static struct snd_pcm_ops spear_pcm_ops = {
81 .open = spear_pcm_open,
82 .close = snd_dmaengine_pcm_close,
83 .ioctl = snd_pcm_lib_ioctl,
84 .hw_params = spear_pcm_hw_params,
85 .hw_free = spear_pcm_hw_free,
86 .trigger = snd_dmaengine_pcm_trigger,
87 .pointer = snd_dmaengine_pcm_pointer,
88 .mmap = spear_pcm_mmap,
89 };
90
91 static int
92 spear_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
93 size_t size)
94 {
95 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
96 struct snd_dma_buffer *buf = &substream->dma_buffer;
97
98 buf->dev.type = SNDRV_DMA_TYPE_DEV;
99 buf->dev.dev = pcm->card->dev;
100 buf->private_data = NULL;
101
102 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
103 &buf->addr, GFP_KERNEL);
104 if (!buf->area)
105 return -ENOMEM;
106
107 dev_info(buf->dev.dev,
108 " preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
109 (void *)buf->area, (void *)buf->addr, size);
110
111 buf->bytes = size;
112 return 0;
113 }
114
115 static void spear_pcm_free(struct snd_pcm *pcm)
116 {
117 struct snd_pcm_substream *substream;
118 struct snd_dma_buffer *buf;
119 int stream;
120
121 for (stream = 0; stream < 2; stream++) {
122 substream = pcm->streams[stream].substream;
123 if (!substream)
124 continue;
125
126 buf = &substream->dma_buffer;
127 if (!buf || !buf->area)
128 continue;
129
130 dma_free_writecombine(pcm->card->dev, buf->bytes,
131 buf->area, buf->addr);
132 buf->area = NULL;
133 }
134 }
135
136 static u64 spear_pcm_dmamask = DMA_BIT_MASK(32);
137
138 static int spear_pcm_new(struct snd_card *card,
139 struct snd_soc_dai *dai, struct snd_pcm *pcm)
140 {
141 int ret;
142
143 if (!card->dev->dma_mask)
144 card->dev->dma_mask = &spear_pcm_dmamask;
145 if (!card->dev->coherent_dma_mask)
146 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
147
148 if (dai->driver->playback.channels_min) {
149 ret = spear_pcm_preallocate_dma_buffer(pcm,
150 SNDRV_PCM_STREAM_PLAYBACK,
151 spear_pcm_hardware.buffer_bytes_max);
152 if (ret)
153 return ret;
154 }
155
156 if (dai->driver->capture.channels_min) {
157 ret = spear_pcm_preallocate_dma_buffer(pcm,
158 SNDRV_PCM_STREAM_CAPTURE,
159 spear_pcm_hardware.buffer_bytes_max);
160 if (ret)
161 return ret;
162 }
163
164 return 0;
165 }
166
167 struct snd_soc_platform_driver spear_soc_platform = {
168 .ops = &spear_pcm_ops,
169 .pcm_new = spear_pcm_new,
170 .pcm_free = spear_pcm_free,
171 };
172
173 static int spear_soc_platform_probe(struct platform_device *pdev)
174 {
175 return snd_soc_register_platform(&pdev->dev, &spear_soc_platform);
176 }
177
178 static int spear_soc_platform_remove(struct platform_device *pdev)
179 {
180 snd_soc_unregister_platform(&pdev->dev);
181
182 return 0;
183 }
184
185 static struct platform_driver spear_pcm_driver = {
186 .driver = {
187 .name = "spear-pcm-audio",
188 .owner = THIS_MODULE,
189 },
190
191 .probe = spear_soc_platform_probe,
192 .remove = spear_soc_platform_remove,
193 };
194
195 module_platform_driver(spear_pcm_driver);
196
197 MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
198 MODULE_DESCRIPTION("SPEAr PCM DMA module");
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS("platform:spear-pcm-audio");
This page took 0.036681 seconds and 6 git commands to generate.