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