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