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