nfsd: pass net to nfsd_init_socks()
[linux-2.6/libata-dev.git] / sound / soc / omap / omap-pcm.c
blob340874ebf9ae79cb550cbfe54e81850802448e16
1 /*
2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
7 * Peter Ujfalusi <peter.ujfalusi@ti.com>
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.
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.
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
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/omap-dma.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/dmaengine_pcm.h>
33 #include <sound/soc.h>
35 #include <plat/cpu.h>
36 #include "omap-pcm.h"
38 static const struct snd_pcm_hardware omap_pcm_hardware = {
39 .info = SNDRV_PCM_INFO_MMAP |
40 SNDRV_PCM_INFO_MMAP_VALID |
41 SNDRV_PCM_INFO_INTERLEAVED |
42 SNDRV_PCM_INFO_PAUSE |
43 SNDRV_PCM_INFO_RESUME |
44 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
45 .formats = SNDRV_PCM_FMTBIT_S16_LE |
46 SNDRV_PCM_FMTBIT_S32_LE,
47 .period_bytes_min = 32,
48 .period_bytes_max = 64 * 1024,
49 .periods_min = 2,
50 .periods_max = 255,
51 .buffer_bytes_max = 128 * 1024,
54 static int omap_pcm_get_dma_buswidth(int num_bits)
56 int buswidth;
58 switch (num_bits) {
59 case 16:
60 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
61 break;
62 case 32:
63 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
64 break;
65 default:
66 buswidth = -EINVAL;
67 break;
69 return buswidth;
73 /* this may get called several times by oss emulation */
74 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
75 struct snd_pcm_hw_params *params)
77 struct snd_pcm_runtime *runtime = substream->runtime;
78 struct snd_soc_pcm_runtime *rtd = substream->private_data;
79 struct omap_pcm_dma_data *dma_data;
80 struct dma_slave_config config;
81 struct dma_chan *chan;
82 int err = 0;
84 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
86 /* return if this is a bufferless transfer e.g.
87 * codec <--> BT codec or GSM modem -- lg FIXME */
88 if (!dma_data)
89 return 0;
91 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
92 runtime->dma_bytes = params_buffer_bytes(params);
94 chan = snd_dmaengine_pcm_get_chan(substream);
95 if (!chan)
96 return -EINVAL;
98 /* fills in addr_width and direction */
99 err = snd_hwparams_to_dma_slave_config(substream, params, &config);
100 if (err)
101 return err;
103 /* Override the *_dma addr_width if requested by the DAI driver */
104 if (dma_data->data_type) {
105 int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
108 config.dst_addr_width = buswidth;
109 else
110 config.src_addr_width = buswidth;
113 config.src_addr = dma_data->port_addr;
114 config.dst_addr = dma_data->port_addr;
115 config.src_maxburst = dma_data->packet_size;
116 config.dst_maxburst = dma_data->packet_size;
118 return dmaengine_slave_config(chan, &config);
121 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
123 snd_pcm_set_runtime_buffer(substream, NULL);
124 return 0;
127 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 struct omap_pcm_dma_data *dma_data;
131 int ret = 0;
133 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
135 switch (cmd) {
136 case SNDRV_PCM_TRIGGER_START:
137 case SNDRV_PCM_TRIGGER_RESUME:
138 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
139 /* Configure McBSP internal buffer usage */
140 if (dma_data->set_threshold)
141 dma_data->set_threshold(substream);
142 break;
144 case SNDRV_PCM_TRIGGER_STOP:
145 case SNDRV_PCM_TRIGGER_SUSPEND:
146 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
147 break;
148 default:
149 ret = -EINVAL;
152 if (ret == 0)
153 ret = snd_dmaengine_pcm_trigger(substream, cmd);
155 return ret;
158 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
160 snd_pcm_uframes_t offset;
162 if (cpu_is_omap1510())
163 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
164 else
165 offset = snd_dmaengine_pcm_pointer(substream);
167 return offset;
170 static int omap_pcm_open(struct snd_pcm_substream *substream)
172 struct snd_pcm_runtime *runtime = substream->runtime;
173 struct snd_soc_pcm_runtime *rtd = substream->private_data;
174 struct omap_pcm_dma_data *dma_data;
175 int ret;
177 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
179 /* Ensure that buffer size is a multiple of period size */
180 ret = snd_pcm_hw_constraint_integer(runtime,
181 SNDRV_PCM_HW_PARAM_PERIODS);
182 if (ret < 0)
183 return ret;
185 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
186 ret = snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
187 &dma_data->dma_req);
188 return ret;
191 static int omap_pcm_close(struct snd_pcm_substream *substream)
193 snd_dmaengine_pcm_close(substream);
194 return 0;
197 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
198 struct vm_area_struct *vma)
200 struct snd_pcm_runtime *runtime = substream->runtime;
202 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
203 runtime->dma_area,
204 runtime->dma_addr,
205 runtime->dma_bytes);
208 static struct snd_pcm_ops omap_pcm_ops = {
209 .open = omap_pcm_open,
210 .close = omap_pcm_close,
211 .ioctl = snd_pcm_lib_ioctl,
212 .hw_params = omap_pcm_hw_params,
213 .hw_free = omap_pcm_hw_free,
214 .trigger = omap_pcm_trigger,
215 .pointer = omap_pcm_pointer,
216 .mmap = omap_pcm_mmap,
219 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
221 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
222 int stream)
224 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
225 struct snd_dma_buffer *buf = &substream->dma_buffer;
226 size_t size = omap_pcm_hardware.buffer_bytes_max;
228 buf->dev.type = SNDRV_DMA_TYPE_DEV;
229 buf->dev.dev = pcm->card->dev;
230 buf->private_data = NULL;
231 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
232 &buf->addr, GFP_KERNEL);
233 if (!buf->area)
234 return -ENOMEM;
236 buf->bytes = size;
237 return 0;
240 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
242 struct snd_pcm_substream *substream;
243 struct snd_dma_buffer *buf;
244 int stream;
246 for (stream = 0; stream < 2; stream++) {
247 substream = pcm->streams[stream].substream;
248 if (!substream)
249 continue;
251 buf = &substream->dma_buffer;
252 if (!buf->area)
253 continue;
255 dma_free_writecombine(pcm->card->dev, buf->bytes,
256 buf->area, buf->addr);
257 buf->area = NULL;
261 static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
263 struct snd_card *card = rtd->card->snd_card;
264 struct snd_pcm *pcm = rtd->pcm;
265 int ret = 0;
267 if (!card->dev->dma_mask)
268 card->dev->dma_mask = &omap_pcm_dmamask;
269 if (!card->dev->coherent_dma_mask)
270 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
272 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
273 ret = omap_pcm_preallocate_dma_buffer(pcm,
274 SNDRV_PCM_STREAM_PLAYBACK);
275 if (ret)
276 goto out;
279 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
280 ret = omap_pcm_preallocate_dma_buffer(pcm,
281 SNDRV_PCM_STREAM_CAPTURE);
282 if (ret)
283 goto out;
286 out:
287 /* free preallocated buffers in case of error */
288 if (ret)
289 omap_pcm_free_dma_buffers(pcm);
291 return ret;
294 static struct snd_soc_platform_driver omap_soc_platform = {
295 .ops = &omap_pcm_ops,
296 .pcm_new = omap_pcm_new,
297 .pcm_free = omap_pcm_free_dma_buffers,
300 static __devinit int omap_pcm_probe(struct platform_device *pdev)
302 return snd_soc_register_platform(&pdev->dev,
303 &omap_soc_platform);
306 static int __devexit omap_pcm_remove(struct platform_device *pdev)
308 snd_soc_unregister_platform(&pdev->dev);
309 return 0;
312 static struct platform_driver omap_pcm_driver = {
313 .driver = {
314 .name = "omap-pcm-audio",
315 .owner = THIS_MODULE,
318 .probe = omap_pcm_probe,
319 .remove = __devexit_p(omap_pcm_remove),
322 module_platform_driver(omap_pcm_driver);
324 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
325 MODULE_DESCRIPTION("OMAP PCM DMA module");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:omap-pcm-audio");