1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
67 /* dapm power sequences - make this per codec in the future */
68 static int dapm_up_seq[] = {
69 [snd_soc_dapm_pre] = 1,
70 [snd_soc_dapm_regulator_supply] = 2,
71 [snd_soc_dapm_pinctrl] = 2,
72 [snd_soc_dapm_clock_supply] = 2,
73 [snd_soc_dapm_supply] = 3,
74 [snd_soc_dapm_micbias] = 4,
75 [snd_soc_dapm_vmid] = 4,
76 [snd_soc_dapm_dai_link] = 3,
77 [snd_soc_dapm_dai_in] = 5,
78 [snd_soc_dapm_dai_out] = 5,
79 [snd_soc_dapm_aif_in] = 5,
80 [snd_soc_dapm_aif_out] = 5,
81 [snd_soc_dapm_mic] = 6,
82 [snd_soc_dapm_siggen] = 6,
83 [snd_soc_dapm_input] = 6,
84 [snd_soc_dapm_output] = 6,
85 [snd_soc_dapm_mux] = 7,
86 [snd_soc_dapm_demux] = 7,
87 [snd_soc_dapm_dac] = 8,
88 [snd_soc_dapm_switch] = 9,
89 [snd_soc_dapm_mixer] = 9,
90 [snd_soc_dapm_mixer_named_ctl] = 9,
91 [snd_soc_dapm_pga] = 10,
92 [snd_soc_dapm_buffer] = 10,
93 [snd_soc_dapm_scheduler] = 10,
94 [snd_soc_dapm_effect] = 10,
95 [snd_soc_dapm_src] = 10,
96 [snd_soc_dapm_asrc] = 10,
97 [snd_soc_dapm_encoder] = 10,
98 [snd_soc_dapm_decoder] = 10,
99 [snd_soc_dapm_adc] = 11,
100 [snd_soc_dapm_out_drv] = 12,
101 [snd_soc_dapm_hp] = 12,
102 [snd_soc_dapm_spk] = 12,
103 [snd_soc_dapm_line] = 12,
104 [snd_soc_dapm_sink] = 12,
105 [snd_soc_dapm_kcontrol] = 13,
106 [snd_soc_dapm_post] = 14,
109 static int dapm_down_seq[] = {
110 [snd_soc_dapm_pre] = 1,
111 [snd_soc_dapm_kcontrol] = 2,
112 [snd_soc_dapm_adc] = 3,
113 [snd_soc_dapm_hp] = 4,
114 [snd_soc_dapm_spk] = 4,
115 [snd_soc_dapm_line] = 4,
116 [snd_soc_dapm_out_drv] = 4,
117 [snd_soc_dapm_sink] = 4,
118 [snd_soc_dapm_pga] = 5,
119 [snd_soc_dapm_buffer] = 5,
120 [snd_soc_dapm_scheduler] = 5,
121 [snd_soc_dapm_effect] = 5,
122 [snd_soc_dapm_src] = 5,
123 [snd_soc_dapm_asrc] = 5,
124 [snd_soc_dapm_encoder] = 5,
125 [snd_soc_dapm_decoder] = 5,
126 [snd_soc_dapm_switch] = 6,
127 [snd_soc_dapm_mixer_named_ctl] = 6,
128 [snd_soc_dapm_mixer] = 6,
129 [snd_soc_dapm_dac] = 7,
130 [snd_soc_dapm_mic] = 8,
131 [snd_soc_dapm_siggen] = 8,
132 [snd_soc_dapm_input] = 8,
133 [snd_soc_dapm_output] = 8,
134 [snd_soc_dapm_micbias] = 9,
135 [snd_soc_dapm_vmid] = 9,
136 [snd_soc_dapm_mux] = 10,
137 [snd_soc_dapm_demux] = 10,
138 [snd_soc_dapm_aif_in] = 11,
139 [snd_soc_dapm_aif_out] = 11,
140 [snd_soc_dapm_dai_in] = 11,
141 [snd_soc_dapm_dai_out] = 11,
142 [snd_soc_dapm_dai_link] = 12,
143 [snd_soc_dapm_supply] = 13,
144 [snd_soc_dapm_clock_supply] = 14,
145 [snd_soc_dapm_pinctrl] = 14,
146 [snd_soc_dapm_regulator_supply] = 14,
147 [snd_soc_dapm_post] = 15,
150 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
152 if (dapm->card && dapm->card->instantiated)
153 lockdep_assert_held(&dapm->card->dapm_mutex);
156 static void pop_wait(u32 pop_time)
159 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
163 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
171 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
176 vsnprintf(buf, PAGE_SIZE, fmt, args);
177 dev_info(dev, "%s", buf);
183 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
185 return !list_empty(&w->dirty);
188 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
190 dapm_assert_locked(w->dapm);
192 if (!dapm_dirty_widget(w)) {
193 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
195 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
200 * Common implementation for dapm_widget_invalidate_input_paths() and
201 * dapm_widget_invalidate_output_paths(). The function is inlined since the
202 * combined size of the two specialized functions is only marginally larger then
203 * the size of the generic function and at the same time the fast path of the
204 * specialized functions is significantly smaller than the generic function.
206 static __always_inline void dapm_widget_invalidate_paths(
207 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
209 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
210 struct snd_soc_dapm_widget *node;
211 struct snd_soc_dapm_path *p;
214 dapm_assert_locked(w->dapm);
216 if (w->endpoints[dir] == -1)
219 list_add_tail(&w->work_list, &list);
220 w->endpoints[dir] = -1;
222 list_for_each_entry(w, &list, work_list) {
223 snd_soc_dapm_widget_for_each_path(w, dir, p) {
224 if (p->is_supply || p->weak || !p->connect)
226 node = p->node[rdir];
227 if (node->endpoints[dir] != -1) {
228 node->endpoints[dir] = -1;
229 list_add_tail(&node->work_list, &list);
236 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
238 * @w: The widget for which to invalidate the cached number of input paths
240 * Resets the cached number of inputs for the specified widget and all widgets
241 * that can be reached via outcoming paths from the widget.
243 * This function must be called if the number of output paths for a widget might
244 * have changed. E.g. if the source state of a widget changes or a path is added
245 * or activated with the widget as the sink.
247 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
249 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
253 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
255 * @w: The widget for which to invalidate the cached number of output paths
257 * Resets the cached number of outputs for the specified widget and all widgets
258 * that can be reached via incoming paths from the widget.
260 * This function must be called if the number of output paths for a widget might
261 * have changed. E.g. if the sink state of a widget changes or a path is added
262 * or activated with the widget as the source.
264 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
266 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
270 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
271 * for the widgets connected to a path
272 * @p: The path to invalidate
274 * Resets the cached number of inputs for the sink of the path and the cached
275 * number of outputs for the source of the path.
277 * This function must be called when a path is added, removed or the connected
280 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
283 * Weak paths or supply paths do not influence the number of input or
284 * output paths of their neighbors.
286 if (p->weak || p->is_supply)
290 * The number of connected endpoints is the sum of the number of
291 * connected endpoints of all neighbors. If a node with 0 connected
292 * endpoints is either connected or disconnected that sum won't change,
293 * so there is no need to re-check the path.
295 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
296 dapm_widget_invalidate_input_paths(p->sink);
297 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
298 dapm_widget_invalidate_output_paths(p->source);
301 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
303 struct snd_soc_dapm_widget *w;
305 mutex_lock(&card->dapm_mutex);
307 for_each_card_widgets(card, w) {
309 dapm_mark_dirty(w, "Rechecking endpoints");
310 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
311 dapm_widget_invalidate_output_paths(w);
312 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
313 dapm_widget_invalidate_input_paths(w);
317 mutex_unlock(&card->dapm_mutex);
319 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
321 /* create a new dapm widget */
322 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
323 const struct snd_soc_dapm_widget *_widget)
325 struct snd_soc_dapm_widget *w;
327 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
332 * w->name is duplicated in caller, but w->sname isn't.
333 * Duplicate it here if defined
335 if (_widget->sname) {
336 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
345 struct dapm_kcontrol_data {
347 struct snd_soc_dapm_widget *widget;
348 struct list_head paths;
349 struct snd_soc_dapm_widget_list *wlist;
352 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
353 struct snd_kcontrol *kcontrol, const char *ctrl_name)
355 struct dapm_kcontrol_data *data;
356 struct soc_mixer_control *mc;
361 data = kzalloc(sizeof(*data), GFP_KERNEL);
365 INIT_LIST_HEAD(&data->paths);
367 switch (widget->id) {
368 case snd_soc_dapm_switch:
369 case snd_soc_dapm_mixer:
370 case snd_soc_dapm_mixer_named_ctl:
371 mc = (struct soc_mixer_control *)kcontrol->private_value;
373 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
374 dev_warn(widget->dapm->dev,
375 "ASoC: Unsupported stereo autodisable control '%s'\n",
378 if (mc->autodisable) {
379 struct snd_soc_dapm_widget template;
381 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
388 memset(&template, 0, sizeof(template));
389 template.reg = mc->reg;
390 template.mask = (1 << fls(mc->max)) - 1;
391 template.shift = mc->shift;
393 template.off_val = mc->max;
395 template.off_val = 0;
396 template.on_val = template.off_val;
397 template.id = snd_soc_dapm_kcontrol;
398 template.name = name;
400 data->value = template.on_val;
403 snd_soc_dapm_new_control_unlocked(widget->dapm,
406 if (IS_ERR(data->widget)) {
407 ret = PTR_ERR(data->widget);
412 case snd_soc_dapm_demux:
413 case snd_soc_dapm_mux:
414 e = (struct soc_enum *)kcontrol->private_value;
416 if (e->autodisable) {
417 struct snd_soc_dapm_widget template;
419 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
426 memset(&template, 0, sizeof(template));
427 template.reg = e->reg;
428 template.mask = e->mask;
429 template.shift = e->shift_l;
430 template.off_val = snd_soc_enum_item_to_val(e, 0);
431 template.on_val = template.off_val;
432 template.id = snd_soc_dapm_kcontrol;
433 template.name = name;
435 data->value = template.on_val;
437 data->widget = snd_soc_dapm_new_control_unlocked(
438 widget->dapm, &template);
440 if (IS_ERR(data->widget)) {
441 ret = PTR_ERR(data->widget);
445 snd_soc_dapm_add_path(widget->dapm, data->widget,
447 } else if (e->reg != SND_SOC_NOPM) {
448 data->value = soc_dapm_read(widget->dapm, e->reg) &
449 (e->mask << e->shift_l);
456 kcontrol->private_data = data;
465 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
467 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
469 list_del(&data->paths);
474 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
475 const struct snd_kcontrol *kcontrol)
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
482 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
483 struct snd_soc_dapm_widget *widget)
485 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
486 struct snd_soc_dapm_widget_list *new_wlist;
490 n = data->wlist->num_widgets + 1;
494 new_wlist = krealloc(data->wlist,
495 struct_size(new_wlist, widgets, n),
500 new_wlist->widgets[n - 1] = widget;
501 new_wlist->num_widgets = n;
503 data->wlist = new_wlist;
508 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
509 struct snd_soc_dapm_path *path)
511 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
513 list_add_tail(&path->list_kcontrol, &data->paths);
516 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
518 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
523 return data->widget->power;
526 static struct list_head *dapm_kcontrol_get_path_list(
527 const struct snd_kcontrol *kcontrol)
529 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
534 #define dapm_kcontrol_for_each_path(path, kcontrol) \
535 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
538 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
540 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
544 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
546 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
549 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
551 if (data->value == value)
555 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
556 case snd_soc_dapm_switch:
557 case snd_soc_dapm_mixer:
558 case snd_soc_dapm_mixer_named_ctl:
559 data->widget->on_val = value & data->widget->mask;
561 case snd_soc_dapm_demux:
562 case snd_soc_dapm_mux:
563 data->widget->on_val = value >> data->widget->shift;
566 data->widget->on_val = value;
577 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
579 * @kcontrol: The kcontrol
581 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
582 struct snd_kcontrol *kcontrol)
584 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
586 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
589 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
591 * @kcontrol: The kcontrol
593 * Note: This function must only be used on kcontrols that are known to have
594 * been registered for a CODEC. Otherwise the behaviour is undefined.
596 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
597 struct snd_kcontrol *kcontrol)
599 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
601 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
603 static void dapm_reset(struct snd_soc_card *card)
605 struct snd_soc_dapm_widget *w;
607 lockdep_assert_held(&card->dapm_mutex);
609 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
611 for_each_card_widgets(card, w) {
612 w->new_power = w->power;
613 w->power_checked = false;
617 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
619 if (!dapm->component)
621 return dapm->component->name_prefix;
624 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
626 if (!dapm->component)
628 return snd_soc_component_read(dapm->component, reg);
631 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
632 int reg, unsigned int mask, unsigned int value)
634 if (!dapm->component)
636 return snd_soc_component_update_bits(dapm->component, reg,
640 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
641 int reg, unsigned int mask, unsigned int value)
643 if (!dapm->component)
645 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
648 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
651 snd_soc_component_async_complete(dapm->component);
654 static struct snd_soc_dapm_widget *
655 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
657 struct snd_soc_dapm_widget *w = wcache->widget;
658 struct list_head *wlist;
663 wlist = &w->dapm->card->widgets;
665 list_for_each_entry_from(w, wlist, list) {
666 if (!strcmp(name, w->name))
677 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
678 struct snd_soc_dapm_widget *w)
684 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
685 * @dapm: The DAPM context for which to set the level
686 * @level: The level to set
688 * Forces the DAPM bias level to a specific state. It will call the bias level
689 * callback of DAPM context with the specified level. This will even happen if
690 * the context is already at the same level. Furthermore it will not go through
691 * the normal bias level sequencing, meaning any intermediate states between the
692 * current and the target state will not be entered.
694 * Note that the change in bias level is only temporary and the next time
695 * snd_soc_dapm_sync() is called the state will be set to the level as
696 * determined by the DAPM core. The function is mainly intended to be used to
697 * used during probe or resume from suspend to power up the device so
698 * initialization can be done, before the DAPM core takes over.
700 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
701 enum snd_soc_bias_level level)
706 ret = snd_soc_component_set_bias_level(dapm->component, level);
709 dapm->bias_level = level;
713 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
716 * snd_soc_dapm_set_bias_level - set the bias level for the system
717 * @dapm: DAPM context
718 * @level: level to configure
720 * Configure the bias (power) levels for the SoC audio device.
722 * Returns 0 for success else error.
724 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
725 enum snd_soc_bias_level level)
727 struct snd_soc_card *card = dapm->card;
730 trace_snd_soc_bias_level_start(card, level);
732 ret = snd_soc_card_set_bias_level(card, dapm, level);
736 if (!card || dapm != &card->dapm)
737 ret = snd_soc_dapm_force_bias_level(dapm, level);
742 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
744 trace_snd_soc_bias_level_done(card, level);
749 /* connect mux widget to its interconnecting audio paths */
750 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
751 struct snd_soc_dapm_path *path, const char *control_name,
752 struct snd_soc_dapm_widget *w)
754 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
755 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
756 unsigned int val, item;
759 if (e->reg != SND_SOC_NOPM) {
760 val = soc_dapm_read(dapm, e->reg);
761 val = (val >> e->shift_l) & e->mask;
762 item = snd_soc_enum_val_to_item(e, val);
764 /* since a virtual mux has no backing registers to
765 * decide which path to connect, it will try to match
766 * with the first enumeration. This is to ensure
767 * that the default mux choice (the first) will be
768 * correctly powered up during initialization.
773 i = match_string(e->texts, e->items, control_name);
777 path->name = e->texts[i];
778 path->connect = (i == item);
783 /* set up initial codec paths */
784 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
787 struct soc_mixer_control *mc = (struct soc_mixer_control *)
788 p->sink->kcontrol_news[i].private_value;
789 unsigned int reg = mc->reg;
790 unsigned int shift = mc->shift;
791 unsigned int max = mc->max;
792 unsigned int mask = (1 << fls(max)) - 1;
793 unsigned int invert = mc->invert;
796 if (reg != SND_SOC_NOPM) {
797 val = soc_dapm_read(p->sink->dapm, reg);
799 * The nth_path argument allows this function to know
800 * which path of a kcontrol it is setting the initial
801 * status for. Ideally this would support any number
802 * of paths and channels. But since kcontrols only come
803 * in mono and stereo variants, we are limited to 2
806 * The following code assumes for stereo controls the
807 * first path is the left channel, and all remaining
808 * paths are the right channel.
810 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
812 val = soc_dapm_read(p->sink->dapm, mc->rreg);
813 val = (val >> mc->rshift) & mask;
815 val = (val >> shift) & mask;
821 /* since a virtual mixer has no backing registers to
822 * decide which path to connect, it will try to match
823 * with initial state. This is to ensure
824 * that the default mixer choice will be
825 * correctly powered up during initialization.
831 /* connect mixer widget to its interconnecting audio paths */
832 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
833 struct snd_soc_dapm_path *path, const char *control_name)
837 /* search for mixer kcontrol */
838 for (i = 0; i < path->sink->num_kcontrols; i++) {
839 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
840 path->name = path->sink->kcontrol_news[i].name;
841 dapm_set_mixer_path_status(path, i, nth_path++);
848 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
849 struct snd_soc_dapm_widget *kcontrolw,
850 const struct snd_kcontrol_new *kcontrol_new,
851 struct snd_kcontrol **kcontrol)
853 struct snd_soc_dapm_widget *w;
858 for_each_card_widgets(dapm->card, w) {
859 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
861 for (i = 0; i < w->num_kcontrols; i++) {
862 if (&w->kcontrol_news[i] == kcontrol_new) {
864 *kcontrol = w->kcontrols[i];
874 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
875 * create it. Either way, add the widget into the control's widget list
877 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
880 struct snd_soc_dapm_context *dapm = w->dapm;
881 struct snd_card *card = dapm->card->snd_card;
885 struct snd_kcontrol *kcontrol;
886 bool wname_in_long_name, kcname_in_long_name;
887 char *long_name = NULL;
891 prefix = soc_dapm_prefix(dapm);
893 prefix_len = strlen(prefix) + 1;
897 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
902 wname_in_long_name = false;
903 kcname_in_long_name = true;
906 case snd_soc_dapm_switch:
907 case snd_soc_dapm_mixer:
908 case snd_soc_dapm_pga:
909 case snd_soc_dapm_effect:
910 case snd_soc_dapm_out_drv:
911 wname_in_long_name = true;
912 kcname_in_long_name = true;
914 case snd_soc_dapm_mixer_named_ctl:
915 wname_in_long_name = false;
916 kcname_in_long_name = true;
918 case snd_soc_dapm_demux:
919 case snd_soc_dapm_mux:
920 wname_in_long_name = true;
921 kcname_in_long_name = false;
928 if (wname_in_long_name && kcname_in_long_name) {
930 * The control will get a prefix from the control
931 * creation process but we're also using the same
932 * prefix for widgets so cut the prefix off the
933 * front of the widget name.
935 long_name = kasprintf(GFP_KERNEL, "%s %s",
936 w->name + prefix_len,
937 w->kcontrol_news[kci].name);
938 if (long_name == NULL)
942 } else if (wname_in_long_name) {
944 name = w->name + prefix_len;
947 name = w->kcontrol_news[kci].name;
950 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
957 kcontrol->private_free = dapm_kcontrol_free;
959 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
961 snd_ctl_free_one(kcontrol);
965 ret = snd_ctl_add(card, kcontrol);
968 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
974 ret = dapm_kcontrol_add_widget(kcontrol, w);
976 w->kcontrols[kci] = kcontrol;
984 /* create new dapm mixer control */
985 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
988 struct snd_soc_dapm_path *path;
989 struct dapm_kcontrol_data *data;
992 for (i = 0; i < w->num_kcontrols; i++) {
994 snd_soc_dapm_widget_for_each_source_path(w, path) {
995 /* mixer/mux paths name must match control name */
996 if (path->name != (char *)w->kcontrol_news[i].name)
999 if (!w->kcontrols[i]) {
1000 ret = dapm_create_or_share_kcontrol(w, i);
1005 dapm_kcontrol_add_path(w->kcontrols[i], path);
1007 data = snd_kcontrol_chip(w->kcontrols[i]);
1009 snd_soc_dapm_add_path(data->widget->dapm,
1019 /* create new dapm mux control */
1020 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1022 struct snd_soc_dapm_context *dapm = w->dapm;
1023 enum snd_soc_dapm_direction dir;
1024 struct snd_soc_dapm_path *path;
1029 case snd_soc_dapm_mux:
1030 dir = SND_SOC_DAPM_DIR_OUT;
1033 case snd_soc_dapm_demux:
1034 dir = SND_SOC_DAPM_DIR_IN;
1041 if (w->num_kcontrols != 1) {
1043 "ASoC: %s %s has incorrect number of controls\n", type,
1048 if (list_empty(&w->edges[dir])) {
1049 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1053 ret = dapm_create_or_share_kcontrol(w, 0);
1057 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1059 dapm_kcontrol_add_path(w->kcontrols[0], path);
1065 /* create new dapm volume control */
1066 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1070 for (i = 0; i < w->num_kcontrols; i++) {
1071 ret = dapm_create_or_share_kcontrol(w, i);
1079 /* create new dapm dai link control */
1080 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1083 struct snd_kcontrol *kcontrol;
1084 struct snd_soc_dapm_context *dapm = w->dapm;
1085 struct snd_card *card = dapm->card->snd_card;
1086 struct snd_soc_pcm_runtime *rtd = w->priv;
1088 /* create control for links with > 1 config */
1089 if (rtd->dai_link->num_params <= 1)
1093 for (i = 0; i < w->num_kcontrols; i++) {
1094 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1096 ret = snd_ctl_add(card, kcontrol);
1099 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1100 w->name, w->kcontrol_news[i].name, ret);
1103 kcontrol->private_data = w;
1104 w->kcontrols[i] = kcontrol;
1110 /* We implement power down on suspend by checking the power state of
1111 * the ALSA card - when we are suspending the ALSA state for the card
1114 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1116 int level = snd_power_get_state(widget->dapm->card->snd_card);
1119 case SNDRV_CTL_POWER_D3hot:
1120 case SNDRV_CTL_POWER_D3cold:
1121 if (widget->ignore_suspend)
1122 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1124 return widget->ignore_suspend;
1130 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1135 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1136 struct list_head *widgets)
1138 struct snd_soc_dapm_widget *w;
1139 struct list_head *it;
1140 unsigned int size = 0;
1143 list_for_each(it, widgets)
1146 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1150 list_for_each_entry(w, widgets, work_list)
1151 (*list)->widgets[i++] = w;
1153 (*list)->num_widgets = i;
1159 * Recursively reset the cached number of inputs or outputs for the specified
1160 * widget and all widgets that can be reached via incoming or outcoming paths
1163 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1164 enum snd_soc_dapm_direction dir)
1166 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1167 struct snd_soc_dapm_path *path;
1169 widget->endpoints[dir] = -1;
1171 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1172 if (path->weak || path->is_supply)
1178 if (path->connect) {
1180 invalidate_paths_ep(path->node[dir], dir);
1187 * Common implementation for is_connected_output_ep() and
1188 * is_connected_input_ep(). The function is inlined since the combined size of
1189 * the two specialized functions is only marginally larger then the size of the
1190 * generic function and at the same time the fast path of the specialized
1191 * functions is significantly smaller than the generic function.
1193 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1194 struct list_head *list, enum snd_soc_dapm_direction dir,
1195 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1196 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1197 enum snd_soc_dapm_direction)),
1198 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1199 enum snd_soc_dapm_direction))
1201 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1202 struct snd_soc_dapm_path *path;
1205 if (widget->endpoints[dir] >= 0)
1206 return widget->endpoints[dir];
1208 DAPM_UPDATE_STAT(widget, path_checks);
1210 /* do we need to add this widget to the list ? */
1212 list_add_tail(&widget->work_list, list);
1214 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1216 custom_stop_condition = NULL;
1219 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1220 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1221 return widget->endpoints[dir];
1224 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1225 DAPM_UPDATE_STAT(widget, neighbour_checks);
1227 if (path->weak || path->is_supply)
1233 trace_snd_soc_dapm_path(widget, dir, path);
1235 if (path->connect) {
1237 con += fn(path->node[dir], list, custom_stop_condition);
1242 widget->endpoints[dir] = con;
1248 * Recursively check for a completed path to an active or physically connected
1249 * output widget. Returns number of complete paths.
1251 * Optionally, can be supplied with a function acting as a stopping condition.
1252 * This function takes the dapm widget currently being examined and the walk
1253 * direction as an arguments, it should return true if widgets from that point
1254 * in the graph onwards should not be added to the widget list.
1256 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1257 struct list_head *list,
1258 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1259 enum snd_soc_dapm_direction))
1261 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1262 is_connected_output_ep, custom_stop_condition);
1266 * Recursively check for a completed path to an active or physically connected
1267 * input widget. Returns number of complete paths.
1269 * Optionally, can be supplied with a function acting as a stopping condition.
1270 * This function takes the dapm widget currently being examined and the walk
1271 * direction as an arguments, it should return true if the walk should be
1272 * stopped and false otherwise.
1274 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1275 struct list_head *list,
1276 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1277 enum snd_soc_dapm_direction))
1279 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1280 is_connected_input_ep, custom_stop_condition);
1284 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1285 * @dai: the soc DAI.
1286 * @stream: stream direction.
1287 * @list: list of active widgets for this stream.
1288 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1289 * walk based on custom logic.
1291 * Queries DAPM graph as to whether a valid audio stream path exists for
1292 * the initial stream specified by name. This takes into account
1293 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1295 * Optionally, can be supplied with a function acting as a stopping condition.
1296 * This function takes the dapm widget currently being examined and the walk
1297 * direction as an arguments, it should return true if the walk should be
1298 * stopped and false otherwise.
1300 * Returns the number of valid paths or negative error.
1302 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1303 struct snd_soc_dapm_widget_list **list,
1304 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1305 enum snd_soc_dapm_direction))
1307 struct snd_soc_card *card = dai->component->card;
1308 struct snd_soc_dapm_widget *w;
1313 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1315 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1316 w = dai->playback_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1318 paths = is_connected_output_ep(w, &widgets,
1319 custom_stop_condition);
1321 w = dai->capture_widget;
1322 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1323 paths = is_connected_input_ep(w, &widgets,
1324 custom_stop_condition);
1327 /* Drop starting point */
1328 list_del(widgets.next);
1330 ret = dapm_widget_list_create(list, &widgets);
1334 trace_snd_soc_dapm_connected(paths, stream);
1335 mutex_unlock(&card->dapm_mutex);
1340 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1342 dapm_widget_list_free(list);
1346 * Handler for regulator supply widget.
1348 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1349 struct snd_kcontrol *kcontrol, int event)
1353 soc_dapm_async_complete(w->dapm);
1355 if (SND_SOC_DAPM_EVENT_ON(event)) {
1356 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1357 ret = regulator_allow_bypass(w->regulator, false);
1359 dev_warn(w->dapm->dev,
1360 "ASoC: Failed to unbypass %s: %d\n",
1364 return regulator_enable(w->regulator);
1366 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1367 ret = regulator_allow_bypass(w->regulator, true);
1369 dev_warn(w->dapm->dev,
1370 "ASoC: Failed to bypass %s: %d\n",
1374 return regulator_disable_deferred(w->regulator, w->shift);
1377 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1380 * Handler for pinctrl widget.
1382 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1383 struct snd_kcontrol *kcontrol, int event)
1385 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1386 struct pinctrl *p = w->pinctrl;
1387 struct pinctrl_state *s;
1392 if (SND_SOC_DAPM_EVENT_ON(event))
1393 s = pinctrl_lookup_state(p, priv->active_state);
1395 s = pinctrl_lookup_state(p, priv->sleep_state);
1400 return pinctrl_select_state(p, s);
1402 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1405 * Handler for clock supply widget.
1407 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1408 struct snd_kcontrol *kcontrol, int event)
1413 soc_dapm_async_complete(w->dapm);
1415 if (SND_SOC_DAPM_EVENT_ON(event)) {
1416 return clk_prepare_enable(w->clk);
1418 clk_disable_unprepare(w->clk);
1424 EXPORT_SYMBOL_GPL(dapm_clock_event);
1426 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1428 if (w->power_checked)
1429 return w->new_power;
1434 w->new_power = w->power_check(w);
1436 w->power_checked = true;
1438 return w->new_power;
1441 /* Generic check to see if a widget should be powered. */
1442 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1446 DAPM_UPDATE_STAT(w, power_checks);
1448 in = is_connected_input_ep(w, NULL, NULL);
1449 out = is_connected_output_ep(w, NULL, NULL);
1450 return out != 0 && in != 0;
1453 /* Check to see if a power supply is needed */
1454 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1456 struct snd_soc_dapm_path *path;
1458 DAPM_UPDATE_STAT(w, power_checks);
1460 /* Check if one of our outputs is connected */
1461 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1462 DAPM_UPDATE_STAT(w, neighbour_checks);
1467 if (path->connected &&
1468 !path->connected(path->source, path->sink))
1471 if (dapm_widget_power_check(path->sink))
1478 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1480 return w->connected;
1483 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1484 struct snd_soc_dapm_widget *b,
1489 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1490 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1495 sort = dapm_down_seq;
1497 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1498 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1500 if (sort[a->id] != sort[b->id])
1501 return sort[a->id] - sort[b->id];
1502 if (a->subseq != b->subseq) {
1504 return a->subseq - b->subseq;
1506 return b->subseq - a->subseq;
1508 if (a->reg != b->reg)
1509 return a->reg - b->reg;
1510 if (a->dapm != b->dapm)
1511 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1516 /* Insert a widget in order into a DAPM power sequence. */
1517 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1518 struct list_head *list,
1521 struct snd_soc_dapm_widget *w;
1523 list_for_each_entry(w, list, power_list)
1524 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1525 list_add_tail(&new_widget->power_list, &w->power_list);
1529 list_add_tail(&new_widget->power_list, list);
1532 static void dapm_seq_check_event(struct snd_soc_card *card,
1533 struct snd_soc_dapm_widget *w, int event)
1535 const char *ev_name;
1539 case SND_SOC_DAPM_PRE_PMU:
1540 ev_name = "PRE_PMU";
1543 case SND_SOC_DAPM_POST_PMU:
1544 ev_name = "POST_PMU";
1547 case SND_SOC_DAPM_PRE_PMD:
1548 ev_name = "PRE_PMD";
1551 case SND_SOC_DAPM_POST_PMD:
1552 ev_name = "POST_PMD";
1555 case SND_SOC_DAPM_WILL_PMU:
1556 ev_name = "WILL_PMU";
1559 case SND_SOC_DAPM_WILL_PMD:
1560 ev_name = "WILL_PMD";
1564 WARN(1, "Unknown event %d\n", event);
1568 if (w->new_power != power)
1571 if (w->event && (w->event_flags & event)) {
1572 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1574 soc_dapm_async_complete(w->dapm);
1575 trace_snd_soc_dapm_widget_event_start(w, event);
1576 ret = w->event(w, NULL, event);
1577 trace_snd_soc_dapm_widget_event_done(w, event);
1579 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1580 ev_name, w->name, ret);
1584 /* Apply the coalesced changes from a DAPM sequence */
1585 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1586 struct list_head *pending)
1588 struct snd_soc_dapm_context *dapm;
1589 struct snd_soc_dapm_widget *w;
1591 unsigned int value = 0;
1592 unsigned int mask = 0;
1594 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1598 list_for_each_entry(w, pending, power_list) {
1599 WARN_ON(reg != w->reg || dapm != w->dapm);
1600 w->power = w->new_power;
1602 mask |= w->mask << w->shift;
1604 value |= w->on_val << w->shift;
1606 value |= w->off_val << w->shift;
1608 pop_dbg(dapm->dev, card->pop_time,
1609 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1610 w->name, reg, value, mask);
1612 /* Check for events */
1613 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1614 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1618 /* Any widget will do, they should all be updating the
1622 pop_dbg(dapm->dev, card->pop_time,
1623 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1624 value, mask, reg, card->pop_time);
1625 pop_wait(card->pop_time);
1626 soc_dapm_update_bits(dapm, reg, mask, value);
1629 list_for_each_entry(w, pending, power_list) {
1630 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1631 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1635 /* Apply a DAPM power sequence.
1637 * We walk over a pre-sorted list of widgets to apply power to. In
1638 * order to minimise the number of writes to the device required
1639 * multiple widgets will be updated in a single write where possible.
1640 * Currently anything that requires more than a single write is not
1643 static void dapm_seq_run(struct snd_soc_card *card,
1644 struct list_head *list, int event, bool power_up)
1646 struct snd_soc_dapm_widget *w, *n;
1647 struct snd_soc_dapm_context *d;
1650 int cur_subseq = -1;
1651 int cur_reg = SND_SOC_NOPM;
1652 struct snd_soc_dapm_context *cur_dapm = NULL;
1659 sort = dapm_down_seq;
1661 list_for_each_entry_safe(w, n, list, power_list) {
1664 /* Do we need to apply any queued changes? */
1665 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1666 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1667 if (!list_empty(&pending))
1668 dapm_seq_run_coalesced(card, &pending);
1670 if (cur_dapm && cur_dapm->component) {
1671 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1672 if (sort[i] == cur_sort)
1673 snd_soc_component_seq_notifier(
1674 cur_dapm->component,
1678 if (cur_dapm && w->dapm != cur_dapm)
1679 soc_dapm_async_complete(cur_dapm);
1681 INIT_LIST_HEAD(&pending);
1683 cur_subseq = INT_MIN;
1684 cur_reg = SND_SOC_NOPM;
1689 case snd_soc_dapm_pre:
1693 if (event == SND_SOC_DAPM_STREAM_START)
1695 NULL, SND_SOC_DAPM_PRE_PMU);
1696 else if (event == SND_SOC_DAPM_STREAM_STOP)
1698 NULL, SND_SOC_DAPM_PRE_PMD);
1701 case snd_soc_dapm_post:
1705 if (event == SND_SOC_DAPM_STREAM_START)
1707 NULL, SND_SOC_DAPM_POST_PMU);
1708 else if (event == SND_SOC_DAPM_STREAM_STOP)
1710 NULL, SND_SOC_DAPM_POST_PMD);
1714 /* Queue it up for application */
1715 cur_sort = sort[w->id];
1716 cur_subseq = w->subseq;
1719 list_move(&w->power_list, &pending);
1724 dev_err(w->dapm->dev,
1725 "ASoC: Failed to apply widget power: %d\n", ret);
1728 if (!list_empty(&pending))
1729 dapm_seq_run_coalesced(card, &pending);
1731 if (cur_dapm && cur_dapm->component) {
1732 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1733 if (sort[i] == cur_sort)
1734 snd_soc_component_seq_notifier(
1735 cur_dapm->component,
1739 for_each_card_dapms(card, d)
1740 soc_dapm_async_complete(d);
1743 static void dapm_widget_update(struct snd_soc_card *card)
1745 struct snd_soc_dapm_update *update = card->update;
1746 struct snd_soc_dapm_widget_list *wlist;
1747 struct snd_soc_dapm_widget *w = NULL;
1751 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1754 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1756 for_each_dapm_widgets(wlist, wi, w) {
1757 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1758 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1760 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1768 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1771 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1774 if (update->has_second_set) {
1775 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1776 update->mask2, update->val2);
1778 dev_err(w->dapm->dev,
1779 "ASoC: %s DAPM update failed: %d\n",
1783 for_each_dapm_widgets(wlist, wi, w) {
1784 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1785 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1787 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1793 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1794 * they're changing state.
1796 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1798 struct snd_soc_dapm_context *d = data;
1801 /* If we're off and we're not supposed to go into STANDBY */
1802 if (d->bias_level == SND_SOC_BIAS_OFF &&
1803 d->target_bias_level != SND_SOC_BIAS_OFF) {
1804 if (d->dev && cookie)
1805 pm_runtime_get_sync(d->dev);
1807 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1810 "ASoC: Failed to turn on bias: %d\n", ret);
1813 /* Prepare for a transition to ON or away from ON */
1814 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1815 d->bias_level != SND_SOC_BIAS_ON) ||
1816 (d->target_bias_level != SND_SOC_BIAS_ON &&
1817 d->bias_level == SND_SOC_BIAS_ON)) {
1818 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1821 "ASoC: Failed to prepare bias: %d\n", ret);
1825 /* Async callback run prior to DAPM sequences - brings to their final
1828 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1830 struct snd_soc_dapm_context *d = data;
1833 /* If we just powered the last thing off drop to standby bias */
1834 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1835 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1836 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1837 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1839 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1843 /* If we're in standby and can support bias off then do that */
1844 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1845 d->target_bias_level == SND_SOC_BIAS_OFF) {
1846 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1848 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1851 if (d->dev && cookie)
1852 pm_runtime_put(d->dev);
1855 /* If we just powered up then move to active bias */
1856 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1857 d->target_bias_level == SND_SOC_BIAS_ON) {
1858 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1860 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1865 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1866 bool power, bool connect)
1868 /* If a connection is being made or broken then that update
1869 * will have marked the peer dirty, otherwise the widgets are
1870 * not connected and this update has no impact. */
1874 /* If the peer is already in the state we're moving to then we
1875 * won't have an impact on it. */
1876 if (power != peer->power)
1877 dapm_mark_dirty(peer, "peer state change");
1880 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1881 struct list_head *up_list,
1882 struct list_head *down_list)
1884 struct snd_soc_dapm_path *path;
1886 if (w->power == power)
1889 trace_snd_soc_dapm_widget_power(w, power);
1891 /* If we changed our power state perhaps our neigbours changed
1894 snd_soc_dapm_widget_for_each_source_path(w, path)
1895 dapm_widget_set_peer_power(path->source, power, path->connect);
1897 /* Supplies can't affect their outputs, only their inputs */
1898 if (!w->is_supply) {
1899 snd_soc_dapm_widget_for_each_sink_path(w, path)
1900 dapm_widget_set_peer_power(path->sink, power,
1905 dapm_seq_insert(w, up_list, true);
1907 dapm_seq_insert(w, down_list, false);
1910 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1911 struct list_head *up_list,
1912 struct list_head *down_list)
1917 case snd_soc_dapm_pre:
1918 dapm_seq_insert(w, down_list, false);
1920 case snd_soc_dapm_post:
1921 dapm_seq_insert(w, up_list, true);
1925 power = dapm_widget_power_check(w);
1927 dapm_widget_set_power(w, power, up_list, down_list);
1932 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1934 if (dapm->idle_bias_off)
1937 switch (snd_power_get_state(dapm->card->snd_card)) {
1938 case SNDRV_CTL_POWER_D3hot:
1939 case SNDRV_CTL_POWER_D3cold:
1940 return dapm->suspend_bias_off;
1949 * Scan each dapm widget for complete audio path.
1950 * A complete path is a route that has valid endpoints i.e.:-
1952 * o DAC to output pin.
1953 * o Input pin to ADC.
1954 * o Input pin to Output pin (bypass, sidetone)
1955 * o DAC to ADC (loopback).
1957 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1959 struct snd_soc_dapm_widget *w;
1960 struct snd_soc_dapm_context *d;
1962 LIST_HEAD(down_list);
1963 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1964 enum snd_soc_bias_level bias;
1967 lockdep_assert_held(&card->dapm_mutex);
1969 trace_snd_soc_dapm_start(card);
1971 for_each_card_dapms(card, d) {
1972 if (dapm_idle_bias_off(d))
1973 d->target_bias_level = SND_SOC_BIAS_OFF;
1975 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1980 /* Check which widgets we need to power and store them in
1981 * lists indicating if they should be powered up or down. We
1982 * only check widgets that have been flagged as dirty but note
1983 * that new widgets may be added to the dirty list while we
1986 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1987 dapm_power_one_widget(w, &up_list, &down_list);
1990 for_each_card_widgets(card, w) {
1992 case snd_soc_dapm_pre:
1993 case snd_soc_dapm_post:
1994 /* These widgets always need to be powered */
1997 list_del_init(&w->dirty);
2004 /* Supplies and micbiases only bring the
2005 * context up to STANDBY as unless something
2006 * else is active and passing audio they
2007 * generally don't require full power. Signal
2008 * generators are virtual pins and have no
2009 * power impact themselves.
2012 case snd_soc_dapm_siggen:
2013 case snd_soc_dapm_vmid:
2015 case snd_soc_dapm_supply:
2016 case snd_soc_dapm_regulator_supply:
2017 case snd_soc_dapm_pinctrl:
2018 case snd_soc_dapm_clock_supply:
2019 case snd_soc_dapm_micbias:
2020 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2021 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2024 d->target_bias_level = SND_SOC_BIAS_ON;
2031 /* Force all contexts in the card to the same bias state if
2032 * they're not ground referenced.
2034 bias = SND_SOC_BIAS_OFF;
2035 for_each_card_dapms(card, d)
2036 if (d->target_bias_level > bias)
2037 bias = d->target_bias_level;
2038 for_each_card_dapms(card, d)
2039 if (!dapm_idle_bias_off(d))
2040 d->target_bias_level = bias;
2042 trace_snd_soc_dapm_walk_done(card);
2044 /* Run card bias changes at first */
2045 dapm_pre_sequence_async(&card->dapm, 0);
2046 /* Run other bias changes in parallel */
2047 for_each_card_dapms(card, d) {
2048 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2049 async_schedule_domain(dapm_pre_sequence_async, d,
2052 async_synchronize_full_domain(&async_domain);
2054 list_for_each_entry(w, &down_list, power_list) {
2055 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2058 list_for_each_entry(w, &up_list, power_list) {
2059 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2062 /* Power down widgets first; try to avoid amplifying pops. */
2063 dapm_seq_run(card, &down_list, event, false);
2065 dapm_widget_update(card);
2068 dapm_seq_run(card, &up_list, event, true);
2070 /* Run all the bias changes in parallel */
2071 for_each_card_dapms(card, d) {
2072 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2073 async_schedule_domain(dapm_post_sequence_async, d,
2076 async_synchronize_full_domain(&async_domain);
2077 /* Run card bias changes at last */
2078 dapm_post_sequence_async(&card->dapm, 0);
2080 /* do we need to notify any clients that DAPM event is complete */
2081 for_each_card_dapms(card, d) {
2085 ret = snd_soc_component_stream_event(d->component, event);
2090 pop_dbg(card->dev, card->pop_time,
2091 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2092 pop_wait(card->pop_time);
2094 trace_snd_soc_dapm_done(card);
2099 #ifdef CONFIG_DEBUG_FS
2100 static ssize_t dapm_widget_power_read_file(struct file *file,
2101 char __user *user_buf,
2102 size_t count, loff_t *ppos)
2104 struct snd_soc_dapm_widget *w = file->private_data;
2105 struct snd_soc_card *card = w->dapm->card;
2106 enum snd_soc_dapm_direction dir, rdir;
2110 struct snd_soc_dapm_path *p = NULL;
2112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2116 mutex_lock(&card->dapm_mutex);
2118 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2123 in = is_connected_input_ep(w, NULL, NULL);
2124 out = is_connected_output_ep(w, NULL, NULL);
2127 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2128 w->name, w->power ? "On" : "Off",
2129 w->force ? " (forced)" : "", in, out);
2132 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2133 " - R%d(0x%x) mask 0x%x",
2134 w->reg, w->reg, w->mask << w->shift);
2136 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2139 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2141 w->active ? "active" : "inactive");
2143 snd_soc_dapm_for_each_direction(dir) {
2144 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2145 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2146 if (p->connected && !p->connected(p->source, p->sink))
2152 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2153 " %s \"%s\" \"%s\"\n",
2154 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2155 p->name ? p->name : "static",
2156 p->node[rdir]->name);
2160 mutex_unlock(&card->dapm_mutex);
2162 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2168 static const struct file_operations dapm_widget_power_fops = {
2169 .open = simple_open,
2170 .read = dapm_widget_power_read_file,
2171 .llseek = default_llseek,
2174 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2175 size_t count, loff_t *ppos)
2177 struct snd_soc_dapm_context *dapm = file->private_data;
2180 switch (dapm->bias_level) {
2181 case SND_SOC_BIAS_ON:
2184 case SND_SOC_BIAS_PREPARE:
2185 level = "Prepare\n";
2187 case SND_SOC_BIAS_STANDBY:
2188 level = "Standby\n";
2190 case SND_SOC_BIAS_OFF:
2194 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2195 level = "Unknown\n";
2199 return simple_read_from_buffer(user_buf, count, ppos, level,
2203 static const struct file_operations dapm_bias_fops = {
2204 .open = simple_open,
2205 .read = dapm_bias_read_file,
2206 .llseek = default_llseek,
2209 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2210 struct dentry *parent)
2212 if (!parent || IS_ERR(parent))
2215 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2217 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2221 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2223 struct snd_soc_dapm_context *dapm = w->dapm;
2225 if (!dapm->debugfs_dapm || !w->name)
2228 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2229 &dapm_widget_power_fops);
2232 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2234 debugfs_remove_recursive(dapm->debugfs_dapm);
2235 dapm->debugfs_dapm = NULL;
2239 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2240 struct dentry *parent)
2244 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2248 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2255 * soc_dapm_connect_path() - Connects or disconnects a path
2256 * @path: The path to update
2257 * @connect: The new connect state of the path. True if the path is connected,
2258 * false if it is disconnected.
2259 * @reason: The reason why the path changed (for debugging only)
2261 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2262 bool connect, const char *reason)
2264 if (path->connect == connect)
2267 path->connect = connect;
2268 dapm_mark_dirty(path->source, reason);
2269 dapm_mark_dirty(path->sink, reason);
2270 dapm_path_invalidate(path);
2273 /* test and update the power status of a mux widget */
2274 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2275 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2277 struct snd_soc_dapm_path *path;
2281 lockdep_assert_held(&card->dapm_mutex);
2283 /* find dapm widget path assoc with kcontrol */
2284 dapm_kcontrol_for_each_path(path, kcontrol) {
2286 /* we now need to match the string in the enum to the path */
2287 if (e && !(strcmp(path->name, e->texts[mux])))
2292 soc_dapm_connect_path(path, connect, "mux update");
2296 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2301 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2302 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2303 struct snd_soc_dapm_update *update)
2305 struct snd_soc_card *card = dapm->card;
2308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2309 card->update = update;
2310 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2311 card->update = NULL;
2312 mutex_unlock(&card->dapm_mutex);
2314 snd_soc_dpcm_runtime_update(card);
2317 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2319 /* test and update the power status of a mixer or switch widget */
2320 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2321 struct snd_kcontrol *kcontrol,
2322 int connect, int rconnect)
2324 struct snd_soc_dapm_path *path;
2327 lockdep_assert_held(&card->dapm_mutex);
2329 /* find dapm widget path assoc with kcontrol */
2330 dapm_kcontrol_for_each_path(path, kcontrol) {
2332 * Ideally this function should support any number of
2333 * paths and channels. But since kcontrols only come
2334 * in mono and stereo variants, we are limited to 2
2337 * The following code assumes for stereo controls the
2338 * first path (when 'found == 0') is the left channel,
2339 * and all remaining paths (when 'found == 1') are the
2342 * A stereo control is signified by a valid 'rconnect'
2343 * value, either 0 for unconnected, or >= 0 for connected.
2344 * This is chosen instead of using snd_soc_volsw_is_stereo,
2345 * so that the behavior of snd_soc_dapm_mixer_update_power
2346 * doesn't change even when the kcontrol passed in is
2349 * It passes 'connect' as the path connect status for
2350 * the left channel, and 'rconnect' for the right
2353 if (found && rconnect >= 0)
2354 soc_dapm_connect_path(path, rconnect, "mixer update");
2356 soc_dapm_connect_path(path, connect, "mixer update");
2361 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2366 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2367 struct snd_kcontrol *kcontrol, int connect,
2368 struct snd_soc_dapm_update *update)
2370 struct snd_soc_card *card = dapm->card;
2373 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2374 card->update = update;
2375 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2376 card->update = NULL;
2377 mutex_unlock(&card->dapm_mutex);
2379 snd_soc_dpcm_runtime_update(card);
2382 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2384 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2387 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2388 struct snd_soc_dapm_widget *w;
2390 char *state = "not set";
2392 /* card won't be set for the dummy component, as a spot fix
2393 * we're checking for that case specifically here but in future
2394 * we will ensure that the dummy component looks like others.
2399 for_each_card_widgets(cmpnt->card, w) {
2400 if (w->dapm != dapm)
2403 /* only display widgets that burn power */
2405 case snd_soc_dapm_hp:
2406 case snd_soc_dapm_mic:
2407 case snd_soc_dapm_spk:
2408 case snd_soc_dapm_line:
2409 case snd_soc_dapm_micbias:
2410 case snd_soc_dapm_dac:
2411 case snd_soc_dapm_adc:
2412 case snd_soc_dapm_pga:
2413 case snd_soc_dapm_effect:
2414 case snd_soc_dapm_out_drv:
2415 case snd_soc_dapm_mixer:
2416 case snd_soc_dapm_mixer_named_ctl:
2417 case snd_soc_dapm_supply:
2418 case snd_soc_dapm_regulator_supply:
2419 case snd_soc_dapm_pinctrl:
2420 case snd_soc_dapm_clock_supply:
2422 count += sprintf(buf + count, "%s: %s\n",
2423 w->name, w->power ? "On":"Off");
2430 switch (snd_soc_dapm_get_bias_level(dapm)) {
2431 case SND_SOC_BIAS_ON:
2434 case SND_SOC_BIAS_PREPARE:
2437 case SND_SOC_BIAS_STANDBY:
2440 case SND_SOC_BIAS_OFF:
2444 count += sprintf(buf + count, "PM State: %s\n", state);
2449 /* show dapm widget status in sys fs */
2450 static ssize_t dapm_widget_show(struct device *dev,
2451 struct device_attribute *attr, char *buf)
2453 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2454 struct snd_soc_dai *codec_dai;
2457 mutex_lock(&rtd->card->dapm_mutex);
2459 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2460 struct snd_soc_component *cmpnt = codec_dai->component;
2462 count += dapm_widget_show_component(cmpnt, buf + count);
2465 mutex_unlock(&rtd->card->dapm_mutex);
2470 static DEVICE_ATTR_RO(dapm_widget);
2472 struct attribute *soc_dapm_dev_attrs[] = {
2473 &dev_attr_dapm_widget.attr,
2477 static void dapm_free_path(struct snd_soc_dapm_path *path)
2479 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2480 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2481 list_del(&path->list_kcontrol);
2482 list_del(&path->list);
2486 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2488 struct snd_soc_dapm_path *p, *next_p;
2489 enum snd_soc_dapm_direction dir;
2492 list_del(&w->dirty);
2494 * remove source and sink paths associated to this widget.
2495 * While removing the path, remove reference to it from both
2496 * source and sink widgets so that path is removed only once.
2498 snd_soc_dapm_for_each_direction(dir) {
2499 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2503 kfree(w->kcontrols);
2504 kfree_const(w->name);
2505 kfree_const(w->sname);
2509 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2511 dapm->path_sink_cache.widget = NULL;
2512 dapm->path_source_cache.widget = NULL;
2515 /* free all dapm widgets and resources */
2516 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2518 struct snd_soc_dapm_widget *w, *next_w;
2520 for_each_card_widgets_safe(dapm->card, w, next_w) {
2521 if (w->dapm != dapm)
2523 snd_soc_dapm_free_widget(w);
2525 snd_soc_dapm_reset_cache(dapm);
2528 static struct snd_soc_dapm_widget *dapm_find_widget(
2529 struct snd_soc_dapm_context *dapm, const char *pin,
2530 bool search_other_contexts)
2532 struct snd_soc_dapm_widget *w;
2533 struct snd_soc_dapm_widget *fallback = NULL;
2534 char prefixed_pin[80];
2535 const char *pin_name;
2536 const char *prefix = soc_dapm_prefix(dapm);
2539 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2541 pin_name = prefixed_pin;
2546 for_each_card_widgets(dapm->card, w) {
2547 if (!strcmp(w->name, pin_name)) {
2548 if (w->dapm == dapm)
2555 if (search_other_contexts)
2562 * set the DAPM pin status:
2563 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2564 * error code; called from kcontrol put callback
2566 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2567 const char *pin, int status)
2569 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2572 dapm_assert_locked(dapm);
2575 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2579 if (w->connected != status) {
2580 dapm_mark_dirty(w, "pin configuration");
2581 dapm_widget_invalidate_input_paths(w);
2582 dapm_widget_invalidate_output_paths(w);
2586 w->connected = status;
2594 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2595 * called from several API functions below
2597 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2598 const char *pin, int status)
2600 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2602 return ret < 0 ? ret : 0;
2606 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2607 * @dapm: DAPM context
2609 * Walks all dapm audio paths and powers widgets according to their
2610 * stream or path usage.
2612 * Requires external locking.
2614 * Returns 0 for success.
2616 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2619 * Suppress early reports (eg, jacks syncing their state) to avoid
2620 * silly DAPM runs during card startup.
2622 if (!dapm->card || !dapm->card->instantiated)
2625 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2627 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2630 * snd_soc_dapm_sync - scan and power dapm paths
2631 * @dapm: DAPM context
2633 * Walks all dapm audio paths and powers widgets according to their
2634 * stream or path usage.
2636 * Returns 0 for success.
2638 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2642 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2643 ret = snd_soc_dapm_sync_unlocked(dapm);
2644 mutex_unlock(&dapm->card->dapm_mutex);
2647 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2649 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2650 struct snd_soc_dapm_widget *w,
2654 case snd_soc_dapm_aif_out:
2655 case snd_soc_dapm_aif_in:
2661 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2662 w->channel < channels ? "Connecting" : "Disconnecting",
2663 p->source->name, p->sink->name);
2665 if (w->channel < channels)
2666 soc_dapm_connect_path(p, true, "dai update");
2668 soc_dapm_connect_path(p, false, "dai update");
2673 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2674 struct snd_pcm_hw_params *params,
2675 struct snd_soc_dai *dai)
2677 int dir = substream->stream;
2678 int channels = params_channels(params);
2679 struct snd_soc_dapm_path *p;
2680 struct snd_soc_dapm_widget *w;
2683 w = snd_soc_dai_get_widget(dai, dir);
2688 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2689 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2691 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2692 ret = dapm_update_dai_chan(p, p->sink, channels);
2697 snd_soc_dapm_widget_for_each_source_path(w, p) {
2698 ret = dapm_update_dai_chan(p, p->source, channels);
2706 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2707 struct snd_pcm_hw_params *params,
2708 struct snd_soc_dai *dai)
2710 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2713 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2714 ret = dapm_update_dai_unlocked(substream, params, dai);
2715 mutex_unlock(&rtd->card->dapm_mutex);
2719 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2722 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2723 * @w: The widget for which to update the flags
2725 * Some widgets have a dynamic category which depends on which neighbors they
2726 * are connected to. This function update the category for these widgets.
2728 * This function must be called whenever a path is added or removed to a widget.
2730 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2732 enum snd_soc_dapm_direction dir;
2733 struct snd_soc_dapm_path *p;
2737 case snd_soc_dapm_input:
2738 /* On a fully routed card an input is never a source */
2739 if (w->dapm->card->fully_routed)
2741 ep = SND_SOC_DAPM_EP_SOURCE;
2742 snd_soc_dapm_widget_for_each_source_path(w, p) {
2743 if (p->source->id == snd_soc_dapm_micbias ||
2744 p->source->id == snd_soc_dapm_mic ||
2745 p->source->id == snd_soc_dapm_line ||
2746 p->source->id == snd_soc_dapm_output) {
2752 case snd_soc_dapm_output:
2753 /* On a fully routed card a output is never a sink */
2754 if (w->dapm->card->fully_routed)
2756 ep = SND_SOC_DAPM_EP_SINK;
2757 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2758 if (p->sink->id == snd_soc_dapm_spk ||
2759 p->sink->id == snd_soc_dapm_hp ||
2760 p->sink->id == snd_soc_dapm_line ||
2761 p->sink->id == snd_soc_dapm_input) {
2767 case snd_soc_dapm_line:
2769 snd_soc_dapm_for_each_direction(dir) {
2770 if (!list_empty(&w->edges[dir]))
2771 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2781 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2782 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2783 const char *control)
2785 bool dynamic_source = false;
2786 bool dynamic_sink = false;
2791 switch (source->id) {
2792 case snd_soc_dapm_demux:
2793 dynamic_source = true;
2800 case snd_soc_dapm_mux:
2801 case snd_soc_dapm_switch:
2802 case snd_soc_dapm_mixer:
2803 case snd_soc_dapm_mixer_named_ctl:
2804 dynamic_sink = true;
2810 if (dynamic_source && dynamic_sink) {
2812 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2813 source->name, control, sink->name);
2815 } else if (!dynamic_source && !dynamic_sink) {
2817 "Control not supported for path %s -> [%s] -> %s\n",
2818 source->name, control, sink->name);
2825 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2826 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2827 const char *control,
2828 int (*connected)(struct snd_soc_dapm_widget *source,
2829 struct snd_soc_dapm_widget *sink))
2831 struct snd_soc_dapm_widget *widgets[2];
2832 enum snd_soc_dapm_direction dir;
2833 struct snd_soc_dapm_path *path;
2836 if (wsink->is_supply && !wsource->is_supply) {
2838 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2839 wsource->name, wsink->name);
2843 if (connected && !wsource->is_supply) {
2845 "connected() callback only supported for supply widgets (%s -> %s)\n",
2846 wsource->name, wsink->name);
2850 if (wsource->is_supply && control) {
2852 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2853 wsource->name, control, wsink->name);
2857 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2861 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2865 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2866 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2867 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2868 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2870 path->connected = connected;
2871 INIT_LIST_HEAD(&path->list);
2872 INIT_LIST_HEAD(&path->list_kcontrol);
2874 if (wsource->is_supply || wsink->is_supply)
2875 path->is_supply = 1;
2877 /* connect static paths */
2878 if (control == NULL) {
2881 switch (wsource->id) {
2882 case snd_soc_dapm_demux:
2883 ret = dapm_connect_mux(dapm, path, control, wsource);
2891 switch (wsink->id) {
2892 case snd_soc_dapm_mux:
2893 ret = dapm_connect_mux(dapm, path, control, wsink);
2897 case snd_soc_dapm_switch:
2898 case snd_soc_dapm_mixer:
2899 case snd_soc_dapm_mixer_named_ctl:
2900 ret = dapm_connect_mixer(dapm, path, control);
2909 list_add(&path->list, &dapm->card->paths);
2910 snd_soc_dapm_for_each_direction(dir)
2911 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2913 snd_soc_dapm_for_each_direction(dir) {
2914 dapm_update_widget_flags(widgets[dir]);
2915 dapm_mark_dirty(widgets[dir], "Route added");
2918 if (dapm->card->instantiated && path->connect)
2919 dapm_path_invalidate(path);
2927 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2928 const struct snd_soc_dapm_route *route)
2930 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2931 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2934 char prefixed_sink[80];
2935 char prefixed_source[80];
2937 unsigned int sink_ref = 0;
2938 unsigned int source_ref = 0;
2941 prefix = soc_dapm_prefix(dapm);
2943 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2944 prefix, route->sink);
2945 sink = prefixed_sink;
2946 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2947 prefix, route->source);
2948 source = prefixed_source;
2951 source = route->source;
2954 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2955 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2957 if (wsink && wsource)
2961 * find src and dest widgets over all widgets but favor a widget from
2962 * current DAPM context
2964 for_each_card_widgets(dapm->card, w) {
2965 if (!wsink && !(strcmp(w->name, sink))) {
2967 if (w->dapm == dapm) {
2975 "ASoC: sink widget %s overwritten\n",
2979 if (!wsource && !(strcmp(w->name, source))) {
2981 if (w->dapm == dapm) {
2989 "ASoC: source widget %s overwritten\n",
2993 /* use widget from another DAPM context if not found from this */
2999 if (wsource == NULL) {
3000 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3004 if (wsink == NULL) {
3005 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3011 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3012 dapm_wcache_update(&dapm->path_source_cache, wsource);
3014 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3021 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
3022 source, route->control, sink);
3026 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3027 const struct snd_soc_dapm_route *route)
3029 struct snd_soc_dapm_widget *wsource, *wsink;
3030 struct snd_soc_dapm_path *path, *p;
3033 char prefixed_sink[80];
3034 char prefixed_source[80];
3037 if (route->control) {
3039 "ASoC: Removal of routes with controls not supported\n");
3043 prefix = soc_dapm_prefix(dapm);
3045 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3046 prefix, route->sink);
3047 sink = prefixed_sink;
3048 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3049 prefix, route->source);
3050 source = prefixed_source;
3053 source = route->source;
3057 list_for_each_entry(p, &dapm->card->paths, list) {
3058 if (strcmp(p->source->name, source) != 0)
3060 if (strcmp(p->sink->name, sink) != 0)
3067 wsource = path->source;
3070 dapm_mark_dirty(wsource, "Route removed");
3071 dapm_mark_dirty(wsink, "Route removed");
3073 dapm_path_invalidate(path);
3075 dapm_free_path(path);
3077 /* Update any path related flags */
3078 dapm_update_widget_flags(wsource);
3079 dapm_update_widget_flags(wsink);
3081 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3089 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3090 * @dapm: DAPM context
3091 * @route: audio routes
3092 * @num: number of routes
3094 * Connects 2 dapm widgets together via a named audio path. The sink is
3095 * the widget receiving the audio signal, whilst the source is the sender
3096 * of the audio signal.
3098 * Returns 0 for success else error. On error all resources can be freed
3099 * with a call to snd_soc_card_free().
3101 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3102 const struct snd_soc_dapm_route *route, int num)
3106 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3107 for (i = 0; i < num; i++) {
3108 r = snd_soc_dapm_add_route(dapm, route);
3110 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3112 route->control ? route->control : "direct",
3118 mutex_unlock(&dapm->card->dapm_mutex);
3122 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3125 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3126 * @dapm: DAPM context
3127 * @route: audio routes
3128 * @num: number of routes
3130 * Removes routes from the DAPM context.
3132 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3133 const struct snd_soc_dapm_route *route, int num)
3137 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3138 for (i = 0; i < num; i++) {
3139 snd_soc_dapm_del_route(dapm, route);
3142 mutex_unlock(&dapm->card->dapm_mutex);
3146 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3148 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3149 const struct snd_soc_dapm_route *route)
3151 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3154 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3157 struct snd_soc_dapm_path *path;
3161 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3167 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3172 if (route->control || route->connected)
3173 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3174 route->source, route->sink);
3176 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3177 if (path->sink == sink) {
3184 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3185 route->source, route->sink);
3187 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3188 count, route->source, route->sink);
3194 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3195 * @dapm: DAPM context
3196 * @route: audio routes
3197 * @num: number of routes
3199 * Mark existing routes matching those specified in the passed array
3200 * as being weak, meaning that they are ignored for the purpose of
3201 * power decisions. The main intended use case is for sidetone paths
3202 * which couple audio between other independent paths if they are both
3203 * active in order to make the combination work better at the user
3204 * level but which aren't intended to be "used".
3206 * Note that CODEC drivers should not use this as sidetone type paths
3207 * can frequently also be used as bypass paths.
3209 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3210 const struct snd_soc_dapm_route *route, int num)
3215 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3216 for (i = 0; i < num; i++) {
3217 err = snd_soc_dapm_weak_route(dapm, route);
3222 mutex_unlock(&dapm->card->dapm_mutex);
3226 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3229 * snd_soc_dapm_new_widgets - add new dapm widgets
3230 * @card: card to be checked for new dapm widgets
3232 * Checks the codec for any new dapm widgets and creates them if found.
3234 * Returns 0 for success.
3236 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3238 struct snd_soc_dapm_widget *w;
3241 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3243 for_each_card_widgets(card, w)
3248 if (w->num_kcontrols) {
3249 w->kcontrols = kcalloc(w->num_kcontrols,
3250 sizeof(struct snd_kcontrol *),
3252 if (!w->kcontrols) {
3253 mutex_unlock(&card->dapm_mutex);
3259 case snd_soc_dapm_switch:
3260 case snd_soc_dapm_mixer:
3261 case snd_soc_dapm_mixer_named_ctl:
3264 case snd_soc_dapm_mux:
3265 case snd_soc_dapm_demux:
3268 case snd_soc_dapm_pga:
3269 case snd_soc_dapm_effect:
3270 case snd_soc_dapm_out_drv:
3273 case snd_soc_dapm_dai_link:
3274 dapm_new_dai_link(w);
3280 /* Read the initial power state from the device */
3282 val = soc_dapm_read(w->dapm, w->reg);
3283 val = val >> w->shift;
3285 if (val == w->on_val)
3291 dapm_mark_dirty(w, "new widget");
3292 dapm_debugfs_add_widget(w);
3295 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3296 mutex_unlock(&card->dapm_mutex);
3299 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3302 * snd_soc_dapm_get_volsw - dapm mixer get callback
3303 * @kcontrol: mixer control
3304 * @ucontrol: control element information
3306 * Callback to get the value of a dapm mixer control.
3308 * Returns 0 for success.
3310 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3311 struct snd_ctl_elem_value *ucontrol)
3313 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3314 struct snd_soc_card *card = dapm->card;
3315 struct soc_mixer_control *mc =
3316 (struct soc_mixer_control *)kcontrol->private_value;
3318 unsigned int shift = mc->shift;
3320 unsigned int width = fls(max);
3321 unsigned int mask = (1 << fls(max)) - 1;
3322 unsigned int invert = mc->invert;
3323 unsigned int reg_val, val, rval = 0;
3325 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3326 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3327 reg_val = soc_dapm_read(dapm, reg);
3328 val = (reg_val >> shift) & mask;
3330 if (reg != mc->rreg)
3331 reg_val = soc_dapm_read(dapm, mc->rreg);
3333 if (snd_soc_volsw_is_stereo(mc))
3334 rval = (reg_val >> mc->rshift) & mask;
3336 reg_val = dapm_kcontrol_get_value(kcontrol);
3337 val = reg_val & mask;
3339 if (snd_soc_volsw_is_stereo(mc))
3340 rval = (reg_val >> width) & mask;
3342 mutex_unlock(&card->dapm_mutex);
3345 ucontrol->value.integer.value[0] = max - val;
3347 ucontrol->value.integer.value[0] = val;
3349 if (snd_soc_volsw_is_stereo(mc)) {
3351 ucontrol->value.integer.value[1] = max - rval;
3353 ucontrol->value.integer.value[1] = rval;
3358 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3361 * snd_soc_dapm_put_volsw - dapm mixer set callback
3362 * @kcontrol: mixer control
3363 * @ucontrol: control element information
3365 * Callback to set the value of a dapm mixer control.
3367 * Returns 0 for success.
3369 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3370 struct snd_ctl_elem_value *ucontrol)
3372 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3373 struct snd_soc_card *card = dapm->card;
3374 struct soc_mixer_control *mc =
3375 (struct soc_mixer_control *)kcontrol->private_value;
3377 unsigned int shift = mc->shift;
3379 unsigned int width = fls(max);
3380 unsigned int mask = (1 << width) - 1;
3381 unsigned int invert = mc->invert;
3382 unsigned int val, rval = 0;
3383 int connect, rconnect = -1, change, reg_change = 0;
3384 struct snd_soc_dapm_update update = {};
3387 val = (ucontrol->value.integer.value[0] & mask);
3393 if (snd_soc_volsw_is_stereo(mc)) {
3394 rval = (ucontrol->value.integer.value[1] & mask);
3400 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3402 /* This assumes field width < (bits in unsigned int / 2) */
3403 if (width > sizeof(unsigned int) * 8 / 2)
3405 "ASoC: control %s field width limit exceeded\n",
3407 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3409 if (reg != SND_SOC_NOPM) {
3411 rval = rval << mc->rshift;
3413 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3415 if (snd_soc_volsw_is_stereo(mc))
3416 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3421 if (change || reg_change) {
3423 if (snd_soc_volsw_is_stereo(mc)) {
3424 update.has_second_set = true;
3425 update.reg2 = mc->rreg;
3426 update.mask2 = mask << mc->rshift;
3429 update.kcontrol = kcontrol;
3431 update.mask = mask << shift;
3433 card->update = &update;
3436 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3439 card->update = NULL;
3442 mutex_unlock(&card->dapm_mutex);
3445 snd_soc_dpcm_runtime_update(card);
3449 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3452 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3453 * @kcontrol: mixer control
3454 * @ucontrol: control element information
3456 * Callback to get the value of a dapm enumerated double mixer control.
3458 * Returns 0 for success.
3460 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3461 struct snd_ctl_elem_value *ucontrol)
3463 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3464 struct snd_soc_card *card = dapm->card;
3465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3466 unsigned int reg_val, val;
3468 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3469 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3470 reg_val = soc_dapm_read(dapm, e->reg);
3472 reg_val = dapm_kcontrol_get_value(kcontrol);
3474 mutex_unlock(&card->dapm_mutex);
3476 val = (reg_val >> e->shift_l) & e->mask;
3477 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3478 if (e->shift_l != e->shift_r) {
3479 val = (reg_val >> e->shift_r) & e->mask;
3480 val = snd_soc_enum_val_to_item(e, val);
3481 ucontrol->value.enumerated.item[1] = val;
3486 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3489 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3490 * @kcontrol: mixer control
3491 * @ucontrol: control element information
3493 * Callback to set the value of a dapm enumerated double mixer control.
3495 * Returns 0 for success.
3497 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3498 struct snd_ctl_elem_value *ucontrol)
3500 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3501 struct snd_soc_card *card = dapm->card;
3502 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3503 unsigned int *item = ucontrol->value.enumerated.item;
3504 unsigned int val, change, reg_change = 0;
3506 struct snd_soc_dapm_update update = {};
3509 if (item[0] >= e->items)
3512 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3513 mask = e->mask << e->shift_l;
3514 if (e->shift_l != e->shift_r) {
3515 if (item[1] > e->items)
3517 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3518 mask |= e->mask << e->shift_r;
3521 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3523 change = dapm_kcontrol_set_value(kcontrol, val);
3525 if (e->reg != SND_SOC_NOPM)
3526 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3528 if (change || reg_change) {
3530 update.kcontrol = kcontrol;
3531 update.reg = e->reg;
3534 card->update = &update;
3537 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3539 card->update = NULL;
3542 mutex_unlock(&card->dapm_mutex);
3545 snd_soc_dpcm_runtime_update(card);
3549 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3552 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3554 * @kcontrol: mixer control
3555 * @uinfo: control element information
3557 * Callback to provide information about a pin switch control.
3559 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3560 struct snd_ctl_elem_info *uinfo)
3562 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3564 uinfo->value.integer.min = 0;
3565 uinfo->value.integer.max = 1;
3569 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3572 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3574 * @kcontrol: mixer control
3577 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3578 struct snd_ctl_elem_value *ucontrol)
3580 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3581 const char *pin = (const char *)kcontrol->private_value;
3583 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3585 ucontrol->value.integer.value[0] =
3586 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3588 mutex_unlock(&card->dapm_mutex);
3592 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3595 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3597 * @kcontrol: mixer control
3600 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3601 struct snd_ctl_elem_value *ucontrol)
3603 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3604 const char *pin = (const char *)kcontrol->private_value;
3607 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3608 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3609 !!ucontrol->value.integer.value[0]);
3610 mutex_unlock(&card->dapm_mutex);
3612 snd_soc_dapm_sync(&card->dapm);
3615 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3617 struct snd_soc_dapm_widget *
3618 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3619 const struct snd_soc_dapm_widget *widget)
3621 enum snd_soc_dapm_direction dir;
3622 struct snd_soc_dapm_widget *w;
3626 if ((w = dapm_cnew_widget(widget)) == NULL)
3627 return ERR_PTR(-ENOMEM);
3630 case snd_soc_dapm_regulator_supply:
3631 w->regulator = devm_regulator_get(dapm->dev, w->name);
3632 if (IS_ERR(w->regulator)) {
3633 ret = PTR_ERR(w->regulator);
3634 goto request_failed;
3637 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3638 ret = regulator_allow_bypass(w->regulator, true);
3641 "ASoC: Failed to bypass %s: %d\n",
3645 case snd_soc_dapm_pinctrl:
3646 w->pinctrl = devm_pinctrl_get(dapm->dev);
3647 if (IS_ERR(w->pinctrl)) {
3648 ret = PTR_ERR(w->pinctrl);
3649 goto request_failed;
3652 /* set to sleep_state when initializing */
3653 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3655 case snd_soc_dapm_clock_supply:
3656 w->clk = devm_clk_get(dapm->dev, w->name);
3657 if (IS_ERR(w->clk)) {
3658 ret = PTR_ERR(w->clk);
3659 goto request_failed;
3666 prefix = soc_dapm_prefix(dapm);
3668 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3670 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3671 if (w->name == NULL) {
3672 kfree_const(w->sname);
3674 return ERR_PTR(-ENOMEM);
3678 case snd_soc_dapm_mic:
3679 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3680 w->power_check = dapm_generic_check_power;
3682 case snd_soc_dapm_input:
3683 if (!dapm->card->fully_routed)
3684 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3685 w->power_check = dapm_generic_check_power;
3687 case snd_soc_dapm_spk:
3688 case snd_soc_dapm_hp:
3689 w->is_ep = SND_SOC_DAPM_EP_SINK;
3690 w->power_check = dapm_generic_check_power;
3692 case snd_soc_dapm_output:
3693 if (!dapm->card->fully_routed)
3694 w->is_ep = SND_SOC_DAPM_EP_SINK;
3695 w->power_check = dapm_generic_check_power;
3697 case snd_soc_dapm_vmid:
3698 case snd_soc_dapm_siggen:
3699 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3700 w->power_check = dapm_always_on_check_power;
3702 case snd_soc_dapm_sink:
3703 w->is_ep = SND_SOC_DAPM_EP_SINK;
3704 w->power_check = dapm_always_on_check_power;
3707 case snd_soc_dapm_mux:
3708 case snd_soc_dapm_demux:
3709 case snd_soc_dapm_switch:
3710 case snd_soc_dapm_mixer:
3711 case snd_soc_dapm_mixer_named_ctl:
3712 case snd_soc_dapm_adc:
3713 case snd_soc_dapm_aif_out:
3714 case snd_soc_dapm_dac:
3715 case snd_soc_dapm_aif_in:
3716 case snd_soc_dapm_pga:
3717 case snd_soc_dapm_buffer:
3718 case snd_soc_dapm_scheduler:
3719 case snd_soc_dapm_effect:
3720 case snd_soc_dapm_src:
3721 case snd_soc_dapm_asrc:
3722 case snd_soc_dapm_encoder:
3723 case snd_soc_dapm_decoder:
3724 case snd_soc_dapm_out_drv:
3725 case snd_soc_dapm_micbias:
3726 case snd_soc_dapm_line:
3727 case snd_soc_dapm_dai_link:
3728 case snd_soc_dapm_dai_out:
3729 case snd_soc_dapm_dai_in:
3730 w->power_check = dapm_generic_check_power;
3732 case snd_soc_dapm_supply:
3733 case snd_soc_dapm_regulator_supply:
3734 case snd_soc_dapm_pinctrl:
3735 case snd_soc_dapm_clock_supply:
3736 case snd_soc_dapm_kcontrol:
3738 w->power_check = dapm_supply_check_power;
3741 w->power_check = dapm_always_on_check_power;
3746 INIT_LIST_HEAD(&w->list);
3747 INIT_LIST_HEAD(&w->dirty);
3748 /* see for_each_card_widgets */
3749 list_add_tail(&w->list, &dapm->card->widgets);
3751 snd_soc_dapm_for_each_direction(dir) {
3752 INIT_LIST_HEAD(&w->edges[dir]);
3753 w->endpoints[dir] = -1;
3756 /* machine layer sets up unconnected pins and insertions */
3761 if (ret != -EPROBE_DEFER)
3762 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3765 kfree_const(w->sname);
3767 return ERR_PTR(ret);
3771 * snd_soc_dapm_new_control - create new dapm control
3772 * @dapm: DAPM context
3773 * @widget: widget template
3775 * Creates new DAPM control based upon a template.
3777 * Returns a widget pointer on success or an error pointer on failure
3779 struct snd_soc_dapm_widget *
3780 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3781 const struct snd_soc_dapm_widget *widget)
3783 struct snd_soc_dapm_widget *w;
3785 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3786 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3787 mutex_unlock(&dapm->card->dapm_mutex);
3791 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3794 * snd_soc_dapm_new_controls - create new dapm controls
3795 * @dapm: DAPM context
3796 * @widget: widget array
3797 * @num: number of widgets
3799 * Creates new DAPM controls based upon the templates.
3801 * Returns 0 for success else error.
3803 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3804 const struct snd_soc_dapm_widget *widget,
3807 struct snd_soc_dapm_widget *w;
3811 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3812 for (i = 0; i < num; i++) {
3813 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3820 mutex_unlock(&dapm->card->dapm_mutex);
3823 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3826 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3827 struct snd_pcm_substream *substream)
3829 struct snd_soc_dapm_path *path;
3830 struct snd_soc_dai *source, *sink;
3831 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3832 struct snd_pcm_hw_params *params = NULL;
3833 const struct snd_soc_pcm_stream *config = NULL;
3834 struct snd_pcm_runtime *runtime = NULL;
3838 params = kzalloc(sizeof(*params), GFP_KERNEL);
3842 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3848 substream->runtime = runtime;
3850 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3851 snd_soc_dapm_widget_for_each_source_path(w, path) {
3852 source = path->source->priv;
3854 ret = snd_soc_dai_startup(source, substream);
3856 dev_err(source->dev,
3857 "ASoC: startup() failed: %d\n", ret);
3860 snd_soc_dai_activate(source, substream->stream);
3863 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3864 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3865 sink = path->sink->priv;
3867 ret = snd_soc_dai_startup(sink, substream);
3870 "ASoC: startup() failed: %d\n", ret);
3873 snd_soc_dai_activate(sink, substream->stream);
3876 substream->hw_opened = 1;
3879 * Note: getting the config after .startup() gives a chance to
3880 * either party on the link to alter the configuration if
3883 config = rtd->dai_link->params + rtd->params_select;
3884 if (WARN_ON(!config)) {
3885 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3890 /* Be a little careful as we don't want to overflow the mask array */
3891 if (config->formats) {
3892 fmt = ffs(config->formats) - 1;
3894 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3901 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3902 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3904 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3906 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3907 = config->channels_min;
3908 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3909 = config->channels_max;
3911 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3912 snd_soc_dapm_widget_for_each_source_path(w, path) {
3913 source = path->source->priv;
3915 ret = snd_soc_dai_hw_params(source, substream, params);
3919 dapm_update_dai_unlocked(substream, params, source);
3922 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3923 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3924 sink = path->sink->priv;
3926 ret = snd_soc_dai_hw_params(sink, substream, params);
3930 dapm_update_dai_unlocked(substream, params, sink);
3933 runtime->format = params_format(params);
3934 runtime->subformat = params_subformat(params);
3935 runtime->channels = params_channels(params);
3936 runtime->rate = params_rate(params);
3943 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3944 struct snd_kcontrol *kcontrol, int event)
3946 struct snd_soc_dapm_path *path;
3947 struct snd_soc_dai *source, *sink;
3948 struct snd_pcm_substream *substream = w->priv;
3949 int ret = 0, saved_stream = substream->stream;
3951 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3952 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3956 case SND_SOC_DAPM_PRE_PMU:
3957 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3963 case SND_SOC_DAPM_POST_PMU:
3964 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3965 sink = path->sink->priv;
3967 ret = snd_soc_dai_digital_mute(sink, 0,
3968 SNDRV_PCM_STREAM_PLAYBACK);
3969 if (ret != 0 && ret != -ENOTSUPP)
3971 "ASoC: Failed to unmute: %d\n", ret);
3976 case SND_SOC_DAPM_PRE_PMD:
3977 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3978 sink = path->sink->priv;
3980 ret = snd_soc_dai_digital_mute(sink, 1,
3981 SNDRV_PCM_STREAM_PLAYBACK);
3982 if (ret != 0 && ret != -ENOTSUPP)
3984 "ASoC: Failed to mute: %d\n", ret);
3988 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3989 snd_soc_dapm_widget_for_each_source_path(w, path) {
3990 source = path->source->priv;
3991 snd_soc_dai_hw_free(source, substream);
3994 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3995 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3996 sink = path->sink->priv;
3997 snd_soc_dai_hw_free(sink, substream);
4000 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
4001 snd_soc_dapm_widget_for_each_source_path(w, path) {
4002 source = path->source->priv;
4003 snd_soc_dai_deactivate(source, substream->stream);
4004 snd_soc_dai_shutdown(source, substream, 0);
4007 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4008 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4009 sink = path->sink->priv;
4010 snd_soc_dai_deactivate(sink, substream->stream);
4011 snd_soc_dai_shutdown(sink, substream, 0);
4015 case SND_SOC_DAPM_POST_PMD:
4016 kfree(substream->runtime);
4020 WARN(1, "Unknown event %d\n", event);
4025 /* Restore the substream direction */
4026 substream->stream = saved_stream;
4030 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4031 struct snd_ctl_elem_value *ucontrol)
4033 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4034 struct snd_soc_pcm_runtime *rtd = w->priv;
4036 ucontrol->value.enumerated.item[0] = rtd->params_select;
4041 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4042 struct snd_ctl_elem_value *ucontrol)
4044 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4045 struct snd_soc_pcm_runtime *rtd = w->priv;
4047 /* Can't change the config when widget is already powered */
4051 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4054 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4057 rtd->params_select = ucontrol->value.enumerated.item[0];
4063 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4064 unsigned long *private_value,
4066 const char **w_param_text)
4070 devm_kfree(card->dev, (void *)*private_value);
4075 for (count = 0 ; count < num_params; count++)
4076 devm_kfree(card->dev, (void *)w_param_text[count]);
4077 devm_kfree(card->dev, w_param_text);
4080 static struct snd_kcontrol_new *
4081 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4083 const struct snd_soc_pcm_stream *params,
4084 int num_params, const char **w_param_text,
4085 unsigned long *private_value)
4087 struct soc_enum w_param_enum[] = {
4088 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4090 struct snd_kcontrol_new kcontrol_dai_link[] = {
4091 SOC_ENUM_EXT(NULL, w_param_enum[0],
4092 snd_soc_dapm_dai_link_get,
4093 snd_soc_dapm_dai_link_put),
4095 struct snd_kcontrol_new *kcontrol_news;
4096 const struct snd_soc_pcm_stream *config = params;
4099 for (count = 0 ; count < num_params; count++) {
4100 if (!config->stream_name) {
4101 dev_warn(card->dapm.dev,
4102 "ASoC: anonymous config %d for dai link %s\n",
4104 w_param_text[count] =
4105 devm_kasprintf(card->dev, GFP_KERNEL,
4106 "Anonymous Configuration %d",
4109 w_param_text[count] = devm_kmemdup(card->dev,
4110 config->stream_name,
4111 strlen(config->stream_name) + 1,
4114 if (!w_param_text[count])
4115 goto outfree_w_param;
4119 w_param_enum[0].items = num_params;
4120 w_param_enum[0].texts = w_param_text;
4123 (unsigned long) devm_kmemdup(card->dev,
4124 (void *)(kcontrol_dai_link[0].private_value),
4125 sizeof(struct soc_enum), GFP_KERNEL);
4126 if (!*private_value) {
4127 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4129 goto outfree_w_param;
4131 kcontrol_dai_link[0].private_value = *private_value;
4132 /* duplicate kcontrol_dai_link on heap so that memory persists */
4133 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4134 sizeof(struct snd_kcontrol_new),
4136 if (!kcontrol_news) {
4137 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4139 goto outfree_w_param;
4141 return kcontrol_news;
4144 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4148 static struct snd_soc_dapm_widget *
4149 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4150 struct snd_pcm_substream *substream,
4153 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4154 struct snd_soc_dapm_widget template;
4155 struct snd_soc_dapm_widget *w;
4156 const char **w_param_text;
4157 unsigned long private_value = 0;
4161 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4162 rtd->dai_link->name, id);
4164 return ERR_PTR(-ENOMEM);
4166 memset(&template, 0, sizeof(template));
4167 template.reg = SND_SOC_NOPM;
4168 template.id = snd_soc_dapm_dai_link;
4169 template.name = link_name;
4170 template.event = snd_soc_dai_link_event;
4171 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4172 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4173 template.kcontrol_news = NULL;
4175 /* allocate memory for control, only in case of multiple configs */
4176 if (rtd->dai_link->num_params > 1) {
4177 w_param_text = devm_kcalloc(card->dev,
4178 rtd->dai_link->num_params,
4179 sizeof(char *), GFP_KERNEL);
4180 if (!w_param_text) {
4185 template.num_kcontrols = 1;
4186 template.kcontrol_news =
4187 snd_soc_dapm_alloc_kcontrol(card,
4189 rtd->dai_link->params,
4190 rtd->dai_link->num_params,
4191 w_param_text, &private_value);
4192 if (!template.kcontrol_news) {
4197 w_param_text = NULL;
4199 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4201 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4204 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4206 goto outfree_kcontrol_news;
4209 w->priv = substream;
4213 outfree_kcontrol_news:
4214 devm_kfree(card->dev, (void *)template.kcontrol_news);
4215 snd_soc_dapm_free_kcontrol(card, &private_value,
4216 rtd->dai_link->num_params, w_param_text);
4218 devm_kfree(card->dev, link_name);
4219 return ERR_PTR(ret);
4222 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4223 struct snd_soc_dai *dai)
4225 struct snd_soc_dapm_widget template;
4226 struct snd_soc_dapm_widget *w;
4228 WARN_ON(dapm->dev != dai->dev);
4230 memset(&template, 0, sizeof(template));
4231 template.reg = SND_SOC_NOPM;
4233 if (dai->driver->playback.stream_name) {
4234 template.id = snd_soc_dapm_dai_in;
4235 template.name = dai->driver->playback.stream_name;
4236 template.sname = dai->driver->playback.stream_name;
4238 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4241 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4246 dai->playback_widget = w;
4249 if (dai->driver->capture.stream_name) {
4250 template.id = snd_soc_dapm_dai_out;
4251 template.name = dai->driver->capture.stream_name;
4252 template.sname = dai->driver->capture.stream_name;
4254 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4257 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4262 dai->capture_widget = w;
4268 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4270 struct snd_soc_dapm_widget *dai_w, *w;
4271 struct snd_soc_dapm_widget *src, *sink;
4272 struct snd_soc_dai *dai;
4274 /* For each DAI widget... */
4275 for_each_card_widgets(card, dai_w) {
4276 switch (dai_w->id) {
4277 case snd_soc_dapm_dai_in:
4278 case snd_soc_dapm_dai_out:
4284 /* let users know there is no DAI to link */
4286 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4293 /* ...find all widgets with the same stream and link them */
4294 for_each_card_widgets(card, w) {
4295 if (w->dapm != dai_w->dapm)
4299 case snd_soc_dapm_dai_in:
4300 case snd_soc_dapm_dai_out:
4306 if (!w->sname || !strstr(w->sname, dai_w->sname))
4309 if (dai_w->id == snd_soc_dapm_dai_in) {
4316 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4317 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4324 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4325 struct snd_soc_dai *src_dai,
4326 struct snd_soc_dapm_widget *src,
4327 struct snd_soc_dapm_widget *dai,
4328 struct snd_soc_dai *sink_dai,
4329 struct snd_soc_dapm_widget *sink)
4331 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4332 src_dai->component->name, src->name,
4333 sink_dai->component->name, sink->name);
4336 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4340 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4343 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4344 struct snd_soc_pcm_runtime *rtd,
4345 struct snd_soc_dai *codec_dai,
4346 struct snd_soc_dai *cpu_dai)
4348 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4349 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4350 struct snd_pcm_substream *substream;
4351 struct snd_pcm_str *streams = rtd->pcm->streams;
4353 if (dai_link->params) {
4354 playback_cpu = cpu_dai->capture_widget;
4355 capture_cpu = cpu_dai->playback_widget;
4357 playback_cpu = cpu_dai->playback_widget;
4358 capture_cpu = cpu_dai->capture_widget;
4361 /* connect BE DAI playback if widgets are valid */
4362 codec = codec_dai->playback_widget;
4364 if (playback_cpu && codec) {
4365 if (dai_link->params && !rtd->playback_widget) {
4366 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4367 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4370 rtd->playback_widget = dai;
4373 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4374 rtd->playback_widget,
4379 /* connect BE DAI capture if widgets are valid */
4380 codec = codec_dai->capture_widget;
4382 if (codec && capture_cpu) {
4383 if (dai_link->params && !rtd->capture_widget) {
4384 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4385 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4388 rtd->capture_widget = dai;
4391 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4392 rtd->capture_widget,
4393 cpu_dai, capture_cpu);
4397 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4400 struct snd_soc_dapm_widget *w;
4403 w = snd_soc_dai_get_widget(dai, stream);
4406 dapm_mark_dirty(w, "stream event");
4408 if (w->id == snd_soc_dapm_dai_in) {
4409 ep = SND_SOC_DAPM_EP_SOURCE;
4410 dapm_widget_invalidate_input_paths(w);
4412 ep = SND_SOC_DAPM_EP_SINK;
4413 dapm_widget_invalidate_output_paths(w);
4417 case SND_SOC_DAPM_STREAM_START:
4421 case SND_SOC_DAPM_STREAM_STOP:
4425 case SND_SOC_DAPM_STREAM_SUSPEND:
4426 case SND_SOC_DAPM_STREAM_RESUME:
4427 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4428 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4434 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4436 struct snd_soc_pcm_runtime *rtd;
4437 struct snd_soc_dai *codec_dai;
4440 /* for each BE DAI link... */
4441 for_each_card_rtds(card, rtd) {
4443 * dynamic FE links have no fixed DAI mapping.
4444 * CODEC<->CODEC links have no direct connection.
4446 if (rtd->dai_link->dynamic)
4449 if (rtd->num_cpus == 1) {
4450 for_each_rtd_codec_dais(rtd, i, codec_dai)
4451 dapm_connect_dai_pair(card, rtd, codec_dai,
4452 asoc_rtd_to_cpu(rtd, 0));
4453 } else if (rtd->num_codecs == rtd->num_cpus) {
4454 for_each_rtd_codec_dais(rtd, i, codec_dai)
4455 dapm_connect_dai_pair(card, rtd, codec_dai,
4456 asoc_rtd_to_cpu(rtd, i));
4459 "N cpus to M codecs link is not supported yet\n");
4464 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4467 struct snd_soc_dai *dai;
4470 for_each_rtd_dais(rtd, i, dai)
4471 soc_dapm_dai_stream_event(dai, stream, event);
4473 dapm_power_widgets(rtd->card, event);
4477 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4478 * @rtd: PCM runtime data
4479 * @stream: stream name
4480 * @event: stream event
4482 * Sends a stream event to the dapm core. The core then makes any
4483 * necessary widget power changes.
4485 * Returns 0 for success else error.
4487 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4490 struct snd_soc_card *card = rtd->card;
4492 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4493 soc_dapm_stream_event(rtd, stream, event);
4494 mutex_unlock(&card->dapm_mutex);
4497 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4499 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4500 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4501 /* powered down playback stream now */
4502 snd_soc_dapm_stream_event(rtd,
4503 SNDRV_PCM_STREAM_PLAYBACK,
4504 SND_SOC_DAPM_STREAM_STOP);
4506 /* start delayed pop wq here for playback streams */
4508 queue_delayed_work(system_power_efficient_wq,
4510 msecs_to_jiffies(rtd->pmdown_time));
4513 /* capture streams can be powered down now */
4514 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4515 SND_SOC_DAPM_STREAM_STOP);
4518 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4521 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4522 * @dapm: DAPM context
4525 * Enables input/output pin and its parents or children widgets iff there is
4526 * a valid audio route and active audio stream.
4528 * Requires external locking.
4530 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4531 * do any widget power switching.
4533 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4536 return snd_soc_dapm_set_pin(dapm, pin, 1);
4538 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4541 * snd_soc_dapm_enable_pin - enable pin.
4542 * @dapm: DAPM context
4545 * Enables input/output pin and its parents or children widgets iff there is
4546 * a valid audio route and active audio stream.
4548 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4549 * do any widget power switching.
4551 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4555 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4557 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4559 mutex_unlock(&dapm->card->dapm_mutex);
4563 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4566 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4567 * @dapm: DAPM context
4570 * Enables input/output pin regardless of any other state. This is
4571 * intended for use with microphone bias supplies used in microphone
4574 * Requires external locking.
4576 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4577 * do any widget power switching.
4579 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4582 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4585 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4589 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4590 if (!w->connected) {
4592 * w->force does not affect the number of input or output paths,
4593 * so we only have to recheck if w->connected is changed
4595 dapm_widget_invalidate_input_paths(w);
4596 dapm_widget_invalidate_output_paths(w);
4600 dapm_mark_dirty(w, "force enable");
4604 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4607 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4608 * @dapm: DAPM context
4611 * Enables input/output pin regardless of any other state. This is
4612 * intended for use with microphone bias supplies used in microphone
4615 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4616 * do any widget power switching.
4618 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4623 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4625 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4627 mutex_unlock(&dapm->card->dapm_mutex);
4631 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4634 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4635 * @dapm: DAPM context
4638 * Disables input/output pin and its parents or children widgets.
4640 * Requires external locking.
4642 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4643 * do any widget power switching.
4645 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4648 return snd_soc_dapm_set_pin(dapm, pin, 0);
4650 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4653 * snd_soc_dapm_disable_pin - disable pin.
4654 * @dapm: DAPM context
4657 * Disables input/output pin and its parents or children widgets.
4659 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4660 * do any widget power switching.
4662 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4667 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4669 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4671 mutex_unlock(&dapm->card->dapm_mutex);
4675 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4678 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4679 * @dapm: DAPM context
4682 * Marks the specified pin as being not connected, disabling it along
4683 * any parent or child widgets. At present this is identical to
4684 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4685 * additional things such as disabling controls which only affect
4686 * paths through the pin.
4688 * Requires external locking.
4690 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4691 * do any widget power switching.
4693 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4696 return snd_soc_dapm_set_pin(dapm, pin, 0);
4698 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4701 * snd_soc_dapm_nc_pin - permanently disable pin.
4702 * @dapm: DAPM context
4705 * Marks the specified pin as being not connected, disabling it along
4706 * any parent or child widgets. At present this is identical to
4707 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4708 * additional things such as disabling controls which only affect
4709 * paths through the pin.
4711 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4712 * do any widget power switching.
4714 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4718 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4720 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4722 mutex_unlock(&dapm->card->dapm_mutex);
4726 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4729 * snd_soc_dapm_get_pin_status - get audio pin status
4730 * @dapm: DAPM context
4731 * @pin: audio signal pin endpoint (or start point)
4733 * Get audio pin status - connected or disconnected.
4735 * Returns 1 for connected otherwise 0.
4737 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4740 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4743 return w->connected;
4747 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4750 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4751 * @dapm: DAPM context
4752 * @pin: audio signal pin endpoint (or start point)
4754 * Mark the given endpoint or pin as ignoring suspend. When the
4755 * system is disabled a path between two endpoints flagged as ignoring
4756 * suspend will not be disabled. The path must already be enabled via
4757 * normal means at suspend time, it will not be turned on if it was not
4760 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4763 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4766 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4770 w->ignore_suspend = 1;
4774 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4777 * snd_soc_dapm_free - free dapm resources
4778 * @dapm: DAPM context
4780 * Free all dapm widgets and resources.
4782 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4784 dapm_debugfs_cleanup(dapm);
4785 dapm_free_widgets(dapm);
4786 list_del(&dapm->list);
4788 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4790 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4791 struct snd_soc_card *card,
4792 struct snd_soc_component *component)
4795 dapm->component = component;
4796 dapm->bias_level = SND_SOC_BIAS_OFF;
4799 dapm->dev = component->dev;
4800 dapm->idle_bias_off = !component->driver->idle_bias_on,
4801 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4803 dapm->dev = card->dev;
4806 INIT_LIST_HEAD(&dapm->list);
4807 /* see for_each_card_dapms */
4808 list_add(&dapm->list, &card->dapm_list);
4810 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4812 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4814 struct snd_soc_card *card = dapm->card;
4815 struct snd_soc_dapm_widget *w;
4816 LIST_HEAD(down_list);
4819 mutex_lock(&card->dapm_mutex);
4821 for_each_card_widgets(dapm->card, w) {
4822 if (w->dapm != dapm)
4825 dapm_seq_insert(w, &down_list, false);
4831 /* If there were no widgets to power down we're already in
4835 if (dapm->bias_level == SND_SOC_BIAS_ON)
4836 snd_soc_dapm_set_bias_level(dapm,
4837 SND_SOC_BIAS_PREPARE);
4838 dapm_seq_run(card, &down_list, 0, false);
4839 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4840 snd_soc_dapm_set_bias_level(dapm,
4841 SND_SOC_BIAS_STANDBY);
4844 mutex_unlock(&card->dapm_mutex);
4848 * snd_soc_dapm_shutdown - callback for system shutdown
4850 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4852 struct snd_soc_dapm_context *dapm;
4854 for_each_card_dapms(card, dapm) {
4855 if (dapm != &card->dapm) {
4856 soc_dapm_shutdown_dapm(dapm);
4857 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4858 snd_soc_dapm_set_bias_level(dapm,
4863 soc_dapm_shutdown_dapm(&card->dapm);
4864 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4865 snd_soc_dapm_set_bias_level(&card->dapm,
4869 /* Module information */
4870 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4871 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4872 MODULE_LICENSE("GPL");