blob: 39456447132c86405730249f8cc48af7589e64a7 [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 Krzysztofikb5442a72010-03-28 22:29:29 +020063 if ((cpu_is_omap1510())) {
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,
Janusz Krzysztofikb5442a72010-03-28 22:29:29 +020067 * so have to maintain our own progress counters
Janusz Krzysztofik64844a62009-08-10 10:50:04 +020068 * 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;
Daniel Mack5f712b22010-03-22 10:11:15 +0100102 struct omap_pcm_dma_data *dma_data;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200103 int err = 0;
104
Daniel Mack5f712b22010-03-22 10:11:15 +0100105 dma_data = snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
106
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900107 /* return if this is a bufferless transfer e.g.
108 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +0200109 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900110 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200111
112 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
113 runtime->dma_bytes = params_buffer_bytes(params);
114
115 if (prtd->dma_data)
116 return 0;
117 prtd->dma_data = dma_data;
118 err = omap_request_dma(dma_data->dma_req, dma_data->name,
119 omap_pcm_dma_irq, substream, &prtd->dma_ch);
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200120 if (!err) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200121 /*
122 * Link channel with itself so DMA doesn't need any
123 * reprogramming while looping the buffer
124 */
125 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
126 }
127
128 return err;
129}
130
131static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
132{
133 struct snd_pcm_runtime *runtime = substream->runtime;
134 struct omap_runtime_data *prtd = runtime->private_data;
135
136 if (prtd->dma_data == NULL)
137 return 0;
138
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200139 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200140 omap_free_dma(prtd->dma_ch);
141 prtd->dma_data = NULL;
142
143 snd_pcm_set_runtime_buffer(substream, NULL);
144
145 return 0;
146}
147
148static int omap_pcm_prepare(struct snd_pcm_substream *substream)
149{
150 struct snd_pcm_runtime *runtime = substream->runtime;
151 struct omap_runtime_data *prtd = runtime->private_data;
152 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
153 struct omap_dma_channel_params dma_params;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600154 int bytes;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200155
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900156 /* return if this is a bufferless transfer e.g.
157 * codec <--> BT codec or GSM modem -- lg FIXME */
158 if (!prtd->dma_data)
159 return 0;
160
Jarkko Nikula2e747962008-04-25 13:55:19 +0200161 memset(&dma_params, 0, sizeof(dma_params));
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600162 dma_params.data_type = dma_data->data_type;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200163 dma_params.trigger = dma_data->dma_req;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300164 dma_params.sync_mode = dma_data->sync_mode;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
166 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
167 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
168 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
169 dma_params.src_start = runtime->dma_addr;
170 dma_params.dst_start = dma_data->port_addr;
Arun KS9d37484c2008-09-30 15:35:16 +0530171 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600172 dma_params.dst_fi = dma_data->packet_size;
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 KS9d37484c2008-09-30 15:35:16 +0530179 dma_params.src_port = OMAP_DMA_PORT_MPUI;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600180 dma_params.src_fi = dma_data->packet_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200181 }
182 /*
183 * Set DMA transfer frame size equal to ALSA period size and frame
184 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
185 * we can transfer the whole ALSA buffer with single DMA transfer but
186 * still can get an interrupt at each period bounary
187 */
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600188 bytes = snd_pcm_lib_period_bytes(substream);
189 dma_params.elem_count = bytes >> dma_data->data_type;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200190 dma_params.frame_count = runtime->periods;
191 omap_set_dma_params(prtd->dma_ch, &dma_params);
192
Janusz Krzysztofikb5442a72010-03-28 22:29:29 +0200193 if ((cpu_is_omap1510()))
Janusz Krzysztofik471e3de2009-08-11 21:44:29 +0200194 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ |
195 OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ);
196 else
197 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200198
Janusz Krzysztofik4d187fb2009-10-21 23:10:03 +0200199 if (!(cpu_class_is_omap1())) {
200 omap_set_dma_src_burst_mode(prtd->dma_ch,
201 OMAP_DMA_DATA_BURST_16);
202 omap_set_dma_dest_burst_mode(prtd->dma_ch,
203 OMAP_DMA_DATA_BURST_16);
204 }
Eduardo Valentin9599d482009-08-20 16:18:21 +0300205
Jarkko Nikula2e747962008-04-25 13:55:19 +0200206 return 0;
207}
208
209static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
210{
211 struct snd_pcm_runtime *runtime = substream->runtime;
212 struct omap_runtime_data *prtd = runtime->private_data;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300213 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
Eero Nurkkala21dff432009-02-02 14:20:46 +0200214 unsigned long flags;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200215 int ret = 0;
216
Eero Nurkkala21dff432009-02-02 14:20:46 +0200217 spin_lock_irqsave(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200218 switch (cmd) {
219 case SNDRV_PCM_TRIGGER_START:
220 case SNDRV_PCM_TRIGGER_RESUME:
221 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
222 prtd->period_index = 0;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300223 /* Configure McBSP internal buffer usage */
224 if (dma_data->set_threshold)
225 dma_data->set_threshold(substream);
226
Jarkko Nikula2e747962008-04-25 13:55:19 +0200227 omap_start_dma(prtd->dma_ch);
228 break;
229
230 case SNDRV_PCM_TRIGGER_STOP:
231 case SNDRV_PCM_TRIGGER_SUSPEND:
232 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233 prtd->period_index = -1;
234 omap_stop_dma(prtd->dma_ch);
235 break;
236 default:
237 ret = -EINVAL;
238 }
Eero Nurkkala21dff432009-02-02 14:20:46 +0200239 spin_unlock_irqrestore(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200240
241 return ret;
242}
243
244static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
245{
246 struct snd_pcm_runtime *runtime = substream->runtime;
247 struct omap_runtime_data *prtd = runtime->private_data;
248 dma_addr_t ptr;
249 snd_pcm_uframes_t offset;
250
Janusz Krzysztofikb5442a72010-03-28 22:29:29 +0200251 if (cpu_is_omap1510()) {
252 offset = prtd->period_index * runtime->period_size;
253 } else 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);
Janusz Krzysztofikb5442a72010-03-28 22:29:29 +0200256 } else {
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200257 ptr = omap_get_dma_src_pos(prtd->dma_ch);
258 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
Janusz Krzysztofikb5442a72010-03-28 22:29:29 +0200259 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200260
Jarkko Nikula2e747962008-04-25 13:55:19 +0200261 if (offset >= runtime->buffer_size)
262 offset = 0;
263
264 return offset;
265}
266
267static int omap_pcm_open(struct snd_pcm_substream *substream)
268{
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 struct omap_runtime_data *prtd;
271 int ret;
272
273 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
274
275 /* Ensure that buffer size is a multiple of period size */
276 ret = snd_pcm_hw_constraint_integer(runtime,
277 SNDRV_PCM_HW_PARAM_PERIODS);
278 if (ret < 0)
279 goto out;
280
Stanley Miao19b3f312008-12-19 22:08:22 +0800281 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200282 if (prtd == NULL) {
283 ret = -ENOMEM;
284 goto out;
285 }
286 spin_lock_init(&prtd->lock);
287 runtime->private_data = prtd;
288
289out:
290 return ret;
291}
292
293static int omap_pcm_close(struct snd_pcm_substream *substream)
294{
295 struct snd_pcm_runtime *runtime = substream->runtime;
296
297 kfree(runtime->private_data);
298 return 0;
299}
300
301static int omap_pcm_mmap(struct snd_pcm_substream *substream,
302 struct vm_area_struct *vma)
303{
304 struct snd_pcm_runtime *runtime = substream->runtime;
305
306 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
307 runtime->dma_area,
308 runtime->dma_addr,
309 runtime->dma_bytes);
310}
311
Mark Brownb2a19d02009-01-17 19:14:26 +0000312static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200313 .open = omap_pcm_open,
314 .close = omap_pcm_close,
315 .ioctl = snd_pcm_lib_ioctl,
316 .hw_params = omap_pcm_hw_params,
317 .hw_free = omap_pcm_hw_free,
318 .prepare = omap_pcm_prepare,
319 .trigger = omap_pcm_trigger,
320 .pointer = omap_pcm_pointer,
321 .mmap = omap_pcm_mmap,
322};
323
Eduardo Valentina152ff22009-08-20 16:18:22 +0300324static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200325
326static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
327 int stream)
328{
329 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
330 struct snd_dma_buffer *buf = &substream->dma_buffer;
331 size_t size = omap_pcm_hardware.buffer_bytes_max;
332
333 buf->dev.type = SNDRV_DMA_TYPE_DEV;
334 buf->dev.dev = pcm->card->dev;
335 buf->private_data = NULL;
336 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
337 &buf->addr, GFP_KERNEL);
338 if (!buf->area)
339 return -ENOMEM;
340
341 buf->bytes = size;
342 return 0;
343}
344
345static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
346{
347 struct snd_pcm_substream *substream;
348 struct snd_dma_buffer *buf;
349 int stream;
350
351 for (stream = 0; stream < 2; stream++) {
352 substream = pcm->streams[stream].substream;
353 if (!substream)
354 continue;
355
356 buf = &substream->dma_buffer;
357 if (!buf->area)
358 continue;
359
360 dma_free_writecombine(pcm->card->dev, buf->bytes,
361 buf->area, buf->addr);
362 buf->area = NULL;
363 }
364}
365
Lopez Cruz, Misaeld756b272009-07-22 20:45:03 -0500366static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200367 struct snd_pcm *pcm)
368{
369 int ret = 0;
370
371 if (!card->dev->dma_mask)
372 card->dev->dma_mask = &omap_pcm_dmamask;
373 if (!card->dev->coherent_dma_mask)
Eduardo Valentina152ff22009-08-20 16:18:22 +0300374 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200375
376 if (dai->playback.channels_min) {
377 ret = omap_pcm_preallocate_dma_buffer(pcm,
378 SNDRV_PCM_STREAM_PLAYBACK);
379 if (ret)
380 goto out;
381 }
382
383 if (dai->capture.channels_min) {
384 ret = omap_pcm_preallocate_dma_buffer(pcm,
385 SNDRV_PCM_STREAM_CAPTURE);
386 if (ret)
387 goto out;
388 }
389
390out:
391 return ret;
392}
393
394struct snd_soc_platform omap_soc_platform = {
395 .name = "omap-pcm-audio",
396 .pcm_ops = &omap_pcm_ops,
397 .pcm_new = omap_pcm_new,
398 .pcm_free = omap_pcm_free_dma_buffers,
399};
400EXPORT_SYMBOL_GPL(omap_soc_platform);
401
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100402static int __init omap_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000403{
404 return snd_soc_register_platform(&omap_soc_platform);
405}
406module_init(omap_soc_platform_init);
407
408static void __exit omap_soc_platform_exit(void)
409{
410 snd_soc_unregister_platform(&omap_soc_platform);
411}
412module_exit(omap_soc_platform_exit);
413
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300414MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200415MODULE_DESCRIPTION("OMAP PCM DMA module");
416MODULE_LICENSE("GPL");