1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2021, Linaro Limited 3 4 #include <linux/init.h> 5 #include <linux/err.h> 6 #include <linux/module.h> 7 #include <linux/of.h> 8 #include <linux/platform_device.h> 9 #include <linux/slab.h> 10 #include <sound/soc.h> 11 #include <sound/soc-dapm.h> 12 #include <linux/spinlock.h> 13 #include <sound/pcm.h> 14 #include <asm/div64.h> 15 #include <asm/dma.h> 16 #include <linux/dma-mapping.h> 17 #include <sound/pcm_params.h> 18 #include "q6apm.h" 19 20 #define DRV_NAME "q6apm-dai" 21 22 #define PLAYBACK_MIN_NUM_PERIODS 2 23 #define PLAYBACK_MAX_NUM_PERIODS 8 24 #define PLAYBACK_MAX_PERIOD_SIZE 65536 25 #define PLAYBACK_MIN_PERIOD_SIZE 128 26 #define CAPTURE_MIN_NUM_PERIODS 2 27 #define CAPTURE_MAX_NUM_PERIODS 8 28 #define CAPTURE_MAX_PERIOD_SIZE 65536 29 #define CAPTURE_MIN_PERIOD_SIZE 6144 30 #define BUFFER_BYTES_MAX (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE) 31 #define BUFFER_BYTES_MIN (PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE) 32 #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024) 33 #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4) 34 #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024) 35 #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4) 36 #define SID_MASK_DEFAULT 0xF 37 38 static const struct snd_compr_codec_caps q6apm_compr_caps = { 39 .num_descriptors = 1, 40 .descriptor[0].max_ch = 2, 41 .descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050, 42 24000, 32000, 44100, 48000, 88200, 43 96000, 176400, 192000 }, 44 .descriptor[0].num_sample_rates = 13, 45 .descriptor[0].bit_rate[0] = 320, 46 .descriptor[0].bit_rate[1] = 128, 47 .descriptor[0].num_bitrates = 2, 48 .descriptor[0].profiles = 0, 49 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO, 50 .descriptor[0].formats = 0, 51 }; 52 53 enum stream_state { 54 Q6APM_STREAM_IDLE = 0, 55 Q6APM_STREAM_STOPPED, 56 Q6APM_STREAM_RUNNING, 57 }; 58 59 struct q6apm_dai_rtd { 60 struct snd_pcm_substream *substream; 61 struct snd_compr_stream *cstream; 62 struct snd_codec codec; 63 struct snd_compr_params codec_param; 64 struct snd_dma_buffer dma_buffer; 65 phys_addr_t phys; 66 unsigned int pcm_size; 67 unsigned int pcm_count; 68 unsigned int periods; 69 uint64_t bytes_sent; 70 uint64_t bytes_received; 71 uint64_t copied_total; 72 uint16_t bits_per_sample; 73 snd_pcm_uframes_t queue_ptr; 74 bool next_track; 75 enum stream_state state; 76 struct q6apm_graph *graph; 77 spinlock_t lock; 78 bool notify_on_drain; 79 }; 80 81 struct q6apm_dai_data { 82 long long sid; 83 }; 84 85 static const struct snd_pcm_hardware q6apm_dai_hardware_capture = { 86 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER | 87 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED | 88 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME | 89 SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | 90 SNDRV_PCM_INFO_BATCH), 91 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE), 92 .rates = SNDRV_PCM_RATE_8000_48000, 93 .rate_min = 8000, 94 .rate_max = 48000, 95 .channels_min = 2, 96 .channels_max = 4, 97 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE, 98 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, 99 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, 100 .periods_min = CAPTURE_MIN_NUM_PERIODS, 101 .periods_max = CAPTURE_MAX_NUM_PERIODS, 102 .fifo_size = 0, 103 }; 104 105 static const struct snd_pcm_hardware q6apm_dai_hardware_playback = { 106 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER | 107 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED | 108 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME | 109 SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | 110 SNDRV_PCM_INFO_BATCH), 111 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE), 112 .rates = SNDRV_PCM_RATE_8000_192000, 113 .rate_min = 8000, 114 .rate_max = 192000, 115 .channels_min = 2, 116 .channels_max = 8, 117 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE), 118 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, 119 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, 120 .periods_min = PLAYBACK_MIN_NUM_PERIODS, 121 .periods_max = PLAYBACK_MAX_NUM_PERIODS, 122 .fifo_size = 0, 123 }; 124 125 static void event_handler(uint32_t opcode, uint32_t token, void *payload, void *priv) 126 { 127 struct q6apm_dai_rtd *prtd = priv; 128 struct snd_pcm_substream *substream = prtd->substream; 129 130 switch (opcode) { 131 case APM_CLIENT_EVENT_CMD_EOS_DONE: 132 prtd->state = Q6APM_STREAM_STOPPED; 133 break; 134 case APM_CLIENT_EVENT_DATA_WRITE_DONE: 135 snd_pcm_period_elapsed(substream); 136 137 break; 138 case APM_CLIENT_EVENT_DATA_READ_DONE: 139 snd_pcm_period_elapsed(substream); 140 if (prtd->state == Q6APM_STREAM_RUNNING) 141 q6apm_read(prtd->graph); 142 143 break; 144 default: 145 break; 146 } 147 } 148 149 static void event_handler_compr(uint32_t opcode, uint32_t token, 150 void *payload, void *priv) 151 { 152 struct q6apm_dai_rtd *prtd = priv; 153 struct snd_compr_stream *substream = prtd->cstream; 154 uint32_t wflags = 0; 155 uint64_t avail; 156 uint32_t bytes_written, bytes_to_write; 157 bool is_last_buffer = false; 158 159 guard(spinlock_irqsave)(&prtd->lock); 160 switch (opcode) { 161 case APM_CLIENT_EVENT_CMD_EOS_DONE: 162 if (prtd->notify_on_drain) { 163 snd_compr_drain_notify(prtd->cstream); 164 prtd->notify_on_drain = false; 165 } else { 166 prtd->state = Q6APM_STREAM_STOPPED; 167 } 168 break; 169 case APM_CLIENT_EVENT_DATA_WRITE_DONE: 170 bytes_written = token >> APM_WRITE_TOKEN_LEN_SHIFT; 171 prtd->copied_total += bytes_written; 172 snd_compr_fragment_elapsed(substream); 173 174 if (prtd->state != Q6APM_STREAM_RUNNING) 175 break; 176 177 avail = prtd->bytes_received - prtd->bytes_sent; 178 179 if (avail > prtd->pcm_count) { 180 bytes_to_write = prtd->pcm_count; 181 } else { 182 if (substream->partial_drain || prtd->notify_on_drain) 183 is_last_buffer = true; 184 bytes_to_write = avail; 185 } 186 187 if (bytes_to_write) { 188 if (substream->partial_drain && is_last_buffer) 189 wflags |= APM_LAST_BUFFER_FLAG; 190 191 q6apm_write_async(prtd->graph, 192 bytes_to_write, 0, 0, wflags); 193 194 prtd->bytes_sent += bytes_to_write; 195 196 if (prtd->notify_on_drain && is_last_buffer) 197 audioreach_shared_memory_send_eos(prtd->graph); 198 } 199 200 break; 201 default: 202 break; 203 } 204 } 205 206 static int q6apm_dai_prepare(struct snd_soc_component *component, 207 struct snd_pcm_substream *substream) 208 { 209 struct snd_pcm_runtime *runtime = substream->runtime; 210 struct q6apm_dai_rtd *prtd = runtime->private_data; 211 struct audioreach_module_config cfg; 212 struct device *dev = component->dev; 213 struct q6apm_dai_data *pdata; 214 int ret; 215 216 pdata = snd_soc_component_get_drvdata(component); 217 if (!pdata) 218 return -EINVAL; 219 220 if (!prtd || !prtd->graph) { 221 dev_err(dev, "%s: private data null or audio client freed\n", __func__); 222 return -EINVAL; 223 } 224 225 cfg.direction = substream->stream; 226 cfg.sample_rate = runtime->rate; 227 cfg.num_channels = runtime->channels; 228 cfg.bit_width = prtd->bits_per_sample; 229 cfg.fmt = SND_AUDIOCODEC_PCM; 230 audioreach_set_default_channel_mapping(cfg.channel_map, runtime->channels); 231 if (prtd->state) { 232 /* clear the previous setup if any */ 233 q6apm_graph_stop(prtd->graph); 234 q6apm_free_fragments(prtd->graph, substream->stream); 235 } 236 237 prtd->pcm_count = snd_pcm_lib_period_bytes(substream); 238 /* rate and channels are sent to audio driver */ 239 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg); 240 if (ret < 0) { 241 dev_err(dev, "%s: q6apm_open_write failed\n", __func__); 242 return ret; 243 } 244 245 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg); 246 if (ret < 0) 247 dev_err(dev, "%s: CMD Format block failed\n", __func__); 248 249 ret = q6apm_alloc_fragments(prtd->graph, substream->stream, prtd->phys, 250 (prtd->pcm_size / prtd->periods), prtd->periods); 251 252 if (ret < 0) { 253 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret); 254 return -ENOMEM; 255 } 256 257 ret = q6apm_graph_prepare(prtd->graph); 258 if (ret) { 259 dev_err(dev, "Failed to prepare Graph %d\n", ret); 260 return ret; 261 } 262 263 ret = q6apm_graph_start(prtd->graph); 264 if (ret) { 265 dev_err(dev, "Failed to Start Graph %d\n", ret); 266 return ret; 267 } 268 269 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 270 int i; 271 /* Queue the buffers for Capture ONLY after graph is started */ 272 for (i = 0; i < runtime->periods; i++) 273 q6apm_read(prtd->graph); 274 275 } 276 277 /* Now that graph as been prepared and started update the internal state accordingly */ 278 prtd->state = Q6APM_STREAM_RUNNING; 279 280 return 0; 281 } 282 283 static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream) 284 { 285 struct snd_pcm_runtime *runtime = substream->runtime; 286 struct q6apm_dai_rtd *prtd = runtime->private_data; 287 int i, ret = 0, avail_periods; 288 289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 290 avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size; 291 for (i = 0; i < avail_periods; i++) { 292 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP); 293 if (ret < 0) { 294 dev_err(component->dev, "Error queuing playback buffer %d\n", ret); 295 return ret; 296 } 297 prtd->queue_ptr += runtime->period_size; 298 } 299 } 300 301 return ret; 302 } 303 304 static int q6apm_dai_trigger(struct snd_soc_component *component, 305 struct snd_pcm_substream *substream, int cmd) 306 { 307 struct snd_pcm_runtime *runtime = substream->runtime; 308 struct q6apm_dai_rtd *prtd = runtime->private_data; 309 int ret = 0; 310 311 switch (cmd) { 312 case SNDRV_PCM_TRIGGER_START: 313 case SNDRV_PCM_TRIGGER_RESUME: 314 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 315 break; 316 case SNDRV_PCM_TRIGGER_STOP: 317 /* TODO support be handled via SoftPause Module */ 318 prtd->state = Q6APM_STREAM_STOPPED; 319 prtd->queue_ptr = 0; 320 break; 321 case SNDRV_PCM_TRIGGER_SUSPEND: 322 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 323 break; 324 default: 325 ret = -EINVAL; 326 break; 327 } 328 329 return ret; 330 } 331 332 static int q6apm_dai_open(struct snd_soc_component *component, 333 struct snd_pcm_substream *substream) 334 { 335 struct snd_pcm_runtime *runtime = substream->runtime; 336 struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream); 337 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0); 338 struct device *dev = component->dev; 339 struct q6apm_dai_data *pdata; 340 struct q6apm_dai_rtd *prtd; 341 int graph_id, ret; 342 343 graph_id = cpu_dai->driver->id; 344 345 pdata = snd_soc_component_get_drvdata(component); 346 if (!pdata) { 347 dev_err(dev, "Drv data not found ..\n"); 348 return -EINVAL; 349 } 350 351 prtd = kzalloc_obj(*prtd); 352 if (prtd == NULL) 353 return -ENOMEM; 354 355 spin_lock_init(&prtd->lock); 356 prtd->substream = substream; 357 prtd->graph = q6apm_graph_open(dev, event_handler, prtd, graph_id, substream->stream); 358 if (IS_ERR(prtd->graph)) { 359 dev_err(dev, "%s: Could not allocate memory\n", __func__); 360 ret = PTR_ERR(prtd->graph); 361 goto err; 362 } 363 364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 365 runtime->hw = q6apm_dai_hardware_playback; 366 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 367 runtime->hw = q6apm_dai_hardware_capture; 368 369 /* Ensure that buffer size is a multiple of period size */ 370 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 371 if (ret < 0) { 372 dev_err(dev, "snd_pcm_hw_constraint_integer failed\n"); 373 goto err; 374 } 375 376 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 377 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 378 BUFFER_BYTES_MIN, BUFFER_BYTES_MAX); 379 if (ret < 0) { 380 dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret); 381 goto err; 382 } 383 } 384 385 /* setup 10ms latency to accommodate DSP restrictions */ 386 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 480); 387 if (ret < 0) { 388 dev_err(dev, "constraint for period bytes step ret = %d\n", ret); 389 goto err; 390 } 391 392 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 480); 393 if (ret < 0) { 394 dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret); 395 goto err; 396 } 397 398 runtime->private_data = prtd; 399 runtime->dma_bytes = BUFFER_BYTES_MAX; 400 if (pdata->sid < 0) 401 prtd->phys = substream->dma_buffer.addr; 402 else 403 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32); 404 405 return 0; 406 err: 407 kfree(prtd); 408 409 return ret; 410 } 411 412 static int q6apm_dai_close(struct snd_soc_component *component, 413 struct snd_pcm_substream *substream) 414 { 415 struct snd_pcm_runtime *runtime = substream->runtime; 416 struct q6apm_dai_rtd *prtd = runtime->private_data; 417 418 if (prtd->state) { 419 /* only stop graph that is started */ 420 q6apm_graph_stop(prtd->graph); 421 q6apm_free_fragments(prtd->graph, substream->stream); 422 } 423 424 q6apm_graph_close(prtd->graph); 425 prtd->graph = NULL; 426 kfree(prtd); 427 runtime->private_data = NULL; 428 429 return 0; 430 } 431 432 static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component, 433 struct snd_pcm_substream *substream) 434 { 435 struct snd_pcm_runtime *runtime = substream->runtime; 436 struct q6apm_dai_rtd *prtd = runtime->private_data; 437 snd_pcm_uframes_t ptr; 438 439 ptr = q6apm_get_hw_pointer(prtd->graph, substream->stream) * runtime->period_size; 440 if (ptr) 441 return ptr - 1; 442 443 return 0; 444 } 445 446 static int q6apm_dai_hw_params(struct snd_soc_component *component, 447 struct snd_pcm_substream *substream, 448 struct snd_pcm_hw_params *params) 449 { 450 struct snd_pcm_runtime *runtime = substream->runtime; 451 struct q6apm_dai_rtd *prtd = runtime->private_data; 452 453 prtd->pcm_size = params_buffer_bytes(params); 454 prtd->periods = params_periods(params); 455 456 switch (params_format(params)) { 457 case SNDRV_PCM_FORMAT_S16_LE: 458 prtd->bits_per_sample = 16; 459 break; 460 case SNDRV_PCM_FORMAT_S24_LE: 461 prtd->bits_per_sample = 24; 462 break; 463 default: 464 return -EINVAL; 465 } 466 467 return 0; 468 } 469 470 static int q6apm_dai_memory_map(struct snd_soc_component *component, 471 struct snd_pcm_substream *substream, int graph_id) 472 { 473 struct q6apm_dai_data *pdata; 474 struct device *dev = component->dev; 475 phys_addr_t phys; 476 int ret; 477 478 pdata = snd_soc_component_get_drvdata(component); 479 if (!pdata) { 480 dev_err(component->dev, "Drv data not found ..\n"); 481 return -EINVAL; 482 } 483 484 if (pdata->sid < 0) 485 phys = substream->dma_buffer.addr; 486 else 487 phys = substream->dma_buffer.addr | (pdata->sid << 32); 488 489 ret = q6apm_map_memory_fixed_region(dev, graph_id, phys, BUFFER_BYTES_MAX); 490 if (ret < 0) 491 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret); 492 493 return ret; 494 } 495 496 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd) 497 { 498 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 499 struct snd_pcm *pcm = rtd->pcm; 500 /* 501 * Allocate one extra page as a workaround for a DSP bug where 32-bit 502 * address arithmetic can overflow when the buffer is placed near the 503 * end of the addressable range. 504 */ 505 int size = BUFFER_BYTES_MAX + PAGE_SIZE; 506 int graph_id, ret; 507 struct snd_pcm_substream *substream; 508 509 graph_id = cpu_dai->driver->id; 510 511 ret = snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, component->dev, size); 512 if (ret) 513 return ret; 514 515 /* Note: DSP backend dais are uni-directional ONLY(either playback or capture) */ 516 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { 517 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 518 ret = q6apm_dai_memory_map(component, substream, graph_id); 519 if (ret) 520 return ret; 521 } 522 523 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { 524 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 525 ret = q6apm_dai_memory_map(component, substream, graph_id); 526 if (ret) 527 return ret; 528 } 529 530 return 0; 531 } 532 533 static void q6apm_dai_memory_unmap(struct snd_soc_component *component, 534 struct snd_pcm_substream *substream) 535 { 536 struct snd_soc_pcm_runtime *soc_prtd; 537 struct snd_soc_dai *cpu_dai; 538 int graph_id; 539 540 soc_prtd = snd_soc_substream_to_rtd(substream); 541 if (!soc_prtd) 542 return; 543 544 cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0); 545 if (!cpu_dai) 546 return; 547 548 graph_id = cpu_dai->driver->id; 549 q6apm_unmap_memory_fixed_region(component->dev, graph_id); 550 } 551 552 static void q6apm_dai_pcm_free(struct snd_soc_component *component, struct snd_pcm *pcm) 553 { 554 struct snd_pcm_substream *substream; 555 556 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 557 if (substream) 558 q6apm_dai_memory_unmap(component, substream); 559 560 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 561 if (substream) 562 q6apm_dai_memory_unmap(component, substream); 563 } 564 565 static int q6apm_dai_compr_open(struct snd_soc_component *component, 566 struct snd_compr_stream *stream) 567 { 568 struct snd_soc_pcm_runtime *rtd = stream->private_data; 569 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 570 struct snd_compr_runtime *runtime = stream->runtime; 571 struct q6apm_dai_rtd *prtd; 572 struct q6apm_dai_data *pdata; 573 struct device *dev = component->dev; 574 int ret, size; 575 int graph_id; 576 577 graph_id = cpu_dai->driver->id; 578 pdata = snd_soc_component_get_drvdata(component); 579 if (!pdata) 580 return -EINVAL; 581 582 prtd = kzalloc_obj(*prtd); 583 if (prtd == NULL) 584 return -ENOMEM; 585 586 prtd->cstream = stream; 587 prtd->graph = q6apm_graph_open(dev, event_handler_compr, prtd, graph_id, 588 SNDRV_PCM_STREAM_PLAYBACK); 589 if (IS_ERR(prtd->graph)) { 590 ret = PTR_ERR(prtd->graph); 591 kfree(prtd); 592 return ret; 593 } 594 595 runtime->private_data = prtd; 596 runtime->dma_bytes = BUFFER_BYTES_MAX; 597 size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; 598 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &prtd->dma_buffer); 599 if (ret) 600 return ret; 601 602 if (pdata->sid < 0) 603 prtd->phys = prtd->dma_buffer.addr; 604 else 605 prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32); 606 607 snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer); 608 spin_lock_init(&prtd->lock); 609 610 q6apm_enable_compress_module(dev, prtd->graph, true); 611 return 0; 612 } 613 614 static int q6apm_dai_compr_free(struct snd_soc_component *component, 615 struct snd_compr_stream *stream) 616 { 617 struct snd_compr_runtime *runtime = stream->runtime; 618 struct q6apm_dai_rtd *prtd = runtime->private_data; 619 620 q6apm_graph_stop(prtd->graph); 621 q6apm_free_fragments(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK); 622 q6apm_unmap_memory_fixed_region(component->dev, prtd->graph->id); 623 q6apm_graph_close(prtd->graph); 624 snd_dma_free_pages(&prtd->dma_buffer); 625 prtd->graph = NULL; 626 kfree(prtd); 627 runtime->private_data = NULL; 628 629 return 0; 630 } 631 632 static int q6apm_dai_compr_get_caps(struct snd_soc_component *component, 633 struct snd_compr_stream *stream, 634 struct snd_compr_caps *caps) 635 { 636 caps->direction = SND_COMPRESS_PLAYBACK; 637 caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE; 638 caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE; 639 caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS; 640 caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; 641 caps->num_codecs = 4; 642 caps->codecs[0] = SND_AUDIOCODEC_MP3; 643 caps->codecs[1] = SND_AUDIOCODEC_AAC; 644 caps->codecs[2] = SND_AUDIOCODEC_FLAC; 645 caps->codecs[3] = SND_AUDIOCODEC_OPUS_RAW; 646 647 return 0; 648 } 649 650 static int q6apm_dai_compr_get_codec_caps(struct snd_soc_component *component, 651 struct snd_compr_stream *stream, 652 struct snd_compr_codec_caps *codec) 653 { 654 switch (codec->codec) { 655 case SND_AUDIOCODEC_MP3: 656 *codec = q6apm_compr_caps; 657 break; 658 default: 659 break; 660 } 661 662 return 0; 663 } 664 665 static int q6apm_dai_compr_pointer(struct snd_soc_component *component, 666 struct snd_compr_stream *stream, 667 struct snd_compr_tstamp64 *tstamp) 668 { 669 struct snd_compr_runtime *runtime = stream->runtime; 670 struct q6apm_dai_rtd *prtd = runtime->private_data; 671 uint64_t temp_copied_total; 672 673 guard(spinlock_irqsave)(&prtd->lock); 674 tstamp->copied_total = prtd->copied_total; 675 temp_copied_total = tstamp->copied_total; 676 tstamp->byte_offset = do_div(temp_copied_total, prtd->pcm_size); 677 678 return 0; 679 } 680 681 static int q6apm_dai_compr_trigger(struct snd_soc_component *component, 682 struct snd_compr_stream *stream, int cmd) 683 { 684 struct snd_compr_runtime *runtime = stream->runtime; 685 struct q6apm_dai_rtd *prtd = runtime->private_data; 686 int ret = 0; 687 688 switch (cmd) { 689 case SNDRV_PCM_TRIGGER_START: 690 case SNDRV_PCM_TRIGGER_RESUME: 691 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 692 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP); 693 break; 694 case SNDRV_PCM_TRIGGER_STOP: 695 break; 696 case SNDRV_PCM_TRIGGER_SUSPEND: 697 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 698 break; 699 case SND_COMPR_TRIGGER_NEXT_TRACK: 700 prtd->next_track = true; 701 break; 702 case SND_COMPR_TRIGGER_DRAIN: 703 case SND_COMPR_TRIGGER_PARTIAL_DRAIN: 704 prtd->notify_on_drain = true; 705 break; 706 default: 707 ret = -EINVAL; 708 break; 709 } 710 711 return ret; 712 } 713 714 static int q6apm_dai_compr_ack(struct snd_soc_component *component, struct snd_compr_stream *stream, 715 size_t count) 716 { 717 struct snd_compr_runtime *runtime = stream->runtime; 718 struct q6apm_dai_rtd *prtd = runtime->private_data; 719 720 guard(spinlock_irqsave)(&prtd->lock); 721 prtd->bytes_received += count; 722 723 return count; 724 } 725 726 static int q6apm_dai_compr_set_params(struct snd_soc_component *component, 727 struct snd_compr_stream *stream, 728 struct snd_compr_params *params) 729 { 730 struct snd_compr_runtime *runtime = stream->runtime; 731 struct q6apm_dai_rtd *prtd = runtime->private_data; 732 struct q6apm_dai_data *pdata; 733 struct audioreach_module_config cfg; 734 struct snd_codec *codec = ¶ms->codec; 735 int dir = stream->direction; 736 int ret; 737 738 pdata = snd_soc_component_get_drvdata(component); 739 if (!pdata) 740 return -EINVAL; 741 742 prtd->periods = runtime->fragments; 743 prtd->pcm_count = runtime->fragment_size; 744 prtd->pcm_size = runtime->fragments * runtime->fragment_size; 745 prtd->bits_per_sample = 16; 746 747 if (prtd->next_track != true) { 748 memcpy(&prtd->codec, codec, sizeof(*codec)); 749 750 ret = q6apm_set_real_module_id(component->dev, prtd->graph, codec->id); 751 if (ret) 752 return ret; 753 754 cfg.direction = dir; 755 cfg.sample_rate = codec->sample_rate; 756 cfg.num_channels = 2; 757 cfg.bit_width = prtd->bits_per_sample; 758 cfg.fmt = codec->id; 759 audioreach_set_default_channel_mapping(cfg.channel_map, 760 cfg.num_channels); 761 memcpy(&cfg.codec, codec, sizeof(*codec)); 762 763 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg); 764 if (ret < 0) 765 return ret; 766 767 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg); 768 if (ret) 769 return ret; 770 771 ret = q6apm_alloc_fragments(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK, 772 prtd->phys, (prtd->pcm_size / prtd->periods), 773 prtd->periods); 774 if (ret < 0) 775 return -ENOMEM; 776 777 ret = q6apm_graph_prepare(prtd->graph); 778 if (ret) 779 return ret; 780 781 ret = q6apm_graph_start(prtd->graph); 782 if (ret) 783 return ret; 784 785 } else { 786 cfg.direction = dir; 787 cfg.sample_rate = codec->sample_rate; 788 cfg.num_channels = 2; 789 cfg.bit_width = prtd->bits_per_sample; 790 cfg.fmt = codec->id; 791 memcpy(&cfg.codec, codec, sizeof(*codec)); 792 793 ret = audioreach_compr_set_param(prtd->graph, &cfg); 794 if (ret < 0) 795 return ret; 796 } 797 prtd->state = Q6APM_STREAM_RUNNING; 798 799 return 0; 800 } 801 802 static int q6apm_dai_compr_set_metadata(struct snd_soc_component *component, 803 struct snd_compr_stream *stream, 804 struct snd_compr_metadata *metadata) 805 { 806 struct snd_compr_runtime *runtime = stream->runtime; 807 struct q6apm_dai_rtd *prtd = runtime->private_data; 808 int ret = 0; 809 810 switch (metadata->key) { 811 case SNDRV_COMPRESS_ENCODER_PADDING: 812 q6apm_remove_trailing_silence(component->dev, prtd->graph, 813 metadata->value[0]); 814 break; 815 case SNDRV_COMPRESS_ENCODER_DELAY: 816 q6apm_remove_initial_silence(component->dev, prtd->graph, 817 metadata->value[0]); 818 break; 819 default: 820 ret = -EINVAL; 821 break; 822 } 823 824 return ret; 825 } 826 827 static int q6apm_dai_compr_mmap(struct snd_soc_component *component, 828 struct snd_compr_stream *stream, 829 struct vm_area_struct *vma) 830 { 831 struct snd_compr_runtime *runtime = stream->runtime; 832 struct q6apm_dai_rtd *prtd = runtime->private_data; 833 struct device *dev = component->dev; 834 835 return dma_mmap_coherent(dev, vma, prtd->dma_buffer.area, prtd->dma_buffer.addr, 836 prtd->dma_buffer.bytes); 837 } 838 839 static int q6apm_compr_copy(struct snd_soc_component *component, 840 struct snd_compr_stream *stream, char __user *buf, 841 size_t count) 842 { 843 struct snd_compr_runtime *runtime = stream->runtime; 844 struct q6apm_dai_rtd *prtd = runtime->private_data; 845 void *dstn; 846 size_t copy; 847 u32 wflags = 0; 848 u32 app_pointer; 849 uint64_t bytes_received; 850 uint64_t temp_bytes_received; 851 uint32_t bytes_to_write; 852 uint64_t avail, bytes_in_flight = 0; 853 854 bytes_received = prtd->bytes_received; 855 temp_bytes_received = bytes_received; 856 857 /** 858 * Make sure that next track data pointer is aligned at 32 bit boundary 859 * This is a Mandatory requirement from DSP data buffers alignment 860 */ 861 if (prtd->next_track) { 862 bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count); 863 temp_bytes_received = bytes_received; 864 } 865 866 app_pointer = do_div(temp_bytes_received, prtd->pcm_size); 867 dstn = prtd->dma_buffer.area + app_pointer; 868 869 if (count < prtd->pcm_size - app_pointer) { 870 if (copy_from_user(dstn, buf, count)) 871 return -EFAULT; 872 } else { 873 copy = prtd->pcm_size - app_pointer; 874 if (copy_from_user(dstn, buf, copy)) 875 return -EFAULT; 876 if (copy_from_user(prtd->dma_buffer.area, buf + copy, count - copy)) 877 return -EFAULT; 878 } 879 880 guard(spinlock_irqsave)(&prtd->lock); 881 bytes_in_flight = prtd->bytes_received - prtd->copied_total; 882 883 if (prtd->next_track) { 884 prtd->next_track = false; 885 prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count); 886 prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count); 887 } 888 889 prtd->bytes_received = bytes_received + count; 890 891 /* Kick off the data to dsp if its starving!! */ 892 if (prtd->state == Q6APM_STREAM_RUNNING && (bytes_in_flight == 0)) { 893 bytes_to_write = prtd->pcm_count; 894 avail = prtd->bytes_received - prtd->bytes_sent; 895 896 if (avail < prtd->pcm_count) 897 bytes_to_write = avail; 898 899 q6apm_write_async(prtd->graph, bytes_to_write, 0, 0, wflags); 900 prtd->bytes_sent += bytes_to_write; 901 } 902 903 return count; 904 } 905 906 static const struct snd_compress_ops q6apm_dai_compress_ops = { 907 .open = q6apm_dai_compr_open, 908 .free = q6apm_dai_compr_free, 909 .get_caps = q6apm_dai_compr_get_caps, 910 .get_codec_caps = q6apm_dai_compr_get_codec_caps, 911 .pointer = q6apm_dai_compr_pointer, 912 .trigger = q6apm_dai_compr_trigger, 913 .ack = q6apm_dai_compr_ack, 914 .set_params = q6apm_dai_compr_set_params, 915 .set_metadata = q6apm_dai_compr_set_metadata, 916 .mmap = q6apm_dai_compr_mmap, 917 .copy = q6apm_compr_copy, 918 }; 919 920 static const struct snd_soc_component_driver q6apm_fe_dai_component = { 921 .name = DRV_NAME, 922 .open = q6apm_dai_open, 923 .close = q6apm_dai_close, 924 .prepare = q6apm_dai_prepare, 925 .pcm_new = q6apm_dai_pcm_new, 926 .pcm_free = q6apm_dai_pcm_free, 927 .hw_params = q6apm_dai_hw_params, 928 .pointer = q6apm_dai_pointer, 929 .trigger = q6apm_dai_trigger, 930 .ack = q6apm_dai_ack, 931 .compress_ops = &q6apm_dai_compress_ops, 932 .use_dai_pcm_id = true, 933 .remove_order = SND_SOC_COMP_ORDER_EARLY, 934 }; 935 936 static int q6apm_dai_probe(struct platform_device *pdev) 937 { 938 struct device *dev = &pdev->dev; 939 struct device_node *node = dev->of_node; 940 struct q6apm_dai_data *pdata; 941 struct of_phandle_args args; 942 int rc; 943 944 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 945 if (!pdata) 946 return -ENOMEM; 947 948 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args); 949 if (rc < 0) 950 pdata->sid = -1; 951 else 952 pdata->sid = args.args[0] & SID_MASK_DEFAULT; 953 954 dev_set_drvdata(dev, pdata); 955 956 return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0); 957 } 958 959 #ifdef CONFIG_OF 960 static const struct of_device_id q6apm_dai_device_id[] = { 961 { .compatible = "qcom,q6apm-dais" }, 962 {}, 963 }; 964 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id); 965 #endif 966 967 static struct platform_driver q6apm_dai_platform_driver = { 968 .driver = { 969 .name = "q6apm-dai", 970 .of_match_table = of_match_ptr(q6apm_dai_device_id), 971 }, 972 .probe = q6apm_dai_probe, 973 }; 974 module_platform_driver(q6apm_dai_platform_driver); 975 976 MODULE_DESCRIPTION("Q6APM dai driver"); 977 MODULE_LICENSE("GPL"); 978