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