2 * File: sound/soc/blackfin/bf5xx-ac97-pcm.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
5 * Created: Tue June 06 2008
6 * Description: DMA Driver for AC97 sound chip
9 * Copyright 2008 Analog Devices Inc.
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see the file COPYING, or write
25 * to the Free Software Foundation, Inc.,
26 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/dma-mapping.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
42 #include "bf5xx-ac97-pcm.h"
43 #include "bf5xx-ac97.h"
44 #include "bf5xx-sport.h"
46 #if defined(CONFIG_SND_MMAP_SUPPORT)
47 static void bf5xx_mmap_copy(struct snd_pcm_substream *substream,
48 snd_pcm_uframes_t count)
50 struct snd_pcm_runtime *runtime = substream->runtime;
51 struct sport_device *sport = runtime->private_data;
52 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
54 (struct ac97_frame *)sport->tx_dma_buf + sport->tx_pos,
55 (__u32 *)runtime->dma_area + sport->tx_pos, count);
56 sport->tx_pos += runtime->period_size;
57 if (sport->tx_pos >= runtime->buffer_size)
58 sport->tx_pos %= runtime->buffer_size;
61 (struct ac97_frame *)sport->rx_dma_buf + sport->rx_pos,
62 (__u32 *)runtime->dma_area + sport->rx_pos, count);
63 sport->rx_pos += runtime->period_size;
64 if (sport->rx_pos >= runtime->buffer_size)
65 sport->rx_pos %= runtime->buffer_size;
70 static void bf5xx_dma_irq(void *data)
72 struct snd_pcm_substream *pcm = data;
73 #if defined(CONFIG_SND_MMAP_SUPPORT)
74 struct snd_pcm_runtime *runtime = pcm->runtime;
75 bf5xx_mmap_copy(pcm, runtime->period_size);
77 snd_pcm_period_elapsed(pcm);
80 /* The memory size for pure pcm data is 128*1024 = 0x20000 bytes.
81 * The total rx/tx buffer is for ac97 frame to hold all pcm data
82 * is 0x20000 * sizeof(struct ac97_frame) / 4.
84 #ifdef CONFIG_SND_MMAP_SUPPORT
85 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
86 .info = SNDRV_PCM_INFO_INTERLEAVED |
88 SNDRV_PCM_INFO_MMAP_VALID |
89 SNDRV_PCM_INFO_BLOCK_TRANSFER,
91 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
92 .info = SNDRV_PCM_INFO_INTERLEAVED |
93 SNDRV_PCM_INFO_BLOCK_TRANSFER,
95 .formats = SNDRV_PCM_FMTBIT_S16_LE,
96 .period_bytes_min = 32,
97 .period_bytes_max = 0x10000,
99 .periods_max = PAGE_SIZE/32,
100 .buffer_bytes_max = 0x20000, /* 128 kbytes */
104 static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
105 struct snd_pcm_hw_params *params)
107 size_t size = bf5xx_pcm_hardware.buffer_bytes_max
108 * sizeof(struct ac97_frame) / 4;
110 snd_pcm_lib_malloc_pages(substream, size);
115 static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
117 snd_pcm_lib_free_pages(substream);
121 static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
123 struct snd_pcm_runtime *runtime = substream->runtime;
124 struct sport_device *sport = runtime->private_data;
126 /* An intermediate buffer is introduced for implementing mmap for
127 * SPORT working in TMD mode(include AC97).
129 #if defined(CONFIG_SND_MMAP_SUPPORT)
130 size_t size = bf5xx_pcm_hardware.buffer_bytes_max
131 * sizeof(struct ac97_frame) / 4;
132 /*clean up intermediate buffer*/
133 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
134 memset(sport->tx_dma_buf, 0, size);
135 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
136 sport_config_tx_dma(sport, sport->tx_dma_buf, runtime->periods,
137 runtime->period_size * sizeof(struct ac97_frame));
139 memset(sport->rx_dma_buf, 0, size);
140 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
141 sport_config_rx_dma(sport, sport->rx_dma_buf, runtime->periods,
142 runtime->period_size * sizeof(struct ac97_frame));
145 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
146 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
147 sport_config_tx_dma(sport, runtime->dma_area, runtime->periods,
148 runtime->period_size * sizeof(struct ac97_frame));
150 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
151 sport_config_rx_dma(sport, runtime->dma_area, runtime->periods,
152 runtime->period_size * sizeof(struct ac97_frame));
158 static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
160 struct snd_pcm_runtime *runtime = substream->runtime;
161 struct sport_device *sport = runtime->private_data;
164 pr_debug("%s enter\n", __func__);
166 case SNDRV_PCM_TRIGGER_START:
167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
168 sport_tx_start(sport);
170 sport_rx_start(sport);
172 case SNDRV_PCM_TRIGGER_STOP:
173 case SNDRV_PCM_TRIGGER_SUSPEND:
174 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
175 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
176 #if defined(CONFIG_SND_MMAP_SUPPORT)
179 sport_tx_stop(sport);
181 #if defined(CONFIG_SND_MMAP_SUPPORT)
184 sport_rx_stop(sport);
193 static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
195 struct snd_pcm_runtime *runtime = substream->runtime;
196 struct sport_device *sport = runtime->private_data;
199 #if defined(CONFIG_SND_MMAP_SUPPORT)
200 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
201 curr = sport->tx_pos;
203 curr = sport->rx_pos;
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
207 curr = sport_curr_offset_tx(sport) / sizeof(struct ac97_frame);
209 curr = sport_curr_offset_rx(sport) / sizeof(struct ac97_frame);
215 static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
217 struct snd_pcm_runtime *runtime = substream->runtime;
220 pr_debug("%s enter\n", __func__);
221 snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
223 ret = snd_pcm_hw_constraint_integer(runtime,
224 SNDRV_PCM_HW_PARAM_PERIODS);
228 if (sport_handle != NULL)
229 runtime->private_data = sport_handle;
231 pr_err("sport_handle is NULL\n");
240 #ifdef CONFIG_SND_MMAP_SUPPORT
241 static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
242 struct vm_area_struct *vma)
244 struct snd_pcm_runtime *runtime = substream->runtime;
245 size_t size = vma->vm_end - vma->vm_start;
246 vma->vm_start = (unsigned long)runtime->dma_area;
247 vma->vm_end = vma->vm_start + size;
248 vma->vm_flags |= VM_SHARED;
252 static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
253 snd_pcm_uframes_t pos,
254 void __user *buf, snd_pcm_uframes_t count)
256 struct snd_pcm_runtime *runtime = substream->runtime;
258 pr_debug("%s copy pos:0x%lx count:0x%lx\n",
259 substream->stream ? "Capture" : "Playback", pos, count);
261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
263 (struct ac97_frame *)runtime->dma_area + pos,
267 (struct ac97_frame *)runtime->dma_area + pos,
273 struct snd_pcm_ops bf5xx_pcm_ac97_ops = {
274 .open = bf5xx_pcm_open,
275 .ioctl = snd_pcm_lib_ioctl,
276 .hw_params = bf5xx_pcm_hw_params,
277 .hw_free = bf5xx_pcm_hw_free,
278 .prepare = bf5xx_pcm_prepare,
279 .trigger = bf5xx_pcm_trigger,
280 .pointer = bf5xx_pcm_pointer,
281 #ifdef CONFIG_SND_MMAP_SUPPORT
282 .mmap = bf5xx_pcm_mmap,
284 .copy = bf5xx_pcm_copy,
288 static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
290 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
291 struct snd_dma_buffer *buf = &substream->dma_buffer;
292 size_t size = bf5xx_pcm_hardware.buffer_bytes_max
293 * sizeof(struct ac97_frame) / 4;
295 buf->dev.type = SNDRV_DMA_TYPE_DEV;
296 buf->dev.dev = pcm->card->dev;
297 buf->private_data = NULL;
298 buf->area = dma_alloc_coherent(pcm->card->dev, size,
299 &buf->addr, GFP_KERNEL);
301 pr_err("Failed to allocate dma memory\n");
302 pr_err("Please increase uncached DMA memory region\n");
307 pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
308 buf->area, buf->bytes);
310 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
311 sport_handle->tx_buf = buf->area;
313 sport_handle->rx_buf = buf->area;
316 * Need to allocate local buffer when enable
317 * MMAP for SPORT working in TMD mode (include AC97).
319 #if defined(CONFIG_SND_MMAP_SUPPORT)
320 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
321 if (!sport_handle->tx_dma_buf) {
322 sport_handle->tx_dma_buf = dma_alloc_coherent(NULL, \
323 size, &sport_handle->tx_dma_phy, GFP_KERNEL);
324 if (!sport_handle->tx_dma_buf) {
325 pr_err("Failed to allocate memory for tx dma \
326 buf - Please increase uncached DMA \
330 memset(sport_handle->tx_dma_buf, 0, size);
332 memset(sport_handle->tx_dma_buf, 0, size);
334 if (!sport_handle->rx_dma_buf) {
335 sport_handle->rx_dma_buf = dma_alloc_coherent(NULL, \
336 size, &sport_handle->rx_dma_phy, GFP_KERNEL);
337 if (!sport_handle->rx_dma_buf) {
338 pr_err("Failed to allocate memory for rx dma \
339 buf - Please increase uncached DMA \
343 memset(sport_handle->rx_dma_buf, 0, size);
345 memset(sport_handle->rx_dma_buf, 0, size);
351 static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
353 struct snd_pcm_substream *substream;
354 struct snd_dma_buffer *buf;
356 #if defined(CONFIG_SND_MMAP_SUPPORT)
357 size_t size = bf5xx_pcm_hardware.buffer_bytes_max *
358 sizeof(struct ac97_frame) / 4;
360 for (stream = 0; stream < 2; stream++) {
361 substream = pcm->streams[stream].substream;
365 buf = &substream->dma_buffer;
368 dma_free_coherent(NULL, buf->bytes, buf->area, 0);
370 #if defined(CONFIG_SND_MMAP_SUPPORT)
371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
372 if (sport_handle->tx_dma_buf)
373 dma_free_coherent(NULL, size, \
374 sport_handle->tx_dma_buf, 0);
375 sport_handle->tx_dma_buf = NULL;
378 if (sport_handle->rx_dma_buf)
379 dma_free_coherent(NULL, size, \
380 sport_handle->rx_dma_buf, 0);
381 sport_handle->rx_dma_buf = NULL;
386 sport_done(sport_handle);
389 static u64 bf5xx_pcm_dmamask = DMA_32BIT_MASK;
391 int bf5xx_pcm_ac97_new(struct snd_card *card, struct snd_soc_dai *dai,
396 pr_debug("%s enter\n", __func__);
397 if (!card->dev->dma_mask)
398 card->dev->dma_mask = &bf5xx_pcm_dmamask;
399 if (!card->dev->coherent_dma_mask)
400 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
402 if (dai->playback.channels_min) {
403 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
404 SNDRV_PCM_STREAM_PLAYBACK);
409 if (dai->capture.channels_min) {
410 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
411 SNDRV_PCM_STREAM_CAPTURE);
419 struct snd_soc_platform bf5xx_ac97_soc_platform = {
420 .name = "bf5xx-audio",
421 .pcm_ops = &bf5xx_pcm_ac97_ops,
422 .pcm_new = bf5xx_pcm_ac97_new,
423 .pcm_free = bf5xx_pcm_free_dma_buffers,
425 EXPORT_SYMBOL_GPL(bf5xx_ac97_soc_platform);
427 MODULE_AUTHOR("Cliff Cai");
428 MODULE_DESCRIPTION("ADI Blackfin AC97 PCM DMA module");
429 MODULE_LICENSE("GPL");