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;
595 char *long_name = NULL;
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,
661 kcontrol->private_free = dapm_kcontrol_free;
663 ret = dapm_kcontrol_data_alloc(w, kcontrol);
665 snd_ctl_free_one(kcontrol);
669 ret = snd_ctl_add(card, kcontrol);
672 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
678 ret = dapm_kcontrol_add_widget(kcontrol, w);
680 w->kcontrols[kci] = kcontrol;
688 /* create new dapm mixer control */
689 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
692 struct snd_soc_dapm_path *path;
695 for (i = 0; i < w->num_kcontrols; i++) {
697 list_for_each_entry(path, &w->sources, list_sink) {
698 /* mixer/mux paths name must match control name */
699 if (path->name != (char *)w->kcontrol_news[i].name)
702 if (w->kcontrols[i]) {
703 dapm_kcontrol_add_path(w->kcontrols[i], path);
707 ret = dapm_create_or_share_mixmux_kcontrol(w, i);
711 dapm_kcontrol_add_path(w->kcontrols[i], path);
718 /* create new dapm mux control */
719 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
721 struct snd_soc_dapm_context *dapm = w->dapm;
722 struct snd_soc_dapm_path *path;
725 if (w->num_kcontrols != 1) {
727 "ASoC: mux %s has incorrect number of controls\n",
732 if (list_empty(&w->sources)) {
733 dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
737 ret = dapm_create_or_share_mixmux_kcontrol(w, 0);
741 list_for_each_entry(path, &w->sources, list_sink)
742 dapm_kcontrol_add_path(w->kcontrols[0], path);
747 /* create new dapm volume control */
748 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
750 if (w->num_kcontrols)
751 dev_err(w->dapm->dev,
752 "ASoC: PGA controls not supported: '%s'\n", w->name);
757 /* reset 'walked' bit for each dapm path */
758 static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
759 struct list_head *sink)
761 struct snd_soc_dapm_path *p;
763 list_for_each_entry(p, sink, list_source) {
766 dapm_clear_walk_output(dapm, &p->sink->sinks);
771 static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
772 struct list_head *source)
774 struct snd_soc_dapm_path *p;
776 list_for_each_entry(p, source, list_sink) {
779 dapm_clear_walk_input(dapm, &p->source->sources);
785 /* We implement power down on suspend by checking the power state of
786 * the ALSA card - when we are suspending the ALSA state for the card
789 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
791 int level = snd_power_get_state(widget->dapm->card->snd_card);
794 case SNDRV_CTL_POWER_D3hot:
795 case SNDRV_CTL_POWER_D3cold:
796 if (widget->ignore_suspend)
797 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
799 return widget->ignore_suspend;
805 /* add widget to list if it's not already in the list */
806 static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
807 struct snd_soc_dapm_widget *w)
809 struct snd_soc_dapm_widget_list *wlist;
810 int wlistsize, wlistentries, i;
817 /* is this widget already in the list */
818 for (i = 0; i < wlist->num_widgets; i++) {
819 if (wlist->widgets[i] == w)
823 /* allocate some new space */
824 wlistentries = wlist->num_widgets + 1;
825 wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
826 wlistentries * sizeof(struct snd_soc_dapm_widget *);
827 *list = krealloc(wlist, wlistsize, GFP_KERNEL);
829 dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
835 /* insert the widget */
836 dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
837 w->name, wlist->num_widgets);
839 wlist->widgets[wlist->num_widgets] = w;
840 wlist->num_widgets++;
845 * Recursively check for a completed path to an active or physically connected
846 * output widget. Returns number of complete paths.
848 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
849 struct snd_soc_dapm_widget_list **list)
851 struct snd_soc_dapm_path *path;
854 if (widget->outputs >= 0)
855 return widget->outputs;
857 DAPM_UPDATE_STAT(widget, path_checks);
859 switch (widget->id) {
860 case snd_soc_dapm_supply:
861 case snd_soc_dapm_regulator_supply:
862 case snd_soc_dapm_clock_supply:
863 case snd_soc_dapm_kcontrol:
869 switch (widget->id) {
870 case snd_soc_dapm_adc:
871 case snd_soc_dapm_aif_out:
872 case snd_soc_dapm_dai_out:
873 if (widget->active) {
874 widget->outputs = snd_soc_dapm_suspend_check(widget);
875 return widget->outputs;
881 if (widget->connected) {
882 /* connected pin ? */
883 if (widget->id == snd_soc_dapm_output && !widget->ext) {
884 widget->outputs = snd_soc_dapm_suspend_check(widget);
885 return widget->outputs;
888 /* connected jack or spk ? */
889 if (widget->id == snd_soc_dapm_hp ||
890 widget->id == snd_soc_dapm_spk ||
891 (widget->id == snd_soc_dapm_line &&
892 !list_empty(&widget->sources))) {
893 widget->outputs = snd_soc_dapm_suspend_check(widget);
894 return widget->outputs;
898 list_for_each_entry(path, &widget->sinks, list_source) {
899 DAPM_UPDATE_STAT(widget, neighbour_checks);
910 trace_snd_soc_dapm_output_path(widget, path);
912 if (path->sink && path->connect) {
916 /* do we need to add this widget to the list ? */
919 err = dapm_list_add_widget(list, path->sink);
921 dev_err(widget->dapm->dev,
922 "ASoC: could not add widget %s\n",
929 con += is_connected_output_ep(path->sink, list);
935 widget->outputs = con;
941 * Recursively check for a completed path to an active or physically connected
942 * input widget. Returns number of complete paths.
944 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
945 struct snd_soc_dapm_widget_list **list)
947 struct snd_soc_dapm_path *path;
950 if (widget->inputs >= 0)
951 return widget->inputs;
953 DAPM_UPDATE_STAT(widget, path_checks);
955 switch (widget->id) {
956 case snd_soc_dapm_supply:
957 case snd_soc_dapm_regulator_supply:
958 case snd_soc_dapm_clock_supply:
959 case snd_soc_dapm_kcontrol:
965 /* active stream ? */
966 switch (widget->id) {
967 case snd_soc_dapm_dac:
968 case snd_soc_dapm_aif_in:
969 case snd_soc_dapm_dai_in:
970 if (widget->active) {
971 widget->inputs = snd_soc_dapm_suspend_check(widget);
972 return widget->inputs;
978 if (widget->connected) {
979 /* connected pin ? */
980 if (widget->id == snd_soc_dapm_input && !widget->ext) {
981 widget->inputs = snd_soc_dapm_suspend_check(widget);
982 return widget->inputs;
985 /* connected VMID/Bias for lower pops */
986 if (widget->id == snd_soc_dapm_vmid) {
987 widget->inputs = snd_soc_dapm_suspend_check(widget);
988 return widget->inputs;
991 /* connected jack ? */
992 if (widget->id == snd_soc_dapm_mic ||
993 (widget->id == snd_soc_dapm_line &&
994 !list_empty(&widget->sinks))) {
995 widget->inputs = snd_soc_dapm_suspend_check(widget);
996 return widget->inputs;
999 /* signal generator */
1000 if (widget->id == snd_soc_dapm_siggen) {
1001 widget->inputs = snd_soc_dapm_suspend_check(widget);
1002 return widget->inputs;
1006 list_for_each_entry(path, &widget->sources, list_sink) {
1007 DAPM_UPDATE_STAT(widget, neighbour_checks);
1018 trace_snd_soc_dapm_input_path(widget, path);
1020 if (path->source && path->connect) {
1024 /* do we need to add this widget to the list ? */
1027 err = dapm_list_add_widget(list, path->source);
1029 dev_err(widget->dapm->dev,
1030 "ASoC: could not add widget %s\n",
1037 con += is_connected_input_ep(path->source, list);
1043 widget->inputs = con;
1049 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1050 * @dai: the soc DAI.
1051 * @stream: stream direction.
1052 * @list: list of active widgets for this stream.
1054 * Queries DAPM graph as to whether an valid audio stream path exists for
1055 * the initial stream specified by name. This takes into account
1056 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1058 * Returns the number of valid paths or negative error.
1060 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1061 struct snd_soc_dapm_widget_list **list)
1063 struct snd_soc_card *card = dai->card;
1066 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1069 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1070 paths = is_connected_output_ep(dai->playback_widget, list);
1071 dapm_clear_walk_output(&card->dapm,
1072 &dai->playback_widget->sinks);
1074 paths = is_connected_input_ep(dai->capture_widget, list);
1075 dapm_clear_walk_input(&card->dapm,
1076 &dai->capture_widget->sources);
1079 trace_snd_soc_dapm_connected(paths, stream);
1080 mutex_unlock(&card->dapm_mutex);
1086 * Handler for regulator supply widget.
1088 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1089 struct snd_kcontrol *kcontrol, int event)
1093 soc_dapm_async_complete(w->dapm);
1095 if (SND_SOC_DAPM_EVENT_ON(event)) {
1096 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1097 ret = regulator_allow_bypass(w->regulator, false);
1099 dev_warn(w->dapm->dev,
1100 "ASoC: Failed to unbypass %s: %d\n",
1104 return regulator_enable(w->regulator);
1106 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1107 ret = regulator_allow_bypass(w->regulator, true);
1109 dev_warn(w->dapm->dev,
1110 "ASoC: Failed to bypass %s: %d\n",
1114 return regulator_disable_deferred(w->regulator, w->shift);
1117 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1120 * Handler for clock supply widget.
1122 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1123 struct snd_kcontrol *kcontrol, int event)
1128 soc_dapm_async_complete(w->dapm);
1130 #ifdef CONFIG_HAVE_CLK
1131 if (SND_SOC_DAPM_EVENT_ON(event)) {
1132 return clk_prepare_enable(w->clk);
1134 clk_disable_unprepare(w->clk);
1140 EXPORT_SYMBOL_GPL(dapm_clock_event);
1142 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1144 if (w->power_checked)
1145 return w->new_power;
1150 w->new_power = w->power_check(w);
1152 w->power_checked = true;
1154 return w->new_power;
1157 /* Generic check to see if a widget should be powered.
1159 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1163 DAPM_UPDATE_STAT(w, power_checks);
1165 in = is_connected_input_ep(w, NULL);
1166 dapm_clear_walk_input(w->dapm, &w->sources);
1167 out = is_connected_output_ep(w, NULL);
1168 dapm_clear_walk_output(w->dapm, &w->sinks);
1169 return out != 0 && in != 0;
1172 /* Check to see if an ADC has power */
1173 static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1177 DAPM_UPDATE_STAT(w, power_checks);
1180 in = is_connected_input_ep(w, NULL);
1181 dapm_clear_walk_input(w->dapm, &w->sources);
1184 return dapm_generic_check_power(w);
1188 /* Check to see if a DAC has power */
1189 static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1193 DAPM_UPDATE_STAT(w, power_checks);
1196 out = is_connected_output_ep(w, NULL);
1197 dapm_clear_walk_output(w->dapm, &w->sinks);
1200 return dapm_generic_check_power(w);
1204 /* Check to see if a power supply is needed */
1205 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1207 struct snd_soc_dapm_path *path;
1209 DAPM_UPDATE_STAT(w, power_checks);
1211 /* Check if one of our outputs is connected */
1212 list_for_each_entry(path, &w->sinks, list_source) {
1213 DAPM_UPDATE_STAT(w, neighbour_checks);
1218 if (path->connected &&
1219 !path->connected(path->source, path->sink))
1225 if (dapm_widget_power_check(path->sink))
1232 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1237 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1238 struct snd_soc_dapm_widget *b,
1246 sort = dapm_down_seq;
1248 if (sort[a->id] != sort[b->id])
1249 return sort[a->id] - sort[b->id];
1250 if (a->subseq != b->subseq) {
1252 return a->subseq - b->subseq;
1254 return b->subseq - a->subseq;
1256 if (a->reg != b->reg)
1257 return a->reg - b->reg;
1258 if (a->dapm != b->dapm)
1259 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1264 /* Insert a widget in order into a DAPM power sequence. */
1265 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1266 struct list_head *list,
1269 struct snd_soc_dapm_widget *w;
1271 list_for_each_entry(w, list, power_list)
1272 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1273 list_add_tail(&new_widget->power_list, &w->power_list);
1277 list_add_tail(&new_widget->power_list, list);
1280 static void dapm_seq_check_event(struct snd_soc_card *card,
1281 struct snd_soc_dapm_widget *w, int event)
1283 const char *ev_name;
1287 case SND_SOC_DAPM_PRE_PMU:
1288 ev_name = "PRE_PMU";
1291 case SND_SOC_DAPM_POST_PMU:
1292 ev_name = "POST_PMU";
1295 case SND_SOC_DAPM_PRE_PMD:
1296 ev_name = "PRE_PMD";
1299 case SND_SOC_DAPM_POST_PMD:
1300 ev_name = "POST_PMD";
1303 case SND_SOC_DAPM_WILL_PMU:
1304 ev_name = "WILL_PMU";
1307 case SND_SOC_DAPM_WILL_PMD:
1308 ev_name = "WILL_PMD";
1312 WARN(1, "Unknown event %d\n", event);
1316 if (w->new_power != power)
1319 if (w->event && (w->event_flags & event)) {
1320 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1322 soc_dapm_async_complete(w->dapm);
1323 trace_snd_soc_dapm_widget_event_start(w, event);
1324 ret = w->event(w, NULL, event);
1325 trace_snd_soc_dapm_widget_event_done(w, event);
1327 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1328 ev_name, w->name, ret);
1332 /* Apply the coalesced changes from a DAPM sequence */
1333 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1334 struct list_head *pending)
1336 struct snd_soc_dapm_context *dapm;
1337 struct snd_soc_dapm_widget *w;
1339 unsigned int value = 0;
1340 unsigned int mask = 0;
1342 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1346 list_for_each_entry(w, pending, power_list) {
1347 WARN_ON(reg != w->reg || dapm != w->dapm);
1348 w->power = w->new_power;
1350 mask |= w->mask << w->shift;
1352 value |= w->on_val << w->shift;
1354 value |= w->off_val << w->shift;
1356 pop_dbg(dapm->dev, card->pop_time,
1357 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1358 w->name, reg, value, mask);
1360 /* Check for events */
1361 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1362 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1366 /* Any widget will do, they should all be updating the
1370 pop_dbg(dapm->dev, card->pop_time,
1371 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1372 value, mask, reg, card->pop_time);
1373 pop_wait(card->pop_time);
1374 soc_dapm_update_bits(dapm, reg, mask, value);
1377 list_for_each_entry(w, pending, power_list) {
1378 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1379 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1383 /* Apply a DAPM power sequence.
1385 * We walk over a pre-sorted list of widgets to apply power to. In
1386 * order to minimise the number of writes to the device required
1387 * multiple widgets will be updated in a single write where possible.
1388 * Currently anything that requires more than a single write is not
1391 static void dapm_seq_run(struct snd_soc_card *card,
1392 struct list_head *list, int event, bool power_up)
1394 struct snd_soc_dapm_widget *w, *n;
1395 struct snd_soc_dapm_context *d;
1398 int cur_subseq = -1;
1399 int cur_reg = SND_SOC_NOPM;
1400 struct snd_soc_dapm_context *cur_dapm = NULL;
1407 sort = dapm_down_seq;
1409 list_for_each_entry_safe(w, n, list, power_list) {
1412 /* Do we need to apply any queued changes? */
1413 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1414 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1415 if (!list_empty(&pending))
1416 dapm_seq_run_coalesced(card, &pending);
1418 if (cur_dapm && cur_dapm->seq_notifier) {
1419 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1420 if (sort[i] == cur_sort)
1421 cur_dapm->seq_notifier(cur_dapm,
1426 if (cur_dapm && w->dapm != cur_dapm)
1427 soc_dapm_async_complete(cur_dapm);
1429 INIT_LIST_HEAD(&pending);
1431 cur_subseq = INT_MIN;
1432 cur_reg = SND_SOC_NOPM;
1437 case snd_soc_dapm_pre:
1439 list_for_each_entry_safe_continue(w, n, list,
1442 if (event == SND_SOC_DAPM_STREAM_START)
1444 NULL, SND_SOC_DAPM_PRE_PMU);
1445 else if (event == SND_SOC_DAPM_STREAM_STOP)
1447 NULL, SND_SOC_DAPM_PRE_PMD);
1450 case snd_soc_dapm_post:
1452 list_for_each_entry_safe_continue(w, n, list,
1455 if (event == SND_SOC_DAPM_STREAM_START)
1457 NULL, SND_SOC_DAPM_POST_PMU);
1458 else if (event == SND_SOC_DAPM_STREAM_STOP)
1460 NULL, SND_SOC_DAPM_POST_PMD);
1464 /* Queue it up for application */
1465 cur_sort = sort[w->id];
1466 cur_subseq = w->subseq;
1469 list_move(&w->power_list, &pending);
1474 dev_err(w->dapm->dev,
1475 "ASoC: Failed to apply widget power: %d\n", ret);
1478 if (!list_empty(&pending))
1479 dapm_seq_run_coalesced(card, &pending);
1481 if (cur_dapm && cur_dapm->seq_notifier) {
1482 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1483 if (sort[i] == cur_sort)
1484 cur_dapm->seq_notifier(cur_dapm,
1488 list_for_each_entry(d, &card->dapm_list, list) {
1489 soc_dapm_async_complete(d);
1493 static void dapm_widget_update(struct snd_soc_card *card)
1495 struct snd_soc_dapm_update *update = card->update;
1496 struct snd_soc_dapm_widget_list *wlist;
1497 struct snd_soc_dapm_widget *w = NULL;
1501 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1504 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1506 for (wi = 0; wi < wlist->num_widgets; wi++) {
1507 w = wlist->widgets[wi];
1509 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1510 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1512 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1520 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1523 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1526 for (wi = 0; wi < wlist->num_widgets; wi++) {
1527 w = wlist->widgets[wi];
1529 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1530 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1532 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1538 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1539 * they're changing state.
1541 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1543 struct snd_soc_dapm_context *d = data;
1546 /* If we're off and we're not supposed to be go into STANDBY */
1547 if (d->bias_level == SND_SOC_BIAS_OFF &&
1548 d->target_bias_level != SND_SOC_BIAS_OFF) {
1550 pm_runtime_get_sync(d->dev);
1552 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1555 "ASoC: Failed to turn on bias: %d\n", ret);
1558 /* Prepare for a transition to ON or away from ON */
1559 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1560 d->bias_level != SND_SOC_BIAS_ON) ||
1561 (d->target_bias_level != SND_SOC_BIAS_ON &&
1562 d->bias_level == SND_SOC_BIAS_ON)) {
1563 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1566 "ASoC: Failed to prepare bias: %d\n", ret);
1570 /* Async callback run prior to DAPM sequences - brings to their final
1573 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1575 struct snd_soc_dapm_context *d = data;
1578 /* If we just powered the last thing off drop to standby bias */
1579 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1580 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1581 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1582 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1584 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1588 /* If we're in standby and can support bias off then do that */
1589 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1590 d->target_bias_level == SND_SOC_BIAS_OFF) {
1591 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1593 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1597 pm_runtime_put(d->dev);
1600 /* If we just powered up then move to active bias */
1601 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1602 d->target_bias_level == SND_SOC_BIAS_ON) {
1603 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1605 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1610 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1611 bool power, bool connect)
1613 /* If a connection is being made or broken then that update
1614 * will have marked the peer dirty, otherwise the widgets are
1615 * not connected and this update has no impact. */
1619 /* If the peer is already in the state we're moving to then we
1620 * won't have an impact on it. */
1621 if (power != peer->power)
1622 dapm_mark_dirty(peer, "peer state change");
1625 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1626 struct list_head *up_list,
1627 struct list_head *down_list)
1629 struct snd_soc_dapm_path *path;
1631 if (w->power == power)
1634 trace_snd_soc_dapm_widget_power(w, power);
1636 /* If we changed our power state perhaps our neigbours changed
1639 list_for_each_entry(path, &w->sources, list_sink) {
1641 dapm_widget_set_peer_power(path->source, power,
1646 case snd_soc_dapm_supply:
1647 case snd_soc_dapm_regulator_supply:
1648 case snd_soc_dapm_clock_supply:
1649 case snd_soc_dapm_kcontrol:
1650 /* Supplies can't affect their outputs, only their inputs */
1653 list_for_each_entry(path, &w->sinks, list_source) {
1655 dapm_widget_set_peer_power(path->sink, power,
1663 dapm_seq_insert(w, up_list, true);
1665 dapm_seq_insert(w, down_list, false);
1668 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1669 struct list_head *up_list,
1670 struct list_head *down_list)
1675 case snd_soc_dapm_pre:
1676 dapm_seq_insert(w, down_list, false);
1678 case snd_soc_dapm_post:
1679 dapm_seq_insert(w, up_list, true);
1683 power = dapm_widget_power_check(w);
1685 dapm_widget_set_power(w, power, up_list, down_list);
1690 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1692 if (dapm->idle_bias_off)
1695 switch (snd_power_get_state(dapm->card->snd_card)) {
1696 case SNDRV_CTL_POWER_D3hot:
1697 case SNDRV_CTL_POWER_D3cold:
1698 return dapm->suspend_bias_off;
1707 * Scan each dapm widget for complete audio path.
1708 * A complete path is a route that has valid endpoints i.e.:-
1710 * o DAC to output pin.
1711 * o Input Pin to ADC.
1712 * o Input pin to Output pin (bypass, sidetone)
1713 * o DAC to ADC (loopback).
1715 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1717 struct snd_soc_dapm_widget *w;
1718 struct snd_soc_dapm_context *d;
1720 LIST_HEAD(down_list);
1721 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1722 enum snd_soc_bias_level bias;
1724 lockdep_assert_held(&card->dapm_mutex);
1726 trace_snd_soc_dapm_start(card);
1728 list_for_each_entry(d, &card->dapm_list, list) {
1729 if (dapm_idle_bias_off(d))
1730 d->target_bias_level = SND_SOC_BIAS_OFF;
1732 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1737 /* Check which widgets we need to power and store them in
1738 * lists indicating if they should be powered up or down. We
1739 * only check widgets that have been flagged as dirty but note
1740 * that new widgets may be added to the dirty list while we
1743 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1744 dapm_power_one_widget(w, &up_list, &down_list);
1747 list_for_each_entry(w, &card->widgets, list) {
1749 case snd_soc_dapm_pre:
1750 case snd_soc_dapm_post:
1751 /* These widgets always need to be powered */
1754 list_del_init(&w->dirty);
1761 /* Supplies and micbiases only bring the
1762 * context up to STANDBY as unless something
1763 * else is active and passing audio they
1764 * generally don't require full power. Signal
1765 * generators are virtual pins and have no
1766 * power impact themselves.
1769 case snd_soc_dapm_siggen:
1770 case snd_soc_dapm_vmid:
1772 case snd_soc_dapm_supply:
1773 case snd_soc_dapm_regulator_supply:
1774 case snd_soc_dapm_clock_supply:
1775 case snd_soc_dapm_micbias:
1776 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1777 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1780 d->target_bias_level = SND_SOC_BIAS_ON;
1787 /* Force all contexts in the card to the same bias state if
1788 * they're not ground referenced.
1790 bias = SND_SOC_BIAS_OFF;
1791 list_for_each_entry(d, &card->dapm_list, list)
1792 if (d->target_bias_level > bias)
1793 bias = d->target_bias_level;
1794 list_for_each_entry(d, &card->dapm_list, list)
1795 if (!dapm_idle_bias_off(d))
1796 d->target_bias_level = bias;
1798 trace_snd_soc_dapm_walk_done(card);
1800 /* Run card bias changes at first */
1801 dapm_pre_sequence_async(&card->dapm, 0);
1802 /* Run other bias changes in parallel */
1803 list_for_each_entry(d, &card->dapm_list, list) {
1804 if (d != &card->dapm)
1805 async_schedule_domain(dapm_pre_sequence_async, d,
1808 async_synchronize_full_domain(&async_domain);
1810 list_for_each_entry(w, &down_list, power_list) {
1811 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
1814 list_for_each_entry(w, &up_list, power_list) {
1815 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
1818 /* Power down widgets first; try to avoid amplifying pops. */
1819 dapm_seq_run(card, &down_list, event, false);
1821 dapm_widget_update(card);
1824 dapm_seq_run(card, &up_list, event, true);
1826 /* Run all the bias changes in parallel */
1827 list_for_each_entry(d, &card->dapm_list, list) {
1828 if (d != &card->dapm)
1829 async_schedule_domain(dapm_post_sequence_async, d,
1832 async_synchronize_full_domain(&async_domain);
1833 /* Run card bias changes at last */
1834 dapm_post_sequence_async(&card->dapm, 0);
1836 /* do we need to notify any clients that DAPM event is complete */
1837 list_for_each_entry(d, &card->dapm_list, list) {
1838 if (d->stream_event)
1839 d->stream_event(d, event);
1842 pop_dbg(card->dev, card->pop_time,
1843 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1844 pop_wait(card->pop_time);
1846 trace_snd_soc_dapm_done(card);
1851 #ifdef CONFIG_DEBUG_FS
1852 static ssize_t dapm_widget_power_read_file(struct file *file,
1853 char __user *user_buf,
1854 size_t count, loff_t *ppos)
1856 struct snd_soc_dapm_widget *w = file->private_data;
1860 struct snd_soc_dapm_path *p = NULL;
1862 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1866 in = is_connected_input_ep(w, NULL);
1867 dapm_clear_walk_input(w->dapm, &w->sources);
1868 out = is_connected_output_ep(w, NULL);
1869 dapm_clear_walk_output(w->dapm, &w->sinks);
1871 ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
1872 w->name, w->power ? "On" : "Off",
1873 w->force ? " (forced)" : "", in, out);
1876 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1877 " - R%d(0x%x) mask 0x%x",
1878 w->reg, w->reg, w->mask << w->shift);
1880 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1883 ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1885 w->active ? "active" : "inactive");
1887 list_for_each_entry(p, &w->sources, list_sink) {
1888 if (p->connected && !p->connected(w, p->source))
1892 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1893 " in \"%s\" \"%s\"\n",
1894 p->name ? p->name : "static",
1897 list_for_each_entry(p, &w->sinks, list_source) {
1898 if (p->connected && !p->connected(w, p->sink))
1902 ret += snprintf(buf + ret, PAGE_SIZE - ret,
1903 " out \"%s\" \"%s\"\n",
1904 p->name ? p->name : "static",
1908 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1914 static const struct file_operations dapm_widget_power_fops = {
1915 .open = simple_open,
1916 .read = dapm_widget_power_read_file,
1917 .llseek = default_llseek,
1920 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1921 size_t count, loff_t *ppos)
1923 struct snd_soc_dapm_context *dapm = file->private_data;
1926 switch (dapm->bias_level) {
1927 case SND_SOC_BIAS_ON:
1930 case SND_SOC_BIAS_PREPARE:
1931 level = "Prepare\n";
1933 case SND_SOC_BIAS_STANDBY:
1934 level = "Standby\n";
1936 case SND_SOC_BIAS_OFF:
1940 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
1941 level = "Unknown\n";
1945 return simple_read_from_buffer(user_buf, count, ppos, level,
1949 static const struct file_operations dapm_bias_fops = {
1950 .open = simple_open,
1951 .read = dapm_bias_read_file,
1952 .llseek = default_llseek,
1955 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1956 struct dentry *parent)
1960 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1962 if (!dapm->debugfs_dapm) {
1964 "ASoC: Failed to create DAPM debugfs directory\n");
1968 d = debugfs_create_file("bias_level", 0444,
1969 dapm->debugfs_dapm, dapm,
1973 "ASoC: Failed to create bias level debugfs file\n");
1976 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1978 struct snd_soc_dapm_context *dapm = w->dapm;
1981 if (!dapm->debugfs_dapm || !w->name)
1984 d = debugfs_create_file(w->name, 0444,
1985 dapm->debugfs_dapm, w,
1986 &dapm_widget_power_fops);
1988 dev_warn(w->dapm->dev,
1989 "ASoC: Failed to create %s debugfs file\n",
1993 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1995 debugfs_remove_recursive(dapm->debugfs_dapm);
1999 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2000 struct dentry *parent)
2004 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2008 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2014 /* test and update the power status of a mux widget */
2015 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2016 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2018 struct snd_soc_dapm_path *path;
2021 lockdep_assert_held(&card->dapm_mutex);
2023 /* find dapm widget path assoc with kcontrol */
2024 dapm_kcontrol_for_each_path(path, kcontrol) {
2025 if (!path->name || !e->texts[mux])
2029 /* we now need to match the string in the enum to the path */
2030 if (!(strcmp(path->name, e->texts[mux]))) {
2031 path->connect = 1; /* new connection */
2032 dapm_mark_dirty(path->source, "mux connection");
2035 dapm_mark_dirty(path->source,
2036 "mux disconnection");
2037 path->connect = 0; /* old connection must be powered down */
2039 dapm_mark_dirty(path->sink, "mux change");
2043 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2048 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2049 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2050 struct snd_soc_dapm_update *update)
2052 struct snd_soc_card *card = dapm->card;
2055 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2056 card->update = update;
2057 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2058 card->update = NULL;
2059 mutex_unlock(&card->dapm_mutex);
2061 soc_dpcm_runtime_update(card);
2064 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2066 /* test and update the power status of a mixer or switch widget */
2067 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2068 struct snd_kcontrol *kcontrol, int connect)
2070 struct snd_soc_dapm_path *path;
2073 lockdep_assert_held(&card->dapm_mutex);
2075 /* find dapm widget path assoc with kcontrol */
2076 dapm_kcontrol_for_each_path(path, kcontrol) {
2078 path->connect = connect;
2079 dapm_mark_dirty(path->source, "mixer connection");
2080 dapm_mark_dirty(path->sink, "mixer update");
2084 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2089 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2090 struct snd_kcontrol *kcontrol, int connect,
2091 struct snd_soc_dapm_update *update)
2093 struct snd_soc_card *card = dapm->card;
2096 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2097 card->update = update;
2098 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2099 card->update = NULL;
2100 mutex_unlock(&card->dapm_mutex);
2102 soc_dpcm_runtime_update(card);
2105 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2107 static ssize_t dapm_widget_show_codec(struct snd_soc_codec *codec, char *buf)
2109 struct snd_soc_dapm_widget *w;
2111 char *state = "not set";
2113 list_for_each_entry(w, &codec->component.card->widgets, list) {
2114 if (w->dapm != &codec->dapm)
2117 /* only display widgets that burnm power */
2119 case snd_soc_dapm_hp:
2120 case snd_soc_dapm_mic:
2121 case snd_soc_dapm_spk:
2122 case snd_soc_dapm_line:
2123 case snd_soc_dapm_micbias:
2124 case snd_soc_dapm_dac:
2125 case snd_soc_dapm_adc:
2126 case snd_soc_dapm_pga:
2127 case snd_soc_dapm_out_drv:
2128 case snd_soc_dapm_mixer:
2129 case snd_soc_dapm_mixer_named_ctl:
2130 case snd_soc_dapm_supply:
2131 case snd_soc_dapm_regulator_supply:
2132 case snd_soc_dapm_clock_supply:
2134 count += sprintf(buf + count, "%s: %s\n",
2135 w->name, w->power ? "On":"Off");
2142 switch (codec->dapm.bias_level) {
2143 case SND_SOC_BIAS_ON:
2146 case SND_SOC_BIAS_PREPARE:
2149 case SND_SOC_BIAS_STANDBY:
2152 case SND_SOC_BIAS_OFF:
2156 count += sprintf(buf + count, "PM State: %s\n", state);
2161 /* show dapm widget status in sys fs */
2162 static ssize_t dapm_widget_show(struct device *dev,
2163 struct device_attribute *attr, char *buf)
2165 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2168 for (i = 0; i < rtd->num_codecs; i++) {
2169 struct snd_soc_codec *codec = rtd->codec_dais[i]->codec;
2170 count += dapm_widget_show_codec(codec, buf + count);
2176 static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2178 int snd_soc_dapm_sys_add(struct device *dev)
2180 return device_create_file(dev, &dev_attr_dapm_widget);
2183 static void snd_soc_dapm_sys_remove(struct device *dev)
2185 device_remove_file(dev, &dev_attr_dapm_widget);
2188 static void dapm_free_path(struct snd_soc_dapm_path *path)
2190 list_del(&path->list_sink);
2191 list_del(&path->list_source);
2192 list_del(&path->list_kcontrol);
2193 list_del(&path->list);
2197 /* free all dapm widgets and resources */
2198 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2200 struct snd_soc_dapm_widget *w, *next_w;
2201 struct snd_soc_dapm_path *p, *next_p;
2203 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2204 if (w->dapm != dapm)
2208 * remove source and sink paths associated to this widget.
2209 * While removing the path, remove reference to it from both
2210 * source and sink widgets so that path is removed only once.
2212 list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2215 list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2218 kfree(w->kcontrols);
2224 static struct snd_soc_dapm_widget *dapm_find_widget(
2225 struct snd_soc_dapm_context *dapm, const char *pin,
2226 bool search_other_contexts)
2228 struct snd_soc_dapm_widget *w;
2229 struct snd_soc_dapm_widget *fallback = NULL;
2231 list_for_each_entry(w, &dapm->card->widgets, list) {
2232 if (!strcmp(w->name, pin)) {
2233 if (w->dapm == dapm)
2240 if (search_other_contexts)
2246 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2247 const char *pin, int status)
2249 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2251 dapm_assert_locked(dapm);
2254 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2258 if (w->connected != status)
2259 dapm_mark_dirty(w, "pin configuration");
2261 w->connected = status;
2269 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2270 * @dapm: DAPM context
2272 * Walks all dapm audio paths and powers widgets according to their
2273 * stream or path usage.
2275 * Requires external locking.
2277 * Returns 0 for success.
2279 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2282 * Suppress early reports (eg, jacks syncing their state) to avoid
2283 * silly DAPM runs during card startup.
2285 if (!dapm->card || !dapm->card->instantiated)
2288 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2290 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2293 * snd_soc_dapm_sync - scan and power dapm paths
2294 * @dapm: DAPM context
2296 * Walks all dapm audio paths and powers widgets according to their
2297 * stream or path usage.
2299 * Returns 0 for success.
2301 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2305 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2306 ret = snd_soc_dapm_sync_unlocked(dapm);
2307 mutex_unlock(&dapm->card->dapm_mutex);
2310 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2312 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2313 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2314 const char *control,
2315 int (*connected)(struct snd_soc_dapm_widget *source,
2316 struct snd_soc_dapm_widget *sink))
2318 struct snd_soc_dapm_path *path;
2321 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2325 path->source = wsource;
2327 path->connected = connected;
2328 INIT_LIST_HEAD(&path->list);
2329 INIT_LIST_HEAD(&path->list_kcontrol);
2330 INIT_LIST_HEAD(&path->list_source);
2331 INIT_LIST_HEAD(&path->list_sink);
2333 /* check for external widgets */
2334 if (wsink->id == snd_soc_dapm_input) {
2335 if (wsource->id == snd_soc_dapm_micbias ||
2336 wsource->id == snd_soc_dapm_mic ||
2337 wsource->id == snd_soc_dapm_line ||
2338 wsource->id == snd_soc_dapm_output)
2341 if (wsource->id == snd_soc_dapm_output) {
2342 if (wsink->id == snd_soc_dapm_spk ||
2343 wsink->id == snd_soc_dapm_hp ||
2344 wsink->id == snd_soc_dapm_line ||
2345 wsink->id == snd_soc_dapm_input)
2349 dapm_mark_dirty(wsource, "Route added");
2350 dapm_mark_dirty(wsink, "Route added");
2352 /* connect static paths */
2353 if (control == NULL) {
2354 list_add(&path->list, &dapm->card->paths);
2355 list_add(&path->list_sink, &wsink->sources);
2356 list_add(&path->list_source, &wsource->sinks);
2361 /* connect dynamic paths */
2362 switch (wsink->id) {
2363 case snd_soc_dapm_adc:
2364 case snd_soc_dapm_dac:
2365 case snd_soc_dapm_pga:
2366 case snd_soc_dapm_out_drv:
2367 case snd_soc_dapm_input:
2368 case snd_soc_dapm_output:
2369 case snd_soc_dapm_siggen:
2370 case snd_soc_dapm_micbias:
2371 case snd_soc_dapm_vmid:
2372 case snd_soc_dapm_pre:
2373 case snd_soc_dapm_post:
2374 case snd_soc_dapm_supply:
2375 case snd_soc_dapm_regulator_supply:
2376 case snd_soc_dapm_clock_supply:
2377 case snd_soc_dapm_aif_in:
2378 case snd_soc_dapm_aif_out:
2379 case snd_soc_dapm_dai_in:
2380 case snd_soc_dapm_dai_out:
2381 case snd_soc_dapm_dai_link:
2382 case snd_soc_dapm_kcontrol:
2383 list_add(&path->list, &dapm->card->paths);
2384 list_add(&path->list_sink, &wsink->sources);
2385 list_add(&path->list_source, &wsource->sinks);
2388 case snd_soc_dapm_mux:
2389 ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2390 &wsink->kcontrol_news[0]);
2394 case snd_soc_dapm_switch:
2395 case snd_soc_dapm_mixer:
2396 case snd_soc_dapm_mixer_named_ctl:
2397 ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2401 case snd_soc_dapm_hp:
2402 case snd_soc_dapm_mic:
2403 case snd_soc_dapm_line:
2404 case snd_soc_dapm_spk:
2405 list_add(&path->list, &dapm->card->paths);
2406 list_add(&path->list_sink, &wsink->sources);
2407 list_add(&path->list_source, &wsource->sinks);
2418 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2419 const struct snd_soc_dapm_route *route)
2421 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2422 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2425 char prefixed_sink[80];
2426 char prefixed_source[80];
2430 prefix = soc_dapm_prefix(dapm);
2432 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2433 prefix, route->sink);
2434 sink = prefixed_sink;
2435 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2436 prefix, route->source);
2437 source = prefixed_source;
2440 source = route->source;
2444 * find src and dest widgets over all widgets but favor a widget from
2445 * current DAPM context
2447 list_for_each_entry(w, &dapm->card->widgets, list) {
2448 if (!wsink && !(strcmp(w->name, sink))) {
2450 if (w->dapm == dapm)
2454 if (!wsource && !(strcmp(w->name, source))) {
2456 if (w->dapm == dapm)
2460 /* use widget from another DAPM context if not found from this */
2466 if (wsource == NULL) {
2467 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2471 if (wsink == NULL) {
2472 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2477 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2484 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2485 source, route->control, sink);
2489 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2490 const struct snd_soc_dapm_route *route)
2492 struct snd_soc_dapm_path *path, *p;
2495 char prefixed_sink[80];
2496 char prefixed_source[80];
2499 if (route->control) {
2501 "ASoC: Removal of routes with controls not supported\n");
2505 prefix = soc_dapm_prefix(dapm);
2507 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2508 prefix, route->sink);
2509 sink = prefixed_sink;
2510 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2511 prefix, route->source);
2512 source = prefixed_source;
2515 source = route->source;
2519 list_for_each_entry(p, &dapm->card->paths, list) {
2520 if (strcmp(p->source->name, source) != 0)
2522 if (strcmp(p->sink->name, sink) != 0)
2529 dapm_mark_dirty(path->source, "Route removed");
2530 dapm_mark_dirty(path->sink, "Route removed");
2532 dapm_free_path(path);
2534 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2542 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2543 * @dapm: DAPM context
2544 * @route: audio routes
2545 * @num: number of routes
2547 * Connects 2 dapm widgets together via a named audio path. The sink is
2548 * the widget receiving the audio signal, whilst the source is the sender
2549 * of the audio signal.
2551 * Returns 0 for success else error. On error all resources can be freed
2552 * with a call to snd_soc_card_free().
2554 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2555 const struct snd_soc_dapm_route *route, int num)
2559 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2560 for (i = 0; i < num; i++) {
2561 r = snd_soc_dapm_add_route(dapm, route);
2563 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2565 route->control ? route->control : "direct",
2571 mutex_unlock(&dapm->card->dapm_mutex);
2575 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2578 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2579 * @dapm: DAPM context
2580 * @route: audio routes
2581 * @num: number of routes
2583 * Removes routes from the DAPM context.
2585 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2586 const struct snd_soc_dapm_route *route, int num)
2590 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2591 for (i = 0; i < num; i++) {
2592 snd_soc_dapm_del_route(dapm, route);
2595 mutex_unlock(&dapm->card->dapm_mutex);
2599 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2601 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2602 const struct snd_soc_dapm_route *route)
2604 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2607 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2610 struct snd_soc_dapm_path *path;
2614 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2620 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2625 if (route->control || route->connected)
2626 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2627 route->source, route->sink);
2629 list_for_each_entry(path, &source->sinks, list_source) {
2630 if (path->sink == sink) {
2637 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2638 route->source, route->sink);
2640 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2641 count, route->source, route->sink);
2647 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2648 * @dapm: DAPM context
2649 * @route: audio routes
2650 * @num: number of routes
2652 * Mark existing routes matching those specified in the passed array
2653 * as being weak, meaning that they are ignored for the purpose of
2654 * power decisions. The main intended use case is for sidetone paths
2655 * which couple audio between other independent paths if they are both
2656 * active in order to make the combination work better at the user
2657 * level but which aren't intended to be "used".
2659 * Note that CODEC drivers should not use this as sidetone type paths
2660 * can frequently also be used as bypass paths.
2662 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2663 const struct snd_soc_dapm_route *route, int num)
2668 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2669 for (i = 0; i < num; i++) {
2670 err = snd_soc_dapm_weak_route(dapm, route);
2675 mutex_unlock(&dapm->card->dapm_mutex);
2679 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2682 * snd_soc_dapm_new_widgets - add new dapm widgets
2683 * @dapm: DAPM context
2685 * Checks the codec for any new dapm widgets and creates them if found.
2687 * Returns 0 for success.
2689 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
2691 struct snd_soc_dapm_widget *w;
2694 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2696 list_for_each_entry(w, &card->widgets, list)
2701 if (w->num_kcontrols) {
2702 w->kcontrols = kzalloc(w->num_kcontrols *
2703 sizeof(struct snd_kcontrol *),
2705 if (!w->kcontrols) {
2706 mutex_unlock(&card->dapm_mutex);
2712 case snd_soc_dapm_switch:
2713 case snd_soc_dapm_mixer:
2714 case snd_soc_dapm_mixer_named_ctl:
2717 case snd_soc_dapm_mux:
2720 case snd_soc_dapm_pga:
2721 case snd_soc_dapm_out_drv:
2728 /* Read the initial power state from the device */
2730 soc_dapm_read(w->dapm, w->reg, &val);
2731 val = val >> w->shift;
2733 if (val == w->on_val)
2739 dapm_mark_dirty(w, "new widget");
2740 dapm_debugfs_add_widget(w);
2743 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2744 mutex_unlock(&card->dapm_mutex);
2747 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2750 * snd_soc_dapm_get_volsw - dapm mixer get callback
2751 * @kcontrol: mixer control
2752 * @ucontrol: control element information
2754 * Callback to get the value of a dapm mixer control.
2756 * Returns 0 for success.
2758 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2761 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2762 struct snd_soc_card *card = dapm->card;
2763 struct soc_mixer_control *mc =
2764 (struct soc_mixer_control *)kcontrol->private_value;
2766 unsigned int shift = mc->shift;
2768 unsigned int mask = (1 << fls(max)) - 1;
2769 unsigned int invert = mc->invert;
2773 if (snd_soc_volsw_is_stereo(mc))
2775 "ASoC: Control '%s' is stereo, which is not supported\n",
2778 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2779 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
2780 ret = soc_dapm_read(dapm, reg, &val);
2781 val = (val >> shift) & mask;
2783 val = dapm_kcontrol_get_value(kcontrol);
2785 mutex_unlock(&card->dapm_mutex);
2788 ucontrol->value.integer.value[0] = max - val;
2790 ucontrol->value.integer.value[0] = val;
2794 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2797 * snd_soc_dapm_put_volsw - dapm mixer set callback
2798 * @kcontrol: mixer control
2799 * @ucontrol: control element information
2801 * Callback to set the value of a dapm mixer control.
2803 * Returns 0 for success.
2805 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_value *ucontrol)
2808 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2809 struct snd_soc_card *card = dapm->card;
2810 struct soc_mixer_control *mc =
2811 (struct soc_mixer_control *)kcontrol->private_value;
2813 unsigned int shift = mc->shift;
2815 unsigned int mask = (1 << fls(max)) - 1;
2816 unsigned int invert = mc->invert;
2818 int connect, change, reg_change = 0;
2819 struct snd_soc_dapm_update update;
2822 if (snd_soc_volsw_is_stereo(mc))
2824 "ASoC: Control '%s' is stereo, which is not supported\n",
2827 val = (ucontrol->value.integer.value[0] & mask);
2833 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2835 change = dapm_kcontrol_set_value(kcontrol, val);
2837 if (reg != SND_SOC_NOPM) {
2838 mask = mask << shift;
2841 reg_change = soc_dapm_test_bits(dapm, reg, mask, val);
2844 if (change || reg_change) {
2846 update.kcontrol = kcontrol;
2850 card->update = &update;
2852 change |= reg_change;
2854 ret = soc_dapm_mixer_update_power(card, kcontrol, connect);
2856 card->update = NULL;
2859 mutex_unlock(&card->dapm_mutex);
2862 soc_dpcm_runtime_update(card);
2866 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2869 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2870 * @kcontrol: mixer control
2871 * @ucontrol: control element information
2873 * Callback to get the value of a dapm enumerated double mixer control.
2875 * Returns 0 for success.
2877 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2880 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2881 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2882 unsigned int reg_val, val;
2884 if (e->reg != SND_SOC_NOPM) {
2885 int ret = soc_dapm_read(dapm, e->reg, ®_val);
2889 reg_val = dapm_kcontrol_get_value(kcontrol);
2892 val = (reg_val >> e->shift_l) & e->mask;
2893 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
2894 if (e->shift_l != e->shift_r) {
2895 val = (reg_val >> e->shift_r) & e->mask;
2896 val = snd_soc_enum_val_to_item(e, val);
2897 ucontrol->value.enumerated.item[1] = val;
2902 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2905 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2906 * @kcontrol: mixer control
2907 * @ucontrol: control element information
2909 * Callback to set the value of a dapm enumerated double mixer control.
2911 * Returns 0 for success.
2913 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2916 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
2917 struct snd_soc_card *card = dapm->card;
2918 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2919 unsigned int *item = ucontrol->value.enumerated.item;
2920 unsigned int val, change;
2922 struct snd_soc_dapm_update update;
2925 if (item[0] >= e->items)
2928 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
2929 mask = e->mask << e->shift_l;
2930 if (e->shift_l != e->shift_r) {
2931 if (item[1] > e->items)
2933 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_l;
2934 mask |= e->mask << e->shift_r;
2937 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2939 if (e->reg != SND_SOC_NOPM)
2940 change = soc_dapm_test_bits(dapm, e->reg, mask, val);
2942 change = dapm_kcontrol_set_value(kcontrol, val);
2945 if (e->reg != SND_SOC_NOPM) {
2946 update.kcontrol = kcontrol;
2947 update.reg = e->reg;
2950 card->update = &update;
2953 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
2955 card->update = NULL;
2958 mutex_unlock(&card->dapm_mutex);
2961 soc_dpcm_runtime_update(card);
2965 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2968 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2970 * @kcontrol: mixer control
2971 * @uinfo: control element information
2973 * Callback to provide information about a pin switch control.
2975 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_info *uinfo)
2978 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2980 uinfo->value.integer.min = 0;
2981 uinfo->value.integer.max = 1;
2985 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
2988 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
2990 * @kcontrol: mixer control
2993 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2996 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
2997 const char *pin = (const char *)kcontrol->private_value;
2999 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3001 ucontrol->value.integer.value[0] =
3002 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3004 mutex_unlock(&card->dapm_mutex);
3008 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3011 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3013 * @kcontrol: mixer control
3016 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3017 struct snd_ctl_elem_value *ucontrol)
3019 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3020 const char *pin = (const char *)kcontrol->private_value;
3022 if (ucontrol->value.integer.value[0])
3023 snd_soc_dapm_enable_pin(&card->dapm, pin);
3025 snd_soc_dapm_disable_pin(&card->dapm, pin);
3027 snd_soc_dapm_sync(&card->dapm);
3030 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3032 static struct snd_soc_dapm_widget *
3033 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3034 const struct snd_soc_dapm_widget *widget)
3036 struct snd_soc_dapm_widget *w;
3040 if ((w = dapm_cnew_widget(widget)) == NULL)
3044 case snd_soc_dapm_regulator_supply:
3045 w->regulator = devm_regulator_get(dapm->dev, w->name);
3046 if (IS_ERR(w->regulator)) {
3047 ret = PTR_ERR(w->regulator);
3048 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3053 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3054 ret = regulator_allow_bypass(w->regulator, true);
3056 dev_warn(w->dapm->dev,
3057 "ASoC: Failed to bypass %s: %d\n",
3061 case snd_soc_dapm_clock_supply:
3062 #ifdef CONFIG_CLKDEV_LOOKUP
3063 w->clk = devm_clk_get(dapm->dev, w->name);
3064 if (IS_ERR(w->clk)) {
3065 ret = PTR_ERR(w->clk);
3066 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3078 prefix = soc_dapm_prefix(dapm);
3080 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3082 w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3084 if (w->name == NULL) {
3090 case snd_soc_dapm_switch:
3091 case snd_soc_dapm_mixer:
3092 case snd_soc_dapm_mixer_named_ctl:
3093 w->power_check = dapm_generic_check_power;
3095 case snd_soc_dapm_mux:
3096 w->power_check = dapm_generic_check_power;
3098 case snd_soc_dapm_dai_out:
3099 w->power_check = dapm_adc_check_power;
3101 case snd_soc_dapm_dai_in:
3102 w->power_check = dapm_dac_check_power;
3104 case snd_soc_dapm_adc:
3105 case snd_soc_dapm_aif_out:
3106 case snd_soc_dapm_dac:
3107 case snd_soc_dapm_aif_in:
3108 case snd_soc_dapm_pga:
3109 case snd_soc_dapm_out_drv:
3110 case snd_soc_dapm_input:
3111 case snd_soc_dapm_output:
3112 case snd_soc_dapm_micbias:
3113 case snd_soc_dapm_spk:
3114 case snd_soc_dapm_hp:
3115 case snd_soc_dapm_mic:
3116 case snd_soc_dapm_line:
3117 case snd_soc_dapm_dai_link:
3118 w->power_check = dapm_generic_check_power;
3120 case snd_soc_dapm_supply:
3121 case snd_soc_dapm_regulator_supply:
3122 case snd_soc_dapm_clock_supply:
3123 case snd_soc_dapm_kcontrol:
3124 w->power_check = dapm_supply_check_power;
3127 w->power_check = dapm_always_on_check_power;
3132 if (dapm->component)
3133 w->codec = dapm->component->codec;
3134 INIT_LIST_HEAD(&w->sources);
3135 INIT_LIST_HEAD(&w->sinks);
3136 INIT_LIST_HEAD(&w->list);
3137 INIT_LIST_HEAD(&w->dirty);
3138 list_add(&w->list, &dapm->card->widgets);
3140 /* machine layer set ups unconnected pins and insertions */
3146 * snd_soc_dapm_new_controls - create new dapm controls
3147 * @dapm: DAPM context
3148 * @widget: widget array
3149 * @num: number of widgets
3151 * Creates new DAPM controls based upon the templates.
3153 * Returns 0 for success else error.
3155 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3156 const struct snd_soc_dapm_widget *widget,
3159 struct snd_soc_dapm_widget *w;
3163 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3164 for (i = 0; i < num; i++) {
3165 w = snd_soc_dapm_new_control(dapm, widget);
3168 "ASoC: Failed to create DAPM control %s\n",
3175 mutex_unlock(&dapm->card->dapm_mutex);
3178 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3180 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3181 struct snd_kcontrol *kcontrol, int event)
3183 struct snd_soc_dapm_path *source_p, *sink_p;
3184 struct snd_soc_dai *source, *sink;
3185 const struct snd_soc_pcm_stream *config = w->params;
3186 struct snd_pcm_substream substream;
3187 struct snd_pcm_hw_params *params = NULL;
3191 if (WARN_ON(!config) ||
3192 WARN_ON(list_empty(&w->sources) || list_empty(&w->sinks)))
3195 /* We only support a single source and sink, pick the first */
3196 source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3198 sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3201 if (WARN_ON(!source_p || !sink_p) ||
3202 WARN_ON(!sink_p->source || !source_p->sink) ||
3203 WARN_ON(!source_p->source || !sink_p->sink))
3206 source = source_p->source->priv;
3207 sink = sink_p->sink->priv;
3209 /* Be a little careful as we don't want to overflow the mask array */
3210 if (config->formats) {
3211 fmt = ffs(config->formats) - 1;
3213 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3218 /* Currently very limited parameter selection */
3219 params = kzalloc(sizeof(*params), GFP_KERNEL);
3224 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3226 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3228 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3231 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3232 = config->channels_min;
3233 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3234 = config->channels_max;
3236 memset(&substream, 0, sizeof(substream));
3239 case SND_SOC_DAPM_PRE_PMU:
3240 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3241 ret = soc_dai_hw_params(&substream, params, source);
3245 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3246 ret = soc_dai_hw_params(&substream, params, sink);
3251 case SND_SOC_DAPM_POST_PMU:
3252 ret = snd_soc_dai_digital_mute(sink, 0,
3253 SNDRV_PCM_STREAM_PLAYBACK);
3254 if (ret != 0 && ret != -ENOTSUPP)
3255 dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3259 case SND_SOC_DAPM_PRE_PMD:
3260 ret = snd_soc_dai_digital_mute(sink, 1,
3261 SNDRV_PCM_STREAM_PLAYBACK);
3262 if (ret != 0 && ret != -ENOTSUPP)
3263 dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3268 WARN(1, "Unknown event %d\n", event);
3277 int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3278 const struct snd_soc_pcm_stream *params,
3279 struct snd_soc_dapm_widget *source,
3280 struct snd_soc_dapm_widget *sink)
3282 struct snd_soc_dapm_widget template;
3283 struct snd_soc_dapm_widget *w;
3288 len = strlen(source->name) + strlen(sink->name) + 2;
3289 link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3292 snprintf(link_name, len, "%s-%s", source->name, sink->name);
3294 memset(&template, 0, sizeof(template));
3295 template.reg = SND_SOC_NOPM;
3296 template.id = snd_soc_dapm_dai_link;
3297 template.name = link_name;
3298 template.event = snd_soc_dai_link_event;
3299 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3300 SND_SOC_DAPM_PRE_PMD;
3302 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3304 w = snd_soc_dapm_new_control(&card->dapm, &template);
3306 dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3313 ret = snd_soc_dapm_add_path(&card->dapm, source, w, NULL, NULL);
3316 return snd_soc_dapm_add_path(&card->dapm, w, sink, NULL, NULL);
3319 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3320 struct snd_soc_dai *dai)
3322 struct snd_soc_dapm_widget template;
3323 struct snd_soc_dapm_widget *w;
3325 WARN_ON(dapm->dev != dai->dev);
3327 memset(&template, 0, sizeof(template));
3328 template.reg = SND_SOC_NOPM;
3330 if (dai->driver->playback.stream_name) {
3331 template.id = snd_soc_dapm_dai_in;
3332 template.name = dai->driver->playback.stream_name;
3333 template.sname = dai->driver->playback.stream_name;
3335 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3338 w = snd_soc_dapm_new_control(dapm, &template);
3340 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3341 dai->driver->playback.stream_name);
3346 dai->playback_widget = w;
3349 if (dai->driver->capture.stream_name) {
3350 template.id = snd_soc_dapm_dai_out;
3351 template.name = dai->driver->capture.stream_name;
3352 template.sname = dai->driver->capture.stream_name;
3354 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3357 w = snd_soc_dapm_new_control(dapm, &template);
3359 dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3360 dai->driver->capture.stream_name);
3365 dai->capture_widget = w;
3371 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3373 struct snd_soc_dapm_widget *dai_w, *w;
3374 struct snd_soc_dapm_widget *src, *sink;
3375 struct snd_soc_dai *dai;
3377 /* For each DAI widget... */
3378 list_for_each_entry(dai_w, &card->widgets, list) {
3379 switch (dai_w->id) {
3380 case snd_soc_dapm_dai_in:
3381 case snd_soc_dapm_dai_out:
3389 /* ...find all widgets with the same stream and link them */
3390 list_for_each_entry(w, &card->widgets, list) {
3391 if (w->dapm != dai_w->dapm)
3395 case snd_soc_dapm_dai_in:
3396 case snd_soc_dapm_dai_out:
3402 if (!w->sname || !strstr(w->sname, dai_w->name))
3405 if (dai_w->id == snd_soc_dapm_dai_in) {
3412 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
3413 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
3420 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
3421 struct snd_soc_pcm_runtime *rtd)
3423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3424 struct snd_soc_dapm_widget *sink, *source;
3427 for (i = 0; i < rtd->num_codecs; i++) {
3428 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
3430 /* there is no point in connecting BE DAI links with dummies */
3431 if (snd_soc_dai_is_dummy(codec_dai) ||
3432 snd_soc_dai_is_dummy(cpu_dai))
3435 /* connect BE DAI playback if widgets are valid */
3436 if (codec_dai->playback_widget && cpu_dai->playback_widget) {
3437 source = cpu_dai->playback_widget;
3438 sink = codec_dai->playback_widget;
3439 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3440 cpu_dai->component->name, source->name,
3441 codec_dai->component->name, sink->name);
3443 snd_soc_dapm_add_path(&card->dapm, source, sink,
3447 /* connect BE DAI capture if widgets are valid */
3448 if (codec_dai->capture_widget && cpu_dai->capture_widget) {
3449 source = codec_dai->capture_widget;
3450 sink = cpu_dai->capture_widget;
3451 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
3452 codec_dai->component->name, source->name,
3453 cpu_dai->component->name, sink->name);
3455 snd_soc_dapm_add_path(&card->dapm, source, sink,
3461 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
3464 struct snd_soc_dapm_widget *w;
3466 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
3467 w = dai->playback_widget;
3469 w = dai->capture_widget;
3472 dapm_mark_dirty(w, "stream event");
3475 case SND_SOC_DAPM_STREAM_START:
3478 case SND_SOC_DAPM_STREAM_STOP:
3481 case SND_SOC_DAPM_STREAM_SUSPEND:
3482 case SND_SOC_DAPM_STREAM_RESUME:
3483 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3484 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3490 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
3492 struct snd_soc_pcm_runtime *rtd = card->rtd;
3495 /* for each BE DAI link... */
3496 for (i = 0; i < card->num_rtd; i++) {
3497 rtd = &card->rtd[i];
3500 * dynamic FE links have no fixed DAI mapping.
3501 * CODEC<->CODEC links have no direct connection.
3503 if (rtd->dai_link->dynamic || rtd->dai_link->params)
3506 dapm_connect_dai_link_widgets(card, rtd);
3510 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3515 soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
3516 for (i = 0; i < rtd->num_codecs; i++)
3517 soc_dapm_dai_stream_event(rtd->codec_dais[i], stream, event);
3519 dapm_power_widgets(rtd->card, event);
3523 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3524 * @rtd: PCM runtime data
3525 * @stream: stream name
3526 * @event: stream event
3528 * Sends a stream event to the dapm core. The core then makes any
3529 * necessary widget power changes.
3531 * Returns 0 for success else error.
3533 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3536 struct snd_soc_card *card = rtd->card;
3538 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3539 soc_dapm_stream_event(rtd, stream, event);
3540 mutex_unlock(&card->dapm_mutex);
3544 * snd_soc_dapm_enable_pin_unlocked - enable pin.
3545 * @dapm: DAPM context
3548 * Enables input/output pin and its parents or children widgets iff there is
3549 * a valid audio route and active audio stream.
3551 * Requires external locking.
3553 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3554 * do any widget power switching.
3556 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3559 return snd_soc_dapm_set_pin(dapm, pin, 1);
3561 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
3564 * snd_soc_dapm_enable_pin - enable pin.
3565 * @dapm: DAPM context
3568 * Enables input/output pin and its parents or children widgets iff there is
3569 * a valid audio route and active audio stream.
3571 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3572 * do any widget power switching.
3574 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3578 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3580 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
3582 mutex_unlock(&dapm->card->dapm_mutex);
3586 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3589 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
3590 * @dapm: DAPM context
3593 * Enables input/output pin regardless of any other state. This is
3594 * intended for use with microphone bias supplies used in microphone
3597 * Requires external locking.
3599 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3600 * do any widget power switching.
3602 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3605 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3608 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3612 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3615 dapm_mark_dirty(w, "force enable");
3619 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
3622 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3623 * @dapm: DAPM context
3626 * Enables input/output pin regardless of any other state. This is
3627 * intended for use with microphone bias supplies used in microphone
3630 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3631 * do any widget power switching.
3633 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3638 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3640 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
3642 mutex_unlock(&dapm->card->dapm_mutex);
3646 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3649 * snd_soc_dapm_disable_pin_unlocked - disable pin.
3650 * @dapm: DAPM context
3653 * Disables input/output pin and its parents or children widgets.
3655 * Requires external locking.
3657 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3658 * do any widget power switching.
3660 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
3663 return snd_soc_dapm_set_pin(dapm, pin, 0);
3665 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
3668 * snd_soc_dapm_disable_pin - disable pin.
3669 * @dapm: DAPM context
3672 * Disables input/output pin and its parents or children widgets.
3674 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3675 * do any widget power switching.
3677 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3682 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3684 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3686 mutex_unlock(&dapm->card->dapm_mutex);
3690 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3693 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
3694 * @dapm: DAPM context
3697 * Marks the specified pin as being not connected, disabling it along
3698 * any parent or child widgets. At present this is identical to
3699 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3700 * additional things such as disabling controls which only affect
3701 * paths through the pin.
3703 * Requires external locking.
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_unlocked(struct snd_soc_dapm_context *dapm,
3711 return snd_soc_dapm_set_pin(dapm, pin, 0);
3713 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
3716 * snd_soc_dapm_nc_pin - permanently disable pin.
3717 * @dapm: DAPM context
3720 * Marks the specified pin as being not connected, disabling it along
3721 * any parent or child widgets. At present this is identical to
3722 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3723 * additional things such as disabling controls which only affect
3724 * paths through the pin.
3726 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3727 * do any widget power switching.
3729 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3733 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3735 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
3737 mutex_unlock(&dapm->card->dapm_mutex);
3741 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3744 * snd_soc_dapm_get_pin_status - get audio pin status
3745 * @dapm: DAPM context
3746 * @pin: audio signal pin endpoint (or start point)
3748 * Get audio pin status - connected or disconnected.
3750 * Returns 1 for connected otherwise 0.
3752 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3755 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3758 return w->connected;
3762 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3765 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3766 * @dapm: DAPM context
3767 * @pin: audio signal pin endpoint (or start point)
3769 * Mark the given endpoint or pin as ignoring suspend. When the
3770 * system is disabled a path between two endpoints flagged as ignoring
3771 * suspend will not be disabled. The path must already be enabled via
3772 * normal means at suspend time, it will not be turned on if it was not
3775 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3778 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3781 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3785 w->ignore_suspend = 1;
3789 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3791 static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3792 struct snd_soc_dapm_widget *w)
3794 struct snd_soc_dapm_path *p;
3796 list_for_each_entry(p, &card->paths, list) {
3797 if ((p->source == w) || (p->sink == w)) {
3799 "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3800 p->source->name, p->source->id, p->source->dapm,
3801 p->sink->name, p->sink->id, p->sink->dapm);
3803 /* Connected to something other than the codec */
3804 if (p->source->dapm != p->sink->dapm)
3807 * Loopback connection from codec external pin to
3808 * codec external pin
3810 if (p->sink->id == snd_soc_dapm_input) {
3811 switch (p->source->id) {
3812 case snd_soc_dapm_output:
3813 case snd_soc_dapm_micbias:
3826 * snd_soc_dapm_auto_nc_pins - call snd_soc_dapm_nc_pin for unused pins
3827 * @card: The card whose pins should be processed
3829 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the card
3830 * which are unused. Pins are used if they are connected externally to a
3831 * component, whether that be to some other device, or a loop-back connection to
3832 * the component itself.
3834 void snd_soc_dapm_auto_nc_pins(struct snd_soc_card *card)
3836 struct snd_soc_dapm_widget *w;
3838 dev_dbg(card->dev, "ASoC: Auto NC: DAPMs: card:%p\n", &card->dapm);
3840 list_for_each_entry(w, &card->widgets, list) {
3842 case snd_soc_dapm_input:
3843 case snd_soc_dapm_output:
3844 case snd_soc_dapm_micbias:
3845 dev_dbg(card->dev, "ASoC: Auto NC: Checking widget %s\n",
3847 if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3849 "... Not in map; disabling\n");
3850 snd_soc_dapm_nc_pin(w->dapm, w->name);
3860 * snd_soc_dapm_free - free dapm resources
3861 * @dapm: DAPM context
3863 * Free all dapm widgets and resources.
3865 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3867 snd_soc_dapm_sys_remove(dapm->dev);
3868 dapm_debugfs_cleanup(dapm);
3869 dapm_free_widgets(dapm);
3870 list_del(&dapm->list);
3872 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3874 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
3876 struct snd_soc_card *card = dapm->card;
3877 struct snd_soc_dapm_widget *w;
3878 LIST_HEAD(down_list);
3881 mutex_lock(&card->dapm_mutex);
3883 list_for_each_entry(w, &dapm->card->widgets, list) {
3884 if (w->dapm != dapm)
3887 dapm_seq_insert(w, &down_list, false);
3893 /* If there were no widgets to power down we're already in
3897 if (dapm->bias_level == SND_SOC_BIAS_ON)
3898 snd_soc_dapm_set_bias_level(dapm,
3899 SND_SOC_BIAS_PREPARE);
3900 dapm_seq_run(card, &down_list, 0, false);
3901 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3902 snd_soc_dapm_set_bias_level(dapm,
3903 SND_SOC_BIAS_STANDBY);
3906 mutex_unlock(&card->dapm_mutex);
3910 * snd_soc_dapm_shutdown - callback for system shutdown
3912 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3914 struct snd_soc_dapm_context *dapm;
3916 list_for_each_entry(dapm, &card->dapm_list, list) {
3917 if (dapm != &card->dapm) {
3918 soc_dapm_shutdown_dapm(dapm);
3919 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
3920 snd_soc_dapm_set_bias_level(dapm,
3925 soc_dapm_shutdown_dapm(&card->dapm);
3926 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3927 snd_soc_dapm_set_bias_level(&card->dapm,
3931 /* Module information */
3932 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3933 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3934 MODULE_LICENSE("GPL");