1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Freescale ASRC ALSA SoC Platform (DMA) driver 4 // 5 // Copyright (C) 2014 Freescale Semiconductor, Inc. 6 // 7 // Author: Nicolin Chen <nicoleotsuka@gmail.com> 8 9 #include <linux/dma-mapping.h> 10 #include <linux/module.h> 11 #include <linux/platform_data/dma-imx.h> 12 #include <sound/dmaengine_pcm.h> 13 #include <sound/pcm_params.h> 14 15 #include "fsl_asrc.h" 16 17 #define FSL_ASRC_DMABUF_SIZE (256 * 1024) 18 19 static struct snd_pcm_hardware snd_imx_hardware = { 20 .info = SNDRV_PCM_INFO_INTERLEAVED | 21 SNDRV_PCM_INFO_BLOCK_TRANSFER | 22 SNDRV_PCM_INFO_MMAP | 23 SNDRV_PCM_INFO_MMAP_VALID, 24 .buffer_bytes_max = FSL_ASRC_DMABUF_SIZE, 25 .period_bytes_min = 128, 26 .period_bytes_max = 65535, /* Limited by SDMA engine */ 27 .periods_min = 2, 28 .periods_max = 255, 29 .fifo_size = 0, 30 }; 31 32 static bool filter(struct dma_chan *chan, void *param) 33 { 34 if (!imx_dma_is_general_purpose(chan)) 35 return false; 36 37 chan->private = param; 38 39 return true; 40 } 41 42 static void fsl_asrc_dma_complete(void *arg) 43 { 44 struct snd_pcm_substream *substream = arg; 45 struct snd_pcm_runtime *runtime = substream->runtime; 46 struct fsl_asrc_pair *pair = runtime->private_data; 47 48 pair->pos += snd_pcm_lib_period_bytes(substream); 49 if (pair->pos >= snd_pcm_lib_buffer_bytes(substream)) 50 pair->pos = 0; 51 52 snd_pcm_period_elapsed(substream); 53 } 54 55 static int fsl_asrc_dma_prepare_and_submit(struct snd_pcm_substream *substream) 56 { 57 u8 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? OUT : IN; 58 struct snd_soc_pcm_runtime *rtd = substream->private_data; 59 struct snd_pcm_runtime *runtime = substream->runtime; 60 struct fsl_asrc_pair *pair = runtime->private_data; 61 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 62 struct device *dev = component->dev; 63 unsigned long flags = DMA_CTRL_ACK; 64 65 /* Prepare and submit Front-End DMA channel */ 66 if (!substream->runtime->no_period_wakeup) 67 flags |= DMA_PREP_INTERRUPT; 68 69 pair->pos = 0; 70 pair->desc[!dir] = dmaengine_prep_dma_cyclic( 71 pair->dma_chan[!dir], runtime->dma_addr, 72 snd_pcm_lib_buffer_bytes(substream), 73 snd_pcm_lib_period_bytes(substream), 74 dir == OUT ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, flags); 75 if (!pair->desc[!dir]) { 76 dev_err(dev, "failed to prepare slave DMA for Front-End\n"); 77 return -ENOMEM; 78 } 79 80 pair->desc[!dir]->callback = fsl_asrc_dma_complete; 81 pair->desc[!dir]->callback_param = substream; 82 83 dmaengine_submit(pair->desc[!dir]); 84 85 /* Prepare and submit Back-End DMA channel */ 86 pair->desc[dir] = dmaengine_prep_dma_cyclic( 87 pair->dma_chan[dir], 0xffff, 64, 64, DMA_DEV_TO_DEV, 0); 88 if (!pair->desc[dir]) { 89 dev_err(dev, "failed to prepare slave DMA for Back-End\n"); 90 return -ENOMEM; 91 } 92 93 dmaengine_submit(pair->desc[dir]); 94 95 return 0; 96 } 97 98 static int fsl_asrc_dma_trigger(struct snd_pcm_substream *substream, int cmd) 99 { 100 struct snd_pcm_runtime *runtime = substream->runtime; 101 struct fsl_asrc_pair *pair = runtime->private_data; 102 int ret; 103 104 switch (cmd) { 105 case SNDRV_PCM_TRIGGER_START: 106 case SNDRV_PCM_TRIGGER_RESUME: 107 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 108 ret = fsl_asrc_dma_prepare_and_submit(substream); 109 if (ret) 110 return ret; 111 dma_async_issue_pending(pair->dma_chan[IN]); 112 dma_async_issue_pending(pair->dma_chan[OUT]); 113 break; 114 case SNDRV_PCM_TRIGGER_STOP: 115 case SNDRV_PCM_TRIGGER_SUSPEND: 116 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 117 dmaengine_terminate_all(pair->dma_chan[OUT]); 118 dmaengine_terminate_all(pair->dma_chan[IN]); 119 break; 120 default: 121 return -EINVAL; 122 } 123 124 return 0; 125 } 126 127 static int fsl_asrc_dma_hw_params(struct snd_pcm_substream *substream, 128 struct snd_pcm_hw_params *params) 129 { 130 enum dma_slave_buswidth buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; 131 struct snd_soc_pcm_runtime *rtd = substream->private_data; 132 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 133 struct snd_dmaengine_dai_dma_data *dma_params_fe = NULL; 134 struct snd_dmaengine_dai_dma_data *dma_params_be = NULL; 135 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 136 struct snd_pcm_runtime *runtime = substream->runtime; 137 struct fsl_asrc_pair *pair = runtime->private_data; 138 struct fsl_asrc *asrc_priv = pair->asrc_priv; 139 struct dma_slave_config config_fe, config_be; 140 enum asrc_pair_index index = pair->index; 141 struct device *dev = component->dev; 142 int stream = substream->stream; 143 struct imx_dma_data *tmp_data; 144 struct snd_soc_dpcm *dpcm; 145 struct dma_chan *tmp_chan; 146 struct device *dev_be; 147 u8 dir = tx ? OUT : IN; 148 dma_cap_mask_t mask; 149 int ret; 150 151 /* Fetch the Back-End dma_data from DPCM */ 152 for_each_dpcm_be(rtd, stream, dpcm) { 153 struct snd_soc_pcm_runtime *be = dpcm->be; 154 struct snd_pcm_substream *substream_be; 155 struct snd_soc_dai *dai = be->cpu_dai; 156 157 if (dpcm->fe != rtd) 158 continue; 159 160 substream_be = snd_soc_dpcm_get_substream(be, stream); 161 dma_params_be = snd_soc_dai_get_dma_data(dai, substream_be); 162 dev_be = dai->dev; 163 break; 164 } 165 166 if (!dma_params_be) { 167 dev_err(dev, "failed to get the substream of Back-End\n"); 168 return -EINVAL; 169 } 170 171 /* Override dma_data of the Front-End and config its dmaengine */ 172 dma_params_fe = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 173 dma_params_fe->addr = asrc_priv->paddr + REG_ASRDx(!dir, index); 174 dma_params_fe->maxburst = dma_params_be->maxburst; 175 176 pair->dma_chan[!dir] = fsl_asrc_get_dma_channel(pair, !dir); 177 if (!pair->dma_chan[!dir]) { 178 dev_err(dev, "failed to request DMA channel\n"); 179 return -EINVAL; 180 } 181 182 memset(&config_fe, 0, sizeof(config_fe)); 183 ret = snd_dmaengine_pcm_prepare_slave_config(substream, params, &config_fe); 184 if (ret) { 185 dev_err(dev, "failed to prepare DMA config for Front-End\n"); 186 return ret; 187 } 188 189 ret = dmaengine_slave_config(pair->dma_chan[!dir], &config_fe); 190 if (ret) { 191 dev_err(dev, "failed to config DMA channel for Front-End\n"); 192 return ret; 193 } 194 195 /* Request and config DMA channel for Back-End */ 196 dma_cap_zero(mask); 197 dma_cap_set(DMA_SLAVE, mask); 198 dma_cap_set(DMA_CYCLIC, mask); 199 200 /* Get DMA request of Back-End */ 201 tmp_chan = dma_request_slave_channel(dev_be, tx ? "tx" : "rx"); 202 tmp_data = tmp_chan->private; 203 pair->dma_data.dma_request = tmp_data->dma_request; 204 dma_release_channel(tmp_chan); 205 206 /* Get DMA request of Front-End */ 207 tmp_chan = fsl_asrc_get_dma_channel(pair, dir); 208 tmp_data = tmp_chan->private; 209 pair->dma_data.dma_request2 = tmp_data->dma_request; 210 pair->dma_data.peripheral_type = tmp_data->peripheral_type; 211 pair->dma_data.priority = tmp_data->priority; 212 dma_release_channel(tmp_chan); 213 214 pair->dma_chan[dir] = dma_request_channel(mask, filter, &pair->dma_data); 215 if (!pair->dma_chan[dir]) { 216 dev_err(dev, "failed to request DMA channel for Back-End\n"); 217 return -EINVAL; 218 } 219 220 if (asrc_priv->asrc_width == 16) 221 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; 222 else 223 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; 224 225 config_be.direction = DMA_DEV_TO_DEV; 226 config_be.src_addr_width = buswidth; 227 config_be.src_maxburst = dma_params_be->maxburst; 228 config_be.dst_addr_width = buswidth; 229 config_be.dst_maxburst = dma_params_be->maxburst; 230 231 if (tx) { 232 config_be.src_addr = asrc_priv->paddr + REG_ASRDO(index); 233 config_be.dst_addr = dma_params_be->addr; 234 } else { 235 config_be.dst_addr = asrc_priv->paddr + REG_ASRDI(index); 236 config_be.src_addr = dma_params_be->addr; 237 } 238 239 ret = dmaengine_slave_config(pair->dma_chan[dir], &config_be); 240 if (ret) { 241 dev_err(dev, "failed to config DMA channel for Back-End\n"); 242 return ret; 243 } 244 245 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 246 247 return 0; 248 } 249 250 static int fsl_asrc_dma_hw_free(struct snd_pcm_substream *substream) 251 { 252 struct snd_pcm_runtime *runtime = substream->runtime; 253 struct fsl_asrc_pair *pair = runtime->private_data; 254 255 snd_pcm_set_runtime_buffer(substream, NULL); 256 257 if (pair->dma_chan[IN]) 258 dma_release_channel(pair->dma_chan[IN]); 259 260 if (pair->dma_chan[OUT]) 261 dma_release_channel(pair->dma_chan[OUT]); 262 263 pair->dma_chan[IN] = NULL; 264 pair->dma_chan[OUT] = NULL; 265 266 return 0; 267 } 268 269 static int fsl_asrc_dma_startup(struct snd_pcm_substream *substream) 270 { 271 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 272 struct snd_soc_pcm_runtime *rtd = substream->private_data; 273 struct snd_pcm_runtime *runtime = substream->runtime; 274 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 275 struct snd_dmaengine_dai_dma_data *dma_data; 276 struct device *dev = component->dev; 277 struct fsl_asrc *asrc_priv = dev_get_drvdata(dev); 278 struct fsl_asrc_pair *pair; 279 struct dma_chan *tmp_chan = NULL; 280 u8 dir = tx ? OUT : IN; 281 bool release_pair = true; 282 int ret = 0; 283 284 ret = snd_pcm_hw_constraint_integer(substream->runtime, 285 SNDRV_PCM_HW_PARAM_PERIODS); 286 if (ret < 0) { 287 dev_err(dev, "failed to set pcm hw params periods\n"); 288 return ret; 289 } 290 291 pair = kzalloc(sizeof(struct fsl_asrc_pair), GFP_KERNEL); 292 if (!pair) 293 return -ENOMEM; 294 295 pair->asrc_priv = asrc_priv; 296 297 runtime->private_data = pair; 298 299 /* Request a dummy pair, which will be released later. 300 * Request pair function needs channel num as input, for this 301 * dummy pair, we just request "1" channel temporarily. 302 */ 303 ret = fsl_asrc_request_pair(1, pair); 304 if (ret < 0) { 305 dev_err(dev, "failed to request asrc pair\n"); 306 goto req_pair_err; 307 } 308 309 /* Request a dummy dma channel, which will be released later. */ 310 tmp_chan = fsl_asrc_get_dma_channel(pair, dir); 311 if (!tmp_chan) { 312 dev_err(dev, "failed to get dma channel\n"); 313 ret = -EINVAL; 314 goto dma_chan_err; 315 } 316 317 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 318 319 /* Refine the snd_imx_hardware according to caps of DMA. */ 320 ret = snd_dmaengine_pcm_refine_runtime_hwparams(substream, 321 dma_data, 322 &snd_imx_hardware, 323 tmp_chan); 324 if (ret < 0) { 325 dev_err(dev, "failed to refine runtime hwparams\n"); 326 goto out; 327 } 328 329 release_pair = false; 330 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); 331 332 out: 333 dma_release_channel(tmp_chan); 334 335 dma_chan_err: 336 fsl_asrc_release_pair(pair); 337 338 req_pair_err: 339 if (release_pair) 340 kfree(pair); 341 342 return ret; 343 } 344 345 static int fsl_asrc_dma_shutdown(struct snd_pcm_substream *substream) 346 { 347 struct snd_pcm_runtime *runtime = substream->runtime; 348 struct fsl_asrc_pair *pair = runtime->private_data; 349 struct fsl_asrc *asrc_priv; 350 351 if (!pair) 352 return 0; 353 354 asrc_priv = pair->asrc_priv; 355 356 if (asrc_priv->pair[pair->index] == pair) 357 asrc_priv->pair[pair->index] = NULL; 358 359 kfree(pair); 360 361 return 0; 362 } 363 364 static snd_pcm_uframes_t fsl_asrc_dma_pcm_pointer(struct snd_pcm_substream *substream) 365 { 366 struct snd_pcm_runtime *runtime = substream->runtime; 367 struct fsl_asrc_pair *pair = runtime->private_data; 368 369 return bytes_to_frames(substream->runtime, pair->pos); 370 } 371 372 static const struct snd_pcm_ops fsl_asrc_dma_pcm_ops = { 373 .ioctl = snd_pcm_lib_ioctl, 374 .hw_params = fsl_asrc_dma_hw_params, 375 .hw_free = fsl_asrc_dma_hw_free, 376 .trigger = fsl_asrc_dma_trigger, 377 .open = fsl_asrc_dma_startup, 378 .close = fsl_asrc_dma_shutdown, 379 .pointer = fsl_asrc_dma_pcm_pointer, 380 }; 381 382 static int fsl_asrc_dma_pcm_new(struct snd_soc_pcm_runtime *rtd) 383 { 384 struct snd_card *card = rtd->card->snd_card; 385 struct snd_pcm_substream *substream; 386 struct snd_pcm *pcm = rtd->pcm; 387 int ret, i; 388 389 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); 390 if (ret) { 391 dev_err(card->dev, "failed to set DMA mask\n"); 392 return ret; 393 } 394 395 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) { 396 substream = pcm->streams[i].substream; 397 if (!substream) 398 continue; 399 400 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev, 401 FSL_ASRC_DMABUF_SIZE, &substream->dma_buffer); 402 if (ret) { 403 dev_err(card->dev, "failed to allocate DMA buffer\n"); 404 goto err; 405 } 406 } 407 408 return 0; 409 410 err: 411 if (--i == 0 && pcm->streams[i].substream) 412 snd_dma_free_pages(&pcm->streams[i].substream->dma_buffer); 413 414 return ret; 415 } 416 417 static void fsl_asrc_dma_pcm_free(struct snd_pcm *pcm) 418 { 419 struct snd_pcm_substream *substream; 420 int i; 421 422 for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_LAST; i++) { 423 substream = pcm->streams[i].substream; 424 if (!substream) 425 continue; 426 427 snd_dma_free_pages(&substream->dma_buffer); 428 substream->dma_buffer.area = NULL; 429 substream->dma_buffer.addr = 0; 430 } 431 } 432 433 struct snd_soc_component_driver fsl_asrc_component = { 434 .name = DRV_NAME, 435 .ops = &fsl_asrc_dma_pcm_ops, 436 .pcm_new = fsl_asrc_dma_pcm_new, 437 .pcm_free = fsl_asrc_dma_pcm_free, 438 }; 439 EXPORT_SYMBOL_GPL(fsl_asrc_component); 440