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 DM |
20 | #include <linux/dmaengine.h> |
21 | ||
7a824e21 ZG |
22 | #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> | |
7a824e21 ZG |
27 | #include <sound/dmaengine_pcm.h> |
28 | ||
29 | struct 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 | ||
44 | static 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 | ||
67 | static 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; | |
d65a1458 | 72 | struct snd_dmaengine_dai_dma_data *dma_params; |
7a824e21 ZG |
73 | 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) { | |
d65a1458 | 85 | slave_config.dst_addr = dma_params->addr; |
7a824e21 ZG |
86 | slave_config.dst_maxburst = 4; |
87 | } else { | |
d65a1458 | 88 | slave_config.src_addr = dma_params->addr; |
7a824e21 ZG |
89 | 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 | ||
101 | static 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 | ||
118 | static 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; | |
ac581e60 | 123 | struct mmp_dma_data dma_data; |
7a824e21 | 124 | struct resource *r; |
7a824e21 ZG |
125 | |
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]); | |
7a824e21 | 132 | |
ac581e60 LPC |
133 | dma_data.dma_res = r; |
134 | dma_data.ssp_id = cpu_dai->id; | |
7a824e21 | 135 | |
7c1c1d4a LPC |
136 | return snd_dmaengine_pcm_open_request_chan(substream, filter, |
137 | &dma_data); | |
7a824e21 ZG |
138 | } |
139 | ||
140 | static 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 | ||
60e10d2f | 152 | static struct snd_pcm_ops mmp_pcm_ops = { |
7a824e21 | 153 | .open = mmp_pcm_open, |
7c1c1d4a | 154 | .close = snd_dmaengine_pcm_close_release_chan, |
7a824e21 ZG |
155 | .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 | ||
162 | static 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 | ||
190 | static 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 | ||
60e10d2f | 213 | static int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd) |
7a824e21 ZG |
214 | { |
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 | ||
229 | err: | |
230 | mmp_pcm_free_dma_buffers(pcm); | |
231 | return ret; | |
232 | } | |
233 | ||
60e10d2f | 234 | static struct snd_soc_platform_driver mmp_soc_platform = { |
7a824e21 ZG |
235 | .ops = &mmp_pcm_ops, |
236 | .pcm_new = mmp_pcm_new, | |
237 | .pcm_free = mmp_pcm_free_dma_buffers, | |
238 | }; | |
239 | ||
570f6fe1 | 240 | static int mmp_pcm_probe(struct platform_device *pdev) |
7a824e21 ZG |
241 | { |
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 | ||
570f6fe1 | 257 | static int mmp_pcm_remove(struct platform_device *pdev) |
7a824e21 ZG |
258 | { |
259 | snd_soc_unregister_platform(&pdev->dev); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static struct platform_driver mmp_pcm_driver = { | |
264 | .driver = { | |
265 | .name = "mmp-pcm-audio", | |
266 | .owner = THIS_MODULE, | |
267 | }, | |
268 | ||
269 | .probe = mmp_pcm_probe, | |
570f6fe1 | 270 | .remove = mmp_pcm_remove, |
7a824e21 ZG |
271 | }; |
272 | ||
273 | module_platform_driver(mmp_pcm_driver); | |
274 | ||
275 | MODULE_AUTHOR("Leo Yan <leoy@marvell.com>"); | |
276 | MODULE_DESCRIPTION("MMP Soc Audio DMA module"); | |
277 | MODULE_LICENSE("GPL"); |