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 | * | |
b08f7a62 JN |
6 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
7 | * Peter Ujfalusi <peter.ujfalusi@nokia.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> | |
26 | #include <sound/core.h> | |
27 | #include <sound/pcm.h> | |
28 | #include <sound/pcm_params.h> | |
29 | #include <sound/soc.h> | |
30 | ||
ce491cf8 | 31 | #include <plat/dma.h> |
2e74796a JN |
32 | #include "omap-pcm.h" |
33 | ||
34 | static const struct snd_pcm_hardware omap_pcm_hardware = { | |
35 | .info = SNDRV_PCM_INFO_MMAP | | |
36 | SNDRV_PCM_INFO_MMAP_VALID | | |
37 | SNDRV_PCM_INFO_INTERLEAVED | | |
38 | SNDRV_PCM_INFO_PAUSE | | |
39 | SNDRV_PCM_INFO_RESUME, | |
e17dd32f MLC |
40 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
41 | SNDRV_PCM_FMTBIT_S32_LE, | |
2e74796a JN |
42 | .period_bytes_min = 32, |
43 | .period_bytes_max = 64 * 1024, | |
44 | .periods_min = 2, | |
45 | .periods_max = 255, | |
46 | .buffer_bytes_max = 128 * 1024, | |
47 | }; | |
48 | ||
49 | struct omap_runtime_data { | |
50 | spinlock_t lock; | |
51 | struct omap_pcm_dma_data *dma_data; | |
52 | int dma_ch; | |
53 | int period_index; | |
54 | }; | |
55 | ||
56 | static void omap_pcm_dma_irq(int ch, u16 stat, void *data) | |
57 | { | |
58 | struct snd_pcm_substream *substream = data; | |
59 | struct snd_pcm_runtime *runtime = substream->runtime; | |
60 | struct omap_runtime_data *prtd = runtime->private_data; | |
61 | unsigned long flags; | |
62 | ||
64844a6a JK |
63 | if ((cpu_is_omap1510()) && |
64 | (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) { | |
2e74796a | 65 | /* |
64844a6a JK |
66 | * OMAP1510 doesn't fully support DMA progress counter |
67 | * and there is no software emulation implemented yet, | |
68 | * so have to maintain our own playback progress counter | |
69 | * that can be used by omap_pcm_pointer() instead. | |
2e74796a JN |
70 | */ |
71 | spin_lock_irqsave(&prtd->lock, flags); | |
471e3dec JK |
72 | if ((stat == OMAP_DMA_LAST_IRQ) && |
73 | (prtd->period_index == runtime->periods - 1)) { | |
74 | /* we are in sync, do nothing */ | |
75 | spin_unlock_irqrestore(&prtd->lock, flags); | |
76 | return; | |
77 | } | |
2e74796a | 78 | if (prtd->period_index >= 0) { |
471e3dec JK |
79 | if (stat & OMAP_DMA_BLOCK_IRQ) { |
80 | /* end of buffer reached, loop back */ | |
81 | prtd->period_index = 0; | |
82 | } else if (stat & OMAP_DMA_LAST_IRQ) { | |
83 | /* update the counter for the last period */ | |
84 | prtd->period_index = runtime->periods - 1; | |
85 | } else if (++prtd->period_index >= runtime->periods) { | |
86 | /* end of buffer missed? loop back */ | |
2e74796a | 87 | prtd->period_index = 0; |
2e74796a JN |
88 | } |
89 | } | |
90 | spin_unlock_irqrestore(&prtd->lock, flags); | |
91 | } | |
92 | ||
93 | snd_pcm_period_elapsed(substream); | |
94 | } | |
95 | ||
96 | /* this may get called several times by oss emulation */ | |
97 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, | |
98 | struct snd_pcm_hw_params *params) | |
99 | { | |
100 | struct snd_pcm_runtime *runtime = substream->runtime; | |
101 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
102 | struct omap_runtime_data *prtd = runtime->private_data; | |
103 | struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data; | |
104 | int err = 0; | |
105 | ||
1b4246a1 JS |
106 | /* return if this is a bufferless transfer e.g. |
107 | * codec <--> BT codec or GSM modem -- lg FIXME */ | |
2e74796a | 108 | if (!dma_data) |
1b4246a1 | 109 | return 0; |
2e74796a JN |
110 | |
111 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); | |
112 | runtime->dma_bytes = params_buffer_bytes(params); | |
113 | ||
114 | if (prtd->dma_data) | |
115 | return 0; | |
116 | prtd->dma_data = dma_data; | |
117 | err = omap_request_dma(dma_data->dma_req, dma_data->name, | |
118 | omap_pcm_dma_irq, substream, &prtd->dma_ch); | |
64844a6a | 119 | if (!err) { |
2e74796a JN |
120 | /* |
121 | * Link channel with itself so DMA doesn't need any | |
122 | * reprogramming while looping the buffer | |
123 | */ | |
124 | omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch); | |
125 | } | |
126 | ||
127 | return err; | |
128 | } | |
129 | ||
130 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) | |
131 | { | |
132 | struct snd_pcm_runtime *runtime = substream->runtime; | |
133 | struct omap_runtime_data *prtd = runtime->private_data; | |
134 | ||
135 | if (prtd->dma_data == NULL) | |
136 | return 0; | |
137 | ||
64844a6a | 138 | omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch); |
2e74796a JN |
139 | omap_free_dma(prtd->dma_ch); |
140 | prtd->dma_data = NULL; | |
141 | ||
142 | snd_pcm_set_runtime_buffer(substream, NULL); | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static int omap_pcm_prepare(struct snd_pcm_substream *substream) | |
148 | { | |
149 | struct snd_pcm_runtime *runtime = substream->runtime; | |
150 | struct omap_runtime_data *prtd = runtime->private_data; | |
151 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; | |
152 | struct omap_dma_channel_params dma_params; | |
e17dd32f | 153 | int bytes; |
2e74796a | 154 | |
1b4246a1 JS |
155 | /* return if this is a bufferless transfer e.g. |
156 | * codec <--> BT codec or GSM modem -- lg FIXME */ | |
157 | if (!prtd->dma_data) | |
158 | return 0; | |
159 | ||
2e74796a | 160 | memset(&dma_params, 0, sizeof(dma_params)); |
e17dd32f | 161 | dma_params.data_type = dma_data->data_type; |
2e74796a | 162 | dma_params.trigger = dma_data->dma_req; |
caebc0cb | 163 | dma_params.sync_mode = dma_data->sync_mode; |
2e74796a JN |
164 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
165 | dma_params.src_amode = OMAP_DMA_AMODE_POST_INC; | |
166 | dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT; | |
167 | dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC; | |
168 | dma_params.src_start = runtime->dma_addr; | |
169 | dma_params.dst_start = dma_data->port_addr; | |
9d37484c | 170 | dma_params.dst_port = OMAP_DMA_PORT_MPUI; |
e17dd32f | 171 | dma_params.dst_fi = dma_data->packet_size; |
2e74796a JN |
172 | } else { |
173 | dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT; | |
174 | dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC; | |
175 | dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC; | |
176 | dma_params.src_start = dma_data->port_addr; | |
177 | dma_params.dst_start = runtime->dma_addr; | |
9d37484c | 178 | dma_params.src_port = OMAP_DMA_PORT_MPUI; |
e17dd32f | 179 | dma_params.src_fi = dma_data->packet_size; |
2e74796a JN |
180 | } |
181 | /* | |
182 | * Set DMA transfer frame size equal to ALSA period size and frame | |
183 | * count as no. of ALSA periods. Then with DMA frame interrupt enabled, | |
184 | * we can transfer the whole ALSA buffer with single DMA transfer but | |
185 | * still can get an interrupt at each period bounary | |
186 | */ | |
e17dd32f MLC |
187 | bytes = snd_pcm_lib_period_bytes(substream); |
188 | dma_params.elem_count = bytes >> dma_data->data_type; | |
2e74796a JN |
189 | dma_params.frame_count = runtime->periods; |
190 | omap_set_dma_params(prtd->dma_ch, &dma_params); | |
191 | ||
471e3dec JK |
192 | if ((cpu_is_omap1510()) && |
193 | (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) | |
194 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ | | |
195 | OMAP_DMA_LAST_IRQ | OMAP_DMA_BLOCK_IRQ); | |
196 | else | |
197 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ); | |
2e74796a | 198 | |
4d187fb8 JK |
199 | if (!(cpu_class_is_omap1())) { |
200 | omap_set_dma_src_burst_mode(prtd->dma_ch, | |
201 | OMAP_DMA_DATA_BURST_16); | |
202 | omap_set_dma_dest_burst_mode(prtd->dma_ch, | |
203 | OMAP_DMA_DATA_BURST_16); | |
204 | } | |
9599d485 | 205 | |
2e74796a JN |
206 | return 0; |
207 | } | |
208 | ||
209 | static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
210 | { | |
211 | struct snd_pcm_runtime *runtime = substream->runtime; | |
212 | struct omap_runtime_data *prtd = runtime->private_data; | |
caebc0cb | 213 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; |
21dff434 | 214 | unsigned long flags; |
2e74796a JN |
215 | int ret = 0; |
216 | ||
21dff434 | 217 | spin_lock_irqsave(&prtd->lock, flags); |
2e74796a JN |
218 | switch (cmd) { |
219 | case SNDRV_PCM_TRIGGER_START: | |
220 | case SNDRV_PCM_TRIGGER_RESUME: | |
221 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
222 | prtd->period_index = 0; | |
caebc0cb EV |
223 | /* Configure McBSP internal buffer usage */ |
224 | if (dma_data->set_threshold) | |
225 | dma_data->set_threshold(substream); | |
226 | ||
2e74796a JN |
227 | omap_start_dma(prtd->dma_ch); |
228 | break; | |
229 | ||
230 | case SNDRV_PCM_TRIGGER_STOP: | |
231 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
232 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
233 | prtd->period_index = -1; | |
234 | omap_stop_dma(prtd->dma_ch); | |
235 | break; | |
236 | default: | |
237 | ret = -EINVAL; | |
238 | } | |
21dff434 | 239 | spin_unlock_irqrestore(&prtd->lock, flags); |
2e74796a JN |
240 | |
241 | return ret; | |
242 | } | |
243 | ||
244 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) | |
245 | { | |
246 | struct snd_pcm_runtime *runtime = substream->runtime; | |
247 | struct omap_runtime_data *prtd = runtime->private_data; | |
248 | dma_addr_t ptr; | |
249 | snd_pcm_uframes_t offset; | |
250 | ||
1bdd7419 | 251 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
2e74796a | 252 | ptr = omap_get_dma_dst_pos(prtd->dma_ch); |
1bdd7419 JK |
253 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
254 | } else if (!(cpu_is_omap1510())) { | |
255 | ptr = omap_get_dma_src_pos(prtd->dma_ch); | |
256 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); | |
257 | } else | |
258 | offset = prtd->period_index * runtime->period_size; | |
2e74796a | 259 | |
2e74796a JN |
260 | if (offset >= runtime->buffer_size) |
261 | offset = 0; | |
262 | ||
263 | return offset; | |
264 | } | |
265 | ||
266 | static int omap_pcm_open(struct snd_pcm_substream *substream) | |
267 | { | |
268 | struct snd_pcm_runtime *runtime = substream->runtime; | |
269 | struct omap_runtime_data *prtd; | |
270 | int ret; | |
271 | ||
272 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); | |
273 | ||
274 | /* Ensure that buffer size is a multiple of period size */ | |
275 | ret = snd_pcm_hw_constraint_integer(runtime, | |
276 | SNDRV_PCM_HW_PARAM_PERIODS); | |
277 | if (ret < 0) | |
278 | goto out; | |
279 | ||
19b3f316 | 280 | prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); |
2e74796a JN |
281 | if (prtd == NULL) { |
282 | ret = -ENOMEM; | |
283 | goto out; | |
284 | } | |
285 | spin_lock_init(&prtd->lock); | |
286 | runtime->private_data = prtd; | |
287 | ||
288 | out: | |
289 | return ret; | |
290 | } | |
291 | ||
292 | static int omap_pcm_close(struct snd_pcm_substream *substream) | |
293 | { | |
294 | struct snd_pcm_runtime *runtime = substream->runtime; | |
295 | ||
296 | kfree(runtime->private_data); | |
297 | return 0; | |
298 | } | |
299 | ||
300 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, | |
301 | struct vm_area_struct *vma) | |
302 | { | |
303 | struct snd_pcm_runtime *runtime = substream->runtime; | |
304 | ||
305 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, | |
306 | runtime->dma_area, | |
307 | runtime->dma_addr, | |
308 | runtime->dma_bytes); | |
309 | } | |
310 | ||
b2a19d02 | 311 | static struct snd_pcm_ops omap_pcm_ops = { |
2e74796a JN |
312 | .open = omap_pcm_open, |
313 | .close = omap_pcm_close, | |
314 | .ioctl = snd_pcm_lib_ioctl, | |
315 | .hw_params = omap_pcm_hw_params, | |
316 | .hw_free = omap_pcm_hw_free, | |
317 | .prepare = omap_pcm_prepare, | |
318 | .trigger = omap_pcm_trigger, | |
319 | .pointer = omap_pcm_pointer, | |
320 | .mmap = omap_pcm_mmap, | |
321 | }; | |
322 | ||
a152ff24 | 323 | static u64 omap_pcm_dmamask = DMA_BIT_MASK(64); |
2e74796a JN |
324 | |
325 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, | |
326 | int stream) | |
327 | { | |
328 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; | |
329 | struct snd_dma_buffer *buf = &substream->dma_buffer; | |
330 | size_t size = omap_pcm_hardware.buffer_bytes_max; | |
331 | ||
332 | buf->dev.type = SNDRV_DMA_TYPE_DEV; | |
333 | buf->dev.dev = pcm->card->dev; | |
334 | buf->private_data = NULL; | |
335 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, | |
336 | &buf->addr, GFP_KERNEL); | |
337 | if (!buf->area) | |
338 | return -ENOMEM; | |
339 | ||
340 | buf->bytes = size; | |
341 | return 0; | |
342 | } | |
343 | ||
344 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) | |
345 | { | |
346 | struct snd_pcm_substream *substream; | |
347 | struct snd_dma_buffer *buf; | |
348 | int stream; | |
349 | ||
350 | for (stream = 0; stream < 2; stream++) { | |
351 | substream = pcm->streams[stream].substream; | |
352 | if (!substream) | |
353 | continue; | |
354 | ||
355 | buf = &substream->dma_buffer; | |
356 | if (!buf->area) | |
357 | continue; | |
358 | ||
359 | dma_free_writecombine(pcm->card->dev, buf->bytes, | |
360 | buf->area, buf->addr); | |
361 | buf->area = NULL; | |
362 | } | |
363 | } | |
364 | ||
d756b277 | 365 | static int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai, |
2e74796a JN |
366 | struct snd_pcm *pcm) |
367 | { | |
368 | int ret = 0; | |
369 | ||
370 | if (!card->dev->dma_mask) | |
371 | card->dev->dma_mask = &omap_pcm_dmamask; | |
372 | if (!card->dev->coherent_dma_mask) | |
a152ff24 | 373 | card->dev->coherent_dma_mask = DMA_BIT_MASK(64); |
2e74796a JN |
374 | |
375 | if (dai->playback.channels_min) { | |
376 | ret = omap_pcm_preallocate_dma_buffer(pcm, | |
377 | SNDRV_PCM_STREAM_PLAYBACK); | |
378 | if (ret) | |
379 | goto out; | |
380 | } | |
381 | ||
382 | if (dai->capture.channels_min) { | |
383 | ret = omap_pcm_preallocate_dma_buffer(pcm, | |
384 | SNDRV_PCM_STREAM_CAPTURE); | |
385 | if (ret) | |
386 | goto out; | |
387 | } | |
388 | ||
389 | out: | |
390 | return ret; | |
391 | } | |
392 | ||
393 | struct snd_soc_platform omap_soc_platform = { | |
394 | .name = "omap-pcm-audio", | |
395 | .pcm_ops = &omap_pcm_ops, | |
396 | .pcm_new = omap_pcm_new, | |
397 | .pcm_free = omap_pcm_free_dma_buffers, | |
398 | }; | |
399 | EXPORT_SYMBOL_GPL(omap_soc_platform); | |
400 | ||
c9b3a40f | 401 | static int __init omap_soc_platform_init(void) |
958e792c MB |
402 | { |
403 | return snd_soc_register_platform(&omap_soc_platform); | |
404 | } | |
405 | module_init(omap_soc_platform_init); | |
406 | ||
407 | static void __exit omap_soc_platform_exit(void) | |
408 | { | |
409 | snd_soc_unregister_platform(&omap_soc_platform); | |
410 | } | |
411 | module_exit(omap_soc_platform_exit); | |
412 | ||
b08f7a62 | 413 | MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>"); |
2e74796a JN |
414 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
415 | MODULE_LICENSE("GPL"); |