2 * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
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.
13 * o Changes power status of internal codec blocks depending on the
14 * dynamic configuration of codec internal audio paths and active
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
21 * o Delayed power down of audio subsystem to reduce pops between a quick
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>
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>
46 #include <trace/events/asoc.h>
48 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
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_in] = 3,
59 [snd_soc_dapm_dai_out] = 3,
60 [snd_soc_dapm_aif_in] = 3,
61 [snd_soc_dapm_aif_out] = 3,
62 [snd_soc_dapm_mic] = 4,
63 [snd_soc_dapm_mux] = 5,
64 [snd_soc_dapm_virt_mux] = 5,
65 [snd_soc_dapm_value_mux] = 5,
66 [snd_soc_dapm_dac] = 6,
67 [snd_soc_dapm_mixer] = 7,
68 [snd_soc_dapm_mixer_named_ctl] = 7,
69 [snd_soc_dapm_pga] = 8,
70 [snd_soc_dapm_adc] = 9,
71 [snd_soc_dapm_out_drv] = 10,
72 [snd_soc_dapm_hp] = 10,
73 [snd_soc_dapm_spk] = 10,
74 [snd_soc_dapm_line] = 10,
75 [snd_soc_dapm_post] = 11,
78 static int dapm_down_seq[] = {
79 [snd_soc_dapm_pre] = 0,
80 [snd_soc_dapm_adc] = 1,
81 [snd_soc_dapm_hp] = 2,
82 [snd_soc_dapm_spk] = 2,
83 [snd_soc_dapm_line] = 2,
84 [snd_soc_dapm_out_drv] = 2,
85 [snd_soc_dapm_pga] = 4,
86 [snd_soc_dapm_mixer_named_ctl] = 5,
87 [snd_soc_dapm_mixer] = 5,
88 [snd_soc_dapm_dac] = 6,
89 [snd_soc_dapm_mic] = 7,
90 [snd_soc_dapm_micbias] = 8,
91 [snd_soc_dapm_mux] = 9,
92 [snd_soc_dapm_virt_mux] = 9,
93 [snd_soc_dapm_value_mux] = 9,
94 [snd_soc_dapm_aif_in] = 10,
95 [snd_soc_dapm_aif_out] = 10,
96 [snd_soc_dapm_dai_in] = 10,
97 [snd_soc_dapm_dai_out] = 10,
98 [snd_soc_dapm_dai_link] = 11,
99 [snd_soc_dapm_clock_supply] = 12,
100 [snd_soc_dapm_regulator_supply] = 12,
101 [snd_soc_dapm_supply] = 12,
102 [snd_soc_dapm_post] = 13,
105 static void pop_wait(u32 pop_time)
108 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
111 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
119 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
124 vsnprintf(buf, PAGE_SIZE, fmt, args);
125 dev_info(dev, "%s", buf);
131 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
133 return !list_empty(&w->dirty);
136 void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
138 if (!dapm_dirty_widget(w)) {
139 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
141 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
144 EXPORT_SYMBOL_GPL(dapm_mark_dirty);
146 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
148 struct snd_soc_card *card = dapm->card;
149 struct snd_soc_dapm_widget *w;
151 mutex_lock(&card->dapm_mutex);
153 list_for_each_entry(w, &card->widgets, list) {
155 case snd_soc_dapm_input:
156 case snd_soc_dapm_output:
157 dapm_mark_dirty(w, "Rechecking inputs and outputs");
164 mutex_unlock(&card->dapm_mutex);
166 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
168 /* create a new dapm widget */
169 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
170 const struct snd_soc_dapm_widget *_widget)
172 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
175 /* get snd_card from DAPM context */
176 static inline struct snd_card *dapm_get_snd_card(
177 struct snd_soc_dapm_context *dapm)
180 return dapm->codec->card->snd_card;
181 else if (dapm->platform)
182 return dapm->platform->card->snd_card;
190 /* get soc_card from DAPM context */
191 static inline struct snd_soc_card *dapm_get_soc_card(
192 struct snd_soc_dapm_context *dapm)
195 return dapm->codec->card;
196 else if (dapm->platform)
197 return dapm->platform->card;
205 static void dapm_reset(struct snd_soc_card *card)
207 struct snd_soc_dapm_widget *w;
209 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
211 list_for_each_entry(w, &card->widgets, list) {
212 w->power_checked = false;
218 static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
221 return snd_soc_read(w->codec, reg);
222 else if (w->platform)
223 return snd_soc_platform_read(w->platform, reg);
225 dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
229 static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
232 return snd_soc_write(w->codec, reg, val);
233 else if (w->platform)
234 return snd_soc_platform_write(w->platform, reg, val);
236 dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
240 static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
242 if (w->codec && !w->codec->using_regmap)
243 mutex_lock(&w->codec->mutex);
244 else if (w->platform)
245 mutex_lock(&w->platform->mutex);
248 static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
250 if (w->codec && !w->codec->using_regmap)
251 mutex_unlock(&w->codec->mutex);
252 else if (w->platform)
253 mutex_unlock(&w->platform->mutex);
256 static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
257 unsigned short reg, unsigned int mask, unsigned int value)
260 unsigned int old, new;
263 if (w->codec && w->codec->using_regmap) {
264 ret = regmap_update_bits_check(w->codec->control_data,
265 reg, mask, value, &change);
270 ret = soc_widget_read(w, reg);
272 soc_widget_unlock(w);
277 new = (old & ~mask) | (value & mask);
280 ret = soc_widget_write(w, reg, new);
282 soc_widget_unlock(w);
286 soc_widget_unlock(w);
293 * snd_soc_dapm_set_bias_level - set the bias level for the system
294 * @dapm: DAPM context
295 * @level: level to configure
297 * Configure the bias (power) levels for the SoC audio device.
299 * Returns 0 for success else error.
301 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
302 enum snd_soc_bias_level level)
304 struct snd_soc_card *card = dapm->card;
307 trace_snd_soc_bias_level_start(card, level);
309 if (card && card->set_bias_level)
310 ret = card->set_bias_level(card, dapm, level);
315 if (dapm->codec->driver->set_bias_level)
316 ret = dapm->codec->driver->set_bias_level(dapm->codec,
319 dapm->bias_level = level;
320 } else if (!card || dapm != &card->dapm) {
321 dapm->bias_level = level;
327 if (card && card->set_bias_level_post)
328 ret = card->set_bias_level_post(card, dapm, level);
330 trace_snd_soc_bias_level_done(card, level);
335 /* set up initial codec paths */
336 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
337 struct snd_soc_dapm_path *p, int i)
340 case snd_soc_dapm_switch:
341 case snd_soc_dapm_mixer:
342 case snd_soc_dapm_mixer_named_ctl: {
344 struct soc_mixer_control *mc = (struct soc_mixer_control *)
345 w->kcontrol_news[i].private_value;
346 unsigned int reg = mc->reg;
347 unsigned int shift = mc->shift;
349 unsigned int mask = (1 << fls(max)) - 1;
350 unsigned int invert = mc->invert;
352 val = soc_widget_read(w, reg);
353 val = (val >> shift) & mask;
360 case snd_soc_dapm_mux: {
361 struct soc_enum *e = (struct soc_enum *)
362 w->kcontrol_news[i].private_value;
365 val = soc_widget_read(w, e->reg);
366 item = (val >> e->shift_l) & e->mask;
369 for (i = 0; i < e->max; i++) {
370 if (!(strcmp(p->name, e->texts[i])) && item == i)
375 case snd_soc_dapm_virt_mux: {
376 struct soc_enum *e = (struct soc_enum *)
377 w->kcontrol_news[i].private_value;
380 /* since a virtual mux has no backing registers to
381 * decide which path to connect, it will try to match
382 * with the first enumeration. This is to ensure
383 * that the default mux choice (the first) will be
384 * correctly powered up during initialization.
386 if (!strcmp(p->name, e->texts[0]))
390 case snd_soc_dapm_value_mux: {
391 struct soc_enum *e = (struct soc_enum *)
392 w->kcontrol_news[i].private_value;
395 val = soc_widget_read(w, e->reg);
396 val = (val >> e->shift_l) & e->mask;
397 for (item = 0; item < e->max; item++) {
398 if (val == e->values[item])
403 for (i = 0; i < e->max; i++) {
404 if (!(strcmp(p->name, e->texts[i])) && item == i)
409 /* does not affect routing - always connected */
410 case snd_soc_dapm_pga:
411 case snd_soc_dapm_out_drv:
412 case snd_soc_dapm_output:
413 case snd_soc_dapm_adc:
414 case snd_soc_dapm_input:
415 case snd_soc_dapm_siggen:
416 case snd_soc_dapm_dac:
417 case snd_soc_dapm_micbias:
418 case snd_soc_dapm_vmid:
419 case snd_soc_dapm_supply:
420 case snd_soc_dapm_regulator_supply:
421 case snd_soc_dapm_clock_supply:
422 case snd_soc_dapm_aif_in:
423 case snd_soc_dapm_aif_out:
424 case snd_soc_dapm_dai_in:
425 case snd_soc_dapm_dai_out:
426 case snd_soc_dapm_hp:
427 case snd_soc_dapm_mic:
428 case snd_soc_dapm_spk:
429 case snd_soc_dapm_line:
430 case snd_soc_dapm_dai_link:
433 /* does affect routing - dynamically connected */
434 case snd_soc_dapm_pre:
435 case snd_soc_dapm_post:
441 /* connect mux widget to its interconnecting audio paths */
442 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
443 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
444 struct snd_soc_dapm_path *path, const char *control_name,
445 const struct snd_kcontrol_new *kcontrol)
447 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
450 for (i = 0; i < e->max; i++) {
451 if (!(strcmp(control_name, e->texts[i]))) {
452 list_add(&path->list, &dapm->card->paths);
453 list_add(&path->list_sink, &dest->sources);
454 list_add(&path->list_source, &src->sinks);
455 path->name = (char*)e->texts[i];
456 dapm_set_path_status(dest, path, 0);
464 /* connect mixer widget to its interconnecting audio paths */
465 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
466 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
467 struct snd_soc_dapm_path *path, const char *control_name)
471 /* search for mixer kcontrol */
472 for (i = 0; i < dest->num_kcontrols; i++) {
473 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
474 list_add(&path->list, &dapm->card->paths);
475 list_add(&path->list_sink, &dest->sources);
476 list_add(&path->list_source, &src->sinks);
477 path->name = dest->kcontrol_news[i].name;
478 dapm_set_path_status(dest, path, i);
485 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
486 struct snd_soc_dapm_widget *kcontrolw,
487 const struct snd_kcontrol_new *kcontrol_new,
488 struct snd_kcontrol **kcontrol)
490 struct snd_soc_dapm_widget *w;
495 list_for_each_entry(w, &dapm->card->widgets, list) {
496 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
498 for (i = 0; i < w->num_kcontrols; i++) {
499 if (&w->kcontrol_news[i] == kcontrol_new) {
501 *kcontrol = w->kcontrols[i];
511 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
512 * create it. Either way, add the widget into the control's widget list
514 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
515 int kci, struct snd_soc_dapm_path *path)
517 struct snd_soc_dapm_context *dapm = w->dapm;
518 struct snd_card *card = dapm->card->snd_card;
522 struct snd_kcontrol *kcontrol;
523 struct snd_soc_dapm_widget_list *wlist;
526 bool wname_in_long_name, kcname_in_long_name;
533 prefix = dapm->codec->name_prefix;
538 prefix_len = strlen(prefix) + 1;
542 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
546 wlist = kcontrol->private_data;
547 wlistentries = wlist->num_widgets + 1;
553 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
554 wlistentries * sizeof(struct snd_soc_dapm_widget *);
555 wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
557 dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
561 wlist->num_widgets = wlistentries;
562 wlist->widgets[wlistentries - 1] = w;
566 wname_in_long_name = false;
567 kcname_in_long_name = true;
570 case snd_soc_dapm_switch:
571 case snd_soc_dapm_mixer:
572 wname_in_long_name = true;
573 kcname_in_long_name = true;
575 case snd_soc_dapm_mixer_named_ctl:
576 wname_in_long_name = false;
577 kcname_in_long_name = true;
579 case snd_soc_dapm_mux:
580 case snd_soc_dapm_virt_mux:
581 case snd_soc_dapm_value_mux:
582 wname_in_long_name = true;
583 kcname_in_long_name = false;
591 if (wname_in_long_name && kcname_in_long_name) {
592 name_len = strlen(w->name) - prefix_len + 1 +
593 strlen(w->kcontrol_news[kci].name) + 1;
595 long_name = kmalloc(name_len, GFP_KERNEL);
596 if (long_name == NULL) {
602 * The control will get a prefix from the control
603 * creation process but we're also using the same
604 * prefix for widgets so cut the prefix off the
605 * front of the widget name.
607 snprintf(long_name, name_len, "%s %s",
608 w->name + prefix_len,
609 w->kcontrol_news[kci].name);
610 long_name[name_len - 1] = '\0';
613 } else if (wname_in_long_name) {
615 name = w->name + prefix_len;
618 name = w->kcontrol_news[kci].name;
621 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
623 ret = snd_ctl_add(card, kcontrol);
626 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
633 path->long_name = long_name;
636 kcontrol->private_data = wlist;
637 w->kcontrols[kci] = kcontrol;
638 path->kcontrol = kcontrol;
643 /* create new dapm mixer control */
644 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
647 struct snd_soc_dapm_path *path;
650 for (i = 0; i < w->num_kcontrols; i++) {
652 list_for_each_entry(path, &w->sources, list_sink) {
653 /* mixer/mux paths name must match control name */
654 if (path->name != (char *)w->kcontrol_news[i].name)
657 if (w->kcontrols[i]) {
658 path->kcontrol = w->kcontrols[i];
662 ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
671 /* create new dapm mux control */
672 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
674 struct snd_soc_dapm_context *dapm = w->dapm;
675 struct snd_soc_dapm_path *path;
678 if (w->num_kcontrols != 1) {
680 "ASoC: mux %s has incorrect number of controls\n",
685 if (list_empty(&w->sources)) {
686 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
690 path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
693 ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
697 list_for_each_entry(path, &w->sources, list_sink)
698 path->kcontrol = w->kcontrols[0];
703 /* create new dapm volume control */
704 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
706 if (w->num_kcontrols)
707 dev_err(w->dapm->dev,
708 "ASoC: PGA controls not supported: '%s'\n", w->name);
713 /* reset 'walked' bit for each dapm path */
714 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
715 struct list_head *sink)
717 struct snd_soc_dapm_path *p;
719 list_for_each_entry(p, sink, list_source) {
722 dapm_clear_walk_output(dapm, &p->sink->sinks);
727 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
728 struct list_head *source)
730 struct snd_soc_dapm_path *p;
732 list_for_each_entry(p, source, list_sink) {
735 dapm_clear_walk_input(dapm, &p->source->sources);
741 /* We implement power down on suspend by checking the power state of
742 * the ALSA card - when we are suspending the ALSA state for the card
745 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
747 int level = snd_power_get_state(widget->dapm->card->snd_card);
750 case SNDRV_CTL_POWER_D3hot:
751 case SNDRV_CTL_POWER_D3cold:
752 if (widget->ignore_suspend)
753 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
755 return widget->ignore_suspend;
761 /* add widget to list if it's not already in the list */
762 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
763 struct snd_soc_dapm_widget *w)
765 struct snd_soc_dapm_widget_list *wlist;
766 int wlistsize, wlistentries, i;
773 /* is this widget already in the list */
774 for (i = 0; i < wlist->num_widgets; i++) {
775 if (wlist->widgets[i] == w)
779 /* allocate some new space */
780 wlistentries = wlist->num_widgets + 1;
781 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
782 wlistentries * sizeof(struct snd_soc_dapm_widget *);
783 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
785 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
791 /* insert the widget */
792 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
793 w->name, wlist->num_widgets);
795 wlist->widgets[wlist->num_widgets] = w;
796 wlist->num_widgets++;
801 * Recursively check for a completed path to an active or physically connected
802 * output widget. Returns number of complete paths.
804 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
805 struct snd_soc_dapm_widget_list **list)
807 struct snd_soc_dapm_path *path;
810 if (widget->outputs >= 0)
811 return widget->outputs;
813 DAPM_UPDATE_STAT(widget, path_checks);
815 switch (widget->id) {
816 case snd_soc_dapm_supply:
817 case snd_soc_dapm_regulator_supply:
818 case snd_soc_dapm_clock_supply:
824 switch (widget->id) {
825 case snd_soc_dapm_adc:
826 case snd_soc_dapm_aif_out:
827 case snd_soc_dapm_dai_out:
828 if (widget->active) {
829 widget->outputs = snd_soc_dapm_suspend_check(widget);
830 return widget->outputs;
836 if (widget->connected) {
837 /* connected pin ? */
838 if (widget->id == snd_soc_dapm_output && !widget->ext) {
839 widget->outputs = snd_soc_dapm_suspend_check(widget);
840 return widget->outputs;
843 /* connected jack or spk ? */
844 if (widget->id == snd_soc_dapm_hp ||
845 widget->id == snd_soc_dapm_spk ||
846 (widget->id == snd_soc_dapm_line &&
847 !list_empty(&widget->sources))) {
848 widget->outputs = snd_soc_dapm_suspend_check(widget);
849 return widget->outputs;
853 list_for_each_entry(path, &widget->sinks, list_source) {
854 DAPM_UPDATE_STAT(widget, neighbour_checks);
865 trace_snd_soc_dapm_output_path(widget, path);
867 if (path->sink && path->connect) {
871 /* do we need to add this widget to the list ? */
874 err = dapm_list_add_widget(list, path->sink);
876 dev_err(widget->dapm->dev,
877 "ASoC: could not add widget %s\n",
884 con += is_connected_output_ep(path->sink, list);
890 widget->outputs = con;
896 * Recursively check for a completed path to an active or physically connected
897 * input widget. Returns number of complete paths.
899 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
900 struct snd_soc_dapm_widget_list **list)
902 struct snd_soc_dapm_path *path;
905 if (widget->inputs >= 0)
906 return widget->inputs;
908 DAPM_UPDATE_STAT(widget, path_checks);
910 switch (widget->id) {
911 case snd_soc_dapm_supply:
912 case snd_soc_dapm_regulator_supply:
913 case snd_soc_dapm_clock_supply:
919 /* active stream ? */
920 switch (widget->id) {
921 case snd_soc_dapm_dac:
922 case snd_soc_dapm_aif_in:
923 case snd_soc_dapm_dai_in:
924 if (widget->active) {
925 widget->inputs = snd_soc_dapm_suspend_check(widget);
926 return widget->inputs;
932 if (widget->connected) {
933 /* connected pin ? */
934 if (widget->id == snd_soc_dapm_input && !widget->ext) {
935 widget->inputs = snd_soc_dapm_suspend_check(widget);
936 return widget->inputs;
939 /* connected VMID/Bias for lower pops */
940 if (widget->id == snd_soc_dapm_vmid) {
941 widget->inputs = snd_soc_dapm_suspend_check(widget);
942 return widget->inputs;
945 /* connected jack ? */
946 if (widget->id == snd_soc_dapm_mic ||
947 (widget->id == snd_soc_dapm_line &&
948 !list_empty(&widget->sinks))) {
949 widget->inputs = snd_soc_dapm_suspend_check(widget);
950 return widget->inputs;
953 /* signal generator */
954 if (widget->id == snd_soc_dapm_siggen) {
955 widget->inputs = snd_soc_dapm_suspend_check(widget);
956 return widget->inputs;
960 list_for_each_entry(path, &widget->sources, list_sink) {
961 DAPM_UPDATE_STAT(widget, neighbour_checks);
972 trace_snd_soc_dapm_input_path(widget, path);
974 if (path->source && path->connect) {
978 /* do we need to add this widget to the list ? */
981 err = dapm_list_add_widget(list, path->source);
983 dev_err(widget->dapm->dev,
984 "ASoC: could not add widget %s\n",
991 con += is_connected_input_ep(path->source, list);
997 widget->inputs = con;
1003 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1004 * @dai: the soc DAI.
1005 * @stream: stream direction.
1006 * @list: list of active widgets for this stream.
1008 * Queries DAPM graph as to whether an valid audio stream path exists for
1009 * the initial stream specified by name. This takes into account
1010 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1012 * Returns the number of valid paths or negative error.
1014 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1015 struct snd_soc_dapm_widget_list **list)
1017 struct snd_soc_card *card = dai->card;
1020 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1023 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1024 paths = is_connected_output_ep(dai->playback_widget, list);
1025 dapm_clear_walk_output(&card->dapm,
1026 &dai->playback_widget->sinks);
1028 paths = is_connected_input_ep(dai->capture_widget, list);
1029 dapm_clear_walk_input(&card->dapm,
1030 &dai->capture_widget->sources);
1033 trace_snd_soc_dapm_connected(paths, stream);
1034 mutex_unlock(&card->dapm_mutex);
1040 * Handler for generic register modifier widget.
1042 int dapm_reg_event(struct snd_soc_dapm_widget *w,
1043 struct snd_kcontrol *kcontrol, int event)
1047 if (SND_SOC_DAPM_EVENT_ON(event))
1052 soc_widget_update_bits_locked(w, -(w->reg + 1),
1053 w->mask << w->shift, val << w->shift);
1057 EXPORT_SYMBOL_GPL(dapm_reg_event);
1060 * Handler for regulator supply widget.
1062 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1063 struct snd_kcontrol *kcontrol, int event)
1067 if (SND_SOC_DAPM_EVENT_ON(event)) {
1068 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1069 ret = regulator_allow_bypass(w->regulator, false);
1071 dev_warn(w->dapm->dev,
1072 "ASoC: Failed to bypass %s: %d\n",
1076 return regulator_enable(w->regulator);
1078 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1079 ret = regulator_allow_bypass(w->regulator, true);
1081 dev_warn(w->dapm->dev,
1082 "ASoC: Failed to unbypass %s: %d\n",
1086 return regulator_disable_deferred(w->regulator, w->shift);
1089 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1092 * Handler for clock supply widget.
1094 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1095 struct snd_kcontrol *kcontrol, int event)
1100 #ifdef CONFIG_HAVE_CLK
1101 if (SND_SOC_DAPM_EVENT_ON(event)) {
1102 return clk_prepare_enable(w->clk);
1104 clk_disable_unprepare(w->clk);
1110 EXPORT_SYMBOL_GPL(dapm_clock_event);
1112 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1114 if (w->power_checked)
1115 return w->new_power;
1120 w->new_power = w->power_check(w);
1122 w->power_checked = true;
1124 return w->new_power;
1127 /* Generic check to see if a widget should be powered.
1129 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1133 DAPM_UPDATE_STAT(w, power_checks);
1135 in = is_connected_input_ep(w, NULL);
1136 dapm_clear_walk_input(w->dapm, &w->sources);
1137 out = is_connected_output_ep(w, NULL);
1138 dapm_clear_walk_output(w->dapm, &w->sinks);
1139 return out != 0 && in != 0;
1142 /* Check to see if an ADC has power */
1143 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1147 DAPM_UPDATE_STAT(w, power_checks);
1150 in = is_connected_input_ep(w, NULL);
1151 dapm_clear_walk_input(w->dapm, &w->sources);
1154 return dapm_generic_check_power(w);
1158 /* Check to see if a DAC has power */
1159 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1163 DAPM_UPDATE_STAT(w, power_checks);
1166 out = is_connected_output_ep(w, NULL);
1167 dapm_clear_walk_output(w->dapm, &w->sinks);
1170 return dapm_generic_check_power(w);
1174 /* Check to see if a power supply is needed */
1175 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1177 struct snd_soc_dapm_path *path;
1179 DAPM_UPDATE_STAT(w, power_checks);
1181 /* Check if one of our outputs is connected */
1182 list_for_each_entry(path, &w->sinks, list_source) {
1183 DAPM_UPDATE_STAT(w, neighbour_checks);
1188 if (path->connected &&
1189 !path->connected(path->source, path->sink))
1195 if (dapm_widget_power_check(path->sink))
1202 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1207 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1208 struct snd_soc_dapm_widget *b,
1216 sort = dapm_down_seq;
1218 if (sort[a->id] != sort[b->id])
1219 return sort[a->id] - sort[b->id];
1220 if (a->subseq != b->subseq) {
1222 return a->subseq - b->subseq;
1224 return b->subseq - a->subseq;
1226 if (a->reg != b->reg)
1227 return a->reg - b->reg;
1228 if (a->dapm != b->dapm)
1229 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1234 /* Insert a widget in order into a DAPM power sequence. */
1235 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1236 struct list_head *list,
1239 struct snd_soc_dapm_widget *w;
1241 list_for_each_entry(w, list, power_list)
1242 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1243 list_add_tail(&new_widget->power_list, &w->power_list);
1247 list_add_tail(&new_widget->power_list, list);
1250 static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1251 struct snd_soc_dapm_widget *w, int event)
1253 struct snd_soc_card *card = dapm->card;
1254 const char *ev_name;
1258 case SND_SOC_DAPM_PRE_PMU:
1259 ev_name = "PRE_PMU";
1262 case SND_SOC_DAPM_POST_PMU:
1263 ev_name = "POST_PMU";
1266 case SND_SOC_DAPM_PRE_PMD:
1267 ev_name = "PRE_PMD";
1270 case SND_SOC_DAPM_POST_PMD:
1271 ev_name = "POST_PMD";
1279 if (w->power != power)
1282 if (w->event && (w->event_flags & event)) {
1283 pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1285 trace_snd_soc_dapm_widget_event_start(w, event);
1286 ret = w->event(w, NULL, event);
1287 trace_snd_soc_dapm_widget_event_done(w, event);
1289 dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
1290 ev_name, w->name, ret);
1294 /* Apply the coalesced changes from a DAPM sequence */
1295 static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1296 struct list_head *pending)
1298 struct snd_soc_card *card = dapm->card;
1299 struct snd_soc_dapm_widget *w;
1301 unsigned int value = 0;
1302 unsigned int mask = 0;
1303 unsigned int cur_mask;
1305 reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1308 list_for_each_entry(w, pending, power_list) {
1309 cur_mask = 1 << w->shift;
1310 BUG_ON(reg != w->reg);
1321 pop_dbg(dapm->dev, card->pop_time,
1322 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1323 w->name, reg, value, mask);
1325 /* Check for events */
1326 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1327 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1331 /* Any widget will do, they should all be updating the
1334 w = list_first_entry(pending, struct snd_soc_dapm_widget,
1337 pop_dbg(dapm->dev, card->pop_time,
1338 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1339 value, mask, reg, card->pop_time);
1340 pop_wait(card->pop_time);
1341 soc_widget_update_bits_locked(w, reg, mask, value);
1344 list_for_each_entry(w, pending, power_list) {
1345 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1346 dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1350 /* Apply a DAPM power sequence.
1352 * We walk over a pre-sorted list of widgets to apply power to. In
1353 * order to minimise the number of writes to the device required
1354 * multiple widgets will be updated in a single write where possible.
1355 * Currently anything that requires more than a single write is not
1358 static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1359 struct list_head *list, int event, bool power_up)
1361 struct snd_soc_dapm_widget *w, *n;
1364 int cur_subseq = -1;
1365 int cur_reg = SND_SOC_NOPM;
1366 struct snd_soc_dapm_context *cur_dapm = NULL;
1373 sort = dapm_down_seq;
1375 list_for_each_entry_safe(w, n, list, power_list) {
1378 /* Do we need to apply any queued changes? */
1379 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1380 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1381 if (!list_empty(&pending))
1382 dapm_seq_run_coalesced(cur_dapm, &pending);
1384 if (cur_dapm && cur_dapm->seq_notifier) {
1385 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1386 if (sort[i] == cur_sort)
1387 cur_dapm->seq_notifier(cur_dapm,
1392 INIT_LIST_HEAD(&pending);
1394 cur_subseq = INT_MIN;
1395 cur_reg = SND_SOC_NOPM;
1400 case snd_soc_dapm_pre:
1402 list_for_each_entry_safe_continue(w, n, list,
1405 if (event == SND_SOC_DAPM_STREAM_START)
1407 NULL, SND_SOC_DAPM_PRE_PMU);
1408 else if (event == SND_SOC_DAPM_STREAM_STOP)
1410 NULL, SND_SOC_DAPM_PRE_PMD);
1413 case snd_soc_dapm_post:
1415 list_for_each_entry_safe_continue(w, n, list,
1418 if (event == SND_SOC_DAPM_STREAM_START)
1420 NULL, SND_SOC_DAPM_POST_PMU);
1421 else if (event == SND_SOC_DAPM_STREAM_STOP)
1423 NULL, SND_SOC_DAPM_POST_PMD);
1427 /* Queue it up for application */
1428 cur_sort = sort[w->id];
1429 cur_subseq = w->subseq;
1432 list_move(&w->power_list, &pending);
1437 dev_err(w->dapm->dev,
1438 "ASoC: Failed to apply widget power: %d\n", ret);
1441 if (!list_empty(&pending))
1442 dapm_seq_run_coalesced(cur_dapm, &pending);
1444 if (cur_dapm && cur_dapm->seq_notifier) {
1445 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1446 if (sort[i] == cur_sort)
1447 cur_dapm->seq_notifier(cur_dapm,
1452 static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1454 struct snd_soc_dapm_update *update = dapm->update;
1455 struct snd_soc_dapm_widget *w;
1464 (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1465 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1467 dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1471 ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1474 dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1478 (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1479 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1481 dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1486 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1487 * they're changing state.
1489 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1491 struct snd_soc_dapm_context *d = data;
1494 /* If we're off and we're not supposed to be go into STANDBY */
1495 if (d->bias_level == SND_SOC_BIAS_OFF &&
1496 d->target_bias_level != SND_SOC_BIAS_OFF) {
1498 pm_runtime_get_sync(d->dev);
1500 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1503 "ASoC: Failed to turn on bias: %d\n", ret);
1506 /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1507 if (d->bias_level != d->target_bias_level) {
1508 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1511 "ASoC: Failed to prepare bias: %d\n", ret);
1515 /* Async callback run prior to DAPM sequences - brings to their final
1518 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1520 struct snd_soc_dapm_context *d = data;
1523 /* If we just powered the last thing off drop to standby bias */
1524 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1525 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1526 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1527 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1529 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1533 /* If we're in standby and can support bias off then do that */
1534 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1535 d->target_bias_level == SND_SOC_BIAS_OFF) {
1536 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1538 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1542 pm_runtime_put(d->dev);
1545 /* If we just powered up then move to active bias */
1546 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1547 d->target_bias_level == SND_SOC_BIAS_ON) {
1548 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1550 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1555 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1556 bool power, bool connect)
1558 /* If a connection is being made or broken then that update
1559 * will have marked the peer dirty, otherwise the widgets are
1560 * not connected and this update has no impact. */
1564 /* If the peer is already in the state we're moving to then we
1565 * won't have an impact on it. */
1566 if (power != peer->power)
1567 dapm_mark_dirty(peer, "peer state change");
1570 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1571 struct list_head *up_list,
1572 struct list_head *down_list)
1574 struct snd_soc_dapm_path *path;
1576 if (w->power == power)
1579 trace_snd_soc_dapm_widget_power(w, power);
1581 /* If we changed our power state perhaps our neigbours changed
1584 list_for_each_entry(path, &w->sources, list_sink) {
1586 dapm_widget_set_peer_power(path->source, power,
1591 case snd_soc_dapm_supply:
1592 case snd_soc_dapm_regulator_supply:
1593 case snd_soc_dapm_clock_supply:
1594 /* Supplies can't affect their outputs, only their inputs */
1597 list_for_each_entry(path, &w->sinks, list_source) {
1599 dapm_widget_set_peer_power(path->sink, power,
1607 dapm_seq_insert(w, up_list, true);
1609 dapm_seq_insert(w, down_list, false);
1614 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1615 struct list_head *up_list,
1616 struct list_head *down_list)
1621 case snd_soc_dapm_pre:
1622 dapm_seq_insert(w, down_list, false);
1624 case snd_soc_dapm_post:
1625 dapm_seq_insert(w, up_list, true);
1629 power = dapm_widget_power_check(w);
1631 dapm_widget_set_power(w, power, up_list, down_list);
1637 * Scan each dapm widget for complete audio path.
1638 * A complete path is a route that has valid endpoints i.e.:-
1640 * o DAC to output pin.
1641 * o Input Pin to ADC.
1642 * o Input pin to Output pin (bypass, sidetone)
1643 * o DAC to ADC (loopback).
1645 static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1647 struct snd_soc_card *card = dapm->card;
1648 struct snd_soc_dapm_widget *w;
1649 struct snd_soc_dapm_context *d;
1651 LIST_HEAD(down_list);
1652 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1653 enum snd_soc_bias_level bias;
1655 trace_snd_soc_dapm_start(card);
1657 list_for_each_entry(d, &card->dapm_list, list) {
1658 if (d->idle_bias_off)
1659 d->target_bias_level = SND_SOC_BIAS_OFF;
1661 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1666 /* Check which widgets we need to power and store them in
1667 * lists indicating if they should be powered up or down. We
1668 * only check widgets that have been flagged as dirty but note
1669 * that new widgets may be added to the dirty list while we
1672 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1673 dapm_power_one_widget(w, &up_list, &down_list);
1676 list_for_each_entry(w, &card->widgets, list) {
1678 case snd_soc_dapm_pre:
1679 case snd_soc_dapm_post:
1680 /* These widgets always need to be powered */
1683 list_del_init(&w->dirty);
1690 /* Supplies and micbiases only bring the
1691 * context up to STANDBY as unless something
1692 * else is active and passing audio they
1693 * generally don't require full power. Signal
1694 * generators are virtual pins and have no
1695 * power impact themselves.
1698 case snd_soc_dapm_siggen:
1700 case snd_soc_dapm_supply:
1701 case snd_soc_dapm_regulator_supply:
1702 case snd_soc_dapm_clock_supply:
1703 case snd_soc_dapm_micbias:
1704 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1705 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1708 d->target_bias_level = SND_SOC_BIAS_ON;
1715 /* Force all contexts in the card to the same bias state if
1716 * they're not ground referenced.
1718 bias = SND_SOC_BIAS_OFF;
1719 list_for_each_entry(d, &card->dapm_list, list)
1720 if (d->target_bias_level > bias)
1721 bias = d->target_bias_level;
1722 list_for_each_entry(d, &card->dapm_list, list)
1723 if (!d->idle_bias_off)
1724 d->target_bias_level = bias;
1726 trace_snd_soc_dapm_walk_done(card);
1728 /* Run all the bias changes in parallel */
1729 list_for_each_entry(d, &dapm->card->dapm_list, list)
1730 async_schedule_domain(dapm_pre_sequence_async, d,
1732 async_synchronize_full_domain(&async_domain);
1734 /* Power down widgets first; try to avoid amplifying pops. */
1735 dapm_seq_run(dapm, &down_list, event, false);
1737 dapm_widget_update(dapm);
1740 dapm_seq_run(dapm, &up_list, event, true);
1742 /* Run all the bias changes in parallel */
1743 list_for_each_entry(d, &dapm->card->dapm_list, list)
1744 async_schedule_domain(dapm_post_sequence_async, d,
1746 async_synchronize_full_domain(&async_domain);
1748 /* do we need to notify any clients that DAPM event is complete */
1749 list_for_each_entry(d, &card->dapm_list, list) {
1750 if (d->stream_event)
1751 d->stream_event(d, event);
1754 pop_dbg(dapm->dev, card->pop_time,
1755 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1756 pop_wait(card->pop_time);
1758 trace_snd_soc_dapm_done(card);
1763 #ifdef CONFIG_DEBUG_FS
1764 static ssize_t dapm_widget_power_read_file(struct file *file,
1765 char __user *user_buf,
1766 size_t count, loff_t *ppos)
1768 struct snd_soc_dapm_widget *w = file->private_data;
1772 struct snd_soc_dapm_path *p = NULL;
1774 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1778 in = is_connected_input_ep(w, NULL);
1779 dapm_clear_walk_input(w->dapm, &w->sources);
1780 out = is_connected_output_ep(w, NULL);
1781 dapm_clear_walk_output(w->dapm, &w->sinks);
1783 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1784 w->name, w->power ? "On" : "Off",
1785 w->force ? " (forced)" : "", in, out);
1788 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1789 " - R%d(0x%x) bit %d",
1790 w->reg, w->reg, w->shift);
1792 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1795 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1797 w->active ? "active" : "inactive");
1799 list_for_each_entry(p, &w->sources, list_sink) {
1800 if (p->connected && !p->connected(w, p->source))
1804 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1805 " in \"%s\" \"%s\"\n",
1806 p->name ? p->name : "static",
1809 list_for_each_entry(p, &w->sinks, list_source) {
1810 if (p->connected && !p->connected(w, p->sink))
1814 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1815 " out \"%s\" \"%s\"\n",
1816 p->name ? p->name : "static",
1820 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1826 static const struct file_operations dapm_widget_power_fops = {
1827 .open = simple_open,
1828 .read = dapm_widget_power_read_file,
1829 .llseek = default_llseek,
1832 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1833 size_t count, loff_t *ppos)
1835 struct snd_soc_dapm_context *dapm = file->private_data;
1838 switch (dapm->bias_level) {
1839 case SND_SOC_BIAS_ON:
1842 case SND_SOC_BIAS_PREPARE:
1843 level = "Prepare\n";
1845 case SND_SOC_BIAS_STANDBY:
1846 level = "Standby\n";
1848 case SND_SOC_BIAS_OFF:
1853 level = "Unknown\n";
1857 return simple_read_from_buffer(user_buf, count, ppos, level,
1861 static const struct file_operations dapm_bias_fops = {
1862 .open = simple_open,
1863 .read = dapm_bias_read_file,
1864 .llseek = default_llseek,
1867 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1868 struct dentry *parent)
1872 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1874 if (!dapm->debugfs_dapm) {
1876 "ASoC: Failed to create DAPM debugfs directory\n");
1880 d = debugfs_create_file("bias_level", 0444,
1881 dapm->debugfs_dapm, dapm,
1885 "ASoC: Failed to create bias level debugfs file\n");
1888 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1890 struct snd_soc_dapm_context *dapm = w->dapm;
1893 if (!dapm->debugfs_dapm || !w->name)
1896 d = debugfs_create_file(w->name, 0444,
1897 dapm->debugfs_dapm, w,
1898 &dapm_widget_power_fops);
1900 dev_warn(w->dapm->dev,
1901 "ASoC: Failed to create %s debugfs file\n",
1905 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1907 debugfs_remove_recursive(dapm->debugfs_dapm);
1911 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1912 struct dentry *parent)
1916 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1920 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1926 /* test and update the power status of a mux widget */
1927 static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1928 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1930 struct snd_soc_dapm_path *path;
1933 if (widget->id != snd_soc_dapm_mux &&
1934 widget->id != snd_soc_dapm_virt_mux &&
1935 widget->id != snd_soc_dapm_value_mux)
1938 /* find dapm widget path assoc with kcontrol */
1939 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1940 if (path->kcontrol != kcontrol)
1943 if (!path->name || !e->texts[mux])
1947 /* we now need to match the string in the enum to the path */
1948 if (!(strcmp(path->name, e->texts[mux]))) {
1949 path->connect = 1; /* new connection */
1950 dapm_mark_dirty(path->source, "mux connection");
1953 dapm_mark_dirty(path->source,
1954 "mux disconnection");
1955 path->connect = 0; /* old connection must be powered down */
1960 dapm_mark_dirty(widget, "mux change");
1961 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1967 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1968 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1970 struct snd_soc_card *card = widget->dapm->card;
1973 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1974 ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1975 mutex_unlock(&card->dapm_mutex);
1977 soc_dpcm_runtime_update(widget);
1980 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1982 /* test and update the power status of a mixer or switch widget */
1983 static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1984 struct snd_kcontrol *kcontrol, int connect)
1986 struct snd_soc_dapm_path *path;
1989 if (widget->id != snd_soc_dapm_mixer &&
1990 widget->id != snd_soc_dapm_mixer_named_ctl &&
1991 widget->id != snd_soc_dapm_switch)
1994 /* find dapm widget path assoc with kcontrol */
1995 list_for_each_entry(path, &widget->dapm->card->paths, list) {
1996 if (path->kcontrol != kcontrol)
1999 /* found, now check type */
2001 path->connect = connect;
2002 dapm_mark_dirty(path->source, "mixer connection");
2006 dapm_mark_dirty(widget, "mixer update");
2007 dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2013 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2014 struct snd_kcontrol *kcontrol, int connect)
2016 struct snd_soc_card *card = widget->dapm->card;
2019 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2020 ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2021 mutex_unlock(&card->dapm_mutex);
2023 soc_dpcm_runtime_update(widget);
2026 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2028 /* show dapm widget status in sys fs */
2029 static ssize_t dapm_widget_show(struct device *dev,
2030 struct device_attribute *attr, char *buf)
2032 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2033 struct snd_soc_codec *codec =rtd->codec;
2034 struct snd_soc_dapm_widget *w;
2036 char *state = "not set";
2038 list_for_each_entry(w, &codec->card->widgets, list) {
2039 if (w->dapm != &codec->dapm)
2042 /* only display widgets that burnm power */
2044 case snd_soc_dapm_hp:
2045 case snd_soc_dapm_mic:
2046 case snd_soc_dapm_spk:
2047 case snd_soc_dapm_line:
2048 case snd_soc_dapm_micbias:
2049 case snd_soc_dapm_dac:
2050 case snd_soc_dapm_adc:
2051 case snd_soc_dapm_pga:
2052 case snd_soc_dapm_out_drv:
2053 case snd_soc_dapm_mixer:
2054 case snd_soc_dapm_mixer_named_ctl:
2055 case snd_soc_dapm_supply:
2056 case snd_soc_dapm_regulator_supply:
2057 case snd_soc_dapm_clock_supply:
2059 count += sprintf(buf + count, "%s: %s\n",
2060 w->name, w->power ? "On":"Off");
2067 switch (codec->dapm.bias_level) {
2068 case SND_SOC_BIAS_ON:
2071 case SND_SOC_BIAS_PREPARE:
2074 case SND_SOC_BIAS_STANDBY:
2077 case SND_SOC_BIAS_OFF:
2081 count += sprintf(buf + count, "PM State: %s\n", state);
2086 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2088 int snd_soc_dapm_sys_add(struct device *dev)
2090 return device_create_file(dev, &dev_attr_dapm_widget);
2093 static void snd_soc_dapm_sys_remove(struct device *dev)
2095 device_remove_file(dev, &dev_attr_dapm_widget);
2098 /* free all dapm widgets and resources */
2099 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2101 struct snd_soc_dapm_widget *w, *next_w;
2102 struct snd_soc_dapm_path *p, *next_p;
2104 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2105 if (w->dapm != dapm)
2109 * remove source and sink paths associated to this widget.
2110 * While removing the path, remove reference to it from both
2111 * source and sink widgets so that path is removed only once.
2113 list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
2114 list_del(&p->list_sink);
2115 list_del(&p->list_source);
2117 kfree(p->long_name);
2120 list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
2121 list_del(&p->list_sink);
2122 list_del(&p->list_source);
2124 kfree(p->long_name);
2127 kfree(w->kcontrols);
2133 static struct snd_soc_dapm_widget *dapm_find_widget(
2134 struct snd_soc_dapm_context *dapm, const char *pin,
2135 bool search_other_contexts)
2137 struct snd_soc_dapm_widget *w;
2138 struct snd_soc_dapm_widget *fallback = NULL;
2140 list_for_each_entry(w, &dapm->card->widgets, list) {
2141 if (!strcmp(w->name, pin)) {
2142 if (w->dapm == dapm)
2149 if (search_other_contexts)
2155 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2156 const char *pin, int status)
2158 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2161 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2165 if (w->connected != status)
2166 dapm_mark_dirty(w, "pin configuration");
2168 w->connected = status;
2176 * snd_soc_dapm_sync - scan and power dapm paths
2177 * @dapm: DAPM context
2179 * Walks all dapm audio paths and powers widgets according to their
2180 * stream or path usage.
2182 * Returns 0 for success.
2184 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2189 * Suppress early reports (eg, jacks syncing their state) to avoid
2190 * silly DAPM runs during card startup.
2192 if (!dapm->card || !dapm->card->instantiated)
2195 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2196 ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2197 mutex_unlock(&dapm->card->dapm_mutex);
2200 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2202 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2203 const struct snd_soc_dapm_route *route)
2205 struct snd_soc_dapm_path *path;
2206 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2207 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2209 const char *control = route->control;
2211 char prefixed_sink[80];
2212 char prefixed_source[80];
2215 if (dapm->codec && dapm->codec->name_prefix) {
2216 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2217 dapm->codec->name_prefix, route->sink);
2218 sink = prefixed_sink;
2219 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2220 dapm->codec->name_prefix, route->source);
2221 source = prefixed_source;
2224 source = route->source;
2228 * find src and dest widgets over all widgets but favor a widget from
2229 * current DAPM context
2231 list_for_each_entry(w, &dapm->card->widgets, list) {
2232 if (!wsink && !(strcmp(w->name, sink))) {
2234 if (w->dapm == dapm)
2238 if (!wsource && !(strcmp(w->name, source))) {
2240 if (w->dapm == dapm)
2244 /* use widget from another DAPM context if not found from this */
2250 if (wsource == NULL) {
2251 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2255 if (wsink == NULL) {
2256 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2261 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2265 path->source = wsource;
2267 path->connected = route->connected;
2268 INIT_LIST_HEAD(&path->list);
2269 INIT_LIST_HEAD(&path->list_source);
2270 INIT_LIST_HEAD(&path->list_sink);
2272 /* check for external widgets */
2273 if (wsink->id == snd_soc_dapm_input) {
2274 if (wsource->id == snd_soc_dapm_micbias ||
2275 wsource->id == snd_soc_dapm_mic ||
2276 wsource->id == snd_soc_dapm_line ||
2277 wsource->id == snd_soc_dapm_output)
2280 if (wsource->id == snd_soc_dapm_output) {
2281 if (wsink->id == snd_soc_dapm_spk ||
2282 wsink->id == snd_soc_dapm_hp ||
2283 wsink->id == snd_soc_dapm_line ||
2284 wsink->id == snd_soc_dapm_input)
2288 /* connect static paths */
2289 if (control == NULL) {
2290 list_add(&path->list, &dapm->card->paths);
2291 list_add(&path->list_sink, &wsink->sources);
2292 list_add(&path->list_source, &wsource->sinks);
2297 /* connect dynamic paths */
2298 switch (wsink->id) {
2299 case snd_soc_dapm_adc:
2300 case snd_soc_dapm_dac:
2301 case snd_soc_dapm_pga:
2302 case snd_soc_dapm_out_drv:
2303 case snd_soc_dapm_input:
2304 case snd_soc_dapm_output:
2305 case snd_soc_dapm_siggen:
2306 case snd_soc_dapm_micbias:
2307 case snd_soc_dapm_vmid:
2308 case snd_soc_dapm_pre:
2309 case snd_soc_dapm_post:
2310 case snd_soc_dapm_supply:
2311 case snd_soc_dapm_regulator_supply:
2312 case snd_soc_dapm_clock_supply:
2313 case snd_soc_dapm_aif_in:
2314 case snd_soc_dapm_aif_out:
2315 case snd_soc_dapm_dai_in:
2316 case snd_soc_dapm_dai_out:
2317 case snd_soc_dapm_dai_link:
2318 list_add(&path->list, &dapm->card->paths);
2319 list_add(&path->list_sink, &wsink->sources);
2320 list_add(&path->list_source, &wsource->sinks);
2323 case snd_soc_dapm_mux:
2324 case snd_soc_dapm_virt_mux:
2325 case snd_soc_dapm_value_mux:
2326 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2327 &wsink->kcontrol_news[0]);
2331 case snd_soc_dapm_switch:
2332 case snd_soc_dapm_mixer:
2333 case snd_soc_dapm_mixer_named_ctl:
2334 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2338 case snd_soc_dapm_hp:
2339 case snd_soc_dapm_mic:
2340 case snd_soc_dapm_line:
2341 case snd_soc_dapm_spk:
2342 list_add(&path->list, &dapm->card->paths);
2343 list_add(&path->list_sink, &wsink->sources);
2344 list_add(&path->list_source, &wsource->sinks);
2349 dapm_mark_dirty(wsource, "Route added");
2350 dapm_mark_dirty(wsink, "Route added");
2355 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2356 source, control, sink);
2361 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2362 const struct snd_soc_dapm_route *route)
2364 struct snd_soc_dapm_path *path, *p;
2367 char prefixed_sink[80];
2368 char prefixed_source[80];
2370 if (route->control) {
2372 "ASoC: Removal of routes with controls not supported\n");
2376 if (dapm->codec && dapm->codec->name_prefix) {
2377 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2378 dapm->codec->name_prefix, route->sink);
2379 sink = prefixed_sink;
2380 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2381 dapm->codec->name_prefix, route->source);
2382 source = prefixed_source;
2385 source = route->source;
2389 list_for_each_entry(p, &dapm->card->paths, list) {
2390 if (strcmp(p->source->name, source) != 0)
2392 if (strcmp(p->sink->name, sink) != 0)
2399 dapm_mark_dirty(path->source, "Route removed");
2400 dapm_mark_dirty(path->sink, "Route removed");
2402 list_del(&path->list);
2403 list_del(&path->list_sink);
2404 list_del(&path->list_source);
2407 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2415 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2416 * @dapm: DAPM context
2417 * @route: audio routes
2418 * @num: number of routes
2420 * Connects 2 dapm widgets together via a named audio path. The sink is
2421 * the widget receiving the audio signal, whilst the source is the sender
2422 * of the audio signal.
2424 * Returns 0 for success else error. On error all resources can be freed
2425 * with a call to snd_soc_card_free().
2427 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2428 const struct snd_soc_dapm_route *route, int num)
2432 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2433 for (i = 0; i < num; i++) {
2434 r = snd_soc_dapm_add_route(dapm, route);
2436 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2438 route->control ? route->control : "direct",
2444 mutex_unlock(&dapm->card->dapm_mutex);
2448 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2451 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2452 * @dapm: DAPM context
2453 * @route: audio routes
2454 * @num: number of routes
2456 * Removes routes from the DAPM context.
2458 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2459 const struct snd_soc_dapm_route *route, int num)
2463 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2464 for (i = 0; i < num; i++) {
2465 snd_soc_dapm_del_route(dapm, route);
2468 mutex_unlock(&dapm->card->dapm_mutex);
2472 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2474 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2475 const struct snd_soc_dapm_route *route)
2477 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2480 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2483 struct snd_soc_dapm_path *path;
2487 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2493 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2498 if (route->control || route->connected)
2499 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2500 route->source, route->sink);
2502 list_for_each_entry(path, &source->sinks, list_source) {
2503 if (path->sink == sink) {
2510 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2511 route->source, route->sink);
2513 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2514 count, route->source, route->sink);
2520 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2521 * @dapm: DAPM context
2522 * @route: audio routes
2523 * @num: number of routes
2525 * Mark existing routes matching those specified in the passed array
2526 * as being weak, meaning that they are ignored for the purpose of
2527 * power decisions. The main intended use case is for sidetone paths
2528 * which couple audio between other independent paths if they are both
2529 * active in order to make the combination work better at the user
2530 * level but which aren't intended to be "used".
2532 * Note that CODEC drivers should not use this as sidetone type paths
2533 * can frequently also be used as bypass paths.
2535 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2536 const struct snd_soc_dapm_route *route, int num)
2541 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2542 for (i = 0; i < num; i++) {
2543 err = snd_soc_dapm_weak_route(dapm, route);
2548 mutex_unlock(&dapm->card->dapm_mutex);
2552 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2555 * snd_soc_dapm_new_widgets - add new dapm widgets
2556 * @dapm: DAPM context
2558 * Checks the codec for any new dapm widgets and creates them if found.
2560 * Returns 0 for success.
2562 int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2564 struct snd_soc_dapm_widget *w;
2567 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2569 list_for_each_entry(w, &dapm->card->widgets, list)
2574 if (w->num_kcontrols) {
2575 w->kcontrols = kzalloc(w->num_kcontrols *
2576 sizeof(struct snd_kcontrol *),
2578 if (!w->kcontrols) {
2579 mutex_unlock(&dapm->card->dapm_mutex);
2585 case snd_soc_dapm_switch:
2586 case snd_soc_dapm_mixer:
2587 case snd_soc_dapm_mixer_named_ctl:
2590 case snd_soc_dapm_mux:
2591 case snd_soc_dapm_virt_mux:
2592 case snd_soc_dapm_value_mux:
2595 case snd_soc_dapm_pga:
2596 case snd_soc_dapm_out_drv:
2603 /* Read the initial power state from the device */
2605 val = soc_widget_read(w, w->reg);
2606 val &= 1 << w->shift;
2616 dapm_mark_dirty(w, "new widget");
2617 dapm_debugfs_add_widget(w);
2620 dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2621 mutex_unlock(&dapm->card->dapm_mutex);
2624 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2627 * snd_soc_dapm_get_volsw - dapm mixer get callback
2628 * @kcontrol: mixer control
2629 * @ucontrol: control element information
2631 * Callback to get the value of a dapm mixer control.
2633 * Returns 0 for success.
2635 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_value *ucontrol)
2638 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2639 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2640 struct soc_mixer_control *mc =
2641 (struct soc_mixer_control *)kcontrol->private_value;
2642 unsigned int reg = mc->reg;
2643 unsigned int shift = mc->shift;
2645 unsigned int mask = (1 << fls(max)) - 1;
2646 unsigned int invert = mc->invert;
2648 if (snd_soc_volsw_is_stereo(mc))
2649 dev_warn(widget->dapm->dev,
2650 "ASoC: Control '%s' is stereo, which is not supported\n",
2653 ucontrol->value.integer.value[0] =
2654 (snd_soc_read(widget->codec, reg) >> shift) & mask;
2656 ucontrol->value.integer.value[0] =
2657 max - ucontrol->value.integer.value[0];
2661 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2664 * snd_soc_dapm_put_volsw - dapm mixer set callback
2665 * @kcontrol: mixer control
2666 * @ucontrol: control element information
2668 * Callback to set the value of a dapm mixer control.
2670 * Returns 0 for success.
2672 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2675 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2676 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2677 struct snd_soc_codec *codec = widget->codec;
2678 struct snd_soc_card *card = codec->card;
2679 struct soc_mixer_control *mc =
2680 (struct soc_mixer_control *)kcontrol->private_value;
2681 unsigned int reg = mc->reg;
2682 unsigned int shift = mc->shift;
2684 unsigned int mask = (1 << fls(max)) - 1;
2685 unsigned int invert = mc->invert;
2687 int connect, change;
2688 struct snd_soc_dapm_update update;
2691 if (snd_soc_volsw_is_stereo(mc))
2692 dev_warn(widget->dapm->dev,
2693 "ASoC: Control '%s' is stereo, which is not supported\n",
2696 val = (ucontrol->value.integer.value[0] & mask);
2701 mask = mask << shift;
2704 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2706 change = snd_soc_test_bits(widget->codec, reg, mask, val);
2708 for (wi = 0; wi < wlist->num_widgets; wi++) {
2709 widget = wlist->widgets[wi];
2711 widget->value = val;
2713 update.kcontrol = kcontrol;
2714 update.widget = widget;
2718 widget->dapm->update = &update;
2720 soc_dapm_mixer_update_power(widget, kcontrol, connect);
2722 widget->dapm->update = NULL;
2726 mutex_unlock(&card->dapm_mutex);
2729 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2732 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2733 * @kcontrol: mixer control
2734 * @ucontrol: control element information
2736 * Callback to get the value of a dapm enumerated double mixer control.
2738 * Returns 0 for success.
2740 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
2743 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2744 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2745 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2748 val = snd_soc_read(widget->codec, e->reg);
2749 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2750 if (e->shift_l != e->shift_r)
2751 ucontrol->value.enumerated.item[1] =
2752 (val >> e->shift_r) & e->mask;
2756 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2759 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2760 * @kcontrol: mixer control
2761 * @ucontrol: control element information
2763 * Callback to set the value of a dapm enumerated double mixer control.
2765 * Returns 0 for success.
2767 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2770 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2771 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2772 struct snd_soc_codec *codec = widget->codec;
2773 struct snd_soc_card *card = codec->card;
2774 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2775 unsigned int val, mux, change;
2777 struct snd_soc_dapm_update update;
2780 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2782 mux = ucontrol->value.enumerated.item[0];
2783 val = mux << e->shift_l;
2784 mask = e->mask << e->shift_l;
2785 if (e->shift_l != e->shift_r) {
2786 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2788 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2789 mask |= e->mask << e->shift_r;
2792 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2794 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2796 for (wi = 0; wi < wlist->num_widgets; wi++) {
2797 widget = wlist->widgets[wi];
2799 widget->value = val;
2801 update.kcontrol = kcontrol;
2802 update.widget = widget;
2803 update.reg = e->reg;
2806 widget->dapm->update = &update;
2808 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2810 widget->dapm->update = NULL;
2814 mutex_unlock(&card->dapm_mutex);
2817 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2820 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2821 * @kcontrol: mixer control
2822 * @ucontrol: control element information
2824 * Returns 0 for success.
2826 int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2829 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2830 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2832 ucontrol->value.enumerated.item[0] = widget->value;
2836 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2839 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2840 * @kcontrol: mixer control
2841 * @ucontrol: control element information
2843 * Returns 0 for success.
2845 int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2848 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2849 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2850 struct snd_soc_codec *codec = widget->codec;
2851 struct snd_soc_card *card = codec->card;
2852 struct soc_enum *e =
2853 (struct soc_enum *)kcontrol->private_value;
2858 if (ucontrol->value.enumerated.item[0] >= e->max)
2861 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2863 change = widget->value != ucontrol->value.enumerated.item[0];
2865 for (wi = 0; wi < wlist->num_widgets; wi++) {
2866 widget = wlist->widgets[wi];
2868 widget->value = ucontrol->value.enumerated.item[0];
2870 soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2874 mutex_unlock(&card->dapm_mutex);
2877 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2880 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2882 * @kcontrol: mixer control
2883 * @ucontrol: control element information
2885 * Callback to get the value of a dapm semi enumerated double mixer control.
2887 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2888 * used for handling bitfield coded enumeration for example.
2890 * Returns 0 for success.
2892 int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2893 struct snd_ctl_elem_value *ucontrol)
2895 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2896 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2897 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2898 unsigned int reg_val, val, mux;
2900 reg_val = snd_soc_read(widget->codec, e->reg);
2901 val = (reg_val >> e->shift_l) & e->mask;
2902 for (mux = 0; mux < e->max; mux++) {
2903 if (val == e->values[mux])
2906 ucontrol->value.enumerated.item[0] = mux;
2907 if (e->shift_l != e->shift_r) {
2908 val = (reg_val >> e->shift_r) & e->mask;
2909 for (mux = 0; mux < e->max; mux++) {
2910 if (val == e->values[mux])
2913 ucontrol->value.enumerated.item[1] = mux;
2918 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2921 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2923 * @kcontrol: mixer control
2924 * @ucontrol: control element information
2926 * Callback to set the value of a dapm semi enumerated double mixer control.
2928 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2929 * used for handling bitfield coded enumeration for example.
2931 * Returns 0 for success.
2933 int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2934 struct snd_ctl_elem_value *ucontrol)
2936 struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2937 struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2938 struct snd_soc_codec *codec = widget->codec;
2939 struct snd_soc_card *card = codec->card;
2940 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2941 unsigned int val, mux, change;
2943 struct snd_soc_dapm_update update;
2946 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2948 mux = ucontrol->value.enumerated.item[0];
2949 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2950 mask = e->mask << e->shift_l;
2951 if (e->shift_l != e->shift_r) {
2952 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2954 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2955 mask |= e->mask << e->shift_r;
2958 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2960 change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2962 for (wi = 0; wi < wlist->num_widgets; wi++) {
2963 widget = wlist->widgets[wi];
2965 widget->value = val;
2967 update.kcontrol = kcontrol;
2968 update.widget = widget;
2969 update.reg = e->reg;
2972 widget->dapm->update = &update;
2974 soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2976 widget->dapm->update = NULL;
2980 mutex_unlock(&card->dapm_mutex);
2983 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2986 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2988 * @kcontrol: mixer control
2989 * @uinfo: control element information
2991 * Callback to provide information about a pin switch control.
2993 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_info *uinfo)
2996 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2998 uinfo->value.integer.min = 0;
2999 uinfo->value.integer.max = 1;
3003 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3006 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3008 * @kcontrol: mixer control
3011 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_value *ucontrol)
3014 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3015 const char *pin = (const char *)kcontrol->private_value;
3017 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3019 ucontrol->value.integer.value[0] =
3020 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3022 mutex_unlock(&card->dapm_mutex);
3026 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3029 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3031 * @kcontrol: mixer control
3034 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3035 struct snd_ctl_elem_value *ucontrol)
3037 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3038 const char *pin = (const char *)kcontrol->private_value;
3040 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3042 if (ucontrol->value.integer.value[0])
3043 snd_soc_dapm_enable_pin(&card->dapm, pin);
3045 snd_soc_dapm_disable_pin(&card->dapm, pin);
3047 mutex_unlock(&card->dapm_mutex);
3049 snd_soc_dapm_sync(&card->dapm);
3052 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3054 static struct snd_soc_dapm_widget *
3055 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3056 const struct snd_soc_dapm_widget *widget)
3058 struct snd_soc_dapm_widget *w;
3062 if ((w = dapm_cnew_widget(widget)) == NULL)
3066 case snd_soc_dapm_regulator_supply:
3067 w->regulator = devm_regulator_get(dapm->dev, w->name);
3068 if (IS_ERR(w->regulator)) {
3069 ret = PTR_ERR(w->regulator);
3070 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3075 if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3076 ret = regulator_allow_bypass(w->regulator, true);
3078 dev_warn(w->dapm->dev,
3079 "ASoC: Failed to unbypass %s: %d\n",
3083 case snd_soc_dapm_clock_supply:
3084 #ifdef CONFIG_CLKDEV_LOOKUP
3085 w->clk = devm_clk_get(dapm->dev, w->name);
3086 if (IS_ERR(w->clk)) {
3087 ret = PTR_ERR(w->clk);
3088 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3100 name_len = strlen(widget->name) + 1;
3101 if (dapm->codec && dapm->codec->name_prefix)
3102 name_len += 1 + strlen(dapm->codec->name_prefix);
3103 w->name = kmalloc(name_len, GFP_KERNEL);
3104 if (w->name == NULL) {
3108 if (dapm->codec && dapm->codec->name_prefix)
3109 snprintf((char *)w->name, name_len, "%s %s",
3110 dapm->codec->name_prefix, widget->name);
3112 snprintf((char *)w->name, name_len, "%s", widget->name);
3115 case snd_soc_dapm_switch:
3116 case snd_soc_dapm_mixer:
3117 case snd_soc_dapm_mixer_named_ctl:
3118 w->power_check = dapm_generic_check_power;
3120 case snd_soc_dapm_mux:
3121 case snd_soc_dapm_virt_mux:
3122 case snd_soc_dapm_value_mux:
3123 w->power_check = dapm_generic_check_power;
3125 case snd_soc_dapm_adc:
3126 case snd_soc_dapm_aif_out:
3127 case snd_soc_dapm_dai_out:
3128 w->power_check = dapm_adc_check_power;
3130 case snd_soc_dapm_dac:
3131 case snd_soc_dapm_aif_in:
3132 case snd_soc_dapm_dai_in:
3133 w->power_check = dapm_dac_check_power;
3135 case snd_soc_dapm_pga:
3136 case snd_soc_dapm_out_drv:
3137 case snd_soc_dapm_input:
3138 case snd_soc_dapm_output:
3139 case snd_soc_dapm_micbias:
3140 case snd_soc_dapm_spk:
3141 case snd_soc_dapm_hp:
3142 case snd_soc_dapm_mic:
3143 case snd_soc_dapm_line:
3144 case snd_soc_dapm_dai_link:
3145 w->power_check = dapm_generic_check_power;
3147 case snd_soc_dapm_supply:
3148 case snd_soc_dapm_regulator_supply:
3149 case snd_soc_dapm_clock_supply:
3150 w->power_check = dapm_supply_check_power;
3153 w->power_check = dapm_always_on_check_power;
3158 w->codec = dapm->codec;
3159 w->platform = dapm->platform;
3160 INIT_LIST_HEAD(&w->sources);
3161 INIT_LIST_HEAD(&w->sinks);
3162 INIT_LIST_HEAD(&w->list);
3163 INIT_LIST_HEAD(&w->dirty);
3164 list_add(&w->list, &dapm->card->widgets);
3166 /* machine layer set ups unconnected pins and insertions */
3172 * snd_soc_dapm_new_controls - create new dapm controls
3173 * @dapm: DAPM context
3174 * @widget: widget array
3175 * @num: number of widgets
3177 * Creates new DAPM controls based upon the templates.
3179 * Returns 0 for success else error.
3181 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3182 const struct snd_soc_dapm_widget *widget,
3185 struct snd_soc_dapm_widget *w;
3189 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3190 for (i = 0; i < num; i++) {
3191 w = snd_soc_dapm_new_control(dapm, widget);
3194 "ASoC: Failed to create DAPM control %s\n",
3201 mutex_unlock(&dapm->card->dapm_mutex);
3204 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3206 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3207 struct snd_kcontrol *kcontrol, int event)
3209 struct snd_soc_dapm_path *source_p, *sink_p;
3210 struct snd_soc_dai *source, *sink;
3211 const struct snd_soc_pcm_stream *config = w->params;
3212 struct snd_pcm_substream substream;
3213 struct snd_pcm_hw_params *params = NULL;
3218 BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3220 /* We only support a single source and sink, pick the first */
3221 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3223 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3226 BUG_ON(!source_p || !sink_p);
3227 BUG_ON(!sink_p->source || !source_p->sink);
3228 BUG_ON(!source_p->source || !sink_p->sink);
3230 source = source_p->source->priv;
3231 sink = sink_p->sink->priv;
3233 /* Be a little careful as we don't want to overflow the mask array */
3234 if (config->formats) {
3235 fmt = ffs(config->formats) - 1;
3237 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3242 /* Currently very limited parameter selection */
3243 params = kzalloc(sizeof(*params), GFP_KERNEL);
3248 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3250 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3252 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3255 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3256 = config->channels_min;
3257 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3258 = config->channels_max;
3260 memset(&substream, 0, sizeof(substream));
3263 case SND_SOC_DAPM_PRE_PMU:
3264 if (source->driver->ops && source->driver->ops->hw_params) {
3265 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3266 ret = source->driver->ops->hw_params(&substream,
3269 dev_err(source->dev,
3270 "ASoC: hw_params() failed: %d\n", ret);
3275 if (sink->driver->ops && sink->driver->ops->hw_params) {
3276 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3277 ret = sink->driver->ops->hw_params(&substream, params,
3281 "ASoC: hw_params() failed: %d\n", ret);
3287 case SND_SOC_DAPM_POST_PMU:
3288 ret = snd_soc_dai_digital_mute(sink, 0,
3289 SNDRV_PCM_STREAM_PLAYBACK);
3290 if (ret != 0 && ret != -ENOTSUPP)
3291 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3295 case SND_SOC_DAPM_PRE_PMD:
3296 ret = snd_soc_dai_digital_mute(sink, 1,
3297 SNDRV_PCM_STREAM_PLAYBACK);
3298 if (ret != 0 && ret != -ENOTSUPP)
3299 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3313 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3314 const struct snd_soc_pcm_stream *params,
3315 struct snd_soc_dapm_widget *source,
3316 struct snd_soc_dapm_widget *sink)
3318 struct snd_soc_dapm_route routes[2];
3319 struct snd_soc_dapm_widget template;
3320 struct snd_soc_dapm_widget *w;
3324 len = strlen(source->name) + strlen(sink->name) + 2;
3325 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3328 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3330 memset(&template, 0, sizeof(template));
3331 template.reg = SND_SOC_NOPM;
3332 template.id = snd_soc_dapm_dai_link;
3333 template.name = link_name;
3334 template.event = snd_soc_dai_link_event;
3335 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3336 SND_SOC_DAPM_PRE_PMD;
3338 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3340 w = snd_soc_dapm_new_control(&card->dapm, &template);
3342 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3349 memset(&routes, 0, sizeof(routes));
3351 routes[0].source = source->name;
3352 routes[0].sink = link_name;
3353 routes[1].source = link_name;
3354 routes[1].sink = sink->name;
3356 return snd_soc_dapm_add_routes(&card->dapm, routes,
3357 ARRAY_SIZE(routes));
3360 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3361 struct snd_soc_dai *dai)
3363 struct snd_soc_dapm_widget template;
3364 struct snd_soc_dapm_widget *w;
3366 WARN_ON(dapm->dev != dai->dev);
3368 memset(&template, 0, sizeof(template));
3369 template.reg = SND_SOC_NOPM;
3371 if (dai->driver->playback.stream_name) {
3372 template.id = snd_soc_dapm_dai_in;
3373 template.name = dai->driver->playback.stream_name;
3374 template.sname = dai->driver->playback.stream_name;
3376 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3379 w = snd_soc_dapm_new_control(dapm, &template);
3381 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3382 dai->driver->playback.stream_name);
3386 dai->playback_widget = w;
3389 if (dai->driver->capture.stream_name) {
3390 template.id = snd_soc_dapm_dai_out;
3391 template.name = dai->driver->capture.stream_name;
3392 template.sname = dai->driver->capture.stream_name;
3394 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3397 w = snd_soc_dapm_new_control(dapm, &template);
3399 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3400 dai->driver->capture.stream_name);
3404 dai->capture_widget = w;
3410 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3412 struct snd_soc_dapm_widget *dai_w, *w;
3413 struct snd_soc_dai *dai;
3414 struct snd_soc_dapm_route r;
3416 memset(&r, 0, sizeof(r));
3418 /* For each DAI widget... */
3419 list_for_each_entry(dai_w, &card->widgets, list) {
3420 switch (dai_w->id) {
3421 case snd_soc_dapm_dai_in:
3422 case snd_soc_dapm_dai_out:
3430 /* ...find all widgets with the same stream and link them */
3431 list_for_each_entry(w, &card->widgets, list) {
3432 if (w->dapm != dai_w->dapm)
3436 case snd_soc_dapm_dai_in:
3437 case snd_soc_dapm_dai_out:
3446 if (dai->driver->playback.stream_name &&
3448 dai->driver->playback.stream_name)) {
3449 r.source = dai->playback_widget->name;
3451 dev_dbg(dai->dev, "%s -> %s\n",
3454 snd_soc_dapm_add_route(w->dapm, &r);
3457 if (dai->driver->capture.stream_name &&
3459 dai->driver->capture.stream_name)) {
3461 r.sink = dai->capture_widget->name;
3462 dev_dbg(dai->dev, "%s -> %s\n",
3465 snd_soc_dapm_add_route(w->dapm, &r);
3473 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3477 struct snd_soc_dapm_widget *w_cpu, *w_codec;
3478 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3479 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3481 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3482 w_cpu = cpu_dai->playback_widget;
3483 w_codec = codec_dai->playback_widget;
3485 w_cpu = cpu_dai->capture_widget;
3486 w_codec = codec_dai->capture_widget;
3491 dapm_mark_dirty(w_cpu, "stream event");
3494 case SND_SOC_DAPM_STREAM_START:
3497 case SND_SOC_DAPM_STREAM_STOP:
3500 case SND_SOC_DAPM_STREAM_SUSPEND:
3501 case SND_SOC_DAPM_STREAM_RESUME:
3502 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3503 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3510 dapm_mark_dirty(w_codec, "stream event");
3513 case SND_SOC_DAPM_STREAM_START:
3514 w_codec->active = 1;
3516 case SND_SOC_DAPM_STREAM_STOP:
3517 w_codec->active = 0;
3519 case SND_SOC_DAPM_STREAM_SUSPEND:
3520 case SND_SOC_DAPM_STREAM_RESUME:
3521 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3522 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3527 dapm_power_widgets(&rtd->card->dapm, event);
3531 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3532 * @rtd: PCM runtime data
3533 * @stream: stream name
3534 * @event: stream event
3536 * Sends a stream event to the dapm core. The core then makes any
3537 * necessary widget power changes.
3539 * Returns 0 for success else error.
3541 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3544 struct snd_soc_card *card = rtd->card;
3546 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3547 soc_dapm_stream_event(rtd, stream, event);
3548 mutex_unlock(&card->dapm_mutex);
3552 * snd_soc_dapm_enable_pin - enable pin.
3553 * @dapm: DAPM context
3556 * Enables input/output pin and its parents or children widgets iff there is
3557 * a valid audio route and active audio stream.
3558 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3559 * do any widget power switching.
3561 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3563 return snd_soc_dapm_set_pin(dapm, pin, 1);
3565 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3568 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3569 * @dapm: DAPM context
3572 * Enables input/output pin regardless of any other state. This is
3573 * intended for use with microphone bias supplies used in microphone
3576 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3577 * do any widget power switching.
3579 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3582 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3585 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3589 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3592 dapm_mark_dirty(w, "force enable");
3596 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3599 * snd_soc_dapm_disable_pin - disable pin.
3600 * @dapm: DAPM context
3603 * Disables input/output pin and its parents or children widgets.
3604 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3605 * do any widget power switching.
3607 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3610 return snd_soc_dapm_set_pin(dapm, pin, 0);
3612 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3615 * snd_soc_dapm_nc_pin - permanently disable pin.
3616 * @dapm: DAPM context
3619 * Marks the specified pin as being not connected, disabling it along
3620 * any parent or child widgets. At present this is identical to
3621 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3622 * additional things such as disabling controls which only affect
3623 * paths through the pin.
3625 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3626 * do any widget power switching.
3628 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3630 return snd_soc_dapm_set_pin(dapm, pin, 0);
3632 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3635 * snd_soc_dapm_get_pin_status - get audio pin status
3636 * @dapm: DAPM context
3637 * @pin: audio signal pin endpoint (or start point)
3639 * Get audio pin status - connected or disconnected.
3641 * Returns 1 for connected otherwise 0.
3643 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3646 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3649 return w->connected;
3653 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3656 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3657 * @dapm: DAPM context
3658 * @pin: audio signal pin endpoint (or start point)
3660 * Mark the given endpoint or pin as ignoring suspend. When the
3661 * system is disabled a path between two endpoints flagged as ignoring
3662 * suspend will not be disabled. The path must already be enabled via
3663 * normal means at suspend time, it will not be turned on if it was not
3666 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3669 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3672 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3676 w->ignore_suspend = 1;
3680 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3682 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3683 struct snd_soc_dapm_widget *w)
3685 struct snd_soc_dapm_path *p;
3687 list_for_each_entry(p, &card->paths, list) {
3688 if ((p->source == w) || (p->sink == w)) {
3690 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3691 p->source->name, p->source->id, p->source->dapm,
3692 p->sink->name, p->sink->id, p->sink->dapm);
3694 /* Connected to something other than the codec */
3695 if (p->source->dapm != p->sink->dapm)
3698 * Loopback connection from codec external pin to
3699 * codec external pin
3701 if (p->sink->id == snd_soc_dapm_input) {
3702 switch (p->source->id) {
3703 case snd_soc_dapm_output:
3704 case snd_soc_dapm_micbias:
3717 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3718 * @codec: The codec whose pins should be processed
3720 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3721 * which are unused. Pins are used if they are connected externally to the
3722 * codec, whether that be to some other device, or a loop-back connection to
3725 void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3727 struct snd_soc_card *card = codec->card;
3728 struct snd_soc_dapm_context *dapm = &codec->dapm;
3729 struct snd_soc_dapm_widget *w;
3731 dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3732 &card->dapm, &codec->dapm);
3734 list_for_each_entry(w, &card->widgets, list) {
3735 if (w->dapm != dapm)
3738 case snd_soc_dapm_input:
3739 case snd_soc_dapm_output:
3740 case snd_soc_dapm_micbias:
3741 dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3743 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3745 "... Not in map; disabling\n");
3746 snd_soc_dapm_nc_pin(dapm, w->name);
3756 * snd_soc_dapm_free - free dapm resources
3757 * @dapm: DAPM context
3759 * Free all dapm widgets and resources.
3761 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3763 snd_soc_dapm_sys_remove(dapm->dev);
3764 dapm_debugfs_cleanup(dapm);
3765 dapm_free_widgets(dapm);
3766 list_del(&dapm->list);
3768 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3770 static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3772 struct snd_soc_card *card = dapm->card;
3773 struct snd_soc_dapm_widget *w;
3774 LIST_HEAD(down_list);
3777 mutex_lock(&card->dapm_mutex);
3779 list_for_each_entry(w, &dapm->card->widgets, list) {
3780 if (w->dapm != dapm)
3783 dapm_seq_insert(w, &down_list, false);
3789 /* If there were no widgets to power down we're already in
3793 if (dapm->bias_level == SND_SOC_BIAS_ON)
3794 snd_soc_dapm_set_bias_level(dapm,
3795 SND_SOC_BIAS_PREPARE);
3796 dapm_seq_run(dapm, &down_list, 0, false);
3797 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3798 snd_soc_dapm_set_bias_level(dapm,
3799 SND_SOC_BIAS_STANDBY);
3802 mutex_unlock(&card->dapm_mutex);
3806 * snd_soc_dapm_shutdown - callback for system shutdown
3808 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3810 struct snd_soc_codec *codec;
3812 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3813 soc_dapm_shutdown_codec(&codec->dapm);
3814 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3815 snd_soc_dapm_set_bias_level(&codec->dapm,
3820 /* Module information */
3821 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3822 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3823 MODULE_LICENSE("GPL");