blob: 682ee52942bfa6532775a5021e172403a929f4c5 [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);
Zhangfei Gao7a824e22012-06-11 18:04:38 +080070 struct dma_slave_config slave_config;
71 int ret;
72
Qiao Zhou48b752a2013-12-17 16:22:24 +080073 ret =
74 snd_dmaengine_pcm_prepare_slave_config(substream, params,
75 &slave_config);
Zhangfei Gao7a824e22012-06-11 18:04:38 +080076 if (ret)
77 return ret;
78
Zhangfei Gao7a824e22012-06-11 18:04:38 +080079 ret = dmaengine_slave_config(chan, &slave_config);
80 if (ret)
81 return ret;
82
83 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
84
85 return 0;
86}
87
88static bool filter(struct dma_chan *chan, void *param)
89{
90 struct mmp_dma_data *dma_data = param;
91 bool found = false;
92 char *devname;
93
94 devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name,
95 dma_data->ssp_id);
96 if ((strcmp(dev_name(chan->device->dev), devname) == 0) &&
97 (chan->chan_id == dma_data->dma_res->start)) {
98 found = true;
99 }
100
101 kfree(devname);
102 return found;
103}
104
105static int mmp_pcm_open(struct snd_pcm_substream *substream)
106{
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
108 struct platform_device *pdev = to_platform_device(rtd->platform->dev);
109 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100110 struct mmp_dma_data dma_data;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800111 struct resource *r;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800112
113 r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream);
114 if (!r)
115 return -EBUSY;
116
117 snd_soc_set_runtime_hwparams(substream,
118 &mmp_pcm_hardware[substream->stream]);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800119
Lars-Peter Clausenac581e62013-03-22 14:12:11 +0100120 dma_data.dma_res = r;
121 dma_data.ssp_id = cpu_dai->id;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800122
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200123 return snd_dmaengine_pcm_open_request_chan(substream, filter,
124 &dma_data);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800125}
126
127static int mmp_pcm_mmap(struct snd_pcm_substream *substream,
128 struct vm_area_struct *vma)
129{
130 struct snd_pcm_runtime *runtime = substream->runtime;
131 unsigned long off = vma->vm_pgoff;
132
133 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
134 return remap_pfn_range(vma, vma->vm_start,
135 __phys_to_pfn(runtime->dma_addr) + off,
136 vma->vm_end - vma->vm_start, vma->vm_page_prot);
137}
138
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200139static struct snd_pcm_ops mmp_pcm_ops = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800140 .open = mmp_pcm_open,
Lars-Peter Clausen7c1c1d42013-04-15 19:19:48 +0200141 .close = snd_dmaengine_pcm_close_release_chan,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800142 .ioctl = snd_pcm_lib_ioctl,
143 .hw_params = mmp_pcm_hw_params,
144 .trigger = snd_dmaengine_pcm_trigger,
145 .pointer = snd_dmaengine_pcm_pointer,
146 .mmap = mmp_pcm_mmap,
147};
148
149static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm)
150{
151 struct snd_pcm_substream *substream;
152 struct snd_dma_buffer *buf;
153 int stream;
154 struct gen_pool *gpool;
155
156 gpool = sram_get_gpool("asram");
157 if (!gpool)
158 return;
159
160 for (stream = 0; stream < 2; stream++) {
161 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
162
163 substream = pcm->streams[stream].substream;
164 if (!substream)
165 continue;
166
167 buf = &substream->dma_buffer;
168 if (!buf->area)
169 continue;
170 gen_pool_free(gpool, (unsigned long)buf->area, size);
171 buf->area = NULL;
172 }
173
174 return;
175}
176
177static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream,
178 int stream)
179{
180 struct snd_dma_buffer *buf = &substream->dma_buffer;
181 size_t size = mmp_pcm_hardware[stream].buffer_bytes_max;
182 struct gen_pool *gpool;
183
184 buf->dev.type = SNDRV_DMA_TYPE_DEV;
185 buf->dev.dev = substream->pcm->card->dev;
186 buf->private_data = NULL;
187
188 gpool = sram_get_gpool("asram");
189 if (!gpool)
190 return -ENOMEM;
191
Nicolin Chen92a2e1c2013-11-12 15:10:00 -0800192 buf->area = gen_pool_dma_alloc(gpool, size, &buf->addr);
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800193 if (!buf->area)
194 return -ENOMEM;
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800195 buf->bytes = size;
196 return 0;
197}
198
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200199static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800200{
201 struct snd_pcm_substream *substream;
202 struct snd_pcm *pcm = rtd->pcm;
203 int ret = 0, stream;
204
205 for (stream = 0; stream < 2; stream++) {
206 substream = pcm->streams[stream].substream;
207
208 ret = mmp_pcm_preallocate_dma_buffer(substream, stream);
209 if (ret)
210 goto err;
211 }
212
213 return 0;
214
215err:
216 mmp_pcm_free_dma_buffers(pcm);
217 return ret;
218}
219
Lars-Peter Clausen60e10d22013-05-14 22:19:51 +0200220static struct snd_soc_platform_driver mmp_soc_platform = {
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800221 .ops = &mmp_pcm_ops,
222 .pcm_new = mmp_pcm_new,
223 .pcm_free = mmp_pcm_free_dma_buffers,
224};
225
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500226static int mmp_pcm_probe(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800227{
228 struct mmp_audio_platdata *pdata = pdev->dev.platform_data;
229
230 if (pdata) {
231 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max =
232 pdata->buffer_max_playback;
233 mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max =
234 pdata->period_max_playback;
235 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max =
236 pdata->buffer_max_capture;
237 mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max =
238 pdata->period_max_capture;
239 }
240 return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform);
241}
242
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500243static int mmp_pcm_remove(struct platform_device *pdev)
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800244{
245 snd_soc_unregister_platform(&pdev->dev);
246 return 0;
247}
248
249static struct platform_driver mmp_pcm_driver = {
250 .driver = {
251 .name = "mmp-pcm-audio",
252 .owner = THIS_MODULE,
253 },
254
255 .probe = mmp_pcm_probe,
Bill Pemberton570f6fe2012-12-07 09:26:17 -0500256 .remove = mmp_pcm_remove,
Zhangfei Gao7a824e22012-06-11 18:04:38 +0800257};
258
259module_platform_driver(mmp_pcm_driver);
260
261MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
262MODULE_DESCRIPTION("MMP Soc Audio DMA module");
263MODULE_LICENSE("GPL");