Merge branch 'master' into for-upstream
[deliverable/linux.git] / sound / soc / blackfin / bf5xx-ac97-pcm.c
1 /*
2 * File: sound/soc/blackfin/bf5xx-ac97-pcm.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
4 *
5 * Created: Tue June 06 2008
6 * Description: DMA Driver for AC97 sound chip
7 *
8 * Modified:
9 * Copyright 2008 Analog Devices Inc.
10 *
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
12 *
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.
17 *
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.
22 *
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
27 */
28
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>
34
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39
40 #include <asm/dma.h>
41
42 #include "bf5xx-ac97-pcm.h"
43 #include "bf5xx-ac97.h"
44 #include "bf5xx-sport.h"
45
46 #if defined(CONFIG_SND_MMAP_SUPPORT)
47 static void bf5xx_mmap_copy(struct snd_pcm_substream *substream,
48 snd_pcm_uframes_t count)
49 {
50 struct snd_pcm_runtime *runtime = substream->runtime;
51 struct sport_device *sport = runtime->private_data;
52 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
53 bf5xx_pcm_to_ac97(
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;
59 sport->tx_delay_pos = sport->tx_pos;
60 } else {
61 bf5xx_ac97_to_pcm(
62 (struct ac97_frame *)sport->rx_dma_buf + sport->rx_pos,
63 (__u32 *)runtime->dma_area + sport->rx_pos, count);
64 sport->rx_pos += runtime->period_size;
65 if (sport->rx_pos >= runtime->buffer_size)
66 sport->rx_pos %= runtime->buffer_size;
67 }
68 }
69 #endif
70
71 static void bf5xx_dma_irq(void *data)
72 {
73 struct snd_pcm_substream *pcm = data;
74 #if defined(CONFIG_SND_MMAP_SUPPORT)
75 struct snd_pcm_runtime *runtime = pcm->runtime;
76 struct sport_device *sport = runtime->private_data;
77 bf5xx_mmap_copy(pcm, runtime->period_size);
78 if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK) {
79 if (sport->once == 0) {
80 snd_pcm_period_elapsed(pcm);
81 bf5xx_mmap_copy(pcm, runtime->period_size);
82 sport->once = 1;
83 }
84 }
85 #endif
86 snd_pcm_period_elapsed(pcm);
87 }
88
89 /* The memory size for pure pcm data is 128*1024 = 0x20000 bytes.
90 * The total rx/tx buffer is for ac97 frame to hold all pcm data
91 * is 0x20000 * sizeof(struct ac97_frame) / 4.
92 */
93 #ifdef CONFIG_SND_MMAP_SUPPORT
94 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
95 .info = SNDRV_PCM_INFO_INTERLEAVED |
96 SNDRV_PCM_INFO_MMAP |
97 SNDRV_PCM_INFO_MMAP_VALID |
98 SNDRV_PCM_INFO_BLOCK_TRANSFER,
99 #else
100 static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
101 .info = SNDRV_PCM_INFO_INTERLEAVED |
102 SNDRV_PCM_INFO_BLOCK_TRANSFER,
103 #endif
104 .formats = SNDRV_PCM_FMTBIT_S16_LE,
105 .period_bytes_min = 32,
106 .period_bytes_max = 0x10000,
107 .periods_min = 1,
108 .periods_max = PAGE_SIZE/32,
109 .buffer_bytes_max = 0x20000, /* 128 kbytes */
110 .fifo_size = 16,
111 };
112
113 static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
115 {
116 size_t size = bf5xx_pcm_hardware.buffer_bytes_max
117 * sizeof(struct ac97_frame) / 4;
118
119 snd_pcm_lib_malloc_pages(substream, size);
120
121 return 0;
122 }
123
124 static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
125 {
126 struct snd_pcm_runtime *runtime = substream->runtime;
127
128 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
129 memset(runtime->dma_area, 0, runtime->buffer_size);
130 snd_pcm_lib_free_pages(substream);
131 return 0;
132 }
133
134 static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
135 {
136 struct snd_pcm_runtime *runtime = substream->runtime;
137 struct sport_device *sport = runtime->private_data;
138
139 /* An intermediate buffer is introduced for implementing mmap for
140 * SPORT working in TMD mode(include AC97).
141 */
142 #if defined(CONFIG_SND_MMAP_SUPPORT)
143 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
144 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
145 sport_config_tx_dma(sport, sport->tx_dma_buf, runtime->periods,
146 runtime->period_size * sizeof(struct ac97_frame));
147 } else {
148 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
149 sport_config_rx_dma(sport, sport->rx_dma_buf, runtime->periods,
150 runtime->period_size * sizeof(struct ac97_frame));
151 }
152 #else
153 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
154 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
155 sport_config_tx_dma(sport, runtime->dma_area, runtime->periods,
156 runtime->period_size * sizeof(struct ac97_frame));
157 } else {
158 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
159 sport_config_rx_dma(sport, runtime->dma_area, runtime->periods,
160 runtime->period_size * sizeof(struct ac97_frame));
161 }
162 #endif
163 return 0;
164 }
165
166 static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
167 {
168 struct snd_pcm_runtime *runtime = substream->runtime;
169 struct sport_device *sport = runtime->private_data;
170 int ret = 0;
171
172 pr_debug("%s enter\n", __func__);
173 switch (cmd) {
174 case SNDRV_PCM_TRIGGER_START:
175 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
176 bf5xx_mmap_copy(substream, runtime->period_size);
177 snd_pcm_period_elapsed(substream);
178 sport->tx_delay_pos = 0;
179 sport_tx_start(sport);
180 }
181 else
182 sport_rx_start(sport);
183 break;
184 case SNDRV_PCM_TRIGGER_STOP:
185 case SNDRV_PCM_TRIGGER_SUSPEND:
186 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
187 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
188 #if defined(CONFIG_SND_MMAP_SUPPORT)
189 sport->tx_pos = 0;
190 #endif
191 sport_tx_stop(sport);
192 } else {
193 #if defined(CONFIG_SND_MMAP_SUPPORT)
194 sport->rx_pos = 0;
195 #endif
196 sport_rx_stop(sport);
197 }
198 break;
199 default:
200 ret = -EINVAL;
201 }
202 return ret;
203 }
204
205 static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
206 {
207 struct snd_pcm_runtime *runtime = substream->runtime;
208 struct sport_device *sport = runtime->private_data;
209 unsigned int curr;
210
211 #if defined(CONFIG_SND_MMAP_SUPPORT)
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213 curr = sport->tx_delay_pos;
214 else
215 curr = sport->rx_pos;
216 #else
217
218 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
219 curr = sport_curr_offset_tx(sport) / sizeof(struct ac97_frame);
220 else
221 curr = sport_curr_offset_rx(sport) / sizeof(struct ac97_frame);
222
223 #endif
224 return curr;
225 }
226
227 static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
228 {
229 struct snd_pcm_runtime *runtime = substream->runtime;
230 int ret;
231
232 pr_debug("%s enter\n", __func__);
233 snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
234
235 ret = snd_pcm_hw_constraint_integer(runtime,
236 SNDRV_PCM_HW_PARAM_PERIODS);
237 if (ret < 0)
238 goto out;
239
240 if (sport_handle != NULL)
241 runtime->private_data = sport_handle;
242 else {
243 pr_err("sport_handle is NULL\n");
244 return -1;
245 }
246 return 0;
247
248 out:
249 return ret;
250 }
251
252 static int bf5xx_pcm_close(struct snd_pcm_substream *substream)
253 {
254 struct snd_pcm_runtime *runtime = substream->runtime;
255 struct sport_device *sport = runtime->private_data;
256
257 pr_debug("%s enter\n", __func__);
258 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
259 sport->once = 0;
260 memset(sport->tx_dma_buf, 0, runtime->buffer_size * sizeof(struct ac97_frame));
261 } else
262 memset(sport->rx_dma_buf, 0, runtime->buffer_size * sizeof(struct ac97_frame));
263
264 return 0;
265 }
266
267 #ifdef CONFIG_SND_MMAP_SUPPORT
268 static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
269 struct vm_area_struct *vma)
270 {
271 struct snd_pcm_runtime *runtime = substream->runtime;
272 size_t size = vma->vm_end - vma->vm_start;
273 vma->vm_start = (unsigned long)runtime->dma_area;
274 vma->vm_end = vma->vm_start + size;
275 vma->vm_flags |= VM_SHARED;
276 return 0 ;
277 }
278 #else
279 static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
280 snd_pcm_uframes_t pos,
281 void __user *buf, snd_pcm_uframes_t count)
282 {
283 struct snd_pcm_runtime *runtime = substream->runtime;
284
285 pr_debug("%s copy pos:0x%lx count:0x%lx\n",
286 substream->stream ? "Capture" : "Playback", pos, count);
287
288 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
289 bf5xx_pcm_to_ac97(
290 (struct ac97_frame *)runtime->dma_area + pos,
291 buf, count);
292 else
293 bf5xx_ac97_to_pcm(
294 (struct ac97_frame *)runtime->dma_area + pos,
295 buf, count);
296 return 0;
297 }
298 #endif
299
300 struct snd_pcm_ops bf5xx_pcm_ac97_ops = {
301 .open = bf5xx_pcm_open,
302 .close = bf5xx_pcm_close,
303 .ioctl = snd_pcm_lib_ioctl,
304 .hw_params = bf5xx_pcm_hw_params,
305 .hw_free = bf5xx_pcm_hw_free,
306 .prepare = bf5xx_pcm_prepare,
307 .trigger = bf5xx_pcm_trigger,
308 .pointer = bf5xx_pcm_pointer,
309 #ifdef CONFIG_SND_MMAP_SUPPORT
310 .mmap = bf5xx_pcm_mmap,
311 #else
312 .copy = bf5xx_pcm_copy,
313 #endif
314 };
315
316 static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
317 {
318 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
319 struct snd_dma_buffer *buf = &substream->dma_buffer;
320 size_t size = bf5xx_pcm_hardware.buffer_bytes_max
321 * sizeof(struct ac97_frame) / 4;
322
323 buf->dev.type = SNDRV_DMA_TYPE_DEV;
324 buf->dev.dev = pcm->card->dev;
325 buf->private_data = NULL;
326 buf->area = dma_alloc_coherent(pcm->card->dev, size,
327 &buf->addr, GFP_KERNEL);
328 if (!buf->area) {
329 pr_err("Failed to allocate dma memory\n");
330 pr_err("Please increase uncached DMA memory region\n");
331 return -ENOMEM;
332 }
333 buf->bytes = size;
334
335 pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
336 buf->area, buf->bytes);
337
338 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
339 sport_handle->tx_buf = buf->area;
340 else
341 sport_handle->rx_buf = buf->area;
342
343 /*
344 * Need to allocate local buffer when enable
345 * MMAP for SPORT working in TMD mode (include AC97).
346 */
347 #if defined(CONFIG_SND_MMAP_SUPPORT)
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
349 if (!sport_handle->tx_dma_buf) {
350 sport_handle->tx_dma_buf = dma_alloc_coherent(NULL, \
351 size, &sport_handle->tx_dma_phy, GFP_KERNEL);
352 if (!sport_handle->tx_dma_buf) {
353 pr_err("Failed to allocate memory for tx dma \
354 buf - Please increase uncached DMA \
355 memory region\n");
356 return -ENOMEM;
357 } else
358 memset(sport_handle->tx_dma_buf, 0, size);
359 } else
360 memset(sport_handle->tx_dma_buf, 0, size);
361 } else {
362 if (!sport_handle->rx_dma_buf) {
363 sport_handle->rx_dma_buf = dma_alloc_coherent(NULL, \
364 size, &sport_handle->rx_dma_phy, GFP_KERNEL);
365 if (!sport_handle->rx_dma_buf) {
366 pr_err("Failed to allocate memory for rx dma \
367 buf - Please increase uncached DMA \
368 memory region\n");
369 return -ENOMEM;
370 } else
371 memset(sport_handle->rx_dma_buf, 0, size);
372 } else
373 memset(sport_handle->rx_dma_buf, 0, size);
374 }
375 #endif
376 return 0;
377 }
378
379 static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
380 {
381 struct snd_pcm_substream *substream;
382 struct snd_dma_buffer *buf;
383 int stream;
384 #if defined(CONFIG_SND_MMAP_SUPPORT)
385 size_t size = bf5xx_pcm_hardware.buffer_bytes_max *
386 sizeof(struct ac97_frame) / 4;
387 #endif
388 for (stream = 0; stream < 2; stream++) {
389 substream = pcm->streams[stream].substream;
390 if (!substream)
391 continue;
392
393 buf = &substream->dma_buffer;
394 if (!buf->area)
395 continue;
396 dma_free_coherent(NULL, buf->bytes, buf->area, 0);
397 buf->area = NULL;
398 #if defined(CONFIG_SND_MMAP_SUPPORT)
399 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
400 if (sport_handle->tx_dma_buf)
401 dma_free_coherent(NULL, size, \
402 sport_handle->tx_dma_buf, 0);
403 sport_handle->tx_dma_buf = NULL;
404 } else {
405
406 if (sport_handle->rx_dma_buf)
407 dma_free_coherent(NULL, size, \
408 sport_handle->rx_dma_buf, 0);
409 sport_handle->rx_dma_buf = NULL;
410 }
411 #endif
412 }
413 if (sport_handle)
414 sport_done(sport_handle);
415 }
416
417 static u64 bf5xx_pcm_dmamask = DMA_32BIT_MASK;
418
419 int bf5xx_pcm_ac97_new(struct snd_card *card, struct snd_soc_dai *dai,
420 struct snd_pcm *pcm)
421 {
422 int ret = 0;
423
424 pr_debug("%s enter\n", __func__);
425 if (!card->dev->dma_mask)
426 card->dev->dma_mask = &bf5xx_pcm_dmamask;
427 if (!card->dev->coherent_dma_mask)
428 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
429
430 if (dai->playback.channels_min) {
431 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
432 SNDRV_PCM_STREAM_PLAYBACK);
433 if (ret)
434 goto out;
435 }
436
437 if (dai->capture.channels_min) {
438 ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
439 SNDRV_PCM_STREAM_CAPTURE);
440 if (ret)
441 goto out;
442 }
443 out:
444 return ret;
445 }
446
447 struct snd_soc_platform bf5xx_ac97_soc_platform = {
448 .name = "bf5xx-audio",
449 .pcm_ops = &bf5xx_pcm_ac97_ops,
450 .pcm_new = bf5xx_pcm_ac97_new,
451 .pcm_free = bf5xx_pcm_free_dma_buffers,
452 };
453 EXPORT_SYMBOL_GPL(bf5xx_ac97_soc_platform);
454
455 MODULE_AUTHOR("Cliff Cai");
456 MODULE_DESCRIPTION("ADI Blackfin AC97 PCM DMA module");
457 MODULE_LICENSE("GPL");
This page took 0.04204 seconds and 6 git commands to generate.