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 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
67 /* dapm power sequences - make this per codec in the future */
68 static int dapm_up_seq[] = {
69 [snd_soc_dapm_pre] = 1,
70 [snd_soc_dapm_regulator_supply] = 2,
71 [snd_soc_dapm_pinctrl] = 2,
72 [snd_soc_dapm_clock_supply] = 2,
73 [snd_soc_dapm_supply] = 3,
74 [snd_soc_dapm_micbias] = 4,
75 [snd_soc_dapm_vmid] = 4,
76 [snd_soc_dapm_dai_link] = 3,
77 [snd_soc_dapm_dai_in] = 5,
78 [snd_soc_dapm_dai_out] = 5,
79 [snd_soc_dapm_aif_in] = 5,
80 [snd_soc_dapm_aif_out] = 5,
81 [snd_soc_dapm_mic] = 6,
82 [snd_soc_dapm_siggen] = 6,
83 [snd_soc_dapm_input] = 6,
84 [snd_soc_dapm_output] = 6,
85 [snd_soc_dapm_mux] = 7,
86 [snd_soc_dapm_demux] = 7,
87 [snd_soc_dapm_dac] = 8,
88 [snd_soc_dapm_switch] = 9,
89 [snd_soc_dapm_mixer] = 9,
90 [snd_soc_dapm_mixer_named_ctl] = 9,
91 [snd_soc_dapm_pga] = 10,
92 [snd_soc_dapm_buffer] = 10,
93 [snd_soc_dapm_scheduler] = 10,
94 [snd_soc_dapm_effect] = 10,
95 [snd_soc_dapm_src] = 10,
96 [snd_soc_dapm_asrc] = 10,
97 [snd_soc_dapm_encoder] = 10,
98 [snd_soc_dapm_decoder] = 10,
99 [snd_soc_dapm_adc] = 11,
100 [snd_soc_dapm_out_drv] = 12,
101 [snd_soc_dapm_hp] = 12,
102 [snd_soc_dapm_spk] = 12,
103 [snd_soc_dapm_line] = 12,
104 [snd_soc_dapm_sink] = 12,
105 [snd_soc_dapm_kcontrol] = 13,
106 [snd_soc_dapm_post] = 14,
109 static int dapm_down_seq[] = {
110 [snd_soc_dapm_pre] = 1,
111 [snd_soc_dapm_kcontrol] = 2,
112 [snd_soc_dapm_adc] = 3,
113 [snd_soc_dapm_hp] = 4,
114 [snd_soc_dapm_spk] = 4,
115 [snd_soc_dapm_line] = 4,
116 [snd_soc_dapm_out_drv] = 4,
117 [snd_soc_dapm_sink] = 4,
118 [snd_soc_dapm_pga] = 5,
119 [snd_soc_dapm_buffer] = 5,
120 [snd_soc_dapm_scheduler] = 5,
121 [snd_soc_dapm_effect] = 5,
122 [snd_soc_dapm_src] = 5,
123 [snd_soc_dapm_asrc] = 5,
124 [snd_soc_dapm_encoder] = 5,
125 [snd_soc_dapm_decoder] = 5,
126 [snd_soc_dapm_switch] = 6,
127 [snd_soc_dapm_mixer_named_ctl] = 6,
128 [snd_soc_dapm_mixer] = 6,
129 [snd_soc_dapm_dac] = 7,
130 [snd_soc_dapm_mic] = 8,
131 [snd_soc_dapm_siggen] = 8,
132 [snd_soc_dapm_input] = 8,
133 [snd_soc_dapm_output] = 8,
134 [snd_soc_dapm_micbias] = 9,
135 [snd_soc_dapm_vmid] = 9,
136 [snd_soc_dapm_mux] = 10,
137 [snd_soc_dapm_demux] = 10,
138 [snd_soc_dapm_aif_in] = 11,
139 [snd_soc_dapm_aif_out] = 11,
140 [snd_soc_dapm_dai_in] = 11,
141 [snd_soc_dapm_dai_out] = 11,
142 [snd_soc_dapm_dai_link] = 12,
143 [snd_soc_dapm_supply] = 13,
144 [snd_soc_dapm_clock_supply] = 14,
145 [snd_soc_dapm_pinctrl] = 14,
146 [snd_soc_dapm_regulator_supply] = 14,
147 [snd_soc_dapm_post] = 15,
150 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
152 if (dapm->card && dapm->card->instantiated)
153 lockdep_assert_held(&dapm->card->dapm_mutex);
156 static void pop_wait(u32 pop_time)
159 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
163 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
171 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
176 vsnprintf(buf, PAGE_SIZE, fmt, args);
177 dev_info(dev, "%s", buf);
183 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
185 return !list_empty(&w->dirty);
188 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
190 dapm_assert_locked(w->dapm);
192 if (!dapm_dirty_widget(w)) {
193 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
195 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
200 * Common implementation for dapm_widget_invalidate_input_paths() and
201 * dapm_widget_invalidate_output_paths(). The function is inlined since the
202 * combined size of the two specialized functions is only marginally larger then
203 * the size of the generic function and at the same time the fast path of the
204 * specialized functions is significantly smaller than the generic function.
206 static __always_inline void dapm_widget_invalidate_paths(
207 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
209 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
210 struct snd_soc_dapm_widget *node;
211 struct snd_soc_dapm_path *p;
214 dapm_assert_locked(w->dapm);
216 if (w->endpoints[dir] == -1)
219 list_add_tail(&w->work_list, &list);
220 w->endpoints[dir] = -1;
222 list_for_each_entry(w, &list, work_list) {
223 snd_soc_dapm_widget_for_each_path(w, dir, p) {
224 if (p->is_supply || p->weak || !p->connect)
226 node = p->node[rdir];
227 if (node->endpoints[dir] != -1) {
228 node->endpoints[dir] = -1;
229 list_add_tail(&node->work_list, &list);
236 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
238 * @w: The widget for which to invalidate the cached number of input paths
240 * Resets the cached number of inputs for the specified widget and all widgets
241 * that can be reached via outcoming paths from the widget.
243 * This function must be called if the number of output paths for a widget might
244 * have changed. E.g. if the source state of a widget changes or a path is added
245 * or activated with the widget as the sink.
247 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
249 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
253 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
255 * @w: The widget for which to invalidate the cached number of output paths
257 * Resets the cached number of outputs for the specified widget and all widgets
258 * that can be reached via incoming paths from the widget.
260 * This function must be called if the number of output paths for a widget might
261 * have changed. E.g. if the sink state of a widget changes or a path is added
262 * or activated with the widget as the source.
264 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
266 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
270 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
271 * for the widgets connected to a path
272 * @p: The path to invalidate
274 * Resets the cached number of inputs for the sink of the path and the cached
275 * number of outputs for the source of the path.
277 * This function must be called when a path is added, removed or the connected
280 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
283 * Weak paths or supply paths do not influence the number of input or
284 * output paths of their neighbors.
286 if (p->weak || p->is_supply)
290 * The number of connected endpoints is the sum of the number of
291 * connected endpoints of all neighbors. If a node with 0 connected
292 * endpoints is either connected or disconnected that sum won't change,
293 * so there is no need to re-check the path.
295 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
296 dapm_widget_invalidate_input_paths(p->sink);
297 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
298 dapm_widget_invalidate_output_paths(p->source);
301 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
303 struct snd_soc_dapm_widget *w;
305 mutex_lock(&card->dapm_mutex);
307 for_each_card_widgets(card, w) {
309 dapm_mark_dirty(w, "Rechecking endpoints");
310 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
311 dapm_widget_invalidate_output_paths(w);
312 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
313 dapm_widget_invalidate_input_paths(w);
317 mutex_unlock(&card->dapm_mutex);
319 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
321 /* create a new dapm widget */
322 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
323 const struct snd_soc_dapm_widget *_widget)
325 struct snd_soc_dapm_widget *w;
327 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
332 * w->name is duplicated in caller, but w->sname isn't.
333 * Duplicate it here if defined
335 if (_widget->sname) {
336 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
345 struct dapm_kcontrol_data {
347 struct snd_soc_dapm_widget *widget;
348 struct list_head paths;
349 struct snd_soc_dapm_widget_list *wlist;
352 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
353 struct snd_kcontrol *kcontrol, const char *ctrl_name)
355 struct dapm_kcontrol_data *data;
356 struct soc_mixer_control *mc;
361 data = kzalloc(sizeof(*data), GFP_KERNEL);
365 INIT_LIST_HEAD(&data->paths);
367 switch (widget->id) {
368 case snd_soc_dapm_switch:
369 case snd_soc_dapm_mixer:
370 case snd_soc_dapm_mixer_named_ctl:
371 mc = (struct soc_mixer_control *)kcontrol->private_value;
373 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
374 dev_warn(widget->dapm->dev,
375 "ASoC: Unsupported stereo autodisable control '%s'\n",
378 if (mc->autodisable) {
379 struct snd_soc_dapm_widget template;
381 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
388 memset(&template, 0, sizeof(template));
389 template.reg = mc->reg;
390 template.mask = (1 << fls(mc->max)) - 1;
391 template.shift = mc->shift;
393 template.off_val = mc->max;
395 template.off_val = 0;
396 template.on_val = template.off_val;
397 template.id = snd_soc_dapm_kcontrol;
398 template.name = name;
400 data->value = template.on_val;
403 snd_soc_dapm_new_control_unlocked(widget->dapm,
406 if (IS_ERR(data->widget)) {
407 ret = PTR_ERR(data->widget);
412 case snd_soc_dapm_demux:
413 case snd_soc_dapm_mux:
414 e = (struct soc_enum *)kcontrol->private_value;
416 if (e->autodisable) {
417 struct snd_soc_dapm_widget template;
419 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
426 memset(&template, 0, sizeof(template));
427 template.reg = e->reg;
428 template.mask = e->mask;
429 template.shift = e->shift_l;
430 template.off_val = snd_soc_enum_item_to_val(e, 0);
431 template.on_val = template.off_val;
432 template.id = snd_soc_dapm_kcontrol;
433 template.name = name;
435 data->value = template.on_val;
437 data->widget = snd_soc_dapm_new_control_unlocked(
438 widget->dapm, &template);
440 if (IS_ERR(data->widget)) {
441 ret = PTR_ERR(data->widget);
445 snd_soc_dapm_add_path(widget->dapm, data->widget,
447 } else if (e->reg != SND_SOC_NOPM) {
448 data->value = soc_dapm_read(widget->dapm, e->reg) &
449 (e->mask << e->shift_l);
456 kcontrol->private_data = data;
465 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
467 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
469 list_del(&data->paths);
474 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
475 const struct snd_kcontrol *kcontrol)
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
482 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
483 struct snd_soc_dapm_widget *widget)
485 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
486 struct snd_soc_dapm_widget_list *new_wlist;
490 n = data->wlist->num_widgets + 1;
494 new_wlist = krealloc(data->wlist,
495 struct_size(new_wlist, widgets, n),
500 new_wlist->widgets[n - 1] = widget;
501 new_wlist->num_widgets = n;
503 data->wlist = new_wlist;
508 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
509 struct snd_soc_dapm_path *path)
511 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
513 list_add_tail(&path->list_kcontrol, &data->paths);
516 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
518 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
523 return data->widget->power;
526 static struct list_head *dapm_kcontrol_get_path_list(
527 const struct snd_kcontrol *kcontrol)
529 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
534 #define dapm_kcontrol_for_each_path(path, kcontrol) \
535 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
538 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
540 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
544 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
546 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
549 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
551 if (data->value == value)
555 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
556 case snd_soc_dapm_switch:
557 case snd_soc_dapm_mixer:
558 case snd_soc_dapm_mixer_named_ctl:
559 data->widget->on_val = value & data->widget->mask;
561 case snd_soc_dapm_demux:
562 case snd_soc_dapm_mux:
563 data->widget->on_val = value >> data->widget->shift;
566 data->widget->on_val = value;
577 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
579 * @kcontrol: The kcontrol
581 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
582 struct snd_kcontrol *kcontrol)
584 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
586 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
589 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
591 * @kcontrol: The kcontrol
593 * Note: This function must only be used on kcontrols that are known to have
594 * been registered for a CODEC. Otherwise the behaviour is undefined.
596 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
597 struct snd_kcontrol *kcontrol)
599 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
601 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
603 static void dapm_reset(struct snd_soc_card *card)
605 struct snd_soc_dapm_widget *w;
607 lockdep_assert_held(&card->dapm_mutex);
609 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
611 for_each_card_widgets(card, w) {
612 w->new_power = w->power;
613 w->power_checked = false;
617 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
619 if (!dapm->component)
621 return dapm->component->name_prefix;
624 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
626 if (!dapm->component)
628 return snd_soc_component_read(dapm->component, reg);
631 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
632 int reg, unsigned int mask, unsigned int value)
634 if (!dapm->component)
636 return snd_soc_component_update_bits(dapm->component, reg,
640 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
641 int reg, unsigned int mask, unsigned int value)
643 if (!dapm->component)
645 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
648 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
651 snd_soc_component_async_complete(dapm->component);
654 static struct snd_soc_dapm_widget *
655 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
657 struct snd_soc_dapm_widget *w = wcache->widget;
660 struct list_head *wlist = &w->dapm->card->widgets;
664 list_for_each_entry_from(w, wlist, list) {
665 if (!strcmp(name, w->name))
676 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
677 struct snd_soc_dapm_widget *w)
683 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
684 * @dapm: The DAPM context for which to set the level
685 * @level: The level to set
687 * Forces the DAPM bias level to a specific state. It will call the bias level
688 * callback of DAPM context with the specified level. This will even happen if
689 * the context is already at the same level. Furthermore it will not go through
690 * the normal bias level sequencing, meaning any intermediate states between the
691 * current and the target state will not be entered.
693 * Note that the change in bias level is only temporary and the next time
694 * snd_soc_dapm_sync() is called the state will be set to the level as
695 * determined by the DAPM core. The function is mainly intended to be used to
696 * used during probe or resume from suspend to power up the device so
697 * initialization can be done, before the DAPM core takes over.
699 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
700 enum snd_soc_bias_level level)
705 ret = snd_soc_component_set_bias_level(dapm->component, level);
708 dapm->bias_level = level;
712 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
715 * snd_soc_dapm_set_bias_level - set the bias level for the system
716 * @dapm: DAPM context
717 * @level: level to configure
719 * Configure the bias (power) levels for the SoC audio device.
721 * Returns 0 for success else error.
723 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
724 enum snd_soc_bias_level level)
726 struct snd_soc_card *card = dapm->card;
729 trace_snd_soc_bias_level_start(card, level);
731 ret = snd_soc_card_set_bias_level(card, dapm, level);
735 if (!card || dapm != &card->dapm)
736 ret = snd_soc_dapm_force_bias_level(dapm, level);
741 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
743 trace_snd_soc_bias_level_done(card, level);
748 /* connect mux widget to its interconnecting audio paths */
749 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
750 struct snd_soc_dapm_path *path, const char *control_name,
751 struct snd_soc_dapm_widget *w)
753 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
754 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
758 if (e->reg != SND_SOC_NOPM) {
760 val = soc_dapm_read(dapm, e->reg);
761 val = (val >> e->shift_l) & e->mask;
762 item = snd_soc_enum_val_to_item(e, val);
764 /* since a virtual mux has no backing registers to
765 * decide which path to connect, it will try to match
766 * with the first enumeration. This is to ensure
767 * that the default mux choice (the first) will be
768 * correctly powered up during initialization.
773 i = match_string(e->texts, e->items, control_name);
777 path->name = e->texts[i];
778 path->connect = (i == item);
783 /* set up initial codec paths */
784 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
787 struct soc_mixer_control *mc = (struct soc_mixer_control *)
788 p->sink->kcontrol_news[i].private_value;
789 unsigned int reg = mc->reg;
790 unsigned int invert = mc->invert;
792 if (reg != SND_SOC_NOPM) {
793 unsigned int shift = mc->shift;
794 unsigned int max = mc->max;
795 unsigned int mask = (1 << fls(max)) - 1;
796 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
799 * The nth_path argument allows this function to know
800 * which path of a kcontrol it is setting the initial
801 * status for. Ideally this would support any number
802 * of paths and channels. But since kcontrols only come
803 * in mono and stereo variants, we are limited to 2
806 * The following code assumes for stereo controls the
807 * first path is the left channel, and all remaining
808 * paths are the right channel.
810 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
812 val = soc_dapm_read(p->sink->dapm, mc->rreg);
813 val = (val >> mc->rshift) & mask;
815 val = (val >> shift) & mask;
821 /* since a virtual mixer has no backing registers to
822 * decide which path to connect, it will try to match
823 * with initial state. This is to ensure
824 * that the default mixer choice will be
825 * correctly powered up during initialization.
831 /* connect mixer widget to its interconnecting audio paths */
832 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
833 struct snd_soc_dapm_path *path, const char *control_name)
837 /* search for mixer kcontrol */
838 for (i = 0; i < path->sink->num_kcontrols; i++) {
839 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
840 path->name = path->sink->kcontrol_news[i].name;
841 dapm_set_mixer_path_status(path, i, nth_path++);
848 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
849 struct snd_soc_dapm_widget *kcontrolw,
850 const struct snd_kcontrol_new *kcontrol_new,
851 struct snd_kcontrol **kcontrol)
853 struct snd_soc_dapm_widget *w;
858 for_each_card_widgets(dapm->card, w) {
859 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
861 for (i = 0; i < w->num_kcontrols; i++) {
862 if (&w->kcontrol_news[i] == kcontrol_new) {
864 *kcontrol = w->kcontrols[i];
874 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
875 * create it. Either way, add the widget into the control's widget list
877 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
880 struct snd_soc_dapm_context *dapm = w->dapm;
881 struct snd_card *card = dapm->card->snd_card;
885 struct snd_kcontrol *kcontrol;
886 bool wname_in_long_name, kcname_in_long_name;
887 char *long_name = NULL;
891 prefix = soc_dapm_prefix(dapm);
893 prefix_len = strlen(prefix) + 1;
897 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
902 wname_in_long_name = false;
903 kcname_in_long_name = true;
906 case snd_soc_dapm_switch:
907 case snd_soc_dapm_mixer:
908 case snd_soc_dapm_pga:
909 case snd_soc_dapm_effect:
910 case snd_soc_dapm_out_drv:
911 wname_in_long_name = true;
912 kcname_in_long_name = true;
914 case snd_soc_dapm_mixer_named_ctl:
915 wname_in_long_name = false;
916 kcname_in_long_name = true;
918 case snd_soc_dapm_demux:
919 case snd_soc_dapm_mux:
920 wname_in_long_name = true;
921 kcname_in_long_name = false;
928 if (wname_in_long_name && kcname_in_long_name) {
930 * The control will get a prefix from the control
931 * creation process but we're also using the same
932 * prefix for widgets so cut the prefix off the
933 * front of the widget name.
935 long_name = kasprintf(GFP_KERNEL, "%s %s",
936 w->name + prefix_len,
937 w->kcontrol_news[kci].name);
938 if (long_name == NULL)
942 } else if (wname_in_long_name) {
944 name = w->name + prefix_len;
947 name = w->kcontrol_news[kci].name;
950 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
957 kcontrol->private_free = dapm_kcontrol_free;
959 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
961 snd_ctl_free_one(kcontrol);
965 ret = snd_ctl_add(card, kcontrol);
968 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
974 ret = dapm_kcontrol_add_widget(kcontrol, w);
976 w->kcontrols[kci] = kcontrol;
984 /* create new dapm mixer control */
985 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
988 struct snd_soc_dapm_path *path;
989 struct dapm_kcontrol_data *data;
992 for (i = 0; i < w->num_kcontrols; i++) {
994 snd_soc_dapm_widget_for_each_source_path(w, path) {
995 /* mixer/mux paths name must match control name */
996 if (path->name != (char *)w->kcontrol_news[i].name)
999 if (!w->kcontrols[i]) {
1000 ret = dapm_create_or_share_kcontrol(w, i);
1005 dapm_kcontrol_add_path(w->kcontrols[i], path);
1007 data = snd_kcontrol_chip(w->kcontrols[i]);
1009 snd_soc_dapm_add_path(data->widget->dapm,
1019 /* create new dapm mux control */
1020 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1022 struct snd_soc_dapm_context *dapm = w->dapm;
1023 enum snd_soc_dapm_direction dir;
1024 struct snd_soc_dapm_path *path;
1029 case snd_soc_dapm_mux:
1030 dir = SND_SOC_DAPM_DIR_OUT;
1033 case snd_soc_dapm_demux:
1034 dir = SND_SOC_DAPM_DIR_IN;
1041 if (w->num_kcontrols != 1) {
1043 "ASoC: %s %s has incorrect number of controls\n", type,
1048 if (list_empty(&w->edges[dir])) {
1049 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1053 ret = dapm_create_or_share_kcontrol(w, 0);
1057 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1059 dapm_kcontrol_add_path(w->kcontrols[0], path);
1065 /* create new dapm volume control */
1066 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1070 for (i = 0; i < w->num_kcontrols; i++) {
1071 int ret = dapm_create_or_share_kcontrol(w, i);
1079 /* create new dapm dai link control */
1080 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1083 struct snd_soc_pcm_runtime *rtd = w->priv;
1085 /* create control for links with > 1 config */
1086 if (rtd->dai_link->num_params <= 1)
1090 for (i = 0; i < w->num_kcontrols; i++) {
1091 struct snd_soc_dapm_context *dapm = w->dapm;
1092 struct snd_card *card = dapm->card->snd_card;
1093 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1095 int ret = snd_ctl_add(card, kcontrol);
1099 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1100 w->name, w->kcontrol_news[i].name, ret);
1103 kcontrol->private_data = w;
1104 w->kcontrols[i] = kcontrol;
1110 /* We implement power down on suspend by checking the power state of
1111 * the ALSA card - when we are suspending the ALSA state for the card
1114 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1116 int level = snd_power_get_state(widget->dapm->card->snd_card);
1119 case SNDRV_CTL_POWER_D3hot:
1120 case SNDRV_CTL_POWER_D3cold:
1121 if (widget->ignore_suspend)
1122 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1124 return widget->ignore_suspend;
1130 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1135 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1136 struct list_head *widgets)
1138 struct snd_soc_dapm_widget *w;
1139 struct list_head *it;
1140 unsigned int size = 0;
1143 list_for_each(it, widgets)
1146 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1150 list_for_each_entry(w, widgets, work_list)
1151 (*list)->widgets[i++] = w;
1153 (*list)->num_widgets = i;
1159 * Recursively reset the cached number of inputs or outputs for the specified
1160 * widget and all widgets that can be reached via incoming or outcoming paths
1163 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1164 enum snd_soc_dapm_direction dir)
1166 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1167 struct snd_soc_dapm_path *path;
1169 widget->endpoints[dir] = -1;
1171 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1172 if (path->weak || path->is_supply)
1178 if (path->connect) {
1180 invalidate_paths_ep(path->node[dir], dir);
1187 * Common implementation for is_connected_output_ep() and
1188 * is_connected_input_ep(). The function is inlined since the combined size of
1189 * the two specialized functions is only marginally larger then the size of the
1190 * generic function and at the same time the fast path of the specialized
1191 * functions is significantly smaller than the generic function.
1193 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1194 struct list_head *list, enum snd_soc_dapm_direction dir,
1195 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction)),
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1199 enum snd_soc_dapm_direction))
1201 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1202 struct snd_soc_dapm_path *path;
1205 if (widget->endpoints[dir] >= 0)
1206 return widget->endpoints[dir];
1208 DAPM_UPDATE_STAT(widget, path_checks);
1210 /* do we need to add this widget to the list ? */
1212 list_add_tail(&widget->work_list, list);
1214 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1216 custom_stop_condition = NULL;
1219 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1220 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1221 return widget->endpoints[dir];
1224 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1225 DAPM_UPDATE_STAT(widget, neighbour_checks);
1227 if (path->weak || path->is_supply)
1233 trace_snd_soc_dapm_path(widget, dir, path);
1235 if (path->connect) {
1237 con += fn(path->node[dir], list, custom_stop_condition);
1242 widget->endpoints[dir] = con;
1248 * Recursively check for a completed path to an active or physically connected
1249 * output widget. Returns number of complete paths.
1251 * Optionally, can be supplied with a function acting as a stopping condition.
1252 * This function takes the dapm widget currently being examined and the walk
1253 * direction as an arguments, it should return true if widgets from that point
1254 * in the graph onwards should not be added to the widget list.
1256 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1257 struct list_head *list,
1258 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1259 enum snd_soc_dapm_direction))
1261 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1262 is_connected_output_ep, custom_stop_condition);
1266 * Recursively check for a completed path to an active or physically connected
1267 * input widget. Returns number of complete paths.
1269 * Optionally, can be supplied with a function acting as a stopping condition.
1270 * This function takes the dapm widget currently being examined and the walk
1271 * direction as an arguments, it should return true if the walk should be
1272 * stopped and false otherwise.
1274 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1275 struct list_head *list,
1276 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1277 enum snd_soc_dapm_direction))
1279 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1280 is_connected_input_ep, custom_stop_condition);
1284 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1285 * @dai: the soc DAI.
1286 * @stream: stream direction.
1287 * @list: list of active widgets for this stream.
1288 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1289 * walk based on custom logic.
1291 * Queries DAPM graph as to whether a valid audio stream path exists for
1292 * the initial stream specified by name. This takes into account
1293 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1295 * Optionally, can be supplied with a function acting as a stopping condition.
1296 * This function takes the dapm widget currently being examined and the walk
1297 * direction as an arguments, it should return true if the walk should be
1298 * stopped and false otherwise.
1300 * Returns the number of valid paths or negative error.
1302 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1303 struct snd_soc_dapm_widget_list **list,
1304 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1305 enum snd_soc_dapm_direction))
1307 struct snd_soc_card *card = dai->component->card;
1308 struct snd_soc_dapm_widget *w;
1313 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1315 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1316 w = dai->playback_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1318 paths = is_connected_output_ep(w, &widgets,
1319 custom_stop_condition);
1321 w = dai->capture_widget;
1322 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1323 paths = is_connected_input_ep(w, &widgets,
1324 custom_stop_condition);
1327 /* Drop starting point */
1328 list_del(widgets.next);
1330 ret = dapm_widget_list_create(list, &widgets);
1334 trace_snd_soc_dapm_connected(paths, stream);
1335 mutex_unlock(&card->dapm_mutex);
1340 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1342 dapm_widget_list_free(list);
1346 * Handler for regulator supply widget.
1348 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1349 struct snd_kcontrol *kcontrol, int event)
1353 soc_dapm_async_complete(w->dapm);
1355 if (SND_SOC_DAPM_EVENT_ON(event)) {
1356 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1357 ret = regulator_allow_bypass(w->regulator, false);
1359 dev_warn(w->dapm->dev,
1360 "ASoC: Failed to unbypass %s: %d\n",
1364 return regulator_enable(w->regulator);
1366 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1367 ret = regulator_allow_bypass(w->regulator, true);
1369 dev_warn(w->dapm->dev,
1370 "ASoC: Failed to bypass %s: %d\n",
1374 return regulator_disable_deferred(w->regulator, w->shift);
1377 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1380 * Handler for pinctrl widget.
1382 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1383 struct snd_kcontrol *kcontrol, int event)
1385 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1386 struct pinctrl *p = w->pinctrl;
1387 struct pinctrl_state *s;
1392 if (SND_SOC_DAPM_EVENT_ON(event))
1393 s = pinctrl_lookup_state(p, priv->active_state);
1395 s = pinctrl_lookup_state(p, priv->sleep_state);
1400 return pinctrl_select_state(p, s);
1402 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1405 * Handler for clock supply widget.
1407 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1408 struct snd_kcontrol *kcontrol, int event)
1413 soc_dapm_async_complete(w->dapm);
1415 if (SND_SOC_DAPM_EVENT_ON(event)) {
1416 return clk_prepare_enable(w->clk);
1418 clk_disable_unprepare(w->clk);
1424 EXPORT_SYMBOL_GPL(dapm_clock_event);
1426 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1428 if (w->power_checked)
1429 return w->new_power;
1434 w->new_power = w->power_check(w);
1436 w->power_checked = true;
1438 return w->new_power;
1441 /* Generic check to see if a widget should be powered. */
1442 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1446 DAPM_UPDATE_STAT(w, power_checks);
1448 in = is_connected_input_ep(w, NULL, NULL);
1449 out = is_connected_output_ep(w, NULL, NULL);
1450 return out != 0 && in != 0;
1453 /* Check to see if a power supply is needed */
1454 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1456 struct snd_soc_dapm_path *path;
1458 DAPM_UPDATE_STAT(w, power_checks);
1460 /* Check if one of our outputs is connected */
1461 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1462 DAPM_UPDATE_STAT(w, neighbour_checks);
1467 if (path->connected &&
1468 !path->connected(path->source, path->sink))
1471 if (dapm_widget_power_check(path->sink))
1478 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1480 return w->connected;
1483 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1484 struct snd_soc_dapm_widget *b,
1489 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1490 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1495 sort = dapm_down_seq;
1497 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1498 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1500 if (sort[a->id] != sort[b->id])
1501 return sort[a->id] - sort[b->id];
1502 if (a->subseq != b->subseq) {
1504 return a->subseq - b->subseq;
1506 return b->subseq - a->subseq;
1508 if (a->reg != b->reg)
1509 return a->reg - b->reg;
1510 if (a->dapm != b->dapm)
1511 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1516 /* Insert a widget in order into a DAPM power sequence. */
1517 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1518 struct list_head *list,
1521 struct snd_soc_dapm_widget *w;
1523 list_for_each_entry(w, list, power_list)
1524 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1525 list_add_tail(&new_widget->power_list, &w->power_list);
1529 list_add_tail(&new_widget->power_list, list);
1532 static void dapm_seq_check_event(struct snd_soc_card *card,
1533 struct snd_soc_dapm_widget *w, int event)
1535 const char *ev_name;
1539 case SND_SOC_DAPM_PRE_PMU:
1540 ev_name = "PRE_PMU";
1543 case SND_SOC_DAPM_POST_PMU:
1544 ev_name = "POST_PMU";
1547 case SND_SOC_DAPM_PRE_PMD:
1548 ev_name = "PRE_PMD";
1551 case SND_SOC_DAPM_POST_PMD:
1552 ev_name = "POST_PMD";
1555 case SND_SOC_DAPM_WILL_PMU:
1556 ev_name = "WILL_PMU";
1559 case SND_SOC_DAPM_WILL_PMD:
1560 ev_name = "WILL_PMD";
1564 WARN(1, "Unknown event %d\n", event);
1568 if (w->new_power != power)
1571 if (w->event && (w->event_flags & event)) {
1574 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1576 soc_dapm_async_complete(w->dapm);
1577 trace_snd_soc_dapm_widget_event_start(w, event);
1578 ret = w->event(w, NULL, event);
1579 trace_snd_soc_dapm_widget_event_done(w, event);
1581 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1582 ev_name, w->name, ret);
1586 /* Apply the coalesced changes from a DAPM sequence */
1587 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1588 struct list_head *pending)
1590 struct snd_soc_dapm_context *dapm;
1591 struct snd_soc_dapm_widget *w;
1593 unsigned int value = 0;
1594 unsigned int mask = 0;
1596 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1600 list_for_each_entry(w, pending, power_list) {
1601 WARN_ON(reg != w->reg || dapm != w->dapm);
1602 w->power = w->new_power;
1604 mask |= w->mask << w->shift;
1606 value |= w->on_val << w->shift;
1608 value |= w->off_val << w->shift;
1610 pop_dbg(dapm->dev, card->pop_time,
1611 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1612 w->name, reg, value, mask);
1614 /* Check for events */
1615 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1616 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1620 /* Any widget will do, they should all be updating the
1624 pop_dbg(dapm->dev, card->pop_time,
1625 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1626 value, mask, reg, card->pop_time);
1627 pop_wait(card->pop_time);
1628 soc_dapm_update_bits(dapm, reg, mask, value);
1631 list_for_each_entry(w, pending, power_list) {
1632 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1633 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1637 /* Apply a DAPM power sequence.
1639 * We walk over a pre-sorted list of widgets to apply power to. In
1640 * order to minimise the number of writes to the device required
1641 * multiple widgets will be updated in a single write where possible.
1642 * Currently anything that requires more than a single write is not
1645 static void dapm_seq_run(struct snd_soc_card *card,
1646 struct list_head *list, int event, bool power_up)
1648 struct snd_soc_dapm_widget *w, *n;
1649 struct snd_soc_dapm_context *d;
1652 int cur_subseq = -1;
1653 int cur_reg = SND_SOC_NOPM;
1654 struct snd_soc_dapm_context *cur_dapm = NULL;
1661 sort = dapm_down_seq;
1663 list_for_each_entry_safe(w, n, list, power_list) {
1666 /* Do we need to apply any queued changes? */
1667 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1668 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1669 if (!list_empty(&pending))
1670 dapm_seq_run_coalesced(card, &pending);
1672 if (cur_dapm && cur_dapm->component) {
1673 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1674 if (sort[i] == cur_sort)
1675 snd_soc_component_seq_notifier(
1676 cur_dapm->component,
1680 if (cur_dapm && w->dapm != cur_dapm)
1681 soc_dapm_async_complete(cur_dapm);
1683 INIT_LIST_HEAD(&pending);
1685 cur_subseq = INT_MIN;
1686 cur_reg = SND_SOC_NOPM;
1691 case snd_soc_dapm_pre:
1695 if (event == SND_SOC_DAPM_STREAM_START)
1697 NULL, SND_SOC_DAPM_PRE_PMU);
1698 else if (event == SND_SOC_DAPM_STREAM_STOP)
1700 NULL, SND_SOC_DAPM_PRE_PMD);
1703 case snd_soc_dapm_post:
1707 if (event == SND_SOC_DAPM_STREAM_START)
1709 NULL, SND_SOC_DAPM_POST_PMU);
1710 else if (event == SND_SOC_DAPM_STREAM_STOP)
1712 NULL, SND_SOC_DAPM_POST_PMD);
1716 /* Queue it up for application */
1717 cur_sort = sort[w->id];
1718 cur_subseq = w->subseq;
1721 list_move(&w->power_list, &pending);
1726 dev_err(w->dapm->dev,
1727 "ASoC: Failed to apply widget power: %d\n", ret);
1730 if (!list_empty(&pending))
1731 dapm_seq_run_coalesced(card, &pending);
1733 if (cur_dapm && cur_dapm->component) {
1734 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1735 if (sort[i] == cur_sort)
1736 snd_soc_component_seq_notifier(
1737 cur_dapm->component,
1741 for_each_card_dapms(card, d)
1742 soc_dapm_async_complete(d);
1745 static void dapm_widget_update(struct snd_soc_card *card)
1747 struct snd_soc_dapm_update *update = card->update;
1748 struct snd_soc_dapm_widget_list *wlist;
1749 struct snd_soc_dapm_widget *w = NULL;
1753 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1756 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1758 for_each_dapm_widgets(wlist, wi, w) {
1759 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1760 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1762 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1770 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1773 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1776 if (update->has_second_set) {
1777 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1778 update->mask2, update->val2);
1780 dev_err(w->dapm->dev,
1781 "ASoC: %s DAPM update failed: %d\n",
1785 for_each_dapm_widgets(wlist, wi, w) {
1786 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1787 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1789 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1795 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1796 * they're changing state.
1798 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1800 struct snd_soc_dapm_context *d = data;
1803 /* If we're off and we're not supposed to go into STANDBY */
1804 if (d->bias_level == SND_SOC_BIAS_OFF &&
1805 d->target_bias_level != SND_SOC_BIAS_OFF) {
1806 if (d->dev && cookie)
1807 pm_runtime_get_sync(d->dev);
1809 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1812 "ASoC: Failed to turn on bias: %d\n", ret);
1815 /* Prepare for a transition to ON or away from ON */
1816 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1817 d->bias_level != SND_SOC_BIAS_ON) ||
1818 (d->target_bias_level != SND_SOC_BIAS_ON &&
1819 d->bias_level == SND_SOC_BIAS_ON)) {
1820 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1823 "ASoC: Failed to prepare bias: %d\n", ret);
1827 /* Async callback run prior to DAPM sequences - brings to their final
1830 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1832 struct snd_soc_dapm_context *d = data;
1835 /* If we just powered the last thing off drop to standby bias */
1836 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1837 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1838 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1839 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1841 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1845 /* If we're in standby and can support bias off then do that */
1846 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1847 d->target_bias_level == SND_SOC_BIAS_OFF) {
1848 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1850 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1853 if (d->dev && cookie)
1854 pm_runtime_put(d->dev);
1857 /* If we just powered up then move to active bias */
1858 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1859 d->target_bias_level == SND_SOC_BIAS_ON) {
1860 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1862 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1867 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1868 bool power, bool connect)
1870 /* If a connection is being made or broken then that update
1871 * will have marked the peer dirty, otherwise the widgets are
1872 * not connected and this update has no impact. */
1876 /* If the peer is already in the state we're moving to then we
1877 * won't have an impact on it. */
1878 if (power != peer->power)
1879 dapm_mark_dirty(peer, "peer state change");
1882 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1883 struct list_head *up_list,
1884 struct list_head *down_list)
1886 struct snd_soc_dapm_path *path;
1888 if (w->power == power)
1891 trace_snd_soc_dapm_widget_power(w, power);
1893 /* If we changed our power state perhaps our neigbours changed
1896 snd_soc_dapm_widget_for_each_source_path(w, path)
1897 dapm_widget_set_peer_power(path->source, power, path->connect);
1899 /* Supplies can't affect their outputs, only their inputs */
1900 if (!w->is_supply) {
1901 snd_soc_dapm_widget_for_each_sink_path(w, path)
1902 dapm_widget_set_peer_power(path->sink, power,
1907 dapm_seq_insert(w, up_list, true);
1909 dapm_seq_insert(w, down_list, false);
1912 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1913 struct list_head *up_list,
1914 struct list_head *down_list)
1919 case snd_soc_dapm_pre:
1920 dapm_seq_insert(w, down_list, false);
1922 case snd_soc_dapm_post:
1923 dapm_seq_insert(w, up_list, true);
1927 power = dapm_widget_power_check(w);
1929 dapm_widget_set_power(w, power, up_list, down_list);
1934 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1936 if (dapm->idle_bias_off)
1939 switch (snd_power_get_state(dapm->card->snd_card)) {
1940 case SNDRV_CTL_POWER_D3hot:
1941 case SNDRV_CTL_POWER_D3cold:
1942 return dapm->suspend_bias_off;
1951 * Scan each dapm widget for complete audio path.
1952 * A complete path is a route that has valid endpoints i.e.:-
1954 * o DAC to output pin.
1955 * o Input pin to ADC.
1956 * o Input pin to Output pin (bypass, sidetone)
1957 * o DAC to ADC (loopback).
1959 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1961 struct snd_soc_dapm_widget *w;
1962 struct snd_soc_dapm_context *d;
1964 LIST_HEAD(down_list);
1965 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1966 enum snd_soc_bias_level bias;
1969 lockdep_assert_held(&card->dapm_mutex);
1971 trace_snd_soc_dapm_start(card);
1973 for_each_card_dapms(card, d) {
1974 if (dapm_idle_bias_off(d))
1975 d->target_bias_level = SND_SOC_BIAS_OFF;
1977 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1982 /* Check which widgets we need to power and store them in
1983 * lists indicating if they should be powered up or down. We
1984 * only check widgets that have been flagged as dirty but note
1985 * that new widgets may be added to the dirty list while we
1988 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1989 dapm_power_one_widget(w, &up_list, &down_list);
1992 for_each_card_widgets(card, w) {
1994 case snd_soc_dapm_pre:
1995 case snd_soc_dapm_post:
1996 /* These widgets always need to be powered */
1999 list_del_init(&w->dirty);
2006 /* Supplies and micbiases only bring the
2007 * context up to STANDBY as unless something
2008 * else is active and passing audio they
2009 * generally don't require full power. Signal
2010 * generators are virtual pins and have no
2011 * power impact themselves.
2014 case snd_soc_dapm_siggen:
2015 case snd_soc_dapm_vmid:
2017 case snd_soc_dapm_supply:
2018 case snd_soc_dapm_regulator_supply:
2019 case snd_soc_dapm_pinctrl:
2020 case snd_soc_dapm_clock_supply:
2021 case snd_soc_dapm_micbias:
2022 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2023 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2026 d->target_bias_level = SND_SOC_BIAS_ON;
2033 /* Force all contexts in the card to the same bias state if
2034 * they're not ground referenced.
2036 bias = SND_SOC_BIAS_OFF;
2037 for_each_card_dapms(card, d)
2038 if (d->target_bias_level > bias)
2039 bias = d->target_bias_level;
2040 for_each_card_dapms(card, d)
2041 if (!dapm_idle_bias_off(d))
2042 d->target_bias_level = bias;
2044 trace_snd_soc_dapm_walk_done(card);
2046 /* Run card bias changes at first */
2047 dapm_pre_sequence_async(&card->dapm, 0);
2048 /* Run other bias changes in parallel */
2049 for_each_card_dapms(card, d) {
2050 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2051 async_schedule_domain(dapm_pre_sequence_async, d,
2054 async_synchronize_full_domain(&async_domain);
2056 list_for_each_entry(w, &down_list, power_list) {
2057 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2060 list_for_each_entry(w, &up_list, power_list) {
2061 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2064 /* Power down widgets first; try to avoid amplifying pops. */
2065 dapm_seq_run(card, &down_list, event, false);
2067 dapm_widget_update(card);
2070 dapm_seq_run(card, &up_list, event, true);
2072 /* Run all the bias changes in parallel */
2073 for_each_card_dapms(card, d) {
2074 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2075 async_schedule_domain(dapm_post_sequence_async, d,
2078 async_synchronize_full_domain(&async_domain);
2079 /* Run card bias changes at last */
2080 dapm_post_sequence_async(&card->dapm, 0);
2082 /* do we need to notify any clients that DAPM event is complete */
2083 for_each_card_dapms(card, d) {
2087 ret = snd_soc_component_stream_event(d->component, event);
2092 pop_dbg(card->dev, card->pop_time,
2093 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2094 pop_wait(card->pop_time);
2096 trace_snd_soc_dapm_done(card);
2101 #ifdef CONFIG_DEBUG_FS
2102 static ssize_t dapm_widget_power_read_file(struct file *file,
2103 char __user *user_buf,
2104 size_t count, loff_t *ppos)
2106 struct snd_soc_dapm_widget *w = file->private_data;
2107 struct snd_soc_card *card = w->dapm->card;
2108 enum snd_soc_dapm_direction dir, rdir;
2112 struct snd_soc_dapm_path *p = NULL;
2114 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2118 mutex_lock(&card->dapm_mutex);
2120 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2125 in = is_connected_input_ep(w, NULL, NULL);
2126 out = is_connected_output_ep(w, NULL, NULL);
2129 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2130 w->name, w->power ? "On" : "Off",
2131 w->force ? " (forced)" : "", in, out);
2134 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2135 " - R%d(0x%x) mask 0x%x",
2136 w->reg, w->reg, w->mask << w->shift);
2138 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2141 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2143 w->active ? "active" : "inactive");
2145 snd_soc_dapm_for_each_direction(dir) {
2146 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2147 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2148 if (p->connected && !p->connected(p->source, p->sink))
2154 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2155 " %s \"%s\" \"%s\"\n",
2156 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2157 p->name ? p->name : "static",
2158 p->node[rdir]->name);
2162 mutex_unlock(&card->dapm_mutex);
2164 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2170 static const struct file_operations dapm_widget_power_fops = {
2171 .open = simple_open,
2172 .read = dapm_widget_power_read_file,
2173 .llseek = default_llseek,
2176 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2177 size_t count, loff_t *ppos)
2179 struct snd_soc_dapm_context *dapm = file->private_data;
2182 switch (dapm->bias_level) {
2183 case SND_SOC_BIAS_ON:
2186 case SND_SOC_BIAS_PREPARE:
2187 level = "Prepare\n";
2189 case SND_SOC_BIAS_STANDBY:
2190 level = "Standby\n";
2192 case SND_SOC_BIAS_OFF:
2196 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2197 level = "Unknown\n";
2201 return simple_read_from_buffer(user_buf, count, ppos, level,
2205 static const struct file_operations dapm_bias_fops = {
2206 .open = simple_open,
2207 .read = dapm_bias_read_file,
2208 .llseek = default_llseek,
2211 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2212 struct dentry *parent)
2214 if (!parent || IS_ERR(parent))
2217 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2219 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2223 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2225 struct snd_soc_dapm_context *dapm = w->dapm;
2227 if (!dapm->debugfs_dapm || !w->name)
2230 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2231 &dapm_widget_power_fops);
2234 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2236 debugfs_remove_recursive(dapm->debugfs_dapm);
2237 dapm->debugfs_dapm = NULL;
2241 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2242 struct dentry *parent)
2246 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2250 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2257 * soc_dapm_connect_path() - Connects or disconnects a path
2258 * @path: The path to update
2259 * @connect: The new connect state of the path. True if the path is connected,
2260 * false if it is disconnected.
2261 * @reason: The reason why the path changed (for debugging only)
2263 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2264 bool connect, const char *reason)
2266 if (path->connect == connect)
2269 path->connect = connect;
2270 dapm_mark_dirty(path->source, reason);
2271 dapm_mark_dirty(path->sink, reason);
2272 dapm_path_invalidate(path);
2275 /* test and update the power status of a mux widget */
2276 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2277 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2279 struct snd_soc_dapm_path *path;
2283 lockdep_assert_held(&card->dapm_mutex);
2285 /* find dapm widget path assoc with kcontrol */
2286 dapm_kcontrol_for_each_path(path, kcontrol) {
2288 /* we now need to match the string in the enum to the path */
2289 if (e && !(strcmp(path->name, e->texts[mux])))
2294 soc_dapm_connect_path(path, connect, "mux update");
2298 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2303 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2304 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2305 struct snd_soc_dapm_update *update)
2307 struct snd_soc_card *card = dapm->card;
2310 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2311 card->update = update;
2312 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2313 card->update = NULL;
2314 mutex_unlock(&card->dapm_mutex);
2316 snd_soc_dpcm_runtime_update(card);
2319 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2321 /* test and update the power status of a mixer or switch widget */
2322 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2323 struct snd_kcontrol *kcontrol,
2324 int connect, int rconnect)
2326 struct snd_soc_dapm_path *path;
2329 lockdep_assert_held(&card->dapm_mutex);
2331 /* find dapm widget path assoc with kcontrol */
2332 dapm_kcontrol_for_each_path(path, kcontrol) {
2334 * Ideally this function should support any number of
2335 * paths and channels. But since kcontrols only come
2336 * in mono and stereo variants, we are limited to 2
2339 * The following code assumes for stereo controls the
2340 * first path (when 'found == 0') is the left channel,
2341 * and all remaining paths (when 'found == 1') are the
2344 * A stereo control is signified by a valid 'rconnect'
2345 * value, either 0 for unconnected, or >= 0 for connected.
2346 * This is chosen instead of using snd_soc_volsw_is_stereo,
2347 * so that the behavior of snd_soc_dapm_mixer_update_power
2348 * doesn't change even when the kcontrol passed in is
2351 * It passes 'connect' as the path connect status for
2352 * the left channel, and 'rconnect' for the right
2355 if (found && rconnect >= 0)
2356 soc_dapm_connect_path(path, rconnect, "mixer update");
2358 soc_dapm_connect_path(path, connect, "mixer update");
2363 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2368 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2369 struct snd_kcontrol *kcontrol, int connect,
2370 struct snd_soc_dapm_update *update)
2372 struct snd_soc_card *card = dapm->card;
2375 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2376 card->update = update;
2377 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2378 card->update = NULL;
2379 mutex_unlock(&card->dapm_mutex);
2381 snd_soc_dpcm_runtime_update(card);
2384 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2386 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2389 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2390 struct snd_soc_dapm_widget *w;
2392 char *state = "not set";
2394 /* card won't be set for the dummy component, as a spot fix
2395 * we're checking for that case specifically here but in future
2396 * we will ensure that the dummy component looks like others.
2401 for_each_card_widgets(cmpnt->card, w) {
2402 if (w->dapm != dapm)
2405 /* only display widgets that burn power */
2407 case snd_soc_dapm_hp:
2408 case snd_soc_dapm_mic:
2409 case snd_soc_dapm_spk:
2410 case snd_soc_dapm_line:
2411 case snd_soc_dapm_micbias:
2412 case snd_soc_dapm_dac:
2413 case snd_soc_dapm_adc:
2414 case snd_soc_dapm_pga:
2415 case snd_soc_dapm_effect:
2416 case snd_soc_dapm_out_drv:
2417 case snd_soc_dapm_mixer:
2418 case snd_soc_dapm_mixer_named_ctl:
2419 case snd_soc_dapm_supply:
2420 case snd_soc_dapm_regulator_supply:
2421 case snd_soc_dapm_pinctrl:
2422 case snd_soc_dapm_clock_supply:
2424 count += sprintf(buf + count, "%s: %s\n",
2425 w->name, w->power ? "On":"Off");
2432 switch (snd_soc_dapm_get_bias_level(dapm)) {
2433 case SND_SOC_BIAS_ON:
2436 case SND_SOC_BIAS_PREPARE:
2439 case SND_SOC_BIAS_STANDBY:
2442 case SND_SOC_BIAS_OFF:
2446 count += sprintf(buf + count, "PM State: %s\n", state);
2451 /* show dapm widget status in sys fs */
2452 static ssize_t dapm_widget_show(struct device *dev,
2453 struct device_attribute *attr, char *buf)
2455 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2456 struct snd_soc_dai *codec_dai;
2459 mutex_lock(&rtd->card->dapm_mutex);
2461 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2462 struct snd_soc_component *cmpnt = codec_dai->component;
2464 count += dapm_widget_show_component(cmpnt, buf + count);
2467 mutex_unlock(&rtd->card->dapm_mutex);
2472 static DEVICE_ATTR_RO(dapm_widget);
2474 struct attribute *soc_dapm_dev_attrs[] = {
2475 &dev_attr_dapm_widget.attr,
2479 static void dapm_free_path(struct snd_soc_dapm_path *path)
2481 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2482 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2483 list_del(&path->list_kcontrol);
2484 list_del(&path->list);
2488 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2490 struct snd_soc_dapm_path *p, *next_p;
2491 enum snd_soc_dapm_direction dir;
2494 list_del(&w->dirty);
2496 * remove source and sink paths associated to this widget.
2497 * While removing the path, remove reference to it from both
2498 * source and sink widgets so that path is removed only once.
2500 snd_soc_dapm_for_each_direction(dir) {
2501 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2505 kfree(w->kcontrols);
2506 kfree_const(w->name);
2507 kfree_const(w->sname);
2511 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2513 dapm->path_sink_cache.widget = NULL;
2514 dapm->path_source_cache.widget = NULL;
2517 /* free all dapm widgets and resources */
2518 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2520 struct snd_soc_dapm_widget *w, *next_w;
2522 for_each_card_widgets_safe(dapm->card, w, next_w) {
2523 if (w->dapm != dapm)
2525 snd_soc_dapm_free_widget(w);
2527 snd_soc_dapm_reset_cache(dapm);
2530 static struct snd_soc_dapm_widget *dapm_find_widget(
2531 struct snd_soc_dapm_context *dapm, const char *pin,
2532 bool search_other_contexts)
2534 struct snd_soc_dapm_widget *w;
2535 struct snd_soc_dapm_widget *fallback = NULL;
2536 char prefixed_pin[80];
2537 const char *pin_name;
2538 const char *prefix = soc_dapm_prefix(dapm);
2541 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2543 pin_name = prefixed_pin;
2548 for_each_card_widgets(dapm->card, w) {
2549 if (!strcmp(w->name, pin_name)) {
2550 if (w->dapm == dapm)
2557 if (search_other_contexts)
2564 * set the DAPM pin status:
2565 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2566 * error code; called from kcontrol put callback
2568 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2569 const char *pin, int status)
2571 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2574 dapm_assert_locked(dapm);
2577 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2581 if (w->connected != status) {
2582 dapm_mark_dirty(w, "pin configuration");
2583 dapm_widget_invalidate_input_paths(w);
2584 dapm_widget_invalidate_output_paths(w);
2588 w->connected = status;
2596 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2597 * called from several API functions below
2599 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2600 const char *pin, int status)
2602 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2604 return ret < 0 ? ret : 0;
2608 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2609 * @dapm: DAPM context
2611 * Walks all dapm audio paths and powers widgets according to their
2612 * stream or path usage.
2614 * Requires external locking.
2616 * Returns 0 for success.
2618 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2621 * Suppress early reports (eg, jacks syncing their state) to avoid
2622 * silly DAPM runs during card startup.
2624 if (!dapm->card || !dapm->card->instantiated)
2627 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2629 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2632 * snd_soc_dapm_sync - scan and power dapm paths
2633 * @dapm: DAPM context
2635 * Walks all dapm audio paths and powers widgets according to their
2636 * stream or path usage.
2638 * Returns 0 for success.
2640 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2644 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2645 ret = snd_soc_dapm_sync_unlocked(dapm);
2646 mutex_unlock(&dapm->card->dapm_mutex);
2649 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2651 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2652 struct snd_soc_dapm_widget *w,
2656 case snd_soc_dapm_aif_out:
2657 case snd_soc_dapm_aif_in:
2663 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2664 w->channel < channels ? "Connecting" : "Disconnecting",
2665 p->source->name, p->sink->name);
2667 if (w->channel < channels)
2668 soc_dapm_connect_path(p, true, "dai update");
2670 soc_dapm_connect_path(p, false, "dai update");
2675 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2676 struct snd_pcm_hw_params *params,
2677 struct snd_soc_dai *dai)
2679 int dir = substream->stream;
2680 int channels = params_channels(params);
2681 struct snd_soc_dapm_path *p;
2682 struct snd_soc_dapm_widget *w;
2685 w = snd_soc_dai_get_widget(dai, dir);
2690 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2691 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2693 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2694 ret = dapm_update_dai_chan(p, p->sink, channels);
2699 snd_soc_dapm_widget_for_each_source_path(w, p) {
2700 ret = dapm_update_dai_chan(p, p->source, channels);
2708 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2709 struct snd_pcm_hw_params *params,
2710 struct snd_soc_dai *dai)
2712 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2715 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2716 ret = dapm_update_dai_unlocked(substream, params, dai);
2717 mutex_unlock(&rtd->card->dapm_mutex);
2721 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2724 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2725 * @w: The widget for which to update the flags
2727 * Some widgets have a dynamic category which depends on which neighbors they
2728 * are connected to. This function update the category for these widgets.
2730 * This function must be called whenever a path is added or removed to a widget.
2732 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2734 enum snd_soc_dapm_direction dir;
2735 struct snd_soc_dapm_path *p;
2739 case snd_soc_dapm_input:
2740 /* On a fully routed card an input is never a source */
2741 if (w->dapm->card->fully_routed)
2743 ep = SND_SOC_DAPM_EP_SOURCE;
2744 snd_soc_dapm_widget_for_each_source_path(w, p) {
2745 if (p->source->id == snd_soc_dapm_micbias ||
2746 p->source->id == snd_soc_dapm_mic ||
2747 p->source->id == snd_soc_dapm_line ||
2748 p->source->id == snd_soc_dapm_output) {
2754 case snd_soc_dapm_output:
2755 /* On a fully routed card a output is never a sink */
2756 if (w->dapm->card->fully_routed)
2758 ep = SND_SOC_DAPM_EP_SINK;
2759 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2760 if (p->sink->id == snd_soc_dapm_spk ||
2761 p->sink->id == snd_soc_dapm_hp ||
2762 p->sink->id == snd_soc_dapm_line ||
2763 p->sink->id == snd_soc_dapm_input) {
2769 case snd_soc_dapm_line:
2771 snd_soc_dapm_for_each_direction(dir) {
2772 if (!list_empty(&w->edges[dir]))
2773 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2783 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2784 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2785 const char *control)
2787 bool dynamic_source = false;
2788 bool dynamic_sink = false;
2793 switch (source->id) {
2794 case snd_soc_dapm_demux:
2795 dynamic_source = true;
2802 case snd_soc_dapm_mux:
2803 case snd_soc_dapm_switch:
2804 case snd_soc_dapm_mixer:
2805 case snd_soc_dapm_mixer_named_ctl:
2806 dynamic_sink = true;
2812 if (dynamic_source && dynamic_sink) {
2814 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2815 source->name, control, sink->name);
2817 } else if (!dynamic_source && !dynamic_sink) {
2819 "Control not supported for path %s -> [%s] -> %s\n",
2820 source->name, control, sink->name);
2827 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2828 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2829 const char *control,
2830 int (*connected)(struct snd_soc_dapm_widget *source,
2831 struct snd_soc_dapm_widget *sink))
2833 struct snd_soc_dapm_widget *widgets[2];
2834 enum snd_soc_dapm_direction dir;
2835 struct snd_soc_dapm_path *path;
2838 if (wsink->is_supply && !wsource->is_supply) {
2840 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2841 wsource->name, wsink->name);
2845 if (connected && !wsource->is_supply) {
2847 "connected() callback only supported for supply widgets (%s -> %s)\n",
2848 wsource->name, wsink->name);
2852 if (wsource->is_supply && control) {
2854 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2855 wsource->name, control, wsink->name);
2859 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2863 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2867 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2868 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2869 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2870 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2872 path->connected = connected;
2873 INIT_LIST_HEAD(&path->list);
2874 INIT_LIST_HEAD(&path->list_kcontrol);
2876 if (wsource->is_supply || wsink->is_supply)
2877 path->is_supply = 1;
2879 /* connect static paths */
2880 if (control == NULL) {
2883 switch (wsource->id) {
2884 case snd_soc_dapm_demux:
2885 ret = dapm_connect_mux(dapm, path, control, wsource);
2893 switch (wsink->id) {
2894 case snd_soc_dapm_mux:
2895 ret = dapm_connect_mux(dapm, path, control, wsink);
2899 case snd_soc_dapm_switch:
2900 case snd_soc_dapm_mixer:
2901 case snd_soc_dapm_mixer_named_ctl:
2902 ret = dapm_connect_mixer(dapm, path, control);
2911 list_add(&path->list, &dapm->card->paths);
2912 snd_soc_dapm_for_each_direction(dir)
2913 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2915 snd_soc_dapm_for_each_direction(dir) {
2916 dapm_update_widget_flags(widgets[dir]);
2917 dapm_mark_dirty(widgets[dir], "Route added");
2920 if (dapm->card->instantiated && path->connect)
2921 dapm_path_invalidate(path);
2929 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2930 const struct snd_soc_dapm_route *route)
2932 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2933 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2936 char prefixed_sink[80];
2937 char prefixed_source[80];
2939 unsigned int sink_ref = 0;
2940 unsigned int source_ref = 0;
2943 prefix = soc_dapm_prefix(dapm);
2945 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2946 prefix, route->sink);
2947 sink = prefixed_sink;
2948 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2949 prefix, route->source);
2950 source = prefixed_source;
2953 source = route->source;
2956 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2957 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2959 if (wsink && wsource)
2963 * find src and dest widgets over all widgets but favor a widget from
2964 * current DAPM context
2966 for_each_card_widgets(dapm->card, w) {
2967 if (!wsink && !(strcmp(w->name, sink))) {
2969 if (w->dapm == dapm) {
2977 "ASoC: sink widget %s overwritten\n",
2981 if (!wsource && !(strcmp(w->name, source))) {
2983 if (w->dapm == dapm) {
2991 "ASoC: source widget %s overwritten\n",
2995 /* use widget from another DAPM context if not found from this */
3001 if (wsource == NULL) {
3002 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3006 if (wsink == NULL) {
3007 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3013 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3014 dapm_wcache_update(&dapm->path_source_cache, wsource);
3016 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3023 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
3024 source, route->control, sink);
3028 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3029 const struct snd_soc_dapm_route *route)
3031 struct snd_soc_dapm_path *path, *p;
3034 char prefixed_sink[80];
3035 char prefixed_source[80];
3038 if (route->control) {
3040 "ASoC: Removal of routes with controls not supported\n");
3044 prefix = soc_dapm_prefix(dapm);
3046 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3047 prefix, route->sink);
3048 sink = prefixed_sink;
3049 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3050 prefix, route->source);
3051 source = prefixed_source;
3054 source = route->source;
3058 list_for_each_entry(p, &dapm->card->paths, list) {
3059 if (strcmp(p->source->name, source) != 0)
3061 if (strcmp(p->sink->name, sink) != 0)
3068 struct snd_soc_dapm_widget *wsource = path->source;
3069 struct snd_soc_dapm_widget *wsink = path->sink;
3071 dapm_mark_dirty(wsource, "Route removed");
3072 dapm_mark_dirty(wsink, "Route removed");
3074 dapm_path_invalidate(path);
3076 dapm_free_path(path);
3078 /* Update any path related flags */
3079 dapm_update_widget_flags(wsource);
3080 dapm_update_widget_flags(wsink);
3082 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3090 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3091 * @dapm: DAPM context
3092 * @route: audio routes
3093 * @num: number of routes
3095 * Connects 2 dapm widgets together via a named audio path. The sink is
3096 * the widget receiving the audio signal, whilst the source is the sender
3097 * of the audio signal.
3099 * Returns 0 for success else error. On error all resources can be freed
3100 * with a call to snd_soc_card_free().
3102 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3103 const struct snd_soc_dapm_route *route, int num)
3107 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3108 for (i = 0; i < num; i++) {
3109 int r = snd_soc_dapm_add_route(dapm, route);
3111 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3113 route->control ? route->control : "direct",
3119 mutex_unlock(&dapm->card->dapm_mutex);
3123 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3126 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3127 * @dapm: DAPM context
3128 * @route: audio routes
3129 * @num: number of routes
3131 * Removes routes from the DAPM context.
3133 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3134 const struct snd_soc_dapm_route *route, int num)
3138 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3139 for (i = 0; i < num; i++) {
3140 snd_soc_dapm_del_route(dapm, route);
3143 mutex_unlock(&dapm->card->dapm_mutex);
3147 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3149 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3150 const struct snd_soc_dapm_route *route)
3152 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3155 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3158 struct snd_soc_dapm_path *path;
3162 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3168 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3173 if (route->control || route->connected)
3174 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3175 route->source, route->sink);
3177 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3178 if (path->sink == sink) {
3185 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3186 route->source, route->sink);
3188 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3189 count, route->source, route->sink);
3195 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3196 * @dapm: DAPM context
3197 * @route: audio routes
3198 * @num: number of routes
3200 * Mark existing routes matching those specified in the passed array
3201 * as being weak, meaning that they are ignored for the purpose of
3202 * power decisions. The main intended use case is for sidetone paths
3203 * which couple audio between other independent paths if they are both
3204 * active in order to make the combination work better at the user
3205 * level but which aren't intended to be "used".
3207 * Note that CODEC drivers should not use this as sidetone type paths
3208 * can frequently also be used as bypass paths.
3210 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3211 const struct snd_soc_dapm_route *route, int num)
3216 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3217 for (i = 0; i < num; i++) {
3218 int err = snd_soc_dapm_weak_route(dapm, route);
3223 mutex_unlock(&dapm->card->dapm_mutex);
3227 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3230 * snd_soc_dapm_new_widgets - add new dapm widgets
3231 * @card: card to be checked for new dapm widgets
3233 * Checks the codec for any new dapm widgets and creates them if found.
3235 * Returns 0 for success.
3237 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3239 struct snd_soc_dapm_widget *w;
3242 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3244 for_each_card_widgets(card, w)
3249 if (w->num_kcontrols) {
3250 w->kcontrols = kcalloc(w->num_kcontrols,
3251 sizeof(struct snd_kcontrol *),
3253 if (!w->kcontrols) {
3254 mutex_unlock(&card->dapm_mutex);
3260 case snd_soc_dapm_switch:
3261 case snd_soc_dapm_mixer:
3262 case snd_soc_dapm_mixer_named_ctl:
3265 case snd_soc_dapm_mux:
3266 case snd_soc_dapm_demux:
3269 case snd_soc_dapm_pga:
3270 case snd_soc_dapm_effect:
3271 case snd_soc_dapm_out_drv:
3274 case snd_soc_dapm_dai_link:
3275 dapm_new_dai_link(w);
3281 /* Read the initial power state from the device */
3283 val = soc_dapm_read(w->dapm, w->reg);
3284 val = val >> w->shift;
3286 if (val == w->on_val)
3292 dapm_mark_dirty(w, "new widget");
3293 dapm_debugfs_add_widget(w);
3296 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3297 mutex_unlock(&card->dapm_mutex);
3300 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3303 * snd_soc_dapm_get_volsw - dapm mixer get callback
3304 * @kcontrol: mixer control
3305 * @ucontrol: control element information
3307 * Callback to get the value of a dapm mixer control.
3309 * Returns 0 for success.
3311 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3314 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3315 struct snd_soc_card *card = dapm->card;
3316 struct soc_mixer_control *mc =
3317 (struct soc_mixer_control *)kcontrol->private_value;
3319 unsigned int shift = mc->shift;
3321 unsigned int width = fls(max);
3322 unsigned int mask = (1 << fls(max)) - 1;
3323 unsigned int invert = mc->invert;
3324 unsigned int reg_val, val, rval = 0;
3326 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3327 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3328 reg_val = soc_dapm_read(dapm, reg);
3329 val = (reg_val >> shift) & mask;
3331 if (reg != mc->rreg)
3332 reg_val = soc_dapm_read(dapm, mc->rreg);
3334 if (snd_soc_volsw_is_stereo(mc))
3335 rval = (reg_val >> mc->rshift) & mask;
3337 reg_val = dapm_kcontrol_get_value(kcontrol);
3338 val = reg_val & mask;
3340 if (snd_soc_volsw_is_stereo(mc))
3341 rval = (reg_val >> width) & mask;
3343 mutex_unlock(&card->dapm_mutex);
3346 ucontrol->value.integer.value[0] = max - val;
3348 ucontrol->value.integer.value[0] = val;
3350 if (snd_soc_volsw_is_stereo(mc)) {
3352 ucontrol->value.integer.value[1] = max - rval;
3354 ucontrol->value.integer.value[1] = rval;
3359 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3362 * snd_soc_dapm_put_volsw - dapm mixer set callback
3363 * @kcontrol: mixer control
3364 * @ucontrol: control element information
3366 * Callback to set the value of a dapm mixer control.
3368 * Returns 0 for success.
3370 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3371 struct snd_ctl_elem_value *ucontrol)
3373 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3374 struct snd_soc_card *card = dapm->card;
3375 struct soc_mixer_control *mc =
3376 (struct soc_mixer_control *)kcontrol->private_value;
3378 unsigned int shift = mc->shift;
3380 unsigned int width = fls(max);
3381 unsigned int mask = (1 << width) - 1;
3382 unsigned int invert = mc->invert;
3383 unsigned int val, rval = 0;
3384 int connect, rconnect = -1, change, reg_change = 0;
3385 struct snd_soc_dapm_update update = {};
3388 val = (ucontrol->value.integer.value[0] & mask);
3394 if (snd_soc_volsw_is_stereo(mc)) {
3395 rval = (ucontrol->value.integer.value[1] & mask);
3401 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3403 /* This assumes field width < (bits in unsigned int / 2) */
3404 if (width > sizeof(unsigned int) * 8 / 2)
3406 "ASoC: control %s field width limit exceeded\n",
3408 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3410 if (reg != SND_SOC_NOPM) {
3412 rval = rval << mc->rshift;
3414 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3416 if (snd_soc_volsw_is_stereo(mc))
3417 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3422 if (change || reg_change) {
3424 if (snd_soc_volsw_is_stereo(mc)) {
3425 update.has_second_set = true;
3426 update.reg2 = mc->rreg;
3427 update.mask2 = mask << mc->rshift;
3430 update.kcontrol = kcontrol;
3432 update.mask = mask << shift;
3434 card->update = &update;
3437 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3440 card->update = NULL;
3443 mutex_unlock(&card->dapm_mutex);
3446 snd_soc_dpcm_runtime_update(card);
3450 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3453 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3454 * @kcontrol: mixer control
3455 * @ucontrol: control element information
3457 * Callback to get the value of a dapm enumerated double mixer control.
3459 * Returns 0 for success.
3461 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol)
3464 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3465 struct snd_soc_card *card = dapm->card;
3466 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3467 unsigned int reg_val, val;
3469 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3470 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3471 reg_val = soc_dapm_read(dapm, e->reg);
3473 reg_val = dapm_kcontrol_get_value(kcontrol);
3475 mutex_unlock(&card->dapm_mutex);
3477 val = (reg_val >> e->shift_l) & e->mask;
3478 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3479 if (e->shift_l != e->shift_r) {
3480 val = (reg_val >> e->shift_r) & e->mask;
3481 val = snd_soc_enum_val_to_item(e, val);
3482 ucontrol->value.enumerated.item[1] = val;
3487 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3490 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3491 * @kcontrol: mixer control
3492 * @ucontrol: control element information
3494 * Callback to set the value of a dapm enumerated double mixer control.
3496 * Returns 0 for success.
3498 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3499 struct snd_ctl_elem_value *ucontrol)
3501 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3502 struct snd_soc_card *card = dapm->card;
3503 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3504 unsigned int *item = ucontrol->value.enumerated.item;
3505 unsigned int val, change, reg_change = 0;
3507 struct snd_soc_dapm_update update = {};
3510 if (item[0] >= e->items)
3513 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3514 mask = e->mask << e->shift_l;
3515 if (e->shift_l != e->shift_r) {
3516 if (item[1] > e->items)
3518 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3519 mask |= e->mask << e->shift_r;
3522 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3524 change = dapm_kcontrol_set_value(kcontrol, val);
3526 if (e->reg != SND_SOC_NOPM)
3527 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3529 if (change || reg_change) {
3531 update.kcontrol = kcontrol;
3532 update.reg = e->reg;
3535 card->update = &update;
3538 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3540 card->update = NULL;
3543 mutex_unlock(&card->dapm_mutex);
3546 snd_soc_dpcm_runtime_update(card);
3550 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3553 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3555 * @kcontrol: mixer control
3556 * @uinfo: control element information
3558 * Callback to provide information about a pin switch control.
3560 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3561 struct snd_ctl_elem_info *uinfo)
3563 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3565 uinfo->value.integer.min = 0;
3566 uinfo->value.integer.max = 1;
3570 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3573 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3575 * @kcontrol: mixer control
3578 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3579 struct snd_ctl_elem_value *ucontrol)
3581 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3582 const char *pin = (const char *)kcontrol->private_value;
3584 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3586 ucontrol->value.integer.value[0] =
3587 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3589 mutex_unlock(&card->dapm_mutex);
3593 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3596 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3598 * @kcontrol: mixer control
3601 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3602 struct snd_ctl_elem_value *ucontrol)
3604 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3605 const char *pin = (const char *)kcontrol->private_value;
3608 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3609 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3610 !!ucontrol->value.integer.value[0]);
3611 mutex_unlock(&card->dapm_mutex);
3613 snd_soc_dapm_sync(&card->dapm);
3616 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3618 struct snd_soc_dapm_widget *
3619 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3620 const struct snd_soc_dapm_widget *widget)
3622 enum snd_soc_dapm_direction dir;
3623 struct snd_soc_dapm_widget *w;
3627 if ((w = dapm_cnew_widget(widget)) == NULL)
3628 return ERR_PTR(-ENOMEM);
3631 case snd_soc_dapm_regulator_supply:
3632 w->regulator = devm_regulator_get(dapm->dev, w->name);
3633 if (IS_ERR(w->regulator)) {
3634 ret = PTR_ERR(w->regulator);
3635 goto request_failed;
3638 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3639 ret = regulator_allow_bypass(w->regulator, true);
3642 "ASoC: Failed to bypass %s: %d\n",
3646 case snd_soc_dapm_pinctrl:
3647 w->pinctrl = devm_pinctrl_get(dapm->dev);
3648 if (IS_ERR(w->pinctrl)) {
3649 ret = PTR_ERR(w->pinctrl);
3650 goto request_failed;
3653 /* set to sleep_state when initializing */
3654 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3656 case snd_soc_dapm_clock_supply:
3657 w->clk = devm_clk_get(dapm->dev, w->name);
3658 if (IS_ERR(w->clk)) {
3659 ret = PTR_ERR(w->clk);
3660 goto request_failed;
3667 prefix = soc_dapm_prefix(dapm);
3669 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3671 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3672 if (w->name == NULL) {
3673 kfree_const(w->sname);
3675 return ERR_PTR(-ENOMEM);
3679 case snd_soc_dapm_mic:
3680 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3681 w->power_check = dapm_generic_check_power;
3683 case snd_soc_dapm_input:
3684 if (!dapm->card->fully_routed)
3685 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3686 w->power_check = dapm_generic_check_power;
3688 case snd_soc_dapm_spk:
3689 case snd_soc_dapm_hp:
3690 w->is_ep = SND_SOC_DAPM_EP_SINK;
3691 w->power_check = dapm_generic_check_power;
3693 case snd_soc_dapm_output:
3694 if (!dapm->card->fully_routed)
3695 w->is_ep = SND_SOC_DAPM_EP_SINK;
3696 w->power_check = dapm_generic_check_power;
3698 case snd_soc_dapm_vmid:
3699 case snd_soc_dapm_siggen:
3700 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3701 w->power_check = dapm_always_on_check_power;
3703 case snd_soc_dapm_sink:
3704 w->is_ep = SND_SOC_DAPM_EP_SINK;
3705 w->power_check = dapm_always_on_check_power;
3708 case snd_soc_dapm_mux:
3709 case snd_soc_dapm_demux:
3710 case snd_soc_dapm_switch:
3711 case snd_soc_dapm_mixer:
3712 case snd_soc_dapm_mixer_named_ctl:
3713 case snd_soc_dapm_adc:
3714 case snd_soc_dapm_aif_out:
3715 case snd_soc_dapm_dac:
3716 case snd_soc_dapm_aif_in:
3717 case snd_soc_dapm_pga:
3718 case snd_soc_dapm_buffer:
3719 case snd_soc_dapm_scheduler:
3720 case snd_soc_dapm_effect:
3721 case snd_soc_dapm_src:
3722 case snd_soc_dapm_asrc:
3723 case snd_soc_dapm_encoder:
3724 case snd_soc_dapm_decoder:
3725 case snd_soc_dapm_out_drv:
3726 case snd_soc_dapm_micbias:
3727 case snd_soc_dapm_line:
3728 case snd_soc_dapm_dai_link:
3729 case snd_soc_dapm_dai_out:
3730 case snd_soc_dapm_dai_in:
3731 w->power_check = dapm_generic_check_power;
3733 case snd_soc_dapm_supply:
3734 case snd_soc_dapm_regulator_supply:
3735 case snd_soc_dapm_pinctrl:
3736 case snd_soc_dapm_clock_supply:
3737 case snd_soc_dapm_kcontrol:
3739 w->power_check = dapm_supply_check_power;
3742 w->power_check = dapm_always_on_check_power;
3747 INIT_LIST_HEAD(&w->list);
3748 INIT_LIST_HEAD(&w->dirty);
3749 /* see for_each_card_widgets */
3750 list_add_tail(&w->list, &dapm->card->widgets);
3752 snd_soc_dapm_for_each_direction(dir) {
3753 INIT_LIST_HEAD(&w->edges[dir]);
3754 w->endpoints[dir] = -1;
3757 /* machine layer sets up unconnected pins and insertions */
3762 if (ret != -EPROBE_DEFER)
3763 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3766 kfree_const(w->sname);
3768 return ERR_PTR(ret);
3772 * snd_soc_dapm_new_control - create new dapm control
3773 * @dapm: DAPM context
3774 * @widget: widget template
3776 * Creates new DAPM control based upon a template.
3778 * Returns a widget pointer on success or an error pointer on failure
3780 struct snd_soc_dapm_widget *
3781 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3782 const struct snd_soc_dapm_widget *widget)
3784 struct snd_soc_dapm_widget *w;
3786 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3787 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3788 mutex_unlock(&dapm->card->dapm_mutex);
3792 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3795 * snd_soc_dapm_new_controls - create new dapm controls
3796 * @dapm: DAPM context
3797 * @widget: widget array
3798 * @num: number of widgets
3800 * Creates new DAPM controls based upon the templates.
3802 * Returns 0 for success else error.
3804 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3805 const struct snd_soc_dapm_widget *widget,
3811 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3812 for (i = 0; i < num; i++) {
3813 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3820 mutex_unlock(&dapm->card->dapm_mutex);
3823 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3826 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3827 struct snd_pcm_substream *substream)
3829 struct snd_soc_dapm_path *path;
3830 struct snd_soc_dai *source, *sink;
3831 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3832 struct snd_pcm_hw_params *params = NULL;
3833 const struct snd_soc_pcm_stream *config = NULL;
3834 struct snd_pcm_runtime *runtime = NULL;
3838 params = kzalloc(sizeof(*params), GFP_KERNEL);
3842 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3848 substream->runtime = runtime;
3850 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3851 snd_soc_dapm_widget_for_each_source_path(w, path) {
3852 source = path->source->priv;
3854 ret = snd_soc_dai_startup(source, substream);
3858 snd_soc_dai_activate(source, substream->stream);
3861 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3862 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3863 sink = path->sink->priv;
3865 ret = snd_soc_dai_startup(sink, substream);
3869 snd_soc_dai_activate(sink, substream->stream);
3872 substream->hw_opened = 1;
3875 * Note: getting the config after .startup() gives a chance to
3876 * either party on the link to alter the configuration if
3879 config = rtd->dai_link->params + rtd->params_select;
3880 if (WARN_ON(!config)) {
3881 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3886 /* Be a little careful as we don't want to overflow the mask array */
3887 if (config->formats) {
3888 fmt = ffs(config->formats) - 1;
3890 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3897 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3898 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3900 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3902 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3903 = config->channels_min;
3904 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3905 = config->channels_max;
3907 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3908 snd_soc_dapm_widget_for_each_source_path(w, path) {
3909 source = path->source->priv;
3911 ret = snd_soc_dai_hw_params(source, substream, params);
3915 dapm_update_dai_unlocked(substream, params, source);
3918 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3919 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3920 sink = path->sink->priv;
3922 ret = snd_soc_dai_hw_params(sink, substream, params);
3926 dapm_update_dai_unlocked(substream, params, sink);
3929 runtime->format = params_format(params);
3930 runtime->subformat = params_subformat(params);
3931 runtime->channels = params_channels(params);
3932 runtime->rate = params_rate(params);
3939 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3940 struct snd_kcontrol *kcontrol, int event)
3942 struct snd_soc_dapm_path *path;
3943 struct snd_soc_dai *source, *sink;
3944 struct snd_pcm_substream *substream = w->priv;
3945 int ret = 0, saved_stream = substream->stream;
3947 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3948 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3952 case SND_SOC_DAPM_PRE_PMU:
3953 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3959 case SND_SOC_DAPM_POST_PMU:
3960 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3961 sink = path->sink->priv;
3963 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3968 case SND_SOC_DAPM_PRE_PMD:
3969 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3970 sink = path->sink->priv;
3972 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3976 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3977 snd_soc_dapm_widget_for_each_source_path(w, path) {
3978 source = path->source->priv;
3979 snd_soc_dai_hw_free(source, substream, 0);
3982 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3983 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3984 sink = path->sink->priv;
3985 snd_soc_dai_hw_free(sink, substream, 0);
3988 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3989 snd_soc_dapm_widget_for_each_source_path(w, path) {
3990 source = path->source->priv;
3991 snd_soc_dai_deactivate(source, substream->stream);
3992 snd_soc_dai_shutdown(source, substream, 0);
3995 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3996 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3997 sink = path->sink->priv;
3998 snd_soc_dai_deactivate(sink, substream->stream);
3999 snd_soc_dai_shutdown(sink, substream, 0);
4003 case SND_SOC_DAPM_POST_PMD:
4004 kfree(substream->runtime);
4008 WARN(1, "Unknown event %d\n", event);
4013 /* Restore the substream direction */
4014 substream->stream = saved_stream;
4018 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4019 struct snd_ctl_elem_value *ucontrol)
4021 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4022 struct snd_soc_pcm_runtime *rtd = w->priv;
4024 ucontrol->value.enumerated.item[0] = rtd->params_select;
4029 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4030 struct snd_ctl_elem_value *ucontrol)
4032 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4033 struct snd_soc_pcm_runtime *rtd = w->priv;
4035 /* Can't change the config when widget is already powered */
4039 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4042 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4045 rtd->params_select = ucontrol->value.enumerated.item[0];
4051 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4052 unsigned long *private_value,
4054 const char **w_param_text)
4058 devm_kfree(card->dev, (void *)*private_value);
4063 for (count = 0 ; count < num_params; count++)
4064 devm_kfree(card->dev, (void *)w_param_text[count]);
4065 devm_kfree(card->dev, w_param_text);
4068 static struct snd_kcontrol_new *
4069 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4071 const struct snd_soc_pcm_stream *params,
4072 int num_params, const char **w_param_text,
4073 unsigned long *private_value)
4075 struct soc_enum w_param_enum[] = {
4076 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4078 struct snd_kcontrol_new kcontrol_dai_link[] = {
4079 SOC_ENUM_EXT(NULL, w_param_enum[0],
4080 snd_soc_dapm_dai_link_get,
4081 snd_soc_dapm_dai_link_put),
4083 struct snd_kcontrol_new *kcontrol_news;
4084 const struct snd_soc_pcm_stream *config = params;
4087 for (count = 0 ; count < num_params; count++) {
4088 if (!config->stream_name) {
4089 dev_warn(card->dapm.dev,
4090 "ASoC: anonymous config %d for dai link %s\n",
4092 w_param_text[count] =
4093 devm_kasprintf(card->dev, GFP_KERNEL,
4094 "Anonymous Configuration %d",
4097 w_param_text[count] = devm_kmemdup(card->dev,
4098 config->stream_name,
4099 strlen(config->stream_name) + 1,
4102 if (!w_param_text[count])
4103 goto outfree_w_param;
4107 w_param_enum[0].items = num_params;
4108 w_param_enum[0].texts = w_param_text;
4111 (unsigned long) devm_kmemdup(card->dev,
4112 (void *)(kcontrol_dai_link[0].private_value),
4113 sizeof(struct soc_enum), GFP_KERNEL);
4114 if (!*private_value) {
4115 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4117 goto outfree_w_param;
4119 kcontrol_dai_link[0].private_value = *private_value;
4120 /* duplicate kcontrol_dai_link on heap so that memory persists */
4121 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4122 sizeof(struct snd_kcontrol_new),
4124 if (!kcontrol_news) {
4125 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4127 goto outfree_w_param;
4129 return kcontrol_news;
4132 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4136 static struct snd_soc_dapm_widget *
4137 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4138 struct snd_pcm_substream *substream,
4141 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4142 struct snd_soc_dapm_widget template;
4143 struct snd_soc_dapm_widget *w;
4144 const char **w_param_text;
4145 unsigned long private_value = 0;
4149 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4150 rtd->dai_link->name, id);
4152 return ERR_PTR(-ENOMEM);
4154 memset(&template, 0, sizeof(template));
4155 template.reg = SND_SOC_NOPM;
4156 template.id = snd_soc_dapm_dai_link;
4157 template.name = link_name;
4158 template.event = snd_soc_dai_link_event;
4159 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4160 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4161 template.kcontrol_news = NULL;
4163 /* allocate memory for control, only in case of multiple configs */
4164 if (rtd->dai_link->num_params > 1) {
4165 w_param_text = devm_kcalloc(card->dev,
4166 rtd->dai_link->num_params,
4167 sizeof(char *), GFP_KERNEL);
4168 if (!w_param_text) {
4173 template.num_kcontrols = 1;
4174 template.kcontrol_news =
4175 snd_soc_dapm_alloc_kcontrol(card,
4177 rtd->dai_link->params,
4178 rtd->dai_link->num_params,
4179 w_param_text, &private_value);
4180 if (!template.kcontrol_news) {
4185 w_param_text = NULL;
4187 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4189 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4192 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4194 goto outfree_kcontrol_news;
4197 w->priv = substream;
4201 outfree_kcontrol_news:
4202 devm_kfree(card->dev, (void *)template.kcontrol_news);
4203 snd_soc_dapm_free_kcontrol(card, &private_value,
4204 rtd->dai_link->num_params, w_param_text);
4206 devm_kfree(card->dev, link_name);
4207 return ERR_PTR(ret);
4210 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4211 struct snd_soc_dai *dai)
4213 struct snd_soc_dapm_widget template;
4214 struct snd_soc_dapm_widget *w;
4216 WARN_ON(dapm->dev != dai->dev);
4218 memset(&template, 0, sizeof(template));
4219 template.reg = SND_SOC_NOPM;
4221 if (dai->driver->playback.stream_name) {
4222 template.id = snd_soc_dapm_dai_in;
4223 template.name = dai->driver->playback.stream_name;
4224 template.sname = dai->driver->playback.stream_name;
4226 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4229 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4234 dai->playback_widget = w;
4237 if (dai->driver->capture.stream_name) {
4238 template.id = snd_soc_dapm_dai_out;
4239 template.name = dai->driver->capture.stream_name;
4240 template.sname = dai->driver->capture.stream_name;
4242 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4245 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4250 dai->capture_widget = w;
4256 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4258 struct snd_soc_dapm_widget *dai_w, *w;
4259 struct snd_soc_dapm_widget *src, *sink;
4260 struct snd_soc_dai *dai;
4262 /* For each DAI widget... */
4263 for_each_card_widgets(card, dai_w) {
4264 switch (dai_w->id) {
4265 case snd_soc_dapm_dai_in:
4266 case snd_soc_dapm_dai_out:
4272 /* let users know there is no DAI to link */
4274 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4281 /* ...find all widgets with the same stream and link them */
4282 for_each_card_widgets(card, w) {
4283 if (w->dapm != dai_w->dapm)
4287 case snd_soc_dapm_dai_in:
4288 case snd_soc_dapm_dai_out:
4294 if (!w->sname || !strstr(w->sname, dai_w->sname))
4297 if (dai_w->id == snd_soc_dapm_dai_in) {
4304 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4305 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4312 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4313 struct snd_soc_dai *src_dai,
4314 struct snd_soc_dapm_widget *src,
4315 struct snd_soc_dapm_widget *dai,
4316 struct snd_soc_dai *sink_dai,
4317 struct snd_soc_dapm_widget *sink)
4319 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4320 src_dai->component->name, src->name,
4321 sink_dai->component->name, sink->name);
4324 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4328 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4331 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4332 struct snd_soc_pcm_runtime *rtd,
4333 struct snd_soc_dai *codec_dai,
4334 struct snd_soc_dai *cpu_dai)
4336 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4337 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4338 struct snd_pcm_substream *substream;
4339 struct snd_pcm_str *streams = rtd->pcm->streams;
4341 if (dai_link->params) {
4342 playback_cpu = cpu_dai->capture_widget;
4343 capture_cpu = cpu_dai->playback_widget;
4345 playback_cpu = cpu_dai->playback_widget;
4346 capture_cpu = cpu_dai->capture_widget;
4349 /* connect BE DAI playback if widgets are valid */
4350 codec = codec_dai->playback_widget;
4352 if (playback_cpu && codec) {
4353 if (dai_link->params && !rtd->playback_widget) {
4354 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4355 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4358 rtd->playback_widget = dai;
4361 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4362 rtd->playback_widget,
4367 /* connect BE DAI capture if widgets are valid */
4368 codec = codec_dai->capture_widget;
4370 if (codec && capture_cpu) {
4371 if (dai_link->params && !rtd->capture_widget) {
4372 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4373 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4376 rtd->capture_widget = dai;
4379 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4380 rtd->capture_widget,
4381 cpu_dai, capture_cpu);
4385 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4388 struct snd_soc_dapm_widget *w;
4390 w = snd_soc_dai_get_widget(dai, stream);
4395 dapm_mark_dirty(w, "stream event");
4397 if (w->id == snd_soc_dapm_dai_in) {
4398 ep = SND_SOC_DAPM_EP_SOURCE;
4399 dapm_widget_invalidate_input_paths(w);
4401 ep = SND_SOC_DAPM_EP_SINK;
4402 dapm_widget_invalidate_output_paths(w);
4406 case SND_SOC_DAPM_STREAM_START:
4410 case SND_SOC_DAPM_STREAM_STOP:
4414 case SND_SOC_DAPM_STREAM_SUSPEND:
4415 case SND_SOC_DAPM_STREAM_RESUME:
4416 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4417 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4423 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4425 struct snd_soc_pcm_runtime *rtd;
4426 struct snd_soc_dai *codec_dai;
4429 /* for each BE DAI link... */
4430 for_each_card_rtds(card, rtd) {
4432 * dynamic FE links have no fixed DAI mapping.
4433 * CODEC<->CODEC links have no direct connection.
4435 if (rtd->dai_link->dynamic)
4438 if (rtd->num_cpus == 1) {
4439 for_each_rtd_codec_dais(rtd, i, codec_dai)
4440 dapm_connect_dai_pair(card, rtd, codec_dai,
4441 asoc_rtd_to_cpu(rtd, 0));
4442 } else if (rtd->num_codecs == rtd->num_cpus) {
4443 for_each_rtd_codec_dais(rtd, i, codec_dai)
4444 dapm_connect_dai_pair(card, rtd, codec_dai,
4445 asoc_rtd_to_cpu(rtd, i));
4448 "N cpus to M codecs link is not supported yet\n");
4453 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4456 struct snd_soc_dai *dai;
4459 for_each_rtd_dais(rtd, i, dai)
4460 soc_dapm_dai_stream_event(dai, stream, event);
4462 dapm_power_widgets(rtd->card, event);
4466 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4467 * @rtd: PCM runtime data
4468 * @stream: stream name
4469 * @event: stream event
4471 * Sends a stream event to the dapm core. The core then makes any
4472 * necessary widget power changes.
4474 * Returns 0 for success else error.
4476 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4479 struct snd_soc_card *card = rtd->card;
4481 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4482 soc_dapm_stream_event(rtd, stream, event);
4483 mutex_unlock(&card->dapm_mutex);
4486 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4488 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4489 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4490 /* powered down playback stream now */
4491 snd_soc_dapm_stream_event(rtd,
4492 SNDRV_PCM_STREAM_PLAYBACK,
4493 SND_SOC_DAPM_STREAM_STOP);
4495 /* start delayed pop wq here for playback streams */
4497 queue_delayed_work(system_power_efficient_wq,
4499 msecs_to_jiffies(rtd->pmdown_time));
4502 /* capture streams can be powered down now */
4503 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4504 SND_SOC_DAPM_STREAM_STOP);
4507 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4510 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4511 * @dapm: DAPM context
4514 * Enables input/output pin and its parents or children widgets iff there is
4515 * a valid audio route and active audio stream.
4517 * Requires external locking.
4519 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4520 * do any widget power switching.
4522 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4525 return snd_soc_dapm_set_pin(dapm, pin, 1);
4527 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4530 * snd_soc_dapm_enable_pin - enable pin.
4531 * @dapm: DAPM context
4534 * Enables input/output pin and its parents or children widgets iff there is
4535 * a valid audio route and active audio stream.
4537 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4538 * do any widget power switching.
4540 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4544 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4546 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4548 mutex_unlock(&dapm->card->dapm_mutex);
4552 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4555 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4556 * @dapm: DAPM context
4559 * Enables input/output pin regardless of any other state. This is
4560 * intended for use with microphone bias supplies used in microphone
4563 * Requires external locking.
4565 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4566 * do any widget power switching.
4568 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4571 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4574 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4578 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4579 if (!w->connected) {
4581 * w->force does not affect the number of input or output paths,
4582 * so we only have to recheck if w->connected is changed
4584 dapm_widget_invalidate_input_paths(w);
4585 dapm_widget_invalidate_output_paths(w);
4589 dapm_mark_dirty(w, "force enable");
4593 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4596 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4597 * @dapm: DAPM context
4600 * Enables input/output pin regardless of any other state. This is
4601 * intended for use with microphone bias supplies used in microphone
4604 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4605 * do any widget power switching.
4607 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4612 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4614 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4616 mutex_unlock(&dapm->card->dapm_mutex);
4620 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4623 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4624 * @dapm: DAPM context
4627 * Disables input/output pin and its parents or children widgets.
4629 * Requires external locking.
4631 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4632 * do any widget power switching.
4634 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4637 return snd_soc_dapm_set_pin(dapm, pin, 0);
4639 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4642 * snd_soc_dapm_disable_pin - disable pin.
4643 * @dapm: DAPM context
4646 * Disables input/output pin and its parents or children widgets.
4648 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4649 * do any widget power switching.
4651 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4656 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4658 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4660 mutex_unlock(&dapm->card->dapm_mutex);
4664 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4667 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4668 * @dapm: DAPM context
4671 * Marks the specified pin as being not connected, disabling it along
4672 * any parent or child widgets. At present this is identical to
4673 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4674 * additional things such as disabling controls which only affect
4675 * paths through the pin.
4677 * Requires external locking.
4679 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4680 * do any widget power switching.
4682 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4685 return snd_soc_dapm_set_pin(dapm, pin, 0);
4687 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4690 * snd_soc_dapm_nc_pin - permanently disable pin.
4691 * @dapm: DAPM context
4694 * Marks the specified pin as being not connected, disabling it along
4695 * any parent or child widgets. At present this is identical to
4696 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4697 * additional things such as disabling controls which only affect
4698 * paths through the pin.
4700 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4701 * do any widget power switching.
4703 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4707 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4709 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4711 mutex_unlock(&dapm->card->dapm_mutex);
4715 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4718 * snd_soc_dapm_get_pin_status - get audio pin status
4719 * @dapm: DAPM context
4720 * @pin: audio signal pin endpoint (or start point)
4722 * Get audio pin status - connected or disconnected.
4724 * Returns 1 for connected otherwise 0.
4726 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4729 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4732 return w->connected;
4736 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4739 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4740 * @dapm: DAPM context
4741 * @pin: audio signal pin endpoint (or start point)
4743 * Mark the given endpoint or pin as ignoring suspend. When the
4744 * system is disabled a path between two endpoints flagged as ignoring
4745 * suspend will not be disabled. The path must already be enabled via
4746 * normal means at suspend time, it will not be turned on if it was not
4749 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4752 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4755 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4759 w->ignore_suspend = 1;
4763 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4766 * snd_soc_dapm_free - free dapm resources
4767 * @dapm: DAPM context
4769 * Free all dapm widgets and resources.
4771 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4773 dapm_debugfs_cleanup(dapm);
4774 dapm_free_widgets(dapm);
4775 list_del(&dapm->list);
4777 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4779 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4780 struct snd_soc_card *card,
4781 struct snd_soc_component *component)
4784 dapm->component = component;
4785 dapm->bias_level = SND_SOC_BIAS_OFF;
4788 dapm->dev = component->dev;
4789 dapm->idle_bias_off = !component->driver->idle_bias_on;
4790 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4792 dapm->dev = card->dev;
4795 INIT_LIST_HEAD(&dapm->list);
4796 /* see for_each_card_dapms */
4797 list_add(&dapm->list, &card->dapm_list);
4799 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4801 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4803 struct snd_soc_card *card = dapm->card;
4804 struct snd_soc_dapm_widget *w;
4805 LIST_HEAD(down_list);
4808 mutex_lock(&card->dapm_mutex);
4810 for_each_card_widgets(dapm->card, w) {
4811 if (w->dapm != dapm)
4814 dapm_seq_insert(w, &down_list, false);
4820 /* If there were no widgets to power down we're already in
4824 if (dapm->bias_level == SND_SOC_BIAS_ON)
4825 snd_soc_dapm_set_bias_level(dapm,
4826 SND_SOC_BIAS_PREPARE);
4827 dapm_seq_run(card, &down_list, 0, false);
4828 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4829 snd_soc_dapm_set_bias_level(dapm,
4830 SND_SOC_BIAS_STANDBY);
4833 mutex_unlock(&card->dapm_mutex);
4837 * snd_soc_dapm_shutdown - callback for system shutdown
4839 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4841 struct snd_soc_dapm_context *dapm;
4843 for_each_card_dapms(card, dapm) {
4844 if (dapm != &card->dapm) {
4845 soc_dapm_shutdown_dapm(dapm);
4846 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4847 snd_soc_dapm_set_bias_level(dapm,
4852 soc_dapm_shutdown_dapm(&card->dapm);
4853 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4854 snd_soc_dapm_set_bias_level(&card->dapm,
4858 /* Module information */
4859 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4860 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4861 MODULE_LICENSE("GPL");