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/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-ac97.h"
43 #include "bf5xx-sport.h"
45 static unsigned int ac97_chan_mask
[] = {
47 SP_STEREO
, /* Stereo */
49 SP_QUAD
,/*Quadraquic*/
50 SP_FL
| SP_FR
| SP_FC
| SP_SL
| SP_SR
,/*5 channels */
54 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
55 static void bf5xx_mmap_copy(struct snd_pcm_substream
*substream
,
56 snd_pcm_uframes_t count
)
58 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
59 struct sport_device
*sport
= runtime
->private_data
;
60 unsigned int chan_mask
= ac97_chan_mask
[runtime
->channels
- 1];
61 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
62 bf5xx_pcm_to_ac97((struct ac97_frame
*)sport
->tx_dma_buf
+
63 sport
->tx_pos
, (__u16
*)runtime
->dma_area
+ sport
->tx_pos
*
64 runtime
->channels
, count
, chan_mask
);
65 sport
->tx_pos
+= runtime
->period_size
;
66 if (sport
->tx_pos
>= runtime
->buffer_size
)
67 sport
->tx_pos
%= runtime
->buffer_size
;
68 sport
->tx_delay_pos
= sport
->tx_pos
;
70 bf5xx_ac97_to_pcm((struct ac97_frame
*)sport
->rx_dma_buf
+
71 sport
->rx_pos
, (__u16
*)runtime
->dma_area
+ sport
->rx_pos
*
72 runtime
->channels
, count
);
73 sport
->rx_pos
+= runtime
->period_size
;
74 if (sport
->rx_pos
>= runtime
->buffer_size
)
75 sport
->rx_pos
%= runtime
->buffer_size
;
80 static void bf5xx_dma_irq(void *data
)
82 struct snd_pcm_substream
*pcm
= data
;
83 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
84 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
85 struct sport_device
*sport
= runtime
->private_data
;
86 bf5xx_mmap_copy(pcm
, runtime
->period_size
);
87 if (pcm
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
88 if (sport
->once
== 0) {
89 snd_pcm_period_elapsed(pcm
);
90 bf5xx_mmap_copy(pcm
, runtime
->period_size
);
95 snd_pcm_period_elapsed(pcm
);
98 /* The memory size for pure pcm data is 128*1024 = 0x20000 bytes.
99 * The total rx/tx buffer is for ac97 frame to hold all pcm data
100 * is 0x20000 * sizeof(struct ac97_frame) / 4.
102 static const struct snd_pcm_hardware bf5xx_pcm_hardware
= {
103 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
104 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
105 SNDRV_PCM_INFO_MMAP
|
106 SNDRV_PCM_INFO_MMAP_VALID
|
108 SNDRV_PCM_INFO_BLOCK_TRANSFER
,
110 .period_bytes_min
= 32,
111 .period_bytes_max
= 0x10000,
113 .periods_max
= PAGE_SIZE
/32,
114 .buffer_bytes_max
= 0x20000, /* 128 kbytes */
118 static int bf5xx_pcm_hw_params(struct snd_pcm_substream
*substream
,
119 struct snd_pcm_hw_params
*params
)
121 size_t size
= bf5xx_pcm_hardware
.buffer_bytes_max
122 * sizeof(struct ac97_frame
) / 4;
124 snd_pcm_lib_malloc_pages(substream
, size
);
129 static int bf5xx_pcm_hw_free(struct snd_pcm_substream
*substream
)
131 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
132 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
133 struct sport_device
*sport
= runtime
->private_data
;
135 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
137 if (runtime
->dma_area
)
138 memset(runtime
->dma_area
, 0, runtime
->buffer_size
);
139 memset(sport
->tx_dma_buf
, 0, runtime
->buffer_size
*
140 sizeof(struct ac97_frame
));
142 memset(sport
->rx_dma_buf
, 0, runtime
->buffer_size
*
143 sizeof(struct ac97_frame
));
145 snd_pcm_lib_free_pages(substream
);
149 static int bf5xx_pcm_prepare(struct snd_pcm_substream
*substream
)
151 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
152 struct sport_device
*sport
= runtime
->private_data
;
154 /* An intermediate buffer is introduced for implementing mmap for
155 * SPORT working in TMD mode(include AC97).
157 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
158 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
159 sport_set_tx_callback(sport
, bf5xx_dma_irq
, substream
);
160 sport_config_tx_dma(sport
, sport
->tx_dma_buf
, runtime
->periods
,
161 runtime
->period_size
* sizeof(struct ac97_frame
));
163 sport_set_rx_callback(sport
, bf5xx_dma_irq
, substream
);
164 sport_config_rx_dma(sport
, sport
->rx_dma_buf
, runtime
->periods
,
165 runtime
->period_size
* sizeof(struct ac97_frame
));
168 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
169 sport_set_tx_callback(sport
, bf5xx_dma_irq
, substream
);
170 sport_config_tx_dma(sport
, runtime
->dma_area
, runtime
->periods
,
171 runtime
->period_size
* sizeof(struct ac97_frame
));
173 sport_set_rx_callback(sport
, bf5xx_dma_irq
, substream
);
174 sport_config_rx_dma(sport
, runtime
->dma_area
, runtime
->periods
,
175 runtime
->period_size
* sizeof(struct ac97_frame
));
181 static int bf5xx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
183 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
184 struct sport_device
*sport
= runtime
->private_data
;
187 pr_debug("%s enter\n", __func__
);
189 case SNDRV_PCM_TRIGGER_START
:
190 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
191 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
192 bf5xx_mmap_copy(substream
, runtime
->period_size
);
193 sport
->tx_delay_pos
= 0;
195 sport_tx_start(sport
);
197 sport_rx_start(sport
);
199 case SNDRV_PCM_TRIGGER_STOP
:
200 case SNDRV_PCM_TRIGGER_SUSPEND
:
201 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
202 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
203 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
206 sport_tx_stop(sport
);
208 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
211 sport_rx_stop(sport
);
220 static snd_pcm_uframes_t
bf5xx_pcm_pointer(struct snd_pcm_substream
*substream
)
222 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
223 struct sport_device
*sport
= runtime
->private_data
;
226 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
227 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
228 curr
= sport
->tx_delay_pos
;
230 curr
= sport
->rx_pos
;
233 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
234 curr
= sport_curr_offset_tx(sport
) / sizeof(struct ac97_frame
);
236 curr
= sport_curr_offset_rx(sport
) / sizeof(struct ac97_frame
);
242 static int bf5xx_pcm_open(struct snd_pcm_substream
*substream
)
244 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
245 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
246 struct sport_device
*sport_handle
= snd_soc_dai_get_drvdata(cpu_dai
);
247 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
250 pr_debug("%s enter\n", __func__
);
251 snd_soc_set_runtime_hwparams(substream
, &bf5xx_pcm_hardware
);
253 ret
= snd_pcm_hw_constraint_integer(runtime
,
254 SNDRV_PCM_HW_PARAM_PERIODS
);
258 if (sport_handle
!= NULL
)
259 runtime
->private_data
= sport_handle
;
261 pr_err("sport_handle is NULL\n");
270 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
271 static int bf5xx_pcm_mmap(struct snd_pcm_substream
*substream
,
272 struct vm_area_struct
*vma
)
274 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
275 size_t size
= vma
->vm_end
- vma
->vm_start
;
276 vma
->vm_start
= (unsigned long)runtime
->dma_area
;
277 vma
->vm_end
= vma
->vm_start
+ size
;
278 vma
->vm_flags
|= VM_SHARED
;
282 static int bf5xx_pcm_copy(struct snd_pcm_substream
*substream
,
283 int channel
, unsigned long pos
,
284 void *buf
, unsigned long count
)
286 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
287 unsigned int chan_mask
= ac97_chan_mask
[runtime
->channels
- 1];
288 struct ac97_frame
*dst
;
290 pr_debug("%s copy pos:0x%lx count:0x%lx\n",
291 substream
->stream
? "Capture" : "Playback", pos
, count
);
292 dst
= (struct ac97_frame
*)runtime
->dma_area
+
293 bytes_to_frames(runtime
, pos
);
294 count
= bytes_to_frames(runtime
, count
);
296 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
297 bf5xx_pcm_to_ac97(dst
, buf
, count
, chan_mask
);
299 bf5xx_ac97_to_pcm(dst
, buf
, count
);
303 static int bf5xx_pcm_copy_user(struct snd_pcm_substream
*substream
,
304 int channel
, unsigned long pos
,
305 void __user
*buf
, unsigned long count
)
307 return bf5xx_pcm_copy(substream
, channel
, pos
, (void *)buf
, count
);
311 static const struct snd_pcm_ops bf5xx_pcm_ac97_ops
= {
312 .open
= bf5xx_pcm_open
,
313 .ioctl
= snd_pcm_lib_ioctl
,
314 .hw_params
= bf5xx_pcm_hw_params
,
315 .hw_free
= bf5xx_pcm_hw_free
,
316 .prepare
= bf5xx_pcm_prepare
,
317 .trigger
= bf5xx_pcm_trigger
,
318 .pointer
= bf5xx_pcm_pointer
,
319 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
320 .mmap
= bf5xx_pcm_mmap
,
322 .copy_user
= bf5xx_pcm_copy_user
,
323 .copy_kernel
= bf5xx_pcm_copy
,
327 static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm
*pcm
, int stream
)
329 struct snd_soc_pcm_runtime
*rtd
= pcm
->private_data
;
330 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
331 struct sport_device
*sport_handle
= snd_soc_dai_get_drvdata(cpu_dai
);
332 struct snd_pcm_substream
*substream
= pcm
->streams
[stream
].substream
;
333 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
334 size_t size
= bf5xx_pcm_hardware
.buffer_bytes_max
335 * sizeof(struct ac97_frame
) / 4;
337 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
338 buf
->dev
.dev
= pcm
->card
->dev
;
339 buf
->private_data
= NULL
;
340 buf
->area
= dma_alloc_coherent(pcm
->card
->dev
, size
,
341 &buf
->addr
, GFP_KERNEL
);
343 pr_err("Failed to allocate dma memory\n");
344 pr_err("Please increase uncached DMA memory region\n");
349 pr_debug("%s, area:%p, size:0x%08lx\n", __func__
,
350 buf
->area
, buf
->bytes
);
352 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
353 sport_handle
->tx_buf
= buf
->area
;
355 sport_handle
->rx_buf
= buf
->area
;
358 * Need to allocate local buffer when enable
359 * MMAP for SPORT working in TMD mode (include AC97).
361 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
362 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
363 if (!sport_handle
->tx_dma_buf
) {
364 sport_handle
->tx_dma_buf
= dma_alloc_coherent(NULL
, \
365 size
, &sport_handle
->tx_dma_phy
, GFP_KERNEL
);
366 if (!sport_handle
->tx_dma_buf
) {
367 pr_err("Failed to allocate memory for tx dma buf - Please increase uncached DMA memory region\n");
370 memset(sport_handle
->tx_dma_buf
, 0, size
);
372 memset(sport_handle
->tx_dma_buf
, 0, size
);
374 if (!sport_handle
->rx_dma_buf
) {
375 sport_handle
->rx_dma_buf
= dma_alloc_coherent(NULL
, \
376 size
, &sport_handle
->rx_dma_phy
, GFP_KERNEL
);
377 if (!sport_handle
->rx_dma_buf
) {
378 pr_err("Failed to allocate memory for rx dma buf - Please increase uncached DMA memory region\n");
381 memset(sport_handle
->rx_dma_buf
, 0, size
);
383 memset(sport_handle
->rx_dma_buf
, 0, size
);
389 static void bf5xx_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
391 struct snd_pcm_substream
*substream
;
392 struct snd_dma_buffer
*buf
;
394 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
395 struct snd_soc_pcm_runtime
*rtd
= pcm
->private_data
;
396 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
397 struct sport_device
*sport_handle
= snd_soc_dai_get_drvdata(cpu_dai
);
398 size_t size
= bf5xx_pcm_hardware
.buffer_bytes_max
*
399 sizeof(struct ac97_frame
) / 4;
401 for (stream
= 0; stream
< 2; stream
++) {
402 substream
= pcm
->streams
[stream
].substream
;
406 buf
= &substream
->dma_buffer
;
409 dma_free_coherent(NULL
, buf
->bytes
, buf
->area
, 0);
411 #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
412 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
413 if (sport_handle
->tx_dma_buf
)
414 dma_free_coherent(NULL
, size
, \
415 sport_handle
->tx_dma_buf
, 0);
416 sport_handle
->tx_dma_buf
= NULL
;
419 if (sport_handle
->rx_dma_buf
)
420 dma_free_coherent(NULL
, size
, \
421 sport_handle
->rx_dma_buf
, 0);
422 sport_handle
->rx_dma_buf
= NULL
;
428 static int bf5xx_pcm_ac97_new(struct snd_soc_pcm_runtime
*rtd
)
430 struct snd_card
*card
= rtd
->card
->snd_card
;
431 struct snd_pcm
*pcm
= rtd
->pcm
;
434 pr_debug("%s enter\n", __func__
);
435 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
439 if (pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
) {
440 ret
= bf5xx_pcm_preallocate_dma_buffer(pcm
,
441 SNDRV_PCM_STREAM_PLAYBACK
);
446 if (pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
) {
447 ret
= bf5xx_pcm_preallocate_dma_buffer(pcm
,
448 SNDRV_PCM_STREAM_CAPTURE
);
456 static struct snd_soc_platform_driver bf5xx_ac97_soc_platform
= {
457 .ops
= &bf5xx_pcm_ac97_ops
,
458 .pcm_new
= bf5xx_pcm_ac97_new
,
459 .pcm_free
= bf5xx_pcm_free_dma_buffers
,
462 static int bf5xx_soc_platform_probe(struct platform_device
*pdev
)
464 return devm_snd_soc_register_platform(&pdev
->dev
,
465 &bf5xx_ac97_soc_platform
);
468 static struct platform_driver bf5xx_pcm_driver
= {
470 .name
= "bfin-ac97-pcm-audio",
473 .probe
= bf5xx_soc_platform_probe
,
476 module_platform_driver(bf5xx_pcm_driver
);
478 MODULE_AUTHOR("Cliff Cai");
479 MODULE_DESCRIPTION("ADI Blackfin AC97 PCM DMA module");
480 MODULE_LICENSE("GPL");