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