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 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
51 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
53 int (*connected)(struct snd_soc_dapm_widget *source,
54 struct snd_soc_dapm_widget *sink));
55 static struct snd_soc_dapm_widget *
56 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
57 const struct snd_soc_dapm_widget *widget);
59 /* dapm power sequences - make this per codec in the future */
60 static int dapm_up_seq[] = {
61 [snd_soc_dapm_pre] = 0,
62 [snd_soc_dapm_regulator_supply] = 1,
63 [snd_soc_dapm_clock_supply] = 1,
64 [snd_soc_dapm_supply] = 2,
65 [snd_soc_dapm_micbias] = 3,
66 [snd_soc_dapm_dai_link] = 2,
67 [snd_soc_dapm_dai_in] = 4,
68 [snd_soc_dapm_dai_out] = 4,
69 [snd_soc_dapm_aif_in] = 4,
70 [snd_soc_dapm_aif_out] = 4,
71 [snd_soc_dapm_mic] = 5,
72 [snd_soc_dapm_mux] = 6,
73 [snd_soc_dapm_dac] = 7,
74 [snd_soc_dapm_switch] = 8,
75 [snd_soc_dapm_mixer] = 8,
76 [snd_soc_dapm_mixer_named_ctl] = 8,
77 [snd_soc_dapm_pga] = 9,
78 [snd_soc_dapm_adc] = 10,
79 [snd_soc_dapm_out_drv] = 11,
80 [snd_soc_dapm_hp] = 11,
81 [snd_soc_dapm_spk] = 11,
82 [snd_soc_dapm_line] = 11,
83 [snd_soc_dapm_kcontrol] = 12,
84 [snd_soc_dapm_post] = 13,
87 static int dapm_down_seq[] = {
88 [snd_soc_dapm_pre] = 0,
89 [snd_soc_dapm_kcontrol] = 1,
90 [snd_soc_dapm_adc] = 2,
91 [snd_soc_dapm_hp] = 3,
92 [snd_soc_dapm_spk] = 3,
93 [snd_soc_dapm_line] = 3,
94 [snd_soc_dapm_out_drv] = 3,
95 [snd_soc_dapm_pga] = 4,
96 [snd_soc_dapm_switch] = 5,
97 [snd_soc_dapm_mixer_named_ctl] = 5,
98 [snd_soc_dapm_mixer] = 5,
99 [snd_soc_dapm_dac] = 6,
100 [snd_soc_dapm_mic] = 7,
101 [snd_soc_dapm_micbias] = 8,
102 [snd_soc_dapm_mux] = 9,
103 [snd_soc_dapm_aif_in] = 10,
104 [snd_soc_dapm_aif_out] = 10,
105 [snd_soc_dapm_dai_in] = 10,
106 [snd_soc_dapm_dai_out] = 10,
107 [snd_soc_dapm_dai_link] = 11,
108 [snd_soc_dapm_supply] = 12,
109 [snd_soc_dapm_clock_supply] = 13,
110 [snd_soc_dapm_regulator_supply] = 13,
111 [snd_soc_dapm_post] = 14,
114 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
116 if (dapm->card && dapm->card->instantiated)
117 lockdep_assert_held(&dapm->card->dapm_mutex);
120 static void pop_wait(u32 pop_time)
123 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
126 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
134 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
139 vsnprintf(buf, PAGE_SIZE, fmt, args);
140 dev_info(dev, "%s", buf);
146 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
148 return !list_empty(&w->dirty);
151 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
153 dapm_assert_locked(w->dapm);
155 if (!dapm_dirty_widget(w)) {
156 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
158 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
162 void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
164 struct snd_soc_card *card = dapm->card;
165 struct snd_soc_dapm_widget *w;
167 mutex_lock(&card->dapm_mutex);
169 list_for_each_entry(w, &card->widgets, list) {
171 case snd_soc_dapm_input:
172 case snd_soc_dapm_output:
173 dapm_mark_dirty(w, "Rechecking inputs and outputs");
180 mutex_unlock(&card->dapm_mutex);
182 EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
184 /* create a new dapm widget */
185 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
186 const struct snd_soc_dapm_widget *_widget)
188 return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
191 struct dapm_kcontrol_data {
193 struct snd_soc_dapm_widget *widget;
194 struct list_head paths;
195 struct snd_soc_dapm_widget_list *wlist;
198 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
199 struct snd_kcontrol *kcontrol)
201 struct dapm_kcontrol_data *data;
202 struct soc_mixer_control *mc;
204 data = kzalloc(sizeof(*data), GFP_KERNEL);
206 dev_err(widget->dapm->dev,
207 "ASoC: can't allocate kcontrol data for %s\n",
212 INIT_LIST_HEAD(&data->paths);
214 switch (widget->id) {
215 case snd_soc_dapm_switch:
216 case snd_soc_dapm_mixer:
217 case snd_soc_dapm_mixer_named_ctl:
218 mc = (struct soc_mixer_control *)kcontrol->private_value;
220 if (mc->autodisable) {
221 struct snd_soc_dapm_widget template;
223 memset(&template, 0, sizeof(template));
224 template.reg = mc->reg;
225 template.mask = (1 << fls(mc->max)) - 1;
226 template.shift = mc->shift;
228 template.off_val = mc->max;
230 template.off_val = 0;
231 template.on_val = template.off_val;
232 template.id = snd_soc_dapm_kcontrol;
233 template.name = kcontrol->id.name;
235 data->value = template.on_val;
237 data->widget = snd_soc_dapm_new_control(widget->dapm,
249 kcontrol->private_data = data;
254 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
256 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
261 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
262 const struct snd_kcontrol *kcontrol)
264 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
269 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
270 struct snd_soc_dapm_widget *widget)
272 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
273 struct snd_soc_dapm_widget_list *new_wlist;
277 n = data->wlist->num_widgets + 1;
281 new_wlist = krealloc(data->wlist,
282 sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
286 new_wlist->widgets[n - 1] = widget;
287 new_wlist->num_widgets = n;
289 data->wlist = new_wlist;
294 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
295 struct snd_soc_dapm_path *path)
297 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
299 list_add_tail(&path->list_kcontrol, &data->paths);
302 snd_soc_dapm_add_path(data->widget->dapm, data->widget,
303 path->source, NULL, NULL);
307 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
309 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
314 return data->widget->power;
317 static struct list_head *dapm_kcontrol_get_path_list(
318 const struct snd_kcontrol *kcontrol)
320 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
325 #define dapm_kcontrol_for_each_path(path, kcontrol) \
326 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
329 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
335 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
337 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
340 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
342 if (data->value == value)
346 data->widget->on_val = value;
354 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
356 * @kcontrol: The kcontrol
358 * Note: This function must only be used on kcontrols that are known to have
359 * been registered for a CODEC. Otherwise the behaviour is undefined.
361 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
362 struct snd_kcontrol *kcontrol)
364 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
366 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
369 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
370 * @kcontrol: The kcontrol
372 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
374 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
376 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
378 static void dapm_reset(struct snd_soc_card *card)
380 struct snd_soc_dapm_widget *w;
382 lockdep_assert_held(&card->dapm_mutex);
384 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
386 list_for_each_entry(w, &card->widgets, list) {
387 w->new_power = w->power;
388 w->power_checked = false;
394 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
396 if (!dapm->component)
398 return dapm->component->name_prefix;
401 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
404 if (!dapm->component)
406 return snd_soc_component_read(dapm->component, reg, value);
409 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
410 int reg, unsigned int mask, unsigned int value)
412 if (!dapm->component)
414 return snd_soc_component_update_bits_async(dapm->component, reg,
418 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
419 int reg, unsigned int mask, unsigned int value)
421 if (!dapm->component)
423 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
426 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
429 snd_soc_component_async_complete(dapm->component);
433 * snd_soc_dapm_set_bias_level - set the bias level for the system
434 * @dapm: DAPM context
435 * @level: level to configure
437 * Configure the bias (power) levels for the SoC audio device.
439 * Returns 0 for success else error.
441 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
442 enum snd_soc_bias_level level)
444 struct snd_soc_card *card = dapm->card;
447 trace_snd_soc_bias_level_start(card, level);
449 if (card && card->set_bias_level)
450 ret = card->set_bias_level(card, dapm, level);
454 if (dapm->set_bias_level)
455 ret = dapm->set_bias_level(dapm, level);
456 else if (!card || dapm != &card->dapm)
457 dapm->bias_level = level;
462 if (card && card->set_bias_level_post)
463 ret = card->set_bias_level_post(card, dapm, level);
465 trace_snd_soc_bias_level_done(card, level);
470 /* connect mux widget to its interconnecting audio paths */
471 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
472 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
473 struct snd_soc_dapm_path *path, const char *control_name,
474 const struct snd_kcontrol_new *kcontrol)
476 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
477 unsigned int val, item;
480 if (e->reg != SND_SOC_NOPM) {
481 soc_dapm_read(dapm, e->reg, &val);
482 val = (val >> e->shift_l) & e->mask;
483 item = snd_soc_enum_val_to_item(e, val);
485 /* since a virtual mux has no backing registers to
486 * decide which path to connect, it will try to match
487 * with the first enumeration. This is to ensure
488 * that the default mux choice (the first) will be
489 * correctly powered up during initialization.
494 for (i = 0; i < e->items; i++) {
495 if (!(strcmp(control_name, e->texts[i]))) {
496 list_add(&path->list, &dapm->card->paths);
497 list_add(&path->list_sink, &dest->sources);
498 list_add(&path->list_source, &src->sinks);
499 path->name = (char*)e->texts[i];
511 /* set up initial codec paths */
512 static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
513 struct snd_soc_dapm_path *p, int i)
515 struct soc_mixer_control *mc = (struct soc_mixer_control *)
516 w->kcontrol_news[i].private_value;
517 unsigned int reg = mc->reg;
518 unsigned int shift = mc->shift;
519 unsigned int max = mc->max;
520 unsigned int mask = (1 << fls(max)) - 1;
521 unsigned int invert = mc->invert;
524 if (reg != SND_SOC_NOPM) {
525 soc_dapm_read(w->dapm, reg, &val);
526 val = (val >> shift) & mask;
535 /* connect mixer widget to its interconnecting audio paths */
536 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
537 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
538 struct snd_soc_dapm_path *path, const char *control_name)
542 /* search for mixer kcontrol */
543 for (i = 0; i < dest->num_kcontrols; i++) {
544 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
545 list_add(&path->list, &dapm->card->paths);
546 list_add(&path->list_sink, &dest->sources);
547 list_add(&path->list_source, &src->sinks);
548 path->name = dest->kcontrol_news[i].name;
549 dapm_set_mixer_path_status(dest, path, i);
556 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
557 struct snd_soc_dapm_widget *kcontrolw,
558 const struct snd_kcontrol_new *kcontrol_new,
559 struct snd_kcontrol **kcontrol)
561 struct snd_soc_dapm_widget *w;
566 list_for_each_entry(w, &dapm->card->widgets, list) {
567 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
569 for (i = 0; i < w->num_kcontrols; i++) {
570 if (&w->kcontrol_news[i] == kcontrol_new) {
572 *kcontrol = w->kcontrols[i];
582 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
583 * create it. Either way, add the widget into the control's widget list
585 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
588 struct snd_soc_dapm_context *dapm = w->dapm;
589 struct snd_card *card = dapm->card->snd_card;
593 struct snd_kcontrol *kcontrol;
594 bool wname_in_long_name, kcname_in_long_name;
599 prefix = soc_dapm_prefix(dapm);
601 prefix_len = strlen(prefix) + 1;
605 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
610 wname_in_long_name = false;
611 kcname_in_long_name = true;
614 case snd_soc_dapm_switch:
615 case snd_soc_dapm_mixer:
616 wname_in_long_name = true;
617 kcname_in_long_name = true;
619 case snd_soc_dapm_mixer_named_ctl:
620 wname_in_long_name = false;
621 kcname_in_long_name = true;
623 case snd_soc_dapm_mux:
624 wname_in_long_name = true;
625 kcname_in_long_name = false;
632 if (wname_in_long_name && kcname_in_long_name) {
634 * The control will get a prefix from the control
635 * creation process but we're also using the same
636 * prefix for widgets so cut the prefix off the
637 * front of the widget name.
639 long_name = kasprintf(GFP_KERNEL, "%s %s",
640 w->name + prefix_len,
641 w->kcontrol_news[kci].name);
642 if (long_name == NULL)
646 } else if (wname_in_long_name) {
648 name = w->name + prefix_len;
651 name = w->kcontrol_news[kci].name;
654 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
659 kcontrol->private_free = dapm_kcontrol_free;
661 ret = dapm_kcontrol_data_alloc(w, kcontrol);
663 snd_ctl_free_one(kcontrol);
667 ret = snd_ctl_add(card, kcontrol);
670 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
676 ret = dapm_kcontrol_add_widget(kcontrol, w);
680 w->kcontrols[kci] = kcontrol;
685 /* create new dapm mixer control */
686 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
689 struct snd_soc_dapm_path *path;
692 for (i = 0; i < w->num_kcontrols; i++) {
694 list_for_each_entry(path, &w->sources, list_sink) {
695 /* mixer/mux paths name must match control name */
696 if (path->name != (char *)w->kcontrol_news[i].name)
699 if (w->kcontrols[i]) {
700 dapm_kcontrol_add_path(w->kcontrols[i], path);
704 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
708 dapm_kcontrol_add_path(w->kcontrols[i], path);
715 /* create new dapm mux control */
716 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
718 struct snd_soc_dapm_context *dapm = w->dapm;
719 struct snd_soc_dapm_path *path;
722 if (w->num_kcontrols != 1) {
724 "ASoC: mux %s has incorrect number of controls\n",
729 if (list_empty(&w->sources)) {
730 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
734 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
738 list_for_each_entry(path, &w->sources, list_sink)
739 dapm_kcontrol_add_path(w->kcontrols[0], path);
744 /* create new dapm volume control */
745 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
747 if (w->num_kcontrols)
748 dev_err(w->dapm->dev,
749 "ASoC: PGA controls not supported: '%s'\n", w->name);
754 /* reset 'walked' bit for each dapm path */
755 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
756 struct list_head *sink)
758 struct snd_soc_dapm_path *p;
760 list_for_each_entry(p, sink, list_source) {
763 dapm_clear_walk_output(dapm, &p->sink->sinks);
768 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
769 struct list_head *source)
771 struct snd_soc_dapm_path *p;
773 list_for_each_entry(p, source, list_sink) {
776 dapm_clear_walk_input(dapm, &p->source->sources);
782 /* We implement power down on suspend by checking the power state of
783 * the ALSA card - when we are suspending the ALSA state for the card
786 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
788 int level = snd_power_get_state(widget->dapm->card->snd_card);
791 case SNDRV_CTL_POWER_D3hot:
792 case SNDRV_CTL_POWER_D3cold:
793 if (widget->ignore_suspend)
794 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
796 return widget->ignore_suspend;
802 /* add widget to list if it's not already in the list */
803 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
804 struct snd_soc_dapm_widget *w)
806 struct snd_soc_dapm_widget_list *wlist;
807 int wlistsize, wlistentries, i;
814 /* is this widget already in the list */
815 for (i = 0; i < wlist->num_widgets; i++) {
816 if (wlist->widgets[i] == w)
820 /* allocate some new space */
821 wlistentries = wlist->num_widgets + 1;
822 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
823 wlistentries * sizeof(struct snd_soc_dapm_widget *);
824 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
826 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
832 /* insert the widget */
833 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
834 w->name, wlist->num_widgets);
836 wlist->widgets[wlist->num_widgets] = w;
837 wlist->num_widgets++;
842 * Recursively check for a completed path to an active or physically connected
843 * output widget. Returns number of complete paths.
845 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
846 struct snd_soc_dapm_widget_list **list)
848 struct snd_soc_dapm_path *path;
851 if (widget->outputs >= 0)
852 return widget->outputs;
854 DAPM_UPDATE_STAT(widget, path_checks);
856 switch (widget->id) {
857 case snd_soc_dapm_supply:
858 case snd_soc_dapm_regulator_supply:
859 case snd_soc_dapm_clock_supply:
860 case snd_soc_dapm_kcontrol:
866 switch (widget->id) {
867 case snd_soc_dapm_adc:
868 case snd_soc_dapm_aif_out:
869 case snd_soc_dapm_dai_out:
870 if (widget->active) {
871 widget->outputs = snd_soc_dapm_suspend_check(widget);
872 return widget->outputs;
878 if (widget->connected) {
879 /* connected pin ? */
880 if (widget->id == snd_soc_dapm_output && !widget->ext) {
881 widget->outputs = snd_soc_dapm_suspend_check(widget);
882 return widget->outputs;
885 /* connected jack or spk ? */
886 if (widget->id == snd_soc_dapm_hp ||
887 widget->id == snd_soc_dapm_spk ||
888 (widget->id == snd_soc_dapm_line &&
889 !list_empty(&widget->sources))) {
890 widget->outputs = snd_soc_dapm_suspend_check(widget);
891 return widget->outputs;
895 list_for_each_entry(path, &widget->sinks, list_source) {
896 DAPM_UPDATE_STAT(widget, neighbour_checks);
907 trace_snd_soc_dapm_output_path(widget, path);
909 if (path->sink && path->connect) {
913 /* do we need to add this widget to the list ? */
916 err = dapm_list_add_widget(list, path->sink);
918 dev_err(widget->dapm->dev,
919 "ASoC: could not add widget %s\n",
926 con += is_connected_output_ep(path->sink, list);
932 widget->outputs = con;
938 * Recursively check for a completed path to an active or physically connected
939 * input widget. Returns number of complete paths.
941 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
942 struct snd_soc_dapm_widget_list **list)
944 struct snd_soc_dapm_path *path;
947 if (widget->inputs >= 0)
948 return widget->inputs;
950 DAPM_UPDATE_STAT(widget, path_checks);
952 switch (widget->id) {
953 case snd_soc_dapm_supply:
954 case snd_soc_dapm_regulator_supply:
955 case snd_soc_dapm_clock_supply:
956 case snd_soc_dapm_kcontrol:
962 /* active stream ? */
963 switch (widget->id) {
964 case snd_soc_dapm_dac:
965 case snd_soc_dapm_aif_in:
966 case snd_soc_dapm_dai_in:
967 if (widget->active) {
968 widget->inputs = snd_soc_dapm_suspend_check(widget);
969 return widget->inputs;
975 if (widget->connected) {
976 /* connected pin ? */
977 if (widget->id == snd_soc_dapm_input && !widget->ext) {
978 widget->inputs = snd_soc_dapm_suspend_check(widget);
979 return widget->inputs;
982 /* connected VMID/Bias for lower pops */
983 if (widget->id == snd_soc_dapm_vmid) {
984 widget->inputs = snd_soc_dapm_suspend_check(widget);
985 return widget->inputs;
988 /* connected jack ? */
989 if (widget->id == snd_soc_dapm_mic ||
990 (widget->id == snd_soc_dapm_line &&
991 !list_empty(&widget->sinks))) {
992 widget->inputs = snd_soc_dapm_suspend_check(widget);
993 return widget->inputs;
996 /* signal generator */
997 if (widget->id == snd_soc_dapm_siggen) {
998 widget->inputs = snd_soc_dapm_suspend_check(widget);
999 return widget->inputs;
1003 list_for_each_entry(path, &widget->sources, list_sink) {
1004 DAPM_UPDATE_STAT(widget, neighbour_checks);
1015 trace_snd_soc_dapm_input_path(widget, path);
1017 if (path->source && path->connect) {
1021 /* do we need to add this widget to the list ? */
1024 err = dapm_list_add_widget(list, path->source);
1026 dev_err(widget->dapm->dev,
1027 "ASoC: could not add widget %s\n",
1034 con += is_connected_input_ep(path->source, list);
1040 widget->inputs = con;
1046 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1047 * @dai: the soc DAI.
1048 * @stream: stream direction.
1049 * @list: list of active widgets for this stream.
1051 * Queries DAPM graph as to whether an valid audio stream path exists for
1052 * the initial stream specified by name. This takes into account
1053 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1055 * Returns the number of valid paths or negative error.
1057 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1058 struct snd_soc_dapm_widget_list **list)
1060 struct snd_soc_card *card = dai->card;
1063 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1066 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1067 paths = is_connected_output_ep(dai->playback_widget, list);
1068 dapm_clear_walk_output(&card->dapm,
1069 &dai->playback_widget->sinks);
1071 paths = is_connected_input_ep(dai->capture_widget, list);
1072 dapm_clear_walk_input(&card->dapm,
1073 &dai->capture_widget->sources);
1076 trace_snd_soc_dapm_connected(paths, stream);
1077 mutex_unlock(&card->dapm_mutex);
1083 * Handler for regulator supply widget.
1085 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1086 struct snd_kcontrol *kcontrol, int event)
1090 soc_dapm_async_complete(w->dapm);
1092 if (SND_SOC_DAPM_EVENT_ON(event)) {
1093 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1094 ret = regulator_allow_bypass(w->regulator, false);
1096 dev_warn(w->dapm->dev,
1097 "ASoC: Failed to unbypass %s: %d\n",
1101 return regulator_enable(w->regulator);
1103 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1104 ret = regulator_allow_bypass(w->regulator, true);
1106 dev_warn(w->dapm->dev,
1107 "ASoC: Failed to bypass %s: %d\n",
1111 return regulator_disable_deferred(w->regulator, w->shift);
1114 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1117 * Handler for clock supply widget.
1119 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1120 struct snd_kcontrol *kcontrol, int event)
1125 soc_dapm_async_complete(w->dapm);
1127 #ifdef CONFIG_HAVE_CLK
1128 if (SND_SOC_DAPM_EVENT_ON(event)) {
1129 return clk_prepare_enable(w->clk);
1131 clk_disable_unprepare(w->clk);
1137 EXPORT_SYMBOL_GPL(dapm_clock_event);
1139 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1141 if (w->power_checked)
1142 return w->new_power;
1147 w->new_power = w->power_check(w);
1149 w->power_checked = true;
1151 return w->new_power;
1154 /* Generic check to see if a widget should be powered.
1156 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1160 DAPM_UPDATE_STAT(w, power_checks);
1162 in = is_connected_input_ep(w, NULL);
1163 dapm_clear_walk_input(w->dapm, &w->sources);
1164 out = is_connected_output_ep(w, NULL);
1165 dapm_clear_walk_output(w->dapm, &w->sinks);
1166 return out != 0 && in != 0;
1169 /* Check to see if an ADC has power */
1170 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1174 DAPM_UPDATE_STAT(w, power_checks);
1177 in = is_connected_input_ep(w, NULL);
1178 dapm_clear_walk_input(w->dapm, &w->sources);
1181 return dapm_generic_check_power(w);
1185 /* Check to see if a DAC has power */
1186 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1190 DAPM_UPDATE_STAT(w, power_checks);
1193 out = is_connected_output_ep(w, NULL);
1194 dapm_clear_walk_output(w->dapm, &w->sinks);
1197 return dapm_generic_check_power(w);
1201 /* Check to see if a power supply is needed */
1202 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1204 struct snd_soc_dapm_path *path;
1206 DAPM_UPDATE_STAT(w, power_checks);
1208 /* Check if one of our outputs is connected */
1209 list_for_each_entry(path, &w->sinks, list_source) {
1210 DAPM_UPDATE_STAT(w, neighbour_checks);
1215 if (path->connected &&
1216 !path->connected(path->source, path->sink))
1222 if (dapm_widget_power_check(path->sink))
1229 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1234 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1235 struct snd_soc_dapm_widget *b,
1243 sort = dapm_down_seq;
1245 if (sort[a->id] != sort[b->id])
1246 return sort[a->id] - sort[b->id];
1247 if (a->subseq != b->subseq) {
1249 return a->subseq - b->subseq;
1251 return b->subseq - a->subseq;
1253 if (a->reg != b->reg)
1254 return a->reg - b->reg;
1255 if (a->dapm != b->dapm)
1256 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1261 /* Insert a widget in order into a DAPM power sequence. */
1262 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1263 struct list_head *list,
1266 struct snd_soc_dapm_widget *w;
1268 list_for_each_entry(w, list, power_list)
1269 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1270 list_add_tail(&new_widget->power_list, &w->power_list);
1274 list_add_tail(&new_widget->power_list, list);
1277 static void dapm_seq_check_event(struct snd_soc_card *card,
1278 struct snd_soc_dapm_widget *w, int event)
1280 const char *ev_name;
1284 case SND_SOC_DAPM_PRE_PMU:
1285 ev_name = "PRE_PMU";
1288 case SND_SOC_DAPM_POST_PMU:
1289 ev_name = "POST_PMU";
1292 case SND_SOC_DAPM_PRE_PMD:
1293 ev_name = "PRE_PMD";
1296 case SND_SOC_DAPM_POST_PMD:
1297 ev_name = "POST_PMD";
1300 case SND_SOC_DAPM_WILL_PMU:
1301 ev_name = "WILL_PMU";
1304 case SND_SOC_DAPM_WILL_PMD:
1305 ev_name = "WILL_PMD";
1309 WARN(1, "Unknown event %d\n", event);
1313 if (w->new_power != power)
1316 if (w->event && (w->event_flags & event)) {
1317 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1319 soc_dapm_async_complete(w->dapm);
1320 trace_snd_soc_dapm_widget_event_start(w, event);
1321 ret = w->event(w, NULL, event);
1322 trace_snd_soc_dapm_widget_event_done(w, event);
1324 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1325 ev_name, w->name, ret);
1329 /* Apply the coalesced changes from a DAPM sequence */
1330 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1331 struct list_head *pending)
1333 struct snd_soc_dapm_context *dapm;
1334 struct snd_soc_dapm_widget *w;
1336 unsigned int value = 0;
1337 unsigned int mask = 0;
1339 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1343 list_for_each_entry(w, pending, power_list) {
1344 WARN_ON(reg != w->reg || dapm != w->dapm);
1345 w->power = w->new_power;
1347 mask |= w->mask << w->shift;
1349 value |= w->on_val << w->shift;
1351 value |= w->off_val << w->shift;
1353 pop_dbg(dapm->dev, card->pop_time,
1354 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1355 w->name, reg, value, mask);
1357 /* Check for events */
1358 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1359 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1363 /* Any widget will do, they should all be updating the
1367 pop_dbg(dapm->dev, card->pop_time,
1368 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1369 value, mask, reg, card->pop_time);
1370 pop_wait(card->pop_time);
1371 soc_dapm_update_bits(dapm, reg, mask, value);
1374 list_for_each_entry(w, pending, power_list) {
1375 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1376 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1380 /* Apply a DAPM power sequence.
1382 * We walk over a pre-sorted list of widgets to apply power to. In
1383 * order to minimise the number of writes to the device required
1384 * multiple widgets will be updated in a single write where possible.
1385 * Currently anything that requires more than a single write is not
1388 static void dapm_seq_run(struct snd_soc_card *card,
1389 struct list_head *list, int event, bool power_up)
1391 struct snd_soc_dapm_widget *w, *n;
1392 struct snd_soc_dapm_context *d;
1395 int cur_subseq = -1;
1396 int cur_reg = SND_SOC_NOPM;
1397 struct snd_soc_dapm_context *cur_dapm = NULL;
1404 sort = dapm_down_seq;
1406 list_for_each_entry_safe(w, n, list, power_list) {
1409 /* Do we need to apply any queued changes? */
1410 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1411 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1412 if (!list_empty(&pending))
1413 dapm_seq_run_coalesced(card, &pending);
1415 if (cur_dapm && cur_dapm->seq_notifier) {
1416 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1417 if (sort[i] == cur_sort)
1418 cur_dapm->seq_notifier(cur_dapm,
1423 if (cur_dapm && w->dapm != cur_dapm)
1424 soc_dapm_async_complete(cur_dapm);
1426 INIT_LIST_HEAD(&pending);
1428 cur_subseq = INT_MIN;
1429 cur_reg = SND_SOC_NOPM;
1434 case snd_soc_dapm_pre:
1436 list_for_each_entry_safe_continue(w, n, list,
1439 if (event == SND_SOC_DAPM_STREAM_START)
1441 NULL, SND_SOC_DAPM_PRE_PMU);
1442 else if (event == SND_SOC_DAPM_STREAM_STOP)
1444 NULL, SND_SOC_DAPM_PRE_PMD);
1447 case snd_soc_dapm_post:
1449 list_for_each_entry_safe_continue(w, n, list,
1452 if (event == SND_SOC_DAPM_STREAM_START)
1454 NULL, SND_SOC_DAPM_POST_PMU);
1455 else if (event == SND_SOC_DAPM_STREAM_STOP)
1457 NULL, SND_SOC_DAPM_POST_PMD);
1461 /* Queue it up for application */
1462 cur_sort = sort[w->id];
1463 cur_subseq = w->subseq;
1466 list_move(&w->power_list, &pending);
1471 dev_err(w->dapm->dev,
1472 "ASoC: Failed to apply widget power: %d\n", ret);
1475 if (!list_empty(&pending))
1476 dapm_seq_run_coalesced(card, &pending);
1478 if (cur_dapm && cur_dapm->seq_notifier) {
1479 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1480 if (sort[i] == cur_sort)
1481 cur_dapm->seq_notifier(cur_dapm,
1485 list_for_each_entry(d, &card->dapm_list, list) {
1486 soc_dapm_async_complete(d);
1490 static void dapm_widget_update(struct snd_soc_card *card)
1492 struct snd_soc_dapm_update *update = card->update;
1493 struct snd_soc_dapm_widget_list *wlist;
1494 struct snd_soc_dapm_widget *w = NULL;
1498 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1501 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1503 for (wi = 0; wi < wlist->num_widgets; wi++) {
1504 w = wlist->widgets[wi];
1506 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1507 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1509 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1517 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1520 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1523 for (wi = 0; wi < wlist->num_widgets; wi++) {
1524 w = wlist->widgets[wi];
1526 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1527 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1529 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1535 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1536 * they're changing state.
1538 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1540 struct snd_soc_dapm_context *d = data;
1543 /* If we're off and we're not supposed to be go into STANDBY */
1544 if (d->bias_level == SND_SOC_BIAS_OFF &&
1545 d->target_bias_level != SND_SOC_BIAS_OFF) {
1547 pm_runtime_get_sync(d->dev);
1549 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1552 "ASoC: Failed to turn on bias: %d\n", ret);
1555 /* Prepare for a transition to ON or away from ON */
1556 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1557 d->bias_level != SND_SOC_BIAS_ON) ||
1558 (d->target_bias_level != SND_SOC_BIAS_ON &&
1559 d->bias_level == SND_SOC_BIAS_ON)) {
1560 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1563 "ASoC: Failed to prepare bias: %d\n", ret);
1567 /* Async callback run prior to DAPM sequences - brings to their final
1570 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1572 struct snd_soc_dapm_context *d = data;
1575 /* If we just powered the last thing off drop to standby bias */
1576 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1577 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1578 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1579 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1581 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1585 /* If we're in standby and can support bias off then do that */
1586 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1587 d->target_bias_level == SND_SOC_BIAS_OFF) {
1588 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1590 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1594 pm_runtime_put(d->dev);
1597 /* If we just powered up then move to active bias */
1598 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1599 d->target_bias_level == SND_SOC_BIAS_ON) {
1600 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1602 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1607 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1608 bool power, bool connect)
1610 /* If a connection is being made or broken then that update
1611 * will have marked the peer dirty, otherwise the widgets are
1612 * not connected and this update has no impact. */
1616 /* If the peer is already in the state we're moving to then we
1617 * won't have an impact on it. */
1618 if (power != peer->power)
1619 dapm_mark_dirty(peer, "peer state change");
1622 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1623 struct list_head *up_list,
1624 struct list_head *down_list)
1626 struct snd_soc_dapm_path *path;
1628 if (w->power == power)
1631 trace_snd_soc_dapm_widget_power(w, power);
1633 /* If we changed our power state perhaps our neigbours changed
1636 list_for_each_entry(path, &w->sources, list_sink) {
1638 dapm_widget_set_peer_power(path->source, power,
1643 case snd_soc_dapm_supply:
1644 case snd_soc_dapm_regulator_supply:
1645 case snd_soc_dapm_clock_supply:
1646 case snd_soc_dapm_kcontrol:
1647 /* Supplies can't affect their outputs, only their inputs */
1650 list_for_each_entry(path, &w->sinks, list_source) {
1652 dapm_widget_set_peer_power(path->sink, power,
1660 dapm_seq_insert(w, up_list, true);
1662 dapm_seq_insert(w, down_list, false);
1665 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1666 struct list_head *up_list,
1667 struct list_head *down_list)
1672 case snd_soc_dapm_pre:
1673 dapm_seq_insert(w, down_list, false);
1675 case snd_soc_dapm_post:
1676 dapm_seq_insert(w, up_list, true);
1680 power = dapm_widget_power_check(w);
1682 dapm_widget_set_power(w, power, up_list, down_list);
1687 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1689 if (dapm->idle_bias_off)
1692 switch (snd_power_get_state(dapm->card->snd_card)) {
1693 case SNDRV_CTL_POWER_D3hot:
1694 case SNDRV_CTL_POWER_D3cold:
1695 return dapm->suspend_bias_off;
1704 * Scan each dapm widget for complete audio path.
1705 * A complete path is a route that has valid endpoints i.e.:-
1707 * o DAC to output pin.
1708 * o Input Pin to ADC.
1709 * o Input pin to Output pin (bypass, sidetone)
1710 * o DAC to ADC (loopback).
1712 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1714 struct snd_soc_dapm_widget *w;
1715 struct snd_soc_dapm_context *d;
1717 LIST_HEAD(down_list);
1718 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1719 enum snd_soc_bias_level bias;
1721 lockdep_assert_held(&card->dapm_mutex);
1723 trace_snd_soc_dapm_start(card);
1725 list_for_each_entry(d, &card->dapm_list, list) {
1726 if (dapm_idle_bias_off(d))
1727 d->target_bias_level = SND_SOC_BIAS_OFF;
1729 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1734 /* Check which widgets we need to power and store them in
1735 * lists indicating if they should be powered up or down. We
1736 * only check widgets that have been flagged as dirty but note
1737 * that new widgets may be added to the dirty list while we
1740 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1741 dapm_power_one_widget(w, &up_list, &down_list);
1744 list_for_each_entry(w, &card->widgets, list) {
1746 case snd_soc_dapm_pre:
1747 case snd_soc_dapm_post:
1748 /* These widgets always need to be powered */
1751 list_del_init(&w->dirty);
1758 /* Supplies and micbiases only bring the
1759 * context up to STANDBY as unless something
1760 * else is active and passing audio they
1761 * generally don't require full power. Signal
1762 * generators are virtual pins and have no
1763 * power impact themselves.
1766 case snd_soc_dapm_siggen:
1767 case snd_soc_dapm_vmid:
1769 case snd_soc_dapm_supply:
1770 case snd_soc_dapm_regulator_supply:
1771 case snd_soc_dapm_clock_supply:
1772 case snd_soc_dapm_micbias:
1773 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1774 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1777 d->target_bias_level = SND_SOC_BIAS_ON;
1784 /* Force all contexts in the card to the same bias state if
1785 * they're not ground referenced.
1787 bias = SND_SOC_BIAS_OFF;
1788 list_for_each_entry(d, &card->dapm_list, list)
1789 if (d->target_bias_level > bias)
1790 bias = d->target_bias_level;
1791 list_for_each_entry(d, &card->dapm_list, list)
1792 if (!dapm_idle_bias_off(d))
1793 d->target_bias_level = bias;
1795 trace_snd_soc_dapm_walk_done(card);
1797 /* Run card bias changes at first */
1798 dapm_pre_sequence_async(&card->dapm, 0);
1799 /* Run other bias changes in parallel */
1800 list_for_each_entry(d, &card->dapm_list, list) {
1801 if (d != &card->dapm)
1802 async_schedule_domain(dapm_pre_sequence_async, d,
1805 async_synchronize_full_domain(&async_domain);
1807 list_for_each_entry(w, &down_list, power_list) {
1808 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1811 list_for_each_entry(w, &up_list, power_list) {
1812 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1815 /* Power down widgets first; try to avoid amplifying pops. */
1816 dapm_seq_run(card, &down_list, event, false);
1818 dapm_widget_update(card);
1821 dapm_seq_run(card, &up_list, event, true);
1823 /* Run all the bias changes in parallel */
1824 list_for_each_entry(d, &card->dapm_list, list) {
1825 if (d != &card->dapm)
1826 async_schedule_domain(dapm_post_sequence_async, d,
1829 async_synchronize_full_domain(&async_domain);
1830 /* Run card bias changes at last */
1831 dapm_post_sequence_async(&card->dapm, 0);
1833 /* do we need to notify any clients that DAPM event is complete */
1834 list_for_each_entry(d, &card->dapm_list, list) {
1835 if (d->stream_event)
1836 d->stream_event(d, event);
1839 pop_dbg(card->dev, card->pop_time,
1840 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1841 pop_wait(card->pop_time);
1843 trace_snd_soc_dapm_done(card);
1848 #ifdef CONFIG_DEBUG_FS
1849 static ssize_t dapm_widget_power_read_file(struct file *file,
1850 char __user *user_buf,
1851 size_t count, loff_t *ppos)
1853 struct snd_soc_dapm_widget *w = file->private_data;
1857 struct snd_soc_dapm_path *p = NULL;
1859 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1863 in = is_connected_input_ep(w, NULL);
1864 dapm_clear_walk_input(w->dapm, &w->sources);
1865 out = is_connected_output_ep(w, NULL);
1866 dapm_clear_walk_output(w->dapm, &w->sinks);
1868 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1869 w->name, w->power ? "On" : "Off",
1870 w->force ? " (forced)" : "", in, out);
1873 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1874 " - R%d(0x%x) mask 0x%x",
1875 w->reg, w->reg, w->mask << w->shift);
1877 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1880 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1882 w->active ? "active" : "inactive");
1884 list_for_each_entry(p, &w->sources, list_sink) {
1885 if (p->connected && !p->connected(w, p->source))
1889 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1890 " in \"%s\" \"%s\"\n",
1891 p->name ? p->name : "static",
1894 list_for_each_entry(p, &w->sinks, list_source) {
1895 if (p->connected && !p->connected(w, p->sink))
1899 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1900 " out \"%s\" \"%s\"\n",
1901 p->name ? p->name : "static",
1905 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1911 static const struct file_operations dapm_widget_power_fops = {
1912 .open = simple_open,
1913 .read = dapm_widget_power_read_file,
1914 .llseek = default_llseek,
1917 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1918 size_t count, loff_t *ppos)
1920 struct snd_soc_dapm_context *dapm = file->private_data;
1923 switch (dapm->bias_level) {
1924 case SND_SOC_BIAS_ON:
1927 case SND_SOC_BIAS_PREPARE:
1928 level = "Prepare\n";
1930 case SND_SOC_BIAS_STANDBY:
1931 level = "Standby\n";
1933 case SND_SOC_BIAS_OFF:
1937 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1938 level = "Unknown\n";
1942 return simple_read_from_buffer(user_buf, count, ppos, level,
1946 static const struct file_operations dapm_bias_fops = {
1947 .open = simple_open,
1948 .read = dapm_bias_read_file,
1949 .llseek = default_llseek,
1952 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1953 struct dentry *parent)
1957 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1959 if (!dapm->debugfs_dapm) {
1961 "ASoC: Failed to create DAPM debugfs directory\n");
1965 d = debugfs_create_file("bias_level", 0444,
1966 dapm->debugfs_dapm, dapm,
1970 "ASoC: Failed to create bias level debugfs file\n");
1973 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1975 struct snd_soc_dapm_context *dapm = w->dapm;
1978 if (!dapm->debugfs_dapm || !w->name)
1981 d = debugfs_create_file(w->name, 0444,
1982 dapm->debugfs_dapm, w,
1983 &dapm_widget_power_fops);
1985 dev_warn(w->dapm->dev,
1986 "ASoC: Failed to create %s debugfs file\n",
1990 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1992 debugfs_remove_recursive(dapm->debugfs_dapm);
1996 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1997 struct dentry *parent)
2001 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2005 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2011 /* test and update the power status of a mux widget */
2012 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2013 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2015 struct snd_soc_dapm_path *path;
2018 lockdep_assert_held(&card->dapm_mutex);
2020 /* find dapm widget path assoc with kcontrol */
2021 dapm_kcontrol_for_each_path(path, kcontrol) {
2022 if (!path->name || !e->texts[mux])
2026 /* we now need to match the string in the enum to the path */
2027 if (!(strcmp(path->name, e->texts[mux]))) {
2028 path->connect = 1; /* new connection */
2029 dapm_mark_dirty(path->source, "mux connection");
2032 dapm_mark_dirty(path->source,
2033 "mux disconnection");
2034 path->connect = 0; /* old connection must be powered down */
2036 dapm_mark_dirty(path->sink, "mux change");
2040 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2045 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2046 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2047 struct snd_soc_dapm_update *update)
2049 struct snd_soc_card *card = dapm->card;
2052 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2053 card->update = update;
2054 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2055 card->update = NULL;
2056 mutex_unlock(&card->dapm_mutex);
2058 soc_dpcm_runtime_update(card);
2061 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2063 /* test and update the power status of a mixer or switch widget */
2064 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2065 struct snd_kcontrol *kcontrol, int connect)
2067 struct snd_soc_dapm_path *path;
2070 lockdep_assert_held(&card->dapm_mutex);
2072 /* find dapm widget path assoc with kcontrol */
2073 dapm_kcontrol_for_each_path(path, kcontrol) {
2075 path->connect = connect;
2076 dapm_mark_dirty(path->source, "mixer connection");
2077 dapm_mark_dirty(path->sink, "mixer update");
2081 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2086 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2087 struct snd_kcontrol *kcontrol, int connect,
2088 struct snd_soc_dapm_update *update)
2090 struct snd_soc_card *card = dapm->card;
2093 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2094 card->update = update;
2095 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2096 card->update = NULL;
2097 mutex_unlock(&card->dapm_mutex);
2099 soc_dpcm_runtime_update(card);
2102 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2104 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2106 struct snd_soc_dapm_widget *w;
2108 char *state = "not set";
2110 list_for_each_entry(w, &codec->component.card->widgets, list) {
2111 if (w->dapm != &codec->dapm)
2114 /* only display widgets that burnm power */
2116 case snd_soc_dapm_hp:
2117 case snd_soc_dapm_mic:
2118 case snd_soc_dapm_spk:
2119 case snd_soc_dapm_line:
2120 case snd_soc_dapm_micbias:
2121 case snd_soc_dapm_dac:
2122 case snd_soc_dapm_adc:
2123 case snd_soc_dapm_pga:
2124 case snd_soc_dapm_out_drv:
2125 case snd_soc_dapm_mixer:
2126 case snd_soc_dapm_mixer_named_ctl:
2127 case snd_soc_dapm_supply:
2128 case snd_soc_dapm_regulator_supply:
2129 case snd_soc_dapm_clock_supply:
2131 count += sprintf(buf + count, "%s: %s\n",
2132 w->name, w->power ? "On":"Off");
2139 switch (codec->dapm.bias_level) {
2140 case SND_SOC_BIAS_ON:
2143 case SND_SOC_BIAS_PREPARE:
2146 case SND_SOC_BIAS_STANDBY:
2149 case SND_SOC_BIAS_OFF:
2153 count += sprintf(buf + count, "PM State: %s\n", state);
2158 /* show dapm widget status in sys fs */
2159 static ssize_t dapm_widget_show(struct device *dev,
2160 struct device_attribute *attr, char *buf)
2162 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2165 for (i = 0; i < rtd->num_codecs; i++) {
2166 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2167 count += dapm_widget_show_codec(codec, buf + count);
2173 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2175 int snd_soc_dapm_sys_add(struct device *dev)
2177 return device_create_file(dev, &dev_attr_dapm_widget);
2180 static void snd_soc_dapm_sys_remove(struct device *dev)
2182 device_remove_file(dev, &dev_attr_dapm_widget);
2185 static void dapm_free_path(struct snd_soc_dapm_path *path)
2187 list_del(&path->list_sink);
2188 list_del(&path->list_source);
2189 list_del(&path->list_kcontrol);
2190 list_del(&path->list);
2194 /* free all dapm widgets and resources */
2195 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2197 struct snd_soc_dapm_widget *w, *next_w;
2198 struct snd_soc_dapm_path *p, *next_p;
2200 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2201 if (w->dapm != dapm)
2205 * remove source and sink paths associated to this widget.
2206 * While removing the path, remove reference to it from both
2207 * source and sink widgets so that path is removed only once.
2209 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2212 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2215 kfree(w->kcontrols);
2221 static struct snd_soc_dapm_widget *dapm_find_widget(
2222 struct snd_soc_dapm_context *dapm, const char *pin,
2223 bool search_other_contexts)
2225 struct snd_soc_dapm_widget *w;
2226 struct snd_soc_dapm_widget *fallback = NULL;
2228 list_for_each_entry(w, &dapm->card->widgets, list) {
2229 if (!strcmp(w->name, pin)) {
2230 if (w->dapm == dapm)
2237 if (search_other_contexts)
2243 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2244 const char *pin, int status)
2246 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2248 dapm_assert_locked(dapm);
2251 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2255 if (w->connected != status)
2256 dapm_mark_dirty(w, "pin configuration");
2258 w->connected = status;
2266 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2267 * @dapm: DAPM context
2269 * Walks all dapm audio paths and powers widgets according to their
2270 * stream or path usage.
2272 * Requires external locking.
2274 * Returns 0 for success.
2276 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2279 * Suppress early reports (eg, jacks syncing their state) to avoid
2280 * silly DAPM runs during card startup.
2282 if (!dapm->card || !dapm->card->instantiated)
2285 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2287 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2290 * snd_soc_dapm_sync - scan and power dapm paths
2291 * @dapm: DAPM context
2293 * Walks all dapm audio paths and powers widgets according to their
2294 * stream or path usage.
2296 * Returns 0 for success.
2298 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2302 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2303 ret = snd_soc_dapm_sync_unlocked(dapm);
2304 mutex_unlock(&dapm->card->dapm_mutex);
2307 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2309 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2310 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2311 const char *control,
2312 int (*connected)(struct snd_soc_dapm_widget *source,
2313 struct snd_soc_dapm_widget *sink))
2315 struct snd_soc_dapm_path *path;
2318 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2322 path->source = wsource;
2324 path->connected = connected;
2325 INIT_LIST_HEAD(&path->list);
2326 INIT_LIST_HEAD(&path->list_kcontrol);
2327 INIT_LIST_HEAD(&path->list_source);
2328 INIT_LIST_HEAD(&path->list_sink);
2330 /* check for external widgets */
2331 if (wsink->id == snd_soc_dapm_input) {
2332 if (wsource->id == snd_soc_dapm_micbias ||
2333 wsource->id == snd_soc_dapm_mic ||
2334 wsource->id == snd_soc_dapm_line ||
2335 wsource->id == snd_soc_dapm_output)
2338 if (wsource->id == snd_soc_dapm_output) {
2339 if (wsink->id == snd_soc_dapm_spk ||
2340 wsink->id == snd_soc_dapm_hp ||
2341 wsink->id == snd_soc_dapm_line ||
2342 wsink->id == snd_soc_dapm_input)
2346 dapm_mark_dirty(wsource, "Route added");
2347 dapm_mark_dirty(wsink, "Route added");
2349 /* connect static paths */
2350 if (control == NULL) {
2351 list_add(&path->list, &dapm->card->paths);
2352 list_add(&path->list_sink, &wsink->sources);
2353 list_add(&path->list_source, &wsource->sinks);
2358 /* connect dynamic paths */
2359 switch (wsink->id) {
2360 case snd_soc_dapm_adc:
2361 case snd_soc_dapm_dac:
2362 case snd_soc_dapm_pga:
2363 case snd_soc_dapm_out_drv:
2364 case snd_soc_dapm_input:
2365 case snd_soc_dapm_output:
2366 case snd_soc_dapm_siggen:
2367 case snd_soc_dapm_micbias:
2368 case snd_soc_dapm_vmid:
2369 case snd_soc_dapm_pre:
2370 case snd_soc_dapm_post:
2371 case snd_soc_dapm_supply:
2372 case snd_soc_dapm_regulator_supply:
2373 case snd_soc_dapm_clock_supply:
2374 case snd_soc_dapm_aif_in:
2375 case snd_soc_dapm_aif_out:
2376 case snd_soc_dapm_dai_in:
2377 case snd_soc_dapm_dai_out:
2378 case snd_soc_dapm_dai_link:
2379 case snd_soc_dapm_kcontrol:
2380 list_add(&path->list, &dapm->card->paths);
2381 list_add(&path->list_sink, &wsink->sources);
2382 list_add(&path->list_source, &wsource->sinks);
2385 case snd_soc_dapm_mux:
2386 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2387 &wsink->kcontrol_news[0]);
2391 case snd_soc_dapm_switch:
2392 case snd_soc_dapm_mixer:
2393 case snd_soc_dapm_mixer_named_ctl:
2394 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2398 case snd_soc_dapm_hp:
2399 case snd_soc_dapm_mic:
2400 case snd_soc_dapm_line:
2401 case snd_soc_dapm_spk:
2402 list_add(&path->list, &dapm->card->paths);
2403 list_add(&path->list_sink, &wsink->sources);
2404 list_add(&path->list_source, &wsource->sinks);
2415 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2416 const struct snd_soc_dapm_route *route)
2418 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2419 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2422 char prefixed_sink[80];
2423 char prefixed_source[80];
2427 prefix = soc_dapm_prefix(dapm);
2429 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2430 prefix, route->sink);
2431 sink = prefixed_sink;
2432 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2433 prefix, route->source);
2434 source = prefixed_source;
2437 source = route->source;
2441 * find src and dest widgets over all widgets but favor a widget from
2442 * current DAPM context
2444 list_for_each_entry(w, &dapm->card->widgets, list) {
2445 if (!wsink && !(strcmp(w->name, sink))) {
2447 if (w->dapm == dapm)
2451 if (!wsource && !(strcmp(w->name, source))) {
2453 if (w->dapm == dapm)
2457 /* use widget from another DAPM context if not found from this */
2463 if (wsource == NULL) {
2464 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2468 if (wsink == NULL) {
2469 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2474 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2481 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2482 source, route->control, sink);
2486 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2487 const struct snd_soc_dapm_route *route)
2489 struct snd_soc_dapm_path *path, *p;
2492 char prefixed_sink[80];
2493 char prefixed_source[80];
2496 if (route->control) {
2498 "ASoC: Removal of routes with controls not supported\n");
2502 prefix = soc_dapm_prefix(dapm);
2504 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2505 prefix, route->sink);
2506 sink = prefixed_sink;
2507 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2508 prefix, route->source);
2509 source = prefixed_source;
2512 source = route->source;
2516 list_for_each_entry(p, &dapm->card->paths, list) {
2517 if (strcmp(p->source->name, source) != 0)
2519 if (strcmp(p->sink->name, sink) != 0)
2526 dapm_mark_dirty(path->source, "Route removed");
2527 dapm_mark_dirty(path->sink, "Route removed");
2529 dapm_free_path(path);
2531 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2539 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2540 * @dapm: DAPM context
2541 * @route: audio routes
2542 * @num: number of routes
2544 * Connects 2 dapm widgets together via a named audio path. The sink is
2545 * the widget receiving the audio signal, whilst the source is the sender
2546 * of the audio signal.
2548 * Returns 0 for success else error. On error all resources can be freed
2549 * with a call to snd_soc_card_free().
2551 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2552 const struct snd_soc_dapm_route *route, int num)
2556 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2557 for (i = 0; i < num; i++) {
2558 r = snd_soc_dapm_add_route(dapm, route);
2560 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2562 route->control ? route->control : "direct",
2568 mutex_unlock(&dapm->card->dapm_mutex);
2572 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2575 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2576 * @dapm: DAPM context
2577 * @route: audio routes
2578 * @num: number of routes
2580 * Removes routes from the DAPM context.
2582 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2583 const struct snd_soc_dapm_route *route, int num)
2587 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2588 for (i = 0; i < num; i++) {
2589 snd_soc_dapm_del_route(dapm, route);
2592 mutex_unlock(&dapm->card->dapm_mutex);
2596 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2598 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2599 const struct snd_soc_dapm_route *route)
2601 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2604 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2607 struct snd_soc_dapm_path *path;
2611 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2617 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2622 if (route->control || route->connected)
2623 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2624 route->source, route->sink);
2626 list_for_each_entry(path, &source->sinks, list_source) {
2627 if (path->sink == sink) {
2634 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2635 route->source, route->sink);
2637 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2638 count, route->source, route->sink);
2644 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2645 * @dapm: DAPM context
2646 * @route: audio routes
2647 * @num: number of routes
2649 * Mark existing routes matching those specified in the passed array
2650 * as being weak, meaning that they are ignored for the purpose of
2651 * power decisions. The main intended use case is for sidetone paths
2652 * which couple audio between other independent paths if they are both
2653 * active in order to make the combination work better at the user
2654 * level but which aren't intended to be "used".
2656 * Note that CODEC drivers should not use this as sidetone type paths
2657 * can frequently also be used as bypass paths.
2659 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2660 const struct snd_soc_dapm_route *route, int num)
2665 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2666 for (i = 0; i < num; i++) {
2667 err = snd_soc_dapm_weak_route(dapm, route);
2672 mutex_unlock(&dapm->card->dapm_mutex);
2676 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2679 * snd_soc_dapm_new_widgets - add new dapm widgets
2680 * @dapm: DAPM context
2682 * Checks the codec for any new dapm widgets and creates them if found.
2684 * Returns 0 for success.
2686 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2688 struct snd_soc_dapm_widget *w;
2691 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2693 list_for_each_entry(w, &card->widgets, list)
2698 if (w->num_kcontrols) {
2699 w->kcontrols = kzalloc(w->num_kcontrols *
2700 sizeof(struct snd_kcontrol *),
2702 if (!w->kcontrols) {
2703 mutex_unlock(&card->dapm_mutex);
2709 case snd_soc_dapm_switch:
2710 case snd_soc_dapm_mixer:
2711 case snd_soc_dapm_mixer_named_ctl:
2714 case snd_soc_dapm_mux:
2717 case snd_soc_dapm_pga:
2718 case snd_soc_dapm_out_drv:
2725 /* Read the initial power state from the device */
2727 soc_dapm_read(w->dapm, w->reg, &val);
2728 val = val >> w->shift;
2730 if (val == w->on_val)
2736 dapm_mark_dirty(w, "new widget");
2737 dapm_debugfs_add_widget(w);
2740 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2741 mutex_unlock(&card->dapm_mutex);
2744 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2747 * snd_soc_dapm_get_volsw - dapm mixer get callback
2748 * @kcontrol: mixer control
2749 * @ucontrol: control element information
2751 * Callback to get the value of a dapm mixer control.
2753 * Returns 0 for success.
2755 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2756 struct snd_ctl_elem_value *ucontrol)
2758 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2759 struct snd_soc_card *card = dapm->card;
2760 struct soc_mixer_control *mc =
2761 (struct soc_mixer_control *)kcontrol->private_value;
2763 unsigned int shift = mc->shift;
2765 unsigned int mask = (1 << fls(max)) - 1;
2766 unsigned int invert = mc->invert;
2770 if (snd_soc_volsw_is_stereo(mc))
2772 "ASoC: Control '%s' is stereo, which is not supported\n",
2775 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2776 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2777 ret = soc_dapm_read(dapm, reg, &val);
2778 val = (val >> shift) & mask;
2780 val = dapm_kcontrol_get_value(kcontrol);
2782 mutex_unlock(&card->dapm_mutex);
2785 ucontrol->value.integer.value[0] = max - val;
2787 ucontrol->value.integer.value[0] = val;
2791 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2794 * snd_soc_dapm_put_volsw - dapm mixer set callback
2795 * @kcontrol: mixer control
2796 * @ucontrol: control element information
2798 * Callback to set the value of a dapm mixer control.
2800 * Returns 0 for success.
2802 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2805 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2806 struct snd_soc_card *card = dapm->card;
2807 struct soc_mixer_control *mc =
2808 (struct soc_mixer_control *)kcontrol->private_value;
2810 unsigned int shift = mc->shift;
2812 unsigned int mask = (1 << fls(max)) - 1;
2813 unsigned int invert = mc->invert;
2815 int connect, change, reg_change = 0;
2816 struct snd_soc_dapm_update update;
2819 if (snd_soc_volsw_is_stereo(mc))
2821 "ASoC: Control '%s' is stereo, which is not supported\n",
2824 val = (ucontrol->value.integer.value[0] & mask);
2830 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2832 change = dapm_kcontrol_set_value(kcontrol, val);
2834 if (reg != SND_SOC_NOPM) {
2835 mask = mask << shift;
2838 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2841 if (change || reg_change) {
2843 update.kcontrol = kcontrol;
2847 card->update = &update;
2849 change |= reg_change;
2851 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2853 card->update = NULL;
2856 mutex_unlock(&card->dapm_mutex);
2859 soc_dpcm_runtime_update(card);
2863 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2866 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2867 * @kcontrol: mixer control
2868 * @ucontrol: control element information
2870 * Callback to get the value of a dapm enumerated double mixer control.
2872 * Returns 0 for success.
2874 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2877 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2878 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2879 unsigned int reg_val, val;
2881 if (e->reg != SND_SOC_NOPM) {
2882 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2886 reg_val = dapm_kcontrol_get_value(kcontrol);
2889 val = (reg_val >> e->shift_l) & e->mask;
2890 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2891 if (e->shift_l != e->shift_r) {
2892 val = (reg_val >> e->shift_r) & e->mask;
2893 val = snd_soc_enum_val_to_item(e, val);
2894 ucontrol->value.enumerated.item[1] = val;
2899 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2902 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2903 * @kcontrol: mixer control
2904 * @ucontrol: control element information
2906 * Callback to set the value of a dapm enumerated double mixer control.
2908 * Returns 0 for success.
2910 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
2913 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2914 struct snd_soc_card *card = dapm->card;
2915 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2916 unsigned int *item = ucontrol->value.enumerated.item;
2917 unsigned int val, change;
2919 struct snd_soc_dapm_update update;
2922 if (item[0] >= e->items)
2925 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2926 mask = e->mask << e->shift_l;
2927 if (e->shift_l != e->shift_r) {
2928 if (item[1] > e->items)
2930 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2931 mask |= e->mask << e->shift_r;
2934 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2936 if (e->reg != SND_SOC_NOPM)
2937 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2939 change = dapm_kcontrol_set_value(kcontrol, val);
2942 if (e->reg != SND_SOC_NOPM) {
2943 update.kcontrol = kcontrol;
2944 update.reg = e->reg;
2947 card->update = &update;
2950 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2952 card->update = NULL;
2955 mutex_unlock(&card->dapm_mutex);
2958 soc_dpcm_runtime_update(card);
2962 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2965 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2967 * @kcontrol: mixer control
2968 * @uinfo: control element information
2970 * Callback to provide information about a pin switch control.
2972 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_info *uinfo)
2975 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2977 uinfo->value.integer.min = 0;
2978 uinfo->value.integer.max = 1;
2982 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2985 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2987 * @kcontrol: mixer control
2990 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2993 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2994 const char *pin = (const char *)kcontrol->private_value;
2996 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2998 ucontrol->value.integer.value[0] =
2999 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3001 mutex_unlock(&card->dapm_mutex);
3005 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3008 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3010 * @kcontrol: mixer control
3013 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
3016 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3017 const char *pin = (const char *)kcontrol->private_value;
3019 if (ucontrol->value.integer.value[0])
3020 snd_soc_dapm_enable_pin(&card->dapm, pin);
3022 snd_soc_dapm_disable_pin(&card->dapm, pin);
3024 snd_soc_dapm_sync(&card->dapm);
3027 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3029 static struct snd_soc_dapm_widget *
3030 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3031 const struct snd_soc_dapm_widget *widget)
3033 struct snd_soc_dapm_widget *w;
3037 if ((w = dapm_cnew_widget(widget)) == NULL)
3041 case snd_soc_dapm_regulator_supply:
3042 w->regulator = devm_regulator_get(dapm->dev, w->name);
3043 if (IS_ERR(w->regulator)) {
3044 ret = PTR_ERR(w->regulator);
3045 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3050 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3051 ret = regulator_allow_bypass(w->regulator, true);
3053 dev_warn(w->dapm->dev,
3054 "ASoC: Failed to bypass %s: %d\n",
3058 case snd_soc_dapm_clock_supply:
3059 #ifdef CONFIG_CLKDEV_LOOKUP
3060 w->clk = devm_clk_get(dapm->dev, w->name);
3061 if (IS_ERR(w->clk)) {
3062 ret = PTR_ERR(w->clk);
3063 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3075 prefix = soc_dapm_prefix(dapm);
3077 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3079 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3081 if (w->name == NULL) {
3087 case snd_soc_dapm_switch:
3088 case snd_soc_dapm_mixer:
3089 case snd_soc_dapm_mixer_named_ctl:
3090 w->power_check = dapm_generic_check_power;
3092 case snd_soc_dapm_mux:
3093 w->power_check = dapm_generic_check_power;
3095 case snd_soc_dapm_dai_out:
3096 w->power_check = dapm_adc_check_power;
3098 case snd_soc_dapm_dai_in:
3099 w->power_check = dapm_dac_check_power;
3101 case snd_soc_dapm_adc:
3102 case snd_soc_dapm_aif_out:
3103 case snd_soc_dapm_dac:
3104 case snd_soc_dapm_aif_in:
3105 case snd_soc_dapm_pga:
3106 case snd_soc_dapm_out_drv:
3107 case snd_soc_dapm_input:
3108 case snd_soc_dapm_output:
3109 case snd_soc_dapm_micbias:
3110 case snd_soc_dapm_spk:
3111 case snd_soc_dapm_hp:
3112 case snd_soc_dapm_mic:
3113 case snd_soc_dapm_line:
3114 case snd_soc_dapm_dai_link:
3115 w->power_check = dapm_generic_check_power;
3117 case snd_soc_dapm_supply:
3118 case snd_soc_dapm_regulator_supply:
3119 case snd_soc_dapm_clock_supply:
3120 case snd_soc_dapm_kcontrol:
3121 w->power_check = dapm_supply_check_power;
3124 w->power_check = dapm_always_on_check_power;
3129 if (dapm->component)
3130 w->codec = dapm->component->codec;
3131 INIT_LIST_HEAD(&w->sources);
3132 INIT_LIST_HEAD(&w->sinks);
3133 INIT_LIST_HEAD(&w->list);
3134 INIT_LIST_HEAD(&w->dirty);
3135 list_add(&w->list, &dapm->card->widgets);
3137 /* machine layer set ups unconnected pins and insertions */
3143 * snd_soc_dapm_new_controls - create new dapm controls
3144 * @dapm: DAPM context
3145 * @widget: widget array
3146 * @num: number of widgets
3148 * Creates new DAPM controls based upon the templates.
3150 * Returns 0 for success else error.
3152 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3153 const struct snd_soc_dapm_widget *widget,
3156 struct snd_soc_dapm_widget *w;
3160 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3161 for (i = 0; i < num; i++) {
3162 w = snd_soc_dapm_new_control(dapm, widget);
3165 "ASoC: Failed to create DAPM control %s\n",
3172 mutex_unlock(&dapm->card->dapm_mutex);
3175 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3177 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3178 struct snd_kcontrol *kcontrol, int event)
3180 struct snd_soc_dapm_path *source_p, *sink_p;
3181 struct snd_soc_dai *source, *sink;
3182 const struct snd_soc_pcm_stream *config = w->params;
3183 struct snd_pcm_substream substream;
3184 struct snd_pcm_hw_params *params = NULL;
3188 if (WARN_ON(!config) ||
3189 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3192 /* We only support a single source and sink, pick the first */
3193 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3195 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3198 if (WARN_ON(!source_p || !sink_p) ||
3199 WARN_ON(!sink_p->source || !source_p->sink) ||
3200 WARN_ON(!source_p->source || !sink_p->sink))
3203 source = source_p->source->priv;
3204 sink = sink_p->sink->priv;
3206 /* Be a little careful as we don't want to overflow the mask array */
3207 if (config->formats) {
3208 fmt = ffs(config->formats) - 1;
3210 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3215 /* Currently very limited parameter selection */
3216 params = kzalloc(sizeof(*params), GFP_KERNEL);
3221 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3223 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3225 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3228 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3229 = config->channels_min;
3230 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3231 = config->channels_max;
3233 memset(&substream, 0, sizeof(substream));
3236 case SND_SOC_DAPM_PRE_PMU:
3237 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3238 ret = soc_dai_hw_params(&substream, params, source);
3242 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3243 ret = soc_dai_hw_params(&substream, params, sink);
3248 case SND_SOC_DAPM_POST_PMU:
3249 ret = snd_soc_dai_digital_mute(sink, 0,
3250 SNDRV_PCM_STREAM_PLAYBACK);
3251 if (ret != 0 && ret != -ENOTSUPP)
3252 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3256 case SND_SOC_DAPM_PRE_PMD:
3257 ret = snd_soc_dai_digital_mute(sink, 1,
3258 SNDRV_PCM_STREAM_PLAYBACK);
3259 if (ret != 0 && ret != -ENOTSUPP)
3260 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3265 WARN(1, "Unknown event %d\n", event);
3274 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3275 const struct snd_soc_pcm_stream *params,
3276 struct snd_soc_dapm_widget *source,
3277 struct snd_soc_dapm_widget *sink)
3279 struct snd_soc_dapm_widget template;
3280 struct snd_soc_dapm_widget *w;
3285 len = strlen(source->name) + strlen(sink->name) + 2;
3286 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3289 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3291 memset(&template, 0, sizeof(template));
3292 template.reg = SND_SOC_NOPM;
3293 template.id = snd_soc_dapm_dai_link;
3294 template.name = link_name;
3295 template.event = snd_soc_dai_link_event;
3296 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3297 SND_SOC_DAPM_PRE_PMD;
3299 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3301 w = snd_soc_dapm_new_control(&card->dapm, &template);
3303 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3310 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3313 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3316 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3317 struct snd_soc_dai *dai)
3319 struct snd_soc_dapm_widget template;
3320 struct snd_soc_dapm_widget *w;
3322 WARN_ON(dapm->dev != dai->dev);
3324 memset(&template, 0, sizeof(template));
3325 template.reg = SND_SOC_NOPM;
3327 if (dai->driver->playback.stream_name) {
3328 template.id = snd_soc_dapm_dai_in;
3329 template.name = dai->driver->playback.stream_name;
3330 template.sname = dai->driver->playback.stream_name;
3332 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3335 w = snd_soc_dapm_new_control(dapm, &template);
3337 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3338 dai->driver->playback.stream_name);
3343 dai->playback_widget = w;
3346 if (dai->driver->capture.stream_name) {
3347 template.id = snd_soc_dapm_dai_out;
3348 template.name = dai->driver->capture.stream_name;
3349 template.sname = dai->driver->capture.stream_name;
3351 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3354 w = snd_soc_dapm_new_control(dapm, &template);
3356 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3357 dai->driver->capture.stream_name);
3362 dai->capture_widget = w;
3368 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3370 struct snd_soc_dapm_widget *dai_w, *w;
3371 struct snd_soc_dapm_widget *src, *sink;
3372 struct snd_soc_dai *dai;
3374 /* For each DAI widget... */
3375 list_for_each_entry(dai_w, &card->widgets, list) {
3376 switch (dai_w->id) {
3377 case snd_soc_dapm_dai_in:
3378 case snd_soc_dapm_dai_out:
3386 /* ...find all widgets with the same stream and link them */
3387 list_for_each_entry(w, &card->widgets, list) {
3388 if (w->dapm != dai_w->dapm)
3392 case snd_soc_dapm_dai_in:
3393 case snd_soc_dapm_dai_out:
3399 if (!w->sname || !strstr(w->sname, dai_w->name))
3402 if (dai_w->id == snd_soc_dapm_dai_in) {
3409 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3410 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3417 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3418 struct snd_soc_pcm_runtime *rtd)
3420 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3421 struct snd_soc_dapm_widget *sink, *source;
3424 for (i = 0; i < rtd->num_codecs; i++) {
3425 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3427 /* there is no point in connecting BE DAI links with dummies */
3428 if (snd_soc_dai_is_dummy(codec_dai) ||
3429 snd_soc_dai_is_dummy(cpu_dai))
3432 /* connect BE DAI playback if widgets are valid */
3433 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3434 source = cpu_dai->playback_widget;
3435 sink = codec_dai->playback_widget;
3436 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3437 cpu_dai->component->name, source->name,
3438 codec_dai->component->name, sink->name);
3440 snd_soc_dapm_add_path(&card->dapm, source, sink,
3444 /* connect BE DAI capture if widgets are valid */
3445 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3446 source = codec_dai->capture_widget;
3447 sink = cpu_dai->capture_widget;
3448 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3449 codec_dai->component->name, source->name,
3450 cpu_dai->component->name, sink->name);
3452 snd_soc_dapm_add_path(&card->dapm, source, sink,
3458 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3461 struct snd_soc_dapm_widget *w;
3463 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3464 w = dai->playback_widget;
3466 w = dai->capture_widget;
3469 dapm_mark_dirty(w, "stream event");
3472 case SND_SOC_DAPM_STREAM_START:
3475 case SND_SOC_DAPM_STREAM_STOP:
3478 case SND_SOC_DAPM_STREAM_SUSPEND:
3479 case SND_SOC_DAPM_STREAM_RESUME:
3480 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3481 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3487 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3489 struct snd_soc_pcm_runtime *rtd = card->rtd;
3492 /* for each BE DAI link... */
3493 for (i = 0; i < card->num_rtd; i++) {
3494 rtd = &card->rtd[i];
3497 * dynamic FE links have no fixed DAI mapping.
3498 * CODEC<->CODEC links have no direct connection.
3500 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3503 dapm_connect_dai_link_widgets(card, rtd);
3507 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3512 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3513 for (i = 0; i < rtd->num_codecs; i++)
3514 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3516 dapm_power_widgets(rtd->card, event);
3520 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3521 * @rtd: PCM runtime data
3522 * @stream: stream name
3523 * @event: stream event
3525 * Sends a stream event to the dapm core. The core then makes any
3526 * necessary widget power changes.
3528 * Returns 0 for success else error.
3530 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3533 struct snd_soc_card *card = rtd->card;
3535 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3536 soc_dapm_stream_event(rtd, stream, event);
3537 mutex_unlock(&card->dapm_mutex);
3541 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3542 * @dapm: DAPM context
3545 * Enables input/output pin and its parents or children widgets iff there is
3546 * a valid audio route and active audio stream.
3548 * Requires external locking.
3550 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3551 * do any widget power switching.
3553 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3556 return snd_soc_dapm_set_pin(dapm, pin, 1);
3558 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3561 * snd_soc_dapm_enable_pin - enable pin.
3562 * @dapm: DAPM context
3565 * Enables input/output pin and its parents or children widgets iff there is
3566 * a valid audio route and active audio stream.
3568 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3569 * do any widget power switching.
3571 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3575 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3577 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3579 mutex_unlock(&dapm->card->dapm_mutex);
3583 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3586 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3587 * @dapm: DAPM context
3590 * Enables input/output pin regardless of any other state. This is
3591 * intended for use with microphone bias supplies used in microphone
3594 * Requires external locking.
3596 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3597 * do any widget power switching.
3599 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3602 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3605 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3609 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3612 dapm_mark_dirty(w, "force enable");
3616 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3619 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3620 * @dapm: DAPM context
3623 * Enables input/output pin regardless of any other state. This is
3624 * intended for use with microphone bias supplies used in microphone
3627 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3628 * do any widget power switching.
3630 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3635 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3637 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3639 mutex_unlock(&dapm->card->dapm_mutex);
3643 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3646 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3647 * @dapm: DAPM context
3650 * Disables input/output pin and its parents or children widgets.
3652 * Requires external locking.
3654 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3655 * do any widget power switching.
3657 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3660 return snd_soc_dapm_set_pin(dapm, pin, 0);
3662 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3665 * snd_soc_dapm_disable_pin - disable pin.
3666 * @dapm: DAPM context
3669 * Disables input/output pin and its parents or children widgets.
3671 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3672 * do any widget power switching.
3674 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3679 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3681 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3683 mutex_unlock(&dapm->card->dapm_mutex);
3687 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3690 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3691 * @dapm: DAPM context
3694 * Marks the specified pin as being not connected, disabling it along
3695 * any parent or child widgets. At present this is identical to
3696 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3697 * additional things such as disabling controls which only affect
3698 * paths through the pin.
3700 * Requires external locking.
3702 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3703 * do any widget power switching.
3705 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3708 return snd_soc_dapm_set_pin(dapm, pin, 0);
3710 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3713 * snd_soc_dapm_nc_pin - permanently disable pin.
3714 * @dapm: DAPM context
3717 * Marks the specified pin as being not connected, disabling it along
3718 * any parent or child widgets. At present this is identical to
3719 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3720 * additional things such as disabling controls which only affect
3721 * paths through the pin.
3723 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3724 * do any widget power switching.
3726 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3730 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3732 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3734 mutex_unlock(&dapm->card->dapm_mutex);
3738 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3741 * snd_soc_dapm_get_pin_status - get audio pin status
3742 * @dapm: DAPM context
3743 * @pin: audio signal pin endpoint (or start point)
3745 * Get audio pin status - connected or disconnected.
3747 * Returns 1 for connected otherwise 0.
3749 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3752 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3755 return w->connected;
3759 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3762 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3763 * @dapm: DAPM context
3764 * @pin: audio signal pin endpoint (or start point)
3766 * Mark the given endpoint or pin as ignoring suspend. When the
3767 * system is disabled a path between two endpoints flagged as ignoring
3768 * suspend will not be disabled. The path must already be enabled via
3769 * normal means at suspend time, it will not be turned on if it was not
3772 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3775 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3778 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3782 w->ignore_suspend = 1;
3786 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3788 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3789 struct snd_soc_dapm_widget *w)
3791 struct snd_soc_dapm_path *p;
3793 list_for_each_entry(p, &card->paths, list) {
3794 if ((p->source == w) || (p->sink == w)) {
3796 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3797 p->source->name, p->source->id, p->source->dapm,
3798 p->sink->name, p->sink->id, p->sink->dapm);
3800 /* Connected to something other than the codec */
3801 if (p->source->dapm != p->sink->dapm)
3804 * Loopback connection from codec external pin to
3805 * codec external pin
3807 if (p->sink->id == snd_soc_dapm_input) {
3808 switch (p->source->id) {
3809 case snd_soc_dapm_output:
3810 case snd_soc_dapm_micbias:
3823 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3824 * @card: The card whose pins should be processed
3826 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3827 * which are unused. Pins are used if they are connected externally to a
3828 * component, whether that be to some other device, or a loop-back connection to
3829 * the component itself.
3831 void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
3833 struct snd_soc_dapm_widget *w;
3835 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
3837 list_for_each_entry(w, &card->widgets, list) {
3839 case snd_soc_dapm_input:
3840 case snd_soc_dapm_output:
3841 case snd_soc_dapm_micbias:
3842 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
3844 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3846 "... Not in map; disabling\n");
3847 snd_soc_dapm_nc_pin(w->dapm, w->name);
3857 * snd_soc_dapm_free - free dapm resources
3858 * @dapm: DAPM context
3860 * Free all dapm widgets and resources.
3862 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3864 snd_soc_dapm_sys_remove(dapm->dev);
3865 dapm_debugfs_cleanup(dapm);
3866 dapm_free_widgets(dapm);
3867 list_del(&dapm->list);
3869 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3871 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3873 struct snd_soc_card *card = dapm->card;
3874 struct snd_soc_dapm_widget *w;
3875 LIST_HEAD(down_list);
3878 mutex_lock(&card->dapm_mutex);
3880 list_for_each_entry(w, &dapm->card->widgets, list) {
3881 if (w->dapm != dapm)
3884 dapm_seq_insert(w, &down_list, false);
3890 /* If there were no widgets to power down we're already in
3894 if (dapm->bias_level == SND_SOC_BIAS_ON)
3895 snd_soc_dapm_set_bias_level(dapm,
3896 SND_SOC_BIAS_PREPARE);
3897 dapm_seq_run(card, &down_list, 0, false);
3898 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3899 snd_soc_dapm_set_bias_level(dapm,
3900 SND_SOC_BIAS_STANDBY);
3903 mutex_unlock(&card->dapm_mutex);
3907 * snd_soc_dapm_shutdown - callback for system shutdown
3909 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3911 struct snd_soc_dapm_context *dapm;
3913 list_for_each_entry(dapm, &card->dapm_list, list) {
3914 if (dapm != &card->dapm) {
3915 soc_dapm_shutdown_dapm(dapm);
3916 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3917 snd_soc_dapm_set_bias_level(dapm,
3922 soc_dapm_shutdown_dapm(&card->dapm);
3923 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3924 snd_soc_dapm_set_bias_level(&card->dapm,
3928 /* Module information */
3929 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3930 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3931 MODULE_LICENSE("GPL");