1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 for_each_card_widgets(card, w) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
343 struct dapm_kcontrol_data {
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 INIT_LIST_HEAD(&data->paths);
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
391 template.off_val = mc->max;
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
398 data->value = template.on_val;
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
433 data->value = template.on_val;
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
451 kcontrol->private_data = data;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
485 n = data->wlist->num_widgets + 1;
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
550 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
551 case snd_soc_dapm_switch:
552 case snd_soc_dapm_mixer:
553 case snd_soc_dapm_mixer_named_ctl:
554 data->widget->on_val = value & data->widget->mask;
556 case snd_soc_dapm_demux:
557 case snd_soc_dapm_mux:
558 data->widget->on_val = value >> data->widget->shift;
561 data->widget->on_val = value;
572 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
574 * @kcontrol: The kcontrol
576 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
577 struct snd_kcontrol *kcontrol)
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
581 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
584 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
586 * @kcontrol: The kcontrol
588 * Note: This function must only be used on kcontrols that are known to have
589 * been registered for a CODEC. Otherwise the behaviour is undefined.
591 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
592 struct snd_kcontrol *kcontrol)
594 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
596 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
598 static void dapm_reset(struct snd_soc_card *card)
600 struct snd_soc_dapm_widget *w;
602 lockdep_assert_held(&card->dapm_mutex);
604 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
606 for_each_card_widgets(card, w) {
607 w->new_power = w->power;
608 w->power_checked = false;
612 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
614 if (!dapm->component)
616 return dapm->component->name_prefix;
619 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
622 if (!dapm->component)
624 return snd_soc_component_read(dapm->component, reg, value);
627 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
628 int reg, unsigned int mask, unsigned int value)
630 if (!dapm->component)
632 return snd_soc_component_update_bits(dapm->component, reg,
636 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
637 int reg, unsigned int mask, unsigned int value)
639 if (!dapm->component)
641 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
644 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
647 snd_soc_component_async_complete(dapm->component);
650 static struct snd_soc_dapm_widget *
651 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
653 struct snd_soc_dapm_widget *w = wcache->widget;
654 struct list_head *wlist;
659 wlist = &w->dapm->card->widgets;
661 list_for_each_entry_from(w, wlist, list) {
662 if (!strcmp(name, w->name))
673 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
674 struct snd_soc_dapm_widget *w)
680 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
681 * @dapm: The DAPM context for which to set the level
682 * @level: The level to set
684 * Forces the DAPM bias level to a specific state. It will call the bias level
685 * callback of DAPM context with the specified level. This will even happen if
686 * the context is already at the same level. Furthermore it will not go through
687 * the normal bias level sequencing, meaning any intermediate states between the
688 * current and the target state will not be entered.
690 * Note that the change in bias level is only temporary and the next time
691 * snd_soc_dapm_sync() is called the state will be set to the level as
692 * determined by the DAPM core. The function is mainly intended to be used to
693 * used during probe or resume from suspend to power up the device so
694 * initialization can be done, before the DAPM core takes over.
696 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
697 enum snd_soc_bias_level level)
702 ret = snd_soc_component_set_bias_level(dapm->component, level);
705 dapm->bias_level = level;
709 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
712 * snd_soc_dapm_set_bias_level - set the bias level for the system
713 * @dapm: DAPM context
714 * @level: level to configure
716 * Configure the bias (power) levels for the SoC audio device.
718 * Returns 0 for success else error.
720 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
721 enum snd_soc_bias_level level)
723 struct snd_soc_card *card = dapm->card;
726 trace_snd_soc_bias_level_start(card, level);
728 if (card && card->set_bias_level)
729 ret = card->set_bias_level(card, dapm, level);
733 if (!card || dapm != &card->dapm)
734 ret = snd_soc_dapm_force_bias_level(dapm, level);
739 if (card && card->set_bias_level_post)
740 ret = card->set_bias_level_post(card, dapm, level);
742 trace_snd_soc_bias_level_done(card, level);
747 /* connect mux widget to its interconnecting audio paths */
748 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
749 struct snd_soc_dapm_path *path, const char *control_name,
750 struct snd_soc_dapm_widget *w)
752 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
753 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
754 unsigned int val, item;
757 if (e->reg != SND_SOC_NOPM) {
758 soc_dapm_read(dapm, e->reg, &val);
759 val = (val >> e->shift_l) & e->mask;
760 item = snd_soc_enum_val_to_item(e, val);
762 /* since a virtual mux has no backing registers to
763 * decide which path to connect, it will try to match
764 * with the first enumeration. This is to ensure
765 * that the default mux choice (the first) will be
766 * correctly powered up during initialization.
771 i = match_string(e->texts, e->items, control_name);
775 path->name = e->texts[i];
776 path->connect = (i == item);
781 /* set up initial codec paths */
782 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
785 struct soc_mixer_control *mc = (struct soc_mixer_control *)
786 p->sink->kcontrol_news[i].private_value;
787 unsigned int reg = mc->reg;
788 unsigned int shift = mc->shift;
789 unsigned int max = mc->max;
790 unsigned int mask = (1 << fls(max)) - 1;
791 unsigned int invert = mc->invert;
794 if (reg != SND_SOC_NOPM) {
795 soc_dapm_read(p->sink->dapm, reg, &val);
797 * The nth_path argument allows this function to know
798 * which path of a kcontrol it is setting the initial
799 * status for. Ideally this would support any number
800 * of paths and channels. But since kcontrols only come
801 * in mono and stereo variants, we are limited to 2
804 * The following code assumes for stereo controls the
805 * first path is the left channel, and all remaining
806 * paths are the right channel.
808 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
810 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
811 val = (val >> mc->rshift) & mask;
813 val = (val >> shift) & mask;
819 /* since a virtual mixer has no backing registers to
820 * decide which path to connect, it will try to match
821 * with initial state. This is to ensure
822 * that the default mixer choice will be
823 * correctly powered up during initialization.
829 /* connect mixer widget to its interconnecting audio paths */
830 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
831 struct snd_soc_dapm_path *path, const char *control_name)
835 /* search for mixer kcontrol */
836 for (i = 0; i < path->sink->num_kcontrols; i++) {
837 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
838 path->name = path->sink->kcontrol_news[i].name;
839 dapm_set_mixer_path_status(path, i, nth_path++);
846 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
847 struct snd_soc_dapm_widget *kcontrolw,
848 const struct snd_kcontrol_new *kcontrol_new,
849 struct snd_kcontrol **kcontrol)
851 struct snd_soc_dapm_widget *w;
856 for_each_card_widgets(dapm->card, w) {
857 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
859 for (i = 0; i < w->num_kcontrols; i++) {
860 if (&w->kcontrol_news[i] == kcontrol_new) {
862 *kcontrol = w->kcontrols[i];
872 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
873 * create it. Either way, add the widget into the control's widget list
875 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
878 struct snd_soc_dapm_context *dapm = w->dapm;
879 struct snd_card *card = dapm->card->snd_card;
883 struct snd_kcontrol *kcontrol;
884 bool wname_in_long_name, kcname_in_long_name;
885 char *long_name = NULL;
889 prefix = soc_dapm_prefix(dapm);
891 prefix_len = strlen(prefix) + 1;
895 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
900 wname_in_long_name = false;
901 kcname_in_long_name = true;
904 case snd_soc_dapm_switch:
905 case snd_soc_dapm_mixer:
906 case snd_soc_dapm_pga:
907 case snd_soc_dapm_effect:
908 case snd_soc_dapm_out_drv:
909 wname_in_long_name = true;
910 kcname_in_long_name = true;
912 case snd_soc_dapm_mixer_named_ctl:
913 wname_in_long_name = false;
914 kcname_in_long_name = true;
916 case snd_soc_dapm_demux:
917 case snd_soc_dapm_mux:
918 wname_in_long_name = true;
919 kcname_in_long_name = false;
926 if (wname_in_long_name && kcname_in_long_name) {
928 * The control will get a prefix from the control
929 * creation process but we're also using the same
930 * prefix for widgets so cut the prefix off the
931 * front of the widget name.
933 long_name = kasprintf(GFP_KERNEL, "%s %s",
934 w->name + prefix_len,
935 w->kcontrol_news[kci].name);
936 if (long_name == NULL)
940 } else if (wname_in_long_name) {
942 name = w->name + prefix_len;
945 name = w->kcontrol_news[kci].name;
948 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
955 kcontrol->private_free = dapm_kcontrol_free;
957 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
959 snd_ctl_free_one(kcontrol);
963 ret = snd_ctl_add(card, kcontrol);
966 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
972 ret = dapm_kcontrol_add_widget(kcontrol, w);
974 w->kcontrols[kci] = kcontrol;
982 /* create new dapm mixer control */
983 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
986 struct snd_soc_dapm_path *path;
987 struct dapm_kcontrol_data *data;
990 for (i = 0; i < w->num_kcontrols; i++) {
992 snd_soc_dapm_widget_for_each_source_path(w, path) {
993 /* mixer/mux paths name must match control name */
994 if (path->name != (char *)w->kcontrol_news[i].name)
997 if (!w->kcontrols[i]) {
998 ret = dapm_create_or_share_kcontrol(w, i);
1003 dapm_kcontrol_add_path(w->kcontrols[i], path);
1005 data = snd_kcontrol_chip(w->kcontrols[i]);
1007 snd_soc_dapm_add_path(data->widget->dapm,
1017 /* create new dapm mux control */
1018 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1020 struct snd_soc_dapm_context *dapm = w->dapm;
1021 enum snd_soc_dapm_direction dir;
1022 struct snd_soc_dapm_path *path;
1027 case snd_soc_dapm_mux:
1028 dir = SND_SOC_DAPM_DIR_OUT;
1031 case snd_soc_dapm_demux:
1032 dir = SND_SOC_DAPM_DIR_IN;
1039 if (w->num_kcontrols != 1) {
1041 "ASoC: %s %s has incorrect number of controls\n", type,
1046 if (list_empty(&w->edges[dir])) {
1047 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1051 ret = dapm_create_or_share_kcontrol(w, 0);
1055 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1057 dapm_kcontrol_add_path(w->kcontrols[0], path);
1063 /* create new dapm volume control */
1064 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1068 for (i = 0; i < w->num_kcontrols; i++) {
1069 ret = dapm_create_or_share_kcontrol(w, i);
1077 /* create new dapm dai link control */
1078 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1081 struct snd_kcontrol *kcontrol;
1082 struct snd_soc_dapm_context *dapm = w->dapm;
1083 struct snd_card *card = dapm->card->snd_card;
1084 struct snd_soc_pcm_runtime *rtd = w->priv;
1086 /* create control for links with > 1 config */
1087 if (rtd->dai_link->num_params <= 1)
1091 for (i = 0; i < w->num_kcontrols; i++) {
1092 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1094 ret = snd_ctl_add(card, kcontrol);
1097 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1098 w->name, w->kcontrol_news[i].name, ret);
1101 kcontrol->private_data = w;
1102 w->kcontrols[i] = kcontrol;
1108 /* We implement power down on suspend by checking the power state of
1109 * the ALSA card - when we are suspending the ALSA state for the card
1112 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1114 int level = snd_power_get_state(widget->dapm->card->snd_card);
1117 case SNDRV_CTL_POWER_D3hot:
1118 case SNDRV_CTL_POWER_D3cold:
1119 if (widget->ignore_suspend)
1120 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1122 return widget->ignore_suspend;
1128 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1133 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1134 struct list_head *widgets)
1136 struct snd_soc_dapm_widget *w;
1137 struct list_head *it;
1138 unsigned int size = 0;
1141 list_for_each(it, widgets)
1144 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1148 list_for_each_entry(w, widgets, work_list)
1149 (*list)->widgets[i++] = w;
1151 (*list)->num_widgets = i;
1157 * Recursively reset the cached number of inputs or outputs for the specified
1158 * widget and all widgets that can be reached via incoming or outcoming paths
1161 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1162 enum snd_soc_dapm_direction dir)
1164 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1165 struct snd_soc_dapm_path *path;
1167 widget->endpoints[dir] = -1;
1169 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1170 if (path->weak || path->is_supply)
1176 if (path->connect) {
1178 invalidate_paths_ep(path->node[dir], dir);
1185 * Common implementation for is_connected_output_ep() and
1186 * is_connected_input_ep(). The function is inlined since the combined size of
1187 * the two specialized functions is only marginally larger then the size of the
1188 * generic function and at the same time the fast path of the specialized
1189 * functions is significantly smaller than the generic function.
1191 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1192 struct list_head *list, enum snd_soc_dapm_direction dir,
1193 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1194 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1195 enum snd_soc_dapm_direction)),
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction))
1199 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1200 struct snd_soc_dapm_path *path;
1203 if (widget->endpoints[dir] >= 0)
1204 return widget->endpoints[dir];
1206 DAPM_UPDATE_STAT(widget, path_checks);
1208 /* do we need to add this widget to the list ? */
1210 list_add_tail(&widget->work_list, list);
1212 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1214 custom_stop_condition = NULL;
1217 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1218 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1219 return widget->endpoints[dir];
1222 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1223 DAPM_UPDATE_STAT(widget, neighbour_checks);
1225 if (path->weak || path->is_supply)
1231 trace_snd_soc_dapm_path(widget, dir, path);
1233 if (path->connect) {
1235 con += fn(path->node[dir], list, custom_stop_condition);
1240 widget->endpoints[dir] = con;
1246 * Recursively check for a completed path to an active or physically connected
1247 * output widget. Returns number of complete paths.
1249 * Optionally, can be supplied with a function acting as a stopping condition.
1250 * This function takes the dapm widget currently being examined and the walk
1251 * direction as an arguments, it should return true if widgets from that point
1252 * in the graph onwards should not be added to the widget list.
1254 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1255 struct list_head *list,
1256 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1257 enum snd_soc_dapm_direction))
1259 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1260 is_connected_output_ep, custom_stop_condition);
1264 * Recursively check for a completed path to an active or physically connected
1265 * input widget. Returns number of complete paths.
1267 * Optionally, can be supplied with a function acting as a stopping condition.
1268 * This function takes the dapm widget currently being examined and the walk
1269 * direction as an arguments, it should return true if the walk should be
1270 * stopped and false otherwise.
1272 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1273 struct list_head *list,
1274 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1275 enum snd_soc_dapm_direction))
1277 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1278 is_connected_input_ep, custom_stop_condition);
1282 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1283 * @dai: the soc DAI.
1284 * @stream: stream direction.
1285 * @list: list of active widgets for this stream.
1286 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1287 * walk based on custom logic.
1289 * Queries DAPM graph as to whether a valid audio stream path exists for
1290 * the initial stream specified by name. This takes into account
1291 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1293 * Optionally, can be supplied with a function acting as a stopping condition.
1294 * This function takes the dapm widget currently being examined and the walk
1295 * direction as an arguments, it should return true if the walk should be
1296 * stopped and false otherwise.
1298 * Returns the number of valid paths or negative error.
1300 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1301 struct snd_soc_dapm_widget_list **list,
1302 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1303 enum snd_soc_dapm_direction))
1305 struct snd_soc_card *card = dai->component->card;
1306 struct snd_soc_dapm_widget *w;
1311 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1313 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1314 w = dai->playback_widget;
1315 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1316 paths = is_connected_output_ep(w, &widgets,
1317 custom_stop_condition);
1319 w = dai->capture_widget;
1320 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1321 paths = is_connected_input_ep(w, &widgets,
1322 custom_stop_condition);
1325 /* Drop starting point */
1326 list_del(widgets.next);
1328 ret = dapm_widget_list_create(list, &widgets);
1332 trace_snd_soc_dapm_connected(paths, stream);
1333 mutex_unlock(&card->dapm_mutex);
1338 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1340 dapm_widget_list_free(list);
1344 * Handler for regulator supply widget.
1346 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1347 struct snd_kcontrol *kcontrol, int event)
1351 soc_dapm_async_complete(w->dapm);
1353 if (SND_SOC_DAPM_EVENT_ON(event)) {
1354 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1355 ret = regulator_allow_bypass(w->regulator, false);
1357 dev_warn(w->dapm->dev,
1358 "ASoC: Failed to unbypass %s: %d\n",
1362 return regulator_enable(w->regulator);
1364 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1365 ret = regulator_allow_bypass(w->regulator, true);
1367 dev_warn(w->dapm->dev,
1368 "ASoC: Failed to bypass %s: %d\n",
1372 return regulator_disable_deferred(w->regulator, w->shift);
1375 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1378 * Handler for pinctrl widget.
1380 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1381 struct snd_kcontrol *kcontrol, int event)
1383 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1384 struct pinctrl *p = w->pinctrl;
1385 struct pinctrl_state *s;
1390 if (SND_SOC_DAPM_EVENT_ON(event))
1391 s = pinctrl_lookup_state(p, priv->active_state);
1393 s = pinctrl_lookup_state(p, priv->sleep_state);
1398 return pinctrl_select_state(p, s);
1400 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1403 * Handler for clock supply widget.
1405 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1406 struct snd_kcontrol *kcontrol, int event)
1411 soc_dapm_async_complete(w->dapm);
1413 if (SND_SOC_DAPM_EVENT_ON(event)) {
1414 return clk_prepare_enable(w->clk);
1416 clk_disable_unprepare(w->clk);
1422 EXPORT_SYMBOL_GPL(dapm_clock_event);
1424 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1426 if (w->power_checked)
1427 return w->new_power;
1432 w->new_power = w->power_check(w);
1434 w->power_checked = true;
1436 return w->new_power;
1439 /* Generic check to see if a widget should be powered. */
1440 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1444 DAPM_UPDATE_STAT(w, power_checks);
1446 in = is_connected_input_ep(w, NULL, NULL);
1447 out = is_connected_output_ep(w, NULL, NULL);
1448 return out != 0 && in != 0;
1451 /* Check to see if a power supply is needed */
1452 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1454 struct snd_soc_dapm_path *path;
1456 DAPM_UPDATE_STAT(w, power_checks);
1458 /* Check if one of our outputs is connected */
1459 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1460 DAPM_UPDATE_STAT(w, neighbour_checks);
1465 if (path->connected &&
1466 !path->connected(path->source, path->sink))
1469 if (dapm_widget_power_check(path->sink))
1476 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1478 return w->connected;
1481 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1482 struct snd_soc_dapm_widget *b,
1487 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1488 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1493 sort = dapm_down_seq;
1495 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1496 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1498 if (sort[a->id] != sort[b->id])
1499 return sort[a->id] - sort[b->id];
1500 if (a->subseq != b->subseq) {
1502 return a->subseq - b->subseq;
1504 return b->subseq - a->subseq;
1506 if (a->reg != b->reg)
1507 return a->reg - b->reg;
1508 if (a->dapm != b->dapm)
1509 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1514 /* Insert a widget in order into a DAPM power sequence. */
1515 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1516 struct list_head *list,
1519 struct snd_soc_dapm_widget *w;
1521 list_for_each_entry(w, list, power_list)
1522 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1523 list_add_tail(&new_widget->power_list, &w->power_list);
1527 list_add_tail(&new_widget->power_list, list);
1530 static void dapm_seq_check_event(struct snd_soc_card *card,
1531 struct snd_soc_dapm_widget *w, int event)
1533 const char *ev_name;
1537 case SND_SOC_DAPM_PRE_PMU:
1538 ev_name = "PRE_PMU";
1541 case SND_SOC_DAPM_POST_PMU:
1542 ev_name = "POST_PMU";
1545 case SND_SOC_DAPM_PRE_PMD:
1546 ev_name = "PRE_PMD";
1549 case SND_SOC_DAPM_POST_PMD:
1550 ev_name = "POST_PMD";
1553 case SND_SOC_DAPM_WILL_PMU:
1554 ev_name = "WILL_PMU";
1557 case SND_SOC_DAPM_WILL_PMD:
1558 ev_name = "WILL_PMD";
1562 WARN(1, "Unknown event %d\n", event);
1566 if (w->new_power != power)
1569 if (w->event && (w->event_flags & event)) {
1570 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1572 soc_dapm_async_complete(w->dapm);
1573 trace_snd_soc_dapm_widget_event_start(w, event);
1574 ret = w->event(w, NULL, event);
1575 trace_snd_soc_dapm_widget_event_done(w, event);
1577 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1578 ev_name, w->name, ret);
1582 /* Apply the coalesced changes from a DAPM sequence */
1583 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1584 struct list_head *pending)
1586 struct snd_soc_dapm_context *dapm;
1587 struct snd_soc_dapm_widget *w;
1589 unsigned int value = 0;
1590 unsigned int mask = 0;
1592 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1596 list_for_each_entry(w, pending, power_list) {
1597 WARN_ON(reg != w->reg || dapm != w->dapm);
1598 w->power = w->new_power;
1600 mask |= w->mask << w->shift;
1602 value |= w->on_val << w->shift;
1604 value |= w->off_val << w->shift;
1606 pop_dbg(dapm->dev, card->pop_time,
1607 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1608 w->name, reg, value, mask);
1610 /* Check for events */
1611 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1612 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1616 /* Any widget will do, they should all be updating the
1620 pop_dbg(dapm->dev, card->pop_time,
1621 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1622 value, mask, reg, card->pop_time);
1623 pop_wait(card->pop_time);
1624 soc_dapm_update_bits(dapm, reg, mask, value);
1627 list_for_each_entry(w, pending, power_list) {
1628 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1629 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1633 /* Apply a DAPM power sequence.
1635 * We walk over a pre-sorted list of widgets to apply power to. In
1636 * order to minimise the number of writes to the device required
1637 * multiple widgets will be updated in a single write where possible.
1638 * Currently anything that requires more than a single write is not
1641 static void dapm_seq_run(struct snd_soc_card *card,
1642 struct list_head *list, int event, bool power_up)
1644 struct snd_soc_dapm_widget *w, *n;
1645 struct snd_soc_dapm_context *d;
1648 int cur_subseq = -1;
1649 int cur_reg = SND_SOC_NOPM;
1650 struct snd_soc_dapm_context *cur_dapm = NULL;
1657 sort = dapm_down_seq;
1659 list_for_each_entry_safe(w, n, list, power_list) {
1662 /* Do we need to apply any queued changes? */
1663 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1664 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1665 if (!list_empty(&pending))
1666 dapm_seq_run_coalesced(card, &pending);
1668 if (cur_dapm && cur_dapm->component) {
1669 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1670 if (sort[i] == cur_sort)
1671 snd_soc_component_seq_notifier(
1672 cur_dapm->component,
1676 if (cur_dapm && w->dapm != cur_dapm)
1677 soc_dapm_async_complete(cur_dapm);
1679 INIT_LIST_HEAD(&pending);
1681 cur_subseq = INT_MIN;
1682 cur_reg = SND_SOC_NOPM;
1687 case snd_soc_dapm_pre:
1689 list_for_each_entry_safe_continue(w, n, list,
1692 if (event == SND_SOC_DAPM_STREAM_START)
1694 NULL, SND_SOC_DAPM_PRE_PMU);
1695 else if (event == SND_SOC_DAPM_STREAM_STOP)
1697 NULL, SND_SOC_DAPM_PRE_PMD);
1700 case snd_soc_dapm_post:
1702 list_for_each_entry_safe_continue(w, n, list,
1705 if (event == SND_SOC_DAPM_STREAM_START)
1707 NULL, SND_SOC_DAPM_POST_PMU);
1708 else if (event == SND_SOC_DAPM_STREAM_STOP)
1710 NULL, SND_SOC_DAPM_POST_PMD);
1714 /* Queue it up for application */
1715 cur_sort = sort[w->id];
1716 cur_subseq = w->subseq;
1719 list_move(&w->power_list, &pending);
1724 dev_err(w->dapm->dev,
1725 "ASoC: Failed to apply widget power: %d\n", ret);
1728 if (!list_empty(&pending))
1729 dapm_seq_run_coalesced(card, &pending);
1731 if (cur_dapm && cur_dapm->component) {
1732 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1733 if (sort[i] == cur_sort)
1734 snd_soc_component_seq_notifier(
1735 cur_dapm->component,
1739 for_each_card_dapms(card, d)
1740 soc_dapm_async_complete(d);
1743 static void dapm_widget_update(struct snd_soc_card *card)
1745 struct snd_soc_dapm_update *update = card->update;
1746 struct snd_soc_dapm_widget_list *wlist;
1747 struct snd_soc_dapm_widget *w = NULL;
1751 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1754 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1756 for_each_dapm_widgets(wlist, wi, w) {
1757 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1758 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1760 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1768 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1771 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1774 if (update->has_second_set) {
1775 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1776 update->mask2, update->val2);
1778 dev_err(w->dapm->dev,
1779 "ASoC: %s DAPM update failed: %d\n",
1783 for_each_dapm_widgets(wlist, wi, w) {
1784 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1785 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1787 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1793 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1794 * they're changing state.
1796 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1798 struct snd_soc_dapm_context *d = data;
1801 /* If we're off and we're not supposed to go into STANDBY */
1802 if (d->bias_level == SND_SOC_BIAS_OFF &&
1803 d->target_bias_level != SND_SOC_BIAS_OFF) {
1805 pm_runtime_get_sync(d->dev);
1807 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1810 "ASoC: Failed to turn on bias: %d\n", ret);
1813 /* Prepare for a transition to ON or away from ON */
1814 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1815 d->bias_level != SND_SOC_BIAS_ON) ||
1816 (d->target_bias_level != SND_SOC_BIAS_ON &&
1817 d->bias_level == SND_SOC_BIAS_ON)) {
1818 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1821 "ASoC: Failed to prepare bias: %d\n", ret);
1825 /* Async callback run prior to DAPM sequences - brings to their final
1828 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1830 struct snd_soc_dapm_context *d = data;
1833 /* If we just powered the last thing off drop to standby bias */
1834 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1835 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1836 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1837 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1839 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1843 /* If we're in standby and can support bias off then do that */
1844 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1845 d->target_bias_level == SND_SOC_BIAS_OFF) {
1846 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1848 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1852 pm_runtime_put(d->dev);
1855 /* If we just powered up then move to active bias */
1856 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1857 d->target_bias_level == SND_SOC_BIAS_ON) {
1858 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1860 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1865 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1866 bool power, bool connect)
1868 /* If a connection is being made or broken then that update
1869 * will have marked the peer dirty, otherwise the widgets are
1870 * not connected and this update has no impact. */
1874 /* If the peer is already in the state we're moving to then we
1875 * won't have an impact on it. */
1876 if (power != peer->power)
1877 dapm_mark_dirty(peer, "peer state change");
1880 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1881 struct list_head *up_list,
1882 struct list_head *down_list)
1884 struct snd_soc_dapm_path *path;
1886 if (w->power == power)
1889 trace_snd_soc_dapm_widget_power(w, power);
1891 /* If we changed our power state perhaps our neigbours changed
1894 snd_soc_dapm_widget_for_each_source_path(w, path)
1895 dapm_widget_set_peer_power(path->source, power, path->connect);
1897 /* Supplies can't affect their outputs, only their inputs */
1898 if (!w->is_supply) {
1899 snd_soc_dapm_widget_for_each_sink_path(w, path)
1900 dapm_widget_set_peer_power(path->sink, power,
1905 dapm_seq_insert(w, up_list, true);
1907 dapm_seq_insert(w, down_list, false);
1910 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1911 struct list_head *up_list,
1912 struct list_head *down_list)
1917 case snd_soc_dapm_pre:
1918 dapm_seq_insert(w, down_list, false);
1920 case snd_soc_dapm_post:
1921 dapm_seq_insert(w, up_list, true);
1925 power = dapm_widget_power_check(w);
1927 dapm_widget_set_power(w, power, up_list, down_list);
1932 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1934 if (dapm->idle_bias_off)
1937 switch (snd_power_get_state(dapm->card->snd_card)) {
1938 case SNDRV_CTL_POWER_D3hot:
1939 case SNDRV_CTL_POWER_D3cold:
1940 return dapm->suspend_bias_off;
1949 * Scan each dapm widget for complete audio path.
1950 * A complete path is a route that has valid endpoints i.e.:-
1952 * o DAC to output pin.
1953 * o Input pin to ADC.
1954 * o Input pin to Output pin (bypass, sidetone)
1955 * o DAC to ADC (loopback).
1957 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1959 struct snd_soc_dapm_widget *w;
1960 struct snd_soc_dapm_context *d;
1962 LIST_HEAD(down_list);
1963 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1964 enum snd_soc_bias_level bias;
1967 lockdep_assert_held(&card->dapm_mutex);
1969 trace_snd_soc_dapm_start(card);
1971 for_each_card_dapms(card, d) {
1972 if (dapm_idle_bias_off(d))
1973 d->target_bias_level = SND_SOC_BIAS_OFF;
1975 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1980 /* Check which widgets we need to power and store them in
1981 * lists indicating if they should be powered up or down. We
1982 * only check widgets that have been flagged as dirty but note
1983 * that new widgets may be added to the dirty list while we
1986 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1987 dapm_power_one_widget(w, &up_list, &down_list);
1990 for_each_card_widgets(card, w) {
1992 case snd_soc_dapm_pre:
1993 case snd_soc_dapm_post:
1994 /* These widgets always need to be powered */
1997 list_del_init(&w->dirty);
2004 /* Supplies and micbiases only bring the
2005 * context up to STANDBY as unless something
2006 * else is active and passing audio they
2007 * generally don't require full power. Signal
2008 * generators are virtual pins and have no
2009 * power impact themselves.
2012 case snd_soc_dapm_siggen:
2013 case snd_soc_dapm_vmid:
2015 case snd_soc_dapm_supply:
2016 case snd_soc_dapm_regulator_supply:
2017 case snd_soc_dapm_pinctrl:
2018 case snd_soc_dapm_clock_supply:
2019 case snd_soc_dapm_micbias:
2020 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2021 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2024 d->target_bias_level = SND_SOC_BIAS_ON;
2031 /* Force all contexts in the card to the same bias state if
2032 * they're not ground referenced.
2034 bias = SND_SOC_BIAS_OFF;
2035 for_each_card_dapms(card, d)
2036 if (d->target_bias_level > bias)
2037 bias = d->target_bias_level;
2038 for_each_card_dapms(card, d)
2039 if (!dapm_idle_bias_off(d))
2040 d->target_bias_level = bias;
2042 trace_snd_soc_dapm_walk_done(card);
2044 /* Run card bias changes at first */
2045 dapm_pre_sequence_async(&card->dapm, 0);
2046 /* Run other bias changes in parallel */
2047 for_each_card_dapms(card, d) {
2048 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2049 async_schedule_domain(dapm_pre_sequence_async, d,
2052 async_synchronize_full_domain(&async_domain);
2054 list_for_each_entry(w, &down_list, power_list) {
2055 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2058 list_for_each_entry(w, &up_list, power_list) {
2059 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2062 /* Power down widgets first; try to avoid amplifying pops. */
2063 dapm_seq_run(card, &down_list, event, false);
2065 dapm_widget_update(card);
2068 dapm_seq_run(card, &up_list, event, true);
2070 /* Run all the bias changes in parallel */
2071 for_each_card_dapms(card, d) {
2072 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2073 async_schedule_domain(dapm_post_sequence_async, d,
2076 async_synchronize_full_domain(&async_domain);
2077 /* Run card bias changes at last */
2078 dapm_post_sequence_async(&card->dapm, 0);
2080 /* do we need to notify any clients that DAPM event is complete */
2081 for_each_card_dapms(card, d) {
2085 ret = snd_soc_component_stream_event(d->component, event);
2090 pop_dbg(card->dev, card->pop_time,
2091 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2092 pop_wait(card->pop_time);
2094 trace_snd_soc_dapm_done(card);
2099 #ifdef CONFIG_DEBUG_FS
2100 static ssize_t dapm_widget_power_read_file(struct file *file,
2101 char __user *user_buf,
2102 size_t count, loff_t *ppos)
2104 struct snd_soc_dapm_widget *w = file->private_data;
2105 struct snd_soc_card *card = w->dapm->card;
2106 enum snd_soc_dapm_direction dir, rdir;
2110 struct snd_soc_dapm_path *p = NULL;
2112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2116 mutex_lock(&card->dapm_mutex);
2118 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2123 in = is_connected_input_ep(w, NULL, NULL);
2124 out = is_connected_output_ep(w, NULL, NULL);
2127 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2128 w->name, w->power ? "On" : "Off",
2129 w->force ? " (forced)" : "", in, out);
2132 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2133 " - R%d(0x%x) mask 0x%x",
2134 w->reg, w->reg, w->mask << w->shift);
2136 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2139 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2141 w->active ? "active" : "inactive");
2143 snd_soc_dapm_for_each_direction(dir) {
2144 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2145 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2146 if (p->connected && !p->connected(p->source, p->sink))
2152 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2153 " %s \"%s\" \"%s\"\n",
2154 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2155 p->name ? p->name : "static",
2156 p->node[rdir]->name);
2160 mutex_unlock(&card->dapm_mutex);
2162 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2168 static const struct file_operations dapm_widget_power_fops = {
2169 .open = simple_open,
2170 .read = dapm_widget_power_read_file,
2171 .llseek = default_llseek,
2174 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2175 size_t count, loff_t *ppos)
2177 struct snd_soc_dapm_context *dapm = file->private_data;
2180 switch (dapm->bias_level) {
2181 case SND_SOC_BIAS_ON:
2184 case SND_SOC_BIAS_PREPARE:
2185 level = "Prepare\n";
2187 case SND_SOC_BIAS_STANDBY:
2188 level = "Standby\n";
2190 case SND_SOC_BIAS_OFF:
2194 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2195 level = "Unknown\n";
2199 return simple_read_from_buffer(user_buf, count, ppos, level,
2203 static const struct file_operations dapm_bias_fops = {
2204 .open = simple_open,
2205 .read = dapm_bias_read_file,
2206 .llseek = default_llseek,
2209 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2210 struct dentry *parent)
2212 if (!parent || IS_ERR(parent))
2215 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2217 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2221 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2223 struct snd_soc_dapm_context *dapm = w->dapm;
2225 if (!dapm->debugfs_dapm || !w->name)
2228 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2229 &dapm_widget_power_fops);
2232 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2234 debugfs_remove_recursive(dapm->debugfs_dapm);
2235 dapm->debugfs_dapm = NULL;
2239 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2240 struct dentry *parent)
2244 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2248 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2255 * soc_dapm_connect_path() - Connects or disconnects a path
2256 * @path: The path to update
2257 * @connect: The new connect state of the path. True if the path is connected,
2258 * false if it is disconnected.
2259 * @reason: The reason why the path changed (for debugging only)
2261 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2262 bool connect, const char *reason)
2264 if (path->connect == connect)
2267 path->connect = connect;
2268 dapm_mark_dirty(path->source, reason);
2269 dapm_mark_dirty(path->sink, reason);
2270 dapm_path_invalidate(path);
2273 /* test and update the power status of a mux widget */
2274 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2275 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2277 struct snd_soc_dapm_path *path;
2281 lockdep_assert_held(&card->dapm_mutex);
2283 /* find dapm widget path assoc with kcontrol */
2284 dapm_kcontrol_for_each_path(path, kcontrol) {
2286 /* we now need to match the string in the enum to the path */
2287 if (e && !(strcmp(path->name, e->texts[mux])))
2292 soc_dapm_connect_path(path, connect, "mux update");
2296 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2301 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2302 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2303 struct snd_soc_dapm_update *update)
2305 struct snd_soc_card *card = dapm->card;
2308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2309 card->update = update;
2310 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2311 card->update = NULL;
2312 mutex_unlock(&card->dapm_mutex);
2314 snd_soc_dpcm_runtime_update(card);
2317 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2319 /* test and update the power status of a mixer or switch widget */
2320 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2321 struct snd_kcontrol *kcontrol,
2322 int connect, int rconnect)
2324 struct snd_soc_dapm_path *path;
2327 lockdep_assert_held(&card->dapm_mutex);
2329 /* find dapm widget path assoc with kcontrol */
2330 dapm_kcontrol_for_each_path(path, kcontrol) {
2332 * Ideally this function should support any number of
2333 * paths and channels. But since kcontrols only come
2334 * in mono and stereo variants, we are limited to 2
2337 * The following code assumes for stereo controls the
2338 * first path (when 'found == 0') is the left channel,
2339 * and all remaining paths (when 'found == 1') are the
2342 * A stereo control is signified by a valid 'rconnect'
2343 * value, either 0 for unconnected, or >= 0 for connected.
2344 * This is chosen instead of using snd_soc_volsw_is_stereo,
2345 * so that the behavior of snd_soc_dapm_mixer_update_power
2346 * doesn't change even when the kcontrol passed in is
2349 * It passes 'connect' as the path connect status for
2350 * the left channel, and 'rconnect' for the right
2353 if (found && rconnect >= 0)
2354 soc_dapm_connect_path(path, rconnect, "mixer update");
2356 soc_dapm_connect_path(path, connect, "mixer update");
2361 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2366 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2367 struct snd_kcontrol *kcontrol, int connect,
2368 struct snd_soc_dapm_update *update)
2370 struct snd_soc_card *card = dapm->card;
2373 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2374 card->update = update;
2375 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2376 card->update = NULL;
2377 mutex_unlock(&card->dapm_mutex);
2379 snd_soc_dpcm_runtime_update(card);
2382 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2384 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2387 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2388 struct snd_soc_dapm_widget *w;
2390 char *state = "not set";
2392 /* card won't be set for the dummy component, as a spot fix
2393 * we're checking for that case specifically here but in future
2394 * we will ensure that the dummy component looks like others.
2399 for_each_card_widgets(cmpnt->card, w) {
2400 if (w->dapm != dapm)
2403 /* only display widgets that burn power */
2405 case snd_soc_dapm_hp:
2406 case snd_soc_dapm_mic:
2407 case snd_soc_dapm_spk:
2408 case snd_soc_dapm_line:
2409 case snd_soc_dapm_micbias:
2410 case snd_soc_dapm_dac:
2411 case snd_soc_dapm_adc:
2412 case snd_soc_dapm_pga:
2413 case snd_soc_dapm_effect:
2414 case snd_soc_dapm_out_drv:
2415 case snd_soc_dapm_mixer:
2416 case snd_soc_dapm_mixer_named_ctl:
2417 case snd_soc_dapm_supply:
2418 case snd_soc_dapm_regulator_supply:
2419 case snd_soc_dapm_pinctrl:
2420 case snd_soc_dapm_clock_supply:
2422 count += sprintf(buf + count, "%s: %s\n",
2423 w->name, w->power ? "On":"Off");
2430 switch (snd_soc_dapm_get_bias_level(dapm)) {
2431 case SND_SOC_BIAS_ON:
2434 case SND_SOC_BIAS_PREPARE:
2437 case SND_SOC_BIAS_STANDBY:
2440 case SND_SOC_BIAS_OFF:
2444 count += sprintf(buf + count, "PM State: %s\n", state);
2449 /* show dapm widget status in sys fs */
2450 static ssize_t dapm_widget_show(struct device *dev,
2451 struct device_attribute *attr, char *buf)
2453 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2454 struct snd_soc_dai *codec_dai;
2457 mutex_lock(&rtd->card->dapm_mutex);
2459 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2460 struct snd_soc_component *cmpnt = codec_dai->component;
2462 count += dapm_widget_show_component(cmpnt, buf + count);
2465 mutex_unlock(&rtd->card->dapm_mutex);
2470 static DEVICE_ATTR_RO(dapm_widget);
2472 struct attribute *soc_dapm_dev_attrs[] = {
2473 &dev_attr_dapm_widget.attr,
2477 static void dapm_free_path(struct snd_soc_dapm_path *path)
2479 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2480 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2481 list_del(&path->list_kcontrol);
2482 list_del(&path->list);
2486 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2488 struct snd_soc_dapm_path *p, *next_p;
2489 enum snd_soc_dapm_direction dir;
2493 * remove source and sink paths associated to this widget.
2494 * While removing the path, remove reference to it from both
2495 * source and sink widgets so that path is removed only once.
2497 snd_soc_dapm_for_each_direction(dir) {
2498 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2502 kfree(w->kcontrols);
2503 kfree_const(w->name);
2504 kfree_const(w->sname);
2508 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2510 dapm->path_sink_cache.widget = NULL;
2511 dapm->path_source_cache.widget = NULL;
2514 /* free all dapm widgets and resources */
2515 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2517 struct snd_soc_dapm_widget *w, *next_w;
2519 for_each_card_widgets_safe(dapm->card, w, next_w) {
2520 if (w->dapm != dapm)
2522 snd_soc_dapm_free_widget(w);
2524 snd_soc_dapm_reset_cache(dapm);
2527 static struct snd_soc_dapm_widget *dapm_find_widget(
2528 struct snd_soc_dapm_context *dapm, const char *pin,
2529 bool search_other_contexts)
2531 struct snd_soc_dapm_widget *w;
2532 struct snd_soc_dapm_widget *fallback = NULL;
2534 for_each_card_widgets(dapm->card, w) {
2535 if (!strcmp(w->name, pin)) {
2536 if (w->dapm == dapm)
2543 if (search_other_contexts)
2549 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2550 const char *pin, int status)
2552 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2554 dapm_assert_locked(dapm);
2557 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2561 if (w->connected != status) {
2562 dapm_mark_dirty(w, "pin configuration");
2563 dapm_widget_invalidate_input_paths(w);
2564 dapm_widget_invalidate_output_paths(w);
2567 w->connected = status;
2575 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2576 * @dapm: DAPM context
2578 * Walks all dapm audio paths and powers widgets according to their
2579 * stream or path usage.
2581 * Requires external locking.
2583 * Returns 0 for success.
2585 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2588 * Suppress early reports (eg, jacks syncing their state) to avoid
2589 * silly DAPM runs during card startup.
2591 if (!dapm->card || !dapm->card->instantiated)
2594 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2596 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2599 * snd_soc_dapm_sync - scan and power dapm paths
2600 * @dapm: DAPM context
2602 * Walks all dapm audio paths and powers widgets according to their
2603 * stream or path usage.
2605 * Returns 0 for success.
2607 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2611 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2612 ret = snd_soc_dapm_sync_unlocked(dapm);
2613 mutex_unlock(&dapm->card->dapm_mutex);
2616 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2618 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2619 struct snd_soc_dapm_widget *w,
2623 case snd_soc_dapm_aif_out:
2624 case snd_soc_dapm_aif_in:
2630 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2631 w->channel < channels ? "Connecting" : "Disconnecting",
2632 p->source->name, p->sink->name);
2634 if (w->channel < channels)
2635 soc_dapm_connect_path(p, true, "dai update");
2637 soc_dapm_connect_path(p, false, "dai update");
2642 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2643 struct snd_pcm_hw_params *params,
2644 struct snd_soc_dai *dai)
2646 int dir = substream->stream;
2647 int channels = params_channels(params);
2648 struct snd_soc_dapm_path *p;
2649 struct snd_soc_dapm_widget *w;
2652 w = snd_soc_dai_get_widget(dai, dir);
2657 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2658 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2660 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2661 ret = dapm_update_dai_chan(p, p->sink, channels);
2666 snd_soc_dapm_widget_for_each_source_path(w, p) {
2667 ret = dapm_update_dai_chan(p, p->source, channels);
2675 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2676 struct snd_pcm_hw_params *params,
2677 struct snd_soc_dai *dai)
2679 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2682 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2683 ret = dapm_update_dai_unlocked(substream, params, dai);
2684 mutex_unlock(&rtd->card->dapm_mutex);
2688 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2691 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2692 * @w: The widget for which to update the flags
2694 * Some widgets have a dynamic category which depends on which neighbors they
2695 * are connected to. This function update the category for these widgets.
2697 * This function must be called whenever a path is added or removed to a widget.
2699 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2701 enum snd_soc_dapm_direction dir;
2702 struct snd_soc_dapm_path *p;
2706 case snd_soc_dapm_input:
2707 /* On a fully routed card an input is never a source */
2708 if (w->dapm->card->fully_routed)
2710 ep = SND_SOC_DAPM_EP_SOURCE;
2711 snd_soc_dapm_widget_for_each_source_path(w, p) {
2712 if (p->source->id == snd_soc_dapm_micbias ||
2713 p->source->id == snd_soc_dapm_mic ||
2714 p->source->id == snd_soc_dapm_line ||
2715 p->source->id == snd_soc_dapm_output) {
2721 case snd_soc_dapm_output:
2722 /* On a fully routed card a output is never a sink */
2723 if (w->dapm->card->fully_routed)
2725 ep = SND_SOC_DAPM_EP_SINK;
2726 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2727 if (p->sink->id == snd_soc_dapm_spk ||
2728 p->sink->id == snd_soc_dapm_hp ||
2729 p->sink->id == snd_soc_dapm_line ||
2730 p->sink->id == snd_soc_dapm_input) {
2736 case snd_soc_dapm_line:
2738 snd_soc_dapm_for_each_direction(dir) {
2739 if (!list_empty(&w->edges[dir]))
2740 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2750 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2751 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2752 const char *control)
2754 bool dynamic_source = false;
2755 bool dynamic_sink = false;
2760 switch (source->id) {
2761 case snd_soc_dapm_demux:
2762 dynamic_source = true;
2769 case snd_soc_dapm_mux:
2770 case snd_soc_dapm_switch:
2771 case snd_soc_dapm_mixer:
2772 case snd_soc_dapm_mixer_named_ctl:
2773 dynamic_sink = true;
2779 if (dynamic_source && dynamic_sink) {
2781 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2782 source->name, control, sink->name);
2784 } else if (!dynamic_source && !dynamic_sink) {
2786 "Control not supported for path %s -> [%s] -> %s\n",
2787 source->name, control, sink->name);
2794 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2795 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2796 const char *control,
2797 int (*connected)(struct snd_soc_dapm_widget *source,
2798 struct snd_soc_dapm_widget *sink))
2800 struct snd_soc_dapm_widget *widgets[2];
2801 enum snd_soc_dapm_direction dir;
2802 struct snd_soc_dapm_path *path;
2805 if (wsink->is_supply && !wsource->is_supply) {
2807 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2808 wsource->name, wsink->name);
2812 if (connected && !wsource->is_supply) {
2814 "connected() callback only supported for supply widgets (%s -> %s)\n",
2815 wsource->name, wsink->name);
2819 if (wsource->is_supply && control) {
2821 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2822 wsource->name, control, wsink->name);
2826 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2830 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2834 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2835 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2836 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2837 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2839 path->connected = connected;
2840 INIT_LIST_HEAD(&path->list);
2841 INIT_LIST_HEAD(&path->list_kcontrol);
2843 if (wsource->is_supply || wsink->is_supply)
2844 path->is_supply = 1;
2846 /* connect static paths */
2847 if (control == NULL) {
2850 switch (wsource->id) {
2851 case snd_soc_dapm_demux:
2852 ret = dapm_connect_mux(dapm, path, control, wsource);
2860 switch (wsink->id) {
2861 case snd_soc_dapm_mux:
2862 ret = dapm_connect_mux(dapm, path, control, wsink);
2866 case snd_soc_dapm_switch:
2867 case snd_soc_dapm_mixer:
2868 case snd_soc_dapm_mixer_named_ctl:
2869 ret = dapm_connect_mixer(dapm, path, control);
2878 list_add(&path->list, &dapm->card->paths);
2879 snd_soc_dapm_for_each_direction(dir)
2880 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2882 snd_soc_dapm_for_each_direction(dir) {
2883 dapm_update_widget_flags(widgets[dir]);
2884 dapm_mark_dirty(widgets[dir], "Route added");
2887 if (dapm->card->instantiated && path->connect)
2888 dapm_path_invalidate(path);
2896 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2897 const struct snd_soc_dapm_route *route)
2899 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2900 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2903 char prefixed_sink[80];
2904 char prefixed_source[80];
2906 unsigned int sink_ref = 0;
2907 unsigned int source_ref = 0;
2910 prefix = soc_dapm_prefix(dapm);
2912 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2913 prefix, route->sink);
2914 sink = prefixed_sink;
2915 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2916 prefix, route->source);
2917 source = prefixed_source;
2920 source = route->source;
2923 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2924 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2926 if (wsink && wsource)
2930 * find src and dest widgets over all widgets but favor a widget from
2931 * current DAPM context
2933 for_each_card_widgets(dapm->card, w) {
2934 if (!wsink && !(strcmp(w->name, sink))) {
2936 if (w->dapm == dapm) {
2944 "ASoC: sink widget %s overwritten\n",
2948 if (!wsource && !(strcmp(w->name, source))) {
2950 if (w->dapm == dapm) {
2958 "ASoC: source widget %s overwritten\n",
2962 /* use widget from another DAPM context if not found from this */
2968 if (wsource == NULL) {
2969 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2973 if (wsink == NULL) {
2974 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2980 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2981 dapm_wcache_update(&dapm->path_source_cache, wsource);
2983 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2990 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2991 source, route->control, sink);
2995 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2996 const struct snd_soc_dapm_route *route)
2998 struct snd_soc_dapm_widget *wsource, *wsink;
2999 struct snd_soc_dapm_path *path, *p;
3002 char prefixed_sink[80];
3003 char prefixed_source[80];
3006 if (route->control) {
3008 "ASoC: Removal of routes with controls not supported\n");
3012 prefix = soc_dapm_prefix(dapm);
3014 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3015 prefix, route->sink);
3016 sink = prefixed_sink;
3017 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3018 prefix, route->source);
3019 source = prefixed_source;
3022 source = route->source;
3026 list_for_each_entry(p, &dapm->card->paths, list) {
3027 if (strcmp(p->source->name, source) != 0)
3029 if (strcmp(p->sink->name, sink) != 0)
3036 wsource = path->source;
3039 dapm_mark_dirty(wsource, "Route removed");
3040 dapm_mark_dirty(wsink, "Route removed");
3042 dapm_path_invalidate(path);
3044 dapm_free_path(path);
3046 /* Update any path related flags */
3047 dapm_update_widget_flags(wsource);
3048 dapm_update_widget_flags(wsink);
3050 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3058 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3059 * @dapm: DAPM context
3060 * @route: audio routes
3061 * @num: number of routes
3063 * Connects 2 dapm widgets together via a named audio path. The sink is
3064 * the widget receiving the audio signal, whilst the source is the sender
3065 * of the audio signal.
3067 * Returns 0 for success else error. On error all resources can be freed
3068 * with a call to snd_soc_card_free().
3070 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3071 const struct snd_soc_dapm_route *route, int num)
3075 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3076 for (i = 0; i < num; i++) {
3077 r = snd_soc_dapm_add_route(dapm, route);
3079 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3081 route->control ? route->control : "direct",
3087 mutex_unlock(&dapm->card->dapm_mutex);
3091 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3094 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3095 * @dapm: DAPM context
3096 * @route: audio routes
3097 * @num: number of routes
3099 * Removes routes from the DAPM context.
3101 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3102 const struct snd_soc_dapm_route *route, int num)
3106 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3107 for (i = 0; i < num; i++) {
3108 snd_soc_dapm_del_route(dapm, route);
3111 mutex_unlock(&dapm->card->dapm_mutex);
3115 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3117 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3118 const struct snd_soc_dapm_route *route)
3120 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3123 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3126 struct snd_soc_dapm_path *path;
3130 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3136 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3141 if (route->control || route->connected)
3142 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3143 route->source, route->sink);
3145 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3146 if (path->sink == sink) {
3153 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3154 route->source, route->sink);
3156 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3157 count, route->source, route->sink);
3163 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3164 * @dapm: DAPM context
3165 * @route: audio routes
3166 * @num: number of routes
3168 * Mark existing routes matching those specified in the passed array
3169 * as being weak, meaning that they are ignored for the purpose of
3170 * power decisions. The main intended use case is for sidetone paths
3171 * which couple audio between other independent paths if they are both
3172 * active in order to make the combination work better at the user
3173 * level but which aren't intended to be "used".
3175 * Note that CODEC drivers should not use this as sidetone type paths
3176 * can frequently also be used as bypass paths.
3178 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3179 const struct snd_soc_dapm_route *route, int num)
3184 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3185 for (i = 0; i < num; i++) {
3186 err = snd_soc_dapm_weak_route(dapm, route);
3191 mutex_unlock(&dapm->card->dapm_mutex);
3195 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3198 * snd_soc_dapm_new_widgets - add new dapm widgets
3199 * @card: card to be checked for new dapm widgets
3201 * Checks the codec for any new dapm widgets and creates them if found.
3203 * Returns 0 for success.
3205 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3207 struct snd_soc_dapm_widget *w;
3210 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3212 for_each_card_widgets(card, w)
3217 if (w->num_kcontrols) {
3218 w->kcontrols = kcalloc(w->num_kcontrols,
3219 sizeof(struct snd_kcontrol *),
3221 if (!w->kcontrols) {
3222 mutex_unlock(&card->dapm_mutex);
3228 case snd_soc_dapm_switch:
3229 case snd_soc_dapm_mixer:
3230 case snd_soc_dapm_mixer_named_ctl:
3233 case snd_soc_dapm_mux:
3234 case snd_soc_dapm_demux:
3237 case snd_soc_dapm_pga:
3238 case snd_soc_dapm_effect:
3239 case snd_soc_dapm_out_drv:
3242 case snd_soc_dapm_dai_link:
3243 dapm_new_dai_link(w);
3249 /* Read the initial power state from the device */
3251 soc_dapm_read(w->dapm, w->reg, &val);
3252 val = val >> w->shift;
3254 if (val == w->on_val)
3260 dapm_mark_dirty(w, "new widget");
3261 dapm_debugfs_add_widget(w);
3264 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3265 mutex_unlock(&card->dapm_mutex);
3268 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3271 * snd_soc_dapm_get_volsw - dapm mixer get callback
3272 * @kcontrol: mixer control
3273 * @ucontrol: control element information
3275 * Callback to get the value of a dapm mixer control.
3277 * Returns 0 for success.
3279 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3280 struct snd_ctl_elem_value *ucontrol)
3282 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3283 struct snd_soc_card *card = dapm->card;
3284 struct soc_mixer_control *mc =
3285 (struct soc_mixer_control *)kcontrol->private_value;
3287 unsigned int shift = mc->shift;
3289 unsigned int width = fls(max);
3290 unsigned int mask = (1 << fls(max)) - 1;
3291 unsigned int invert = mc->invert;
3292 unsigned int reg_val, val, rval = 0;
3295 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3296 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3297 ret = soc_dapm_read(dapm, reg, ®_val);
3298 val = (reg_val >> shift) & mask;
3300 if (ret == 0 && reg != mc->rreg)
3301 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3303 if (snd_soc_volsw_is_stereo(mc))
3304 rval = (reg_val >> mc->rshift) & mask;
3306 reg_val = dapm_kcontrol_get_value(kcontrol);
3307 val = reg_val & mask;
3309 if (snd_soc_volsw_is_stereo(mc))
3310 rval = (reg_val >> width) & mask;
3312 mutex_unlock(&card->dapm_mutex);
3318 ucontrol->value.integer.value[0] = max - val;
3320 ucontrol->value.integer.value[0] = val;
3322 if (snd_soc_volsw_is_stereo(mc)) {
3324 ucontrol->value.integer.value[1] = max - rval;
3326 ucontrol->value.integer.value[1] = rval;
3331 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3334 * snd_soc_dapm_put_volsw - dapm mixer set callback
3335 * @kcontrol: mixer control
3336 * @ucontrol: control element information
3338 * Callback to set the value of a dapm mixer control.
3340 * Returns 0 for success.
3342 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3343 struct snd_ctl_elem_value *ucontrol)
3345 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3346 struct snd_soc_card *card = dapm->card;
3347 struct soc_mixer_control *mc =
3348 (struct soc_mixer_control *)kcontrol->private_value;
3350 unsigned int shift = mc->shift;
3352 unsigned int width = fls(max);
3353 unsigned int mask = (1 << width) - 1;
3354 unsigned int invert = mc->invert;
3355 unsigned int val, rval = 0;
3356 int connect, rconnect = -1, change, reg_change = 0;
3357 struct snd_soc_dapm_update update = {};
3360 val = (ucontrol->value.integer.value[0] & mask);
3366 if (snd_soc_volsw_is_stereo(mc)) {
3367 rval = (ucontrol->value.integer.value[1] & mask);
3373 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3375 /* This assumes field width < (bits in unsigned int / 2) */
3376 if (width > sizeof(unsigned int) * 8 / 2)
3378 "ASoC: control %s field width limit exceeded\n",
3380 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3382 if (reg != SND_SOC_NOPM) {
3384 rval = rval << mc->rshift;
3386 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3388 if (snd_soc_volsw_is_stereo(mc))
3389 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3394 if (change || reg_change) {
3396 if (snd_soc_volsw_is_stereo(mc)) {
3397 update.has_second_set = true;
3398 update.reg2 = mc->rreg;
3399 update.mask2 = mask << mc->rshift;
3402 update.kcontrol = kcontrol;
3404 update.mask = mask << shift;
3406 card->update = &update;
3408 change |= reg_change;
3410 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3413 card->update = NULL;
3416 mutex_unlock(&card->dapm_mutex);
3419 snd_soc_dpcm_runtime_update(card);
3423 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3426 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3427 * @kcontrol: mixer control
3428 * @ucontrol: control element information
3430 * Callback to get the value of a dapm enumerated double mixer control.
3432 * Returns 0 for success.
3434 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3435 struct snd_ctl_elem_value *ucontrol)
3437 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3438 struct snd_soc_card *card = dapm->card;
3439 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3440 unsigned int reg_val, val;
3442 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3443 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3444 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3446 mutex_unlock(&card->dapm_mutex);
3450 reg_val = dapm_kcontrol_get_value(kcontrol);
3452 mutex_unlock(&card->dapm_mutex);
3454 val = (reg_val >> e->shift_l) & e->mask;
3455 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3456 if (e->shift_l != e->shift_r) {
3457 val = (reg_val >> e->shift_r) & e->mask;
3458 val = snd_soc_enum_val_to_item(e, val);
3459 ucontrol->value.enumerated.item[1] = val;
3464 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3467 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3468 * @kcontrol: mixer control
3469 * @ucontrol: control element information
3471 * Callback to set the value of a dapm enumerated double mixer control.
3473 * Returns 0 for success.
3475 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3476 struct snd_ctl_elem_value *ucontrol)
3478 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3479 struct snd_soc_card *card = dapm->card;
3480 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3481 unsigned int *item = ucontrol->value.enumerated.item;
3482 unsigned int val, change, reg_change = 0;
3484 struct snd_soc_dapm_update update = {};
3487 if (item[0] >= e->items)
3490 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3491 mask = e->mask << e->shift_l;
3492 if (e->shift_l != e->shift_r) {
3493 if (item[1] > e->items)
3495 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3496 mask |= e->mask << e->shift_r;
3499 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3501 change = dapm_kcontrol_set_value(kcontrol, val);
3503 if (e->reg != SND_SOC_NOPM)
3504 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3506 if (change || reg_change) {
3508 update.kcontrol = kcontrol;
3509 update.reg = e->reg;
3512 card->update = &update;
3514 change |= reg_change;
3516 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3518 card->update = NULL;
3521 mutex_unlock(&card->dapm_mutex);
3524 snd_soc_dpcm_runtime_update(card);
3528 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3531 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3533 * @kcontrol: mixer control
3534 * @uinfo: control element information
3536 * Callback to provide information about a pin switch control.
3538 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3539 struct snd_ctl_elem_info *uinfo)
3541 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3543 uinfo->value.integer.min = 0;
3544 uinfo->value.integer.max = 1;
3548 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3551 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3553 * @kcontrol: mixer control
3556 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3557 struct snd_ctl_elem_value *ucontrol)
3559 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3560 const char *pin = (const char *)kcontrol->private_value;
3562 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3564 ucontrol->value.integer.value[0] =
3565 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3567 mutex_unlock(&card->dapm_mutex);
3571 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3574 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3576 * @kcontrol: mixer control
3579 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3580 struct snd_ctl_elem_value *ucontrol)
3582 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3583 const char *pin = (const char *)kcontrol->private_value;
3585 if (ucontrol->value.integer.value[0])
3586 snd_soc_dapm_enable_pin(&card->dapm, pin);
3588 snd_soc_dapm_disable_pin(&card->dapm, pin);
3590 snd_soc_dapm_sync(&card->dapm);
3593 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3595 struct snd_soc_dapm_widget *
3596 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3597 const struct snd_soc_dapm_widget *widget)
3599 enum snd_soc_dapm_direction dir;
3600 struct snd_soc_dapm_widget *w;
3604 if ((w = dapm_cnew_widget(widget)) == NULL)
3605 return ERR_PTR(-ENOMEM);
3608 case snd_soc_dapm_regulator_supply:
3609 w->regulator = devm_regulator_get(dapm->dev, w->name);
3610 if (IS_ERR(w->regulator)) {
3611 ret = PTR_ERR(w->regulator);
3612 goto request_failed;
3615 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3616 ret = regulator_allow_bypass(w->regulator, true);
3619 "ASoC: Failed to bypass %s: %d\n",
3623 case snd_soc_dapm_pinctrl:
3624 w->pinctrl = devm_pinctrl_get(dapm->dev);
3625 if (IS_ERR(w->pinctrl)) {
3626 ret = PTR_ERR(w->pinctrl);
3627 goto request_failed;
3630 /* set to sleep_state when initializing */
3631 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3633 case snd_soc_dapm_clock_supply:
3634 w->clk = devm_clk_get(dapm->dev, w->name);
3635 if (IS_ERR(w->clk)) {
3636 ret = PTR_ERR(w->clk);
3637 goto request_failed;
3644 prefix = soc_dapm_prefix(dapm);
3646 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3648 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3649 if (w->name == NULL) {
3650 kfree_const(w->sname);
3652 return ERR_PTR(-ENOMEM);
3656 case snd_soc_dapm_mic:
3657 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3658 w->power_check = dapm_generic_check_power;
3660 case snd_soc_dapm_input:
3661 if (!dapm->card->fully_routed)
3662 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3663 w->power_check = dapm_generic_check_power;
3665 case snd_soc_dapm_spk:
3666 case snd_soc_dapm_hp:
3667 w->is_ep = SND_SOC_DAPM_EP_SINK;
3668 w->power_check = dapm_generic_check_power;
3670 case snd_soc_dapm_output:
3671 if (!dapm->card->fully_routed)
3672 w->is_ep = SND_SOC_DAPM_EP_SINK;
3673 w->power_check = dapm_generic_check_power;
3675 case snd_soc_dapm_vmid:
3676 case snd_soc_dapm_siggen:
3677 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3678 w->power_check = dapm_always_on_check_power;
3680 case snd_soc_dapm_sink:
3681 w->is_ep = SND_SOC_DAPM_EP_SINK;
3682 w->power_check = dapm_always_on_check_power;
3685 case snd_soc_dapm_mux:
3686 case snd_soc_dapm_demux:
3687 case snd_soc_dapm_switch:
3688 case snd_soc_dapm_mixer:
3689 case snd_soc_dapm_mixer_named_ctl:
3690 case snd_soc_dapm_adc:
3691 case snd_soc_dapm_aif_out:
3692 case snd_soc_dapm_dac:
3693 case snd_soc_dapm_aif_in:
3694 case snd_soc_dapm_pga:
3695 case snd_soc_dapm_buffer:
3696 case snd_soc_dapm_scheduler:
3697 case snd_soc_dapm_effect:
3698 case snd_soc_dapm_src:
3699 case snd_soc_dapm_asrc:
3700 case snd_soc_dapm_encoder:
3701 case snd_soc_dapm_decoder:
3702 case snd_soc_dapm_out_drv:
3703 case snd_soc_dapm_micbias:
3704 case snd_soc_dapm_line:
3705 case snd_soc_dapm_dai_link:
3706 case snd_soc_dapm_dai_out:
3707 case snd_soc_dapm_dai_in:
3708 w->power_check = dapm_generic_check_power;
3710 case snd_soc_dapm_supply:
3711 case snd_soc_dapm_regulator_supply:
3712 case snd_soc_dapm_pinctrl:
3713 case snd_soc_dapm_clock_supply:
3714 case snd_soc_dapm_kcontrol:
3716 w->power_check = dapm_supply_check_power;
3719 w->power_check = dapm_always_on_check_power;
3724 INIT_LIST_HEAD(&w->list);
3725 INIT_LIST_HEAD(&w->dirty);
3726 /* see for_each_card_widgets */
3727 list_add_tail(&w->list, &dapm->card->widgets);
3729 snd_soc_dapm_for_each_direction(dir) {
3730 INIT_LIST_HEAD(&w->edges[dir]);
3731 w->endpoints[dir] = -1;
3734 /* machine layer sets up unconnected pins and insertions */
3739 if (ret != -EPROBE_DEFER)
3740 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3743 kfree_const(w->sname);
3745 return ERR_PTR(ret);
3749 * snd_soc_dapm_new_control - create new dapm control
3750 * @dapm: DAPM context
3751 * @widget: widget template
3753 * Creates new DAPM control based upon a template.
3755 * Returns a widget pointer on success or an error pointer on failure
3757 struct snd_soc_dapm_widget *
3758 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3759 const struct snd_soc_dapm_widget *widget)
3761 struct snd_soc_dapm_widget *w;
3763 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3764 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3765 mutex_unlock(&dapm->card->dapm_mutex);
3769 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3772 * snd_soc_dapm_new_controls - create new dapm controls
3773 * @dapm: DAPM context
3774 * @widget: widget array
3775 * @num: number of widgets
3777 * Creates new DAPM controls based upon the templates.
3779 * Returns 0 for success else error.
3781 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3782 const struct snd_soc_dapm_widget *widget,
3785 struct snd_soc_dapm_widget *w;
3789 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3790 for (i = 0; i < num; i++) {
3791 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3798 mutex_unlock(&dapm->card->dapm_mutex);
3801 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3804 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3805 struct snd_pcm_substream *substream)
3807 struct snd_soc_dapm_path *path;
3808 struct snd_soc_dai *source, *sink;
3809 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3810 struct snd_pcm_hw_params *params = NULL;
3811 const struct snd_soc_pcm_stream *config = NULL;
3812 struct snd_pcm_runtime *runtime = NULL;
3816 params = kzalloc(sizeof(*params), GFP_KERNEL);
3820 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3826 substream->runtime = runtime;
3828 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3829 snd_soc_dapm_widget_for_each_source_path(w, path) {
3830 source = path->source->priv;
3832 ret = snd_soc_dai_startup(source, substream);
3834 dev_err(source->dev,
3835 "ASoC: startup() failed: %d\n", ret);
3841 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3842 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3843 sink = path->sink->priv;
3845 ret = snd_soc_dai_startup(sink, substream);
3848 "ASoC: startup() failed: %d\n", ret);
3854 substream->hw_opened = 1;
3857 * Note: getting the config after .startup() gives a chance to
3858 * either party on the link to alter the configuration if
3861 config = rtd->dai_link->params + rtd->params_select;
3862 if (WARN_ON(!config)) {
3863 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3868 /* Be a little careful as we don't want to overflow the mask array */
3869 if (config->formats) {
3870 fmt = ffs(config->formats) - 1;
3872 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3879 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3880 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3882 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3884 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3885 = config->channels_min;
3886 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3887 = config->channels_max;
3889 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3890 snd_soc_dapm_widget_for_each_source_path(w, path) {
3891 source = path->source->priv;
3893 ret = snd_soc_dai_hw_params(source, substream, params);
3897 dapm_update_dai_unlocked(substream, params, source);
3900 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3901 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3902 sink = path->sink->priv;
3904 ret = snd_soc_dai_hw_params(sink, substream, params);
3908 dapm_update_dai_unlocked(substream, params, sink);
3911 runtime->format = params_format(params);
3912 runtime->subformat = params_subformat(params);
3913 runtime->channels = params_channels(params);
3914 runtime->rate = params_rate(params);
3921 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3922 struct snd_kcontrol *kcontrol, int event)
3924 struct snd_soc_dapm_path *path;
3925 struct snd_soc_dai *source, *sink;
3926 struct snd_pcm_substream *substream = w->priv;
3927 int ret = 0, saved_stream = substream->stream;
3929 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3930 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3934 case SND_SOC_DAPM_PRE_PMU:
3935 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3941 case SND_SOC_DAPM_POST_PMU:
3942 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3943 sink = path->sink->priv;
3945 ret = snd_soc_dai_digital_mute(sink, 0,
3946 SNDRV_PCM_STREAM_PLAYBACK);
3947 if (ret != 0 && ret != -ENOTSUPP)
3949 "ASoC: Failed to unmute: %d\n", ret);
3954 case SND_SOC_DAPM_PRE_PMD:
3955 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3956 sink = path->sink->priv;
3958 ret = snd_soc_dai_digital_mute(sink, 1,
3959 SNDRV_PCM_STREAM_PLAYBACK);
3960 if (ret != 0 && ret != -ENOTSUPP)
3962 "ASoC: Failed to mute: %d\n", ret);
3966 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3967 snd_soc_dapm_widget_for_each_source_path(w, path) {
3968 source = path->source->priv;
3969 snd_soc_dai_hw_free(source, substream);
3972 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3973 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3974 sink = path->sink->priv;
3975 snd_soc_dai_hw_free(sink, substream);
3978 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3979 snd_soc_dapm_widget_for_each_source_path(w, path) {
3980 source = path->source->priv;
3982 snd_soc_dai_shutdown(source, substream);
3985 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3986 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3987 sink = path->sink->priv;
3989 snd_soc_dai_shutdown(sink, substream);
3993 case SND_SOC_DAPM_POST_PMD:
3994 kfree(substream->runtime);
3998 WARN(1, "Unknown event %d\n", event);
4003 /* Restore the substream direction */
4004 substream->stream = saved_stream;
4008 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4009 struct snd_ctl_elem_value *ucontrol)
4011 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4012 struct snd_soc_pcm_runtime *rtd = w->priv;
4014 ucontrol->value.enumerated.item[0] = rtd->params_select;
4019 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4020 struct snd_ctl_elem_value *ucontrol)
4022 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4023 struct snd_soc_pcm_runtime *rtd = w->priv;
4025 /* Can't change the config when widget is already powered */
4029 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4032 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4035 rtd->params_select = ucontrol->value.enumerated.item[0];
4041 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4042 unsigned long *private_value,
4044 const char **w_param_text)
4048 devm_kfree(card->dev, (void *)*private_value);
4053 for (count = 0 ; count < num_params; count++)
4054 devm_kfree(card->dev, (void *)w_param_text[count]);
4055 devm_kfree(card->dev, w_param_text);
4058 static struct snd_kcontrol_new *
4059 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4061 const struct snd_soc_pcm_stream *params,
4062 int num_params, const char **w_param_text,
4063 unsigned long *private_value)
4065 struct soc_enum w_param_enum[] = {
4066 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4068 struct snd_kcontrol_new kcontrol_dai_link[] = {
4069 SOC_ENUM_EXT(NULL, w_param_enum[0],
4070 snd_soc_dapm_dai_link_get,
4071 snd_soc_dapm_dai_link_put),
4073 struct snd_kcontrol_new *kcontrol_news;
4074 const struct snd_soc_pcm_stream *config = params;
4077 for (count = 0 ; count < num_params; count++) {
4078 if (!config->stream_name) {
4079 dev_warn(card->dapm.dev,
4080 "ASoC: anonymous config %d for dai link %s\n",
4082 w_param_text[count] =
4083 devm_kasprintf(card->dev, GFP_KERNEL,
4084 "Anonymous Configuration %d",
4087 w_param_text[count] = devm_kmemdup(card->dev,
4088 config->stream_name,
4089 strlen(config->stream_name) + 1,
4092 if (!w_param_text[count])
4093 goto outfree_w_param;
4097 w_param_enum[0].items = num_params;
4098 w_param_enum[0].texts = w_param_text;
4101 (unsigned long) devm_kmemdup(card->dev,
4102 (void *)(kcontrol_dai_link[0].private_value),
4103 sizeof(struct soc_enum), GFP_KERNEL);
4104 if (!*private_value) {
4105 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4107 goto outfree_w_param;
4109 kcontrol_dai_link[0].private_value = *private_value;
4110 /* duplicate kcontrol_dai_link on heap so that memory persists */
4111 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4112 sizeof(struct snd_kcontrol_new),
4114 if (!kcontrol_news) {
4115 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4117 goto outfree_w_param;
4119 return kcontrol_news;
4122 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4126 static struct snd_soc_dapm_widget *
4127 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4128 struct snd_pcm_substream *substream,
4131 struct snd_soc_pcm_runtime *rtd = substream->private_data;
4132 struct snd_soc_dapm_widget template;
4133 struct snd_soc_dapm_widget *w;
4134 const char **w_param_text;
4135 unsigned long private_value = 0;
4139 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4140 rtd->dai_link->name, id);
4142 return ERR_PTR(-ENOMEM);
4144 memset(&template, 0, sizeof(template));
4145 template.reg = SND_SOC_NOPM;
4146 template.id = snd_soc_dapm_dai_link;
4147 template.name = link_name;
4148 template.event = snd_soc_dai_link_event;
4149 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4150 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4151 template.kcontrol_news = NULL;
4153 /* allocate memory for control, only in case of multiple configs */
4154 if (rtd->dai_link->num_params > 1) {
4155 w_param_text = devm_kcalloc(card->dev,
4156 rtd->dai_link->num_params,
4157 sizeof(char *), GFP_KERNEL);
4158 if (!w_param_text) {
4163 template.num_kcontrols = 1;
4164 template.kcontrol_news =
4165 snd_soc_dapm_alloc_kcontrol(card,
4167 rtd->dai_link->params,
4168 rtd->dai_link->num_params,
4169 w_param_text, &private_value);
4170 if (!template.kcontrol_news) {
4175 w_param_text = NULL;
4177 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4179 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4182 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4184 goto outfree_kcontrol_news;
4187 w->priv = substream;
4191 outfree_kcontrol_news:
4192 devm_kfree(card->dev, (void *)template.kcontrol_news);
4193 snd_soc_dapm_free_kcontrol(card, &private_value,
4194 rtd->dai_link->num_params, w_param_text);
4196 devm_kfree(card->dev, link_name);
4197 return ERR_PTR(ret);
4200 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4201 struct snd_soc_dai *dai)
4203 struct snd_soc_dapm_widget template;
4204 struct snd_soc_dapm_widget *w;
4206 WARN_ON(dapm->dev != dai->dev);
4208 memset(&template, 0, sizeof(template));
4209 template.reg = SND_SOC_NOPM;
4211 if (dai->driver->playback.stream_name) {
4212 template.id = snd_soc_dapm_dai_in;
4213 template.name = dai->driver->playback.stream_name;
4214 template.sname = dai->driver->playback.stream_name;
4216 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4219 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4224 dai->playback_widget = w;
4227 if (dai->driver->capture.stream_name) {
4228 template.id = snd_soc_dapm_dai_out;
4229 template.name = dai->driver->capture.stream_name;
4230 template.sname = dai->driver->capture.stream_name;
4232 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4235 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4240 dai->capture_widget = w;
4246 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4248 struct snd_soc_dapm_widget *dai_w, *w;
4249 struct snd_soc_dapm_widget *src, *sink;
4250 struct snd_soc_dai *dai;
4252 /* For each DAI widget... */
4253 for_each_card_widgets(card, dai_w) {
4254 switch (dai_w->id) {
4255 case snd_soc_dapm_dai_in:
4256 case snd_soc_dapm_dai_out:
4262 /* let users know there is no DAI to link */
4264 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4271 /* ...find all widgets with the same stream and link them */
4272 for_each_card_widgets(card, w) {
4273 if (w->dapm != dai_w->dapm)
4277 case snd_soc_dapm_dai_in:
4278 case snd_soc_dapm_dai_out:
4284 if (!w->sname || !strstr(w->sname, dai_w->sname))
4287 if (dai_w->id == snd_soc_dapm_dai_in) {
4294 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4295 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4302 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4303 struct snd_soc_dai *src_dai,
4304 struct snd_soc_dapm_widget *src,
4305 struct snd_soc_dapm_widget *dai,
4306 struct snd_soc_dai *sink_dai,
4307 struct snd_soc_dapm_widget *sink)
4309 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4310 src_dai->component->name, src->name,
4311 sink_dai->component->name, sink->name);
4314 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4318 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4321 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4322 struct snd_soc_pcm_runtime *rtd,
4323 struct snd_soc_dai *codec_dai,
4324 struct snd_soc_dai *cpu_dai)
4326 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4327 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4328 struct snd_pcm_substream *substream;
4329 struct snd_pcm_str *streams = rtd->pcm->streams;
4331 if (dai_link->params) {
4332 playback_cpu = cpu_dai->capture_widget;
4333 capture_cpu = cpu_dai->playback_widget;
4335 playback_cpu = cpu_dai->playback_widget;
4336 capture_cpu = cpu_dai->capture_widget;
4339 /* connect BE DAI playback if widgets are valid */
4340 codec = codec_dai->playback_widget;
4342 if (playback_cpu && codec) {
4343 if (dai_link->params && !dai_link->playback_widget) {
4344 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4345 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4348 dai_link->playback_widget = dai;
4351 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4352 dai_link->playback_widget,
4357 /* connect BE DAI capture if widgets are valid */
4358 codec = codec_dai->capture_widget;
4360 if (codec && capture_cpu) {
4361 if (dai_link->params && !dai_link->capture_widget) {
4362 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4363 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4366 dai_link->capture_widget = dai;
4369 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4370 dai_link->capture_widget,
4371 cpu_dai, capture_cpu);
4375 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4378 struct snd_soc_dapm_widget *w;
4381 w = snd_soc_dai_get_widget(dai, stream);
4384 dapm_mark_dirty(w, "stream event");
4386 if (w->id == snd_soc_dapm_dai_in) {
4387 ep = SND_SOC_DAPM_EP_SOURCE;
4388 dapm_widget_invalidate_input_paths(w);
4390 ep = SND_SOC_DAPM_EP_SINK;
4391 dapm_widget_invalidate_output_paths(w);
4395 case SND_SOC_DAPM_STREAM_START:
4399 case SND_SOC_DAPM_STREAM_STOP:
4403 case SND_SOC_DAPM_STREAM_SUSPEND:
4404 case SND_SOC_DAPM_STREAM_RESUME:
4405 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4406 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4412 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4414 struct snd_soc_pcm_runtime *rtd;
4415 struct snd_soc_dai *codec_dai;
4418 /* for each BE DAI link... */
4419 for_each_card_rtds(card, rtd) {
4421 * dynamic FE links have no fixed DAI mapping.
4422 * CODEC<->CODEC links have no direct connection.
4424 if (rtd->dai_link->dynamic)
4427 if (rtd->num_cpus == 1) {
4428 for_each_rtd_codec_dais(rtd, i, codec_dai)
4429 dapm_connect_dai_pair(card, rtd, codec_dai,
4431 } else if (rtd->num_codecs == rtd->num_cpus) {
4432 for_each_rtd_codec_dais(rtd, i, codec_dai)
4433 dapm_connect_dai_pair(card, rtd, codec_dai,
4437 "N cpus to M codecs link is not supported yet\n");
4442 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4445 struct snd_soc_dai *dai;
4448 for_each_rtd_dais(rtd, i, dai)
4449 soc_dapm_dai_stream_event(dai, stream, event);
4451 dapm_power_widgets(rtd->card, event);
4455 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4456 * @rtd: PCM runtime data
4457 * @stream: stream name
4458 * @event: stream event
4460 * Sends a stream event to the dapm core. The core then makes any
4461 * necessary widget power changes.
4463 * Returns 0 for success else error.
4465 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4468 struct snd_soc_card *card = rtd->card;
4470 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4471 soc_dapm_stream_event(rtd, stream, event);
4472 mutex_unlock(&card->dapm_mutex);
4475 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4477 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4478 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4479 /* powered down playback stream now */
4480 snd_soc_dapm_stream_event(rtd,
4481 SNDRV_PCM_STREAM_PLAYBACK,
4482 SND_SOC_DAPM_STREAM_STOP);
4484 /* start delayed pop wq here for playback streams */
4486 queue_delayed_work(system_power_efficient_wq,
4488 msecs_to_jiffies(rtd->pmdown_time));
4491 /* capture streams can be powered down now */
4492 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4493 SND_SOC_DAPM_STREAM_STOP);
4496 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4499 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4500 * @dapm: DAPM context
4503 * Enables input/output pin and its parents or children widgets iff there is
4504 * a valid audio route and active audio stream.
4506 * Requires external locking.
4508 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4509 * do any widget power switching.
4511 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4514 return snd_soc_dapm_set_pin(dapm, pin, 1);
4516 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4519 * snd_soc_dapm_enable_pin - enable pin.
4520 * @dapm: DAPM context
4523 * Enables input/output pin and its parents or children widgets iff there is
4524 * a valid audio route and active audio stream.
4526 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4527 * do any widget power switching.
4529 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4533 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4535 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4537 mutex_unlock(&dapm->card->dapm_mutex);
4541 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4544 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4545 * @dapm: DAPM context
4548 * Enables input/output pin regardless of any other state. This is
4549 * intended for use with microphone bias supplies used in microphone
4552 * Requires external locking.
4554 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4555 * do any widget power switching.
4557 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4560 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4563 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4567 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4568 if (!w->connected) {
4570 * w->force does not affect the number of input or output paths,
4571 * so we only have to recheck if w->connected is changed
4573 dapm_widget_invalidate_input_paths(w);
4574 dapm_widget_invalidate_output_paths(w);
4578 dapm_mark_dirty(w, "force enable");
4582 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4585 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4586 * @dapm: DAPM context
4589 * Enables input/output pin regardless of any other state. This is
4590 * intended for use with microphone bias supplies used in microphone
4593 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4594 * do any widget power switching.
4596 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4601 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4603 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4605 mutex_unlock(&dapm->card->dapm_mutex);
4609 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4612 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4613 * @dapm: DAPM context
4616 * Disables input/output pin and its parents or children widgets.
4618 * Requires external locking.
4620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4621 * do any widget power switching.
4623 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4626 return snd_soc_dapm_set_pin(dapm, pin, 0);
4628 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4631 * snd_soc_dapm_disable_pin - disable pin.
4632 * @dapm: DAPM context
4635 * Disables input/output pin and its parents or children widgets.
4637 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4638 * do any widget power switching.
4640 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4645 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4647 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4649 mutex_unlock(&dapm->card->dapm_mutex);
4653 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4656 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4657 * @dapm: DAPM context
4660 * Marks the specified pin as being not connected, disabling it along
4661 * any parent or child widgets. At present this is identical to
4662 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4663 * additional things such as disabling controls which only affect
4664 * paths through the pin.
4666 * Requires external locking.
4668 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4669 * do any widget power switching.
4671 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4674 return snd_soc_dapm_set_pin(dapm, pin, 0);
4676 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4679 * snd_soc_dapm_nc_pin - permanently disable pin.
4680 * @dapm: DAPM context
4683 * Marks the specified pin as being not connected, disabling it along
4684 * any parent or child widgets. At present this is identical to
4685 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4686 * additional things such as disabling controls which only affect
4687 * paths through the pin.
4689 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4690 * do any widget power switching.
4692 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4696 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4698 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4700 mutex_unlock(&dapm->card->dapm_mutex);
4704 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4707 * snd_soc_dapm_get_pin_status - get audio pin status
4708 * @dapm: DAPM context
4709 * @pin: audio signal pin endpoint (or start point)
4711 * Get audio pin status - connected or disconnected.
4713 * Returns 1 for connected otherwise 0.
4715 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4718 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4721 return w->connected;
4725 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4728 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4729 * @dapm: DAPM context
4730 * @pin: audio signal pin endpoint (or start point)
4732 * Mark the given endpoint or pin as ignoring suspend. When the
4733 * system is disabled a path between two endpoints flagged as ignoring
4734 * suspend will not be disabled. The path must already be enabled via
4735 * normal means at suspend time, it will not be turned on if it was not
4738 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4741 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4744 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4748 w->ignore_suspend = 1;
4752 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4755 * snd_soc_dapm_free - free dapm resources
4756 * @dapm: DAPM context
4758 * Free all dapm widgets and resources.
4760 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4762 dapm_debugfs_cleanup(dapm);
4763 dapm_free_widgets(dapm);
4764 list_del(&dapm->list);
4766 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4768 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4769 struct snd_soc_card *card,
4770 struct snd_soc_component *component)
4773 dapm->component = component;
4774 dapm->bias_level = SND_SOC_BIAS_OFF;
4777 dapm->dev = component->dev;
4778 dapm->idle_bias_off = !component->driver->idle_bias_on,
4779 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4781 dapm->dev = card->dev;
4784 INIT_LIST_HEAD(&dapm->list);
4785 /* see for_each_card_dapms */
4786 list_add(&dapm->list, &card->dapm_list);
4788 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4790 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4792 struct snd_soc_card *card = dapm->card;
4793 struct snd_soc_dapm_widget *w;
4794 LIST_HEAD(down_list);
4797 mutex_lock(&card->dapm_mutex);
4799 for_each_card_widgets(dapm->card, w) {
4800 if (w->dapm != dapm)
4803 dapm_seq_insert(w, &down_list, false);
4809 /* If there were no widgets to power down we're already in
4813 if (dapm->bias_level == SND_SOC_BIAS_ON)
4814 snd_soc_dapm_set_bias_level(dapm,
4815 SND_SOC_BIAS_PREPARE);
4816 dapm_seq_run(card, &down_list, 0, false);
4817 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4818 snd_soc_dapm_set_bias_level(dapm,
4819 SND_SOC_BIAS_STANDBY);
4822 mutex_unlock(&card->dapm_mutex);
4826 * snd_soc_dapm_shutdown - callback for system shutdown
4828 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4830 struct snd_soc_dapm_context *dapm;
4832 for_each_card_dapms(card, dapm) {
4833 if (dapm != &card->dapm) {
4834 soc_dapm_shutdown_dapm(dapm);
4835 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4836 snd_soc_dapm_set_bias_level(dapm,
4841 soc_dapm_shutdown_dapm(&card->dapm);
4842 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4843 snd_soc_dapm_set_bias_level(&card->dapm,
4847 /* Module information */
4848 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4849 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4850 MODULE_LICENSE("GPL");