1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 for_each_card_widgets(card, w) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
343 struct dapm_kcontrol_data {
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 INIT_LIST_HEAD(&data->paths);
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
391 template.off_val = mc->max;
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
398 data->value = template.on_val;
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
433 data->value = template.on_val;
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
451 kcontrol->private_data = data;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
485 n = data->wlist->num_widgets + 1;
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
550 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
551 case snd_soc_dapm_switch:
552 case snd_soc_dapm_mixer:
553 case snd_soc_dapm_mixer_named_ctl:
554 data->widget->on_val = value & data->widget->mask;
556 case snd_soc_dapm_demux:
557 case snd_soc_dapm_mux:
558 data->widget->on_val = value >> data->widget->shift;
561 data->widget->on_val = value;
572 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
574 * @kcontrol: The kcontrol
576 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
577 struct snd_kcontrol *kcontrol)
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
581 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
584 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
586 * @kcontrol: The kcontrol
588 * Note: This function must only be used on kcontrols that are known to have
589 * been registered for a CODEC. Otherwise the behaviour is undefined.
591 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
592 struct snd_kcontrol *kcontrol)
594 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
596 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
598 static void dapm_reset(struct snd_soc_card *card)
600 struct snd_soc_dapm_widget *w;
602 lockdep_assert_held(&card->dapm_mutex);
604 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
606 for_each_card_widgets(card, w) {
607 w->new_power = w->power;
608 w->power_checked = false;
612 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
614 if (!dapm->component)
616 return dapm->component->name_prefix;
619 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
621 if (!dapm->component)
623 return snd_soc_component_read(dapm->component, reg);
626 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
627 int reg, unsigned int mask, unsigned int value)
629 if (!dapm->component)
631 return snd_soc_component_update_bits(dapm->component, reg,
635 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
636 int reg, unsigned int mask, unsigned int value)
638 if (!dapm->component)
640 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
643 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
646 snd_soc_component_async_complete(dapm->component);
649 static struct snd_soc_dapm_widget *
650 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
652 struct snd_soc_dapm_widget *w = wcache->widget;
655 struct list_head *wlist = &w->dapm->card->widgets;
659 list_for_each_entry_from(w, wlist, list) {
660 if (!strcmp(name, w->name))
671 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
672 struct snd_soc_dapm_widget *w)
678 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
679 * @dapm: The DAPM context for which to set the level
680 * @level: The level to set
682 * Forces the DAPM bias level to a specific state. It will call the bias level
683 * callback of DAPM context with the specified level. This will even happen if
684 * the context is already at the same level. Furthermore it will not go through
685 * the normal bias level sequencing, meaning any intermediate states between the
686 * current and the target state will not be entered.
688 * Note that the change in bias level is only temporary and the next time
689 * snd_soc_dapm_sync() is called the state will be set to the level as
690 * determined by the DAPM core. The function is mainly intended to be used to
691 * used during probe or resume from suspend to power up the device so
692 * initialization can be done, before the DAPM core takes over.
694 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
695 enum snd_soc_bias_level level)
700 ret = snd_soc_component_set_bias_level(dapm->component, level);
703 dapm->bias_level = level;
707 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
710 * snd_soc_dapm_set_bias_level - set the bias level for the system
711 * @dapm: DAPM context
712 * @level: level to configure
714 * Configure the bias (power) levels for the SoC audio device.
716 * Returns 0 for success else error.
718 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
719 enum snd_soc_bias_level level)
721 struct snd_soc_card *card = dapm->card;
724 trace_snd_soc_bias_level_start(card, level);
726 ret = snd_soc_card_set_bias_level(card, dapm, level);
730 if (!card || dapm != &card->dapm)
731 ret = snd_soc_dapm_force_bias_level(dapm, level);
736 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
738 trace_snd_soc_bias_level_done(card, level);
743 /* connect mux widget to its interconnecting audio paths */
744 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
745 struct snd_soc_dapm_path *path, const char *control_name,
746 struct snd_soc_dapm_widget *w)
748 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
749 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
753 if (e->reg != SND_SOC_NOPM) {
755 val = soc_dapm_read(dapm, e->reg);
756 val = (val >> e->shift_l) & e->mask;
757 item = snd_soc_enum_val_to_item(e, val);
759 /* since a virtual mux has no backing registers to
760 * decide which path to connect, it will try to match
761 * with the first enumeration. This is to ensure
762 * that the default mux choice (the first) will be
763 * correctly powered up during initialization.
768 i = match_string(e->texts, e->items, control_name);
772 path->name = e->texts[i];
773 path->connect = (i == item);
778 /* set up initial codec paths */
779 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
783 p->sink->kcontrol_news[i].private_value;
784 unsigned int reg = mc->reg;
785 unsigned int invert = mc->invert;
787 if (reg != SND_SOC_NOPM) {
788 unsigned int shift = mc->shift;
789 unsigned int max = mc->max;
790 unsigned int mask = (1 << fls(max)) - 1;
791 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
807 val = soc_dapm_read(p->sink->dapm, mc->rreg);
808 val = (val >> mc->rshift) & mask;
810 val = (val >> shift) & mask;
816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
826 /* connect mixer widget to its interconnecting audio paths */
827 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
828 struct snd_soc_dapm_path *path, const char *control_name)
832 /* search for mixer kcontrol */
833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
836 dapm_set_mixer_path_status(path, i, nth_path++);
843 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
844 struct snd_soc_dapm_widget *kcontrolw,
845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
848 struct snd_soc_dapm_widget *w;
853 for_each_card_widgets(dapm->card, w) {
854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
859 *kcontrol = w->kcontrols[i];
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
872 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
875 struct snd_soc_dapm_context *dapm = w->dapm;
876 struct snd_card *card = dapm->card->snd_card;
880 struct snd_kcontrol *kcontrol;
881 bool wname_in_long_name, kcname_in_long_name;
882 char *long_name = NULL;
886 prefix = soc_dapm_prefix(dapm);
888 prefix_len = strlen(prefix) + 1;
892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
903 case snd_soc_dapm_pga:
904 case snd_soc_dapm_effect:
905 case snd_soc_dapm_out_drv:
906 wname_in_long_name = true;
907 kcname_in_long_name = true;
909 case snd_soc_dapm_mixer_named_ctl:
910 wname_in_long_name = false;
911 kcname_in_long_name = true;
913 case snd_soc_dapm_demux:
914 case snd_soc_dapm_mux:
915 wname_in_long_name = true;
916 kcname_in_long_name = false;
923 if (wname_in_long_name && kcname_in_long_name) {
925 * The control will get a prefix from the control
926 * creation process but we're also using the same
927 * prefix for widgets so cut the prefix off the
928 * front of the widget name.
930 long_name = kasprintf(GFP_KERNEL, "%s %s",
931 w->name + prefix_len,
932 w->kcontrol_news[kci].name);
933 if (long_name == NULL)
937 } else if (wname_in_long_name) {
939 name = w->name + prefix_len;
942 name = w->kcontrol_news[kci].name;
945 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
952 kcontrol->private_free = dapm_kcontrol_free;
954 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
956 snd_ctl_free_one(kcontrol);
960 ret = snd_ctl_add(card, kcontrol);
963 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
969 ret = dapm_kcontrol_add_widget(kcontrol, w);
971 w->kcontrols[kci] = kcontrol;
979 /* create new dapm mixer control */
980 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
983 struct snd_soc_dapm_path *path;
984 struct dapm_kcontrol_data *data;
987 for (i = 0; i < w->num_kcontrols; i++) {
989 snd_soc_dapm_widget_for_each_source_path(w, path) {
990 /* mixer/mux paths name must match control name */
991 if (path->name != (char *)w->kcontrol_news[i].name)
994 if (!w->kcontrols[i]) {
995 ret = dapm_create_or_share_kcontrol(w, i);
1000 dapm_kcontrol_add_path(w->kcontrols[i], path);
1002 data = snd_kcontrol_chip(w->kcontrols[i]);
1004 snd_soc_dapm_add_path(data->widget->dapm,
1014 /* create new dapm mux control */
1015 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1017 struct snd_soc_dapm_context *dapm = w->dapm;
1018 enum snd_soc_dapm_direction dir;
1019 struct snd_soc_dapm_path *path;
1024 case snd_soc_dapm_mux:
1025 dir = SND_SOC_DAPM_DIR_OUT;
1028 case snd_soc_dapm_demux:
1029 dir = SND_SOC_DAPM_DIR_IN;
1036 if (w->num_kcontrols != 1) {
1038 "ASoC: %s %s has incorrect number of controls\n", type,
1043 if (list_empty(&w->edges[dir])) {
1044 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1048 ret = dapm_create_or_share_kcontrol(w, 0);
1052 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1054 dapm_kcontrol_add_path(w->kcontrols[0], path);
1060 /* create new dapm volume control */
1061 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1065 for (i = 0; i < w->num_kcontrols; i++) {
1066 int ret = dapm_create_or_share_kcontrol(w, i);
1074 /* create new dapm dai link control */
1075 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1078 struct snd_soc_pcm_runtime *rtd = w->priv;
1080 /* create control for links with > 1 config */
1081 if (rtd->dai_link->num_params <= 1)
1085 for (i = 0; i < w->num_kcontrols; i++) {
1086 struct snd_soc_dapm_context *dapm = w->dapm;
1087 struct snd_card *card = dapm->card->snd_card;
1088 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1090 int ret = snd_ctl_add(card, kcontrol);
1094 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1095 w->name, w->kcontrol_news[i].name, ret);
1098 kcontrol->private_data = w;
1099 w->kcontrols[i] = kcontrol;
1105 /* We implement power down on suspend by checking the power state of
1106 * the ALSA card - when we are suspending the ALSA state for the card
1109 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1111 int level = snd_power_get_state(widget->dapm->card->snd_card);
1114 case SNDRV_CTL_POWER_D3hot:
1115 case SNDRV_CTL_POWER_D3cold:
1116 if (widget->ignore_suspend)
1117 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1119 return widget->ignore_suspend;
1125 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1130 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1131 struct list_head *widgets)
1133 struct snd_soc_dapm_widget *w;
1134 struct list_head *it;
1135 unsigned int size = 0;
1138 list_for_each(it, widgets)
1141 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1145 list_for_each_entry(w, widgets, work_list)
1146 (*list)->widgets[i++] = w;
1148 (*list)->num_widgets = i;
1154 * Recursively reset the cached number of inputs or outputs for the specified
1155 * widget and all widgets that can be reached via incoming or outcoming paths
1158 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1159 enum snd_soc_dapm_direction dir)
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1164 widget->endpoints[dir] = -1;
1166 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1167 if (path->weak || path->is_supply)
1173 if (path->connect) {
1175 invalidate_paths_ep(path->node[dir], dir);
1182 * Common implementation for is_connected_output_ep() and
1183 * is_connected_input_ep(). The function is inlined since the combined size of
1184 * the two specialized functions is only marginally larger then the size of the
1185 * generic function and at the same time the fast path of the specialized
1186 * functions is significantly smaller than the generic function.
1188 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1189 struct list_head *list, enum snd_soc_dapm_direction dir,
1190 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1191 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1192 enum snd_soc_dapm_direction)),
1193 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1194 enum snd_soc_dapm_direction))
1196 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1197 struct snd_soc_dapm_path *path;
1200 if (widget->endpoints[dir] >= 0)
1201 return widget->endpoints[dir];
1203 DAPM_UPDATE_STAT(widget, path_checks);
1205 /* do we need to add this widget to the list ? */
1207 list_add_tail(&widget->work_list, list);
1209 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1211 custom_stop_condition = NULL;
1214 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1215 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1216 return widget->endpoints[dir];
1219 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1220 DAPM_UPDATE_STAT(widget, neighbour_checks);
1222 if (path->weak || path->is_supply)
1228 trace_snd_soc_dapm_path(widget, dir, path);
1230 if (path->connect) {
1232 con += fn(path->node[dir], list, custom_stop_condition);
1237 widget->endpoints[dir] = con;
1243 * Recursively check for a completed path to an active or physically connected
1244 * output widget. Returns number of complete paths.
1246 * Optionally, can be supplied with a function acting as a stopping condition.
1247 * This function takes the dapm widget currently being examined and the walk
1248 * direction as an arguments, it should return true if widgets from that point
1249 * in the graph onwards should not be added to the widget list.
1251 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1252 struct list_head *list,
1253 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1254 enum snd_soc_dapm_direction))
1256 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1257 is_connected_output_ep, custom_stop_condition);
1261 * Recursively check for a completed path to an active or physically connected
1262 * input widget. Returns number of complete paths.
1264 * Optionally, can be supplied with a function acting as a stopping condition.
1265 * This function takes the dapm widget currently being examined and the walk
1266 * direction as an arguments, it should return true if the walk should be
1267 * stopped and false otherwise.
1269 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1270 struct list_head *list,
1271 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1272 enum snd_soc_dapm_direction))
1274 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1275 is_connected_input_ep, custom_stop_condition);
1279 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1280 * @dai: the soc DAI.
1281 * @stream: stream direction.
1282 * @list: list of active widgets for this stream.
1283 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1284 * walk based on custom logic.
1286 * Queries DAPM graph as to whether a valid audio stream path exists for
1287 * the initial stream specified by name. This takes into account
1288 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1290 * Optionally, can be supplied with a function acting as a stopping condition.
1291 * This function takes the dapm widget currently being examined and the walk
1292 * direction as an arguments, it should return true if the walk should be
1293 * stopped and false otherwise.
1295 * Returns the number of valid paths or negative error.
1297 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1298 struct snd_soc_dapm_widget_list **list,
1299 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1300 enum snd_soc_dapm_direction))
1302 struct snd_soc_card *card = dai->component->card;
1303 struct snd_soc_dapm_widget *w;
1308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1310 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1311 w = dai->playback_widget;
1312 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1313 paths = is_connected_output_ep(w, &widgets,
1314 custom_stop_condition);
1316 w = dai->capture_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1318 paths = is_connected_input_ep(w, &widgets,
1319 custom_stop_condition);
1322 /* Drop starting point */
1323 list_del(widgets.next);
1325 ret = dapm_widget_list_create(list, &widgets);
1329 trace_snd_soc_dapm_connected(paths, stream);
1330 mutex_unlock(&card->dapm_mutex);
1335 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1337 dapm_widget_list_free(list);
1341 * Handler for regulator supply widget.
1343 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1344 struct snd_kcontrol *kcontrol, int event)
1348 soc_dapm_async_complete(w->dapm);
1350 if (SND_SOC_DAPM_EVENT_ON(event)) {
1351 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1352 ret = regulator_allow_bypass(w->regulator, false);
1354 dev_warn(w->dapm->dev,
1355 "ASoC: Failed to unbypass %s: %d\n",
1359 return regulator_enable(w->regulator);
1361 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1362 ret = regulator_allow_bypass(w->regulator, true);
1364 dev_warn(w->dapm->dev,
1365 "ASoC: Failed to bypass %s: %d\n",
1369 return regulator_disable_deferred(w->regulator, w->shift);
1372 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1375 * Handler for pinctrl widget.
1377 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1378 struct snd_kcontrol *kcontrol, int event)
1380 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1381 struct pinctrl *p = w->pinctrl;
1382 struct pinctrl_state *s;
1387 if (SND_SOC_DAPM_EVENT_ON(event))
1388 s = pinctrl_lookup_state(p, priv->active_state);
1390 s = pinctrl_lookup_state(p, priv->sleep_state);
1395 return pinctrl_select_state(p, s);
1397 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1400 * Handler for clock supply widget.
1402 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1403 struct snd_kcontrol *kcontrol, int event)
1408 soc_dapm_async_complete(w->dapm);
1410 if (SND_SOC_DAPM_EVENT_ON(event)) {
1411 return clk_prepare_enable(w->clk);
1413 clk_disable_unprepare(w->clk);
1419 EXPORT_SYMBOL_GPL(dapm_clock_event);
1421 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1423 if (w->power_checked)
1424 return w->new_power;
1429 w->new_power = w->power_check(w);
1431 w->power_checked = true;
1433 return w->new_power;
1436 /* Generic check to see if a widget should be powered. */
1437 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1441 DAPM_UPDATE_STAT(w, power_checks);
1443 in = is_connected_input_ep(w, NULL, NULL);
1444 out = is_connected_output_ep(w, NULL, NULL);
1445 return out != 0 && in != 0;
1448 /* Check to see if a power supply is needed */
1449 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1451 struct snd_soc_dapm_path *path;
1453 DAPM_UPDATE_STAT(w, power_checks);
1455 /* Check if one of our outputs is connected */
1456 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1457 DAPM_UPDATE_STAT(w, neighbour_checks);
1462 if (path->connected &&
1463 !path->connected(path->source, path->sink))
1466 if (dapm_widget_power_check(path->sink))
1473 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1475 return w->connected;
1478 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1479 struct snd_soc_dapm_widget *b,
1484 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1485 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1490 sort = dapm_down_seq;
1492 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1493 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1495 if (sort[a->id] != sort[b->id])
1496 return sort[a->id] - sort[b->id];
1497 if (a->subseq != b->subseq) {
1499 return a->subseq - b->subseq;
1501 return b->subseq - a->subseq;
1503 if (a->reg != b->reg)
1504 return a->reg - b->reg;
1505 if (a->dapm != b->dapm)
1506 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1511 /* Insert a widget in order into a DAPM power sequence. */
1512 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1513 struct list_head *list,
1516 struct snd_soc_dapm_widget *w;
1518 list_for_each_entry(w, list, power_list)
1519 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1520 list_add_tail(&new_widget->power_list, &w->power_list);
1524 list_add_tail(&new_widget->power_list, list);
1527 static void dapm_seq_check_event(struct snd_soc_card *card,
1528 struct snd_soc_dapm_widget *w, int event)
1530 const char *ev_name;
1534 case SND_SOC_DAPM_PRE_PMU:
1535 ev_name = "PRE_PMU";
1538 case SND_SOC_DAPM_POST_PMU:
1539 ev_name = "POST_PMU";
1542 case SND_SOC_DAPM_PRE_PMD:
1543 ev_name = "PRE_PMD";
1546 case SND_SOC_DAPM_POST_PMD:
1547 ev_name = "POST_PMD";
1550 case SND_SOC_DAPM_WILL_PMU:
1551 ev_name = "WILL_PMU";
1554 case SND_SOC_DAPM_WILL_PMD:
1555 ev_name = "WILL_PMD";
1559 WARN(1, "Unknown event %d\n", event);
1563 if (w->new_power != power)
1566 if (w->event && (w->event_flags & event)) {
1569 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1571 soc_dapm_async_complete(w->dapm);
1572 trace_snd_soc_dapm_widget_event_start(w, event);
1573 ret = w->event(w, NULL, event);
1574 trace_snd_soc_dapm_widget_event_done(w, event);
1576 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1577 ev_name, w->name, ret);
1581 /* Apply the coalesced changes from a DAPM sequence */
1582 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1583 struct list_head *pending)
1585 struct snd_soc_dapm_context *dapm;
1586 struct snd_soc_dapm_widget *w;
1588 unsigned int value = 0;
1589 unsigned int mask = 0;
1591 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1595 list_for_each_entry(w, pending, power_list) {
1596 WARN_ON(reg != w->reg || dapm != w->dapm);
1597 w->power = w->new_power;
1599 mask |= w->mask << w->shift;
1601 value |= w->on_val << w->shift;
1603 value |= w->off_val << w->shift;
1605 pop_dbg(dapm->dev, card->pop_time,
1606 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1607 w->name, reg, value, mask);
1609 /* Check for events */
1610 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1611 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1615 /* Any widget will do, they should all be updating the
1619 pop_dbg(dapm->dev, card->pop_time,
1620 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1621 value, mask, reg, card->pop_time);
1622 pop_wait(card->pop_time);
1623 soc_dapm_update_bits(dapm, reg, mask, value);
1626 list_for_each_entry(w, pending, power_list) {
1627 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1628 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1632 /* Apply a DAPM power sequence.
1634 * We walk over a pre-sorted list of widgets to apply power to. In
1635 * order to minimise the number of writes to the device required
1636 * multiple widgets will be updated in a single write where possible.
1637 * Currently anything that requires more than a single write is not
1640 static void dapm_seq_run(struct snd_soc_card *card,
1641 struct list_head *list, int event, bool power_up)
1643 struct snd_soc_dapm_widget *w, *n;
1644 struct snd_soc_dapm_context *d;
1647 int cur_subseq = -1;
1648 int cur_reg = SND_SOC_NOPM;
1649 struct snd_soc_dapm_context *cur_dapm = NULL;
1656 sort = dapm_down_seq;
1658 list_for_each_entry_safe(w, n, list, power_list) {
1661 /* Do we need to apply any queued changes? */
1662 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1663 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1664 if (!list_empty(&pending))
1665 dapm_seq_run_coalesced(card, &pending);
1667 if (cur_dapm && cur_dapm->component) {
1668 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1669 if (sort[i] == cur_sort)
1670 snd_soc_component_seq_notifier(
1671 cur_dapm->component,
1675 if (cur_dapm && w->dapm != cur_dapm)
1676 soc_dapm_async_complete(cur_dapm);
1678 INIT_LIST_HEAD(&pending);
1680 cur_subseq = INT_MIN;
1681 cur_reg = SND_SOC_NOPM;
1686 case snd_soc_dapm_pre:
1690 if (event == SND_SOC_DAPM_STREAM_START)
1692 NULL, SND_SOC_DAPM_PRE_PMU);
1693 else if (event == SND_SOC_DAPM_STREAM_STOP)
1695 NULL, SND_SOC_DAPM_PRE_PMD);
1698 case snd_soc_dapm_post:
1702 if (event == SND_SOC_DAPM_STREAM_START)
1704 NULL, SND_SOC_DAPM_POST_PMU);
1705 else if (event == SND_SOC_DAPM_STREAM_STOP)
1707 NULL, SND_SOC_DAPM_POST_PMD);
1711 /* Queue it up for application */
1712 cur_sort = sort[w->id];
1713 cur_subseq = w->subseq;
1716 list_move(&w->power_list, &pending);
1721 dev_err(w->dapm->dev,
1722 "ASoC: Failed to apply widget power: %d\n", ret);
1725 if (!list_empty(&pending))
1726 dapm_seq_run_coalesced(card, &pending);
1728 if (cur_dapm && cur_dapm->component) {
1729 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1730 if (sort[i] == cur_sort)
1731 snd_soc_component_seq_notifier(
1732 cur_dapm->component,
1736 for_each_card_dapms(card, d)
1737 soc_dapm_async_complete(d);
1740 static void dapm_widget_update(struct snd_soc_card *card)
1742 struct snd_soc_dapm_update *update = card->update;
1743 struct snd_soc_dapm_widget_list *wlist;
1744 struct snd_soc_dapm_widget *w = NULL;
1748 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1751 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1753 for_each_dapm_widgets(wlist, wi, w) {
1754 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1755 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1757 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1765 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1768 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1771 if (update->has_second_set) {
1772 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1773 update->mask2, update->val2);
1775 dev_err(w->dapm->dev,
1776 "ASoC: %s DAPM update failed: %d\n",
1780 for_each_dapm_widgets(wlist, wi, w) {
1781 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1782 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1784 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1790 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1791 * they're changing state.
1793 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1795 struct snd_soc_dapm_context *d = data;
1798 /* If we're off and we're not supposed to go into STANDBY */
1799 if (d->bias_level == SND_SOC_BIAS_OFF &&
1800 d->target_bias_level != SND_SOC_BIAS_OFF) {
1801 if (d->dev && cookie)
1802 pm_runtime_get_sync(d->dev);
1804 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1807 "ASoC: Failed to turn on bias: %d\n", ret);
1810 /* Prepare for a transition to ON or away from ON */
1811 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1812 d->bias_level != SND_SOC_BIAS_ON) ||
1813 (d->target_bias_level != SND_SOC_BIAS_ON &&
1814 d->bias_level == SND_SOC_BIAS_ON)) {
1815 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1818 "ASoC: Failed to prepare bias: %d\n", ret);
1822 /* Async callback run prior to DAPM sequences - brings to their final
1825 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1827 struct snd_soc_dapm_context *d = data;
1830 /* If we just powered the last thing off drop to standby bias */
1831 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1832 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1833 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1834 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1836 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1840 /* If we're in standby and can support bias off then do that */
1841 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1842 d->target_bias_level == SND_SOC_BIAS_OFF) {
1843 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1845 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1848 if (d->dev && cookie)
1849 pm_runtime_put(d->dev);
1852 /* If we just powered up then move to active bias */
1853 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1854 d->target_bias_level == SND_SOC_BIAS_ON) {
1855 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1857 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1862 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1863 bool power, bool connect)
1865 /* If a connection is being made or broken then that update
1866 * will have marked the peer dirty, otherwise the widgets are
1867 * not connected and this update has no impact. */
1871 /* If the peer is already in the state we're moving to then we
1872 * won't have an impact on it. */
1873 if (power != peer->power)
1874 dapm_mark_dirty(peer, "peer state change");
1877 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1878 struct list_head *up_list,
1879 struct list_head *down_list)
1881 struct snd_soc_dapm_path *path;
1883 if (w->power == power)
1886 trace_snd_soc_dapm_widget_power(w, power);
1888 /* If we changed our power state perhaps our neigbours changed
1891 snd_soc_dapm_widget_for_each_source_path(w, path)
1892 dapm_widget_set_peer_power(path->source, power, path->connect);
1894 /* Supplies can't affect their outputs, only their inputs */
1895 if (!w->is_supply) {
1896 snd_soc_dapm_widget_for_each_sink_path(w, path)
1897 dapm_widget_set_peer_power(path->sink, power,
1902 dapm_seq_insert(w, up_list, true);
1904 dapm_seq_insert(w, down_list, false);
1907 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1908 struct list_head *up_list,
1909 struct list_head *down_list)
1914 case snd_soc_dapm_pre:
1915 dapm_seq_insert(w, down_list, false);
1917 case snd_soc_dapm_post:
1918 dapm_seq_insert(w, up_list, true);
1922 power = dapm_widget_power_check(w);
1924 dapm_widget_set_power(w, power, up_list, down_list);
1929 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1931 if (dapm->idle_bias_off)
1934 switch (snd_power_get_state(dapm->card->snd_card)) {
1935 case SNDRV_CTL_POWER_D3hot:
1936 case SNDRV_CTL_POWER_D3cold:
1937 return dapm->suspend_bias_off;
1946 * Scan each dapm widget for complete audio path.
1947 * A complete path is a route that has valid endpoints i.e.:-
1949 * o DAC to output pin.
1950 * o Input pin to ADC.
1951 * o Input pin to Output pin (bypass, sidetone)
1952 * o DAC to ADC (loopback).
1954 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1956 struct snd_soc_dapm_widget *w;
1957 struct snd_soc_dapm_context *d;
1959 LIST_HEAD(down_list);
1960 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1961 enum snd_soc_bias_level bias;
1964 lockdep_assert_held(&card->dapm_mutex);
1966 trace_snd_soc_dapm_start(card);
1968 for_each_card_dapms(card, d) {
1969 if (dapm_idle_bias_off(d))
1970 d->target_bias_level = SND_SOC_BIAS_OFF;
1972 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1977 /* Check which widgets we need to power and store them in
1978 * lists indicating if they should be powered up or down. We
1979 * only check widgets that have been flagged as dirty but note
1980 * that new widgets may be added to the dirty list while we
1983 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1984 dapm_power_one_widget(w, &up_list, &down_list);
1987 for_each_card_widgets(card, w) {
1989 case snd_soc_dapm_pre:
1990 case snd_soc_dapm_post:
1991 /* These widgets always need to be powered */
1994 list_del_init(&w->dirty);
2001 /* Supplies and micbiases only bring the
2002 * context up to STANDBY as unless something
2003 * else is active and passing audio they
2004 * generally don't require full power. Signal
2005 * generators are virtual pins and have no
2006 * power impact themselves.
2009 case snd_soc_dapm_siggen:
2010 case snd_soc_dapm_vmid:
2012 case snd_soc_dapm_supply:
2013 case snd_soc_dapm_regulator_supply:
2014 case snd_soc_dapm_pinctrl:
2015 case snd_soc_dapm_clock_supply:
2016 case snd_soc_dapm_micbias:
2017 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2018 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2021 d->target_bias_level = SND_SOC_BIAS_ON;
2028 /* Force all contexts in the card to the same bias state if
2029 * they're not ground referenced.
2031 bias = SND_SOC_BIAS_OFF;
2032 for_each_card_dapms(card, d)
2033 if (d->target_bias_level > bias)
2034 bias = d->target_bias_level;
2035 for_each_card_dapms(card, d)
2036 if (!dapm_idle_bias_off(d))
2037 d->target_bias_level = bias;
2039 trace_snd_soc_dapm_walk_done(card);
2041 /* Run card bias changes at first */
2042 dapm_pre_sequence_async(&card->dapm, 0);
2043 /* Run other bias changes in parallel */
2044 for_each_card_dapms(card, d) {
2045 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2046 async_schedule_domain(dapm_pre_sequence_async, d,
2049 async_synchronize_full_domain(&async_domain);
2051 list_for_each_entry(w, &down_list, power_list) {
2052 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2055 list_for_each_entry(w, &up_list, power_list) {
2056 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2059 /* Power down widgets first; try to avoid amplifying pops. */
2060 dapm_seq_run(card, &down_list, event, false);
2062 dapm_widget_update(card);
2065 dapm_seq_run(card, &up_list, event, true);
2067 /* Run all the bias changes in parallel */
2068 for_each_card_dapms(card, d) {
2069 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2070 async_schedule_domain(dapm_post_sequence_async, d,
2073 async_synchronize_full_domain(&async_domain);
2074 /* Run card bias changes at last */
2075 dapm_post_sequence_async(&card->dapm, 0);
2077 /* do we need to notify any clients that DAPM event is complete */
2078 for_each_card_dapms(card, d) {
2082 ret = snd_soc_component_stream_event(d->component, event);
2087 pop_dbg(card->dev, card->pop_time,
2088 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2089 pop_wait(card->pop_time);
2091 trace_snd_soc_dapm_done(card);
2096 #ifdef CONFIG_DEBUG_FS
2097 static ssize_t dapm_widget_power_read_file(struct file *file,
2098 char __user *user_buf,
2099 size_t count, loff_t *ppos)
2101 struct snd_soc_dapm_widget *w = file->private_data;
2102 struct snd_soc_card *card = w->dapm->card;
2103 enum snd_soc_dapm_direction dir, rdir;
2107 struct snd_soc_dapm_path *p = NULL;
2109 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2113 mutex_lock(&card->dapm_mutex);
2115 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2120 in = is_connected_input_ep(w, NULL, NULL);
2121 out = is_connected_output_ep(w, NULL, NULL);
2124 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2125 w->name, w->power ? "On" : "Off",
2126 w->force ? " (forced)" : "", in, out);
2129 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2130 " - R%d(0x%x) mask 0x%x",
2131 w->reg, w->reg, w->mask << w->shift);
2133 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2136 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2138 w->active ? "active" : "inactive");
2140 snd_soc_dapm_for_each_direction(dir) {
2141 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2142 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2143 if (p->connected && !p->connected(p->source, p->sink))
2149 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2150 " %s \"%s\" \"%s\"\n",
2151 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2152 p->name ? p->name : "static",
2153 p->node[rdir]->name);
2157 mutex_unlock(&card->dapm_mutex);
2159 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2165 static const struct file_operations dapm_widget_power_fops = {
2166 .open = simple_open,
2167 .read = dapm_widget_power_read_file,
2168 .llseek = default_llseek,
2171 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2172 size_t count, loff_t *ppos)
2174 struct snd_soc_dapm_context *dapm = file->private_data;
2177 switch (dapm->bias_level) {
2178 case SND_SOC_BIAS_ON:
2181 case SND_SOC_BIAS_PREPARE:
2182 level = "Prepare\n";
2184 case SND_SOC_BIAS_STANDBY:
2185 level = "Standby\n";
2187 case SND_SOC_BIAS_OFF:
2191 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2192 level = "Unknown\n";
2196 return simple_read_from_buffer(user_buf, count, ppos, level,
2200 static const struct file_operations dapm_bias_fops = {
2201 .open = simple_open,
2202 .read = dapm_bias_read_file,
2203 .llseek = default_llseek,
2206 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2207 struct dentry *parent)
2209 if (!parent || IS_ERR(parent))
2212 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2214 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2218 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2220 struct snd_soc_dapm_context *dapm = w->dapm;
2222 if (!dapm->debugfs_dapm || !w->name)
2225 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2226 &dapm_widget_power_fops);
2229 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2231 debugfs_remove_recursive(dapm->debugfs_dapm);
2232 dapm->debugfs_dapm = NULL;
2236 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2237 struct dentry *parent)
2241 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2245 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2252 * soc_dapm_connect_path() - Connects or disconnects a path
2253 * @path: The path to update
2254 * @connect: The new connect state of the path. True if the path is connected,
2255 * false if it is disconnected.
2256 * @reason: The reason why the path changed (for debugging only)
2258 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2259 bool connect, const char *reason)
2261 if (path->connect == connect)
2264 path->connect = connect;
2265 dapm_mark_dirty(path->source, reason);
2266 dapm_mark_dirty(path->sink, reason);
2267 dapm_path_invalidate(path);
2270 /* test and update the power status of a mux widget */
2271 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2272 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2274 struct snd_soc_dapm_path *path;
2278 lockdep_assert_held(&card->dapm_mutex);
2280 /* find dapm widget path assoc with kcontrol */
2281 dapm_kcontrol_for_each_path(path, kcontrol) {
2283 /* we now need to match the string in the enum to the path */
2284 if (e && !(strcmp(path->name, e->texts[mux])))
2289 soc_dapm_connect_path(path, connect, "mux update");
2293 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2298 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2299 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2300 struct snd_soc_dapm_update *update)
2302 struct snd_soc_card *card = dapm->card;
2305 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2306 card->update = update;
2307 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2308 card->update = NULL;
2309 mutex_unlock(&card->dapm_mutex);
2311 snd_soc_dpcm_runtime_update(card);
2314 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2316 /* test and update the power status of a mixer or switch widget */
2317 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2318 struct snd_kcontrol *kcontrol,
2319 int connect, int rconnect)
2321 struct snd_soc_dapm_path *path;
2324 lockdep_assert_held(&card->dapm_mutex);
2326 /* find dapm widget path assoc with kcontrol */
2327 dapm_kcontrol_for_each_path(path, kcontrol) {
2329 * Ideally this function should support any number of
2330 * paths and channels. But since kcontrols only come
2331 * in mono and stereo variants, we are limited to 2
2334 * The following code assumes for stereo controls the
2335 * first path (when 'found == 0') is the left channel,
2336 * and all remaining paths (when 'found == 1') are the
2339 * A stereo control is signified by a valid 'rconnect'
2340 * value, either 0 for unconnected, or >= 0 for connected.
2341 * This is chosen instead of using snd_soc_volsw_is_stereo,
2342 * so that the behavior of snd_soc_dapm_mixer_update_power
2343 * doesn't change even when the kcontrol passed in is
2346 * It passes 'connect' as the path connect status for
2347 * the left channel, and 'rconnect' for the right
2350 if (found && rconnect >= 0)
2351 soc_dapm_connect_path(path, rconnect, "mixer update");
2353 soc_dapm_connect_path(path, connect, "mixer update");
2358 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2363 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2364 struct snd_kcontrol *kcontrol, int connect,
2365 struct snd_soc_dapm_update *update)
2367 struct snd_soc_card *card = dapm->card;
2370 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2371 card->update = update;
2372 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2373 card->update = NULL;
2374 mutex_unlock(&card->dapm_mutex);
2376 snd_soc_dpcm_runtime_update(card);
2379 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2381 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2384 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2385 struct snd_soc_dapm_widget *w;
2387 char *state = "not set";
2389 /* card won't be set for the dummy component, as a spot fix
2390 * we're checking for that case specifically here but in future
2391 * we will ensure that the dummy component looks like others.
2396 for_each_card_widgets(cmpnt->card, w) {
2397 if (w->dapm != dapm)
2400 /* only display widgets that burn power */
2402 case snd_soc_dapm_hp:
2403 case snd_soc_dapm_mic:
2404 case snd_soc_dapm_spk:
2405 case snd_soc_dapm_line:
2406 case snd_soc_dapm_micbias:
2407 case snd_soc_dapm_dac:
2408 case snd_soc_dapm_adc:
2409 case snd_soc_dapm_pga:
2410 case snd_soc_dapm_effect:
2411 case snd_soc_dapm_out_drv:
2412 case snd_soc_dapm_mixer:
2413 case snd_soc_dapm_mixer_named_ctl:
2414 case snd_soc_dapm_supply:
2415 case snd_soc_dapm_regulator_supply:
2416 case snd_soc_dapm_pinctrl:
2417 case snd_soc_dapm_clock_supply:
2419 count += sprintf(buf + count, "%s: %s\n",
2420 w->name, w->power ? "On":"Off");
2427 switch (snd_soc_dapm_get_bias_level(dapm)) {
2428 case SND_SOC_BIAS_ON:
2431 case SND_SOC_BIAS_PREPARE:
2434 case SND_SOC_BIAS_STANDBY:
2437 case SND_SOC_BIAS_OFF:
2441 count += sprintf(buf + count, "PM State: %s\n", state);
2446 /* show dapm widget status in sys fs */
2447 static ssize_t dapm_widget_show(struct device *dev,
2448 struct device_attribute *attr, char *buf)
2450 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2451 struct snd_soc_dai *codec_dai;
2454 mutex_lock(&rtd->card->dapm_mutex);
2456 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2457 struct snd_soc_component *cmpnt = codec_dai->component;
2459 count += dapm_widget_show_component(cmpnt, buf + count);
2462 mutex_unlock(&rtd->card->dapm_mutex);
2467 static DEVICE_ATTR_RO(dapm_widget);
2469 struct attribute *soc_dapm_dev_attrs[] = {
2470 &dev_attr_dapm_widget.attr,
2474 static void dapm_free_path(struct snd_soc_dapm_path *path)
2476 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2477 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2478 list_del(&path->list_kcontrol);
2479 list_del(&path->list);
2483 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2485 struct snd_soc_dapm_path *p, *next_p;
2486 enum snd_soc_dapm_direction dir;
2489 list_del(&w->dirty);
2491 * remove source and sink paths associated to this widget.
2492 * While removing the path, remove reference to it from both
2493 * source and sink widgets so that path is removed only once.
2495 snd_soc_dapm_for_each_direction(dir) {
2496 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2500 kfree(w->kcontrols);
2501 kfree_const(w->name);
2502 kfree_const(w->sname);
2506 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2508 dapm->path_sink_cache.widget = NULL;
2509 dapm->path_source_cache.widget = NULL;
2512 /* free all dapm widgets and resources */
2513 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2515 struct snd_soc_dapm_widget *w, *next_w;
2517 for_each_card_widgets_safe(dapm->card, w, next_w) {
2518 if (w->dapm != dapm)
2520 snd_soc_dapm_free_widget(w);
2522 snd_soc_dapm_reset_cache(dapm);
2525 static struct snd_soc_dapm_widget *dapm_find_widget(
2526 struct snd_soc_dapm_context *dapm, const char *pin,
2527 bool search_other_contexts)
2529 struct snd_soc_dapm_widget *w;
2530 struct snd_soc_dapm_widget *fallback = NULL;
2531 char prefixed_pin[80];
2532 const char *pin_name;
2533 const char *prefix = soc_dapm_prefix(dapm);
2536 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2538 pin_name = prefixed_pin;
2543 for_each_card_widgets(dapm->card, w) {
2544 if (!strcmp(w->name, pin_name)) {
2545 if (w->dapm == dapm)
2552 if (search_other_contexts)
2559 * set the DAPM pin status:
2560 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2561 * error code; called from kcontrol put callback
2563 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2564 const char *pin, int status)
2566 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2569 dapm_assert_locked(dapm);
2572 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2576 if (w->connected != status) {
2577 dapm_mark_dirty(w, "pin configuration");
2578 dapm_widget_invalidate_input_paths(w);
2579 dapm_widget_invalidate_output_paths(w);
2583 w->connected = status;
2591 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2592 * called from several API functions below
2594 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2595 const char *pin, int status)
2597 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2599 return ret < 0 ? ret : 0;
2603 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2604 * @dapm: DAPM context
2606 * Walks all dapm audio paths and powers widgets according to their
2607 * stream or path usage.
2609 * Requires external locking.
2611 * Returns 0 for success.
2613 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2616 * Suppress early reports (eg, jacks syncing their state) to avoid
2617 * silly DAPM runs during card startup.
2619 if (!dapm->card || !dapm->card->instantiated)
2622 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2624 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2627 * snd_soc_dapm_sync - scan and power dapm paths
2628 * @dapm: DAPM context
2630 * Walks all dapm audio paths and powers widgets according to their
2631 * stream or path usage.
2633 * Returns 0 for success.
2635 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2639 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2640 ret = snd_soc_dapm_sync_unlocked(dapm);
2641 mutex_unlock(&dapm->card->dapm_mutex);
2644 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2646 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2647 struct snd_soc_dapm_widget *w,
2651 case snd_soc_dapm_aif_out:
2652 case snd_soc_dapm_aif_in:
2658 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2659 w->channel < channels ? "Connecting" : "Disconnecting",
2660 p->source->name, p->sink->name);
2662 if (w->channel < channels)
2663 soc_dapm_connect_path(p, true, "dai update");
2665 soc_dapm_connect_path(p, false, "dai update");
2670 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2671 struct snd_pcm_hw_params *params,
2672 struct snd_soc_dai *dai)
2674 int dir = substream->stream;
2675 int channels = params_channels(params);
2676 struct snd_soc_dapm_path *p;
2677 struct snd_soc_dapm_widget *w;
2680 w = snd_soc_dai_get_widget(dai, dir);
2685 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2686 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2688 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2689 ret = dapm_update_dai_chan(p, p->sink, channels);
2694 snd_soc_dapm_widget_for_each_source_path(w, p) {
2695 ret = dapm_update_dai_chan(p, p->source, channels);
2703 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2704 struct snd_pcm_hw_params *params,
2705 struct snd_soc_dai *dai)
2707 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2710 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2711 ret = dapm_update_dai_unlocked(substream, params, dai);
2712 mutex_unlock(&rtd->card->dapm_mutex);
2716 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2719 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2720 * @w: The widget for which to update the flags
2722 * Some widgets have a dynamic category which depends on which neighbors they
2723 * are connected to. This function update the category for these widgets.
2725 * This function must be called whenever a path is added or removed to a widget.
2727 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2729 enum snd_soc_dapm_direction dir;
2730 struct snd_soc_dapm_path *p;
2734 case snd_soc_dapm_input:
2735 /* On a fully routed card an input is never a source */
2736 if (w->dapm->card->fully_routed)
2738 ep = SND_SOC_DAPM_EP_SOURCE;
2739 snd_soc_dapm_widget_for_each_source_path(w, p) {
2740 if (p->source->id == snd_soc_dapm_micbias ||
2741 p->source->id == snd_soc_dapm_mic ||
2742 p->source->id == snd_soc_dapm_line ||
2743 p->source->id == snd_soc_dapm_output) {
2749 case snd_soc_dapm_output:
2750 /* On a fully routed card a output is never a sink */
2751 if (w->dapm->card->fully_routed)
2753 ep = SND_SOC_DAPM_EP_SINK;
2754 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2755 if (p->sink->id == snd_soc_dapm_spk ||
2756 p->sink->id == snd_soc_dapm_hp ||
2757 p->sink->id == snd_soc_dapm_line ||
2758 p->sink->id == snd_soc_dapm_input) {
2764 case snd_soc_dapm_line:
2766 snd_soc_dapm_for_each_direction(dir) {
2767 if (!list_empty(&w->edges[dir]))
2768 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2778 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2779 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2780 const char *control)
2782 bool dynamic_source = false;
2783 bool dynamic_sink = false;
2788 switch (source->id) {
2789 case snd_soc_dapm_demux:
2790 dynamic_source = true;
2797 case snd_soc_dapm_mux:
2798 case snd_soc_dapm_switch:
2799 case snd_soc_dapm_mixer:
2800 case snd_soc_dapm_mixer_named_ctl:
2801 dynamic_sink = true;
2807 if (dynamic_source && dynamic_sink) {
2809 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2810 source->name, control, sink->name);
2812 } else if (!dynamic_source && !dynamic_sink) {
2814 "Control not supported for path %s -> [%s] -> %s\n",
2815 source->name, control, sink->name);
2822 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2823 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2824 const char *control,
2825 int (*connected)(struct snd_soc_dapm_widget *source,
2826 struct snd_soc_dapm_widget *sink))
2828 struct snd_soc_dapm_widget *widgets[2];
2829 enum snd_soc_dapm_direction dir;
2830 struct snd_soc_dapm_path *path;
2833 if (wsink->is_supply && !wsource->is_supply) {
2835 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2836 wsource->name, wsink->name);
2840 if (connected && !wsource->is_supply) {
2842 "connected() callback only supported for supply widgets (%s -> %s)\n",
2843 wsource->name, wsink->name);
2847 if (wsource->is_supply && control) {
2849 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2850 wsource->name, control, wsink->name);
2854 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2858 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2862 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2863 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2864 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2865 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2867 path->connected = connected;
2868 INIT_LIST_HEAD(&path->list);
2869 INIT_LIST_HEAD(&path->list_kcontrol);
2871 if (wsource->is_supply || wsink->is_supply)
2872 path->is_supply = 1;
2874 /* connect static paths */
2875 if (control == NULL) {
2878 switch (wsource->id) {
2879 case snd_soc_dapm_demux:
2880 ret = dapm_connect_mux(dapm, path, control, wsource);
2888 switch (wsink->id) {
2889 case snd_soc_dapm_mux:
2890 ret = dapm_connect_mux(dapm, path, control, wsink);
2894 case snd_soc_dapm_switch:
2895 case snd_soc_dapm_mixer:
2896 case snd_soc_dapm_mixer_named_ctl:
2897 ret = dapm_connect_mixer(dapm, path, control);
2906 list_add(&path->list, &dapm->card->paths);
2907 snd_soc_dapm_for_each_direction(dir)
2908 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2910 snd_soc_dapm_for_each_direction(dir) {
2911 dapm_update_widget_flags(widgets[dir]);
2912 dapm_mark_dirty(widgets[dir], "Route added");
2915 if (dapm->card->instantiated && path->connect)
2916 dapm_path_invalidate(path);
2924 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2925 const struct snd_soc_dapm_route *route)
2927 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2928 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2931 char prefixed_sink[80];
2932 char prefixed_source[80];
2934 unsigned int sink_ref = 0;
2935 unsigned int source_ref = 0;
2938 prefix = soc_dapm_prefix(dapm);
2940 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2941 prefix, route->sink);
2942 sink = prefixed_sink;
2943 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2944 prefix, route->source);
2945 source = prefixed_source;
2948 source = route->source;
2951 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2952 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2954 if (wsink && wsource)
2958 * find src and dest widgets over all widgets but favor a widget from
2959 * current DAPM context
2961 for_each_card_widgets(dapm->card, w) {
2962 if (!wsink && !(strcmp(w->name, sink))) {
2964 if (w->dapm == dapm) {
2972 "ASoC: sink widget %s overwritten\n",
2976 if (!wsource && !(strcmp(w->name, source))) {
2978 if (w->dapm == dapm) {
2986 "ASoC: source widget %s overwritten\n",
2990 /* use widget from another DAPM context if not found from this */
2996 if (wsource == NULL) {
2997 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3001 if (wsink == NULL) {
3002 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3008 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3009 dapm_wcache_update(&dapm->path_source_cache, wsource);
3011 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3018 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
3019 source, route->control, sink);
3023 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3024 const struct snd_soc_dapm_route *route)
3026 struct snd_soc_dapm_path *path, *p;
3029 char prefixed_sink[80];
3030 char prefixed_source[80];
3033 if (route->control) {
3035 "ASoC: Removal of routes with controls not supported\n");
3039 prefix = soc_dapm_prefix(dapm);
3041 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3042 prefix, route->sink);
3043 sink = prefixed_sink;
3044 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3045 prefix, route->source);
3046 source = prefixed_source;
3049 source = route->source;
3053 list_for_each_entry(p, &dapm->card->paths, list) {
3054 if (strcmp(p->source->name, source) != 0)
3056 if (strcmp(p->sink->name, sink) != 0)
3063 struct snd_soc_dapm_widget *wsource = path->source;
3064 struct snd_soc_dapm_widget *wsink = path->sink;
3066 dapm_mark_dirty(wsource, "Route removed");
3067 dapm_mark_dirty(wsink, "Route removed");
3069 dapm_path_invalidate(path);
3071 dapm_free_path(path);
3073 /* Update any path related flags */
3074 dapm_update_widget_flags(wsource);
3075 dapm_update_widget_flags(wsink);
3077 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3085 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3086 * @dapm: DAPM context
3087 * @route: audio routes
3088 * @num: number of routes
3090 * Connects 2 dapm widgets together via a named audio path. The sink is
3091 * the widget receiving the audio signal, whilst the source is the sender
3092 * of the audio signal.
3094 * Returns 0 for success else error. On error all resources can be freed
3095 * with a call to snd_soc_card_free().
3097 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3098 const struct snd_soc_dapm_route *route, int num)
3102 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3103 for (i = 0; i < num; i++) {
3104 int r = snd_soc_dapm_add_route(dapm, route);
3106 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3108 route->control ? route->control : "direct",
3114 mutex_unlock(&dapm->card->dapm_mutex);
3118 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3121 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3122 * @dapm: DAPM context
3123 * @route: audio routes
3124 * @num: number of routes
3126 * Removes routes from the DAPM context.
3128 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3129 const struct snd_soc_dapm_route *route, int num)
3133 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3134 for (i = 0; i < num; i++) {
3135 snd_soc_dapm_del_route(dapm, route);
3138 mutex_unlock(&dapm->card->dapm_mutex);
3142 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3144 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3145 const struct snd_soc_dapm_route *route)
3147 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3150 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3153 struct snd_soc_dapm_path *path;
3157 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3163 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3168 if (route->control || route->connected)
3169 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3170 route->source, route->sink);
3172 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3173 if (path->sink == sink) {
3180 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3181 route->source, route->sink);
3183 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3184 count, route->source, route->sink);
3190 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3191 * @dapm: DAPM context
3192 * @route: audio routes
3193 * @num: number of routes
3195 * Mark existing routes matching those specified in the passed array
3196 * as being weak, meaning that they are ignored for the purpose of
3197 * power decisions. The main intended use case is for sidetone paths
3198 * which couple audio between other independent paths if they are both
3199 * active in order to make the combination work better at the user
3200 * level but which aren't intended to be "used".
3202 * Note that CODEC drivers should not use this as sidetone type paths
3203 * can frequently also be used as bypass paths.
3205 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3206 const struct snd_soc_dapm_route *route, int num)
3211 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3212 for (i = 0; i < num; i++) {
3213 int err = snd_soc_dapm_weak_route(dapm, route);
3218 mutex_unlock(&dapm->card->dapm_mutex);
3222 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3225 * snd_soc_dapm_new_widgets - add new dapm widgets
3226 * @card: card to be checked for new dapm widgets
3228 * Checks the codec for any new dapm widgets and creates them if found.
3230 * Returns 0 for success.
3232 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3234 struct snd_soc_dapm_widget *w;
3237 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3239 for_each_card_widgets(card, w)
3244 if (w->num_kcontrols) {
3245 w->kcontrols = kcalloc(w->num_kcontrols,
3246 sizeof(struct snd_kcontrol *),
3248 if (!w->kcontrols) {
3249 mutex_unlock(&card->dapm_mutex);
3255 case snd_soc_dapm_switch:
3256 case snd_soc_dapm_mixer:
3257 case snd_soc_dapm_mixer_named_ctl:
3260 case snd_soc_dapm_mux:
3261 case snd_soc_dapm_demux:
3264 case snd_soc_dapm_pga:
3265 case snd_soc_dapm_effect:
3266 case snd_soc_dapm_out_drv:
3269 case snd_soc_dapm_dai_link:
3270 dapm_new_dai_link(w);
3276 /* Read the initial power state from the device */
3278 val = soc_dapm_read(w->dapm, w->reg);
3279 val = val >> w->shift;
3281 if (val == w->on_val)
3287 dapm_mark_dirty(w, "new widget");
3288 dapm_debugfs_add_widget(w);
3291 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3292 mutex_unlock(&card->dapm_mutex);
3295 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3298 * snd_soc_dapm_get_volsw - dapm mixer get callback
3299 * @kcontrol: mixer control
3300 * @ucontrol: control element information
3302 * Callback to get the value of a dapm mixer control.
3304 * Returns 0 for success.
3306 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3307 struct snd_ctl_elem_value *ucontrol)
3309 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3310 struct snd_soc_card *card = dapm->card;
3311 struct soc_mixer_control *mc =
3312 (struct soc_mixer_control *)kcontrol->private_value;
3314 unsigned int shift = mc->shift;
3316 unsigned int width = fls(max);
3317 unsigned int mask = (1 << fls(max)) - 1;
3318 unsigned int invert = mc->invert;
3319 unsigned int reg_val, val, rval = 0;
3321 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3322 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3323 reg_val = soc_dapm_read(dapm, reg);
3324 val = (reg_val >> shift) & mask;
3326 if (reg != mc->rreg)
3327 reg_val = soc_dapm_read(dapm, mc->rreg);
3329 if (snd_soc_volsw_is_stereo(mc))
3330 rval = (reg_val >> mc->rshift) & mask;
3332 reg_val = dapm_kcontrol_get_value(kcontrol);
3333 val = reg_val & mask;
3335 if (snd_soc_volsw_is_stereo(mc))
3336 rval = (reg_val >> width) & mask;
3338 mutex_unlock(&card->dapm_mutex);
3341 ucontrol->value.integer.value[0] = max - val;
3343 ucontrol->value.integer.value[0] = val;
3345 if (snd_soc_volsw_is_stereo(mc)) {
3347 ucontrol->value.integer.value[1] = max - rval;
3349 ucontrol->value.integer.value[1] = rval;
3354 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3357 * snd_soc_dapm_put_volsw - dapm mixer set callback
3358 * @kcontrol: mixer control
3359 * @ucontrol: control element information
3361 * Callback to set the value of a dapm mixer control.
3363 * Returns 0 for success.
3365 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_value *ucontrol)
3368 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3369 struct snd_soc_card *card = dapm->card;
3370 struct soc_mixer_control *mc =
3371 (struct soc_mixer_control *)kcontrol->private_value;
3373 unsigned int shift = mc->shift;
3375 unsigned int width = fls(max);
3376 unsigned int mask = (1 << width) - 1;
3377 unsigned int invert = mc->invert;
3378 unsigned int val, rval = 0;
3379 int connect, rconnect = -1, change, reg_change = 0;
3380 struct snd_soc_dapm_update update = {};
3383 val = (ucontrol->value.integer.value[0] & mask);
3389 if (snd_soc_volsw_is_stereo(mc)) {
3390 rval = (ucontrol->value.integer.value[1] & mask);
3396 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3398 /* This assumes field width < (bits in unsigned int / 2) */
3399 if (width > sizeof(unsigned int) * 8 / 2)
3401 "ASoC: control %s field width limit exceeded\n",
3403 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3405 if (reg != SND_SOC_NOPM) {
3407 rval = rval << mc->rshift;
3409 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3411 if (snd_soc_volsw_is_stereo(mc))
3412 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3417 if (change || reg_change) {
3419 if (snd_soc_volsw_is_stereo(mc)) {
3420 update.has_second_set = true;
3421 update.reg2 = mc->rreg;
3422 update.mask2 = mask << mc->rshift;
3425 update.kcontrol = kcontrol;
3427 update.mask = mask << shift;
3429 card->update = &update;
3432 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3435 card->update = NULL;
3438 mutex_unlock(&card->dapm_mutex);
3441 snd_soc_dpcm_runtime_update(card);
3445 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3448 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3449 * @kcontrol: mixer control
3450 * @ucontrol: control element information
3452 * Callback to get the value of a dapm enumerated double mixer control.
3454 * Returns 0 for success.
3456 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3457 struct snd_ctl_elem_value *ucontrol)
3459 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3460 struct snd_soc_card *card = dapm->card;
3461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3462 unsigned int reg_val, val;
3464 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3465 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3466 reg_val = soc_dapm_read(dapm, e->reg);
3468 reg_val = dapm_kcontrol_get_value(kcontrol);
3470 mutex_unlock(&card->dapm_mutex);
3472 val = (reg_val >> e->shift_l) & e->mask;
3473 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3474 if (e->shift_l != e->shift_r) {
3475 val = (reg_val >> e->shift_r) & e->mask;
3476 val = snd_soc_enum_val_to_item(e, val);
3477 ucontrol->value.enumerated.item[1] = val;
3482 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3485 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3486 * @kcontrol: mixer control
3487 * @ucontrol: control element information
3489 * Callback to set the value of a dapm enumerated double mixer control.
3491 * Returns 0 for success.
3493 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3494 struct snd_ctl_elem_value *ucontrol)
3496 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3497 struct snd_soc_card *card = dapm->card;
3498 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3499 unsigned int *item = ucontrol->value.enumerated.item;
3500 unsigned int val, change, reg_change = 0;
3502 struct snd_soc_dapm_update update = {};
3505 if (item[0] >= e->items)
3508 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3509 mask = e->mask << e->shift_l;
3510 if (e->shift_l != e->shift_r) {
3511 if (item[1] > e->items)
3513 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3514 mask |= e->mask << e->shift_r;
3517 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3519 change = dapm_kcontrol_set_value(kcontrol, val);
3521 if (e->reg != SND_SOC_NOPM)
3522 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3524 if (change || reg_change) {
3526 update.kcontrol = kcontrol;
3527 update.reg = e->reg;
3530 card->update = &update;
3533 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3535 card->update = NULL;
3538 mutex_unlock(&card->dapm_mutex);
3541 snd_soc_dpcm_runtime_update(card);
3545 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3548 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3550 * @kcontrol: mixer control
3551 * @uinfo: control element information
3553 * Callback to provide information about a pin switch control.
3555 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3556 struct snd_ctl_elem_info *uinfo)
3558 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3560 uinfo->value.integer.min = 0;
3561 uinfo->value.integer.max = 1;
3565 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3568 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3570 * @kcontrol: mixer control
3573 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3574 struct snd_ctl_elem_value *ucontrol)
3576 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3577 const char *pin = (const char *)kcontrol->private_value;
3579 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3581 ucontrol->value.integer.value[0] =
3582 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3584 mutex_unlock(&card->dapm_mutex);
3588 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3591 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3593 * @kcontrol: mixer control
3596 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3597 struct snd_ctl_elem_value *ucontrol)
3599 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3600 const char *pin = (const char *)kcontrol->private_value;
3603 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3604 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3605 !!ucontrol->value.integer.value[0]);
3606 mutex_unlock(&card->dapm_mutex);
3608 snd_soc_dapm_sync(&card->dapm);
3611 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3613 struct snd_soc_dapm_widget *
3614 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3615 const struct snd_soc_dapm_widget *widget)
3617 enum snd_soc_dapm_direction dir;
3618 struct snd_soc_dapm_widget *w;
3622 if ((w = dapm_cnew_widget(widget)) == NULL)
3623 return ERR_PTR(-ENOMEM);
3626 case snd_soc_dapm_regulator_supply:
3627 w->regulator = devm_regulator_get(dapm->dev, w->name);
3628 if (IS_ERR(w->regulator)) {
3629 ret = PTR_ERR(w->regulator);
3630 goto request_failed;
3633 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3634 ret = regulator_allow_bypass(w->regulator, true);
3637 "ASoC: Failed to bypass %s: %d\n",
3641 case snd_soc_dapm_pinctrl:
3642 w->pinctrl = devm_pinctrl_get(dapm->dev);
3643 if (IS_ERR(w->pinctrl)) {
3644 ret = PTR_ERR(w->pinctrl);
3645 goto request_failed;
3648 /* set to sleep_state when initializing */
3649 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3651 case snd_soc_dapm_clock_supply:
3652 w->clk = devm_clk_get(dapm->dev, w->name);
3653 if (IS_ERR(w->clk)) {
3654 ret = PTR_ERR(w->clk);
3655 goto request_failed;
3662 prefix = soc_dapm_prefix(dapm);
3664 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3666 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3667 if (w->name == NULL) {
3668 kfree_const(w->sname);
3670 return ERR_PTR(-ENOMEM);
3674 case snd_soc_dapm_mic:
3675 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3676 w->power_check = dapm_generic_check_power;
3678 case snd_soc_dapm_input:
3679 if (!dapm->card->fully_routed)
3680 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3681 w->power_check = dapm_generic_check_power;
3683 case snd_soc_dapm_spk:
3684 case snd_soc_dapm_hp:
3685 w->is_ep = SND_SOC_DAPM_EP_SINK;
3686 w->power_check = dapm_generic_check_power;
3688 case snd_soc_dapm_output:
3689 if (!dapm->card->fully_routed)
3690 w->is_ep = SND_SOC_DAPM_EP_SINK;
3691 w->power_check = dapm_generic_check_power;
3693 case snd_soc_dapm_vmid:
3694 case snd_soc_dapm_siggen:
3695 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3696 w->power_check = dapm_always_on_check_power;
3698 case snd_soc_dapm_sink:
3699 w->is_ep = SND_SOC_DAPM_EP_SINK;
3700 w->power_check = dapm_always_on_check_power;
3703 case snd_soc_dapm_mux:
3704 case snd_soc_dapm_demux:
3705 case snd_soc_dapm_switch:
3706 case snd_soc_dapm_mixer:
3707 case snd_soc_dapm_mixer_named_ctl:
3708 case snd_soc_dapm_adc:
3709 case snd_soc_dapm_aif_out:
3710 case snd_soc_dapm_dac:
3711 case snd_soc_dapm_aif_in:
3712 case snd_soc_dapm_pga:
3713 case snd_soc_dapm_buffer:
3714 case snd_soc_dapm_scheduler:
3715 case snd_soc_dapm_effect:
3716 case snd_soc_dapm_src:
3717 case snd_soc_dapm_asrc:
3718 case snd_soc_dapm_encoder:
3719 case snd_soc_dapm_decoder:
3720 case snd_soc_dapm_out_drv:
3721 case snd_soc_dapm_micbias:
3722 case snd_soc_dapm_line:
3723 case snd_soc_dapm_dai_link:
3724 case snd_soc_dapm_dai_out:
3725 case snd_soc_dapm_dai_in:
3726 w->power_check = dapm_generic_check_power;
3728 case snd_soc_dapm_supply:
3729 case snd_soc_dapm_regulator_supply:
3730 case snd_soc_dapm_pinctrl:
3731 case snd_soc_dapm_clock_supply:
3732 case snd_soc_dapm_kcontrol:
3734 w->power_check = dapm_supply_check_power;
3737 w->power_check = dapm_always_on_check_power;
3742 INIT_LIST_HEAD(&w->list);
3743 INIT_LIST_HEAD(&w->dirty);
3744 /* see for_each_card_widgets */
3745 list_add_tail(&w->list, &dapm->card->widgets);
3747 snd_soc_dapm_for_each_direction(dir) {
3748 INIT_LIST_HEAD(&w->edges[dir]);
3749 w->endpoints[dir] = -1;
3752 /* machine layer sets up unconnected pins and insertions */
3757 if (ret != -EPROBE_DEFER)
3758 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3761 kfree_const(w->sname);
3763 return ERR_PTR(ret);
3767 * snd_soc_dapm_new_control - create new dapm control
3768 * @dapm: DAPM context
3769 * @widget: widget template
3771 * Creates new DAPM control based upon a template.
3773 * Returns a widget pointer on success or an error pointer on failure
3775 struct snd_soc_dapm_widget *
3776 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3777 const struct snd_soc_dapm_widget *widget)
3779 struct snd_soc_dapm_widget *w;
3781 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3782 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3783 mutex_unlock(&dapm->card->dapm_mutex);
3787 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3790 * snd_soc_dapm_new_controls - create new dapm controls
3791 * @dapm: DAPM context
3792 * @widget: widget array
3793 * @num: number of widgets
3795 * Creates new DAPM controls based upon the templates.
3797 * Returns 0 for success else error.
3799 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3800 const struct snd_soc_dapm_widget *widget,
3806 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3807 for (i = 0; i < num; i++) {
3808 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3815 mutex_unlock(&dapm->card->dapm_mutex);
3818 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3821 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3822 struct snd_pcm_substream *substream)
3824 struct snd_soc_dapm_path *path;
3825 struct snd_soc_dai *source, *sink;
3826 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3827 struct snd_pcm_hw_params *params = NULL;
3828 const struct snd_soc_pcm_stream *config = NULL;
3829 struct snd_pcm_runtime *runtime = NULL;
3833 params = kzalloc(sizeof(*params), GFP_KERNEL);
3837 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3843 substream->runtime = runtime;
3845 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3846 snd_soc_dapm_widget_for_each_source_path(w, path) {
3847 source = path->source->priv;
3849 ret = snd_soc_dai_startup(source, substream);
3853 snd_soc_dai_activate(source, substream->stream);
3856 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3857 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3858 sink = path->sink->priv;
3860 ret = snd_soc_dai_startup(sink, substream);
3864 snd_soc_dai_activate(sink, substream->stream);
3867 substream->hw_opened = 1;
3870 * Note: getting the config after .startup() gives a chance to
3871 * either party on the link to alter the configuration if
3874 config = rtd->dai_link->params + rtd->params_select;
3875 if (WARN_ON(!config)) {
3876 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3881 /* Be a little careful as we don't want to overflow the mask array */
3882 if (config->formats) {
3883 fmt = ffs(config->formats) - 1;
3885 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3892 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3893 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3895 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3897 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3898 = config->channels_min;
3899 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3900 = config->channels_max;
3902 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3903 snd_soc_dapm_widget_for_each_source_path(w, path) {
3904 source = path->source->priv;
3906 ret = snd_soc_dai_hw_params(source, substream, params);
3910 dapm_update_dai_unlocked(substream, params, source);
3913 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3914 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3915 sink = path->sink->priv;
3917 ret = snd_soc_dai_hw_params(sink, substream, params);
3921 dapm_update_dai_unlocked(substream, params, sink);
3924 runtime->format = params_format(params);
3925 runtime->subformat = params_subformat(params);
3926 runtime->channels = params_channels(params);
3927 runtime->rate = params_rate(params);
3934 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3935 struct snd_kcontrol *kcontrol, int event)
3937 struct snd_soc_dapm_path *path;
3938 struct snd_soc_dai *source, *sink;
3939 struct snd_pcm_substream *substream = w->priv;
3940 int ret = 0, saved_stream = substream->stream;
3942 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3943 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3947 case SND_SOC_DAPM_PRE_PMU:
3948 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3954 case SND_SOC_DAPM_POST_PMU:
3955 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3956 sink = path->sink->priv;
3958 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3963 case SND_SOC_DAPM_PRE_PMD:
3964 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3965 sink = path->sink->priv;
3967 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3971 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3972 snd_soc_dapm_widget_for_each_source_path(w, path) {
3973 source = path->source->priv;
3974 snd_soc_dai_hw_free(source, substream, 0);
3977 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3978 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3979 sink = path->sink->priv;
3980 snd_soc_dai_hw_free(sink, substream, 0);
3983 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3984 snd_soc_dapm_widget_for_each_source_path(w, path) {
3985 source = path->source->priv;
3986 snd_soc_dai_deactivate(source, substream->stream);
3987 snd_soc_dai_shutdown(source, substream, 0);
3990 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3991 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3992 sink = path->sink->priv;
3993 snd_soc_dai_deactivate(sink, substream->stream);
3994 snd_soc_dai_shutdown(sink, substream, 0);
3998 case SND_SOC_DAPM_POST_PMD:
3999 kfree(substream->runtime);
4003 WARN(1, "Unknown event %d\n", event);
4008 /* Restore the substream direction */
4009 substream->stream = saved_stream;
4013 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4014 struct snd_ctl_elem_value *ucontrol)
4016 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4017 struct snd_soc_pcm_runtime *rtd = w->priv;
4019 ucontrol->value.enumerated.item[0] = rtd->params_select;
4024 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4025 struct snd_ctl_elem_value *ucontrol)
4027 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4028 struct snd_soc_pcm_runtime *rtd = w->priv;
4030 /* Can't change the config when widget is already powered */
4034 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4037 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4040 rtd->params_select = ucontrol->value.enumerated.item[0];
4046 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4047 unsigned long *private_value,
4049 const char **w_param_text)
4053 devm_kfree(card->dev, (void *)*private_value);
4058 for (count = 0 ; count < num_params; count++)
4059 devm_kfree(card->dev, (void *)w_param_text[count]);
4060 devm_kfree(card->dev, w_param_text);
4063 static struct snd_kcontrol_new *
4064 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4066 const struct snd_soc_pcm_stream *params,
4067 int num_params, const char **w_param_text,
4068 unsigned long *private_value)
4070 struct soc_enum w_param_enum[] = {
4071 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4073 struct snd_kcontrol_new kcontrol_dai_link[] = {
4074 SOC_ENUM_EXT(NULL, w_param_enum[0],
4075 snd_soc_dapm_dai_link_get,
4076 snd_soc_dapm_dai_link_put),
4078 struct snd_kcontrol_new *kcontrol_news;
4079 const struct snd_soc_pcm_stream *config = params;
4082 for (count = 0 ; count < num_params; count++) {
4083 if (!config->stream_name) {
4084 dev_warn(card->dapm.dev,
4085 "ASoC: anonymous config %d for dai link %s\n",
4087 w_param_text[count] =
4088 devm_kasprintf(card->dev, GFP_KERNEL,
4089 "Anonymous Configuration %d",
4092 w_param_text[count] = devm_kmemdup(card->dev,
4093 config->stream_name,
4094 strlen(config->stream_name) + 1,
4097 if (!w_param_text[count])
4098 goto outfree_w_param;
4102 w_param_enum[0].items = num_params;
4103 w_param_enum[0].texts = w_param_text;
4106 (unsigned long) devm_kmemdup(card->dev,
4107 (void *)(kcontrol_dai_link[0].private_value),
4108 sizeof(struct soc_enum), GFP_KERNEL);
4109 if (!*private_value) {
4110 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4112 goto outfree_w_param;
4114 kcontrol_dai_link[0].private_value = *private_value;
4115 /* duplicate kcontrol_dai_link on heap so that memory persists */
4116 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4117 sizeof(struct snd_kcontrol_new),
4119 if (!kcontrol_news) {
4120 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4122 goto outfree_w_param;
4124 return kcontrol_news;
4127 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4131 static struct snd_soc_dapm_widget *
4132 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4133 struct snd_pcm_substream *substream,
4136 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4137 struct snd_soc_dapm_widget template;
4138 struct snd_soc_dapm_widget *w;
4139 const char **w_param_text;
4140 unsigned long private_value = 0;
4144 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4145 rtd->dai_link->name, id);
4147 return ERR_PTR(-ENOMEM);
4149 memset(&template, 0, sizeof(template));
4150 template.reg = SND_SOC_NOPM;
4151 template.id = snd_soc_dapm_dai_link;
4152 template.name = link_name;
4153 template.event = snd_soc_dai_link_event;
4154 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4155 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4156 template.kcontrol_news = NULL;
4158 /* allocate memory for control, only in case of multiple configs */
4159 if (rtd->dai_link->num_params > 1) {
4160 w_param_text = devm_kcalloc(card->dev,
4161 rtd->dai_link->num_params,
4162 sizeof(char *), GFP_KERNEL);
4163 if (!w_param_text) {
4168 template.num_kcontrols = 1;
4169 template.kcontrol_news =
4170 snd_soc_dapm_alloc_kcontrol(card,
4172 rtd->dai_link->params,
4173 rtd->dai_link->num_params,
4174 w_param_text, &private_value);
4175 if (!template.kcontrol_news) {
4180 w_param_text = NULL;
4182 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4184 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4187 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4189 goto outfree_kcontrol_news;
4192 w->priv = substream;
4196 outfree_kcontrol_news:
4197 devm_kfree(card->dev, (void *)template.kcontrol_news);
4198 snd_soc_dapm_free_kcontrol(card, &private_value,
4199 rtd->dai_link->num_params, w_param_text);
4201 devm_kfree(card->dev, link_name);
4202 return ERR_PTR(ret);
4205 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4206 struct snd_soc_dai *dai)
4208 struct snd_soc_dapm_widget template;
4209 struct snd_soc_dapm_widget *w;
4211 WARN_ON(dapm->dev != dai->dev);
4213 memset(&template, 0, sizeof(template));
4214 template.reg = SND_SOC_NOPM;
4216 if (dai->driver->playback.stream_name) {
4217 template.id = snd_soc_dapm_dai_in;
4218 template.name = dai->driver->playback.stream_name;
4219 template.sname = dai->driver->playback.stream_name;
4221 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4224 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4229 dai->playback_widget = w;
4232 if (dai->driver->capture.stream_name) {
4233 template.id = snd_soc_dapm_dai_out;
4234 template.name = dai->driver->capture.stream_name;
4235 template.sname = dai->driver->capture.stream_name;
4237 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4240 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4245 dai->capture_widget = w;
4251 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4253 struct snd_soc_dapm_widget *dai_w, *w;
4254 struct snd_soc_dapm_widget *src, *sink;
4255 struct snd_soc_dai *dai;
4257 /* For each DAI widget... */
4258 for_each_card_widgets(card, dai_w) {
4259 switch (dai_w->id) {
4260 case snd_soc_dapm_dai_in:
4261 case snd_soc_dapm_dai_out:
4267 /* let users know there is no DAI to link */
4269 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4276 /* ...find all widgets with the same stream and link them */
4277 for_each_card_widgets(card, w) {
4278 if (w->dapm != dai_w->dapm)
4282 case snd_soc_dapm_dai_in:
4283 case snd_soc_dapm_dai_out:
4289 if (!w->sname || !strstr(w->sname, dai_w->sname))
4292 if (dai_w->id == snd_soc_dapm_dai_in) {
4299 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4300 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4307 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4308 struct snd_soc_dai *src_dai,
4309 struct snd_soc_dapm_widget *src,
4310 struct snd_soc_dapm_widget *dai,
4311 struct snd_soc_dai *sink_dai,
4312 struct snd_soc_dapm_widget *sink)
4314 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4315 src_dai->component->name, src->name,
4316 sink_dai->component->name, sink->name);
4319 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4323 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4326 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4327 struct snd_soc_pcm_runtime *rtd,
4328 struct snd_soc_dai *codec_dai,
4329 struct snd_soc_dai *cpu_dai)
4331 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4332 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4333 struct snd_pcm_substream *substream;
4334 struct snd_pcm_str *streams = rtd->pcm->streams;
4336 if (dai_link->params) {
4337 playback_cpu = cpu_dai->capture_widget;
4338 capture_cpu = cpu_dai->playback_widget;
4340 playback_cpu = cpu_dai->playback_widget;
4341 capture_cpu = cpu_dai->capture_widget;
4344 /* connect BE DAI playback if widgets are valid */
4345 codec = codec_dai->playback_widget;
4347 if (playback_cpu && codec) {
4348 if (dai_link->params && !rtd->playback_widget) {
4349 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4350 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4353 rtd->playback_widget = dai;
4356 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4357 rtd->playback_widget,
4362 /* connect BE DAI capture if widgets are valid */
4363 codec = codec_dai->capture_widget;
4365 if (codec && capture_cpu) {
4366 if (dai_link->params && !rtd->capture_widget) {
4367 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4368 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4371 rtd->capture_widget = dai;
4374 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4375 rtd->capture_widget,
4376 cpu_dai, capture_cpu);
4380 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4383 struct snd_soc_dapm_widget *w;
4385 w = snd_soc_dai_get_widget(dai, stream);
4390 dapm_mark_dirty(w, "stream event");
4392 if (w->id == snd_soc_dapm_dai_in) {
4393 ep = SND_SOC_DAPM_EP_SOURCE;
4394 dapm_widget_invalidate_input_paths(w);
4396 ep = SND_SOC_DAPM_EP_SINK;
4397 dapm_widget_invalidate_output_paths(w);
4401 case SND_SOC_DAPM_STREAM_START:
4405 case SND_SOC_DAPM_STREAM_STOP:
4409 case SND_SOC_DAPM_STREAM_SUSPEND:
4410 case SND_SOC_DAPM_STREAM_RESUME:
4411 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4412 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4418 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4420 struct snd_soc_pcm_runtime *rtd;
4421 struct snd_soc_dai *codec_dai;
4424 /* for each BE DAI link... */
4425 for_each_card_rtds(card, rtd) {
4427 * dynamic FE links have no fixed DAI mapping.
4428 * CODEC<->CODEC links have no direct connection.
4430 if (rtd->dai_link->dynamic)
4433 if (rtd->num_cpus == 1) {
4434 for_each_rtd_codec_dais(rtd, i, codec_dai)
4435 dapm_connect_dai_pair(card, rtd, codec_dai,
4436 asoc_rtd_to_cpu(rtd, 0));
4437 } else if (rtd->num_codecs == rtd->num_cpus) {
4438 for_each_rtd_codec_dais(rtd, i, codec_dai)
4439 dapm_connect_dai_pair(card, rtd, codec_dai,
4440 asoc_rtd_to_cpu(rtd, i));
4443 "N cpus to M codecs link is not supported yet\n");
4448 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4451 struct snd_soc_dai *dai;
4454 for_each_rtd_dais(rtd, i, dai)
4455 soc_dapm_dai_stream_event(dai, stream, event);
4457 dapm_power_widgets(rtd->card, event);
4461 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4462 * @rtd: PCM runtime data
4463 * @stream: stream name
4464 * @event: stream event
4466 * Sends a stream event to the dapm core. The core then makes any
4467 * necessary widget power changes.
4469 * Returns 0 for success else error.
4471 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4474 struct snd_soc_card *card = rtd->card;
4476 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4477 soc_dapm_stream_event(rtd, stream, event);
4478 mutex_unlock(&card->dapm_mutex);
4481 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4483 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4484 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4485 /* powered down playback stream now */
4486 snd_soc_dapm_stream_event(rtd,
4487 SNDRV_PCM_STREAM_PLAYBACK,
4488 SND_SOC_DAPM_STREAM_STOP);
4490 /* start delayed pop wq here for playback streams */
4492 queue_delayed_work(system_power_efficient_wq,
4494 msecs_to_jiffies(rtd->pmdown_time));
4497 /* capture streams can be powered down now */
4498 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4499 SND_SOC_DAPM_STREAM_STOP);
4502 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4505 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4506 * @dapm: DAPM context
4509 * Enables input/output pin and its parents or children widgets iff there is
4510 * a valid audio route and active audio stream.
4512 * Requires external locking.
4514 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4515 * do any widget power switching.
4517 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4520 return snd_soc_dapm_set_pin(dapm, pin, 1);
4522 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4525 * snd_soc_dapm_enable_pin - enable pin.
4526 * @dapm: DAPM context
4529 * Enables input/output pin and its parents or children widgets iff there is
4530 * a valid audio route and active audio stream.
4532 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4533 * do any widget power switching.
4535 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4539 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4541 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4543 mutex_unlock(&dapm->card->dapm_mutex);
4547 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4550 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4551 * @dapm: DAPM context
4554 * Enables input/output pin regardless of any other state. This is
4555 * intended for use with microphone bias supplies used in microphone
4558 * Requires external locking.
4560 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4561 * do any widget power switching.
4563 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4566 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4569 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4573 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4574 if (!w->connected) {
4576 * w->force does not affect the number of input or output paths,
4577 * so we only have to recheck if w->connected is changed
4579 dapm_widget_invalidate_input_paths(w);
4580 dapm_widget_invalidate_output_paths(w);
4584 dapm_mark_dirty(w, "force enable");
4588 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4591 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4592 * @dapm: DAPM context
4595 * Enables input/output pin regardless of any other state. This is
4596 * intended for use with microphone bias supplies used in microphone
4599 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4600 * do any widget power switching.
4602 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4607 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4609 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4611 mutex_unlock(&dapm->card->dapm_mutex);
4615 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4618 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4619 * @dapm: DAPM context
4622 * Disables input/output pin and its parents or children widgets.
4624 * Requires external locking.
4626 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4627 * do any widget power switching.
4629 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4632 return snd_soc_dapm_set_pin(dapm, pin, 0);
4634 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4637 * snd_soc_dapm_disable_pin - disable pin.
4638 * @dapm: DAPM context
4641 * Disables input/output pin and its parents or children widgets.
4643 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4644 * do any widget power switching.
4646 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4651 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4653 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4655 mutex_unlock(&dapm->card->dapm_mutex);
4659 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4662 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4663 * @dapm: DAPM context
4666 * Marks the specified pin as being not connected, disabling it along
4667 * any parent or child widgets. At present this is identical to
4668 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4669 * additional things such as disabling controls which only affect
4670 * paths through the pin.
4672 * Requires external locking.
4674 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4675 * do any widget power switching.
4677 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4680 return snd_soc_dapm_set_pin(dapm, pin, 0);
4682 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4685 * snd_soc_dapm_nc_pin - permanently disable pin.
4686 * @dapm: DAPM context
4689 * Marks the specified pin as being not connected, disabling it along
4690 * any parent or child widgets. At present this is identical to
4691 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4692 * additional things such as disabling controls which only affect
4693 * paths through the pin.
4695 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4696 * do any widget power switching.
4698 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4702 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4704 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4706 mutex_unlock(&dapm->card->dapm_mutex);
4710 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4713 * snd_soc_dapm_get_pin_status - get audio pin status
4714 * @dapm: DAPM context
4715 * @pin: audio signal pin endpoint (or start point)
4717 * Get audio pin status - connected or disconnected.
4719 * Returns 1 for connected otherwise 0.
4721 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4724 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4727 return w->connected;
4731 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4734 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4735 * @dapm: DAPM context
4736 * @pin: audio signal pin endpoint (or start point)
4738 * Mark the given endpoint or pin as ignoring suspend. When the
4739 * system is disabled a path between two endpoints flagged as ignoring
4740 * suspend will not be disabled. The path must already be enabled via
4741 * normal means at suspend time, it will not be turned on if it was not
4744 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4747 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4750 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4754 w->ignore_suspend = 1;
4758 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4761 * snd_soc_dapm_free - free dapm resources
4762 * @dapm: DAPM context
4764 * Free all dapm widgets and resources.
4766 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4768 dapm_debugfs_cleanup(dapm);
4769 dapm_free_widgets(dapm);
4770 list_del(&dapm->list);
4772 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4774 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4775 struct snd_soc_card *card,
4776 struct snd_soc_component *component)
4779 dapm->component = component;
4780 dapm->bias_level = SND_SOC_BIAS_OFF;
4783 dapm->dev = component->dev;
4784 dapm->idle_bias_off = !component->driver->idle_bias_on;
4785 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4787 dapm->dev = card->dev;
4790 INIT_LIST_HEAD(&dapm->list);
4791 /* see for_each_card_dapms */
4792 list_add(&dapm->list, &card->dapm_list);
4794 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4796 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4798 struct snd_soc_card *card = dapm->card;
4799 struct snd_soc_dapm_widget *w;
4800 LIST_HEAD(down_list);
4803 mutex_lock(&card->dapm_mutex);
4805 for_each_card_widgets(dapm->card, w) {
4806 if (w->dapm != dapm)
4809 dapm_seq_insert(w, &down_list, false);
4815 /* If there were no widgets to power down we're already in
4819 if (dapm->bias_level == SND_SOC_BIAS_ON)
4820 snd_soc_dapm_set_bias_level(dapm,
4821 SND_SOC_BIAS_PREPARE);
4822 dapm_seq_run(card, &down_list, 0, false);
4823 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4824 snd_soc_dapm_set_bias_level(dapm,
4825 SND_SOC_BIAS_STANDBY);
4828 mutex_unlock(&card->dapm_mutex);
4832 * snd_soc_dapm_shutdown - callback for system shutdown
4834 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4836 struct snd_soc_dapm_context *dapm;
4838 for_each_card_dapms(card, dapm) {
4839 if (dapm != &card->dapm) {
4840 soc_dapm_shutdown_dapm(dapm);
4841 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4842 snd_soc_dapm_set_bias_level(dapm,
4847 soc_dapm_shutdown_dapm(&card->dapm);
4848 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4849 snd_soc_dapm_set_bias_level(&card->dapm,
4853 /* Module information */
4854 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4855 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4856 MODULE_LICENSE("GPL");