Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap-pcm.c -- ALSA PCM interface for the OMAP SoC |
| 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 6 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
| 7 | * Peter Ujfalusi <peter.ujfalusi@nokia.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 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 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 31 | #include <mach/dma.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 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, |
| 40 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 41 | .period_bytes_min = 32, |
| 42 | .period_bytes_max = 64 * 1024, |
| 43 | .periods_min = 2, |
| 44 | .periods_max = 255, |
| 45 | .buffer_bytes_max = 128 * 1024, |
| 46 | }; |
| 47 | |
| 48 | struct omap_runtime_data { |
| 49 | spinlock_t lock; |
| 50 | struct omap_pcm_dma_data *dma_data; |
| 51 | int dma_ch; |
| 52 | int period_index; |
| 53 | }; |
| 54 | |
| 55 | static void omap_pcm_dma_irq(int ch, u16 stat, void *data) |
| 56 | { |
| 57 | struct snd_pcm_substream *substream = data; |
| 58 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 59 | struct omap_runtime_data *prtd = runtime->private_data; |
| 60 | unsigned long flags; |
| 61 | |
| 62 | if (cpu_is_omap1510()) { |
| 63 | /* |
| 64 | * OMAP1510 doesn't support DMA chaining so have to restart |
| 65 | * the transfer after all periods are transferred |
| 66 | */ |
| 67 | spin_lock_irqsave(&prtd->lock, flags); |
| 68 | if (prtd->period_index >= 0) { |
| 69 | if (++prtd->period_index == runtime->periods) { |
| 70 | prtd->period_index = 0; |
| 71 | omap_start_dma(prtd->dma_ch); |
| 72 | } |
| 73 | } |
| 74 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 75 | } |
| 76 | |
| 77 | snd_pcm_period_elapsed(substream); |
| 78 | } |
| 79 | |
| 80 | /* this may get called several times by oss emulation */ |
| 81 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, |
| 82 | struct snd_pcm_hw_params *params) |
| 83 | { |
| 84 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 85 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 86 | struct omap_runtime_data *prtd = runtime->private_data; |
| 87 | struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data; |
| 88 | int err = 0; |
| 89 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 90 | /* return if this is a bufferless transfer e.g. |
| 91 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 92 | if (!dma_data) |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 93 | return 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 94 | |
| 95 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 96 | runtime->dma_bytes = params_buffer_bytes(params); |
| 97 | |
| 98 | if (prtd->dma_data) |
| 99 | return 0; |
| 100 | prtd->dma_data = dma_data; |
| 101 | err = omap_request_dma(dma_data->dma_req, dma_data->name, |
| 102 | omap_pcm_dma_irq, substream, &prtd->dma_ch); |
Roel Kluin | 472346d | 2008-12-22 17:40:45 +0100 | [diff] [blame] | 103 | if (!err && !cpu_is_omap1510()) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 104 | /* |
| 105 | * Link channel with itself so DMA doesn't need any |
| 106 | * reprogramming while looping the buffer |
| 107 | */ |
| 108 | omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch); |
| 109 | } |
| 110 | |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
| 115 | { |
| 116 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 117 | struct omap_runtime_data *prtd = runtime->private_data; |
| 118 | |
| 119 | if (prtd->dma_data == NULL) |
| 120 | return 0; |
| 121 | |
| 122 | if (!cpu_is_omap1510()) |
| 123 | omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch); |
| 124 | omap_free_dma(prtd->dma_ch); |
| 125 | prtd->dma_data = NULL; |
| 126 | |
| 127 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int omap_pcm_prepare(struct snd_pcm_substream *substream) |
| 133 | { |
| 134 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 135 | struct omap_runtime_data *prtd = runtime->private_data; |
| 136 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; |
| 137 | struct omap_dma_channel_params dma_params; |
| 138 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 139 | /* return if this is a bufferless transfer e.g. |
| 140 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
| 141 | if (!prtd->dma_data) |
| 142 | return 0; |
| 143 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 144 | memset(&dma_params, 0, sizeof(dma_params)); |
| 145 | /* |
| 146 | * Note: Regardless of interface data formats supported by OMAP McBSP |
| 147 | * or EAC blocks, internal representation is always fixed 16-bit/sample |
| 148 | */ |
| 149 | dma_params.data_type = OMAP_DMA_DATA_TYPE_S16; |
| 150 | dma_params.trigger = dma_data->dma_req; |
| 151 | dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT; |
| 152 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 153 | dma_params.src_amode = OMAP_DMA_AMODE_POST_INC; |
| 154 | dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT; |
| 155 | dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC; |
| 156 | dma_params.src_start = runtime->dma_addr; |
| 157 | dma_params.dst_start = dma_data->port_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 158 | dma_params.dst_port = OMAP_DMA_PORT_MPUI; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 159 | } else { |
| 160 | dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT; |
| 161 | dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC; |
| 162 | dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC; |
| 163 | dma_params.src_start = dma_data->port_addr; |
| 164 | dma_params.dst_start = runtime->dma_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 165 | dma_params.src_port = OMAP_DMA_PORT_MPUI; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 166 | } |
| 167 | /* |
| 168 | * Set DMA transfer frame size equal to ALSA period size and frame |
| 169 | * count as no. of ALSA periods. Then with DMA frame interrupt enabled, |
| 170 | * we can transfer the whole ALSA buffer with single DMA transfer but |
| 171 | * still can get an interrupt at each period bounary |
| 172 | */ |
| 173 | dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2; |
| 174 | dma_params.frame_count = runtime->periods; |
| 175 | omap_set_dma_params(prtd->dma_ch, &dma_params); |
| 176 | |
| 177 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 183 | { |
| 184 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 185 | struct omap_runtime_data *prtd = runtime->private_data; |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 186 | unsigned long flags; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 187 | int ret = 0; |
| 188 | |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 189 | spin_lock_irqsave(&prtd->lock, flags); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 190 | switch (cmd) { |
| 191 | case SNDRV_PCM_TRIGGER_START: |
| 192 | case SNDRV_PCM_TRIGGER_RESUME: |
| 193 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 194 | prtd->period_index = 0; |
| 195 | omap_start_dma(prtd->dma_ch); |
| 196 | break; |
| 197 | |
| 198 | case SNDRV_PCM_TRIGGER_STOP: |
| 199 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 200 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 201 | prtd->period_index = -1; |
| 202 | omap_stop_dma(prtd->dma_ch); |
| 203 | break; |
| 204 | default: |
| 205 | ret = -EINVAL; |
| 206 | } |
Eero Nurkkala | 21dff43 | 2009-02-02 14:20:46 +0200 | [diff] [blame] | 207 | spin_unlock_irqrestore(&prtd->lock, flags); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
| 213 | { |
| 214 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 215 | struct omap_runtime_data *prtd = runtime->private_data; |
| 216 | dma_addr_t ptr; |
| 217 | snd_pcm_uframes_t offset; |
| 218 | |
| 219 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 220 | ptr = omap_get_dma_src_pos(prtd->dma_ch); |
| 221 | else |
| 222 | ptr = omap_get_dma_dst_pos(prtd->dma_ch); |
| 223 | |
| 224 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
| 225 | if (offset >= runtime->buffer_size) |
| 226 | offset = 0; |
| 227 | |
| 228 | return offset; |
| 229 | } |
| 230 | |
| 231 | static int omap_pcm_open(struct snd_pcm_substream *substream) |
| 232 | { |
| 233 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 234 | struct omap_runtime_data *prtd; |
| 235 | int ret; |
| 236 | |
| 237 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); |
| 238 | |
| 239 | /* Ensure that buffer size is a multiple of period size */ |
| 240 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 241 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 242 | if (ret < 0) |
| 243 | goto out; |
| 244 | |
Stanley Miao | 19b3f31 | 2008-12-19 22:08:22 +0800 | [diff] [blame] | 245 | prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 246 | if (prtd == NULL) { |
| 247 | ret = -ENOMEM; |
| 248 | goto out; |
| 249 | } |
| 250 | spin_lock_init(&prtd->lock); |
| 251 | runtime->private_data = prtd; |
| 252 | |
| 253 | out: |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static int omap_pcm_close(struct snd_pcm_substream *substream) |
| 258 | { |
| 259 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 260 | |
| 261 | kfree(runtime->private_data); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
| 266 | struct vm_area_struct *vma) |
| 267 | { |
| 268 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 269 | |
| 270 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 271 | runtime->dma_area, |
| 272 | runtime->dma_addr, |
| 273 | runtime->dma_bytes); |
| 274 | } |
| 275 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 276 | static struct snd_pcm_ops omap_pcm_ops = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 277 | .open = omap_pcm_open, |
| 278 | .close = omap_pcm_close, |
| 279 | .ioctl = snd_pcm_lib_ioctl, |
| 280 | .hw_params = omap_pcm_hw_params, |
| 281 | .hw_free = omap_pcm_hw_free, |
| 282 | .prepare = omap_pcm_prepare, |
| 283 | .trigger = omap_pcm_trigger, |
| 284 | .pointer = omap_pcm_pointer, |
| 285 | .mmap = omap_pcm_mmap, |
| 286 | }; |
| 287 | |
| 288 | static u64 omap_pcm_dmamask = DMA_BIT_MASK(32); |
| 289 | |
| 290 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 291 | int stream) |
| 292 | { |
| 293 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 294 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 295 | size_t size = omap_pcm_hardware.buffer_bytes_max; |
| 296 | |
| 297 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 298 | buf->dev.dev = pcm->card->dev; |
| 299 | buf->private_data = NULL; |
| 300 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 301 | &buf->addr, GFP_KERNEL); |
| 302 | if (!buf->area) |
| 303 | return -ENOMEM; |
| 304 | |
| 305 | buf->bytes = size; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 310 | { |
| 311 | struct snd_pcm_substream *substream; |
| 312 | struct snd_dma_buffer *buf; |
| 313 | int stream; |
| 314 | |
| 315 | for (stream = 0; stream < 2; stream++) { |
| 316 | substream = pcm->streams[stream].substream; |
| 317 | if (!substream) |
| 318 | continue; |
| 319 | |
| 320 | buf = &substream->dma_buffer; |
| 321 | if (!buf->area) |
| 322 | continue; |
| 323 | |
| 324 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 325 | buf->area, buf->addr); |
| 326 | buf->area = NULL; |
| 327 | } |
| 328 | } |
| 329 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 330 | int omap_pcm_new(struct snd_card *card, struct snd_soc_dai *dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 331 | struct snd_pcm *pcm) |
| 332 | { |
| 333 | int ret = 0; |
| 334 | |
| 335 | if (!card->dev->dma_mask) |
| 336 | card->dev->dma_mask = &omap_pcm_dmamask; |
| 337 | if (!card->dev->coherent_dma_mask) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 338 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 339 | |
| 340 | if (dai->playback.channels_min) { |
| 341 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 342 | SNDRV_PCM_STREAM_PLAYBACK); |
| 343 | if (ret) |
| 344 | goto out; |
| 345 | } |
| 346 | |
| 347 | if (dai->capture.channels_min) { |
| 348 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 349 | SNDRV_PCM_STREAM_CAPTURE); |
| 350 | if (ret) |
| 351 | goto out; |
| 352 | } |
| 353 | |
| 354 | out: |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | struct snd_soc_platform omap_soc_platform = { |
| 359 | .name = "omap-pcm-audio", |
| 360 | .pcm_ops = &omap_pcm_ops, |
| 361 | .pcm_new = omap_pcm_new, |
| 362 | .pcm_free = omap_pcm_free_dma_buffers, |
| 363 | }; |
| 364 | EXPORT_SYMBOL_GPL(omap_soc_platform); |
| 365 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 366 | static int __init omap_soc_platform_init(void) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 367 | { |
| 368 | return snd_soc_register_platform(&omap_soc_platform); |
| 369 | } |
| 370 | module_init(omap_soc_platform_init); |
| 371 | |
| 372 | static void __exit omap_soc_platform_exit(void) |
| 373 | { |
| 374 | snd_soc_unregister_platform(&omap_soc_platform); |
| 375 | } |
| 376 | module_exit(omap_soc_platform_exit); |
| 377 | |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 378 | MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 379 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
| 380 | MODULE_LICENSE("GPL"); |