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/mmp_audio.h>
19 #include <sound/pxa2xx-lib.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <mach/sram.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
;
125 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, substream
->stream
);
129 snd_soc_set_runtime_hwparams(substream
,
130 &mmp_pcm_hardware
[substream
->stream
]);
131 dma_data
= devm_kzalloc(&pdev
->dev
,
132 sizeof(struct mmp_dma_data
), GFP_KERNEL
);
133 if (dma_data
== NULL
)
136 dma_data
->dma_res
= r
;
137 dma_data
->ssp_id
= cpu_dai
->id
;
139 ret
= snd_dmaengine_pcm_open(substream
, filter
, dma_data
);
141 devm_kfree(&pdev
->dev
, dma_data
);
145 snd_dmaengine_pcm_set_data(substream
, dma_data
);
149 static int mmp_pcm_close(struct snd_pcm_substream
*substream
)
151 struct mmp_dma_data
*dma_data
= snd_dmaengine_pcm_get_data(substream
);
152 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
153 struct platform_device
*pdev
= to_platform_device(rtd
->platform
->dev
);
155 snd_dmaengine_pcm_close(substream
);
156 devm_kfree(&pdev
->dev
, dma_data
);
160 static int mmp_pcm_mmap(struct snd_pcm_substream
*substream
,
161 struct vm_area_struct
*vma
)
163 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
164 unsigned long off
= vma
->vm_pgoff
;
166 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
167 return remap_pfn_range(vma
, vma
->vm_start
,
168 __phys_to_pfn(runtime
->dma_addr
) + off
,
169 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
172 struct snd_pcm_ops mmp_pcm_ops
= {
173 .open
= mmp_pcm_open
,
174 .close
= mmp_pcm_close
,
175 .ioctl
= snd_pcm_lib_ioctl
,
176 .hw_params
= mmp_pcm_hw_params
,
177 .trigger
= snd_dmaengine_pcm_trigger
,
178 .pointer
= snd_dmaengine_pcm_pointer
,
179 .mmap
= mmp_pcm_mmap
,
182 static void mmp_pcm_free_dma_buffers(struct snd_pcm
*pcm
)
184 struct snd_pcm_substream
*substream
;
185 struct snd_dma_buffer
*buf
;
187 struct gen_pool
*gpool
;
189 gpool
= sram_get_gpool("asram");
193 for (stream
= 0; stream
< 2; stream
++) {
194 size_t size
= mmp_pcm_hardware
[stream
].buffer_bytes_max
;
196 substream
= pcm
->streams
[stream
].substream
;
200 buf
= &substream
->dma_buffer
;
203 gen_pool_free(gpool
, (unsigned long)buf
->area
, size
);
210 static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream
*substream
,
213 struct snd_dma_buffer
*buf
= &substream
->dma_buffer
;
214 size_t size
= mmp_pcm_hardware
[stream
].buffer_bytes_max
;
215 struct gen_pool
*gpool
;
217 buf
->dev
.type
= SNDRV_DMA_TYPE_DEV
;
218 buf
->dev
.dev
= substream
->pcm
->card
->dev
;
219 buf
->private_data
= NULL
;
221 gpool
= sram_get_gpool("asram");
225 buf
->area
= (unsigned char *)gen_pool_alloc(gpool
, size
);
228 buf
->addr
= gen_pool_virt_to_phys(gpool
, (unsigned long)buf
->area
);
233 int mmp_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
235 struct snd_pcm_substream
*substream
;
236 struct snd_pcm
*pcm
= rtd
->pcm
;
239 for (stream
= 0; stream
< 2; stream
++) {
240 substream
= pcm
->streams
[stream
].substream
;
242 ret
= mmp_pcm_preallocate_dma_buffer(substream
, stream
);
250 mmp_pcm_free_dma_buffers(pcm
);
254 struct snd_soc_platform_driver mmp_soc_platform
= {
256 .pcm_new
= mmp_pcm_new
,
257 .pcm_free
= mmp_pcm_free_dma_buffers
,
260 static __devinit
int mmp_pcm_probe(struct platform_device
*pdev
)
262 struct mmp_audio_platdata
*pdata
= pdev
->dev
.platform_data
;
265 mmp_pcm_hardware
[SNDRV_PCM_STREAM_PLAYBACK
].buffer_bytes_max
=
266 pdata
->buffer_max_playback
;
267 mmp_pcm_hardware
[SNDRV_PCM_STREAM_PLAYBACK
].period_bytes_max
=
268 pdata
->period_max_playback
;
269 mmp_pcm_hardware
[SNDRV_PCM_STREAM_CAPTURE
].buffer_bytes_max
=
270 pdata
->buffer_max_capture
;
271 mmp_pcm_hardware
[SNDRV_PCM_STREAM_CAPTURE
].period_bytes_max
=
272 pdata
->period_max_capture
;
274 return snd_soc_register_platform(&pdev
->dev
, &mmp_soc_platform
);
277 static int __devexit
mmp_pcm_remove(struct platform_device
*pdev
)
279 snd_soc_unregister_platform(&pdev
->dev
);
283 static struct platform_driver mmp_pcm_driver
= {
285 .name
= "mmp-pcm-audio",
286 .owner
= THIS_MODULE
,
289 .probe
= mmp_pcm_probe
,
290 .remove
= __devexit_p(mmp_pcm_remove
),
293 module_platform_driver(mmp_pcm_driver
);
295 MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
296 MODULE_DESCRIPTION("MMP Soc Audio DMA module");
297 MODULE_LICENSE("GPL");