Commit | Line | Data |
---|---|---|
2e74796a JN |
1 | /* |
2 | * omap-pcm.c -- ALSA PCM interface for the OMAP SoC | |
3 | * | |
4 | * Copyright (C) 2008 Nokia Corporation | |
5 | * | |
7ec41ee5 | 6 | * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com> |
1c7687b9 | 7 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
2e74796a JN |
8 | * |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * version 2 as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
21 | * 02110-1301 USA | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <linux/dma-mapping.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
da155d5b | 27 | #include <linux/module.h> |
946cc36a | 28 | #include <linux/omap-dma.h> |
2e74796a JN |
29 | #include <sound/core.h> |
30 | #include <sound/pcm.h> | |
31 | #include <sound/pcm_params.h> | |
946cc36a | 32 | #include <sound/dmaengine_pcm.h> |
2e74796a JN |
33 | #include <sound/soc.h> |
34 | ||
27615a97 TL |
35 | #ifdef CONFIG_ARCH_OMAP1 |
36 | #define pcm_omap1510() cpu_is_omap1510() | |
37 | #else | |
38 | #define pcm_omap1510() 0 | |
39 | #endif | |
40 | ||
2e74796a JN |
41 | static const struct snd_pcm_hardware omap_pcm_hardware = { |
42 | .info = SNDRV_PCM_INFO_MMAP | | |
43 | SNDRV_PCM_INFO_MMAP_VALID | | |
44 | SNDRV_PCM_INFO_INTERLEAVED | | |
45 | SNDRV_PCM_INFO_PAUSE | | |
b4173824 PU |
46 | SNDRV_PCM_INFO_RESUME | |
47 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, | |
2e74796a JN |
48 | .period_bytes_min = 32, |
49 | .period_bytes_max = 64 * 1024, | |
50 | .periods_min = 2, | |
51 | .periods_max = 255, | |
52 | .buffer_bytes_max = 128 * 1024, | |
53 | }; | |
54 | ||
2e74796a JN |
55 | /* this may get called several times by oss emulation */ |
56 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, | |
57 | struct snd_pcm_hw_params *params) | |
58 | { | |
59 | struct snd_pcm_runtime *runtime = substream->runtime; | |
60 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
5f712b2b | 61 | struct omap_pcm_dma_data *dma_data; |
946cc36a PU |
62 | struct dma_slave_config config; |
63 | struct dma_chan *chan; | |
2e74796a JN |
64 | int err = 0; |
65 | ||
f0fba2ad | 66 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
5f712b2b | 67 | |
1b4246a1 JS |
68 | /* return if this is a bufferless transfer e.g. |
69 | * codec <--> BT codec or GSM modem -- lg FIXME */ | |
2e74796a | 70 | if (!dma_data) |
1b4246a1 | 71 | return 0; |
2e74796a JN |
72 | |
73 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); | |
74 | runtime->dma_bytes = params_buffer_bytes(params); | |
75 | ||
946cc36a PU |
76 | chan = snd_dmaengine_pcm_get_chan(substream); |
77 | if (!chan) | |
78 | return -EINVAL; | |
2e74796a | 79 | |
946cc36a PU |
80 | /* fills in addr_width and direction */ |
81 | err = snd_hwparams_to_dma_slave_config(substream, params, &config); | |
82 | if (err) | |
83 | return err; | |
2e74796a | 84 | |
09ae3aaf LPC |
85 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
86 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream), | |
87 | &config); | |
2e74796a | 88 | |
946cc36a | 89 | return dmaengine_slave_config(chan, &config); |
2e74796a JN |
90 | } |
91 | ||
946cc36a | 92 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
2e74796a | 93 | { |
946cc36a | 94 | snd_pcm_set_runtime_buffer(substream, NULL); |
2e74796a JN |
95 | return 0; |
96 | } | |
97 | ||
2e74796a JN |
98 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
99 | { | |
2e74796a JN |
100 | snd_pcm_uframes_t offset; |
101 | ||
27615a97 | 102 | if (pcm_omap1510()) |
946cc36a PU |
103 | offset = snd_dmaengine_pcm_pointer_no_residue(substream); |
104 | else | |
105 | offset = snd_dmaengine_pcm_pointer(substream); | |
2e74796a JN |
106 | |
107 | return offset; | |
108 | } | |
109 | ||
110 | static int omap_pcm_open(struct snd_pcm_substream *substream) | |
111 | { | |
946cc36a | 112 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
09ae3aaf | 113 | struct snd_dmaengine_dai_dma_data *dma_data; |
f6becf0b | 114 | int ret; |
2e74796a JN |
115 | |
116 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); | |
117 | ||
946cc36a | 118 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
a92b5317 | 119 | |
f6becf0b PU |
120 | /* DT boot: filter_data is the DMA name */ |
121 | if (rtd->cpu_dai->dev->of_node) { | |
122 | struct dma_chan *chan; | |
123 | ||
124 | chan = dma_request_slave_channel(rtd->cpu_dai->dev, | |
125 | dma_data->filter_data); | |
126 | ret = snd_dmaengine_pcm_open(substream, chan); | |
127 | } else { | |
128 | ret = snd_dmaengine_pcm_open_request_chan(substream, | |
129 | omap_dma_filter_fn, | |
130 | dma_data->filter_data); | |
131 | } | |
132 | return ret; | |
2e74796a JN |
133 | } |
134 | ||
2e74796a JN |
135 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
136 | struct vm_area_struct *vma) | |
137 | { | |
138 | struct snd_pcm_runtime *runtime = substream->runtime; | |
139 | ||
140 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, | |
141 | runtime->dma_area, | |
142 | runtime->dma_addr, | |
143 | runtime->dma_bytes); | |
144 | } | |
145 | ||
b2a19d02 | 146 | static struct snd_pcm_ops omap_pcm_ops = { |
2e74796a | 147 | .open = omap_pcm_open, |
7c1c1d4a | 148 | .close = snd_dmaengine_pcm_close_release_chan, |
2e74796a JN |
149 | .ioctl = snd_pcm_lib_ioctl, |
150 | .hw_params = omap_pcm_hw_params, | |
151 | .hw_free = omap_pcm_hw_free, | |
abe99370 | 152 | .trigger = snd_dmaengine_pcm_trigger, |
2e74796a JN |
153 | .pointer = omap_pcm_pointer, |
154 | .mmap = omap_pcm_mmap, | |
155 | }; | |
156 | ||
2e74796a JN |
157 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
158 | int stream) | |
159 | { | |
160 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; | |
161 | struct snd_dma_buffer *buf = &substream->dma_buffer; | |
162 | size_t size = omap_pcm_hardware.buffer_bytes_max; | |
163 | ||
164 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
165 | buf->dev.dev = pcm->card->dev; | |
166 | buf->private_data = NULL; | |
167 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, | |
168 | &buf->addr, GFP_KERNEL); | |
169 | if (!buf->area) | |
170 | return -ENOMEM; | |
171 | ||
172 | buf->bytes = size; | |
173 | return 0; | |
174 | } | |
175 | ||
176 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) | |
177 | { | |
178 | struct snd_pcm_substream *substream; | |
179 | struct snd_dma_buffer *buf; | |
180 | int stream; | |
181 | ||
182 | for (stream = 0; stream < 2; stream++) { | |
183 | substream = pcm->streams[stream].substream; | |
184 | if (!substream) | |
185 | continue; | |
186 | ||
187 | buf = &substream->dma_buffer; | |
188 | if (!buf->area) | |
189 | continue; | |
190 | ||
191 | dma_free_writecombine(pcm->card->dev, buf->bytes, | |
192 | buf->area, buf->addr); | |
193 | buf->area = NULL; | |
194 | } | |
195 | } | |
196 | ||
552d1ef6 | 197 | static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd) |
2e74796a | 198 | { |
552d1ef6 | 199 | struct snd_card *card = rtd->card->snd_card; |
552d1ef6 | 200 | struct snd_pcm *pcm = rtd->pcm; |
c9bd5e69 | 201 | int ret; |
2e74796a | 202 | |
c9bd5e69 RK |
203 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64)); |
204 | if (ret) | |
205 | return ret; | |
2e74796a | 206 | |
25e9e756 | 207 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
2e74796a JN |
208 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
209 | SNDRV_PCM_STREAM_PLAYBACK); | |
210 | if (ret) | |
211 | goto out; | |
212 | } | |
213 | ||
25e9e756 | 214 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
2e74796a JN |
215 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
216 | SNDRV_PCM_STREAM_CAPTURE); | |
217 | if (ret) | |
218 | goto out; | |
219 | } | |
220 | ||
221 | out: | |
fad9365b OM |
222 | /* free preallocated buffers in case of error */ |
223 | if (ret) | |
224 | omap_pcm_free_dma_buffers(pcm); | |
225 | ||
2e74796a JN |
226 | return ret; |
227 | } | |
228 | ||
f0fba2ad LG |
229 | static struct snd_soc_platform_driver omap_soc_platform = { |
230 | .ops = &omap_pcm_ops, | |
2e74796a JN |
231 | .pcm_new = omap_pcm_new, |
232 | .pcm_free = omap_pcm_free_dma_buffers, | |
233 | }; | |
2e74796a | 234 | |
7ff60006 | 235 | static int omap_pcm_probe(struct platform_device *pdev) |
f0fba2ad LG |
236 | { |
237 | return snd_soc_register_platform(&pdev->dev, | |
238 | &omap_soc_platform); | |
239 | } | |
240 | ||
7ff60006 | 241 | static int omap_pcm_remove(struct platform_device *pdev) |
f0fba2ad LG |
242 | { |
243 | snd_soc_unregister_platform(&pdev->dev); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static struct platform_driver omap_pcm_driver = { | |
248 | .driver = { | |
249 | .name = "omap-pcm-audio", | |
250 | .owner = THIS_MODULE, | |
251 | }, | |
252 | ||
253 | .probe = omap_pcm_probe, | |
7ff60006 | 254 | .remove = omap_pcm_remove, |
f0fba2ad LG |
255 | }; |
256 | ||
beda5bf5 | 257 | module_platform_driver(omap_pcm_driver); |
958e792c | 258 | |
7ec41ee5 | 259 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>"); |
2e74796a JN |
260 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
261 | MODULE_LICENSE("GPL"); | |
5e70b7fc | 262 | MODULE_ALIAS("platform:omap-pcm-audio"); |