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