Commit | Line | Data |
---|---|---|
7a824e21 ZG |
1 | /* |
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> | |
ca057410 | 18 | #include <linux/platform_data/dma-mmp_tdma.h> |
7a824e21 | 19 | #include <linux/platform_data/mmp_audio.h> |
d65a1458 | 20 | |
7a824e21 ZG |
21 | #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> | |
7a824e21 ZG |
26 | #include <sound/dmaengine_pcm.h> |
27 | ||
28 | struct 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 | ||
43 | static 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 | ||
66 | static 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; | |
d65a1458 | 71 | struct snd_dmaengine_dai_dma_data *dma_params; |
7a824e21 ZG |
72 | 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) { | |
d65a1458 | 84 | slave_config.dst_addr = dma_params->addr; |
7a824e21 ZG |
85 | slave_config.dst_maxburst = 4; |
86 | } else { | |
d65a1458 | 87 | slave_config.src_addr = dma_params->addr; |
7a824e21 ZG |
88 | 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 | ||
100 | static 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 | ||
117 | static 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; | |
ac581e60 | 122 | struct mmp_dma_data dma_data; |
7a824e21 | 123 | struct resource *r; |
7a824e21 ZG |
124 | |
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]); | |
7a824e21 | 131 | |
ac581e60 LPC |
132 | dma_data.dma_res = r; |
133 | dma_data.ssp_id = cpu_dai->id; | |
7a824e21 | 134 | |
7c1c1d4a LPC |
135 | return snd_dmaengine_pcm_open_request_chan(substream, filter, |
136 | &dma_data); | |
7a824e21 ZG |
137 | } |
138 | ||
139 | static 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 | ||
60e10d2f | 151 | static struct snd_pcm_ops mmp_pcm_ops = { |
7a824e21 | 152 | .open = mmp_pcm_open, |
7c1c1d4a | 153 | .close = snd_dmaengine_pcm_close_release_chan, |
7a824e21 ZG |
154 | .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 | ||
161 | static 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 | ||
189 | static 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 | ||
204 | buf->area = (unsigned char *)gen_pool_alloc(gpool, size); | |
205 | if (!buf->area) | |
206 | return -ENOMEM; | |
207 | buf->addr = gen_pool_virt_to_phys(gpool, (unsigned long)buf->area); | |
208 | buf->bytes = size; | |
209 | return 0; | |
210 | } | |
211 | ||
60e10d2f | 212 | static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd) |
7a824e21 ZG |
213 | { |
214 | struct snd_pcm_substream *substream; | |
215 | struct snd_pcm *pcm = rtd->pcm; | |
216 | int ret = 0, stream; | |
217 | ||
218 | for (stream = 0; stream < 2; stream++) { | |
219 | substream = pcm->streams[stream].substream; | |
220 | ||
221 | ret = mmp_pcm_preallocate_dma_buffer(substream, stream); | |
222 | if (ret) | |
223 | goto err; | |
224 | } | |
225 | ||
226 | return 0; | |
227 | ||
228 | err: | |
229 | mmp_pcm_free_dma_buffers(pcm); | |
230 | return ret; | |
231 | } | |
232 | ||
60e10d2f | 233 | static struct snd_soc_platform_driver mmp_soc_platform = { |
7a824e21 ZG |
234 | .ops = &mmp_pcm_ops, |
235 | .pcm_new = mmp_pcm_new, | |
236 | .pcm_free = mmp_pcm_free_dma_buffers, | |
237 | }; | |
238 | ||
570f6fe1 | 239 | static int mmp_pcm_probe(struct platform_device *pdev) |
7a824e21 ZG |
240 | { |
241 | struct mmp_audio_platdata *pdata = pdev->dev.platform_data; | |
242 | ||
243 | if (pdata) { | |
244 | mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max = | |
245 | pdata->buffer_max_playback; | |
246 | mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max = | |
247 | pdata->period_max_playback; | |
248 | mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max = | |
249 | pdata->buffer_max_capture; | |
250 | mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max = | |
251 | pdata->period_max_capture; | |
252 | } | |
253 | return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform); | |
254 | } | |
255 | ||
570f6fe1 | 256 | static int mmp_pcm_remove(struct platform_device *pdev) |
7a824e21 ZG |
257 | { |
258 | snd_soc_unregister_platform(&pdev->dev); | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static struct platform_driver mmp_pcm_driver = { | |
263 | .driver = { | |
264 | .name = "mmp-pcm-audio", | |
265 | .owner = THIS_MODULE, | |
266 | }, | |
267 | ||
268 | .probe = mmp_pcm_probe, | |
570f6fe1 | 269 | .remove = mmp_pcm_remove, |
7a824e21 ZG |
270 | }; |
271 | ||
272 | module_platform_driver(mmp_pcm_driver); | |
273 | ||
274 | MODULE_AUTHOR("Leo Yan <leoy@marvell.com>"); | |
275 | MODULE_DESCRIPTION("MMP Soc Audio DMA module"); | |
276 | MODULE_LICENSE("GPL"); |