Merge branch 'topic/omap' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[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
JN
33#include <sound/soc.h>
34
2e74796a
JN
35#include "omap-pcm.h"
36
27615a97
TL
37#ifdef CONFIG_ARCH_OMAP1
38#define pcm_omap1510() cpu_is_omap1510()
39#else
40#define pcm_omap1510() 0
41#endif
42
2e74796a
JN
43static const struct snd_pcm_hardware omap_pcm_hardware = {
44 .info = SNDRV_PCM_INFO_MMAP |
45 SNDRV_PCM_INFO_MMAP_VALID |
46 SNDRV_PCM_INFO_INTERLEAVED |
47 SNDRV_PCM_INFO_PAUSE |
b4173824
PU
48 SNDRV_PCM_INFO_RESUME |
49 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
e17dd32f
MLC
50 .formats = SNDRV_PCM_FMTBIT_S16_LE |
51 SNDRV_PCM_FMTBIT_S32_LE,
2e74796a
JN
52 .period_bytes_min = 32,
53 .period_bytes_max = 64 * 1024,
54 .periods_min = 2,
55 .periods_max = 255,
56 .buffer_bytes_max = 128 * 1024,
57};
58
946cc36a 59static int omap_pcm_get_dma_buswidth(int num_bits)
2e74796a 60{
946cc36a 61 int buswidth;
2e74796a 62
946cc36a
PU
63 switch (num_bits) {
64 case 16:
65 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
66 break;
67 case 32:
68 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
69 break;
70 default:
71 buswidth = -EINVAL;
72 break;
73 }
74 return buswidth;
2e74796a
JN
75}
76
946cc36a 77
2e74796a
JN
78/* this may get called several times by oss emulation */
79static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params *params)
81{
82 struct snd_pcm_runtime *runtime = substream->runtime;
83 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5f712b2b 84 struct omap_pcm_dma_data *dma_data;
946cc36a
PU
85 struct dma_slave_config config;
86 struct dma_chan *chan;
2e74796a
JN
87 int err = 0;
88
f0fba2ad 89 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
5f712b2b 90
1b4246a1
JS
91 /* return if this is a bufferless transfer e.g.
92 * codec <--> BT codec or GSM modem -- lg FIXME */
2e74796a 93 if (!dma_data)
1b4246a1 94 return 0;
2e74796a
JN
95
96 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
97 runtime->dma_bytes = params_buffer_bytes(params);
98
946cc36a
PU
99 chan = snd_dmaengine_pcm_get_chan(substream);
100 if (!chan)
101 return -EINVAL;
2e74796a 102
946cc36a
PU
103 /* fills in addr_width and direction */
104 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
105 if (err)
106 return err;
2e74796a 107
946cc36a
PU
108 /* Override the *_dma addr_width if requested by the DAI driver */
109 if (dma_data->data_type) {
110 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
2e74796a 111
946cc36a
PU
112 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
113 config.dst_addr_width = buswidth;
114 else
115 config.src_addr_width = buswidth;
116 }
2e74796a 117
946cc36a
PU
118 config.src_addr = dma_data->port_addr;
119 config.dst_addr = dma_data->port_addr;
120 config.src_maxburst = dma_data->packet_size;
121 config.dst_maxburst = dma_data->packet_size;
2e74796a 122
946cc36a 123 return dmaengine_slave_config(chan, &config);
2e74796a
JN
124}
125
946cc36a 126static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
2e74796a 127{
946cc36a 128 snd_pcm_set_runtime_buffer(substream, NULL);
2e74796a
JN
129 return 0;
130}
131
2e74796a
JN
132static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
133{
2e74796a
JN
134 snd_pcm_uframes_t offset;
135
27615a97 136 if (pcm_omap1510())
946cc36a
PU
137 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
138 else
139 offset = snd_dmaengine_pcm_pointer(substream);
2e74796a
JN
140
141 return offset;
142}
143
144static int omap_pcm_open(struct snd_pcm_substream *substream)
145{
946cc36a
PU
146 struct snd_soc_pcm_runtime *rtd = substream->private_data;
147 struct omap_pcm_dma_data *dma_data;
2e74796a
JN
148
149 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
150
946cc36a 151 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
a92b5317
PU
152
153 return snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
154 &dma_data->dma_req);
2e74796a
JN
155}
156
2e74796a
JN
157static int omap_pcm_mmap(struct snd_pcm_substream *substream,
158 struct vm_area_struct *vma)
159{
160 struct snd_pcm_runtime *runtime = substream->runtime;
161
162 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
163 runtime->dma_area,
164 runtime->dma_addr,
165 runtime->dma_bytes);
166}
167
b2a19d02 168static struct snd_pcm_ops omap_pcm_ops = {
2e74796a 169 .open = omap_pcm_open,
340af748 170 .close = snd_dmaengine_pcm_close,
2e74796a
JN
171 .ioctl = snd_pcm_lib_ioctl,
172 .hw_params = omap_pcm_hw_params,
173 .hw_free = omap_pcm_hw_free,
abe99370 174 .trigger = snd_dmaengine_pcm_trigger,
2e74796a
JN
175 .pointer = omap_pcm_pointer,
176 .mmap = omap_pcm_mmap,
177};
178
a152ff24 179static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
2e74796a
JN
180
181static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
182 int stream)
183{
184 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
185 struct snd_dma_buffer *buf = &substream->dma_buffer;
186 size_t size = omap_pcm_hardware.buffer_bytes_max;
187
188 buf->dev.type = SNDRV_DMA_TYPE_DEV;
189 buf->dev.dev = pcm->card->dev;
190 buf->private_data = NULL;
191 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
192 &buf->addr, GFP_KERNEL);
193 if (!buf->area)
194 return -ENOMEM;
195
196 buf->bytes = size;
197 return 0;
198}
199
200static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
201{
202 struct snd_pcm_substream *substream;
203 struct snd_dma_buffer *buf;
204 int stream;
205
206 for (stream = 0; stream < 2; stream++) {
207 substream = pcm->streams[stream].substream;
208 if (!substream)
209 continue;
210
211 buf = &substream->dma_buffer;
212 if (!buf->area)
213 continue;
214
215 dma_free_writecombine(pcm->card->dev, buf->bytes,
216 buf->area, buf->addr);
217 buf->area = NULL;
218 }
219}
220
552d1ef6 221static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
2e74796a 222{
552d1ef6 223 struct snd_card *card = rtd->card->snd_card;
552d1ef6 224 struct snd_pcm *pcm = rtd->pcm;
2e74796a
JN
225 int ret = 0;
226
227 if (!card->dev->dma_mask)
228 card->dev->dma_mask = &omap_pcm_dmamask;
229 if (!card->dev->coherent_dma_mask)
a152ff24 230 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
2e74796a 231
25e9e756 232 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
2e74796a
JN
233 ret = omap_pcm_preallocate_dma_buffer(pcm,
234 SNDRV_PCM_STREAM_PLAYBACK);
235 if (ret)
236 goto out;
237 }
238
25e9e756 239 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
2e74796a
JN
240 ret = omap_pcm_preallocate_dma_buffer(pcm,
241 SNDRV_PCM_STREAM_CAPTURE);
242 if (ret)
243 goto out;
244 }
245
246out:
fad9365b
OM
247 /* free preallocated buffers in case of error */
248 if (ret)
249 omap_pcm_free_dma_buffers(pcm);
250
2e74796a
JN
251 return ret;
252}
253
f0fba2ad
LG
254static struct snd_soc_platform_driver omap_soc_platform = {
255 .ops = &omap_pcm_ops,
2e74796a
JN
256 .pcm_new = omap_pcm_new,
257 .pcm_free = omap_pcm_free_dma_buffers,
258};
2e74796a 259
7ff60006 260static int omap_pcm_probe(struct platform_device *pdev)
f0fba2ad
LG
261{
262 return snd_soc_register_platform(&pdev->dev,
263 &omap_soc_platform);
264}
265
7ff60006 266static int omap_pcm_remove(struct platform_device *pdev)
f0fba2ad
LG
267{
268 snd_soc_unregister_platform(&pdev->dev);
269 return 0;
270}
271
272static struct platform_driver omap_pcm_driver = {
273 .driver = {
274 .name = "omap-pcm-audio",
275 .owner = THIS_MODULE,
276 },
277
278 .probe = omap_pcm_probe,
7ff60006 279 .remove = omap_pcm_remove,
f0fba2ad
LG
280};
281
beda5bf5 282module_platform_driver(omap_pcm_driver);
958e792c 283
7ec41ee5 284MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
2e74796a
JN
285MODULE_DESCRIPTION("OMAP PCM DMA module");
286MODULE_LICENSE("GPL");
5e70b7fc 287MODULE_ALIAS("platform:omap-pcm-audio");
This page took 0.246701 seconds and 5 git commands to generate.