blob: 39538c0f81f0dd1ce6b05a660201aa5a4594269c [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
Tony Lindgrence491cf2009-10-20 09:40:47 -070031#include <plat/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,
Misael Lopez Cruze17dd322010-02-22 15:09:19 -060040 .formats = SNDRV_PCM_FMTBIT_S16_LE |
41 SNDRV_PCM_FMTBIT_S32_LE,
Jarkko Nikula2e747962008-04-25 13:55:19 +020042 .period_bytes_min = 32,
43 .period_bytes_max = 64 * 1024,
44 .periods_min = 2,
45 .periods_max = 255,
46 .buffer_bytes_max = 128 * 1024,
47};
48
49struct omap_runtime_data {
50 spinlock_t lock;
51 struct omap_pcm_dma_data *dma_data;
52 int dma_ch;
53 int period_index;
54};
55
56static void omap_pcm_dma_irq(int ch, u16 stat, void *data)
57{
58 struct snd_pcm_substream *substream = data;
59 struct snd_pcm_runtime *runtime = substream->runtime;
60 struct omap_runtime_data *prtd = runtime->private_data;
61 unsigned long flags;
62
Janusz Krzysztofik64844a62009-08-10 10:50:04 +020063 if ((cpu_is_omap1510()) &&
64 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) {
Jarkko Nikula2e747962008-04-25 13:55:19 +020065 /*
Janusz Krzysztofik64844a62009-08-10 10:50:04 +020066 * OMAP1510 doesn't fully support DMA progress counter
67 * and there is no software emulation implemented yet,
68 * so have to maintain our own playback progress counter
69 * that can be used by omap_pcm_pointer() instead.
Jarkko Nikula2e747962008-04-25 13:55:19 +020070 */
71 spin_lock_irqsave(&prtd->lock, flags);
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +020072 if ((stat == OMAP_DMA_LAST_IRQ) &&
73 (prtd->period_index == runtime->periods - 1)) {
74 /* we are in sync, do nothing */
75 spin_unlock_irqrestore(&prtd->lock, flags);
76 return;
77 }
Jarkko Nikula2e747962008-04-25 13:55:19 +020078 if (prtd->period_index >= 0) {
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +020079 if (stat & OMAP_DMA_BLOCK_IRQ) {
80 /* end of buffer reached, loop back */
81 prtd->period_index = 0;
82 } else if (stat & OMAP_DMA_LAST_IRQ) {
83 /* update the counter for the last period */
84 prtd->period_index = runtime->periods - 1;
85 } else if (++prtd->period_index >= runtime->periods) {
86 /* end of buffer missed? loop back */
Jarkko Nikula2e747962008-04-25 13:55:19 +020087 prtd->period_index = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020088 }
89 }
90 spin_unlock_irqrestore(&prtd->lock, flags);
91 }
92
93 snd_pcm_period_elapsed(substream);
94}
95
96/* this may get called several times by oss emulation */
97static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
98 struct snd_pcm_hw_params *params)
99{
100 struct snd_pcm_runtime *runtime = substream->runtime;
101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
102 struct omap_runtime_data *prtd = runtime->private_data;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000103 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200104 int err = 0;
105
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000106 dma_data = snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
107
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900108 /* return if this is a bufferless transfer e.g.
109 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +0200110 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900111 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200112
113 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
114 runtime->dma_bytes = params_buffer_bytes(params);
115
116 if (prtd->dma_data)
117 return 0;
118 prtd->dma_data = dma_data;
119 err = omap_request_dma(dma_data->dma_req, dma_data->name,
120 omap_pcm_dma_irq, substream, &prtd->dma_ch);
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200121 if (!err) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200122 /*
123 * Link channel with itself so DMA doesn't need any
124 * reprogramming while looping the buffer
125 */
126 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
127 }
128
129 return err;
130}
131
132static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
133{
134 struct snd_pcm_runtime *runtime = substream->runtime;
135 struct omap_runtime_data *prtd = runtime->private_data;
136
137 if (prtd->dma_data == NULL)
138 return 0;
139
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200140 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200141 omap_free_dma(prtd->dma_ch);
142 prtd->dma_data = NULL;
143
144 snd_pcm_set_runtime_buffer(substream, NULL);
145
146 return 0;
147}
148
149static int omap_pcm_prepare(struct snd_pcm_substream *substream)
150{
151 struct snd_pcm_runtime *runtime = substream->runtime;
152 struct omap_runtime_data *prtd = runtime->private_data;
153 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
154 struct omap_dma_channel_params dma_params;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600155 int bytes;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200156
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900157 /* return if this is a bufferless transfer e.g.
158 * codec <--> BT codec or GSM modem -- lg FIXME */
159 if (!prtd->dma_data)
160 return 0;
161
Jarkko Nikula2e747962008-04-25 13:55:19 +0200162 memset(&dma_params, 0, sizeof(dma_params));
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600163 dma_params.data_type = dma_data->data_type;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200164 dma_params.trigger = dma_data->dma_req;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300165 dma_params.sync_mode = dma_data->sync_mode;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200166 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;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600173 dma_params.dst_fi = dma_data->packet_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200174 } else {
175 dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
176 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
177 dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
178 dma_params.src_start = dma_data->port_addr;
179 dma_params.dst_start = runtime->dma_addr;
Arun KS9d374842008-09-30 15:35:16 +0530180 dma_params.src_port = OMAP_DMA_PORT_MPUI;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600181 dma_params.src_fi = dma_data->packet_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200182 }
183 /*
184 * Set DMA transfer frame size equal to ALSA period size and frame
185 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
186 * we can transfer the whole ALSA buffer with single DMA transfer but
187 * still can get an interrupt at each period bounary
188 */
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600189 bytes = snd_pcm_lib_period_bytes(substream);
190 dma_params.elem_count = bytes >> dma_data->data_type;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200191 dma_params.frame_count = runtime->periods;
192 omap_set_dma_params(prtd->dma_ch, &dma_params);
193
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +0200194 if ((cpu_is_omap1510()) &&
195 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
196 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ |
197 OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ);
198 else
199 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200200
Janusz Krzysztofik4d187fb2009-10-21 23:10:03 +0200201 if (!(cpu_class_is_omap1())) {
202 omap_set_dma_src_burst_mode(prtd->dma_ch,
203 OMAP_DMA_DATA_BURST_16);
204 omap_set_dma_dest_burst_mode(prtd->dma_ch,
205 OMAP_DMA_DATA_BURST_16);
206 }
Eduardo Valentin9599d482009-08-20 16:18:21 +0300207
Jarkko Nikula2e747962008-04-25 13:55:19 +0200208 return 0;
209}
210
211static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
212{
213 struct snd_pcm_runtime *runtime = substream->runtime;
214 struct omap_runtime_data *prtd = runtime->private_data;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300215 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
Eero Nurkkala21dff432009-02-02 14:20:46 +0200216 unsigned long flags;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200217 int ret = 0;
218
Eero Nurkkala21dff432009-02-02 14:20:46 +0200219 spin_lock_irqsave(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200220 switch (cmd) {
221 case SNDRV_PCM_TRIGGER_START:
222 case SNDRV_PCM_TRIGGER_RESUME:
223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
224 prtd->period_index = 0;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300225 /* Configure McBSP internal buffer usage */
226 if (dma_data->set_threshold)
227 dma_data->set_threshold(substream);
228
Jarkko Nikula2e747962008-04-25 13:55:19 +0200229 omap_start_dma(prtd->dma_ch);
230 break;
231
232 case SNDRV_PCM_TRIGGER_STOP:
233 case SNDRV_PCM_TRIGGER_SUSPEND:
234 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
235 prtd->period_index = -1;
236 omap_stop_dma(prtd->dma_ch);
237 break;
238 default:
239 ret = -EINVAL;
240 }
Eero Nurkkala21dff432009-02-02 14:20:46 +0200241 spin_unlock_irqrestore(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200242
243 return ret;
244}
245
246static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
247{
248 struct snd_pcm_runtime *runtime = substream->runtime;
249 struct omap_runtime_data *prtd = runtime->private_data;
250 dma_addr_t ptr;
251 snd_pcm_uframes_t offset;
252
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200253 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200254 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200255 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
256 } else if (!(cpu_is_omap1510())) {
257 ptr = omap_get_dma_src_pos(prtd->dma_ch);
258 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
259 } else
260 offset = prtd->period_index * runtime->period_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200261
Jarkko Nikula2e747962008-04-25 13:55:19 +0200262 if (offset >= runtime->buffer_size)
263 offset = 0;
264
265 return offset;
266}
267
268static int omap_pcm_open(struct snd_pcm_substream *substream)
269{
270 struct snd_pcm_runtime *runtime = substream->runtime;
271 struct omap_runtime_data *prtd;
272 int ret;
273
274 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
275
276 /* Ensure that buffer size is a multiple of period size */
277 ret = snd_pcm_hw_constraint_integer(runtime,
278 SNDRV_PCM_HW_PARAM_PERIODS);
279 if (ret < 0)
280 goto out;
281
Stanley Miao19b3f312008-12-19 22:08:22 +0800282 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200283 if (prtd == NULL) {
284 ret = -ENOMEM;
285 goto out;
286 }
287 spin_lock_init(&prtd->lock);
288 runtime->private_data = prtd;
289
290out:
291 return ret;
292}
293
294static int omap_pcm_close(struct snd_pcm_substream *substream)
295{
296 struct snd_pcm_runtime *runtime = substream->runtime;
297
298 kfree(runtime->private_data);
299 return 0;
300}
301
302static int omap_pcm_mmap(struct snd_pcm_substream *substream,
303 struct vm_area_struct *vma)
304{
305 struct snd_pcm_runtime *runtime = substream->runtime;
306
307 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
308 runtime->dma_area,
309 runtime->dma_addr,
310 runtime->dma_bytes);
311}
312
Mark Brownb2a19d02009-01-17 19:14:26 +0000313static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200314 .open = omap_pcm_open,
315 .close = omap_pcm_close,
316 .ioctl = snd_pcm_lib_ioctl,
317 .hw_params = omap_pcm_hw_params,
318 .hw_free = omap_pcm_hw_free,
319 .prepare = omap_pcm_prepare,
320 .trigger = omap_pcm_trigger,
321 .pointer = omap_pcm_pointer,
322 .mmap = omap_pcm_mmap,
323};
324
Eduardo Valentina152ff22009-08-20 16:18:22 +0300325static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200326
327static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
328 int stream)
329{
330 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
331 struct snd_dma_buffer *buf = &substream->dma_buffer;
332 size_t size = omap_pcm_hardware.buffer_bytes_max;
333
334 buf->dev.type = SNDRV_DMA_TYPE_DEV;
335 buf->dev.dev = pcm->card->dev;
336 buf->private_data = NULL;
337 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
338 &buf->addr, GFP_KERNEL);
339 if (!buf->area)
340 return -ENOMEM;
341
342 buf->bytes = size;
343 return 0;
344}
345
346static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
347{
348 struct snd_pcm_substream *substream;
349 struct snd_dma_buffer *buf;
350 int stream;
351
352 for (stream = 0; stream < 2; stream++) {
353 substream = pcm->streams[stream].substream;
354 if (!substream)
355 continue;
356
357 buf = &substream->dma_buffer;
358 if (!buf->area)
359 continue;
360
361 dma_free_writecombine(pcm->card->dev, buf->bytes,
362 buf->area, buf->addr);
363 buf->area = NULL;
364 }
365}
366
Lopez Cruz, Misaeld756b272009-07-22 20:45:03 -0500367static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200368 struct snd_pcm *pcm)
369{
370 int ret = 0;
371
372 if (!card->dev->dma_mask)
373 card->dev->dma_mask = &omap_pcm_dmamask;
374 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300375 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200376
377 if (dai->playback.channels_min) {
378 ret = omap_pcm_preallocate_dma_buffer(pcm,
379 SNDRV_PCM_STREAM_PLAYBACK);
380 if (ret)
381 goto out;
382 }
383
384 if (dai->capture.channels_min) {
385 ret = omap_pcm_preallocate_dma_buffer(pcm,
386 SNDRV_PCM_STREAM_CAPTURE);
387 if (ret)
388 goto out;
389 }
390
391out:
392 return ret;
393}
394
395struct snd_soc_platform omap_soc_platform = {
396 .name = "omap-pcm-audio",
397 .pcm_ops = &omap_pcm_ops,
398 .pcm_new = omap_pcm_new,
399 .pcm_free = omap_pcm_free_dma_buffers,
400};
401EXPORT_SYMBOL_GPL(omap_soc_platform);
402
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100403static int __init omap_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000404{
405 return snd_soc_register_platform(&omap_soc_platform);
406}
407module_init(omap_soc_platform_init);
408
409static void __exit omap_soc_platform_exit(void)
410{
411 snd_soc_unregister_platform(&omap_soc_platform);
412}
413module_exit(omap_soc_platform_exit);
414
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300415MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200416MODULE_DESCRIPTION("OMAP PCM DMA module");
417MODULE_LICENSE("GPL");