]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/mxs/mxs-pcm.c
ASoC: mxs-pcm: Remove unused fields from struct mxs_pcm_runtime_data
[karo-tx-linux.git] / sound / soc / mxs / mxs-pcm.c
1 /*
2  * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  * Based on sound/soc/imx/imx-pcm-dma-mx2.c
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/dmaengine.h>
31
32 #include <sound/core.h>
33 #include <sound/initval.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37
38 #include <mach/dma.h>
39 #include "mxs-pcm.h"
40
41 static struct snd_pcm_hardware snd_mxs_hardware = {
42         .info                   = SNDRV_PCM_INFO_MMAP |
43                                   SNDRV_PCM_INFO_MMAP_VALID |
44                                   SNDRV_PCM_INFO_PAUSE |
45                                   SNDRV_PCM_INFO_RESUME |
46                                   SNDRV_PCM_INFO_INTERLEAVED,
47         .formats                = SNDRV_PCM_FMTBIT_S16_LE |
48                                   SNDRV_PCM_FMTBIT_S20_3LE |
49                                   SNDRV_PCM_FMTBIT_S24_LE,
50         .channels_min           = 2,
51         .channels_max           = 2,
52         .period_bytes_min       = 32,
53         .period_bytes_max       = 8192,
54         .periods_min            = 1,
55         .periods_max            = 52,
56         .buffer_bytes_max       = 64 * 1024,
57         .fifo_size              = 32,
58
59 };
60
61 static void audio_dma_irq(void *data)
62 {
63         struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
64         struct snd_pcm_runtime *runtime = substream->runtime;
65         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
66
67         iprtd->offset += iprtd->period_bytes;
68         iprtd->offset %= iprtd->period_bytes * iprtd->periods;
69         snd_pcm_period_elapsed(substream);
70 }
71
72 static bool filter(struct dma_chan *chan, void *param)
73 {
74         struct mxs_pcm_runtime_data *iprtd = param;
75         struct mxs_pcm_dma_params *dma_params = iprtd->dma_params;
76
77         if (!mxs_dma_is_apbx(chan))
78                 return false;
79
80         if (chan->chan_id != dma_params->chan_num)
81                 return false;
82
83         chan->private = &iprtd->dma_data;
84
85         return true;
86 }
87
88 static int mxs_dma_alloc(struct snd_pcm_substream *substream,
89                                 struct snd_pcm_hw_params *params)
90 {
91         struct snd_soc_pcm_runtime *rtd = substream->private_data;
92         struct snd_pcm_runtime *runtime = substream->runtime;
93         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
94         dma_cap_mask_t mask;
95
96         iprtd->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
97
98         dma_cap_zero(mask);
99         dma_cap_set(DMA_SLAVE, mask);
100         iprtd->dma_data.chan_irq = iprtd->dma_params->chan_irq;
101         iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
102         if (!iprtd->dma_chan)
103                 return -EINVAL;
104
105         return 0;
106 }
107
108 static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
109                                 struct snd_pcm_hw_params *params)
110 {
111         struct snd_pcm_runtime *runtime = substream->runtime;
112         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
113         unsigned long dma_addr;
114         struct dma_chan *chan;
115         int ret;
116
117         ret = mxs_dma_alloc(substream, params);
118         if (ret)
119                 return ret;
120         chan = iprtd->dma_chan;
121
122         iprtd->periods = params_periods(params);
123         iprtd->period_bytes = params_period_bytes(params);
124         iprtd->offset = 0;
125
126         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
127
128         dma_addr = runtime->dma_addr;
129
130         iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
131                         iprtd->period_bytes * iprtd->periods,
132                         iprtd->period_bytes,
133                         substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
134                         DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
135         if (!iprtd->desc) {
136                 dev_err(&chan->dev->device, "cannot prepare slave dma\n");
137                 return -EINVAL;
138         }
139
140         iprtd->desc->callback = audio_dma_irq;
141         iprtd->desc->callback_param = substream;
142
143         return 0;
144 }
145
146 static int snd_mxs_pcm_hw_free(struct snd_pcm_substream *substream)
147 {
148         struct snd_pcm_runtime *runtime = substream->runtime;
149         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
150
151         if (iprtd->dma_chan) {
152                 dma_release_channel(iprtd->dma_chan);
153                 iprtd->dma_chan = NULL;
154         }
155
156         return 0;
157 }
158
159 static int snd_mxs_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
160 {
161         struct snd_pcm_runtime *runtime = substream->runtime;
162         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
163
164         switch (cmd) {
165         case SNDRV_PCM_TRIGGER_START:
166         case SNDRV_PCM_TRIGGER_RESUME:
167         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
168                 dmaengine_submit(iprtd->desc);
169
170                 break;
171         case SNDRV_PCM_TRIGGER_STOP:
172         case SNDRV_PCM_TRIGGER_SUSPEND:
173         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
174                 dmaengine_terminate_all(iprtd->dma_chan);
175
176                 break;
177         default:
178                 return -EINVAL;
179         }
180
181         return 0;
182 }
183
184 static snd_pcm_uframes_t snd_mxs_pcm_pointer(
185                 struct snd_pcm_substream *substream)
186 {
187         struct snd_pcm_runtime *runtime = substream->runtime;
188         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
189
190         return bytes_to_frames(substream->runtime, iprtd->offset);
191 }
192
193 static int snd_mxs_open(struct snd_pcm_substream *substream)
194 {
195         struct snd_pcm_runtime *runtime = substream->runtime;
196         struct mxs_pcm_runtime_data *iprtd;
197         int ret;
198
199         iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
200         if (iprtd == NULL)
201                 return -ENOMEM;
202         runtime->private_data = iprtd;
203
204         ret = snd_pcm_hw_constraint_integer(substream->runtime,
205                         SNDRV_PCM_HW_PARAM_PERIODS);
206         if (ret < 0) {
207                 kfree(iprtd);
208                 return ret;
209         }
210
211         snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
212
213         return 0;
214 }
215
216 static int snd_mxs_close(struct snd_pcm_substream *substream)
217 {
218         struct snd_pcm_runtime *runtime = substream->runtime;
219         struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
220
221         kfree(iprtd);
222
223         return 0;
224 }
225
226 static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
227                 struct vm_area_struct *vma)
228 {
229         struct snd_pcm_runtime *runtime = substream->runtime;
230
231         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
232                                         runtime->dma_area,
233                                         runtime->dma_addr,
234                                         runtime->dma_bytes);
235 }
236
237 static struct snd_pcm_ops mxs_pcm_ops = {
238         .open           = snd_mxs_open,
239         .close          = snd_mxs_close,
240         .ioctl          = snd_pcm_lib_ioctl,
241         .hw_params      = snd_mxs_pcm_hw_params,
242         .hw_free        = snd_mxs_pcm_hw_free,
243         .trigger        = snd_mxs_pcm_trigger,
244         .pointer        = snd_mxs_pcm_pointer,
245         .mmap           = snd_mxs_pcm_mmap,
246 };
247
248 static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
249 {
250         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
251         struct snd_dma_buffer *buf = &substream->dma_buffer;
252         size_t size = snd_mxs_hardware.buffer_bytes_max;
253
254         buf->dev.type = SNDRV_DMA_TYPE_DEV;
255         buf->dev.dev = pcm->card->dev;
256         buf->private_data = NULL;
257         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
258                                            &buf->addr, GFP_KERNEL);
259         if (!buf->area)
260                 return -ENOMEM;
261         buf->bytes = size;
262
263         return 0;
264 }
265
266 static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
267 static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
268 {
269         struct snd_card *card = rtd->card->snd_card;
270         struct snd_pcm *pcm = rtd->pcm;
271         int ret = 0;
272
273         if (!card->dev->dma_mask)
274                 card->dev->dma_mask = &mxs_pcm_dmamask;
275         if (!card->dev->coherent_dma_mask)
276                 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
277
278         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
279                 ret = mxs_pcm_preallocate_dma_buffer(pcm,
280                         SNDRV_PCM_STREAM_PLAYBACK);
281                 if (ret)
282                         goto out;
283         }
284
285         if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
286                 ret = mxs_pcm_preallocate_dma_buffer(pcm,
287                         SNDRV_PCM_STREAM_CAPTURE);
288                 if (ret)
289                         goto out;
290         }
291
292 out:
293         return ret;
294 }
295
296 static void mxs_pcm_free(struct snd_pcm *pcm)
297 {
298         struct snd_pcm_substream *substream;
299         struct snd_dma_buffer *buf;
300         int stream;
301
302         for (stream = 0; stream < 2; stream++) {
303                 substream = pcm->streams[stream].substream;
304                 if (!substream)
305                         continue;
306
307                 buf = &substream->dma_buffer;
308                 if (!buf->area)
309                         continue;
310
311                 dma_free_writecombine(pcm->card->dev, buf->bytes,
312                                       buf->area, buf->addr);
313                 buf->area = NULL;
314         }
315 }
316
317 static struct snd_soc_platform_driver mxs_soc_platform = {
318         .ops            = &mxs_pcm_ops,
319         .pcm_new        = mxs_pcm_new,
320         .pcm_free       = mxs_pcm_free,
321 };
322
323 static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
324 {
325         return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
326 }
327
328 static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
329 {
330         snd_soc_unregister_platform(&pdev->dev);
331
332         return 0;
333 }
334
335 static struct platform_driver mxs_pcm_driver = {
336         .driver = {
337                 .name = "mxs-pcm-audio",
338                 .owner = THIS_MODULE,
339         },
340         .probe = mxs_soc_platform_probe,
341         .remove = __devexit_p(mxs_soc_platform_remove),
342 };
343
344 module_platform_driver(mxs_pcm_driver);
345
346 MODULE_LICENSE("GPL");
347 MODULE_ALIAS("platform:mxs-pcm-audio");