Linux 3.13-rc1
[deliverable/linux.git] / sound / soc / pxa / mmp-pcm.c
CommitLineData
7a824e21
ZG
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>
ca057410 18#include <linux/platform_data/dma-mmp_tdma.h>
7a824e21 19#include <linux/platform_data/mmp_audio.h>
d65a1458 20
7a824e21
ZG
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>
7a824e21
ZG
26#include <sound/dmaengine_pcm.h>
27
28struct 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#define MMP_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
40 SNDRV_PCM_FMTBIT_S24_LE | \
41 SNDRV_PCM_FMTBIT_S32_LE)
42
43static struct snd_pcm_hardware mmp_pcm_hardware[] = {
44 {
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,
53 },
54 {
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,
63 },
64};
65
66static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
68{
69 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
70 struct snd_soc_pcm_runtime *rtd = substream->private_data;
d65a1458 71 struct snd_dmaengine_dai_dma_data *dma_params;
7a824e21
ZG
72 struct dma_slave_config slave_config;
73 int ret;
74
75 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
76 if (!dma_params)
77 return 0;
78
79 ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
80 if (ret)
81 return ret;
82
83 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
d65a1458 84 slave_config.dst_addr = dma_params->addr;
7a824e21
ZG
85 slave_config.dst_maxburst = 4;
86 } else {
d65a1458 87 slave_config.src_addr = dma_params->addr;
7a824e21
ZG
88 slave_config.src_maxburst = 4;
89 }
90
91 ret = dmaengine_slave_config(chan, &slave_config);
92 if (ret)
93 return ret;
94
95 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
96
97 return 0;
98}
99
100static bool filter(struct dma_chan *chan, void *param)
101{
102 struct mmp_dma_data *dma_data = param;
103 bool found = false;
104 char *devname;
105
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;
111 }
112
113 kfree(devname);
114 return found;
115}
116
117static int mmp_pcm_open(struct snd_pcm_substream *substream)
118{
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;
ac581e60 122 struct mmp_dma_data dma_data;
7a824e21 123 struct resource *r;
7a824e21
ZG
124
125 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
126 if (!r)
127 return -EBUSY;
128
129 snd_soc_set_runtime_hwparams(substream,
130 &mmp_pcm_hardware[substream->stream]);
7a824e21 131
ac581e60
LPC
132 dma_data.dma_res = r;
133 dma_data.ssp_id = cpu_dai->id;
7a824e21 134
7c1c1d4a
LPC
135 return snd_dmaengine_pcm_open_request_chan(substream, filter,
136 &dma_data);
7a824e21
ZG
137}
138
139static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
140 struct vm_area_struct *vma)
141{
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 unsigned long off = vma->vm_pgoff;
144
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);
149}
150
60e10d2f 151static struct snd_pcm_ops mmp_pcm_ops = {
7a824e21 152 .open = mmp_pcm_open,
7c1c1d4a 153 .close = snd_dmaengine_pcm_close_release_chan,
7a824e21
ZG
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,
159};
160
161static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
162{
163 struct snd_pcm_substream *substream;
164 struct snd_dma_buffer *buf;
165 int stream;
166 struct gen_pool *gpool;
167
168 gpool = sram_get_gpool("asram");
169 if (!gpool)
170 return;
171
172 for (stream = 0; stream < 2; stream++) {
173 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
174
175 substream = pcm->streams[stream].substream;
176 if (!substream)
177 continue;
178
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;
184 }
185
186 return;
187}
188
189static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
190 int stream)
191{
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;
195
196 buf->dev.type = SNDRV_DMA_TYPE_DEV;
197 buf->dev.dev = substream->pcm->card->dev;
198 buf->private_data = NULL;
199
200 gpool = sram_get_gpool("asram");
201 if (!gpool)
202 return -ENOMEM;
203
92a2e1cb 204 buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
7a824e21
ZG
205 if (!buf->area)
206 return -ENOMEM;
7a824e21
ZG
207 buf->bytes = size;
208 return 0;
209}
210
60e10d2f 211static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
7a824e21
ZG
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
227err:
228 mmp_pcm_free_dma_buffers(pcm);
229 return ret;
230}
231
60e10d2f 232static struct snd_soc_platform_driver mmp_soc_platform = {
7a824e21
ZG
233 .ops = &mmp_pcm_ops,
234 .pcm_new = mmp_pcm_new,
235 .pcm_free = mmp_pcm_free_dma_buffers,
236};
237
570f6fe1 238static int mmp_pcm_probe(struct platform_device *pdev)
7a824e21
ZG
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
570f6fe1 255static int mmp_pcm_remove(struct platform_device *pdev)
7a824e21
ZG
256{
257 snd_soc_unregister_platform(&pdev->dev);
258 return 0;
259}
260
261static struct platform_driver mmp_pcm_driver = {
262 .driver = {
263 .name = "mmp-pcm-audio",
264 .owner = THIS_MODULE,
265 },
266
267 .probe = mmp_pcm_probe,
570f6fe1 268 .remove = mmp_pcm_remove,
7a824e21
ZG
269};
270
271module_platform_driver(mmp_pcm_driver);
272
273MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
274MODULE_DESCRIPTION("MMP Soc Audio DMA module");
275MODULE_LICENSE("GPL");
This page took 0.084153 seconds and 5 git commands to generate.