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 static unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
331 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
336 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
339 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
341 if (data->value == value)
345 data->widget->on_val = value;
353 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
355 * @kcontrol: The kcontrol
357 * Note: This function must only be used on kcontrols that are known to have
358 * been registered for a CODEC. Otherwise the behaviour is undefined.
360 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
361 struct snd_kcontrol *kcontrol)
363 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
365 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
368 * snd_soc_dapm_kcontrol_codec() - Returns the codec associated to a kcontrol
369 * @kcontrol: The kcontrol
371 struct snd_soc_codec *snd_soc_dapm_kcontrol_codec(struct snd_kcontrol *kcontrol)
373 return snd_soc_dapm_to_codec(snd_soc_dapm_kcontrol_dapm(kcontrol));
375 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_codec);
377 static void dapm_reset(struct snd_soc_card *card)
379 struct snd_soc_dapm_widget *w;
381 lockdep_assert_held(&card->dapm_mutex);
383 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
385 list_for_each_entry(w, &card->widgets, list) {
386 w->new_power = w->power;
387 w->power_checked = false;
393 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
395 if (!dapm->component)
397 return dapm->component->name_prefix;
400 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
403 if (!dapm->component)
405 return snd_soc_component_read(dapm->component, reg, value);
408 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
409 int reg, unsigned int mask, unsigned int value)
411 if (!dapm->component)
413 return snd_soc_component_update_bits_async(dapm->component, reg,
417 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
418 int reg, unsigned int mask, unsigned int value)
420 if (!dapm->component)
422 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
425 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
428 snd_soc_component_async_complete(dapm->component);
432 * snd_soc_dapm_set_bias_level - set the bias level for the system
433 * @dapm: DAPM context
434 * @level: level to configure
436 * Configure the bias (power) levels for the SoC audio device.
438 * Returns 0 for success else error.
440 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
441 enum snd_soc_bias_level level)
443 struct snd_soc_card *card = dapm->card;
446 trace_snd_soc_bias_level_start(card, level);
448 if (card && card->set_bias_level)
449 ret = card->set_bias_level(card, dapm, level);
453 if (dapm->set_bias_level)
454 ret = dapm->set_bias_level(dapm, level);
455 else if (!card || dapm != &card->dapm)
456 dapm->bias_level = level;
461 if (card && card->set_bias_level_post)
462 ret = card->set_bias_level_post(card, dapm, level);
464 trace_snd_soc_bias_level_done(card, level);
469 /* connect mux widget to its interconnecting audio paths */
470 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
471 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
472 struct snd_soc_dapm_path *path, const char *control_name,
473 const struct snd_kcontrol_new *kcontrol)
475 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
476 unsigned int val, item;
479 if (e->reg != SND_SOC_NOPM) {
480 soc_dapm_read(dapm, e->reg, &val);
481 val = (val >> e->shift_l) & e->mask;
482 item = snd_soc_enum_val_to_item(e, val);
484 /* since a virtual mux has no backing registers to
485 * decide which path to connect, it will try to match
486 * with the first enumeration. This is to ensure
487 * that the default mux choice (the first) will be
488 * correctly powered up during initialization.
493 for (i = 0; i < e->items; i++) {
494 if (!(strcmp(control_name, e->texts[i]))) {
495 list_add(&path->list, &dapm->card->paths);
496 list_add(&path->list_sink, &dest->sources);
497 list_add(&path->list_source, &src->sinks);
498 path->name = (char*)e->texts[i];
510 /* set up initial codec paths */
511 static void dapm_set_mixer_path_status(struct snd_soc_dapm_widget *w,
512 struct snd_soc_dapm_path *p, int i)
514 struct soc_mixer_control *mc = (struct soc_mixer_control *)
515 w->kcontrol_news[i].private_value;
516 unsigned int reg = mc->reg;
517 unsigned int shift = mc->shift;
518 unsigned int max = mc->max;
519 unsigned int mask = (1 << fls(max)) - 1;
520 unsigned int invert = mc->invert;
523 if (reg != SND_SOC_NOPM) {
524 soc_dapm_read(w->dapm, reg, &val);
525 val = (val >> shift) & mask;
534 /* connect mixer widget to its interconnecting audio paths */
535 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
536 struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
537 struct snd_soc_dapm_path *path, const char *control_name)
541 /* search for mixer kcontrol */
542 for (i = 0; i < dest->num_kcontrols; i++) {
543 if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
544 list_add(&path->list, &dapm->card->paths);
545 list_add(&path->list_sink, &dest->sources);
546 list_add(&path->list_source, &src->sinks);
547 path->name = dest->kcontrol_news[i].name;
548 dapm_set_mixer_path_status(dest, path, i);
555 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
556 struct snd_soc_dapm_widget *kcontrolw,
557 const struct snd_kcontrol_new *kcontrol_new,
558 struct snd_kcontrol **kcontrol)
560 struct snd_soc_dapm_widget *w;
565 list_for_each_entry(w, &dapm->card->widgets, list) {
566 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
568 for (i = 0; i < w->num_kcontrols; i++) {
569 if (&w->kcontrol_news[i] == kcontrol_new) {
571 *kcontrol = w->kcontrols[i];
581 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
582 * create it. Either way, add the widget into the control's widget list
584 static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
587 struct snd_soc_dapm_context *dapm = w->dapm;
588 struct snd_card *card = dapm->card->snd_card;
592 struct snd_kcontrol *kcontrol;
593 bool wname_in_long_name, kcname_in_long_name;
598 prefix = soc_dapm_prefix(dapm);
600 prefix_len = strlen(prefix) + 1;
604 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
609 wname_in_long_name = false;
610 kcname_in_long_name = true;
613 case snd_soc_dapm_switch:
614 case snd_soc_dapm_mixer:
615 wname_in_long_name = true;
616 kcname_in_long_name = true;
618 case snd_soc_dapm_mixer_named_ctl:
619 wname_in_long_name = false;
620 kcname_in_long_name = true;
622 case snd_soc_dapm_mux:
623 wname_in_long_name = true;
624 kcname_in_long_name = false;
631 if (wname_in_long_name && kcname_in_long_name) {
633 * The control will get a prefix from the control
634 * creation process but we're also using the same
635 * prefix for widgets so cut the prefix off the
636 * front of the widget name.
638 long_name = kasprintf(GFP_KERNEL, "%s %s",
639 w->name + prefix_len,
640 w->kcontrol_news[kci].name);
641 if (long_name == NULL)
645 } else if (wname_in_long_name) {
647 name = w->name + prefix_len;
650 name = w->kcontrol_news[kci].name;
653 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
658 kcontrol->private_free = dapm_kcontrol_free;
660 ret = dapm_kcontrol_data_alloc(w, kcontrol);
662 snd_ctl_free_one(kcontrol);
666 ret = snd_ctl_add(card, kcontrol);
669 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
675 ret = dapm_kcontrol_add_widget(kcontrol, w);
679 w->kcontrols[kci] = kcontrol;
684 /* create new dapm mixer control */
685 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
688 struct snd_soc_dapm_path *path;
691 for (i = 0; i < w->num_kcontrols; i++) {
693 list_for_each_entry(path, &w->sources, list_sink) {
694 /* mixer/mux paths name must match control name */
695 if (path->name != (char *)w->kcontrol_news[i].name)
698 if (w->kcontrols[i]) {
699 dapm_kcontrol_add_path(w->kcontrols[i], path);
703 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
707 dapm_kcontrol_add_path(w->kcontrols[i], path);
714 /* create new dapm mux control */
715 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
717 struct snd_soc_dapm_context *dapm = w->dapm;
718 struct snd_soc_dapm_path *path;
721 if (w->num_kcontrols != 1) {
723 "ASoC: mux %s has incorrect number of controls\n",
728 if (list_empty(&w->sources)) {
729 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
733 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
737 list_for_each_entry(path, &w->sources, list_sink)
738 dapm_kcontrol_add_path(w->kcontrols[0], path);
743 /* create new dapm volume control */
744 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
746 if (w->num_kcontrols)
747 dev_err(w->dapm->dev,
748 "ASoC: PGA controls not supported: '%s'\n", w->name);
753 /* reset 'walked' bit for each dapm path */
754 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
755 struct list_head *sink)
757 struct snd_soc_dapm_path *p;
759 list_for_each_entry(p, sink, list_source) {
762 dapm_clear_walk_output(dapm, &p->sink->sinks);
767 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
768 struct list_head *source)
770 struct snd_soc_dapm_path *p;
772 list_for_each_entry(p, source, list_sink) {
775 dapm_clear_walk_input(dapm, &p->source->sources);
781 /* We implement power down on suspend by checking the power state of
782 * the ALSA card - when we are suspending the ALSA state for the card
785 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
787 int level = snd_power_get_state(widget->dapm->card->snd_card);
790 case SNDRV_CTL_POWER_D3hot:
791 case SNDRV_CTL_POWER_D3cold:
792 if (widget->ignore_suspend)
793 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
795 return widget->ignore_suspend;
801 /* add widget to list if it's not already in the list */
802 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
803 struct snd_soc_dapm_widget *w)
805 struct snd_soc_dapm_widget_list *wlist;
806 int wlistsize, wlistentries, i;
813 /* is this widget already in the list */
814 for (i = 0; i < wlist->num_widgets; i++) {
815 if (wlist->widgets[i] == w)
819 /* allocate some new space */
820 wlistentries = wlist->num_widgets + 1;
821 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
822 wlistentries * sizeof(struct snd_soc_dapm_widget *);
823 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
825 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
831 /* insert the widget */
832 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
833 w->name, wlist->num_widgets);
835 wlist->widgets[wlist->num_widgets] = w;
836 wlist->num_widgets++;
841 * Recursively check for a completed path to an active or physically connected
842 * output widget. Returns number of complete paths.
844 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
845 struct snd_soc_dapm_widget_list **list)
847 struct snd_soc_dapm_path *path;
850 if (widget->outputs >= 0)
851 return widget->outputs;
853 DAPM_UPDATE_STAT(widget, path_checks);
855 switch (widget->id) {
856 case snd_soc_dapm_supply:
857 case snd_soc_dapm_regulator_supply:
858 case snd_soc_dapm_clock_supply:
859 case snd_soc_dapm_kcontrol:
865 switch (widget->id) {
866 case snd_soc_dapm_adc:
867 case snd_soc_dapm_aif_out:
868 case snd_soc_dapm_dai_out:
869 if (widget->active) {
870 widget->outputs = snd_soc_dapm_suspend_check(widget);
871 return widget->outputs;
877 if (widget->connected) {
878 /* connected pin ? */
879 if (widget->id == snd_soc_dapm_output && !widget->ext) {
880 widget->outputs = snd_soc_dapm_suspend_check(widget);
881 return widget->outputs;
884 /* connected jack or spk ? */
885 if (widget->id == snd_soc_dapm_hp ||
886 widget->id == snd_soc_dapm_spk ||
887 (widget->id == snd_soc_dapm_line &&
888 !list_empty(&widget->sources))) {
889 widget->outputs = snd_soc_dapm_suspend_check(widget);
890 return widget->outputs;
894 list_for_each_entry(path, &widget->sinks, list_source) {
895 DAPM_UPDATE_STAT(widget, neighbour_checks);
906 trace_snd_soc_dapm_output_path(widget, path);
908 if (path->sink && path->connect) {
912 /* do we need to add this widget to the list ? */
915 err = dapm_list_add_widget(list, path->sink);
917 dev_err(widget->dapm->dev,
918 "ASoC: could not add widget %s\n",
925 con += is_connected_output_ep(path->sink, list);
931 widget->outputs = con;
937 * Recursively check for a completed path to an active or physically connected
938 * input widget. Returns number of complete paths.
940 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
941 struct snd_soc_dapm_widget_list **list)
943 struct snd_soc_dapm_path *path;
946 if (widget->inputs >= 0)
947 return widget->inputs;
949 DAPM_UPDATE_STAT(widget, path_checks);
951 switch (widget->id) {
952 case snd_soc_dapm_supply:
953 case snd_soc_dapm_regulator_supply:
954 case snd_soc_dapm_clock_supply:
955 case snd_soc_dapm_kcontrol:
961 /* active stream ? */
962 switch (widget->id) {
963 case snd_soc_dapm_dac:
964 case snd_soc_dapm_aif_in:
965 case snd_soc_dapm_dai_in:
966 if (widget->active) {
967 widget->inputs = snd_soc_dapm_suspend_check(widget);
968 return widget->inputs;
974 if (widget->connected) {
975 /* connected pin ? */
976 if (widget->id == snd_soc_dapm_input && !widget->ext) {
977 widget->inputs = snd_soc_dapm_suspend_check(widget);
978 return widget->inputs;
981 /* connected VMID/Bias for lower pops */
982 if (widget->id == snd_soc_dapm_vmid) {
983 widget->inputs = snd_soc_dapm_suspend_check(widget);
984 return widget->inputs;
987 /* connected jack ? */
988 if (widget->id == snd_soc_dapm_mic ||
989 (widget->id == snd_soc_dapm_line &&
990 !list_empty(&widget->sinks))) {
991 widget->inputs = snd_soc_dapm_suspend_check(widget);
992 return widget->inputs;
995 /* signal generator */
996 if (widget->id == snd_soc_dapm_siggen) {
997 widget->inputs = snd_soc_dapm_suspend_check(widget);
998 return widget->inputs;
1002 list_for_each_entry(path, &widget->sources, list_sink) {
1003 DAPM_UPDATE_STAT(widget, neighbour_checks);
1014 trace_snd_soc_dapm_input_path(widget, path);
1016 if (path->source && path->connect) {
1020 /* do we need to add this widget to the list ? */
1023 err = dapm_list_add_widget(list, path->source);
1025 dev_err(widget->dapm->dev,
1026 "ASoC: could not add widget %s\n",
1033 con += is_connected_input_ep(path->source, list);
1039 widget->inputs = con;
1045 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1046 * @dai: the soc DAI.
1047 * @stream: stream direction.
1048 * @list: list of active widgets for this stream.
1050 * Queries DAPM graph as to whether an valid audio stream path exists for
1051 * the initial stream specified by name. This takes into account
1052 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1054 * Returns the number of valid paths or negative error.
1056 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1057 struct snd_soc_dapm_widget_list **list)
1059 struct snd_soc_card *card = dai->card;
1062 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1065 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1066 paths = is_connected_output_ep(dai->playback_widget, list);
1067 dapm_clear_walk_output(&card->dapm,
1068 &dai->playback_widget->sinks);
1070 paths = is_connected_input_ep(dai->capture_widget, list);
1071 dapm_clear_walk_input(&card->dapm,
1072 &dai->capture_widget->sources);
1075 trace_snd_soc_dapm_connected(paths, stream);
1076 mutex_unlock(&card->dapm_mutex);
1082 * Handler for regulator supply widget.
1084 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1085 struct snd_kcontrol *kcontrol, int event)
1089 soc_dapm_async_complete(w->dapm);
1091 if (SND_SOC_DAPM_EVENT_ON(event)) {
1092 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1093 ret = regulator_allow_bypass(w->regulator, false);
1095 dev_warn(w->dapm->dev,
1096 "ASoC: Failed to unbypass %s: %d\n",
1100 return regulator_enable(w->regulator);
1102 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1103 ret = regulator_allow_bypass(w->regulator, true);
1105 dev_warn(w->dapm->dev,
1106 "ASoC: Failed to bypass %s: %d\n",
1110 return regulator_disable_deferred(w->regulator, w->shift);
1113 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1116 * Handler for clock supply widget.
1118 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1119 struct snd_kcontrol *kcontrol, int event)
1124 soc_dapm_async_complete(w->dapm);
1126 #ifdef CONFIG_HAVE_CLK
1127 if (SND_SOC_DAPM_EVENT_ON(event)) {
1128 return clk_prepare_enable(w->clk);
1130 clk_disable_unprepare(w->clk);
1136 EXPORT_SYMBOL_GPL(dapm_clock_event);
1138 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1140 if (w->power_checked)
1141 return w->new_power;
1146 w->new_power = w->power_check(w);
1148 w->power_checked = true;
1150 return w->new_power;
1153 /* Generic check to see if a widget should be powered.
1155 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1159 DAPM_UPDATE_STAT(w, power_checks);
1161 in = is_connected_input_ep(w, NULL);
1162 dapm_clear_walk_input(w->dapm, &w->sources);
1163 out = is_connected_output_ep(w, NULL);
1164 dapm_clear_walk_output(w->dapm, &w->sinks);
1165 return out != 0 && in != 0;
1168 /* Check to see if an ADC has power */
1169 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1173 DAPM_UPDATE_STAT(w, power_checks);
1176 in = is_connected_input_ep(w, NULL);
1177 dapm_clear_walk_input(w->dapm, &w->sources);
1180 return dapm_generic_check_power(w);
1184 /* Check to see if a DAC has power */
1185 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1189 DAPM_UPDATE_STAT(w, power_checks);
1192 out = is_connected_output_ep(w, NULL);
1193 dapm_clear_walk_output(w->dapm, &w->sinks);
1196 return dapm_generic_check_power(w);
1200 /* Check to see if a power supply is needed */
1201 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1203 struct snd_soc_dapm_path *path;
1205 DAPM_UPDATE_STAT(w, power_checks);
1207 /* Check if one of our outputs is connected */
1208 list_for_each_entry(path, &w->sinks, list_source) {
1209 DAPM_UPDATE_STAT(w, neighbour_checks);
1214 if (path->connected &&
1215 !path->connected(path->source, path->sink))
1221 if (dapm_widget_power_check(path->sink))
1228 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1233 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1234 struct snd_soc_dapm_widget *b,
1242 sort = dapm_down_seq;
1244 if (sort[a->id] != sort[b->id])
1245 return sort[a->id] - sort[b->id];
1246 if (a->subseq != b->subseq) {
1248 return a->subseq - b->subseq;
1250 return b->subseq - a->subseq;
1252 if (a->reg != b->reg)
1253 return a->reg - b->reg;
1254 if (a->dapm != b->dapm)
1255 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1260 /* Insert a widget in order into a DAPM power sequence. */
1261 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1262 struct list_head *list,
1265 struct snd_soc_dapm_widget *w;
1267 list_for_each_entry(w, list, power_list)
1268 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1269 list_add_tail(&new_widget->power_list, &w->power_list);
1273 list_add_tail(&new_widget->power_list, list);
1276 static void dapm_seq_check_event(struct snd_soc_card *card,
1277 struct snd_soc_dapm_widget *w, int event)
1279 const char *ev_name;
1283 case SND_SOC_DAPM_PRE_PMU:
1284 ev_name = "PRE_PMU";
1287 case SND_SOC_DAPM_POST_PMU:
1288 ev_name = "POST_PMU";
1291 case SND_SOC_DAPM_PRE_PMD:
1292 ev_name = "PRE_PMD";
1295 case SND_SOC_DAPM_POST_PMD:
1296 ev_name = "POST_PMD";
1299 case SND_SOC_DAPM_WILL_PMU:
1300 ev_name = "WILL_PMU";
1303 case SND_SOC_DAPM_WILL_PMD:
1304 ev_name = "WILL_PMD";
1308 WARN(1, "Unknown event %d\n", event);
1312 if (w->new_power != power)
1315 if (w->event && (w->event_flags & event)) {
1316 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1318 soc_dapm_async_complete(w->dapm);
1319 trace_snd_soc_dapm_widget_event_start(w, event);
1320 ret = w->event(w, NULL, event);
1321 trace_snd_soc_dapm_widget_event_done(w, event);
1323 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1324 ev_name, w->name, ret);
1328 /* Apply the coalesced changes from a DAPM sequence */
1329 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1330 struct list_head *pending)
1332 struct snd_soc_dapm_context *dapm;
1333 struct snd_soc_dapm_widget *w;
1335 unsigned int value = 0;
1336 unsigned int mask = 0;
1338 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1342 list_for_each_entry(w, pending, power_list) {
1343 WARN_ON(reg != w->reg || dapm != w->dapm);
1344 w->power = w->new_power;
1346 mask |= w->mask << w->shift;
1348 value |= w->on_val << w->shift;
1350 value |= w->off_val << w->shift;
1352 pop_dbg(dapm->dev, card->pop_time,
1353 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1354 w->name, reg, value, mask);
1356 /* Check for events */
1357 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1358 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1362 /* Any widget will do, they should all be updating the
1366 pop_dbg(dapm->dev, card->pop_time,
1367 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1368 value, mask, reg, card->pop_time);
1369 pop_wait(card->pop_time);
1370 soc_dapm_update_bits(dapm, reg, mask, value);
1373 list_for_each_entry(w, pending, power_list) {
1374 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1375 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1379 /* Apply a DAPM power sequence.
1381 * We walk over a pre-sorted list of widgets to apply power to. In
1382 * order to minimise the number of writes to the device required
1383 * multiple widgets will be updated in a single write where possible.
1384 * Currently anything that requires more than a single write is not
1387 static void dapm_seq_run(struct snd_soc_card *card,
1388 struct list_head *list, int event, bool power_up)
1390 struct snd_soc_dapm_widget *w, *n;
1391 struct snd_soc_dapm_context *d;
1394 int cur_subseq = -1;
1395 int cur_reg = SND_SOC_NOPM;
1396 struct snd_soc_dapm_context *cur_dapm = NULL;
1403 sort = dapm_down_seq;
1405 list_for_each_entry_safe(w, n, list, power_list) {
1408 /* Do we need to apply any queued changes? */
1409 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1410 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1411 if (!list_empty(&pending))
1412 dapm_seq_run_coalesced(card, &pending);
1414 if (cur_dapm && cur_dapm->seq_notifier) {
1415 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1416 if (sort[i] == cur_sort)
1417 cur_dapm->seq_notifier(cur_dapm,
1422 if (cur_dapm && w->dapm != cur_dapm)
1423 soc_dapm_async_complete(cur_dapm);
1425 INIT_LIST_HEAD(&pending);
1427 cur_subseq = INT_MIN;
1428 cur_reg = SND_SOC_NOPM;
1433 case snd_soc_dapm_pre:
1435 list_for_each_entry_safe_continue(w, n, list,
1438 if (event == SND_SOC_DAPM_STREAM_START)
1440 NULL, SND_SOC_DAPM_PRE_PMU);
1441 else if (event == SND_SOC_DAPM_STREAM_STOP)
1443 NULL, SND_SOC_DAPM_PRE_PMD);
1446 case snd_soc_dapm_post:
1448 list_for_each_entry_safe_continue(w, n, list,
1451 if (event == SND_SOC_DAPM_STREAM_START)
1453 NULL, SND_SOC_DAPM_POST_PMU);
1454 else if (event == SND_SOC_DAPM_STREAM_STOP)
1456 NULL, SND_SOC_DAPM_POST_PMD);
1460 /* Queue it up for application */
1461 cur_sort = sort[w->id];
1462 cur_subseq = w->subseq;
1465 list_move(&w->power_list, &pending);
1470 dev_err(w->dapm->dev,
1471 "ASoC: Failed to apply widget power: %d\n", ret);
1474 if (!list_empty(&pending))
1475 dapm_seq_run_coalesced(card, &pending);
1477 if (cur_dapm && cur_dapm->seq_notifier) {
1478 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1479 if (sort[i] == cur_sort)
1480 cur_dapm->seq_notifier(cur_dapm,
1484 list_for_each_entry(d, &card->dapm_list, list) {
1485 soc_dapm_async_complete(d);
1489 static void dapm_widget_update(struct snd_soc_card *card)
1491 struct snd_soc_dapm_update *update = card->update;
1492 struct snd_soc_dapm_widget_list *wlist;
1493 struct snd_soc_dapm_widget *w = NULL;
1497 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1500 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1502 for (wi = 0; wi < wlist->num_widgets; wi++) {
1503 w = wlist->widgets[wi];
1505 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1506 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1508 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1516 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1519 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1522 for (wi = 0; wi < wlist->num_widgets; wi++) {
1523 w = wlist->widgets[wi];
1525 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1526 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1528 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1534 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1535 * they're changing state.
1537 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1539 struct snd_soc_dapm_context *d = data;
1542 /* If we're off and we're not supposed to be go into STANDBY */
1543 if (d->bias_level == SND_SOC_BIAS_OFF &&
1544 d->target_bias_level != SND_SOC_BIAS_OFF) {
1546 pm_runtime_get_sync(d->dev);
1548 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1551 "ASoC: Failed to turn on bias: %d\n", ret);
1554 /* Prepare for a transition to ON or away from ON */
1555 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1556 d->bias_level != SND_SOC_BIAS_ON) ||
1557 (d->target_bias_level != SND_SOC_BIAS_ON &&
1558 d->bias_level == SND_SOC_BIAS_ON)) {
1559 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1562 "ASoC: Failed to prepare bias: %d\n", ret);
1566 /* Async callback run prior to DAPM sequences - brings to their final
1569 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1571 struct snd_soc_dapm_context *d = data;
1574 /* If we just powered the last thing off drop to standby bias */
1575 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1576 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1577 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1578 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1580 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1584 /* If we're in standby and can support bias off then do that */
1585 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1586 d->target_bias_level == SND_SOC_BIAS_OFF) {
1587 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1589 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1593 pm_runtime_put(d->dev);
1596 /* If we just powered up then move to active bias */
1597 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1598 d->target_bias_level == SND_SOC_BIAS_ON) {
1599 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1601 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1606 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1607 bool power, bool connect)
1609 /* If a connection is being made or broken then that update
1610 * will have marked the peer dirty, otherwise the widgets are
1611 * not connected and this update has no impact. */
1615 /* If the peer is already in the state we're moving to then we
1616 * won't have an impact on it. */
1617 if (power != peer->power)
1618 dapm_mark_dirty(peer, "peer state change");
1621 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1622 struct list_head *up_list,
1623 struct list_head *down_list)
1625 struct snd_soc_dapm_path *path;
1627 if (w->power == power)
1630 trace_snd_soc_dapm_widget_power(w, power);
1632 /* If we changed our power state perhaps our neigbours changed
1635 list_for_each_entry(path, &w->sources, list_sink) {
1637 dapm_widget_set_peer_power(path->source, power,
1642 case snd_soc_dapm_supply:
1643 case snd_soc_dapm_regulator_supply:
1644 case snd_soc_dapm_clock_supply:
1645 case snd_soc_dapm_kcontrol:
1646 /* Supplies can't affect their outputs, only their inputs */
1649 list_for_each_entry(path, &w->sinks, list_source) {
1651 dapm_widget_set_peer_power(path->sink, power,
1659 dapm_seq_insert(w, up_list, true);
1661 dapm_seq_insert(w, down_list, false);
1664 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1665 struct list_head *up_list,
1666 struct list_head *down_list)
1671 case snd_soc_dapm_pre:
1672 dapm_seq_insert(w, down_list, false);
1674 case snd_soc_dapm_post:
1675 dapm_seq_insert(w, up_list, true);
1679 power = dapm_widget_power_check(w);
1681 dapm_widget_set_power(w, power, up_list, down_list);
1687 * Scan each dapm widget for complete audio path.
1688 * A complete path is a route that has valid endpoints i.e.:-
1690 * o DAC to output pin.
1691 * o Input Pin to ADC.
1692 * o Input pin to Output pin (bypass, sidetone)
1693 * o DAC to ADC (loopback).
1695 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1697 struct snd_soc_dapm_widget *w;
1698 struct snd_soc_dapm_context *d;
1700 LIST_HEAD(down_list);
1701 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1702 enum snd_soc_bias_level bias;
1704 lockdep_assert_held(&card->dapm_mutex);
1706 trace_snd_soc_dapm_start(card);
1708 list_for_each_entry(d, &card->dapm_list, list) {
1709 if (d->idle_bias_off)
1710 d->target_bias_level = SND_SOC_BIAS_OFF;
1712 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1717 /* Check which widgets we need to power and store them in
1718 * lists indicating if they should be powered up or down. We
1719 * only check widgets that have been flagged as dirty but note
1720 * that new widgets may be added to the dirty list while we
1723 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1724 dapm_power_one_widget(w, &up_list, &down_list);
1727 list_for_each_entry(w, &card->widgets, list) {
1729 case snd_soc_dapm_pre:
1730 case snd_soc_dapm_post:
1731 /* These widgets always need to be powered */
1734 list_del_init(&w->dirty);
1741 /* Supplies and micbiases only bring the
1742 * context up to STANDBY as unless something
1743 * else is active and passing audio they
1744 * generally don't require full power. Signal
1745 * generators are virtual pins and have no
1746 * power impact themselves.
1749 case snd_soc_dapm_siggen:
1750 case snd_soc_dapm_vmid:
1752 case snd_soc_dapm_supply:
1753 case snd_soc_dapm_regulator_supply:
1754 case snd_soc_dapm_clock_supply:
1755 case snd_soc_dapm_micbias:
1756 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1757 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1760 d->target_bias_level = SND_SOC_BIAS_ON;
1767 /* Force all contexts in the card to the same bias state if
1768 * they're not ground referenced.
1770 bias = SND_SOC_BIAS_OFF;
1771 list_for_each_entry(d, &card->dapm_list, list)
1772 if (d->target_bias_level > bias)
1773 bias = d->target_bias_level;
1774 list_for_each_entry(d, &card->dapm_list, list)
1775 if (!d->idle_bias_off)
1776 d->target_bias_level = bias;
1778 trace_snd_soc_dapm_walk_done(card);
1780 /* Run card bias changes at first */
1781 dapm_pre_sequence_async(&card->dapm, 0);
1782 /* Run other bias changes in parallel */
1783 list_for_each_entry(d, &card->dapm_list, list) {
1784 if (d != &card->dapm)
1785 async_schedule_domain(dapm_pre_sequence_async, d,
1788 async_synchronize_full_domain(&async_domain);
1790 list_for_each_entry(w, &down_list, power_list) {
1791 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1794 list_for_each_entry(w, &up_list, power_list) {
1795 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1798 /* Power down widgets first; try to avoid amplifying pops. */
1799 dapm_seq_run(card, &down_list, event, false);
1801 dapm_widget_update(card);
1804 dapm_seq_run(card, &up_list, event, true);
1806 /* Run all the bias changes in parallel */
1807 list_for_each_entry(d, &card->dapm_list, list) {
1808 if (d != &card->dapm)
1809 async_schedule_domain(dapm_post_sequence_async, d,
1812 async_synchronize_full_domain(&async_domain);
1813 /* Run card bias changes at last */
1814 dapm_post_sequence_async(&card->dapm, 0);
1816 /* do we need to notify any clients that DAPM event is complete */
1817 list_for_each_entry(d, &card->dapm_list, list) {
1818 if (d->stream_event)
1819 d->stream_event(d, event);
1822 pop_dbg(card->dev, card->pop_time,
1823 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1824 pop_wait(card->pop_time);
1826 trace_snd_soc_dapm_done(card);
1831 #ifdef CONFIG_DEBUG_FS
1832 static ssize_t dapm_widget_power_read_file(struct file *file,
1833 char __user *user_buf,
1834 size_t count, loff_t *ppos)
1836 struct snd_soc_dapm_widget *w = file->private_data;
1840 struct snd_soc_dapm_path *p = NULL;
1842 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1846 in = is_connected_input_ep(w, NULL);
1847 dapm_clear_walk_input(w->dapm, &w->sources);
1848 out = is_connected_output_ep(w, NULL);
1849 dapm_clear_walk_output(w->dapm, &w->sinks);
1851 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1852 w->name, w->power ? "On" : "Off",
1853 w->force ? " (forced)" : "", in, out);
1856 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1857 " - R%d(0x%x) mask 0x%x",
1858 w->reg, w->reg, w->mask << w->shift);
1860 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1863 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1865 w->active ? "active" : "inactive");
1867 list_for_each_entry(p, &w->sources, list_sink) {
1868 if (p->connected && !p->connected(w, p->source))
1872 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1873 " in \"%s\" \"%s\"\n",
1874 p->name ? p->name : "static",
1877 list_for_each_entry(p, &w->sinks, list_source) {
1878 if (p->connected && !p->connected(w, p->sink))
1882 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1883 " out \"%s\" \"%s\"\n",
1884 p->name ? p->name : "static",
1888 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1894 static const struct file_operations dapm_widget_power_fops = {
1895 .open = simple_open,
1896 .read = dapm_widget_power_read_file,
1897 .llseek = default_llseek,
1900 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1901 size_t count, loff_t *ppos)
1903 struct snd_soc_dapm_context *dapm = file->private_data;
1906 switch (dapm->bias_level) {
1907 case SND_SOC_BIAS_ON:
1910 case SND_SOC_BIAS_PREPARE:
1911 level = "Prepare\n";
1913 case SND_SOC_BIAS_STANDBY:
1914 level = "Standby\n";
1916 case SND_SOC_BIAS_OFF:
1920 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1921 level = "Unknown\n";
1925 return simple_read_from_buffer(user_buf, count, ppos, level,
1929 static const struct file_operations dapm_bias_fops = {
1930 .open = simple_open,
1931 .read = dapm_bias_read_file,
1932 .llseek = default_llseek,
1935 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1936 struct dentry *parent)
1940 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1942 if (!dapm->debugfs_dapm) {
1944 "ASoC: Failed to create DAPM debugfs directory\n");
1948 d = debugfs_create_file("bias_level", 0444,
1949 dapm->debugfs_dapm, dapm,
1953 "ASoC: Failed to create bias level debugfs file\n");
1956 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1958 struct snd_soc_dapm_context *dapm = w->dapm;
1961 if (!dapm->debugfs_dapm || !w->name)
1964 d = debugfs_create_file(w->name, 0444,
1965 dapm->debugfs_dapm, w,
1966 &dapm_widget_power_fops);
1968 dev_warn(w->dapm->dev,
1969 "ASoC: Failed to create %s debugfs file\n",
1973 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1975 debugfs_remove_recursive(dapm->debugfs_dapm);
1979 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1980 struct dentry *parent)
1984 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1988 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1994 /* test and update the power status of a mux widget */
1995 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
1996 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1998 struct snd_soc_dapm_path *path;
2001 lockdep_assert_held(&card->dapm_mutex);
2003 /* find dapm widget path assoc with kcontrol */
2004 dapm_kcontrol_for_each_path(path, kcontrol) {
2005 if (!path->name || !e->texts[mux])
2009 /* we now need to match the string in the enum to the path */
2010 if (!(strcmp(path->name, e->texts[mux]))) {
2011 path->connect = 1; /* new connection */
2012 dapm_mark_dirty(path->source, "mux connection");
2015 dapm_mark_dirty(path->source,
2016 "mux disconnection");
2017 path->connect = 0; /* old connection must be powered down */
2019 dapm_mark_dirty(path->sink, "mux change");
2023 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2028 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2029 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2030 struct snd_soc_dapm_update *update)
2032 struct snd_soc_card *card = dapm->card;
2035 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2036 card->update = update;
2037 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2038 card->update = NULL;
2039 mutex_unlock(&card->dapm_mutex);
2041 soc_dpcm_runtime_update(card);
2044 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2046 /* test and update the power status of a mixer or switch widget */
2047 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2048 struct snd_kcontrol *kcontrol, int connect)
2050 struct snd_soc_dapm_path *path;
2053 lockdep_assert_held(&card->dapm_mutex);
2055 /* find dapm widget path assoc with kcontrol */
2056 dapm_kcontrol_for_each_path(path, kcontrol) {
2058 path->connect = connect;
2059 dapm_mark_dirty(path->source, "mixer connection");
2060 dapm_mark_dirty(path->sink, "mixer update");
2064 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2069 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2070 struct snd_kcontrol *kcontrol, int connect,
2071 struct snd_soc_dapm_update *update)
2073 struct snd_soc_card *card = dapm->card;
2076 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2077 card->update = update;
2078 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2079 card->update = NULL;
2080 mutex_unlock(&card->dapm_mutex);
2082 soc_dpcm_runtime_update(card);
2085 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2087 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2089 struct snd_soc_dapm_widget *w;
2091 char *state = "not set";
2093 list_for_each_entry(w, &codec->component.card->widgets, list) {
2094 if (w->dapm != &codec->dapm)
2097 /* only display widgets that burnm power */
2099 case snd_soc_dapm_hp:
2100 case snd_soc_dapm_mic:
2101 case snd_soc_dapm_spk:
2102 case snd_soc_dapm_line:
2103 case snd_soc_dapm_micbias:
2104 case snd_soc_dapm_dac:
2105 case snd_soc_dapm_adc:
2106 case snd_soc_dapm_pga:
2107 case snd_soc_dapm_out_drv:
2108 case snd_soc_dapm_mixer:
2109 case snd_soc_dapm_mixer_named_ctl:
2110 case snd_soc_dapm_supply:
2111 case snd_soc_dapm_regulator_supply:
2112 case snd_soc_dapm_clock_supply:
2114 count += sprintf(buf + count, "%s: %s\n",
2115 w->name, w->power ? "On":"Off");
2122 switch (codec->dapm.bias_level) {
2123 case SND_SOC_BIAS_ON:
2126 case SND_SOC_BIAS_PREPARE:
2129 case SND_SOC_BIAS_STANDBY:
2132 case SND_SOC_BIAS_OFF:
2136 count += sprintf(buf + count, "PM State: %s\n", state);
2141 /* show dapm widget status in sys fs */
2142 static ssize_t dapm_widget_show(struct device *dev,
2143 struct device_attribute *attr, char *buf)
2145 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2148 for (i = 0; i < rtd->num_codecs; i++) {
2149 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2150 count += dapm_widget_show_codec(codec, buf + count);
2156 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2158 int snd_soc_dapm_sys_add(struct device *dev)
2160 return device_create_file(dev, &dev_attr_dapm_widget);
2163 static void snd_soc_dapm_sys_remove(struct device *dev)
2165 device_remove_file(dev, &dev_attr_dapm_widget);
2168 static void dapm_free_path(struct snd_soc_dapm_path *path)
2170 list_del(&path->list_sink);
2171 list_del(&path->list_source);
2172 list_del(&path->list_kcontrol);
2173 list_del(&path->list);
2177 /* free all dapm widgets and resources */
2178 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2180 struct snd_soc_dapm_widget *w, *next_w;
2181 struct snd_soc_dapm_path *p, *next_p;
2183 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2184 if (w->dapm != dapm)
2188 * remove source and sink paths associated to this widget.
2189 * While removing the path, remove reference to it from both
2190 * source and sink widgets so that path is removed only once.
2192 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2195 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2198 kfree(w->kcontrols);
2204 static struct snd_soc_dapm_widget *dapm_find_widget(
2205 struct snd_soc_dapm_context *dapm, const char *pin,
2206 bool search_other_contexts)
2208 struct snd_soc_dapm_widget *w;
2209 struct snd_soc_dapm_widget *fallback = NULL;
2211 list_for_each_entry(w, &dapm->card->widgets, list) {
2212 if (!strcmp(w->name, pin)) {
2213 if (w->dapm == dapm)
2220 if (search_other_contexts)
2226 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2227 const char *pin, int status)
2229 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2231 dapm_assert_locked(dapm);
2234 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2238 if (w->connected != status)
2239 dapm_mark_dirty(w, "pin configuration");
2241 w->connected = status;
2249 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2250 * @dapm: DAPM context
2252 * Walks all dapm audio paths and powers widgets according to their
2253 * stream or path usage.
2255 * Requires external locking.
2257 * Returns 0 for success.
2259 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2262 * Suppress early reports (eg, jacks syncing their state) to avoid
2263 * silly DAPM runs during card startup.
2265 if (!dapm->card || !dapm->card->instantiated)
2268 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2270 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2273 * snd_soc_dapm_sync - scan and power dapm paths
2274 * @dapm: DAPM context
2276 * Walks all dapm audio paths and powers widgets according to their
2277 * stream or path usage.
2279 * Returns 0 for success.
2281 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2285 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2286 ret = snd_soc_dapm_sync_unlocked(dapm);
2287 mutex_unlock(&dapm->card->dapm_mutex);
2290 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2292 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2293 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2294 const char *control,
2295 int (*connected)(struct snd_soc_dapm_widget *source,
2296 struct snd_soc_dapm_widget *sink))
2298 struct snd_soc_dapm_path *path;
2301 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2305 path->source = wsource;
2307 path->connected = connected;
2308 INIT_LIST_HEAD(&path->list);
2309 INIT_LIST_HEAD(&path->list_kcontrol);
2310 INIT_LIST_HEAD(&path->list_source);
2311 INIT_LIST_HEAD(&path->list_sink);
2313 /* check for external widgets */
2314 if (wsink->id == snd_soc_dapm_input) {
2315 if (wsource->id == snd_soc_dapm_micbias ||
2316 wsource->id == snd_soc_dapm_mic ||
2317 wsource->id == snd_soc_dapm_line ||
2318 wsource->id == snd_soc_dapm_output)
2321 if (wsource->id == snd_soc_dapm_output) {
2322 if (wsink->id == snd_soc_dapm_spk ||
2323 wsink->id == snd_soc_dapm_hp ||
2324 wsink->id == snd_soc_dapm_line ||
2325 wsink->id == snd_soc_dapm_input)
2329 dapm_mark_dirty(wsource, "Route added");
2330 dapm_mark_dirty(wsink, "Route added");
2332 /* connect static paths */
2333 if (control == NULL) {
2334 list_add(&path->list, &dapm->card->paths);
2335 list_add(&path->list_sink, &wsink->sources);
2336 list_add(&path->list_source, &wsource->sinks);
2341 /* connect dynamic paths */
2342 switch (wsink->id) {
2343 case snd_soc_dapm_adc:
2344 case snd_soc_dapm_dac:
2345 case snd_soc_dapm_pga:
2346 case snd_soc_dapm_out_drv:
2347 case snd_soc_dapm_input:
2348 case snd_soc_dapm_output:
2349 case snd_soc_dapm_siggen:
2350 case snd_soc_dapm_micbias:
2351 case snd_soc_dapm_vmid:
2352 case snd_soc_dapm_pre:
2353 case snd_soc_dapm_post:
2354 case snd_soc_dapm_supply:
2355 case snd_soc_dapm_regulator_supply:
2356 case snd_soc_dapm_clock_supply:
2357 case snd_soc_dapm_aif_in:
2358 case snd_soc_dapm_aif_out:
2359 case snd_soc_dapm_dai_in:
2360 case snd_soc_dapm_dai_out:
2361 case snd_soc_dapm_dai_link:
2362 case snd_soc_dapm_kcontrol:
2363 list_add(&path->list, &dapm->card->paths);
2364 list_add(&path->list_sink, &wsink->sources);
2365 list_add(&path->list_source, &wsource->sinks);
2368 case snd_soc_dapm_mux:
2369 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2370 &wsink->kcontrol_news[0]);
2374 case snd_soc_dapm_switch:
2375 case snd_soc_dapm_mixer:
2376 case snd_soc_dapm_mixer_named_ctl:
2377 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2381 case snd_soc_dapm_hp:
2382 case snd_soc_dapm_mic:
2383 case snd_soc_dapm_line:
2384 case snd_soc_dapm_spk:
2385 list_add(&path->list, &dapm->card->paths);
2386 list_add(&path->list_sink, &wsink->sources);
2387 list_add(&path->list_source, &wsource->sinks);
2398 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2399 const struct snd_soc_dapm_route *route)
2401 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2402 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2405 char prefixed_sink[80];
2406 char prefixed_source[80];
2410 prefix = soc_dapm_prefix(dapm);
2412 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2413 prefix, route->sink);
2414 sink = prefixed_sink;
2415 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2416 prefix, route->source);
2417 source = prefixed_source;
2420 source = route->source;
2424 * find src and dest widgets over all widgets but favor a widget from
2425 * current DAPM context
2427 list_for_each_entry(w, &dapm->card->widgets, list) {
2428 if (!wsink && !(strcmp(w->name, sink))) {
2430 if (w->dapm == dapm)
2434 if (!wsource && !(strcmp(w->name, source))) {
2436 if (w->dapm == dapm)
2440 /* use widget from another DAPM context if not found from this */
2446 if (wsource == NULL) {
2447 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2451 if (wsink == NULL) {
2452 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2457 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2464 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2465 source, route->control, sink);
2469 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2470 const struct snd_soc_dapm_route *route)
2472 struct snd_soc_dapm_path *path, *p;
2475 char prefixed_sink[80];
2476 char prefixed_source[80];
2479 if (route->control) {
2481 "ASoC: Removal of routes with controls not supported\n");
2485 prefix = soc_dapm_prefix(dapm);
2487 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2488 prefix, route->sink);
2489 sink = prefixed_sink;
2490 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2491 prefix, route->source);
2492 source = prefixed_source;
2495 source = route->source;
2499 list_for_each_entry(p, &dapm->card->paths, list) {
2500 if (strcmp(p->source->name, source) != 0)
2502 if (strcmp(p->sink->name, sink) != 0)
2509 dapm_mark_dirty(path->source, "Route removed");
2510 dapm_mark_dirty(path->sink, "Route removed");
2512 dapm_free_path(path);
2514 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2522 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2523 * @dapm: DAPM context
2524 * @route: audio routes
2525 * @num: number of routes
2527 * Connects 2 dapm widgets together via a named audio path. The sink is
2528 * the widget receiving the audio signal, whilst the source is the sender
2529 * of the audio signal.
2531 * Returns 0 for success else error. On error all resources can be freed
2532 * with a call to snd_soc_card_free().
2534 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2535 const struct snd_soc_dapm_route *route, int num)
2539 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2540 for (i = 0; i < num; i++) {
2541 r = snd_soc_dapm_add_route(dapm, route);
2543 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2545 route->control ? route->control : "direct",
2551 mutex_unlock(&dapm->card->dapm_mutex);
2555 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2558 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2559 * @dapm: DAPM context
2560 * @route: audio routes
2561 * @num: number of routes
2563 * Removes routes from the DAPM context.
2565 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2566 const struct snd_soc_dapm_route *route, int num)
2570 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2571 for (i = 0; i < num; i++) {
2572 snd_soc_dapm_del_route(dapm, route);
2575 mutex_unlock(&dapm->card->dapm_mutex);
2579 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2581 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2582 const struct snd_soc_dapm_route *route)
2584 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2587 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2590 struct snd_soc_dapm_path *path;
2594 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2600 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2605 if (route->control || route->connected)
2606 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2607 route->source, route->sink);
2609 list_for_each_entry(path, &source->sinks, list_source) {
2610 if (path->sink == sink) {
2617 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2618 route->source, route->sink);
2620 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2621 count, route->source, route->sink);
2627 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2628 * @dapm: DAPM context
2629 * @route: audio routes
2630 * @num: number of routes
2632 * Mark existing routes matching those specified in the passed array
2633 * as being weak, meaning that they are ignored for the purpose of
2634 * power decisions. The main intended use case is for sidetone paths
2635 * which couple audio between other independent paths if they are both
2636 * active in order to make the combination work better at the user
2637 * level but which aren't intended to be "used".
2639 * Note that CODEC drivers should not use this as sidetone type paths
2640 * can frequently also be used as bypass paths.
2642 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2643 const struct snd_soc_dapm_route *route, int num)
2648 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2649 for (i = 0; i < num; i++) {
2650 err = snd_soc_dapm_weak_route(dapm, route);
2655 mutex_unlock(&dapm->card->dapm_mutex);
2659 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2662 * snd_soc_dapm_new_widgets - add new dapm widgets
2663 * @dapm: DAPM context
2665 * Checks the codec for any new dapm widgets and creates them if found.
2667 * Returns 0 for success.
2669 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2671 struct snd_soc_dapm_widget *w;
2674 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2676 list_for_each_entry(w, &card->widgets, list)
2681 if (w->num_kcontrols) {
2682 w->kcontrols = kzalloc(w->num_kcontrols *
2683 sizeof(struct snd_kcontrol *),
2685 if (!w->kcontrols) {
2686 mutex_unlock(&card->dapm_mutex);
2692 case snd_soc_dapm_switch:
2693 case snd_soc_dapm_mixer:
2694 case snd_soc_dapm_mixer_named_ctl:
2697 case snd_soc_dapm_mux:
2700 case snd_soc_dapm_pga:
2701 case snd_soc_dapm_out_drv:
2708 /* Read the initial power state from the device */
2710 soc_dapm_read(w->dapm, w->reg, &val);
2711 val = val >> w->shift;
2713 if (val == w->on_val)
2719 dapm_mark_dirty(w, "new widget");
2720 dapm_debugfs_add_widget(w);
2723 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2724 mutex_unlock(&card->dapm_mutex);
2727 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2730 * snd_soc_dapm_get_volsw - dapm mixer get callback
2731 * @kcontrol: mixer control
2732 * @ucontrol: control element information
2734 * Callback to get the value of a dapm mixer control.
2736 * Returns 0 for success.
2738 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
2741 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2742 struct snd_soc_card *card = dapm->card;
2743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
2746 unsigned int shift = mc->shift;
2748 unsigned int mask = (1 << fls(max)) - 1;
2749 unsigned int invert = mc->invert;
2753 if (snd_soc_volsw_is_stereo(mc))
2755 "ASoC: Control '%s' is stereo, which is not supported\n",
2758 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2759 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2760 ret = soc_dapm_read(dapm, reg, &val);
2761 val = (val >> shift) & mask;
2763 val = dapm_kcontrol_get_value(kcontrol);
2765 mutex_unlock(&card->dapm_mutex);
2768 ucontrol->value.integer.value[0] = max - val;
2770 ucontrol->value.integer.value[0] = val;
2774 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2777 * snd_soc_dapm_put_volsw - dapm mixer set callback
2778 * @kcontrol: mixer control
2779 * @ucontrol: control element information
2781 * Callback to set the value of a dapm mixer control.
2783 * Returns 0 for success.
2785 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2786 struct snd_ctl_elem_value *ucontrol)
2788 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2789 struct snd_soc_card *card = dapm->card;
2790 struct soc_mixer_control *mc =
2791 (struct soc_mixer_control *)kcontrol->private_value;
2793 unsigned int shift = mc->shift;
2795 unsigned int mask = (1 << fls(max)) - 1;
2796 unsigned int invert = mc->invert;
2798 int connect, change, reg_change = 0;
2799 struct snd_soc_dapm_update update;
2802 if (snd_soc_volsw_is_stereo(mc))
2804 "ASoC: Control '%s' is stereo, which is not supported\n",
2807 val = (ucontrol->value.integer.value[0] & mask);
2813 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2815 change = dapm_kcontrol_set_value(kcontrol, val);
2817 if (reg != SND_SOC_NOPM) {
2818 mask = mask << shift;
2821 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2824 if (change || reg_change) {
2826 update.kcontrol = kcontrol;
2830 card->update = &update;
2832 change |= reg_change;
2834 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2836 card->update = NULL;
2839 mutex_unlock(&card->dapm_mutex);
2842 soc_dpcm_runtime_update(card);
2846 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2849 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2850 * @kcontrol: mixer control
2851 * @ucontrol: control element information
2853 * Callback to get the value of a dapm enumerated double mixer control.
2855 * Returns 0 for success.
2857 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2858 struct snd_ctl_elem_value *ucontrol)
2860 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2861 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2862 unsigned int reg_val, val;
2864 if (e->reg != SND_SOC_NOPM) {
2865 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2869 reg_val = dapm_kcontrol_get_value(kcontrol);
2872 val = (reg_val >> e->shift_l) & e->mask;
2873 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2874 if (e->shift_l != e->shift_r) {
2875 val = (reg_val >> e->shift_r) & e->mask;
2876 val = snd_soc_enum_val_to_item(e, val);
2877 ucontrol->value.enumerated.item[1] = val;
2882 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2885 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2886 * @kcontrol: mixer control
2887 * @ucontrol: control element information
2889 * Callback to set the value of a dapm enumerated double mixer control.
2891 * Returns 0 for success.
2893 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_value *ucontrol)
2896 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2897 struct snd_soc_card *card = dapm->card;
2898 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2899 unsigned int *item = ucontrol->value.enumerated.item;
2900 unsigned int val, change;
2902 struct snd_soc_dapm_update update;
2905 if (item[0] >= e->items)
2908 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2909 mask = e->mask << e->shift_l;
2910 if (e->shift_l != e->shift_r) {
2911 if (item[1] > e->items)
2913 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2914 mask |= e->mask << e->shift_r;
2917 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2919 if (e->reg != SND_SOC_NOPM)
2920 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2922 change = dapm_kcontrol_set_value(kcontrol, val);
2925 if (e->reg != SND_SOC_NOPM) {
2926 update.kcontrol = kcontrol;
2927 update.reg = e->reg;
2930 card->update = &update;
2933 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2935 card->update = NULL;
2938 mutex_unlock(&card->dapm_mutex);
2941 soc_dpcm_runtime_update(card);
2945 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2948 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2950 * @kcontrol: mixer control
2951 * @uinfo: control element information
2953 * Callback to provide information about a pin switch control.
2955 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2956 struct snd_ctl_elem_info *uinfo)
2958 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2960 uinfo->value.integer.min = 0;
2961 uinfo->value.integer.max = 1;
2965 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2968 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2970 * @kcontrol: mixer control
2973 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2974 struct snd_ctl_elem_value *ucontrol)
2976 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2977 const char *pin = (const char *)kcontrol->private_value;
2979 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2981 ucontrol->value.integer.value[0] =
2982 snd_soc_dapm_get_pin_status(&card->dapm, pin);
2984 mutex_unlock(&card->dapm_mutex);
2988 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
2991 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
2993 * @kcontrol: mixer control
2996 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_value *ucontrol)
2999 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3000 const char *pin = (const char *)kcontrol->private_value;
3002 if (ucontrol->value.integer.value[0])
3003 snd_soc_dapm_enable_pin(&card->dapm, pin);
3005 snd_soc_dapm_disable_pin(&card->dapm, pin);
3007 snd_soc_dapm_sync(&card->dapm);
3010 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3012 static struct snd_soc_dapm_widget *
3013 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3014 const struct snd_soc_dapm_widget *widget)
3016 struct snd_soc_dapm_widget *w;
3020 if ((w = dapm_cnew_widget(widget)) == NULL)
3024 case snd_soc_dapm_regulator_supply:
3025 w->regulator = devm_regulator_get(dapm->dev, w->name);
3026 if (IS_ERR(w->regulator)) {
3027 ret = PTR_ERR(w->regulator);
3028 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3033 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3034 ret = regulator_allow_bypass(w->regulator, true);
3036 dev_warn(w->dapm->dev,
3037 "ASoC: Failed to bypass %s: %d\n",
3041 case snd_soc_dapm_clock_supply:
3042 #ifdef CONFIG_CLKDEV_LOOKUP
3043 w->clk = devm_clk_get(dapm->dev, w->name);
3044 if (IS_ERR(w->clk)) {
3045 ret = PTR_ERR(w->clk);
3046 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3058 prefix = soc_dapm_prefix(dapm);
3060 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3062 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3064 if (w->name == NULL) {
3070 case snd_soc_dapm_switch:
3071 case snd_soc_dapm_mixer:
3072 case snd_soc_dapm_mixer_named_ctl:
3073 w->power_check = dapm_generic_check_power;
3075 case snd_soc_dapm_mux:
3076 w->power_check = dapm_generic_check_power;
3078 case snd_soc_dapm_dai_out:
3079 w->power_check = dapm_adc_check_power;
3081 case snd_soc_dapm_dai_in:
3082 w->power_check = dapm_dac_check_power;
3084 case snd_soc_dapm_adc:
3085 case snd_soc_dapm_aif_out:
3086 case snd_soc_dapm_dac:
3087 case snd_soc_dapm_aif_in:
3088 case snd_soc_dapm_pga:
3089 case snd_soc_dapm_out_drv:
3090 case snd_soc_dapm_input:
3091 case snd_soc_dapm_output:
3092 case snd_soc_dapm_micbias:
3093 case snd_soc_dapm_spk:
3094 case snd_soc_dapm_hp:
3095 case snd_soc_dapm_mic:
3096 case snd_soc_dapm_line:
3097 case snd_soc_dapm_dai_link:
3098 w->power_check = dapm_generic_check_power;
3100 case snd_soc_dapm_supply:
3101 case snd_soc_dapm_regulator_supply:
3102 case snd_soc_dapm_clock_supply:
3103 case snd_soc_dapm_kcontrol:
3104 w->power_check = dapm_supply_check_power;
3107 w->power_check = dapm_always_on_check_power;
3112 w->codec = dapm->codec;
3113 INIT_LIST_HEAD(&w->sources);
3114 INIT_LIST_HEAD(&w->sinks);
3115 INIT_LIST_HEAD(&w->list);
3116 INIT_LIST_HEAD(&w->dirty);
3117 list_add(&w->list, &dapm->card->widgets);
3119 /* machine layer set ups unconnected pins and insertions */
3125 * snd_soc_dapm_new_controls - create new dapm controls
3126 * @dapm: DAPM context
3127 * @widget: widget array
3128 * @num: number of widgets
3130 * Creates new DAPM controls based upon the templates.
3132 * Returns 0 for success else error.
3134 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3135 const struct snd_soc_dapm_widget *widget,
3138 struct snd_soc_dapm_widget *w;
3142 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3143 for (i = 0; i < num; i++) {
3144 w = snd_soc_dapm_new_control(dapm, widget);
3147 "ASoC: Failed to create DAPM control %s\n",
3154 mutex_unlock(&dapm->card->dapm_mutex);
3157 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3159 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3160 struct snd_kcontrol *kcontrol, int event)
3162 struct snd_soc_dapm_path *source_p, *sink_p;
3163 struct snd_soc_dai *source, *sink;
3164 const struct snd_soc_pcm_stream *config = w->params;
3165 struct snd_pcm_substream substream;
3166 struct snd_pcm_hw_params *params = NULL;
3170 if (WARN_ON(!config) ||
3171 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3174 /* We only support a single source and sink, pick the first */
3175 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3177 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3180 if (WARN_ON(!source_p || !sink_p) ||
3181 WARN_ON(!sink_p->source || !source_p->sink) ||
3182 WARN_ON(!source_p->source || !sink_p->sink))
3185 source = source_p->source->priv;
3186 sink = sink_p->sink->priv;
3188 /* Be a little careful as we don't want to overflow the mask array */
3189 if (config->formats) {
3190 fmt = ffs(config->formats) - 1;
3192 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3197 /* Currently very limited parameter selection */
3198 params = kzalloc(sizeof(*params), GFP_KERNEL);
3203 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3205 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3207 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3210 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3211 = config->channels_min;
3212 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3213 = config->channels_max;
3215 memset(&substream, 0, sizeof(substream));
3218 case SND_SOC_DAPM_PRE_PMU:
3219 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3220 ret = soc_dai_hw_params(&substream, params, source);
3224 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3225 ret = soc_dai_hw_params(&substream, params, sink);
3230 case SND_SOC_DAPM_POST_PMU:
3231 ret = snd_soc_dai_digital_mute(sink, 0,
3232 SNDRV_PCM_STREAM_PLAYBACK);
3233 if (ret != 0 && ret != -ENOTSUPP)
3234 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3238 case SND_SOC_DAPM_PRE_PMD:
3239 ret = snd_soc_dai_digital_mute(sink, 1,
3240 SNDRV_PCM_STREAM_PLAYBACK);
3241 if (ret != 0 && ret != -ENOTSUPP)
3242 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3247 WARN(1, "Unknown event %d\n", event);
3256 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3257 const struct snd_soc_pcm_stream *params,
3258 struct snd_soc_dapm_widget *source,
3259 struct snd_soc_dapm_widget *sink)
3261 struct snd_soc_dapm_widget template;
3262 struct snd_soc_dapm_widget *w;
3267 len = strlen(source->name) + strlen(sink->name) + 2;
3268 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3271 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3273 memset(&template, 0, sizeof(template));
3274 template.reg = SND_SOC_NOPM;
3275 template.id = snd_soc_dapm_dai_link;
3276 template.name = link_name;
3277 template.event = snd_soc_dai_link_event;
3278 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3279 SND_SOC_DAPM_PRE_PMD;
3281 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3283 w = snd_soc_dapm_new_control(&card->dapm, &template);
3285 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3292 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3295 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3298 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3299 struct snd_soc_dai *dai)
3301 struct snd_soc_dapm_widget template;
3302 struct snd_soc_dapm_widget *w;
3304 WARN_ON(dapm->dev != dai->dev);
3306 memset(&template, 0, sizeof(template));
3307 template.reg = SND_SOC_NOPM;
3309 if (dai->driver->playback.stream_name) {
3310 template.id = snd_soc_dapm_dai_in;
3311 template.name = dai->driver->playback.stream_name;
3312 template.sname = dai->driver->playback.stream_name;
3314 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3317 w = snd_soc_dapm_new_control(dapm, &template);
3319 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3320 dai->driver->playback.stream_name);
3325 dai->playback_widget = w;
3328 if (dai->driver->capture.stream_name) {
3329 template.id = snd_soc_dapm_dai_out;
3330 template.name = dai->driver->capture.stream_name;
3331 template.sname = dai->driver->capture.stream_name;
3333 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3336 w = snd_soc_dapm_new_control(dapm, &template);
3338 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3339 dai->driver->capture.stream_name);
3344 dai->capture_widget = w;
3350 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3352 struct snd_soc_dapm_widget *dai_w, *w;
3353 struct snd_soc_dapm_widget *src, *sink;
3354 struct snd_soc_dai *dai;
3356 /* For each DAI widget... */
3357 list_for_each_entry(dai_w, &card->widgets, list) {
3358 switch (dai_w->id) {
3359 case snd_soc_dapm_dai_in:
3360 case snd_soc_dapm_dai_out:
3368 /* ...find all widgets with the same stream and link them */
3369 list_for_each_entry(w, &card->widgets, list) {
3370 if (w->dapm != dai_w->dapm)
3374 case snd_soc_dapm_dai_in:
3375 case snd_soc_dapm_dai_out:
3381 if (!w->sname || !strstr(w->sname, dai_w->name))
3384 if (dai_w->id == snd_soc_dapm_dai_in) {
3391 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3392 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3399 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3400 struct snd_soc_pcm_runtime *rtd)
3402 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3403 struct snd_soc_dapm_widget *sink, *source;
3406 for (i = 0; i < rtd->num_codecs; i++) {
3407 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3409 /* there is no point in connecting BE DAI links with dummies */
3410 if (snd_soc_dai_is_dummy(codec_dai) ||
3411 snd_soc_dai_is_dummy(cpu_dai))
3414 /* connect BE DAI playback if widgets are valid */
3415 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3416 source = cpu_dai->playback_widget;
3417 sink = codec_dai->playback_widget;
3418 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3419 cpu_dai->component->name, source->name,
3420 codec_dai->component->name, sink->name);
3422 snd_soc_dapm_add_path(&card->dapm, source, sink,
3426 /* connect BE DAI capture if widgets are valid */
3427 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3428 source = codec_dai->capture_widget;
3429 sink = cpu_dai->capture_widget;
3430 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3431 codec_dai->component->name, source->name,
3432 cpu_dai->component->name, sink->name);
3434 snd_soc_dapm_add_path(&card->dapm, source, sink,
3440 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3443 struct snd_soc_dapm_widget *w;
3445 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3446 w = dai->playback_widget;
3448 w = dai->capture_widget;
3451 dapm_mark_dirty(w, "stream event");
3454 case SND_SOC_DAPM_STREAM_START:
3457 case SND_SOC_DAPM_STREAM_STOP:
3460 case SND_SOC_DAPM_STREAM_SUSPEND:
3461 case SND_SOC_DAPM_STREAM_RESUME:
3462 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3463 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3469 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3471 struct snd_soc_pcm_runtime *rtd = card->rtd;
3474 /* for each BE DAI link... */
3475 for (i = 0; i < card->num_rtd; i++) {
3476 rtd = &card->rtd[i];
3479 * dynamic FE links have no fixed DAI mapping.
3480 * CODEC<->CODEC links have no direct connection.
3482 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3485 dapm_connect_dai_link_widgets(card, rtd);
3489 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3494 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3495 for (i = 0; i < rtd->num_codecs; i++)
3496 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3498 dapm_power_widgets(rtd->card, event);
3502 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3503 * @rtd: PCM runtime data
3504 * @stream: stream name
3505 * @event: stream event
3507 * Sends a stream event to the dapm core. The core then makes any
3508 * necessary widget power changes.
3510 * Returns 0 for success else error.
3512 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3515 struct snd_soc_card *card = rtd->card;
3517 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3518 soc_dapm_stream_event(rtd, stream, event);
3519 mutex_unlock(&card->dapm_mutex);
3523 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3524 * @dapm: DAPM context
3527 * Enables input/output pin and its parents or children widgets iff there is
3528 * a valid audio route and active audio stream.
3530 * Requires external locking.
3532 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3533 * do any widget power switching.
3535 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3538 return snd_soc_dapm_set_pin(dapm, pin, 1);
3540 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3543 * snd_soc_dapm_enable_pin - enable pin.
3544 * @dapm: DAPM context
3547 * Enables input/output pin and its parents or children widgets iff there is
3548 * a valid audio route and active audio stream.
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(struct snd_soc_dapm_context *dapm, const char *pin)
3557 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3559 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3561 mutex_unlock(&dapm->card->dapm_mutex);
3565 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3568 * snd_soc_dapm_force_enable_pin_unlocked - 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 * Requires external locking.
3578 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3579 * do any widget power switching.
3581 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3584 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3587 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3591 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3594 dapm_mark_dirty(w, "force enable");
3598 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3601 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3602 * @dapm: DAPM context
3605 * Enables input/output pin regardless of any other state. This is
3606 * intended for use with microphone bias supplies used in microphone
3609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3610 * do any widget power switching.
3612 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3617 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3619 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3621 mutex_unlock(&dapm->card->dapm_mutex);
3625 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3628 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3629 * @dapm: DAPM context
3632 * Disables input/output pin and its parents or children widgets.
3634 * Requires external locking.
3636 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3637 * do any widget power switching.
3639 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3642 return snd_soc_dapm_set_pin(dapm, pin, 0);
3644 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3647 * snd_soc_dapm_disable_pin - disable pin.
3648 * @dapm: DAPM context
3651 * Disables input/output pin and its parents or children widgets.
3653 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3654 * do any widget power switching.
3656 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3661 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3663 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3665 mutex_unlock(&dapm->card->dapm_mutex);
3669 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3672 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3673 * @dapm: DAPM context
3676 * Marks the specified pin as being not connected, disabling it along
3677 * any parent or child widgets. At present this is identical to
3678 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3679 * additional things such as disabling controls which only affect
3680 * paths through the pin.
3682 * Requires external locking.
3684 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3685 * do any widget power switching.
3687 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
3690 return snd_soc_dapm_set_pin(dapm, pin, 0);
3692 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3695 * snd_soc_dapm_nc_pin - permanently disable pin.
3696 * @dapm: DAPM context
3699 * Marks the specified pin as being not connected, disabling it along
3700 * any parent or child widgets. At present this is identical to
3701 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3702 * additional things such as disabling controls which only affect
3703 * paths through the pin.
3705 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3706 * do any widget power switching.
3708 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3712 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3714 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3716 mutex_unlock(&dapm->card->dapm_mutex);
3720 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3723 * snd_soc_dapm_get_pin_status - get audio pin status
3724 * @dapm: DAPM context
3725 * @pin: audio signal pin endpoint (or start point)
3727 * Get audio pin status - connected or disconnected.
3729 * Returns 1 for connected otherwise 0.
3731 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3734 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3737 return w->connected;
3741 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3744 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3745 * @dapm: DAPM context
3746 * @pin: audio signal pin endpoint (or start point)
3748 * Mark the given endpoint or pin as ignoring suspend. When the
3749 * system is disabled a path between two endpoints flagged as ignoring
3750 * suspend will not be disabled. The path must already be enabled via
3751 * normal means at suspend time, it will not be turned on if it was not
3754 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3757 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3760 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3764 w->ignore_suspend = 1;
3768 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3770 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3771 struct snd_soc_dapm_widget *w)
3773 struct snd_soc_dapm_path *p;
3775 list_for_each_entry(p, &card->paths, list) {
3776 if ((p->source == w) || (p->sink == w)) {
3778 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3779 p->source->name, p->source->id, p->source->dapm,
3780 p->sink->name, p->sink->id, p->sink->dapm);
3782 /* Connected to something other than the codec */
3783 if (p->source->dapm != p->sink->dapm)
3786 * Loopback connection from codec external pin to
3787 * codec external pin
3789 if (p->sink->id == snd_soc_dapm_input) {
3790 switch (p->source->id) {
3791 case snd_soc_dapm_output:
3792 case snd_soc_dapm_micbias:
3805 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3806 * @card: The card whose pins should be processed
3808 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3809 * which are unused. Pins are used if they are connected externally to a
3810 * component, whether that be to some other device, or a loop-back connection to
3811 * the component itself.
3813 void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
3815 struct snd_soc_dapm_widget *w;
3817 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
3819 list_for_each_entry(w, &card->widgets, list) {
3821 case snd_soc_dapm_input:
3822 case snd_soc_dapm_output:
3823 case snd_soc_dapm_micbias:
3824 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
3826 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3828 "... Not in map; disabling\n");
3829 snd_soc_dapm_nc_pin(w->dapm, w->name);
3839 * snd_soc_dapm_free - free dapm resources
3840 * @dapm: DAPM context
3842 * Free all dapm widgets and resources.
3844 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3846 snd_soc_dapm_sys_remove(dapm->dev);
3847 dapm_debugfs_cleanup(dapm);
3848 dapm_free_widgets(dapm);
3849 list_del(&dapm->list);
3851 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3853 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3855 struct snd_soc_card *card = dapm->card;
3856 struct snd_soc_dapm_widget *w;
3857 LIST_HEAD(down_list);
3860 mutex_lock(&card->dapm_mutex);
3862 list_for_each_entry(w, &dapm->card->widgets, list) {
3863 if (w->dapm != dapm)
3866 dapm_seq_insert(w, &down_list, false);
3872 /* If there were no widgets to power down we're already in
3876 if (dapm->bias_level == SND_SOC_BIAS_ON)
3877 snd_soc_dapm_set_bias_level(dapm,
3878 SND_SOC_BIAS_PREPARE);
3879 dapm_seq_run(card, &down_list, 0, false);
3880 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3881 snd_soc_dapm_set_bias_level(dapm,
3882 SND_SOC_BIAS_STANDBY);
3885 mutex_unlock(&card->dapm_mutex);
3889 * snd_soc_dapm_shutdown - callback for system shutdown
3891 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3893 struct snd_soc_dapm_context *dapm;
3895 list_for_each_entry(dapm, &card->dapm_list, list) {
3896 if (dapm != &card->dapm) {
3897 soc_dapm_shutdown_dapm(dapm);
3898 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3899 snd_soc_dapm_set_bias_level(dapm,
3904 soc_dapm_shutdown_dapm(&card->dapm);
3905 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3906 snd_soc_dapm_set_bias_level(&card->dapm,
3910 /* Module information */
3911 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3912 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3913 MODULE_LICENSE("GPL");