sound/soc/pxa/mmp-pcm.c: use gen_pool_dma_alloc() to allocate dma buffer
[linux-2.6.git] / sound / soc / pxa / mmp-pcm.c
blob7929e19b0ef5970b1942da441770df909f29629e
1 /*
2 * linux/sound/soc/pxa/mmp-pcm.c
4 * Copyright (C) 2011 Marvell International Ltd.
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.
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>
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>
28 struct mmp_dma_data {
29 int ssp_id;
30 struct resource *dma_res;
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)
39 #define MMP_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
40 SNDRV_PCM_FMTBIT_S24_LE | \
41 SNDRV_PCM_FMTBIT_S32_LE)
43 static struct snd_pcm_hardware mmp_pcm_hardware[] = {
45 .info = MMP_PCM_INFO,
46 .formats = MMP_PCM_FORMATS,
47 .period_bytes_min = 1024,
48 .period_bytes_max = 2048,
49 .periods_min = 2,
50 .periods_max = 32,
51 .buffer_bytes_max = 4096,
52 .fifo_size = 32,
55 .info = MMP_PCM_INFO,
56 .formats = MMP_PCM_FORMATS,
57 .period_bytes_min = 1024,
58 .period_bytes_max = 2048,
59 .periods_min = 2,
60 .periods_max = 32,
61 .buffer_bytes_max = 4096,
62 .fifo_size = 32,
66 static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
69 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
70 struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 struct snd_dmaengine_dai_dma_data *dma_params;
72 struct dma_slave_config slave_config;
73 int ret;
75 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
76 if (!dma_params)
77 return 0;
79 ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
80 if (ret)
81 return ret;
83 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
84 slave_config.dst_addr = dma_params->addr;
85 slave_config.dst_maxburst = 4;
86 } else {
87 slave_config.src_addr = dma_params->addr;
88 slave_config.src_maxburst = 4;
91 ret = dmaengine_slave_config(chan, &slave_config);
92 if (ret)
93 return ret;
95 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
97 return 0;
100 static bool filter(struct dma_chan *chan, void *param)
102 struct mmp_dma_data *dma_data = param;
103 bool found = false;
104 char *devname;
106 devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
107 dma_data->ssp_id);
108 if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
109 (chan->chan_id == dma_data->dma_res->start)) {
110 found = true;
113 kfree(devname);
114 return found;
117 static int mmp_pcm_open(struct snd_pcm_substream *substream)
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120 struct platform_device *pdev = to_platform_device(rtd->platform->dev);
121 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
122 struct mmp_dma_data dma_data;
123 struct resource *r;
125 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
126 if (!r)
127 return -EBUSY;
129 snd_soc_set_runtime_hwparams(substream,
130 &mmp_pcm_hardware[substream->stream]);
132 dma_data.dma_res = r;
133 dma_data.ssp_id = cpu_dai->id;
135 return snd_dmaengine_pcm_open_request_chan(substream, filter,
136 &dma_data);
139 static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
140 struct vm_area_struct *vma)
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 unsigned long off = vma->vm_pgoff;
145 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
146 return remap_pfn_range(vma, vma->vm_start,
147 __phys_to_pfn(runtime->dma_addr) + off,
148 vma->vm_end - vma->vm_start, vma->vm_page_prot);
151 static struct snd_pcm_ops mmp_pcm_ops = {
152 .open = mmp_pcm_open,
153 .close = snd_dmaengine_pcm_close_release_chan,
154 .ioctl = snd_pcm_lib_ioctl,
155 .hw_params = mmp_pcm_hw_params,
156 .trigger = snd_dmaengine_pcm_trigger,
157 .pointer = snd_dmaengine_pcm_pointer,
158 .mmap = mmp_pcm_mmap,
161 static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
163 struct snd_pcm_substream *substream;
164 struct snd_dma_buffer *buf;
165 int stream;
166 struct gen_pool *gpool;
168 gpool = sram_get_gpool("asram");
169 if (!gpool)
170 return;
172 for (stream = 0; stream < 2; stream++) {
173 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
175 substream = pcm->streams[stream].substream;
176 if (!substream)
177 continue;
179 buf = &substream->dma_buffer;
180 if (!buf->area)
181 continue;
182 gen_pool_free(gpool, (unsigned long)buf->area, size);
183 buf->area = NULL;
186 return;
189 static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
190 int stream)
192 struct snd_dma_buffer *buf = &substream->dma_buffer;
193 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
194 struct gen_pool *gpool;
196 buf->dev.type = SNDRV_DMA_TYPE_DEV;
197 buf->dev.dev = substream->pcm->card->dev;
198 buf->private_data = NULL;
200 gpool = sram_get_gpool("asram");
201 if (!gpool)
202 return -ENOMEM;
204 buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
205 if (!buf->area)
206 return -ENOMEM;
207 buf->bytes = size;
208 return 0;
211 static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
213 struct snd_pcm_substream *substream;
214 struct snd_pcm *pcm = rtd->pcm;
215 int ret = 0, stream;
217 for (stream = 0; stream < 2; stream++) {
218 substream = pcm->streams[stream].substream;
220 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
221 if (ret)
222 goto err;
225 return 0;
227 err:
228 mmp_pcm_free_dma_buffers(pcm);
229 return ret;
232 static 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,
238 static int mmp_pcm_probe(struct platform_device *pdev)
240 struct mmp_audio_platdata *pdata = pdev->dev.platform_data;
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;
252 return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform);
255 static int mmp_pcm_remove(struct platform_device *pdev)
257 snd_soc_unregister_platform(&pdev->dev);
258 return 0;
261 static struct platform_driver mmp_pcm_driver = {
262 .driver = {
263 .name = "mmp-pcm-audio",
264 .owner = THIS_MODULE,
267 .probe = mmp_pcm_probe,
268 .remove = mmp_pcm_remove,
271 module_platform_driver(mmp_pcm_driver);
273 MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
274 MODULE_DESCRIPTION("MMP Soc Audio DMA module");
275 MODULE_LICENSE("GPL");