1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2020 Intel Corporation 4 // 5 // Author: Cezary Rojewski <cezary.rojewski@intel.com> 6 // 7 8 #include <linux/pm_runtime.h> 9 #include <sound/soc.h> 10 #include <sound/pcm_params.h> 11 #include <uapi/sound/tlv.h> 12 #include "core.h" 13 #include "messages.h" 14 15 struct catpt_stream_template { 16 enum catpt_path_id path_id; 17 enum catpt_stream_type type; 18 u32 persistent_size; 19 u8 num_entries; 20 struct catpt_module_entry entries[]; 21 }; 22 23 static struct catpt_stream_template system_pb = { 24 .path_id = CATPT_PATH_SSP0_OUT, 25 .type = CATPT_STRM_TYPE_SYSTEM, 26 .num_entries = 1, 27 .entries = {{ CATPT_MODID_PCM_SYSTEM, 0 }}, 28 }; 29 30 static struct catpt_stream_template system_cp = { 31 .path_id = CATPT_PATH_SSP0_IN, 32 .type = CATPT_STRM_TYPE_CAPTURE, 33 .num_entries = 1, 34 .entries = {{ CATPT_MODID_PCM_CAPTURE, 0 }}, 35 }; 36 37 static struct catpt_stream_template offload_pb = { 38 .path_id = CATPT_PATH_SSP0_OUT, 39 .type = CATPT_STRM_TYPE_RENDER, 40 .num_entries = 1, 41 .entries = {{ CATPT_MODID_PCM, 0 }}, 42 }; 43 44 static struct catpt_stream_template loopback_cp = { 45 .path_id = CATPT_PATH_SSP0_OUT, 46 .type = CATPT_STRM_TYPE_LOOPBACK, 47 .num_entries = 1, 48 .entries = {{ CATPT_MODID_PCM_REFERENCE, 0 }}, 49 }; 50 51 static struct catpt_stream_template bluetooth_pb = { 52 .path_id = CATPT_PATH_SSP1_OUT, 53 .type = CATPT_STRM_TYPE_BLUETOOTH_RENDER, 54 .num_entries = 1, 55 .entries = {{ CATPT_MODID_BLUETOOTH_RENDER, 0 }}, 56 }; 57 58 static struct catpt_stream_template bluetooth_cp = { 59 .path_id = CATPT_PATH_SSP1_IN, 60 .type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE, 61 .num_entries = 1, 62 .entries = {{ CATPT_MODID_BLUETOOTH_CAPTURE, 0 }}, 63 }; 64 65 static struct catpt_stream_template *catpt_topology[] = { 66 [CATPT_STRM_TYPE_RENDER] = &offload_pb, 67 [CATPT_STRM_TYPE_SYSTEM] = &system_pb, 68 [CATPT_STRM_TYPE_CAPTURE] = &system_cp, 69 [CATPT_STRM_TYPE_LOOPBACK] = &loopback_cp, 70 [CATPT_STRM_TYPE_BLUETOOTH_RENDER] = &bluetooth_pb, 71 [CATPT_STRM_TYPE_BLUETOOTH_CAPTURE] = &bluetooth_cp, 72 }; 73 74 static struct catpt_stream_template * 75 catpt_get_stream_template(struct snd_pcm_substream *substream) 76 { 77 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream); 78 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0); 79 enum catpt_stream_type type; 80 81 type = cpu_dai->driver->id; 82 83 /* account for capture in bidirectional dais */ 84 switch (type) { 85 case CATPT_STRM_TYPE_SYSTEM: 86 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 87 type = CATPT_STRM_TYPE_CAPTURE; 88 break; 89 case CATPT_STRM_TYPE_BLUETOOTH_RENDER: 90 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 91 type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE; 92 break; 93 default: 94 break; 95 } 96 97 return catpt_topology[type]; 98 } 99 100 struct catpt_stream_runtime * 101 catpt_stream_find(struct catpt_dev *cdev, u8 stream_hw_id) 102 { 103 struct catpt_stream_runtime *pos, *result = NULL; 104 105 spin_lock(&cdev->list_lock); 106 list_for_each_entry(pos, &cdev->stream_list, node) { 107 if (pos->info.stream_hw_id == stream_hw_id) { 108 result = pos; 109 break; 110 } 111 } 112 113 spin_unlock(&cdev->list_lock); 114 return result; 115 } 116 117 static u32 catpt_stream_read_position(struct catpt_dev *cdev, 118 struct catpt_stream_runtime *stream) 119 { 120 u32 pos; 121 122 memcpy_fromio(&pos, cdev->lpe_ba + stream->info.read_pos_regaddr, 123 sizeof(pos)); 124 return pos; 125 } 126 127 static u32 catpt_stream_volume(struct catpt_dev *cdev, 128 struct catpt_stream_runtime *stream, u32 channel) 129 { 130 u32 volume, offset; 131 132 if (channel >= CATPT_CHANNELS_MAX) 133 channel = 0; 134 135 offset = stream->info.volume_regaddr[channel]; 136 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume)); 137 return volume; 138 } 139 140 static u32 catpt_mixer_volume(struct catpt_dev *cdev, 141 struct catpt_mixer_stream_info *info, u32 channel) 142 { 143 u32 volume, offset; 144 145 if (channel >= CATPT_CHANNELS_MAX) 146 channel = 0; 147 148 offset = info->volume_regaddr[channel]; 149 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume)); 150 return volume; 151 } 152 153 static void catpt_arrange_page_table(struct snd_pcm_substream *substream, 154 struct snd_dma_buffer *pgtbl) 155 { 156 struct snd_pcm_runtime *rtm = substream->runtime; 157 struct snd_dma_buffer *databuf = snd_pcm_get_dma_buf(substream); 158 int i, pages; 159 160 pages = snd_sgbuf_aligned_pages(rtm->dma_bytes); 161 162 for (i = 0; i < pages; i++) { 163 u32 pfn, offset; 164 u32 *page_table; 165 166 pfn = PFN_DOWN(snd_sgbuf_get_addr(databuf, i * PAGE_SIZE)); 167 /* incrementing by 2 on even and 3 on odd */ 168 offset = ((i << 2) + i) >> 1; 169 page_table = (u32 *)(pgtbl->area + offset); 170 171 if (i & 1) 172 *page_table |= (pfn << 4); 173 else 174 *page_table |= pfn; 175 } 176 } 177 178 static u32 catpt_get_channel_map(enum catpt_channel_config config) 179 { 180 switch (config) { 181 case CATPT_CHANNEL_CONFIG_MONO: 182 return GENMASK(31, 4) | CATPT_CHANNEL_CENTER; 183 184 case CATPT_CHANNEL_CONFIG_STEREO: 185 return GENMASK(31, 8) | CATPT_CHANNEL_LEFT 186 | (CATPT_CHANNEL_RIGHT << 4); 187 188 case CATPT_CHANNEL_CONFIG_2_POINT_1: 189 return GENMASK(31, 12) | CATPT_CHANNEL_LEFT 190 | (CATPT_CHANNEL_RIGHT << 4) 191 | (CATPT_CHANNEL_LFE << 8); 192 193 case CATPT_CHANNEL_CONFIG_3_POINT_0: 194 return GENMASK(31, 12) | CATPT_CHANNEL_LEFT 195 | (CATPT_CHANNEL_CENTER << 4) 196 | (CATPT_CHANNEL_RIGHT << 8); 197 198 case CATPT_CHANNEL_CONFIG_3_POINT_1: 199 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT 200 | (CATPT_CHANNEL_CENTER << 4) 201 | (CATPT_CHANNEL_RIGHT << 8) 202 | (CATPT_CHANNEL_LFE << 12); 203 204 case CATPT_CHANNEL_CONFIG_QUATRO: 205 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT 206 | (CATPT_CHANNEL_RIGHT << 4) 207 | (CATPT_CHANNEL_LEFT_SURROUND << 8) 208 | (CATPT_CHANNEL_RIGHT_SURROUND << 12); 209 210 case CATPT_CHANNEL_CONFIG_4_POINT_0: 211 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT 212 | (CATPT_CHANNEL_CENTER << 4) 213 | (CATPT_CHANNEL_RIGHT << 8) 214 | (CATPT_CHANNEL_CENTER_SURROUND << 12); 215 216 case CATPT_CHANNEL_CONFIG_5_POINT_0: 217 return GENMASK(31, 20) | CATPT_CHANNEL_LEFT 218 | (CATPT_CHANNEL_CENTER << 4) 219 | (CATPT_CHANNEL_RIGHT << 8) 220 | (CATPT_CHANNEL_LEFT_SURROUND << 12) 221 | (CATPT_CHANNEL_RIGHT_SURROUND << 16); 222 223 case CATPT_CHANNEL_CONFIG_5_POINT_1: 224 return GENMASK(31, 24) | CATPT_CHANNEL_CENTER 225 | (CATPT_CHANNEL_LEFT << 4) 226 | (CATPT_CHANNEL_RIGHT << 8) 227 | (CATPT_CHANNEL_LEFT_SURROUND << 12) 228 | (CATPT_CHANNEL_RIGHT_SURROUND << 16) 229 | (CATPT_CHANNEL_LFE << 20); 230 231 case CATPT_CHANNEL_CONFIG_DUAL_MONO: 232 return GENMASK(31, 8) | CATPT_CHANNEL_LEFT 233 | (CATPT_CHANNEL_LEFT << 4); 234 235 default: 236 return U32_MAX; 237 } 238 } 239 240 static enum catpt_channel_config catpt_get_channel_config(u32 num_channels) 241 { 242 switch (num_channels) { 243 case 6: 244 return CATPT_CHANNEL_CONFIG_5_POINT_1; 245 case 5: 246 return CATPT_CHANNEL_CONFIG_5_POINT_0; 247 case 4: 248 return CATPT_CHANNEL_CONFIG_QUATRO; 249 case 3: 250 return CATPT_CHANNEL_CONFIG_2_POINT_1; 251 case 1: 252 return CATPT_CHANNEL_CONFIG_MONO; 253 case 2: 254 default: 255 return CATPT_CHANNEL_CONFIG_STEREO; 256 } 257 } 258 259 static int catpt_dai_startup(struct snd_pcm_substream *substream, 260 struct snd_soc_dai *dai) 261 { 262 struct catpt_stream_template *template; 263 struct catpt_stream_runtime *stream; 264 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 265 struct resource *res; 266 int ret; 267 268 template = catpt_get_stream_template(substream); 269 270 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 271 if (!stream) 272 return -ENOMEM; 273 274 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, cdev->dev, PAGE_SIZE, 275 &stream->pgtbl); 276 if (ret) 277 goto err_pgtbl; 278 279 res = catpt_request_region(&cdev->dram, template->persistent_size); 280 if (!res) { 281 ret = -EBUSY; 282 goto err_request; 283 } 284 285 catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask); 286 287 stream->template = template; 288 stream->persistent = res; 289 stream->substream = substream; 290 INIT_LIST_HEAD(&stream->node); 291 snd_soc_dai_set_dma_data(dai, substream, stream); 292 293 spin_lock(&cdev->list_lock); 294 list_add_tail(&stream->node, &cdev->stream_list); 295 spin_unlock(&cdev->list_lock); 296 297 return 0; 298 299 err_request: 300 snd_dma_free_pages(&stream->pgtbl); 301 err_pgtbl: 302 kfree(stream); 303 return ret; 304 } 305 306 static void catpt_dai_shutdown(struct snd_pcm_substream *substream, 307 struct snd_soc_dai *dai) 308 { 309 struct catpt_stream_runtime *stream; 310 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 311 312 stream = snd_soc_dai_get_dma_data(dai, substream); 313 314 spin_lock(&cdev->list_lock); 315 list_del(&stream->node); 316 spin_unlock(&cdev->list_lock); 317 318 release_resource(stream->persistent); 319 kfree(stream->persistent); 320 catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask); 321 322 snd_dma_free_pages(&stream->pgtbl); 323 kfree(stream); 324 snd_soc_dai_set_dma_data(dai, substream, NULL); 325 } 326 327 static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol); 328 329 static int catpt_dai_apply_usettings(struct snd_soc_dai *dai, 330 struct catpt_stream_runtime *stream) 331 { 332 struct snd_soc_component *component = dai->component; 333 struct snd_kcontrol *pos; 334 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 335 const char *name; 336 int ret; 337 u32 id = stream->info.stream_hw_id; 338 339 /* only selected streams have individual controls */ 340 switch (id) { 341 case CATPT_PIN_ID_OFFLOAD1: 342 name = "Media0 Playback Volume"; 343 break; 344 case CATPT_PIN_ID_OFFLOAD2: 345 name = "Media1 Playback Volume"; 346 break; 347 case CATPT_PIN_ID_CAPTURE1: 348 name = "Mic Capture Volume"; 349 break; 350 case CATPT_PIN_ID_REFERENCE: 351 name = "Loopback Mute"; 352 break; 353 default: 354 return 0; 355 } 356 357 list_for_each_entry(pos, &component->card->snd_card->controls, list) { 358 if (pos->private_data == component && 359 !strncmp(name, pos->id.name, sizeof(pos->id.name))) 360 break; 361 } 362 if (list_entry_is_head(pos, &component->card->snd_card->controls, list)) 363 return -ENOENT; 364 365 if (stream->template->type != CATPT_STRM_TYPE_LOOPBACK) 366 return catpt_set_dspvol(cdev, id, (long *)pos->private_value); 367 ret = catpt_ipc_mute_loopback(cdev, id, *(bool *)pos->private_value); 368 if (ret) 369 return CATPT_IPC_ERROR(ret); 370 return 0; 371 } 372 373 static int catpt_dai_hw_params(struct snd_pcm_substream *substream, 374 struct snd_pcm_hw_params *params, 375 struct snd_soc_dai *dai) 376 { 377 struct snd_pcm_runtime *rtm = substream->runtime; 378 struct snd_dma_buffer *dmab; 379 struct catpt_stream_runtime *stream; 380 struct catpt_audio_format afmt; 381 struct catpt_ring_info rinfo; 382 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 383 int ret; 384 385 stream = snd_soc_dai_get_dma_data(dai, substream); 386 if (stream->allocated) 387 return 0; 388 389 memset(&afmt, 0, sizeof(afmt)); 390 afmt.sample_rate = params_rate(params); 391 afmt.bit_depth = params_physical_width(params); 392 afmt.valid_bit_depth = params_width(params); 393 afmt.num_channels = params_channels(params); 394 afmt.channel_config = catpt_get_channel_config(afmt.num_channels); 395 afmt.channel_map = catpt_get_channel_map(afmt.channel_config); 396 afmt.interleaving = CATPT_INTERLEAVING_PER_CHANNEL; 397 398 dmab = snd_pcm_get_dma_buf(substream); 399 catpt_arrange_page_table(substream, &stream->pgtbl); 400 401 memset(&rinfo, 0, sizeof(rinfo)); 402 rinfo.page_table_addr = stream->pgtbl.addr; 403 rinfo.num_pages = DIV_ROUND_UP(rtm->dma_bytes, PAGE_SIZE); 404 rinfo.size = rtm->dma_bytes; 405 rinfo.offset = 0; 406 rinfo.ring_first_page_pfn = PFN_DOWN(snd_sgbuf_get_addr(dmab, 0)); 407 408 ret = catpt_ipc_alloc_stream(cdev, stream->template->path_id, 409 stream->template->type, 410 &afmt, &rinfo, 411 stream->template->num_entries, 412 stream->template->entries, 413 stream->persistent, 414 cdev->scratch, 415 &stream->info); 416 if (ret) 417 return CATPT_IPC_ERROR(ret); 418 419 ret = catpt_dai_apply_usettings(dai, stream); 420 if (ret) 421 return ret; 422 423 stream->allocated = true; 424 return 0; 425 } 426 427 static int catpt_dai_hw_free(struct snd_pcm_substream *substream, 428 struct snd_soc_dai *dai) 429 { 430 struct catpt_stream_runtime *stream; 431 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 432 433 stream = snd_soc_dai_get_dma_data(dai, substream); 434 if (!stream->allocated) 435 return 0; 436 437 catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id); 438 catpt_ipc_free_stream(cdev, stream->info.stream_hw_id); 439 440 stream->allocated = false; 441 return 0; 442 } 443 444 static int catpt_dai_prepare(struct snd_pcm_substream *substream, 445 struct snd_soc_dai *dai) 446 { 447 struct catpt_stream_runtime *stream; 448 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 449 int ret; 450 451 stream = snd_soc_dai_get_dma_data(dai, substream); 452 if (stream->prepared) 453 return 0; 454 455 ret = catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id); 456 if (ret) 457 return CATPT_IPC_ERROR(ret); 458 459 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id); 460 if (ret) 461 return CATPT_IPC_ERROR(ret); 462 463 stream->prepared = true; 464 return 0; 465 } 466 467 static int catpt_dai_trigger(struct snd_pcm_substream *substream, int cmd, 468 struct snd_soc_dai *dai) 469 { 470 struct snd_pcm_runtime *runtime = substream->runtime; 471 struct catpt_stream_runtime *stream; 472 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 473 snd_pcm_uframes_t pos; 474 int ret; 475 476 stream = snd_soc_dai_get_dma_data(dai, substream); 477 478 switch (cmd) { 479 case SNDRV_PCM_TRIGGER_START: 480 /* only offload is set_write_pos driven */ 481 if (stream->template->type != CATPT_STRM_TYPE_RENDER) 482 goto resume_stream; 483 484 pos = frames_to_bytes(runtime, runtime->start_threshold); 485 /* 486 * Dsp operates on buffer halves, thus max 2x set_write_pos 487 * (entire buffer filled) prior to stream start. 488 */ 489 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id, 490 pos, false, false); 491 if (ret) 492 return CATPT_IPC_ERROR(ret); 493 fallthrough; 494 case SNDRV_PCM_TRIGGER_RESUME: 495 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 496 resume_stream: 497 catpt_dsp_update_lpclock(cdev); 498 ret = catpt_ipc_resume_stream(cdev, stream->info.stream_hw_id); 499 if (ret) 500 return CATPT_IPC_ERROR(ret); 501 break; 502 503 case SNDRV_PCM_TRIGGER_STOP: 504 stream->prepared = false; 505 fallthrough; 506 case SNDRV_PCM_TRIGGER_SUSPEND: 507 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 508 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id); 509 catpt_dsp_update_lpclock(cdev); 510 if (ret) 511 return CATPT_IPC_ERROR(ret); 512 break; 513 514 default: 515 break; 516 } 517 518 return 0; 519 } 520 521 void catpt_stream_update_position(struct catpt_dev *cdev, 522 struct catpt_stream_runtime *stream, 523 struct catpt_notify_position *pos) 524 { 525 struct snd_pcm_substream *substream = stream->substream; 526 struct snd_pcm_runtime *r = substream->runtime; 527 snd_pcm_uframes_t dsppos, newpos; 528 int ret; 529 530 dsppos = bytes_to_frames(r, pos->stream_position); 531 532 if (!stream->prepared) 533 goto exit; 534 /* only offload is set_write_pos driven */ 535 if (stream->template->type != CATPT_STRM_TYPE_RENDER) 536 goto exit; 537 538 if (dsppos >= r->buffer_size / 2) 539 newpos = r->buffer_size / 2; 540 else 541 newpos = 0; 542 /* 543 * Dsp operates on buffer halves, thus on every notify position 544 * (buffer half consumed) update wp to allow stream progression. 545 */ 546 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id, 547 frames_to_bytes(r, newpos), 548 false, false); 549 if (ret) { 550 dev_err(cdev->dev, "update position for stream %d failed: %d\n", 551 stream->info.stream_hw_id, ret); 552 return; 553 } 554 exit: 555 snd_pcm_period_elapsed(substream); 556 } 557 558 /* 200 ms for 2 32-bit channels at 48kHz (native format) */ 559 #define CATPT_BUFFER_MAX_SIZE 76800 560 #define CATPT_PCM_PERIODS_MAX 4 561 #define CATPT_PCM_PERIODS_MIN 2 562 563 static const struct snd_pcm_hardware catpt_pcm_hardware = { 564 .info = SNDRV_PCM_INFO_MMAP | 565 SNDRV_PCM_INFO_MMAP_VALID | 566 SNDRV_PCM_INFO_INTERLEAVED | 567 SNDRV_PCM_INFO_PAUSE | 568 SNDRV_PCM_INFO_RESUME | 569 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, 570 .formats = SNDRV_PCM_FMTBIT_S16_LE | 571 SNDRV_PCM_FMTBIT_S24_LE | 572 SNDRV_PCM_FMTBIT_S32_LE, 573 .period_bytes_min = PAGE_SIZE, 574 .period_bytes_max = CATPT_BUFFER_MAX_SIZE / CATPT_PCM_PERIODS_MIN, 575 .periods_min = CATPT_PCM_PERIODS_MIN, 576 .periods_max = CATPT_PCM_PERIODS_MAX, 577 .buffer_bytes_max = CATPT_BUFFER_MAX_SIZE, 578 }; 579 580 static int catpt_component_pcm_construct(struct snd_soc_component *component, 581 struct snd_soc_pcm_runtime *rtm) 582 { 583 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 584 585 snd_pcm_set_managed_buffer_all(rtm->pcm, SNDRV_DMA_TYPE_DEV_SG, 586 cdev->dev, 587 catpt_pcm_hardware.buffer_bytes_max, 588 catpt_pcm_hardware.buffer_bytes_max); 589 590 return 0; 591 } 592 593 static int catpt_component_open(struct snd_soc_component *component, 594 struct snd_pcm_substream *substream) 595 { 596 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream); 597 598 if (!rtm->dai_link->no_pcm) 599 snd_soc_set_runtime_hwparams(substream, &catpt_pcm_hardware); 600 return 0; 601 } 602 603 static snd_pcm_uframes_t 604 catpt_component_pointer(struct snd_soc_component *component, 605 struct snd_pcm_substream *substream) 606 { 607 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream); 608 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0); 609 struct catpt_stream_runtime *stream; 610 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 611 u32 pos; 612 613 if (rtm->dai_link->no_pcm) 614 return 0; 615 616 stream = snd_soc_dai_get_dma_data(cpu_dai, substream); 617 pos = catpt_stream_read_position(cdev, stream); 618 619 return bytes_to_frames(substream->runtime, pos); 620 } 621 622 static const struct snd_soc_dai_ops catpt_fe_dai_ops = { 623 .startup = catpt_dai_startup, 624 .shutdown = catpt_dai_shutdown, 625 .hw_params = catpt_dai_hw_params, 626 .hw_free = catpt_dai_hw_free, 627 .prepare = catpt_dai_prepare, 628 .trigger = catpt_dai_trigger, 629 }; 630 631 static int catpt_dai_pcm_new(struct snd_soc_pcm_runtime *rtm, 632 struct snd_soc_dai *dai) 633 { 634 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtm, 0); 635 struct catpt_ssp_device_format devfmt; 636 struct catpt_dev *cdev = dev_get_drvdata(dai->dev); 637 int ret; 638 639 devfmt.iface = dai->driver->id; 640 devfmt.channels = codec_dai->driver->capture.channels_max; 641 642 switch (devfmt.iface) { 643 case CATPT_SSP_IFACE_0: 644 devfmt.mclk = CATPT_MCLK_FREQ_24_MHZ; 645 646 switch (devfmt.channels) { 647 case 4: 648 devfmt.mode = CATPT_SSP_MODE_TDM_PROVIDER; 649 devfmt.clock_divider = 4; 650 break; 651 case 2: 652 default: 653 devfmt.mode = CATPT_SSP_MODE_I2S_PROVIDER; 654 devfmt.clock_divider = 9; 655 break; 656 } 657 break; 658 659 case CATPT_SSP_IFACE_1: 660 devfmt.mclk = CATPT_MCLK_OFF; 661 devfmt.mode = CATPT_SSP_MODE_I2S_CONSUMER; 662 devfmt.clock_divider = 0; 663 break; 664 } 665 666 /* see if this is a new configuration */ 667 if (!memcmp(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt))) 668 return 0; 669 670 ret = pm_runtime_resume_and_get(cdev->dev); 671 if (ret < 0 && ret != -EACCES) 672 return ret; 673 674 ret = catpt_ipc_set_device_format(cdev, &devfmt); 675 676 pm_runtime_put_autosuspend(cdev->dev); 677 678 if (ret) 679 return CATPT_IPC_ERROR(ret); 680 681 /* store device format set for given SSP */ 682 memcpy(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt)); 683 return 0; 684 } 685 686 static const struct snd_soc_dai_ops catpt_dai_ops = { 687 .pcm_new = catpt_dai_pcm_new, 688 }; 689 690 static struct snd_soc_dai_driver dai_drivers[] = { 691 /* FE DAIs */ 692 { 693 .name = "System Pin", 694 .id = CATPT_STRM_TYPE_SYSTEM, 695 .ops = &catpt_fe_dai_ops, 696 .playback = { 697 .stream_name = "System Playback", 698 .channels_min = 2, 699 .channels_max = 2, 700 .rates = SNDRV_PCM_RATE_48000, 701 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 702 }, 703 .capture = { 704 .stream_name = "Analog Capture", 705 .channels_min = 2, 706 .channels_max = 4, 707 .rates = SNDRV_PCM_RATE_48000, 708 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 709 }, 710 }, 711 { 712 .name = "Offload0 Pin", 713 .id = CATPT_STRM_TYPE_RENDER, 714 .ops = &catpt_fe_dai_ops, 715 .playback = { 716 .stream_name = "Offload0 Playback", 717 .channels_min = 2, 718 .channels_max = 2, 719 .rates = SNDRV_PCM_RATE_8000_192000, 720 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 721 }, 722 }, 723 { 724 .name = "Offload1 Pin", 725 .id = CATPT_STRM_TYPE_RENDER, 726 .ops = &catpt_fe_dai_ops, 727 .playback = { 728 .stream_name = "Offload1 Playback", 729 .channels_min = 2, 730 .channels_max = 2, 731 .rates = SNDRV_PCM_RATE_8000_192000, 732 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 733 }, 734 }, 735 { 736 .name = "Loopback Pin", 737 .id = CATPT_STRM_TYPE_LOOPBACK, 738 .ops = &catpt_fe_dai_ops, 739 .capture = { 740 .stream_name = "Loopback Capture", 741 .channels_min = 2, 742 .channels_max = 2, 743 .rates = SNDRV_PCM_RATE_48000, 744 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 745 }, 746 }, 747 { 748 .name = "Bluetooth Pin", 749 .id = CATPT_STRM_TYPE_BLUETOOTH_RENDER, 750 .ops = &catpt_fe_dai_ops, 751 .playback = { 752 .stream_name = "Bluetooth Playback", 753 .channels_min = 1, 754 .channels_max = 1, 755 .rates = SNDRV_PCM_RATE_8000, 756 .formats = SNDRV_PCM_FMTBIT_S16_LE, 757 }, 758 .capture = { 759 .stream_name = "Bluetooth Capture", 760 .channels_min = 1, 761 .channels_max = 1, 762 .rates = SNDRV_PCM_RATE_8000, 763 .formats = SNDRV_PCM_FMTBIT_S16_LE, 764 }, 765 }, 766 /* BE DAIs */ 767 { 768 .name = "ssp0-port", 769 .id = CATPT_SSP_IFACE_0, 770 .playback = { 771 .channels_min = 1, 772 .channels_max = 8, 773 }, 774 .capture = { 775 .channels_min = 1, 776 .channels_max = 8, 777 }, 778 .ops = &catpt_dai_ops, 779 }, 780 { 781 .name = "ssp1-port", 782 .id = CATPT_SSP_IFACE_1, 783 .playback = { 784 .channels_min = 1, 785 .channels_max = 8, 786 }, 787 .capture = { 788 .channels_min = 1, 789 .channels_max = 8, 790 }, 791 .ops = &catpt_dai_ops, 792 }, 793 }; 794 795 #define DSP_VOLUME_MAX S32_MAX /* 0db */ 796 #define DSP_VOLUME_STEP_MAX 30 797 798 static u32 ctlvol_to_dspvol(u32 value) 799 { 800 if (value > DSP_VOLUME_STEP_MAX) 801 value = 0; 802 return DSP_VOLUME_MAX >> (DSP_VOLUME_STEP_MAX - value); 803 } 804 805 static u32 dspvol_to_ctlvol(u32 volume) 806 { 807 if (volume > DSP_VOLUME_MAX) 808 return DSP_VOLUME_STEP_MAX; 809 return volume ? __fls(volume) : 0; 810 } 811 812 static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol) 813 { 814 u32 dspvol; 815 int ret, i; 816 817 for (i = 1; i < CATPT_CHANNELS_MAX; i++) 818 if (ctlvol[i] != ctlvol[0]) 819 break; 820 821 if (i == CATPT_CHANNELS_MAX) { 822 dspvol = ctlvol_to_dspvol(ctlvol[0]); 823 824 ret = catpt_ipc_set_volume(cdev, stream_id, 825 CATPT_ALL_CHANNELS_MASK, dspvol, 826 0, CATPT_AUDIO_CURVE_NONE); 827 } else { 828 for (i = 0; i < CATPT_CHANNELS_MAX; i++) { 829 dspvol = ctlvol_to_dspvol(ctlvol[i]); 830 831 ret = catpt_ipc_set_volume(cdev, stream_id, 832 i, dspvol, 833 0, CATPT_AUDIO_CURVE_NONE); 834 if (ret) 835 break; 836 } 837 } 838 839 if (ret) 840 return CATPT_IPC_ERROR(ret); 841 return 0; 842 } 843 844 static int catpt_volume_info(struct snd_kcontrol *kcontrol, 845 struct snd_ctl_elem_info *uinfo) 846 { 847 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 848 uinfo->count = CATPT_CHANNELS_MAX; 849 uinfo->value.integer.min = 0; 850 uinfo->value.integer.max = DSP_VOLUME_STEP_MAX; 851 return 0; 852 } 853 854 static int catpt_mixer_volume_get(struct snd_kcontrol *kcontrol, 855 struct snd_ctl_elem_value *ucontrol) 856 { 857 struct snd_soc_component *component = 858 snd_soc_kcontrol_component(kcontrol); 859 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 860 u32 dspvol; 861 int ret; 862 int i; 863 864 ret = pm_runtime_resume_and_get(cdev->dev); 865 if (ret < 0 && ret != -EACCES) 866 return ret; 867 868 for (i = 0; i < CATPT_CHANNELS_MAX; i++) { 869 dspvol = catpt_mixer_volume(cdev, &cdev->mixer, i); 870 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol); 871 } 872 873 pm_runtime_put_autosuspend(cdev->dev); 874 875 return 0; 876 } 877 878 static int catpt_mixer_volume_put(struct snd_kcontrol *kcontrol, 879 struct snd_ctl_elem_value *ucontrol) 880 { 881 struct snd_soc_component *component = 882 snd_soc_kcontrol_component(kcontrol); 883 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 884 int ret; 885 886 ret = pm_runtime_resume_and_get(cdev->dev); 887 if (ret < 0 && ret != -EACCES) 888 return ret; 889 890 ret = catpt_set_dspvol(cdev, cdev->mixer.mixer_hw_id, 891 ucontrol->value.integer.value); 892 893 pm_runtime_put_autosuspend(cdev->dev); 894 895 return ret; 896 } 897 898 static int catpt_stream_volume_get(struct snd_kcontrol *kcontrol, 899 struct snd_ctl_elem_value *ucontrol, 900 enum catpt_pin_id pin_id) 901 { 902 struct snd_soc_component *component = 903 snd_soc_kcontrol_component(kcontrol); 904 struct catpt_stream_runtime *stream; 905 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 906 long *ctlvol = (long *)kcontrol->private_value; 907 u32 dspvol; 908 int ret; 909 int i; 910 911 stream = catpt_stream_find(cdev, pin_id); 912 if (!stream) { 913 for (i = 0; i < CATPT_CHANNELS_MAX; i++) 914 ucontrol->value.integer.value[i] = ctlvol[i]; 915 return 0; 916 } 917 918 ret = pm_runtime_resume_and_get(cdev->dev); 919 if (ret < 0 && ret != -EACCES) 920 return ret; 921 922 for (i = 0; i < CATPT_CHANNELS_MAX; i++) { 923 dspvol = catpt_stream_volume(cdev, stream, i); 924 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol); 925 } 926 927 pm_runtime_put_autosuspend(cdev->dev); 928 929 return 0; 930 } 931 932 static int catpt_stream_volume_put(struct snd_kcontrol *kcontrol, 933 struct snd_ctl_elem_value *ucontrol, 934 enum catpt_pin_id pin_id) 935 { 936 struct snd_soc_component *component = 937 snd_soc_kcontrol_component(kcontrol); 938 struct catpt_stream_runtime *stream; 939 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 940 long *ctlvol = (long *)kcontrol->private_value; 941 int ret, i; 942 943 stream = catpt_stream_find(cdev, pin_id); 944 if (!stream) { 945 for (i = 0; i < CATPT_CHANNELS_MAX; i++) 946 ctlvol[i] = ucontrol->value.integer.value[i]; 947 return 0; 948 } 949 950 ret = pm_runtime_resume_and_get(cdev->dev); 951 if (ret < 0 && ret != -EACCES) 952 return ret; 953 954 ret = catpt_set_dspvol(cdev, stream->info.stream_hw_id, 955 ucontrol->value.integer.value); 956 957 pm_runtime_put_autosuspend(cdev->dev); 958 959 if (ret) 960 return ret; 961 962 for (i = 0; i < CATPT_CHANNELS_MAX; i++) 963 ctlvol[i] = ucontrol->value.integer.value[i]; 964 return 0; 965 } 966 967 static int catpt_offload1_volume_get(struct snd_kcontrol *kctl, 968 struct snd_ctl_elem_value *uctl) 969 { 970 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD1); 971 } 972 973 static int catpt_offload1_volume_put(struct snd_kcontrol *kctl, 974 struct snd_ctl_elem_value *uctl) 975 { 976 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD1); 977 } 978 979 static int catpt_offload2_volume_get(struct snd_kcontrol *kctl, 980 struct snd_ctl_elem_value *uctl) 981 { 982 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD2); 983 } 984 985 static int catpt_offload2_volume_put(struct snd_kcontrol *kctl, 986 struct snd_ctl_elem_value *uctl) 987 { 988 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD2); 989 } 990 991 static int catpt_capture_volume_get(struct snd_kcontrol *kctl, 992 struct snd_ctl_elem_value *uctl) 993 { 994 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_CAPTURE1); 995 } 996 997 static int catpt_capture_volume_put(struct snd_kcontrol *kctl, 998 struct snd_ctl_elem_value *uctl) 999 { 1000 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_CAPTURE1); 1001 } 1002 1003 static int catpt_loopback_switch_get(struct snd_kcontrol *kcontrol, 1004 struct snd_ctl_elem_value *ucontrol) 1005 { 1006 ucontrol->value.integer.value[0] = *(bool *)kcontrol->private_value; 1007 return 0; 1008 } 1009 1010 static int catpt_loopback_switch_put(struct snd_kcontrol *kcontrol, 1011 struct snd_ctl_elem_value *ucontrol) 1012 { 1013 struct snd_soc_component *component = 1014 snd_soc_kcontrol_component(kcontrol); 1015 struct catpt_stream_runtime *stream; 1016 struct catpt_dev *cdev = dev_get_drvdata(component->dev); 1017 bool mute; 1018 int ret; 1019 1020 mute = (bool)ucontrol->value.integer.value[0]; 1021 stream = catpt_stream_find(cdev, CATPT_PIN_ID_REFERENCE); 1022 if (!stream) { 1023 *(bool *)kcontrol->private_value = mute; 1024 return 0; 1025 } 1026 1027 ret = pm_runtime_resume_and_get(cdev->dev); 1028 if (ret < 0 && ret != -EACCES) 1029 return ret; 1030 1031 ret = catpt_ipc_mute_loopback(cdev, stream->info.stream_hw_id, mute); 1032 1033 pm_runtime_put_autosuspend(cdev->dev); 1034 1035 if (ret) 1036 return CATPT_IPC_ERROR(ret); 1037 1038 *(bool *)kcontrol->private_value = mute; 1039 return 0; 1040 } 1041 1042 static int catpt_waves_switch_get(struct snd_kcontrol *kcontrol, 1043 struct snd_ctl_elem_value *ucontrol) 1044 { 1045 return 0; 1046 } 1047 1048 static int catpt_waves_switch_put(struct snd_kcontrol *kcontrol, 1049 struct snd_ctl_elem_value *ucontrol) 1050 { 1051 return 0; 1052 } 1053 1054 static int catpt_waves_param_get(struct snd_kcontrol *kcontrol, 1055 unsigned int __user *bytes, 1056 unsigned int size) 1057 { 1058 return 0; 1059 } 1060 1061 static int catpt_waves_param_put(struct snd_kcontrol *kcontrol, 1062 const unsigned int __user *bytes, 1063 unsigned int size) 1064 { 1065 return 0; 1066 } 1067 1068 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(catpt_volume_tlv, -9000, 300, 1); 1069 1070 #define CATPT_VOLUME_CTL(kname, sname) \ 1071 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 1072 .name = (kname), \ 1073 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \ 1074 SNDRV_CTL_ELEM_ACCESS_READWRITE, \ 1075 .info = catpt_volume_info, \ 1076 .get = catpt_##sname##_volume_get, \ 1077 .put = catpt_##sname##_volume_put, \ 1078 .tlv.p = catpt_volume_tlv, \ 1079 .private_value = (unsigned long) \ 1080 &(long[CATPT_CHANNELS_MAX]) {0} } 1081 1082 static const struct snd_kcontrol_new component_kcontrols[] = { 1083 /* Master volume (mixer stream) */ 1084 CATPT_VOLUME_CTL("Master Playback Volume", mixer), 1085 /* Individual volume controls for offload and capture */ 1086 CATPT_VOLUME_CTL("Media0 Playback Volume", offload1), 1087 CATPT_VOLUME_CTL("Media1 Playback Volume", offload2), 1088 CATPT_VOLUME_CTL("Mic Capture Volume", capture), 1089 SOC_SINGLE_BOOL_EXT("Loopback Mute", (unsigned long)&(bool[1]) {0}, 1090 catpt_loopback_switch_get, catpt_loopback_switch_put), 1091 /* Enable or disable WAVES module */ 1092 SOC_SINGLE_BOOL_EXT("Waves Switch", 0, 1093 catpt_waves_switch_get, catpt_waves_switch_put), 1094 /* WAVES module parameter control */ 1095 SND_SOC_BYTES_TLV("Waves Set Param", 128, 1096 catpt_waves_param_get, catpt_waves_param_put), 1097 }; 1098 1099 static const struct snd_soc_dapm_widget component_widgets[] = { 1100 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0), 1101 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0), 1102 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0), 1103 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0), 1104 1105 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0), 1106 }; 1107 1108 static const struct snd_soc_dapm_route component_routes[] = { 1109 {"Playback VMixer", NULL, "System Playback"}, 1110 {"Playback VMixer", NULL, "Offload0 Playback"}, 1111 {"Playback VMixer", NULL, "Offload1 Playback"}, 1112 1113 {"SSP0 CODEC OUT", NULL, "Playback VMixer"}, 1114 1115 {"Analog Capture", NULL, "SSP0 CODEC IN"}, 1116 {"Loopback Capture", NULL, "SSP0 CODEC IN"}, 1117 1118 {"SSP1 BT OUT", NULL, "Bluetooth Playback"}, 1119 {"Bluetooth Capture", NULL, "SSP1 BT IN"}, 1120 }; 1121 1122 static const struct snd_soc_component_driver catpt_comp_driver = { 1123 .name = "catpt-platform", 1124 1125 .pcm_construct = catpt_component_pcm_construct, 1126 .open = catpt_component_open, 1127 .pointer = catpt_component_pointer, 1128 1129 .controls = component_kcontrols, 1130 .num_controls = ARRAY_SIZE(component_kcontrols), 1131 .dapm_widgets = component_widgets, 1132 .num_dapm_widgets = ARRAY_SIZE(component_widgets), 1133 .dapm_routes = component_routes, 1134 .num_dapm_routes = ARRAY_SIZE(component_routes), 1135 }; 1136 1137 int catpt_arm_stream_templates(struct catpt_dev *cdev) 1138 { 1139 struct resource *res; 1140 u32 scratch_size = 0; 1141 int i, j; 1142 1143 for (i = 0; i < ARRAY_SIZE(catpt_topology); i++) { 1144 struct catpt_stream_template *template; 1145 struct catpt_module_entry *entry; 1146 struct catpt_module_type *type; 1147 1148 template = catpt_topology[i]; 1149 template->persistent_size = 0; 1150 1151 for (j = 0; j < template->num_entries; j++) { 1152 entry = &template->entries[j]; 1153 type = &cdev->modules[entry->module_id]; 1154 1155 if (!type->loaded) 1156 return -ENOENT; 1157 1158 entry->entry_point = type->entry_point; 1159 template->persistent_size += type->persistent_size; 1160 if (type->scratch_size > scratch_size) 1161 scratch_size = type->scratch_size; 1162 } 1163 } 1164 1165 if (scratch_size) { 1166 /* allocate single scratch area for all modules */ 1167 res = catpt_request_region(&cdev->dram, scratch_size); 1168 if (!res) 1169 return -EBUSY; 1170 cdev->scratch = res; 1171 } 1172 1173 return 0; 1174 } 1175 1176 int catpt_register_plat_component(struct catpt_dev *cdev) 1177 { 1178 struct snd_soc_component *component; 1179 int ret; 1180 1181 component = devm_kzalloc(cdev->dev, sizeof(*component), GFP_KERNEL); 1182 if (!component) 1183 return -ENOMEM; 1184 1185 ret = snd_soc_component_initialize(component, &catpt_comp_driver, 1186 cdev->dev); 1187 if (ret) 1188 return ret; 1189 1190 component->name = catpt_comp_driver.name; 1191 return snd_soc_add_component(component, dai_drivers, 1192 ARRAY_SIZE(dai_drivers)); 1193 } 1194