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>
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>
29 struct resource *dma_res;
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)
38 #define MMP_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
39 SNDRV_PCM_FMTBIT_S24_LE | \
40 SNDRV_PCM_FMTBIT_S32_LE)
42 static struct snd_pcm_hardware mmp_pcm_hardware[] = {
45 .formats = MMP_PCM_FORMATS,
46 .period_bytes_min = 1024,
47 .period_bytes_max = 2048,
50 .buffer_bytes_max = 4096,
55 .formats = MMP_PCM_FORMATS,
56 .period_bytes_min = 1024,
57 .period_bytes_max = 2048,
60 .buffer_bytes_max = 4096,
65 static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params *params)
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;
74 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
78 ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
82 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
83 slave_config.dst_addr = dma_params->dev_addr;
84 slave_config.dst_maxburst = 4;
86 slave_config.src_addr = dma_params->dev_addr;
87 slave_config.src_maxburst = 4;
90 ret = dmaengine_slave_config(chan, &slave_config);
94 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
99 static bool filter(struct dma_chan *chan, void *param)
101 struct mmp_dma_data *dma_data = param;
105 devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
107 if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
108 (chan->chan_id == dma_data->dma_res->start)) {
116 static int mmp_pcm_open(struct snd_pcm_substream *substream)
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;
124 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
128 snd_soc_set_runtime_hwparams(substream,
129 &mmp_pcm_hardware[substream->stream]);
131 dma_data.dma_res = r;
132 dma_data.ssp_id = cpu_dai->id;
134 return snd_dmaengine_pcm_open_request_chan(substream, filter,
138 static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
139 struct vm_area_struct *vma)
141 struct snd_pcm_runtime *runtime = substream->runtime;
142 unsigned long off = vma->vm_pgoff;
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);
150 static 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,
160 static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
162 struct snd_pcm_substream *substream;
163 struct snd_dma_buffer *buf;
165 struct gen_pool *gpool;
167 gpool = sram_get_gpool("asram");
171 for (stream = 0; stream < 2; stream++) {
172 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
174 substream = pcm->streams[stream].substream;
178 buf = &substream->dma_buffer;
181 gen_pool_free(gpool, (unsigned long)buf->area, size);
188 static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
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;
195 buf->dev.type = SNDRV_DMA_TYPE_DEV;
196 buf->dev.dev = substream->pcm->card->dev;
197 buf->private_data = NULL;
199 gpool = sram_get_gpool("asram");
203 buf->area = (unsigned char *)gen_pool_alloc(gpool, size);
206 buf->addr = gen_pool_virt_to_phys(gpool, (unsigned long)buf->area);
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;
217 for (stream = 0; stream < 2; stream++) {
218 substream = pcm->streams[stream].substream;
220 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
228 mmp_pcm_free_dma_buffers(pcm);
232 static struct snd_soc_platform_driver mmp_soc_platform = {
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;
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);
261 static struct platform_driver mmp_pcm_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");