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