Cliff Cai | aff0510 | 2008-09-05 18:21:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: sound/soc/blackfin/bf5xx-ac97-pcm.c |
| 3 | * Author: Cliff Cai <Cliff.Cai@analog.com> |
| 4 | * |
| 5 | * Created: Tue June 06 2008 |
| 6 | * Description: DMA Driver for AC97 sound chip |
| 7 | * |
| 8 | * Modified: |
| 9 | * Copyright 2008 Analog Devices Inc. |
| 10 | * |
| 11 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, see the file COPYING, or write |
| 25 | * to the Free Software Foundation, Inc., |
| 26 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | |
| 35 | #include <sound/core.h> |
| 36 | #include <sound/pcm.h> |
| 37 | #include <sound/pcm_params.h> |
| 38 | #include <sound/soc.h> |
| 39 | |
| 40 | #include <asm/dma.h> |
| 41 | |
| 42 | #include "bf5xx-ac97-pcm.h" |
| 43 | #include "bf5xx-ac97.h" |
| 44 | #include "bf5xx-sport.h" |
| 45 | |
| 46 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 47 | static void bf5xx_mmap_copy(struct snd_pcm_substream *substream, |
| 48 | snd_pcm_uframes_t count) |
| 49 | { |
| 50 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 51 | struct sport_device *sport = runtime->private_data; |
| 52 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 53 | bf5xx_pcm_to_ac97( |
| 54 | (struct ac97_frame *)sport->tx_dma_buf + sport->tx_pos, |
| 55 | (__u32 *)runtime->dma_area + sport->tx_pos, count); |
| 56 | sport->tx_pos += runtime->period_size; |
| 57 | if (sport->tx_pos >= runtime->buffer_size) |
| 58 | sport->tx_pos %= runtime->buffer_size; |
| 59 | } else { |
| 60 | bf5xx_ac97_to_pcm( |
| 61 | (struct ac97_frame *)sport->rx_dma_buf + sport->rx_pos, |
| 62 | (__u32 *)runtime->dma_area + sport->rx_pos, count); |
| 63 | sport->rx_pos += runtime->period_size; |
| 64 | if (sport->rx_pos >= runtime->buffer_size) |
| 65 | sport->rx_pos %= runtime->buffer_size; |
| 66 | } |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | static void bf5xx_dma_irq(void *data) |
| 71 | { |
| 72 | struct snd_pcm_substream *pcm = data; |
| 73 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 74 | struct snd_pcm_runtime *runtime = pcm->runtime; |
| 75 | bf5xx_mmap_copy(pcm, runtime->period_size); |
| 76 | #endif |
| 77 | snd_pcm_period_elapsed(pcm); |
| 78 | } |
| 79 | |
| 80 | /* The memory size for pure pcm data is 128*1024 = 0x20000 bytes. |
| 81 | * The total rx/tx buffer is for ac97 frame to hold all pcm data |
| 82 | * is 0x20000 * sizeof(struct ac97_frame) / 4. |
| 83 | */ |
| 84 | #ifdef CONFIG_SND_MMAP_SUPPORT |
| 85 | static const struct snd_pcm_hardware bf5xx_pcm_hardware = { |
| 86 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 87 | SNDRV_PCM_INFO_MMAP | |
| 88 | SNDRV_PCM_INFO_MMAP_VALID | |
| 89 | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 90 | #else |
| 91 | static const struct snd_pcm_hardware bf5xx_pcm_hardware = { |
| 92 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 93 | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 94 | #endif |
| 95 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 96 | .period_bytes_min = 32, |
| 97 | .period_bytes_max = 0x10000, |
| 98 | .periods_min = 1, |
| 99 | .periods_max = PAGE_SIZE/32, |
| 100 | .buffer_bytes_max = 0x20000, /* 128 kbytes */ |
| 101 | .fifo_size = 16, |
| 102 | }; |
| 103 | |
| 104 | static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 105 | struct snd_pcm_hw_params *params) |
| 106 | { |
| 107 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max |
| 108 | * sizeof(struct ac97_frame) / 4; |
| 109 | |
| 110 | snd_pcm_lib_malloc_pages(substream, size); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 116 | { |
| 117 | snd_pcm_lib_free_pages(substream); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream) |
| 122 | { |
| 123 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 124 | struct sport_device *sport = runtime->private_data; |
| 125 | |
| 126 | /* An intermediate buffer is introduced for implementing mmap for |
| 127 | * SPORT working in TMD mode(include AC97). |
| 128 | */ |
| 129 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 130 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max |
| 131 | * sizeof(struct ac97_frame) / 4; |
| 132 | /*clean up intermediate buffer*/ |
| 133 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 134 | memset(sport->tx_dma_buf, 0, size); |
| 135 | sport_set_tx_callback(sport, bf5xx_dma_irq, substream); |
| 136 | sport_config_tx_dma(sport, sport->tx_dma_buf, runtime->periods, |
| 137 | runtime->period_size * sizeof(struct ac97_frame)); |
| 138 | } else { |
| 139 | memset(sport->rx_dma_buf, 0, size); |
| 140 | sport_set_rx_callback(sport, bf5xx_dma_irq, substream); |
| 141 | sport_config_rx_dma(sport, sport->rx_dma_buf, runtime->periods, |
| 142 | runtime->period_size * sizeof(struct ac97_frame)); |
| 143 | } |
| 144 | #else |
| 145 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 146 | sport_set_tx_callback(sport, bf5xx_dma_irq, substream); |
| 147 | sport_config_tx_dma(sport, runtime->dma_area, runtime->periods, |
| 148 | runtime->period_size * sizeof(struct ac97_frame)); |
| 149 | } else { |
| 150 | sport_set_rx_callback(sport, bf5xx_dma_irq, substream); |
| 151 | sport_config_rx_dma(sport, runtime->dma_area, runtime->periods, |
| 152 | runtime->period_size * sizeof(struct ac97_frame)); |
| 153 | } |
| 154 | #endif |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 159 | { |
| 160 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 161 | struct sport_device *sport = runtime->private_data; |
| 162 | int ret = 0; |
| 163 | |
| 164 | pr_debug("%s enter\n", __func__); |
| 165 | switch (cmd) { |
| 166 | case SNDRV_PCM_TRIGGER_START: |
| 167 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 168 | sport_tx_start(sport); |
| 169 | else |
| 170 | sport_rx_start(sport); |
| 171 | break; |
| 172 | case SNDRV_PCM_TRIGGER_STOP: |
| 173 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 174 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 175 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 176 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 177 | sport->tx_pos = 0; |
| 178 | #endif |
| 179 | sport_tx_stop(sport); |
| 180 | } else { |
| 181 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 182 | sport->rx_pos = 0; |
| 183 | #endif |
| 184 | sport_rx_stop(sport); |
| 185 | } |
| 186 | break; |
| 187 | default: |
| 188 | ret = -EINVAL; |
| 189 | } |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream) |
| 194 | { |
| 195 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 196 | struct sport_device *sport = runtime->private_data; |
| 197 | unsigned int curr; |
| 198 | |
| 199 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 200 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 201 | curr = sport->tx_pos; |
| 202 | else |
| 203 | curr = sport->rx_pos; |
| 204 | #else |
| 205 | |
| 206 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 207 | curr = sport_curr_offset_tx(sport) / sizeof(struct ac97_frame); |
| 208 | else |
| 209 | curr = sport_curr_offset_rx(sport) / sizeof(struct ac97_frame); |
| 210 | |
| 211 | #endif |
| 212 | return curr; |
| 213 | } |
| 214 | |
| 215 | static int bf5xx_pcm_open(struct snd_pcm_substream *substream) |
| 216 | { |
| 217 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 218 | int ret; |
| 219 | |
| 220 | pr_debug("%s enter\n", __func__); |
| 221 | snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware); |
| 222 | |
| 223 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 224 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 225 | if (ret < 0) |
| 226 | goto out; |
| 227 | |
| 228 | if (sport_handle != NULL) |
| 229 | runtime->private_data = sport_handle; |
| 230 | else { |
| 231 | pr_err("sport_handle is NULL\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | return 0; |
| 235 | |
| 236 | out: |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | #ifdef CONFIG_SND_MMAP_SUPPORT |
| 241 | static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream, |
| 242 | struct vm_area_struct *vma) |
| 243 | { |
| 244 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 245 | size_t size = vma->vm_end - vma->vm_start; |
| 246 | vma->vm_start = (unsigned long)runtime->dma_area; |
| 247 | vma->vm_end = vma->vm_start + size; |
| 248 | vma->vm_flags |= VM_SHARED; |
| 249 | return 0 ; |
| 250 | } |
| 251 | #else |
| 252 | static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel, |
| 253 | snd_pcm_uframes_t pos, |
| 254 | void __user *buf, snd_pcm_uframes_t count) |
| 255 | { |
| 256 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 257 | |
| 258 | pr_debug("%s copy pos:0x%lx count:0x%lx\n", |
| 259 | substream->stream ? "Capture" : "Playback", pos, count); |
| 260 | |
| 261 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 262 | bf5xx_pcm_to_ac97( |
| 263 | (struct ac97_frame *)runtime->dma_area + pos, |
| 264 | buf, count); |
| 265 | else |
| 266 | bf5xx_ac97_to_pcm( |
| 267 | (struct ac97_frame *)runtime->dma_area + pos, |
| 268 | buf, count); |
| 269 | return 0; |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | struct snd_pcm_ops bf5xx_pcm_ac97_ops = { |
| 274 | .open = bf5xx_pcm_open, |
| 275 | .ioctl = snd_pcm_lib_ioctl, |
| 276 | .hw_params = bf5xx_pcm_hw_params, |
| 277 | .hw_free = bf5xx_pcm_hw_free, |
| 278 | .prepare = bf5xx_pcm_prepare, |
| 279 | .trigger = bf5xx_pcm_trigger, |
| 280 | .pointer = bf5xx_pcm_pointer, |
| 281 | #ifdef CONFIG_SND_MMAP_SUPPORT |
| 282 | .mmap = bf5xx_pcm_mmap, |
| 283 | #else |
| 284 | .copy = bf5xx_pcm_copy, |
| 285 | #endif |
| 286 | }; |
| 287 | |
| 288 | static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
| 289 | { |
| 290 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 291 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 292 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max |
| 293 | * sizeof(struct ac97_frame) / 4; |
| 294 | |
| 295 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 296 | buf->dev.dev = pcm->card->dev; |
| 297 | buf->private_data = NULL; |
| 298 | buf->area = dma_alloc_coherent(pcm->card->dev, size, |
| 299 | &buf->addr, GFP_KERNEL); |
| 300 | if (!buf->area) { |
| 301 | pr_err("Failed to allocate dma memory\n"); |
| 302 | pr_err("Please increase uncached DMA memory region\n"); |
| 303 | return -ENOMEM; |
| 304 | } |
| 305 | buf->bytes = size; |
| 306 | |
| 307 | pr_debug("%s, area:%p, size:0x%08lx\n", __func__, |
| 308 | buf->area, buf->bytes); |
| 309 | |
| 310 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 311 | sport_handle->tx_buf = buf->area; |
| 312 | else |
| 313 | sport_handle->rx_buf = buf->area; |
| 314 | |
| 315 | /* |
| 316 | * Need to allocate local buffer when enable |
| 317 | * MMAP for SPORT working in TMD mode (include AC97). |
| 318 | */ |
| 319 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 320 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 321 | if (!sport_handle->tx_dma_buf) { |
| 322 | sport_handle->tx_dma_buf = dma_alloc_coherent(NULL, \ |
| 323 | size, &sport_handle->tx_dma_phy, GFP_KERNEL); |
| 324 | if (!sport_handle->tx_dma_buf) { |
| 325 | pr_err("Failed to allocate memory for tx dma \ |
| 326 | buf - Please increase uncached DMA \ |
| 327 | memory region\n"); |
| 328 | return -ENOMEM; |
| 329 | } else |
| 330 | memset(sport_handle->tx_dma_buf, 0, size); |
| 331 | } else |
| 332 | memset(sport_handle->tx_dma_buf, 0, size); |
| 333 | } else { |
| 334 | if (!sport_handle->rx_dma_buf) { |
| 335 | sport_handle->rx_dma_buf = dma_alloc_coherent(NULL, \ |
| 336 | size, &sport_handle->rx_dma_phy, GFP_KERNEL); |
| 337 | if (!sport_handle->rx_dma_buf) { |
| 338 | pr_err("Failed to allocate memory for rx dma \ |
| 339 | buf - Please increase uncached DMA \ |
| 340 | memory region\n"); |
| 341 | return -ENOMEM; |
| 342 | } else |
| 343 | memset(sport_handle->rx_dma_buf, 0, size); |
| 344 | } else |
| 345 | memset(sport_handle->rx_dma_buf, 0, size); |
| 346 | } |
| 347 | #endif |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 352 | { |
| 353 | struct snd_pcm_substream *substream; |
| 354 | struct snd_dma_buffer *buf; |
| 355 | int stream; |
| 356 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 357 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max * |
| 358 | sizeof(struct ac97_frame) / 4; |
| 359 | #endif |
| 360 | for (stream = 0; stream < 2; stream++) { |
| 361 | substream = pcm->streams[stream].substream; |
| 362 | if (!substream) |
| 363 | continue; |
| 364 | |
| 365 | buf = &substream->dma_buffer; |
| 366 | if (!buf->area) |
| 367 | continue; |
| 368 | dma_free_coherent(NULL, buf->bytes, buf->area, 0); |
| 369 | buf->area = NULL; |
| 370 | #if defined(CONFIG_SND_MMAP_SUPPORT) |
| 371 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 372 | if (sport_handle->tx_dma_buf) |
| 373 | dma_free_coherent(NULL, size, \ |
| 374 | sport_handle->tx_dma_buf, 0); |
| 375 | sport_handle->tx_dma_buf = NULL; |
| 376 | } else { |
| 377 | |
| 378 | if (sport_handle->rx_dma_buf) |
| 379 | dma_free_coherent(NULL, size, \ |
| 380 | sport_handle->rx_dma_buf, 0); |
| 381 | sport_handle->rx_dma_buf = NULL; |
| 382 | } |
| 383 | #endif |
| 384 | } |
| 385 | if (sport_handle) |
| 386 | sport_done(sport_handle); |
| 387 | } |
| 388 | |
| 389 | static u64 bf5xx_pcm_dmamask = DMA_32BIT_MASK; |
| 390 | |
| 391 | int bf5xx_pcm_ac97_new(struct snd_card *card, struct snd_soc_dai *dai, |
| 392 | struct snd_pcm *pcm) |
| 393 | { |
| 394 | int ret = 0; |
| 395 | |
| 396 | pr_debug("%s enter\n", __func__); |
| 397 | if (!card->dev->dma_mask) |
| 398 | card->dev->dma_mask = &bf5xx_pcm_dmamask; |
| 399 | if (!card->dev->coherent_dma_mask) |
| 400 | card->dev->coherent_dma_mask = DMA_32BIT_MASK; |
| 401 | |
| 402 | if (dai->playback.channels_min) { |
| 403 | ret = bf5xx_pcm_preallocate_dma_buffer(pcm, |
| 404 | SNDRV_PCM_STREAM_PLAYBACK); |
| 405 | if (ret) |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | if (dai->capture.channels_min) { |
| 410 | ret = bf5xx_pcm_preallocate_dma_buffer(pcm, |
| 411 | SNDRV_PCM_STREAM_CAPTURE); |
| 412 | if (ret) |
| 413 | goto out; |
| 414 | } |
| 415 | out: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | struct snd_soc_platform bf5xx_ac97_soc_platform = { |
| 420 | .name = "bf5xx-audio", |
| 421 | .pcm_ops = &bf5xx_pcm_ac97_ops, |
| 422 | .pcm_new = bf5xx_pcm_ac97_new, |
| 423 | .pcm_free = bf5xx_pcm_free_dma_buffers, |
| 424 | }; |
| 425 | EXPORT_SYMBOL_GPL(bf5xx_ac97_soc_platform); |
| 426 | |
| 427 | MODULE_AUTHOR("Cliff Cai"); |
| 428 | MODULE_DESCRIPTION("ADI Blackfin AC97 PCM DMA module"); |
| 429 | MODULE_LICENSE("GPL"); |