2 * File: sound/soc/blackfin/bf5xx-i2s-pcm.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
5 * Created: Tue June 06 2008
6 * Description: DMA driver for i2s codec
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/dma-mapping.h>
33 #include <linux/gfp.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-sport.h"
44 static void bf5xx_dma_irq(void *data)
46 struct snd_pcm_substream *pcm = data;
47 snd_pcm_period_elapsed(pcm);
50 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
51 .info = SNDRV_PCM_INFO_INTERLEAVED |
53 SNDRV_PCM_INFO_MMAP_VALID |
54 SNDRV_PCM_INFO_BLOCK_TRANSFER,
55 .formats = SNDRV_PCM_FMTBIT_S16_LE |
56 SNDRV_PCM_FMTBIT_S24_LE |
57 SNDRV_PCM_FMTBIT_S32_LE,
58 .period_bytes_min = 32,
59 .period_bytes_max = 0x10000,
61 .periods_max = PAGE_SIZE/32,
62 .buffer_bytes_max = 0x20000, /* 128 kbytes */
66 static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
69 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
72 static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
74 snd_pcm_lib_free_pages(substream);
79 static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
81 struct snd_pcm_runtime *runtime = substream->runtime;
82 struct sport_device *sport = runtime->private_data;
83 int period_bytes = frames_to_bytes(runtime, runtime->period_size);
85 pr_debug("%s enter\n", __func__);
86 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
87 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
88 sport_config_tx_dma(sport, runtime->dma_area,
89 runtime->periods, period_bytes);
91 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
92 sport_config_rx_dma(sport, runtime->dma_area,
93 runtime->periods, period_bytes);
99 static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
101 struct snd_pcm_runtime *runtime = substream->runtime;
102 struct sport_device *sport = runtime->private_data;
105 pr_debug("%s enter\n", __func__);
107 case SNDRV_PCM_TRIGGER_START:
108 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
109 sport_tx_start(sport);
111 sport_rx_start(sport);
113 case SNDRV_PCM_TRIGGER_STOP:
114 case SNDRV_PCM_TRIGGER_SUSPEND:
115 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
116 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
117 sport_tx_stop(sport);
119 sport_rx_stop(sport);
128 static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
130 struct snd_pcm_runtime *runtime = substream->runtime;
131 struct sport_device *sport = runtime->private_data;
133 snd_pcm_uframes_t frames;
134 pr_debug("%s enter\n", __func__);
135 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
136 diff = sport_curr_offset_tx(sport);
138 diff = sport_curr_offset_rx(sport);
142 * TX at least can report one frame beyond the end of the
143 * buffer if we hit the wraparound case - clamp to within the
144 * buffer as the ALSA APIs require.
146 if (diff == snd_pcm_lib_buffer_bytes(substream))
149 frames = bytes_to_frames(substream->runtime, diff);
154 static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
156 struct snd_soc_pcm_runtime *rtd = substream->private_data;
157 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
159 struct snd_pcm_runtime *runtime = substream->runtime;
160 struct snd_dma_buffer *buf = &substream->dma_buffer;
163 pr_debug("%s enter\n", __func__);
165 snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
167 ret = snd_pcm_hw_constraint_integer(runtime,
168 SNDRV_PCM_HW_PARAM_PERIODS);
172 if (sport_handle != NULL) {
173 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
174 sport_handle->tx_buf = buf->area;
176 sport_handle->rx_buf = buf->area;
178 runtime->private_data = sport_handle;
180 pr_err("sport_handle is NULL\n");
189 static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
190 struct vm_area_struct *vma)
192 struct snd_pcm_runtime *runtime = substream->runtime;
193 size_t size = vma->vm_end - vma->vm_start;
194 vma->vm_start = (unsigned long)runtime->dma_area;
195 vma->vm_end = vma->vm_start + size;
196 vma->vm_flags |= VM_SHARED;
201 static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
202 .open = bf5xx_pcm_open,
203 .ioctl = snd_pcm_lib_ioctl,
204 .hw_params = bf5xx_pcm_hw_params,
205 .hw_free = bf5xx_pcm_hw_free,
206 .prepare = bf5xx_pcm_prepare,
207 .trigger = bf5xx_pcm_trigger,
208 .pointer = bf5xx_pcm_pointer,
209 .mmap = bf5xx_pcm_mmap,
212 static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
214 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
215 struct snd_dma_buffer *buf = &substream->dma_buffer;
216 size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
218 buf->dev.type = SNDRV_DMA_TYPE_DEV;
219 buf->dev.dev = pcm->card->dev;
220 buf->private_data = NULL;
221 buf->area = dma_alloc_coherent(pcm->card->dev, size,
222 &buf->addr, GFP_KERNEL);
224 pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");
229 pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
230 buf->area, buf->bytes);
235 static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
237 struct snd_pcm_substream *substream;
238 struct snd_dma_buffer *buf;
241 for (stream = 0; stream < 2; stream++) {
242 substream = pcm->streams[stream].substream;
246 buf = &substream->dma_buffer;
249 dma_free_coherent(NULL, buf->bytes, buf->area, 0);
254 static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
256 static int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime *rtd)
258 struct snd_card *card = rtd->card->snd_card;
259 struct snd_pcm *pcm = rtd->pcm;
262 pr_debug("%s enter\n", __func__);
263 if (!card->dev->dma_mask)
264 card->dev->dma_mask = &bf5xx_pcm_dmamask;
265 if (!card->dev->coherent_dma_mask)
266 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
268 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
269 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
270 SNDRV_PCM_STREAM_PLAYBACK);
275 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
276 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
277 SNDRV_PCM_STREAM_CAPTURE);
285 static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
286 .ops = &bf5xx_pcm_i2s_ops,
287 .pcm_new = bf5xx_pcm_i2s_new,
288 .pcm_free = bf5xx_pcm_free_dma_buffers,
291 static int bfin_i2s_soc_platform_probe(struct platform_device *pdev)
293 return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
296 static int bfin_i2s_soc_platform_remove(struct platform_device *pdev)
298 snd_soc_unregister_platform(&pdev->dev);
302 static struct platform_driver bfin_i2s_pcm_driver = {
304 .name = "bfin-i2s-pcm-audio",
305 .owner = THIS_MODULE,
308 .probe = bfin_i2s_soc_platform_probe,
309 .remove = bfin_i2s_soc_platform_remove,
312 module_platform_driver(bfin_i2s_pcm_driver);
314 MODULE_AUTHOR("Cliff Cai");
315 MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
316 MODULE_LICENSE("GPL");