1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2019 Spreadtrum Communications Inc. 3 4 #include <linux/dma-mapping.h> 5 #include <linux/dmaengine.h> 6 #include <linux/dma/sprd-dma.h> 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/platform_device.h> 10 #include <sound/pcm.h> 11 #include <sound/pcm_params.h> 12 #include <sound/soc.h> 13 14 #include "sprd-pcm-dma.h" 15 16 #define SPRD_PCM_DMA_LINKLIST_SIZE 64 17 #define SPRD_PCM_DMA_BRUST_LEN 640 18 19 struct sprd_pcm_dma_data { 20 struct dma_chan *chan; 21 struct dma_async_tx_descriptor *desc; 22 dma_cookie_t cookie; 23 dma_addr_t phys; 24 void *virt; 25 int pre_pointer; 26 }; 27 28 struct sprd_pcm_dma_private { 29 struct snd_pcm_substream *substream; 30 struct sprd_pcm_dma_params *params; 31 struct sprd_pcm_dma_data data[SPRD_PCM_CHANNEL_MAX]; 32 int hw_chan; 33 int dma_addr_offset; 34 }; 35 36 static const struct snd_pcm_hardware sprd_pcm_hardware = { 37 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 38 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_PAUSE | 39 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, 40 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 41 .period_bytes_min = 1, 42 .period_bytes_max = 64 * 1024, 43 .periods_min = 1, 44 .periods_max = PAGE_SIZE / SPRD_PCM_DMA_LINKLIST_SIZE, 45 .buffer_bytes_max = 64 * 1024, 46 }; 47 48 static int sprd_pcm_open(struct snd_pcm_substream *substream) 49 { 50 struct snd_pcm_runtime *runtime = substream->runtime; 51 struct snd_soc_pcm_runtime *rtd = substream->private_data; 52 struct snd_soc_component *component = 53 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 54 struct device *dev = component->dev; 55 struct sprd_pcm_dma_private *dma_private; 56 int hw_chan = SPRD_PCM_CHANNEL_MAX; 57 int size, ret, i; 58 59 snd_soc_set_runtime_hwparams(substream, &sprd_pcm_hardware); 60 61 ret = snd_pcm_hw_constraint_step(runtime, 0, 62 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 63 SPRD_PCM_DMA_BRUST_LEN); 64 if (ret < 0) 65 return ret; 66 67 ret = snd_pcm_hw_constraint_step(runtime, 0, 68 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 69 SPRD_PCM_DMA_BRUST_LEN); 70 if (ret < 0) 71 return ret; 72 73 ret = snd_pcm_hw_constraint_integer(runtime, 74 SNDRV_PCM_HW_PARAM_PERIODS); 75 if (ret < 0) 76 return ret; 77 78 dma_private = devm_kzalloc(dev, sizeof(*dma_private), GFP_KERNEL); 79 if (!dma_private) 80 return -ENOMEM; 81 82 size = runtime->hw.periods_max * SPRD_PCM_DMA_LINKLIST_SIZE; 83 84 for (i = 0; i < hw_chan; i++) { 85 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 86 87 data->virt = dmam_alloc_coherent(dev, size, &data->phys, 88 GFP_KERNEL); 89 if (!data->virt) { 90 ret = -ENOMEM; 91 goto error; 92 } 93 } 94 95 dma_private->hw_chan = hw_chan; 96 runtime->private_data = dma_private; 97 dma_private->substream = substream; 98 99 return 0; 100 101 error: 102 for (i = 0; i < hw_chan; i++) { 103 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 104 105 if (data->virt) 106 dmam_free_coherent(dev, size, data->virt, data->phys); 107 } 108 109 devm_kfree(dev, dma_private); 110 return ret; 111 } 112 113 static int sprd_pcm_close(struct snd_pcm_substream *substream) 114 { 115 struct snd_pcm_runtime *runtime = substream->runtime; 116 struct snd_soc_pcm_runtime *rtd = substream->private_data; 117 struct sprd_pcm_dma_private *dma_private = runtime->private_data; 118 struct snd_soc_component *component = 119 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 120 struct device *dev = component->dev; 121 int size = runtime->hw.periods_max * SPRD_PCM_DMA_LINKLIST_SIZE; 122 int i; 123 124 for (i = 0; i < dma_private->hw_chan; i++) { 125 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 126 127 dmam_free_coherent(dev, size, data->virt, data->phys); 128 } 129 130 devm_kfree(dev, dma_private); 131 132 return 0; 133 } 134 135 static void sprd_pcm_dma_complete(void *data) 136 { 137 struct sprd_pcm_dma_private *dma_private = data; 138 struct snd_pcm_substream *substream = dma_private->substream; 139 140 snd_pcm_period_elapsed(substream); 141 } 142 143 static void sprd_pcm_release_dma_channel(struct snd_pcm_substream *substream) 144 { 145 struct snd_pcm_runtime *runtime = substream->runtime; 146 struct sprd_pcm_dma_private *dma_private = runtime->private_data; 147 int i; 148 149 for (i = 0; i < SPRD_PCM_CHANNEL_MAX; i++) { 150 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 151 152 if (data->chan) { 153 dma_release_channel(data->chan); 154 data->chan = NULL; 155 } 156 } 157 } 158 159 static int sprd_pcm_request_dma_channel(struct snd_pcm_substream *substream, 160 int channels) 161 { 162 struct snd_pcm_runtime *runtime = substream->runtime; 163 struct sprd_pcm_dma_private *dma_private = runtime->private_data; 164 struct snd_soc_pcm_runtime *rtd = substream->private_data; 165 struct snd_soc_component *component = 166 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 167 struct device *dev = component->dev; 168 struct sprd_pcm_dma_params *dma_params = dma_private->params; 169 int i; 170 171 if (channels > SPRD_PCM_CHANNEL_MAX) { 172 dev_err(dev, "invalid dma channel number:%d\n", channels); 173 return -EINVAL; 174 } 175 176 for (i = 0; i < channels; i++) { 177 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 178 179 data->chan = dma_request_slave_channel(dev, 180 dma_params->chan_name[i]); 181 if (!data->chan) { 182 dev_err(dev, "failed to request dma channel:%s\n", 183 dma_params->chan_name[i]); 184 sprd_pcm_release_dma_channel(substream); 185 return -ENODEV; 186 } 187 } 188 189 return 0; 190 } 191 192 static int sprd_pcm_hw_params(struct snd_pcm_substream *substream, 193 struct snd_pcm_hw_params *params) 194 { 195 struct snd_pcm_runtime *runtime = substream->runtime; 196 struct sprd_pcm_dma_private *dma_private = runtime->private_data; 197 struct snd_soc_pcm_runtime *rtd = substream->private_data; 198 struct snd_soc_component *component = 199 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 200 struct sprd_pcm_dma_params *dma_params; 201 size_t totsize = params_buffer_bytes(params); 202 size_t period = params_period_bytes(params); 203 int channels = params_channels(params); 204 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 205 struct scatterlist *sg; 206 unsigned long flags; 207 int ret, i, j, sg_num; 208 209 dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); 210 if (!dma_params) { 211 dev_warn(component->dev, "no dma parameters setting\n"); 212 dma_private->params = NULL; 213 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 214 runtime->dma_bytes = totsize; 215 return 0; 216 } 217 218 if (!dma_private->params) { 219 dma_private->params = dma_params; 220 ret = sprd_pcm_request_dma_channel(substream, channels); 221 if (ret) 222 return ret; 223 } 224 225 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 226 227 runtime->dma_bytes = totsize; 228 sg_num = totsize / period; 229 dma_private->dma_addr_offset = totsize / channels; 230 231 sg = devm_kcalloc(component->dev, sg_num, sizeof(*sg), GFP_KERNEL); 232 if (!sg) { 233 ret = -ENOMEM; 234 goto sg_err; 235 } 236 237 for (i = 0; i < channels; i++) { 238 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 239 struct dma_chan *chan = data->chan; 240 struct dma_slave_config config = { }; 241 struct sprd_dma_linklist link = { }; 242 enum dma_transfer_direction dir; 243 struct scatterlist *sgt = sg; 244 245 config.src_maxburst = dma_params->fragment_len[i]; 246 config.src_addr_width = dma_params->datawidth[i]; 247 config.dst_addr_width = dma_params->datawidth[i]; 248 if (is_playback) { 249 config.src_addr = runtime->dma_addr + 250 i * dma_private->dma_addr_offset; 251 config.dst_addr = dma_params->dev_phys[i]; 252 dir = DMA_MEM_TO_DEV; 253 } else { 254 config.src_addr = dma_params->dev_phys[i]; 255 config.dst_addr = runtime->dma_addr + 256 i * dma_private->dma_addr_offset; 257 dir = DMA_DEV_TO_MEM; 258 } 259 260 sg_init_table(sgt, sg_num); 261 for (j = 0; j < sg_num; j++, sgt++) { 262 u32 sg_len = period / channels; 263 264 sg_dma_len(sgt) = sg_len; 265 sg_dma_address(sgt) = runtime->dma_addr + 266 i * dma_private->dma_addr_offset + sg_len * j; 267 } 268 269 /* 270 * Configure the link-list address for the DMA engine link-list 271 * mode. 272 */ 273 link.virt_addr = (unsigned long)data->virt; 274 link.phy_addr = data->phys; 275 276 ret = dmaengine_slave_config(chan, &config); 277 if (ret) { 278 dev_err(component->dev, 279 "failed to set slave configuration: %d\n", ret); 280 goto config_err; 281 } 282 283 /* 284 * We configure the DMA request mode, interrupt mode, channel 285 * mode and channel trigger mode by the flags. 286 */ 287 flags = SPRD_DMA_FLAGS(SPRD_DMA_CHN_MODE_NONE, SPRD_DMA_NO_TRG, 288 SPRD_DMA_FRAG_REQ, SPRD_DMA_TRANS_INT); 289 data->desc = chan->device->device_prep_slave_sg(chan, sg, 290 sg_num, dir, 291 flags, &link); 292 if (!data->desc) { 293 dev_err(component->dev, "failed to prepare slave sg\n"); 294 ret = -ENOMEM; 295 goto config_err; 296 } 297 298 if (!runtime->no_period_wakeup) { 299 data->desc->callback = sprd_pcm_dma_complete; 300 data->desc->callback_param = dma_private; 301 } 302 } 303 304 devm_kfree(component->dev, sg); 305 306 return 0; 307 308 config_err: 309 devm_kfree(component->dev, sg); 310 sg_err: 311 sprd_pcm_release_dma_channel(substream); 312 return ret; 313 } 314 315 static int sprd_pcm_hw_free(struct snd_pcm_substream *substream) 316 { 317 snd_pcm_set_runtime_buffer(substream, NULL); 318 sprd_pcm_release_dma_channel(substream); 319 320 return 0; 321 } 322 323 static int sprd_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 324 { 325 struct sprd_pcm_dma_private *dma_private = 326 substream->runtime->private_data; 327 struct snd_soc_pcm_runtime *rtd = substream->private_data; 328 struct snd_soc_component *component = 329 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 330 int ret = 0, i; 331 332 switch (cmd) { 333 case SNDRV_PCM_TRIGGER_START: 334 for (i = 0; i < dma_private->hw_chan; i++) { 335 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 336 337 if (!data->desc) 338 continue; 339 340 data->cookie = dmaengine_submit(data->desc); 341 ret = dma_submit_error(data->cookie); 342 if (ret) { 343 dev_err(component->dev, 344 "failed to submit dma request: %d\n", 345 ret); 346 return ret; 347 } 348 349 dma_async_issue_pending(data->chan); 350 } 351 352 break; 353 case SNDRV_PCM_TRIGGER_RESUME: 354 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 355 for (i = 0; i < dma_private->hw_chan; i++) { 356 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 357 358 if (data->chan) 359 dmaengine_resume(data->chan); 360 } 361 362 break; 363 case SNDRV_PCM_TRIGGER_STOP: 364 for (i = 0; i < dma_private->hw_chan; i++) { 365 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 366 367 if (data->chan) 368 dmaengine_terminate_async(data->chan); 369 } 370 371 break; 372 case SNDRV_PCM_TRIGGER_SUSPEND: 373 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 374 for (i = 0; i < dma_private->hw_chan; i++) { 375 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 376 377 if (data->chan) 378 dmaengine_pause(data->chan); 379 } 380 381 break; 382 default: 383 ret = -EINVAL; 384 } 385 386 return ret; 387 } 388 389 static snd_pcm_uframes_t sprd_pcm_pointer(struct snd_pcm_substream *substream) 390 { 391 struct snd_pcm_runtime *runtime = substream->runtime; 392 struct snd_soc_pcm_runtime *rtd = substream->private_data; 393 struct sprd_pcm_dma_private *dma_private = runtime->private_data; 394 struct snd_soc_component *component = 395 snd_soc_rtdcom_lookup(rtd, DRV_NAME); 396 int pointer[SPRD_PCM_CHANNEL_MAX]; 397 int bytes_of_pointer = 0, sel_max = 0, i; 398 snd_pcm_uframes_t x; 399 struct dma_tx_state state; 400 enum dma_status status; 401 402 for (i = 0; i < dma_private->hw_chan; i++) { 403 struct sprd_pcm_dma_data *data = &dma_private->data[i]; 404 405 if (!data->chan) 406 continue; 407 408 status = dmaengine_tx_status(data->chan, data->cookie, &state); 409 if (status == DMA_ERROR) { 410 dev_err(component->dev, 411 "failed to get dma channel %d status\n", i); 412 return 0; 413 } 414 415 /* 416 * We just get current transfer address from the DMA engine, so 417 * we need convert to current pointer. 418 */ 419 pointer[i] = state.residue - runtime->dma_addr - 420 i * dma_private->dma_addr_offset; 421 422 if (i == 0) { 423 bytes_of_pointer = pointer[i]; 424 sel_max = pointer[i] < data->pre_pointer ? 1 : 0; 425 } else { 426 sel_max ^= pointer[i] < data->pre_pointer ? 1 : 0; 427 428 if (sel_max) 429 bytes_of_pointer = 430 max(pointer[i], pointer[i - 1]) << 1; 431 else 432 bytes_of_pointer = 433 min(pointer[i], pointer[i - 1]) << 1; 434 } 435 436 data->pre_pointer = pointer[i]; 437 } 438 439 x = bytes_to_frames(runtime, bytes_of_pointer); 440 if (x == runtime->buffer_size) 441 x = 0; 442 443 return x; 444 } 445 446 static int sprd_pcm_mmap(struct snd_pcm_substream *substream, 447 struct vm_area_struct *vma) 448 { 449 struct snd_pcm_runtime *runtime = substream->runtime; 450 451 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 452 return remap_pfn_range(vma, vma->vm_start, 453 runtime->dma_addr >> PAGE_SHIFT, 454 vma->vm_end - vma->vm_start, 455 vma->vm_page_prot); 456 } 457 458 static struct snd_pcm_ops sprd_pcm_ops = { 459 .open = sprd_pcm_open, 460 .close = sprd_pcm_close, 461 .ioctl = snd_pcm_lib_ioctl, 462 .hw_params = sprd_pcm_hw_params, 463 .hw_free = sprd_pcm_hw_free, 464 .trigger = sprd_pcm_trigger, 465 .pointer = sprd_pcm_pointer, 466 .mmap = sprd_pcm_mmap, 467 }; 468 469 static int sprd_pcm_new(struct snd_soc_pcm_runtime *rtd) 470 { 471 struct snd_card *card = rtd->card->snd_card; 472 struct snd_pcm *pcm = rtd->pcm; 473 struct snd_pcm_substream *substream; 474 int ret; 475 476 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); 477 if (ret) 478 return ret; 479 480 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 481 if (substream) { 482 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev, 483 sprd_pcm_hardware.buffer_bytes_max, 484 &substream->dma_buffer); 485 if (ret) { 486 dev_err(card->dev, 487 "can't alloc playback dma buffer: %d\n", ret); 488 return ret; 489 } 490 } 491 492 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 493 if (substream) { 494 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev, 495 sprd_pcm_hardware.buffer_bytes_max, 496 &substream->dma_buffer); 497 if (ret) { 498 dev_err(card->dev, 499 "can't alloc capture dma buffer: %d\n", ret); 500 snd_dma_free_pages(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer); 501 return ret; 502 } 503 } 504 505 return 0; 506 } 507 508 static void sprd_pcm_free(struct snd_pcm *pcm) 509 { 510 struct snd_pcm_substream *substream; 511 int i; 512 513 for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) { 514 substream = pcm->streams[i].substream; 515 if (substream) { 516 snd_dma_free_pages(&substream->dma_buffer); 517 substream->dma_buffer.area = NULL; 518 substream->dma_buffer.addr = 0; 519 } 520 } 521 } 522 523 static const struct snd_soc_component_driver sprd_soc_component = { 524 .name = DRV_NAME, 525 .ops = &sprd_pcm_ops, 526 .compr_ops = &sprd_platform_compr_ops, 527 .pcm_new = sprd_pcm_new, 528 .pcm_free = sprd_pcm_free, 529 }; 530 531 static int sprd_soc_platform_probe(struct platform_device *pdev) 532 { 533 int ret; 534 535 ret = devm_snd_soc_register_component(&pdev->dev, &sprd_soc_component, 536 NULL, 0); 537 if (ret) 538 dev_err(&pdev->dev, "could not register platform:%d\n", ret); 539 540 return ret; 541 } 542 543 static const struct of_device_id sprd_pcm_of_match[] = { 544 { .compatible = "sprd,pcm-platform", }, 545 { }, 546 }; 547 MODULE_DEVICE_TABLE(of, sprd_pcm_of_match); 548 549 static struct platform_driver sprd_pcm_driver = { 550 .driver = { 551 .name = "sprd-pcm-audio", 552 .of_match_table = sprd_pcm_of_match, 553 }, 554 555 .probe = sprd_soc_platform_probe, 556 }; 557 558 module_platform_driver(sprd_pcm_driver); 559 560 MODULE_DESCRIPTION("Spreadtrum ASoC PCM DMA"); 561 MODULE_LICENSE("GPL v2"); 562 MODULE_ALIAS("platform:sprd-audio"); 563