]> Pileus Git - ~andy/linux/blob - sound/soc/omap/omap-pcm.c
Merge remote-tracking branch 'asoc/topic/wm8994' into asoc-next
[~andy/linux] / sound / soc / omap / omap-pcm.c
1 /*
2  * omap-pcm.c  --  ALSA PCM interface for the OMAP SoC
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
7  *          Peter Ujfalusi <peter.ujfalusi@ti.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/omap-dma.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/dmaengine_pcm.h>
33 #include <sound/soc.h>
34
35 #ifdef CONFIG_ARCH_OMAP1
36 #define pcm_omap1510()  cpu_is_omap1510()
37 #else
38 #define pcm_omap1510()  0
39 #endif
40
41 static const struct snd_pcm_hardware omap_pcm_hardware = {
42         .info                   = SNDRV_PCM_INFO_MMAP |
43                                   SNDRV_PCM_INFO_MMAP_VALID |
44                                   SNDRV_PCM_INFO_INTERLEAVED |
45                                   SNDRV_PCM_INFO_PAUSE |
46                                   SNDRV_PCM_INFO_RESUME |
47                                   SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
48         .formats                = SNDRV_PCM_FMTBIT_S16_LE |
49                                   SNDRV_PCM_FMTBIT_S32_LE,
50         .period_bytes_min       = 32,
51         .period_bytes_max       = 64 * 1024,
52         .periods_min            = 2,
53         .periods_max            = 255,
54         .buffer_bytes_max       = 128 * 1024,
55 };
56
57 /* this may get called several times by oss emulation */
58 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
59                               struct snd_pcm_hw_params *params)
60 {
61         struct snd_pcm_runtime *runtime = substream->runtime;
62         struct snd_soc_pcm_runtime *rtd = substream->private_data;
63         struct omap_pcm_dma_data *dma_data;
64         struct dma_slave_config config;
65         struct dma_chan *chan;
66         int err = 0;
67
68         dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
69
70         /* return if this is a bufferless transfer e.g.
71          * codec <--> BT codec or GSM modem -- lg FIXME */
72         if (!dma_data)
73                 return 0;
74
75         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
76         runtime->dma_bytes = params_buffer_bytes(params);
77
78         chan = snd_dmaengine_pcm_get_chan(substream);
79         if (!chan)
80                 return -EINVAL;
81
82         /* fills in addr_width and direction */
83         err = snd_hwparams_to_dma_slave_config(substream, params, &config);
84         if (err)
85                 return err;
86
87         snd_dmaengine_pcm_set_config_from_dai_data(substream,
88                         snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
89                         &config);
90
91         return dmaengine_slave_config(chan, &config);
92 }
93
94 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
95 {
96         snd_pcm_set_runtime_buffer(substream, NULL);
97         return 0;
98 }
99
100 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
101 {
102         snd_pcm_uframes_t offset;
103
104         if (pcm_omap1510())
105                 offset = snd_dmaengine_pcm_pointer_no_residue(substream);
106         else
107                 offset = snd_dmaengine_pcm_pointer(substream);
108
109         return offset;
110 }
111
112 static int omap_pcm_open(struct snd_pcm_substream *substream)
113 {
114         struct snd_soc_pcm_runtime *rtd = substream->private_data;
115         struct snd_dmaengine_dai_dma_data *dma_data;
116
117         snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
118
119         dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
120
121         return snd_dmaengine_pcm_open(substream, omap_dma_filter_fn,
122                                       dma_data->filter_data);
123 }
124
125 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
126         struct vm_area_struct *vma)
127 {
128         struct snd_pcm_runtime *runtime = substream->runtime;
129
130         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
131                                      runtime->dma_area,
132                                      runtime->dma_addr,
133                                      runtime->dma_bytes);
134 }
135
136 static struct snd_pcm_ops omap_pcm_ops = {
137         .open           = omap_pcm_open,
138         .close          = snd_dmaengine_pcm_close,
139         .ioctl          = snd_pcm_lib_ioctl,
140         .hw_params      = omap_pcm_hw_params,
141         .hw_free        = omap_pcm_hw_free,
142         .trigger        = snd_dmaengine_pcm_trigger,
143         .pointer        = omap_pcm_pointer,
144         .mmap           = omap_pcm_mmap,
145 };
146
147 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
148
149 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
150         int stream)
151 {
152         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
153         struct snd_dma_buffer *buf = &substream->dma_buffer;
154         size_t size = omap_pcm_hardware.buffer_bytes_max;
155
156         buf->dev.type = SNDRV_DMA_TYPE_DEV;
157         buf->dev.dev = pcm->card->dev;
158         buf->private_data = NULL;
159         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
160                                            &buf->addr, GFP_KERNEL);
161         if (!buf->area)
162                 return -ENOMEM;
163
164         buf->bytes = size;
165         return 0;
166 }
167
168 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
169 {
170         struct snd_pcm_substream *substream;
171         struct snd_dma_buffer *buf;
172         int stream;
173
174         for (stream = 0; stream < 2; stream++) {
175                 substream = pcm->streams[stream].substream;
176                 if (!substream)
177                         continue;
178
179                 buf = &substream->dma_buffer;
180                 if (!buf->area)
181                         continue;
182
183                 dma_free_writecombine(pcm->card->dev, buf->bytes,
184                                       buf->area, buf->addr);
185                 buf->area = NULL;
186         }
187 }
188
189 static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
190 {
191         struct snd_card *card = rtd->card->snd_card;
192         struct snd_pcm *pcm = rtd->pcm;
193         int ret = 0;
194
195         if (!card->dev->dma_mask)
196                 card->dev->dma_mask = &omap_pcm_dmamask;
197         if (!card->dev->coherent_dma_mask)
198                 card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
199
200         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
201                 ret = omap_pcm_preallocate_dma_buffer(pcm,
202                         SNDRV_PCM_STREAM_PLAYBACK);
203                 if (ret)
204                         goto out;
205         }
206
207         if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
208                 ret = omap_pcm_preallocate_dma_buffer(pcm,
209                         SNDRV_PCM_STREAM_CAPTURE);
210                 if (ret)
211                         goto out;
212         }
213
214 out:
215         /* free preallocated buffers in case of error */
216         if (ret)
217                 omap_pcm_free_dma_buffers(pcm);
218
219         return ret;
220 }
221
222 static struct snd_soc_platform_driver omap_soc_platform = {
223         .ops            = &omap_pcm_ops,
224         .pcm_new        = omap_pcm_new,
225         .pcm_free       = omap_pcm_free_dma_buffers,
226 };
227
228 static int omap_pcm_probe(struct platform_device *pdev)
229 {
230         return snd_soc_register_platform(&pdev->dev,
231                         &omap_soc_platform);
232 }
233
234 static int omap_pcm_remove(struct platform_device *pdev)
235 {
236         snd_soc_unregister_platform(&pdev->dev);
237         return 0;
238 }
239
240 static struct platform_driver omap_pcm_driver = {
241         .driver = {
242                         .name = "omap-pcm-audio",
243                         .owner = THIS_MODULE,
244         },
245
246         .probe = omap_pcm_probe,
247         .remove = omap_pcm_remove,
248 };
249
250 module_platform_driver(omap_pcm_driver);
251
252 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
253 MODULE_DESCRIPTION("OMAP PCM DMA module");
254 MODULE_LICENSE("GPL");
255 MODULE_ALIAS("platform:omap-pcm-audio");