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 | * |
| 6 | * Contact: Jarkko Nikula <jarkko.nikula@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
| 29 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 30 | #include <mach/dma.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 31 | #include "omap-pcm.h" |
| 32 | |
| 33 | static const struct snd_pcm_hardware omap_pcm_hardware = { |
| 34 | .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 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 40 | .period_bytes_min = 32, |
| 41 | .period_bytes_max = 64 * 1024, |
| 42 | .periods_min = 2, |
| 43 | .periods_max = 255, |
| 44 | .buffer_bytes_max = 128 * 1024, |
| 45 | }; |
| 46 | |
| 47 | struct omap_runtime_data { |
| 48 | spinlock_t lock; |
| 49 | struct omap_pcm_dma_data *dma_data; |
| 50 | int dma_ch; |
| 51 | int period_index; |
| 52 | }; |
| 53 | |
| 54 | static void omap_pcm_dma_irq(int ch, u16 stat, void *data) |
| 55 | { |
| 56 | struct snd_pcm_substream *substream = data; |
| 57 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 58 | struct omap_runtime_data *prtd = runtime->private_data; |
| 59 | unsigned long flags; |
| 60 | |
| 61 | if (cpu_is_omap1510()) { |
| 62 | /* |
| 63 | * OMAP1510 doesn't support DMA chaining so have to restart |
| 64 | * the transfer after all periods are transferred |
| 65 | */ |
| 66 | spin_lock_irqsave(&prtd->lock, flags); |
| 67 | if (prtd->period_index >= 0) { |
| 68 | if (++prtd->period_index == runtime->periods) { |
| 69 | prtd->period_index = 0; |
| 70 | omap_start_dma(prtd->dma_ch); |
| 71 | } |
| 72 | } |
| 73 | spin_unlock_irqrestore(&prtd->lock, flags); |
| 74 | } |
| 75 | |
| 76 | snd_pcm_period_elapsed(substream); |
| 77 | } |
| 78 | |
| 79 | /* this may get called several times by oss emulation */ |
| 80 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, |
| 81 | struct snd_pcm_hw_params *params) |
| 82 | { |
| 83 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 84 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 85 | struct omap_runtime_data *prtd = runtime->private_data; |
| 86 | struct omap_pcm_dma_data *dma_data = rtd->dai->cpu_dai->dma_data; |
| 87 | int err = 0; |
| 88 | |
| 89 | if (!dma_data) |
| 90 | return -ENODEV; |
| 91 | |
| 92 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 93 | runtime->dma_bytes = params_buffer_bytes(params); |
| 94 | |
| 95 | if (prtd->dma_data) |
| 96 | return 0; |
| 97 | prtd->dma_data = dma_data; |
| 98 | err = omap_request_dma(dma_data->dma_req, dma_data->name, |
| 99 | omap_pcm_dma_irq, substream, &prtd->dma_ch); |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 100 | if (!err & !cpu_is_omap1510()) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Link channel with itself so DMA doesn't need any |
| 103 | * reprogramming while looping the buffer |
| 104 | */ |
| 105 | omap_dma_link_lch(prtd->dma_ch, prtd->dma_ch); |
| 106 | } |
| 107 | |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
| 112 | { |
| 113 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 114 | struct omap_runtime_data *prtd = runtime->private_data; |
| 115 | |
| 116 | if (prtd->dma_data == NULL) |
| 117 | return 0; |
| 118 | |
| 119 | if (!cpu_is_omap1510()) |
| 120 | omap_dma_unlink_lch(prtd->dma_ch, prtd->dma_ch); |
| 121 | omap_free_dma(prtd->dma_ch); |
| 122 | prtd->dma_data = NULL; |
| 123 | |
| 124 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int omap_pcm_prepare(struct snd_pcm_substream *substream) |
| 130 | { |
| 131 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 132 | struct omap_runtime_data *prtd = runtime->private_data; |
| 133 | struct omap_pcm_dma_data *dma_data = prtd->dma_data; |
| 134 | struct omap_dma_channel_params dma_params; |
| 135 | |
| 136 | memset(&dma_params, 0, sizeof(dma_params)); |
| 137 | /* |
| 138 | * Note: Regardless of interface data formats supported by OMAP McBSP |
| 139 | * or EAC blocks, internal representation is always fixed 16-bit/sample |
| 140 | */ |
| 141 | dma_params.data_type = OMAP_DMA_DATA_TYPE_S16; |
| 142 | dma_params.trigger = dma_data->dma_req; |
| 143 | dma_params.sync_mode = OMAP_DMA_SYNC_ELEMENT; |
| 144 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 145 | dma_params.src_amode = OMAP_DMA_AMODE_POST_INC; |
| 146 | dma_params.dst_amode = OMAP_DMA_AMODE_CONSTANT; |
| 147 | dma_params.src_or_dst_synch = OMAP_DMA_DST_SYNC; |
| 148 | dma_params.src_start = runtime->dma_addr; |
| 149 | dma_params.dst_start = dma_data->port_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 150 | dma_params.dst_port = OMAP_DMA_PORT_MPUI; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 151 | } else { |
| 152 | dma_params.src_amode = OMAP_DMA_AMODE_CONSTANT; |
| 153 | dma_params.dst_amode = OMAP_DMA_AMODE_POST_INC; |
| 154 | dma_params.src_or_dst_synch = OMAP_DMA_SRC_SYNC; |
| 155 | dma_params.src_start = dma_data->port_addr; |
| 156 | dma_params.dst_start = runtime->dma_addr; |
Arun KS | 9d37484 | 2008-09-30 15:35:16 +0530 | [diff] [blame] | 157 | dma_params.src_port = OMAP_DMA_PORT_MPUI; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 158 | } |
| 159 | /* |
| 160 | * Set DMA transfer frame size equal to ALSA period size and frame |
| 161 | * count as no. of ALSA periods. Then with DMA frame interrupt enabled, |
| 162 | * we can transfer the whole ALSA buffer with single DMA transfer but |
| 163 | * still can get an interrupt at each period bounary |
| 164 | */ |
| 165 | dma_params.elem_count = snd_pcm_lib_period_bytes(substream) / 2; |
| 166 | dma_params.frame_count = runtime->periods; |
| 167 | omap_set_dma_params(prtd->dma_ch, &dma_params); |
| 168 | |
| 169 | omap_enable_dma_irq(prtd->dma_ch, OMAP_DMA_FRAME_IRQ); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 175 | { |
| 176 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 177 | struct omap_runtime_data *prtd = runtime->private_data; |
| 178 | int ret = 0; |
| 179 | |
| 180 | spin_lock_irq(&prtd->lock); |
| 181 | switch (cmd) { |
| 182 | case SNDRV_PCM_TRIGGER_START: |
| 183 | case SNDRV_PCM_TRIGGER_RESUME: |
| 184 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 185 | prtd->period_index = 0; |
| 186 | omap_start_dma(prtd->dma_ch); |
| 187 | break; |
| 188 | |
| 189 | case SNDRV_PCM_TRIGGER_STOP: |
| 190 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 191 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 192 | prtd->period_index = -1; |
| 193 | omap_stop_dma(prtd->dma_ch); |
| 194 | break; |
| 195 | default: |
| 196 | ret = -EINVAL; |
| 197 | } |
| 198 | spin_unlock_irq(&prtd->lock); |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
| 204 | { |
| 205 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 206 | struct omap_runtime_data *prtd = runtime->private_data; |
| 207 | dma_addr_t ptr; |
| 208 | snd_pcm_uframes_t offset; |
| 209 | |
| 210 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 211 | ptr = omap_get_dma_src_pos(prtd->dma_ch); |
| 212 | else |
| 213 | ptr = omap_get_dma_dst_pos(prtd->dma_ch); |
| 214 | |
| 215 | offset = bytes_to_frames(runtime, ptr - runtime->dma_addr); |
| 216 | if (offset >= runtime->buffer_size) |
| 217 | offset = 0; |
| 218 | |
| 219 | return offset; |
| 220 | } |
| 221 | |
| 222 | static int omap_pcm_open(struct snd_pcm_substream *substream) |
| 223 | { |
| 224 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 225 | struct omap_runtime_data *prtd; |
| 226 | int ret; |
| 227 | |
| 228 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); |
| 229 | |
| 230 | /* Ensure that buffer size is a multiple of period size */ |
| 231 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 232 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 233 | if (ret < 0) |
| 234 | goto out; |
| 235 | |
| 236 | prtd = kzalloc(sizeof(prtd), GFP_KERNEL); |
| 237 | if (prtd == NULL) { |
| 238 | ret = -ENOMEM; |
| 239 | goto out; |
| 240 | } |
| 241 | spin_lock_init(&prtd->lock); |
| 242 | runtime->private_data = prtd; |
| 243 | |
| 244 | out: |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static int omap_pcm_close(struct snd_pcm_substream *substream) |
| 249 | { |
| 250 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 251 | |
| 252 | kfree(runtime->private_data); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
| 257 | struct vm_area_struct *vma) |
| 258 | { |
| 259 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 260 | |
| 261 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 262 | runtime->dma_area, |
| 263 | runtime->dma_addr, |
| 264 | runtime->dma_bytes); |
| 265 | } |
| 266 | |
| 267 | struct snd_pcm_ops omap_pcm_ops = { |
| 268 | .open = omap_pcm_open, |
| 269 | .close = omap_pcm_close, |
| 270 | .ioctl = snd_pcm_lib_ioctl, |
| 271 | .hw_params = omap_pcm_hw_params, |
| 272 | .hw_free = omap_pcm_hw_free, |
| 273 | .prepare = omap_pcm_prepare, |
| 274 | .trigger = omap_pcm_trigger, |
| 275 | .pointer = omap_pcm_pointer, |
| 276 | .mmap = omap_pcm_mmap, |
| 277 | }; |
| 278 | |
| 279 | static u64 omap_pcm_dmamask = DMA_BIT_MASK(32); |
| 280 | |
| 281 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 282 | int stream) |
| 283 | { |
| 284 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 285 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 286 | size_t size = omap_pcm_hardware.buffer_bytes_max; |
| 287 | |
| 288 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 289 | buf->dev.dev = pcm->card->dev; |
| 290 | buf->private_data = NULL; |
| 291 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 292 | &buf->addr, GFP_KERNEL); |
| 293 | if (!buf->area) |
| 294 | return -ENOMEM; |
| 295 | |
| 296 | buf->bytes = size; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 301 | { |
| 302 | struct snd_pcm_substream *substream; |
| 303 | struct snd_dma_buffer *buf; |
| 304 | int stream; |
| 305 | |
| 306 | for (stream = 0; stream < 2; stream++) { |
| 307 | substream = pcm->streams[stream].substream; |
| 308 | if (!substream) |
| 309 | continue; |
| 310 | |
| 311 | buf = &substream->dma_buffer; |
| 312 | if (!buf->area) |
| 313 | continue; |
| 314 | |
| 315 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 316 | buf->area, buf->addr); |
| 317 | buf->area = NULL; |
| 318 | } |
| 319 | } |
| 320 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 321 | 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] | 322 | struct snd_pcm *pcm) |
| 323 | { |
| 324 | int ret = 0; |
| 325 | |
| 326 | if (!card->dev->dma_mask) |
| 327 | card->dev->dma_mask = &omap_pcm_dmamask; |
| 328 | if (!card->dev->coherent_dma_mask) |
| 329 | card->dev->coherent_dma_mask = DMA_32BIT_MASK; |
| 330 | |
| 331 | if (dai->playback.channels_min) { |
| 332 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 333 | SNDRV_PCM_STREAM_PLAYBACK); |
| 334 | if (ret) |
| 335 | goto out; |
| 336 | } |
| 337 | |
| 338 | if (dai->capture.channels_min) { |
| 339 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 340 | SNDRV_PCM_STREAM_CAPTURE); |
| 341 | if (ret) |
| 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | out: |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | struct snd_soc_platform omap_soc_platform = { |
| 350 | .name = "omap-pcm-audio", |
| 351 | .pcm_ops = &omap_pcm_ops, |
| 352 | .pcm_new = omap_pcm_new, |
| 353 | .pcm_free = omap_pcm_free_dma_buffers, |
| 354 | }; |
| 355 | EXPORT_SYMBOL_GPL(omap_soc_platform); |
| 356 | |
| 357 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>"); |
| 358 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
| 359 | MODULE_LICENSE("GPL"); |