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