1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 for_each_card_widgets(card, w) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
343 struct dapm_kcontrol_data {
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 INIT_LIST_HEAD(&data->paths);
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
391 template.off_val = mc->max;
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
398 data->value = template.on_val;
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask << e->shift_l;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
433 data->value = template.on_val;
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
451 kcontrol->private_data = data;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
485 n = data->wlist->num_widgets + 1;
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
550 data->widget->on_val = value;
558 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
560 * @kcontrol: The kcontrol
562 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
563 struct snd_kcontrol *kcontrol)
565 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
567 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
570 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
572 * @kcontrol: The kcontrol
574 * Note: This function must only be used on kcontrols that are known to have
575 * been registered for a CODEC. Otherwise the behaviour is undefined.
577 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
578 struct snd_kcontrol *kcontrol)
580 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
582 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
584 static void dapm_reset(struct snd_soc_card *card)
586 struct snd_soc_dapm_widget *w;
588 lockdep_assert_held(&card->dapm_mutex);
590 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
592 for_each_card_widgets(card, w) {
593 w->new_power = w->power;
594 w->power_checked = false;
598 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
600 if (!dapm->component)
602 return dapm->component->name_prefix;
605 static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
608 if (!dapm->component)
610 return snd_soc_component_read(dapm->component, reg, value);
613 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
614 int reg, unsigned int mask, unsigned int value)
616 if (!dapm->component)
618 return snd_soc_component_update_bits(dapm->component, reg,
622 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
623 int reg, unsigned int mask, unsigned int value)
625 if (!dapm->component)
627 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
630 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
633 snd_soc_component_async_complete(dapm->component);
636 static struct snd_soc_dapm_widget *
637 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
639 struct snd_soc_dapm_widget *w = wcache->widget;
640 struct list_head *wlist;
645 wlist = &w->dapm->card->widgets;
647 list_for_each_entry_from(w, wlist, list) {
648 if (!strcmp(name, w->name))
659 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
660 struct snd_soc_dapm_widget *w)
666 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
667 * @dapm: The DAPM context for which to set the level
668 * @level: The level to set
670 * Forces the DAPM bias level to a specific state. It will call the bias level
671 * callback of DAPM context with the specified level. This will even happen if
672 * the context is already at the same level. Furthermore it will not go through
673 * the normal bias level sequencing, meaning any intermediate states between the
674 * current and the target state will not be entered.
676 * Note that the change in bias level is only temporary and the next time
677 * snd_soc_dapm_sync() is called the state will be set to the level as
678 * determined by the DAPM core. The function is mainly intended to be used to
679 * used during probe or resume from suspend to power up the device so
680 * initialization can be done, before the DAPM core takes over.
682 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
683 enum snd_soc_bias_level level)
688 ret = snd_soc_component_set_bias_level(dapm->component, level);
691 dapm->bias_level = level;
695 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
698 * snd_soc_dapm_set_bias_level - set the bias level for the system
699 * @dapm: DAPM context
700 * @level: level to configure
702 * Configure the bias (power) levels for the SoC audio device.
704 * Returns 0 for success else error.
706 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
707 enum snd_soc_bias_level level)
709 struct snd_soc_card *card = dapm->card;
712 trace_snd_soc_bias_level_start(card, level);
714 if (card && card->set_bias_level)
715 ret = card->set_bias_level(card, dapm, level);
719 if (!card || dapm != &card->dapm)
720 ret = snd_soc_dapm_force_bias_level(dapm, level);
725 if (card && card->set_bias_level_post)
726 ret = card->set_bias_level_post(card, dapm, level);
728 trace_snd_soc_bias_level_done(card, level);
733 /* connect mux widget to its interconnecting audio paths */
734 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
735 struct snd_soc_dapm_path *path, const char *control_name,
736 struct snd_soc_dapm_widget *w)
738 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
739 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
740 unsigned int val, item;
743 if (e->reg != SND_SOC_NOPM) {
744 soc_dapm_read(dapm, e->reg, &val);
745 val = (val >> e->shift_l) & e->mask;
746 item = snd_soc_enum_val_to_item(e, val);
748 /* since a virtual mux has no backing registers to
749 * decide which path to connect, it will try to match
750 * with the first enumeration. This is to ensure
751 * that the default mux choice (the first) will be
752 * correctly powered up during initialization.
757 i = match_string(e->texts, e->items, control_name);
761 path->name = e->texts[i];
762 path->connect = (i == item);
767 /* set up initial codec paths */
768 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
771 struct soc_mixer_control *mc = (struct soc_mixer_control *)
772 p->sink->kcontrol_news[i].private_value;
773 unsigned int reg = mc->reg;
774 unsigned int shift = mc->shift;
775 unsigned int max = mc->max;
776 unsigned int mask = (1 << fls(max)) - 1;
777 unsigned int invert = mc->invert;
780 if (reg != SND_SOC_NOPM) {
781 soc_dapm_read(p->sink->dapm, reg, &val);
783 * The nth_path argument allows this function to know
784 * which path of a kcontrol it is setting the initial
785 * status for. Ideally this would support any number
786 * of paths and channels. But since kcontrols only come
787 * in mono and stereo variants, we are limited to 2
790 * The following code assumes for stereo controls the
791 * first path is the left channel, and all remaining
792 * paths are the right channel.
794 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
796 soc_dapm_read(p->sink->dapm, mc->rreg, &val);
797 val = (val >> mc->rshift) & mask;
799 val = (val >> shift) & mask;
805 /* since a virtual mixer has no backing registers to
806 * decide which path to connect, it will try to match
807 * with initial state. This is to ensure
808 * that the default mixer choice will be
809 * correctly powered up during initialization.
815 /* connect mixer widget to its interconnecting audio paths */
816 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
817 struct snd_soc_dapm_path *path, const char *control_name)
821 /* search for mixer kcontrol */
822 for (i = 0; i < path->sink->num_kcontrols; i++) {
823 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
824 path->name = path->sink->kcontrol_news[i].name;
825 dapm_set_mixer_path_status(path, i, nth_path++);
832 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
833 struct snd_soc_dapm_widget *kcontrolw,
834 const struct snd_kcontrol_new *kcontrol_new,
835 struct snd_kcontrol **kcontrol)
837 struct snd_soc_dapm_widget *w;
842 for_each_card_widgets(dapm->card, w) {
843 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
845 for (i = 0; i < w->num_kcontrols; i++) {
846 if (&w->kcontrol_news[i] == kcontrol_new) {
848 *kcontrol = w->kcontrols[i];
858 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
859 * create it. Either way, add the widget into the control's widget list
861 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
864 struct snd_soc_dapm_context *dapm = w->dapm;
865 struct snd_card *card = dapm->card->snd_card;
869 struct snd_kcontrol *kcontrol;
870 bool wname_in_long_name, kcname_in_long_name;
871 char *long_name = NULL;
875 prefix = soc_dapm_prefix(dapm);
877 prefix_len = strlen(prefix) + 1;
881 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
886 wname_in_long_name = false;
887 kcname_in_long_name = true;
890 case snd_soc_dapm_switch:
891 case snd_soc_dapm_mixer:
892 case snd_soc_dapm_pga:
893 case snd_soc_dapm_effect:
894 case snd_soc_dapm_out_drv:
895 wname_in_long_name = true;
896 kcname_in_long_name = true;
898 case snd_soc_dapm_mixer_named_ctl:
899 wname_in_long_name = false;
900 kcname_in_long_name = true;
902 case snd_soc_dapm_demux:
903 case snd_soc_dapm_mux:
904 wname_in_long_name = true;
905 kcname_in_long_name = false;
912 if (wname_in_long_name && kcname_in_long_name) {
914 * The control will get a prefix from the control
915 * creation process but we're also using the same
916 * prefix for widgets so cut the prefix off the
917 * front of the widget name.
919 long_name = kasprintf(GFP_KERNEL, "%s %s",
920 w->name + prefix_len,
921 w->kcontrol_news[kci].name);
922 if (long_name == NULL)
926 } else if (wname_in_long_name) {
928 name = w->name + prefix_len;
931 name = w->kcontrol_news[kci].name;
934 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
941 kcontrol->private_free = dapm_kcontrol_free;
943 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
945 snd_ctl_free_one(kcontrol);
949 ret = snd_ctl_add(card, kcontrol);
952 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
958 ret = dapm_kcontrol_add_widget(kcontrol, w);
960 w->kcontrols[kci] = kcontrol;
968 /* create new dapm mixer control */
969 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
972 struct snd_soc_dapm_path *path;
973 struct dapm_kcontrol_data *data;
976 for (i = 0; i < w->num_kcontrols; i++) {
978 snd_soc_dapm_widget_for_each_source_path(w, path) {
979 /* mixer/mux paths name must match control name */
980 if (path->name != (char *)w->kcontrol_news[i].name)
983 if (!w->kcontrols[i]) {
984 ret = dapm_create_or_share_kcontrol(w, i);
989 dapm_kcontrol_add_path(w->kcontrols[i], path);
991 data = snd_kcontrol_chip(w->kcontrols[i]);
993 snd_soc_dapm_add_path(data->widget->dapm,
1003 /* create new dapm mux control */
1004 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1006 struct snd_soc_dapm_context *dapm = w->dapm;
1007 enum snd_soc_dapm_direction dir;
1008 struct snd_soc_dapm_path *path;
1013 case snd_soc_dapm_mux:
1014 dir = SND_SOC_DAPM_DIR_OUT;
1017 case snd_soc_dapm_demux:
1018 dir = SND_SOC_DAPM_DIR_IN;
1025 if (w->num_kcontrols != 1) {
1027 "ASoC: %s %s has incorrect number of controls\n", type,
1032 if (list_empty(&w->edges[dir])) {
1033 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1037 ret = dapm_create_or_share_kcontrol(w, 0);
1041 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1043 dapm_kcontrol_add_path(w->kcontrols[0], path);
1049 /* create new dapm volume control */
1050 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1054 for (i = 0; i < w->num_kcontrols; i++) {
1055 ret = dapm_create_or_share_kcontrol(w, i);
1063 /* create new dapm dai link control */
1064 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1067 struct snd_kcontrol *kcontrol;
1068 struct snd_soc_dapm_context *dapm = w->dapm;
1069 struct snd_card *card = dapm->card->snd_card;
1070 struct snd_soc_pcm_runtime *rtd = w->priv;
1072 /* create control for links with > 1 config */
1073 if (rtd->dai_link->num_params <= 1)
1077 for (i = 0; i < w->num_kcontrols; i++) {
1078 kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
1080 ret = snd_ctl_add(card, kcontrol);
1083 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1084 w->name, w->kcontrol_news[i].name, ret);
1087 kcontrol->private_data = w;
1088 w->kcontrols[i] = kcontrol;
1094 /* We implement power down on suspend by checking the power state of
1095 * the ALSA card - when we are suspending the ALSA state for the card
1098 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1100 int level = snd_power_get_state(widget->dapm->card->snd_card);
1103 case SNDRV_CTL_POWER_D3hot:
1104 case SNDRV_CTL_POWER_D3cold:
1105 if (widget->ignore_suspend)
1106 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1108 return widget->ignore_suspend;
1114 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1119 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1120 struct list_head *widgets)
1122 struct snd_soc_dapm_widget *w;
1123 struct list_head *it;
1124 unsigned int size = 0;
1127 list_for_each(it, widgets)
1130 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1134 list_for_each_entry(w, widgets, work_list)
1135 (*list)->widgets[i++] = w;
1137 (*list)->num_widgets = i;
1143 * Recursively reset the cached number of inputs or outputs for the specified
1144 * widget and all widgets that can be reached via incoming or outcoming paths
1147 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1148 enum snd_soc_dapm_direction dir)
1150 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1151 struct snd_soc_dapm_path *path;
1153 widget->endpoints[dir] = -1;
1155 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1156 if (path->weak || path->is_supply)
1162 if (path->connect) {
1164 invalidate_paths_ep(path->node[dir], dir);
1171 * Common implementation for is_connected_output_ep() and
1172 * is_connected_input_ep(). The function is inlined since the combined size of
1173 * the two specialized functions is only marginally larger then the size of the
1174 * generic function and at the same time the fast path of the specialized
1175 * functions is significantly smaller than the generic function.
1177 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1178 struct list_head *list, enum snd_soc_dapm_direction dir,
1179 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1180 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1181 enum snd_soc_dapm_direction)),
1182 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1183 enum snd_soc_dapm_direction))
1185 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1186 struct snd_soc_dapm_path *path;
1189 if (widget->endpoints[dir] >= 0)
1190 return widget->endpoints[dir];
1192 DAPM_UPDATE_STAT(widget, path_checks);
1194 /* do we need to add this widget to the list ? */
1196 list_add_tail(&widget->work_list, list);
1198 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1200 custom_stop_condition = NULL;
1203 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1204 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1205 return widget->endpoints[dir];
1208 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1209 DAPM_UPDATE_STAT(widget, neighbour_checks);
1211 if (path->weak || path->is_supply)
1217 trace_snd_soc_dapm_path(widget, dir, path);
1219 if (path->connect) {
1221 con += fn(path->node[dir], list, custom_stop_condition);
1226 widget->endpoints[dir] = con;
1232 * Recursively check for a completed path to an active or physically connected
1233 * output widget. Returns number of complete paths.
1235 * Optionally, can be supplied with a function acting as a stopping condition.
1236 * This function takes the dapm widget currently being examined and the walk
1237 * direction as an arguments, it should return true if widgets from that point
1238 * in the graph onwards should not be added to the widget list.
1240 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1241 struct list_head *list,
1242 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1243 enum snd_soc_dapm_direction))
1245 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1246 is_connected_output_ep, custom_stop_condition);
1250 * Recursively check for a completed path to an active or physically connected
1251 * input widget. Returns number of complete paths.
1253 * Optionally, can be supplied with a function acting as a stopping condition.
1254 * This function takes the dapm widget currently being examined and the walk
1255 * direction as an arguments, it should return true if the walk should be
1256 * stopped and false otherwise.
1258 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1259 struct list_head *list,
1260 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1261 enum snd_soc_dapm_direction))
1263 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1264 is_connected_input_ep, custom_stop_condition);
1268 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1269 * @dai: the soc DAI.
1270 * @stream: stream direction.
1271 * @list: list of active widgets for this stream.
1272 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1273 * walk based on custom logic.
1275 * Queries DAPM graph as to whether a valid audio stream path exists for
1276 * the initial stream specified by name. This takes into account
1277 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1279 * Optionally, can be supplied with a function acting as a stopping condition.
1280 * This function takes the dapm widget currently being examined and the walk
1281 * direction as an arguments, it should return true if the walk should be
1282 * stopped and false otherwise.
1284 * Returns the number of valid paths or negative error.
1286 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1287 struct snd_soc_dapm_widget_list **list,
1288 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1289 enum snd_soc_dapm_direction))
1291 struct snd_soc_card *card = dai->component->card;
1292 struct snd_soc_dapm_widget *w;
1297 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1299 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1300 w = dai->playback_widget;
1301 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1302 paths = is_connected_output_ep(w, &widgets,
1303 custom_stop_condition);
1305 w = dai->capture_widget;
1306 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1307 paths = is_connected_input_ep(w, &widgets,
1308 custom_stop_condition);
1311 /* Drop starting point */
1312 list_del(widgets.next);
1314 ret = dapm_widget_list_create(list, &widgets);
1318 trace_snd_soc_dapm_connected(paths, stream);
1319 mutex_unlock(&card->dapm_mutex);
1324 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1326 dapm_widget_list_free(list);
1330 * Handler for regulator supply widget.
1332 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1333 struct snd_kcontrol *kcontrol, int event)
1337 soc_dapm_async_complete(w->dapm);
1339 if (SND_SOC_DAPM_EVENT_ON(event)) {
1340 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1341 ret = regulator_allow_bypass(w->regulator, false);
1343 dev_warn(w->dapm->dev,
1344 "ASoC: Failed to unbypass %s: %d\n",
1348 return regulator_enable(w->regulator);
1350 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1351 ret = regulator_allow_bypass(w->regulator, true);
1353 dev_warn(w->dapm->dev,
1354 "ASoC: Failed to bypass %s: %d\n",
1358 return regulator_disable_deferred(w->regulator, w->shift);
1361 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1364 * Handler for pinctrl widget.
1366 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1367 struct snd_kcontrol *kcontrol, int event)
1369 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1370 struct pinctrl *p = w->pinctrl;
1371 struct pinctrl_state *s;
1376 if (SND_SOC_DAPM_EVENT_ON(event))
1377 s = pinctrl_lookup_state(p, priv->active_state);
1379 s = pinctrl_lookup_state(p, priv->sleep_state);
1384 return pinctrl_select_state(p, s);
1386 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1389 * Handler for clock supply widget.
1391 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1392 struct snd_kcontrol *kcontrol, int event)
1397 soc_dapm_async_complete(w->dapm);
1399 if (SND_SOC_DAPM_EVENT_ON(event)) {
1400 return clk_prepare_enable(w->clk);
1402 clk_disable_unprepare(w->clk);
1408 EXPORT_SYMBOL_GPL(dapm_clock_event);
1410 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1412 if (w->power_checked)
1413 return w->new_power;
1418 w->new_power = w->power_check(w);
1420 w->power_checked = true;
1422 return w->new_power;
1425 /* Generic check to see if a widget should be powered. */
1426 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1430 DAPM_UPDATE_STAT(w, power_checks);
1432 in = is_connected_input_ep(w, NULL, NULL);
1433 out = is_connected_output_ep(w, NULL, NULL);
1434 return out != 0 && in != 0;
1437 /* Check to see if a power supply is needed */
1438 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1440 struct snd_soc_dapm_path *path;
1442 DAPM_UPDATE_STAT(w, power_checks);
1444 /* Check if one of our outputs is connected */
1445 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1446 DAPM_UPDATE_STAT(w, neighbour_checks);
1451 if (path->connected &&
1452 !path->connected(path->source, path->sink))
1455 if (dapm_widget_power_check(path->sink))
1462 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1464 return w->connected;
1467 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1468 struct snd_soc_dapm_widget *b,
1473 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1474 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1479 sort = dapm_down_seq;
1481 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1482 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1484 if (sort[a->id] != sort[b->id])
1485 return sort[a->id] - sort[b->id];
1486 if (a->subseq != b->subseq) {
1488 return a->subseq - b->subseq;
1490 return b->subseq - a->subseq;
1492 if (a->reg != b->reg)
1493 return a->reg - b->reg;
1494 if (a->dapm != b->dapm)
1495 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1500 /* Insert a widget in order into a DAPM power sequence. */
1501 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1502 struct list_head *list,
1505 struct snd_soc_dapm_widget *w;
1507 list_for_each_entry(w, list, power_list)
1508 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1509 list_add_tail(&new_widget->power_list, &w->power_list);
1513 list_add_tail(&new_widget->power_list, list);
1516 static void dapm_seq_check_event(struct snd_soc_card *card,
1517 struct snd_soc_dapm_widget *w, int event)
1519 const char *ev_name;
1523 case SND_SOC_DAPM_PRE_PMU:
1524 ev_name = "PRE_PMU";
1527 case SND_SOC_DAPM_POST_PMU:
1528 ev_name = "POST_PMU";
1531 case SND_SOC_DAPM_PRE_PMD:
1532 ev_name = "PRE_PMD";
1535 case SND_SOC_DAPM_POST_PMD:
1536 ev_name = "POST_PMD";
1539 case SND_SOC_DAPM_WILL_PMU:
1540 ev_name = "WILL_PMU";
1543 case SND_SOC_DAPM_WILL_PMD:
1544 ev_name = "WILL_PMD";
1548 WARN(1, "Unknown event %d\n", event);
1552 if (w->new_power != power)
1555 if (w->event && (w->event_flags & event)) {
1556 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1558 soc_dapm_async_complete(w->dapm);
1559 trace_snd_soc_dapm_widget_event_start(w, event);
1560 ret = w->event(w, NULL, event);
1561 trace_snd_soc_dapm_widget_event_done(w, event);
1563 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1564 ev_name, w->name, ret);
1568 /* Apply the coalesced changes from a DAPM sequence */
1569 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1570 struct list_head *pending)
1572 struct snd_soc_dapm_context *dapm;
1573 struct snd_soc_dapm_widget *w;
1575 unsigned int value = 0;
1576 unsigned int mask = 0;
1578 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1582 list_for_each_entry(w, pending, power_list) {
1583 WARN_ON(reg != w->reg || dapm != w->dapm);
1584 w->power = w->new_power;
1586 mask |= w->mask << w->shift;
1588 value |= w->on_val << w->shift;
1590 value |= w->off_val << w->shift;
1592 pop_dbg(dapm->dev, card->pop_time,
1593 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1594 w->name, reg, value, mask);
1596 /* Check for events */
1597 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1598 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1602 /* Any widget will do, they should all be updating the
1606 pop_dbg(dapm->dev, card->pop_time,
1607 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1608 value, mask, reg, card->pop_time);
1609 pop_wait(card->pop_time);
1610 soc_dapm_update_bits(dapm, reg, mask, value);
1613 list_for_each_entry(w, pending, power_list) {
1614 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1615 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1619 /* Apply a DAPM power sequence.
1621 * We walk over a pre-sorted list of widgets to apply power to. In
1622 * order to minimise the number of writes to the device required
1623 * multiple widgets will be updated in a single write where possible.
1624 * Currently anything that requires more than a single write is not
1627 static void dapm_seq_run(struct snd_soc_card *card,
1628 struct list_head *list, int event, bool power_up)
1630 struct snd_soc_dapm_widget *w, *n;
1631 struct snd_soc_dapm_context *d;
1634 int cur_subseq = -1;
1635 int cur_reg = SND_SOC_NOPM;
1636 struct snd_soc_dapm_context *cur_dapm = NULL;
1643 sort = dapm_down_seq;
1645 list_for_each_entry_safe(w, n, list, power_list) {
1648 /* Do we need to apply any queued changes? */
1649 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1650 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1651 if (!list_empty(&pending))
1652 dapm_seq_run_coalesced(card, &pending);
1654 if (cur_dapm && cur_dapm->component) {
1655 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1656 if (sort[i] == cur_sort)
1657 snd_soc_component_seq_notifier(
1658 cur_dapm->component,
1662 if (cur_dapm && w->dapm != cur_dapm)
1663 soc_dapm_async_complete(cur_dapm);
1665 INIT_LIST_HEAD(&pending);
1667 cur_subseq = INT_MIN;
1668 cur_reg = SND_SOC_NOPM;
1673 case snd_soc_dapm_pre:
1675 list_for_each_entry_safe_continue(w, n, list,
1678 if (event == SND_SOC_DAPM_STREAM_START)
1680 NULL, SND_SOC_DAPM_PRE_PMU);
1681 else if (event == SND_SOC_DAPM_STREAM_STOP)
1683 NULL, SND_SOC_DAPM_PRE_PMD);
1686 case snd_soc_dapm_post:
1688 list_for_each_entry_safe_continue(w, n, list,
1691 if (event == SND_SOC_DAPM_STREAM_START)
1693 NULL, SND_SOC_DAPM_POST_PMU);
1694 else if (event == SND_SOC_DAPM_STREAM_STOP)
1696 NULL, SND_SOC_DAPM_POST_PMD);
1700 /* Queue it up for application */
1701 cur_sort = sort[w->id];
1702 cur_subseq = w->subseq;
1705 list_move(&w->power_list, &pending);
1710 dev_err(w->dapm->dev,
1711 "ASoC: Failed to apply widget power: %d\n", ret);
1714 if (!list_empty(&pending))
1715 dapm_seq_run_coalesced(card, &pending);
1717 if (cur_dapm && cur_dapm->component) {
1718 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1719 if (sort[i] == cur_sort)
1720 snd_soc_component_seq_notifier(
1721 cur_dapm->component,
1725 for_each_card_dapms(card, d)
1726 soc_dapm_async_complete(d);
1729 static void dapm_widget_update(struct snd_soc_card *card)
1731 struct snd_soc_dapm_update *update = card->update;
1732 struct snd_soc_dapm_widget_list *wlist;
1733 struct snd_soc_dapm_widget *w = NULL;
1737 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1740 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1742 for_each_dapm_widgets(wlist, wi, w) {
1743 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1744 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1746 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1754 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1757 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1760 if (update->has_second_set) {
1761 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1762 update->mask2, update->val2);
1764 dev_err(w->dapm->dev,
1765 "ASoC: %s DAPM update failed: %d\n",
1769 for_each_dapm_widgets(wlist, wi, w) {
1770 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1771 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1773 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1779 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1780 * they're changing state.
1782 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1784 struct snd_soc_dapm_context *d = data;
1787 /* If we're off and we're not supposed to go into STANDBY */
1788 if (d->bias_level == SND_SOC_BIAS_OFF &&
1789 d->target_bias_level != SND_SOC_BIAS_OFF) {
1791 pm_runtime_get_sync(d->dev);
1793 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1796 "ASoC: Failed to turn on bias: %d\n", ret);
1799 /* Prepare for a transition to ON or away from ON */
1800 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1801 d->bias_level != SND_SOC_BIAS_ON) ||
1802 (d->target_bias_level != SND_SOC_BIAS_ON &&
1803 d->bias_level == SND_SOC_BIAS_ON)) {
1804 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1807 "ASoC: Failed to prepare bias: %d\n", ret);
1811 /* Async callback run prior to DAPM sequences - brings to their final
1814 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1816 struct snd_soc_dapm_context *d = data;
1819 /* If we just powered the last thing off drop to standby bias */
1820 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1821 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1822 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1823 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1825 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1829 /* If we're in standby and can support bias off then do that */
1830 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1831 d->target_bias_level == SND_SOC_BIAS_OFF) {
1832 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1834 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1838 pm_runtime_put(d->dev);
1841 /* If we just powered up then move to active bias */
1842 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1843 d->target_bias_level == SND_SOC_BIAS_ON) {
1844 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1846 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1851 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1852 bool power, bool connect)
1854 /* If a connection is being made or broken then that update
1855 * will have marked the peer dirty, otherwise the widgets are
1856 * not connected and this update has no impact. */
1860 /* If the peer is already in the state we're moving to then we
1861 * won't have an impact on it. */
1862 if (power != peer->power)
1863 dapm_mark_dirty(peer, "peer state change");
1866 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1867 struct list_head *up_list,
1868 struct list_head *down_list)
1870 struct snd_soc_dapm_path *path;
1872 if (w->power == power)
1875 trace_snd_soc_dapm_widget_power(w, power);
1877 /* If we changed our power state perhaps our neigbours changed
1880 snd_soc_dapm_widget_for_each_source_path(w, path)
1881 dapm_widget_set_peer_power(path->source, power, path->connect);
1883 /* Supplies can't affect their outputs, only their inputs */
1884 if (!w->is_supply) {
1885 snd_soc_dapm_widget_for_each_sink_path(w, path)
1886 dapm_widget_set_peer_power(path->sink, power,
1891 dapm_seq_insert(w, up_list, true);
1893 dapm_seq_insert(w, down_list, false);
1896 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1897 struct list_head *up_list,
1898 struct list_head *down_list)
1903 case snd_soc_dapm_pre:
1904 dapm_seq_insert(w, down_list, false);
1906 case snd_soc_dapm_post:
1907 dapm_seq_insert(w, up_list, true);
1911 power = dapm_widget_power_check(w);
1913 dapm_widget_set_power(w, power, up_list, down_list);
1918 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1920 if (dapm->idle_bias_off)
1923 switch (snd_power_get_state(dapm->card->snd_card)) {
1924 case SNDRV_CTL_POWER_D3hot:
1925 case SNDRV_CTL_POWER_D3cold:
1926 return dapm->suspend_bias_off;
1935 * Scan each dapm widget for complete audio path.
1936 * A complete path is a route that has valid endpoints i.e.:-
1938 * o DAC to output pin.
1939 * o Input pin to ADC.
1940 * o Input pin to Output pin (bypass, sidetone)
1941 * o DAC to ADC (loopback).
1943 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1945 struct snd_soc_dapm_widget *w;
1946 struct snd_soc_dapm_context *d;
1948 LIST_HEAD(down_list);
1949 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1950 enum snd_soc_bias_level bias;
1953 lockdep_assert_held(&card->dapm_mutex);
1955 trace_snd_soc_dapm_start(card);
1957 for_each_card_dapms(card, d) {
1958 if (dapm_idle_bias_off(d))
1959 d->target_bias_level = SND_SOC_BIAS_OFF;
1961 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1966 /* Check which widgets we need to power and store them in
1967 * lists indicating if they should be powered up or down. We
1968 * only check widgets that have been flagged as dirty but note
1969 * that new widgets may be added to the dirty list while we
1972 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1973 dapm_power_one_widget(w, &up_list, &down_list);
1976 for_each_card_widgets(card, w) {
1978 case snd_soc_dapm_pre:
1979 case snd_soc_dapm_post:
1980 /* These widgets always need to be powered */
1983 list_del_init(&w->dirty);
1990 /* Supplies and micbiases only bring the
1991 * context up to STANDBY as unless something
1992 * else is active and passing audio they
1993 * generally don't require full power. Signal
1994 * generators are virtual pins and have no
1995 * power impact themselves.
1998 case snd_soc_dapm_siggen:
1999 case snd_soc_dapm_vmid:
2001 case snd_soc_dapm_supply:
2002 case snd_soc_dapm_regulator_supply:
2003 case snd_soc_dapm_pinctrl:
2004 case snd_soc_dapm_clock_supply:
2005 case snd_soc_dapm_micbias:
2006 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2007 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2010 d->target_bias_level = SND_SOC_BIAS_ON;
2017 /* Force all contexts in the card to the same bias state if
2018 * they're not ground referenced.
2020 bias = SND_SOC_BIAS_OFF;
2021 for_each_card_dapms(card, d)
2022 if (d->target_bias_level > bias)
2023 bias = d->target_bias_level;
2024 for_each_card_dapms(card, d)
2025 if (!dapm_idle_bias_off(d))
2026 d->target_bias_level = bias;
2028 trace_snd_soc_dapm_walk_done(card);
2030 /* Run card bias changes at first */
2031 dapm_pre_sequence_async(&card->dapm, 0);
2032 /* Run other bias changes in parallel */
2033 for_each_card_dapms(card, d) {
2034 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2035 async_schedule_domain(dapm_pre_sequence_async, d,
2038 async_synchronize_full_domain(&async_domain);
2040 list_for_each_entry(w, &down_list, power_list) {
2041 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2044 list_for_each_entry(w, &up_list, power_list) {
2045 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2048 /* Power down widgets first; try to avoid amplifying pops. */
2049 dapm_seq_run(card, &down_list, event, false);
2051 dapm_widget_update(card);
2054 dapm_seq_run(card, &up_list, event, true);
2056 /* Run all the bias changes in parallel */
2057 for_each_card_dapms(card, d) {
2058 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2059 async_schedule_domain(dapm_post_sequence_async, d,
2062 async_synchronize_full_domain(&async_domain);
2063 /* Run card bias changes at last */
2064 dapm_post_sequence_async(&card->dapm, 0);
2066 /* do we need to notify any clients that DAPM event is complete */
2067 for_each_card_dapms(card, d) {
2071 ret = snd_soc_component_stream_event(d->component, event);
2076 pop_dbg(card->dev, card->pop_time,
2077 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2078 pop_wait(card->pop_time);
2080 trace_snd_soc_dapm_done(card);
2085 #ifdef CONFIG_DEBUG_FS
2086 static ssize_t dapm_widget_power_read_file(struct file *file,
2087 char __user *user_buf,
2088 size_t count, loff_t *ppos)
2090 struct snd_soc_dapm_widget *w = file->private_data;
2091 struct snd_soc_card *card = w->dapm->card;
2092 enum snd_soc_dapm_direction dir, rdir;
2096 struct snd_soc_dapm_path *p = NULL;
2098 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2102 mutex_lock(&card->dapm_mutex);
2104 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2109 in = is_connected_input_ep(w, NULL, NULL);
2110 out = is_connected_output_ep(w, NULL, NULL);
2113 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2114 w->name, w->power ? "On" : "Off",
2115 w->force ? " (forced)" : "", in, out);
2118 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2119 " - R%d(0x%x) mask 0x%x",
2120 w->reg, w->reg, w->mask << w->shift);
2122 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2125 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2127 w->active ? "active" : "inactive");
2129 snd_soc_dapm_for_each_direction(dir) {
2130 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2131 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2132 if (p->connected && !p->connected(p->source, p->sink))
2138 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2139 " %s \"%s\" \"%s\"\n",
2140 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2141 p->name ? p->name : "static",
2142 p->node[rdir]->name);
2146 mutex_unlock(&card->dapm_mutex);
2148 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2154 static const struct file_operations dapm_widget_power_fops = {
2155 .open = simple_open,
2156 .read = dapm_widget_power_read_file,
2157 .llseek = default_llseek,
2160 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2161 size_t count, loff_t *ppos)
2163 struct snd_soc_dapm_context *dapm = file->private_data;
2166 switch (dapm->bias_level) {
2167 case SND_SOC_BIAS_ON:
2170 case SND_SOC_BIAS_PREPARE:
2171 level = "Prepare\n";
2173 case SND_SOC_BIAS_STANDBY:
2174 level = "Standby\n";
2176 case SND_SOC_BIAS_OFF:
2180 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2181 level = "Unknown\n";
2185 return simple_read_from_buffer(user_buf, count, ppos, level,
2189 static const struct file_operations dapm_bias_fops = {
2190 .open = simple_open,
2191 .read = dapm_bias_read_file,
2192 .llseek = default_llseek,
2195 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2196 struct dentry *parent)
2198 if (!parent || IS_ERR(parent))
2201 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2203 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2207 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2209 struct snd_soc_dapm_context *dapm = w->dapm;
2211 if (!dapm->debugfs_dapm || !w->name)
2214 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2215 &dapm_widget_power_fops);
2218 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2220 debugfs_remove_recursive(dapm->debugfs_dapm);
2221 dapm->debugfs_dapm = NULL;
2225 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2226 struct dentry *parent)
2230 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2234 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2241 * soc_dapm_connect_path() - Connects or disconnects a path
2242 * @path: The path to update
2243 * @connect: The new connect state of the path. True if the path is connected,
2244 * false if it is disconnected.
2245 * @reason: The reason why the path changed (for debugging only)
2247 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2248 bool connect, const char *reason)
2250 if (path->connect == connect)
2253 path->connect = connect;
2254 dapm_mark_dirty(path->source, reason);
2255 dapm_mark_dirty(path->sink, reason);
2256 dapm_path_invalidate(path);
2259 /* test and update the power status of a mux widget */
2260 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2261 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2263 struct snd_soc_dapm_path *path;
2267 lockdep_assert_held(&card->dapm_mutex);
2269 /* find dapm widget path assoc with kcontrol */
2270 dapm_kcontrol_for_each_path(path, kcontrol) {
2272 /* we now need to match the string in the enum to the path */
2273 if (e && !(strcmp(path->name, e->texts[mux])))
2278 soc_dapm_connect_path(path, connect, "mux update");
2282 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2287 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2288 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2289 struct snd_soc_dapm_update *update)
2291 struct snd_soc_card *card = dapm->card;
2294 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2295 card->update = update;
2296 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2297 card->update = NULL;
2298 mutex_unlock(&card->dapm_mutex);
2300 snd_soc_dpcm_runtime_update(card);
2303 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2305 /* test and update the power status of a mixer or switch widget */
2306 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2307 struct snd_kcontrol *kcontrol,
2308 int connect, int rconnect)
2310 struct snd_soc_dapm_path *path;
2313 lockdep_assert_held(&card->dapm_mutex);
2315 /* find dapm widget path assoc with kcontrol */
2316 dapm_kcontrol_for_each_path(path, kcontrol) {
2318 * Ideally this function should support any number of
2319 * paths and channels. But since kcontrols only come
2320 * in mono and stereo variants, we are limited to 2
2323 * The following code assumes for stereo controls the
2324 * first path (when 'found == 0') is the left channel,
2325 * and all remaining paths (when 'found == 1') are the
2328 * A stereo control is signified by a valid 'rconnect'
2329 * value, either 0 for unconnected, or >= 0 for connected.
2330 * This is chosen instead of using snd_soc_volsw_is_stereo,
2331 * so that the behavior of snd_soc_dapm_mixer_update_power
2332 * doesn't change even when the kcontrol passed in is
2335 * It passes 'connect' as the path connect status for
2336 * the left channel, and 'rconnect' for the right
2339 if (found && rconnect >= 0)
2340 soc_dapm_connect_path(path, rconnect, "mixer update");
2342 soc_dapm_connect_path(path, connect, "mixer update");
2347 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2352 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2353 struct snd_kcontrol *kcontrol, int connect,
2354 struct snd_soc_dapm_update *update)
2356 struct snd_soc_card *card = dapm->card;
2359 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2360 card->update = update;
2361 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2362 card->update = NULL;
2363 mutex_unlock(&card->dapm_mutex);
2365 snd_soc_dpcm_runtime_update(card);
2368 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2370 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2373 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2374 struct snd_soc_dapm_widget *w;
2376 char *state = "not set";
2378 /* card won't be set for the dummy component, as a spot fix
2379 * we're checking for that case specifically here but in future
2380 * we will ensure that the dummy component looks like others.
2385 for_each_card_widgets(cmpnt->card, w) {
2386 if (w->dapm != dapm)
2389 /* only display widgets that burn power */
2391 case snd_soc_dapm_hp:
2392 case snd_soc_dapm_mic:
2393 case snd_soc_dapm_spk:
2394 case snd_soc_dapm_line:
2395 case snd_soc_dapm_micbias:
2396 case snd_soc_dapm_dac:
2397 case snd_soc_dapm_adc:
2398 case snd_soc_dapm_pga:
2399 case snd_soc_dapm_effect:
2400 case snd_soc_dapm_out_drv:
2401 case snd_soc_dapm_mixer:
2402 case snd_soc_dapm_mixer_named_ctl:
2403 case snd_soc_dapm_supply:
2404 case snd_soc_dapm_regulator_supply:
2405 case snd_soc_dapm_pinctrl:
2406 case snd_soc_dapm_clock_supply:
2408 count += sprintf(buf + count, "%s: %s\n",
2409 w->name, w->power ? "On":"Off");
2416 switch (snd_soc_dapm_get_bias_level(dapm)) {
2417 case SND_SOC_BIAS_ON:
2420 case SND_SOC_BIAS_PREPARE:
2423 case SND_SOC_BIAS_STANDBY:
2426 case SND_SOC_BIAS_OFF:
2430 count += sprintf(buf + count, "PM State: %s\n", state);
2435 /* show dapm widget status in sys fs */
2436 static ssize_t dapm_widget_show(struct device *dev,
2437 struct device_attribute *attr, char *buf)
2439 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2440 struct snd_soc_dai *codec_dai;
2443 mutex_lock(&rtd->card->dapm_mutex);
2445 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2446 struct snd_soc_component *cmpnt = codec_dai->component;
2448 count += dapm_widget_show_component(cmpnt, buf + count);
2451 mutex_unlock(&rtd->card->dapm_mutex);
2456 static DEVICE_ATTR_RO(dapm_widget);
2458 struct attribute *soc_dapm_dev_attrs[] = {
2459 &dev_attr_dapm_widget.attr,
2463 static void dapm_free_path(struct snd_soc_dapm_path *path)
2465 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2466 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2467 list_del(&path->list_kcontrol);
2468 list_del(&path->list);
2472 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2474 struct snd_soc_dapm_path *p, *next_p;
2475 enum snd_soc_dapm_direction dir;
2479 * remove source and sink paths associated to this widget.
2480 * While removing the path, remove reference to it from both
2481 * source and sink widgets so that path is removed only once.
2483 snd_soc_dapm_for_each_direction(dir) {
2484 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2488 kfree(w->kcontrols);
2489 kfree_const(w->name);
2490 kfree_const(w->sname);
2494 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2496 dapm->path_sink_cache.widget = NULL;
2497 dapm->path_source_cache.widget = NULL;
2500 /* free all dapm widgets and resources */
2501 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2503 struct snd_soc_dapm_widget *w, *next_w;
2505 for_each_card_widgets_safe(dapm->card, w, next_w) {
2506 if (w->dapm != dapm)
2508 snd_soc_dapm_free_widget(w);
2510 snd_soc_dapm_reset_cache(dapm);
2513 static struct snd_soc_dapm_widget *dapm_find_widget(
2514 struct snd_soc_dapm_context *dapm, const char *pin,
2515 bool search_other_contexts)
2517 struct snd_soc_dapm_widget *w;
2518 struct snd_soc_dapm_widget *fallback = NULL;
2520 for_each_card_widgets(dapm->card, w) {
2521 if (!strcmp(w->name, pin)) {
2522 if (w->dapm == dapm)
2529 if (search_other_contexts)
2535 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2536 const char *pin, int status)
2538 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2540 dapm_assert_locked(dapm);
2543 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2547 if (w->connected != status) {
2548 dapm_mark_dirty(w, "pin configuration");
2549 dapm_widget_invalidate_input_paths(w);
2550 dapm_widget_invalidate_output_paths(w);
2553 w->connected = status;
2561 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2562 * @dapm: DAPM context
2564 * Walks all dapm audio paths and powers widgets according to their
2565 * stream or path usage.
2567 * Requires external locking.
2569 * Returns 0 for success.
2571 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2574 * Suppress early reports (eg, jacks syncing their state) to avoid
2575 * silly DAPM runs during card startup.
2577 if (!dapm->card || !dapm->card->instantiated)
2580 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2582 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2585 * snd_soc_dapm_sync - scan and power dapm paths
2586 * @dapm: DAPM context
2588 * Walks all dapm audio paths and powers widgets according to their
2589 * stream or path usage.
2591 * Returns 0 for success.
2593 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2597 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2598 ret = snd_soc_dapm_sync_unlocked(dapm);
2599 mutex_unlock(&dapm->card->dapm_mutex);
2602 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2604 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2605 struct snd_soc_dapm_widget *w,
2609 case snd_soc_dapm_aif_out:
2610 case snd_soc_dapm_aif_in:
2616 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2617 w->channel < channels ? "Connecting" : "Disconnecting",
2618 p->source->name, p->sink->name);
2620 if (w->channel < channels)
2621 soc_dapm_connect_path(p, true, "dai update");
2623 soc_dapm_connect_path(p, false, "dai update");
2628 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2629 struct snd_pcm_hw_params *params,
2630 struct snd_soc_dai *dai)
2632 int dir = substream->stream;
2633 int channels = params_channels(params);
2634 struct snd_soc_dapm_path *p;
2635 struct snd_soc_dapm_widget *w;
2638 w = snd_soc_dai_get_widget(dai, dir);
2643 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2644 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2646 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2647 ret = dapm_update_dai_chan(p, p->sink, channels);
2652 snd_soc_dapm_widget_for_each_source_path(w, p) {
2653 ret = dapm_update_dai_chan(p, p->source, channels);
2661 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2662 struct snd_pcm_hw_params *params,
2663 struct snd_soc_dai *dai)
2665 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2668 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2669 ret = dapm_update_dai_unlocked(substream, params, dai);
2670 mutex_unlock(&rtd->card->dapm_mutex);
2674 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2677 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2678 * @w: The widget for which to update the flags
2680 * Some widgets have a dynamic category which depends on which neighbors they
2681 * are connected to. This function update the category for these widgets.
2683 * This function must be called whenever a path is added or removed to a widget.
2685 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2687 enum snd_soc_dapm_direction dir;
2688 struct snd_soc_dapm_path *p;
2692 case snd_soc_dapm_input:
2693 /* On a fully routed card an input is never a source */
2694 if (w->dapm->card->fully_routed)
2696 ep = SND_SOC_DAPM_EP_SOURCE;
2697 snd_soc_dapm_widget_for_each_source_path(w, p) {
2698 if (p->source->id == snd_soc_dapm_micbias ||
2699 p->source->id == snd_soc_dapm_mic ||
2700 p->source->id == snd_soc_dapm_line ||
2701 p->source->id == snd_soc_dapm_output) {
2707 case snd_soc_dapm_output:
2708 /* On a fully routed card a output is never a sink */
2709 if (w->dapm->card->fully_routed)
2711 ep = SND_SOC_DAPM_EP_SINK;
2712 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2713 if (p->sink->id == snd_soc_dapm_spk ||
2714 p->sink->id == snd_soc_dapm_hp ||
2715 p->sink->id == snd_soc_dapm_line ||
2716 p->sink->id == snd_soc_dapm_input) {
2722 case snd_soc_dapm_line:
2724 snd_soc_dapm_for_each_direction(dir) {
2725 if (!list_empty(&w->edges[dir]))
2726 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2736 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2737 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2738 const char *control)
2740 bool dynamic_source = false;
2741 bool dynamic_sink = false;
2746 switch (source->id) {
2747 case snd_soc_dapm_demux:
2748 dynamic_source = true;
2755 case snd_soc_dapm_mux:
2756 case snd_soc_dapm_switch:
2757 case snd_soc_dapm_mixer:
2758 case snd_soc_dapm_mixer_named_ctl:
2759 dynamic_sink = true;
2765 if (dynamic_source && dynamic_sink) {
2767 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2768 source->name, control, sink->name);
2770 } else if (!dynamic_source && !dynamic_sink) {
2772 "Control not supported for path %s -> [%s] -> %s\n",
2773 source->name, control, sink->name);
2780 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2781 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2782 const char *control,
2783 int (*connected)(struct snd_soc_dapm_widget *source,
2784 struct snd_soc_dapm_widget *sink))
2786 struct snd_soc_dapm_widget *widgets[2];
2787 enum snd_soc_dapm_direction dir;
2788 struct snd_soc_dapm_path *path;
2791 if (wsink->is_supply && !wsource->is_supply) {
2793 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2794 wsource->name, wsink->name);
2798 if (connected && !wsource->is_supply) {
2800 "connected() callback only supported for supply widgets (%s -> %s)\n",
2801 wsource->name, wsink->name);
2805 if (wsource->is_supply && control) {
2807 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2808 wsource->name, control, wsink->name);
2812 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2816 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2820 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2821 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2822 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2823 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2825 path->connected = connected;
2826 INIT_LIST_HEAD(&path->list);
2827 INIT_LIST_HEAD(&path->list_kcontrol);
2829 if (wsource->is_supply || wsink->is_supply)
2830 path->is_supply = 1;
2832 /* connect static paths */
2833 if (control == NULL) {
2836 switch (wsource->id) {
2837 case snd_soc_dapm_demux:
2838 ret = dapm_connect_mux(dapm, path, control, wsource);
2846 switch (wsink->id) {
2847 case snd_soc_dapm_mux:
2848 ret = dapm_connect_mux(dapm, path, control, wsink);
2852 case snd_soc_dapm_switch:
2853 case snd_soc_dapm_mixer:
2854 case snd_soc_dapm_mixer_named_ctl:
2855 ret = dapm_connect_mixer(dapm, path, control);
2864 list_add(&path->list, &dapm->card->paths);
2865 snd_soc_dapm_for_each_direction(dir)
2866 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2868 snd_soc_dapm_for_each_direction(dir) {
2869 dapm_update_widget_flags(widgets[dir]);
2870 dapm_mark_dirty(widgets[dir], "Route added");
2873 if (dapm->card->instantiated && path->connect)
2874 dapm_path_invalidate(path);
2882 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2883 const struct snd_soc_dapm_route *route)
2885 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2886 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2889 char prefixed_sink[80];
2890 char prefixed_source[80];
2892 unsigned int sink_ref = 0;
2893 unsigned int source_ref = 0;
2896 prefix = soc_dapm_prefix(dapm);
2898 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2899 prefix, route->sink);
2900 sink = prefixed_sink;
2901 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2902 prefix, route->source);
2903 source = prefixed_source;
2906 source = route->source;
2909 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2910 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2912 if (wsink && wsource)
2916 * find src and dest widgets over all widgets but favor a widget from
2917 * current DAPM context
2919 for_each_card_widgets(dapm->card, w) {
2920 if (!wsink && !(strcmp(w->name, sink))) {
2922 if (w->dapm == dapm) {
2930 "ASoC: sink widget %s overwritten\n",
2934 if (!wsource && !(strcmp(w->name, source))) {
2936 if (w->dapm == dapm) {
2944 "ASoC: source widget %s overwritten\n",
2948 /* use widget from another DAPM context if not found from this */
2954 if (wsource == NULL) {
2955 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2959 if (wsink == NULL) {
2960 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2966 dapm_wcache_update(&dapm->path_sink_cache, wsink);
2967 dapm_wcache_update(&dapm->path_source_cache, wsource);
2969 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
2976 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2977 source, route->control, sink);
2981 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2982 const struct snd_soc_dapm_route *route)
2984 struct snd_soc_dapm_widget *wsource, *wsink;
2985 struct snd_soc_dapm_path *path, *p;
2988 char prefixed_sink[80];
2989 char prefixed_source[80];
2992 if (route->control) {
2994 "ASoC: Removal of routes with controls not supported\n");
2998 prefix = soc_dapm_prefix(dapm);
3000 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3001 prefix, route->sink);
3002 sink = prefixed_sink;
3003 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3004 prefix, route->source);
3005 source = prefixed_source;
3008 source = route->source;
3012 list_for_each_entry(p, &dapm->card->paths, list) {
3013 if (strcmp(p->source->name, source) != 0)
3015 if (strcmp(p->sink->name, sink) != 0)
3022 wsource = path->source;
3025 dapm_mark_dirty(wsource, "Route removed");
3026 dapm_mark_dirty(wsink, "Route removed");
3028 dapm_path_invalidate(path);
3030 dapm_free_path(path);
3032 /* Update any path related flags */
3033 dapm_update_widget_flags(wsource);
3034 dapm_update_widget_flags(wsink);
3036 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3044 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3045 * @dapm: DAPM context
3046 * @route: audio routes
3047 * @num: number of routes
3049 * Connects 2 dapm widgets together via a named audio path. The sink is
3050 * the widget receiving the audio signal, whilst the source is the sender
3051 * of the audio signal.
3053 * Returns 0 for success else error. On error all resources can be freed
3054 * with a call to snd_soc_card_free().
3056 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3057 const struct snd_soc_dapm_route *route, int num)
3061 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3062 for (i = 0; i < num; i++) {
3063 r = snd_soc_dapm_add_route(dapm, route);
3065 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3067 route->control ? route->control : "direct",
3073 mutex_unlock(&dapm->card->dapm_mutex);
3077 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3080 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3081 * @dapm: DAPM context
3082 * @route: audio routes
3083 * @num: number of routes
3085 * Removes routes from the DAPM context.
3087 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3088 const struct snd_soc_dapm_route *route, int num)
3092 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3093 for (i = 0; i < num; i++) {
3094 snd_soc_dapm_del_route(dapm, route);
3097 mutex_unlock(&dapm->card->dapm_mutex);
3101 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3103 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3104 const struct snd_soc_dapm_route *route)
3106 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3109 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3112 struct snd_soc_dapm_path *path;
3116 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3122 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3127 if (route->control || route->connected)
3128 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3129 route->source, route->sink);
3131 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3132 if (path->sink == sink) {
3139 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3140 route->source, route->sink);
3142 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3143 count, route->source, route->sink);
3149 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3150 * @dapm: DAPM context
3151 * @route: audio routes
3152 * @num: number of routes
3154 * Mark existing routes matching those specified in the passed array
3155 * as being weak, meaning that they are ignored for the purpose of
3156 * power decisions. The main intended use case is for sidetone paths
3157 * which couple audio between other independent paths if they are both
3158 * active in order to make the combination work better at the user
3159 * level but which aren't intended to be "used".
3161 * Note that CODEC drivers should not use this as sidetone type paths
3162 * can frequently also be used as bypass paths.
3164 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3165 const struct snd_soc_dapm_route *route, int num)
3170 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3171 for (i = 0; i < num; i++) {
3172 err = snd_soc_dapm_weak_route(dapm, route);
3177 mutex_unlock(&dapm->card->dapm_mutex);
3181 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3184 * snd_soc_dapm_new_widgets - add new dapm widgets
3185 * @card: card to be checked for new dapm widgets
3187 * Checks the codec for any new dapm widgets and creates them if found.
3189 * Returns 0 for success.
3191 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3193 struct snd_soc_dapm_widget *w;
3196 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3198 for_each_card_widgets(card, w)
3203 if (w->num_kcontrols) {
3204 w->kcontrols = kcalloc(w->num_kcontrols,
3205 sizeof(struct snd_kcontrol *),
3207 if (!w->kcontrols) {
3208 mutex_unlock(&card->dapm_mutex);
3214 case snd_soc_dapm_switch:
3215 case snd_soc_dapm_mixer:
3216 case snd_soc_dapm_mixer_named_ctl:
3219 case snd_soc_dapm_mux:
3220 case snd_soc_dapm_demux:
3223 case snd_soc_dapm_pga:
3224 case snd_soc_dapm_effect:
3225 case snd_soc_dapm_out_drv:
3228 case snd_soc_dapm_dai_link:
3229 dapm_new_dai_link(w);
3235 /* Read the initial power state from the device */
3237 soc_dapm_read(w->dapm, w->reg, &val);
3238 val = val >> w->shift;
3240 if (val == w->on_val)
3246 dapm_mark_dirty(w, "new widget");
3247 dapm_debugfs_add_widget(w);
3250 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3251 mutex_unlock(&card->dapm_mutex);
3254 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3257 * snd_soc_dapm_get_volsw - dapm mixer get callback
3258 * @kcontrol: mixer control
3259 * @ucontrol: control element information
3261 * Callback to get the value of a dapm mixer control.
3263 * Returns 0 for success.
3265 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3266 struct snd_ctl_elem_value *ucontrol)
3268 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3269 struct snd_soc_card *card = dapm->card;
3270 struct soc_mixer_control *mc =
3271 (struct soc_mixer_control *)kcontrol->private_value;
3273 unsigned int shift = mc->shift;
3275 unsigned int width = fls(max);
3276 unsigned int mask = (1 << fls(max)) - 1;
3277 unsigned int invert = mc->invert;
3278 unsigned int reg_val, val, rval = 0;
3281 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3282 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3283 ret = soc_dapm_read(dapm, reg, ®_val);
3284 val = (reg_val >> shift) & mask;
3286 if (ret == 0 && reg != mc->rreg)
3287 ret = soc_dapm_read(dapm, mc->rreg, ®_val);
3289 if (snd_soc_volsw_is_stereo(mc))
3290 rval = (reg_val >> mc->rshift) & mask;
3292 reg_val = dapm_kcontrol_get_value(kcontrol);
3293 val = reg_val & mask;
3295 if (snd_soc_volsw_is_stereo(mc))
3296 rval = (reg_val >> width) & mask;
3298 mutex_unlock(&card->dapm_mutex);
3304 ucontrol->value.integer.value[0] = max - val;
3306 ucontrol->value.integer.value[0] = val;
3308 if (snd_soc_volsw_is_stereo(mc)) {
3310 ucontrol->value.integer.value[1] = max - rval;
3312 ucontrol->value.integer.value[1] = rval;
3317 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3320 * snd_soc_dapm_put_volsw - dapm mixer set callback
3321 * @kcontrol: mixer control
3322 * @ucontrol: control element information
3324 * Callback to set the value of a dapm mixer control.
3326 * Returns 0 for success.
3328 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
3331 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3332 struct snd_soc_card *card = dapm->card;
3333 struct soc_mixer_control *mc =
3334 (struct soc_mixer_control *)kcontrol->private_value;
3336 unsigned int shift = mc->shift;
3338 unsigned int width = fls(max);
3339 unsigned int mask = (1 << width) - 1;
3340 unsigned int invert = mc->invert;
3341 unsigned int val, rval = 0;
3342 int connect, rconnect = -1, change, reg_change = 0;
3343 struct snd_soc_dapm_update update = {};
3346 val = (ucontrol->value.integer.value[0] & mask);
3352 if (snd_soc_volsw_is_stereo(mc)) {
3353 rval = (ucontrol->value.integer.value[1] & mask);
3359 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3361 /* This assumes field width < (bits in unsigned int / 2) */
3362 if (width > sizeof(unsigned int) * 8 / 2)
3364 "ASoC: control %s field width limit exceeded\n",
3366 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3368 if (reg != SND_SOC_NOPM) {
3370 rval = rval << mc->rshift;
3372 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3374 if (snd_soc_volsw_is_stereo(mc))
3375 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3380 if (change || reg_change) {
3382 if (snd_soc_volsw_is_stereo(mc)) {
3383 update.has_second_set = true;
3384 update.reg2 = mc->rreg;
3385 update.mask2 = mask << mc->rshift;
3388 update.kcontrol = kcontrol;
3390 update.mask = mask << shift;
3392 card->update = &update;
3394 change |= reg_change;
3396 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3399 card->update = NULL;
3402 mutex_unlock(&card->dapm_mutex);
3405 snd_soc_dpcm_runtime_update(card);
3409 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3412 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3413 * @kcontrol: mixer control
3414 * @ucontrol: control element information
3416 * Callback to get the value of a dapm enumerated double mixer control.
3418 * Returns 0 for success.
3420 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_value *ucontrol)
3423 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3424 struct snd_soc_card *card = dapm->card;
3425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3426 unsigned int reg_val, val;
3428 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3429 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3430 int ret = soc_dapm_read(dapm, e->reg, ®_val);
3432 mutex_unlock(&card->dapm_mutex);
3436 reg_val = dapm_kcontrol_get_value(kcontrol);
3438 mutex_unlock(&card->dapm_mutex);
3440 val = (reg_val >> e->shift_l) & e->mask;
3441 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3442 if (e->shift_l != e->shift_r) {
3443 val = (reg_val >> e->shift_r) & e->mask;
3444 val = snd_soc_enum_val_to_item(e, val);
3445 ucontrol->value.enumerated.item[1] = val;
3450 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3453 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3454 * @kcontrol: mixer control
3455 * @ucontrol: control element information
3457 * Callback to set the value of a dapm enumerated double mixer control.
3459 * Returns 0 for success.
3461 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol)
3464 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3465 struct snd_soc_card *card = dapm->card;
3466 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3467 unsigned int *item = ucontrol->value.enumerated.item;
3468 unsigned int val, change, reg_change = 0;
3470 struct snd_soc_dapm_update update = {};
3473 if (item[0] >= e->items)
3476 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3477 mask = e->mask << e->shift_l;
3478 if (e->shift_l != e->shift_r) {
3479 if (item[1] > e->items)
3481 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3482 mask |= e->mask << e->shift_r;
3485 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3487 change = dapm_kcontrol_set_value(kcontrol, val);
3489 if (e->reg != SND_SOC_NOPM)
3490 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3492 if (change || reg_change) {
3494 update.kcontrol = kcontrol;
3495 update.reg = e->reg;
3498 card->update = &update;
3500 change |= reg_change;
3502 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3504 card->update = NULL;
3507 mutex_unlock(&card->dapm_mutex);
3510 snd_soc_dpcm_runtime_update(card);
3514 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3517 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3519 * @kcontrol: mixer control
3520 * @uinfo: control element information
3522 * Callback to provide information about a pin switch control.
3524 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3525 struct snd_ctl_elem_info *uinfo)
3527 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3529 uinfo->value.integer.min = 0;
3530 uinfo->value.integer.max = 1;
3534 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3537 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3539 * @kcontrol: mixer control
3542 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3545 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3546 const char *pin = (const char *)kcontrol->private_value;
3548 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3550 ucontrol->value.integer.value[0] =
3551 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3553 mutex_unlock(&card->dapm_mutex);
3557 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3560 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3562 * @kcontrol: mixer control
3565 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3566 struct snd_ctl_elem_value *ucontrol)
3568 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3569 const char *pin = (const char *)kcontrol->private_value;
3571 if (ucontrol->value.integer.value[0])
3572 snd_soc_dapm_enable_pin(&card->dapm, pin);
3574 snd_soc_dapm_disable_pin(&card->dapm, pin);
3576 snd_soc_dapm_sync(&card->dapm);
3579 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3581 struct snd_soc_dapm_widget *
3582 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3583 const struct snd_soc_dapm_widget *widget)
3585 enum snd_soc_dapm_direction dir;
3586 struct snd_soc_dapm_widget *w;
3590 if ((w = dapm_cnew_widget(widget)) == NULL)
3591 return ERR_PTR(-ENOMEM);
3594 case snd_soc_dapm_regulator_supply:
3595 w->regulator = devm_regulator_get(dapm->dev, w->name);
3596 if (IS_ERR(w->regulator)) {
3597 ret = PTR_ERR(w->regulator);
3598 goto request_failed;
3601 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3602 ret = regulator_allow_bypass(w->regulator, true);
3605 "ASoC: Failed to bypass %s: %d\n",
3609 case snd_soc_dapm_pinctrl:
3610 w->pinctrl = devm_pinctrl_get(dapm->dev);
3611 if (IS_ERR(w->pinctrl)) {
3612 ret = PTR_ERR(w->pinctrl);
3613 goto request_failed;
3616 /* set to sleep_state when initializing */
3617 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3619 case snd_soc_dapm_clock_supply:
3620 w->clk = devm_clk_get(dapm->dev, w->name);
3621 if (IS_ERR(w->clk)) {
3622 ret = PTR_ERR(w->clk);
3623 goto request_failed;
3630 prefix = soc_dapm_prefix(dapm);
3632 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3634 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3635 if (w->name == NULL) {
3636 kfree_const(w->sname);
3638 return ERR_PTR(-ENOMEM);
3642 case snd_soc_dapm_mic:
3643 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3644 w->power_check = dapm_generic_check_power;
3646 case snd_soc_dapm_input:
3647 if (!dapm->card->fully_routed)
3648 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3649 w->power_check = dapm_generic_check_power;
3651 case snd_soc_dapm_spk:
3652 case snd_soc_dapm_hp:
3653 w->is_ep = SND_SOC_DAPM_EP_SINK;
3654 w->power_check = dapm_generic_check_power;
3656 case snd_soc_dapm_output:
3657 if (!dapm->card->fully_routed)
3658 w->is_ep = SND_SOC_DAPM_EP_SINK;
3659 w->power_check = dapm_generic_check_power;
3661 case snd_soc_dapm_vmid:
3662 case snd_soc_dapm_siggen:
3663 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3664 w->power_check = dapm_always_on_check_power;
3666 case snd_soc_dapm_sink:
3667 w->is_ep = SND_SOC_DAPM_EP_SINK;
3668 w->power_check = dapm_always_on_check_power;
3671 case snd_soc_dapm_mux:
3672 case snd_soc_dapm_demux:
3673 case snd_soc_dapm_switch:
3674 case snd_soc_dapm_mixer:
3675 case snd_soc_dapm_mixer_named_ctl:
3676 case snd_soc_dapm_adc:
3677 case snd_soc_dapm_aif_out:
3678 case snd_soc_dapm_dac:
3679 case snd_soc_dapm_aif_in:
3680 case snd_soc_dapm_pga:
3681 case snd_soc_dapm_buffer:
3682 case snd_soc_dapm_scheduler:
3683 case snd_soc_dapm_effect:
3684 case snd_soc_dapm_src:
3685 case snd_soc_dapm_asrc:
3686 case snd_soc_dapm_encoder:
3687 case snd_soc_dapm_decoder:
3688 case snd_soc_dapm_out_drv:
3689 case snd_soc_dapm_micbias:
3690 case snd_soc_dapm_line:
3691 case snd_soc_dapm_dai_link:
3692 case snd_soc_dapm_dai_out:
3693 case snd_soc_dapm_dai_in:
3694 w->power_check = dapm_generic_check_power;
3696 case snd_soc_dapm_supply:
3697 case snd_soc_dapm_regulator_supply:
3698 case snd_soc_dapm_pinctrl:
3699 case snd_soc_dapm_clock_supply:
3700 case snd_soc_dapm_kcontrol:
3702 w->power_check = dapm_supply_check_power;
3705 w->power_check = dapm_always_on_check_power;
3710 INIT_LIST_HEAD(&w->list);
3711 INIT_LIST_HEAD(&w->dirty);
3712 /* see for_each_card_widgets */
3713 list_add_tail(&w->list, &dapm->card->widgets);
3715 snd_soc_dapm_for_each_direction(dir) {
3716 INIT_LIST_HEAD(&w->edges[dir]);
3717 w->endpoints[dir] = -1;
3720 /* machine layer sets up unconnected pins and insertions */
3725 if (ret != -EPROBE_DEFER)
3726 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3729 kfree_const(w->sname);
3731 return ERR_PTR(ret);
3735 * snd_soc_dapm_new_control - create new dapm control
3736 * @dapm: DAPM context
3737 * @widget: widget template
3739 * Creates new DAPM control based upon a template.
3741 * Returns a widget pointer on success or an error pointer on failure
3743 struct snd_soc_dapm_widget *
3744 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3745 const struct snd_soc_dapm_widget *widget)
3747 struct snd_soc_dapm_widget *w;
3749 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3750 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3751 mutex_unlock(&dapm->card->dapm_mutex);
3755 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3758 * snd_soc_dapm_new_controls - create new dapm controls
3759 * @dapm: DAPM context
3760 * @widget: widget array
3761 * @num: number of widgets
3763 * Creates new DAPM controls based upon the templates.
3765 * Returns 0 for success else error.
3767 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3768 const struct snd_soc_dapm_widget *widget,
3771 struct snd_soc_dapm_widget *w;
3775 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3776 for (i = 0; i < num; i++) {
3777 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3784 mutex_unlock(&dapm->card->dapm_mutex);
3787 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3790 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3791 struct snd_pcm_substream *substream)
3793 struct snd_soc_dapm_path *path;
3794 struct snd_soc_dai *source, *sink;
3795 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3796 struct snd_pcm_hw_params *params = NULL;
3797 const struct snd_soc_pcm_stream *config = NULL;
3798 struct snd_pcm_runtime *runtime = NULL;
3802 params = kzalloc(sizeof(*params), GFP_KERNEL);
3806 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3812 substream->runtime = runtime;
3814 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3815 snd_soc_dapm_widget_for_each_source_path(w, path) {
3816 source = path->source->priv;
3818 ret = snd_soc_dai_startup(source, substream);
3820 dev_err(source->dev,
3821 "ASoC: startup() failed: %d\n", ret);
3827 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3828 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3829 sink = path->sink->priv;
3831 ret = snd_soc_dai_startup(sink, substream);
3834 "ASoC: startup() failed: %d\n", ret);
3840 substream->hw_opened = 1;
3843 * Note: getting the config after .startup() gives a chance to
3844 * either party on the link to alter the configuration if
3847 config = rtd->dai_link->params + rtd->params_select;
3848 if (WARN_ON(!config)) {
3849 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3854 /* Be a little careful as we don't want to overflow the mask array */
3855 if (config->formats) {
3856 fmt = ffs(config->formats) - 1;
3858 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3865 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3866 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3868 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3870 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3871 = config->channels_min;
3872 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3873 = config->channels_max;
3875 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3876 snd_soc_dapm_widget_for_each_source_path(w, path) {
3877 source = path->source->priv;
3879 ret = snd_soc_dai_hw_params(source, substream, params);
3883 dapm_update_dai_unlocked(substream, params, source);
3886 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3887 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3888 sink = path->sink->priv;
3890 ret = snd_soc_dai_hw_params(sink, substream, params);
3894 dapm_update_dai_unlocked(substream, params, sink);
3897 runtime->format = params_format(params);
3898 runtime->subformat = params_subformat(params);
3899 runtime->channels = params_channels(params);
3900 runtime->rate = params_rate(params);
3907 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3908 struct snd_kcontrol *kcontrol, int event)
3910 struct snd_soc_dapm_path *path;
3911 struct snd_soc_dai *source, *sink;
3912 struct snd_pcm_substream *substream = w->priv;
3913 int ret = 0, saved_stream = substream->stream;
3915 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3916 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3920 case SND_SOC_DAPM_PRE_PMU:
3921 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3927 case SND_SOC_DAPM_POST_PMU:
3928 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3929 sink = path->sink->priv;
3931 ret = snd_soc_dai_digital_mute(sink, 0,
3932 SNDRV_PCM_STREAM_PLAYBACK);
3933 if (ret != 0 && ret != -ENOTSUPP)
3935 "ASoC: Failed to unmute: %d\n", ret);
3940 case SND_SOC_DAPM_PRE_PMD:
3941 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3942 sink = path->sink->priv;
3944 ret = snd_soc_dai_digital_mute(sink, 1,
3945 SNDRV_PCM_STREAM_PLAYBACK);
3946 if (ret != 0 && ret != -ENOTSUPP)
3948 "ASoC: Failed to mute: %d\n", ret);
3952 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3953 snd_soc_dapm_widget_for_each_source_path(w, path) {
3954 source = path->source->priv;
3955 snd_soc_dai_hw_free(source, substream);
3958 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3959 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3960 sink = path->sink->priv;
3961 snd_soc_dai_hw_free(sink, substream);
3964 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3965 snd_soc_dapm_widget_for_each_source_path(w, path) {
3966 source = path->source->priv;
3968 snd_soc_dai_shutdown(source, substream);
3971 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3972 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3973 sink = path->sink->priv;
3975 snd_soc_dai_shutdown(sink, substream);
3979 case SND_SOC_DAPM_POST_PMD:
3980 kfree(substream->runtime);
3984 WARN(1, "Unknown event %d\n", event);
3989 /* Restore the substream direction */
3990 substream->stream = saved_stream;
3994 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
3995 struct snd_ctl_elem_value *ucontrol)
3997 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
3998 struct snd_soc_pcm_runtime *rtd = w->priv;
4000 ucontrol->value.enumerated.item[0] = rtd->params_select;
4005 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4006 struct snd_ctl_elem_value *ucontrol)
4008 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4009 struct snd_soc_pcm_runtime *rtd = w->priv;
4011 /* Can't change the config when widget is already powered */
4015 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4018 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4021 rtd->params_select = ucontrol->value.enumerated.item[0];
4027 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4028 unsigned long *private_value,
4030 const char **w_param_text)
4034 devm_kfree(card->dev, (void *)*private_value);
4039 for (count = 0 ; count < num_params; count++)
4040 devm_kfree(card->dev, (void *)w_param_text[count]);
4041 devm_kfree(card->dev, w_param_text);
4044 static struct snd_kcontrol_new *
4045 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4047 const struct snd_soc_pcm_stream *params,
4048 int num_params, const char **w_param_text,
4049 unsigned long *private_value)
4051 struct soc_enum w_param_enum[] = {
4052 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4054 struct snd_kcontrol_new kcontrol_dai_link[] = {
4055 SOC_ENUM_EXT(NULL, w_param_enum[0],
4056 snd_soc_dapm_dai_link_get,
4057 snd_soc_dapm_dai_link_put),
4059 struct snd_kcontrol_new *kcontrol_news;
4060 const struct snd_soc_pcm_stream *config = params;
4063 for (count = 0 ; count < num_params; count++) {
4064 if (!config->stream_name) {
4065 dev_warn(card->dapm.dev,
4066 "ASoC: anonymous config %d for dai link %s\n",
4068 w_param_text[count] =
4069 devm_kasprintf(card->dev, GFP_KERNEL,
4070 "Anonymous Configuration %d",
4073 w_param_text[count] = devm_kmemdup(card->dev,
4074 config->stream_name,
4075 strlen(config->stream_name) + 1,
4078 if (!w_param_text[count])
4079 goto outfree_w_param;
4083 w_param_enum[0].items = num_params;
4084 w_param_enum[0].texts = w_param_text;
4087 (unsigned long) devm_kmemdup(card->dev,
4088 (void *)(kcontrol_dai_link[0].private_value),
4089 sizeof(struct soc_enum), GFP_KERNEL);
4090 if (!*private_value) {
4091 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4093 goto outfree_w_param;
4095 kcontrol_dai_link[0].private_value = *private_value;
4096 /* duplicate kcontrol_dai_link on heap so that memory persists */
4097 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4098 sizeof(struct snd_kcontrol_new),
4100 if (!kcontrol_news) {
4101 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4103 goto outfree_w_param;
4105 return kcontrol_news;
4108 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4112 static struct snd_soc_dapm_widget *
4113 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4114 struct snd_pcm_substream *substream,
4117 struct snd_soc_pcm_runtime *rtd = substream->private_data;
4118 struct snd_soc_dapm_widget template;
4119 struct snd_soc_dapm_widget *w;
4120 const char **w_param_text;
4121 unsigned long private_value = 0;
4125 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4126 rtd->dai_link->name, id);
4128 return ERR_PTR(-ENOMEM);
4130 memset(&template, 0, sizeof(template));
4131 template.reg = SND_SOC_NOPM;
4132 template.id = snd_soc_dapm_dai_link;
4133 template.name = link_name;
4134 template.event = snd_soc_dai_link_event;
4135 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4136 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4137 template.kcontrol_news = NULL;
4139 /* allocate memory for control, only in case of multiple configs */
4140 if (rtd->dai_link->num_params > 1) {
4141 w_param_text = devm_kcalloc(card->dev,
4142 rtd->dai_link->num_params,
4143 sizeof(char *), GFP_KERNEL);
4144 if (!w_param_text) {
4149 template.num_kcontrols = 1;
4150 template.kcontrol_news =
4151 snd_soc_dapm_alloc_kcontrol(card,
4153 rtd->dai_link->params,
4154 rtd->dai_link->num_params,
4155 w_param_text, &private_value);
4156 if (!template.kcontrol_news) {
4161 w_param_text = NULL;
4163 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4165 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4168 goto outfree_kcontrol_news;
4171 w->priv = substream;
4175 outfree_kcontrol_news:
4176 devm_kfree(card->dev, (void *)template.kcontrol_news);
4177 snd_soc_dapm_free_kcontrol(card, &private_value,
4178 rtd->dai_link->num_params, w_param_text);
4180 devm_kfree(card->dev, link_name);
4181 return ERR_PTR(ret);
4184 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4185 struct snd_soc_dai *dai)
4187 struct snd_soc_dapm_widget template;
4188 struct snd_soc_dapm_widget *w;
4190 WARN_ON(dapm->dev != dai->dev);
4192 memset(&template, 0, sizeof(template));
4193 template.reg = SND_SOC_NOPM;
4195 if (dai->driver->playback.stream_name) {
4196 template.id = snd_soc_dapm_dai_in;
4197 template.name = dai->driver->playback.stream_name;
4198 template.sname = dai->driver->playback.stream_name;
4200 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4203 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4208 dai->playback_widget = w;
4211 if (dai->driver->capture.stream_name) {
4212 template.id = snd_soc_dapm_dai_out;
4213 template.name = dai->driver->capture.stream_name;
4214 template.sname = dai->driver->capture.stream_name;
4216 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4219 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4224 dai->capture_widget = w;
4230 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4232 struct snd_soc_dapm_widget *dai_w, *w;
4233 struct snd_soc_dapm_widget *src, *sink;
4234 struct snd_soc_dai *dai;
4236 /* For each DAI widget... */
4237 for_each_card_widgets(card, dai_w) {
4238 switch (dai_w->id) {
4239 case snd_soc_dapm_dai_in:
4240 case snd_soc_dapm_dai_out:
4246 /* let users know there is no DAI to link */
4248 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4255 /* ...find all widgets with the same stream and link them */
4256 for_each_card_widgets(card, w) {
4257 if (w->dapm != dai_w->dapm)
4261 case snd_soc_dapm_dai_in:
4262 case snd_soc_dapm_dai_out:
4268 if (!w->sname || !strstr(w->sname, dai_w->sname))
4271 if (dai_w->id == snd_soc_dapm_dai_in) {
4278 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4279 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4286 static void dapm_add_valid_dai_widget(struct snd_soc_card *card,
4287 struct snd_soc_pcm_runtime *rtd,
4288 struct snd_soc_dai *codec_dai,
4289 struct snd_soc_dai *cpu_dai)
4291 struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
4292 struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
4293 struct snd_pcm_substream *substream;
4294 struct snd_pcm_str *streams = rtd->pcm->streams;
4296 if (rtd->dai_link->params) {
4297 playback_cpu = cpu_dai->capture_widget;
4298 capture_cpu = cpu_dai->playback_widget;
4300 playback = cpu_dai->playback_widget;
4301 capture = cpu_dai->capture_widget;
4302 playback_cpu = playback;
4303 capture_cpu = capture;
4306 /* connect BE DAI playback if widgets are valid */
4307 codec = codec_dai->playback_widget;
4309 if (playback_cpu && codec) {
4311 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4312 playback = snd_soc_dapm_new_dai(card, substream,
4314 if (IS_ERR(playback)) {
4316 "ASoC: Failed to create DAI %s: %ld\n",
4322 snd_soc_dapm_add_path(&card->dapm, playback_cpu,
4323 playback, NULL, NULL);
4326 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4327 cpu_dai->component->name, playback_cpu->name,
4328 codec_dai->component->name, codec->name);
4330 snd_soc_dapm_add_path(&card->dapm, playback, codec,
4335 /* connect BE DAI capture if widgets are valid */
4336 codec = codec_dai->capture_widget;
4338 if (codec && capture_cpu) {
4340 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4341 capture = snd_soc_dapm_new_dai(card, substream,
4343 if (IS_ERR(capture)) {
4345 "ASoC: Failed to create DAI %s: %ld\n",
4351 snd_soc_dapm_add_path(&card->dapm, capture,
4352 capture_cpu, NULL, NULL);
4355 dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
4356 codec_dai->component->name, codec->name,
4357 cpu_dai->component->name, capture_cpu->name);
4359 snd_soc_dapm_add_path(&card->dapm, codec, capture,
4364 static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
4365 struct snd_soc_pcm_runtime *rtd)
4367 struct snd_soc_dai *codec_dai;
4370 if (rtd->num_cpus == 1) {
4371 for_each_rtd_codec_dais(rtd, i, codec_dai)
4372 dapm_add_valid_dai_widget(card, rtd, codec_dai,
4373 asoc_rtd_to_cpu(rtd, 0));
4374 } else if (rtd->num_codecs == rtd->num_cpus) {
4375 for_each_rtd_codec_dais(rtd, i, codec_dai)
4376 dapm_add_valid_dai_widget(card, rtd, codec_dai,
4377 asoc_rtd_to_cpu(rtd, i));
4380 "N cpus to M codecs link is not supported yet\n");
4385 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4388 struct snd_soc_dapm_widget *w;
4391 w = snd_soc_dai_get_widget(dai, stream);
4394 dapm_mark_dirty(w, "stream event");
4396 if (w->id == snd_soc_dapm_dai_in) {
4397 ep = SND_SOC_DAPM_EP_SOURCE;
4398 dapm_widget_invalidate_input_paths(w);
4400 ep = SND_SOC_DAPM_EP_SINK;
4401 dapm_widget_invalidate_output_paths(w);
4405 case SND_SOC_DAPM_STREAM_START:
4409 case SND_SOC_DAPM_STREAM_STOP:
4413 case SND_SOC_DAPM_STREAM_SUSPEND:
4414 case SND_SOC_DAPM_STREAM_RESUME:
4415 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4416 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4422 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4424 struct snd_soc_pcm_runtime *rtd;
4426 /* for each BE DAI link... */
4427 for_each_card_rtds(card, rtd) {
4429 * dynamic FE links have no fixed DAI mapping.
4430 * CODEC<->CODEC links have no direct connection.
4432 if (rtd->dai_link->dynamic)
4435 dapm_connect_dai_link_widgets(card, rtd);
4439 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4442 struct snd_soc_dai *dai;
4445 for_each_rtd_dais(rtd, i, dai)
4446 soc_dapm_dai_stream_event(dai, stream, event);
4448 dapm_power_widgets(rtd->card, event);
4452 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4453 * @rtd: PCM runtime data
4454 * @stream: stream name
4455 * @event: stream event
4457 * Sends a stream event to the dapm core. The core then makes any
4458 * necessary widget power changes.
4460 * Returns 0 for success else error.
4462 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4465 struct snd_soc_card *card = rtd->card;
4467 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4468 soc_dapm_stream_event(rtd, stream, event);
4469 mutex_unlock(&card->dapm_mutex);
4472 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4474 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4475 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4476 /* powered down playback stream now */
4477 snd_soc_dapm_stream_event(rtd,
4478 SNDRV_PCM_STREAM_PLAYBACK,
4479 SND_SOC_DAPM_STREAM_STOP);
4481 /* start delayed pop wq here for playback streams */
4483 queue_delayed_work(system_power_efficient_wq,
4485 msecs_to_jiffies(rtd->pmdown_time));
4488 /* capture streams can be powered down now */
4489 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4490 SND_SOC_DAPM_STREAM_STOP);
4493 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4496 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4497 * @dapm: DAPM context
4500 * Enables input/output pin and its parents or children widgets iff there is
4501 * a valid audio route and active audio stream.
4503 * Requires external locking.
4505 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4506 * do any widget power switching.
4508 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4511 return snd_soc_dapm_set_pin(dapm, pin, 1);
4513 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4516 * snd_soc_dapm_enable_pin - enable pin.
4517 * @dapm: DAPM context
4520 * Enables input/output pin and its parents or children widgets iff there is
4521 * a valid audio route and active audio stream.
4523 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4524 * do any widget power switching.
4526 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4530 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4532 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4534 mutex_unlock(&dapm->card->dapm_mutex);
4538 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4541 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4542 * @dapm: DAPM context
4545 * Enables input/output pin regardless of any other state. This is
4546 * intended for use with microphone bias supplies used in microphone
4549 * Requires external locking.
4551 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4552 * do any widget power switching.
4554 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4557 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4560 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4564 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4565 if (!w->connected) {
4567 * w->force does not affect the number of input or output paths,
4568 * so we only have to recheck if w->connected is changed
4570 dapm_widget_invalidate_input_paths(w);
4571 dapm_widget_invalidate_output_paths(w);
4575 dapm_mark_dirty(w, "force enable");
4579 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4582 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4583 * @dapm: DAPM context
4586 * Enables input/output pin regardless of any other state. This is
4587 * intended for use with microphone bias supplies used in microphone
4590 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4591 * do any widget power switching.
4593 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4598 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4600 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4602 mutex_unlock(&dapm->card->dapm_mutex);
4606 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4609 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4610 * @dapm: DAPM context
4613 * Disables input/output pin and its parents or children widgets.
4615 * Requires external locking.
4617 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4618 * do any widget power switching.
4620 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4623 return snd_soc_dapm_set_pin(dapm, pin, 0);
4625 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4628 * snd_soc_dapm_disable_pin - disable pin.
4629 * @dapm: DAPM context
4632 * Disables input/output pin and its parents or children widgets.
4634 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4635 * do any widget power switching.
4637 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4642 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4644 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4646 mutex_unlock(&dapm->card->dapm_mutex);
4650 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4653 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4654 * @dapm: DAPM context
4657 * Marks the specified pin as being not connected, disabling it along
4658 * any parent or child widgets. At present this is identical to
4659 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4660 * additional things such as disabling controls which only affect
4661 * paths through the pin.
4663 * Requires external locking.
4665 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4666 * do any widget power switching.
4668 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4671 return snd_soc_dapm_set_pin(dapm, pin, 0);
4673 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4676 * snd_soc_dapm_nc_pin - permanently disable pin.
4677 * @dapm: DAPM context
4680 * Marks the specified pin as being not connected, disabling it along
4681 * any parent or child widgets. At present this is identical to
4682 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4683 * additional things such as disabling controls which only affect
4684 * paths through the pin.
4686 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4687 * do any widget power switching.
4689 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4693 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4695 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4697 mutex_unlock(&dapm->card->dapm_mutex);
4701 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4704 * snd_soc_dapm_get_pin_status - get audio pin status
4705 * @dapm: DAPM context
4706 * @pin: audio signal pin endpoint (or start point)
4708 * Get audio pin status - connected or disconnected.
4710 * Returns 1 for connected otherwise 0.
4712 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4715 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4718 return w->connected;
4722 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4725 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4726 * @dapm: DAPM context
4727 * @pin: audio signal pin endpoint (or start point)
4729 * Mark the given endpoint or pin as ignoring suspend. When the
4730 * system is disabled a path between two endpoints flagged as ignoring
4731 * suspend will not be disabled. The path must already be enabled via
4732 * normal means at suspend time, it will not be turned on if it was not
4735 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4738 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4741 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4745 w->ignore_suspend = 1;
4749 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4752 * snd_soc_dapm_free - free dapm resources
4753 * @dapm: DAPM context
4755 * Free all dapm widgets and resources.
4757 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4759 dapm_debugfs_cleanup(dapm);
4760 dapm_free_widgets(dapm);
4761 list_del(&dapm->list);
4763 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4765 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4766 struct snd_soc_card *card,
4767 struct snd_soc_component *component)
4770 dapm->component = component;
4771 dapm->bias_level = SND_SOC_BIAS_OFF;
4774 dapm->dev = component->dev;
4775 dapm->idle_bias_off = !component->driver->idle_bias_on,
4776 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4778 dapm->dev = card->dev;
4781 INIT_LIST_HEAD(&dapm->list);
4782 /* see for_each_card_dapms */
4783 list_add(&dapm->list, &card->dapm_list);
4785 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4787 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4789 struct snd_soc_card *card = dapm->card;
4790 struct snd_soc_dapm_widget *w;
4791 LIST_HEAD(down_list);
4794 mutex_lock(&card->dapm_mutex);
4796 for_each_card_widgets(dapm->card, w) {
4797 if (w->dapm != dapm)
4800 dapm_seq_insert(w, &down_list, false);
4806 /* If there were no widgets to power down we're already in
4810 if (dapm->bias_level == SND_SOC_BIAS_ON)
4811 snd_soc_dapm_set_bias_level(dapm,
4812 SND_SOC_BIAS_PREPARE);
4813 dapm_seq_run(card, &down_list, 0, false);
4814 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4815 snd_soc_dapm_set_bias_level(dapm,
4816 SND_SOC_BIAS_STANDBY);
4819 mutex_unlock(&card->dapm_mutex);
4823 * snd_soc_dapm_shutdown - callback for system shutdown
4825 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4827 struct snd_soc_dapm_context *dapm;
4829 for_each_card_dapms(card, dapm) {
4830 if (dapm != &card->dapm) {
4831 soc_dapm_shutdown_dapm(dapm);
4832 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4833 snd_soc_dapm_set_bias_level(dapm,
4838 soc_dapm_shutdown_dapm(&card->dapm);
4839 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4840 snd_soc_dapm_set_bias_level(&card->dapm,
4844 /* Module information */
4845 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4846 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4847 MODULE_LICENSE("GPL");