1 /* 2 * imx-pcm-fiq.c -- ALSA Soc Audio Layer 3 * 4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de> 5 * 6 * This code is based on code copyrighted by Freescale, 7 * Liam Girdwood, Javier Martin and probably others. 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 */ 14 #include <linux/clk.h> 15 #include <linux/delay.h> 16 #include <linux/device.h> 17 #include <linux/dma-mapping.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/module.h> 21 #include <linux/platform_device.h> 22 #include <linux/slab.h> 23 24 #include <sound/core.h> 25 #include <sound/dmaengine_pcm.h> 26 #include <sound/initval.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/soc.h> 30 31 #include <asm/fiq.h> 32 33 #include <linux/platform_data/asoc-imx-ssi.h> 34 35 #include "imx-ssi.h" 36 #include "imx-pcm.h" 37 38 struct imx_pcm_runtime_data { 39 unsigned int period; 40 int periods; 41 unsigned long offset; 42 struct hrtimer hrt; 43 int poll_time_ns; 44 struct snd_pcm_substream *substream; 45 atomic_t playing; 46 atomic_t capturing; 47 }; 48 49 static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt) 50 { 51 struct imx_pcm_runtime_data *iprtd = 52 container_of(hrt, struct imx_pcm_runtime_data, hrt); 53 struct snd_pcm_substream *substream = iprtd->substream; 54 struct snd_pcm_runtime *runtime = substream->runtime; 55 struct pt_regs regs; 56 57 if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing)) 58 return HRTIMER_NORESTART; 59 60 get_fiq_regs(®s); 61 62 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 63 iprtd->offset = regs.ARM_r8 & 0xffff; 64 else 65 iprtd->offset = regs.ARM_r9 & 0xffff; 66 67 snd_pcm_period_elapsed(substream); 68 69 hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns)); 70 71 return HRTIMER_RESTART; 72 } 73 74 static struct fiq_handler fh = { 75 .name = DRV_NAME, 76 }; 77 78 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream, 79 struct snd_pcm_hw_params *params) 80 { 81 struct snd_pcm_runtime *runtime = substream->runtime; 82 struct imx_pcm_runtime_data *iprtd = runtime->private_data; 83 84 iprtd->periods = params_periods(params); 85 iprtd->period = params_period_bytes(params); 86 iprtd->offset = 0; 87 iprtd->poll_time_ns = 1000000000 / params_rate(params) * 88 params_period_size(params); 89 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 90 91 return 0; 92 } 93 94 static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream) 95 { 96 struct snd_pcm_runtime *runtime = substream->runtime; 97 struct imx_pcm_runtime_data *iprtd = runtime->private_data; 98 struct pt_regs regs; 99 100 get_fiq_regs(®s); 101 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 102 regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16; 103 else 104 regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16; 105 106 set_fiq_regs(®s); 107 108 return 0; 109 } 110 111 static int imx_pcm_fiq; 112 113 static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 114 { 115 struct snd_pcm_runtime *runtime = substream->runtime; 116 struct imx_pcm_runtime_data *iprtd = runtime->private_data; 117 118 switch (cmd) { 119 case SNDRV_PCM_TRIGGER_START: 120 case SNDRV_PCM_TRIGGER_RESUME: 121 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 122 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 123 atomic_set(&iprtd->playing, 1); 124 else 125 atomic_set(&iprtd->capturing, 1); 126 hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns), 127 HRTIMER_MODE_REL); 128 enable_fiq(imx_pcm_fiq); 129 break; 130 131 case SNDRV_PCM_TRIGGER_STOP: 132 case SNDRV_PCM_TRIGGER_SUSPEND: 133 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 134 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 135 atomic_set(&iprtd->playing, 0); 136 else 137 atomic_set(&iprtd->capturing, 0); 138 if (!atomic_read(&iprtd->playing) && 139 !atomic_read(&iprtd->capturing)) 140 disable_fiq(imx_pcm_fiq); 141 break; 142 143 default: 144 return -EINVAL; 145 } 146 147 return 0; 148 } 149 150 static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream) 151 { 152 struct snd_pcm_runtime *runtime = substream->runtime; 153 struct imx_pcm_runtime_data *iprtd = runtime->private_data; 154 155 return bytes_to_frames(substream->runtime, iprtd->offset); 156 } 157 158 static struct snd_pcm_hardware snd_imx_hardware = { 159 .info = SNDRV_PCM_INFO_INTERLEAVED | 160 SNDRV_PCM_INFO_BLOCK_TRANSFER | 161 SNDRV_PCM_INFO_MMAP | 162 SNDRV_PCM_INFO_MMAP_VALID | 163 SNDRV_PCM_INFO_PAUSE | 164 SNDRV_PCM_INFO_RESUME, 165 .formats = SNDRV_PCM_FMTBIT_S16_LE, 166 .rate_min = 8000, 167 .channels_min = 2, 168 .channels_max = 2, 169 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE, 170 .period_bytes_min = 128, 171 .period_bytes_max = 16 * 1024, 172 .periods_min = 4, 173 .periods_max = 255, 174 .fifo_size = 0, 175 }; 176 177 static int snd_imx_open(struct snd_pcm_substream *substream) 178 { 179 struct snd_pcm_runtime *runtime = substream->runtime; 180 struct imx_pcm_runtime_data *iprtd; 181 int ret; 182 183 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL); 184 if (iprtd == NULL) 185 return -ENOMEM; 186 runtime->private_data = iprtd; 187 188 iprtd->substream = substream; 189 190 atomic_set(&iprtd->playing, 0); 191 atomic_set(&iprtd->capturing, 0); 192 hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 193 iprtd->hrt.function = snd_hrtimer_callback; 194 195 ret = snd_pcm_hw_constraint_integer(substream->runtime, 196 SNDRV_PCM_HW_PARAM_PERIODS); 197 if (ret < 0) { 198 kfree(iprtd); 199 return ret; 200 } 201 202 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); 203 return 0; 204 } 205 206 static int snd_imx_close(struct snd_pcm_substream *substream) 207 { 208 struct snd_pcm_runtime *runtime = substream->runtime; 209 struct imx_pcm_runtime_data *iprtd = runtime->private_data; 210 211 hrtimer_cancel(&iprtd->hrt); 212 213 kfree(iprtd); 214 215 return 0; 216 } 217 218 static int snd_imx_pcm_mmap(struct snd_pcm_substream *substream, 219 struct vm_area_struct *vma) 220 { 221 struct snd_pcm_runtime *runtime = substream->runtime; 222 int ret; 223 224 ret = dma_mmap_writecombine(substream->pcm->card->dev, vma, 225 runtime->dma_area, runtime->dma_addr, runtime->dma_bytes); 226 227 pr_debug("%s: ret: %d %p 0x%08x 0x%08x\n", __func__, ret, 228 runtime->dma_area, 229 runtime->dma_addr, 230 runtime->dma_bytes); 231 return ret; 232 } 233 234 static struct snd_pcm_ops imx_pcm_ops = { 235 .open = snd_imx_open, 236 .close = snd_imx_close, 237 .ioctl = snd_pcm_lib_ioctl, 238 .hw_params = snd_imx_pcm_hw_params, 239 .prepare = snd_imx_pcm_prepare, 240 .trigger = snd_imx_pcm_trigger, 241 .pointer = snd_imx_pcm_pointer, 242 .mmap = snd_imx_pcm_mmap, 243 }; 244 245 static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) 246 { 247 struct snd_pcm_substream *substream = pcm->streams[stream].substream; 248 struct snd_dma_buffer *buf = &substream->dma_buffer; 249 size_t size = IMX_SSI_DMABUF_SIZE; 250 251 buf->dev.type = SNDRV_DMA_TYPE_DEV; 252 buf->dev.dev = pcm->card->dev; 253 buf->private_data = NULL; 254 buf->area = dma_alloc_writecombine(pcm->card->dev, size, 255 &buf->addr, GFP_KERNEL); 256 if (!buf->area) 257 return -ENOMEM; 258 buf->bytes = size; 259 260 return 0; 261 } 262 263 static u64 imx_pcm_dmamask = DMA_BIT_MASK(32); 264 265 static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd) 266 { 267 struct snd_card *card = rtd->card->snd_card; 268 struct snd_pcm *pcm = rtd->pcm; 269 int ret = 0; 270 271 if (!card->dev->dma_mask) 272 card->dev->dma_mask = &imx_pcm_dmamask; 273 if (!card->dev->coherent_dma_mask) 274 card->dev->coherent_dma_mask = DMA_BIT_MASK(32); 275 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { 276 ret = imx_pcm_preallocate_dma_buffer(pcm, 277 SNDRV_PCM_STREAM_PLAYBACK); 278 if (ret) 279 goto out; 280 } 281 282 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { 283 ret = imx_pcm_preallocate_dma_buffer(pcm, 284 SNDRV_PCM_STREAM_CAPTURE); 285 if (ret) 286 goto out; 287 } 288 289 out: 290 return ret; 291 } 292 293 static int ssi_irq = 0; 294 295 static int imx_pcm_fiq_new(struct snd_soc_pcm_runtime *rtd) 296 { 297 struct snd_pcm *pcm = rtd->pcm; 298 struct snd_pcm_substream *substream; 299 int ret; 300 301 ret = imx_pcm_new(rtd); 302 if (ret) 303 return ret; 304 305 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 306 if (substream) { 307 struct snd_dma_buffer *buf = &substream->dma_buffer; 308 309 imx_ssi_fiq_tx_buffer = (unsigned long)buf->area; 310 } 311 312 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 313 if (substream) { 314 struct snd_dma_buffer *buf = &substream->dma_buffer; 315 316 imx_ssi_fiq_rx_buffer = (unsigned long)buf->area; 317 } 318 319 set_fiq_handler(&imx_ssi_fiq_start, 320 &imx_ssi_fiq_end - &imx_ssi_fiq_start); 321 322 return 0; 323 } 324 325 static void imx_pcm_free(struct snd_pcm *pcm) 326 { 327 struct snd_pcm_substream *substream; 328 struct snd_dma_buffer *buf; 329 int stream; 330 331 for (stream = 0; stream < 2; stream++) { 332 substream = pcm->streams[stream].substream; 333 if (!substream) 334 continue; 335 336 buf = &substream->dma_buffer; 337 if (!buf->area) 338 continue; 339 340 dma_free_writecombine(pcm->card->dev, buf->bytes, 341 buf->area, buf->addr); 342 buf->area = NULL; 343 } 344 } 345 346 static void imx_pcm_fiq_free(struct snd_pcm *pcm) 347 { 348 mxc_set_irq_fiq(ssi_irq, 0); 349 release_fiq(&fh); 350 imx_pcm_free(pcm); 351 } 352 353 static struct snd_soc_platform_driver imx_soc_platform_fiq = { 354 .ops = &imx_pcm_ops, 355 .pcm_new = imx_pcm_fiq_new, 356 .pcm_free = imx_pcm_fiq_free, 357 }; 358 359 int imx_pcm_fiq_init(struct platform_device *pdev, 360 struct imx_pcm_fiq_params *params) 361 { 362 int ret; 363 364 ret = claim_fiq(&fh); 365 if (ret) { 366 dev_err(&pdev->dev, "failed to claim fiq: %d", ret); 367 return ret; 368 } 369 370 mxc_set_irq_fiq(params->irq, 1); 371 ssi_irq = params->irq; 372 373 imx_pcm_fiq = params->irq; 374 375 imx_ssi_fiq_base = (unsigned long)params->base; 376 377 params->dma_params_tx->maxburst = 4; 378 params->dma_params_rx->maxburst = 6; 379 380 ret = snd_soc_register_platform(&pdev->dev, &imx_soc_platform_fiq); 381 if (ret) 382 goto failed_register; 383 384 return 0; 385 386 failed_register: 387 mxc_set_irq_fiq(ssi_irq, 0); 388 release_fiq(&fh); 389 390 return ret; 391 } 392 EXPORT_SYMBOL_GPL(imx_pcm_fiq_init); 393 394 void imx_pcm_fiq_exit(struct platform_device *pdev) 395 { 396 snd_soc_unregister_platform(&pdev->dev); 397 } 398 EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit); 399 400 MODULE_LICENSE("GPL"); 401