Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: sound/soc/blackfin/bf5xx-i2s-pcm.c |
| 3 | * Author: Cliff Cai <Cliff.Cai@analog.com> |
| 4 | * |
| 5 | * Created: Tue June 06 2008 |
| 6 | * Description: DMA driver for i2s codec |
| 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> |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 32 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/gfp.h> |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 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-i2s-pcm.h" |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 43 | #include "bf5xx-sport.h" |
| 44 | |
| 45 | static void bf5xx_dma_irq(void *data) |
| 46 | { |
| 47 | struct snd_pcm_substream *pcm = data; |
| 48 | snd_pcm_period_elapsed(pcm); |
| 49 | } |
| 50 | |
| 51 | static const struct snd_pcm_hardware bf5xx_pcm_hardware = { |
| 52 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 53 | SNDRV_PCM_INFO_MMAP | |
| 54 | SNDRV_PCM_INFO_MMAP_VALID | |
| 55 | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 56 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 57 | SNDRV_PCM_FMTBIT_S24_LE | |
| 58 | SNDRV_PCM_FMTBIT_S32_LE, |
| 59 | .period_bytes_min = 32, |
| 60 | .period_bytes_max = 0x10000, |
| 61 | .periods_min = 1, |
| 62 | .periods_max = PAGE_SIZE/32, |
| 63 | .buffer_bytes_max = 0x20000, /* 128 kbytes */ |
| 64 | .fifo_size = 16, |
| 65 | }; |
| 66 | |
| 67 | static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 68 | struct snd_pcm_hw_params *params) |
| 69 | { |
| 70 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max; |
| 71 | snd_pcm_lib_malloc_pages(substream, size); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 77 | { |
| 78 | snd_pcm_lib_free_pages(substream); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream) |
| 84 | { |
| 85 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 86 | struct sport_device *sport = runtime->private_data; |
| 87 | int period_bytes = frames_to_bytes(runtime, runtime->period_size); |
| 88 | |
| 89 | pr_debug("%s enter\n", __func__); |
| 90 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 91 | sport_set_tx_callback(sport, bf5xx_dma_irq, substream); |
| 92 | sport_config_tx_dma(sport, runtime->dma_area, |
| 93 | runtime->periods, period_bytes); |
| 94 | } else { |
| 95 | sport_set_rx_callback(sport, bf5xx_dma_irq, substream); |
| 96 | sport_config_rx_dma(sport, runtime->dma_area, |
| 97 | runtime->periods, period_bytes); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 104 | { |
| 105 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 106 | struct sport_device *sport = runtime->private_data; |
| 107 | int ret = 0; |
| 108 | |
| 109 | pr_debug("%s enter\n", __func__); |
| 110 | switch (cmd) { |
| 111 | case SNDRV_PCM_TRIGGER_START: |
| 112 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 113 | sport_tx_start(sport); |
| 114 | else |
| 115 | sport_rx_start(sport); |
| 116 | break; |
| 117 | case SNDRV_PCM_TRIGGER_STOP: |
| 118 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 119 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 120 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 121 | sport_tx_stop(sport); |
| 122 | else |
| 123 | sport_rx_stop(sport); |
| 124 | break; |
| 125 | default: |
| 126 | ret = -EINVAL; |
| 127 | } |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream) |
| 133 | { |
| 134 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 135 | struct sport_device *sport = runtime->private_data; |
| 136 | unsigned int diff; |
| 137 | snd_pcm_uframes_t frames; |
| 138 | pr_debug("%s enter\n", __func__); |
| 139 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 140 | diff = sport_curr_offset_tx(sport); |
| 141 | frames = bytes_to_frames(substream->runtime, diff); |
| 142 | } else { |
| 143 | diff = sport_curr_offset_rx(sport); |
| 144 | frames = bytes_to_frames(substream->runtime, diff); |
| 145 | } |
| 146 | return frames; |
| 147 | } |
| 148 | |
| 149 | static int bf5xx_pcm_open(struct snd_pcm_substream *substream) |
| 150 | { |
Barry Song | 2c66cb9 | 2011-03-28 01:45:10 -0400 | [diff] [blame] | 151 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 152 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 153 | struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai); |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 154 | struct snd_pcm_runtime *runtime = substream->runtime; |
Barry Song | 2c66cb9 | 2011-03-28 01:45:10 -0400 | [diff] [blame] | 155 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 156 | int ret; |
| 157 | |
| 158 | pr_debug("%s enter\n", __func__); |
Barry Song | 2c66cb9 | 2011-03-28 01:45:10 -0400 | [diff] [blame] | 159 | |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 160 | snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware); |
| 161 | |
| 162 | ret = snd_pcm_hw_constraint_integer(runtime, \ |
| 163 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 164 | if (ret < 0) |
| 165 | goto out; |
| 166 | |
Barry Song | 2c66cb9 | 2011-03-28 01:45:10 -0400 | [diff] [blame] | 167 | if (sport_handle != NULL) { |
| 168 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 169 | sport_handle->tx_buf = buf->area; |
| 170 | else |
| 171 | sport_handle->rx_buf = buf->area; |
| 172 | |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 173 | runtime->private_data = sport_handle; |
Barry Song | 2c66cb9 | 2011-03-28 01:45:10 -0400 | [diff] [blame] | 174 | } else { |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 175 | pr_err("sport_handle is NULL\n"); |
| 176 | return -1; |
| 177 | } |
| 178 | return 0; |
| 179 | |
| 180 | out: |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream, |
| 185 | struct vm_area_struct *vma) |
| 186 | { |
| 187 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 188 | size_t size = vma->vm_end - vma->vm_start; |
| 189 | vma->vm_start = (unsigned long)runtime->dma_area; |
| 190 | vma->vm_end = vma->vm_start + size; |
| 191 | vma->vm_flags |= VM_SHARED; |
| 192 | |
| 193 | return 0 ; |
| 194 | } |
| 195 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 196 | static struct snd_pcm_ops bf5xx_pcm_i2s_ops = { |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 197 | .open = bf5xx_pcm_open, |
| 198 | .ioctl = snd_pcm_lib_ioctl, |
| 199 | .hw_params = bf5xx_pcm_hw_params, |
| 200 | .hw_free = bf5xx_pcm_hw_free, |
| 201 | .prepare = bf5xx_pcm_prepare, |
| 202 | .trigger = bf5xx_pcm_trigger, |
| 203 | .pointer = bf5xx_pcm_pointer, |
| 204 | .mmap = bf5xx_pcm_mmap, |
| 205 | }; |
| 206 | |
| 207 | static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
| 208 | { |
| 209 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 210 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 211 | size_t size = bf5xx_pcm_hardware.buffer_bytes_max; |
| 212 | |
| 213 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 214 | buf->dev.dev = pcm->card->dev; |
| 215 | buf->private_data = NULL; |
| 216 | buf->area = dma_alloc_coherent(pcm->card->dev, size, |
| 217 | &buf->addr, GFP_KERNEL); |
| 218 | if (!buf->area) { |
Joe Perches | 2f1ff66 | 2010-01-31 12:02:12 -0800 | [diff] [blame] | 219 | pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n"); |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 220 | return -ENOMEM; |
| 221 | } |
| 222 | buf->bytes = size; |
| 223 | |
| 224 | pr_debug("%s, area:%p, size:0x%08lx\n", __func__, |
| 225 | buf->area, buf->bytes); |
| 226 | |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 231 | { |
| 232 | struct snd_pcm_substream *substream; |
| 233 | struct snd_dma_buffer *buf; |
| 234 | int stream; |
| 235 | |
| 236 | for (stream = 0; stream < 2; stream++) { |
| 237 | substream = pcm->streams[stream].substream; |
| 238 | if (!substream) |
| 239 | continue; |
| 240 | |
| 241 | buf = &substream->dma_buffer; |
| 242 | if (!buf->area) |
| 243 | continue; |
| 244 | dma_free_coherent(NULL, buf->bytes, buf->area, 0); |
| 245 | buf->area = NULL; |
| 246 | } |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 249 | static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32); |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 250 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame^] | 251 | int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime *rtd) |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 252 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame^] | 253 | struct snd_card *card = rtd->card->snd_card; |
| 254 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 255 | struct snd_pcm *pcm = rtd->pcm; |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 256 | int ret = 0; |
| 257 | |
| 258 | pr_debug("%s enter\n", __func__); |
| 259 | if (!card->dev->dma_mask) |
| 260 | card->dev->dma_mask = &bf5xx_pcm_dmamask; |
| 261 | if (!card->dev->coherent_dma_mask) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 262 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 263 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 264 | if (dai->driver->playback.channels_min) { |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 265 | ret = bf5xx_pcm_preallocate_dma_buffer(pcm, |
| 266 | SNDRV_PCM_STREAM_PLAYBACK); |
| 267 | if (ret) |
| 268 | goto out; |
| 269 | } |
| 270 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 271 | if (dai->driver->capture.channels_min) { |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 272 | ret = bf5xx_pcm_preallocate_dma_buffer(pcm, |
| 273 | SNDRV_PCM_STREAM_CAPTURE); |
| 274 | if (ret) |
| 275 | goto out; |
| 276 | } |
| 277 | out: |
| 278 | return ret; |
| 279 | } |
| 280 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 281 | static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = { |
| 282 | .ops = &bf5xx_pcm_i2s_ops, |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 283 | .pcm_new = bf5xx_pcm_i2s_new, |
| 284 | .pcm_free = bf5xx_pcm_free_dma_buffers, |
| 285 | }; |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 286 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 287 | static int __devinit bfin_i2s_soc_platform_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 288 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 289 | return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 290 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 291 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 292 | static int __devexit bfin_i2s_soc_platform_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 293 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 294 | snd_soc_unregister_platform(&pdev->dev); |
| 295 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 296 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 297 | |
| 298 | static struct platform_driver bfin_i2s_pcm_driver = { |
| 299 | .driver = { |
Mike Frysinger | bfe4ee0 | 2011-03-28 01:45:09 -0400 | [diff] [blame] | 300 | .name = "bfin-i2s-pcm-audio", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 301 | .owner = THIS_MODULE, |
| 302 | }, |
| 303 | |
| 304 | .probe = bfin_i2s_soc_platform_probe, |
| 305 | .remove = __devexit_p(bfin_i2s_soc_platform_remove), |
| 306 | }; |
| 307 | |
| 308 | static int __init snd_bfin_i2s_pcm_init(void) |
| 309 | { |
| 310 | return platform_driver_register(&bfin_i2s_pcm_driver); |
| 311 | } |
| 312 | module_init(snd_bfin_i2s_pcm_init); |
| 313 | |
| 314 | static void __exit snd_bfin_i2s_pcm_exit(void) |
| 315 | { |
| 316 | platform_driver_unregister(&bfin_i2s_pcm_driver); |
| 317 | } |
| 318 | module_exit(snd_bfin_i2s_pcm_exit); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 319 | |
Cliff Cai | 88740da | 2008-09-05 18:21:38 +0800 | [diff] [blame] | 320 | MODULE_AUTHOR("Cliff Cai"); |
| 321 | MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module"); |
| 322 | MODULE_LICENSE("GPL"); |