1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management 4 // 5 // Copyright 2005 Wolfson Microelectronics PLC. 6 // Author: Liam Girdwood <lrg@slimlogic.co.uk> 7 // 8 // Features: 9 // o Changes power status of internal codec blocks depending on the 10 // dynamic configuration of codec internal audio paths and active 11 // DACs/ADCs. 12 // o Platform power domain - can support external components i.e. amps and 13 // mic/headphone insertion events. 14 // o Automatic Mic Bias support 15 // o Jack insertion power event initiation - e.g. hp insertion will enable 16 // sinks, dacs, etc 17 // o Delayed power down of audio subsystem to reduce pops between a quick 18 // device reopen. 19 20 #include <linux/module.h> 21 #include <linux/moduleparam.h> 22 #include <linux/init.h> 23 #include <linux/async.h> 24 #include <linux/delay.h> 25 #include <linux/pm.h> 26 #include <linux/bitops.h> 27 #include <linux/platform_device.h> 28 #include <linux/jiffies.h> 29 #include <linux/debugfs.h> 30 #include <linux/pm_runtime.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/pinctrl/consumer.h> 33 #include <linux/clk.h> 34 #include <linux/slab.h> 35 #include <sound/core.h> 36 #include <sound/pcm.h> 37 #include <sound/pcm_params.h> 38 #include <sound/soc.h> 39 #include <sound/initval.h> 40 41 #include <trace/events/asoc.h> 42 43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++; 44 45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \ 46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN) 47 48 #define snd_soc_dapm_for_each_direction(dir) \ 49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ 50 (dir)++) 51 52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm, 53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink, 54 const char *control, 55 int (*connected)(struct snd_soc_dapm_widget *source, 56 struct snd_soc_dapm_widget *sink)); 57 58 struct snd_soc_dapm_widget * 59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, 60 const struct snd_soc_dapm_widget *widget); 61 62 struct snd_soc_dapm_widget * 63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, 64 const struct snd_soc_dapm_widget *widget); 65 66 /* dapm power sequences - make this per codec in the future */ 67 static int dapm_up_seq[] = { 68 [snd_soc_dapm_pre] = 0, 69 [snd_soc_dapm_regulator_supply] = 1, 70 [snd_soc_dapm_pinctrl] = 1, 71 [snd_soc_dapm_clock_supply] = 1, 72 [snd_soc_dapm_supply] = 2, 73 [snd_soc_dapm_micbias] = 3, 74 [snd_soc_dapm_dai_link] = 2, 75 [snd_soc_dapm_dai_in] = 4, 76 [snd_soc_dapm_dai_out] = 4, 77 [snd_soc_dapm_aif_in] = 4, 78 [snd_soc_dapm_aif_out] = 4, 79 [snd_soc_dapm_mic] = 5, 80 [snd_soc_dapm_mux] = 6, 81 [snd_soc_dapm_demux] = 6, 82 [snd_soc_dapm_dac] = 7, 83 [snd_soc_dapm_switch] = 8, 84 [snd_soc_dapm_mixer] = 8, 85 [snd_soc_dapm_mixer_named_ctl] = 8, 86 [snd_soc_dapm_pga] = 9, 87 [snd_soc_dapm_adc] = 10, 88 [snd_soc_dapm_out_drv] = 11, 89 [snd_soc_dapm_hp] = 11, 90 [snd_soc_dapm_spk] = 11, 91 [snd_soc_dapm_line] = 11, 92 [snd_soc_dapm_kcontrol] = 12, 93 [snd_soc_dapm_post] = 13, 94 }; 95 96 static int dapm_down_seq[] = { 97 [snd_soc_dapm_pre] = 0, 98 [snd_soc_dapm_kcontrol] = 1, 99 [snd_soc_dapm_adc] = 2, 100 [snd_soc_dapm_hp] = 3, 101 [snd_soc_dapm_spk] = 3, 102 [snd_soc_dapm_line] = 3, 103 [snd_soc_dapm_out_drv] = 3, 104 [snd_soc_dapm_pga] = 4, 105 [snd_soc_dapm_switch] = 5, 106 [snd_soc_dapm_mixer_named_ctl] = 5, 107 [snd_soc_dapm_mixer] = 5, 108 [snd_soc_dapm_dac] = 6, 109 [snd_soc_dapm_mic] = 7, 110 [snd_soc_dapm_micbias] = 8, 111 [snd_soc_dapm_mux] = 9, 112 [snd_soc_dapm_demux] = 9, 113 [snd_soc_dapm_aif_in] = 10, 114 [snd_soc_dapm_aif_out] = 10, 115 [snd_soc_dapm_dai_in] = 10, 116 [snd_soc_dapm_dai_out] = 10, 117 [snd_soc_dapm_dai_link] = 11, 118 [snd_soc_dapm_supply] = 12, 119 [snd_soc_dapm_clock_supply] = 13, 120 [snd_soc_dapm_pinctrl] = 13, 121 [snd_soc_dapm_regulator_supply] = 13, 122 [snd_soc_dapm_post] = 14, 123 }; 124 125 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm) 126 { 127 if (dapm->card && dapm->card->instantiated) 128 lockdep_assert_held(&dapm->card->dapm_mutex); 129 } 130 131 static void pop_wait(u32 pop_time) 132 { 133 if (pop_time) 134 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time)); 135 } 136 137 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...) 138 { 139 va_list args; 140 char *buf; 141 142 if (!pop_time) 143 return; 144 145 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 146 if (buf == NULL) 147 return; 148 149 va_start(args, fmt); 150 vsnprintf(buf, PAGE_SIZE, fmt, args); 151 dev_info(dev, "%s", buf); 152 va_end(args); 153 154 kfree(buf); 155 } 156 157 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w) 158 { 159 return !list_empty(&w->dirty); 160 } 161 162 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason) 163 { 164 dapm_assert_locked(w->dapm); 165 166 if (!dapm_dirty_widget(w)) { 167 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n", 168 w->name, reason); 169 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty); 170 } 171 } 172 173 /* 174 * Common implementation for dapm_widget_invalidate_input_paths() and 175 * dapm_widget_invalidate_output_paths(). The function is inlined since the 176 * combined size of the two specialized functions is only marginally larger then 177 * the size of the generic function and at the same time the fast path of the 178 * specialized functions is significantly smaller than the generic function. 179 */ 180 static __always_inline void dapm_widget_invalidate_paths( 181 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir) 182 { 183 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir); 184 struct snd_soc_dapm_widget *node; 185 struct snd_soc_dapm_path *p; 186 LIST_HEAD(list); 187 188 dapm_assert_locked(w->dapm); 189 190 if (w->endpoints[dir] == -1) 191 return; 192 193 list_add_tail(&w->work_list, &list); 194 w->endpoints[dir] = -1; 195 196 list_for_each_entry(w, &list, work_list) { 197 snd_soc_dapm_widget_for_each_path(w, dir, p) { 198 if (p->is_supply || p->weak || !p->connect) 199 continue; 200 node = p->node[rdir]; 201 if (node->endpoints[dir] != -1) { 202 node->endpoints[dir] = -1; 203 list_add_tail(&node->work_list, &list); 204 } 205 } 206 } 207 } 208 209 /* 210 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of 211 * input paths 212 * @w: The widget for which to invalidate the cached number of input paths 213 * 214 * Resets the cached number of inputs for the specified widget and all widgets 215 * that can be reached via outcoming paths from the widget. 216 * 217 * This function must be called if the number of output paths for a widget might 218 * have changed. E.g. if the source state of a widget changes or a path is added 219 * or activated with the widget as the sink. 220 */ 221 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w) 222 { 223 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN); 224 } 225 226 /* 227 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of 228 * output paths 229 * @w: The widget for which to invalidate the cached number of output paths 230 * 231 * Resets the cached number of outputs for the specified widget and all widgets 232 * that can be reached via incoming paths from the widget. 233 * 234 * This function must be called if the number of output paths for a widget might 235 * have changed. E.g. if the sink state of a widget changes or a path is added 236 * or activated with the widget as the source. 237 */ 238 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w) 239 { 240 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT); 241 } 242 243 /* 244 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs 245 * for the widgets connected to a path 246 * @p: The path to invalidate 247 * 248 * Resets the cached number of inputs for the sink of the path and the cached 249 * number of outputs for the source of the path. 250 * 251 * This function must be called when a path is added, removed or the connected 252 * state changes. 253 */ 254 static void dapm_path_invalidate(struct snd_soc_dapm_path *p) 255 { 256 /* 257 * Weak paths or supply paths do not influence the number of input or 258 * output paths of their neighbors. 259 */ 260 if (p->weak || p->is_supply) 261 return; 262 263 /* 264 * The number of connected endpoints is the sum of the number of 265 * connected endpoints of all neighbors. If a node with 0 connected 266 * endpoints is either connected or disconnected that sum won't change, 267 * so there is no need to re-check the path. 268 */ 269 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0) 270 dapm_widget_invalidate_input_paths(p->sink); 271 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0) 272 dapm_widget_invalidate_output_paths(p->source); 273 } 274 275 void dapm_mark_endpoints_dirty(struct snd_soc_card *card) 276 { 277 struct snd_soc_dapm_widget *w; 278 279 mutex_lock(&card->dapm_mutex); 280 281 list_for_each_entry(w, &card->widgets, list) { 282 if (w->is_ep) { 283 dapm_mark_dirty(w, "Rechecking endpoints"); 284 if (w->is_ep & SND_SOC_DAPM_EP_SINK) 285 dapm_widget_invalidate_output_paths(w); 286 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE) 287 dapm_widget_invalidate_input_paths(w); 288 } 289 } 290 291 mutex_unlock(&card->dapm_mutex); 292 } 293 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty); 294 295 /* create a new dapm widget */ 296 static inline struct snd_soc_dapm_widget *dapm_cnew_widget( 297 const struct snd_soc_dapm_widget *_widget) 298 { 299 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL); 300 } 301 302 struct dapm_kcontrol_data { 303 unsigned int value; 304 struct snd_soc_dapm_widget *widget; 305 struct list_head paths; 306 struct snd_soc_dapm_widget_list *wlist; 307 }; 308 309 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget, 310 struct snd_kcontrol *kcontrol, const char *ctrl_name) 311 { 312 struct dapm_kcontrol_data *data; 313 struct soc_mixer_control *mc; 314 struct soc_enum *e; 315 const char *name; 316 int ret; 317 318 data = kzalloc(sizeof(*data), GFP_KERNEL); 319 if (!data) 320 return -ENOMEM; 321 322 INIT_LIST_HEAD(&data->paths); 323 324 switch (widget->id) { 325 case snd_soc_dapm_switch: 326 case snd_soc_dapm_mixer: 327 case snd_soc_dapm_mixer_named_ctl: 328 mc = (struct soc_mixer_control *)kcontrol->private_value; 329 330 if (mc->autodisable && snd_soc_volsw_is_stereo(mc)) 331 dev_warn(widget->dapm->dev, 332 "ASoC: Unsupported stereo autodisable control '%s'\n", 333 ctrl_name); 334 335 if (mc->autodisable) { 336 struct snd_soc_dapm_widget template; 337 338 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name, 339 "Autodisable"); 340 if (!name) { 341 ret = -ENOMEM; 342 goto err_data; 343 } 344 345 memset(&template, 0, sizeof(template)); 346 template.reg = mc->reg; 347 template.mask = (1 << fls(mc->max)) - 1; 348 template.shift = mc->shift; 349 if (mc->invert) 350 template.off_val = mc->max; 351 else 352 template.off_val = 0; 353 template.on_val = template.off_val; 354 template.id = snd_soc_dapm_kcontrol; 355 template.name = name; 356 357 data->value = template.on_val; 358 359 data->widget = 360 snd_soc_dapm_new_control_unlocked(widget->dapm, 361 &template); 362 kfree(name); 363 if (IS_ERR(data->widget)) { 364 ret = PTR_ERR(data->widget); 365 goto err_data; 366 } 367 if (!data->widget) { 368 ret = -ENOMEM; 369 goto err_data; 370 } 371 } 372 break; 373 case snd_soc_dapm_demux: 374 case snd_soc_dapm_mux: 375 e = (struct soc_enum *)kcontrol->private_value; 376 377 if (e->autodisable) { 378 struct snd_soc_dapm_widget template; 379 380 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name, 381 "Autodisable"); 382 if (!name) { 383 ret = -ENOMEM; 384 goto err_data; 385 } 386 387 memset(&template, 0, sizeof(template)); 388 template.reg = e->reg; 389 template.mask = e->mask << e->shift_l; 390 template.shift = e->shift_l; 391 template.off_val = snd_soc_enum_item_to_val(e, 0); 392 template.on_val = template.off_val; 393 template.id = snd_soc_dapm_kcontrol; 394 template.name = name; 395 396 data->value = template.on_val; 397 398 data->widget = snd_soc_dapm_new_control_unlocked( 399 widget->dapm, &template); 400 kfree(name); 401 if (IS_ERR(data->widget)) { 402 ret = PTR_ERR(data->widget); 403 goto err_data; 404 } 405 if (!data->widget) { 406 ret = -ENOMEM; 407 goto err_data; 408 } 409 410 snd_soc_dapm_add_path(widget->dapm, data->widget, 411 widget, NULL, NULL); 412 } 413 break; 414 default: 415 break; 416 } 417 418 kcontrol->private_data = data; 419 420 return 0; 421 422 err_data: 423 kfree(data); 424 return ret; 425 } 426 427 static void dapm_kcontrol_free(struct snd_kcontrol *kctl) 428 { 429 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl); 430 431 list_del(&data->paths); 432 kfree(data->wlist); 433 kfree(data); 434 } 435 436 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist( 437 const struct snd_kcontrol *kcontrol) 438 { 439 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 440 441 return data->wlist; 442 } 443 444 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol, 445 struct snd_soc_dapm_widget *widget) 446 { 447 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 448 struct snd_soc_dapm_widget_list *new_wlist; 449 unsigned int n; 450 451 if (data->wlist) 452 n = data->wlist->num_widgets + 1; 453 else 454 n = 1; 455 456 new_wlist = krealloc(data->wlist, 457 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL); 458 if (!new_wlist) 459 return -ENOMEM; 460 461 new_wlist->widgets[n - 1] = widget; 462 new_wlist->num_widgets = n; 463 464 data->wlist = new_wlist; 465 466 return 0; 467 } 468 469 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol, 470 struct snd_soc_dapm_path *path) 471 { 472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 473 474 list_add_tail(&path->list_kcontrol, &data->paths); 475 } 476 477 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol) 478 { 479 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 480 481 if (!data->widget) 482 return true; 483 484 return data->widget->power; 485 } 486 487 static struct list_head *dapm_kcontrol_get_path_list( 488 const struct snd_kcontrol *kcontrol) 489 { 490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 491 492 return &data->paths; 493 } 494 495 #define dapm_kcontrol_for_each_path(path, kcontrol) \ 496 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \ 497 list_kcontrol) 498 499 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol) 500 { 501 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 502 503 return data->value; 504 } 505 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value); 506 507 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol, 508 unsigned int value) 509 { 510 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 511 512 if (data->value == value) 513 return false; 514 515 if (data->widget) 516 data->widget->on_val = value; 517 518 data->value = value; 519 520 return true; 521 } 522 523 /** 524 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a 525 * kcontrol 526 * @kcontrol: The kcontrol 527 */ 528 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget( 529 struct snd_kcontrol *kcontrol) 530 { 531 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]; 532 } 533 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget); 534 535 /** 536 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a 537 * kcontrol 538 * @kcontrol: The kcontrol 539 * 540 * Note: This function must only be used on kcontrols that are known to have 541 * been registered for a CODEC. Otherwise the behaviour is undefined. 542 */ 543 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm( 544 struct snd_kcontrol *kcontrol) 545 { 546 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm; 547 } 548 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm); 549 550 static void dapm_reset(struct snd_soc_card *card) 551 { 552 struct snd_soc_dapm_widget *w; 553 554 lockdep_assert_held(&card->dapm_mutex); 555 556 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats)); 557 558 list_for_each_entry(w, &card->widgets, list) { 559 w->new_power = w->power; 560 w->power_checked = false; 561 } 562 } 563 564 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm) 565 { 566 if (!dapm->component) 567 return NULL; 568 return dapm->component->name_prefix; 569 } 570 571 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg, 572 unsigned int *value) 573 { 574 if (!dapm->component) 575 return -EIO; 576 return snd_soc_component_read(dapm->component, reg, value); 577 } 578 579 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm, 580 int reg, unsigned int mask, unsigned int value) 581 { 582 if (!dapm->component) 583 return -EIO; 584 return snd_soc_component_update_bits(dapm->component, reg, 585 mask, value); 586 } 587 588 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm, 589 int reg, unsigned int mask, unsigned int value) 590 { 591 if (!dapm->component) 592 return -EIO; 593 return snd_soc_component_test_bits(dapm->component, reg, mask, value); 594 } 595 596 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm) 597 { 598 if (dapm->component) 599 snd_soc_component_async_complete(dapm->component); 600 } 601 602 static struct snd_soc_dapm_widget * 603 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name) 604 { 605 struct snd_soc_dapm_widget *w = wcache->widget; 606 struct list_head *wlist; 607 const int depth = 2; 608 int i = 0; 609 610 if (w) { 611 wlist = &w->dapm->card->widgets; 612 613 list_for_each_entry_from(w, wlist, list) { 614 if (!strcmp(name, w->name)) 615 return w; 616 617 if (++i == depth) 618 break; 619 } 620 } 621 622 return NULL; 623 } 624 625 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache, 626 struct snd_soc_dapm_widget *w) 627 { 628 wcache->widget = w; 629 } 630 631 /** 632 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level 633 * @dapm: The DAPM context for which to set the level 634 * @level: The level to set 635 * 636 * Forces the DAPM bias level to a specific state. It will call the bias level 637 * callback of DAPM context with the specified level. This will even happen if 638 * the context is already at the same level. Furthermore it will not go through 639 * the normal bias level sequencing, meaning any intermediate states between the 640 * current and the target state will not be entered. 641 * 642 * Note that the change in bias level is only temporary and the next time 643 * snd_soc_dapm_sync() is called the state will be set to the level as 644 * determined by the DAPM core. The function is mainly intended to be used to 645 * used during probe or resume from suspend to power up the device so 646 * initialization can be done, before the DAPM core takes over. 647 */ 648 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm, 649 enum snd_soc_bias_level level) 650 { 651 int ret = 0; 652 653 if (dapm->set_bias_level) 654 ret = dapm->set_bias_level(dapm, level); 655 656 if (ret == 0) 657 dapm->bias_level = level; 658 659 return ret; 660 } 661 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level); 662 663 /** 664 * snd_soc_dapm_set_bias_level - set the bias level for the system 665 * @dapm: DAPM context 666 * @level: level to configure 667 * 668 * Configure the bias (power) levels for the SoC audio device. 669 * 670 * Returns 0 for success else error. 671 */ 672 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm, 673 enum snd_soc_bias_level level) 674 { 675 struct snd_soc_card *card = dapm->card; 676 int ret = 0; 677 678 trace_snd_soc_bias_level_start(card, level); 679 680 if (card && card->set_bias_level) 681 ret = card->set_bias_level(card, dapm, level); 682 if (ret != 0) 683 goto out; 684 685 if (!card || dapm != &card->dapm) 686 ret = snd_soc_dapm_force_bias_level(dapm, level); 687 688 if (ret != 0) 689 goto out; 690 691 if (card && card->set_bias_level_post) 692 ret = card->set_bias_level_post(card, dapm, level); 693 out: 694 trace_snd_soc_bias_level_done(card, level); 695 696 return ret; 697 } 698 699 /* connect mux widget to its interconnecting audio paths */ 700 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm, 701 struct snd_soc_dapm_path *path, const char *control_name, 702 struct snd_soc_dapm_widget *w) 703 { 704 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0]; 705 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 706 unsigned int val, item; 707 int i; 708 709 if (e->reg != SND_SOC_NOPM) { 710 soc_dapm_read(dapm, e->reg, &val); 711 val = (val >> e->shift_l) & e->mask; 712 item = snd_soc_enum_val_to_item(e, val); 713 } else { 714 /* since a virtual mux has no backing registers to 715 * decide which path to connect, it will try to match 716 * with the first enumeration. This is to ensure 717 * that the default mux choice (the first) will be 718 * correctly powered up during initialization. 719 */ 720 item = 0; 721 } 722 723 i = match_string(e->texts, e->items, control_name); 724 if (i < 0) 725 return -ENODEV; 726 727 path->name = e->texts[i]; 728 path->connect = (i == item); 729 return 0; 730 731 } 732 733 /* set up initial codec paths */ 734 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i, 735 int nth_path) 736 { 737 struct soc_mixer_control *mc = (struct soc_mixer_control *) 738 p->sink->kcontrol_news[i].private_value; 739 unsigned int reg = mc->reg; 740 unsigned int shift = mc->shift; 741 unsigned int max = mc->max; 742 unsigned int mask = (1 << fls(max)) - 1; 743 unsigned int invert = mc->invert; 744 unsigned int val; 745 746 if (reg != SND_SOC_NOPM) { 747 soc_dapm_read(p->sink->dapm, reg, &val); 748 /* 749 * The nth_path argument allows this function to know 750 * which path of a kcontrol it is setting the initial 751 * status for. Ideally this would support any number 752 * of paths and channels. But since kcontrols only come 753 * in mono and stereo variants, we are limited to 2 754 * channels. 755 * 756 * The following code assumes for stereo controls the 757 * first path is the left channel, and all remaining 758 * paths are the right channel. 759 */ 760 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) { 761 if (reg != mc->rreg) 762 soc_dapm_read(p->sink->dapm, mc->rreg, &val); 763 val = (val >> mc->rshift) & mask; 764 } else { 765 val = (val >> shift) & mask; 766 } 767 if (invert) 768 val = max - val; 769 p->connect = !!val; 770 } else { 771 p->connect = 0; 772 } 773 } 774 775 /* connect mixer widget to its interconnecting audio paths */ 776 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm, 777 struct snd_soc_dapm_path *path, const char *control_name) 778 { 779 int i, nth_path = 0; 780 781 /* search for mixer kcontrol */ 782 for (i = 0; i < path->sink->num_kcontrols; i++) { 783 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) { 784 path->name = path->sink->kcontrol_news[i].name; 785 dapm_set_mixer_path_status(path, i, nth_path++); 786 return 0; 787 } 788 } 789 return -ENODEV; 790 } 791 792 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm, 793 struct snd_soc_dapm_widget *kcontrolw, 794 const struct snd_kcontrol_new *kcontrol_new, 795 struct snd_kcontrol **kcontrol) 796 { 797 struct snd_soc_dapm_widget *w; 798 int i; 799 800 *kcontrol = NULL; 801 802 list_for_each_entry(w, &dapm->card->widgets, list) { 803 if (w == kcontrolw || w->dapm != kcontrolw->dapm) 804 continue; 805 for (i = 0; i < w->num_kcontrols; i++) { 806 if (&w->kcontrol_news[i] == kcontrol_new) { 807 if (w->kcontrols) 808 *kcontrol = w->kcontrols[i]; 809 return 1; 810 } 811 } 812 } 813 814 return 0; 815 } 816 817 /* 818 * Determine if a kcontrol is shared. If it is, look it up. If it isn't, 819 * create it. Either way, add the widget into the control's widget list 820 */ 821 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w, 822 int kci) 823 { 824 struct snd_soc_dapm_context *dapm = w->dapm; 825 struct snd_card *card = dapm->card->snd_card; 826 const char *prefix; 827 size_t prefix_len; 828 int shared; 829 struct snd_kcontrol *kcontrol; 830 bool wname_in_long_name, kcname_in_long_name; 831 char *long_name = NULL; 832 const char *name; 833 int ret = 0; 834 835 prefix = soc_dapm_prefix(dapm); 836 if (prefix) 837 prefix_len = strlen(prefix) + 1; 838 else 839 prefix_len = 0; 840 841 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci], 842 &kcontrol); 843 844 if (!kcontrol) { 845 if (shared) { 846 wname_in_long_name = false; 847 kcname_in_long_name = true; 848 } else { 849 switch (w->id) { 850 case snd_soc_dapm_switch: 851 case snd_soc_dapm_mixer: 852 case snd_soc_dapm_pga: 853 case snd_soc_dapm_out_drv: 854 wname_in_long_name = true; 855 kcname_in_long_name = true; 856 break; 857 case snd_soc_dapm_mixer_named_ctl: 858 wname_in_long_name = false; 859 kcname_in_long_name = true; 860 break; 861 case snd_soc_dapm_demux: 862 case snd_soc_dapm_mux: 863 wname_in_long_name = true; 864 kcname_in_long_name = false; 865 break; 866 default: 867 return -EINVAL; 868 } 869 } 870 871 if (wname_in_long_name && kcname_in_long_name) { 872 /* 873 * The control will get a prefix from the control 874 * creation process but we're also using the same 875 * prefix for widgets so cut the prefix off the 876 * front of the widget name. 877 */ 878 long_name = kasprintf(GFP_KERNEL, "%s %s", 879 w->name + prefix_len, 880 w->kcontrol_news[kci].name); 881 if (long_name == NULL) 882 return -ENOMEM; 883 884 name = long_name; 885 } else if (wname_in_long_name) { 886 long_name = NULL; 887 name = w->name + prefix_len; 888 } else { 889 long_name = NULL; 890 name = w->kcontrol_news[kci].name; 891 } 892 893 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name, 894 prefix); 895 if (!kcontrol) { 896 ret = -ENOMEM; 897 goto exit_free; 898 } 899 900 kcontrol->private_free = dapm_kcontrol_free; 901 902 ret = dapm_kcontrol_data_alloc(w, kcontrol, name); 903 if (ret) { 904 snd_ctl_free_one(kcontrol); 905 goto exit_free; 906 } 907 908 ret = snd_ctl_add(card, kcontrol); 909 if (ret < 0) { 910 dev_err(dapm->dev, 911 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n", 912 w->name, name, ret); 913 goto exit_free; 914 } 915 } 916 917 ret = dapm_kcontrol_add_widget(kcontrol, w); 918 if (ret == 0) 919 w->kcontrols[kci] = kcontrol; 920 921 exit_free: 922 kfree(long_name); 923 924 return ret; 925 } 926 927 /* create new dapm mixer control */ 928 static int dapm_new_mixer(struct snd_soc_dapm_widget *w) 929 { 930 int i, ret; 931 struct snd_soc_dapm_path *path; 932 struct dapm_kcontrol_data *data; 933 934 /* add kcontrol */ 935 for (i = 0; i < w->num_kcontrols; i++) { 936 /* match name */ 937 snd_soc_dapm_widget_for_each_source_path(w, path) { 938 /* mixer/mux paths name must match control name */ 939 if (path->name != (char *)w->kcontrol_news[i].name) 940 continue; 941 942 if (!w->kcontrols[i]) { 943 ret = dapm_create_or_share_kcontrol(w, i); 944 if (ret < 0) 945 return ret; 946 } 947 948 dapm_kcontrol_add_path(w->kcontrols[i], path); 949 950 data = snd_kcontrol_chip(w->kcontrols[i]); 951 if (data->widget) 952 snd_soc_dapm_add_path(data->widget->dapm, 953 data->widget, 954 path->source, 955 NULL, NULL); 956 } 957 } 958 959 return 0; 960 } 961 962 /* create new dapm mux control */ 963 static int dapm_new_mux(struct snd_soc_dapm_widget *w) 964 { 965 struct snd_soc_dapm_context *dapm = w->dapm; 966 enum snd_soc_dapm_direction dir; 967 struct snd_soc_dapm_path *path; 968 const char *type; 969 int ret; 970 971 switch (w->id) { 972 case snd_soc_dapm_mux: 973 dir = SND_SOC_DAPM_DIR_OUT; 974 type = "mux"; 975 break; 976 case snd_soc_dapm_demux: 977 dir = SND_SOC_DAPM_DIR_IN; 978 type = "demux"; 979 break; 980 default: 981 return -EINVAL; 982 } 983 984 if (w->num_kcontrols != 1) { 985 dev_err(dapm->dev, 986 "ASoC: %s %s has incorrect number of controls\n", type, 987 w->name); 988 return -EINVAL; 989 } 990 991 if (list_empty(&w->edges[dir])) { 992 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name); 993 return -EINVAL; 994 } 995 996 ret = dapm_create_or_share_kcontrol(w, 0); 997 if (ret < 0) 998 return ret; 999 1000 snd_soc_dapm_widget_for_each_path(w, dir, path) { 1001 if (path->name) 1002 dapm_kcontrol_add_path(w->kcontrols[0], path); 1003 } 1004 1005 return 0; 1006 } 1007 1008 /* create new dapm volume control */ 1009 static int dapm_new_pga(struct snd_soc_dapm_widget *w) 1010 { 1011 int i, ret; 1012 1013 for (i = 0; i < w->num_kcontrols; i++) { 1014 ret = dapm_create_or_share_kcontrol(w, i); 1015 if (ret < 0) 1016 return ret; 1017 } 1018 1019 return 0; 1020 } 1021 1022 /* create new dapm dai link control */ 1023 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w) 1024 { 1025 int i, ret; 1026 struct snd_kcontrol *kcontrol; 1027 struct snd_soc_dapm_context *dapm = w->dapm; 1028 struct snd_card *card = dapm->card->snd_card; 1029 1030 /* create control for links with > 1 config */ 1031 if (w->num_params <= 1) 1032 return 0; 1033 1034 /* add kcontrol */ 1035 for (i = 0; i < w->num_kcontrols; i++) { 1036 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w, 1037 w->name, NULL); 1038 ret = snd_ctl_add(card, kcontrol); 1039 if (ret < 0) { 1040 dev_err(dapm->dev, 1041 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n", 1042 w->name, w->kcontrol_news[i].name, ret); 1043 return ret; 1044 } 1045 kcontrol->private_data = w; 1046 w->kcontrols[i] = kcontrol; 1047 } 1048 1049 return 0; 1050 } 1051 1052 /* We implement power down on suspend by checking the power state of 1053 * the ALSA card - when we are suspending the ALSA state for the card 1054 * is set to D3. 1055 */ 1056 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget) 1057 { 1058 int level = snd_power_get_state(widget->dapm->card->snd_card); 1059 1060 switch (level) { 1061 case SNDRV_CTL_POWER_D3hot: 1062 case SNDRV_CTL_POWER_D3cold: 1063 if (widget->ignore_suspend) 1064 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n", 1065 widget->name); 1066 return widget->ignore_suspend; 1067 default: 1068 return 1; 1069 } 1070 } 1071 1072 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list, 1073 struct list_head *widgets) 1074 { 1075 struct snd_soc_dapm_widget *w; 1076 struct list_head *it; 1077 unsigned int size = 0; 1078 unsigned int i = 0; 1079 1080 list_for_each(it, widgets) 1081 size++; 1082 1083 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL); 1084 if (*list == NULL) 1085 return -ENOMEM; 1086 1087 list_for_each_entry(w, widgets, work_list) 1088 (*list)->widgets[i++] = w; 1089 1090 (*list)->num_widgets = i; 1091 1092 return 0; 1093 } 1094 1095 /* 1096 * Common implementation for is_connected_output_ep() and 1097 * is_connected_input_ep(). The function is inlined since the combined size of 1098 * the two specialized functions is only marginally larger then the size of the 1099 * generic function and at the same time the fast path of the specialized 1100 * functions is significantly smaller than the generic function. 1101 */ 1102 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget, 1103 struct list_head *list, enum snd_soc_dapm_direction dir, 1104 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *, 1105 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *, 1106 enum snd_soc_dapm_direction)), 1107 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *, 1108 enum snd_soc_dapm_direction)) 1109 { 1110 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir); 1111 struct snd_soc_dapm_path *path; 1112 int con = 0; 1113 1114 if (widget->endpoints[dir] >= 0) 1115 return widget->endpoints[dir]; 1116 1117 DAPM_UPDATE_STAT(widget, path_checks); 1118 1119 /* do we need to add this widget to the list ? */ 1120 if (list) 1121 list_add_tail(&widget->work_list, list); 1122 1123 if (custom_stop_condition && custom_stop_condition(widget, dir)) { 1124 widget->endpoints[dir] = 1; 1125 return widget->endpoints[dir]; 1126 } 1127 1128 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) { 1129 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget); 1130 return widget->endpoints[dir]; 1131 } 1132 1133 snd_soc_dapm_widget_for_each_path(widget, rdir, path) { 1134 DAPM_UPDATE_STAT(widget, neighbour_checks); 1135 1136 if (path->weak || path->is_supply) 1137 continue; 1138 1139 if (path->walking) 1140 return 1; 1141 1142 trace_snd_soc_dapm_path(widget, dir, path); 1143 1144 if (path->connect) { 1145 path->walking = 1; 1146 con += fn(path->node[dir], list, custom_stop_condition); 1147 path->walking = 0; 1148 } 1149 } 1150 1151 widget->endpoints[dir] = con; 1152 1153 return con; 1154 } 1155 1156 /* 1157 * Recursively check for a completed path to an active or physically connected 1158 * output widget. Returns number of complete paths. 1159 * 1160 * Optionally, can be supplied with a function acting as a stopping condition. 1161 * This function takes the dapm widget currently being examined and the walk 1162 * direction as an arguments, it should return true if the walk should be 1163 * stopped and false otherwise. 1164 */ 1165 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget, 1166 struct list_head *list, 1167 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i, 1168 enum snd_soc_dapm_direction)) 1169 { 1170 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT, 1171 is_connected_output_ep, custom_stop_condition); 1172 } 1173 1174 /* 1175 * Recursively check for a completed path to an active or physically connected 1176 * input widget. Returns number of complete paths. 1177 * 1178 * Optionally, can be supplied with a function acting as a stopping condition. 1179 * This function takes the dapm widget currently being examined and the walk 1180 * direction as an arguments, it should return true if the walk should be 1181 * stopped and false otherwise. 1182 */ 1183 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget, 1184 struct list_head *list, 1185 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i, 1186 enum snd_soc_dapm_direction)) 1187 { 1188 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN, 1189 is_connected_input_ep, custom_stop_condition); 1190 } 1191 1192 /** 1193 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets. 1194 * @dai: the soc DAI. 1195 * @stream: stream direction. 1196 * @list: list of active widgets for this stream. 1197 * @custom_stop_condition: (optional) a function meant to stop the widget graph 1198 * walk based on custom logic. 1199 * 1200 * Queries DAPM graph as to whether a valid audio stream path exists for 1201 * the initial stream specified by name. This takes into account 1202 * current mixer and mux kcontrol settings. Creates list of valid widgets. 1203 * 1204 * Optionally, can be supplied with a function acting as a stopping condition. 1205 * This function takes the dapm widget currently being examined and the walk 1206 * direction as an arguments, it should return true if the walk should be 1207 * stopped and false otherwise. 1208 * 1209 * Returns the number of valid paths or negative error. 1210 */ 1211 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream, 1212 struct snd_soc_dapm_widget_list **list, 1213 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *, 1214 enum snd_soc_dapm_direction)) 1215 { 1216 struct snd_soc_card *card = dai->component->card; 1217 struct snd_soc_dapm_widget *w; 1218 LIST_HEAD(widgets); 1219 int paths; 1220 int ret; 1221 1222 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 1223 1224 /* 1225 * For is_connected_{output,input}_ep fully discover the graph we need 1226 * to reset the cached number of inputs and outputs. 1227 */ 1228 list_for_each_entry(w, &card->widgets, list) { 1229 w->endpoints[SND_SOC_DAPM_DIR_IN] = -1; 1230 w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1; 1231 } 1232 1233 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 1234 paths = is_connected_output_ep(dai->playback_widget, &widgets, 1235 custom_stop_condition); 1236 else 1237 paths = is_connected_input_ep(dai->capture_widget, &widgets, 1238 custom_stop_condition); 1239 1240 /* Drop starting point */ 1241 list_del(widgets.next); 1242 1243 ret = dapm_widget_list_create(list, &widgets); 1244 if (ret) 1245 paths = ret; 1246 1247 trace_snd_soc_dapm_connected(paths, stream); 1248 mutex_unlock(&card->dapm_mutex); 1249 1250 return paths; 1251 } 1252 1253 /* 1254 * Handler for regulator supply widget. 1255 */ 1256 int dapm_regulator_event(struct snd_soc_dapm_widget *w, 1257 struct snd_kcontrol *kcontrol, int event) 1258 { 1259 int ret; 1260 1261 soc_dapm_async_complete(w->dapm); 1262 1263 if (SND_SOC_DAPM_EVENT_ON(event)) { 1264 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) { 1265 ret = regulator_allow_bypass(w->regulator, false); 1266 if (ret != 0) 1267 dev_warn(w->dapm->dev, 1268 "ASoC: Failed to unbypass %s: %d\n", 1269 w->name, ret); 1270 } 1271 1272 return regulator_enable(w->regulator); 1273 } else { 1274 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) { 1275 ret = regulator_allow_bypass(w->regulator, true); 1276 if (ret != 0) 1277 dev_warn(w->dapm->dev, 1278 "ASoC: Failed to bypass %s: %d\n", 1279 w->name, ret); 1280 } 1281 1282 return regulator_disable_deferred(w->regulator, w->shift); 1283 } 1284 } 1285 EXPORT_SYMBOL_GPL(dapm_regulator_event); 1286 1287 /* 1288 * Handler for pinctrl widget. 1289 */ 1290 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w, 1291 struct snd_kcontrol *kcontrol, int event) 1292 { 1293 struct snd_soc_dapm_pinctrl_priv *priv = w->priv; 1294 struct pinctrl *p = w->pinctrl; 1295 struct pinctrl_state *s; 1296 1297 if (!p || !priv) 1298 return -EIO; 1299 1300 if (SND_SOC_DAPM_EVENT_ON(event)) 1301 s = pinctrl_lookup_state(p, priv->active_state); 1302 else 1303 s = pinctrl_lookup_state(p, priv->sleep_state); 1304 1305 if (IS_ERR(s)) 1306 return PTR_ERR(s); 1307 1308 return pinctrl_select_state(p, s); 1309 } 1310 EXPORT_SYMBOL_GPL(dapm_pinctrl_event); 1311 1312 /* 1313 * Handler for clock supply widget. 1314 */ 1315 int dapm_clock_event(struct snd_soc_dapm_widget *w, 1316 struct snd_kcontrol *kcontrol, int event) 1317 { 1318 if (!w->clk) 1319 return -EIO; 1320 1321 soc_dapm_async_complete(w->dapm); 1322 1323 #ifdef CONFIG_HAVE_CLK 1324 if (SND_SOC_DAPM_EVENT_ON(event)) { 1325 return clk_prepare_enable(w->clk); 1326 } else { 1327 clk_disable_unprepare(w->clk); 1328 return 0; 1329 } 1330 #endif 1331 return 0; 1332 } 1333 EXPORT_SYMBOL_GPL(dapm_clock_event); 1334 1335 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w) 1336 { 1337 if (w->power_checked) 1338 return w->new_power; 1339 1340 if (w->force) 1341 w->new_power = 1; 1342 else 1343 w->new_power = w->power_check(w); 1344 1345 w->power_checked = true; 1346 1347 return w->new_power; 1348 } 1349 1350 /* Generic check to see if a widget should be powered. */ 1351 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w) 1352 { 1353 int in, out; 1354 1355 DAPM_UPDATE_STAT(w, power_checks); 1356 1357 in = is_connected_input_ep(w, NULL, NULL); 1358 out = is_connected_output_ep(w, NULL, NULL); 1359 return out != 0 && in != 0; 1360 } 1361 1362 /* Check to see if a power supply is needed */ 1363 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w) 1364 { 1365 struct snd_soc_dapm_path *path; 1366 1367 DAPM_UPDATE_STAT(w, power_checks); 1368 1369 /* Check if one of our outputs is connected */ 1370 snd_soc_dapm_widget_for_each_sink_path(w, path) { 1371 DAPM_UPDATE_STAT(w, neighbour_checks); 1372 1373 if (path->weak) 1374 continue; 1375 1376 if (path->connected && 1377 !path->connected(path->source, path->sink)) 1378 continue; 1379 1380 if (dapm_widget_power_check(path->sink)) 1381 return 1; 1382 } 1383 1384 return 0; 1385 } 1386 1387 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w) 1388 { 1389 return w->connected; 1390 } 1391 1392 static int dapm_seq_compare(struct snd_soc_dapm_widget *a, 1393 struct snd_soc_dapm_widget *b, 1394 bool power_up) 1395 { 1396 int *sort; 1397 1398 if (power_up) 1399 sort = dapm_up_seq; 1400 else 1401 sort = dapm_down_seq; 1402 1403 if (sort[a->id] != sort[b->id]) 1404 return sort[a->id] - sort[b->id]; 1405 if (a->subseq != b->subseq) { 1406 if (power_up) 1407 return a->subseq - b->subseq; 1408 else 1409 return b->subseq - a->subseq; 1410 } 1411 if (a->reg != b->reg) 1412 return a->reg - b->reg; 1413 if (a->dapm != b->dapm) 1414 return (unsigned long)a->dapm - (unsigned long)b->dapm; 1415 1416 return 0; 1417 } 1418 1419 /* Insert a widget in order into a DAPM power sequence. */ 1420 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget, 1421 struct list_head *list, 1422 bool power_up) 1423 { 1424 struct snd_soc_dapm_widget *w; 1425 1426 list_for_each_entry(w, list, power_list) 1427 if (dapm_seq_compare(new_widget, w, power_up) < 0) { 1428 list_add_tail(&new_widget->power_list, &w->power_list); 1429 return; 1430 } 1431 1432 list_add_tail(&new_widget->power_list, list); 1433 } 1434 1435 static void dapm_seq_check_event(struct snd_soc_card *card, 1436 struct snd_soc_dapm_widget *w, int event) 1437 { 1438 const char *ev_name; 1439 int power, ret; 1440 1441 switch (event) { 1442 case SND_SOC_DAPM_PRE_PMU: 1443 ev_name = "PRE_PMU"; 1444 power = 1; 1445 break; 1446 case SND_SOC_DAPM_POST_PMU: 1447 ev_name = "POST_PMU"; 1448 power = 1; 1449 break; 1450 case SND_SOC_DAPM_PRE_PMD: 1451 ev_name = "PRE_PMD"; 1452 power = 0; 1453 break; 1454 case SND_SOC_DAPM_POST_PMD: 1455 ev_name = "POST_PMD"; 1456 power = 0; 1457 break; 1458 case SND_SOC_DAPM_WILL_PMU: 1459 ev_name = "WILL_PMU"; 1460 power = 1; 1461 break; 1462 case SND_SOC_DAPM_WILL_PMD: 1463 ev_name = "WILL_PMD"; 1464 power = 0; 1465 break; 1466 default: 1467 WARN(1, "Unknown event %d\n", event); 1468 return; 1469 } 1470 1471 if (w->new_power != power) 1472 return; 1473 1474 if (w->event && (w->event_flags & event)) { 1475 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n", 1476 w->name, ev_name); 1477 soc_dapm_async_complete(w->dapm); 1478 trace_snd_soc_dapm_widget_event_start(w, event); 1479 ret = w->event(w, NULL, event); 1480 trace_snd_soc_dapm_widget_event_done(w, event); 1481 if (ret < 0) 1482 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n", 1483 ev_name, w->name, ret); 1484 } 1485 } 1486 1487 /* Apply the coalesced changes from a DAPM sequence */ 1488 static void dapm_seq_run_coalesced(struct snd_soc_card *card, 1489 struct list_head *pending) 1490 { 1491 struct snd_soc_dapm_context *dapm; 1492 struct snd_soc_dapm_widget *w; 1493 int reg; 1494 unsigned int value = 0; 1495 unsigned int mask = 0; 1496 1497 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list); 1498 reg = w->reg; 1499 dapm = w->dapm; 1500 1501 list_for_each_entry(w, pending, power_list) { 1502 WARN_ON(reg != w->reg || dapm != w->dapm); 1503 w->power = w->new_power; 1504 1505 mask |= w->mask << w->shift; 1506 if (w->power) 1507 value |= w->on_val << w->shift; 1508 else 1509 value |= w->off_val << w->shift; 1510 1511 pop_dbg(dapm->dev, card->pop_time, 1512 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n", 1513 w->name, reg, value, mask); 1514 1515 /* Check for events */ 1516 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU); 1517 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD); 1518 } 1519 1520 if (reg >= 0) { 1521 /* Any widget will do, they should all be updating the 1522 * same register. 1523 */ 1524 1525 pop_dbg(dapm->dev, card->pop_time, 1526 "pop test : Applying 0x%x/0x%x to %x in %dms\n", 1527 value, mask, reg, card->pop_time); 1528 pop_wait(card->pop_time); 1529 soc_dapm_update_bits(dapm, reg, mask, value); 1530 } 1531 1532 list_for_each_entry(w, pending, power_list) { 1533 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU); 1534 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD); 1535 } 1536 } 1537 1538 /* Apply a DAPM power sequence. 1539 * 1540 * We walk over a pre-sorted list of widgets to apply power to. In 1541 * order to minimise the number of writes to the device required 1542 * multiple widgets will be updated in a single write where possible. 1543 * Currently anything that requires more than a single write is not 1544 * handled. 1545 */ 1546 static void dapm_seq_run(struct snd_soc_card *card, 1547 struct list_head *list, int event, bool power_up) 1548 { 1549 struct snd_soc_dapm_widget *w, *n; 1550 struct snd_soc_dapm_context *d; 1551 LIST_HEAD(pending); 1552 int cur_sort = -1; 1553 int cur_subseq = -1; 1554 int cur_reg = SND_SOC_NOPM; 1555 struct snd_soc_dapm_context *cur_dapm = NULL; 1556 int ret, i; 1557 int *sort; 1558 1559 if (power_up) 1560 sort = dapm_up_seq; 1561 else 1562 sort = dapm_down_seq; 1563 1564 list_for_each_entry_safe(w, n, list, power_list) { 1565 ret = 0; 1566 1567 /* Do we need to apply any queued changes? */ 1568 if (sort[w->id] != cur_sort || w->reg != cur_reg || 1569 w->dapm != cur_dapm || w->subseq != cur_subseq) { 1570 if (!list_empty(&pending)) 1571 dapm_seq_run_coalesced(card, &pending); 1572 1573 if (cur_dapm && cur_dapm->seq_notifier) { 1574 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) 1575 if (sort[i] == cur_sort) 1576 cur_dapm->seq_notifier(cur_dapm, 1577 i, 1578 cur_subseq); 1579 } 1580 1581 if (cur_dapm && w->dapm != cur_dapm) 1582 soc_dapm_async_complete(cur_dapm); 1583 1584 INIT_LIST_HEAD(&pending); 1585 cur_sort = -1; 1586 cur_subseq = INT_MIN; 1587 cur_reg = SND_SOC_NOPM; 1588 cur_dapm = NULL; 1589 } 1590 1591 switch (w->id) { 1592 case snd_soc_dapm_pre: 1593 if (!w->event) 1594 list_for_each_entry_safe_continue(w, n, list, 1595 power_list); 1596 1597 if (event == SND_SOC_DAPM_STREAM_START) 1598 ret = w->event(w, 1599 NULL, SND_SOC_DAPM_PRE_PMU); 1600 else if (event == SND_SOC_DAPM_STREAM_STOP) 1601 ret = w->event(w, 1602 NULL, SND_SOC_DAPM_PRE_PMD); 1603 break; 1604 1605 case snd_soc_dapm_post: 1606 if (!w->event) 1607 list_for_each_entry_safe_continue(w, n, list, 1608 power_list); 1609 1610 if (event == SND_SOC_DAPM_STREAM_START) 1611 ret = w->event(w, 1612 NULL, SND_SOC_DAPM_POST_PMU); 1613 else if (event == SND_SOC_DAPM_STREAM_STOP) 1614 ret = w->event(w, 1615 NULL, SND_SOC_DAPM_POST_PMD); 1616 break; 1617 1618 default: 1619 /* Queue it up for application */ 1620 cur_sort = sort[w->id]; 1621 cur_subseq = w->subseq; 1622 cur_reg = w->reg; 1623 cur_dapm = w->dapm; 1624 list_move(&w->power_list, &pending); 1625 break; 1626 } 1627 1628 if (ret < 0) 1629 dev_err(w->dapm->dev, 1630 "ASoC: Failed to apply widget power: %d\n", ret); 1631 } 1632 1633 if (!list_empty(&pending)) 1634 dapm_seq_run_coalesced(card, &pending); 1635 1636 if (cur_dapm && cur_dapm->seq_notifier) { 1637 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) 1638 if (sort[i] == cur_sort) 1639 cur_dapm->seq_notifier(cur_dapm, 1640 i, cur_subseq); 1641 } 1642 1643 list_for_each_entry(d, &card->dapm_list, list) { 1644 soc_dapm_async_complete(d); 1645 } 1646 } 1647 1648 static void dapm_widget_update(struct snd_soc_card *card) 1649 { 1650 struct snd_soc_dapm_update *update = card->update; 1651 struct snd_soc_dapm_widget_list *wlist; 1652 struct snd_soc_dapm_widget *w = NULL; 1653 unsigned int wi; 1654 int ret; 1655 1656 if (!update || !dapm_kcontrol_is_powered(update->kcontrol)) 1657 return; 1658 1659 wlist = dapm_kcontrol_get_wlist(update->kcontrol); 1660 1661 for (wi = 0; wi < wlist->num_widgets; wi++) { 1662 w = wlist->widgets[wi]; 1663 1664 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) { 1665 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG); 1666 if (ret != 0) 1667 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n", 1668 w->name, ret); 1669 } 1670 } 1671 1672 if (!w) 1673 return; 1674 1675 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask, 1676 update->val); 1677 if (ret < 0) 1678 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n", 1679 w->name, ret); 1680 1681 if (update->has_second_set) { 1682 ret = soc_dapm_update_bits(w->dapm, update->reg2, 1683 update->mask2, update->val2); 1684 if (ret < 0) 1685 dev_err(w->dapm->dev, 1686 "ASoC: %s DAPM update failed: %d\n", 1687 w->name, ret); 1688 } 1689 1690 for (wi = 0; wi < wlist->num_widgets; wi++) { 1691 w = wlist->widgets[wi]; 1692 1693 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) { 1694 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG); 1695 if (ret != 0) 1696 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n", 1697 w->name, ret); 1698 } 1699 } 1700 } 1701 1702 /* Async callback run prior to DAPM sequences - brings to _PREPARE if 1703 * they're changing state. 1704 */ 1705 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie) 1706 { 1707 struct snd_soc_dapm_context *d = data; 1708 int ret; 1709 1710 /* If we're off and we're not supposed to go into STANDBY */ 1711 if (d->bias_level == SND_SOC_BIAS_OFF && 1712 d->target_bias_level != SND_SOC_BIAS_OFF) { 1713 if (d->dev) 1714 pm_runtime_get_sync(d->dev); 1715 1716 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); 1717 if (ret != 0) 1718 dev_err(d->dev, 1719 "ASoC: Failed to turn on bias: %d\n", ret); 1720 } 1721 1722 /* Prepare for a transition to ON or away from ON */ 1723 if ((d->target_bias_level == SND_SOC_BIAS_ON && 1724 d->bias_level != SND_SOC_BIAS_ON) || 1725 (d->target_bias_level != SND_SOC_BIAS_ON && 1726 d->bias_level == SND_SOC_BIAS_ON)) { 1727 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE); 1728 if (ret != 0) 1729 dev_err(d->dev, 1730 "ASoC: Failed to prepare bias: %d\n", ret); 1731 } 1732 } 1733 1734 /* Async callback run prior to DAPM sequences - brings to their final 1735 * state. 1736 */ 1737 static void dapm_post_sequence_async(void *data, async_cookie_t cookie) 1738 { 1739 struct snd_soc_dapm_context *d = data; 1740 int ret; 1741 1742 /* If we just powered the last thing off drop to standby bias */ 1743 if (d->bias_level == SND_SOC_BIAS_PREPARE && 1744 (d->target_bias_level == SND_SOC_BIAS_STANDBY || 1745 d->target_bias_level == SND_SOC_BIAS_OFF)) { 1746 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); 1747 if (ret != 0) 1748 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n", 1749 ret); 1750 } 1751 1752 /* If we're in standby and can support bias off then do that */ 1753 if (d->bias_level == SND_SOC_BIAS_STANDBY && 1754 d->target_bias_level == SND_SOC_BIAS_OFF) { 1755 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF); 1756 if (ret != 0) 1757 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n", 1758 ret); 1759 1760 if (d->dev) 1761 pm_runtime_put(d->dev); 1762 } 1763 1764 /* If we just powered up then move to active bias */ 1765 if (d->bias_level == SND_SOC_BIAS_PREPARE && 1766 d->target_bias_level == SND_SOC_BIAS_ON) { 1767 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON); 1768 if (ret != 0) 1769 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n", 1770 ret); 1771 } 1772 } 1773 1774 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer, 1775 bool power, bool connect) 1776 { 1777 /* If a connection is being made or broken then that update 1778 * will have marked the peer dirty, otherwise the widgets are 1779 * not connected and this update has no impact. */ 1780 if (!connect) 1781 return; 1782 1783 /* If the peer is already in the state we're moving to then we 1784 * won't have an impact on it. */ 1785 if (power != peer->power) 1786 dapm_mark_dirty(peer, "peer state change"); 1787 } 1788 1789 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power, 1790 struct list_head *up_list, 1791 struct list_head *down_list) 1792 { 1793 struct snd_soc_dapm_path *path; 1794 1795 if (w->power == power) 1796 return; 1797 1798 trace_snd_soc_dapm_widget_power(w, power); 1799 1800 /* If we changed our power state perhaps our neigbours changed 1801 * also. 1802 */ 1803 snd_soc_dapm_widget_for_each_source_path(w, path) 1804 dapm_widget_set_peer_power(path->source, power, path->connect); 1805 1806 /* Supplies can't affect their outputs, only their inputs */ 1807 if (!w->is_supply) { 1808 snd_soc_dapm_widget_for_each_sink_path(w, path) 1809 dapm_widget_set_peer_power(path->sink, power, 1810 path->connect); 1811 } 1812 1813 if (power) 1814 dapm_seq_insert(w, up_list, true); 1815 else 1816 dapm_seq_insert(w, down_list, false); 1817 } 1818 1819 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w, 1820 struct list_head *up_list, 1821 struct list_head *down_list) 1822 { 1823 int power; 1824 1825 switch (w->id) { 1826 case snd_soc_dapm_pre: 1827 dapm_seq_insert(w, down_list, false); 1828 break; 1829 case snd_soc_dapm_post: 1830 dapm_seq_insert(w, up_list, true); 1831 break; 1832 1833 default: 1834 power = dapm_widget_power_check(w); 1835 1836 dapm_widget_set_power(w, power, up_list, down_list); 1837 break; 1838 } 1839 } 1840 1841 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm) 1842 { 1843 if (dapm->idle_bias_off) 1844 return true; 1845 1846 switch (snd_power_get_state(dapm->card->snd_card)) { 1847 case SNDRV_CTL_POWER_D3hot: 1848 case SNDRV_CTL_POWER_D3cold: 1849 return dapm->suspend_bias_off; 1850 default: 1851 break; 1852 } 1853 1854 return false; 1855 } 1856 1857 /* 1858 * Scan each dapm widget for complete audio path. 1859 * A complete path is a route that has valid endpoints i.e.:- 1860 * 1861 * o DAC to output pin. 1862 * o Input pin to ADC. 1863 * o Input pin to Output pin (bypass, sidetone) 1864 * o DAC to ADC (loopback). 1865 */ 1866 static int dapm_power_widgets(struct snd_soc_card *card, int event) 1867 { 1868 struct snd_soc_dapm_widget *w; 1869 struct snd_soc_dapm_context *d; 1870 LIST_HEAD(up_list); 1871 LIST_HEAD(down_list); 1872 ASYNC_DOMAIN_EXCLUSIVE(async_domain); 1873 enum snd_soc_bias_level bias; 1874 1875 lockdep_assert_held(&card->dapm_mutex); 1876 1877 trace_snd_soc_dapm_start(card); 1878 1879 list_for_each_entry(d, &card->dapm_list, list) { 1880 if (dapm_idle_bias_off(d)) 1881 d->target_bias_level = SND_SOC_BIAS_OFF; 1882 else 1883 d->target_bias_level = SND_SOC_BIAS_STANDBY; 1884 } 1885 1886 dapm_reset(card); 1887 1888 /* Check which widgets we need to power and store them in 1889 * lists indicating if they should be powered up or down. We 1890 * only check widgets that have been flagged as dirty but note 1891 * that new widgets may be added to the dirty list while we 1892 * iterate. 1893 */ 1894 list_for_each_entry(w, &card->dapm_dirty, dirty) { 1895 dapm_power_one_widget(w, &up_list, &down_list); 1896 } 1897 1898 list_for_each_entry(w, &card->widgets, list) { 1899 switch (w->id) { 1900 case snd_soc_dapm_pre: 1901 case snd_soc_dapm_post: 1902 /* These widgets always need to be powered */ 1903 break; 1904 default: 1905 list_del_init(&w->dirty); 1906 break; 1907 } 1908 1909 if (w->new_power) { 1910 d = w->dapm; 1911 1912 /* Supplies and micbiases only bring the 1913 * context up to STANDBY as unless something 1914 * else is active and passing audio they 1915 * generally don't require full power. Signal 1916 * generators are virtual pins and have no 1917 * power impact themselves. 1918 */ 1919 switch (w->id) { 1920 case snd_soc_dapm_siggen: 1921 case snd_soc_dapm_vmid: 1922 break; 1923 case snd_soc_dapm_supply: 1924 case snd_soc_dapm_regulator_supply: 1925 case snd_soc_dapm_pinctrl: 1926 case snd_soc_dapm_clock_supply: 1927 case snd_soc_dapm_micbias: 1928 if (d->target_bias_level < SND_SOC_BIAS_STANDBY) 1929 d->target_bias_level = SND_SOC_BIAS_STANDBY; 1930 break; 1931 default: 1932 d->target_bias_level = SND_SOC_BIAS_ON; 1933 break; 1934 } 1935 } 1936 1937 } 1938 1939 /* Force all contexts in the card to the same bias state if 1940 * they're not ground referenced. 1941 */ 1942 bias = SND_SOC_BIAS_OFF; 1943 list_for_each_entry(d, &card->dapm_list, list) 1944 if (d->target_bias_level > bias) 1945 bias = d->target_bias_level; 1946 list_for_each_entry(d, &card->dapm_list, list) 1947 if (!dapm_idle_bias_off(d)) 1948 d->target_bias_level = bias; 1949 1950 trace_snd_soc_dapm_walk_done(card); 1951 1952 /* Run card bias changes at first */ 1953 dapm_pre_sequence_async(&card->dapm, 0); 1954 /* Run other bias changes in parallel */ 1955 list_for_each_entry(d, &card->dapm_list, list) { 1956 if (d != &card->dapm) 1957 async_schedule_domain(dapm_pre_sequence_async, d, 1958 &async_domain); 1959 } 1960 async_synchronize_full_domain(&async_domain); 1961 1962 list_for_each_entry(w, &down_list, power_list) { 1963 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD); 1964 } 1965 1966 list_for_each_entry(w, &up_list, power_list) { 1967 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU); 1968 } 1969 1970 /* Power down widgets first; try to avoid amplifying pops. */ 1971 dapm_seq_run(card, &down_list, event, false); 1972 1973 dapm_widget_update(card); 1974 1975 /* Now power up. */ 1976 dapm_seq_run(card, &up_list, event, true); 1977 1978 /* Run all the bias changes in parallel */ 1979 list_for_each_entry(d, &card->dapm_list, list) { 1980 if (d != &card->dapm) 1981 async_schedule_domain(dapm_post_sequence_async, d, 1982 &async_domain); 1983 } 1984 async_synchronize_full_domain(&async_domain); 1985 /* Run card bias changes at last */ 1986 dapm_post_sequence_async(&card->dapm, 0); 1987 1988 /* do we need to notify any clients that DAPM event is complete */ 1989 list_for_each_entry(d, &card->dapm_list, list) { 1990 if (d->stream_event) 1991 d->stream_event(d, event); 1992 } 1993 1994 pop_dbg(card->dev, card->pop_time, 1995 "DAPM sequencing finished, waiting %dms\n", card->pop_time); 1996 pop_wait(card->pop_time); 1997 1998 trace_snd_soc_dapm_done(card); 1999 2000 return 0; 2001 } 2002 2003 #ifdef CONFIG_DEBUG_FS 2004 static ssize_t dapm_widget_power_read_file(struct file *file, 2005 char __user *user_buf, 2006 size_t count, loff_t *ppos) 2007 { 2008 struct snd_soc_dapm_widget *w = file->private_data; 2009 struct snd_soc_card *card = w->dapm->card; 2010 enum snd_soc_dapm_direction dir, rdir; 2011 char *buf; 2012 int in, out; 2013 ssize_t ret; 2014 struct snd_soc_dapm_path *p = NULL; 2015 2016 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 2017 if (!buf) 2018 return -ENOMEM; 2019 2020 mutex_lock(&card->dapm_mutex); 2021 2022 /* Supply widgets are not handled by is_connected_{input,output}_ep() */ 2023 if (w->is_supply) { 2024 in = 0; 2025 out = 0; 2026 } else { 2027 in = is_connected_input_ep(w, NULL, NULL); 2028 out = is_connected_output_ep(w, NULL, NULL); 2029 } 2030 2031 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d", 2032 w->name, w->power ? "On" : "Off", 2033 w->force ? " (forced)" : "", in, out); 2034 2035 if (w->reg >= 0) 2036 ret += snprintf(buf + ret, PAGE_SIZE - ret, 2037 " - R%d(0x%x) mask 0x%x", 2038 w->reg, w->reg, w->mask << w->shift); 2039 2040 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); 2041 2042 if (w->sname) 2043 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n", 2044 w->sname, 2045 w->active ? "active" : "inactive"); 2046 2047 snd_soc_dapm_for_each_direction(dir) { 2048 rdir = SND_SOC_DAPM_DIR_REVERSE(dir); 2049 snd_soc_dapm_widget_for_each_path(w, dir, p) { 2050 if (p->connected && !p->connected(p->source, p->sink)) 2051 continue; 2052 2053 if (!p->connect) 2054 continue; 2055 2056 ret += snprintf(buf + ret, PAGE_SIZE - ret, 2057 " %s \"%s\" \"%s\"\n", 2058 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out", 2059 p->name ? p->name : "static", 2060 p->node[rdir]->name); 2061 } 2062 } 2063 2064 mutex_unlock(&card->dapm_mutex); 2065 2066 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 2067 2068 kfree(buf); 2069 return ret; 2070 } 2071 2072 static const struct file_operations dapm_widget_power_fops = { 2073 .open = simple_open, 2074 .read = dapm_widget_power_read_file, 2075 .llseek = default_llseek, 2076 }; 2077 2078 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf, 2079 size_t count, loff_t *ppos) 2080 { 2081 struct snd_soc_dapm_context *dapm = file->private_data; 2082 char *level; 2083 2084 switch (dapm->bias_level) { 2085 case SND_SOC_BIAS_ON: 2086 level = "On\n"; 2087 break; 2088 case SND_SOC_BIAS_PREPARE: 2089 level = "Prepare\n"; 2090 break; 2091 case SND_SOC_BIAS_STANDBY: 2092 level = "Standby\n"; 2093 break; 2094 case SND_SOC_BIAS_OFF: 2095 level = "Off\n"; 2096 break; 2097 default: 2098 WARN(1, "Unknown bias_level %d\n", dapm->bias_level); 2099 level = "Unknown\n"; 2100 break; 2101 } 2102 2103 return simple_read_from_buffer(user_buf, count, ppos, level, 2104 strlen(level)); 2105 } 2106 2107 static const struct file_operations dapm_bias_fops = { 2108 .open = simple_open, 2109 .read = dapm_bias_read_file, 2110 .llseek = default_llseek, 2111 }; 2112 2113 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm, 2114 struct dentry *parent) 2115 { 2116 struct dentry *d; 2117 2118 if (!parent) 2119 return; 2120 2121 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent); 2122 2123 if (!dapm->debugfs_dapm) { 2124 dev_warn(dapm->dev, 2125 "ASoC: Failed to create DAPM debugfs directory\n"); 2126 return; 2127 } 2128 2129 d = debugfs_create_file("bias_level", 0444, 2130 dapm->debugfs_dapm, dapm, 2131 &dapm_bias_fops); 2132 if (!d) 2133 dev_warn(dapm->dev, 2134 "ASoC: Failed to create bias level debugfs file\n"); 2135 } 2136 2137 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w) 2138 { 2139 struct snd_soc_dapm_context *dapm = w->dapm; 2140 struct dentry *d; 2141 2142 if (!dapm->debugfs_dapm || !w->name) 2143 return; 2144 2145 d = debugfs_create_file(w->name, 0444, 2146 dapm->debugfs_dapm, w, 2147 &dapm_widget_power_fops); 2148 if (!d) 2149 dev_warn(w->dapm->dev, 2150 "ASoC: Failed to create %s debugfs file\n", 2151 w->name); 2152 } 2153 2154 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm) 2155 { 2156 debugfs_remove_recursive(dapm->debugfs_dapm); 2157 } 2158 2159 #else 2160 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm, 2161 struct dentry *parent) 2162 { 2163 } 2164 2165 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w) 2166 { 2167 } 2168 2169 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm) 2170 { 2171 } 2172 2173 #endif 2174 2175 /* 2176 * soc_dapm_connect_path() - Connects or disconnects a path 2177 * @path: The path to update 2178 * @connect: The new connect state of the path. True if the path is connected, 2179 * false if it is disconnected. 2180 * @reason: The reason why the path changed (for debugging only) 2181 */ 2182 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path, 2183 bool connect, const char *reason) 2184 { 2185 if (path->connect == connect) 2186 return; 2187 2188 path->connect = connect; 2189 dapm_mark_dirty(path->source, reason); 2190 dapm_mark_dirty(path->sink, reason); 2191 dapm_path_invalidate(path); 2192 } 2193 2194 /* test and update the power status of a mux widget */ 2195 static int soc_dapm_mux_update_power(struct snd_soc_card *card, 2196 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e) 2197 { 2198 struct snd_soc_dapm_path *path; 2199 int found = 0; 2200 bool connect; 2201 2202 lockdep_assert_held(&card->dapm_mutex); 2203 2204 /* find dapm widget path assoc with kcontrol */ 2205 dapm_kcontrol_for_each_path(path, kcontrol) { 2206 found = 1; 2207 /* we now need to match the string in the enum to the path */ 2208 if (!(strcmp(path->name, e->texts[mux]))) 2209 connect = true; 2210 else 2211 connect = false; 2212 2213 soc_dapm_connect_path(path, connect, "mux update"); 2214 } 2215 2216 if (found) 2217 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP); 2218 2219 return found; 2220 } 2221 2222 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm, 2223 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e, 2224 struct snd_soc_dapm_update *update) 2225 { 2226 struct snd_soc_card *card = dapm->card; 2227 int ret; 2228 2229 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 2230 card->update = update; 2231 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e); 2232 card->update = NULL; 2233 mutex_unlock(&card->dapm_mutex); 2234 if (ret > 0) 2235 soc_dpcm_runtime_update(card); 2236 return ret; 2237 } 2238 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power); 2239 2240 /* test and update the power status of a mixer or switch widget */ 2241 static int soc_dapm_mixer_update_power(struct snd_soc_card *card, 2242 struct snd_kcontrol *kcontrol, 2243 int connect, int rconnect) 2244 { 2245 struct snd_soc_dapm_path *path; 2246 int found = 0; 2247 2248 lockdep_assert_held(&card->dapm_mutex); 2249 2250 /* find dapm widget path assoc with kcontrol */ 2251 dapm_kcontrol_for_each_path(path, kcontrol) { 2252 /* 2253 * Ideally this function should support any number of 2254 * paths and channels. But since kcontrols only come 2255 * in mono and stereo variants, we are limited to 2 2256 * channels. 2257 * 2258 * The following code assumes for stereo controls the 2259 * first path (when 'found == 0') is the left channel, 2260 * and all remaining paths (when 'found == 1') are the 2261 * right channel. 2262 * 2263 * A stereo control is signified by a valid 'rconnect' 2264 * value, either 0 for unconnected, or >= 0 for connected. 2265 * This is chosen instead of using snd_soc_volsw_is_stereo, 2266 * so that the behavior of snd_soc_dapm_mixer_update_power 2267 * doesn't change even when the kcontrol passed in is 2268 * stereo. 2269 * 2270 * It passes 'connect' as the path connect status for 2271 * the left channel, and 'rconnect' for the right 2272 * channel. 2273 */ 2274 if (found && rconnect >= 0) 2275 soc_dapm_connect_path(path, rconnect, "mixer update"); 2276 else 2277 soc_dapm_connect_path(path, connect, "mixer update"); 2278 found = 1; 2279 } 2280 2281 if (found) 2282 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP); 2283 2284 return found; 2285 } 2286 2287 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm, 2288 struct snd_kcontrol *kcontrol, int connect, 2289 struct snd_soc_dapm_update *update) 2290 { 2291 struct snd_soc_card *card = dapm->card; 2292 int ret; 2293 2294 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 2295 card->update = update; 2296 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1); 2297 card->update = NULL; 2298 mutex_unlock(&card->dapm_mutex); 2299 if (ret > 0) 2300 soc_dpcm_runtime_update(card); 2301 return ret; 2302 } 2303 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power); 2304 2305 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt, 2306 char *buf) 2307 { 2308 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt); 2309 struct snd_soc_dapm_widget *w; 2310 int count = 0; 2311 char *state = "not set"; 2312 2313 /* card won't be set for the dummy component, as a spot fix 2314 * we're checking for that case specifically here but in future 2315 * we will ensure that the dummy component looks like others. 2316 */ 2317 if (!cmpnt->card) 2318 return 0; 2319 2320 list_for_each_entry(w, &cmpnt->card->widgets, list) { 2321 if (w->dapm != dapm) 2322 continue; 2323 2324 /* only display widgets that burn power */ 2325 switch (w->id) { 2326 case snd_soc_dapm_hp: 2327 case snd_soc_dapm_mic: 2328 case snd_soc_dapm_spk: 2329 case snd_soc_dapm_line: 2330 case snd_soc_dapm_micbias: 2331 case snd_soc_dapm_dac: 2332 case snd_soc_dapm_adc: 2333 case snd_soc_dapm_pga: 2334 case snd_soc_dapm_out_drv: 2335 case snd_soc_dapm_mixer: 2336 case snd_soc_dapm_mixer_named_ctl: 2337 case snd_soc_dapm_supply: 2338 case snd_soc_dapm_regulator_supply: 2339 case snd_soc_dapm_pinctrl: 2340 case snd_soc_dapm_clock_supply: 2341 if (w->name) 2342 count += sprintf(buf + count, "%s: %s\n", 2343 w->name, w->power ? "On":"Off"); 2344 break; 2345 default: 2346 break; 2347 } 2348 } 2349 2350 switch (snd_soc_dapm_get_bias_level(dapm)) { 2351 case SND_SOC_BIAS_ON: 2352 state = "On"; 2353 break; 2354 case SND_SOC_BIAS_PREPARE: 2355 state = "Prepare"; 2356 break; 2357 case SND_SOC_BIAS_STANDBY: 2358 state = "Standby"; 2359 break; 2360 case SND_SOC_BIAS_OFF: 2361 state = "Off"; 2362 break; 2363 } 2364 count += sprintf(buf + count, "PM State: %s\n", state); 2365 2366 return count; 2367 } 2368 2369 /* show dapm widget status in sys fs */ 2370 static ssize_t dapm_widget_show(struct device *dev, 2371 struct device_attribute *attr, char *buf) 2372 { 2373 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 2374 int i, count = 0; 2375 2376 mutex_lock(&rtd->card->dapm_mutex); 2377 2378 for (i = 0; i < rtd->num_codecs; i++) { 2379 struct snd_soc_component *cmpnt = rtd->codec_dais[i]->component; 2380 2381 count += dapm_widget_show_component(cmpnt, buf + count); 2382 } 2383 2384 mutex_unlock(&rtd->card->dapm_mutex); 2385 2386 return count; 2387 } 2388 2389 static DEVICE_ATTR_RO(dapm_widget); 2390 2391 struct attribute *soc_dapm_dev_attrs[] = { 2392 &dev_attr_dapm_widget.attr, 2393 NULL 2394 }; 2395 2396 static void dapm_free_path(struct snd_soc_dapm_path *path) 2397 { 2398 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]); 2399 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]); 2400 list_del(&path->list_kcontrol); 2401 list_del(&path->list); 2402 kfree(path); 2403 } 2404 2405 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w) 2406 { 2407 struct snd_soc_dapm_path *p, *next_p; 2408 enum snd_soc_dapm_direction dir; 2409 2410 list_del(&w->list); 2411 /* 2412 * remove source and sink paths associated to this widget. 2413 * While removing the path, remove reference to it from both 2414 * source and sink widgets so that path is removed only once. 2415 */ 2416 snd_soc_dapm_for_each_direction(dir) { 2417 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) 2418 dapm_free_path(p); 2419 } 2420 2421 kfree(w->kcontrols); 2422 kfree_const(w->name); 2423 kfree(w); 2424 } 2425 2426 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm) 2427 { 2428 dapm->path_sink_cache.widget = NULL; 2429 dapm->path_source_cache.widget = NULL; 2430 } 2431 2432 /* free all dapm widgets and resources */ 2433 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm) 2434 { 2435 struct snd_soc_dapm_widget *w, *next_w; 2436 2437 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { 2438 if (w->dapm != dapm) 2439 continue; 2440 snd_soc_dapm_free_widget(w); 2441 } 2442 snd_soc_dapm_reset_cache(dapm); 2443 } 2444 2445 static struct snd_soc_dapm_widget *dapm_find_widget( 2446 struct snd_soc_dapm_context *dapm, const char *pin, 2447 bool search_other_contexts) 2448 { 2449 struct snd_soc_dapm_widget *w; 2450 struct snd_soc_dapm_widget *fallback = NULL; 2451 2452 list_for_each_entry(w, &dapm->card->widgets, list) { 2453 if (!strcmp(w->name, pin)) { 2454 if (w->dapm == dapm) 2455 return w; 2456 else 2457 fallback = w; 2458 } 2459 } 2460 2461 if (search_other_contexts) 2462 return fallback; 2463 2464 return NULL; 2465 } 2466 2467 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm, 2468 const char *pin, int status) 2469 { 2470 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 2471 2472 dapm_assert_locked(dapm); 2473 2474 if (!w) { 2475 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin); 2476 return -EINVAL; 2477 } 2478 2479 if (w->connected != status) { 2480 dapm_mark_dirty(w, "pin configuration"); 2481 dapm_widget_invalidate_input_paths(w); 2482 dapm_widget_invalidate_output_paths(w); 2483 } 2484 2485 w->connected = status; 2486 if (status == 0) 2487 w->force = 0; 2488 2489 return 0; 2490 } 2491 2492 /** 2493 * snd_soc_dapm_sync_unlocked - scan and power dapm paths 2494 * @dapm: DAPM context 2495 * 2496 * Walks all dapm audio paths and powers widgets according to their 2497 * stream or path usage. 2498 * 2499 * Requires external locking. 2500 * 2501 * Returns 0 for success. 2502 */ 2503 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm) 2504 { 2505 /* 2506 * Suppress early reports (eg, jacks syncing their state) to avoid 2507 * silly DAPM runs during card startup. 2508 */ 2509 if (!dapm->card || !dapm->card->instantiated) 2510 return 0; 2511 2512 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP); 2513 } 2514 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked); 2515 2516 /** 2517 * snd_soc_dapm_sync - scan and power dapm paths 2518 * @dapm: DAPM context 2519 * 2520 * Walks all dapm audio paths and powers widgets according to their 2521 * stream or path usage. 2522 * 2523 * Returns 0 for success. 2524 */ 2525 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm) 2526 { 2527 int ret; 2528 2529 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 2530 ret = snd_soc_dapm_sync_unlocked(dapm); 2531 mutex_unlock(&dapm->card->dapm_mutex); 2532 return ret; 2533 } 2534 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync); 2535 2536 /* 2537 * dapm_update_widget_flags() - Re-compute widget sink and source flags 2538 * @w: The widget for which to update the flags 2539 * 2540 * Some widgets have a dynamic category which depends on which neighbors they 2541 * are connected to. This function update the category for these widgets. 2542 * 2543 * This function must be called whenever a path is added or removed to a widget. 2544 */ 2545 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w) 2546 { 2547 enum snd_soc_dapm_direction dir; 2548 struct snd_soc_dapm_path *p; 2549 unsigned int ep; 2550 2551 switch (w->id) { 2552 case snd_soc_dapm_input: 2553 /* On a fully routed card an input is never a source */ 2554 if (w->dapm->card->fully_routed) 2555 return; 2556 ep = SND_SOC_DAPM_EP_SOURCE; 2557 snd_soc_dapm_widget_for_each_source_path(w, p) { 2558 if (p->source->id == snd_soc_dapm_micbias || 2559 p->source->id == snd_soc_dapm_mic || 2560 p->source->id == snd_soc_dapm_line || 2561 p->source->id == snd_soc_dapm_output) { 2562 ep = 0; 2563 break; 2564 } 2565 } 2566 break; 2567 case snd_soc_dapm_output: 2568 /* On a fully routed card a output is never a sink */ 2569 if (w->dapm->card->fully_routed) 2570 return; 2571 ep = SND_SOC_DAPM_EP_SINK; 2572 snd_soc_dapm_widget_for_each_sink_path(w, p) { 2573 if (p->sink->id == snd_soc_dapm_spk || 2574 p->sink->id == snd_soc_dapm_hp || 2575 p->sink->id == snd_soc_dapm_line || 2576 p->sink->id == snd_soc_dapm_input) { 2577 ep = 0; 2578 break; 2579 } 2580 } 2581 break; 2582 case snd_soc_dapm_line: 2583 ep = 0; 2584 snd_soc_dapm_for_each_direction(dir) { 2585 if (!list_empty(&w->edges[dir])) 2586 ep |= SND_SOC_DAPM_DIR_TO_EP(dir); 2587 } 2588 break; 2589 default: 2590 return; 2591 } 2592 2593 w->is_ep = ep; 2594 } 2595 2596 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm, 2597 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink, 2598 const char *control) 2599 { 2600 bool dynamic_source = false; 2601 bool dynamic_sink = false; 2602 2603 if (!control) 2604 return 0; 2605 2606 switch (source->id) { 2607 case snd_soc_dapm_demux: 2608 dynamic_source = true; 2609 break; 2610 default: 2611 break; 2612 } 2613 2614 switch (sink->id) { 2615 case snd_soc_dapm_mux: 2616 case snd_soc_dapm_switch: 2617 case snd_soc_dapm_mixer: 2618 case snd_soc_dapm_mixer_named_ctl: 2619 dynamic_sink = true; 2620 break; 2621 default: 2622 break; 2623 } 2624 2625 if (dynamic_source && dynamic_sink) { 2626 dev_err(dapm->dev, 2627 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n", 2628 source->name, control, sink->name); 2629 return -EINVAL; 2630 } else if (!dynamic_source && !dynamic_sink) { 2631 dev_err(dapm->dev, 2632 "Control not supported for path %s -> [%s] -> %s\n", 2633 source->name, control, sink->name); 2634 return -EINVAL; 2635 } 2636 2637 return 0; 2638 } 2639 2640 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm, 2641 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink, 2642 const char *control, 2643 int (*connected)(struct snd_soc_dapm_widget *source, 2644 struct snd_soc_dapm_widget *sink)) 2645 { 2646 struct snd_soc_dapm_widget *widgets[2]; 2647 enum snd_soc_dapm_direction dir; 2648 struct snd_soc_dapm_path *path; 2649 int ret; 2650 2651 if (wsink->is_supply && !wsource->is_supply) { 2652 dev_err(dapm->dev, 2653 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n", 2654 wsource->name, wsink->name); 2655 return -EINVAL; 2656 } 2657 2658 if (connected && !wsource->is_supply) { 2659 dev_err(dapm->dev, 2660 "connected() callback only supported for supply widgets (%s -> %s)\n", 2661 wsource->name, wsink->name); 2662 return -EINVAL; 2663 } 2664 2665 if (wsource->is_supply && control) { 2666 dev_err(dapm->dev, 2667 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n", 2668 wsource->name, control, wsink->name); 2669 return -EINVAL; 2670 } 2671 2672 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control); 2673 if (ret) 2674 return ret; 2675 2676 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL); 2677 if (!path) 2678 return -ENOMEM; 2679 2680 path->node[SND_SOC_DAPM_DIR_IN] = wsource; 2681 path->node[SND_SOC_DAPM_DIR_OUT] = wsink; 2682 widgets[SND_SOC_DAPM_DIR_IN] = wsource; 2683 widgets[SND_SOC_DAPM_DIR_OUT] = wsink; 2684 2685 path->connected = connected; 2686 INIT_LIST_HEAD(&path->list); 2687 INIT_LIST_HEAD(&path->list_kcontrol); 2688 2689 if (wsource->is_supply || wsink->is_supply) 2690 path->is_supply = 1; 2691 2692 /* connect static paths */ 2693 if (control == NULL) { 2694 path->connect = 1; 2695 } else { 2696 switch (wsource->id) { 2697 case snd_soc_dapm_demux: 2698 ret = dapm_connect_mux(dapm, path, control, wsource); 2699 if (ret) 2700 goto err; 2701 break; 2702 default: 2703 break; 2704 } 2705 2706 switch (wsink->id) { 2707 case snd_soc_dapm_mux: 2708 ret = dapm_connect_mux(dapm, path, control, wsink); 2709 if (ret != 0) 2710 goto err; 2711 break; 2712 case snd_soc_dapm_switch: 2713 case snd_soc_dapm_mixer: 2714 case snd_soc_dapm_mixer_named_ctl: 2715 ret = dapm_connect_mixer(dapm, path, control); 2716 if (ret != 0) 2717 goto err; 2718 break; 2719 default: 2720 break; 2721 } 2722 } 2723 2724 list_add(&path->list, &dapm->card->paths); 2725 snd_soc_dapm_for_each_direction(dir) 2726 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]); 2727 2728 snd_soc_dapm_for_each_direction(dir) { 2729 dapm_update_widget_flags(widgets[dir]); 2730 dapm_mark_dirty(widgets[dir], "Route added"); 2731 } 2732 2733 if (dapm->card->instantiated && path->connect) 2734 dapm_path_invalidate(path); 2735 2736 return 0; 2737 err: 2738 kfree(path); 2739 return ret; 2740 } 2741 2742 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm, 2743 const struct snd_soc_dapm_route *route) 2744 { 2745 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w; 2746 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL; 2747 const char *sink; 2748 const char *source; 2749 char prefixed_sink[80]; 2750 char prefixed_source[80]; 2751 const char *prefix; 2752 int ret; 2753 2754 prefix = soc_dapm_prefix(dapm); 2755 if (prefix) { 2756 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s", 2757 prefix, route->sink); 2758 sink = prefixed_sink; 2759 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s", 2760 prefix, route->source); 2761 source = prefixed_source; 2762 } else { 2763 sink = route->sink; 2764 source = route->source; 2765 } 2766 2767 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source); 2768 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink); 2769 2770 if (wsink && wsource) 2771 goto skip_search; 2772 2773 /* 2774 * find src and dest widgets over all widgets but favor a widget from 2775 * current DAPM context 2776 */ 2777 list_for_each_entry(w, &dapm->card->widgets, list) { 2778 if (!wsink && !(strcmp(w->name, sink))) { 2779 wtsink = w; 2780 if (w->dapm == dapm) { 2781 wsink = w; 2782 if (wsource) 2783 break; 2784 } 2785 continue; 2786 } 2787 if (!wsource && !(strcmp(w->name, source))) { 2788 wtsource = w; 2789 if (w->dapm == dapm) { 2790 wsource = w; 2791 if (wsink) 2792 break; 2793 } 2794 } 2795 } 2796 /* use widget from another DAPM context if not found from this */ 2797 if (!wsink) 2798 wsink = wtsink; 2799 if (!wsource) 2800 wsource = wtsource; 2801 2802 if (wsource == NULL) { 2803 dev_err(dapm->dev, "ASoC: no source widget found for %s\n", 2804 route->source); 2805 return -ENODEV; 2806 } 2807 if (wsink == NULL) { 2808 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n", 2809 route->sink); 2810 return -ENODEV; 2811 } 2812 2813 skip_search: 2814 dapm_wcache_update(&dapm->path_sink_cache, wsink); 2815 dapm_wcache_update(&dapm->path_source_cache, wsource); 2816 2817 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control, 2818 route->connected); 2819 if (ret) 2820 goto err; 2821 2822 return 0; 2823 err: 2824 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n", 2825 source, route->control, sink); 2826 return ret; 2827 } 2828 2829 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm, 2830 const struct snd_soc_dapm_route *route) 2831 { 2832 struct snd_soc_dapm_widget *wsource, *wsink; 2833 struct snd_soc_dapm_path *path, *p; 2834 const char *sink; 2835 const char *source; 2836 char prefixed_sink[80]; 2837 char prefixed_source[80]; 2838 const char *prefix; 2839 2840 if (route->control) { 2841 dev_err(dapm->dev, 2842 "ASoC: Removal of routes with controls not supported\n"); 2843 return -EINVAL; 2844 } 2845 2846 prefix = soc_dapm_prefix(dapm); 2847 if (prefix) { 2848 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s", 2849 prefix, route->sink); 2850 sink = prefixed_sink; 2851 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s", 2852 prefix, route->source); 2853 source = prefixed_source; 2854 } else { 2855 sink = route->sink; 2856 source = route->source; 2857 } 2858 2859 path = NULL; 2860 list_for_each_entry(p, &dapm->card->paths, list) { 2861 if (strcmp(p->source->name, source) != 0) 2862 continue; 2863 if (strcmp(p->sink->name, sink) != 0) 2864 continue; 2865 path = p; 2866 break; 2867 } 2868 2869 if (path) { 2870 wsource = path->source; 2871 wsink = path->sink; 2872 2873 dapm_mark_dirty(wsource, "Route removed"); 2874 dapm_mark_dirty(wsink, "Route removed"); 2875 if (path->connect) 2876 dapm_path_invalidate(path); 2877 2878 dapm_free_path(path); 2879 2880 /* Update any path related flags */ 2881 dapm_update_widget_flags(wsource); 2882 dapm_update_widget_flags(wsink); 2883 } else { 2884 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n", 2885 source, sink); 2886 } 2887 2888 return 0; 2889 } 2890 2891 /** 2892 * snd_soc_dapm_add_routes - Add routes between DAPM widgets 2893 * @dapm: DAPM context 2894 * @route: audio routes 2895 * @num: number of routes 2896 * 2897 * Connects 2 dapm widgets together via a named audio path. The sink is 2898 * the widget receiving the audio signal, whilst the source is the sender 2899 * of the audio signal. 2900 * 2901 * Returns 0 for success else error. On error all resources can be freed 2902 * with a call to snd_soc_card_free(). 2903 */ 2904 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm, 2905 const struct snd_soc_dapm_route *route, int num) 2906 { 2907 int i, r, ret = 0; 2908 2909 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 2910 for (i = 0; i < num; i++) { 2911 r = snd_soc_dapm_add_route(dapm, route); 2912 if (r < 0) { 2913 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n", 2914 route->source, 2915 route->control ? route->control : "direct", 2916 route->sink); 2917 ret = r; 2918 } 2919 route++; 2920 } 2921 mutex_unlock(&dapm->card->dapm_mutex); 2922 2923 return ret; 2924 } 2925 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes); 2926 2927 /** 2928 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets 2929 * @dapm: DAPM context 2930 * @route: audio routes 2931 * @num: number of routes 2932 * 2933 * Removes routes from the DAPM context. 2934 */ 2935 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm, 2936 const struct snd_soc_dapm_route *route, int num) 2937 { 2938 int i; 2939 2940 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 2941 for (i = 0; i < num; i++) { 2942 snd_soc_dapm_del_route(dapm, route); 2943 route++; 2944 } 2945 mutex_unlock(&dapm->card->dapm_mutex); 2946 2947 return 0; 2948 } 2949 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes); 2950 2951 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm, 2952 const struct snd_soc_dapm_route *route) 2953 { 2954 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm, 2955 route->source, 2956 true); 2957 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm, 2958 route->sink, 2959 true); 2960 struct snd_soc_dapm_path *path; 2961 int count = 0; 2962 2963 if (!source) { 2964 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n", 2965 route->source); 2966 return -ENODEV; 2967 } 2968 2969 if (!sink) { 2970 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n", 2971 route->sink); 2972 return -ENODEV; 2973 } 2974 2975 if (route->control || route->connected) 2976 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n", 2977 route->source, route->sink); 2978 2979 snd_soc_dapm_widget_for_each_sink_path(source, path) { 2980 if (path->sink == sink) { 2981 path->weak = 1; 2982 count++; 2983 } 2984 } 2985 2986 if (count == 0) 2987 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n", 2988 route->source, route->sink); 2989 if (count > 1) 2990 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n", 2991 count, route->source, route->sink); 2992 2993 return 0; 2994 } 2995 2996 /** 2997 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak 2998 * @dapm: DAPM context 2999 * @route: audio routes 3000 * @num: number of routes 3001 * 3002 * Mark existing routes matching those specified in the passed array 3003 * as being weak, meaning that they are ignored for the purpose of 3004 * power decisions. The main intended use case is for sidetone paths 3005 * which couple audio between other independent paths if they are both 3006 * active in order to make the combination work better at the user 3007 * level but which aren't intended to be "used". 3008 * 3009 * Note that CODEC drivers should not use this as sidetone type paths 3010 * can frequently also be used as bypass paths. 3011 */ 3012 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm, 3013 const struct snd_soc_dapm_route *route, int num) 3014 { 3015 int i, err; 3016 int ret = 0; 3017 3018 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT); 3019 for (i = 0; i < num; i++) { 3020 err = snd_soc_dapm_weak_route(dapm, route); 3021 if (err) 3022 ret = err; 3023 route++; 3024 } 3025 mutex_unlock(&dapm->card->dapm_mutex); 3026 3027 return ret; 3028 } 3029 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes); 3030 3031 /** 3032 * snd_soc_dapm_new_widgets - add new dapm widgets 3033 * @card: card to be checked for new dapm widgets 3034 * 3035 * Checks the codec for any new dapm widgets and creates them if found. 3036 * 3037 * Returns 0 for success. 3038 */ 3039 int snd_soc_dapm_new_widgets(struct snd_soc_card *card) 3040 { 3041 struct snd_soc_dapm_widget *w; 3042 unsigned int val; 3043 3044 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT); 3045 3046 list_for_each_entry(w, &card->widgets, list) 3047 { 3048 if (w->new) 3049 continue; 3050 3051 if (w->num_kcontrols) { 3052 w->kcontrols = kcalloc(w->num_kcontrols, 3053 sizeof(struct snd_kcontrol *), 3054 GFP_KERNEL); 3055 if (!w->kcontrols) { 3056 mutex_unlock(&card->dapm_mutex); 3057 return -ENOMEM; 3058 } 3059 } 3060 3061 switch(w->id) { 3062 case snd_soc_dapm_switch: 3063 case snd_soc_dapm_mixer: 3064 case snd_soc_dapm_mixer_named_ctl: 3065 dapm_new_mixer(w); 3066 break; 3067 case snd_soc_dapm_mux: 3068 case snd_soc_dapm_demux: 3069 dapm_new_mux(w); 3070 break; 3071 case snd_soc_dapm_pga: 3072 case snd_soc_dapm_out_drv: 3073 dapm_new_pga(w); 3074 break; 3075 case snd_soc_dapm_dai_link: 3076 dapm_new_dai_link(w); 3077 break; 3078 default: 3079 break; 3080 } 3081 3082 /* Read the initial power state from the device */ 3083 if (w->reg >= 0) { 3084 soc_dapm_read(w->dapm, w->reg, &val); 3085 val = val >> w->shift; 3086 val &= w->mask; 3087 if (val == w->on_val) 3088 w->power = 1; 3089 } 3090 3091 w->new = 1; 3092 3093 dapm_mark_dirty(w, "new widget"); 3094 dapm_debugfs_add_widget(w); 3095 } 3096 3097 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP); 3098 mutex_unlock(&card->dapm_mutex); 3099 return 0; 3100 } 3101 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets); 3102 3103 /** 3104 * snd_soc_dapm_get_volsw - dapm mixer get callback 3105 * @kcontrol: mixer control 3106 * @ucontrol: control element information 3107 * 3108 * Callback to get the value of a dapm mixer control. 3109 * 3110 * Returns 0 for success. 3111 */ 3112 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol, 3113 struct snd_ctl_elem_value *ucontrol) 3114 { 3115 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3116 struct snd_soc_card *card = dapm->card; 3117 struct soc_mixer_control *mc = 3118 (struct soc_mixer_control *)kcontrol->private_value; 3119 int reg = mc->reg; 3120 unsigned int shift = mc->shift; 3121 int max = mc->max; 3122 unsigned int width = fls(max); 3123 unsigned int mask = (1 << fls(max)) - 1; 3124 unsigned int invert = mc->invert; 3125 unsigned int reg_val, val, rval = 0; 3126 int ret = 0; 3127 3128 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3129 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) { 3130 ret = soc_dapm_read(dapm, reg, ®_val); 3131 val = (reg_val >> shift) & mask; 3132 3133 if (ret == 0 && reg != mc->rreg) 3134 ret = soc_dapm_read(dapm, mc->rreg, ®_val); 3135 3136 if (snd_soc_volsw_is_stereo(mc)) 3137 rval = (reg_val >> mc->rshift) & mask; 3138 } else { 3139 reg_val = dapm_kcontrol_get_value(kcontrol); 3140 val = reg_val & mask; 3141 3142 if (snd_soc_volsw_is_stereo(mc)) 3143 rval = (reg_val >> width) & mask; 3144 } 3145 mutex_unlock(&card->dapm_mutex); 3146 3147 if (ret) 3148 return ret; 3149 3150 if (invert) 3151 ucontrol->value.integer.value[0] = max - val; 3152 else 3153 ucontrol->value.integer.value[0] = val; 3154 3155 if (snd_soc_volsw_is_stereo(mc)) { 3156 if (invert) 3157 ucontrol->value.integer.value[1] = max - rval; 3158 else 3159 ucontrol->value.integer.value[1] = rval; 3160 } 3161 3162 return ret; 3163 } 3164 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw); 3165 3166 /** 3167 * snd_soc_dapm_put_volsw - dapm mixer set callback 3168 * @kcontrol: mixer control 3169 * @ucontrol: control element information 3170 * 3171 * Callback to set the value of a dapm mixer control. 3172 * 3173 * Returns 0 for success. 3174 */ 3175 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol, 3176 struct snd_ctl_elem_value *ucontrol) 3177 { 3178 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3179 struct snd_soc_card *card = dapm->card; 3180 struct soc_mixer_control *mc = 3181 (struct soc_mixer_control *)kcontrol->private_value; 3182 int reg = mc->reg; 3183 unsigned int shift = mc->shift; 3184 int max = mc->max; 3185 unsigned int width = fls(max); 3186 unsigned int mask = (1 << width) - 1; 3187 unsigned int invert = mc->invert; 3188 unsigned int val, rval = 0; 3189 int connect, rconnect = -1, change, reg_change = 0; 3190 struct snd_soc_dapm_update update = {}; 3191 int ret = 0; 3192 3193 val = (ucontrol->value.integer.value[0] & mask); 3194 connect = !!val; 3195 3196 if (invert) 3197 val = max - val; 3198 3199 if (snd_soc_volsw_is_stereo(mc)) { 3200 rval = (ucontrol->value.integer.value[1] & mask); 3201 rconnect = !!rval; 3202 if (invert) 3203 rval = max - rval; 3204 } 3205 3206 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3207 3208 /* This assumes field width < (bits in unsigned int / 2) */ 3209 if (width > sizeof(unsigned int) * 8 / 2) 3210 dev_warn(dapm->dev, 3211 "ASoC: control %s field width limit exceeded\n", 3212 kcontrol->id.name); 3213 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width)); 3214 3215 if (reg != SND_SOC_NOPM) { 3216 val = val << shift; 3217 rval = rval << mc->rshift; 3218 3219 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val); 3220 3221 if (snd_soc_volsw_is_stereo(mc)) 3222 reg_change |= soc_dapm_test_bits(dapm, mc->rreg, 3223 mask << mc->rshift, 3224 rval); 3225 } 3226 3227 if (change || reg_change) { 3228 if (reg_change) { 3229 if (snd_soc_volsw_is_stereo(mc)) { 3230 update.has_second_set = true; 3231 update.reg2 = mc->rreg; 3232 update.mask2 = mask << mc->rshift; 3233 update.val2 = rval; 3234 } 3235 update.kcontrol = kcontrol; 3236 update.reg = reg; 3237 update.mask = mask << shift; 3238 update.val = val; 3239 card->update = &update; 3240 } 3241 change |= reg_change; 3242 3243 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, 3244 rconnect); 3245 3246 card->update = NULL; 3247 } 3248 3249 mutex_unlock(&card->dapm_mutex); 3250 3251 if (ret > 0) 3252 soc_dpcm_runtime_update(card); 3253 3254 return change; 3255 } 3256 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw); 3257 3258 /** 3259 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback 3260 * @kcontrol: mixer control 3261 * @ucontrol: control element information 3262 * 3263 * Callback to get the value of a dapm enumerated double mixer control. 3264 * 3265 * Returns 0 for success. 3266 */ 3267 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol, 3268 struct snd_ctl_elem_value *ucontrol) 3269 { 3270 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3271 struct snd_soc_card *card = dapm->card; 3272 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 3273 unsigned int reg_val, val; 3274 3275 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3276 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) { 3277 int ret = soc_dapm_read(dapm, e->reg, ®_val); 3278 if (ret) { 3279 mutex_unlock(&card->dapm_mutex); 3280 return ret; 3281 } 3282 } else { 3283 reg_val = dapm_kcontrol_get_value(kcontrol); 3284 } 3285 mutex_unlock(&card->dapm_mutex); 3286 3287 val = (reg_val >> e->shift_l) & e->mask; 3288 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val); 3289 if (e->shift_l != e->shift_r) { 3290 val = (reg_val >> e->shift_r) & e->mask; 3291 val = snd_soc_enum_val_to_item(e, val); 3292 ucontrol->value.enumerated.item[1] = val; 3293 } 3294 3295 return 0; 3296 } 3297 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double); 3298 3299 /** 3300 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback 3301 * @kcontrol: mixer control 3302 * @ucontrol: control element information 3303 * 3304 * Callback to set the value of a dapm enumerated double mixer control. 3305 * 3306 * Returns 0 for success. 3307 */ 3308 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol, 3309 struct snd_ctl_elem_value *ucontrol) 3310 { 3311 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3312 struct snd_soc_card *card = dapm->card; 3313 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 3314 unsigned int *item = ucontrol->value.enumerated.item; 3315 unsigned int val, change, reg_change = 0; 3316 unsigned int mask; 3317 struct snd_soc_dapm_update update = {}; 3318 int ret = 0; 3319 3320 if (item[0] >= e->items) 3321 return -EINVAL; 3322 3323 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 3324 mask = e->mask << e->shift_l; 3325 if (e->shift_l != e->shift_r) { 3326 if (item[1] > e->items) 3327 return -EINVAL; 3328 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r; 3329 mask |= e->mask << e->shift_r; 3330 } 3331 3332 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3333 3334 change = dapm_kcontrol_set_value(kcontrol, val); 3335 3336 if (e->reg != SND_SOC_NOPM) 3337 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val); 3338 3339 if (change || reg_change) { 3340 if (reg_change) { 3341 update.kcontrol = kcontrol; 3342 update.reg = e->reg; 3343 update.mask = mask; 3344 update.val = val; 3345 card->update = &update; 3346 } 3347 change |= reg_change; 3348 3349 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e); 3350 3351 card->update = NULL; 3352 } 3353 3354 mutex_unlock(&card->dapm_mutex); 3355 3356 if (ret > 0) 3357 soc_dpcm_runtime_update(card); 3358 3359 return change; 3360 } 3361 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double); 3362 3363 /** 3364 * snd_soc_dapm_info_pin_switch - Info for a pin switch 3365 * 3366 * @kcontrol: mixer control 3367 * @uinfo: control element information 3368 * 3369 * Callback to provide information about a pin switch control. 3370 */ 3371 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol, 3372 struct snd_ctl_elem_info *uinfo) 3373 { 3374 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 3375 uinfo->count = 1; 3376 uinfo->value.integer.min = 0; 3377 uinfo->value.integer.max = 1; 3378 3379 return 0; 3380 } 3381 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch); 3382 3383 /** 3384 * snd_soc_dapm_get_pin_switch - Get information for a pin switch 3385 * 3386 * @kcontrol: mixer control 3387 * @ucontrol: Value 3388 */ 3389 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol, 3390 struct snd_ctl_elem_value *ucontrol) 3391 { 3392 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 3393 const char *pin = (const char *)kcontrol->private_value; 3394 3395 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3396 3397 ucontrol->value.integer.value[0] = 3398 snd_soc_dapm_get_pin_status(&card->dapm, pin); 3399 3400 mutex_unlock(&card->dapm_mutex); 3401 3402 return 0; 3403 } 3404 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch); 3405 3406 /** 3407 * snd_soc_dapm_put_pin_switch - Set information for a pin switch 3408 * 3409 * @kcontrol: mixer control 3410 * @ucontrol: Value 3411 */ 3412 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol, 3413 struct snd_ctl_elem_value *ucontrol) 3414 { 3415 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 3416 const char *pin = (const char *)kcontrol->private_value; 3417 3418 if (ucontrol->value.integer.value[0]) 3419 snd_soc_dapm_enable_pin(&card->dapm, pin); 3420 else 3421 snd_soc_dapm_disable_pin(&card->dapm, pin); 3422 3423 snd_soc_dapm_sync(&card->dapm); 3424 return 0; 3425 } 3426 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch); 3427 3428 struct snd_soc_dapm_widget * 3429 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, 3430 const struct snd_soc_dapm_widget *widget) 3431 { 3432 struct snd_soc_dapm_widget *w; 3433 3434 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 3435 w = snd_soc_dapm_new_control_unlocked(dapm, widget); 3436 /* Do not nag about probe deferrals */ 3437 if (IS_ERR(w)) { 3438 int ret = PTR_ERR(w); 3439 3440 if (ret != -EPROBE_DEFER) 3441 dev_err(dapm->dev, 3442 "ASoC: Failed to create DAPM control %s (%d)\n", 3443 widget->name, ret); 3444 goto out_unlock; 3445 } 3446 if (!w) 3447 dev_err(dapm->dev, 3448 "ASoC: Failed to create DAPM control %s\n", 3449 widget->name); 3450 3451 out_unlock: 3452 mutex_unlock(&dapm->card->dapm_mutex); 3453 return w; 3454 } 3455 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control); 3456 3457 struct snd_soc_dapm_widget * 3458 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, 3459 const struct snd_soc_dapm_widget *widget) 3460 { 3461 enum snd_soc_dapm_direction dir; 3462 struct snd_soc_dapm_widget *w; 3463 const char *prefix; 3464 int ret; 3465 3466 if ((w = dapm_cnew_widget(widget)) == NULL) 3467 return NULL; 3468 3469 switch (w->id) { 3470 case snd_soc_dapm_regulator_supply: 3471 w->regulator = devm_regulator_get(dapm->dev, w->name); 3472 if (IS_ERR(w->regulator)) { 3473 ret = PTR_ERR(w->regulator); 3474 if (ret == -EPROBE_DEFER) 3475 return ERR_PTR(ret); 3476 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n", 3477 w->name, ret); 3478 return NULL; 3479 } 3480 3481 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) { 3482 ret = regulator_allow_bypass(w->regulator, true); 3483 if (ret != 0) 3484 dev_warn(w->dapm->dev, 3485 "ASoC: Failed to bypass %s: %d\n", 3486 w->name, ret); 3487 } 3488 break; 3489 case snd_soc_dapm_pinctrl: 3490 w->pinctrl = devm_pinctrl_get(dapm->dev); 3491 if (IS_ERR_OR_NULL(w->pinctrl)) { 3492 ret = PTR_ERR(w->pinctrl); 3493 if (ret == -EPROBE_DEFER) 3494 return ERR_PTR(ret); 3495 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n", 3496 w->name, ret); 3497 return NULL; 3498 } 3499 break; 3500 case snd_soc_dapm_clock_supply: 3501 #ifdef CONFIG_CLKDEV_LOOKUP 3502 w->clk = devm_clk_get(dapm->dev, w->name); 3503 if (IS_ERR(w->clk)) { 3504 ret = PTR_ERR(w->clk); 3505 if (ret == -EPROBE_DEFER) 3506 return ERR_PTR(ret); 3507 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n", 3508 w->name, ret); 3509 return NULL; 3510 } 3511 #else 3512 return NULL; 3513 #endif 3514 break; 3515 default: 3516 break; 3517 } 3518 3519 prefix = soc_dapm_prefix(dapm); 3520 if (prefix) 3521 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name); 3522 else 3523 w->name = kstrdup_const(widget->name, GFP_KERNEL); 3524 if (w->name == NULL) { 3525 kfree(w); 3526 return NULL; 3527 } 3528 3529 switch (w->id) { 3530 case snd_soc_dapm_mic: 3531 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3532 w->power_check = dapm_generic_check_power; 3533 break; 3534 case snd_soc_dapm_input: 3535 if (!dapm->card->fully_routed) 3536 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3537 w->power_check = dapm_generic_check_power; 3538 break; 3539 case snd_soc_dapm_spk: 3540 case snd_soc_dapm_hp: 3541 w->is_ep = SND_SOC_DAPM_EP_SINK; 3542 w->power_check = dapm_generic_check_power; 3543 break; 3544 case snd_soc_dapm_output: 3545 if (!dapm->card->fully_routed) 3546 w->is_ep = SND_SOC_DAPM_EP_SINK; 3547 w->power_check = dapm_generic_check_power; 3548 break; 3549 case snd_soc_dapm_vmid: 3550 case snd_soc_dapm_siggen: 3551 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3552 w->power_check = dapm_always_on_check_power; 3553 break; 3554 case snd_soc_dapm_sink: 3555 w->is_ep = SND_SOC_DAPM_EP_SINK; 3556 w->power_check = dapm_always_on_check_power; 3557 break; 3558 3559 case snd_soc_dapm_mux: 3560 case snd_soc_dapm_demux: 3561 case snd_soc_dapm_switch: 3562 case snd_soc_dapm_mixer: 3563 case snd_soc_dapm_mixer_named_ctl: 3564 case snd_soc_dapm_adc: 3565 case snd_soc_dapm_aif_out: 3566 case snd_soc_dapm_dac: 3567 case snd_soc_dapm_aif_in: 3568 case snd_soc_dapm_pga: 3569 case snd_soc_dapm_out_drv: 3570 case snd_soc_dapm_micbias: 3571 case snd_soc_dapm_line: 3572 case snd_soc_dapm_dai_link: 3573 case snd_soc_dapm_dai_out: 3574 case snd_soc_dapm_dai_in: 3575 w->power_check = dapm_generic_check_power; 3576 break; 3577 case snd_soc_dapm_supply: 3578 case snd_soc_dapm_regulator_supply: 3579 case snd_soc_dapm_pinctrl: 3580 case snd_soc_dapm_clock_supply: 3581 case snd_soc_dapm_kcontrol: 3582 w->is_supply = 1; 3583 w->power_check = dapm_supply_check_power; 3584 break; 3585 default: 3586 w->power_check = dapm_always_on_check_power; 3587 break; 3588 } 3589 3590 w->dapm = dapm; 3591 INIT_LIST_HEAD(&w->list); 3592 INIT_LIST_HEAD(&w->dirty); 3593 list_add_tail(&w->list, &dapm->card->widgets); 3594 3595 snd_soc_dapm_for_each_direction(dir) { 3596 INIT_LIST_HEAD(&w->edges[dir]); 3597 w->endpoints[dir] = -1; 3598 } 3599 3600 /* machine layer sets up unconnected pins and insertions */ 3601 w->connected = 1; 3602 return w; 3603 } 3604 3605 /** 3606 * snd_soc_dapm_new_controls - create new dapm controls 3607 * @dapm: DAPM context 3608 * @widget: widget array 3609 * @num: number of widgets 3610 * 3611 * Creates new DAPM controls based upon the templates. 3612 * 3613 * Returns 0 for success else error. 3614 */ 3615 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm, 3616 const struct snd_soc_dapm_widget *widget, 3617 int num) 3618 { 3619 struct snd_soc_dapm_widget *w; 3620 int i; 3621 int ret = 0; 3622 3623 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT); 3624 for (i = 0; i < num; i++) { 3625 w = snd_soc_dapm_new_control_unlocked(dapm, widget); 3626 if (IS_ERR(w)) { 3627 ret = PTR_ERR(w); 3628 /* Do not nag about probe deferrals */ 3629 if (ret == -EPROBE_DEFER) 3630 break; 3631 dev_err(dapm->dev, 3632 "ASoC: Failed to create DAPM control %s (%d)\n", 3633 widget->name, ret); 3634 break; 3635 } 3636 if (!w) { 3637 dev_err(dapm->dev, 3638 "ASoC: Failed to create DAPM control %s\n", 3639 widget->name); 3640 ret = -ENOMEM; 3641 break; 3642 } 3643 widget++; 3644 } 3645 mutex_unlock(&dapm->card->dapm_mutex); 3646 return ret; 3647 } 3648 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls); 3649 3650 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w, 3651 struct snd_kcontrol *kcontrol, int event) 3652 { 3653 struct snd_soc_dapm_path *source_p, *sink_p; 3654 struct snd_soc_dai *source, *sink; 3655 const struct snd_soc_pcm_stream *config = w->params + w->params_select; 3656 struct snd_pcm_substream substream; 3657 struct snd_pcm_hw_params *params = NULL; 3658 struct snd_pcm_runtime *runtime = NULL; 3659 unsigned int fmt; 3660 int ret; 3661 3662 if (WARN_ON(!config) || 3663 WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) || 3664 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN]))) 3665 return -EINVAL; 3666 3667 /* We only support a single source and sink, pick the first */ 3668 source_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_OUT], 3669 struct snd_soc_dapm_path, 3670 list_node[SND_SOC_DAPM_DIR_OUT]); 3671 sink_p = list_first_entry(&w->edges[SND_SOC_DAPM_DIR_IN], 3672 struct snd_soc_dapm_path, 3673 list_node[SND_SOC_DAPM_DIR_IN]); 3674 3675 source = source_p->source->priv; 3676 sink = sink_p->sink->priv; 3677 3678 /* Be a little careful as we don't want to overflow the mask array */ 3679 if (config->formats) { 3680 fmt = ffs(config->formats) - 1; 3681 } else { 3682 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n", 3683 config->formats); 3684 fmt = 0; 3685 } 3686 3687 /* Currently very limited parameter selection */ 3688 params = kzalloc(sizeof(*params), GFP_KERNEL); 3689 if (!params) { 3690 ret = -ENOMEM; 3691 goto out; 3692 } 3693 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt); 3694 3695 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min = 3696 config->rate_min; 3697 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max = 3698 config->rate_max; 3699 3700 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min 3701 = config->channels_min; 3702 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max 3703 = config->channels_max; 3704 3705 memset(&substream, 0, sizeof(substream)); 3706 3707 /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */ 3708 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 3709 if (!runtime) { 3710 ret = -ENOMEM; 3711 goto out; 3712 } 3713 substream.runtime = runtime; 3714 3715 switch (event) { 3716 case SND_SOC_DAPM_PRE_PMU: 3717 substream.stream = SNDRV_PCM_STREAM_CAPTURE; 3718 if (source->driver->ops->startup) { 3719 ret = source->driver->ops->startup(&substream, source); 3720 if (ret < 0) { 3721 dev_err(source->dev, 3722 "ASoC: startup() failed: %d\n", ret); 3723 goto out; 3724 } 3725 source->active++; 3726 } 3727 ret = soc_dai_hw_params(&substream, params, source); 3728 if (ret < 0) 3729 goto out; 3730 3731 substream.stream = SNDRV_PCM_STREAM_PLAYBACK; 3732 if (sink->driver->ops->startup) { 3733 ret = sink->driver->ops->startup(&substream, sink); 3734 if (ret < 0) { 3735 dev_err(sink->dev, 3736 "ASoC: startup() failed: %d\n", ret); 3737 goto out; 3738 } 3739 sink->active++; 3740 } 3741 ret = soc_dai_hw_params(&substream, params, sink); 3742 if (ret < 0) 3743 goto out; 3744 break; 3745 3746 case SND_SOC_DAPM_POST_PMU: 3747 ret = snd_soc_dai_digital_mute(sink, 0, 3748 SNDRV_PCM_STREAM_PLAYBACK); 3749 if (ret != 0 && ret != -ENOTSUPP) 3750 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret); 3751 ret = 0; 3752 break; 3753 3754 case SND_SOC_DAPM_PRE_PMD: 3755 ret = snd_soc_dai_digital_mute(sink, 1, 3756 SNDRV_PCM_STREAM_PLAYBACK); 3757 if (ret != 0 && ret != -ENOTSUPP) 3758 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret); 3759 ret = 0; 3760 3761 source->active--; 3762 if (source->driver->ops->shutdown) { 3763 substream.stream = SNDRV_PCM_STREAM_CAPTURE; 3764 source->driver->ops->shutdown(&substream, source); 3765 } 3766 3767 sink->active--; 3768 if (sink->driver->ops->shutdown) { 3769 substream.stream = SNDRV_PCM_STREAM_PLAYBACK; 3770 sink->driver->ops->shutdown(&substream, sink); 3771 } 3772 break; 3773 3774 default: 3775 WARN(1, "Unknown event %d\n", event); 3776 ret = -EINVAL; 3777 } 3778 3779 out: 3780 kfree(runtime); 3781 kfree(params); 3782 return ret; 3783 } 3784 3785 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol, 3786 struct snd_ctl_elem_value *ucontrol) 3787 { 3788 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol); 3789 3790 ucontrol->value.enumerated.item[0] = w->params_select; 3791 3792 return 0; 3793 } 3794 3795 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol, 3796 struct snd_ctl_elem_value *ucontrol) 3797 { 3798 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol); 3799 3800 /* Can't change the config when widget is already powered */ 3801 if (w->power) 3802 return -EBUSY; 3803 3804 if (ucontrol->value.enumerated.item[0] == w->params_select) 3805 return 0; 3806 3807 if (ucontrol->value.enumerated.item[0] >= w->num_params) 3808 return -EINVAL; 3809 3810 w->params_select = ucontrol->value.enumerated.item[0]; 3811 3812 return 0; 3813 } 3814 3815 static void 3816 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card, 3817 unsigned long *private_value, 3818 int num_params, 3819 const char **w_param_text) 3820 { 3821 int count; 3822 3823 devm_kfree(card->dev, (void *)*private_value); 3824 for (count = 0 ; count < num_params; count++) 3825 devm_kfree(card->dev, (void *)w_param_text[count]); 3826 devm_kfree(card->dev, w_param_text); 3827 } 3828 3829 static struct snd_kcontrol_new * 3830 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card, 3831 char *link_name, 3832 const struct snd_soc_pcm_stream *params, 3833 int num_params, const char **w_param_text, 3834 unsigned long *private_value) 3835 { 3836 struct soc_enum w_param_enum[] = { 3837 SOC_ENUM_SINGLE(0, 0, 0, NULL), 3838 }; 3839 struct snd_kcontrol_new kcontrol_dai_link[] = { 3840 SOC_ENUM_EXT(NULL, w_param_enum[0], 3841 snd_soc_dapm_dai_link_get, 3842 snd_soc_dapm_dai_link_put), 3843 }; 3844 struct snd_kcontrol_new *kcontrol_news; 3845 const struct snd_soc_pcm_stream *config = params; 3846 int count; 3847 3848 for (count = 0 ; count < num_params; count++) { 3849 if (!config->stream_name) { 3850 dev_warn(card->dapm.dev, 3851 "ASoC: anonymous config %d for dai link %s\n", 3852 count, link_name); 3853 w_param_text[count] = 3854 devm_kasprintf(card->dev, GFP_KERNEL, 3855 "Anonymous Configuration %d", 3856 count); 3857 } else { 3858 w_param_text[count] = devm_kmemdup(card->dev, 3859 config->stream_name, 3860 strlen(config->stream_name) + 1, 3861 GFP_KERNEL); 3862 } 3863 if (!w_param_text[count]) 3864 goto outfree_w_param; 3865 config++; 3866 } 3867 3868 w_param_enum[0].items = num_params; 3869 w_param_enum[0].texts = w_param_text; 3870 3871 *private_value = 3872 (unsigned long) devm_kmemdup(card->dev, 3873 (void *)(kcontrol_dai_link[0].private_value), 3874 sizeof(struct soc_enum), GFP_KERNEL); 3875 if (!*private_value) { 3876 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n", 3877 link_name); 3878 goto outfree_w_param; 3879 } 3880 kcontrol_dai_link[0].private_value = *private_value; 3881 /* duplicate kcontrol_dai_link on heap so that memory persists */ 3882 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0], 3883 sizeof(struct snd_kcontrol_new), 3884 GFP_KERNEL); 3885 if (!kcontrol_news) { 3886 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n", 3887 link_name); 3888 goto outfree_w_param; 3889 } 3890 return kcontrol_news; 3891 3892 outfree_w_param: 3893 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text); 3894 return NULL; 3895 } 3896 3897 int snd_soc_dapm_new_pcm(struct snd_soc_card *card, 3898 const struct snd_soc_pcm_stream *params, 3899 unsigned int num_params, 3900 struct snd_soc_dapm_widget *source, 3901 struct snd_soc_dapm_widget *sink) 3902 { 3903 struct snd_soc_dapm_widget template; 3904 struct snd_soc_dapm_widget *w; 3905 const char **w_param_text; 3906 unsigned long private_value; 3907 char *link_name; 3908 int ret; 3909 3910 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s", 3911 source->name, sink->name); 3912 if (!link_name) 3913 return -ENOMEM; 3914 3915 memset(&template, 0, sizeof(template)); 3916 template.reg = SND_SOC_NOPM; 3917 template.id = snd_soc_dapm_dai_link; 3918 template.name = link_name; 3919 template.event = snd_soc_dai_link_event; 3920 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | 3921 SND_SOC_DAPM_PRE_PMD; 3922 template.kcontrol_news = NULL; 3923 3924 /* allocate memory for control, only in case of multiple configs */ 3925 if (num_params > 1) { 3926 w_param_text = devm_kcalloc(card->dev, num_params, 3927 sizeof(char *), GFP_KERNEL); 3928 if (!w_param_text) { 3929 ret = -ENOMEM; 3930 goto param_fail; 3931 } 3932 3933 template.num_kcontrols = 1; 3934 template.kcontrol_news = 3935 snd_soc_dapm_alloc_kcontrol(card, 3936 link_name, params, num_params, 3937 w_param_text, &private_value); 3938 if (!template.kcontrol_news) { 3939 ret = -ENOMEM; 3940 goto param_fail; 3941 } 3942 } else { 3943 w_param_text = NULL; 3944 } 3945 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name); 3946 3947 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template); 3948 if (IS_ERR(w)) { 3949 ret = PTR_ERR(w); 3950 /* Do not nag about probe deferrals */ 3951 if (ret != -EPROBE_DEFER) 3952 dev_err(card->dev, 3953 "ASoC: Failed to create %s widget (%d)\n", 3954 link_name, ret); 3955 goto outfree_kcontrol_news; 3956 } 3957 if (!w) { 3958 dev_err(card->dev, "ASoC: Failed to create %s widget\n", 3959 link_name); 3960 ret = -ENOMEM; 3961 goto outfree_kcontrol_news; 3962 } 3963 3964 w->params = params; 3965 w->num_params = num_params; 3966 3967 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL); 3968 if (ret) 3969 goto outfree_w; 3970 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL); 3971 3972 outfree_w: 3973 devm_kfree(card->dev, w); 3974 outfree_kcontrol_news: 3975 devm_kfree(card->dev, (void *)template.kcontrol_news); 3976 snd_soc_dapm_free_kcontrol(card, &private_value, num_params, w_param_text); 3977 param_fail: 3978 devm_kfree(card->dev, link_name); 3979 return ret; 3980 } 3981 3982 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm, 3983 struct snd_soc_dai *dai) 3984 { 3985 struct snd_soc_dapm_widget template; 3986 struct snd_soc_dapm_widget *w; 3987 3988 WARN_ON(dapm->dev != dai->dev); 3989 3990 memset(&template, 0, sizeof(template)); 3991 template.reg = SND_SOC_NOPM; 3992 3993 if (dai->driver->playback.stream_name) { 3994 template.id = snd_soc_dapm_dai_in; 3995 template.name = dai->driver->playback.stream_name; 3996 template.sname = dai->driver->playback.stream_name; 3997 3998 dev_dbg(dai->dev, "ASoC: adding %s widget\n", 3999 template.name); 4000 4001 w = snd_soc_dapm_new_control_unlocked(dapm, &template); 4002 if (IS_ERR(w)) { 4003 int ret = PTR_ERR(w); 4004 4005 /* Do not nag about probe deferrals */ 4006 if (ret != -EPROBE_DEFER) 4007 dev_err(dapm->dev, 4008 "ASoC: Failed to create %s widget (%d)\n", 4009 dai->driver->playback.stream_name, ret); 4010 return ret; 4011 } 4012 if (!w) { 4013 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n", 4014 dai->driver->playback.stream_name); 4015 return -ENOMEM; 4016 } 4017 4018 w->priv = dai; 4019 dai->playback_widget = w; 4020 } 4021 4022 if (dai->driver->capture.stream_name) { 4023 template.id = snd_soc_dapm_dai_out; 4024 template.name = dai->driver->capture.stream_name; 4025 template.sname = dai->driver->capture.stream_name; 4026 4027 dev_dbg(dai->dev, "ASoC: adding %s widget\n", 4028 template.name); 4029 4030 w = snd_soc_dapm_new_control_unlocked(dapm, &template); 4031 if (IS_ERR(w)) { 4032 int ret = PTR_ERR(w); 4033 4034 /* Do not nag about probe deferrals */ 4035 if (ret != -EPROBE_DEFER) 4036 dev_err(dapm->dev, 4037 "ASoC: Failed to create %s widget (%d)\n", 4038 dai->driver->playback.stream_name, ret); 4039 return ret; 4040 } 4041 if (!w) { 4042 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n", 4043 dai->driver->capture.stream_name); 4044 return -ENOMEM; 4045 } 4046 4047 w->priv = dai; 4048 dai->capture_widget = w; 4049 } 4050 4051 return 0; 4052 } 4053 4054 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card) 4055 { 4056 struct snd_soc_dapm_widget *dai_w, *w; 4057 struct snd_soc_dapm_widget *src, *sink; 4058 struct snd_soc_dai *dai; 4059 4060 /* For each DAI widget... */ 4061 list_for_each_entry(dai_w, &card->widgets, list) { 4062 switch (dai_w->id) { 4063 case snd_soc_dapm_dai_in: 4064 case snd_soc_dapm_dai_out: 4065 break; 4066 default: 4067 continue; 4068 } 4069 4070 /* let users know there is no DAI to link */ 4071 if (!dai_w->priv) { 4072 dev_dbg(card->dev, "dai widget %s has no DAI\n", 4073 dai_w->name); 4074 continue; 4075 } 4076 4077 dai = dai_w->priv; 4078 4079 /* ...find all widgets with the same stream and link them */ 4080 list_for_each_entry(w, &card->widgets, list) { 4081 if (w->dapm != dai_w->dapm) 4082 continue; 4083 4084 switch (w->id) { 4085 case snd_soc_dapm_dai_in: 4086 case snd_soc_dapm_dai_out: 4087 continue; 4088 default: 4089 break; 4090 } 4091 4092 if (!w->sname || !strstr(w->sname, dai_w->sname)) 4093 continue; 4094 4095 if (dai_w->id == snd_soc_dapm_dai_in) { 4096 src = dai_w; 4097 sink = w; 4098 } else { 4099 src = w; 4100 sink = dai_w; 4101 } 4102 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name); 4103 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL); 4104 } 4105 } 4106 4107 return 0; 4108 } 4109 4110 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card, 4111 struct snd_soc_pcm_runtime *rtd) 4112 { 4113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 4114 struct snd_soc_dapm_widget *sink, *source; 4115 int i; 4116 4117 for (i = 0; i < rtd->num_codecs; i++) { 4118 struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; 4119 4120 /* connect BE DAI playback if widgets are valid */ 4121 if (codec_dai->playback_widget && cpu_dai->playback_widget) { 4122 source = cpu_dai->playback_widget; 4123 sink = codec_dai->playback_widget; 4124 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n", 4125 cpu_dai->component->name, source->name, 4126 codec_dai->component->name, sink->name); 4127 4128 snd_soc_dapm_add_path(&card->dapm, source, sink, 4129 NULL, NULL); 4130 } 4131 4132 /* connect BE DAI capture if widgets are valid */ 4133 if (codec_dai->capture_widget && cpu_dai->capture_widget) { 4134 source = codec_dai->capture_widget; 4135 sink = cpu_dai->capture_widget; 4136 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n", 4137 codec_dai->component->name, source->name, 4138 cpu_dai->component->name, sink->name); 4139 4140 snd_soc_dapm_add_path(&card->dapm, source, sink, 4141 NULL, NULL); 4142 } 4143 } 4144 } 4145 4146 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream, 4147 int event) 4148 { 4149 struct snd_soc_dapm_widget *w; 4150 unsigned int ep; 4151 4152 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 4153 w = dai->playback_widget; 4154 else 4155 w = dai->capture_widget; 4156 4157 if (w) { 4158 dapm_mark_dirty(w, "stream event"); 4159 4160 if (w->id == snd_soc_dapm_dai_in) { 4161 ep = SND_SOC_DAPM_EP_SOURCE; 4162 dapm_widget_invalidate_input_paths(w); 4163 } else { 4164 ep = SND_SOC_DAPM_EP_SINK; 4165 dapm_widget_invalidate_output_paths(w); 4166 } 4167 4168 switch (event) { 4169 case SND_SOC_DAPM_STREAM_START: 4170 w->active = 1; 4171 w->is_ep = ep; 4172 break; 4173 case SND_SOC_DAPM_STREAM_STOP: 4174 w->active = 0; 4175 w->is_ep = 0; 4176 break; 4177 case SND_SOC_DAPM_STREAM_SUSPEND: 4178 case SND_SOC_DAPM_STREAM_RESUME: 4179 case SND_SOC_DAPM_STREAM_PAUSE_PUSH: 4180 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE: 4181 break; 4182 } 4183 } 4184 } 4185 4186 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card) 4187 { 4188 struct snd_soc_pcm_runtime *rtd; 4189 4190 /* for each BE DAI link... */ 4191 list_for_each_entry(rtd, &card->rtd_list, list) { 4192 /* 4193 * dynamic FE links have no fixed DAI mapping. 4194 * CODEC<->CODEC links have no direct connection. 4195 */ 4196 if (rtd->dai_link->dynamic || rtd->dai_link->params) 4197 continue; 4198 4199 dapm_connect_dai_link_widgets(card, rtd); 4200 } 4201 } 4202 4203 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, 4204 int event) 4205 { 4206 int i; 4207 4208 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event); 4209 for (i = 0; i < rtd->num_codecs; i++) 4210 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event); 4211 4212 dapm_power_widgets(rtd->card, event); 4213 } 4214 4215 /** 4216 * snd_soc_dapm_stream_event - send a stream event to the dapm core 4217 * @rtd: PCM runtime data 4218 * @stream: stream name 4219 * @event: stream event 4220 * 4221 * Sends a stream event to the dapm core. The core then makes any 4222 * necessary widget power changes. 4223 * 4224 * Returns 0 for success else error. 4225 */ 4226 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, 4227 int event) 4228 { 4229 struct snd_soc_card *card = rtd->card; 4230 4231 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 4232 soc_dapm_stream_event(rtd, stream, event); 4233 mutex_unlock(&card->dapm_mutex); 4234 } 4235 4236 /** 4237 * snd_soc_dapm_enable_pin_unlocked - enable pin. 4238 * @dapm: DAPM context 4239 * @pin: pin name 4240 * 4241 * Enables input/output pin and its parents or children widgets iff there is 4242 * a valid audio route and active audio stream. 4243 * 4244 * Requires external locking. 4245 * 4246 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4247 * do any widget power switching. 4248 */ 4249 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4250 const char *pin) 4251 { 4252 return snd_soc_dapm_set_pin(dapm, pin, 1); 4253 } 4254 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked); 4255 4256 /** 4257 * snd_soc_dapm_enable_pin - enable pin. 4258 * @dapm: DAPM context 4259 * @pin: pin name 4260 * 4261 * Enables input/output pin and its parents or children widgets iff there is 4262 * a valid audio route and active audio stream. 4263 * 4264 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4265 * do any widget power switching. 4266 */ 4267 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin) 4268 { 4269 int ret; 4270 4271 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 4272 4273 ret = snd_soc_dapm_set_pin(dapm, pin, 1); 4274 4275 mutex_unlock(&dapm->card->dapm_mutex); 4276 4277 return ret; 4278 } 4279 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin); 4280 4281 /** 4282 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled 4283 * @dapm: DAPM context 4284 * @pin: pin name 4285 * 4286 * Enables input/output pin regardless of any other state. This is 4287 * intended for use with microphone bias supplies used in microphone 4288 * jack detection. 4289 * 4290 * Requires external locking. 4291 * 4292 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4293 * do any widget power switching. 4294 */ 4295 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4296 const char *pin) 4297 { 4298 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 4299 4300 if (!w) { 4301 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin); 4302 return -EINVAL; 4303 } 4304 4305 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin); 4306 if (!w->connected) { 4307 /* 4308 * w->force does not affect the number of input or output paths, 4309 * so we only have to recheck if w->connected is changed 4310 */ 4311 dapm_widget_invalidate_input_paths(w); 4312 dapm_widget_invalidate_output_paths(w); 4313 w->connected = 1; 4314 } 4315 w->force = 1; 4316 dapm_mark_dirty(w, "force enable"); 4317 4318 return 0; 4319 } 4320 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked); 4321 4322 /** 4323 * snd_soc_dapm_force_enable_pin - force a pin to be enabled 4324 * @dapm: DAPM context 4325 * @pin: pin name 4326 * 4327 * Enables input/output pin regardless of any other state. This is 4328 * intended for use with microphone bias supplies used in microphone 4329 * jack detection. 4330 * 4331 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4332 * do any widget power switching. 4333 */ 4334 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm, 4335 const char *pin) 4336 { 4337 int ret; 4338 4339 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 4340 4341 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin); 4342 4343 mutex_unlock(&dapm->card->dapm_mutex); 4344 4345 return ret; 4346 } 4347 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin); 4348 4349 /** 4350 * snd_soc_dapm_disable_pin_unlocked - disable pin. 4351 * @dapm: DAPM context 4352 * @pin: pin name 4353 * 4354 * Disables input/output pin and its parents or children widgets. 4355 * 4356 * Requires external locking. 4357 * 4358 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4359 * do any widget power switching. 4360 */ 4361 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4362 const char *pin) 4363 { 4364 return snd_soc_dapm_set_pin(dapm, pin, 0); 4365 } 4366 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked); 4367 4368 /** 4369 * snd_soc_dapm_disable_pin - disable pin. 4370 * @dapm: DAPM context 4371 * @pin: pin name 4372 * 4373 * Disables input/output pin and its parents or children widgets. 4374 * 4375 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4376 * do any widget power switching. 4377 */ 4378 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm, 4379 const char *pin) 4380 { 4381 int ret; 4382 4383 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 4384 4385 ret = snd_soc_dapm_set_pin(dapm, pin, 0); 4386 4387 mutex_unlock(&dapm->card->dapm_mutex); 4388 4389 return ret; 4390 } 4391 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin); 4392 4393 /** 4394 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin. 4395 * @dapm: DAPM context 4396 * @pin: pin name 4397 * 4398 * Marks the specified pin as being not connected, disabling it along 4399 * any parent or child widgets. At present this is identical to 4400 * snd_soc_dapm_disable_pin() but in future it will be extended to do 4401 * additional things such as disabling controls which only affect 4402 * paths through the pin. 4403 * 4404 * Requires external locking. 4405 * 4406 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4407 * do any widget power switching. 4408 */ 4409 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm, 4410 const char *pin) 4411 { 4412 return snd_soc_dapm_set_pin(dapm, pin, 0); 4413 } 4414 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked); 4415 4416 /** 4417 * snd_soc_dapm_nc_pin - permanently disable pin. 4418 * @dapm: DAPM context 4419 * @pin: pin name 4420 * 4421 * Marks the specified pin as being not connected, disabling it along 4422 * any parent or child widgets. At present this is identical to 4423 * snd_soc_dapm_disable_pin() but in future it will be extended to do 4424 * additional things such as disabling controls which only affect 4425 * paths through the pin. 4426 * 4427 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4428 * do any widget power switching. 4429 */ 4430 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin) 4431 { 4432 int ret; 4433 4434 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME); 4435 4436 ret = snd_soc_dapm_set_pin(dapm, pin, 0); 4437 4438 mutex_unlock(&dapm->card->dapm_mutex); 4439 4440 return ret; 4441 } 4442 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin); 4443 4444 /** 4445 * snd_soc_dapm_get_pin_status - get audio pin status 4446 * @dapm: DAPM context 4447 * @pin: audio signal pin endpoint (or start point) 4448 * 4449 * Get audio pin status - connected or disconnected. 4450 * 4451 * Returns 1 for connected otherwise 0. 4452 */ 4453 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm, 4454 const char *pin) 4455 { 4456 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 4457 4458 if (w) 4459 return w->connected; 4460 4461 return 0; 4462 } 4463 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status); 4464 4465 /** 4466 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint 4467 * @dapm: DAPM context 4468 * @pin: audio signal pin endpoint (or start point) 4469 * 4470 * Mark the given endpoint or pin as ignoring suspend. When the 4471 * system is disabled a path between two endpoints flagged as ignoring 4472 * suspend will not be disabled. The path must already be enabled via 4473 * normal means at suspend time, it will not be turned on if it was not 4474 * already enabled. 4475 */ 4476 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm, 4477 const char *pin) 4478 { 4479 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false); 4480 4481 if (!w) { 4482 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin); 4483 return -EINVAL; 4484 } 4485 4486 w->ignore_suspend = 1; 4487 4488 return 0; 4489 } 4490 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend); 4491 4492 /** 4493 * snd_soc_dapm_free - free dapm resources 4494 * @dapm: DAPM context 4495 * 4496 * Free all dapm widgets and resources. 4497 */ 4498 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm) 4499 { 4500 dapm_debugfs_cleanup(dapm); 4501 dapm_free_widgets(dapm); 4502 list_del(&dapm->list); 4503 } 4504 EXPORT_SYMBOL_GPL(snd_soc_dapm_free); 4505 4506 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm) 4507 { 4508 struct snd_soc_card *card = dapm->card; 4509 struct snd_soc_dapm_widget *w; 4510 LIST_HEAD(down_list); 4511 int powerdown = 0; 4512 4513 mutex_lock(&card->dapm_mutex); 4514 4515 list_for_each_entry(w, &dapm->card->widgets, list) { 4516 if (w->dapm != dapm) 4517 continue; 4518 if (w->power) { 4519 dapm_seq_insert(w, &down_list, false); 4520 w->power = 0; 4521 powerdown = 1; 4522 } 4523 } 4524 4525 /* If there were no widgets to power down we're already in 4526 * standby. 4527 */ 4528 if (powerdown) { 4529 if (dapm->bias_level == SND_SOC_BIAS_ON) 4530 snd_soc_dapm_set_bias_level(dapm, 4531 SND_SOC_BIAS_PREPARE); 4532 dapm_seq_run(card, &down_list, 0, false); 4533 if (dapm->bias_level == SND_SOC_BIAS_PREPARE) 4534 snd_soc_dapm_set_bias_level(dapm, 4535 SND_SOC_BIAS_STANDBY); 4536 } 4537 4538 mutex_unlock(&card->dapm_mutex); 4539 } 4540 4541 /* 4542 * snd_soc_dapm_shutdown - callback for system shutdown 4543 */ 4544 void snd_soc_dapm_shutdown(struct snd_soc_card *card) 4545 { 4546 struct snd_soc_dapm_context *dapm; 4547 4548 list_for_each_entry(dapm, &card->dapm_list, list) { 4549 if (dapm != &card->dapm) { 4550 soc_dapm_shutdown_dapm(dapm); 4551 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) 4552 snd_soc_dapm_set_bias_level(dapm, 4553 SND_SOC_BIAS_OFF); 4554 } 4555 } 4556 4557 soc_dapm_shutdown_dapm(&card->dapm); 4558 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) 4559 snd_soc_dapm_set_bias_level(&card->dapm, 4560 SND_SOC_BIAS_OFF); 4561 } 4562 4563 /* Module information */ 4564 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 4565 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC"); 4566 MODULE_LICENSE("GPL"); 4567