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