blob: 3e3a9d478c0da1e332ccd629fc06d3f30febaa7c [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);
71 if (prtd->period_index >= 0) {
72 if (++prtd->period_index == runtime->periods) {
73 prtd->period_index = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020074 }
75 }
76 spin_unlock_irqrestore(&prtd->lock, flags);
77 }
78
79 snd_pcm_period_elapsed(substream);
80}
81
82/* this may get called several times by oss emulation */
83static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
84 struct snd_pcm_hw_params *params)
85{
86 struct snd_pcm_runtime *runtime = substream->runtime;
87 struct snd_soc_pcm_runtime *rtd = substream->private_data;
88 struct omap_runtime_data *prtd = runtime->private_data;
89 struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data;
90 int err = 0;
91
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090092 /* return if this is a bufferless transfer e.g.
93 * codec <--> BT codec or GSM modem -- lg FIXME */
Jarkko Nikula2e747962008-04-25 13:55:19 +020094 if (!dma_data)
Joonyoung Shim1b4246a2009-04-22 10:56:50 +090095 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +020096
97 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
98 runtime->dma_bytes = params_buffer_bytes(params);
99
100 if (prtd->dma_data)
101 return 0;
102 prtd->dma_data = dma_data;
103 err = omap_request_dma(dma_data->dma_req, dma_data->name,
104 omap_pcm_dma_irq, substream, &prtd->dma_ch);
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200105 if (!err) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200106 /*
107 * Link channel with itself so DMA doesn't need any
108 * reprogramming while looping the buffer
109 */
110 omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch);
111 }
112
113 return err;
114}
115
116static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
117{
118 struct snd_pcm_runtime *runtime = substream->runtime;
119 struct omap_runtime_data *prtd = runtime->private_data;
120
121 if (prtd->dma_data == NULL)
122 return 0;
123
Janusz Krzysztofik64844a62009-08-10 10:50:04 +0200124 omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200125 omap_free_dma(prtd->dma_ch);
126 prtd->dma_data = NULL;
127
128 snd_pcm_set_runtime_buffer(substream, NULL);
129
130 return 0;
131}
132
133static int omap_pcm_prepare(struct snd_pcm_substream *substream)
134{
135 struct snd_pcm_runtime *runtime = substream->runtime;
136 struct omap_runtime_data *prtd = runtime->private_data;
137 struct omap_pcm_dma_data *dma_data = prtd->dma_data;
138 struct omap_dma_channel_params dma_params;
139
Joonyoung Shim1b4246a2009-04-22 10:56:50 +0900140 /* return if this is a bufferless transfer e.g.
141 * codec <--> BT codec or GSM modem -- lg FIXME */
142 if (!prtd->dma_data)
143 return 0;
144
Jarkko Nikula2e747962008-04-25 13:55:19 +0200145 memset(&dma_params, 0, sizeof(dma_params));
146 /*
147 * Note: Regardless of interface data formats supported by OMAP McBSP
148 * or EAC blocks, internal representation is always fixed 16-bit/sample
149 */
150 dma_params.data_type = OMAP_DMA_DATA_TYPE_S16;
151 dma_params.trigger = dma_data->dma_req;
152 dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT;
153 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
154 dma_params.src_amode = OMAP_DMA_AMODE_POST_INC;
155 dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT;
156 dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC;
157 dma_params.src_start = runtime->dma_addr;
158 dma_params.dst_start = dma_data->port_addr;
Arun KS9d374842008-09-30 15:35:16 +0530159 dma_params.dst_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200160 } else {
161 dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT;
162 dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC;
163 dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC;
164 dma_params.src_start = dma_data->port_addr;
165 dma_params.dst_start = runtime->dma_addr;
Arun KS9d374842008-09-30 15:35:16 +0530166 dma_params.src_port = OMAP_DMA_PORT_MPUI;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200167 }
168 /*
169 * Set DMA transfer frame size equal to ALSA period size and frame
170 * count as no. of ALSA periods. Then with DMA frame interrupt enabled,
171 * we can transfer the whole ALSA buffer with single DMA transfer but
172 * still can get an interrupt at each period bounary
173 */
174 dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2;
175 dma_params.frame_count = runtime->periods;
176 omap_set_dma_params(prtd->dma_ch, &dma_params);
177
178 omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ);
179
180 return 0;
181}
182
183static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
184{
185 struct snd_pcm_runtime *runtime = substream->runtime;
186 struct omap_runtime_data *prtd = runtime->private_data;
Eero Nurkkala21dff432009-02-02 14:20:46 +0200187 unsigned long flags;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200188 int ret = 0;
189
Eero Nurkkala21dff432009-02-02 14:20:46 +0200190 spin_lock_irqsave(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200191 switch (cmd) {
192 case SNDRV_PCM_TRIGGER_START:
193 case SNDRV_PCM_TRIGGER_RESUME:
194 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
195 prtd->period_index = 0;
196 omap_start_dma(prtd->dma_ch);
197 break;
198
199 case SNDRV_PCM_TRIGGER_STOP:
200 case SNDRV_PCM_TRIGGER_SUSPEND:
201 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
202 prtd->period_index = -1;
203 omap_stop_dma(prtd->dma_ch);
204 break;
205 default:
206 ret = -EINVAL;
207 }
Eero Nurkkala21dff432009-02-02 14:20:46 +0200208 spin_unlock_irqrestore(&prtd->lock, flags);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200209
210 return ret;
211}
212
213static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
214{
215 struct snd_pcm_runtime *runtime = substream->runtime;
216 struct omap_runtime_data *prtd = runtime->private_data;
217 dma_addr_t ptr;
218 snd_pcm_uframes_t offset;
219
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200220 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200221 ptr = omap_get_dma_dst_pos(prtd->dma_ch);
Janusz Krzysztofik1bdd7412009-06-28 00:21:05 +0200222 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
223 } else if (!(cpu_is_omap1510())) {
224 ptr = omap_get_dma_src_pos(prtd->dma_ch);
225 offset = bytes_to_frames(runtime, ptr - runtime->dma_addr);
226 } else
227 offset = prtd->period_index * runtime->period_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200228
Jarkko Nikula2e747962008-04-25 13:55:19 +0200229 if (offset >= runtime->buffer_size)
230 offset = 0;
231
232 return offset;
233}
234
235static int omap_pcm_open(struct snd_pcm_substream *substream)
236{
237 struct snd_pcm_runtime *runtime = substream->runtime;
238 struct omap_runtime_data *prtd;
239 int ret;
240
241 snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
242
243 /* Ensure that buffer size is a multiple of period size */
244 ret = snd_pcm_hw_constraint_integer(runtime,
245 SNDRV_PCM_HW_PARAM_PERIODS);
246 if (ret < 0)
247 goto out;
248
Stanley Miao19b3f312008-12-19 22:08:22 +0800249 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200250 if (prtd == NULL) {
251 ret = -ENOMEM;
252 goto out;
253 }
254 spin_lock_init(&prtd->lock);
255 runtime->private_data = prtd;
256
257out:
258 return ret;
259}
260
261static int omap_pcm_close(struct snd_pcm_substream *substream)
262{
263 struct snd_pcm_runtime *runtime = substream->runtime;
264
265 kfree(runtime->private_data);
266 return 0;
267}
268
269static int omap_pcm_mmap(struct snd_pcm_substream *substream,
270 struct vm_area_struct *vma)
271{
272 struct snd_pcm_runtime *runtime = substream->runtime;
273
274 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
275 runtime->dma_area,
276 runtime->dma_addr,
277 runtime->dma_bytes);
278}
279
Mark Brownb2a19d02009-01-17 19:14:26 +0000280static struct snd_pcm_ops omap_pcm_ops = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200281 .open = omap_pcm_open,
282 .close = omap_pcm_close,
283 .ioctl = snd_pcm_lib_ioctl,
284 .hw_params = omap_pcm_hw_params,
285 .hw_free = omap_pcm_hw_free,
286 .prepare = omap_pcm_prepare,
287 .trigger = omap_pcm_trigger,
288 .pointer = omap_pcm_pointer,
289 .mmap = omap_pcm_mmap,
290};
291
292static u64 omap_pcm_dmamask = DMA_BIT_MASK(32);
293
294static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
295 int stream)
296{
297 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
298 struct snd_dma_buffer *buf = &substream->dma_buffer;
299 size_t size = omap_pcm_hardware.buffer_bytes_max;
300
301 buf->dev.type = SNDRV_DMA_TYPE_DEV;
302 buf->dev.dev = pcm->card->dev;
303 buf->private_data = NULL;
304 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
305 &buf->addr, GFP_KERNEL);
306 if (!buf->area)
307 return -ENOMEM;
308
309 buf->bytes = size;
310 return 0;
311}
312
313static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
314{
315 struct snd_pcm_substream *substream;
316 struct snd_dma_buffer *buf;
317 int stream;
318
319 for (stream = 0; stream < 2; stream++) {
320 substream = pcm->streams[stream].substream;
321 if (!substream)
322 continue;
323
324 buf = &substream->dma_buffer;
325 if (!buf->area)
326 continue;
327
328 dma_free_writecombine(pcm->card->dev, buf->bytes,
329 buf->area, buf->addr);
330 buf->area = NULL;
331 }
332}
333
Lopez Cruz, Misaeld756b272009-07-22 20:45:03 -0500334static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200335 struct snd_pcm *pcm)
336{
337 int ret = 0;
338
339 if (!card->dev->dma_mask)
340 card->dev->dma_mask = &omap_pcm_dmamask;
341 if (!card->dev->coherent_dma_mask)
Yang Hongyang284901a2009-04-06 19:01:15 -0700342 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200343
344 if (dai->playback.channels_min) {
345 ret = omap_pcm_preallocate_dma_buffer(pcm,
346 SNDRV_PCM_STREAM_PLAYBACK);
347 if (ret)
348 goto out;
349 }
350
351 if (dai->capture.channels_min) {
352 ret = omap_pcm_preallocate_dma_buffer(pcm,
353 SNDRV_PCM_STREAM_CAPTURE);
354 if (ret)
355 goto out;
356 }
357
358out:
359 return ret;
360}
361
362struct snd_soc_platform omap_soc_platform = {
363 .name = "omap-pcm-audio",
364 .pcm_ops = &omap_pcm_ops,
365 .pcm_new = omap_pcm_new,
366 .pcm_free = omap_pcm_free_dma_buffers,
367};
368EXPORT_SYMBOL_GPL(omap_soc_platform);
369
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100370static int __init omap_soc_platform_init(void)
Mark Brown958e7922008-12-03 19:58:17 +0000371{
372 return snd_soc_register_platform(&omap_soc_platform);
373}
374module_init(omap_soc_platform_init);
375
376static void __exit omap_soc_platform_exit(void)
377{
378 snd_soc_unregister_platform(&omap_soc_platform);
379}
380module_exit(omap_soc_platform_exit);
381
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300382MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200383MODULE_DESCRIPTION("OMAP PCM DMA module");
384MODULE_LICENSE("GPL");