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