Merge branch 'for-3.14' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[linux-2.6-block.git] / sound / soc / pxa / mmp-pcm.c
1 /*
2  * linux/sound/soc/pxa/mmp-pcm.c
3  *
4  * Copyright (C) 2011 Marvell International Ltd.
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  */
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmaengine.h>
18 #include <linux/platform_data/dma-mmp_tdma.h>
19 #include <linux/platform_data/mmp_audio.h>
20
21 #include <sound/pxa2xx-lib.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/dmaengine_pcm.h>
27
28 struct mmp_dma_data {
29         int ssp_id;
30         struct resource *dma_res;
31 };
32
33 #define MMP_PCM_INFO (SNDRV_PCM_INFO_MMAP |     \
34                 SNDRV_PCM_INFO_MMAP_VALID |     \
35                 SNDRV_PCM_INFO_INTERLEAVED |    \
36                 SNDRV_PCM_INFO_PAUSE |          \
37                 SNDRV_PCM_INFO_RESUME)
38
39 static struct snd_pcm_hardware mmp_pcm_hardware[] = {
40         {
41                 .info                   = MMP_PCM_INFO,
42                 .period_bytes_min       = 1024,
43                 .period_bytes_max       = 2048,
44                 .periods_min            = 2,
45                 .periods_max            = 32,
46                 .buffer_bytes_max       = 4096,
47                 .fifo_size              = 32,
48         },
49         {
50                 .info                   = MMP_PCM_INFO,
51                 .period_bytes_min       = 1024,
52                 .period_bytes_max       = 2048,
53                 .periods_min            = 2,
54                 .periods_max            = 32,
55                 .buffer_bytes_max       = 4096,
56                 .fifo_size              = 32,
57         },
58 };
59
60 static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
61                               struct snd_pcm_hw_params *params)
62 {
63         struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
64         struct dma_slave_config slave_config;
65         int ret;
66
67         ret =
68             snd_dmaengine_pcm_prepare_slave_config(substream, params,
69                                                    &slave_config);
70         if (ret)
71                 return ret;
72
73         ret = dmaengine_slave_config(chan, &slave_config);
74         if (ret)
75                 return ret;
76
77         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
78
79         return 0;
80 }
81
82 static bool filter(struct dma_chan *chan, void *param)
83 {
84         struct mmp_dma_data *dma_data = param;
85         bool found = false;
86         char *devname;
87
88         devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
89                 dma_data->ssp_id);
90         if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
91                 (chan->chan_id == dma_data->dma_res->start)) {
92                 found = true;
93         }
94
95         kfree(devname);
96         return found;
97 }
98
99 static int mmp_pcm_open(struct snd_pcm_substream *substream)
100 {
101         struct snd_soc_pcm_runtime *rtd = substream->private_data;
102         struct platform_device *pdev = to_platform_device(rtd->platform->dev);
103         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
104         struct mmp_dma_data dma_data;
105         struct resource *r;
106
107         r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
108         if (!r)
109                 return -EBUSY;
110
111         snd_soc_set_runtime_hwparams(substream,
112                                 &mmp_pcm_hardware[substream->stream]);
113
114         dma_data.dma_res = r;
115         dma_data.ssp_id = cpu_dai->id;
116
117         return snd_dmaengine_pcm_open_request_chan(substream, filter,
118                     &dma_data);
119 }
120
121 static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
122                          struct vm_area_struct *vma)
123 {
124         struct snd_pcm_runtime *runtime = substream->runtime;
125         unsigned long off = vma->vm_pgoff;
126
127         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
128         return remap_pfn_range(vma, vma->vm_start,
129                 __phys_to_pfn(runtime->dma_addr) + off,
130                 vma->vm_end - vma->vm_start, vma->vm_page_prot);
131 }
132
133 static struct snd_pcm_ops mmp_pcm_ops = {
134         .open           = mmp_pcm_open,
135         .close          = snd_dmaengine_pcm_close_release_chan,
136         .ioctl          = snd_pcm_lib_ioctl,
137         .hw_params      = mmp_pcm_hw_params,
138         .trigger        = snd_dmaengine_pcm_trigger,
139         .pointer        = snd_dmaengine_pcm_pointer,
140         .mmap           = mmp_pcm_mmap,
141 };
142
143 static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
144 {
145         struct snd_pcm_substream *substream;
146         struct snd_dma_buffer *buf;
147         int stream;
148         struct gen_pool *gpool;
149
150         gpool = sram_get_gpool("asram");
151         if (!gpool)
152                 return;
153
154         for (stream = 0; stream < 2; stream++) {
155                 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
156
157                 substream = pcm->streams[stream].substream;
158                 if (!substream)
159                         continue;
160
161                 buf = &substream->dma_buffer;
162                 if (!buf->area)
163                         continue;
164                 gen_pool_free(gpool, (unsigned long)buf->area, size);
165                 buf->area = NULL;
166         }
167
168         return;
169 }
170
171 static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
172                                                                 int stream)
173 {
174         struct snd_dma_buffer *buf = &substream->dma_buffer;
175         size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
176         struct gen_pool *gpool;
177
178         buf->dev.type = SNDRV_DMA_TYPE_DEV;
179         buf->dev.dev = substream->pcm->card->dev;
180         buf->private_data = NULL;
181
182         gpool = sram_get_gpool("asram");
183         if (!gpool)
184                 return -ENOMEM;
185
186         buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
187         if (!buf->area)
188                 return -ENOMEM;
189         buf->bytes = size;
190         return 0;
191 }
192
193 static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
194 {
195         struct snd_pcm_substream *substream;
196         struct snd_pcm *pcm = rtd->pcm;
197         int ret = 0, stream;
198
199         for (stream = 0; stream < 2; stream++) {
200                 substream = pcm->streams[stream].substream;
201
202                 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
203                 if (ret)
204                         goto err;
205         }
206
207         return 0;
208
209 err:
210         mmp_pcm_free_dma_buffers(pcm);
211         return ret;
212 }
213
214 static struct snd_soc_platform_driver mmp_soc_platform = {
215         .ops            = &mmp_pcm_ops,
216         .pcm_new        = mmp_pcm_new,
217         .pcm_free       = mmp_pcm_free_dma_buffers,
218 };
219
220 static int mmp_pcm_probe(struct platform_device *pdev)
221 {
222         struct mmp_audio_platdata *pdata = pdev->dev.platform_data;
223
224         if (pdata) {
225                 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max =
226                                                 pdata->buffer_max_playback;
227                 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max =
228                                                 pdata->period_max_playback;
229                 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max =
230                                                 pdata->buffer_max_capture;
231                 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max =
232                                                 pdata->period_max_capture;
233         }
234         return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform);
235 }
236
237 static int mmp_pcm_remove(struct platform_device *pdev)
238 {
239         snd_soc_unregister_platform(&pdev->dev);
240         return 0;
241 }
242
243 static struct platform_driver mmp_pcm_driver = {
244         .driver = {
245                 .name = "mmp-pcm-audio",
246                 .owner = THIS_MODULE,
247         },
248
249         .probe = mmp_pcm_probe,
250         .remove = mmp_pcm_remove,
251 };
252
253 module_platform_driver(mmp_pcm_driver);
254
255 MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
256 MODULE_DESCRIPTION("MMP Soc Audio DMA module");
257 MODULE_LICENSE("GPL");