1/* 2 * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip 3 * 4 * Author: Nicolas Pitre 5 * Created: Nov 30, 2004 6 * Copyright: (C) 2004 MontaVista Software, Inc. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13#include <linux/module.h> 14#include <linux/init.h> 15#include <linux/platform_device.h> 16#include <linux/slab.h> 17#include <linux/dma-mapping.h> 18 19#include <sound/driver.h> 20#include <sound/core.h> 21#include <sound/pcm.h> 22#include <sound/pcm_params.h> 23#include <sound/soc.h> 24 25#include <asm/dma.h> 26#include <asm/hardware.h> 27#include <asm/arch/pxa-regs.h> 28#include <asm/arch/audio.h> 29 30#include "pxa2xx-pcm.h" 31 32static const struct snd_pcm_hardware pxa2xx_pcm_hardware = { 33 .info = SNDRV_PCM_INFO_MMAP | 34 SNDRV_PCM_INFO_MMAP_VALID | 35 SNDRV_PCM_INFO_INTERLEAVED | 36 SNDRV_PCM_INFO_PAUSE | 37 SNDRV_PCM_INFO_RESUME, 38 .formats = SNDRV_PCM_FMTBIT_S16_LE | 39 SNDRV_PCM_FMTBIT_S24_LE | 40 SNDRV_PCM_FMTBIT_S32_LE, 41 .period_bytes_min = 32, 42 .period_bytes_max = 8192 - 32, 43 .periods_min = 1, 44 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc), 45 .buffer_bytes_max = 128 * 1024, 46 .fifo_size = 32, 47}; 48 49struct pxa2xx_runtime_data { 50 int dma_ch; 51 struct pxa2xx_pcm_dma_params *params; 52 pxa_dma_desc *dma_desc_array; 53 dma_addr_t dma_desc_array_phys; 54}; 55 56static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id) 57{ 58 struct snd_pcm_substream *substream = dev_id; 59 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; 60 int dcsr; 61 62 dcsr = DCSR(dma_ch); 63 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN; 64 65 if (dcsr & DCSR_ENDINTR) { 66 snd_pcm_period_elapsed(substream); 67 } else { 68 printk( KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n", 69 prtd->params->name, dma_ch, dcsr ); 70 } 71} 72 73static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream, 74 struct snd_pcm_hw_params *params) 75{ 76 struct snd_pcm_runtime *runtime = substream->runtime; 77 struct pxa2xx_runtime_data *prtd = runtime->private_data; 78 struct snd_soc_pcm_runtime *rtd = substream->private_data; 79 struct pxa2xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data; 80 size_t totsize = params_buffer_bytes(params); 81 size_t period = params_period_bytes(params); 82 pxa_dma_desc *dma_desc; 83 dma_addr_t dma_buff_phys, next_desc_phys; 84 int ret; 85 86 if (!dma) 87 return 0; 88 89 /* this may get called several times by oss emulation 90 * with different params */ 91 if (prtd->params == NULL) { 92 prtd->params = dma; 93 ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW, 94 pxa2xx_pcm_dma_irq, substream); 95 if (ret < 0) 96 return ret; 97 prtd->dma_ch = ret; 98 } else if (prtd->params != dma) { 99 pxa_free_dma(prtd->dma_ch); 100 prtd->params = dma; 101 ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW, 102 pxa2xx_pcm_dma_irq, substream); 103 if (ret < 0) 104 return ret; 105 prtd->dma_ch = ret; 106 } 107 108 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 109 runtime->dma_bytes = totsize; 110 111 dma_desc = prtd->dma_desc_array; 112 next_desc_phys = prtd->dma_desc_array_phys; 113 dma_buff_phys = runtime->dma_addr; 114 do { 115 next_desc_phys += sizeof(pxa_dma_desc); 116 dma_desc->ddadr = next_desc_phys; 117 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 118 dma_desc->dsadr = dma_buff_phys; 119 dma_desc->dtadr = prtd->params->dev_addr; 120 } else { 121 dma_desc->dsadr = prtd->params->dev_addr; 122 dma_desc->dtadr = dma_buff_phys; 123 } 124 if (period > totsize) 125 period = totsize; 126 dma_desc->dcmd = prtd->params->dcmd | period | DCMD_ENDIRQEN; 127 dma_desc++; 128 dma_buff_phys += period; 129 } while (totsize -= period); 130 dma_desc[-1].ddadr = prtd->dma_desc_array_phys; 131 132 return 0; 133} 134 135static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream) 136{ 137 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; 138 139 if (prtd && prtd->params) 140 *prtd->params->drcmr = 0; 141 142 if (prtd->dma_ch) { 143 snd_pcm_set_runtime_buffer(substream, NULL); 144 pxa_free_dma(prtd->dma_ch); 145 prtd->dma_ch = 0; 146 } 147 148 return 0; 149} 150 151static int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream) 152{ 153 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; 154 155 DCSR(prtd->dma_ch) &= ~DCSR_RUN; 156 DCSR(prtd->dma_ch) = 0; 157 DCMD(prtd->dma_ch) = 0; 158 *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD; 159 160 return 0; 161} 162 163static int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 164{ 165 struct pxa2xx_runtime_data *prtd = substream->runtime->private_data; 166 int ret = 0; 167 168 switch (cmd) { 169 case SNDRV_PCM_TRIGGER_START: 170 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys; 171 DCSR(prtd->dma_ch) = DCSR_RUN; 172 break; 173 174 case SNDRV_PCM_TRIGGER_STOP: 175 case SNDRV_PCM_TRIGGER_SUSPEND: 176 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 177 DCSR(prtd->dma_ch) &= ~DCSR_RUN; 178 break; 179 180 case SNDRV_PCM_TRIGGER_RESUME: 181 DCSR(prtd->dma_ch) |= DCSR_RUN; 182 break; 183 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 184 DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys; 185 DCSR(prtd->dma_ch) |= DCSR_RUN; 186 break; 187 188 default: 189 ret = -EINVAL; 190 } 191 192 return ret; 193} 194 195static snd_pcm_uframes_t 196pxa2xx_pcm_pointer(struct snd_pcm_substream *substream) 197{ 198 struct snd_pcm_runtime *runtime = substream->runtime; 199 struct pxa2xx_runtime_data *prtd = runtime->private_data; 200 201 dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 202 DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch); 203 snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr); 204 205 if (x == runtime->buffer_size) 206 x = 0; 207 return x; 208} 209 210static int pxa2xx_pcm_open(struct snd_pcm_substream *substream) 211{ 212 struct snd_pcm_runtime *runtime = substream->runtime; 213 struct pxa2xx_runtime_data *prtd; 214 int ret; 215 216 snd_soc_set_runtime_hwparams(substream, &pxa2xx_pcm_hardware); 217 218 /* 219 * For mysterious reasons (and despite what the manual says) 220 * playback samples are lost if the DMA count is not a multiple 221 * of the DMA burst size. Let's add a rule to enforce that. 222 */ 223 ret = snd_pcm_hw_constraint_step(runtime, 0, 224 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); 225 if (ret) 226 goto out; 227 228 ret = snd_pcm_hw_constraint_step(runtime, 0, 229 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); 230 if (ret) 231 goto out; 232 233 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 234 if (ret < 0) 235 goto out; 236 237 prtd = kzalloc(sizeof(struct pxa2xx_runtime_data), GFP_KERNEL); 238 if (prtd == NULL) { 239 ret = -ENOMEM; 240 goto out; 241 } 242 243 prtd->dma_desc_array = 244 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE, 245 &prtd->dma_desc_array_phys, GFP_KERNEL); 246 if (!prtd->dma_desc_array) { 247 ret = -ENOMEM; 248 goto err1; 249 } 250 251 runtime->private_data = prtd; 252 return 0; 253 254 err1: 255 kfree(prtd); 256 out: 257 return ret; 258} 259 260static int pxa2xx_pcm_close(struct snd_pcm_substream *substream) 261{ 262 struct snd_pcm_runtime *runtime = substream->runtime; 263 struct pxa2xx_runtime_data *prtd = runtime->private_data; 264 265 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE, 266 prtd->dma_desc_array, prtd->dma_desc_array_phys); 267 kfree(prtd); 268 return 0; 269} 270 271static int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream, 272 struct vm_area_struct *vma) 273{ 274 struct snd_pcm_runtime *runtime = substream->runtime; 275 return dma_mmap_writecombine(substream->pcm->card->dev, vma, 276 runtime->dma_area, 277 runtime->dma_addr, 278 runtime->dma_bytes); 279} 280 281struct snd_pcm_ops pxa2xx_pcm_ops = { 282 .open = pxa2xx_pcm_open, 283 .close = pxa2xx_pcm_close, 284 .ioctl = snd_pcm_lib_ioctl, 285 .hw_params = pxa2xx_pcm_hw_params, 286 .hw_free = pxa2xx_pcm_hw_free, 287 .prepare = pxa2xx_pcm_prepare, 288 .trigger = pxa2xx_pcm_trigger, 289 .pointer = pxa2xx_pcm_pointer, 290 .mmap = pxa2xx_pcm_mmap, 291}; 292 293static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) 294{ 295 struct snd_pcm_substream *substream = pcm->streams[stream].substream; 296 struct snd_dma_buffer *buf = &substream->dma_buffer; 297 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max; 298 buf->dev.type = SNDRV_DMA_TYPE_DEV; 299 buf->dev.dev = pcm->card->dev; 300 buf->private_data = NULL; 301 buf->area = dma_alloc_writecombine(pcm->card->dev, size, 302 &buf->addr, GFP_KERNEL); 303 if (!buf->area) 304 return -ENOMEM; 305 buf->bytes = size; 306 return 0; 307} 308 309static void pxa2xx_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 330static u64 pxa2xx_pcm_dmamask = DMA_32BIT_MASK; 331 332int pxa2xx_pcm_new(struct snd_card *card, struct snd_soc_codec_dai *dai, 333 struct snd_pcm *pcm) 334{ 335 int ret = 0; 336 337 if (!card->dev->dma_mask) 338 card->dev->dma_mask = &pxa2xx_pcm_dmamask; 339 if (!card->dev->coherent_dma_mask) 340 card->dev->coherent_dma_mask = DMA_32BIT_MASK; 341 342 if (dai->playback.channels_min) { 343 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, 344 SNDRV_PCM_STREAM_PLAYBACK); 345 if (ret) 346 goto out; 347 } 348 349 if (dai->capture.channels_min) { 350 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, 351 SNDRV_PCM_STREAM_CAPTURE); 352 if (ret) 353 goto out; 354 } 355 out: 356 return ret; 357} 358 359struct snd_soc_platform pxa2xx_soc_platform = { 360 .name = "pxa2xx-audio", 361 .pcm_ops = &pxa2xx_pcm_ops, 362 .pcm_new = pxa2xx_pcm_new, 363 .pcm_free = pxa2xx_pcm_free_dma_buffers, 364}; 365 366EXPORT_SYMBOL_GPL(pxa2xx_soc_platform); 367 368MODULE_AUTHOR("Nicolas Pitre"); 369MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module"); 370MODULE_LICENSE("GPL"); 371