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/cleanup.h> 24 #include <linux/delay.h> 25 #include <linux/pm.h> 26 #include <linux/bitops.h> 27 #include <linux/platform_device.h> 28 #include <linux/jiffies.h> 29 #include <linux/debugfs.h> 30 #include <linux/pm_runtime.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/pinctrl/consumer.h> 33 #include <linux/clk.h> 34 #include <linux/slab.h> 35 #include <sound/core.h> 36 #include <sound/pcm.h> 37 #include <sound/pcm_params.h> 38 #include <sound/soc.h> 39 #include <sound/initval.h> 40 41 #include <trace/events/asoc.h> 42 43 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++; 44 45 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \ 46 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN) 47 48 #define snd_soc_dapm_for_each_direction(dir) \ 49 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \ 50 (dir)++) 51 52 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm, 53 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink, 54 const char *control, 55 int (*connected)(struct snd_soc_dapm_widget *source, 56 struct snd_soc_dapm_widget *sink)); 57 58 struct snd_soc_dapm_widget * 59 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, 60 const struct snd_soc_dapm_widget *widget); 61 62 struct snd_soc_dapm_widget * 63 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, 64 const struct snd_soc_dapm_widget *widget); 65 66 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg); 67 68 /* dapm power sequences - make this per codec in the future */ 69 static int dapm_up_seq[] = { 70 [snd_soc_dapm_pre] = 1, 71 [snd_soc_dapm_regulator_supply] = 2, 72 [snd_soc_dapm_pinctrl] = 2, 73 [snd_soc_dapm_clock_supply] = 2, 74 [snd_soc_dapm_supply] = 3, 75 [snd_soc_dapm_dai_link] = 3, 76 [snd_soc_dapm_micbias] = 4, 77 [snd_soc_dapm_vmid] = 4, 78 [snd_soc_dapm_dai_in] = 5, 79 [snd_soc_dapm_dai_out] = 5, 80 [snd_soc_dapm_aif_in] = 5, 81 [snd_soc_dapm_aif_out] = 5, 82 [snd_soc_dapm_mic] = 6, 83 [snd_soc_dapm_siggen] = 6, 84 [snd_soc_dapm_input] = 6, 85 [snd_soc_dapm_output] = 6, 86 [snd_soc_dapm_mux] = 7, 87 [snd_soc_dapm_demux] = 7, 88 [snd_soc_dapm_dac] = 8, 89 [snd_soc_dapm_switch] = 9, 90 [snd_soc_dapm_mixer] = 9, 91 [snd_soc_dapm_mixer_named_ctl] = 9, 92 [snd_soc_dapm_pga] = 10, 93 [snd_soc_dapm_buffer] = 10, 94 [snd_soc_dapm_scheduler] = 10, 95 [snd_soc_dapm_effect] = 10, 96 [snd_soc_dapm_src] = 10, 97 [snd_soc_dapm_asrc] = 10, 98 [snd_soc_dapm_encoder] = 10, 99 [snd_soc_dapm_decoder] = 10, 100 [snd_soc_dapm_adc] = 11, 101 [snd_soc_dapm_out_drv] = 12, 102 [snd_soc_dapm_hp] = 12, 103 [snd_soc_dapm_line] = 12, 104 [snd_soc_dapm_sink] = 12, 105 [snd_soc_dapm_spk] = 13, 106 [snd_soc_dapm_kcontrol] = 14, 107 [snd_soc_dapm_post] = 15, 108 }; 109 110 static int dapm_down_seq[] = { 111 [snd_soc_dapm_pre] = 1, 112 [snd_soc_dapm_kcontrol] = 2, 113 [snd_soc_dapm_adc] = 3, 114 [snd_soc_dapm_spk] = 4, 115 [snd_soc_dapm_hp] = 5, 116 [snd_soc_dapm_line] = 5, 117 [snd_soc_dapm_out_drv] = 5, 118 [snd_soc_dapm_sink] = 6, 119 [snd_soc_dapm_pga] = 6, 120 [snd_soc_dapm_buffer] = 6, 121 [snd_soc_dapm_scheduler] = 6, 122 [snd_soc_dapm_effect] = 6, 123 [snd_soc_dapm_src] = 6, 124 [snd_soc_dapm_asrc] = 6, 125 [snd_soc_dapm_encoder] = 6, 126 [snd_soc_dapm_decoder] = 6, 127 [snd_soc_dapm_switch] = 7, 128 [snd_soc_dapm_mixer_named_ctl] = 7, 129 [snd_soc_dapm_mixer] = 7, 130 [snd_soc_dapm_dac] = 8, 131 [snd_soc_dapm_mic] = 9, 132 [snd_soc_dapm_siggen] = 9, 133 [snd_soc_dapm_input] = 9, 134 [snd_soc_dapm_output] = 9, 135 [snd_soc_dapm_micbias] = 10, 136 [snd_soc_dapm_vmid] = 10, 137 [snd_soc_dapm_mux] = 11, 138 [snd_soc_dapm_demux] = 11, 139 [snd_soc_dapm_aif_in] = 12, 140 [snd_soc_dapm_aif_out] = 12, 141 [snd_soc_dapm_dai_in] = 12, 142 [snd_soc_dapm_dai_out] = 12, 143 [snd_soc_dapm_dai_link] = 13, 144 [snd_soc_dapm_supply] = 14, 145 [snd_soc_dapm_clock_supply] = 15, 146 [snd_soc_dapm_pinctrl] = 15, 147 [snd_soc_dapm_regulator_supply] = 15, 148 [snd_soc_dapm_post] = 16, 149 }; 150 151 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm) 152 { 153 if (snd_soc_card_is_instantiated(dapm->card)) 154 snd_soc_dapm_mutex_assert_held(dapm); 155 } 156 157 static void pop_wait(u32 pop_time) 158 { 159 if (pop_time) 160 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time)); 161 } 162 163 __printf(3, 4) 164 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...) 165 { 166 va_list args; 167 char *buf; 168 169 if (!pop_time) 170 return; 171 172 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 173 if (buf == NULL) 174 return; 175 176 va_start(args, fmt); 177 vsnprintf(buf, PAGE_SIZE, fmt, args); 178 dev_info(dev, "%s", buf); 179 va_end(args); 180 181 kfree(buf); 182 } 183 184 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w) 185 { 186 return !list_empty(&w->dirty); 187 } 188 189 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason) 190 { 191 dapm_assert_locked(w->dapm); 192 193 if (!dapm_dirty_widget(w)) { 194 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n", 195 w->name, reason); 196 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty); 197 } 198 } 199 200 /* 201 * Common implementation for dapm_widget_invalidate_input_paths() and 202 * dapm_widget_invalidate_output_paths(). The function is inlined since the 203 * combined size of the two specialized functions is only marginally larger then 204 * the size of the generic function and at the same time the fast path of the 205 * specialized functions is significantly smaller than the generic function. 206 */ 207 static __always_inline void dapm_widget_invalidate_paths( 208 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir) 209 { 210 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir); 211 struct snd_soc_dapm_widget *node; 212 struct snd_soc_dapm_path *p; 213 LIST_HEAD(list); 214 215 dapm_assert_locked(w->dapm); 216 217 if (w->endpoints[dir] == -1) 218 return; 219 220 list_add_tail(&w->work_list, &list); 221 w->endpoints[dir] = -1; 222 223 list_for_each_entry(w, &list, work_list) { 224 snd_soc_dapm_widget_for_each_path(w, dir, p) { 225 if (p->is_supply || p->weak || !p->connect) 226 continue; 227 node = p->node[rdir]; 228 if (node->endpoints[dir] != -1) { 229 node->endpoints[dir] = -1; 230 list_add_tail(&node->work_list, &list); 231 } 232 } 233 } 234 } 235 236 /* 237 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of 238 * input paths 239 * @w: The widget for which to invalidate the cached number of input paths 240 * 241 * Resets the cached number of inputs for the specified widget and all widgets 242 * that can be reached via outcoming paths from the widget. 243 * 244 * This function must be called if the number of output paths for a widget might 245 * have changed. E.g. if the source state of a widget changes or a path is added 246 * or activated with the widget as the sink. 247 */ 248 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w) 249 { 250 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN); 251 } 252 253 /* 254 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of 255 * output paths 256 * @w: The widget for which to invalidate the cached number of output paths 257 * 258 * Resets the cached number of outputs for the specified widget and all widgets 259 * that can be reached via incoming paths from the widget. 260 * 261 * This function must be called if the number of output paths for a widget might 262 * have changed. E.g. if the sink state of a widget changes or a path is added 263 * or activated with the widget as the source. 264 */ 265 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w) 266 { 267 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT); 268 } 269 270 /* 271 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs 272 * for the widgets connected to a path 273 * @p: The path to invalidate 274 * 275 * Resets the cached number of inputs for the sink of the path and the cached 276 * number of outputs for the source of the path. 277 * 278 * This function must be called when a path is added, removed or the connected 279 * state changes. 280 */ 281 static void dapm_path_invalidate(struct snd_soc_dapm_path *p) 282 { 283 /* 284 * Weak paths or supply paths do not influence the number of input or 285 * output paths of their neighbors. 286 */ 287 if (p->weak || p->is_supply) 288 return; 289 290 /* 291 * The number of connected endpoints is the sum of the number of 292 * connected endpoints of all neighbors. If a node with 0 connected 293 * endpoints is either connected or disconnected that sum won't change, 294 * so there is no need to re-check the path. 295 */ 296 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0) 297 dapm_widget_invalidate_input_paths(p->sink); 298 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0) 299 dapm_widget_invalidate_output_paths(p->source); 300 } 301 302 void dapm_mark_endpoints_dirty(struct snd_soc_card *card) 303 { 304 struct snd_soc_dapm_widget *w; 305 306 snd_soc_dapm_mutex_lock_root(card); 307 308 for_each_card_widgets(card, w) { 309 if (w->is_ep) { 310 dapm_mark_dirty(w, "Rechecking endpoints"); 311 if (w->is_ep & SND_SOC_DAPM_EP_SINK) 312 dapm_widget_invalidate_output_paths(w); 313 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE) 314 dapm_widget_invalidate_input_paths(w); 315 } 316 } 317 318 snd_soc_dapm_mutex_unlock(card); 319 } 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 __free(kfree) = kmemdup(_widget, 327 sizeof(*_widget), 328 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 return NULL; 338 339 if (_widget->sname) { 340 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL); 341 if (!w->sname) { 342 kfree_const(w->name); 343 return NULL; 344 } 345 } 346 347 return_ptr(w); 348 } 349 350 struct dapm_kcontrol_data { 351 unsigned int value; 352 struct snd_soc_dapm_widget *widget; 353 struct list_head paths; 354 struct snd_soc_dapm_widget_list *wlist; 355 }; 356 357 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget, 358 struct snd_kcontrol *kcontrol, const char *ctrl_name) 359 { 360 struct dapm_kcontrol_data *data; 361 struct soc_mixer_control *mc; 362 struct soc_enum *e; 363 const char *name; 364 int ret; 365 366 data = kzalloc(sizeof(*data), GFP_KERNEL); 367 if (!data) 368 return -ENOMEM; 369 370 INIT_LIST_HEAD(&data->paths); 371 372 switch (widget->id) { 373 case snd_soc_dapm_switch: 374 case snd_soc_dapm_mixer: 375 case snd_soc_dapm_mixer_named_ctl: 376 mc = (struct soc_mixer_control *)kcontrol->private_value; 377 378 if (mc->autodisable) { 379 struct snd_soc_dapm_widget template; 380 381 if (snd_soc_volsw_is_stereo(mc)) 382 dev_warn(widget->dapm->dev, 383 "ASoC: Unsupported stereo autodisable control '%s'\n", 384 ctrl_name); 385 386 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name, 387 "Autodisable"); 388 if (!name) { 389 ret = -ENOMEM; 390 goto err_data; 391 } 392 393 memset(&template, 0, sizeof(template)); 394 template.reg = mc->reg; 395 template.mask = (1 << fls(mc->max)) - 1; 396 template.shift = mc->shift; 397 if (mc->invert) 398 template.off_val = mc->max; 399 else 400 template.off_val = 0; 401 template.on_val = template.off_val; 402 template.id = snd_soc_dapm_kcontrol; 403 template.name = name; 404 405 data->value = template.on_val; 406 407 data->widget = 408 snd_soc_dapm_new_control_unlocked(widget->dapm, 409 &template); 410 kfree(name); 411 if (IS_ERR(data->widget)) { 412 ret = PTR_ERR(data->widget); 413 goto err_data; 414 } 415 } 416 break; 417 case snd_soc_dapm_demux: 418 case snd_soc_dapm_mux: 419 e = (struct soc_enum *)kcontrol->private_value; 420 421 if (e->autodisable) { 422 struct snd_soc_dapm_widget template; 423 424 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name, 425 "Autodisable"); 426 if (!name) { 427 ret = -ENOMEM; 428 goto err_data; 429 } 430 431 memset(&template, 0, sizeof(template)); 432 template.reg = e->reg; 433 template.mask = e->mask; 434 template.shift = e->shift_l; 435 template.off_val = snd_soc_enum_item_to_val(e, 0); 436 template.on_val = template.off_val; 437 template.id = snd_soc_dapm_kcontrol; 438 template.name = name; 439 440 data->value = template.on_val; 441 442 data->widget = snd_soc_dapm_new_control_unlocked( 443 widget->dapm, &template); 444 kfree(name); 445 if (IS_ERR(data->widget)) { 446 ret = PTR_ERR(data->widget); 447 goto err_data; 448 } 449 450 snd_soc_dapm_add_path(widget->dapm, data->widget, 451 widget, NULL, NULL); 452 } else if (e->reg != SND_SOC_NOPM) { 453 data->value = soc_dapm_read(widget->dapm, e->reg) & 454 (e->mask << e->shift_l); 455 } 456 break; 457 default: 458 break; 459 } 460 461 kcontrol->private_data = data; 462 463 return 0; 464 465 err_data: 466 kfree(data); 467 return ret; 468 } 469 470 static void dapm_kcontrol_free(struct snd_kcontrol *kctl) 471 { 472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl); 473 474 list_del(&data->paths); 475 kfree(data->wlist); 476 kfree(data); 477 } 478 479 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist( 480 const struct snd_kcontrol *kcontrol) 481 { 482 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 483 484 return data->wlist; 485 } 486 487 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol, 488 struct snd_soc_dapm_widget *widget) 489 { 490 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 491 struct snd_soc_dapm_widget_list *new_wlist; 492 unsigned int n; 493 494 if (data->wlist) 495 n = data->wlist->num_widgets + 1; 496 else 497 n = 1; 498 499 new_wlist = krealloc(data->wlist, 500 struct_size(new_wlist, widgets, n), 501 GFP_KERNEL); 502 if (!new_wlist) 503 return -ENOMEM; 504 505 new_wlist->num_widgets = n; 506 new_wlist->widgets[n - 1] = widget; 507 508 data->wlist = new_wlist; 509 510 return 0; 511 } 512 513 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol, 514 struct snd_soc_dapm_path *path) 515 { 516 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 517 518 list_add_tail(&path->list_kcontrol, &data->paths); 519 } 520 521 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol) 522 { 523 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 524 525 if (!data->widget) 526 return true; 527 528 return data->widget->power; 529 } 530 531 static struct list_head *dapm_kcontrol_get_path_list( 532 const struct snd_kcontrol *kcontrol) 533 { 534 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 535 536 return &data->paths; 537 } 538 539 #define dapm_kcontrol_for_each_path(path, kcontrol) \ 540 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \ 541 list_kcontrol) 542 543 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol) 544 { 545 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 546 547 return data->value; 548 } 549 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value); 550 551 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol, 552 unsigned int value) 553 { 554 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol); 555 556 if (data->value == value) 557 return false; 558 559 if (data->widget) { 560 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) { 561 case snd_soc_dapm_switch: 562 case snd_soc_dapm_mixer: 563 case snd_soc_dapm_mixer_named_ctl: 564 data->widget->on_val = value & data->widget->mask; 565 break; 566 case snd_soc_dapm_demux: 567 case snd_soc_dapm_mux: 568 data->widget->on_val = value >> data->widget->shift; 569 break; 570 default: 571 data->widget->on_val = value; 572 break; 573 } 574 } 575 576 data->value = value; 577 578 return true; 579 } 580 581 /** 582 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a 583 * kcontrol 584 * @kcontrol: The kcontrol 585 */ 586 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget( 587 struct snd_kcontrol *kcontrol) 588 { 589 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]; 590 } 591 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget); 592 593 /** 594 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a 595 * kcontrol 596 * @kcontrol: The kcontrol 597 * 598 * Note: This function must only be used on kcontrols that are known to have 599 * been registered for a CODEC. Otherwise the behaviour is undefined. 600 */ 601 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm( 602 struct snd_kcontrol *kcontrol) 603 { 604 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm; 605 } 606 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm); 607 608 static void dapm_reset(struct snd_soc_card *card) 609 { 610 struct snd_soc_dapm_widget *w; 611 612 snd_soc_dapm_mutex_assert_held(card); 613 614 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats)); 615 616 for_each_card_widgets(card, w) { 617 w->new_power = w->power; 618 w->power_checked = false; 619 } 620 } 621 622 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm) 623 { 624 if (!dapm->component) 625 return NULL; 626 return dapm->component->name_prefix; 627 } 628 629 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg) 630 { 631 if (!dapm->component) 632 return -EIO; 633 return snd_soc_component_read(dapm->component, reg); 634 } 635 636 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm, 637 int reg, unsigned int mask, unsigned int value) 638 { 639 if (!dapm->component) 640 return -EIO; 641 return snd_soc_component_update_bits(dapm->component, reg, 642 mask, value); 643 } 644 645 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm, 646 int reg, unsigned int mask, unsigned int value) 647 { 648 if (!dapm->component) 649 return -EIO; 650 return snd_soc_component_test_bits(dapm->component, reg, mask, value); 651 } 652 653 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm) 654 { 655 if (dapm->component) 656 snd_soc_component_async_complete(dapm->component); 657 } 658 659 static struct snd_soc_dapm_widget * 660 dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name) 661 { 662 if (w) { 663 struct list_head *wlist = &w->dapm->card->widgets; 664 const int depth = 2; 665 int i = 0; 666 667 list_for_each_entry_from(w, wlist, list) { 668 if (!strcmp(name, w->name)) 669 return w; 670 671 if (++i == depth) 672 break; 673 } 674 } 675 676 return NULL; 677 } 678 679 /** 680 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level 681 * @dapm: The DAPM context for which to set the level 682 * @level: The level to set 683 * 684 * Forces the DAPM bias level to a specific state. It will call the bias level 685 * callback of DAPM context with the specified level. This will even happen if 686 * the context is already at the same level. Furthermore it will not go through 687 * the normal bias level sequencing, meaning any intermediate states between the 688 * current and the target state will not be entered. 689 * 690 * Note that the change in bias level is only temporary and the next time 691 * snd_soc_dapm_sync() is called the state will be set to the level as 692 * determined by the DAPM core. The function is mainly intended to be used to 693 * used during probe or resume from suspend to power up the device so 694 * initialization can be done, before the DAPM core takes over. 695 */ 696 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm, 697 enum snd_soc_bias_level level) 698 { 699 int ret = 0; 700 701 if (dapm->component) 702 ret = snd_soc_component_set_bias_level(dapm->component, level); 703 704 if (ret == 0) 705 dapm->bias_level = level; 706 707 return ret; 708 } 709 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level); 710 711 /** 712 * snd_soc_dapm_set_bias_level - set the bias level for the system 713 * @dapm: DAPM context 714 * @level: level to configure 715 * 716 * Configure the bias (power) levels for the SoC audio device. 717 * 718 * Returns 0 for success else error. 719 */ 720 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm, 721 enum snd_soc_bias_level level) 722 { 723 struct snd_soc_card *card = dapm->card; 724 int ret = 0; 725 726 trace_snd_soc_bias_level_start(dapm, level); 727 728 ret = snd_soc_card_set_bias_level(card, dapm, level); 729 if (ret != 0) 730 goto out; 731 732 if (dapm != &card->dapm) 733 ret = snd_soc_dapm_force_bias_level(dapm, level); 734 735 if (ret != 0) 736 goto out; 737 738 ret = snd_soc_card_set_bias_level_post(card, dapm, level); 739 out: 740 trace_snd_soc_bias_level_done(dapm, level); 741 742 return ret; 743 } 744 745 /* connect mux widget to its interconnecting audio paths */ 746 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm, 747 struct snd_soc_dapm_path *path, const char *control_name, 748 struct snd_soc_dapm_widget *w) 749 { 750 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0]; 751 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 752 unsigned int item; 753 int i; 754 755 if (e->reg != SND_SOC_NOPM) { 756 unsigned int val; 757 val = soc_dapm_read(dapm, e->reg); 758 val = (val >> e->shift_l) & e->mask; 759 item = snd_soc_enum_val_to_item(e, val); 760 } else { 761 /* since a virtual mux has no backing registers to 762 * decide which path to connect, it will try to match 763 * with the first enumeration. This is to ensure 764 * that the default mux choice (the first) will be 765 * correctly powered up during initialization. 766 */ 767 item = 0; 768 } 769 770 i = match_string(e->texts, e->items, control_name); 771 if (i < 0) 772 return -ENODEV; 773 774 path->name = e->texts[i]; 775 path->connect = (i == item); 776 return 0; 777 778 } 779 780 /* set up initial codec paths */ 781 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i, 782 int nth_path) 783 { 784 struct soc_mixer_control *mc = (struct soc_mixer_control *) 785 p->sink->kcontrol_news[i].private_value; 786 unsigned int reg = mc->reg; 787 unsigned int invert = mc->invert; 788 789 if (reg != SND_SOC_NOPM) { 790 unsigned int shift = mc->shift; 791 unsigned int max = mc->max; 792 unsigned int mask = (1 << fls(max)) - 1; 793 unsigned int val = soc_dapm_read(p->sink->dapm, reg); 794 795 /* 796 * The nth_path argument allows this function to know 797 * which path of a kcontrol it is setting the initial 798 * status for. Ideally this would support any number 799 * of paths and channels. But since kcontrols only come 800 * in mono and stereo variants, we are limited to 2 801 * channels. 802 * 803 * The following code assumes for stereo controls the 804 * first path is the left channel, and all remaining 805 * paths are the right channel. 806 */ 807 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) { 808 if (reg != mc->rreg) 809 val = soc_dapm_read(p->sink->dapm, mc->rreg); 810 val = (val >> mc->rshift) & mask; 811 } else { 812 val = (val >> shift) & mask; 813 } 814 if (invert) 815 val = max - val; 816 p->connect = !!val; 817 } else { 818 /* since a virtual mixer has no backing registers to 819 * decide which path to connect, it will try to match 820 * with initial state. This is to ensure 821 * that the default mixer choice will be 822 * correctly powered up during initialization. 823 */ 824 p->connect = invert; 825 } 826 } 827 828 /* connect mixer widget to its interconnecting audio paths */ 829 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm, 830 struct snd_soc_dapm_path *path, const char *control_name) 831 { 832 int i, nth_path = 0; 833 834 /* search for mixer kcontrol */ 835 for (i = 0; i < path->sink->num_kcontrols; i++) { 836 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) { 837 path->name = path->sink->kcontrol_news[i].name; 838 dapm_set_mixer_path_status(path, i, nth_path++); 839 return 0; 840 } 841 } 842 return -ENODEV; 843 } 844 845 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm, 846 struct snd_soc_dapm_widget *kcontrolw, 847 const struct snd_kcontrol_new *kcontrol_new, 848 struct snd_kcontrol **kcontrol) 849 { 850 struct snd_soc_dapm_widget *w; 851 int i; 852 853 *kcontrol = NULL; 854 855 for_each_card_widgets(dapm->card, w) { 856 if (w == kcontrolw || w->dapm != kcontrolw->dapm) 857 continue; 858 for (i = 0; i < w->num_kcontrols; i++) { 859 if (&w->kcontrol_news[i] == kcontrol_new) { 860 if (w->kcontrols) 861 *kcontrol = w->kcontrols[i]; 862 return 1; 863 } 864 } 865 } 866 867 return 0; 868 } 869 870 /* 871 * Determine if a kcontrol is shared. If it is, look it up. If it isn't, 872 * create it. Either way, add the widget into the control's widget list 873 */ 874 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w, 875 int kci) 876 { 877 struct snd_soc_dapm_context *dapm = w->dapm; 878 struct snd_card *card = dapm->card->snd_card; 879 const char *prefix; 880 size_t prefix_len; 881 int shared; 882 struct snd_kcontrol *kcontrol; 883 bool wname_in_long_name, kcname_in_long_name; 884 char *long_name = NULL; 885 const char *name; 886 int ret = 0; 887 888 prefix = soc_dapm_prefix(dapm); 889 if (prefix) 890 prefix_len = strlen(prefix) + 1; 891 else 892 prefix_len = 0; 893 894 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci], 895 &kcontrol); 896 897 if (!kcontrol) { 898 if (shared) { 899 wname_in_long_name = false; 900 kcname_in_long_name = true; 901 } else { 902 switch (w->id) { 903 case snd_soc_dapm_switch: 904 case snd_soc_dapm_mixer: 905 case snd_soc_dapm_pga: 906 case snd_soc_dapm_effect: 907 case snd_soc_dapm_out_drv: 908 wname_in_long_name = true; 909 kcname_in_long_name = true; 910 break; 911 case snd_soc_dapm_mixer_named_ctl: 912 wname_in_long_name = false; 913 kcname_in_long_name = true; 914 break; 915 case snd_soc_dapm_demux: 916 case snd_soc_dapm_mux: 917 wname_in_long_name = true; 918 kcname_in_long_name = false; 919 break; 920 default: 921 return -EINVAL; 922 } 923 } 924 if (w->no_wname_in_kcontrol_name) 925 wname_in_long_name = false; 926 927 if (wname_in_long_name && kcname_in_long_name) { 928 /* 929 * The control will get a prefix from the control 930 * creation process but we're also using the same 931 * prefix for widgets so cut the prefix off the 932 * front of the widget name. 933 */ 934 long_name = kasprintf(GFP_KERNEL, "%s %s", 935 w->name + prefix_len, 936 w->kcontrol_news[kci].name); 937 if (long_name == NULL) 938 return -ENOMEM; 939 940 name = long_name; 941 } else if (wname_in_long_name) { 942 long_name = NULL; 943 name = w->name + prefix_len; 944 } else { 945 long_name = NULL; 946 name = w->kcontrol_news[kci].name; 947 } 948 949 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name, 950 prefix); 951 if (!kcontrol) { 952 ret = -ENOMEM; 953 goto exit_free; 954 } 955 956 kcontrol->private_free = dapm_kcontrol_free; 957 958 ret = dapm_kcontrol_data_alloc(w, kcontrol, name); 959 if (ret) { 960 snd_ctl_free_one(kcontrol); 961 goto exit_free; 962 } 963 964 ret = snd_ctl_add(card, kcontrol); 965 if (ret < 0) { 966 dev_err(dapm->dev, 967 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n", 968 w->name, name, ret); 969 goto exit_free; 970 } 971 } 972 973 ret = dapm_kcontrol_add_widget(kcontrol, w); 974 if (ret == 0) 975 w->kcontrols[kci] = kcontrol; 976 977 exit_free: 978 kfree(long_name); 979 980 return ret; 981 } 982 983 /* create new dapm mixer control */ 984 static int dapm_new_mixer(struct snd_soc_dapm_widget *w) 985 { 986 int i, ret; 987 struct snd_soc_dapm_path *path; 988 struct dapm_kcontrol_data *data; 989 990 /* add kcontrol */ 991 for (i = 0; i < w->num_kcontrols; i++) { 992 /* match name */ 993 snd_soc_dapm_widget_for_each_source_path(w, path) { 994 /* mixer/mux paths name must match control name */ 995 if (path->name != (char *)w->kcontrol_news[i].name) 996 continue; 997 998 if (!w->kcontrols[i]) { 999 ret = dapm_create_or_share_kcontrol(w, i); 1000 if (ret < 0) 1001 return ret; 1002 } 1003 1004 dapm_kcontrol_add_path(w->kcontrols[i], path); 1005 1006 data = snd_kcontrol_chip(w->kcontrols[i]); 1007 if (data->widget) 1008 snd_soc_dapm_add_path(data->widget->dapm, 1009 data->widget, 1010 path->source, 1011 NULL, NULL); 1012 } 1013 } 1014 1015 return 0; 1016 } 1017 1018 /* create new dapm mux control */ 1019 static int dapm_new_mux(struct snd_soc_dapm_widget *w) 1020 { 1021 struct snd_soc_dapm_context *dapm = w->dapm; 1022 enum snd_soc_dapm_direction dir; 1023 struct snd_soc_dapm_path *path; 1024 const char *type; 1025 int ret; 1026 1027 switch (w->id) { 1028 case snd_soc_dapm_mux: 1029 dir = SND_SOC_DAPM_DIR_OUT; 1030 type = "mux"; 1031 break; 1032 case snd_soc_dapm_demux: 1033 dir = SND_SOC_DAPM_DIR_IN; 1034 type = "demux"; 1035 break; 1036 default: 1037 return -EINVAL; 1038 } 1039 1040 if (w->num_kcontrols != 1) { 1041 dev_err(dapm->dev, 1042 "ASoC: %s %s has incorrect number of controls\n", type, 1043 w->name); 1044 return -EINVAL; 1045 } 1046 1047 if (list_empty(&w->edges[dir])) { 1048 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name); 1049 return -EINVAL; 1050 } 1051 1052 ret = dapm_create_or_share_kcontrol(w, 0); 1053 if (ret < 0) 1054 return ret; 1055 1056 snd_soc_dapm_widget_for_each_path(w, dir, path) { 1057 if (path->name) 1058 dapm_kcontrol_add_path(w->kcontrols[0], path); 1059 } 1060 1061 return 0; 1062 } 1063 1064 /* create new dapm volume control */ 1065 static int dapm_new_pga(struct snd_soc_dapm_widget *w) 1066 { 1067 int i; 1068 1069 for (i = 0; i < w->num_kcontrols; i++) { 1070 int ret = dapm_create_or_share_kcontrol(w, i); 1071 if (ret < 0) 1072 return ret; 1073 } 1074 1075 return 0; 1076 } 1077 1078 /* create new dapm dai link control */ 1079 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w) 1080 { 1081 int i; 1082 struct snd_soc_pcm_runtime *rtd = w->priv; 1083 1084 /* create control for links with > 1 config */ 1085 if (rtd->dai_link->num_c2c_params <= 1) 1086 return 0; 1087 1088 /* add kcontrol */ 1089 for (i = 0; i < w->num_kcontrols; i++) { 1090 struct snd_soc_dapm_context *dapm = w->dapm; 1091 struct snd_card *card = dapm->card->snd_card; 1092 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i], 1093 w, w->name, NULL); 1094 int ret = snd_ctl_add(card, kcontrol); 1095 1096 if (ret < 0) { 1097 dev_err(dapm->dev, 1098 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n", 1099 w->name, w->kcontrol_news[i].name, ret); 1100 return ret; 1101 } 1102 kcontrol->private_data = w; 1103 w->kcontrols[i] = kcontrol; 1104 } 1105 1106 return 0; 1107 } 1108 1109 /* We implement power down on suspend by checking the power state of 1110 * the ALSA card - when we are suspending the ALSA state for the card 1111 * is set to D3. 1112 */ 1113 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget) 1114 { 1115 int level = snd_power_get_state(widget->dapm->card->snd_card); 1116 1117 switch (level) { 1118 case SNDRV_CTL_POWER_D3hot: 1119 case SNDRV_CTL_POWER_D3cold: 1120 if (widget->ignore_suspend) 1121 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n", 1122 widget->name); 1123 return widget->ignore_suspend; 1124 default: 1125 return 1; 1126 } 1127 } 1128 1129 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list) 1130 { 1131 kfree(*list); 1132 } 1133 1134 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list, 1135 struct list_head *widgets) 1136 { 1137 struct snd_soc_dapm_widget *w; 1138 struct list_head *it; 1139 unsigned int size = 0; 1140 unsigned int i = 0; 1141 1142 list_for_each(it, widgets) 1143 size++; 1144 1145 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL); 1146 if (*list == NULL) 1147 return -ENOMEM; 1148 1149 (*list)->num_widgets = size; 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, struct snd_soc_dapm_update *update) 1747 { 1748 struct snd_soc_dapm_widget_list *wlist; 1749 struct snd_soc_dapm_widget *w = NULL; 1750 unsigned int wi; 1751 int ret; 1752 1753 if (!update || !dapm_kcontrol_is_powered(update->kcontrol)) 1754 return; 1755 1756 wlist = dapm_kcontrol_get_wlist(update->kcontrol); 1757 1758 for_each_dapm_widgets(wlist, wi, w) { 1759 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) { 1760 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG); 1761 if (ret != 0) 1762 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n", 1763 w->name, ret); 1764 } 1765 } 1766 1767 if (!w) 1768 return; 1769 1770 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask, 1771 update->val); 1772 if (ret < 0) 1773 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n", 1774 w->name, ret); 1775 1776 if (update->has_second_set) { 1777 ret = soc_dapm_update_bits(w->dapm, update->reg2, 1778 update->mask2, update->val2); 1779 if (ret < 0) 1780 dev_err(w->dapm->dev, 1781 "ASoC: %s DAPM update failed: %d\n", 1782 w->name, ret); 1783 } 1784 1785 for_each_dapm_widgets(wlist, wi, w) { 1786 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) { 1787 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG); 1788 if (ret != 0) 1789 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n", 1790 w->name, ret); 1791 } 1792 } 1793 } 1794 1795 /* Async callback run prior to DAPM sequences - brings to _PREPARE if 1796 * they're changing state. 1797 */ 1798 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie) 1799 { 1800 struct snd_soc_dapm_context *d = data; 1801 int ret; 1802 1803 /* If we're off and we're not supposed to go into STANDBY */ 1804 if (d->bias_level == SND_SOC_BIAS_OFF && 1805 d->target_bias_level != SND_SOC_BIAS_OFF) { 1806 if (d->dev && cookie) 1807 pm_runtime_get_sync(d->dev); 1808 1809 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); 1810 if (ret != 0) 1811 dev_err(d->dev, 1812 "ASoC: Failed to turn on bias: %d\n", ret); 1813 } 1814 1815 /* Prepare for a transition to ON or away from ON */ 1816 if ((d->target_bias_level == SND_SOC_BIAS_ON && 1817 d->bias_level != SND_SOC_BIAS_ON) || 1818 (d->target_bias_level != SND_SOC_BIAS_ON && 1819 d->bias_level == SND_SOC_BIAS_ON)) { 1820 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE); 1821 if (ret != 0) 1822 dev_err(d->dev, 1823 "ASoC: Failed to prepare bias: %d\n", ret); 1824 } 1825 } 1826 1827 /* Async callback run prior to DAPM sequences - brings to their final 1828 * state. 1829 */ 1830 static void dapm_post_sequence_async(void *data, async_cookie_t cookie) 1831 { 1832 struct snd_soc_dapm_context *d = data; 1833 int ret; 1834 1835 /* If we just powered the last thing off drop to standby bias */ 1836 if (d->bias_level == SND_SOC_BIAS_PREPARE && 1837 (d->target_bias_level == SND_SOC_BIAS_STANDBY || 1838 d->target_bias_level == SND_SOC_BIAS_OFF)) { 1839 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY); 1840 if (ret != 0) 1841 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n", 1842 ret); 1843 } 1844 1845 /* If we're in standby and can support bias off then do that */ 1846 if (d->bias_level == SND_SOC_BIAS_STANDBY && 1847 d->target_bias_level == SND_SOC_BIAS_OFF) { 1848 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF); 1849 if (ret != 0) 1850 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n", 1851 ret); 1852 1853 if (d->dev && cookie) 1854 pm_runtime_put(d->dev); 1855 } 1856 1857 /* If we just powered up then move to active bias */ 1858 if (d->bias_level == SND_SOC_BIAS_PREPARE && 1859 d->target_bias_level == SND_SOC_BIAS_ON) { 1860 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON); 1861 if (ret != 0) 1862 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n", 1863 ret); 1864 } 1865 } 1866 1867 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer, 1868 bool power, bool connect) 1869 { 1870 /* If a connection is being made or broken then that update 1871 * will have marked the peer dirty, otherwise the widgets are 1872 * not connected and this update has no impact. */ 1873 if (!connect) 1874 return; 1875 1876 /* If the peer is already in the state we're moving to then we 1877 * won't have an impact on it. */ 1878 if (power != peer->power) 1879 dapm_mark_dirty(peer, "peer state change"); 1880 } 1881 1882 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w, 1883 struct list_head *up_list, 1884 struct list_head *down_list) 1885 { 1886 struct snd_soc_dapm_path *path; 1887 int power; 1888 1889 switch (w->id) { 1890 case snd_soc_dapm_pre: 1891 power = 0; 1892 goto end; 1893 case snd_soc_dapm_post: 1894 power = 1; 1895 goto end; 1896 default: 1897 break; 1898 } 1899 1900 power = dapm_widget_power_check(w); 1901 1902 if (w->power == power) 1903 return; 1904 1905 trace_snd_soc_dapm_widget_power(w, power); 1906 1907 /* 1908 * If we changed our power state perhaps our neigbours 1909 * changed also. 1910 */ 1911 snd_soc_dapm_widget_for_each_source_path(w, path) 1912 dapm_widget_set_peer_power(path->source, power, path->connect); 1913 1914 /* 1915 * Supplies can't affect their outputs, only their inputs 1916 */ 1917 if (!w->is_supply) 1918 snd_soc_dapm_widget_for_each_sink_path(w, path) 1919 dapm_widget_set_peer_power(path->sink, power, path->connect); 1920 1921 end: 1922 if (power) 1923 dapm_seq_insert(w, up_list, true); 1924 else 1925 dapm_seq_insert(w, down_list, false); 1926 } 1927 1928 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm) 1929 { 1930 if (dapm->idle_bias_off) 1931 return true; 1932 1933 switch (snd_power_get_state(dapm->card->snd_card)) { 1934 case SNDRV_CTL_POWER_D3hot: 1935 case SNDRV_CTL_POWER_D3cold: 1936 return dapm->suspend_bias_off; 1937 default: 1938 break; 1939 } 1940 1941 return false; 1942 } 1943 1944 /* 1945 * Scan each dapm widget for complete audio path. 1946 * A complete path is a route that has valid endpoints i.e.:- 1947 * 1948 * o DAC to output pin. 1949 * o Input pin to ADC. 1950 * o Input pin to Output pin (bypass, sidetone) 1951 * o DAC to ADC (loopback). 1952 */ 1953 static int dapm_power_widgets(struct snd_soc_card *card, int event, 1954 struct snd_soc_dapm_update *update) 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, update); 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 (IS_ERR_OR_NULL(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, 2336 struct snd_soc_dapm_update *update, 2337 int mux, struct soc_enum *e) 2338 { 2339 struct snd_soc_dapm_path *path; 2340 int found = 0; 2341 bool connect; 2342 2343 snd_soc_dapm_mutex_assert_held(card); 2344 2345 /* find dapm widget path assoc with kcontrol */ 2346 dapm_kcontrol_for_each_path(path, kcontrol) { 2347 found = 1; 2348 /* we now need to match the string in the enum to the path */ 2349 if (e && !(strcmp(path->name, e->texts[mux]))) 2350 connect = true; 2351 else 2352 connect = false; 2353 2354 soc_dapm_connect_path(path, connect, "mux update"); 2355 } 2356 2357 if (found) 2358 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update); 2359 2360 return found; 2361 } 2362 2363 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm, 2364 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e, 2365 struct snd_soc_dapm_update *update) 2366 { 2367 struct snd_soc_card *card = dapm->card; 2368 int ret; 2369 2370 snd_soc_dapm_mutex_lock(card); 2371 ret = soc_dapm_mux_update_power(card, kcontrol, update, mux, e); 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 struct snd_soc_dapm_update *update, 2383 int connect, int rconnect) 2384 { 2385 struct snd_soc_dapm_path *path; 2386 int found = 0; 2387 2388 snd_soc_dapm_mutex_assert_held(card); 2389 2390 /* find dapm widget path assoc with kcontrol */ 2391 dapm_kcontrol_for_each_path(path, kcontrol) { 2392 /* 2393 * Ideally this function should support any number of 2394 * paths and channels. But since kcontrols only come 2395 * in mono and stereo variants, we are limited to 2 2396 * channels. 2397 * 2398 * The following code assumes for stereo controls the 2399 * first path (when 'found == 0') is the left channel, 2400 * and all remaining paths (when 'found == 1') are the 2401 * right channel. 2402 * 2403 * A stereo control is signified by a valid 'rconnect' 2404 * value, either 0 for unconnected, or >= 0 for connected. 2405 * This is chosen instead of using snd_soc_volsw_is_stereo, 2406 * so that the behavior of snd_soc_dapm_mixer_update_power 2407 * doesn't change even when the kcontrol passed in is 2408 * stereo. 2409 * 2410 * It passes 'connect' as the path connect status for 2411 * the left channel, and 'rconnect' for the right 2412 * channel. 2413 */ 2414 if (found && rconnect >= 0) 2415 soc_dapm_connect_path(path, rconnect, "mixer update"); 2416 else 2417 soc_dapm_connect_path(path, connect, "mixer update"); 2418 found = 1; 2419 } 2420 2421 if (found) 2422 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, update); 2423 2424 return found; 2425 } 2426 2427 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm, 2428 struct snd_kcontrol *kcontrol, int connect, 2429 struct snd_soc_dapm_update *update) 2430 { 2431 struct snd_soc_card *card = dapm->card; 2432 int ret; 2433 2434 snd_soc_dapm_mutex_lock(card); 2435 ret = soc_dapm_mixer_update_power(card, kcontrol, update, connect, -1); 2436 snd_soc_dapm_mutex_unlock(card); 2437 if (ret > 0) 2438 snd_soc_dpcm_runtime_update(card); 2439 return ret; 2440 } 2441 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power); 2442 2443 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt, 2444 char *buf, int count) 2445 { 2446 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt); 2447 struct snd_soc_dapm_widget *w; 2448 char *state = "not set"; 2449 2450 /* card won't be set for the dummy component, as a spot fix 2451 * we're checking for that case specifically here but in future 2452 * we will ensure that the dummy component looks like others. 2453 */ 2454 if (!cmpnt->card) 2455 return 0; 2456 2457 for_each_card_widgets(cmpnt->card, w) { 2458 if (w->dapm != dapm) 2459 continue; 2460 2461 /* only display widgets that burn power */ 2462 switch (w->id) { 2463 case snd_soc_dapm_hp: 2464 case snd_soc_dapm_mic: 2465 case snd_soc_dapm_spk: 2466 case snd_soc_dapm_line: 2467 case snd_soc_dapm_micbias: 2468 case snd_soc_dapm_dac: 2469 case snd_soc_dapm_adc: 2470 case snd_soc_dapm_pga: 2471 case snd_soc_dapm_effect: 2472 case snd_soc_dapm_out_drv: 2473 case snd_soc_dapm_mixer: 2474 case snd_soc_dapm_mixer_named_ctl: 2475 case snd_soc_dapm_supply: 2476 case snd_soc_dapm_regulator_supply: 2477 case snd_soc_dapm_pinctrl: 2478 case snd_soc_dapm_clock_supply: 2479 if (w->name) 2480 count += sysfs_emit_at(buf, count, "%s: %s\n", 2481 w->name, w->power ? "On":"Off"); 2482 break; 2483 default: 2484 break; 2485 } 2486 } 2487 2488 switch (snd_soc_dapm_get_bias_level(dapm)) { 2489 case SND_SOC_BIAS_ON: 2490 state = "On"; 2491 break; 2492 case SND_SOC_BIAS_PREPARE: 2493 state = "Prepare"; 2494 break; 2495 case SND_SOC_BIAS_STANDBY: 2496 state = "Standby"; 2497 break; 2498 case SND_SOC_BIAS_OFF: 2499 state = "Off"; 2500 break; 2501 } 2502 count += sysfs_emit_at(buf, count, "PM State: %s\n", state); 2503 2504 return count; 2505 } 2506 2507 /* show dapm widget status in sys fs */ 2508 static ssize_t dapm_widget_show(struct device *dev, 2509 struct device_attribute *attr, char *buf) 2510 { 2511 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); 2512 struct snd_soc_dai *codec_dai; 2513 int i, count = 0; 2514 2515 snd_soc_dapm_mutex_lock_root(rtd->card); 2516 2517 for_each_rtd_codec_dais(rtd, i, codec_dai) { 2518 struct snd_soc_component *cmpnt = codec_dai->component; 2519 2520 count = dapm_widget_show_component(cmpnt, buf, count); 2521 } 2522 2523 snd_soc_dapm_mutex_unlock(rtd->card); 2524 2525 return count; 2526 } 2527 2528 static DEVICE_ATTR_RO(dapm_widget); 2529 2530 struct attribute *soc_dapm_dev_attrs[] = { 2531 &dev_attr_dapm_widget.attr, 2532 NULL 2533 }; 2534 2535 static void dapm_free_path(struct snd_soc_dapm_path *path) 2536 { 2537 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]); 2538 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]); 2539 list_del(&path->list_kcontrol); 2540 list_del(&path->list); 2541 kfree(path); 2542 } 2543 2544 /** 2545 * snd_soc_dapm_free_widget - Free specified widget 2546 * @w: widget to free 2547 * 2548 * Removes widget from all paths and frees memory occupied by it. 2549 */ 2550 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w) 2551 { 2552 struct snd_soc_dapm_path *p, *next_p; 2553 enum snd_soc_dapm_direction dir; 2554 2555 if (!w) 2556 return; 2557 2558 list_del(&w->list); 2559 list_del(&w->dirty); 2560 /* 2561 * remove source and sink paths associated to this widget. 2562 * While removing the path, remove reference to it from both 2563 * source and sink widgets so that path is removed only once. 2564 */ 2565 snd_soc_dapm_for_each_direction(dir) { 2566 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p) 2567 dapm_free_path(p); 2568 } 2569 2570 dapm_debugfs_free_widget(w); 2571 2572 kfree(w->kcontrols); 2573 kfree_const(w->name); 2574 kfree_const(w->sname); 2575 kfree(w); 2576 } 2577 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget); 2578 2579 /* free all dapm widgets and resources */ 2580 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm) 2581 { 2582 struct snd_soc_dapm_widget *w, *next_w; 2583 2584 for_each_card_widgets_safe(dapm->card, w, next_w) { 2585 if (w->dapm != dapm) 2586 continue; 2587 snd_soc_dapm_free_widget(w); 2588 } 2589 2590 dapm->wcache_sink = NULL; 2591 dapm->wcache_source = NULL; 2592 } 2593 2594 static struct snd_soc_dapm_widget *dapm_find_widget( 2595 struct snd_soc_dapm_context *dapm, const char *pin, 2596 bool search_other_contexts) 2597 { 2598 struct snd_soc_dapm_widget *w; 2599 struct snd_soc_dapm_widget *fallback = NULL; 2600 char prefixed_pin[80]; 2601 const char *pin_name; 2602 const char *prefix = soc_dapm_prefix(dapm); 2603 2604 if (prefix) { 2605 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s", 2606 prefix, pin); 2607 pin_name = prefixed_pin; 2608 } else { 2609 pin_name = pin; 2610 } 2611 2612 for_each_card_widgets(dapm->card, w) { 2613 if (!strcmp(w->name, pin_name)) { 2614 if (w->dapm == dapm) 2615 return w; 2616 else 2617 fallback = w; 2618 } 2619 } 2620 2621 if (search_other_contexts) 2622 return fallback; 2623 2624 return NULL; 2625 } 2626 2627 /* 2628 * set the DAPM pin status: 2629 * returns 1 when the value has been updated, 0 when unchanged, or a negative 2630 * error code; called from kcontrol put callback 2631 */ 2632 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm, 2633 const char *pin, int status) 2634 { 2635 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 2636 int ret = 0; 2637 2638 dapm_assert_locked(dapm); 2639 2640 if (!w) { 2641 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin); 2642 return -EINVAL; 2643 } 2644 2645 if (w->connected != status) { 2646 dapm_mark_dirty(w, "pin configuration"); 2647 dapm_widget_invalidate_input_paths(w); 2648 dapm_widget_invalidate_output_paths(w); 2649 ret = 1; 2650 } 2651 2652 w->connected = status; 2653 if (status == 0) 2654 w->force = 0; 2655 2656 return ret; 2657 } 2658 2659 /* 2660 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful; 2661 * called from several API functions below 2662 */ 2663 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm, 2664 const char *pin, int status) 2665 { 2666 int ret = __snd_soc_dapm_set_pin(dapm, pin, status); 2667 2668 return ret < 0 ? ret : 0; 2669 } 2670 2671 /** 2672 * snd_soc_dapm_sync_unlocked - scan and power dapm paths 2673 * @dapm: DAPM context 2674 * 2675 * Walks all dapm audio paths and powers widgets according to their 2676 * stream or path usage. 2677 * 2678 * Requires external locking. 2679 * 2680 * Returns 0 for success. 2681 */ 2682 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm) 2683 { 2684 /* 2685 * Suppress early reports (eg, jacks syncing their state) to avoid 2686 * silly DAPM runs during card startup. 2687 */ 2688 if (!snd_soc_card_is_instantiated(dapm->card)) 2689 return 0; 2690 2691 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP, NULL); 2692 } 2693 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked); 2694 2695 /** 2696 * snd_soc_dapm_sync - scan and power dapm paths 2697 * @dapm: DAPM context 2698 * 2699 * Walks all dapm audio paths and powers widgets according to their 2700 * stream or path usage. 2701 * 2702 * Returns 0 for success. 2703 */ 2704 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm) 2705 { 2706 int ret; 2707 2708 snd_soc_dapm_mutex_lock(dapm); 2709 ret = snd_soc_dapm_sync_unlocked(dapm); 2710 snd_soc_dapm_mutex_unlock(dapm); 2711 return ret; 2712 } 2713 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync); 2714 2715 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p, 2716 struct snd_soc_dapm_widget *w, 2717 int channels) 2718 { 2719 switch (w->id) { 2720 case snd_soc_dapm_aif_out: 2721 case snd_soc_dapm_aif_in: 2722 break; 2723 default: 2724 return 0; 2725 } 2726 2727 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n", 2728 w->channel < channels ? "Connecting" : "Disconnecting", 2729 p->source->name, p->sink->name); 2730 2731 if (w->channel < channels) 2732 soc_dapm_connect_path(p, true, "dai update"); 2733 else 2734 soc_dapm_connect_path(p, false, "dai update"); 2735 2736 return 0; 2737 } 2738 2739 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream, 2740 struct snd_pcm_hw_params *params, 2741 struct snd_soc_dai *dai) 2742 { 2743 int dir = substream->stream; 2744 int channels = params_channels(params); 2745 struct snd_soc_dapm_path *p; 2746 struct snd_soc_dapm_widget *w; 2747 int ret; 2748 2749 w = snd_soc_dai_get_widget(dai, dir); 2750 2751 if (!w) 2752 return 0; 2753 2754 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name, snd_pcm_direction_name(dir)); 2755 2756 snd_soc_dapm_widget_for_each_sink_path(w, p) { 2757 ret = dapm_update_dai_chan(p, p->sink, channels); 2758 if (ret < 0) 2759 return ret; 2760 } 2761 2762 snd_soc_dapm_widget_for_each_source_path(w, p) { 2763 ret = dapm_update_dai_chan(p, p->source, channels); 2764 if (ret < 0) 2765 return ret; 2766 } 2767 2768 return 0; 2769 } 2770 2771 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream, 2772 struct snd_pcm_hw_params *params, 2773 struct snd_soc_dai *dai) 2774 { 2775 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 2776 int ret; 2777 2778 snd_soc_dapm_mutex_lock(rtd->card); 2779 ret = dapm_update_dai_unlocked(substream, params, dai); 2780 snd_soc_dapm_mutex_unlock(rtd->card); 2781 2782 return ret; 2783 } 2784 2785 int snd_soc_dapm_widget_name_cmp(struct snd_soc_dapm_widget *widget, const char *s) 2786 { 2787 struct snd_soc_component *component = widget->dapm->component; 2788 const char *wname = widget->name; 2789 2790 if (component && component->name_prefix) 2791 wname += strlen(component->name_prefix) + 1; /* plus space */ 2792 2793 return strcmp(wname, s); 2794 } 2795 EXPORT_SYMBOL_GPL(snd_soc_dapm_widget_name_cmp); 2796 2797 /* 2798 * dapm_update_widget_flags() - Re-compute widget sink and source flags 2799 * @w: The widget for which to update the flags 2800 * 2801 * Some widgets have a dynamic category which depends on which neighbors they 2802 * are connected to. This function update the category for these widgets. 2803 * 2804 * This function must be called whenever a path is added or removed to a widget. 2805 */ 2806 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w) 2807 { 2808 enum snd_soc_dapm_direction dir; 2809 struct snd_soc_dapm_path *p; 2810 unsigned int ep; 2811 2812 switch (w->id) { 2813 case snd_soc_dapm_input: 2814 /* On a fully routed card an input is never a source */ 2815 if (w->dapm->card->fully_routed) 2816 return; 2817 ep = SND_SOC_DAPM_EP_SOURCE; 2818 snd_soc_dapm_widget_for_each_source_path(w, p) { 2819 if (p->source->id == snd_soc_dapm_micbias || 2820 p->source->id == snd_soc_dapm_mic || 2821 p->source->id == snd_soc_dapm_line || 2822 p->source->id == snd_soc_dapm_output) { 2823 ep = 0; 2824 break; 2825 } 2826 } 2827 break; 2828 case snd_soc_dapm_output: 2829 /* On a fully routed card a output is never a sink */ 2830 if (w->dapm->card->fully_routed) 2831 return; 2832 ep = SND_SOC_DAPM_EP_SINK; 2833 snd_soc_dapm_widget_for_each_sink_path(w, p) { 2834 if (p->sink->id == snd_soc_dapm_spk || 2835 p->sink->id == snd_soc_dapm_hp || 2836 p->sink->id == snd_soc_dapm_line || 2837 p->sink->id == snd_soc_dapm_input) { 2838 ep = 0; 2839 break; 2840 } 2841 } 2842 break; 2843 case snd_soc_dapm_line: 2844 ep = 0; 2845 snd_soc_dapm_for_each_direction(dir) { 2846 if (!list_empty(&w->edges[dir])) 2847 ep |= SND_SOC_DAPM_DIR_TO_EP(dir); 2848 } 2849 break; 2850 default: 2851 return; 2852 } 2853 2854 w->is_ep = ep; 2855 } 2856 2857 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm, 2858 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink, 2859 const char *control) 2860 { 2861 bool dynamic_source = false; 2862 bool dynamic_sink = false; 2863 2864 if (!control) 2865 return 0; 2866 2867 switch (source->id) { 2868 case snd_soc_dapm_demux: 2869 dynamic_source = true; 2870 break; 2871 default: 2872 break; 2873 } 2874 2875 switch (sink->id) { 2876 case snd_soc_dapm_mux: 2877 case snd_soc_dapm_switch: 2878 case snd_soc_dapm_mixer: 2879 case snd_soc_dapm_mixer_named_ctl: 2880 dynamic_sink = true; 2881 break; 2882 default: 2883 break; 2884 } 2885 2886 if (dynamic_source && dynamic_sink) { 2887 dev_err(dapm->dev, 2888 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n", 2889 source->name, control, sink->name); 2890 return -EINVAL; 2891 } else if (!dynamic_source && !dynamic_sink) { 2892 dev_err(dapm->dev, 2893 "Control not supported for path %s -> [%s] -> %s\n", 2894 source->name, control, sink->name); 2895 return -EINVAL; 2896 } 2897 2898 return 0; 2899 } 2900 2901 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm, 2902 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink, 2903 const char *control, 2904 int (*connected)(struct snd_soc_dapm_widget *source, 2905 struct snd_soc_dapm_widget *sink)) 2906 { 2907 enum snd_soc_dapm_direction dir; 2908 struct snd_soc_dapm_path *path; 2909 int ret; 2910 2911 if (wsink->is_supply && !wsource->is_supply) { 2912 dev_err(dapm->dev, 2913 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n", 2914 wsource->name, wsink->name); 2915 return -EINVAL; 2916 } 2917 2918 if (connected && !wsource->is_supply) { 2919 dev_err(dapm->dev, 2920 "connected() callback only supported for supply widgets (%s -> %s)\n", 2921 wsource->name, wsink->name); 2922 return -EINVAL; 2923 } 2924 2925 if (wsource->is_supply && control) { 2926 dev_err(dapm->dev, 2927 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n", 2928 wsource->name, control, wsink->name); 2929 return -EINVAL; 2930 } 2931 2932 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control); 2933 if (ret) 2934 return ret; 2935 2936 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL); 2937 if (!path) 2938 return -ENOMEM; 2939 2940 path->node[SND_SOC_DAPM_DIR_IN] = wsource; 2941 path->node[SND_SOC_DAPM_DIR_OUT] = wsink; 2942 2943 path->connected = connected; 2944 INIT_LIST_HEAD(&path->list); 2945 INIT_LIST_HEAD(&path->list_kcontrol); 2946 2947 if (wsource->is_supply || wsink->is_supply) 2948 path->is_supply = 1; 2949 2950 /* connect static paths */ 2951 if (control == NULL) { 2952 path->connect = 1; 2953 } else { 2954 switch (wsource->id) { 2955 case snd_soc_dapm_demux: 2956 ret = dapm_connect_mux(dapm, path, control, wsource); 2957 if (ret) 2958 goto err; 2959 break; 2960 default: 2961 break; 2962 } 2963 2964 switch (wsink->id) { 2965 case snd_soc_dapm_mux: 2966 ret = dapm_connect_mux(dapm, path, control, wsink); 2967 if (ret != 0) 2968 goto err; 2969 break; 2970 case snd_soc_dapm_switch: 2971 case snd_soc_dapm_mixer: 2972 case snd_soc_dapm_mixer_named_ctl: 2973 ret = dapm_connect_mixer(dapm, path, control); 2974 if (ret != 0) 2975 goto err; 2976 break; 2977 default: 2978 break; 2979 } 2980 } 2981 2982 list_add(&path->list, &dapm->card->paths); 2983 2984 snd_soc_dapm_for_each_direction(dir) 2985 list_add(&path->list_node[dir], &path->node[dir]->edges[dir]); 2986 2987 snd_soc_dapm_for_each_direction(dir) { 2988 dapm_update_widget_flags(path->node[dir]); 2989 dapm_mark_dirty(path->node[dir], "Route added"); 2990 } 2991 2992 if (snd_soc_card_is_instantiated(dapm->card) && path->connect) 2993 dapm_path_invalidate(path); 2994 2995 return 0; 2996 err: 2997 kfree(path); 2998 return ret; 2999 } 3000 3001 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm, 3002 const struct snd_soc_dapm_route *route) 3003 { 3004 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w; 3005 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL; 3006 const char *sink; 3007 const char *source; 3008 char prefixed_sink[80]; 3009 char prefixed_source[80]; 3010 const char *prefix; 3011 unsigned int sink_ref = 0; 3012 unsigned int source_ref = 0; 3013 int ret; 3014 3015 prefix = soc_dapm_prefix(dapm); 3016 if (prefix) { 3017 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s", 3018 prefix, route->sink); 3019 sink = prefixed_sink; 3020 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s", 3021 prefix, route->source); 3022 source = prefixed_source; 3023 } else { 3024 sink = route->sink; 3025 source = route->source; 3026 } 3027 3028 wsource = dapm_wcache_lookup(dapm->wcache_source, source); 3029 wsink = dapm_wcache_lookup(dapm->wcache_sink, sink); 3030 3031 if (wsink && wsource) 3032 goto skip_search; 3033 3034 /* 3035 * find src and dest widgets over all widgets but favor a widget from 3036 * current DAPM context 3037 */ 3038 for_each_card_widgets(dapm->card, w) { 3039 if (!wsink && !(strcmp(w->name, sink))) { 3040 wtsink = w; 3041 if (w->dapm == dapm) { 3042 wsink = w; 3043 if (wsource) 3044 break; 3045 } 3046 sink_ref++; 3047 if (sink_ref > 1) 3048 dev_warn(dapm->dev, 3049 "ASoC: sink widget %s overwritten\n", 3050 w->name); 3051 continue; 3052 } 3053 if (!wsource && !(strcmp(w->name, source))) { 3054 wtsource = w; 3055 if (w->dapm == dapm) { 3056 wsource = w; 3057 if (wsink) 3058 break; 3059 } 3060 source_ref++; 3061 if (source_ref > 1) 3062 dev_warn(dapm->dev, 3063 "ASoC: source widget %s overwritten\n", 3064 w->name); 3065 } 3066 } 3067 /* use widget from another DAPM context if not found from this */ 3068 if (!wsink) 3069 wsink = wtsink; 3070 if (!wsource) 3071 wsource = wtsource; 3072 3073 ret = -ENODEV; 3074 if (!wsource) 3075 goto err; 3076 if (!wsink) 3077 goto err; 3078 3079 skip_search: 3080 /* update cache */ 3081 dapm->wcache_sink = wsink; 3082 dapm->wcache_source = wsource; 3083 3084 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control, 3085 route->connected); 3086 err: 3087 if (ret) 3088 dev_err(dapm->dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n", 3089 source, !wsource ? "(*)" : "", 3090 !route->control ? "" : "> [", 3091 !route->control ? "" : route->control, 3092 !route->control ? "" : "] -", 3093 sink, !wsink ? "(*)" : ""); 3094 return ret; 3095 } 3096 3097 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm, 3098 const struct snd_soc_dapm_route *route) 3099 { 3100 struct snd_soc_dapm_path *path, *p; 3101 const char *sink; 3102 const char *source; 3103 char prefixed_sink[80]; 3104 char prefixed_source[80]; 3105 const char *prefix; 3106 3107 if (route->control) { 3108 dev_err(dapm->dev, 3109 "ASoC: Removal of routes with controls not supported\n"); 3110 return -EINVAL; 3111 } 3112 3113 prefix = soc_dapm_prefix(dapm); 3114 if (prefix) { 3115 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s", 3116 prefix, route->sink); 3117 sink = prefixed_sink; 3118 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s", 3119 prefix, route->source); 3120 source = prefixed_source; 3121 } else { 3122 sink = route->sink; 3123 source = route->source; 3124 } 3125 3126 path = NULL; 3127 list_for_each_entry(p, &dapm->card->paths, list) { 3128 if (strcmp(p->source->name, source) != 0) 3129 continue; 3130 if (strcmp(p->sink->name, sink) != 0) 3131 continue; 3132 path = p; 3133 break; 3134 } 3135 3136 if (path) { 3137 struct snd_soc_dapm_widget *wsource = path->source; 3138 struct snd_soc_dapm_widget *wsink = path->sink; 3139 3140 dapm_mark_dirty(wsource, "Route removed"); 3141 dapm_mark_dirty(wsink, "Route removed"); 3142 if (path->connect) 3143 dapm_path_invalidate(path); 3144 3145 dapm_free_path(path); 3146 3147 /* Update any path related flags */ 3148 dapm_update_widget_flags(wsource); 3149 dapm_update_widget_flags(wsink); 3150 } else { 3151 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n", 3152 source, sink); 3153 } 3154 3155 return 0; 3156 } 3157 3158 /** 3159 * snd_soc_dapm_add_routes - Add routes between DAPM widgets 3160 * @dapm: DAPM context 3161 * @route: audio routes 3162 * @num: number of routes 3163 * 3164 * Connects 2 dapm widgets together via a named audio path. The sink is 3165 * the widget receiving the audio signal, whilst the source is the sender 3166 * of the audio signal. 3167 * 3168 * Returns 0 for success else error. On error all resources can be freed 3169 * with a call to snd_soc_card_free(). 3170 */ 3171 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm, 3172 const struct snd_soc_dapm_route *route, int num) 3173 { 3174 int i, ret = 0; 3175 3176 snd_soc_dapm_mutex_lock(dapm); 3177 for (i = 0; i < num; i++) { 3178 int r = snd_soc_dapm_add_route(dapm, route); 3179 if (r < 0) 3180 ret = r; 3181 route++; 3182 } 3183 snd_soc_dapm_mutex_unlock(dapm); 3184 3185 return ret; 3186 } 3187 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes); 3188 3189 /** 3190 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets 3191 * @dapm: DAPM context 3192 * @route: audio routes 3193 * @num: number of routes 3194 * 3195 * Removes routes from the DAPM context. 3196 */ 3197 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm, 3198 const struct snd_soc_dapm_route *route, int num) 3199 { 3200 int i; 3201 3202 snd_soc_dapm_mutex_lock(dapm); 3203 for (i = 0; i < num; i++) { 3204 snd_soc_dapm_del_route(dapm, route); 3205 route++; 3206 } 3207 snd_soc_dapm_mutex_unlock(dapm); 3208 3209 return 0; 3210 } 3211 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes); 3212 3213 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm, 3214 const struct snd_soc_dapm_route *route) 3215 { 3216 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm, 3217 route->source, 3218 true); 3219 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm, 3220 route->sink, 3221 true); 3222 struct snd_soc_dapm_path *path; 3223 int count = 0; 3224 3225 if (!source) { 3226 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n", 3227 route->source); 3228 return -ENODEV; 3229 } 3230 3231 if (!sink) { 3232 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n", 3233 route->sink); 3234 return -ENODEV; 3235 } 3236 3237 if (route->control || route->connected) 3238 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n", 3239 route->source, route->sink); 3240 3241 snd_soc_dapm_widget_for_each_sink_path(source, path) { 3242 if (path->sink == sink) { 3243 path->weak = 1; 3244 count++; 3245 } 3246 } 3247 3248 if (count == 0) 3249 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n", 3250 route->source, route->sink); 3251 if (count > 1) 3252 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n", 3253 count, route->source, route->sink); 3254 3255 return 0; 3256 } 3257 3258 /** 3259 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak 3260 * @dapm: DAPM context 3261 * @route: audio routes 3262 * @num: number of routes 3263 * 3264 * Mark existing routes matching those specified in the passed array 3265 * as being weak, meaning that they are ignored for the purpose of 3266 * power decisions. The main intended use case is for sidetone paths 3267 * which couple audio between other independent paths if they are both 3268 * active in order to make the combination work better at the user 3269 * level but which aren't intended to be "used". 3270 * 3271 * Note that CODEC drivers should not use this as sidetone type paths 3272 * can frequently also be used as bypass paths. 3273 */ 3274 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm, 3275 const struct snd_soc_dapm_route *route, int num) 3276 { 3277 int i; 3278 int ret = 0; 3279 3280 snd_soc_dapm_mutex_lock_root(dapm); 3281 for (i = 0; i < num; i++) { 3282 int err = snd_soc_dapm_weak_route(dapm, route); 3283 if (err) 3284 ret = err; 3285 route++; 3286 } 3287 snd_soc_dapm_mutex_unlock(dapm); 3288 3289 return ret; 3290 } 3291 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes); 3292 3293 /** 3294 * snd_soc_dapm_new_widgets - add new dapm widgets 3295 * @card: card to be checked for new dapm widgets 3296 * 3297 * Checks the codec for any new dapm widgets and creates them if found. 3298 * 3299 * Returns 0 for success. 3300 */ 3301 int snd_soc_dapm_new_widgets(struct snd_soc_card *card) 3302 { 3303 struct snd_soc_dapm_widget *w; 3304 unsigned int val; 3305 3306 snd_soc_dapm_mutex_lock_root(card); 3307 3308 for_each_card_widgets(card, w) 3309 { 3310 if (w->new) 3311 continue; 3312 3313 if (w->num_kcontrols) { 3314 w->kcontrols = kcalloc(w->num_kcontrols, 3315 sizeof(struct snd_kcontrol *), 3316 GFP_KERNEL); 3317 if (!w->kcontrols) { 3318 snd_soc_dapm_mutex_unlock(card); 3319 return -ENOMEM; 3320 } 3321 } 3322 3323 switch(w->id) { 3324 case snd_soc_dapm_switch: 3325 case snd_soc_dapm_mixer: 3326 case snd_soc_dapm_mixer_named_ctl: 3327 dapm_new_mixer(w); 3328 break; 3329 case snd_soc_dapm_mux: 3330 case snd_soc_dapm_demux: 3331 dapm_new_mux(w); 3332 break; 3333 case snd_soc_dapm_pga: 3334 case snd_soc_dapm_effect: 3335 case snd_soc_dapm_out_drv: 3336 dapm_new_pga(w); 3337 break; 3338 case snd_soc_dapm_dai_link: 3339 dapm_new_dai_link(w); 3340 break; 3341 default: 3342 break; 3343 } 3344 3345 /* Read the initial power state from the device */ 3346 if (w->reg >= 0) { 3347 val = soc_dapm_read(w->dapm, w->reg); 3348 val = val >> w->shift; 3349 val &= w->mask; 3350 if (val == w->on_val) 3351 w->power = 1; 3352 } 3353 3354 w->new = 1; 3355 3356 dapm_mark_dirty(w, "new widget"); 3357 dapm_debugfs_add_widget(w); 3358 } 3359 3360 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP, NULL); 3361 snd_soc_dapm_mutex_unlock(card); 3362 return 0; 3363 } 3364 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets); 3365 3366 /** 3367 * snd_soc_dapm_get_volsw - dapm mixer get callback 3368 * @kcontrol: mixer control 3369 * @ucontrol: control element information 3370 * 3371 * Callback to get the value of a dapm mixer control. 3372 * 3373 * Returns 0 for success. 3374 */ 3375 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol, 3376 struct snd_ctl_elem_value *ucontrol) 3377 { 3378 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3379 struct soc_mixer_control *mc = 3380 (struct soc_mixer_control *)kcontrol->private_value; 3381 int reg = mc->reg; 3382 unsigned int shift = mc->shift; 3383 int max = mc->max; 3384 unsigned int width = fls(max); 3385 unsigned int mask = (1 << fls(max)) - 1; 3386 unsigned int invert = mc->invert; 3387 unsigned int reg_val, val, rval = 0; 3388 3389 snd_soc_dapm_mutex_lock(dapm); 3390 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) { 3391 reg_val = soc_dapm_read(dapm, reg); 3392 val = (reg_val >> shift) & mask; 3393 3394 if (reg != mc->rreg) 3395 reg_val = soc_dapm_read(dapm, mc->rreg); 3396 3397 if (snd_soc_volsw_is_stereo(mc)) 3398 rval = (reg_val >> mc->rshift) & mask; 3399 } else { 3400 reg_val = dapm_kcontrol_get_value(kcontrol); 3401 val = reg_val & mask; 3402 3403 if (snd_soc_volsw_is_stereo(mc)) 3404 rval = (reg_val >> width) & mask; 3405 } 3406 snd_soc_dapm_mutex_unlock(dapm); 3407 3408 if (invert) 3409 ucontrol->value.integer.value[0] = max - val; 3410 else 3411 ucontrol->value.integer.value[0] = val; 3412 3413 if (snd_soc_volsw_is_stereo(mc)) { 3414 if (invert) 3415 ucontrol->value.integer.value[1] = max - rval; 3416 else 3417 ucontrol->value.integer.value[1] = rval; 3418 } 3419 3420 return 0; 3421 } 3422 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw); 3423 3424 /** 3425 * snd_soc_dapm_put_volsw - dapm mixer set callback 3426 * @kcontrol: mixer control 3427 * @ucontrol: control element information 3428 * 3429 * Callback to set the value of a dapm mixer control. 3430 * 3431 * Returns 0 for success. 3432 */ 3433 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol, 3434 struct snd_ctl_elem_value *ucontrol) 3435 { 3436 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3437 struct snd_soc_card *card = dapm->card; 3438 struct soc_mixer_control *mc = 3439 (struct soc_mixer_control *)kcontrol->private_value; 3440 int reg = mc->reg; 3441 unsigned int shift = mc->shift; 3442 int max = mc->max; 3443 unsigned int width = fls(max); 3444 unsigned int mask = (1 << width) - 1; 3445 unsigned int invert = mc->invert; 3446 unsigned int val, rval = 0; 3447 int connect, rconnect = -1, change, reg_change = 0; 3448 struct snd_soc_dapm_update update = {}; 3449 struct snd_soc_dapm_update *pupdate = NULL; 3450 int ret = 0; 3451 3452 val = (ucontrol->value.integer.value[0] & mask); 3453 connect = !!val; 3454 3455 if (invert) 3456 val = max - val; 3457 3458 if (snd_soc_volsw_is_stereo(mc)) { 3459 rval = (ucontrol->value.integer.value[1] & mask); 3460 rconnect = !!rval; 3461 if (invert) 3462 rval = max - rval; 3463 } 3464 3465 snd_soc_dapm_mutex_lock(card); 3466 3467 /* This assumes field width < (bits in unsigned int / 2) */ 3468 if (width > sizeof(unsigned int) * 8 / 2) 3469 dev_warn(dapm->dev, 3470 "ASoC: control %s field width limit exceeded\n", 3471 kcontrol->id.name); 3472 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width)); 3473 3474 if (reg != SND_SOC_NOPM) { 3475 val = val << shift; 3476 rval = rval << mc->rshift; 3477 3478 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val); 3479 3480 if (snd_soc_volsw_is_stereo(mc)) 3481 reg_change |= soc_dapm_test_bits(dapm, mc->rreg, 3482 mask << mc->rshift, 3483 rval); 3484 } 3485 3486 if (change || reg_change) { 3487 if (reg_change) { 3488 if (snd_soc_volsw_is_stereo(mc)) { 3489 update.has_second_set = true; 3490 update.reg2 = mc->rreg; 3491 update.mask2 = mask << mc->rshift; 3492 update.val2 = rval; 3493 } 3494 update.kcontrol = kcontrol; 3495 update.reg = reg; 3496 update.mask = mask << shift; 3497 update.val = val; 3498 pupdate = &update; 3499 } 3500 ret = soc_dapm_mixer_update_power(card, kcontrol, pupdate, connect, rconnect); 3501 } 3502 3503 snd_soc_dapm_mutex_unlock(card); 3504 3505 if (ret > 0) 3506 snd_soc_dpcm_runtime_update(card); 3507 3508 return change; 3509 } 3510 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw); 3511 3512 /** 3513 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback 3514 * @kcontrol: mixer control 3515 * @ucontrol: control element information 3516 * 3517 * Callback to get the value of a dapm enumerated double mixer control. 3518 * 3519 * Returns 0 for success. 3520 */ 3521 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol, 3522 struct snd_ctl_elem_value *ucontrol) 3523 { 3524 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3525 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 3526 unsigned int reg_val, val; 3527 3528 snd_soc_dapm_mutex_lock(dapm); 3529 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) { 3530 reg_val = soc_dapm_read(dapm, e->reg); 3531 } else { 3532 reg_val = dapm_kcontrol_get_value(kcontrol); 3533 } 3534 snd_soc_dapm_mutex_unlock(dapm); 3535 3536 val = (reg_val >> e->shift_l) & e->mask; 3537 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val); 3538 if (e->shift_l != e->shift_r) { 3539 val = (reg_val >> e->shift_r) & e->mask; 3540 val = snd_soc_enum_val_to_item(e, val); 3541 ucontrol->value.enumerated.item[1] = val; 3542 } 3543 3544 return 0; 3545 } 3546 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double); 3547 3548 /** 3549 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback 3550 * @kcontrol: mixer control 3551 * @ucontrol: control element information 3552 * 3553 * Callback to set the value of a dapm enumerated double mixer control. 3554 * 3555 * Returns 0 for success. 3556 */ 3557 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol, 3558 struct snd_ctl_elem_value *ucontrol) 3559 { 3560 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3561 struct snd_soc_card *card = dapm->card; 3562 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 3563 unsigned int *item = ucontrol->value.enumerated.item; 3564 unsigned int val, change, reg_change = 0; 3565 unsigned int mask; 3566 struct snd_soc_dapm_update update = {}; 3567 struct snd_soc_dapm_update *pupdate = NULL; 3568 int ret = 0; 3569 3570 if (item[0] >= e->items) 3571 return -EINVAL; 3572 3573 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 3574 mask = e->mask << e->shift_l; 3575 if (e->shift_l != e->shift_r) { 3576 if (item[1] > e->items) 3577 return -EINVAL; 3578 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r; 3579 mask |= e->mask << e->shift_r; 3580 } 3581 3582 snd_soc_dapm_mutex_lock(card); 3583 3584 change = dapm_kcontrol_set_value(kcontrol, val); 3585 3586 if (e->reg != SND_SOC_NOPM) 3587 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val); 3588 3589 if (change || reg_change) { 3590 if (reg_change) { 3591 update.kcontrol = kcontrol; 3592 update.reg = e->reg; 3593 update.mask = mask; 3594 update.val = val; 3595 pupdate = &update; 3596 } 3597 ret = soc_dapm_mux_update_power(card, kcontrol, pupdate, item[0], e); 3598 } 3599 3600 snd_soc_dapm_mutex_unlock(card); 3601 3602 if (ret > 0) 3603 snd_soc_dpcm_runtime_update(card); 3604 3605 return change; 3606 } 3607 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double); 3608 3609 /** 3610 * snd_soc_dapm_info_pin_switch - Info for a pin switch 3611 * 3612 * @kcontrol: mixer control 3613 * @uinfo: control element information 3614 * 3615 * Callback to provide information about a pin switch control. 3616 */ 3617 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol, 3618 struct snd_ctl_elem_info *uinfo) 3619 { 3620 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 3621 uinfo->count = 1; 3622 uinfo->value.integer.min = 0; 3623 uinfo->value.integer.max = 1; 3624 3625 return 0; 3626 } 3627 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch); 3628 3629 static int __snd_soc_dapm_get_pin_switch(struct snd_soc_dapm_context *dapm, 3630 const char *pin, 3631 struct snd_ctl_elem_value *ucontrol) 3632 { 3633 snd_soc_dapm_mutex_lock(dapm); 3634 ucontrol->value.integer.value[0] = snd_soc_dapm_get_pin_status(dapm, pin); 3635 snd_soc_dapm_mutex_unlock(dapm); 3636 3637 return 0; 3638 } 3639 3640 /** 3641 * snd_soc_dapm_get_pin_switch - Get information for a pin switch 3642 * 3643 * @kcontrol: mixer control 3644 * @ucontrol: Value 3645 * 3646 * Callback to provide information for a pin switch added at the card 3647 * level. 3648 */ 3649 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol, 3650 struct snd_ctl_elem_value *ucontrol) 3651 { 3652 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 3653 const char *pin = (const char *)kcontrol->private_value; 3654 3655 return __snd_soc_dapm_get_pin_switch(&card->dapm, pin, ucontrol); 3656 } 3657 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch); 3658 3659 /** 3660 * snd_soc_dapm_get_component_pin_switch - Get information for a pin switch 3661 * 3662 * @kcontrol: mixer control 3663 * @ucontrol: Value 3664 * 3665 * Callback to provide information for a pin switch added at the component 3666 * level. 3667 */ 3668 int snd_soc_dapm_get_component_pin_switch(struct snd_kcontrol *kcontrol, 3669 struct snd_ctl_elem_value *ucontrol) 3670 { 3671 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3672 const char *pin = (const char *)kcontrol->private_value; 3673 3674 return __snd_soc_dapm_get_pin_switch(&component->dapm, pin, ucontrol); 3675 } 3676 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_component_pin_switch); 3677 3678 static int __snd_soc_dapm_put_pin_switch(struct snd_soc_dapm_context *dapm, 3679 const char *pin, 3680 struct snd_ctl_elem_value *ucontrol) 3681 { 3682 int ret; 3683 3684 snd_soc_dapm_mutex_lock(dapm); 3685 ret = __snd_soc_dapm_set_pin(dapm, pin, !!ucontrol->value.integer.value[0]); 3686 snd_soc_dapm_mutex_unlock(dapm); 3687 3688 snd_soc_dapm_sync(dapm); 3689 3690 return ret; 3691 } 3692 3693 /** 3694 * snd_soc_dapm_put_pin_switch - Set information for a pin switch 3695 * 3696 * @kcontrol: mixer control 3697 * @ucontrol: Value 3698 * 3699 * Callback to provide information for a pin switch added at the card 3700 * level. 3701 */ 3702 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol, 3703 struct snd_ctl_elem_value *ucontrol) 3704 { 3705 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); 3706 const char *pin = (const char *)kcontrol->private_value; 3707 3708 return __snd_soc_dapm_put_pin_switch(&card->dapm, pin, ucontrol); 3709 } 3710 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch); 3711 3712 /** 3713 * snd_soc_dapm_put_component_pin_switch - Set information for a pin switch 3714 * 3715 * @kcontrol: mixer control 3716 * @ucontrol: Value 3717 * 3718 * Callback to provide information for a pin switch added at the component 3719 * level. 3720 */ 3721 int snd_soc_dapm_put_component_pin_switch(struct snd_kcontrol *kcontrol, 3722 struct snd_ctl_elem_value *ucontrol) 3723 { 3724 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 3725 const char *pin = (const char *)kcontrol->private_value; 3726 3727 return __snd_soc_dapm_put_pin_switch(&component->dapm, pin, ucontrol); 3728 } 3729 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_component_pin_switch); 3730 3731 struct snd_soc_dapm_widget * 3732 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, 3733 const struct snd_soc_dapm_widget *widget) 3734 { 3735 enum snd_soc_dapm_direction dir; 3736 struct snd_soc_dapm_widget *w; 3737 int ret = -ENOMEM; 3738 3739 w = dapm_cnew_widget(widget, soc_dapm_prefix(dapm)); 3740 if (!w) 3741 goto cnew_failed; 3742 3743 switch (w->id) { 3744 case snd_soc_dapm_regulator_supply: 3745 w->regulator = devm_regulator_get(dapm->dev, widget->name); 3746 if (IS_ERR(w->regulator)) { 3747 ret = PTR_ERR(w->regulator); 3748 goto request_failed; 3749 } 3750 3751 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) { 3752 ret = regulator_allow_bypass(w->regulator, true); 3753 if (ret != 0) 3754 dev_warn(dapm->dev, 3755 "ASoC: Failed to bypass %s: %d\n", 3756 w->name, ret); 3757 } 3758 break; 3759 case snd_soc_dapm_pinctrl: 3760 w->pinctrl = devm_pinctrl_get(dapm->dev); 3761 if (IS_ERR(w->pinctrl)) { 3762 ret = PTR_ERR(w->pinctrl); 3763 goto request_failed; 3764 } 3765 3766 /* set to sleep_state when initializing */ 3767 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD); 3768 break; 3769 case snd_soc_dapm_clock_supply: 3770 w->clk = devm_clk_get(dapm->dev, widget->name); 3771 if (IS_ERR(w->clk)) { 3772 ret = PTR_ERR(w->clk); 3773 goto request_failed; 3774 } 3775 break; 3776 default: 3777 break; 3778 } 3779 3780 switch (w->id) { 3781 case snd_soc_dapm_mic: 3782 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3783 w->power_check = dapm_generic_check_power; 3784 break; 3785 case snd_soc_dapm_input: 3786 if (!dapm->card->fully_routed) 3787 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3788 w->power_check = dapm_generic_check_power; 3789 break; 3790 case snd_soc_dapm_spk: 3791 case snd_soc_dapm_hp: 3792 w->is_ep = SND_SOC_DAPM_EP_SINK; 3793 w->power_check = dapm_generic_check_power; 3794 break; 3795 case snd_soc_dapm_output: 3796 if (!dapm->card->fully_routed) 3797 w->is_ep = SND_SOC_DAPM_EP_SINK; 3798 w->power_check = dapm_generic_check_power; 3799 break; 3800 case snd_soc_dapm_vmid: 3801 case snd_soc_dapm_siggen: 3802 w->is_ep = SND_SOC_DAPM_EP_SOURCE; 3803 w->power_check = dapm_always_on_check_power; 3804 break; 3805 case snd_soc_dapm_sink: 3806 w->is_ep = SND_SOC_DAPM_EP_SINK; 3807 w->power_check = dapm_always_on_check_power; 3808 break; 3809 3810 case snd_soc_dapm_mux: 3811 case snd_soc_dapm_demux: 3812 case snd_soc_dapm_switch: 3813 case snd_soc_dapm_mixer: 3814 case snd_soc_dapm_mixer_named_ctl: 3815 case snd_soc_dapm_adc: 3816 case snd_soc_dapm_aif_out: 3817 case snd_soc_dapm_dac: 3818 case snd_soc_dapm_aif_in: 3819 case snd_soc_dapm_pga: 3820 case snd_soc_dapm_buffer: 3821 case snd_soc_dapm_scheduler: 3822 case snd_soc_dapm_effect: 3823 case snd_soc_dapm_src: 3824 case snd_soc_dapm_asrc: 3825 case snd_soc_dapm_encoder: 3826 case snd_soc_dapm_decoder: 3827 case snd_soc_dapm_out_drv: 3828 case snd_soc_dapm_micbias: 3829 case snd_soc_dapm_line: 3830 case snd_soc_dapm_dai_link: 3831 case snd_soc_dapm_dai_out: 3832 case snd_soc_dapm_dai_in: 3833 w->power_check = dapm_generic_check_power; 3834 break; 3835 case snd_soc_dapm_supply: 3836 case snd_soc_dapm_regulator_supply: 3837 case snd_soc_dapm_pinctrl: 3838 case snd_soc_dapm_clock_supply: 3839 case snd_soc_dapm_kcontrol: 3840 w->is_supply = 1; 3841 w->power_check = dapm_supply_check_power; 3842 break; 3843 default: 3844 w->power_check = dapm_always_on_check_power; 3845 break; 3846 } 3847 3848 w->dapm = dapm; 3849 INIT_LIST_HEAD(&w->list); 3850 INIT_LIST_HEAD(&w->dirty); 3851 /* see for_each_card_widgets */ 3852 list_add_tail(&w->list, &dapm->card->widgets); 3853 3854 snd_soc_dapm_for_each_direction(dir) { 3855 INIT_LIST_HEAD(&w->edges[dir]); 3856 w->endpoints[dir] = -1; 3857 } 3858 3859 /* machine layer sets up unconnected pins and insertions */ 3860 w->connected = 1; 3861 return w; 3862 3863 request_failed: 3864 dev_err_probe(dapm->dev, ret, "ASoC: Failed to request %s\n", 3865 w->name); 3866 kfree_const(w->name); 3867 kfree_const(w->sname); 3868 kfree(w); 3869 cnew_failed: 3870 return ERR_PTR(ret); 3871 } 3872 3873 /** 3874 * snd_soc_dapm_new_control - create new dapm control 3875 * @dapm: DAPM context 3876 * @widget: widget template 3877 * 3878 * Creates new DAPM control based upon a template. 3879 * 3880 * Returns a widget pointer on success or an error pointer on failure 3881 */ 3882 struct snd_soc_dapm_widget * 3883 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, 3884 const struct snd_soc_dapm_widget *widget) 3885 { 3886 struct snd_soc_dapm_widget *w; 3887 3888 snd_soc_dapm_mutex_lock(dapm); 3889 w = snd_soc_dapm_new_control_unlocked(dapm, widget); 3890 snd_soc_dapm_mutex_unlock(dapm); 3891 3892 return w; 3893 } 3894 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control); 3895 3896 /** 3897 * snd_soc_dapm_new_controls - create new dapm controls 3898 * @dapm: DAPM context 3899 * @widget: widget array 3900 * @num: number of widgets 3901 * 3902 * Creates new DAPM controls based upon the templates. 3903 * 3904 * Returns 0 for success else error. 3905 */ 3906 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm, 3907 const struct snd_soc_dapm_widget *widget, 3908 unsigned int num) 3909 { 3910 int i; 3911 int ret = 0; 3912 3913 snd_soc_dapm_mutex_lock_root(dapm); 3914 for (i = 0; i < num; i++) { 3915 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget); 3916 if (IS_ERR(w)) { 3917 ret = PTR_ERR(w); 3918 break; 3919 } 3920 widget++; 3921 } 3922 snd_soc_dapm_mutex_unlock(dapm); 3923 return ret; 3924 } 3925 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls); 3926 3927 static int 3928 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w, 3929 struct snd_pcm_substream *substream) 3930 { 3931 struct snd_soc_dapm_path *path; 3932 struct snd_soc_dai *source, *sink; 3933 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 3934 const struct snd_soc_pcm_stream *config = NULL; 3935 struct snd_pcm_runtime *runtime = NULL; 3936 unsigned int fmt; 3937 int ret; 3938 3939 /* 3940 * NOTE 3941 * 3942 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is 3943 * starting to get a bit excessive for allocation on the stack, 3944 * especially when you're building with some of the KASAN type 3945 * stuff that increases stack usage. 3946 * So, we use kzalloc()/kfree() for params in this function. 3947 */ 3948 struct snd_pcm_hw_params *params __free(kfree) = kzalloc(sizeof(*params), 3949 GFP_KERNEL); 3950 if (!params) 3951 return -ENOMEM; 3952 3953 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 3954 if (!runtime) 3955 return -ENOMEM; 3956 3957 substream->runtime = runtime; 3958 3959 substream->stream = SNDRV_PCM_STREAM_CAPTURE; 3960 snd_soc_dapm_widget_for_each_source_path(w, path) { 3961 source = path->source->priv; 3962 3963 ret = snd_soc_dai_startup(source, substream); 3964 if (ret < 0) 3965 return ret; 3966 3967 snd_soc_dai_activate(source, substream->stream); 3968 } 3969 3970 substream->stream = SNDRV_PCM_STREAM_PLAYBACK; 3971 snd_soc_dapm_widget_for_each_sink_path(w, path) { 3972 sink = path->sink->priv; 3973 3974 ret = snd_soc_dai_startup(sink, substream); 3975 if (ret < 0) 3976 return ret; 3977 3978 snd_soc_dai_activate(sink, substream->stream); 3979 } 3980 3981 substream->hw_opened = 1; 3982 3983 /* 3984 * Note: getting the config after .startup() gives a chance to 3985 * either party on the link to alter the configuration if 3986 * necessary 3987 */ 3988 config = rtd->dai_link->c2c_params + rtd->c2c_params_select; 3989 if (!config) { 3990 dev_err(w->dapm->dev, "ASoC: link config missing\n"); 3991 return -EINVAL; 3992 } 3993 3994 /* Be a little careful as we don't want to overflow the mask array */ 3995 if (!config->formats) { 3996 dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n"); 3997 3998 return -EINVAL; 3999 } 4000 4001 fmt = ffs(config->formats) - 1; 4002 4003 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt); 4004 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min = 4005 config->rate_min; 4006 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max = 4007 config->rate_max; 4008 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min 4009 = config->channels_min; 4010 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max 4011 = config->channels_max; 4012 4013 substream->stream = SNDRV_PCM_STREAM_CAPTURE; 4014 snd_soc_dapm_widget_for_each_source_path(w, path) { 4015 source = path->source->priv; 4016 4017 ret = snd_soc_dai_hw_params(source, substream, params); 4018 if (ret < 0) 4019 return ret; 4020 4021 dapm_update_dai_unlocked(substream, params, source); 4022 } 4023 4024 substream->stream = SNDRV_PCM_STREAM_PLAYBACK; 4025 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4026 sink = path->sink->priv; 4027 4028 ret = snd_soc_dai_hw_params(sink, substream, params); 4029 if (ret < 0) 4030 return ret; 4031 4032 dapm_update_dai_unlocked(substream, params, sink); 4033 } 4034 4035 runtime->format = params_format(params); 4036 runtime->subformat = params_subformat(params); 4037 runtime->channels = params_channels(params); 4038 runtime->rate = params_rate(params); 4039 4040 return 0; 4041 } 4042 4043 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w, 4044 struct snd_kcontrol *kcontrol, int event) 4045 { 4046 struct snd_soc_dapm_path *path; 4047 struct snd_soc_dai *source, *sink; 4048 struct snd_pcm_substream *substream = w->priv; 4049 int ret = 0, saved_stream = substream->stream; 4050 4051 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) || 4052 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN]))) 4053 return -EINVAL; 4054 4055 switch (event) { 4056 case SND_SOC_DAPM_PRE_PMU: 4057 ret = snd_soc_dai_link_event_pre_pmu(w, substream); 4058 if (ret < 0) 4059 goto out; 4060 4061 break; 4062 4063 case SND_SOC_DAPM_POST_PMU: 4064 snd_soc_dapm_widget_for_each_source_path(w, path) { 4065 source = path->source->priv; 4066 4067 snd_soc_dai_prepare(source, substream); 4068 } 4069 4070 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4071 sink = path->sink->priv; 4072 4073 snd_soc_dai_prepare(sink, substream); 4074 } 4075 4076 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4077 sink = path->sink->priv; 4078 4079 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK); 4080 ret = 0; 4081 } 4082 break; 4083 4084 case SND_SOC_DAPM_PRE_PMD: 4085 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4086 sink = path->sink->priv; 4087 4088 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK); 4089 ret = 0; 4090 } 4091 4092 substream->stream = SNDRV_PCM_STREAM_CAPTURE; 4093 snd_soc_dapm_widget_for_each_source_path(w, path) { 4094 source = path->source->priv; 4095 snd_soc_dai_hw_free(source, substream, 0); 4096 } 4097 4098 substream->stream = SNDRV_PCM_STREAM_PLAYBACK; 4099 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4100 sink = path->sink->priv; 4101 snd_soc_dai_hw_free(sink, substream, 0); 4102 } 4103 4104 substream->stream = SNDRV_PCM_STREAM_CAPTURE; 4105 snd_soc_dapm_widget_for_each_source_path(w, path) { 4106 source = path->source->priv; 4107 snd_soc_dai_deactivate(source, substream->stream); 4108 snd_soc_dai_shutdown(source, substream, 0); 4109 } 4110 4111 substream->stream = SNDRV_PCM_STREAM_PLAYBACK; 4112 snd_soc_dapm_widget_for_each_sink_path(w, path) { 4113 sink = path->sink->priv; 4114 snd_soc_dai_deactivate(sink, substream->stream); 4115 snd_soc_dai_shutdown(sink, substream, 0); 4116 } 4117 break; 4118 4119 case SND_SOC_DAPM_POST_PMD: 4120 kfree(substream->runtime); 4121 substream->runtime = NULL; 4122 break; 4123 4124 default: 4125 WARN(1, "Unknown event %d\n", event); 4126 ret = -EINVAL; 4127 } 4128 4129 out: 4130 /* Restore the substream direction */ 4131 substream->stream = saved_stream; 4132 return ret; 4133 } 4134 4135 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol, 4136 struct snd_ctl_elem_value *ucontrol) 4137 { 4138 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol); 4139 struct snd_soc_pcm_runtime *rtd = w->priv; 4140 4141 ucontrol->value.enumerated.item[0] = rtd->c2c_params_select; 4142 4143 return 0; 4144 } 4145 4146 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol, 4147 struct snd_ctl_elem_value *ucontrol) 4148 { 4149 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol); 4150 struct snd_soc_pcm_runtime *rtd = w->priv; 4151 4152 /* Can't change the config when widget is already powered */ 4153 if (w->power) 4154 return -EBUSY; 4155 4156 if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select) 4157 return 0; 4158 4159 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params) 4160 return -EINVAL; 4161 4162 rtd->c2c_params_select = ucontrol->value.enumerated.item[0]; 4163 4164 return 1; 4165 } 4166 4167 static void 4168 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card, 4169 unsigned long *private_value, 4170 int num_c2c_params, 4171 const char **w_param_text) 4172 { 4173 int count; 4174 4175 devm_kfree(card->dev, (void *)*private_value); 4176 4177 if (!w_param_text) 4178 return; 4179 4180 for (count = 0 ; count < num_c2c_params; count++) 4181 devm_kfree(card->dev, (void *)w_param_text[count]); 4182 devm_kfree(card->dev, w_param_text); 4183 } 4184 4185 static struct snd_kcontrol_new * 4186 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card, 4187 char *link_name, 4188 const struct snd_soc_pcm_stream *c2c_params, 4189 int num_c2c_params, const char **w_param_text, 4190 unsigned long *private_value) 4191 { 4192 struct soc_enum w_param_enum[] = { 4193 SOC_ENUM_SINGLE(0, 0, 0, NULL), 4194 }; 4195 struct snd_kcontrol_new kcontrol_dai_link[] = { 4196 SOC_ENUM_EXT(NULL, w_param_enum[0], 4197 snd_soc_dapm_dai_link_get, 4198 snd_soc_dapm_dai_link_put), 4199 }; 4200 struct snd_kcontrol_new *kcontrol_news; 4201 const struct snd_soc_pcm_stream *config = c2c_params; 4202 int count; 4203 4204 for (count = 0 ; count < num_c2c_params; count++) { 4205 if (!config->stream_name) { 4206 dev_warn(card->dapm.dev, 4207 "ASoC: anonymous config %d for dai link %s\n", 4208 count, link_name); 4209 w_param_text[count] = 4210 devm_kasprintf(card->dev, GFP_KERNEL, 4211 "Anonymous Configuration %d", 4212 count); 4213 } else { 4214 w_param_text[count] = devm_kmemdup(card->dev, 4215 config->stream_name, 4216 strlen(config->stream_name) + 1, 4217 GFP_KERNEL); 4218 } 4219 if (!w_param_text[count]) 4220 goto outfree_w_param; 4221 config++; 4222 } 4223 4224 w_param_enum[0].items = num_c2c_params; 4225 w_param_enum[0].texts = w_param_text; 4226 4227 *private_value = 4228 (unsigned long) devm_kmemdup(card->dev, 4229 (void *)(kcontrol_dai_link[0].private_value), 4230 sizeof(struct soc_enum), GFP_KERNEL); 4231 if (!*private_value) { 4232 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n", 4233 link_name); 4234 goto outfree_w_param; 4235 } 4236 kcontrol_dai_link[0].private_value = *private_value; 4237 /* duplicate kcontrol_dai_link on heap so that memory persists */ 4238 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0], 4239 sizeof(struct snd_kcontrol_new), 4240 GFP_KERNEL); 4241 if (!kcontrol_news) { 4242 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n", 4243 link_name); 4244 goto outfree_w_param; 4245 } 4246 return kcontrol_news; 4247 4248 outfree_w_param: 4249 snd_soc_dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text); 4250 return NULL; 4251 } 4252 4253 static struct snd_soc_dapm_widget * 4254 snd_soc_dapm_new_dai(struct snd_soc_card *card, 4255 struct snd_pcm_substream *substream, 4256 char *id) 4257 { 4258 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 4259 struct snd_soc_dapm_widget template; 4260 struct snd_soc_dapm_widget *w; 4261 const struct snd_kcontrol_new *kcontrol_news; 4262 int num_kcontrols; 4263 const char **w_param_text; 4264 unsigned long private_value = 0; 4265 char *link_name; 4266 int ret = -ENOMEM; 4267 4268 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s", 4269 rtd->dai_link->name, id); 4270 if (!link_name) 4271 goto name_fail; 4272 4273 /* allocate memory for control, only in case of multiple configs */ 4274 w_param_text = NULL; 4275 kcontrol_news = NULL; 4276 num_kcontrols = 0; 4277 if (rtd->dai_link->num_c2c_params > 1) { 4278 w_param_text = devm_kcalloc(card->dev, 4279 rtd->dai_link->num_c2c_params, 4280 sizeof(char *), GFP_KERNEL); 4281 if (!w_param_text) 4282 goto param_fail; 4283 4284 num_kcontrols = 1; 4285 kcontrol_news = snd_soc_dapm_alloc_kcontrol(card, link_name, 4286 rtd->dai_link->c2c_params, 4287 rtd->dai_link->num_c2c_params, 4288 w_param_text, &private_value); 4289 if (!kcontrol_news) 4290 goto param_fail; 4291 } 4292 4293 memset(&template, 0, sizeof(template)); 4294 template.reg = SND_SOC_NOPM; 4295 template.id = snd_soc_dapm_dai_link; 4296 template.name = link_name; 4297 template.event = snd_soc_dai_link_event; 4298 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU | 4299 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD; 4300 template.kcontrol_news = kcontrol_news; 4301 template.num_kcontrols = num_kcontrols; 4302 4303 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name); 4304 4305 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template); 4306 if (IS_ERR(w)) { 4307 ret = PTR_ERR(w); 4308 goto outfree_kcontrol_news; 4309 } 4310 4311 w->priv = substream; 4312 4313 return w; 4314 4315 outfree_kcontrol_news: 4316 devm_kfree(card->dev, (void *)template.kcontrol_news); 4317 snd_soc_dapm_free_kcontrol(card, &private_value, 4318 rtd->dai_link->num_c2c_params, w_param_text); 4319 param_fail: 4320 devm_kfree(card->dev, link_name); 4321 name_fail: 4322 dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n", 4323 rtd->dai_link->name, id, ret); 4324 return ERR_PTR(ret); 4325 } 4326 4327 /** 4328 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets 4329 * @dapm: DAPM context 4330 * @dai: parent DAI 4331 * 4332 * Returns 0 on success, error code otherwise. 4333 */ 4334 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm, 4335 struct snd_soc_dai *dai) 4336 { 4337 struct snd_soc_dapm_widget template; 4338 struct snd_soc_dapm_widget *w; 4339 4340 WARN_ON(dapm->dev != dai->dev); 4341 4342 memset(&template, 0, sizeof(template)); 4343 template.reg = SND_SOC_NOPM; 4344 4345 if (dai->driver->playback.stream_name) { 4346 template.id = snd_soc_dapm_dai_in; 4347 template.name = dai->driver->playback.stream_name; 4348 template.sname = dai->driver->playback.stream_name; 4349 4350 dev_dbg(dai->dev, "ASoC: adding %s widget\n", 4351 template.name); 4352 4353 w = snd_soc_dapm_new_control_unlocked(dapm, &template); 4354 if (IS_ERR(w)) 4355 return PTR_ERR(w); 4356 4357 w->priv = dai; 4358 snd_soc_dai_set_widget_playback(dai, w); 4359 } 4360 4361 if (dai->driver->capture.stream_name) { 4362 template.id = snd_soc_dapm_dai_out; 4363 template.name = dai->driver->capture.stream_name; 4364 template.sname = dai->driver->capture.stream_name; 4365 4366 dev_dbg(dai->dev, "ASoC: adding %s widget\n", 4367 template.name); 4368 4369 w = snd_soc_dapm_new_control_unlocked(dapm, &template); 4370 if (IS_ERR(w)) 4371 return PTR_ERR(w); 4372 4373 w->priv = dai; 4374 snd_soc_dai_set_widget_capture(dai, w); 4375 } 4376 4377 return 0; 4378 } 4379 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets); 4380 4381 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card) 4382 { 4383 struct snd_soc_dapm_widget *dai_w, *w; 4384 struct snd_soc_dapm_widget *src, *sink; 4385 struct snd_soc_dai *dai; 4386 4387 /* For each DAI widget... */ 4388 for_each_card_widgets(card, dai_w) { 4389 switch (dai_w->id) { 4390 case snd_soc_dapm_dai_in: 4391 case snd_soc_dapm_dai_out: 4392 break; 4393 default: 4394 continue; 4395 } 4396 4397 /* let users know there is no DAI to link */ 4398 if (!dai_w->priv) { 4399 dev_dbg(card->dev, "dai widget %s has no DAI\n", 4400 dai_w->name); 4401 continue; 4402 } 4403 4404 dai = dai_w->priv; 4405 4406 /* ...find all widgets with the same stream and link them */ 4407 for_each_card_widgets(card, w) { 4408 if (w->dapm != dai_w->dapm) 4409 continue; 4410 4411 switch (w->id) { 4412 case snd_soc_dapm_dai_in: 4413 case snd_soc_dapm_dai_out: 4414 continue; 4415 default: 4416 break; 4417 } 4418 4419 if (!w->sname || !strstr(w->sname, dai_w->sname)) 4420 continue; 4421 4422 if (dai_w->id == snd_soc_dapm_dai_in) { 4423 src = dai_w; 4424 sink = w; 4425 } else { 4426 src = w; 4427 sink = dai_w; 4428 } 4429 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name); 4430 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL); 4431 } 4432 } 4433 4434 return 0; 4435 } 4436 4437 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm, 4438 struct snd_soc_dai *src_dai, 4439 struct snd_soc_dapm_widget *src, 4440 struct snd_soc_dapm_widget *dai, 4441 struct snd_soc_dai *sink_dai, 4442 struct snd_soc_dapm_widget *sink) 4443 { 4444 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n", 4445 src_dai->component->name, src->name, 4446 sink_dai->component->name, sink->name); 4447 4448 if (dai) { 4449 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL); 4450 src = dai; 4451 } 4452 4453 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL); 4454 } 4455 4456 static void dapm_connect_dai_pair(struct snd_soc_card *card, 4457 struct snd_soc_pcm_runtime *rtd, 4458 struct snd_soc_dai *codec_dai, 4459 struct snd_soc_dai *cpu_dai) 4460 { 4461 struct snd_soc_dai_link *dai_link = rtd->dai_link; 4462 struct snd_soc_dapm_widget *codec, *cpu; 4463 struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai }; 4464 struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai }; 4465 struct snd_soc_dapm_widget **src[] = { &cpu, &codec }; 4466 struct snd_soc_dapm_widget **sink[] = { &codec, &cpu }; 4467 char *widget_name[] = { "playback", "capture" }; 4468 int stream; 4469 4470 for_each_pcm_streams(stream) { 4471 int stream_cpu, stream_codec; 4472 4473 stream_cpu = snd_soc_get_stream_cpu(dai_link, stream); 4474 stream_codec = stream; 4475 4476 /* connect BE DAI playback if widgets are valid */ 4477 cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu); 4478 codec = snd_soc_dai_get_widget(codec_dai, stream_codec); 4479 4480 if (!cpu || !codec) 4481 continue; 4482 4483 /* special handling for [Codec2Codec] */ 4484 if (dai_link->c2c_params && !rtd->c2c_widget[stream]) { 4485 struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream; 4486 struct snd_soc_dapm_widget *dai = snd_soc_dapm_new_dai(card, substream, 4487 widget_name[stream]); 4488 4489 if (IS_ERR(dai)) 4490 continue; 4491 4492 rtd->c2c_widget[stream] = dai; 4493 } 4494 4495 dapm_connect_dai_routes(&card->dapm, src_dai[stream], *src[stream], 4496 rtd->c2c_widget[stream], 4497 sink_dai[stream], *sink[stream]); 4498 } 4499 } 4500 4501 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream, 4502 int event) 4503 { 4504 struct snd_soc_dapm_widget *w; 4505 4506 w = snd_soc_dai_get_widget(dai, stream); 4507 4508 if (w) { 4509 unsigned int ep; 4510 4511 dapm_mark_dirty(w, "stream event"); 4512 4513 if (w->id == snd_soc_dapm_dai_in) { 4514 ep = SND_SOC_DAPM_EP_SOURCE; 4515 dapm_widget_invalidate_input_paths(w); 4516 } else { 4517 ep = SND_SOC_DAPM_EP_SINK; 4518 dapm_widget_invalidate_output_paths(w); 4519 } 4520 4521 switch (event) { 4522 case SND_SOC_DAPM_STREAM_START: 4523 w->active = 1; 4524 w->is_ep = ep; 4525 break; 4526 case SND_SOC_DAPM_STREAM_STOP: 4527 w->active = 0; 4528 w->is_ep = 0; 4529 break; 4530 case SND_SOC_DAPM_STREAM_SUSPEND: 4531 case SND_SOC_DAPM_STREAM_RESUME: 4532 case SND_SOC_DAPM_STREAM_PAUSE_PUSH: 4533 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE: 4534 break; 4535 } 4536 } 4537 } 4538 4539 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card) 4540 { 4541 struct snd_soc_pcm_runtime *rtd; 4542 struct snd_soc_dai *cpu_dai; 4543 struct snd_soc_dai *codec_dai; 4544 4545 /* for each BE DAI link... */ 4546 for_each_card_rtds(card, rtd) { 4547 struct snd_soc_dai_link_ch_map *ch_maps; 4548 int i; 4549 4550 /* 4551 * dynamic FE links have no fixed DAI mapping. 4552 * CODEC<->CODEC links have no direct connection. 4553 */ 4554 if (rtd->dai_link->dynamic) 4555 continue; 4556 4557 /* 4558 * see 4559 * soc.h :: [dai_link->ch_maps Image sample] 4560 */ 4561 for_each_rtd_ch_maps(rtd, i, ch_maps) { 4562 cpu_dai = snd_soc_rtd_to_cpu(rtd, ch_maps->cpu); 4563 codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec); 4564 4565 dapm_connect_dai_pair(card, rtd, codec_dai, cpu_dai); 4566 } 4567 } 4568 } 4569 4570 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, 4571 int event) 4572 { 4573 struct snd_soc_dai *dai; 4574 int i; 4575 4576 for_each_rtd_dais(rtd, i, dai) 4577 soc_dapm_dai_stream_event(dai, stream, event); 4578 4579 dapm_power_widgets(rtd->card, event, NULL); 4580 } 4581 4582 /** 4583 * snd_soc_dapm_stream_event - send a stream event to the dapm core 4584 * @rtd: PCM runtime data 4585 * @stream: stream name 4586 * @event: stream event 4587 * 4588 * Sends a stream event to the dapm core. The core then makes any 4589 * necessary widget power changes. 4590 * 4591 * Returns 0 for success else error. 4592 */ 4593 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream, 4594 int event) 4595 { 4596 struct snd_soc_card *card = rtd->card; 4597 4598 snd_soc_dapm_mutex_lock(card); 4599 soc_dapm_stream_event(rtd, stream, event); 4600 snd_soc_dapm_mutex_unlock(card); 4601 } 4602 4603 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream) 4604 { 4605 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 4606 if (snd_soc_runtime_ignore_pmdown_time(rtd)) { 4607 /* powered down playback stream now */ 4608 snd_soc_dapm_stream_event(rtd, 4609 SNDRV_PCM_STREAM_PLAYBACK, 4610 SND_SOC_DAPM_STREAM_STOP); 4611 } else { 4612 /* start delayed pop wq here for playback streams */ 4613 rtd->pop_wait = 1; 4614 queue_delayed_work(system_power_efficient_wq, 4615 &rtd->delayed_work, 4616 msecs_to_jiffies(rtd->pmdown_time)); 4617 } 4618 } else { 4619 /* capture streams can be powered down now */ 4620 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, 4621 SND_SOC_DAPM_STREAM_STOP); 4622 } 4623 } 4624 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop); 4625 4626 /** 4627 * snd_soc_dapm_enable_pin_unlocked - enable pin. 4628 * @dapm: DAPM context 4629 * @pin: pin name 4630 * 4631 * Enables input/output pin and its parents or children widgets iff there is 4632 * a valid audio route and active audio stream. 4633 * 4634 * Requires external locking. 4635 * 4636 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4637 * do any widget power switching. 4638 */ 4639 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4640 const char *pin) 4641 { 4642 return snd_soc_dapm_set_pin(dapm, pin, 1); 4643 } 4644 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked); 4645 4646 /** 4647 * snd_soc_dapm_enable_pin - enable pin. 4648 * @dapm: DAPM context 4649 * @pin: pin name 4650 * 4651 * Enables input/output pin and its parents or children widgets iff there is 4652 * a valid audio route and active audio stream. 4653 * 4654 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4655 * do any widget power switching. 4656 */ 4657 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin) 4658 { 4659 int ret; 4660 4661 snd_soc_dapm_mutex_lock(dapm); 4662 4663 ret = snd_soc_dapm_set_pin(dapm, pin, 1); 4664 4665 snd_soc_dapm_mutex_unlock(dapm); 4666 4667 return ret; 4668 } 4669 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin); 4670 4671 /** 4672 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled 4673 * @dapm: DAPM context 4674 * @pin: pin name 4675 * 4676 * Enables input/output pin regardless of any other state. This is 4677 * intended for use with microphone bias supplies used in microphone 4678 * jack detection. 4679 * 4680 * Requires external locking. 4681 * 4682 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4683 * do any widget power switching. 4684 */ 4685 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4686 const char *pin) 4687 { 4688 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 4689 4690 if (!w) { 4691 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin); 4692 return -EINVAL; 4693 } 4694 4695 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin); 4696 if (!w->connected) { 4697 /* 4698 * w->force does not affect the number of input or output paths, 4699 * so we only have to recheck if w->connected is changed 4700 */ 4701 dapm_widget_invalidate_input_paths(w); 4702 dapm_widget_invalidate_output_paths(w); 4703 w->connected = 1; 4704 } 4705 w->force = 1; 4706 dapm_mark_dirty(w, "force enable"); 4707 4708 return 0; 4709 } 4710 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked); 4711 4712 /** 4713 * snd_soc_dapm_force_enable_pin - force a pin to be enabled 4714 * @dapm: DAPM context 4715 * @pin: pin name 4716 * 4717 * Enables input/output pin regardless of any other state. This is 4718 * intended for use with microphone bias supplies used in microphone 4719 * jack detection. 4720 * 4721 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4722 * do any widget power switching. 4723 */ 4724 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm, 4725 const char *pin) 4726 { 4727 int ret; 4728 4729 snd_soc_dapm_mutex_lock(dapm); 4730 4731 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin); 4732 4733 snd_soc_dapm_mutex_unlock(dapm); 4734 4735 return ret; 4736 } 4737 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin); 4738 4739 /** 4740 * snd_soc_dapm_disable_pin_unlocked - disable pin. 4741 * @dapm: DAPM context 4742 * @pin: pin name 4743 * 4744 * Disables input/output pin and its parents or children widgets. 4745 * 4746 * Requires external locking. 4747 * 4748 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4749 * do any widget power switching. 4750 */ 4751 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm, 4752 const char *pin) 4753 { 4754 return snd_soc_dapm_set_pin(dapm, pin, 0); 4755 } 4756 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked); 4757 4758 /** 4759 * snd_soc_dapm_disable_pin - disable pin. 4760 * @dapm: DAPM context 4761 * @pin: pin name 4762 * 4763 * Disables input/output pin and its parents or children widgets. 4764 * 4765 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4766 * do any widget power switching. 4767 */ 4768 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm, 4769 const char *pin) 4770 { 4771 int ret; 4772 4773 snd_soc_dapm_mutex_lock(dapm); 4774 4775 ret = snd_soc_dapm_set_pin(dapm, pin, 0); 4776 4777 snd_soc_dapm_mutex_unlock(dapm); 4778 4779 return ret; 4780 } 4781 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin); 4782 4783 /** 4784 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin. 4785 * @dapm: DAPM context 4786 * @pin: pin name 4787 * 4788 * Marks the specified pin as being not connected, disabling it along 4789 * any parent or child widgets. At present this is identical to 4790 * snd_soc_dapm_disable_pin() but in future it will be extended to do 4791 * additional things such as disabling controls which only affect 4792 * paths through the pin. 4793 * 4794 * Requires external locking. 4795 * 4796 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4797 * do any widget power switching. 4798 */ 4799 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm, 4800 const char *pin) 4801 { 4802 return snd_soc_dapm_set_pin(dapm, pin, 0); 4803 } 4804 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked); 4805 4806 /** 4807 * snd_soc_dapm_nc_pin - permanently disable pin. 4808 * @dapm: DAPM context 4809 * @pin: pin name 4810 * 4811 * Marks the specified pin as being not connected, disabling it along 4812 * any parent or child widgets. At present this is identical to 4813 * snd_soc_dapm_disable_pin() but in future it will be extended to do 4814 * additional things such as disabling controls which only affect 4815 * paths through the pin. 4816 * 4817 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to 4818 * do any widget power switching. 4819 */ 4820 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin) 4821 { 4822 int ret; 4823 4824 snd_soc_dapm_mutex_lock(dapm); 4825 4826 ret = snd_soc_dapm_set_pin(dapm, pin, 0); 4827 4828 snd_soc_dapm_mutex_unlock(dapm); 4829 4830 return ret; 4831 } 4832 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin); 4833 4834 /** 4835 * snd_soc_dapm_get_pin_status - get audio pin status 4836 * @dapm: DAPM context 4837 * @pin: audio signal pin endpoint (or start point) 4838 * 4839 * Get audio pin status - connected or disconnected. 4840 * 4841 * Returns 1 for connected otherwise 0. 4842 */ 4843 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm, 4844 const char *pin) 4845 { 4846 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true); 4847 4848 if (w) 4849 return w->connected; 4850 4851 return 0; 4852 } 4853 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status); 4854 4855 /** 4856 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint 4857 * @dapm: DAPM context 4858 * @pin: audio signal pin endpoint (or start point) 4859 * 4860 * Mark the given endpoint or pin as ignoring suspend. When the 4861 * system is disabled a path between two endpoints flagged as ignoring 4862 * suspend will not be disabled. The path must already be enabled via 4863 * normal means at suspend time, it will not be turned on if it was not 4864 * already enabled. 4865 */ 4866 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm, 4867 const char *pin) 4868 { 4869 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false); 4870 4871 if (!w) { 4872 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin); 4873 return -EINVAL; 4874 } 4875 4876 w->ignore_suspend = 1; 4877 4878 return 0; 4879 } 4880 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend); 4881 4882 /** 4883 * snd_soc_dapm_free - free dapm resources 4884 * @dapm: DAPM context 4885 * 4886 * Free all dapm widgets and resources. 4887 */ 4888 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm) 4889 { 4890 dapm_debugfs_cleanup(dapm); 4891 dapm_free_widgets(dapm); 4892 list_del(&dapm->list); 4893 } 4894 EXPORT_SYMBOL_GPL(snd_soc_dapm_free); 4895 4896 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm, 4897 struct snd_soc_card *card, 4898 struct snd_soc_component *component) 4899 { 4900 dapm->card = card; 4901 dapm->component = component; 4902 dapm->bias_level = SND_SOC_BIAS_OFF; 4903 4904 if (component) { 4905 dapm->dev = component->dev; 4906 dapm->idle_bias_off = !component->driver->idle_bias_on; 4907 dapm->suspend_bias_off = component->driver->suspend_bias_off; 4908 } else { 4909 dapm->dev = card->dev; 4910 } 4911 4912 INIT_LIST_HEAD(&dapm->list); 4913 /* see for_each_card_dapms */ 4914 list_add(&dapm->list, &card->dapm_list); 4915 } 4916 4917 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm) 4918 { 4919 struct snd_soc_card *card = dapm->card; 4920 struct snd_soc_dapm_widget *w; 4921 LIST_HEAD(down_list); 4922 int powerdown = 0; 4923 4924 snd_soc_dapm_mutex_lock_root(card); 4925 4926 for_each_card_widgets(dapm->card, w) { 4927 if (w->dapm != dapm) 4928 continue; 4929 if (w->power) { 4930 dapm_seq_insert(w, &down_list, false); 4931 w->new_power = 0; 4932 powerdown = 1; 4933 } 4934 } 4935 4936 /* If there were no widgets to power down we're already in 4937 * standby. 4938 */ 4939 if (powerdown) { 4940 if (dapm->bias_level == SND_SOC_BIAS_ON) 4941 snd_soc_dapm_set_bias_level(dapm, 4942 SND_SOC_BIAS_PREPARE); 4943 dapm_seq_run(card, &down_list, 0, false); 4944 if (dapm->bias_level == SND_SOC_BIAS_PREPARE) 4945 snd_soc_dapm_set_bias_level(dapm, 4946 SND_SOC_BIAS_STANDBY); 4947 } 4948 4949 snd_soc_dapm_mutex_unlock(card); 4950 } 4951 4952 /* 4953 * snd_soc_dapm_shutdown - callback for system shutdown 4954 */ 4955 void snd_soc_dapm_shutdown(struct snd_soc_card *card) 4956 { 4957 struct snd_soc_dapm_context *dapm; 4958 4959 for_each_card_dapms(card, dapm) { 4960 if (dapm != &card->dapm) { 4961 soc_dapm_shutdown_dapm(dapm); 4962 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) 4963 snd_soc_dapm_set_bias_level(dapm, 4964 SND_SOC_BIAS_OFF); 4965 } 4966 } 4967 4968 soc_dapm_shutdown_dapm(&card->dapm); 4969 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) 4970 snd_soc_dapm_set_bias_level(&card->dapm, 4971 SND_SOC_BIAS_OFF); 4972 } 4973 4974 /* Module information */ 4975 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); 4976 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC"); 4977 MODULE_LICENSE("GPL"); 4978