blob: 12e14c01068e7eb5d67cdd6f89d5a2102b32e1e5 [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
Janusz Krzysztofik64844a62009-08-10 10:50:04 +020062 if ((cpu_is_omap1510()) &&
63 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) {
Jarkko Nikula2e747962008-04-25 13:55:19 +020064 /*
Janusz Krzysztofik64844a62009-08-10 10:50:04 +020065 * OMAP1510 doesn't fully support DMA progress counter
66 * and there is no software emulation implemented yet,
67 * so have to maintain our own playback progress counter
68 * that can be used by omap_pcm_pointer() instead.
Jarkko Nikula2e747962008-04-25 13:55:19 +020069 */
70 spin_lock_irqsave(&prtd->lock, flags);
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +020071 if ((stat == OMAP_DMA_LAST_IRQ) &&
72 (prtd->period_index == runtime->periods - 1)) {
73 /* we are in sync, do nothing */
74 spin_unlock_irqrestore(&prtd->lock, flags);
75 return;
76 }
Jarkko Nikula2e747962008-04-25 13:55:19 +020077 if (prtd->period_index >= 0) {
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +020078 if (stat & OMAP_DMA_BLOCK_IRQ) {
79 /* end of buffer reached, loop back */
80 prtd->period_index = 0;
81 } else if (stat & OMAP_DMA_LAST_IRQ) {
82 /* update the counter for the last period */
83 prtd->period_index = runtime->periods - 1;
84 } else if (++prtd->period_index >= runtime->periods) {
85 /* end of buffer missed? loop back */
Jarkko Nikula2e747962008-04-25 13:55:19 +020086 prtd->period_index = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020087 }
88 }
89 spin_unlock_irqrestore(&prtd->lock, flags);
90 }
91
92 snd_pcm_period_elapsed(substream);
93}
94
95/* this may get called several times by oss emulation */
96static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
97 struct snd_pcm_hw_params *params)
98{
99 struct snd_pcm_runtime *runtime = substream->runtime;
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
101 struct omap_runtime_data *prtd = runtime->private_data;
102 struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
103 int err = 0;
104
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900105 /* return if this is a bufferless transfer e.g.
106 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +0200107 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900108 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200109
110 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
111 runtime->dma_bytes = params_buffer_bytes(params);
112
113 if (prtd->dma_data)
114 return 0;
115 prtd->dma_data = dma_data;
116 err = omap_request_dma(dma_data->dma_req, dma_data->name,
117 omap_pcm_dma_irq, substream, &prtd->dma_ch);
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200118 if (!err) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200119 /*
120 * Link channel with itself so DMA doesn't need any
121 * reprogramming while looping the buffer
122 */
123 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
124 }
125
126 return err;
127}
128
129static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
130{
131 struct snd_pcm_runtime *runtime = substream->runtime;
132 struct omap_runtime_data *prtd = runtime->private_data;
133
134 if (prtd->dma_data == NULL)
135 return 0;
136
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200137 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200138 omap_free_dma(prtd->dma_ch);
139 prtd->dma_data = NULL;
140
141 snd_pcm_set_runtime_buffer(substream, NULL);
142
143 return 0;
144}
145
146static int omap_pcm_prepare(struct snd_pcm_substream *substream)
147{
148 struct snd_pcm_runtime *runtime = substream->runtime;
149 struct omap_runtime_data *prtd = runtime->private_data;
150 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
151 struct omap_dma_channel_params dma_params;
152
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900153 /* return if this is a bufferless transfer e.g.
154 * codec <--> BT codec or GSM modem -- lg FIXME */
155 if (!prtd->dma_data)
156 return 0;
157
Jarkko Nikula2e747962008-04-25 13:55:19 +0200158 memset(&dma_params, 0, sizeof(dma_params));
159 /*
160 * Note: Regardless of interface data formats supported by OMAP McBSP
161 * or EAC blocks, internal representation is always fixed 16-bit/sample
162 */
163 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
164 dma_params.trigger = dma_data->dma_req;
165 dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
167 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
168 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
169 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
170 dma_params.src_start = runtime->dma_addr;
171 dma_params.dst_start = dma_data->port_addr;
Arun KS9d374842008-09-30 15:35:16 +0530172 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200173 } else {
174 dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
175 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
176 dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
177 dma_params.src_start = dma_data->port_addr;
178 dma_params.dst_start = runtime->dma_addr;
Arun KS9d374842008-09-30 15:35:16 +0530179 dma_params.src_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200180 }
181 /*
182 * Set DMA transfer frame size equal to ALSA period size and frame
183 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
184 * we can transfer the whole ALSA buffer with single DMA transfer but
185 * still can get an interrupt at each period bounary
186 */
187 dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
188 dma_params.frame_count = runtime->periods;
189 omap_set_dma_params(prtd->dma_ch, &dma_params);
190
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +0200191 if ((cpu_is_omap1510()) &&
192 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
193 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ |
194 OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ);
195 else
196 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200197
198 return 0;
199}
200
201static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
202{
203 struct snd_pcm_runtime *runtime = substream->runtime;
204 struct omap_runtime_data *prtd = runtime->private_data;
Eero Nurkkala21dff432009-02-02 14:20:46 +0200205 unsigned long flags;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200206 int ret = 0;
207
Eero Nurkkala21dff432009-02-02 14:20:46 +0200208 spin_lock_irqsave(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200209 switch (cmd) {
210 case SNDRV_PCM_TRIGGER_START:
211 case SNDRV_PCM_TRIGGER_RESUME:
212 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
213 prtd->period_index = 0;
214 omap_start_dma(prtd->dma_ch);
215 break;
216
217 case SNDRV_PCM_TRIGGER_STOP:
218 case SNDRV_PCM_TRIGGER_SUSPEND:
219 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
220 prtd->period_index = -1;
221 omap_stop_dma(prtd->dma_ch);
222 break;
223 default:
224 ret = -EINVAL;
225 }
Eero Nurkkala21dff432009-02-02 14:20:46 +0200226 spin_unlock_irqrestore(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200227
228 return ret;
229}
230
231static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
232{
233 struct snd_pcm_runtime *runtime = substream->runtime;
234 struct omap_runtime_data *prtd = runtime->private_data;
235 dma_addr_t ptr;
236 snd_pcm_uframes_t offset;
237
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200238 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200239 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200240 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
241 } else if (!(cpu_is_omap1510())) {
242 ptr = omap_get_dma_src_pos(prtd->dma_ch);
243 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
244 } else
245 offset = prtd->period_index * runtime->period_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200246
Jarkko Nikula2e747962008-04-25 13:55:19 +0200247 if (offset >= runtime->buffer_size)
248 offset = 0;
249
250 return offset;
251}
252
253static int omap_pcm_open(struct snd_pcm_substream *substream)
254{
255 struct snd_pcm_runtime *runtime = substream->runtime;
256 struct omap_runtime_data *prtd;
257 int ret;
258
259 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
260
261 /* Ensure that buffer size is a multiple of period size */
262 ret = snd_pcm_hw_constraint_integer(runtime,
263 SNDRV_PCM_HW_PARAM_PERIODS);
264 if (ret < 0)
265 goto out;
266
Stanley Miao19b3f312008-12-19 22:08:22 +0800267 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200268 if (prtd == NULL) {
269 ret = -ENOMEM;
270 goto out;
271 }
272 spin_lock_init(&prtd->lock);
273 runtime->private_data = prtd;
274
275out:
276 return ret;
277}
278
279static int omap_pcm_close(struct snd_pcm_substream *substream)
280{
281 struct snd_pcm_runtime *runtime = substream->runtime;
282
283 kfree(runtime->private_data);
284 return 0;
285}
286
287static int omap_pcm_mmap(struct snd_pcm_substream *substream,
288 struct vm_area_struct *vma)
289{
290 struct snd_pcm_runtime *runtime = substream->runtime;
291
292 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
293 runtime->dma_area,
294 runtime->dma_addr,
295 runtime->dma_bytes);
296}
297
Mark Brownb2a19d02009-01-17 19:14:26 +0000298static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200299 .open = omap_pcm_open,
300 .close = omap_pcm_close,
301 .ioctl = snd_pcm_lib_ioctl,
302 .hw_params = omap_pcm_hw_params,
303 .hw_free = omap_pcm_hw_free,
304 .prepare = omap_pcm_prepare,
305 .trigger = omap_pcm_trigger,
306 .pointer = omap_pcm_pointer,
307 .mmap = omap_pcm_mmap,
308};
309
310static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
311
312static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
313 int stream)
314{
315 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
316 struct snd_dma_buffer *buf = &substream->dma_buffer;
317 size_t size = omap_pcm_hardware.buffer_bytes_max;
318
319 buf->dev.type = SNDRV_DMA_TYPE_DEV;
320 buf->dev.dev = pcm->card->dev;
321 buf->private_data = NULL;
322 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
323 &buf->addr, GFP_KERNEL);
324 if (!buf->area)
325 return -ENOMEM;
326
327 buf->bytes = size;
328 return 0;
329}
330
331static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
332{
333 struct snd_pcm_substream *substream;
334 struct snd_dma_buffer *buf;
335 int stream;
336
337 for (stream = 0; stream < 2; stream++) {
338 substream = pcm->streams[stream].substream;
339 if (!substream)
340 continue;
341
342 buf = &substream->dma_buffer;
343 if (!buf->area)
344 continue;
345
346 dma_free_writecombine(pcm->card->dev, buf->bytes,
347 buf->area, buf->addr);
348 buf->area = NULL;
349 }
350}
351
Lopez Cruz, Misaeld756b272009-07-22 20:45:03 -0500352static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200353 struct snd_pcm *pcm)
354{
355 int ret = 0;
356
357 if (!card->dev->dma_mask)
358 card->dev->dma_mask = &omap_pcm_dmamask;
359 if (!card->dev->coherent_dma_mask)
Yang Hongyang284901a2009-04-06 19:01:15 -0700360 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200361
362 if (dai->playback.channels_min) {
363 ret = omap_pcm_preallocate_dma_buffer(pcm,
364 SNDRV_PCM_STREAM_PLAYBACK);
365 if (ret)
366 goto out;
367 }
368
369 if (dai->capture.channels_min) {
370 ret = omap_pcm_preallocate_dma_buffer(pcm,
371 SNDRV_PCM_STREAM_CAPTURE);
372 if (ret)
373 goto out;
374 }
375
376out:
377 return ret;
378}
379
380struct snd_soc_platform omap_soc_platform = {
381 .name = "omap-pcm-audio",
382 .pcm_ops = &omap_pcm_ops,
383 .pcm_new = omap_pcm_new,
384 .pcm_free = omap_pcm_free_dma_buffers,
385};
386EXPORT_SYMBOL_GPL(omap_soc_platform);
387
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100388static int __init omap_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000389{
390 return snd_soc_register_platform(&omap_soc_platform);
391}
392module_init(omap_soc_platform_init);
393
394static void __exit omap_soc_platform_exit(void)
395{
396 snd_soc_unregister_platform(&omap_soc_platform);
397}
398module_exit(omap_soc_platform_exit);
399
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300400MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200401MODULE_DESCRIPTION("OMAP PCM DMA module");
402MODULE_LICENSE("GPL");