blob: 9a97843ab09f1270e97e7751a2ce86b637d85757 [file] [log] [blame]
Zhangfei Gao7a824e22012-06-11 18:04:38 +08001/*
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>
Chris Ballca057412012-10-06 07:38:16 -040018#include <linux/platform_data/dma-mmp_tdma.h>
Zhangfei Gao7a824e22012-06-11 18:04:38 +080019#include <linux/platform_data/mmp_audio.h>
Daniel Mackd65a1452013-08-12 10:42:39 +020020#include <linux/dmaengine.h>
21
Zhangfei Gao7a824e22012-06-11 18:04:38 +080022#include <sound/pxa2xx-lib.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
Zhangfei Gao7a824e22012-06-11 18:04:38 +080027#include <sound/dmaengine_pcm.h>
28
29struct mmp_dma_data {
30 int ssp_id;
31 struct resource *dma_res;
32};
33
34#define MMP_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
35 SNDRV_PCM_INFO_MMAP_VALID | \
36 SNDRV_PCM_INFO_INTERLEAVED | \
37 SNDRV_PCM_INFO_PAUSE | \
38 SNDRV_PCM_INFO_RESUME)
39
40#define MMP_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
41 SNDRV_PCM_FMTBIT_S24_LE | \
42 SNDRV_PCM_FMTBIT_S32_LE)
43
44static struct snd_pcm_hardware mmp_pcm_hardware[] = {
45 {
46 .info = MMP_PCM_INFO,
47 .formats = MMP_PCM_FORMATS,
48 .period_bytes_min = 1024,
49 .period_bytes_max = 2048,
50 .periods_min = 2,
51 .periods_max = 32,
52 .buffer_bytes_max = 4096,
53 .fifo_size = 32,
54 },
55 {
56 .info = MMP_PCM_INFO,
57 .formats = MMP_PCM_FORMATS,
58 .period_bytes_min = 1024,
59 .period_bytes_max = 2048,
60 .periods_min = 2,
61 .periods_max = 32,
62 .buffer_bytes_max = 4096,
63 .fifo_size = 32,
64 },
65};
66
67static int mmp_pcm_hw_params(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params *params)
69{
70 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
71 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Daniel Mackd65a1452013-08-12 10:42:39 +020072 struct snd_dmaengine_dai_dma_data *dma_params;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080073 struct dma_slave_config slave_config;
74 int ret;
75
76 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
77 if (!dma_params)
78 return 0;
79
80 ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
81 if (ret)
82 return ret;
83
84 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Daniel Mackd65a1452013-08-12 10:42:39 +020085 slave_config.dst_addr = dma_params->addr;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080086 slave_config.dst_maxburst = 4;
87 } else {
Daniel Mackd65a1452013-08-12 10:42:39 +020088 slave_config.src_addr = dma_params->addr;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080089 slave_config.src_maxburst = 4;
90 }
91
92 ret = dmaengine_slave_config(chan, &slave_config);
93 if (ret)
94 return ret;
95
96 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
97
98 return 0;
99}
100
101static bool filter(struct dma_chan *chan, void *param)
102{
103 struct mmp_dma_data *dma_data = param;
104 bool found = false;
105 char *devname;
106
107 devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
108 dma_data->ssp_id);
109 if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
110 (chan->chan_id == dma_data->dma_res->start)) {
111 found = true;
112 }
113
114 kfree(devname);
115 return found;
116}
117
118static int mmp_pcm_open(struct snd_pcm_substream *substream)
119{
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
121 struct platform_device *pdev = to_platform_device(rtd->platform->dev);
122 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100123 struct mmp_dma_data dma_data;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800124 struct resource *r;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800125
126 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
127 if (!r)
128 return -EBUSY;
129
130 snd_soc_set_runtime_hwparams(substream,
131 &mmp_pcm_hardware[substream->stream]);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800132
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100133 dma_data.dma_res = r;
134 dma_data.ssp_id = cpu_dai->id;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800135
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200136 return snd_dmaengine_pcm_open_request_chan(substream, filter,
137 &dma_data);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800138}
139
140static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
141 struct vm_area_struct *vma)
142{
143 struct snd_pcm_runtime *runtime = substream->runtime;
144 unsigned long off = vma->vm_pgoff;
145
146 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
147 return remap_pfn_range(vma, vma->vm_start,
148 __phys_to_pfn(runtime->dma_addr) + off,
149 vma->vm_end - vma->vm_start, vma->vm_page_prot);
150}
151
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200152static struct snd_pcm_ops mmp_pcm_ops = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800153 .open = mmp_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200154 .close = snd_dmaengine_pcm_close_release_chan,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800155 .ioctl = snd_pcm_lib_ioctl,
156 .hw_params = mmp_pcm_hw_params,
157 .trigger = snd_dmaengine_pcm_trigger,
158 .pointer = snd_dmaengine_pcm_pointer,
159 .mmap = mmp_pcm_mmap,
160};
161
162static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
163{
164 struct snd_pcm_substream *substream;
165 struct snd_dma_buffer *buf;
166 int stream;
167 struct gen_pool *gpool;
168
169 gpool = sram_get_gpool("asram");
170 if (!gpool)
171 return;
172
173 for (stream = 0; stream < 2; stream++) {
174 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
175
176 substream = pcm->streams[stream].substream;
177 if (!substream)
178 continue;
179
180 buf = &substream->dma_buffer;
181 if (!buf->area)
182 continue;
183 gen_pool_free(gpool, (unsigned long)buf->area, size);
184 buf->area = NULL;
185 }
186
187 return;
188}
189
190static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
191 int stream)
192{
193 struct snd_dma_buffer *buf = &substream->dma_buffer;
194 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
195 struct gen_pool *gpool;
196
197 buf->dev.type = SNDRV_DMA_TYPE_DEV;
198 buf->dev.dev = substream->pcm->card->dev;
199 buf->private_data = NULL;
200
201 gpool = sram_get_gpool("asram");
202 if (!gpool)
203 return -ENOMEM;
204
205 buf->area = (unsigned char *)gen_pool_alloc(gpool, size);
206 if (!buf->area)
207 return -ENOMEM;
208 buf->addr = gen_pool_virt_to_phys(gpool, (unsigned long)buf->area);
209 buf->bytes = size;
210 return 0;
211}
212
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200213static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800214{
215 struct snd_pcm_substream *substream;
216 struct snd_pcm *pcm = rtd->pcm;
217 int ret = 0, stream;
218
219 for (stream = 0; stream < 2; stream++) {
220 substream = pcm->streams[stream].substream;
221
222 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
223 if (ret)
224 goto err;
225 }
226
227 return 0;
228
229err:
230 mmp_pcm_free_dma_buffers(pcm);
231 return ret;
232}
233
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200234static struct snd_soc_platform_driver mmp_soc_platform = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800235 .ops = &mmp_pcm_ops,
236 .pcm_new = mmp_pcm_new,
237 .pcm_free = mmp_pcm_free_dma_buffers,
238};
239
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500240static int mmp_pcm_probe(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800241{
242 struct mmp_audio_platdata *pdata = pdev->dev.platform_data;
243
244 if (pdata) {
245 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max =
246 pdata->buffer_max_playback;
247 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max =
248 pdata->period_max_playback;
249 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max =
250 pdata->buffer_max_capture;
251 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max =
252 pdata->period_max_capture;
253 }
254 return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform);
255}
256
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500257static int mmp_pcm_remove(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800258{
259 snd_soc_unregister_platform(&pdev->dev);
260 return 0;
261}
262
263static struct platform_driver mmp_pcm_driver = {
264 .driver = {
265 .name = "mmp-pcm-audio",
266 .owner = THIS_MODULE,
267 },
268
269 .probe = mmp_pcm_probe,
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500270 .remove = mmp_pcm_remove,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800271};
272
273module_platform_driver(mmp_pcm_driver);
274
275MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
276MODULE_DESCRIPTION("MMP Soc Audio DMA module");
277MODULE_LICENSE("GPL");