1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. 3 // Copyright (c) 2018, Linaro Limited 4 5 #include <linux/init.h> 6 #include <linux/err.h> 7 #include <linux/module.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 <sound/pcm.h> 13 #include <linux/spinlock.h> 14 #include <sound/compress_driver.h> 15 #include <asm/dma.h> 16 #include <linux/dma-mapping.h> 17 #include <linux/of_device.h> 18 #include <sound/pcm_params.h> 19 #include "q6asm.h" 20 #include "q6routing.h" 21 #include "q6dsp-errno.h" 22 23 #define DRV_NAME "q6asm-fe-dai" 24 25 #define PLAYBACK_MIN_NUM_PERIODS 2 26 #define PLAYBACK_MAX_NUM_PERIODS 8 27 #define PLAYBACK_MAX_PERIOD_SIZE 65536 28 #define PLAYBACK_MIN_PERIOD_SIZE 128 29 #define CAPTURE_MIN_NUM_PERIODS 2 30 #define CAPTURE_MAX_NUM_PERIODS 8 31 #define CAPTURE_MAX_PERIOD_SIZE 4096 32 #define CAPTURE_MIN_PERIOD_SIZE 320 33 #define SID_MASK_DEFAULT 0xF 34 35 /* Default values used if user space does not set */ 36 #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024) 37 #define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024) 38 #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4) 39 #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4) 40 #define Q6ASM_DAI_TX_RX 0 41 #define Q6ASM_DAI_TX 1 42 #define Q6ASM_DAI_RX 2 43 44 #define ALAC_CH_LAYOUT_MONO ((101 << 16) | 1) 45 #define ALAC_CH_LAYOUT_STEREO ((101 << 16) | 2) 46 47 enum stream_state { 48 Q6ASM_STREAM_IDLE = 0, 49 Q6ASM_STREAM_STOPPED, 50 Q6ASM_STREAM_RUNNING, 51 }; 52 53 struct q6asm_dai_rtd { 54 struct snd_pcm_substream *substream; 55 struct snd_compr_stream *cstream; 56 struct snd_compr_params codec_param; 57 struct snd_dma_buffer dma_buffer; 58 spinlock_t lock; 59 phys_addr_t phys; 60 unsigned int pcm_size; 61 unsigned int pcm_count; 62 unsigned int pcm_irq_pos; /* IRQ position */ 63 unsigned int periods; 64 unsigned int bytes_sent; 65 unsigned int bytes_received; 66 unsigned int copied_total; 67 uint16_t bits_per_sample; 68 uint16_t source; /* Encoding source bit mask */ 69 struct audio_client *audio_client; 70 uint16_t session_id; 71 enum stream_state state; 72 }; 73 74 struct q6asm_dai_data { 75 struct snd_soc_dai_driver *dais; 76 int num_dais; 77 long long int sid; 78 }; 79 80 static const struct snd_pcm_hardware q6asm_dai_hardware_capture = { 81 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH | 82 SNDRV_PCM_INFO_BLOCK_TRANSFER | 83 SNDRV_PCM_INFO_MMAP_VALID | 84 SNDRV_PCM_INFO_INTERLEAVED | 85 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), 86 .formats = (SNDRV_PCM_FMTBIT_S16_LE | 87 SNDRV_PCM_FMTBIT_S24_LE), 88 .rates = SNDRV_PCM_RATE_8000_48000, 89 .rate_min = 8000, 90 .rate_max = 48000, 91 .channels_min = 1, 92 .channels_max = 4, 93 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * 94 CAPTURE_MAX_PERIOD_SIZE, 95 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, 96 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, 97 .periods_min = CAPTURE_MIN_NUM_PERIODS, 98 .periods_max = CAPTURE_MAX_NUM_PERIODS, 99 .fifo_size = 0, 100 }; 101 102 static struct snd_pcm_hardware q6asm_dai_hardware_playback = { 103 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH | 104 SNDRV_PCM_INFO_BLOCK_TRANSFER | 105 SNDRV_PCM_INFO_MMAP_VALID | 106 SNDRV_PCM_INFO_INTERLEAVED | 107 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), 108 .formats = (SNDRV_PCM_FMTBIT_S16_LE | 109 SNDRV_PCM_FMTBIT_S24_LE), 110 .rates = SNDRV_PCM_RATE_8000_192000, 111 .rate_min = 8000, 112 .rate_max = 192000, 113 .channels_min = 1, 114 .channels_max = 8, 115 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * 116 PLAYBACK_MAX_PERIOD_SIZE), 117 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, 118 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, 119 .periods_min = PLAYBACK_MIN_NUM_PERIODS, 120 .periods_max = PLAYBACK_MAX_NUM_PERIODS, 121 .fifo_size = 0, 122 }; 123 124 #define Q6ASM_FEDAI_DRIVER(num) { \ 125 .playback = { \ 126 .stream_name = "MultiMedia"#num" Playback", \ 127 .rates = (SNDRV_PCM_RATE_8000_192000| \ 128 SNDRV_PCM_RATE_KNOT), \ 129 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \ 130 SNDRV_PCM_FMTBIT_S24_LE), \ 131 .channels_min = 1, \ 132 .channels_max = 8, \ 133 .rate_min = 8000, \ 134 .rate_max = 192000, \ 135 }, \ 136 .capture = { \ 137 .stream_name = "MultiMedia"#num" Capture", \ 138 .rates = (SNDRV_PCM_RATE_8000_48000| \ 139 SNDRV_PCM_RATE_KNOT), \ 140 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \ 141 SNDRV_PCM_FMTBIT_S24_LE), \ 142 .channels_min = 1, \ 143 .channels_max = 4, \ 144 .rate_min = 8000, \ 145 .rate_max = 48000, \ 146 }, \ 147 .name = "MultiMedia"#num, \ 148 .id = MSM_FRONTEND_DAI_MULTIMEDIA##num, \ 149 } 150 151 /* Conventional and unconventional sample rate supported */ 152 static unsigned int supported_sample_rates[] = { 153 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 154 88200, 96000, 176400, 192000 155 }; 156 157 static struct snd_pcm_hw_constraint_list constraints_sample_rates = { 158 .count = ARRAY_SIZE(supported_sample_rates), 159 .list = supported_sample_rates, 160 .mask = 0, 161 }; 162 163 static const struct snd_compr_codec_caps q6asm_compr_caps = { 164 .num_descriptors = 1, 165 .descriptor[0].max_ch = 2, 166 .descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050, 167 24000, 32000, 44100, 48000, 88200, 168 96000, 176400, 192000 }, 169 .descriptor[0].num_sample_rates = 13, 170 .descriptor[0].bit_rate[0] = 320, 171 .descriptor[0].bit_rate[1] = 128, 172 .descriptor[0].num_bitrates = 2, 173 .descriptor[0].profiles = 0, 174 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO, 175 .descriptor[0].formats = 0, 176 }; 177 178 static void event_handler(uint32_t opcode, uint32_t token, 179 uint32_t *payload, void *priv) 180 { 181 struct q6asm_dai_rtd *prtd = priv; 182 struct snd_pcm_substream *substream = prtd->substream; 183 184 switch (opcode) { 185 case ASM_CLIENT_EVENT_CMD_RUN_DONE: 186 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 187 q6asm_write_async(prtd->audio_client, 188 prtd->pcm_count, 0, 0, NO_TIMESTAMP); 189 break; 190 case ASM_CLIENT_EVENT_CMD_EOS_DONE: 191 prtd->state = Q6ASM_STREAM_STOPPED; 192 break; 193 case ASM_CLIENT_EVENT_DATA_WRITE_DONE: { 194 prtd->pcm_irq_pos += prtd->pcm_count; 195 snd_pcm_period_elapsed(substream); 196 if (prtd->state == Q6ASM_STREAM_RUNNING) 197 q6asm_write_async(prtd->audio_client, 198 prtd->pcm_count, 0, 0, NO_TIMESTAMP); 199 200 break; 201 } 202 case ASM_CLIENT_EVENT_DATA_READ_DONE: 203 prtd->pcm_irq_pos += prtd->pcm_count; 204 snd_pcm_period_elapsed(substream); 205 if (prtd->state == Q6ASM_STREAM_RUNNING) 206 q6asm_read(prtd->audio_client); 207 208 break; 209 default: 210 break; 211 } 212 } 213 214 static int q6asm_dai_prepare(struct snd_soc_component *component, 215 struct snd_pcm_substream *substream) 216 { 217 struct snd_pcm_runtime *runtime = substream->runtime; 218 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data; 219 struct q6asm_dai_rtd *prtd = runtime->private_data; 220 struct q6asm_dai_data *pdata; 221 int ret, i; 222 223 pdata = snd_soc_component_get_drvdata(component); 224 if (!pdata) 225 return -EINVAL; 226 227 if (!prtd || !prtd->audio_client) { 228 pr_err("%s: private data null or audio client freed\n", 229 __func__); 230 return -EINVAL; 231 } 232 233 prtd->pcm_count = snd_pcm_lib_period_bytes(substream); 234 prtd->pcm_irq_pos = 0; 235 /* rate and channels are sent to audio driver */ 236 if (prtd->state) { 237 /* clear the previous setup if any */ 238 q6asm_cmd(prtd->audio_client, CMD_CLOSE); 239 q6asm_unmap_memory_regions(substream->stream, 240 prtd->audio_client); 241 q6routing_stream_close(soc_prtd->dai_link->id, 242 substream->stream); 243 } 244 245 ret = q6asm_map_memory_regions(substream->stream, prtd->audio_client, 246 prtd->phys, 247 (prtd->pcm_size / prtd->periods), 248 prtd->periods); 249 250 if (ret < 0) { 251 pr_err("Audio Start: Buffer Allocation failed rc = %d\n", 252 ret); 253 return -ENOMEM; 254 } 255 256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 257 ret = q6asm_open_write(prtd->audio_client, FORMAT_LINEAR_PCM, 258 0, prtd->bits_per_sample); 259 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 260 ret = q6asm_open_read(prtd->audio_client, FORMAT_LINEAR_PCM, 261 prtd->bits_per_sample); 262 } 263 264 if (ret < 0) { 265 pr_err("%s: q6asm_open_write failed\n", __func__); 266 q6asm_audio_client_free(prtd->audio_client); 267 prtd->audio_client = NULL; 268 return -ENOMEM; 269 } 270 271 prtd->session_id = q6asm_get_session_id(prtd->audio_client); 272 ret = q6routing_stream_open(soc_prtd->dai_link->id, LEGACY_PCM_MODE, 273 prtd->session_id, substream->stream); 274 if (ret) { 275 pr_err("%s: stream reg failed ret:%d\n", __func__, ret); 276 return ret; 277 } 278 279 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 280 ret = q6asm_media_format_block_multi_ch_pcm( 281 prtd->audio_client, runtime->rate, 282 runtime->channels, NULL, 283 prtd->bits_per_sample); 284 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 285 ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client, 286 runtime->rate, runtime->channels, 287 prtd->bits_per_sample); 288 289 /* Queue the buffers */ 290 for (i = 0; i < runtime->periods; i++) 291 q6asm_read(prtd->audio_client); 292 293 } 294 if (ret < 0) 295 pr_info("%s: CMD Format block failed\n", __func__); 296 297 prtd->state = Q6ASM_STREAM_RUNNING; 298 299 return 0; 300 } 301 302 static int q6asm_dai_trigger(struct snd_soc_component *component, 303 struct snd_pcm_substream *substream, int cmd) 304 { 305 int ret = 0; 306 struct snd_pcm_runtime *runtime = substream->runtime; 307 struct q6asm_dai_rtd *prtd = runtime->private_data; 308 309 switch (cmd) { 310 case SNDRV_PCM_TRIGGER_START: 311 case SNDRV_PCM_TRIGGER_RESUME: 312 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 313 ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0); 314 break; 315 case SNDRV_PCM_TRIGGER_STOP: 316 prtd->state = Q6ASM_STREAM_STOPPED; 317 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS); 318 break; 319 case SNDRV_PCM_TRIGGER_SUSPEND: 320 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 321 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE); 322 break; 323 default: 324 ret = -EINVAL; 325 break; 326 } 327 328 return ret; 329 } 330 331 static int q6asm_dai_open(struct snd_soc_component *component, 332 struct snd_pcm_substream *substream) 333 { 334 struct snd_pcm_runtime *runtime = substream->runtime; 335 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data; 336 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0); 337 struct q6asm_dai_rtd *prtd; 338 struct q6asm_dai_data *pdata; 339 struct device *dev = component->dev; 340 int ret = 0; 341 int stream_id; 342 343 stream_id = cpu_dai->driver->id; 344 345 pdata = snd_soc_component_get_drvdata(component); 346 if (!pdata) { 347 pr_err("Drv data not found ..\n"); 348 return -EINVAL; 349 } 350 351 prtd = kzalloc(sizeof(struct q6asm_dai_rtd), GFP_KERNEL); 352 if (prtd == NULL) 353 return -ENOMEM; 354 355 prtd->substream = substream; 356 prtd->audio_client = q6asm_audio_client_alloc(dev, 357 (q6asm_cb)event_handler, prtd, stream_id, 358 LEGACY_PCM_MODE); 359 if (IS_ERR(prtd->audio_client)) { 360 pr_info("%s: Could not allocate memory\n", __func__); 361 ret = PTR_ERR(prtd->audio_client); 362 kfree(prtd); 363 return ret; 364 } 365 366 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 367 runtime->hw = q6asm_dai_hardware_playback; 368 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 369 runtime->hw = q6asm_dai_hardware_capture; 370 371 ret = snd_pcm_hw_constraint_list(runtime, 0, 372 SNDRV_PCM_HW_PARAM_RATE, 373 &constraints_sample_rates); 374 if (ret < 0) 375 pr_info("snd_pcm_hw_constraint_list failed\n"); 376 /* Ensure that buffer size is a multiple of period size */ 377 ret = snd_pcm_hw_constraint_integer(runtime, 378 SNDRV_PCM_HW_PARAM_PERIODS); 379 if (ret < 0) 380 pr_info("snd_pcm_hw_constraint_integer failed\n"); 381 382 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 383 ret = snd_pcm_hw_constraint_minmax(runtime, 384 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 385 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE, 386 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE); 387 if (ret < 0) { 388 pr_err("constraint for buffer bytes min max ret = %d\n", 389 ret); 390 } 391 } 392 393 ret = snd_pcm_hw_constraint_step(runtime, 0, 394 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); 395 if (ret < 0) { 396 pr_err("constraint for period bytes step ret = %d\n", 397 ret); 398 } 399 ret = snd_pcm_hw_constraint_step(runtime, 0, 400 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); 401 if (ret < 0) { 402 pr_err("constraint for buffer bytes step ret = %d\n", 403 ret); 404 } 405 406 runtime->private_data = prtd; 407 408 snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_playback); 409 410 runtime->dma_bytes = q6asm_dai_hardware_playback.buffer_bytes_max; 411 412 413 if (pdata->sid < 0) 414 prtd->phys = substream->dma_buffer.addr; 415 else 416 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32); 417 418 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 419 420 return 0; 421 } 422 423 static int q6asm_dai_close(struct snd_soc_component *component, 424 struct snd_pcm_substream *substream) 425 { 426 struct snd_pcm_runtime *runtime = substream->runtime; 427 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data; 428 struct q6asm_dai_rtd *prtd = runtime->private_data; 429 430 if (prtd->audio_client) { 431 if (prtd->state) 432 q6asm_cmd(prtd->audio_client, CMD_CLOSE); 433 434 q6asm_unmap_memory_regions(substream->stream, 435 prtd->audio_client); 436 q6asm_audio_client_free(prtd->audio_client); 437 prtd->audio_client = NULL; 438 } 439 q6routing_stream_close(soc_prtd->dai_link->id, 440 substream->stream); 441 kfree(prtd); 442 return 0; 443 } 444 445 static snd_pcm_uframes_t q6asm_dai_pointer(struct snd_soc_component *component, 446 struct snd_pcm_substream *substream) 447 { 448 449 struct snd_pcm_runtime *runtime = substream->runtime; 450 struct q6asm_dai_rtd *prtd = runtime->private_data; 451 452 if (prtd->pcm_irq_pos >= prtd->pcm_size) 453 prtd->pcm_irq_pos = 0; 454 455 return bytes_to_frames(runtime, (prtd->pcm_irq_pos)); 456 } 457 458 static int q6asm_dai_mmap(struct snd_soc_component *component, 459 struct snd_pcm_substream *substream, 460 struct vm_area_struct *vma) 461 { 462 struct snd_pcm_runtime *runtime = substream->runtime; 463 struct device *dev = component->dev; 464 465 return dma_mmap_coherent(dev, vma, 466 runtime->dma_area, runtime->dma_addr, 467 runtime->dma_bytes); 468 } 469 470 static int q6asm_dai_hw_params(struct snd_soc_component *component, 471 struct snd_pcm_substream *substream, 472 struct snd_pcm_hw_params *params) 473 { 474 struct snd_pcm_runtime *runtime = substream->runtime; 475 struct q6asm_dai_rtd *prtd = runtime->private_data; 476 477 prtd->pcm_size = params_buffer_bytes(params); 478 prtd->periods = params_periods(params); 479 480 switch (params_format(params)) { 481 case SNDRV_PCM_FORMAT_S16_LE: 482 prtd->bits_per_sample = 16; 483 break; 484 case SNDRV_PCM_FORMAT_S24_LE: 485 prtd->bits_per_sample = 24; 486 break; 487 } 488 489 return 0; 490 } 491 492 static void compress_event_handler(uint32_t opcode, uint32_t token, 493 uint32_t *payload, void *priv) 494 { 495 struct q6asm_dai_rtd *prtd = priv; 496 struct snd_compr_stream *substream = prtd->cstream; 497 unsigned long flags; 498 uint64_t avail; 499 500 switch (opcode) { 501 case ASM_CLIENT_EVENT_CMD_RUN_DONE: 502 spin_lock_irqsave(&prtd->lock, flags); 503 if (!prtd->bytes_sent) { 504 q6asm_write_async(prtd->audio_client, prtd->pcm_count, 505 0, 0, NO_TIMESTAMP); 506 prtd->bytes_sent += prtd->pcm_count; 507 } 508 509 spin_unlock_irqrestore(&prtd->lock, flags); 510 break; 511 512 case ASM_CLIENT_EVENT_CMD_EOS_DONE: 513 prtd->state = Q6ASM_STREAM_STOPPED; 514 break; 515 516 case ASM_CLIENT_EVENT_DATA_WRITE_DONE: 517 spin_lock_irqsave(&prtd->lock, flags); 518 519 prtd->copied_total += prtd->pcm_count; 520 snd_compr_fragment_elapsed(substream); 521 522 if (prtd->state != Q6ASM_STREAM_RUNNING) { 523 spin_unlock_irqrestore(&prtd->lock, flags); 524 break; 525 } 526 527 avail = prtd->bytes_received - prtd->bytes_sent; 528 529 if (avail >= prtd->pcm_count) { 530 q6asm_write_async(prtd->audio_client, 531 prtd->pcm_count, 0, 0, NO_TIMESTAMP); 532 prtd->bytes_sent += prtd->pcm_count; 533 } 534 535 spin_unlock_irqrestore(&prtd->lock, flags); 536 break; 537 538 default: 539 break; 540 } 541 } 542 543 static int q6asm_dai_compr_open(struct snd_compr_stream *stream) 544 { 545 struct snd_soc_pcm_runtime *rtd = stream->private_data; 546 struct snd_soc_component *c = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 547 struct snd_compr_runtime *runtime = stream->runtime; 548 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 549 struct q6asm_dai_data *pdata; 550 struct device *dev = c->dev; 551 struct q6asm_dai_rtd *prtd; 552 int stream_id, size, ret; 553 554 stream_id = cpu_dai->driver->id; 555 pdata = snd_soc_component_get_drvdata(c); 556 if (!pdata) { 557 dev_err(dev, "Drv data not found ..\n"); 558 return -EINVAL; 559 } 560 561 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); 562 if (!prtd) 563 return -ENOMEM; 564 565 prtd->cstream = stream; 566 prtd->audio_client = q6asm_audio_client_alloc(dev, 567 (q6asm_cb)compress_event_handler, 568 prtd, stream_id, LEGACY_PCM_MODE); 569 if (IS_ERR(prtd->audio_client)) { 570 dev_err(dev, "Could not allocate memory\n"); 571 ret = PTR_ERR(prtd->audio_client); 572 goto free_prtd; 573 } 574 575 size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * 576 COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; 577 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, 578 &prtd->dma_buffer); 579 if (ret) { 580 dev_err(dev, "Cannot allocate buffer(s)\n"); 581 goto free_client; 582 } 583 584 if (pdata->sid < 0) 585 prtd->phys = prtd->dma_buffer.addr; 586 else 587 prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32); 588 589 snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer); 590 spin_lock_init(&prtd->lock); 591 runtime->private_data = prtd; 592 593 return 0; 594 595 free_client: 596 q6asm_audio_client_free(prtd->audio_client); 597 free_prtd: 598 kfree(prtd); 599 600 return ret; 601 } 602 603 static int q6asm_dai_compr_free(struct snd_compr_stream *stream) 604 { 605 struct snd_compr_runtime *runtime = stream->runtime; 606 struct q6asm_dai_rtd *prtd = runtime->private_data; 607 struct snd_soc_pcm_runtime *rtd = stream->private_data; 608 609 if (prtd->audio_client) { 610 if (prtd->state) 611 q6asm_cmd(prtd->audio_client, CMD_CLOSE); 612 613 snd_dma_free_pages(&prtd->dma_buffer); 614 q6asm_unmap_memory_regions(stream->direction, 615 prtd->audio_client); 616 q6asm_audio_client_free(prtd->audio_client); 617 prtd->audio_client = NULL; 618 } 619 q6routing_stream_close(rtd->dai_link->id, stream->direction); 620 kfree(prtd); 621 622 return 0; 623 } 624 625 static int q6asm_dai_compr_set_params(struct snd_compr_stream *stream, 626 struct snd_compr_params *params) 627 { 628 struct snd_compr_runtime *runtime = stream->runtime; 629 struct q6asm_dai_rtd *prtd = runtime->private_data; 630 struct snd_soc_pcm_runtime *rtd = stream->private_data; 631 struct snd_soc_component *c = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 632 int dir = stream->direction; 633 struct q6asm_dai_data *pdata; 634 struct q6asm_flac_cfg flac_cfg; 635 struct q6asm_wma_cfg wma_cfg; 636 struct q6asm_alac_cfg alac_cfg; 637 struct q6asm_ape_cfg ape_cfg; 638 unsigned int wma_v9 = 0; 639 struct device *dev = c->dev; 640 int ret; 641 union snd_codec_options *codec_options; 642 struct snd_dec_flac *flac; 643 struct snd_dec_wma *wma; 644 struct snd_dec_alac *alac; 645 struct snd_dec_ape *ape; 646 647 codec_options = &(prtd->codec_param.codec.options); 648 649 650 memcpy(&prtd->codec_param, params, sizeof(*params)); 651 652 pdata = snd_soc_component_get_drvdata(c); 653 if (!pdata) 654 return -EINVAL; 655 656 if (!prtd || !prtd->audio_client) { 657 dev_err(dev, "private data null or audio client freed\n"); 658 return -EINVAL; 659 } 660 661 prtd->periods = runtime->fragments; 662 prtd->pcm_count = runtime->fragment_size; 663 prtd->pcm_size = runtime->fragments * runtime->fragment_size; 664 prtd->bits_per_sample = 16; 665 if (dir == SND_COMPRESS_PLAYBACK) { 666 ret = q6asm_open_write(prtd->audio_client, params->codec.id, 667 params->codec.profile, prtd->bits_per_sample); 668 669 if (ret < 0) { 670 dev_err(dev, "q6asm_open_write failed\n"); 671 q6asm_audio_client_free(prtd->audio_client); 672 prtd->audio_client = NULL; 673 return ret; 674 } 675 } 676 677 prtd->session_id = q6asm_get_session_id(prtd->audio_client); 678 ret = q6routing_stream_open(rtd->dai_link->id, LEGACY_PCM_MODE, 679 prtd->session_id, dir); 680 if (ret) { 681 dev_err(dev, "Stream reg failed ret:%d\n", ret); 682 return ret; 683 } 684 685 switch (params->codec.id) { 686 case SND_AUDIOCODEC_FLAC: 687 688 memset(&flac_cfg, 0x0, sizeof(struct q6asm_flac_cfg)); 689 flac = &codec_options->flac_d; 690 691 flac_cfg.ch_cfg = params->codec.ch_in; 692 flac_cfg.sample_rate = params->codec.sample_rate; 693 flac_cfg.stream_info_present = 1; 694 flac_cfg.sample_size = flac->sample_size; 695 flac_cfg.min_blk_size = flac->min_blk_size; 696 flac_cfg.max_blk_size = flac->max_blk_size; 697 flac_cfg.max_frame_size = flac->max_frame_size; 698 flac_cfg.min_frame_size = flac->min_frame_size; 699 700 ret = q6asm_stream_media_format_block_flac(prtd->audio_client, 701 &flac_cfg); 702 if (ret < 0) { 703 dev_err(dev, "FLAC CMD Format block failed:%d\n", ret); 704 return -EIO; 705 } 706 break; 707 708 case SND_AUDIOCODEC_WMA: 709 wma = &codec_options->wma_d; 710 711 memset(&wma_cfg, 0x0, sizeof(struct q6asm_wma_cfg)); 712 713 wma_cfg.sample_rate = params->codec.sample_rate; 714 wma_cfg.num_channels = params->codec.ch_in; 715 wma_cfg.bytes_per_sec = params->codec.bit_rate / 8; 716 wma_cfg.block_align = params->codec.align; 717 wma_cfg.bits_per_sample = prtd->bits_per_sample; 718 wma_cfg.enc_options = wma->encoder_option; 719 wma_cfg.adv_enc_options = wma->adv_encoder_option; 720 wma_cfg.adv_enc_options2 = wma->adv_encoder_option2; 721 722 if (wma_cfg.num_channels == 1) 723 wma_cfg.channel_mask = 4; /* Mono Center */ 724 else if (wma_cfg.num_channels == 2) 725 wma_cfg.channel_mask = 3; /* Stereo FL/FR */ 726 else 727 return -EINVAL; 728 729 /* check the codec profile */ 730 switch (params->codec.profile) { 731 case SND_AUDIOPROFILE_WMA9: 732 wma_cfg.fmtag = 0x161; 733 wma_v9 = 1; 734 break; 735 736 case SND_AUDIOPROFILE_WMA10: 737 wma_cfg.fmtag = 0x166; 738 break; 739 740 case SND_AUDIOPROFILE_WMA9_PRO: 741 wma_cfg.fmtag = 0x162; 742 break; 743 744 case SND_AUDIOPROFILE_WMA9_LOSSLESS: 745 wma_cfg.fmtag = 0x163; 746 break; 747 748 case SND_AUDIOPROFILE_WMA10_LOSSLESS: 749 wma_cfg.fmtag = 0x167; 750 break; 751 752 default: 753 dev_err(dev, "Unknown WMA profile:%x\n", 754 params->codec.profile); 755 return -EIO; 756 } 757 758 if (wma_v9) 759 ret = q6asm_stream_media_format_block_wma_v9( 760 prtd->audio_client, &wma_cfg); 761 else 762 ret = q6asm_stream_media_format_block_wma_v10( 763 prtd->audio_client, &wma_cfg); 764 if (ret < 0) { 765 dev_err(dev, "WMA9 CMD failed:%d\n", ret); 766 return -EIO; 767 } 768 break; 769 770 case SND_AUDIOCODEC_ALAC: 771 memset(&alac_cfg, 0x0, sizeof(alac_cfg)); 772 alac = &codec_options->alac_d; 773 774 alac_cfg.sample_rate = params->codec.sample_rate; 775 alac_cfg.avg_bit_rate = params->codec.bit_rate; 776 alac_cfg.bit_depth = prtd->bits_per_sample; 777 alac_cfg.num_channels = params->codec.ch_in; 778 779 alac_cfg.frame_length = alac->frame_length; 780 alac_cfg.pb = alac->pb; 781 alac_cfg.mb = alac->mb; 782 alac_cfg.kb = alac->kb; 783 alac_cfg.max_run = alac->max_run; 784 alac_cfg.compatible_version = alac->compatible_version; 785 alac_cfg.max_frame_bytes = alac->max_frame_bytes; 786 787 switch (params->codec.ch_in) { 788 case 1: 789 alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_MONO; 790 break; 791 case 2: 792 alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_STEREO; 793 break; 794 } 795 ret = q6asm_stream_media_format_block_alac(prtd->audio_client, 796 &alac_cfg); 797 if (ret < 0) { 798 dev_err(dev, "ALAC CMD Format block failed:%d\n", ret); 799 return -EIO; 800 } 801 break; 802 803 case SND_AUDIOCODEC_APE: 804 memset(&ape_cfg, 0x0, sizeof(ape_cfg)); 805 ape = &codec_options->ape_d; 806 807 ape_cfg.sample_rate = params->codec.sample_rate; 808 ape_cfg.num_channels = params->codec.ch_in; 809 ape_cfg.bits_per_sample = prtd->bits_per_sample; 810 811 ape_cfg.compatible_version = ape->compatible_version; 812 ape_cfg.compression_level = ape->compression_level; 813 ape_cfg.format_flags = ape->format_flags; 814 ape_cfg.blocks_per_frame = ape->blocks_per_frame; 815 ape_cfg.final_frame_blocks = ape->final_frame_blocks; 816 ape_cfg.total_frames = ape->total_frames; 817 ape_cfg.seek_table_present = ape->seek_table_present; 818 819 ret = q6asm_stream_media_format_block_ape(prtd->audio_client, 820 &ape_cfg); 821 if (ret < 0) { 822 dev_err(dev, "APE CMD Format block failed:%d\n", ret); 823 return -EIO; 824 } 825 break; 826 827 default: 828 break; 829 } 830 831 ret = q6asm_map_memory_regions(dir, prtd->audio_client, prtd->phys, 832 (prtd->pcm_size / prtd->periods), 833 prtd->periods); 834 835 if (ret < 0) { 836 dev_err(dev, "Buffer Mapping failed ret:%d\n", ret); 837 return -ENOMEM; 838 } 839 840 prtd->state = Q6ASM_STREAM_RUNNING; 841 842 return 0; 843 } 844 845 static int q6asm_dai_compr_trigger(struct snd_compr_stream *stream, int cmd) 846 { 847 struct snd_compr_runtime *runtime = stream->runtime; 848 struct q6asm_dai_rtd *prtd = runtime->private_data; 849 int ret = 0; 850 851 switch (cmd) { 852 case SNDRV_PCM_TRIGGER_START: 853 case SNDRV_PCM_TRIGGER_RESUME: 854 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 855 ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0); 856 break; 857 case SNDRV_PCM_TRIGGER_STOP: 858 prtd->state = Q6ASM_STREAM_STOPPED; 859 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS); 860 break; 861 case SNDRV_PCM_TRIGGER_SUSPEND: 862 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 863 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE); 864 break; 865 default: 866 ret = -EINVAL; 867 break; 868 } 869 870 return ret; 871 } 872 873 static int q6asm_dai_compr_pointer(struct snd_compr_stream *stream, 874 struct snd_compr_tstamp *tstamp) 875 { 876 struct snd_compr_runtime *runtime = stream->runtime; 877 struct q6asm_dai_rtd *prtd = runtime->private_data; 878 unsigned long flags; 879 880 spin_lock_irqsave(&prtd->lock, flags); 881 882 tstamp->copied_total = prtd->copied_total; 883 tstamp->byte_offset = prtd->copied_total % prtd->pcm_size; 884 885 spin_unlock_irqrestore(&prtd->lock, flags); 886 887 return 0; 888 } 889 890 static int q6asm_dai_compr_ack(struct snd_compr_stream *stream, 891 size_t count) 892 { 893 struct snd_compr_runtime *runtime = stream->runtime; 894 struct q6asm_dai_rtd *prtd = runtime->private_data; 895 unsigned long flags; 896 897 spin_lock_irqsave(&prtd->lock, flags); 898 prtd->bytes_received += count; 899 spin_unlock_irqrestore(&prtd->lock, flags); 900 901 return count; 902 } 903 904 static int q6asm_dai_compr_mmap(struct snd_compr_stream *stream, 905 struct vm_area_struct *vma) 906 { 907 struct snd_compr_runtime *runtime = stream->runtime; 908 struct q6asm_dai_rtd *prtd = runtime->private_data; 909 struct snd_soc_pcm_runtime *rtd = stream->private_data; 910 struct snd_soc_component *c = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 911 struct device *dev = c->dev; 912 913 return dma_mmap_coherent(dev, vma, 914 prtd->dma_buffer.area, prtd->dma_buffer.addr, 915 prtd->dma_buffer.bytes); 916 } 917 918 static int q6asm_dai_compr_get_caps(struct snd_compr_stream *stream, 919 struct snd_compr_caps *caps) 920 { 921 caps->direction = SND_COMPRESS_PLAYBACK; 922 caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE; 923 caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE; 924 caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS; 925 caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; 926 caps->num_codecs = 5; 927 caps->codecs[0] = SND_AUDIOCODEC_MP3; 928 caps->codecs[1] = SND_AUDIOCODEC_FLAC; 929 caps->codecs[2] = SND_AUDIOCODEC_WMA; 930 caps->codecs[3] = SND_AUDIOCODEC_ALAC; 931 caps->codecs[4] = SND_AUDIOCODEC_APE; 932 933 return 0; 934 } 935 936 static int q6asm_dai_compr_get_codec_caps(struct snd_compr_stream *stream, 937 struct snd_compr_codec_caps *codec) 938 { 939 switch (codec->codec) { 940 case SND_AUDIOCODEC_MP3: 941 *codec = q6asm_compr_caps; 942 break; 943 default: 944 break; 945 } 946 947 return 0; 948 } 949 950 static struct snd_compr_ops q6asm_dai_compr_ops = { 951 .open = q6asm_dai_compr_open, 952 .free = q6asm_dai_compr_free, 953 .set_params = q6asm_dai_compr_set_params, 954 .pointer = q6asm_dai_compr_pointer, 955 .trigger = q6asm_dai_compr_trigger, 956 .get_caps = q6asm_dai_compr_get_caps, 957 .get_codec_caps = q6asm_dai_compr_get_codec_caps, 958 .mmap = q6asm_dai_compr_mmap, 959 .ack = q6asm_dai_compr_ack, 960 }; 961 962 static int q6asm_dai_pcm_new(struct snd_soc_component *component, 963 struct snd_soc_pcm_runtime *rtd) 964 { 965 struct snd_pcm_substream *psubstream, *csubstream; 966 struct snd_pcm *pcm = rtd->pcm; 967 struct device *dev; 968 int size, ret; 969 970 dev = component->dev; 971 size = q6asm_dai_hardware_playback.buffer_bytes_max; 972 psubstream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 973 if (psubstream) { 974 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, 975 &psubstream->dma_buffer); 976 if (ret) { 977 dev_err(dev, "Cannot allocate buffer(s)\n"); 978 return ret; 979 } 980 } 981 982 csubstream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 983 if (csubstream) { 984 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, 985 &csubstream->dma_buffer); 986 if (ret) { 987 dev_err(dev, "Cannot allocate buffer(s)\n"); 988 if (psubstream) 989 snd_dma_free_pages(&psubstream->dma_buffer); 990 return ret; 991 } 992 } 993 994 return 0; 995 } 996 997 static void q6asm_dai_pcm_free(struct snd_soc_component *component, 998 struct snd_pcm *pcm) 999 { 1000 struct snd_pcm_substream *substream; 1001 int i; 1002 1003 for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) { 1004 substream = pcm->streams[i].substream; 1005 if (substream) { 1006 snd_dma_free_pages(&substream->dma_buffer); 1007 substream->dma_buffer.area = NULL; 1008 substream->dma_buffer.addr = 0; 1009 } 1010 } 1011 } 1012 1013 static const struct snd_soc_component_driver q6asm_fe_dai_component = { 1014 .name = DRV_NAME, 1015 .open = q6asm_dai_open, 1016 .hw_params = q6asm_dai_hw_params, 1017 .close = q6asm_dai_close, 1018 .prepare = q6asm_dai_prepare, 1019 .trigger = q6asm_dai_trigger, 1020 .pointer = q6asm_dai_pointer, 1021 .mmap = q6asm_dai_mmap, 1022 .pcm_construct = q6asm_dai_pcm_new, 1023 .pcm_destruct = q6asm_dai_pcm_free, 1024 .compr_ops = &q6asm_dai_compr_ops, 1025 }; 1026 1027 static struct snd_soc_dai_driver q6asm_fe_dais_template[] = { 1028 Q6ASM_FEDAI_DRIVER(1), 1029 Q6ASM_FEDAI_DRIVER(2), 1030 Q6ASM_FEDAI_DRIVER(3), 1031 Q6ASM_FEDAI_DRIVER(4), 1032 Q6ASM_FEDAI_DRIVER(5), 1033 Q6ASM_FEDAI_DRIVER(6), 1034 Q6ASM_FEDAI_DRIVER(7), 1035 Q6ASM_FEDAI_DRIVER(8), 1036 }; 1037 1038 static int of_q6asm_parse_dai_data(struct device *dev, 1039 struct q6asm_dai_data *pdata) 1040 { 1041 struct snd_soc_dai_driver *dai_drv; 1042 struct snd_soc_pcm_stream empty_stream; 1043 struct device_node *node; 1044 int ret, id, dir, idx = 0; 1045 1046 1047 pdata->num_dais = of_get_child_count(dev->of_node); 1048 if (!pdata->num_dais) { 1049 dev_err(dev, "No dais found in DT\n"); 1050 return -EINVAL; 1051 } 1052 1053 pdata->dais = devm_kcalloc(dev, pdata->num_dais, sizeof(*dai_drv), 1054 GFP_KERNEL); 1055 if (!pdata->dais) 1056 return -ENOMEM; 1057 1058 memset(&empty_stream, 0, sizeof(empty_stream)); 1059 1060 for_each_child_of_node(dev->of_node, node) { 1061 ret = of_property_read_u32(node, "reg", &id); 1062 if (ret || id >= MAX_SESSIONS || id < 0) { 1063 dev_err(dev, "valid dai id not found:%d\n", ret); 1064 continue; 1065 } 1066 1067 dai_drv = &pdata->dais[idx++]; 1068 *dai_drv = q6asm_fe_dais_template[id]; 1069 1070 ret = of_property_read_u32(node, "direction", &dir); 1071 if (ret) 1072 continue; 1073 1074 if (dir == Q6ASM_DAI_RX) 1075 dai_drv->capture = empty_stream; 1076 else if (dir == Q6ASM_DAI_TX) 1077 dai_drv->playback = empty_stream; 1078 1079 if (of_property_read_bool(node, "is-compress-dai")) 1080 dai_drv->compress_new = snd_soc_new_compress; 1081 } 1082 1083 return 0; 1084 } 1085 1086 static int q6asm_dai_probe(struct platform_device *pdev) 1087 { 1088 struct device *dev = &pdev->dev; 1089 struct device_node *node = dev->of_node; 1090 struct of_phandle_args args; 1091 struct q6asm_dai_data *pdata; 1092 int rc; 1093 1094 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 1095 if (!pdata) 1096 return -ENOMEM; 1097 1098 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args); 1099 if (rc < 0) 1100 pdata->sid = -1; 1101 else 1102 pdata->sid = args.args[0] & SID_MASK_DEFAULT; 1103 1104 dev_set_drvdata(dev, pdata); 1105 1106 rc = of_q6asm_parse_dai_data(dev, pdata); 1107 if (rc) 1108 return rc; 1109 1110 return devm_snd_soc_register_component(dev, &q6asm_fe_dai_component, 1111 pdata->dais, pdata->num_dais); 1112 } 1113 1114 static const struct of_device_id q6asm_dai_device_id[] = { 1115 { .compatible = "qcom,q6asm-dais" }, 1116 {}, 1117 }; 1118 MODULE_DEVICE_TABLE(of, q6asm_dai_device_id); 1119 1120 static struct platform_driver q6asm_dai_platform_driver = { 1121 .driver = { 1122 .name = "q6asm-dai", 1123 .of_match_table = of_match_ptr(q6asm_dai_device_id), 1124 }, 1125 .probe = q6asm_dai_probe, 1126 }; 1127 module_platform_driver(q6asm_dai_platform_driver); 1128 1129 MODULE_DESCRIPTION("Q6ASM dai driver"); 1130 MODULE_LICENSE("GPL v2"); 1131