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