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 ZG |
19 | #include <linux/platform_data/mmp_audio.h> |
20 | #include <sound/pxa2xx-lib.h> | |
21 | #include <sound/core.h> | |
22 | #include <sound/pcm.h> | |
23 | #include <sound/pcm_params.h> | |
24 | #include <sound/soc.h> | |
7a824e21 ZG |
25 | #include <sound/dmaengine_pcm.h> |
26 | ||
27 | struct mmp_dma_data { | |
28 | int ssp_id; | |
29 | struct resource *dma_res; | |
30 | }; | |
31 | ||
32 | #define MMP_PCM_INFO (SNDRV_PCM_INFO_MMAP | \ | |
33 | SNDRV_PCM_INFO_MMAP_VALID | \ | |
34 | SNDRV_PCM_INFO_INTERLEAVED | \ | |
35 | SNDRV_PCM_INFO_PAUSE | \ | |
36 | SNDRV_PCM_INFO_RESUME) | |
37 | ||
38 | #define MMP_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ | |
39 | SNDRV_PCM_FMTBIT_S24_LE | \ | |
40 | SNDRV_PCM_FMTBIT_S32_LE) | |
41 | ||
42 | static struct snd_pcm_hardware mmp_pcm_hardware[] = { | |
43 | { | |
44 | .info = MMP_PCM_INFO, | |
45 | .formats = MMP_PCM_FORMATS, | |
46 | .period_bytes_min = 1024, | |
47 | .period_bytes_max = 2048, | |
48 | .periods_min = 2, | |
49 | .periods_max = 32, | |
50 | .buffer_bytes_max = 4096, | |
51 | .fifo_size = 32, | |
52 | }, | |
53 | { | |
54 | .info = MMP_PCM_INFO, | |
55 | .formats = MMP_PCM_FORMATS, | |
56 | .period_bytes_min = 1024, | |
57 | .period_bytes_max = 2048, | |
58 | .periods_min = 2, | |
59 | .periods_max = 32, | |
60 | .buffer_bytes_max = 4096, | |
61 | .fifo_size = 32, | |
62 | }, | |
63 | }; | |
64 | ||
65 | static int mmp_pcm_hw_params(struct snd_pcm_substream *substream, | |
66 | struct snd_pcm_hw_params *params) | |
67 | { | |
68 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); | |
69 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
70 | struct pxa2xx_pcm_dma_params *dma_params; | |
71 | struct dma_slave_config slave_config; | |
72 | int ret; | |
73 | ||
74 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); | |
75 | if (!dma_params) | |
76 | return 0; | |
77 | ||
78 | ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config); | |
79 | if (ret) | |
80 | return ret; | |
81 | ||
82 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
83 | slave_config.dst_addr = dma_params->dev_addr; | |
84 | slave_config.dst_maxburst = 4; | |
85 | } else { | |
86 | slave_config.src_addr = dma_params->dev_addr; | |
87 | slave_config.src_maxburst = 4; | |
88 | } | |
89 | ||
90 | ret = dmaengine_slave_config(chan, &slave_config); | |
91 | if (ret) | |
92 | return ret; | |
93 | ||
94 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static bool filter(struct dma_chan *chan, void *param) | |
100 | { | |
101 | struct mmp_dma_data *dma_data = param; | |
102 | bool found = false; | |
103 | char *devname; | |
104 | ||
105 | devname = kasprintf(GFP_KERNEL, "%s.%d", dma_data->dma_res->name, | |
106 | dma_data->ssp_id); | |
107 | if ((strcmp(dev_name(chan->device->dev), devname) == 0) && | |
108 | (chan->chan_id == dma_data->dma_res->start)) { | |
109 | found = true; | |
110 | } | |
111 | ||
112 | kfree(devname); | |
113 | return found; | |
114 | } | |
115 | ||
116 | static int mmp_pcm_open(struct snd_pcm_substream *substream) | |
117 | { | |
118 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
119 | struct platform_device *pdev = to_platform_device(rtd->platform->dev); | |
120 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
ac581e60 | 121 | struct mmp_dma_data dma_data; |
7a824e21 | 122 | struct resource *r; |
7a824e21 ZG |
123 | |
124 | r = platform_get_resource(pdev, IORESOURCE_DMA, substream->stream); | |
125 | if (!r) | |
126 | return -EBUSY; | |
127 | ||
128 | snd_soc_set_runtime_hwparams(substream, | |
129 | &mmp_pcm_hardware[substream->stream]); | |
7a824e21 | 130 | |
ac581e60 LPC |
131 | dma_data.dma_res = r; |
132 | dma_data.ssp_id = cpu_dai->id; | |
7a824e21 | 133 | |
ac581e60 | 134 | return snd_dmaengine_pcm_open(substream, filter, &dma_data); |
7a824e21 ZG |
135 | } |
136 | ||
137 | static int mmp_pcm_mmap(struct snd_pcm_substream *substream, | |
138 | struct vm_area_struct *vma) | |
139 | { | |
140 | struct snd_pcm_runtime *runtime = substream->runtime; | |
141 | unsigned long off = vma->vm_pgoff; | |
142 | ||
143 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
144 | return remap_pfn_range(vma, vma->vm_start, | |
145 | __phys_to_pfn(runtime->dma_addr) + off, | |
146 | vma->vm_end - vma->vm_start, vma->vm_page_prot); | |
147 | } | |
148 | ||
149 | struct snd_pcm_ops mmp_pcm_ops = { | |
150 | .open = mmp_pcm_open, | |
ac581e60 | 151 | .close = snd_dmaengine_pcm_close, |
7a824e21 ZG |
152 | .ioctl = snd_pcm_lib_ioctl, |
153 | .hw_params = mmp_pcm_hw_params, | |
154 | .trigger = snd_dmaengine_pcm_trigger, | |
155 | .pointer = snd_dmaengine_pcm_pointer, | |
156 | .mmap = mmp_pcm_mmap, | |
157 | }; | |
158 | ||
159 | static void mmp_pcm_free_dma_buffers(struct snd_pcm *pcm) | |
160 | { | |
161 | struct snd_pcm_substream *substream; | |
162 | struct snd_dma_buffer *buf; | |
163 | int stream; | |
164 | struct gen_pool *gpool; | |
165 | ||
166 | gpool = sram_get_gpool("asram"); | |
167 | if (!gpool) | |
168 | return; | |
169 | ||
170 | for (stream = 0; stream < 2; stream++) { | |
171 | size_t size = mmp_pcm_hardware[stream].buffer_bytes_max; | |
172 | ||
173 | substream = pcm->streams[stream].substream; | |
174 | if (!substream) | |
175 | continue; | |
176 | ||
177 | buf = &substream->dma_buffer; | |
178 | if (!buf->area) | |
179 | continue; | |
180 | gen_pool_free(gpool, (unsigned long)buf->area, size); | |
181 | buf->area = NULL; | |
182 | } | |
183 | ||
184 | return; | |
185 | } | |
186 | ||
187 | static int mmp_pcm_preallocate_dma_buffer(struct snd_pcm_substream *substream, | |
188 | int stream) | |
189 | { | |
190 | struct snd_dma_buffer *buf = &substream->dma_buffer; | |
191 | size_t size = mmp_pcm_hardware[stream].buffer_bytes_max; | |
192 | struct gen_pool *gpool; | |
193 | ||
194 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
195 | buf->dev.dev = substream->pcm->card->dev; | |
196 | buf->private_data = NULL; | |
197 | ||
198 | gpool = sram_get_gpool("asram"); | |
199 | if (!gpool) | |
200 | return -ENOMEM; | |
201 | ||
202 | buf->area = (unsigned char *)gen_pool_alloc(gpool, size); | |
203 | if (!buf->area) | |
204 | return -ENOMEM; | |
205 | buf->addr = gen_pool_virt_to_phys(gpool, (unsigned long)buf->area); | |
206 | buf->bytes = size; | |
207 | return 0; | |
208 | } | |
209 | ||
210 | int mmp_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
211 | { | |
212 | struct snd_pcm_substream *substream; | |
213 | struct snd_pcm *pcm = rtd->pcm; | |
214 | int ret = 0, stream; | |
215 | ||
216 | for (stream = 0; stream < 2; stream++) { | |
217 | substream = pcm->streams[stream].substream; | |
218 | ||
219 | ret = mmp_pcm_preallocate_dma_buffer(substream, stream); | |
220 | if (ret) | |
221 | goto err; | |
222 | } | |
223 | ||
224 | return 0; | |
225 | ||
226 | err: | |
227 | mmp_pcm_free_dma_buffers(pcm); | |
228 | return ret; | |
229 | } | |
230 | ||
231 | struct snd_soc_platform_driver mmp_soc_platform = { | |
232 | .ops = &mmp_pcm_ops, | |
233 | .pcm_new = mmp_pcm_new, | |
234 | .pcm_free = mmp_pcm_free_dma_buffers, | |
235 | }; | |
236 | ||
570f6fe1 | 237 | static int mmp_pcm_probe(struct platform_device *pdev) |
7a824e21 ZG |
238 | { |
239 | struct mmp_audio_platdata *pdata = pdev->dev.platform_data; | |
240 | ||
241 | if (pdata) { | |
242 | mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].buffer_bytes_max = | |
243 | pdata->buffer_max_playback; | |
244 | mmp_pcm_hardware[SNDRV_PCM_STREAM_PLAYBACK].period_bytes_max = | |
245 | pdata->period_max_playback; | |
246 | mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].buffer_bytes_max = | |
247 | pdata->buffer_max_capture; | |
248 | mmp_pcm_hardware[SNDRV_PCM_STREAM_CAPTURE].period_bytes_max = | |
249 | pdata->period_max_capture; | |
250 | } | |
251 | return snd_soc_register_platform(&pdev->dev, &mmp_soc_platform); | |
252 | } | |
253 | ||
570f6fe1 | 254 | static int mmp_pcm_remove(struct platform_device *pdev) |
7a824e21 ZG |
255 | { |
256 | snd_soc_unregister_platform(&pdev->dev); | |
257 | return 0; | |
258 | } | |
259 | ||
260 | static struct platform_driver mmp_pcm_driver = { | |
261 | .driver = { | |
262 | .name = "mmp-pcm-audio", | |
263 | .owner = THIS_MODULE, | |
264 | }, | |
265 | ||
266 | .probe = mmp_pcm_probe, | |
570f6fe1 | 267 | .remove = mmp_pcm_remove, |
7a824e21 ZG |
268 | }; |
269 | ||
270 | module_platform_driver(mmp_pcm_driver); | |
271 | ||
272 | MODULE_AUTHOR("Leo Yan <leoy@marvell.com>"); | |
273 | MODULE_DESCRIPTION("MMP Soc Audio DMA module"); | |
274 | MODULE_LICENSE("GPL"); |