blob: 7929e19b0ef5970b1942da441770df909f29629e [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
Zhangfei Gao7a824e22012-06-11 18:04:38 +080021#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>
Zhangfei Gao7a824e22012-06-11 18:04:38 +080026#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;
Daniel Mackd65a1452013-08-12 10:42:39 +020071 struct snd_dmaengine_dai_dma_data *dma_params;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080072 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) {
Daniel Mackd65a1452013-08-12 10:42:39 +020084 slave_config.dst_addr = dma_params->addr;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080085 slave_config.dst_maxburst = 4;
86 } else {
Daniel Mackd65a1452013-08-12 10:42:39 +020087 slave_config.src_addr = dma_params->addr;
Zhangfei Gao7a824e22012-06-11 18:04:38 +080088 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;
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100122 struct mmp_dma_data dma_data;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800123 struct resource *r;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800124
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]);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800131
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100132 dma_data.dma_res = r;
133 dma_data.ssp_id = cpu_dai->id;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800134
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200135 return snd_dmaengine_pcm_open_request_chan(substream, filter,
136 &dma_data);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800137}
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
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200151static struct snd_pcm_ops mmp_pcm_ops = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800152 .open = mmp_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200153 .close = snd_dmaengine_pcm_close_release_chan,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800154 .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
Nicolin Chen92a2e1c2013-11-12 15:10:00 -0800204 buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800205 if (!buf->area)
206 return -ENOMEM;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800207 buf->bytes = size;
208 return 0;
209}
210
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200211static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800212{
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
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200232static struct snd_soc_platform_driver mmp_soc_platform = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800233 .ops = &mmp_pcm_ops,
234 .pcm_new = mmp_pcm_new,
235 .pcm_free = mmp_pcm_free_dma_buffers,
236};
237
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500238static int mmp_pcm_probe(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800239{
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
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500255static int mmp_pcm_remove(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800256{
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,
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500268 .remove = mmp_pcm_remove,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800269};
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");