blob: b078ed537bc3f51b708b6830518f7215562f0c31 [file] [log] [blame]
Jarkko Nikula2e747962008-04-25 13:55:19 +02001/*
2 * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Jarkko Nikulab08f7a62009-04-17 14:42:26 +03006 * Contact: Jarkko Nikula <jhnikula@gmail.com>
7 * Peter Ujfalusi <peter.ujfalusi@nokia.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02008 *
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>
26#include <sound/core.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/soc.h>
30
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/dma.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020032#include "omap-pcm.h"
33
34static const struct snd_pcm_hardware omap_pcm_hardware = {
35 .info = SNDRV_PCM_INFO_MMAP |
36 SNDRV_PCM_INFO_MMAP_VALID |
37 SNDRV_PCM_INFO_INTERLEAVED |
38 SNDRV_PCM_INFO_PAUSE |
39 SNDRV_PCM_INFO_RESUME,
40 .formats = SNDRV_PCM_FMTBIT_S16_LE,
41 .period_bytes_min = 32,
42 .period_bytes_max = 64 * 1024,
43 .periods_min = 2,
44 .periods_max = 255,
45 .buffer_bytes_max = 128 * 1024,
46};
47
48struct omap_runtime_data {
49 spinlock_t lock;
50 struct omap_pcm_dma_data *dma_data;
51 int dma_ch;
52 int period_index;
53};
54
55static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
56{
57 struct snd_pcm_substream *substream = data;
58 struct snd_pcm_runtime *runtime = substream->runtime;
59 struct omap_runtime_data *prtd = runtime->private_data;
60 unsigned long flags;
61
62 if (cpu_is_omap1510()) {
63 /*
64 * OMAP1510 doesn't support DMA chaining so have to restart
65 * the transfer after all periods are transferred
66 */
67 spin_lock_irqsave(&prtd->lock, flags);
68 if (prtd->period_index >= 0) {
69 if (++prtd->period_index == runtime->periods) {
70 prtd->period_index = 0;
71 omap_start_dma(prtd->dma_ch);
72 }
73 }
74 spin_unlock_irqrestore(&prtd->lock, flags);
75 }
76
77 snd_pcm_period_elapsed(substream);
78}
79
80/* this may get called several times by oss emulation */
81static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
82 struct snd_pcm_hw_params *params)
83{
84 struct snd_pcm_runtime *runtime = substream->runtime;
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
86 struct omap_runtime_data *prtd = runtime->private_data;
87 struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
88 int err = 0;
89
90 if (!dma_data)
91 return -ENODEV;
92
93 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
94 runtime->dma_bytes = params_buffer_bytes(params);
95
96 if (prtd->dma_data)
97 return 0;
98 prtd->dma_data = dma_data;
99 err = omap_request_dma(dma_data->dma_req, dma_data->name,
100 omap_pcm_dma_irq, substream, &prtd->dma_ch);
Roel Kluin472346d2008-12-22 17:40:45 +0100101 if (!err && !cpu_is_omap1510()) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200102 /*
103 * Link channel with itself so DMA doesn't need any
104 * reprogramming while looping the buffer
105 */
106 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
107 }
108
109 return err;
110}
111
112static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
113{
114 struct snd_pcm_runtime *runtime = substream->runtime;
115 struct omap_runtime_data *prtd = runtime->private_data;
116
117 if (prtd->dma_data == NULL)
118 return 0;
119
120 if (!cpu_is_omap1510())
121 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
122 omap_free_dma(prtd->dma_ch);
123 prtd->dma_data = NULL;
124
125 snd_pcm_set_runtime_buffer(substream, NULL);
126
127 return 0;
128}
129
130static int omap_pcm_prepare(struct snd_pcm_substream *substream)
131{
132 struct snd_pcm_runtime *runtime = substream->runtime;
133 struct omap_runtime_data *prtd = runtime->private_data;
134 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
135 struct omap_dma_channel_params dma_params;
136
137 memset(&dma_params, 0, sizeof(dma_params));
138 /*
139 * Note: Regardless of interface data formats supported by OMAP McBSP
140 * or EAC blocks, internal representation is always fixed 16-bit/sample
141 */
142 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
143 dma_params.trigger = dma_data->dma_req;
144 dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
145 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
146 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
147 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
148 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
149 dma_params.src_start = runtime->dma_addr;
150 dma_params.dst_start = dma_data->port_addr;
Arun KS9d374842008-09-30 15:35:16 +0530151 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200152 } else {
153 dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
154 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
155 dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
156 dma_params.src_start = dma_data->port_addr;
157 dma_params.dst_start = runtime->dma_addr;
Arun KS9d374842008-09-30 15:35:16 +0530158 dma_params.src_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200159 }
160 /*
161 * Set DMA transfer frame size equal to ALSA period size and frame
162 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
163 * we can transfer the whole ALSA buffer with single DMA transfer but
164 * still can get an interrupt at each period bounary
165 */
166 dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
167 dma_params.frame_count = runtime->periods;
168 omap_set_dma_params(prtd->dma_ch, &dma_params);
169
170 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
171
172 return 0;
173}
174
175static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
176{
177 struct snd_pcm_runtime *runtime = substream->runtime;
178 struct omap_runtime_data *prtd = runtime->private_data;
Eero Nurkkala21dff432009-02-02 14:20:46 +0200179 unsigned long flags;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200180 int ret = 0;
181
Eero Nurkkala21dff432009-02-02 14:20:46 +0200182 spin_lock_irqsave(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200183 switch (cmd) {
184 case SNDRV_PCM_TRIGGER_START:
185 case SNDRV_PCM_TRIGGER_RESUME:
186 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
187 prtd->period_index = 0;
188 omap_start_dma(prtd->dma_ch);
189 break;
190
191 case SNDRV_PCM_TRIGGER_STOP:
192 case SNDRV_PCM_TRIGGER_SUSPEND:
193 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
194 prtd->period_index = -1;
195 omap_stop_dma(prtd->dma_ch);
196 break;
197 default:
198 ret = -EINVAL;
199 }
Eero Nurkkala21dff432009-02-02 14:20:46 +0200200 spin_unlock_irqrestore(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200201
202 return ret;
203}
204
205static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
206{
207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct omap_runtime_data *prtd = runtime->private_data;
209 dma_addr_t ptr;
210 snd_pcm_uframes_t offset;
211
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213 ptr = omap_get_dma_src_pos(prtd->dma_ch);
214 else
215 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
216
217 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
218 if (offset >= runtime->buffer_size)
219 offset = 0;
220
221 return offset;
222}
223
224static int omap_pcm_open(struct snd_pcm_substream *substream)
225{
226 struct snd_pcm_runtime *runtime = substream->runtime;
227 struct omap_runtime_data *prtd;
228 int ret;
229
230 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
231
232 /* Ensure that buffer size is a multiple of period size */
233 ret = snd_pcm_hw_constraint_integer(runtime,
234 SNDRV_PCM_HW_PARAM_PERIODS);
235 if (ret < 0)
236 goto out;
237
Stanley Miao19b3f312008-12-19 22:08:22 +0800238 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200239 if (prtd == NULL) {
240 ret = -ENOMEM;
241 goto out;
242 }
243 spin_lock_init(&prtd->lock);
244 runtime->private_data = prtd;
245
246out:
247 return ret;
248}
249
250static int omap_pcm_close(struct snd_pcm_substream *substream)
251{
252 struct snd_pcm_runtime *runtime = substream->runtime;
253
254 kfree(runtime->private_data);
255 return 0;
256}
257
258static int omap_pcm_mmap(struct snd_pcm_substream *substream,
259 struct vm_area_struct *vma)
260{
261 struct snd_pcm_runtime *runtime = substream->runtime;
262
263 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
264 runtime->dma_area,
265 runtime->dma_addr,
266 runtime->dma_bytes);
267}
268
Mark Brownb2a19d02009-01-17 19:14:26 +0000269static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200270 .open = omap_pcm_open,
271 .close = omap_pcm_close,
272 .ioctl = snd_pcm_lib_ioctl,
273 .hw_params = omap_pcm_hw_params,
274 .hw_free = omap_pcm_hw_free,
275 .prepare = omap_pcm_prepare,
276 .trigger = omap_pcm_trigger,
277 .pointer = omap_pcm_pointer,
278 .mmap = omap_pcm_mmap,
279};
280
281static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
282
283static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
284 int stream)
285{
286 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
287 struct snd_dma_buffer *buf = &substream->dma_buffer;
288 size_t size = omap_pcm_hardware.buffer_bytes_max;
289
290 buf->dev.type = SNDRV_DMA_TYPE_DEV;
291 buf->dev.dev = pcm->card->dev;
292 buf->private_data = NULL;
293 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
294 &buf->addr, GFP_KERNEL);
295 if (!buf->area)
296 return -ENOMEM;
297
298 buf->bytes = size;
299 return 0;
300}
301
302static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
303{
304 struct snd_pcm_substream *substream;
305 struct snd_dma_buffer *buf;
306 int stream;
307
308 for (stream = 0; stream < 2; stream++) {
309 substream = pcm->streams[stream].substream;
310 if (!substream)
311 continue;
312
313 buf = &substream->dma_buffer;
314 if (!buf->area)
315 continue;
316
317 dma_free_writecombine(pcm->card->dev, buf->bytes,
318 buf->area, buf->addr);
319 buf->area = NULL;
320 }
321}
322
Liam Girdwood8687eb82008-07-07 16:08:07 +0100323int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200324 struct snd_pcm *pcm)
325{
326 int ret = 0;
327
328 if (!card->dev->dma_mask)
329 card->dev->dma_mask = &omap_pcm_dmamask;
330 if (!card->dev->coherent_dma_mask)
331 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
332
333 if (dai->playback.channels_min) {
334 ret = omap_pcm_preallocate_dma_buffer(pcm,
335 SNDRV_PCM_STREAM_PLAYBACK);
336 if (ret)
337 goto out;
338 }
339
340 if (dai->capture.channels_min) {
341 ret = omap_pcm_preallocate_dma_buffer(pcm,
342 SNDRV_PCM_STREAM_CAPTURE);
343 if (ret)
344 goto out;
345 }
346
347out:
348 return ret;
349}
350
351struct snd_soc_platform omap_soc_platform = {
352 .name = "omap-pcm-audio",
353 .pcm_ops = &omap_pcm_ops,
354 .pcm_new = omap_pcm_new,
355 .pcm_free = omap_pcm_free_dma_buffers,
356};
357EXPORT_SYMBOL_GPL(omap_soc_platform);
358
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100359static int __init omap_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000360{
361 return snd_soc_register_platform(&omap_soc_platform);
362}
363module_init(omap_soc_platform_init);
364
365static void __exit omap_soc_platform_exit(void)
366{
367 snd_soc_unregister_platform(&omap_soc_platform);
368}
369module_exit(omap_soc_platform_exit);
370
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300371MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200372MODULE_DESCRIPTION("OMAP PCM DMA module");
373MODULE_LICENSE("GPL");