Merge branch 'broonie/spi-next' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / fsl / imx-pcm.c
1 /*
2 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
3 *
4 * This code is based on code copyrighted by Freescale,
5 * Liam Girdwood, Javier Martin and probably others.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12
13 #include <linux/dma-mapping.h>
14 #include <linux/module.h>
15 #include <sound/pcm.h>
16 #include <sound/soc.h>
17 #include "imx-pcm.h"
18
19 int snd_imx_pcm_mmap(struct snd_pcm_substream *substream,
20 struct vm_area_struct *vma)
21 {
22 struct snd_pcm_runtime *runtime = substream->runtime;
23 int ret;
24
25 ret = dma_mmap_writecombine(substream->pcm->card->dev, vma,
26 runtime->dma_area, runtime->dma_addr, runtime->dma_bytes);
27
28 pr_debug("%s: ret: %d %p 0x%08x 0x%08x\n", __func__, ret,
29 runtime->dma_area,
30 runtime->dma_addr,
31 runtime->dma_bytes);
32 return ret;
33 }
34
35 static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
36 {
37 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
38 struct snd_dma_buffer *buf = &substream->dma_buffer;
39 size_t size = IMX_SSI_DMABUF_SIZE;
40
41 buf->dev.type = SNDRV_DMA_TYPE_DEV;
42 buf->dev.dev = pcm->card->dev;
43 buf->private_data = NULL;
44 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
45 &buf->addr, GFP_KERNEL);
46 if (!buf->area)
47 return -ENOMEM;
48 buf->bytes = size;
49
50 return 0;
51 }
52
53 static u64 imx_pcm_dmamask = DMA_BIT_MASK(32);
54
55 int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
56 {
57 struct snd_card *card = rtd->card->snd_card;
58 struct snd_pcm *pcm = rtd->pcm;
59 int ret = 0;
60
61 if (!card->dev->dma_mask)
62 card->dev->dma_mask = &imx_pcm_dmamask;
63 if (!card->dev->coherent_dma_mask)
64 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
65 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
66 ret = imx_pcm_preallocate_dma_buffer(pcm,
67 SNDRV_PCM_STREAM_PLAYBACK);
68 if (ret)
69 goto out;
70 }
71
72 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
73 ret = imx_pcm_preallocate_dma_buffer(pcm,
74 SNDRV_PCM_STREAM_CAPTURE);
75 if (ret)
76 goto out;
77 }
78
79 out:
80 return ret;
81 }
82
83 void imx_pcm_free(struct snd_pcm *pcm)
84 {
85 struct snd_pcm_substream *substream;
86 struct snd_dma_buffer *buf;
87 int stream;
88
89 for (stream = 0; stream < 2; stream++) {
90 substream = pcm->streams[stream].substream;
91 if (!substream)
92 continue;
93
94 buf = &substream->dma_buffer;
95 if (!buf->area)
96 continue;
97
98 dma_free_writecombine(pcm->card->dev, buf->bytes,
99 buf->area, buf->addr);
100 buf->area = NULL;
101 }
102 }
103
104 MODULE_DESCRIPTION("Freescale i.MX PCM driver");
105 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
106 MODULE_LICENSE("GPL");