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"
43 #include "bf5xx-i2s-pcm.h"
45 static void bf5xx_dma_irq(void *data
)
47 struct snd_pcm_substream
*pcm
= data
;
48 snd_pcm_period_elapsed(pcm
);
51 static const struct snd_pcm_hardware bf5xx_pcm_hardware
= {
52 .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 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
70 unsigned int buffer_size
= params_buffer_bytes(params
);
71 struct bf5xx_i2s_pcm_data
*dma_data
;
73 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
75 if (dma_data
->tdm_mode
)
76 buffer_size
= buffer_size
/ params_channels(params
) * 8;
78 return snd_pcm_lib_malloc_pages(substream
, buffer_size
);
81 static int bf5xx_pcm_hw_free(struct snd_pcm_substream
*substream
)
83 snd_pcm_lib_free_pages(substream
);
88 static int bf5xx_pcm_prepare(struct snd_pcm_substream
*substream
)
90 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
91 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
92 struct sport_device
*sport
= runtime
->private_data
;
93 int period_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
94 struct bf5xx_i2s_pcm_data
*dma_data
;
96 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
98 if (dma_data
->tdm_mode
)
99 period_bytes
= period_bytes
/ runtime
->channels
* 8;
101 pr_debug("%s enter\n", __func__
);
102 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
103 sport_set_tx_callback(sport
, bf5xx_dma_irq
, substream
);
104 sport_config_tx_dma(sport
, runtime
->dma_area
,
105 runtime
->periods
, period_bytes
);
107 sport_set_rx_callback(sport
, bf5xx_dma_irq
, substream
);
108 sport_config_rx_dma(sport
, runtime
->dma_area
,
109 runtime
->periods
, period_bytes
);
115 static int bf5xx_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
117 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
118 struct sport_device
*sport
= runtime
->private_data
;
121 pr_debug("%s enter\n", __func__
);
123 case SNDRV_PCM_TRIGGER_START
:
124 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
125 sport_tx_start(sport
);
127 sport_rx_start(sport
);
129 case SNDRV_PCM_TRIGGER_STOP
:
130 case SNDRV_PCM_TRIGGER_SUSPEND
:
131 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
132 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
133 sport_tx_stop(sport
);
135 sport_rx_stop(sport
);
144 static snd_pcm_uframes_t
bf5xx_pcm_pointer(struct snd_pcm_substream
*substream
)
146 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
147 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
148 struct sport_device
*sport
= runtime
->private_data
;
150 snd_pcm_uframes_t frames
;
151 struct bf5xx_i2s_pcm_data
*dma_data
;
153 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
155 pr_debug("%s enter\n", __func__
);
156 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
157 diff
= sport_curr_offset_tx(sport
);
159 diff
= sport_curr_offset_rx(sport
);
163 * TX at least can report one frame beyond the end of the
164 * buffer if we hit the wraparound case - clamp to within the
165 * buffer as the ALSA APIs require.
167 if (diff
== snd_pcm_lib_buffer_bytes(substream
))
170 frames
= bytes_to_frames(substream
->runtime
, diff
);
171 if (dma_data
->tdm_mode
)
172 frames
= frames
* runtime
->channels
/ 8;
177 static int bf5xx_pcm_open(struct snd_pcm_substream
*substream
)
179 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
180 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
181 struct sport_device
*sport_handle
= snd_soc_dai_get_drvdata(cpu_dai
);
182 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
183 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
184 struct bf5xx_i2s_pcm_data
*dma_data
;
187 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
189 pr_debug("%s enter\n", __func__
);
191 snd_soc_set_runtime_hwparams(substream
, &bf5xx_pcm_hardware
);
192 if (dma_data
->tdm_mode
)
193 runtime
->hw
.buffer_bytes_max
/= 4;
195 runtime
->hw
.info
|= SNDRV_PCM_INFO_MMAP
;
197 ret
= snd_pcm_hw_constraint_integer(runtime
,
198 SNDRV_PCM_HW_PARAM_PERIODS
);
202 if (sport_handle
!= NULL
) {
203 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
204 sport_handle
->tx_buf
= buf
->area
;
206 sport_handle
->rx_buf
= buf
->area
;
208 runtime
->private_data
= sport_handle
;
210 pr_err("sport_handle is NULL\n");
219 static int bf5xx_pcm_mmap(struct snd_pcm_substream
*substream
,
220 struct vm_area_struct
*vma
)
222 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
223 size_t size
= vma
->vm_end
- vma
->vm_start
;
224 vma
->vm_start
= (unsigned long)runtime
->dma_area
;
225 vma
->vm_end
= vma
->vm_start
+ size
;
226 vma
->vm_flags
|= VM_SHARED
;
231 static int bf5xx_pcm_copy(struct snd_pcm_substream
*substream
, int channel
,
232 snd_pcm_uframes_t pos
, void *buf
, snd_pcm_uframes_t count
)
234 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
235 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
236 unsigned int sample_size
= runtime
->sample_bits
/ 8;
237 struct bf5xx_i2s_pcm_data
*dma_data
;
241 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
243 if (dma_data
->tdm_mode
) {
244 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
246 dst
= runtime
->dma_area
;
247 dst
+= pos
* sample_size
* 8;
250 for (i
= 0; i
< runtime
->channels
; i
++) {
251 memcpy(dst
+ dma_data
->map
[i
] *
252 sample_size
, src
, sample_size
);
255 dst
+= 8 * sample_size
;
258 src
= runtime
->dma_area
;
259 src
+= pos
* sample_size
* 8;
263 for (i
= 0; i
< runtime
->channels
; i
++) {
264 memcpy(dst
, src
+ dma_data
->map
[i
] *
265 sample_size
, sample_size
);
268 src
+= 8 * sample_size
;
272 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
274 dst
= runtime
->dma_area
;
275 dst
+= frames_to_bytes(runtime
, pos
);
277 src
= runtime
->dma_area
;
278 src
+= frames_to_bytes(runtime
, pos
);
282 memcpy(dst
, src
, frames_to_bytes(runtime
, count
));
288 static int bf5xx_pcm_silence(struct snd_pcm_substream
*substream
,
289 int channel
, snd_pcm_uframes_t pos
, snd_pcm_uframes_t count
)
291 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
292 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
293 unsigned int sample_size
= runtime
->sample_bits
/ 8;
294 void *buf
= runtime
->dma_area
;
295 struct bf5xx_i2s_pcm_data
*dma_data
;
296 unsigned int offset
, size
;
298 dma_data
= snd_soc_dai_get_dma_data(rtd
->cpu_dai
, substream
);
300 if (dma_data
->tdm_mode
) {
301 offset
= pos
* 8 * sample_size
;
302 size
= count
* 8 * sample_size
;
304 offset
= frames_to_bytes(runtime
, pos
);
305 size
= frames_to_bytes(runtime
, count
);
308 snd_pcm_format_set_silence(runtime
->format
, buf
+ offset
, size
);
313 static struct snd_pcm_ops bf5xx_pcm_i2s_ops
= {
314 .open
= bf5xx_pcm_open
,
315 .ioctl
= snd_pcm_lib_ioctl
,
316 .hw_params
= bf5xx_pcm_hw_params
,
317 .hw_free
= bf5xx_pcm_hw_free
,
318 .prepare
= bf5xx_pcm_prepare
,
319 .trigger
= bf5xx_pcm_trigger
,
320 .pointer
= bf5xx_pcm_pointer
,
321 .mmap
= bf5xx_pcm_mmap
,
322 .copy
= bf5xx_pcm_copy
,
323 .silence
= bf5xx_pcm_silence
,
326 static int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime
*rtd
)
328 struct snd_card
*card
= rtd
->card
->snd_card
;
329 size_t size
= bf5xx_pcm_hardware
.buffer_bytes_max
;
332 pr_debug("%s enter\n", __func__
);
333 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
337 return snd_pcm_lib_preallocate_pages_for_all(rtd
->pcm
,
338 SNDRV_DMA_TYPE_DEV
, card
->dev
, size
, size
);
341 static struct snd_soc_platform_driver bf5xx_i2s_soc_platform
= {
342 .ops
= &bf5xx_pcm_i2s_ops
,
343 .pcm_new
= bf5xx_pcm_i2s_new
,
346 static int bfin_i2s_soc_platform_probe(struct platform_device
*pdev
)
348 return snd_soc_register_platform(&pdev
->dev
, &bf5xx_i2s_soc_platform
);
351 static int bfin_i2s_soc_platform_remove(struct platform_device
*pdev
)
353 snd_soc_unregister_platform(&pdev
->dev
);
357 static struct platform_driver bfin_i2s_pcm_driver
= {
359 .name
= "bfin-i2s-pcm-audio",
360 .owner
= THIS_MODULE
,
363 .probe
= bfin_i2s_soc_platform_probe
,
364 .remove
= bfin_i2s_soc_platform_remove
,
367 module_platform_driver(bfin_i2s_pcm_driver
);
369 MODULE_AUTHOR("Cliff Cai");
370 MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
371 MODULE_LICENSE("GPL");