1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 /* dapm power sequences - make this per codec in the future */
66 static int dapm_up_seq[] = {
67 [snd_soc_dapm_pre] = 1,
68 [snd_soc_dapm_regulator_supply] = 2,
69 [snd_soc_dapm_pinctrl] = 2,
70 [snd_soc_dapm_clock_supply] = 2,
71 [snd_soc_dapm_supply] = 3,
72 [snd_soc_dapm_micbias] = 4,
73 [snd_soc_dapm_vmid] = 4,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_dai_in] = 5,
76 [snd_soc_dapm_dai_out] = 5,
77 [snd_soc_dapm_aif_in] = 5,
78 [snd_soc_dapm_aif_out] = 5,
79 [snd_soc_dapm_mic] = 6,
80 [snd_soc_dapm_siggen] = 6,
81 [snd_soc_dapm_input] = 6,
82 [snd_soc_dapm_output] = 6,
83 [snd_soc_dapm_mux] = 7,
84 [snd_soc_dapm_demux] = 7,
85 [snd_soc_dapm_dac] = 8,
86 [snd_soc_dapm_switch] = 9,
87 [snd_soc_dapm_mixer] = 9,
88 [snd_soc_dapm_mixer_named_ctl] = 9,
89 [snd_soc_dapm_pga] = 10,
90 [snd_soc_dapm_buffer] = 10,
91 [snd_soc_dapm_scheduler] = 10,
92 [snd_soc_dapm_effect] = 10,
93 [snd_soc_dapm_src] = 10,
94 [snd_soc_dapm_asrc] = 10,
95 [snd_soc_dapm_encoder] = 10,
96 [snd_soc_dapm_decoder] = 10,
97 [snd_soc_dapm_adc] = 11,
98 [snd_soc_dapm_out_drv] = 12,
99 [snd_soc_dapm_hp] = 12,
100 [snd_soc_dapm_spk] = 12,
101 [snd_soc_dapm_line] = 12,
102 [snd_soc_dapm_sink] = 12,
103 [snd_soc_dapm_kcontrol] = 13,
104 [snd_soc_dapm_post] = 14,
107 static int dapm_down_seq[] = {
108 [snd_soc_dapm_pre] = 1,
109 [snd_soc_dapm_kcontrol] = 2,
110 [snd_soc_dapm_adc] = 3,
111 [snd_soc_dapm_hp] = 4,
112 [snd_soc_dapm_spk] = 4,
113 [snd_soc_dapm_line] = 4,
114 [snd_soc_dapm_out_drv] = 4,
115 [snd_soc_dapm_sink] = 4,
116 [snd_soc_dapm_pga] = 5,
117 [snd_soc_dapm_buffer] = 5,
118 [snd_soc_dapm_scheduler] = 5,
119 [snd_soc_dapm_effect] = 5,
120 [snd_soc_dapm_src] = 5,
121 [snd_soc_dapm_asrc] = 5,
122 [snd_soc_dapm_encoder] = 5,
123 [snd_soc_dapm_decoder] = 5,
124 [snd_soc_dapm_switch] = 6,
125 [snd_soc_dapm_mixer_named_ctl] = 6,
126 [snd_soc_dapm_mixer] = 6,
127 [snd_soc_dapm_dac] = 7,
128 [snd_soc_dapm_mic] = 8,
129 [snd_soc_dapm_siggen] = 8,
130 [snd_soc_dapm_input] = 8,
131 [snd_soc_dapm_output] = 8,
132 [snd_soc_dapm_micbias] = 9,
133 [snd_soc_dapm_vmid] = 9,
134 [snd_soc_dapm_mux] = 10,
135 [snd_soc_dapm_demux] = 10,
136 [snd_soc_dapm_aif_in] = 11,
137 [snd_soc_dapm_aif_out] = 11,
138 [snd_soc_dapm_dai_in] = 11,
139 [snd_soc_dapm_dai_out] = 11,
140 [snd_soc_dapm_dai_link] = 12,
141 [snd_soc_dapm_supply] = 13,
142 [snd_soc_dapm_clock_supply] = 14,
143 [snd_soc_dapm_pinctrl] = 14,
144 [snd_soc_dapm_regulator_supply] = 14,
145 [snd_soc_dapm_post] = 15,
148 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
150 if (dapm->card && dapm->card->instantiated)
151 lockdep_assert_held(&dapm->card->dapm_mutex);
154 static void pop_wait(u32 pop_time)
157 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
161 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
169 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
174 vsnprintf(buf, PAGE_SIZE, fmt, args);
175 dev_info(dev, "%s", buf);
181 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
183 return !list_empty(&w->dirty);
186 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
188 dapm_assert_locked(w->dapm);
190 if (!dapm_dirty_widget(w)) {
191 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
193 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
198 * Common implementation for dapm_widget_invalidate_input_paths() and
199 * dapm_widget_invalidate_output_paths(). The function is inlined since the
200 * combined size of the two specialized functions is only marginally larger then
201 * the size of the generic function and at the same time the fast path of the
202 * specialized functions is significantly smaller than the generic function.
204 static __always_inline void dapm_widget_invalidate_paths(
205 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
207 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
208 struct snd_soc_dapm_widget *node;
209 struct snd_soc_dapm_path *p;
212 dapm_assert_locked(w->dapm);
214 if (w->endpoints[dir] == -1)
217 list_add_tail(&w->work_list, &list);
218 w->endpoints[dir] = -1;
220 list_for_each_entry(w, &list, work_list) {
221 snd_soc_dapm_widget_for_each_path(w, dir, p) {
222 if (p->is_supply || p->weak || !p->connect)
224 node = p->node[rdir];
225 if (node->endpoints[dir] != -1) {
226 node->endpoints[dir] = -1;
227 list_add_tail(&node->work_list, &list);
234 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
236 * @w: The widget for which to invalidate the cached number of input paths
238 * Resets the cached number of inputs for the specified widget and all widgets
239 * that can be reached via outcoming paths from the widget.
241 * This function must be called if the number of output paths for a widget might
242 * have changed. E.g. if the source state of a widget changes or a path is added
243 * or activated with the widget as the sink.
245 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
247 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
251 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
253 * @w: The widget for which to invalidate the cached number of output paths
255 * Resets the cached number of outputs for the specified widget and all widgets
256 * that can be reached via incoming paths from the widget.
258 * This function must be called if the number of output paths for a widget might
259 * have changed. E.g. if the sink state of a widget changes or a path is added
260 * or activated with the widget as the source.
262 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
264 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
268 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
269 * for the widgets connected to a path
270 * @p: The path to invalidate
272 * Resets the cached number of inputs for the sink of the path and the cached
273 * number of outputs for the source of the path.
275 * This function must be called when a path is added, removed or the connected
278 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
281 * Weak paths or supply paths do not influence the number of input or
282 * output paths of their neighbors.
284 if (p->weak || p->is_supply)
288 * The number of connected endpoints is the sum of the number of
289 * connected endpoints of all neighbors. If a node with 0 connected
290 * endpoints is either connected or disconnected that sum won't change,
291 * so there is no need to re-check the path.
293 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
294 dapm_widget_invalidate_input_paths(p->sink);
295 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
296 dapm_widget_invalidate_output_paths(p->source);
299 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
301 struct snd_soc_dapm_widget *w;
303 mutex_lock(&card->dapm_mutex);
305 for_each_card_widgets(card, w) {
307 dapm_mark_dirty(w, "Rechecking endpoints");
308 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
309 dapm_widget_invalidate_output_paths(w);
310 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
311 dapm_widget_invalidate_input_paths(w);
315 mutex_unlock(&card->dapm_mutex);
317 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
319 /* create a new dapm widget */
320 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
321 const struct snd_soc_dapm_widget *_widget)
323 struct snd_soc_dapm_widget *w;
325 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
330 * w->name is duplicated in caller, but w->sname isn't.
331 * Duplicate it here if defined
333 if (_widget->sname) {
334 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
343 struct dapm_kcontrol_data {
345 struct snd_soc_dapm_widget *widget;
346 struct list_head paths;
347 struct snd_soc_dapm_widget_list *wlist;
350 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
351 struct snd_kcontrol *kcontrol, const char *ctrl_name)
353 struct dapm_kcontrol_data *data;
354 struct soc_mixer_control *mc;
359 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 INIT_LIST_HEAD(&data->paths);
365 switch (widget->id) {
366 case snd_soc_dapm_switch:
367 case snd_soc_dapm_mixer:
368 case snd_soc_dapm_mixer_named_ctl:
369 mc = (struct soc_mixer_control *)kcontrol->private_value;
371 if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
372 dev_warn(widget->dapm->dev,
373 "ASoC: Unsupported stereo autodisable control '%s'\n",
376 if (mc->autodisable) {
377 struct snd_soc_dapm_widget template;
379 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
386 memset(&template, 0, sizeof(template));
387 template.reg = mc->reg;
388 template.mask = (1 << fls(mc->max)) - 1;
389 template.shift = mc->shift;
391 template.off_val = mc->max;
393 template.off_val = 0;
394 template.on_val = template.off_val;
395 template.id = snd_soc_dapm_kcontrol;
396 template.name = name;
398 data->value = template.on_val;
401 snd_soc_dapm_new_control_unlocked(widget->dapm,
404 if (IS_ERR(data->widget)) {
405 ret = PTR_ERR(data->widget);
410 case snd_soc_dapm_demux:
411 case snd_soc_dapm_mux:
412 e = (struct soc_enum *)kcontrol->private_value;
414 if (e->autodisable) {
415 struct snd_soc_dapm_widget template;
417 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
424 memset(&template, 0, sizeof(template));
425 template.reg = e->reg;
426 template.mask = e->mask;
427 template.shift = e->shift_l;
428 template.off_val = snd_soc_enum_item_to_val(e, 0);
429 template.on_val = template.off_val;
430 template.id = snd_soc_dapm_kcontrol;
431 template.name = name;
433 data->value = template.on_val;
435 data->widget = snd_soc_dapm_new_control_unlocked(
436 widget->dapm, &template);
438 if (IS_ERR(data->widget)) {
439 ret = PTR_ERR(data->widget);
443 snd_soc_dapm_add_path(widget->dapm, data->widget,
451 kcontrol->private_data = data;
460 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
462 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
464 list_del(&data->paths);
469 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
470 const struct snd_kcontrol *kcontrol)
472 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
477 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
478 struct snd_soc_dapm_widget *widget)
480 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
481 struct snd_soc_dapm_widget_list *new_wlist;
485 n = data->wlist->num_widgets + 1;
489 new_wlist = krealloc(data->wlist,
490 struct_size(new_wlist, widgets, n),
495 new_wlist->widgets[n - 1] = widget;
496 new_wlist->num_widgets = n;
498 data->wlist = new_wlist;
503 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
504 struct snd_soc_dapm_path *path)
506 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
508 list_add_tail(&path->list_kcontrol, &data->paths);
511 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
513 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
518 return data->widget->power;
521 static struct list_head *dapm_kcontrol_get_path_list(
522 const struct snd_kcontrol *kcontrol)
524 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
529 #define dapm_kcontrol_for_each_path(path, kcontrol) \
530 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
533 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
535 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
539 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
541 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
544 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
546 if (data->value == value)
550 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
551 case snd_soc_dapm_switch:
552 case snd_soc_dapm_mixer:
553 case snd_soc_dapm_mixer_named_ctl:
554 data->widget->on_val = value & data->widget->mask;
556 case snd_soc_dapm_demux:
557 case snd_soc_dapm_mux:
558 data->widget->on_val = value >> data->widget->shift;
561 data->widget->on_val = value;
572 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
574 * @kcontrol: The kcontrol
576 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
577 struct snd_kcontrol *kcontrol)
579 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
581 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
584 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
586 * @kcontrol: The kcontrol
588 * Note: This function must only be used on kcontrols that are known to have
589 * been registered for a CODEC. Otherwise the behaviour is undefined.
591 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
592 struct snd_kcontrol *kcontrol)
594 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
596 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
598 static void dapm_reset(struct snd_soc_card *card)
600 struct snd_soc_dapm_widget *w;
602 lockdep_assert_held(&card->dapm_mutex);
604 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
606 for_each_card_widgets(card, w) {
607 w->new_power = w->power;
608 w->power_checked = false;
612 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
614 if (!dapm->component)
616 return dapm->component->name_prefix;
619 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
621 if (!dapm->component)
623 return snd_soc_component_read(dapm->component, reg);
626 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
627 int reg, unsigned int mask, unsigned int value)
629 if (!dapm->component)
631 return snd_soc_component_update_bits(dapm->component, reg,
635 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
636 int reg, unsigned int mask, unsigned int value)
638 if (!dapm->component)
640 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
643 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
646 snd_soc_component_async_complete(dapm->component);
649 static struct snd_soc_dapm_widget *
650 dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
652 struct snd_soc_dapm_widget *w = wcache->widget;
655 struct list_head *wlist = &w->dapm->card->widgets;
659 list_for_each_entry_from(w, wlist, list) {
660 if (!strcmp(name, w->name))
671 static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
672 struct snd_soc_dapm_widget *w)
678 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
679 * @dapm: The DAPM context for which to set the level
680 * @level: The level to set
682 * Forces the DAPM bias level to a specific state. It will call the bias level
683 * callback of DAPM context with the specified level. This will even happen if
684 * the context is already at the same level. Furthermore it will not go through
685 * the normal bias level sequencing, meaning any intermediate states between the
686 * current and the target state will not be entered.
688 * Note that the change in bias level is only temporary and the next time
689 * snd_soc_dapm_sync() is called the state will be set to the level as
690 * determined by the DAPM core. The function is mainly intended to be used to
691 * used during probe or resume from suspend to power up the device so
692 * initialization can be done, before the DAPM core takes over.
694 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
695 enum snd_soc_bias_level level)
700 ret = snd_soc_component_set_bias_level(dapm->component, level);
703 dapm->bias_level = level;
707 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
710 * snd_soc_dapm_set_bias_level - set the bias level for the system
711 * @dapm: DAPM context
712 * @level: level to configure
714 * Configure the bias (power) levels for the SoC audio device.
716 * Returns 0 for success else error.
718 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
719 enum snd_soc_bias_level level)
721 struct snd_soc_card *card = dapm->card;
724 trace_snd_soc_bias_level_start(card, level);
726 ret = snd_soc_card_set_bias_level(card, dapm, level);
730 if (!card || dapm != &card->dapm)
731 ret = snd_soc_dapm_force_bias_level(dapm, level);
736 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
738 trace_snd_soc_bias_level_done(card, level);
743 /* connect mux widget to its interconnecting audio paths */
744 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
745 struct snd_soc_dapm_path *path, const char *control_name,
746 struct snd_soc_dapm_widget *w)
748 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
749 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
753 if (e->reg != SND_SOC_NOPM) {
755 val = soc_dapm_read(dapm, e->reg);
756 val = (val >> e->shift_l) & e->mask;
757 item = snd_soc_enum_val_to_item(e, val);
759 /* since a virtual mux has no backing registers to
760 * decide which path to connect, it will try to match
761 * with the first enumeration. This is to ensure
762 * that the default mux choice (the first) will be
763 * correctly powered up during initialization.
768 i = match_string(e->texts, e->items, control_name);
772 path->name = e->texts[i];
773 path->connect = (i == item);
778 /* set up initial codec paths */
779 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
782 struct soc_mixer_control *mc = (struct soc_mixer_control *)
783 p->sink->kcontrol_news[i].private_value;
784 unsigned int reg = mc->reg;
785 unsigned int invert = mc->invert;
787 if (reg != SND_SOC_NOPM) {
788 unsigned int shift = mc->shift;
789 unsigned int max = mc->max;
790 unsigned int mask = (1 << fls(max)) - 1;
791 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
794 * The nth_path argument allows this function to know
795 * which path of a kcontrol it is setting the initial
796 * status for. Ideally this would support any number
797 * of paths and channels. But since kcontrols only come
798 * in mono and stereo variants, we are limited to 2
801 * The following code assumes for stereo controls the
802 * first path is the left channel, and all remaining
803 * paths are the right channel.
805 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
807 val = soc_dapm_read(p->sink->dapm, mc->rreg);
808 val = (val >> mc->rshift) & mask;
810 val = (val >> shift) & mask;
816 /* since a virtual mixer has no backing registers to
817 * decide which path to connect, it will try to match
818 * with initial state. This is to ensure
819 * that the default mixer choice will be
820 * correctly powered up during initialization.
826 /* connect mixer widget to its interconnecting audio paths */
827 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
828 struct snd_soc_dapm_path *path, const char *control_name)
832 /* search for mixer kcontrol */
833 for (i = 0; i < path->sink->num_kcontrols; i++) {
834 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
835 path->name = path->sink->kcontrol_news[i].name;
836 dapm_set_mixer_path_status(path, i, nth_path++);
843 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
844 struct snd_soc_dapm_widget *kcontrolw,
845 const struct snd_kcontrol_new *kcontrol_new,
846 struct snd_kcontrol **kcontrol)
848 struct snd_soc_dapm_widget *w;
853 for_each_card_widgets(dapm->card, w) {
854 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
856 for (i = 0; i < w->num_kcontrols; i++) {
857 if (&w->kcontrol_news[i] == kcontrol_new) {
859 *kcontrol = w->kcontrols[i];
869 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
870 * create it. Either way, add the widget into the control's widget list
872 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
875 struct snd_soc_dapm_context *dapm = w->dapm;
876 struct snd_card *card = dapm->card->snd_card;
880 struct snd_kcontrol *kcontrol;
881 bool wname_in_long_name, kcname_in_long_name;
882 char *long_name = NULL;
886 prefix = soc_dapm_prefix(dapm);
888 prefix_len = strlen(prefix) + 1;
892 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
897 wname_in_long_name = false;
898 kcname_in_long_name = true;
901 case snd_soc_dapm_switch:
902 case snd_soc_dapm_mixer:
903 case snd_soc_dapm_pga:
904 case snd_soc_dapm_effect:
905 case snd_soc_dapm_out_drv:
906 wname_in_long_name = true;
907 kcname_in_long_name = true;
909 case snd_soc_dapm_mixer_named_ctl:
910 wname_in_long_name = false;
911 kcname_in_long_name = true;
913 case snd_soc_dapm_demux:
914 case snd_soc_dapm_mux:
915 wname_in_long_name = true;
916 kcname_in_long_name = false;
923 if (wname_in_long_name && kcname_in_long_name) {
925 * The control will get a prefix from the control
926 * creation process but we're also using the same
927 * prefix for widgets so cut the prefix off the
928 * front of the widget name.
930 long_name = kasprintf(GFP_KERNEL, "%s %s",
931 w->name + prefix_len,
932 w->kcontrol_news[kci].name);
933 if (long_name == NULL)
937 } else if (wname_in_long_name) {
939 name = w->name + prefix_len;
942 name = w->kcontrol_news[kci].name;
945 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
952 kcontrol->private_free = dapm_kcontrol_free;
954 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
956 snd_ctl_free_one(kcontrol);
960 ret = snd_ctl_add(card, kcontrol);
963 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
969 ret = dapm_kcontrol_add_widget(kcontrol, w);
971 w->kcontrols[kci] = kcontrol;
979 /* create new dapm mixer control */
980 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
983 struct snd_soc_dapm_path *path;
984 struct dapm_kcontrol_data *data;
987 for (i = 0; i < w->num_kcontrols; i++) {
989 snd_soc_dapm_widget_for_each_source_path(w, path) {
990 /* mixer/mux paths name must match control name */
991 if (path->name != (char *)w->kcontrol_news[i].name)
994 if (!w->kcontrols[i]) {
995 ret = dapm_create_or_share_kcontrol(w, i);
1000 dapm_kcontrol_add_path(w->kcontrols[i], path);
1002 data = snd_kcontrol_chip(w->kcontrols[i]);
1004 snd_soc_dapm_add_path(data->widget->dapm,
1014 /* create new dapm mux control */
1015 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1017 struct snd_soc_dapm_context *dapm = w->dapm;
1018 enum snd_soc_dapm_direction dir;
1019 struct snd_soc_dapm_path *path;
1024 case snd_soc_dapm_mux:
1025 dir = SND_SOC_DAPM_DIR_OUT;
1028 case snd_soc_dapm_demux:
1029 dir = SND_SOC_DAPM_DIR_IN;
1036 if (w->num_kcontrols != 1) {
1038 "ASoC: %s %s has incorrect number of controls\n", type,
1043 if (list_empty(&w->edges[dir])) {
1044 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1048 ret = dapm_create_or_share_kcontrol(w, 0);
1052 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1054 dapm_kcontrol_add_path(w->kcontrols[0], path);
1060 /* create new dapm volume control */
1061 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1065 for (i = 0; i < w->num_kcontrols; i++) {
1066 int ret = dapm_create_or_share_kcontrol(w, i);
1074 /* create new dapm dai link control */
1075 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1078 struct snd_soc_pcm_runtime *rtd = w->priv;
1080 /* create control for links with > 1 config */
1081 if (rtd->dai_link->num_params <= 1)
1085 for (i = 0; i < w->num_kcontrols; i++) {
1086 struct snd_soc_dapm_context *dapm = w->dapm;
1087 struct snd_card *card = dapm->card->snd_card;
1088 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1090 int ret = snd_ctl_add(card, kcontrol);
1094 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1095 w->name, w->kcontrol_news[i].name, ret);
1098 kcontrol->private_data = w;
1099 w->kcontrols[i] = kcontrol;
1105 /* We implement power down on suspend by checking the power state of
1106 * the ALSA card - when we are suspending the ALSA state for the card
1109 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1111 int level = snd_power_get_state(widget->dapm->card->snd_card);
1114 case SNDRV_CTL_POWER_D3hot:
1115 case SNDRV_CTL_POWER_D3cold:
1116 if (widget->ignore_suspend)
1117 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1119 return widget->ignore_suspend;
1125 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1130 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1131 struct list_head *widgets)
1133 struct snd_soc_dapm_widget *w;
1134 struct list_head *it;
1135 unsigned int size = 0;
1138 list_for_each(it, widgets)
1141 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1145 list_for_each_entry(w, widgets, work_list)
1146 (*list)->widgets[i++] = w;
1148 (*list)->num_widgets = i;
1154 * Recursively reset the cached number of inputs or outputs for the specified
1155 * widget and all widgets that can be reached via incoming or outcoming paths
1158 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1159 enum snd_soc_dapm_direction dir)
1161 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1162 struct snd_soc_dapm_path *path;
1164 widget->endpoints[dir] = -1;
1166 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1167 if (path->weak || path->is_supply)
1173 if (path->connect) {
1175 invalidate_paths_ep(path->node[dir], dir);
1182 * Common implementation for is_connected_output_ep() and
1183 * is_connected_input_ep(). The function is inlined since the combined size of
1184 * the two specialized functions is only marginally larger then the size of the
1185 * generic function and at the same time the fast path of the specialized
1186 * functions is significantly smaller than the generic function.
1188 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1189 struct list_head *list, enum snd_soc_dapm_direction dir,
1190 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1191 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1192 enum snd_soc_dapm_direction)),
1193 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1194 enum snd_soc_dapm_direction))
1196 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1197 struct snd_soc_dapm_path *path;
1200 if (widget->endpoints[dir] >= 0)
1201 return widget->endpoints[dir];
1203 DAPM_UPDATE_STAT(widget, path_checks);
1205 /* do we need to add this widget to the list ? */
1207 list_add_tail(&widget->work_list, list);
1209 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1211 custom_stop_condition = NULL;
1214 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1215 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1216 return widget->endpoints[dir];
1219 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1220 DAPM_UPDATE_STAT(widget, neighbour_checks);
1222 if (path->weak || path->is_supply)
1228 trace_snd_soc_dapm_path(widget, dir, path);
1230 if (path->connect) {
1232 con += fn(path->node[dir], list, custom_stop_condition);
1237 widget->endpoints[dir] = con;
1243 * Recursively check for a completed path to an active or physically connected
1244 * output widget. Returns number of complete paths.
1246 * Optionally, can be supplied with a function acting as a stopping condition.
1247 * This function takes the dapm widget currently being examined and the walk
1248 * direction as an arguments, it should return true if widgets from that point
1249 * in the graph onwards should not be added to the widget list.
1251 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1252 struct list_head *list,
1253 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1254 enum snd_soc_dapm_direction))
1256 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1257 is_connected_output_ep, custom_stop_condition);
1261 * Recursively check for a completed path to an active or physically connected
1262 * input widget. Returns number of complete paths.
1264 * Optionally, can be supplied with a function acting as a stopping condition.
1265 * This function takes the dapm widget currently being examined and the walk
1266 * direction as an arguments, it should return true if the walk should be
1267 * stopped and false otherwise.
1269 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1270 struct list_head *list,
1271 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1272 enum snd_soc_dapm_direction))
1274 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1275 is_connected_input_ep, custom_stop_condition);
1279 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1280 * @dai: the soc DAI.
1281 * @stream: stream direction.
1282 * @list: list of active widgets for this stream.
1283 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1284 * walk based on custom logic.
1286 * Queries DAPM graph as to whether a valid audio stream path exists for
1287 * the initial stream specified by name. This takes into account
1288 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1290 * Optionally, can be supplied with a function acting as a stopping condition.
1291 * This function takes the dapm widget currently being examined and the walk
1292 * direction as an arguments, it should return true if the walk should be
1293 * stopped and false otherwise.
1295 * Returns the number of valid paths or negative error.
1297 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1298 struct snd_soc_dapm_widget_list **list,
1299 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1300 enum snd_soc_dapm_direction))
1302 struct snd_soc_card *card = dai->component->card;
1303 struct snd_soc_dapm_widget *w;
1308 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1310 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1311 w = dai->playback_widget;
1312 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1313 paths = is_connected_output_ep(w, &widgets,
1314 custom_stop_condition);
1316 w = dai->capture_widget;
1317 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1318 paths = is_connected_input_ep(w, &widgets,
1319 custom_stop_condition);
1322 /* Drop starting point */
1323 list_del(widgets.next);
1325 ret = dapm_widget_list_create(list, &widgets);
1329 trace_snd_soc_dapm_connected(paths, stream);
1330 mutex_unlock(&card->dapm_mutex);
1334 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1336 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1338 dapm_widget_list_free(list);
1340 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1343 * Handler for regulator supply widget.
1345 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1346 struct snd_kcontrol *kcontrol, int event)
1350 soc_dapm_async_complete(w->dapm);
1352 if (SND_SOC_DAPM_EVENT_ON(event)) {
1353 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1354 ret = regulator_allow_bypass(w->regulator, false);
1356 dev_warn(w->dapm->dev,
1357 "ASoC: Failed to unbypass %s: %d\n",
1361 return regulator_enable(w->regulator);
1363 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1364 ret = regulator_allow_bypass(w->regulator, true);
1366 dev_warn(w->dapm->dev,
1367 "ASoC: Failed to bypass %s: %d\n",
1371 return regulator_disable_deferred(w->regulator, w->shift);
1374 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1377 * Handler for pinctrl widget.
1379 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1380 struct snd_kcontrol *kcontrol, int event)
1382 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1383 struct pinctrl *p = w->pinctrl;
1384 struct pinctrl_state *s;
1389 if (SND_SOC_DAPM_EVENT_ON(event))
1390 s = pinctrl_lookup_state(p, priv->active_state);
1392 s = pinctrl_lookup_state(p, priv->sleep_state);
1397 return pinctrl_select_state(p, s);
1399 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1402 * Handler for clock supply widget.
1404 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1405 struct snd_kcontrol *kcontrol, int event)
1410 soc_dapm_async_complete(w->dapm);
1412 if (SND_SOC_DAPM_EVENT_ON(event)) {
1413 return clk_prepare_enable(w->clk);
1415 clk_disable_unprepare(w->clk);
1421 EXPORT_SYMBOL_GPL(dapm_clock_event);
1423 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1425 if (w->power_checked)
1426 return w->new_power;
1431 w->new_power = w->power_check(w);
1433 w->power_checked = true;
1435 return w->new_power;
1438 /* Generic check to see if a widget should be powered. */
1439 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1443 DAPM_UPDATE_STAT(w, power_checks);
1445 in = is_connected_input_ep(w, NULL, NULL);
1446 out = is_connected_output_ep(w, NULL, NULL);
1447 return out != 0 && in != 0;
1450 /* Check to see if a power supply is needed */
1451 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1453 struct snd_soc_dapm_path *path;
1455 DAPM_UPDATE_STAT(w, power_checks);
1457 /* Check if one of our outputs is connected */
1458 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1459 DAPM_UPDATE_STAT(w, neighbour_checks);
1464 if (path->connected &&
1465 !path->connected(path->source, path->sink))
1468 if (dapm_widget_power_check(path->sink))
1475 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1477 return w->connected;
1480 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1481 struct snd_soc_dapm_widget *b,
1486 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1487 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1492 sort = dapm_down_seq;
1494 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1495 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1497 if (sort[a->id] != sort[b->id])
1498 return sort[a->id] - sort[b->id];
1499 if (a->subseq != b->subseq) {
1501 return a->subseq - b->subseq;
1503 return b->subseq - a->subseq;
1505 if (a->reg != b->reg)
1506 return a->reg - b->reg;
1507 if (a->dapm != b->dapm)
1508 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1513 /* Insert a widget in order into a DAPM power sequence. */
1514 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1515 struct list_head *list,
1518 struct snd_soc_dapm_widget *w;
1520 list_for_each_entry(w, list, power_list)
1521 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1522 list_add_tail(&new_widget->power_list, &w->power_list);
1526 list_add_tail(&new_widget->power_list, list);
1529 static void dapm_seq_check_event(struct snd_soc_card *card,
1530 struct snd_soc_dapm_widget *w, int event)
1532 const char *ev_name;
1536 case SND_SOC_DAPM_PRE_PMU:
1537 ev_name = "PRE_PMU";
1540 case SND_SOC_DAPM_POST_PMU:
1541 ev_name = "POST_PMU";
1544 case SND_SOC_DAPM_PRE_PMD:
1545 ev_name = "PRE_PMD";
1548 case SND_SOC_DAPM_POST_PMD:
1549 ev_name = "POST_PMD";
1552 case SND_SOC_DAPM_WILL_PMU:
1553 ev_name = "WILL_PMU";
1556 case SND_SOC_DAPM_WILL_PMD:
1557 ev_name = "WILL_PMD";
1561 WARN(1, "Unknown event %d\n", event);
1565 if (w->new_power != power)
1568 if (w->event && (w->event_flags & event)) {
1571 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1573 soc_dapm_async_complete(w->dapm);
1574 trace_snd_soc_dapm_widget_event_start(w, event);
1575 ret = w->event(w, NULL, event);
1576 trace_snd_soc_dapm_widget_event_done(w, event);
1578 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1579 ev_name, w->name, ret);
1583 /* Apply the coalesced changes from a DAPM sequence */
1584 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1585 struct list_head *pending)
1587 struct snd_soc_dapm_context *dapm;
1588 struct snd_soc_dapm_widget *w;
1590 unsigned int value = 0;
1591 unsigned int mask = 0;
1593 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1597 list_for_each_entry(w, pending, power_list) {
1598 WARN_ON(reg != w->reg || dapm != w->dapm);
1599 w->power = w->new_power;
1601 mask |= w->mask << w->shift;
1603 value |= w->on_val << w->shift;
1605 value |= w->off_val << w->shift;
1607 pop_dbg(dapm->dev, card->pop_time,
1608 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1609 w->name, reg, value, mask);
1611 /* Check for events */
1612 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1613 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1617 /* Any widget will do, they should all be updating the
1621 pop_dbg(dapm->dev, card->pop_time,
1622 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1623 value, mask, reg, card->pop_time);
1624 pop_wait(card->pop_time);
1625 soc_dapm_update_bits(dapm, reg, mask, value);
1628 list_for_each_entry(w, pending, power_list) {
1629 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1630 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1634 /* Apply a DAPM power sequence.
1636 * We walk over a pre-sorted list of widgets to apply power to. In
1637 * order to minimise the number of writes to the device required
1638 * multiple widgets will be updated in a single write where possible.
1639 * Currently anything that requires more than a single write is not
1642 static void dapm_seq_run(struct snd_soc_card *card,
1643 struct list_head *list, int event, bool power_up)
1645 struct snd_soc_dapm_widget *w, *n;
1646 struct snd_soc_dapm_context *d;
1649 int cur_subseq = -1;
1650 int cur_reg = SND_SOC_NOPM;
1651 struct snd_soc_dapm_context *cur_dapm = NULL;
1658 sort = dapm_down_seq;
1660 list_for_each_entry_safe(w, n, list, power_list) {
1663 /* Do we need to apply any queued changes? */
1664 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1665 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1666 if (!list_empty(&pending))
1667 dapm_seq_run_coalesced(card, &pending);
1669 if (cur_dapm && cur_dapm->component) {
1670 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1671 if (sort[i] == cur_sort)
1672 snd_soc_component_seq_notifier(
1673 cur_dapm->component,
1677 if (cur_dapm && w->dapm != cur_dapm)
1678 soc_dapm_async_complete(cur_dapm);
1680 INIT_LIST_HEAD(&pending);
1682 cur_subseq = INT_MIN;
1683 cur_reg = SND_SOC_NOPM;
1688 case snd_soc_dapm_pre:
1690 list_for_each_entry_safe_continue(w, n, list,
1693 if (event == SND_SOC_DAPM_STREAM_START)
1695 NULL, SND_SOC_DAPM_PRE_PMU);
1696 else if (event == SND_SOC_DAPM_STREAM_STOP)
1698 NULL, SND_SOC_DAPM_PRE_PMD);
1701 case snd_soc_dapm_post:
1703 list_for_each_entry_safe_continue(w, n, list,
1706 if (event == SND_SOC_DAPM_STREAM_START)
1708 NULL, SND_SOC_DAPM_POST_PMU);
1709 else if (event == SND_SOC_DAPM_STREAM_STOP)
1711 NULL, SND_SOC_DAPM_POST_PMD);
1715 /* Queue it up for application */
1716 cur_sort = sort[w->id];
1717 cur_subseq = w->subseq;
1720 list_move(&w->power_list, &pending);
1725 dev_err(w->dapm->dev,
1726 "ASoC: Failed to apply widget power: %d\n", ret);
1729 if (!list_empty(&pending))
1730 dapm_seq_run_coalesced(card, &pending);
1732 if (cur_dapm && cur_dapm->component) {
1733 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1734 if (sort[i] == cur_sort)
1735 snd_soc_component_seq_notifier(
1736 cur_dapm->component,
1740 for_each_card_dapms(card, d)
1741 soc_dapm_async_complete(d);
1744 static void dapm_widget_update(struct snd_soc_card *card)
1746 struct snd_soc_dapm_update *update = card->update;
1747 struct snd_soc_dapm_widget_list *wlist;
1748 struct snd_soc_dapm_widget *w = NULL;
1752 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1755 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1757 for_each_dapm_widgets(wlist, wi, w) {
1758 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1759 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1761 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1769 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1772 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1775 if (update->has_second_set) {
1776 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1777 update->mask2, update->val2);
1779 dev_err(w->dapm->dev,
1780 "ASoC: %s DAPM update failed: %d\n",
1784 for_each_dapm_widgets(wlist, wi, w) {
1785 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1786 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1788 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1794 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1795 * they're changing state.
1797 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1799 struct snd_soc_dapm_context *d = data;
1802 /* If we're off and we're not supposed to go into STANDBY */
1803 if (d->bias_level == SND_SOC_BIAS_OFF &&
1804 d->target_bias_level != SND_SOC_BIAS_OFF) {
1805 if (d->dev && cookie)
1806 pm_runtime_get_sync(d->dev);
1808 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1811 "ASoC: Failed to turn on bias: %d\n", ret);
1814 /* Prepare for a transition to ON or away from ON */
1815 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1816 d->bias_level != SND_SOC_BIAS_ON) ||
1817 (d->target_bias_level != SND_SOC_BIAS_ON &&
1818 d->bias_level == SND_SOC_BIAS_ON)) {
1819 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1822 "ASoC: Failed to prepare bias: %d\n", ret);
1826 /* Async callback run prior to DAPM sequences - brings to their final
1829 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1831 struct snd_soc_dapm_context *d = data;
1834 /* If we just powered the last thing off drop to standby bias */
1835 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1836 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1837 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1838 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1840 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1844 /* If we're in standby and can support bias off then do that */
1845 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1846 d->target_bias_level == SND_SOC_BIAS_OFF) {
1847 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1849 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1852 if (d->dev && cookie)
1853 pm_runtime_put(d->dev);
1856 /* If we just powered up then move to active bias */
1857 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1858 d->target_bias_level == SND_SOC_BIAS_ON) {
1859 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1861 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1866 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1867 bool power, bool connect)
1869 /* If a connection is being made or broken then that update
1870 * will have marked the peer dirty, otherwise the widgets are
1871 * not connected and this update has no impact. */
1875 /* If the peer is already in the state we're moving to then we
1876 * won't have an impact on it. */
1877 if (power != peer->power)
1878 dapm_mark_dirty(peer, "peer state change");
1881 static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1882 struct list_head *up_list,
1883 struct list_head *down_list)
1885 struct snd_soc_dapm_path *path;
1887 if (w->power == power)
1890 trace_snd_soc_dapm_widget_power(w, power);
1892 /* If we changed our power state perhaps our neigbours changed
1895 snd_soc_dapm_widget_for_each_source_path(w, path)
1896 dapm_widget_set_peer_power(path->source, power, path->connect);
1898 /* Supplies can't affect their outputs, only their inputs */
1899 if (!w->is_supply) {
1900 snd_soc_dapm_widget_for_each_sink_path(w, path)
1901 dapm_widget_set_peer_power(path->sink, power,
1906 dapm_seq_insert(w, up_list, true);
1908 dapm_seq_insert(w, down_list, false);
1911 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1912 struct list_head *up_list,
1913 struct list_head *down_list)
1918 case snd_soc_dapm_pre:
1919 dapm_seq_insert(w, down_list, false);
1921 case snd_soc_dapm_post:
1922 dapm_seq_insert(w, up_list, true);
1926 power = dapm_widget_power_check(w);
1928 dapm_widget_set_power(w, power, up_list, down_list);
1933 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1935 if (dapm->idle_bias_off)
1938 switch (snd_power_get_state(dapm->card->snd_card)) {
1939 case SNDRV_CTL_POWER_D3hot:
1940 case SNDRV_CTL_POWER_D3cold:
1941 return dapm->suspend_bias_off;
1950 * Scan each dapm widget for complete audio path.
1951 * A complete path is a route that has valid endpoints i.e.:-
1953 * o DAC to output pin.
1954 * o Input pin to ADC.
1955 * o Input pin to Output pin (bypass, sidetone)
1956 * o DAC to ADC (loopback).
1958 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1960 struct snd_soc_dapm_widget *w;
1961 struct snd_soc_dapm_context *d;
1963 LIST_HEAD(down_list);
1964 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1965 enum snd_soc_bias_level bias;
1968 lockdep_assert_held(&card->dapm_mutex);
1970 trace_snd_soc_dapm_start(card);
1972 for_each_card_dapms(card, d) {
1973 if (dapm_idle_bias_off(d))
1974 d->target_bias_level = SND_SOC_BIAS_OFF;
1976 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1981 /* Check which widgets we need to power and store them in
1982 * lists indicating if they should be powered up or down. We
1983 * only check widgets that have been flagged as dirty but note
1984 * that new widgets may be added to the dirty list while we
1987 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1988 dapm_power_one_widget(w, &up_list, &down_list);
1991 for_each_card_widgets(card, w) {
1993 case snd_soc_dapm_pre:
1994 case snd_soc_dapm_post:
1995 /* These widgets always need to be powered */
1998 list_del_init(&w->dirty);
2005 /* Supplies and micbiases only bring the
2006 * context up to STANDBY as unless something
2007 * else is active and passing audio they
2008 * generally don't require full power. Signal
2009 * generators are virtual pins and have no
2010 * power impact themselves.
2013 case snd_soc_dapm_siggen:
2014 case snd_soc_dapm_vmid:
2016 case snd_soc_dapm_supply:
2017 case snd_soc_dapm_regulator_supply:
2018 case snd_soc_dapm_pinctrl:
2019 case snd_soc_dapm_clock_supply:
2020 case snd_soc_dapm_micbias:
2021 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2022 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2025 d->target_bias_level = SND_SOC_BIAS_ON;
2032 /* Force all contexts in the card to the same bias state if
2033 * they're not ground referenced.
2035 bias = SND_SOC_BIAS_OFF;
2036 for_each_card_dapms(card, d)
2037 if (d->target_bias_level > bias)
2038 bias = d->target_bias_level;
2039 for_each_card_dapms(card, d)
2040 if (!dapm_idle_bias_off(d))
2041 d->target_bias_level = bias;
2043 trace_snd_soc_dapm_walk_done(card);
2045 /* Run card bias changes at first */
2046 dapm_pre_sequence_async(&card->dapm, 0);
2047 /* Run other bias changes in parallel */
2048 for_each_card_dapms(card, d) {
2049 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2050 async_schedule_domain(dapm_pre_sequence_async, d,
2053 async_synchronize_full_domain(&async_domain);
2055 list_for_each_entry(w, &down_list, power_list) {
2056 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2059 list_for_each_entry(w, &up_list, power_list) {
2060 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2063 /* Power down widgets first; try to avoid amplifying pops. */
2064 dapm_seq_run(card, &down_list, event, false);
2066 dapm_widget_update(card);
2069 dapm_seq_run(card, &up_list, event, true);
2071 /* Run all the bias changes in parallel */
2072 for_each_card_dapms(card, d) {
2073 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2074 async_schedule_domain(dapm_post_sequence_async, d,
2077 async_synchronize_full_domain(&async_domain);
2078 /* Run card bias changes at last */
2079 dapm_post_sequence_async(&card->dapm, 0);
2081 /* do we need to notify any clients that DAPM event is complete */
2082 for_each_card_dapms(card, d) {
2086 ret = snd_soc_component_stream_event(d->component, event);
2091 pop_dbg(card->dev, card->pop_time,
2092 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2093 pop_wait(card->pop_time);
2095 trace_snd_soc_dapm_done(card);
2100 #ifdef CONFIG_DEBUG_FS
2101 static ssize_t dapm_widget_power_read_file(struct file *file,
2102 char __user *user_buf,
2103 size_t count, loff_t *ppos)
2105 struct snd_soc_dapm_widget *w = file->private_data;
2106 struct snd_soc_card *card = w->dapm->card;
2107 enum snd_soc_dapm_direction dir, rdir;
2111 struct snd_soc_dapm_path *p = NULL;
2113 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2117 mutex_lock(&card->dapm_mutex);
2119 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2124 in = is_connected_input_ep(w, NULL, NULL);
2125 out = is_connected_output_ep(w, NULL, NULL);
2128 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2129 w->name, w->power ? "On" : "Off",
2130 w->force ? " (forced)" : "", in, out);
2133 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2134 " - R%d(0x%x) mask 0x%x",
2135 w->reg, w->reg, w->mask << w->shift);
2137 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2140 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2142 w->active ? "active" : "inactive");
2144 snd_soc_dapm_for_each_direction(dir) {
2145 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2146 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2147 if (p->connected && !p->connected(p->source, p->sink))
2153 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2154 " %s \"%s\" \"%s\"\n",
2155 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2156 p->name ? p->name : "static",
2157 p->node[rdir]->name);
2161 mutex_unlock(&card->dapm_mutex);
2163 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2169 static const struct file_operations dapm_widget_power_fops = {
2170 .open = simple_open,
2171 .read = dapm_widget_power_read_file,
2172 .llseek = default_llseek,
2175 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2176 size_t count, loff_t *ppos)
2178 struct snd_soc_dapm_context *dapm = file->private_data;
2181 switch (dapm->bias_level) {
2182 case SND_SOC_BIAS_ON:
2185 case SND_SOC_BIAS_PREPARE:
2186 level = "Prepare\n";
2188 case SND_SOC_BIAS_STANDBY:
2189 level = "Standby\n";
2191 case SND_SOC_BIAS_OFF:
2195 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2196 level = "Unknown\n";
2200 return simple_read_from_buffer(user_buf, count, ppos, level,
2204 static const struct file_operations dapm_bias_fops = {
2205 .open = simple_open,
2206 .read = dapm_bias_read_file,
2207 .llseek = default_llseek,
2210 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2211 struct dentry *parent)
2213 if (!parent || IS_ERR(parent))
2216 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2218 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2222 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2224 struct snd_soc_dapm_context *dapm = w->dapm;
2226 if (!dapm->debugfs_dapm || !w->name)
2229 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2230 &dapm_widget_power_fops);
2233 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2235 debugfs_remove_recursive(dapm->debugfs_dapm);
2236 dapm->debugfs_dapm = NULL;
2240 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2241 struct dentry *parent)
2245 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2249 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2256 * soc_dapm_connect_path() - Connects or disconnects a path
2257 * @path: The path to update
2258 * @connect: The new connect state of the path. True if the path is connected,
2259 * false if it is disconnected.
2260 * @reason: The reason why the path changed (for debugging only)
2262 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2263 bool connect, const char *reason)
2265 if (path->connect == connect)
2268 path->connect = connect;
2269 dapm_mark_dirty(path->source, reason);
2270 dapm_mark_dirty(path->sink, reason);
2271 dapm_path_invalidate(path);
2274 /* test and update the power status of a mux widget */
2275 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2276 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2278 struct snd_soc_dapm_path *path;
2282 lockdep_assert_held(&card->dapm_mutex);
2284 /* find dapm widget path assoc with kcontrol */
2285 dapm_kcontrol_for_each_path(path, kcontrol) {
2287 /* we now need to match the string in the enum to the path */
2288 if (e && !(strcmp(path->name, e->texts[mux])))
2293 soc_dapm_connect_path(path, connect, "mux update");
2297 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2302 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2303 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2304 struct snd_soc_dapm_update *update)
2306 struct snd_soc_card *card = dapm->card;
2309 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2310 card->update = update;
2311 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2312 card->update = NULL;
2313 mutex_unlock(&card->dapm_mutex);
2315 snd_soc_dpcm_runtime_update(card);
2318 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2320 /* test and update the power status of a mixer or switch widget */
2321 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2322 struct snd_kcontrol *kcontrol,
2323 int connect, int rconnect)
2325 struct snd_soc_dapm_path *path;
2328 lockdep_assert_held(&card->dapm_mutex);
2330 /* find dapm widget path assoc with kcontrol */
2331 dapm_kcontrol_for_each_path(path, kcontrol) {
2333 * Ideally this function should support any number of
2334 * paths and channels. But since kcontrols only come
2335 * in mono and stereo variants, we are limited to 2
2338 * The following code assumes for stereo controls the
2339 * first path (when 'found == 0') is the left channel,
2340 * and all remaining paths (when 'found == 1') are the
2343 * A stereo control is signified by a valid 'rconnect'
2344 * value, either 0 for unconnected, or >= 0 for connected.
2345 * This is chosen instead of using snd_soc_volsw_is_stereo,
2346 * so that the behavior of snd_soc_dapm_mixer_update_power
2347 * doesn't change even when the kcontrol passed in is
2350 * It passes 'connect' as the path connect status for
2351 * the left channel, and 'rconnect' for the right
2354 if (found && rconnect >= 0)
2355 soc_dapm_connect_path(path, rconnect, "mixer update");
2357 soc_dapm_connect_path(path, connect, "mixer update");
2362 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2367 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2368 struct snd_kcontrol *kcontrol, int connect,
2369 struct snd_soc_dapm_update *update)
2371 struct snd_soc_card *card = dapm->card;
2374 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2375 card->update = update;
2376 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2377 card->update = NULL;
2378 mutex_unlock(&card->dapm_mutex);
2380 snd_soc_dpcm_runtime_update(card);
2383 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2385 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2388 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2389 struct snd_soc_dapm_widget *w;
2391 char *state = "not set";
2393 /* card won't be set for the dummy component, as a spot fix
2394 * we're checking for that case specifically here but in future
2395 * we will ensure that the dummy component looks like others.
2400 for_each_card_widgets(cmpnt->card, w) {
2401 if (w->dapm != dapm)
2404 /* only display widgets that burn power */
2406 case snd_soc_dapm_hp:
2407 case snd_soc_dapm_mic:
2408 case snd_soc_dapm_spk:
2409 case snd_soc_dapm_line:
2410 case snd_soc_dapm_micbias:
2411 case snd_soc_dapm_dac:
2412 case snd_soc_dapm_adc:
2413 case snd_soc_dapm_pga:
2414 case snd_soc_dapm_effect:
2415 case snd_soc_dapm_out_drv:
2416 case snd_soc_dapm_mixer:
2417 case snd_soc_dapm_mixer_named_ctl:
2418 case snd_soc_dapm_supply:
2419 case snd_soc_dapm_regulator_supply:
2420 case snd_soc_dapm_pinctrl:
2421 case snd_soc_dapm_clock_supply:
2423 count += sprintf(buf + count, "%s: %s\n",
2424 w->name, w->power ? "On":"Off");
2431 switch (snd_soc_dapm_get_bias_level(dapm)) {
2432 case SND_SOC_BIAS_ON:
2435 case SND_SOC_BIAS_PREPARE:
2438 case SND_SOC_BIAS_STANDBY:
2441 case SND_SOC_BIAS_OFF:
2445 count += sprintf(buf + count, "PM State: %s\n", state);
2450 /* show dapm widget status in sys fs */
2451 static ssize_t dapm_widget_show(struct device *dev,
2452 struct device_attribute *attr, char *buf)
2454 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2455 struct snd_soc_dai *codec_dai;
2458 mutex_lock(&rtd->card->dapm_mutex);
2460 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2461 struct snd_soc_component *cmpnt = codec_dai->component;
2463 count += dapm_widget_show_component(cmpnt, buf + count);
2466 mutex_unlock(&rtd->card->dapm_mutex);
2471 static DEVICE_ATTR_RO(dapm_widget);
2473 struct attribute *soc_dapm_dev_attrs[] = {
2474 &dev_attr_dapm_widget.attr,
2478 static void dapm_free_path(struct snd_soc_dapm_path *path)
2480 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2481 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2482 list_del(&path->list_kcontrol);
2483 list_del(&path->list);
2488 * snd_soc_dapm_free_widget - Free specified widget
2489 * @w: widget to free
2491 * Removes widget from all paths and frees memory occupied by it.
2493 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2495 struct snd_soc_dapm_path *p, *next_p;
2496 enum snd_soc_dapm_direction dir;
2499 list_del(&w->dirty);
2501 * remove source and sink paths associated to this widget.
2502 * While removing the path, remove reference to it from both
2503 * source and sink widgets so that path is removed only once.
2505 snd_soc_dapm_for_each_direction(dir) {
2506 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2510 kfree(w->kcontrols);
2511 kfree_const(w->name);
2512 kfree_const(w->sname);
2515 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2517 void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
2519 dapm->path_sink_cache.widget = NULL;
2520 dapm->path_source_cache.widget = NULL;
2523 /* free all dapm widgets and resources */
2524 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2526 struct snd_soc_dapm_widget *w, *next_w;
2528 for_each_card_widgets_safe(dapm->card, w, next_w) {
2529 if (w->dapm != dapm)
2531 snd_soc_dapm_free_widget(w);
2533 snd_soc_dapm_reset_cache(dapm);
2536 static struct snd_soc_dapm_widget *dapm_find_widget(
2537 struct snd_soc_dapm_context *dapm, const char *pin,
2538 bool search_other_contexts)
2540 struct snd_soc_dapm_widget *w;
2541 struct snd_soc_dapm_widget *fallback = NULL;
2542 char prefixed_pin[80];
2543 const char *pin_name;
2544 const char *prefix = soc_dapm_prefix(dapm);
2547 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2549 pin_name = prefixed_pin;
2554 for_each_card_widgets(dapm->card, w) {
2555 if (!strcmp(w->name, pin_name)) {
2556 if (w->dapm == dapm)
2563 if (search_other_contexts)
2570 * set the DAPM pin status:
2571 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2572 * error code; called from kcontrol put callback
2574 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2575 const char *pin, int status)
2577 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2580 dapm_assert_locked(dapm);
2583 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2587 if (w->connected != status) {
2588 dapm_mark_dirty(w, "pin configuration");
2589 dapm_widget_invalidate_input_paths(w);
2590 dapm_widget_invalidate_output_paths(w);
2594 w->connected = status;
2602 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2603 * called from several API functions below
2605 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2606 const char *pin, int status)
2608 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2610 return ret < 0 ? ret : 0;
2614 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2615 * @dapm: DAPM context
2617 * Walks all dapm audio paths and powers widgets according to their
2618 * stream or path usage.
2620 * Requires external locking.
2622 * Returns 0 for success.
2624 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2627 * Suppress early reports (eg, jacks syncing their state) to avoid
2628 * silly DAPM runs during card startup.
2630 if (!dapm->card || !dapm->card->instantiated)
2633 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2635 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2638 * snd_soc_dapm_sync - scan and power dapm paths
2639 * @dapm: DAPM context
2641 * Walks all dapm audio paths and powers widgets according to their
2642 * stream or path usage.
2644 * Returns 0 for success.
2646 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2650 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2651 ret = snd_soc_dapm_sync_unlocked(dapm);
2652 mutex_unlock(&dapm->card->dapm_mutex);
2655 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2657 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2658 struct snd_soc_dapm_widget *w,
2662 case snd_soc_dapm_aif_out:
2663 case snd_soc_dapm_aif_in:
2669 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2670 w->channel < channels ? "Connecting" : "Disconnecting",
2671 p->source->name, p->sink->name);
2673 if (w->channel < channels)
2674 soc_dapm_connect_path(p, true, "dai update");
2676 soc_dapm_connect_path(p, false, "dai update");
2681 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2682 struct snd_pcm_hw_params *params,
2683 struct snd_soc_dai *dai)
2685 int dir = substream->stream;
2686 int channels = params_channels(params);
2687 struct snd_soc_dapm_path *p;
2688 struct snd_soc_dapm_widget *w;
2691 w = snd_soc_dai_get_widget(dai, dir);
2696 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2697 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2699 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2700 ret = dapm_update_dai_chan(p, p->sink, channels);
2705 snd_soc_dapm_widget_for_each_source_path(w, p) {
2706 ret = dapm_update_dai_chan(p, p->source, channels);
2714 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2715 struct snd_pcm_hw_params *params,
2716 struct snd_soc_dai *dai)
2718 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2721 mutex_lock_nested(&rtd->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2722 ret = dapm_update_dai_unlocked(substream, params, dai);
2723 mutex_unlock(&rtd->card->dapm_mutex);
2727 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2730 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2731 * @w: The widget for which to update the flags
2733 * Some widgets have a dynamic category which depends on which neighbors they
2734 * are connected to. This function update the category for these widgets.
2736 * This function must be called whenever a path is added or removed to a widget.
2738 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2740 enum snd_soc_dapm_direction dir;
2741 struct snd_soc_dapm_path *p;
2745 case snd_soc_dapm_input:
2746 /* On a fully routed card an input is never a source */
2747 if (w->dapm->card->fully_routed)
2749 ep = SND_SOC_DAPM_EP_SOURCE;
2750 snd_soc_dapm_widget_for_each_source_path(w, p) {
2751 if (p->source->id == snd_soc_dapm_micbias ||
2752 p->source->id == snd_soc_dapm_mic ||
2753 p->source->id == snd_soc_dapm_line ||
2754 p->source->id == snd_soc_dapm_output) {
2760 case snd_soc_dapm_output:
2761 /* On a fully routed card a output is never a sink */
2762 if (w->dapm->card->fully_routed)
2764 ep = SND_SOC_DAPM_EP_SINK;
2765 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2766 if (p->sink->id == snd_soc_dapm_spk ||
2767 p->sink->id == snd_soc_dapm_hp ||
2768 p->sink->id == snd_soc_dapm_line ||
2769 p->sink->id == snd_soc_dapm_input) {
2775 case snd_soc_dapm_line:
2777 snd_soc_dapm_for_each_direction(dir) {
2778 if (!list_empty(&w->edges[dir]))
2779 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2789 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2790 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2791 const char *control)
2793 bool dynamic_source = false;
2794 bool dynamic_sink = false;
2799 switch (source->id) {
2800 case snd_soc_dapm_demux:
2801 dynamic_source = true;
2808 case snd_soc_dapm_mux:
2809 case snd_soc_dapm_switch:
2810 case snd_soc_dapm_mixer:
2811 case snd_soc_dapm_mixer_named_ctl:
2812 dynamic_sink = true;
2818 if (dynamic_source && dynamic_sink) {
2820 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2821 source->name, control, sink->name);
2823 } else if (!dynamic_source && !dynamic_sink) {
2825 "Control not supported for path %s -> [%s] -> %s\n",
2826 source->name, control, sink->name);
2833 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2834 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2835 const char *control,
2836 int (*connected)(struct snd_soc_dapm_widget *source,
2837 struct snd_soc_dapm_widget *sink))
2839 struct snd_soc_dapm_widget *widgets[2];
2840 enum snd_soc_dapm_direction dir;
2841 struct snd_soc_dapm_path *path;
2844 if (wsink->is_supply && !wsource->is_supply) {
2846 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2847 wsource->name, wsink->name);
2851 if (connected && !wsource->is_supply) {
2853 "connected() callback only supported for supply widgets (%s -> %s)\n",
2854 wsource->name, wsink->name);
2858 if (wsource->is_supply && control) {
2860 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2861 wsource->name, control, wsink->name);
2865 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2869 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2873 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2874 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2875 widgets[SND_SOC_DAPM_DIR_IN] = wsource;
2876 widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
2878 path->connected = connected;
2879 INIT_LIST_HEAD(&path->list);
2880 INIT_LIST_HEAD(&path->list_kcontrol);
2882 if (wsource->is_supply || wsink->is_supply)
2883 path->is_supply = 1;
2885 /* connect static paths */
2886 if (control == NULL) {
2889 switch (wsource->id) {
2890 case snd_soc_dapm_demux:
2891 ret = dapm_connect_mux(dapm, path, control, wsource);
2899 switch (wsink->id) {
2900 case snd_soc_dapm_mux:
2901 ret = dapm_connect_mux(dapm, path, control, wsink);
2905 case snd_soc_dapm_switch:
2906 case snd_soc_dapm_mixer:
2907 case snd_soc_dapm_mixer_named_ctl:
2908 ret = dapm_connect_mixer(dapm, path, control);
2917 list_add(&path->list, &dapm->card->paths);
2918 snd_soc_dapm_for_each_direction(dir)
2919 list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
2921 snd_soc_dapm_for_each_direction(dir) {
2922 dapm_update_widget_flags(widgets[dir]);
2923 dapm_mark_dirty(widgets[dir], "Route added");
2926 if (dapm->card->instantiated && path->connect)
2927 dapm_path_invalidate(path);
2935 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2936 const struct snd_soc_dapm_route *route)
2938 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2939 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2942 char prefixed_sink[80];
2943 char prefixed_source[80];
2945 unsigned int sink_ref = 0;
2946 unsigned int source_ref = 0;
2949 prefix = soc_dapm_prefix(dapm);
2951 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2952 prefix, route->sink);
2953 sink = prefixed_sink;
2954 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2955 prefix, route->source);
2956 source = prefixed_source;
2959 source = route->source;
2962 wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
2963 wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
2965 if (wsink && wsource)
2969 * find src and dest widgets over all widgets but favor a widget from
2970 * current DAPM context
2972 for_each_card_widgets(dapm->card, w) {
2973 if (!wsink && !(strcmp(w->name, sink))) {
2975 if (w->dapm == dapm) {
2983 "ASoC: sink widget %s overwritten\n",
2987 if (!wsource && !(strcmp(w->name, source))) {
2989 if (w->dapm == dapm) {
2997 "ASoC: source widget %s overwritten\n",
3001 /* use widget from another DAPM context if not found from this */
3007 if (wsource == NULL) {
3008 dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
3012 if (wsink == NULL) {
3013 dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
3019 dapm_wcache_update(&dapm->path_sink_cache, wsink);
3020 dapm_wcache_update(&dapm->path_source_cache, wsource);
3022 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3029 dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
3030 source, route->control, sink);
3034 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3035 const struct snd_soc_dapm_route *route)
3037 struct snd_soc_dapm_path *path, *p;
3040 char prefixed_sink[80];
3041 char prefixed_source[80];
3044 if (route->control) {
3046 "ASoC: Removal of routes with controls not supported\n");
3050 prefix = soc_dapm_prefix(dapm);
3052 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3053 prefix, route->sink);
3054 sink = prefixed_sink;
3055 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3056 prefix, route->source);
3057 source = prefixed_source;
3060 source = route->source;
3064 list_for_each_entry(p, &dapm->card->paths, list) {
3065 if (strcmp(p->source->name, source) != 0)
3067 if (strcmp(p->sink->name, sink) != 0)
3074 struct snd_soc_dapm_widget *wsource = path->source;
3075 struct snd_soc_dapm_widget *wsink = path->sink;
3077 dapm_mark_dirty(wsource, "Route removed");
3078 dapm_mark_dirty(wsink, "Route removed");
3080 dapm_path_invalidate(path);
3082 dapm_free_path(path);
3084 /* Update any path related flags */
3085 dapm_update_widget_flags(wsource);
3086 dapm_update_widget_flags(wsink);
3088 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3096 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3097 * @dapm: DAPM context
3098 * @route: audio routes
3099 * @num: number of routes
3101 * Connects 2 dapm widgets together via a named audio path. The sink is
3102 * the widget receiving the audio signal, whilst the source is the sender
3103 * of the audio signal.
3105 * Returns 0 for success else error. On error all resources can be freed
3106 * with a call to snd_soc_card_free().
3108 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3109 const struct snd_soc_dapm_route *route, int num)
3113 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3114 for (i = 0; i < num; i++) {
3115 int r = snd_soc_dapm_add_route(dapm, route);
3117 dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
3119 route->control ? route->control : "direct",
3125 mutex_unlock(&dapm->card->dapm_mutex);
3129 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3132 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3133 * @dapm: DAPM context
3134 * @route: audio routes
3135 * @num: number of routes
3137 * Removes routes from the DAPM context.
3139 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3140 const struct snd_soc_dapm_route *route, int num)
3144 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3145 for (i = 0; i < num; i++) {
3146 snd_soc_dapm_del_route(dapm, route);
3149 mutex_unlock(&dapm->card->dapm_mutex);
3153 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3155 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3156 const struct snd_soc_dapm_route *route)
3158 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3161 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3164 struct snd_soc_dapm_path *path;
3168 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3174 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3179 if (route->control || route->connected)
3180 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3181 route->source, route->sink);
3183 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3184 if (path->sink == sink) {
3191 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3192 route->source, route->sink);
3194 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3195 count, route->source, route->sink);
3201 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3202 * @dapm: DAPM context
3203 * @route: audio routes
3204 * @num: number of routes
3206 * Mark existing routes matching those specified in the passed array
3207 * as being weak, meaning that they are ignored for the purpose of
3208 * power decisions. The main intended use case is for sidetone paths
3209 * which couple audio between other independent paths if they are both
3210 * active in order to make the combination work better at the user
3211 * level but which aren't intended to be "used".
3213 * Note that CODEC drivers should not use this as sidetone type paths
3214 * can frequently also be used as bypass paths.
3216 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3217 const struct snd_soc_dapm_route *route, int num)
3222 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3223 for (i = 0; i < num; i++) {
3224 int err = snd_soc_dapm_weak_route(dapm, route);
3229 mutex_unlock(&dapm->card->dapm_mutex);
3233 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3236 * snd_soc_dapm_new_widgets - add new dapm widgets
3237 * @card: card to be checked for new dapm widgets
3239 * Checks the codec for any new dapm widgets and creates them if found.
3241 * Returns 0 for success.
3243 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3245 struct snd_soc_dapm_widget *w;
3248 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3250 for_each_card_widgets(card, w)
3255 if (w->num_kcontrols) {
3256 w->kcontrols = kcalloc(w->num_kcontrols,
3257 sizeof(struct snd_kcontrol *),
3259 if (!w->kcontrols) {
3260 mutex_unlock(&card->dapm_mutex);
3266 case snd_soc_dapm_switch:
3267 case snd_soc_dapm_mixer:
3268 case snd_soc_dapm_mixer_named_ctl:
3271 case snd_soc_dapm_mux:
3272 case snd_soc_dapm_demux:
3275 case snd_soc_dapm_pga:
3276 case snd_soc_dapm_effect:
3277 case snd_soc_dapm_out_drv:
3280 case snd_soc_dapm_dai_link:
3281 dapm_new_dai_link(w);
3287 /* Read the initial power state from the device */
3289 val = soc_dapm_read(w->dapm, w->reg);
3290 val = val >> w->shift;
3292 if (val == w->on_val)
3298 dapm_mark_dirty(w, "new widget");
3299 dapm_debugfs_add_widget(w);
3302 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3303 mutex_unlock(&card->dapm_mutex);
3306 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3309 * snd_soc_dapm_get_volsw - dapm mixer get callback
3310 * @kcontrol: mixer control
3311 * @ucontrol: control element information
3313 * Callback to get the value of a dapm mixer control.
3315 * Returns 0 for success.
3317 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3318 struct snd_ctl_elem_value *ucontrol)
3320 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3321 struct snd_soc_card *card = dapm->card;
3322 struct soc_mixer_control *mc =
3323 (struct soc_mixer_control *)kcontrol->private_value;
3325 unsigned int shift = mc->shift;
3327 unsigned int width = fls(max);
3328 unsigned int mask = (1 << fls(max)) - 1;
3329 unsigned int invert = mc->invert;
3330 unsigned int reg_val, val, rval = 0;
3332 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3333 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3334 reg_val = soc_dapm_read(dapm, reg);
3335 val = (reg_val >> shift) & mask;
3337 if (reg != mc->rreg)
3338 reg_val = soc_dapm_read(dapm, mc->rreg);
3340 if (snd_soc_volsw_is_stereo(mc))
3341 rval = (reg_val >> mc->rshift) & mask;
3343 reg_val = dapm_kcontrol_get_value(kcontrol);
3344 val = reg_val & mask;
3346 if (snd_soc_volsw_is_stereo(mc))
3347 rval = (reg_val >> width) & mask;
3349 mutex_unlock(&card->dapm_mutex);
3352 ucontrol->value.integer.value[0] = max - val;
3354 ucontrol->value.integer.value[0] = val;
3356 if (snd_soc_volsw_is_stereo(mc)) {
3358 ucontrol->value.integer.value[1] = max - rval;
3360 ucontrol->value.integer.value[1] = rval;
3365 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3368 * snd_soc_dapm_put_volsw - dapm mixer set callback
3369 * @kcontrol: mixer control
3370 * @ucontrol: control element information
3372 * Callback to set the value of a dapm mixer control.
3374 * Returns 0 for success.
3376 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3377 struct snd_ctl_elem_value *ucontrol)
3379 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3380 struct snd_soc_card *card = dapm->card;
3381 struct soc_mixer_control *mc =
3382 (struct soc_mixer_control *)kcontrol->private_value;
3384 unsigned int shift = mc->shift;
3386 unsigned int width = fls(max);
3387 unsigned int mask = (1 << width) - 1;
3388 unsigned int invert = mc->invert;
3389 unsigned int val, rval = 0;
3390 int connect, rconnect = -1, change, reg_change = 0;
3391 struct snd_soc_dapm_update update = {};
3394 val = (ucontrol->value.integer.value[0] & mask);
3400 if (snd_soc_volsw_is_stereo(mc)) {
3401 rval = (ucontrol->value.integer.value[1] & mask);
3407 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3409 /* This assumes field width < (bits in unsigned int / 2) */
3410 if (width > sizeof(unsigned int) * 8 / 2)
3412 "ASoC: control %s field width limit exceeded\n",
3414 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3416 if (reg != SND_SOC_NOPM) {
3418 rval = rval << mc->rshift;
3420 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3422 if (snd_soc_volsw_is_stereo(mc))
3423 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3428 if (change || reg_change) {
3430 if (snd_soc_volsw_is_stereo(mc)) {
3431 update.has_second_set = true;
3432 update.reg2 = mc->rreg;
3433 update.mask2 = mask << mc->rshift;
3436 update.kcontrol = kcontrol;
3438 update.mask = mask << shift;
3440 card->update = &update;
3442 change |= reg_change;
3444 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3447 card->update = NULL;
3450 mutex_unlock(&card->dapm_mutex);
3453 snd_soc_dpcm_runtime_update(card);
3457 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3460 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3461 * @kcontrol: mixer control
3462 * @ucontrol: control element information
3464 * Callback to get the value of a dapm enumerated double mixer control.
3466 * Returns 0 for success.
3468 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3469 struct snd_ctl_elem_value *ucontrol)
3471 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3472 struct snd_soc_card *card = dapm->card;
3473 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3474 unsigned int reg_val, val;
3476 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3477 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3478 reg_val = soc_dapm_read(dapm, e->reg);
3480 reg_val = dapm_kcontrol_get_value(kcontrol);
3482 mutex_unlock(&card->dapm_mutex);
3484 val = (reg_val >> e->shift_l) & e->mask;
3485 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3486 if (e->shift_l != e->shift_r) {
3487 val = (reg_val >> e->shift_r) & e->mask;
3488 val = snd_soc_enum_val_to_item(e, val);
3489 ucontrol->value.enumerated.item[1] = val;
3494 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3497 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3498 * @kcontrol: mixer control
3499 * @ucontrol: control element information
3501 * Callback to set the value of a dapm enumerated double mixer control.
3503 * Returns 0 for success.
3505 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3506 struct snd_ctl_elem_value *ucontrol)
3508 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3509 struct snd_soc_card *card = dapm->card;
3510 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3511 unsigned int *item = ucontrol->value.enumerated.item;
3512 unsigned int val, change, reg_change = 0;
3514 struct snd_soc_dapm_update update = {};
3517 if (item[0] >= e->items)
3520 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3521 mask = e->mask << e->shift_l;
3522 if (e->shift_l != e->shift_r) {
3523 if (item[1] > e->items)
3525 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3526 mask |= e->mask << e->shift_r;
3529 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3531 change = dapm_kcontrol_set_value(kcontrol, val);
3533 if (e->reg != SND_SOC_NOPM)
3534 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3536 if (change || reg_change) {
3538 update.kcontrol = kcontrol;
3539 update.reg = e->reg;
3542 card->update = &update;
3544 change |= reg_change;
3546 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3548 card->update = NULL;
3551 mutex_unlock(&card->dapm_mutex);
3554 snd_soc_dpcm_runtime_update(card);
3558 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3561 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3563 * @kcontrol: mixer control
3564 * @uinfo: control element information
3566 * Callback to provide information about a pin switch control.
3568 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3569 struct snd_ctl_elem_info *uinfo)
3571 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3573 uinfo->value.integer.min = 0;
3574 uinfo->value.integer.max = 1;
3578 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3581 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3583 * @kcontrol: mixer control
3586 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3587 struct snd_ctl_elem_value *ucontrol)
3589 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3590 const char *pin = (const char *)kcontrol->private_value;
3592 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3594 ucontrol->value.integer.value[0] =
3595 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3597 mutex_unlock(&card->dapm_mutex);
3601 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3604 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3606 * @kcontrol: mixer control
3609 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3610 struct snd_ctl_elem_value *ucontrol)
3612 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3613 const char *pin = (const char *)kcontrol->private_value;
3616 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3617 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3618 !!ucontrol->value.integer.value[0]);
3619 mutex_unlock(&card->dapm_mutex);
3621 snd_soc_dapm_sync(&card->dapm);
3624 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3626 struct snd_soc_dapm_widget *
3627 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3628 const struct snd_soc_dapm_widget *widget)
3630 enum snd_soc_dapm_direction dir;
3631 struct snd_soc_dapm_widget *w;
3635 if ((w = dapm_cnew_widget(widget)) == NULL)
3636 return ERR_PTR(-ENOMEM);
3639 case snd_soc_dapm_regulator_supply:
3640 w->regulator = devm_regulator_get(dapm->dev, w->name);
3641 if (IS_ERR(w->regulator)) {
3642 ret = PTR_ERR(w->regulator);
3643 goto request_failed;
3646 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3647 ret = regulator_allow_bypass(w->regulator, true);
3650 "ASoC: Failed to bypass %s: %d\n",
3654 case snd_soc_dapm_pinctrl:
3655 w->pinctrl = devm_pinctrl_get(dapm->dev);
3656 if (IS_ERR(w->pinctrl)) {
3657 ret = PTR_ERR(w->pinctrl);
3658 goto request_failed;
3661 /* set to sleep_state when initializing */
3662 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3664 case snd_soc_dapm_clock_supply:
3665 w->clk = devm_clk_get(dapm->dev, w->name);
3666 if (IS_ERR(w->clk)) {
3667 ret = PTR_ERR(w->clk);
3668 goto request_failed;
3675 prefix = soc_dapm_prefix(dapm);
3677 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3679 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3680 if (w->name == NULL) {
3681 kfree_const(w->sname);
3683 return ERR_PTR(-ENOMEM);
3687 case snd_soc_dapm_mic:
3688 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3689 w->power_check = dapm_generic_check_power;
3691 case snd_soc_dapm_input:
3692 if (!dapm->card->fully_routed)
3693 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3694 w->power_check = dapm_generic_check_power;
3696 case snd_soc_dapm_spk:
3697 case snd_soc_dapm_hp:
3698 w->is_ep = SND_SOC_DAPM_EP_SINK;
3699 w->power_check = dapm_generic_check_power;
3701 case snd_soc_dapm_output:
3702 if (!dapm->card->fully_routed)
3703 w->is_ep = SND_SOC_DAPM_EP_SINK;
3704 w->power_check = dapm_generic_check_power;
3706 case snd_soc_dapm_vmid:
3707 case snd_soc_dapm_siggen:
3708 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3709 w->power_check = dapm_always_on_check_power;
3711 case snd_soc_dapm_sink:
3712 w->is_ep = SND_SOC_DAPM_EP_SINK;
3713 w->power_check = dapm_always_on_check_power;
3716 case snd_soc_dapm_mux:
3717 case snd_soc_dapm_demux:
3718 case snd_soc_dapm_switch:
3719 case snd_soc_dapm_mixer:
3720 case snd_soc_dapm_mixer_named_ctl:
3721 case snd_soc_dapm_adc:
3722 case snd_soc_dapm_aif_out:
3723 case snd_soc_dapm_dac:
3724 case snd_soc_dapm_aif_in:
3725 case snd_soc_dapm_pga:
3726 case snd_soc_dapm_buffer:
3727 case snd_soc_dapm_scheduler:
3728 case snd_soc_dapm_effect:
3729 case snd_soc_dapm_src:
3730 case snd_soc_dapm_asrc:
3731 case snd_soc_dapm_encoder:
3732 case snd_soc_dapm_decoder:
3733 case snd_soc_dapm_out_drv:
3734 case snd_soc_dapm_micbias:
3735 case snd_soc_dapm_line:
3736 case snd_soc_dapm_dai_link:
3737 case snd_soc_dapm_dai_out:
3738 case snd_soc_dapm_dai_in:
3739 w->power_check = dapm_generic_check_power;
3741 case snd_soc_dapm_supply:
3742 case snd_soc_dapm_regulator_supply:
3743 case snd_soc_dapm_pinctrl:
3744 case snd_soc_dapm_clock_supply:
3745 case snd_soc_dapm_kcontrol:
3747 w->power_check = dapm_supply_check_power;
3750 w->power_check = dapm_always_on_check_power;
3755 INIT_LIST_HEAD(&w->list);
3756 INIT_LIST_HEAD(&w->dirty);
3757 /* see for_each_card_widgets */
3758 list_add_tail(&w->list, &dapm->card->widgets);
3760 snd_soc_dapm_for_each_direction(dir) {
3761 INIT_LIST_HEAD(&w->edges[dir]);
3762 w->endpoints[dir] = -1;
3765 /* machine layer sets up unconnected pins and insertions */
3770 if (ret != -EPROBE_DEFER)
3771 dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3774 kfree_const(w->sname);
3776 return ERR_PTR(ret);
3780 * snd_soc_dapm_new_control - create new dapm control
3781 * @dapm: DAPM context
3782 * @widget: widget template
3784 * Creates new DAPM control based upon a template.
3786 * Returns a widget pointer on success or an error pointer on failure
3788 struct snd_soc_dapm_widget *
3789 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3790 const struct snd_soc_dapm_widget *widget)
3792 struct snd_soc_dapm_widget *w;
3794 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3795 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3796 mutex_unlock(&dapm->card->dapm_mutex);
3800 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3803 * snd_soc_dapm_new_controls - create new dapm controls
3804 * @dapm: DAPM context
3805 * @widget: widget array
3806 * @num: number of widgets
3808 * Creates new DAPM controls based upon the templates.
3810 * Returns 0 for success else error.
3812 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3813 const struct snd_soc_dapm_widget *widget,
3819 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3820 for (i = 0; i < num; i++) {
3821 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3828 mutex_unlock(&dapm->card->dapm_mutex);
3831 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3834 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3835 struct snd_pcm_substream *substream)
3837 struct snd_soc_dapm_path *path;
3838 struct snd_soc_dai *source, *sink;
3839 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3840 struct snd_pcm_hw_params *params = NULL;
3841 const struct snd_soc_pcm_stream *config = NULL;
3842 struct snd_pcm_runtime *runtime = NULL;
3846 params = kzalloc(sizeof(*params), GFP_KERNEL);
3850 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3856 substream->runtime = runtime;
3858 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3859 snd_soc_dapm_widget_for_each_source_path(w, path) {
3860 source = path->source->priv;
3862 ret = snd_soc_dai_startup(source, substream);
3866 snd_soc_dai_activate(source, substream->stream);
3869 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3870 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3871 sink = path->sink->priv;
3873 ret = snd_soc_dai_startup(sink, substream);
3877 snd_soc_dai_activate(sink, substream->stream);
3880 substream->hw_opened = 1;
3883 * Note: getting the config after .startup() gives a chance to
3884 * either party on the link to alter the configuration if
3887 config = rtd->dai_link->params + rtd->params_select;
3888 if (WARN_ON(!config)) {
3889 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3894 /* Be a little careful as we don't want to overflow the mask array */
3895 if (config->formats) {
3896 fmt = ffs(config->formats) - 1;
3898 dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3905 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3906 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3908 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3910 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3911 = config->channels_min;
3912 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3913 = config->channels_max;
3915 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3916 snd_soc_dapm_widget_for_each_source_path(w, path) {
3917 source = path->source->priv;
3919 ret = snd_soc_dai_hw_params(source, substream, params);
3923 dapm_update_dai_unlocked(substream, params, source);
3926 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3927 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3928 sink = path->sink->priv;
3930 ret = snd_soc_dai_hw_params(sink, substream, params);
3934 dapm_update_dai_unlocked(substream, params, sink);
3937 runtime->format = params_format(params);
3938 runtime->subformat = params_subformat(params);
3939 runtime->channels = params_channels(params);
3940 runtime->rate = params_rate(params);
3947 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3948 struct snd_kcontrol *kcontrol, int event)
3950 struct snd_soc_dapm_path *path;
3951 struct snd_soc_dai *source, *sink;
3952 struct snd_pcm_substream *substream = w->priv;
3953 int ret = 0, saved_stream = substream->stream;
3955 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3956 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3960 case SND_SOC_DAPM_PRE_PMU:
3961 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3967 case SND_SOC_DAPM_POST_PMU:
3968 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3969 sink = path->sink->priv;
3971 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3976 case SND_SOC_DAPM_PRE_PMD:
3977 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3978 sink = path->sink->priv;
3980 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3984 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3985 snd_soc_dapm_widget_for_each_source_path(w, path) {
3986 source = path->source->priv;
3987 snd_soc_dai_hw_free(source, substream, 0);
3990 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3991 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3992 sink = path->sink->priv;
3993 snd_soc_dai_hw_free(sink, substream, 0);
3996 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3997 snd_soc_dapm_widget_for_each_source_path(w, path) {
3998 source = path->source->priv;
3999 snd_soc_dai_deactivate(source, substream->stream);
4000 snd_soc_dai_shutdown(source, substream, 0);
4003 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
4004 snd_soc_dapm_widget_for_each_sink_path(w, path) {
4005 sink = path->sink->priv;
4006 snd_soc_dai_deactivate(sink, substream->stream);
4007 snd_soc_dai_shutdown(sink, substream, 0);
4011 case SND_SOC_DAPM_POST_PMD:
4012 kfree(substream->runtime);
4016 WARN(1, "Unknown event %d\n", event);
4021 /* Restore the substream direction */
4022 substream->stream = saved_stream;
4026 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4027 struct snd_ctl_elem_value *ucontrol)
4029 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4030 struct snd_soc_pcm_runtime *rtd = w->priv;
4032 ucontrol->value.enumerated.item[0] = rtd->params_select;
4037 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4038 struct snd_ctl_elem_value *ucontrol)
4040 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4041 struct snd_soc_pcm_runtime *rtd = w->priv;
4043 /* Can't change the config when widget is already powered */
4047 if (ucontrol->value.enumerated.item[0] == rtd->params_select)
4050 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
4053 rtd->params_select = ucontrol->value.enumerated.item[0];
4059 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4060 unsigned long *private_value,
4062 const char **w_param_text)
4066 devm_kfree(card->dev, (void *)*private_value);
4071 for (count = 0 ; count < num_params; count++)
4072 devm_kfree(card->dev, (void *)w_param_text[count]);
4073 devm_kfree(card->dev, w_param_text);
4076 static struct snd_kcontrol_new *
4077 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4079 const struct snd_soc_pcm_stream *params,
4080 int num_params, const char **w_param_text,
4081 unsigned long *private_value)
4083 struct soc_enum w_param_enum[] = {
4084 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4086 struct snd_kcontrol_new kcontrol_dai_link[] = {
4087 SOC_ENUM_EXT(NULL, w_param_enum[0],
4088 snd_soc_dapm_dai_link_get,
4089 snd_soc_dapm_dai_link_put),
4091 struct snd_kcontrol_new *kcontrol_news;
4092 const struct snd_soc_pcm_stream *config = params;
4095 for (count = 0 ; count < num_params; count++) {
4096 if (!config->stream_name) {
4097 dev_warn(card->dapm.dev,
4098 "ASoC: anonymous config %d for dai link %s\n",
4100 w_param_text[count] =
4101 devm_kasprintf(card->dev, GFP_KERNEL,
4102 "Anonymous Configuration %d",
4105 w_param_text[count] = devm_kmemdup(card->dev,
4106 config->stream_name,
4107 strlen(config->stream_name) + 1,
4110 if (!w_param_text[count])
4111 goto outfree_w_param;
4115 w_param_enum[0].items = num_params;
4116 w_param_enum[0].texts = w_param_text;
4119 (unsigned long) devm_kmemdup(card->dev,
4120 (void *)(kcontrol_dai_link[0].private_value),
4121 sizeof(struct soc_enum), GFP_KERNEL);
4122 if (!*private_value) {
4123 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4125 goto outfree_w_param;
4127 kcontrol_dai_link[0].private_value = *private_value;
4128 /* duplicate kcontrol_dai_link on heap so that memory persists */
4129 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4130 sizeof(struct snd_kcontrol_new),
4132 if (!kcontrol_news) {
4133 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4135 goto outfree_w_param;
4137 return kcontrol_news;
4140 snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
4144 static struct snd_soc_dapm_widget *
4145 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4146 struct snd_pcm_substream *substream,
4149 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4150 struct snd_soc_dapm_widget template;
4151 struct snd_soc_dapm_widget *w;
4152 const char **w_param_text;
4153 unsigned long private_value = 0;
4157 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4158 rtd->dai_link->name, id);
4160 return ERR_PTR(-ENOMEM);
4162 memset(&template, 0, sizeof(template));
4163 template.reg = SND_SOC_NOPM;
4164 template.id = snd_soc_dapm_dai_link;
4165 template.name = link_name;
4166 template.event = snd_soc_dai_link_event;
4167 template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
4168 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD;
4169 template.kcontrol_news = NULL;
4171 /* allocate memory for control, only in case of multiple configs */
4172 if (rtd->dai_link->num_params > 1) {
4173 w_param_text = devm_kcalloc(card->dev,
4174 rtd->dai_link->num_params,
4175 sizeof(char *), GFP_KERNEL);
4176 if (!w_param_text) {
4181 template.num_kcontrols = 1;
4182 template.kcontrol_news =
4183 snd_soc_dapm_alloc_kcontrol(card,
4185 rtd->dai_link->params,
4186 rtd->dai_link->num_params,
4187 w_param_text, &private_value);
4188 if (!template.kcontrol_news) {
4193 w_param_text = NULL;
4195 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4197 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4200 dev_err(rtd->dev, "ASoC: Failed to create %s widget: %d\n",
4202 goto outfree_kcontrol_news;
4205 w->priv = substream;
4209 outfree_kcontrol_news:
4210 devm_kfree(card->dev, (void *)template.kcontrol_news);
4211 snd_soc_dapm_free_kcontrol(card, &private_value,
4212 rtd->dai_link->num_params, w_param_text);
4214 devm_kfree(card->dev, link_name);
4215 return ERR_PTR(ret);
4219 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4220 * @dapm: DAPM context
4223 * Returns 0 on success, error code otherwise.
4225 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4226 struct snd_soc_dai *dai)
4228 struct snd_soc_dapm_widget template;
4229 struct snd_soc_dapm_widget *w;
4231 WARN_ON(dapm->dev != dai->dev);
4233 memset(&template, 0, sizeof(template));
4234 template.reg = SND_SOC_NOPM;
4236 if (dai->driver->playback.stream_name) {
4237 template.id = snd_soc_dapm_dai_in;
4238 template.name = dai->driver->playback.stream_name;
4239 template.sname = dai->driver->playback.stream_name;
4241 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4244 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4249 dai->playback_widget = w;
4252 if (dai->driver->capture.stream_name) {
4253 template.id = snd_soc_dapm_dai_out;
4254 template.name = dai->driver->capture.stream_name;
4255 template.sname = dai->driver->capture.stream_name;
4257 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4260 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4265 dai->capture_widget = w;
4270 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4272 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4274 struct snd_soc_dapm_widget *dai_w, *w;
4275 struct snd_soc_dapm_widget *src, *sink;
4276 struct snd_soc_dai *dai;
4278 /* For each DAI widget... */
4279 for_each_card_widgets(card, dai_w) {
4280 switch (dai_w->id) {
4281 case snd_soc_dapm_dai_in:
4282 case snd_soc_dapm_dai_out:
4288 /* let users know there is no DAI to link */
4290 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4297 /* ...find all widgets with the same stream and link them */
4298 for_each_card_widgets(card, w) {
4299 if (w->dapm != dai_w->dapm)
4303 case snd_soc_dapm_dai_in:
4304 case snd_soc_dapm_dai_out:
4310 if (!w->sname || !strstr(w->sname, dai_w->sname))
4313 if (dai_w->id == snd_soc_dapm_dai_in) {
4320 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4321 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4328 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4329 struct snd_soc_dai *src_dai,
4330 struct snd_soc_dapm_widget *src,
4331 struct snd_soc_dapm_widget *dai,
4332 struct snd_soc_dai *sink_dai,
4333 struct snd_soc_dapm_widget *sink)
4335 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4336 src_dai->component->name, src->name,
4337 sink_dai->component->name, sink->name);
4340 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4344 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4347 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4348 struct snd_soc_pcm_runtime *rtd,
4349 struct snd_soc_dai *codec_dai,
4350 struct snd_soc_dai *cpu_dai)
4352 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4353 struct snd_soc_dapm_widget *dai, *codec, *playback_cpu, *capture_cpu;
4354 struct snd_pcm_substream *substream;
4355 struct snd_pcm_str *streams = rtd->pcm->streams;
4357 if (dai_link->params) {
4358 playback_cpu = cpu_dai->capture_widget;
4359 capture_cpu = cpu_dai->playback_widget;
4361 playback_cpu = cpu_dai->playback_widget;
4362 capture_cpu = cpu_dai->capture_widget;
4365 /* connect BE DAI playback if widgets are valid */
4366 codec = codec_dai->playback_widget;
4368 if (playback_cpu && codec) {
4369 if (dai_link->params && !rtd->playback_widget) {
4370 substream = streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
4371 dai = snd_soc_dapm_new_dai(card, substream, "playback");
4374 rtd->playback_widget = dai;
4377 dapm_connect_dai_routes(&card->dapm, cpu_dai, playback_cpu,
4378 rtd->playback_widget,
4383 /* connect BE DAI capture if widgets are valid */
4384 codec = codec_dai->capture_widget;
4386 if (codec && capture_cpu) {
4387 if (dai_link->params && !rtd->capture_widget) {
4388 substream = streams[SNDRV_PCM_STREAM_CAPTURE].substream;
4389 dai = snd_soc_dapm_new_dai(card, substream, "capture");
4392 rtd->capture_widget = dai;
4395 dapm_connect_dai_routes(&card->dapm, codec_dai, codec,
4396 rtd->capture_widget,
4397 cpu_dai, capture_cpu);
4401 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4404 struct snd_soc_dapm_widget *w;
4406 w = snd_soc_dai_get_widget(dai, stream);
4411 dapm_mark_dirty(w, "stream event");
4413 if (w->id == snd_soc_dapm_dai_in) {
4414 ep = SND_SOC_DAPM_EP_SOURCE;
4415 dapm_widget_invalidate_input_paths(w);
4417 ep = SND_SOC_DAPM_EP_SINK;
4418 dapm_widget_invalidate_output_paths(w);
4422 case SND_SOC_DAPM_STREAM_START:
4426 case SND_SOC_DAPM_STREAM_STOP:
4430 case SND_SOC_DAPM_STREAM_SUSPEND:
4431 case SND_SOC_DAPM_STREAM_RESUME:
4432 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4433 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4439 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4441 struct snd_soc_pcm_runtime *rtd;
4442 struct snd_soc_dai *codec_dai;
4445 /* for each BE DAI link... */
4446 for_each_card_rtds(card, rtd) {
4448 * dynamic FE links have no fixed DAI mapping.
4449 * CODEC<->CODEC links have no direct connection.
4451 if (rtd->dai_link->dynamic)
4454 if (rtd->num_cpus == 1) {
4455 for_each_rtd_codec_dais(rtd, i, codec_dai)
4456 dapm_connect_dai_pair(card, rtd, codec_dai,
4457 asoc_rtd_to_cpu(rtd, 0));
4458 } else if (rtd->num_codecs == rtd->num_cpus) {
4459 for_each_rtd_codec_dais(rtd, i, codec_dai)
4460 dapm_connect_dai_pair(card, rtd, codec_dai,
4461 asoc_rtd_to_cpu(rtd, i));
4464 "N cpus to M codecs link is not supported yet\n");
4469 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4472 struct snd_soc_dai *dai;
4475 for_each_rtd_dais(rtd, i, dai)
4476 soc_dapm_dai_stream_event(dai, stream, event);
4478 dapm_power_widgets(rtd->card, event);
4482 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4483 * @rtd: PCM runtime data
4484 * @stream: stream name
4485 * @event: stream event
4487 * Sends a stream event to the dapm core. The core then makes any
4488 * necessary widget power changes.
4490 * Returns 0 for success else error.
4492 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4495 struct snd_soc_card *card = rtd->card;
4497 mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4498 soc_dapm_stream_event(rtd, stream, event);
4499 mutex_unlock(&card->dapm_mutex);
4502 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4504 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4505 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4506 /* powered down playback stream now */
4507 snd_soc_dapm_stream_event(rtd,
4508 SNDRV_PCM_STREAM_PLAYBACK,
4509 SND_SOC_DAPM_STREAM_STOP);
4511 /* start delayed pop wq here for playback streams */
4513 queue_delayed_work(system_power_efficient_wq,
4515 msecs_to_jiffies(rtd->pmdown_time));
4518 /* capture streams can be powered down now */
4519 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4520 SND_SOC_DAPM_STREAM_STOP);
4523 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4526 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4527 * @dapm: DAPM context
4530 * Enables input/output pin and its parents or children widgets iff there is
4531 * a valid audio route and active audio stream.
4533 * Requires external locking.
4535 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4536 * do any widget power switching.
4538 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4541 return snd_soc_dapm_set_pin(dapm, pin, 1);
4543 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4546 * snd_soc_dapm_enable_pin - enable pin.
4547 * @dapm: DAPM context
4550 * Enables input/output pin and its parents or children widgets iff there is
4551 * a valid audio route and active audio stream.
4553 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4554 * do any widget power switching.
4556 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4560 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4562 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4564 mutex_unlock(&dapm->card->dapm_mutex);
4568 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4571 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4572 * @dapm: DAPM context
4575 * Enables input/output pin regardless of any other state. This is
4576 * intended for use with microphone bias supplies used in microphone
4579 * Requires external locking.
4581 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4582 * do any widget power switching.
4584 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4587 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4590 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4594 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4595 if (!w->connected) {
4597 * w->force does not affect the number of input or output paths,
4598 * so we only have to recheck if w->connected is changed
4600 dapm_widget_invalidate_input_paths(w);
4601 dapm_widget_invalidate_output_paths(w);
4605 dapm_mark_dirty(w, "force enable");
4609 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4612 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4613 * @dapm: DAPM context
4616 * Enables input/output pin regardless of any other state. This is
4617 * intended for use with microphone bias supplies used in microphone
4620 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4621 * do any widget power switching.
4623 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4628 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4630 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4632 mutex_unlock(&dapm->card->dapm_mutex);
4636 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4639 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4640 * @dapm: DAPM context
4643 * Disables input/output pin and its parents or children widgets.
4645 * Requires external locking.
4647 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4648 * do any widget power switching.
4650 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4653 return snd_soc_dapm_set_pin(dapm, pin, 0);
4655 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4658 * snd_soc_dapm_disable_pin - disable pin.
4659 * @dapm: DAPM context
4662 * Disables input/output pin and its parents or children widgets.
4664 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4665 * do any widget power switching.
4667 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4672 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4674 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4676 mutex_unlock(&dapm->card->dapm_mutex);
4680 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4683 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4684 * @dapm: DAPM context
4687 * Marks the specified pin as being not connected, disabling it along
4688 * any parent or child widgets. At present this is identical to
4689 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4690 * additional things such as disabling controls which only affect
4691 * paths through the pin.
4693 * Requires external locking.
4695 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4696 * do any widget power switching.
4698 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4701 return snd_soc_dapm_set_pin(dapm, pin, 0);
4703 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4706 * snd_soc_dapm_nc_pin - permanently disable pin.
4707 * @dapm: DAPM context
4710 * Marks the specified pin as being not connected, disabling it along
4711 * any parent or child widgets. At present this is identical to
4712 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4713 * additional things such as disabling controls which only affect
4714 * paths through the pin.
4716 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4717 * do any widget power switching.
4719 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4723 mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
4725 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4727 mutex_unlock(&dapm->card->dapm_mutex);
4731 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4734 * snd_soc_dapm_get_pin_status - get audio pin status
4735 * @dapm: DAPM context
4736 * @pin: audio signal pin endpoint (or start point)
4738 * Get audio pin status - connected or disconnected.
4740 * Returns 1 for connected otherwise 0.
4742 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4745 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4748 return w->connected;
4752 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4755 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4756 * @dapm: DAPM context
4757 * @pin: audio signal pin endpoint (or start point)
4759 * Mark the given endpoint or pin as ignoring suspend. When the
4760 * system is disabled a path between two endpoints flagged as ignoring
4761 * suspend will not be disabled. The path must already be enabled via
4762 * normal means at suspend time, it will not be turned on if it was not
4765 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4768 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4771 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4775 w->ignore_suspend = 1;
4779 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4782 * snd_soc_dapm_free - free dapm resources
4783 * @dapm: DAPM context
4785 * Free all dapm widgets and resources.
4787 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4789 dapm_debugfs_cleanup(dapm);
4790 dapm_free_widgets(dapm);
4791 list_del(&dapm->list);
4793 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4795 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4796 struct snd_soc_card *card,
4797 struct snd_soc_component *component)
4800 dapm->component = component;
4801 dapm->bias_level = SND_SOC_BIAS_OFF;
4804 dapm->dev = component->dev;
4805 dapm->idle_bias_off = !component->driver->idle_bias_on;
4806 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4808 dapm->dev = card->dev;
4811 INIT_LIST_HEAD(&dapm->list);
4812 /* see for_each_card_dapms */
4813 list_add(&dapm->list, &card->dapm_list);
4815 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4817 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4819 struct snd_soc_card *card = dapm->card;
4820 struct snd_soc_dapm_widget *w;
4821 LIST_HEAD(down_list);
4824 mutex_lock(&card->dapm_mutex);
4826 for_each_card_widgets(dapm->card, w) {
4827 if (w->dapm != dapm)
4830 dapm_seq_insert(w, &down_list, false);
4836 /* If there were no widgets to power down we're already in
4840 if (dapm->bias_level == SND_SOC_BIAS_ON)
4841 snd_soc_dapm_set_bias_level(dapm,
4842 SND_SOC_BIAS_PREPARE);
4843 dapm_seq_run(card, &down_list, 0, false);
4844 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4845 snd_soc_dapm_set_bias_level(dapm,
4846 SND_SOC_BIAS_STANDBY);
4849 mutex_unlock(&card->dapm_mutex);
4853 * snd_soc_dapm_shutdown - callback for system shutdown
4855 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4857 struct snd_soc_dapm_context *dapm;
4859 for_each_card_dapms(card, dapm) {
4860 if (dapm != &card->dapm) {
4861 soc_dapm_shutdown_dapm(dapm);
4862 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4863 snd_soc_dapm_set_bias_level(dapm,
4868 soc_dapm_shutdown_dapm(&card->dapm);
4869 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4870 snd_soc_dapm_set_bias_level(&card->dapm,
4874 /* Module information */
4875 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4876 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4877 MODULE_LICENSE("GPL");