1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 // o Changes power status of internal codec blocks depending on the
10 // dynamic configuration of codec internal audio paths and active
12 // o Platform power domain - can support external components i.e. amps and
13 // mic/headphone insertion events.
14 // o Automatic Mic Bias support
15 // o Jack insertion power event initiation - e.g. hp insertion will enable
17 // o Delayed power down of audio subsystem to reduce pops between a quick
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/async.h>
23 #include <linux/delay.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/jiffies.h>
28 #include <linux/debugfs.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/initval.h>
40 #include <trace/events/asoc.h>
42 #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
44 #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
45 SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
47 #define snd_soc_dapm_for_each_direction(dir) \
48 for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
51 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
52 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
54 int (*connected)(struct snd_soc_dapm_widget *source,
55 struct snd_soc_dapm_widget *sink));
57 struct snd_soc_dapm_widget *
58 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
59 const struct snd_soc_dapm_widget *widget);
61 struct snd_soc_dapm_widget *
62 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
63 const struct snd_soc_dapm_widget *widget);
65 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg);
67 /* dapm power sequences - make this per codec in the future */
68 static int dapm_up_seq[] = {
69 [snd_soc_dapm_pre] = 1,
70 [snd_soc_dapm_regulator_supply] = 2,
71 [snd_soc_dapm_pinctrl] = 2,
72 [snd_soc_dapm_clock_supply] = 2,
73 [snd_soc_dapm_supply] = 3,
74 [snd_soc_dapm_dai_link] = 3,
75 [snd_soc_dapm_micbias] = 4,
76 [snd_soc_dapm_vmid] = 4,
77 [snd_soc_dapm_dai_in] = 5,
78 [snd_soc_dapm_dai_out] = 5,
79 [snd_soc_dapm_aif_in] = 5,
80 [snd_soc_dapm_aif_out] = 5,
81 [snd_soc_dapm_mic] = 6,
82 [snd_soc_dapm_siggen] = 6,
83 [snd_soc_dapm_input] = 6,
84 [snd_soc_dapm_output] = 6,
85 [snd_soc_dapm_mux] = 7,
86 [snd_soc_dapm_demux] = 7,
87 [snd_soc_dapm_dac] = 8,
88 [snd_soc_dapm_switch] = 9,
89 [snd_soc_dapm_mixer] = 9,
90 [snd_soc_dapm_mixer_named_ctl] = 9,
91 [snd_soc_dapm_pga] = 10,
92 [snd_soc_dapm_buffer] = 10,
93 [snd_soc_dapm_scheduler] = 10,
94 [snd_soc_dapm_effect] = 10,
95 [snd_soc_dapm_src] = 10,
96 [snd_soc_dapm_asrc] = 10,
97 [snd_soc_dapm_encoder] = 10,
98 [snd_soc_dapm_decoder] = 10,
99 [snd_soc_dapm_adc] = 11,
100 [snd_soc_dapm_out_drv] = 12,
101 [snd_soc_dapm_hp] = 12,
102 [snd_soc_dapm_line] = 12,
103 [snd_soc_dapm_sink] = 12,
104 [snd_soc_dapm_spk] = 13,
105 [snd_soc_dapm_kcontrol] = 14,
106 [snd_soc_dapm_post] = 15,
109 static int dapm_down_seq[] = {
110 [snd_soc_dapm_pre] = 1,
111 [snd_soc_dapm_kcontrol] = 2,
112 [snd_soc_dapm_adc] = 3,
113 [snd_soc_dapm_spk] = 4,
114 [snd_soc_dapm_hp] = 5,
115 [snd_soc_dapm_line] = 5,
116 [snd_soc_dapm_out_drv] = 5,
117 [snd_soc_dapm_sink] = 6,
118 [snd_soc_dapm_pga] = 6,
119 [snd_soc_dapm_buffer] = 6,
120 [snd_soc_dapm_scheduler] = 6,
121 [snd_soc_dapm_effect] = 6,
122 [snd_soc_dapm_src] = 6,
123 [snd_soc_dapm_asrc] = 6,
124 [snd_soc_dapm_encoder] = 6,
125 [snd_soc_dapm_decoder] = 6,
126 [snd_soc_dapm_switch] = 7,
127 [snd_soc_dapm_mixer_named_ctl] = 7,
128 [snd_soc_dapm_mixer] = 7,
129 [snd_soc_dapm_dac] = 8,
130 [snd_soc_dapm_mic] = 9,
131 [snd_soc_dapm_siggen] = 9,
132 [snd_soc_dapm_input] = 9,
133 [snd_soc_dapm_output] = 9,
134 [snd_soc_dapm_micbias] = 10,
135 [snd_soc_dapm_vmid] = 10,
136 [snd_soc_dapm_mux] = 11,
137 [snd_soc_dapm_demux] = 11,
138 [snd_soc_dapm_aif_in] = 12,
139 [snd_soc_dapm_aif_out] = 12,
140 [snd_soc_dapm_dai_in] = 12,
141 [snd_soc_dapm_dai_out] = 12,
142 [snd_soc_dapm_dai_link] = 13,
143 [snd_soc_dapm_supply] = 14,
144 [snd_soc_dapm_clock_supply] = 15,
145 [snd_soc_dapm_pinctrl] = 15,
146 [snd_soc_dapm_regulator_supply] = 15,
147 [snd_soc_dapm_post] = 16,
150 static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
152 if (snd_soc_card_is_instantiated(dapm->card))
153 snd_soc_dapm_mutex_assert_held(dapm);
156 static void pop_wait(u32 pop_time)
159 schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
163 static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
171 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
176 vsnprintf(buf, PAGE_SIZE, fmt, args);
177 dev_info(dev, "%s", buf);
183 static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
185 return !list_empty(&w->dirty);
188 static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
190 dapm_assert_locked(w->dapm);
192 if (!dapm_dirty_widget(w)) {
193 dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
195 list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
200 * Common implementation for dapm_widget_invalidate_input_paths() and
201 * dapm_widget_invalidate_output_paths(). The function is inlined since the
202 * combined size of the two specialized functions is only marginally larger then
203 * the size of the generic function and at the same time the fast path of the
204 * specialized functions is significantly smaller than the generic function.
206 static __always_inline void dapm_widget_invalidate_paths(
207 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
209 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
210 struct snd_soc_dapm_widget *node;
211 struct snd_soc_dapm_path *p;
214 dapm_assert_locked(w->dapm);
216 if (w->endpoints[dir] == -1)
219 list_add_tail(&w->work_list, &list);
220 w->endpoints[dir] = -1;
222 list_for_each_entry(w, &list, work_list) {
223 snd_soc_dapm_widget_for_each_path(w, dir, p) {
224 if (p->is_supply || p->weak || !p->connect)
226 node = p->node[rdir];
227 if (node->endpoints[dir] != -1) {
228 node->endpoints[dir] = -1;
229 list_add_tail(&node->work_list, &list);
236 * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
238 * @w: The widget for which to invalidate the cached number of input paths
240 * Resets the cached number of inputs for the specified widget and all widgets
241 * that can be reached via outcoming paths from the widget.
243 * This function must be called if the number of output paths for a widget might
244 * have changed. E.g. if the source state of a widget changes or a path is added
245 * or activated with the widget as the sink.
247 static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
249 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
253 * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
255 * @w: The widget for which to invalidate the cached number of output paths
257 * Resets the cached number of outputs for the specified widget and all widgets
258 * that can be reached via incoming paths from the widget.
260 * This function must be called if the number of output paths for a widget might
261 * have changed. E.g. if the sink state of a widget changes or a path is added
262 * or activated with the widget as the source.
264 static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
266 dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
270 * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
271 * for the widgets connected to a path
272 * @p: The path to invalidate
274 * Resets the cached number of inputs for the sink of the path and the cached
275 * number of outputs for the source of the path.
277 * This function must be called when a path is added, removed or the connected
280 static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
283 * Weak paths or supply paths do not influence the number of input or
284 * output paths of their neighbors.
286 if (p->weak || p->is_supply)
290 * The number of connected endpoints is the sum of the number of
291 * connected endpoints of all neighbors. If a node with 0 connected
292 * endpoints is either connected or disconnected that sum won't change,
293 * so there is no need to re-check the path.
295 if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
296 dapm_widget_invalidate_input_paths(p->sink);
297 if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
298 dapm_widget_invalidate_output_paths(p->source);
301 void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
303 struct snd_soc_dapm_widget *w;
305 snd_soc_dapm_mutex_lock_root(card);
307 for_each_card_widgets(card, w) {
309 dapm_mark_dirty(w, "Rechecking endpoints");
310 if (w->is_ep & SND_SOC_DAPM_EP_SINK)
311 dapm_widget_invalidate_output_paths(w);
312 if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
313 dapm_widget_invalidate_input_paths(w);
317 snd_soc_dapm_mutex_unlock(card);
319 EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
321 /* create a new dapm widget */
322 static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
323 const struct snd_soc_dapm_widget *_widget)
325 struct snd_soc_dapm_widget *w;
327 w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
332 * w->name is duplicated in caller, but w->sname isn't.
333 * Duplicate it here if defined
335 if (_widget->sname) {
336 w->sname = kstrdup_const(_widget->sname, GFP_KERNEL);
345 struct dapm_kcontrol_data {
347 struct snd_soc_dapm_widget *widget;
348 struct list_head paths;
349 struct snd_soc_dapm_widget_list *wlist;
352 static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
353 struct snd_kcontrol *kcontrol, const char *ctrl_name)
355 struct dapm_kcontrol_data *data;
356 struct soc_mixer_control *mc;
361 data = kzalloc(sizeof(*data), GFP_KERNEL);
365 INIT_LIST_HEAD(&data->paths);
367 switch (widget->id) {
368 case snd_soc_dapm_switch:
369 case snd_soc_dapm_mixer:
370 case snd_soc_dapm_mixer_named_ctl:
371 mc = (struct soc_mixer_control *)kcontrol->private_value;
373 if (mc->autodisable) {
374 struct snd_soc_dapm_widget template;
376 if (snd_soc_volsw_is_stereo(mc))
377 dev_warn(widget->dapm->dev,
378 "ASoC: Unsupported stereo autodisable control '%s'\n",
381 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
388 memset(&template, 0, sizeof(template));
389 template.reg = mc->reg;
390 template.mask = (1 << fls(mc->max)) - 1;
391 template.shift = mc->shift;
393 template.off_val = mc->max;
395 template.off_val = 0;
396 template.on_val = template.off_val;
397 template.id = snd_soc_dapm_kcontrol;
398 template.name = name;
400 data->value = template.on_val;
403 snd_soc_dapm_new_control_unlocked(widget->dapm,
406 if (IS_ERR(data->widget)) {
407 ret = PTR_ERR(data->widget);
412 case snd_soc_dapm_demux:
413 case snd_soc_dapm_mux:
414 e = (struct soc_enum *)kcontrol->private_value;
416 if (e->autodisable) {
417 struct snd_soc_dapm_widget template;
419 name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
426 memset(&template, 0, sizeof(template));
427 template.reg = e->reg;
428 template.mask = e->mask;
429 template.shift = e->shift_l;
430 template.off_val = snd_soc_enum_item_to_val(e, 0);
431 template.on_val = template.off_val;
432 template.id = snd_soc_dapm_kcontrol;
433 template.name = name;
435 data->value = template.on_val;
437 data->widget = snd_soc_dapm_new_control_unlocked(
438 widget->dapm, &template);
440 if (IS_ERR(data->widget)) {
441 ret = PTR_ERR(data->widget);
445 snd_soc_dapm_add_path(widget->dapm, data->widget,
447 } else if (e->reg != SND_SOC_NOPM) {
448 data->value = soc_dapm_read(widget->dapm, e->reg) &
449 (e->mask << e->shift_l);
456 kcontrol->private_data = data;
465 static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
467 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
469 list_del(&data->paths);
474 static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
475 const struct snd_kcontrol *kcontrol)
477 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
482 static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
483 struct snd_soc_dapm_widget *widget)
485 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
486 struct snd_soc_dapm_widget_list *new_wlist;
490 n = data->wlist->num_widgets + 1;
494 new_wlist = krealloc(data->wlist,
495 struct_size(new_wlist, widgets, n),
500 new_wlist->widgets[n - 1] = widget;
501 new_wlist->num_widgets = n;
503 data->wlist = new_wlist;
508 static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
509 struct snd_soc_dapm_path *path)
511 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
513 list_add_tail(&path->list_kcontrol, &data->paths);
516 static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
518 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
523 return data->widget->power;
526 static struct list_head *dapm_kcontrol_get_path_list(
527 const struct snd_kcontrol *kcontrol)
529 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
534 #define dapm_kcontrol_for_each_path(path, kcontrol) \
535 list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
538 unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
540 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
544 EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
546 static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
549 struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
551 if (data->value == value)
555 switch (dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->id) {
556 case snd_soc_dapm_switch:
557 case snd_soc_dapm_mixer:
558 case snd_soc_dapm_mixer_named_ctl:
559 data->widget->on_val = value & data->widget->mask;
561 case snd_soc_dapm_demux:
562 case snd_soc_dapm_mux:
563 data->widget->on_val = value >> data->widget->shift;
566 data->widget->on_val = value;
577 * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
579 * @kcontrol: The kcontrol
581 struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
582 struct snd_kcontrol *kcontrol)
584 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
586 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
589 * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
591 * @kcontrol: The kcontrol
593 * Note: This function must only be used on kcontrols that are known to have
594 * been registered for a CODEC. Otherwise the behaviour is undefined.
596 struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
597 struct snd_kcontrol *kcontrol)
599 return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
601 EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
603 static void dapm_reset(struct snd_soc_card *card)
605 struct snd_soc_dapm_widget *w;
607 snd_soc_dapm_mutex_assert_held(card);
609 memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
611 for_each_card_widgets(card, w) {
612 w->new_power = w->power;
613 w->power_checked = false;
617 static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
619 if (!dapm->component)
621 return dapm->component->name_prefix;
624 static unsigned int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg)
626 if (!dapm->component)
628 return snd_soc_component_read(dapm->component, reg);
631 static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
632 int reg, unsigned int mask, unsigned int value)
634 if (!dapm->component)
636 return snd_soc_component_update_bits(dapm->component, reg,
640 static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
641 int reg, unsigned int mask, unsigned int value)
643 if (!dapm->component)
645 return snd_soc_component_test_bits(dapm->component, reg, mask, value);
648 static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
651 snd_soc_component_async_complete(dapm->component);
654 static struct snd_soc_dapm_widget *
655 dapm_wcache_lookup(struct snd_soc_dapm_widget *w, const char *name)
658 struct list_head *wlist = &w->dapm->card->widgets;
662 list_for_each_entry_from(w, wlist, list) {
663 if (!strcmp(name, w->name))
675 * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
676 * @dapm: The DAPM context for which to set the level
677 * @level: The level to set
679 * Forces the DAPM bias level to a specific state. It will call the bias level
680 * callback of DAPM context with the specified level. This will even happen if
681 * the context is already at the same level. Furthermore it will not go through
682 * the normal bias level sequencing, meaning any intermediate states between the
683 * current and the target state will not be entered.
685 * Note that the change in bias level is only temporary and the next time
686 * snd_soc_dapm_sync() is called the state will be set to the level as
687 * determined by the DAPM core. The function is mainly intended to be used to
688 * used during probe or resume from suspend to power up the device so
689 * initialization can be done, before the DAPM core takes over.
691 int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
692 enum snd_soc_bias_level level)
697 ret = snd_soc_component_set_bias_level(dapm->component, level);
700 dapm->bias_level = level;
704 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
707 * snd_soc_dapm_set_bias_level - set the bias level for the system
708 * @dapm: DAPM context
709 * @level: level to configure
711 * Configure the bias (power) levels for the SoC audio device.
713 * Returns 0 for success else error.
715 static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
716 enum snd_soc_bias_level level)
718 struct snd_soc_card *card = dapm->card;
721 trace_snd_soc_bias_level_start(card, level);
723 ret = snd_soc_card_set_bias_level(card, dapm, level);
727 if (!card || dapm != &card->dapm)
728 ret = snd_soc_dapm_force_bias_level(dapm, level);
733 ret = snd_soc_card_set_bias_level_post(card, dapm, level);
735 trace_snd_soc_bias_level_done(card, level);
740 /* connect mux widget to its interconnecting audio paths */
741 static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
742 struct snd_soc_dapm_path *path, const char *control_name,
743 struct snd_soc_dapm_widget *w)
745 const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
746 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
750 if (e->reg != SND_SOC_NOPM) {
752 val = soc_dapm_read(dapm, e->reg);
753 val = (val >> e->shift_l) & e->mask;
754 item = snd_soc_enum_val_to_item(e, val);
756 /* since a virtual mux has no backing registers to
757 * decide which path to connect, it will try to match
758 * with the first enumeration. This is to ensure
759 * that the default mux choice (the first) will be
760 * correctly powered up during initialization.
765 i = match_string(e->texts, e->items, control_name);
769 path->name = e->texts[i];
770 path->connect = (i == item);
775 /* set up initial codec paths */
776 static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
779 struct soc_mixer_control *mc = (struct soc_mixer_control *)
780 p->sink->kcontrol_news[i].private_value;
781 unsigned int reg = mc->reg;
782 unsigned int invert = mc->invert;
784 if (reg != SND_SOC_NOPM) {
785 unsigned int shift = mc->shift;
786 unsigned int max = mc->max;
787 unsigned int mask = (1 << fls(max)) - 1;
788 unsigned int val = soc_dapm_read(p->sink->dapm, reg);
791 * The nth_path argument allows this function to know
792 * which path of a kcontrol it is setting the initial
793 * status for. Ideally this would support any number
794 * of paths and channels. But since kcontrols only come
795 * in mono and stereo variants, we are limited to 2
798 * The following code assumes for stereo controls the
799 * first path is the left channel, and all remaining
800 * paths are the right channel.
802 if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
804 val = soc_dapm_read(p->sink->dapm, mc->rreg);
805 val = (val >> mc->rshift) & mask;
807 val = (val >> shift) & mask;
813 /* since a virtual mixer has no backing registers to
814 * decide which path to connect, it will try to match
815 * with initial state. This is to ensure
816 * that the default mixer choice will be
817 * correctly powered up during initialization.
823 /* connect mixer widget to its interconnecting audio paths */
824 static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
825 struct snd_soc_dapm_path *path, const char *control_name)
829 /* search for mixer kcontrol */
830 for (i = 0; i < path->sink->num_kcontrols; i++) {
831 if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
832 path->name = path->sink->kcontrol_news[i].name;
833 dapm_set_mixer_path_status(path, i, nth_path++);
840 static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
841 struct snd_soc_dapm_widget *kcontrolw,
842 const struct snd_kcontrol_new *kcontrol_new,
843 struct snd_kcontrol **kcontrol)
845 struct snd_soc_dapm_widget *w;
850 for_each_card_widgets(dapm->card, w) {
851 if (w == kcontrolw || w->dapm != kcontrolw->dapm)
853 for (i = 0; i < w->num_kcontrols; i++) {
854 if (&w->kcontrol_news[i] == kcontrol_new) {
856 *kcontrol = w->kcontrols[i];
866 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
867 * create it. Either way, add the widget into the control's widget list
869 static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
872 struct snd_soc_dapm_context *dapm = w->dapm;
873 struct snd_card *card = dapm->card->snd_card;
877 struct snd_kcontrol *kcontrol;
878 bool wname_in_long_name, kcname_in_long_name;
879 char *long_name = NULL;
883 prefix = soc_dapm_prefix(dapm);
885 prefix_len = strlen(prefix) + 1;
889 shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
894 wname_in_long_name = false;
895 kcname_in_long_name = true;
898 case snd_soc_dapm_switch:
899 case snd_soc_dapm_mixer:
900 case snd_soc_dapm_pga:
901 case snd_soc_dapm_effect:
902 case snd_soc_dapm_out_drv:
903 wname_in_long_name = true;
904 kcname_in_long_name = true;
906 case snd_soc_dapm_mixer_named_ctl:
907 wname_in_long_name = false;
908 kcname_in_long_name = true;
910 case snd_soc_dapm_demux:
911 case snd_soc_dapm_mux:
912 wname_in_long_name = true;
913 kcname_in_long_name = false;
920 if (wname_in_long_name && kcname_in_long_name) {
922 * The control will get a prefix from the control
923 * creation process but we're also using the same
924 * prefix for widgets so cut the prefix off the
925 * front of the widget name.
927 long_name = kasprintf(GFP_KERNEL, "%s %s",
928 w->name + prefix_len,
929 w->kcontrol_news[kci].name);
930 if (long_name == NULL)
934 } else if (wname_in_long_name) {
936 name = w->name + prefix_len;
939 name = w->kcontrol_news[kci].name;
942 kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
949 kcontrol->private_free = dapm_kcontrol_free;
951 ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
953 snd_ctl_free_one(kcontrol);
957 ret = snd_ctl_add(card, kcontrol);
960 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
966 ret = dapm_kcontrol_add_widget(kcontrol, w);
968 w->kcontrols[kci] = kcontrol;
976 /* create new dapm mixer control */
977 static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
980 struct snd_soc_dapm_path *path;
981 struct dapm_kcontrol_data *data;
984 for (i = 0; i < w->num_kcontrols; i++) {
986 snd_soc_dapm_widget_for_each_source_path(w, path) {
987 /* mixer/mux paths name must match control name */
988 if (path->name != (char *)w->kcontrol_news[i].name)
991 if (!w->kcontrols[i]) {
992 ret = dapm_create_or_share_kcontrol(w, i);
997 dapm_kcontrol_add_path(w->kcontrols[i], path);
999 data = snd_kcontrol_chip(w->kcontrols[i]);
1001 snd_soc_dapm_add_path(data->widget->dapm,
1011 /* create new dapm mux control */
1012 static int dapm_new_mux(struct snd_soc_dapm_widget *w)
1014 struct snd_soc_dapm_context *dapm = w->dapm;
1015 enum snd_soc_dapm_direction dir;
1016 struct snd_soc_dapm_path *path;
1021 case snd_soc_dapm_mux:
1022 dir = SND_SOC_DAPM_DIR_OUT;
1025 case snd_soc_dapm_demux:
1026 dir = SND_SOC_DAPM_DIR_IN;
1033 if (w->num_kcontrols != 1) {
1035 "ASoC: %s %s has incorrect number of controls\n", type,
1040 if (list_empty(&w->edges[dir])) {
1041 dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
1045 ret = dapm_create_or_share_kcontrol(w, 0);
1049 snd_soc_dapm_widget_for_each_path(w, dir, path) {
1051 dapm_kcontrol_add_path(w->kcontrols[0], path);
1057 /* create new dapm volume control */
1058 static int dapm_new_pga(struct snd_soc_dapm_widget *w)
1062 for (i = 0; i < w->num_kcontrols; i++) {
1063 int ret = dapm_create_or_share_kcontrol(w, i);
1071 /* create new dapm dai link control */
1072 static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
1075 struct snd_soc_pcm_runtime *rtd = w->priv;
1077 /* create control for links with > 1 config */
1078 if (rtd->dai_link->num_c2c_params <= 1)
1082 for (i = 0; i < w->num_kcontrols; i++) {
1083 struct snd_soc_dapm_context *dapm = w->dapm;
1084 struct snd_card *card = dapm->card->snd_card;
1085 struct snd_kcontrol *kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
1087 int ret = snd_ctl_add(card, kcontrol);
1091 "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
1092 w->name, w->kcontrol_news[i].name, ret);
1095 kcontrol->private_data = w;
1096 w->kcontrols[i] = kcontrol;
1102 /* We implement power down on suspend by checking the power state of
1103 * the ALSA card - when we are suspending the ALSA state for the card
1106 static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
1108 int level = snd_power_get_state(widget->dapm->card->snd_card);
1111 case SNDRV_CTL_POWER_D3hot:
1112 case SNDRV_CTL_POWER_D3cold:
1113 if (widget->ignore_suspend)
1114 dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
1116 return widget->ignore_suspend;
1122 static void dapm_widget_list_free(struct snd_soc_dapm_widget_list **list)
1127 static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
1128 struct list_head *widgets)
1130 struct snd_soc_dapm_widget *w;
1131 struct list_head *it;
1132 unsigned int size = 0;
1135 list_for_each(it, widgets)
1138 *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
1142 list_for_each_entry(w, widgets, work_list)
1143 (*list)->widgets[i++] = w;
1145 (*list)->num_widgets = i;
1151 * Recursively reset the cached number of inputs or outputs for the specified
1152 * widget and all widgets that can be reached via incoming or outcoming paths
1155 static void invalidate_paths_ep(struct snd_soc_dapm_widget *widget,
1156 enum snd_soc_dapm_direction dir)
1158 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1159 struct snd_soc_dapm_path *path;
1161 widget->endpoints[dir] = -1;
1163 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1164 if (path->weak || path->is_supply)
1170 if (path->connect) {
1172 invalidate_paths_ep(path->node[dir], dir);
1179 * Common implementation for is_connected_output_ep() and
1180 * is_connected_input_ep(). The function is inlined since the combined size of
1181 * the two specialized functions is only marginally larger then the size of the
1182 * generic function and at the same time the fast path of the specialized
1183 * functions is significantly smaller than the generic function.
1185 static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
1186 struct list_head *list, enum snd_soc_dapm_direction dir,
1187 int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
1188 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1189 enum snd_soc_dapm_direction)),
1190 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1191 enum snd_soc_dapm_direction))
1193 enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
1194 struct snd_soc_dapm_path *path;
1197 if (widget->endpoints[dir] >= 0)
1198 return widget->endpoints[dir];
1200 DAPM_UPDATE_STAT(widget, path_checks);
1202 /* do we need to add this widget to the list ? */
1204 list_add_tail(&widget->work_list, list);
1206 if (custom_stop_condition && custom_stop_condition(widget, dir)) {
1208 custom_stop_condition = NULL;
1211 if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
1212 widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
1213 return widget->endpoints[dir];
1216 snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
1217 DAPM_UPDATE_STAT(widget, neighbour_checks);
1219 if (path->weak || path->is_supply)
1225 trace_snd_soc_dapm_path(widget, dir, path);
1227 if (path->connect) {
1229 con += fn(path->node[dir], list, custom_stop_condition);
1234 widget->endpoints[dir] = con;
1240 * Recursively check for a completed path to an active or physically connected
1241 * output widget. Returns number of complete paths.
1243 * Optionally, can be supplied with a function acting as a stopping condition.
1244 * This function takes the dapm widget currently being examined and the walk
1245 * direction as an arguments, it should return true if widgets from that point
1246 * in the graph onwards should not be added to the widget list.
1248 static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
1249 struct list_head *list,
1250 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1251 enum snd_soc_dapm_direction))
1253 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
1254 is_connected_output_ep, custom_stop_condition);
1258 * Recursively check for a completed path to an active or physically connected
1259 * input widget. Returns number of complete paths.
1261 * Optionally, can be supplied with a function acting as a stopping condition.
1262 * This function takes the dapm widget currently being examined and the walk
1263 * direction as an arguments, it should return true if the walk should be
1264 * stopped and false otherwise.
1266 static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
1267 struct list_head *list,
1268 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
1269 enum snd_soc_dapm_direction))
1271 return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
1272 is_connected_input_ep, custom_stop_condition);
1276 * snd_soc_dapm_dai_get_connected_widgets - query audio path and it's widgets.
1277 * @dai: the soc DAI.
1278 * @stream: stream direction.
1279 * @list: list of active widgets for this stream.
1280 * @custom_stop_condition: (optional) a function meant to stop the widget graph
1281 * walk based on custom logic.
1283 * Queries DAPM graph as to whether a valid audio stream path exists for
1284 * the initial stream specified by name. This takes into account
1285 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1287 * Optionally, can be supplied with a function acting as a stopping condition.
1288 * This function takes the dapm widget currently being examined and the walk
1289 * direction as an arguments, it should return true if the walk should be
1290 * stopped and false otherwise.
1292 * Returns the number of valid paths or negative error.
1294 int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1295 struct snd_soc_dapm_widget_list **list,
1296 bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
1297 enum snd_soc_dapm_direction))
1299 struct snd_soc_card *card = dai->component->card;
1300 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, stream);
1305 snd_soc_dapm_mutex_lock(card);
1307 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1308 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_OUT);
1309 paths = is_connected_output_ep(w, &widgets,
1310 custom_stop_condition);
1312 invalidate_paths_ep(w, SND_SOC_DAPM_DIR_IN);
1313 paths = is_connected_input_ep(w, &widgets,
1314 custom_stop_condition);
1317 /* Drop starting point */
1318 list_del(widgets.next);
1320 ret = dapm_widget_list_create(list, &widgets);
1324 trace_snd_soc_dapm_connected(paths, stream);
1325 snd_soc_dapm_mutex_unlock(card);
1329 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_get_connected_widgets);
1331 void snd_soc_dapm_dai_free_widgets(struct snd_soc_dapm_widget_list **list)
1333 dapm_widget_list_free(list);
1335 EXPORT_SYMBOL_GPL(snd_soc_dapm_dai_free_widgets);
1338 * Handler for regulator supply widget.
1340 int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1341 struct snd_kcontrol *kcontrol, int event)
1345 soc_dapm_async_complete(w->dapm);
1347 if (SND_SOC_DAPM_EVENT_ON(event)) {
1348 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1349 ret = regulator_allow_bypass(w->regulator, false);
1351 dev_warn(w->dapm->dev,
1352 "ASoC: Failed to unbypass %s: %d\n",
1356 return regulator_enable(w->regulator);
1358 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
1359 ret = regulator_allow_bypass(w->regulator, true);
1361 dev_warn(w->dapm->dev,
1362 "ASoC: Failed to bypass %s: %d\n",
1366 return regulator_disable_deferred(w->regulator, w->shift);
1369 EXPORT_SYMBOL_GPL(dapm_regulator_event);
1372 * Handler for pinctrl widget.
1374 int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
1375 struct snd_kcontrol *kcontrol, int event)
1377 struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
1378 struct pinctrl *p = w->pinctrl;
1379 struct pinctrl_state *s;
1384 if (SND_SOC_DAPM_EVENT_ON(event))
1385 s = pinctrl_lookup_state(p, priv->active_state);
1387 s = pinctrl_lookup_state(p, priv->sleep_state);
1392 return pinctrl_select_state(p, s);
1394 EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
1397 * Handler for clock supply widget.
1399 int dapm_clock_event(struct snd_soc_dapm_widget *w,
1400 struct snd_kcontrol *kcontrol, int event)
1405 soc_dapm_async_complete(w->dapm);
1407 if (SND_SOC_DAPM_EVENT_ON(event)) {
1408 return clk_prepare_enable(w->clk);
1410 clk_disable_unprepare(w->clk);
1416 EXPORT_SYMBOL_GPL(dapm_clock_event);
1418 static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1420 if (w->power_checked)
1421 return w->new_power;
1426 w->new_power = w->power_check(w);
1428 w->power_checked = true;
1430 return w->new_power;
1433 /* Generic check to see if a widget should be powered. */
1434 static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1438 DAPM_UPDATE_STAT(w, power_checks);
1440 in = is_connected_input_ep(w, NULL, NULL);
1441 out = is_connected_output_ep(w, NULL, NULL);
1442 return out != 0 && in != 0;
1445 /* Check to see if a power supply is needed */
1446 static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1448 struct snd_soc_dapm_path *path;
1450 DAPM_UPDATE_STAT(w, power_checks);
1452 /* Check if one of our outputs is connected */
1453 snd_soc_dapm_widget_for_each_sink_path(w, path) {
1454 DAPM_UPDATE_STAT(w, neighbour_checks);
1459 if (path->connected &&
1460 !path->connected(path->source, path->sink))
1463 if (dapm_widget_power_check(path->sink))
1470 static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1472 return w->connected;
1475 static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1476 struct snd_soc_dapm_widget *b,
1481 BUILD_BUG_ON(ARRAY_SIZE(dapm_up_seq) != SND_SOC_DAPM_TYPE_COUNT);
1482 BUILD_BUG_ON(ARRAY_SIZE(dapm_down_seq) != SND_SOC_DAPM_TYPE_COUNT);
1487 sort = dapm_down_seq;
1489 WARN_ONCE(sort[a->id] == 0, "offset a->id %d not initialized\n", a->id);
1490 WARN_ONCE(sort[b->id] == 0, "offset b->id %d not initialized\n", b->id);
1492 if (sort[a->id] != sort[b->id])
1493 return sort[a->id] - sort[b->id];
1494 if (a->subseq != b->subseq) {
1496 return a->subseq - b->subseq;
1498 return b->subseq - a->subseq;
1500 if (a->reg != b->reg)
1501 return a->reg - b->reg;
1502 if (a->dapm != b->dapm)
1503 return (unsigned long)a->dapm - (unsigned long)b->dapm;
1508 /* Insert a widget in order into a DAPM power sequence. */
1509 static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1510 struct list_head *list,
1513 struct snd_soc_dapm_widget *w;
1515 list_for_each_entry(w, list, power_list)
1516 if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1517 list_add_tail(&new_widget->power_list, &w->power_list);
1521 list_add_tail(&new_widget->power_list, list);
1524 static void dapm_seq_check_event(struct snd_soc_card *card,
1525 struct snd_soc_dapm_widget *w, int event)
1527 const char *ev_name;
1531 case SND_SOC_DAPM_PRE_PMU:
1532 ev_name = "PRE_PMU";
1535 case SND_SOC_DAPM_POST_PMU:
1536 ev_name = "POST_PMU";
1539 case SND_SOC_DAPM_PRE_PMD:
1540 ev_name = "PRE_PMD";
1543 case SND_SOC_DAPM_POST_PMD:
1544 ev_name = "POST_PMD";
1547 case SND_SOC_DAPM_WILL_PMU:
1548 ev_name = "WILL_PMU";
1551 case SND_SOC_DAPM_WILL_PMD:
1552 ev_name = "WILL_PMD";
1556 WARN(1, "Unknown event %d\n", event);
1560 if (w->new_power != power)
1563 if (w->event && (w->event_flags & event)) {
1566 pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
1568 soc_dapm_async_complete(w->dapm);
1569 trace_snd_soc_dapm_widget_event_start(w, event);
1570 ret = w->event(w, NULL, event);
1571 trace_snd_soc_dapm_widget_event_done(w, event);
1573 dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
1574 ev_name, w->name, ret);
1578 /* Apply the coalesced changes from a DAPM sequence */
1579 static void dapm_seq_run_coalesced(struct snd_soc_card *card,
1580 struct list_head *pending)
1582 struct snd_soc_dapm_context *dapm;
1583 struct snd_soc_dapm_widget *w;
1585 unsigned int value = 0;
1586 unsigned int mask = 0;
1588 w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
1592 list_for_each_entry(w, pending, power_list) {
1593 WARN_ON(reg != w->reg || dapm != w->dapm);
1594 w->power = w->new_power;
1596 mask |= w->mask << w->shift;
1598 value |= w->on_val << w->shift;
1600 value |= w->off_val << w->shift;
1602 pop_dbg(dapm->dev, card->pop_time,
1603 "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1604 w->name, reg, value, mask);
1606 /* Check for events */
1607 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
1608 dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
1612 /* Any widget will do, they should all be updating the
1616 pop_dbg(dapm->dev, card->pop_time,
1617 "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1618 value, mask, reg, card->pop_time);
1619 pop_wait(card->pop_time);
1620 soc_dapm_update_bits(dapm, reg, mask, value);
1623 list_for_each_entry(w, pending, power_list) {
1624 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
1625 dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
1629 /* Apply a DAPM power sequence.
1631 * We walk over a pre-sorted list of widgets to apply power to. In
1632 * order to minimise the number of writes to the device required
1633 * multiple widgets will be updated in a single write where possible.
1634 * Currently anything that requires more than a single write is not
1637 static void dapm_seq_run(struct snd_soc_card *card,
1638 struct list_head *list, int event, bool power_up)
1640 struct snd_soc_dapm_widget *w, *n;
1641 struct snd_soc_dapm_context *d;
1644 int cur_subseq = -1;
1645 int cur_reg = SND_SOC_NOPM;
1646 struct snd_soc_dapm_context *cur_dapm = NULL;
1653 sort = dapm_down_seq;
1655 list_for_each_entry_safe(w, n, list, power_list) {
1658 /* Do we need to apply any queued changes? */
1659 if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1660 w->dapm != cur_dapm || w->subseq != cur_subseq) {
1661 if (!list_empty(&pending))
1662 dapm_seq_run_coalesced(card, &pending);
1664 if (cur_dapm && cur_dapm->component) {
1665 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1666 if (sort[i] == cur_sort)
1667 snd_soc_component_seq_notifier(
1668 cur_dapm->component,
1672 if (cur_dapm && w->dapm != cur_dapm)
1673 soc_dapm_async_complete(cur_dapm);
1675 INIT_LIST_HEAD(&pending);
1677 cur_subseq = INT_MIN;
1678 cur_reg = SND_SOC_NOPM;
1683 case snd_soc_dapm_pre:
1687 if (event == SND_SOC_DAPM_STREAM_START)
1689 NULL, SND_SOC_DAPM_PRE_PMU);
1690 else if (event == SND_SOC_DAPM_STREAM_STOP)
1692 NULL, SND_SOC_DAPM_PRE_PMD);
1695 case snd_soc_dapm_post:
1699 if (event == SND_SOC_DAPM_STREAM_START)
1701 NULL, SND_SOC_DAPM_POST_PMU);
1702 else if (event == SND_SOC_DAPM_STREAM_STOP)
1704 NULL, SND_SOC_DAPM_POST_PMD);
1708 /* Queue it up for application */
1709 cur_sort = sort[w->id];
1710 cur_subseq = w->subseq;
1713 list_move(&w->power_list, &pending);
1718 dev_err(w->dapm->dev,
1719 "ASoC: Failed to apply widget power: %d\n", ret);
1722 if (!list_empty(&pending))
1723 dapm_seq_run_coalesced(card, &pending);
1725 if (cur_dapm && cur_dapm->component) {
1726 for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1727 if (sort[i] == cur_sort)
1728 snd_soc_component_seq_notifier(
1729 cur_dapm->component,
1733 for_each_card_dapms(card, d)
1734 soc_dapm_async_complete(d);
1737 static void dapm_widget_update(struct snd_soc_card *card)
1739 struct snd_soc_dapm_update *update = card->update;
1740 struct snd_soc_dapm_widget_list *wlist;
1741 struct snd_soc_dapm_widget *w = NULL;
1745 if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
1748 wlist = dapm_kcontrol_get_wlist(update->kcontrol);
1750 for_each_dapm_widgets(wlist, wi, w) {
1751 if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1752 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1754 dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1762 ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
1765 dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1768 if (update->has_second_set) {
1769 ret = soc_dapm_update_bits(w->dapm, update->reg2,
1770 update->mask2, update->val2);
1772 dev_err(w->dapm->dev,
1773 "ASoC: %s DAPM update failed: %d\n",
1777 for_each_dapm_widgets(wlist, wi, w) {
1778 if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1779 ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1781 dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1787 /* Async callback run prior to DAPM sequences - brings to _PREPARE if
1788 * they're changing state.
1790 static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1792 struct snd_soc_dapm_context *d = data;
1795 /* If we're off and we're not supposed to go into STANDBY */
1796 if (d->bias_level == SND_SOC_BIAS_OFF &&
1797 d->target_bias_level != SND_SOC_BIAS_OFF) {
1798 if (d->dev && cookie)
1799 pm_runtime_get_sync(d->dev);
1801 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1804 "ASoC: Failed to turn on bias: %d\n", ret);
1807 /* Prepare for a transition to ON or away from ON */
1808 if ((d->target_bias_level == SND_SOC_BIAS_ON &&
1809 d->bias_level != SND_SOC_BIAS_ON) ||
1810 (d->target_bias_level != SND_SOC_BIAS_ON &&
1811 d->bias_level == SND_SOC_BIAS_ON)) {
1812 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1815 "ASoC: Failed to prepare bias: %d\n", ret);
1819 /* Async callback run prior to DAPM sequences - brings to their final
1822 static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1824 struct snd_soc_dapm_context *d = data;
1827 /* If we just powered the last thing off drop to standby bias */
1828 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1829 (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1830 d->target_bias_level == SND_SOC_BIAS_OFF)) {
1831 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1833 dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1837 /* If we're in standby and can support bias off then do that */
1838 if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1839 d->target_bias_level == SND_SOC_BIAS_OFF) {
1840 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1842 dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1845 if (d->dev && cookie)
1846 pm_runtime_put(d->dev);
1849 /* If we just powered up then move to active bias */
1850 if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1851 d->target_bias_level == SND_SOC_BIAS_ON) {
1852 ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1854 dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1859 static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1860 bool power, bool connect)
1862 /* If a connection is being made or broken then that update
1863 * will have marked the peer dirty, otherwise the widgets are
1864 * not connected and this update has no impact. */
1868 /* If the peer is already in the state we're moving to then we
1869 * won't have an impact on it. */
1870 if (power != peer->power)
1871 dapm_mark_dirty(peer, "peer state change");
1874 static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1875 struct list_head *up_list,
1876 struct list_head *down_list)
1878 struct snd_soc_dapm_path *path;
1882 case snd_soc_dapm_pre:
1885 case snd_soc_dapm_post:
1892 power = dapm_widget_power_check(w);
1894 if (w->power == power)
1897 trace_snd_soc_dapm_widget_power(w, power);
1900 * If we changed our power state perhaps our neigbours
1903 snd_soc_dapm_widget_for_each_source_path(w, path)
1904 dapm_widget_set_peer_power(path->source, power, path->connect);
1907 * Supplies can't affect their outputs, only their inputs
1910 snd_soc_dapm_widget_for_each_sink_path(w, path)
1911 dapm_widget_set_peer_power(path->sink, power, path->connect);
1915 dapm_seq_insert(w, up_list, true);
1917 dapm_seq_insert(w, down_list, false);
1920 static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
1922 if (dapm->idle_bias_off)
1925 switch (snd_power_get_state(dapm->card->snd_card)) {
1926 case SNDRV_CTL_POWER_D3hot:
1927 case SNDRV_CTL_POWER_D3cold:
1928 return dapm->suspend_bias_off;
1937 * Scan each dapm widget for complete audio path.
1938 * A complete path is a route that has valid endpoints i.e.:-
1940 * o DAC to output pin.
1941 * o Input pin to ADC.
1942 * o Input pin to Output pin (bypass, sidetone)
1943 * o DAC to ADC (loopback).
1945 static int dapm_power_widgets(struct snd_soc_card *card, int event)
1947 struct snd_soc_dapm_widget *w;
1948 struct snd_soc_dapm_context *d;
1950 LIST_HEAD(down_list);
1951 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1952 enum snd_soc_bias_level bias;
1955 snd_soc_dapm_mutex_assert_held(card);
1957 trace_snd_soc_dapm_start(card);
1959 for_each_card_dapms(card, d) {
1960 if (dapm_idle_bias_off(d))
1961 d->target_bias_level = SND_SOC_BIAS_OFF;
1963 d->target_bias_level = SND_SOC_BIAS_STANDBY;
1968 /* Check which widgets we need to power and store them in
1969 * lists indicating if they should be powered up or down. We
1970 * only check widgets that have been flagged as dirty but note
1971 * that new widgets may be added to the dirty list while we
1974 list_for_each_entry(w, &card->dapm_dirty, dirty) {
1975 dapm_power_one_widget(w, &up_list, &down_list);
1978 for_each_card_widgets(card, w) {
1980 case snd_soc_dapm_pre:
1981 case snd_soc_dapm_post:
1982 /* These widgets always need to be powered */
1985 list_del_init(&w->dirty);
1992 /* Supplies and micbiases only bring the
1993 * context up to STANDBY as unless something
1994 * else is active and passing audio they
1995 * generally don't require full power. Signal
1996 * generators are virtual pins and have no
1997 * power impact themselves.
2000 case snd_soc_dapm_siggen:
2001 case snd_soc_dapm_vmid:
2003 case snd_soc_dapm_supply:
2004 case snd_soc_dapm_regulator_supply:
2005 case snd_soc_dapm_pinctrl:
2006 case snd_soc_dapm_clock_supply:
2007 case snd_soc_dapm_micbias:
2008 if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
2009 d->target_bias_level = SND_SOC_BIAS_STANDBY;
2012 d->target_bias_level = SND_SOC_BIAS_ON;
2019 /* Force all contexts in the card to the same bias state if
2020 * they're not ground referenced.
2022 bias = SND_SOC_BIAS_OFF;
2023 for_each_card_dapms(card, d)
2024 if (d->target_bias_level > bias)
2025 bias = d->target_bias_level;
2026 for_each_card_dapms(card, d)
2027 if (!dapm_idle_bias_off(d))
2028 d->target_bias_level = bias;
2030 trace_snd_soc_dapm_walk_done(card);
2032 /* Run card bias changes at first */
2033 dapm_pre_sequence_async(&card->dapm, 0);
2034 /* Run other bias changes in parallel */
2035 for_each_card_dapms(card, d) {
2036 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2037 async_schedule_domain(dapm_pre_sequence_async, d,
2040 async_synchronize_full_domain(&async_domain);
2042 list_for_each_entry(w, &down_list, power_list) {
2043 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
2046 list_for_each_entry(w, &up_list, power_list) {
2047 dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
2050 /* Power down widgets first; try to avoid amplifying pops. */
2051 dapm_seq_run(card, &down_list, event, false);
2053 dapm_widget_update(card);
2056 dapm_seq_run(card, &up_list, event, true);
2058 /* Run all the bias changes in parallel */
2059 for_each_card_dapms(card, d) {
2060 if (d != &card->dapm && d->bias_level != d->target_bias_level)
2061 async_schedule_domain(dapm_post_sequence_async, d,
2064 async_synchronize_full_domain(&async_domain);
2065 /* Run card bias changes at last */
2066 dapm_post_sequence_async(&card->dapm, 0);
2068 /* do we need to notify any clients that DAPM event is complete */
2069 for_each_card_dapms(card, d) {
2073 ret = snd_soc_component_stream_event(d->component, event);
2078 pop_dbg(card->dev, card->pop_time,
2079 "DAPM sequencing finished, waiting %dms\n", card->pop_time);
2080 pop_wait(card->pop_time);
2082 trace_snd_soc_dapm_done(card);
2087 #ifdef CONFIG_DEBUG_FS
2088 static ssize_t dapm_widget_power_read_file(struct file *file,
2089 char __user *user_buf,
2090 size_t count, loff_t *ppos)
2092 struct snd_soc_dapm_widget *w = file->private_data;
2093 enum snd_soc_dapm_direction dir, rdir;
2097 struct snd_soc_dapm_path *p = NULL;
2099 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2103 snd_soc_dapm_mutex_lock_root(w->dapm);
2105 /* Supply widgets are not handled by is_connected_{input,output}_ep() */
2110 in = is_connected_input_ep(w, NULL, NULL);
2111 out = is_connected_output_ep(w, NULL, NULL);
2114 ret = scnprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
2115 w->name, w->power ? "On" : "Off",
2116 w->force ? " (forced)" : "", in, out);
2119 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2120 " - R%d(0x%x) mask 0x%x",
2121 w->reg, w->reg, w->mask << w->shift);
2123 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
2126 ret += scnprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
2128 w->active ? "active" : "inactive");
2130 snd_soc_dapm_for_each_direction(dir) {
2131 rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
2132 snd_soc_dapm_widget_for_each_path(w, dir, p) {
2133 if (p->connected && !p->connected(p->source, p->sink))
2139 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
2140 " %s \"%s\" \"%s\"\n",
2141 (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
2142 p->name ? p->name : "static",
2143 p->node[rdir]->name);
2147 snd_soc_dapm_mutex_unlock(w->dapm);
2149 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
2155 static const struct file_operations dapm_widget_power_fops = {
2156 .open = simple_open,
2157 .read = dapm_widget_power_read_file,
2158 .llseek = default_llseek,
2161 static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
2162 size_t count, loff_t *ppos)
2164 struct snd_soc_dapm_context *dapm = file->private_data;
2167 switch (dapm->bias_level) {
2168 case SND_SOC_BIAS_ON:
2171 case SND_SOC_BIAS_PREPARE:
2172 level = "Prepare\n";
2174 case SND_SOC_BIAS_STANDBY:
2175 level = "Standby\n";
2177 case SND_SOC_BIAS_OFF:
2181 WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
2182 level = "Unknown\n";
2186 return simple_read_from_buffer(user_buf, count, ppos, level,
2190 static const struct file_operations dapm_bias_fops = {
2191 .open = simple_open,
2192 .read = dapm_bias_read_file,
2193 .llseek = default_llseek,
2196 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2197 struct dentry *parent)
2199 if (!parent || IS_ERR(parent))
2202 dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
2204 debugfs_create_file("bias_level", 0444, dapm->debugfs_dapm, dapm,
2208 static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2210 struct snd_soc_dapm_context *dapm = w->dapm;
2212 if (!dapm->debugfs_dapm || !w->name)
2215 debugfs_create_file(w->name, 0444, dapm->debugfs_dapm, w,
2216 &dapm_widget_power_fops);
2219 static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2221 debugfs_remove_recursive(dapm->debugfs_dapm);
2222 dapm->debugfs_dapm = NULL;
2226 void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
2227 struct dentry *parent)
2231 static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
2235 static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
2242 * soc_dapm_connect_path() - Connects or disconnects a path
2243 * @path: The path to update
2244 * @connect: The new connect state of the path. True if the path is connected,
2245 * false if it is disconnected.
2246 * @reason: The reason why the path changed (for debugging only)
2248 static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
2249 bool connect, const char *reason)
2251 if (path->connect == connect)
2254 path->connect = connect;
2255 dapm_mark_dirty(path->source, reason);
2256 dapm_mark_dirty(path->sink, reason);
2257 dapm_path_invalidate(path);
2260 /* test and update the power status of a mux widget */
2261 static int soc_dapm_mux_update_power(struct snd_soc_card *card,
2262 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
2264 struct snd_soc_dapm_path *path;
2268 snd_soc_dapm_mutex_assert_held(card);
2270 /* find dapm widget path assoc with kcontrol */
2271 dapm_kcontrol_for_each_path(path, kcontrol) {
2273 /* we now need to match the string in the enum to the path */
2274 if (e && !(strcmp(path->name, e->texts[mux])))
2279 soc_dapm_connect_path(path, connect, "mux update");
2283 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2288 int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
2289 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
2290 struct snd_soc_dapm_update *update)
2292 struct snd_soc_card *card = dapm->card;
2295 snd_soc_dapm_mutex_lock(card);
2296 card->update = update;
2297 ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
2298 card->update = NULL;
2299 snd_soc_dapm_mutex_unlock(card);
2301 snd_soc_dpcm_runtime_update(card);
2304 EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
2306 /* test and update the power status of a mixer or switch widget */
2307 static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
2308 struct snd_kcontrol *kcontrol,
2309 int connect, int rconnect)
2311 struct snd_soc_dapm_path *path;
2314 snd_soc_dapm_mutex_assert_held(card);
2316 /* find dapm widget path assoc with kcontrol */
2317 dapm_kcontrol_for_each_path(path, kcontrol) {
2319 * Ideally this function should support any number of
2320 * paths and channels. But since kcontrols only come
2321 * in mono and stereo variants, we are limited to 2
2324 * The following code assumes for stereo controls the
2325 * first path (when 'found == 0') is the left channel,
2326 * and all remaining paths (when 'found == 1') are the
2329 * A stereo control is signified by a valid 'rconnect'
2330 * value, either 0 for unconnected, or >= 0 for connected.
2331 * This is chosen instead of using snd_soc_volsw_is_stereo,
2332 * so that the behavior of snd_soc_dapm_mixer_update_power
2333 * doesn't change even when the kcontrol passed in is
2336 * It passes 'connect' as the path connect status for
2337 * the left channel, and 'rconnect' for the right
2340 if (found && rconnect >= 0)
2341 soc_dapm_connect_path(path, rconnect, "mixer update");
2343 soc_dapm_connect_path(path, connect, "mixer update");
2348 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
2353 int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
2354 struct snd_kcontrol *kcontrol, int connect,
2355 struct snd_soc_dapm_update *update)
2357 struct snd_soc_card *card = dapm->card;
2360 snd_soc_dapm_mutex_lock(card);
2361 card->update = update;
2362 ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
2363 card->update = NULL;
2364 snd_soc_dapm_mutex_unlock(card);
2366 snd_soc_dpcm_runtime_update(card);
2369 EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2371 static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
2372 char *buf, int count)
2374 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
2375 struct snd_soc_dapm_widget *w;
2376 char *state = "not set";
2378 /* card won't be set for the dummy component, as a spot fix
2379 * we're checking for that case specifically here but in future
2380 * we will ensure that the dummy component looks like others.
2385 for_each_card_widgets(cmpnt->card, w) {
2386 if (w->dapm != dapm)
2389 /* only display widgets that burn power */
2391 case snd_soc_dapm_hp:
2392 case snd_soc_dapm_mic:
2393 case snd_soc_dapm_spk:
2394 case snd_soc_dapm_line:
2395 case snd_soc_dapm_micbias:
2396 case snd_soc_dapm_dac:
2397 case snd_soc_dapm_adc:
2398 case snd_soc_dapm_pga:
2399 case snd_soc_dapm_effect:
2400 case snd_soc_dapm_out_drv:
2401 case snd_soc_dapm_mixer:
2402 case snd_soc_dapm_mixer_named_ctl:
2403 case snd_soc_dapm_supply:
2404 case snd_soc_dapm_regulator_supply:
2405 case snd_soc_dapm_pinctrl:
2406 case snd_soc_dapm_clock_supply:
2408 count += sysfs_emit_at(buf, count, "%s: %s\n",
2409 w->name, w->power ? "On":"Off");
2416 switch (snd_soc_dapm_get_bias_level(dapm)) {
2417 case SND_SOC_BIAS_ON:
2420 case SND_SOC_BIAS_PREPARE:
2423 case SND_SOC_BIAS_STANDBY:
2426 case SND_SOC_BIAS_OFF:
2430 count += sysfs_emit_at(buf, count, "PM State: %s\n", state);
2435 /* show dapm widget status in sys fs */
2436 static ssize_t dapm_widget_show(struct device *dev,
2437 struct device_attribute *attr, char *buf)
2439 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2440 struct snd_soc_dai *codec_dai;
2443 snd_soc_dapm_mutex_lock_root(rtd->card);
2445 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2446 struct snd_soc_component *cmpnt = codec_dai->component;
2448 count = dapm_widget_show_component(cmpnt, buf, count);
2451 snd_soc_dapm_mutex_unlock(rtd->card);
2456 static DEVICE_ATTR_RO(dapm_widget);
2458 struct attribute *soc_dapm_dev_attrs[] = {
2459 &dev_attr_dapm_widget.attr,
2463 static void dapm_free_path(struct snd_soc_dapm_path *path)
2465 list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
2466 list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
2467 list_del(&path->list_kcontrol);
2468 list_del(&path->list);
2473 * snd_soc_dapm_free_widget - Free specified widget
2474 * @w: widget to free
2476 * Removes widget from all paths and frees memory occupied by it.
2478 void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
2480 struct snd_soc_dapm_path *p, *next_p;
2481 enum snd_soc_dapm_direction dir;
2487 list_del(&w->dirty);
2489 * remove source and sink paths associated to this widget.
2490 * While removing the path, remove reference to it from both
2491 * source and sink widgets so that path is removed only once.
2493 snd_soc_dapm_for_each_direction(dir) {
2494 snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
2498 kfree(w->kcontrols);
2499 kfree_const(w->name);
2500 kfree_const(w->sname);
2503 EXPORT_SYMBOL_GPL(snd_soc_dapm_free_widget);
2505 /* free all dapm widgets and resources */
2506 static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2508 struct snd_soc_dapm_widget *w, *next_w;
2510 for_each_card_widgets_safe(dapm->card, w, next_w) {
2511 if (w->dapm != dapm)
2513 snd_soc_dapm_free_widget(w);
2516 dapm->wcache_sink = NULL;
2517 dapm->wcache_source = NULL;
2520 static struct snd_soc_dapm_widget *dapm_find_widget(
2521 struct snd_soc_dapm_context *dapm, const char *pin,
2522 bool search_other_contexts)
2524 struct snd_soc_dapm_widget *w;
2525 struct snd_soc_dapm_widget *fallback = NULL;
2526 char prefixed_pin[80];
2527 const char *pin_name;
2528 const char *prefix = soc_dapm_prefix(dapm);
2531 snprintf(prefixed_pin, sizeof(prefixed_pin), "%s %s",
2533 pin_name = prefixed_pin;
2538 for_each_card_widgets(dapm->card, w) {
2539 if (!strcmp(w->name, pin_name)) {
2540 if (w->dapm == dapm)
2547 if (search_other_contexts)
2554 * set the DAPM pin status:
2555 * returns 1 when the value has been updated, 0 when unchanged, or a negative
2556 * error code; called from kcontrol put callback
2558 static int __snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2559 const char *pin, int status)
2561 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2564 dapm_assert_locked(dapm);
2567 dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2571 if (w->connected != status) {
2572 dapm_mark_dirty(w, "pin configuration");
2573 dapm_widget_invalidate_input_paths(w);
2574 dapm_widget_invalidate_output_paths(w);
2578 w->connected = status;
2586 * similar as __snd_soc_dapm_set_pin(), but returns 0 when successful;
2587 * called from several API functions below
2589 static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2590 const char *pin, int status)
2592 int ret = __snd_soc_dapm_set_pin(dapm, pin, status);
2594 return ret < 0 ? ret : 0;
2598 * snd_soc_dapm_sync_unlocked - scan and power dapm paths
2599 * @dapm: DAPM context
2601 * Walks all dapm audio paths and powers widgets according to their
2602 * stream or path usage.
2604 * Requires external locking.
2606 * Returns 0 for success.
2608 int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
2611 * Suppress early reports (eg, jacks syncing their state) to avoid
2612 * silly DAPM runs during card startup.
2614 if (!snd_soc_card_is_instantiated(dapm->card))
2617 return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
2619 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
2622 * snd_soc_dapm_sync - scan and power dapm paths
2623 * @dapm: DAPM context
2625 * Walks all dapm audio paths and powers widgets according to their
2626 * stream or path usage.
2628 * Returns 0 for success.
2630 int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2634 snd_soc_dapm_mutex_lock(dapm);
2635 ret = snd_soc_dapm_sync_unlocked(dapm);
2636 snd_soc_dapm_mutex_unlock(dapm);
2639 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2641 static int dapm_update_dai_chan(struct snd_soc_dapm_path *p,
2642 struct snd_soc_dapm_widget *w,
2646 case snd_soc_dapm_aif_out:
2647 case snd_soc_dapm_aif_in:
2653 dev_dbg(w->dapm->dev, "%s DAI route %s -> %s\n",
2654 w->channel < channels ? "Connecting" : "Disconnecting",
2655 p->source->name, p->sink->name);
2657 if (w->channel < channels)
2658 soc_dapm_connect_path(p, true, "dai update");
2660 soc_dapm_connect_path(p, false, "dai update");
2665 static int dapm_update_dai_unlocked(struct snd_pcm_substream *substream,
2666 struct snd_pcm_hw_params *params,
2667 struct snd_soc_dai *dai)
2669 int dir = substream->stream;
2670 int channels = params_channels(params);
2671 struct snd_soc_dapm_path *p;
2672 struct snd_soc_dapm_widget *w;
2675 w = snd_soc_dai_get_widget(dai, dir);
2680 dev_dbg(dai->dev, "Update DAI routes for %s %s\n", dai->name,
2681 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
2683 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2684 ret = dapm_update_dai_chan(p, p->sink, channels);
2689 snd_soc_dapm_widget_for_each_source_path(w, p) {
2690 ret = dapm_update_dai_chan(p, p->source, channels);
2698 int snd_soc_dapm_update_dai(struct snd_pcm_substream *substream,
2699 struct snd_pcm_hw_params *params,
2700 struct snd_soc_dai *dai)
2702 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2705 snd_soc_dapm_mutex_lock(rtd->card);
2706 ret = dapm_update_dai_unlocked(substream, params, dai);
2707 snd_soc_dapm_mutex_unlock(rtd->card);
2711 EXPORT_SYMBOL_GPL(snd_soc_dapm_update_dai);
2714 * dapm_update_widget_flags() - Re-compute widget sink and source flags
2715 * @w: The widget for which to update the flags
2717 * Some widgets have a dynamic category which depends on which neighbors they
2718 * are connected to. This function update the category for these widgets.
2720 * This function must be called whenever a path is added or removed to a widget.
2722 static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
2724 enum snd_soc_dapm_direction dir;
2725 struct snd_soc_dapm_path *p;
2729 case snd_soc_dapm_input:
2730 /* On a fully routed card an input is never a source */
2731 if (w->dapm->card->fully_routed)
2733 ep = SND_SOC_DAPM_EP_SOURCE;
2734 snd_soc_dapm_widget_for_each_source_path(w, p) {
2735 if (p->source->id == snd_soc_dapm_micbias ||
2736 p->source->id == snd_soc_dapm_mic ||
2737 p->source->id == snd_soc_dapm_line ||
2738 p->source->id == snd_soc_dapm_output) {
2744 case snd_soc_dapm_output:
2745 /* On a fully routed card a output is never a sink */
2746 if (w->dapm->card->fully_routed)
2748 ep = SND_SOC_DAPM_EP_SINK;
2749 snd_soc_dapm_widget_for_each_sink_path(w, p) {
2750 if (p->sink->id == snd_soc_dapm_spk ||
2751 p->sink->id == snd_soc_dapm_hp ||
2752 p->sink->id == snd_soc_dapm_line ||
2753 p->sink->id == snd_soc_dapm_input) {
2759 case snd_soc_dapm_line:
2761 snd_soc_dapm_for_each_direction(dir) {
2762 if (!list_empty(&w->edges[dir]))
2763 ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
2773 static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
2774 struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
2775 const char *control)
2777 bool dynamic_source = false;
2778 bool dynamic_sink = false;
2783 switch (source->id) {
2784 case snd_soc_dapm_demux:
2785 dynamic_source = true;
2792 case snd_soc_dapm_mux:
2793 case snd_soc_dapm_switch:
2794 case snd_soc_dapm_mixer:
2795 case snd_soc_dapm_mixer_named_ctl:
2796 dynamic_sink = true;
2802 if (dynamic_source && dynamic_sink) {
2804 "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
2805 source->name, control, sink->name);
2807 } else if (!dynamic_source && !dynamic_sink) {
2809 "Control not supported for path %s -> [%s] -> %s\n",
2810 source->name, control, sink->name);
2817 static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
2818 struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
2819 const char *control,
2820 int (*connected)(struct snd_soc_dapm_widget *source,
2821 struct snd_soc_dapm_widget *sink))
2823 enum snd_soc_dapm_direction dir;
2824 struct snd_soc_dapm_path *path;
2827 if (wsink->is_supply && !wsource->is_supply) {
2829 "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
2830 wsource->name, wsink->name);
2834 if (connected && !wsource->is_supply) {
2836 "connected() callback only supported for supply widgets (%s -> %s)\n",
2837 wsource->name, wsink->name);
2841 if (wsource->is_supply && control) {
2843 "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
2844 wsource->name, control, wsink->name);
2848 ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
2852 path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2856 path->node[SND_SOC_DAPM_DIR_IN] = wsource;
2857 path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
2859 path->connected = connected;
2860 INIT_LIST_HEAD(&path->list);
2861 INIT_LIST_HEAD(&path->list_kcontrol);
2863 if (wsource->is_supply || wsink->is_supply)
2864 path->is_supply = 1;
2866 /* connect static paths */
2867 if (control == NULL) {
2870 switch (wsource->id) {
2871 case snd_soc_dapm_demux:
2872 ret = dapm_connect_mux(dapm, path, control, wsource);
2880 switch (wsink->id) {
2881 case snd_soc_dapm_mux:
2882 ret = dapm_connect_mux(dapm, path, control, wsink);
2886 case snd_soc_dapm_switch:
2887 case snd_soc_dapm_mixer:
2888 case snd_soc_dapm_mixer_named_ctl:
2889 ret = dapm_connect_mixer(dapm, path, control);
2898 list_add(&path->list, &dapm->card->paths);
2900 snd_soc_dapm_for_each_direction(dir)
2901 list_add(&path->list_node[dir], &path->node[dir]->edges[dir]);
2903 snd_soc_dapm_for_each_direction(dir) {
2904 dapm_update_widget_flags(path->node[dir]);
2905 dapm_mark_dirty(path->node[dir], "Route added");
2908 if (snd_soc_card_is_instantiated(dapm->card) && path->connect)
2909 dapm_path_invalidate(path);
2917 static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2918 const struct snd_soc_dapm_route *route)
2920 struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2921 struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2924 char prefixed_sink[80];
2925 char prefixed_source[80];
2927 unsigned int sink_ref = 0;
2928 unsigned int source_ref = 0;
2931 prefix = soc_dapm_prefix(dapm);
2933 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2934 prefix, route->sink);
2935 sink = prefixed_sink;
2936 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2937 prefix, route->source);
2938 source = prefixed_source;
2941 source = route->source;
2944 wsource = dapm_wcache_lookup(dapm->wcache_source, source);
2945 wsink = dapm_wcache_lookup(dapm->wcache_sink, sink);
2947 if (wsink && wsource)
2951 * find src and dest widgets over all widgets but favor a widget from
2952 * current DAPM context
2954 for_each_card_widgets(dapm->card, w) {
2955 if (!wsink && !(strcmp(w->name, sink))) {
2957 if (w->dapm == dapm) {
2965 "ASoC: sink widget %s overwritten\n",
2969 if (!wsource && !(strcmp(w->name, source))) {
2971 if (w->dapm == dapm) {
2979 "ASoC: source widget %s overwritten\n",
2983 /* use widget from another DAPM context if not found from this */
2997 dapm->wcache_sink = wsink;
2998 dapm->wcache_source = wsource;
3000 ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
3004 dev_err(dapm->dev, "ASoC: Failed to add route %s%s -%s%s%s> %s%s\n",
3005 source, !wsource ? "(*)" : "",
3006 !route->control ? "" : "> [",
3007 !route->control ? "" : route->control,
3008 !route->control ? "" : "] -",
3009 sink, !wsink ? "(*)" : "");
3013 static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
3014 const struct snd_soc_dapm_route *route)
3016 struct snd_soc_dapm_path *path, *p;
3019 char prefixed_sink[80];
3020 char prefixed_source[80];
3023 if (route->control) {
3025 "ASoC: Removal of routes with controls not supported\n");
3029 prefix = soc_dapm_prefix(dapm);
3031 snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
3032 prefix, route->sink);
3033 sink = prefixed_sink;
3034 snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
3035 prefix, route->source);
3036 source = prefixed_source;
3039 source = route->source;
3043 list_for_each_entry(p, &dapm->card->paths, list) {
3044 if (strcmp(p->source->name, source) != 0)
3046 if (strcmp(p->sink->name, sink) != 0)
3053 struct snd_soc_dapm_widget *wsource = path->source;
3054 struct snd_soc_dapm_widget *wsink = path->sink;
3056 dapm_mark_dirty(wsource, "Route removed");
3057 dapm_mark_dirty(wsink, "Route removed");
3059 dapm_path_invalidate(path);
3061 dapm_free_path(path);
3063 /* Update any path related flags */
3064 dapm_update_widget_flags(wsource);
3065 dapm_update_widget_flags(wsink);
3067 dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
3075 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
3076 * @dapm: DAPM context
3077 * @route: audio routes
3078 * @num: number of routes
3080 * Connects 2 dapm widgets together via a named audio path. The sink is
3081 * the widget receiving the audio signal, whilst the source is the sender
3082 * of the audio signal.
3084 * Returns 0 for success else error. On error all resources can be freed
3085 * with a call to snd_soc_card_free().
3087 int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
3088 const struct snd_soc_dapm_route *route, int num)
3092 snd_soc_dapm_mutex_lock(dapm);
3093 for (i = 0; i < num; i++) {
3094 int r = snd_soc_dapm_add_route(dapm, route);
3099 snd_soc_dapm_mutex_unlock(dapm);
3103 EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
3106 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
3107 * @dapm: DAPM context
3108 * @route: audio routes
3109 * @num: number of routes
3111 * Removes routes from the DAPM context.
3113 int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
3114 const struct snd_soc_dapm_route *route, int num)
3118 snd_soc_dapm_mutex_lock(dapm);
3119 for (i = 0; i < num; i++) {
3120 snd_soc_dapm_del_route(dapm, route);
3123 snd_soc_dapm_mutex_unlock(dapm);
3127 EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
3129 static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
3130 const struct snd_soc_dapm_route *route)
3132 struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
3135 struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
3138 struct snd_soc_dapm_path *path;
3142 dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
3148 dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
3153 if (route->control || route->connected)
3154 dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
3155 route->source, route->sink);
3157 snd_soc_dapm_widget_for_each_sink_path(source, path) {
3158 if (path->sink == sink) {
3165 dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
3166 route->source, route->sink);
3168 dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
3169 count, route->source, route->sink);
3175 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
3176 * @dapm: DAPM context
3177 * @route: audio routes
3178 * @num: number of routes
3180 * Mark existing routes matching those specified in the passed array
3181 * as being weak, meaning that they are ignored for the purpose of
3182 * power decisions. The main intended use case is for sidetone paths
3183 * which couple audio between other independent paths if they are both
3184 * active in order to make the combination work better at the user
3185 * level but which aren't intended to be "used".
3187 * Note that CODEC drivers should not use this as sidetone type paths
3188 * can frequently also be used as bypass paths.
3190 int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
3191 const struct snd_soc_dapm_route *route, int num)
3196 snd_soc_dapm_mutex_lock_root(dapm);
3197 for (i = 0; i < num; i++) {
3198 int err = snd_soc_dapm_weak_route(dapm, route);
3203 snd_soc_dapm_mutex_unlock(dapm);
3207 EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
3210 * snd_soc_dapm_new_widgets - add new dapm widgets
3211 * @card: card to be checked for new dapm widgets
3213 * Checks the codec for any new dapm widgets and creates them if found.
3215 * Returns 0 for success.
3217 int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
3219 struct snd_soc_dapm_widget *w;
3222 snd_soc_dapm_mutex_lock_root(card);
3224 for_each_card_widgets(card, w)
3229 if (w->num_kcontrols) {
3230 w->kcontrols = kcalloc(w->num_kcontrols,
3231 sizeof(struct snd_kcontrol *),
3233 if (!w->kcontrols) {
3234 snd_soc_dapm_mutex_unlock(card);
3240 case snd_soc_dapm_switch:
3241 case snd_soc_dapm_mixer:
3242 case snd_soc_dapm_mixer_named_ctl:
3245 case snd_soc_dapm_mux:
3246 case snd_soc_dapm_demux:
3249 case snd_soc_dapm_pga:
3250 case snd_soc_dapm_effect:
3251 case snd_soc_dapm_out_drv:
3254 case snd_soc_dapm_dai_link:
3255 dapm_new_dai_link(w);
3261 /* Read the initial power state from the device */
3263 val = soc_dapm_read(w->dapm, w->reg);
3264 val = val >> w->shift;
3266 if (val == w->on_val)
3272 dapm_mark_dirty(w, "new widget");
3273 dapm_debugfs_add_widget(w);
3276 dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
3277 snd_soc_dapm_mutex_unlock(card);
3280 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
3283 * snd_soc_dapm_get_volsw - dapm mixer get callback
3284 * @kcontrol: mixer control
3285 * @ucontrol: control element information
3287 * Callback to get the value of a dapm mixer control.
3289 * Returns 0 for success.
3291 int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_value *ucontrol)
3294 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3295 struct soc_mixer_control *mc =
3296 (struct soc_mixer_control *)kcontrol->private_value;
3298 unsigned int shift = mc->shift;
3300 unsigned int width = fls(max);
3301 unsigned int mask = (1 << fls(max)) - 1;
3302 unsigned int invert = mc->invert;
3303 unsigned int reg_val, val, rval = 0;
3305 snd_soc_dapm_mutex_lock(dapm);
3306 if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
3307 reg_val = soc_dapm_read(dapm, reg);
3308 val = (reg_val >> shift) & mask;
3310 if (reg != mc->rreg)
3311 reg_val = soc_dapm_read(dapm, mc->rreg);
3313 if (snd_soc_volsw_is_stereo(mc))
3314 rval = (reg_val >> mc->rshift) & mask;
3316 reg_val = dapm_kcontrol_get_value(kcontrol);
3317 val = reg_val & mask;
3319 if (snd_soc_volsw_is_stereo(mc))
3320 rval = (reg_val >> width) & mask;
3322 snd_soc_dapm_mutex_unlock(dapm);
3325 ucontrol->value.integer.value[0] = max - val;
3327 ucontrol->value.integer.value[0] = val;
3329 if (snd_soc_volsw_is_stereo(mc)) {
3331 ucontrol->value.integer.value[1] = max - rval;
3333 ucontrol->value.integer.value[1] = rval;
3338 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
3341 * snd_soc_dapm_put_volsw - dapm mixer set callback
3342 * @kcontrol: mixer control
3343 * @ucontrol: control element information
3345 * Callback to set the value of a dapm mixer control.
3347 * Returns 0 for success.
3349 int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
3350 struct snd_ctl_elem_value *ucontrol)
3352 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3353 struct snd_soc_card *card = dapm->card;
3354 struct soc_mixer_control *mc =
3355 (struct soc_mixer_control *)kcontrol->private_value;
3357 unsigned int shift = mc->shift;
3359 unsigned int width = fls(max);
3360 unsigned int mask = (1 << width) - 1;
3361 unsigned int invert = mc->invert;
3362 unsigned int val, rval = 0;
3363 int connect, rconnect = -1, change, reg_change = 0;
3364 struct snd_soc_dapm_update update = {};
3367 val = (ucontrol->value.integer.value[0] & mask);
3373 if (snd_soc_volsw_is_stereo(mc)) {
3374 rval = (ucontrol->value.integer.value[1] & mask);
3380 snd_soc_dapm_mutex_lock(card);
3382 /* This assumes field width < (bits in unsigned int / 2) */
3383 if (width > sizeof(unsigned int) * 8 / 2)
3385 "ASoC: control %s field width limit exceeded\n",
3387 change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
3389 if (reg != SND_SOC_NOPM) {
3391 rval = rval << mc->rshift;
3393 reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
3395 if (snd_soc_volsw_is_stereo(mc))
3396 reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
3401 if (change || reg_change) {
3403 if (snd_soc_volsw_is_stereo(mc)) {
3404 update.has_second_set = true;
3405 update.reg2 = mc->rreg;
3406 update.mask2 = mask << mc->rshift;
3409 update.kcontrol = kcontrol;
3411 update.mask = mask << shift;
3413 card->update = &update;
3416 ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
3419 card->update = NULL;
3422 snd_soc_dapm_mutex_unlock(card);
3425 snd_soc_dpcm_runtime_update(card);
3429 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
3432 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
3433 * @kcontrol: mixer control
3434 * @ucontrol: control element information
3436 * Callback to get the value of a dapm enumerated double mixer control.
3438 * Returns 0 for success.
3440 int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
3441 struct snd_ctl_elem_value *ucontrol)
3443 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3445 unsigned int reg_val, val;
3447 snd_soc_dapm_mutex_lock(dapm);
3448 if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
3449 reg_val = soc_dapm_read(dapm, e->reg);
3451 reg_val = dapm_kcontrol_get_value(kcontrol);
3453 snd_soc_dapm_mutex_unlock(dapm);
3455 val = (reg_val >> e->shift_l) & e->mask;
3456 ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
3457 if (e->shift_l != e->shift_r) {
3458 val = (reg_val >> e->shift_r) & e->mask;
3459 val = snd_soc_enum_val_to_item(e, val);
3460 ucontrol->value.enumerated.item[1] = val;
3465 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
3468 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
3469 * @kcontrol: mixer control
3470 * @ucontrol: control element information
3472 * Callback to set the value of a dapm enumerated double mixer control.
3474 * Returns 0 for success.
3476 int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
3477 struct snd_ctl_elem_value *ucontrol)
3479 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
3480 struct snd_soc_card *card = dapm->card;
3481 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3482 unsigned int *item = ucontrol->value.enumerated.item;
3483 unsigned int val, change, reg_change = 0;
3485 struct snd_soc_dapm_update update = {};
3488 if (item[0] >= e->items)
3491 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
3492 mask = e->mask << e->shift_l;
3493 if (e->shift_l != e->shift_r) {
3494 if (item[1] > e->items)
3496 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
3497 mask |= e->mask << e->shift_r;
3500 snd_soc_dapm_mutex_lock(card);
3502 change = dapm_kcontrol_set_value(kcontrol, val);
3504 if (e->reg != SND_SOC_NOPM)
3505 reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
3507 if (change || reg_change) {
3509 update.kcontrol = kcontrol;
3510 update.reg = e->reg;
3513 card->update = &update;
3516 ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
3518 card->update = NULL;
3521 snd_soc_dapm_mutex_unlock(card);
3524 snd_soc_dpcm_runtime_update(card);
3528 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
3531 * snd_soc_dapm_info_pin_switch - Info for a pin switch
3533 * @kcontrol: mixer control
3534 * @uinfo: control element information
3536 * Callback to provide information about a pin switch control.
3538 int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3539 struct snd_ctl_elem_info *uinfo)
3541 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3543 uinfo->value.integer.min = 0;
3544 uinfo->value.integer.max = 1;
3548 EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3551 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3553 * @kcontrol: mixer control
3556 int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3557 struct snd_ctl_elem_value *ucontrol)
3559 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3560 const char *pin = (const char *)kcontrol->private_value;
3562 snd_soc_dapm_mutex_lock(card);
3564 ucontrol->value.integer.value[0] =
3565 snd_soc_dapm_get_pin_status(&card->dapm, pin);
3567 snd_soc_dapm_mutex_unlock(card);
3571 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3574 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3576 * @kcontrol: mixer control
3579 int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3580 struct snd_ctl_elem_value *ucontrol)
3582 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3583 const char *pin = (const char *)kcontrol->private_value;
3586 snd_soc_dapm_mutex_lock(card);
3587 ret = __snd_soc_dapm_set_pin(&card->dapm, pin,
3588 !!ucontrol->value.integer.value[0]);
3589 snd_soc_dapm_mutex_unlock(card);
3591 snd_soc_dapm_sync(&card->dapm);
3594 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3596 struct snd_soc_dapm_widget *
3597 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
3598 const struct snd_soc_dapm_widget *widget)
3600 enum snd_soc_dapm_direction dir;
3601 struct snd_soc_dapm_widget *w;
3605 if ((w = dapm_cnew_widget(widget)) == NULL)
3608 prefix = soc_dapm_prefix(dapm);
3610 w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
3612 w->name = kstrdup_const(widget->name, GFP_KERNEL);
3617 case snd_soc_dapm_regulator_supply:
3618 w->regulator = devm_regulator_get(dapm->dev, widget->name);
3619 if (IS_ERR(w->regulator)) {
3620 ret = PTR_ERR(w->regulator);
3621 goto request_failed;
3624 if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
3625 ret = regulator_allow_bypass(w->regulator, true);
3628 "ASoC: Failed to bypass %s: %d\n",
3632 case snd_soc_dapm_pinctrl:
3633 w->pinctrl = devm_pinctrl_get(dapm->dev);
3634 if (IS_ERR(w->pinctrl)) {
3635 ret = PTR_ERR(w->pinctrl);
3636 goto request_failed;
3639 /* set to sleep_state when initializing */
3640 dapm_pinctrl_event(w, NULL, SND_SOC_DAPM_POST_PMD);
3642 case snd_soc_dapm_clock_supply:
3643 w->clk = devm_clk_get(dapm->dev, w->name);
3644 if (IS_ERR(w->clk)) {
3645 ret = PTR_ERR(w->clk);
3646 goto request_failed;
3654 case snd_soc_dapm_mic:
3655 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3656 w->power_check = dapm_generic_check_power;
3658 case snd_soc_dapm_input:
3659 if (!dapm->card->fully_routed)
3660 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3661 w->power_check = dapm_generic_check_power;
3663 case snd_soc_dapm_spk:
3664 case snd_soc_dapm_hp:
3665 w->is_ep = SND_SOC_DAPM_EP_SINK;
3666 w->power_check = dapm_generic_check_power;
3668 case snd_soc_dapm_output:
3669 if (!dapm->card->fully_routed)
3670 w->is_ep = SND_SOC_DAPM_EP_SINK;
3671 w->power_check = dapm_generic_check_power;
3673 case snd_soc_dapm_vmid:
3674 case snd_soc_dapm_siggen:
3675 w->is_ep = SND_SOC_DAPM_EP_SOURCE;
3676 w->power_check = dapm_always_on_check_power;
3678 case snd_soc_dapm_sink:
3679 w->is_ep = SND_SOC_DAPM_EP_SINK;
3680 w->power_check = dapm_always_on_check_power;
3683 case snd_soc_dapm_mux:
3684 case snd_soc_dapm_demux:
3685 case snd_soc_dapm_switch:
3686 case snd_soc_dapm_mixer:
3687 case snd_soc_dapm_mixer_named_ctl:
3688 case snd_soc_dapm_adc:
3689 case snd_soc_dapm_aif_out:
3690 case snd_soc_dapm_dac:
3691 case snd_soc_dapm_aif_in:
3692 case snd_soc_dapm_pga:
3693 case snd_soc_dapm_buffer:
3694 case snd_soc_dapm_scheduler:
3695 case snd_soc_dapm_effect:
3696 case snd_soc_dapm_src:
3697 case snd_soc_dapm_asrc:
3698 case snd_soc_dapm_encoder:
3699 case snd_soc_dapm_decoder:
3700 case snd_soc_dapm_out_drv:
3701 case snd_soc_dapm_micbias:
3702 case snd_soc_dapm_line:
3703 case snd_soc_dapm_dai_link:
3704 case snd_soc_dapm_dai_out:
3705 case snd_soc_dapm_dai_in:
3706 w->power_check = dapm_generic_check_power;
3708 case snd_soc_dapm_supply:
3709 case snd_soc_dapm_regulator_supply:
3710 case snd_soc_dapm_pinctrl:
3711 case snd_soc_dapm_clock_supply:
3712 case snd_soc_dapm_kcontrol:
3714 w->power_check = dapm_supply_check_power;
3717 w->power_check = dapm_always_on_check_power;
3722 INIT_LIST_HEAD(&w->list);
3723 INIT_LIST_HEAD(&w->dirty);
3724 /* see for_each_card_widgets */
3725 list_add_tail(&w->list, &dapm->card->widgets);
3727 snd_soc_dapm_for_each_direction(dir) {
3728 INIT_LIST_HEAD(&w->edges[dir]);
3729 w->endpoints[dir] = -1;
3732 /* machine layer sets up unconnected pins and insertions */
3737 dev_err_probe(dapm->dev, ret, "ASoC: Failed to request %s\n",
3739 kfree_const(w->name);
3741 kfree_const(w->sname);
3744 return ERR_PTR(ret);
3748 * snd_soc_dapm_new_control - create new dapm control
3749 * @dapm: DAPM context
3750 * @widget: widget template
3752 * Creates new DAPM control based upon a template.
3754 * Returns a widget pointer on success or an error pointer on failure
3756 struct snd_soc_dapm_widget *
3757 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3758 const struct snd_soc_dapm_widget *widget)
3760 struct snd_soc_dapm_widget *w;
3762 snd_soc_dapm_mutex_lock(dapm);
3763 w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3764 snd_soc_dapm_mutex_unlock(dapm);
3768 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
3771 * snd_soc_dapm_new_controls - create new dapm controls
3772 * @dapm: DAPM context
3773 * @widget: widget array
3774 * @num: number of widgets
3776 * Creates new DAPM controls based upon the templates.
3778 * Returns 0 for success else error.
3780 int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3781 const struct snd_soc_dapm_widget *widget,
3787 snd_soc_dapm_mutex_lock_root(dapm);
3788 for (i = 0; i < num; i++) {
3789 struct snd_soc_dapm_widget *w = snd_soc_dapm_new_control_unlocked(dapm, widget);
3796 snd_soc_dapm_mutex_unlock(dapm);
3799 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3802 snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
3803 struct snd_pcm_substream *substream)
3805 struct snd_soc_dapm_path *path;
3806 struct snd_soc_dai *source, *sink;
3807 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
3808 struct snd_pcm_hw_params *params = NULL;
3809 const struct snd_soc_pcm_stream *config = NULL;
3810 struct snd_pcm_runtime *runtime = NULL;
3817 * snd_pcm_hw_params is quite large (608 bytes on arm64) and is
3818 * starting to get a bit excessive for allocation on the stack,
3819 * especially when you're building with some of the KASAN type
3820 * stuff that increases stack usage.
3821 * So, we use kzalloc()/kfree() for params in this function.
3823 params = kzalloc(sizeof(*params), GFP_KERNEL);
3827 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
3833 substream->runtime = runtime;
3835 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3836 snd_soc_dapm_widget_for_each_source_path(w, path) {
3837 source = path->source->priv;
3839 ret = snd_soc_dai_startup(source, substream);
3843 snd_soc_dai_activate(source, substream->stream);
3846 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3847 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3848 sink = path->sink->priv;
3850 ret = snd_soc_dai_startup(sink, substream);
3854 snd_soc_dai_activate(sink, substream->stream);
3857 substream->hw_opened = 1;
3860 * Note: getting the config after .startup() gives a chance to
3861 * either party on the link to alter the configuration if
3864 config = rtd->dai_link->c2c_params + rtd->c2c_params_select;
3866 dev_err(w->dapm->dev, "ASoC: link config missing\n");
3871 /* Be a little careful as we don't want to overflow the mask array */
3872 if (!config->formats) {
3873 dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
3879 fmt = ffs(config->formats) - 1;
3881 snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3882 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3884 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3886 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3887 = config->channels_min;
3888 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3889 = config->channels_max;
3891 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3892 snd_soc_dapm_widget_for_each_source_path(w, path) {
3893 source = path->source->priv;
3895 ret = snd_soc_dai_hw_params(source, substream, params);
3899 dapm_update_dai_unlocked(substream, params, source);
3902 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3903 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3904 sink = path->sink->priv;
3906 ret = snd_soc_dai_hw_params(sink, substream, params);
3910 dapm_update_dai_unlocked(substream, params, sink);
3913 runtime->format = params_format(params);
3914 runtime->subformat = params_subformat(params);
3915 runtime->channels = params_channels(params);
3916 runtime->rate = params_rate(params);
3919 /* see above NOTE */
3925 static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3926 struct snd_kcontrol *kcontrol, int event)
3928 struct snd_soc_dapm_path *path;
3929 struct snd_soc_dai *source, *sink;
3930 struct snd_pcm_substream *substream = w->priv;
3931 int ret = 0, saved_stream = substream->stream;
3933 if (WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
3934 list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
3938 case SND_SOC_DAPM_PRE_PMU:
3939 ret = snd_soc_dai_link_event_pre_pmu(w, substream);
3945 case SND_SOC_DAPM_POST_PMU:
3946 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3947 sink = path->sink->priv;
3949 snd_soc_dai_digital_mute(sink, 0, SNDRV_PCM_STREAM_PLAYBACK);
3954 case SND_SOC_DAPM_PRE_PMD:
3955 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3956 sink = path->sink->priv;
3958 snd_soc_dai_digital_mute(sink, 1, SNDRV_PCM_STREAM_PLAYBACK);
3962 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3963 snd_soc_dapm_widget_for_each_source_path(w, path) {
3964 source = path->source->priv;
3965 snd_soc_dai_hw_free(source, substream, 0);
3968 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3969 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3970 sink = path->sink->priv;
3971 snd_soc_dai_hw_free(sink, substream, 0);
3974 substream->stream = SNDRV_PCM_STREAM_CAPTURE;
3975 snd_soc_dapm_widget_for_each_source_path(w, path) {
3976 source = path->source->priv;
3977 snd_soc_dai_deactivate(source, substream->stream);
3978 snd_soc_dai_shutdown(source, substream, 0);
3981 substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
3982 snd_soc_dapm_widget_for_each_sink_path(w, path) {
3983 sink = path->sink->priv;
3984 snd_soc_dai_deactivate(sink, substream->stream);
3985 snd_soc_dai_shutdown(sink, substream, 0);
3989 case SND_SOC_DAPM_POST_PMD:
3990 kfree(substream->runtime);
3994 WARN(1, "Unknown event %d\n", event);
3999 /* Restore the substream direction */
4000 substream->stream = saved_stream;
4004 static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
4005 struct snd_ctl_elem_value *ucontrol)
4007 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4008 struct snd_soc_pcm_runtime *rtd = w->priv;
4010 ucontrol->value.enumerated.item[0] = rtd->c2c_params_select;
4015 static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
4016 struct snd_ctl_elem_value *ucontrol)
4018 struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
4019 struct snd_soc_pcm_runtime *rtd = w->priv;
4021 /* Can't change the config when widget is already powered */
4025 if (ucontrol->value.enumerated.item[0] == rtd->c2c_params_select)
4028 if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_c2c_params)
4031 rtd->c2c_params_select = ucontrol->value.enumerated.item[0];
4037 snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
4038 unsigned long *private_value,
4040 const char **w_param_text)
4044 devm_kfree(card->dev, (void *)*private_value);
4049 for (count = 0 ; count < num_c2c_params; count++)
4050 devm_kfree(card->dev, (void *)w_param_text[count]);
4051 devm_kfree(card->dev, w_param_text);
4054 static struct snd_kcontrol_new *
4055 snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
4057 const struct snd_soc_pcm_stream *c2c_params,
4058 int num_c2c_params, const char **w_param_text,
4059 unsigned long *private_value)
4061 struct soc_enum w_param_enum[] = {
4062 SOC_ENUM_SINGLE(0, 0, 0, NULL),
4064 struct snd_kcontrol_new kcontrol_dai_link[] = {
4065 SOC_ENUM_EXT(NULL, w_param_enum[0],
4066 snd_soc_dapm_dai_link_get,
4067 snd_soc_dapm_dai_link_put),
4069 struct snd_kcontrol_new *kcontrol_news;
4070 const struct snd_soc_pcm_stream *config = c2c_params;
4073 for (count = 0 ; count < num_c2c_params; count++) {
4074 if (!config->stream_name) {
4075 dev_warn(card->dapm.dev,
4076 "ASoC: anonymous config %d for dai link %s\n",
4078 w_param_text[count] =
4079 devm_kasprintf(card->dev, GFP_KERNEL,
4080 "Anonymous Configuration %d",
4083 w_param_text[count] = devm_kmemdup(card->dev,
4084 config->stream_name,
4085 strlen(config->stream_name) + 1,
4088 if (!w_param_text[count])
4089 goto outfree_w_param;
4093 w_param_enum[0].items = num_c2c_params;
4094 w_param_enum[0].texts = w_param_text;
4097 (unsigned long) devm_kmemdup(card->dev,
4098 (void *)(kcontrol_dai_link[0].private_value),
4099 sizeof(struct soc_enum), GFP_KERNEL);
4100 if (!*private_value) {
4101 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4103 goto outfree_w_param;
4105 kcontrol_dai_link[0].private_value = *private_value;
4106 /* duplicate kcontrol_dai_link on heap so that memory persists */
4107 kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
4108 sizeof(struct snd_kcontrol_new),
4110 if (!kcontrol_news) {
4111 dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
4113 goto outfree_w_param;
4115 return kcontrol_news;
4118 snd_soc_dapm_free_kcontrol(card, private_value, num_c2c_params, w_param_text);
4122 static struct snd_soc_dapm_widget *
4123 snd_soc_dapm_new_dai(struct snd_soc_card *card,
4124 struct snd_pcm_substream *substream,
4127 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4128 struct snd_soc_dapm_widget template;
4129 struct snd_soc_dapm_widget *w;
4130 const struct snd_kcontrol_new *kcontrol_news;
4132 const char **w_param_text;
4133 unsigned long private_value = 0;
4137 link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
4138 rtd->dai_link->name, id);
4142 /* allocate memory for control, only in case of multiple configs */
4143 w_param_text = NULL;
4144 kcontrol_news = NULL;
4146 if (rtd->dai_link->num_c2c_params > 1) {
4147 w_param_text = devm_kcalloc(card->dev,
4148 rtd->dai_link->num_c2c_params,
4149 sizeof(char *), GFP_KERNEL);
4154 kcontrol_news = snd_soc_dapm_alloc_kcontrol(card, link_name,
4155 rtd->dai_link->c2c_params,
4156 rtd->dai_link->num_c2c_params,
4157 w_param_text, &private_value);
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 = kcontrol_news;
4170 template.num_kcontrols = num_kcontrols;
4172 dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
4174 w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
4177 goto outfree_kcontrol_news;
4180 w->priv = substream;
4184 outfree_kcontrol_news:
4185 devm_kfree(card->dev, (void *)template.kcontrol_news);
4186 snd_soc_dapm_free_kcontrol(card, &private_value,
4187 rtd->dai_link->num_c2c_params, w_param_text);
4189 devm_kfree(card->dev, link_name);
4191 dev_err(rtd->dev, "ASoC: Failed to create %s-%s widget: %d\n",
4192 rtd->dai_link->name, id, ret);
4193 return ERR_PTR(ret);
4197 * snd_soc_dapm_new_dai_widgets - Create new DAPM widgets
4198 * @dapm: DAPM context
4201 * Returns 0 on success, error code otherwise.
4203 int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
4204 struct snd_soc_dai *dai)
4206 struct snd_soc_dapm_widget template;
4207 struct snd_soc_dapm_widget *w;
4209 WARN_ON(dapm->dev != dai->dev);
4211 memset(&template, 0, sizeof(template));
4212 template.reg = SND_SOC_NOPM;
4214 if (dai->driver->playback.stream_name) {
4215 template.id = snd_soc_dapm_dai_in;
4216 template.name = dai->driver->playback.stream_name;
4217 template.sname = dai->driver->playback.stream_name;
4219 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4222 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4227 snd_soc_dai_set_widget_playback(dai, w);
4230 if (dai->driver->capture.stream_name) {
4231 template.id = snd_soc_dapm_dai_out;
4232 template.name = dai->driver->capture.stream_name;
4233 template.sname = dai->driver->capture.stream_name;
4235 dev_dbg(dai->dev, "ASoC: adding %s widget\n",
4238 w = snd_soc_dapm_new_control_unlocked(dapm, &template);
4243 snd_soc_dai_set_widget_capture(dai, w);
4248 EXPORT_SYMBOL_GPL(snd_soc_dapm_new_dai_widgets);
4250 int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
4252 struct snd_soc_dapm_widget *dai_w, *w;
4253 struct snd_soc_dapm_widget *src, *sink;
4254 struct snd_soc_dai *dai;
4256 /* For each DAI widget... */
4257 for_each_card_widgets(card, dai_w) {
4258 switch (dai_w->id) {
4259 case snd_soc_dapm_dai_in:
4260 case snd_soc_dapm_dai_out:
4266 /* let users know there is no DAI to link */
4268 dev_dbg(card->dev, "dai widget %s has no DAI\n",
4275 /* ...find all widgets with the same stream and link them */
4276 for_each_card_widgets(card, w) {
4277 if (w->dapm != dai_w->dapm)
4281 case snd_soc_dapm_dai_in:
4282 case snd_soc_dapm_dai_out:
4288 if (!w->sname || !strstr(w->sname, dai_w->sname))
4291 if (dai_w->id == snd_soc_dapm_dai_in) {
4298 dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
4299 snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
4306 static void dapm_connect_dai_routes(struct snd_soc_dapm_context *dapm,
4307 struct snd_soc_dai *src_dai,
4308 struct snd_soc_dapm_widget *src,
4309 struct snd_soc_dapm_widget *dai,
4310 struct snd_soc_dai *sink_dai,
4311 struct snd_soc_dapm_widget *sink)
4313 dev_dbg(dapm->dev, "connected DAI link %s:%s -> %s:%s\n",
4314 src_dai->component->name, src->name,
4315 sink_dai->component->name, sink->name);
4318 snd_soc_dapm_add_path(dapm, src, dai, NULL, NULL);
4322 snd_soc_dapm_add_path(dapm, src, sink, NULL, NULL);
4325 static int get_stream_cpu(struct snd_soc_dai_link *dai_link, int stream)
4331 * CPU : SNDRV_PCM_STREAM_PLAYBACK
4332 * Codec: SNDRV_PCM_STREAM_PLAYBACK
4335 * CPU : SNDRV_PCM_STREAM_CAPTURE
4336 * Codec: SNDRV_PCM_STREAM_CAPTURE
4338 if (!dai_link->c2c_params)
4345 * CPU : SNDRV_PCM_STREAM_CAPTURE
4346 * Codec: SNDRV_PCM_STREAM_PLAYBACK
4349 * CPU : SNDRV_PCM_STREAM_PLAYBACK
4350 * Codec: SNDRV_PCM_STREAM_CAPTURE
4352 if (stream == SNDRV_PCM_STREAM_CAPTURE)
4353 return SNDRV_PCM_STREAM_PLAYBACK;
4355 return SNDRV_PCM_STREAM_CAPTURE;
4358 static void dapm_connect_dai_pair(struct snd_soc_card *card,
4359 struct snd_soc_pcm_runtime *rtd,
4360 struct snd_soc_dai *codec_dai,
4361 struct snd_soc_dai *cpu_dai)
4363 struct snd_soc_dai_link *dai_link = rtd->dai_link;
4364 struct snd_soc_dapm_widget *codec, *cpu;
4365 struct snd_soc_dai *src_dai[] = { cpu_dai, codec_dai };
4366 struct snd_soc_dai *sink_dai[] = { codec_dai, cpu_dai };
4367 struct snd_soc_dapm_widget **src[] = { &cpu, &codec };
4368 struct snd_soc_dapm_widget **sink[] = { &codec, &cpu };
4369 char *widget_name[] = { "playback", "capture" };
4372 for_each_pcm_streams(stream) {
4373 int stream_cpu, stream_codec;
4375 stream_cpu = get_stream_cpu(dai_link, stream);
4376 stream_codec = stream;
4378 /* connect BE DAI playback if widgets are valid */
4379 cpu = snd_soc_dai_get_widget(cpu_dai, stream_cpu);
4380 codec = snd_soc_dai_get_widget(codec_dai, stream_codec);
4385 /* special handling for [Codec2Codec] */
4386 if (dai_link->c2c_params && !rtd->c2c_widget[stream]) {
4387 struct snd_pcm_substream *substream = rtd->pcm->streams[stream].substream;
4388 struct snd_soc_dapm_widget *dai = snd_soc_dapm_new_dai(card, substream,
4389 widget_name[stream]);
4394 rtd->c2c_widget[stream] = dai;
4397 dapm_connect_dai_routes(&card->dapm, src_dai[stream], *src[stream],
4398 rtd->c2c_widget[stream],
4399 sink_dai[stream], *sink[stream]);
4403 static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
4406 struct snd_soc_dapm_widget *w;
4408 w = snd_soc_dai_get_widget(dai, stream);
4413 dapm_mark_dirty(w, "stream event");
4415 if (w->id == snd_soc_dapm_dai_in) {
4416 ep = SND_SOC_DAPM_EP_SOURCE;
4417 dapm_widget_invalidate_input_paths(w);
4419 ep = SND_SOC_DAPM_EP_SINK;
4420 dapm_widget_invalidate_output_paths(w);
4424 case SND_SOC_DAPM_STREAM_START:
4428 case SND_SOC_DAPM_STREAM_STOP:
4432 case SND_SOC_DAPM_STREAM_SUSPEND:
4433 case SND_SOC_DAPM_STREAM_RESUME:
4434 case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
4435 case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
4441 void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
4443 struct snd_soc_pcm_runtime *rtd;
4444 struct snd_soc_dai *codec_dai;
4447 /* for each BE DAI link... */
4448 for_each_card_rtds(card, rtd) {
4450 * dynamic FE links have no fixed DAI mapping.
4451 * CODEC<->CODEC links have no direct connection.
4453 if (rtd->dai_link->dynamic)
4456 if (rtd->dai_link->num_cpus == 1) {
4457 for_each_rtd_codec_dais(rtd, i, codec_dai)
4458 dapm_connect_dai_pair(card, rtd, codec_dai,
4459 asoc_rtd_to_cpu(rtd, 0));
4460 } else if (rtd->dai_link->num_codecs == rtd->dai_link->num_cpus) {
4461 for_each_rtd_codec_dais(rtd, i, codec_dai)
4462 dapm_connect_dai_pair(card, rtd, codec_dai,
4463 asoc_rtd_to_cpu(rtd, i));
4466 "N cpus to M codecs link is not supported yet\n");
4471 static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4474 struct snd_soc_dai *dai;
4477 for_each_rtd_dais(rtd, i, dai)
4478 soc_dapm_dai_stream_event(dai, stream, event);
4480 dapm_power_widgets(rtd->card, event);
4484 * snd_soc_dapm_stream_event - send a stream event to the dapm core
4485 * @rtd: PCM runtime data
4486 * @stream: stream name
4487 * @event: stream event
4489 * Sends a stream event to the dapm core. The core then makes any
4490 * necessary widget power changes.
4492 * Returns 0 for success else error.
4494 void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
4497 struct snd_soc_card *card = rtd->card;
4499 snd_soc_dapm_mutex_lock(card);
4500 soc_dapm_stream_event(rtd, stream, event);
4501 snd_soc_dapm_mutex_unlock(card);
4504 void snd_soc_dapm_stream_stop(struct snd_soc_pcm_runtime *rtd, int stream)
4506 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
4507 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
4508 /* powered down playback stream now */
4509 snd_soc_dapm_stream_event(rtd,
4510 SNDRV_PCM_STREAM_PLAYBACK,
4511 SND_SOC_DAPM_STREAM_STOP);
4513 /* start delayed pop wq here for playback streams */
4515 queue_delayed_work(system_power_efficient_wq,
4517 msecs_to_jiffies(rtd->pmdown_time));
4520 /* capture streams can be powered down now */
4521 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
4522 SND_SOC_DAPM_STREAM_STOP);
4525 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_stop);
4528 * snd_soc_dapm_enable_pin_unlocked - enable pin.
4529 * @dapm: DAPM context
4532 * Enables input/output pin and its parents or children widgets iff there is
4533 * a valid audio route and active audio stream.
4535 * Requires external locking.
4537 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4538 * do any widget power switching.
4540 int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4543 return snd_soc_dapm_set_pin(dapm, pin, 1);
4545 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
4548 * snd_soc_dapm_enable_pin - enable pin.
4549 * @dapm: DAPM context
4552 * Enables input/output pin and its parents or children widgets iff there is
4553 * a valid audio route and active audio stream.
4555 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4556 * do any widget power switching.
4558 int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4562 snd_soc_dapm_mutex_lock(dapm);
4564 ret = snd_soc_dapm_set_pin(dapm, pin, 1);
4566 snd_soc_dapm_mutex_unlock(dapm);
4570 EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
4573 * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
4574 * @dapm: DAPM context
4577 * Enables input/output pin regardless of any other state. This is
4578 * intended for use with microphone bias supplies used in microphone
4581 * Requires external locking.
4583 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4584 * do any widget power switching.
4586 int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4589 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4592 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4596 dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
4597 if (!w->connected) {
4599 * w->force does not affect the number of input or output paths,
4600 * so we only have to recheck if w->connected is changed
4602 dapm_widget_invalidate_input_paths(w);
4603 dapm_widget_invalidate_output_paths(w);
4607 dapm_mark_dirty(w, "force enable");
4611 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
4614 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
4615 * @dapm: DAPM context
4618 * Enables input/output pin regardless of any other state. This is
4619 * intended for use with microphone bias supplies used in microphone
4622 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4623 * do any widget power switching.
4625 int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
4630 snd_soc_dapm_mutex_lock(dapm);
4632 ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
4634 snd_soc_dapm_mutex_unlock(dapm);
4638 EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
4641 * snd_soc_dapm_disable_pin_unlocked - disable pin.
4642 * @dapm: DAPM context
4645 * Disables input/output pin and its parents or children widgets.
4647 * Requires external locking.
4649 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4650 * do any widget power switching.
4652 int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
4655 return snd_soc_dapm_set_pin(dapm, pin, 0);
4657 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
4660 * snd_soc_dapm_disable_pin - disable pin.
4661 * @dapm: DAPM context
4664 * Disables input/output pin and its parents or children widgets.
4666 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4667 * do any widget power switching.
4669 int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
4674 snd_soc_dapm_mutex_lock(dapm);
4676 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4678 snd_soc_dapm_mutex_unlock(dapm);
4682 EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
4685 * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
4686 * @dapm: DAPM context
4689 * Marks the specified pin as being not connected, disabling it along
4690 * any parent or child widgets. At present this is identical to
4691 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4692 * additional things such as disabling controls which only affect
4693 * paths through the pin.
4695 * Requires external locking.
4697 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4698 * do any widget power switching.
4700 int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
4703 return snd_soc_dapm_set_pin(dapm, pin, 0);
4705 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
4708 * snd_soc_dapm_nc_pin - permanently disable pin.
4709 * @dapm: DAPM context
4712 * Marks the specified pin as being not connected, disabling it along
4713 * any parent or child widgets. At present this is identical to
4714 * snd_soc_dapm_disable_pin() but in future it will be extended to do
4715 * additional things such as disabling controls which only affect
4716 * paths through the pin.
4718 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
4719 * do any widget power switching.
4721 int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
4725 snd_soc_dapm_mutex_lock(dapm);
4727 ret = snd_soc_dapm_set_pin(dapm, pin, 0);
4729 snd_soc_dapm_mutex_unlock(dapm);
4733 EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
4736 * snd_soc_dapm_get_pin_status - get audio pin status
4737 * @dapm: DAPM context
4738 * @pin: audio signal pin endpoint (or start point)
4740 * Get audio pin status - connected or disconnected.
4742 * Returns 1 for connected otherwise 0.
4744 int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
4747 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
4750 return w->connected;
4754 EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
4757 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
4758 * @dapm: DAPM context
4759 * @pin: audio signal pin endpoint (or start point)
4761 * Mark the given endpoint or pin as ignoring suspend. When the
4762 * system is disabled a path between two endpoints flagged as ignoring
4763 * suspend will not be disabled. The path must already be enabled via
4764 * normal means at suspend time, it will not be turned on if it was not
4767 int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
4770 struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
4773 dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
4777 w->ignore_suspend = 1;
4781 EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
4784 * snd_soc_dapm_free - free dapm resources
4785 * @dapm: DAPM context
4787 * Free all dapm widgets and resources.
4789 void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
4791 dapm_debugfs_cleanup(dapm);
4792 dapm_free_widgets(dapm);
4793 list_del(&dapm->list);
4795 EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
4797 void snd_soc_dapm_init(struct snd_soc_dapm_context *dapm,
4798 struct snd_soc_card *card,
4799 struct snd_soc_component *component)
4802 dapm->component = component;
4803 dapm->bias_level = SND_SOC_BIAS_OFF;
4806 dapm->dev = component->dev;
4807 dapm->idle_bias_off = !component->driver->idle_bias_on;
4808 dapm->suspend_bias_off = component->driver->suspend_bias_off;
4810 dapm->dev = card->dev;
4813 INIT_LIST_HEAD(&dapm->list);
4814 /* see for_each_card_dapms */
4815 list_add(&dapm->list, &card->dapm_list);
4817 EXPORT_SYMBOL_GPL(snd_soc_dapm_init);
4819 static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
4821 struct snd_soc_card *card = dapm->card;
4822 struct snd_soc_dapm_widget *w;
4823 LIST_HEAD(down_list);
4826 snd_soc_dapm_mutex_lock_root(card);
4828 for_each_card_widgets(dapm->card, w) {
4829 if (w->dapm != dapm)
4832 dapm_seq_insert(w, &down_list, false);
4838 /* If there were no widgets to power down we're already in
4842 if (dapm->bias_level == SND_SOC_BIAS_ON)
4843 snd_soc_dapm_set_bias_level(dapm,
4844 SND_SOC_BIAS_PREPARE);
4845 dapm_seq_run(card, &down_list, 0, false);
4846 if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
4847 snd_soc_dapm_set_bias_level(dapm,
4848 SND_SOC_BIAS_STANDBY);
4851 snd_soc_dapm_mutex_unlock(card);
4855 * snd_soc_dapm_shutdown - callback for system shutdown
4857 void snd_soc_dapm_shutdown(struct snd_soc_card *card)
4859 struct snd_soc_dapm_context *dapm;
4861 for_each_card_dapms(card, dapm) {
4862 if (dapm != &card->dapm) {
4863 soc_dapm_shutdown_dapm(dapm);
4864 if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
4865 snd_soc_dapm_set_bias_level(dapm,
4870 soc_dapm_shutdown_dapm(&card->dapm);
4871 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
4872 snd_soc_dapm_set_bias_level(&card->dapm,
4876 /* Module information */
4877 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4878 MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
4879 MODULE_LICENSE("GPL");