ASoC: omap: Kconfig: Support for omap5-uevm analog audio
[deliverable/linux.git] / sound / soc / omap / omap-pcm.c
CommitLineData
2e74796a
JN
1/*
2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
7ec41ee5 6 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
1c7687b9 7 * Peter Ujfalusi <peter.ujfalusi@ti.com>
2e74796a
JN
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/dma-mapping.h>
5a0e3ad6 26#include <linux/slab.h>
da155d5b 27#include <linux/module.h>
946cc36a 28#include <linux/omap-dma.h>
2e74796a
JN
29#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
946cc36a 32#include <sound/dmaengine_pcm.h>
2e74796a 33#include <sound/soc.h>
76a77f47 34#include <sound/omap-pcm.h>
2e74796a 35
27615a97
TL
36#ifdef CONFIG_ARCH_OMAP1
37#define pcm_omap1510() cpu_is_omap1510()
38#else
39#define pcm_omap1510() 0
40#endif
41
2e74796a
JN
42static const struct snd_pcm_hardware omap_pcm_hardware = {
43 .info = SNDRV_PCM_INFO_MMAP |
44 SNDRV_PCM_INFO_MMAP_VALID |
45 SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_PAUSE |
b4173824
PU
47 SNDRV_PCM_INFO_RESUME |
48 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
2e74796a
JN
49 .period_bytes_min = 32,
50 .period_bytes_max = 64 * 1024,
51 .periods_min = 2,
52 .periods_max = 255,
53 .buffer_bytes_max = 128 * 1024,
54};
55
2e74796a
JN
56/* this may get called several times by oss emulation */
57static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
58 struct snd_pcm_hw_params *params)
59{
60 struct snd_pcm_runtime *runtime = substream->runtime;
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 62 struct omap_pcm_dma_data *dma_data;
946cc36a
PU
63 struct dma_slave_config config;
64 struct dma_chan *chan;
2e74796a
JN
65 int err = 0;
66
f0fba2ad 67 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
5f712b2b 68
1b4246a1
JS
69 /* return if this is a bufferless transfer e.g.
70 * codec <--> BT codec or GSM modem -- lg FIXME */
2e74796a 71 if (!dma_data)
1b4246a1 72 return 0;
2e74796a
JN
73
74 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
75 runtime->dma_bytes = params_buffer_bytes(params);
76
946cc36a
PU
77 chan = snd_dmaengine_pcm_get_chan(substream);
78 if (!chan)
79 return -EINVAL;
2e74796a 80
946cc36a
PU
81 /* fills in addr_width and direction */
82 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
83 if (err)
84 return err;
2e74796a 85
09ae3aaf
LPC
86 snd_dmaengine_pcm_set_config_from_dai_data(substream,
87 snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
88 &config);
2e74796a 89
946cc36a 90 return dmaengine_slave_config(chan, &config);
2e74796a
JN
91}
92
946cc36a 93static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
2e74796a 94{
946cc36a 95 snd_pcm_set_runtime_buffer(substream, NULL);
2e74796a
JN
96 return 0;
97}
98
2e74796a
JN
99static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
100{
2e74796a
JN
101 snd_pcm_uframes_t offset;
102
27615a97 103 if (pcm_omap1510())
946cc36a
PU
104 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
105 else
106 offset = snd_dmaengine_pcm_pointer(substream);
2e74796a
JN
107
108 return offset;
109}
110
111static int omap_pcm_open(struct snd_pcm_substream *substream)
112{
946cc36a 113 struct snd_soc_pcm_runtime *rtd = substream->private_data;
09ae3aaf 114 struct snd_dmaengine_dai_dma_data *dma_data;
f6becf0b 115 int ret;
2e74796a
JN
116
117 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
118
946cc36a 119 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
a92b5317 120
f6becf0b
PU
121 /* DT boot: filter_data is the DMA name */
122 if (rtd->cpu_dai->dev->of_node) {
123 struct dma_chan *chan;
124
125 chan = dma_request_slave_channel(rtd->cpu_dai->dev,
126 dma_data->filter_data);
127 ret = snd_dmaengine_pcm_open(substream, chan);
128 } else {
129 ret = snd_dmaengine_pcm_open_request_chan(substream,
130 omap_dma_filter_fn,
131 dma_data->filter_data);
132 }
133 return ret;
2e74796a
JN
134}
135
2e74796a
JN
136static int omap_pcm_mmap(struct snd_pcm_substream *substream,
137 struct vm_area_struct *vma)
138{
139 struct snd_pcm_runtime *runtime = substream->runtime;
140
141 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
142 runtime->dma_area,
143 runtime->dma_addr,
144 runtime->dma_bytes);
145}
146
b2a19d02 147static struct snd_pcm_ops omap_pcm_ops = {
2e74796a 148 .open = omap_pcm_open,
7c1c1d4a 149 .close = snd_dmaengine_pcm_close_release_chan,
2e74796a
JN
150 .ioctl = snd_pcm_lib_ioctl,
151 .hw_params = omap_pcm_hw_params,
152 .hw_free = omap_pcm_hw_free,
abe99370 153 .trigger = snd_dmaengine_pcm_trigger,
2e74796a
JN
154 .pointer = omap_pcm_pointer,
155 .mmap = omap_pcm_mmap,
156};
157
2e74796a
JN
158static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
159 int stream)
160{
161 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
162 struct snd_dma_buffer *buf = &substream->dma_buffer;
163 size_t size = omap_pcm_hardware.buffer_bytes_max;
164
165 buf->dev.type = SNDRV_DMA_TYPE_DEV;
166 buf->dev.dev = pcm->card->dev;
167 buf->private_data = NULL;
168 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
169 &buf->addr, GFP_KERNEL);
170 if (!buf->area)
171 return -ENOMEM;
172
173 buf->bytes = size;
174 return 0;
175}
176
177static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
178{
179 struct snd_pcm_substream *substream;
180 struct snd_dma_buffer *buf;
181 int stream;
182
183 for (stream = 0; stream < 2; stream++) {
184 substream = pcm->streams[stream].substream;
185 if (!substream)
186 continue;
187
188 buf = &substream->dma_buffer;
189 if (!buf->area)
190 continue;
191
192 dma_free_writecombine(pcm->card->dev, buf->bytes,
193 buf->area, buf->addr);
194 buf->area = NULL;
195 }
196}
197
552d1ef6 198static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
2e74796a 199{
552d1ef6 200 struct snd_card *card = rtd->card->snd_card;
552d1ef6 201 struct snd_pcm *pcm = rtd->pcm;
c9bd5e69 202 int ret;
2e74796a 203
c9bd5e69
RK
204 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64));
205 if (ret)
206 return ret;
2e74796a 207
25e9e756 208 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
2e74796a
JN
209 ret = omap_pcm_preallocate_dma_buffer(pcm,
210 SNDRV_PCM_STREAM_PLAYBACK);
211 if (ret)
212 goto out;
213 }
214
25e9e756 215 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
2e74796a
JN
216 ret = omap_pcm_preallocate_dma_buffer(pcm,
217 SNDRV_PCM_STREAM_CAPTURE);
218 if (ret)
219 goto out;
220 }
221
222out:
fad9365b
OM
223 /* free preallocated buffers in case of error */
224 if (ret)
225 omap_pcm_free_dma_buffers(pcm);
226
2e74796a
JN
227 return ret;
228}
229
f0fba2ad
LG
230static struct snd_soc_platform_driver omap_soc_platform = {
231 .ops = &omap_pcm_ops,
2e74796a
JN
232 .pcm_new = omap_pcm_new,
233 .pcm_free = omap_pcm_free_dma_buffers,
234};
2e74796a 235
fe7b5868
PU
236int omap_pcm_platform_register(struct device *dev)
237{
238 return devm_snd_soc_register_platform(dev, &omap_soc_platform);
239}
240EXPORT_SYMBOL_GPL(omap_pcm_platform_register);
241
7ec41ee5 242MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
2e74796a
JN
243MODULE_DESCRIPTION("OMAP PCM DMA module");
244MODULE_LICENSE("GPL");
This page took 0.319807 seconds and 5 git commands to generate.