1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // soc-pcm.c -- ALSA SoC PCM 4 // 5 // Copyright 2005 Wolfson Microelectronics PLC. 6 // Copyright 2005 Openedhand Ltd. 7 // Copyright (C) 2010 Slimlogic Ltd. 8 // Copyright (C) 2010 Texas Instruments Inc. 9 // 10 // Authors: Liam Girdwood <lrg@ti.com> 11 // Mark Brown <broonie@opensource.wolfsonmicro.com> 12 13 #include <linux/kernel.h> 14 #include <linux/init.h> 15 #include <linux/delay.h> 16 #include <linux/pinctrl/consumer.h> 17 #include <linux/slab.h> 18 #include <linux/workqueue.h> 19 #include <linux/export.h> 20 #include <linux/debugfs.h> 21 #include <sound/core.h> 22 #include <sound/pcm.h> 23 #include <sound/pcm_params.h> 24 #include <sound/soc.h> 25 #include <sound/soc-dpcm.h> 26 #include <sound/soc-link.h> 27 #include <sound/initval.h> 28 29 #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret) 30 static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd, 31 const char *func, int ret) 32 { 33 /* Positive, Zero values are not errors */ 34 if (ret >= 0) 35 return ret; 36 37 /* Negative values might be errors */ 38 switch (ret) { 39 case -EPROBE_DEFER: 40 case -ENOTSUPP: 41 break; 42 default: 43 dev_err(rtd->dev, 44 "ASoC: error at %s on %s: %d\n", 45 func, rtd->dai_link->name, ret); 46 } 47 48 return ret; 49 } 50 51 /* is the current PCM operation for this FE ? */ 52 #if 0 53 static int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 54 { 55 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 56 return 1; 57 return 0; 58 } 59 #endif 60 61 /* is the current PCM operation for this BE ? */ 62 static int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 63 struct snd_soc_pcm_runtime *be, int stream) 64 { 65 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 66 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 67 be->dpcm[stream].runtime_update)) 68 return 1; 69 return 0; 70 } 71 72 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, 73 struct snd_soc_pcm_runtime *be, 74 int stream, 75 const enum snd_soc_dpcm_state *states, 76 int num_states) 77 { 78 struct snd_soc_dpcm *dpcm; 79 int state; 80 int ret = 1; 81 int i; 82 83 for_each_dpcm_fe(be, stream, dpcm) { 84 85 if (dpcm->fe == fe) 86 continue; 87 88 state = dpcm->fe->dpcm[stream].state; 89 for (i = 0; i < num_states; i++) { 90 if (state == states[i]) { 91 ret = 0; 92 break; 93 } 94 } 95 } 96 97 /* it's safe to do this BE DAI */ 98 return ret; 99 } 100 101 /* 102 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 103 * are not running, paused or suspended for the specified stream direction. 104 */ 105 static int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 106 struct snd_soc_pcm_runtime *be, int stream) 107 { 108 const enum snd_soc_dpcm_state state[] = { 109 SND_SOC_DPCM_STATE_START, 110 SND_SOC_DPCM_STATE_PAUSED, 111 SND_SOC_DPCM_STATE_SUSPEND, 112 }; 113 114 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 115 } 116 117 /* 118 * We can only change hw params a BE DAI if any of it's FE are not prepared, 119 * running, paused or suspended for the specified stream direction. 120 */ 121 static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 122 struct snd_soc_pcm_runtime *be, int stream) 123 { 124 const enum snd_soc_dpcm_state state[] = { 125 SND_SOC_DPCM_STATE_START, 126 SND_SOC_DPCM_STATE_PAUSED, 127 SND_SOC_DPCM_STATE_SUSPEND, 128 SND_SOC_DPCM_STATE_PREPARE, 129 }; 130 131 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 132 } 133 134 /* 135 * We can only prepare a BE DAI if any of it's FE are not prepared, 136 * running or paused for the specified stream direction. 137 */ 138 static int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe, 139 struct snd_soc_pcm_runtime *be, int stream) 140 { 141 const enum snd_soc_dpcm_state state[] = { 142 SND_SOC_DPCM_STATE_START, 143 SND_SOC_DPCM_STATE_PAUSED, 144 SND_SOC_DPCM_STATE_PREPARE, 145 }; 146 147 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 148 } 149 150 #define DPCM_MAX_BE_USERS 8 151 152 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd) 153 { 154 return (rtd)->dai_link->num_cpus == 1 ? snd_soc_rtd_to_cpu(rtd, 0)->name : "multicpu"; 155 } 156 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd) 157 { 158 return (rtd)->dai_link->num_codecs == 1 ? snd_soc_rtd_to_codec(rtd, 0)->name : "multicodec"; 159 } 160 161 #ifdef CONFIG_DEBUG_FS 162 static const char *dpcm_state_string(enum snd_soc_dpcm_state state) 163 { 164 switch (state) { 165 case SND_SOC_DPCM_STATE_NEW: 166 return "new"; 167 case SND_SOC_DPCM_STATE_OPEN: 168 return "open"; 169 case SND_SOC_DPCM_STATE_HW_PARAMS: 170 return "hw_params"; 171 case SND_SOC_DPCM_STATE_PREPARE: 172 return "prepare"; 173 case SND_SOC_DPCM_STATE_START: 174 return "start"; 175 case SND_SOC_DPCM_STATE_STOP: 176 return "stop"; 177 case SND_SOC_DPCM_STATE_SUSPEND: 178 return "suspend"; 179 case SND_SOC_DPCM_STATE_PAUSED: 180 return "paused"; 181 case SND_SOC_DPCM_STATE_HW_FREE: 182 return "hw_free"; 183 case SND_SOC_DPCM_STATE_CLOSE: 184 return "close"; 185 } 186 187 return "unknown"; 188 } 189 190 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 191 int stream, char *buf, size_t size) 192 { 193 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 194 struct snd_soc_dpcm *dpcm; 195 ssize_t offset = 0; 196 197 /* FE state */ 198 offset += scnprintf(buf + offset, size - offset, 199 "[%s - %s]\n", fe->dai_link->name, 200 stream ? "Capture" : "Playback"); 201 202 offset += scnprintf(buf + offset, size - offset, "State: %s\n", 203 dpcm_state_string(fe->dpcm[stream].state)); 204 205 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 206 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 207 offset += scnprintf(buf + offset, size - offset, 208 "Hardware Params: " 209 "Format = %s, Channels = %d, Rate = %d\n", 210 snd_pcm_format_name(params_format(params)), 211 params_channels(params), 212 params_rate(params)); 213 214 /* BEs state */ 215 offset += scnprintf(buf + offset, size - offset, "Backends:\n"); 216 217 if (list_empty(&fe->dpcm[stream].be_clients)) { 218 offset += scnprintf(buf + offset, size - offset, 219 " No active DSP links\n"); 220 goto out; 221 } 222 223 for_each_dpcm_be(fe, stream, dpcm) { 224 struct snd_soc_pcm_runtime *be = dpcm->be; 225 params = &be->dpcm[stream].hw_params; 226 227 offset += scnprintf(buf + offset, size - offset, 228 "- %s\n", be->dai_link->name); 229 230 offset += scnprintf(buf + offset, size - offset, 231 " State: %s\n", 232 dpcm_state_string(be->dpcm[stream].state)); 233 234 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 235 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 236 offset += scnprintf(buf + offset, size - offset, 237 " Hardware Params: " 238 "Format = %s, Channels = %d, Rate = %d\n", 239 snd_pcm_format_name(params_format(params)), 240 params_channels(params), 241 params_rate(params)); 242 } 243 out: 244 return offset; 245 } 246 247 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 248 size_t count, loff_t *ppos) 249 { 250 struct snd_soc_pcm_runtime *fe = file->private_data; 251 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 252 int stream; 253 char *buf; 254 255 if (fe->dai_link->num_cpus > 1) { 256 dev_err(fe->dev, 257 "%s doesn't support Multi CPU yet\n", __func__); 258 return -EINVAL; 259 } 260 261 buf = kmalloc(out_count, GFP_KERNEL); 262 if (!buf) 263 return -ENOMEM; 264 265 snd_soc_dpcm_mutex_lock(fe); 266 for_each_pcm_streams(stream) 267 if (snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0), stream)) 268 offset += dpcm_show_state(fe, stream, 269 buf + offset, 270 out_count - offset); 271 snd_soc_dpcm_mutex_unlock(fe); 272 273 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 274 275 kfree(buf); 276 return ret; 277 } 278 279 static const struct file_operations dpcm_state_fops = { 280 .open = simple_open, 281 .read = dpcm_state_read_file, 282 .llseek = default_llseek, 283 }; 284 285 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 286 { 287 if (!rtd->dai_link->dynamic) 288 return; 289 290 if (!rtd->card->debugfs_card_root) 291 return; 292 293 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 294 rtd->card->debugfs_card_root); 295 296 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 297 rtd, &dpcm_state_fops); 298 } 299 300 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream) 301 { 302 char *name; 303 304 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name, 305 snd_pcm_direction_name(stream)); 306 if (name) { 307 dpcm->debugfs_state = debugfs_create_dir( 308 name, dpcm->fe->debugfs_dpcm_root); 309 debugfs_create_u32("state", 0644, dpcm->debugfs_state, 310 &dpcm->state); 311 kfree(name); 312 } 313 } 314 315 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 316 { 317 debugfs_remove_recursive(dpcm->debugfs_state); 318 } 319 320 #else 321 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, 322 int stream) 323 { 324 } 325 326 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 327 { 328 } 329 #endif 330 331 /* Set FE's runtime_update state; the state is protected via PCM stream lock 332 * for avoiding the race with trigger callback. 333 * If the state is unset and a trigger is pending while the previous operation, 334 * process the pending trigger action here. 335 */ 336 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 337 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 338 int stream, enum snd_soc_dpcm_update state) 339 { 340 struct snd_pcm_substream *substream = 341 snd_soc_dpcm_get_substream(fe, stream); 342 343 snd_pcm_stream_lock_irq(substream); 344 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 345 dpcm_fe_dai_do_trigger(substream, 346 fe->dpcm[stream].trigger_pending - 1); 347 fe->dpcm[stream].trigger_pending = 0; 348 } 349 fe->dpcm[stream].runtime_update = state; 350 snd_pcm_stream_unlock_irq(substream); 351 } 352 353 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be, 354 int stream, enum snd_soc_dpcm_update state) 355 { 356 be->dpcm[stream].runtime_update = state; 357 } 358 359 /** 360 * snd_soc_runtime_action() - Increment/Decrement active count for 361 * PCM runtime components 362 * @rtd: ASoC PCM runtime that is activated 363 * @stream: Direction of the PCM stream 364 * @action: Activate stream if 1. Deactivate if -1. 365 * 366 * Increments/Decrements the active count for all the DAIs and components 367 * attached to a PCM runtime. 368 * Should typically be called when a stream is opened. 369 * 370 * Must be called with the rtd->card->pcm_mutex being held 371 */ 372 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, 373 int stream, int action) 374 { 375 struct snd_soc_component *component; 376 struct snd_soc_dai *dai; 377 int i; 378 379 snd_soc_dpcm_mutex_assert_held(rtd); 380 381 for_each_rtd_dais(rtd, i, dai) 382 snd_soc_dai_action(dai, stream, action); 383 384 /* Increments/Decrements the active count for components without DAIs */ 385 for_each_rtd_components(rtd, i, component) { 386 if (component->num_dai) 387 continue; 388 component->active += action; 389 } 390 } 391 EXPORT_SYMBOL_GPL(snd_soc_runtime_action); 392 393 /** 394 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 395 * @rtd: The ASoC PCM runtime that should be checked. 396 * 397 * This function checks whether the power down delay should be ignored for a 398 * specific PCM runtime. Returns true if the delay is 0, if the DAI link has 399 * been configured to ignore the delay, or if none of the components benefits 400 * from having the delay. 401 */ 402 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 403 { 404 struct snd_soc_component *component; 405 int i; 406 407 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 408 return true; 409 410 for_each_rtd_components(rtd, i, component) 411 if (component->driver->use_pmdown_time) 412 /* No need to go through all components */ 413 return false; 414 415 return true; 416 } 417 418 /* DPCM stream event, send event to FE and all active BEs. */ 419 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 420 int event) 421 { 422 struct snd_soc_dpcm *dpcm; 423 424 snd_soc_dpcm_mutex_assert_held(fe); 425 426 for_each_dpcm_be(fe, dir, dpcm) { 427 428 struct snd_soc_pcm_runtime *be = dpcm->be; 429 430 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 431 be->dai_link->name, event, dir); 432 433 if ((event == SND_SOC_DAPM_STREAM_STOP) && 434 (be->dpcm[dir].users >= 1)) 435 continue; 436 437 snd_soc_dapm_stream_event(be, dir, event); 438 } 439 440 snd_soc_dapm_stream_event(fe, dir, event); 441 442 return 0; 443 } 444 445 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai, 446 struct snd_pcm_hw_params *params) 447 { 448 if (params) { 449 dai->symmetric_rate = params_rate(params); 450 dai->symmetric_channels = params_channels(params); 451 dai->symmetric_sample_bits = snd_pcm_format_physical_width(params_format(params)); 452 } else { 453 dai->symmetric_rate = 0; 454 dai->symmetric_channels = 0; 455 dai->symmetric_sample_bits = 0; 456 } 457 } 458 459 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 460 struct snd_soc_dai *soc_dai) 461 { 462 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 463 int ret; 464 465 if (!snd_soc_dai_active(soc_dai)) 466 return 0; 467 468 #define __soc_pcm_apply_symmetry(name, NAME) \ 469 if (soc_dai->symmetric_##name && \ 470 (soc_dai->driver->symmetric_##name || rtd->dai_link->symmetric_##name)) { \ 471 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\ 472 #name, soc_dai->symmetric_##name); \ 473 \ 474 ret = snd_pcm_hw_constraint_single(substream->runtime, \ 475 SNDRV_PCM_HW_PARAM_##NAME,\ 476 soc_dai->symmetric_##name); \ 477 if (ret < 0) { \ 478 dev_err(soc_dai->dev, \ 479 "ASoC: Unable to apply %s constraint: %d\n",\ 480 #name, ret); \ 481 return ret; \ 482 } \ 483 } 484 485 __soc_pcm_apply_symmetry(rate, RATE); 486 __soc_pcm_apply_symmetry(channels, CHANNELS); 487 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS); 488 489 return 0; 490 } 491 492 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 493 struct snd_pcm_hw_params *params) 494 { 495 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 496 struct snd_soc_dai d; 497 struct snd_soc_dai *dai; 498 struct snd_soc_dai *cpu_dai; 499 unsigned int symmetry, i; 500 501 d.name = __func__; 502 soc_pcm_set_dai_params(&d, params); 503 504 #define __soc_pcm_params_symmetry(xxx) \ 505 symmetry = rtd->dai_link->symmetric_##xxx; \ 506 for_each_rtd_dais(rtd, i, dai) \ 507 symmetry |= dai->driver->symmetric_##xxx; \ 508 \ 509 if (symmetry) \ 510 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \ 511 if (!snd_soc_dai_is_dummy(cpu_dai) && \ 512 cpu_dai->symmetric_##xxx && \ 513 cpu_dai->symmetric_##xxx != d.symmetric_##xxx) { \ 514 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \ 515 #xxx, cpu_dai->name, cpu_dai->symmetric_##xxx, \ 516 d.name, d.symmetric_##xxx); \ 517 return -EINVAL; \ 518 } 519 520 /* reject unmatched parameters when applying symmetry */ 521 __soc_pcm_params_symmetry(rate); 522 __soc_pcm_params_symmetry(channels); 523 __soc_pcm_params_symmetry(sample_bits); 524 525 return 0; 526 } 527 528 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream) 529 { 530 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 531 struct snd_soc_dai_link *link = rtd->dai_link; 532 struct snd_soc_dai *dai; 533 unsigned int symmetry, i; 534 535 symmetry = link->symmetric_rate || 536 link->symmetric_channels || 537 link->symmetric_sample_bits; 538 539 for_each_rtd_dais(rtd, i, dai) 540 symmetry = symmetry || 541 dai->driver->symmetric_rate || 542 dai->driver->symmetric_channels || 543 dai->driver->symmetric_sample_bits; 544 545 if (symmetry) 546 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 547 } 548 549 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 550 { 551 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 552 int ret; 553 554 if (!bits) 555 return; 556 557 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 558 if (ret != 0) 559 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 560 bits, ret); 561 } 562 563 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 564 { 565 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 566 struct snd_soc_dai *cpu_dai; 567 struct snd_soc_dai *codec_dai; 568 int stream = substream->stream; 569 int i; 570 unsigned int bits = 0, cpu_bits = 0; 571 572 for_each_rtd_codec_dais(rtd, i, codec_dai) { 573 const struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); 574 575 if (pcm_codec->sig_bits == 0) { 576 bits = 0; 577 break; 578 } 579 bits = max(pcm_codec->sig_bits, bits); 580 } 581 582 for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 583 const struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 584 585 if (pcm_cpu->sig_bits == 0) { 586 cpu_bits = 0; 587 break; 588 } 589 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits); 590 } 591 592 soc_pcm_set_msb(substream, bits); 593 soc_pcm_set_msb(substream, cpu_bits); 594 } 595 596 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw) 597 { 598 hw->rates = UINT_MAX; 599 hw->rate_min = 0; 600 hw->rate_max = UINT_MAX; 601 hw->channels_min = 0; 602 hw->channels_max = UINT_MAX; 603 hw->formats = ULLONG_MAX; 604 } 605 606 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw, 607 const struct snd_soc_pcm_stream *p) 608 { 609 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates); 610 611 /* setup hw->rate_min/max via hw->rates first */ 612 snd_pcm_hw_limit_rates(hw); 613 614 /* update hw->rate_min/max by snd_soc_pcm_stream */ 615 hw->rate_min = max(hw->rate_min, p->rate_min); 616 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max); 617 } 618 619 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw, 620 const struct snd_soc_pcm_stream *p) 621 { 622 hw->channels_min = max(hw->channels_min, p->channels_min); 623 hw->channels_max = min(hw->channels_max, p->channels_max); 624 } 625 626 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw, 627 const struct snd_soc_pcm_stream *p) 628 { 629 hw->formats &= p->formats; 630 } 631 632 static void soc_pcm_hw_update_subformat(struct snd_pcm_hardware *hw, 633 const struct snd_soc_pcm_stream *p) 634 { 635 hw->subformats &= p->subformats; 636 } 637 638 /** 639 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream 640 * @rtd: ASoC PCM runtime 641 * @hw: PCM hardware parameters (output) 642 * @stream: Direction of the PCM stream 643 * 644 * Calculates the subset of stream parameters supported by all DAIs 645 * associated with the PCM stream. 646 */ 647 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd, 648 struct snd_pcm_hardware *hw, int stream) 649 { 650 struct snd_soc_dai *codec_dai; 651 struct snd_soc_dai *cpu_dai; 652 const struct snd_soc_pcm_stream *codec_stream; 653 const struct snd_soc_pcm_stream *cpu_stream; 654 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX; 655 int i; 656 657 soc_pcm_hw_init(hw); 658 659 /* first calculate min/max only for CPUs in the DAI link */ 660 for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 661 662 /* 663 * Skip CPUs which don't support the current stream type. 664 * Otherwise, since the rate, channel, and format values will 665 * zero in that case, we would have no usable settings left, 666 * causing the resulting setup to fail. 667 */ 668 if (!snd_soc_dai_stream_valid(cpu_dai, stream)) 669 continue; 670 671 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 672 673 soc_pcm_hw_update_chan(hw, cpu_stream); 674 soc_pcm_hw_update_rate(hw, cpu_stream); 675 soc_pcm_hw_update_format(hw, cpu_stream); 676 soc_pcm_hw_update_subformat(hw, cpu_stream); 677 } 678 cpu_chan_min = hw->channels_min; 679 cpu_chan_max = hw->channels_max; 680 681 /* second calculate min/max only for CODECs in the DAI link */ 682 for_each_rtd_codec_dais(rtd, i, codec_dai) { 683 684 /* 685 * Skip CODECs which don't support the current stream type. 686 * Otherwise, since the rate, channel, and format values will 687 * zero in that case, we would have no usable settings left, 688 * causing the resulting setup to fail. 689 */ 690 if (!snd_soc_dai_stream_valid(codec_dai, stream)) 691 continue; 692 693 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); 694 695 soc_pcm_hw_update_chan(hw, codec_stream); 696 soc_pcm_hw_update_rate(hw, codec_stream); 697 soc_pcm_hw_update_format(hw, codec_stream); 698 soc_pcm_hw_update_subformat(hw, codec_stream); 699 } 700 701 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */ 702 if (!hw->channels_min) 703 return -EINVAL; 704 705 /* 706 * chan min/max cannot be enforced if there are multiple CODEC DAIs 707 * connected to CPU DAI(s), use CPU DAI's directly and let 708 * channel allocation be fixed up later 709 */ 710 if (rtd->dai_link->num_codecs > 1) { 711 hw->channels_min = cpu_chan_min; 712 hw->channels_max = cpu_chan_max; 713 } 714 715 return 0; 716 } 717 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw); 718 719 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 720 { 721 struct snd_pcm_hardware *hw = &substream->runtime->hw; 722 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 723 u64 formats = hw->formats; 724 725 /* 726 * At least one CPU and one CODEC should match. Otherwise, we should 727 * have bailed out on a higher level, since there would be no CPU or 728 * CODEC to support the transfer direction in that case. 729 */ 730 snd_soc_runtime_calc_hw(rtd, hw, substream->stream); 731 732 if (formats) 733 hw->formats &= formats; 734 } 735 736 static int soc_pcm_components_open(struct snd_pcm_substream *substream) 737 { 738 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 739 struct snd_soc_component *component; 740 int i, ret = 0; 741 742 for_each_rtd_components(rtd, i, component) { 743 ret = snd_soc_component_module_get_when_open(component, substream); 744 if (ret < 0) 745 break; 746 747 ret = snd_soc_component_open(component, substream); 748 if (ret < 0) 749 break; 750 } 751 752 return ret; 753 } 754 755 static int soc_pcm_components_close(struct snd_pcm_substream *substream, 756 int rollback) 757 { 758 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 759 struct snd_soc_component *component; 760 int i, ret = 0; 761 762 for_each_rtd_components(rtd, i, component) { 763 int r = snd_soc_component_close(component, substream, rollback); 764 if (r < 0) 765 ret = r; /* use last ret */ 766 767 snd_soc_component_module_put_when_close(component, substream, rollback); 768 } 769 770 return ret; 771 } 772 773 static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd, 774 struct snd_pcm_substream *substream, int rollback) 775 { 776 struct snd_soc_component *component; 777 struct snd_soc_dai *dai; 778 int i; 779 780 snd_soc_dpcm_mutex_assert_held(rtd); 781 782 if (!rollback) { 783 snd_soc_runtime_deactivate(rtd, substream->stream); 784 785 /* Make sure DAI parameters cleared if the DAI becomes inactive */ 786 for_each_rtd_dais(rtd, i, dai) { 787 if (snd_soc_dai_active(dai) == 0) 788 soc_pcm_set_dai_params(dai, NULL); 789 } 790 } 791 792 for_each_rtd_dais_reverse(rtd, i, dai) 793 snd_soc_dai_shutdown(dai, substream, rollback); 794 795 snd_soc_link_shutdown(substream, rollback); 796 797 soc_pcm_components_close(substream, rollback); 798 799 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback); 800 801 for_each_rtd_components(rtd, i, component) 802 if (!snd_soc_component_active(component)) 803 pinctrl_pm_select_sleep_state(component->dev); 804 805 return 0; 806 } 807 808 /* 809 * Called by ALSA when a PCM substream is closed. Private data can be 810 * freed here. The cpu DAI, codec DAI, machine and components are also 811 * shutdown. 812 */ 813 static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd, 814 struct snd_pcm_substream *substream) 815 { 816 return soc_pcm_clean(rtd, substream, 0); 817 } 818 819 /* PCM close ops for non-DPCM streams */ 820 static int soc_pcm_close(struct snd_pcm_substream *substream) 821 { 822 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 823 824 snd_soc_dpcm_mutex_lock(rtd); 825 __soc_pcm_close(rtd, substream); 826 snd_soc_dpcm_mutex_unlock(rtd); 827 return 0; 828 } 829 830 static int soc_hw_sanity_check(struct snd_pcm_substream *substream) 831 { 832 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 833 struct snd_pcm_hardware *hw = &substream->runtime->hw; 834 const char *name_cpu = soc_cpu_dai_name(rtd); 835 const char *name_codec = soc_codec_dai_name(rtd); 836 const char *err_msg; 837 struct device *dev = rtd->dev; 838 839 err_msg = "rates"; 840 if (!hw->rates) 841 goto config_err; 842 843 err_msg = "formats"; 844 if (!hw->formats) 845 goto config_err; 846 847 err_msg = "channels"; 848 if (!hw->channels_min || !hw->channels_max || 849 hw->channels_min > hw->channels_max) 850 goto config_err; 851 852 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec, 853 name_cpu); 854 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates); 855 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min, 856 hw->channels_max); 857 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min, 858 hw->rate_max); 859 860 return 0; 861 862 config_err: 863 dev_err(dev, "ASoC: %s <-> %s No matching %s\n", 864 name_codec, name_cpu, err_msg); 865 return -EINVAL; 866 } 867 868 /* 869 * Called by ALSA when a PCM substream is opened, the runtime->hw record is 870 * then initialized and any private data can be allocated. This also calls 871 * startup for the cpu DAI, component, machine and codec DAI. 872 */ 873 static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd, 874 struct snd_pcm_substream *substream) 875 { 876 struct snd_soc_component *component; 877 struct snd_soc_dai *dai; 878 int i, ret = 0; 879 880 snd_soc_dpcm_mutex_assert_held(rtd); 881 882 for_each_rtd_components(rtd, i, component) 883 pinctrl_pm_select_default_state(component->dev); 884 885 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream); 886 if (ret < 0) 887 goto err; 888 889 ret = soc_pcm_components_open(substream); 890 if (ret < 0) 891 goto err; 892 893 ret = snd_soc_link_startup(substream); 894 if (ret < 0) 895 goto err; 896 897 /* startup the audio subsystem */ 898 for_each_rtd_dais(rtd, i, dai) { 899 ret = snd_soc_dai_startup(dai, substream); 900 if (ret < 0) 901 goto err; 902 } 903 904 /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 905 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 906 goto dynamic; 907 908 /* Check that the codec and cpu DAIs are compatible */ 909 soc_pcm_init_runtime_hw(substream); 910 911 soc_pcm_update_symmetry(substream); 912 913 ret = soc_hw_sanity_check(substream); 914 if (ret < 0) 915 goto err; 916 917 soc_pcm_apply_msb(substream); 918 919 /* Symmetry only applies if we've already got an active stream. */ 920 for_each_rtd_dais(rtd, i, dai) { 921 ret = soc_pcm_apply_symmetry(substream, dai); 922 if (ret != 0) 923 goto err; 924 } 925 dynamic: 926 snd_soc_runtime_activate(rtd, substream->stream); 927 ret = 0; 928 err: 929 if (ret < 0) 930 soc_pcm_clean(rtd, substream, 1); 931 932 return soc_pcm_ret(rtd, ret); 933 } 934 935 /* PCM open ops for non-DPCM streams */ 936 static int soc_pcm_open(struct snd_pcm_substream *substream) 937 { 938 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 939 int ret; 940 941 snd_soc_dpcm_mutex_lock(rtd); 942 ret = __soc_pcm_open(rtd, substream); 943 snd_soc_dpcm_mutex_unlock(rtd); 944 return ret; 945 } 946 947 /* 948 * Called by ALSA when the PCM substream is prepared, can set format, sample 949 * rate, etc. This function is non atomic and can be called multiple times, 950 * it can refer to the runtime info. 951 */ 952 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd, 953 struct snd_pcm_substream *substream) 954 { 955 struct snd_soc_dai *dai; 956 int i, ret = 0; 957 958 snd_soc_dpcm_mutex_assert_held(rtd); 959 960 ret = snd_soc_link_prepare(substream); 961 if (ret < 0) 962 goto out; 963 964 ret = snd_soc_pcm_component_prepare(substream); 965 if (ret < 0) 966 goto out; 967 968 ret = snd_soc_pcm_dai_prepare(substream); 969 if (ret < 0) 970 goto out; 971 972 /* cancel any delayed stream shutdown that is pending */ 973 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 974 rtd->pop_wait) { 975 rtd->pop_wait = 0; 976 cancel_delayed_work(&rtd->delayed_work); 977 } 978 979 snd_soc_dapm_stream_event(rtd, substream->stream, 980 SND_SOC_DAPM_STREAM_START); 981 982 for_each_rtd_dais(rtd, i, dai) { 983 if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger) 984 snd_soc_dai_digital_mute(dai, 0, substream->stream); 985 } 986 987 out: 988 /* 989 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity 990 * 991 * We don't want to log an error since we do not want to give userspace a way to do a 992 * denial-of-service attack on the syslog / diskspace. 993 */ 994 return ret; 995 } 996 997 /* PCM prepare ops for non-DPCM streams */ 998 static int soc_pcm_prepare(struct snd_pcm_substream *substream) 999 { 1000 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1001 int ret; 1002 1003 snd_soc_dpcm_mutex_lock(rtd); 1004 ret = __soc_pcm_prepare(rtd, substream); 1005 snd_soc_dpcm_mutex_unlock(rtd); 1006 1007 /* 1008 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity 1009 * 1010 * We don't want to log an error since we do not want to give userspace a way to do a 1011 * denial-of-service attack on the syslog / diskspace. 1012 */ 1013 return ret; 1014 } 1015 1016 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 1017 unsigned int mask) 1018 { 1019 struct snd_interval *interval; 1020 int channels = hweight_long(mask); 1021 1022 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 1023 interval->min = channels; 1024 interval->max = channels; 1025 } 1026 1027 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd, 1028 struct snd_pcm_substream *substream, int rollback) 1029 { 1030 struct snd_soc_dai *dai; 1031 int i; 1032 1033 snd_soc_dpcm_mutex_assert_held(rtd); 1034 1035 /* clear the corresponding DAIs parameters when going to be inactive */ 1036 for_each_rtd_dais(rtd, i, dai) { 1037 if (snd_soc_dai_active(dai) == 1) 1038 soc_pcm_set_dai_params(dai, NULL); 1039 1040 if (snd_soc_dai_stream_active(dai, substream->stream) == 1) { 1041 if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger) 1042 snd_soc_dai_digital_mute(dai, 1, substream->stream); 1043 } 1044 } 1045 1046 /* run the stream event */ 1047 snd_soc_dapm_stream_stop(rtd, substream->stream); 1048 1049 /* free any machine hw params */ 1050 snd_soc_link_hw_free(substream, rollback); 1051 1052 /* free any component resources */ 1053 snd_soc_pcm_component_hw_free(substream, rollback); 1054 1055 /* now free hw params for the DAIs */ 1056 for_each_rtd_dais(rtd, i, dai) 1057 if (snd_soc_dai_stream_valid(dai, substream->stream)) 1058 snd_soc_dai_hw_free(dai, substream, rollback); 1059 1060 return 0; 1061 } 1062 1063 /* 1064 * Frees resources allocated by hw_params, can be called multiple times 1065 */ 1066 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd, 1067 struct snd_pcm_substream *substream) 1068 { 1069 return soc_pcm_hw_clean(rtd, substream, 0); 1070 } 1071 1072 /* hw_free PCM ops for non-DPCM streams */ 1073 static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1074 { 1075 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1076 int ret; 1077 1078 snd_soc_dpcm_mutex_lock(rtd); 1079 ret = __soc_pcm_hw_free(rtd, substream); 1080 snd_soc_dpcm_mutex_unlock(rtd); 1081 return ret; 1082 } 1083 1084 /* 1085 * Called by ALSA when the hardware params are set by application. This 1086 * function can also be called multiple times and can allocate buffers 1087 * (using snd_pcm_lib_* ). It's non-atomic. 1088 */ 1089 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd, 1090 struct snd_pcm_substream *substream, 1091 struct snd_pcm_hw_params *params) 1092 { 1093 struct snd_soc_dai *cpu_dai; 1094 struct snd_soc_dai *codec_dai; 1095 struct snd_pcm_hw_params tmp_params; 1096 int i, ret = 0; 1097 1098 snd_soc_dpcm_mutex_assert_held(rtd); 1099 1100 ret = soc_pcm_params_symmetry(substream, params); 1101 if (ret) 1102 goto out; 1103 1104 ret = snd_soc_link_hw_params(substream, params); 1105 if (ret < 0) 1106 goto out; 1107 1108 for_each_rtd_codec_dais(rtd, i, codec_dai) { 1109 unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream); 1110 1111 /* 1112 * Skip CODECs which don't support the current stream type, 1113 * the idea being that if a CODEC is not used for the currently 1114 * set up transfer direction, it should not need to be 1115 * configured, especially since the configuration used might 1116 * not even be supported by that CODEC. There may be cases 1117 * however where a CODEC needs to be set up although it is 1118 * actually not being used for the transfer, e.g. if a 1119 * capture-only CODEC is acting as an LRCLK and/or BCLK master 1120 * for the DAI link including a playback-only CODEC. 1121 * If this becomes necessary, we will have to augment the 1122 * machine driver setup with information on how to act, so 1123 * we can do the right thing here. 1124 */ 1125 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1126 continue; 1127 1128 /* copy params for each codec */ 1129 tmp_params = *params; 1130 1131 /* fixup params based on TDM slot masks */ 1132 if (tdm_mask) 1133 soc_pcm_codec_params_fixup(&tmp_params, tdm_mask); 1134 1135 ret = snd_soc_dai_hw_params(codec_dai, substream, 1136 &tmp_params); 1137 if(ret < 0) 1138 goto out; 1139 1140 soc_pcm_set_dai_params(codec_dai, &tmp_params); 1141 snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai); 1142 } 1143 1144 for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1145 struct snd_soc_dai_link_ch_map *ch_maps; 1146 unsigned int ch_mask = 0; 1147 int j; 1148 1149 /* 1150 * Skip CPUs which don't support the current stream 1151 * type. See soc_pcm_init_runtime_hw() for more details 1152 */ 1153 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 1154 continue; 1155 1156 /* copy params for each cpu */ 1157 tmp_params = *params; 1158 1159 /* 1160 * construct cpu channel mask by combining ch_mask of each 1161 * codec which maps to the cpu. 1162 * see 1163 * soc.h :: [dai_link->ch_maps Image sample] 1164 */ 1165 for_each_rtd_ch_maps(rtd, j, ch_maps) 1166 if (ch_maps->cpu == i) 1167 ch_mask |= ch_maps->ch_mask; 1168 1169 /* fixup cpu channel number */ 1170 if (ch_mask) 1171 soc_pcm_codec_params_fixup(&tmp_params, ch_mask); 1172 1173 ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params); 1174 if (ret < 0) 1175 goto out; 1176 1177 /* store the parameters for each DAI */ 1178 soc_pcm_set_dai_params(cpu_dai, &tmp_params); 1179 snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai); 1180 } 1181 1182 ret = snd_soc_pcm_component_hw_params(substream, params); 1183 out: 1184 if (ret < 0) 1185 soc_pcm_hw_clean(rtd, substream, 1); 1186 1187 return soc_pcm_ret(rtd, ret); 1188 } 1189 1190 /* hw_params PCM ops for non-DPCM streams */ 1191 static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 1192 struct snd_pcm_hw_params *params) 1193 { 1194 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1195 int ret; 1196 1197 snd_soc_dpcm_mutex_lock(rtd); 1198 ret = __soc_pcm_hw_params(rtd, substream, params); 1199 snd_soc_dpcm_mutex_unlock(rtd); 1200 return ret; 1201 } 1202 1203 #define TRIGGER_MAX 3 1204 static int (* const trigger[][TRIGGER_MAX])(struct snd_pcm_substream *substream, int cmd, int rollback) = { 1205 [SND_SOC_TRIGGER_ORDER_DEFAULT] = { 1206 snd_soc_link_trigger, 1207 snd_soc_pcm_component_trigger, 1208 snd_soc_pcm_dai_trigger, 1209 }, 1210 [SND_SOC_TRIGGER_ORDER_LDC] = { 1211 snd_soc_link_trigger, 1212 snd_soc_pcm_dai_trigger, 1213 snd_soc_pcm_component_trigger, 1214 }, 1215 }; 1216 1217 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 1218 { 1219 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1220 struct snd_soc_component *component; 1221 int ret = 0, r = 0, i; 1222 int rollback = 0; 1223 int start = 0, stop = 0; 1224 1225 /* 1226 * select START/STOP sequence 1227 */ 1228 for_each_rtd_components(rtd, i, component) { 1229 if (component->driver->trigger_start) 1230 start = component->driver->trigger_start; 1231 if (component->driver->trigger_stop) 1232 stop = component->driver->trigger_stop; 1233 } 1234 if (rtd->dai_link->trigger_start) 1235 start = rtd->dai_link->trigger_start; 1236 if (rtd->dai_link->trigger_stop) 1237 stop = rtd->dai_link->trigger_stop; 1238 1239 if (start < 0 || start >= SND_SOC_TRIGGER_ORDER_MAX || 1240 stop < 0 || stop >= SND_SOC_TRIGGER_ORDER_MAX) 1241 return -EINVAL; 1242 1243 /* 1244 * START 1245 */ 1246 switch (cmd) { 1247 case SNDRV_PCM_TRIGGER_START: 1248 case SNDRV_PCM_TRIGGER_RESUME: 1249 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1250 for (i = 0; i < TRIGGER_MAX; i++) { 1251 r = trigger[start][i](substream, cmd, 0); 1252 if (r < 0) 1253 break; 1254 } 1255 } 1256 1257 /* 1258 * Rollback if START failed 1259 * find correspond STOP command 1260 */ 1261 if (r < 0) { 1262 rollback = 1; 1263 ret = r; 1264 switch (cmd) { 1265 case SNDRV_PCM_TRIGGER_START: 1266 cmd = SNDRV_PCM_TRIGGER_STOP; 1267 break; 1268 case SNDRV_PCM_TRIGGER_RESUME: 1269 cmd = SNDRV_PCM_TRIGGER_SUSPEND; 1270 break; 1271 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1272 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH; 1273 break; 1274 } 1275 } 1276 1277 /* 1278 * STOP 1279 */ 1280 switch (cmd) { 1281 case SNDRV_PCM_TRIGGER_STOP: 1282 case SNDRV_PCM_TRIGGER_SUSPEND: 1283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1284 for (i = TRIGGER_MAX; i > 0; i--) { 1285 r = trigger[stop][i - 1](substream, cmd, rollback); 1286 if (r < 0) 1287 ret = r; 1288 } 1289 } 1290 1291 return ret; 1292 } 1293 1294 /* 1295 * soc level wrapper for pointer callback 1296 * If cpu_dai, codec_dai, component driver has the delay callback, then 1297 * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay(). 1298 */ 1299 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1300 { 1301 struct snd_pcm_runtime *runtime = substream->runtime; 1302 snd_pcm_uframes_t offset = 0; 1303 snd_pcm_sframes_t codec_delay = 0; 1304 snd_pcm_sframes_t cpu_delay = 0; 1305 1306 offset = snd_soc_pcm_component_pointer(substream); 1307 1308 /* should be called *after* snd_soc_pcm_component_pointer() */ 1309 snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay); 1310 snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay); 1311 1312 runtime->delay = cpu_delay + codec_delay; 1313 1314 return offset; 1315 } 1316 1317 /* connect a FE and BE */ 1318 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 1319 struct snd_soc_pcm_runtime *be, int stream) 1320 { 1321 struct snd_pcm_substream *fe_substream; 1322 struct snd_pcm_substream *be_substream; 1323 struct snd_soc_dpcm *dpcm; 1324 1325 snd_soc_dpcm_mutex_assert_held(fe); 1326 1327 /* only add new dpcms */ 1328 for_each_dpcm_be(fe, stream, dpcm) { 1329 if (dpcm->be == be && dpcm->fe == fe) 1330 return 0; 1331 } 1332 1333 fe_substream = snd_soc_dpcm_get_substream(fe, stream); 1334 be_substream = snd_soc_dpcm_get_substream(be, stream); 1335 1336 if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) { 1337 dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n", 1338 __func__); 1339 return -EINVAL; 1340 } 1341 if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) { 1342 dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n"); 1343 be_substream->pcm->nonatomic = 1; 1344 } 1345 1346 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 1347 if (!dpcm) 1348 return -ENOMEM; 1349 1350 dpcm->be = be; 1351 dpcm->fe = fe; 1352 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1353 snd_pcm_stream_lock_irq(fe_substream); 1354 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 1355 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1356 snd_pcm_stream_unlock_irq(fe_substream); 1357 1358 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 1359 snd_pcm_direction_name(stream), fe->dai_link->name, 1360 stream ? "<-" : "->", be->dai_link->name); 1361 1362 dpcm_create_debugfs_state(dpcm, stream); 1363 1364 return 1; 1365 } 1366 1367 /* reparent a BE onto another FE */ 1368 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 1369 struct snd_soc_pcm_runtime *be, int stream) 1370 { 1371 struct snd_soc_dpcm *dpcm; 1372 struct snd_pcm_substream *fe_substream, *be_substream; 1373 1374 /* reparent if BE is connected to other FEs */ 1375 if (!be->dpcm[stream].users) 1376 return; 1377 1378 be_substream = snd_soc_dpcm_get_substream(be, stream); 1379 if (!be_substream) 1380 return; 1381 1382 for_each_dpcm_fe(be, stream, dpcm) { 1383 if (dpcm->fe == fe) 1384 continue; 1385 1386 dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 1387 snd_pcm_direction_name(stream), 1388 dpcm->fe->dai_link->name, 1389 stream ? "<-" : "->", dpcm->be->dai_link->name); 1390 1391 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 1392 be_substream->runtime = fe_substream->runtime; 1393 break; 1394 } 1395 } 1396 1397 /* disconnect a BE and FE */ 1398 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 1399 { 1400 struct snd_soc_dpcm *dpcm, *d; 1401 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream); 1402 LIST_HEAD(deleted_dpcms); 1403 1404 snd_soc_dpcm_mutex_assert_held(fe); 1405 1406 snd_pcm_stream_lock_irq(substream); 1407 for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1408 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 1409 snd_pcm_direction_name(stream), 1410 dpcm->be->dai_link->name); 1411 1412 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 1413 continue; 1414 1415 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 1416 snd_pcm_direction_name(stream), fe->dai_link->name, 1417 stream ? "<-" : "->", dpcm->be->dai_link->name); 1418 1419 /* BEs still alive need new FE */ 1420 dpcm_be_reparent(fe, dpcm->be, stream); 1421 1422 list_del(&dpcm->list_be); 1423 list_move(&dpcm->list_fe, &deleted_dpcms); 1424 } 1425 snd_pcm_stream_unlock_irq(substream); 1426 1427 while (!list_empty(&deleted_dpcms)) { 1428 dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm, 1429 list_fe); 1430 list_del(&dpcm->list_fe); 1431 dpcm_remove_debugfs_state(dpcm); 1432 kfree(dpcm); 1433 } 1434 } 1435 1436 /* get BE for DAI widget and stream */ 1437 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 1438 struct snd_soc_dapm_widget *widget, int stream) 1439 { 1440 struct snd_soc_pcm_runtime *be; 1441 struct snd_soc_dapm_widget *w; 1442 struct snd_soc_dai *dai; 1443 int i; 1444 1445 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 1446 1447 for_each_card_rtds(card, be) { 1448 1449 if (!be->dai_link->no_pcm) 1450 continue; 1451 1452 if (!snd_soc_dpcm_get_substream(be, stream)) 1453 continue; 1454 1455 for_each_rtd_dais(be, i, dai) { 1456 w = snd_soc_dai_get_widget(dai, stream); 1457 1458 dev_dbg(card->dev, "ASoC: try BE : %s\n", 1459 w ? w->name : "(not set)"); 1460 1461 if (w == widget) 1462 return be; 1463 } 1464 } 1465 1466 /* Widget provided is not a BE */ 1467 return NULL; 1468 } 1469 1470 int widget_in_list(struct snd_soc_dapm_widget_list *list, 1471 struct snd_soc_dapm_widget *widget) 1472 { 1473 struct snd_soc_dapm_widget *w; 1474 int i; 1475 1476 for_each_dapm_widgets(list, i, w) 1477 if (widget == w) 1478 return 1; 1479 1480 return 0; 1481 } 1482 EXPORT_SYMBOL_GPL(widget_in_list); 1483 1484 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir) 1485 { 1486 struct snd_soc_card *card = widget->dapm->card; 1487 struct snd_soc_pcm_runtime *rtd; 1488 int stream; 1489 1490 /* adjust dir to stream */ 1491 if (dir == SND_SOC_DAPM_DIR_OUT) 1492 stream = SNDRV_PCM_STREAM_PLAYBACK; 1493 else 1494 stream = SNDRV_PCM_STREAM_CAPTURE; 1495 1496 rtd = dpcm_get_be(card, widget, stream); 1497 if (rtd) 1498 return true; 1499 1500 return false; 1501 } 1502 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be); 1503 1504 int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 1505 int stream, struct snd_soc_dapm_widget_list **list) 1506 { 1507 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0); 1508 int paths; 1509 1510 if (fe->dai_link->num_cpus > 1) { 1511 dev_err(fe->dev, 1512 "%s doesn't support Multi CPU yet\n", __func__); 1513 return -EINVAL; 1514 } 1515 1516 /* get number of valid DAI paths and their widgets */ 1517 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 1518 fe->card->component_chaining ? 1519 NULL : dpcm_end_walk_at_be); 1520 1521 if (paths > 0) 1522 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 1523 snd_pcm_direction_name(stream)); 1524 else if (paths == 0) 1525 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name, 1526 snd_pcm_direction_name(stream)); 1527 1528 return paths; 1529 } 1530 1531 void dpcm_path_put(struct snd_soc_dapm_widget_list **list) 1532 { 1533 snd_soc_dapm_dai_free_widgets(list); 1534 } 1535 1536 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream, 1537 struct snd_soc_dapm_widget_list *list) 1538 { 1539 struct snd_soc_dai *dai; 1540 unsigned int i; 1541 1542 /* is there a valid DAI widget for this BE */ 1543 for_each_rtd_dais(dpcm->be, i, dai) { 1544 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream); 1545 1546 /* 1547 * The BE is pruned only if none of the dai 1548 * widgets are in the active list. 1549 */ 1550 if (widget && widget_in_list(list, widget)) 1551 return true; 1552 } 1553 1554 return false; 1555 } 1556 1557 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 1558 struct snd_soc_dapm_widget_list **list_) 1559 { 1560 struct snd_soc_dpcm *dpcm; 1561 int prune = 0; 1562 1563 /* Destroy any old FE <--> BE connections */ 1564 for_each_dpcm_be(fe, stream, dpcm) { 1565 if (dpcm_be_is_active(dpcm, stream, *list_)) 1566 continue; 1567 1568 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 1569 snd_pcm_direction_name(stream), 1570 dpcm->be->dai_link->name, fe->dai_link->name); 1571 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1572 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE); 1573 prune++; 1574 } 1575 1576 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 1577 return prune; 1578 } 1579 1580 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 1581 struct snd_soc_dapm_widget_list **list_) 1582 { 1583 struct snd_soc_card *card = fe->card; 1584 struct snd_soc_dapm_widget_list *list = *list_; 1585 struct snd_soc_pcm_runtime *be; 1586 struct snd_soc_dapm_widget *widget; 1587 struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream); 1588 int i, new = 0, err; 1589 1590 /* don't connect if FE is not running */ 1591 if (!fe_substream->runtime && !fe->fe_compr) 1592 return new; 1593 1594 /* Create any new FE <--> BE connections */ 1595 for_each_dapm_widgets(list, i, widget) { 1596 1597 switch (widget->id) { 1598 case snd_soc_dapm_dai_in: 1599 if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1600 continue; 1601 break; 1602 case snd_soc_dapm_dai_out: 1603 if (stream != SNDRV_PCM_STREAM_CAPTURE) 1604 continue; 1605 break; 1606 default: 1607 continue; 1608 } 1609 1610 /* is there a valid BE rtd for this widget */ 1611 be = dpcm_get_be(card, widget, stream); 1612 if (!be) { 1613 dev_dbg(fe->dev, "ASoC: no BE found for %s\n", 1614 widget->name); 1615 continue; 1616 } 1617 1618 /* 1619 * Filter for systems with 'component_chaining' enabled. 1620 * This helps to avoid unnecessary re-configuration of an 1621 * already active BE on such systems. 1622 */ 1623 if (fe->card->component_chaining && 1624 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 1625 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 1626 continue; 1627 1628 /* newly connected FE and BE */ 1629 err = dpcm_be_connect(fe, be, stream); 1630 if (err < 0) { 1631 dev_err(fe->dev, "ASoC: can't connect %s\n", 1632 widget->name); 1633 break; 1634 } else if (err == 0) /* already connected */ 1635 continue; 1636 1637 /* new */ 1638 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE); 1639 new++; 1640 } 1641 1642 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 1643 return new; 1644 } 1645 1646 /* 1647 * Find the corresponding BE DAIs that source or sink audio to this 1648 * FE substream. 1649 */ 1650 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 1651 int stream, struct snd_soc_dapm_widget_list **list, int new) 1652 { 1653 if (new) 1654 return dpcm_add_paths(fe, stream, list); 1655 else 1656 return dpcm_prune_paths(fe, stream, list); 1657 } 1658 1659 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 1660 { 1661 struct snd_soc_dpcm *dpcm; 1662 1663 for_each_dpcm_be(fe, stream, dpcm) 1664 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO); 1665 } 1666 1667 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream, 1668 int do_hw_free, struct snd_soc_dpcm *last) 1669 { 1670 struct snd_soc_dpcm *dpcm; 1671 1672 /* disable any enabled and non active backends */ 1673 for_each_dpcm_be(fe, stream, dpcm) { 1674 struct snd_soc_pcm_runtime *be = dpcm->be; 1675 struct snd_pcm_substream *be_substream = 1676 snd_soc_dpcm_get_substream(be, stream); 1677 1678 if (dpcm == last) 1679 return; 1680 1681 /* is this op for this BE ? */ 1682 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1683 continue; 1684 1685 if (be->dpcm[stream].users == 0) { 1686 dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 1687 snd_pcm_direction_name(stream), 1688 be->dpcm[stream].state); 1689 continue; 1690 } 1691 1692 if (--be->dpcm[stream].users != 0) 1693 continue; 1694 1695 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) { 1696 if (!do_hw_free) 1697 continue; 1698 1699 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) { 1700 __soc_pcm_hw_free(be, be_substream); 1701 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 1702 } 1703 } 1704 1705 __soc_pcm_close(be, be_substream); 1706 be_substream->runtime = NULL; 1707 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1708 } 1709 } 1710 1711 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 1712 { 1713 struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream); 1714 struct snd_soc_pcm_runtime *be; 1715 struct snd_soc_dpcm *dpcm; 1716 int err, count = 0; 1717 1718 /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 1719 for_each_dpcm_be(fe, stream, dpcm) { 1720 struct snd_pcm_substream *be_substream; 1721 1722 be = dpcm->be; 1723 be_substream = snd_soc_dpcm_get_substream(be, stream); 1724 1725 if (!be_substream) { 1726 dev_err(be->dev, "ASoC: no backend %s stream\n", 1727 snd_pcm_direction_name(stream)); 1728 continue; 1729 } 1730 1731 /* is this op for this BE ? */ 1732 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 1733 continue; 1734 1735 /* first time the dpcm is open ? */ 1736 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) { 1737 dev_err(be->dev, "ASoC: too many users %s at open %d\n", 1738 snd_pcm_direction_name(stream), 1739 be->dpcm[stream].state); 1740 continue; 1741 } 1742 1743 if (be->dpcm[stream].users++ != 0) 1744 continue; 1745 1746 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 1747 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 1748 continue; 1749 1750 dev_dbg(be->dev, "ASoC: open %s BE %s\n", 1751 snd_pcm_direction_name(stream), be->dai_link->name); 1752 1753 be_substream->runtime = fe_substream->runtime; 1754 err = __soc_pcm_open(be, be_substream); 1755 if (err < 0) { 1756 be->dpcm[stream].users--; 1757 if (be->dpcm[stream].users < 0) 1758 dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 1759 snd_pcm_direction_name(stream), 1760 be->dpcm[stream].state); 1761 1762 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1763 goto unwind; 1764 } 1765 be->dpcm[stream].be_start = 0; 1766 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1767 count++; 1768 } 1769 1770 return count; 1771 1772 unwind: 1773 dpcm_be_dai_startup_rollback(fe, stream, dpcm); 1774 1775 return soc_pcm_ret(fe, err); 1776 } 1777 1778 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream) 1779 { 1780 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 1781 struct snd_pcm_runtime *runtime = substream->runtime; 1782 struct snd_pcm_hardware *hw = &runtime->hw; 1783 struct snd_soc_dai *dai; 1784 int stream = substream->stream; 1785 u64 formats = hw->formats; 1786 int i; 1787 1788 soc_pcm_hw_init(hw); 1789 1790 if (formats) 1791 hw->formats &= formats; 1792 1793 for_each_rtd_cpu_dais(fe, i, dai) { 1794 const struct snd_soc_pcm_stream *cpu_stream; 1795 1796 /* 1797 * Skip CPUs which don't support the current stream 1798 * type. See soc_pcm_init_runtime_hw() for more details 1799 */ 1800 if (!snd_soc_dai_stream_valid(dai, stream)) 1801 continue; 1802 1803 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1804 1805 soc_pcm_hw_update_rate(hw, cpu_stream); 1806 soc_pcm_hw_update_chan(hw, cpu_stream); 1807 soc_pcm_hw_update_format(hw, cpu_stream); 1808 soc_pcm_hw_update_subformat(hw, cpu_stream); 1809 } 1810 1811 } 1812 1813 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream) 1814 { 1815 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 1816 struct snd_pcm_runtime *runtime = substream->runtime; 1817 struct snd_pcm_hardware *hw = &runtime->hw; 1818 struct snd_soc_dpcm *dpcm; 1819 struct snd_soc_dai *dai; 1820 int stream = substream->stream; 1821 1822 if (!fe->dai_link->dpcm_merged_format) 1823 return; 1824 1825 /* 1826 * It returns merged BE codec format 1827 * if FE want to use it (= dpcm_merged_format) 1828 */ 1829 1830 for_each_dpcm_be(fe, stream, dpcm) { 1831 struct snd_soc_pcm_runtime *be = dpcm->be; 1832 const struct snd_soc_pcm_stream *codec_stream; 1833 int i; 1834 1835 for_each_rtd_codec_dais(be, i, dai) { 1836 /* 1837 * Skip CODECs which don't support the current stream 1838 * type. See soc_pcm_init_runtime_hw() for more details 1839 */ 1840 if (!snd_soc_dai_stream_valid(dai, stream)) 1841 continue; 1842 1843 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1844 1845 soc_pcm_hw_update_format(hw, codec_stream); 1846 soc_pcm_hw_update_subformat(hw, codec_stream); 1847 } 1848 } 1849 } 1850 1851 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream) 1852 { 1853 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 1854 struct snd_pcm_runtime *runtime = substream->runtime; 1855 struct snd_pcm_hardware *hw = &runtime->hw; 1856 struct snd_soc_dpcm *dpcm; 1857 int stream = substream->stream; 1858 1859 if (!fe->dai_link->dpcm_merged_chan) 1860 return; 1861 1862 /* 1863 * It returns merged BE codec channel; 1864 * if FE want to use it (= dpcm_merged_chan) 1865 */ 1866 1867 for_each_dpcm_be(fe, stream, dpcm) { 1868 struct snd_soc_pcm_runtime *be = dpcm->be; 1869 const struct snd_soc_pcm_stream *cpu_stream; 1870 struct snd_soc_dai *dai; 1871 int i; 1872 1873 for_each_rtd_cpu_dais(be, i, dai) { 1874 /* 1875 * Skip CPUs which don't support the current stream 1876 * type. See soc_pcm_init_runtime_hw() for more details 1877 */ 1878 if (!snd_soc_dai_stream_valid(dai, stream)) 1879 continue; 1880 1881 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1882 1883 soc_pcm_hw_update_chan(hw, cpu_stream); 1884 } 1885 1886 /* 1887 * chan min/max cannot be enforced if there are multiple CODEC 1888 * DAIs connected to a single CPU DAI, use CPU DAI's directly 1889 */ 1890 if (be->dai_link->num_codecs == 1) { 1891 const struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream( 1892 snd_soc_rtd_to_codec(be, 0), stream); 1893 1894 soc_pcm_hw_update_chan(hw, codec_stream); 1895 } 1896 } 1897 } 1898 1899 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream) 1900 { 1901 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 1902 struct snd_pcm_runtime *runtime = substream->runtime; 1903 struct snd_pcm_hardware *hw = &runtime->hw; 1904 struct snd_soc_dpcm *dpcm; 1905 int stream = substream->stream; 1906 1907 if (!fe->dai_link->dpcm_merged_rate) 1908 return; 1909 1910 /* 1911 * It returns merged BE codec channel; 1912 * if FE want to use it (= dpcm_merged_chan) 1913 */ 1914 1915 for_each_dpcm_be(fe, stream, dpcm) { 1916 struct snd_soc_pcm_runtime *be = dpcm->be; 1917 const struct snd_soc_pcm_stream *pcm; 1918 struct snd_soc_dai *dai; 1919 int i; 1920 1921 for_each_rtd_dais(be, i, dai) { 1922 /* 1923 * Skip DAIs which don't support the current stream 1924 * type. See soc_pcm_init_runtime_hw() for more details 1925 */ 1926 if (!snd_soc_dai_stream_valid(dai, stream)) 1927 continue; 1928 1929 pcm = snd_soc_dai_get_pcm_stream(dai, stream); 1930 1931 soc_pcm_hw_update_rate(hw, pcm); 1932 } 1933 } 1934 } 1935 1936 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 1937 int stream) 1938 { 1939 struct snd_soc_dpcm *dpcm; 1940 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream); 1941 struct snd_soc_dai *fe_cpu_dai; 1942 int err = 0; 1943 int i; 1944 1945 /* apply symmetry for FE */ 1946 soc_pcm_update_symmetry(fe_substream); 1947 1948 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) { 1949 /* Symmetry only applies if we've got an active stream. */ 1950 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 1951 if (err < 0) 1952 goto error; 1953 } 1954 1955 /* apply symmetry for BE */ 1956 for_each_dpcm_be(fe, stream, dpcm) { 1957 struct snd_soc_pcm_runtime *be = dpcm->be; 1958 struct snd_pcm_substream *be_substream = 1959 snd_soc_dpcm_get_substream(be, stream); 1960 struct snd_soc_pcm_runtime *rtd; 1961 struct snd_soc_dai *dai; 1962 1963 /* A backend may not have the requested substream */ 1964 if (!be_substream) 1965 continue; 1966 1967 rtd = snd_soc_substream_to_rtd(be_substream); 1968 if (rtd->dai_link->be_hw_params_fixup) 1969 continue; 1970 1971 soc_pcm_update_symmetry(be_substream); 1972 1973 /* Symmetry only applies if we've got an active stream. */ 1974 for_each_rtd_dais(rtd, i, dai) { 1975 err = soc_pcm_apply_symmetry(fe_substream, dai); 1976 if (err < 0) 1977 goto error; 1978 } 1979 } 1980 error: 1981 return soc_pcm_ret(fe, err); 1982 } 1983 1984 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 1985 { 1986 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream); 1987 int stream = fe_substream->stream, ret = 0; 1988 1989 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 1990 1991 ret = dpcm_be_dai_startup(fe, stream); 1992 if (ret < 0) 1993 goto be_err; 1994 1995 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 1996 1997 /* start the DAI frontend */ 1998 ret = __soc_pcm_open(fe, fe_substream); 1999 if (ret < 0) 2000 goto unwind; 2001 2002 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 2003 2004 dpcm_runtime_setup_fe(fe_substream); 2005 2006 dpcm_runtime_setup_be_format(fe_substream); 2007 dpcm_runtime_setup_be_chan(fe_substream); 2008 dpcm_runtime_setup_be_rate(fe_substream); 2009 2010 ret = dpcm_apply_symmetry(fe_substream, stream); 2011 2012 unwind: 2013 if (ret < 0) 2014 dpcm_be_dai_startup_unwind(fe, stream); 2015 be_err: 2016 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2017 2018 return soc_pcm_ret(fe, ret); 2019 } 2020 2021 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 2022 { 2023 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2024 int stream = substream->stream; 2025 2026 snd_soc_dpcm_mutex_assert_held(fe); 2027 2028 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2029 2030 /* shutdown the BEs */ 2031 dpcm_be_dai_shutdown(fe, stream); 2032 2033 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 2034 2035 /* now shutdown the frontend */ 2036 __soc_pcm_close(fe, substream); 2037 2038 /* run the stream stop event */ 2039 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 2040 2041 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2042 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2043 return 0; 2044 } 2045 2046 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 2047 { 2048 struct snd_soc_dpcm *dpcm; 2049 2050 /* only hw_params backends that are either sinks or sources 2051 * to this frontend DAI */ 2052 for_each_dpcm_be(fe, stream, dpcm) { 2053 2054 struct snd_soc_pcm_runtime *be = dpcm->be; 2055 struct snd_pcm_substream *be_substream = 2056 snd_soc_dpcm_get_substream(be, stream); 2057 2058 /* is this op for this BE ? */ 2059 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2060 continue; 2061 2062 /* only free hw when no longer used - check all FEs */ 2063 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2064 continue; 2065 2066 /* do not free hw if this BE is used by other FE */ 2067 if (be->dpcm[stream].users > 1) 2068 continue; 2069 2070 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2071 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 2072 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 2073 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 2074 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 2075 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 2076 continue; 2077 2078 dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 2079 be->dai_link->name); 2080 2081 __soc_pcm_hw_free(be, be_substream); 2082 2083 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2084 } 2085 } 2086 2087 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 2088 { 2089 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2090 int stream = substream->stream; 2091 2092 snd_soc_dpcm_mutex_lock(fe); 2093 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2094 2095 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 2096 2097 /* call hw_free on the frontend */ 2098 soc_pcm_hw_clean(fe, substream, 0); 2099 2100 /* only hw_params backends that are either sinks or sources 2101 * to this frontend DAI */ 2102 dpcm_be_dai_hw_free(fe, stream); 2103 2104 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2105 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2106 2107 snd_soc_dpcm_mutex_unlock(fe); 2108 return 0; 2109 } 2110 2111 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 2112 { 2113 struct snd_soc_pcm_runtime *be; 2114 struct snd_pcm_substream *be_substream; 2115 struct snd_soc_dpcm *dpcm; 2116 int ret; 2117 2118 for_each_dpcm_be(fe, stream, dpcm) { 2119 struct snd_pcm_hw_params hw_params; 2120 2121 be = dpcm->be; 2122 be_substream = snd_soc_dpcm_get_substream(be, stream); 2123 2124 /* is this op for this BE ? */ 2125 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2126 continue; 2127 2128 /* copy params for each dpcm */ 2129 memcpy(&hw_params, &fe->dpcm[stream].hw_params, 2130 sizeof(struct snd_pcm_hw_params)); 2131 2132 /* perform any hw_params fixups */ 2133 ret = snd_soc_link_be_hw_params_fixup(be, &hw_params); 2134 if (ret < 0) 2135 goto unwind; 2136 2137 /* copy the fixed-up hw params for BE dai */ 2138 memcpy(&be->dpcm[stream].hw_params, &hw_params, 2139 sizeof(struct snd_pcm_hw_params)); 2140 2141 /* only allow hw_params() if no connected FEs are running */ 2142 if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2143 continue; 2144 2145 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2146 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2147 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2148 continue; 2149 2150 dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 2151 be->dai_link->name); 2152 2153 ret = __soc_pcm_hw_params(be, be_substream, &hw_params); 2154 if (ret < 0) 2155 goto unwind; 2156 2157 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 2158 } 2159 return 0; 2160 2161 unwind: 2162 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n", 2163 __func__, be->dai_link->name, ret); 2164 2165 /* disable any enabled and non active backends */ 2166 for_each_dpcm_be_rollback(fe, stream, dpcm) { 2167 be = dpcm->be; 2168 be_substream = snd_soc_dpcm_get_substream(be, stream); 2169 2170 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2171 continue; 2172 2173 /* only allow hw_free() if no connected FEs are running */ 2174 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 2175 continue; 2176 2177 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2178 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2179 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 2180 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 2181 continue; 2182 2183 __soc_pcm_hw_free(be, be_substream); 2184 } 2185 2186 return ret; 2187 } 2188 2189 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 2190 struct snd_pcm_hw_params *params) 2191 { 2192 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2193 int ret, stream = substream->stream; 2194 2195 snd_soc_dpcm_mutex_lock(fe); 2196 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2197 2198 memcpy(&fe->dpcm[stream].hw_params, params, 2199 sizeof(struct snd_pcm_hw_params)); 2200 ret = dpcm_be_dai_hw_params(fe, stream); 2201 if (ret < 0) 2202 goto out; 2203 2204 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 2205 fe->dai_link->name, params_rate(params), 2206 params_channels(params), params_format(params)); 2207 2208 /* call hw_params on the frontend */ 2209 ret = __soc_pcm_hw_params(fe, substream, params); 2210 if (ret < 0) 2211 dpcm_be_dai_hw_free(fe, stream); 2212 else 2213 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 2214 2215 out: 2216 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2217 snd_soc_dpcm_mutex_unlock(fe); 2218 2219 return soc_pcm_ret(fe, ret); 2220 } 2221 2222 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 2223 int cmd) 2224 { 2225 struct snd_soc_pcm_runtime *be; 2226 bool pause_stop_transition; 2227 struct snd_soc_dpcm *dpcm; 2228 unsigned long flags; 2229 int ret = 0; 2230 2231 for_each_dpcm_be(fe, stream, dpcm) { 2232 struct snd_pcm_substream *be_substream; 2233 2234 be = dpcm->be; 2235 be_substream = snd_soc_dpcm_get_substream(be, stream); 2236 2237 snd_pcm_stream_lock_irqsave_nested(be_substream, flags); 2238 2239 /* is this op for this BE ? */ 2240 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2241 goto next; 2242 2243 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n", 2244 be->dai_link->name, cmd); 2245 2246 switch (cmd) { 2247 case SNDRV_PCM_TRIGGER_START: 2248 if (!be->dpcm[stream].be_start && 2249 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 2250 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 2251 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 2252 goto next; 2253 2254 be->dpcm[stream].be_start++; 2255 if (be->dpcm[stream].be_start != 1) 2256 goto next; 2257 2258 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED) 2259 ret = soc_pcm_trigger(be_substream, 2260 SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 2261 else 2262 ret = soc_pcm_trigger(be_substream, 2263 SNDRV_PCM_TRIGGER_START); 2264 if (ret) { 2265 be->dpcm[stream].be_start--; 2266 goto next; 2267 } 2268 2269 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2270 break; 2271 case SNDRV_PCM_TRIGGER_RESUME: 2272 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 2273 goto next; 2274 2275 be->dpcm[stream].be_start++; 2276 if (be->dpcm[stream].be_start != 1) 2277 goto next; 2278 2279 ret = soc_pcm_trigger(be_substream, cmd); 2280 if (ret) { 2281 be->dpcm[stream].be_start--; 2282 goto next; 2283 } 2284 2285 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2286 break; 2287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2288 if (!be->dpcm[stream].be_start && 2289 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) && 2290 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 2291 goto next; 2292 2293 fe->dpcm[stream].fe_pause = false; 2294 be->dpcm[stream].be_pause--; 2295 2296 be->dpcm[stream].be_start++; 2297 if (be->dpcm[stream].be_start != 1) 2298 goto next; 2299 2300 ret = soc_pcm_trigger(be_substream, cmd); 2301 if (ret) { 2302 be->dpcm[stream].be_start--; 2303 goto next; 2304 } 2305 2306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2307 break; 2308 case SNDRV_PCM_TRIGGER_STOP: 2309 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) && 2310 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 2311 goto next; 2312 2313 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START) 2314 be->dpcm[stream].be_start--; 2315 2316 if (be->dpcm[stream].be_start != 0) 2317 goto next; 2318 2319 pause_stop_transition = false; 2320 if (fe->dpcm[stream].fe_pause) { 2321 pause_stop_transition = true; 2322 fe->dpcm[stream].fe_pause = false; 2323 be->dpcm[stream].be_pause--; 2324 } 2325 2326 if (be->dpcm[stream].be_pause != 0) 2327 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); 2328 else 2329 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP); 2330 2331 if (ret) { 2332 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START) 2333 be->dpcm[stream].be_start++; 2334 if (pause_stop_transition) { 2335 fe->dpcm[stream].fe_pause = true; 2336 be->dpcm[stream].be_pause++; 2337 } 2338 goto next; 2339 } 2340 2341 if (be->dpcm[stream].be_pause != 0) 2342 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2343 else 2344 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 2345 2346 break; 2347 case SNDRV_PCM_TRIGGER_SUSPEND: 2348 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2349 goto next; 2350 2351 be->dpcm[stream].be_start--; 2352 if (be->dpcm[stream].be_start != 0) 2353 goto next; 2354 2355 ret = soc_pcm_trigger(be_substream, cmd); 2356 if (ret) { 2357 be->dpcm[stream].be_start++; 2358 goto next; 2359 } 2360 2361 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 2362 break; 2363 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2364 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2365 goto next; 2366 2367 fe->dpcm[stream].fe_pause = true; 2368 be->dpcm[stream].be_pause++; 2369 2370 be->dpcm[stream].be_start--; 2371 if (be->dpcm[stream].be_start != 0) 2372 goto next; 2373 2374 ret = soc_pcm_trigger(be_substream, cmd); 2375 if (ret) { 2376 be->dpcm[stream].be_start++; 2377 goto next; 2378 } 2379 2380 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2381 break; 2382 } 2383 next: 2384 snd_pcm_stream_unlock_irqrestore(be_substream, flags); 2385 if (ret) 2386 break; 2387 } 2388 return soc_pcm_ret(fe, ret); 2389 } 2390 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 2391 2392 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, 2393 int cmd, bool fe_first) 2394 { 2395 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2396 int ret; 2397 2398 /* call trigger on the frontend before the backend. */ 2399 if (fe_first) { 2400 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2401 fe->dai_link->name, cmd); 2402 2403 ret = soc_pcm_trigger(substream, cmd); 2404 if (ret < 0) 2405 return ret; 2406 2407 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2408 return ret; 2409 } 2410 2411 /* call trigger on the frontend after the backend. */ 2412 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2413 if (ret < 0) 2414 return ret; 2415 2416 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2417 fe->dai_link->name, cmd); 2418 2419 ret = soc_pcm_trigger(substream, cmd); 2420 2421 return ret; 2422 } 2423 2424 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 2425 { 2426 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2427 int stream = substream->stream; 2428 int ret = 0; 2429 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2430 2431 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 2432 2433 switch (trigger) { 2434 case SND_SOC_DPCM_TRIGGER_PRE: 2435 switch (cmd) { 2436 case SNDRV_PCM_TRIGGER_START: 2437 case SNDRV_PCM_TRIGGER_RESUME: 2438 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2439 case SNDRV_PCM_TRIGGER_DRAIN: 2440 ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2441 break; 2442 case SNDRV_PCM_TRIGGER_STOP: 2443 case SNDRV_PCM_TRIGGER_SUSPEND: 2444 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2445 ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2446 break; 2447 default: 2448 ret = -EINVAL; 2449 break; 2450 } 2451 break; 2452 case SND_SOC_DPCM_TRIGGER_POST: 2453 switch (cmd) { 2454 case SNDRV_PCM_TRIGGER_START: 2455 case SNDRV_PCM_TRIGGER_RESUME: 2456 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2457 case SNDRV_PCM_TRIGGER_DRAIN: 2458 ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2459 break; 2460 case SNDRV_PCM_TRIGGER_STOP: 2461 case SNDRV_PCM_TRIGGER_SUSPEND: 2462 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2463 ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2464 break; 2465 default: 2466 ret = -EINVAL; 2467 break; 2468 } 2469 break; 2470 default: 2471 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 2472 fe->dai_link->name); 2473 ret = -EINVAL; 2474 goto out; 2475 } 2476 2477 if (ret < 0) { 2478 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", 2479 cmd, ret); 2480 goto out; 2481 } 2482 2483 switch (cmd) { 2484 case SNDRV_PCM_TRIGGER_START: 2485 case SNDRV_PCM_TRIGGER_RESUME: 2486 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2487 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2488 break; 2489 case SNDRV_PCM_TRIGGER_STOP: 2490 case SNDRV_PCM_TRIGGER_SUSPEND: 2491 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 2492 break; 2493 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2494 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2495 break; 2496 } 2497 2498 out: 2499 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 2500 return ret; 2501 } 2502 2503 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2504 { 2505 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2506 int stream = substream->stream; 2507 2508 /* if FE's runtime_update is already set, we're in race; 2509 * process this trigger later at exit 2510 */ 2511 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2512 fe->dpcm[stream].trigger_pending = cmd + 1; 2513 return 0; /* delayed, assuming it's successful */ 2514 } 2515 2516 /* we're alone, let's trigger */ 2517 return dpcm_fe_dai_do_trigger(substream, cmd); 2518 } 2519 2520 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 2521 { 2522 struct snd_soc_dpcm *dpcm; 2523 int ret = 0; 2524 2525 for_each_dpcm_be(fe, stream, dpcm) { 2526 2527 struct snd_soc_pcm_runtime *be = dpcm->be; 2528 struct snd_pcm_substream *be_substream = 2529 snd_soc_dpcm_get_substream(be, stream); 2530 2531 /* is this op for this BE ? */ 2532 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2533 continue; 2534 2535 if (!snd_soc_dpcm_can_be_prepared(fe, be, stream)) 2536 continue; 2537 2538 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2539 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 2540 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && 2541 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 2542 continue; 2543 2544 dev_dbg(be->dev, "ASoC: prepare BE %s\n", 2545 be->dai_link->name); 2546 2547 ret = __soc_pcm_prepare(be, be_substream); 2548 if (ret < 0) 2549 break; 2550 2551 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 2552 } 2553 2554 /* 2555 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity 2556 * 2557 * We don't want to log an error since we do not want to give userspace a way to do a 2558 * denial-of-service attack on the syslog / diskspace. 2559 */ 2560 return ret; 2561 } 2562 2563 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 2564 { 2565 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream); 2566 int stream = substream->stream, ret = 0; 2567 2568 snd_soc_dpcm_mutex_lock(fe); 2569 2570 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 2571 2572 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 2573 2574 /* there is no point preparing this FE if there are no BEs */ 2575 if (list_empty(&fe->dpcm[stream].be_clients)) { 2576 /* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles */ 2577 dev_err_once(fe->dev, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n", 2578 fe->dai_link->name); 2579 dev_dbg(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 2580 fe->dai_link->name); 2581 ret = -EINVAL; 2582 goto out; 2583 } 2584 2585 ret = dpcm_be_dai_prepare(fe, stream); 2586 if (ret < 0) 2587 goto out; 2588 2589 /* call prepare on the frontend */ 2590 ret = __soc_pcm_prepare(fe, substream); 2591 if (ret < 0) 2592 goto out; 2593 2594 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 2595 2596 out: 2597 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2598 snd_soc_dpcm_mutex_unlock(fe); 2599 2600 /* 2601 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity 2602 * 2603 * We don't want to log an error since we do not want to give userspace a way to do a 2604 * denial-of-service attack on the syslog / diskspace. 2605 */ 2606 return ret; 2607 } 2608 2609 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2610 { 2611 int err; 2612 2613 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2614 snd_pcm_direction_name(stream), fe->dai_link->name); 2615 2616 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2617 2618 dpcm_be_dai_hw_free(fe, stream); 2619 2620 dpcm_be_dai_shutdown(fe, stream); 2621 2622 /* run the stream event for each BE */ 2623 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2624 2625 return soc_pcm_ret(fe, err); 2626 } 2627 2628 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2629 { 2630 struct snd_soc_dpcm *dpcm; 2631 int ret = 0; 2632 2633 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2634 snd_pcm_direction_name(stream), fe->dai_link->name); 2635 2636 /* Only start the BE if the FE is ready */ 2637 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2638 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) { 2639 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n", 2640 fe->dai_link->name, fe->dpcm[stream].state); 2641 ret = -EINVAL; 2642 goto disconnect; 2643 } 2644 2645 /* startup must always be called for new BEs */ 2646 ret = dpcm_be_dai_startup(fe, stream); 2647 if (ret < 0) 2648 goto disconnect; 2649 2650 /* keep going if FE state is > open */ 2651 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2652 return 0; 2653 2654 ret = dpcm_be_dai_hw_params(fe, stream); 2655 if (ret < 0) 2656 goto close; 2657 2658 /* keep going if FE state is > hw_params */ 2659 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2660 return 0; 2661 2662 ret = dpcm_be_dai_prepare(fe, stream); 2663 if (ret < 0) 2664 goto hw_free; 2665 2666 /* run the stream event for each BE */ 2667 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2668 2669 /* keep going if FE state is > prepare */ 2670 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2671 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2672 return 0; 2673 2674 ret = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_START); 2675 if (ret < 0) 2676 goto hw_free; 2677 2678 return 0; 2679 2680 hw_free: 2681 dpcm_be_dai_hw_free(fe, stream); 2682 close: 2683 dpcm_be_dai_shutdown(fe, stream); 2684 disconnect: 2685 /* disconnect any pending BEs */ 2686 for_each_dpcm_be(fe, stream, dpcm) { 2687 struct snd_soc_pcm_runtime *be = dpcm->be; 2688 2689 /* is this op for this BE ? */ 2690 if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 2691 continue; 2692 2693 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE || 2694 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW) 2695 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2696 } 2697 2698 return soc_pcm_ret(fe, ret); 2699 } 2700 2701 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2702 { 2703 struct snd_soc_dapm_widget_list *list; 2704 int stream; 2705 int count, paths; 2706 2707 if (!fe->dai_link->dynamic) 2708 return 0; 2709 2710 if (fe->dai_link->num_cpus > 1) { 2711 dev_err(fe->dev, 2712 "%s doesn't support Multi CPU yet\n", __func__); 2713 return -EINVAL; 2714 } 2715 2716 /* only check active links */ 2717 if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe, 0))) 2718 return 0; 2719 2720 /* DAPM sync will call this to update DSP paths */ 2721 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2722 new ? "new" : "old", fe->dai_link->name); 2723 2724 for_each_pcm_streams(stream) { 2725 2726 /* skip if FE doesn't have playback/capture capability */ 2727 if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0), stream) || 2728 !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe, 0), stream)) 2729 continue; 2730 2731 /* skip if FE isn't currently playing/capturing */ 2732 if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe, 0), stream) || 2733 !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe, 0), stream)) 2734 continue; 2735 2736 paths = dpcm_path_get(fe, stream, &list); 2737 if (paths < 0) 2738 return paths; 2739 2740 /* update any playback/capture paths */ 2741 count = dpcm_process_paths(fe, stream, &list, new); 2742 if (count) { 2743 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2744 if (new) 2745 dpcm_run_update_startup(fe, stream); 2746 else 2747 dpcm_run_update_shutdown(fe, stream); 2748 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2749 2750 dpcm_clear_pending_state(fe, stream); 2751 dpcm_be_disconnect(fe, stream); 2752 } 2753 2754 dpcm_path_put(&list); 2755 } 2756 2757 return 0; 2758 } 2759 2760 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2761 * any DAI links. 2762 */ 2763 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card) 2764 { 2765 struct snd_soc_pcm_runtime *fe; 2766 int ret = 0; 2767 2768 snd_soc_dpcm_mutex_lock(card); 2769 /* shutdown all old paths first */ 2770 for_each_card_rtds(card, fe) { 2771 ret = soc_dpcm_fe_runtime_update(fe, 0); 2772 if (ret) 2773 goto out; 2774 } 2775 2776 /* bring new paths up */ 2777 for_each_card_rtds(card, fe) { 2778 ret = soc_dpcm_fe_runtime_update(fe, 1); 2779 if (ret) 2780 goto out; 2781 } 2782 2783 out: 2784 snd_soc_dpcm_mutex_unlock(card); 2785 return ret; 2786 } 2787 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update); 2788 2789 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream) 2790 { 2791 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream); 2792 struct snd_soc_dpcm *dpcm; 2793 int stream = fe_substream->stream; 2794 2795 snd_soc_dpcm_mutex_assert_held(fe); 2796 2797 /* mark FE's links ready to prune */ 2798 for_each_dpcm_be(fe, stream, dpcm) 2799 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2800 2801 dpcm_be_disconnect(fe, stream); 2802 } 2803 2804 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 2805 { 2806 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream); 2807 int ret; 2808 2809 snd_soc_dpcm_mutex_lock(fe); 2810 ret = dpcm_fe_dai_shutdown(fe_substream); 2811 2812 dpcm_fe_dai_cleanup(fe_substream); 2813 2814 snd_soc_dpcm_mutex_unlock(fe); 2815 return ret; 2816 } 2817 2818 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 2819 { 2820 struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream); 2821 struct snd_soc_dapm_widget_list *list; 2822 int ret; 2823 int stream = fe_substream->stream; 2824 2825 snd_soc_dpcm_mutex_lock(fe); 2826 2827 ret = dpcm_path_get(fe, stream, &list); 2828 if (ret < 0) 2829 goto open_end; 2830 2831 /* calculate valid and active FE <-> BE dpcms */ 2832 dpcm_process_paths(fe, stream, &list, 1); 2833 2834 ret = dpcm_fe_dai_startup(fe_substream); 2835 if (ret < 0) 2836 dpcm_fe_dai_cleanup(fe_substream); 2837 2838 dpcm_clear_pending_state(fe, stream); 2839 dpcm_path_put(&list); 2840 open_end: 2841 snd_soc_dpcm_mutex_unlock(fe); 2842 return ret; 2843 } 2844 2845 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd, 2846 int *playback, int *capture) 2847 { 2848 struct snd_soc_dai_link *dai_link = rtd->dai_link; 2849 struct snd_soc_dai *cpu_dai; 2850 struct snd_soc_dai *codec_dai; 2851 struct snd_soc_dai_link_ch_map *ch_maps; 2852 struct snd_soc_dai *dummy_dai = snd_soc_find_dai(&snd_soc_dummy_dlc); 2853 int cpu_capture; 2854 int cpu_playback; 2855 int has_playback = 0; 2856 int has_capture = 0; 2857 int i; 2858 2859 if (dai_link->dynamic && dai_link->num_cpus > 1) { 2860 dev_err(rtd->dev, "DPCM doesn't support Multi CPU for Front-Ends yet\n"); 2861 return -EINVAL; 2862 } 2863 2864 /* Adapt stream for codec2codec links */ 2865 cpu_capture = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_CAPTURE); 2866 cpu_playback = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_PLAYBACK); 2867 2868 /* 2869 * see 2870 * soc.h :: [dai_link->ch_maps Image sample] 2871 */ 2872 for_each_rtd_ch_maps(rtd, i, ch_maps) { 2873 cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu); 2874 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec); 2875 2876 /* 2877 * FIXME 2878 * 2879 * DPCM Codec has been no checked before. 2880 * It should be checked, but it breaks compatibility. 2881 * 2882 * For example there is a case that CPU have loopback capabilities which is used 2883 * for tests on boards where the Codec has no capture capabilities. In this case, 2884 * Codec capture validation check will be fail, but system should allow capture 2885 * capabilities. We have no solution for it today. 2886 */ 2887 if (dai_link->dynamic || dai_link->no_pcm) 2888 codec_dai = dummy_dai; 2889 2890 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 2891 snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) 2892 has_playback = 1; 2893 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 2894 snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) 2895 has_capture = 1; 2896 } 2897 2898 if (dai_link->playback_only) 2899 has_capture = 0; 2900 2901 if (dai_link->capture_only) 2902 has_playback = 0; 2903 2904 if (!has_playback && !has_capture) { 2905 dev_err(rtd->dev, "substream %s has no playback, no capture\n", 2906 dai_link->stream_name); 2907 2908 return -EINVAL; 2909 } 2910 2911 *playback = has_playback; 2912 *capture = has_capture; 2913 2914 return 0; 2915 } 2916 2917 static int soc_create_pcm(struct snd_pcm **pcm, 2918 struct snd_soc_pcm_runtime *rtd, 2919 int playback, int capture) 2920 { 2921 char new_name[64]; 2922 int ret; 2923 2924 /* create the PCM */ 2925 if (rtd->dai_link->c2c_params) { 2926 snprintf(new_name, sizeof(new_name), "codec2codec(%s)", 2927 rtd->dai_link->stream_name); 2928 2929 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id, 2930 playback, capture, pcm); 2931 } else if (rtd->dai_link->no_pcm) { 2932 snprintf(new_name, sizeof(new_name), "(%s)", 2933 rtd->dai_link->stream_name); 2934 2935 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id, 2936 playback, capture, pcm); 2937 } else { 2938 if (rtd->dai_link->dynamic) 2939 snprintf(new_name, sizeof(new_name), "%s (*)", 2940 rtd->dai_link->stream_name); 2941 else 2942 snprintf(new_name, sizeof(new_name), "%s %s-%d", 2943 rtd->dai_link->stream_name, 2944 soc_codec_dai_name(rtd), rtd->id); 2945 2946 ret = snd_pcm_new(rtd->card->snd_card, new_name, rtd->id, playback, 2947 capture, pcm); 2948 } 2949 if (ret < 0) { 2950 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n", 2951 new_name, rtd->dai_link->name, ret); 2952 return ret; 2953 } 2954 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n", rtd->id, new_name); 2955 2956 return 0; 2957 } 2958 2959 /* create a new pcm */ 2960 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd) 2961 { 2962 struct snd_soc_component *component; 2963 struct snd_pcm *pcm; 2964 int ret = 0, playback = 0, capture = 0; 2965 int i; 2966 2967 ret = soc_get_playback_capture(rtd, &playback, &capture); 2968 if (ret < 0) 2969 return ret; 2970 2971 ret = soc_create_pcm(&pcm, rtd, playback, capture); 2972 if (ret < 0) 2973 return ret; 2974 2975 /* DAPM dai link stream work */ 2976 /* 2977 * Currently nothing to do for c2c links 2978 * Since c2c links are internal nodes in the DAPM graph and 2979 * don't interface with the outside world or application layer 2980 * we don't have to do any special handling on close. 2981 */ 2982 if (!rtd->dai_link->c2c_params) 2983 rtd->close_delayed_work_func = snd_soc_close_delayed_work; 2984 2985 rtd->pcm = pcm; 2986 pcm->nonatomic = rtd->dai_link->nonatomic; 2987 pcm->private_data = rtd; 2988 pcm->no_device_suspend = true; 2989 2990 if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) { 2991 if (playback) 2992 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 2993 if (capture) 2994 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 2995 goto out; 2996 } 2997 2998 /* ASoC PCM operations */ 2999 if (rtd->dai_link->dynamic) { 3000 rtd->ops.open = dpcm_fe_dai_open; 3001 rtd->ops.hw_params = dpcm_fe_dai_hw_params; 3002 rtd->ops.prepare = dpcm_fe_dai_prepare; 3003 rtd->ops.trigger = dpcm_fe_dai_trigger; 3004 rtd->ops.hw_free = dpcm_fe_dai_hw_free; 3005 rtd->ops.close = dpcm_fe_dai_close; 3006 rtd->ops.pointer = soc_pcm_pointer; 3007 } else { 3008 rtd->ops.open = soc_pcm_open; 3009 rtd->ops.hw_params = soc_pcm_hw_params; 3010 rtd->ops.prepare = soc_pcm_prepare; 3011 rtd->ops.trigger = soc_pcm_trigger; 3012 rtd->ops.hw_free = soc_pcm_hw_free; 3013 rtd->ops.close = soc_pcm_close; 3014 rtd->ops.pointer = soc_pcm_pointer; 3015 } 3016 3017 for_each_rtd_components(rtd, i, component) { 3018 const struct snd_soc_component_driver *drv = component->driver; 3019 3020 if (drv->ioctl) 3021 rtd->ops.ioctl = snd_soc_pcm_component_ioctl; 3022 if (drv->sync_stop) 3023 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; 3024 if (drv->copy) 3025 rtd->ops.copy = snd_soc_pcm_component_copy; 3026 if (drv->page) 3027 rtd->ops.page = snd_soc_pcm_component_page; 3028 if (drv->mmap) 3029 rtd->ops.mmap = snd_soc_pcm_component_mmap; 3030 if (drv->ack) 3031 rtd->ops.ack = snd_soc_pcm_component_ack; 3032 } 3033 3034 if (playback) 3035 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 3036 3037 if (capture) 3038 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 3039 3040 ret = snd_soc_pcm_component_new(rtd); 3041 if (ret < 0) 3042 return ret; 3043 out: 3044 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n", 3045 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd)); 3046 return ret; 3047 } 3048 3049 /* get the substream for this BE */ 3050 struct snd_pcm_substream * 3051 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 3052 { 3053 return be->pcm->streams[stream].substream; 3054 } 3055 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 3056