initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / sound / soc / blackfin / bf5xx-i2s-pcm.c
blob77dae1643997d228728c8f072350c42636e6ca7d
1 /*
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
8 * Modified:
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>
40 #include <asm/dma.h>
42 #include "bf5xx-i2s-pcm.h"
43 #include "bf5xx-i2s.h"
44 #include "bf5xx-sport.h"
46 static void bf5xx_dma_irq(void *data)
48 struct snd_pcm_substream *pcm = data;
49 snd_pcm_period_elapsed(pcm);
52 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
53 .info = SNDRV_PCM_INFO_INTERLEAVED |
54 SNDRV_PCM_INFO_MMAP |
55 SNDRV_PCM_INFO_MMAP_VALID |
56 SNDRV_PCM_INFO_BLOCK_TRANSFER,
57 .formats = SNDRV_PCM_FMTBIT_S16_LE |
58 SNDRV_PCM_FMTBIT_S24_LE |
59 SNDRV_PCM_FMTBIT_S32_LE,
60 .period_bytes_min = 32,
61 .period_bytes_max = 0x10000,
62 .periods_min = 1,
63 .periods_max = PAGE_SIZE/32,
64 .buffer_bytes_max = 0x20000, /* 128 kbytes */
65 .fifo_size = 16,
68 static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params *params)
71 size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
72 snd_pcm_lib_malloc_pages(substream, size);
74 return 0;
77 static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
79 snd_pcm_lib_free_pages(substream);
81 return 0;
84 static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
86 struct snd_pcm_runtime *runtime = substream->runtime;
87 struct sport_device *sport = runtime->private_data;
88 int period_bytes = frames_to_bytes(runtime, runtime->period_size);
90 pr_debug("%s enter\n", __func__);
91 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
92 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
93 sport_config_tx_dma(sport, runtime->dma_area,
94 runtime->periods, period_bytes);
95 } else {
96 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
97 sport_config_rx_dma(sport, runtime->dma_area,
98 runtime->periods, period_bytes);
101 return 0;
104 static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
106 struct snd_pcm_runtime *runtime = substream->runtime;
107 struct sport_device *sport = runtime->private_data;
108 int ret = 0;
110 pr_debug("%s enter\n", __func__);
111 switch (cmd) {
112 case SNDRV_PCM_TRIGGER_START:
113 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
114 sport_tx_start(sport);
115 else
116 sport_rx_start(sport);
117 break;
118 case SNDRV_PCM_TRIGGER_STOP:
119 case SNDRV_PCM_TRIGGER_SUSPEND:
120 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
121 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
122 sport_tx_stop(sport);
123 else
124 sport_rx_stop(sport);
125 break;
126 default:
127 ret = -EINVAL;
130 return ret;
133 static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
135 struct snd_pcm_runtime *runtime = substream->runtime;
136 struct sport_device *sport = runtime->private_data;
137 unsigned int diff;
138 snd_pcm_uframes_t frames;
139 pr_debug("%s enter\n", __func__);
140 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
141 diff = sport_curr_offset_tx(sport);
142 } else {
143 diff = sport_curr_offset_rx(sport);
147 * TX at least can report one frame beyond the end of the
148 * buffer if we hit the wraparound case - clamp to within the
149 * buffer as the ALSA APIs require.
151 if (diff == snd_pcm_lib_buffer_bytes(substream))
152 diff = 0;
154 frames = bytes_to_frames(substream->runtime, diff);
156 return frames;
159 static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
161 struct snd_pcm_runtime *runtime = substream->runtime;
162 int ret;
164 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);
169 if (ret < 0)
170 goto out;
172 if (sport_handle != NULL)
173 runtime->private_data = sport_handle;
174 else {
175 pr_err("sport_handle is NULL\n");
176 return -1;
178 return 0;
180 out:
181 return ret;
184 static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
185 struct vm_area_struct *vma)
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 size_t size = vma->vm_end - vma->vm_start;
189 vma->vm_start = (unsigned long)runtime->dma_area;
190 vma->vm_end = vma->vm_start + size;
191 vma->vm_flags |= VM_SHARED;
193 return 0 ;
196 static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
197 .open = bf5xx_pcm_open,
198 .ioctl = snd_pcm_lib_ioctl,
199 .hw_params = bf5xx_pcm_hw_params,
200 .hw_free = bf5xx_pcm_hw_free,
201 .prepare = bf5xx_pcm_prepare,
202 .trigger = bf5xx_pcm_trigger,
203 .pointer = bf5xx_pcm_pointer,
204 .mmap = bf5xx_pcm_mmap,
207 static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
209 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
210 struct snd_dma_buffer *buf = &substream->dma_buffer;
211 size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
213 buf->dev.type = SNDRV_DMA_TYPE_DEV;
214 buf->dev.dev = pcm->card->dev;
215 buf->private_data = NULL;
216 buf->area = dma_alloc_coherent(pcm->card->dev, size,
217 &buf->addr, GFP_KERNEL);
218 if (!buf->area) {
219 pr_err("Failed to allocate dma memory \
220 Please increase uncached DMA memory region\n");
221 return -ENOMEM;
223 buf->bytes = size;
225 pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
226 buf->area, buf->bytes);
228 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
229 sport_handle->tx_buf = buf->area;
230 else
231 sport_handle->rx_buf = buf->area;
233 return 0;
236 static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
238 struct snd_pcm_substream *substream;
239 struct snd_dma_buffer *buf;
240 int stream;
242 for (stream = 0; stream < 2; stream++) {
243 substream = pcm->streams[stream].substream;
244 if (!substream)
245 continue;
247 buf = &substream->dma_buffer;
248 if (!buf->area)
249 continue;
250 dma_free_coherent(NULL, buf->bytes, buf->area, 0);
251 buf->area = NULL;
253 if (sport_handle)
254 sport_done(sport_handle);
257 static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
259 int bf5xx_pcm_i2s_new(struct snd_card *card, struct snd_soc_dai *dai,
260 struct snd_pcm *pcm)
262 int ret = 0;
264 pr_debug("%s enter\n", __func__);
265 if (!card->dev->dma_mask)
266 card->dev->dma_mask = &bf5xx_pcm_dmamask;
267 if (!card->dev->coherent_dma_mask)
268 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
270 if (dai->playback.channels_min) {
271 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
272 SNDRV_PCM_STREAM_PLAYBACK);
273 if (ret)
274 goto out;
277 if (dai->capture.channels_min) {
278 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
279 SNDRV_PCM_STREAM_CAPTURE);
280 if (ret)
281 goto out;
283 out:
284 return ret;
287 struct snd_soc_platform bf5xx_i2s_soc_platform = {
288 .name = "bf5xx-audio",
289 .pcm_ops = &bf5xx_pcm_i2s_ops,
290 .pcm_new = bf5xx_pcm_i2s_new,
291 .pcm_free = bf5xx_pcm_free_dma_buffers,
293 EXPORT_SYMBOL_GPL(bf5xx_i2s_soc_platform);
295 static int __init bfin_i2s_init(void)
297 return snd_soc_register_platform(&bf5xx_i2s_soc_platform);
299 module_init(bfin_i2s_init);
301 static void __exit bfin_i2s_exit(void)
303 snd_soc_unregister_platform(&bf5xx_i2s_soc_platform);
305 module_exit(bfin_i2s_exit);
307 MODULE_AUTHOR("Cliff Cai");
308 MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
309 MODULE_LICENSE("GPL");